Mercurial > projects > ddmd
annotate dmd/ScopeDsymbol.d @ 178:e3afd1303184
Many small bugs fixed
Made all classes derive from TObject to detect memory leaks (functionality is disabled for now)
Began work on overriding backend memory allocations (to avoid memory leaks)
author | korDen |
---|---|
date | Sun, 17 Oct 2010 07:42:00 +0400 |
parents | 60bb0fe4563e |
children | b0d41ff5e0df |
rev | line source |
---|---|
0 | 1 module dmd.ScopeDsymbol; |
2 | |
114 | 3 import dmd.common; |
0 | 4 import dmd.Dsymbol; |
5 import dmd.Declaration; | |
6 import dmd.Array; | |
7 import dmd.OverloadSet; | |
8 import dmd.Import; | |
9 import dmd.DsymbolTable; | |
10 import dmd.Identifier; | |
11 import dmd.Loc; | |
12 import dmd.PROT; | |
13 import dmd.FuncDeclaration; | |
14 import dmd.Scope; | |
15 import dmd.Util; | |
16 import dmd.Id; | |
17 import dmd.expression.Util; | |
18 | |
79 | 19 import std.stdio : writef; |
20 //core.stdc.stdlib; | |
4 | 21 import core.memory; |
0 | 22 |
23 class ScopeDsymbol : Dsymbol | |
24 { | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
25 Dsymbols members; // all Dsymbol's in this scope |
0 | 26 DsymbolTable symtab; // members[] sorted into table |
27 | |
28 Array imports; // imported ScopeDsymbol's | |
29 PROT* prots; // array of PROT, one for each import | |
30 | |
31 this() | |
32 { | |
178 | 33 register(); |
0 | 34 // do nothing |
35 } | |
36 | |
37 this(Identifier id) | |
38 { | |
178 | 39 register(); |
0 | 40 super(id); |
41 } | |
42 | |
72 | 43 override Dsymbol syntaxCopy(Dsymbol s) |
0 | 44 { |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
45 //printf("ScopeDsymbol.syntaxCopy('%s')\n", toChars()); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
46 |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
47 ScopeDsymbol sd; |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
48 if (s) |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
49 sd = cast(ScopeDsymbol)s; |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
50 else |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
51 sd = new ScopeDsymbol(ident); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
52 sd.members = arraySyntaxCopy(members); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
53 return sd; |
0 | 54 } |
55 | |
72 | 56 override Dsymbol search(Loc loc, Identifier ident, int flags) |
0 | 57 { |
58 //printf("%s.ScopeDsymbol.search(ident='%s', flags=x%x)\n", toChars(), ident.toChars(), flags); | |
59 //if (strcmp(ident.toChars(),"c") == 0) *(char*)0=0; | |
60 | |
61 // Look in symbols declared in this module | |
62 Dsymbol s = symtab ? symtab.lookup(ident) : null; | |
79 | 63 // writef("\ts = %p, imports = %p, %d\n", s, imports, imports ? imports->dim : 0); |
0 | 64 if (s) |
65 { | |
66 //printf("\ts = '%s.%s'\n",toChars(),s.toChars()); | |
67 } | |
68 else if (imports) | |
69 { | |
70 OverloadSet a = null; | |
71 | |
72 // Look in imported modules | |
73 for (int i = 0; i < imports.dim; i++) | |
74 { | |
75 ScopeDsymbol ss = cast(ScopeDsymbol)imports.data[i]; | |
76 Dsymbol s2; | |
77 | |
78 // If private import, don't search it | |
79 if (flags & 1 && prots[i] == PROT.PROTprivate) | |
80 continue; | |
81 | |
82 //printf("\tscanning import '%s', prots = %d, isModule = %p, isImport = %p\n", ss.toChars(), prots[i], ss.isModule(), ss.isImport()); | |
83 /* Don't find private members if ss is a module | |
84 */ | |
85 s2 = ss.search(loc, ident, ss.isModule() ? 1 : 0); | |
86 if (!s) | |
87 s = s2; | |
88 else if (s2 && s != s2) | |
89 { | |
90 if (s.toAlias() == s2.toAlias()) | |
91 { | |
92 /* After following aliases, we found the same symbol, | |
93 * so it's not an ambiguity. | |
94 * But if one alias is deprecated, prefer the other. | |
95 */ | |
96 if (s.isDeprecated()) | |
97 s = s2; | |
98 } | |
99 else | |
100 { | |
101 /* Two imports of the same module should be regarded as | |
102 * the same. | |
103 */ | |
104 Import i1 = s.isImport(); | |
105 Import i2 = s2.isImport(); | |
106 if (!(i1 && i2 && | |
107 (i1.mod == i2.mod || | |
108 (!i1.parent.isImport() && !i2.parent.isImport() && | |
109 i1.ident.equals(i2.ident)) | |
110 ) | |
111 ) | |
112 ) | |
113 { | |
114 /* If both s2 and s are overloadable (though we only | |
115 * need to check s once) | |
116 */ | |
117 if (s2.isOverloadable() && (a || s.isOverloadable())) | |
118 { if (!a) | |
119 a = new OverloadSet(); | |
120 /* Don't add to a[] if s2 is alias of previous sym | |
121 */ | |
77
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
74
diff
changeset
|
122 foreach (size_t j, Dsymbol s3; a.a) |
0 | 123 { |
124 if (s2.toAlias() == s3.toAlias()) | |
125 { | |
126 if (s3.isDeprecated()) | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
127 a.a[j] = s2; |
0 | 128 goto Lcontinue; |
129 } | |
130 } | |
131 a.push(s2); | |
132 Lcontinue: | |
133 continue; | |
134 } | |
135 if (flags & 4) // if return null on ambiguity | |
136 return null; | |
137 if (!(flags & 2)) | |
138 ss.multiplyDefined(loc, s, s2); | |
139 break; | |
140 } | |
141 } | |
142 } | |
143 } | |
144 | |
145 /* Build special symbol if we had multiple finds | |
146 */ | |
147 if (a) | |
148 { | |
149 assert(s); | |
150 a.push(s); | |
151 s = a; | |
152 } | |
153 | |
154 if (s) | |
155 { | |
156 Declaration d = s.isDeclaration(); | |
157 if (d && d.protection == PROT.PROTprivate && !d.parent.isTemplateMixin() && !(flags & 2)) | |
158 error("%s is private", d.toPrettyChars()); | |
159 } | |
160 } | |
161 return s; | |
162 } | |
163 | |
164 void importScope(ScopeDsymbol s, PROT protection) | |
165 { | |
79 | 166 //writef("%s.ScopeDsymbol.importScope(%s, %d)\n", toChars(), s.toChars(), protection); |
0 | 167 |
168 // No circular or redundant import's | |
79 | 169 if (s !is this) |
0 | 170 { |
171 if (!imports) | |
172 imports = new Array(); | |
173 else | |
174 { | |
175 for (int i = 0; i < imports.dim; i++) | |
176 { | |
177 ScopeDsymbol ss = cast(ScopeDsymbol)imports.data[i]; | |
178 if (ss is s) // if already imported | |
179 { | |
180 if (protection > prots[i]) | |
181 prots[i] = protection; // upgrade access | |
182 return; | |
183 } | |
184 } | |
185 } | |
186 imports.push(cast(void*)s); | |
2 | 187 prots = cast(PROT*)GC.realloc(prots, imports.dim * prots[0].sizeof); |
0 | 188 prots[imports.dim - 1] = protection; |
189 } | |
190 } | |
191 | |
72 | 192 override int isforwardRef() |
0 | 193 { |
79 | 194 return (members is null); |
0 | 195 } |
196 | |
72 | 197 override void defineRef(Dsymbol s) |
0 | 198 { |
79 | 199 ScopeDsymbol ss = s.isScopeDsymbol(); |
200 members = ss.members; | |
201 ss.members = null; | |
0 | 202 } |
203 | |
204 static void multiplyDefined(Loc loc, Dsymbol s1, Dsymbol s2) | |
205 { | |
206 static if (false) { | |
207 printf("ScopeDsymbol::multiplyDefined()\n"); | |
208 printf("s1 = %p, '%s' kind = '%s', parent = %s\n", s1, s1.toChars(), s1.kind(), s1.parent ? s1.parent.toChars() : ""); | |
209 printf("s2 = %p, '%s' kind = '%s', parent = %s\n", s2, s2.toChars(), s2.kind(), s2.parent ? s2.parent.toChars() : ""); | |
210 } | |
211 if (loc.filename) | |
212 { | |
213 .error(loc, "%s at %s conflicts with %s at %s", | |
214 s1.toPrettyChars(), | |
215 s1.locToChars(), | |
216 s2.toPrettyChars(), | |
217 s2.locToChars()); | |
218 } | |
219 else | |
220 { | |
221 s1.error(loc, "conflicts with %s %s at %s", s2.kind(), s2.toPrettyChars(), s2.locToChars()); | |
222 } | |
223 } | |
224 | |
225 Dsymbol nameCollision(Dsymbol s) | |
226 { | |
227 assert(false); | |
228 } | |
229 | |
72 | 230 override string kind() |
0 | 231 { |
232 assert(false); | |
233 } | |
234 | |
79 | 235 version(DMDV2) |
236 { | |
0 | 237 /******************************************* |
238 * Look for member of the form: | |
239 * const(MemberInfo)[] getMembers(string); | |
240 * Returns NULL if not found | |
241 */ | |
242 FuncDeclaration findGetMembers() | |
243 { | |
244 Dsymbol s = search_function(this, Id.getmembers); | |
245 FuncDeclaration fdx = s ? s.isFuncDeclaration() : null; | |
246 | |
247 static if (false) { // Finish | |
248 static __gshared TypeFunction tfgetmembers; | |
249 | |
250 if (!tfgetmembers) | |
251 { | |
252 Scope sc; | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
253 auto arguments = new Arguments(); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
254 auto arg = new Argument(STCin, Type.tchar.constOf().arrayOf(), null, null); |
0 | 255 arguments.push(arg); |
256 | |
257 Type tret = null; | |
258 tfgetmembers = new TypeFunction(arguments, tret, 0, LINK.LINKd); | |
259 tfgetmembers = cast(TypeFunction)tfgetmembers.semantic(0, &sc); | |
260 } | |
261 if (fdx) | |
262 fdx = fdx.overloadExactMatch(tfgetmembers); | |
263 } | |
264 if (fdx && fdx.isVirtual()) { | |
265 fdx = null; | |
266 } | |
267 | |
268 return fdx; | |
269 } | |
79 | 270 } |
271 | |
272 Dsymbol symtabInsert(Dsymbol s) | |
273 { | |
274 return symtab.insert(s); | |
275 } | |
0 | 276 |
277 void emitMemberComments(Scope sc) | |
278 { | |
279 assert(false); | |
280 } | |
281 | |
79 | 282 version(DMDV2) |
283 { | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
284 static size_t dim(Dsymbols members) |
0 | 285 { |
286 assert(false); | |
287 } | |
79 | 288 |
289 | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
290 static Dsymbol getNth(Dsymbols members, size_t nth, size_t* pn = null) |
0 | 291 { |
292 assert(false); | |
293 } | |
79 | 294 } |
72 | 295 override ScopeDsymbol isScopeDsymbol() { return this; } |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
296 } |