This repository has been archived by the owner on Oct 14, 2019. It is now read-only.
/
symdef.pas
1729 lines (1391 loc) · 40.9 KB
/
symdef.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
(*
Copyright © by Patryk Wychowaniec, 2013-2014
All rights reserved.
*)
{$MODESWITCH ADVANCEDRECORDS}
Unit symdef;
Interface
Uses FGL, Expression, FlowGraph, Tokens, Serialization, SysUtils;
{ ESymdefException }
Type ESymdefException = Class(Exception);
(* forward declarations *)
Type TType = class;
TVariable = class;
TFunction = class;
TNamespace = class;
TSymbol = class;
(* auxiliary declarations *)
Type TVisibility = (mvPublic, mvPrivate, mvStrictPrivate);
Type PTypeAttributes = ^TTypeAttributes;
TTypeAttributes = Set of (taStrict, taFunction, taEnum, taUnspecialized, taNull);
Type TVariableAttributes = Set of (vaConst, vaEnumItem, vaFuncParam, vaDontAllocate, vaVolatile, vaCatchVar);
Type TFunctionAttributes = Set of (faNaked);
{ TRange }
Type PRange = ^TRange;
TRange =
Record
PBegin, PEnd: TToken_P;
End;
{ TStackSavedReg }
Type PStackSavedReg = ^TStackSavedReg;
TStackSavedReg =
Record
Public
RegChar: Char;
RegID : uint8;
Public
Function getRegisterName: String;
End;
Type TStackSavedRegs = specialize TFPGList<PStackSavedReg>;
{ TNamespaceVisibility }
Type PNamespaceVisibility = ^TNamespaceVisibility;
TNamespaceVisibility =
Record
Namespace: TNamespace;
Range : TRange;
End;
Type TNamespaceVisibilityList = specialize TFPGList<PNamespaceVisibility>;
Type TRefSymbol = class;
(* lists *)
Type TNamespaceList = specialize TFPGList<TNamespace>;
TVariableList = specialize TFPGList<TVariable>;
TSymbolList = specialize TFPGList<TSymbol>;
{ TFunctionParam }
Type PFunctionParam = ^TFunctionParam;
TFunctionParam =
Record
Name : String;
Typ : TType;
DefaultValue: TConstantExpressionNode;
Attributes : TVariableAttributes;
isConst : Boolean;
isVar : Boolean; // `isPassedByRef`
End;
{ TFunctionParamList }
Type PFunctionParamList = ^TFunctionParamList;
TFunctionParamList = Array of TFunctionParam;
{ TSymdefObject }
Type TSymdefObject =
Class
Public { fields }
RefSymbol: TRefSymbol;
End;
{ TType }
Type TType =
Class(TSymdefObject)
Public { fields }
RegPrefix : Char; // bytecode register prefix, any of: b, c, i, f, s, r
InternalID: uint8;
ArrayPrimitive: TType; // array base (primitive) type (it has to be a primary type)
ArrayDimCount : uint8; // array dimension count
// for function types
FuncReturn: TType;
FuncParams: TFunctionParamList;
// for enumeration types
EnumBase : TType;
EnumItemList: TVariableList;
// global
Attributes: TTypeAttributes;
Public { methods }
Constructor Create;
Constructor Create(const Unserializer: TUnserializer);
Constructor Create(const Root: TNode);
Function isUnspecialized: Boolean;
Function isStrict: Boolean;
Function getSerializedForm: String;
Function getBytecodeSize: uint8;
Function getLowerArray: TType;
Function isAny: Boolean;
Function isVoid: Boolean;
Function isBool: Boolean;
Function isChar: Boolean;
Function isInt: Boolean;
Function isFloat: Boolean;
Function isString: Boolean;
Function isNumerical: Boolean;
Function isNull: Boolean;
Function isArray(const RegardStringAsArray: Boolean=True): Boolean;
Function isObject: Boolean;
Function isFunctionPointer: Boolean;
Function isEnum: Boolean;
Function isEnumItem: Boolean;
Function isSimple: Boolean;
Function CanBeAssignedTo(T2: TType): Boolean;
Function CanBeCastedTo(T2: TType): Boolean;
Function Clone: TType;
Function asString(const AcceptTypeNames: Boolean=True): String;
End;
{ TVarLocation }
Type TVarLocation = (vlNone, vlRegister, vlStack, vlMemory);
{ TVariableLocationData }
Type TVariableLocationData =
Record
Case Location: TVarLocation of
vlRegister: (RegisterID: uint8); // 1..4
vlStack : (StackPosition: int8);
vlMemory : (MemSymbolName: ShortString); // these types of variables are allocated by the linker, that's why here's "MemSymbolName" rather that something like "MemoryAddress: uint32;"
End;
{ TVariable }
Type TVariable =
Class(TSymdefObject)
Public { fields }
LocationData: TVariableLocationData;
Typ : TType;
Value: TConstantExpressionNode; // when variable is either a constant or a global variable
Attributes: TVariableAttributes;
Public { methods }
Constructor Create;
Constructor Create(const HLCompiler: TObject; const Unserializer: TUnserializer);
Constructor Create(const HLCompiler: TObject; const Root: TNode);
Function isConst: Boolean;
Function isFuncParam: Boolean;
Function isVolatile: Boolean;
Function isCatchVar: Boolean;
Function DontAllocate: Boolean;
Function getAllocationPos(const StackShift: int8=0): String;
Function getSerializedForm: String;
End;
{ TFunction }
Type TFunction =
Class(TSymdefObject)
Public { fields }
RefVar: TVariable;
LabelName : String;
ModuleName: String; // @TODO: shouldn't it be in "TRefSymbol"?
Return: TType; // return type
ParamList : TFunctionParamList; // parameter list
SymbolList: TSymbolList; // local symbol list
FlowGraph : TCFGraph; // control flowgraph
StackSize: uint16; // number of variables put onto stack. It does NOT consider prolog 'push'es!
StackRegs: TStackSavedRegs;
LastLabelID: uint32;
FirstOpcode, LastOpcode: Pointer; // first and last function opcode (defines function bounds); the first is always the function's label and the last is the "ret" opcode
Attributes: TFunctionAttributes;
Public { methods }
Constructor Create;
Destructor Destroy; override;
Function createNode(const fParent: TCFGNode; const ffToken: PToken_P=nil): TCFGNode;
Function createNode(const fParent: TCFGNode; const fTyp: TCFGNodeType; const fValue: TExpressionNode; const ffToken: PToken_P=nil): TCFGNode;
Function findSymbol(const SymName: String): TSymbol;
Function findSymbol(const SymName: String; const SymScope: TToken_P): TSymbol;
Procedure invalidateRegister(const RegisterChar: Char; const RegisterID: uint8);
Function generateLabelName: String;
Function getSerializedForm: String;
Function isNaked: Boolean;
End;
{ TNamespace }
Type TNamespace =
Class(TSymdefObject)
Public { fields } // @TODO: should be private
SymbolList: TSymbolList; // global symbol list
Public { methods }
Constructor Create;
Function findSymbol(const SymName: String): TSymbol;
Function findSymbol(const SymName: String; const SymScope: TToken_P): TSymbol;
Function findFunction(const FuncName: String): TFunction;
Public
Property getSymbolList: TSymbolList read SymbolList;
End;
{ TRefSymbol }
Type TRefSymbol =
Class
Public { fields }
Name : String; // symbol name
Range: TRange; // accessibility range
DeclNamespace: TNamespace; // namespace in which identifier has been declared
DeclFunction : TFunction; // function in which identifier has been declared
Visibility: TVisibility; // visibility
mCompiler : Pointer; // compiler in which symbol has been declared
DeclToken : PToken_P; // declaration token pointer
isInternal: Boolean; // eg.`null` is an internal symbol
Public { methods }
Constructor Create;
Function Clone: TRefSymbol;
Procedure CopyTo(const Symbol: TRefSymbol);
Function getFullName(const Separator: String='.'): String;
Function isLocal: Boolean;
Function isGlobal: Boolean;
End;
{ TSymbol }
Type TSymbolType = (stNamespace, stFunction, stVariable, stConstant, stType);
Type TSymbol =
Class (TRefSymbol)
Public { fields }
Typ: TSymbolType;
mNamespace: TNamespace;
mVariable : TVariable;
mFunction : TFunction;
mType : TType;
Public { methods }
Constructor Create(const SymbolType: TSymbolType; const CreateInstance: Boolean=True);
Constructor Create(const SymbolType: TSymbolType; const Instance: TObject);
Constructor Create(const CloneOf: TSymbol);
Function getSymdefObject: TSymdefObject;
End;
// functions
Function type_equal(const A, B: TType): Boolean;
Operator in (Token: TToken_P; Range: TRange): Boolean;
Operator = (A, B: TVariableLocationData): Boolean;
Function TYPE_ANY: TType;
Function TYPE_VOID: TType;
Function TYPE_BOOL: TType;
Function TYPE_CHAR: TType;
Function TYPE_INT: TType;
Function TYPE_FLOAT: TType;
Function TYPE_STRING: TType;
Function TYPE_NULL: TType;
Implementation
Uses Logging, ExpressionParser, LLCompiler, Messages;
(* type_equal *)
{
Compares two TTypes (everything but their names).
}
Function type_equal(const A, B: TType): Boolean; // @TODO: change this function name
Var I: Integer;
Begin
if (A = nil) or (B = nil) Then
Exit(False);
Result :=
(A.ArrayDimCount = B.ArrayDimCount) and
(A.InternalID = B.InternalID) and
// (A.Attributes = B.Attributes) and
(A.RegPrefix = B.RegPrefix);
if (not Result) Then
Exit;
// array primitives
if (A.ArrayPrimitive <> nil) and (B.ArrayPrimitive <> nil) Then
Result := Result and type_equal(A.ArrayPrimitive, B.ArrayPrimitive);
// func return
if (A.FuncReturn <> nil) and (B.FuncReturn <> nil) Then
Result := Result and type_equal(A.FuncReturn, B.FuncReturn);
// func parameter list
Result := Result and (Length(A.FuncParams) = Length(B.FuncParams));
// enum base
if (A.EnumBase <> nil) and (B.EnumBase <> nil) Then
Result := Result and type_equal(A.EnumBase, B.EnumBase);
if ((A.EnumBase = nil) and (B.EnumBase <> nil)) or
((A.EnumBase <> nil) and (B.EnumBase = nil)) Then
Exit(False);
// enum item list
Result := Result and (A.EnumItemList.Count = B.EnumItemList.Count);
if (not Result) Then
Exit;
For I := 0 To A.EnumItemList.Count-1 Do
Result := Result and (A.EnumItemList[I] = B.EnumItemList[I]);
End;
(* `TToken_P` in `TRange` *)
Operator in (Token: TToken_P; Range: TRange): Boolean;
Begin
Result := (Token.Position >= Range.PBegin.Position) and (Token.Position <= Range.PEnd.Position);
End;
(* TVariableLocationData = TVariableLocationData *)
Operator = (A, B: TVariableLocationData): Boolean;
Begin
Result := (A.Location = B.Location);
if (Result) Then
Begin
Case A.Location of
vlRegister: Result := (A.RegisterID = B.RegisterID);
vlStack : Result := (A.StackPosition = B.StackPosition);
vlMemory : Result := (A.MemSymbolName = B.MemSymbolName);
End;
End;
End;
(* TYPE_ANY *)
Function TYPE_ANY: TType;
Begin
Result := TType.Create;
With Result do
Begin
RefSymbol.Name := 'any';
RegPrefix := 'i';
InternalID := TYPE_ANY_id;
End;
End;
(* TYPE_VOID *)
Function TYPE_VOID: TType;
Begin
Result := TType.Create;
With Result do
Begin
RefSymbol.Name := 'void';
RegPrefix := 'i';
InternalID := TYPE_VOID_id;
End;
End;
(* TYPE_BOOL *)
Function TYPE_BOOL: TType;
Begin
Result := TType.Create;
With Result do
Begin
RefSymbol.Name := 'bool';
RegPrefix := 'b';
InternalID := TYPE_BOOL_id;
End;
End;
(* TYPE_CHAR *)
Function TYPE_CHAR: TType;
Begin
Result := TType.Create;
With Result do
Begin
RefSymbol.Name := 'char';
RegPrefix := 'c';
InternalID := TYPE_CHAR_id;
End;
End;
(* TYPE_INT *)
Function TYPE_INT: TType;
Begin
Result := TType.Create;
With Result do
Begin
RefSymbol.Name := 'int';
RegPrefix := 'i';
InternalID := TYPE_INT_id;
End;
End;
(* TYPE_FLOAT *)
Function TYPE_FLOAT: TType;
Begin
Result := TType.Create;
With Result do
Begin
RefSymbol.Name := 'float';
RegPrefix := 'f';
InternalID := TYPE_FLOAT_id;
End;
End;
(* TYPE_STRING *)
Function TYPE_STRING: TType;
Begin
Result := TType.Create;
With Result do
Begin
RefSymbol.Name := 'string';
RegPrefix := 's';
InternalID := TYPE_STRING_id;
ArrayPrimitive := TYPE_CHAR;
ArrayDimCount := 1;
End;
End;
(* TYPE_NULL *)
Function TYPE_NULL: TType;
Begin
Result := TType.Create;
With Result do
Begin
RefSymbol.Name := 'nulltype'#0;
RegPrefix := 'i';
InternalID := TYPE_INT_id;
Attributes += [taNull];
End;
End;
// -------------------------------------------------------------------------- //
(* TStackSavedReg.getRegisterName *)
Function TStackSavedReg.getRegisterName: String;
Begin
if (RegChar = 'b') and (RegID = 5) Then
Result := 'if' Else
Result := Format('e%s%d', [String(RegChar), RegID]);
End;
// -------------------------------------------------------------------------- //
(* TType.Create *)
{
Constructor for TType
}
Constructor TType.Create;
Begin
RefSymbol := TRefSymbol.Create;
RegPrefix := 'i';
ArrayDimCount := 0;
ArrayPrimitive := nil;
InternalID := 0;
FuncReturn := nil;
EnumBase := nil;
EnumItemList := TVariableList.Create;
Attributes := [];
SetLength(FuncParams, 0);
End;
(* TType.Create *)
{ Creates type from its serialized form. }
Constructor TType.Create(const Unserializer: TUnserializer);
Begin
Create(Unserializer.getRoot);
End;
(* TType.Create *)
{ Creates type from its serialized form. }
Constructor TType.Create(const Root: TNode);
Var I : int32;
mVar: TVariable;
Begin
Create();
if (Root.getChildren.Count = 0) Then // no data to parse, leave
Exit;
if (Root[0].getValue <> 'type') Then
raise ESymdefException.Create('TType.Create() -> invalid serialized form!');
RegPrefix := Root[1].getValue[1];
InternalID := Root[2].getInt;
if (Root[3].getChildren.Count > 0) Then
ArrayPrimitive := TType.Create(Root[3]);
ArrayDimCount := Root[4].getInt;
if (Root[5].getChildren.Count > 0) Then
FuncReturn := TType.Create(Root[5]);
SetLength(FuncParams, Root[6].getInt);
For I := Low(FuncParams) To High(FuncParams) Do
Begin
FuncParams[I].isConst := Root[7][I][0].getBool;
FuncParams[I].isVar := Root[7][I][1].getBool;
FuncParams[I].Typ := TType.Create(Root[7][I][2]);
End;
if (Root[8].getChildren.Count > 0) Then
EnumBase := TType.Create(Root[8]);
I := Root[9].getInt;
For I := 0 To I-1 Do
Begin
mVar := TVariable.Create;
mVar.RefSymbol.Name := Root[10][I].getString;
EnumItemList.Add(mVar);
End;
I := Root[11].getInt;
Attributes := PTypeAttributes(@I)^;
End;
(* TType.isUnspecialized *)
{
Returns `true`, when type is marked as `unspecialized`
}
Function TType.isUnspecialized: Boolean;
Begin
Result := (taUnspecialized in Attributes);
End;
(* TType.isStrict *)
{
Returns `true`, when type is marked as `strict`
}
Function TType.isStrict: Boolean;
Begin
Result := (taStrict in Attributes);
End;
(* TType.getSerializedForm *)
{
Returns serialized form of type.
}
Function TType.getSerializedForm: String;
Var I: int32;
Begin
if (self = nil) Then
Exit('()');
Result := '(';
Result += 'type$';
Result += RegPrefix+'$';
Result += IntToStr(InternalID)+'$';
Result += ArrayPrimitive.getSerializedForm+'$';
Result += IntToStr(ArrayDimCount)+'$';
Result += FuncReturn.getSerializedForm+'$';
Result += IntToStr(Length(FuncParams))+'$';
Result += '(';
For I := Low(FuncParams) To High(FuncParams) Do
Begin
Result += '(';
Result += IntToStr(uint8(FuncParams[I].isConst))+'$';
Result += IntToStr(uint8(FuncParams[I].isVar))+'$';
Result += FuncParams[I].Typ.getSerializedForm;
Result += ')$';
End;
Result += ')$';
Result += EnumBase.getSerializedForm+'$';
Result += IntToStr(EnumItemList.Count)+'$';
Result += '(';
For I := 0 To EnumItemList.Count-1 Do
Result += '('+EnumItemList[I].RefSymbol.Name+')$';
Result += ')$';
Result += IntToStr(PLongWord(@Attributes)^);
Result += ')';
End;
(* TType.getBytecodeSize *)
{ Returns internal type's bytecode size in bytes }
Function TType.getBytecodeSize: uint8;
Begin
Case RegPrefix of
'b': Result := 1;
'c': Result := 1;
'i': Result := 8;
'f': Result := 10;
's': Result := 8;
'r': Result := 8;
else
raise ESymdefException.CreateFmt('TType.getBytecodeSize() -> invalid ''self.RegPrefix=#%d''!', [ord(RegPrefix)]);
End;
End;
(* TType.getLowerArray *)
{
Returns 'lower' type of array type.
When type is 'int[][]', it returns 'int[]',
When type is 'string' it returns 'char' and so on.
}
Function TType.getLowerArray: TType;
Begin
if (not isArray) Then
raise ESymdefException.Create('TType.getLowerArray() called on non-array type');
if (ArrayDimCount = 1) Then
Begin
Result := ArrayPrimitive;
End Else
Begin
Result := TType.Create;
Result.ArrayDimCount := ArrayDimCount-1;
Result.RefSymbol := RefSymbol;
Result.Attributes := Attributes;
if (isString) and (ArrayDimCount = 2) Then
Begin
Result.ArrayPrimitive := ArrayPrimitive.ArrayPrimitive;
Result.RegPrefix := ArrayPrimitive.RegPrefix;
Result.InternalID := ArrayPrimitive.InternalID;
End Else
Begin
Result.ArrayPrimitive := ArrayPrimitive;
Result.RegPrefix := RegPrefix;
Result.InternalID := InternalID;
End;
End;
End;
(* TType.isAny *)
{
Returns `true` when type passed in parameter is `any`.
}
Function TType.isAny: Boolean;
Begin
if (self = nil) Then
Exit(False);
Exit(InternalID = TYPE_ANY_id);
End;
(* TType.isVoid *)
{
Returns `true` when type passed in parameter is `void`.
}
Function TType.isVoid: Boolean;
Begin
if (self = nil) Then
Exit(False);
Exit(InternalID = TYPE_VOID_id);
End;
(* TType.isBool *)
{
Returns `true` when type passed in parameter is `bool`.
}
Function TType.isBool: Boolean;
Begin
if (self = nil) Then
Exit(False);
Exit(InternalID = TYPE_BOOL_id);
End;
(* TType.isChar *)
{
Returns `true` when type passed in parameter is `char`.
}
Function TType.isChar: Boolean;
Begin
if (self = nil) Then
Exit(False);
Exit(InternalID = TYPE_CHAR_id);
End;
(* TType.isInt *)
{
Returns `true` when type passed in parameter is `int` or a pointer.
}
Function TType.isInt: Boolean;
Begin
if (self = nil) Then
Exit(False);
Exit((InternalID = TYPE_INT_id) or (taFunction in Attributes));
End;
(* TType.isFloat *)
{
Returns `true` when type passed in parameter is `float`.
}
Function TType.isFloat: Boolean;
Begin
if (self = nil) Then
Exit(False);
Exit(InternalID = TYPE_FLOAT_id);
End;
(* TType.isString *)
{
Returns `true` when type passed in parameter is `string`.
}
Function TType.isString: Boolean;
Begin
if (self = nil) Then
Exit(False);
Exit(InternalID = TYPE_STRING_id);
End;
(* TType.isNumerical *)
{
Returns `true` when type passed in parameter is a numerical type (int, float, char and pointers) or numerical-derived; in other cases returns `false`.
}
Function TType.isNumerical: Boolean;
Begin
if (self = nil) Then
Exit(False);
Exit(isInt or isFloat or isChar or isFunctionPointer);
End;
(* TType.isNull *)
{
Returns `true` when type is 'null'.
}
Function TType.isNull: Boolean;
Begin
if (self = nil) Then
Exit(False);
Exit(taNull in Attributes);
End;
(* TType.isArray *)
{
Returns `true` when type passed in parameter is an array; in other case, returns `false`.
}
Function TType.isArray(const RegardStringAsArray: Boolean=True): Boolean;
Begin
if (self = nil) Then
Exit(False);
if ((isString) and (ArrayDimCount = 1)) Then
Exit(RegardStringAsArray);
Exit(ArrayDimCount > 0);
End;
(* TType.isObject *)
{
Returns `true` when type passed in parameter is an object.
}
Function TType.isObject: Boolean;
Begin
if (self = nil) Then
Exit(False);
Result := isArray(False); // so far only arrays are some-kind-of-objects
End;
(* TType.isFunctionPointer *)
{
Returns `true` when type passed in parameter is a function pointer.
}
Function TType.isFunctionPointer: Boolean;
Begin
if (self = nil) Then
Exit(False);
Exit(taFunction in Attributes);
End;
(* TType.isEnum *)
{
Returns `true` when type passed in parameter is an enumeration type (enum).
}
Function TType.isEnum: Boolean;
Begin
if (self = nil) Then
Exit(False);
Exit(taEnum in Attributes);
End;
(* TType.isEnumItem *)
{
Returns `true` when type passed in parameter is an enumeration type's item.
}
Function TType.isEnumItem: Boolean;
Begin
if (self = nil) Then
Exit(False);
Exit(EnumBase <> nil);
End;
(* TType.isSimple *)
{
Returns 'true' if type is "simple" (ie. is either bool/char/int/float/string/pointer but not an array)
}
Function TType.isSimple: Boolean;
Begin
if (self = nil) Then
Exit(False);
Result := (isBool or isChar or isInt or isFloat or isString or isFunctionPointer) and (not isArray(False));
End;
(* TType.Clone *)
{
Returns a clone of self.
}
Function TType.Clone: TType;
Var I: Integer;
Begin
if (self = nil) Then
Exit(nil);
Result := TType.Create;
Result.RefSymbol := RefSymbol.Clone;
Result.RegPrefix := RegPrefix;
Result.InternalID := InternalID;
Result.ArrayDimCount := ArrayDimCount;
Result.FuncReturn := FuncReturn.Clone;
SetLength(Result.FuncParams, Length(FuncParams));
For I := Low(FuncParams) To High(FuncParams) Do
Begin
Result.FuncParams[I] := FuncParams[I];
Result.FuncParams[I].Typ := FuncParams[I].Typ.Clone;
End;
Result.EnumBase := EnumBase.Clone;
Result.EnumItemList := EnumItemList; // @Note: do NOT clone enum items (see `type_equal` routine)
Result.Attributes := Attributes;
if (ArrayPrimitive = self) Then
Begin
DevLog(dvError, 'ArrayPrimitive = self; cannot do the entire type cloning');
Result.ArrayPrimitive := ArrayPrimitive;
Exit;
End;
if (ArrayPrimitive = nil) Then
Result.ArrayPrimitive := nil Else
Result.ArrayPrimitive := ArrayPrimitive.Clone;
End;
(* TType.asString *)
{
Returns type declaration.
Eg.`int[]` or `function<void>(string)`.
}
Function TType.asString(const AcceptTypeNames: Boolean): String;
Var I: Integer;
Begin
if (self = nil) Then // erroneous (invalid) type
Exit('erroneous type');
if (AcceptTypeNames) and (Length(RefSymbol.Name) > 0) Then
Exit(RefSymbol.getFullName('::'));
Result := '';
{ is function? }
if (taFunction in Attributes) Then
Begin
if (taUnspecialized in Attributes) Then
Result += 'unspecialized function' Else
Begin
Result += 'function<'+FuncReturn.asString+'>(';
For I := Low(FuncParams) To High(FuncParams) Do
Begin
if (FuncParams[I].isConst) Then
Result += 'const ';
if (FuncParams[I].isVar) Then
Result += 'var ';
Result += FuncParams[I].Typ.asString;
if (FuncParams[I].DefaultValue <> nil) Then
Result += ' = '+FuncParams[I].DefaultValue.getValue;
if (I <> High(FuncParams)) Then
Result += ', ';
End;
Result += ')';
End;
For I := 1 To ArrayDimCount Do
Result += '[]';
Exit;
End Else
{ is enum? }
if (taEnum in Attributes) Then
Begin
Result := 'enum-item of '+RefSymbol.Name;
End Else
{ is enum item? }
if (EnumBase <> nil) Then
Begin
Result := 'enum-item of '+EnumBase.RefSymbol.Name;
End Else
{ is primary? }
if (ArrayDimCount = 0) Then
Begin
if (isString) Then
Exit('char');
if (InternalID > High(PrimaryTypeNames)) Then
Begin
DevLog(dvError, 'InternalID > High(PrimaryTypeNames) [%d > %d]; returned `erroneous type`', [InternalID, High(PrimaryTypeNames)]);
Exit('erroneous type');
End;
Exit(PrimaryTypeNames[InternalID]);
End Else
Begin
{ is array? }
if (isString) or (ArrayPrimitive.isString) Then
Begin
I := ArrayDimCount-1;
Result += 'string';
End Else
Begin