Mercurial > projects > ldc
annotate dmd/declaration.c @ 156:ccd07d9f2ce9 trunk
[svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
author | ChristianK |
---|---|
date | Thu, 01 May 2008 13:05:53 +0200 |
parents | 0ab29b838084 |
children | 5acec6b2eef8 |
rev | line source |
---|---|
1 | 1 |
2 // Compiler implementation of the D programming language | |
3 // Copyright (c) 1999-2007 by Digital Mars | |
4 // All Rights Reserved | |
5 // written by Walter Bright | |
6 // http://www.digitalmars.com | |
7 // License for redistribution is by either the Artistic License | |
8 // in artistic.txt, or the GNU General Public License in gnu.txt. | |
9 // See the included readme.txt for details. | |
10 | |
11 #include <stdio.h> | |
12 #include <assert.h> | |
13 | |
14 #include "init.h" | |
15 #include "declaration.h" | |
16 #include "attrib.h" | |
17 #include "mtype.h" | |
18 #include "template.h" | |
19 #include "scope.h" | |
20 #include "aggregate.h" | |
21 #include "module.h" | |
22 #include "id.h" | |
23 #include "expression.h" | |
24 #include "hdrgen.h" | |
25 | |
26 /********************************* Declaration ****************************/ | |
27 | |
28 Declaration::Declaration(Identifier *id) | |
29 : Dsymbol(id) | |
30 { | |
31 type = NULL; | |
130 | 32 originalType = NULL; |
1 | 33 storage_class = STCundefined; |
34 protection = PROTundefined; | |
50
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
35 linkage = LINKdefault; |
1 | 36 } |
37 | |
38 void Declaration::semantic(Scope *sc) | |
39 { | |
40 } | |
41 | |
42 char *Declaration::kind() | |
43 { | |
44 return "declaration"; | |
45 } | |
46 | |
47 unsigned Declaration::size(Loc loc) | |
48 { | |
49 assert(type); | |
50 return type->size(); | |
51 } | |
52 | |
53 int Declaration::isStaticConstructor() | |
54 { | |
55 return FALSE; | |
56 } | |
57 | |
58 int Declaration::isStaticDestructor() | |
59 { | |
60 return FALSE; | |
61 } | |
62 | |
63 int Declaration::isDelete() | |
64 { | |
65 return FALSE; | |
66 } | |
67 | |
68 int Declaration::isDataseg() | |
69 { | |
70 return FALSE; | |
71 } | |
72 | |
73 int Declaration::isCodeseg() | |
74 { | |
75 return FALSE; | |
76 } | |
77 | |
78 enum PROT Declaration::prot() | |
79 { | |
80 return protection; | |
81 } | |
82 | |
83 /********************************* TupleDeclaration ****************************/ | |
84 | |
85 TupleDeclaration::TupleDeclaration(Loc loc, Identifier *id, Objects *objects) | |
86 : Declaration(id) | |
87 { | |
88 this->type = NULL; | |
89 this->objects = objects; | |
90 this->isexp = 0; | |
91 this->tupletype = NULL; | |
92 } | |
93 | |
94 Dsymbol *TupleDeclaration::syntaxCopy(Dsymbol *s) | |
95 { | |
96 assert(0); | |
97 return NULL; | |
98 } | |
99 | |
100 char *TupleDeclaration::kind() | |
101 { | |
102 return "tuple"; | |
103 } | |
104 | |
105 Type *TupleDeclaration::getType() | |
106 { | |
107 /* If this tuple represents a type, return that type | |
108 */ | |
109 | |
110 //printf("TupleDeclaration::getType() %s\n", toChars()); | |
111 if (isexp) | |
112 return NULL; | |
113 if (!tupletype) | |
114 { | |
115 /* It's only a type tuple if all the Object's are types | |
116 */ | |
117 for (size_t i = 0; i < objects->dim; i++) | |
118 { Object *o = (Object *)objects->data[i]; | |
119 | |
120 if (o->dyncast() != DYNCAST_TYPE) | |
121 { | |
122 //printf("\tnot[%d], %p, %d\n", i, o, o->dyncast()); | |
123 return NULL; | |
124 } | |
125 } | |
126 | |
127 /* We know it's a type tuple, so build the TypeTuple | |
128 */ | |
129 Arguments *args = new Arguments(); | |
130 args->setDim(objects->dim); | |
131 OutBuffer buf; | |
132 for (size_t i = 0; i < objects->dim; i++) | |
133 { Type *t = (Type *)objects->data[i]; | |
134 | |
135 //printf("type = %s\n", t->toChars()); | |
136 #if 0 | |
137 buf.printf("_%s_%d", ident->toChars(), i); | |
138 char *name = (char *)buf.extractData(); | |
139 Identifier *id = new Identifier(name, TOKidentifier); | |
140 Argument *arg = new Argument(STCin, t, id, NULL); | |
141 #else | |
142 Argument *arg = new Argument(STCin, t, NULL, NULL); | |
143 #endif | |
144 args->data[i] = (void *)arg; | |
145 } | |
146 | |
147 tupletype = new TypeTuple(args); | |
148 } | |
149 | |
150 return tupletype; | |
151 } | |
152 | |
153 int TupleDeclaration::needThis() | |
154 { | |
155 //printf("TupleDeclaration::needThis(%s)\n", toChars()); | |
156 for (size_t i = 0; i < objects->dim; i++) | |
157 { Object *o = (Object *)objects->data[i]; | |
158 if (o->dyncast() == DYNCAST_EXPRESSION) | |
159 { Expression *e = (Expression *)o; | |
160 if (e->op == TOKdsymbol) | |
161 { DsymbolExp *ve = (DsymbolExp *)e; | |
162 Declaration *d = ve->s->isDeclaration(); | |
163 if (d && d->needThis()) | |
164 { | |
165 return 1; | |
166 } | |
167 } | |
168 } | |
169 } | |
170 return 0; | |
171 } | |
172 | |
173 /********************************* TypedefDeclaration ****************************/ | |
174 | |
175 TypedefDeclaration::TypedefDeclaration(Loc loc, Identifier *id, Type *basetype, Initializer *init) | |
176 : Declaration(id) | |
177 { | |
178 this->type = new TypeTypedef(this); | |
179 this->basetype = basetype->toBasetype(); | |
180 this->init = init; | |
181 #ifdef _DH | |
182 this->htype = NULL; | |
183 this->hbasetype = NULL; | |
184 #endif | |
185 this->sem = 0; | |
186 this->inuse = 0; | |
187 this->loc = loc; | |
188 this->sinit = NULL; | |
189 } | |
190 | |
191 Dsymbol *TypedefDeclaration::syntaxCopy(Dsymbol *s) | |
192 { | |
193 Type *basetype = this->basetype->syntaxCopy(); | |
194 | |
195 Initializer *init = NULL; | |
196 if (this->init) | |
197 init = this->init->syntaxCopy(); | |
198 | |
199 assert(!s); | |
200 TypedefDeclaration *st; | |
201 st = new TypedefDeclaration(loc, ident, basetype, init); | |
202 #ifdef _DH | |
203 // Syntax copy for header file | |
204 if (!htype) // Don't overwrite original | |
205 { if (type) // Make copy for both old and new instances | |
206 { htype = type->syntaxCopy(); | |
207 st->htype = type->syntaxCopy(); | |
208 } | |
209 } | |
210 else // Make copy of original for new instance | |
211 st->htype = htype->syntaxCopy(); | |
212 if (!hbasetype) | |
213 { if (basetype) | |
214 { hbasetype = basetype->syntaxCopy(); | |
215 st->hbasetype = basetype->syntaxCopy(); | |
216 } | |
217 } | |
218 else | |
219 st->hbasetype = hbasetype->syntaxCopy(); | |
220 #endif | |
221 return st; | |
222 } | |
223 | |
224 void TypedefDeclaration::semantic(Scope *sc) | |
225 { | |
226 //printf("TypedefDeclaration::semantic(%s) sem = %d\n", toChars(), sem); | |
227 if (sem == 0) | |
228 { sem = 1; | |
229 basetype = basetype->semantic(loc, sc); | |
230 sem = 2; | |
231 type = type->semantic(loc, sc); | |
232 if (sc->parent->isFuncDeclaration() && init) | |
233 semantic2(sc); | |
234 } | |
235 else if (sem == 1) | |
236 { | |
237 error("circular definition"); | |
238 } | |
239 } | |
240 | |
241 void TypedefDeclaration::semantic2(Scope *sc) | |
242 { | |
243 //printf("TypedefDeclaration::semantic2(%s) sem = %d\n", toChars(), sem); | |
244 if (sem == 2) | |
245 { sem = 3; | |
246 if (init) | |
247 { | |
248 init = init->semantic(sc, basetype); | |
249 | |
250 ExpInitializer *ie = init->isExpInitializer(); | |
251 if (ie) | |
252 { | |
253 if (ie->exp->type == basetype) | |
254 ie->exp->type = type; | |
255 } | |
256 } | |
257 } | |
258 } | |
259 | |
260 char *TypedefDeclaration::kind() | |
261 { | |
262 return "typedef"; | |
263 } | |
264 | |
265 Type *TypedefDeclaration::getType() | |
266 { | |
267 return type; | |
268 } | |
269 | |
270 void TypedefDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
271 { | |
272 buf->writestring("typedef "); | |
273 basetype->toCBuffer(buf, ident, hgs); | |
274 if (init) | |
275 { | |
276 buf->writestring(" = "); | |
277 init->toCBuffer(buf, hgs); | |
278 } | |
279 buf->writeByte(';'); | |
280 buf->writenl(); | |
281 } | |
282 | |
283 /********************************* AliasDeclaration ****************************/ | |
284 | |
285 AliasDeclaration::AliasDeclaration(Loc loc, Identifier *id, Type *type) | |
286 : Declaration(id) | |
287 { | |
288 //printf("AliasDeclaration(id = '%s', type = %p)\n", id->toChars(), type); | |
289 //printf("type = '%s'\n", type->toChars()); | |
290 this->loc = loc; | |
291 this->type = type; | |
292 this->aliassym = NULL; | |
293 #ifdef _DH | |
294 this->htype = NULL; | |
295 this->haliassym = NULL; | |
296 #endif | |
297 this->overnext = NULL; | |
298 this->inSemantic = 0; | |
299 assert(type); | |
300 } | |
301 | |
302 AliasDeclaration::AliasDeclaration(Loc loc, Identifier *id, Dsymbol *s) | |
303 : Declaration(id) | |
304 { | |
305 //printf("AliasDeclaration(id = '%s', s = %p)\n", id->toChars(), s); | |
306 assert(s != this); | |
307 this->loc = loc; | |
308 this->type = NULL; | |
309 this->aliassym = s; | |
310 #ifdef _DH | |
311 this->htype = NULL; | |
312 this->haliassym = NULL; | |
313 #endif | |
314 this->overnext = NULL; | |
315 this->inSemantic = 0; | |
316 assert(s); | |
317 } | |
318 | |
319 Dsymbol *AliasDeclaration::syntaxCopy(Dsymbol *s) | |
320 { | |
321 assert(!s); | |
322 AliasDeclaration *sa; | |
323 if (type) | |
324 sa = new AliasDeclaration(loc, ident, type->syntaxCopy()); | |
325 else | |
326 sa = new AliasDeclaration(loc, ident, aliassym->syntaxCopy(NULL)); | |
327 #ifdef _DH | |
328 // Syntax copy for header file | |
329 if (!htype) // Don't overwrite original | |
330 { if (type) // Make copy for both old and new instances | |
331 { htype = type->syntaxCopy(); | |
332 sa->htype = type->syntaxCopy(); | |
333 } | |
334 } | |
335 else // Make copy of original for new instance | |
336 sa->htype = htype->syntaxCopy(); | |
337 if (!haliassym) | |
338 { if (aliassym) | |
339 { haliassym = aliassym->syntaxCopy(s); | |
340 sa->haliassym = aliassym->syntaxCopy(s); | |
341 } | |
342 } | |
343 else | |
344 sa->haliassym = haliassym->syntaxCopy(s); | |
345 #endif | |
346 return sa; | |
347 } | |
348 | |
349 void AliasDeclaration::semantic(Scope *sc) | |
350 { | |
351 //printf("AliasDeclaration::semantic() %s\n", toChars()); | |
352 if (aliassym) | |
353 { | |
354 if (aliassym->isTemplateInstance()) | |
355 aliassym->semantic(sc); | |
356 return; | |
357 } | |
358 this->inSemantic = 1; | |
359 | |
360 if (storage_class & STCconst) | |
361 error("cannot be const"); | |
362 | |
363 storage_class |= sc->stc & STCdeprecated; | |
364 | |
365 // Given: | |
366 // alias foo.bar.abc def; | |
367 // it is not knowable from the syntax whether this is an alias | |
368 // for a type or an alias for a symbol. It is up to the semantic() | |
369 // pass to distinguish. | |
370 // If it is a type, then type is set and getType() will return that | |
371 // type. If it is a symbol, then aliassym is set and type is NULL - | |
372 // toAlias() will return aliasssym. | |
373 | |
374 Dsymbol *s; | |
375 Type *t; | |
376 Expression *e; | |
377 | |
378 /* This section is needed because resolve() will: | |
379 * const x = 3; | |
380 * alias x y; | |
381 * try to alias y to 3. | |
382 */ | |
383 s = type->toDsymbol(sc); | |
384 if (s) | |
385 goto L2; // it's a symbolic alias | |
386 | |
387 //printf("alias type is %s\n", type->toChars()); | |
388 type->resolve(loc, sc, &e, &t, &s); | |
389 if (s) | |
390 { | |
391 goto L2; | |
392 } | |
393 else if (e) | |
394 { | |
395 // Try to convert Expression to Dsymbol | |
396 if (e->op == TOKvar) | |
397 { s = ((VarExp *)e)->var; | |
398 goto L2; | |
399 } | |
400 else if (e->op == TOKfunction) | |
401 { s = ((FuncExp *)e)->fd; | |
402 goto L2; | |
403 } | |
404 else | |
405 { error("cannot alias an expression %s", e->toChars()); | |
406 t = e->type; | |
407 } | |
408 } | |
409 else if (t) | |
410 type = t; | |
411 if (overnext) | |
412 ScopeDsymbol::multiplyDefined(0, this, overnext); | |
413 this->inSemantic = 0; | |
414 return; | |
415 | |
416 L2: | |
417 //printf("alias is a symbol %s %s\n", s->kind(), s->toChars()); | |
418 type = NULL; | |
419 VarDeclaration *v = s->isVarDeclaration(); | |
420 if (v && v->linkage == LINKdefault) | |
421 { | |
422 error("forward reference of %s", v->toChars()); | |
423 s = NULL; | |
424 } | |
425 else | |
426 { | |
427 FuncDeclaration *f = s->toAlias()->isFuncDeclaration(); | |
428 if (f) | |
429 { | |
430 if (overnext) | |
431 { | |
432 FuncAliasDeclaration *fa = new FuncAliasDeclaration(f); | |
433 if (!fa->overloadInsert(overnext)) | |
434 ScopeDsymbol::multiplyDefined(0, f, overnext); | |
435 overnext = NULL; | |
436 s = fa; | |
437 s->parent = sc->parent; | |
438 } | |
439 } | |
440 if (overnext) | |
441 ScopeDsymbol::multiplyDefined(0, s, overnext); | |
442 if (s == this) | |
443 { | |
444 assert(global.errors); | |
445 s = NULL; | |
446 } | |
447 } | |
448 aliassym = s; | |
449 this->inSemantic = 0; | |
450 } | |
451 | |
452 int AliasDeclaration::overloadInsert(Dsymbol *s) | |
453 { | |
454 /* Don't know yet what the aliased symbol is, so assume it can | |
455 * be overloaded and check later for correctness. | |
456 */ | |
457 | |
458 //printf("AliasDeclaration::overloadInsert('%s')\n", s->toChars()); | |
459 if (overnext == NULL) | |
460 { overnext = s; | |
461 return TRUE; | |
462 } | |
463 else | |
464 { | |
465 return overnext->overloadInsert(s); | |
466 } | |
467 } | |
468 | |
469 char *AliasDeclaration::kind() | |
470 { | |
471 return "alias"; | |
472 } | |
473 | |
474 Type *AliasDeclaration::getType() | |
475 { | |
476 return type; | |
477 } | |
478 | |
479 Dsymbol *AliasDeclaration::toAlias() | |
480 { | |
481 //printf("AliasDeclaration::toAlias('%s', this = %p, aliassym = %p, kind = '%s')\n", toChars(), this, aliassym, aliassym ? aliassym->kind() : ""); | |
482 assert(this != aliassym); | |
483 //static int count; if (++count == 10) *(char*)0=0; | |
484 if (inSemantic) | |
485 { error("recursive alias declaration"); | |
486 // return this; | |
487 } | |
488 Dsymbol *s = aliassym ? aliassym->toAlias() : this; | |
489 return s; | |
490 } | |
491 | |
492 void AliasDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
493 { | |
494 buf->writestring("alias "); | |
495 #if 0 && _DH | |
496 if (hgs->hdrgen) | |
497 { | |
498 if (haliassym) | |
499 { | |
500 haliassym->toCBuffer(buf, hgs); | |
501 buf->writeByte(' '); | |
502 buf->writestring(ident->toChars()); | |
503 } | |
504 else | |
505 htype->toCBuffer(buf, ident, hgs); | |
506 } | |
507 else | |
508 #endif | |
509 { | |
510 if (aliassym) | |
511 { | |
512 aliassym->toCBuffer(buf, hgs); | |
513 buf->writeByte(' '); | |
514 buf->writestring(ident->toChars()); | |
515 } | |
516 else | |
517 type->toCBuffer(buf, ident, hgs); | |
518 } | |
519 buf->writeByte(';'); | |
520 buf->writenl(); | |
521 } | |
522 | |
523 /********************************* VarDeclaration ****************************/ | |
524 | |
525 VarDeclaration::VarDeclaration(Loc loc, Type *type, Identifier *id, Initializer *init) | |
526 : Declaration(id) | |
527 { | |
528 //printf("VarDeclaration('%s')\n", id->toChars()); | |
529 #ifdef DEBUG | |
530 if (!type && !init) | |
531 { printf("VarDeclaration('%s')\n", id->toChars()); | |
532 //*(char*)0=0; | |
533 } | |
534 #endif | |
535 assert(type || init); | |
536 this->type = type; | |
537 this->init = init; | |
538 #ifdef _DH | |
539 this->htype = NULL; | |
540 this->hinit = NULL; | |
541 #endif | |
542 this->loc = loc; | |
543 offset = 0; | |
544 noauto = 0; | |
545 nestedref = 0; | |
546 inuse = 0; | |
547 ctorinit = 0; | |
548 aliassym = NULL; | |
549 onstack = 0; | |
550 canassign = 0; | |
551 value = NULL; | |
156
ccd07d9f2ce9
[svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents:
139
diff
changeset
|
552 |
ccd07d9f2ce9
[svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents:
139
diff
changeset
|
553 // LLVMDC |
136
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
130
diff
changeset
|
554 needsStorage = false; |
1 | 555 } |
556 | |
557 Dsymbol *VarDeclaration::syntaxCopy(Dsymbol *s) | |
558 { | |
559 //printf("VarDeclaration::syntaxCopy(%s)\n", toChars()); | |
560 | |
561 VarDeclaration *sv; | |
562 if (s) | |
563 { sv = (VarDeclaration *)s; | |
564 } | |
565 else | |
566 { | |
567 Initializer *init = NULL; | |
568 if (this->init) | |
569 { init = this->init->syntaxCopy(); | |
570 //init->isExpInitializer()->exp->print(); | |
571 //init->isExpInitializer()->exp->dump(0); | |
572 } | |
573 | |
574 sv = new VarDeclaration(loc, type ? type->syntaxCopy() : NULL, ident, init); | |
575 sv->storage_class = storage_class; | |
576 } | |
577 #ifdef _DH | |
578 // Syntax copy for header file | |
579 if (!htype) // Don't overwrite original | |
580 { if (type) // Make copy for both old and new instances | |
581 { htype = type->syntaxCopy(); | |
582 sv->htype = type->syntaxCopy(); | |
583 } | |
584 } | |
585 else // Make copy of original for new instance | |
586 sv->htype = htype->syntaxCopy(); | |
587 if (!hinit) | |
588 { if (init) | |
589 { hinit = init->syntaxCopy(); | |
590 sv->hinit = init->syntaxCopy(); | |
591 } | |
592 } | |
593 else | |
594 sv->hinit = hinit->syntaxCopy(); | |
595 #endif | |
596 return sv; | |
597 } | |
598 | |
599 void VarDeclaration::semantic(Scope *sc) | |
600 { | |
601 //printf("VarDeclaration::semantic('%s', parent = '%s')\n", toChars(), sc->parent->toChars()); | |
602 //printf("type = %s\n", type->toChars()); | |
603 //printf("linkage = %d\n", sc->linkage); | |
604 //if (strcmp(toChars(), "mul") == 0) halt(); | |
605 | |
606 storage_class |= sc->stc; | |
607 if (storage_class & STCextern && init) | |
608 error("extern symbols cannot have initializers"); | |
609 | |
610 /* If auto type inference, do the inference | |
611 */ | |
612 int inferred = 0; | |
613 if (!type) | |
614 { inuse++; | |
615 type = init->inferType(sc); | |
616 inuse--; | |
617 inferred = 1; | |
618 | |
619 /* This is a kludge to support the existing syntax for RAII | |
620 * declarations. | |
621 */ | |
622 storage_class &= ~STCauto; | |
130 | 623 originalType = type; |
1 | 624 } |
625 else | |
130 | 626 { if (!originalType) |
627 originalType = type; | |
1 | 628 type = type->semantic(loc, sc); |
130 | 629 } |
1 | 630 |
631 type->checkDeprecated(loc, sc); | |
632 linkage = sc->linkage; | |
633 this->parent = sc->parent; | |
634 //printf("this = %p, parent = %p, '%s'\n", this, parent, parent->toChars()); | |
635 protection = sc->protection; | |
636 //printf("sc->stc = %x\n", sc->stc); | |
637 //printf("storage_class = %x\n", storage_class); | |
638 | |
639 Dsymbol *parent = toParent(); | |
640 FuncDeclaration *fd = parent->isFuncDeclaration(); | |
641 | |
642 Type *tb = type->toBasetype(); | |
643 if (tb->ty == Tvoid && !(storage_class & STClazy)) | |
644 { error("voids have no value"); | |
645 type = Type::terror; | |
646 tb = type; | |
647 } | |
648 if (tb->ty == Tfunction) | |
649 { error("cannot be declared to be a function"); | |
650 type = Type::terror; | |
651 tb = type; | |
652 } | |
653 if (tb->ty == Tstruct) | |
654 { TypeStruct *ts = (TypeStruct *)tb; | |
655 | |
656 if (!ts->sym->members) | |
657 { | |
658 error("no definition of struct %s", ts->toChars()); | |
659 } | |
660 } | |
661 | |
662 if (tb->ty == Ttuple) | |
663 { /* Instead, declare variables for each of the tuple elements | |
664 * and add those. | |
665 */ | |
666 TypeTuple *tt = (TypeTuple *)tb; | |
667 size_t nelems = Argument::dim(tt->arguments); | |
668 Objects *exps = new Objects(); | |
669 exps->setDim(nelems); | |
670 | |
671 for (size_t i = 0; i < nelems; i++) | |
672 { Argument *arg = Argument::getNth(tt->arguments, i); | |
673 | |
674 OutBuffer buf; | |
675 buf.printf("_%s_field_%zu", ident->toChars(), i); | |
676 buf.writeByte(0); | |
677 char *name = (char *)buf.extractData(); | |
678 Identifier *id = new Identifier(name, TOKidentifier); | |
679 | |
680 VarDeclaration *v = new VarDeclaration(loc, arg->type, id, NULL); | |
681 //printf("declaring field %s of type %s\n", v->toChars(), v->type->toChars()); | |
682 v->semantic(sc); | |
683 | |
684 if (sc->scopesym) | |
685 { //printf("adding %s to %s\n", v->toChars(), sc->scopesym->toChars()); | |
686 if (sc->scopesym->members) | |
687 sc->scopesym->members->push(v); | |
688 } | |
689 | |
690 Expression *e = new DsymbolExp(loc, v); | |
691 exps->data[i] = e; | |
692 } | |
693 TupleDeclaration *v2 = new TupleDeclaration(loc, ident, exps); | |
694 v2->isexp = 1; | |
695 aliassym = v2; | |
696 return; | |
697 } | |
698 | |
699 if (storage_class & STCconst && !init && !fd) | |
700 // Initialize by constructor only | |
701 storage_class = (storage_class & ~STCconst) | STCctorinit; | |
702 | |
703 if (isConst()) | |
704 { | |
705 } | |
706 else if (isStatic()) | |
707 { | |
708 } | |
709 else if (isSynchronized()) | |
710 { | |
711 error("variable %s cannot be synchronized", toChars()); | |
712 } | |
713 else if (isOverride()) | |
714 { | |
715 error("override cannot be applied to variable"); | |
716 } | |
717 else if (isAbstract()) | |
718 { | |
719 error("abstract cannot be applied to variable"); | |
720 } | |
721 else if (storage_class & STCtemplateparameter) | |
722 { | |
723 } | |
724 else | |
725 { | |
726 AggregateDeclaration *aad = sc->anonAgg; | |
727 if (!aad) | |
728 aad = parent->isAggregateDeclaration(); | |
729 if (aad) | |
730 { | |
731 aad->addField(sc, this); | |
732 } | |
733 | |
734 InterfaceDeclaration *id = parent->isInterfaceDeclaration(); | |
735 if (id) | |
736 { | |
737 error("field not allowed in interface"); | |
738 } | |
739 | |
740 TemplateInstance *ti = parent->isTemplateInstance(); | |
741 if (ti) | |
742 { | |
743 // Take care of nested templates | |
744 while (1) | |
745 { | |
746 TemplateInstance *ti2 = ti->tempdecl->parent->isTemplateInstance(); | |
747 if (!ti2) | |
748 break; | |
749 ti = ti2; | |
750 } | |
751 | |
752 // If it's a member template | |
753 AggregateDeclaration *ad = ti->tempdecl->isMember(); | |
754 if (ad && storage_class != STCundefined) | |
755 { | |
756 error("cannot use template to add field to aggregate '%s'", ad->toChars()); | |
757 } | |
758 } | |
759 } | |
760 | |
761 if (type->isauto() && !noauto) | |
762 { | |
763 if (storage_class & (STCfield | STCout | STCref | STCstatic) || !fd) | |
764 { | |
765 error("globals, statics, fields, ref and out parameters cannot be auto"); | |
766 } | |
767 | |
768 if (!(storage_class & (STCauto | STCscope))) | |
769 { | |
770 if (!(storage_class & STCparameter) && ident != Id::withSym) | |
771 error("reference to scope class must be scope"); | |
772 } | |
773 } | |
774 | |
775 if (!init && !sc->inunion && !isStatic() && !isConst() && fd && | |
92 | 776 !(storage_class & (STCfield | STCin | STCforeach)) && |
777 type->size() != 0) | |
1 | 778 { |
779 // Provide a default initializer | |
780 //printf("Providing default initializer for '%s'\n", toChars()); | |
781 if (type->ty == Tstruct && | |
782 ((TypeStruct *)type)->sym->zeroInit == 1) | |
19 | 783 { /* If a struct is all zeros, as a special case |
784 * set it's initializer to the integer 0. | |
785 * In AssignExp::toElem(), we check for this and issue | |
786 * a memset() to initialize the struct. | |
787 * Must do same check in interpreter. | |
788 */ | |
1 | 789 Expression *e = new IntegerExp(loc, 0, Type::tint32); |
790 Expression *e1; | |
791 e1 = new VarExp(loc, this); | |
792 e = new AssignExp(loc, e1, e); | |
793 e->type = e1->type; | |
794 init = new ExpInitializer(loc, e/*->type->defaultInit()*/); | |
795 return; | |
796 } | |
797 else if (type->ty == Ttypedef) | |
798 { TypeTypedef *td = (TypeTypedef *)type; | |
799 if (td->sym->init) | |
800 { init = td->sym->init; | |
801 ExpInitializer *ie = init->isExpInitializer(); | |
802 if (ie) | |
803 // Make copy so we can modify it | |
804 init = new ExpInitializer(ie->loc, ie->exp); | |
805 } | |
806 else | |
807 init = getExpInitializer(); | |
808 } | |
809 else | |
810 { | |
811 init = getExpInitializer(); | |
812 } | |
813 } | |
814 | |
815 if (init) | |
816 { | |
817 ArrayInitializer *ai = init->isArrayInitializer(); | |
19 | 818 if (ai && tb->ty == Taarray) |
1 | 819 { |
820 init = ai->toAssocArrayInitializer(); | |
821 } | |
822 | |
19 | 823 StructInitializer *si = init->isStructInitializer(); |
1 | 824 ExpInitializer *ei = init->isExpInitializer(); |
825 | |
826 // See if we can allocate on the stack | |
827 if (ei && isScope() && ei->exp->op == TOKnew) | |
828 { NewExp *ne = (NewExp *)ei->exp; | |
829 if (!(ne->newargs && ne->newargs->dim)) | |
830 { ne->onstack = 1; | |
831 onstack = 1; | |
832 if (type->isBaseOf(ne->newtype->semantic(loc, sc), NULL)) | |
833 onstack = 2; | |
834 } | |
835 } | |
836 | |
837 // If inside function, there is no semantic3() call | |
838 if (sc->func) | |
839 { | |
840 // If local variable, use AssignExp to handle all the various | |
841 // possibilities. | |
842 if (fd && !isStatic() && !isConst() && !init->isVoidInitializer()) | |
843 { | |
844 Expression *e1; | |
845 Type *t; | |
846 int dim; | |
847 | |
848 //printf("fd = '%s', var = '%s'\n", fd->toChars(), toChars()); | |
849 if (!ei) | |
850 { | |
851 Expression *e = init->toExpression(); | |
852 if (!e) | |
853 { | |
854 init = init->semantic(sc, type); | |
855 e = init->toExpression(); | |
856 if (!e) | |
857 { error("is not a static and cannot have static initializer"); | |
858 return; | |
859 } | |
860 } | |
861 ei = new ExpInitializer(init->loc, e); | |
862 init = ei; | |
863 } | |
864 | |
865 e1 = new VarExp(loc, this); | |
866 | |
867 t = type->toBasetype(); | |
868 if (t->ty == Tsarray) | |
869 { | |
92 | 870 ei->exp = ei->exp->semantic(sc); |
871 if (!ei->exp->implicitConvTo(type)) | |
1 | 872 { |
92 | 873 dim = ((TypeSArray *)t)->dim->toInteger(); |
874 // If multidimensional static array, treat as one large array | |
875 while (1) | |
876 { | |
877 t = t->nextOf()->toBasetype(); | |
878 if (t->ty != Tsarray) | |
879 break; | |
1 | 880 dim *= ((TypeSArray *)t)->dim->toInteger(); |
92 | 881 e1->type = new TypeSArray(t->nextOf(), new IntegerExp(0, dim, Type::tindex)); |
882 } | |
1 | 883 } |
884 e1 = new SliceExp(loc, e1, NULL, NULL); | |
885 } | |
886 else if (t->ty == Tstruct) | |
887 { | |
888 ei->exp = ei->exp->semantic(sc); | |
889 if (!ei->exp->implicitConvTo(type)) | |
890 ei->exp = new CastExp(loc, ei->exp, type); | |
891 } | |
892 ei->exp = new AssignExp(loc, e1, ei->exp); | |
893 ei->exp->op = TOKconstruct; | |
894 canassign++; | |
895 ei->exp = ei->exp->semantic(sc); | |
896 canassign--; | |
897 ei->exp->optimize(WANTvalue); | |
898 } | |
899 else | |
900 { | |
901 init = init->semantic(sc, type); | |
902 if (fd && isConst() && !isStatic()) | |
903 { // Make it static | |
904 storage_class |= STCstatic; | |
905 } | |
906 } | |
907 } | |
908 else if (isConst() || isFinal()) | |
909 { | |
910 /* Because we may need the results of a const declaration in a | |
911 * subsequent type, such as an array dimension, before semantic2() | |
912 * gets ordinarily run, try to run semantic2() now. | |
913 * Ignore failure. | |
914 */ | |
915 | |
19 | 916 if (!global.errors && !inferred) |
1 | 917 { |
918 unsigned errors = global.errors; | |
919 global.gag++; | |
920 //printf("+gag\n"); | |
19 | 921 Expression *e; |
922 Initializer *i2 = init; | |
1 | 923 inuse++; |
19 | 924 if (ei) |
925 { | |
926 e = ei->exp->syntaxCopy(); | |
927 e = e->semantic(sc); | |
928 e = e->implicitCastTo(sc, type); | |
929 } | |
930 else if (si || ai) | |
931 { i2 = init->syntaxCopy(); | |
932 i2 = i2->semantic(sc, type); | |
933 } | |
1 | 934 inuse--; |
935 global.gag--; | |
936 //printf("-gag\n"); | |
937 if (errors != global.errors) // if errors happened | |
938 { | |
939 if (global.gag == 0) | |
940 global.errors = errors; // act as if nothing happened | |
941 } | |
19 | 942 else if (ei) |
1 | 943 { |
944 e = e->optimize(WANTvalue | WANTinterpret); | |
945 if (e->op == TOKint64 || e->op == TOKstring) | |
946 { | |
947 ei->exp = e; // no errors, keep result | |
948 } | |
949 } | |
19 | 950 else |
951 init = i2; // no errors, keep result | |
1 | 952 } |
953 } | |
954 } | |
955 } | |
956 | |
957 ExpInitializer *VarDeclaration::getExpInitializer() | |
958 { | |
959 ExpInitializer *ei; | |
960 | |
961 if (init) | |
962 ei = init->isExpInitializer(); | |
963 else | |
964 { | |
965 Expression *e = type->defaultInit(); | |
966 if (e) | |
967 ei = new ExpInitializer(loc, e); | |
968 else | |
969 ei = NULL; | |
970 } | |
971 return ei; | |
972 } | |
973 | |
974 void VarDeclaration::semantic2(Scope *sc) | |
975 { | |
976 //printf("VarDeclaration::semantic2('%s')\n", toChars()); | |
977 if (init && !toParent()->isFuncDeclaration()) | |
978 { inuse++; | |
979 #if 0 | |
980 ExpInitializer *ei = init->isExpInitializer(); | |
981 if (ei) | |
982 { | |
983 ei->exp->dump(0); | |
984 printf("type = %p\n", ei->exp->type); | |
985 } | |
986 #endif | |
987 init = init->semantic(sc, type); | |
988 inuse--; | |
989 } | |
990 } | |
991 | |
992 char *VarDeclaration::kind() | |
993 { | |
994 return "variable"; | |
995 } | |
996 | |
997 Dsymbol *VarDeclaration::toAlias() | |
998 { | |
999 //printf("VarDeclaration::toAlias('%s', this = %p, aliassym = %p)\n", toChars(), this, aliassym); | |
1000 assert(this != aliassym); | |
1001 Dsymbol *s = aliassym ? aliassym->toAlias() : this; | |
1002 return s; | |
1003 } | |
1004 | |
1005 void VarDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1006 { | |
1007 if (storage_class & STCconst) | |
1008 buf->writestring("const "); | |
1009 if (storage_class & STCstatic) | |
1010 buf->writestring("static "); | |
1011 if (type) | |
1012 type->toCBuffer(buf, ident, hgs); | |
1013 else | |
1014 buf->writestring(ident->toChars()); | |
1015 if (init) | |
1016 { buf->writestring(" = "); | |
1017 init->toCBuffer(buf, hgs); | |
1018 } | |
1019 buf->writeByte(';'); | |
1020 buf->writenl(); | |
1021 } | |
1022 | |
1023 int VarDeclaration::needThis() | |
1024 { | |
1025 //printf("VarDeclaration::needThis(%s, x%x)\n", toChars(), storage_class); | |
1026 return storage_class & STCfield; | |
1027 } | |
1028 | |
1029 int VarDeclaration::isImportedSymbol() | |
1030 { | |
1031 if (protection == PROTexport && !init && (isStatic() || isConst() || parent->isModule())) | |
1032 return TRUE; | |
1033 return FALSE; | |
1034 } | |
1035 | |
1036 void VarDeclaration::checkCtorConstInit() | |
1037 { | |
1038 if (ctorinit == 0 && isCtorinit() && !(storage_class & STCfield)) | |
1039 error("missing initializer in static constructor for const variable"); | |
1040 } | |
1041 | |
1042 /************************************ | |
1043 * Check to see if variable is a reference to an enclosing function | |
1044 * or not. | |
1045 */ | |
1046 | |
1047 void VarDeclaration::checkNestedReference(Scope *sc, Loc loc) | |
1048 { | |
92 | 1049 if (parent && !isDataseg() && parent != sc->parent) |
1 | 1050 { |
1051 FuncDeclaration *fdv = toParent()->isFuncDeclaration(); | |
1052 FuncDeclaration *fdthis = sc->parent->isFuncDeclaration(); | |
1053 | |
1054 if (fdv && fdthis) | |
1055 { | |
1056 if (loc.filename) | |
1057 fdthis->getLevel(loc, fdv); | |
1058 nestedref = 1; | |
1059 fdv->nestedFrameRef = 1; | |
136
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
130
diff
changeset
|
1060 fdv->nestedVars.insert(this); |
1 | 1061 //printf("var %s in function %s is nested ref\n", toChars(), fdv->toChars()); |
1062 } | |
1063 } | |
1064 } | |
1065 | |
1066 /******************************* | |
1067 * Does symbol go into data segment? | |
1068 */ | |
1069 | |
1070 int VarDeclaration::isDataseg() | |
1071 { | |
1072 #if 0 | |
1073 printf("VarDeclaration::isDataseg(%p, '%s')\n", this, toChars()); | |
1074 printf("%x, %p, %p\n", storage_class & (STCstatic | STCconst), parent->isModule(), parent->isTemplateInstance()); | |
1075 printf("parent = '%s'\n", parent->toChars()); | |
1076 #endif | |
1077 Dsymbol *parent = this->toParent(); | |
1078 if (!parent && !(storage_class & (STCstatic | STCconst))) | |
1079 { error("forward referenced"); | |
1080 type = Type::terror; | |
1081 return 0; | |
1082 } | |
1083 return (storage_class & (STCstatic | STCconst) || | |
1084 parent->isModule() || | |
1085 parent->isTemplateInstance()); | |
1086 } | |
1087 | |
1088 int VarDeclaration::hasPointers() | |
1089 { | |
1090 return (!isDataseg() && type->hasPointers()); | |
1091 } | |
1092 | |
1093 /****************************************** | |
1094 * If a variable has an auto destructor call, return call for it. | |
1095 * Otherwise, return NULL. | |
1096 */ | |
1097 | |
1098 Expression *VarDeclaration::callAutoDtor() | |
1099 { Expression *e = NULL; | |
1100 | |
139
0ab29b838084
[svn r143] Fixed: a few bugs in debug information, still only line info, but should be correct files now :)
lindquist
parents:
136
diff
changeset
|
1101 //printf("VarDeclaration::callAutoDtor() %s\n", toChars()); |
1 | 1102 if (storage_class & (STCauto | STCscope) && !noauto) |
1103 { | |
1104 for (ClassDeclaration *cd = type->isClassHandle(); | |
1105 cd; | |
1106 cd = cd->baseClass) | |
1107 { | |
1108 /* We can do better if there's a way with onstack | |
1109 * classes to determine if there's no way the monitor | |
1110 * could be set. | |
1111 */ | |
139
0ab29b838084
[svn r143] Fixed: a few bugs in debug information, still only line info, but should be correct files now :)
lindquist
parents:
136
diff
changeset
|
1112 if (cd->isInterfaceDeclaration()) |
0ab29b838084
[svn r143] Fixed: a few bugs in debug information, still only line info, but should be correct files now :)
lindquist
parents:
136
diff
changeset
|
1113 error("interface %s cannot be scope", cd->toChars()); |
1 | 1114 if (1 || onstack || cd->dtors.dim) // if any destructors |
1115 { | |
1116 // delete this; | |
1117 Expression *ec; | |
1118 | |
1119 ec = new VarExp(loc, this); | |
1120 e = new DeleteExp(loc, ec); | |
1121 e->type = Type::tvoid; | |
1122 break; | |
1123 } | |
1124 } | |
1125 } | |
1126 return e; | |
1127 } | |
1128 | |
1129 | |
1130 /********************************* ClassInfoDeclaration ****************************/ | |
1131 | |
1132 ClassInfoDeclaration::ClassInfoDeclaration(ClassDeclaration *cd) | |
1133 : VarDeclaration(0, ClassDeclaration::classinfo->type, cd->ident, NULL) | |
1134 { | |
1135 this->cd = cd; | |
1136 storage_class = STCstatic; | |
1137 } | |
1138 | |
1139 Dsymbol *ClassInfoDeclaration::syntaxCopy(Dsymbol *s) | |
1140 { | |
1141 assert(0); // should never be produced by syntax | |
1142 return NULL; | |
1143 } | |
1144 | |
1145 void ClassInfoDeclaration::semantic(Scope *sc) | |
1146 { | |
1147 } | |
1148 | |
1149 /********************************* ModuleInfoDeclaration ****************************/ | |
1150 | |
1151 ModuleInfoDeclaration::ModuleInfoDeclaration(Module *mod) | |
1152 : VarDeclaration(0, Module::moduleinfo->type, mod->ident, NULL) | |
1153 { | |
1154 this->mod = mod; | |
1155 storage_class = STCstatic; | |
1156 } | |
1157 | |
1158 Dsymbol *ModuleInfoDeclaration::syntaxCopy(Dsymbol *s) | |
1159 { | |
1160 assert(0); // should never be produced by syntax | |
1161 return NULL; | |
1162 } | |
1163 | |
1164 void ModuleInfoDeclaration::semantic(Scope *sc) | |
1165 { | |
1166 } | |
1167 | |
1168 /********************************* TypeInfoDeclaration ****************************/ | |
1169 | |
1170 TypeInfoDeclaration::TypeInfoDeclaration(Type *tinfo, int internal) | |
1171 : VarDeclaration(0, Type::typeinfo->type, tinfo->getTypeInfoIdent(internal), NULL) | |
1172 { | |
1173 this->tinfo = tinfo; | |
1174 storage_class = STCstatic; | |
1175 protection = PROTpublic; | |
1176 linkage = LINKc; | |
1177 } | |
1178 | |
1179 Dsymbol *TypeInfoDeclaration::syntaxCopy(Dsymbol *s) | |
1180 { | |
1181 assert(0); // should never be produced by syntax | |
1182 return NULL; | |
1183 } | |
1184 | |
1185 void TypeInfoDeclaration::semantic(Scope *sc) | |
1186 { | |
1187 assert(linkage == LINKc); | |
1188 } | |
1189 | |
92 | 1190 /***************************** TypeInfoConstDeclaration **********************/ |
1191 | |
1192 #if V2 | |
1193 TypeInfoConstDeclaration::TypeInfoConstDeclaration(Type *tinfo) | |
1194 : TypeInfoDeclaration(tinfo, 0) | |
1195 { | |
1196 } | |
1197 #endif | |
1198 | |
1199 /***************************** TypeInfoInvariantDeclaration **********************/ | |
1200 | |
1201 #if V2 | |
1202 TypeInfoInvariantDeclaration::TypeInfoInvariantDeclaration(Type *tinfo) | |
1203 : TypeInfoDeclaration(tinfo, 0) | |
1204 { | |
1205 } | |
1206 #endif | |
1207 | |
1 | 1208 /***************************** TypeInfoStructDeclaration **********************/ |
1209 | |
1210 TypeInfoStructDeclaration::TypeInfoStructDeclaration(Type *tinfo) | |
1211 : TypeInfoDeclaration(tinfo, 0) | |
1212 { | |
1213 } | |
1214 | |
1215 /***************************** TypeInfoClassDeclaration ***********************/ | |
1216 | |
1217 TypeInfoClassDeclaration::TypeInfoClassDeclaration(Type *tinfo) | |
1218 : TypeInfoDeclaration(tinfo, 0) | |
1219 { | |
1220 } | |
1221 | |
1222 /***************************** TypeInfoInterfaceDeclaration *******************/ | |
1223 | |
1224 TypeInfoInterfaceDeclaration::TypeInfoInterfaceDeclaration(Type *tinfo) | |
1225 : TypeInfoDeclaration(tinfo, 0) | |
1226 { | |
1227 } | |
1228 | |
1229 /***************************** TypeInfoTypedefDeclaration *********************/ | |
1230 | |
1231 TypeInfoTypedefDeclaration::TypeInfoTypedefDeclaration(Type *tinfo) | |
1232 : TypeInfoDeclaration(tinfo, 0) | |
1233 { | |
1234 } | |
1235 | |
1236 /***************************** TypeInfoPointerDeclaration *********************/ | |
1237 | |
1238 TypeInfoPointerDeclaration::TypeInfoPointerDeclaration(Type *tinfo) | |
1239 : TypeInfoDeclaration(tinfo, 0) | |
1240 { | |
1241 } | |
1242 | |
1243 /***************************** TypeInfoArrayDeclaration ***********************/ | |
1244 | |
1245 TypeInfoArrayDeclaration::TypeInfoArrayDeclaration(Type *tinfo) | |
1246 : TypeInfoDeclaration(tinfo, 0) | |
1247 { | |
1248 } | |
1249 | |
1250 /***************************** TypeInfoStaticArrayDeclaration *****************/ | |
1251 | |
1252 TypeInfoStaticArrayDeclaration::TypeInfoStaticArrayDeclaration(Type *tinfo) | |
1253 : TypeInfoDeclaration(tinfo, 0) | |
1254 { | |
1255 } | |
1256 | |
1257 /***************************** TypeInfoAssociativeArrayDeclaration ************/ | |
1258 | |
1259 TypeInfoAssociativeArrayDeclaration::TypeInfoAssociativeArrayDeclaration(Type *tinfo) | |
1260 : TypeInfoDeclaration(tinfo, 0) | |
1261 { | |
1262 } | |
1263 | |
1264 /***************************** TypeInfoEnumDeclaration ***********************/ | |
1265 | |
1266 TypeInfoEnumDeclaration::TypeInfoEnumDeclaration(Type *tinfo) | |
1267 : TypeInfoDeclaration(tinfo, 0) | |
1268 { | |
1269 } | |
1270 | |
1271 /***************************** TypeInfoFunctionDeclaration ********************/ | |
1272 | |
1273 TypeInfoFunctionDeclaration::TypeInfoFunctionDeclaration(Type *tinfo) | |
1274 : TypeInfoDeclaration(tinfo, 0) | |
1275 { | |
1276 } | |
1277 | |
1278 /***************************** TypeInfoDelegateDeclaration ********************/ | |
1279 | |
1280 TypeInfoDelegateDeclaration::TypeInfoDelegateDeclaration(Type *tinfo) | |
1281 : TypeInfoDeclaration(tinfo, 0) | |
1282 { | |
1283 } | |
1284 | |
1285 /***************************** TypeInfoTupleDeclaration **********************/ | |
1286 | |
1287 TypeInfoTupleDeclaration::TypeInfoTupleDeclaration(Type *tinfo) | |
1288 : TypeInfoDeclaration(tinfo, 0) | |
1289 { | |
1290 } | |
1291 | |
1292 /********************************* ThisDeclaration ****************************/ | |
1293 | |
1294 // For the "this" parameter to member functions | |
1295 | |
1296 ThisDeclaration::ThisDeclaration(Type *t) | |
1297 : VarDeclaration(0, t, Id::This, NULL) | |
1298 { | |
1299 noauto = 1; | |
1300 } | |
1301 | |
1302 Dsymbol *ThisDeclaration::syntaxCopy(Dsymbol *s) | |
1303 { | |
1304 assert(0); // should never be produced by syntax | |
1305 return NULL; | |
1306 } | |
1307 | |
1308 |