@@ -81,6 +81,15 @@ PrintOptions PrintOptions::printTextualInterfaceFile() {
8181 result.SkipImports = true ;
8282 result.OmitNameOfInaccessibleProperties = true ;
8383
84+ result.FunctionBody = [](const ValueDecl *decl, ASTPrinter &printer) {
85+ auto AFD = dyn_cast<AbstractFunctionDecl>(decl);
86+ if (!AFD || !AFD->hasInlinableBodyText ()) return ;
87+ if (AFD->getResilienceExpansion () != ResilienceExpansion::Minimal)
88+ return ;
89+ SmallString<128 > scratch;
90+ printer << " " << AFD->getInlinableBodyText (scratch);
91+ };
92+
8493 class ShouldPrintForTextualInterface : public ShouldPrintChecker {
8594 bool shouldPrint (const Decl *D, const PrintOptions &options) override {
8695 // Skip anything that isn't 'public' or '@usableFromInline'.
@@ -645,6 +654,7 @@ class PrintAST : public ASTVisitor<PrintAST> {
645654
646655 void printAttributes (const Decl *D);
647656 void printTypedPattern (const TypedPattern *TP);
657+ void printBraceStmt (const BraceStmt *stmt, bool newlineIfEmpty = true );
648658
649659public:
650660 void printPattern (const Pattern *pattern);
@@ -686,6 +696,7 @@ class PrintAST : public ASTVisitor<PrintAST> {
686696 void printGenericDeclGenericParams (GenericContext *decl);
687697 void printGenericDeclGenericRequirements (GenericContext *decl);
688698 void printInherited (const Decl *decl);
699+ void printBodyIfNecessary (const AbstractFunctionDecl *decl);
689700
690701 void printEnumElement (EnumElementDecl *elt);
691702
@@ -1471,6 +1482,29 @@ bool PrintAST::shouldPrint(const Decl *D, bool Notify) {
14711482 return Result;
14721483}
14731484
1485+ void PrintAST::printBraceStmt (const BraceStmt *stmt, bool newlineIfEmpty) {
1486+ Printer << " {" ;
1487+ if (printASTNodes (stmt->getElements ()) || newlineIfEmpty) {
1488+ Printer.printNewline ();
1489+ indent ();
1490+ }
1491+ Printer << " }" ;
1492+ }
1493+
1494+ void PrintAST::printBodyIfNecessary (const AbstractFunctionDecl *decl) {
1495+ if (auto BodyFunc = Options.FunctionBody ) {
1496+ BodyFunc (decl, Printer);
1497+ indent ();
1498+ return ;
1499+ }
1500+
1501+ if (!Options.FunctionDefinitions || !decl->getBody ())
1502+ return ;
1503+
1504+ Printer << " " ;
1505+ printBraceStmt (decl->getBody (), /* newlineIfEmpty*/ !isa<AccessorDecl>(decl));
1506+ }
1507+
14741508static bool isAccessorAssumedNonMutating (AccessorKind kind) {
14751509 switch (kind) {
14761510 case AccessorKind::Get:
@@ -1630,26 +1664,37 @@ void PrintAST::printAccessors(AbstractStorageDecl *ASD) {
16301664 Printer << " " ;
16311665 Printer.printKeyword (getAccessorLabel (Accessor));
16321666 } else {
1633- Printer.printNewline ();
1634- IndentRAII IndentMore (*this );
1667+ {
1668+ IndentRAII IndentMore (*this );
1669+ indent ();
1670+ visit (Accessor);
1671+ }
16351672 indent ();
1636- visit (Accessor );
1673+ Printer. printNewline ( );
16371674 }
16381675 };
16391676
1640- Printer << " {" ;
16411677 if ((PrintAbstract ||
16421678 (impl.getReadImpl () == ReadImplKind::Get && ASD->getGetter ())) &&
16431679 !ASD->supportsMutation () && !ASD->isGetterMutating () &&
16441680 PrintAccessorBody && !Options.FunctionDefinitions ) {
16451681 // Omit the 'get' keyword. Directly print getter
16461682 if (auto BodyFunc = Options.FunctionBody ) {
1647- Printer.printNewline ();
1648- IndentRAII IndentBody (*this );
1683+ BodyFunc (ASD->getGetter (), Printer);
16491684 indent ();
1650- Printer << BodyFunc (ASD-> getGetter ()) ;
1685+ return ;
16511686 }
1652- } else if (PrintAbstract) {
1687+ Printer.printNewline ();
1688+ indent ();
1689+ return ;
1690+ }
1691+
1692+ Printer << " {" ;
1693+
1694+ if (PrintAccessorBody)
1695+ Printer.printNewline ();
1696+
1697+ if (PrintAbstract) {
16531698 PrintAccessor (ASD->getGetter ());
16541699 if (ASD->supportsMutation ())
16551700 PrintAccessor (ASD->getSetter ());
@@ -1693,12 +1738,16 @@ void PrintAST::printAccessors(AbstractStorageDecl *ASD) {
16931738 break ;
16941739 }
16951740 }
1696- if (PrintAccessorBody) {
1697- Printer.printNewline ();
1698- indent ();
1699- } else
1741+
1742+ if (!PrintAccessorBody)
17001743 Printer << " " ;
1744+
17011745 Printer << " }" ;
1746+
1747+ if (!PrintAbstract)
1748+ Printer.printNewline ();
1749+
1750+ indent ();
17021751}
17031752
17041753void PrintAST::printMembersOfDecl (Decl *D, bool needComma,
@@ -2497,7 +2546,6 @@ void PrintAST::visitAccessorDecl(AccessorDecl *decl) {
24972546 [&]{
24982547 Printer << getAccessorLabel (decl);
24992548 });
2500- Printer << " {" ;
25012549 break ;
25022550 case AccessorKind::Set:
25032551 case AccessorKind::WillSet:
@@ -2515,24 +2563,9 @@ void PrintAST::visitAccessorDecl(AccessorDecl *decl) {
25152563 }
25162564 }
25172565 });
2518- Printer << " {" ;
25192566 }
2520- if (auto BodyFunc = Options.FunctionBody ) {
2521- {
2522- IndentRAII IndentBody (*this );
2523- indent ();
2524- Printer.printNewline ();
2525- Printer << BodyFunc (decl);
2526- }
2527- indent ();
2528- Printer.printNewline ();
2529- } else if (Options.FunctionDefinitions && decl->getBody ()) {
2530- if (printASTNodes (decl->getBody ()->getElements ())) {
2531- Printer.printNewline ();
2532- indent ();
2533- }
2534- }
2535- Printer << " }" ;
2567+
2568+ printBodyIfNecessary (decl);
25362569}
25372570
25382571void PrintAST::visitFuncDecl (FuncDecl *decl) {
@@ -2604,22 +2637,7 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
26042637 printGenericDeclGenericRequirements (decl);
26052638 }
26062639
2607- if (auto BodyFunc = Options.FunctionBody ) {
2608- Printer << " {" ;
2609- Printer.printNewline ();
2610- {
2611- IndentRAII IndentBody (*this );
2612- indent ();
2613- Printer << BodyFunc (decl);
2614- }
2615- indent ();
2616- Printer.printNewline ();
2617- Printer << " }" ;
2618-
2619- } else if (Options.FunctionDefinitions && decl->getBody ()) {
2620- Printer << " " ;
2621- visit (decl->getBody ());
2622- }
2640+ printBodyIfNecessary (decl);
26232641}
26242642
26252643void PrintAST::printEnumElement (EnumElementDecl *elt) {
@@ -2788,21 +2806,7 @@ void PrintAST::visitConstructorDecl(ConstructorDecl *decl) {
27882806
27892807 printGenericDeclGenericRequirements (decl);
27902808
2791- if (auto BodyFunc = Options.FunctionBody ) {
2792- Printer << " {" ;
2793- {
2794- Printer.printNewline ();
2795- IndentRAII IndentBody (*this );
2796- indent ();
2797- Printer << BodyFunc (decl);
2798- }
2799- indent ();
2800- Printer.printNewline ();
2801- Printer << " }" ;
2802- } else if (Options.FunctionDefinitions && decl->getBody ()) {
2803- Printer << " " ;
2804- visit (decl->getBody ());
2805- }
2809+ printBodyIfNecessary (decl);
28062810}
28072811
28082812void PrintAST::visitDestructorDecl (DestructorDecl *decl) {
@@ -2814,12 +2818,7 @@ void PrintAST::visitDestructorDecl(DestructorDecl *decl) {
28142818 Printer << " deinit" ;
28152819 });
28162820
2817- if (!Options.FunctionDefinitions || !decl->getBody ()) {
2818- return ;
2819- }
2820-
2821- Printer << " " ;
2822- visit (decl->getBody ());
2821+ printBodyIfNecessary (decl);
28232822}
28242823
28252824void PrintAST::visitInfixOperatorDecl (InfixOperatorDecl *decl) {
@@ -2929,11 +2928,7 @@ void PrintAST::visitMissingMemberDecl(MissingMemberDecl *decl) {
29292928}
29302929
29312930void PrintAST::visitBraceStmt (BraceStmt *stmt) {
2932- Printer << " {" ;
2933- printASTNodes (stmt->getElements ());
2934- Printer.printNewline ();
2935- indent ();
2936- Printer << " }" ;
2931+ printBraceStmt (stmt);
29372932}
29382933
29392934void PrintAST::visitReturnStmt (ReturnStmt *stmt) {
0 commit comments