Skip to content

Commit 03d8690

Browse files
authored
Merge pull request #38403 from CodaFi/sigarillo
Lift Requirement and Parameter Accessors up to GenericSignature
2 parents 5248388 + 1329f3c commit 03d8690

File tree

83 files changed

+409
-463
lines changed

Some content is hidden

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

83 files changed

+409
-463
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/AutoDiff.h

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -727,10 +727,7 @@ template <> struct DenseMapInfo<AutoDiffConfig> {
727727
}
728728

729729
static unsigned getHashValue(const AutoDiffConfig &Val) {
730-
auto canGenSig =
731-
Val.derivativeGenericSignature
732-
? Val.derivativeGenericSignature->getCanonicalSignature()
733-
: nullptr;
730+
auto canGenSig = Val.derivativeGenericSignature.getCanonicalSignature();
734731
unsigned combinedHash = hash_combine(
735732
~1U, DenseMapInfo<void *>::getHashValue(Val.parameterIndices),
736733
DenseMapInfo<void *>::getHashValue(Val.resultIndices),
@@ -739,14 +736,8 @@ template <> struct DenseMapInfo<AutoDiffConfig> {
739736
}
740737

741738
static bool isEqual(const AutoDiffConfig &LHS, const AutoDiffConfig &RHS) {
742-
auto lhsCanGenSig =
743-
LHS.derivativeGenericSignature
744-
? LHS.derivativeGenericSignature->getCanonicalSignature()
745-
: nullptr;
746-
auto rhsCanGenSig =
747-
RHS.derivativeGenericSignature
748-
? RHS.derivativeGenericSignature->getCanonicalSignature()
749-
: nullptr;
739+
auto lhsCanGenSig = LHS.derivativeGenericSignature.getCanonicalSignature();
740+
auto rhsCanGenSig = RHS.derivativeGenericSignature.getCanonicalSignature();
750741
return LHS.parameterIndices == RHS.parameterIndices &&
751742
LHS.resultIndices == RHS.resultIndices &&
752743
DenseMapInfo<GenericSignature>::isEqual(lhsCanGenSig, rhsCanGenSig);

include/swift/AST/GenericSignature.h

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,27 @@ 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;
198+
199+
/// Whether this generic signature involves a type variable.
200+
bool hasTypeVariable() const;
201+
202+
/// Returns the generic environment that provides fresh contextual types
203+
/// (archetypes) that correspond to the interface types in this generic
204+
/// signature.
205+
GenericEnvironment *getGenericEnvironment() const;
185206
};
186207

187208
/// A reference to a canonical generic signature.
@@ -255,23 +276,6 @@ class alignas(1 << TypeAlignInBits) GenericSignatureImpl final
255276
friend class ArchetypeType;
256277

257278
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-
275279
/// Only allow allocation by doing a placement new.
276280
void *operator new(size_t Bytes, void *Mem) {
277281
assert(Mem);
@@ -312,20 +316,12 @@ class alignas(1 << TypeAlignInBits) GenericSignatureImpl final
312316

313317
ASTContext &getASTContext() const;
314318

315-
/// Returns the canonical generic signature. The result is cached.
316-
CanGenericSignature getCanonicalSignature() const;
317-
318319
/// Retrieve the generic signature builder for the given generic signature.
319320
GenericSignatureBuilder *getGenericSignatureBuilder() const;
320321

321322
/// Retrieve the requirement machine for the given generic signature.
322323
RequirementMachine *getRequirementMachine() const;
323324

324-
/// Returns the generic environment that provides fresh contextual types
325-
/// (archetypes) that correspond to the interface types in this generic
326-
/// signature.
327-
GenericEnvironment *getGenericEnvironment() const;
328-
329325
/// Collects a set of requirements on a type parameter. Used by
330326
/// GenericEnvironment for building archetypes.
331327
GenericSignature::LocalRequirements getLocalRequirements(Type depType) const;
@@ -428,9 +424,6 @@ class alignas(1 << TypeAlignInBits) GenericSignatureImpl final
428424
/// generic parameter types by their sugared form.
429425
Type getSugaredType(Type type) const;
430426

431-
/// Whether this generic signature involves a type variable.
432-
bool hasTypeVariable() const;
433-
434427
static void Profile(llvm::FoldingSetNodeID &ID,
435428
TypeArrayView<GenericTypeParamType> genericParams,
436429
ArrayRef<Requirement> requirements);
@@ -439,6 +432,35 @@ class alignas(1 << TypeAlignInBits) GenericSignatureImpl final
439432
void print(ASTPrinter &Printer, PrintOptions Opts = PrintOptions()) const;
440433
SWIFT_DEBUG_DUMP;
441434
std::string getAsString() const;
435+
436+
private:
437+
friend GenericSignature;
438+
friend CanGenericSignature;
439+
440+
/// Retrieve the generic parameters.
441+
TypeArrayView<GenericTypeParamType> getGenericParams() const {
442+
return TypeArrayView<GenericTypeParamType>(
443+
{getTrailingObjects<Type>(), NumGenericParams});
444+
}
445+
446+
/// Retrieve the innermost generic parameters.
447+
///
448+
/// Given a generic signature for a nested generic type, produce an
449+
/// array of the generic parameters for the innermost generic type.
450+
TypeArrayView<GenericTypeParamType> getInnermostGenericParams() const;
451+
452+
/// Retrieve the requirements.
453+
ArrayRef<Requirement> getRequirements() const {
454+
return {getTrailingObjects<Requirement>(), NumRequirements};
455+
}
456+
457+
/// Returns the canonical generic signature. The result is cached.
458+
CanGenericSignature getCanonicalSignature() const;
459+
460+
/// Returns the generic environment that provides fresh contextual types
461+
/// (archetypes) that correspond to the interface types in this generic
462+
/// signature.
463+
GenericEnvironment *getGenericEnvironment() const;
442464
};
443465

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

lib/AST/ASTContext.cpp

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -913,8 +913,7 @@ static VarDecl *getPointeeProperty(VarDecl *&cache,
913913
NominalTypeDecl *nominal = (ctx.*getNominal)();
914914
if (!nominal) return nullptr;
915915
auto sig = nominal->getGenericSignature();
916-
if (!sig) return nullptr;
917-
if (sig->getGenericParams().size() != 1) return nullptr;
916+
if (sig.getGenericParams().size() != 1) return nullptr;
918917

919918
// There must be a property named "pointee".
920919
auto identifier = ctx.getIdentifier("pointee");
@@ -924,7 +923,7 @@ static VarDecl *getPointeeProperty(VarDecl *&cache,
924923
// The property must have type T.
925924
auto *property = dyn_cast<VarDecl>(results[0]);
926925
if (!property) return nullptr;
927-
if (!property->getInterfaceType()->isEqual(sig->getGenericParams()[0]))
926+
if (!property->getInterfaceType()->isEqual(sig.getGenericParams()[0]))
928927
return nullptr;
929928

930929
cache = property;
@@ -1776,8 +1775,8 @@ static AllocationArena getArena(GenericSignature genericSig) {
17761775
if (!genericSig)
17771776
return AllocationArena::Permanent;
17781777

1779-
if (genericSig->hasTypeVariable()) {
1780-
assert(false && "What's going on");
1778+
if (genericSig.hasTypeVariable()) {
1779+
assert(false && "Unsubstituted type variable leaked into generic signature");
17811780
return AllocationArena::ConstraintSolver;
17821781
}
17831782

@@ -1844,13 +1843,13 @@ GenericSignatureBuilder *ASTContext::getOrCreateGenericSignatureBuilder(
18441843
reprocessedSig->print(llvm::errs());
18451844
llvm::errs() << "\n";
18461845

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];
1846+
if (sig.getGenericParams().size() ==
1847+
reprocessedSig.getGenericParams().size() &&
1848+
sig.getRequirements().size() ==
1849+
reprocessedSig.getRequirements().size()) {
1850+
for (unsigned i : indices(sig.getRequirements())) {
1851+
auto sigReq = sig.getRequirements()[i];
1852+
auto reprocessedReq = reprocessedSig.getRequirements()[i];
18541853
if (sigReq.getKind() != reprocessedReq.getKind()) {
18551854
llvm::errs() << "Requirement mismatch:\n";
18561855
llvm::errs() << " Original: ";
@@ -1895,7 +1894,7 @@ GenericSignatureBuilder *ASTContext::getOrCreateGenericSignatureBuilder(
18951894

18961895
RequirementMachine *ASTContext::getOrCreateRequirementMachine(
18971896
CanGenericSignature sig) {
1898-
assert(!sig->hasTypeVariable());
1897+
assert(!sig.hasTypeVariable());
18991898

19001899
auto &rewriteCtx = getImpl().TheRewriteContext;
19011900
if (!rewriteCtx)
@@ -3608,12 +3607,12 @@ GenericTypeParamType *GenericTypeParamType::get(unsigned depth, unsigned index,
36083607

36093608
TypeArrayView<GenericTypeParamType>
36103609
GenericFunctionType::getGenericParams() const {
3611-
return Signature->getGenericParams();
3610+
return Signature.getGenericParams();
36123611
}
36133612

36143613
/// Retrieve the requirements of this polymorphic function type.
36153614
ArrayRef<Requirement> GenericFunctionType::getRequirements() const {
3616-
return Signature->getRequirements();
3615+
return Signature.getRequirements();
36173616
}
36183617

36193618
void SILFunctionType::Profile(
@@ -3747,7 +3746,7 @@ SILFunctionType::SILFunctionType(
37473746
"If all generic parameters are concrete, SILFunctionType should "
37483747
"not have a generic signature at all");
37493748

3750-
for (auto gparam : genericSig->getGenericParams()) {
3749+
for (auto gparam : genericSig.getGenericParams()) {
37513750
(void)gparam;
37523751
assert(gparam->isCanonical() && "generic signature is not canonicalized");
37533752
}
@@ -4123,7 +4122,7 @@ OpaqueTypeArchetypeType::get(OpaqueTypeDecl *Decl,
41234122
// Same-type-constrain the arguments in the outer signature to their
41244123
// replacements in the substitution map.
41254124
if (auto outerSig = Decl->getGenericSignature()) {
4126-
for (auto outerParam : outerSig->getGenericParams()) {
4125+
for (auto outerParam : outerSig.getGenericParams()) {
41274126
auto boundType = Type(outerParam).subst(Substitutions);
41284127
newRequirements.push_back(
41294128
Requirement(RequirementKind::SameType, Type(outerParam), boundType));
@@ -4138,7 +4137,7 @@ OpaqueTypeArchetypeType::get(OpaqueTypeDecl *Decl,
41384137
(void)newRequirements;
41394138
# ifndef NDEBUG
41404139
for (auto reqt :
4141-
Decl->getOpaqueInterfaceGenericSignature()->getRequirements()) {
4140+
Decl->getOpaqueInterfaceGenericSignature().getRequirements()) {
41424141
auto reqtBase = reqt.getFirstType()->getRootGenericParam();
41434142
if (reqtBase->isEqual(Decl->getUnderlyingInterfaceType())) {
41444143
assert(reqt.getKind() != RequirementKind::SameType
@@ -4261,7 +4260,7 @@ GenericEnvironment *OpenedArchetypeType::getGenericEnvironment() const {
42614260
// Create a generic environment to represent the opened type.
42624261
auto signature = ctx.getOpenedArchetypeSignature(Opened);
42634262
auto *env = GenericEnvironment::getIncomplete(signature);
4264-
env->addMapping(signature->getGenericParams()[0], thisType);
4263+
env->addMapping(signature.getGenericParams().front().getPointer(), thisType);
42654264
Environment = env;
42664265

42674266
return env;
@@ -4419,7 +4418,7 @@ GenericEnvironment *GenericEnvironment::getIncomplete(
44194418
auto &ctx = signature->getASTContext();
44204419

44214420
// Allocate and construct the new environment.
4422-
unsigned numGenericParams = signature->getGenericParams().size();
4421+
unsigned numGenericParams = signature.getGenericParams().size();
44234422
size_t bytes = totalSizeToAlloc<Type>(numGenericParams);
44244423
void *mem = ctx.Allocate(bytes, alignof(GenericEnvironment));
44254424
return new (mem) GenericEnvironment(signature);
@@ -4913,7 +4912,7 @@ CanGenericSignature ASTContext::getOpenedArchetypeSignature(Type type) {
49134912
// The opened archetype signature for a protocol type is identical
49144913
// to the protocol's own canonical generic signature.
49154914
if (const auto protoTy = dyn_cast<ProtocolType>(existential)) {
4916-
return protoTy->getDecl()->getGenericSignature()->getCanonicalSignature();
4915+
return protoTy->getDecl()->getGenericSignature().getCanonicalSignature();
49174916
}
49184917

49194918
auto found = getImpl().ExistentialSignatures.find(existential);
@@ -4982,9 +4981,9 @@ ASTContext::getOverrideGenericSignature(const ValueDecl *base,
49824981
unsigned derivedDepth = 0;
49834982
unsigned baseDepth = 0;
49844983
if (derivedClassSig)
4985-
derivedDepth = derivedClassSig->getGenericParams().back()->getDepth() + 1;
4984+
derivedDepth = derivedClassSig.getGenericParams().back()->getDepth() + 1;
49864985
if (const auto baseClassSig = baseClass->getGenericSignature())
4987-
baseDepth = baseClassSig->getGenericParams().back()->getDepth() + 1;
4986+
baseDepth = baseClassSig.getGenericParams().back()->getDepth() + 1;
49884987

49894988
SmallVector<GenericTypeParamType *, 2> addedGenericParams;
49904989
if (const auto *gpList = derived->getAsGenericContext()->getGenericParams()) {
@@ -5018,7 +5017,7 @@ ASTContext::getOverrideGenericSignature(const ValueDecl *base,
50185017
};
50195018

50205019
SmallVector<Requirement, 2> addedRequirements;
5021-
for (auto reqt : baseGenericSig->getRequirements()) {
5020+
for (auto reqt : baseGenericSig.getRequirements()) {
50225021
if (auto substReqt = reqt.subst(substFn, lookupConformanceFn)) {
50235022
addedRequirements.push_back(*substReqt);
50245023
}
@@ -5116,7 +5115,7 @@ CanSILBoxType SILBoxType::get(ASTContext &C,
51165115
CanSILBoxType SILBoxType::get(CanType boxedType) {
51175116
auto &ctx = boxedType->getASTContext();
51185117
auto singleGenericParamSignature = ctx.getSingleGenericParameterSignature();
5119-
auto genericParam = singleGenericParamSignature->getGenericParams()[0];
5118+
auto genericParam = singleGenericParamSignature.getGenericParams()[0];
51205119
auto layout = SILLayout::get(ctx, singleGenericParamSignature,
51215120
SILField(CanType(genericParam),
51225121
/*mutable*/ true));
@@ -5274,9 +5273,7 @@ AutoDiffDerivativeFunctionIdentifier *AutoDiffDerivativeFunctionIdentifier::get(
52745273
llvm::FoldingSetNodeID id;
52755274
id.AddInteger((unsigned)kind);
52765275
id.AddPointer(parameterIndices);
5277-
CanGenericSignature derivativeCanGenSig;
5278-
if (derivativeGenericSignature)
5279-
derivativeCanGenSig = derivativeGenericSignature->getCanonicalSignature();
5276+
auto derivativeCanGenSig = derivativeGenericSignature.getCanonicalSignature();
52805277
id.AddPointer(derivativeCanGenSig.getPointer());
52815278

52825279
void *insertPos;

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

0 commit comments

Comments
 (0)