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