0
|
1 module dmd.TypeQualified;
|
|
2
|
114
|
3 import dmd.common;
|
0
|
4 import dmd.Type;
|
|
5 import dmd.Import;
|
73
|
6 import dmd.DsymbolExp;
|
0
|
7 import dmd.TypeExp;
|
|
8 import dmd.DotIdExp;
|
|
9 import dmd.VarDeclaration;
|
|
10 import dmd.EnumMember;
|
|
11 import dmd.TupleDeclaration;
|
|
12 import dmd.Id;
|
|
13 import dmd.VarExp;
|
|
14 import dmd.TemplateInstance;
|
|
15 import dmd.Loc;
|
|
16 import dmd.Array;
|
|
17 import dmd.TY;
|
|
18 import dmd.Identifier;
|
|
19 import dmd.OutBuffer;
|
|
20 import dmd.HdrGenState;
|
|
21 import dmd.Scope;
|
|
22 import dmd.Dsymbol;
|
|
23 import dmd.DYNCAST;
|
|
24 import dmd.Expression;
|
|
25 import dmd.Util;
|
|
26
|
|
27 class TypeQualified : Type
|
|
28 {
|
|
29 Loc loc;
|
|
30 Array idents; // array of Identifier's representing ident.ident.ident etc.
|
|
31
|
|
32 this(TY ty, Loc loc)
|
|
33 {
|
|
34 super(ty);
|
|
35 this.loc = loc;
|
|
36
|
|
37 idents = new Array();
|
|
38 }
|
|
39
|
|
40 version (DumbClone) {
|
|
41 } else {
|
|
42 Type clone()
|
|
43 {
|
|
44 assert(false);
|
|
45 }
|
|
46 }
|
|
47 void syntaxCopyHelper(TypeQualified t)
|
|
48 {
|
|
49 //printf("TypeQualified::syntaxCopyHelper(%s) %s\n", t->toChars(), toChars());
|
|
50 idents.setDim(t.idents.dim);
|
|
51 for (int i = 0; i < idents.dim; i++)
|
|
52 {
|
52
|
53 Object o = cast(Object)t.idents.data[i];
|
|
54 if (TemplateInstance ti = cast(TemplateInstance)o)
|
0
|
55 {
|
52
|
56 o = ti.syntaxCopy(null);
|
0
|
57 }
|
|
58
|
52
|
59 idents.data[i] = cast(void*)o;
|
0
|
60 }
|
|
61 }
|
|
62
|
52
|
63 void addIdent(Object ident)
|
0
|
64 {
|
56
|
65 assert(ident !is null);
|
0
|
66 idents.push(cast(void*)ident);
|
|
67 }
|
|
68
|
|
69 void toCBuffer2Helper(OutBuffer buf, HdrGenState* hgs)
|
|
70 {
|
|
71 int i;
|
|
72
|
|
73 for (i = 0; i < idents.dim; i++)
|
|
74 {
|
|
75 Identifier id = cast(Identifier)idents.data[i];
|
|
76 buf.writeByte('.');
|
|
77
|
|
78 if (id.dyncast() == DYNCAST.DYNCAST_DSYMBOL)
|
|
79 {
|
|
80 TemplateInstance ti = cast(TemplateInstance)id;
|
|
81 ti.toCBuffer(buf, hgs);
|
|
82 } else {
|
|
83 buf.writestring(id.toChars());
|
|
84 }
|
|
85 }
|
|
86 }
|
|
87
|
72
|
88 override ulong size(Loc loc)
|
0
|
89 {
|
|
90 assert(false);
|
|
91 }
|
|
92
|
|
93 /*************************************
|
|
94 * Takes an array of Identifiers and figures out if
|
|
95 * it represents a Type or an Expression.
|
|
96 * Output:
|
|
97 * if expression, *pe is set
|
|
98 * if type, *pt is set
|
|
99 */
|
|
100 void resolveHelper(Loc loc, Scope sc, Dsymbol s, Dsymbol scopesym, Expression* pe, Type* pt, Dsymbol* ps)
|
|
101 {
|
|
102 VarDeclaration v;
|
|
103 EnumMember em;
|
|
104 TupleDeclaration td;
|
|
105 Expression e;
|
|
106
|
|
107 static if (false) {
|
|
108 printf("TypeQualified.resolveHelper(sc = %p, idents = '%s')\n", sc, toChars());
|
|
109 if (scopesym)
|
|
110 printf("\tscopesym = '%s'\n", scopesym.toChars());
|
|
111 }
|
|
112 *pe = null;
|
|
113 *pt = null;
|
|
114 *ps = null;
|
|
115 if (s)
|
|
116 {
|
|
117 //printf("\t1: s = '%s' %p, kind = '%s'\n",s.toChars(), s, s.kind());
|
|
118 s.checkDeprecated(loc, sc); // check for deprecated aliases
|
|
119 s = s.toAlias();
|
|
120 //printf("\t2: s = '%s' %p, kind = '%s'\n",s.toChars(), s, s.kind());
|
|
121 for (int i = 0; i < idents.dim; i++)
|
|
122 {
|
56
|
123 Object o = cast(Object)idents.data[i];
|
|
124
|
|
125 Dsymbol sm = s.searchX(loc, sc, o);
|
|
126 Identifier id = cast(Identifier)o;
|
0
|
127 //printf("\t3: s = '%s' %p, kind = '%s'\n",s.toChars(), s, s.kind());
|
|
128 //printf("\tgetType = '%s'\n", s.getType().toChars());
|
|
129 if (!sm)
|
|
130 {
|
|
131 Type t;
|
|
132
|
|
133 v = s.isVarDeclaration();
|
|
134 if (v && id == Id.length)
|
|
135 {
|
|
136 e = v.getConstInitializer();
|
|
137 if (!e)
|
|
138 e = new VarExp(loc, v);
|
|
139 t = e.type;
|
|
140 if (!t)
|
|
141 goto Lerror;
|
|
142 goto L3;
|
|
143 }
|
73
|
144 else if (v && id == Id.stringof_)
|
|
145 {
|
|
146 e = new DsymbolExp(loc, s, 0);
|
|
147 do
|
|
148 {
|
|
149 id = cast(Identifier)idents.data[i];
|
|
150 e = new DotIdExp(loc, e, id);
|
|
151 } while (++i < idents.dim);
|
|
152 e = e.semantic(sc);
|
|
153 *pe = e;
|
|
154 return;
|
|
155 }
|
|
156
|
0
|
157 t = s.getType();
|
|
158 if (!t && s.isDeclaration())
|
|
159 t = s.isDeclaration().type;
|
|
160 if (t)
|
|
161 {
|
|
162 sm = t.toDsymbol(sc);
|
|
163 if (sm)
|
|
164 { sm = sm.search(loc, id, 0);
|
|
165 if (sm)
|
|
166 goto L2;
|
|
167 }
|
|
168 //e = t.getProperty(loc, id);
|
|
169 e = new TypeExp(loc, t);
|
|
170 e = t.dotExp(sc, e, id);
|
|
171 i++;
|
|
172 L3:
|
|
173 for (; i < idents.dim; i++)
|
|
174 {
|
|
175 id = cast(Identifier)idents.data[i];
|
|
176 //printf("e: '%s', id: '%s', type = %p\n", e.toChars(), id.toChars(), e.type);
|
|
177 if (id == Id.offsetof)
|
|
178 { e = new DotIdExp(e.loc, e, id);
|
|
179 e = e.semantic(sc);
|
|
180 }
|
|
181 else
|
|
182 e = e.type.dotExp(sc, e, id);
|
|
183 }
|
|
184 *pe = e;
|
|
185 }
|
|
186 else
|
|
187 Lerror:
|
|
188 error(loc, "identifier '%s' of '%s' is not defined", id.toChars(), toChars());
|
|
189 return;
|
|
190 }
|
|
191 L2:
|
|
192 s = sm.toAlias();
|
|
193 }
|
|
194
|
|
195 v = s.isVarDeclaration();
|
|
196 if (v)
|
|
197 {
|
|
198 ///static if (false) {
|
|
199 /// // It's not a type, it's an expression
|
|
200 /// Expression *e = v.getConstInitializer();
|
|
201 /// if (e)
|
|
202 /// {
|
|
203 /// *pe = e.copy(); // make copy so we can change loc
|
|
204 /// (*pe).loc = loc;
|
|
205 /// }
|
|
206 /// else
|
|
207 ///}
|
|
208 {
|
|
209 ///static if (false) {
|
|
210 /// WithScopeSymbol withsym;
|
|
211 /// if (scopesym && (withsym = scopesym.isWithScopeSymbol()) !is null)
|
|
212 /// {
|
|
213 /// // Same as wthis.ident
|
|
214 /// e = new VarExp(loc, withsym.withstate.wthis);
|
|
215 /// e = new DotIdExp(loc, e, ident);
|
|
216 /// //assert(0); // BUG: should handle this
|
|
217 /// }
|
|
218 /// else
|
|
219 ///}
|
|
220 *pe = new VarExp(loc, v);
|
|
221 }
|
|
222 return;
|
|
223 }
|
|
224 em = s.isEnumMember();
|
|
225 if (em)
|
|
226 {
|
|
227 // It's not a type, it's an expression
|
|
228 *pe = em.value.copy();
|
|
229 return;
|
|
230 }
|
|
231
|
|
232 L1:
|
|
233 Type t = s.getType();
|
|
234 if (!t)
|
|
235 {
|
|
236 // If the symbol is an import, try looking inside the import
|
|
237 Import si;
|
|
238
|
|
239 si = s.isImport();
|
|
240 if (si)
|
|
241 {
|
|
242 s = si.search(loc, s.ident, 0);
|
|
243 if (s && s != si)
|
|
244 goto L1;
|
|
245 s = si;
|
|
246 }
|
|
247 *ps = s;
|
|
248 return;
|
|
249 }
|
|
250 if (t.ty == TY.Tinstance && t != this && !t.deco)
|
|
251 {
|
|
252 error(loc, "forward reference to '%s'", t.toChars());
|
|
253 return;
|
|
254 }
|
|
255
|
|
256 if (t != this)
|
|
257 {
|
|
258 if (t.reliesOnTident())
|
|
259 {
|
73
|
260 if (s.scope_)
|
|
261 t = t.semantic(loc, s.scope_);
|
|
262 else
|
0
|
263 {
|
73
|
264 /* Attempt to find correct scope in which to evaluate t.
|
|
265 * Not sure if this is right or not, or if we should just
|
|
266 * give forward reference error if s.scope is not set.
|
|
267 */
|
|
268 for (Scope scx = sc; 1; scx = scx.enclosing)
|
|
269 {
|
|
270 if (!scx)
|
|
271 {
|
|
272 error(loc, "forward reference to '%s'", t.toChars());
|
|
273 return;
|
|
274 }
|
|
275 if (scx.scopesym == scopesym)
|
|
276 {
|
|
277 t = t.semantic(loc, scx);
|
|
278 break;
|
|
279 }
|
0
|
280 }
|
|
281 }
|
73
|
282
|
0
|
283 }
|
|
284 }
|
|
285 if (t.ty == TY.Ttuple)
|
|
286 *pt = t;
|
|
287 else
|
|
288 *pt = t.merge();
|
|
289 }
|
|
290 if (!s)
|
|
291 {
|
|
292 error(loc, "identifier '%s' is not defined", toChars());
|
|
293 }
|
|
294 }
|
72
|
295 }
|