Skip to content

Commit 850583a

Browse files
committed
[NFC] Extract Parser::parseUnqualifiedDeclBaseName
1 parent 8551b32 commit 850583a

File tree

2 files changed

+47
-21
lines changed

2 files changed

+47
-21
lines changed

include/swift/Parse/Parser.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,11 +1411,26 @@ class Parser {
14111411
/// \param loc The location of the label (empty if it doesn't exist)
14121412
void parseOptionalArgumentLabel(Identifier &name, SourceLoc &loc);
14131413

1414-
/// Parse an unqualified-decl-name.
1414+
/// Parse an unqualified-decl-base-name.
14151415
///
14161416
/// unqualified-decl-name:
14171417
/// identifier
1418-
/// identifier '(' ((identifier | '_') ':') + ')'
1418+
///
1419+
/// \param afterDot Whether this identifier is coming after a period, which
1420+
/// enables '.init' and '.default' like expressions.
1421+
/// \param loc Will be populated with the location of the name.
1422+
/// \param diag The diagnostic to emit if this is not a name.
1423+
/// \param allowOperators Whether to allow operator basenames too.
1424+
DeclBaseName parseUnqualifiedDeclBaseName(bool afterDot, DeclNameLoc &loc,
1425+
const Diagnostic &diag,
1426+
bool allowOperators=false,
1427+
bool allowDeinitAndSubscript=false);
1428+
1429+
/// Parse an unqualified-decl-name.
1430+
///
1431+
/// unqualified-decl-name:
1432+
/// unqualified-decl-base-name
1433+
/// unqualified-decl-base-name '(' ((identifier | '_') ':') + ')'
14191434
///
14201435
/// \param afterDot Whether this identifier is coming after a period, which
14211436
/// enables '.init' and '.default' like expressions.

lib/Parse/ParseExpr.cpp

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2077,12 +2077,12 @@ void Parser::parseOptionalArgumentLabel(Identifier &name, SourceLoc &loc) {
20772077
}
20782078
}
20792079

2080-
DeclName Parser::parseUnqualifiedDeclName(bool afterDot,
2081-
DeclNameLoc &loc,
2082-
const Diagnostic &diag,
2083-
bool allowOperators,
2084-
bool allowZeroArgCompoundNames,
2085-
bool allowDeinitAndSubscript) {
2080+
DeclBaseName Parser::parseUnqualifiedDeclBaseName(
2081+
bool afterDot,
2082+
DeclNameLoc &loc,
2083+
const Diagnostic &diag,
2084+
bool allowOperators,
2085+
bool allowDeinitAndSubscript) {
20862086
// Consume the base name.
20872087
DeclBaseName baseName;
20882088
SourceLoc baseNameLoc;
@@ -2114,12 +2114,25 @@ DeclName Parser::parseUnqualifiedDeclName(bool afterDot,
21142114
return DeclName();
21152115
}
21162116

2117+
loc = DeclNameLoc(baseNameLoc);
2118+
return baseName;
2119+
}
2120+
2121+
2122+
DeclName Parser::parseUnqualifiedDeclName(bool afterDot,
2123+
DeclNameLoc &loc,
2124+
const Diagnostic &diag,
2125+
bool allowOperators,
2126+
bool allowZeroArgCompoundNames,
2127+
bool allowDeinitAndSubscript) {
2128+
// Consume the base name.
2129+
auto baseName = parseUnqualifiedDeclBaseName(afterDot, loc, diag,
2130+
allowOperators,
2131+
allowDeinitAndSubscript);
2132+
21172133
// If the next token isn't a following '(', we don't have a compound name.
2118-
if (!Tok.isFollowingLParen()) {
2119-
loc = DeclNameLoc(baseNameLoc);
2134+
if (!baseName || !Tok.isFollowingLParen())
21202135
return baseName;
2121-
}
2122-
21232136

21242137
// If the next token is a ')' then we have a 0-arg compound name. This is
21252138
// explicitly differentiated from "simple" (non-compound) name in DeclName.
@@ -2133,7 +2146,6 @@ DeclName Parser::parseUnqualifiedDeclName(bool afterDot,
21332146
ParsedSyntaxRecorder::makeBlankDeclNameArgumentList(
21342147
leadingTriviaLoc(), *SyntaxContext));
21352148
consumeToken(tok::r_paren);
2136-
loc = DeclNameLoc(baseNameLoc);
21372149
SmallVector<Identifier, 2> argumentLabels;
21382150
return DeclName(Context, baseName, argumentLabels);
21392151
}
@@ -2142,7 +2154,6 @@ DeclName Parser::parseUnqualifiedDeclName(bool afterDot,
21422154
// compound name.
21432155
if ((!peekToken().canBeArgumentLabel() && !peekToken().is(tok::colon)) ||
21442156
Identifier::isEditorPlaceholder(peekToken().getText())) {
2145-
loc = DeclNameLoc(baseNameLoc);
21462157
return baseName;
21472158
}
21482159

@@ -2176,7 +2187,6 @@ DeclName Parser::parseUnqualifiedDeclName(bool afterDot,
21762187

21772188
// This is not a compound name.
21782189
// FIXME: Could recover better if we "know" it's a compound name.
2179-
loc = DeclNameLoc(baseNameLoc);
21802190
ArgCtxt.setBackTracking();
21812191
ArgsCtxt.setBackTracking();
21822192
return baseName;
@@ -2190,7 +2200,7 @@ DeclName Parser::parseUnqualifiedDeclName(bool afterDot,
21902200
assert(!argumentLabels.empty() && "Logic above should prevent this");
21912201
assert(argumentLabels.size() == argumentLabelLocs.size());
21922202

2193-
loc = DeclNameLoc(Context, baseNameLoc, lparenLoc, argumentLabelLocs,
2203+
loc = DeclNameLoc(Context, loc.getBaseNameLoc(), lparenLoc, argumentLabelLocs,
21942204
rparenLoc);
21952205
return DeclName(Context, baseName, argumentLabels);
21962206
}
@@ -3020,17 +3030,18 @@ ParserStatus Parser::parseExprList(tok leftTok, tok rightTok,
30203030
if (Tok.isBinaryOperator() && peekToken().isAny(rightTok, tok::comma)) {
30213031
SyntaxParsingContext operatorContext(SyntaxContext,
30223032
SyntaxKind::IdentifierExpr);
3023-
SourceLoc Loc;
3024-
Identifier OperName;
3025-
if (parseAnyIdentifier(OperName, Loc, diag::expected_operator_ref)) {
3033+
DeclNameLoc Loc;
3034+
auto OperName = parseUnqualifiedDeclBaseName(/*afterDot=*/false, Loc,
3035+
diag::expected_operator_ref,
3036+
/*allowOperators=*/true);
3037+
if (!OperName) {
30263038
return makeParserError();
30273039
}
30283040
// Bypass local lookup. Use an 'Ordinary' reference kind so that the
30293041
// reference may resolve to any unary or binary operator based on
30303042
// context.
30313043
SubExpr = new(Context) UnresolvedDeclRefExpr(OperName,
3032-
DeclRefKind::Ordinary,
3033-
DeclNameLoc(Loc));
3044+
DeclRefKind::Ordinary, Loc);
30343045
} else if (isPostfix && Tok.is(tok::code_complete)) {
30353046
// Handle call arguments specially because it may need argument labels.
30363047
auto CCExpr = new (Context) CodeCompletionExpr(Tok.getLoc());

0 commit comments

Comments
 (0)