Skip to content

Commit b8b93eb

Browse files
committed
[CS] Add first-element casting methods for locators
1 parent dff4c78 commit b8b93eb

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

lib/Sema/ConstraintLocator.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,27 @@ class ConstraintLocator : public llvm::FoldingSetNode {
502502
/// Determine whether this locator points to the contextual type.
503503
bool isForContextualType() const;
504504

505+
/// Attempts to cast the first path element of the locator to a specific
506+
/// \c LocatorPathElt subclass, returning \c None if either unsuccessful or
507+
/// the locator has no path elements.
508+
template <class T>
509+
Optional<T> getFirstElementAs() const {
510+
auto path = getPath();
511+
if (path.empty())
512+
return None;
513+
514+
return path[0].getAs<T>();
515+
}
516+
517+
/// Casts the first path element of the locator to a specific
518+
/// \c LocatorPathElt subclass, asserting that it has at least one element.
519+
template <class T>
520+
T castFirstElementTo() const {
521+
auto path = getPath();
522+
assert(!path.empty() && "Expected at least one path element!");
523+
return path[0].castTo<T>();
524+
}
525+
505526
/// Check whether the last element in the path of this locator
506527
/// is of a given kind.
507528
bool isLastElement(ConstraintLocator::PathElementKind kind) const;

0 commit comments

Comments
 (0)