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