-
-
Notifications
You must be signed in to change notification settings - Fork 594
/
symbol.d
2453 lines (2112 loc) · 65.9 KB
/
symbol.d
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
/**
* Compiler implementation of the
* $(LINK2 http://www.dlang.org, D programming language).
*
* Copyright: Copyright (C) 1984-1998 by Symantec
* Copyright (C) 2000-2018 by The D Language Foundation, All Rights Reserved
* Authors: $(LINK2 http://www.digitalmars.com, Walter Bright)
* License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
* Source: https://github.com/dlang/dmd/blob/master/src/dmd/backend/symbol.d
*/
module dmd.backend.symbol;
version (SCPP)
{
version = COMPILE;
version = SCPP_HTOD;
}
version (HTOD)
{
version = COMPILE;
version = SCPP_HTOD;
}
version (MARS)
{
version = COMPILE;
enum HYDRATE = false;
enum DEHYDRATE = false;
}
version (COMPILE)
{
import core.stdc.stdio;
import core.stdc.stdlib;
import core.stdc.string;
import dmd.backend.cdef;
import dmd.backend.cc;
import dmd.backend.cgcv;
import dmd.backend.dlist;
import dmd.backend.dt;
import dmd.backend.dvec;
import dmd.backend.el;
import dmd.backend.global;
import dmd.backend.memh;
import dmd.backend.oper;
import dmd.backend.ty;
import dmd.backend.type;
version (SCPP_HTOD)
{
import cpp;
import dtoken;
import scopeh;
import msgs2;
import parser;
import precomp;
extern (C++) void baseclass_free(baseclass_t *b);
}
extern (C++):
alias MEM_PH_MALLOC = mem_malloc;
alias MEM_PH_CALLOC = mem_calloc;
alias MEM_PH_FREE = mem_free;
alias MEM_PH_FREEFP = mem_freefp;
alias MEM_PH_STRDUP = mem_strdup;
alias MEM_PH_REALLOC = mem_realloc;
alias MEM_PARF_MALLOC = mem_malloc;
alias MEM_PARF_CALLOC = mem_calloc;
alias MEM_PARF_REALLOC = mem_realloc;
alias MEM_PARF_FREE = mem_free;
alias MEM_PARF_STRDUP = mem_strdup;
version (SCPP_HTOD)
enum mBP = 0x20;
else
import dmd.backend.code_x86;
void struct_free(struct_t *st) { }
func_t* func_calloc() { return cast(func_t *) calloc(1, func_t.sizeof); }
void func_free(func_t* f) { free(f); }
/*********************************
* Allocate/free symbol table.
*/
extern (C) Symbol **symtab_realloc(Symbol **tab, size_t symmax)
{ Symbol **newtab;
if (config.flags2 & (CFG2phgen | CFG2phuse | CFG2phauto | CFG2phautoy))
{
newtab = cast(Symbol **) MEM_PH_REALLOC(tab, symmax * (Symbol *).sizeof);
}
else
{
newtab = cast(Symbol **) realloc(tab, symmax * (Symbol *).sizeof);
if (!newtab)
err_nomem();
}
return newtab;
}
Symbol **symtab_malloc(size_t symmax)
{ Symbol **newtab;
if (config.flags2 & (CFG2phgen | CFG2phuse | CFG2phauto | CFG2phautoy))
{
newtab = cast(Symbol **) MEM_PH_MALLOC(symmax * (Symbol *).sizeof);
}
else
{
newtab = cast(Symbol **) malloc(symmax * (Symbol *).sizeof);
if (!newtab)
err_nomem();
}
return newtab;
}
Symbol **symtab_calloc(size_t symmax)
{ Symbol **newtab;
if (config.flags2 & (CFG2phgen | CFG2phuse | CFG2phauto | CFG2phautoy))
{
newtab = cast(Symbol **) MEM_PH_CALLOC(symmax * (Symbol *).sizeof);
}
else
{
newtab = cast(Symbol **) calloc(symmax, (Symbol *).sizeof);
if (!newtab)
err_nomem();
}
return newtab;
}
void symtab_free(Symbol **tab)
{
if (config.flags2 & (CFG2phgen | CFG2phuse | CFG2phauto | CFG2phautoy))
MEM_PH_FREE(tab);
else if (tab)
free(tab);
}
/*******************************
* Type out symbol information.
*/
void symbol_print(Symbol *s)
{
debug
{
version (COMPILE)
{
if (!s) return;
printf("symbol %p '%s'\n ",s,s.Sident.ptr);
printf(" Sclass = "); WRclass(cast(SC) s.Sclass);
printf(" Ssymnum = %d",s.Ssymnum);
printf(" Sfl = "); WRFL(cast(FL) s.Sfl);
printf(" Sseg = %d\n",s.Sseg);
// printf(" Ssize = x%02x\n",s.Ssize);
printf(" Soffset = x%04llx",cast(ulong)s.Soffset);
printf(" Sweight = %d",s.Sweight);
printf(" Sflags = x%04x",cast(uint)s.Sflags);
printf(" Sxtrnnum = %d\n",s.Sxtrnnum);
printf(" Stype = %p",s.Stype);
version (SCPP_HTOD)
{
printf(" Ssequence = %x", s.Ssequence);
printf(" Scover = %p", s.Scover);
}
printf(" Sl = %p",s.Sl);
printf(" Sr = %p\n",s.Sr);
if (s.Sscope)
printf(" Sscope = '%s'\n",s.Sscope.Sident.ptr);
if (s.Stype)
type_print(s.Stype);
if (s.Sclass == SCmember || s.Sclass == SCfield)
{
printf(" Smemoff =%5lld", cast(long)s.Smemoff);
printf(" Sbit =%3d",s.Sbit);
printf(" Swidth =%3d\n",s.Swidth);
}
version (SCPP_HTOD)
{
if (s.Sclass == SCstruct)
{
printf(" Svbptr = %p, Svptr = %p\n",s.Sstruct.Svbptr,s.Sstruct.Svptr);
}
}
}
}
}
/*********************************
* Terminate use of symbol table.
*/
private __gshared Symbol *keep;
void symbol_term()
{
symbol_free(keep);
}
/****************************************
* Keep symbol around until symbol_term().
*/
static if (TERMCODE)
{
void symbol_keep(Symbol *s)
{
symbol_debug(s);
s.Sr = keep; // use Sr so symbol_free() doesn't nest
keep = s;
}
}
/****************************************
* Return alignment of symbol.
*/
int Symbol_Salignsize(Symbol* s)
{
if (s.Salignment > 0)
return s.Salignment;
int alignsize = type_alignsize(s.Stype);
/* Reduce alignment faults when SIMD vectors
* are reinterpreted cast to other types with less alignment.
*/
if (config.fpxmmregs && alignsize < 16 &&
s.Sclass == SCauto &&
type_size(s.Stype) == 16)
{
alignsize = 16;
}
return alignsize;
}
/****************************************
* Aver if Symbol is not only merely dead, but really most sincerely dead.
* Params:
* anyInlineAsm = true if there's any inline assembler code
* Returns:
* true if symbol is dead.
*/
bool Symbol_Sisdead(Symbol* s, bool anyInlineAsm)
{
version (MARS)
enum vol = false;
else
enum vol = true;
return s.Sflags & SFLdead ||
/* SFLdead means the optimizer found no references to it.
* The rest deals with variables that the compiler never needed
* to read from memory because they were cached in registers,
* and so no memory needs to be allocated for them.
* Code that does write those variables to memory gets NOPed out
* during address assignment.
*/
(!anyInlineAsm && !(s.Sflags & SFLread) && s.Sflags & SFLunambig &&
// mTYvolatile means this variable has been reference by a nested function
(vol || !(s.Stype.Tty & mTYvolatile)) &&
(config.flags4 & CFG4optimized || !config.fulltypes));
}
/****************************************
* Determine if symbol needs a 'this' pointer.
*/
int Symbol_needThis(Symbol* s)
{
//printf("needThis() '%s'\n", Sident.ptr);
debug assert(isclassmember(s));
if (s.Sclass == SCmember || s.Sclass == SCfield)
return 1;
if (tyfunc(s.Stype.Tty) && !(s.Sfunc.Fflags & Fstatic))
return 1;
return 0;
}
/***********************************
* Get user name of symbol.
*/
char *symbol_ident(Symbol *s)
{
version (SCPP_HTOD)
{
__gshared char* noname = cast(char*)"__unnamed".ptr;
switch (s.Sclass)
{ case SCstruct:
if (s.Sstruct.Salias)
s = s.Sstruct.Salias;
else if (s.Sstruct.Sflags & STRnotagname)
return noname;
break;
case SCenum:
if (CPP)
{ if (s.Senum.SEalias)
s = s.Senum.SEalias;
else if (s.Senum.SEflags & SENnotagname)
return noname;
}
break;
case SCnamespace:
if (s.Sident[0] == '?' && s.Sident.ptr[1] == '%')
return cast(char*)"unique".ptr; // an unnamed namespace
break;
default:
break;
}
}
return s.Sident.ptr;
}
/****************************************
* Create a new symbol.
*/
Symbol * symbol_calloc(const(char)* id)
{
return symbol_calloc(id, cast(uint)strlen(id));
}
Symbol * symbol_calloc(const(char)* id, uint len)
{ Symbol *s;
//printf("sizeof(symbol)=%d, sizeof(s.Sident)=%d, len=%d\n",sizeof(symbol),sizeof(s.Sident),(int)len);
s = cast(Symbol *) mem_fmalloc(Symbol.sizeof - s.Sident.length + len + 1 + 5);
memset(s,0,Symbol.sizeof - s.Sident.length);
version (SCPP_HTOD)
{
s.Ssequence = pstate.STsequence;
pstate.STsequence += 1;
//if (s.Ssequence == 0x21) *cast(char*)0=0;
}
debug
{
if (debugy)
printf("symbol_calloc('%s') = %p\n",id,s);
s.id = Symbol.IDsymbol;
}
memcpy(s.Sident.ptr,id,len + 1);
s.Ssymnum = -1;
return s;
}
/****************************************
* Create a symbol, given a name and type.
*/
Symbol * symbol_name(const(char)* name,int sclass,type *t)
{
return symbol_name(name, cast(uint)strlen(name), sclass, t);
}
Symbol * symbol_name(const(char)* name, uint len, int sclass, type *t)
{
type_debug(t);
Symbol *s = symbol_calloc(name, len);
s.Sclass = cast(char) sclass;
s.Stype = t;
s.Stype.Tcount++;
if (tyfunc(t.Tty))
symbol_func(s);
return s;
}
/****************************************
* Create a symbol that is an alias to another function symbol.
*/
Funcsym *symbol_funcalias(Funcsym *sf)
{
Funcsym *s;
symbol_debug(sf);
assert(tyfunc(sf.Stype.Tty));
if (sf.Sclass == SCfuncalias)
sf = sf.Sfunc.Falias;
s = cast(Funcsym *)symbol_name(sf.Sident.ptr,SCfuncalias,sf.Stype);
s.Sfunc.Falias = sf;
version (SCPP_HTOD)
s.Scover = sf.Scover;
return s;
}
/****************************************
* Create a symbol, give it a name, storage class and type.
*/
Symbol * symbol_generate(int sclass,type *t)
{
__gshared int tmpnum;
char[4 + tmpnum.sizeof * 3 + 1] name;
//printf("symbol_generate(_TMP%d)\n", tmpnum);
sprintf(name.ptr,"_TMP%d",tmpnum++);
Symbol *s = symbol_name(name.ptr,sclass,t);
//symbol_print(s);
version (MARS)
s.Sflags |= SFLnodebug | SFLartifical;
return s;
}
/****************************************
* Generate an auto symbol, and add it to the symbol table.
*/
Symbol * symbol_genauto(type *t)
{ Symbol *s;
s = symbol_generate(SCauto,t);
version (SCPP_HTOD)
{
//printf("symbol_genauto(t) '%s'\n", s.Sident.ptr);
if (pstate.STdefertemps)
{ symbol_keep(s);
s.Ssymnum = -1;
}
else
{ s.Sflags |= SFLfree;
if (init_staticctor)
{ // variable goes into _STI_xxxx
s.Ssymnum = -1; // deferred allocation
//printf("test2\n");
//if (s.Sident[4] == '2') *(char*)0=0;
}
else
{
symbol_add(s);
}
}
}
else
{
s.Sflags |= SFLfree;
symbol_add(s);
}
return s;
}
/******************************************
* Generate symbol into which we can copy the contents of expression e.
*/
Symbol *symbol_genauto(elem *e)
{
return symbol_genauto(type_fake(e.Ety));
}
/******************************************
* Generate symbol into which we can copy the contents of expression e.
*/
Symbol *symbol_genauto(tym_t ty)
{
return symbol_genauto(type_fake(ty));
}
/****************************************
* Add in the variants for a function symbol.
*/
void symbol_func(Symbol *s)
{
//printf("symbol_func(%s, x%x)\n", s.Sident.ptr, fregsaved);
symbol_debug(s);
s.Sfl = FLfunc;
// Interrupt functions modify all registers
// BUG: do interrupt functions really save BP?
// Note that fregsaved may not be set yet
s.Sregsaved = (s.Stype && tybasic(s.Stype.Tty) == TYifunc) ? cast(regm_t) mBP : fregsaved;
s.Sseg = UNKNOWN; // don't know what segment it is in
if (!s.Sfunc)
s.Sfunc = func_calloc();
}
/***************************************
* Add a field to a struct s.
* Input:
* s the struct symbol
* name field name
* t the type of the field
* offset offset of the field
*/
void symbol_struct_addField(Symbol *s, const(char)* name, type *t, uint offset)
{
Symbol *s2 = symbol_name(name, SCmember, t);
s2.Smemoff = offset;
list_append(&s.Sstruct.Sfldlst, s2);
}
/********************************
* Define symbol in specified symbol table.
* Returns:
* pointer to symbol
*/
version (SCPP_HTOD)
{
Symbol * defsy(const(char)* p,Symbol **parent)
{
Symbol *s = symbol_calloc(p);
symbol_addtotree(parent,s);
return s;
}
}
/********************************
* Check integrity of symbol data structure.
*/
debug
{
void symbol_check(Symbol *s)
{
//printf("symbol_check('%s',%p)\n",s.Sident.ptr,s);
symbol_debug(s);
if (s.Stype) type_debug(s.Stype);
assert(cast(uint)s.Sclass < cast(uint)SCMAX);
version (SCPP_HTOD)
{
if (s.Sscope)
symbol_check(s.Sscope);
if (s.Scover)
symbol_check(s.Scover);
}
}
void symbol_tree_check(Symbol *s)
{
while (s)
{ symbol_check(s);
symbol_tree_check(s.Sl);
s = s.Sr;
}
}
}
/********************************
* Insert symbol in specified symbol table.
*/
version (SCPP_HTOD)
{
void symbol_addtotree(Symbol **parent,Symbol *s)
{ Symbol *rover;
byte cmp;
size_t len;
const(char)* p;
char c;
//printf("symbol_addtotree('%s',%p)\n",s.Sident.ptr,*parent);
debug
{
symbol_tree_check(*parent);
assert(!s.Sl && !s.Sr);
}
symbol_debug(s);
p = s.Sident.ptr;
c = *p;
len = strlen(p);
p++;
rover = *parent;
while (rover != null) // while we haven't run out of tree
{ symbol_debug(rover);
if ((cmp = cast(byte)(c - rover.Sident[0])) == 0)
{ cmp = cast(byte)memcmp(p,rover.Sident.ptr + 1,len); // compare identifier strings
if (cmp == 0) // found it if strings match
{
if (CPP)
{ Symbol *s2;
switch (rover.Sclass)
{ case SCstruct:
s2 = rover;
goto case_struct;
case_struct:
if (s2.Sstruct.Sctor &&
!(s2.Sstruct.Sctor.Sfunc.Fflags & Fgen))
cpperr(EM_ctor_disallowed,p); // no ctor allowed for class rover
s2.Sstruct.Sflags |= STRnoctor;
goto case_cover;
case_cover:
// Replace rover with the new symbol s, and
// have s 'cover' the tag symbol s2.
// BUG: memory leak on rover if s2!=rover
assert(!s2.Scover);
s.Sl = rover.Sl;
s.Sr = rover.Sr;
s.Scover = s2;
*parent = s;
rover.Sl = rover.Sr = null;
return;
case SCenum:
s2 = rover;
goto case_cover;
case SCtemplate:
s2 = rover;
s2.Stemplate.TMflags |= STRnoctor;
goto case_cover;
case SCalias:
s2 = rover.Smemalias;
if (s2.Sclass == SCstruct)
goto case_struct;
if (s2.Sclass == SCenum)
goto case_cover;
break;
default:
break;
}
}
synerr(EM_multiple_def,p - 1); // symbol is already defined
//symbol_undef(s); // undefine the symbol
return;
}
}
parent = (cmp < 0) ? /* if we go down left side */
&(rover.Sl) : /* then get left child */
&(rover.Sr); /* else get right child */
rover = *parent; /* get child */
}
/* not in table, so insert into table */
*parent = s; /* link new symbol into tree */
L1:
}
}
/*************************************
* Search for symbol in multiple symbol tables,
* starting with most recently nested one.
* Input:
* p . identifier string
* Returns:
* pointer to symbol
* null if couldn't find it
*/
static if (0)
{
Symbol * lookupsym(const(char)* p)
{
return scope_search(p,SCTglobal | SCTlocal);
}
}
/*************************************
* Search for symbol in symbol table.
* Input:
* p . identifier string
* rover . where to start looking
* Returns:
* pointer to symbol (null if not found)
*/
version (SCPP_HTOD)
{
Symbol * findsy(const(char)* p,Symbol *rover)
{
/+
#if TX86 && __DMC__
volatile int len;
__asm
{
#if !_WIN32
push DS
pop ES
#endif
mov EDI,p
xor AL,AL
mov BL,[EDI]
mov ECX,-1
repne scasb
not ECX
mov EDX,p
dec ECX
inc EDX
mov len,ECX
mov AL,BL
mov EBX,rover
mov ESI,EDX
test EBX,EBX
je L6
cmp AL,symbol.Sident[EBX]
js L2
lea EDI,symbol.Sident+1[EBX]
je L5
mov EBX,symbol.Sr[EBX]
jmp L3
L1: mov ECX,len
L2: mov EBX,symbol.Sl[EBX]
L3: test EBX,EBX
je L6
L4: cmp AL,symbol.Sident[EBX]
js L2
lea EDI,symbol.Sident+1[EBX]
je L5
mov EBX,symbol.Sr[EBX]
jmp L3
L5: rep cmpsb
mov ESI,EDX
js L1
je L6
mov EBX,symbol.Sr[EBX]
mov ECX,len
test EBX,EBX
jne L4
L6: mov EAX,EBX
}
#else
+/
size_t len;
byte cmp; /* set to value of strcmp */
char c = *p;
len = strlen(p);
p++; // will pick up 0 on memcmp
while (rover != null) // while we haven't run out of tree
{ symbol_debug(rover);
if ((cmp = cast(byte)(c - rover.Sident[0])) == 0)
{ cmp = cast(byte)memcmp(p,rover.Sident.ptr + 1,len); /* compare identifier strings */
if (cmp == 0)
return rover; /* found it if strings match */
}
rover = (cmp < 0) ? rover.Sl : rover.Sr;
}
return rover; // failed to find it
//#endif
}
}
/***********************************
* Create a new symbol table.
*/
version (SCPP_HTOD)
{
void createglobalsymtab()
{
assert(!scope_end);
if (CPP)
scope_push(null,cast(scope_fp)&findsy, SCTcglobal);
else
scope_push(null,cast(scope_fp)&findsy, SCTglobaltag);
scope_push(null,cast(scope_fp)&findsy, SCTglobal);
}
void createlocalsymtab()
{
assert(scope_end);
if (!CPP)
scope_push(null,cast(scope_fp)&findsy, SCTtag);
scope_push(null,cast(scope_fp)&findsy, SCTlocal);
}
/***********************************
* Delete current symbol table and back up one.
*/
void deletesymtab()
{ Symbol *root;
root = cast(Symbol *)scope_pop();
if (root)
{
if (funcsym_p)
list_prepend(&funcsym_p.Sfunc.Fsymtree,root);
else
symbol_free(root); // free symbol table
}
if (!CPP)
{
root = cast(Symbol *)scope_pop();
if (root)
{
if (funcsym_p)
list_prepend(&funcsym_p.Sfunc.Fsymtree,root);
else
symbol_free(root); // free symbol table
}
}
}
}
/*********************************
* Delete symbol from symbol table, taking care to delete
* all children of a symbol.
* Make sure there are no more forward references (labels, tags).
* Input:
* pointer to a symbol
*/
void meminit_free(meminit_t *m) /* helper for symbol_free() */
{
list_free(&m.MIelemlist,cast(list_free_fp)&el_free);
MEM_PARF_FREE(m);
}
void symbol_free(Symbol *s)
{
while (s) /* if symbol exists */
{ Symbol *sr;
debug
{
if (debugy)
printf("symbol_free('%s',%p)\n",s.Sident.ptr,s);
symbol_debug(s);
assert(/*s.Sclass != SCunde &&*/ cast(int) s.Sclass < cast(int) SCMAX);
}
{ type *t = s.Stype;
if (t)
type_debug(t);
if (t && tyfunc(t.Tty) && s.Sfunc)
{
func_t *f = s.Sfunc;
debug assert(f);
blocklist_free(&f.Fstartblock);
freesymtab(f.Flocsym.tab,0,f.Flocsym.top);
symtab_free(f.Flocsym.tab);
if (CPP)
{
if (f.Fflags & Fnotparent)
{ debug if (debugy) printf("not parent, returning\n");
return;
}
/* We could be freeing the symbol before it's class is */
/* freed, so remove it from the class's field list */
if (f.Fclass)
{ list_t tl;
symbol_debug(f.Fclass);
tl = list_inlist(f.Fclass.Sstruct.Sfldlst,s);
if (tl)
list_setsymbol(tl, null);
}
if (f.Foversym && f.Foversym.Sfunc)
{ f.Foversym.Sfunc.Fflags &= ~Fnotparent;
f.Foversym.Sfunc.Fclass = null;
symbol_free(f.Foversym);
}
if (f.Fexplicitspec)
symbol_free(f.Fexplicitspec);
/* If operator function, remove from list of such functions */
if (f.Fflags & Foperator)
{ assert(f.Foper && f.Foper < OPMAX);
//if (list_inlist(cpp_operfuncs[f.Foper],s))
// list_subtract(&cpp_operfuncs[f.Foper],s);
}
list_free(&f.Fclassfriends,FPNULL);
list_free(&f.Ffwdrefinstances,FPNULL);
param_free(&f.Farglist);
param_free(&f.Fptal);
list_free(&f.Fexcspec,cast(list_free_fp)&type_free);
version (SCPP_HTOD)
token_free(f.Fbody);
el_free(f.Fbaseinit);
if (f.Fthunk && !(f.Fflags & Finstance))
MEM_PH_FREE(f.Fthunk);
list_free(&f.Fthunks,cast(list_free_fp)&symbol_free);
}
list_free(&f.Fsymtree,cast(list_free_fp)&symbol_free);
free(f.typesTable);
func_free(f);
}
switch (s.Sclass)
{
version (SCPP_HTOD)
{
case SClabel:
if (!s.Slabel)
synerr(EM_unknown_label,s.Sident.ptr);
break;
}
case SCstruct:
version (SCPP_HTOD)
{
if (CPP)
{
struct_t *st = s.Sstruct;
assert(st);
list_free(&st.Sclassfriends,FPNULL);
list_free(&st.Sfriendclass,FPNULL);
list_free(&st.Sfriendfuncs,FPNULL);
list_free(&st.Scastoverload,FPNULL);
list_free(&st.Sopoverload,FPNULL);
list_free(&st.Svirtual,&MEM_PH_FREEFP);
list_free(&st.Sfldlst,FPNULL);
symbol_free(st.Sroot);
baseclass_t* b,bn;
for (b = st.Sbase; b; b = bn)
{ bn = b.BCnext;
list_free(&b.BCpublics,FPNULL);
baseclass_free(b);
}
for (b = st.Svirtbase; b; b = bn)
{ bn = b.BCnext;
baseclass_free(b);
}
for (b = st.Smptrbase; b; b = bn)
{ bn = b.BCnext;
list_free(&b.BCmptrlist,&MEM_PH_FREEFP);
baseclass_free(b);
}
for (b = st.Svbptrbase; b; b = bn)
{ bn = b.BCnext;
baseclass_free(b);
}
param_free(&st.Sarglist);
param_free(&st.Spr_arglist);
struct_free(st);
}
}
if (!CPP)
{
debug if (debugy)
printf("freeing members %p\n",s.Sstruct.Sfldlst);
list_free(&s.Sstruct.Sfldlst,FPNULL);
symbol_free(s.Sstruct.Sroot);
struct_free(s.Sstruct);
}
static if (0) /* Don't complain anymore about these, ANSI C says */
{
/* it's ok */
if (t && t.Tflags & TFsizeunknown)
synerr(EM_unknown_tag,s.Sident.ptr);
}