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