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