Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
7681 lines (6513 sloc) 294 KB
//===--- CSApply.cpp - Constraint Application -----------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file implements application of a solution to a constraint
// system to a particular expression, resulting in a
// fully-type-checked expression.
//
//===----------------------------------------------------------------------===//
#include "ConstraintSystem.h"
#include "CodeSynthesis.h"
#include "CSDiagnostics.h"
#include "MiscDiagnostics.h"
#include "TypeCheckProtocol.h"
#include "swift/AST/ASTVisitor.h"
#include "swift/AST/ASTWalker.h"
#include "swift/AST/ExistentialLayout.h"
#include "swift/AST/Initializer.h"
#include "swift/AST/GenericSignature.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/ProtocolConformance.h"
#include "swift/AST/SubstitutionMap.h"
#include "swift/Basic/StringExtras.h"
#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/SaveAndRestore.h"
using namespace swift;
using namespace constraints;
/// Retrieve the fixed type for the given type variable.
Type Solution::getFixedType(TypeVariableType *typeVar) const {
auto knownBinding = typeBindings.find(typeVar);
assert(knownBinding != typeBindings.end());
return knownBinding->second;
}
/// Determine whether the given type is an opened AnyObject.
///
/// This comes up in computeSubstitutions() when accessing
/// members via dynamic lookup.
static bool isOpenedAnyObject(Type type) {
auto archetype = type->getAs<OpenedArchetypeType>();
if (!archetype)
return false;
return archetype->getOpenedExistentialType()->isAnyObject();
}
SubstitutionMap Solution::computeSubstitutions(
GenericSignature *sig,
ConstraintLocatorBuilder locatorBuilder) const {
if (sig == nullptr)
return SubstitutionMap();
// Gather the substitutions from dependent types to concrete types.
auto locator = getConstraintSystem().getConstraintLocator(locatorBuilder);
auto openedTypes = OpenedTypes.find(locator);
// If we have a member reference on an existential, there are no
// opened types or substitutions.
if (openedTypes == OpenedTypes.end())
return SubstitutionMap();
TypeSubstitutionMap subs;
for (const auto &opened : openedTypes->second)
subs[opened.first] = getFixedType(opened.second);
auto lookupConformanceFn =
[&](CanType original, Type replacement, ProtocolDecl *protoType)
-> Optional<ProtocolConformanceRef> {
if (replacement->hasError() ||
isOpenedAnyObject(replacement) ||
replacement->is<GenericTypeParamType>()) {
return ProtocolConformanceRef(protoType);
}
return TypeChecker::conformsToProtocol(replacement, protoType,
getConstraintSystem().DC,
ConformanceCheckFlags::InExpression);
};
return SubstitutionMap::get(sig,
QueryTypeSubstitutionMap{subs},
lookupConformanceFn);
}
static bool shouldAccessStorageDirectly(Expr *base, VarDecl *member,
DeclContext *DC) {
// This only matters for stored properties.
if (!member->hasStorage())
return false;
// ... referenced from constructors and destructors.
auto *AFD = dyn_cast<AbstractFunctionDecl>(DC);
if (AFD == nullptr)
return false;
if (!isa<ConstructorDecl>(AFD) && !isa<DestructorDecl>(AFD))
return false;
// ... via a "self.property" reference.
auto *DRE = dyn_cast<DeclRefExpr>(base);
if (DRE == nullptr)
return false;
if (AFD->getImplicitSelfDecl() != cast<DeclRefExpr>(base)->getDecl())
return false;
// Convenience initializers do not require special handling.
// FIXME: This is a language change -- for now, keep the old behavior
#if 0
if (auto *CD = dyn_cast<ConstructorDecl>(AFD))
if (!CD->isDesignatedInit())
return false;
#endif
// Ctor or dtor are for immediate class, not a derived class.
if (!AFD->getParent()->getDeclaredInterfaceType()->isEqual(
member->getDeclContext()->getDeclaredInterfaceType()))
return false;
// If the storage is resilient, we cannot access it directly at all.
if (member->isResilient(DC->getParentModule(),
DC->getResilienceExpansion()))
return false;
return true;
}
/// Return the implicit access kind for a MemberRefExpr with the
/// specified base and member in the specified DeclContext.
static AccessSemantics
getImplicitMemberReferenceAccessSemantics(Expr *base, VarDecl *member,
DeclContext *DC) {
// Properties that have storage and accessors are frequently accessed through
// accessors. However, in the init and destructor methods for the type
// immediately containing the property, accesses are done direct.
if (shouldAccessStorageDirectly(base, member, DC)) {
// Access this directly instead of going through (e.g.) observing or
// trivial accessors.
return AccessSemantics::DirectToStorage;
}
// Check whether this is a member access on 'self'.
bool isAccessOnSelf = false;
if (auto *baseDRE = dyn_cast<DeclRefExpr>(base->getValueProvidingExpr()))
if (auto *baseVar = dyn_cast<VarDecl>(baseDRE->getDecl()))
isAccessOnSelf = baseVar->isSelfParameter();
// If the value is always directly accessed from this context, do it.
return member->getAccessSemanticsFromContext(DC, isAccessOnSelf);
}
/// This extends functionality of `Expr::isTypeReference` with
/// support for `UnresolvedDotExpr` and `UnresolvedMemberExpr`.
/// This method could be used on not yet fully type-checked AST.
bool ConstraintSystem::isTypeReference(const Expr *E) {
return E->isTypeReference(
[&](const Expr *E) -> Type { return simplifyType(getType(E)); },
[&](const Expr *E) -> Decl * {
if (auto *UDE = dyn_cast<UnresolvedDotExpr>(E)) {
return findResolvedMemberRef(
getConstraintLocator(UDE, ConstraintLocator::Member));
}
if (auto *UME = dyn_cast<UnresolvedMemberExpr>(E)) {
return findResolvedMemberRef(
getConstraintLocator(UME, ConstraintLocator::UnresolvedMember));
}
if (isa<OverloadSetRefExpr>(E))
return findResolvedMemberRef(
getConstraintLocator(const_cast<Expr *>(E)));
return nullptr;
});
}
bool ConstraintSystem::isStaticallyDerivedMetatype(const Expr *E) {
return E->isStaticallyDerivedMetatype(
[&](const Expr *E) -> Type { return simplifyType(getType(E)); },
[&](const Expr *E) -> bool { return isTypeReference(E); });
}
Type ConstraintSystem::getInstanceType(const TypeExpr *E) {
return E->getInstanceType([&](const Expr *E) -> bool { return hasType(E); },
[&](const Expr *E) -> Type { return getType(E); });
}
Type ConstraintSystem::getResultType(const AbstractClosureExpr *E) {
return E->getResultType([&](const Expr *E) -> Type { return getType(E); });
}
static bool buildObjCKeyPathString(KeyPathExpr *E,
llvm::SmallVectorImpl<char> &buf) {
for (auto &component : E->getComponents()) {
switch (component.getKind()) {
case KeyPathExpr::Component::Kind::OptionalChain:
case KeyPathExpr::Component::Kind::OptionalForce:
case KeyPathExpr::Component::Kind::OptionalWrap:
// KVC propagates nulls, so these don't affect the key path string.
continue;
case KeyPathExpr::Component::Kind::Identity:
// The identity component can be elided from the KVC string (unless it's
// the only component, in which case we use @"self").
continue;
case KeyPathExpr::Component::Kind::Property: {
// Property references must be to @objc properties.
// TODO: If we added special properties matching KVC operators like '@sum',
// '@count', etc. those could be mapped too.
auto property = cast<VarDecl>(component.getDeclRef().getDecl());
if (!property->isObjC())
return false;
if (!buf.empty()) {
buf.push_back('.');
}
auto objcName = property->getObjCPropertyName().str();
buf.append(objcName.begin(), objcName.end());
continue;
}
case KeyPathExpr::Component::Kind::TupleElement:
case KeyPathExpr::Component::Kind::Subscript:
// Subscripts and tuples aren't generally represented in KVC.
// TODO: There are some subscript forms we could map to KVC, such as
// when indexing a Dictionary or NSDictionary by string, or when applying
// a mapping subscript operation to Array/Set or NSArray/NSSet.
return false;
case KeyPathExpr::Component::Kind::Invalid:
case KeyPathExpr::Component::Kind::UnresolvedProperty:
case KeyPathExpr::Component::Kind::UnresolvedSubscript:
// Don't bother building the key path string if the key path didn't even
// resolve.
return false;
}
}
// If there are no non-identity components, this is the "self" key.
if (buf.empty()) {
auto self = StringRef("self");
buf.append(self.begin(), self.end());
}
return true;
}
/// Form a type checked expression for the index of a @dynamicMemberLookup
/// subscript index parameter.
/// The index expression will have a tuple type of `(dynamicMember: T)`.
static Expr *buildDynamicMemberLookupIndexExpr(StringRef name, SourceLoc loc,
DeclContext *dc,
ConstraintSystem &cs) {
auto &ctx = cs.TC.Context;
auto *stringDecl = ctx.getStringDecl();
auto stringType = stringDecl->getDeclaredType();
// Build and type check the string literal index value to the specific
// string type expected by the subscript.
auto *nameExpr = new (ctx) StringLiteralExpr(name, loc, /*implicit*/true);
nameExpr->setBuiltinInitializer(ctx.getStringBuiltinInitDecl(stringDecl));
nameExpr->setType(stringType);
cs.cacheExprTypes(nameExpr);
return nameExpr;
}
namespace {
/// Rewrites an expression by applying the solution of a constraint
/// system to that expression.
class ExprRewriter : public ExprVisitor<ExprRewriter, Expr *> {
public:
ConstraintSystem &cs;
DeclContext *dc;
const Solution &solution;
bool SuppressDiagnostics;
/// Coerce the given tuple to another tuple type.
///
/// \param expr The expression we're converting.
///
/// \param fromTuple The tuple type we're converting from, which is the same
/// as \c expr->getType().
///
/// \param toTuple The tuple type we're converting to.
///
/// \param locator Locator describing where this tuple conversion occurs.
///
/// \param sources The sources of each of the elements to be used in the
/// resulting tuple, as provided by \c computeTupleShuffle.
Expr *coerceTupleToTuple(Expr *expr, TupleType *fromTuple,
TupleType *toTuple,
ConstraintLocatorBuilder locator,
ArrayRef<unsigned> sources);
/// Coerce a subclass, class-constrained archetype, class-constrained
/// existential or to a superclass type.
///
/// Also supports metatypes of the above.
///
/// \param expr The expression to be coerced.
/// \param toType The type to which the expression will be coerced.
/// \param locator Locator describing where this conversion occurs.
///
/// \return The coerced expression, whose type will be equivalent to
/// \c toType.
Expr *coerceSuperclass(Expr *expr, Type toType,
ConstraintLocatorBuilder locator);
/// Coerce the given value to existential type.
///
/// The following conversions are supported:
/// - concrete to existential
/// - existential to existential
/// - concrete metatype to existential metatype
/// - existential metatype to existential metatype
///
/// \param expr The expression to be coerced.
/// \param toType The type to which the expression will be coerced.
/// \param locator Locator describing where this conversion occurs.
///
/// \return The coerced expression, whose type will be equivalent to
/// \c toType.
Expr *coerceExistential(Expr *expr, Type toType,
ConstraintLocatorBuilder locator);
/// Coerce an expression of (possibly unchecked) optional
/// type to have a different (possibly unchecked) optional type.
Expr *coerceOptionalToOptional(Expr *expr, Type toType,
ConstraintLocatorBuilder locator,
Optional<Pattern*> typeFromPattern = None);
/// Coerce an expression of implicitly unwrapped optional type to its
/// underlying value type, in the correct way for an implicit
/// look-through.
Expr *coerceImplicitlyUnwrappedOptionalToValue(Expr *expr, Type objTy);
/// Peephole an array upcast.
void peepholeArrayUpcast(ArrayExpr *expr, Type toType, bool bridged,
Type elementType,
ConstraintLocatorBuilder locator);
/// Peephole a dictionary upcast.
void peepholeDictionaryUpcast(DictionaryExpr *expr, Type toType,
bool bridged, Type keyType,
Type valueType,
ConstraintLocatorBuilder locator);
/// Try to peephole the collection upcast, eliminating the need for
/// a separate collection-upcast expression.
///
/// \returns true if the peephole operation succeeded, in which case
/// \c expr already subsumes the upcast.
bool peepholeCollectionUpcast(Expr *expr, Type toType, bool bridged,
ConstraintLocatorBuilder locator);
/// Build a collection upcast expression.
///
/// \param bridged Whether this is a bridging conversion, meaning that the
/// element types themselves are bridged (vs. simply coerced).
Expr *buildCollectionUpcastExpr(Expr *expr, Type toType,
bool bridged,
ConstraintLocatorBuilder locator);
/// Build the expression that performs a bridging operation from the
/// given expression to the given \c toType.
Expr *buildObjCBridgeExpr(Expr *expr, Type toType,
ConstraintLocatorBuilder locator);
static Type getBaseType(AnyFunctionType *fnType,
bool wantsRValueInstanceType = true) {
auto params = fnType->getParams();
assert(params.size() == 1);
const auto &base = params.front();
if (wantsRValueInstanceType)
return base.getPlainType()->getMetatypeInstanceType();
return base.getOldType();
}
public:
/// Build a reference to the given declaration.
Expr *buildDeclRef(OverloadChoice choice, DeclNameLoc loc, Type openedType,
ConstraintLocatorBuilder locator, bool implicit,
FunctionRefKind functionRefKind,
AccessSemantics semantics) {
auto *decl = choice.getDecl();
// Determine the declaration selected for this overloaded reference.
auto &ctx = cs.getASTContext();
// If this is a member of a nominal type, build a reference to the
// member with an implied base type.
if (decl->getDeclContext()->isTypeContext() && isa<FuncDecl>(decl)) {
assert(cast<FuncDecl>(decl)->isOperator() && "Must be an operator");
auto openedFnType = openedType->castTo<FunctionType>();
auto simplifiedFnType
= simplifyType(openedFnType)->castTo<FunctionType>();
auto baseTy = getBaseType(simplifiedFnType);
// Handle operator requirements found in protocols.
if (auto proto = dyn_cast<ProtocolDecl>(decl->getDeclContext())) {
// If we don't have an archetype or existential, we have to call the
// witness.
// FIXME: This is awful. We should be able to handle this as a call to
// the protocol requirement with Self == the concrete type, and SILGen
// (or later) can devirtualize as appropriate.
if (!baseTy->is<ArchetypeType>() && !baseTy->isAnyExistentialType()) {
auto &tc = cs.getTypeChecker();
auto conformance =
TypeChecker::conformsToProtocol(
baseTy, proto, cs.DC,
ConformanceCheckFlags::InExpression);
if (conformance && conformance->isConcrete()) {
if (auto witness =
conformance->getConcrete()->getWitnessDecl(decl)) {
// Hack up an AST that we can type-check (independently) to get
// it into the right form.
// FIXME: the hop through 'getDecl()' is because
// SpecializedProtocolConformance doesn't substitute into
// witnesses' ConcreteDeclRefs.
Type expectedFnType = simplifiedFnType->getResult();
Expr *refExpr;
if (witness->getDeclContext()->isTypeContext()) {
Expr *base =
TypeExpr::createImplicitHack(loc.getBaseNameLoc(), baseTy,
ctx);
refExpr = new (ctx) MemberRefExpr(base, SourceLoc(), witness,
loc, /*Implicit=*/true);
} else {
auto declRefExpr = new (ctx) DeclRefExpr(witness, loc,
/*Implicit=*/false);
declRefExpr->setFunctionRefKind(functionRefKind);
refExpr = declRefExpr;
}
auto resultTy = tc.typeCheckExpression(
refExpr, cs.DC, TypeLoc::withoutLoc(expectedFnType),
CTP_CannotFail);
if (!resultTy)
return nullptr;
cs.cacheExprTypes(refExpr);
// Remove an outer function-conversion expression. This
// happens when we end up referring to a witness for a
// superclass conformance, and 'Self' differs.
if (auto fnConv = dyn_cast<FunctionConversionExpr>(refExpr))
refExpr = fnConv->getSubExpr();
return forceUnwrapIfExpected(refExpr, choice, locator);
}
}
}
}
// Build a reference to the protocol requirement.
Expr *base =
TypeExpr::createImplicitHack(loc.getBaseNameLoc(), baseTy, ctx);
cs.cacheExprTypes(base);
return buildMemberRef(base, openedType, SourceLoc(), choice, loc,
openedFnType->getResult(), locator, locator,
implicit, functionRefKind, semantics,
/*isDynamic=*/false);
}
auto type = solution.simplifyType(openedType);
if (isa<TypeDecl>(decl) && !isa<ModuleDecl>(decl)) {
auto typeExpr = TypeExpr::createImplicitHack(
loc.getBaseNameLoc(), type->getMetatypeInstanceType(),
ctx);
cs.cacheType(typeExpr);
return typeExpr;
}
auto substitutions =
solution.computeSubstitutions(
decl->getInnermostDeclContext()->getGenericSignatureOfContext(),
locator);
auto declRefExpr =
new (ctx) DeclRefExpr(ConcreteDeclRef(decl, substitutions),
loc, implicit, semantics, type);
cs.cacheType(declRefExpr);
declRefExpr->setFunctionRefKind(functionRefKind);
return forceUnwrapIfExpected(declRefExpr, choice, locator);
}
/// Describes an opened existential that has not yet been closed.
struct OpenedExistential {
/// The archetype describing this opened existential.
OpenedArchetypeType *Archetype;
/// The existential value being opened.
Expr *ExistentialValue;
/// The opaque value (of archetype type) stored within the
/// existential.
OpaqueValueExpr *OpaqueValue;
/// The depth of this currently-opened existential. Once the
/// depth of the expression stack is equal to this value, the
/// existential can be closed.
unsigned Depth;
};
/// A stack of opened existentials that have not yet been closed.
/// Ordered by decreasing depth.
llvm::SmallVector<OpenedExistential, 2> OpenedExistentials;
/// A stack of expressions being walked, used to compute existential depth.
llvm::SmallVector<Expr *, 8> ExprStack;
/// Members which are AbstractFunctionDecls but not FuncDecls cannot
/// mutate self.
bool isNonMutatingMember(ValueDecl *member) {
if (!isa<AbstractFunctionDecl>(member))
return false;
return !isa<FuncDecl>(member) || !cast<FuncDecl>(member)->isMutating();
}
unsigned getNaturalArgumentCount(ValueDecl *member) {
if (isa<AbstractFunctionDecl>(member)) {
// For functions, close the existential once the function
// has been fully applied.
return 2;
} else {
// For storage, close the existential either when it's
// accessed (if it's an rvalue only) or when it is loaded or
// stored (if it's an lvalue).
assert(isa<AbstractStorageDecl>(member) &&
"unknown member when opening existential");
return 1;
}
}
/// If the expression might be a dynamic method call, return the base
/// value for the call.
Expr *getBaseExpr(Expr *expr) {
// Keep going up as long as this expression is the parent's base.
if (auto unresolvedDot = dyn_cast<UnresolvedDotExpr>(expr)) {
return unresolvedDot->getBase();
// Remaining cases should only come up when we're re-typechecking.
// FIXME: really it would be much better if Sema had stricter phase
// separation.
} else if (auto dotSyntax = dyn_cast<DotSyntaxCallExpr>(expr)) {
return dotSyntax->getArg();
} else if (auto ctorRef = dyn_cast<ConstructorRefCallExpr>(expr)) {
return ctorRef->getArg();
} else if (auto apply = dyn_cast<ApplyExpr>(expr)) {
return apply->getFn();
} else if (auto lookupRef = dyn_cast<LookupExpr>(expr)) {
return lookupRef->getBase();
} else if (auto load = dyn_cast<LoadExpr>(expr)) {
return load->getSubExpr();
} else if (auto inout = dyn_cast<InOutExpr>(expr)) {
return inout->getSubExpr();
} else if (auto force = dyn_cast<ForceValueExpr>(expr)) {
return force->getSubExpr();
} else {
return nullptr;
}
}
/// Calculates the nesting depth of the current application.
unsigned getArgCount(unsigned maxArgCount) {
unsigned e = ExprStack.size();
unsigned argCount;
// Starting from the current expression, count up if the expression is
// equal to its parent expression's base.
Expr *prev = ExprStack.back();
for (argCount = 1; argCount < maxArgCount && argCount < e; argCount++) {
Expr *result = ExprStack[e - argCount - 1];
Expr *base = getBaseExpr(result);
if (base != prev)
break;
prev = result;
}
// Invalid case -- direct call of a metatype. Has one less argument
// application because there's no ".init".
if (isa<ApplyExpr>(ExprStack.back()))
argCount--;
return argCount;
}
/// Open an existential value into a new, opaque value of
/// archetype type.
///
/// \param base An expression of existential type whose value will
/// be opened.
///
/// \param archetype The archetype that describes the opened existential
/// type.
///
/// \param member The member that is being referenced on the existential
/// type.
///
/// \returns An OpaqueValueExpr that provides a reference to the value
/// stored within the expression or its metatype (if the base was a
/// metatype).
Expr *openExistentialReference(Expr *base, OpenedArchetypeType *archetype,
ValueDecl *member) {
assert(archetype && "archetype not already opened?");
auto &tc = cs.getTypeChecker();
// Dig out the base type.
Type baseTy = cs.getType(base);
// Look through lvalues.
bool isLValue = false;
if (auto lvalueTy = baseTy->getAs<LValueType>()) {
isLValue = true;
baseTy = lvalueTy->getObjectType();
}
// Look through metatypes.
bool isMetatype = false;
if (auto metaTy = baseTy->getAs<AnyMetatypeType>()) {
isMetatype = true;
baseTy = metaTy->getInstanceType();
}
assert(baseTy->isAnyExistentialType() && "Type must be existential");
// If the base was an lvalue but it will only be treated as an
// rvalue, turn the base into an rvalue now. This results in
// better SILGen.
if (isLValue &&
(isNonMutatingMember(member) ||
member->getDeclContext()->getDeclaredInterfaceType()
->hasReferenceSemantics())) {
base = cs.coerceToRValue(base);
isLValue = false;
}
// Determine the number of applications that need to occur before
// we can close this existential, and record it.
unsigned maxArgCount = getNaturalArgumentCount(member);
unsigned depth = ExprStack.size() - getArgCount(maxArgCount);
// Create the opaque opened value. If we started with a
// metatype, it's a metatype.
Type opaqueType = archetype;
if (isMetatype)
opaqueType = MetatypeType::get(opaqueType);
if (isLValue)
opaqueType = LValueType::get(opaqueType);
ASTContext &ctx = tc.Context;
auto archetypeVal =
new (ctx) OpaqueValueExpr(base->getSourceRange(), opaqueType);
cs.cacheType(archetypeVal);
// Record the opened existential.
OpenedExistentials.push_back({archetype, base, archetypeVal, depth});
return archetypeVal;
}
/// Trying to close the active existential, if there is one.
bool closeExistential(Expr *&result, ConstraintLocatorBuilder locator,
bool force=false) {
if (OpenedExistentials.empty())
return false;
auto &record = OpenedExistentials.back();
assert(record.Depth <= ExprStack.size());
if (!force && record.Depth < ExprStack.size() - 1)
return false;
// If we had a return type of 'Self', erase it.
ConstraintSystem &innerCS = solution.getConstraintSystem();
auto &tc = innerCS.getTypeChecker();
Type resultTy;
resultTy = cs.getType(result);
if (resultTy->hasOpenedExistential(record.Archetype)) {
Type erasedTy = resultTy->eraseOpenedExistential(record.Archetype);
auto range = result->getSourceRange();
result = coerceToType(result, erasedTy, locator);
// FIXME: Implement missing tuple-to-tuple conversion
if (result == nullptr) {
result = new (tc.Context) ErrorExpr(range);
cs.setType(result, erasedTy);
// The opaque value is no longer reachable in an AST walk as
// a result of the result above being replaced with an
// ErrorExpr, but there is code expecting to have a type set
// on it. Since we no longer have a reachable reference,
// we'll null this out.
record.OpaqueValue = nullptr;
}
}
// Form the open-existential expression.
result = new (tc.Context) OpenExistentialExpr(
record.ExistentialValue,
record.OpaqueValue,
result, cs.getType(result));
cs.cacheType(result);
OpenedExistentials.pop_back();
return true;
}
/// Build a new member reference with the given base and member.
Expr *buildMemberRef(Expr *base, Type openedFullType, SourceLoc dotLoc,
OverloadChoice choice, DeclNameLoc memberLoc,
Type openedType, ConstraintLocatorBuilder locator,
ConstraintLocatorBuilder memberLocator, bool Implicit,
FunctionRefKind functionRefKind,
AccessSemantics semantics, bool isDynamic) {
ValueDecl *member = choice.getDecl();
auto &tc = cs.getTypeChecker();
auto &context = tc.Context;
bool isSuper = base->isSuperExpr();
// The formal type of the 'self' value for the call.
Type baseTy = cs.getType(base)->getRValueType();
// Figure out the actual base type, and whether we have an instance of
// that type or its metatype.
bool baseIsInstance = true;
bool isExistentialMetatype = false;
if (auto baseMeta = baseTy->getAs<AnyMetatypeType>()) {
baseIsInstance = false;
isExistentialMetatype = baseMeta->is<ExistentialMetatypeType>();
baseTy = baseMeta->getInstanceType();
}
// Build a member reference.
SubstitutionMap substitutions =
solution.computeSubstitutions(
member->getInnermostDeclContext()->getGenericSignatureOfContext(),
memberLocator);
auto memberRef = ConcreteDeclRef(member, substitutions);
auto refTy = solution.simplifyType(openedFullType);
// If we're referring to the member of a module, it's just a simple
// reference.
if (baseTy->is<ModuleType>()) {
assert(semantics == AccessSemantics::Ordinary &&
"Direct property access doesn't make sense for this");
auto ref = new (context) DeclRefExpr(memberRef, memberLoc, Implicit);
cs.setType(ref, refTy);
ref->setFunctionRefKind(functionRefKind);
auto *DSBI = cs.cacheType(new (context) DotSyntaxBaseIgnoredExpr(
base, dotLoc, ref, cs.getType(ref)));
return forceUnwrapIfExpected(DSBI, choice, memberLocator);
}
// If we're referring to a member type, it's just a type
// reference.
if (isa<TypeDecl>(member)) {
Type refType = simplifyType(openedType);
auto ref =
TypeExpr::createImplicitHack(memberLoc.getBaseNameLoc(),
refType, context);
cs.setType(ref, refType);
auto *result = new (context) DotSyntaxBaseIgnoredExpr(
base, dotLoc, ref, refType);
cs.setType(result, refType);
return result;
}
// The formal type of the 'self' value for the member's declaration.
Type containerTy = getBaseType(refTy->castTo<FunctionType>());
// If we have an opened existential, selfTy and baseTy will both be
// the same opened existential type.
Type selfTy = containerTy;
// If we opened up an existential when referencing this member, update
// the base accordingly.
auto knownOpened = solution.OpenedExistentialTypes.find(
getConstraintSystem().getConstraintLocator(
memberLocator));
bool openedExistential = false;
if (knownOpened != solution.OpenedExistentialTypes.end()) {
base = openExistentialReference(base, knownOpened->second, member);
baseTy = knownOpened->second;
selfTy = baseTy;
openedExistential = true;
}
// If this is a method whose result type is dynamic Self, or a
// construction, replace the result type with the actual object type.
Type dynamicSelfFnType;
if (!member->getDeclContext()->getSelfProtocolDecl()) {
if (auto func = dyn_cast<AbstractFunctionDecl>(member)) {
if (func->hasDynamicSelfResult() &&
!baseTy->getOptionalObjectType()) {
refTy = refTy->replaceCovariantResultType(containerTy, 2);
if (!baseTy->isEqual(containerTy)) {
dynamicSelfFnType = refTy->replaceCovariantResultType(baseTy, 2);
}
}
} else if (auto *decl = dyn_cast<VarDecl>(member)) {
if (decl->getValueInterfaceType()->hasDynamicSelfType()) {
refTy = refTy->replaceCovariantResultType(containerTy, 1);
if (!baseTy->isEqual(containerTy)) {
dynamicSelfFnType = refTy->replaceCovariantResultType(baseTy, 1);
}
}
}
}
// References to properties with accessors and storage usually go
// through the accessors, but sometimes are direct.
if (auto *VD = dyn_cast<VarDecl>(member)) {
if (semantics == AccessSemantics::Ordinary)
semantics = getImplicitMemberReferenceAccessSemantics(base, VD, dc);
}
if (baseIsInstance) {
// Convert the base to the appropriate container type, turning it
// into an lvalue if required.
// If the base is already an lvalue with the right base type, we can
// pass it as an inout qualified type.
auto selfParamTy = isDynamic ? selfTy : containerTy;
if (selfTy->isEqual(baseTy))
if (cs.getType(base)->is<LValueType>())
selfParamTy = InOutType::get(selfTy);
base = coerceObjectArgumentToType(
base, selfParamTy, member, semantics,
locator.withPathElement(ConstraintLocator::MemberRefBase));
} else {
if (!isExistentialMetatype || openedExistential) {
// Convert the base to an rvalue of the appropriate metatype.
base = coerceToType(base,
MetatypeType::get(
isDynamic ? selfTy : containerTy),
locator.withPathElement(
ConstraintLocator::MemberRefBase));
}
if (!base)
return nullptr;
base = cs.coerceToRValue(base);
}
assert(base && "Unable to convert base?");
// Handle dynamic references.
if (isDynamic || member->getAttrs().hasAttribute<OptionalAttr>()) {
base = cs.coerceToRValue(base);
Expr *ref = new (context) DynamicMemberRefExpr(base, dotLoc, memberRef,
memberLoc);
ref->setImplicit(Implicit);
// FIXME: FunctionRefKind
// Compute the type of the reference.
Type refType = simplifyType(openedType);
// If the base was an opened existential, erase the opened
// existential.
if (openedExistential &&
refType->hasOpenedExistential(knownOpened->second)) {
refType = refType->eraseOpenedExistential(knownOpened->second);
}
cs.setType(ref, refType);
closeExistential(ref, locator, /*force=*/openedExistential);
// If this attribute was inferred based on deprecated Swift 3 rules,
// complain.
if (auto attr = member->getAttrs().getAttribute<ObjCAttr>()) {
if (attr->isSwift3Inferred() &&
tc.Context.LangOpts.WarnSwift3ObjCInference
== Swift3ObjCInferenceWarnings::Minimal) {
tc.diagnose(memberLoc,
diag::expr_dynamic_lookup_swift3_objc_inference,
member->getDescriptiveKind(),
member->getFullName(),
member->getDeclContext()
->getSelfNominalTypeDecl()
->getName());
tc.diagnose(member, diag::make_decl_objc,
member->getDescriptiveKind())
.fixItInsert(member->getAttributeInsertionLoc(false),
"@objc ");
}
}
if (isDynamic) {
// Rewrite for implicit unwrapping if the solution requires it.
auto *dynamicLocator =
cs.getConstraintLocator(memberLocator.withPathElement(
ConstraintLocator::DynamicLookupResult));
if (solution.getDisjunctionChoice(dynamicLocator)) {
auto *forceValue =
new (context) ForceValueExpr(ref, ref->getEndLoc());
auto optTy = cs.getType(forceValue->getSubExpr());
cs.setType(forceValue, optTy->getOptionalObjectType());
ref = forceValue;
}
}
// We also need to handle the implicitly unwrap of the result
// of the called function if that's the type checking solution
// we ended up with.
return forceUnwrapIfExpected(
ref, choice, memberLocator,
member->getAttrs().hasAttribute<OptionalAttr>());
}
// For properties, build member references.
if (isa<VarDecl>(member)) {
if (!baseIsInstance && member->isInstanceMember()) {
assert(memberLocator.getBaseLocator() &&
cs.UnevaluatedRootExprs.count(
memberLocator.getBaseLocator()->getAnchor()) &&
"Attempt to reference an instance member of a metatype");
auto baseInstanceTy = cs.getType(base)
->getInOutObjectType()->getMetatypeInstanceType();
base = new (context) UnevaluatedInstanceExpr(base, baseInstanceTy);
cs.cacheType(base);
base->setImplicit();
}
auto memberRefExpr
= new (context) MemberRefExpr(base, dotLoc, memberRef,
memberLoc, Implicit, semantics);
memberRefExpr->setIsSuper(isSuper);
// Skip the synthesized 'self' input type of the opened type.
cs.setType(memberRefExpr, simplifyType(openedType));
Expr *result = memberRefExpr;
closeExistential(result, locator);
if (dynamicSelfFnType) {
result = new (context) CovariantReturnConversionExpr(result,
dynamicSelfFnType);
cs.cacheType(result);
cs.setType(result, simplifyType(openedType));
}
return forceUnwrapIfExpected(result, choice, memberLocator);
}
// Handle all other references.
auto declRefExpr = new (context) DeclRefExpr(memberRef, memberLoc,
Implicit, semantics);
declRefExpr->setFunctionRefKind(functionRefKind);
cs.setType(declRefExpr, refTy);
Expr *ref = declRefExpr;
// If the reference needs to be converted, do so now.
if (dynamicSelfFnType) {
ref = new (context) CovariantFunctionConversionExpr(ref,
dynamicSelfFnType);
cs.cacheType(ref);
}
ApplyExpr *apply;
if (isa<ConstructorDecl>(member)) {
// FIXME: Provide type annotation.
ref = forceUnwrapIfExpected(ref, choice, memberLocator);
apply = new (context) ConstructorRefCallExpr(ref, base);
} else if (!baseIsInstance && member->isInstanceMember()) {
// Reference to an unbound instance method.
Expr *result = new (context) DotSyntaxBaseIgnoredExpr(base, dotLoc,
ref,
cs.getType(ref));
cs.cacheType(result);
closeExistential(result, locator, /*force=*/openedExistential);
return forceUnwrapIfExpected(result, choice, memberLocator);
} else {
assert((!baseIsInstance || member->isInstanceMember()) &&
"can't call a static method on an instance");
ref = forceUnwrapIfExpected(ref, choice, memberLocator);
apply = new (context) DotSyntaxCallExpr(ref, dotLoc, base);
if (Implicit) {
apply->setImplicit();
}
}
return finishApply(apply, openedType, locator);
}
/// Describes either a type or the name of a type to be resolved.
using TypeOrName = llvm::PointerUnion<Identifier, Type>;
/// Convert the given literal expression via a protocol pair.
///
/// This routine handles the two-step literal conversion process used
/// by integer, float, character, extended grapheme cluster, and string
/// literals. The first step uses \c builtinProtocol while the second
/// step uses \c protocol.
///
/// \param literal The literal expression.
///
/// \param type The literal type. This type conforms to \c protocol,
/// and may also conform to \c builtinProtocol.
///
/// \param protocol The protocol that describes the literal requirement.
///
/// \param literalType The name of the associated type in \c protocol that
/// describes the argument type of the conversion function (\c
/// literalFuncName).
///
/// \param literalFuncName The name of the conversion function requirement
/// in \c protocol.
///
/// \param builtinProtocol The "builtin" form of the protocol, which
/// always takes builtin types and can only be properly implemented
/// by standard library types. If \c type does not conform to this
/// protocol, it's literal type will.
///
/// \param builtinLiteralFuncName The name of the conversion function
/// requirement in \c builtinProtocol.
///
/// \param brokenProtocolDiag The diagnostic to emit if the protocol
/// is broken.
///
/// \param brokenBuiltinProtocolDiag The diagnostic to emit if the builtin
/// protocol is broken.
///
/// \returns the converted literal expression.
Expr *convertLiteralInPlace(Expr *literal,
Type type,
ProtocolDecl *protocol,
Identifier literalType,
DeclName literalFuncName,
ProtocolDecl *builtinProtocol,
DeclName builtinLiteralFuncName,
Diag<> brokenProtocolDiag,
Diag<> brokenBuiltinProtocolDiag);
/// Finish a function application by performing the appropriate
/// conversions on the function and argument expressions and setting
/// the resulting type.
///
/// \param apply The function application to finish type-checking, which
/// may be a newly-built expression.
///
/// \param openedType The "opened" type this expression had during
/// type checking, which will be used to specialize the resulting,
/// type-checked expression appropriately.
///
/// \param locator The locator for the original expression.
Expr *finishApply(ApplyExpr *apply, Type openedType,
ConstraintLocatorBuilder locator);
// Resolve @dynamicCallable applications.
Expr *finishApplyDynamicCallable(const Solution &solution, ApplyExpr *apply,
ConstraintLocatorBuilder locator);
private:
/// Simplify the given type by substituting all occurrences of
/// type variables for their fixed types.
Type simplifyType(Type type) {
return solution.simplifyType(type);
}
public:
/// Coerce a closure expression with a non-Void return type to a
/// contextual function type with a Void return type.
///
/// This operation cannot fail.
///
/// \param expr The closure expression to coerce.
///
/// \returns The coerced closure expression.
///
ClosureExpr *coerceClosureExprToVoid(ClosureExpr *expr);
/// Coerce a closure expression with a Never return type to a
/// contextual function type with some other return type.
///
/// This operation cannot fail.
///
/// \param expr The closure expression to coerce.
///
/// \returns The coerced closure expression.
///
ClosureExpr *coerceClosureExprFromNever(ClosureExpr *expr);
/// Coerce the given expression to the given type.
///
/// This operation cannot fail.
///
/// \param expr The expression to coerce.
/// \param toType The type to coerce the expression to.
/// \param locator Locator used to describe where in this expression we are.
/// \param typeFromPattern Optionally, the caller can specify the pattern
/// from where the toType is derived, so that we can deliver better fixit.
///
/// \returns the coerced expression, which will have type \c ToType.
Expr *coerceToType(Expr *expr, Type toType,
ConstraintLocatorBuilder locator,
Optional<Pattern*> typeFromPattern = None);
/// Coerce the given expression (which is the argument to a call) to
/// the given parameter type.
///
/// This operation cannot fail.
///
/// \param arg The argument expression.
/// \param funcType The function type.
/// \param apply The ApplyExpr that forms the call.
/// \param argLabels The argument labels provided for the call.
/// \param hasTrailingClosure Whether the last argument is a trailing
/// closure.
/// \param locator Locator used to describe where in this expression we are.
///
/// \returns the coerced expression, which will have type \c ToType.
Expr *
coerceCallArguments(Expr *arg, AnyFunctionType *funcType,
ApplyExpr *apply,
ArrayRef<Identifier> argLabels,
bool hasTrailingClosure,
ConstraintLocatorBuilder locator);
/// Coerce the given object argument (e.g., for the base of a
/// member expression) to the given type.
///
/// \param expr The expression to coerce.
///
/// \param baseTy The base type
///
/// \param member The member being accessed.
///
/// \param semantics The kind of access we've been asked to perform.
///
/// \param locator Locator used to describe where in this expression we are.
Expr *coerceObjectArgumentToType(Expr *expr,
Type baseTy, ValueDecl *member,
AccessSemantics semantics,
ConstraintLocatorBuilder locator);
private:
/// Build a new subscript.
///
/// \param base The base of the subscript.
/// \param index The index of the subscript.
/// \param locator The locator used to refer to the subscript.
/// \param isImplicit Whether this is an implicit subscript.
Expr *buildSubscript(Expr *base, Expr *index,
ArrayRef<Identifier> argLabels,
bool hasTrailingClosure,
ConstraintLocatorBuilder locator, bool isImplicit,
AccessSemantics semantics,
Optional<SelectedOverload> selected = None) {
// Determine the declaration selected for this subscript operation.
if (!selected)
selected = solution.getOverloadChoiceIfAvailable(
cs.getConstraintLocator(
locator.withPathElement(
ConstraintLocator::SubscriptMember)));
// Handles situation where there was a solution available but it didn't
// have a proper overload selected from subscript call, might be because
// solver was allowed to return free or unresolved types, which can
// happen while running diagnostics on one of the expressions.
if (!selected.hasValue()) {
auto &tc = cs.TC;
auto baseType = cs.getType(base);
if (auto errorType = baseType->getAs<ErrorType>()) {
tc.diagnose(base->getLoc(), diag::cannot_subscript_base,
errorType->getOriginalType())
.highlight(base->getSourceRange());
} else {
tc.diagnose(base->getLoc(), diag::cannot_subscript_ambiguous_base)
.highlight(base->getSourceRange());
}
return nullptr;
}
// Build the new subscript.
auto newSubscript = buildSubscriptHelper(base, index, argLabels,
*selected, hasTrailingClosure,
locator, isImplicit, semantics);
if (selected->choice.getKind() == OverloadChoiceKind::DeclViaDynamic) {
// Rewrite for implicit unwrapping if the solution requires it.
auto *dynamicLocator = cs.getConstraintLocator(
locator.withPathElement(ConstraintLocator::SubscriptMember)
.withPathElement(ConstraintLocator::DynamicLookupResult));
if (solution.getDisjunctionChoice(dynamicLocator)) {
auto *forceValue = new (cs.getASTContext())
ForceValueExpr(newSubscript, newSubscript->getEndLoc());
auto optTy = cs.getType(forceValue->getSubExpr());
cs.setType(forceValue, optTy->getOptionalObjectType());
newSubscript = forceValue;
}
}
if (selected->choice.isDecl()) {
auto locatorKind = ConstraintLocator::SubscriptMember;
if (selected->choice.getKind() ==
OverloadChoiceKind::DynamicMemberLookup)
locatorKind = ConstraintLocator::Member;
if (selected->choice.getKind() ==
OverloadChoiceKind::KeyPathDynamicMemberLookup &&
!isa<SubscriptExpr>(locator.getAnchor()))
locatorKind = ConstraintLocator::Member;
newSubscript =
forceUnwrapIfExpected(newSubscript, selected->choice,
locator.withPathElement(locatorKind));
}
return newSubscript;
}
Expr *buildSubscriptHelper(Expr *base, Expr *index,
ArrayRef<Identifier> argLabels,
SelectedOverload &selected,
bool hasTrailingClosure,
ConstraintLocatorBuilder locator,
bool isImplicit, AccessSemantics semantics) {
auto choice = selected.choice;
// Apply a key path if we have one.
if (choice.getKind() == OverloadChoiceKind::KeyPathApplication) {
auto applicationTy =
simplifyType(selected.openedType)->castTo<FunctionType>();
index = cs.coerceToRValue(index);
// The index argument should be (keyPath: KeyPath<Root, Value>).
// Dig the key path expression out of the arguments.
auto indexKP = cast<TupleExpr>(index)->getElement(0);
auto keyPathExprTy = cs.getType(indexKP);
auto keyPathTy = applicationTy->getParams().front().getOldType();
Type valueTy;
Type baseTy;
bool resultIsLValue;
if (auto nom = keyPathTy->getAs<NominalType>()) {
// AnyKeyPath is <T> rvalue T -> rvalue Any?
if (nom->getDecl() == cs.getASTContext().getAnyKeyPathDecl()) {
valueTy = ProtocolCompositionType::get(cs.getASTContext(), {},
/*explicit anyobject*/ false);
valueTy = OptionalType::get(valueTy);
resultIsLValue = false;
base = cs.coerceToRValue(base);
baseTy = cs.getType(base);
// We don't really want to attempt AnyKeyPath application
// if we know a more specific key path type is being applied.
if (!keyPathTy->isEqual(keyPathExprTy)) {
cs.TC.diagnose(base->getLoc(),
diag::expr_smart_keypath_application_type_mismatch,
keyPathExprTy,
baseTy)
.highlight(index->getSourceRange());
}
} else {
llvm_unreachable("unknown key path class!");
}
} else {
auto keyPathBGT = keyPathTy->castTo<BoundGenericType>();
baseTy = keyPathBGT->getGenericArgs()[0];
// Coerce the index to the key path's type
indexKP = coerceToType(indexKP, keyPathTy, locator);
// Coerce the base to the key path's expected base type.
if (!baseTy->isEqual(cs.getType(base)->getRValueType()))
base = coerceToType(base, baseTy, locator);
if (keyPathBGT->getDecl()
== cs.getASTContext().getPartialKeyPathDecl()) {
// PartialKeyPath<T> is rvalue T -> rvalue Any
valueTy = ProtocolCompositionType::get(cs.getASTContext(), {},
/*explicit anyobject*/ false);
resultIsLValue = false;
base = cs.coerceToRValue(base);
} else {
// *KeyPath<T, U> is T -> U, with rvalueness based on mutability
// of base and keypath
valueTy = keyPathBGT->getGenericArgs()[1];
// The result may be an lvalue based on the base and key path kind.
if (keyPathBGT->getDecl() == cs.getASTContext().getKeyPathDecl()) {
resultIsLValue = false;
base = cs.coerceToRValue(base);
} else if (keyPathBGT->getDecl() ==
cs.getASTContext().getWritableKeyPathDecl()) {
resultIsLValue = cs.getType(base)->hasLValueType();
} else if (keyPathBGT->getDecl() ==
cs.getASTContext().getReferenceWritableKeyPathDecl()) {
resultIsLValue = true;
base = cs.coerceToRValue(base);
} else {
llvm_unreachable("unknown key path class!");
}
}
}
if (resultIsLValue)
valueTy = LValueType::get(valueTy);
auto keyPathAp = new (cs.getASTContext())
KeyPathApplicationExpr(base, index->getStartLoc(), indexKP,
index->getEndLoc(), valueTy,
base->isImplicit() && index->isImplicit());
cs.setType(keyPathAp, valueTy);
return keyPathAp;
}
auto subscript = cast<SubscriptDecl>(choice.getDecl());
auto &tc = cs.getTypeChecker();
auto baseTy = cs.getType(base)->getRValueType();
bool baseIsInstance = true;
if (auto baseMeta = baseTy->getAs<AnyMetatypeType>()) {
baseIsInstance = false;
baseTy = baseMeta->getInstanceType();
}
// Check whether the base is 'super'.
bool isSuper = base->isSuperExpr();
// Use the correct locator kind based on the subscript kind.
auto locatorKind = ConstraintLocator::SubscriptMember;
if (choice.getKind() == OverloadChoiceKind::DynamicMemberLookup)
locatorKind = ConstraintLocator::Member;
if (choice.getKind() == OverloadChoiceKind::KeyPathDynamicMemberLookup) {
locatorKind = isa<SubscriptExpr>(locator.getAnchor())
? ConstraintLocator::SubscriptMember
: ConstraintLocator::Member;
}
// If we opened up an existential when performing the subscript, open
// the base accordingly.
auto knownOpened = solution.OpenedExistentialTypes.find(
getConstraintSystem().getConstraintLocator(
locator.withPathElement(locatorKind)));
if (knownOpened != solution.OpenedExistentialTypes.end()) {
base = openExistentialReference(base, knownOpened->second, subscript);
baseTy = knownOpened->second;
}
// Figure out the index and result types.
Type resultTy;
if (choice.getKind() != OverloadChoiceKind::DynamicMemberLookup &&
choice.getKind() != OverloadChoiceKind::KeyPathDynamicMemberLookup) {
auto subscriptTy = simplifyType(selected.openedType);
auto *subscriptFnTy = subscriptTy->castTo<FunctionType>();
resultTy = subscriptFnTy->getResult();
// Coerce the index argument.
index = coerceCallArguments(index, subscriptFnTy, nullptr,
argLabels, hasTrailingClosure,
locator.withPathElement(
ConstraintLocator::ApplyArgument));
if (!index)
return nullptr;
} else {
// If this is a @dynamicMemberLookup, then the type of the selection is
// actually the property/result type. That's fine though, and we
// already have the index type adjusted to the correct type expected by
// the subscript.
resultTy = simplifyType(selected.openedType);
}
auto getType = [&](const Expr *E) -> Type {
return cs.getType(E);
};
// Form the subscript expression.
// Compute the substitutions used to reference the subscript.
SubstitutionMap substitutions =
solution.computeSubstitutions(
subscript->getInnermostDeclContext()->getGenericSignatureOfContext(),
locator.withPathElement(locatorKind));
ConcreteDeclRef subscriptRef(subscript, substitutions);
// Handle dynamic lookup.
if (choice.getKind() == OverloadChoiceKind::DeclViaDynamic ||
subscript->getAttrs().hasAttribute<OptionalAttr>()) {
base = coerceObjectArgumentToType(base, baseTy, subscript,
AccessSemantics::Ordinary, locator);
if (!base)
return nullptr;
// TODO: diagnose if semantics != AccessSemantics::Ordinary?
auto subscriptExpr = DynamicSubscriptExpr::create(tc.Context, base,
index, subscriptRef,
isImplicit, getType);
cs.setType(subscriptExpr, resultTy);
Expr *result = subscriptExpr;
closeExistential(result, locator);
return result;
}
// Convert the base.
auto openedFullFnType = selected.openedFullType->castTo<FunctionType>();
auto openedBaseType =
getBaseType(openedFullFnType, /*wantsRValue*/ false);
auto containerTy = solution.simplifyType(openedBaseType);
if (baseIsInstance) {
base = coerceObjectArgumentToType(
base, containerTy, subscript, AccessSemantics::Ordinary,
locator.withPathElement(ConstraintLocator::MemberRefBase));
} else {
base = coerceToType(base,
MetatypeType::get(containerTy),
locator.withPathElement(
ConstraintLocator::MemberRefBase));
if (!base)
return nullptr;
base = cs.coerceToRValue(base);
}
if (!base)
return nullptr;
// Form the subscript expression.
auto subscriptExpr = SubscriptExpr::create(
tc.Context, base, index, subscriptRef, isImplicit, semantics, getType);
cs.setType(subscriptExpr, resultTy);
subscriptExpr->setIsSuper(isSuper);
Expr *result = subscriptExpr;
closeExistential(result, locator);
if (subscript->getElementInterfaceType()->hasDynamicSelfType()) {
auto dynamicSelfFnType =
openedFullFnType->replaceCovariantResultType(baseTy, 2);
result = new (tc.Context) CovariantReturnConversionExpr(result,
dynamicSelfFnType);
cs.cacheType(result);
cs.setType(result, simplifyType(baseTy));
}
return result;
}
/// Build a new reference to another constructor.
Expr *buildOtherConstructorRef(Type openedFullType,
ConstructorDecl *ctor, Expr *base,
DeclNameLoc loc,
ConstraintLocatorBuilder locator,
bool implicit) {
auto &tc = cs.getTypeChecker();
auto &ctx = tc.Context;
// Compute the concrete reference.
SubstitutionMap substitutions =
solution.computeSubstitutions(ctor->getGenericSignature(), locator);
auto ref = ConcreteDeclRef(ctor, substitutions);
// The constructor was opened with the allocating type, not the
// initializer type. Map the former into the latter.
auto resultTy = solution.simplifyType(openedFullType);
auto selfTy = getBaseType(resultTy->castTo<FunctionType>());
// Also replace the result type with the base type, so that calls
// to constructors defined in a superclass will know to cast the
// result to the derived type.
resultTy = resultTy->replaceCovariantResultType(selfTy, 2);
ParameterTypeFlags flags;
if (!selfTy->hasReferenceSemantics())
flags = flags.withInOut(true);
auto selfParam = AnyFunctionType::Param(selfTy, Identifier(), flags);
resultTy = FunctionType::get({selfParam},
resultTy->castTo<FunctionType>()->getResult(),
resultTy->castTo<FunctionType>()->getExtInfo());
// Build the constructor reference.
Expr *ctorRef = cs.cacheType(
new (ctx) OtherConstructorDeclRefExpr(ref, loc, implicit, resultTy));
// Wrap in covariant `Self` return if needed.
if (selfTy->hasReferenceSemantics()) {
auto covariantTy = resultTy->replaceCovariantResultType(
cs.getType(base)->getWithoutSpecifierType(), 2);
if (!covariantTy->isEqual(resultTy))
ctorRef = cs.cacheType(
new (ctx) CovariantFunctionConversionExpr(ctorRef, covariantTy));
}
return ctorRef;
}
/// Build an implicit argument for keypath based dynamic lookup,
/// which consists of KeyPath expression and a single component.
///
/// \param keyPathTy The type of the keypath argument.
/// \param dotLoc The location of the '.' preceding member name.
/// \param memberLoc The locator to be associated with new argument.
Expr *buildKeyPathDynamicMemberIndexExpr(BoundGenericType *keyPathTy,
SourceLoc dotLoc,
ConstraintLocator *memberLoc) {
auto &ctx = cs.getASTContext();
auto *anchor = memberLoc->getAnchor();
KeyPathExpr::Component component;
// Let's create a KeyPath expression and fill in "parsed path"
// after component is built.
auto *keyPath = new (ctx) KeyPathExpr(/*backslashLoc=*/dotLoc,
/*parsedRoot=*/nullptr,
/*parsedPath=*/anchor,
/*isImplicit=*/true);
// Type of the keypath expression we are forming is known
// in advance, so let's set it right away.
keyPath->setType(keyPathTy);
cs.cacheType(keyPath);
auto *componentLoc = cs.getConstraintLocator(
memberLoc,
LocatorPathElt::KeyPathDynamicMember(keyPathTy->getAnyNominal()));
auto overload = solution.getOverloadChoice(componentLoc);
// Looks like there is a chain of implicit `subscript(dynamicMember:)`
// calls necessary to resolve a member reference.
if (overload.choice.getKind() ==
OverloadChoiceKind::KeyPathDynamicMemberLookup) {
keyPath->resolveComponents(ctx,
buildKeyPathSubscriptComponent(
overload, dotLoc, /*indexExpr=*/nullptr,
ctx.Id_dynamicMember, componentLoc));
return keyPath;
}
// We can't reuse existing expression because type-check
// based diagnostics could hold the reference to original AST.
Expr *componentExpr = nullptr;
auto *dotExpr = new (ctx) KeyPathDotExpr(dotLoc);
// Determines whether this index is built to be used for
// one of the existing keypath components e.g. `\Lens<[Int]>.count`
// instead of a regular expression e.g. `lens[0]`.
bool forKeyPathComponent = false;
// Looks like keypath dynamic member lookup was used inside
// of a keypath expression e.g. `\Lens<[Int]>.count` where
// `count` is referenced using dynamic lookup.
if (auto *KPE = dyn_cast<KeyPathExpr>(anchor)) {
auto kpElt = memberLoc->findFirst<LocatorPathElt::KeyPathComponent>();
assert(kpElt && "no keypath component node");
auto &origComponent = KPE->getComponents()[kpElt->getIndex()];
using ComponentKind = KeyPathExpr::Component::Kind;
if (origComponent.getKind() == ComponentKind::UnresolvedProperty) {
anchor = new (ctx) UnresolvedDotExpr(
dotExpr, dotLoc, origComponent.getUnresolvedDeclName(),
DeclNameLoc(origComponent.getLoc()),
/*Implicit=*/true);
} else if (origComponent.getKind() ==
ComponentKind::UnresolvedSubscript) {
anchor = SubscriptExpr::create(
ctx, dotExpr, origComponent.getIndexExpr(), ConcreteDeclRef(),
/*implicit=*/true, AccessSemantics::Ordinary,
[&](const Expr *expr) { return simplifyType(cs.getType(expr)); });
} else {
return nullptr;
}
anchor->setType(simplifyType(overload.openedType));
cs.cacheType(anchor);
forKeyPathComponent = true;
}
if (auto *UDE = dyn_cast<UnresolvedDotExpr>(anchor)) {
componentExpr =
forKeyPathComponent
? UDE
: new (ctx) UnresolvedDotExpr(dotExpr, dotLoc, UDE->getName(),
UDE->getNameLoc(),
/*Implicit=*/true);
component = buildKeyPathPropertyComponent(overload, UDE->getLoc(),
componentLoc);
} else if (auto *SE = dyn_cast<SubscriptExpr>(anchor)) {
componentExpr = SE;
// If this is not for a keypath component, we have to copy
// original subscript expression because expression based
// diagnostics might have a reference to it, so it couldn't
// be modified.
if (!forKeyPathComponent) {
SmallVector<Expr *, 4> arguments;
if (auto *TE = dyn_cast<TupleExpr>(SE->getIndex())) {
auto args = TE->getElements();
arguments.append(args.begin(), args.end());
} else {
arguments.push_back(SE->getIndex()->getSemanticsProvidingExpr());
}
Expr *trailingClosure = nullptr;
if (SE->hasTrailingClosure())
trailingClosure = arguments.back();
componentExpr = SubscriptExpr::create(
ctx, dotExpr, SE->getStartLoc(), arguments,
SE->getArgumentLabels(), SE->getArgumentLabelLocs(),
SE->getEndLoc(), trailingClosure,
SE->hasDecl() ? SE->getDecl() : ConcreteDeclRef(),
/*implicit=*/true, SE->getAccessSemantics());
}
component = buildKeyPathSubscriptComponent(
overload, SE->getLoc(), SE->getIndex(), SE->getArgumentLabels(),
componentLoc);
} else {
return nullptr;
}
assert(componentExpr);
Type ty = simplifyType(cs.getType(anchor));
componentExpr->setType(ty);
cs.cacheType(componentExpr);
cs.setType(keyPath, 0, ty);
keyPath->setParsedPath(componentExpr);
keyPath->resolveComponents(ctx, {component});
return keyPath;
}
/// Bridge the given value (which is an error type) to NSError.
Expr *bridgeErrorToObjectiveC(Expr *value) {
auto &tc = cs.getTypeChecker();
auto nsErrorDecl = tc.Context.getNSErrorDecl();
assert(nsErrorDecl && "Missing NSError?");
Type nsErrorType = nsErrorDecl->getDeclaredInterfaceType();
auto result = new (tc.Context) BridgeToObjCExpr(value, nsErrorType);
return cs.cacheType(result);
}
/// Bridge the given value to its corresponding Objective-C object
/// type.
///
/// This routine should only be used for bridging value types.
///
/// \param value The value to be bridged.
Expr *bridgeToObjectiveC(Expr *value, Type objcType) {
auto &tc = cs.getTypeChecker();
auto result = new (tc.Context) BridgeToObjCExpr(value, objcType);
return cs.cacheType(result);
}
/// Bridge the given object from Objective-C to its value type.
///
/// This routine should only be used for bridging value types.
///
/// \param object The object, whose type should already be of the type
/// that the value type bridges through.
///
/// \param valueType The value type to which we are bridging.
///
/// \param conditional Whether the bridging should be conditional. If false,
/// uses forced bridging.
///
/// \returns a value of type \c valueType (optional if \c conditional) that
/// stores the bridged result or (when \c conditional) an empty optional if
/// conditional bridging fails.
Expr *bridgeFromObjectiveC(Expr *object, Type valueType, bool conditional) {
auto &tc = cs.getTypeChecker();
if (!conditional) {
auto result = new (tc.Context) BridgeFromObjCExpr(object, valueType);
return cs.cacheType(result);
}
// Find the _BridgedToObjectiveC protocol.
auto bridgedProto
= tc.Context.getProtocol(KnownProtocolKind::ObjectiveCBridgeable);
// Try to find the conformance of the value type to _BridgedToObjectiveC.
auto bridgedToObjectiveCConformance
= TypeChecker::conformsToProtocol(valueType,
bridgedProto,
cs.DC,
ConformanceCheckFlags::InExpression);
FuncDecl *fn = nullptr;
if (bridgedToObjectiveCConformance) {
assert(bridgedToObjectiveCConformance->getConditionalRequirements()
.empty() &&
"cannot conditionally conform to _BridgedToObjectiveC");
// The conformance to _BridgedToObjectiveC is statically known.
// Retrieve the bridging operation to be used if a static conformance
// to _BridgedToObjectiveC can be proven.
fn = conditional
? tc.Context.getConditionallyBridgeFromObjectiveCBridgeable()
: tc.Context.getForceBridgeFromObjectiveCBridgeable();
} else {
// Retrieve the bridging operation to be used if a static conformance
// to _BridgedToObjectiveC cannot be proven.
fn = conditional ? tc.Context.getConditionallyBridgeFromObjectiveC()
: tc.Context.getForceBridgeFromObjectiveC();
}
if (!fn) {
tc.diagnose(object->getLoc(), diag::missing_bridging_function,
conditional);
return nullptr;
}
tc.validateDecl(fn);
// Form a reference to the function. The bridging operations are generic,
// so we need to form substitutions and compute the resulting type.
auto genericSig = fn->getGenericSignature();
auto subMap = SubstitutionMap::get(
genericSig,
[&](SubstitutableType *type) -> Type {
assert(type->isEqual(genericSig->getGenericParams()[0]));
return valueType;
},
[&](CanType origType, Type replacementType, ProtocolDecl *protoType)
-> ProtocolConformanceRef {
assert(bridgedToObjectiveCConformance);
return *bridgedToObjectiveCConformance;
});
ConcreteDeclRef fnSpecRef(fn, subMap);
auto resultType = OptionalType::get(valueType);
auto result = new (tc.Context) ConditionalBridgeFromObjCExpr(object,
resultType, fnSpecRef);
return cs.cacheType(result);
}
/// Bridge the given object from Objective-C to its value type.
///
/// This routine should only be used for bridging value types.
///
/// \param object The object, whose type should already be of the type
/// that the value type bridges through.
///
/// \param valueType The value type to which we are bridging.
///
/// \returns a value of type \c valueType that stores the bridged result.
Expr *forceBridgeFromObjectiveC(Expr *object, Type valueType) {
return bridgeFromObjectiveC(object, valueType, false);
}
public:
ExprRewriter(ConstraintSystem &cs, const Solution &solution,
bool suppressDiagnostics)
: cs(cs), dc(cs.DC), solution(solution),
SuppressDiagnostics(suppressDiagnostics) {}
ConstraintSystem &getConstraintSystem() const { return cs; }
/// Simplify the expression type and return the expression.
///
/// This routine is used for 'simple' expressions that only need their
/// types simplified, with no further computation.
Expr *simplifyExprType(Expr *expr) {
auto toType = simplifyType(cs.getType(expr));
cs.setType(expr, toType);
return expr;
}
Expr *visitErrorExpr(ErrorExpr *expr) {
// Do nothing with error expressions.
return expr;
}
Expr *visitCodeCompletionExpr(CodeCompletionExpr *expr) {
// Do nothing with code completion expressions.
auto toType = simplifyType(cs.getType(expr));
cs.setType(expr, toType);
return expr;
}
Expr *handleIntegerLiteralExpr(LiteralExpr *expr) {
// If the literal has been assigned a builtin integer type,
// don't mess with it.
if (cs.getType(expr)->is<AnyBuiltinIntegerType>())
return expr;
auto &tc = cs.getTypeChecker();
ProtocolDecl *protocol
= tc.getProtocol(expr->getLoc(),
KnownProtocolKind::ExpressibleByIntegerLiteral);
ProtocolDecl *builtinProtocol
= tc.getProtocol(expr->getLoc(),
KnownProtocolKind::ExpressibleByBuiltinIntegerLiteral);
// For type-sugar reasons, prefer the spelling of the default literal
// type.
auto type = simplifyType(cs.getType(expr));
if (auto defaultType = tc.getDefaultType(protocol, dc)) {
if (defaultType->isEqual(type))
type = defaultType;
}
if (auto floatProtocol
= tc.getProtocol(expr->getLoc(),
KnownProtocolKind::ExpressibleByFloatLiteral)) {
if (auto defaultFloatType = tc.getDefaultType(floatProtocol, dc)) {
if (defaultFloatType->isEqual(type))
type = defaultFloatType;
}
}
DeclName initName(tc.Context, DeclBaseName::createConstructor(),
{ tc.Context.Id_integerLiteral });
DeclName builtinInitName(tc.Context, DeclBaseName::createConstructor(),
{ tc.Context.Id_builtinIntegerLiteral });
auto *result = convertLiteralInPlace(
expr, type, protocol, tc.Context.Id_IntegerLiteralType, initName,
builtinProtocol, builtinInitName, diag::integer_literal_broken_proto,
diag::builtin_integer_literal_broken_proto);
if (result) {
// TODO: It seems that callers expect this to have types assigned...
result->setType(cs.getType(result));
}
return result;
}
Expr *visitNilLiteralExpr(NilLiteralExpr *expr) {
auto type = simplifyType(cs.getType(expr));
// By far the most common 'nil' literal is for Optional<T>.none.
// We don't have to look up the witness in this case since SILGen
// knows how to lower it directly.
if (auto objectType = type->getOptionalObjectType()) {
cs.setType(expr, type);
return expr;
}
auto &tc = cs.getTypeChecker();
auto *protocol = tc.getProtocol(expr->getLoc(),
KnownProtocolKind::ExpressibleByNilLiteral);
// For type-sugar reasons, prefer the spelling of the default literal
// type.
if (auto defaultType = tc.getDefaultType(protocol, dc)) {
if (defaultType->isEqual(type))
type = defaultType;
}
DeclName initName(tc.Context, DeclBaseName::createConstructor(),
{ tc.Context.Id_nilLiteral });
return convertLiteralInPlace(expr, type, protocol,
Identifier(), initName,
nullptr,
Identifier(),
diag::nil_literal_broken_proto,
diag::nil_literal_broken_proto);
}
Expr *visitIntegerLiteralExpr(IntegerLiteralExpr *expr) {
return handleIntegerLiteralExpr(expr);
}
Expr *visitFloatLiteralExpr(FloatLiteralExpr *expr) {
// If the literal has been assigned a builtin float type,
// don't mess with it.
if (cs.getType(expr)->is<BuiltinFloatType>())
return expr;
auto &tc = cs.getTypeChecker();
ProtocolDecl *protocol
= tc.getProtocol(expr->getLoc(),
KnownProtocolKind::ExpressibleByFloatLiteral);
ProtocolDecl *builtinProtocol
= tc.getProtocol(expr->getLoc(),
KnownProtocolKind::ExpressibleByBuiltinFloatLiteral);
// For type-sugar reasons, prefer the spelling of the default literal
// type.
auto type = simplifyType(cs.getType(expr));
if (auto defaultType = tc.getDefaultType(protocol, dc)) {
if (defaultType->isEqual(type))
type = defaultType;
}
// Get the _MaxBuiltinFloatType decl, or look for it if it's not cached.
auto maxFloatTypeDecl = tc.Context.get_MaxBuiltinFloatTypeDecl();
if (!maxFloatTypeDecl ||
!maxFloatTypeDecl->hasInterfaceType() ||
!maxFloatTypeDecl->getDeclaredInterfaceType()->is<BuiltinFloatType>()) {
tc.diagnose(expr->getLoc(), diag::no_MaxBuiltinFloatType_found);
return nullptr;
}
tc.validateDecl(maxFloatTypeDecl);
auto maxType = maxFloatTypeDecl->getUnderlyingTypeLoc().getType();
DeclName initName(tc.Context, DeclBaseName::createConstructor(),
{ tc.Context.Id_floatLiteral });
DeclName builtinInitName(tc.Context, DeclBaseName::createConstructor(),
{ tc.Context.Id_builtinFloatLiteral });
expr->setBuiltinType(maxType);
return convertLiteralInPlace(
expr, type, protocol, tc.Context.Id_FloatLiteralType, initName,
builtinProtocol, builtinInitName, diag::float_literal_broken_proto,
diag::builtin_float_literal_broken_proto);
}
Expr *visitBooleanLiteralExpr(BooleanLiteralExpr *expr) {
if (cs.getType(expr) && cs.getType(expr)->is<BuiltinIntegerType>())
return expr;
auto &tc = cs.getTypeChecker();
ProtocolDecl *protocol
= tc.getProtocol(expr->getLoc(),
KnownProtocolKind::ExpressibleByBooleanLiteral);
ProtocolDecl *builtinProtocol
= tc.getProtocol(expr->getLoc(),
KnownProtocolKind::ExpressibleByBuiltinBooleanLiteral);
if (!protocol || !builtinProtocol)
return nullptr;
auto type = simplifyType(cs.getType(expr));
DeclName initName(tc.Context, DeclBaseName::createConstructor(),
{ tc.Context.Id_booleanLiteral });
DeclName builtinInitName(tc.Context, DeclBaseName::createConstructor(),
{ tc.Context.Id_builtinBooleanLiteral });
return convertLiteralInPlace(
expr,
type,
protocol,
tc.Context.Id_BooleanLiteralType,
initName,
builtinProtocol,
builtinInitName,
diag::boolean_literal_broken_proto,
diag::builtin_boolean_literal_broken_proto);
}
Expr *handleStringLiteralExpr(LiteralExpr *expr) {
auto stringLiteral = dyn_cast<StringLiteralExpr>(expr);
auto magicLiteral = dyn_cast<MagicIdentifierLiteralExpr>(expr);
assert(bool(stringLiteral) != bool(magicLiteral) &&
"literal must be either a string literal or a magic literal");
auto type = simplifyType(cs.getType(expr));
auto &tc = cs.getTypeChecker();
bool isStringLiteral = true;
bool isGraphemeClusterLiteral = false;
ProtocolDecl *protocol = tc.getProtocol(
expr->getLoc(), KnownProtocolKind::ExpressibleByStringLiteral);
if (!TypeChecker::conformsToProtocol(type, protocol, cs.DC,
ConformanceCheckFlags::InExpression)) {
// If the type does not conform to ExpressibleByStringLiteral, it should
// be ExpressibleByExtendedGraphemeClusterLiteral.
protocol = tc.getProtocol(
expr->getLoc(),
KnownProtocolKind::ExpressibleByExtendedGraphemeClusterLiteral);
isStringLiteral = false;
isGraphemeClusterLiteral = true;
}
if (!TypeChecker::conformsToProtocol(type, protocol, cs.DC,
ConformanceCheckFlags::InExpression)) {
// ... or it should be ExpressibleByUnicodeScalarLiteral.
protocol = tc.getProtocol(
expr->getLoc(),
KnownProtocolKind::ExpressibleByUnicodeScalarLiteral);
isStringLiteral = false;
isGraphemeClusterLiteral = false;
}
// For type-sugar reasons, prefer the spelling of the default literal
// type.
if (auto defaultType = tc.getDefaultType(protocol, dc)) {
if (defaultType->isEqual(type))
type = defaultType;
}
ProtocolDecl *builtinProtocol;
Identifier literalType;
DeclName literalFuncName;
DeclName builtinLiteralFuncName;
Diag<> brokenProtocolDiag;
Diag<> brokenBuiltinProtocolDiag;
if (isStringLiteral) {
literalType = tc.Context.Id_StringLiteralType;
literalFuncName = DeclName(tc.Context, DeclBaseName::createConstructor(),
{ tc.Context.Id_stringLiteral });
builtinProtocol = tc.getProtocol(
expr->getLoc(),
KnownProtocolKind::ExpressibleByBuiltinStringLiteral);
builtinLiteralFuncName
= DeclName(tc.Context, DeclBaseName::createConstructor(),
{ tc.Context.Id_builtinStringLiteral,
tc.Context.getIdentifier("utf8CodeUnitCount"),
tc.Context.getIdentifier("isASCII") });
if (stringLiteral)
stringLiteral->setEncoding(StringLiteralExpr::UTF8);
else
magicLiteral->setStringEncoding(StringLiteralExpr::UTF8);
brokenProtocolDiag = diag::string_literal_broken_proto;
brokenBuiltinProtocolDiag = diag::builtin_string_literal_broken_proto;
} else if (isGraphemeClusterLiteral) {
literalType = tc.Context.Id_ExtendedGraphemeClusterLiteralType;
literalFuncName
= DeclName(tc.Context, DeclBaseName::createConstructor(),
{tc.Context.Id_extendedGraphemeClusterLiteral});
builtinLiteralFuncName
= DeclName(tc.Context, DeclBaseName::createConstructor(),
{ tc.Context.Id_builtinExtendedGraphemeClusterLiteral,
tc.Context.getIdentifier("utf8CodeUnitCount"),
tc.Context.getIdentifier("isASCII") });
builtinProtocol = tc.getProtocol(
expr->getLoc(),
KnownProtocolKind::ExpressibleByBuiltinExtendedGraphemeClusterLiteral);
brokenProtocolDiag =
diag::extended_grapheme_cluster_literal_broken_proto;
brokenBuiltinProtocolDiag =
diag::builtin_extended_grapheme_cluster_literal_broken_proto;
} else {
// Otherwise, we should have just one Unicode scalar.
literalType = tc.Context.Id_UnicodeScalarLiteralType;
literalFuncName
= DeclName(tc.Context, DeclBaseName::createConstructor(),
{tc.Context.Id_unicodeScalarLiteral});
builtinLiteralFuncName
= DeclName(tc.Context, DeclBaseName::createConstructor(),
{tc.Context.Id_builtinUnicodeScalarLiteral});
builtinProtocol = tc.getProtocol(
expr->getLoc(),
KnownProtocolKind::ExpressibleByBuiltinUnicodeScalarLiteral);
brokenProtocolDiag = diag::unicode_scalar_literal_broken_proto;
brokenBuiltinProtocolDiag =
diag::builtin_unicode_scalar_literal_broken_proto;
stringLiteral->setEncoding(StringLiteralExpr::OneUnicodeScalar);
}
return convertLiteralInPlace(expr,
type,
protocol,
literalType,
literalFuncName,
builtinProtocol,
builtinLiteralFuncName,
brokenProtocolDiag,
brokenBuiltinProtocolDiag);
}
Expr *visitStringLiteralExpr(StringLiteralExpr *expr) {
return handleStringLiteralExpr(expr);
}
Expr *
visitInterpolatedStringLiteralExpr(InterpolatedStringLiteralExpr *expr) {
// Figure out the string type we're converting to.
auto openedType = cs.getType(expr);
auto type = simplifyType(openedType);
cs.setType(expr, type);
auto &tc = cs.getTypeChecker();
auto loc = expr->getStartLoc();
auto fetchProtocolInitWitness =
[&](KnownProtocolKind protocolKind, Type type,
ArrayRef<Identifier> argLabels) -> ConcreteDeclRef {
auto proto = tc.getProtocol(loc, protocolKind);
assert(proto && "Missing string interpolation protocol?");
auto conformance =
TypeChecker::conformsToProtocol(type, proto, cs.DC,
ConformanceCheckFlags::InExpression);
assert(conformance && "string interpolation type conforms to protocol");
DeclName constrName(tc.Context, DeclBaseName::createConstructor(), argLabels);
ConcreteDeclRef witness =
conformance->getWitnessByName(type->getRValueType(), constrName);
if (!witness || !isa<AbstractFunctionDecl>(witness.getDecl()))
return nullptr;
return witness;
};
auto associatedTypeArray =
tc.getProtocol(expr->getLoc(),
KnownProtocolKind::ExpressibleByStringInterpolation)
->lookupDirect(tc.Context.Id_StringInterpolation);
if (associatedTypeArray.empty()) {
tc.diagnose(expr->getStartLoc(), diag::interpolation_broken_proto);
return nullptr;
}
auto associatedTypeDecl =
cast<AssociatedTypeDecl>(associatedTypeArray.front());
auto interpolationType =
simplifyType(DependentMemberType::get(openedType, associatedTypeDecl));
// Fetch needed witnesses.
ConcreteDeclRef builderInit = fetchProtocolInitWitness(
KnownProtocolKind::StringInterpolationProtocol, interpolationType,
{ tc.Context.Id_literalCapacity, tc.Context.Id_interpolationCount });
if (!builderInit) return nullptr;
expr->setBuilderInit(builderInit);
ConcreteDeclRef resultInit = fetchProtocolInitWitness(
KnownProtocolKind::ExpressibleByStringInterpolation, type,
{ tc.Context.Id_stringInterpolation });
if (!resultInit) return nullptr;
expr->setResultInit(resultInit);
// Make the integer literals for the parameters.
auto buildExprFromUnsigned = [&](unsigned value) {
LiteralExpr *expr =
IntegerLiteralExpr::createFromUnsigned(tc.Context, value);
cs.setType(expr, tc.getIntType(cs.DC));
return handleIntegerLiteralExpr(expr);
};
expr->setLiteralCapacityExpr(
buildExprFromUnsigned(expr->getLiteralCapacity()));
expr->setInterpolationCountExpr(
buildExprFromUnsigned(expr->getInterpolationCount()));
// This OpaqueValueExpr represents the result of builderInit above in
// silgen.
OpaqueValueExpr *interpolationExpr = new (tc.Context)
OpaqueValueExpr(expr->getSourceRange(), interpolationType);
cs.setType(interpolationExpr, interpolationType);
expr->setInterpolationExpr(interpolationExpr);
auto appendingExpr = expr->getAppendingExpr();
appendingExpr->setSubExpr(interpolationExpr);
return expr;
}
Expr *visitMagicIdentifierLiteralExpr(MagicIdentifierLiteralExpr *expr) {
switch (expr->getKind()) {
case MagicIdentifierLiteralExpr::File:
case MagicIdentifierLiteralExpr::Function:
return handleStringLiteralExpr(expr);
case MagicIdentifierLiteralExpr::Line:
case MagicIdentifierLiteralExpr::Column:
return handleIntegerLiteralExpr(expr);
case MagicIdentifierLiteralExpr::DSOHandle:
return expr;
}
llvm_unreachable("Unhandled MagicIdentifierLiteralExpr in switch.");
}
Expr *visitObjectLiteralExpr(ObjectLiteralExpr *expr) {
if (cs.getType(expr) && !cs.getType(expr)->hasTypeVariable())
return expr;
auto &tc = cs.getTypeChecker();
// Figure out the type we're converting to.
auto openedType = cs.getType(expr);
auto type = simplifyType(openedType);
cs.setType(expr, type);
if (type->is<UnresolvedType>()) return expr;
Type conformingType = type;
if (auto baseType = conformingType->getOptionalObjectType()) {
// The type may be optional due to a failable initializer in the
// protocol.
conformingType = baseType;
}
// Find the appropriate object literal protocol.
auto proto = tc.getLiteralProtocol(expr);
assert(proto && "Missing object literal protocol?");
auto conformance =
TypeChecker::conformsToProtocol(conformingType, proto, cs.DC,
ConformanceCheckFlags::InExpression);
assert(conformance && "object literal type conforms to protocol");
DeclName constrName(tc.getObjectLiteralConstructorName(expr));
ConcreteDeclRef witness =
conformance->getWitnessByName(conformingType->getRValueType(),
constrName);
if (!witness || !isa<AbstractFunctionDecl>(witness.getDecl()))
return nullptr;
expr->setInitializer(witness);
return expr;
}
// Add a forced unwrap of an expression which either has type Optional<T>
// or is a function that returns an Optional<T>. The latter turns into a
// conversion expression that we will hoist above the ApplyExpr
// that needs to be forced during the process of rewriting the expression.
//
// forForcedOptional is used to indicate that we will further need
// to hoist this result above an explicit force of an optional that is
// in place for something like an @optional protocol member from
// Objective C that we might otherwise mistake for the thing we mean to
// force here.
Expr *forceUnwrapResult(Expr *expr, bool forForcedOptional =false) {
auto ty = simplifyType(cs.getType(expr));
if (forForcedOptional)
ty = ty->getOptionalObjectType();
if (auto *fnTy = ty->getAs<AnyFunctionType>()) {
auto underlyingType = cs.replaceFinalResultTypeWithUnderlying(fnTy);
auto &ctx = cs.getTypeChecker().Context;
return cs.cacheType(new (ctx) ImplicitlyUnwrappedFunctionConversionExpr(
expr, underlyingType));
} else {
return coerceImplicitlyUnwrappedOptionalToValue(
expr, ty->getWithoutSpecifierType()->getOptionalObjectType());
}
}
bool shouldForceUnwrapResult(OverloadChoice choice,
ConstraintLocatorBuilder locator) {
if (!choice.isImplicitlyUnwrappedValueOrReturnValue())
return false;
auto *choiceLocator = cs.getConstraintLocator(locator.withPathElement(
ConstraintLocator::ImplicitlyUnwrappedDisjunctionChoice));
return solution.getDisjunctionChoice(choiceLocator);
}
Expr *forceUnwrapIfExpected(Expr *expr, OverloadChoice choice,
ConstraintLocatorBuilder locator,
bool forForcedOptional = false) {
if (!shouldForceUnwrapResult(choice, locator))
return expr;
// Force the expression if required for the solution.
return forceUnwrapResult(expr, forForcedOptional);
}
Expr *visitDeclRefExpr(DeclRefExpr *expr) {
auto locator = cs.getConstraintLocator(expr);
// Find the overload choice used for this declaration reference.
auto selected = solution.getOverloadChoiceIfAvailable(locator);
if (!selected.hasValue()) {
auto *varDecl = cast<VarDecl>(expr->getDecl());
assert(varDecl->getType()->is<UnresolvedType>() &&
"should only happen for closure arguments in CSDiags");
cs.setType(expr, varDecl->getType());
return expr;
}
return buildDeclRef(selected->choice, expr->getNameLoc(),
selected->openedFullType, locator, expr->isImplicit(),
expr->getFunctionRefKind(),
expr->getAccessSemantics());
}
Expr *visitSuperRefExpr(SuperRefExpr *expr) {
simplifyExprType(expr);
return expr;
}
Expr *visitTypeExpr(TypeExpr *expr) {
auto toType = simplifyType(cs.getType(expr->getTypeLoc()));
expr->getTypeLoc().setType(toType);
cs.setType(expr, MetatypeType::get(toType));
return expr;
}
Expr *visitOtherConstructorDeclRefExpr(OtherConstructorDeclRefExpr *expr) {
cs.setType(expr, expr->getDecl()->getInitializerInterfaceType());
return expr;
}
Expr *visitDotSyntaxBaseIgnoredExpr(DotSyntaxBaseIgnoredExpr *expr) {
return simplifyExprType(expr);
}
Expr *visitOverloadedDeclRefExpr(OverloadedDeclRefExpr *expr) {
// Determine the declaration selected for this overloaded reference.
auto locator = cs.getConstraintLocator(expr);
auto selected = solution.getOverloadChoice(locator);
auto choice = selected.choice;
return buildDeclRef(choice, expr->getNameLoc(), selected.openedFullType,
locator, expr->isImplicit(),
choice.getFunctionRefKind(),
AccessSemantics::Ordinary);
}
Expr *visitUnresolvedDeclRefExpr(UnresolvedDeclRefExpr *expr) {
// FIXME: We should have generated an overload set from this, in which
// case we can emit a typo-correction error here but recover well.
return nullptr;
}
Expr *visitUnresolvedSpecializeExpr(UnresolvedSpecializeExpr *expr) {
// Our specializations should have resolved the subexpr to the right type.
return expr->getSubExpr();
}
Expr *visitMemberRefExpr(MemberRefExpr *expr) {
auto memberLocator = cs.getConstraintLocator(expr,
ConstraintLocator::Member);
auto selected = solution.getOverloadChoice(memberLocator);
bool isDynamic
= selected.choice.getKind() == OverloadChoiceKind::DeclViaDynamic;
return buildMemberRef(
expr->getBase(), selected.openedFullType, expr->getDotLoc(),
selected.choice, expr->getNameLoc(), selected.openedType,
cs.getConstraintLocator(expr), memberLocator, expr->isImplicit(),
selected.choice.getFunctionRefKind(), expr->getAccessSemantics(),
isDynamic);
}
Expr *visitDynamicMemberRefExpr(DynamicMemberRefExpr *expr) {
llvm_unreachable("already type-checked?");
}
Expr *visitUnresolvedMemberExpr(UnresolvedMemberExpr *expr) {
// Dig out the type of the base, which will be the result type of this
// expression. If constraint solving resolved this to an UnresolvedType,
// then we're in an ambiguity tolerant mode used for diagnostic
// generation. Just leave this as an unresolved member reference.
Type resultTy = simplifyType(cs.getType(expr));
if (resultTy->getRValueType()->is<UnresolvedType>()) {
cs.setType(expr, resultTy);
return expr;
}
Type baseTy = resultTy->getRValueType();
auto &tc = cs.getTypeChecker();
// Find the selected member.
auto memberLocator = cs.getConstraintLocator(
expr, ConstraintLocator::UnresolvedMember);
auto selected = solution.getOverloadChoice(memberLocator);
// If the member came by optional unwrapping, then unwrap the base type.
if (selected.choice.getKind()
== OverloadChoiceKind::DeclViaUnwrappedOptional) {
baseTy = baseTy->getOptionalObjectType();
assert(baseTy
&& "got unwrapped optional decl from non-optional base?!");
}
// The base expression is simply the metatype of the base type.
// FIXME: This location info is bogus.
auto base = TypeExpr::createImplicitHack(expr->getDotLoc(), baseTy,
tc.Context);
cs.cacheExprTypes(base);
// Build the member reference.
bool isDynamic
= selected.choice.getKind() == OverloadChoiceKind::DeclViaDynamic;
auto result = buildMemberRef(
base, selected.openedFullType, expr->getDotLoc(), selected.choice,
expr->getNameLoc(), selected.openedType,
cs.getConstraintLocator(expr), memberLocator, expr->isImplicit(),
selected.choice.getFunctionRefKind(), AccessSemantics::Ordinary,
isDynamic);
if (!result)
return nullptr;
auto getType = [&](const Expr *E) -> Type {
return cs.getType(E);
};
// If there was an argument, apply it.
if (auto arg = expr->getArgument()) {
ApplyExpr *apply = CallExpr::create(
tc.Context, result, arg, expr->getArgumentLabels(),
expr->getArgumentLabelLocs(), expr->hasTrailingClosure(),
/*implicit=*/expr->isImplicit(), Type(), getType);
result = finishApply(apply, Type(), cs.getConstraintLocator(expr));
// FIXME: Application could fail, because some of the solutions
// are not expressible in AST (yet?), like certain tuple-to-tuple
// conversions. Better solution here would be not to form solutions
// which couldn't be applied by e.g. detecting situations like that
// and inserting fixes early.
if (!result)
return nullptr;
}
// Check for ambiguous member if the base is an Optional
if (baseTy->getOptionalObjectType()) {
diagnoseAmbiguousNominalMember(baseTy, result);
}
return coerceToType(result, resultTy, cs.getConstraintLocator(expr));
}
/// Diagnose if the base type is optional, we're referring to a nominal
/// type member via the dot syntax and the member name matches
/// Optional<T>.{member name}
void diagnoseAmbiguousNominalMember(Type baseTy, Expr *result) {
if (auto baseTyUnwrapped = baseTy->lookThroughAllOptionalTypes()) {
// Return if the base type doesn't have a nominal type decl
if (!baseTyUnwrapped->getNominalOrBoundGenericNominal()) {
return;
}
// Otherwise, continue digging
if (auto DSCE = dyn_cast<DotSyntaxCallExpr>(result)) {
auto calledValue = DSCE->getCalledValue();
auto isOptional = false;
Identifier memberName;
// Try cast the assigned value to an enum case
//
// This will always succeed if the base is Optional<T> & the
// assigned case comes from Optional<T>
if (auto EED = dyn_cast<EnumElementDecl>(calledValue)) {
isOptional = EED->getParentEnum()->isOptionalDecl();
memberName = EED->getBaseName().getIdentifier();
}
// Return if the enum case doesn't come from Optional<T>
if (!isOptional) {
return;
}
// Look up the enum case in the unwrapped type to check if it exists
// as a member
auto baseTyNominalDecl = baseTyUnwrapped
->getNominalOrBoundGenericNominal();
auto &tc = cs.getTypeChecker();
auto results = tc.lookupMember(baseTyNominalDecl->getModuleContext(),
baseTyUnwrapped,
memberName,
defaultMemberLookupOptions);
// Lookup didn't find anything, so return
if (results.empty()) {
return;
}
if (auto member = results.front().getValueDecl()) {
// Lookup returned a member that is an instance member,
// so return
if (member->isInstanceMember()) {
return;
}
// Return if the member is an enum case w/ assoc values, as we only
// care (for now) about cases with no assoc values (like none)
if (auto EED = dyn_cast<EnumElementDecl>(member)) {
if (EED->hasAssociatedValues()) {
return;
}
}
// Emit a diagnostic with some fixits
auto baseTyName = baseTy->getCanonicalType().getString();
auto baseTyUnwrappedName = baseTyUnwrapped->getString();
auto loc = DSCE->getLoc();
auto startLoc = DSCE->getStartLoc();
tc.diagnose(loc, swift::diag::optional_ambiguous_case_ref,
baseTyName, baseTyUnwrappedName, memberName.str());
tc.diagnose(loc, swift::diag::optional_fixit_ambiguous_case_ref)
.fixItInsert(startLoc, "Optional");
tc.diagnose(loc, swift::diag::type_fixit_optional_ambiguous_case_ref,
baseTyUnwrappedName, memberName.str())
.fixItInsert(startLoc, baseTyUnwrappedName);
}
}
}
}
private:
/// A list of "suspicious" optional injections that come from
/// forced downcasts.
SmallVector<InjectIntoOptionalExpr *, 4> SuspiciousOptionalInjections;
public:
/// A list of optional injections that have been diagnosed.
llvm::SmallPtrSet<InjectIntoOptionalExpr *, 4> DiagnosedOptionalInjections;
private:
/// Create a member reference to the given constructor.
Expr *applyCtorRefExpr(Expr *expr, Expr *base, SourceLoc dotLoc,
DeclNameLoc nameLoc, bool implicit,
ConstraintLocator *ctorLocator,
OverloadChoice choice,
FunctionRefKind functionRefKind, Type openedType) {
auto *ctor = cast<ConstructorDecl>(choice.getDecl());
// If the subexpression is a metatype, build a direct reference to the
// constructor.
if (cs.getType(base)->is<AnyMetatypeType>()) {
return buildMemberRef(
base, openedType, dotLoc, choice, nameLoc, cs.getType(expr),
ConstraintLocatorBuilder(cs.getConstraintLocator(expr)),
ctorLocator, implicit, functionRefKind, AccessSemantics::Ordinary,
/*isDynamic=*/false);
}
// The subexpression must be either 'self' or 'super'.
if (!base->isSuperExpr()) {
// 'super' references have already been fully checked; handle the
// 'self' case below.
auto &tc = cs.getTypeChecker();
bool diagnoseBadInitRef = true;
auto arg = base->getSemanticsProvidingExpr();
if (auto dre = dyn_cast<DeclRefExpr>(arg)) {
if (dre->getDecl()->getFullName() == cs.getASTContext().Id_self) {
// We have a reference to 'self'.
diagnoseBadInitRef = false;
// Make sure the reference to 'self' occurs within an initializer.
if (!dyn_cast_or_null<ConstructorDecl>(
cs.DC->getInnermostMethodContext())) {
if (!SuppressDiagnostics)
tc.diagnose(dotLoc, diag::init_delegation_outside_initializer);
return nullptr;
}
}
}
// If we need to diagnose this as a bad reference to an initializer,
// do so now.
if (diagnoseBadInitRef) {
// Determine whether 'super' would have made sense as a base.
bool hasSuper = false;
if (auto func = cs.DC->getInnermostMethodContext()) {
if (auto classDecl = func->getDeclContext()->getSelfClassDecl()) {
hasSuper = classDecl->hasSuperclass();
}
}
if (SuppressDiagnostics)
return nullptr;
tc.diagnose(dotLoc, diag::bad_init_ref_base, hasSuper);
}
}
// Build a partial application of the delegated initializer.
Expr *ctorRef = buildOtherConstructorRef(openedType, ctor, base, nameLoc,
ctorLocator, implicit);
auto *call = new (cs.getASTContext()) DotSyntaxCallExpr(ctorRef, dotLoc,
base);
return finishApply(call, cs.getType(expr),
ConstraintLocatorBuilder(
cs.getConstraintLocator(expr)));
}
Expr *applyMemberRefExpr(Expr *expr, Expr *base, SourceLoc dotLoc,
DeclNameLoc nameLoc, bool implicit) {
// If we have a constructor member, handle it as a constructor.
auto ctorLocator = cs.getConstraintLocator(
expr,
ConstraintLocator::ConstructorMember);
if (auto selected = solution.getOverloadChoiceIfAvailable(ctorLocator)) {
auto choice = selected->choice;
return applyCtorRefExpr(
expr, base, dotLoc, nameLoc, implicit, ctorLocator, choice,
choice.getFunctionRefKind(), selected->openedFullType);
}
// Determine the declaration selected for this overloaded reference.
auto memberLocator = cs.getConstraintLocator(expr,
ConstraintLocator::Member);
auto selectedElt = solution.getOverloadChoiceIfAvailable(memberLocator);
if (!selectedElt) {
// If constraint solving resolved this to an UnresolvedType, then we're
// in an ambiguity tolerant mode used for diagnostic generation. Just
// leave this as whatever type of member reference it already is.
Type resultTy = simplifyType(cs.getType(expr));
cs.setType(expr, resultTy);
return expr;
}
auto selected = *selectedElt;
if (!selected.choice.getBaseType()) {
// This is one of the "outer alternatives", meaning the innermost
// methods didn't work out.
//
// The only way to get here is via an UnresolvedDotExpr with outer
// alternatives.
auto UDE = cast<UnresolvedDotExpr>(expr);
cs.diagnoseDeprecatedConditionalConformanceOuterAccess(
UDE, selected.choice.getDecl());
return buildDeclRef(selected.choice, nameLoc, selected.openedFullType,
memberLocator, implicit,
selected.choice.getFunctionRefKind(),
AccessSemantics::Ordinary);
}
switch (selected.choice.getKind()) {
case OverloadChoiceKind::DeclViaBridge: {
base = cs.coerceToRValue(base);
// Look through an implicitly unwrapped optional.
auto baseTy = cs.getType(base);
auto &tc = cs.getTypeChecker();
auto &ctx = tc.Context;
auto baseMetaTy = baseTy->getAs<MetatypeType>();
auto baseInstTy = (baseMetaTy ? baseMetaTy->getInstanceType() : baseTy);
auto classTy = ctx.getBridgedToObjC(cs.DC, baseInstTy);
if (baseMetaTy) {
// FIXME: We're dropping side effects in the base here!
base = TypeExpr::createImplicitHack(base->getLoc(), classTy,
tc.Context);
cs.cacheExprTypes(base);
} else {
// Bridge the base to its corresponding Objective-C object.
base = bridgeToObjectiveC(base, classTy);
}
// Fall through to build the member reference.
LLVM_FALLTHROUGH;
}
case OverloadChoiceKind::Decl:
case OverloadChoiceKind::DeclViaUnwrappedOptional:
case OverloadChoiceKind::DeclViaDynamic: {
bool isDynamic
= selected.choice.getKind() == OverloadChoiceKind::DeclViaDynamic;
return buildMemberRef(base, selected.openedFullType, dotLoc,
selected.choice, nameLoc, selected.openedType,
cs.getConstraintLocator(expr), memberLocator,
implicit, selected.choice.getFunctionRefKind(),
AccessSemantics::Ordinary, isDynamic);
}
case OverloadChoiceKind::TupleIndex: {
Type toType = simplifyType(cs.getType(expr));
auto baseTy = cs.getType(base);
// If the base type is not a tuple l-value, access to
// its elements supposed to be r-value as well.
//
// This is modeled in constraint system in a way
// that when member type is resolved by `resolveOverload`
// it would take r-value type of the element at
// specified index, but if it's a type variable it
// could still be bound to l-value later.
if (!baseTy->is<LValueType>())
toType = toType->getRValueType();
// If the result type is an rvalue and the base contains lvalues,
// need a full tuple coercion to properly load & set access kind
// on all underlying elements before taking a single element.
if (!toType->hasLValueType() && baseTy->hasLValueType())
base = coerceToType(base, baseTy->getRValueType(),
cs.getConstraintLocator(base));
return cs.cacheType(new (cs.getASTContext())
TupleElementExpr(base, dotLoc,
selected.choice.getTupleIndex(),
nameLoc.getBaseNameLoc(), toType));
}
case OverloadChoiceKind::BaseType:
return base;
case OverloadChoiceKind::KeyPathApplication:
llvm_unreachable("should only happen in a subscript");
case OverloadChoiceKind::DynamicMemberLookup:
case OverloadChoiceKind::KeyPathDynamicMemberLookup: {
return buildDynamicMemberLookupRef(
expr, base, dotLoc, nameLoc.getStartLoc(), selected, memberLocator);
}
}
llvm_unreachable("Unhandled OverloadChoiceKind in switch.");
}
Expr *buildDynamicMemberLookupRef(Expr *expr, Expr *base, SourceLoc dotLoc,
SourceLoc nameLoc,
const SelectedOverload &overload,
ConstraintLocator *memberLocator) {
// Application of a DynamicMemberLookup result turns
// a member access of `x.foo` into x[dynamicMember: "foo"], or
// x[dynamicMember: KeyPath<T, U>]
auto &ctx = cs.getASTContext();
// Figure out the expected type of the lookup parameter. We know the
// openedFullType will be "xType -> indexType -> resultType". Dig out
// its index type.
auto paramTy = getTypeOfDynamicMemberIndex(overload);
Expr *argExpr = nullptr;
if (overload.choice.getKind() ==
OverloadChoiceKind::DynamicMemberLookup) {
// Build and type check the string literal index value to the specific
// string type expected by the subscript.
auto fieldName = overload.choice.getName().getBaseIdentifier().str();
argExpr = buildDynamicMemberLookupIndexExpr(fieldName, nameLoc, dc, cs);
} else {
argExpr = buildKeyPathDynamicMemberIndexExpr(
paramTy->castTo<BoundGenericType>(), dotLoc, memberLocator);
}
if (!argExpr)
return nullptr;
// Build a tuple so that the argument has a label.
auto tupleTy =
TupleType::get(TupleTypeElt(paramTy, ctx.Id_dynamicMember), ctx);
Expr *index =
TupleExpr::createImplicit(ctx, argExpr, ctx.Id_dynamicMember);
index->setType(tupleTy);
cs.cacheType(index);
// Build and return a subscript that uses this string as the index.
return buildSubscript(
base, index, ctx.Id_dynamicMember,
/*trailingClosure*/ false, cs.getConstraintLocator(expr),
/*isImplicit*/ true, AccessSemantics::Ordinary, overload);
}
Type getTypeOfDynamicMemberIndex(const SelectedOverload &overload) {
assert(overload.choice.getKind() ==
OverloadChoiceKind::DynamicMemberLookup ||
overload.choice.getKind() ==
OverloadChoiceKind::KeyPathDynamicMemberLookup);
auto declTy = solution.simplifyType(overload.openedFullType);
auto subscriptTy = declTy->castTo<FunctionType>()->getResult();
auto refFnType = subscriptTy->castTo<FunctionType>();
assert(refFnType->getParams().size() == 1 &&
"subscript always has one arg");
return refFnType->getParams()[0].getPlainType();
}
public:
Expr *visitUnresolvedDotExpr(UnresolvedDotExpr *expr) {
return applyMemberRefExpr(expr, expr->getBase(), expr->getDotLoc(),
expr->getNameLoc(), expr->isImplicit());
}
Expr *visitSequenceExpr(SequenceExpr *expr) {
llvm_unreachable("Expression wasn't parsed?");
}
Expr *visitArrowExpr(ArrowExpr *expr) {
llvm_unreachable("Arrow expr wasn't converted to type?");
}
Expr *visitIdentityExpr(IdentityExpr *expr) {
cs.setType(expr, cs.getType(expr->getSubExpr()));
return expr;
}
Expr *visitAnyTryExpr(AnyTryExpr *expr) {
cs.setType(expr, cs.getType(expr->getSubExpr()));
return expr;
}
Expr *visitOptionalTryExpr(OptionalTryExpr *expr) {
// Prior to Swift 5, 'try?' simply wraps the type of its sub-expression
// in an Optional, regardless of the sub-expression type.
//
// In Swift 5+, the type of a 'try?' expression of static type T is:
// - Equal to T if T is optional
// - Equal to T? if T is not optional
//
// The result is that in Swift 5, 'try?' avoids producing nested optionals.
if (!cs.getTypeChecker().getLangOpts().isSwiftVersionAtLeast(5)) {
// Nothing to do for Swift 4 and earlier!
return simplifyExprType(expr);
}
Type exprType = simplifyType(cs.getType(expr));
auto subExpr = coerceToType(expr->getSubExpr(), exprType,
cs.getConstraintLocator(expr));
if (!subExpr) return nullptr;
expr->setSubExpr(subExpr);
cs.setType(expr, exprType);
return expr;
}
Expr *visitParenExpr(ParenExpr *expr) {
return simplifyExprType(expr);
}
Expr *visitTupleExpr(TupleExpr *expr) {
return simplifyExprType(expr);
}
Expr *visitSubscriptExpr(SubscriptExpr *expr) {
auto *memberLocator =
cs.getConstraintLocator(expr, ConstraintLocator::SubscriptMember);
auto overload = solution.getOverloadChoiceIfAvailable(memberLocator);
if (overload && overload->choice.getKind() ==
OverloadChoiceKind::KeyPathDynamicMemberLookup) {
return buildDynamicMemberLookupRef(
expr, expr->getBase(), expr->getIndex()->getStartLoc(), SourceLoc(),
*overload, memberLocator);
}
return buildSubscript(
expr->getBase(), expr->getIndex(), expr->getArgumentLabels(),
expr->hasTrailingClosure(), cs.getConstraintLocator(expr),
expr->isImplicit(), expr->getAccessSemantics(), overload);
}
/// "Finish" an array expression by filling in the semantic expression.
ArrayExpr *finishArrayExpr(ArrayExpr *expr) {
Type arrayTy = cs.getType(expr);
auto &tc = cs.getTypeChecker();
ProtocolDecl *arrayProto
= tc.getProtocol(expr->getLoc(),
KnownProtocolKind::ExpressibleByArrayLiteral);
assert(arrayProto && "type-checked array literal w/o protocol?!");
auto conformance =
TypeChecker::conformsToProtocol(arrayTy, arrayProto, cs.DC,
ConformanceCheckFlags::InExpression);
assert(conformance && "Type does not conform to protocol?");
DeclName name(tc.Context, DeclBaseName::createConstructor(),
{ tc.Context.Id_arrayLiteral });
ConcreteDeclRef witness =
conformance->getWitnessByName(arrayTy->getRValueType(), name);
if (!witness || !isa<AbstractFunctionDecl>(witness.getDecl()))
return nullptr;
expr->setInitializer(witness);
auto elementType = expr->getElementType();
for (auto &element : expr->getElements()) {
element = coerceToType(element, elementType,
cs.getConstraintLocator(element));
}
return expr;
}
Expr *visitArrayExpr(ArrayExpr *expr) {
Type openedType = cs.getType(expr);
Type arrayTy = simplifyType(openedType);
cs.setType(expr, arrayTy);
if (!finishArrayExpr(expr)) return nullptr;
// If the array element type was defaulted, note that in the expression.
if (solution.DefaultedConstraints.count(cs.getConstraintLocator(expr)))
expr->setIsTypeDefaulted();
return expr;
}
/// "Finish" a dictionary expression by filling in the semantic expression.
DictionaryExpr *finishDictionaryExpr(DictionaryExpr *expr) {
Type dictionaryTy = cs.getType(expr);
auto &tc = cs.getTypeChecker();
ProtocolDecl *dictionaryProto
= tc.getProtocol(expr->getLoc(),
KnownProtocolKind::ExpressibleByDictionaryLiteral);
auto conformance =
TypeChecker::conformsToProtocol(dictionaryTy, dictionaryProto, cs.DC,
ConformanceCheckFlags::InExpression);
if (!conformance)
return nullptr;
DeclName name(tc.Context, DeclBaseName::createConstructor(),
{ tc.Context.Id_dictionaryLiteral });
ConcreteDeclRef witness =
conformance->getWitnessByName(dictionaryTy->getRValueType(), name);
if (!witness || !isa<AbstractFunctionDecl>(witness.getDecl()))
return nullptr;
expr->setInitializer(witness);
auto elementType = expr->getElementType();
for (auto &element : expr->getElements()) {
element = coerceToType(element, elementType,
cs.getConstraintLocator(element));
}
return expr;
}
Expr *visitDictionaryExpr(DictionaryExpr *expr) {
Type openedType = cs.getType(expr);
Type dictionaryTy = simplifyType(openedType);
cs.setType(expr, dictionaryTy);
if (!finishDictionaryExpr(expr)) return nullptr;
// If the dictionary key or value type was defaulted, note that in the
// expression.
if (solution.DefaultedConstraints.count(cs.getConstraintLocator(expr)))
expr->setIsTypeDefaulted();
return expr;
}
Expr *visitDynamicSubscriptExpr(DynamicSubscriptExpr *expr) {
return buildSubscript(expr->getBase(), expr->getIndex(),
expr->getArgumentLabels(),
expr->hasTrailingClosure(),
cs.getConstraintLocator(expr),
expr->isImplicit(), AccessSemantics::Ordinary);
}
Expr *visitTupleElementExpr(TupleElementExpr *expr) {
simplifyExprType(expr);
return expr;
}
Expr *visitCaptureListExpr(CaptureListExpr *expr) {
// The type of the capture list is the type of the closure contained
// inside it.
cs.setType(expr, cs.getType(expr->getClosureBody()));
return expr;
}
Expr *visitClosureExpr(ClosureExpr *expr) {
llvm_unreachable("Handled by the walker directly");
}
Expr *visitAutoClosureExpr(AutoClosureExpr *expr) {
llvm_unreachable("Already type-checked");
}
Expr *visitInOutExpr(InOutExpr *expr) {
auto objectTy = cs.getType(expr->getSubExpr())->getRValueType();
// The type is simply inout of whatever the lvalue's object type was.
cs.setType(expr, InOutType::get(objectTy));
return expr;
}
Expr *visitVarargExpansionExpr(VarargExpansionExpr *expr) {
simplifyExprType(expr);
auto arrayTy = cs.getType(expr);
expr->setSubExpr(coerceToType(expr->getSubExpr(), arrayTy,
cs.getConstraintLocator(expr)));
return expr;
}
Expr *visitDynamicTypeExpr(DynamicTypeExpr *expr) {
Expr *base = expr->getBase();
base = cs.coerceToRValue(base);
expr->setBase(base);
return simplifyExprType(expr);
}
Expr *visitOpaqueValueExpr(OpaqueValueExpr *expr) {
assert(expr->isPlaceholder() && "Already type-checked");
return expr;
}
Expr *visitDefaultArgumentExpr(DefaultArgumentExpr *expr) {
llvm_unreachable("Already type-checked");
}
Expr *visitCallerDefaultArgumentExpr(CallerDefaultArgumentExpr *expr) {
llvm_unreachable("Already type-checked");
}
Expr *visitApplyExpr(ApplyExpr *expr) {
return finishApply(expr, cs.getType(expr),
ConstraintLocatorBuilder(
cs.getConstraintLocator(expr)));
}
Expr *visitRebindSelfInConstructorExpr(RebindSelfInConstructorExpr *expr) {
// A non-failable initializer cannot delegate to a failable
// initializer.
Expr *unwrappedSubExpr = expr->getSubExpr()->getSemanticsProvidingExpr();
Type valueTy = cs.getType(unwrappedSubExpr)->getOptionalObjectType();
auto inCtor = cast<ConstructorDecl>(cs.DC->getInnermostMethodContext());
if (valueTy && !inCtor->isFailable()) {
bool isChaining;
auto *otherCtorRef = expr->getCalledConstructor(isChaining);
ConstructorDecl *ctor = otherCtorRef->getDecl();
assert(ctor);
// If the initializer we're calling is not declared as
// checked, it's an error.
bool isError = !ctor->isImplicitlyUnwrappedOptional();
// If we're suppressing diagnostics, just fail.
if (isError && SuppressDiagnostics)
return nullptr;
auto &tc = cs.getTypeChecker();
auto &ctx = tc.Context;
if (isError) {
if (auto *optTry = dyn_cast<OptionalTryExpr>(unwrappedSubExpr)) {
tc.diagnose(optTry->getTryLoc(),
diag::delegate_chain_nonoptional_to_optional_try,
isChaining);
tc.diagnose(optTry->getTryLoc(), diag::init_delegate_force_try)
.fixItReplace({optTry->getTryLoc(), optTry->getQuestionLoc()},
"try!");
tc.diagnose(inCtor->getLoc(), diag::init_propagate_failure)
.fixItInsertAfter(inCtor->getLoc(), "?");
} else {
// Give the user the option of adding '!' or making the enclosing
// initializer failable.
tc.diagnose(otherCtorRef->getLoc(),
diag::delegate_chain_nonoptional_to_optional,
isChaining, ctor->getFullName());
tc.diagnose(otherCtorRef->getLoc(), diag::init_force_unwrap)
.fixItInsertAfter(expr->getEndLoc(), "!");
tc.diagnose(inCtor->getLoc(), diag::init_propagate_failure)
.fixItInsertAfter(inCtor->getLoc(), "?");
}
}
// Recover by injecting the force operation (the first option).
Expr *newSub = new (ctx) ForceValueExpr(expr->getSubExpr(),
expr->getEndLoc());
cs.setType(newSub, valueTy);
newSub->setImplicit();
expr->setSubExpr(newSub);
}
return expr;
}
Expr *visitIfExpr(IfExpr *expr) {
auto resultTy = simplifyType(cs.getType(expr));
cs.setType(expr, resultTy);
auto cond = cs.coerceToRValue(expr->getCondExpr());
expr->setCondExpr(cond);
// Coerce the then/else branches to the common type.
expr->setThenExpr(coerceToType(expr->getThenExpr(), resultTy,
cs.getConstraintLocator(expr->getThenExpr())));
expr->setElseExpr(coerceToType(expr->getElseExpr(), resultTy,
cs.getConstraintLocator(expr->getElseExpr())));
return expr;
}
Expr *visitImplicitConversionExpr(ImplicitConversionExpr *expr) {
llvm_unreachable("Already type-checked");
}
Expr *visitIsExpr(IsExpr *expr) {
// Turn the subexpression into an rvalue.
auto &tc = cs.getTypeChecker();
auto toType = simplifyType(cs.getType(expr->getCastTypeLoc()));
auto sub = cs.coerceToRValue(expr->getSubExpr());
expr->setSubExpr(sub);
// Set the type we checked against.
expr->getCastTypeLoc().setType(toType);
auto fromType = cs.getType(sub);
auto castContextKind =
SuppressDiagnostics ? CheckedCastContextKind::None
: CheckedCastContextKind::IsExpr;
auto castKind = tc.typeCheckCheckedCast(
fromType, toType, castContextKind, cs.DC,
expr->getLoc(), sub,
expr->getCastTypeLoc().getSourceRange());
switch (castKind) {
case CheckedCastKind::Unresolved:
expr->setCastKind(CheckedCastKind::ValueCast);
break;
case CheckedCastKind::Coercion:
case CheckedCastKind::BridgingCoercion:
// Check is trivially true.
tc.diagnose(expr->getLoc(), diag::isa_is_always_true, "is");
expr->setCastKind(castKind);
break;
case CheckedCastKind::ValueCast:
// Check the cast target is a non-foreign type
if (auto cls = toType->getAs<ClassType>()) {
if (cls->getDecl()->getForeignClassKind() ==
ClassDecl::ForeignKind::CFType) {
tc.diagnose(expr->getLoc(), diag::isa_is_foreign_check, toType);
}
}
expr->setCastKind(castKind);
break;
case CheckedCastKind::ArrayDowncast:
case CheckedCastKind::DictionaryDowncast:
case CheckedCastKind::SetDowncast:
// Valid checks.
expr->setCastKind(castKind);
break;
}
// SIL-generation magically turns this into a Bool; make sure it can.
if (!tc.Context.getBoolBuiltinInitDecl()) {
tc.diagnose(expr->getLoc(), diag::broken_bool);
// Continue anyway.
}
// Dig through the optionals in the from/to types.
SmallVector<Type, 2> fromOptionals;
fromType->lookThroughAllOptionalTypes(fromOptionals);
SmallVector<Type, 2> toOptionals;
toType->lookThroughAllOptionalTypes(toOptionals);
// If we have an imbalance of optionals or a collection
// downcast, handle this as a checked cast followed by a
// a 'hasValue' check.
if (fromOptionals.size() != toOptionals.size() ||
castKind == CheckedCastKind::ArrayDowncast ||
castKind == CheckedCastKind::DictionaryDowncast ||
castKind == CheckedCastKind::SetDowncast) {
auto toOptType = OptionalType::get(toType);
ConditionalCheckedCastExpr *cast
= new (tc.Context) ConditionalCheckedCastExpr(
sub, expr->getLoc(), SourceLoc(),
TypeLoc::withoutLoc(toType));
cs.setType(cast, toOptType);
cs.setType(cast->getCastTypeLoc(), toType);
if (expr->isImplicit())
cast->setImplicit();
// Type-check this conditional case.
Expr *result = handleConditionalCheckedCastExpr(cast, true);
if (!result)
return nullptr;
// Extract a Bool from the resulting expression.
tc.requireOptionalIntrinsics(expr->getLoc());
// Match the optional value against its `Some` case.
auto &ctx = tc.Context;
auto *someDecl = tc.Context.getOptionalSomeDecl();
auto isSomeExpr = new (tc.Context) EnumIsCaseExpr(result, someDecl);
auto boolDecl = ctx.getBoolDecl();
if (!boolDecl) {
tc.diagnose(SourceLoc(), diag::broken_bool);
}
cs.setType(isSomeExpr, boolDecl ? boolDecl->getDeclaredType() : Type());
return isSomeExpr;
}
return expr;
}
/// The kind of cast we're working with for handling optional bindings.
enum class OptionalBindingsCastKind {
/// An explicit bridging conversion, spelled "as".
Bridged,
/// A forced cast, spelled "as!".
Forced,
/// A conditional cast, spelled "as?".
Conditional,
};
/// Handle optional operands and results in an explicit cast.
Expr *handleOptionalBindingsForCast(ExplicitCastExpr *cast,
Type finalResultType,
OptionalBindingsCastKind castKind) {
return handleOptionalBindings(cast->getSubExpr(), finalResultType,
castKind,
[&](Expr *sub, Type resultType) -> Expr* {
// Complain about conditional casts to CF class types; they can't
// actually be conditionally checked.
if (castKind == OptionalBindingsCastKind::Conditional) {
Type destValueType = resultType->getOptionalObjectType();
auto destObjectType = destValueType;
if (auto metaTy = destObjectType->getAs<MetatypeType>())
destObjectType = metaTy->getInstanceType();
if (auto destClass = destObjectType->getClassOrBoundGenericClass()) {
if (destClass->getForeignClassKind() ==
ClassDecl::ForeignKind::CFType) {
if (SuppressDiagnostics)
return nullptr;
auto &tc = cs.getTypeChecker();
tc.diagnose(cast->getLoc(), diag::conditional_downcast_foreign,
destValueType);
ConcreteDeclRef refDecl = sub->getReferencedDecl();
if (refDecl) {
tc.diagnose(cast->getLoc(), diag::note_explicitly_compare_cftypeid,
refDecl.getDecl()->getBaseName(), destValueType);
}
}
}
}
// Set the expression as the sub-expression of the cast, then
// use the cast as the inner operation.
cast->setSubExpr(sub);
cs.setType(cast, resultType);
return cast;
});
}
/// A helper function to build an operation. The inner result type
/// is the expected type of the operation; it will be a non-optional
/// type unless the castKind is Conditional.
using OperationBuilderRef =
llvm::function_ref<Expr*(Expr *subExpr, Type innerResultType)>;
/// Handle optional operands and results in an explicit cast.
Expr *handleOptionalBindings(Expr *subExpr, Type finalResultType,
OptionalBindingsCastKind castKind,
OperationBuilderRef buildInnerOperation) {
auto &tc = cs.getTypeChecker();
unsigned destExtraOptionals;
bool forceExtraSourceOptionals;
switch (castKind) {
case OptionalBindingsCastKind::Bridged:
destExtraOptionals = 0;
forceExtraSourceOptionals = true;
break;
case OptionalBindingsCastKind::Forced:
destExtraOptionals = 0;
forceExtraSourceOptionals = true;
break;
case OptionalBindingsCastKind::Conditional:
destExtraOptionals = 1;
forceExtraSourceOptionals = false;
break;
}
// FIXME: some of this work needs to be delayed until runtime to
// properly account for archetypes dynamically being optional
// types. For example, if we're casting T to NSView?, that
// should succeed if T=NSObject? and its value is actually nil.
Type srcType = cs.getType(subExpr);
SmallVector<Type, 4> srcOptionals;
srcType = srcType->lookThroughAllOptionalTypes(srcOptionals);
SmallVector<Type, 4> destOptionals;
auto destValueType
= finalResultType->lookThroughAllOptionalTypes(destOptionals);
auto isBridgeToAnyObject =
castKind == OptionalBindingsCastKind::Bridged &&
destValueType->isAnyObject();
// If the destination value type is 'AnyObject' when performing a
// bridging operation, or if the destination value type could dynamically
// be an optional type, leave any extra optionals on the source in place.
// Only apply the latter condition in Swift 5 mode to best preserve
// compatibility with Swift 4.1's casting behaviour.
if (isBridgeToAnyObject || (tc.Context.isSwiftVersionAtLeast(5) &&
destValueType->canDynamicallyBeOptionalType(
/*includeExistential*/ false))) {
auto destOptionalsCount = destOptionals.size() - destExtraOptionals;
if (srcOptionals.size() > destOptionalsCount) {
srcType = srcOptionals[destOptionalsCount];
srcOptionals.erase(srcOptionals.begin() + destOptionalsCount,
srcOptionals.end());
}
}
// When performing a bridging operation, if the destination type
// is more optional than the source, we'll add extra optional injections
// at the end.
SmallVector<Type, 4> destOptionalInjections;
if (castKind == OptionalBindingsCastKind::Bridged &&
destOptionals.size() > srcOptionals.size()) {
// Remove the extra optionals from destOptionals, but keep them around
// separately so we can perform the injections on the final result of
// the cast.
auto cutPoint = destOptionals.end() - srcOptionals.size();
destOptionalInjections.append(destOptionals.begin(), cutPoint);
destOptionals.erase(destOptionals.begin(), cutPoint);
finalResultType = destOptionals.empty() ? destValueType
: destOptionals.front();
}
// Local function to add the optional injections to final result.
auto addFinalOptionalInjections = [&](Expr *result) {
for (auto destType : reversed(destOptionalInjections)) {
result =
cs.cacheType(new (tc.Context) InjectIntoOptionalExpr(result,
destType));
}
return result;
};
// There's nothing special to do if the operand isn't optional
// and we don't need any bridging.
if (srcOptionals.empty()) {
Expr *result = buildInnerOperation(subExpr, finalResultType);
if (!result) return nullptr;
return addFinalOptionalInjections(result);
}
// The result type (without the final optional) is a subtype of
// the operand type, so it will never have a higher depth.
assert(destOptionals.size() - destExtraOptionals <= srcOptionals.size());
// The outermost N levels of optionals on the operand must all
// be present or the cast fails. The innermost M levels of
// optionals on the operand are reflected in the requested
// destination type, so we should map these nils into the result.
unsigned numRequiredOptionals =
srcOptionals.size() - (destOptionals.size() - destExtraOptionals);
// The number of OptionalEvaluationExprs between the point of the
// inner cast and the enclosing OptionalEvaluationExpr (exclusive)
// which represents failure for the entire operation.
unsigned failureDepth = destOptionals.size() - destExtraOptionals;
// Drill down on the operand until it's non-optional.
SourceLoc fakeQuestionLoc = subExpr->getEndLoc();
for (unsigned i : indices(srcOptionals)) {
Type valueType =
(i + 1 == srcOptionals.size() ? srcType : srcOptionals[i+1]);
// As we move into the range of mapped optionals, start
// lowering the depth.
unsigned depth = failureDepth;
if (i >= numRequiredOptionals) {
depth -= (i - numRequiredOptionals) + 1;
} else if (forceExtraSourceOptionals) {
// For a forced cast, force the required optionals.
subExpr = new (tc.Context) ForceValueExpr(subExpr, fakeQuestionLoc);
cs.setType(subExpr, valueType);
subExpr->setImplicit(true);
continue;
}
subExpr =
cs.cacheType(new (tc.Context) BindOptionalExpr(subExpr,
fakeQuestionLoc,
depth, valueType));
subExpr->setImplicit(true);
}
// If this is a conditional cast, the result type will always
// have at least one level of optional, which should become the
// type of the checked-cast expression.
Expr *result;
if (castKind == OptionalBindingsCastKind::Conditional) {
assert(!destOptionals.empty() &&
"result of checked cast is not an optional type");
result = buildInnerOperation(subExpr, destOptionals.back());
} else {
result = buildInnerOperation(subExpr, destValueType);
}
if (!result) return nullptr;
// If we're casting to an optional type, we need to capture the
// final M bindings.
if (destOptionals.size() > destExtraOptionals) {
if (castKind == OptionalBindingsCastKind::Conditional) {
// If the innermost cast fails, the entire expression fails. To
// get this behavior, we have to bind and then re-inject the result.
// (SILGen should know how to peephole this.)
result =
cs.cacheType(new (tc.Context) BindOptionalExpr(result,
result->getEndLoc(),
failureDepth,
destValueType));
result->setImplicit(true);
}
for (unsigned i = destOptionals.size(); i != 0; --i) {
Type destType = destOptionals[i-1];
result =
cs.cacheType(new (tc.Context) InjectIntoOptionalExpr(result,
destType));
result =
cs.cacheType(new (tc.Context) OptionalEvaluationExpr(result,
destType));
}
// Otherwise, we just need to capture the failure-depth binding.
} else if (!forceExtraSourceOptionals) {
result =
cs.cacheType(new (tc.Context) OptionalEvaluationExpr(result,
finalResultType));
}
return addFinalOptionalInjections(result);
}
bool hasForcedOptionalResult(ExplicitCastExpr *expr) {
auto *TR = expr->getCastTypeLoc().getTypeRepr();
if (TR && TR->getKind() == TypeReprKind::ImplicitlyUnwrappedOptional) {
auto *locator = cs.getConstraintLocator(
expr, ConstraintLocator::ImplicitlyUnwrappedDisjunctionChoice);
return solution.getDisjunctionChoice(locator);
}
return false;
}
Expr *visitCoerceExpr(CoerceExpr *expr) {
// If we need to insert a force-unwrap for coercions of the form
// 'as T!', do so now.
if (hasForcedOptionalResult(expr)) {
auto *coerced = visitCoerceExpr(expr, None);
if (!coerced)
return nullptr;
return coerceImplicitlyUnwrappedOptionalToValue(
coerced, cs.getType(coerced)->getOptionalObjectType());
}
return visitCoerceExpr(expr, None);
}
Expr *visitCoerceExpr(CoerceExpr *expr, Optional<unsigned> choice) {
// Simplify the type we're casting to.
auto toType = simplifyType(cs.getType(expr->getCastTypeLoc()));
expr->getCastTypeLoc().setType(toType);
auto &tc = cs.getTypeChecker();
// If this is a literal that got converted into constructor call
// lets put proper source information in place.
if (expr->isLiteralInit()) {
auto *literalInit = expr->getSubExpr();
if (auto *call = dyn_cast<CallExpr>(literalInit)) {
call->getFn()->forEachChildExpr([&](Expr *subExpr) -> Expr * {
auto *TE = dyn_cast<TypeExpr>(subExpr);
if (!TE)
return subExpr;
auto type = TE->getInstanceType(
[&](const Expr *expr) { return cs.hasType(expr); },
[&](const Expr *expr) { return cs.getType(expr); });
assert(!type->hasError());
if (!type->isEqual(toType))
return subExpr;
return cs.cacheType(new (tc.Context)
TypeExpr(expr->getCastTypeLoc()));
});
}
if (auto *literal = dyn_cast<NumberLiteralExpr>(literalInit)) {
literal->setExplicitConversion();
} else {
literalInit->setImplicit(false);
}
cs.setType(expr, toType);
// Keep the coercion around, because it contains the source range
// for the original constructor call.
return expr;
}
// Turn the subexpression into an rvalue.
auto rvalueSub = cs.coerceToRValue(expr->getSubExpr());
expr->setSubExpr(rvalueSub);
// If we weren't explicitly told by the caller which disjunction choice,
// get it from the solution to determine whether we've picked a coercion
// or a bridging conversion.
auto *locator = cs.getConstraintLocator(expr);
if (!choice) {
choice = solution.getDisjunctionChoice(locator);
}
// Handle the coercion/bridging of the underlying subexpression, where
// optionality has been removed.
if (*choice == 0) {
// Convert the subexpression.
Expr *sub = expr->getSubExpr();
cs.setExprTypes(sub);
if (tc.convertToType(sub, toType, cs.DC))
return nullptr;
cs.cacheExprTypes(sub);
expr->setSubExpr(sub);
cs.setType(expr, toType);
return expr;
}
// Bridging conversion.
assert(*choice == 1 && "should be bridging");
// Handle optional bindings.
Expr *sub = handleOptionalBindings(expr->getSubExpr(), toType,
OptionalBindingsCastKind::Bridged,
[&](Expr *sub, Type toInstanceType) {
return buildObjCBridgeExpr(sub, toInstanceType, locator);
});
if (!sub) return nullptr;
expr->setSubExpr(sub);
cs.setType(expr, toType);
return expr;
}
// Rewrite ForcedCheckedCastExpr based on what the solver computed.
Expr *visitForcedCheckedCastExpr(ForcedCheckedCastExpr *expr) {
// Simplify the type we're casting to.
auto toType = simplifyType(cs.getType(expr->getCastTypeLoc()));
if (hasForcedOptionalResult(expr))
toType = toType->getOptionalObjectType();
expr->getCastTypeLoc().setType(toType);
// The subexpression is always an rvalue.
auto &tc = cs.getTypeChecker();
auto sub = cs.coerceToRValue(expr->getSubExpr());
expr->setSubExpr(sub);
auto castContextKind =
SuppressDiagnostics ? CheckedCastContextKind::None
: CheckedCastContextKind::ForcedCast;
auto fromType = cs.getType(sub);
auto castKind = tc.typeCheckCheckedCast(
fromType, toType, castContextKind, cs.DC,
expr->getLoc(), sub,
expr->getCastTypeLoc().getSourceRange());
switch (castKind) {
/// Invalid cast.
case CheckedCastKind::Unresolved:
return nullptr;
case CheckedCastKind::Coercion:
case CheckedCastKind::BridgingCoercion: {
if (SuppressDiagnostics)
return nullptr;
if (cs.getType(sub)->isEqual(toType)) {
tc.diagnose(expr->getLoc(), diag::forced_downcast_noop, toType)
.fixItRemove(SourceRange(expr->getLoc(),
expr->getCastTypeLoc().getSourceRange().End));
} else {
tc.diagnose(expr->getLoc(), diag::forced_downcast_coercion,
cs.getType(sub), toType)
.fixItReplace(SourceRange(expr->getLoc(), expr->getExclaimLoc()),
"as");
}
// Transmute the checked cast into a coercion expression.
auto *result = new (tc.Context) CoerceExpr(sub, expr->getLoc(),
expr->getCastTypeLoc());
cs.setType(result, toType);
cs.setType(result->getCastTypeLoc(), toType);
unsigned disjunctionChoice =
(castKind == CheckedCastKind::Coercion ? 0 : 1);
return visitCoerceExpr(result, disjunctionChoice);
}
// Valid casts.
case CheckedCastKind::ArrayDowncast:
case CheckedCastKind::DictionaryDowncast:
case CheckedCastKind::SetDowncast:
case CheckedCastKind::ValueCast:
expr->setCastKind(castKind);
break;
}
return handleOptionalBindingsForCast(expr, simplifyType(cs.getType(expr)),
OptionalBindingsCastKind::Forced);
}
Expr *visitConditionalCheckedCastExpr(ConditionalCheckedCastExpr *expr) {
// If we need to insert a force-unwrap for coercions of the form
// 'as! T!', do so now.
if (hasForcedOptionalResult(expr)) {
auto *coerced = handleConditionalCheckedCastExpr(expr);
if (!coerced)
return nullptr;
return coerceImplicitlyUnwrappedOptionalToValue(
coerced, cs.getType(coerced)->getOptionalObjectType());
}
return handleConditionalCheckedCastExpr(expr);
}
Expr *handleConditionalCheckedCastExpr(ConditionalCheckedCastExpr *expr,
bool isInsideIsExpr = false) {
// Simplify the type we're casting to.
auto toType = simplifyType(cs.getType(expr->getCastTypeLoc()));
expr->getCastTypeLoc().setType(toType);
// The subexpression is always an rvalue.
auto &tc = cs.getTypeChecker();
auto sub = cs.coerceToRValue(expr->getSubExpr());
expr->setSubExpr(sub);
auto castContextKind =
(SuppressDiagnostics || isInsideIsExpr)
? CheckedCastContextKind::None
: CheckedCastContextKind::ConditionalCast;
auto fromType = cs.getType(sub);
auto castKind = tc.typeCheckCheckedCast(
fromType, toType, castContextKind, cs.DC,
expr->getLoc(), sub,
expr->getCastTypeLoc().getSourceRange());
switch (castKind) {
/// Invalid cast.
case CheckedCastKind::Unresolved:
expr->setCastKind(CheckedCastKind::ValueCast);
break;
case CheckedCastKind::Coercion:
case CheckedCastKind::BridgingCoercion: {
if (SuppressDiagnostics)
return nullptr;
tc.diagnose(expr->getLoc(), diag::conditional_downcast_coercion,
cs.getType(sub), toType);
// Transmute the checked cast into a coercion expression.
auto *coerce = new (tc.Context) CoerceExpr(sub, expr->getLoc(),
expr->getCastTypeLoc());
cs.setType(coerce, toType);
cs.setType(coerce->getCastTypeLoc(), toType);
unsigned disjunctionChoice =
(castKind == CheckedCastKind::Coercion ? 0 : 1);
Expr *result = visitCoerceExpr(coerce, disjunctionChoice);
if (!result)
return nullptr;
// Wrap the result in an optional. Mark the optional injection as
// explicit, because the user did in fact write the '?' as part of
// 'as?', even though it wasn't necessary.
result = new (tc.Context) InjectIntoOptionalExpr(
result,
OptionalType::get(toType));
result->setImplicit(false);
return cs.cacheType(result);
}
// Valid casts.
case CheckedCastKind::ArrayDowncast:
case CheckedCastKind::DictionaryDowncast:
case CheckedCastKind::SetDowncast:
case CheckedCastKind::ValueCast:
expr->setCastKind(castKind);
break;
}
return handleOptionalBindingsForCast(expr, simplifyType(cs.getType(expr)),
OptionalBindingsCastKind::Conditional);
}
Expr *visitAssignExpr(AssignExpr *expr) {
// Convert the source to the simplified destination type.
auto destTy = simplifyType(cs.getType(expr->getDest()));
auto locator =
ConstraintLocatorBuilder(cs.getConstraintLocator(expr->getSrc()));
Expr *src = coerceToType(expr->getSrc(), destTy->getRValueType(), locator);
if (!src)
return nullptr;
expr->setSrc(src);
if (!SuppressDiagnostics) {
// If we're performing an assignment to a weak or unowned variable from
// a constructor call, emit a warning that the instance will be
// immediately deallocated.
diagnoseUnownedImmediateDeallocation(cs.getTypeChecker(), expr);
}
return expr;
}
Expr *visitDiscardAssignmentExpr(DiscardAssignmentExpr *expr) {
return simplifyExprType(expr);
}
Expr *visitUnresolvedPatternExpr(UnresolvedPatternExpr *expr) {
// If we end up here, we should have diagnosed somewhere else
// already.
Expr *simplified = simplifyExprType(expr);
if (!SuppressDiagnostics
&& !cs.getType(simplified)->is<UnresolvedType>()) {
cs.TC.diagnose(simplified->getLoc(), diag::pattern_in_expr,
expr->getSubPattern()->getKind());
}
return simplified;
}
Expr *visitBindOptionalExpr(BindOptionalExpr *expr) {
return simplifyExprType(expr);
}
Expr *visitOptionalEvaluationExpr(OptionalEvaluationExpr *expr) {
Type optType = simplifyType(cs.getType(expr));
// If this is an optional chain that isn't chaining anything, and if the
// subexpression is already optional (not IUO), then this is a noop:
// reject it. This avoids confusion of the model (where the programmer
// thought it was doing something) and keeps pointless ?'s out of the
// code.
if (!SuppressDiagnostics)
if (auto *Bind = dyn_cast<BindOptionalExpr>(
expr->getSubExpr()->getSemanticsProvidingExpr())) {
if (cs.getType(Bind->getSubExpr())->isEqual(optType))
cs.TC.diagnose(expr->getLoc(), diag::optional_chain_noop,
optType).fixItRemove(Bind->getQuestionLoc());
else
cs.TC.diagnose(expr->getLoc(), diag::optional_chain_isnt_chaining);
}
Expr *subExpr = coerceToType(expr->getSubExpr(), optType,
cs.getConstraintLocator(expr));
if (!subExpr) return nullptr;
expr->setSubExpr(subExpr);
cs.setType(expr, optType);
return expr;
}
Expr *visitForceValueExpr(ForceValueExpr *expr) {
// Check to see if we are forcing an