@@ -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-
62006198std::pair<std::vector<Decl *>, llvm::Optional<Fingerprint>>
62016199Parser::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.
66566653ParserStatus 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
67536747std::pair<std::vector<Decl *>, llvm::Optional<Fingerprint>>
67546748Parser::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;
0 commit comments