1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
|
/**
* Lazily evaluate static conditions for `static if`, `static assert` and template constraints.
*
* Copyright: Copyright (C) 1999-2022 by The D Language Foundation, All Rights Reserved
* Authors: $(LINK2 https://www.digitalmars.com, Walter Bright)
* License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/staticcond.d, _staticcond.d)
* Documentation: https://dlang.org/phobos/dmd_staticcond.html
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/staticcond.d
*/
module dmd.staticcond;
import dmd.arraytypes;
import dmd.dmodule;
import dmd.dscope;
import dmd.dsymbol;
import dmd.errors;
import dmd.expression;
import dmd.expressionsem;
import dmd.globals;
import dmd.identifier;
import dmd.mtype;
import dmd.root.array;
import dmd.common.outbuffer;
import dmd.tokens;
/********************************************
* Semantically analyze and then evaluate a static condition at compile time.
* This is special because short circuit operators &&, || and ?: at the top
* level are not semantically analyzed if the result of the expression is not
* necessary.
* Params:
* sc = instantiating scope
* original = original expression, for error messages
* e = resulting expression
* errors = set to `true` if errors occurred
* negatives = array to store negative clauses
* Returns:
* true if evaluates to true
*/
bool evalStaticCondition(Scope* sc, Expression original, Expression e, out bool errors, Expressions* negatives = null)
{
if (negatives)
negatives.setDim(0);
bool impl(Expression e)
{
if (e.isNotExp())
{
NotExp ne = cast(NotExp)e;
return !impl(ne.e1);
}
if (e.op == EXP.andAnd || e.op == EXP.orOr)
{
LogicalExp aae = cast(LogicalExp)e;
bool result = impl(aae.e1);
if (errors)
return false;
if (e.op == EXP.andAnd)
{
if (!result)
return false;
}
else
{
if (result)
return true;
}
result = impl(aae.e2);
return !errors && result;
}
if (e.op == EXP.question)
{
CondExp ce = cast(CondExp)e;
bool result = impl(ce.econd);
if (errors)
return false;
Expression leg = result ? ce.e1 : ce.e2;
result = impl(leg);
return !errors && result;
}
Expression before = e;
const uint nerrors = global.errors;
sc = sc.startCTFE();
sc.flags |= SCOPE.condition;
e = e.expressionSemantic(sc);
e = resolveProperties(sc, e);
e = e.toBoolean(sc);
sc = sc.endCTFE();
e = e.optimize(WANTvalue);
if (nerrors != global.errors ||
e.isErrorExp() ||
e.type.toBasetype() == Type.terror)
{
errors = true;
return false;
}
e = e.ctfeInterpret();
const opt = e.toBool();
if (opt.isEmpty())
{
e.error("expression `%s` is not constant", e.toChars());
errors = true;
return false;
}
if (negatives && !opt.get())
negatives.push(before);
return opt.get();
}
return impl(e);
}
/********************************************
* Format a static condition as a tree-like structure, marking failed and
* bypassed expressions.
* Params:
* original = original expression
* instantiated = instantiated expression
* negatives = array with negative clauses from `instantiated` expression
* full = controls whether it shows the full output or only failed parts
* itemCount = returns the number of written clauses
* Returns:
* formatted string or `null` if the expressions were `null`, or if the
* instantiated expression is not based on the original one
*/
const(char)* visualizeStaticCondition(Expression original, Expression instantiated,
const Expression[] negatives, bool full, ref uint itemCount)
{
if (!original || !instantiated || original.loc !is instantiated.loc)
return null;
OutBuffer buf;
if (full)
itemCount = visualizeFull(original, instantiated, negatives, buf);
else
itemCount = visualizeShort(original, instantiated, negatives, buf);
return buf.extractChars();
}
private uint visualizeFull(Expression original, Expression instantiated,
const Expression[] negatives, ref OutBuffer buf)
{
// tree-like structure; traverse and format simultaneously
uint count;
uint indent;
static void printOr(uint indent, ref OutBuffer buf)
{
buf.reserve(indent * 4 + 8);
foreach (i; 0 .. indent)
buf.writestring(" ");
buf.writestring(" or:\n");
}
// returns true if satisfied
bool impl(Expression orig, Expression e, bool inverted, bool orOperand, bool unreached)
{
EXP op = orig.op;
// lower all 'not' to the bottom
// !(A && B) -> !A || !B
// !(A || B) -> !A && !B
if (inverted)
{
if (op == EXP.andAnd)
op = EXP.orOr;
else if (op == EXP.orOr)
op = EXP.andAnd;
}
if (op == EXP.not)
{
NotExp no = cast(NotExp)orig;
NotExp ne = cast(NotExp)e;
assert(ne);
return impl(no.e1, ne.e1, !inverted, orOperand, unreached);
}
else if (op == EXP.andAnd)
{
BinExp bo = cast(BinExp)orig;
BinExp be = cast(BinExp)e;
assert(be);
const r1 = impl(bo.e1, be.e1, inverted, false, unreached);
const r2 = impl(bo.e2, be.e2, inverted, false, unreached || !r1);
return r1 && r2;
}
else if (op == EXP.orOr)
{
if (!orOperand) // do not indent A || B || C twice
indent++;
BinExp bo = cast(BinExp)orig;
BinExp be = cast(BinExp)e;
assert(be);
const r1 = impl(bo.e1, be.e1, inverted, true, unreached);
printOr(indent, buf);
const r2 = impl(bo.e2, be.e2, inverted, true, unreached);
if (!orOperand)
indent--;
return r1 || r2;
}
else if (op == EXP.question)
{
CondExp co = cast(CondExp)orig;
CondExp ce = cast(CondExp)e;
assert(ce);
if (!inverted)
{
// rewrite (A ? B : C) as (A && B || !A && C)
if (!orOperand)
indent++;
const r1 = impl(co.econd, ce.econd, inverted, false, unreached);
const r2 = impl(co.e1, ce.e1, inverted, false, unreached || !r1);
printOr(indent, buf);
const r3 = impl(co.econd, ce.econd, !inverted, false, unreached);
const r4 = impl(co.e2, ce.e2, inverted, false, unreached || !r3);
if (!orOperand)
indent--;
return r1 && r2 || r3 && r4;
}
else
{
// rewrite !(A ? B : C) as (!A || !B) && (A || !C)
if (!orOperand)
indent++;
const r1 = impl(co.econd, ce.econd, inverted, false, unreached);
printOr(indent, buf);
const r2 = impl(co.e1, ce.e1, inverted, false, unreached);
const r12 = r1 || r2;
const r3 = impl(co.econd, ce.econd, !inverted, false, unreached || !r12);
printOr(indent, buf);
const r4 = impl(co.e2, ce.e2, inverted, false, unreached || !r12);
if (!orOperand)
indent--;
return (r1 || r2) && (r3 || r4);
}
}
else // 'primitive' expression
{
buf.reserve(indent * 4 + 4);
foreach (i; 0 .. indent)
buf.writestring(" ");
// find its value; it may be not computed, if there was a short circuit,
// but we handle this case with `unreached` flag
bool value = true;
if (!unreached)
{
foreach (fe; negatives)
{
if (fe is e)
{
value = false;
break;
}
}
}
// write the marks first
const satisfied = inverted ? !value : value;
if (!satisfied && !unreached)
buf.writestring(" > ");
else if (unreached)
buf.writestring(" - ");
else
buf.writestring(" ");
// then the expression itself
if (inverted)
buf.writeByte('!');
buf.writestring(orig.toChars);
buf.writenl();
count++;
return satisfied;
}
}
impl(original, instantiated, false, true, false);
return count;
}
private uint visualizeShort(Expression original, Expression instantiated,
const Expression[] negatives, ref OutBuffer buf)
{
// simple list; somewhat similar to long version, so no comments
// one difference is that it needs to hold items to display in a stack
static struct Item
{
Expression orig;
bool inverted;
}
Array!Item stack;
bool impl(Expression orig, Expression e, bool inverted)
{
EXP op = orig.op;
if (inverted)
{
if (op == EXP.andAnd)
op = EXP.orOr;
else if (op == EXP.orOr)
op = EXP.andAnd;
}
if (op == EXP.not)
{
NotExp no = cast(NotExp)orig;
NotExp ne = cast(NotExp)e;
assert(ne);
return impl(no.e1, ne.e1, !inverted);
}
else if (op == EXP.andAnd)
{
BinExp bo = cast(BinExp)orig;
BinExp be = cast(BinExp)e;
assert(be);
bool r = impl(bo.e1, be.e1, inverted);
r = r && impl(bo.e2, be.e2, inverted);
return r;
}
else if (op == EXP.orOr)
{
BinExp bo = cast(BinExp)orig;
BinExp be = cast(BinExp)e;
assert(be);
const lbefore = stack.length;
bool r = impl(bo.e1, be.e1, inverted);
r = r || impl(bo.e2, be.e2, inverted);
if (r)
stack.setDim(lbefore); // purge added positive items
return r;
}
else if (op == EXP.question)
{
CondExp co = cast(CondExp)orig;
CondExp ce = cast(CondExp)e;
assert(ce);
if (!inverted)
{
const lbefore = stack.length;
bool a = impl(co.econd, ce.econd, inverted);
a = a && impl(co.e1, ce.e1, inverted);
bool b;
if (!a)
{
b = impl(co.econd, ce.econd, !inverted);
b = b && impl(co.e2, ce.e2, inverted);
}
const r = a || b;
if (r)
stack.setDim(lbefore);
return r;
}
else
{
bool a;
{
const lbefore = stack.length;
a = impl(co.econd, ce.econd, inverted);
a = a || impl(co.e1, ce.e1, inverted);
if (a)
stack.setDim(lbefore);
}
bool b;
if (a)
{
const lbefore = stack.length;
b = impl(co.econd, ce.econd, !inverted);
b = b || impl(co.e2, ce.e2, inverted);
if (b)
stack.setDim(lbefore);
}
return a && b;
}
}
else // 'primitive' expression
{
bool value = true;
foreach (fe; negatives)
{
if (fe is e)
{
value = false;
break;
}
}
const satisfied = inverted ? !value : value;
if (!satisfied)
stack.push(Item(orig, inverted));
return satisfied;
}
}
impl(original, instantiated, false);
foreach (i; 0 .. stack.length)
{
// write the expression only
buf.writestring(" ");
if (stack[i].inverted)
buf.writeByte('!');
buf.writestring(stack[i].orig.toChars);
// here with no trailing newline
if (i + 1 < stack.length)
buf.writenl();
}
return cast(uint)stack.length;
}
|