Skip to content

Commit 814ce14

Browse files
authored
Merge pull request #14655 from rudkx/dump-expr-types-from-cs-cache
2 parents 2b61d4e + 50b189b commit 814ce14

File tree

5 files changed

+76
-9
lines changed

5 files changed

+76
-9
lines changed

include/swift/AST/Expr.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,8 +566,22 @@ class alignas(8) Expr {
566566
LLVM_ATTRIBUTE_DEPRECATED(
567567
void dump() const LLVM_ATTRIBUTE_USED,
568568
"only for use within the debugger");
569+
LLVM_ATTRIBUTE_DEPRECATED(
570+
void dump(llvm::function_ref<Type(const Expr *)> getType,
571+
llvm::function_ref<Type(const TypeLoc &)> getTypeOfTypeLoc)
572+
const LLVM_ATTRIBUTE_USED,
573+
"only for use within the debugger");
574+
LLVM_ATTRIBUTE_DEPRECATED(
575+
void dump(raw_ostream &OS, llvm::function_ref<Type(const Expr *)> getType,
576+
llvm::function_ref<Type(const TypeLoc &)> getTypeOfTypeLoc)
577+
const LLVM_ATTRIBUTE_USED,
578+
"only for use within the debugger");
579+
569580
void dump(raw_ostream &OS) const;
570581
void print(raw_ostream &OS, unsigned Indent = 0) const;
582+
void print(raw_ostream &OS, llvm::function_ref<Type(const Expr *)> getType,
583+
llvm::function_ref<Type(const TypeLoc &)> getTypeOfTypeLoc,
584+
unsigned Indent = 0) const;
571585
void print(ASTPrinter &Printer, const PrintOptions &Opts) const;
572586

573587
// Only allow allocation of Exprs using the allocator in ASTContext

lib/AST/ASTDumper.cpp

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1622,10 +1622,16 @@ namespace {
16221622
class PrintExpr : public ExprVisitor<PrintExpr> {
16231623
public:
16241624
raw_ostream &OS;
1625+
llvm::function_ref<Type(const Expr *)> GetTypeOfExpr;
1626+
llvm::function_ref<Type(const TypeLoc &)> GetTypeOfTypeLoc;
16251627
unsigned Indent;
16261628

1627-
PrintExpr(raw_ostream &os, unsigned indent) : OS(os), Indent(indent) {
1628-
}
1629+
PrintExpr(raw_ostream &os,
1630+
llvm::function_ref<Type(const Expr *)> getTypeOfExpr,
1631+
llvm::function_ref<Type(const TypeLoc &)> getTypeOfTypeLoc,
1632+
unsigned indent)
1633+
: OS(os), GetTypeOfExpr(getTypeOfExpr),
1634+
GetTypeOfTypeLoc(getTypeOfTypeLoc), Indent(indent) {}
16291635

16301636
void printRec(Expr *E) {
16311637
Indent += 2;
@@ -1669,15 +1675,15 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
16691675

16701676
if (E->isImplicit())
16711677
PrintWithColorRAII(OS, ExprModifierColor) << " implicit";
1672-
PrintWithColorRAII(OS, TypeColor) << " type='" << E->getType() << '\'';
1678+
PrintWithColorRAII(OS, TypeColor) << " type='" << GetTypeOfExpr(E) << '\'';
16731679

16741680
if (E->hasLValueAccessKind()) {
16751681
PrintWithColorRAII(OS, ExprModifierColor)
16761682
<< " accessKind=" << getAccessKindString(E->getLValueAccessKind());
16771683
}
16781684

16791685
// If we have a source range and an ASTContext, print the source range.
1680-
if (auto Ty = E->getType()) {
1686+
if (auto Ty = GetTypeOfExpr(E)) {
16811687
auto &Ctx = Ty->getASTContext();
16821688
auto L = E->getLoc();
16831689
if (L.isValid()) {
@@ -1719,7 +1725,7 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
17191725
if (E->isNegative())
17201726
PrintWithColorRAII(OS, LiteralValueColor) << " negative";
17211727
PrintWithColorRAII(OS, LiteralValueColor) << " value=";
1722-
Type T = E->getType();
1728+
Type T = GetTypeOfExpr(E);
17231729
if (T.isNull() || !T->is<BuiltinIntegerType>())
17241730
PrintWithColorRAII(OS, LiteralValueColor) << E->getDigitsText();
17251731
else
@@ -2345,7 +2351,7 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
23452351
if (auto checkedCast = dyn_cast<CheckedCastExpr>(E))
23462352
OS << getCheckedCastKindName(checkedCast->getCastKind()) << ' ';
23472353
OS << "writtenType='";
2348-
E->getCastTypeLoc().getType().print(OS);
2354+
GetTypeOfTypeLoc(E->getCastTypeLoc()).print(OS);
23492355
OS << "'\n";
23502356
printRec(E->getSubExpr());
23512357
PrintWithColorRAII(OS, ParenthesisColor) << ')';
@@ -2546,12 +2552,23 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
25462552

25472553
} // end anonymous namespace
25482554

2555+
void Expr::dump(
2556+
raw_ostream &OS, llvm::function_ref<Type(const Expr *)> getTypeOfExpr,
2557+
llvm::function_ref<Type(const TypeLoc &)> getTypeOfTypeLoc) const {
2558+
if (auto ty = getTypeOfExpr(this)) {
2559+
llvm::SaveAndRestore<bool> X(
2560+
ty->getASTContext().LangOpts.DebugConstraintSolver, true);
2561+
print(OS, getTypeOfExpr, getTypeOfTypeLoc);
2562+
} else {
2563+
print(OS, getTypeOfExpr, getTypeOfTypeLoc);
2564+
}
2565+
OS << '\n';
2566+
}
25492567

25502568
void Expr::dump(raw_ostream &OS) const {
25512569
if (auto ty = getType()) {
25522570
llvm::SaveAndRestore<bool> X(ty->getASTContext().LangOpts.
25532571
DebugConstraintSolver, true);
2554-
25552572
print(OS);
25562573
} else {
25572574
print(OS);
@@ -2563,8 +2580,26 @@ void Expr::dump() const {
25632580
dump(llvm::errs());
25642581
}
25652582

2583+
void Expr::dump(
2584+
llvm::function_ref<Type(const Expr *)> getTypeOfExpr,
2585+
llvm::function_ref<Type(const TypeLoc &)> getTypeOfTypeLoc) const {
2586+
dump(llvm::errs(), getTypeOfExpr, getTypeOfTypeLoc);
2587+
}
2588+
2589+
void Expr::print(raw_ostream &OS,
2590+
llvm::function_ref<Type(const Expr *)> getTypeOfExpr,
2591+
llvm::function_ref<Type(const TypeLoc &)> getTypeOfTypeLoc,
2592+
unsigned Indent) const {
2593+
PrintExpr(OS, getTypeOfExpr, getTypeOfTypeLoc, Indent)
2594+
.visit(const_cast<Expr *>(this));
2595+
}
2596+
25662597
void Expr::print(raw_ostream &OS, unsigned Indent) const {
2567-
PrintExpr(OS, Indent).visit(const_cast<Expr*>(this));
2598+
auto getTypeOfExpr = [](const Expr *E) -> Type { return E->getType(); };
2599+
auto getTypeOfTypeLoc = [](const TypeLoc &TL) -> Type {
2600+
return TL.getType();
2601+
};
2602+
print(OS, getTypeOfExpr, getTypeOfTypeLoc, Indent);
25682603
}
25692604

25702605
void Expr::print(ASTPrinter &Printer, const PrintOptions &Opts) const {

lib/Sema/CSSolver.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1380,9 +1380,15 @@ ConstraintSystem::solve(Expr *&expr,
13801380
}
13811381

13821382
if (TC.getLangOpts().DebugConstraintSolver) {
1383+
auto getTypeOfExpr = [&](const Expr *E) -> Type { return getType(E); };
1384+
auto getTypeOfTypeLoc = [&](const TypeLoc &TL) -> Type {
1385+
return getType(TL);
1386+
};
1387+
13831388
auto &log = getASTContext().TypeCheckerDebug->getStream();
13841389
log << "---Initial constraints for the given expression---\n";
1385-
expr->print(log);
1390+
1391+
expr->print(log, getTypeOfExpr, getTypeOfTypeLoc);
13861392
log << "\n";
13871393
print(log);
13881394
}

lib/Sema/ConstraintSystem.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3101,6 +3101,9 @@ class ConstraintSystem {
31013101
LLVM_ATTRIBUTE_DEPRECATED(
31023102
void dump() LLVM_ATTRIBUTE_USED,
31033103
"only for use within the debugger");
3104+
LLVM_ATTRIBUTE_DEPRECATED(void dump(Expr *) LLVM_ATTRIBUTE_USED,
3105+
"only for use within the debugger");
3106+
31043107
void print(raw_ostream &out);
31053108
};
31063109

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3118,6 +3118,15 @@ void ConstraintSystem::dump() {
31183118
print(llvm::errs());
31193119
}
31203120

3121+
void ConstraintSystem::dump(Expr *E) {
3122+
auto getTypeOfExpr = [&](const Expr *E) -> Type { return getType(E); };
3123+
auto getTypeOfTypeLoc = [&](const TypeLoc &TL) -> Type {
3124+
return getType(TL);
3125+
};
3126+
3127+
E->dump(getTypeOfExpr, getTypeOfTypeLoc);
3128+
}
3129+
31213130
void ConstraintSystem::print(raw_ostream &out) {
31223131
// Print all type variables as $T0 instead of _ here.
31233132
llvm::SaveAndRestore<bool> X(getASTContext().LangOpts.DebugConstraintSolver,

0 commit comments

Comments
 (0)