/
symbol.d
1548 lines (1347 loc) · 42.6 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
/**
* Symbols for the back end
*
* Compiler implementation of the
* $(LINK2 https://www.dlang.org, D programming language).
*
* Copyright: Copyright (C) 1984-1998 by Symantec
* Copyright (C) 2000-2024 by The D Language Foundation, All Rights Reserved
* Authors: $(LINK2 https://www.digitalmars.com, Walter Bright)
* License: $(LINK2 https://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;
enum HYDRATE = false;
enum DEHYDRATE = false;
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.mem;
import dmd.backend.oper;
import dmd.backend.symtab;
import dmd.backend.ty;
import dmd.backend.type;
nothrow:
@safe:
import dmd.backend.code_x86;
void struct_free(struct_t *st) { }
@trusted @nogc
func_t* func_calloc()
{
func_t* f = cast(func_t *) calloc(1, func_t.sizeof);
if (!f)
err_nomem();
return f;
}
@trusted
void func_free(func_t* f) { free(f); }
/*******************************
* Type out symbol information.
*/
@trusted
void symbol_print(const Symbol *s)
{
debug
{
if (!s) return;
printf("symbol %p '%s'\n ",s,s.Sident.ptr);
printf(" Sclass = %s ", class_str(s.Sclass));
printf(" Ssymnum = %d",cast(int)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);
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 == SC.member || s.Sclass == SC.field)
{
printf(" Smemoff =%5lld", cast(long)s.Smemoff);
printf(" Sbit =%3d",s.Sbit);
printf(" Swidth =%3d\n",s.Swidth);
}
}
}
/*********************************
* Terminate use of symbol table.
*/
private __gshared Symbol *keep;
@trusted
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.
*/
@trusted
int Symbol_Salignsize(ref 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 == SC.auto_ &&
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.
*/
@trusted
bool Symbol_Sisdead(const ref Symbol s, bool anyInlineAsm)
{
enum vol = false;
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.
*/
@trusted
int Symbol_needThis(const ref Symbol s)
{
//printf("needThis() '%s'\n", Sident.ptr);
debug assert(isclassmember(&s));
if (s.Sclass == SC.member || s.Sclass == SC.field)
return 1;
if (tyfunc(s.Stype.Tty) && !(s.Sfunc.Fflags & Fstatic))
return 1;
return 0;
}
/************************************
* Determine if `s` may be affected if an assignment is done through
* a pointer.
* Params:
* s = symbol to check
* Returns:
* true if it may be modified by assignment through a pointer
*/
bool Symbol_isAffected(const ref Symbol s)
{
//printf("s: %s %d\n", s.Sident.ptr, !(s.Sflags & SFLunambig) && !(s.ty() & (mTYconst | mTYimmutable)));
//symbol_print(s);
/* If nobody took its address and it's not statically allocated,
* then it is not accessible via pointer and so is not affected.
*/
if (s.Sflags & SFLunambig)
return false;
/* If it's immutable, it can't be affected.
*
* Disable this check because:
* 1. Symbol_isAffected is not used by copyprop() and should be.
* 2. Non-@safe functions can temporarilly cast away immutable.
* 3. Need to add an @safe flag to funcsym_p to enable this.
* 4. Const can be mutated by a separate view.
* Address this in a separate PR.
*/
static if (0)
if (s.ty() & (mTYconst | mTYimmutable))
{
/* Disabled for the moment because even @safe functions
* may have inlined unsafe code from other functions
*/
if (funcsym_p.Sfunc.Fflags3 & F3safe &&
s.ty() & mTYimmutable)
{
return false;
}
}
return true;
}
/***********************************
* Get user name of symbol.
*/
@trusted
const(char)* symbol_ident(const Symbol *s)
{
return s.Sident.ptr;
}
/****************************************
* Create a new symbol.
*/
@trusted @nogc
extern (C)
Symbol * symbol_calloc(const(char)[] id)
{
//printf("sizeof(symbol)=%d, sizeof(s.Sident)=%d, len=%d\n", symbol.sizeof, s.Sident.sizeof, cast(int)id.length);
Symbol* s = cast(Symbol *) mem_fmalloc(Symbol.sizeof - Symbol.Sident.length + id.length + 1 + 5);
memset(s,0,Symbol.sizeof - s.Sident.length);
memcpy(s.Sident.ptr,id.ptr,id.length);
s.Sident.ptr[id.length] = 0;
s.Ssymnum = SYMIDX.max;
if (debugy)
printf("symbol_calloc('%s') = %p\n",s.Sident.ptr,s);
debug s.id = Symbol.IDsymbol;
return s;
}
/****************************************
* Create a Symbol
* Params:
* name = name to give the Symbol
* type = type for the Symbol
* Returns:
* created Symbol
*/
@trusted @nogc
extern (C)
Symbol * symbol_name(const(char)[] name, SC sclass, type *t)
{
type_debug(t);
Symbol *s = symbol_calloc(name);
s.Sclass = 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.
*/
@trusted
Funcsym *symbol_funcalias(Funcsym *sf)
{
symbol_debug(sf);
assert(tyfunc(sf.Stype.Tty));
if (sf.Sclass == SC.funcalias)
sf = sf.Sfunc.Falias;
auto s = cast(Funcsym *)symbol_name(sf.Sident.ptr[0 .. strlen(sf.Sident.ptr)],SC.funcalias,sf.Stype);
s.Sfunc.Falias = sf;
return s;
}
/****************************************
* Create a symbol, give it a name, storage class and type.
*/
@trusted @nogc
Symbol * symbol_generate(SC sclass,type *t)
{
__gshared int tmpnum;
char[4 + tmpnum.sizeof * 3 + 1] name;
//printf("symbol_generate(_TMP%d)\n", tmpnum);
const length = snprintf(name.ptr,name.length,"_TMP%d",tmpnum++);
Symbol *s = symbol_name(name.ptr[0 .. length],sclass,t);
//symbol_print(s);
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(SC.auto_,t);
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.
*/
@trusted @nogc
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.
* Params:
* s = the struct symbol
* name = field name
* t = the type of the field
* offset = offset of the field
*/
@trusted
void symbol_struct_addField(Symbol *s, const(char)* name, type *t, uint offset)
{
Symbol *s2 = symbol_name(name[0 .. strlen(name)], SC.member, t);
s2.Smemoff = offset;
list_append(&s.Sstruct.Sfldlst, s2);
}
/***************************************
* Add a bit field to a struct s.
* Params:
* s = the struct symbol
* name = field name
* t = the type of the field
* offset = offset of the field
* fieldWidth = width of bit field
* bitOffset = bit number of start of field
*/
@trusted
void symbol_struct_addBitField(Symbol *s, const(char)* name, type *t, uint offset, uint fieldWidth, uint bitOffset)
{
//printf("symbol_struct_addBitField() s: %s\n", s.Sident.ptr);
Symbol *s2 = symbol_name(name[0 .. strlen(name)], SC.field, t);
s2.Smemoff = offset;
s2.Swidth = cast(ubyte)fieldWidth;
s2.Sbit = cast(ubyte)bitOffset;
list_append(&s.Sstruct.Sfldlst, s2);
symbol_struct_hasBitFields(s);
}
/***************************************
* Mark struct s as having bit fields
* Params:
* s = the struct symbol
*/
@trusted
void symbol_struct_hasBitFields(Symbol *s)
{
s.Sstruct.Sflags |= STRbitfields;
}
/***************************************
* Add a base class to a struct s.
* Input:
* s the struct/class symbol
* t the type of the base class
* offset offset of the base class in the struct/class
*/
@trusted
void symbol_struct_addBaseClass(Symbol *s, type *t, uint offset)
{
assert(t && t.Tty == TYstruct);
auto bc = cast(baseclass_t*)mem_fmalloc(baseclass_t.sizeof);
bc.BCbase = t.Ttag;
bc.BCoffset = offset;
bc.BCnext = s.Sstruct.Sbase;
s.Sstruct.Sbase = bc;
}
/********************************
* Check integrity of symbol data structure.
*/
debug
{
void symbol_check(const 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);
}
void symbol_tree_check(const(Symbol)* s)
{
while (s)
{ symbol_check(s);
symbol_tree_check(s.Sl);
s = s.Sr;
}
}
}
/*************************************
* 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);
}
}
/*********************************
* 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
*/
@trusted
void meminit_free(meminit_t *m) /* helper for symbol_free() */
{
list_free(&m.MIelemlist,cast(list_free_fp)&el_free);
mem_free(m);
}
@trusted
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 != SC.unde &&*/ 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[].ptr,0,f.Flocsym.length);
f.Flocsym.dtor();
if (CPP)
{
if (f.Fflags & Fnotparent)
{ debug if (debugy) printf("not parent, returning\n");
return;
}
/* We could be freeing the symbol before its 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);
el_free(f.Fbaseinit);
if (f.Fthunk && !(f.Fflags & Finstance))
mem_free(f.Fthunk);
list_free(&f.Fthunks,cast(list_free_fp)&symbol_free);
}
list_free(&f.Fsymtree,cast(list_free_fp)&symbol_free);
f.typesTable.dtor();
func_free(f);
}
switch (s.Sclass)
{
case SC.struct_:
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);
}
break;
case SC.enum_:
/* The actual member symbols are either in a local */
/* table or on the member list of a class, so we */
/* don't free them here. */
assert(s.Senum);
list_free(&s.Senum.SEenumlist,FPNULL);
mem_free(s.Senum);
s.Senum = null;
break;
case SC.parameter:
case SC.regpar:
case SC.fastpar:
case SC.shadowreg:
case SC.register:
case SC.auto_:
vec_free(s.Srange);
static if (0)
{
goto case SC.const_;
case SC.const_:
if (s.Sflags & (SFLvalue | SFLdtorexp))
el_free(s.Svalue);
}
break;
default:
break;
}
if (s.Sflags & (SFLvalue | SFLdtorexp))
el_free(s.Svalue);
if (s.Sdt)
dt_free(s.Sdt);
type_free(t);
symbol_free(s.Sl);
sr = s.Sr;
debug
{
s.id = 0;
}
mem_ffree(s);
}
s = sr;
}
}
/********************************
* Undefine a symbol.
* Assume error msg was already printed.
*/
static if (0)
{
private void symbol_undef(Symbol *s)
{
s.Sclass = SC.unde;
s.Ssymnum = SYMIDX.max;
type_free(s.Stype); /* free type data */
s.Stype = null;
}
}
/*****************************
* Add symbol to current symbol array.
*/
@trusted
SYMIDX symbol_add(Symbol *s)
{
return symbol_add(*cstate.CSpsymtab, s);
}
@trusted
SYMIDX symbol_add(ref symtab_t symtab, Symbol* s)
{
//printf("symbol_add('%s')\n", s.Sident.ptr);
debug
{
if (!s || !s.Sident[0])
{ printf("bad symbol\n");
assert(0);
}
}
symbol_debug(s);
if (pstate.STinsizeof)
{ symbol_keep(s);
return SYMIDX.max;
}
const sitop = symtab.length;
symtab.setLength(sitop + 1);
symtab[sitop] = s;
debug if (debugy)
printf("symbol_add(%p '%s') = %d\n",s,s.Sident.ptr, cast(int) symtab.length);
debug if (s.Ssymnum != SYMIDX.max)
printf("symbol %s already added\n", s.Sident.ptr);
assert(s.Ssymnum == SYMIDX.max);
s.Ssymnum = sitop;
return sitop;
}
/********************************************
* Insert s into symtab at position n.
* Returns:
* position in table
*/
@trusted
SYMIDX symbol_insert(ref symtab_t symtab, Symbol* s, SYMIDX n)
{
const sinew = symbol_add(s); // added at end, have to move it
for (SYMIDX i = sinew; i > n; --i)
{
symtab[i] = symtab[i - 1];
symtab[i].Ssymnum += 1;
}
globsym[n] = s;
s.Ssymnum = n;
return n;
}
/****************************
* Free up the symbols stab[n1 .. n2]
*/
@trusted
void freesymtab(Symbol **stab,SYMIDX n1,SYMIDX n2)
{
if (!stab)
return;
debug if (debugy)
printf("freesymtab(from %d to %d)\n", cast(int) n1, cast(int) n2);
assert(stab != globsym[].ptr || (n1 <= n2 && n2 <= globsym.length));
foreach (ref s; stab[n1 .. n2])
{
if (s && s.Sflags & SFLfree)
{
debug
{
if (debugy)
printf("Freeing %p '%s'\n",s,s.Sident.ptr);
symbol_debug(s);
}
s.Sl = s.Sr = null;
s.Ssymnum = SYMIDX.max;
symbol_free(s);
s = null;
}
}
}
/****************************
* Create a copy of a symbol.
*/
@trusted
Symbol * symbol_copy(Symbol *s)
{ Symbol *scopy;
type *t;
symbol_debug(s);
/*printf("symbol_copy(%s)\n",s.Sident.ptr);*/
scopy = symbol_calloc(s.Sident.ptr[0 .. strlen(s.Sident.ptr)]);
memcpy(scopy,s,Symbol.sizeof - s.Sident.sizeof);
scopy.Sl = scopy.Sr = scopy.Snext = null;
scopy.Ssymnum = SYMIDX.max;
if (scopy.Sdt)
{
auto dtb = DtBuilder(0);
dtb.nzeros(cast(uint)type_size(scopy.Stype));
scopy.Sdt = dtb.finish();
}
if (scopy.Sflags & (SFLvalue | SFLdtorexp))
scopy.Svalue = el_copytree(s.Svalue);
t = scopy.Stype;
if (t)
{ t.Tcount++; /* one more parent of the type */
type_debug(t);
}
return scopy;
}
/*******************************************
* Hydrate a symbol tree.
*/
static if (HYDRATE)
{
@trusted
void symbol_tree_hydrate(Symbol **ps)
{ Symbol *s;
while (isdehydrated(*ps)) /* if symbol is dehydrated */
{
s = symbol_hydrate(ps);
symbol_debug(s);
if (s.Scover)
symbol_hydrate(&s.Scover);
symbol_tree_hydrate(&s.Sl);
ps = &s.Sr;
}
}
}
/*******************************************
* Dehydrate a symbol tree.
*/
static if (DEHYDRATE)
{
@trusted
void symbol_tree_dehydrate(Symbol **ps)
{ Symbol *s;
while ((s = *ps) != null && !isdehydrated(s)) /* if symbol exists */
{
symbol_debug(s);
symbol_dehydrate(ps);
version (DEBUG_XSYMGEN)
{
if (xsym_gen && ph_in_head(s))
return;
}
symbol_dehydrate(&s.Scover);
symbol_tree_dehydrate(&s.Sl);
ps = &s.Sr;
}
}
}
/*******************************************
* Hydrate a symbol.
*/
static if (HYDRATE)
{
@trusted
Symbol *symbol_hydrate(Symbol **ps)
{ Symbol *s;
s = *ps;
if (isdehydrated(s)) /* if symbol is dehydrated */
{ type *t;
struct_t *st;
s = cast(Symbol *) ph_hydrate(cast(void**)ps);
debug debugy && printf("symbol_hydrate('%s')\n",s.Sident.ptr);
symbol_debug(s);
if (!isdehydrated(s.Stype)) // if this symbol is already dehydrated
return s; // no need to do it again
if (pstate.SThflag != FLAG_INPLACE && s.Sfl != FLreg)
s.Sxtrnnum = 0; // not written to .OBJ file yet
type_hydrate(&s.Stype);
//printf("symbol_hydrate(%p, '%s', t = %p)\n",s,s.Sident.ptr,s.Stype);
t = s.Stype;
if (t)
type_debug(t);
if (t && tyfunc(t.Tty) && ph_hydrate(cast(void**)&s.Sfunc))
{
func_t *f = s.Sfunc;
SYMIDX si;
debug assert(f);
list_hydrate(&f.Fsymtree,cast(list_free_fp)&symbol_tree_hydrate);
blocklist_hydrate(&f.Fstartblock);
ph_hydrate(cast(void**)&f.Flocsym.tab);
for (si = 0; si < f.Flocsym.length; si++)
symbol_hydrate(&f.Flocsym[].ptr[si]);
srcpos_hydrate(&f.Fstartline);
srcpos_hydrate(&f.Fendline);
symbol_hydrate(&f.F__func__);
if (CPP)
{
symbol_hydrate(&f.Fparsescope);
Classsym_hydrate(&f.Fclass);
symbol_hydrate(&f.Foversym);
symbol_hydrate(&f.Fexplicitspec);
symbol_hydrate(&f.Fsurrogatesym);
list_hydrate(&f.Fclassfriends,cast(list_free_fp)&symbol_hydrate);
el_hydrate(&f.Fbaseinit);
token_hydrate(&f.Fbody);
symbol_hydrate(&f.Falias);
list_hydrate(&f.Fthunks,cast(list_free_fp)&symbol_hydrate);
if (f.Fflags & Finstance)
symbol_hydrate(&f.Ftempl);
else
thunk_hydrate(&f.Fthunk);
param_hydrate(&f.Farglist);
param_hydrate(&f.Fptal);
list_hydrate(&f.Ffwdrefinstances,cast(list_free_fp)&symbol_hydrate);
list_hydrate(&f.Fexcspec,cast(list_free_fp)&type_hydrate);
}
}
if (CPP)
symbol_hydrate(&s.Sscope);
switch (s.Sclass)
{
case SC.struct_:
if (CPP)
{
st = cast(struct_t *) ph_hydrate(cast(void**)&s.Sstruct);
assert(st);
symbol_tree_hydrate(&st.Sroot);
ph_hydrate(cast(void**)&st.Spvirtder);
list_hydrate(&st.Sfldlst,cast(list_free_fp)&symbol_hydrate);
list_hydrate(&st.Svirtual,cast(list_free_fp)&mptr_hydrate);
list_hydrate(&st.Sopoverload,cast(list_free_fp)&symbol_hydrate);
list_hydrate(&st.Scastoverload,cast(list_free_fp)&symbol_hydrate);
list_hydrate(&st.Sclassfriends,cast(list_free_fp)&symbol_hydrate);
list_hydrate(&st.Sfriendclass,cast(list_free_fp)&symbol_hydrate);
list_hydrate(&st.Sfriendfuncs,cast(list_free_fp)&symbol_hydrate);
assert(!st.Sinlinefuncs);
baseclass_hydrate(&st.Sbase);
baseclass_hydrate(&st.Svirtbase);
baseclass_hydrate(&st.Smptrbase);
baseclass_hydrate(&st.Sprimary);
baseclass_hydrate(&st.Svbptrbase);
ph_hydrate(cast(void**)&st.Svecctor);
ph_hydrate(cast(void**)&st.Sctor);
ph_hydrate(cast(void**)&st.Sdtor);
ph_hydrate(cast(void**)&st.Sprimdtor);
ph_hydrate(cast(void**)&st.Spriminv);
ph_hydrate(cast(void**)&st.Sscaldeldtor);
ph_hydrate(cast(void**)&st.Sinvariant);
ph_hydrate(cast(void**)&st.Svptr);
ph_hydrate(cast(void**)&st.Svtbl);
ph_hydrate(cast(void**)&st.Sopeq);
ph_hydrate(cast(void**)&st.Sopeq2);
ph_hydrate(cast(void**)&st.Scpct);
ph_hydrate(cast(void**)&st.Sveccpct);
ph_hydrate(cast(void**)&st.Salias);
ph_hydrate(cast(void**)&st.Stempsym);
param_hydrate(&st.Sarglist);
param_hydrate(&st.Spr_arglist);
ph_hydrate(cast(void**)&st.Svbptr);
ph_hydrate(cast(void**)&st.Svbptr_parent);
ph_hydrate(cast(void**)&st.Svbtbl);
}
else
{
ph_hydrate(cast(void**)&s.Sstruct);
symbol_tree_hydrate(&s.Sstruct.Sroot);
list_hydrate(&s.Sstruct.Sfldlst,cast(list_free_fp)&symbol_hydrate);
}
break;
case SC.enum_:
assert(s.Senum);
ph_hydrate(cast(void**)&s.Senum);
if (CPP)
{ ph_hydrate(cast(void**)&s.Senum.SEalias);
list_hydrate(&s.Senum.SEenumlist,cast(list_free_fp)&symbol_hydrate);
}
break;
case SC.template_:
{ template_t *tm;
tm = cast(template_t *) ph_hydrate(cast(void**)&s.Stemplate);
list_hydrate(&tm.TMinstances,cast(list_free_fp)&symbol_hydrate);
list_hydrate(&tm.TMfriends,cast(list_free_fp)&symbol_hydrate);
param_hydrate(&tm.TMptpl);
param_hydrate(&tm.TMptal);
token_hydrate(&tm.TMbody);
list_hydrate(&tm.TMmemberfuncs,cast(list_free_fp)&tmf_hydrate);
list_hydrate(&tm.TMexplicit,cast(list_free_fp)&tme_hydrate);
list_hydrate(&tm.TMnestedexplicit,cast(list_free_fp)&tmne_hydrate);
list_hydrate(&tm.TMnestedfriends,cast(list_free_fp)&tmnf_hydrate);
ph_hydrate(cast(void**)&tm.TMnext);
symbol_hydrate(&tm.TMpartial);
symbol_hydrate(&tm.TMprimary);
break;
}
case SC.namespace:
symbol_tree_hydrate(&s.Snameroot);
list_hydrate(&s.Susing,cast(list_free_fp)&symbol_hydrate);
break;
case SC.memalias:
case SC.funcalias:
case SC.adl:
list_hydrate(&s.Spath,cast(list_free_fp)&symbol_hydrate);
goto case SC.alias_;
case SC.alias_: