Allow implicit conversion "foo*?" to "void*"
[delight/core.git] / dmd2 / scope.c
blob3de74709158bfd055962a8ac38762d11c5e1f238
2 // Copyright (c) 1999-2005 by Digital Mars
3 // All Rights Reserved
4 // written by Walter Bright
5 // http://www.digitalmars.com
6 // License for redistribution is by either the Artistic License
7 // in artistic.txt, or the GNU General Public License in gnu.txt.
8 // See the included readme.txt for details.
10 /* NOTE: This file has been patched from the original DMD distribution to
11 work with the GDC compiler.
13 Modified by David Friedman, December 2006
16 #include <stdio.h>
17 #include <assert.h>
19 #include "root.h"
21 #include "mars.h"
22 #include "init.h"
23 #include "identifier.h"
24 #include "scope.h"
25 #include "attrib.h"
26 #include "dsymbol.h"
27 #include "declaration.h"
28 #include "aggregate.h"
29 #include "module.h"
30 #include "id.h"
32 Scope *Scope::freelist = NULL;
34 void *Scope::operator new(size_t size)
36 if (freelist)
38 Scope *s = freelist;
39 freelist = s->enclosing;
40 //printf("freelist %p\n", s);
41 assert(s->flags & SCOPEfree);
42 s->flags &= ~SCOPEfree;
43 return s;
46 void *p = ::operator new(size);
47 //printf("new %p\n", p);
48 return p;
51 Scope::Scope()
52 { // Create root scope
54 //printf("Scope::Scope() %p\n", this);
55 this->module = NULL;
56 this->scopesym = NULL;
57 this->sd = NULL;
58 this->enclosing = NULL;
59 this->parent = NULL;
60 this->sw = NULL;
61 this->tf = NULL;
62 this->sbreak = NULL;
63 this->scontinue = NULL;
64 this->fes = NULL;
65 this->structalign = global.structalign;
66 this->func = NULL;
67 this->slabel = NULL;
68 this->linkage = LINKd;
69 this->protection = PROTpublic;
70 this->explicitProtection = 0;
71 this->stc = 0;
72 this->attributes = NULL;
73 this->offset = 0;
74 this->inunion = 0;
75 this->incontract = 0;
76 this->nofree = 0;
77 this->noctor = 0;
78 this->noaccesscheck = 0;
79 this->intypeof = 0;
80 this->parameterSpecialization = 0;
81 this->callSuper = 0;
82 this->flags = 0;
83 this->anonAgg = NULL;
84 this->lastdc = NULL;
85 this->lastoffset = 0;
86 this->docbuf = NULL;
89 Scope::Scope(Scope *enclosing)
91 //printf("Scope::Scope(enclosing = %p) %p\n", enclosing, this);
92 assert(!(enclosing->flags & SCOPEfree));
93 this->module = enclosing->module;
94 this->func = enclosing->func;
95 this->parent = enclosing->parent;
96 this->scopesym = NULL;
97 this->sd = NULL;
98 this->sw = enclosing->sw;
99 this->tf = enclosing->tf;
100 this->sbreak = enclosing->sbreak;
101 this->scontinue = enclosing->scontinue;
102 this->fes = enclosing->fes;
103 this->structalign = enclosing->structalign;
104 this->enclosing = enclosing;
105 #ifdef DEBUG
106 if (enclosing->enclosing)
107 assert(!(enclosing->enclosing->flags & SCOPEfree));
108 if (this == enclosing->enclosing)
110 printf("this = %p, enclosing = %p, enclosing->enclosing = %p\n", this, enclosing, enclosing->enclosing);
112 assert(this != enclosing->enclosing);
113 #endif
114 this->slabel = NULL;
115 this->linkage = enclosing->linkage;
116 this->protection = enclosing->protection;
117 this->explicitProtection = enclosing->explicitProtection;
118 this->stc = enclosing->stc;
119 this->attributes = enclosing->attributes;
120 this->offset = 0;
121 this->inunion = enclosing->inunion;
122 this->incontract = enclosing->incontract;
123 this->nofree = 0;
124 this->noctor = enclosing->noctor;
125 this->noaccesscheck = enclosing->noaccesscheck;
126 this->intypeof = enclosing->intypeof;
127 this->parameterSpecialization = enclosing->parameterSpecialization;
128 this->callSuper = enclosing->callSuper;
129 this->flags = 0;
130 this->anonAgg = NULL;
131 this->lastdc = NULL;
132 this->lastoffset = 0;
133 this->docbuf = enclosing->docbuf;
134 assert(this != enclosing);
137 Scope *Scope::createGlobal(Module *module)
139 Scope *sc;
141 sc = new Scope();
142 sc->module = module;
143 sc->scopesym = new ScopeDsymbol();
144 sc->scopesym->symtab = new DsymbolTable();
146 // Add top level package as member of this global scope
147 Dsymbol *m = module;
148 while (m->parent)
149 m = m->parent;
150 m->addMember(NULL, sc->scopesym, 1);
151 m->parent = NULL; // got changed by addMember()
153 // Create the module scope underneath the global scope
154 sc = sc->push(module);
155 sc->parent = module;
156 return sc;
159 Scope *Scope::push()
161 //printf("Scope::push()\n");
162 Scope *s = new Scope(this);
163 assert(this != s);
164 return s;
167 Scope *Scope::push(ScopeDsymbol *ss)
169 //printf("Scope::push(%s)\n", ss->toChars());
170 Scope *s = push();
171 s->scopesym = ss;
172 return s;
175 Scope *Scope::pop()
177 //printf("Scope::pop() %p nofree = %d\n", this, nofree);
178 Scope *enc = enclosing;
180 if (enclosing)
181 enclosing->callSuper |= callSuper;
183 if (!nofree)
184 { enclosing = freelist;
185 freelist = this;
186 flags |= SCOPEfree;
189 return enc;
192 void Scope::mergeCallSuper(Loc loc, unsigned cs)
194 // This does a primitive flow analysis to support the restrictions
195 // regarding when and how constructors can appear.
196 // It merges the results of two paths.
197 // The two paths are callSuper and cs; the result is merged into callSuper.
199 if (cs != callSuper)
200 { int a;
201 int b;
203 callSuper |= cs & (CSXany_ctor | CSXlabel);
204 if (cs & CSXreturn)
207 else if (callSuper & CSXreturn)
209 callSuper = cs | (callSuper & (CSXany_ctor | CSXlabel));
211 else
213 a = (cs & (CSXthis_ctor | CSXsuper_ctor)) != 0;
214 b = (callSuper & (CSXthis_ctor | CSXsuper_ctor)) != 0;
215 if (a != b)
216 error(loc, "one path skips constructor");
217 callSuper |= cs;
222 Dsymbol *Scope::search(Loc loc, Identifier *ident, Dsymbol **pscopesym)
223 { Dsymbol *s;
224 Scope *sc;
226 //printf("Scope::search(%p, '%s')\n", this, ident->toChars());
227 if (ident == Id::empty)
229 // Look for module scope
230 for (sc = this; sc; sc = sc->enclosing)
232 assert(sc != sc->enclosing);
233 if (sc->scopesym)
235 s = sc->scopesym->isModule();
236 if (s)
238 //printf("\tfound %s.%s\n", s->parent ? s->parent->toChars() : "", s->toChars());
239 if (pscopesym)
240 *pscopesym = sc->scopesym;
241 return s;
245 return NULL;
248 for (sc = this; sc; sc = sc->enclosing)
250 assert(sc != sc->enclosing);
251 if (sc->scopesym)
253 //printf("\tlooking in scopesym '%s', kind = '%s'\n", sc->scopesym->toChars(), sc->scopesym->kind());
254 s = sc->scopesym->search(loc, ident, 0);
255 if (s)
257 if ((global.params.warnings ||
258 global.params.Dversion > 1) &&
259 ident == Id::length &&
260 sc->scopesym->isArrayScopeSymbol() &&
261 sc->enclosing &&
262 sc->enclosing->search(loc, ident, NULL))
264 if (global.params.warnings)
265 fprintf(stdmsg, "warning - ");
266 error(s->loc, "array 'length' hides other 'length' name in outer scope");
269 //printf("\tfound %s.%s, kind = '%s'\n", s->parent ? s->parent->toChars() : "", s->toChars(), s->kind());
270 if (pscopesym)
271 *pscopesym = sc->scopesym;
272 return s;
277 return NULL;
280 Dsymbol *Scope::insert(Dsymbol *s)
281 { Scope *sc;
283 for (sc = this; sc; sc = sc->enclosing)
285 //printf("\tsc = %p\n", sc);
286 if (sc->scopesym)
288 //printf("\t\tsc->scopesym = %p\n", sc->scopesym);
289 if (!sc->scopesym->symtab)
290 sc->scopesym->symtab = new DsymbolTable();
291 return sc->scopesym->symtab->insert(s);
294 assert(0);
295 return NULL;
298 /********************************************
299 * Search enclosing scopes for ClassDeclaration.
302 ClassDeclaration *Scope::getClassScope()
303 { Scope *sc;
305 for (sc = this; sc; sc = sc->enclosing)
307 ClassDeclaration *cd;
309 if (sc->scopesym)
311 cd = sc->scopesym->isClassDeclaration();
312 if (cd)
313 return cd;
316 return NULL;
319 /********************************************
320 * Search enclosing scopes for ClassDeclaration.
323 AggregateDeclaration *Scope::getStructClassScope()
324 { Scope *sc;
326 for (sc = this; sc; sc = sc->enclosing)
328 AggregateDeclaration *ad;
330 if (sc->scopesym)
332 ad = sc->scopesym->isClassDeclaration();
333 if (ad)
334 return ad;
335 else
336 { ad = sc->scopesym->isStructDeclaration();
337 if (ad)
338 return ad;
342 return NULL;
345 /*******************************************
346 * For TemplateDeclarations, we need to remember the Scope
347 * where it was declared. So mark the Scope as not
348 * to be free'd.
351 void Scope::setNoFree()
352 { Scope *sc;
353 //int i = 0;
355 //printf("Scope::setNoFree(this = %p)\n", this);
356 for (sc = this; sc; sc = sc->enclosing)
358 //printf("\tsc = %p\n", sc);
359 sc->nofree = 1;
361 assert(!(flags & SCOPEfree));
362 //assert(sc != sc->enclosing);
363 //assert(!sc->enclosing || sc != sc->enclosing->enclosing);
364 //if (++i == 10)
365 //assert(0);