Mercurial > projects > ldc
annotate dmd/dsymbol.c @ 1146:1860414bf3b7
* Moved ir/irsymbol.cpp/h into ir/irdsymbol.cpp/h.
* Added #if IN_DMD versioning around DMD backend specific code in the D1 frontend, D2 is currently broken.
* All part of a greater cleanup scheme.
author | Tomas Lindquist Olsen <tomas.l.olsen gmail.com> |
---|---|
date | Thu, 26 Mar 2009 20:45:53 +0100 |
parents | b30fe7e1dbb9 |
children | dbe4af57b240 |
rev | line source |
---|---|
159 | 1 |
2 // Compiler implementation of the D programming language | |
336 | 3 // Copyright (c) 1999-2008 by Digital Mars |
159 | 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 | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
946
diff
changeset
|
15 #include "rmem.h" |
159 | 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; | |
1146
1860414bf3b7
* Moved ir/irsymbol.cpp/h into ir/irdsymbol.cpp/h.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1103
diff
changeset
|
42 #if IN_DMD |
159 | 43 this->csym = NULL; |
44 this->isym = NULL; | |
1146
1860414bf3b7
* Moved ir/irsymbol.cpp/h into ir/irdsymbol.cpp/h.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1103
diff
changeset
|
45 #endif |
159 | 46 this->loc = 0; |
47 this->comment = NULL; | |
48 | |
1146
1860414bf3b7
* Moved ir/irsymbol.cpp/h into ir/irdsymbol.cpp/h.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1103
diff
changeset
|
49 #if IN_LLVM |
159 | 50 this->llvmInternal = LLVMnone; |
1146
1860414bf3b7
* Moved ir/irsymbol.cpp/h into ir/irdsymbol.cpp/h.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1103
diff
changeset
|
51 #endif |
159 | 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; | |
1146
1860414bf3b7
* Moved ir/irsymbol.cpp/h into ir/irdsymbol.cpp/h.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1103
diff
changeset
|
60 #if IN_DMD |
159 | 61 this->csym = NULL; |
62 this->isym = NULL; | |
1146
1860414bf3b7
* Moved ir/irsymbol.cpp/h into ir/irdsymbol.cpp/h.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1103
diff
changeset
|
63 #endif |
159 | 64 this->loc = 0; |
65 this->comment = NULL; | |
66 | |
1146
1860414bf3b7
* Moved ir/irsymbol.cpp/h into ir/irdsymbol.cpp/h.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1103
diff
changeset
|
67 #if IN_LLVM |
159 | 68 this->llvmInternal = LLVMnone; |
1146
1860414bf3b7
* Moved ir/irsymbol.cpp/h into ir/irdsymbol.cpp/h.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1103
diff
changeset
|
69 #endif |
159 | 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 | |
336 | 208 const char *Dsymbol::kind() |
159 | 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 | |
336 | 251 TemplateInstance *Dsymbol::inTemplateInstance() |
252 { | |
253 for (Dsymbol *parent = this->parent; parent; parent = parent->parent) | |
254 { | |
255 TemplateInstance *ti = parent->isTemplateInstance(); | |
256 if (ti) | |
257 return ti; | |
258 } | |
259 return NULL; | |
260 } | |
159 | 261 |
262 int Dsymbol::isAnonymous() | |
263 { | |
264 return ident ? 0 : 1; | |
265 } | |
266 | |
267 void Dsymbol::semantic(Scope *sc) | |
268 { | |
269 error("%p has no semantic routine", this); | |
270 } | |
271 | |
272 void Dsymbol::semantic2(Scope *sc) | |
273 { | |
274 // Most Dsymbols have no further semantic analysis needed | |
275 } | |
276 | |
277 void Dsymbol::semantic3(Scope *sc) | |
278 { | |
279 // Most Dsymbols have no further semantic analysis needed | |
280 } | |
281 | |
282 void Dsymbol::inlineScan() | |
283 { | |
284 // Most Dsymbols have no further semantic analysis needed | |
285 } | |
286 | |
336 | 287 /********************************************* |
288 * Search for ident as member of s. | |
289 * Input: | |
290 * flags: 1 don't find private members | |
291 * 2 don't give error messages | |
292 * 4 return NULL if ambiguous | |
293 * Returns: | |
294 * NULL if not found | |
295 */ | |
296 | |
159 | 297 Dsymbol *Dsymbol::search(Loc loc, Identifier *ident, int flags) |
298 { | |
299 //printf("Dsymbol::search(this=%p,%s, ident='%s')\n", this, toChars(), ident->toChars()); | |
300 return NULL; | |
301 } | |
302 | |
303 /*************************************** | |
304 * Search for identifier id as a member of 'this'. | |
305 * id may be a template instance. | |
306 * Returns: | |
307 * symbol found, NULL if not | |
308 */ | |
309 | |
310 Dsymbol *Dsymbol::searchX(Loc loc, Scope *sc, Identifier *id) | |
311 { | |
312 //printf("Dsymbol::searchX(this=%p,%s, ident='%s')\n", this, toChars(), ident->toChars()); | |
313 Dsymbol *s = toAlias(); | |
314 Dsymbol *sm; | |
315 | |
316 switch (id->dyncast()) | |
317 { | |
318 case DYNCAST_IDENTIFIER: | |
319 sm = s->search(loc, id, 0); | |
320 break; | |
321 | |
322 case DYNCAST_DSYMBOL: | |
323 { // It's a template instance | |
324 //printf("\ttemplate instance id\n"); | |
325 Dsymbol *st = (Dsymbol *)id; | |
326 TemplateInstance *ti = st->isTemplateInstance(); | |
327 id = ti->name; | |
328 sm = s->search(loc, id, 0); | |
329 if (!sm) | |
330 { error("template identifier %s is not a member of %s %s", | |
331 id->toChars(), s->kind(), s->toChars()); | |
332 return NULL; | |
333 } | |
334 sm = sm->toAlias(); | |
335 TemplateDeclaration *td = sm->isTemplateDeclaration(); | |
336 if (!td) | |
337 { | |
338 error("%s is not a template, it is a %s", id->toChars(), sm->kind()); | |
339 return NULL; | |
340 } | |
341 ti->tempdecl = td; | |
342 if (!ti->semanticdone) | |
343 ti->semantic(sc); | |
344 sm = ti->toAlias(); | |
345 break; | |
346 } | |
347 | |
348 default: | |
349 assert(0); | |
350 } | |
351 return sm; | |
352 } | |
353 | |
354 int Dsymbol::overloadInsert(Dsymbol *s) | |
355 { | |
356 //printf("Dsymbol::overloadInsert('%s')\n", s->toChars()); | |
357 return FALSE; | |
358 } | |
359 | |
360 void Dsymbol::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
361 { | |
362 buf->writestring(toChars()); | |
363 } | |
364 | |
365 unsigned Dsymbol::size(Loc loc) | |
366 { | |
367 error("Dsymbol '%s' has no size\n", toChars()); | |
368 return 0; | |
369 } | |
370 | |
371 int Dsymbol::isforwardRef() | |
372 { | |
373 return FALSE; | |
374 } | |
375 | |
376 AggregateDeclaration *Dsymbol::isThis() | |
377 { | |
378 return NULL; | |
379 } | |
380 | |
381 ClassDeclaration *Dsymbol::isClassMember() // are we a member of a class? | |
382 { | |
383 Dsymbol *parent = toParent(); | |
384 if (parent && parent->isClassDeclaration()) | |
385 return (ClassDeclaration *)parent; | |
386 return NULL; | |
387 } | |
388 | |
389 void Dsymbol::defineRef(Dsymbol *s) | |
390 { | |
391 assert(0); | |
392 } | |
393 | |
394 int Dsymbol::isExport() | |
395 { | |
396 return FALSE; | |
397 } | |
398 | |
399 int Dsymbol::isImportedSymbol() | |
400 { | |
401 return FALSE; | |
402 } | |
403 | |
404 int Dsymbol::isDeprecated() | |
405 { | |
406 return FALSE; | |
407 } | |
408 | |
409 LabelDsymbol *Dsymbol::isLabel() // is this a LabelDsymbol()? | |
410 { | |
411 return NULL; | |
412 } | |
413 | |
414 AggregateDeclaration *Dsymbol::isMember() // is this a member of an AggregateDeclaration? | |
415 { | |
336 | 416 //printf("Dsymbol::isMember() %s\n", toChars()); |
159 | 417 Dsymbol *parent = toParent(); |
336 | 418 //printf("parent is %s %s\n", parent->kind(), parent->toChars()); |
159 | 419 return parent ? parent->isAggregateDeclaration() : NULL; |
420 } | |
421 | |
422 Type *Dsymbol::getType() | |
423 { | |
424 return NULL; | |
425 } | |
426 | |
427 int Dsymbol::needThis() | |
428 { | |
429 return FALSE; | |
430 } | |
431 | |
432 int Dsymbol::addMember(Scope *sc, ScopeDsymbol *sd, int memnum) | |
433 { | |
434 //printf("Dsymbol::addMember('%s')\n", toChars()); | |
435 //printf("Dsymbol::addMember(this = %p, '%s' scopesym = '%s')\n", this, toChars(), sd->toChars()); | |
436 //printf("Dsymbol::addMember(this = %p, '%s' sd = %p, sd->symtab = %p)\n", this, toChars(), sd, sd->symtab); | |
437 parent = sd; | |
438 if (!isAnonymous()) // no name, so can't add it to symbol table | |
439 { | |
440 if (!sd->symtab->insert(this)) // if name is already defined | |
441 { | |
442 Dsymbol *s2; | |
443 | |
444 s2 = sd->symtab->lookup(ident); | |
445 if (!s2->overloadInsert(this)) | |
446 { | |
447 sd->multiplyDefined(0, this, s2); | |
448 } | |
449 } | |
450 if (sd->isAggregateDeclaration() || sd->isEnumDeclaration()) | |
451 { | |
452 if (ident == Id::__sizeof || ident == Id::alignof || ident == Id::mangleof) | |
453 error(".%s property cannot be redefined", ident->toChars()); | |
454 } | |
455 return 1; | |
456 } | |
457 return 0; | |
458 } | |
459 | |
460 void Dsymbol::error(const char *format, ...) | |
461 { | |
462 //printf("Dsymbol::error()\n"); | |
463 if (!global.gag) | |
464 { | |
465 char *p = locToChars(); | |
466 | |
467 if (*p) | |
468 fprintf(stdmsg, "%s: ", p); | |
469 mem.free(p); | |
470 | |
427
f1d37dc5d354
Give errors originating from Dsymbol::error the proper 'Error:' heading.
Christian Kamm <kamm incasoftware de>
parents:
336
diff
changeset
|
471 fprintf(stdmsg, "Error: "); |
159 | 472 if (isAnonymous()) |
473 fprintf(stdmsg, "%s ", kind()); | |
474 else | |
475 fprintf(stdmsg, "%s %s ", kind(), toPrettyChars()); | |
476 | |
477 va_list ap; | |
478 va_start(ap, format); | |
479 vfprintf(stdmsg, format, ap); | |
480 va_end(ap); | |
481 | |
482 fprintf(stdmsg, "\n"); | |
483 fflush(stdmsg); | |
484 } | |
485 global.errors++; | |
486 | |
487 //fatal(); | |
488 } | |
489 | |
490 void Dsymbol::error(Loc loc, const char *format, ...) | |
491 { | |
492 if (!global.gag) | |
493 { | |
494 char *p = loc.toChars(); | |
495 if (!*p) | |
496 p = locToChars(); | |
497 | |
498 if (*p) | |
499 fprintf(stdmsg, "%s: ", p); | |
500 mem.free(p); | |
501 | |
427
f1d37dc5d354
Give errors originating from Dsymbol::error the proper 'Error:' heading.
Christian Kamm <kamm incasoftware de>
parents:
336
diff
changeset
|
502 fprintf(stdmsg, "Error: "); |
159 | 503 fprintf(stdmsg, "%s %s ", kind(), toPrettyChars()); |
504 | |
505 va_list ap; | |
506 va_start(ap, format); | |
507 vfprintf(stdmsg, format, ap); | |
508 va_end(ap); | |
509 | |
510 fprintf(stdmsg, "\n"); | |
511 fflush(stdmsg); | |
512 } | |
513 | |
514 global.errors++; | |
515 | |
516 //fatal(); | |
517 } | |
518 | |
519 void Dsymbol::checkDeprecated(Loc loc, Scope *sc) | |
520 { | |
521 if (!global.params.useDeprecated && isDeprecated()) | |
522 { | |
523 // Don't complain if we're inside a deprecated symbol's scope | |
524 for (Dsymbol *sp = sc->parent; sp; sp = sp->parent) | |
525 { if (sp->isDeprecated()) | |
526 return; | |
527 } | |
528 | |
529 for (; sc; sc = sc->enclosing) | |
530 { | |
531 if (sc->scopesym && sc->scopesym->isDeprecated()) | |
532 return; | |
336 | 533 |
534 // If inside a StorageClassDeclaration that is deprecated | |
535 if (sc->stc & STCdeprecated) | |
536 return; | |
159 | 537 } |
538 | |
539 error(loc, "is deprecated"); | |
540 } | |
541 } | |
542 | |
543 /********************************** | |
544 * Determine which Module a Dsymbol is in. | |
545 */ | |
546 | |
547 Module *Dsymbol::getModule() | |
548 { | |
549 Module *m; | |
550 Dsymbol *s; | |
551 | |
552 //printf("Dsymbol::getModule()\n"); | |
553 s = this; | |
554 while (s) | |
555 { | |
556 //printf("\ts = '%s'\n", s->toChars()); | |
557 m = s->isModule(); | |
558 if (m) | |
559 return m; | |
560 s = s->parent; | |
561 } | |
562 return NULL; | |
563 } | |
564 | |
946
1714836f2c0b
Mostly rewrite debug info generation in terms of llvm/Analysis/DebugInfo.h.
Christian Kamm <kamm incasoftware de>
parents:
626
diff
changeset
|
565 |
1714836f2c0b
Mostly rewrite debug info generation in terms of llvm/Analysis/DebugInfo.h.
Christian Kamm <kamm incasoftware de>
parents:
626
diff
changeset
|
566 /********************************** |
1714836f2c0b
Mostly rewrite debug info generation in terms of llvm/Analysis/DebugInfo.h.
Christian Kamm <kamm incasoftware de>
parents:
626
diff
changeset
|
567 * Determine which Module a Dsymbol will be compiled in. |
1714836f2c0b
Mostly rewrite debug info generation in terms of llvm/Analysis/DebugInfo.h.
Christian Kamm <kamm incasoftware de>
parents:
626
diff
changeset
|
568 * This may be different from getModule for templates. |
1714836f2c0b
Mostly rewrite debug info generation in terms of llvm/Analysis/DebugInfo.h.
Christian Kamm <kamm incasoftware de>
parents:
626
diff
changeset
|
569 */ |
1714836f2c0b
Mostly rewrite debug info generation in terms of llvm/Analysis/DebugInfo.h.
Christian Kamm <kamm incasoftware de>
parents:
626
diff
changeset
|
570 |
1714836f2c0b
Mostly rewrite debug info generation in terms of llvm/Analysis/DebugInfo.h.
Christian Kamm <kamm incasoftware de>
parents:
626
diff
changeset
|
571 Module *Dsymbol::getCompilationModule() |
1714836f2c0b
Mostly rewrite debug info generation in terms of llvm/Analysis/DebugInfo.h.
Christian Kamm <kamm incasoftware de>
parents:
626
diff
changeset
|
572 { |
1714836f2c0b
Mostly rewrite debug info generation in terms of llvm/Analysis/DebugInfo.h.
Christian Kamm <kamm incasoftware de>
parents:
626
diff
changeset
|
573 Module *m; |
1714836f2c0b
Mostly rewrite debug info generation in terms of llvm/Analysis/DebugInfo.h.
Christian Kamm <kamm incasoftware de>
parents:
626
diff
changeset
|
574 TemplateInstance *ti; |
1714836f2c0b
Mostly rewrite debug info generation in terms of llvm/Analysis/DebugInfo.h.
Christian Kamm <kamm incasoftware de>
parents:
626
diff
changeset
|
575 Dsymbol *s; |
1714836f2c0b
Mostly rewrite debug info generation in terms of llvm/Analysis/DebugInfo.h.
Christian Kamm <kamm incasoftware de>
parents:
626
diff
changeset
|
576 |
1714836f2c0b
Mostly rewrite debug info generation in terms of llvm/Analysis/DebugInfo.h.
Christian Kamm <kamm incasoftware de>
parents:
626
diff
changeset
|
577 //printf("Dsymbol::getModule()\n"); |
1714836f2c0b
Mostly rewrite debug info generation in terms of llvm/Analysis/DebugInfo.h.
Christian Kamm <kamm incasoftware de>
parents:
626
diff
changeset
|
578 s = this; |
1714836f2c0b
Mostly rewrite debug info generation in terms of llvm/Analysis/DebugInfo.h.
Christian Kamm <kamm incasoftware de>
parents:
626
diff
changeset
|
579 while (s) |
1714836f2c0b
Mostly rewrite debug info generation in terms of llvm/Analysis/DebugInfo.h.
Christian Kamm <kamm incasoftware de>
parents:
626
diff
changeset
|
580 { |
1714836f2c0b
Mostly rewrite debug info generation in terms of llvm/Analysis/DebugInfo.h.
Christian Kamm <kamm incasoftware de>
parents:
626
diff
changeset
|
581 //printf("\ts = '%s'\n", s->toChars()); |
1714836f2c0b
Mostly rewrite debug info generation in terms of llvm/Analysis/DebugInfo.h.
Christian Kamm <kamm incasoftware de>
parents:
626
diff
changeset
|
582 m = s->isModule(); |
1714836f2c0b
Mostly rewrite debug info generation in terms of llvm/Analysis/DebugInfo.h.
Christian Kamm <kamm incasoftware de>
parents:
626
diff
changeset
|
583 if (m) |
1714836f2c0b
Mostly rewrite debug info generation in terms of llvm/Analysis/DebugInfo.h.
Christian Kamm <kamm incasoftware de>
parents:
626
diff
changeset
|
584 return m; |
1714836f2c0b
Mostly rewrite debug info generation in terms of llvm/Analysis/DebugInfo.h.
Christian Kamm <kamm incasoftware de>
parents:
626
diff
changeset
|
585 ti = s->isTemplateInstance(); |
1714836f2c0b
Mostly rewrite debug info generation in terms of llvm/Analysis/DebugInfo.h.
Christian Kamm <kamm incasoftware de>
parents:
626
diff
changeset
|
586 if (ti && ti->tmodule) |
1714836f2c0b
Mostly rewrite debug info generation in terms of llvm/Analysis/DebugInfo.h.
Christian Kamm <kamm incasoftware de>
parents:
626
diff
changeset
|
587 return ti->tmodule; |
1714836f2c0b
Mostly rewrite debug info generation in terms of llvm/Analysis/DebugInfo.h.
Christian Kamm <kamm incasoftware de>
parents:
626
diff
changeset
|
588 s = s->parent; |
1714836f2c0b
Mostly rewrite debug info generation in terms of llvm/Analysis/DebugInfo.h.
Christian Kamm <kamm incasoftware de>
parents:
626
diff
changeset
|
589 } |
1714836f2c0b
Mostly rewrite debug info generation in terms of llvm/Analysis/DebugInfo.h.
Christian Kamm <kamm incasoftware de>
parents:
626
diff
changeset
|
590 return NULL; |
1714836f2c0b
Mostly rewrite debug info generation in terms of llvm/Analysis/DebugInfo.h.
Christian Kamm <kamm incasoftware de>
parents:
626
diff
changeset
|
591 } |
1714836f2c0b
Mostly rewrite debug info generation in terms of llvm/Analysis/DebugInfo.h.
Christian Kamm <kamm incasoftware de>
parents:
626
diff
changeset
|
592 |
159 | 593 /************************************* |
594 */ | |
595 | |
596 enum PROT Dsymbol::prot() | |
597 { | |
598 return PROTpublic; | |
599 } | |
600 | |
601 /************************************* | |
602 * Do syntax copy of an array of Dsymbol's. | |
603 */ | |
604 | |
605 | |
606 Array *Dsymbol::arraySyntaxCopy(Array *a) | |
607 { | |
608 | |
609 Array *b = NULL; | |
610 if (a) | |
611 { | |
612 b = a->copy(); | |
613 for (int i = 0; i < b->dim; i++) | |
614 { | |
615 Dsymbol *s = (Dsymbol *)b->data[i]; | |
616 | |
617 s = s->syntaxCopy(NULL); | |
618 b->data[i] = (void *)s; | |
619 } | |
620 } | |
621 return b; | |
622 } | |
623 | |
624 | |
625 /**************************************** | |
626 * Add documentation comment to Dsymbol. | |
627 * Ignore NULL comments. | |
628 */ | |
629 | |
630 void Dsymbol::addComment(unsigned char *comment) | |
631 { | |
632 // if (comment) | |
633 // printf("adding comment '%s' to symbol %p '%s'\n", comment, this, toChars()); | |
634 | |
635 if (!this->comment) | |
636 this->comment = comment; | |
637 #if 1 | |
638 else if (comment && strcmp((char *)comment, (char *)this->comment)) | |
639 { // Concatenate the two | |
640 this->comment = Lexer::combineComments(this->comment, comment); | |
641 } | |
642 #endif | |
643 } | |
644 | |
645 | |
646 /********************************* ScopeDsymbol ****************************/ | |
647 | |
648 ScopeDsymbol::ScopeDsymbol() | |
649 : Dsymbol() | |
650 { | |
651 members = NULL; | |
652 symtab = NULL; | |
653 imports = NULL; | |
654 prots = NULL; | |
655 } | |
656 | |
657 ScopeDsymbol::ScopeDsymbol(Identifier *id) | |
658 : Dsymbol(id) | |
659 { | |
660 members = NULL; | |
661 symtab = NULL; | |
662 imports = NULL; | |
663 prots = NULL; | |
664 } | |
665 | |
666 Dsymbol *ScopeDsymbol::syntaxCopy(Dsymbol *s) | |
667 { | |
668 //printf("ScopeDsymbol::syntaxCopy('%s')\n", toChars()); | |
669 | |
670 ScopeDsymbol *sd; | |
671 if (s) | |
672 sd = (ScopeDsymbol *)s; | |
673 else | |
674 sd = new ScopeDsymbol(ident); | |
675 sd->members = arraySyntaxCopy(members); | |
676 return sd; | |
677 } | |
678 | |
679 Dsymbol *ScopeDsymbol::search(Loc loc, Identifier *ident, int flags) | |
336 | 680 { |
681 //printf("%s->ScopeDsymbol::search(ident='%s', flags=x%x)\n", toChars(), ident->toChars(), flags); | |
159 | 682 |
683 // Look in symbols declared in this module | |
336 | 684 Dsymbol *s = symtab ? symtab->lookup(ident) : NULL; |
626
e83ba4ae4878
Fix import visibility bugs 313 and 314.
Christian Kamm <kamm incasoftware de>
parents:
443
diff
changeset
|
685 |
e83ba4ae4878
Fix import visibility bugs 313 and 314.
Christian Kamm <kamm incasoftware de>
parents:
443
diff
changeset
|
686 // hide private nonlocal symbols |
e83ba4ae4878
Fix import visibility bugs 313 and 314.
Christian Kamm <kamm incasoftware de>
parents:
443
diff
changeset
|
687 if (flags & 1 && s && s->prot() == PROTprivate) |
e83ba4ae4878
Fix import visibility bugs 313 and 314.
Christian Kamm <kamm incasoftware de>
parents:
443
diff
changeset
|
688 s = NULL; |
e83ba4ae4878
Fix import visibility bugs 313 and 314.
Christian Kamm <kamm incasoftware de>
parents:
443
diff
changeset
|
689 |
159 | 690 if (s) |
691 { | |
692 //printf("\ts = '%s.%s'\n",toChars(),s->toChars()); | |
693 } | |
694 else if (imports) | |
695 { | |
696 // Look in imported modules | |
336 | 697 for (int i = 0; i < imports->dim; i++) |
159 | 698 { ScopeDsymbol *ss = (ScopeDsymbol *)imports->data[i]; |
699 Dsymbol *s2; | |
700 | |
701 // If private import, don't search it | |
702 if (flags & 1 && prots[i] == PROTprivate) | |
703 continue; | |
704 | |
705 //printf("\tscanning import '%s', prots = %d, isModule = %p, isImport = %p\n", ss->toChars(), prots[i], ss->isModule(), ss->isImport()); | |
336 | 706 /* Don't find private members if ss is a module |
707 */ | |
159 | 708 s2 = ss->search(loc, ident, ss->isModule() ? 1 : 0); |
709 if (!s) | |
710 s = s2; | |
711 else if (s2 && s != s2) | |
712 { | |
713 if (s->toAlias() == s2->toAlias()) | |
714 { | |
336 | 715 /* After following aliases, we found the same symbol, |
716 * so it's not an ambiguity. | |
717 * But if one alias is deprecated, prefer the other. | |
718 */ | |
159 | 719 if (s->isDeprecated()) |
720 s = s2; | |
721 } | |
722 else | |
723 { | |
724 /* Two imports of the same module should be regarded as | |
725 * the same. | |
726 */ | |
727 Import *i1 = s->isImport(); | |
728 Import *i2 = s2->isImport(); | |
729 if (!(i1 && i2 && | |
730 (i1->mod == i2->mod || | |
731 (!i1->parent->isImport() && !i2->parent->isImport() && | |
732 i1->ident->equals(i2->ident)) | |
733 ) | |
734 ) | |
735 ) | |
736 { | |
737 ss->multiplyDefined(loc, s, s2); | |
738 break; | |
739 } | |
740 } | |
741 } | |
742 } | |
743 if (s) | |
744 { | |
745 Declaration *d = s->isDeclaration(); | |
746 if (d && d->protection == PROTprivate && !d->parent->isTemplateMixin()) | |
747 error("%s is private", d->toPrettyChars()); | |
748 } | |
749 } | |
750 return s; | |
751 } | |
752 | |
753 void ScopeDsymbol::importScope(ScopeDsymbol *s, enum PROT protection) | |
754 { | |
755 //printf("%s->ScopeDsymbol::importScope(%s, %d)\n", toChars(), s->toChars(), protection); | |
756 | |
757 // No circular or redundant import's | |
758 if (s != this) | |
759 { | |
760 if (!imports) | |
761 imports = new Array(); | |
762 else | |
763 { | |
764 for (int i = 0; i < imports->dim; i++) | |
765 { ScopeDsymbol *ss; | |
766 | |
767 ss = (ScopeDsymbol *) imports->data[i]; | |
768 if (ss == s) | |
769 { | |
770 if (protection > prots[i]) | |
771 prots[i] = protection; // upgrade access | |
772 return; | |
773 } | |
774 } | |
775 } | |
776 imports->push(s); | |
777 prots = (unsigned char *)mem.realloc(prots, imports->dim * sizeof(prots[0])); | |
778 prots[imports->dim - 1] = protection; | |
779 } | |
780 } | |
781 | |
782 int ScopeDsymbol::isforwardRef() | |
783 { | |
784 return (members == NULL); | |
785 } | |
786 | |
787 void ScopeDsymbol::defineRef(Dsymbol *s) | |
788 { | |
789 ScopeDsymbol *ss; | |
790 | |
791 ss = s->isScopeDsymbol(); | |
792 members = ss->members; | |
793 ss->members = NULL; | |
794 } | |
795 | |
796 void ScopeDsymbol::multiplyDefined(Loc loc, Dsymbol *s1, Dsymbol *s2) | |
797 { | |
798 #if 0 | |
799 printf("ScopeDsymbol::multiplyDefined()\n"); | |
800 printf("s1 = %p, '%s' kind = '%s', parent = %s\n", s1, s1->toChars(), s1->kind(), s1->parent ? s1->parent->toChars() : ""); | |
801 printf("s2 = %p, '%s' kind = '%s', parent = %s\n", s2, s2->toChars(), s2->kind(), s2->parent ? s2->parent->toChars() : ""); | |
802 #endif | |
803 if (loc.filename) | |
804 { ::error(loc, "%s at %s conflicts with %s at %s", | |
805 s1->toPrettyChars(), | |
806 s1->locToChars(), | |
807 s2->toPrettyChars(), | |
808 s2->locToChars()); | |
809 } | |
810 else | |
811 { | |
812 s1->error(loc, "conflicts with %s %s at %s", | |
813 s2->kind(), | |
814 s2->toPrettyChars(), | |
815 s2->locToChars()); | |
816 } | |
817 } | |
818 | |
819 Dsymbol *ScopeDsymbol::nameCollision(Dsymbol *s) | |
820 { | |
821 Dsymbol *sprev; | |
822 | |
823 // Look to see if we are defining a forward referenced symbol | |
824 | |
825 sprev = symtab->lookup(s->ident); | |
826 assert(sprev); | |
827 if (s->equals(sprev)) // if the same symbol | |
828 { | |
829 if (s->isforwardRef()) // if second declaration is a forward reference | |
830 return sprev; | |
831 if (sprev->isforwardRef()) | |
832 { | |
833 sprev->defineRef(s); // copy data from s into sprev | |
834 return sprev; | |
835 } | |
836 } | |
837 multiplyDefined(0, s, sprev); | |
838 return sprev; | |
839 } | |
840 | |
336 | 841 const char *ScopeDsymbol::kind() |
159 | 842 { |
843 return "ScopeDsymbol"; | |
844 } | |
845 | |
846 | |
847 /******************************************* | |
848 * Look for member of the form: | |
849 * const(MemberInfo)[] getMembers(string); | |
850 * Returns NULL if not found | |
851 */ | |
852 | |
336 | 853 #if DMDV2 |
159 | 854 FuncDeclaration *ScopeDsymbol::findGetMembers() |
855 { | |
856 Dsymbol *s = search_function(this, Id::getmembers); | |
857 FuncDeclaration *fdx = s ? s->isFuncDeclaration() : NULL; | |
858 | |
859 #if 0 // Finish | |
860 static TypeFunction *tfgetmembers; | |
861 | |
862 if (!tfgetmembers) | |
863 { | |
864 Scope sc; | |
865 Arguments *arguments = new Arguments; | |
866 Arguments *arg = new Argument(STCin, Type::tchar->constOf()->arrayOf(), NULL, NULL); | |
867 arguments->push(arg); | |
868 | |
869 Type *tret = NULL; | |
870 tfgetmembers = new TypeFunction(arguments, tret, 0, LINKd); | |
871 tfgetmembers = (TypeFunction *)tfgetmembers->semantic(0, &sc); | |
872 } | |
873 if (fdx) | |
874 fdx = fdx->overloadExactMatch(tfgetmembers); | |
875 #endif | |
876 if (fdx && fdx->isVirtual()) | |
877 fdx = NULL; | |
878 | |
879 return fdx; | |
880 } | |
881 #endif | |
882 | |
883 | |
884 /****************************** WithScopeSymbol ******************************/ | |
885 | |
886 WithScopeSymbol::WithScopeSymbol(WithStatement *withstate) | |
887 : ScopeDsymbol() | |
888 { | |
889 this->withstate = withstate; | |
890 } | |
891 | |
892 Dsymbol *WithScopeSymbol::search(Loc loc, Identifier *ident, int flags) | |
893 { | |
894 // Acts as proxy to the with class declaration | |
895 return withstate->exp->type->toDsymbol(NULL)->search(loc, ident, 0); | |
896 } | |
897 | |
898 /****************************** ArrayScopeSymbol ******************************/ | |
899 | |
900 ArrayScopeSymbol::ArrayScopeSymbol(Expression *e) | |
901 : ScopeDsymbol() | |
902 { | |
903 assert(e->op == TOKindex || e->op == TOKslice); | |
904 exp = e; | |
905 type = NULL; | |
906 td = NULL; | |
907 } | |
908 | |
909 ArrayScopeSymbol::ArrayScopeSymbol(TypeTuple *t) | |
910 : ScopeDsymbol() | |
911 { | |
912 exp = NULL; | |
913 type = t; | |
914 td = NULL; | |
915 } | |
916 | |
917 ArrayScopeSymbol::ArrayScopeSymbol(TupleDeclaration *s) | |
918 : ScopeDsymbol() | |
919 { | |
920 exp = NULL; | |
921 type = NULL; | |
922 td = s; | |
923 } | |
924 | |
925 Dsymbol *ArrayScopeSymbol::search(Loc loc, Identifier *ident, int flags) | |
926 { | |
927 //printf("ArrayScopeSymbol::search('%s', flags = %d)\n", ident->toChars(), flags); | |
928 if (ident == Id::length || ident == Id::dollar) | |
929 { VarDeclaration **pvar; | |
930 Expression *ce; | |
931 | |
932 L1: | |
933 | |
934 if (td) | |
935 { | |
936 VarDeclaration *v = new VarDeclaration(loc, Type::tsize_t, Id::dollar, NULL); | |
937 Expression *e = new IntegerExp(0, td->objects->dim, Type::tsize_t); | |
938 v->init = new ExpInitializer(0, e); | |
939 v->storage_class |= STCconst; | |
940 return v; | |
941 } | |
942 | |
943 if (type) | |
944 { | |
945 VarDeclaration *v = new VarDeclaration(loc, Type::tsize_t, Id::dollar, NULL); | |
946 Expression *e = new IntegerExp(0, type->arguments->dim, Type::tsize_t); | |
947 v->init = new ExpInitializer(0, e); | |
948 v->storage_class |= STCconst; | |
949 return v; | |
950 } | |
951 | |
952 if (exp->op == TOKindex) | |
953 { | |
954 IndexExp *ie = (IndexExp *)exp; | |
955 | |
956 pvar = &ie->lengthVar; | |
957 ce = ie->e1; | |
958 } | |
959 else if (exp->op == TOKslice) | |
960 { | |
961 SliceExp *se = (SliceExp *)exp; | |
962 | |
963 pvar = &se->lengthVar; | |
964 ce = se->e1; | |
965 } | |
966 else | |
967 return NULL; | |
968 | |
969 if (ce->op == TOKtype) | |
970 { | |
971 Type *t = ((TypeExp *)ce)->type; | |
972 if (t->ty == Ttuple) | |
973 { type = (TypeTuple *)t; | |
974 goto L1; | |
975 } | |
976 } | |
977 | |
978 if (!*pvar) | |
979 { | |
980 VarDeclaration *v = new VarDeclaration(loc, Type::tsize_t, Id::dollar, NULL); | |
981 | |
982 if (ce->op == TOKstring) | |
983 { /* It is for a string literal, so the | |
984 * length will be a const. | |
985 */ | |
986 Expression *e = new IntegerExp(0, ((StringExp *)ce)->len, Type::tsize_t); | |
987 v->init = new ExpInitializer(0, e); | |
988 v->storage_class |= STCconst; | |
989 } | |
990 else if (ce->op == TOKarrayliteral) | |
991 { /* It is for an array literal, so the | |
992 * length will be a const. | |
993 */ | |
994 Expression *e = new IntegerExp(0, ((ArrayLiteralExp *)ce)->elements->dim, Type::tsize_t); | |
995 v->init = new ExpInitializer(0, e); | |
996 v->storage_class |= STCconst; | |
997 } | |
998 else if (ce->op == TOKtuple) | |
999 { /* It is for an expression tuple, so the | |
1000 * length will be a const. | |
1001 */ | |
1002 Expression *e = new IntegerExp(0, ((TupleExp *)ce)->exps->dim, Type::tsize_t); | |
1003 v->init = new ExpInitializer(0, e); | |
1004 v->storage_class |= STCconst; | |
1005 } | |
1006 *pvar = v; | |
1007 } | |
1008 return (*pvar); | |
1009 } | |
1010 return NULL; | |
1011 } | |
1012 | |
1013 | |
1014 /****************************** DsymbolTable ******************************/ | |
1015 | |
1016 DsymbolTable::DsymbolTable() | |
1017 { | |
1018 tab = new StringTable; | |
1019 } | |
1020 | |
1021 DsymbolTable::~DsymbolTable() | |
1022 { | |
1023 delete tab; | |
1024 } | |
1025 | |
1026 Dsymbol *DsymbolTable::lookup(Identifier *ident) | |
1027 { StringValue *sv; | |
1028 | |
1029 #ifdef DEBUG | |
1030 assert(ident); | |
1031 assert(tab); | |
1032 #endif | |
1033 sv = tab->lookup((char*)ident->string, ident->len); | |
1034 return (Dsymbol *)(sv ? sv->ptrvalue : NULL); | |
1035 } | |
1036 | |
1037 Dsymbol *DsymbolTable::insert(Dsymbol *s) | |
1038 { StringValue *sv; | |
1039 Identifier *ident; | |
1040 | |
1041 //printf("DsymbolTable::insert(this = %p, '%s')\n", this, s->ident->toChars()); | |
1042 ident = s->ident; | |
1043 #ifdef DEBUG | |
1044 assert(ident); | |
1045 assert(tab); | |
1046 #endif | |
1047 sv = tab->insert(ident->toChars(), ident->len); | |
1048 if (!sv) | |
1049 return NULL; // already in table | |
1050 sv->ptrvalue = s; | |
1051 return s; | |
1052 } | |
1053 | |
1054 Dsymbol *DsymbolTable::insert(Identifier *ident, Dsymbol *s) | |
1055 { StringValue *sv; | |
1056 | |
1057 //printf("DsymbolTable::insert()\n"); | |
1058 sv = tab->insert(ident->toChars(), ident->len); | |
1059 if (!sv) | |
1060 return NULL; // already in table | |
1061 sv->ptrvalue = s; | |
1062 return s; | |
1063 } | |
1064 | |
1065 Dsymbol *DsymbolTable::update(Dsymbol *s) | |
1066 { StringValue *sv; | |
1067 Identifier *ident; | |
1068 | |
1069 ident = s->ident; | |
1070 sv = tab->update(ident->toChars(), ident->len); | |
1071 sv->ptrvalue = s; | |
1072 return s; | |
1073 } | |
1074 | |
1075 | |
1076 | |
1077 |