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