Mercurial > projects > ddmd
annotate dmd/Scope.d @ 82:e95073e26356
fixed a hack I used as a lack of copy ctor in D
author | korDen |
---|---|
date | Mon, 30 Aug 2010 18:38:35 +0400 |
parents | 43073c7c7769 |
children | ee670dd808a8 |
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 } | |
82 | 168 |
169 Scope clone() | |
170 { | |
171 // similar code is used in Type.clone() | |
172 // TODO: move to Util or something... | |
173 size_t size = __traits(classInstanceSize, typeof(this)); | |
174 void* mem = malloc(size); | |
175 memcpy(mem, this, size); | |
176 | |
177 return cast(typeof(this))mem; | |
178 } | |
0 | 179 |
180 Scope push() | |
181 { | |
182 //printf("Scope.push()\n"); | |
183 Scope s = new Scope(this); | |
184 assert(this !is s); /// huh? | |
185 return s; | |
186 } | |
187 | |
188 Scope push(ScopeDsymbol ss) | |
189 { | |
190 //printf("Scope.push(%s)\n", ss.toChars()); | |
191 Scope s = push(); | |
192 s.scopesym = ss; | |
193 return s; | |
194 } | |
195 | |
196 Scope pop() | |
197 { | |
198 //printf("Scope.pop() %p nofree = %d\n", this, nofree); | |
199 Scope enc = enclosing; | |
200 | |
201 if (enclosing) | |
202 enclosing.callSuper |= callSuper; | |
203 | |
204 if (!nofree) | |
205 { | |
206 enclosing = freelist; | |
207 freelist = this; | |
208 flags |= SCOPE.SCOPEfree; | |
209 } | |
210 | |
211 return enc; | |
212 } | |
213 | |
214 void mergeCallSuper(Loc loc, uint cs) | |
215 { | |
216 // This does a primitive flow analysis to support the restrictions | |
217 // regarding when and how constructors can appear. | |
218 // It merges the results of two paths. | |
219 // The two paths are callSuper and cs; the result is merged into callSuper. | |
220 | |
221 if (cs != callSuper) | |
222 { | |
223 int a; | |
224 int b; | |
225 | |
226 callSuper |= cs & (CSX.CSXany_ctor | CSX.CSXlabel); | |
227 if (cs & CSX.CSXreturn) | |
228 { | |
229 ; | |
230 } | |
231 else if (callSuper & CSX.CSXreturn) | |
232 { | |
233 callSuper = cs | (callSuper & (CSX.CSXany_ctor | CSX.CSXlabel)); | |
234 } | |
235 else | |
236 { | |
237 a = (cs & (CSX.CSXthis_ctor | CSX.CSXsuper_ctor)) != 0; | |
238 b = (callSuper & (CSX.CSXthis_ctor | CSX.CSXsuper_ctor)) != 0; | |
239 | |
240 if (a != b) | |
241 error(loc, "one path skips constructor"); | |
242 callSuper |= cs; | |
243 } | |
244 } | |
245 } | |
246 | |
247 Dsymbol search(Loc loc, Identifier ident, Dsymbol* pscopesym) | |
248 { | |
249 Dsymbol s; | |
250 Scope sc; | |
251 | |
252 //printf("Scope.search(%p, '%s')\n", this, ident.toChars()); | |
253 if (ident is Id.empty) | |
254 { | |
255 // Look for module scope | |
256 for (sc = this; sc; sc = sc.enclosing) | |
257 { | |
258 assert(sc != sc.enclosing); | |
259 if (sc.scopesym) | |
260 { | |
261 s = sc.scopesym.isModule(); | |
262 if (s) | |
263 { | |
264 //printf("\tfound %s.%s\n", s.parent ? s.parent.toChars() : "", s.toChars()); | |
265 if (pscopesym) | |
266 *pscopesym = sc.scopesym; | |
267 return s; | |
268 } | |
269 } | |
270 } | |
271 return null; | |
272 } | |
273 | |
274 for (sc = this; sc; sc = sc.enclosing) | |
275 { | |
276 assert(sc != sc.enclosing); | |
277 if (sc.scopesym) | |
278 { | |
279 //printf("\tlooking in scopesym '%s', kind = '%s'\n", sc.scopesym.toChars(), sc.scopesym.kind()); | |
280 s = sc.scopesym.search(loc, ident, 0); | |
281 if (s) | |
282 { | |
283 if ((global.params.warnings || | |
284 global.params.Dversion > 1) && | |
285 ident == Id.length && | |
286 sc.scopesym.isArrayScopeSymbol() && | |
287 sc.enclosing && | |
288 sc.enclosing.search(loc, ident, null)) | |
289 { | |
290 warning(s.loc, "array 'length' hides other 'length' name in outer scope"); | |
291 } | |
292 | |
293 //printf("\tfound %s.%s, kind = '%s'\n", s.parent ? s.parent.toChars() : "", s.toChars(), s.kind()); | |
294 if (pscopesym) | |
295 *pscopesym = sc.scopesym; | |
296 return s; | |
297 } | |
298 } | |
299 } | |
300 | |
301 return null; | |
302 } | |
303 | |
304 Dsymbol insert(Dsymbol s) | |
305 { | |
306 for (Scope sc = this; sc; sc = sc.enclosing) | |
307 { | |
308 //printf("\tsc = %p\n", sc); | |
309 if (sc.scopesym) | |
310 { | |
311 //printf("\t\tsc.scopesym = %p\n", sc.scopesym); | |
312 if (!sc.scopesym.symtab) | |
313 sc.scopesym.symtab = new DsymbolTable(); | |
314 | |
79 | 315 return sc.scopesym.symtabInsert(s); |
0 | 316 } |
317 } | |
318 | |
319 assert(false); | |
320 } | |
321 | |
322 ClassDeclaration getClassScope() | |
323 { | |
324 assert(false); | |
325 } | |
326 | |
327 /******************************************** | |
328 * Search enclosing scopes for ClassDeclaration. | |
329 */ | |
330 AggregateDeclaration getStructClassScope() | |
331 { | |
332 for (Scope sc = this; sc; sc = sc.enclosing) | |
333 { | |
334 AggregateDeclaration ad; | |
335 | |
336 if (sc.scopesym) | |
337 { | |
338 ad = sc.scopesym.isClassDeclaration(); | |
339 if (ad) | |
340 return ad; | |
341 else | |
342 { | |
343 ad = sc.scopesym.isStructDeclaration(); | |
344 if (ad) | |
345 return ad; | |
346 } | |
347 } | |
348 } | |
349 | |
350 return null; | |
351 } | |
352 | |
353 void setNoFree() | |
354 { | |
355 //int i = 0; | |
356 | |
357 //printf("Scope.setNoFree(this = %p)\n", this); | |
358 for (Scope sc = this; sc; sc = sc.enclosing) | |
359 { | |
360 //printf("\tsc = %p\n", sc); | |
361 sc.nofree = 1; | |
362 | |
363 assert(!(flags & SCOPE.SCOPEfree)); | |
364 //assert(sc != sc.enclosing); | |
365 //assert(!sc.enclosing || sc != sc.enclosing.enclosing); | |
366 //if (++i == 10) | |
367 //assert(0); | |
368 } | |
369 } | |
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
|
370 } |