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