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