Skip to content

Commit d86551d

Browse files
committed
Lift Requirement and Parameter Accessors up to GenericSignature
Start treating the null {Can}GenericSignature as a regular signature with no requirements and no parameters. This not only makes for a much safer abstraction, but allows us to simplify a lot of the clients of GenericSignature that would previously have to check for null before using the abstraction.
1 parent 9a1d18a commit d86551d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+283
-247
lines changed

include/swift/AST/ASTMangler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ class ASTMangler : public Mangler {
387387

388388
void appendRequirement(const Requirement &reqt);
389389

390-
void appendGenericSignatureParts(TypeArrayView<GenericTypeParamType> params,
390+
void appendGenericSignatureParts(ArrayRef<CanTypeWrapper<GenericTypeParamType>> params,
391391
unsigned initialParamDepth,
392392
ArrayRef<Requirement> requirements);
393393

include/swift/AST/GenericSignature.h

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,19 @@ class GenericSignature {
182182
// first, or use isEqual.
183183
void operator==(GenericSignature T) const = delete;
184184
void operator!=(GenericSignature T) const = delete;
185+
186+
public:
187+
/// Retrieve the generic parameters.
188+
TypeArrayView<GenericTypeParamType> getGenericParams() const;
189+
190+
/// Retrieve the innermost generic parameters.
191+
///
192+
/// Given a generic signature for a nested generic type, produce an
193+
/// array of the generic parameters for the innermost generic type.
194+
TypeArrayView<GenericTypeParamType> getInnermostGenericParams() const;
195+
196+
/// Retrieve the requirements.
197+
ArrayRef<Requirement> getRequirements() const;
185198
};
186199

187200
/// A reference to a canonical generic signature.
@@ -255,23 +268,6 @@ class alignas(1 << TypeAlignInBits) GenericSignatureImpl final
255268
friend class ArchetypeType;
256269

257270
public:
258-
/// Retrieve the generic parameters.
259-
TypeArrayView<GenericTypeParamType> getGenericParams() const {
260-
return TypeArrayView<GenericTypeParamType>(
261-
{getTrailingObjects<Type>(), NumGenericParams});
262-
}
263-
264-
/// Retrieve the innermost generic parameters.
265-
///
266-
/// Given a generic signature for a nested generic type, produce an
267-
/// array of the generic parameters for the innermost generic type.
268-
TypeArrayView<GenericTypeParamType> getInnermostGenericParams() const;
269-
270-
/// Retrieve the requirements.
271-
ArrayRef<Requirement> getRequirements() const {
272-
return {getTrailingObjects<Requirement>(), NumRequirements};
273-
}
274-
275271
/// Only allow allocation by doing a placement new.
276272
void *operator new(size_t Bytes, void *Mem) {
277273
assert(Mem);
@@ -439,6 +435,27 @@ class alignas(1 << TypeAlignInBits) GenericSignatureImpl final
439435
void print(ASTPrinter &Printer, PrintOptions Opts = PrintOptions()) const;
440436
SWIFT_DEBUG_DUMP;
441437
std::string getAsString() const;
438+
439+
private:
440+
friend GenericSignature;
441+
friend CanGenericSignature;
442+
443+
/// Retrieve the generic parameters.
444+
TypeArrayView<GenericTypeParamType> getGenericParams() const {
445+
return TypeArrayView<GenericTypeParamType>(
446+
{getTrailingObjects<Type>(), NumGenericParams});
447+
}
448+
449+
/// Retrieve the innermost generic parameters.
450+
///
451+
/// Given a generic signature for a nested generic type, produce an
452+
/// array of the generic parameters for the innermost generic type.
453+
TypeArrayView<GenericTypeParamType> getInnermostGenericParams() const;
454+
455+
/// Retrieve the requirements.
456+
ArrayRef<Requirement> getRequirements() const {
457+
return {getTrailingObjects<Requirement>(), NumRequirements};
458+
}
442459
};
443460

444461
void simple_display(raw_ostream &out, GenericSignature sig);

lib/AST/ASTContext.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@ static VarDecl *getPointeeProperty(VarDecl *&cache,
914914
if (!nominal) return nullptr;
915915
auto sig = nominal->getGenericSignature();
916916
if (!sig) return nullptr;
917-
if (sig->getGenericParams().size() != 1) return nullptr;
917+
if (sig.getGenericParams().size() != 1) return nullptr;
918918

919919
// There must be a property named "pointee".
920920
auto identifier = ctx.getIdentifier("pointee");
@@ -924,7 +924,7 @@ static VarDecl *getPointeeProperty(VarDecl *&cache,
924924
// The property must have type T.
925925
auto *property = dyn_cast<VarDecl>(results[0]);
926926
if (!property) return nullptr;
927-
if (!property->getInterfaceType()->isEqual(sig->getGenericParams()[0]))
927+
if (!property->getInterfaceType()->isEqual(sig.getGenericParams()[0]))
928928
return nullptr;
929929

930930
cache = property;
@@ -1844,13 +1844,13 @@ GenericSignatureBuilder *ASTContext::getOrCreateGenericSignatureBuilder(
18441844
reprocessedSig->print(llvm::errs());
18451845
llvm::errs() << "\n";
18461846

1847-
if (sig->getGenericParams().size() ==
1848-
reprocessedSig->getGenericParams().size() &&
1849-
sig->getRequirements().size() ==
1850-
reprocessedSig->getRequirements().size()) {
1851-
for (unsigned i : indices(sig->getRequirements())) {
1852-
auto sigReq = sig->getRequirements()[i];
1853-
auto reprocessedReq = reprocessedSig->getRequirements()[i];
1847+
if (sig.getGenericParams().size() ==
1848+
reprocessedSig.getGenericParams().size() &&
1849+
sig.getRequirements().size() ==
1850+
reprocessedSig.getRequirements().size()) {
1851+
for (unsigned i : indices(sig.getRequirements())) {
1852+
auto sigReq = sig.getRequirements()[i];
1853+
auto reprocessedReq = reprocessedSig.getRequirements()[i];
18541854
if (sigReq.getKind() != reprocessedReq.getKind()) {
18551855
llvm::errs() << "Requirement mismatch:\n";
18561856
llvm::errs() << " Original: ";
@@ -3608,12 +3608,12 @@ GenericTypeParamType *GenericTypeParamType::get(unsigned depth, unsigned index,
36083608

36093609
TypeArrayView<GenericTypeParamType>
36103610
GenericFunctionType::getGenericParams() const {
3611-
return Signature->getGenericParams();
3611+
return Signature.getGenericParams();
36123612
}
36133613

36143614
/// Retrieve the requirements of this polymorphic function type.
36153615
ArrayRef<Requirement> GenericFunctionType::getRequirements() const {
3616-
return Signature->getRequirements();
3616+
return Signature.getRequirements();
36173617
}
36183618

36193619
void SILFunctionType::Profile(
@@ -3747,7 +3747,7 @@ SILFunctionType::SILFunctionType(
37473747
"If all generic parameters are concrete, SILFunctionType should "
37483748
"not have a generic signature at all");
37493749

3750-
for (auto gparam : genericSig->getGenericParams()) {
3750+
for (auto gparam : genericSig.getGenericParams()) {
37513751
(void)gparam;
37523752
assert(gparam->isCanonical() && "generic signature is not canonicalized");
37533753
}
@@ -4123,7 +4123,7 @@ OpaqueTypeArchetypeType::get(OpaqueTypeDecl *Decl,
41234123
// Same-type-constrain the arguments in the outer signature to their
41244124
// replacements in the substitution map.
41254125
if (auto outerSig = Decl->getGenericSignature()) {
4126-
for (auto outerParam : outerSig->getGenericParams()) {
4126+
for (auto outerParam : outerSig.getGenericParams()) {
41274127
auto boundType = Type(outerParam).subst(Substitutions);
41284128
newRequirements.push_back(
41294129
Requirement(RequirementKind::SameType, Type(outerParam), boundType));
@@ -4138,7 +4138,7 @@ OpaqueTypeArchetypeType::get(OpaqueTypeDecl *Decl,
41384138
(void)newRequirements;
41394139
# ifndef NDEBUG
41404140
for (auto reqt :
4141-
Decl->getOpaqueInterfaceGenericSignature()->getRequirements()) {
4141+
Decl->getOpaqueInterfaceGenericSignature().getRequirements()) {
41424142
auto reqtBase = reqt.getFirstType()->getRootGenericParam();
41434143
if (reqtBase->isEqual(Decl->getUnderlyingInterfaceType())) {
41444144
assert(reqt.getKind() != RequirementKind::SameType
@@ -4261,7 +4261,7 @@ GenericEnvironment *OpenedArchetypeType::getGenericEnvironment() const {
42614261
// Create a generic environment to represent the opened type.
42624262
auto signature = ctx.getOpenedArchetypeSignature(Opened);
42634263
auto *env = GenericEnvironment::getIncomplete(signature);
4264-
env->addMapping(signature->getGenericParams()[0], thisType);
4264+
env->addMapping(signature.getGenericParams().front().getPointer(), thisType);
42654265
Environment = env;
42664266

42674267
return env;
@@ -4419,7 +4419,7 @@ GenericEnvironment *GenericEnvironment::getIncomplete(
44194419
auto &ctx = signature->getASTContext();
44204420

44214421
// Allocate and construct the new environment.
4422-
unsigned numGenericParams = signature->getGenericParams().size();
4422+
unsigned numGenericParams = signature.getGenericParams().size();
44234423
size_t bytes = totalSizeToAlloc<Type>(numGenericParams);
44244424
void *mem = ctx.Allocate(bytes, alignof(GenericEnvironment));
44254425
return new (mem) GenericEnvironment(signature);
@@ -4982,9 +4982,9 @@ ASTContext::getOverrideGenericSignature(const ValueDecl *base,
49824982
unsigned derivedDepth = 0;
49834983
unsigned baseDepth = 0;
49844984
if (derivedClassSig)
4985-
derivedDepth = derivedClassSig->getGenericParams().back()->getDepth() + 1;
4985+
derivedDepth = derivedClassSig.getGenericParams().back()->getDepth() + 1;
49864986
if (const auto baseClassSig = baseClass->getGenericSignature())
4987-
baseDepth = baseClassSig->getGenericParams().back()->getDepth() + 1;
4987+
baseDepth = baseClassSig.getGenericParams().back()->getDepth() + 1;
49884988

49894989
SmallVector<GenericTypeParamType *, 2> addedGenericParams;
49904990
if (const auto *gpList = derived->getAsGenericContext()->getGenericParams()) {
@@ -5018,7 +5018,7 @@ ASTContext::getOverrideGenericSignature(const ValueDecl *base,
50185018
};
50195019

50205020
SmallVector<Requirement, 2> addedRequirements;
5021-
for (auto reqt : baseGenericSig->getRequirements()) {
5021+
for (auto reqt : baseGenericSig.getRequirements()) {
50225022
if (auto substReqt = reqt.subst(substFn, lookupConformanceFn)) {
50235023
addedRequirements.push_back(*substReqt);
50245024
}
@@ -5116,7 +5116,7 @@ CanSILBoxType SILBoxType::get(ASTContext &C,
51165116
CanSILBoxType SILBoxType::get(CanType boxedType) {
51175117
auto &ctx = boxedType->getASTContext();
51185118
auto singleGenericParamSignature = ctx.getSingleGenericParameterSignature();
5119-
auto genericParam = singleGenericParamSignature->getGenericParams()[0];
5119+
auto genericParam = singleGenericParamSignature.getGenericParams()[0];
51205120
auto layout = SILLayout::get(ctx, singleGenericParamSignature,
51215121
SILField(CanType(genericParam),
51225122
/*mutable*/ true));

lib/AST/ASTDemangler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ createSubstitutionMapFromGenericArgs(GenericSignature genericSig,
205205
if (!genericSig)
206206
return SubstitutionMap();
207207

208-
if (genericSig->getGenericParams().size() != args.size())
208+
if (genericSig.getGenericParams().size() != args.size())
209209
return SubstitutionMap();
210210

211211
return SubstitutionMap::get(
@@ -306,7 +306,7 @@ Type ASTBuilder::createBoundGenericType(GenericTypeDecl *decl,
306306

307307
auto genericSig = aliasDecl->getGenericSignature();
308308
for (unsigned i = 0, e = args.size(); i < e; ++i) {
309-
auto origTy = genericSig->getInnermostGenericParams()[i];
309+
auto origTy = genericSig.getInnermostGenericParams()[i];
310310
auto substTy = args[i];
311311

312312
subs[origTy->getCanonicalType()->castTo<GenericTypeParamType>()] =

lib/AST/ASTDumper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3286,7 +3286,7 @@ static void dumpSubstitutionMapRec(
32863286
}
32873287

32883288
genericSig->print(out);
3289-
auto genericParams = genericSig->getGenericParams();
3289+
auto genericParams = genericSig.getGenericParams();
32903290
auto replacementTypes =
32913291
static_cast<const SubstitutionMap &>(map).getReplacementTypesBuffer();
32923292
for (unsigned i : indices(genericParams)) {
@@ -3315,7 +3315,7 @@ static void dumpSubstitutionMapRec(
33153315
return;
33163316

33173317
auto conformances = map.getConformances();
3318-
for (const auto &req : genericSig->getRequirements()) {
3318+
for (const auto &req : genericSig.getRequirements()) {
33193319
if (req.getKind() != RequirementKind::Conformance)
33203320
continue;
33213321

lib/AST/ASTMangler.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,7 +1498,7 @@ unsigned ASTMangler::appendBoundGenericArgs(DeclContext *dc,
14981498
// If we are generic at this level, emit all of the replacements at
14991499
// this level.
15001500
if (genericContext->isGeneric()) {
1501-
auto genericParams = subs.getGenericSignature()->getGenericParams();
1501+
auto genericParams = subs.getGenericSignature().getGenericParams();
15021502
unsigned depth = genericParams[currentGenericParamIdx]->getDepth();
15031503
auto replacements = subs.getReplacementTypes();
15041504
for (unsigned lastGenericParamIdx = genericParams.size();
@@ -2586,7 +2586,7 @@ bool ASTMangler::appendGenericSignature(GenericSignature sig,
25862586
CurGenericSignature = canSig;
25872587

25882588
unsigned initialParamDepth;
2589-
TypeArrayView<GenericTypeParamType> genericParams;
2589+
ArrayRef<CanTypeWrapper<GenericTypeParamType>> genericParams;
25902590
ArrayRef<Requirement> requirements;
25912591
SmallVector<Requirement, 4> requirementsBuffer;
25922592
if (contextSig) {
@@ -2597,12 +2597,12 @@ bool ASTMangler::appendGenericSignature(GenericSignature sig,
25972597
}
25982598

25992599
// The signature depth starts above the depth of the context signature.
2600-
if (!contextSig->getGenericParams().empty()) {
2601-
initialParamDepth = contextSig->getGenericParams().back()->getDepth() + 1;
2600+
if (!contextSig.getGenericParams().empty()) {
2601+
initialParamDepth = contextSig.getGenericParams().back()->getDepth() + 1;
26022602
}
26032603

26042604
// Find the parameters at this depth (or greater).
2605-
genericParams = canSig->getGenericParams();
2605+
genericParams = canSig.getGenericParams();
26062606
unsigned firstParam = genericParams.size();
26072607
while (firstParam > 1 &&
26082608
genericParams[firstParam-1]->getDepth() >= initialParamDepth)
@@ -2614,20 +2614,20 @@ bool ASTMangler::appendGenericSignature(GenericSignature sig,
26142614
// it's better to mangle the complete canonical signature because we
26152615
// have a special-case mangling for that.
26162616
if (genericParams.empty() &&
2617-
contextSig->getGenericParams().size() == 1 &&
2618-
contextSig->getRequirements().empty()) {
2617+
contextSig.getGenericParams().size() == 1 &&
2618+
contextSig.getRequirements().empty()) {
26192619
initialParamDepth = 0;
2620-
genericParams = canSig->getGenericParams();
2621-
requirements = canSig->getRequirements();
2620+
genericParams = canSig.getGenericParams();
2621+
requirements = canSig.getRequirements();
26222622
} else {
26232623
requirementsBuffer = canSig->requirementsNotSatisfiedBy(contextSig);
26242624
requirements = requirementsBuffer;
26252625
}
26262626
} else {
26272627
// Use the complete canonical signature.
26282628
initialParamDepth = 0;
2629-
genericParams = canSig->getGenericParams();
2630-
requirements = canSig->getRequirements();
2629+
genericParams = canSig.getGenericParams();
2630+
requirements = canSig.getRequirements();
26312631
}
26322632

26332633
if (genericParams.empty() && requirements.empty())
@@ -2708,7 +2708,7 @@ void ASTMangler::appendRequirement(const Requirement &reqt) {
27082708
}
27092709

27102710
void ASTMangler::appendGenericSignatureParts(
2711-
TypeArrayView<GenericTypeParamType> params,
2711+
ArrayRef<CanTypeWrapper<GenericTypeParamType>> params,
27122712
unsigned initialParamDepth,
27132713
ArrayRef<Requirement> requirements) {
27142714
// Mangle the requirements.
@@ -3127,7 +3127,7 @@ void ASTMangler::appendDependentProtocolConformance(
31273127
appendProtocolName(entry.second);
31283128
auto index =
31293129
conformanceRequirementIndex(entry,
3130-
CurGenericSignature->getRequirements());
3130+
CurGenericSignature.getRequirements());
31313131
// This is never an unknown index and so must be adjusted by 2 per ABI.
31323132
appendOperator("HD", Index(index + 2));
31333133
continue;
@@ -3173,7 +3173,7 @@ void ASTMangler::appendAnyProtocolConformance(
31733173
appendDependentProtocolConformance(path);
31743174
} else if (auto opaqueType = conformingType->getAs<OpaqueTypeArchetypeType>()) {
31753175
GenericSignature opaqueSignature = opaqueType->getBoundSignature();
3176-
GenericTypeParamType *opaqueTypeParam = opaqueSignature->getGenericParams().back();
3176+
GenericTypeParamType *opaqueTypeParam = opaqueSignature.getGenericParams().back();
31773177
ConformanceAccessPath conformanceAccessPath =
31783178
opaqueSignature->getConformanceAccessPath(opaqueTypeParam,
31793179
conformance.getAbstract());

lib/AST/ASTPrinter.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,7 +1506,7 @@ static unsigned getDepthOfRequirement(const Requirement &req) {
15061506
static void getRequirementsAtDepth(GenericSignature genericSig,
15071507
unsigned depth,
15081508
SmallVectorImpl<Requirement> &result) {
1509-
for (auto reqt : genericSig->getRequirements()) {
1509+
for (auto reqt : genericSig.getRequirements()) {
15101510
unsigned currentDepth = getDepthOfRequirement(reqt);
15111511
assert(currentDepth != ErrorDepth);
15121512
if (currentDepth == depth)
@@ -1524,17 +1524,17 @@ void PrintAST::printGenericSignature(GenericSignature genericSig,
15241524
void PrintAST::printGenericSignature(
15251525
GenericSignature genericSig, unsigned flags,
15261526
llvm::function_ref<bool(const Requirement &)> filter) {
1527-
auto requirements = genericSig->getRequirements();
1527+
auto requirements = genericSig.getRequirements();
15281528

15291529
if (flags & InnermostOnly) {
1530-
auto genericParams = genericSig->getInnermostGenericParams();
1530+
auto genericParams = genericSig.getInnermostGenericParams();
15311531

15321532
printSingleDepthOfGenericSignature(genericParams, requirements, flags,
15331533
filter);
15341534
return;
15351535
}
15361536

1537-
auto genericParams = genericSig->getGenericParams();
1537+
auto genericParams = genericSig.getGenericParams();
15381538

15391539
if (!Options.PrintInSILBody) {
15401540
printSingleDepthOfGenericSignature(genericParams, requirements, flags,
@@ -2349,8 +2349,8 @@ void PrintAST::printSynthesizedExtension(Type ExtendedType,
23492349

23502350
auto printRequirementsFrom = [&](ExtensionDecl *ED, bool &IsFirst) {
23512351
auto Sig = ED->getGenericSignature();
2352-
printSingleDepthOfGenericSignature(Sig->getGenericParams(),
2353-
Sig->getRequirements(),
2352+
printSingleDepthOfGenericSignature(Sig.getGenericParams(),
2353+
Sig.getRequirements(),
23542354
IsFirst, PrintRequirements,
23552355
[](const Requirement &Req){
23562356
return true;
@@ -2619,7 +2619,7 @@ static bool usesFeatureRethrowsProtocol(
26192619

26202620
if (auto genericSig = decl->getInnermostDeclContext()
26212621
->getGenericSignatureOfContext()) {
2622-
for (const auto &req : genericSig->getRequirements()) {
2622+
for (const auto &req : genericSig.getRequirements()) {
26232623
if (req.getKind() == RequirementKind::Conformance &&
26242624
usesFeatureRethrowsProtocol(req.getProtocolDecl(), checked))
26252625
return true;

0 commit comments

Comments
 (0)