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