]>
git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/d/dmd/staticcond.c
2 /* Compiler implementation of the D programming language
3 * Copyright (C) 1999-2021 by The D Language Foundation, All Rights Reserved
4 * written by Walter Bright
5 * http://www.digitalmars.com
6 * Distributed under the Boost Software License, Version 1.0.
7 * http://www.boost.org/LICENSE_1_0.txt
8 * https://github.com/D-Programming-Language/dmd/blob/master/src/staticcond.c
12 #include "expression.h"
16 /********************************************
17 * Semantically analyze and then evaluate a static condition at compile time.
18 * This is special because short circuit operators &&, || and ?: at the top
19 * level are not semantically analyzed if the result of the expression is not
22 * exp = original expression, for error messages
24 * true if evaluates to true
27 bool evalStaticCondition(Scope
*sc
, Expression
*exp
, Expression
*e
, bool &errors
)
29 if (e
->op
== TOKandand
|| e
->op
== TOKoror
)
31 LogicalExp
*aae
= (LogicalExp
*)e
;
32 bool result
= evalStaticCondition(sc
, exp
, aae
->e1
, errors
);
35 if (e
->op
== TOKandand
)
45 result
= evalStaticCondition(sc
, exp
, aae
->e2
, errors
);
46 return !errors
&& result
;
49 if (e
->op
== TOKquestion
)
51 CondExp
*ce
= (CondExp
*)e
;
52 bool result
= evalStaticCondition(sc
, exp
, ce
->econd
, errors
);
55 Expression
*leg
= result
? ce
->e1
: ce
->e2
;
56 result
= evalStaticCondition(sc
, exp
, leg
, errors
);
57 return !errors
&& result
;
60 unsigned nerrors
= global
.errors
;
63 sc
->flags
|= SCOPEcondition
;
65 e
= expressionSemantic(e
, sc
);
66 e
= resolveProperties(sc
, e
);
69 e
= e
->optimize(WANTvalue
);
71 if (nerrors
!= global
.errors
||
73 e
->type
->toBasetype() == Type::terror
)
79 if (!e
->type
->isBoolean())
81 exp
->error("expression %s of type %s does not have a boolean value", exp
->toChars(), e
->type
->toChars());
86 e
= e
->ctfeInterpret();
90 else if (e
->isBool(false))
93 e
->error("expression %s is not constant", e
->toChars());