Skip to content

Commit 340a60b

Browse files
committed
Sema: Add PotentialBindings::dump() and improve BindingSet::dump()
1 parent cfcc73f commit 340a60b

File tree

2 files changed

+77
-26
lines changed

2 files changed

+77
-26
lines changed

include/swift/Sema/CSBindings.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,11 @@ struct PotentialBindings {
301301
Constraint *constraint);
302302

303303
void reset();
304+
305+
void dump(ConstraintSystem &CS,
306+
TypeVariableType *TypeVar,
307+
llvm::raw_ostream &out,
308+
unsigned indent) const;
304309
};
305310

306311

lib/Sema/CSBindings.cpp

Lines changed: 72 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2045,6 +2045,48 @@ void PotentialBindings::reset() {
20452045
AssociatedCodeCompletionToken = ASTNode();
20462046
}
20472047

2048+
void PotentialBindings::dump(ConstraintSystem &cs,
2049+
TypeVariableType *typeVar,
2050+
llvm::raw_ostream &out,
2051+
unsigned indent) const {
2052+
PrintOptions PO;
2053+
PO.PrintTypesForDebugging = true;
2054+
2055+
out << "Potential bindings for ";
2056+
typeVar->getImpl().print(out);
2057+
out << "\n";
2058+
2059+
out << "[constraints: ";
2060+
interleave(Constraints,
2061+
[&](Constraint *constraint) {
2062+
constraint->print(out, &cs.getASTContext().SourceMgr, indent,
2063+
/*skipLocator=*/true);
2064+
},
2065+
[&out]() { out << ", "; });
2066+
out << "] ";
2067+
2068+
if (!AdjacentVars.empty()) {
2069+
out << "[adjacent to: ";
2070+
SmallVector<std::pair<TypeVariableType *, Constraint *>> adjacentVars(
2071+
AdjacentVars.begin(), AdjacentVars.end());
2072+
llvm::sort(adjacentVars,
2073+
[](auto lhs, auto rhs) {
2074+
return lhs.first->getID() < rhs.first->getID();
2075+
});
2076+
interleave(adjacentVars,
2077+
[&](std::pair<TypeVariableType *, Constraint *> pair) {
2078+
out << pair.first->getString(PO);
2079+
if (pair.first->getImpl().getFixedType(/*record=*/nullptr))
2080+
out << " (fixed)";
2081+
out << " via ";
2082+
pair.second->print(out, &cs.getASTContext().SourceMgr, indent,
2083+
/*skipLocator=*/true);
2084+
},
2085+
[&out]() { out << ", "; });
2086+
out << "] ";
2087+
}
2088+
}
2089+
20482090
void BindingSet::forEachLiteralRequirement(
20492091
llvm::function_ref<void(KnownProtocolKind)> callback) const {
20502092
for (const auto &literal : Literals) {
@@ -2184,22 +2226,21 @@ void BindingSet::dump(llvm::raw_ostream &out, unsigned indent) const {
21842226
if (!attributes.empty())
21852227
out << "] ";
21862228

2187-
if (involvesTypeVariables()) {
2229+
if (!AdjacentVars.empty()) {
21882230
out << "[adjacent to: ";
2189-
if (AdjacentVars.empty()) {
2190-
out << "<none>";
2191-
} else {
2192-
SmallVector<TypeVariableType *> adjacentVars(AdjacentVars.begin(),
2193-
AdjacentVars.end());
2194-
llvm::sort(adjacentVars,
2195-
[](const TypeVariableType *lhs, const TypeVariableType *rhs) {
2231+
SmallVector<TypeVariableType *> adjacentVars(AdjacentVars.begin(),
2232+
AdjacentVars.end());
2233+
llvm::sort(adjacentVars,
2234+
[](const TypeVariableType *lhs, const TypeVariableType *rhs) {
21962235
return lhs->getID() < rhs->getID();
2197-
});
2198-
interleave(
2199-
adjacentVars,
2200-
[&](const auto *typeVar) { out << typeVar->getString(PO); },
2201-
[&out]() { out << ", "; });
2202-
}
2236+
});
2237+
interleave(adjacentVars,
2238+
[&](auto *typeVar) {
2239+
out << typeVar->getString(PO);
2240+
if (typeVar->getImpl().getFixedType(/*record=*/nullptr))
2241+
out << " (fixed)";
2242+
},
2243+
[&out]() { out << ", "; });
22032244
out << "] ";
22042245
}
22052246

@@ -2212,24 +2253,25 @@ void BindingSet::dump(llvm::raw_ostream &out, unsigned indent) const {
22122253
enum class BindingKind { Exact, Subtypes, Supertypes, Literal };
22132254
BindingKind Kind;
22142255
Type BindingType;
2215-
PrintableBinding(BindingKind kind, Type bindingType)
2216-
: Kind(kind), BindingType(bindingType) {}
2256+
bool Viable;
2257+
PrintableBinding(BindingKind kind, Type bindingType, bool viable)
2258+
: Kind(kind), BindingType(bindingType), Viable(viable) {}
22172259

22182260
public:
22192261
static PrintableBinding supertypesOf(Type binding) {
2220-
return PrintableBinding{BindingKind::Supertypes, binding};
2262+
return PrintableBinding{BindingKind::Supertypes, binding, true};
22212263
}
22222264

22232265
static PrintableBinding subtypesOf(Type binding) {
2224-
return PrintableBinding{BindingKind::Subtypes, binding};
2266+
return PrintableBinding{BindingKind::Subtypes, binding, true};
22252267
}
22262268

22272269
static PrintableBinding exact(Type binding) {
2228-
return PrintableBinding{BindingKind::Exact, binding};
2270+
return PrintableBinding{BindingKind::Exact, binding, true};
22292271
}
22302272

2231-
static PrintableBinding literalDefaultType(Type binding) {
2232-
return PrintableBinding{BindingKind::Literal, binding};
2273+
static PrintableBinding literalDefaultType(Type binding, bool viable) {
2274+
return PrintableBinding{BindingKind::Literal, binding, viable};
22332275
}
22342276

22352277
void print(llvm::raw_ostream &out, const PrintOptions &PO,
@@ -2247,7 +2289,10 @@ void BindingSet::dump(llvm::raw_ostream &out, unsigned indent) const {
22472289
out << "(default type of literal) ";
22482290
break;
22492291
}
2250-
BindingType.print(out, PO);
2292+
if (BindingType)
2293+
BindingType.print(out, PO);
2294+
if (!Viable)
2295+
out << " [literal not viable]";
22512296
}
22522297
};
22532298

@@ -2269,10 +2314,11 @@ void BindingSet::dump(llvm::raw_ostream &out, unsigned indent) const {
22692314
}
22702315
}
22712316
for (const auto &literal : Literals) {
2272-
if (literal.second.viableAsBinding()) {
2273-
potentialBindings.push_back(PrintableBinding::literalDefaultType(
2274-
literal.second.getDefaultType()));
2275-
}
2317+
potentialBindings.push_back(PrintableBinding::literalDefaultType(
2318+
literal.second.hasDefaultType()
2319+
? literal.second.getDefaultType()
2320+
: Type(),
2321+
literal.second.viableAsBinding()));
22762322
}
22772323
if (potentialBindings.empty()) {
22782324
out << "<none>";

0 commit comments

Comments
 (0)