0
|
1 module dmd.IsExp;
|
|
2
|
|
3 import dmd.Expression;
|
|
4 import dmd.Identifier;
|
|
5 import dmd.ArrayTypes;
|
|
6 import dmd.Type;
|
|
7 import dmd.TOK;
|
|
8 import dmd.OutBuffer;
|
|
9 import dmd.Loc;
|
|
10 import dmd.Scope;
|
|
11 import dmd.HdrGenState;
|
|
12 import dmd.TY;
|
|
13 import dmd.TypeEnum;
|
|
14 import dmd.STC;
|
|
15 import dmd.TypeClass;
|
|
16 import dmd.TemplateParameter;
|
|
17 import dmd.BaseClass;
|
|
18 import dmd.ClassDeclaration;
|
|
19 import dmd.TypeStruct;
|
|
20 import dmd.TypeTypedef;
|
|
21 import dmd.IntegerExp;
|
|
22 import dmd.AliasDeclaration;
|
|
23 import dmd.Dsymbol;
|
|
24 import dmd.TypeTuple;
|
|
25 import dmd.TypeDelegate;
|
|
26 import dmd.Declaration;
|
|
27 import dmd.TypeFunction;
|
|
28 import dmd.MATCH;
|
|
29 import dmd.TypePointer;
|
|
30 import dmd.Argument;
|
|
31
|
|
32 class IsExp : Expression
|
|
33 {
|
|
34 /* is(targ id tok tspec)
|
|
35 * is(targ id == tok2)
|
|
36 */
|
|
37 Type targ;
|
|
38 Identifier id; // can be null
|
|
39 TOK tok; // ':' or '=='
|
|
40 Type tspec; // can be null
|
|
41 TOK tok2; // 'struct', 'union', 'typedef', etc.
|
|
42 TemplateParameters parameters;
|
|
43
|
|
44 this(Loc loc, Type targ, Identifier id, TOK tok, Type tspec, TOK tok2, TemplateParameters parameters)
|
|
45 {
|
|
46 super(loc, TOK.TOKis, IsExp.sizeof);
|
|
47
|
|
48 this.targ = targ;
|
|
49 this.id = id;
|
|
50 this.tok = tok;
|
|
51 this.tspec = tspec;
|
|
52 this.tok2 = tok2;
|
|
53 this.parameters = parameters;
|
|
54 }
|
|
55
|
|
56 Expression syntaxCopy()
|
|
57 {
|
42
|
58 // This section is identical to that in TemplateDeclaration.syntaxCopy()
|
|
59 TemplateParameters p = null;
|
|
60 if (parameters)
|
|
61 {
|
|
62 p = new TemplateParameters();
|
|
63 p.setDim(parameters.dim);
|
|
64 for (int i = 0; i < p.dim; i++)
|
|
65 {
|
|
66 TemplateParameter tp = cast(TemplateParameter)parameters.data[i];
|
|
67 p.data[i] = cast(void*)tp.syntaxCopy();
|
|
68 }
|
|
69 }
|
|
70
|
|
71 return new IsExp(loc,
|
|
72 targ.syntaxCopy(),
|
|
73 id,
|
|
74 tok,
|
|
75 tspec ? tspec.syntaxCopy() : null,
|
|
76 tok2,
|
|
77 p);
|
0
|
78 }
|
|
79
|
|
80 Expression semantic(Scope sc)
|
|
81 {
|
|
82 Type tded;
|
|
83
|
|
84 /* is(targ id tok tspec)
|
|
85 * is(targ id == tok2)
|
|
86 */
|
|
87
|
|
88 //printf("IsExp.semantic(%s)\n", toChars());
|
|
89 if (id && !(sc.flags & SCOPE.SCOPEstaticif))
|
|
90 error("can only declare type aliases within static if conditionals");
|
|
91
|
|
92 Type t = targ.trySemantic(loc, sc);
|
|
93 if (!t)
|
|
94 goto Lno; // errors, so condition is false
|
|
95 targ = t;
|
|
96 if (tok2 != TOK.TOKreserved)
|
|
97 {
|
|
98 switch (tok2)
|
|
99 {
|
|
100 case TOKtypedef:
|
|
101 if (targ.ty != Ttypedef)
|
|
102 goto Lno;
|
|
103 tded = (cast(TypeTypedef)targ).sym.basetype;
|
|
104 break;
|
|
105
|
|
106 case TOKstruct:
|
|
107 if (targ.ty != Tstruct)
|
|
108 goto Lno;
|
|
109 if ((cast(TypeStruct)targ).sym.isUnionDeclaration())
|
|
110 goto Lno;
|
|
111 tded = targ;
|
|
112 break;
|
|
113
|
|
114 case TOKunion:
|
|
115 if (targ.ty != Tstruct)
|
|
116 goto Lno;
|
|
117 if (!(cast(TypeStruct)targ).sym.isUnionDeclaration())
|
|
118 goto Lno;
|
|
119 tded = targ;
|
|
120 break;
|
|
121
|
|
122 case TOKclass:
|
|
123 if (targ.ty != Tclass)
|
|
124 goto Lno;
|
|
125 if ((cast(TypeClass)targ).sym.isInterfaceDeclaration())
|
|
126 goto Lno;
|
|
127 tded = targ;
|
|
128 break;
|
|
129
|
|
130 case TOKinterface:
|
|
131 if (targ.ty != Tclass)
|
|
132 goto Lno;
|
|
133 if (!(cast(TypeClass)targ).sym.isInterfaceDeclaration())
|
|
134 goto Lno;
|
|
135 tded = targ;
|
|
136 break;
|
|
137 version (DMDV2) {
|
|
138 case TOKconst:
|
|
139 if (!targ.isConst())
|
|
140 goto Lno;
|
|
141 tded = targ;
|
|
142 break;
|
|
143
|
|
144 case TOKinvariant:
|
|
145 case TOKimmutable:
|
|
146 if (!targ.isInvariant())
|
|
147 goto Lno;
|
|
148 tded = targ;
|
|
149 break;
|
|
150
|
|
151 case TOKshared:
|
|
152 if (!targ.isShared())
|
|
153 goto Lno;
|
|
154 tded = targ;
|
|
155 break;
|
|
156 }
|
|
157
|
|
158 case TOKsuper:
|
|
159 // If class or interface, get the base class and interfaces
|
|
160 if (targ.ty != Tclass)
|
|
161 goto Lno;
|
|
162 else
|
|
163 { ClassDeclaration cd = (cast(TypeClass)targ).sym;
|
|
164 Arguments args = new Arguments;
|
|
165 args.reserve(cd.baseclasses.dim);
|
|
166 for (size_t i = 0; i < cd.baseclasses.dim; i++)
|
|
167 {
|
|
168 BaseClass b = cast(BaseClass)cd.baseclasses.data[i];
|
|
169 args.push(cast(void*)new Argument(STCin, b.type, null, null));
|
|
170 }
|
|
171 tded = new TypeTuple(args);
|
|
172 }
|
|
173 break;
|
|
174
|
|
175 case TOKenum:
|
|
176 if (targ.ty != Tenum)
|
|
177 goto Lno;
|
|
178 tded = (cast(TypeEnum)targ).sym.memtype;
|
|
179 break;
|
|
180
|
|
181 case TOKdelegate:
|
|
182 if (targ.ty != Tdelegate)
|
|
183 goto Lno;
|
|
184 tded = (cast(TypeDelegate)targ).next; // the underlying function type
|
|
185 break;
|
|
186
|
|
187 case TOKfunction:
|
|
188 {
|
|
189 if (targ.ty != Tfunction)
|
|
190 goto Lno;
|
|
191 tded = targ;
|
|
192
|
|
193 /* Generate tuple from function parameter types.
|
|
194 */
|
|
195 assert(tded.ty == Tfunction);
|
|
196 Arguments params = (cast(TypeFunction)tded).parameters;
|
|
197 size_t dim = Argument.dim(params);
|
|
198 Arguments args = new Arguments;
|
|
199 args.reserve(dim);
|
|
200 for (size_t i = 0; i < dim; i++)
|
|
201 {
|
|
202 Argument arg = Argument.getNth(params, i);
|
|
203 assert(arg && arg.type);
|
|
204 args.push(cast(void*)new Argument(arg.storageClass, arg.type, null, null));
|
|
205 }
|
|
206 tded = new TypeTuple(args);
|
|
207 break;
|
|
208 }
|
|
209
|
|
210 case TOKreturn:
|
|
211 /* Get the 'return type' for the function,
|
|
212 * delegate, or pointer to function.
|
|
213 */
|
|
214 if (targ.ty == Tfunction)
|
|
215 tded = (cast(TypeFunction)targ).next;
|
|
216 else if (targ.ty == Tdelegate)
|
|
217 { tded = (cast(TypeDelegate)targ).next;
|
|
218 tded = (cast(TypeFunction)tded).next;
|
|
219 }
|
|
220 else if (targ.ty == Tpointer &&
|
|
221 (cast(TypePointer)targ).next.ty == Tfunction)
|
|
222 { tded = (cast(TypePointer)targ).next;
|
|
223 tded = (cast(TypeFunction)tded).next;
|
|
224 }
|
|
225 else
|
|
226 goto Lno;
|
|
227 break;
|
|
228
|
|
229 default:
|
|
230 assert(0);
|
|
231 }
|
|
232 goto Lyes;
|
|
233 }
|
|
234 else if (id && tspec)
|
|
235 {
|
|
236 /* Evaluate to true if targ matches tspec.
|
|
237 * If true, declare id as an alias for the specialized type.
|
|
238 */
|
|
239
|
|
240 MATCH m;
|
|
241 assert(parameters && parameters.dim);
|
|
242
|
|
243 scope Objects dedtypes = new Objects();
|
|
244 dedtypes.setDim(parameters.dim);
|
|
245 dedtypes.zero();
|
|
246
|
|
247 m = targ.deduceType(null, tspec, parameters, dedtypes);
|
|
248 if (m == MATCHnomatch ||
|
|
249 (m != MATCHexact && tok == TOKequal))
|
|
250 {
|
|
251 goto Lno;
|
|
252 }
|
|
253 else
|
|
254 {
|
|
255 tded = cast(Type)dedtypes.data[0];
|
|
256 if (!tded)
|
|
257 tded = targ;
|
|
258
|
|
259 scope Objects tiargs = new Objects();
|
|
260 tiargs.setDim(1);
|
|
261 tiargs.data[0] = cast(void*)targ;
|
|
262
|
|
263 /* Declare trailing parameters
|
|
264 */
|
|
265 for (int i = 1; i < parameters.dim; i++)
|
|
266 {
|
|
267 TemplateParameter tp = cast(TemplateParameter)parameters.data[i];
|
|
268 Declaration s = null;
|
|
269
|
|
270 m = tp.matchArg(sc, tiargs, i, parameters, dedtypes, &s);
|
|
271 if (m == MATCHnomatch)
|
|
272 goto Lno;
|
|
273 s.semantic(sc);
|
|
274 if (!sc.insert(s))
|
|
275 error("declaration %s is already defined", s.toChars());
|
|
276 static if (false) {
|
|
277 Object o = cast(Object)dedtypes.data[i];
|
|
278 Dsymbol s = TemplateDeclaration.declareParameter(loc, sc, tp, o);
|
|
279 }
|
|
280 if (sc.sd)
|
13
|
281 s.addMember(sc, sc.sd, true);
|
0
|
282 }
|
|
283
|
|
284 goto Lyes;
|
|
285 }
|
|
286 }
|
|
287 else if (id)
|
|
288 {
|
|
289 /* Declare id as an alias for type targ. Evaluate to true
|
|
290 */
|
|
291 tded = targ;
|
|
292 goto Lyes;
|
|
293 }
|
|
294 else if (tspec)
|
|
295 {
|
|
296 /* Evaluate to true if targ matches tspec
|
|
297 * is(targ == tspec)
|
|
298 * is(targ : tspec)
|
|
299 */
|
|
300 tspec = tspec.semantic(loc, sc);
|
|
301 //printf("targ = %s\n", targ.toChars());
|
|
302 //printf("tspec = %s\n", tspec.toChars());
|
|
303 if (tok == TOKcolon)
|
|
304 {
|
|
305 if (targ.implicitConvTo(tspec))
|
|
306 goto Lyes;
|
|
307 else
|
|
308 goto Lno;
|
|
309 }
|
|
310 else /* == */
|
|
311 {
|
|
312 if (targ.equals(tspec))
|
|
313 goto Lyes;
|
|
314 else
|
|
315 goto Lno;
|
|
316 }
|
|
317 }
|
|
318
|
|
319 Lyes:
|
|
320 if (id)
|
|
321 {
|
|
322 Dsymbol s = new AliasDeclaration(loc, id, tded);
|
|
323 s.semantic(sc);
|
|
324 if (!sc.insert(s))
|
|
325 error("declaration %s is already defined", s.toChars());
|
|
326 if (sc.sd)
|
13
|
327 s.addMember(sc, sc.sd, true);
|
0
|
328 }
|
|
329 //printf("Lyes\n");
|
|
330 return new IntegerExp(loc, 1, Type.tbool);
|
|
331
|
|
332 Lno:
|
|
333 //printf("Lno\n");
|
|
334 return new IntegerExp(loc, 0, Type.tbool);
|
|
335 }
|
|
336
|
|
337 void toCBuffer(OutBuffer buf, HdrGenState* hgs)
|
|
338 {
|
|
339 assert(false);
|
|
340 }
|
|
341 }
|
|
342
|