/
symb-comp.bas
1299 lines (1040 loc) · 34.7 KB
/
symb-comp.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
'' complex/compound/composite symbols helper functions
''
'' chng: sep/2006 written [v1ctor]
#include once "fb.bi"
#include once "fbint.bi"
#include once "rtl.bi"
type FB_SYMBNEST
sym as FBSYMBOL ptr
symtb as FBSYMBOLTB ptr '' prev symbol tb
hashtb as FBHASHTB ptr '' prev hash tb
ns as FBSYMBOL ptr '' prev namespace
end type
sub symbCompInit( )
for i as integer = 0 to AST_OPCODES-1
symb.globOpOvlTb(i).head = NULL
next
stackNew( @symb.neststk, 16, len( FB_SYMBNEST ), FALSE )
end sub
sub symbCompEnd( )
stackFree( @symb.neststk )
end sub
sub symbUdtAllocExt( byval udt as FBSYMBOL ptr )
assert( symbIsStruct( udt ) )
if( udt->udt.ext = NULL ) then
udt->udt.ext = xcallocate( sizeof( FB_STRUCTEXT ) )
end if
end sub
''::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'' default ctors
''::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
private function hDeclareProc _
( _
byval udt as FBSYMBOL ptr, _
byval op as AST_OP, _
byval add_rhs as integer, _
byval attrib as FB_SYMBATTRIB _
) as FBSYMBOL ptr
dim as FBSYMBOL ptr proc = any
'' Into the UDT namespace
symbNestBegin( udt, TRUE )
proc = symbPreAddProc( NULL )
'' add "this"
symbAddProcInstancePtr( udt, proc )
'' add right-side hand param?
if( add_rhs ) then
assert( symbIsStruct( udt ) )
symbAddProcParam( proc, "__FB_RHS__", FB_DATATYPE_STRUCT, udt, _
0, FB_PARAMMODE_BYREF, FB_SYMBATTRIB_NONE )
end if
attrib or= FB_SYMBATTRIB_METHOD
attrib or= FB_SYMBATTRIB_PRIVATE
'' cons|destructor?
if( op = INVALID ) then
proc = symbAddCtor( proc, NULL, attrib, _
FB_FUNCMODE_CDECL, FB_SYMBOPT_DECLARING )
'' op..
else
proc = symbAddOperator( proc, op, NULL, FB_DATATYPE_VOID, NULL, attrib, _
FB_FUNCMODE_CDECL, FB_SYMBOPT_DECLARING )
end if
'' Close the namespace again
symbNestEnd( TRUE )
function = proc
end function
private sub hSetMinimumVtableSize( byval udt as FBSYMBOL ptr )
'' vtables always have at least 2 elements (following GCC):
'' index 0: a NULL pointer
'' index 1: the pointer to this type's RTTI table
'' Slots for the pointers for virtual methods start at index 2.
''
'' Note: A vtable must be generated even if there are no virtuals,
'' to support RTTI. In that case it will only have the first two
'' elements (null and rtti pointers).
if( udt->udt.ext->vtableelements = 0 ) then
udt->udt.ext->vtableelements = 2
end if
end sub
private sub hBuildRtti( byval udt as FBSYMBOL ptr )
static as FBARRAYDIM dTB(0)
dim as ASTNODE ptr initree = any, rttibase = any
dim as FBSYMBOL ptr rtti = any, fld = any
'' static shared UDT.rtti as $fb_RTTI
'' (real identifier given later during mangling)
symbNestBegin( udt, TRUE )
rtti = symbAddVar( NULL, NULL, FB_DATATYPE_STRUCT, symb.rtti.fb_rtti, 0, 0, dTB(), _
FB_SYMBATTRIB_CONST or FB_SYMBATTRIB_STATIC or FB_SYMBATTRIB_SHARED, _
FB_SYMBOPT_PRESERVECASE )
rtti->stats or= FB_SYMBSTATS_RTTITABLE
symbNestEnd( TRUE )
udt->udt.ext->rtti = rtti
'' initializer
initree = astTypeIniBegin( FB_DATATYPE_STRUCT, symb.rtti.fb_rtti, FALSE, 0 )
astTypeIniScopeBegin( initree, rtti )
'' stdlibvtable = NULL
fld = symbUdtGetFirstField( symb.rtti.fb_rtti )
astTypeIniAddAssign( initree, astNewCONSTi( 0, typeAddrOf( FB_DATATYPE_VOID ), NULL ), fld )
'' id = @"mangled name"
fld = symbUdtGetNextInitableField( fld )
astTypeIniAddAssign( initree, astNewADDROF( astNewVAR( symbAllocStrConst( symbGetMangledName( udt ), -1 ) ) ), fld )
'' rttibase = @(base's RTTI data) or NULL if there is no base
fld = symbUdtGetNextInitableField( fld )
if( udt->udt.base ) then
rttibase = astNewADDROF( astNewVAR( udt->udt.base->subtype->udt.ext->rtti ) )
else
rttibase = astNewCONSTi( 0, typeAddrOf( FB_DATATYPE_VOID ) )
end if
astTypeIniAddAssign( initree, rttibase, fld )
astTypeIniScopeEnd( initree, rtti )
astTypeIniEnd( initree, TRUE )
symbSetTypeIniTree( rtti, initree )
end sub
private sub hBuildVtable( byval udt as FBSYMBOL ptr )
static as FBARRAYDIM dTB(0)
dim as ASTNODE ptr initree = any, basevtableinitree = any
dim as FBSYMBOL ptr member = any, rtti = any, vtable = any
dim as integer i = any, basevtableelements = any
'' The vtable is an array of pointers:
'' 0. null pointer (why? just following GCC...)
'' 1. rtti pointer
'' 2. and following: procptrs corresponding to virtual methods
'' in the order they were parsed.
assert( udt->udt.ext->vtableelements >= 2 )
'' static shared UDT.vtable(0 to elements-1) as any ptr
'' (real identifier given later during mangling)
symbNestBegin( udt, TRUE )
dTB(0).upper = udt->udt.ext->vtableelements - 1
vtable = symbAddVar( NULL, NULL, typeAddrOf( FB_DATATYPE_VOID ), NULL, 0, 1, dTB(), _
FB_SYMBATTRIB_CONST or FB_SYMBATTRIB_STATIC or FB_SYMBATTRIB_SHARED, _
FB_SYMBOPT_PRESERVECASE )
vtable->stats or= FB_SYMBSTATS_VTABLE
symbNestEnd( TRUE )
'' Find information about the base UDT's vtable:
'' the number of elements,
'' and the initree (so it can be copied into the new vtable)
''
'' - If the base has no virtuals, vtableelements will be 2
'' (due to hSetMinimumVtableSize())
'' - If the base is OBJECT, the vtable is hidden in the rtlib,
'' thus there is no initree for us to use. Luckily we don't need it
'' anyways, since OBJECT doesn't have any virtuals.
assert( symbIsField( udt->udt.base ) )
assert( symbGetType( udt->udt.base ) = FB_DATATYPE_STRUCT )
assert( symbIsStruct( udt->udt.base->subtype ) )
basevtableelements = udt->udt.base->subtype->udt.ext->vtableelements
'' Any virtuals (more than the default 2 elements)?
if( basevtableelements > 2 ) then
assert( symbIsVar( udt->udt.base->subtype->udt.ext->vtable ) )
basevtableinitree = udt->udt.base->subtype->udt.ext->vtable->var_.initree
else
basevtableinitree = NULL
end if
'' {
initree = astTypeIniBegin( typeAddrOf( FB_DATATYPE_VOID ), NULL, FALSE, 0 )
astTypeIniScopeBegin( initree, vtable )
'' 0. null pointer = NULL
astTypeIniAddAssign( initree, astNewCONSTi( 0, typeAddrOf( FB_DATATYPE_VOID ) ), vtable )
'' 1. rtti pointer = @rtti
rtti = udt->udt.ext->rtti
astTypeIniAddAssign( initree, astNewADDROF( astNewVAR( rtti ) ), vtable )
'' initialize inherited procptrs, to the same expression as in the
'' base vtable's initializer
i = 2
if( basevtableinitree ) then
'' Copy the typeini assigns from the base vtable's initializer,
'' except the first 2 (they are set above already)
astTypeIniCopyElements( initree, basevtableinitree, 2 )
assert( basevtableelements > 2 )
i += (basevtableelements - 2)
end if
'' Fill new vtable entries with NULLs first, to be safe, and also to
'' initialize any new unimplemented pure-virtual slots.
'' We could let them point to __cxa_pure_virtual() like gcc,
'' but with a NULL pointer crash instead of the abort() we'll actually
'' get a more useful run-time error under -exx.
while( i <= dTB(0).upper )
astTypeIniAddAssign( initree, astNewCONSTi( 0, typeAddrOf( FB_DATATYPE_VOID ) ), vtable )
i += 1
wend
'' }
astTypeIniScopeEnd( initree, vtable )
astTypeIniEnd( initree, TRUE )
symbSetTypeIniTree( vtable, initree )
'' 1. new (and not inherited) entries for ...
'' - virtuals: must be set to point to their bodies for now.
'' (not yet overridden)
'' - abstracts: are set to NULL, so if they're not overridden, a NULL
'' pointer crash will happen when they're called, which is handled by
'' -exx error checking (see also astBuildVtableLookup()).
'' (GCC sets it to point to __cxa_pure_virtual(), which shows a
'' run-time error message and aborts the program)
''
'' 2. any entries for inherited virtuals/abstracts that were overridden
'' by a normal method must be updated to point to the normal method.
'' For each member of this UDT (does not include inherited members)
member = symbGetCompSymbTb( udt ).head
while( member )
'' procedure?
if( symbIsProc( member ) ) then
i = symbProcGetVtableIndex( member )
if( (i > 0) and (not symbIsAbstract( member )) ) then
astTypeIniReplaceElement( initree, i, astBuildProcAddrof( member ) )
end if
end if
member = member->next
wend
udt->udt.ext->vtable = vtable
end sub
private sub hProcBegin( byval udt as FBSYMBOL ptr, byval proc as FBSYMBOL ptr )
'' constructor|destructor|operator parent[.let]( ... )
symbNestBegin( udt, TRUE )
astProcBegin( proc, FALSE )
end sub
private sub hProcEnd( )
'' end constructor|destructor|operator
astProcEnd( FALSE )
symbNestEnd( TRUE )
end sub
private sub hAddCtorBody _
( _
byval udt as FBSYMBOL ptr, _
byval proc as FBSYMBOL ptr, _
byval is_copyctor as integer _
)
'' The AST will add any implicit base/field construction/destruction
'' code automatically
hProcBegin( udt, proc )
if( is_copyctor ) then
'' assign op overload will do the rest
astAdd( astNewASSIGN( _
astBuildVarField( symbGetParamVar( symbGetProcHeadParam( proc ) ) ), _
astBuildVarField( symbGetParamVar( symbGetProcTailParam( proc ) ) ) ) )
end if
hProcEnd( )
symbSetCantUndef( udt )
end sub
private function hArrayDescPtr _
( _
byval descexpr as ASTNODE ptr, _
byval dtype as integer, _
byval subtype as FBSYMBOL ptr _
) as ASTNODE ptr
descexpr = astNewADDROF( descexpr )
descexpr = astNewBOP( AST_OP_ADD, descexpr, astNewCONSTi( symb.fbarray_ptr ) )
descexpr = astNewCONV( typeMultAddrOf( dtype, 2 ), subtype, descexpr, AST_CONVOPT_DONTCHKPTR )
descexpr = astNewDEREF( descexpr )
function = descexpr
end function
'' Copying over a dynamic array, needed for the automatically-generated implicit
'' LET overloads for UDTs containing dynamic array fields.
private sub hAssignDynamicArray _
( _
byval fld as FBSYMBOL ptr, _
byval dstexpr as ASTNODE ptr, _
byval srcexpr as ASTNODE ptr _
)
dim as integer dtype = any
dim as FBSYMBOL ptr dst = any, src = any, limit = any
dim as FBSYMBOL ptr looplabel = any, exitlabel = any
'' 1. REDIM dest to same size as source (will call dtors/ctors as needed)
dtype = fld->typ
astAdd( rtlArrayRedimTo( dstexpr, srcexpr, dtype, fld->subtype ) )
'' 2. Loop over all elements (if any) and copy them over 1 by 1, using
'' astNewASSIGN(), that will call let overloads as needed, and handle
'' strings.
dtype = typeAddrOf( dtype )
looplabel = symbAddLabel( NULL )
exitlabel = symbAddLabel( NULL )
dst = symbAddTempVar( dtype, fld->subtype )
src = symbAddTempVar( dtype, fld->subtype )
limit = symbAddTempVar( dtype, fld->subtype )
'' dst = this.dstdesc.ptr
'' src = this.srcdesc.ptr
'' limit = src + this.srcdesc.size
astAdd( astBuildVarAssign( dst, astBuildDerefAddrOf( dstexpr, symb.fbarray_ptr, dtype, fld->subtype ) ) )
astAdd( astBuildVarAssign( src, astBuildDerefAddrOf( srcexpr, symb.fbarray_ptr, dtype, fld->subtype ) ) )
astAdd( astBuildVarAssign( limit, _
astNewBOP( AST_OP_ADD, _
astNewVAR( src ), _
astBuildDerefAddrOf( srcexpr, symb.fbarray_size, FB_DATATYPE_UINT, NULL ) ) ) )
'' looplabel:
astAdd( astNewLABEL( looplabel ) )
'' if src >= limit then goto exitlabel
astAdd( astBuildBranch( astNewBOP( AST_OP_GE, astNewVAR( src ), astNewVAR( limit ) ), exitlabel, TRUE ) )
'' *dst = *src
astAdd( astNewASSIGN( astBuildVarDeref( dst ), astBuildVarDeref( src ) ) )
'' dst += 1 (astBuildVarInc() does pointer arithmetic)
'' src += 1
astAdd( astBuildVarInc( dst, 1 ) )
astAdd( astBuildVarInc( src, 1 ) )
'' goto looplabel
astAdd( astNewBRANCH( AST_OP_JMP, looplabel ) )
'' exitlabel:
astAdd( astNewLABEL( exitlabel ) )
end sub
'':::::
private sub hAssignList _
( _
byval fld as FBSYMBOL ptr, _
byval dstexpr as ASTNODE ptr, _
byval srcexpr as ASTNODE ptr _
) static
dim as FBSYMBOL ptr cnt, label, dst, src, subtype
subtype = symbGetSubtype( fld )
cnt = symbAddTempVar( FB_DATATYPE_INTEGER )
label = symbAddLabel( NULL )
dst = symbAddTempVar( typeAddrOf( symbGetType( fld ) ), subtype )
src = symbAddTempVar( typeAddrOf( symbGetType( fld ) ), subtype )
'' dst = @this.dst(0)
astAdd( astBuildVarAssign( dst, astNewADDROF( dstexpr ) ) )
'' src = @this.src(0)
astAdd( astBuildVarAssign( src, astNewADDROF( srcexpr ) ) )
'' for cnt = 0 to symbGetArrayElements( dst )-1
astAdd( astBuildForBegin( NULL, cnt, label, 0 ) )
'' *dst = *src
astAdd( astNewASSIGN( astBuildVarDeref( dst ), astBuildVarDeref( src ) ) )
'' dst += 1
astAdd( astBuildVarInc( dst, 1 ) )
'' src += 1
astAdd( astBuildVarInc( src, 1 ) )
'' next
astAdd( astBuildForEnd( NULL, cnt, label, astNewCONSTi( symbGetArrayElements( fld ) ) ) )
end sub
'':::::
private function hCopyUnionFields _
( _
byval this_ as FBSYMBOL ptr, _
byval rhs as FBSYMBOL ptr, _
byval base_fld as FBSYMBOL ptr _
) as FBSYMBOL ptr
dim as FBSYMBOL ptr fld = any
dim as longint bytes = any, lgt = any, base_ofs = any
'' merge all union fields
fld = base_fld
bytes = 0
base_ofs = symbGetOfs( base_fld )
do
lgt = (symbGetLen( fld ) * symbGetArrayElements( fld )) + _
(symbGetOfs( fld ) - base_ofs)
if( lgt > bytes ) then
bytes = lgt
end if
fld = fld->next
if( fld = NULL ) then
exit do
end if
loop while( symbGetIsUnionField( fld ) )
'' copy all them at once
astAdd( astNewMEM( AST_OP_MEMMOVE, _
astBuildVarField( this_, base_fld ), _
astBuildVarField( rhs, base_fld ), _
bytes ) )
function = fld
end function
private sub hAddLetOpBody _
( _
byval udt as FBSYMBOL ptr, _
byval letproc as FBSYMBOL ptr _
)
dim as FBSYMBOL ptr fld = any, this_ = any, rhs = any
dim as ASTNODE ptr dstexpr = any, srcexpr = any
hProcBegin( udt, letproc )
this_ = symbGetParamVar( symbGetProcHeadParam( letproc ) )
rhs = symbGetParamVar( symbGetProcTailParam( letproc ) )
'' For each field, except dynamic array field descriptors (instead, the
'' fake dynamic array field will be handled in order to copy the dynamic
'' array)
fld = symbGetCompSymbTb( udt ).head
while( fld )
if( symbIsField( fld ) and (not symbIsDescriptor( fld )) ) then
'' part of an union?
if( symbGetIsUnionField( fld ) ) then
fld = hCopyUnionFields( this_, rhs, fld )
continue while
end if
dstexpr = astBuildVarField( this_, fld )
srcexpr = astBuildVarField( rhs, fld )
'' Dynamic array field?
if( symbIsDynamic( fld ) ) then
hAssignDynamicArray( fld, dstexpr, srcexpr )
else
if( symbGetArrayDimensions( fld ) = 0 ) then
'' this.field = rhs.field
astAdd( astNewASSIGN( dstexpr, srcexpr ) )
else
hAssignList( fld, dstexpr, srcexpr )
end if
end if
end if
fld = fld->next
wend
hProcEnd( )
symbSetCantUndef( udt )
end sub
'' Declare & add any implicit/default members and global vars if needed
sub symbUdtAddDefaultMembers( byval udt as FBSYMBOL ptr )
dim as FBSYMBOL ptr defctor = any, copyctor = any, letop = any, dtor = any
dim as integer base_without_defaultctor = any
''
'' If this UDT has fields with ctors, we have to make sure to add
'' default and copy ctors aswell as a dtor and a Let operator to the
'' parent, if the user didn't do that yet. This ensures the fields'
'' ctors/dtors will be called and also that they will be copied
'' correctly: In case they have Let overloads themselves, we can't just
'' do the default memcpy(). (If the parent already has a Let overload,
'' we can assume it's correct already)
''
'' Besides that, in case there were any field initializers specified,
'' we want to add a default constructor if there isn't any constructor
'' yet, to ensure the field initializers are getting used.
''
'' Derived?
if( udt->udt.base ) then
assert( symbIsField( udt->udt.base ) )
assert( symbGetType( udt->udt.base ) = FB_DATATYPE_STRUCT )
assert( symbIsStruct( udt->udt.base->subtype ) )
'' No default ctor?
base_without_defaultctor = (symbGetCompDefCtor( udt->udt.base->subtype ) = NULL)
else
base_without_defaultctor = FALSE
end if
defctor = NULL
copyctor = NULL
letop = NULL
dtor = NULL
'' Ctor/inited fields and no ctor yet?
if( (symbGetUDTHasCtorField( udt ) or symbGetUDTHasInitedField( udt )) and _
(symbGetCompCtorHead( udt ) = NULL) ) then
if( base_without_defaultctor ) then
'' Cannot implicitly generate a default ctor,
'' show a nicer error message than astProcEnd() would.
'' It would report the missing BASE() initializer,
'' but from here we can show a more useful error.
errReport( FB_ERRMSG_NEEDEXPLICITDEFCTOR )
else
'' Add default ctor
defctor = hDeclareProc( udt, INVALID, FALSE, FB_SYMBATTRIB_OVERLOADED or FB_SYMBATTRIB_CONSTRUCTOR )
end if
end if
if( symbGetUDTHasCtorField( udt ) ) then
'' Let operator (must be defined before the copy ctor)
if( symbGetCompCloneProc( udt ) = NULL ) then
letop = hDeclareProc( udt, AST_OP_ASSIGN, TRUE, FB_SYMBATTRIB_OVERLOADED or FB_SYMBATTRIB_OPERATOR )
'' Don't allow the implicit LET to override a FINAL LET from the base
symbProcCheckOverridden( letop, TRUE )
end if
'' Copy ctor
if( symbGetCompCopyCtor( udt ) = NULL ) then
if( base_without_defaultctor ) then
'' Cannot implicitly generate a copy ctor,
'' same as with default ctor above.
errReport( FB_ERRMSG_NEEDEXPLICITCOPYCTOR )
else
copyctor = hDeclareProc( udt, INVALID, TRUE, FB_SYMBATTRIB_OVERLOADED or FB_SYMBATTRIB_CONSTRUCTOR )
end if
end if
end if
'' has fields with dtors?
if( symbGetUDTHasDtorField( udt ) ) then
'' no default dtor explicitly defined?
if( symbGetCompDtor( udt ) = NULL ) then
'' Dtor
dtor = hDeclareProc( udt, INVALID, FALSE, FB_SYMBATTRIB_DESTRUCTOR )
'' Don't allow the implicit dtor to override a FINAL dtor from the base
symbProcCheckOverridden( dtor, TRUE )
end if
end if
''
'' Add vtable and rtti global variables
''
'' - The vtable can only be created once all methods are known,
'' how many virtuals there are, which method overrides virtuals
'' from the base, etc.
'' Even the implicit destructor (if any) must be declared before
'' the vtable is added, in case it should override a virtual dtor.
''
'' - Creating the vtable depends on FBSYMBOL.udt.ext->rtti being set,
'' because the vtable includes a pointer to the rtti table,
'' thus the rtti table should be added before the vtable.
''
'' - Any constructor body depends on FBSYMBOL.udt.ext->vtable being set,
'' so the vtable must be added before any constructor bodies.
''
if( symbGetHasRTTI( udt ) ) then
symbUdtAllocExt( udt )
hSetMinimumVtableSize( udt )
'' only if it isn't FB's own Object base super class
'' (for which the rtlib already contains these declarations)
if( udt <> symb.rtti.fb_object ) then
hBuildRtti( udt )
hBuildVtable( udt )
end if
end if
''
'' Add bodies if any implicit ctor/dtor/let procs were declared above
''
if( defctor ) then
hAddCtorBody( udt, defctor, FALSE )
end if
if( copyctor ) then
hAddCtorBody( udt, copyctor, TRUE )
end if
if( letop ) then
hAddLetOpBody( udt, letop )
end if
if( dtor ) then
hAddCtorBody( udt, dtor, FALSE )
end if
end sub
''::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'' getters/setters
''::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'':::::
private function hIsLhsEqRhs _
( _
byval parent as FBSYMBOL ptr, _
byval proc as FBSYMBOL ptr _
) as integer static
dim as FBSYMBOL ptr param, subtype
function = FALSE
param = symbGetProcTailParam( proc )
if( symbGetParamMode( param ) = FB_PARAMMODE_BYREF ) then
subtype = symbGetSubtype( param )
if( subtype = NULL ) then
return FALSE
end if
'' forward?
if( symbGetClass( subtype ) = FB_SYMBCLASS_FWDREF ) then
'' not a pointer?
if( symbGetType( param ) = FB_DATATYPE_FWDREF ) then
'' same name?
if( subtype->hash.index = parent->hash.index ) then
return TRUE
end if
end if
return FALSE
end if
if( subtype = parent ) then
select case symbGetClass( parent )
case FB_SYMBCLASS_STRUCT
function = ( symbGetType( param ) = FB_DATATYPE_STRUCT )
case FB_SYMBCLASS_CLASS
'...
end select
end if
end if
end function
'' Check whether UDT doesn't have either of dtor/copy ctor/virtual methods
'' (UDTs that have any of these are handled specially by BYVAL params and
'' function results. For example, BYVAL params do copy construction, and use
'' this function to check whether there is a copyctor and whether a temp copy
'' to be passed byref must be used or not)
function symbCompIsTrivial( byval sym as FBSYMBOL ptr ) as integer
function = ((symbGetCompCopyCtor( sym ) = NULL) and _
(symbGetCompDtor( sym ) = NULL) and _
(not symbGetHasRTTI( sym )))
end function
sub symbSetCompCtorHead( byval sym as FBSYMBOL ptr, byval proc as FBSYMBOL ptr )
if( symbIsStruct( sym ) ) then
assert( symbIsConstructor( proc ) )
symbUdtAllocExt( sym )
if( sym->udt.ext->ctorhead = NULL ) then
'' Add ctor head (first overload)
sym->udt.ext->ctorhead = proc
end if
end if
end sub
sub symbCheckCompCtor( byval sym as FBSYMBOL ptr, byval proc as FBSYMBOL ptr )
if( symbIsStruct( sym ) ) then
select case( symbGetProcParams( proc ) )
'' default?
case 1
'' only the THIS param - it's a default ctor
'' (this takes precedence over other ctors with all optional params)
sym->udt.ext->defctor = proc
'' copy?
case 2
'' 2 params - it could be a copy ctor
if( sym->udt.ext->copyctor = NULL ) then
if( hIsLhsEqRhs( sym, proc ) ) then
sym->udt.ext->copyctor = proc
end if
end if
end select
'' all params optional? then it can be used as default ctor
if( sym->udt.ext->defctor = NULL ) then
if( symbGetProcOptParams( proc ) = symbGetProcParams( proc ) - 1 ) then
sym->udt.ext->defctor = proc
end if
end if
end if
end sub
sub symbSetCompDtor( byval sym as FBSYMBOL ptr, byval proc as FBSYMBOL ptr )
if( symbIsStruct( sym ) ) then
assert( symbIsDestructor( proc ) )
symbUdtAllocExt( sym )
if( sym->udt.ext->dtor = NULL ) then
'' Add dtor
sym->udt.ext->dtor = proc
end if
end if
end sub
function symbGetCompCtorHead( byval sym as FBSYMBOL ptr ) as FBSYMBOL ptr
if( sym ) then
if( symbIsStruct( sym ) ) then
if( sym->udt.ext ) then
function = sym->udt.ext->ctorhead
end if
end if
end if
end function
function symbGetCompDefCtor( byval sym as FBSYMBOL ptr ) as FBSYMBOL ptr
if( sym ) then
if( symbIsStruct( sym ) ) then
if( sym->udt.ext ) then
function = sym->udt.ext->defctor
end if
end if
end if
end function
function symbGetCompCopyCtor( byval sym as FBSYMBOL ptr ) as FBSYMBOL ptr
if( sym ) then
if( symbIsStruct( sym ) ) then
if( sym->udt.ext ) then
function = sym->udt.ext->copyctor
end if
end if
end if
end function
function symbGetCompDtor( byval sym as FBSYMBOL ptr ) as FBSYMBOL ptr
if( sym ) then
if( symbIsStruct( sym ) ) then
if( sym->udt.ext ) then
function = sym->udt.ext->dtor
end if
end if
end if
end function
sub symbCheckCompClone( byval sym as FBSYMBOL ptr, byval proc as FBSYMBOL ptr )
if( symbIsStruct( sym ) ) then
'' clone?
if( hIsLhsEqRhs( sym, proc ) ) then
symbUdtAllocExt( sym )
sym->udt.ext->clone = proc
end if
end if
end sub
function symbGetCompCloneProc( byval sym as FBSYMBOL ptr ) as FBSYMBOL ptr
if( sym ) then
if( symbIsStruct( sym ) ) then
if( sym->udt.ext ) then
function = sym->udt.ext->clone
end if
end if
end if
end function
'':::::
function symbGetCompOpOvlHead _
( _
byval sym as FBSYMBOL ptr, _
byval op as AST_OP _
) as FBSYMBOL ptr
'' self?
if( astGetOpIsSelf( op ) ) then
select case symbGetClass( sym )
case FB_SYMBCLASS_STRUCT
if( sym->udt.ext = NULL ) then
return NULL
end if
function = symbGetUDTOpOvlTb(sym)(op - AST_OP_SELFBASE)
case FB_SYMBCLASS_ENUM
function = NULL
case FB_SYMBCLASS_CLASS
'' ...
end select
'' not self..
else
function = symb.globOpOvlTb(op).head
end if
end function
'':::::
sub symbSetCompOpOvlHead _
( _
byval sym as FBSYMBOL ptr, _
byval proc as FBSYMBOL ptr _
)
dim as AST_OP op = symbGetProcOpOvl( proc )
'' self?
if( astGetOpIsSelf( op ) ) then
if( symbIsStruct( sym ) ) then
symbUdtAllocExt( sym )
symbGetUDTOpOvlTb(sym)(op - AST_OP_SELFBASE) = proc
end if
'' assign?
if( op = AST_OP_ASSIGN ) then
symbCheckCompClone( sym, proc )
end if
'' not self..
else
symb.globOpOvlTb(op).head = proc
end if
end sub
'' Returns vtable index for new virtual method
function symbCompAddVirtual( byval udt as FBSYMBOL ptr ) as integer
'' Virtuals require the vptr, i.e. the UDT must extend OBJECT
assert( symbGetHasRTTI( udt ) )
symbUdtAllocExt( udt )
hSetMinimumVtableSize( udt )
function = udt->udt.ext->vtableelements
udt->udt.ext->vtableelements += 1
end function
function symbCompGetAbstractCount( byval udt as FBSYMBOL ptr ) as integer
assert( symbIsStruct( udt ) )
if( udt->udt.ext ) then
function = udt->udt.ext->abstractcount
end if
end function
''::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'' nesting
''::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
/'private sub hDumpHashTb
dim as FBHASHTB ptr hashtb = symb.hashlist.tail
do
dim as zstring ptr id = symbGetName( hashtb->owner )
hashtb = hashtb->prev
print *iif( id, id, @"main" );
if( hashtb = NULL ) then
exit do
end if
print ,
loop
print
end sub'/
'':::::
private sub hInsertNested _
( _
byval sym as FBSYMBOL ptr, _
byval lasttb as FBHASHTB ptr, _
byval base_ns as FBSYMBOL ptr _
)
dim as FBHASHTB ptr hashtb = any
'' add all parents to hash list, but the base one
dim as FBSYMBOL ptr ns = symbGetNamespace( sym )
do until( ns = base_ns )
hashtb = @symbGetCompHashTb( ns )
if( symbGetCompExt( ns ) = NULL ) then
symbGetCompExt( ns ) = symbCompAllocExt( )
end if
symbGetCompExt( ns )->cnt += 1
if( symbGetCompExt( ns )->cnt <> 1 ) then
'' remove from import hash tb list
symbHashListRemoveNamespace( ns )
end if
'' add to nested hash tb list
'' (in reverse other, child ns must be the tail, parents follow)
symbHashListAddBefore( lasttb, hashtb )
lasttb = hashtb
ns = symbGetNamespace( ns )
loop
end sub
'':::::
private sub hRemoveNested _
( _
byval sym as FBSYMBOL ptr, _
byval base_ns as FBSYMBOL ptr _
)
'' remove all parents from the hash list, but the base one
dim as FBSYMBOL ptr ns = symbGetNamespace( sym )
do until( ns = base_ns )
symbGetCompExt( ns )->cnt -= 1
'' remove from nested hash tb list
symbHashListDel( @symbGetCompHashTb( ns ) )
if( symbGetCompExt( ns )->cnt <> 0 ) then
'' add to import hash tb list
symbHashListInsertNamespace( ns, _
symbGetCompSymbTb( ns ).head )
end if
ns = symbGetNamespace( ns )
loop
end sub
'':::::
private sub hInsertImported _
( _
byval sym as FBSYMBOL ptr _
)
if( symbGetCompExt( sym ) = NULL ) then
exit sub
end if
dim as FBSYMBOL ptr imp_ = symbGetCompImportHead( sym )
do while( imp_ <> NULL )
dim as FBSYMBOL ptr ns = symbGetImportNamespc( imp_ )
if( ns <> NULL ) then
symbGetCompExt( ns )->cnt += 1
if( symbGetCompExt( ns )->cnt = 1 ) then
'' add to import hash tb list
symbHashListInsertNamespace( ns, _
symbGetCompSymbTb( ns ).head )
end if
end if
imp_ = symbGetImportNext( imp_ )
loop
end sub
'':::::
private sub hRemoveImported _
( _
byval sym as FBSYMBOL ptr _
)
if( symbGetCompExt( sym ) = NULL ) then
exit sub
end if
dim as FBSYMBOL ptr imp_ = symbGetCompImportHead( sym )
do while( imp_ <> NULL )
dim as FBSYMBOL ptr ns = symbGetImportNamespc( imp_ )
if( ns <> NULL ) then
symbGetCompExt( ns )->cnt -= 1
if( symbGetCompExt( ns )->cnt = 0 ) then
'' remove from import hash tb list
symbHashListRemoveNamespace( ns )
end if
end if