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