Skip to content

Commit 099379a

Browse files
committed
[ConstraintSystem] Switch ConstraintLocator to be anchored on TypedNode
1 parent 288e286 commit 099379a

File tree

2 files changed

+35
-27
lines changed

2 files changed

+35
-27
lines changed

lib/Sema/ConstraintLocator.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
using namespace swift;
2727
using namespace constraints;
2828

29-
void ConstraintLocator::Profile(llvm::FoldingSetNodeID &id, Expr *anchor,
29+
void ConstraintLocator::Profile(llvm::FoldingSetNodeID &id, TypedNode anchor,
3030
ArrayRef<PathElement> path) {
31-
id.AddPointer(anchor);
31+
id.AddPointer(anchor.getOpaqueValue());
3232
id.AddInteger(path.size());
3333
for (auto elt : path) {
3434
id.AddInteger(elt.getKind());
@@ -149,7 +149,7 @@ bool LocatorPathElt::isResultOfSingleExprFunction() const {
149149
/// Determine whether given locator points to the subscript reference
150150
/// e.g. `foo[0]` or `\Foo.[0]`
151151
bool ConstraintLocator::isSubscriptMemberRef() const {
152-
auto *anchor = getAnchor();
152+
auto anchor = getAnchor();
153153
auto path = getPath();
154154

155155
if (!anchor || path.empty())
@@ -159,16 +159,16 @@ bool ConstraintLocator::isSubscriptMemberRef() const {
159159
}
160160

161161
bool ConstraintLocator::isKeyPathType() const {
162-
auto *anchor = getAnchor();
162+
auto anchor = getAnchor();
163163
auto path = getPath();
164164
// The format of locator should be `<keypath expr> -> key path type`
165-
if (!anchor || !isa<KeyPathExpr>(anchor) || path.size() != 1)
165+
if (!anchor || !isExpr<KeyPathExpr>(anchor) || path.size() != 1)
166166
return false;
167167
return path.back().getKind() == ConstraintLocator::KeyPathType;
168168
}
169169

170170
bool ConstraintLocator::isKeyPathRoot() const {
171-
auto *anchor = getAnchor();
171+
auto anchor = getAnchor();
172172
auto path = getPath();
173173

174174
if (!anchor || path.empty())
@@ -178,7 +178,7 @@ bool ConstraintLocator::isKeyPathRoot() const {
178178
}
179179

180180
bool ConstraintLocator::isKeyPathValue() const {
181-
auto *anchor = getAnchor();
181+
auto anchor = getAnchor();
182182
auto path = getPath();
183183

184184
if (!anchor || path.empty())
@@ -194,7 +194,7 @@ bool ConstraintLocator::isResultOfKeyPathDynamicMemberLookup() const {
194194
}
195195

196196
bool ConstraintLocator::isKeyPathSubscriptComponent() const {
197-
auto *anchor = getAnchor();
197+
auto *anchor = getAnchor().dyn_cast<const Expr *>();
198198
auto *KPE = dyn_cast_or_null<KeyPathExpr>(anchor);
199199
if (!KPE)
200200
return false;
@@ -270,11 +270,11 @@ void ConstraintLocator::dump(SourceManager *sm, raw_ostream &out) const {
270270

271271
out << "locator@" << (void*) this << " [";
272272

273-
if (anchor) {
274-
out << Expr::getKindName(anchor->getKind());
273+
if (auto *expr = anchor.dyn_cast<const Expr *>()) {
274+
out << Expr::getKindName(expr->getKind());
275275
if (sm) {
276276
out << '@';
277-
anchor->getLoc().print(out, *sm);
277+
expr->getLoc().print(out, *sm);
278278
}
279279
}
280280

lib/Sema/ConstraintLocator.h

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,18 @@
3434
namespace swift {
3535

3636
class Expr;
37+
struct TypeLoc;
38+
class VarDecl;
39+
class Pattern;
3740
class SourceManager;
3841

3942
namespace constraints {
40-
class ConstraintSystem;
43+
44+
class ConstraintSystem;
45+
46+
/// An AST node that can gain type information while solving.
47+
using TypedNode = llvm::PointerUnion<const Expr *, const TypeLoc *,
48+
const VarDecl *, const Pattern *>;
4149

4250
/// Locates a given constraint within the expression being
4351
/// type-checked, which may refer down into subexpressions and parts of
@@ -302,8 +310,8 @@ class ConstraintLocator : public llvm::FoldingSetNode {
302310
}
303311

304312
/// Retrieve the expression that anchors this locator.
305-
Expr *getAnchor() const { return anchor; }
306-
313+
TypedNode getAnchor() const { return anchor; }
314+
307315
/// Retrieve the path that extends from the anchor to a specific
308316
/// subcomponent.
309317
ArrayRef<PathElement> getPath() const {
@@ -377,8 +385,10 @@ class ConstraintLocator : public llvm::FoldingSetNode {
377385

378386
/// Determine whether this locator points directly to a given expression.
379387
template <class E> bool directlyAt() const {
380-
auto *anchor = getAnchor();
381-
return anchor && isa<E>(anchor) && getPath().empty();
388+
if (auto *anchor = getAnchor().dyn_cast<const Expr *>()) {
389+
return isa<E>(anchor) && getPath().empty();
390+
}
391+
return false;
382392
}
383393

384394
/// Attempts to cast the first path element of the locator to a specific
@@ -492,9 +502,9 @@ class ConstraintLocator : public llvm::FoldingSetNode {
492502
GenericTypeParamType *getGenericParameter() const;
493503

494504
/// Produce a profile of this locator, for use in a folding set.
495-
static void Profile(llvm::FoldingSetNodeID &id, Expr *anchor,
505+
static void Profile(llvm::FoldingSetNodeID &id, TypedNode anchor,
496506
ArrayRef<PathElement> path);
497-
507+
498508
/// Produce a profile of this locator, for use in a folding set.
499509
void Profile(llvm::FoldingSetNodeID &id) {
500510
Profile(id, anchor, getPath());
@@ -508,10 +518,9 @@ class ConstraintLocator : public llvm::FoldingSetNode {
508518

509519
private:
510520
/// Initialize a constraint locator with an anchor and a path.
511-
ConstraintLocator(Expr *anchor, ArrayRef<PathElement> path,
521+
ConstraintLocator(TypedNode anchor, ArrayRef<PathElement> path,
512522
unsigned flags)
513-
: anchor(anchor), numPathElements(path.size()), summaryFlags(flags)
514-
{
523+
: anchor(anchor), numPathElements(path.size()), summaryFlags(flags) {
515524
// FIXME: Alignment.
516525
std::copy(path.begin(), path.end(),
517526
reinterpret_cast<PathElement *>(this + 1));
@@ -524,8 +533,7 @@ class ConstraintLocator : public llvm::FoldingSetNode {
524533
/// of the locator. The ConstraintSystem object is responsible for
525534
/// uniquing via the FoldingSet.
526535
static ConstraintLocator *create(llvm::BumpPtrAllocator &allocator,
527-
Expr *anchor,
528-
ArrayRef<PathElement> path,
536+
TypedNode anchor, ArrayRef<PathElement> path,
529537
unsigned flags) {
530538
// FIXME: Alignment.
531539
unsigned size = sizeof(ConstraintLocator)
@@ -535,7 +543,7 @@ class ConstraintLocator : public llvm::FoldingSetNode {
535543
}
536544

537545
/// The expression at which this locator is anchored.
538-
Expr *anchor;
546+
TypedNode anchor;
539547

540548
/// The number of path elements in this locator.
541549
///
@@ -934,19 +942,19 @@ class ConstraintLocatorBuilder {
934942
}
935943

936944
/// Get anchor expression associated with this locator builder.
937-
Expr *getAnchor() const {
945+
TypedNode getAnchor() const {
938946
for (auto prev = this; prev;
939947
prev = prev->previous.dyn_cast<ConstraintLocatorBuilder *>()) {
940948
if (auto *locator = prev->previous.dyn_cast<ConstraintLocator *>())
941949
return locator->getAnchor();
942950
}
943951

944-
return nullptr;
952+
return {};
945953
}
946954

947955
/// Retrieve the components of the complete locator, which includes
948956
/// the anchor expression and the path.
949-
Expr *getLocatorParts(SmallVectorImpl<LocatorPathElt> &path) const {
957+
TypedNode getLocatorParts(SmallVectorImpl<LocatorPathElt> &path) const {
950958
for (auto prev = this;
951959
prev;
952960
prev = prev->previous.dyn_cast<ConstraintLocatorBuilder *>()) {

0 commit comments

Comments
 (0)