@@ -181,18 +181,21 @@ namespace {
181181} // end anonymous namespace
182182
183183void PersistentParserState::parseMembers (IterableDeclContext *IDC) {
184- if (!hasDelayedDeclList (IDC))
185- return ;
186184 SourceFile &SF = *IDC->getDecl ()->getDeclContext ()->getParentSourceFile ();
187185 assert (!SF.hasInterfaceHash () &&
188186 " Cannot delay parsing if we care about the interface hash." );
187+ assert (SF.Kind != SourceFileKind::SIL && " cannot delay parsing SIL" );
189188 unsigned BufferID = *SF.getBufferID ();
189+
190190 // MarkedPos is not useful for delayed parsing because we know where we should
191191 // jump the parser to. However, we should recover the MarkedPos here in case
192192 // the PersistentParserState will be used to continuously parse the rest of
193193 // the file linearly.
194194 llvm::SaveAndRestore<ParserPosition> Pos (MarkedPos, ParserPosition ());
195- Parser TheParser (BufferID, SF, nullptr , this );
195+
196+ // Lexer diaganostics have been emitted during skipping, so we disable lexer's
197+ // diagnostic engine here.
198+ Parser TheParser (BufferID, SF, /* No Lexer Diags*/ nullptr , nullptr , this );
196199 // Disable libSyntax creation in the delayed parsing.
197200 TheParser.SyntaxContext ->disable ();
198201 TheParser.parseDeclListDelayed (IDC);
@@ -2292,7 +2295,8 @@ static unsigned skipUntilMatchingRBrace(Parser &P, bool &HasPoundDirective,
22922295 SyntaxParsingContext BodyContext (SyntaxContext, SyntaxKind::TokenList);
22932296 unsigned OpenBraces = 1 ;
22942297 while (OpenBraces != 0 && P.Tok .isNot (tok::eof)) {
2295- HasPoundDirective |= P.Tok .isAny (tok::pound_sourceLocation, tok::pound_line);
2298+ HasPoundDirective |= P.Tok .isAny (tok::pound_sourceLocation, tok::pound_line,
2299+ tok::pound_if, tok::pound_else, tok::pound_endif, tok::pound_elseif);
22962300 if (P.consumeIf (tok::l_brace)) {
22972301 OpenBraces++;
22982302 continue ;
@@ -2902,6 +2906,15 @@ void Parser::parseDeclListDelayed(IterableDeclContext *IDC) {
29022906 ParseDeclOptions (DelayedState->Flags ),
29032907 [&] (Decl *D) { ext->addMember (D); });
29042908 ext->setBraces ({LBLoc, RBLoc});
2909+ } else if (auto *cd = dyn_cast<ClassDecl>(D)) {
2910+ auto handler = [&] (Decl *D) {
2911+ cd->addMember (D);
2912+ if (isa<DestructorDecl>(D))
2913+ cd->setHasDestructor ();
2914+ };
2915+ parseDeclList (cd->getBraces ().Start , RBLoc, Id,
2916+ ParseDeclOptions (DelayedState->Flags ), handler);
2917+ cd->setBraces ({LBLoc, RBLoc});
29052918 } else {
29062919 auto *ntd = cast<NominalTypeDecl>(D);
29072920 parseDeclList (ntd->getBraces ().Start , RBLoc, Id,
@@ -3349,6 +3362,10 @@ bool Parser::parseDeclList(SourceLoc LBLoc, SourceLoc &RBLoc,
33493362}
33503363
33513364bool Parser::canDelayMemberDeclParsing () {
3365+ // There's no fundamental reasons that SIL cannnot be lasily parsed. We need
3366+ // to keep SILParserTUStateBase persistent to make it happen.
3367+ if (isInSILMode ())
3368+ return false ;
33523369 // Calculating interface hash requires tokens consumed in the original order.
33533370 if (SF.hasInterfaceHash ())
33543371 return false ;
@@ -5831,6 +5848,7 @@ ParserResult<StructDecl> Parser::parseDeclStruct(ParseDeclOptions Flags,
58315848 // Make the entities of the struct as a code block.
58325849 SyntaxParsingContext BlockContext (SyntaxContext, SyntaxKind::MemberDeclBlock);
58335850 SourceLoc LBLoc, RBLoc;
5851+ SourceLoc PosBeforeLB = Tok.getLoc ();
58345852 if (parseToken (tok::l_brace, LBLoc, diag::expected_lbrace_struct)) {
58355853 LBLoc = PreviousLoc;
58365854 RBLoc = LBLoc;
@@ -5839,9 +5857,20 @@ ParserResult<StructDecl> Parser::parseDeclStruct(ParseDeclOptions Flags,
58395857 // Parse the body.
58405858 Scope S (this , ScopeKind::StructBody);
58415859 ParseDeclOptions Options (PD_HasContainerType | PD_InStruct);
5842- if (parseDeclList (LBLoc, RBLoc, diag::expected_rbrace_struct,
5843- Options, [&](Decl *D) {SD->addMember (D);}))
5844- Status.setIsParseError ();
5860+ if (canDelayMemberDeclParsing ()) {
5861+ if (Tok.is (tok::r_brace)) {
5862+ RBLoc = consumeToken ();
5863+ } else {
5864+ RBLoc = Tok.getLoc ();
5865+ Status.setIsParseError ();
5866+ }
5867+ State->delayDeclList (SD, Options.toRaw (), CurDeclContext, { LBLoc, RBLoc },
5868+ PosBeforeLB);
5869+ } else {
5870+ if (parseDeclList (LBLoc, RBLoc, diag::expected_rbrace_struct,
5871+ Options, [&](Decl *D) {SD->addMember (D);}))
5872+ Status.setIsParseError ();
5873+ }
58455874 }
58465875
58475876 SD->setBraces ({LBLoc, RBLoc});
@@ -5942,6 +5971,7 @@ ParserResult<ClassDecl> Parser::parseDeclClass(ParseDeclOptions Flags,
59425971
59435972 SyntaxParsingContext BlockContext (SyntaxContext, SyntaxKind::MemberDeclBlock);
59445973 SourceLoc LBLoc, RBLoc;
5974+ auto PosBeforeLB = Tok.getLoc ();
59455975 if (parseToken (tok::l_brace, LBLoc, diag::expected_lbrace_class)) {
59465976 LBLoc = PreviousLoc;
59475977 RBLoc = LBLoc;
@@ -5951,14 +5981,25 @@ ParserResult<ClassDecl> Parser::parseDeclClass(ParseDeclOptions Flags,
59515981 Scope S (this , ScopeKind::ClassBody);
59525982 ParseDeclOptions Options (PD_HasContainerType | PD_AllowDestructor |
59535983 PD_InClass);
5954- auto Handler = [&] (Decl *D) {
5955- CD->addMember (D);
5956- if (isa<DestructorDecl>(D))
5957- CD->setHasDestructor ();
5958- };
5959- if (parseDeclList (LBLoc, RBLoc, diag::expected_rbrace_class,
5960- Options, Handler))
5961- Status.setIsParseError ();
5984+ if (canDelayMemberDeclParsing ()) {
5985+ if (Tok.is (tok::r_brace)) {
5986+ RBLoc = consumeToken ();
5987+ } else {
5988+ RBLoc = Tok.getLoc ();
5989+ Status.setIsParseError ();
5990+ }
5991+ State->delayDeclList (CD, Options.toRaw (), CurDeclContext, { LBLoc, RBLoc },
5992+ PosBeforeLB);
5993+ } else {
5994+ auto Handler = [&] (Decl *D) {
5995+ CD->addMember (D);
5996+ if (isa<DestructorDecl>(D))
5997+ CD->setHasDestructor ();
5998+ };
5999+ if (parseDeclList (LBLoc, RBLoc, diag::expected_rbrace_class,
6000+ Options, Handler))
6001+ Status.setIsParseError ();
6002+ }
59626003 }
59636004
59646005 CD->setBraces ({LBLoc, RBLoc});
@@ -6040,6 +6081,7 @@ parseDeclProtocol(ParseDeclOptions Flags, DeclAttributes &Attributes) {
60406081 SyntaxParsingContext BlockContext (SyntaxContext, SyntaxKind::MemberDeclBlock);
60416082 SourceLoc LBraceLoc;
60426083 SourceLoc RBraceLoc;
6084+ SourceLoc PosBeforeLB = Tok.getLoc ();
60436085 if (parseToken (tok::l_brace, LBraceLoc, diag::expected_lbrace_protocol)) {
60446086 LBraceLoc = PreviousLoc;
60456087 RBraceLoc = LBraceLoc;
@@ -6049,9 +6091,21 @@ parseDeclProtocol(ParseDeclOptions Flags, DeclAttributes &Attributes) {
60496091 ParseDeclOptions Options (PD_HasContainerType |
60506092 PD_DisallowInit |
60516093 PD_InProtocol);
6052- if (parseDeclList (LBraceLoc, RBraceLoc, diag::expected_rbrace_protocol,
6053- Options, [&](Decl *D) {Proto->addMember (D);}))
6054- Status.setIsParseError ();
6094+ if (canDelayMemberDeclParsing ()) {
6095+ if (Tok.is (tok::r_brace)) {
6096+ RBraceLoc = consumeToken ();
6097+ } else {
6098+ RBraceLoc = Tok.getLoc ();
6099+ Status.setIsParseError ();
6100+ }
6101+ State->delayDeclList (Proto, Options.toRaw (), CurDeclContext,
6102+ { LBraceLoc, RBraceLoc },
6103+ PosBeforeLB);
6104+ } else {
6105+ if (parseDeclList (LBraceLoc, RBraceLoc, diag::expected_rbrace_protocol,
6106+ Options, [&](Decl *D) {Proto->addMember (D);}))
6107+ Status.setIsParseError ();
6108+ }
60556109 }
60566110
60576111 // Install the protocol elements.
0 commit comments