Skip to content

Commit 919ac41

Browse files
committed
[NFC, Scoped Enum] Convert Sema::ExpressionEvaluationContext into a scoped Enum
- also replace direct equality checks against the ConstantEvaluated enumerator with isConstantEvaluted(), in anticipation of adding finer granularity to the various ConstantEvaluated contexts and reinstating certain restrictions on where lambda expressions can occur in C++17. - update the clang tablegen backend that uses these Enumerators, and add the relevant scope where needed. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@299316 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent d11ae8a commit 919ac41

25 files changed

+257
-181
lines changed

include/clang/Sema/Sema.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,8 @@ class Sema {
681681
: S(S), SavedContext(S, DC)
682682
{
683683
S.PushFunctionScope();
684-
S.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
684+
S.PushExpressionEvaluationContext(
685+
Sema::ExpressionEvaluationContext::PotentiallyEvaluated);
685686
}
686687

687688
~SynthesizedFunctionScope() {
@@ -802,7 +803,7 @@ class Sema {
802803

803804
/// \brief Describes how the expressions currently being parsed are
804805
/// evaluated at run-time, if at all.
805-
enum ExpressionEvaluationContext {
806+
enum class ExpressionEvaluationContext {
806807
/// \brief The current expression and its subexpressions occur within an
807808
/// unevaluated operand (C++11 [expr]p7), such as the subexpression of
808809
/// \c sizeof, where the type of the expression may be significant but
@@ -908,8 +909,12 @@ class Sema {
908909
MangleNumberingContext &getMangleNumberingContext(ASTContext &Ctx);
909910

910911
bool isUnevaluated() const {
911-
return Context == Unevaluated || Context == UnevaluatedAbstract ||
912-
Context == UnevaluatedList;
912+
return Context == ExpressionEvaluationContext::Unevaluated ||
913+
Context == ExpressionEvaluationContext::UnevaluatedAbstract ||
914+
Context == ExpressionEvaluationContext::UnevaluatedList;
915+
}
916+
bool isConstantEvaluated() const {
917+
return Context == ExpressionEvaluationContext::ConstantEvaluated;
913918
}
914919
};
915920

@@ -10315,6 +10320,7 @@ class EnterExpressionEvaluationContext {
1031510320
bool Entered = true;
1031610321

1031710322
public:
10323+
1031810324
EnterExpressionEvaluationContext(Sema &Actions,
1031910325
Sema::ExpressionEvaluationContext NewContext,
1032010326
Decl *LambdaContextDecl = nullptr,
@@ -10345,8 +10351,8 @@ class EnterExpressionEvaluationContext {
1034510351
// a context.
1034610352
if (ShouldEnter && Actions.isUnevaluatedContext() &&
1034710353
Actions.getLangOpts().CPlusPlus11) {
10348-
Actions.PushExpressionEvaluationContext(Sema::UnevaluatedList, nullptr,
10349-
false);
10354+
Actions.PushExpressionEvaluationContext(
10355+
Sema::ExpressionEvaluationContext::UnevaluatedList, nullptr, false);
1035010356
Entered = true;
1035110357
}
1035210358
}

lib/Parse/ParseCXXInlineMethods.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,9 @@ void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
344344

345345
// The argument isn't actually potentially evaluated unless it is
346346
// used.
347-
EnterExpressionEvaluationContext Eval(Actions,
348-
Sema::PotentiallyEvaluatedIfUsed,
349-
Param);
347+
EnterExpressionEvaluationContext Eval(
348+
Actions,
349+
Sema::ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed, Param);
350350

351351
ExprResult DefArgResult;
352352
if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {

lib/Parse/ParseDecl.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,9 @@ unsigned Parser::ParseAttributeArgsCommon(
308308
do {
309309
bool Uneval = attributeParsedArgsUnevaluated(*AttrName);
310310
EnterExpressionEvaluationContext Unevaluated(
311-
Actions, Uneval ? Sema::Unevaluated : Sema::ConstantEvaluated,
311+
Actions,
312+
Uneval ? Sema::ExpressionEvaluationContext::Unevaluated
313+
: Sema::ExpressionEvaluationContext::ConstantEvaluated,
312314
/*LambdaContextDecl=*/nullptr,
313315
/*IsDecltype=*/false);
314316

@@ -4093,8 +4095,8 @@ void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
40934095
// anything that's a simple-type-specifier followed by '(' as an
40944096
// expression. This suffices because function types are not valid
40954097
// underlying types anyway.
4096-
EnterExpressionEvaluationContext Unevaluated(Actions,
4097-
Sema::ConstantEvaluated);
4098+
EnterExpressionEvaluationContext Unevaluated(
4099+
Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated);
40984100
TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
40994101
// If the next token starts an expression, we know we're parsing a
41004102
// bit-field. This is the common case.
@@ -6270,9 +6272,10 @@ void Parser::ParseParameterDeclarationClause(
62706272

62716273
// The argument isn't actually potentially evaluated unless it is
62726274
// used.
6273-
EnterExpressionEvaluationContext Eval(Actions,
6274-
Sema::PotentiallyEvaluatedIfUsed,
6275-
Param);
6275+
EnterExpressionEvaluationContext Eval(
6276+
Actions,
6277+
Sema::ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed,
6278+
Param);
62766279

62776280
ExprResult DefArgResult;
62786281
if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
@@ -6422,8 +6425,8 @@ void Parser::ParseBracketDeclarator(Declarator &D) {
64226425
if (getLangOpts().CPlusPlus) {
64236426
NumElements = ParseConstantExpression();
64246427
} else {
6425-
EnterExpressionEvaluationContext Unevaluated(Actions,
6426-
Sema::ConstantEvaluated);
6428+
EnterExpressionEvaluationContext Unevaluated(
6429+
Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated);
64276430
NumElements =
64286431
Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
64296432
}
@@ -6558,8 +6561,9 @@ void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
65586561

65596562
const bool hasParens = Tok.is(tok::l_paren);
65606563

6561-
EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
6562-
Sema::ReuseLambdaContextDecl);
6564+
EnterExpressionEvaluationContext Unevaluated(
6565+
Actions, Sema::ExpressionEvaluationContext::Unevaluated,
6566+
Sema::ReuseLambdaContextDecl);
65636567

65646568
bool isCastExpr;
65656569
ParsedType CastTy;

lib/Parse/ParseDeclCXX.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -935,8 +935,9 @@ SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
935935

936936
// C++11 [dcl.type.simple]p4:
937937
// The operand of the decltype specifier is an unevaluated operand.
938-
EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
939-
nullptr,/*IsDecltype=*/true);
938+
EnterExpressionEvaluationContext Unevaluated(
939+
Actions, Sema::ExpressionEvaluationContext::Unevaluated, nullptr,
940+
/*IsDecltype=*/true);
940941
Result =
941942
Actions.CorrectDelayedTyposInExpr(ParseExpression(), [](Expr *E) {
942943
return E->hasPlaceholderType() ? ExprError() : E;
@@ -2897,9 +2898,8 @@ ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
28972898
assert(Tok.isOneOf(tok::equal, tok::l_brace)
28982899
&& "Data member initializer not starting with '=' or '{'");
28992900

2900-
EnterExpressionEvaluationContext Context(Actions,
2901-
Sema::PotentiallyEvaluated,
2902-
D);
2901+
EnterExpressionEvaluationContext Context(
2902+
Actions, Sema::ExpressionEvaluationContext::PotentiallyEvaluated, D);
29032903
if (TryConsumeToken(tok::equal, EqualLoc)) {
29042904
if (Tok.is(tok::kw_delete)) {
29052905
// In principle, an initializer of '= delete p;' is legal, but it will

lib/Parse/ParseExpr.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ ExprResult Parser::ParseConstantExpression(TypeCastState isTypeCast) {
198198
// An expression is potentially evaluated unless it appears where an
199199
// integral constant expression is required (see 5.19) [...].
200200
// C++98 and C++11 have no such rule, but this is only a defect in C++98.
201-
EnterExpressionEvaluationContext ConstantEvaluated(Actions,
202-
Sema::ConstantEvaluated);
201+
EnterExpressionEvaluationContext ConstantEvaluated(
202+
Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated);
203203

204204
ExprResult LHS(ParseCastExpression(false, false, isTypeCast));
205205
ExprResult Res(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
@@ -1313,7 +1313,8 @@ ExprResult Parser::ParseCastExpression(bool isUnaryExpression,
13131313
// C++11 [expr.unary.noexcept]p1:
13141314
// The noexcept operator determines whether the evaluation of its operand,
13151315
// which is an unevaluated operand, can throw an exception.
1316-
EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
1316+
EnterExpressionEvaluationContext Unevaluated(
1317+
Actions, Sema::ExpressionEvaluationContext::Unevaluated);
13171318
ExprResult Result = ParseExpression();
13181319

13191320
T.consumeClose();
@@ -1882,9 +1883,10 @@ ExprResult Parser::ParseUnaryExprOrTypeTraitExpression() {
18821883

18831884
if (!Name)
18841885
return ExprError();
1885-
1886-
EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
1887-
Sema::ReuseLambdaContextDecl);
1886+
1887+
EnterExpressionEvaluationContext Unevaluated(
1888+
Actions, Sema::ExpressionEvaluationContext::Unevaluated,
1889+
Sema::ReuseLambdaContextDecl);
18881890

18891891
return Actions.ActOnSizeofParameterPackExpr(getCurScope(),
18901892
OpTok.getLocation(),
@@ -1895,8 +1897,9 @@ ExprResult Parser::ParseUnaryExprOrTypeTraitExpression() {
18951897
if (OpTok.isOneOf(tok::kw_alignof, tok::kw__Alignof))
18961898
Diag(OpTok, diag::warn_cxx98_compat_alignof);
18971899

1898-
EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
1899-
Sema::ReuseLambdaContextDecl);
1900+
EnterExpressionEvaluationContext Unevaluated(
1901+
Actions, Sema::ExpressionEvaluationContext::Unevaluated,
1902+
Sema::ReuseLambdaContextDecl);
19001903

19011904
bool isCastExpr;
19021905
ParsedType CastTy;
@@ -2569,7 +2572,8 @@ ExprResult Parser::ParseGenericSelectionExpression() {
25692572
{
25702573
// C11 6.5.1.1p3 "The controlling expression of a generic selection is
25712574
// not evaluated."
2572-
EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
2575+
EnterExpressionEvaluationContext Unevaluated(
2576+
Actions, Sema::ExpressionEvaluationContext::Unevaluated);
25732577
ControllingExpr =
25742578
Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
25752579
if (ControllingExpr.isInvalid()) {

lib/Parse/ParseExprCXX.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -866,8 +866,8 @@ Optional<unsigned> Parser::ParseLambdaIntroducer(LambdaIntroducer &Intro,
866866
// Each lambda init-capture forms its own full expression, which clears
867867
// Actions.MaybeODRUseExprs. So create an expression evaluation context
868868
// to save the necessary state, and restore it later.
869-
EnterExpressionEvaluationContext EC(Actions,
870-
Sema::PotentiallyEvaluated);
869+
EnterExpressionEvaluationContext EC(
870+
Actions, Sema::ExpressionEvaluationContext::PotentiallyEvaluated);
871871

872872
if (TryConsumeToken(tok::equal))
873873
InitKind = LambdaCaptureInitKind::CopyInit;
@@ -1405,8 +1405,9 @@ ExprResult Parser::ParseCXXTypeid() {
14051405
// We enter the unevaluated context before trying to determine whether we
14061406
// have a type-id, because the tentative parse logic will try to resolve
14071407
// names, and must treat them as unevaluated.
1408-
EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
1409-
Sema::ReuseLambdaContextDecl);
1408+
EnterExpressionEvaluationContext Unevaluated(
1409+
Actions, Sema::ExpressionEvaluationContext::Unevaluated,
1410+
Sema::ReuseLambdaContextDecl);
14101411

14111412
if (isTypeIdInParens()) {
14121413
TypeResult Ty = ParseTypeName();
@@ -1469,7 +1470,8 @@ ExprResult Parser::ParseCXXUuidof() {
14691470
Ty.get().getAsOpaquePtr(),
14701471
T.getCloseLocation());
14711472
} else {
1472-
EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
1473+
EnterExpressionEvaluationContext Unevaluated(
1474+
Actions, Sema::ExpressionEvaluationContext::Unevaluated);
14731475
Result = ParseExpression();
14741476

14751477
// Match the ')'.

lib/Parse/ParseStmt.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,7 +1179,8 @@ StmtResult Parser::ParseIfStatement(SourceLocation *TrailingElseLoc) {
11791179
StmtResult ThenStmt;
11801180
{
11811181
EnterExpressionEvaluationContext PotentiallyDiscarded(
1182-
Actions, Sema::DiscardedStatement, nullptr, false,
1182+
Actions, Sema::ExpressionEvaluationContext::DiscardedStatement, nullptr,
1183+
false,
11831184
/*ShouldEnter=*/ConstexprCondition && !*ConstexprCondition);
11841185
ThenStmt = ParseStatement(&InnerStatementTrailingElseLoc);
11851186
}
@@ -1212,7 +1213,8 @@ StmtResult Parser::ParseIfStatement(SourceLocation *TrailingElseLoc) {
12121213
Tok.is(tok::l_brace));
12131214

12141215
EnterExpressionEvaluationContext PotentiallyDiscarded(
1215-
Actions, Sema::DiscardedStatement, nullptr, false,
1216+
Actions, Sema::ExpressionEvaluationContext::DiscardedStatement, nullptr,
1217+
false,
12161218
/*ShouldEnter=*/ConstexprCondition && *ConstexprCondition);
12171219
ElseStmt = ParseStatement();
12181220

lib/Parse/ParseTemplate.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -701,8 +701,8 @@ Parser::ParseNonTypeTemplateParameter(unsigned Depth, unsigned Position) {
701701
// end of the template-parameter-list rather than a greater-than
702702
// operator.
703703
GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
704-
EnterExpressionEvaluationContext ConstantEvaluated(Actions,
705-
Sema::ConstantEvaluated);
704+
EnterExpressionEvaluationContext ConstantEvaluated(
705+
Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated);
706706

707707
DefaultArg = Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
708708
if (DefaultArg.isInvalid())
@@ -1275,7 +1275,8 @@ bool Parser::IsTemplateArgumentList(unsigned Skip) {
12751275
bool
12761276
Parser::ParseTemplateArgumentList(TemplateArgList &TemplateArgs) {
12771277
// Template argument lists are constant-evaluation contexts.
1278-
EnterExpressionEvaluationContext EvalContext(Actions,Sema::ConstantEvaluated);
1278+
EnterExpressionEvaluationContext EvalContext(
1279+
Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated);
12791280
ColonProtectionRAIIObject ColonProtection(*this, false);
12801281

12811282
do {

lib/Sema/Sema.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,9 @@ Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer,
122122
// Tell diagnostics how to render things from the AST library.
123123
Diags.SetArgToStringFn(&FormatASTNodeDiagnosticArgument, &Context);
124124

125-
ExprEvalContexts.emplace_back(PotentiallyEvaluated, 0, CleanupInfo{}, nullptr,
126-
false);
125+
ExprEvalContexts.emplace_back(
126+
ExpressionEvaluationContext::PotentiallyEvaluated, 0, CleanupInfo{},
127+
nullptr, false);
127128

128129
FunctionScopes.push_back(new FunctionScopeInfo(Diags));
129130

lib/Sema/SemaDecl.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10850,7 +10850,8 @@ void Sema::CheckCompleteVariableDeclaration(VarDecl *var) {
1085010850
// Regardless, we don't want to ignore array nesting when
1085110851
// constructing this copy.
1085210852
if (type->isStructureOrClassType()) {
10853-
EnterExpressionEvaluationContext scope(*this, PotentiallyEvaluated);
10853+
EnterExpressionEvaluationContext scope(
10854+
*this, ExpressionEvaluationContext::PotentiallyEvaluated);
1085410855
SourceLocation poi = var->getLocation();
1085510856
Expr *varRef =new (Context) DeclRefExpr(var, false, type, VK_LValue, poi);
1085610857
ExprResult result

0 commit comments

Comments
 (0)