Mercurial > projects > ddmd
annotate dmd/VarDeclaration.d @ 157:b7b61140701d
* added all missing default cases in switch statements
+ Lexer.getDocComment
+ Lexer.combineComments
author | trass3r |
---|---|
date | Thu, 16 Sep 2010 01:34:10 +0200 |
parents | af1bebfd96a4 |
children | e3afd1303184 |
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) | |
157
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
199 { |
0 | 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 } | |
135 | 346 else if (type.isImmutable()) |
0 | 347 storage_class |= STC.STCimmutable; |
348 else if (type.isShared()) | |
349 storage_class |= STC.STCshared; | |
135 | 350 else if (type.isWild()) |
351 storage_class |= STC.STCwild; | |
0 | 352 |
353 if (isSynchronized()) | |
354 { | |
355 error("variable %s cannot be synchronized", toChars()); | |
356 } | |
357 else if (isOverride()) | |
358 { | |
359 error("override cannot be applied to variable"); | |
360 } | |
361 else if (isAbstract()) | |
362 { | |
363 error("abstract cannot be applied to variable"); | |
364 } | |
365 else if (storage_class & STC.STCfinal) | |
366 { | |
367 error("final cannot be applied to variable"); | |
368 } | |
369 | |
370 if (storage_class & (STC.STCstatic | STC.STCextern | STC.STCmanifest | STC.STCtemplateparameter | STC.STCtls | STC.STCgshared)) | |
371 { | |
372 } | |
373 else | |
374 { | |
375 AggregateDeclaration aad = sc.anonAgg; | |
376 if (!aad) | |
377 aad = parent.isAggregateDeclaration(); | |
378 if (aad) | |
379 { | |
380 ///version (DMDV2) { | |
381 assert(!(storage_class & (STC.STCextern | STC.STCstatic | STC.STCtls | STC.STCgshared))); | |
382 | |
383 if (storage_class & (STC.STCconst | STC.STCimmutable) && init) | |
384 { | |
385 if (!type.toBasetype().isTypeBasic()) | |
386 storage_class |= STC.STCstatic; | |
387 } | |
388 else | |
389 ///} | |
390 aad.addField(sc, this); | |
391 } | |
392 | |
393 InterfaceDeclaration id = parent.isInterfaceDeclaration(); | |
394 if (id) | |
395 { | |
396 error("field not allowed in interface"); | |
397 } | |
398 | |
399 /* Templates cannot add fields to aggregates | |
400 */ | |
401 TemplateInstance ti = parent.isTemplateInstance(); | |
402 if (ti) | |
403 { | |
404 // Take care of nested templates | |
405 while (1) | |
406 { | |
407 TemplateInstance ti2 = ti.tempdecl.parent.isTemplateInstance(); | |
408 if (!ti2) | |
409 break; | |
410 ti = ti2; | |
411 } | |
412 | |
413 // If it's a member template | |
414 AggregateDeclaration ad = ti.tempdecl.isMember(); | |
415 if (ad && storage_class != STC.STCundefined) | |
416 { | |
417 error("cannot use template to add field to aggregate '%s'", ad.toChars()); | |
418 } | |
419 } | |
420 } | |
421 | |
422 version (DMDV2) { | |
423 if ((storage_class & (STC.STCref | STC.STCparameter | STC.STCforeach)) == STC.STCref && ident != Id.This) | |
424 { | |
425 error("only parameters or foreach declarations can be ref"); | |
426 } | |
135 | 427 |
428 if ((storage_class & (STCstatic | STCextern | STCtls | STCgshared | STCmanifest) || | |
429 isDataseg()) && | |
430 type.hasWild()) | |
431 { | |
432 error("only fields, parameters or stack based variables can be inout"); | |
433 } | |
0 | 434 } |
435 | |
436 if (type.isauto() && !noauto) | |
437 { | |
438 if (storage_class & (STC.STCfield | STC.STCout | STC.STCref | STC.STCstatic | STC.STCmanifest | STC.STCtls | STC.STCgshared) || !fd) | |
439 { | |
440 error("globals, statics, fields, manifest constants, ref and out parameters cannot be scope"); | |
441 } | |
442 | |
443 if (!(storage_class & (STC.STCauto | STC.STCscope))) | |
444 { | |
445 if (!(storage_class & STC.STCparameter) && ident != Id.withSym) | |
446 error("reference to scope class must be scope"); | |
447 } | |
448 } | |
449 | |
135 | 450 if ((isConst() || isImmutable()) && !init && !fd) |
0 | 451 { |
452 // Initialize by constructor only | |
453 storage_class |= STC.STCctorinit; | |
454 } | |
455 | |
456 if (init) | |
457 storage_class |= STC.STCinit; // remember we had an explicit initializer | |
458 else if (storage_class & STC.STCmanifest) | |
459 error("manifest constants must have initializers"); | |
460 | |
461 TOK op = TOK.TOKconstruct; | |
462 if (!init && !sc.inunion && !isStatic() && fd && | |
463 (!(storage_class & (STC.STCfield | STC.STCin | STC.STCforeach | STC.STCparameter)) || (storage_class & STC.STCout)) && | |
464 type.size() != 0) | |
465 { | |
466 // Provide a default initializer | |
467 //printf("Providing default initializer for '%s'\n", toChars()); | |
468 if (type.ty == TY.Tstruct && | |
469 (cast(TypeStruct)type).sym.zeroInit) | |
470 { /* If a struct is all zeros, as a special case | |
471 * set it's initializer to the integer 0. | |
472 * In AssignExp.toElem(), we check for this and issue | |
473 * a memset() to initialize the struct. | |
474 * Must do same check in interpreter. | |
475 */ | |
476 Expression e = new IntegerExp(loc, 0, Type.tint32); | |
477 Expression e1; | |
478 e1 = new VarExp(loc, this); | |
479 e = new AssignExp(loc, e1, e); | |
480 e.op = TOK.TOKconstruct; | |
481 e.type = e1.type; // don't type check this, it would fail | |
482 init = new ExpInitializer(loc, e); | |
483 return; | |
484 } | |
485 else if (type.ty == TY.Ttypedef) | |
486 { TypeTypedef td = cast(TypeTypedef)type; | |
487 if (td.sym.init) | |
488 { init = td.sym.init; | |
489 ExpInitializer ie = init.isExpInitializer(); | |
490 if (ie) | |
491 // Make copy so we can modify it | |
492 init = new ExpInitializer(ie.loc, ie.exp); | |
493 } | |
494 else | |
495 init = getExpInitializer(); | |
496 } | |
497 else | |
498 { | |
499 init = getExpInitializer(); | |
500 } | |
501 // Default initializer is always a blit | |
502 op = TOK.TOKblit; | |
503 } | |
504 | |
505 if (init) | |
506 { | |
507 sc = sc.push(); | |
508 sc.stc &= ~(STC.STC_TYPECTOR | STC.STCpure | STC.STCnothrow | STC.STCref); | |
509 | |
510 ArrayInitializer ai = init.isArrayInitializer(); | |
511 if (ai && tb.ty == TY.Taarray) | |
512 { | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
513 Expression e = ai.toAssocArrayLiteral(); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
514 init = new ExpInitializer(e.loc, e); |
0 | 515 } |
516 | |
517 StructInitializer si = init.isStructInitializer(); | |
518 ExpInitializer ei = init.isExpInitializer(); | |
519 | |
520 // See if initializer is a NewExp that can be allocated on the stack | |
521 if (ei && isScope() && ei.exp.op == TOK.TOKnew) | |
522 { NewExp ne = cast(NewExp)ei.exp; | |
523 if (!(ne.newargs && ne.newargs.dim)) | |
524 { ne.onstack = 1; | |
525 onstack = 1; | |
526 if (type.isBaseOf(ne.newtype.semantic(loc, sc), null)) | |
527 onstack = 2; | |
528 } | |
529 } | |
530 | |
531 // If inside function, there is no semantic3() call | |
532 if (sc.func) | |
533 { | |
534 // If local variable, use AssignExp to handle all the various | |
535 // possibilities. | |
536 if (fd && | |
537 !(storage_class & (STC.STCmanifest | STC.STCstatic | STC.STCtls | STC.STCgshared | STC.STCextern)) && | |
538 !init.isVoidInitializer()) | |
539 { | |
540 //printf("fd = '%s', var = '%s'\n", fd.toChars(), toChars()); | |
541 if (!ei) | |
542 { | |
543 Expression e = init.toExpression(); | |
544 if (!e) | |
545 { | |
546 init = init.semantic(sc, type); | |
547 e = init.toExpression(); | |
548 if (!e) | |
549 { error("is not a static and cannot have static initializer"); | |
550 return; | |
551 } | |
552 } | |
553 ei = new ExpInitializer(init.loc, e); | |
554 init = ei; | |
555 } | |
556 | |
557 Expression e1 = new VarExp(loc, this); | |
558 | |
559 Type t = type.toBasetype(); | |
560 if (t.ty == TY.Tsarray && !(storage_class & (STC.STCref | STC.STCout))) | |
561 { | |
562 ei.exp = ei.exp.semantic(sc); | |
563 if (!ei.exp.implicitConvTo(type)) | |
564 { | |
565 int dim = cast(int)(cast(TypeSArray)t).dim.toInteger(); /// | |
566 // If multidimensional static array, treat as one large array | |
567 while (1) | |
568 { | |
569 t = t.nextOf().toBasetype(); | |
570 if (t.ty != TY.Tsarray) | |
571 break; | |
572 dim *= (cast(TypeSArray)t).dim.toInteger(); | |
573 e1.type = new TypeSArray(t.nextOf(), new IntegerExp(Loc(0), dim, Type.tindex)); | |
574 } | |
575 } | |
576 e1 = new SliceExp(loc, e1, null, null); | |
577 } | |
578 else if (t.ty == TY.Tstruct) | |
579 { | |
580 ei.exp = ei.exp.semantic(sc); | |
98 | 581 ei.exp = resolveProperties(sc, ei.exp); |
582 StructDeclaration sd = (cast(TypeStruct)t).sym; | |
583 version (DMDV2) | |
584 { | |
0 | 585 /* Look to see if initializer is a call to the constructor |
586 */ | |
587 if (sd.ctor && // there are constructors | |
588 ei.exp.type.ty == TY.Tstruct && // rvalue is the same struct | |
589 (cast(TypeStruct)ei.exp.type).sym == sd && | |
590 ei.exp.op == TOK.TOKstar) | |
591 { | |
592 /* Look for form of constructor call which is: | |
593 * *__ctmp.ctor(arguments...) | |
594 */ | |
595 PtrExp pe = cast(PtrExp)ei.exp; | |
596 if (pe.e1.op == TOK.TOKcall) | |
597 { CallExp ce = cast(CallExp)pe.e1; | |
598 if (ce.e1.op == TOK.TOKdotvar) | |
599 { DotVarExp dve = cast(DotVarExp)ce.e1; | |
600 if (dve.var.isCtorDeclaration()) | |
601 { /* It's a constructor call, currently constructing | |
602 * a temporary __ctmp. | |
603 */ | |
604 /* Before calling the constructor, initialize | |
605 * variable with a bit copy of the default | |
606 * initializer | |
607 */ | |
608 Expression e = new AssignExp(loc, new VarExp(loc, this), t.defaultInit(loc)); | |
609 e.op = TOK.TOKblit; | |
610 e.type = t; | |
611 ei.exp = new CommaExp(loc, e, ei.exp); | |
612 | |
613 /* Replace __ctmp being constructed with e1 | |
614 */ | |
615 dve.e1 = e1; | |
616 return; | |
617 } | |
618 } | |
619 } | |
620 } | |
621 } | |
622 if (!ei.exp.implicitConvTo(type)) | |
73 | 623 { |
103 | 624 Type ti = ei.exp.type.toBasetype(); |
98 | 625 // Look for constructor first |
626 if (sd.ctor && | |
627 /* Initializing with the same type is done differently | |
628 */ | |
629 !(ti.ty == Tstruct && t.toDsymbol(sc) == ti.toDsymbol(sc))) | |
630 { | |
631 // Rewrite as e1.ctor(arguments) | |
632 Expression ector = new DotIdExp(loc, e1, Id.ctor); | |
633 ei.exp = new CallExp(loc, ector, ei.exp); | |
634 } | |
635 else | |
73 | 636 /* Look for opCall |
637 * See bugzilla 2702 for more discussion | |
638 */ | |
98 | 639 |
73 | 640 // Don't cast away invariant or mutability in initializer |
641 if (search_function(sd, Id.call) && | |
642 /* Initializing with the same type is done differently | |
643 */ | |
644 !(ti.ty == Tstruct && t.toDsymbol(sc) == ti.toDsymbol(sc))) | |
645 { // Rewrite as e1.call(arguments) | |
646 Expression eCall = new DotIdExp(loc, e1, Id.call); | |
647 ei.exp = new CallExp(loc, eCall, ei.exp); | |
648 } | |
0 | 649 } |
650 } | |
651 ei.exp = new AssignExp(loc, e1, ei.exp); | |
652 ei.exp.op = op; | |
653 canassign++; | |
654 ei.exp = ei.exp.semantic(sc); | |
655 canassign--; | |
656 ei.exp.optimize(WANT.WANTvalue); | |
657 } | |
658 else | |
659 { | |
660 init = init.semantic(sc, type); | |
661 } | |
662 } | |
663 else if (storage_class & (STC.STCconst | STC.STCimmutable | STC.STCmanifest) || | |
135 | 664 type.isConst() || type.isImmutable() || |
98 | 665 parent.isAggregateDeclaration()) |
0 | 666 { |
667 /* Because we may need the results of a const declaration in a | |
668 * subsequent type, such as an array dimension, before semantic2() | |
669 * gets ordinarily run, try to run semantic2() now. | |
670 * Ignore failure. | |
671 */ | |
672 | |
673 if (!global.errors && !inferred) | |
674 { | |
675 uint errors = global.errors; | |
676 global.gag++; | |
677 //printf("+gag\n"); | |
678 Expression e; | |
679 Initializer i2 = init; | |
680 inuse++; | |
681 if (ei) | |
682 { | |
683 e = ei.exp.syntaxCopy(); | |
684 e = e.semantic(sc); | |
685 e = e.implicitCastTo(sc, type); | |
686 } | |
687 else if (si || ai) | |
688 { i2 = init.syntaxCopy(); | |
689 i2 = i2.semantic(sc, type); | |
690 } | |
691 inuse--; | |
692 global.gag--; | |
693 //printf("-gag\n"); | |
694 if (errors != global.errors) // if errors happened | |
695 { | |
696 if (global.gag == 0) | |
697 global.errors = errors; // act as if nothing happened | |
698 version (DMDV2) { | |
699 /* Save scope for later use, to try again | |
700 */ | |
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
|
701 scope_ = sc.clone(); |
0 | 702 scope_.setNoFree(); |
703 } | |
704 } | |
705 else if (ei) | |
706 { | |
707 if (isDataseg()) | |
708 /* static const/invariant does CTFE | |
709 */ | |
710 e = e.optimize(WANT.WANTvalue | WANT.WANTinterpret); | |
711 else | |
712 e = e.optimize(WANT.WANTvalue); | |
713 if (e.op == TOK.TOKint64 || e.op == TOK.TOKstring || e.op == TOK.TOKfloat64) | |
714 { | |
715 ei.exp = e; // no errors, keep result | |
716 } | |
717 ///version (DMDV2) { | |
718 else | |
719 { | |
720 /* Save scope for later use, to try again | |
721 */ | |
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
|
722 scope_ = sc.clone(); |
0 | 723 scope_.setNoFree(); |
724 } | |
725 ///} | |
726 } | |
727 else | |
728 init = i2; // no errors, keep result | |
729 } | |
730 } | |
731 sc = sc.pop(); | |
732 } | |
733 } | |
734 | |
72 | 735 override void semantic2(Scope sc) |
0 | 736 { |
737 //printf("VarDeclaration.semantic2('%s')\n", toChars()); | |
738 if (init && !toParent().isFuncDeclaration()) | |
739 { | |
740 inuse++; | |
741 static if (false) { | |
742 ExpInitializer ei = init.isExpInitializer(); | |
743 if (ei) | |
744 { | |
745 ei.exp.dump(0); | |
746 printf("type = %p\n", ei.exp.type); | |
747 } | |
748 } | |
749 init = init.semantic(sc, type); | |
750 inuse--; | |
751 } | |
752 } | |
753 | |
72 | 754 override string kind() |
0 | 755 { |
756 return "variable"; | |
757 } | |
758 | |
72 | 759 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) |
0 | 760 { |
68
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
53
diff
changeset
|
761 StorageClassDeclaration.stcToCBuffer(buf, storage_class); |
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 /* If changing, be sure and fix CompoundDeclarationStatement.toCBuffer() |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
53
diff
changeset
|
764 * too. |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
53
diff
changeset
|
765 */ |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
53
diff
changeset
|
766 if (type) |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
53
diff
changeset
|
767 type.toCBuffer(buf, ident, 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 buf.writestring(ident.toChars()); |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
53
diff
changeset
|
770 if (init) |
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.writestring(" = "); |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
53
diff
changeset
|
773 ///version (DMDV2) { |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
53
diff
changeset
|
774 ExpInitializer ie = init.isExpInitializer(); |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
53
diff
changeset
|
775 if (ie && (ie.exp.op == TOKconstruct || ie.exp.op == TOKblit)) |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
53
diff
changeset
|
776 (cast(AssignExp)ie.exp).e2.toCBuffer(buf, hgs); |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
53
diff
changeset
|
777 else |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
53
diff
changeset
|
778 ///} |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
53
diff
changeset
|
779 init.toCBuffer(buf, hgs); |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
53
diff
changeset
|
780 } |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
53
diff
changeset
|
781 buf.writeByte(';'); |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
53
diff
changeset
|
782 buf.writenl(); |
0 | 783 } |
784 | |
785 version (_DH) { | |
786 Type htype; | |
787 Initializer hinit; | |
788 } | |
72 | 789 override bool needThis() |
0 | 790 { |
791 //printf("VarDeclaration.needThis(%s, x%x)\n", toChars(), storage_class); | |
792 return (storage_class & STC.STCfield) != 0; | |
793 } | |
794 | |
72 | 795 override bool isImportedSymbol() |
0 | 796 { |
797 if (protection == PROT.PROTexport && !init && (storage_class & STC.STCstatic || parent.isModule())) | |
798 return true; | |
799 | |
800 return false; | |
801 } | |
802 | |
72 | 803 override bool isDataseg() |
0 | 804 { |
805 static if (false) { | |
806 printf("VarDeclaration.isDataseg(%p, '%s')\n", this, toChars()); | |
135 | 807 printf("%llx, isModule: %p, isTemplateInstance: %p\n", storage_class & (STC.STCstatic | STC.STCconst), parent.isModule(), parent.isTemplateInstance()); |
0 | 808 printf("parent = '%s'\n", parent.toChars()); |
809 } | |
810 if (storage_class & STC.STCmanifest) | |
811 return false; | |
812 | |
813 Dsymbol parent = this.toParent(); | |
814 if (!parent && !(storage_class & STC.STCstatic)) | |
815 { | |
816 error("forward referenced"); | |
817 type = Type.terror; | |
818 return false; | |
819 } | |
820 | |
821 return canTakeAddressOf() && (storage_class & (STC.STCstatic | STC.STCextern | STC.STCtls | STC.STCgshared) || toParent().isModule() || toParent().isTemplateInstance()); | |
822 } | |
823 | |
72 | 824 override bool isThreadlocal() |
0 | 825 { |
826 //printf("VarDeclaration.isThreadlocal(%p, '%s')\n", this, toChars()); | |
827 static if (false) { /// || TARGET_OSX | |
828 /* To be thread-local, must use the __thread storage class. | |
829 * BUG: OSX doesn't support thread local yet. | |
830 */ | |
831 return isDataseg() && (storage_class & (STC.STCtls | STC.STCconst | STC.STCimmutable | STC.STCshared | STC.STCgshared)) == STC.STCtls; | |
832 } else { | |
833 /* Data defaults to being thread-local. It is not thread-local | |
834 * if it is immutable, const or shared. | |
835 */ | |
836 bool i = isDataseg() && !(storage_class & (STC.STCimmutable | STC.STCconst | STC.STCshared | STC.STCgshared)); | |
837 //printf("\treturn %d\n", i); | |
838 return i; | |
839 } | |
840 } | |
841 | |
135 | 842 /******************************************** |
843 * Can variable be read and written by CTFE? | |
844 */ | |
845 | |
846 int isCTFE() | |
847 { | |
848 return (storage_class & STCctfe) || !isDataseg(); | |
849 } | |
850 | |
72 | 851 override bool hasPointers() |
0 | 852 { |
853 //printf("VarDeclaration.hasPointers() %s, ty = %d\n", toChars(), type.ty); | |
854 return (!isDataseg() && type.hasPointers()); | |
855 } | |
856 | |
857 version (DMDV2) { | |
858 bool canTakeAddressOf() | |
859 { | |
860 static if (false) { | |
861 /* Global variables and struct/class fields of the form: | |
862 * const int x = 3; | |
863 * are not stored and hence cannot have their address taken. | |
864 */ | |
135 | 865 if ((isConst() || isImmutable()) && (storage_class & STC.STCinit) && (!(storage_class & (STC.STCstatic | STC.STCextern)) || (storage_class & STC.STCfield)) && |
0 | 866 (!parent || toParent().isModule() || toParent().isTemplateInstance()) && type.toBasetype().isTypeBasic()) |
867 { | |
868 return false; | |
869 } | |
870 } else { | |
871 if (storage_class & STC.STCmanifest) | |
872 return false; | |
873 } | |
874 return true; | |
875 } | |
876 | |
12
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 * 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
|
879 */ |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
880 bool needsAutoDtor() |
0 | 881 { |
12
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
882 //printf("VarDeclaration.needsAutoDtor() %s\n", toChars()); |
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 if (noauto || storage_class & STCnodtor) |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
885 return false; |
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 // Destructors for structs and arrays of structs |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
888 Type tv = type.toBasetype(); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
889 while (tv.ty == Tsarray) |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
890 { |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
891 TypeSArray ta = cast(TypeSArray)tv; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
892 tv = tv.nextOf().toBasetype(); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
893 } |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
894 if (tv.ty == Tstruct) |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
895 { |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
896 TypeStruct ts = cast(TypeStruct)tv; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
897 StructDeclaration sd = ts.sym; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
898 if (sd.dtor) |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
899 return true; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
900 } |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
901 |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
902 // Destructors for classes |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
903 if (storage_class & (STCauto | STCscope)) |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
904 { |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
905 if (type.isClassHandle()) |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
906 return true; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
907 } |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
908 return false; |
0 | 909 } |
910 } | |
911 | |
912 /****************************************** | |
913 * If a variable has an auto destructor call, return call for it. | |
914 * Otherwise, return null. | |
915 */ | |
916 Expression callAutoDtor(Scope sc) | |
917 { | |
918 Expression e = null; | |
919 | |
920 //printf("VarDeclaration.callAutoDtor() %s\n", toChars()); | |
921 | |
922 if (noauto || storage_class & STC.STCnodtor) | |
923 return null; | |
924 | |
925 // Destructors for structs and arrays of structs | |
926 bool array = false; | |
927 Type tv = type.toBasetype(); | |
928 while (tv.ty == TY.Tsarray) | |
929 { | |
930 TypeSArray ta = cast(TypeSArray)tv; | |
931 array = true; | |
932 tv = tv.nextOf().toBasetype(); | |
933 } | |
934 if (tv.ty == TY.Tstruct) | |
935 { | |
936 TypeStruct ts = cast(TypeStruct)tv; | |
937 StructDeclaration sd = ts.sym; | |
938 if (sd.dtor) | |
939 { | |
940 if (array) | |
941 { | |
942 // Typeinfo.destroy(cast(void*)&v); | |
943 Expression ea = new SymOffExp(loc, this, 0, 0); | |
944 ea = new CastExp(loc, ea, Type.tvoid.pointerTo()); | |
945 Expressions args = new Expressions(); | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
77
diff
changeset
|
946 args.push(ea); |
0 | 947 |
948 Expression et = type.getTypeInfo(sc); | |
949 et = new DotIdExp(loc, et, Id.destroy); | |
950 | |
951 e = new CallExp(loc, et, args); | |
952 } | |
953 else | |
954 { | |
955 e = new VarExp(loc, this); | |
956 e = new DotVarExp(loc, e, sd.dtor, 0); | |
957 e = new CallExp(loc, e); | |
958 } | |
959 return e; | |
960 } | |
961 } | |
962 | |
963 // Destructors for classes | |
964 if (storage_class & (STC.STCauto | STC.STCscope)) | |
965 { | |
966 for (ClassDeclaration cd = type.isClassHandle(); cd; cd = cd.baseClass) | |
967 { | |
968 /* We can do better if there's a way with onstack | |
969 * classes to determine if there's no way the monitor | |
970 * could be set. | |
971 */ | |
972 //if (cd.isInterfaceDeclaration()) | |
973 //error("interface %s cannot be scope", cd.toChars()); | |
974 if (1 || onstack || cd.dtors.dim) // if any destructors | |
975 { | |
976 // delete this; | |
977 Expression ec = new VarExp(loc, this); | |
978 e = new DeleteExp(loc, ec); | |
979 e.type = Type.tvoid; | |
980 break; | |
981 } | |
982 } | |
983 } | |
984 return e; | |
985 } | |
986 | |
987 /**************************** | |
988 * Get ExpInitializer for a variable, if there is one. | |
989 */ | |
990 ExpInitializer getExpInitializer() | |
991 { | |
992 ExpInitializer ei; | |
993 | |
994 if (init) | |
995 ei = init.isExpInitializer(); | |
996 else | |
997 { | |
998 Expression e = type.defaultInit(loc); | |
999 if (e) | |
1000 ei = new ExpInitializer(loc, e); | |
1001 else | |
1002 ei = null; | |
1003 } | |
1004 return ei; | |
1005 } | |
1006 | |
1007 /******************************************* | |
1008 * If variable has a constant expression initializer, get it. | |
1009 * Otherwise, return null. | |
1010 */ | |
1011 Expression getConstInitializer() | |
1012 { | |
135 | 1013 if ((isConst() || isImmutable() || storage_class & STC.STCmanifest) && storage_class & STC.STCinit) |
0 | 1014 { |
1015 ExpInitializer ei = getExpInitializer(); | |
1016 if (ei) | |
1017 return ei.exp; | |
1018 } | |
1019 | |
1020 return null; | |
1021 } | |
1022 | |
72 | 1023 override void checkCtorConstInit() |
0 | 1024 { |
1025 static if (false) { /* doesn't work if more than one static ctor */ | |
1026 if (ctorinit == 0 && isCtorinit() && !(storage_class & STCfield)) | |
1027 error("missing initializer in static constructor for const variable"); | |
1028 } | |
1029 } | |
1030 | |
1031 /************************************ | |
1032 * Check to see if this variable is actually in an enclosing function | |
1033 * rather than the current one. | |
1034 */ | |
1035 void checkNestedReference(Scope sc, Loc loc) | |
1036 { | |
1037 if (parent && !isDataseg() && parent != sc.parent && !(storage_class & STC.STCmanifest)) | |
1038 { | |
1039 // The function that this variable is in | |
1040 FuncDeclaration fdv = toParent().isFuncDeclaration(); | |
1041 // The current function | |
1042 FuncDeclaration fdthis = sc.parent.isFuncDeclaration(); | |
1043 | |
1044 if (fdv && fdthis && fdv !is fdthis) | |
1045 { | |
1046 if (loc.filename) | |
1047 fdthis.getLevel(loc, fdv); | |
1048 | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
77
diff
changeset
|
1049 foreach (f; nestedrefs) |
0 | 1050 { |
1051 if (f == fdthis) | |
1052 goto L1; | |
1053 } | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
73
diff
changeset
|
1054 nestedrefs.push(fdthis); |
0 | 1055 L1: ; |
1056 | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
77
diff
changeset
|
1057 foreach (s; fdv.closureVars) |
0 | 1058 { |
1059 if (s == this) | |
1060 goto L2; | |
1061 } | |
1062 | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
73
diff
changeset
|
1063 fdv.closureVars.push(this); |
0 | 1064 L2: ; |
1065 | |
1066 //printf("fdthis is %s\n", fdthis.toChars()); | |
1067 //printf("var %s in function %s is nested ref\n", toChars(), fdv.toChars()); | |
1068 } | |
1069 } | |
1070 } | |
1071 | |
72 | 1072 override Dsymbol toAlias() |
0 | 1073 { |
1074 //printf("VarDeclaration::toAlias('%s', this = %p, aliassym = %p)\n", toChars(), this, aliassym); | |
1075 assert(this !is aliassym); | |
1076 return aliassym ? aliassym.toAlias() : this; | |
1077 } | |
1078 | |
72 | 1079 override Symbol* toSymbol() |
0 | 1080 { |
1081 //printf("VarDeclaration.toSymbol(%s)\n", toChars()); | |
1082 //if (needThis()) *(char*)0=0; | |
1083 assert(!needThis()); | |
1084 if (!csym) | |
1085 { | |
1086 Symbol* s; | |
1087 TYPE* t; | |
1088 string id; | |
1089 | |
1090 if (isDataseg()) | |
1091 id = mangle(); | |
1092 else | |
1093 id = ident.toChars(); | |
1094 | |
1095 s = symbol_calloc(toStringz(id)); | |
1096 | |
1097 if (storage_class & (STC.STCout | STC.STCref)) | |
1098 { | |
1099 if (global.params.symdebug && storage_class & STC.STCparameter) | |
1100 { | |
1101 t = type_alloc(TYM.TYnptr); // should be TYref, but problems in back end | |
1102 t.Tnext = type.toCtype(); | |
1103 t.Tnext.Tcount++; | |
1104 } | |
1105 else | |
1106 t = type_fake(TYM.TYnptr); | |
1107 } | |
1108 else if (storage_class & STC.STClazy) | |
1109 t = type_fake(TYM.TYdelegate); // Tdelegate as C type | |
1110 else if (isParameter()) | |
1111 t = type.toCParamtype(); | |
1112 else | |
1113 t = type.toCtype(); | |
1114 | |
1115 t.Tcount++; | |
1116 | |
1117 if (isDataseg()) | |
1118 { | |
1119 if (isThreadlocal()) | |
1120 { | |
1121 /* Thread local storage | |
1122 */ | |
1123 TYPE* ts = t; | |
1124 ts.Tcount++; // make sure a different t is allocated | |
1125 type_setty(&t, t.Tty | mTY.mTYthread); | |
1126 ts.Tcount--; | |
1127 | |
1128 if (global.params.vtls) | |
1129 { | |
1130 string p = loc.toChars(); | |
1131 writef("%s: %s is thread local\n", p ? p : "", toChars()); | |
1132 } | |
1133 } | |
1134 | |
1135 s.Sclass = SC.SCextern; | |
1136 s.Sfl = FL.FLextern; | |
1137 slist_add(s); | |
1138 } | |
1139 else | |
1140 { | |
1141 s.Sclass = SC.SCauto; | |
1142 s.Sfl = FL.FLauto; | |
1143 | |
1144 if (nestedrefs.dim) | |
1145 { | |
1146 /* Symbol is accessed by a nested function. Make sure | |
1147 * it is not put in a register, and that the optimizer | |
1148 * assumes it is modified across function calls and pointer | |
1149 * dereferences. | |
1150 */ | |
1151 //printf("\tnested ref, not register\n"); | |
1152 type_setcv(&t, t.Tty | mTY.mTYvolatile); | |
1153 } | |
1154 } | |
1155 | |
1156 mangle_t m = 0; | |
1157 switch (linkage) | |
1158 { | |
1159 case LINK.LINKwindows: | |
1160 m = mTYman.mTYman_std; | |
1161 break; | |
1162 | |
1163 case LINK.LINKpascal: | |
1164 m = mTYman.mTYman_pas; | |
1165 break; | |
1166 | |
1167 case LINK.LINKc: | |
1168 m = mTYman.mTYman_c; | |
1169 break; | |
1170 | |
1171 case LINK.LINKd: | |
1172 m = mTYman.mTYman_d; | |
1173 break; | |
1174 | |
1175 case LINK.LINKcpp: | |
1176 m = mTYman.mTYman_cpp; | |
1177 break; | |
1178 | |
1179 default: | |
1180 writef("linkage = %d\n", linkage); | |
1181 assert(0); | |
1182 } | |
1183 type_setmangle(&t, m); | |
1184 s.Stype = t; | |
1185 | |
1186 csym = s; | |
1187 } | |
1188 return csym; | |
1189 } | |
1190 | |
72 | 1191 override void toObjFile(int multiobj) // compile to .obj file |
0 | 1192 { |
1193 Symbol* s; | |
1194 uint sz; | |
1195 Dsymbol parent; | |
1196 | |
1197 //printf("VarDeclaration.toObjFile(%p '%s' type=%s) protection %d\n", this, toChars(), type.toChars(), protection); | |
1198 //printf("\talign = %d\n", type.alignsize()); | |
1199 | |
1200 if (aliassym) | |
1201 { | |
1202 toAlias().toObjFile(0); | |
1203 return; | |
1204 } | |
1205 | |
1206 version (DMDV2) { | |
1207 // Do not store variables we cannot take the address of | |
1208 if (!canTakeAddressOf()) | |
1209 { | |
1210 return; | |
1211 } | |
1212 } | |
1213 | |
1214 if (isDataseg() && !(storage_class & STC.STCextern)) | |
1215 { | |
1216 s = toSymbol(); | |
1217 sz = cast(uint)type.size(); | |
1218 | |
1219 parent = this.toParent(); | |
1220 /// version (DMDV1) { /* private statics should still get a global symbol, in case | |
1221 /// * another module inlines a function that references it. | |
1222 /// */ | |
1223 /// if (/*protection == PROT.PROTprivate ||*/ | |
1224 /// !parent || parent.ident == null || parent.isFuncDeclaration()) | |
1225 /// { | |
1226 /// s.Sclass = SC.SCstatic; | |
1227 /// } | |
1228 /// else | |
1229 /// } | |
1230 { | |
1231 if (storage_class & STC.STCcomdat) | |
1232 s.Sclass = SC.SCcomdat; | |
1233 else | |
1234 s.Sclass = SC.SCglobal; | |
1235 | |
1236 do | |
1237 { | |
1238 /* Global template data members need to be in comdat's | |
1239 * in case multiple .obj files instantiate the same | |
1240 * template with the same types. | |
1241 */ | |
1242 if (parent.isTemplateInstance() && !parent.isTemplateMixin()) | |
1243 { | |
1244 version (DMDV1) { | |
1245 /* These symbol constants have already been copied, | |
1246 * so no reason to output them. | |
1247 * Note that currently there is no way to take | |
1248 * the address of such a const. | |
1249 */ | |
1250 if (isConst() && type.toBasetype().ty != TY.Tsarray && init && init.isExpInitializer()) | |
1251 return; | |
1252 } | |
1253 s.Sclass = SC.SCcomdat; | |
1254 break; | |
1255 } | |
1256 parent = parent.parent; | |
1257 } while (parent); | |
1258 } | |
1259 | |
1260 s.Sfl = FL.FLdata; | |
1261 | |
1262 if (init) | |
1263 { | |
1264 s.Sdt = init.toDt(); | |
1265 | |
1266 // Look for static array that is block initialized | |
1267 Type tb; | |
1268 ExpInitializer ie = init.isExpInitializer(); | |
1269 | |
1270 tb = type.toBasetype(); | |
1271 if (tb.ty == TY.Tsarray && ie | |
1272 && !tb.nextOf().equals(ie.exp.type.toBasetype().nextOf()) | |
1273 && ie.exp.implicitConvTo(tb.nextOf())) | |
1274 { | |
1275 int dim = cast(int)(cast(TypeSArray)tb).dim.toInteger(); | |
1276 | |
1277 // Duplicate Sdt 'dim-1' times, as we already have the first one | |
1278 while (--dim > 0) | |
1279 { | |
1280 ie.exp.toDt(&s.Sdt); | |
1281 } | |
1282 } | |
1283 } | |
1284 else if (storage_class & STC.STCextern) | |
1285 { | |
1286 s.Sclass = SC.SCextern; | |
1287 s.Sfl = FL.FLextern; | |
1288 s.Sdt = null; | |
1289 // BUG: if isExport(), shouldn't we make it dllimport? | |
1290 return; | |
1291 } | |
1292 else | |
1293 { | |
1294 type.toDt(&s.Sdt); | |
1295 } | |
1296 dt_optimize(s.Sdt); | |
1297 | |
1298 // See if we can convert a comdat to a comdef, | |
1299 // which saves on exe file space. | |
1300 if (s.Sclass == SC.SCcomdat && | |
1301 s.Sdt && | |
1302 s.Sdt.dt == DT.DT_azeros && | |
1303 s.Sdt.DTnext is null && | |
1304 !isThreadlocal()) | |
1305 { | |
1306 s.Sclass = SC.SCglobal; | |
1307 s.Sdt.dt = DT.DT_common; | |
1308 } | |
1309 | |
1310 version (ELFOBJ_OR_MACHOBJ) { // Burton | |
1311 if (s.Sdt && s.Sdt.dt == DT.DT_azeros && s.Sdt.DTnext is null) | |
1312 s.Sseg = Segment.UDATA; | |
1313 else | |
1314 s.Sseg = Segment.DATA; | |
1315 } | |
1316 if (sz) | |
1317 { | |
1318 outdata(s); | |
1319 if (isExport()) | |
1320 obj_export(s, 0); | |
1321 } | |
1322 } | |
1323 } | |
1324 | |
72 | 1325 override int cvMember(ubyte* p) |
0 | 1326 { |
1327 assert(false); | |
1328 } | |
1329 | |
1330 // Eliminate need for dynamic_cast | |
72 | 1331 override VarDeclaration isVarDeclaration() { return this; } |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
1332 } |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
77
diff
changeset
|
1333 |
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
77
diff
changeset
|
1334 alias Vector!VarDeclaration VarDeclarations; |