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