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