comparison dmd/VarDeclaration.d @ 0:10317f0c89a5

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