Skip to content

Commit b3a2762

Browse files
committed
Improve the dumping of ProtocolConformances, Substitutions,
and ErasureExpr.
1 parent 92848eb commit b3a2762

File tree

4 files changed

+68
-21
lines changed

4 files changed

+68
-21
lines changed

include/swift/AST/ProtocolConformance.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ class alignas(1 << DeclAlignInBits) ProtocolConformance {
286286
AbstractStorageDecl *getBehaviorDecl() const;
287287

288288
void dump() const;
289+
void dump(llvm::raw_ostream &out, unsigned indent = 0) const;
289290

290291
private:
291292
friend class Substitution;

include/swift/AST/ProtocolConformanceRef.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
#include "llvm/ADT/PointerUnion.h"
2121
#include "swift/AST/TypeAlignments.h"
2222

23+
namespace llvm {
24+
class raw_ostream;
25+
}
26+
2327
namespace swift {
2428

2529
/// A ProtocolConformanceRef is a handle to a protocol conformance which
@@ -80,6 +84,7 @@ class ProtocolConformanceRef {
8084
ProtocolDecl *getRequirement() const;
8185

8286
void dump() const;
87+
void dump(llvm::raw_ostream &out, unsigned indent = 0) const;
8388

8489
bool operator==(ProtocolConformanceRef other) const {
8590
return Union == other.Union;

include/swift/AST/Substitution.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class Substitution {
5757
void print(llvm::raw_ostream &os,
5858
const PrintOptions &PO = PrintOptions()) const;
5959
void dump() const;
60+
void dump(llvm::raw_ostream &os, unsigned indent = 0) const;
6061

6162
/// Substitute the replacement and conformance types with the given
6263
/// substitution vector.

lib/AST/ASTDumper.cpp

Lines changed: 61 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,6 +1504,9 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
15041504
PrintPattern(OS, Indent+2).visit(const_cast<Pattern *>(P));
15051505
}
15061506
void printRec(TypeRepr *T);
1507+
void printRec(ProtocolConformanceRef conf) {
1508+
conf.dump(OS, Indent + 2);
1509+
}
15071510

15081511
static const char *getAccessKindString(AccessKind kind) {
15091512
switch (kind) {
@@ -1868,6 +1871,10 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
18681871
}
18691872
void visitErasureExpr(ErasureExpr *E) {
18701873
printCommon(E, "erasure_expr") << '\n';
1874+
for (auto conf : E->getConformances()) {
1875+
printRec(conf);
1876+
OS << '\n';
1877+
}
18711878
printRec(E->getSubExpr());
18721879
OS << ')';
18731880
}
@@ -2385,32 +2392,30 @@ void TypeRepr::dump() const {
23852392
}
23862393

23872394
void Substitution::dump() const {
2388-
llvm::raw_ostream &os = llvm::errs();
2389-
2390-
print(os);
2391-
os << '\n';
2395+
dump(llvm::errs());
2396+
}
23922397

2393-
if (!Conformance.size()) return;
2398+
void Substitution::dump(llvm::raw_ostream &out, unsigned indent) const {
2399+
out.indent(indent);
2400+
print(out);
2401+
out << '\n';
23942402

2395-
os << '[';
2396-
bool first = true;
23972403
for (auto &c : Conformance) {
2398-
if (first) {
2399-
first = false;
2400-
} else {
2401-
os << ' ';
2402-
}
2403-
c.dump();
2404+
c.dump(out, indent + 2);
24042405
}
2405-
os << " ]";
24062406
}
24072407

24082408
void ProtocolConformanceRef::dump() const {
2409-
llvm::raw_ostream &os = llvm::errs();
2409+
dump(llvm::errs());
2410+
}
2411+
2412+
void ProtocolConformanceRef::dump(llvm::raw_ostream &out,
2413+
unsigned indent) const {
24102414
if (isConcrete()) {
2411-
getConcrete()->printName(os);
2415+
getConcrete()->dump(out, indent);
24122416
} else {
2413-
os << "abstract:" << getAbstract()->getName();
2417+
out.indent(indent) << "(abstract_conformance protocol="
2418+
<< getAbstract()->getName() << ')';
24142419
}
24152420
}
24162421

@@ -2423,10 +2428,45 @@ void swift::dump(const ArrayRef<Substitution> &subs) {
24232428
}
24242429

24252430
void ProtocolConformance::dump() const {
2426-
// FIXME: If we ever write a full print() method for ProtocolConformance, use
2427-
// that.
2428-
printName(llvm::errs());
2429-
llvm::errs() << '\n';
2431+
auto &out = llvm::errs();
2432+
dump(out);
2433+
out << '\n';
2434+
}
2435+
2436+
void ProtocolConformance::dump(llvm::raw_ostream &out, unsigned indent) const {
2437+
auto printCommon = [&](StringRef kind) {
2438+
out.indent(indent) << '(' << kind << "_conformance type=" << getType()
2439+
<< " protocol=" << getProtocol()->getName();
2440+
};
2441+
2442+
switch (getKind()) {
2443+
case ProtocolConformanceKind::Normal:
2444+
printCommon("normal");
2445+
// Maybe print information about the conforming context?
2446+
break;
2447+
2448+
case ProtocolConformanceKind::Inherited: {
2449+
auto conf = cast<InheritedProtocolConformance>(this);
2450+
printCommon("inherited");
2451+
out << '\n';
2452+
conf->getInheritedConformance()->dump(out, indent + 2);
2453+
break;
2454+
}
2455+
2456+
case ProtocolConformanceKind::Specialized: {
2457+
auto conf = cast<SpecializedProtocolConformance>(this);
2458+
printCommon("specialized");
2459+
out << '\n';
2460+
for (auto sub : conf->getGenericSubstitutions()) {
2461+
sub.dump(out, indent + 2);
2462+
out << '\n';
2463+
}
2464+
conf->getGenericConformance()->dump(out, indent + 2);
2465+
break;
2466+
}
2467+
}
2468+
2469+
out << ')';
24302470
}
24312471

24322472
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)