comparison dmd/CastExp.d @ 73:ef02e2e203c2

Updating to dmd2.033
author korDen
date Sat, 28 Aug 2010 19:42:41 +0400
parents 2e2a5c3f943a
children 43073c7c7769
comparison
equal deleted inserted replaced
72:2e2a5c3f943a 73:ef02e2e203c2
1 module dmd.CastExp; 1 module dmd.CastExp;
2 2
3 import dmd.Expression; 3 import dmd.Expression;
4 import dmd.TY; 4 import dmd.TY;
5 import dmd.TypeStruct; 5 import dmd.TypeStruct;
6 import dmd.ErrorExp;
6 import dmd.TypeExp; 7 import dmd.TypeExp;
7 import dmd.DotIdExp; 8 import dmd.DotIdExp;
8 import dmd.CallExp; 9 import dmd.CallExp;
9 import dmd.Global; 10 import dmd.Global;
10 import dmd.Id; 11 import dmd.Id;
82 if (type) 83 if (type)
83 return this; 84 return this;
84 super.semantic(sc); 85 super.semantic(sc);
85 if (e1.type) // if not a tuple 86 if (e1.type) // if not a tuple
86 { 87 {
87 e1 = resolveProperties(sc, e1); 88 e1 = resolveProperties(sc, e1);
88 89
89 if (!to) 90 if (!to)
90 {
91 /* Handle cast(const) and cast(immutable), etc.
92 */
93 to = e1.type.castMod(mod);
94 }
95 else
96 to = to.semantic(loc, sc);
97
98 if (!to.equals(e1.type))
99 {
100 e = op_overload(sc);
101 if (e)
102 { 91 {
103 return e.implicitCastTo(sc, to); 92 /* Handle cast(const) and cast(immutable), etc.
104 } 93 */
105 } 94 to = e1.type.castMod(mod);
106 95 }
107 Type t1b = e1.type.toBasetype(); 96 else
108 Type tob = to.toBasetype(); 97 to = to.semantic(loc, sc);
109 if (tob.ty == TY.Tstruct && 98
110 !tob.equals(t1b) && 99 if (!to.equals(e1.type))
111 (cast(TypeStruct)tob).sym.search(Loc(0), Id.call, 0) 100 {
112 ) 101 e = op_overload(sc);
113 { 102 if (e)
114 /* Look to replace: 103 {
115 * cast(S)t 104 return e.implicitCastTo(sc, to);
116 * with: 105 }
117 * S(t) 106 }
118 */ 107
119 108 Type t1b = e1.type.toBasetype();
120 // Rewrite as to.call(e1) 109 Type tob = to.toBasetype();
121 e = new TypeExp(loc, to); 110 if (tob.ty == TY.Tstruct &&
122 e = new DotIdExp(loc, e, Id.call); 111 !tob.equals(t1b) &&
123 e = new CallExp(loc, e, e1); 112 (cast(TypeStruct)tob).sym.search(Loc(0), Id.call, 0)
124 e = e.semantic(sc); 113 )
125 return e; 114 {
126 } 115 /* Look to replace:
116 * cast(S)t
117 * with:
118 * S(t)
119 */
120
121 // Rewrite as to.call(e1)
122 e = new TypeExp(loc, to);
123 e = new DotIdExp(loc, e, Id.call);
124 e = new CallExp(loc, e, e1);
125 e = e.semantic(sc);
126 return e;
127 }
128
129 // Struct casts are possible only when the sizes match
130 if (tob.ty == Tstruct || t1b.ty == Tstruct)
131 {
132 size_t fromsize = cast(size_t)t1b.size(loc);
133 size_t tosize = cast(size_t)tob.size(loc);
134 if (fromsize != tosize)
135 {
136 error("cannot cast from %s to %s", e1.type.toChars(), to.toChars());
137 return new ErrorExp();
138 }
139 }
127 } 140 }
128 else if (!to) 141 else if (!to)
129 { error("cannot cast tuple"); 142 {
130 to = Type.terror; 143 error("cannot cast tuple");
144 to = Type.terror;
131 } 145 }
132 146
133 if (global.params.safe && !sc.module_.safe && !sc.intypeof) 147 if (global.params.safe && !sc.module_.safe && !sc.intypeof)
134 { // Disallow unsafe casts 148 { // Disallow unsafe casts
135 Type tob = to.toBasetype(); 149 Type tob = to.toBasetype();
210 if (ir.imax & 0x80000000) 224 if (ir.imax & 0x80000000)
211 ir.imax |= 0xFFFFFFFF00000000UL; 225 ir.imax |= 0xFFFFFFFF00000000UL;
212 break; 226 break;
213 default: 227 default:
214 } 228 }
215 ir.imin &= type.sizemask(); 229
216 ir.imax &= type.sizemask(); 230 if (type.isintegral())
217 //printf("CastExp: imin = x%llx, imax = x%llx\n", ir.imin, ir.imax); 231 {
232 ir.imin &= type.sizemask();
233 ir.imax &= type.sizemask();
234 }
235
236 //printf("CastExp: imin = x%llx, imax = x%llx\n", ir.imin, ir.imax);
218 return ir; 237 return ir;
219 } 238 }
220 239
221 override Expression optimize(int result) 240 override Expression optimize(int result)
222 { 241 {