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