Skip to content

Commit 5ae2f76

Browse files
committed
Parse: Factor out new Parser::delayParsingDeclList() method
1 parent 0bdf0fd commit 5ae2f76

File tree

2 files changed

+29
-38
lines changed

2 files changed

+29
-38
lines changed

include/swift/Parse/Parser.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,13 @@ class Parser {
798798

799799
void parseDeclListDelayed(IterableDeclContext *IDC);
800800

801+
bool canDelayMemberDeclParsing();
802+
803+
bool delayParsingDeclList(SourceLoc LBLoc, SourceLoc &RBLoc,
804+
SourceLoc PosBeforeLB,
805+
ParseDeclOptions Options,
806+
IterableDeclContext *IDC);
807+
801808
ParserResult<TypeDecl> parseDeclTypeAlias(ParseDeclOptions Flags,
802809
DeclAttributes &Attributes);
803810

@@ -1428,8 +1435,6 @@ class Parser {
14281435
parsePlatformVersionConstraintSpec();
14291436
ParserResult<PlatformAgnosticVersionConstraintAvailabilitySpec>
14301437
parsePlatformAgnosticVersionConstraintSpec();
1431-
1432-
bool canDelayMemberDeclParsing();
14331438
};
14341439

14351440
/// Describes a parsed declaration name.

lib/Parse/ParseDecl.cpp

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3528,6 +3528,23 @@ bool Parser::canDelayMemberDeclParsing() {
35283528
return !BackTrack.willBacktrack();
35293529
}
35303530

3531+
bool Parser::delayParsingDeclList(SourceLoc LBLoc, SourceLoc &RBLoc,
3532+
SourceLoc PosBeforeLB,
3533+
ParseDeclOptions Options,
3534+
IterableDeclContext *IDC) {
3535+
bool error = false;
3536+
3537+
if (Tok.is(tok::r_brace)) {
3538+
RBLoc = consumeToken();
3539+
} else {
3540+
RBLoc = Tok.getLoc();
3541+
error = true;
3542+
}
3543+
State->delayDeclList(IDC, Options.toRaw(), CurDeclContext, { LBLoc, RBLoc },
3544+
PosBeforeLB);
3545+
return error;
3546+
}
3547+
35313548
/// Parse an 'extension' declaration.
35323549
///
35333550
/// \verbatim
@@ -3592,14 +3609,8 @@ Parser::parseDeclExtension(ParseDeclOptions Flags, DeclAttributes &Attributes) {
35923609
Scope S(this, ScopeKind::Extension);
35933610
ParseDeclOptions Options(PD_HasContainerType | PD_InExtension);
35943611
if (canDelayMemberDeclParsing()) {
3595-
if (Tok.is(tok::r_brace)) {
3596-
RBLoc = consumeToken();
3597-
} else {
3598-
RBLoc = Tok.getLoc();
3612+
if (delayParsingDeclList(LBLoc, RBLoc, PosBeforeLB, Options, ext))
35993613
status.setIsParseError();
3600-
}
3601-
State->delayDeclList(ext, Options.toRaw(), CurDeclContext, { LBLoc, RBLoc },
3602-
PosBeforeLB);
36033614
} else {
36043615
if (parseDeclList(LBLoc, RBLoc, diag::expected_rbrace_extension,
36053616
Options, ext))
@@ -5732,14 +5743,8 @@ ParserResult<EnumDecl> Parser::parseDeclEnum(ParseDeclOptions Flags,
57325743
Scope S(this, ScopeKind::ClassBody);
57335744
ParseDeclOptions Options(PD_HasContainerType | PD_AllowEnumElement | PD_InEnum);
57345745
if (canDelayMemberDeclParsing()) {
5735-
if (Tok.is(tok::r_brace)) {
5736-
RBLoc = consumeToken();
5737-
} else {
5738-
RBLoc = Tok.getLoc();
5746+
if (delayParsingDeclList(LBLoc, RBLoc, PosBeforeLB, Options, ED))
57395747
Status.setIsParseError();
5740-
}
5741-
State->delayDeclList(ED, Options.toRaw(), CurDeclContext, { LBLoc, RBLoc },
5742-
PosBeforeLB);
57435748
} else {
57445749
if (parseDeclList(LBLoc, RBLoc, diag::expected_rbrace_enum,
57455750
Options, ED))
@@ -6017,14 +6022,8 @@ ParserResult<StructDecl> Parser::parseDeclStruct(ParseDeclOptions Flags,
60176022
Scope S(this, ScopeKind::StructBody);
60186023
ParseDeclOptions Options(PD_HasContainerType | PD_InStruct);
60196024
if (canDelayMemberDeclParsing()) {
6020-
if (Tok.is(tok::r_brace)) {
6021-
RBLoc = consumeToken();
6022-
} else {
6023-
RBLoc = Tok.getLoc();
6025+
if (delayParsingDeclList(LBLoc, RBLoc, PosBeforeLB, Options, SD))
60246026
Status.setIsParseError();
6025-
}
6026-
State->delayDeclList(SD, Options.toRaw(), CurDeclContext, { LBLoc, RBLoc },
6027-
PosBeforeLB);
60286027
} else {
60296028
if (parseDeclList(LBLoc, RBLoc, diag::expected_rbrace_struct,
60306029
Options,SD))
@@ -6141,14 +6140,8 @@ ParserResult<ClassDecl> Parser::parseDeclClass(ParseDeclOptions Flags,
61416140
ParseDeclOptions Options(PD_HasContainerType | PD_AllowDestructor |
61426141
PD_InClass);
61436142
if (canDelayMemberDeclParsing()) {
6144-
if (Tok.is(tok::r_brace)) {
6145-
RBLoc = consumeToken();
6146-
} else {
6147-
RBLoc = Tok.getLoc();
6143+
if (delayParsingDeclList(LBLoc, RBLoc, PosBeforeLB, Options, CD))
61486144
Status.setIsParseError();
6149-
}
6150-
State->delayDeclList(CD, Options.toRaw(), CurDeclContext, { LBLoc, RBLoc },
6151-
PosBeforeLB);
61526145
} else {
61536146
if (parseDeclList(LBLoc, RBLoc, diag::expected_rbrace_class,
61546147
Options, CD))
@@ -6246,15 +6239,8 @@ parseDeclProtocol(ParseDeclOptions Flags, DeclAttributes &Attributes) {
62466239
PD_DisallowInit |
62476240
PD_InProtocol);
62486241
if (canDelayMemberDeclParsing()) {
6249-
if (Tok.is(tok::r_brace)) {
6250-
RBraceLoc = consumeToken();
6251-
} else {
6252-
RBraceLoc = Tok.getLoc();
6242+
if (delayParsingDeclList(LBraceLoc, RBraceLoc, PosBeforeLB, Options, Proto))
62536243
Status.setIsParseError();
6254-
}
6255-
State->delayDeclList(Proto, Options.toRaw(), CurDeclContext,
6256-
{ LBraceLoc, RBraceLoc },
6257-
PosBeforeLB);
62586244
} else {
62596245
if (parseDeclList(LBraceLoc, RBraceLoc, diag::expected_rbrace_protocol,
62606246
Options, Proto))

0 commit comments

Comments
 (0)