Skip to content

Commit e8c684a

Browse files
committed
[clang][NFC] Convert Parser::ParenParseOption to scoped enum
1 parent 830cf36 commit e8c684a

File tree

4 files changed

+48
-43
lines changed

4 files changed

+48
-43
lines changed

clang/include/clang/Parse/Parser.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ enum class TypeCastState { NotTypeCast = 0, MaybeTypeCast, IsTypeCast };
107107
/// Control what ParseCastExpression will parse.
108108
enum class CastParseKind { AnyCastExpr = 0, UnaryExprOnly, PrimaryExprOnly };
109109

110+
/// ParenParseOption - Control what ParseParenExpression will parse.
111+
enum class ParenParseOption {
112+
SimpleExpr, // Only parse '(' expression ')'
113+
FoldExpr, // Also allow fold-expression <anything>
114+
CompoundStmt, // Also allow '(' compound-statement ')'
115+
CompoundLiteral, // Also allow '(' type-name ')' '{' ... '}'
116+
CastExpr // Also allow '(' type-name ')' <anything>
117+
};
118+
110119
/// Parser - This implements a parser for the C family of languages. After
111120
/// parsing units of the grammar, productions are invoked to handle whatever has
112121
/// been read.
@@ -1951,14 +1960,6 @@ class Parser : public CodeCompletionHandler {
19511960
/// used for misc language extensions.
19521961
bool ParseSimpleExpressionList(SmallVectorImpl<Expr *> &Exprs);
19531962

1954-
/// ParenParseOption - Control what ParseParenExpression will parse.
1955-
enum ParenParseOption {
1956-
SimpleExpr, // Only parse '(' expression ')'
1957-
FoldExpr, // Also allow fold-expression <anything>
1958-
CompoundStmt, // Also allow '(' compound-statement ')'
1959-
CompoundLiteral, // Also allow '(' type-name ')' '{' ... '}'
1960-
CastExpr // Also allow '(' type-name ')' <anything>
1961-
};
19621963
ExprResult ParseParenExpression(ParenParseOption &ExprType,
19631964
bool stopIfCastExpr,
19641965
bool isTypeCast,

clang/lib/Parse/ParseExpr.cpp

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,7 +1105,7 @@ ExprResult Parser::ParseCastExpression(CastParseKind ParseKind,
11051105
ParenExprType = ParenParseOption::CastExpr;
11061106
break;
11071107
case CastParseKind::PrimaryExprOnly:
1108-
ParenExprType = FoldExpr;
1108+
ParenExprType = ParenParseOption::FoldExpr;
11091109
break;
11101110
}
11111111
ParsedType CastTy;
@@ -1121,17 +1121,19 @@ ExprResult Parser::ParseCastExpression(CastParseKind ParseKind,
11211121
return Res;
11221122

11231123
switch (ParenExprType) {
1124-
case SimpleExpr: break; // Nothing else to do.
1125-
case CompoundStmt: break; // Nothing else to do.
1126-
case CompoundLiteral:
1124+
case ParenParseOption::SimpleExpr:
1125+
break; // Nothing else to do.
1126+
case ParenParseOption::CompoundStmt:
1127+
break; // Nothing else to do.
1128+
case ParenParseOption::CompoundLiteral:
11271129
// We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
11281130
// postfix-expression exist, parse them now.
11291131
break;
1130-
case CastExpr:
1132+
case ParenParseOption::CastExpr:
11311133
// We have parsed the cast-expression and no postfix-expr pieces are
11321134
// following.
11331135
return Res;
1134-
case FoldExpr:
1136+
case ParenParseOption::FoldExpr:
11351137
// We only parsed a fold-expression. There might be postfix-expr pieces
11361138
// afterwards; parse them now.
11371139
break;
@@ -2528,7 +2530,7 @@ Parser::ParseExprAfterUnaryExprOrTypeTrait(const Token &OpTok,
25282530
// type-name, or it is a unary-expression that starts with a compound
25292531
// literal, or starts with a primary-expression that is a parenthesized
25302532
// expression.
2531-
ParenParseOption ExprType = CastExpr;
2533+
ParenParseOption ExprType = ParenParseOption::CastExpr;
25322534
SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
25332535

25342536
Operand = ParseParenExpression(ExprType, true/*stopIfCastExpr*/,
@@ -2537,7 +2539,7 @@ Parser::ParseExprAfterUnaryExprOrTypeTrait(const Token &OpTok,
25372539

25382540
// If ParseParenExpression parsed a '(typename)' sequence only, then this is
25392541
// a type.
2540-
if (ExprType == CastExpr) {
2542+
if (ExprType == ParenParseOption::CastExpr) {
25412543
isCastExpr = true;
25422544
return ExprEmpty();
25432545
}
@@ -3095,7 +3097,7 @@ Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr,
30953097
cutOffParsing();
30963098
Actions.CodeCompletion().CodeCompleteExpression(
30973099
getCurScope(), PreferredType.get(Tok.getLocation()),
3098-
/*IsParenthesized=*/ExprType >= CompoundLiteral);
3100+
/*IsParenthesized=*/ExprType >= ParenParseOption::CompoundLiteral);
30993101
return ExprError();
31003102
}
31013103

@@ -3119,7 +3121,7 @@ Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr,
31193121

31203122
// None of these cases should fall through with an invalid Result
31213123
// unless they've already reported an error.
3122-
if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
3124+
if (ExprType >= ParenParseOption::CompoundStmt && Tok.is(tok::l_brace)) {
31233125
Diag(Tok, OpenLoc.isMacroID() ? diag::ext_gnu_statement_expr_macro
31243126
: diag::ext_gnu_statement_expr);
31253127

@@ -3142,7 +3144,7 @@ Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr,
31423144
Actions.ActOnStartStmtExpr();
31433145

31443146
StmtResult Stmt(ParseCompoundStatement(true));
3145-
ExprType = CompoundStmt;
3147+
ExprType = ParenParseOption::CompoundStmt;
31463148

31473149
// If the substmt parsed correctly, build the AST node.
31483150
if (!Stmt.isInvalid()) {
@@ -3152,7 +3154,7 @@ Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr,
31523154
Actions.ActOnStmtExprError();
31533155
}
31543156
}
3155-
} else if (ExprType >= CompoundLiteral && BridgeCast) {
3157+
} else if (ExprType >= ParenParseOption::CompoundLiteral && BridgeCast) {
31563158
tok::TokenKind tokenKind = Tok.getKind();
31573159
SourceLocation BridgeKeywordLoc = ConsumeToken();
31583160

@@ -3189,7 +3191,7 @@ Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr,
31893191
return Actions.ObjC().ActOnObjCBridgedCast(getCurScope(), OpenLoc, Kind,
31903192
BridgeKeywordLoc, Ty.get(),
31913193
RParenLoc, SubExpr.get());
3192-
} else if (ExprType >= CompoundLiteral &&
3194+
} else if (ExprType >= ParenParseOption::CompoundLiteral &&
31933195
isTypeIdInParens(isAmbiguousTypeId)) {
31943196

31953197
// Otherwise, this is a compound literal expression or cast expression.
@@ -3233,7 +3235,7 @@ Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr,
32333235
ColonProtection.restore();
32343236
RParenLoc = T.getCloseLocation();
32353237
if (Tok.is(tok::l_brace)) {
3236-
ExprType = CompoundLiteral;
3238+
ExprType = ParenParseOption::CompoundLiteral;
32373239
TypeResult Ty;
32383240
{
32393241
InMessageExpressionRAIIObject InMessage(*this, false);
@@ -3285,7 +3287,7 @@ Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr,
32853287
}
32863288
}
32873289

3288-
if (ExprType == CastExpr) {
3290+
if (ExprType == ParenParseOption::CastExpr) {
32893291
// We parsed '(' type-name ')' and the thing after it wasn't a '{'.
32903292

32913293
if (DeclaratorInfo.isInvalidType())
@@ -3331,9 +3333,9 @@ Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr,
33313333
Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
33323334
return ExprError();
33333335
}
3334-
} else if (ExprType >= FoldExpr && Tok.is(tok::ellipsis) &&
3336+
} else if (ExprType >= ParenParseOption::FoldExpr && Tok.is(tok::ellipsis) &&
33353337
isFoldOperator(NextToken().getKind())) {
3336-
ExprType = FoldExpr;
3338+
ExprType = ParenParseOption::FoldExpr;
33373339
return ParseFoldExpression(ExprResult(), T);
33383340
} else if (isTypeCast) {
33393341
// Parse the expression-list.
@@ -3343,18 +3345,18 @@ Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr,
33433345
if (!ParseSimpleExpressionList(ArgExprs)) {
33443346
// FIXME: If we ever support comma expressions as operands to
33453347
// fold-expressions, we'll need to allow multiple ArgExprs here.
3346-
if (ExprType >= FoldExpr && ArgExprs.size() == 1 &&
3348+
if (ExprType >= ParenParseOption::FoldExpr && ArgExprs.size() == 1 &&
33473349
isFoldOperator(Tok.getKind()) && NextToken().is(tok::ellipsis)) {
3348-
ExprType = FoldExpr;
3350+
ExprType = ParenParseOption::FoldExpr;
33493351
return ParseFoldExpression(ArgExprs[0], T);
33503352
}
33513353

3352-
ExprType = SimpleExpr;
3354+
ExprType = ParenParseOption::SimpleExpr;
33533355
Result = Actions.ActOnParenListExpr(OpenLoc, Tok.getLocation(),
33543356
ArgExprs);
33553357
}
33563358
} else if (getLangOpts().OpenMP >= 50 && OpenMPDirectiveParsing &&
3357-
ExprType == CastExpr && Tok.is(tok::l_square) &&
3359+
ExprType == ParenParseOption::CastExpr && Tok.is(tok::l_square) &&
33583360
tryParseOpenMPArrayShapingCastPart()) {
33593361
bool ErrorFound = false;
33603362
SmallVector<Expr *, 4> OMPDimensions;
@@ -3395,12 +3397,12 @@ Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr,
33953397
Result = Actions.CorrectDelayedTyposInExpr(Result);
33963398
}
33973399

3398-
if (ExprType >= FoldExpr && isFoldOperator(Tok.getKind()) &&
3399-
NextToken().is(tok::ellipsis)) {
3400-
ExprType = FoldExpr;
3400+
if (ExprType >= ParenParseOption::FoldExpr &&
3401+
isFoldOperator(Tok.getKind()) && NextToken().is(tok::ellipsis)) {
3402+
ExprType = ParenParseOption::FoldExpr;
34013403
return ParseFoldExpression(Result, T);
34023404
}
3403-
ExprType = SimpleExpr;
3405+
ExprType = ParenParseOption::SimpleExpr;
34043406

34053407
// Don't build a paren expression unless we actually match a ')'.
34063408
if (!Result.isInvalid() && Tok.is(tok::r_paren))

clang/lib/Parse/ParseExprCXX.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4085,7 +4085,8 @@ Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
40854085
BalancedDelimiterTracker &Tracker,
40864086
ColonProtectionRAIIObject &ColonProt) {
40874087
assert(getLangOpts().CPlusPlus && "Should only be called for C++!");
4088-
assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
4088+
assert(ExprType == ParenParseOption::CastExpr &&
4089+
"Compound literals are not ambiguous!");
40894090
assert(isTypeIdInParens() && "Not a type-id!");
40904091

40914092
ExprResult Result(true);
@@ -4122,7 +4123,7 @@ Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
41224123
}
41234124

41244125
if (Tok.is(tok::l_brace)) {
4125-
ParseAs = CompoundLiteral;
4126+
ParseAs = ParenParseOption::CompoundLiteral;
41264127
} else {
41274128
bool NotCastExpr;
41284129
if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
@@ -4140,7 +4141,8 @@ Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
41404141

41414142
// If we parsed a cast-expression, it's really a type-id, otherwise it's
41424143
// an expression.
4143-
ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
4144+
ParseAs =
4145+
NotCastExpr ? ParenParseOption::SimpleExpr : ParenParseOption::CastExpr;
41444146
}
41454147

41464148
// Create a fake EOF to mark end of Toks buffer.
@@ -4161,7 +4163,7 @@ Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
41614163
// as when we entered this function.
41624164
ConsumeAnyToken();
41634165

4164-
if (ParseAs >= CompoundLiteral) {
4166+
if (ParseAs >= ParenParseOption::CompoundLiteral) {
41654167
// Parse the type declarator.
41664168
DeclSpec DS(AttrFactory);
41674169
Declarator DeclaratorInfo(DS, ParsedAttributesView::none(),
@@ -4180,8 +4182,8 @@ Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
41804182
assert(Tok.is(tok::eof) && Tok.getEofData() == AttrEnd.getEofData());
41814183
ConsumeAnyToken();
41824184

4183-
if (ParseAs == CompoundLiteral) {
4184-
ExprType = CompoundLiteral;
4185+
if (ParseAs == ParenParseOption::CompoundLiteral) {
4186+
ExprType = ParenParseOption::CompoundLiteral;
41854187
if (DeclaratorInfo.isInvalidType())
41864188
return ExprError();
41874189

@@ -4192,7 +4194,7 @@ Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
41924194
}
41934195

41944196
// We parsed '(' type-id ')' and the thing after it wasn't a '{'.
4195-
assert(ParseAs == CastExpr);
4197+
assert(ParseAs == ParenParseOption::CastExpr);
41964198

41974199
if (DeclaratorInfo.isInvalidType())
41984200
return ExprError();
@@ -4206,9 +4208,9 @@ Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
42064208
}
42074209

42084210
// Not a compound literal, and not followed by a cast-expression.
4209-
assert(ParseAs == SimpleExpr);
4211+
assert(ParseAs == ParenParseOption::SimpleExpr);
42104212

4211-
ExprType = SimpleExpr;
4213+
ExprType = ParenParseOption::SimpleExpr;
42124214
Result = ParseExpression();
42134215
if (!Result.isInvalid() && Tok.is(tok::r_paren))
42144216
Result = Actions.ActOnParenExpr(Tracker.getOpenLocation(),

clang/lib/Parse/Parser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1712,7 +1712,7 @@ ExprResult Parser::ParseAsmStringLiteral(bool ForAsmLabel) {
17121712
}
17131713
} else if (!ForAsmLabel && getLangOpts().CPlusPlus11 &&
17141714
Tok.is(tok::l_paren)) {
1715-
ParenParseOption ExprType = SimpleExpr;
1715+
ParenParseOption ExprType = ParenParseOption::SimpleExpr;
17161716
SourceLocation RParenLoc;
17171717
ParsedType CastTy;
17181718

0 commit comments

Comments
 (0)