@@ -1931,8 +1931,13 @@ void Parser::delayParseFromBeginningToHere(ParserPosition BeginParserPosition,
1931
1931
// / decl-import
1932
1932
// / decl-operator
1933
1933
// / \endverbatim
1934
- ParserStatus Parser::parseDecl (SmallVectorImpl<Decl*> &Entries,
1935
- ParseDeclOptions Flags) {
1934
+ ParserStatus Parser::parseDecl (ParseDeclOptions Flags,
1935
+ llvm::function_ref<void (Decl*)> Handler) {
1936
+ Decl* LastDecl = nullptr ;
1937
+ auto InternalHandler = [&](Decl *D) {
1938
+ LastDecl = D;
1939
+ Handler (D);
1940
+ };
1936
1941
ParserPosition BeginParserPosition;
1937
1942
if (isCodeCompletionFirstPass ())
1938
1943
BeginParserPosition = getParserPosition ();
@@ -2115,12 +2120,15 @@ ParserStatus Parser::parseDecl(SmallVectorImpl<Decl*> &Entries,
2115
2120
Status = DeclResult;
2116
2121
break ;
2117
2122
case tok::kw_let:
2118
- case tok::kw_var:
2123
+ case tok::kw_var: {
2124
+ llvm::SmallVector<Decl *, 4 > Entries;
2119
2125
Status = parseDeclVar (Flags, Attributes, Entries, StaticLoc,
2120
2126
StaticSpelling, tryLoc);
2121
2127
StaticLoc = SourceLoc (); // we handled static if present.
2122
2128
MayNeedOverrideCompletion = true ;
2129
+ std::for_each (Entries.begin (), Entries.end (), InternalHandler);
2123
2130
break ;
2131
+ }
2124
2132
case tok::kw_typealias:
2125
2133
DeclResult = parseDeclTypeAlias (Flags, Attributes);
2126
2134
Status = DeclResult;
@@ -2133,9 +2141,12 @@ ParserStatus Parser::parseDecl(SmallVectorImpl<Decl*> &Entries,
2133
2141
DeclResult = parseDeclEnum (Flags, Attributes);
2134
2142
Status = DeclResult;
2135
2143
break ;
2136
- case tok::kw_case:
2144
+ case tok::kw_case: {
2145
+ llvm::SmallVector<Decl *, 4 > Entries;
2137
2146
Status = parseDeclEnumCase (Flags, Attributes, Entries);
2147
+ std::for_each (Entries.begin (), Entries.end (), InternalHandler);
2138
2148
break ;
2149
+ }
2139
2150
case tok::kw_struct:
2140
2151
DeclResult = parseDeclStruct (Flags, Attributes);
2141
2152
Status = DeclResult;
@@ -2162,10 +2173,10 @@ ParserStatus Parser::parseDecl(SmallVectorImpl<Decl*> &Entries,
2162
2173
2163
2174
if (auto ICD = IfConfigResult.getPtrOrNull ()) {
2164
2175
// The IfConfigDecl is ahead of its members in source order.
2165
- Entries. push_back (ICD);
2176
+ InternalHandler (ICD);
2166
2177
// Copy the active members into the entries list.
2167
2178
for (auto activeMember : ICD->getActiveMembers ()) {
2168
- Entries. push_back (activeMember);
2179
+ InternalHandler (activeMember);
2169
2180
}
2170
2181
}
2171
2182
break ;
@@ -2185,15 +2196,18 @@ ParserStatus Parser::parseDecl(SmallVectorImpl<Decl*> &Entries,
2185
2196
MayNeedOverrideCompletion = true ;
2186
2197
break ;
2187
2198
2188
- case tok::kw_subscript:
2199
+ case tok::kw_subscript: {
2189
2200
if (StaticLoc.isValid ()) {
2190
2201
diagnose (Tok, diag::subscript_static, StaticSpelling)
2191
2202
.fixItRemove (SourceRange (StaticLoc));
2192
2203
StaticLoc = SourceLoc ();
2193
2204
}
2205
+ llvm::SmallVector<Decl *, 4 > Entries;
2194
2206
Status = parseDeclSubscript (Flags, Attributes, Entries);
2207
+ std::for_each (Entries.begin (), Entries.end (), InternalHandler);
2195
2208
MayNeedOverrideCompletion = true ;
2196
2209
break ;
2210
+ }
2197
2211
2198
2212
case tok::code_complete:
2199
2213
MayNeedOverrideCompletion = true ;
@@ -2261,20 +2275,20 @@ ParserStatus Parser::parseDecl(SmallVectorImpl<Decl*> &Entries,
2261
2275
if (DeclResult.isNonNull ()) {
2262
2276
Decl *D = DeclResult.get ();
2263
2277
if (!declWasHandledAlready (D))
2264
- Entries. push_back (DeclResult.get ());
2278
+ InternalHandler (DeclResult.get ());
2265
2279
}
2266
2280
2267
2281
if (Tok.is (tok::semi)) {
2268
2282
SourceLoc TrailingSemiLoc = consumeToken (tok::semi);
2269
2283
if (Status.isSuccess ())
2270
- Entries. back () ->TrailingSemiLoc = TrailingSemiLoc;
2284
+ LastDecl ->TrailingSemiLoc = TrailingSemiLoc;
2271
2285
}
2272
2286
2273
2287
if (Status.isSuccess ()) {
2274
2288
// If we parsed 'class' or 'static', but didn't handle it above, complain
2275
2289
// about it.
2276
2290
if (StaticLoc.isValid ())
2277
- diagnose (Entries. back () ->getLoc (), diag::decl_not_static,
2291
+ diagnose (LastDecl ->getLoc (), diag::decl_not_static,
2278
2292
StaticSpelling)
2279
2293
.fixItRemove (SourceRange (StaticLoc));
2280
2294
}
@@ -2309,8 +2323,7 @@ void Parser::parseDeclDelayed() {
2309
2323
Scope S (this , DelayedState->takeScope ());
2310
2324
ContextChange CC (*this , DelayedState->ParentContext );
2311
2325
2312
- SmallVector<Decl *, 2 > Entries;
2313
- parseDecl (Entries, ParseDeclOptions (DelayedState->Flags ));
2326
+ parseDecl (ParseDeclOptions (DelayedState->Flags ), [](Decl *D) {});
2314
2327
}
2315
2328
2316
2329
// / \brief Parse an 'import' declaration, doing no token skipping on error.
@@ -2609,7 +2622,6 @@ Parser::parseDeclExtension(ParseDeclOptions Flags, DeclAttributes &Attributes) {
2609
2622
trailingWhereClause);
2610
2623
ext->getAttrs () = Attributes;
2611
2624
2612
- SmallVector<Decl*, 8 > MemberDecls;
2613
2625
SourceLoc LBLoc, RBLoc;
2614
2626
if (parseToken (tok::l_brace, LBLoc, diag::expected_lbrace_extension)) {
2615
2627
LBLoc = PreviousLoc;
@@ -2627,7 +2639,7 @@ Parser::parseDeclExtension(ParseDeclOptions Flags, DeclAttributes &Attributes) {
2627
2639
ParseDeclOptions Options (PD_HasContainerType |
2628
2640
PD_InExtension);
2629
2641
2630
- return parseDecl (MemberDecls, Options );
2642
+ return parseDecl (Options, [&] (Decl *D) {ext-> addMember (D);} );
2631
2643
});
2632
2644
// Don't propagate the code completion bit from members: we cannot help
2633
2645
// code completion inside a member decl, and our callers cannot do
@@ -2637,8 +2649,6 @@ Parser::parseDeclExtension(ParseDeclOptions Flags, DeclAttributes &Attributes) {
2637
2649
}
2638
2650
2639
2651
ext->setBraces ({LBLoc, RBLoc});
2640
- for (auto member : MemberDecls)
2641
- ext->addMember (member);
2642
2652
2643
2653
if (!DCC.movedToTopLevel () && !(Flags & PD_AllowTopLevel)) {
2644
2654
diagnose (ExtensionLoc, diag::decl_inner_scope);
@@ -2834,7 +2844,7 @@ ParserResult<IfConfigDecl> Parser::parseDeclIfConfig(ParseDeclOptions Flags) {
2834
2844
ParserStatus Status;
2835
2845
while (Tok.isNot (tok::pound_else) && Tok.isNot (tok::pound_endif) &&
2836
2846
Tok.isNot (tok::pound_elseif)) {
2837
- Status = parseDecl (Decls, Flags );
2847
+ Status = parseDecl (Flags, [&](Decl *D) {Decls. push_back (D);} );
2838
2848
if (Status.isError ()) {
2839
2849
diagnose (Tok, diag::expected_close_to_if_directive);
2840
2850
skipUntilConditionalBlockClose ();
@@ -4654,7 +4664,6 @@ ParserResult<EnumDecl> Parser::parseDeclEnum(ParseDeclOptions Flags,
4654
4664
UD->setInherited (Context.AllocateCopy (Inherited));
4655
4665
}
4656
4666
4657
- SmallVector<Decl*, 8 > MemberDecls;
4658
4667
SourceLoc LBLoc, RBLoc;
4659
4668
if (parseToken (tok::l_brace, LBLoc, diag::expected_lbrace_enum)) {
4660
4669
LBLoc = PreviousLoc;
@@ -4664,15 +4673,13 @@ ParserResult<EnumDecl> Parser::parseDeclEnum(ParseDeclOptions Flags,
4664
4673
ContextChange CC (*this , UD);
4665
4674
Scope S (this , ScopeKind::ClassBody);
4666
4675
ParseDeclOptions Options (PD_HasContainerType | PD_AllowEnumElement | PD_InEnum);
4667
- if (parseNominalDeclMembers (MemberDecls, LBLoc, RBLoc,
4676
+ if (parseNominalDeclMembers (LBLoc, RBLoc,
4668
4677
diag::expected_rbrace_enum,
4669
- Options))
4678
+ Options, [&] (Decl *D) { UD-> addMember (D); } ))
4670
4679
Status.setIsParseError ();
4671
4680
}
4672
4681
4673
4682
UD->setBraces ({LBLoc, RBLoc});
4674
- for (auto member : MemberDecls)
4675
- UD->addMember (member);
4676
4683
4677
4684
addToScope (UD);
4678
4685
@@ -4851,9 +4858,14 @@ ParserStatus Parser::parseDeclEnumCase(ParseDeclOptions Flags,
4851
4858
// / \verbatim
4852
4859
// / decl*
4853
4860
// / \endverbatim
4854
- bool Parser::parseNominalDeclMembers (SmallVectorImpl<Decl *> &memberDecls,
4855
- SourceLoc LBLoc, SourceLoc &RBLoc,
4856
- Diag<> ErrorDiag, ParseDeclOptions flags) {
4861
+ bool Parser::parseNominalDeclMembers (SourceLoc LBLoc, SourceLoc &RBLoc,
4862
+ Diag<> ErrorDiag, ParseDeclOptions flags,
4863
+ llvm::function_ref<void (Decl*)> handler) {
4864
+ Decl *lastDecl = nullptr ;
4865
+ auto internalHandler = [&](Decl *D) {
4866
+ lastDecl = D;
4867
+ handler (D);
4868
+ };
4857
4869
bool previousHadSemi = true ;
4858
4870
parseList (tok::r_brace, LBLoc, RBLoc, tok::semi, /* OptionalSep=*/ true ,
4859
4871
/* AllowSepAfterLast=*/ false , ErrorDiag, [&]() -> ParserStatus {
@@ -4867,11 +4879,11 @@ bool Parser::parseNominalDeclMembers(SmallVectorImpl<Decl *> &memberDecls,
4867
4879
}
4868
4880
4869
4881
previousHadSemi = false ;
4870
- if (parseDecl (memberDecls, flags ).isError ())
4882
+ if (parseDecl (flags, internalHandler ).isError ())
4871
4883
return makeParserError ();
4872
4884
4873
4885
// Check whether the previous declaration had a semicolon after it.
4874
- if (!memberDecls. empty () && memberDecls. back () ->TrailingSemiLoc .isValid ())
4886
+ if (lastDecl && lastDecl ->TrailingSemiLoc .isValid ())
4875
4887
previousHadSemi = true ;
4876
4888
4877
4889
return makeParserSuccess ();
@@ -4935,7 +4947,6 @@ ParserResult<StructDecl> Parser::parseDeclStruct(ParseDeclOptions Flags,
4935
4947
SD->setInherited (Context.AllocateCopy (Inherited));
4936
4948
}
4937
4949
4938
- SmallVector<Decl*, 8 > MemberDecls;
4939
4950
SourceLoc LBLoc, RBLoc;
4940
4951
if (parseToken (tok::l_brace, LBLoc, diag::expected_lbrace_struct)) {
4941
4952
LBLoc = PreviousLoc;
@@ -4946,15 +4957,13 @@ ParserResult<StructDecl> Parser::parseDeclStruct(ParseDeclOptions Flags,
4946
4957
ContextChange CC (*this , SD);
4947
4958
Scope S (this , ScopeKind::StructBody);
4948
4959
ParseDeclOptions Options (PD_HasContainerType | PD_InStruct);
4949
- if (parseNominalDeclMembers (MemberDecls, LBLoc, RBLoc,
4960
+ if (parseNominalDeclMembers (LBLoc, RBLoc,
4950
4961
diag::expected_rbrace_struct,
4951
- Options))
4962
+ Options, [&](Decl *D) {SD-> addMember (D);} ))
4952
4963
Status.setIsParseError ();
4953
4964
}
4954
4965
4955
4966
SD->setBraces ({LBLoc, RBLoc});
4956
- for (auto member : MemberDecls)
4957
- SD->addMember (member);
4958
4967
4959
4968
addToScope (SD);
4960
4969
@@ -5018,7 +5027,6 @@ ParserResult<ClassDecl> Parser::parseDeclClass(SourceLoc ClassLoc,
5018
5027
CD->setInherited (Context.AllocateCopy (Inherited));
5019
5028
}
5020
5029
5021
- SmallVector<Decl*, 8 > MemberDecls;
5022
5030
SourceLoc LBLoc, RBLoc;
5023
5031
if (parseToken (tok::l_brace, LBLoc, diag::expected_lbrace_class)) {
5024
5032
LBLoc = PreviousLoc;
@@ -5030,18 +5038,17 @@ ParserResult<ClassDecl> Parser::parseDeclClass(SourceLoc ClassLoc,
5030
5038
Scope S (this , ScopeKind::ClassBody);
5031
5039
ParseDeclOptions Options (PD_HasContainerType | PD_AllowDestructor |
5032
5040
PD_InClass);
5033
- if (parseNominalDeclMembers (MemberDecls, LBLoc, RBLoc,
5034
- diag::expected_rbrace_class,
5035
- Options))
5041
+ auto Handler = [&] (Decl *D) {
5042
+ CD->addMember (D);
5043
+ if (isa<DestructorDecl>(D))
5044
+ CD->setHasDestructor ();
5045
+ };
5046
+ if (parseNominalDeclMembers (LBLoc, RBLoc, diag::expected_rbrace_class,
5047
+ Options, Handler))
5036
5048
Status.setIsParseError ();
5037
5049
}
5038
5050
5039
5051
CD->setBraces ({LBLoc, RBLoc});
5040
- for (auto member : MemberDecls) {
5041
- CD->addMember (member);
5042
- if (isa<DestructorDecl>(member))
5043
- CD->setHasDestructor ();
5044
- }
5045
5052
5046
5053
addToScope (CD);
5047
5054
@@ -5120,9 +5127,6 @@ parseDeclProtocol(ParseDeclOptions Flags, DeclAttributes &Attributes) {
5120
5127
5121
5128
// Parse the body.
5122
5129
{
5123
- // The list of protocol elements.
5124
- SmallVector<Decl*, 8 > Members;
5125
-
5126
5130
SourceLoc LBraceLoc;
5127
5131
SourceLoc RBraceLoc;
5128
5132
if (parseToken (tok::l_brace, LBraceLoc, diag::expected_lbrace_protocol)) {
@@ -5135,16 +5139,14 @@ parseDeclProtocol(ParseDeclOptions Flags, DeclAttributes &Attributes) {
5135
5139
PD_DisallowNominalTypes |
5136
5140
PD_DisallowInit |
5137
5141
PD_InProtocol);
5138
- if (parseNominalDeclMembers (Members, LBraceLoc, RBraceLoc,
5142
+ if (parseNominalDeclMembers (LBraceLoc, RBraceLoc,
5139
5143
diag::expected_rbrace_protocol,
5140
- Options))
5144
+ Options, [&](Decl *D) {Proto-> addMember (D);} ))
5141
5145
Status.setIsParseError ();
5142
5146
}
5143
5147
5144
5148
// Install the protocol elements.
5145
5149
Proto->setBraces ({LBraceLoc, RBraceLoc});
5146
- for (auto member : Members)
5147
- Proto->addMember (member);
5148
5150
}
5149
5151
5150
5152
if (Flags & PD_DisallowNominalTypes) {
0 commit comments