@@ -465,6 +465,18 @@ void Parser::consumeDecl(ParserPosition BeginParserPosition, unsigned Flags,
465
465
}
466
466
}
467
467
468
+ void Parser::setLocalDiscriminator (ValueDecl *D) {
469
+ // If we're not in a local context, this is unnecessary.
470
+ if (!LocalDiscriminators) return ;
471
+
472
+ Identifier name = D->getName ();
473
+ assert (!name.empty () &&
474
+ " setting a local discriminator on an anonymous decl; "
475
+ " maybe the name hasn't been set yet?" );
476
+ unsigned discriminator = (*LocalDiscriminators)[name]++;
477
+ D->setLocalDiscriminator (discriminator);
478
+ }
479
+
468
480
// / \brief Parse a single syntactic declaration and return a list of decl
469
481
// / ASTs. This can return multiple results for var decls that bind to multiple
470
482
// / values, structs that define a struct decl and a constructor, etc.
@@ -1160,7 +1172,7 @@ bool Parser::parseGetSet(bool HasContainerType, Pattern *Indices,
1160
1172
addFunctionParametersToScope (Get->getBodyParamPatterns (), Get);
1161
1173
1162
1174
// Establish the new context.
1163
- ContextChange CC (*this , Get);
1175
+ ParseFunctionBody CC (*this , Get);
1164
1176
1165
1177
SmallVector<ASTNode, 16 > Entries;
1166
1178
parseBraceItems (Entries, BraceItemListKind::Variable);
@@ -1281,7 +1293,7 @@ bool Parser::parseGetSet(bool HasContainerType, Pattern *Indices,
1281
1293
addFunctionParametersToScope (Set->getBodyParamPatterns (), Set);
1282
1294
1283
1295
// Establish the new context.
1284
- ContextChange CC (*this , Set);
1296
+ ParseFunctionBody CC (*this , Set);
1285
1297
1286
1298
// Parse the body.
1287
1299
SmallVector<ASTNode, 16 > Entries;
@@ -1334,6 +1346,8 @@ void Parser::parseDeclVarGetSet(Pattern &pattern, bool HasContainerType,
1334
1346
diagnose (pattern.getLoc (), diag::getset_missing_type);
1335
1347
TyLoc = TypeLoc::withoutLoc (ErrorType::get (Context));
1336
1348
}
1349
+
1350
+ setLocalDiscriminator (PrimaryVar);
1337
1351
1338
1352
SourceLoc LBLoc = consumeToken (tok::l_brace);
1339
1353
@@ -1684,6 +1698,7 @@ Parser::parseDeclFunc(SourceLoc StaticLoc, unsigned Flags,
1684
1698
1685
1699
addFunctionParametersToScope (FD->getBodyParamPatterns (), FD);
1686
1700
setVarContext (FD->getArgParamPatterns (), FD);
1701
+ setLocalDiscriminator (FD);
1687
1702
1688
1703
// Now that we have a context, update the generic parameters with that
1689
1704
// context.
@@ -1694,7 +1709,7 @@ Parser::parseDeclFunc(SourceLoc StaticLoc, unsigned Flags,
1694
1709
}
1695
1710
1696
1711
// Establish the new context.
1697
- ContextChange CC (*this , FD);
1712
+ ParseFunctionBody CC (*this , FD);
1698
1713
1699
1714
// Check to see if we have a "{" to start a brace statement.
1700
1715
if (Tok.is (tok::l_brace)) {
@@ -1762,7 +1777,7 @@ bool Parser::parseAbstractFunctionBodyDelayed(AbstractFunctionDecl *AFD) {
1762
1777
1763
1778
// Re-enter the lexical scope.
1764
1779
Scope S (this , FunctionParserState->takeScope ());
1765
- ContextChange CC (*this , AFD);
1780
+ ParseFunctionBody CC (*this , AFD);
1766
1781
1767
1782
ParserResult<BraceStmt> Body =
1768
1783
parseBraceItemList (diag::func_decl_without_brace);
@@ -1810,6 +1825,7 @@ ParserResult<EnumDecl> Parser::parseDeclEnum(unsigned Flags,
1810
1825
1811
1826
EnumDecl *UD = new (Context) EnumDecl (EnumLoc, EnumName, EnumNameLoc,
1812
1827
{ }, GenericParams, CurDeclContext);
1828
+ setLocalDiscriminator (UD);
1813
1829
1814
1830
if (Attributes.isValid ())
1815
1831
UD->getMutableAttrs () = Attributes;
@@ -2068,6 +2084,7 @@ ParserResult<StructDecl> Parser::parseDeclStruct(unsigned Flags,
2068
2084
{ },
2069
2085
GenericParams,
2070
2086
CurDeclContext);
2087
+ setLocalDiscriminator (SD);
2071
2088
2072
2089
if (Attributes.isValid ())
2073
2090
SD->getMutableAttrs () = Attributes;
@@ -2152,6 +2169,7 @@ ParserResult<ClassDecl> Parser::parseDeclClass(unsigned Flags,
2152
2169
// Create the class.
2153
2170
ClassDecl *CD = new (Context) ClassDecl (ClassLoc, ClassName, ClassNameLoc,
2154
2171
{ }, GenericParams, CurDeclContext);
2172
+ setLocalDiscriminator (CD);
2155
2173
2156
2174
// Attach attributes.
2157
2175
if (Attributes.isValid ())
@@ -2238,6 +2256,7 @@ parseDeclProtocol(unsigned Flags, DeclAttributes &Attributes) {
2238
2256
= new (Context) ProtocolDecl (CurDeclContext, ProtocolLoc, NameLoc,
2239
2257
ProtocolName,
2240
2258
Context.AllocateCopy (InheritedProtocols));
2259
+ // No need to setLocalDiscriminator: protocols can't appear in local contexts.
2241
2260
2242
2261
if (Attributes.isValid ())
2243
2262
Proto->getMutableAttrs () = Attributes;
@@ -2376,6 +2395,9 @@ ParserStatus Parser::parseDeclSubscript(bool HasContainerType,
2376
2395
SubscriptLoc, Indices.get (), ArrowLoc,
2377
2396
ElementTy.get (), DefRange,
2378
2397
Get, Set, CurDeclContext);
2398
+ // No need to setLocalDiscriminator because subscripts cannot
2399
+ // validly appear outside of type decls.
2400
+
2379
2401
if (Attributes.isValid ())
2380
2402
Subscript->getMutableAttrs () = Attributes;
2381
2403
@@ -2445,6 +2467,7 @@ Parser::parseDeclConstructor(unsigned Flags, DeclAttributes &Attributes) {
2445
2467
new (Context) ConstructorDecl (Context.getIdentifier (" init" ),
2446
2468
ConstructorLoc, ArgPattern, BodyPattern,
2447
2469
SelfDecl, GenericParams, CurDeclContext);
2470
+ // No need to setLocalDiscriminator.
2448
2471
2449
2472
if (HasSelectorStyleSignature)
2450
2473
CD->setHasSelectorStyleSignature ();
@@ -2482,7 +2505,7 @@ Parser::parseDeclConstructor(unsigned Flags, DeclAttributes &Attributes) {
2482
2505
}
2483
2506
} else {
2484
2507
// Parse the body.
2485
- ContextChange CC (*this , CD);
2508
+ ParseFunctionBody CC (*this , CD);
2486
2509
2487
2510
if (!isDelayedParsingEnabled ()) {
2488
2511
ParserResult<BraceStmt> Body = parseBraceItemList (diag::invalid_diagnostic);
@@ -2560,13 +2583,14 @@ parseDeclDestructor(unsigned Flags, DeclAttributes &Attributes) {
2560
2583
DestructorDecl *DD
2561
2584
= new (Context) DestructorDecl (Context.getIdentifier (" destructor" ),
2562
2585
DestructorLoc, SelfDecl, CurDeclContext);
2586
+ // No need to setLocalDiscriminator.
2563
2587
2564
2588
SelfDecl->setDeclContext (DD);
2565
2589
addToScope (SelfDecl);
2566
2590
2567
2591
// Parse the body.
2568
2592
if (Tok.is (tok::l_brace)) {
2569
- ContextChange CC (*this , DD);
2593
+ ParseFunctionBody CC (*this , DD);
2570
2594
if (!isDelayedParsingEnabled ()) {
2571
2595
ParserResult<BraceStmt> Body = parseBraceItemList (diag::invalid_diagnostic);
2572
2596
0 commit comments