Mercurial > projects > ldc
annotate dmd/dsymbol.c @ 100:5071469303d4 trunk
[svn r104] TONS OF FIXES.
Split up declaration, constant initializer gen and definition for globals, structs, classes and functions.
Improved ClassInfo support (not complete), not in vtable yet.
Fixed a bunch of forward reference problems.
Much more. Major commit! :)
author | lindquist |
---|---|
date | Fri, 16 Nov 2007 08:21:47 +0100 |
parents | 0ccfae271c45 |
children | 027b8d8b71ec |
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 <string.h> | |
13 #include <assert.h> | |
14 | |
15 #include "mem.h" | |
16 | |
17 #include "mars.h" | |
18 #include "dsymbol.h" | |
19 #include "aggregate.h" | |
20 #include "identifier.h" | |
21 #include "module.h" | |
22 #include "mtype.h" | |
23 #include "expression.h" | |
24 #include "statement.h" | |
25 #include "declaration.h" | |
26 #include "id.h" | |
27 #include "scope.h" | |
28 #include "init.h" | |
29 #include "import.h" | |
30 #include "template.h" | |
31 | |
32 #include "../gen/enums.h" | |
33 | |
34 /****************************** Dsymbol ******************************/ | |
35 | |
36 Dsymbol::Dsymbol() | |
37 { | |
38 //printf("Dsymbol::Dsymbol(%p)\n", this); | |
39 this->ident = NULL; | |
40 this->c_ident = NULL; | |
41 this->parent = NULL; | |
42 this->csym = NULL; | |
43 this->isym = NULL; | |
44 this->loc = 0; | |
45 this->comment = NULL; | |
46 this->llvmInternal = LLVMnone; | |
47 this->llvmInternal1 = NULL; | |
48 this->llvmInternal2 = NULL; | |
49 this->llvmValue = NULL; | |
55
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
19
diff
changeset
|
50 this->llvmDModule = NULL; |
100 | 51 this->llvmTouched = false; |
1 | 52 } |
53 | |
54 Dsymbol::Dsymbol(Identifier *ident) | |
55 { | |
56 //printf("Dsymbol::Dsymbol(%p, ident)\n", this); | |
57 this->ident = ident; | |
58 this->c_ident = NULL; | |
59 this->parent = NULL; | |
60 this->csym = NULL; | |
61 this->isym = NULL; | |
62 this->loc = 0; | |
63 this->comment = NULL; | |
64 this->llvmInternal = LLVMnone; | |
65 this->llvmInternal1 = NULL; | |
66 this->llvmInternal2 = NULL; | |
67 this->llvmValue = NULL; | |
68 this->llvmDModule = NULL; | |
100 | 69 this->llvmTouched = false; |
1 | 70 } |
71 | |
72 int Dsymbol::equals(Object *o) | |
73 { Dsymbol *s; | |
74 | |
75 if (this == o) | |
76 return TRUE; | |
77 s = (Dsymbol *)(o); | |
78 if (s && ident->equals(s->ident)) | |
79 return TRUE; | |
80 return FALSE; | |
81 } | |
82 | |
83 /************************************** | |
84 * Copy the syntax. | |
85 * Used for template instantiations. | |
86 * If s is NULL, allocate the new object, otherwise fill it in. | |
87 */ | |
88 | |
89 Dsymbol *Dsymbol::syntaxCopy(Dsymbol *s) | |
90 { | |
91 print(); | |
92 printf("%s %s\n", kind(), toChars()); | |
93 assert(0); | |
94 return NULL; | |
95 } | |
96 | |
97 /************************************** | |
98 * Determine if this symbol is only one. | |
99 * Returns: | |
100 * FALSE, *ps = NULL: There are 2 or more symbols | |
101 * TRUE, *ps = NULL: There are zero symbols | |
102 * TRUE, *ps = symbol: The one and only one symbol | |
103 */ | |
104 | |
105 int Dsymbol::oneMember(Dsymbol **ps) | |
106 { | |
107 //printf("Dsymbol::oneMember()\n"); | |
108 *ps = this; | |
109 return TRUE; | |
110 } | |
111 | |
112 /***************************************** | |
113 * Same as Dsymbol::oneMember(), but look at an array of Dsymbols. | |
114 */ | |
115 | |
116 int Dsymbol::oneMembers(Array *members, Dsymbol **ps) | |
117 { | |
118 //printf("Dsymbol::oneMembers() %d\n", members ? members->dim : 0); | |
119 Dsymbol *s = NULL; | |
120 | |
121 if (members) | |
122 { | |
123 for (int i = 0; i < members->dim; i++) | |
124 { Dsymbol *sx = (Dsymbol *)members->data[i]; | |
125 | |
126 int x = sx->oneMember(ps); | |
127 //printf("\t[%d] kind %s = %d, s = %p\n", i, sx->kind(), x, *ps); | |
128 if (!x) | |
129 { | |
130 //printf("\tfalse 1\n"); | |
131 assert(*ps == NULL); | |
132 return FALSE; | |
133 } | |
134 if (*ps) | |
135 { | |
136 if (s) // more than one symbol | |
137 { *ps = NULL; | |
138 //printf("\tfalse 2\n"); | |
139 return FALSE; | |
140 } | |
141 s = *ps; | |
142 } | |
143 } | |
144 } | |
145 *ps = s; // s is the one symbol, NULL if none | |
146 //printf("\ttrue\n"); | |
147 return TRUE; | |
148 } | |
149 | |
150 /***************************************** | |
151 * Is Dsymbol a variable that contains pointers? | |
152 */ | |
153 | |
154 int Dsymbol::hasPointers() | |
155 { | |
156 //printf("Dsymbol::hasPointers() %s\n", toChars()); | |
157 return 0; | |
158 } | |
159 | |
160 char *Dsymbol::toChars() | |
161 { | |
162 return ident ? ident->toChars() : (char *)"__anonymous"; | |
163 } | |
164 | |
165 char *Dsymbol::toPrettyChars() | |
166 { Dsymbol *p; | |
167 char *s; | |
168 char *q; | |
169 size_t len; | |
170 | |
171 //printf("Dsymbol::toPrettyChars() '%s'\n", toChars()); | |
172 if (!parent) | |
173 return toChars(); | |
174 | |
175 len = 0; | |
176 for (p = this; p; p = p->parent) | |
177 len += strlen(p->toChars()) + 1; | |
178 | |
179 s = (char *)mem.malloc(len); | |
180 q = s + len - 1; | |
181 *q = 0; | |
182 for (p = this; p; p = p->parent) | |
183 { | |
184 char *t = p->toChars(); | |
185 len = strlen(t); | |
186 q -= len; | |
187 memcpy(q, t, len); | |
188 if (q == s) | |
189 break; | |
190 q--; | |
191 *q = '.'; | |
192 } | |
193 return s; | |
194 } | |
195 | |
196 char *Dsymbol::locToChars() | |
197 { | |
198 OutBuffer buf; | |
199 char *p; | |
200 | |
201 Module *m = getModule(); | |
202 | |
203 if (m && m->srcfile) | |
204 loc.filename = m->srcfile->toChars(); | |
205 return loc.toChars(); | |
206 } | |
207 | |
208 char *Dsymbol::kind() | |
209 { | |
210 return "symbol"; | |
211 } | |
212 | |
213 /********************************* | |
214 * If this symbol is really an alias for another, | |
215 * return that other. | |
216 */ | |
217 | |
218 Dsymbol *Dsymbol::toAlias() | |
219 { | |
220 return this; | |
221 } | |
222 | |
223 Dsymbol *Dsymbol::toParent() | |
224 { | |
225 return parent ? parent->pastMixin() : NULL; | |
226 } | |
227 | |
228 Dsymbol *Dsymbol::pastMixin() | |
229 { | |
230 Dsymbol *s = this; | |
231 | |
232 //printf("Dsymbol::pastMixin() %s\n", toChars()); | |
233 while (s && s->isTemplateMixin()) | |
234 s = s->parent; | |
235 return s; | |
236 } | |
237 | |
238 /********************************** | |
239 * Use this instead of toParent() when looking for the | |
240 * 'this' pointer of the enclosing function/class. | |
241 */ | |
242 | |
243 Dsymbol *Dsymbol::toParent2() | |
244 { | |
245 Dsymbol *s = parent; | |
246 while (s && s->isTemplateInstance()) | |
247 s = s->parent; | |
248 return s; | |
249 } | |
250 | |
251 | |
252 int Dsymbol::isAnonymous() | |
253 { | |
254 return ident ? 0 : 1; | |
255 } | |
256 | |
257 void Dsymbol::semantic(Scope *sc) | |
258 { | |
259 error("%p has no semantic routine", this); | |
260 } | |
261 | |
262 void Dsymbol::semantic2(Scope *sc) | |
263 { | |
264 // Most Dsymbols have no further semantic analysis needed | |
265 } | |
266 | |
267 void Dsymbol::semantic3(Scope *sc) | |
268 { | |
269 // Most Dsymbols have no further semantic analysis needed | |
270 } | |
271 | |
272 void Dsymbol::inlineScan() | |
273 { | |
274 // Most Dsymbols have no further semantic analysis needed | |
275 } | |
276 | |
277 Dsymbol *Dsymbol::search(Loc loc, Identifier *ident, int flags) | |
278 { | |
279 //printf("Dsymbol::search(this=%p,%s, ident='%s')\n", this, toChars(), ident->toChars()); | |
280 return NULL; | |
281 } | |
282 | |
283 /*************************************** | |
284 * Search for identifier id as a member of 'this'. | |
285 * id may be a template instance. | |
286 * Returns: | |
287 * symbol found, NULL if not | |
288 */ | |
289 | |
290 Dsymbol *Dsymbol::searchX(Loc loc, Scope *sc, Identifier *id) | |
291 { | |
292 //printf("Dsymbol::searchX(this=%p,%s, ident='%s')\n", this, toChars(), ident->toChars()); | |
293 Dsymbol *s = toAlias(); | |
294 Dsymbol *sm; | |
295 | |
296 switch (id->dyncast()) | |
297 { | |
298 case DYNCAST_IDENTIFIER: | |
299 sm = s->search(loc, id, 0); | |
300 break; | |
301 | |
302 case DYNCAST_DSYMBOL: | |
303 { // It's a template instance | |
304 //printf("\ttemplate instance id\n"); | |
305 Dsymbol *st = (Dsymbol *)id; | |
306 TemplateInstance *ti = st->isTemplateInstance(); | |
307 id = ti->name; | |
308 sm = s->search(loc, id, 0); | |
309 if (!sm) | |
310 { error("template identifier %s is not a member of %s %s", | |
311 id->toChars(), s->kind(), s->toChars()); | |
312 return NULL; | |
313 } | |
314 sm = sm->toAlias(); | |
315 TemplateDeclaration *td = sm->isTemplateDeclaration(); | |
316 if (!td) | |
317 { | |
318 error("%s is not a template, it is a %s", id->toChars(), sm->kind()); | |
319 return NULL; | |
320 } | |
321 ti->tempdecl = td; | |
322 if (!ti->semanticdone) | |
323 ti->semantic(sc); | |
324 sm = ti->toAlias(); | |
325 break; | |
326 } | |
327 | |
328 default: | |
329 assert(0); | |
330 } | |
331 return sm; | |
332 } | |
333 | |
334 int Dsymbol::overloadInsert(Dsymbol *s) | |
335 { | |
336 //printf("Dsymbol::overloadInsert('%s')\n", s->toChars()); | |
337 return FALSE; | |
338 } | |
339 | |
340 void Dsymbol::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
341 { | |
342 buf->writestring(toChars()); | |
343 } | |
344 | |
345 unsigned Dsymbol::size(Loc loc) | |
346 { | |
347 error("Dsymbol '%s' has no size\n", toChars()); | |
348 return 0; | |
349 } | |
350 | |
351 int Dsymbol::isforwardRef() | |
352 { | |
353 return FALSE; | |
354 } | |
355 | |
356 AggregateDeclaration *Dsymbol::isThis() | |
357 { | |
358 return NULL; | |
359 } | |
360 | |
361 ClassDeclaration *Dsymbol::isClassMember() // are we a member of a class? | |
362 { | |
363 Dsymbol *parent = toParent(); | |
364 if (parent && parent->isClassDeclaration()) | |
365 return (ClassDeclaration *)parent; | |
366 return NULL; | |
367 } | |
368 | |
369 void Dsymbol::defineRef(Dsymbol *s) | |
370 { | |
371 assert(0); | |
372 } | |
373 | |
374 int Dsymbol::isExport() | |
375 { | |
376 return FALSE; | |
377 } | |
378 | |
379 int Dsymbol::isImportedSymbol() | |
380 { | |
381 return FALSE; | |
382 } | |
383 | |
384 int Dsymbol::isDeprecated() | |
385 { | |
386 return FALSE; | |
387 } | |
388 | |
389 LabelDsymbol *Dsymbol::isLabel() // is this a LabelDsymbol()? | |
390 { | |
391 return NULL; | |
392 } | |
393 | |
394 AggregateDeclaration *Dsymbol::isMember() // is this a member of an AggregateDeclaration? | |
395 { | |
396 Dsymbol *parent = toParent(); | |
397 return parent ? parent->isAggregateDeclaration() : NULL; | |
398 } | |
399 | |
400 Type *Dsymbol::getType() | |
401 { | |
402 return NULL; | |
403 } | |
404 | |
405 int Dsymbol::needThis() | |
406 { | |
407 return FALSE; | |
408 } | |
409 | |
410 int Dsymbol::addMember(Scope *sc, ScopeDsymbol *sd, int memnum) | |
411 { | |
412 //printf("Dsymbol::addMember('%s')\n", toChars()); | |
413 //printf("Dsymbol::addMember(this = %p, '%s' scopesym = '%s')\n", this, toChars(), sd->toChars()); | |
414 //printf("Dsymbol::addMember(this = %p, '%s' sd = %p, sd->symtab = %p)\n", this, toChars(), sd, sd->symtab); | |
415 parent = sd; | |
416 if (!isAnonymous()) // no name, so can't add it to symbol table | |
417 { | |
418 if (!sd->symtab->insert(this)) // if name is already defined | |
419 { | |
420 Dsymbol *s2; | |
421 | |
422 s2 = sd->symtab->lookup(ident); | |
423 if (!s2->overloadInsert(this)) | |
424 { | |
425 sd->multiplyDefined(0, this, s2); | |
426 } | |
427 } | |
428 if (sd->isAggregateDeclaration() || sd->isEnumDeclaration()) | |
429 { | |
430 if (ident == Id::__sizeof || ident == Id::alignof || ident == Id::mangleof) | |
431 error(".%s property cannot be redefined", ident->toChars()); | |
432 } | |
433 return 1; | |
434 } | |
435 return 0; | |
436 } | |
437 | |
438 void Dsymbol::error(const char *format, ...) | |
439 { | |
440 //printf("Dsymbol::error()\n"); | |
441 if (!global.gag) | |
442 { | |
443 char *p = locToChars(); | |
444 | |
445 if (*p) | |
446 fprintf(stdmsg, "%s: ", p); | |
447 mem.free(p); | |
448 | |
449 if (isAnonymous()) | |
450 fprintf(stdmsg, "%s ", kind()); | |
451 else | |
452 fprintf(stdmsg, "%s %s ", kind(), toPrettyChars()); | |
453 | |
454 va_list ap; | |
455 va_start(ap, format); | |
456 vfprintf(stdmsg, format, ap); | |
457 va_end(ap); | |
458 | |
459 fprintf(stdmsg, "\n"); | |
460 fflush(stdmsg); | |
461 } | |
462 global.errors++; | |
463 | |
464 //fatal(); | |
465 } | |
466 | |
467 void Dsymbol::error(Loc loc, const char *format, ...) | |
468 { | |
469 if (!global.gag) | |
470 { | |
471 char *p = loc.toChars(); | |
472 if (!*p) | |
473 p = locToChars(); | |
474 | |
475 if (*p) | |
476 fprintf(stdmsg, "%s: ", p); | |
477 mem.free(p); | |
478 | |
479 fprintf(stdmsg, "%s %s ", kind(), toPrettyChars()); | |
480 | |
481 va_list ap; | |
482 va_start(ap, format); | |
483 vfprintf(stdmsg, format, ap); | |
484 va_end(ap); | |
485 | |
486 fprintf(stdmsg, "\n"); | |
487 fflush(stdmsg); | |
488 } | |
489 | |
490 global.errors++; | |
491 | |
492 //fatal(); | |
493 } | |
494 | |
495 void Dsymbol::checkDeprecated(Loc loc, Scope *sc) | |
496 { | |
497 if (!global.params.useDeprecated && isDeprecated()) | |
498 { | |
499 // Don't complain if we're inside a deprecated symbol's scope | |
500 for (Dsymbol *sp = sc->parent; sp; sp = sp->parent) | |
501 { if (sp->isDeprecated()) | |
502 return; | |
503 } | |
504 | |
505 for (; sc; sc = sc->enclosing) | |
506 { | |
507 if (sc->scopesym && sc->scopesym->isDeprecated()) | |
508 return; | |
509 } | |
510 | |
511 error(loc, "is deprecated"); | |
512 } | |
513 } | |
514 | |
515 /********************************** | |
516 * Determine which Module a Dsymbol is in. | |
517 */ | |
518 | |
519 Module *Dsymbol::getModule() | |
520 { | |
521 Module *m; | |
522 Dsymbol *s; | |
523 | |
524 //printf("Dsymbol::getModule()\n"); | |
525 s = this; | |
526 while (s) | |
527 { | |
528 //printf("\ts = '%s'\n", s->toChars()); | |
529 m = s->isModule(); | |
530 if (m) | |
531 return m; | |
532 s = s->parent; | |
533 } | |
534 return NULL; | |
535 } | |
536 | |
537 /************************************* | |
538 */ | |
539 | |
540 enum PROT Dsymbol::prot() | |
541 { | |
542 return PROTpublic; | |
543 } | |
544 | |
545 /************************************* | |
546 * Do syntax copy of an array of Dsymbol's. | |
547 */ | |
548 | |
549 | |
550 Array *Dsymbol::arraySyntaxCopy(Array *a) | |
551 { | |
552 | |
553 Array *b = NULL; | |
554 if (a) | |
555 { | |
556 b = a->copy(); | |
557 for (int i = 0; i < b->dim; i++) | |
558 { | |
559 Dsymbol *s = (Dsymbol *)b->data[i]; | |
560 | |
561 s = s->syntaxCopy(NULL); | |
562 b->data[i] = (void *)s; | |
563 } | |
564 } | |
565 return b; | |
566 } | |
567 | |
568 | |
569 /**************************************** | |
570 * Add documentation comment to Dsymbol. | |
571 * Ignore NULL comments. | |
572 */ | |
573 | |
574 void Dsymbol::addComment(unsigned char *comment) | |
575 { | |
576 // if (comment) | |
577 // printf("adding comment '%s' to symbol %p '%s'\n", comment, this, toChars()); | |
578 | |
579 if (!this->comment) | |
580 this->comment = comment; | |
581 #if 1 | |
582 else if (comment && strcmp((char *)comment, (char *)this->comment)) | |
583 { // Concatenate the two | |
584 this->comment = Lexer::combineComments(this->comment, comment); | |
585 } | |
586 #endif | |
587 } | |
588 | |
589 | |
590 /********************************* ScopeDsymbol ****************************/ | |
591 | |
592 ScopeDsymbol::ScopeDsymbol() | |
593 : Dsymbol() | |
594 { | |
595 members = NULL; | |
596 symtab = NULL; | |
597 imports = NULL; | |
598 prots = NULL; | |
599 } | |
600 | |
601 ScopeDsymbol::ScopeDsymbol(Identifier *id) | |
602 : Dsymbol(id) | |
603 { | |
604 members = NULL; | |
605 symtab = NULL; | |
606 imports = NULL; | |
607 prots = NULL; | |
608 } | |
609 | |
610 Dsymbol *ScopeDsymbol::syntaxCopy(Dsymbol *s) | |
611 { | |
612 //printf("ScopeDsymbol::syntaxCopy('%s')\n", toChars()); | |
613 | |
614 ScopeDsymbol *sd; | |
615 if (s) | |
616 sd = (ScopeDsymbol *)s; | |
617 else | |
618 sd = new ScopeDsymbol(ident); | |
619 sd->members = arraySyntaxCopy(members); | |
620 return sd; | |
621 } | |
622 | |
623 Dsymbol *ScopeDsymbol::search(Loc loc, Identifier *ident, int flags) | |
624 { Dsymbol *s; | |
625 int i; | |
626 | |
627 //printf("%s->ScopeDsymbol::search(ident='%s', flags=x%x)\n", toChars(), ident->toChars(), flags); | |
628 // Look in symbols declared in this module | |
629 s = symtab ? symtab->lookup(ident) : NULL; | |
630 if (s) | |
631 { | |
632 //printf("\ts = '%s.%s'\n",toChars(),s->toChars()); | |
633 } | |
634 else if (imports) | |
635 { | |
636 // Look in imported modules | |
637 for (i = 0; i < imports->dim; i++) | |
638 { ScopeDsymbol *ss = (ScopeDsymbol *)imports->data[i]; | |
639 Dsymbol *s2; | |
640 | |
641 // If private import, don't search it | |
642 if (flags & 1 && prots[i] == PROTprivate) | |
643 continue; | |
644 | |
645 //printf("\tscanning import '%s', prots = %d, isModule = %p, isImport = %p\n", ss->toChars(), prots[i], ss->isModule(), ss->isImport()); | |
646 s2 = ss->search(loc, ident, ss->isModule() ? 1 : 0); | |
647 if (!s) | |
648 s = s2; | |
649 else if (s2 && s != s2) | |
650 { | |
651 if (s->toAlias() == s2->toAlias()) | |
652 { | |
653 if (s->isDeprecated()) | |
654 s = s2; | |
655 } | |
656 else | |
657 { | |
658 /* Two imports of the same module should be regarded as | |
659 * the same. | |
660 */ | |
661 Import *i1 = s->isImport(); | |
662 Import *i2 = s2->isImport(); | |
663 if (!(i1 && i2 && | |
664 (i1->mod == i2->mod || | |
665 (!i1->parent->isImport() && !i2->parent->isImport() && | |
666 i1->ident->equals(i2->ident)) | |
667 ) | |
668 ) | |
669 ) | |
670 { | |
671 ss->multiplyDefined(loc, s, s2); | |
672 break; | |
673 } | |
674 } | |
675 } | |
676 } | |
677 if (s) | |
678 { | |
679 Declaration *d = s->isDeclaration(); | |
680 if (d && d->protection == PROTprivate && !d->parent->isTemplateMixin()) | |
681 error("%s is private", d->toPrettyChars()); | |
682 } | |
683 } | |
684 return s; | |
685 } | |
686 | |
687 void ScopeDsymbol::importScope(ScopeDsymbol *s, enum PROT protection) | |
688 { | |
689 //printf("%s->ScopeDsymbol::importScope(%s, %d)\n", toChars(), s->toChars(), protection); | |
690 | |
691 // No circular or redundant import's | |
692 if (s != this) | |
693 { | |
694 if (!imports) | |
695 imports = new Array(); | |
696 else | |
697 { | |
698 for (int i = 0; i < imports->dim; i++) | |
699 { ScopeDsymbol *ss; | |
700 | |
701 ss = (ScopeDsymbol *) imports->data[i]; | |
702 if (ss == s) | |
703 { | |
704 if (protection > prots[i]) | |
705 prots[i] = protection; // upgrade access | |
706 return; | |
707 } | |
708 } | |
709 } | |
710 imports->push(s); | |
711 prots = (unsigned char *)mem.realloc(prots, imports->dim * sizeof(prots[0])); | |
712 prots[imports->dim - 1] = protection; | |
713 } | |
714 } | |
715 | |
716 int ScopeDsymbol::isforwardRef() | |
717 { | |
718 return (members == NULL); | |
719 } | |
720 | |
721 void ScopeDsymbol::defineRef(Dsymbol *s) | |
722 { | |
723 ScopeDsymbol *ss; | |
724 | |
725 ss = s->isScopeDsymbol(); | |
726 members = ss->members; | |
727 ss->members = NULL; | |
728 } | |
729 | |
730 void ScopeDsymbol::multiplyDefined(Loc loc, Dsymbol *s1, Dsymbol *s2) | |
731 { | |
732 #if 0 | |
733 printf("ScopeDsymbol::multiplyDefined()\n"); | |
734 printf("s1 = %p, '%s' kind = '%s', parent = %s\n", s1, s1->toChars(), s1->kind(), s1->parent ? s1->parent->toChars() : ""); | |
735 printf("s2 = %p, '%s' kind = '%s', parent = %s\n", s2, s2->toChars(), s2->kind(), s2->parent ? s2->parent->toChars() : ""); | |
736 #endif | |
737 if (loc.filename) | |
738 { ::error(loc, "%s at %s conflicts with %s at %s", | |
739 s1->toPrettyChars(), | |
740 s1->locToChars(), | |
741 s2->toPrettyChars(), | |
742 s2->locToChars()); | |
743 } | |
744 else | |
745 { | |
746 s1->error(loc, "conflicts with %s %s at %s", | |
747 s2->kind(), | |
748 s2->toPrettyChars(), | |
749 s2->locToChars()); | |
750 } | |
751 } | |
752 | |
753 Dsymbol *ScopeDsymbol::nameCollision(Dsymbol *s) | |
754 { | |
755 Dsymbol *sprev; | |
756 | |
757 // Look to see if we are defining a forward referenced symbol | |
758 | |
759 sprev = symtab->lookup(s->ident); | |
760 assert(sprev); | |
761 if (s->equals(sprev)) // if the same symbol | |
762 { | |
763 if (s->isforwardRef()) // if second declaration is a forward reference | |
764 return sprev; | |
765 if (sprev->isforwardRef()) | |
766 { | |
767 sprev->defineRef(s); // copy data from s into sprev | |
768 return sprev; | |
769 } | |
770 } | |
771 multiplyDefined(0, s, sprev); | |
772 return sprev; | |
773 } | |
774 | |
775 char *ScopeDsymbol::kind() | |
776 { | |
777 return "ScopeDsymbol"; | |
778 } | |
779 | |
780 | |
19 | 781 /******************************************* |
782 * Look for member of the form: | |
783 * const(MemberInfo)[] getMembers(string); | |
784 * Returns NULL if not found | |
785 */ | |
786 | |
787 #if V2 | |
788 FuncDeclaration *ScopeDsymbol::findGetMembers() | |
789 { | |
790 Dsymbol *s = search_function(this, Id::getmembers); | |
791 FuncDeclaration *fdx = s ? s->isFuncDeclaration() : NULL; | |
792 | |
793 #if 0 // Finish | |
794 static TypeFunction *tfgetmembers; | |
795 | |
796 if (!tfgetmembers) | |
797 { | |
798 Scope sc; | |
799 Arguments *arguments = new Arguments; | |
800 Arguments *arg = new Argument(STCin, Type::tchar->constOf()->arrayOf(), NULL, NULL); | |
801 arguments->push(arg); | |
802 | |
803 Type *tret = NULL; | |
804 tfgetmembers = new TypeFunction(arguments, tret, 0, LINKd); | |
805 tfgetmembers = (TypeFunction *)tfgetmembers->semantic(0, &sc); | |
806 } | |
807 if (fdx) | |
808 fdx = fdx->overloadExactMatch(tfgetmembers); | |
809 #endif | |
810 if (fdx && fdx->isVirtual()) | |
811 fdx = NULL; | |
812 | |
813 return fdx; | |
814 } | |
815 #endif | |
816 | |
817 | |
1 | 818 /****************************** WithScopeSymbol ******************************/ |
819 | |
820 WithScopeSymbol::WithScopeSymbol(WithStatement *withstate) | |
821 : ScopeDsymbol() | |
822 { | |
823 this->withstate = withstate; | |
824 } | |
825 | |
826 Dsymbol *WithScopeSymbol::search(Loc loc, Identifier *ident, int flags) | |
827 { | |
828 // Acts as proxy to the with class declaration | |
829 return withstate->exp->type->toDsymbol(NULL)->search(loc, ident, 0); | |
830 } | |
831 | |
832 /****************************** ArrayScopeSymbol ******************************/ | |
833 | |
834 ArrayScopeSymbol::ArrayScopeSymbol(Expression *e) | |
835 : ScopeDsymbol() | |
836 { | |
837 assert(e->op == TOKindex || e->op == TOKslice); | |
838 exp = e; | |
839 type = NULL; | |
840 td = NULL; | |
841 } | |
842 | |
843 ArrayScopeSymbol::ArrayScopeSymbol(TypeTuple *t) | |
844 : ScopeDsymbol() | |
845 { | |
846 exp = NULL; | |
847 type = t; | |
848 td = NULL; | |
849 } | |
850 | |
851 ArrayScopeSymbol::ArrayScopeSymbol(TupleDeclaration *s) | |
852 : ScopeDsymbol() | |
853 { | |
854 exp = NULL; | |
855 type = NULL; | |
856 td = s; | |
857 } | |
858 | |
859 Dsymbol *ArrayScopeSymbol::search(Loc loc, Identifier *ident, int flags) | |
860 { | |
861 //printf("ArrayScopeSymbol::search('%s', flags = %d)\n", ident->toChars(), flags); | |
862 if (ident == Id::length || ident == Id::dollar) | |
863 { VarDeclaration **pvar; | |
864 Expression *ce; | |
865 | |
866 L1: | |
867 | |
868 if (td) | |
869 { | |
870 VarDeclaration *v = new VarDeclaration(loc, Type::tsize_t, Id::dollar, NULL); | |
871 Expression *e = new IntegerExp(0, td->objects->dim, Type::tsize_t); | |
872 v->init = new ExpInitializer(0, e); | |
873 v->storage_class |= STCconst; | |
874 return v; | |
875 } | |
876 | |
877 if (type) | |
878 { | |
879 VarDeclaration *v = new VarDeclaration(loc, Type::tsize_t, Id::dollar, NULL); | |
880 Expression *e = new IntegerExp(0, type->arguments->dim, Type::tsize_t); | |
881 v->init = new ExpInitializer(0, e); | |
882 v->storage_class |= STCconst; | |
883 return v; | |
884 } | |
885 | |
886 if (exp->op == TOKindex) | |
887 { | |
888 IndexExp *ie = (IndexExp *)exp; | |
889 | |
890 pvar = &ie->lengthVar; | |
891 ce = ie->e1; | |
892 } | |
893 else if (exp->op == TOKslice) | |
894 { | |
895 SliceExp *se = (SliceExp *)exp; | |
896 | |
897 pvar = &se->lengthVar; | |
898 ce = se->e1; | |
899 } | |
900 else | |
901 return NULL; | |
902 | |
903 if (ce->op == TOKtype) | |
904 { | |
905 Type *t = ((TypeExp *)ce)->type; | |
906 if (t->ty == Ttuple) | |
907 { type = (TypeTuple *)t; | |
908 goto L1; | |
909 } | |
910 } | |
911 | |
912 if (!*pvar) | |
913 { | |
914 VarDeclaration *v = new VarDeclaration(loc, Type::tsize_t, Id::dollar, NULL); | |
915 | |
916 if (ce->op == TOKstring) | |
917 { /* It is for a string literal, so the | |
918 * length will be a const. | |
919 */ | |
920 Expression *e = new IntegerExp(0, ((StringExp *)ce)->len, Type::tsize_t); | |
921 v->init = new ExpInitializer(0, e); | |
922 v->storage_class |= STCconst; | |
923 } | |
924 else if (ce->op == TOKarrayliteral) | |
925 { /* It is for an array literal, so the | |
926 * length will be a const. | |
927 */ | |
928 Expression *e = new IntegerExp(0, ((ArrayLiteralExp *)ce)->elements->dim, Type::tsize_t); | |
929 v->init = new ExpInitializer(0, e); | |
930 v->storage_class |= STCconst; | |
931 } | |
932 else if (ce->op == TOKtuple) | |
933 { /* It is for an expression tuple, so the | |
934 * length will be a const. | |
935 */ | |
936 Expression *e = new IntegerExp(0, ((TupleExp *)ce)->exps->dim, Type::tsize_t); | |
937 v->init = new ExpInitializer(0, e); | |
938 v->storage_class |= STCconst; | |
939 } | |
940 *pvar = v; | |
941 } | |
942 return (*pvar); | |
943 } | |
944 return NULL; | |
945 } | |
946 | |
947 | |
948 /****************************** DsymbolTable ******************************/ | |
949 | |
950 DsymbolTable::DsymbolTable() | |
951 { | |
952 tab = new StringTable; | |
953 } | |
954 | |
955 DsymbolTable::~DsymbolTable() | |
956 { | |
957 delete tab; | |
958 } | |
959 | |
960 Dsymbol *DsymbolTable::lookup(Identifier *ident) | |
961 { StringValue *sv; | |
962 | |
963 #ifdef DEBUG | |
964 assert(ident); | |
965 assert(tab); | |
966 #endif | |
967 sv = tab->lookup((char*)ident->string, ident->len); | |
968 return (Dsymbol *)(sv ? sv->ptrvalue : NULL); | |
969 } | |
970 | |
971 Dsymbol *DsymbolTable::insert(Dsymbol *s) | |
972 { StringValue *sv; | |
973 Identifier *ident; | |
974 | |
975 //printf("DsymbolTable::insert(this = %p, '%s')\n", this, s->ident->toChars()); | |
976 ident = s->ident; | |
977 #ifdef DEBUG | |
978 assert(ident); | |
979 assert(tab); | |
980 #endif | |
981 sv = tab->insert(ident->toChars(), ident->len); | |
982 if (!sv) | |
983 return NULL; // already in table | |
984 sv->ptrvalue = s; | |
985 return s; | |
986 } | |
987 | |
988 Dsymbol *DsymbolTable::insert(Identifier *ident, Dsymbol *s) | |
989 { StringValue *sv; | |
990 | |
991 //printf("DsymbolTable::insert()\n"); | |
992 sv = tab->insert(ident->toChars(), ident->len); | |
993 if (!sv) | |
994 return NULL; // already in table | |
995 sv->ptrvalue = s; | |
996 return s; | |
997 } | |
998 | |
999 Dsymbol *DsymbolTable::update(Dsymbol *s) | |
1000 { StringValue *sv; | |
1001 Identifier *ident; | |
1002 | |
1003 ident = s->ident; | |
1004 sv = tab->update(ident->toChars(), ident->len); | |
1005 sv->ptrvalue = s; | |
1006 return s; | |
1007 } | |
1008 | |
1009 | |
1010 | |
1011 |