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