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