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