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