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