AST: Completely hide internal representation of SubstitutionMap

Make the addSubstitution() and addConformance() methods private,
and declare GenericEnvironment and GenericSignature as friends of
SubstitutionMap.

At some point in the future, we can switch to a more efficient
representation of SubstitutionMap, where instead of storing
multiple hashtables, we store arrays; the keys are pre-determined.
This commit is contained in:
Slava Pestov
2017-02-12 01:32:56 -08:00
parent 3cd7bb2571
commit c5dfb5238a
3 changed files with 29 additions and 12 deletions

View File

@@ -77,6 +77,8 @@ class alignas(1 << TypeAlignInBits) GenericSignature final
/// Retrieve the generic signature builder for the given generic signature.
GenericSignatureBuilder *getGenericSignatureBuilder(ModuleDecl &mod);
void populateParentMap(SubstitutionMap &subMap) const;
friend class ArchetypeType;
public:

View File

@@ -20,6 +20,9 @@
// either archetypes or interface types. Care must be exercised to only look up
// one or the other.
//
// SubstitutionMaps are constructed by calling the getSubstitutionMap() method
// on a GenericSignature or GenericEnvironment.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_AST_SUBSTITUTION_MAP_H
@@ -35,6 +38,8 @@
namespace swift {
class GenericSignature;
class CenericEnvironment;
class SubstitutableType;
template<class Type> class CanTypeWrapper;
@@ -43,6 +48,7 @@ typedef CanTypeWrapper<SubstitutableType> CanSubstitutableType;
class SubstitutionMap {
using ParentType = std::pair<CanType, AssociatedTypeDecl *>;
// FIXME: Switch to a more efficient representation.
llvm::DenseMap<SubstitutableType *, Type> subMap;
llvm::DenseMap<TypeBase *, SmallVector<ProtocolConformanceRef, 1>>
conformanceMap;
@@ -77,15 +83,12 @@ public:
/// Retrieve the conformances for the given type.
ArrayRef<ProtocolConformanceRef> getConformances(CanType type) const;
void addSubstitution(CanSubstitutableType type, Type replacement);
/// Look up the replacement for the given type parameter or interface type.
/// Note that this only finds replacements for maps that are directly
/// stored inside the map. In most cases, you should call Type::subst()
/// instead, since that will resolve member types also.
Type lookupSubstitution(CanSubstitutableType type) const;
void addConformance(CanType type, ProtocolConformanceRef conformance);
void addParent(CanType type, CanType parent,
AssociatedTypeDecl *assocType);
bool empty() const {
return subMap.empty();
}
@@ -142,6 +145,19 @@ public:
void dump(llvm::raw_ostream &out) const;
LLVM_ATTRIBUTE_DEPRECATED(void dump() const, "only for use in the debugger");
private:
friend class GenericSignature;
friend class GenericEnvironment;
// You should not need to call these directly to build SubstitutionMaps;
// instead, use GenericSignature::getSubstitutionMap() or
// GenericEnvironment::getSubstitutionMap().
void addSubstitution(CanSubstitutableType type, Type replacement);
void addConformance(CanType type, ProtocolConformanceRef conformance);
void addParent(CanType type, CanType parent,
AssociatedTypeDecl *assocType);
};
} // end namespace swift