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