0
|
1 module dmd.AnonDeclaration;
|
|
2
|
|
3 import dmd.OutBuffer;
|
|
4 import dmd.Loc;
|
|
5 import dmd.Scope;
|
|
6 import dmd.Array;
|
|
7 import dmd.AttribDeclaration;
|
|
8 import dmd.HdrGenState;
|
|
9 import dmd.Dsymbol;
|
|
10 import dmd.AggregateDeclaration;
|
|
11 import dmd.AnonymousAggregateDeclaration;
|
|
12 import dmd.STC;
|
|
13 import dmd.Module;
|
|
14 import dmd.VarDeclaration;
|
|
15
|
|
16 class AnonDeclaration : AttribDeclaration
|
|
17 {
|
|
18 int isunion;
|
|
19 int sem = 0; // 1 if successful semantic()
|
|
20
|
|
21 this(Loc loc, int isunion, Array decl)
|
|
22 {
|
|
23 super(decl);
|
|
24 this.loc = loc;
|
|
25 this.isunion = isunion;
|
|
26 }
|
|
27
|
|
28 Dsymbol syntaxCopy(Dsymbol s)
|
|
29 {
|
|
30 assert(false);
|
|
31 }
|
|
32
|
|
33 void semantic(Scope sc)
|
|
34 {
|
|
35 //printf("\tAnonDeclaration::semantic %s %p\n", isunion ? "union" : "struct", this);
|
|
36
|
|
37 Scope scx = null;
|
|
38 if (scope_)
|
|
39 {
|
|
40 sc = scope_;
|
|
41 scx = scope_;
|
|
42 scope_ = null;
|
|
43 }
|
|
44
|
|
45 assert(sc.parent);
|
|
46
|
|
47 Dsymbol parent = sc.parent.pastMixin();
|
|
48 AggregateDeclaration ad = parent.isAggregateDeclaration();
|
|
49
|
|
50 if (!ad || (!ad.isStructDeclaration() && !ad.isClassDeclaration()))
|
|
51 {
|
|
52 error("can only be a part of an aggregate");
|
|
53 return;
|
|
54 }
|
|
55
|
|
56 if (decl)
|
|
57 {
|
|
58 AnonymousAggregateDeclaration aad = new AnonymousAggregateDeclaration();
|
|
59 int adisunion;
|
|
60
|
|
61 if (sc.anonAgg)
|
|
62 {
|
|
63 ad = sc.anonAgg;
|
|
64 adisunion = sc.inunion;
|
|
65 }
|
|
66 else
|
|
67 adisunion = ad.isUnionDeclaration() !is null;
|
|
68
|
|
69 // printf("\tsc.anonAgg = %p\n", sc.anonAgg);
|
|
70 // printf("\tad = %p\n", ad);
|
|
71 // printf("\taad = %p\n", &aad);
|
|
72
|
|
73 sc = sc.push();
|
|
74 sc.anonAgg = aad;
|
|
75 sc.stc &= ~(STCauto | STCscope | STCstatic | STCtls | STCgshared);
|
|
76 sc.inunion = isunion;
|
|
77 sc.offset = 0;
|
|
78 sc.flags = cast(SCOPE)0;
|
|
79 aad.structalign = sc.structalign;
|
|
80 aad.parent = ad;
|
|
81
|
|
82 for (uint i = 0; i < decl.dim; i++)
|
|
83 {
|
|
84 Dsymbol s = cast(Dsymbol)decl.data[i];
|
|
85
|
|
86 s.semantic(sc);
|
|
87 if (isunion)
|
|
88 sc.offset = 0;
|
|
89 if (aad.sizeok == 2)
|
|
90 break;
|
|
91 }
|
|
92 sc = sc.pop();
|
|
93
|
|
94 // If failed due to forward references, unwind and try again later
|
|
95 if (aad.sizeok == 2)
|
|
96 {
|
|
97 ad.sizeok = 2;
|
|
98 //printf("\tsetting ad.sizeok %p to 2\n", ad);
|
|
99 if (!sc.anonAgg)
|
|
100 {
|
|
101 scope_ = scx ? scx : new Scope(sc); ///<
|
|
102 scope_.setNoFree();
|
|
103 scope_.module_.addDeferredSemantic(this);
|
|
104 }
|
|
105 //printf("\tforward reference %p\n", this);
|
|
106 return;
|
|
107 }
|
|
108 if (sem == 0)
|
|
109 {
|
|
110 Module.dprogress++;
|
|
111 sem = 1;
|
|
112 //printf("\tcompleted %p\n", this);
|
|
113 }
|
|
114 else {
|
|
115 ;//printf("\talready completed %p\n", this);
|
|
116 }
|
|
117
|
|
118 // 0 sized structs are set to 1 byte
|
|
119 if (aad.structsize == 0)
|
|
120 {
|
|
121 aad.structsize = 1;
|
|
122 aad.alignsize = 1;
|
|
123 }
|
|
124
|
|
125 // Align size of anonymous aggregate
|
|
126 //printf("aad.structalign = %d, aad.alignsize = %d, sc.offset = %d\n", aad.structalign, aad.alignsize, sc.offset);
|
|
127 ad.alignmember(aad.structalign, aad.alignsize, &sc.offset);
|
|
128 //ad.structsize = sc.offset;
|
|
129 //printf("sc.offset = %d\n", sc.offset);
|
|
130
|
|
131 // Add members of aad to ad
|
|
132 //printf("\tadding members of aad to '%s'\n", ad.toChars());
|
|
133 for (uint i = 0; i < aad.fields.dim; i++)
|
|
134 {
|
|
135 VarDeclaration v = cast(VarDeclaration)aad.fields.data[i];
|
|
136
|
|
137 v.offset += sc.offset;
|
|
138 ad.fields.push(cast(void*)v);
|
|
139 }
|
|
140
|
|
141 // Add size of aad to ad
|
|
142 if (adisunion)
|
|
143 {
|
|
144 if (aad.structsize > ad.structsize)
|
|
145 ad.structsize = aad.structsize;
|
|
146 sc.offset = 0;
|
|
147 }
|
|
148 else
|
|
149 {
|
|
150 ad.structsize = sc.offset + aad.structsize;
|
|
151 sc.offset = ad.structsize;
|
|
152 }
|
|
153
|
|
154 if (ad.alignsize < aad.alignsize)
|
|
155 ad.alignsize = aad.alignsize;
|
|
156 }
|
|
157 }
|
|
158
|
|
159 void toCBuffer(OutBuffer buf, HdrGenState* hgs)
|
|
160 {
|
|
161 assert(false);
|
|
162 }
|
|
163
|
|
164 string kind()
|
|
165 {
|
|
166 assert(false);
|
|
167 }
|
|
168 }
|
|
169
|