Skip to content

Commit 888a32b

Browse files
authored
Merge pull request #4758 from slavapestov/small-generics-cleanups
Small generics cleanups
2 parents 28f25ff + 960bbc3 commit 888a32b

23 files changed

+192
-259
lines changed

include/swift/AST/ArchetypeBuilder.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,10 +346,6 @@ class ArchetypeBuilder {
346346
GenericEnvironment *genericEnv,
347347
Type type);
348348

349-
using SameTypeRequirement
350-
= std::pair<PotentialArchetype *,
351-
PointerUnion<Type, PotentialArchetype*>>;
352-
353349
/// \brief Dump all of the requirements, both specified and inferred.
354350
LLVM_ATTRIBUTE_DEPRECATED(
355351
void dump(),

include/swift/AST/GenericSignature.h

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,27 @@ class SubstitutionMap;
3535
/// signature and their dependent members.
3636
class GenericSignatureWitnessIterator {
3737
ArrayRef<Requirement> p;
38-
38+
39+
void checkValid() const {
40+
assert(!p.empty() &&
41+
p.front().getKind() == RequirementKind::WitnessMarker);
42+
}
43+
44+
bool shouldSkip() const {
45+
return (!p.empty() &&
46+
p.front().getKind() != RequirementKind::WitnessMarker);
47+
}
48+
3949
public:
4050
GenericSignatureWitnessIterator() = default;
41-
GenericSignatureWitnessIterator(ArrayRef<Requirement> p)
42-
: p(p)
43-
{
44-
assert(p.empty() || p.front().getKind() == RequirementKind::WitnessMarker);
51+
GenericSignatureWitnessIterator(ArrayRef<Requirement> requirements)
52+
: p(requirements) {
53+
while (shouldSkip()) { p = p.slice(1); }
4554
}
4655

4756
GenericSignatureWitnessIterator &operator++() {
48-
do {
49-
p = p.slice(1);
50-
} while (!p.empty()
51-
&& p.front().getKind() != RequirementKind::WitnessMarker);
57+
checkValid();
58+
do { p = p.slice(1); } while (shouldSkip());
5259
return *this;
5360
}
5461

@@ -59,12 +66,12 @@ class GenericSignatureWitnessIterator {
5966
}
6067

6168
Type operator*() const {
62-
assert(p.front().getKind() == RequirementKind::WitnessMarker);
69+
checkValid();
6370
return p.front().getFirstType();
6471
}
6572

6673
Type operator->() const {
67-
assert(p.front().getKind() == RequirementKind::WitnessMarker);
74+
checkValid();
6875
return p.front().getFirstType();
6976
}
7077

@@ -162,7 +169,14 @@ class GenericSignature final : public llvm::FoldingSetNode,
162169
return const_cast<GenericSignature *>(this)->getRequirementsBuffer();
163170
}
164171

165-
// Only allow allocation by doing a placement new.
172+
/// Check if the generic signature makes all generic parameters
173+
/// concrete.
174+
bool areAllParamsConcrete() const {
175+
auto iter = getAllDependentTypes();
176+
return iter.begin() == iter.end();
177+
}
178+
179+
/// Only allow allocation by doing a placement new.
166180
void *operator new(size_t Bytes, void *Mem) {
167181
assert(Mem);
168182
return Mem;

include/swift/AST/Substitution.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,6 @@ class Substitution {
5757

5858
/// Apply a substitution to this substitution's replacement type and
5959
/// conformances.
60-
///
61-
/// Our replacement type must be written in terms of the context
62-
/// archetypes of 'env', which in turn must be derived from the
63-
/// generic requirements of 'sig'.
64-
Substitution subst(ModuleDecl *module,
65-
GenericSignature *sig,
66-
GenericEnvironment *env,
67-
ArrayRef<Substitution> subs) const;
68-
6960
Substitution subst(ModuleDecl *module,
7061
const SubstitutionMap &subMap) const;
7162

include/swift/AST/Types.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3757,17 +3757,6 @@ class ArchetypeType final : public SubstitutableType,
37573757
return AssocTypeOrProto.dyn_cast<ProtocolDecl *>();
37583758
}
37593759

3760-
/// True if this is the 'Self' parameter of a protocol or an associated type
3761-
/// of 'Self'.
3762-
bool isSelfDerived() {
3763-
ArchetypeType *t = getPrimary();
3764-
3765-
if (t && t->getSelfProtocol())
3766-
return true;
3767-
3768-
return false;
3769-
}
3770-
37713760
/// getConformsTo - Retrieve the set of protocols to which this substitutable
37723761
/// type shall conform.
37733762
ArrayRef<ProtocolDecl *> getConformsTo() const { return ConformsTo; }

include/swift/SIL/TypeSubstCloner.h

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#ifndef SWIFT_SIL_TYPESUBSTCLONER_H
1919
#define SWIFT_SIL_TYPESUBSTCLONER_H
2020

21+
#include "swift/AST/GenericEnvironment.h"
2122
#include "swift/AST/Type.h"
2223
#include "swift/SIL/SILCloner.h"
2324
#include "swift/SIL/DynamicCasts.h"
@@ -38,6 +39,13 @@ class TypeSubstCloner : public SILClonerWithScopes<ImplClass> {
3839
llvm_unreachable("Clients need to explicitly call a base class impl!");
3940
}
4041

42+
void computeSubsMap() {
43+
if (auto *env = Original.getGenericEnvironment()) {
44+
auto sig = Original.getLoweredFunctionType()->getGenericSignature();
45+
SubsMap = env->getSubstitutionMap(SwiftMod, sig, ApplySubs);
46+
}
47+
}
48+
4149
public:
4250
using SILClonerWithScopes<ImplClass>::asImpl;
4351
using SILClonerWithScopes<ImplClass>::getBuilder;
@@ -55,28 +63,28 @@ class TypeSubstCloner : public SILClonerWithScopes<ImplClass> {
5563

5664
TypeSubstCloner(SILFunction &To,
5765
SILFunction &From,
58-
const SubstitutionMap &ContextSubs,
5966
ArrayRef<Substitution> ApplySubs,
6067
SILOpenedArchetypesTracker &OpenedArchetypesTracker,
6168
bool Inlining = false)
6269
: SILClonerWithScopes<ImplClass>(To, OpenedArchetypesTracker, Inlining),
6370
SwiftMod(From.getModule().getSwiftModule()),
64-
SubsMap(ContextSubs),
6571
Original(From),
6672
ApplySubs(ApplySubs),
67-
Inlining(Inlining) { }
73+
Inlining(Inlining) {
74+
computeSubsMap();
75+
}
6876

6977
TypeSubstCloner(SILFunction &To,
7078
SILFunction &From,
71-
const SubstitutionMap &ContextSubs,
7279
ArrayRef<Substitution> ApplySubs,
7380
bool Inlining = false)
7481
: SILClonerWithScopes<ImplClass>(To, Inlining),
7582
SwiftMod(From.getModule().getSwiftModule()),
76-
SubsMap(ContextSubs),
7783
Original(From),
7884
ApplySubs(ApplySubs),
79-
Inlining(Inlining) { }
85+
Inlining(Inlining) {
86+
computeSubsMap();
87+
}
8088

8189

8290
protected:
@@ -90,11 +98,8 @@ class TypeSubstCloner : public SILClonerWithScopes<ImplClass> {
9098
}
9199

92100
Substitution remapSubstitution(Substitution sub) {
93-
if (!ApplySubs.empty()) {
94-
auto sig = Original.getLoweredFunctionType()->getGenericSignature();
95-
auto *env = Original.getGenericEnvironment();
96-
sub = sub.subst(SwiftMod, sig, env, ApplySubs);
97-
}
101+
sub = sub.subst(SwiftMod, SubsMap);
102+
98103
// Remap opened archetypes into the cloned context.
99104
return Substitution(getASTTypeInClonedContext(sub.getReplacement()
100105
->getCanonicalType()),
@@ -208,12 +213,7 @@ class TypeSubstCloner : public SILClonerWithScopes<ImplClass> {
208213
void visitWitnessMethodInst(WitnessMethodInst *Inst) {
209214
// Specialize the Self substitution of the witness_method.
210215
auto sub = Inst->getSelfSubstitution();
211-
if (!ApplySubs.empty()) {
212-
auto sig = Original.getLoweredFunctionType()->getGenericSignature();
213-
auto *env = Original.getGenericEnvironment();
214-
sub = sub.subst(Inst->getModule().getSwiftModule(),
215-
sig, env, ApplySubs);
216-
}
216+
sub = sub.subst(Inst->getModule().getSwiftModule(), SubsMap);
217217

218218
assert(sub.getConformances().size() == 1 &&
219219
"didn't get conformance from substitution?!");
@@ -289,7 +289,7 @@ class TypeSubstCloner : public SILClonerWithScopes<ImplClass> {
289289
/// The Swift module that the cloned function belongs to.
290290
Module *SwiftMod;
291291
/// The substitutions list for the specialization.
292-
const SubstitutionMap &SubsMap;
292+
SubstitutionMap SubsMap;
293293
/// The original function to specialize.
294294
SILFunction &Original;
295295
/// The substitutions used at the call site.

include/swift/SILOptimizer/Utils/GenericCloner.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,10 @@ class GenericCloner : public TypeSubstCloner<GenericCloner> {
4141
GenericCloner(SILFunction *F,
4242
IsFragile_t Fragile,
4343
const ReabstractionInfo &ReInfo,
44-
SubstitutionMap &ContextSubs,
4544
ArrayRef<Substitution> ParamSubs,
4645
StringRef NewName,
4746
CloneCollector::CallbackType Callback)
48-
: TypeSubstCloner(*initCloned(F, Fragile, ReInfo, NewName), *F, ContextSubs,
47+
: TypeSubstCloner(*initCloned(F, Fragile, ReInfo, NewName), *F,
4948
ParamSubs), ReInfo(ReInfo), Callback(Callback) {
5049
assert(F->getDebugScope()->Parent != getCloned()->getDebugScope()->Parent);
5150
}
@@ -56,12 +55,11 @@ class GenericCloner : public TypeSubstCloner<GenericCloner> {
5655
cloneFunction(SILFunction *F,
5756
IsFragile_t Fragile,
5857
const ReabstractionInfo &ReInfo,
59-
SubstitutionMap &ContextSubs,
6058
ArrayRef<Substitution> ParamSubs,
6159
StringRef NewName,
6260
CloneCollector::CallbackType Callback =nullptr) {
6361
// Clone and specialize the function.
64-
GenericCloner SC(F, Fragile, ReInfo, ContextSubs, ParamSubs,
62+
GenericCloner SC(F, Fragile, ReInfo, ParamSubs,
6563
NewName, Callback);
6664
SC.populateCloned();
6765
SC.cleanUp(SC.getCloned());

include/swift/SILOptimizer/Utils/SILInliner.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ class SILInliner : public TypeSubstCloner<SILInliner> {
4646
};
4747

4848
SILInliner(SILFunction &To, SILFunction &From, InlineKind IKind,
49-
SubstitutionMap &ContextSubs, ArrayRef<Substitution> ApplySubs,
49+
ArrayRef<Substitution> ApplySubs,
5050
SILOpenedArchetypesTracker &OpenedArchetypesTracker,
5151
CloneCollector::CallbackType Callback = nullptr)
52-
: TypeSubstCloner<SILInliner>(To, From, ContextSubs, ApplySubs,
52+
: TypeSubstCloner<SILInliner>(To, From, ApplySubs,
5353
OpenedArchetypesTracker, true),
5454
IKind(IKind), CalleeEntryBB(nullptr), CallSiteScope(nullptr),
5555
Callback(Callback) {

lib/AST/ASTScope.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,6 +1439,13 @@ SourceRange ASTScope::getSourceRangeImpl() const {
14391439
return cast<NominalTypeDecl>(iterableDeclContext)->getBraces();
14401440

14411441
case ASTScopeKind::GenericParams:
1442+
// A protocol's generic parameter list is not written in source, and
1443+
// is visible from the start of the body.
1444+
if (auto *protoDecl = dyn_cast<ProtocolDecl>(genericParams.decl)) {
1445+
return SourceRange(protoDecl->getBraces().Start,
1446+
protoDecl->getEndLoc());
1447+
}
1448+
14421449
// Explicitly-written generic parameters are in scope following their
14431450
// definition.
14441451
return SourceRange(genericParams.params->getParams()[genericParams.index]

lib/AST/Decl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2185,7 +2185,7 @@ GenericTypeDecl::GenericTypeDecl(DeclKind K, DeclContext *DC,
21852185

21862186

21872187
void GenericTypeDecl::setGenericParams(GenericParamList *params) {
2188-
// Set the specified generic parameters onto this type alias, setting
2188+
// Set the specified generic parameters onto this type declaration, setting
21892189
// the parameters' context along the way.
21902190
GenericParams = params;
21912191
if (params)

lib/AST/Substitution.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,6 @@ Substitution::Substitution(Type Replacement,
4242
&& "cannot substitute with a non-materializable type");
4343
}
4444

45-
Substitution Substitution::subst(Module *module,
46-
GenericSignature *sig,
47-
GenericEnvironment *env,
48-
ArrayRef<Substitution> subs) const {
49-
assert(sig && env);
50-
auto subMap = env->getSubstitutionMap(module, sig, subs);
51-
return subst(module, subMap);
52-
}
53-
5445
Substitution Substitution::subst(Module *module,
5546
const SubstitutionMap &subMap) const {
5647
// Substitute the replacement.

lib/AST/Type.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2816,19 +2816,10 @@ static Type getMemberForBaseType(ConformanceSource conformances,
28162816
if (archetypeParent->hasNestedType(name))
28172817
return archetypeParent->getNestedTypeValue(name);
28182818

2819-
if (auto parent = archetypeParent->getParent()) {
2820-
// If the archetype doesn't have the requested type and the parent is not
2821-
// self derived, error out
2822-
return parent->isSelfDerived() ? parent->getNestedTypeValue(name)
2823-
: ErrorType::get(substBase->getASTContext());
2824-
}
2825-
28262819
// If looking for an associated type and the archetype is constrained to a
28272820
// class, continue to the default associated type lookup
2828-
if (!assocType || !archetypeParent->getSuperclass()) {
2829-
// else just error out
2821+
if (!assocType || !archetypeParent->getSuperclass())
28302822
return ErrorType::get(substBase->getASTContext());
2831-
}
28322823
}
28332824

28342825
// If the parent is a type variable, retrieve its member type

0 commit comments

Comments
 (0)