@@ -3008,9 +3008,10 @@ void Parser::consumeDecl(ParserPosition BeginParserPosition,
30083008 backtrackToPosition (BeginParserPosition);
30093009 SourceLoc BeginLoc = Tok.getLoc ();
30103010
3011- State->delayDecl (PersistentParserState::DelayedDeclKind::Decl, Flags.toRaw (),
3012- CurDeclContext, {BeginLoc, EndLoc},
3013- BeginParserPosition.PreviousLoc );
3011+ State->setCodeCompletionDelayedDeclState (
3012+ PersistentParserState::CodeCompletionDelayedDeclKind::Decl,
3013+ Flags.toRaw (), CurDeclContext, {BeginLoc, EndLoc},
3014+ BeginParserPosition.PreviousLoc );
30143015
30153016 while (SourceMgr.isBeforeInBuffer (Tok.getLoc (), CurrentLoc))
30163017 consumeToken ();
@@ -3045,21 +3046,6 @@ void Parser::setLocalDiscriminatorToParamList(ParameterList *PL) {
30453046 }
30463047}
30473048
3048- void Parser::delayParseFromBeginningToHere (ParserPosition BeginParserPosition,
3049- ParseDeclOptions Flags) {
3050- auto CurLoc = Tok.getLoc ();
3051- backtrackToPosition (BeginParserPosition);
3052- SourceLoc BeginLoc = Tok.getLoc ();
3053- SourceLoc EndLoc = CurLoc;
3054- State->delayDecl (PersistentParserState::DelayedDeclKind::Decl,
3055- Flags.toRaw (),
3056- CurDeclContext, {BeginLoc, EndLoc},
3057- BeginParserPosition.PreviousLoc );
3058-
3059- while (Tok.isNot (tok::eof))
3060- consumeToken ();
3061- }
3062-
30633049// / Parse a single syntactic declaration and return a list of decl
30643050// / ASTs. This can return multiple results for var decls that bind to multiple
30653051// / values, structs that define a struct decl and a constructor, etc.
@@ -3429,26 +3415,23 @@ Parser::parseDecl(ParseDeclOptions Flags,
34293415 consumeToken (tok::code_complete);
34303416 }
34313417
3432- if (AttrStatus.hasCodeCompletion ()) {
3433- if (CodeCompletion) {
3418+ if (AttrStatus.hasCodeCompletion () || DeclResult.hasCodeCompletion ()) {
3419+ if (isCodeCompletionFirstPass () &&
3420+ !CurDeclContext->isModuleScopeContext () &&
3421+ !isa<TopLevelCodeDecl>(CurDeclContext) &&
3422+ !isa<AbstractClosureExpr>(CurDeclContext)) {
3423+ // Only consume non-toplevel decls.
3424+ consumeDecl (BeginParserPosition, Flags, /* IsTopLevel=*/ false );
3425+
3426+ return makeParserError ();
3427+ }
3428+ if (AttrStatus.hasCodeCompletion () && CodeCompletion) {
34343429 Optional<DeclKind> DK;
34353430 if (DeclResult.isNonNull ())
34363431 DK = DeclResult.get ()->getKind ();
34373432 CodeCompletion->setAttrTargetDeclKind (DK);
3438- } else {
3439- delayParseFromBeginningToHere (BeginParserPosition, Flags);
3440- return makeParserError ();
34413433 }
3442- }
3443-
3444- if (DeclResult.hasCodeCompletion () && isCodeCompletionFirstPass () &&
3445- !CurDeclContext->isModuleScopeContext () &&
3446- !isa<TopLevelCodeDecl>(CurDeclContext) &&
3447- !isa<AbstractClosureExpr>(CurDeclContext)) {
3448- // Only consume non-toplevel decls.
3449- consumeDecl (BeginParserPosition, Flags, /* IsTopLevel=*/ false );
3450-
3451- return makeParserError ();
3434+ DeclResult.setHasCodeCompletion ();
34523435 }
34533436
34543437 if (auto SF = CurDeclContext->getParentSourceFile ()) {
@@ -3593,48 +3576,6 @@ std::vector<Decl *> Parser::parseDeclListDelayed(IterableDeclContext *IDC) {
35933576 return parseDeclList (LBLoc, RBLoc, Id, Options, IDC, hadError);
35943577}
35953578
3596- void Parser::parseDeclDelayed () {
3597- auto DelayedState = State->takeDelayedDeclState ();
3598- assert (DelayedState.get () && " should have delayed state" );
3599-
3600- auto BeginParserPosition = getParserPosition (DelayedState->BodyPos );
3601- auto EndLexerState = L->getStateForEndOfTokenLoc (DelayedState->BodyEnd );
3602-
3603- // ParserPositionRAII needs a primed parser to restore to.
3604- if (Tok.is (tok::NUM_TOKENS))
3605- consumeTokenWithoutFeedingReceiver ();
3606-
3607- // Ensure that we restore the parser state at exit.
3608- ParserPositionRAII PPR (*this );
3609-
3610- // Create a lexer that cannot go past the end state.
3611- Lexer LocalLex (*L, BeginParserPosition.LS , EndLexerState);
3612-
3613- // Temporarily swap out the parser's current lexer with our new one.
3614- llvm::SaveAndRestore<Lexer *> T (L, &LocalLex);
3615-
3616- // Rewind to the beginning of the decl.
3617- restoreParserPosition (BeginParserPosition);
3618-
3619- // Re-enter the lexical scope.
3620- Scope S (this , DelayedState->takeScope ());
3621- ContextChange CC (*this , DelayedState->ParentContext );
3622-
3623- parseDecl (ParseDeclOptions (DelayedState->Flags ),
3624- /* IsAtStartOfLineOrPreviousHadSemi=*/ true ,
3625- [&](Decl *D) {
3626- if (auto *parent = DelayedState->ParentContext ) {
3627- if (auto *NTD = dyn_cast<NominalTypeDecl>(parent)) {
3628- NTD->addMember (D);
3629- } else if (auto *ED = dyn_cast<ExtensionDecl>(parent)) {
3630- ED->addMember (D);
3631- } else if (auto *SF = dyn_cast<SourceFile>(parent)) {
3632- SF->Decls .push_back (D);
3633- }
3634- }
3635- });
3636- }
3637-
36383579// / Parse an 'import' declaration, doing no token skipping on error.
36393580// /
36403581// / \verbatim
@@ -5658,9 +5599,9 @@ void Parser::consumeAbstractFunctionBody(AbstractFunctionDecl *AFD,
56585599
56595600 if (isCodeCompletionFirstPass ()) {
56605601 if (SourceMgr.rangeContainsCodeCompletionLoc (BodyRange)) {
5661- State->delayDecl (PersistentParserState::DelayedDeclKind::FunctionBody,
5662- PD_Default, AFD, BodyRange ,
5663- BeginParserPosition.PreviousLoc );
5602+ State->setCodeCompletionDelayedDeclState (
5603+ PersistentParserState::CodeCompletionDelayedDeclKind::FunctionBody ,
5604+ PD_Default, AFD, BodyRange, BeginParserPosition.PreviousLoc );
56645605 } else {
56655606 AFD->setBodySkipped (BodyRange);
56665607 }
@@ -5975,20 +5916,6 @@ BraceStmt *Parser::parseAbstractFunctionBodyDelayed(AbstractFunctionDecl *AFD) {
59755916 return parseBraceItemList (diag::func_decl_without_brace).getPtrOrNull ();
59765917}
59775918
5978- // / Parse a delayed function body from the 'PersistentParserState'.
5979- void Parser::parseAbstractFunctionBodyDelayed () {
5980- auto DelayedState = State->takeDelayedDeclState ();
5981- assert (DelayedState.get () && " should have delayed state" );
5982- auto CD = DelayedState->ParentContext ->getAsDecl ();
5983- auto AFD = cast<AbstractFunctionDecl>(CD);
5984-
5985- // Eagarly parse local decls or nested function bodies inside the body.
5986- llvm::SaveAndRestore<bool > DisableDelayedBody (DelayBodyParsing, false );
5987-
5988- auto body = parseAbstractFunctionBodyDelayed (AFD);
5989- AFD->setBodyParsed (body);
5990- }
5991-
59925919// / Parse a 'enum' declaration, returning true (and doing no token
59935920// / skipping) on error.
59945921// /
0 commit comments