Mercurial > projects > ddmd
annotate dmd/VarDeclaration.d @ 130:60bb0fe4563e
dmdfe 2.037 first main iteration
author | Eldar Insafutdinov <e.insafutdinov@gmail.com> |
---|---|
date | Thu, 09 Sep 2010 22:51:44 +0100 |
parents | e28b18c23469 |
children | af1bebfd96a4 |
rev | line source |
---|---|
0 | 1 module dmd.VarDeclaration; |
2 | |
114 | 3 import dmd.common; |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
77
diff
changeset
|
4 import dmd.Array; |
0 | 5 import dmd.Declaration; |
6 import dmd.SliceExp; | |
7 import dmd.ClassDeclaration; | |
8 import dmd.DeleteExp; | |
9 import dmd.SymOffExp; | |
10 import dmd.DotIdExp; | |
11 import dmd.PtrExp; | |
12 import dmd.CallExp; | |
13 import dmd.DotVarExp; | |
14 import dmd.CommaExp; | |
15 import dmd.CastExp; | |
16 import dmd.WANT; | |
17 import dmd.StructDeclaration; | |
68
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
53
diff
changeset
|
18 import dmd.StorageClassDeclaration; |
0 | 19 import dmd.DsymbolExp; |
20 import dmd.TypeSArray; | |
21 import dmd.IntegerExp; | |
22 import dmd.VarExp; | |
23 import dmd.AssignExp; | |
24 import dmd.TypeTypedef; | |
25 import dmd.ArrayInitializer; | |
26 import dmd.StructInitializer; | |
27 import dmd.NewExp; | |
28 import dmd.TupleDeclaration; | |
29 import dmd.AggregateDeclaration; | |
30 import dmd.InterfaceDeclaration; | |
31 import dmd.TemplateInstance; | |
32 import dmd.Id; | |
33 import dmd.Initializer; | |
34 import dmd.TypeStruct; | |
35 import dmd.TypeTuple; | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
36 import dmd.Parameter; |
0 | 37 import dmd.ExpInitializer; |
38 import dmd.ArrayTypes; | |
39 import dmd.Dsymbol; | |
40 import dmd.Expression; | |
41 import dmd.Loc; | |
42 import dmd.STC; | |
43 import dmd.TOK; | |
44 import dmd.TupleExp; | |
45 import dmd.Global; | |
22
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
16
diff
changeset
|
46 import dmd.Module; |
0 | 47 import dmd.FuncDeclaration; |
48 import dmd.Type; | |
49 import dmd.TY; | |
50 import dmd.LINK; | |
51 import dmd.Scope; | |
52 import dmd.Identifier; | |
53 import dmd.OutBuffer; | |
54 import dmd.HdrGenState; | |
55 import dmd.PROT; | |
73 | 56 import dmd.expression.Util; |
0 | 57 |
58 import dmd.backend.Symbol; | |
59 import dmd.backend.TYM; | |
60 import dmd.backend.FL; | |
61 import dmd.backend.DT; | |
62 import dmd.backend.mTY; | |
63 import dmd.backend.SC; | |
64 import dmd.backend.mTYman; | |
65 import dmd.backend.TYPE; | |
66 import dmd.backend.Util; | |
67 import dmd.backend.LIST; | |
68 | |
69 import std.stdio : writef; | |
70 import std.string : toStringz; | |
71 | |
72 class VarDeclaration : Declaration | |
73 { | |
74 Initializer init; | |
75 uint offset; | |
76 bool noauto; // no auto semantics | |
77 version (DMDV2) { | |
98 | 78 FuncDeclarations nestedrefs; // referenced by these lexically nested functions |
79 bool isargptr = false; // if parameter that _argptr points to | |
0 | 80 } else { |
81 int nestedref; // referenced by a lexically nested function | |
82 } | |
83 int ctorinit; // it has been initialized in a ctor | |
84 int onstack; // 1: it has been allocated on the stack | |
85 // 2: on stack, run destructor anyway | |
86 int canassign; // it can be assigned to | |
87 Dsymbol aliassym; // if redone as alias to another symbol | |
88 Expression value; // when interpreting, this is the value | |
89 // (null if value not determinable) | |
90 version (DMDV2) { | |
91 VarDeclaration rundtor; // if !null, rundtor is tested at runtime to see | |
92 // if the destructor should be run. Used to prevent | |
93 // dtor calls on postblitted vars | |
94 } | |
95 | |
96 this(Loc loc, Type type, Identifier id, Initializer init) | |
97 { | |
98 super(id); | |
99 | |
98 | 100 debug |
101 { | |
0 | 102 if (!type && !init) |
103 { | |
104 writef("VarDeclaration('%s')\n", id.toChars()); | |
105 //*(char*)0=0; | |
106 } | |
107 } | |
108 assert(type || init); | |
109 this.type = type; | |
110 this.init = init; | |
98 | 111 version(_DH) |
112 { | |
113 this.htype = null; | |
114 this.hinit = null; | |
115 } | |
0 | 116 this.loc = loc; |
117 | |
98 | 118 /* TODO: |
119 #if DMDV1 | |
120 nestedref = 0; | |
121 #endif | |
122 ctorinit = 0; | |
123 aliassym = NULL; | |
124 onstack = 0; | |
125 canassign = 0; | |
126 value = NULL; | |
127 rundtor = NULL; | |
128 */ | |
0 | 129 nestedrefs = new FuncDeclarations(); |
130 } | |
131 | |
72 | 132 override Dsymbol syntaxCopy(Dsymbol s) |
0 | 133 { |
134 //printf("VarDeclaration.syntaxCopy(%s)\n", toChars()); | |
135 | |
136 VarDeclaration sv; | |
137 if (s) | |
138 { | |
139 sv = cast(VarDeclaration)s; | |
140 } | |
141 else | |
142 { | |
143 Initializer init = null; | |
144 if (this.init) | |
145 { | |
146 init = this.init.syntaxCopy(); | |
147 //init.isExpInitializer().exp.print(); | |
148 //init.isExpInitializer().exp.dump(0); | |
149 } | |
150 | |
151 sv = new VarDeclaration(loc, type ? type.syntaxCopy() : null, ident, init); | |
152 sv.storage_class = storage_class; | |
153 } | |
154 | |
155 version (_DH) { | |
156 // Syntax copy for header file | |
157 if (!htype) // Don't overwrite original | |
158 { | |
159 if (type) // Make copy for both old and new instances | |
160 { htype = type.syntaxCopy(); | |
161 sv.htype = type.syntaxCopy(); | |
162 } | |
163 } | |
164 else // Make copy of original for new instance | |
165 sv.htype = htype.syntaxCopy(); | |
166 if (!hinit) | |
167 { | |
168 if (init) | |
169 { | |
170 hinit = init.syntaxCopy(); | |
171 sv.hinit = init.syntaxCopy(); | |
172 } | |
173 } | |
174 else | |
175 sv.hinit = hinit.syntaxCopy(); | |
176 } | |
177 return sv; | |
178 } | |
179 | |
72 | 180 override void semantic(Scope sc) |
0 | 181 { |
182 static if (false) { | |
183 printf("VarDeclaration.semantic('%s', parent = '%s')\n", toChars(), sc.parent.toChars()); | |
184 printf(" type = %s\n", type ? type.toChars() : "null"); | |
185 printf(" stc = x%x\n", sc.stc); | |
186 printf(" storage_class = x%x\n", storage_class); | |
187 printf("linkage = %d\n", sc.linkage); | |
188 //if (strcmp(toChars(), "mul") == 0) halt(); | |
189 } | |
190 | |
191 storage_class |= sc.stc; | |
192 if (storage_class & STC.STCextern && init) | |
193 error("extern symbols cannot have initializers"); | |
194 | |
195 /* If auto type inference, do the inference | |
196 */ | |
197 int inferred = 0; | |
198 if (!type) | |
199 { | |
200 inuse++; | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
201 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
202 ArrayInitializer ai = init.isArrayInitializer(); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
203 if (ai) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
204 { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
205 Expression e; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
206 if (ai.isAssociativeArray()) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
207 e = ai.toAssocArrayLiteral(); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
208 else |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
209 e = init.toExpression(); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
210 init = new ExpInitializer(e.loc, e); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
211 type = init.inferType(sc); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
212 if (type.ty == TY.Tsarray) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
213 type = type.nextOf().arrayOf(); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
214 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
215 else |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
216 type = init.inferType(sc); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
217 |
0 | 218 inuse--; |
219 inferred = 1; | |
220 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
221 if (init.isArrayInitializer() && type.toBasetype().ty == TY.Tsarray) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
222 { // Prefer array literals to give a T[] type rather than a T[dim] |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
223 type = type.toBasetype().nextOf().arrayOf(); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
224 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
225 |
0 | 226 /* This is a kludge to support the existing syntax for RAII |
227 * declarations. | |
228 */ | |
229 storage_class &= ~STC.STCauto; | |
230 originalType = type; | |
231 } | |
232 else | |
233 { | |
234 if (!originalType) | |
235 originalType = type; | |
236 | |
237 type = type.semantic(loc, sc); | |
238 } | |
239 //printf(" semantic type = %s\n", type ? type.toChars() : "null"); | |
240 | |
241 type.checkDeprecated(loc, sc); | |
242 linkage = sc.linkage; | |
243 this.parent = sc.parent; | |
244 //printf("this = %p, parent = %p, '%s'\n", this, parent, parent.toChars()); | |
245 protection = sc.protection; | |
246 //printf("sc.stc = %x\n", sc.stc); | |
247 //printf("storage_class = x%x\n", storage_class); | |
248 | |
249 version (DMDV2) { | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
250 static if (true) { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
251 if (storage_class & STC.STCgshared && sc.func && sc.func.isSafe()) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
252 { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
253 error("__gshared not allowed in safe functions; use shared"); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
254 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
255 } else { |
0 | 256 if (storage_class & STC.STCgshared && global.params.safe && !sc.module_.safe) |
257 { | |
258 error("__gshared not allowed in safe mode; use shared"); | |
259 } | |
260 } | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
261 } |
0 | 262 |
263 Dsymbol parent = toParent(); | |
264 FuncDeclaration fd = parent.isFuncDeclaration(); | |
265 | |
266 Type tb = type.toBasetype(); | |
267 if (tb.ty == TY.Tvoid && !(storage_class & STC.STClazy)) | |
268 { error("voids have no value"); | |
269 type = Type.terror; | |
270 tb = type; | |
271 } | |
272 if (tb.ty == TY.Tfunction) | |
273 { error("cannot be declared to be a function"); | |
274 type = Type.terror; | |
275 tb = type; | |
276 } | |
277 if (tb.ty == TY.Tstruct) | |
278 { TypeStruct ts = cast(TypeStruct)tb; | |
279 | |
280 if (!ts.sym.members) | |
281 { | |
282 error("no definition of struct %s", ts.toChars()); | |
283 } | |
284 } | |
285 | |
286 if (tb.ty == TY.Ttuple) | |
287 { /* Instead, declare variables for each of the tuple elements | |
288 * and add those. | |
289 */ | |
290 TypeTuple tt = cast(TypeTuple)tb; | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
291 size_t nelems = Parameter.dim(tt.arguments); |
0 | 292 Objects exps = new Objects(); |
293 exps.setDim(nelems); | |
294 Expression ie = init ? init.toExpression() : null; | |
295 | |
296 for (size_t i = 0; i < nelems; i++) | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
297 { auto arg = Parameter.getNth(tt.arguments, i); |
0 | 298 |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
299 auto buf = new OutBuffer(); |
53 | 300 ///buf.printf("_%s_field_%zu", ident.toChars(), i); |
301 buf.printf("_%s_field_%s", ident.toChars(), i); | |
0 | 302 buf.writeByte(0); |
303 string name = buf.extractString(); | |
304 Identifier id = new Identifier(name, TOK.TOKidentifier); | |
305 | |
306 Expression einit = ie; | |
307 if (ie && ie.op == TOK.TOKtuple) | |
113
3482c73a991b
More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
110
diff
changeset
|
308 { einit = (cast(TupleExp)ie).exps[i]; |
0 | 309 } |
310 Initializer ti = init; | |
311 if (einit) | |
312 { ti = new ExpInitializer(einit.loc, einit); | |
313 } | |
314 | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
73
diff
changeset
|
315 auto v = new VarDeclaration(loc, arg.type, id, ti); |
0 | 316 //printf("declaring field %s of type %s\n", v.toChars(), v.type.toChars()); |
317 v.semantic(sc); | |
318 | |
319 if (sc.scopesym) | |
320 { //printf("adding %s to %s\n", v.toChars(), sc.scopesym.toChars()); | |
321 if (sc.scopesym.members) | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
73
diff
changeset
|
322 sc.scopesym.members.push(v); |
0 | 323 } |
324 | |
94
3a0b150c9841
Objects -> Vector!Object iteration 1
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
88
diff
changeset
|
325 auto e = new DsymbolExp(loc, v); |
3a0b150c9841
Objects -> Vector!Object iteration 1
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
88
diff
changeset
|
326 exps[i] = e; |
0 | 327 } |
94
3a0b150c9841
Objects -> Vector!Object iteration 1
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
88
diff
changeset
|
328 auto v2 = new TupleDeclaration(loc, ident, exps); |
0 | 329 v2.isexp = 1; |
330 aliassym = v2; | |
331 return; | |
332 } | |
333 | |
334 Lagain: | |
335 /* Storage class can modify the type | |
336 */ | |
337 type = type.addStorageClass(storage_class); | |
338 | |
339 /* Adjust storage class to reflect type | |
340 */ | |
341 if (type.isConst()) | |
342 { storage_class |= STC.STCconst; | |
343 if (type.isShared()) | |
344 storage_class |= STC.STCshared; | |
345 } | |
346 else if (type.isInvariant()) | |
347 storage_class |= STC.STCimmutable; | |
348 else if (type.isShared()) | |
349 storage_class |= STC.STCshared; | |
350 | |
351 if (isSynchronized()) | |
352 { | |
353 error("variable %s cannot be synchronized", toChars()); | |
354 } | |
355 else if (isOverride()) | |
356 { | |
357 error("override cannot be applied to variable"); | |
358 } | |
359 else if (isAbstract()) | |
360 { | |
361 error("abstract cannot be applied to variable"); | |
362 } | |
363 else if (storage_class & STC.STCfinal) | |
364 { | |
365 error("final cannot be applied to variable"); | |
366 } | |
367 | |
368 if (storage_class & (STC.STCstatic | STC.STCextern | STC.STCmanifest | STC.STCtemplateparameter | STC.STCtls | STC.STCgshared)) | |
369 { | |
370 } | |
371 else | |
372 { | |
373 AggregateDeclaration aad = sc.anonAgg; | |
374 if (!aad) | |
375 aad = parent.isAggregateDeclaration(); | |
376 if (aad) | |
377 { | |
378 ///version (DMDV2) { | |
379 assert(!(storage_class & (STC.STCextern | STC.STCstatic | STC.STCtls | STC.STCgshared))); | |
380 | |
381 if (storage_class & (STC.STCconst | STC.STCimmutable) && init) | |
382 { | |
383 if (!type.toBasetype().isTypeBasic()) | |
384 storage_class |= STC.STCstatic; | |
385 } | |
386 else | |
387 ///} | |
388 aad.addField(sc, this); | |
389 } | |
390 | |
391 InterfaceDeclaration id = parent.isInterfaceDeclaration(); | |
392 if (id) | |
393 { | |
394 error("field not allowed in interface"); | |
395 } | |
396 | |
397 /* Templates cannot add fields to aggregates | |
398 */ | |
399 TemplateInstance ti = parent.isTemplateInstance(); | |
400 if (ti) | |
401 { | |
402 // Take care of nested templates | |
403 while (1) | |
404 { | |
405 TemplateInstance ti2 = ti.tempdecl.parent.isTemplateInstance(); | |
406 if (!ti2) | |
407 break; | |
408 ti = ti2; | |
409 } | |
410 | |
411 // If it's a member template | |
412 AggregateDeclaration ad = ti.tempdecl.isMember(); | |
413 if (ad && storage_class != STC.STCundefined) | |
414 { | |
415 error("cannot use template to add field to aggregate '%s'", ad.toChars()); | |
416 } | |
417 } | |
418 } | |
419 | |
420 version (DMDV2) { | |
421 if ((storage_class & (STC.STCref | STC.STCparameter | STC.STCforeach)) == STC.STCref && ident != Id.This) | |
422 { | |
423 error("only parameters or foreach declarations can be ref"); | |
424 } | |
425 } | |
426 | |
427 if (type.isauto() && !noauto) | |
428 { | |
429 if (storage_class & (STC.STCfield | STC.STCout | STC.STCref | STC.STCstatic | STC.STCmanifest | STC.STCtls | STC.STCgshared) || !fd) | |
430 { | |
431 error("globals, statics, fields, manifest constants, ref and out parameters cannot be scope"); | |
432 } | |
433 | |
434 if (!(storage_class & (STC.STCauto | STC.STCscope))) | |
435 { | |
436 if (!(storage_class & STC.STCparameter) && ident != Id.withSym) | |
437 error("reference to scope class must be scope"); | |
438 } | |
439 } | |
440 | |
441 if ((isConst() || isInvariant()) && !init && !fd) | |
442 { | |
443 // Initialize by constructor only | |
444 storage_class |= STC.STCctorinit; | |
445 } | |
446 | |
447 if (init) | |
448 storage_class |= STC.STCinit; // remember we had an explicit initializer | |
449 else if (storage_class & STC.STCmanifest) | |
450 error("manifest constants must have initializers"); | |
451 | |
452 TOK op = TOK.TOKconstruct; | |
453 if (!init && !sc.inunion && !isStatic() && fd && | |
454 (!(storage_class & (STC.STCfield | STC.STCin | STC.STCforeach | STC.STCparameter)) || (storage_class & STC.STCout)) && | |
455 type.size() != 0) | |
456 { | |
457 // Provide a default initializer | |
458 //printf("Providing default initializer for '%s'\n", toChars()); | |
459 if (type.ty == TY.Tstruct && | |
460 (cast(TypeStruct)type).sym.zeroInit) | |
461 { /* If a struct is all zeros, as a special case | |
462 * set it's initializer to the integer 0. | |
463 * In AssignExp.toElem(), we check for this and issue | |
464 * a memset() to initialize the struct. | |
465 * Must do same check in interpreter. | |
466 */ | |
467 Expression e = new IntegerExp(loc, 0, Type.tint32); | |
468 Expression e1; | |
469 e1 = new VarExp(loc, this); | |
470 e = new AssignExp(loc, e1, e); | |
471 e.op = TOK.TOKconstruct; | |
472 e.type = e1.type; // don't type check this, it would fail | |
473 init = new ExpInitializer(loc, e); | |
474 return; | |
475 } | |
476 else if (type.ty == TY.Ttypedef) | |
477 { TypeTypedef td = cast(TypeTypedef)type; | |
478 if (td.sym.init) | |
479 { init = td.sym.init; | |
480 ExpInitializer ie = init.isExpInitializer(); | |
481 if (ie) | |
482 // Make copy so we can modify it | |
483 init = new ExpInitializer(ie.loc, ie.exp); | |
484 } | |
485 else | |
486 init = getExpInitializer(); | |
487 } | |
488 else | |
489 { | |
490 init = getExpInitializer(); | |
491 } | |
492 // Default initializer is always a blit | |
493 op = TOK.TOKblit; | |
494 } | |
495 | |
496 if (init) | |
497 { | |
498 sc = sc.push(); | |
499 sc.stc &= ~(STC.STC_TYPECTOR | STC.STCpure | STC.STCnothrow | STC.STCref); | |
500 | |
501 ArrayInitializer ai = init.isArrayInitializer(); | |
502 if (ai && tb.ty == TY.Taarray) | |
503 { | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
504 Expression e = ai.toAssocArrayLiteral(); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
505 init = new ExpInitializer(e.loc, e); |
0 | 506 } |
507 | |
508 StructInitializer si = init.isStructInitializer(); | |
509 ExpInitializer ei = init.isExpInitializer(); | |
510 | |
511 // See if initializer is a NewExp that can be allocated on the stack | |
512 if (ei && isScope() && ei.exp.op == TOK.TOKnew) | |
513 { NewExp ne = cast(NewExp)ei.exp; | |
514 if (!(ne.newargs && ne.newargs.dim)) | |
515 { ne.onstack = 1; | |
516 onstack = 1; | |
517 if (type.isBaseOf(ne.newtype.semantic(loc, sc), null)) | |
518 onstack = 2; | |
519 } | |
520 } | |
521 | |
522 // If inside function, there is no semantic3() call | |
523 if (sc.func) | |
524 { | |
525 // If local variable, use AssignExp to handle all the various | |
526 // possibilities. | |
527 if (fd && | |
528 !(storage_class & (STC.STCmanifest | STC.STCstatic | STC.STCtls | STC.STCgshared | STC.STCextern)) && | |
529 !init.isVoidInitializer()) | |
530 { | |
531 //printf("fd = '%s', var = '%s'\n", fd.toChars(), toChars()); | |
532 if (!ei) | |
533 { | |
534 Expression e = init.toExpression(); | |
535 if (!e) | |
536 { | |
537 init = init.semantic(sc, type); | |
538 e = init.toExpression(); | |
539 if (!e) | |
540 { error("is not a static and cannot have static initializer"); | |
541 return; | |
542 } | |
543 } | |
544 ei = new ExpInitializer(init.loc, e); | |
545 init = ei; | |
546 } | |
547 | |
548 Expression e1 = new VarExp(loc, this); | |
549 | |
550 Type t = type.toBasetype(); | |
551 if (t.ty == TY.Tsarray && !(storage_class & (STC.STCref | STC.STCout))) | |
552 { | |
553 ei.exp = ei.exp.semantic(sc); | |
554 if (!ei.exp.implicitConvTo(type)) | |
555 { | |
556 int dim = cast(int)(cast(TypeSArray)t).dim.toInteger(); /// | |
557 // If multidimensional static array, treat as one large array | |
558 while (1) | |
559 { | |
560 t = t.nextOf().toBasetype(); | |
561 if (t.ty != TY.Tsarray) | |
562 break; | |
563 dim *= (cast(TypeSArray)t).dim.toInteger(); | |
564 e1.type = new TypeSArray(t.nextOf(), new IntegerExp(Loc(0), dim, Type.tindex)); | |
565 } | |
566 } | |
567 e1 = new SliceExp(loc, e1, null, null); | |
568 } | |
569 else if (t.ty == TY.Tstruct) | |
570 { | |
571 ei.exp = ei.exp.semantic(sc); | |
98 | 572 ei.exp = resolveProperties(sc, ei.exp); |
573 StructDeclaration sd = (cast(TypeStruct)t).sym; | |
574 version (DMDV2) | |
575 { | |
0 | 576 /* Look to see if initializer is a call to the constructor |
577 */ | |
578 if (sd.ctor && // there are constructors | |
579 ei.exp.type.ty == TY.Tstruct && // rvalue is the same struct | |
580 (cast(TypeStruct)ei.exp.type).sym == sd && | |
581 ei.exp.op == TOK.TOKstar) | |
582 { | |
583 /* Look for form of constructor call which is: | |
584 * *__ctmp.ctor(arguments...) | |
585 */ | |
586 PtrExp pe = cast(PtrExp)ei.exp; | |
587 if (pe.e1.op == TOK.TOKcall) | |
588 { CallExp ce = cast(CallExp)pe.e1; | |
589 if (ce.e1.op == TOK.TOKdotvar) | |
590 { DotVarExp dve = cast(DotVarExp)ce.e1; | |
591 if (dve.var.isCtorDeclaration()) | |
592 { /* It's a constructor call, currently constructing | |
593 * a temporary __ctmp. | |
594 */ | |
595 /* Before calling the constructor, initialize | |
596 * variable with a bit copy of the default | |
597 * initializer | |
598 */ | |
599 Expression e = new AssignExp(loc, new VarExp(loc, this), t.defaultInit(loc)); | |
600 e.op = TOK.TOKblit; | |
601 e.type = t; | |
602 ei.exp = new CommaExp(loc, e, ei.exp); | |
603 | |
604 /* Replace __ctmp being constructed with e1 | |
605 */ | |
606 dve.e1 = e1; | |
607 return; | |
608 } | |
609 } | |
610 } | |
611 } | |
612 } | |
613 if (!ei.exp.implicitConvTo(type)) | |
73 | 614 { |
103 | 615 Type ti = ei.exp.type.toBasetype(); |
98 | 616 // Look for constructor first |
617 if (sd.ctor && | |
618 /* Initializing with the same type is done differently | |
619 */ | |
620 !(ti.ty == Tstruct && t.toDsymbol(sc) == ti.toDsymbol(sc))) | |
621 { | |
622 // Rewrite as e1.ctor(arguments) | |
623 Expression ector = new DotIdExp(loc, e1, Id.ctor); | |
624 ei.exp = new CallExp(loc, ector, ei.exp); | |
625 } | |
626 else | |
73 | 627 /* Look for opCall |
628 * See bugzilla 2702 for more discussion | |
629 */ | |
98 | 630 |
73 | 631 // Don't cast away invariant or mutability in initializer |
632 if (search_function(sd, Id.call) && | |
633 /* Initializing with the same type is done differently | |
634 */ | |
635 !(ti.ty == Tstruct && t.toDsymbol(sc) == ti.toDsymbol(sc))) | |
636 { // Rewrite as e1.call(arguments) | |
637 Expression eCall = new DotIdExp(loc, e1, Id.call); | |
638 ei.exp = new CallExp(loc, eCall, ei.exp); | |
639 } | |
0 | 640 } |
641 } | |
642 ei.exp = new AssignExp(loc, e1, ei.exp); | |
643 ei.exp.op = op; | |
644 canassign++; | |
645 ei.exp = ei.exp.semantic(sc); | |
646 canassign--; | |
647 ei.exp.optimize(WANT.WANTvalue); | |
648 } | |
649 else | |
650 { | |
651 init = init.semantic(sc, type); | |
652 } | |
653 } | |
654 else if (storage_class & (STC.STCconst | STC.STCimmutable | STC.STCmanifest) || | |
98 | 655 type.isConst() || type.isInvariant() || |
656 parent.isAggregateDeclaration()) | |
0 | 657 { |
658 /* Because we may need the results of a const declaration in a | |
659 * subsequent type, such as an array dimension, before semantic2() | |
660 * gets ordinarily run, try to run semantic2() now. | |
661 * Ignore failure. | |
662 */ | |
663 | |
664 if (!global.errors && !inferred) | |
665 { | |
666 uint errors = global.errors; | |
667 global.gag++; | |
668 //printf("+gag\n"); | |
669 Expression e; | |
670 Initializer i2 = init; | |
671 inuse++; | |
672 if (ei) | |
673 { | |
674 e = ei.exp.syntaxCopy(); | |
675 e = e.semantic(sc); | |
676 e = e.implicitCastTo(sc, type); | |
677 } | |
678 else if (si || ai) | |
679 { i2 = init.syntaxCopy(); | |
680 i2 = i2.semantic(sc, type); | |
681 } | |
682 inuse--; | |
683 global.gag--; | |
684 //printf("-gag\n"); | |
685 if (errors != global.errors) // if errors happened | |
686 { | |
687 if (global.gag == 0) | |
688 global.errors = errors; // act as if nothing happened | |
689 version (DMDV2) { | |
690 /* Save scope for later use, to try again | |
691 */ | |
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:
77
diff
changeset
|
692 scope_ = sc.clone(); |
0 | 693 scope_.setNoFree(); |
694 } | |
695 } | |
696 else if (ei) | |
697 { | |
698 if (isDataseg()) | |
699 /* static const/invariant does CTFE | |
700 */ | |
701 e = e.optimize(WANT.WANTvalue | WANT.WANTinterpret); | |
702 else | |
703 e = e.optimize(WANT.WANTvalue); | |
704 if (e.op == TOK.TOKint64 || e.op == TOK.TOKstring || e.op == TOK.TOKfloat64) | |
705 { | |
706 ei.exp = e; // no errors, keep result | |
707 } | |
708 ///version (DMDV2) { | |
709 else | |
710 { | |
711 /* Save scope for later use, to try again | |
712 */ | |
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:
77
diff
changeset
|
713 scope_ = sc.clone(); |
0 | 714 scope_.setNoFree(); |
715 } | |
716 ///} | |
717 } | |
718 else | |
719 init = i2; // no errors, keep result | |
720 } | |
721 } | |
722 sc = sc.pop(); | |
723 } | |
724 } | |
725 | |
72 | 726 override void semantic2(Scope sc) |
0 | 727 { |
728 //printf("VarDeclaration.semantic2('%s')\n", toChars()); | |
729 if (init && !toParent().isFuncDeclaration()) | |
730 { | |
731 inuse++; | |
732 static if (false) { | |
733 ExpInitializer ei = init.isExpInitializer(); | |
734 if (ei) | |
735 { | |
736 ei.exp.dump(0); | |
737 printf("type = %p\n", ei.exp.type); | |
738 } | |
739 } | |
740 init = init.semantic(sc, type); | |
741 inuse--; | |
742 } | |
743 } | |
744 | |
72 | 745 override string kind() |
0 | 746 { |
747 return "variable"; | |
748 } | |
749 | |
72 | 750 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) |
0 | 751 { |
68
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
53
diff
changeset
|
752 StorageClassDeclaration.stcToCBuffer(buf, storage_class); |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
53
diff
changeset
|
753 |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
53
diff
changeset
|
754 /* If changing, be sure and fix CompoundDeclarationStatement.toCBuffer() |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
53
diff
changeset
|
755 * too. |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
53
diff
changeset
|
756 */ |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
53
diff
changeset
|
757 if (type) |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
53
diff
changeset
|
758 type.toCBuffer(buf, ident, hgs); |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
53
diff
changeset
|
759 else |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
53
diff
changeset
|
760 buf.writestring(ident.toChars()); |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
53
diff
changeset
|
761 if (init) |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
53
diff
changeset
|
762 { |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
53
diff
changeset
|
763 buf.writestring(" = "); |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
53
diff
changeset
|
764 ///version (DMDV2) { |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
53
diff
changeset
|
765 ExpInitializer ie = init.isExpInitializer(); |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
53
diff
changeset
|
766 if (ie && (ie.exp.op == TOKconstruct || ie.exp.op == TOKblit)) |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
53
diff
changeset
|
767 (cast(AssignExp)ie.exp).e2.toCBuffer(buf, hgs); |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
53
diff
changeset
|
768 else |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
53
diff
changeset
|
769 ///} |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
53
diff
changeset
|
770 init.toCBuffer(buf, hgs); |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
53
diff
changeset
|
771 } |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
53
diff
changeset
|
772 buf.writeByte(';'); |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
53
diff
changeset
|
773 buf.writenl(); |
0 | 774 } |
775 | |
776 version (_DH) { | |
777 Type htype; | |
778 Initializer hinit; | |
779 } | |
72 | 780 override bool needThis() |
0 | 781 { |
782 //printf("VarDeclaration.needThis(%s, x%x)\n", toChars(), storage_class); | |
783 return (storage_class & STC.STCfield) != 0; | |
784 } | |
785 | |
72 | 786 override bool isImportedSymbol() |
0 | 787 { |
788 if (protection == PROT.PROTexport && !init && (storage_class & STC.STCstatic || parent.isModule())) | |
789 return true; | |
790 | |
791 return false; | |
792 } | |
793 | |
72 | 794 override bool isDataseg() |
0 | 795 { |
796 static if (false) { | |
797 printf("VarDeclaration.isDataseg(%p, '%s')\n", this, toChars()); | |
798 printf("%x, %p, %p\n", storage_class & (STC.STCstatic | STC.STCconst), parent.isModule(), parent.isTemplateInstance()); | |
799 printf("parent = '%s'\n", parent.toChars()); | |
800 } | |
801 if (storage_class & STC.STCmanifest) | |
802 return false; | |
803 | |
804 Dsymbol parent = this.toParent(); | |
805 if (!parent && !(storage_class & STC.STCstatic)) | |
806 { | |
807 error("forward referenced"); | |
808 type = Type.terror; | |
809 return false; | |
810 } | |
811 | |
812 return canTakeAddressOf() && (storage_class & (STC.STCstatic | STC.STCextern | STC.STCtls | STC.STCgshared) || toParent().isModule() || toParent().isTemplateInstance()); | |
813 } | |
814 | |
72 | 815 override bool isThreadlocal() |
0 | 816 { |
817 //printf("VarDeclaration.isThreadlocal(%p, '%s')\n", this, toChars()); | |
818 static if (false) { /// || TARGET_OSX | |
819 /* To be thread-local, must use the __thread storage class. | |
820 * BUG: OSX doesn't support thread local yet. | |
821 */ | |
822 return isDataseg() && (storage_class & (STC.STCtls | STC.STCconst | STC.STCimmutable | STC.STCshared | STC.STCgshared)) == STC.STCtls; | |
823 } else { | |
824 /* Data defaults to being thread-local. It is not thread-local | |
825 * if it is immutable, const or shared. | |
826 */ | |
827 bool i = isDataseg() && !(storage_class & (STC.STCimmutable | STC.STCconst | STC.STCshared | STC.STCgshared)); | |
828 //printf("\treturn %d\n", i); | |
829 return i; | |
830 } | |
831 } | |
832 | |
72 | 833 override bool hasPointers() |
0 | 834 { |
835 //printf("VarDeclaration.hasPointers() %s, ty = %d\n", toChars(), type.ty); | |
836 return (!isDataseg() && type.hasPointers()); | |
837 } | |
838 | |
839 version (DMDV2) { | |
840 bool canTakeAddressOf() | |
841 { | |
842 static if (false) { | |
843 /* Global variables and struct/class fields of the form: | |
844 * const int x = 3; | |
845 * are not stored and hence cannot have their address taken. | |
846 */ | |
847 if ((isConst() || isInvariant()) && (storage_class & STC.STCinit) && (!(storage_class & (STC.STCstatic | STC.STCextern)) || (storage_class & STC.STCfield)) && | |
848 (!parent || toParent().isModule() || toParent().isTemplateInstance()) && type.toBasetype().isTypeBasic()) | |
849 { | |
850 return false; | |
851 } | |
852 } else { | |
853 if (storage_class & STC.STCmanifest) | |
854 return false; | |
855 } | |
856 return true; | |
857 } | |
858 | |
12
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
859 /****************************************** |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
860 * Return TRUE if variable needs to call the destructor. |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
861 */ |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
862 bool needsAutoDtor() |
0 | 863 { |
12
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
864 //printf("VarDeclaration.needsAutoDtor() %s\n", toChars()); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
865 |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
866 if (noauto || storage_class & STCnodtor) |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
867 return false; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
868 |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
869 // Destructors for structs and arrays of structs |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
870 Type tv = type.toBasetype(); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
871 while (tv.ty == Tsarray) |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
872 { |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
873 TypeSArray ta = cast(TypeSArray)tv; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
874 tv = tv.nextOf().toBasetype(); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
875 } |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
876 if (tv.ty == Tstruct) |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
877 { |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
878 TypeStruct ts = cast(TypeStruct)tv; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
879 StructDeclaration sd = ts.sym; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
880 if (sd.dtor) |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
881 return true; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
882 } |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
883 |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
884 // Destructors for classes |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
885 if (storage_class & (STCauto | STCscope)) |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
886 { |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
887 if (type.isClassHandle()) |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
888 return true; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
889 } |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
890 return false; |
0 | 891 } |
892 } | |
893 | |
894 /****************************************** | |
895 * If a variable has an auto destructor call, return call for it. | |
896 * Otherwise, return null. | |
897 */ | |
898 Expression callAutoDtor(Scope sc) | |
899 { | |
900 Expression e = null; | |
901 | |
902 //printf("VarDeclaration.callAutoDtor() %s\n", toChars()); | |
903 | |
904 if (noauto || storage_class & STC.STCnodtor) | |
905 return null; | |
906 | |
907 // Destructors for structs and arrays of structs | |
908 bool array = false; | |
909 Type tv = type.toBasetype(); | |
910 while (tv.ty == TY.Tsarray) | |
911 { | |
912 TypeSArray ta = cast(TypeSArray)tv; | |
913 array = true; | |
914 tv = tv.nextOf().toBasetype(); | |
915 } | |
916 if (tv.ty == TY.Tstruct) | |
917 { | |
918 TypeStruct ts = cast(TypeStruct)tv; | |
919 StructDeclaration sd = ts.sym; | |
920 if (sd.dtor) | |
921 { | |
922 if (array) | |
923 { | |
924 // Typeinfo.destroy(cast(void*)&v); | |
925 Expression ea = new SymOffExp(loc, this, 0, 0); | |
926 ea = new CastExp(loc, ea, Type.tvoid.pointerTo()); | |
927 Expressions args = new Expressions(); | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
77
diff
changeset
|
928 args.push(ea); |
0 | 929 |
930 Expression et = type.getTypeInfo(sc); | |
931 et = new DotIdExp(loc, et, Id.destroy); | |
932 | |
933 e = new CallExp(loc, et, args); | |
934 } | |
935 else | |
936 { | |
937 e = new VarExp(loc, this); | |
938 e = new DotVarExp(loc, e, sd.dtor, 0); | |
939 e = new CallExp(loc, e); | |
940 } | |
941 return e; | |
942 } | |
943 } | |
944 | |
945 // Destructors for classes | |
946 if (storage_class & (STC.STCauto | STC.STCscope)) | |
947 { | |
948 for (ClassDeclaration cd = type.isClassHandle(); cd; cd = cd.baseClass) | |
949 { | |
950 /* We can do better if there's a way with onstack | |
951 * classes to determine if there's no way the monitor | |
952 * could be set. | |
953 */ | |
954 //if (cd.isInterfaceDeclaration()) | |
955 //error("interface %s cannot be scope", cd.toChars()); | |
956 if (1 || onstack || cd.dtors.dim) // if any destructors | |
957 { | |
958 // delete this; | |
959 Expression ec = new VarExp(loc, this); | |
960 e = new DeleteExp(loc, ec); | |
961 e.type = Type.tvoid; | |
962 break; | |
963 } | |
964 } | |
965 } | |
966 return e; | |
967 } | |
968 | |
969 /**************************** | |
970 * Get ExpInitializer for a variable, if there is one. | |
971 */ | |
972 ExpInitializer getExpInitializer() | |
973 { | |
974 ExpInitializer ei; | |
975 | |
976 if (init) | |
977 ei = init.isExpInitializer(); | |
978 else | |
979 { | |
980 Expression e = type.defaultInit(loc); | |
981 if (e) | |
982 ei = new ExpInitializer(loc, e); | |
983 else | |
984 ei = null; | |
985 } | |
986 return ei; | |
987 } | |
988 | |
989 /******************************************* | |
990 * If variable has a constant expression initializer, get it. | |
991 * Otherwise, return null. | |
992 */ | |
993 Expression getConstInitializer() | |
994 { | |
995 if ((isConst() || isInvariant() || storage_class & STC.STCmanifest) && storage_class & STC.STCinit) | |
996 { | |
997 ExpInitializer ei = getExpInitializer(); | |
998 if (ei) | |
999 return ei.exp; | |
1000 } | |
1001 | |
1002 return null; | |
1003 } | |
1004 | |
72 | 1005 override void checkCtorConstInit() |
0 | 1006 { |
1007 static if (false) { /* doesn't work if more than one static ctor */ | |
1008 if (ctorinit == 0 && isCtorinit() && !(storage_class & STCfield)) | |
1009 error("missing initializer in static constructor for const variable"); | |
1010 } | |
1011 } | |
1012 | |
1013 /************************************ | |
1014 * Check to see if this variable is actually in an enclosing function | |
1015 * rather than the current one. | |
1016 */ | |
1017 void checkNestedReference(Scope sc, Loc loc) | |
1018 { | |
1019 if (parent && !isDataseg() && parent != sc.parent && !(storage_class & STC.STCmanifest)) | |
1020 { | |
1021 // The function that this variable is in | |
1022 FuncDeclaration fdv = toParent().isFuncDeclaration(); | |
1023 // The current function | |
1024 FuncDeclaration fdthis = sc.parent.isFuncDeclaration(); | |
1025 | |
1026 if (fdv && fdthis && fdv !is fdthis) | |
1027 { | |
1028 if (loc.filename) | |
1029 fdthis.getLevel(loc, fdv); | |
1030 | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
77
diff
changeset
|
1031 foreach (f; nestedrefs) |
0 | 1032 { |
1033 if (f == fdthis) | |
1034 goto L1; | |
1035 } | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
73
diff
changeset
|
1036 nestedrefs.push(fdthis); |
0 | 1037 L1: ; |
1038 | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
77
diff
changeset
|
1039 foreach (s; fdv.closureVars) |
0 | 1040 { |
1041 if (s == this) | |
1042 goto L2; | |
1043 } | |
1044 | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
73
diff
changeset
|
1045 fdv.closureVars.push(this); |
0 | 1046 L2: ; |
1047 | |
1048 //printf("fdthis is %s\n", fdthis.toChars()); | |
1049 //printf("var %s in function %s is nested ref\n", toChars(), fdv.toChars()); | |
1050 } | |
1051 } | |
1052 } | |
1053 | |
72 | 1054 override Dsymbol toAlias() |
0 | 1055 { |
1056 //printf("VarDeclaration::toAlias('%s', this = %p, aliassym = %p)\n", toChars(), this, aliassym); | |
1057 assert(this !is aliassym); | |
1058 return aliassym ? aliassym.toAlias() : this; | |
1059 } | |
1060 | |
72 | 1061 override Symbol* toSymbol() |
0 | 1062 { |
1063 //printf("VarDeclaration.toSymbol(%s)\n", toChars()); | |
1064 //if (needThis()) *(char*)0=0; | |
1065 assert(!needThis()); | |
1066 if (!csym) | |
1067 { | |
1068 Symbol* s; | |
1069 TYPE* t; | |
1070 string id; | |
1071 | |
1072 if (isDataseg()) | |
1073 id = mangle(); | |
1074 else | |
1075 id = ident.toChars(); | |
1076 | |
1077 s = symbol_calloc(toStringz(id)); | |
1078 | |
1079 if (storage_class & (STC.STCout | STC.STCref)) | |
1080 { | |
1081 if (global.params.symdebug && storage_class & STC.STCparameter) | |
1082 { | |
1083 t = type_alloc(TYM.TYnptr); // should be TYref, but problems in back end | |
1084 t.Tnext = type.toCtype(); | |
1085 t.Tnext.Tcount++; | |
1086 } | |
1087 else | |
1088 t = type_fake(TYM.TYnptr); | |
1089 } | |
1090 else if (storage_class & STC.STClazy) | |
1091 t = type_fake(TYM.TYdelegate); // Tdelegate as C type | |
1092 else if (isParameter()) | |
1093 t = type.toCParamtype(); | |
1094 else | |
1095 t = type.toCtype(); | |
1096 | |
1097 t.Tcount++; | |
1098 | |
1099 if (isDataseg()) | |
1100 { | |
1101 if (isThreadlocal()) | |
1102 { | |
1103 /* Thread local storage | |
1104 */ | |
1105 TYPE* ts = t; | |
1106 ts.Tcount++; // make sure a different t is allocated | |
1107 type_setty(&t, t.Tty | mTY.mTYthread); | |
1108 ts.Tcount--; | |
1109 | |
1110 if (global.params.vtls) | |
1111 { | |
1112 string p = loc.toChars(); | |
1113 writef("%s: %s is thread local\n", p ? p : "", toChars()); | |
1114 } | |
1115 } | |
1116 | |
1117 s.Sclass = SC.SCextern; | |
1118 s.Sfl = FL.FLextern; | |
1119 slist_add(s); | |
1120 } | |
1121 else | |
1122 { | |
1123 s.Sclass = SC.SCauto; | |
1124 s.Sfl = FL.FLauto; | |
1125 | |
1126 if (nestedrefs.dim) | |
1127 { | |
1128 /* Symbol is accessed by a nested function. Make sure | |
1129 * it is not put in a register, and that the optimizer | |
1130 * assumes it is modified across function calls and pointer | |
1131 * dereferences. | |
1132 */ | |
1133 //printf("\tnested ref, not register\n"); | |
1134 type_setcv(&t, t.Tty | mTY.mTYvolatile); | |
1135 } | |
1136 } | |
1137 | |
1138 mangle_t m = 0; | |
1139 switch (linkage) | |
1140 { | |
1141 case LINK.LINKwindows: | |
1142 m = mTYman.mTYman_std; | |
1143 break; | |
1144 | |
1145 case LINK.LINKpascal: | |
1146 m = mTYman.mTYman_pas; | |
1147 break; | |
1148 | |
1149 case LINK.LINKc: | |
1150 m = mTYman.mTYman_c; | |
1151 break; | |
1152 | |
1153 case LINK.LINKd: | |
1154 m = mTYman.mTYman_d; | |
1155 break; | |
1156 | |
1157 case LINK.LINKcpp: | |
1158 m = mTYman.mTYman_cpp; | |
1159 break; | |
1160 | |
1161 default: | |
1162 writef("linkage = %d\n", linkage); | |
1163 assert(0); | |
1164 } | |
1165 type_setmangle(&t, m); | |
1166 s.Stype = t; | |
1167 | |
1168 csym = s; | |
1169 } | |
1170 return csym; | |
1171 } | |
1172 | |
72 | 1173 override void toObjFile(int multiobj) // compile to .obj file |
0 | 1174 { |
1175 Symbol* s; | |
1176 uint sz; | |
1177 Dsymbol parent; | |
1178 | |
1179 //printf("VarDeclaration.toObjFile(%p '%s' type=%s) protection %d\n", this, toChars(), type.toChars(), protection); | |
1180 //printf("\talign = %d\n", type.alignsize()); | |
1181 | |
1182 if (aliassym) | |
1183 { | |
1184 toAlias().toObjFile(0); | |
1185 return; | |
1186 } | |
1187 | |
1188 version (DMDV2) { | |
1189 // Do not store variables we cannot take the address of | |
1190 if (!canTakeAddressOf()) | |
1191 { | |
1192 return; | |
1193 } | |
1194 } | |
1195 | |
1196 if (isDataseg() && !(storage_class & STC.STCextern)) | |
1197 { | |
1198 s = toSymbol(); | |
1199 sz = cast(uint)type.size(); | |
1200 | |
1201 parent = this.toParent(); | |
1202 /// version (DMDV1) { /* private statics should still get a global symbol, in case | |
1203 /// * another module inlines a function that references it. | |
1204 /// */ | |
1205 /// if (/*protection == PROT.PROTprivate ||*/ | |
1206 /// !parent || parent.ident == null || parent.isFuncDeclaration()) | |
1207 /// { | |
1208 /// s.Sclass = SC.SCstatic; | |
1209 /// } | |
1210 /// else | |
1211 /// } | |
1212 { | |
1213 if (storage_class & STC.STCcomdat) | |
1214 s.Sclass = SC.SCcomdat; | |
1215 else | |
1216 s.Sclass = SC.SCglobal; | |
1217 | |
1218 do | |
1219 { | |
1220 /* Global template data members need to be in comdat's | |
1221 * in case multiple .obj files instantiate the same | |
1222 * template with the same types. | |
1223 */ | |
1224 if (parent.isTemplateInstance() && !parent.isTemplateMixin()) | |
1225 { | |
1226 version (DMDV1) { | |
1227 /* These symbol constants have already been copied, | |
1228 * so no reason to output them. | |
1229 * Note that currently there is no way to take | |
1230 * the address of such a const. | |
1231 */ | |
1232 if (isConst() && type.toBasetype().ty != TY.Tsarray && init && init.isExpInitializer()) | |
1233 return; | |
1234 } | |
1235 s.Sclass = SC.SCcomdat; | |
1236 break; | |
1237 } | |
1238 parent = parent.parent; | |
1239 } while (parent); | |
1240 } | |
1241 | |
1242 s.Sfl = FL.FLdata; | |
1243 | |
1244 if (init) | |
1245 { | |
1246 s.Sdt = init.toDt(); | |
1247 | |
1248 // Look for static array that is block initialized | |
1249 Type tb; | |
1250 ExpInitializer ie = init.isExpInitializer(); | |
1251 | |
1252 tb = type.toBasetype(); | |
1253 if (tb.ty == TY.Tsarray && ie | |
1254 && !tb.nextOf().equals(ie.exp.type.toBasetype().nextOf()) | |
1255 && ie.exp.implicitConvTo(tb.nextOf())) | |
1256 { | |
1257 int dim = cast(int)(cast(TypeSArray)tb).dim.toInteger(); | |
1258 | |
1259 // Duplicate Sdt 'dim-1' times, as we already have the first one | |
1260 while (--dim > 0) | |
1261 { | |
1262 ie.exp.toDt(&s.Sdt); | |
1263 } | |
1264 } | |
1265 } | |
1266 else if (storage_class & STC.STCextern) | |
1267 { | |
1268 s.Sclass = SC.SCextern; | |
1269 s.Sfl = FL.FLextern; | |
1270 s.Sdt = null; | |
1271 // BUG: if isExport(), shouldn't we make it dllimport? | |
1272 return; | |
1273 } | |
1274 else | |
1275 { | |
1276 type.toDt(&s.Sdt); | |
1277 } | |
1278 dt_optimize(s.Sdt); | |
1279 | |
1280 // See if we can convert a comdat to a comdef, | |
1281 // which saves on exe file space. | |
1282 if (s.Sclass == SC.SCcomdat && | |
1283 s.Sdt && | |
1284 s.Sdt.dt == DT.DT_azeros && | |
1285 s.Sdt.DTnext is null && | |
1286 !isThreadlocal()) | |
1287 { | |
1288 s.Sclass = SC.SCglobal; | |
1289 s.Sdt.dt = DT.DT_common; | |
1290 } | |
1291 | |
1292 version (ELFOBJ_OR_MACHOBJ) { // Burton | |
1293 if (s.Sdt && s.Sdt.dt == DT.DT_azeros && s.Sdt.DTnext is null) | |
1294 s.Sseg = Segment.UDATA; | |
1295 else | |
1296 s.Sseg = Segment.DATA; | |
1297 } | |
1298 if (sz) | |
1299 { | |
1300 outdata(s); | |
1301 if (isExport()) | |
1302 obj_export(s, 0); | |
1303 } | |
1304 } | |
1305 } | |
1306 | |
72 | 1307 override int cvMember(ubyte* p) |
0 | 1308 { |
1309 assert(false); | |
1310 } | |
1311 | |
1312 // Eliminate need for dynamic_cast | |
72 | 1313 override VarDeclaration isVarDeclaration() { return this; } |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
1314 } |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
77
diff
changeset
|
1315 |
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
77
diff
changeset
|
1316 alias Vector!VarDeclaration VarDeclarations; |