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