Mercurial > projects > ddmd
annotate dmd/Scope.d @ 135:af1bebfd96a4 dmd2037
dmd 2.038
author | Eldar Insafutdinov <e.insafutdinov@gmail.com> |
---|---|
date | Mon, 13 Sep 2010 22:19:42 +0100 |
parents | 206db751bd4c |
children | 78bf0fe43974 96c0fff6897d |
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 | |
131
206db751bd4c
dmdfe 2.037 compiles now
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
81 StorageClass stc; // storage class |
0 | 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; | |
135 | 140 this.tinst = enclosing.tinst; |
0 | 141 this.tinst = enclosing.tinst; |
142 this.sbreak = enclosing.sbreak; | |
143 this.scontinue = enclosing.scontinue; | |
144 this.fes = enclosing.fes; | |
145 this.structalign = enclosing.structalign; | |
146 this.enclosing = enclosing; | |
147 debug { | |
148 if (enclosing.enclosing) | |
149 assert(!(enclosing.enclosing.flags & SCOPE.SCOPEfree)); | |
150 | |
151 if (this is enclosing.enclosing) /// huh? | |
152 { | |
153 writef("this = %p, enclosing = %p, enclosing.enclosing = %p\n", this, enclosing, enclosing.enclosing); | |
154 } | |
155 assert(this !is enclosing.enclosing); | |
156 } | |
157 this.linkage = enclosing.linkage; | |
158 this.protection = enclosing.protection; | |
159 this.explicitProtection = enclosing.explicitProtection; | |
160 this.stc = enclosing.stc; | |
161 this.inunion = enclosing.inunion; | |
162 this.incontract = enclosing.incontract; | |
163 this.noctor = enclosing.noctor; | |
164 this.noaccesscheck = enclosing.noaccesscheck; | |
165 this.mustsemantic = enclosing.mustsemantic; | |
166 this.intypeof = enclosing.intypeof; | |
167 this.parameterSpecialization = enclosing.parameterSpecialization; | |
168 this.callSuper = enclosing.callSuper; | |
169 this.docbuf = enclosing.docbuf; | |
170 assert(this !is enclosing); /// huh? | |
171 } | |
82 | 172 |
173 Scope clone() | |
174 { | |
129 | 175 return cloneThis(this); |
82 | 176 } |
0 | 177 |
178 Scope push() | |
179 { | |
180 //printf("Scope.push()\n"); | |
181 Scope s = new Scope(this); | |
182 assert(this !is s); /// huh? | |
183 return s; | |
184 } | |
185 | |
186 Scope push(ScopeDsymbol ss) | |
187 { | |
188 //printf("Scope.push(%s)\n", ss.toChars()); | |
189 Scope s = push(); | |
190 s.scopesym = ss; | |
191 return s; | |
192 } | |
193 | |
194 Scope pop() | |
195 { | |
196 //printf("Scope.pop() %p nofree = %d\n", this, nofree); | |
197 Scope enc = enclosing; | |
198 | |
199 if (enclosing) | |
200 enclosing.callSuper |= callSuper; | |
201 | |
202 if (!nofree) | |
203 { | |
204 enclosing = freelist; | |
205 freelist = this; | |
206 flags |= SCOPE.SCOPEfree; | |
207 } | |
208 | |
209 return enc; | |
210 } | |
211 | |
212 void mergeCallSuper(Loc loc, uint cs) | |
213 { | |
214 // This does a primitive flow analysis to support the restrictions | |
215 // regarding when and how constructors can appear. | |
216 // It merges the results of two paths. | |
217 // The two paths are callSuper and cs; the result is merged into callSuper. | |
218 | |
219 if (cs != callSuper) | |
220 { | |
221 int a; | |
222 int b; | |
223 | |
224 callSuper |= cs & (CSX.CSXany_ctor | CSX.CSXlabel); | |
225 if (cs & CSX.CSXreturn) | |
226 { | |
227 ; | |
228 } | |
229 else if (callSuper & CSX.CSXreturn) | |
230 { | |
231 callSuper = cs | (callSuper & (CSX.CSXany_ctor | CSX.CSXlabel)); | |
232 } | |
233 else | |
234 { | |
235 a = (cs & (CSX.CSXthis_ctor | CSX.CSXsuper_ctor)) != 0; | |
236 b = (callSuper & (CSX.CSXthis_ctor | CSX.CSXsuper_ctor)) != 0; | |
237 | |
238 if (a != b) | |
239 error(loc, "one path skips constructor"); | |
240 callSuper |= cs; | |
241 } | |
242 } | |
243 } | |
244 | |
245 Dsymbol search(Loc loc, Identifier ident, Dsymbol* pscopesym) | |
246 { | |
247 Dsymbol s; | |
248 Scope sc; | |
249 | |
250 //printf("Scope.search(%p, '%s')\n", this, ident.toChars()); | |
251 if (ident is Id.empty) | |
252 { | |
253 // Look for module scope | |
254 for (sc = this; sc; sc = sc.enclosing) | |
255 { | |
256 assert(sc != sc.enclosing); | |
257 if (sc.scopesym) | |
258 { | |
259 s = sc.scopesym.isModule(); | |
260 if (s) | |
261 { | |
262 //printf("\tfound %s.%s\n", s.parent ? s.parent.toChars() : "", s.toChars()); | |
263 if (pscopesym) | |
264 *pscopesym = sc.scopesym; | |
265 return s; | |
266 } | |
267 } | |
268 } | |
269 return null; | |
270 } | |
271 | |
272 for (sc = this; sc; sc = sc.enclosing) | |
273 { | |
274 assert(sc != sc.enclosing); | |
275 if (sc.scopesym) | |
276 { | |
277 //printf("\tlooking in scopesym '%s', kind = '%s'\n", sc.scopesym.toChars(), sc.scopesym.kind()); | |
278 s = sc.scopesym.search(loc, ident, 0); | |
279 if (s) | |
280 { | |
281 if ((global.params.warnings || | |
282 global.params.Dversion > 1) && | |
283 ident == Id.length && | |
284 sc.scopesym.isArrayScopeSymbol() && | |
285 sc.enclosing && | |
286 sc.enclosing.search(loc, ident, null)) | |
287 { | |
288 warning(s.loc, "array 'length' hides other 'length' name in outer scope"); | |
289 } | |
290 | |
291 //printf("\tfound %s.%s, kind = '%s'\n", s.parent ? s.parent.toChars() : "", s.toChars(), s.kind()); | |
292 if (pscopesym) | |
293 *pscopesym = sc.scopesym; | |
294 return s; | |
295 } | |
296 } | |
297 } | |
298 | |
299 return null; | |
300 } | |
301 | |
302 Dsymbol insert(Dsymbol s) | |
303 { | |
304 for (Scope sc = this; sc; sc = sc.enclosing) | |
305 { | |
306 //printf("\tsc = %p\n", sc); | |
307 if (sc.scopesym) | |
308 { | |
309 //printf("\t\tsc.scopesym = %p\n", sc.scopesym); | |
310 if (!sc.scopesym.symtab) | |
311 sc.scopesym.symtab = new DsymbolTable(); | |
312 | |
79 | 313 return sc.scopesym.symtabInsert(s); |
0 | 314 } |
315 } | |
316 | |
317 assert(false); | |
318 } | |
319 | |
320 ClassDeclaration getClassScope() | |
321 { | |
322 assert(false); | |
323 } | |
324 | |
325 /******************************************** | |
326 * Search enclosing scopes for ClassDeclaration. | |
327 */ | |
328 AggregateDeclaration getStructClassScope() | |
329 { | |
330 for (Scope sc = this; sc; sc = sc.enclosing) | |
331 { | |
332 AggregateDeclaration ad; | |
333 | |
334 if (sc.scopesym) | |
335 { | |
336 ad = sc.scopesym.isClassDeclaration(); | |
337 if (ad) | |
338 return ad; | |
339 else | |
340 { | |
341 ad = sc.scopesym.isStructDeclaration(); | |
342 if (ad) | |
343 return ad; | |
344 } | |
345 } | |
346 } | |
347 | |
348 return null; | |
349 } | |
350 | |
351 void setNoFree() | |
352 { | |
353 //int i = 0; | |
354 | |
355 //printf("Scope.setNoFree(this = %p)\n", this); | |
356 for (Scope sc = this; sc; sc = sc.enclosing) | |
357 { | |
358 //printf("\tsc = %p\n", sc); | |
359 sc.nofree = 1; | |
360 | |
361 assert(!(flags & SCOPE.SCOPEfree)); | |
362 //assert(sc != sc.enclosing); | |
363 //assert(!sc.enclosing || sc != sc.enclosing.enclosing); | |
364 //if (++i == 10) | |
365 //assert(0); | |
366 } | |
367 } | |
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
|
368 } |