Skip to content

Commit 0bba7d9

Browse files
authored
ASTPrinter: Remove ASTPrinter's dependency on Sema. (#10985)
1 parent b058ed8 commit 0bba7d9

File tree

9 files changed

+139
-102
lines changed

9 files changed

+139
-102
lines changed

include/swift/AST/ASTPrinter.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,6 @@ class ExtraIndentStreamPrinter : public StreamPrinter {
305305
}
306306
};
307307

308-
bool shouldPrint(const Decl *D, PrintOptions &Options);
309-
bool shouldPrintPattern(const Pattern *P, PrintOptions &Options);
310-
311308
void printContext(raw_ostream &os, DeclContext *dc);
312309

313310
bool printRequirementStub(ValueDecl *Requirement, DeclContext *Adopter,

include/swift/AST/PrintOptions.h

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ namespace swift {
2424
class GenericEnvironment;
2525
class CanType;
2626
class Decl;
27+
class Pattern;
2728
class ValueDecl;
2829
class ExtensionDecl;
2930
class NominalTypeDecl;
@@ -49,32 +50,6 @@ struct TypeTransformContext {
4950
bool isPrintingSynthesizedExtension() const;
5051
};
5152

52-
typedef std::pair<ExtensionDecl*, bool> ExtensionAndIsSynthesized;
53-
typedef llvm::function_ref<void(ArrayRef<ExtensionAndIsSynthesized>)>
54-
ExtensionGroupOperation;
55-
56-
class SynthesizedExtensionAnalyzer {
57-
struct Implementation;
58-
Implementation &Impl;
59-
public:
60-
SynthesizedExtensionAnalyzer(NominalTypeDecl *Target,
61-
PrintOptions Options,
62-
bool IncludeUnconditional = true);
63-
~SynthesizedExtensionAnalyzer();
64-
65-
enum class MergeGroupKind : char {
66-
All,
67-
MergeableWithTypeDef,
68-
UnmergeableWithTypeDef,
69-
};
70-
71-
void forEachExtensionMergeGroup(MergeGroupKind Kind,
72-
ExtensionGroupOperation Fn);
73-
bool isInSynthesizedExtension(const ValueDecl *VD);
74-
bool shouldPrintRequirement(ExtensionDecl *ED, StringRef Req);
75-
bool hasMergeGroup(MergeGroupKind Kind);
76-
};
77-
7853
class BracketOptions {
7954
Decl* Target;
8055
bool OpenExtension;
@@ -131,6 +106,12 @@ class AnyAttrKind {
131106
bool operator!=(AnyAttrKind K) const { return !(*this == K); }
132107
};
133108

109+
struct ShouldPrintChecker {
110+
virtual bool shouldPrint(const Decl *D, PrintOptions &Options);
111+
bool shouldPrint(const Pattern *P, PrintOptions &Options);
112+
virtual ~ShouldPrintChecker() = default;
113+
};
114+
134115
/// Options for printing AST nodes.
135116
///
136117
/// A default-constructed PrintOptions is suitable for printing to users;
@@ -294,6 +275,9 @@ struct PrintOptions {
294275
/// Whether to print the extensions from conforming protocols.
295276
bool PrintExtensionFromConformingProtocols = false;
296277

278+
std::shared_ptr<ShouldPrintChecker> CurrentPrintabilityChecker =
279+
std::make_shared<ShouldPrintChecker>();
280+
297281
enum class ArgAndParamPrintingMode {
298282
ArgumentOnly,
299283
MatchSource,
@@ -423,6 +407,7 @@ struct PrintOptions {
423407
return result;
424408
}
425409

410+
static PrintOptions printModuleInterface();
426411
static PrintOptions printTypeInterface(Type T);
427412

428413
void setBaseType(Type T);
@@ -431,6 +416,13 @@ struct PrintOptions {
431416

432417
void clearSynthesizedExtension();
433418

419+
bool shouldPrint(const Decl* D) {
420+
return CurrentPrintabilityChecker->shouldPrint(D, *this);
421+
}
422+
bool shouldPrint(const Pattern* P) {
423+
return CurrentPrintabilityChecker->shouldPrint(P, *this);
424+
}
425+
434426
/// Retrieve the print options that are suitable to print the testable interface.
435427
static PrintOptions printTestableInterface() {
436428
PrintOptions result = printInterface();
@@ -449,19 +441,7 @@ struct PrintOptions {
449441

450442
/// Retrieve the set of options suitable for interface generation for
451443
/// documentation purposes.
452-
static PrintOptions printDocInterface() {
453-
PrintOptions result = PrintOptions::printInterface();
454-
result.PrintAccessibility = false;
455-
result.SkipUnavailable = false;
456-
result.ExcludeAttrList.push_back(DAK_Available);
457-
result.ArgAndParamPrinting =
458-
PrintOptions::ArgAndParamPrintingMode::BothAlways;
459-
result.PrintDocumentationComments = false;
460-
result.PrintRegularClangComments = false;
461-
result.PrintAccessibility = false;
462-
result.PrintFunctionRepresentationAttrs = false;
463-
return result;
464-
}
444+
static PrintOptions printDocInterface();
465445

466446
/// Retrieve the set of options suitable for printing SIL functions.
467447
static PrintOptions printSIL() {

include/swift/Sema/IDETypeChecking.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ namespace swift {
2929
class LazyResolver;
3030
class ExtensionDecl;
3131
class ProtocolDecl;
32+
class Type;
33+
class DeclContext;
34+
class ConcreteDeclRef;
35+
class ValueDecl;
36+
class DeclName;
3237

3338
/// \brief Typecheck a declaration parsed during code completion.
3439
///
@@ -131,6 +136,33 @@ namespace swift {
131136

132137
/// Creates a lazy type resolver for use in lookups.
133138
OwnedResolver createLazyResolver(ASTContext &Ctx);
139+
140+
typedef std::pair<ExtensionDecl*, bool> ExtensionAndIsSynthesized;
141+
142+
typedef llvm::function_ref<void(ArrayRef<ExtensionAndIsSynthesized>)>
143+
ExtensionGroupOperation;
144+
145+
class SynthesizedExtensionAnalyzer {
146+
struct Implementation;
147+
Implementation &Impl;
148+
public:
149+
SynthesizedExtensionAnalyzer(NominalTypeDecl *Target,
150+
PrintOptions Options,
151+
bool IncludeUnconditional = true);
152+
~SynthesizedExtensionAnalyzer();
153+
154+
enum class MergeGroupKind : char {
155+
All,
156+
MergeableWithTypeDef,
157+
UnmergeableWithTypeDef,
158+
};
159+
160+
void forEachExtensionMergeGroup(MergeGroupKind Kind,
161+
ExtensionGroupOperation Fn);
162+
bool isInSynthesizedExtension(const ValueDecl *VD);
163+
bool shouldPrintRequirement(ExtensionDecl *ED, StringRef Req);
164+
bool hasMergeGroup(MergeGroupKind Kind);
165+
};
134166
}
135167

136168
#endif

lib/AST/ASTPrinter.cpp

Lines changed: 21 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
#include "swift/Basic/StringExtras.h"
3737
#include "swift/Config.h"
3838
#include "swift/Parse/Lexer.h"
39-
#include "swift/Sema/IDETypeChecking.h"
4039
#include "swift/Strings.h"
4140
#include "clang/AST/ASTContext.h"
4241
#include "clang/AST/Decl.h"
@@ -52,17 +51,6 @@
5251

5352
using namespace swift;
5453

55-
PrintOptions PrintOptions::printTypeInterface(Type T) {
56-
PrintOptions result = printInterface();
57-
result.PrintExtensionFromConformingProtocols = true;
58-
result.TransformContext = TypeTransformContext(T);
59-
result.printExtensionContentAsMembers = [T](const ExtensionDecl *ED) {
60-
return isExtensionApplied(*T->getNominalOrBoundGenericNominal()->
61-
getDeclContext(), T, ED);
62-
};
63-
return result;
64-
}
65-
6654
void PrintOptions::setBaseType(Type T) {
6755
TransformContext = TypeTransformContext(T);
6856
}
@@ -830,17 +818,17 @@ static const unsigned ErrorDepth = ~0U;
830818
/// A helper function to return the depth of a type.
831819
static unsigned getDepthOfType(Type ty) {
832820
unsigned depth = ErrorDepth;
833-
821+
834822
auto combineDepth = [&depth](unsigned newDepth) -> bool {
835823
// If there is no current depth (depth == ErrorDepth), then assign to
836824
// newDepth; otherwise, choose the deeper of the current and new depth.
837-
825+
838826
// Since ErrorDepth == ~0U, ErrorDepth + 1 == 0, which is smaller than any
839827
// valid depth + 1.
840828
depth = std::max(depth+1U, newDepth+1U) - 1U;
841829
return false;
842830
};
843-
831+
844832
ty.findIf([combineDepth](Type t) -> bool {
845833
if (auto paramTy = t->getAs<GenericTypeParamType>())
846834
return combineDepth(paramTy->getDepth());
@@ -856,7 +844,7 @@ static unsigned getDepthOfType(Type ty) {
856844

857845
return false;
858846
});
859-
847+
860848
return depth;
861849
}
862850

@@ -1222,16 +1210,8 @@ void PrintAST::printRequirement(const Requirement &req) {
12221210
printType(req.getSecondType());
12231211
}
12241212

1225-
bool swift::shouldPrintPattern(const Pattern *P, PrintOptions &Options) {
1226-
bool ShouldPrint = false;
1227-
P->forEachVariable([&](VarDecl *VD) {
1228-
ShouldPrint |= shouldPrint(VD, Options);
1229-
});
1230-
return ShouldPrint;
1231-
}
1232-
12331213
bool PrintAST::shouldPrintPattern(const Pattern *P) {
1234-
return swift::shouldPrintPattern(P, Options);
1214+
return Options.shouldPrint(P);
12351215
}
12361216

12371217
void PrintAST::printPatternType(const Pattern *P) {
@@ -1241,24 +1221,15 @@ void PrintAST::printPatternType(const Pattern *P) {
12411221
}
12421222
}
12431223

1244-
static bool shouldPrintAsFavorable(const Decl *D, PrintOptions &Options) {
1245-
if (!Options.TransformContext || !D->getDeclContext()->isExtensionContext() ||
1246-
!Options.TransformContext->isPrintingSynthesizedExtension())
1247-
return true;
1248-
NominalTypeDecl *Target = Options.TransformContext->getNominal();
1249-
Type BaseTy = Target->getDeclaredTypeInContext();
1250-
const auto *FD = dyn_cast<FuncDecl>(D);
1251-
if (!FD)
1252-
return true;
1253-
ResolvedMemberResult Result = resolveValueMember(*Target->getDeclContext(),
1254-
BaseTy,
1255-
FD->getEffectiveFullName());
1256-
return !(Result.hasBestOverload() && Result.getBestOverload() != D);
1224+
bool ShouldPrintChecker::shouldPrint(const Pattern *P, PrintOptions &Options) {
1225+
bool ShouldPrint = false;
1226+
P->forEachVariable([&](const VarDecl *VD) {
1227+
ShouldPrint |= shouldPrint(VD, Options);
1228+
});
1229+
return ShouldPrint;
12571230
}
12581231

1259-
bool swift::shouldPrint(const Decl *D, PrintOptions &Options) {
1260-
if (!shouldPrintAsFavorable(D, Options))
1261-
return false;
1232+
bool ShouldPrintChecker::shouldPrint(const Decl *D, PrintOptions &Options) {
12621233
if (auto *ED= dyn_cast<ExtensionDecl>(D)) {
12631234
if (Options.printExtensionContentAsMembers(ED))
12641235
return false;
@@ -1358,7 +1329,7 @@ bool swift::shouldPrint(const Decl *D, PrintOptions &Options) {
13581329
if (auto *PD = dyn_cast<PatternBindingDecl>(D)) {
13591330
auto ShouldPrint = false;
13601331
for (auto entry : PD->getPatternList()) {
1361-
ShouldPrint |= shouldPrintPattern(entry.getPattern(), Options);
1332+
ShouldPrint |= shouldPrint(entry.getPattern(), Options);
13621333
if (ShouldPrint)
13631334
return true;
13641335
}
@@ -1368,7 +1339,7 @@ bool swift::shouldPrint(const Decl *D, PrintOptions &Options) {
13681339
}
13691340

13701341
bool PrintAST::shouldPrint(const Decl *D, bool Notify) {
1371-
auto Result = swift::shouldPrint(D, Options);
1342+
auto Result = Options.shouldPrint(D);
13721343
if (!Result && Notify)
13731344
Printer.callAvoidPrintDeclPost(D);
13741345
return Result;
@@ -3600,7 +3571,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
36003571
};
36013572

36023573
printFunctionExtInfo(T->getExtInfo());
3603-
3574+
36043575
// If we're stripping argument labels from types, do it when printing.
36053576
Type inputType = T->getInput();
36063577
if (auto tupleTy = dyn_cast<TupleType>(inputType.getPointer())) {
@@ -3614,15 +3585,15 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
36143585
bool needsParens =
36153586
!isa<ParenType>(inputType.getPointer()) &&
36163587
!inputType->is<TupleType>();
3617-
3588+
36183589
if (needsParens)
36193590
Printer << "(";
36203591

36213592
visit(inputType);
3622-
3593+
36233594
if (needsParens)
36243595
Printer << ")";
3625-
3596+
36263597
if (T->throws())
36273598
Printer << " " << tok::kw_throws;
36283599

@@ -3653,7 +3624,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
36533624
bool needsParens =
36543625
!isa<ParenType>(T->getInput().getPointer()) &&
36553626
!T->getInput()->is<TupleType>();
3656-
3627+
36573628
if (needsParens)
36583629
Printer << "(";
36593630

@@ -3742,7 +3713,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
37423713
PrintOptions subOptions = Options;
37433714
subOptions.GenericEnv = nullptr;
37443715
TypePrinter sub(Printer, subOptions);
3745-
3716+
37463717
// Capture list used here to ensure we don't print anything using `this`
37473718
// printer, but only the sub-Printer.
37483719
[&sub, T]{
@@ -3764,7 +3735,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
37643735
sub.Printer << " }";
37653736
}();
37663737
}
3767-
3738+
37683739
// The arguments to the layout, if any, do come from the outer environment.
37693740
if (!T->getGenericArgs().empty()) {
37703741
Printer << " <";

0 commit comments

Comments
 (0)