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