/
symb-var.bas
978 lines (808 loc) · 29.5 KB
/
symb-var.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
'' symbol table module for variables (scalars and arrays)
''
'' chng: sep/2004 written [v1ctor]
'' jan/2005 updated to use real linked-lists [v1ctor]
#include once "fb.bi"
#include once "fbint.bi"
#include once "parser.bi"
#include once "hash.bi"
#include once "list.bi"
#include once "ast.bi"
sub symbVarInit( )
'' assuming it's safe to create UDT symbols here, the array
'' dimension type must be allocated at module-level or it
'' would be removed when going out scope
static as FBARRAYDIM dTB(0)
'' type FBARRAYDIM
symb.fbarraydim = symbStructBegin( NULL, NULL, NULL, "__FB_ARRAYDIMTB$", NULL, FALSE, 0, FALSE, 0, 0 )
'' elements as integer
symbAddField( symb.fbarraydim, "elements", 0, dTB(), _
FB_DATATYPE_INTEGER, NULL, 0, 0, 0 )
'' lbound as integer
symbAddField( symb.fbarraydim, "lbound", 0, dTB(), _
FB_DATATYPE_INTEGER, NULL, 0, 0, 0 )
'' ubound as integer
symbAddField( symb.fbarraydim, "ubound", 0, dTB(), _
FB_DATATYPE_INTEGER, NULL, 0, 0, 0 )
'' end type
symbStructEnd( symb.fbarraydim )
''
'' Store some field offsets into globals for easy access
''
'' (should have been set by now)
assert( (env.pointersize = 4) or (env.pointersize = 8) )
'' FBARRAY
symb.fbarray_data = 0
symb.fbarray_ptr = env.pointersize '' behind: data
symb.fbarray_size = env.pointersize * 2 '' behind: data, ptr
symb.fbarray_dimtb = env.pointersize * 6 '' behind: data, ptr, size, element_len, dimensions, flags
'' FBARRAYDIM
symb.fbarraydim_lbound = env.pointersize '' behind: elements
symb.fbarraydim_ubound = env.pointersize * 2 '' behind: elements, lbound
end sub
sub symbVarEnd( )
end sub
''::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'' add
''::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'' Retrieve the arraydtype for which an array descriptor type was created:
'' It is encoded in the descriptor's data pointer field type.
sub symbGetDescTypeArrayDtype _
( _
byval desctype as FBSYMBOL ptr, _
byref arraydtype as integer, _
byref arraysubtype as FBSYMBOL ptr _
)
dim as FBSYMBOL ptr fld = any
assert( symbIsStruct( desctype) and symbIsDescriptor( desctype ) )
fld = symbUdtGetFirstField( desctype )
assert( typeIsPtr( symbGetType( fld ) ) )
arraydtype = typeDeref( symbGetFullType( fld ) )
arraysubtype = fld->subtype
end sub
'' Retrieve the dimension count for which an array descriptor type was created
function symbGetDescTypeDimensions( byval desctype as FBSYMBOL ptr ) as integer
dim as integer dimtbsize = any, dimensions = any
assert( symbIsStruct( desctype) and symbIsDescriptor( desctype ) )
'' If the dimensions were unknown, return -1 again, not FB_MAXARRAYDIMS,
'' so that if this is used in a call to symbAddArrayDescriptorType(), it
'' would produce/re-use the same descriptor type as this one.
if( *desctype->id.alias = "FBARRAY" ) then
return -1
end if
'' dimensions = sizeof(dimTB) \ sizeof(FBARRAYDIM)
dimtbsize = symbGetSizeOf( desctype ) - (env.pointersize * 6)
dimensions = dimtbsize \ (env.pointersize * 3)
assert( (dimensions > 0) and (dimensions <= FB_MAXARRAYDIMS) )
assert( *desctype->id.alias = "FBARRAY" & dimensions )
function = dimensions
end function
function symbAddArrayDescriptorType _
( _
byval dimensions as integer, _
byval arraydtype as integer, _
byval arraysubtype as FBSYMBOL ptr _
) as FBSYMBOL ptr
static as FBARRAYDIM dTB(0)
dim as string id, aliasid
dim as FBSYMBOL ptr sym = any, parent = any
dim as FBSYMBOLTB ptr symtb = any
dim as FBHASHTB ptr hashtb = any
dim as integer attrib = any, pattrib = any
'' Note: the arraydtype may even be FB_DATATYPE_VOID, e.g. in case of
'' "array() as any" parameters.
''
'' How to handle fixed-length string arrays? The descriptor type's data
'' pointer can only be a zstring/wstring ptr, because FB doesn't support
'' something like "zstring * N ptr". Thus as far as the descriptor type
'' is concerned, the information about string length is lost (although
'' it's still stored in the descriptor's element_len field at runtime).
''
'' This also means that the string length won't/doesn't need to be
'' encoded in the descriptor type's mangled name, neither here nor in
'' symbMangleType().
if( typeGetDtAndPtrOnly( arraydtype ) = FB_DATATYPE_FIXSTR ) then
arraydtype = typeJoin( arraydtype, FB_DATATYPE_CHAR )
end if
assert( typeGetDtOnly( arraydtype ) <> FB_DATATYPE_FIXSTR )
''
'' The ALIAS for the C++ mangling (hMangleUdtId() will add the array
'' dtype as template parameter later, because it may have to use
'' abbreviations according to the Itanium C++ ABI mangling rules).
'' Arrays becoming FBARRAY matches how strings become FBSTRING.
'' array() = "FBARRAY"
'' array(any) = "FBARRAY1"
'' array(any, any) = "FBARRAY2"
'' array(any * 8) = "FBARRAY8"
''
'' array() and array(any * FB_MAXARRAYDIMS) must be mangled different so
'' that we get different C++ mangling for these two procedures:
'' sub f( byval p as sub( array() as integer ) )
'' sub f( byval p as sub( array(any, any, any, any, any, any, any, any) as integer ) )
'' otherwise they'd collide in the generated .asm.
''
aliasid = "FBARRAY"
if( dimensions > 0 ) then
aliasid &= dimensions
end if
'' Some unique internal id that allows this descriptor type to be looked
'' up later when we need one with the same dimensions & array dtype
'' again. '$' prefix ensures that there are no collisions with user's
'' ids. Always keep the top-level const for array datatypes to avoid
'' conflicts between types differing only by const.
id = "$" + aliasid
id += "<"
symbMangleType( id, arraydtype, arraysubtype, FB_MANGLEOPT_KEEPTOPCONST )
symbMangleResetAbbrev( )
id += ">"
'' Similar to symbAddProcPtr(), the descriptor type must be added to the
'' current scope, or to the toplevel namespace if at the toplevel.
'' It's not safe to always make it global because it may reference local
'' symbols through the arraydtype (e.g. an array with a local UDT as its
'' dtype), that may end up being deleted before the global.
attrib = FB_SYMBATTRIB_NONE
pattrib = FB_PROCATTRIB_NONE
sym = symbLookupInternallyMangledSubtype( id, NULL, attrib, pattrib, NULL, symtb, hashtb )
if( sym ) then
return sym
end if
''
'' Create new descriptor type (it's a simple struct, no methods or
'' anything, so it should be ok to do this in any context, even
'' recursively when array descriptor fields are added)
''
'' Mark it with FB_SYMBATTRIB_DESCRIPTOR so it can be identified as
'' descriptor type, e.g. in hMangleUdtId().
''
attrib or= FB_SYMBATTRIB_DESCRIPTOR
''
'' If it is a struct and also a simple local struct, then mark the descriptor as local too
if( (arraydtype = FB_DATATYPE_STRUCT) andalso _
(symbIsStruct(arraySubType)) andalso _
(symbIsLocal(arraySubType)) ) then
attrib or= FB_SYMBATTRIB_LOCAL
end if
sym = symbStructBegin( symtb, hashtb, NULL, id, aliasid, FALSE, 0, FALSE, attrib, FB_SYMBOPT_PRESERVECASE )
assert( sym )
dTB(0).lower = 0
dTB(0).upper = 0
symbAddField( sym, "data", 0, dTB(), typeAddrOf( arraydtype ), arraysubtype, 0, 0, 0 )
symbAddField( sym, "ptr", 0, dTB(), typeAddrOf( arraydtype ), arraysubtype, 0, 0, 0 )
symbAddField( sym, "size", 0, dTB(), FB_DATATYPE_INTEGER, NULL, 0, 0, 0 )
symbAddField( sym, "element_len", 0, dTB(), FB_DATATYPE_INTEGER, NULL, 0, 0, 0 )
symbAddField( sym, "dimensions", 0, dTB(), FB_DATATYPE_INTEGER, NULL, 0, 0, 0 )
symbAddField( sym, "flags", 0, dTB(), FB_DATATYPE_INTEGER, NULL, 0, 0, 0 )
'' If dimensions are unknown, the descriptor type must have room for
'' FB_MAXARRAYDIMS
if( dimensions = -1 ) then
dimensions = FB_MAXARRAYDIMS
end if
assert( (dimensions >= 1) and (dimensions <= FB_MAXARRAYDIMS) )
dTB(0).lower = 0
dTB(0).upper = dimensions-1
symbAddField( sym, "dimTB", 1, dTB(), FB_DATATYPE_STRUCT, symb.fbarraydim, 0, 0, 0 )
symbStructEnd( sym )
function = sym
end function
function symbAddArrayDesc( byval array as FBSYMBOL ptr ) as FBSYMBOL ptr
static as string tempid
dim as zstring ptr id = any, id_alias = any
dim as FBSYMBOL ptr desc = any, desctype = any
dim as FB_SYMBATTRIB attrib = any
dim as FBSYMBOLTB ptr symtb = any
dim as integer isdynamic = any, ispubext = any, stats = any
function = NULL
id_alias = NULL
stats = 0
'' field?
if( symbIsField( array ) ) then
id = symbUniqueId( )
'' Only store an alias if in BASIC mangling
if( array->mangling <> FB_MANGLING_BASIC ) then
id_alias = id
end if
attrib = FB_SYMBATTRIB_LOCAL
stats = FB_SYMBSTATS_IMPLICIT
'' can't be ever static, the address has to be always
'' calculated at runtime
'' var_..
else
isdynamic = symbIsDynamic( array )
ispubext = (array->attrib and (FB_SYMBATTRIB_PUBLIC or FB_SYMBATTRIB_EXTERN)) <> 0
'' common or dynamic? Use the array's name/alias for the
'' descriptor, allowing it to be a from other modules if
'' Extern, and making debugging nicer.
if( symbIsCommon( array ) or isdynamic ) then
id = array->id.name
id_alias = array->id.alias
'' Preserve FB_SYMBSTATS_HASALIAS stat too
stats = array->stats and FB_SYMBSTATS_HASALIAS
'' If the array had a type suffix, there may be others
'' with the same id. We must prevent their descriptors
'' from colliding with each-other (it probably only
'' matters for -gen gcc), and since we can't preserve
'' FB_SYMBATTRIB_SUFFIXED on the descriptor (it has a
'' different dtype than the array anyways), we must do
'' that part of the mangling here because
'' hMangleVariable() won't do it.
if( symbIsSuffixed( array ) ) then
tempid = *id
tempid += *hMangleBuiltInType( symbGetType( array ) )
select case env.clopt.backend
case FB_BACKEND_GCC, FB_BACKEND_CLANG
tempid += "$"
end select
id = strptr( tempid )
end if
'' otherwise, create a temporary name for the descriptor,
'' as it will be used privately only, and must co-exist with
'' the static array symbol.
else
id = symbUniqueId( )
'' Only store an alias if in BASIC mangling
if( array->mangling <> FB_MANGLING_BASIC ) then
id_alias = id
end if
stats = FB_SYMBSTATS_IMPLICIT
end if
attrib = array->attrib and (FB_SYMBATTRIB_SHARED or _
FB_SYMBATTRIB_COMMON or _
FB_SYMBATTRIB_STATIC or _
FB_SYMBATTRIB_EXTERN or _
FB_SYMBATTRIB_PUBLIC or _
FB_SYMBATTRIB_LOCAL)
'' not dynamic?
if( isdynamic = FALSE ) then
'' extern? always emit the descriptor (if accessed),
'' because the original one won't be accessible (or may don't
'' exist, if it's a "C" extern array)
if( symbIsExtern( array ) ) then
attrib and= not FB_SYMBATTRIB_EXTERN
end if
'' if not-dynamic, the descriptor can't be ever public
attrib and= not FB_SYMBATTRIB_PUBLIC
end if
end if
attrib or= FB_SYMBATTRIB_DESCRIPTOR
'' field?
if( symbIsField( array ) ) then
'' if at mod-level, it can't be static, alloc on main()'s stack
if( parser.scope = FB_MAINSCOPE ) then
symtb = @symbGetProcSymbTb( parser.currproc )
'' otherwise, let newSymbol() set it, we could be inside an
'' scope block (ie: a var initializer)
else
symtb = NULL
end if
'' use the same symb tb as the array
else
symtb = array->symtb
end if
desctype = symbAddArrayDescriptorType( symbGetArrayDimensions( array ), array->typ, array->subtype )
desc = symbNewSymbol( FB_SYMBOPT_PRESERVECASE, NULL, symtb, NULL, _
FB_SYMBCLASS_VAR, id, id_alias, _
FB_DATATYPE_STRUCT, desctype, attrib, FB_PROCATTRIB_NONE )
if( desc = NULL ) then
exit function
end if
desc->lgt = symbGetSizeOf( desctype )
desc->ofs = 0
desc->stats = stats or (array->stats and FB_SYMBSTATS_ACCESSED)
symbVarInitFields( desc )
desc->var_.desc.array = array '' back link
function = desc
end function
private sub symbDropArrayDims( byval s as FBSYMBOL ptr )
deallocate( s->var_.array.dimtb )
s->var_.array.dimtb = NULL
end sub
private sub symbRecalcArrayDiff( byval sym as FBSYMBOL ptr )
dim as longint diff = any, elements = any
dim as integer last = any
dim as FBARRAYDIM ptr dimtb = any
assert( symbIsDynamic( sym ) = FALSE )
assert( symbGetArrayDimensions( sym ) > 0 )
dimtb = sym->var_.array.dimtb
diff = 0
last = sym->var_.array.dimensions - 1
for i as integer = 0 to last - 1
elements = (dimtb[i+1].upper - dimtb[i+1].lower) + 1
diff = (diff + dimtb[i].lower) * elements
next
diff = (diff + dimtb[last].lower) * sym->lgt
sym->var_.array.diff = -diff
end sub
private sub symbRecalcArrayDiffAndElements( byval sym as FBSYMBOL ptr )
assert( symbGetIsDynamic( sym ) = FALSE )
if( symbGetArrayDimensions( sym ) > 0 ) then
symbRecalcArrayDiff( sym )
sym->var_.array.elements = symbCalcArrayElements( sym, 0 )
else
sym->var_.array.diff = 0
sym->var_.array.elements = 1
end if
end sub
function symbArrayHasUnknownBounds( byval sym as FBSYMBOL ptr ) as integer
assert( symbIsVar( sym ) or symbIsField( sym ) )
function = FALSE
if( symbGetIsDynamic( sym ) ) then
exit function
end if
for i as integer = 0 to symbGetArrayDimensions( sym ) - 1
if( symbArrayUbound( sym, i ) = FB_ARRAYDIM_UNKNOWN ) then
return TRUE
end if
next
end function
''
'' Allocate the companion array descriptor for VARs, if there are no more
'' FB_ARRAYDIM_UNKNOWN's in the dimensions. VARs are "unique", they map to
'' runtime memory 1:1, it's ok to have exactly 1 descriptor per VAR.
''
'' But not for FIELDs, because they are not unique: There can be many instances
'' of a field in memory at runtime, so there is no single descriptor per FIELD
'' symbol. Instead, astNewARG() has to call symbAddArrayDesc() on-demand and
'' create a new descriptor everytime. And dynamic array fields have their own
'' descriptor associated anyways.
''
sub symbMaybeAddArrayDesc( byval sym as FBSYMBOL ptr )
assert( symbIsField( sym ) = FALSE )
assert( symbIsParamVarBydesc( sym ) = FALSE )
if( symbGetArrayDimensions( sym ) = 0 ) then
exit sub
end if
if( symbArrayHasUnknownBounds( sym ) ) then
'' Not yet; there still are unknown array dimensions
exit sub
end if
if( sym->var_.array.desc = NULL ) then
sym->var_.array.desc = symbAddArrayDesc( sym )
sym->var_.array.desctype = sym->var_.array.desc->subtype
sym->var_.array.desc->var_.initree = _
astBuildArrayDescIniTree( sym->var_.array.desc, sym, NULL )
end if
end sub
sub symbSetArrayDimTb _
( _
byval sym as FBSYMBOL ptr, _
byval dimensions as integer, _
dTB() as FBARRAYDIM _
)
assert( symbGetIsDynamic( sym ) = FALSE )
assert( dimensions > 0 )
'' Delete existing dimensions, if any
symbDropArrayDims( sym )
sym->var_.array.dimensions = dimensions
'' Copy the dTB() into the FBSYMBOL
sym->var_.array.dimtb = xallocate( dimensions * sizeof( FBARRAYDIM ) )
for i as integer = 0 to dimensions - 1
sym->var_.array.dimtb[i] = dTB(i)
next
symbRecalcArrayDiffAndElements( sym )
end sub
sub symbSetFixedSizeArrayDimensionElements _
( _
byval sym as FBSYMBOL ptr, _
byval dimension as integer, _
byval elements as longint _
)
assert( symbGetIsDynamic( sym ) = FALSE )
assert( (dimension >= 0) and (dimension < symbGetArrayDimensions( sym )) )
with( symbGetArrayDim( sym, dimension ) )
.upper = .lower + elements - 1
end with
symbRecalcArrayDiffAndElements( sym )
end sub
'' Show a compile-time error if a dynamic array access or REDIM has a wrong
'' number of dimensions. This can only be done for dynamic arrays for which we
'' know the dimension count.
sub symbCheckDynamicArrayDimensions( byval sym as FBSYMBOL ptr, byval dimensions as integer )
'' Secondary declarations with dimensions = -1 don't make a difference.
'' (e.g. secondary REDIMs)
if( dimensions = -1 ) then
exit sub
end if
var existingdimensions = symbGetArrayDimensions( sym )
if( (existingdimensions <> -1) and (existingdimensions <> dimensions) ) then
errReportEx( FB_ERRMSG_WRONGDIMENSIONS, symbGetName( sym ) )
end if
end sub
sub symbVarInitFields( byval sym as FBSYMBOL ptr )
sym->var_.initree = NULL
sym->var_.array.dimensions = 0
sym->var_.array.dimtb = NULL
sym->var_.array.diff = 0
sym->var_.array.elements = 1
sym->var_.array.desc = NULL
sym->var_.array.desctype = NULL
sym->var_.desc.array = NULL
sym->var_.stmtnum = parser.stmt.cnt
sym->var_.align = 0
sym->var_.data.prev = NULL
sym->var_.bitpos = 0
sym->var_.bits = 0
end sub
sub symbVarInitArrayDimensions _
( _
byval sym as FBSYMBOL ptr, _
byval dimensions as integer, _
dTB() as FBARRAYDIM _
)
if( dimensions <> 0 ) then
if( symbGetIsDynamic( sym ) ) then
sym->var_.array.dimensions = dimensions
else
symbSetArrayDimTb( sym, dimensions, dTB() )
end if
if( (not symbIsField( sym )) and (not symbIsParamVarBydesc( sym )) ) then
symbMaybeAddArrayDesc( sym )
end if
end if
end sub
function symbAddVar _
( _
byval id as const zstring ptr, _
byval id_alias as const zstring ptr, _
byval dtype as integer, _
byval subtype as FBSYMBOL ptr, _
byval lgt as longint, _
byval dimensions as integer, _
dTB() as FBARRAYDIM, _
byval attrib as FB_SYMBATTRIB, _
byval options as FB_SYMBOPT _
) as FBSYMBOL ptr
dim as FBSYMBOL ptr s = any
dim as FBSYMBOLTB ptr symtb = any
dim as FBHASHTB ptr hashtb = any
dim as integer isglobal = any, stats = any
function = NULL
''
isglobal = (attrib and (FB_SYMBATTRIB_PUBLIC or _
FB_SYMBATTRIB_EXTERN or _
FB_SYMBATTRIB_SHARED or _
FB_SYMBATTRIB_COMMON)) <> 0
''
if( lgt <= 0 ) then
lgt = symbCalcLen( dtype, subtype )
end if
'' no explict alias?
if( id_alias = NULL ) then
'' only preserve a case-sensitive version if in BASIC mangling
if( parser.mangling <> FB_MANGLING_BASIC ) then
id_alias = id
end if
stats = 0
else
stats = FB_SYMBSTATS_HASALIAS
end if
'' local? add to local symb & hash tbs
if( isglobal = FALSE ) then
attrib or= FB_SYMBATTRIB_LOCAL
'' QB quirk: implicit variables are moved to the function scope..
if( (options and FB_SYMBOPT_UNSCOPE) = 0 ) then
symtb = symb.symtb
else
symtb = @symbGetProcSymbTb( parser.currproc )
end if
hashtb = symb.hashtb
'' can't add local static vars to global list because
'' symbDelSymbolTb() will miss them when flushing the
'' proc/scope block, and also because the GDB info
'' global..
else
symtb = @symbGetGlobalTb( )
hashtb = @symbGetGlobalHashTb( )
'' inside a namespace?
if( symbIsGlobalNamespc( ) = FALSE ) then
'' respect namespaces?
if( (options and FB_SYMBOPT_MOVETOGLOB) = 0 ) then
symtb = @symbGetCompSymbTb( symbGetCurrentNamespc( ) )
hashtb = @symbGetCompHashTb( symbGetCurrentNamespc( ) )
end if
end if
end if
s = symbNewSymbol( options or FB_SYMBOPT_DOHASH, NULL, symtb, hashtb, _
FB_SYMBCLASS_VAR, id, id_alias, dtype, subtype, attrib, FB_PROCATTRIB_NONE )
if( s = NULL ) then
exit function
end if
s->stats or= stats
s->lgt = lgt
s->ofs = 0
symbVarInitFields( s )
symbVarInitArrayDimensions( s, dimensions, dTB() )
'' QB quirk: see above
if( (options and FB_SYMBOPT_UNSCOPE) <> 0 ) then
s->var_.stmtnum = parser.currproc->proc.ext->stmtnum + 1
'' move to global?
elseif( (options and FB_SYMBOPT_MOVETOGLOB) <> 0 ) then
s->scope = FB_MAINSCOPE
end if
'' Static member var using parent UDT as dtype? Length must be
'' recalculated later, when UDT was fully parsed...
if( symbIsParentNamespace( symbGetType( s ), s->subtype ) ) then
symbSetUdtHasRecByvalRes( subtype )
end if
function = s
end function
'' For implicit variables that should live only in the current statement
'' - Will be marked with FB_SYMBATTRIB_TEMP, so it will not be destroyed
'' at scope breaks, but instead via the AST's dtor list and astAdd()
'' - Using a unique id, to avoid conflicts with the user's code
'' - As a positive side-effect, creating the var will always succeed
'' - FB_SYMBOPT_UNSCOPE is allowed, probably to ensure that temp vars used in
'' the user's var initializers will be unscoped just like the user's vars and
'' their initializers
'' - Temp vars should never be made STATIC automatically, e.g. due to
'' symbGetProcStaticLocals(), because they are hidden to the user, and making
'' them STATIC could cause hidden bugs with recursion or multi-threading.
function symbAddTempVar _
( _
byval dtype as integer, _
byval subtype as FBSYMBOL ptr _
) as FBSYMBOL ptr
static as FBARRAYDIM dTB(0)
dim as FB_SYMBOPT options = FB_SYMBOPT_NONE
'' Cannot create temp z/wstrings this way, since the length is unknown
assert( typeGetDtAndPtrOnly( dtype ) <> FB_DATATYPE_CHAR )
assert( typeGetDtAndPtrOnly( dtype ) <> FB_DATATYPE_WCHAR )
if( fbLangOptIsSet( FB_LANG_OPT_SCOPE ) = FALSE ) then
options or= FB_SYMBOPT_UNSCOPE
end if
var sym = symbAddVar( symbUniqueId( ), NULL, dtype, subtype, 0, 0, _
dTB(), FB_SYMBATTRIB_TEMP, options )
symbSetIsImplicit( sym )
function = sym
end function
'' For implicit variables that should live in the current scope, longer than
'' just the current statement.
'' - not marked with FB_SYMBATTRIB_TEMP, so the var will be destroyed properly
'' at scope breaks in the current scope, and at the end of the current scope
'' - just like a user-defined variable, just with an auto-generated name
'' - ditto regarding symbGetProcStaticLocals()
function symbAddImplicitVar _
( _
byval dtype as integer, _
byval subtype as FBSYMBOL ptr, _
byval options as integer _
) as FBSYMBOL ptr
static as FBARRAYDIM dTB(0)
'' Cannot create temp z/wstrings this way, since the length is unknown
assert( typeGetDtAndPtrOnly( dtype ) <> FB_DATATYPE_CHAR )
assert( typeGetDtAndPtrOnly( dtype ) <> FB_DATATYPE_WCHAR )
var sym = symbAddVar( symbUniqueId( ), NULL, dtype, subtype, 0, 0, _
dTB(), 0, options )
symbSetIsImplicit( sym )
function = sym
end function
function symbAddAndAllocateTempVar( byval dtype as integer ) as FBSYMBOL ptr
dim as FBSYMBOL ptr s = any
s = symbAddTempVar( dtype )
assert( env.clopt.backend = FB_BACKEND_GAS )
irProcAllocLocal( parser.currproc, s )
function = s
end function
''::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'' misc
''::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function symbGetRealSize( byval sym as FBSYMBOL ptr ) as longint
assert( symbIsVar( sym ) or symbIsField( sym ) )
var size = iif( symbIsRef( sym ), env.pointersize, symbGetSizeOf( sym ) )
size *= symbGetArrayElements( sym )
function = size
end function
sub symbGetRealType( byval sym as FBSYMBOL ptr, byref dtype as integer, byref subtype as FBSYMBOL ptr )
assert( symbIsVar( sym ) or symbIsField( sym ) )
dtype = symbGetFullType( sym )
subtype = sym->subtype
if( symbIsParamVar( sym ) ) then
dim parammode as integer
dim bydescrealsubtype as FBSYMBOL ptr
if( symbIsParamVarByref( sym ) ) then
parammode = FB_PARAMMODE_BYREF
elseif( symbIsParamVarBydesc( sym ) ) then
parammode = FB_PARAMMODE_BYDESC
bydescrealsubtype = sym->var_.array.desctype
else
assert( symbIsParamVarByval( sym ) )
parammode = FB_PARAMMODE_BYVAL
end if
symbGetRealParamDtype( parammode, bydescrealsubtype, dtype, subtype )
elseif( symbIsRef( sym ) or symbIsImport( sym ) ) then
dtype = typeAddrOf( dtype )
end if
end sub
'' Calculate a static array's total number of elements, all dimensions together.
'' <first> may be specified to calculate only the elements for <first> and the
'' following dimensions.
function symbCalcArrayElements _
( _
byval sym as FBSYMBOL ptr, _
byval first as integer _
) as longint
dim as longint totalelements = any
totalelements = 1
for i as integer = first to sym->var_.array.dimensions - 1
totalelements *= (sym->var_.array.dimtb[i].upper - sym->var_.array.dimtb[i].lower) + 1
next
function = totalelements
end function
function symbCheckArraySize _
( _
byval dimensions as integer, _
byval dimtb as FBARRAYDIM ptr, _
byval lgt as longint, _
byval is_on_stack as integer _
) as integer
dim as ulongint allelements = any
dim as ulongint elements = any
dim as integer found_too_big = any
found_too_big = FALSE
allelements = 1
for i as integer = 0 to dimensions - 1
'' ellipsis upper bound?
if( dimtb[i].upper = FB_ARRAYDIM_UNKNOWN ) then
'' Each dimension using ellipsis will have at least 1 element,
'' this is what can be assumed for now. (The check will need
'' to be repeated later when the real size is known)
elements = 1
else
'' elements for this array dimension
elements = (dimtb[i].upper - dimtb[i].lower) + 1
end if
'' Too many elements in this dimension?
if( elements > &h7FFFFFFFu ) then
found_too_big = TRUE
exit for
end if
allelements *= elements
'' Too many elements overall after adding this dimension?
if( allelements > &h7FFFFFFFull ) then
found_too_big = TRUE
exit for
end if
next
if( found_too_big = FALSE ) then
'' Apply data type size
allelements *= lgt
if( allelements > &h7FFFFFFFull ) then
found_too_big = TRUE
end if
end if
if( found_too_big ) then
function = FALSE
else
if( is_on_stack and (allelements > culngint( env.clopt.stacksize )) ) then
errReportWarn( FB_WARNINGMSG_HUGEARRAYONSTACK )
end if
function = TRUE
end if
end function
function symbGetVarHasCtor( byval s as FBSYMBOL ptr ) as integer
'' shared, static, ref, param or temp?
if( (s->attrib and (FB_SYMBATTRIB_SHARED or _
FB_SYMBATTRIB_STATIC or _
FB_SYMBATTRIB_COMMON or _
FB_SYMBATTRIB_REF or _
FB_SYMBATTRIB_PARAMVARBYDESC or _
FB_SYMBATTRIB_PARAMVARBYVAL or _
FB_SYMBATTRIB_PARAMVARBYREF or _
FB_SYMBATTRIB_TEMP or _
FB_SYMBATTRIB_FUNCRESULT)) <> 0 ) then
return FALSE
end if
select case( symbGetType( s ) )
'' var-len string?
case FB_DATATYPE_STRING
return TRUE
'' wchar ptr marked as "dynamic wstring"?
case typeAddrOf( FB_DATATYPE_WCHAR )
if( symbGetIsTemporary( s ) ) then
return TRUE
end if
end select
'' array? dims can be -1 with "DIM foo()" arrays..
if( symbGetArrayDimensions( s ) <> 0 ) then
'' (note: it doesn't matter if it's dynamic array or not, local
'' non-dynamic array allocations will have to fill
'' the descriptor, so arrays can't be accessed before that)
return TRUE
end if
'' UDT var with ctor?
function = symbHasCtor( s )
end function
function symbGetVarHasDtor( byval s as FBSYMBOL ptr ) as integer
'' shared, static, ref, param or temporary?
if( (s->attrib and (FB_SYMBATTRIB_SHARED or _
FB_SYMBATTRIB_STATIC or _
FB_SYMBATTRIB_COMMON or _
FB_SYMBATTRIB_REF or _
FB_SYMBATTRIB_PARAMVARBYDESC or _
FB_SYMBATTRIB_PARAMVARBYVAL or _
FB_SYMBATTRIB_PARAMVARBYREF or _
FB_SYMBATTRIB_TEMP or _
FB_SYMBATTRIB_FUNCRESULT)) <> 0 ) then
return FALSE
end if
select case( symbGetType( s ) )
'' var-len string?
case FB_DATATYPE_STRING
return TRUE
'' wchar ptr marked as "dynamic wstring"?
case typeAddrOf( FB_DATATYPE_WCHAR )
if( symbGetIsTemporary(s) ) then
return TRUE
end if
end select
'' array? dims can be -1 with "DIM foo()" arrays..
if( symbGetArrayDimensions( s ) <> 0 ) then
'' dynamic?
if( symbIsDynamic( s ) ) then
return TRUE
end if
end if
'' UDT var with dtor?
function = symbHasDtor( s )
end function
function symbCloneVar( byval sym as FBSYMBOL ptr ) as FBSYMBOL ptr
static as FBARRAYDIM dTB(0 to FB_MAXARRAYDIMS-1)
'' assuming only temp vars or temp array descs will be cloned
if( symbIsDescriptor( sym ) ) then
function = symbAddArrayDesc( sym->var_.desc.array )
'' no need to dup desc.initree, it was flushed in newARG() and
'' should be fixed up with the new symbol in TypeIniFlush()
elseif( symbIsTemp( sym ) ) then
function = symbAddTempVar( symbGetType( sym ), symbGetSubType( sym ) )
else
if( symbIsDynamic( sym ) = FALSE ) then
'' Fill the dTB() with the array's dimensions, if any
for i as integer = 0 to symbGetArrayDimensions( sym ) - 1
dTB(i) = symbGetArrayDim( sym, i )
next
end if
function = symbAddVar( symbGetName( sym ), NULL, _
symbGetType( sym ), symbGetSubType( sym ), 0, _
symbGetArrayDimensions( sym ), dTB(), symbGetAttrib( sym ), 0 )
end if
end function
function symbVarCheckAccess( byval sym as FBSYMBOL ptr ) as integer
'' inside a proc?
if( fbIsModLevel( ) = FALSE ) then
'' local?
if( symbIsLocal( sym ) ) then
'' not a main()'s local?
if( symbGetScope( sym ) = FB_MAINSCOPE ) then
return FALSE
end if
'' not shared?
elseif( symbIsShared( sym ) = FALSE ) then
return FALSE
end if
end if
function = TRUE
end function
''::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'' del
''::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
sub symbDelVar( byval s as FBSYMBOL ptr, byval is_tbdel as integer )
symbDropArrayDims( s )
if( is_tbdel = FALSE ) then
'' del the array descriptor, recursively
if( s->var_.array.desc ) then
symbDelSymbol( s->var_.array.desc, FALSE )
end if
end if
if( symbGetIsLiteral( s ) ) then
if( typeGetDtAndPtrOnly( s->typ ) = FB_DATATYPE_WCHAR ) then
if( s->var_.littextw <> NULL ) then
WstrFree( s->var_.littextw )
end if
else
if( s->var_.littext <> NULL ) then
ZstrFree( s->var_.littext )
end if
end if
end if
'' Note: FBSYMBOL.var_.initree will be free'ed by astEnd() already
symbFreeSymbol( s )
end sub