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