annotate dmd/Cast.d @ 0:10317f0c89a5

Initial commit
author korDen
date Sat, 24 Oct 2009 08:42:06 +0400
parents
children cab4c37afb89
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1 module dmd.Cast;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
2
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
3 import dmd.Expression;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
4 import dmd.Type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
5 import dmd.Loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
6 import dmd.MATCH;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7 import dmd.IntegerExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
8 import dmd.RealExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
9 import dmd.ComplexExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
10 import dmd.StructDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
11 import dmd.ArrayTypes;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
12 import dmd.Dsymbol;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
13 import dmd.VarDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
14 import dmd.StructLiteralExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15 import dmd.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16 import dmd.TY;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17 import dmd.TOK;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18 import dmd.GlobalExpressions;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20 import dmd.Complex;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 Expression expType(Type type, Expression e)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24 if (type !is e.type)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26 e = e.copy();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27 e.type = type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 /* Also returns EXP_CANT_INTERPRET if cannot be computed.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33 * to: type to cast to
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34 * type: type to paint the result
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 Expression Cast(Type type, Type to, Expression e1)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39 Expression e = EXP_CANT_INTERPRET;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40 Loc loc = e1.loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42 //printf("Cast(type = %s, to = %s, e1 = %s)\n", type.toChars(), to.toChars(), e1.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43 //printf("\te1.type = %s\n", e1.type.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 if (e1.type.equals(type) && type.equals(to))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 return e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46 if (e1.type.implicitConvTo(to) >= MATCH.MATCHconst || to.implicitConvTo(e1.type) >= MATCH.MATCHconst)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 return expType(to, e1);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49 Type tb = to.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 Type typeb = type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52 /* Allow casting from one string type to another
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54 if (e1.op == TOK.TOKstring)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 if (tb.ty == TY.Tarray && typeb.ty == TY.Tarray &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 tb.nextOf().size() == typeb.nextOf().size())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 return expType(to, e1);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 if (e1.isConst() != 1)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 return EXP_CANT_INTERPRET;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66 if (tb.ty == TY.Tbool)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 e = new IntegerExp(loc, e1.toInteger() != 0, type);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 else if (type.isintegral())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 if (e1.type.isfloating())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72 long result;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 real r = e1.toReal();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75 switch (typeb.ty)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 case TY.Tint8: result = cast(byte)r; break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78 case TY.Tchar:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79 case TY.Tuns8: result = cast(ubyte)r; break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 case TY.Tint16: result = cast(short)r; break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 case TY.Twchar:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 case TY.Tuns16: result = cast(ushort)r; break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 case TY.Tint32: result = cast(int)r; break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 case TY.Tdchar:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 case TY.Tuns32: result = cast(uint)r; break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 case TY.Tint64: result = cast(long)r; break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 case TY.Tuns64: result = cast(ulong)r; break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90 e = new IntegerExp(loc, result, type);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92 else if (type.isunsigned())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93 e = new IntegerExp(loc, e1.toUInteger(), type);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95 e = new IntegerExp(loc, e1.toInteger(), type);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97 else if (tb.isreal())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99 real value = e1.toReal();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100 e = new RealExp(loc, value, type);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102 else if (tb.isimaginary())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104 real value = e1.toImaginary();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105 e = new RealExp(loc, value, type);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107 else if (tb.iscomplex())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109 Complex!(real) value = e1.toComplex();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110 e = new ComplexExp(loc, value, type);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112 else if (tb.isscalar())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113 e = new IntegerExp(loc, e1.toInteger(), type);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114 else if (tb.ty == TY.Tvoid)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
115 e = EXP_CANT_INTERPRET;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116 else if (tb.ty == TY.Tstruct && e1.op == TOK.TOKint64)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118 // Struct = 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119 StructDeclaration sd = tb.toDsymbol(null).isStructDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
120 assert(sd);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
121 Expressions elements = new Expressions;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
122 for (size_t i = 0; i < sd.fields.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
123 { Dsymbol s = cast(Dsymbol)sd.fields.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
124 VarDeclaration v = s.isVarDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
125 assert(v);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
126
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
127 Expression exp = new IntegerExp(0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
128 exp = Cast(v.type, v.type, exp);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
129 if (exp == EXP_CANT_INTERPRET)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
130 return exp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
131 elements.push(cast(void*)exp);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
132 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
133 e = new StructLiteralExp(loc, sd, elements);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
134 e.type = type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
135 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
136 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
137 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
138 error(loc, "cannot cast %s to %s", e1.type.toChars(), type.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
139 e = new IntegerExp(loc, 0, Type.tint32);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
140 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
141 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
142 }