Skip to content

Commit 9bfe02e

Browse files
committed
AST: Refactor getOverrideSubstitutions() to not take derivedSig
1 parent e7785e1 commit 9bfe02e

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

include/swift/AST/SubstitutionMap.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ namespace llvm {
3333
namespace swift {
3434

3535
class GenericEnvironment;
36+
class GenericParamList;
3637
class SubstitutableType;
3738
typedef CanTypeWrapper<GenericTypeParamType> CanGenericTypeParamType;
3839

@@ -207,7 +208,7 @@ class SubstitutionMap {
207208
getOverrideSubstitutions(const ClassDecl *baseClass,
208209
const ClassDecl *derivedClass,
209210
GenericSignature baseSig,
210-
GenericSignature derivedSig);
211+
GenericParamList *derivedParams);
211212

212213
/// Combine two substitution maps as follows.
213214
///

lib/AST/SubstitutionMap.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "swift/AST/ASTContext.h"
2828
#include "swift/AST/Decl.h"
2929
#include "swift/AST/GenericEnvironment.h"
30+
#include "swift/AST/GenericParamList.h"
3031
#include "swift/AST/LazyResolver.h"
3132
#include "swift/AST/Module.h"
3233
#include "swift/AST/ProtocolConformance.h"
@@ -510,18 +511,23 @@ SubstitutionMap::getOverrideSubstitutions(
510511

511512
auto baseSig = baseDecl->getInnermostDeclContext()
512513
->getGenericSignatureOfContext();
513-
auto derivedSig = derivedDecl->getInnermostDeclContext()
514-
->getGenericSignatureOfContext();
515514

516-
return getOverrideSubstitutions(baseClass, derivedClass,
517-
baseSig, derivedSig);
515+
// If more kinds of overridable decls with generic parameter lists appear,
516+
// add them here.
517+
GenericParamList *derivedParams = nullptr;
518+
if (auto *funcDecl = dyn_cast<AbstractFunctionDecl>(derivedDecl))
519+
derivedParams = funcDecl->getGenericParams();
520+
else if (auto *subscriptDecl = dyn_cast<SubscriptDecl>(derivedDecl))
521+
derivedParams = subscriptDecl->getGenericParams();
522+
523+
return getOverrideSubstitutions(baseClass, derivedClass, baseSig, derivedParams);
518524
}
519525

520526
SubstitutionMap
521527
SubstitutionMap::getOverrideSubstitutions(const ClassDecl *baseClass,
522528
const ClassDecl *derivedClass,
523529
GenericSignature baseSig,
524-
GenericSignature derivedSig) {
530+
GenericParamList *derivedParams) {
525531
if (baseSig.isNull())
526532
return SubstitutionMap();
527533

@@ -547,10 +553,14 @@ SubstitutionMap::getOverrideSubstitutions(const ClassDecl *baseClass,
547553
if (auto gp = type->getAs<GenericTypeParamType>()) {
548554
if (gp->getDepth() >= baseDepth) {
549555
assert(gp->getDepth() == baseDepth);
550-
auto substGP = GenericTypeParamType::get(gp->isTypeSequence(),
551-
gp->getDepth() + origDepth - baseDepth,
552-
gp->getIndex(), ctx);
553-
return derivedSig->getSugaredType(substGP);
556+
if (derivedParams != nullptr) {
557+
return derivedParams->getParams()[gp->getIndex()]
558+
->getDeclaredInterfaceType();
559+
}
560+
561+
return GenericTypeParamType::get(gp->isTypeSequence(),
562+
gp->getDepth() + origDepth - baseDepth,
563+
gp->getIndex(), ctx);
554564
}
555565
}
556566

0 commit comments

Comments
 (0)