@@ -5743,6 +5743,41 @@ setOriginalDeclarationForDifferentiableAttributes(DeclAttributes attrs,
5743
5743
const_cast <DerivativeAttr *>(attr)->setOriginalDeclaration (D);
5744
5744
}
5745
5745
5746
+ // / Determine the declaration parsing options to use when parsing the decl in
5747
+ // / the given context.
5748
+ static Parser::ParseDeclOptions getParseDeclOptions (DeclContext *DC) {
5749
+ using ParseDeclOptions = Parser::ParseDeclOptions;
5750
+
5751
+ if (DC->isModuleScopeContext ())
5752
+ return Parser::PD_AllowTopLevel;
5753
+
5754
+ if (DC->isLocalContext ())
5755
+ return Parser::PD_Default;
5756
+
5757
+ auto decl = DC->getAsDecl ();
5758
+ switch (decl->getKind ()) {
5759
+ case DeclKind::Extension:
5760
+ return ParseDeclOptions (Parser::PD_HasContainerType |
5761
+ Parser::PD_InExtension);
5762
+ case DeclKind::Enum:
5763
+ return ParseDeclOptions (Parser::PD_HasContainerType |
5764
+ Parser::PD_AllowEnumElement | Parser::PD_InEnum);
5765
+
5766
+ case DeclKind::Protocol:
5767
+ return ParseDeclOptions (Parser::PD_HasContainerType |
5768
+ Parser::PD_DisallowInit | Parser::PD_InProtocol);
5769
+
5770
+ case DeclKind::Class:
5771
+ return ParseDeclOptions (Parser::PD_HasContainerType | Parser::PD_InClass);
5772
+
5773
+ case DeclKind::Struct:
5774
+ return ParseDeclOptions (Parser::PD_HasContainerType | Parser::PD_InStruct);
5775
+
5776
+ default :
5777
+ llvm_unreachable (" Bad iterable decl context kinds." );
5778
+ }
5779
+ }
5780
+
5746
5781
// / Parse a single syntactic declaration and return a list of decl
5747
5782
// / ASTs. This can return multiple results for var decls that bind to multiple
5748
5783
// / values, structs that define a struct decl and a constructor, etc.
@@ -5760,11 +5795,10 @@ setOriginalDeclarationForDifferentiableAttributes(DeclAttributes attrs,
5760
5795
// / decl-import
5761
5796
// / decl-operator
5762
5797
// / \endverbatim
5763
- ParserResult<Decl>
5764
- Parser::parseDecl (ParseDeclOptions Flags,
5765
- bool IsAtStartOfLineOrPreviousHadSemi,
5766
- bool IfConfigsAreDeclAttrs,
5767
- llvm::function_ref<void (Decl*)> Handler) {
5798
+ ParserResult<Decl> Parser::parseDecl (bool IsAtStartOfLineOrPreviousHadSemi,
5799
+ bool IfConfigsAreDeclAttrs,
5800
+ llvm::function_ref<void (Decl *)> Handler) {
5801
+ ParseDeclOptions Flags = getParseDeclOptions (CurDeclContext);
5768
5802
ParserPosition BeginParserPosition;
5769
5803
if (isIDEInspectionFirstPass ())
5770
5804
BeginParserPosition = getParserPosition ();
@@ -5784,8 +5818,8 @@ Parser::parseDecl(ParseDeclOptions Flags,
5784
5818
skipUntilConditionalBlockClose ();
5785
5819
break ;
5786
5820
}
5787
- Status |= parseDeclItem (PreviousHadSemi, Flags,
5788
- [&](Decl *D) {Decls.emplace_back (D);});
5821
+ Status |= parseDeclItem (PreviousHadSemi,
5822
+ [&](Decl *D) { Decls.emplace_back (D); });
5789
5823
}
5790
5824
});
5791
5825
if (IfConfigResult.hasCodeCompletion () && isIDEInspectionFirstPass ()) {
@@ -6165,38 +6199,6 @@ Parser::parseDecl(ParseDeclOptions Flags,
6165
6199
return DeclResult;
6166
6200
}
6167
6201
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
-
6200
6202
std::pair<std::vector<Decl *>, llvm::Optional<Fingerprint>>
6201
6203
Parser::parseDeclListDelayed (IterableDeclContext *IDC) {
6202
6204
Decl *D = const_cast <Decl*>(IDC->getDecl ());
@@ -6256,8 +6258,7 @@ Parser::parseDeclListDelayed(IterableDeclContext *IDC) {
6256
6258
llvm_unreachable (" Bad iterable decl context kinds." );
6257
6259
}
6258
6260
bool hadError = false ;
6259
- ParseDeclOptions Options = getMemberParseDeclOptions (IDC);
6260
- return parseDeclList (LBLoc, RBLoc, Id, Options, IDC, hadError);
6261
+ return parseDeclList (LBLoc, RBLoc, Id, hadError);
6261
6262
}
6262
6263
6263
6264
// / Parse an 'import' declaration, doing no token skipping on error.
@@ -6654,8 +6655,7 @@ void Parser::diagnoseConsecutiveIDs(StringRef First, SourceLoc FirstLoc,
6654
6655
6655
6656
// / Parse a Decl item in decl list.
6656
6657
ParserStatus Parser::parseDeclItem (bool &PreviousHadSemi,
6657
- ParseDeclOptions Options,
6658
- llvm::function_ref<void (Decl*)> handler) {
6658
+ llvm::function_ref<void (Decl *)> handler) {
6659
6659
if (Tok.is (tok::semi)) {
6660
6660
// Consume ';' without preceding decl.
6661
6661
diagnose (Tok, diag::unexpected_separator, " ;" )
@@ -6682,9 +6682,9 @@ ParserStatus Parser::parseDeclItem(bool &PreviousHadSemi,
6682
6682
return LineDirectiveStatus;
6683
6683
}
6684
6684
6685
- ParserResult<Decl> Result = parseDecl (
6686
- Options, IsAtStartOfLineOrPreviousHadSemi,
6687
- /* IfConfigsAreDeclAttrs=*/ false , handler);
6685
+ ParserResult<Decl> Result =
6686
+ parseDecl ( IsAtStartOfLineOrPreviousHadSemi,
6687
+ /* IfConfigsAreDeclAttrs=*/ false , handler);
6688
6688
if (Result.isParseErrorOrHasCompletion ())
6689
6689
skipUntilDeclRBrace (tok::semi, tok::pound_endif);
6690
6690
SourceLoc SemiLoc;
@@ -6727,9 +6727,7 @@ bool Parser::parseMemberDeclList(SourceLoc &LBLoc, SourceLoc &RBLoc,
6727
6727
// When forced to eagerly parse, do so and cache the results in the
6728
6728
// evaluator.
6729
6729
bool hadError = false ;
6730
- ParseDeclOptions Options = getMemberParseDeclOptions (IDC);
6731
- auto membersAndHash =
6732
- parseDeclList (LBLoc, RBLoc, RBraceDiag, Options, IDC, hadError);
6730
+ auto membersAndHash = parseDeclList (LBLoc, RBLoc, RBraceDiag, hadError);
6733
6731
IDC->setMaybeHasOperatorDeclarations ();
6734
6732
IDC->setMaybeHasNestedClassDeclarations ();
6735
6733
Context.evaluator .cacheOutput (
@@ -6752,7 +6750,6 @@ bool Parser::parseMemberDeclList(SourceLoc &LBLoc, SourceLoc &RBLoc,
6752
6750
// / \endverbatim
6753
6751
std::pair<std::vector<Decl *>, llvm::Optional<Fingerprint>>
6754
6752
Parser::parseDeclList (SourceLoc LBLoc, SourceLoc &RBLoc, Diag<> ErrorDiag,
6755
- ParseDeclOptions Options, IterableDeclContext *IDC,
6756
6753
bool &hadError) {
6757
6754
6758
6755
// Hash the type body separately.
@@ -6767,8 +6764,8 @@ Parser::parseDeclList(SourceLoc LBLoc, SourceLoc &RBLoc, Diag<> ErrorDiag,
6767
6764
bool PreviousHadSemi = true ;
6768
6765
{
6769
6766
while (Tok.isNot (tok::r_brace)) {
6770
- Status |= parseDeclItem (PreviousHadSemi, Options,
6771
- [&](Decl *D) { decls.push_back (D); });
6767
+ Status |=
6768
+ parseDeclItem (PreviousHadSemi, [&](Decl *D) { decls.push_back (D); });
6772
6769
if (Tok.isAny (tok::eof, tok::pound_endif, tok::pound_else,
6773
6770
tok::pound_elseif)) {
6774
6771
IsInputIncomplete = true ;
@@ -8048,9 +8045,7 @@ void Parser::parseExpandedMemberList(SmallVectorImpl<ASTNode> &items) {
8048
8045
8049
8046
SourceLoc startingLoc = Tok.getLoc ();
8050
8047
while (!Tok.is (tok::eof)) {
8051
- parseDeclItem (previousHadSemi,
8052
- getMemberParseDeclOptions (idc),
8053
- [&](Decl *d) { items.push_back (d); });
8048
+ parseDeclItem (previousHadSemi, [&](Decl *d) { items.push_back (d); });
8054
8049
8055
8050
if (Tok.getLoc () == startingLoc)
8056
8051
break ;
0 commit comments