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