-
Notifications
You must be signed in to change notification settings - Fork 28
/
lpvartypes_array.pas
1668 lines (1462 loc) · 58.2 KB
/
lpvartypes_array.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 (https://github.com/nielsAD/lape)
License: GNU Lesser GPL (http://www.gnu.org/licenses/lgpl.html)
Array types.
}
unit lpvartypes_array;
{$I lape.inc}
interface
uses
Classes, SysUtils,
lptypes, lptree, lpvartypes, lpvartypes_helper;
type
TLapeType_DynArray = class(TLapeType_Pointer)
protected
function getAsString: lpString; override;
function canInlineHelper(Field: TLapeGlobalVar; out Typ: TLapeType): Boolean;
public
constructor Create(ArrayType: TLapeType; ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil); reintroduce; virtual;
function CreateCopy(DeepCopy: Boolean = False): TLapeType; override;
function VarToStringBody(ToStr: TLapeType_OverloadedMethod = nil): lpString; override;
function VarToString(AVar: Pointer): lpString; override;
function VarLo(AVar: Pointer = nil): TLapeGlobalVar; override;
function VarHi(AVar: Pointer = nil): TLapeGlobalVar; override;
procedure VarSetLength(var AVar: Pointer; ALen: SizeInt); overload; virtual;
procedure VarSetLength(AVar, ALen: TResVar; var Offset: Integer; Pos: PDocPos = nil); overload; virtual;
procedure VarSetLength(AVar: TLapeGlobalVar; ALen: SizeInt); overload; virtual;
procedure RangeCheck(AVar, AIndex: TLapeGlobalVar; Flags: ELapeEvalFlags); overload; virtual;
procedure RangeCheck(var AVar, AIndex: TResVar; Flags: ELapeEvalFlags; var Offset: Integer; Pos: PDocPos = nil); overload; virtual;
function EvalRes(Op: EOperator; Right: TLapeGlobalVar; Flags: ELapeEvalFlags = []): TLapeType; override;
function EvalRes(Op: EOperator; ARight: TLapeType = nil; Flags: ELapeEvalFlags = []): TLapeType; override;
function EvalConst(Op: EOperator; ALeft, ARight: TLapeGlobalVar; Flags: ELapeEvalFlags): TLapeGlobalVar; override;
function Eval(Op: EOperator; var Dest: TResVar; ALeft, ARight: TResVar; Flags: ELapeEvalFlags; var Offset: Integer; Pos: PDocPos = nil): TResVar; override;
procedure addArrayHelper(Helper: TLapeType_HelperClass; AName: String); virtual;
procedure addArrayHelpers; virtual;
end;
TLapeType_StaticArray = class(TLapeType_DynArray)
protected
FRange: TLapeRange;
function getPadding: SizeInt; override;
function getSize: SizeInt; override;
function getAsString: lpString; override;
public
constructor Create(ARange: TLapeRange; ArrayType: TLapeType; ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil); reintroduce; virtual;
function CreateCopy(DeepCopy: Boolean = False): TLapeType; override;
function VarToString(AVar: Pointer): lpString; override;
function VarLo(AVar: Pointer = nil): TLapeGlobalVar; override;
function VarHi(AVar: Pointer = nil): TLapeGlobalVar; override;
function NewGlobalVar(AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar; 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;
procedure Finalize(AVar: TResVar; var Offset: Integer; UseCompiler: Boolean = True; Pos: PDocPos = nil); override;
procedure addArrayHelpers; override;
property Range: TLapeRange read FRange;
end;
TLapeType_String = class(TLapeType_DynArray)
public
function VarToString(AVar: Pointer): lpString; override;
procedure VarUnique(var AVar: Pointer); overload; virtual;
procedure VarUnique(AVar: TResVar; var Offset: Integer; Pos: PDocPos = nil); overload; virtual;
function VarLo(AVar: Pointer = nil): TLapeGlobalVar; override;
function VarHi(AVar: Pointer = nil): TLapeGlobalVar; override;
function NewGlobalVarStr(Str: AnsiString; AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar; override;
function NewGlobalVar(Str: AnsiString; AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar; reintroduce; overload; virtual;
function NewGlobalVarStr(Str: UnicodeString; AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar; override;
function NewGlobalVar(Str: UnicodeString; AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar; reintroduce; overload; virtual;
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 addArrayHelpers; override;
end;
TLapeType_AnsiString = class(TLapeType_String)
public constructor Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil); reintroduce; virtual; end;
TLapeType_WideString = class(TLapeType_String)
public constructor Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil); reintroduce; virtual; end;
TLapeType_UnicodeString = class(TLapeType_String)
public constructor Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil); reintroduce; virtual; end;
TLapeType_ShortString = class(TLapeType_StaticArray)
protected
function getAsString: lpString; override;
public
constructor Create(ACompiler: TLapeCompilerBase; ASize: UInt8 = High(UInt8); AName: lpString = ''; ADocPos: PDocPos = nil); reintroduce; virtual;
function VarToString(AVar: Pointer): lpString; override;
function VarLo(AVar: Pointer = nil): TLapeGlobalVar; override;
function VarHi(AVar: Pointer = nil): TLapeGlobalVar; override;
function NewGlobalVarStr(Str: UnicodeString; AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar; override;
function NewGlobalVar(Str: ShortString; AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar; reintroduce; overload; virtual;
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;
implementation
uses
lpparser, lpeval, lpmessages, lpinternalmethods, lpeval_extra;
function TLapeType_DynArray.getAsString: lpString;
begin
if (FAsString = '') and (FBaseType = ltDynArray) then
if HasType() then
FAsString := 'array of ' + FPType.AsString
else
FAsString := 'array';
Result := inherited;
end;
function TLapeType_DynArray.canInlineHelper(Field: TLapeGlobalVar; out Typ: TLapeType): Boolean;
var
Decl: TLapeDeclaration;
begin
if ValidFieldName(Field) and FManagedDecls.Get(PlpString(Field.Ptr)^, TLapeGlobalVar, Decl, bTrue) then
begin
Typ := TLapeGlobalVar(Decl).VarType;
Result := (Typ is TLapeType_ArrayHelper_First) or
(Typ is TLapeType_ArrayHelper_Last) or
(Typ is TLapeType_ArrayHelper_Pop) or
(Typ is TLapeType_ArrayHelper_High) or
(Typ is TLapeType_ArrayHelper_Low) or
(Typ is TLapeType_ArrayHelper_Length);
end else
Result := False;
end;
constructor TLapeType_DynArray.Create(ArrayType: TLapeType; ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil);
begin
inherited Create(ACompiler, ArrayType, False, AName, ADocPos);
FBaseType := ltDynArray;
addArrayHelpers();
end;
function TLapeType_DynArray.CreateCopy(DeepCopy: Boolean = False): TLapeType;
type
TLapeClassType = class of TLapeType_DynArray;
begin
Result := TLapeClassType(Self.ClassType).Create(FPType, FCompiler, Name, @_DocPos);
with TLapeType_DynArray(Result) do
begin
inheritManagedDecls(Self, not DeepCopy);
TypeID := Self.TypeID;
FBaseType := Self.BaseType;
CopyHints(Self);
end;
end;
function TLapeType_DynArray.VarToStringBody(ToStr: TLapeType_OverloadedMethod = nil): lpString;
var
Index: Integer;
begin
Result := '';
if (ToStr = nil) or (FCompiler = nil) or (not HasType()) then
Exit;
Index := ToStr.getMethodIndex(getTypeArray([PType]), FCompiler.getBaseType(ltString));
if (Index < 0) then
Exit;
Result := lpString(
' function _ElementToString(p: System.ConstPointer): System.string; static;' + LineEnding +
' begin' + LineEnding +
' Result := System.ToString['+IntToStr(Index)+'](p^);' + LineEnding +
' end;' + LineEnding +
'type' + LineEnding +
' TArrayToString = private function(constref Arr; AToString: System.ConstPointer; Len, Size: System.SizeInt): System.string;' + LineEnding +
'var' + LineEnding +
' Len: System.SizeInt;' + LineEnding +
'begin' + LineEnding +
' Len := System.Length(Param0);' + LineEnding +
' if (Len <= 0) then' + LineEnding +
' Result := '#39'[]'#39'' + LineEnding +
' else' + LineEnding +
' Result := TArrayToString(System._ArrayToString)(' + LineEnding +
' Param0['+IntToStr(VarLo().AsInteger)+'],' + LineEnding +
' _ElementToString, Len, System.SizeOf(Param0[0]));' + LineEnding +
'end;');
end;
function TLapeType_DynArray.VarToString(AVar: Pointer): lpString;
var
i: Integer;
p: Pointer;
Builder: TLapeStringList;
begin
Result := '[';
if (AVar <> nil) and HasType() then
try
p := PPointer(AVar)^;
Builder := TLapeStringList.Create('', dupAccept, True, False);
for i := 0 to Length(PCodeArray(AVar)^) - 1 do
Builder.Add(FPType.VarToString(Pointer(PtrUInt(p) + PtrUInt(FPType.Size * i))));
Result := Result + Builder.Implode(', ');
finally
Builder.Free();
end;
Result := Result + ']';
end;
function TLapeType_DynArray.VarLo(AVar: Pointer = nil): TLapeGlobalVar;
begin
if (FCompiler = nil) then
Result := nil
else
Result := FCompiler.getConstant(0, ltSizeInt);
end;
function TLapeType_DynArray.VarHi(AVar: Pointer = nil): TLapeGlobalVar;
begin
if (FCompiler = nil) or (AVar = nil) then
Result := nil
else
Result := FCompiler.getConstant(High(PCodeArray(AVar)^), ltSizeInt);
end;
procedure TLapeType_DynArray.VarSetLength(var AVar: Pointer; ALen: SizeInt);
var
i, OldLen, NewSize: SizeInt;
NewP: Pointer;
DoFree: Boolean;
tmpLeft, tmpRight: TLapeGlobalVar;
begin
if (not (BaseType in LapeArrayTypes - [ltStaticArray])) then
LapeException(lpeInvalidEvaluation);
if (not HasType()) then
Exit;
if (BaseType in LapeStringTypes) then
begin
case BaseType of
ltAnsiString: SetLength(AnsiString(AVar), ALen);
ltWideString: SetLength(WideString(AVar), ALen);
ltUnicodeString: SetLength(UnicodeString(AVar), ALen);
ltShortString:
begin
tmpRight := VarHi();
if (ALen < 0) or (tmpRight = nil) or (ALen > tmpRight.AsInteger) then
LapeException(lpeOutOfTypeRange)
else
SetLength(PShortString(AVar)^, ALen);
end
else LapeException(lpeImpossible);
end;
Exit;
end;
NewSize := ALen * PType.Size;
DoFree := NewSize <= 0;
Inc(NewSize, SizeOf(PtrInt) + SizeOf(SizeInt));
if (AVar = nil) then
begin
if DoFree then
Exit;
AVar := AllocMem(NewSize);
PtrInt(AVar^) := 1;
Inc(PtrUInt(AVar), SizeOf(PtrInt));
SizeInt(AVar^) := ALen {$IFDEF FPC}-1{$ENDIF};
Inc(PtrUInt(AVar), SizeOf(SizeInt));
Exit;
end;
Dec(PtrUInt(AVar), SizeOf(SizeInt));
OldLen := SizeInt(AVar^) {$IFDEF FPC}+1{$ENDIF};
Dec(PtrUInt(AVar), SizeOf(PtrInt));
if (PtrInt(AVar^) <= 1) then
begin
if (ALen = OldLen) then
begin
Inc(PtrUInt(AVar), SizeOf(SizeInt) + SizeOf(PtrInt));
Exit;
end;
if (ALen < OldLen) and PType.NeedFinalization then
begin
Inc(PtrUInt(AVar), SizeOf(SizeInt) + SizeOf(PtrInt));
i := ALen;
while (i < OldLen) do
begin
tmpLeft := PType.NewGlobalVarP(Pointer(PtrInt(AVar) + (i * PType.Size)));
try
PType.Finalize(tmpLeft);
finally
tmpLeft.Free();
end;
Inc(i);
end;
Dec(PtrUInt(AVar), SizeOf(SizeInt) + SizeOf(PtrInt));
end;
if DoFree then
begin
FreeMem(AVar);
AVar := nil;
Exit;
end;
ReallocMem(AVar, NewSize);
PtrInt(AVar^) := 1;
Inc(PtrUInt(AVar), SizeOf(PtrInt));
SizeInt(AVar^) := ALen {$IFDEF FPC}-1{$ENDIF};
Inc(PtrUInt(AVar), SizeOf(SizeInt));
if (ALen > OldLen) then
FillChar(Pointer(PtrInt(AVar) + (OldLen * PType.Size))^, (ALen - OldLen) * PType.Size, 0);
end
else
begin
Dec(PtrInt(AVar^));
NewP := nil;
VarSetLength(NewP, ALen);
i := OldLen;
if (ALen < OldLen) then
i := ALen;
Inc(PtrUInt(AVar), SizeOf(PtrInt) + SizeOf(SizeInt));
Dec(i);
while (i >= 0) do
begin
tmpLeft := PType.NewGlobalVarP(Pointer(PtrInt(NewP) + (i * PType.Size)));
tmpRight := PType.NewGlobalVarP(Pointer(PtrInt(AVar) + (i * PType.Size)));
try
PType.EvalConst(op_Assign, tmpLeft, tmpRight, []);
finally
tmpLeft.Free();
tmpRight.Free();
end;
Dec(i);
end;
AVar := NewP;
end;
end;
procedure TLapeType_DynArray.VarSetLength(AVar, ALen: TResVar; var Offset: Integer; Pos: PDocPos = nil);
begin
Assert(FCompiler <> nil);
//FCompiler.EmitCode('System.SetLength(AVar, ALen);', ['AVar', 'ALen'], [], [AVar, ALen], Offset, Pos);
with TLapeTree_InternalMethod_SetLength.Create(FCompiler, Pos) do
try
addParam(TLapeTree_ResVar.Create(AVar, FCompiler, Pos));
addParam(TLapeTree_ResVar.Create(ALen, FCompiler, Pos));
Compile(Offset).Spill(1);
finally
Free();
end;
end;
procedure TLapeType_DynArray.VarSetLength(AVar: TLapeGlobalVar; ALen: SizeInt);
var
Arr: Pointer;
begin
if AVar.isNull() then
Arr := nil
else
Arr := PPointer(AVar.Ptr)^;
VarSetLength(Arr, ALen);
PPointer(AVar.Ptr)^ := Arr;
end;
procedure TLapeType_DynArray.RangeCheck(AVar, AIndex: TLapeGlobalVar; Flags: ELapeEvalFlags);
var
Idx: SizeInt;
Lo, Hi: TLapeGlobalVar;
begin
if (AVar = nil) or (not AIndex.HasType()) then
LapeException(lpeInvalidEvaluation)
else if (not AIndex.VarType.IsOrdinal()) then
LapeExceptionFmt(lpeInvalidIndex, [AIndex.VarType.AsString])
else if (not (lefConstRangeCheck in Flags)) then
Exit;
Idx := AIndex.AsInteger;
if (FBaseType in LapeStringTypes) then
Inc(Idx);
Lo := VarLo(AVar.Ptr);
Hi := VarHi(AVar.Ptr);
if ((Lo <> nil) and (Idx < Lo.AsInteger)) or ((Hi <> nil) and (Idx > Hi.AsInteger)) then
if (Lo <> nil) and (Hi <> nil) then
LapeExceptionFmt(lpeIndexOutOfRange, [idx, Lo.AsInteger, Hi.AsInteger])
else if (Lo <> nil) then
LapeExceptionFmt(lpeIndexOutOfRangeLow, [idx, Lo.AsInteger])
else if (Hi <> nil) then
LapeExceptionFmt(lpeIndexOutOfRangeHigh, [idx, Hi.AsInteger]);
end;
procedure TLapeType_DynArray.RangeCheck(var AVar, AIndex: TResVar; Flags: ELapeEvalFlags; var Offset: Integer; Pos: PDocPos = nil);
var
Idx: SizeInt;
Lo, Hi: TLapeGlobalVar;
Check, Len: TLapeTree_Invoke;
TempVar, DestVar, ArrayPtrVar: TResVar;
begin
if (not AIndex.HasType()) then
LapeException(lpeInvalidEvaluation)
else if (not AIndex.VarType.IsOrdinal()) then
LapeExceptionFmt(lpeInvalidIndex, [AIndex.VarType.AsString])
else if (([lefRangeCheck, lefConstRangeCheck]) * Flags = []) then
Exit;
if AVar.isConstant and (AVar.VarPos.MemPos = mpMem) then
begin
Lo := VarLo(AVar.VarPos.GlobalVar.Ptr);
Hi := VarHi(AVar.VarPos.GlobalVar.Ptr);
end
else
begin
Lo := VarLo();
Hi := VarHi();
end;
if (lefConstRangeCheck in Flags) and
AVar.HasType() and (AVar.VarType.BaseType in LapeArrayTypes) and
AIndex.isConstant and (AIndex.VarPos.MemPos = mpMem)
then
begin
Idx := AIndex.VarPos.GlobalVar.AsInteger;
if ((Lo <> nil) and (Idx < Lo.AsInteger)) or ((Hi <> nil) and (Idx > Hi.AsInteger)) then
begin
if (Lo <> nil) and (Hi <> nil) then
LapeExceptionFmt(lpeIndexOutOfRange, [idx, Lo.AsInteger, Hi.AsInteger])
else if (Lo <> nil) then
LapeExceptionFmt(lpeIndexOutOfRangeLow, [idx, Lo.AsInteger])
else if (Hi <> nil) then
LapeExceptionFmt(lpeIndexOutOfRangeHigh, [idx, Hi.AsInteger]);
end
else if (Lo <> nil) and (Hi <> nil) then
Exit;
end;
if (not (lefRangeCheck in Flags)) then
Exit;
if (BaseType = ltDynArray) then
begin
AIndex.VarType := FCompiler.getBaseType(AIndex.VarType.BaseIntType);
if (AIndex.VarPos.MemPos = mpStack) then
begin
AIndex := _ResVar.New(FCompiler.getTempVar(AIndex.VarType));
FCompiler.Emitter._PopStackToVar(AIndex.VarType.Size, AIndex.VarPos.StackVar.Offset, Offset, @_DocPos);
end;
FCompiler.Emitter._Eval(getEvalProc_DynArrayRangeCheck(AIndex.VarType.BaseIntType), AVar, AVar, AIndex, Offset, Pos);
end else
begin
AIndex.VarType := FCompiler.getBaseType(AIndex.VarType.BaseIntType);
if (AIndex.VarPos.MemPos = mpStack) then
begin
AIndex := _ResVar.New(FCompiler.getTempVar(AIndex.VarType));
FCompiler.Emitter._PopStackToVar(AIndex.VarType.Size, AIndex.VarPos.StackVar.Offset, Offset, @_DocPos);
end;
Check := TLapeTree_Invoke.Create('_RangeCheck', FCompiler, Pos);
with Check do
try
addParam(TLapeTree_ResVar.Create(AIndex.IncLock(), Check));
Len := TLapeTree_InternalMethod_Low.Create(FCompiler, Pos);
Len.addParam(TLapeTree_ResVar.Create(AVar.IncLock(), Check));
addParam(TLapeTree_ExprBase(Len.FoldConstants()));
Len := TLapeTree_InternalMethod_High.Create(FCompiler, Pos);
Len.addParam(TLapeTree_ResVar.Create(AVar.IncLock(), Check));
addParam(TLapeTree_ExprBase(Len.FoldConstants()));
Compile(Offset).Spill(1);
finally
Free();
end;
end;
end;
function TLapeType_DynArray.EvalRes(Op: EOperator; Right: TLapeGlobalVar; Flags: ELapeEvalFlags): TLapeType;
var
Typ: TLapeType;
begin
if (Op = op_Dot) and CanInlineHelper(Right, Typ) then
if (Typ is TLapeType_ArrayHelper_First) or (Typ is TLapeType_ArrayHelper_Last) or (Typ is TLapeType_ArrayHelper_Pop) then
Result := FPType
else if (Typ is TLapeType_ArrayHelper_Low) or (Typ is TLapeType_ArrayHelper_High) or (Typ is TLapeType_ArrayHelper_Length) then
Result := FCompiler.getBaseType(ltSizeInt)
else
Result := nil
else
Result := inherited EvalRes(Op, Right, Flags);
end;
function TLapeType_DynArray.EvalRes(Op: EOperator; ARight: TLapeType = nil; Flags: ELapeEvalFlags = []): TLapeType;
begin
if (op = op_Index) then
Result := FPType
else if (op = op_Assign) and (BaseType = ltDynArray) and HasType() and (ARight <> nil) and (ARight is ClassType) and FPType.Equals(TLapeType_DynArray(ARight).FPType) then
Result := Self
else if (op = op_Plus) and (BaseType = ltDynArray) and HasType() and FPType.CompatibleWith(ARight) then
Result := Self
else if (op = op_Plus) and (BaseType = ltDynArray) and HasType() and (ARight <> nil) and (ARight is ClassType) and FPType.Equals(TLapeType_DynArray(ARight).FPType) then
Result := Self
else
Result := inherited;
end;
function TLapeType_DynArray.EvalConst(Op: EOperator; ALeft, ARight: TLapeGlobalVar; Flags: ELapeEvalFlags): TLapeGlobalVar;
var
tmpType: ELapeBaseType;
IndexVar: TLapeGlobalVar;
begin
Assert(FCompiler <> nil);
Assert((ALeft = nil) or (ALeft.VarType is TLapeType_Pointer));
if (op = op_Index) then
begin
//Cache AsString before changing FBaseType
GetAsString();
RangeCheck(ALeft, ARight, Flags);
tmpType := FBaseType;
FBaseType := ltPointer;
IndexVar := nil;
try
IndexVar := inherited EvalConst(Op, ALeft, ARight, Flags);
Result := //Result := Pointer[Index]^
EvalConst(
op_Deref,
IndexVar,
nil,
[]
);
Result.CopyFlags(ALeft);
finally
if (IndexVar <> nil) then
IndexVar.Free();
FBaseType := tmpType;
end;
end
else if (op = op_Assign) and (BaseType = ltDynArray) and (ALeft <> nil) and (ARight <> nil) and CompatibleWith(ARight.VarType) then
if (ARight.BaseType = ltDynArray) then
begin
if (PPointer(ALeft.Ptr)^ = PPointer(ARight.Ptr)^) then
Exit(ALeft);
VarSetLength(PPointer(ALeft.Ptr)^, 0);
Result := inherited;
end
else if (ARight.VarType is TLapeType_StaticArray) then
begin
VarSetLength(PPointer(ALeft.Ptr)^, TLapeType_StaticArray(ARight.VarType).Range.Hi - TLapeType_StaticArray(ARight.VarType).Range.Lo + 1);
Result := ALeft;
IndexVar := EvalConst(op_Index, ALeft, FCompiler.getConstant(0), [lefAssigning]);
try
IndexVar.VarType := ARight.VarType;
IndexVar.VarType.EvalConst(op_Assign, IndexVar, ARight, []);
finally
IndexVar.Free();
end;
end
else
LapeException(lpeImpossible)
else if (op = op_Plus) and (BaseType = ltDynArray) and (ALeft <> nil) and (ARight <> nil) and HasType() and FPType.CompatibleWith(ARight.VarType) then
begin
Result := EvalConst(op_Assign, NewGlobalVarP(), ALeft, []);
IndexVar := FCompiler.getConstant(Length(PCodeArray(Result.Ptr)^));
VarSetLength(PPointer(Result.Ptr)^, IndexVar.AsInteger + 1);
IndexVar := EvalConst(op_Index, Result, IndexVar, [lefAssigning]);
try
PType.EvalConst(op_Assign, IndexVar, ARight, []);
finally
IndexVar.Free();
end;
end
else
Result := inherited;
end;
function TLapeType_DynArray.Eval(Op: EOperator; var Dest: TResVar; ALeft, ARight: TResVar; Flags: ELapeEvalFlags; var Offset: Integer; Pos: PDocPos = nil): TResVar;
var
tmpType: ELapeBaseType;
tmpVar: TLapeStackTempVar;
IndexVar, tmpResVar: TResVar;
wasConstant: Boolean;
Node: TLapeTree_Base;
Typ: TLapeType;
begin
Assert(FCompiler <> nil);
Assert(ALeft.VarType is TLapeType_Pointer);
IndexVar := NullResVar;
tmpResVar := NullResVar;
tmpVar := nil;
if (op = op_Index) then
begin
//Cache AsString before changing FBaseType
GetAsString();
RangeCheck(ALeft, ARight, Flags, Offset, Pos);
tmpType := FBaseType;
FBaseType := ltPointer;
try
if (not ALeft.VarPos.isPointer) then
begin
if (Dest.VarPos.MemPos = mpStack) then
Dest := NullResVar;
Result := inherited Eval(Op, Dest, ALeft, ARight, Flags, Offset, Pos);
Result.VarPos.isPointer := True;
Result.VarType := FPType;
end
else
begin
IndexVar := inherited Eval(Op, IndexVar, ALeft, ARight, Flags, Offset, Pos);
Result := //Result := Pointer[Index]^
Eval(
op_Deref,
Dest,
IndexVar,
NullResVar,
[],
Offset,
Pos
);
IndexVar.Spill(1);
end;
finally
FBaseType := tmpType;
Result.CopyFlags(ALeft);
end;
end
else if (op = op_Assign) and (BaseType = ltDynArray) and CompatibleWith(ARight.VarType) then
begin
Dest := NullResVar;
Result := ALeft;
if (ALeft.VarPos.MemPos = mpStack) then
begin
tmpVar := FCompiler.getTempVar(Self);
ALeft := _ResVar.New(tmpVar);
end;
if (ARight.VarType.BaseType = ltDynArray) then
begin
{FCompiler.EmitCode(
'if (System.Pointer(Left) <> System.Pointer(nil)) and' +
' (System.Pointer(Left) <> System.Pointer(Right)) then' +
'begin' +
' System.SetLength(Left, 0);' +
' System.Pointer(Left) := System.Pointer(Right);' +
' if (System.Pointer(Left) <> nil) then' +
' System.Inc(System.PtrInt(System.Pointer(Left)[-' +
IntToStr(SizeOf(SizeInt)+SizeOf(PtrInt)) +
']^));' +
'end;'
, ['Left', 'Right'], [], [ALeft, ARight], Offset, Pos)}
with TLapeTree_If.Create(FCompiler, Pos) do
try
Condition := TLapeTree_Operator.Create(op_AND, Self.FCompiler, Pos);
TLapeTree_Operator(Condition).Left := TLapeTree_Operator.Create(op_cmp_NotEqual, Self.FCompiler);
with TLapeTree_Operator(TLapeTree_Operator(Condition).Left) do
begin
Left := TLapeTree_ResVar.Create(ALeft.IncLock(), Condition);
Right := TLapeTree_GlobalVar.Create('nil', ltPointer, Condition);
end;
TLapeTree_Operator(Condition).Right := TLapeTree_Operator.Create(op_cmp_NotEqual, Self.FCompiler);
with TLapeTree_Operator(TLapeTree_Operator(Condition).Right) do
begin
Left := TLapeTree_ResVar.Create(ALeft.IncLock(), Condition);
Right := TLapeTree_ResVar.Create(ARight.IncLock(), Condition);
end;
Body := TLapeTree_InternalMethod_SetLength.Create(Condition);
with TLapeTree_InternalMethod_SetLength(Body) do
begin
addParam(TLapeTree_ResVar.Create(ALeft.IncLock(), Body));
addParam(TLapeTree_Integer.Create(0, Body));
end;
Compile(Offset).Spill(1);
finally
Free();
end;
inherited;
end
else if (ARight.VarType is TLapeType_StaticArray) then
begin
{FCompiler.EmitCode(
'System.SetLength(Left, '+IntToStr(
TLapeType_StaticArray(ARight.VarType).Range.Hi -
TLapeType_StaticArray(ARight.VarType).Range.Lo + 1) +
');' +
'PType(@Left[0])^ := Right;',
['PType', 'Left', 'Right'], [FCompiler.getTypeVar(FCompiler.getPointerType(ARight.VarType))],
[ALeft, ARight], Offset, Pos)}
with TLapeType_StaticArray(ARight.VarType).Range do
VarSetLength(ALeft.IncLock(), _ResVar.New(FCompiler.getConstant(Hi - Lo + 1)), Offset, Pos);
with TLapeTree_Operator.Create(op_Index, FCompiler, Pos) do
try
Left := TLapeTree_ResVar.Create(ALeft.IncLock(), FCompiler, Pos);
Right := TLapeTree_Integer.Create(0, Left);
CompilerOptions := CompilerOptions - [lcoRangeCheck];
IndexVar := Compile(Offset);
finally
Free();
end;
IndexVar.VarType := ARight.VarType;
with TLapeTree_Operator.Create(op_Assign, FCompiler, Pos) do
try
Left := TLapeTree_ResVar.Create(IndexVar.IncLock(), FCompiler, Pos);
Right := TLapeTree_ResVar.Create(ARight.IncLock(), Left);
Compile(Offset);
finally
Free();
end;
end
else
LapeException(lpeImpossible);
if (tmpVar <> nil) then
begin
FCompiler.Emitter._PopVarToStack(Size, tmpVar.Offset, Offset, Pos);
ALeft.Spill(1);
end;
end
else if (op = op_Plus) and (BaseType = ltDynArray) and HasType() and FPType.CompatibleWith(ARight.VarType) then
begin
Result := NullResVar;
Result.VarType := Self;
FCompiler.getDestVar(Dest, Result, op);
if (Result.VarPos.MemPos = mpStack) then
begin
tmpVar := FCompiler.getTempVar(Self);
Result := _ResVar.New(tmpVar);
end;
wasConstant := not Result.Writeable;
if wasConstant then
Result.Writeable := True;
Result := Eval(op_Assign, tmpResVar, Result, ALeft, [], Offset, Pos);
{FCompiler.EmitCode(
'System.SetLength(Result, System.Length(Result) + 1);' +
'Result[System.High(Result)] := Right;'
, ['Result', 'Right'], [], [Result, ARight], Offset, Pos);}
IndexVar := _ResVar.New(FCompiler.getTempVar(ltSizeInt));
with TLapeTree_Operator.Create(op_Plus, FCompiler, Pos) do
try
Left := TLapeTree_Operator.Create(op_Assign, FCompiler, Pos);
Right := TLapeTree_Integer.Create(1, Left);
with TLapeTree_Operator(Left) do
begin
Left := TLapeTree_ResVar.Create(IndexVar.IncLock(2), FCompiler, Pos);
Right := TLapeTree_InternalMethod_Length.Create(Left);
TLapeTree_InternalMethod_Length(Right).addParam(TLapeTree_ResVar.Create(Result.IncLock(), Left));
end;
Dest := VarResVar;
tmpResVar := Compile(Offset);
finally
Free();
end;
VarSetLength(Result.IncLock(), tmpResVar, Offset, Pos);
with TLapeTree_Operator.Create(op_Assign, FCompiler, Pos) do
try
Left := TLapeTree_Operator.Create(op_Index, FCompiler, Pos);
with TLapeTree_Operator(Left) do
begin
CompilerOptions := CompilerOptions - [lcoRangeCheck];
Left := TLapeTree_ResVar.Create(Result.IncLock(), FCompiler, Pos);
Right := TLapeTree_ResVar.Create(IndexVar.IncLock(), Left);
end;
Right := TLapeTree_ResVar.Create(ARight.IncLock(), Left);
Compile(Offset);
finally
Free();
end;
IndexVar.Spill(1);
if wasConstant then
Result.Writeable := False;
end
else if (op = op_Plus) and (BaseType = ltDynArray) and CompatibleWith(ARight.VarType) then
begin
//Result := NullResVar;
//Result.VarType := Self;
//FCompiler.getDestVar(Dest, Result, op);
//
//if (Result.VarPos.MemPos = mpStack) then
//begin
// tmpVar := FCompiler.getTempVar(Self);
// Result := _ResVar.New(tmpVar);
//end;
Result := _ResVar.New(FCompiler.getTempVar(Self));
Dest := NullResVar;
wasConstant := not Result.Writeable;
if wasConstant then Result.Writeable := True;
Result := Eval(op_Assign, tmpResVar, Result, ALeft, [], Offset, Pos);
with TLapeTree_InternalMethod_Insert.Create(FCompiler, Pos) do
try
addParam(TLapeTree_ResVar.Create(ARight, FCompiler, Pos));
addParam(TLapeTree_ResVar.Create(Result.IncLock(), FCompiler, Pos));
Node := TLapeTree_InternalMethod_Length.Create(FCompiler, Pos);
TLapeTree_InternalMethod_Length(Node).addParam(
TLapeTree_ResVar.Create(Result.IncLock(), FCompiler)
);
addParam(Node as TLapeTree_ExprBase);
Compile(Offset);
finally
Free();
end;
if wasConstant then Result.Writeable := False;
end else if (Op = op_Dot) and ValidFieldName(ARight) and CanInlineHelper(ARight.VarPos.GlobalVar, Typ) then
begin
Dest := NullResVar;
if (Typ is TLapeType_ArrayHelper_First) or
(Typ is TLapeType_ArrayHelper_Last) or
(Typ is TLapeType_ArrayHelper_Pop) then
begin
if (Typ is TLapeType_ArrayHelper_First) then
IndexVar := _ResVar.New(VarLo())
else
with TLapeTree_InternalMethod_High.Create(FCompiler, Pos) do
try
addParam(TLapeTree_ResVar.Create(ALeft, FCompiler, Pos));
IndexVar := Compile(Offset);
finally
Free();
end;
Result := _ResVar.New(FCompiler.getTempVar(FPType, 1));
Result := FPType.Eval(op_Assign, tmpResVar, Result, Eval(op_Index, tmpResVar, ALeft, IndexVar, [lefRangeCheck], Offset, Pos), [], Offset, Pos);
Result.isConstant := True;
if (Typ is TLapeType_ArrayHelper_Pop) then
with TLapeTree_InternalMethod_SetLength.Create(FCompiler, Pos) do
try
addParam(TLapeTree_ResVar.Create(ALeft.IncLock(), FCompiler, Pos));
// Length=High for strings because 1 based indexing...
if (ALeft.VarType.BaseType in LapeStringTypes) then
begin
addParam(TLapeTree_InternalMethod_Pred.Create(FCompiler, Pos));
TLapeTree_InternalMethod_Pred(Params[1]).addParam(TLapeTree_ResVar.Create(IndexVar.IncLock(), FCompiler, Pos));
end else
addParam(TLapeTree_ResVar.Create(IndexVar.IncLock(), FCompiler, Pos));
Compile(Offset);
finally
Free();
end;
end
else if (Typ is TLapeType_ArrayHelper_Low) then
with TLapeTree_InternalMethod_Low.Create(FCompiler, Pos) do
try
addParam(TLapeTree_ResVar.Create(ALeft, FCompiler, Pos));
Result := FoldConstants(False).Compile(Offset);
finally
Free();
end
else if (Typ is TLapeType_ArrayHelper_High) then
with TLapeTree_InternalMethod_High.Create(FCompiler, Pos) do
try
addParam(TLapeTree_ResVar.Create(ALeft, FCompiler, Pos));
Result := FoldConstants(False).Compile(Offset);
finally
Free();
end
else if (Typ is TLapeType_ArrayHelper_Length) then
with TLapeTree_InternalMethod_Length.Create(FCompiler, Pos) do
try
addParam(TLapeTree_ResVar.Create(ALeft, FCompiler, Pos));
Result := FoldConstants(False).Compile(Offset);
finally
Free();
end
else
LapeException(lpeImpossible, DocPos);
Assert(Result.isConstant and ((Result.VarPos.MemPos <> mpVar) or (Result.Lock > 0)));
end else
Result := inherited;
end;
procedure TLapeType_DynArray.addArrayHelper(Helper: TLapeType_HelperClass; AName: String);
begin
Assert(lcoArrayHelpers in FCompiler.Options);
FManagedDecls.addDeclaration(
TLapeType_Helper(FCompiler.addManagedType(Helper.Create(FCompiler, AName))).NewGlobalVar(AName)
);
end;
procedure TLapeType_DynArray.addArrayHelpers;
begin
if (not (lcoArrayHelpers in FCompiler.Options)) then
Exit;
// properties, these are done "inlined" in DynArray.Eval
addArrayHelper(TLapeType_ArrayHelper_Low, 'Low');
addArrayHelper(TLapeType_ArrayHelper_High, 'High');
addArrayHelper(TLapeType_ArrayHelper_Length, 'Length');
addArrayHelper(TLapeType_ArrayHelper_First, 'First');
addArrayHelper(TLapeType_ArrayHelper_Last, 'Last');
addArrayHelper(TLapeType_ArrayHelper_Pop, 'Pop');
// functions
addArrayHelper(TLapeType_ArrayHelper_Contains, 'Contains');
addArrayHelper(TLapeType_ArrayHelper_Remove, 'Remove');
addArrayHelper(TLapeType_ArrayHelper_Delete, 'Delete');
addArrayHelper(TLapeType_ArrayHelper_Insert, 'Insert');
addArrayHelper(TLapeType_ArrayHelper_Swap, 'Swap');
addArrayHelper(TLapeType_ArrayHelper_Unique, 'Unique');
addArrayHelper(TLapeType_ArrayHelper_IndexOf, 'IndexOf');
addArrayHelper(TLapeType_ArrayHelper_IndicesOf, 'IndicesOf');
addArrayHelper(TLapeType_ArrayHelper_Sort, 'Sort');
addArrayHelper(TLapeType_ArrayHelper_Sorted, 'Sorted');
addArrayHelper(TLapeType_ArrayHelper_SetLength, 'SetLength');
addArrayHelper(TLapeType_ArrayHelper_Copy, 'Copy');
addArrayHelper(TLapeType_ArrayHelper_Random, 'Random');
addArrayHelper(TLapeType_ArrayHelper_Reverse, 'Reverse');
addArrayHelper(TLapeType_ArrayHelper_Reversed, 'Reversed');
addArrayHelper(TLapeType_ArrayHelper_Clear, 'Clear');
addArrayHelper(TLapeType_ArrayHelper_Append, 'Append');
addArrayHelper(TLapeType_ArrayHelper_Extend, 'Extend');
addArrayHelper(TLapeType_ArrayHelper_Slice, 'Slice');
addArrayHelper(TLapeType_ArrayHelper_Difference, 'Difference');
addArrayHelper(TLapeType_ArrayHelper_SymDifference, 'SymDifference');
addArrayHelper(TLapeType_ArrayHelper_Intersection, 'Intersection');
addArrayHelper(TLapeType_ArrayHelper_Equals, 'Equals');
// properties
addArrayHelper(TLapeType_ArrayHelper_Median, 'Median');
addArrayHelper(TLapeType_ArrayHelper_Mode, 'Mode');
addArrayHelper(TLapeType_ArrayHelper_Min, 'Min');
addArrayHelper(TLapeType_ArrayHelper_Max, 'Max');
addArrayHelper(TLapeType_ArrayHelper_Sum, 'Sum');
addArrayHelper(TLapeType_ArrayHelper_Mean, 'Mean');
addArrayHelper(TLapeType_ArrayHelper_Variance, 'Variance');
addArrayHelper(TLapeType_ArrayHelper_Stdev, 'Stdev');
end;
function TLapeType_StaticArray.getPadding: SizeInt;
begin
if HasType() then
Result := (FRange.Hi - FRange.Lo + 1) * FPType.Padding
else
Result := inherited;
end;
function TLapeType_StaticArray.getSize: SizeInt;
begin