34
34
namespace swift {
35
35
36
36
class Expr ;
37
+ struct TypeLoc ;
38
+ class VarDecl ;
39
+ class Pattern ;
37
40
class SourceManager ;
38
41
39
42
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 *>;
41
49
42
50
// / Locates a given constraint within the expression being
43
51
// / type-checked, which may refer down into subexpressions and parts of
@@ -302,8 +310,8 @@ class ConstraintLocator : public llvm::FoldingSetNode {
302
310
}
303
311
304
312
// / Retrieve the expression that anchors this locator.
305
- Expr * getAnchor () const { return anchor; }
306
-
313
+ TypedNode getAnchor () const { return anchor; }
314
+
307
315
// / Retrieve the path that extends from the anchor to a specific
308
316
// / subcomponent.
309
317
ArrayRef<PathElement> getPath () const {
@@ -377,8 +385,10 @@ class ConstraintLocator : public llvm::FoldingSetNode {
377
385
378
386
// / Determine whether this locator points directly to a given expression.
379
387
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 ;
382
392
}
383
393
384
394
// / Attempts to cast the first path element of the locator to a specific
@@ -492,9 +502,9 @@ class ConstraintLocator : public llvm::FoldingSetNode {
492
502
GenericTypeParamType *getGenericParameter () const ;
493
503
494
504
// / 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,
496
506
ArrayRef<PathElement> path);
497
-
507
+
498
508
// / Produce a profile of this locator, for use in a folding set.
499
509
void Profile (llvm::FoldingSetNodeID &id) {
500
510
Profile (id, anchor, getPath ());
@@ -508,10 +518,9 @@ class ConstraintLocator : public llvm::FoldingSetNode {
508
518
509
519
private:
510
520
// / Initialize a constraint locator with an anchor and a path.
511
- ConstraintLocator (Expr * anchor, ArrayRef<PathElement> path,
521
+ ConstraintLocator (TypedNode anchor, ArrayRef<PathElement> path,
512
522
unsigned flags)
513
- : anchor(anchor), numPathElements(path.size()), summaryFlags(flags)
514
- {
523
+ : anchor(anchor), numPathElements(path.size()), summaryFlags(flags) {
515
524
// FIXME: Alignment.
516
525
std::copy (path.begin (), path.end (),
517
526
reinterpret_cast <PathElement *>(this + 1 ));
@@ -524,8 +533,7 @@ class ConstraintLocator : public llvm::FoldingSetNode {
524
533
// / of the locator. The ConstraintSystem object is responsible for
525
534
// / uniquing via the FoldingSet.
526
535
static ConstraintLocator *create (llvm::BumpPtrAllocator &allocator,
527
- Expr *anchor,
528
- ArrayRef<PathElement> path,
536
+ TypedNode anchor, ArrayRef<PathElement> path,
529
537
unsigned flags) {
530
538
// FIXME: Alignment.
531
539
unsigned size = sizeof (ConstraintLocator)
@@ -535,7 +543,7 @@ class ConstraintLocator : public llvm::FoldingSetNode {
535
543
}
536
544
537
545
// / The expression at which this locator is anchored.
538
- Expr * anchor;
546
+ TypedNode anchor;
539
547
540
548
// / The number of path elements in this locator.
541
549
// /
@@ -934,19 +942,19 @@ class ConstraintLocatorBuilder {
934
942
}
935
943
936
944
// / Get anchor expression associated with this locator builder.
937
- Expr * getAnchor () const {
945
+ TypedNode getAnchor () const {
938
946
for (auto prev = this ; prev;
939
947
prev = prev->previous .dyn_cast <ConstraintLocatorBuilder *>()) {
940
948
if (auto *locator = prev->previous .dyn_cast <ConstraintLocator *>())
941
949
return locator->getAnchor ();
942
950
}
943
951
944
- return nullptr ;
952
+ return {} ;
945
953
}
946
954
947
955
// / Retrieve the components of the complete locator, which includes
948
956
// / the anchor expression and the path.
949
- Expr * getLocatorParts (SmallVectorImpl<LocatorPathElt> &path) const {
957
+ TypedNode getLocatorParts (SmallVectorImpl<LocatorPathElt> &path) const {
950
958
for (auto prev = this ;
951
959
prev;
952
960
prev = prev->previous .dyn_cast <ConstraintLocatorBuilder *>()) {
0 commit comments