Mercurial > projects > ddmd
annotate dmd/Cast.d @ 114:e28b18c23469
added a module dmd.common for commonly used stuff
it currently holds code for consistency checking of predefined versions
also added a VisualD project file
author | Trass3r |
---|---|
date | Wed, 01 Sep 2010 18:21:58 +0200 |
parents | 8e69d041a99d |
children | af1bebfd96a4 |
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 | |
64 if (e1.isConst() != 1) | |
65 return EXP_CANT_INTERPRET; | |
66 | |
67 if (tb.ty == TY.Tbool) | |
68 e = new IntegerExp(loc, e1.toInteger() != 0, type); | |
69 else if (type.isintegral()) | |
70 { | |
71 if (e1.type.isfloating()) | |
72 { | |
73 long result; | |
74 real r = e1.toReal(); | |
75 | |
76 switch (typeb.ty) | |
77 { | |
78 case TY.Tint8: result = cast(byte)r; break; | |
79 case TY.Tchar: | |
80 case TY.Tuns8: result = cast(ubyte)r; break; | |
81 case TY.Tint16: result = cast(short)r; break; | |
82 case TY.Twchar: | |
83 case TY.Tuns16: result = cast(ushort)r; break; | |
84 case TY.Tint32: result = cast(int)r; break; | |
85 case TY.Tdchar: | |
86 case TY.Tuns32: result = cast(uint)r; break; | |
87 case TY.Tint64: result = cast(long)r; break; | |
88 case TY.Tuns64: result = cast(ulong)r; break; | |
89 } | |
90 | |
91 e = new IntegerExp(loc, result, type); | |
92 } | |
93 else if (type.isunsigned()) | |
94 e = new IntegerExp(loc, e1.toUInteger(), type); | |
95 else | |
96 e = new IntegerExp(loc, e1.toInteger(), type); | |
97 } | |
98 else if (tb.isreal()) | |
99 { | |
100 real value = e1.toReal(); | |
101 e = new RealExp(loc, value, type); | |
102 } | |
103 else if (tb.isimaginary()) | |
104 { | |
105 real value = e1.toImaginary(); | |
106 e = new RealExp(loc, value, type); | |
107 } | |
108 else if (tb.iscomplex()) | |
109 { | |
110 Complex!(real) value = e1.toComplex(); | |
111 e = new ComplexExp(loc, value, type); | |
112 } | |
113 else if (tb.isscalar()) | |
114 e = new IntegerExp(loc, e1.toInteger(), type); | |
115 else if (tb.ty == TY.Tvoid) | |
116 e = EXP_CANT_INTERPRET; | |
117 else if (tb.ty == TY.Tstruct && e1.op == TOK.TOKint64) | |
118 { | |
119 // Struct = 0; | |
120 StructDeclaration sd = tb.toDsymbol(null).isStructDeclaration(); | |
121 assert(sd); | |
122 Expressions elements = new Expressions; | |
85
8e69d041a99d
Previous commit didn't compile. Fixed.
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
123 foreach (VarDeclaration v; sd.fields) |
79 | 124 { |
0 | 125 assert(v); |
126 | |
127 Expression exp = new IntegerExp(0); | |
128 exp = Cast(v.type, v.type, exp); | |
63 | 129 if (exp is EXP_CANT_INTERPRET) |
0 | 130 return exp; |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
131 elements.push(exp); |
0 | 132 } |
133 e = new StructLiteralExp(loc, sd, elements); | |
134 e.type = type; | |
135 } | |
136 else | |
137 { | |
138 error(loc, "cannot cast %s to %s", e1.type.toChars(), type.toChars()); | |
139 e = new IntegerExp(loc, 0, Type.tint32); | |
140 } | |
141 return e; | |
142 } |