0
|
1 module dmd.Expression;
|
|
2
|
|
3 import dmd.Loc;
|
|
4 import dmd.TOK;
|
|
5 import dmd.Type;
|
|
6 import dmd.WANT;
|
|
7 import dmd.Scope;
|
|
8 import dmd.ArrayTypes;
|
|
9 import dmd.OutBuffer;
|
|
10 import dmd.HdrGenState;
|
|
11 import dmd.MATCH;
|
|
12 import dmd.IntRange;
|
|
13 import dmd.Dsymbol;
|
|
14 import dmd.FuncDeclaration;
|
|
15 import dmd.InterState;
|
|
16 import dmd.InlineCostState;
|
|
17 import dmd.InlineDoState;
|
|
18 import dmd.InlineScanState;
|
|
19 import dmd.Identifier;
|
|
20 import dmd.IRState;
|
|
21 import dmd.DotIdExp;
|
|
22 import dmd.TypeExp;
|
|
23 import dmd.DYNCAST;
|
|
24 import dmd.TY;
|
|
25 import dmd.CallExp;
|
|
26 import dmd.VarExp;
|
|
27 import dmd.STC;
|
|
28 import dmd.TemplateInstance;
|
|
29 import dmd.CommaExp;
|
|
30 import dmd.NullExp;
|
|
31 import dmd.AddrExp;
|
|
32 import dmd.ErrorExp;
|
|
33 import dmd.TypeStruct;
|
|
34 import dmd.CastExp;
|
|
35 import dmd.Global;
|
|
36 import dmd.Token;
|
|
37 import dmd.TypeClass;
|
|
38 import dmd.PtrExp;
|
|
39 import dmd.TypeSArray;
|
|
40 import dmd.TypeReference;
|
|
41 import dmd.Util;
|
|
42 import dmd.Complex;
|
|
43
|
|
44 import dmd.backend.elem;
|
|
45 import dmd.backend.dt_t;
|
|
46
|
2
|
47 import dmd.Memory;
|
|
48
|
0
|
49 import std.stdio : writef;
|
|
50 import core.stdc.stdlib : malloc;
|
|
51
|
|
52 import std.conv;
|
|
53
|
|
54 /* Things like:
|
|
55 * int.size
|
|
56 * foo.size
|
|
57 * (foo).size
|
|
58 * cast(foo).size
|
|
59 */
|
|
60
|
|
61 Expression typeDotIdExp(Loc loc, Type type, Identifier ident)
|
|
62 {
|
|
63 return new DotIdExp(loc, new TypeExp(loc, type), ident);
|
|
64 }
|
|
65
|
|
66 /*****************************************
|
|
67 * Determine if 'this' is available.
|
|
68 * If it is, return the FuncDeclaration that has it.
|
|
69 */
|
|
70
|
|
71 FuncDeclaration hasThis(Scope sc)
|
|
72 {
|
|
73 FuncDeclaration fd;
|
|
74 FuncDeclaration fdthis;
|
|
75
|
|
76 //printf("hasThis()\n");
|
|
77 fdthis = sc.parent.isFuncDeclaration();
|
|
78 //printf("fdthis = %p, '%s'\n", fdthis, fdthis ? fdthis.toChars() : "");
|
|
79
|
|
80 // Go upwards until we find the enclosing member function
|
|
81 fd = fdthis;
|
|
82 while (1)
|
|
83 {
|
|
84 if (!fd)
|
|
85 {
|
|
86 goto Lno;
|
|
87 }
|
|
88 if (!fd.isNested())
|
|
89 break;
|
|
90
|
|
91 Dsymbol parent = fd.parent;
|
|
92 while (parent)
|
|
93 {
|
|
94 TemplateInstance ti = parent.isTemplateInstance();
|
|
95 if (ti)
|
|
96 parent = ti.parent;
|
|
97 else
|
|
98 break;
|
|
99 }
|
|
100
|
|
101 fd = fd.parent.isFuncDeclaration();
|
|
102 }
|
|
103
|
|
104 if (!fd.isThis())
|
|
105 {
|
|
106 //printf("test '%s'\n", fd.toChars());
|
|
107 goto Lno;
|
|
108 }
|
|
109
|
|
110 assert(fd.vthis);
|
|
111 return fd;
|
|
112
|
|
113 Lno:
|
|
114 return null; // don't have 'this' available
|
|
115 }
|
|
116
|
|
117 /***************************************
|
|
118 * Pull out any properties.
|
|
119 */
|
|
120
|
|
121 Expression resolveProperties(Scope sc, Expression e)
|
|
122 {
|
|
123 //printf("resolveProperties(%s)\n", e.toChars());
|
|
124 if (e.type)
|
|
125 {
|
|
126 Type t = e.type.toBasetype();
|
|
127
|
|
128 if (t.ty == TY.Tfunction || e.op == TOK.TOKoverloadset)
|
|
129 {
|
|
130 e = new CallExp(e.loc, e);
|
|
131 e = e.semantic(sc);
|
|
132 }
|
|
133
|
|
134 /* Look for e being a lazy parameter; rewrite as delegate call
|
|
135 */
|
|
136 else if (e.op == TOK.TOKvar)
|
|
137 { VarExp ve = cast(VarExp)e;
|
|
138
|
|
139 if (ve.var.storage_class & STC.STClazy)
|
|
140 {
|
|
141 e = new CallExp(e.loc, e);
|
|
142 e = e.semantic(sc);
|
|
143 }
|
|
144 }
|
|
145
|
|
146 else if (e.op == TOK.TOKdotexp)
|
|
147 {
|
|
148 e.error("expression has no value");
|
|
149 }
|
|
150 }
|
|
151 else if (e.op == TOK.TOKdottd)
|
|
152 {
|
|
153 e = new CallExp(e.loc, e);
|
|
154 e = e.semantic(sc);
|
|
155 }
|
|
156
|
|
157 return e;
|
|
158 }
|
|
159
|
|
160 class Expression
|
|
161 {
|
|
162 Loc loc; // file location
|
|
163 TOK op; // handy to minimize use of dynamic_cast
|
|
164 Type type; // !=null means that semantic() has been run
|
|
165 int size; // # of bytes in Expression so we can copy() it
|
|
166
|
|
167 this(Loc loc, TOK op, int size)
|
|
168 {
|
|
169 this.loc = loc;
|
|
170 //writef("Expression.Expression(op = %d %s) this = %p\n", op, to!(string)(op), this);
|
|
171 this.op = op;
|
|
172 this.size = size;
|
|
173 type = null;
|
|
174 }
|
|
175
|
|
176 int equals(Object o)
|
|
177 {
|
|
178 return this is o;
|
|
179 }
|
|
180
|
|
181 /*********************************
|
|
182 * Does *not* do a deep copy.
|
|
183 */
|
|
184 Expression copy() /// bad bad bad
|
|
185 {
|
|
186 Expression e;
|
|
187 if (!size)
|
|
188 {
|
|
189 debug {
|
|
190 writef("No expression copy for: %s\n", toChars());
|
|
191 writef("op = %d\n", op);
|
|
192 dump(0);
|
|
193 }
|
|
194 assert(0);
|
|
195 }
|
|
196 auto size = this.classinfo.init.length;
|
2
|
197 auto ptr = GC.malloc(size);
|
0
|
198 memcpy(ptr, cast(void*)this, size);
|
|
199
|
|
200 return cast(Expression)ptr;
|
|
201 }
|
|
202
|
|
203 Expression syntaxCopy()
|
|
204 {
|
|
205 //printf("Expression::syntaxCopy()\n");
|
|
206 //dump(0);
|
|
207 return copy();
|
|
208 }
|
|
209
|
|
210 Expression semantic(Scope sc)
|
|
211 {
|
|
212 version (LOGSEMANTIC) {
|
|
213 printf("Expression.semantic() %s\n", toChars());
|
|
214 }
|
|
215 if (type)
|
|
216 type = type.semantic(loc, sc);
|
|
217 else
|
|
218 type = Type.tvoid;
|
|
219 return this;
|
|
220 }
|
|
221
|
|
222 Expression trySemantic(Scope sc)
|
|
223 {
|
|
224 uint errors = global.errors;
|
|
225 global.gag++;
|
|
226 Expression e = semantic(sc);
|
|
227 global.gag--;
|
|
228 if (errors != global.errors)
|
|
229 {
|
|
230 global.errors = errors;
|
|
231 e = null;
|
|
232 }
|
|
233 return e;
|
|
234 }
|
|
235
|
|
236 DYNCAST dyncast() { return DYNCAST.DYNCAST_EXPRESSION; } // kludge for template.isExpression()
|
|
237
|
|
238 void print()
|
|
239 {
|
|
240 assert(false);
|
|
241 }
|
|
242
|
|
243 string toChars()
|
|
244 {
|
|
245 scope OutBuffer buf = new OutBuffer();
|
|
246 HdrGenState hgs;
|
|
247
|
|
248 toCBuffer(buf, &hgs);
|
|
249 return buf.toChars();
|
|
250 }
|
|
251
|
|
252 void dump(int indent)
|
|
253 {
|
|
254 assert(false);
|
|
255 }
|
|
256
|
|
257 void error(T...)(string format, T t)
|
|
258 {
|
|
259 .error(loc, format, t);
|
|
260 }
|
|
261
|
|
262 void warning(T...)(string formar, T t)
|
|
263 {
|
|
264 .warning(loc, format, t);
|
|
265 }
|
|
266
|
|
267 void rvalue()
|
|
268 {
|
|
269 if (type && type.toBasetype().ty == TY.Tvoid)
|
|
270 {
|
|
271 error("expression %s is void and has no value", toChars());
|
|
272 static if (false) {
|
|
273 dump(0);
|
|
274 halt();
|
|
275 }
|
|
276 type = Type.terror;
|
|
277 }
|
|
278 }
|
|
279
|
|
280 static Expression combine(Expression e1, Expression e2)
|
|
281 {
|
|
282 if (e1)
|
|
283 {
|
|
284 if (e2)
|
|
285 {
|
|
286 e1 = new CommaExp(e1.loc, e1, e2);
|
|
287 e1.type = e2.type;
|
|
288 }
|
|
289 }
|
|
290 else
|
|
291 {
|
|
292 e1 = e2;
|
|
293 }
|
|
294
|
|
295 return e1;
|
|
296 }
|
|
297
|
|
298 static Expressions arraySyntaxCopy(Expressions exps)
|
|
299 {
|
|
300 Expressions a = null;
|
|
301
|
|
302 if (exps)
|
|
303 {
|
|
304 a = new Expressions();
|
|
305 a.setDim(exps.dim);
|
|
306 for (int i = 0; i < a.dim; i++)
|
|
307 {
|
|
308 Expression e = cast(Expression)exps.data[i];
|
|
309
|
|
310 e = e.syntaxCopy();
|
|
311 a.data[i] = cast(void*)e;
|
|
312 }
|
|
313 }
|
|
314 return a;
|
|
315 }
|
|
316
|
|
317 ulong toInteger()
|
|
318 {
|
|
319 assert(false);
|
|
320 }
|
|
321
|
|
322 ulong toUInteger()
|
|
323 {
|
|
324 //printf("Expression %s\n", Token.toChars(op));
|
|
325 return cast(ulong)toInteger();
|
|
326 }
|
|
327
|
|
328 real toReal()
|
|
329 {
|
|
330 assert(false);
|
|
331 }
|
|
332
|
|
333 real toImaginary()
|
|
334 {
|
|
335 assert(false);
|
|
336 }
|
|
337
|
|
338 Complex!(real) toComplex()
|
|
339 {
|
|
340 assert(false);
|
|
341 }
|
|
342
|
|
343 void toCBuffer(OutBuffer buf, HdrGenState* hgs)
|
|
344 {
|
|
345 buf.writestring(Token.toChars(op));
|
|
346 }
|
|
347
|
|
348 void toMangleBuffer(OutBuffer buf)
|
|
349 {
|
|
350 assert(false);
|
|
351 }
|
|
352
|
|
353 int isLvalue()
|
|
354 {
|
|
355 assert(false);
|
|
356 }
|
|
357
|
|
358 Expression toLvalue(Scope sc, Expression e)
|
|
359 {
|
|
360 assert(false);
|
|
361 }
|
|
362
|
|
363 Expression modifiableLvalue(Scope sc, Expression e)
|
|
364 {
|
|
365 //printf("Expression::modifiableLvalue() %s, type = %s\n", toChars(), type.toChars());
|
|
366
|
|
367 // See if this expression is a modifiable lvalue (i.e. not const)
|
|
368 version (DMDV2) {
|
|
369 if (type && (!type.isMutable() || !type.isAssignable()))
|
|
370 error("%s is not mutable", e.toChars());
|
|
371 }
|
|
372 return toLvalue(sc, e);
|
|
373 }
|
|
374
|
|
375 /**************************************
|
|
376 * Do an implicit cast.
|
|
377 * Issue error if it can't be done.
|
|
378 */
|
|
379 Expression implicitCastTo(Scope sc, Type t)
|
|
380 {
|
|
381 //printf("Expression.implicitCastTo(%s of type %s) => %s\n", toChars(), type.toChars(), t.toChars());
|
|
382
|
|
383 MATCH match = implicitConvTo(t);
|
|
384 if (match)
|
|
385 {
|
|
386 TY tyfrom = type.toBasetype().ty;
|
|
387 TY tyto = t.toBasetype().ty;
|
|
388
|
|
389 version (DMDV1) {
|
|
390 if (global.params.warnings &&
|
|
391 Type.impcnvWarn[tyfrom][tyto] &&
|
|
392 op != TOKint64)
|
|
393 {
|
|
394 Expression e = optimize(WANT.WANTflags | WANT.WANTvalue);
|
|
395
|
|
396 if (e.op == TOK.TOKint64)
|
|
397 return e.implicitCastTo(sc, t);
|
|
398 if (tyfrom == Tint32 && (op == TOKadd || op == TOKmin || op == TOKand || op == TOKor || op == TOKxor))
|
|
399 {
|
|
400 /* This is really only a semi-kludge fix,
|
|
401 * we really should look at the operands of op
|
|
402 * and see if they are narrower types.
|
|
403 * For example, b=b|b and b=b|7 and s=b+b should be allowed,
|
|
404 * but b=b|i should be an error.
|
|
405 */
|
|
406 ;
|
|
407 }
|
|
408 else
|
|
409 {
|
|
410 warning("implicit conversion of expression (%s) of type %s to %s can cause loss of data", toChars(), type.toChars(), t.toChars());
|
|
411 }
|
|
412 }
|
|
413 }
|
|
414 version (DMDV2) {
|
|
415 if (match == MATCH.MATCHconst && t == type.constOf())
|
|
416 {
|
|
417 Expression e = copy();
|
|
418 e.type = t;
|
|
419 return e;
|
|
420 }
|
|
421 }
|
|
422 return castTo(sc, t);
|
|
423 }
|
|
424
|
|
425 Expression e = optimize(WANT.WANTflags | WANT.WANTvalue);
|
|
426 if (e != this)
|
|
427 return e.implicitCastTo(sc, t);
|
|
428
|
|
429 static if (false) {
|
|
430 printf("ty = %d\n", type.ty);
|
|
431 print();
|
|
432 type.print();
|
|
433 printf("to:\n");
|
|
434 t.print();
|
|
435 printf("%p %p type: %s to: %s\n", type.deco, t.deco, type.deco, t.deco);
|
|
436 //printf("%p %p %p\n", type.nextOf().arrayOf(), type, t);
|
|
437 fflush(stdout);
|
|
438 }
|
|
439 if (!t.deco) {
|
|
440 /* Can happen with:
|
|
441 * enum E { One }
|
|
442 * class A
|
|
443 * { static void fork(EDG dg) { dg(E.One); }
|
|
444 * alias void delegate(E) EDG;
|
|
445 * }
|
|
446 * Should eventually make it work.
|
|
447 */
|
|
448 error("forward reference to type %s", t.toChars());
|
|
449 } else if (t.reliesOnTident()) {
|
|
450 error("forward reference to type %s", t.reliesOnTident().toChars());
|
|
451 }
|
|
452
|
|
453 error("cannot implicitly convert expression (%s) of type %s to %s", toChars(), type.toChars(), t.toChars());
|
|
454 return castTo(sc, t);
|
|
455 }
|
|
456
|
|
457 /*******************************************
|
|
458 * Return !=0 if we can implicitly convert this to type t.
|
|
459 * Don't do the actual cast.
|
|
460 */
|
|
461 MATCH implicitConvTo(Type t)
|
|
462 {
|
|
463 static if (false) {
|
|
464 printf("Expression.implicitConvTo(this=%s, type=%s, t=%s)\n",
|
|
465 toChars(), type.toChars(), t.toChars());
|
|
466 }
|
|
467 //static int nest; if (++nest == 10) halt();
|
|
468 if (!type)
|
|
469 {
|
|
470 error("%s is not an expression", toChars());
|
|
471 type = Type.terror;
|
|
472 }
|
|
473 Expression e = optimize(WANT.WANTvalue | WANT.WANTflags);
|
|
474 if (e.type == t)
|
|
475 return MATCH.MATCHexact;
|
|
476 if (e != this)
|
|
477 {
|
|
478 //printf("\toptimized to %s of type %s\n", e.toChars(), e.type.toChars());
|
|
479 return e.implicitConvTo(t);
|
|
480 }
|
|
481 MATCH match = type.implicitConvTo(t);
|
|
482 if (match != MATCH.MATCHnomatch)
|
|
483 return match;
|
|
484
|
|
485 /* See if we can do integral narrowing conversions
|
|
486 */
|
|
487 if (type.isintegral() && t.isintegral() &&
|
|
488 type.isTypeBasic() && t.isTypeBasic())
|
|
489 {
|
|
490 IntRange ir = getIntRange();
|
|
491 if (ir.imax <= t.sizemask())
|
|
492 return MATCH.MATCHconvert;
|
|
493 }
|
|
494
|
|
495 static if (false) {
|
|
496 Type tb = t.toBasetype();
|
|
497 if (tb.ty == Tdelegate)
|
|
498 {
|
|
499 TypeDelegate td = cast(TypeDelegate)tb;
|
|
500 TypeFunction tf = cast(TypeFunction)td.nextOf();
|
|
501
|
|
502 if (!tf.varargs && !(tf.arguments && tf.arguments.dim))
|
|
503 {
|
|
504 match = type.implicitConvTo(tf.nextOf());
|
|
505 if (match)
|
|
506 return match;
|
|
507 if (tf.nextOf().toBasetype().ty == Tvoid)
|
|
508 return MATCH.MATCHconvert;
|
|
509 }
|
|
510 }
|
|
511 }
|
|
512 return MATCH.MATCHnomatch;
|
|
513 }
|
|
514
|
|
515 IntRange getIntRange()
|
|
516 {
|
|
517 assert(false);
|
|
518 }
|
|
519
|
|
520 /**************************************
|
|
521 * Do an explicit cast.
|
|
522 */
|
|
523 Expression castTo(Scope sc, Type t)
|
|
524 {
|
|
525 //printf("Expression.castTo(this=%s, t=%s)\n", toChars(), t.toChars());
|
|
526 static if (false) {
|
|
527 writef("Expression.castTo(this=%s, type=%s, t=%s)\n",
|
|
528 toChars(), type.toChars(), t.toChars());
|
|
529 }
|
|
530 if (type is t)
|
|
531 return this;
|
|
532 Expression e = this;
|
|
533 Type tb = t.toBasetype();
|
|
534 Type typeb = type.toBasetype();
|
|
535 if (tb != typeb)
|
|
536 {
|
|
537 // Do (type *) cast of (type [dim])
|
|
538 if (tb.ty == TY.Tpointer && typeb.ty == TY.Tsarray
|
|
539 )
|
|
540 {
|
|
541 //printf("Converting [dim] to *\n");
|
|
542
|
|
543 if (typeb.size(loc) == 0)
|
|
544 e = new NullExp(loc);
|
|
545 else
|
|
546 e = new AddrExp(loc, e);
|
|
547 }
|
|
548 else {
|
|
549 static if (false) {
|
|
550 if (tb.ty == Tdelegate && type.ty != Tdelegate)
|
|
551 {
|
|
552 TypeDelegate td = cast(TypeDelegate)tb;
|
|
553 TypeFunction tf = cast(TypeFunction)td.nextOf();
|
|
554 return toDelegate(sc, tf.nextOf());
|
|
555 }
|
|
556 }
|
|
557 if (typeb.ty == TY.Tstruct)
|
|
558 {
|
|
559 TypeStruct ts = cast(TypeStruct)typeb;
|
|
560 if (!(tb.ty == TY.Tstruct && ts.sym == (cast(TypeStruct)tb).sym) &&
|
|
561 ts.sym.aliasthis)
|
|
562 { /* Forward the cast to our alias this member, rewrite to:
|
|
563 * cast(to)e1.aliasthis
|
|
564 */
|
|
565 Expression e1 = new DotIdExp(loc, this, ts.sym.aliasthis.ident);
|
|
566 Expression e2 = new CastExp(loc, e1, tb);
|
|
567 e2 = e2.semantic(sc);
|
|
568 return e2;
|
|
569 }
|
|
570 }
|
|
571 else if (typeb.ty == TY.Tclass)
|
|
572 {
|
|
573 TypeClass ts = cast(TypeClass)typeb;
|
|
574 if (tb.ty != TY.Tclass && ts.sym.aliasthis)
|
|
575 { /* Forward the cast to our alias this member, rewrite to:
|
|
576 * cast(to)e1.aliasthis
|
|
577 */
|
|
578 Expression e1 = new DotIdExp(loc, this, ts.sym.aliasthis.ident);
|
|
579 Expression e2 = new CastExp(loc, e1, tb);
|
|
580 e2 = e2.semantic(sc);
|
|
581 return e2;
|
|
582 }
|
|
583 }
|
|
584 e = new CastExp(loc, e, tb);
|
|
585 }
|
|
586 }
|
|
587 else
|
|
588 {
|
|
589 e = e.copy(); // because of COW for assignment to e.type
|
|
590 }
|
|
591
|
|
592 assert(e != this);
|
|
593 e.type = t;
|
|
594 //printf("Returning: %s\n", e.toChars());
|
|
595 return e;
|
|
596 }
|
|
597
|
|
598 /************************************
|
|
599 * Detect cases where pointers to the stack can 'escape' the
|
|
600 * lifetime of the stack frame.
|
|
601 */
|
|
602 void checkEscape()
|
|
603 {
|
|
604 }
|
|
605
|
|
606 void checkScalar()
|
|
607 {
|
|
608 if (!type.isscalar())
|
|
609 error("'%s' is not a scalar, it is a %s", toChars(), type.toChars());
|
|
610
|
|
611 rvalue();
|
|
612 }
|
|
613
|
|
614 void checkNoBool()
|
|
615 {
|
|
616 if (type.toBasetype().ty == TY.Tbool)
|
|
617 error("operation not allowed on bool '%s'", toChars());
|
|
618 }
|
|
619
|
|
620 Expression checkIntegral()
|
|
621 {
|
|
622 if (!type.isintegral())
|
|
623 {
|
|
624 error("'%s' is not of integral type, it is a %s", toChars(), type.toChars());
|
|
625 return new ErrorExp();
|
|
626 }
|
|
627
|
|
628 rvalue();
|
|
629 return this;
|
|
630 }
|
|
631
|
|
632 Expression checkArithmetic()
|
|
633 {
|
|
634 if (!type.isintegral() && !type.isfloating())
|
|
635 {
|
|
636 error("'%s' is not of arithmetic type, it is a %s", toChars(), type.toChars());
|
|
637 return new ErrorExp();
|
|
638 }
|
|
639
|
|
640 rvalue();
|
|
641 return this;
|
|
642 }
|
|
643
|
|
644 void checkDeprecated(Scope sc, Dsymbol s)
|
|
645 {
|
|
646 s.checkDeprecated(loc, sc);
|
|
647 }
|
|
648
|
|
649 void checkPurity(Scope sc, FuncDeclaration f)
|
|
650 {
|
|
651 static if (true) {
|
|
652 if (sc.func)
|
|
653 {
|
|
654 FuncDeclaration outerfunc = sc.func;
|
|
655 while (outerfunc.toParent2() && outerfunc.toParent2().isFuncDeclaration())
|
|
656 {
|
|
657 outerfunc = outerfunc.toParent2().isFuncDeclaration();
|
|
658 }
|
|
659 if (outerfunc.isPure() && !sc.intypeof && (!f.isNested() && !f.isPure()))
|
|
660 error("pure function '%s' cannot call impure function '%s'\n",
|
|
661 sc.func.toChars(), f.toChars());
|
|
662 }
|
|
663 } else {
|
|
664 if (sc.func && sc.func.isPure() && !sc.intypeof && !f.isPure())
|
|
665 error("pure function '%s' cannot call impure function '%s'\n",
|
|
666 sc.func.toChars(), .toChars());
|
|
667 }
|
|
668 }
|
|
669
|
|
670 /*****************************
|
|
671 * Check that expression can be tested for true or false.
|
|
672 */
|
|
673 Expression checkToBoolean()
|
|
674 {
|
|
675 // Default is 'yes' - do nothing
|
|
676
|
|
677 debug {
|
|
678 if (!type)
|
|
679 dump(0);
|
|
680 }
|
|
681
|
|
682 if (!type.checkBoolean())
|
|
683 {
|
|
684 error("expression %s of type %s does not have a boolean value", toChars(), type.toChars());
|
|
685 }
|
|
686
|
|
687 return this;
|
|
688 }
|
|
689
|
|
690 Expression checkToPointer()
|
|
691 {
|
|
692 Expression e;
|
|
693 Type tb;
|
|
694
|
|
695 //printf("Expression::checkToPointer()\n");
|
|
696 e = this;
|
|
697
|
|
698 // If C static array, convert to pointer
|
|
699 tb = type.toBasetype();
|
|
700 if (tb.ty == Tsarray)
|
|
701 {
|
|
702 TypeSArray ts = cast(TypeSArray)tb;
|
|
703 if (ts.size(loc) == 0)
|
|
704 e = new NullExp(loc);
|
|
705 else
|
|
706 e = new AddrExp(loc, this);
|
|
707 e.type = ts.next.pointerTo();
|
|
708 }
|
|
709 return e;
|
|
710 }
|
|
711
|
|
712 Expression addressOf(Scope sc)
|
|
713 {
|
|
714 //printf("Expression::addressOf()\n");
|
|
715 Expression e = toLvalue(sc, null);
|
|
716 e = new AddrExp(loc, e);
|
|
717 e.type = type.pointerTo();
|
|
718 return e;
|
|
719 }
|
|
720
|
|
721 /******************************
|
|
722 * If this is a reference, dereference it.
|
|
723 */
|
|
724 Expression deref()
|
|
725 {
|
|
726 //printf("Expression::deref()\n");
|
|
727 if (type.ty == TY.Treference)
|
|
728 {
|
|
729 Expression e = new PtrExp(loc, this);
|
|
730 e.type = (cast(TypeReference)type).next;
|
|
731 return e;
|
|
732 }
|
|
733 return this;
|
|
734 }
|
|
735
|
|
736 /***********************************
|
|
737 * Do integral promotions (convertchk).
|
|
738 * Don't convert <array of> to <pointer to>
|
|
739 */
|
|
740 Expression integralPromotions(Scope sc)
|
|
741 {
|
|
742 Expression e = this;
|
|
743
|
|
744 //printf("integralPromotions %s %s\n", e.toChars(), e.type.toChars());
|
|
745 switch (type.toBasetype().ty)
|
|
746 {
|
|
747 case TY.Tvoid:
|
|
748 error("void has no value");
|
|
749 break;
|
|
750
|
|
751 case TY.Tint8:
|
|
752 case TY.Tuns8:
|
|
753 case TY.Tint16:
|
|
754 case TY.Tuns16:
|
|
755 case TY.Tbit:
|
|
756 case TY.Tbool:
|
|
757 case TY.Tchar:
|
|
758 case TY.Twchar:
|
|
759 e = e.castTo(sc, Type.tint32);
|
|
760 break;
|
|
761
|
|
762 case TY.Tdchar:
|
|
763 e = e.castTo(sc, Type.tuns32);
|
|
764 break;
|
|
765 default:
|
|
766 break; ///
|
|
767 }
|
|
768 return e;
|
|
769 }
|
|
770
|
|
771 Expression toDelegate(Scope sc, Type t)
|
|
772 {
|
|
773 assert(false);
|
|
774 }
|
|
775
|
|
776 void scanForNestedRef(Scope sc)
|
|
777 {
|
|
778 assert(false);
|
|
779 }
|
|
780
|
|
781 Expression optimize(int result)
|
|
782 {
|
|
783 //printf("Expression.optimize(result = x%x) %s\n", result, toChars());
|
|
784 return this;
|
|
785 }
|
|
786
|
|
787 Expression interpret(InterState istate)
|
|
788 {
|
|
789 assert(false);
|
|
790 }
|
|
791
|
|
792 int isConst()
|
|
793 {
|
|
794 //printf("Expression::isConst(): %s\n", toChars());
|
|
795 return 0;
|
|
796 }
|
|
797
|
|
798 /********************************
|
|
799 * Does this expression statically evaluate to a boolean TRUE or FALSE?
|
|
800 */
|
|
801 bool isBool(bool result)
|
|
802 {
|
|
803 return false;
|
|
804 }
|
|
805
|
|
806 int isBit()
|
|
807 {
|
|
808 assert(false);
|
|
809 }
|
|
810
|
|
811 /********************************
|
|
812 * Check for expressions that have no use.
|
|
813 * Input:
|
|
814 * flag 0 not going to use the result, so issue error message if no
|
|
815 * side effects
|
|
816 * 1 the result of the expression is used, but still check
|
|
817 * for useless subexpressions
|
|
818 * 2 do not issue error messages, just return !=0 if expression
|
|
819 * has side effects
|
|
820 */
|
|
821 bool checkSideEffect(int flag)
|
|
822 {
|
|
823 if (flag == 0)
|
|
824 {
|
|
825 if (op == TOKimport)
|
|
826 error("%s has no effect", toChars());
|
|
827 else
|
|
828 error("%s has no effect in expression (%s)",
|
|
829
|
|
830 Token.toChars(op), toChars());
|
|
831 }
|
|
832
|
|
833 return false;
|
|
834 }
|
|
835
|
|
836 bool canThrow()
|
|
837 {
|
|
838 version (DMDV2) {
|
|
839 return false;
|
|
840 } else {
|
|
841 return true;
|
|
842 }
|
|
843 }
|
|
844
|
|
845 int inlineCost(InlineCostState* ics)
|
|
846 {
|
|
847 return 1;
|
|
848 }
|
|
849
|
|
850 Expression doInline(InlineDoState ids)
|
|
851 {
|
|
852 //printf("Expression.doInline(%s): %s\n", Token.toChars(op), toChars());
|
|
853 return copy();
|
|
854 }
|
|
855
|
|
856 Expression inlineScan(InlineScanState* iss)
|
|
857 {
|
|
858 return this;
|
|
859 }
|
|
860
|
|
861 /***********************************
|
|
862 * Determine if operands of binary op can be reversed
|
|
863 * to fit operator overload.
|
|
864 */
|
|
865
|
|
866 // For operator overloading
|
|
867 bool isCommutative()
|
|
868 {
|
|
869 return false; // default is no reverse
|
|
870 }
|
|
871
|
|
872 /***********************************
|
|
873 * Get Identifier for operator overload.
|
|
874 */
|
|
875 Identifier opId()
|
|
876 {
|
|
877 assert(false);
|
|
878 }
|
|
879
|
|
880 /***********************************
|
|
881 * Get Identifier for reverse operator overload,
|
|
882 * null if not supported for this operator.
|
|
883 */
|
|
884 Identifier opId_r()
|
|
885 {
|
|
886 return null;
|
|
887 }
|
|
888
|
|
889 // For array ops
|
|
890 void buildArrayIdent(OutBuffer buf, Expressions arguments)
|
|
891 {
|
|
892 assert(false);
|
|
893 }
|
|
894
|
|
895 Expression buildArrayLoop(Arguments fparams)
|
|
896 {
|
|
897 assert(false);
|
|
898 }
|
|
899
|
|
900 // Back end
|
|
901 elem* toElem(IRState* irs)
|
|
902 {
|
|
903 assert(false);
|
|
904 }
|
|
905
|
|
906 dt_t** toDt(dt_t** pdt)
|
|
907 {
|
|
908 assert(false);
|
|
909 }
|
|
910 } |