Skip to content

Commit 115ba5c

Browse files
committed
AST: Factor out GenericTypeParamType::withDepth()
1 parent 86c30d6 commit 115ba5c

File tree

7 files changed

+25
-44
lines changed

7 files changed

+25
-44
lines changed

include/swift/AST/SubstitutionMap.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,6 @@ class LookUpConformanceInSubstitutionMap {
306306
};
307307

308308
struct OverrideSubsInfo {
309-
ASTContext &Ctx;
310309
unsigned BaseDepth;
311310
unsigned OrigDepth;
312311
SubstitutionMap BaseSubMap;

include/swift/AST/Types.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7366,6 +7366,8 @@ class GenericTypeParamType : public SubstitutableType,
73667366

73677367
Type getValueType() const;
73687368

7369+
GenericTypeParamType *withDepth(unsigned depth) const;
7370+
73697371
void Profile(llvm::FoldingSetNodeID &ID) {
73707372
// Note: We explicitly don't use 'getName()' because for canonical forms
73717373
// which don't store an identifier we'll go create a tau based form. We

lib/AST/RequirementEnvironment.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,8 @@ RequirementEnvironment::RequirementEnvironment(
4646

4747
auto conformanceToWitnessThunkGenericParamFn = [&](GenericTypeParamType *genericParam)
4848
-> GenericTypeParamType * {
49-
return GenericTypeParamType::get(genericParam->getParamKind(),
50-
genericParam->getDepth() + (covariantSelf ? 1 : 0),
51-
genericParam->getIndex(),
52-
genericParam->getValueType(), ctx);
49+
return genericParam->withDepth(
50+
genericParam->getDepth() + (covariantSelf ? 1 : 0));
5351
};
5452

5553
// This is a substitution function from the generic parameters of the
@@ -109,9 +107,7 @@ RequirementEnvironment::RequirementEnvironment(
109107
// invalid code.
110108
if (genericParam->getDepth() != 1)
111109
return Type();
112-
Type substGenericParam = GenericTypeParamType::get(
113-
genericParam->getParamKind(), depth, genericParam->getIndex(),
114-
genericParam->getValueType(), ctx);
110+
Type substGenericParam = genericParam->withDepth(depth);
115111
if (genericParam->isParameterPack()) {
116112
substGenericParam = PackType::getSingletonPackExpansion(
117113
substGenericParam);
@@ -210,10 +206,7 @@ RequirementEnvironment::RequirementEnvironment(
210206
}
211207

212208
// Create an equivalent generic parameter at the next depth.
213-
auto substGenericParam = GenericTypeParamType::get(
214-
genericParam->getParamKind(), depth, genericParam->getIndex(),
215-
genericParam->getValueType(), ctx);
216-
209+
auto substGenericParam = genericParam->withDepth(depth);
217210
genericParamTypes.push_back(substGenericParam);
218211
}
219212

lib/AST/SubstitutionMap.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -427,8 +427,7 @@ OverrideSubsInfo::OverrideSubsInfo(const NominalTypeDecl *baseNominal,
427427
const NominalTypeDecl *derivedNominal,
428428
GenericSignature baseSig,
429429
const GenericParamList *derivedParams)
430-
: Ctx(baseSig->getASTContext()),
431-
BaseDepth(0),
430+
: BaseDepth(0),
432431
OrigDepth(0),
433432
DerivedParams(derivedParams) {
434433

@@ -468,10 +467,7 @@ Type QueryOverrideSubs::operator()(SubstitutableType *type) const {
468467
->getDeclaredInterfaceType();
469468
}
470469

471-
return GenericTypeParamType::get(
472-
gp->getParamKind(),
473-
gp->getDepth() + info.OrigDepth - info.BaseDepth,
474-
gp->getIndex(), gp->getValueType(), info.Ctx);
470+
return gp->withDepth(gp->getDepth() + info.OrigDepth - info.BaseDepth);
475471
}
476472
}
477473

lib/AST/Type.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2163,6 +2163,14 @@ Type GenericTypeParamType::getValueType() const {
21632163
return ValueType;
21642164
}
21652165

2166+
GenericTypeParamType *GenericTypeParamType::withDepth(unsigned depth) const {
2167+
return GenericTypeParamType::get(getParamKind(),
2168+
depth,
2169+
getIndex(),
2170+
getValueType(),
2171+
getASTContext());
2172+
}
2173+
21662174
const llvm::fltSemantics &BuiltinFloatType::getAPFloatSemantics() const {
21672175
switch (getFPKind()) {
21682176
case BuiltinFloatType::IEEE16: return APFloat::IEEEhalf();

lib/SILOptimizer/Utils/Devirtualize.cpp

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -405,21 +405,15 @@ combineSubstitutionMaps(SubstitutionMap firstSubMap,
405405
unsigned firstDepth,
406406
unsigned secondDepth,
407407
GenericSignature genericSig) {
408-
auto &ctx = genericSig->getASTContext();
409-
410408
return SubstitutionMap::get(
411409
genericSig,
412410
[&](SubstitutableType *type) {
413411
auto *gp = cast<GenericTypeParamType>(type);
414412
if (gp->getDepth() < firstDepth)
415413
return QuerySubstitutionMap{firstSubMap}(gp);
416414

417-
auto *replacement = GenericTypeParamType::get(
418-
gp->getParamKind(),
419-
gp->getDepth() + secondDepth - firstDepth,
420-
gp->getIndex(),
421-
gp->getValueType(),
422-
ctx);
415+
auto *replacement = gp->withDepth(
416+
gp->getDepth() + secondDepth - firstDepth);
423417
return QuerySubstitutionMap{secondSubMap}(replacement);
424418
},
425419
// We might not have enough information in the substitution maps alone.
@@ -1056,16 +1050,13 @@ getWitnessMethodSubstitutions(
10561050
}
10571051

10581052
if (depth < baseDepth) {
1059-
paramType = GenericTypeParamType::get(paramType->getParamKind(),
1060-
depth, paramType->getIndex(), paramType->getValueType(), ctx);
1061-
1053+
paramType = paramType->withDepth(depth);
10621054
return Type(paramType).subst(baseSubMap);
10631055
}
10641056

10651057
depth = depth - baseDepth + 1;
10661058

1067-
paramType = GenericTypeParamType::get(paramType->getParamKind(),
1068-
depth, paramType->getIndex(), paramType->getValueType(), ctx);
1059+
paramType = paramType->withDepth(depth);
10691060
return Type(paramType).subst(origSubMap);
10701061
},
10711062
[&](CanType type, Type substType, ProtocolDecl *proto) {
@@ -1084,10 +1075,8 @@ getWitnessMethodSubstitutions(
10841075

10851076
if (depth < baseDepth) {
10861077
type = CanType(type.transformRec([&](TypeBase *t) -> std::optional<Type> {
1087-
if (t == paramType) {
1088-
return Type(GenericTypeParamType::get(paramType->getParamKind(),
1089-
depth, paramType->getIndex(), paramType->getValueType(), ctx));
1090-
}
1078+
if (t == paramType)
1079+
return paramType->withDepth(depth);
10911080

10921081
assert(!isa<GenericTypeParamType>(t));
10931082
return std::nullopt;
@@ -1099,10 +1088,8 @@ getWitnessMethodSubstitutions(
10991088
depth = depth - baseDepth + 1;
11001089

11011090
type = CanType(type.transformRec([&](TypeBase *t) -> std::optional<Type> {
1102-
if (t == paramType) {
1103-
return Type(GenericTypeParamType::get(paramType->getParamKind(),
1104-
depth, paramType->getIndex(), paramType->getValueType(), ctx));
1105-
}
1091+
if (t == paramType)
1092+
return paramType->withDepth(depth);
11061093

11071094
assert(!isa<GenericTypeParamType>(t));
11081095
return std::nullopt;

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,11 +1034,7 @@ findMissingGenericRequirementForSolutionFix(
10341034
return conformance->getType();
10351035

10361036
ASSERT(gp->getDepth() > 0);
1037-
gp = GenericTypeParamType::get(gp->getParamKind(),
1038-
gp->getDepth() - 1,
1039-
gp->getIndex(),
1040-
gp->getValueType(),
1041-
ctx);
1037+
gp = gp->withDepth(gp->getDepth() - 1);
10421038
}
10431039

10441040
if (!sig)

0 commit comments

Comments
 (0)