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