@@ -812,7 +812,7 @@ class PrintAST : public ASTVisitor<PrintAST> {
812
812
bool shouldPrintPattern (const Pattern *P);
813
813
void printPatternType (const Pattern *P);
814
814
void printAccessors (const AbstractStorageDecl *ASD);
815
- void printMutatingModifiersIfNeeded (const AccessorDecl *accessor );
815
+ void printMutabilityModifiersIfNeeded (const FuncDecl *FD );
816
816
void printMembersOfDecl (Decl * NTD, bool needComma = false ,
817
817
bool openBracket = true , bool closeBracket = true );
818
818
void printMembers (ArrayRef<Decl *> members, bool needComma = false ,
@@ -990,9 +990,6 @@ void PrintAST::printAttributes(const Decl *D) {
990
990
#define CONTEXTUAL_SIMPLE_DECL_ATTR (X, Class, Y, Z ) EXCLUDE_ATTR(Class)
991
991
#define CONTEXTUAL_DECL_ATTR_ALIAS (X, Class ) EXCLUDE_ATTR(Class)
992
992
#include " swift/AST/Attr.def"
993
- } else if (isa<FuncDecl>(D)) {
994
- Options.ExcludeAttrList .push_back (DAK_Mutating);
995
- Options.ExcludeAttrList .push_back (DAK_NonMutating);
996
993
}
997
994
998
995
// If the declaration is implicitly @objc, print the attribute now.
@@ -1008,19 +1005,23 @@ void PrintAST::printAttributes(const Decl *D) {
1008
1005
// clients, or if it inherits superclass convenience initializers,
1009
1006
// then print those attributes specially.
1010
1007
if (auto CD = dyn_cast<ClassDecl>(D)) {
1011
- if (Options.PrintImplicitAttrs ) {
1012
- if (CD->inheritsSuperclassInitializers ()) {
1013
- Printer.printAttrName (" @_inheritsConvenienceInitializers" );
1014
- Printer << " " ;
1015
- }
1016
- if (CD->hasMissingDesignatedInitializers ()) {
1017
- Printer.printAttrName (" @_hasMissingDesignatedInitializers" );
1018
- Printer << " " ;
1019
- }
1008
+ if (CD->inheritsSuperclassInitializers ()) {
1009
+ Printer.printAttrName (" @_inheritsConvenienceInitializers" );
1010
+ Printer << " " ;
1011
+ }
1012
+ if (CD->hasMissingDesignatedInitializers ()) {
1013
+ Printer.printAttrName (" @_hasMissingDesignatedInitializers" );
1014
+ Printer << " " ;
1020
1015
}
1021
1016
}
1022
1017
}
1023
1018
1019
+ // We will handle 'mutating' and 'nonmutating' separately.
1020
+ if (isa<FuncDecl>(D)) {
1021
+ Options.ExcludeAttrList .push_back (DAK_Mutating);
1022
+ Options.ExcludeAttrList .push_back (DAK_NonMutating);
1023
+ }
1024
+
1024
1025
D->getAttrs ().print (Printer, Options, D);
1025
1026
1026
1027
// Print the implicit 'final' attribute.
@@ -1758,12 +1759,15 @@ void PrintAST::printBodyIfNecessary(const AbstractFunctionDecl *decl) {
1758
1759
printBraceStmt (decl->getBody (), /* newlineIfEmpty*/ !isa<AccessorDecl>(decl));
1759
1760
}
1760
1761
1761
- void PrintAST::printMutatingModifiersIfNeeded (const AccessorDecl *accessor) {
1762
- if (accessor->isAssumedNonMutating () && accessor->isMutating () &&
1763
- !Options.excludeAttrKind (DAK_Mutating)) {
1764
- Printer.printKeyword (" mutating" , Options, " " );
1765
- } else if (accessor->isExplicitNonMutating () &&
1766
- !Options.excludeAttrKind (DAK_NonMutating)) {
1762
+ void PrintAST::printMutabilityModifiersIfNeeded (const FuncDecl *FD) {
1763
+ const auto *AD = dyn_cast<AccessorDecl>(FD);
1764
+
1765
+ if (FD->isMutating ()) {
1766
+ if (AD == nullptr || AD->isAssumedNonMutating ())
1767
+ if (!Options.excludeAttrKind (DAK_Mutating))
1768
+ Printer.printKeyword (" mutating" , Options, " " );
1769
+ } else if (AD && AD->isExplicitNonMutating () &&
1770
+ !Options.excludeAttrKind (DAK_Mutating)) {
1767
1771
Printer.printKeyword (" nonmutating" , Options, " " );
1768
1772
}
1769
1773
}
@@ -1881,7 +1885,7 @@ void PrintAST::printAccessors(const AbstractStorageDecl *ASD) {
1881
1885
return true ;
1882
1886
if (!PrintAccessorBody) {
1883
1887
Printer << " " ;
1884
- printMutatingModifiersIfNeeded (Accessor);
1888
+ printMutabilityModifiersIfNeeded (Accessor);
1885
1889
Printer.printKeyword (getAccessorLabel (Accessor->getAccessorKind ()), Options);
1886
1890
} else {
1887
1891
{
@@ -2777,10 +2781,9 @@ bool PrintAST::printASTNodes(const ArrayRef<ASTNode> &Elements,
2777
2781
void PrintAST::visitAccessorDecl (AccessorDecl *decl) {
2778
2782
printDocumentationComment (decl);
2779
2783
printAttributes (decl);
2784
+ // Explicitly print 'mutating' and 'nonmutating' if needed.
2785
+ printMutabilityModifiersIfNeeded (decl);
2780
2786
2781
- // Explicitly print 'mutating' and 'nonmutating' before getters and setters
2782
- // for which that is true.
2783
- printMutatingModifiersIfNeeded (decl);
2784
2787
switch (auto kind = decl->getAccessorKind ()) {
2785
2788
case AccessorKind::Get:
2786
2789
case AccessorKind::Address:
@@ -2837,9 +2840,9 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
2837
2840
if (!Options.SkipIntroducerKeywords ) {
2838
2841
if (decl->isStatic () && Options.PrintStaticKeyword )
2839
2842
printStaticKeyword (decl->getCorrectStaticSpelling ());
2840
- if (decl-> isMutating () && !Options. excludeAttrKind (DAK_Mutating)) {
2841
- Printer. printKeyword ( " mutating " , Options, " " );
2842
- } else if (decl->isConsuming () && !decl->getAttrs ().hasAttribute <ConsumingAttr>()) {
2843
+
2844
+ printMutabilityModifiersIfNeeded (decl );
2845
+ if (decl->isConsuming () && !decl->getAttrs ().hasAttribute <ConsumingAttr>()) {
2843
2846
Printer.printKeyword (" __consuming" , Options, " " );
2844
2847
}
2845
2848
Printer << tok::kw_func << " " ;
0 commit comments