Skip to content

Commit 638816e

Browse files
committed
Now that resolveLocatorToDecl has a single caller, move it to CSApply.cpp
and make it a static function. NFC.
1 parent 005809d commit 638816e

File tree

3 files changed

+101
-108
lines changed

3 files changed

+101
-108
lines changed

lib/Sema/CSApply.cpp

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3322,6 +3322,107 @@ namespace {
33223322
};
33233323
}
33243324

3325+
3326+
/// Resolve a locator to the specific declaration it references, if possible.
3327+
///
3328+
/// \param cs The constraint system in which the locator will be resolved.
3329+
///
3330+
/// \param locator The locator to resolve.
3331+
///
3332+
/// \param findOvlChoice A function that searches for the overload choice
3333+
/// associated with the given locator, or an empty optional if there is no such
3334+
/// overload.
3335+
///
3336+
/// \returns the decl to which the locator resolved.
3337+
///
3338+
static ConcreteDeclRef
3339+
resolveLocatorToDecl(ConstraintSystem &cs, ConstraintLocator *locator,
3340+
std::function<Optional<SelectedOverload>(ConstraintLocator *)> findOvlChoice,
3341+
std::function<ConcreteDeclRef (ValueDecl *decl,
3342+
Type openedType)> getConcreteDeclRef)
3343+
{
3344+
assert(locator && "Null locator");
3345+
if (!locator->getAnchor())
3346+
return ConcreteDeclRef();
3347+
3348+
auto anchor = locator->getAnchor();
3349+
// Unwrap any specializations, constructor calls, implicit conversions, and
3350+
// '.'s.
3351+
// FIXME: This is brittle.
3352+
do {
3353+
if (auto specialize = dyn_cast<UnresolvedSpecializeExpr>(anchor)) {
3354+
anchor = specialize->getSubExpr();
3355+
continue;
3356+
}
3357+
3358+
if (auto implicit = dyn_cast<ImplicitConversionExpr>(anchor)) {
3359+
anchor = implicit->getSubExpr();
3360+
continue;
3361+
}
3362+
3363+
if (auto identity = dyn_cast<IdentityExpr>(anchor)) {
3364+
anchor = identity->getSubExpr();
3365+
continue;
3366+
}
3367+
3368+
if (auto tryExpr = dyn_cast<AnyTryExpr>(anchor)) {
3369+
if (isa<OptionalTryExpr>(tryExpr))
3370+
break;
3371+
3372+
anchor = tryExpr->getSubExpr();
3373+
continue;
3374+
}
3375+
3376+
if (auto selfApply = dyn_cast<SelfApplyExpr>(anchor)) {
3377+
anchor = selfApply->getFn();
3378+
continue;
3379+
}
3380+
3381+
if (auto dotSyntax = dyn_cast<DotSyntaxBaseIgnoredExpr>(anchor)) {
3382+
anchor = dotSyntax->getRHS();
3383+
continue;
3384+
}
3385+
break;
3386+
} while (true);
3387+
3388+
// Simple case: direct reference to a declaration.
3389+
if (auto dre = dyn_cast<DeclRefExpr>(anchor))
3390+
return dre->getDeclRef();
3391+
3392+
// Simple case: direct reference to a declaration.
3393+
if (auto mre = dyn_cast<MemberRefExpr>(anchor))
3394+
return mre->getMember();
3395+
3396+
if (auto ctorRef = dyn_cast<OtherConstructorDeclRefExpr>(anchor))
3397+
return ctorRef->getDeclRef();
3398+
3399+
if (isa<OverloadedDeclRefExpr>(anchor) ||
3400+
isa<OverloadedMemberRefExpr>(anchor) ||
3401+
isa<UnresolvedDeclRefExpr>(anchor)) {
3402+
// Overloaded and unresolved cases: find the resolved overload.
3403+
auto anchorLocator = cs.getConstraintLocator(anchor);
3404+
if (auto selected = findOvlChoice(anchorLocator)) {
3405+
if (selected->choice.isDecl())
3406+
return getConcreteDeclRef(selected->choice.getDecl(),
3407+
selected->openedType);
3408+
}
3409+
}
3410+
3411+
if (isa<UnresolvedMemberExpr>(anchor)) {
3412+
// Unresolved member: find the resolved overload.
3413+
auto anchorLocator = cs.getConstraintLocator(anchor,
3414+
ConstraintLocator::UnresolvedMember);
3415+
if (auto selected = findOvlChoice(anchorLocator)) {
3416+
if (selected->choice.isDecl())
3417+
return getConcreteDeclRef(selected->choice.getDecl(),
3418+
selected->openedType);
3419+
}
3420+
}
3421+
3422+
return ConcreteDeclRef();
3423+
}
3424+
3425+
33253426
/// \brief Given a constraint locator, find the owner of default arguments for
33263427
/// that tuple, i.e., a FuncDecl.
33273428
static ConcreteDeclRef

lib/Sema/CSDiag.cpp

Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -289,93 +289,6 @@ static Expr *simplifyLocatorToAnchor(ConstraintSystem &cs,
289289
return locator->getAnchor();
290290
}
291291

292-
ConcreteDeclRef constraints::resolveLocatorToDecl(
293-
ConstraintSystem &cs,
294-
ConstraintLocator *locator,
295-
std::function<Optional<SelectedOverload>(ConstraintLocator *)> findOvlChoice,
296-
std::function<ConcreteDeclRef (ValueDecl *decl,
297-
Type openedType)> getConcreteDeclRef)
298-
{
299-
assert(locator && "Null locator");
300-
if (!locator->getAnchor())
301-
return ConcreteDeclRef();
302-
303-
auto anchor = locator->getAnchor();
304-
// Unwrap any specializations, constructor calls, implicit conversions, and
305-
// '.'s.
306-
// FIXME: This is brittle.
307-
do {
308-
if (auto specialize = dyn_cast<UnresolvedSpecializeExpr>(anchor)) {
309-
anchor = specialize->getSubExpr();
310-
continue;
311-
}
312-
313-
if (auto implicit = dyn_cast<ImplicitConversionExpr>(anchor)) {
314-
anchor = implicit->getSubExpr();
315-
continue;
316-
}
317-
318-
if (auto identity = dyn_cast<IdentityExpr>(anchor)) {
319-
anchor = identity->getSubExpr();
320-
continue;
321-
}
322-
323-
if (auto tryExpr = dyn_cast<AnyTryExpr>(anchor)) {
324-
if (isa<OptionalTryExpr>(tryExpr))
325-
break;
326-
327-
anchor = tryExpr->getSubExpr();
328-
continue;
329-
}
330-
331-
if (auto selfApply = dyn_cast<SelfApplyExpr>(anchor)) {
332-
anchor = selfApply->getFn();
333-
continue;
334-
}
335-
336-
if (auto dotSyntax = dyn_cast<DotSyntaxBaseIgnoredExpr>(anchor)) {
337-
anchor = dotSyntax->getRHS();
338-
continue;
339-
}
340-
break;
341-
} while (true);
342-
343-
// Simple case: direct reference to a declaration.
344-
if (auto dre = dyn_cast<DeclRefExpr>(anchor))
345-
return dre->getDeclRef();
346-
347-
// Simple case: direct reference to a declaration.
348-
if (auto mre = dyn_cast<MemberRefExpr>(anchor))
349-
return mre->getMember();
350-
351-
if (auto ctorRef = dyn_cast<OtherConstructorDeclRefExpr>(anchor))
352-
return ctorRef->getDeclRef();
353-
354-
if (isa<OverloadedDeclRefExpr>(anchor) ||
355-
isa<OverloadedMemberRefExpr>(anchor) ||
356-
isa<UnresolvedDeclRefExpr>(anchor)) {
357-
// Overloaded and unresolved cases: find the resolved overload.
358-
auto anchorLocator = cs.getConstraintLocator(anchor);
359-
if (auto selected = findOvlChoice(anchorLocator)) {
360-
if (selected->choice.isDecl())
361-
return getConcreteDeclRef(selected->choice.getDecl(),
362-
selected->openedType);
363-
}
364-
}
365-
366-
if (isa<UnresolvedMemberExpr>(anchor)) {
367-
// Unresolved member: find the resolved overload.
368-
auto anchorLocator = cs.getConstraintLocator(anchor,
369-
ConstraintLocator::UnresolvedMember);
370-
if (auto selected = findOvlChoice(anchorLocator)) {
371-
if (selected->choice.isDecl())
372-
return getConcreteDeclRef(selected->choice.getDecl(),
373-
selected->openedType);
374-
}
375-
}
376-
377-
return ConcreteDeclRef();
378-
}
379292

380293
/// Emit a note referring to the target of a diagnostic, e.g., the function
381294
/// or parameter being used.

lib/Sema/ConstraintSystem.h

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2232,27 +2232,6 @@ void simplifyLocator(Expr *&anchor,
22322232
SmallVectorImpl<LocatorPathElt> &targetPath,
22332233
SourceRange &range);
22342234

2235-
/// Resolve a locator to the specific declaration it references, if possible.
2236-
///
2237-
/// \param cs The constraint system in which the locator will be resolved.
2238-
///
2239-
/// \param locator The locator to resolve.
2240-
///
2241-
/// \param findOvlChoice A function that searches for the overload choice
2242-
/// associated with the given locator, or an empty optional if there is no such
2243-
/// overload.
2244-
///
2245-
/// \returns the entity to which the locator resolved.
2246-
///
2247-
/// FIXME: It would be more natural to express the result as a locator.
2248-
ConcreteDeclRef resolveLocatorToDecl(
2249-
ConstraintSystem &cs,
2250-
ConstraintLocator *locator,
2251-
std::function<Optional<SelectedOverload>(ConstraintLocator *)>
2252-
findOvlChoice,
2253-
std::function<ConcreteDeclRef(ValueDecl *decl,
2254-
Type openedType)> getConcreteDeclRef);
2255-
22562235
} // end namespace constraints
22572236

22582237
template<typename ...Args>

0 commit comments

Comments
 (0)