-
Notifications
You must be signed in to change notification settings - Fork 4
/
lpvartypes.pas
4549 lines (3937 loc) · 154 KB
/
lpvartypes.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
Author: Niels A.D
Project: Lape (http://code.google.com/p/la-pe/)
License: GNU Lesser GPL (http://www.gnu.org/licenses/lgpl.html)
All (script)type and (script)variable classes, including corresponding evaluation functions (runtime/compile time).
}
unit lpvartypes;
{$I lape.inc}
interface
uses
Classes, SysUtils,
lptypes, lpparser, lpcodeemitter;
type
ECompilerOption = (
lcoAssertions, // {$C} {$ASSERTIONS}
lcoRangeCheck, // {$R} {$RANGECHECKS} TODO
lcoShortCircuit, // {$B} {$BOOLEVAL}
lcoAlwaysInitialize, // {$M} {$MEMORYINIT}
lcoFullDisposal, // {$D} {$FULLDISPOSAL}
lcoLooseSemicolon, // {$L} {$LOOSESEMICOLON}
lcoLooseSyntax, // {$X} {$EXTENDEDSYNTAX}
lcoAutoInvoke, // {$F} {$AUTOINVOKE}
lcoAutoProperties, // {$P} {$AUTOPROPERTIES}
lcoScopedEnums, // {$S} {$SCOPEDENUMS}
lcoConstAddress, // {$J} {$CONSTADDRESS}
lcoContinueCase, // {$CONTINUECASE}
lcoCOperators, // {$COPERATORS}
lcoInitExternalResult // Ensure empty result for external calls (useful for ffi)
);
ECompilerOptionsSet = set of ECompilerOption;
PCompilerOptionsSet = ^ECompilerOptionsSet;
const
Lape_OptionsDef = [lcoCOperators, lcoRangeCheck, lcoShortCircuit, lcoAlwaysInitialize, lcoAutoInvoke, lcoConstAddress];
Lape_PackRecordsDef = 8;
type
TLapeType = class;
TLapeVar = class;
TLapeStackVar = class;
TLapeStackInfo = class;
TLapeGlobalVar = class;
TLapeType_OverloadedMethod = class;
TLapeCompilerBase = class;
TLapeBaseTypes = array[ELapeBaseType] of TLapeType;
TLapeTypeArray = array of TLapeType;
TLapeVarStack = {$IFDEF FPC}specialize{$ENDIF} TLapeList<TLapeStackVar>;
TLapeVarMap = {$IFDEF FPC}specialize{$ENDIF} TLapeStringMap<TLapeGlobalVar>;
TVarPos = record
isPointer: Boolean;
Offset: Integer;
case MemPos: EMemoryPos of
mpVar: (StackVar: TLapeStackVar);
mpMem: (GlobalVar: TLapeGlobalVar);
mpStack: (ForceVariable: Boolean);
end;
PResVar = ^TResVar;
TResVar = {$IFDEF FPC}object{$ELSE}record{$ENDIF}
private
function getReadable: Boolean;
function getWriteable: Boolean;
function getConstant: Boolean;
public
VarType: TLapeType;
VarPos: TVarPos;
class function New(AVar: TLapeVar): TResVar; {$IFNDEF FPC}static;{$ENDIF}
function HasType: Boolean; {$IFDEF Lape_Inline}inline;{$ENDIF}
procedure Spill(Unlock: Integer = 0); {$IFDEF Lape_Inline}inline;{$ENDIF}
function IncLock(Count: Integer = 1): TResVar; {$IFDEF Lape_Inline}inline;{$ENDIF}
function DecLock(Count: Integer = 1): TResVar; {$IFDEF Lape_Inline}inline;{$ENDIF}
function InScope(AStack: TLapeStackInfo; Pos: PDocPos = nil): TResVar; {$IFDEF Lape_Inline}inline;{$ENDIF}
procedure IncOffset(Offset: Integer); {$IFDEF Lape_Inline}inline;{$ENDIF}
procedure DecOffset(Offset: Integer); {$IFDEF Lape_Inline}inline;{$ENDIF}
procedure setOffset(Offset: Integer); {$IFDEF Lape_Inline}inline;{$ENDIF}
procedure setReadable(AReadable: Boolean; ChangeStack: Boolean); overload;
procedure setReadable(AReadable: Boolean); overload;
procedure setWriteable(AWriteable: Boolean; ChangeStack: Boolean); overload;
procedure setWriteable(AWriteable: Boolean); overload;
procedure setConstant(AConst: Boolean; ChangeStack: Boolean); overload;
procedure setConstant(AConst: Boolean); overload;
procedure setReadWrite(AReadable, AWriteable: Boolean);
procedure CopyFlags(Other: TResVar);
property Readable: Boolean read getReadable write setReadable;
property Writeable: Boolean read getWriteable write setWriteable;
property isConstant: Boolean read getConstant write setConstant;
end;
ELapeParameterType = (lptNormal, lptConst, lptConstRef, lptVar, lptOut);
TLapeParameter = record
ParType: ELapeParameterType;
VarType: TLapeType;
Default: TLapeVar;
end;
TLapeParameterList = {$IFDEF FPC}specialize{$ENDIF} TLapeList<TLapeParameter>;
ELapeVarFlag = (lvfReadable, lvfWriteable);
ELapeVarFlags = set of ELapeVarFlag;
TLapeVar = class(TLapeDeclaration)
protected
FVarFlags: ELapeVarFlags;
function getBaseType: ELapeBaseType; virtual;
function getSize: SizeInt; virtual;
function getLo: TLapeGlobalVar; virtual;
function getHi: TLapeGlobalVar; virtual;
function getInitialization: Boolean; virtual;
function getFinalization: Boolean; virtual;
function getReadable: Boolean; virtual;
procedure setReadable(AReadable: Boolean); virtual;
function getWriteable: Boolean; virtual;
procedure setWriteable(AWriteable: Boolean); virtual;
function getConstant: Boolean;
procedure setConstant(AConst: Boolean);
public
VarType: TLapeType;
constructor Create(AVarType: TLapeType; AName: lpString = ''; ADocPos: PDocPos = nil; AList: TLapeDeclarationList = nil); reintroduce; virtual;
function HasType: Boolean;
procedure setReadWrite(AReadable, AWriteable: Boolean);
procedure CopyFlags(Other: TLapeVar);
property BaseType: ELapeBaseType read getBaseType;
property Size: SizeInt read getSize;
property Lo: TLapeGlobalVar read getLo;
property Hi: TLapeGlobalVar read getHi;
property NeedInitialization: Boolean read getInitialization;
property NeedFinalization: Boolean read getFinalization;
property Readable: Boolean read getReadable write setReadable;
property Writeable: Boolean read getWriteable write setWriteable;
property isConstant: Boolean read getConstant write setConstant;
end;
TLapeStackVar = class(TLapeVar)
protected
FStack: TLapeVarStack;
procedure setStack(Stack: TLapeVarStack); virtual;
function getOffset: Integer; virtual;
public
constructor Create(AVarType: TLapeType; AStack: TLapeVarStack; AName: lpString = ''; ADocPos: PDocPos = nil; AList: TLapeDeclarationList = nil); reintroduce; virtual;
destructor Destroy; override;
property Stack: TLapeVarStack read FStack write setStack;
property Offset: Integer read getOffset;
end;
TLapeStackTempVar = class(TLapeStackVar)
protected
FLock: Integer;
function getLocked: Boolean;
procedure setLocked(DoLock: Boolean);
public
constructor Create(AVarType: TLapeType; AStack: TLapeVarStack; AName: lpString = ''; ADocPos: PDocPos = nil; AList: TLapeDeclarationList = nil); override;
function IncLock(Count: Integer = 1): Integer; virtual;
function Declock(Count: Integer = 1): Integer; virtual;
property Locked: Boolean read getLocked write setLocked;
end;
TLapeParameterVar = class(TLapeStackVar)
protected
FParType: ELapeParameterType;
function getSize: SizeInt; override;
function getInitialization: Boolean; override;
function getFinalization: Boolean; override;
public
constructor Create(AParType: ELapeParameterType; AVarType: TLapeType; AStack: TLapeVarStack; AName: lpString = ''; ADocPos: PDocPos = nil; AList: TLapeDeclarationList = nil); reintroduce; virtual;
property ParType: ELapeParameterType read FParType;
end;
TLapeStackInheritedVar = class(TLapeParameterVar)
protected
FParent: TLapeStackVar;
public
constructor Create(AParent: TLapeStackVar; AStack: TLapeVarStack; AList: TLapeDeclarationList = nil); reintroduce; virtual;
property Parent: TLapeStackVar read FParent;
end;
TLapeGlobalVar = class(TLapeVar)
protected
FBasePtr: Pointer;
FPtr: Pointer;
function getAsString: lpString; virtual;
function getAsInt: Int64; virtual;
public
DoManage: Boolean;
constructor Create(AVarType: TLapeType; Initialize: Boolean = True; ManagePtr: Boolean = True; AName: lpString = ''; ADocPos: PDocPos = nil; AList: TLapeDeclarationList = nil); reintroduce; overload; virtual;
constructor Create(AVarType: TLapeType; Ptr: Pointer; ManagePtr: Boolean = False; AName: lpString = ''; ADocPos: PDocPos = nil; AList: TLapeDeclarationList = nil); reintroduce; overload; virtual;
destructor Destroy; override;
function CreateCopy(CopyContent: Boolean = True): TLapeGlobalVar; virtual;
function CompatibleWith(Other: TLapeGlobalVar): Boolean; virtual;
function isNull: Boolean; virtual;
function Equals(Other: TLapeGlobalVar): Boolean; reintroduce; virtual;
property Ptr: Pointer read FPtr;
property AsString: lpString read getAsString;
property AsInteger: Int64 read getAsInt;
end;
ELapeEvalFlag = (lefAssigning, lefInvoking, lefConstAddress, lefConstRangeCheck, lefRangeCheck);
ELapeEvalFlags = set of ELapeEvalFlag;
TLapeType = class(TLapeManagingDeclaration)
protected
FBaseType: ELapeBaseType;
FCompiler: TLapeCompilerBase;
FSize: SizeInt;
FInit: TInitBool;
FStatic: Boolean;
FLo: TLapeGlobalVar;
FHi: TLapeGlobalVar;
FAsString: lpString;
function getEvalRes(Op: EOperator; Left, Right: ELapeBaseType): ELapeBaseType; virtual;
function getEvalProc(Op: EOperator; Left, Right: ELapeBaseType): TLapeEvalProc; virtual;
procedure setBaseType(ABaseType: ELapeBaseType); virtual;
function getBaseIntType: ELapeBaseType; virtual;
function getPadding: SizeInt; virtual;
function getSize: SizeInt; virtual;
function getInitialization: Boolean; virtual;
function getFinalization: Boolean; virtual;
function getAsString: lpString; virtual;
public
TypeID: Integer;
constructor Create(ABaseType: ELapeBaseType; ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil); reintroduce; virtual;
function CreateCopy(DeepCopy: Boolean = False): TLapeType; virtual;
function Equals(Other: TLapeType; ContextOnly: Boolean = True): Boolean; reintroduce; virtual;
function CompatibleWith(Other: TLapeType): Boolean; virtual;
procedure ClearCache; virtual;
function VarToStringBody(ToStr: TLapeType_OverloadedMethod = nil): lpString; virtual;
function VarToString(AVar: Pointer): lpString; virtual;
function VarToInt(AVar: Pointer): Int64; virtual;
function VarLo(AVar: Pointer = nil): TLapeGlobalVar; virtual;
function VarHi(AVar: Pointer = nil): TLapeGlobalVar; virtual;
function NewGlobalVarP(Ptr: Pointer = nil; AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar; virtual;
function NewGlobalVarStr(Str: AnsiString; AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar; overload; virtual;
function NewGlobalVarStr(Str: UnicodeString; AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar; overload; virtual;
function addSubDeclaration(ADecl: TLapeDeclaration): Integer; override;
function CanHaveChild: Boolean; virtual;
function IsOrdinal(OrPointer: Boolean = False): Boolean; virtual;
function HasChild(AName: lpString): Boolean; overload; virtual;
function HasChild(ADecl: TLapeDeclaration): Boolean; overload; virtual;
function HasConstantChild(Left: TLapeGlobalVar; AName: lpString): Boolean; virtual;
function EvalRes(Op: EOperator; Right: TLapeType = nil; Flags: ELapeEvalFlags = []): TLapeType; overload; virtual;
function EvalRes(Op: EOperator; Right: TLapeGlobalVar; Flags: ELapeEvalFlags = []): TLapeType; overload; virtual;
function CanEvalConst(Op: EOperator; Left, Right: TLapeGlobalVar): Boolean; virtual;
function EvalConst(Op: EOperator; Left, Right: TLapeGlobalVar; Flags: ELapeEvalFlags): TLapeGlobalVar; virtual;
function Eval(Op: EOperator; var Dest: TResVar; Left, Right: TResVar; Flags: ELapeEvalFlags; var Offset: Integer; Pos: PDocPos = nil): TResVar; overload; virtual;
function Eval(Op: EOperator; var Dest: TResVar; Left, Right: TResVar; Flags: ELapeEvalFlags; Pos: PDocPos = nil): TResVar; overload; virtual;
procedure Finalize(AVar: TResVar; var Offset: Integer; UseCompiler: Boolean = True; Pos: PDocPos = nil); overload; virtual;
procedure Finalize(AVar: TResVar; UseCompiler: Boolean = True; Pos: PDocPos = nil); overload; virtual;
procedure Finalize(AVar: TLapeVar; var Offset: Integer; UseCompiler: Boolean = False; Pos: PDocPos = nil); overload; virtual;
procedure Finalize(AVar: TLapeVar; UseCompiler: Boolean = False; Pos: PDocPos = nil); overload; virtual;
property Compiler: TLapeCompilerBase read FCompiler;
property BaseType: ELapeBaseType read FBaseType write setBaseType;
property BaseIntType: ELapeBaseType read getBaseIntType;
property Padding: SizeInt read getPadding;
property Size: SizeInt read getSize;
property IsStatic: Boolean read FStatic;
property NeedInitialization: Boolean read getInitialization;
property NeedFinalization: Boolean read getFinalization;
property AsString: lpString read getAsString;
end;
TLapeTTypeClass = class of TLapeType_Type;
TLapeType_Type = class(TLapeType)
protected
FTType: TLapeType;
function getAsString: lpString; override;
public
constructor Create(AType: TLapeType; ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil); reintroduce; virtual;
function CreateCopy(DeepCopy: Boolean = False): TLapeType; override;
function HasConstantChild(Left: TLapeGlobalVar; AName: lpString): Boolean; override;
function EvalRes(Op: EOperator; Right: TLapeType = nil; Flags: ELapeEvalFlags = []): TLapeType; override;
property TType: TLapeType read FTType;
end;
TLapeType_TypeEnum = class(TLapeType_Type)
public
function CanHaveChild: Boolean; override;
function HasChild(AName: lpString): Boolean; override;
function HasConstantChild(Left: TLapeGlobalVar; AName: lpString): Boolean; override;
function EvalRes(Op: EOperator; Right: TLapeGlobalVar; Flags: ELapeEvalFlags = []): TLapeType; override;
function EvalConst(Op: EOperator; Left, Right: TLapeGlobalVar; Flags: ELapeEvalFlags): TLapeGlobalVar; override;
end;
TLapeType_Pointer = class(TLapeType)
protected
FPType: TLapeType;
FPConst: Boolean;
function getAsString: lpString; override;
public
constructor Create(ACompiler: TLapeCompilerBase; PointerType: TLapeType = nil; ConstPointer: Boolean = True; AName: lpString = ''; ADocPos: PDocPos = nil); reintroduce; virtual;
function CreateCopy(DeepCopy: Boolean = False): TLapeType; override;
function Equals(Other: TLapeType; ContextOnly: Boolean = True): Boolean; override;
function VarToStringBody(ToStr: TLapeType_OverloadedMethod = nil): lpString; override;
function VarToString(AVar: Pointer): lpString; override;
function NewGlobalVar(Ptr: Pointer = nil; AName: lpString = ''; ADocPos: PDocPos = nil; AsValue: Boolean = True): TLapeGlobalVar; virtual;
function NewGlobalVarStr(Str: UnicodeString; AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar; override;
function HasType: Boolean;
function EvalRes(Op: EOperator; Right: TLapeType = nil; Flags: ELapeEvalFlags = []): TLapeType; override;
function EvalConst(Op: EOperator; Left, Right: TLapeGlobalVar; Flags: ELapeEvalFlags): TLapeGlobalVar; override;
function Eval(Op: EOperator; var Dest: TResVar; Left, Right: TResVar; Flags: ELapeEvalFlags; var Offset: Integer; Pos: PDocPos = nil): TResVar; override;
property PType: TLapeType read FPType;
property PConst: Boolean read FPConst;
end;
TLapeType_Label = class(TLapeType_Pointer)
public
constructor Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil); reintroduce; virtual;
function EvalRes(Op: EOperator; Right: TLapeType = nil; Flags: ELapeEvalFlags = []): TLapeType; override;
function EvalConst(Op: EOperator; Left, Right: TLapeGlobalVar; Flags: ELapeEvalFlags): TLapeGlobalVar; override;
function Eval(Op: EOperator; var Dest: TResVar; Left, Right: TResVar; Flags: ELapeEvalFlags; var Offset: Integer; Pos: PDocPos = nil): TResVar; override;
end;
TLapeType_Method = class(TLapeType)
protected
FParams: TLapeParameterList;
procedure setBaseType(ABaseType: ELapeBaseType); override;
function getSize: SizeInt; override;
function getAsString: lpString; override;
function getParamSize: SizeInt; virtual;
function getParamInitialization: Boolean; virtual;
public
FreeParams: Boolean;
ImplicitParams: Integer;
Res: TLapeType;
IsOperator: Boolean;
constructor Create(ACompiler: TLapeCompilerBase; AParams: TLapeParameterList; ARes: TLapeType = nil; AName: lpString = ''; ADocPos: PDocPos = nil); reintroduce; overload; virtual;
constructor Create(ACompiler: TLapeCompilerBase; AParams: array of TLapeType; AParTypes: array of ELapeParameterType; AParDefaults: array of TLapeGlobalVar; ARes: TLapeType = nil; AName: lpString = ''; ADocPos: PDocPos = nil); reintroduce; overload; virtual;
constructor Create(AMethod: TLapeType_Method); overload; virtual;
function CreateCopy(DeepCopy: Boolean = False): TLapeType; override;
destructor Destroy; override;
function Equals(Other: TLapeType; ContextOnly: Boolean = True): Boolean; override;
function VarToStringBody(ToStr: TLapeType_OverloadedMethod = nil): lpString; override;
function EqualParams(Other: TLapeType_Method; ContextOnly: Boolean = True; IgnoreDefault: Boolean = False): Boolean; virtual;
procedure addParam(Param: TLapeParameter); virtual;
procedure setImported(AVar: TLapeGlobalVar; isImported: Boolean); virtual;
function NewGlobalVar(Ptr: Pointer = nil; AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar; overload; virtual;
function NewGlobalVar(CodePos: TCodePos; AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar; overload; virtual;
function EvalRes(Op: EOperator; Right: TLapeType = nil; Flags: ELapeEvalFlags = []): TLapeType; override;
function EvalConst(Op: EOperator; Left, Right: TLapeGlobalVar; Flags: ELapeEvalFlags): TLapeGlobalVar; override;
function Eval(Op: EOperator; var Dest: TResVar; Left, Right: TResVar; Flags: ELapeEvalFlags; var Offset: Integer; Pos: PDocPos = nil): TResVar; override;
property Params: TLapeParameterList read FParams;
property ParamSize: SizeInt read getParamSize;
property ParamInitialization: Boolean read getParamInitialization;
end;
TLapeType_MethodOfObject = class(TLapeType_Method)
protected
FMethodRecord: TLapeType;
function getSize: SizeInt; override;
function getAsString: lpString; override;
function getParamSize: SizeInt; override;
public
constructor Create(ACompiler: TLapeCompilerBase; AParams: TLapeParameterList; ARes: TLapeType = nil; AName: lpString = ''; ADocPos: PDocPos = nil); override;
function Equals(Other: TLapeType; ContextOnly: Boolean = True): Boolean; override;
function VarToStringBody(ToStr: TLapeType_OverloadedMethod = nil): lpString; override;
function NewGlobalVar(AMethod: TMethod; AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar; overload; virtual;
function EvalRes(Op: EOperator; Right: TLapeType = nil; Flags: ELapeEvalFlags = []): TLapeType; override;
function EvalConst(Op: EOperator; Left, Right: TLapeGlobalVar; Flags: ELapeEvalFlags): TLapeGlobalVar; override;
function Eval(Op: EOperator; var Dest: TResVar; Left, Right: TResVar; Flags: ELapeEvalFlags; var Offset: Integer; Pos: PDocPos = nil): TResVar; override;
end;
TLapeType_MethodOfType = class(TLapeType_MethodOfObject)
protected
FObjectType: TLapeType;
function getAsString: lpString; override;
public
SelfParam: ELapeParameterType;
constructor Create(ACompiler: TLapeCompilerBase; AObjType: TLapeType; AParams: TLapeParameterList; ARes: TLapeType = nil; AName: lpString = ''; ADocPos: PDocPos = nil); reintroduce; overload; virtual;
constructor Create(AMethod: TLapeType_Method; AObjType: TLapeType); overload; virtual;
function CreateCopy(DeepCopy: Boolean = False): TLapeType; override;
function Equals(Other: TLapeType; ContextOnly: Boolean = True): Boolean; override;
function EqualParams(Other: TLapeType_Method; ContextOnly: Boolean = True; IgnoreDefault: Boolean = False): Boolean; override;
property ObjectType: TLapeType read FObjectType;
end;
TLapeGetOverloadedMethod = function(Sender: TLapeType_OverloadedMethod; AType: TLapeType_Method;
AParams: TLapeTypeArray = nil; AResult: TLapeType = nil): TLapeGlobalVar of object;
TLapeType_OverloadedMethod = class(TLapeType)
protected
FOfObject: TInitBool;
function getAsString: lpString; override;
public
OnFunctionNotFound: TLapeGetOverloadedMethod;
NeedFullMatch: Boolean;
constructor Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil); reintroduce; virtual;
function CreateCopy(DeepCopy: Boolean = False): TLapeType; override;
function addSubDeclaration(ADecl: TLapeDeclaration): Integer; override;
procedure addMethod(AMethod: TLapeGlobalVar; DoOverride: Boolean = False); virtual;
function overrideMethod(AMethod: TLapeGlobalVar): TLapeGlobalVar; virtual;
function getMethodIndex(AType: TLapeType_Method): Integer; overload; virtual;
function getMethodIndex(AParams: TLapeTypeArray; AResult: TLapeType = nil): Integer; overload; virtual;
function getMethod(AType: TLapeType_Method): TLapeGlobalVar; overload; virtual;
function getMethod(AParams: TLapeTypeArray; AResult: TLapeType = nil): TLapeGlobalVar; overload; virtual;
function NewGlobalVar(AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar; virtual;
function EvalRes(Op: EOperator; Right: TLapeType = nil; Flags: ELapeEvalFlags = []): TLapeType; override;
function EvalRes(Op: EOperator; Right: TLapeGlobalVar; Flags: ELapeEvalFlags = []): TLapeType; override;
function CanEvalConst(Op: EOperator; Left, Right: TLapeGlobalVar): Boolean; override;
function EvalConst(Op: EOperator; Left, Right: TLapeGlobalVar; Flags: ELapeEvalFlags): TLapeGlobalVar; override;
function Eval(Op: EOperator; var Dest: TResVar; Left, Right: TResVar; Flags: ELapeEvalFlags; var Offset: Integer; Pos: PDocPos = nil): TResVar; override;
property MethodsOfObject: TInitBool read FOfObject;
end;
TLapeWithDeclRec = record
WithVar: PResVar;
WithType: TLapeType;
end;
TLapeWithDeclarationList = {$IFDEF FPC}specialize{$ENDIF} TLapeList<TLapeWithDeclRec>;
TLapeWithDeclaration = class(TLapeDeclaration)
protected
FWithDeclRec: TLapeWithDeclRec;
public
constructor Create(AWithDeclRec: TLapeWithDeclRec); reintroduce; virtual;
property WithDeclRec: TLapeWithDeclRec read FWithDeclRec;
end;
TLapeVarRef = record
Lock: Integer;
ResVar: TResVar;
RefVar: TLapeVar;
end;
TLapeVarRefMap = {$IFDEF FPC}specialize{$ENDIF} TLapeStringMap<TLapeVarRef>;
TLapeType_VarRefMap = class(TLapeType)
protected
FVarMap: TLapeVarRefMap;
public
constructor Create(ACompiler: TLapeCompilerBase); reintroduce; virtual;
destructor Destroy; override;
function CanHaveChild: Boolean; override;
function HasChild(AName: lpString): Boolean; override;
function EvalRes(Op: EOperator; Right: TLapeType = nil; Flags: ELapeEvalFlags = []): TLapeType; override;
function EvalRes(Op: EOperator; Right: TLapeGlobalVar; Flags: ELapeEvalFlags = []): TLapeType; override;
function CanEvalConst(Op: EOperator; Left, Right: TLapeGlobalVar): Boolean; override;
function EvalConst(Op: EOperator; Left, Right: TLapeGlobalVar; Flags: ELapeEvalFlags): TLapeGlobalVar; override;
function Eval(Op: EOperator; var Dest: TResVar; Left, Right: TResVar; Flags: ELapeEvalFlags; var Offset: Integer; Pos: PDocPos = nil): TResVar; override;
procedure addVar(RefVar: TLapeVar; AName: lpString); overload; virtual;
procedure addVar(RefVar: TResVar; AName: lpString); overload; virtual;
property VarMap: TLapeVarRefMap read FVarMap;
end;
TLapeStackInfo = class(TLapeDeclarationList)
protected
FVarStack: TLapeVarStack;
FWithStack: TLapeWithDeclarationList;
FOldStackPos: Integer;
FOldMaxStack: Integer;
function getVar(Index: Integer): TLapeStackVar; virtual;
function getVarCount: Integer; virtual;
function getTotalSize: SizeInt; virtual;
function getTotalParamSize: SizeInt; virtual;
function getTotalNoParamSize: SizeInt; virtual;
function getInitialization: Boolean; virtual;
function getFinalization: Boolean; virtual;
public
Owner: TLapeStackInfo;
FreeVars: Boolean;
CodePos: Integer;
ForceInitialization: Boolean;
FullDisposal: Boolean;
constructor Create(AlwaysInitialize: Boolean = True; ForceDisposal: Boolean = False; AOwner: TLapeStackInfo = nil; ManageVars: Boolean = True); reintroduce; virtual;
destructor Destroy; override;
procedure Clear; override;
function getDeclaration(Name: lpString; CheckParent: TInitBool; CheckWith: Boolean): TLapeDeclaration; virtual;
function hasDeclaration(Name: lpString; CheckParent: TInitBool; CheckWith: Boolean): Boolean; overload; virtual;
function hasDeclaration(Decl: TLapeDeclaration; CheckParent: TInitBool; CheckWith: Boolean): Boolean; overload; virtual;
function getTempVar(VarType: TLapeType; Lock: Integer = 1): TLapeStackTempVar; virtual;
function addDeclaration(Decl: TLapeDeclaration): Integer; override;
function addVar(StackVar: TLapeStackVar): TLapeStackVar; overload; virtual;
function addVar(VarType: TLapeType; Name: lpString = ''): TLapeStackVar; overload; virtual;
function addVar(ParType: ELapeParameterType; VarType: TLapeType; Name: lpString = ''): TLapeStackVar; overload; virtual;
function inheritVar(StackVar: TLapeStackVar): TLapeStackVar; virtual;
function getInheritedVar(StackVar: TLapeStackVar): TLapeStackVar; virtual;
function addWith(AWith: TLapeWithDeclRec): Integer; virtual;
procedure delWith(ACount: Integer); virtual;
property VarStack: TLapeVarStack read FVarStack;
property WithStack: TLapeWithDeclarationList read FWithStack;
property Vars[Index: Integer]: TLapeStackVar read getVar; default;
property VarCount: Integer read getVarCount;
property TotalSize: SizeInt read getTotalSize;
property TotalParamSize: SizeInt read getTotalParamSize;
property TotalNoParamSize: SizeInt read getTotalNoParamSize;
property NeedInitialization: Boolean read getInitialization;
property NeedFinalization: Boolean read getFinalization;
property OldStackPos: Integer read FOldStackPos;
property OldMaxStack: Integer read FOldMaxStack;
end;
TLapeDeclStack = class(TLapeStackInfo)
protected
FManagingList: TLapeManagingDeclaration;
public
//constructor Create(AList: TLapeDeclarationList; AOwner: TLapeStackInfo = nil); reintroduce; overload; virtual;
constructor Create(AList: TLapeManagingDeclaration; AOwner: TLapeStackInfo = nil); reintroduce; overload; virtual;
function addDeclaration(Decl: TLapeDeclaration): Integer; override;
function addVar(StackVar: TLapeStackVar): TLapeStackVar; override;
property ManagingList: TLapeManagingDeclaration read FManagingList;
end;
TLapeEmptyStack = class(TLapeStackInfo)
public
function addDeclaration(Decl: TLapeDeclaration): Integer; override;
end;
TLapeCodeEmitter = class(TLapeCodeEmitterBase)
public
function _IncCall(ACodePos: TResVar; AParamSize: UInt16; var Offset: Integer; Pos: PDocPos = nil): Integer; overload;
function _IncCall(ACodePos: TResVar; AParamSize: UInt16; Pos: PDocPos = nil): Integer; overload;
function _InvokeImportedProc(AMemPos: TResVar; AParamSize: UInt16; var Offset: Integer; Pos: PDocPos = nil): Integer; overload;
function _InvokeImportedProc(AMemPos: TResVar; AParamSize: UInt16; Pos: PDocPos = nil): Integer; overload;
function _InvokeImportedFunc(AMemPos, AResPos: TResVar; AParamSize: UInt16; var Offset: Integer; Pos: PDocPos = nil): Integer; overload;
function _InvokeImportedFunc(AMemPos, AResPos: TResVar; AParamSize: UInt16; Pos: PDocPos = nil): Integer; overload;
function _JmpRIf(Jmp: TCodeOffset; Cond: TResVar; var Offset: Integer; Pos: PDocPos = nil): Integer; overload; virtual;
function _JmpRIf(Jmp: TCodeOffset; Cond: TResVar; Pos: PDocPos = nil): Integer; overload; virtual;
function _JmpRIfNot(Jmp: TCodeOffset; Cond: TResVar; var Offset: Integer; Pos: PDocPos = nil): Integer; overload; virtual;
function _JmpRIfNot(Jmp: TCodeOffset; Cond: TResVar; Pos: PDocPos = nil): Integer; overload; virtual;
function _Eval(AProc: TLapeEvalProc; Dest, Left, Right: TResVar; var Offset: Integer; Pos: PDocPos = nil): Integer; overload; virtual;
function _Eval(AProc: TLapeEvalProc; Dest, Left, Right: TResVar; Pos: PDocPos = nil): Integer; overload; virtual;
end;
TLapeCompilerBase = class(TLapeBaseDeclClass)
protected
FEmitter: TLapeCodeEmitter;
FStackInfo: TLapeStackInfo;
FBaseTypes: TLapeBaseTypes;
FGlobalDeclarations: TLapeDeclarationList;
FManagedDeclarations: TLapeDeclarationList;
FCachedDeclarations: TLapeVarMap;
FBaseOptions: ECompilerOptionsSet;
FBaseOptions_PackRecords: UInt8;
FOptions: ECompilerOptionsSet;
FOptions_PackRecords: UInt8;
procedure Reset; virtual;
procedure setEmitter(AEmitter: TLapeCodeEmitter); virtual;
public
FreeEmitter: Boolean;
constructor Create(AEmitter: TLapeCodeEmitter = nil; ManageEmitter: Boolean = True); reintroduce; virtual;
destructor Destroy; override;
procedure Clear; virtual;
procedure VarToDefault(AVar: TResVar; var Offset: Integer; Pos: PDocPos = nil); overload; virtual;
procedure VarToDefault(AVar: TResVar; Pos: PDocPos = nil); overload; virtual;
procedure FinalizeVar(AVar: TResVar; var Offset: Integer; Pos: PDocPos = nil); overload; virtual;
procedure FinalizeVar(AVar: TResVar; Pos: PDocPos = nil); overload; virtual;
function PopVarToStack(AVar: TResVar; var Offset: Integer; Pos: PDocPos = nil): TResVar; virtual;
function IncStackInfo(AStackInfo: TLapeStackInfo; var Offset: Integer; Emit: Boolean = True; Pos: PDocPos = nil): TLapeStackInfo; overload; virtual;
function IncStackInfo(var Offset: Integer; Emit: Boolean = True; Pos: PDocPos = nil): TLapeStackInfo; overload; virtual;
function IncStackInfo(Emit: Boolean = False): TLapeStackInfo; overload; virtual;
function DecStackInfo(var Offset: Integer; InFunction: Boolean = False; Emit: Boolean = True; DoFree: Boolean = False; Pos: PDocPos = nil): TLapeStackInfo; overload; virtual;
function DecStackInfo(InFunction: Boolean = False; Emit: Boolean = False; DoFree: Boolean = False): TLapeStackInfo; overload; virtual;
procedure EmitCode(ACode: lpString; var Offset: Integer; Pos: PDocPos = nil); overload; virtual; abstract;
procedure EmitCode(ACode: lpString; AVarNames: array of lpString; AVars: array of TLapeVar; AResVars: array of TResVar; var Offset: Integer; Pos: PDocPos = nil); overload; virtual;
function getBaseType(Name: lpString): TLapeType; overload; virtual;
function getBaseType(BaseType: ELapeBaseType): TLapeType; overload; virtual;
function addLocalDecl(ADecl: TLapeDeclaration; AStackInfo: TLapeStackInfo): TLapeDeclaration; overload; virtual;
function addLocalDecl(ADecl: TLapeDeclaration): TLapeDeclaration; overload; virtual;
function addGlobalDecl(ADecl: TLapeDeclaration): TLapeDeclaration; virtual;
function addManagedDecl(ADecl: TLapeDeclaration): TLapeDeclaration; virtual;
function addManagedVar(AVar: TLapeVar; PtrCheckOnly: Boolean = False): TLapeVar; virtual;
function addManagedType(AType: TLapeType): TLapeType; virtual;
function addStackVar(VarType: TLapeType; Name: lpString): TLapeStackVar; virtual;
function getCachedConstant(Str: lpString; BaseType: ELapeBaseType = ltUnknown): TLapeGlobalVar; virtual;
function getConstant(Str: lpString; BaseType: ELapeBaseType = ltString; DoGrow: Boolean = False; ForceType: Boolean = False): TLapeGlobalVar; overload; virtual;
function getConstant(i: Int64; IntType: ELapeBaseType = ltNativeInt; DoGrow: Boolean = False; ForceType: Boolean = False): TLapeGlobalVar; overload; virtual;
function getLabel(CodePos: Integer): TLapeGlobalVar; virtual;
procedure getDestVar(var Dest, Res: TResVar; Op: EOperator); virtual;
function getTempVar(VarType: ELapeBaseType; Lock: Integer = 1): TLapeStackTempVar; overload; virtual;
function getTempVar(VarType: TLapeType; Lock: Integer = 1): TLapeStackTempVar; overload; virtual;
function getTempStackVar(VarType: ELapeBaseType): TResVar; overload; virtual;
function getTempStackVar(VarType: TLapeType): TResVar; overload; virtual;
function getPointerType(PType: ELapeBaseType; PConst: Boolean): TLapeType_Pointer; overload; virtual;
function getPointerType(PType: TLapeType; PConst: Boolean): TLapeType_Pointer; overload; virtual;
function getTypeVar(AType: ELapeBaseType): TLapeGlobalVar; overload; virtual;
function getTypeVar(AType: TLapeType): TLapeGlobalVar; overload; virtual;
function getGlobalVar(AName: lpString): TLapeGlobalVar; virtual;
function getGlobalType(AName: lpString): TLapeType; virtual;
function getDeclaration(AName: lpString; AStackInfo: TLapeStackInfo; LocalOnly: Boolean = False; CheckWith: Boolean = True): TLapeDeclaration; overload; virtual;
function getDeclaration(AName: lpString; LocalOnly: Boolean = False; CheckWith: Boolean = True): TLapeDeclaration; overload; virtual;
function hasDeclaration(AName: lpString; AStackInfo: TLapeStackInfo; LocalOnly: Boolean = False; CheckWith: Boolean = True): Boolean; overload; virtual;
function hasDeclaration(AName: lpString; LocalOnly: Boolean = False; CheckWith: Boolean = True): Boolean; overload; virtual;
function hasDeclaration(ADecl: TLapeDeclaration; AStackInfo: TLapeStackInfo; LocalOnly: Boolean = False; CheckWith: Boolean = True): Boolean; overload; virtual;
function hasDeclaration(ADecl: TLapeDeclaration; LocalOnly: Boolean = False; CheckWith: Boolean = True): Boolean; overload; virtual;
property StackInfo: TLapeStackInfo read FStackInfo;
property BaseTypes: TLapeBaseTypes read FBaseTypes;
property GlobalDeclarations: TLapeDeclarationList read FGlobalDeclarations;
property ManagedDeclarations: TLapeDeclarationList read FManagedDeclarations;
property Globals[AName: lpString]: TLapeGlobalVar read getGlobalVar; default;
published
property Emitter: TLapeCodeEmitter read FEmitter write setEmitter;
property Options: ECompilerOptionsSet read FOptions write FBaseOptions default Lape_OptionsDef;
property Options_PackRecords: UInt8 read FOptions_PackRecords write FBaseOptions_PackRecords default Lape_PackRecordsDef;
end;
function ResolveCompoundOp(op:EOperator; typ:TLapeType): EOperator; {$IFDEF Lape_Inline}inline;{$ENDIF}
function getTypeArray(Arr: array of TLapeType): TLapeTypeArray;
procedure ClearBaseTypes(var Arr: TLapeBaseTypes; DoFree: Boolean);
procedure LoadBaseTypes(var Arr: TLapeBaseTypes; Compiler: TLapeCompilerBase);
function MethodOfObject(VarType: TLapeType): Boolean; {$IFDEF Lape_Inline}inline;{$ENDIF}
function ValidFieldName(Field: TLapeGlobalVar): Boolean; overload; {$IFDEF Lape_Inline}inline;{$ENDIF}
function ValidFieldName(Field: TResVar): Boolean; overload; {$IFDEF Lape_Inline}inline;{$ENDIF}
const
BigLock = 256;
TypeID_Unknown = Ord(Low(ELapeBaseType)) - 1;
TypeID_User = Ord(High(ELapeBaseType)) + 1;
NullResVar: TResVar = (VarType: nil; VarPos: (isPointer: False; Offset: 0; MemPos: mpNone; GlobalVar: nil));
VarResVar: TResVar = (VarType: nil; VarPos: (isPointer: False; Offset: 0; MemPos: mpVar; StackVar : nil));
StackResVar:TResVar = (VarType: nil; VarPos: (isPointer: False; Offset: 0; MemPos: mpStack; ForceVariable: False));
NullParameter: TLapeParameter = (ParType: lptNormal; VarType: nil; Default: nil);
NullWithDecl: TLapeWithDeclRec = (WithVar: nil; WithType: nil);
NullVarRef: TLapeVarRef = (Lock: -1; ResVar: (VarType: nil; VarPos: (isPointer: False; Offset: 0; MemPos: mpNone; GlobalVar: nil)); RefVar: nil);
Lape_RefParams = [lptConstRef, lptOut, lptVar];
Lape_ConstParams = [lptConst, lptConstRef];
Lape_ValParams = Lape_ConstParams + [lptNormal];
Lape_SelfParam = lptVar;
var
LapeReservedLocals: lpString = '|System|';
EmptyStackInfo: TLapeEmptyStack = nil;
_ResVar: TResVar;
implementation
uses
{$IFDEF Lape_NeedAnsiStringsUnit}AnsiStrings,{$ENDIF}
lpvartypes_ord, lpvartypes_array,
lpexceptions, lpeval, lpinterpreter;
function ResolveCompoundOp(op:EOperator; typ:TLapeType): EOperator;
begin
case op of
op_AssignDiv:
if (typ.BaseType in LapeRealTypes) then
Result := op_divide
else
Result := op_DIV;
op_AssignMinus: Result := op_Minus;
op_AssignMul: Result := op_Multiply;
op_AssignPlus: Result := op_Plus;
else Result := op_Unknown;
end;
end;
function getTypeArray(Arr: array of TLapeType): TLapeTypeArray;
var
i: Integer;
begin
SetLength(Result, Length(Arr));
for i := 0 to High(Arr) do
Result[i] := Arr[i];
end;
procedure ClearBaseTypes(var Arr: TLapeBaseTypes; DoFree: Boolean);
var
BaseType: ELapeBaseType;
begin
for BaseType := Low(ELapeBaseType) to High(ELapeBaseType) do
if (Arr[BaseType] <> nil) then
if DoFree then
FreeAndNil(Arr[BaseType])
else
Arr[BaseType].ClearSubDeclarations();
end;
procedure LoadBaseTypes(var Arr: TLapeBaseTypes; Compiler: TLapeCompilerBase);
begin
Arr[ltUInt8] := TLapeType_UInt8.Create(Compiler, LapeTypeToString(ltUInt8));
Arr[ltInt8] := TLapeType_Int8.Create(Compiler, LapeTypeToString(ltInt8));
Arr[ltUInt16] := TLapeType_UInt16.Create(Compiler, LapeTypeToString(ltUInt16));
Arr[ltInt16] := TLapeType_Int16.Create(Compiler, LapeTypeToString(ltInt16));
Arr[ltUInt32] := TLapeType_UInt32.Create(Compiler, LapeTypeToString(ltUInt32));
Arr[ltInt32] := TLapeType_Int32.Create(Compiler, LapeTypeToString(ltInt32));
Arr[ltUInt64] := TLapeType_UInt64.Create(Compiler, LapeTypeToString(ltUInt64));
Arr[ltInt64] := TLapeType_Int64.Create(Compiler, LapeTypeToString(ltInt64));
Arr[ltSingle] := TLapeType_Single.Create(Compiler, LapeTypeToString(ltSingle));
Arr[ltDouble] := TLapeType_Double.Create(Compiler, LapeTypeToString(ltDouble));
Arr[ltCurrency] := TLapeType_Currency.Create(Compiler, LapeTypeToString(ltCurrency));
Arr[ltExtended] := TLapeType_Extended.Create(Compiler, LapeTypeToString(ltExtended));
Arr[ltBoolean] := TLapeType_Boolean.Create(Compiler, LapeTypeToString(ltBoolean));
Arr[ltByteBool] := TLapeType_ByteBool.Create(Compiler, LapeTypeToString(ltByteBool));
Arr[ltWordBool] := TLapeType_WordBool.Create(Compiler, LapeTypeToString(ltWordBool));
Arr[ltLongBool] := TLapeType_LongBool.Create(Compiler, LapeTypeToString(ltLongBool));
Arr[ltAnsiChar] := TLapeType_AnsiChar.Create(Compiler, LapeTypeToString(ltAnsiChar));
Arr[ltWideChar] := TLapeType_WideChar.Create(Compiler, LapeTypeToString(ltWideChar));
Arr[ltShortString] := TLapeType_ShortString.Create(Compiler, High(UInt8), LapeTypeToString(ltShortString));
Arr[ltAnsiString] := TLapeType_AnsiString.Create(Compiler, LapeTypeToString(ltAnsiString));
Arr[ltWideString] := TLapeType_WideString.Create(Compiler, LapeTypeToString(ltWideString));
Arr[ltUnicodeString] := TLapeType_UnicodeString.Create(Compiler, LapeTypeToString(ltUnicodeString));
Arr[ltVariant] := TLapeType_Variant.Create(Compiler, LapeTypeToString(ltVariant));
Arr[ltPointer] := TLapeType_Pointer.Create(Compiler, nil, False, LapeTypeToString(ltPointer));
end;
function MethodOfObject(VarType: TLapeType): Boolean;
begin
Result := (VarType is TLapeType_MethodOfObject) or
((VarType is TLapeType_OverloadedMethod) and
(TLapeType_OverloadedMethod(VarType).MethodsOfObject <> bFalse));
end;
function ValidFieldName(Field: TLapeGlobalVar): Boolean; overload;
begin
Result := (Field <> nil) and Field.isConstant and (Field.BaseType = ltString) and (Field.Ptr <> nil);
end;
function ValidFieldName(Field: TResVar): Boolean; overload;
begin
Result := (Field.VarPos.MemPos = mpMem) and Field.isConstant and Field.HasType() and (Field.VarType.BaseType = ltString) and (Field.VarPos.GlobalVar.Ptr <> nil);
end;
function TResVar.getReadable: Boolean;
begin
if (VarPos.MemPos = mpMem) and (VarPos.GlobalVar <> nil) then
Result := VarPos.GlobalVar.Readable
else if (VarPos.MemPos = mpVar) and (VarPos.StackVar <> nil) then
Result := VarPos.StackVar.Readable
else if (VarPos.MemPos = mpStack) then
Result := not VarPos.ForceVariable
else
Result := True;
end;
function TResVar.getWriteable: Boolean;
begin
Result := ((VarPos.MemPos = mpStack) and (VarPos.isPointer or VarPos.ForceVariable)) or
((VarPos.MemPos = mpMem) and (VarPos.GlobalVar <> nil) and VarPos.GlobalVar.Writeable) or
((VarPos.MemPos = mpVar) and (VarPos.StackVar <> nil) and VarPos.StackVar.Writeable);
end;
function TResVar.getConstant: Boolean;
begin
Result := Readable and (not Writeable);
end;
class function TResVar.New(AVar: TLapeVar): TResVar;
begin
Result := NullResVar;
if (AVar <> nil) then
begin
Result.VarType := AVar.VarType;
if (AVar is TLapeStackVar) then
begin
Result.VarPos.MemPos := mpVar;
Result.VarPos.StackVar := AVar as TLapeStackVar;
end
else if (AVar is TLapeGlobalVar) then
begin
Result.VarPos.MemPos := mpMem;
Result.VarPos.GlobalVar := AVar as TLapeGlobalVar;
end;
if (AVar is TLapeParameterVar) then
Result.VarPos.isPointer := (TLapeParameterVar(AVar).ParType in Lape_RefParams);
end;
end;
function TResVar.HasType: Boolean;
begin
Result := VarType <> nil;
end;
procedure TResVar.Spill(Unlock: Integer = 0);
begin
DecLock(Unlock);
Self := NullResVar;
end;
function TResVar.IncLock(Count: Integer = 1): TResVar;
begin
Result := Self;
if (Count > 0) and (VarPos.MemPos = mpVar) and
(VarPos.StackVar <> nil) and (VarPos.StackVar is TLapeStackTempVar)
then
TLapeStackTempVar(VarPos.StackVar).IncLock(Count);
end;
function TResVar.DecLock(Count: Integer = 1): TResVar;
begin
Result := Self;
if (Count > 0) and (VarPos.MemPos = mpVar) and
(VarPos.StackVar <> nil) and (VarPos.StackVar is TLapeStackTempVar)
then
TLapeStackTempVar(VarPos.StackVar).DecLock(Count);
end;
function TResVar.InScope(AStack: TLapeStackInfo; Pos: PDocPos = nil): TResVar;
begin
Result := Self;
if (VarPos.MemPos = mpVar) and (AStack <> nil) and (VarPos.StackVar.Stack <> AStack.VarStack) then
begin
Result.VarPos.StackVar := AStack.getInheritedVar(VarPos.StackVar);
Result.VarPos.isPointer := True;
if (Result.VarPos.StackVar = nil) then
if (Pos <> nil) then
LapeException(lpeParentOutOfScope, Pos^)
else
LapeException(lpeParentOutOfScope, VarPos.StackVar.DocPos);
end;
end;
procedure TResVar.IncOffset(Offset: Integer);
begin
if VarPos.isPointer or ((VarPos.MemPos = mpVar) and (VarPos.StackVar <> nil)) then
Inc(VarPos.Offset, Offset)
else if (VarPos.MemPos = mpMem) and (VarPos.GlobalVar <> nil) then
Inc(PtrInt(VarPos.GlobalVar.FPtr), Offset)
else if (VarPos.MemPos = mpStack) then
Dec(VarPos.Offset, Offset);
end;
procedure TResVar.DecOffset(Offset: Integer);
begin
IncOffset(-Offset);
end;
procedure TResVar.setOffset(Offset: Integer);
begin
VarPos.Offset := 0;
IncOffset(Offset);
end;
procedure TResVar.setReadable(AReadable: Boolean; ChangeStack: Boolean);
begin
if (VarPos.MemPos = mpMem) and (VarPos.GlobalVar <> nil) then
VarPos.GlobalVar.Readable := AReadable
else if (VarPos.MemPos = mpVar) and (VarPos.StackVar <> nil) then
VarPos.StackVar.Readable := AReadable
else if ChangeStack and (VarPos.MemPos = mpStack) then
VarPos.ForceVariable := not AReadable;
end;
procedure TResVar.setReadable(AReadable: Boolean);
begin
setReadable(AReadable, True);
end;
procedure TResVar.setWriteable(AWriteable: Boolean; ChangeStack: Boolean);
begin
if (VarPos.MemPos = mpMem) and (VarPos.GlobalVar <> nil) then
VarPos.GlobalVar.Writeable := AWriteable
else if (VarPos.MemPos = mpVar) and (VarPos.StackVar <> nil) then
VarPos.StackVar.Writeable := AWriteable
else if ChangeStack and (VarPos.MemPos = mpStack) then
VarPos.ForceVariable := AWriteable;
end;
procedure TResVar.setWriteable(AWriteable: Boolean);
begin
setWriteable(AWriteable, True);
end;
procedure TResVar.setConstant(AConst: Boolean; ChangeStack: Boolean);
begin
setReadable(AConst, ChangeStack);
setWriteable(not AConst, ChangeStack);
end;
procedure TResVar.setConstant(AConst: Boolean);
begin
setConstant(AConst, True);
end;
procedure TResVar.setReadWrite(AReadable, AWriteable: Boolean);
begin
Readable := AReadable;
Writeable := AWriteable;
end;
procedure TResVar.CopyFlags(Other: TResVar);
begin
Readable := Other.Readable;
Writeable := Other.Writeable;
end;
function TLapeVar.getBaseType: ELapeBaseType;
begin
if HasType() then
Result := VarType.BaseType
else
Result := ltUnknown;
end;
function TLapeVar.getSize: SizeInt;
begin
if HasType() then
Result := VarType.Size
else
Result := -1;
end;
function TLapeVar.getLo: TLapeGlobalVar;
begin
if HasType() then
Result := VarType.VarLo(nil)
else
Result := nil;
end;
function TLapeVar.getHi: TLapeGlobalVar;
begin
if HasType() then
Result := VarType.VarHi(nil)
else
Result := nil;
end;