@@ -476,6 +476,7 @@ static bool escapeKeywordInContext(StringRef keyword, PrintNameContext context){
476
476
case PrintNameContext::Attribute:
477
477
return isKeyword;
478
478
case PrintNameContext::Keyword:
479
+ case PrintNameContext::IntroducerKeyword:
479
480
return false ;
480
481
481
482
case PrintNameContext::ClassDynamicSelf:
@@ -877,7 +878,7 @@ class PrintAST : public ASTVisitor<PrintAST> {
877
878
bool shouldPrintPattern (const Pattern *P);
878
879
void printPatternType (const Pattern *P);
879
880
void printAccessors (const AbstractStorageDecl *ASD);
880
- void printMutabilityModifiersIfNeeded (const FuncDecl *FD);
881
+ void printSelfAccessKindModifiersIfNeeded (const FuncDecl *FD);
881
882
void printMembersOfDecl (Decl * NTD, bool needComma = false ,
882
883
bool openBracket = true , bool closeBracket = true );
883
884
void printMembers (ArrayRef<Decl *> members, bool needComma = false ,
@@ -1143,6 +1144,7 @@ void PrintAST::printAttributes(const Decl *D) {
1143
1144
if (isa<FuncDecl>(D)) {
1144
1145
Options.ExcludeAttrList .push_back (DAK_Mutating);
1145
1146
Options.ExcludeAttrList .push_back (DAK_NonMutating);
1147
+ Options.ExcludeAttrList .push_back (DAK_Consuming);
1146
1148
}
1147
1149
1148
1150
D->getAttrs ().print (Printer, Options, D);
@@ -1291,10 +1293,9 @@ void PrintAST::printPattern(const Pattern *pattern) {
1291
1293
break ;
1292
1294
1293
1295
case PatternKind::Binding:
1294
- if (!Options.SkipIntroducerKeywords )
1295
- Printer << (cast<BindingPattern>(pattern)->isLet () ? tok::kw_let
1296
- : tok::kw_var)
1297
- << " " ;
1296
+ Printer.printIntroducerKeyword (
1297
+ cast<BindingPattern>(pattern)->isLet () ? " let" : " var" ,
1298
+ Options, " " );
1298
1299
printPattern (cast<BindingPattern>(pattern)->getSubPattern ());
1299
1300
}
1300
1301
}
@@ -1901,16 +1902,27 @@ void PrintAST::printBodyIfNecessary(const AbstractFunctionDecl *decl) {
1901
1902
printBraceStmt (decl->getBody (), /* newlineIfEmpty*/ !isa<AccessorDecl>(decl));
1902
1903
}
1903
1904
1904
- void PrintAST::printMutabilityModifiersIfNeeded (const FuncDecl *FD) {
1905
+ void PrintAST::printSelfAccessKindModifiersIfNeeded (const FuncDecl *FD) {
1906
+ if (!Options.PrintSelfAccessKindKeyword )
1907
+ return ;
1908
+
1905
1909
const auto *AD = dyn_cast<AccessorDecl>(FD);
1906
1910
1907
- if (FD->isMutating ()) {
1908
- if (AD == nullptr || AD->isAssumedNonMutating ())
1909
- if (!Options.excludeAttrKind (DAK_Mutating))
1910
- Printer.printKeyword (" mutating" , Options, " " );
1911
- } else if (AD && AD->isExplicitNonMutating () &&
1912
- !Options.excludeAttrKind (DAK_Mutating)) {
1913
- Printer.printKeyword (" nonmutating" , Options, " " );
1911
+ switch (FD->getSelfAccessKind ()) {
1912
+ case SelfAccessKind::Mutating:
1913
+ if ((!AD || AD->isAssumedNonMutating ()) &&
1914
+ !Options.excludeAttrKind (DAK_Mutating))
1915
+ Printer.printKeyword (" mutating" , Options, " " );
1916
+ break ;
1917
+ case SelfAccessKind::NonMutating:
1918
+ if (AD && AD->isExplicitNonMutating () &&
1919
+ !Options.excludeAttrKind (DAK_NonMutating))
1920
+ Printer.printKeyword (" nonmutating" , Options, " " );
1921
+ break ;
1922
+ case SelfAccessKind::Consuming:
1923
+ if (!Options.excludeAttrKind (DAK_Consuming))
1924
+ Printer.printKeyword (" __consuming" , Options, " " );
1925
+ break ;
1914
1926
}
1915
1927
}
1916
1928
@@ -2037,7 +2049,7 @@ void PrintAST::printAccessors(const AbstractStorageDecl *ASD) {
2037
2049
return true ;
2038
2050
if (!PrintAccessorBody) {
2039
2051
Printer << " " ;
2040
- printMutabilityModifiersIfNeeded (Accessor);
2052
+ printSelfAccessKindModifiersIfNeeded (Accessor);
2041
2053
2042
2054
Printer.printKeyword (getAccessorLabel (Accessor->getAccessorKind ()), Options);
2043
2055
@@ -2271,7 +2283,7 @@ static void getModuleEntities(ImportDecl *Import,
2271
2283
2272
2284
void PrintAST::visitImportDecl (ImportDecl *decl) {
2273
2285
printAttributes (decl);
2274
- Printer << tok::kw_import << " " ;
2286
+ Printer. printIntroducerKeyword ( " import " , Options, " " ) ;
2275
2287
2276
2288
switch (decl->getImportKind ()) {
2277
2289
case ImportKind::Module:
@@ -2391,7 +2403,7 @@ void PrintAST::printSynthesizedExtension(Type ExtendedType,
2391
2403
if (Options.BracketOptions .shouldOpenExtension (ExtDecl)) {
2392
2404
printDocumentationComment (ExtDecl);
2393
2405
printAttributes (ExtDecl);
2394
- Printer << tok::kw_extension << " " ;
2406
+ Printer. printIntroducerKeyword ( " extension " , Options, " " ) ;
2395
2407
2396
2408
printExtendedTypeName (TypeLoc::withoutLoc (ExtendedType));
2397
2409
printInherited (ExtDecl);
@@ -2429,7 +2441,7 @@ void PrintAST::printExtension(ExtensionDecl *decl) {
2429
2441
if (Options.BracketOptions .shouldOpenExtension (decl)) {
2430
2442
printDocumentationComment (decl);
2431
2443
printAttributes (decl);
2432
- Printer << " extension " ;
2444
+ Printer. printIntroducerKeyword ( " extension " , Options, " " ) ;
2433
2445
recordDeclLoc (decl, [&]{
2434
2446
// We cannot extend sugared types.
2435
2447
Type extendedType = decl->getExtendedType ();
@@ -2961,8 +2973,7 @@ void PrintAST::visitTypeAliasDecl(TypeAliasDecl *decl) {
2961
2973
printDocumentationComment (decl);
2962
2974
printAttributes (decl);
2963
2975
printAccess (decl);
2964
- if (!Options.SkipIntroducerKeywords )
2965
- Printer << tok::kw_typealias << " " ;
2976
+ Printer.printIntroducerKeyword (" typealias" , Options, " " );
2966
2977
printContextIfNeeded (decl);
2967
2978
recordDeclLoc (decl,
2968
2979
[&]{
@@ -3005,8 +3016,7 @@ void PrintAST::visitGenericTypeParamDecl(GenericTypeParamDecl *decl) {
3005
3016
void PrintAST::visitAssociatedTypeDecl (AssociatedTypeDecl *decl) {
3006
3017
printDocumentationComment (decl);
3007
3018
printAttributes (decl);
3008
- if (!Options.SkipIntroducerKeywords )
3009
- Printer << tok::kw_associatedtype << " " ;
3019
+ Printer.printIntroducerKeyword (" associatedtype" , Options, " " );
3010
3020
recordDeclLoc (decl,
3011
3021
[&]{
3012
3022
Printer.printName (decl->getName (), PrintNameContext::TypeMember);
@@ -3035,8 +3045,7 @@ void PrintAST::visitEnumDecl(EnumDecl *decl) {
3035
3045
printSourceRange (CharSourceRange (Ctx.SourceMgr , decl->getStartLoc (),
3036
3046
decl->getBraces ().Start .getAdvancedLoc (-1 )), Ctx);
3037
3047
} else {
3038
- if (!Options.SkipIntroducerKeywords )
3039
- Printer << tok::kw_enum << " " ;
3048
+ Printer.printIntroducerKeyword (" enum" , Options, " " );
3040
3049
printContextIfNeeded (decl);
3041
3050
recordDeclLoc (decl,
3042
3051
[&]{
@@ -3063,8 +3072,7 @@ void PrintAST::visitStructDecl(StructDecl *decl) {
3063
3072
printSourceRange (CharSourceRange (Ctx.SourceMgr , decl->getStartLoc (),
3064
3073
decl->getBraces ().Start .getAdvancedLoc (-1 )), Ctx);
3065
3074
} else {
3066
- if (!Options.SkipIntroducerKeywords )
3067
- Printer << tok::kw_struct << " " ;
3075
+ Printer.printIntroducerKeyword (" struct" , Options, " " );
3068
3076
printContextIfNeeded (decl);
3069
3077
recordDeclLoc (decl,
3070
3078
[&]{
@@ -3091,13 +3099,8 @@ void PrintAST::visitClassDecl(ClassDecl *decl) {
3091
3099
printSourceRange (CharSourceRange (Ctx.SourceMgr , decl->getStartLoc (),
3092
3100
decl->getBraces ().Start .getAdvancedLoc (-1 )), Ctx);
3093
3101
} else {
3094
- if (!Options.SkipIntroducerKeywords ) {
3095
- if (decl->isExplicitActor ()) {
3096
- Printer.printKeyword (" actor" , Options, " " );
3097
- } else {
3098
- Printer << tok::kw_class << " " ;
3099
- }
3100
- }
3102
+ Printer.printIntroducerKeyword (
3103
+ decl->isExplicitActor () ? " actor" : " class" , Options, " " );
3101
3104
printContextIfNeeded (decl);
3102
3105
recordDeclLoc (decl,
3103
3106
[&]{
@@ -3126,8 +3129,7 @@ void PrintAST::visitProtocolDecl(ProtocolDecl *decl) {
3126
3129
printSourceRange (CharSourceRange (Ctx.SourceMgr , decl->getStartLoc (),
3127
3130
decl->getBraces ().Start .getAdvancedLoc (-1 )), Ctx);
3128
3131
} else {
3129
- if (!Options.SkipIntroducerKeywords )
3130
- Printer << tok::kw_protocol << " " ;
3132
+ Printer.printIntroducerKeyword (" protocol" , Options, " " );
3131
3133
printContextIfNeeded (decl);
3132
3134
recordDeclLoc (decl,
3133
3135
[&]{
@@ -3207,20 +3209,12 @@ void PrintAST::visitVarDecl(VarDecl *decl) {
3207
3209
Printer << " @_hasStorage " ;
3208
3210
printAttributes (decl);
3209
3211
printAccess (decl);
3210
- if (!Options.SkipIntroducerKeywords ) {
3211
- if (decl->isStatic () && Options.PrintStaticKeyword )
3212
- printStaticKeyword (decl->getCorrectStaticSpelling ());
3213
- if (decl->getKind () == DeclKind::Var
3214
- || Options.PrintParameterSpecifiers ) {
3215
- // Map all non-let specifiers to 'var'. This is not correct, but
3216
- // SourceKit relies on this for info about parameter decls.
3217
- if (decl->isLet ())
3218
- Printer << tok::kw_let;
3219
- else
3220
- Printer << tok::kw_var;
3221
-
3222
- Printer << " " ;
3223
- }
3212
+ if (decl->isStatic () && Options.PrintStaticKeyword )
3213
+ printStaticKeyword (decl->getCorrectStaticSpelling ());
3214
+ if (decl->getKind () == DeclKind::Var || Options.PrintParameterSpecifiers ) {
3215
+ // Map all non-let specifiers to 'var'. This is not correct, but
3216
+ // SourceKit relies on this for info about parameter decls.
3217
+ Printer.printIntroducerKeyword (decl->isLet () ? " let" : " var" , Options, " " );
3224
3218
}
3225
3219
printContextIfNeeded (decl);
3226
3220
recordDeclLoc (decl,
@@ -3431,10 +3425,8 @@ void PrintAST::visitAccessorDecl(AccessorDecl *decl) {
3431
3425
printDocumentationComment (decl);
3432
3426
printAttributes (decl);
3433
3427
// Explicitly print 'mutating' and 'nonmutating' if needed.
3434
- printMutabilityModifiersIfNeeded (decl);
3435
- if (decl->isConsuming ()) {
3436
- Printer.printKeyword (" __consuming" , Options, " " );
3437
- }
3428
+ printSelfAccessKindModifiersIfNeeded (decl);
3429
+
3438
3430
switch (auto kind = decl->getAccessorKind ()) {
3439
3431
case AccessorKind::Get:
3440
3432
case AccessorKind::Address:
@@ -3492,16 +3484,12 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
3492
3484
SourceRange (StartLoc, EndLoc));
3493
3485
printSourceRange (Range, Ctx);
3494
3486
} else {
3495
- if (!Options.SkipIntroducerKeywords ) {
3496
- if (decl->isStatic () && Options.PrintStaticKeyword )
3497
- printStaticKeyword (decl->getCorrectStaticSpelling ());
3487
+ if (decl->isStatic () && Options.PrintStaticKeyword )
3488
+ printStaticKeyword (decl->getCorrectStaticSpelling ());
3489
+
3490
+ printSelfAccessKindModifiersIfNeeded (decl);
3491
+ Printer.printIntroducerKeyword (" func" , Options, " " );
3498
3492
3499
- printMutabilityModifiersIfNeeded (decl);
3500
- if (decl->isConsuming () && !decl->getAttrs ().hasAttribute <ConsumingAttr>()) {
3501
- Printer.printKeyword (" __consuming" , Options, " " );
3502
- }
3503
- Printer << tok::kw_func << " " ;
3504
- }
3505
3493
printContextIfNeeded (decl);
3506
3494
recordDeclLoc (decl,
3507
3495
[&]{ // Name
@@ -3659,7 +3647,7 @@ void PrintAST::visitEnumCaseDecl(EnumCaseDecl *decl) {
3659
3647
printDocumentationComment (elems[0 ]);
3660
3648
printAttributes (elems[0 ]);
3661
3649
}
3662
- Printer << tok::kw_case << " " ;
3650
+ Printer. printIntroducerKeyword ( " case " , Options, " " ) ;
3663
3651
3664
3652
llvm::interleave (elems.begin (), elems.end (),
3665
3653
[&](EnumElementDecl *elt) {
@@ -3673,16 +3661,15 @@ void PrintAST::visitEnumElementDecl(EnumElementDecl *decl) {
3673
3661
// In cases where there is no parent EnumCaseDecl (such as imported or
3674
3662
// deserialized elements), print the element independently.
3675
3663
printAttributes (decl);
3676
- Printer << tok::kw_case << " " ;
3664
+ Printer. printIntroducerKeyword ( " case " , Options, " " ) ;
3677
3665
printEnumElement (decl);
3678
3666
}
3679
3667
3680
3668
void PrintAST::visitSubscriptDecl (SubscriptDecl *decl) {
3681
3669
printDocumentationComment (decl);
3682
3670
printAttributes (decl);
3683
3671
printAccess (decl);
3684
- if (!Options.SkipIntroducerKeywords && decl->isStatic () &&
3685
- Options.PrintStaticKeyword )
3672
+ if (decl->isStatic () && Options.PrintStaticKeyword )
3686
3673
printStaticKeyword (decl->getCorrectStaticSpelling ());
3687
3674
printContextIfNeeded (decl);
3688
3675
recordDeclLoc (decl, [&]{
@@ -3782,7 +3769,7 @@ void PrintAST::visitDestructorDecl(DestructorDecl *decl) {
3782
3769
3783
3770
void PrintAST::visitInfixOperatorDecl (InfixOperatorDecl *decl) {
3784
3771
Printer.printKeyword (" infix" , Options, " " );
3785
- Printer << tok::kw_operator << " " ;
3772
+ Printer. printIntroducerKeyword ( " operator " , Options, " " ) ;
3786
3773
recordDeclLoc (decl,
3787
3774
[&]{
3788
3775
Printer.printName (decl->getName ());
@@ -3801,7 +3788,7 @@ void PrintAST::visitInfixOperatorDecl(InfixOperatorDecl *decl) {
3801
3788
}
3802
3789
3803
3790
void PrintAST::visitPrecedenceGroupDecl (PrecedenceGroupDecl *decl) {
3804
- Printer << tok::kw_precedencegroup << " " ;
3791
+ Printer. printIntroducerKeyword ( " precedencegroup " , Options, " " ) ;
3805
3792
recordDeclLoc (decl,
3806
3793
[&]{
3807
3794
Printer.printName (decl->getName ());
@@ -3861,7 +3848,7 @@ void PrintAST::visitPrecedenceGroupDecl(PrecedenceGroupDecl *decl) {
3861
3848
3862
3849
void PrintAST::visitPrefixOperatorDecl (PrefixOperatorDecl *decl) {
3863
3850
Printer.printKeyword (" prefix" , Options, " " );
3864
- Printer << tok::kw_operator << " " ;
3851
+ Printer. printIntroducerKeyword ( " operator " , Options, " " ) ;
3865
3852
recordDeclLoc (decl,
3866
3853
[&]{
3867
3854
Printer.printName (decl->getName ());
@@ -3879,7 +3866,7 @@ void PrintAST::visitPrefixOperatorDecl(PrefixOperatorDecl *decl) {
3879
3866
3880
3867
void PrintAST::visitPostfixOperatorDecl (PostfixOperatorDecl *decl) {
3881
3868
Printer.printKeyword (" postfix" , Options, " " );
3882
- Printer << tok::kw_operator << " " ;
3869
+ Printer. printIntroducerKeyword ( " operator " , Options, " " ) ;
3883
3870
recordDeclLoc (decl,
3884
3871
[&]{
3885
3872
Printer.printName (decl->getName ());
0 commit comments