Skip to content

Commit f3975de

Browse files
authored
Merge pull request #69628 from rintaro/parse-parsedecloptions
[Parse] Remove 'ParseDeclOptions' parameter from 'parseDecl' function
2 parents 8ac7f0b + 7e27d99 commit f3975de

File tree

6 files changed

+75
-96
lines changed

6 files changed

+75
-96
lines changed

include/swift/Parse/Parser.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -953,13 +953,11 @@ class Parser {
953953
/// Options that control the parsing of declarations.
954954
using ParseDeclOptions = OptionSet<ParseDeclFlags>;
955955

956-
void consumeDecl(ParserPosition BeginParserPosition, ParseDeclOptions Flags,
957-
bool IsTopLevel);
956+
void consumeDecl(ParserPosition BeginParserPosition, bool IsTopLevel);
958957

959-
ParserResult<Decl> parseDecl(ParseDeclOptions Flags,
960-
bool IsAtStartOfLineOrPreviousHadSemi,
958+
ParserResult<Decl> parseDecl(bool IsAtStartOfLineOrPreviousHadSemi,
961959
bool IfConfigsAreDeclAttrs,
962-
llvm::function_ref<void(Decl*)> Handler);
960+
llvm::function_ref<void(Decl *)> Handler);
963961

964962
std::pair<std::vector<Decl *>, llvm::Optional<Fingerprint>>
965963
parseDeclListDelayed(IterableDeclContext *IDC);
@@ -1235,11 +1233,9 @@ class Parser {
12351233
bool allowAnyObject,
12361234
SourceLoc *parseTildeCopyable = nullptr);
12371235
ParserStatus parseDeclItem(bool &PreviousHadSemi,
1238-
ParseDeclOptions Options,
1239-
llvm::function_ref<void(Decl*)> handler);
1236+
llvm::function_ref<void(Decl *)> handler);
12401237
std::pair<std::vector<Decl *>, llvm::Optional<Fingerprint>>
12411238
parseDeclList(SourceLoc LBLoc, SourceLoc &RBLoc, Diag<> ErrorDiag,
1242-
ParseDeclOptions Options, IterableDeclContext *IDC,
12431239
bool &hadError);
12441240
ParserResult<ExtensionDecl> parseDeclExtension(ParseDeclOptions Flags,
12451241
DeclAttributes &Attributes);

include/swift/Parse/PersistentParserState.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,17 @@ enum class IDEInspectionDelayedDeclKind {
3434
class IDEInspectionDelayedDeclState {
3535
public:
3636
IDEInspectionDelayedDeclKind Kind;
37-
unsigned Flags;
3837
DeclContext *ParentContext;
3938
unsigned StartOffset;
4039
unsigned EndOffset;
4140
unsigned PrevOffset;
4241

4342
IDEInspectionDelayedDeclState(IDEInspectionDelayedDeclKind Kind,
44-
unsigned Flags, DeclContext *ParentContext,
43+
DeclContext *ParentContext,
4544
unsigned StartOffset, unsigned EndOffset,
4645
unsigned PrevOffset)
47-
: Kind(Kind), Flags(Flags), ParentContext(ParentContext),
48-
StartOffset(StartOffset), EndOffset(EndOffset), PrevOffset(PrevOffset) {
49-
}
46+
: Kind(Kind), ParentContext(ParentContext), StartOffset(StartOffset),
47+
EndOffset(EndOffset), PrevOffset(PrevOffset) {}
5048
};
5149

5250
/// Parser state persistent across multiple parses.
@@ -60,7 +58,6 @@ class PersistentParserState {
6058

6159
void setIDEInspectionDelayedDeclState(SourceManager &SM, unsigned BufferID,
6260
IDEInspectionDelayedDeclKind Kind,
63-
unsigned Flags,
6461
DeclContext *ParentContext,
6562
SourceRange BodyRange,
6663
SourceLoc PreviousLoc);

lib/Parse/ParseDecl.cpp

Lines changed: 55 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -5695,9 +5695,7 @@ bool Parser::isStartOfFreestandingMacroExpansion() {
56955695
return false;
56965696
}
56975697

5698-
void Parser::consumeDecl(ParserPosition BeginParserPosition,
5699-
ParseDeclOptions Flags,
5700-
bool IsTopLevel) {
5698+
void Parser::consumeDecl(ParserPosition BeginParserPosition, bool IsTopLevel) {
57015699
SourceLoc CurrentLoc = Tok.getLoc();
57025700

57035701
SourceLoc EndLoc = PreviousLoc;
@@ -5706,8 +5704,7 @@ void Parser::consumeDecl(ParserPosition BeginParserPosition,
57065704

57075705
State->setIDEInspectionDelayedDeclState(
57085706
SourceMgr, L->getBufferID(), IDEInspectionDelayedDeclKind::Decl,
5709-
Flags.toRaw(), CurDeclContext, {BeginLoc, EndLoc},
5710-
BeginParserPosition.PreviousLoc);
5707+
CurDeclContext, {BeginLoc, EndLoc}, BeginParserPosition.PreviousLoc);
57115708

57125709
while (SourceMgr.isBeforeInBuffer(Tok.getLoc(), CurrentLoc))
57135710
consumeToken();
@@ -5743,6 +5740,41 @@ setOriginalDeclarationForDifferentiableAttributes(DeclAttributes attrs,
57435740
const_cast<DerivativeAttr *>(attr)->setOriginalDeclaration(D);
57445741
}
57455742

5743+
/// Determine the declaration parsing options to use when parsing the decl in
5744+
/// the given context.
5745+
static Parser::ParseDeclOptions getParseDeclOptions(DeclContext *DC) {
5746+
using ParseDeclOptions = Parser::ParseDeclOptions;
5747+
5748+
if (DC->isModuleScopeContext())
5749+
return Parser::PD_AllowTopLevel;
5750+
5751+
if (DC->isLocalContext())
5752+
return Parser::PD_Default;
5753+
5754+
auto decl = DC->getAsDecl();
5755+
switch (decl->getKind()) {
5756+
case DeclKind::Extension:
5757+
return ParseDeclOptions(Parser::PD_HasContainerType |
5758+
Parser::PD_InExtension);
5759+
case DeclKind::Enum:
5760+
return ParseDeclOptions(Parser::PD_HasContainerType |
5761+
Parser::PD_AllowEnumElement | Parser::PD_InEnum);
5762+
5763+
case DeclKind::Protocol:
5764+
return ParseDeclOptions(Parser::PD_HasContainerType |
5765+
Parser::PD_DisallowInit | Parser::PD_InProtocol);
5766+
5767+
case DeclKind::Class:
5768+
return ParseDeclOptions(Parser::PD_HasContainerType | Parser::PD_InClass);
5769+
5770+
case DeclKind::Struct:
5771+
return ParseDeclOptions(Parser::PD_HasContainerType | Parser::PD_InStruct);
5772+
5773+
default:
5774+
llvm_unreachable("Bad iterable decl context kinds.");
5775+
}
5776+
}
5777+
57465778
/// Parse a single syntactic declaration and return a list of decl
57475779
/// ASTs. This can return multiple results for var decls that bind to multiple
57485780
/// values, structs that define a struct decl and a constructor, etc.
@@ -5760,11 +5792,10 @@ setOriginalDeclarationForDifferentiableAttributes(DeclAttributes attrs,
57605792
/// decl-import
57615793
/// decl-operator
57625794
/// \endverbatim
5763-
ParserResult<Decl>
5764-
Parser::parseDecl(ParseDeclOptions Flags,
5765-
bool IsAtStartOfLineOrPreviousHadSemi,
5766-
bool IfConfigsAreDeclAttrs,
5767-
llvm::function_ref<void(Decl*)> Handler) {
5795+
ParserResult<Decl> Parser::parseDecl(bool IsAtStartOfLineOrPreviousHadSemi,
5796+
bool IfConfigsAreDeclAttrs,
5797+
llvm::function_ref<void(Decl *)> Handler) {
5798+
ParseDeclOptions Flags = getParseDeclOptions(CurDeclContext);
57685799
ParserPosition BeginParserPosition;
57695800
if (isIDEInspectionFirstPass())
57705801
BeginParserPosition = getParserPosition();
@@ -5784,13 +5815,12 @@ Parser::parseDecl(ParseDeclOptions Flags,
57845815
skipUntilConditionalBlockClose();
57855816
break;
57865817
}
5787-
Status |= parseDeclItem(PreviousHadSemi, Flags,
5788-
[&](Decl *D) {Decls.emplace_back(D);});
5818+
Status |= parseDeclItem(PreviousHadSemi,
5819+
[&](Decl *D) { Decls.emplace_back(D); });
57895820
}
57905821
});
57915822
if (IfConfigResult.hasCodeCompletion() && isIDEInspectionFirstPass()) {
5792-
consumeDecl(BeginParserPosition, Flags,
5793-
CurDeclContext->isModuleScopeContext());
5823+
consumeDecl(BeginParserPosition, CurDeclContext->isModuleScopeContext());
57945824
return makeParserError();
57955825
}
57965826

@@ -6131,7 +6161,7 @@ Parser::parseDecl(ParseDeclOptions Flags,
61316161
!isa<TopLevelCodeDecl>(CurDeclContext) &&
61326162
!isa<AbstractClosureExpr>(CurDeclContext)) {
61336163
// Only consume non-toplevel decls.
6134-
consumeDecl(BeginParserPosition, Flags, /*IsTopLevel=*/false);
6164+
consumeDecl(BeginParserPosition, /*IsTopLevel=*/false);
61356165

61366166
return makeParserError();
61376167
}
@@ -6165,38 +6195,6 @@ Parser::parseDecl(ParseDeclOptions Flags,
61656195
return DeclResult;
61666196
}
61676197

6168-
/// Determine the declaration parsing options to use when parsing the members
6169-
/// of the given context.
6170-
static Parser::ParseDeclOptions getMemberParseDeclOptions(
6171-
IterableDeclContext *idc) {
6172-
using ParseDeclOptions = Parser::ParseDeclOptions;
6173-
6174-
auto decl = idc->getDecl();
6175-
switch (decl->getKind()) {
6176-
case DeclKind::Extension:
6177-
return ParseDeclOptions(
6178-
Parser::PD_HasContainerType | Parser::PD_InExtension);
6179-
case DeclKind::Enum:
6180-
return ParseDeclOptions(
6181-
Parser::PD_HasContainerType | Parser::PD_AllowEnumElement |
6182-
Parser::PD_InEnum);
6183-
6184-
case DeclKind::Protocol:
6185-
return ParseDeclOptions(
6186-
Parser::PD_HasContainerType | Parser::PD_DisallowInit |
6187-
Parser::PD_InProtocol);
6188-
6189-
case DeclKind::Class:
6190-
return ParseDeclOptions(Parser::PD_HasContainerType | Parser::PD_InClass);
6191-
6192-
case DeclKind::Struct:
6193-
return ParseDeclOptions(Parser::PD_HasContainerType | Parser::PD_InStruct);
6194-
6195-
default:
6196-
llvm_unreachable("Bad iterable decl context kinds.");
6197-
}
6198-
}
6199-
62006198
std::pair<std::vector<Decl *>, llvm::Optional<Fingerprint>>
62016199
Parser::parseDeclListDelayed(IterableDeclContext *IDC) {
62026200
Decl *D = const_cast<Decl*>(IDC->getDecl());
@@ -6256,8 +6254,7 @@ Parser::parseDeclListDelayed(IterableDeclContext *IDC) {
62566254
llvm_unreachable("Bad iterable decl context kinds.");
62576255
}
62586256
bool hadError = false;
6259-
ParseDeclOptions Options = getMemberParseDeclOptions(IDC);
6260-
return parseDeclList(LBLoc, RBLoc, Id, Options, IDC, hadError);
6257+
return parseDeclList(LBLoc, RBLoc, Id, hadError);
62616258
}
62626259

62636260
/// Parse an 'import' declaration, doing no token skipping on error.
@@ -6654,8 +6651,7 @@ void Parser::diagnoseConsecutiveIDs(StringRef First, SourceLoc FirstLoc,
66546651

66556652
/// Parse a Decl item in decl list.
66566653
ParserStatus Parser::parseDeclItem(bool &PreviousHadSemi,
6657-
ParseDeclOptions Options,
6658-
llvm::function_ref<void(Decl*)> handler) {
6654+
llvm::function_ref<void(Decl *)> handler) {
66596655
if (Tok.is(tok::semi)) {
66606656
// Consume ';' without preceding decl.
66616657
diagnose(Tok, diag::unexpected_separator, ";")
@@ -6682,9 +6678,9 @@ ParserStatus Parser::parseDeclItem(bool &PreviousHadSemi,
66826678
return LineDirectiveStatus;
66836679
}
66846680

6685-
ParserResult<Decl> Result = parseDecl(
6686-
Options, IsAtStartOfLineOrPreviousHadSemi,
6687-
/* IfConfigsAreDeclAttrs=*/false, handler);
6681+
ParserResult<Decl> Result =
6682+
parseDecl(IsAtStartOfLineOrPreviousHadSemi,
6683+
/* IfConfigsAreDeclAttrs=*/false, handler);
66886684
if (Result.isParseErrorOrHasCompletion())
66896685
skipUntilDeclRBrace(tok::semi, tok::pound_endif);
66906686
SourceLoc SemiLoc;
@@ -6727,9 +6723,7 @@ bool Parser::parseMemberDeclList(SourceLoc &LBLoc, SourceLoc &RBLoc,
67276723
// When forced to eagerly parse, do so and cache the results in the
67286724
// evaluator.
67296725
bool hadError = false;
6730-
ParseDeclOptions Options = getMemberParseDeclOptions(IDC);
6731-
auto membersAndHash =
6732-
parseDeclList(LBLoc, RBLoc, RBraceDiag, Options, IDC, hadError);
6726+
auto membersAndHash = parseDeclList(LBLoc, RBLoc, RBraceDiag, hadError);
67336727
IDC->setMaybeHasOperatorDeclarations();
67346728
IDC->setMaybeHasNestedClassDeclarations();
67356729
Context.evaluator.cacheOutput(
@@ -6752,7 +6746,6 @@ bool Parser::parseMemberDeclList(SourceLoc &LBLoc, SourceLoc &RBLoc,
67526746
/// \endverbatim
67536747
std::pair<std::vector<Decl *>, llvm::Optional<Fingerprint>>
67546748
Parser::parseDeclList(SourceLoc LBLoc, SourceLoc &RBLoc, Diag<> ErrorDiag,
6755-
ParseDeclOptions Options, IterableDeclContext *IDC,
67566749
bool &hadError) {
67576750

67586751
// Hash the type body separately.
@@ -6767,8 +6760,8 @@ Parser::parseDeclList(SourceLoc LBLoc, SourceLoc &RBLoc, Diag<> ErrorDiag,
67676760
bool PreviousHadSemi = true;
67686761
{
67696762
while (Tok.isNot(tok::r_brace)) {
6770-
Status |= parseDeclItem(PreviousHadSemi, Options,
6771-
[&](Decl *D) { decls.push_back(D); });
6763+
Status |=
6764+
parseDeclItem(PreviousHadSemi, [&](Decl *D) { decls.push_back(D); });
67726765
if (Tok.isAny(tok::eof, tok::pound_endif, tok::pound_else,
67736766
tok::pound_elseif)) {
67746767
IsInputIncomplete = true;
@@ -8048,9 +8041,7 @@ void Parser::parseExpandedMemberList(SmallVectorImpl<ASTNode> &items) {
80488041

80498042
SourceLoc startingLoc = Tok.getLoc();
80508043
while (!Tok.is(tok::eof)) {
8051-
parseDeclItem(previousHadSemi,
8052-
getMemberParseDeclOptions(idc),
8053-
[&](Decl *d) { items.push_back(d); });
8044+
parseDeclItem(previousHadSemi, [&](Decl *d) { items.push_back(d); });
80548045

80558046
if (Tok.getLoc() == startingLoc)
80568047
break;
@@ -8908,7 +8899,7 @@ void Parser::parseAbstractFunctionBody(AbstractFunctionDecl *AFD) {
89088899
State->takeIDEInspectionDelayedDeclState();
89098900
State->setIDEInspectionDelayedDeclState(
89108901
SourceMgr, L->getBufferID(), IDEInspectionDelayedDeclKind::FunctionBody,
8911-
PD_Default, AFD, BodyRange, BodyPreviousLoc);
8902+
AFD, BodyRange, BodyPreviousLoc);
89128903
};
89138904

89148905
bool HasNestedTypeDeclarations;

lib/Parse/ParseStmt.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,8 @@ void Parser::consumeTopLevelDecl(ParserPosition BeginParserPosition,
275275
SourceLoc BeginLoc = Tok.getLoc();
276276
State->setIDEInspectionDelayedDeclState(
277277
SourceMgr, L->getBufferID(),
278-
IDEInspectionDelayedDeclKind::TopLevelCodeDecl,
279-
PD_Default, TLCD, {BeginLoc, EndLoc}, BeginParserPosition.PreviousLoc);
278+
IDEInspectionDelayedDeclKind::TopLevelCodeDecl, TLCD, {BeginLoc, EndLoc},
279+
BeginParserPosition.PreviousLoc);
280280

281281
// Skip the rest of the file to prevent the parser from constructing the AST
282282
// for it. Forward references are not allowed at the top level.
@@ -376,7 +376,7 @@ ParserStatus Parser::parseBraceItems(SmallVectorImpl<ASTNode> &Entries,
376376
IsFollowingGuard);
377377
});
378378
if (IfConfigResult.hasCodeCompletion() && isIDEInspectionFirstPass()) {
379-
consumeDecl(BeginParserPosition, llvm::None, IsTopLevel);
379+
consumeDecl(BeginParserPosition, IsTopLevel);
380380
return IfConfigResult;
381381
}
382382
BraceItemsStatus |= IfConfigResult;
@@ -407,11 +407,9 @@ ParserStatus Parser::parseBraceItems(SmallVectorImpl<ASTNode> &Entries,
407407
NeedParseErrorRecovery = Status.isErrorOrHasCompletion();
408408
} else if (isStartOfSwiftDecl()) {
409409
SmallVector<Decl*, 8> TmpDecls;
410-
ParserResult<Decl> DeclResult =
411-
parseDecl(IsTopLevel ? PD_AllowTopLevel : PD_Default,
412-
IsAtStartOfLineOrPreviousHadSemi,
413-
/*IfConfigsAreDeclAttrs=*/true,
414-
[&](Decl *D) {
410+
ParserResult<Decl> DeclResult =
411+
parseDecl(IsAtStartOfLineOrPreviousHadSemi,
412+
/*IfConfigsAreDeclAttrs=*/true, [&](Decl *D) {
415413
TmpDecls.push_back(D);
416414

417415
// Any function after a 'guard' statement is marked as
@@ -428,7 +426,7 @@ ParserStatus Parser::parseBraceItems(SmallVectorImpl<ASTNode> &Entries,
428426
NeedParseErrorRecovery = true;
429427
if (DeclResult.hasCodeCompletion() && IsTopLevel &&
430428
isIDEInspectionFirstPass()) {
431-
consumeDecl(BeginParserPosition, llvm::None, IsTopLevel);
429+
consumeDecl(BeginParserPosition, IsTopLevel);
432430
return DeclResult;
433431
}
434432
}

lib/Parse/Parser.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,8 @@ void Parser::performIDEInspectionSecondPassImpl(
179179
"Delayed decl must be a type member or a top-level decl");
180180
ContextChange CC(*this, DC);
181181

182-
parseDecl(ParseDeclOptions(info.Flags),
183-
/*IsAtStartOfLineOrPreviousHadSemi=*/true,
184-
/*IfConfigsAreDeclAttrs=*/false,
185-
[&](Decl *D) {
182+
parseDecl(/*IsAtStartOfLineOrPreviousHadSemi=*/true,
183+
/*IfConfigsAreDeclAttrs=*/false, [&](Decl *D) {
186184
if (auto *NTD = dyn_cast<NominalTypeDecl>(DC)) {
187185
NTD->addMemberPreservingSourceOrder(D);
188186
} else if (auto *ED = dyn_cast<ExtensionDecl>(DC)) {

lib/Parse/PersistentParserState.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ PersistentParserState::~PersistentParserState() { }
2828

2929
void PersistentParserState::setIDEInspectionDelayedDeclState(
3030
SourceManager &SM, unsigned BufferID, IDEInspectionDelayedDeclKind Kind,
31-
unsigned Flags, DeclContext *ParentContext, SourceRange BodyRange,
32-
SourceLoc PreviousLoc) {
31+
DeclContext *ParentContext, SourceRange BodyRange, SourceLoc PreviousLoc) {
3332
assert(!IDEInspectionDelayedDeclStat.get() &&
3433
"only one decl can be delayed for code completion");
3534
unsigned startOffset = SM.getLocOffsetInBuffer(BodyRange.Start, BufferID);
@@ -39,12 +38,12 @@ void PersistentParserState::setIDEInspectionDelayedDeclState(
3938
prevOffset = SM.getLocOffsetInBuffer(PreviousLoc, BufferID);
4039

4140
IDEInspectionDelayedDeclStat.reset(new IDEInspectionDelayedDeclState(
42-
Kind, Flags, ParentContext, startOffset, endOffset, prevOffset));
41+
Kind, ParentContext, startOffset, endOffset, prevOffset));
4342
}
4443

4544
void PersistentParserState::restoreIDEInspectionDelayedDeclState(
4645
const IDEInspectionDelayedDeclState &other) {
4746
IDEInspectionDelayedDeclStat.reset(new IDEInspectionDelayedDeclState(
48-
other.Kind, other.Flags, other.ParentContext,
49-
other.StartOffset, other.EndOffset, other.PrevOffset));
47+
other.Kind, other.ParentContext, other.StartOffset, other.EndOffset,
48+
other.PrevOffset));
5049
}

0 commit comments

Comments
 (0)