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