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