Skip to content

Commit 54d2581

Browse files
committed
[ConstraintSystem] Add isStaticallyDerivedMetatype to Solution
This is useful for a new style code completion which doesn't apply solutions back to neither constraint system nor AST.
1 parent 05c3333 commit 54d2581

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

lib/Sema/CSApply.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,46 @@ bool ConstraintSystem::isTypeReference(Expr *E) {
176176
});
177177
}
178178

179+
bool Solution::isTypeReference(Expr *E) const {
180+
return E->isTypeReference(
181+
[&](Expr *expr) -> Type { return simplifyType(getType(expr)); },
182+
[&](Expr *expr) -> Decl * {
183+
ConstraintLocator *locator = nullptr;
184+
if (auto *UDE = dyn_cast<UnresolvedDotExpr>(E)) {
185+
locator = getConstraintLocator(UDE, {ConstraintLocator::Member});
186+
}
187+
188+
if (auto *UME = dyn_cast<UnresolvedMemberExpr>(E)) {
189+
locator =
190+
getConstraintLocator(UME, {ConstraintLocator::UnresolvedMember});
191+
}
192+
193+
if (isa<OverloadSetRefExpr>(E))
194+
locator = getConstraintLocator(const_cast<Expr *>(E));
195+
196+
if (locator) {
197+
if (auto selectedOverload = getOverloadChoiceIfAvailable(locator)) {
198+
const auto &choice = selectedOverload->choice;
199+
return choice.getDeclOrNull();
200+
}
201+
}
202+
203+
return nullptr;
204+
});
205+
}
206+
179207
bool ConstraintSystem::isStaticallyDerivedMetatype(Expr *E) {
180208
return E->isStaticallyDerivedMetatype(
181209
[&](Expr *E) -> Type { return simplifyType(getType(E)); },
182210
[&](Expr *E) -> bool { return isTypeReference(E); });
183211
}
184212

213+
bool Solution::isStaticallyDerivedMetatype(Expr *E) const {
214+
return E->isStaticallyDerivedMetatype(
215+
[&](Expr *E) -> Type { return simplifyType(getType(E)); },
216+
[&](Expr *E) -> bool { return isTypeReference(E); });
217+
}
218+
185219
Type ConstraintSystem::getInstanceType(TypeExpr *E) {
186220
if (!hasType(E))
187221
return Type();

lib/Sema/ConstraintSystem.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,6 +1210,16 @@ class Solution {
12101210
: nullptr;
12111211
}
12121212

1213+
/// This method implements functionality of `Expr::isTypeReference`
1214+
/// with data provided by a given solution.
1215+
bool isTypeReference(Expr *E) const;
1216+
1217+
/// Call Expr::isIsStaticallyDerivedMetatype on the given
1218+
/// expression, using a custom accessor for the type on the
1219+
/// expression that reads the type from the Solution
1220+
/// expression type map.
1221+
bool isStaticallyDerivedMetatype(Expr *E) const;
1222+
12131223
SWIFT_DEBUG_DUMP;
12141224

12151225
/// Dump this solution.

0 commit comments

Comments
 (0)