Skip to content

Commit c4efa0d

Browse files
committed
[AST] Factor out Expr::getNameLoc
There are a bunch of AST nodes that can have associated DeclNameLocs, make sure we cover them all. I don't think this makes a difference for `unwrapPropertyWrapperParameterTypes` since the extra cases should be invalid, but for cursor info it ensures we handle UnresolvedMemberExprs.
1 parent dd97862 commit c4efa0d

File tree

5 files changed

+40
-28
lines changed

5 files changed

+40
-28
lines changed

include/swift/AST/Expr.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,10 @@ class alignas(8) Expr : public ASTAllocated<Expr> {
552552
/// \c nullptr.
553553
ArgumentList *getArgs() const;
554554

555+
/// If the expression has a DeclNameLoc, returns it. Otherwise, returns
556+
/// an nullp DeclNameLoc.
557+
DeclNameLoc getNameLoc() const;
558+
555559
/// Produce a mapping from each subexpression to its parent
556560
/// expression, with the provided expression serving as the root of
557561
/// the parent map.

lib/AST/Expr.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,25 @@ ArgumentList *Expr::getArgs() const {
859859
return nullptr;
860860
}
861861

862+
DeclNameLoc Expr::getNameLoc() const {
863+
if (auto *DRE = dyn_cast<DeclRefExpr>(this))
864+
return DRE->getNameLoc();
865+
if (auto *UDRE = dyn_cast<UnresolvedDeclRefExpr>(this))
866+
return UDRE->getNameLoc();
867+
if (auto *ODRE = dyn_cast<OverloadedDeclRefExpr>(this))
868+
return ODRE->getNameLoc();
869+
if (auto *UDE = dyn_cast<UnresolvedDotExpr>(this))
870+
return UDE->getNameLoc();
871+
if (auto *UME = dyn_cast<UnresolvedMemberExpr>(this))
872+
return UME->getNameLoc();
873+
if (auto *MRE = dyn_cast<MemberRefExpr>(this))
874+
return MRE->getNameLoc();
875+
if (auto *DRME = dyn_cast<DynamicMemberRefExpr>(this))
876+
return DRME->getNameLoc();
877+
878+
return DeclNameLoc();
879+
}
880+
862881
llvm::DenseMap<Expr *, Expr *> Expr::getParentMap() {
863882
class RecordingTraversal : public ASTWalker {
864883
public:

lib/IDE/CursorInfo.cpp

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -214,23 +214,6 @@ class NodeFinder : ASTWalker {
214214
return Action::Continue();
215215
}
216216

217-
/// Retrieve the name location for an expression that supports cursor info.
218-
DeclNameLoc getExprNameLoc(Expr *E) {
219-
if (auto *DRE = dyn_cast<DeclRefExpr>(E))
220-
return DRE->getNameLoc();
221-
222-
if (auto *UDRE = dyn_cast<UnresolvedDeclRefExpr>(E))
223-
return UDRE->getNameLoc();
224-
225-
if (auto *ODRE = dyn_cast<OverloadedDeclRefExpr>(E))
226-
return ODRE->getNameLoc();
227-
228-
if (auto *UDE = dyn_cast<UnresolvedDotExpr>(E))
229-
return UDE->getNameLoc();
230-
231-
return DeclNameLoc();
232-
}
233-
234217
PreWalkResult<Expr *> walkToExprPre(Expr *E) override {
235218
if (auto closure = dyn_cast<ClosureExpr>(E)) {
236219
DeclContextStack.push_back(closure);
@@ -247,7 +230,7 @@ class NodeFinder : ASTWalker {
247230
}
248231
}
249232

250-
if (getExprNameLoc(E).getBaseNameLoc() != LocToResolve)
233+
if (E->getNameLoc().getBaseNameLoc() != LocToResolve)
251234
return Action::Continue(E);
252235

253236
assert(Result == nullptr);

lib/Sema/TypeOfReference.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -706,16 +706,8 @@ unwrapPropertyWrapperParameterTypes(ConstraintSystem &cs, AbstractFunctionDecl *
706706
SmallVector<AnyFunctionType::Param, 4> adjustedParamTypes;
707707

708708
DeclNameLoc nameLoc;
709-
auto *ref = getAsExpr(locator.getAnchor());
710-
if (auto *declRef = dyn_cast<DeclRefExpr>(ref)) {
711-
nameLoc = declRef->getNameLoc();
712-
} else if (auto *dotExpr = dyn_cast<UnresolvedDotExpr>(ref)) {
713-
nameLoc = dotExpr->getNameLoc();
714-
} else if (auto *overloadedRef = dyn_cast<OverloadedDeclRefExpr>(ref)) {
715-
nameLoc = overloadedRef->getNameLoc();
716-
} else if (auto *memberExpr = dyn_cast<UnresolvedMemberExpr>(ref)) {
717-
nameLoc = memberExpr->getNameLoc();
718-
}
709+
if (auto *ref = getAsExpr(locator.getAnchor()))
710+
nameLoc = ref->getNameLoc();
719711

720712
for (unsigned i : indices(*paramList)) {
721713
Identifier argLabel;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
struct S {
2+
static func foo(_ x: Int) -> S {}
3+
static func foo(_ x: String) -> S {}
4+
}
5+
6+
// https://github.com/swiftlang/swift/issues/77981 - Make sure we can resolve
7+
// solver-based cursor info for UnresolvedMemberExprs.
8+
func bar() {
9+
// RUN: %sourcekitd-test -req=cursor -pos=%(line + 1):15 %s -- %s | %FileCheck %s
10+
let _: S = .foo()
11+
}
12+
13+
// CHECK-DAG: source.lang.swift.ref.function.method.static (2:15-2:28)
14+
// CHECK-DAG: source.lang.swift.ref.function.method.static (3:15-3:31)

0 commit comments

Comments
 (0)