Skip to content

Small SubstitutionMap cleanups #16740

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/swift/AST/GenericSignatureBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ class GenericSignatureBuilder {
Optional<ProtocolConformanceRef>
operator()(CanType dependentType,
Type conformingReplacementType,
ProtocolType *conformedProtocol) const {
ProtocolDecl *conformedProtocol) const {
return builder->lookupConformance(dependentType,
conformingReplacementType,
conformedProtocol);
Expand All @@ -522,7 +522,7 @@ class GenericSignatureBuilder {
/// Lookup a protocol conformance in a module-agnostic manner.
Optional<ProtocolConformanceRef>
lookupConformance(CanType dependentType, Type conformingReplacementType,
ProtocolType *conformedProtocol);
ProtocolDecl *conformedProtocol);


/// Retrieve the lazy resolver, if there is one.
Expand Down
2 changes: 1 addition & 1 deletion include/swift/AST/RequirementEnvironment.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class RequirementEnvironment {

/// Retrieve the substitution map that maps the interface types of the
/// requirement to the interface types of the synthetic environment.
const SubstitutionMap &getRequirementToSyntheticMap() const {
SubstitutionMap getRequirementToSyntheticMap() const {
return reqToSyntheticEnvMap;
}
};
Expand Down
28 changes: 25 additions & 3 deletions include/swift/AST/SubstitutionMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class SubstitutionMap {

/// Apply a substitution to all replacement types in the map. Does not
/// change keys.
SubstitutionMap subst(const SubstitutionMap &subMap) const;
SubstitutionMap subst(SubstitutionMap subMap) const;

/// Apply a substitution to all replacement types in the map. Does not
/// change keys.
Expand Down Expand Up @@ -208,8 +208,8 @@ class SubstitutionMap {
///
/// The 'how' parameter determines if we're looking at the depth or index.
static SubstitutionMap
combineSubstitutionMaps(const SubstitutionMap &firstSubMap,
const SubstitutionMap &secondSubMap,
combineSubstitutionMaps(SubstitutionMap firstSubMap,
SubstitutionMap secondSubMap,
CombineSubstitutionMaps how,
unsigned baseDepthOrIndex,
unsigned origDepthOrIndex,
Expand Down Expand Up @@ -270,6 +270,28 @@ class SubstitutionMap {
Type lookupSubstitution(CanSubstitutableType type) const;
};

/// A function object suitable for use as a \c TypeSubstitutionFn that
/// queries an underlying \c SubstitutionMap.
struct QuerySubstitutionMap {
SubstitutionMap subMap;

Type operator()(SubstitutableType *type) const;
};

/// Functor class suitable for use as a \c LookupConformanceFn to look up a
/// conformance in a \c SubstitutionMap.
class LookUpConformanceInSubstitutionMap {
SubstitutionMap Subs;
public:
explicit LookUpConformanceInSubstitutionMap(SubstitutionMap Subs)
: Subs(Subs) {}

Optional<ProtocolConformanceRef>
operator()(CanType dependentType,
Type conformingReplacementType,
ProtocolDecl *conformedProtocol) const;
};

} // end namespace swift

namespace llvm {
Expand Down
36 changes: 7 additions & 29 deletions include/swift/AST/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,11 @@ struct QueryTypeSubstitutionMapOrIdentity {
Type operator()(SubstitutableType *type) const;
};

/// A function object suitable for use as a \c TypeSubstitutionFn that
/// queries an underlying \c SubstitutionMap.
struct QuerySubstitutionMap {
const SubstitutionMap &subMap;

Type operator()(SubstitutableType *type) const;
};

/// Function used to resolve conformances.
using GenericFunction = auto(CanType dependentType,
Type conformingReplacementType,
ProtocolType *conformedProtocol)
->Optional<ProtocolConformanceRef>;
Type conformingReplacementType,
ProtocolDecl *conformedProtocol)
-> Optional<ProtocolConformanceRef>;
using LookupConformanceFn = llvm::function_ref<GenericFunction>;

/// Functor class suitable for use as a \c LookupConformanceFn to look up a
Expand All @@ -114,21 +106,7 @@ class LookUpConformanceInModule {
Optional<ProtocolConformanceRef>
operator()(CanType dependentType,
Type conformingReplacementType,
ProtocolType *conformedProtocol) const;
};

/// Functor class suitable for use as a \c LookupConformanceFn to look up a
/// conformance in a \c SubstitutionMap.
class LookUpConformanceInSubstitutionMap {
const SubstitutionMap &Subs;
public:
explicit LookUpConformanceInSubstitutionMap(const SubstitutionMap &Subs)
: Subs(Subs) {}

Optional<ProtocolConformanceRef>
operator()(CanType dependentType,
Type conformingReplacementType,
ProtocolType *conformedProtocol) const;
ProtocolDecl *conformedProtocol) const;
};

/// Functor class suitable for use as a \c LookupConformanceFn that provides
Expand All @@ -139,7 +117,7 @@ class MakeAbstractConformanceForGenericType {
Optional<ProtocolConformanceRef>
operator()(CanType dependentType,
Type conformingReplacementType,
ProtocolType *conformedProtocol) const;
ProtocolDecl *conformedProtocol) const;
};

/// Functor class suitable for use as a \c LookupConformanceFn that fetches
Expand All @@ -153,7 +131,7 @@ class LookUpConformanceInSignature {
Optional<ProtocolConformanceRef>
operator()(CanType dependentType,
Type conformingReplacementType,
ProtocolType *conformedProtocol) const;
ProtocolDecl *conformedProtocol) const;
};

/// Flags that can be passed when substituting into a type.
Expand Down Expand Up @@ -314,7 +292,7 @@ class Type {
/// \param options Options that affect the substitutions.
///
/// \returns the substituted type, or a null type if an error occurred.
Type subst(const SubstitutionMap &substitutions,
Type subst(SubstitutionMap substitutions,
SubstOptions options = None) const;

/// Replace references to substitutable types with new, concrete types and
Expand Down
10 changes: 5 additions & 5 deletions include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -1605,7 +1605,7 @@ class NameAliasType final
friend TrailingObjects;

NameAliasType(TypeAliasDecl *typealias, Type parent,
const SubstitutionMap &substitutions, Type underlying,
SubstitutionMap substitutions, Type underlying,
RecursiveTypeProperties properties);

size_t numTrailingObjects(OverloadToken<Type>) const {
Expand All @@ -1623,7 +1623,7 @@ class NameAliasType final

public:
static NameAliasType *get(TypeAliasDecl *typealias, Type parent,
const SubstitutionMap &substitutions,
SubstitutionMap substitutions,
Type underlying);

/// \brief Returns the declaration that declares this type.
Expand Down Expand Up @@ -1660,7 +1660,7 @@ class NameAliasType final
void Profile(llvm::FoldingSetNodeID &id) const;

static void Profile(llvm::FoldingSetNodeID &id, TypeAliasDecl *typealias,
Type parent, const SubstitutionMap &substitutions,
Type parent, SubstitutionMap substitutions,
Type underlying);

// Implement isa/cast/dyncast/etc.
Expand Down Expand Up @@ -3057,7 +3057,7 @@ class GenericFunctionType final : public AnyFunctionType,

/// Substitute the given generic arguments into this generic
/// function type and return the resulting non-generic type.
FunctionType *substGenericArgs(const SubstitutionMap &subs);
FunctionType *substGenericArgs(SubstitutionMap subs);

/// Substitute the given generic arguments into this generic
/// function type using the given substitution and conformance lookup
Expand Down Expand Up @@ -4033,7 +4033,7 @@ class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,
isABICompatibleWith(CanSILFunctionType other) const;

CanSILFunctionType substGenericArgs(SILModule &silModule,
const SubstitutionMap &subs);
SubstitutionMap subs);
CanSILFunctionType substGenericArgs(SILModule &silModule,
TypeSubstitutionFn subs,
LookupConformanceFn conformances);
Expand Down
4 changes: 2 additions & 2 deletions include/swift/SIL/SILType.h
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ class SILType {
///
/// Only call this with function types!
SILType substGenericArgs(SILModule &M,
const SubstitutionMap &SubMap) const;
SubstitutionMap SubMap) const;

/// If the original type is generic, pass the signature as genericSig.
///
Expand All @@ -450,7 +450,7 @@ class SILType {
LookupConformanceFn conformances,
CanGenericSignature genericSig=CanGenericSignature()) const;

SILType subst(SILModule &silModule, const SubstitutionMap &subs) const;
SILType subst(SILModule &silModule, SubstitutionMap subs) const;

/// Return true if this type references a "ref" type that has a single pointer
/// representation. Class existentials do not always qualify.
Expand Down
2 changes: 1 addition & 1 deletion include/swift/SILOptimizer/Utils/Generics.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class ReabstractionInfo {

// Create a new substituted type with the updated signature.
CanSILFunctionType createSubstitutedType(SILFunction *OrigF,
const SubstitutionMap &SubstMap,
SubstitutionMap SubstMap,
bool HasUnboundGenericParams);

void createSubstitutedAndSpecializedTypes();
Expand Down
6 changes: 3 additions & 3 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2874,7 +2874,7 @@ StringRef ASTContext::getSwiftName(KnownFoundationEntity kind) {
//===----------------------------------------------------------------------===//

NameAliasType::NameAliasType(TypeAliasDecl *typealias, Type parent,
const SubstitutionMap &substitutions,
SubstitutionMap substitutions,
Type underlying,
RecursiveTypeProperties properties)
: SugarType(TypeKind::NameAlias, underlying, properties),
Expand All @@ -2897,7 +2897,7 @@ NameAliasType::NameAliasType(TypeAliasDecl *typealias, Type parent,
}

NameAliasType *NameAliasType::get(TypeAliasDecl *typealias, Type parent,
const SubstitutionMap &substitutions,
SubstitutionMap substitutions,
Type underlying) {
// Compute the recursive properties.
//
Expand Down Expand Up @@ -2950,7 +2950,7 @@ void NameAliasType::Profile(llvm::FoldingSetNodeID &id) const {
void NameAliasType::Profile(
llvm::FoldingSetNodeID &id,
TypeAliasDecl *typealias,
Type parent, const SubstitutionMap &substitutions,
Type parent, SubstitutionMap substitutions,
Type underlying) {
id.AddPointer(typealias);
id.AddPointer(parent.getPointer());
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/GenericSignature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ bool GenericSignature::isRequirementSatisfied(Requirement requirement) {
return conformsToProtocol(canFirstType, protocol);
else
return (bool)GSB->lookupConformance(/*dependentType=*/CanType(),
canFirstType, protocolType);
canFirstType, protocol);
}

case RequirementKind::SameType: {
Expand Down
18 changes: 7 additions & 11 deletions lib/AST/GenericSignatureBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2639,9 +2639,7 @@ GenericSignatureBuilder::resolveConcreteConformance(ResolvedType type,
// Lookup the conformance of the concrete type to this protocol.
auto conformance =
lookupConformance(type.getDependentType(*this)->getCanonicalType(),
concrete,
proto->getDeclaredInterfaceType()
->castTo<ProtocolType>());
concrete, proto);
if (!conformance) {
if (!concrete->hasError() && concreteSource->getLoc().isValid()) {
Impl->HadAnyError = true;
Expand Down Expand Up @@ -2672,9 +2670,7 @@ const RequirementSource *GenericSignatureBuilder::resolveSuperConformance(
// Lookup the conformance of the superclass to this protocol.
auto conformance =
lookupConformance(type.getDependentType(*this)->getCanonicalType(),
superclass,
proto->getDeclaredInterfaceType()
->castTo<ProtocolType>());
superclass, proto);
if (!conformance) return nullptr;

// Conformance to this protocol is redundant; update the requirement source
Expand Down Expand Up @@ -3825,16 +3821,16 @@ GenericSignatureBuilder::getLookupConformanceFn()
Optional<ProtocolConformanceRef>
GenericSignatureBuilder::lookupConformance(CanType dependentType,
Type conformingReplacementType,
ProtocolType *conformedProtocol) {
ProtocolDecl *conformedProtocol) {
if (conformingReplacementType->isTypeParameter())
return ProtocolConformanceRef(conformedProtocol->getDecl());
return ProtocolConformanceRef(conformedProtocol);

// Figure out which module to look into.
// FIXME: When lookupConformance() starts respecting modules, we'll need
// to do some filtering here.
ModuleDecl *searchModule = conformedProtocol->getDecl()->getParentModule();
ModuleDecl *searchModule = conformedProtocol->getParentModule();
auto result = searchModule->lookupConformance(conformingReplacementType,
conformedProtocol->getDecl());
conformedProtocol);
if (result && getLazyResolver())
getLazyResolver()->markConformanceUsed(*result, searchModule);

Expand Down Expand Up @@ -4726,7 +4722,7 @@ ConstraintResult GenericSignatureBuilder::addTypeRequirement(
// getLookupConformanceFns used in here don't use that parameter anyway.
auto dependentType = CanType();
auto conformance =
getLookupConformanceFn()(dependentType, subjectType, proto);
getLookupConformanceFn()(dependentType, subjectType, proto->getDecl());

// FIXME: diagnose if there's no conformance.
if (conformance) {
Expand Down
3 changes: 1 addition & 2 deletions lib/AST/ProtocolConformance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ ProtocolConformanceRef::subst(Type origType,

// Check the conformance map.
if (auto result = conformances(origType->getCanonicalType(),
substType,
proto->getDeclaredType())) {
substType, proto)) {
return *result;
}

Expand Down
4 changes: 1 addition & 3 deletions lib/AST/RequirementEnvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,8 @@ RequirementEnvironment::RequirementEnvironment(
return substGenericParam;
},
[selfType, substConcreteType, conformance, conformanceDC, &ctx](
CanType type, Type replacement, ProtocolType *protoType)
CanType type, Type replacement, ProtocolDecl *proto)
-> Optional<ProtocolConformanceRef> {
auto proto = protoType->getDecl();

// The protocol 'Self' conforms concretely to the conforming type.
if (type->isEqual(selfType)) {
ProtocolConformance *specialized = conformance;
Expand Down
Loading