Repository navigation
Expand file tree
/
Copy pathuSyntax.pas
More file actions
executable file
·1591 lines (1478 loc) · 50.2 KB
/
Copy pathuSyntax.pas
File metadata and controls
executable file
·1591 lines (1478 loc) · 50.2 KB
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
unit uSyntax;
{ ThinkSQL Relational Database Management System
Copyright © 2000-2012 Greg Gaughan
See LICENCE.txt for details
}
{Syntax Tree routines}
//{$DEFINE DEBUGDETAIL}
//{$DEFINE DEBUGDETAIL2}
//{$DEFINE DEBUGDETAIL3}
interface
uses uGlobal;
type //note: subtract 10 from line number to get ORD()
nodeType=(ntNOP, //used for syntax tree manipulation
ntOperator,
ntExpression,
ntCommand, //-
ntCreateSchema,
ntAuthorization,
ntCreateTable,
ntCreateView,
ntCreateRoutine, //-
ntRoutine,
ntProcedure,
ntFunction,
ntProcedureOrFunction,
ntParameterDef,
ntVariableRef, //note: converted by evalConExpr.complete from ntColumnRef
ntIn,
ntOut,
ntInOut,
ntResult,
ntCallRoutine,
ntUserFunction,
ntDeclaration,
ntAssignment,
ntReturn,
ntCursorDeclaration,
ntSensitive,
ntInsensitive,
ntAsensitive,
ntScroll,
ntCursorHold,
ntCursorReturn,
ntForReadOnly,
ntForUpdate,
ntOpen,
ntClose,
ntFetch,
ntNext,
ntPrior,
ntFirst,
ntLast,
ntAbsolute,
ntRelative,
ntSQLState,
ntCreateDomain,
ntWithCheckOption,
ntCascaded,
ntLocal,
ntNumber,
ntString,
ntId,
ntLocalTemporary,
ntGlobalTemporary,
ntOnCommitDelete,
ntOnCommitPreserve,
ntColumnDef,
ntNull,
ntParam,
ntDefault,
ntNotNull,
ntPrimaryKey,
ntUnique, //note: used for 2 slightly different nodes (2nd one should really be ntUNIQUE)
ntReferences,
ntReferencesDef,
ntConstraintDef, //-
ntConstraint,
ntAlterTable,
ntAddColumn,
ntAlterColumn,
ntDropColumn,
ntAddConstraint,
ntDropConstraint,
ntDropUser, //-
ntDropSchema,
ntDropTable,
ntDropView,
ntDropRoutine,
ntDropDomain,
ntNumeric, //-
ntDecimal,
ntInteger,
ntSmallInt,
ntBigInt,
ntFloat,
ntCharacter,
ntVarChar,
ntBit,
ntVarBit,
ntDate,
ntTime,
ntTimestamp,
ntWithTimezone,
ntBlob,
ntClob,
ntCount, //-
ntAvg,
ntMax,
ntMin,
ntSum,
ntAggregate,
ntPlus,
ntMinus,
ntMultiply,
ntDivide,
ntPrimaryKeyDef,
ntUniqueDef,
ntForeignKeyDef,
ntMatchFull,
ntMatchPartial,
ntOnDelete,
ntOnUpdate,
ntNoAction,
ntCascade,
ntRestrict,
ntSetDefault,
ntSetNull,
ntCheckConstraint,
ntCondExpText, //todo never used: remove?
ntInitiallyDeferred,
ntInitiallyImmediate,
ntDeferrable,
ntNotDeferrable,
ntSetConstraints,
ntDeferred,
ntImmediate,
ntSchema,
ntCatalog,
ntDomain,
ntConcat, //-
ntSelect,
ntInto,
ntDistinct,
ntAll,
ntWhere,
ntSelectItem,
ntSelectAll,
ntColumnRef,
ntHaving,
ntGroupBy,
ntOrderBy,
ntOrderItem,
ntAsc,
ntDesc,
ntTableRef, //-
ntAND,
ntOR,
ntNOT,
ntIS,
ntTrue,
ntFalse,
ntUnknown,
ntISnull,
ntEqual,
ntEqualOrNull,
ntLT,
ntLTEQ,
ntGT,
ntGTEQ,
ntNotEqual,
ntLike,
ntAny,
ntInScalar,
ntExists,
ntIsUnique, //-
ntInsert,
ntInsertValues,
ntDefaultValues,
ntUpdate,
ntUpdateAssignment,
ntDelete,
ntTableConstructor, //-
ntTable,
ntRowConstructor,
ntCharacterExp, //todo remove?
ntBitExp, //todo remove?
ntNumericExp, //todo rename to genericExp
ntDatetimeExp, //todo remove?
ntTableExp, //-
ntJoin,
ntCrossJoin,
ntJoinOn,
ntJoinUsing,
ntJoinInner,
ntJoinLeft,
ntJoinRight,
ntJoinFull,
ntJoinUnion,
ntOuter,
ntNatural,
ntNonJoinTableExp,
ntCorrespondingBy,
ntCorresponding,
ntNonJoinTableTerm,
ntJoinTableExp,
ntUnionExcept,
ntUnion,
ntExcept,
ntIntersect,
ntTableTerm,
ntTablePrimary,
ntNonJoinTablePrimary, //-
ntCommit,
ntRollback,
ntSetTransaction,
ntOptionDiagnostic,
ntOptionReadOnly,
ntOptionReadWrite,
ntOptionIsolationReadUncommitted,
ntOptionIsolationReadCommitted,
ntOptionIsolationRepeatableRead,
ntOptionIsolationSerializable,
ntConnect,
ntAsConnection,
ntUser,
ntCurrentUser,
ntSessionUser,
ntSystemUser,
ntCurrentDate,
ntCurrentTime,
ntCurrentTimestamp,
ntDisconnect,
ntSetSchema,
ntGrant,
ntAllPrivileges,
ntPrivilegeSelect,
ntPrivilegeInsert,
ntPrivilegeUpdate,
ntPrivilegeDelete,
ntPrivilegeReferences,
ntPrivilegeUsage,
ntPrivilegeExecute,
ntCharacterSet,
ntCollation,
ntTranslation,
ntWithGrantOption,
ntRevoke,
ntCast,
ntCase,
ntCaseOf,
ntWhen,
ntWhenType2,
ntCoalesce,
ntNullIf, //-
ntMatch,
ntPARTIAL,
ntFULL, //-
ntEqualFilter, //internally generated node to allow separate chaining
ntTrim,
ntTrimWhat,
ntTrimLeading,
ntTrimTrailing,
ntTrimBoth,
ntCharLength,
ntOctetLength,
ntLower,
ntUpper,
ntPosition,
ntSubstring,
ntSubstringFrom,
ntCompoundBlock,
ntAtomic,
ntNotAtomic,
ntCompoundElement,
ntCompoundWhile,
ntCompoundIf,
ntCompoundCase,
ntIfThen,
ntCompoundLoop,
ntCompoundRepeat,
ntLeave,
ntIterate,
//Non-SQL/92 (implementation-defined) nodes: subtract 12
ntPassword,
ntCreateCatalog,
ntCreateUser,
ntAlterUser,
ntDEBUGTABLE, //DEBUG ONLY - REMOVE/HIDE!
ntDEBUGINDEX, //DEBUG ONLY - REMOVE/HIDE!
ntDEBUGCATALOG, //DEBUG ONLY - REMOVE/HIDE!
ntDEBUGSERVER, //DEBUG ONLY - REMOVE/HIDE!
ntDEBUGPAGE, //DEBUG ONLY - REMOVE/HIDE!
ntDEBUGPLAN, //DEBUG ONLY - REMOVE/HIDE!
ntDEBUGPRINT, //DEBUG ONLY - REMOVE/HIDE!
ntSUMMARY, //DEBUG ONLY - REMOVE/HIDE!
ntKILLTRAN,
ntCANCELTRAN,
ntREBUILDINDEX, //DEBUG ONLY - REMOVE/HIDE!
ntSHOWTRANS, //DEBUG ONLY - REMOVE!
ntCurrentAuthID,
ntCurrentCatalog,
ntCurrentSchema,
ntIndex,
ntCreateIndex,
ntDropIndex,
ntSequence,
ntCreateSequence,
ntDropSequence,
ntStartingAt,
ntNextSequence,
ntLatestSequence,
ntBackupCatalog,
ntOpenCatalog,
ntCloseCatalog,
ntGarbageCollectCatalog,
ntSHUTDOWN //SYSTEM DEBUG - REMOVE!?
); //todo etc?
{Used during optimisation SARG passing}
pushedType=(ptAvailable, //available for a child to pull
ptPulled, //marked as pulled by a child
ptMustPull //force pull to child, e.g. to override local rules for equi-join inner-filter
);
TSyntaxNodePtr=^TSyntaxNode;
{Note: use the functions below to create and link!}
TSyntaxNode=record
{Node type}
nType:nodeType;
systemNode:boolean; //(initially) used for ntColumnRef/Id for optimiser to FindCol natural join columns
//also used to flag optimiser-handled joins (i.e. common columns set by optimiser from implicit join)
{Reference count}
refCount:shortint; //reference count to allow a sub-tree to be referenced more than once (save space & time)
//Note: this is also inc/dec'ed in the CondToCNF routine(s) & processor routine for cursor declarations
//Note: if/when these syntax trees can be shared among threads we must make
// sure that the inc/dec refCount is made thread-safe! todo
{Link to iterator root}
ptree:TObject; {=TIterator;} //plan root - needed for main & sub-plans
//since we now build (& destroy) the sub-plans once, eval needs links
{Link to algebra root}
atree:pointer; {=TAlgebraNodePtr;} //algebra root - only needed so we can delete the algebra tree/sub-tree(s) once
//we're done with the plans.
//probably better to use ptree.anodeRef once we're sure that
//it is always set properly - then we can get rid of this link space!
{Link to tuple column}
cTuple:TObject; {=TTuple} //used for fast evaluation once FindCol has found the source
cRef:colRef; // "
{Link to variable def}
vVariableSet:TObject; {=TVariableSet} //used for fast evaluation once FindVar has found the source
vRef:varRef; // "
{Definition (based on tuple column or variable def & passed up tree)}
//todo: are these always associated with some iTuple.colDef? if so can't we use that to store this data?
dType:TDataType; //datatype, passed up through tree
dwidth:integer; //storage size (0=variable)
dscale:smallint; //precision
aggregate:boolean; //flag to indicate aggregation within IterGroup group-by loops
{Mutually exclusive values}
nullVal:boolean; //null (introduced for aggregate counting + used for null literal)
idVal:string; //id (also used to store param number (will be name in future))
numVal:double; //number (also used to store param length if param string contains #0s, i.e. blob length)
strVal:string; //string (also used to store param value - initially ?=value not set)
(*todo ?
blobVal:pointer; //blob (used to store param value - used with strVal ?=value not set)
blobLen:cardinal; //blob length
*)
pushed:pushedType; //pushed flag - set during optimisation to denote moving of a SARG (actually is 'pulled'?)
optimised:pushedType; //pushed flag - set during (trial) algebra optimisation to denote moving of a SARG (actually is 'pulled'?)
//in development: note: will replace pushed
{Child nodes}
leftChild:TSyntaxNodePtr;
rightChild:TSyntaxNodePtr;
{Peer nodes}
nextNode:TSyntaxNodePtr;
prevNode:TSyntaxNodePtr; //Note: used for hiding dynamically created syntax nodes at the root level to allow full cleanup //todo use another hook for this...
{Lexical references}
line,col:word; //pointer back to source SQL
//Note this will break if column gets too big, e.g. create schema on one line
// - make an integer & have lexlib.inc(colNo) wrap around to 0 instead, i.e. make IT a word as well!
allocNext:TSyntaxNodePtr; //allocation chain pointer (to guarantee full clean up after syntax error etc.)
//old deleteSyntaxTree tried used recursive traversal & refCount but
//would miss some nodes (& so sub-trees) (maybe after other routines had fiddled with the tree)
//and yacc wouldn't return partial trees after syntax error
// - seemed quicker to chain all allocations to a stmt root node
// and the deleteSyntaxTree is quick and simple & should never lose any nodes
//May make pre-allocation/pooling of nodes easier in future?
end; {TSyntaxNode} //approx size=1+2+4+4+4+2+1+2+2+1+1+1+4+4+4+1+4+4+4+4+2+2 +idVal+strVal = 58+ ~10 = ~68 bytes (so <16 per K)
// how much of this is unecessary overhead?:
// is prevNode ever used?
// combine idVal & strVal - too confusing!
// line, col any use here?
// ptree/atree rarely used - maybe use for other things (but be careful, e.g. when deleting trees!)
// cTuple/cRef rarely used - maybe use for other things (no! as TTuple often used to test)
//(but remember, record is not packed, so saving <4 bytes may not reduce the size)
//todo: maybe a better solution would be to have a basic syntaxNode class
// and inherited classes could add their own data
// - more elegant (safer), but what's the speed/memory overhead?
// - also, would be much harder to use our own memory allocation
// because the sizes would be different & less control (whereas a record can be plonked anywhere)
//So maybe use a variant record instead? with nType as the flag/switch...
function mkNode(allocRoot:TSyntaxNodePtr;ntype:nodeType;dtype:TdataType;lp,rp:TSyntaxNodePtr):TSyntaxNodePtr;
function cloneNode(allocRoot:TSyntaxNodePtr;np:TSyntaxNodePtr;cloneSiblingLinks,cloneChildLinks:boolean):TSyntaxNodePtr;
procedure chainNext(var tp:TSyntaxNodePtr;np:TSyntaxNodePtr);
procedure chainAppendNext(tp,np:TSyntaxNodePtr);
procedure chainTempLink(var tp:TSyntaxNodePtr;np:TSyntaxNodePtr);
procedure chainAppendTempLink(var tp:TSyntaxNodePtr;np:TSyntaxNodePtr);
//Note: iterator.optimise routines unlink by-hand using nextNode pointers!
procedure chainPrev(var tp:TSyntaxNodePtr;np:TSyntaxNodePtr);
procedure linkLeftChild(tp,cp:TSyntaxNodePtr);
procedure linkRightChild(tp,cp:TSyntaxNodePtr);
procedure unlinkRightChild(tp:TSyntaxNodePtr);
procedure unlinkLeftChild(tp:TSyntaxNodePtr);
function mkLeaf(allocRoot:TSyntaxNodePtr;ntype:nodeType;dtype:Tdatatype;dwidth:integer;dscale:smallint):TSyntaxNodePtr;
function infixSyntax(node:TSyntaxNodePtr):string;
function DisplaySyntaxTree(stree:TSyntaxNodePtr):integer;
function DeleteSyntaxTree(stree:TSyntaxNodePtr):integer;
function hasAggregate(stree:TSyntaxNodePtr):boolean;
var
debugSyntaxCreate:integer=0; //todo remove -or at least make thread-safe & private
debugSyntaxDestroy:integer=0; //"
implementation
uses
{$IFDEF Debug_Log}
uLog,
{$ENDIF}
sysUtils, uAlgebra, uIterator {for deleting trees}, uIterMaterialise;
const
where='uSyntax';
who='';
function mkNode(allocRoot:TSyntaxNodePtr;ntype:nodeType;dtype:Tdatatype;lp,rp:TSyntaxNodePtr):TSyntaxNodePtr;
{Make a syntax tree node
Note: dtype can be used to override the default type which is:
x if both children are x (or only one child exists)
else ctUnknown?... todo or default to left child type?...
dwidth, dscale are taken from the child/children
}
const routine=':mkNode';
var
n:TSyntaxNodePtr;
combinedDtype:TDatatype;
lDWidth,rDWidth:integer;
lDScale,rDScale:smallint;
begin
//todo replace new() with a node handler
new(n);
inc(debugSyntaxCreate); //todo remove
{$IFDEF DEBUG_LOG}
if debugSyntaxCreate=1 then
log.add(who,where,format(' Syntax node memory size=%d',[sizeof(TSyntaxNode)]),vDebugLow);
{$ENDIF}
n.allocNext:=allocRoot.allocNext;
allocRoot.allocNext:=n;
{$IFDEF DEBUGDETAIL2}
{$IFDEF DEBUG_LOG}
log.add(who,where+routine,format(' chaining syntax node %p to %p',[n,allocRoot]),vdebugLow);
{$ENDIF}
{$ENDIF}
n.ntype:=ntype;
n.systemNode:=False;
n.refCount:=0; //we increment via link/chain..Child() and decrement during DeleteSyntaxTree
n.ptree:=nil;
n.atree:=nil;
n.cTuple:=nil;
n.cRef:=0; //todo better make maxint? because 0 is actually valid!
n.vVariableSet:=nil;
n.vRef:=0; //todo better make maxint? because 0 is actually valid!
n.dwidth:=0; //default //remove=speed
n.dscale:=0; //default //remove=speed
n.aggregate:=false;
n.nullVal:=true;
n.idVal:='';
n.numVal:=0;
n.strVal:='';
n.pushed:=ptAvailable;
n.optimised:=ptAvailable;
n.nextNode:=nil; n.prevNode:=nil;
//todo use link routines...!
n.leftChild:=lp; if lp<>nil then inc(lp.refCount);
n.rightChild:=rp; if rp<>nil then inc(rp.refCount);
{Pass up the datatype etc. from the children}
//speed- could directly change n values...
combinedDtype:=dtype; //default/override
if combinedDtype=ctUnknown then
begin
if n.rightChild<>nil then
combinedDtype:=n.rightChild.dtype
else
if n.leftChild<>nil then //note: new addition to handle ntunknown with typed left-child
combinedDtype:=n.leftChild.dtype;
if n.leftChild<>nil then
begin
if (n.leftChild.dType=combinedDtype) or (n.leftChild.dType=ctUnknown) then
//ok, all children are same - use this dtype
else
if combinedDtype<>ctUnknown then
{$IFDEF DEBUG_LOG}
log.add(who,where+routine,format('Invalid types: node default [%d], left [%d]',[ord(dtype),ord(n.leftChild.dtype)]),vDebugError);
{$ELSE}
;
{$ENDIF}
end;
end;
n.dtype:=combinedDtype;
{etc}
lDWidth:=0; rDWidth:=0;
lDScale:=0; rDScale:=0;
if n.leftChild<>nil then
begin
lDWidth:=n.leftChild.dWidth;
lDScale:=n.leftChild.dScale;
end;
if n.rightChild<>nil then
begin
rDWidth:=n.rightChild.dWidth;
rDScale:=n.rightChild.dScale;
end;
{Note the following rules are not always correct (80/20?)
e.g. 'ABC'||'D' should have dWidth=4, not 3
but we leave this to the next phases to correct
//todo could update it in parser e.g. in || rule?
// - although not if column ref etc. were involved - need to defer...
}
n.dWidth:=maxINTEGER(lDWidth,rDWidth);
n.dScale:=maxSMALLINT(lDScale,rDScale);
n.line:=0;
n.col:=0;
{$IFDEF DEBUGDETAIL}
{$IFDEF DEBUG_LOG}
log.add(who,where+routine,format('(%p)%d [%d %d %d] %p %p',[n,ord(n.ntype),ord(n.dtype),n.dwidth,n.dscale,n.leftChild,n.rightChild]),vDebug);
{$ENDIF}
{$ENDIF}
result:=n;
end; {mkNode}
function cloneNode(allocRoot:TSyntaxNodePtr;np:TSyntaxNodePtr;cloneSiblingLinks,cloneChildLinks:boolean):TSyntaxNodePtr;
{Make a copy of a syntax tree node
IN: np the node to clone
cloneSiblingLinks True=increment reference counts on siblings, else share siblings
cloneChildLinks True=increment reference counts on children, else share children
Note:
this new node's ref count is not copied, but set to 0
}
const routine=':cloneNode';
var
n:TSyntaxNodePtr;
begin
//todo replace new() with a node handler
new(n);
inc(debugSyntaxCreate); //todo remove
n.allocNext:=allocRoot.allocNext;
allocRoot.allocNext:=n;
{$IFDEF DEBUGDETAIL2}
{$IFDEF DEBUG_LOG}
log.add(who,where+routine,format(' chaining syntax node %p to %p',[n,allocRoot]),vdebugLow);
{$ENDIF}
{$ENDIF}
n.ntype:=np.ntype;
n.systemNode:=False;
n.refCount:=0; //we increment via link/chain..Child() and decrement during DeleteSyntaxTree
n.ptree:=np.ptree;
n.atree:=np.atree;
n.cTuple:=np.cTuple;
n.cRef:=np.cRef;
n.vVariableSet:=np.vVariableSet;
n.vRef:=np.vRef;
n.dwidth:=np.dwidth;
n.dscale:=np.dscale;
n.aggregate:=np.aggregate;
n.nullVal:=np.nullVal;
n.idVal:=np.idVal;
n.numVal:=np.numVal;
n.strVal:=np.strVal;
n.pushed:=np.pushed;
n.optimised:=np.optimised;
n.nextNode:=np.nextNode;
n.prevNode:=np.prevNode;
//todo use link routines...!
if cloneSiblingLinks then
begin
if n.nextNode<>nil then inc(n.nextNode.refCount);
if n.prevNode<>nil then inc(n.prevNode.refCount);
end;
n.leftChild:=np.leftChild;
n.rightChild:=np.rightChild;
//todo use link routines...!
if cloneChildLinks then
begin
if n.leftChild<>nil then inc(n.leftChild.refCount);
if n.rightChild<>nil then inc(n.rightChild.refCount);
end;
n.dtype:=np.dtype;
{etc}
n.dWidth:=np.dWidth;
n.dScale:=np.dScale;
n.line:=np.line;
n.col:=np.col;
{$IFDEF DEBUGDETAIL}
{$IFDEF DEBUG_LOG}
log.add(who,where+routine,format('(%p <- %p)%d [%d %d %d] %p %p',[n,np,ord(n.ntype),ord(n.dtype),n.dwidth,n.dscale,n.leftChild,n.rightChild]),vDebug);
{$ENDIF}
{$ENDIF}
result:=n;
end; {cloneNode}
procedure chainNext(var tp:TSyntaxNodePtr;np:TSyntaxNodePtr);
{Insert node (or chain) between this node and its next one
effectively stacking the new node & assuming a static head-node
28/03/00 if tp=nil, then set to np to start a new chain (for local SARG lists)
Note: still works a bit weird: e.g. 1,2,3,4 -> 1,4,3,2 ?
-not used from Yacc (yet?)
}
const routine=':chainNext';
var
tempn, lastInInsertedChain:TSyntaxNodePtr;
looping:integer; //todo remove loop detector in final code!
begin
if tp<>np then
begin
if tp<>nil then
begin
if np<>nil then
begin
tempn:=tp.nextNode;
tp.nextNode:=np; //insert in chain
inc(np.refCount);
//todo link np.prevNode?
lastInInsertedChain:=np; //find end of this sub-chain being inserted
looping:=0; //todo remove loop detector ****or at least increase in case we have many items!!!
while (lastInInsertedChain.nextNode<>nil) and (looping<200) do
begin
lastInInsertedChain:=lastInInsertedChain.nextNode;
inc(looping);
end;
if looping=200 then
begin
{$IFDEF DEBUG_LOG}
log.add(who,where+routine,format('%p %p: Chain probably has a loop! It will be corrupt now...',[tp,np]),vDebugError);
{$ELSE}
;
{$ENDIF}
end;
lastInInsertedChain.NextNode:=tempn; //re-link end to rest of existing chain
//todo link tempn.prevNode?
{$IFDEF DEBUGDETAIL}
{$IFDEF DEBUG_LOG}
log.add(who,where+routine,format('%p %p',[tp,np]),vDebug);
{$ENDIF}
{$ENDIF}
end
else
{$IFDEF DEBUG_LOG}
log.add(who,where+routine,'np=nil',vDebug);
{$ELSE}
;
{$ENDIF}
end
else
begin //tp=nil -> new list
if np<>nil then
begin
tp:=np;
//todo remove! inc(np.refCount);
// we assume that if we're starting a list this way that
// the np node will become the tp node & so is not actually being referenced
// from another node (but is pointing tp to itself, so caller must ignore np = fine if temp/loop)
{$IFDEF DEBUGDETAIL}
{$IFDEF DEBUG_LOG}
log.add(who,where+routine,format('%s %p',['nil',np]),vDebug);
{$ENDIF}
{$ENDIF}
end
else
{$IFDEF DEBUG_LOG}
log.add(who,where+routine,'np=nil (and tp=nil!)',vDebug);
{$ELSE}
;
{$ENDIF}
end;
end
else
{$IFDEF DEBUG_LOG}
log.add(who,where+routine,'tp=np',vAssertion);
{$ELSE}
;
{$ENDIF}
end; {chainNext}
procedure chainAppendNext(tp,np:TSyntaxNodePtr);
{Append node (or chain) at end of this current node's chain}
const routine=':chainAppendNext';
var
lastInChain:TSyntaxNodePtr;
looping:integer; //todo remove loop detector in final code!
begin
if tp<>np then
begin
if tp<>nil then
begin
if np<>nil then
begin
lastInChain:=tp; //find end of the current chain //todo speed up repeated calls - maybe tp.prevNode should point to its end=loop?
looping:=0; //todo remove loop detector ****or at least increase in case we have many items!!!
while (lastInChain.nextNode<>nil) and (looping<200) do
begin
lastInChain:=lastInChain.nextNode;
inc(looping);
end;
if looping=200 then
begin
{$IFDEF DEBUG_LOG}
log.add(who,where+routine,format('%p %p: Chain probably has a loop! It will be corrupt now...',[tp,np]),vDebugError);
{$ENDIF}
end;
lastInChain.NextNode:=np; //link end to new node/chain
inc(np.refCount);
//todo link np.prevNode?
{$IFDEF DEBUGDETAIL}
{$IFDEF DEBUG_LOG}
log.add(who,where+routine,format('%p %p',[tp,np]),vDebug);
{$ELSE}
;
{$ENDIF}
{$ENDIF}
end
else
{$IFDEF DEBUG_LOG}
log.add(who,where+routine,'np=nil',vDebug);
{$ELSE}
;
{$ENDIF}
end
else
{$IFDEF DEBUG_LOG}
log.add(who,where+routine,'tp=nil',vDebug);
{$ELSE}
;
{$ENDIF}
end
else
{$IFDEF DEBUG_LOG}
log.add(who,where+routine,'tp=np',vAssertion);
{$ELSE}
;
{$ENDIF}
end; {chainAppendNext}
procedure chainTempLink(var tp:TSyntaxNodePtr;np:TSyntaxNodePtr);
{Chain node (or chain) to build temporary chain
If tp is nil, returns head of np as tp to start new chain
Note: no reference counts are incremented - it is assumed this list
is a temporary list of copied pointers to nodes that already exist and
are reference-counted elsewhere.
Also, the caller is free to unlink/dispose of copy nodes by-hand
(introduced for initial iterator.optimise SARG pushing routines)
When children use parts of this chain, they must create a new duplicate
root node for each so that they can use separate prev/nextNode
links and leave the originals alone so that the parent
iterator can still find and remove them from the cumulative SARGlist.
//TODO: **** increase loop detection to allow long SARG lists!
}
const routine=':chainTempLink';
var
tempn, lastInInsertedChain:TSyntaxNodePtr;
looping:integer; //todo remove loop detector in final code!
begin
if tp=nil then
begin
tp:=np;
end
else
begin
if tp<>np then
begin
if np<>nil then
begin
tempn:=tp.nextNode;
tp.nextNode:=np; //insert in chain
//todo link np.prevNode?
lastInInsertedChain:=np; //find end of this sub-chain being inserted
looping:=0; //todo remove loop detector ****or at least increase in case we have many items!!!
while (lastInInsertedChain.nextNode<>nil) and (looping<2000) do
begin
lastInInsertedChain:=lastInInsertedChain.nextNode;
inc(looping);
end;
if looping=2000 then
begin
{$IFDEF DEBUG_LOG}
log.add(who,where+routine,format('%p %p: Chain probably has a loop! It will be corrupt now...',[tp,np]),vDebugError);
{$ELSE}
;
{$ENDIF}
end;
lastInInsertedChain.NextNode:=tempn; //re-link end to rest of existing chain
//todo link tempn.prevNode?
{$IFDEF DEBUGDETAIL}
{$IFDEF DEBUG_LOG}
log.add(who,where+routine,format('%p %p',[tp,np]),vDebug);
{$ENDIF}
{$ENDIF}
end;
//else np=nil - ok, nothing to chain
end
else
{$IFDEF DEBUG_LOG}
log.add(who,where+routine,'tp=np',vAssertion);
{$ELSE}
;
{$ENDIF}
end;
end; {chainTempLink}
procedure chainAppendTempLink(var tp:TSyntaxNodePtr;np:TSyntaxNodePtr);
{Append node (or chain) to build temporary chain
If tp is nil, returns head of np as tp to start new chain
Note: no reference counts are incremented - it is assumed this list
is a temporary list of copied pointers to nodes that already exist and
are reference-counted elsewhere.
Also, the caller is free to unlink/dispose of copy nodes by-hand
(introduced for initial iterator.optimise SARG pushing routines)
(Append introduced to ensure unlink traversal match doesn't include new chain itself!
because chainTempLink attaches new chain to old chain!)
When children use parts of this chain, they must create a new duplicate
root node for each so that they can use separate prev/nextNode
links and leave the originals alone so that the parent
iterator can still find and remove them from the cumulative SARGlist.
//TODO: **** increase loop detection to allow long SARG lists!
}
const routine=':chainAppendTempLink';
var
lastInChain:TSyntaxNodePtr;
looping:integer; //todo remove loop detector in final code!
begin
if tp=nil then
begin
tp:=np;
end
else
begin
if tp<>np then
begin
if np<>nil then
begin
lastInChain:=tp; //find end of the current chain //todo speed up repeated calls - maybe tp.prevNode should point to its end=loop?
looping:=0; //todo remove loop detector ****or at least increase in case we have many items!!!
while (lastInChain.nextNode<>nil) and (looping<2000) do
begin
lastInChain:=lastInChain.nextNode;
inc(looping);
end;
if looping=2000 then
begin
{$IFDEF DEBUG_LOG}
log.add(who,where+routine,format('%p %p: Chain probably has a loop! It will be corrupt now...',[tp,np]),vDebugError);
{$ENDIF}
end;
lastInChain.NextNode:=np; //link end to new node/chain
//todo link prevNode?
{$IFDEF DEBUGDETAIL}
{$IFDEF DEBUG_LOG}
log.add(who,where+routine,format('%p %p',[tp,np]),vDebug);
{$ENDIF}
{$ENDIF}
end;
//else np=nil - ok, nothing to chain
end
else
{$IFDEF DEBUG_LOG}
log.add(who,where+routine,'tp=np',vAssertion);
{$ELSE}
;
{$ENDIF}
end;
end; {chainAppendTempLink}
procedure chainPrev(var tp:TSyntaxNodePtr;np:TSyntaxNodePtr);
{Insert node (or chain) between this node and its previous one
effectively stacking the new node & assuming a static head-node
28/03/00 if tp=nil, then set to np to start a new chain (for local SARG lists)
Note: still works a bit weird: e.g. 1,2,3,4 -> 1,4,3,2 ?
-not used from Yacc (yet?)
Note: added to be able to attach nodes to root for clean-up but for
processing from elsewhere, not the root
}
const routine=':chainPrev';
var
tempn, lastInInsertedChain:TSyntaxNodePtr;
looping:integer; //todo remove loop detector in final code!
begin
if tp<>np then
begin
if tp<>nil then
begin
if np<>nil then
begin
tempn:=tp.prevNode;
tp.prevNode:=np; //insert in chain
inc(np.refCount);
//todo link np.nextNode?!
lastInInsertedChain:=np; //find end of this sub-chain being inserted
looping:=0; //todo remove loop detector ****or at least increase in case we have many items!!!
while (lastInInsertedChain.prevNode<>nil) and (looping<200) do
begin
lastInInsertedChain:=lastInInsertedChain.prevNode;
inc(looping);
end;
if looping=200 then
begin
{$IFDEF DEBUG_LOG}
log.add(who,where+routine,format('%p %p: Chain probably has a loop! It will be corrupt now...',[tp,np]),vDebugError);
{$ENDIF}
end;
lastInInsertedChain.prevNode:=tempn; //re-link end to rest of existing chain
//todo link tempn.prevNode?
{$IFDEF DEBUGDETAIL}
{$IFDEF DEBUG_LOG}
log.add(who,where+routine,format('%p %p',[tp,np]),vDebug);
{$ENDIF}
{$ENDIF}
end
else
{$IFDEF DEBUG_LOG}
log.add(who,where+routine,'np=nil',vDebug);
{$ELSE}
;
{$ENDIF}
end
else
begin //tp=nil -> new list
if np<>nil then
begin
tp:=np;
//todo remove! inc(np.refCount);
// we assume that if we're starting a list this way that
// the np node will become the tp node & so is not actually being referenced
// from another node (but is pointing tp to itself, so caller must ignore np = fine if temp/loop)
{$IFDEF DEBUGDETAIL}
{$IFDEF DEBUG_LOG}
log.add(who,where+routine,format('%s %p',['nil',np]),vDebug);
{$ENDIF}