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