Mercurial > projects > ddmd
annotate dmd/StructDeclaration.d @ 131:206db751bd4c
dmdfe 2.037 compiles now
author | Eldar Insafutdinov <e.insafutdinov@gmail.com> |
---|---|
date | Fri, 10 Sep 2010 00:27:37 +0100 |
parents | 60bb0fe4563e |
children | af1bebfd96a4 |
rev | line source |
---|---|
0 | 1 module dmd.StructDeclaration; |
2 | |
114 | 3 import dmd.common; |
0 | 4 import dmd.AggregateDeclaration; |
5 import dmd.FuncDeclaration; | |
6 import dmd.DeclarationExp; | |
7 import dmd.VoidInitializer; | |
8 import dmd.Initializer; | |
9 import dmd.ExpInitializer; | |
10 import dmd.TOK; | |
11 import dmd.Statement; | |
12 import dmd.VarExp; | |
13 import dmd.CompoundStatement; | |
14 import dmd.AssignExp; | |
15 import dmd.DotVarExp; | |
16 import dmd.AddrExp; | |
17 import dmd.CastExp; | |
18 import dmd.PostBlitDeclaration; | |
19 import dmd.Lexer; | |
20 import dmd.ExpStatement; | |
21 import dmd.DotIdExp; | |
22 import dmd.TypeSArray; | |
23 import dmd.ThisExp; | |
24 import dmd.ThisDeclaration; | |
25 import dmd.TypeFunction; | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
26 import dmd.Parameter; |
0 | 27 import dmd.Id; |
28 import dmd.TY; | |
29 import dmd.LINK; | |
30 import dmd.Type; | |
31 import dmd.DsymbolTable; | |
32 import dmd.ArrayTypes; | |
33 import dmd.Loc; | |
34 import dmd.STC; | |
35 import dmd.Identifier; | |
36 import dmd.TemplateInstance; | |
37 import dmd.Dsymbol; | |
38 import dmd.Scope; | |
39 import dmd.OutBuffer; | |
40 import dmd.HdrGenState; | |
41 import dmd.PROT; | |
42 import dmd.TypeStruct; | |
43 import dmd.expression.Util; | |
44 import dmd.Expression; | |
45 import dmd.IdentifierExp; | |
46 import dmd.PtrExp; | |
47 import dmd.CallExp; | |
48 import dmd.ReturnStatement; | |
49 import dmd.ScopeDsymbol; | |
50 import dmd.Module; | |
51 import dmd.VarDeclaration; | |
52 import dmd.InvariantDeclaration; | |
53 import dmd.NewDeclaration; | |
54 import dmd.DeleteDeclaration; | |
55 import dmd.Global; | |
131
206db751bd4c
dmdfe 2.037 compiles now
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
130
diff
changeset
|
56 import dmd.MOD; |
206db751bd4c
dmdfe 2.037 compiles now
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
130
diff
changeset
|
57 import dmd.IntegerExp; |
206db751bd4c
dmdfe 2.037 compiles now
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
130
diff
changeset
|
58 import dmd.EqualExp; |
206db751bd4c
dmdfe 2.037 compiles now
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
130
diff
changeset
|
59 import dmd.AndAndExp; |
0 | 60 |
61 import dmd.backend.dt_t; | |
62 import dmd.backend.Util; | |
63 import dmd.backend.SC; | |
64 import dmd.backend.DT; | |
65 import dmd.backend.FL; | |
66 import dmd.backend.glue; | |
67 | |
129 | 68 import std.stdio; |
69 | |
0 | 70 class StructDeclaration : AggregateDeclaration |
71 { | |
72 bool zeroInit; // true if initialize with 0 fill | |
73 | |
74 version (DMDV2) { | |
75 int hasIdentityAssign; // !=0 if has identity opAssign | |
76 FuncDeclaration cpctor; // generated copy-constructor, if any | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
77 FuncDeclaration eq; // bool opEquals(ref const T), if any |
0 | 78 |
79 FuncDeclarations postblits; // Array of postblit functions | |
80 FuncDeclaration postblit; // aggregate postblit | |
81 } | |
82 | |
83 this(Loc loc, Identifier id) | |
84 { | |
85 super(loc, id); | |
86 | |
87 // For forward references | |
88 type = new TypeStruct(this); | |
89 | |
90 postblits = new FuncDeclarations(); /// | |
91 } | |
92 | |
72 | 93 override Dsymbol syntaxCopy(Dsymbol s) |
0 | 94 { |
49 | 95 StructDeclaration sd; |
96 | |
97 if (s) | |
98 sd = cast(StructDeclaration)s; | |
99 else | |
100 sd = new StructDeclaration(loc, ident); | |
101 ScopeDsymbol.syntaxCopy(sd); | |
102 return sd; | |
0 | 103 } |
104 | |
72 | 105 override void semantic(Scope sc) |
0 | 106 { |
107 Scope sc2; | |
108 | |
109 //printf("+StructDeclaration.semantic(this=%p, '%s')\n", this, toChars()); | |
110 | |
111 //static int count; if (++count == 20) *(char*)0=0; | |
112 | |
113 assert(type); | |
114 if (!members) // if forward reference | |
115 return; | |
116 | |
117 if (symtab) | |
118 { if (!scope_) | |
119 return; // semantic() already completed | |
120 } | |
121 else | |
122 symtab = new DsymbolTable(); | |
123 | |
124 Scope scx = null; | |
125 if (scope_) | |
126 { sc = scope_; | |
127 scx = scope_; // save so we don't make redundant copies | |
128 scope_ = null; | |
129 } | |
130 | |
131 parent = sc.parent; | |
132 type = type.semantic(loc, sc); | |
133 version (STRUCTTHISREF) { | |
134 handle = type; | |
135 } else { | |
136 handle = type.pointerTo(); | |
137 } | |
138 structalign = sc.structalign; | |
139 protection = sc.protection; | |
140 storage_class |= sc.stc; | |
141 if (sc.stc & STC.STCdeprecated) | |
142 isdeprecated = 1; | |
143 assert(!isAnonymous()); | |
144 if (sc.stc & STC.STCabstract) | |
145 error("structs, unions cannot be abstract"); | |
146 version (DMDV2) { | |
147 if (storage_class & STC.STCimmutable) | |
148 type = type.invariantOf(); | |
149 else if (storage_class & STC.STCconst) | |
150 type = type.constOf(); | |
151 else if (storage_class & STC.STCshared) | |
152 type = type.sharedOf(); | |
153 } | |
154 | |
155 if (sizeok == 0) // if not already done the addMember step | |
156 { | |
157 int hasfunctions = 0; | |
77
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
74
diff
changeset
|
158 foreach(Dsymbol s; members) |
0 | 159 { |
160 //printf("adding member '%s' to '%s'\n", s.toChars(), this.toChars()); | |
13 | 161 s.addMember(sc, this, true); |
0 | 162 if (s.isFuncDeclaration()) |
163 hasfunctions = 1; | |
164 } | |
165 | |
166 // If nested struct, add in hidden 'this' pointer to outer scope | |
167 if (hasfunctions && !(storage_class & STC.STCstatic)) | |
168 { Dsymbol s = toParent2(); | |
169 if (s) | |
170 { | |
171 AggregateDeclaration ad = s.isAggregateDeclaration(); | |
172 FuncDeclaration fd = s.isFuncDeclaration(); | |
173 | |
174 TemplateInstance ti; | |
175 if (ad && (ti = ad.parent.isTemplateInstance()) !is null && ti.isnested || fd) | |
176 { isnested = true; | |
177 Type t; | |
178 if (ad) | |
179 t = ad.handle; | |
180 else if (fd) | |
181 { | |
182 AggregateDeclaration add = fd.isMember2(); | |
183 if (add) | |
184 t = add.handle; | |
185 else | |
186 t = Type.tvoidptr; | |
187 } | |
188 else | |
189 assert(0); | |
190 if (t.ty == TY.Tstruct) | |
191 t = Type.tvoidptr; // t should not be a ref type | |
192 assert(!vthis); | |
193 vthis = new ThisDeclaration(loc, t); | |
194 //vthis.storage_class |= STC.STCref; | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
195 members.push(vthis); |
0 | 196 } |
197 } | |
198 } | |
199 } | |
200 | |
201 sizeok = 0; | |
202 sc2 = sc.push(this); | |
203 sc2.stc &= storage_class & STC.STC_TYPECTOR; | |
204 sc2.parent = this; | |
205 if (isUnionDeclaration()) | |
206 sc2.inunion = 1; | |
207 sc2.protection = PROT.PROTpublic; | |
208 sc2.explicitProtection = 0; | |
209 | |
210 int members_dim = members.dim; | |
77
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
74
diff
changeset
|
211 foreach(Dsymbol s; members) |
0 | 212 { |
129 | 213 s.semantic(sc2); |
214 if (isUnionDeclaration()) | |
215 sc2.offset = 0; | |
0 | 216 static if (false) { |
129 | 217 if (sizeok == 2) |
218 { //printf("forward reference\n"); | |
219 break; | |
220 } | |
0 | 221 } |
129 | 222 if (auto d = s.isDeclaration()) |
223 { | |
224 if (auto t = d.type) { | |
225 if (t.toBasetype().ty == TY.Tstruct) { | |
226 auto ad = t.toDsymbol(sc).isThis(); | |
227 /* | |
228 StructDeclaration sd = cast(StructDeclaration)foo; | |
229 if (foo && !sd) { | |
230 writeln(t.classin); | |
231 writeln(foo.classinfo.name); | |
232 assert(false); | |
233 } | |
234 */ | |
235 if (ad && ad.isnested) | |
236 error("inner struct %s cannot be a field", ad.toChars()); | |
237 } | |
238 } | |
239 } | |
0 | 240 } |
241 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
242 version(DMDV1) { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
243 /* This doesn't work for DMDV2 because (ref S) and (S) parameter |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
244 * lists will overload the same. |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
245 */ |
0 | 246 /* The TypeInfo_Struct is expecting an opEquals and opCmp with |
247 * a parameter that is a pointer to the struct. But if there | |
248 * isn't one, but is an opEquals or opCmp with a value, write | |
249 * another that is a shell around the value: | |
250 * int opCmp(struct *p) { return opCmp(*p); } | |
251 */ | |
252 | |
253 TypeFunction tfeqptr; | |
254 { | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
255 auto arguments = new Parameters; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
256 auto arg = new Parameter(STC.STCin, handle, Id.p, null); |
0 | 257 |
129 | 258 arguments.push(arg); |
259 tfeqptr = new TypeFunction(arguments, Type.tint32, 0, LINK.LINKd); | |
260 tfeqptr = cast(TypeFunction)tfeqptr.semantic(Loc(0), sc); | |
0 | 261 } |
262 | |
263 TypeFunction tfeq; | |
264 { | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
265 auto arguments = new Parameters; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
266 auto arg = new Parameter(STC.STCin, type, null, null); |
0 | 267 |
129 | 268 arguments.push(arg); |
269 tfeq = new TypeFunction(arguments, Type.tint32, 0, LINK.LINKd); | |
270 tfeq = cast(TypeFunction)tfeq.semantic(Loc(0), sc); | |
0 | 271 } |
272 | |
273 Identifier id = Id.eq; | |
274 for (int j = 0; j < 2; j++) | |
275 { | |
276 Dsymbol s = search_function(this, id); | |
277 FuncDeclaration fdx = s ? s.isFuncDeclaration() : null; | |
278 if (fdx) | |
279 { FuncDeclaration fd = fdx.overloadExactMatch(tfeqptr); | |
280 if (!fd) | |
281 { fd = fdx.overloadExactMatch(tfeq); | |
282 if (fd) | |
283 { // Create the thunk, fdptr | |
284 FuncDeclaration fdptr = new FuncDeclaration(loc, loc, fdx.ident, STC.STCundefined, tfeqptr); | |
285 Expression e = new IdentifierExp(loc, Id.p); | |
286 e = new PtrExp(loc, e); | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
287 auto args = new Expressions(); |
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
288 args.push(e); |
0 | 289 e = new IdentifierExp(loc, id); |
290 e = new CallExp(loc, e, args); | |
291 fdptr.fbody = new ReturnStatement(loc, e); | |
292 ScopeDsymbol ss = fdx.parent.isScopeDsymbol(); | |
293 assert(ss); | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
294 ss.members.push(fdptr); |
13 | 295 fdptr.addMember(sc, ss, true); |
0 | 296 fdptr.semantic(sc2); |
297 } | |
298 } | |
299 } | |
300 | |
301 id = Id.cmp; | |
302 } | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
303 } |
0 | 304 version (DMDV2) { |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
305 /* Try to find the opEquals function. Build it if necessary. |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
306 */ |
131
206db751bd4c
dmdfe 2.037 compiles now
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
130
diff
changeset
|
307 TypeFunction tfeqptr; |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
308 { // bool opEquals(const T*) const; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
309 auto parameters = new Parameters; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
310 version(STRUCTTHISREF) { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
311 // bool opEquals(ref const T) const; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
312 auto param = new Parameter(STC.STCref, type.constOf(), null, null); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
313 } else { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
314 // bool opEquals(const T*) const; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
315 auto param = new Parameter(STC.STCin, type.pointerTo(), null, null); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
316 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
317 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
318 parameters.push(param); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
319 tfeqptr = new TypeFunction(parameters, Type.tbool, 0, LINK.LINKd); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
320 tfeqptr.mod = MOD.MODconst; |
131
206db751bd4c
dmdfe 2.037 compiles now
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
130
diff
changeset
|
321 tfeqptr = cast(TypeFunction)(tfeqptr.semantic(Loc(0), sc2)); |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
322 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
323 Dsymbol s = search_function(this, Id.eq); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
324 FuncDeclaration fdx = s ? s.isFuncDeclaration() : null; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
325 if (fdx) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
326 { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
327 eq = fdx.overloadExactMatch(tfeqptr); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
328 if (!eq) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
329 fdx.error("type signature should be %s not %s", tfeqptr.toChars(), fdx.type.toChars()); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
330 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
331 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
332 if (!eq) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
333 eq = buildOpEquals(sc2); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
334 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
335 |
0 | 336 dtor = buildDtor(sc2); |
337 postblit = buildPostBlit(sc2); | |
338 cpctor = buildCpCtor(sc2); | |
339 buildOpAssign(sc2); | |
340 } | |
341 | |
342 sc2.pop(); | |
343 | |
344 if (sizeok == 2) | |
345 { // semantic() failed because of forward references. | |
346 // Unwind what we did, and defer it for later | |
347 fields.setDim(0); | |
348 structsize = 0; | |
349 alignsize = 0; | |
350 structalign = 0; | |
351 | |
87
b17640f0e4e8
Fixed a bug with a Scope.this(Scope enclosing) being called instead of Scope.clone() method (as a copy ctor replacement)
korDen
parents:
79
diff
changeset
|
352 scope_ = scx ? scx : sc.clone(); |
0 | 353 scope_.setNoFree(); |
354 scope_.module_.addDeferredSemantic(this); | |
355 //printf("\tdeferring %s\n", toChars()); | |
356 return; | |
357 } | |
358 | |
359 // 0 sized struct's are set to 1 byte | |
360 if (structsize == 0) | |
361 { | |
362 structsize = 1; | |
363 alignsize = 1; | |
364 } | |
365 | |
366 // Round struct size up to next alignsize boundary. | |
367 // This will ensure that arrays of structs will get their internals | |
368 // aligned properly. | |
369 structsize = (structsize + alignsize - 1) & ~(alignsize - 1); | |
370 | |
371 sizeok = 1; | |
372 Module.dprogress++; | |
373 | |
374 //printf("-StructDeclaration.semantic(this=%p, '%s')\n", this, toChars()); | |
375 | |
376 // Determine if struct is all zeros or not | |
377 zeroInit = true; | |
85
8e69d041a99d
Previous commit didn't compile. Fixed.
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
378 foreach (VarDeclaration vd; fields) |
0 | 379 { |
79 | 380 if (vd && !vd.isDataseg()) |
0 | 381 { |
79 | 382 if (vd.init) |
383 { | |
384 // Should examine init to see if it is really all 0's | |
0 | 385 zeroInit = false; |
386 break; | |
79 | 387 } |
388 else | |
389 { | |
390 if (!vd.type.isZeroInit(loc)) | |
391 { | |
392 zeroInit = false; | |
393 break; | |
394 } | |
395 } | |
0 | 396 } |
397 } | |
398 | |
399 /* Look for special member functions. | |
400 */ | |
401 version (DMDV2) { | |
402 ctor = search(Loc(0), Id.ctor, 0); | |
403 } | |
404 inv = cast(InvariantDeclaration)search(Loc(0), Id.classInvariant, 0); | |
405 aggNew = cast(NewDeclaration)search(Loc(0), Id.classNew, 0); | |
406 aggDelete = cast(DeleteDeclaration)search(Loc(0), Id.classDelete, 0); | |
407 | |
408 if (sc.func) | |
409 { | |
410 semantic2(sc); | |
411 semantic3(sc); | |
412 } | |
413 } | |
414 | |
72 | 415 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) |
0 | 416 { |
417 assert(false); | |
418 } | |
419 | |
72 | 420 override string mangle() |
0 | 421 { |
422 //printf("StructDeclaration.mangle() '%s'\n", toChars()); | |
423 return Dsymbol.mangle(); | |
424 } | |
425 | |
72 | 426 override string kind() |
0 | 427 { |
428 assert(false); | |
429 } | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
430 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
431 version(DMDV1) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
432 { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
433 Expression cloneMembers() |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
434 { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
435 assert(false); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
436 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
437 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
438 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
439 version(DMDV2) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
440 { |
0 | 441 /******************************************* |
442 * We need an opAssign for the struct if | |
443 * it has a destructor or a postblit. | |
444 * We need to generate one if a user-specified one does not exist. | |
445 */ | |
446 bool needOpAssign() | |
447 { | |
448 static if (false) { | |
449 printf("StructDeclaration.needOpAssign() %s\n", toChars()); | |
450 } | |
451 if (hasIdentityAssign) | |
452 goto Ldontneed; | |
453 | |
454 if (dtor || postblit) | |
455 goto Lneed; | |
456 | |
457 /* If any of the fields need an opAssign, then we | |
458 * need it too. | |
459 */ | |
85
8e69d041a99d
Previous commit didn't compile. Fixed.
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
460 foreach (VarDeclaration v; fields) |
0 | 461 { |
462 assert(v && v.storage_class & STC.STCfield); | |
463 if (v.storage_class & STC.STCref) | |
464 continue; | |
465 Type tv = v.type.toBasetype(); | |
466 while (tv.ty == TY.Tsarray) | |
467 { TypeSArray ta = cast(TypeSArray)tv; | |
468 tv = tv.nextOf().toBasetype(); | |
469 } | |
470 if (tv.ty == TY.Tstruct) | |
471 { TypeStruct ts = cast(TypeStruct)tv; | |
472 StructDeclaration sd = ts.sym; | |
473 if (sd.needOpAssign()) | |
474 goto Lneed; | |
475 } | |
476 } | |
477 Ldontneed: | |
478 static if (false) { | |
479 printf("\tdontneed\n"); | |
480 } | |
481 return false; | |
482 | |
483 Lneed: | |
484 static if (false) { | |
485 printf("\tneed\n"); | |
486 } | |
487 return true; | |
488 } | |
489 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
490 /******************************************* |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
491 * We need an opEquals for the struct if |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
492 * any fields has an opEquals. |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
493 * Generate one if a user-specified one does not exist. |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
494 */ |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
495 bool needOpEquals() |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
496 { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
497 enum X = 0; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
498 static if (X) printf("StructDeclaration::needOpEquals() %s\n", toChars()); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
499 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
500 /* If any of the fields has an opEquals, then we |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
501 * need it too. |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
502 */ |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
503 foreach (s; fields) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
504 { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
505 VarDeclaration v = s.isVarDeclaration(); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
506 assert(v && v.storage_class & STC.STCfield); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
507 if (v.storage_class & STC.STCref) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
508 continue; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
509 Type tv = v.type.toBasetype(); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
510 while (tv.ty == Tsarray) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
511 { auto ta = cast(TypeSArray)tv; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
512 tv = tv.nextOf().toBasetype(); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
513 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
514 if (tv.ty == Tstruct) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
515 { auto ts = cast(TypeStruct)tv; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
516 StructDeclaration sd = ts.sym; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
517 if (sd.eq) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
518 goto Lneed; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
519 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
520 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
521 Ldontneed: |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
522 static if (X) printf("\tdontneed\n"); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
523 return false; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
524 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
525 Lneed: |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
526 static if (X) printf("\tneed\n"); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
527 return true; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
528 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
529 |
0 | 530 /****************************************** |
531 * Build opAssign for struct. | |
532 * S* opAssign(S s) { ... } | |
533 */ | |
534 FuncDeclaration buildOpAssign(Scope sc) | |
535 { | |
536 if (!needOpAssign()) | |
537 return null; | |
538 | |
539 //printf("StructDeclaration.buildOpAssign() %s\n", toChars()); | |
540 | |
541 FuncDeclaration fop = null; | |
542 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
543 auto param = new Parameter(STC.STCnodtor, type, Id.p, null); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
544 auto fparams = new Parameters; |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
545 fparams.push(param); |
0 | 546 Type ftype = new TypeFunction(fparams, handle, false, LINK.LINKd); |
547 version (STRUCTTHISREF) { | |
548 (cast(TypeFunction)ftype).isref = 1; | |
549 } | |
550 | |
551 fop = new FuncDeclaration(Loc(0), Loc(0), Id.assign, STC.STCundefined, ftype); | |
552 | |
553 Expression e = null; | |
554 if (postblit) | |
555 { /* Swap: | |
556 * tmp = *this; *this = s; tmp.dtor(); | |
557 */ | |
558 //printf("\tswap copy\n"); | |
559 Identifier idtmp = Lexer.uniqueId("__tmp"); | |
560 VarDeclaration tmp; | |
561 AssignExp ec = null; | |
562 if (dtor) | |
563 { | |
564 tmp = new VarDeclaration(Loc(0), type, idtmp, new VoidInitializer(Loc(0))); | |
565 tmp.noauto = true; | |
566 e = new DeclarationExp(Loc(0), tmp); | |
567 | |
568 Expression e2; | |
569 version (STRUCTTHISREF) { | |
570 e2 = new ThisExp(Loc(0)); | |
571 } else { | |
572 e2 = new PtrExp(Loc(0), new ThisExp(Loc(0))); | |
573 } | |
574 ec = new AssignExp(Loc(0), new VarExp(Loc(0), tmp), e2); | |
575 ec.op = TOK.TOKblit; | |
576 e = Expression.combine(e, ec); | |
577 } | |
578 Expression e2; | |
579 version (STRUCTTHISREF) { | |
580 e2 = new ThisExp(Loc(0)); | |
581 } else { | |
582 e2 = new PtrExp(Loc(0), new ThisExp(Loc(0))); | |
583 } | |
584 | |
585 ec = new AssignExp(Loc(0), e2, new IdentifierExp(Loc(0), Id.p)); | |
586 ec.op = TOK.TOKblit; | |
587 e = Expression.combine(e, ec); | |
588 if (dtor) | |
589 { | |
590 /* Instead of running the destructor on s, run it | |
591 * on tmp. This avoids needing to copy tmp back in to s. | |
592 */ | |
593 Expression ecc = new DotVarExp(Loc(0), new VarExp(Loc(0), tmp), dtor, 0); | |
594 ecc = new CallExp(Loc(0), ecc); | |
595 e = Expression.combine(e, ecc); | |
596 } | |
597 } | |
598 else | |
599 { /* Do memberwise copy | |
600 */ | |
601 //printf("\tmemberwise copy\n"); | |
85
8e69d041a99d
Previous commit didn't compile. Fixed.
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
602 foreach (VarDeclaration v; fields) |
0 | 603 { |
604 assert(v && v.storage_class & STC.STCfield); | |
605 // this.v = s.v; | |
606 AssignExp ec = new AssignExp(Loc(0), new DotVarExp(Loc(0), new ThisExp(Loc(0)), v, 0), new DotVarExp(Loc(0), new IdentifierExp(Loc(0), Id.p), v, 0)); | |
607 ec.op = TOK.TOKblit; | |
608 e = Expression.combine(e, ec); | |
609 } | |
610 } | |
611 Statement s1 = new ExpStatement(Loc(0), e); | |
612 | |
613 /* Add: | |
614 * return this; | |
615 */ | |
616 e = new ThisExp(Loc(0)); | |
617 Statement s2 = new ReturnStatement(Loc(0), e); | |
618 | |
619 fop.fbody = new CompoundStatement(Loc(0), s1, s2); | |
620 | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
621 members.push(fop); |
13 | 622 fop.addMember(sc, this, true); |
0 | 623 |
624 sc = sc.push(); | |
625 sc.stc = STC.STCundefined; | |
626 sc.linkage = LINK.LINKd; | |
627 | |
628 fop.semantic(sc); | |
629 | |
630 sc.pop(); | |
631 | |
632 //printf("-StructDeclaration.buildOpAssign() %s\n", toChars()); | |
633 | |
634 return fop; | |
635 } | |
636 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
637 /****************************************** |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
638 * Build opEquals for struct. |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
639 * const bool opEquals(const ref S s) { ... } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
640 */ |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
641 FuncDeclaration buildOpEquals(Scope sc) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
642 { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
643 if (!needOpEquals()) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
644 return null; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
645 //printf("StructDeclaration::buildOpEquals() %s\n", toChars()); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
646 Loc loc = this.loc; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
647 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
648 auto parameters = new Parameters; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
649 version (STRUCTTHISREF) { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
650 // bool opEquals(ref const T) const; |
131
206db751bd4c
dmdfe 2.037 compiles now
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
130
diff
changeset
|
651 auto param = new Parameter(STC.STCref, type.constOf(), Id.p, null); |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
652 } else { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
653 // bool opEquals(const T*) const; |
131
206db751bd4c
dmdfe 2.037 compiles now
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
130
diff
changeset
|
654 auto param = new Parameter(STC.STCin, type.pointerTo(), Id.p, null); |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
655 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
656 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
657 parameters.push(param); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
658 auto ftype = new TypeFunction(parameters, Type.tbool, 0, LINKd); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
659 ftype.mod = MOD.MODconst; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
660 ftype = cast(TypeFunction)ftype.semantic(loc, sc); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
661 |
131
206db751bd4c
dmdfe 2.037 compiles now
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
130
diff
changeset
|
662 auto fop = new FuncDeclaration(loc, Loc(0), Id.eq, STC.STCundefined, ftype); |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
663 |
131
206db751bd4c
dmdfe 2.037 compiles now
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
130
diff
changeset
|
664 Expression e = null; |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
665 /* Do memberwise compare |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
666 */ |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
667 //printf("\tmemberwise compare\n"); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
668 foreach (s; fields) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
669 { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
670 VarDeclaration v = s.isVarDeclaration(); |
131
206db751bd4c
dmdfe 2.037 compiles now
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
130
diff
changeset
|
671 assert(v && v.storage_class & STC.STCfield); |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
672 if (v.storage_class & STC.STCref) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
673 assert(0); // what should we do with this? |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
674 // this.v == s.v; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
675 auto ec = new EqualExp(TOKequal, loc, |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
676 new DotVarExp(loc, new ThisExp(loc), v, 0), |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
677 new DotVarExp(loc, new IdentifierExp(loc, Id.p), v, 0)); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
678 if (e) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
679 e = new AndAndExp(loc, e, ec); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
680 else |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
681 e = ec; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
682 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
683 if (!e) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
684 e = new IntegerExp(loc, 1, Type.tbool); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
685 fop.fbody = new ReturnStatement(loc, e); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
686 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
687 members.push(fop); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
688 fop.addMember(sc, this, 1); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
689 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
690 sc = sc.push(); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
691 sc.stc = 0; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
692 sc.linkage = LINK.LINKd; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
693 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
694 fop.semantic(sc); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
695 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
696 sc.pop(); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
697 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
698 //printf("-StructDeclaration::buildOpEquals() %s\n", toChars()); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
699 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
700 return fop; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
701 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
702 |
0 | 703 /***************************************** |
704 * Create inclusive postblit for struct by aggregating | |
705 * all the postblits in postblits[] with the postblits for | |
706 * all the members. | |
707 * Note the close similarity with AggregateDeclaration.buildDtor(), | |
708 * and the ordering changes (runs forward instead of backwards). | |
709 */ | |
710 | |
711 version (DMDV2) { | |
712 FuncDeclaration buildPostBlit(Scope sc) | |
713 { | |
714 //printf("StructDeclaration.buildPostBlit() %s\n", toChars()); | |
715 Expression e = null; | |
716 | |
85
8e69d041a99d
Previous commit didn't compile. Fixed.
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
717 foreach (VarDeclaration v; fields) |
0 | 718 { |
79 | 719 assert(v && v.storage_class & STC.STCfield); |
720 if (v.storage_class & STC.STCref) | |
721 continue; | |
722 Type tv = v.type.toBasetype(); | |
723 size_t dim = 1; | |
724 while (tv.ty == TY.Tsarray) | |
725 { TypeSArray ta = cast(TypeSArray)tv; | |
726 dim *= (cast(TypeSArray)tv).dim.toInteger(); | |
727 tv = tv.nextOf().toBasetype(); | |
0 | 728 } |
79 | 729 if (tv.ty == TY.Tstruct) |
730 { TypeStruct ts = cast(TypeStruct)tv; | |
731 StructDeclaration sd = ts.sym; | |
732 if (sd.postblit) | |
733 { Expression ex; | |
734 | |
735 // this.v | |
736 ex = new ThisExp(Loc(0)); | |
737 ex = new DotVarExp(Loc(0), ex, v, 0); | |
738 | |
739 if (dim == 1) | |
740 { // this.v.postblit() | |
741 ex = new DotVarExp(Loc(0), ex, sd.postblit, 0); | |
742 ex = new CallExp(Loc(0), ex); | |
743 } | |
744 else | |
745 { | |
746 // Typeinfo.postblit(cast(void*)&this.v); | |
747 Expression ea = new AddrExp(Loc(0), ex); | |
748 ea = new CastExp(Loc(0), ea, Type.tvoid.pointerTo()); | |
749 | |
750 Expression et = v.type.getTypeInfo(sc); | |
103 | 751 et = new DotIdExp(Loc(0), et, Id._postblit); |
79 | 752 |
753 ex = new CallExp(Loc(0), et, ea); | |
754 } | |
755 e = Expression.combine(e, ex); // combine in forward order | |
756 } | |
0 | 757 } |
758 } | |
759 | |
760 /* Build our own "postblit" which executes e | |
761 */ | |
762 if (e) | |
763 { //printf("Building __fieldPostBlit()\n"); | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
764 auto dd = new PostBlitDeclaration(Loc(0), Loc(0), Lexer.idPool("__fieldPostBlit")); |
0 | 765 dd.fbody = new ExpStatement(Loc(0), e); |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
766 postblits.shift(dd); |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
767 members.push(dd); |
0 | 768 dd.semantic(sc); |
769 } | |
770 | |
771 switch (postblits.dim) | |
772 { | |
773 case 0: | |
774 return null; | |
775 | |
776 case 1: | |
77
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
74
diff
changeset
|
777 return cast(FuncDeclaration)postblits[0]; |
0 | 778 |
779 default: | |
780 e = null; | |
77
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
74
diff
changeset
|
781 foreach(FuncDeclaration fd; postblits) |
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
74
diff
changeset
|
782 { |
0 | 783 Expression ex = new ThisExp(Loc(0)); |
784 ex = new DotVarExp(Loc(0), ex, fd, 0); | |
785 ex = new CallExp(Loc(0), ex); | |
786 e = Expression.combine(e, ex); | |
787 } | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
788 auto dd = new PostBlitDeclaration(Loc(0), Loc(0), Lexer.idPool("__aggrPostBlit")); |
0 | 789 dd.fbody = new ExpStatement(Loc(0), e); |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
790 members.push(dd); |
0 | 791 dd.semantic(sc); |
792 return dd; | |
793 } | |
794 } | |
795 } | |
796 | |
797 /******************************************* | |
798 * Build copy constructor for struct. | |
799 * Copy constructors are compiler generated only, and are only | |
800 * callable from the compiler. They are not user accessible. | |
801 * A copy constructor is: | |
802 * void cpctpr(ref S s) | |
803 * { | |
804 * *this = s; | |
805 * this.postBlit(); | |
806 * } | |
807 * This is done so: | |
808 * - postBlit() never sees uninitialized data | |
809 * - memcpy can be much more efficient than memberwise copy | |
810 * - no fields are overlooked | |
811 */ | |
812 FuncDeclaration buildCpCtor(Scope sc) | |
813 { | |
814 //printf("StructDeclaration.buildCpCtor() %s\n", toChars()); | |
815 FuncDeclaration fcp = null; | |
816 | |
817 /* Copy constructor is only necessary if there is a postblit function, | |
818 * otherwise the code generator will just do a bit copy. | |
819 */ | |
820 if (postblit) | |
821 { | |
129 | 822 //printf("generating cpctor\n"); |
0 | 823 |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
824 auto param = new Parameter(STC.STCref, type, Id.p, null); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
825 auto fparams = new Parameters; |
129 | 826 fparams.push(param); |
827 Type ftype = new TypeFunction(fparams, Type.tvoid, false, LINK.LINKd); | |
0 | 828 |
129 | 829 fcp = new FuncDeclaration(Loc(0), Loc(0), Id.cpctor, STC.STCundefined, ftype); |
0 | 830 |
129 | 831 // Build *this = p; |
832 Expression e = new ThisExp(Loc(0)); | |
0 | 833 version (STRUCTTHISREF) { |
834 } else { | |
129 | 835 e = new PtrExp(Loc(0), e); |
0 | 836 } |
129 | 837 AssignExp ea = new AssignExp(Loc(0), e, new IdentifierExp(Loc(0), Id.p)); |
838 ea.op = TOK.TOKblit; | |
839 Statement s = new ExpStatement(Loc(0), ea); | |
0 | 840 |
129 | 841 // Build postBlit(); |
842 e = new VarExp(Loc(0), postblit, 0); | |
843 e = new CallExp(Loc(0), e); | |
0 | 844 |
129 | 845 s = new CompoundStatement(Loc(0), s, new ExpStatement(Loc(0), e)); |
846 fcp.fbody = s; | |
0 | 847 |
129 | 848 members.push(fcp); |
0 | 849 |
129 | 850 sc = sc.push(); |
851 sc.stc = STC.STCundefined; | |
852 sc.linkage = LINK.LINKd; | |
0 | 853 |
129 | 854 fcp.semantic(sc); |
0 | 855 |
129 | 856 sc.pop(); |
0 | 857 } |
858 | |
859 return fcp; | |
860 } | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
861 } |
72 | 862 override void toDocBuffer(OutBuffer buf) |
0 | 863 { |
864 assert(false); | |
865 } | |
866 | |
72 | 867 override PROT getAccess(Dsymbol smember) // determine access to smember |
0 | 868 { |
869 assert(false); | |
870 } | |
871 | |
72 | 872 override void toObjFile(int multiobj) // compile to .obj file |
0 | 873 { |
874 //printf("StructDeclaration.toObjFile('%s')\n", toChars()); | |
875 | |
876 if (multiobj) | |
877 { | |
878 obj_append(this); | |
879 return; | |
880 } | |
881 | |
882 // Anonymous structs/unions only exist as part of others, | |
883 // do not output forward referenced structs's | |
884 if (!isAnonymous() && members) | |
885 { | |
886 if (global.params.symdebug) { | |
887 toDebug(); | |
888 } | |
889 | |
890 type.getTypeInfo(null); // generate TypeInfo | |
891 | |
892 if (true) | |
893 { | |
894 // Generate static initializer | |
895 toInitializer(); | |
896 | |
897 static if (false) { | |
898 sinit.Sclass = SC.SCcomdat; | |
899 } else { | |
900 if (inTemplateInstance()) | |
901 { | |
902 sinit.Sclass = SC.SCcomdat; | |
903 } | |
904 else | |
905 { | |
906 sinit.Sclass = SC.SCglobal; | |
907 } | |
908 } | |
909 sinit.Sfl = FL.FLdata; | |
910 | |
911 toDt(&sinit.Sdt); | |
912 | |
95 | 913 version (OMFOBJ) |
914 { | |
0 | 915 /* For OMF, common blocks aren't pulled in from the library. |
916 */ | |
917 /* ELF comdef's generate multiple | |
918 * definition errors for them from the gnu linker. | |
919 * Need to figure out how to generate proper comdef's for ELF. | |
920 */ | |
921 // See if we can convert a comdat to a comdef, | |
922 // which saves on exe file space. | |
95 | 923 if (0 && // causes multiple def problems with COMMON in one file and COMDAT in library |
924 sinit.Sclass == SCcomdat && | |
0 | 925 sinit.Sdt && |
926 sinit.Sdt.dt == DT.DT_azeros && | |
927 sinit.Sdt.DTnext == null && | |
928 !global.params.multiobj) | |
929 { | |
930 sinit.Sclass = SC.SCglobal; | |
931 sinit.Sdt.dt = DT.DT_common; | |
932 } | |
933 } | |
934 | |
935 version (ELFOBJ) { | |
936 sinit.Sseg = Segment.CDATA; | |
937 } | |
938 version (MACHOBJ) { | |
939 sinit.Sseg = Segment.DATA; | |
940 } | |
941 outdata(sinit); | |
942 } | |
943 | |
944 // Put out the members | |
77
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
74
diff
changeset
|
945 foreach(Dsymbol member; members) |
0 | 946 member.toObjFile(0); |
947 } | |
948 } | |
949 | |
950 void toDt(dt_t** pdt) | |
951 { | |
952 uint offset; | |
953 dt_t* dt; | |
954 | |
955 //printf("StructDeclaration.toDt(), this='%s'\n", toChars()); | |
956 offset = 0; | |
957 | |
958 // Note equivalence of this loop to class's | |
79 | 959 for (uint i = 0; i < fields.dim; i++) |
0 | 960 { |
79 | 961 VarDeclaration v = cast(VarDeclaration)fields[i]; |
0 | 962 //printf("\tfield '%s' voffset %d, offset = %d\n", v.toChars(), v.offset, offset); |
963 dt = null; | |
964 int sz; | |
965 | |
966 if (v.storage_class & STC.STCref) | |
967 { | |
968 sz = PTRSIZE; | |
969 if (v.offset >= offset) | |
970 dtnzeros(&dt, sz); | |
971 } | |
972 else | |
973 { | |
974 sz = cast(uint)v.type.size(); | |
975 Initializer init = v.init; | |
976 if (init) | |
977 { | |
978 //printf("\t\thas initializer %s\n", init.toChars()); | |
979 ExpInitializer ei = init.isExpInitializer(); | |
980 Type tb = v.type.toBasetype(); | |
981 if (ei && tb.ty == TY.Tsarray) | |
982 (cast(TypeSArray)tb).toDtElem(&dt, ei.exp); | |
983 else | |
984 dt = init.toDt(); | |
985 } | |
986 else if (v.offset >= offset) | |
987 v.type.toDt(&dt); | |
988 } | |
989 if (dt) | |
990 { | |
991 if (v.offset < offset) | |
992 error("overlapping initialization for struct %s.%s", toChars(), v.toChars()); | |
993 else | |
994 { | |
995 if (offset < v.offset) | |
996 dtnzeros(pdt, v.offset - offset); | |
997 dtcat(pdt, dt); | |
998 offset = v.offset + sz; | |
999 } | |
1000 } | |
1001 } | |
1002 | |
1003 if (offset < structsize) | |
1004 dtnzeros(pdt, structsize - offset); | |
1005 | |
1006 dt_optimize(*pdt); | |
1007 } | |
1008 | |
1009 void toDebug() // to symbolic debug info | |
1010 { | |
1011 assert(false); | |
1012 } | |
1013 | |
72 | 1014 override StructDeclaration isStructDeclaration() { return this; } |
1015 } |