0
|
1 module dmd.CondExp;
|
|
2
|
114
|
3 import dmd.common;
|
0
|
4 import dmd.BinExp;
|
|
5 import dmd.Loc;
|
123
|
6 import dmd.PtrExp;
|
0
|
7 import dmd.MATCH;
|
|
8 import dmd.Expression;
|
|
9 import dmd.Scope;
|
|
10 import dmd.InterState;
|
|
11 import dmd.OutBuffer;
|
|
12 import dmd.HdrGenState;
|
|
13 import dmd.Type;
|
|
14 import dmd.InlineCostState;
|
|
15 import dmd.InlineDoState;
|
|
16 import dmd.InlineScanState;
|
|
17 import dmd.IRState;
|
|
18 import dmd.TOK;
|
|
19 import dmd.TY;
|
|
20 import dmd.WANT;
|
50
|
21 import dmd.PREC;
|
0
|
22 import dmd.Global;
|
|
23
|
|
24 import dmd.backend.elem;
|
|
25 import dmd.backend.Util;
|
|
26 import dmd.backend.OPER;
|
|
27 import dmd.backend.mTY;
|
|
28 import dmd.backend.TYM;
|
|
29 import dmd.codegen.Util;
|
|
30
|
50
|
31 import dmd.expression.Util;
|
|
32
|
0
|
33 class CondExp : BinExp
|
|
34 {
|
|
35 Expression econd;
|
|
36
|
|
37 this(Loc loc, Expression econd, Expression e1, Expression e2)
|
|
38 {
|
|
39 super(loc, TOK.TOKquestion, CondExp.sizeof, e1, e2);
|
|
40 this.econd = econd;
|
|
41 }
|
|
42
|
72
|
43 override Expression syntaxCopy()
|
0
|
44 {
|
43
|
45 return new CondExp(loc, econd.syntaxCopy(), e1.syntaxCopy(), e2.syntaxCopy());
|
0
|
46 }
|
|
47
|
72
|
48 override Expression semantic(Scope sc)
|
0
|
49 {
|
|
50 Type t1;
|
|
51 Type t2;
|
|
52 uint cs0;
|
|
53 uint cs1;
|
|
54
|
|
55 version (LOGSEMANTIC) {
|
|
56 printf("CondExp.semantic('%s')\n", toChars());
|
|
57 }
|
|
58 if (type)
|
|
59 return this;
|
|
60
|
|
61 econd = econd.semantic(sc);
|
|
62 econd = resolveProperties(sc, econd);
|
|
63 econd = econd.checkToPointer();
|
|
64 econd = econd.checkToBoolean();
|
|
65
|
|
66 static if (false) {
|
|
67 /* this cannot work right because the types of e1 and e2
|
|
68 * both contribute to the type of the result.
|
|
69 */
|
|
70 if (sc.flags & SCOPEstaticif)
|
|
71 {
|
|
72 /* If in static if, don't evaluate what we don't have to.
|
|
73 */
|
|
74 econd = econd.optimize(WANTflags);
|
|
75 if (econd.isBool(TRUE))
|
|
76 {
|
|
77 e1 = e1.semantic(sc);
|
|
78 e1 = resolveProperties(sc, e1);
|
|
79 return e1;
|
|
80 }
|
|
81 else if (econd.isBool(FALSE))
|
|
82 {
|
|
83 e2 = e2.semantic(sc);
|
|
84 e2 = resolveProperties(sc, e2);
|
|
85 return e2;
|
|
86 }
|
|
87 }
|
|
88 }
|
|
89
|
|
90 cs0 = sc.callSuper;
|
|
91 e1 = e1.semantic(sc);
|
|
92 e1 = resolveProperties(sc, e1);
|
|
93 cs1 = sc.callSuper;
|
|
94 sc.callSuper = cs0;
|
|
95 e2 = e2.semantic(sc);
|
|
96 e2 = resolveProperties(sc, e2);
|
|
97 sc.mergeCallSuper(loc, cs1);
|
|
98
|
|
99 // If either operand is void, the result is void
|
|
100 t1 = e1.type;
|
|
101 t2 = e2.type;
|
|
102 if (t1.ty == Tvoid || t2.ty == Tvoid)
|
|
103 type = Type.tvoid;
|
|
104 else if (t1 == t2)
|
|
105 type = t1;
|
|
106 else
|
|
107 {
|
|
108 typeCombine(sc);
|
|
109 switch (e1.type.toBasetype().ty)
|
|
110 {
|
|
111 case Tcomplex32:
|
|
112 case Tcomplex64:
|
|
113 case Tcomplex80:
|
|
114 e2 = e2.castTo(sc, e1.type);
|
|
115 break;
|
|
116 default:
|
|
117 break;
|
|
118 }
|
|
119 switch (e2.type.toBasetype().ty)
|
|
120 {
|
|
121 case Tcomplex32:
|
|
122 case Tcomplex64:
|
|
123 case Tcomplex80:
|
|
124 e1 = e1.castTo(sc, e2.type);
|
|
125 break;
|
|
126 default:
|
|
127 break;
|
|
128 }
|
|
129 if (type.toBasetype().ty == Tarray)
|
|
130 {
|
|
131 e1 = e1.castTo(sc, type);
|
|
132 e2 = e2.castTo(sc, type);
|
|
133 }
|
|
134 }
|
|
135 static if (false) {
|
|
136 printf("res: %s\n", type.toChars());
|
|
137 printf("e1 : %s\n", e1.type.toChars());
|
|
138 printf("e2 : %s\n", e2.type.toChars());
|
|
139 }
|
|
140 return this;
|
|
141 }
|
|
142
|
72
|
143 override Expression optimize(int result)
|
0
|
144 {
|
|
145 Expression e;
|
|
146
|
|
147 econd = econd.optimize(WANTflags | (result & WANTinterpret));
|
|
148 if (econd.isBool(true))
|
|
149 e = e1.optimize(result);
|
|
150 else if (econd.isBool(false))
|
|
151 e = e2.optimize(result);
|
|
152 else
|
|
153 {
|
|
154 e1 = e1.optimize(result);
|
|
155 e2 = e2.optimize(result);
|
|
156 e = this;
|
|
157 }
|
|
158
|
|
159 return e;
|
|
160 }
|
|
161
|
72
|
162 override Expression interpret(InterState istate)
|
0
|
163 {
|
|
164 assert(false);
|
|
165 }
|
|
166
|
72
|
167 override void checkEscape()
|
0
|
168 {
|
|
169 e1.checkEscape();
|
|
170 e2.checkEscape();
|
|
171 }
|
|
172
|
72
|
173 override int isLvalue()
|
0
|
174 {
|
123
|
175 return e1.isLvalue() && e2.isLvalue();
|
0
|
176 }
|
|
177
|
123
|
178 override Expression toLvalue(Scope sc, Expression ex)
|
0
|
179 {
|
123
|
180 PtrExp e;
|
|
181
|
|
182 // convert (econd ? e1 : e2) to *(econd ? &e1 : &e2)
|
|
183 e = new PtrExp(loc, this, type);
|
|
184
|
|
185 e1 = e1.addressOf(sc);
|
|
186 //e1 = e1.toLvalue(sc, null);
|
|
187
|
|
188 e2 = e2.addressOf(sc);
|
|
189 //e2 = e2.toLvalue(sc, null);
|
|
190
|
|
191 typeCombine(sc);
|
|
192
|
|
193 type = e2.type;
|
|
194 return e;
|
0
|
195 }
|
|
196
|
72
|
197 override Expression modifiableLvalue(Scope sc, Expression e)
|
0
|
198 {
|
123
|
199 error("conditional expression %s is not a modifiable lvalue", toChars());
|
|
200 return this;
|
0
|
201 }
|
|
202
|
72
|
203 override Expression checkToBoolean()
|
0
|
204 {
|
123
|
205 e1 = e1.checkToBoolean();
|
|
206 e2 = e2.checkToBoolean();
|
|
207 return this;
|
0
|
208 }
|
|
209
|
72
|
210 override bool checkSideEffect(int flag)
|
0
|
211 {
|
64
|
212 if (flag == 2)
|
|
213 {
|
|
214 return econd.checkSideEffect(2) || e1.checkSideEffect(2) || e2.checkSideEffect(2);
|
|
215 }
|
|
216 else
|
|
217 {
|
|
218 econd.checkSideEffect(1);
|
|
219 e1.checkSideEffect(flag);
|
|
220 return e2.checkSideEffect(flag);
|
|
221 }
|
0
|
222 }
|
|
223
|
72
|
224 override void toCBuffer(OutBuffer buf, HdrGenState* hgs)
|
0
|
225 {
|
50
|
226 expToCBuffer(buf, hgs, econd, PREC_oror);
|
|
227 buf.writestring(" ? ");
|
|
228 expToCBuffer(buf, hgs, e1, PREC_expr);
|
|
229 buf.writestring(" : ");
|
|
230 expToCBuffer(buf, hgs, e2, PREC_cond);
|
0
|
231 }
|
|
232
|
72
|
233 override MATCH implicitConvTo(Type t)
|
0
|
234 {
|
|
235 MATCH m1 = e1.implicitConvTo(t);
|
|
236 MATCH m2 = e2.implicitConvTo(t);
|
|
237 //printf("CondExp: m1 %d m2 %d\n", m1, m2);
|
|
238
|
|
239 // Pick the worst match
|
|
240 return (m1 < m2) ? m1 : m2;
|
|
241 }
|
|
242
|
72
|
243 override Expression castTo(Scope sc, Type t)
|
0
|
244 {
|
|
245 Expression e = this;
|
|
246
|
|
247 if (type !is t)
|
|
248 {
|
|
249 if (1 || e1.op == TOKstring || e2.op == TOKstring)
|
|
250 {
|
|
251 e = new CondExp(loc, econd, e1.castTo(sc, t), e2.castTo(sc, t));
|
|
252 e.type = t;
|
|
253 }
|
|
254 else
|
|
255 e = Expression.castTo(sc, t);
|
|
256 }
|
|
257 return e;
|
|
258 }
|
|
259
|
72
|
260 override void scanForNestedRef(Scope sc)
|
0
|
261 {
|
|
262 assert(false);
|
|
263 }
|
|
264
|
72
|
265 override bool canThrow()
|
0
|
266 {
|
|
267 return econd.canThrow() || e1.canThrow() || e2.canThrow();
|
|
268 }
|
|
269
|
72
|
270 override int inlineCost(InlineCostState* ics)
|
0
|
271 {
|
|
272 return 1 + e1.inlineCost(ics) + e2.inlineCost(ics) + econd.inlineCost(ics);
|
|
273 }
|
|
274
|
72
|
275 override Expression doInline(InlineDoState ids)
|
0
|
276 {
|
|
277 CondExp ce = cast(CondExp)copy();
|
|
278
|
|
279 ce.econd = econd.doInline(ids);
|
|
280 ce.e1 = e1.doInline(ids);
|
|
281 ce.e2 = e2.doInline(ids);
|
|
282 return ce;
|
|
283 }
|
|
284
|
72
|
285 override Expression inlineScan(InlineScanState* iss)
|
0
|
286 {
|
|
287 econd = econd.inlineScan(iss);
|
|
288 e1 = e1.inlineScan(iss);
|
|
289 e2 = e2.inlineScan(iss);
|
|
290 return this;
|
|
291 }
|
|
292
|
72
|
293 override elem* toElem(IRState* irs)
|
0
|
294 {
|
|
295 elem* eleft;
|
|
296 elem* eright;
|
|
297
|
|
298 elem* ec = econd.toElem(irs);
|
|
299
|
|
300 eleft = e1.toElem(irs);
|
|
301 tym_t ty = eleft.Ety;
|
|
302 if (global.params.cov && e1.loc.linnum)
|
|
303 eleft = el_combine(incUsageElem(irs, e1.loc), eleft);
|
|
304
|
|
305 eright = e2.toElem(irs);
|
|
306 if (global.params.cov && e2.loc.linnum)
|
|
307 eright = el_combine(incUsageElem(irs, e2.loc), eright);
|
|
308
|
|
309 elem* e = el_bin(OPcond, ty, ec, el_bin(OPcolon, ty, eleft, eright));
|
|
310 if (tybasic(ty) == TYstruct)
|
|
311 e.Enumbytes = cast(uint)e1.type.size();
|
|
312
|
|
313 el_setLoc(e, loc);
|
|
314 return e;
|
|
315 }
|
72
|
316 }
|