Skip to content

Commit da53c3d

Browse files
committed
AST: Add new form of getMemberSubstitutionMap()
1 parent 18550b1 commit da53c3d

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

include/swift/AST/Types.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -882,13 +882,15 @@ class alignas(1 << TypeAlignInBits) TypeBase {
882882
/// Get the substitutions to apply to the type of the given member as seen
883883
/// from this base type.
884884
///
885-
/// If the member has its own generic parameters, they will remain unchanged
886-
/// by the substitution.
885+
/// \param genericEnv If non-null, generic parameters of the member are
886+
/// mapped to context archetypes of this generic environment.
887887
SubstitutionMap getMemberSubstitutionMap(ModuleDecl *module,
888-
const ValueDecl *member);
888+
const ValueDecl *member,
889+
GenericEnvironment *genericEnv=nullptr);
889890

890891
/// Deprecated version of the above.
891-
TypeSubstitutionMap getMemberSubstitutions(const ValueDecl *member);
892+
TypeSubstitutionMap getMemberSubstitutions(const ValueDecl *member,
893+
GenericEnvironment *genericEnv=nullptr);
892894

893895
/// Retrieve the type of the given member as seen through the given base
894896
/// type, substituting generic arguments where necessary.

lib/AST/Type.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "swift/AST/TypeWalker.h"
2121
#include "swift/AST/Decl.h"
2222
#include "swift/AST/AST.h"
23+
#include "swift/AST/GenericEnvironment.h"
2324
#include "swift/AST/LazyResolver.h"
2425
#include "swift/AST/Module.h"
2526
#include "swift/AST/SubstitutionMap.h"
@@ -3240,7 +3241,7 @@ TypeSubstitutionMap TypeBase::getContextSubstitutions(const DeclContext *dc) {
32403241
}
32413242

32423243
SubstitutionMap TypeBase::getContextSubstitutionMap(
3243-
ModuleDecl *module, const DeclContext *dc) {
3244+
ModuleDecl *module, const DeclContext *dc) {
32443245
auto *genericSig = dc->getGenericSignatureOfContext();
32453246
if (genericSig == nullptr)
32463247
return SubstitutionMap();
@@ -3249,7 +3250,9 @@ SubstitutionMap TypeBase::getContextSubstitutionMap(
32493250
LookUpConformanceInModule(module));
32503251
}
32513252

3252-
TypeSubstitutionMap TypeBase::getMemberSubstitutions(const ValueDecl *member) {
3253+
TypeSubstitutionMap TypeBase::getMemberSubstitutions(
3254+
const ValueDecl *member,
3255+
GenericEnvironment *genericEnv) {
32533256
auto *memberDC = member->getDeclContext();
32543257

32553258
TypeSubstitutionMap substitutions;
@@ -3262,14 +3265,18 @@ TypeSubstitutionMap TypeBase::getMemberSubstitutions(const ValueDecl *member) {
32623265
// We need this since code completion and diagnostics want to be able
32633266
// to call getTypeOfMember() with functions and nested types.
32643267
if (isa<AbstractFunctionDecl>(member) ||
3265-
isa<GenericTypeDecl>(member)) {
3268+
isa<GenericTypeDecl>(member) ||
3269+
isa<SubscriptDecl>(member)) {
32663270
auto *innerDC = member->getInnermostDeclContext();
32673271
if (innerDC->isInnermostContextGeneric()) {
32683272
auto *sig = innerDC->getGenericSignatureOfContext();
32693273
for (auto param : sig->getInnermostGenericParams()) {
32703274
auto *genericParam = param->getCanonicalType()
32713275
->castTo<GenericTypeParamType>();
3272-
substitutions[genericParam] = param;
3276+
substitutions[genericParam] =
3277+
(genericEnv
3278+
? genericEnv->mapTypeIntoContext(param)
3279+
: param);
32733280
}
32743281
}
32753282
}
@@ -3278,13 +3285,15 @@ TypeSubstitutionMap TypeBase::getMemberSubstitutions(const ValueDecl *member) {
32783285
}
32793286

32803287
SubstitutionMap TypeBase::getMemberSubstitutionMap(
3281-
ModuleDecl *module, const ValueDecl *member) {
3288+
ModuleDecl *module, const ValueDecl *member,
3289+
GenericEnvironment *genericEnv) {
32823290
auto *genericSig = member->getInnermostDeclContext()
32833291
->getGenericSignatureOfContext();
32843292
if (genericSig == nullptr)
32853293
return SubstitutionMap();
3294+
auto subs = getMemberSubstitutions(member, genericEnv);
32863295
return genericSig->getSubstitutionMap(
3287-
QueryTypeSubstitutionMap{getMemberSubstitutions(member)},
3296+
QueryTypeSubstitutionMap{subs},
32883297
LookUpConformanceInModule(module));
32893298
}
32903299

0 commit comments

Comments
 (0)