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