@@ -819,7 +819,7 @@ class PrintAST : public ASTVisitor<PrintAST> {
819
819
bool shouldPrintPattern (const Pattern *P);
820
820
void printPatternType (const Pattern *P);
821
821
void printAccessors (const AbstractStorageDecl *ASD);
822
- void printMutatingModifiersIfNeeded (const AccessorDecl *accessor );
822
+ void printMutabilityModifiersIfNeeded (const FuncDecl *FD );
823
823
void printMembersOfDecl (Decl * NTD, bool needComma = false ,
824
824
bool openBracket = true , bool closeBracket = true );
825
825
void printMembers (ArrayRef<Decl *> members, bool needComma = false ,
@@ -1008,9 +1008,6 @@ void PrintAST::printAttributes(const Decl *D) {
1008
1008
#define CONTEXTUAL_SIMPLE_DECL_ATTR (X, Class, Y, Z ) EXCLUDE_ATTR(Class)
1009
1009
#define CONTEXTUAL_DECL_ATTR_ALIAS (X, Class ) EXCLUDE_ATTR(Class)
1010
1010
#include " swift/AST/Attr.def"
1011
- } else if (isa<FuncDecl>(D)) {
1012
- Options.ExcludeAttrList .push_back (DAK_Mutating);
1013
- Options.ExcludeAttrList .push_back (DAK_NonMutating);
1014
1011
}
1015
1012
1016
1013
// If the declaration is implicitly @objc, print the attribute now.
@@ -1026,19 +1023,23 @@ void PrintAST::printAttributes(const Decl *D) {
1026
1023
// clients, or if it inherits superclass convenience initializers,
1027
1024
// then print those attributes specially.
1028
1025
if (auto CD = dyn_cast<ClassDecl>(D)) {
1029
- if (Options.PrintImplicitAttrs ) {
1030
- if (CD->inheritsSuperclassInitializers ()) {
1031
- Printer.printAttrName (" @_inheritsConvenienceInitializers" );
1032
- Printer << " " ;
1033
- }
1034
- if (CD->hasMissingDesignatedInitializers ()) {
1035
- Printer.printAttrName (" @_hasMissingDesignatedInitializers" );
1036
- Printer << " " ;
1037
- }
1026
+ if (CD->inheritsSuperclassInitializers ()) {
1027
+ Printer.printAttrName (" @_inheritsConvenienceInitializers" );
1028
+ Printer << " " ;
1029
+ }
1030
+ if (CD->hasMissingDesignatedInitializers ()) {
1031
+ Printer.printAttrName (" @_hasMissingDesignatedInitializers" );
1032
+ Printer << " " ;
1038
1033
}
1039
1034
}
1040
1035
}
1041
1036
1037
+ // We will handle 'mutating' and 'nonmutating' separately.
1038
+ if (isa<FuncDecl>(D)) {
1039
+ Options.ExcludeAttrList .push_back (DAK_Mutating);
1040
+ Options.ExcludeAttrList .push_back (DAK_NonMutating);
1041
+ }
1042
+
1042
1043
D->getAttrs ().print (Printer, Options, D);
1043
1044
1044
1045
// Print the implicit 'final' attribute.
@@ -1776,12 +1777,15 @@ void PrintAST::printBodyIfNecessary(const AbstractFunctionDecl *decl) {
1776
1777
printBraceStmt (decl->getBody (), /* newlineIfEmpty*/ !isa<AccessorDecl>(decl));
1777
1778
}
1778
1779
1779
- void PrintAST::printMutatingModifiersIfNeeded (const AccessorDecl *accessor) {
1780
- if (accessor->isAssumedNonMutating () && accessor->isMutating () &&
1781
- !Options.excludeAttrKind (DAK_Mutating)) {
1782
- Printer.printKeyword (" mutating" , Options, " " );
1783
- } else if (accessor->isExplicitNonMutating () &&
1784
- !Options.excludeAttrKind (DAK_NonMutating)) {
1780
+ void PrintAST::printMutabilityModifiersIfNeeded (const FuncDecl *FD) {
1781
+ const auto *AD = dyn_cast<AccessorDecl>(FD);
1782
+
1783
+ if (FD->isMutating ()) {
1784
+ if (AD == nullptr || AD->isAssumedNonMutating ())
1785
+ if (!Options.excludeAttrKind (DAK_Mutating))
1786
+ Printer.printKeyword (" mutating" , Options, " " );
1787
+ } else if (AD && AD->isExplicitNonMutating () &&
1788
+ !Options.excludeAttrKind (DAK_Mutating)) {
1785
1789
Printer.printKeyword (" nonmutating" , Options, " " );
1786
1790
}
1787
1791
}
@@ -1899,7 +1903,7 @@ void PrintAST::printAccessors(const AbstractStorageDecl *ASD) {
1899
1903
return true ;
1900
1904
if (!PrintAccessorBody) {
1901
1905
Printer << " " ;
1902
- printMutatingModifiersIfNeeded (Accessor);
1906
+ printMutabilityModifiersIfNeeded (Accessor);
1903
1907
Printer.printKeyword (getAccessorLabel (Accessor->getAccessorKind ()), Options);
1904
1908
} else {
1905
1909
{
@@ -2795,10 +2799,9 @@ bool PrintAST::printASTNodes(const ArrayRef<ASTNode> &Elements,
2795
2799
void PrintAST::visitAccessorDecl (AccessorDecl *decl) {
2796
2800
printDocumentationComment (decl);
2797
2801
printAttributes (decl);
2802
+ // Explicitly print 'mutating' and 'nonmutating' if needed.
2803
+ printMutabilityModifiersIfNeeded (decl);
2798
2804
2799
- // Explicitly print 'mutating' and 'nonmutating' before getters and setters
2800
- // for which that is true.
2801
- printMutatingModifiersIfNeeded (decl);
2802
2805
switch (auto kind = decl->getAccessorKind ()) {
2803
2806
case AccessorKind::Get:
2804
2807
case AccessorKind::Address:
@@ -2855,9 +2858,9 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
2855
2858
if (!Options.SkipIntroducerKeywords ) {
2856
2859
if (decl->isStatic () && Options.PrintStaticKeyword )
2857
2860
printStaticKeyword (decl->getCorrectStaticSpelling ());
2858
- if (decl-> isMutating () && !Options. excludeAttrKind (DAK_Mutating)) {
2859
- Printer. printKeyword ( " mutating " , Options, " " );
2860
- } else if (decl->isConsuming () && !decl->getAttrs ().hasAttribute <ConsumingAttr>()) {
2861
+
2862
+ printMutabilityModifiersIfNeeded (decl );
2863
+ if (decl->isConsuming () && !decl->getAttrs ().hasAttribute <ConsumingAttr>()) {
2861
2864
Printer.printKeyword (" __consuming" , Options, " " );
2862
2865
}
2863
2866
Printer << tok::kw_func << " " ;
0 commit comments