Skip to content

Commit b243ee3

Browse files
authored
Merge pull request #7976 from slavapestov/kill-gather-all-substitutions
Kill TypeBase::gatherAllSubstitutions()
2 parents b6c3c07 + c4f32ed commit b243ee3

40 files changed

+525
-639
lines changed

include/swift/AST/ASTContext.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -724,18 +724,9 @@ class ASTContext {
724724
InheritedProtocolConformance *
725725
getInheritedConformance(Type type, ProtocolConformance *inherited);
726726

727-
/// \brief Create trivial substitutions for the given bound generic type.
728-
Optional<SubstitutionList>
729-
createTrivialSubstitutions(BoundGenericType *BGT,
730-
DeclContext *gpContext) const;
731-
732727
/// Record compiler-known protocol information in the AST.
733728
void recordKnownProtocols(ModuleDecl *Stdlib);
734729

735-
/// \brief Retrieve the substitutions for a bound generic type, if known.
736-
Optional<SubstitutionList>
737-
getSubstitutions(TypeBase *type, DeclContext *gpContext) const;
738-
739730
/// Get the lazy data for the given declaration.
740731
///
741732
/// \param lazyLoader If non-null, the lazy loader to use when creating the
@@ -852,12 +843,6 @@ class ASTContext {
852843
void setBriefComment(const Decl *D, StringRef Comment);
853844

854845
friend TypeBase;
855-
856-
/// \brief Set the substitutions for the given bound generic type.
857-
void setSubstitutions(TypeBase *type,
858-
DeclContext *gpContext,
859-
SubstitutionList Subs) const;
860-
861846
friend ArchetypeType;
862847

863848
/// Provide context-level uniquing for SIL lowered type layouts and boxes.

include/swift/AST/GenericSignature.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,6 @@ class alignas(1 << TypeAlignInBits) GenericSignature final
214214
LookupConformanceFn lookupConformance,
215215
SmallVectorImpl<Substitution> &result) const;
216216

217-
/// Build an array of substitutions from an interface type substitution map,
218-
/// using the given function to look up conformances.
219-
void getSubstitutions(const TypeSubstitutionMap &subMap,
220-
LookupConformanceFn lookupConformance,
221-
SmallVectorImpl<Substitution> &result) const;
222-
223217
/// Build an array of substitutions from an interface type substitution map,
224218
/// using the given function to look up conformances.
225219
void getSubstitutions(const SubstitutionMap &subMap,

include/swift/AST/SubstitutionMap.h

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ class SubstitutableType;
4545
template<class Type> class CanTypeWrapper;
4646
typedef CanTypeWrapper<SubstitutableType> CanSubstitutableType;
4747

48+
enum class CombineSubstitutionMaps {
49+
AtDepth,
50+
AtIndex
51+
};
52+
4853
class SubstitutionMap {
4954
using ParentType = std::pair<CanType, AssociatedTypeDecl *>;
5055

@@ -131,19 +136,23 @@ class SubstitutionMap {
131136

132137
/// Combine two substitution maps as follows.
133138
///
134-
/// The result is written in terms of the generic parameters of 'baseSig'.
139+
/// The result is written in terms of the generic parameters of 'genericSig'.
140+
///
141+
/// Generic parameters with a depth or index less than 'firstDepthOrIndex'
142+
/// come from 'firstSubMap'.
135143
///
136-
/// Generic parameters with a depth less than 'baseDepth' come from
137-
/// 'baseSubs'.
144+
/// Generic parameters with a depth greater than 'firstDepthOrIndex' come
145+
/// from 'secondSubMap', but are looked up starting with a depth or index of
146+
/// 'secondDepthOrIndex'.
138147
///
139-
/// Generic parameters with a depth greater than 'baseDepth' come from
140-
/// 'origSubs', but are looked up starting with a depth of 'origDepth'.
148+
/// The 'how' parameter determines if we're looking at the depth or index.
141149
static SubstitutionMap
142-
combineSubstitutionMaps(const SubstitutionMap &baseSubMap,
143-
const SubstitutionMap &origSubMap,
144-
unsigned baseDepth,
145-
unsigned origDepth,
146-
GenericSignature *baseSig);
150+
combineSubstitutionMaps(const SubstitutionMap &firstSubMap,
151+
const SubstitutionMap &secondSubMap,
152+
CombineSubstitutionMaps how,
153+
unsigned baseDepthOrIndex,
154+
unsigned origDepthOrIndex,
155+
GenericSignature *genericSig);
147156

148157
/// Dump the contents of this substitution map for debugging purposes.
149158
void dump(llvm::raw_ostream &out) const;

include/swift/AST/Types.h

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,9 @@ class alignas(1 << TypeAlignInBits) TypeBase {
516516
return getRecursiveProperties().hasTypeParameter();
517517
}
518518

519+
/// Return the root generic parameter of this type parameter type.
520+
GenericTypeParamType *getRootGenericParam();
521+
519522
/// Determines whether this type is an lvalue. This includes both straight
520523
/// lvalue types as well as tuples or optionals of lvalues.
521524
bool isLValueType() {
@@ -597,15 +600,6 @@ class alignas(1 << TypeAlignInBits) TypeBase {
597600
/// specialized, but the type Vector is not.
598601
bool isSpecialized();
599602

600-
/// Gather all of the substitutions used to produce the given specialized type
601-
/// from its unspecialized type.
602-
///
603-
/// \returns ASTContext-allocated substitutions.
604-
SubstitutionList gatherAllSubstitutions(
605-
ModuleDecl *module,
606-
LazyResolver *resolver,
607-
DeclContext *gpContext = nullptr);
608-
609603
/// \brief Determine whether this type is a legal, lowered SIL type.
610604
///
611605
/// A type is SIL-illegal if it is:
@@ -866,11 +860,17 @@ class alignas(1 << TypeAlignInBits) TypeBase {
866860
/// the context of the extension above will produce substitutions T
867861
/// -> Int and U -> String suitable for mapping the type of
868862
/// \c SomeArray.
863+
///
864+
/// \param genericEnv If non-null and the type is nested inside of a
865+
/// generic function, generic parameters of the outer context are
866+
/// mapped to context archetypes of this generic environment.
869867
SubstitutionMap getContextSubstitutionMap(ModuleDecl *module,
870-
const DeclContext *dc);
868+
const DeclContext *dc,
869+
GenericEnvironment *genericEnv=nullptr);
871870

872871
/// Deprecated version of the above.
873-
TypeSubstitutionMap getContextSubstitutions(const DeclContext *dc);
872+
TypeSubstitutionMap getContextSubstitutions(const DeclContext *dc,
873+
GenericEnvironment *genericEnv=nullptr);
874874

875875
/// Get the substitutions to apply to the type of the given member as seen
876876
/// from this base type.
@@ -4370,6 +4370,15 @@ inline bool TypeBase::isTypeParameter() {
43704370
return false;
43714371
}
43724372

4373+
inline GenericTypeParamType *TypeBase::getRootGenericParam() {
4374+
Type t(this);
4375+
4376+
while (auto *memberTy = t->getAs<DependentMemberType>())
4377+
t = memberTy->getBase();
4378+
4379+
return t->castTo<GenericTypeParamType>();
4380+
}
4381+
43734382
inline bool TypeBase::isExistentialType() {
43744383
return getCanonicalType().isExistentialType();
43754384
}

include/swift/SIL/SILInstruction.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3876,12 +3876,6 @@ class WitnessMethodInst final
38763876

38773877
ProtocolConformanceRef getConformance() const { return Conformance; }
38783878

3879-
/// Get a representation of the lookup type as a substitution of the
3880-
/// protocol's Self archetype.
3881-
Substitution getSelfSubstitution() const {
3882-
return Substitution{getLookupType(), Conformance};
3883-
}
3884-
38853879
ArrayRef<Operand> getAllOperands() const {
38863880
return { getTrailingObjects<Operand>(), NumOperands };
38873881
}

include/swift/SIL/SILType.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,13 @@ class SILType {
423423
SILType substGenericArgs(SILModule &M,
424424
SubstitutionList Subs) const;
425425

426+
/// Transform the function type SILType by replacing all of its interface
427+
/// generic args with the appropriate item from the substitution.
428+
///
429+
/// Only call this with function types!
430+
SILType substGenericArgs(SILModule &M,
431+
const SubstitutionMap &SubMap) const;
432+
426433
/// If the original type is generic, pass the signature as genericSig.
427434
///
428435
/// If the replacement types are generic, you must push a generic context
@@ -434,10 +441,6 @@ class SILType {
434441

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

437-
/// If this is a specialized generic type, return all substitutions used to
438-
/// generate it.
439-
SubstitutionList gatherAllSubstitutions(SILModule &M);
440-
441444
/// Return true if this type references a "ref" type that has a single pointer
442445
/// representation. Class existentials do not always qualify.
443446
bool isHeapObjectReferenceType() const;

lib/AST/ASTContext.cpp

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,6 @@ struct ASTContext::Implementation {
262262
llvm::FoldingSet<ProtocolType> ProtocolTypes;
263263
llvm::FoldingSet<LayoutConstraintInfo> LayoutConstraints;
264264

265-
llvm::DenseMap<std::pair<TypeBase *, DeclContext *>,
266-
SubstitutionList>
267-
BoundGenericSubstitutions;
268-
269265
/// The set of normal protocol conformances.
270266
llvm::FoldingSet<NormalProtocolConformance> NormalConformances;
271267

@@ -1101,52 +1097,6 @@ static AllocationArena getArena(RecursiveTypeProperties properties) {
11011097
: AllocationArena::Permanent;
11021098
}
11031099

1104-
Optional<SubstitutionList>
1105-
ASTContext::createTrivialSubstitutions(BoundGenericType *BGT,
1106-
DeclContext *gpContext) const {
1107-
assert(gpContext && "No generic parameter context");
1108-
assert(gpContext->getGenericEnvironmentOfContext() != nullptr &&
1109-
"Not type-checked yet");
1110-
assert(BGT->getGenericArgs().size() == 1);
1111-
Substitution Subst(BGT->getGenericArgs()[0], {});
1112-
auto Substitutions = AllocateCopy(llvm::makeArrayRef(Subst));
1113-
auto arena = getArena(BGT->getRecursiveProperties());
1114-
Impl.getArena(arena).BoundGenericSubstitutions
1115-
.insert(std::make_pair(std::make_pair(BGT, gpContext), Substitutions));
1116-
return Substitutions;
1117-
}
1118-
1119-
Optional<SubstitutionList>
1120-
ASTContext::getSubstitutions(TypeBase *type,
1121-
DeclContext *gpContext) const {
1122-
assert(gpContext && "Missing generic parameter context");
1123-
auto arena = getArena(type->getRecursiveProperties());
1124-
auto &boundGenericSubstitutions
1125-
= Impl.getArena(arena).BoundGenericSubstitutions;
1126-
auto known = boundGenericSubstitutions.find({type, gpContext});
1127-
if (known != boundGenericSubstitutions.end())
1128-
return known->second;
1129-
1130-
// We can trivially create substitutions for Array and Optional.
1131-
if (auto bound = dyn_cast<BoundGenericType>(type))
1132-
if (bound->getDecl() == getArrayDecl() ||
1133-
bound->getDecl() == getOptionalDecl())
1134-
return createTrivialSubstitutions(bound, gpContext);
1135-
1136-
return None;
1137-
}
1138-
1139-
void ASTContext::setSubstitutions(TypeBase* type,
1140-
DeclContext *gpContext,
1141-
SubstitutionList Subs) const {
1142-
auto arena = getArena(type->getRecursiveProperties());
1143-
auto &boundGenericSubstitutions
1144-
= Impl.getArena(arena).BoundGenericSubstitutions;
1145-
assert(boundGenericSubstitutions.count({type, gpContext}) == 0 &&
1146-
"Already have substitutions?");
1147-
boundGenericSubstitutions[{type, gpContext}] = Subs;
1148-
}
1149-
11501100
void ASTContext::addSearchPath(StringRef searchPath, bool isFramework,
11511101
bool isSystem) {
11521102
OptionSet<SearchPathKind> &loaded = Impl.SearchPathsSet[searchPath];
@@ -1609,13 +1559,12 @@ size_t ASTContext::Implementation::Arena::getTotalMemory() const {
16091559
llvm::capacity_in_bytes(LValueTypes) +
16101560
llvm::capacity_in_bytes(InOutTypes) +
16111561
llvm::capacity_in_bytes(DependentMemberTypes) +
1612-
llvm::capacity_in_bytes(DynamicSelfTypes) +
1562+
llvm::capacity_in_bytes(DynamicSelfTypes);
16131563
// EnumTypes ?
16141564
// StructTypes ?
16151565
// ClassTypes ?
16161566
// UnboundGenericTypes ?
16171567
// BoundGenericTypes ?
1618-
llvm::capacity_in_bytes(BoundGenericSubstitutions);
16191568
// NormalConformances ?
16201569
// SpecializedConformances ?
16211570
// InheritedConformances ?

lib/AST/ASTMangler.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,12 +1468,10 @@ static bool isMethodDecl(const Decl *decl) {
14681468
}
14691469

14701470
static bool genericParamIsBelowDepth(Type type, unsigned methodDepth) {
1471-
if (auto gp = type->getAs<GenericTypeParamType>()) {
1471+
if (type->isTypeParameter()) {
1472+
auto gp = type->getRootGenericParam();
14721473
return gp->getDepth() >= methodDepth;
14731474
}
1474-
if (auto dm = type->getAs<DependentMemberType>()) {
1475-
return genericParamIsBelowDepth(dm->getBase(), methodDepth);
1476-
}
14771475
// Non-dependent types in a same-type requirement don't affect whether we
14781476
// mangle the requirement.
14791477
return false;

lib/AST/Decl.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2914,10 +2914,7 @@ findProtocolSelfReferences(const ProtocolDecl *proto, Type type,
29142914

29152915
// Special handling for associated types.
29162916
if (!skipAssocTypes && type->is<DependentMemberType>()) {
2917-
while (auto depMemTy = type->getAs<DependentMemberType>()) {
2918-
type = depMemTy->getBase();
2919-
}
2920-
2917+
type = type->getRootGenericParam();
29212918
if (proto->getProtocolSelfType()->isEqual(type))
29222919
return SelfReferenceKind::Other();
29232920
}

lib/AST/GenericSignature.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -461,14 +461,6 @@ getSubstitutionMap(TypeSubstitutionFn subs,
461461
return subMap;
462462
}
463463

464-
void GenericSignature::
465-
getSubstitutions(const TypeSubstitutionMap &subs,
466-
GenericSignature::LookupConformanceFn lookupConformance,
467-
SmallVectorImpl<Substitution> &result) const {
468-
getSubstitutions(QueryTypeSubstitutionMap{subs}, lookupConformance,
469-
result);
470-
}
471-
472464
void GenericSignature::
473465
getSubstitutions(TypeSubstitutionFn subs,
474466
GenericSignature::LookupConformanceFn lookupConformance,

0 commit comments

Comments
 (0)