Skip to content

Commit 5ce503c

Browse files
committed
Simplify resolveLocatorToDecl to return a ConcreteDeclRef instead of
having it return a ResolvedLocator, allowing us to remove ResolvedLocator.
1 parent a65ffab commit 5ce503c

File tree

2 files changed

+24
-83
lines changed

2 files changed

+24
-83
lines changed

lib/Sema/CSDiag.cpp

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ static ParameterList *getParameterList(ValueDecl *decl) {
313313
return nullptr;
314314
}
315315

316-
ResolvedLocator constraints::resolveLocatorToDecl(
316+
ConcreteDeclRef constraints::resolveLocatorToDecl(
317317
ConstraintSystem &cs,
318318
ConstraintLocator *locator,
319319
std::function<Optional<SelectedOverload>(ConstraintLocator *)> findOvlChoice,
@@ -322,7 +322,7 @@ ResolvedLocator constraints::resolveLocatorToDecl(
322322
{
323323
assert(locator && "Null locator");
324324
if (!locator->getAnchor())
325-
return ResolvedLocator();
325+
return ConcreteDeclRef();
326326

327327
ConcreteDeclRef declRef;
328328
auto anchor = locator->getAnchor();
@@ -401,7 +401,7 @@ ResolvedLocator constraints::resolveLocatorToDecl(
401401

402402
// If we didn't find the declaration, we're out of luck.
403403
if (!declRef)
404-
return ResolvedLocator();
404+
return ConcreteDeclRef();
405405

406406
// Use the declaration and the path to produce a more specific result.
407407
// FIXME: This is an egregious hack. We'd be far better off
@@ -426,10 +426,8 @@ ResolvedLocator constraints::resolveLocatorToDecl(
426426
if (!parameterList) break;
427427

428428
unsigned index = path[0].getValue2();
429-
if (index < parameterList->size()) {
430-
auto param = parameterList->get(index);
431-
return ResolvedLocator(ResolvedLocator::ForVar, param);
432-
}
429+
if (index < parameterList->size())
430+
return parameterList->get(index);
433431
break;
434432
}
435433

@@ -441,14 +439,13 @@ ResolvedLocator constraints::resolveLocatorToDecl(
441439
}
442440

443441
// Otherwise, do the best we can with the declaration we found.
444-
if (isa<FuncDecl>(declRef.getDecl()))
445-
return ResolvedLocator(ResolvedLocator::ForFunction, declRef);
446-
if (isa<ConstructorDecl>(declRef.getDecl()))
447-
return ResolvedLocator(ResolvedLocator::ForConstructor, declRef);
448-
449442
// FIXME: Deal with the other interesting cases here, e.g.,
450443
// subscript declarations.
451-
return ResolvedLocator();
444+
if (isa<FuncDecl>(declRef.getDecl()) ||
445+
isa<ConstructorDecl>(declRef.getDecl()))
446+
return declRef;
447+
448+
return ConcreteDeclRef();
452449
}
453450

454451
/// Emit a note referring to the target of a diagnostic, e.g., the function
@@ -473,32 +470,29 @@ static void noteTargetOfDiagnostic(ConstraintSystem &cs,
473470
if (!resolved)
474471
return;
475472

476-
switch (resolved.getKind()) {
477-
case ResolvedLocatorKind::Unresolved:
478-
// Can't emit any diagnostic here.
479-
return;
480-
481-
case ResolvedLocatorKind::Function: {
482-
auto name = resolved.getDecl().getDecl()->getName();
483-
cs.getTypeChecker().diagnose(resolved.getDecl().getDecl(),
473+
auto decl = resolved.getDecl();
474+
if (isa<FuncDecl>(decl)) {
475+
auto name = decl->getName();
476+
cs.getTypeChecker().diagnose(decl,
484477
name.isOperator()? diag::note_call_to_operator
485478
: diag::note_call_to_func,
486-
resolved.getDecl().getDecl()->getName());
479+
name);
487480
return;
488481
}
489482

490-
case ResolvedLocatorKind::Constructor:
483+
if (isa<ConstructorDecl>(decl)) {
491484
// FIXME: Specialize for implicitly-generated constructors.
492-
cs.getTypeChecker().diagnose(resolved.getDecl().getDecl(),
493-
diag::note_call_to_initializer);
485+
cs.getTypeChecker().diagnose(decl, diag::note_call_to_initializer);
494486
return;
487+
}
495488

496-
case ResolvedLocatorKind::Parameter:
497-
cs.getTypeChecker().diagnose(resolved.getDecl().getDecl(),
498-
diag::note_init_parameter,
499-
resolved.getDecl().getDecl()->getName());
489+
if (isa<ParamDecl>(decl)) {
490+
cs.getTypeChecker().diagnose(decl, diag::note_init_parameter,
491+
decl->getName());
500492
return;
501493
}
494+
495+
// FIXME: Other decl types too.
502496
}
503497

504498
/// \brief Determine the number of distinct overload choices in the

lib/Sema/ConstraintSystem.h

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

2235-
/// Describes the kind of entity to which a locator was resolved.
2236-
enum class ResolvedLocatorKind : uint8_t {
2237-
/// The locator could not be resolved.
2238-
Unresolved,
2239-
/// The locator refers to a function.
2240-
Function,
2241-
/// The locator refers to a constructor.
2242-
Constructor,
2243-
/// The locator refers to a parameter of a function.
2244-
Parameter
2245-
};
2246-
2247-
/// The entity to which a locator resolved.
2248-
class ResolvedLocator {
2249-
ResolvedLocatorKind kind;
2250-
ConcreteDeclRef decl;
2251-
2252-
public:
2253-
ResolvedLocator() : kind(ResolvedLocatorKind::Unresolved) { }
2254-
2255-
enum ForFunction_t { ForFunction };
2256-
enum ForConstructor_t { ForConstructor };
2257-
enum ForVar_t { ForVar };
2258-
2259-
ResolvedLocator(ForFunction_t, ConcreteDeclRef decl)
2260-
: kind(ResolvedLocatorKind::Function), decl(decl)
2261-
{
2262-
assert(isa<FuncDecl>(decl.getDecl()));
2263-
}
2264-
2265-
ResolvedLocator(ForConstructor_t, ConcreteDeclRef decl)
2266-
: kind(ResolvedLocatorKind::Constructor), decl(decl)
2267-
{
2268-
assert(isa<ConstructorDecl>(decl.getDecl()));
2269-
}
2270-
2271-
ResolvedLocator(ForVar_t, ConcreteDeclRef decl)
2272-
: kind(ResolvedLocatorKind::Parameter), decl(decl)
2273-
{
2274-
assert(isa<VarDecl>(decl.getDecl()));
2275-
}
2276-
2277-
/// Determine the kind of entity to which the locator resolved.
2278-
ResolvedLocatorKind getKind() const { return kind; }
2279-
2280-
/// Retrieve the declaration to which the locator resolved.
2281-
ConcreteDeclRef getDecl() const { return decl; }
2282-
2283-
explicit operator bool() const {
2284-
return getKind() != ResolvedLocatorKind::Unresolved;
2285-
}
2286-
};
2287-
22882235
/// Resolve a locator to the specific declaration it references, if possible.
22892236
///
22902237
/// \param cs The constraint system in which the locator will be resolved.
@@ -2298,7 +2245,7 @@ class ResolvedLocator {
22982245
/// \returns the entity to which the locator resolved.
22992246
///
23002247
/// FIXME: It would be more natural to express the result as a locator.
2301-
ResolvedLocator resolveLocatorToDecl(
2248+
ConcreteDeclRef resolveLocatorToDecl(
23022249
ConstraintSystem &cs,
23032250
ConstraintLocator *locator,
23042251
std::function<Optional<SelectedOverload>(ConstraintLocator *)>

0 commit comments

Comments
 (0)