/
ast-node-typeini.bas
1294 lines (1043 loc) · 33.4 KB
/
ast-node-typeini.bas
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
'' AST type initializer nodes
'' tree : l = head; r = (when constructing: tail, when updating: base var)
'' expr node: l = expr; r = next
'' pad node : l = NULL; r = next
''
'' chng: mar/2006 written [v1ctor]
#include once "fb.bi"
#include once "fbint.bi"
#include once "parser.bi"
#include once "ir.bi"
#include once "ast.bi"
'':::::
function astTypeIniBegin _
( _
byval dtype as integer, _
byval subtype as FBSYMBOL ptr, _
byval is_local as integer, _
byval ofs as longint _
) as ASTNODE ptr
dim as ASTNODE ptr n = any
'' alloc new node
n = astNewNode( AST_NODECLASS_TYPEINI, _
dtype, _
subtype )
function = n
'' n is a newly allocated INITREE
'' ofs will be zero unless the symbol passed in to cInitializer()
'' had an offset due to a field initializers
'' however, bytes initialized by this tree should start at zero. Only
'' when(if) this tree is merged to another tree do the bytes contribute
'' to the parent.
n->typeini.ofs = ofs
n->typeini.bytes = 0
dim as integer add_scope = FALSE
if( is_local = FALSE ) then
if( symbIsScope( parser.currblock ) ) then
'' Don't add a new temp scope if already inside one
'' (from a parent TYPEINI)
add_scope = not astIsTYPEINI( parser.currblock->scp.backnode )
else
add_scope = TRUE
end if
end if
if( add_scope ) then
'' create a new scope block to handle temp vars allocated inside the
'' tree - with shared vars, the temps must be moved to another function
n->typeini.scp = astTempScopeBegin( n->typeini.lastscp, n )
else
n->typeini.scp = NULL
end if
end function
private sub hAstTypeIniMaybeConvertUpcast _
( _
byval n as ASTNODE ptr, _
byval l as ASTNODE ptr _
)
dim as FBSYMBOL ptr sym = any
dim as longint maxsize = any
sym = n->sym
'' we want the size of the elements (type) not the whole array
'' !!!TODO!!! byrefs? with symbGetRealSize( sym )?
maxsize = symbGetLen( sym )
'' This should only be true if astTypeIniAddAssign()
'' determined that the initree was constant
'' !!!TODO!!! there is a few places where we use the same
'' pattern of if statements to check if one ast node is a type
'' derived from another. Maybe make a common function?
if( typeGet( astGetDataType( n ) ) = FB_DATATYPE_STRUCT ) then
if( typeGet( astGetDataType( l ) ) = FB_DATATYPE_STRUCT ) then
if( astGetSubtype( n ) <> astGetSubtype( l ) ) then
if( symbGetUDTBaseLevel( astGetSubtype( l ), astGetSubtype( n ) ) > 0 ) then
'' patch the subtype (up-cast)
l->subtype = n->subtype
l->typeini.bytes = maxsize
'' patch the next node too
l->l->typeini.ofs = n->typeini.ofs
l->l->typeini.bytes = maxsize
l->l->subtype = n->subtype
'' !!!TODO!!! confirm ast class? - it only makes sense if it
'' is another TYPEINI node that tracks offset/bytes/type
'' do we need this, or can it be assumed that everywhere handled
'' this correctly?
#ifdef __FB_DEBUG__
select case astGetClass( l->l )
case AST_NODECLASS_TYPEINI
case AST_NODECLASS_TYPEINI_ASSIGN
case AST_NODECLASS_TYPEINI_SCOPEINI
case else
assert( FALSE )
end select
#endif
end if
end if
end if
end if
end sub
'':::::
sub astTypeIniEnd _
( _
byval tree as ASTNODE ptr, _
byval is_initializer as integer _
)
dim as ASTNODE ptr n = any, p = any, l = any, r = any
dim as longint ofs = any, bytes = any
assert( astIsTYPEINI( tree ) )
'' can't leave r pointing to the any node as the
'' tail node is linked already
tree->r = NULL
if( is_initializer = FALSE ) then
'' We're going to use the TYPEINI as expression that will be astAdd'ed,
'' to be updated by e.g. astTypeIniUpdate() later
ast.typeinicount += 1
end if
'' Otherwise, if it's an initializer, it will either be passed to astTypeIniFlush() immediately,
'' or kept in an FBSYMBOL.var_.initree. In this case there is no TYPEINI that would need to be
'' registered for astTypeIniUpdate() later.
'' merge nested type ini trees
p = NULL
n = tree->l
do while( n <> NULL )
'' expression node?
if( n->class = AST_NODECLASS_TYPEINI_ASSIGN ) then
'' n = the typeini tree we want to update (what we are assigning)
'' n->l = what we want to assign
'' n->r = next item in the list
l = n->l
'' is it an ini tree too?
if( astIsTYPEINI( l ) ) then
ast.typeinicount -= 1
'' current offset and size from the tree we are updating
ofs = n->typeini.ofs
bytes = n->typeini.bytes
'' up-cast?
hAstTypeIniMaybeConvertUpcast( n, l )
r = n->r
astDelNode( n )
n = l->l
astDelNode( l )
'' relink
if( p <> NULL ) then
p->r = n
else
tree->l = n
end if
'' update the offset, using the parent's
do while( n->r <> NULL )
n->typeini.ofs += ofs
n->typeini.bytes += bytes
n = n->r
loop
n->typeini.ofs += ofs
n->typeini.bytes += bytes
n->r = r
end if
end if
'' next
p = n
n = n->r
loop
'' close the scope block
if( tree->typeini.scp <> NULL ) then
astTempScopeEnd( tree->typeini.scp, tree->typeini.lastscp )
end if
end sub
private function hAddNode _
( _
byval tree as ASTNODE ptr, _
byval class_ as integer, _
byval sym as FBSYMBOL ptr = NULL, _
byval dtype as integer = FB_DATATYPE_INVALID, _
byval subtype as FBSYMBOL ptr = NULL _
) as ASTNODE ptr
dim as ASTNODE ptr n = any
assert( astIsTYPEINI( tree ) )
if( (dtype = FB_DATATYPE_INVALID) and (sym <> NULL) ) then
dtype = symbGetFullType( sym )
subtype = symbGetSubtype( sym )
end if
'' tree => tree
'' / \
'' n n
''
'' tree => tree => tree
'' / \ / \ / \
'' l r l r l n
'' \ \ \
'' n n n
''
'' tree = AST_NODE_TYPEINI AST_NODECLASS_TYPEINI
'' tree->l... = list of items in TREE
'' tree->r = last node of list
n = astNewNode( class_, dtype, subtype )
if( tree->r <> NULL ) then
tree->r->r = n
else
tree->l = n
end if
tree->r = n
function = n
end function
sub astTypeIniRemoveLastNode( byval tree as ASTNODE ptr )
dim as ASTNODE ptr prev = any, n = any
assert( astIsTYPEINI( tree ) )
'' Find the last node, and the previous one
prev = NULL
n = tree->l
while( n )
'' Last node reached?
if( n->r = NULL ) then
assert( tree->r = n )
assert( n <> prev )
'' Unlink from the TYPEINI tree
if( prev ) then
assert( prev->r = n )
prev->r = NULL
else
tree->l = NULL
end if
tree->r = prev
astDelTree( n )
exit while
end if
prev = n
n = n->r
wend
end sub
function astTypeIniAddPad _
( _
byval tree as ASTNODE ptr, _
byval bytes as longint _
) as ASTNODE ptr
dim as ASTNODE ptr n = any
assert( astIsTYPEINI( tree ) )
n = hAddNode( tree, AST_NODECLASS_TYPEINI_PAD )
n->typeini.bytes = bytes
n->typeini.ofs = tree->typeini.ofs
'' accumulate total bytes in this tree
tree->typeini.bytes += n->typeini.bytes
function = n
end function
private function hAstIniTreeIsConstant _
( _
byval tree as ASTNODE ptr _
) as integer
if( tree = NULL ) then
return TRUE
end if
'' determine if the tree only contains constants and
'' references and nothing that would need a temporary
'' variable when upcasting.
select case astGetClass( tree )
case AST_NODECLASS_TYPEINI
case AST_NODECLASS_TYPEINI_PAD
case AST_NODECLASS_TYPEINI_ASSIGN
case AST_NODECLASS_TYPEINI_CTORCALL
return FALSE
case AST_NODECLASS_TYPEINI_CTORLIST
return FALSE
case AST_NODECLASS_TYPEINI_SCOPEINI
case AST_NODECLASS_TYPEINI_SCOPEEND
case AST_NODECLASS_CALL
return FALSE
case AST_NODECLASS_CALLCTOR
return FALSE
case AST_NODECLASS_VAR
return TRUE
case AST_NODECLASS_CONST
return TRUE
'' assume anything else is not constant
case else
return FALSE
end select
if( tree->l ) then
if( hAstIniTreeIsConstant( tree->l ) = FALSE ) then
return FALSE
end if
end if
if( tree->r ) then
if( hAstIniTreeIsConstant( tree->r ) = FALSE ) then
return FALSE
end if
end if
return TRUE
end function
private sub hAstTypeIniTreeMergeUpcast _
( _
byval tree as ASTNODE ptr, _
byval n as ASTNODE ptr, _
byref expr as ASTNODE ptr, _
byval sym as FBSYMBOL ptr _
)
assert( astGetClass( n ) = AST_NODECLASS_TYPEINI_ASSIGN )
assert( astGetClass( expr ) = AST_NODECLASS_TYPEINI )
assert( n->l = expr )
assert( typeGet( astGetDataType( n ) ) = FB_DATATYPE_STRUCT )
''
'' n (TYPEINI_ASSIGN) base
'' /
'' expr (TYPEINI) derived
''
'' if we got to here, then hDoAssign() would have identified this
'' as an up-cast and we should take care when merging the trees
'' our goal is that any assignment should not write outside the
'' size of the target. hAstCheckTypeIniAssignment() called
'' by astTypeIniFlush() will also check for initializers that
'' write outside of the target and generate a warning with
'' -w upcast. This allows us to preserve the original typeini
'' here for now. What we don't want to do is lose information
'' about the size of the original, lose any intitialers
'' that are calls, or lose the size of the target.
dim as integer maxsize = any
'' maxsize = symbGetRealSize( sym )
maxsize = symbGetLen( sym )
'' too big for target?
if( tree->typeini.ofs + expr->typeini.bytes > maxsize ) then
'' If the struct we are initializing has no ctor/dtor and,
'' if expr is all constant with no function calls, then we
'' don't need the conversion and astTypeIniFlush() will warn
'' us on discarded initializers.
'' otherwise, assignment of expr to the tree would cause us
'' to write outside of the target, so we need to add a
'' conversion to force the use of a temprary variable
var isconst = (hAstIniTreeIsConstant( expr ) = TRUE )
var hasdtor = (symbGetCompDtor1( astGetSubtype( tree ) ) <> NULL)
if( (isconst = FALSE) or (hasdtor = TRUE) ) then
dim as ASTNODE ptr l = any
'' hDoAssign() and astCheckASSIGNToType() should have ensured that
'' this conversion should always work...
l = astNewCONV( symbGetType( sym ), symbGetSubtype( sym ), expr )
assert( l <> NULL )
if( l <> NULL ) then
n->l = l
end if
end if
'' either way, don't exceed the maximum size
tree->typeini.ofs += maxsize
tree->typeini.bytes += maxsize
'' otherwise, we are still writing within the
'' limits of the target
else
tree->typeini.ofs += expr->typeini.bytes
tree->typeini.bytes += expr->typeini.bytes
end if
end sub
function astTypeIniAddAssign _
( _
byval tree as ASTNODE ptr, _
byval expr as ASTNODE ptr, _
byval sym as FBSYMBOL ptr, _
byval dtype as integer, _
byval subtype as FBSYMBOL ptr, _
byval check_upcast as integer = FALSE _
) as ASTNODE ptr
dim as ASTNODE ptr n = any
assert( astIsTYPEINI( tree ) )
n = hAddNode( tree, AST_NODECLASS_TYPEINI_ASSIGN, sym, dtype, subtype )
n->l = expr
n->sym = sym
n->typeini.ofs = tree->typeini.ofs
n->typeini.bytes = 0
'' sym only tells us the first field
'' but the tree might actually be larger
if( astGetClass( expr ) = AST_NODECLASS_TYPEINI ) then
'' Check for up-casting but only if the caller requested it.
'' Maybe we can always check for up-casting but until every caller
'' is analyzed, it seems safer to only check if requested.
dim as integer upcast = FALSE
if( check_upcast ) then
'' We need to preserve the size of the target even after the
'' expression is merged with the initializer tree. The merge is
'' a kind of of assignment that won't be handled properly
'' anywhere else if type or size information is lost or replaced
'' by a larger type. We need to avoid writing beyond the limits
'' of the target.
if( typeGet( dtype ) = FB_DATATYPE_STRUCT ) then
if( typeGet( astGetDataType( expr ) ) = FB_DATATYPE_STRUCT ) then
if( subtype <> astGetSubtype( expr ) ) then
if( symbGetUDTBaseLevel( astGetSubtype( expr ), subtype ) > 0 ) then
upcast = TRUE
end if
end if
end if
end if
end if
if( upcast ) then
hAstTypeIniTreeMergeUpcast( tree, n, expr, sym )
else
tree->typeini.ofs += expr->typeini.bytes
tree->typeini.bytes += expr->typeini.bytes
end if
else
'' vars and fields could be byref / array / etc
'' and boundstypeini may pass a NULL sym
if( sym ) then
if( symbIsRef( sym ) ) then
n->typeini.bytes = env.pointersize
else
n->typeini.bytes = symbGetLen( sym )
end if
end if
tree->typeini.ofs += n->typeini.bytes
tree->typeini.bytes += n->typeini.bytes
end if
function = n
end function
function astTypeIniAddCtorCall _
( _
byval tree as ASTNODE ptr, _
byval sym as FBSYMBOL ptr, _
byval procexpr as ASTNODE ptr, _
byval dtype as integer, _
byval subtype as FBSYMBOL ptr _
) as ASTNODE ptr
dim as ASTNODE ptr n = any
assert( astIsTYPEINI( tree ) )
n = hAddNode( tree, AST_NODECLASS_TYPEINI_CTORCALL, sym, dtype, subtype )
n->sym = sym
n->typeini.ofs = tree->typeini.ofs
n->typeini.bytes = symbGetLen( sym )
n->l = procexpr
tree->typeini.ofs += n->typeini.bytes
tree->typeini.bytes += n->typeini.bytes
function = n
end function
function astTypeIniAddCtorList _
( _
byval tree as ASTNODE ptr, _
byval sym as FBSYMBOL ptr, _
byval elements as longint, _
byval dtype as integer, _
byval subtype as FBSYMBOL ptr _
) as ASTNODE ptr
dim as ASTNODE ptr n = any
assert( astIsTYPEINI( tree ) )
n = hAddNode( tree, AST_NODECLASS_TYPEINI_CTORLIST, sym, dtype, subtype )
n->sym = sym
n->typeini.ofs = tree->typeini.ofs
'' AST_NODECLASS_TYPEINI_CTORLIST node uses typeini.elements
'' instead of typeini.elements to track position in the typeini tree
n->typeini.elements = elements
tree->typeini.ofs += symbGetLen( sym ) * elements
tree->typeini.bytes += symbGetLen( sym ) * elements
function = n
end function
'' is_array tells the LLVM backend whether the initializer is for an array or a struct.
function astTypeIniScopeBegin _
( _
byval tree as ASTNODE ptr, _
byval sym as FBSYMBOL ptr, _
byval is_array as integer _
) as ASTNODE ptr
dim as ASTNODE ptr n = any
assert( astIsTYPEINI( tree ) )
n = hAddNode( tree, AST_NODECLASS_TYPEINI_SCOPEINI )
n->sym = sym
n->typeiniscope.is_array = is_array
'' ofs and bytes (elements) should never be used for typeini scopes
'' zero them out anyway so debugging display is a little nicer
n->typeini.ofs = 0
n->typeini.bytes = 0
function = n
end function
function astTypeIniScopeEnd _
( _
byval tree as ASTNODE ptr, _
byval sym as FBSYMBOL ptr _
) as ASTNODE ptr
dim as ASTNODE ptr n = any
assert( astIsTYPEINI( tree ) )
n = hAddNode( tree, AST_NODECLASS_TYPEINI_SCOPEEND )
n->sym = sym
'' ofs and bytes (elements) should never be used for typeini scopes
'' zero them out anyway so debugging display is a little nicer
n->typeini.ofs = 0
n->typeini.bytes = 0
function = n
end function
'' Takes an array elements initializer and adds the same TYPEINI_ASSIGN's to
'' the new typeini tree (duplicating the expressions),
'' but not the TYPEINI_SCOPEINI/TYPEINI_SCOPEEND,
'' and assuming there are only TYPEINI_ASSIGN's and no ctorcalls or padding etc.
'' "beginindex" is the index of the first TYPEINI_ASSIGN that should be copied,
'' this allows to skip some array elements at the front.
sub astTypeIniCopyElements _
( _
byval tree as ASTNODE ptr, _
byval source as ASTNODE ptr, _
byval beginindex as integer _
)
dim as integer i = any
assert( astIsTYPEINI( tree ) )
assert( astIsTYPEINI( source ) )
source = source->l
assert( source->class = AST_NODECLASS_TYPEINI_SCOPEINI )
source = source->r
i = 0
while( source->class = AST_NODECLASS_TYPEINI_ASSIGN )
if( i >= beginindex ) then
astTypeIniAddAssign( tree, astCloneTree( source->l ), source->sym )
end if
source = source->r
i += 1
wend
assert( source->class = AST_NODECLASS_TYPEINI_SCOPEEND )
end sub
sub astTypeIniReplaceElement _
( _
byval tree as ASTNODE ptr, _
byval element as integer, _
byval expr as ASTNODE ptr _
)
'' Walk through the TYPEINI tree until the assign at index "element"
'' is reached, then replace the expression.
'' assumptions:
'' - tree is an array initializer,
'' - there are only TYPEINI_ASSIGN's, no ctorcalls/padding
dim as integer i = any
assert( astIsTYPEINI( tree ) )
tree = tree->l
assert( tree->class = AST_NODECLASS_TYPEINI_SCOPEINI )
tree = tree->r
i = 0
while( tree->class = AST_NODECLASS_TYPEINI_ASSIGN )
if( i = element ) then
astDelTree( tree->l )
tree->l = expr
exit sub
end if
tree = tree->r
i += 1
wend
'' should always be found
assert( FALSE )
end sub
private function hCallCtorList _
( _
byval t as ASTNODE ptr, _
byval n as ASTNODE ptr, _
byval target as ASTNODE ptr _
) as ASTNODE ptr
dim as ASTNODE ptr fldexpr = any
'' iter = *cast( subtype ptr, cast( byte ptr, @array(0) ) + ofs) )
fldexpr = astBuildDerefAddrOf( astCloneTree( target ), n->typeini.ofs, n->dtype, n->subtype, n->sym )
if( n->typeini.elements > 1 ) then
dim as FBSYMBOL ptr cnt, label, iter
cnt = symbAddTempVar( FB_DATATYPE_INTEGER )
label = symbAddLabel( NULL )
iter = symbAddTempVar( typeAddrOf( n->dtype ), n->subtype )
t = astNewLINK( t, astBuildVarAssign( iter, astNewADDROF( fldexpr ), AST_OPOPT_ISINI ), AST_LINK_RETURN_NONE )
'' for cnt = 0 to elements-1
t = astBuildForBegin( t, cnt, label, 0 )
'' ctor( *iter )
t = astNewLINK( t, astBuildCtorCall( n->subtype, astBuildVarDeref( iter ) ), AST_LINK_RETURN_NONE )
'' iter += 1
t = astNewLINK( t, astBuildVarInc( iter, 1 ), AST_LINK_RETURN_NONE )
'' next
t = astBuildForEnd( t, cnt, label, astNewCONSTi( n->typeini.elements ) )
else
'' ctor( this )
t = astNewLINK( t, astBuildCtorCall( n->subtype, fldexpr ), AST_LINK_RETURN_NONE )
end if
function = t
end function
private function hAstCheckTypeIniAssignment _
( _
byval n as ASTNODE ptr, _
byval maxsize as longint, _
byval scoped as integer _
) as integer
if( (maxsize > 0) ) then
select case n->class
case AST_NODECLASS_TYPEINI_PAD
if( n->typeini.ofs + n->typeini.bytes > maxsize ) then
return FALSE
end if
case else
if( n->typeini.ofs + n->typeini.bytes > maxsize ) then
if( fbPdCheckIsSet( FB_PDCHECK_UPCAST ) ) then
errReportWarn( FB_WARNINGMSG_UPCASTDISCARDSINITIALIZER )
end if
return FALSE
end if
end select
end if
return TRUE
end function
'' Builds up code to write a TYPEINI tree into a target variable/deref,
'' and deletes the TYPEINI tree.
function astTypeIniFlush overload _
( _
byval target as ASTNODE ptr, _
byval initree as ASTNODE ptr, _
byval update_typeinicount as integer, _
byval assignoptions as integer _
) as ASTNODE ptr
dim as ASTNODE ptr n = any, nxt = any, t = any, l = any
dim as longint maxsize = any
dim as integer scoped = 0
assert( astIsTYPEINI( initree ) )
assert( astIsVAR( target ) or astIsDEREF( target ) or _
astIsFIELD( target ) or astIsIDX( target ) )
if( update_typeinicount ) then
'' Unregister the TYPEINI - we're emitting it right now, no need
'' for astTypeIniUpdate() later. This only makes sense for TYPEINIs
'' that were previously registered via astTypeIniEnd(..., FALSE).
ast.typeinicount -= 1
end if
'' maxsize of 0 indicates no checking for target size
maxsize = 0
if( astIsVAR( target ) or astIsField( target ) ) then
maxsize = symbGetRealSize( target->sym )
end if
t = NULL
n = initree->l
'' Remove side-effects, if there's more than one initializer element,
'' because every element uses one "instance" of the target expression.
if( n ) then
if( n->r ) then
if( astHasSideFx( target ) ) then
t = astRemSideFx( target )
end if
end if
end if
while( n )
select case( n->class )
'' Write the given initializer expression to the given offset in the target
case AST_NODECLASS_TYPEINI_ASSIGN
if( hAstCheckTypeIniAssignment( n, maxsize, scoped ) ) then
if( n->sym ) then
'' Field?
if( symbIsField( n->sym ) ) then
'' If it's a bitfield, clear the whole field containing this bitfield,
'' otherwise the bitfield assignment(s) would leave unused bits
'' uninitialized.
if( symbFieldIsBitfield( n->sym ) ) then
'' Beginning of a field containing one or more bitfields?
if( n->sym->var_.bitpos = 0 ) then
l = astBuildDerefAddrOf( astCloneTree( target ), n->typeini.ofs, n->dtype, n->subtype )
l = astNewMEM( AST_OP_MEMCLEAR, l, astNewCONSTi( typeGetSize( symbGetFullType( n->sym ) ) ) )
t = astNewLINK( t, l, AST_LINK_RETURN_NONE )
end if
end if
end if
end if
l = astBuildDerefAddrOf( astCloneTree( target ), n->typeini.ofs, n->dtype, n->subtype, n->sym )
l = astNewASSIGN( l, n->l, assignoptions or AST_OPOPT_DONTCHKPTR )
assert( l )
t = astNewLINK( t, l, AST_LINK_RETURN_NONE )
end if
'' Clear the given amount of bytes at the given offset in the target
case AST_NODECLASS_TYPEINI_PAD
if( hAstCheckTypeIniAssignment( n, maxsize, scoped ) ) then
l = astBuildDerefAddrOf( astCloneTree( target ), n->typeini.ofs, n->dtype, n->subtype )
l = astNewMEM( AST_OP_MEMCLEAR, l, astNewCONSTi( n->typeini.bytes ) )
t = astNewLINK( t, l, AST_LINK_RETURN_NONE )
end if
'' Use the given CALL (and its ARGs) as-is, but insert the byref instance argument,
'' pointing to the given offset in the target
case AST_NODECLASS_TYPEINI_CTORCALL
if( hAstCheckTypeIniAssignment( n, maxsize, scoped ) ) then
l = astBuildDerefAddrOf( astCloneTree( target ), n->typeini.ofs, n->dtype, n->subtype, n->sym )
l = astPatchCtorCall( n->l, l )
t = astNewLINK( t, l, AST_LINK_RETURN_NONE )
end if
'' Build constructor calls for an array of elements
case AST_NODECLASS_TYPEINI_CTORLIST
t = hCallCtorList( t, n, target )
case AST_NODECLASS_TYPEINI_SCOPEINI
scoped += 1
case AST_NODECLASS_TYPEINI_SCOPEEND
scoped -= 1
case else
assert( FALSE )
end select
nxt = n->r
astDelNode( n )
n = nxt
wend
astDelNode( initree )
astDelTree( target )
function = t
end function
function astTypeIniFlush overload _
( _
byval target as FBSYMBOL ptr, _
byval initree as ASTNODE ptr, _
byval update_typeinicount as integer, _
byval assignoptions as integer _
) as ASTNODE ptr
assert( astIsTYPEINI( initree ) )
assert( symbIsVar( target ) )
function = astTypeIniFlush( astNewVAR( target ), initree, update_typeinicount, assignoptions )
end function
private sub hFlushExprStatic( byval n as ASTNODE ptr, byval basesym as FBSYMBOL ptr )
'' Get lhs symbol: maybe a field (in case of TYPEINI_ASSIGN in a struct),
'' or the basesym (in case of TYPEINI_ASSIGN to global var).
var sym = n->sym
if( sym = NULL ) then
sym = basesym
end if
var sdtype = symbGetType( sym )
var sfulldtype = symbGetFullType( sym )
if( symbIsRef( sym ) ) then
'' Initializers for references initialize the pointer,
'' not an object of the symbol's type.
sdtype = typeAddrOf( sdtype )
sfulldtype = typeAddrOf( sfulldtype )
end if
'' Get rhs expression
var expr = n->l
var edtype = astGetDataType( expr )
dim as FBSYMBOL ptr litsym = NULL
select case edtype
case FB_DATATYPE_CHAR, FB_DATATYPE_WCHAR
litsym = astGetStrLitSymbol( expr )
end select
'' not a literal string?
if( litsym = NULL ) then
'' offset?
if( astIsOFFSET( expr ) ) then
irEmitVARINIOFS( sym, astGetSymbol( expr ), expr->ofs.ofs )
'' anything else
else
'' Explicit cast of an address? We can discard the cast if it's an offset.
scope
var lexpr = expr->l
while( lexpr )
if( astIsOFFSET( lexpr ) ) then
irEmitVARINIOFS( sym, astGetSymbol( lexpr ), lexpr->ofs.ofs )
expr = NULL
exit while
end if
lexpr = lexpr->l
wend
end scope
if( expr ) then
'' must be a constant
assert( astIsCONST( expr ) )
'' different types? and there was no explicit cast?
if( edtype <> sdtype ) then
expr = astNewCONV( sfulldtype, symbGetSubtype( sym ), expr, AST_CONVOPT_DONTCHKPTR )
assert( expr <> NULL )
end if
if( typeGetClass( sdtype ) = FB_DATACLASS_FPOINT ) then
irEmitVARINIf( sym, astConstGetFloat( expr ) )
else
irEmitVARINIi( sym, astConstGetInt( expr ) )
end if
end if
end if
'' literal string..
else
'' not a wstring?
if( sdtype <> FB_DATATYPE_WCHAR ) then
'' convert?
if( edtype <> FB_DATATYPE_WCHAR ) then
'' less the null-char
irEmitVARINISTR( symbGetStrLen( sym ) - 1, _
symbGetVarLitText( litsym ), _
symbGetStrLen( litsym ) - 1 )
else
'' ditto
irEmitVARINISTR( symbGetStrLen( sym ) - 1, _
str( *symbGetVarLitTextW( litsym ) ), _
symbGetWstrLen( litsym ) - 1 )
end if
'' wstring..
else
'' convert?
if( edtype <> FB_DATATYPE_WCHAR ) then
'' less the null-char
irEmitVARINIWSTR( symbGetWstrLen( sym ) - 1, _
wstr( *symbGetVarLitText( litsym ) ), _
symbGetStrLen( litsym ) - 1 )
else
'' ditto
irEmitVARINIWSTR( symbGetWstrLen( sym ) - 1, _
symbGetVarLitTextW( litsym ), _
symbGetWstrLen( litsym ) - 1 )
end if
end if
end if
astDelTree( n->l )
n->l = NULL
end sub
sub astLoadStaticInitializer _
( _
byval tree as ASTNODE ptr, _
byval basesym as FBSYMBOL ptr _
)
dim as ASTNODE ptr n = any, nxt = any
assert( astIsTYPEINI( tree ) )
irEmitVARINIBEGIN( basesym )
n = tree->l
do while( n <> NULL )
nxt = n->r
select case as const n->class
case AST_NODECLASS_TYPEINI_PAD
irEmitVARINIPAD( n->typeini.bytes )
case AST_NODECLASS_TYPEINI_SCOPEINI
irEmitVARINISCOPEBEGIN( n->sym, n->typeiniscope.is_array )
case AST_NODECLASS_TYPEINI_SCOPEEND
irEmitVARINISCOPEEND( )