Mercurial > projects > ddmd
annotate dmd/Scope.d @ 114:e28b18c23469
added a module dmd.common for commonly used stuff
it currently holds code for consistency checking of predefined versions
also added a VisualD project file
author | Trass3r |
---|---|
date | Wed, 01 Sep 2010 18:21:58 +0200 |
parents | ee670dd808a8 |
children | 010eb8f0e18d |
rev | line source |
---|---|
0 | 1 module dmd.Scope; |
2 | |
114 | 3 import dmd.common; |
0 | 4 import dmd.Module; |
5 import dmd.ScopeDsymbol; | |
6 import dmd.FuncDeclaration; | |
7 import dmd.Id; | |
8 import dmd.Dsymbol; | |
9 import dmd.LabelStatement; | |
10 import dmd.SwitchStatement; | |
11 import dmd.TryFinallyStatement; | |
12 import dmd.TemplateInstance; | |
13 import dmd.Statement; | |
14 import dmd.ForeachStatement; | |
15 import dmd.LINK; | |
16 import dmd.PROT; | |
17 import dmd.STC; | |
18 import dmd.AnonymousAggregateDeclaration; | |
19 import dmd.AggregateDeclaration; | |
20 import dmd.ClassDeclaration; | |
21 import dmd.Identifier; | |
22 import dmd.Loc; | |
23 import dmd.OutBuffer; | |
24 import dmd.DocComment; | |
25 import dmd.DsymbolTable; | |
26 import dmd.Global; | |
27 import dmd.CSX; | |
28 import dmd.Util; | |
29 | |
83
ee670dd808a8
malloc -> GC.malloc change, plus compilation issues fixed (always check before commiting stuff!!!)
korDen
parents:
82
diff
changeset
|
30 import core.memory; |
ee670dd808a8
malloc -> GC.malloc change, plus compilation issues fixed (always check before commiting stuff!!!)
korDen
parents:
82
diff
changeset
|
31 |
0 | 32 enum SCOPE |
33 { | |
34 SCOPEctor = 1, // constructor type | |
35 SCOPEstaticif = 2, // inside static if | |
36 SCOPEfree = 4, // is on free list | |
37 } | |
38 | |
39 class Scope | |
40 { | |
41 Scope enclosing; // enclosing Scope | |
42 | |
43 Module module_; // Root module | |
44 ScopeDsymbol scopesym; // current symbol | |
45 ScopeDsymbol sd; // if in static if, and declaring new symbols, | |
46 // sd gets the addMember() | |
47 FuncDeclaration func; // function we are in | |
48 Dsymbol parent; // parent to use | |
49 LabelStatement slabel; // enclosing labelled statement | |
50 SwitchStatement sw; // enclosing switch statement | |
51 TryFinallyStatement tf; // enclosing try finally statement | |
52 TemplateInstance tinst; // enclosing template instance | |
53 Statement sbreak; // enclosing statement that supports "break" | |
54 Statement scontinue; // enclosing statement that supports "continue" | |
55 ForeachStatement fes; // if nested function for ForeachStatement, this is it | |
56 uint offset; // next offset to use in aggregate | |
57 int inunion; // we're processing members of a union | |
58 int incontract; // we're inside contract code | |
59 int nofree; // set if shouldn't free it | |
60 int noctor; // set if constructor calls aren't allowed | |
61 int intypeof; // in typeof(exp) | |
62 int parameterSpecialization; // if in template parameter specialization | |
63 int noaccesscheck; // don't do access checks | |
64 int mustsemantic; // cannot defer semantic() | |
65 | |
66 uint callSuper; // primitive flow analysis for constructors | |
67 ///#define CSXthis_ctor 1 // called this() | |
68 ///#define CSXsuper_ctor 2 // called super() | |
69 ///#define CSXthis 4 // referenced this | |
70 ///#define CSXsuper 8 // referenced super | |
71 ///#define CSXlabel 0x10 // seen a label | |
72 ///#define CSXreturn 0x20 // seen a return statement | |
73 ///#define CSXany_ctor 0x40 // either this() or super() was called | |
74 | |
75 uint structalign; // alignment for struct members | |
76 LINK linkage = LINK.LINKd; // linkage for external functions | |
77 | |
78 PROT protection = PROT.PROTpublic; // protection for class members | |
79 int explicitProtection; // set if in an explicit protection attribute | |
80 | |
81 STC stc; // storage class | |
82 | |
83 SCOPE flags; | |
84 | |
85 AnonymousAggregateDeclaration anonAgg; // for temporary analysis | |
86 | |
87 DocComment lastdc; // documentation comment for last symbol at this scope | |
88 uint lastoffset; // offset in docbuf of where to insert next dec | |
89 OutBuffer docbuf; // buffer for documentation output | |
90 | |
91 static Scope freelist; | |
92 /// static void *operator new(size_t sz); | |
93 static Scope createGlobal(Module module_) | |
94 { | |
95 Scope sc = new Scope(); | |
96 sc.module_ = module_; | |
97 sc.scopesym = new ScopeDsymbol(); | |
98 sc.scopesym.symtab = new DsymbolTable(); | |
99 | |
100 // Add top level package as member of this global scope | |
101 Dsymbol m = module_; | |
102 while (m.parent !is null) | |
103 m = m.parent; | |
104 | |
13 | 105 m.addMember(null, sc.scopesym, true); |
0 | 106 m.parent = null; // got changed by addMember() |
107 | |
108 // Create the module scope underneath the global scope | |
109 sc = sc.push(module_); | |
110 sc.parent = module_; | |
111 return sc; | |
112 } | |
113 | |
114 this() | |
115 { | |
116 // Create root scope | |
117 | |
118 //printf("Scope.Scope() %p\n", this); | |
21
26b9f97f6162
Added in some = new OutBuffer();'s to be on the safe side, been getting some issues with null OutBuffers.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
119 this.docbuf = new OutBuffer; |
0 | 120 this.structalign = global.structalign; |
121 } | |
122 | |
123 this(Module module_) | |
124 { | |
125 assert(false); | |
21
26b9f97f6162
Added in some = new OutBuffer();'s to be on the safe side, been getting some issues with null OutBuffers.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
126 this.docbuf = new OutBuffer; |
0 | 127 } |
128 | |
129 this(Scope enclosing) | |
130 { | |
131 //printf("Scope.Scope(enclosing = %p) %p\n", enclosing, this); | |
132 assert(!(enclosing.flags & SCOPE.SCOPEfree)); | |
133 this.module_ = enclosing.module_; | |
134 this.func = enclosing.func; | |
135 this.parent = enclosing.parent; | |
79 | 136 this.scopesym = null; |
137 this.sd = null; | |
0 | 138 this.sw = enclosing.sw; |
139 this.tf = enclosing.tf; | |
140 this.tinst = enclosing.tinst; | |
141 this.sbreak = enclosing.sbreak; | |
142 this.scontinue = enclosing.scontinue; | |
143 this.fes = enclosing.fes; | |
144 this.structalign = enclosing.structalign; | |
145 this.enclosing = enclosing; | |
146 debug { | |
147 if (enclosing.enclosing) | |
148 assert(!(enclosing.enclosing.flags & SCOPE.SCOPEfree)); | |
149 | |
150 if (this is enclosing.enclosing) /// huh? | |
151 { | |
152 writef("this = %p, enclosing = %p, enclosing.enclosing = %p\n", this, enclosing, enclosing.enclosing); | |
153 } | |
154 assert(this !is enclosing.enclosing); | |
155 } | |
156 this.linkage = enclosing.linkage; | |
157 this.protection = enclosing.protection; | |
158 this.explicitProtection = enclosing.explicitProtection; | |
159 this.stc = enclosing.stc; | |
160 this.inunion = enclosing.inunion; | |
161 this.incontract = enclosing.incontract; | |
162 this.noctor = enclosing.noctor; | |
163 this.noaccesscheck = enclosing.noaccesscheck; | |
164 this.mustsemantic = enclosing.mustsemantic; | |
165 this.intypeof = enclosing.intypeof; | |
166 this.parameterSpecialization = enclosing.parameterSpecialization; | |
167 this.callSuper = enclosing.callSuper; | |
168 this.docbuf = enclosing.docbuf; | |
169 assert(this !is enclosing); /// huh? | |
170 } | |
82 | 171 |
172 Scope clone() | |
173 { | |
174 // similar code is used in Type.clone() | |
175 // TODO: move to Util or something... | |
176 size_t size = __traits(classInstanceSize, typeof(this)); | |
83
ee670dd808a8
malloc -> GC.malloc change, plus compilation issues fixed (always check before commiting stuff!!!)
korDen
parents:
82
diff
changeset
|
177 void* mem = GC.malloc(size); |
ee670dd808a8
malloc -> GC.malloc change, plus compilation issues fixed (always check before commiting stuff!!!)
korDen
parents:
82
diff
changeset
|
178 memcpy(mem, cast(void*)this, size); |
82 | 179 |
180 return cast(typeof(this))mem; | |
181 } | |
0 | 182 |
183 Scope push() | |
184 { | |
185 //printf("Scope.push()\n"); | |
186 Scope s = new Scope(this); | |
187 assert(this !is s); /// huh? | |
188 return s; | |
189 } | |
190 | |
191 Scope push(ScopeDsymbol ss) | |
192 { | |
193 //printf("Scope.push(%s)\n", ss.toChars()); | |
194 Scope s = push(); | |
195 s.scopesym = ss; | |
196 return s; | |
197 } | |
198 | |
199 Scope pop() | |
200 { | |
201 //printf("Scope.pop() %p nofree = %d\n", this, nofree); | |
202 Scope enc = enclosing; | |
203 | |
204 if (enclosing) | |
205 enclosing.callSuper |= callSuper; | |
206 | |
207 if (!nofree) | |
208 { | |
209 enclosing = freelist; | |
210 freelist = this; | |
211 flags |= SCOPE.SCOPEfree; | |
212 } | |
213 | |
214 return enc; | |
215 } | |
216 | |
217 void mergeCallSuper(Loc loc, uint cs) | |
218 { | |
219 // This does a primitive flow analysis to support the restrictions | |
220 // regarding when and how constructors can appear. | |
221 // It merges the results of two paths. | |
222 // The two paths are callSuper and cs; the result is merged into callSuper. | |
223 | |
224 if (cs != callSuper) | |
225 { | |
226 int a; | |
227 int b; | |
228 | |
229 callSuper |= cs & (CSX.CSXany_ctor | CSX.CSXlabel); | |
230 if (cs & CSX.CSXreturn) | |
231 { | |
232 ; | |
233 } | |
234 else if (callSuper & CSX.CSXreturn) | |
235 { | |
236 callSuper = cs | (callSuper & (CSX.CSXany_ctor | CSX.CSXlabel)); | |
237 } | |
238 else | |
239 { | |
240 a = (cs & (CSX.CSXthis_ctor | CSX.CSXsuper_ctor)) != 0; | |
241 b = (callSuper & (CSX.CSXthis_ctor | CSX.CSXsuper_ctor)) != 0; | |
242 | |
243 if (a != b) | |
244 error(loc, "one path skips constructor"); | |
245 callSuper |= cs; | |
246 } | |
247 } | |
248 } | |
249 | |
250 Dsymbol search(Loc loc, Identifier ident, Dsymbol* pscopesym) | |
251 { | |
252 Dsymbol s; | |
253 Scope sc; | |
254 | |
255 //printf("Scope.search(%p, '%s')\n", this, ident.toChars()); | |
256 if (ident is Id.empty) | |
257 { | |
258 // Look for module scope | |
259 for (sc = this; sc; sc = sc.enclosing) | |
260 { | |
261 assert(sc != sc.enclosing); | |
262 if (sc.scopesym) | |
263 { | |
264 s = sc.scopesym.isModule(); | |
265 if (s) | |
266 { | |
267 //printf("\tfound %s.%s\n", s.parent ? s.parent.toChars() : "", s.toChars()); | |
268 if (pscopesym) | |
269 *pscopesym = sc.scopesym; | |
270 return s; | |
271 } | |
272 } | |
273 } | |
274 return null; | |
275 } | |
276 | |
277 for (sc = this; sc; sc = sc.enclosing) | |
278 { | |
279 assert(sc != sc.enclosing); | |
280 if (sc.scopesym) | |
281 { | |
282 //printf("\tlooking in scopesym '%s', kind = '%s'\n", sc.scopesym.toChars(), sc.scopesym.kind()); | |
283 s = sc.scopesym.search(loc, ident, 0); | |
284 if (s) | |
285 { | |
286 if ((global.params.warnings || | |
287 global.params.Dversion > 1) && | |
288 ident == Id.length && | |
289 sc.scopesym.isArrayScopeSymbol() && | |
290 sc.enclosing && | |
291 sc.enclosing.search(loc, ident, null)) | |
292 { | |
293 warning(s.loc, "array 'length' hides other 'length' name in outer scope"); | |
294 } | |
295 | |
296 //printf("\tfound %s.%s, kind = '%s'\n", s.parent ? s.parent.toChars() : "", s.toChars(), s.kind()); | |
297 if (pscopesym) | |
298 *pscopesym = sc.scopesym; | |
299 return s; | |
300 } | |
301 } | |
302 } | |
303 | |
304 return null; | |
305 } | |
306 | |
307 Dsymbol insert(Dsymbol s) | |
308 { | |
309 for (Scope sc = this; sc; sc = sc.enclosing) | |
310 { | |
311 //printf("\tsc = %p\n", sc); | |
312 if (sc.scopesym) | |
313 { | |
314 //printf("\t\tsc.scopesym = %p\n", sc.scopesym); | |
315 if (!sc.scopesym.symtab) | |
316 sc.scopesym.symtab = new DsymbolTable(); | |
317 | |
79 | 318 return sc.scopesym.symtabInsert(s); |
0 | 319 } |
320 } | |
321 | |
322 assert(false); | |
323 } | |
324 | |
325 ClassDeclaration getClassScope() | |
326 { | |
327 assert(false); | |
328 } | |
329 | |
330 /******************************************** | |
331 * Search enclosing scopes for ClassDeclaration. | |
332 */ | |
333 AggregateDeclaration getStructClassScope() | |
334 { | |
335 for (Scope sc = this; sc; sc = sc.enclosing) | |
336 { | |
337 AggregateDeclaration ad; | |
338 | |
339 if (sc.scopesym) | |
340 { | |
341 ad = sc.scopesym.isClassDeclaration(); | |
342 if (ad) | |
343 return ad; | |
344 else | |
345 { | |
346 ad = sc.scopesym.isStructDeclaration(); | |
347 if (ad) | |
348 return ad; | |
349 } | |
350 } | |
351 } | |
352 | |
353 return null; | |
354 } | |
355 | |
356 void setNoFree() | |
357 { | |
358 //int i = 0; | |
359 | |
360 //printf("Scope.setNoFree(this = %p)\n", this); | |
361 for (Scope sc = this; sc; sc = sc.enclosing) | |
362 { | |
363 //printf("\tsc = %p\n", sc); | |
364 sc.nofree = 1; | |
365 | |
366 assert(!(flags & SCOPE.SCOPEfree)); | |
367 //assert(sc != sc.enclosing); | |
368 //assert(!sc.enclosing || sc != sc.enclosing.enclosing); | |
369 //if (++i == 10) | |
370 //assert(0); | |
371 } | |
372 } | |
21
26b9f97f6162
Added in some = new OutBuffer();'s to be on the safe side, been getting some issues with null OutBuffers.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
373 } |