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