Skip to content

Commit b0208a1

Browse files
committed
AST: Move getSugaredType() from GenericEnvironment to GenericSignature
None of this actually involves archetypes, so it can just be an operation on the GenericSignature itself.
1 parent ce550f2 commit b0208a1

File tree

9 files changed

+74
-71
lines changed

9 files changed

+74
-71
lines changed

include/swift/AST/GenericEnvironment.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,6 @@ class alignas(1 << DeclAlignInBits) GenericEnvironment final
149149
std::pair<Type, ProtocolConformanceRef>
150150
mapConformanceRefIntoContext(Type conformingType,
151151
ProtocolConformanceRef conformance) const;
152-
153-
/// Get the sugared form of a generic parameter type.
154-
GenericTypeParamType *getSugaredType(GenericTypeParamType *type) const;
155-
156-
/// Get the sugared form of a type by substituting any
157-
/// generic parameter types by their sugared form.
158-
Type getSugaredType(Type type) const;
159152

160153
SubstitutionMap getForwardingSubstitutionMap() const;
161154

include/swift/AST/GenericSignature.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,11 +397,18 @@ class alignas(1 << TypeAlignInBits) GenericSignatureImpl final
397397
/// <t_0_0, t_0_1, t_1_0>
398398
/// then this will return 0 for t_0_0, 1 for t_0_1, and 2 for t_1_0.
399399
unsigned getGenericParamOrdinal(GenericTypeParamType *param) const;
400-
400+
401401
/// Get a substitution map that maps all of the generic signature's
402402
/// generic parameters to themselves.
403403
SubstitutionMap getIdentitySubstitutionMap() const;
404404

405+
/// Get the sugared form of a generic parameter type.
406+
GenericTypeParamType *getSugaredType(GenericTypeParamType *type) const;
407+
408+
/// Get the sugared form of a type by substituting any
409+
/// generic parameter types by their sugared form.
410+
Type getSugaredType(Type type) const;
411+
405412
/// Whether this generic signature involves a type variable.
406413
bool hasTypeVariable() const;
407414

include/swift/AST/PrintOptions.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
namespace swift {
2626
class ASTPrinter;
27-
class GenericEnvironment;
27+
class GenericSignatureImpl;
2828
class CanType;
2929
class Decl;
3030
class Pattern;
@@ -423,8 +423,8 @@ struct PrintOptions {
423423
/// Replaces the name of private and internal properties of types with '_'.
424424
bool OmitNameOfInaccessibleProperties = false;
425425

426-
/// Print dependent types as references into this generic environment.
427-
GenericEnvironment *GenericEnv = nullptr;
426+
/// Use this signature to re-sugar dependent types.
427+
const GenericSignatureImpl *GenericSig = nullptr;
428428

429429
/// Print types with alternative names from their canonical names.
430430
llvm::DenseMap<CanType, Identifier> *AlternativeTypeNames = nullptr;

lib/AST/ASTPrinter.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include "swift/AST/Decl.h"
2525
#include "swift/AST/Expr.h"
2626
#include "swift/AST/FileUnit.h"
27-
#include "swift/AST/GenericEnvironment.h"
27+
#include "swift/AST/GenericSignature.h"
2828
#include "swift/AST/Module.h"
2929
#include "swift/AST/NameLookup.h"
3030
#include "swift/AST/ParameterList.h"
@@ -2488,14 +2488,14 @@ void PrintAST::visitTypeAliasDecl(TypeAliasDecl *decl) {
24882488
Printer << " = ";
24892489
// FIXME: An inferred associated type witness type alias may reference
24902490
// an opaque type, but OpaqueTypeArchetypes are always canonicalized
2491-
// so lose type sugar for generic params. Bind the generic environment so
2492-
// we can map params back into the generic environment and print them
2491+
// so lose type sugar for generic params. Bind the generic signature so
2492+
// we can map params back into the generic signature and print them
24932493
// correctly.
24942494
//
24952495
// Remove this when we have a way to represent non-canonical archetypes
24962496
// preserving sugar.
2497-
llvm::SaveAndRestore<GenericEnvironment*> setGenericEnv(Options.GenericEnv,
2498-
decl->getGenericEnvironment());
2497+
llvm::SaveAndRestore<const GenericSignatureImpl *> setGenericSig(
2498+
Options.GenericSig, decl->getGenericSignature().getPointer());
24992499
printTypeLoc(TypeLoc(decl->getUnderlyingTypeRepr(), Ty));
25002500
printDeclGenericRequirements(decl);
25012501
}
@@ -4319,7 +4319,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
43194319
Optional<TypePrinter> subBuffer;
43204320
PrintOptions subOptions = Options;
43214321
if (auto substitutions = T->getPatternSubstitutions()) {
4322-
subOptions.GenericEnv = nullptr;
4322+
subOptions.GenericSig = nullptr;
43234323
subBuffer.emplace(Printer, subOptions);
43244324
sub = &*subBuffer;
43254325

@@ -4411,7 +4411,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
44114411
// A box layout has its own independent generic environment. Don't try
44124412
// to print it with the environment's generic params.
44134413
PrintOptions subOptions = Options;
4414-
subOptions.GenericEnv = nullptr;
4414+
subOptions.GenericSig = nullptr;
44154415
TypePrinter sub(Printer, subOptions);
44164416

44174417
// Capture list used here to ensure we don't print anything using `this`
@@ -4594,10 +4594,10 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
45944594
}
45954595
}
45964596

4597-
// When printing SIL types, use a generic environment to map them from
4597+
// When printing SIL types, use a generic signature to map them from
45984598
// canonical types to sugared types.
4599-
if (Options.GenericEnv)
4600-
T = Options.GenericEnv->getSugaredType(T);
4599+
if (Options.GenericSig)
4600+
T = Options.GenericSig->getSugaredType(T);
46014601
}
46024602

46034603
auto Name = T->getName();

lib/AST/Attr.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -590,15 +590,15 @@ static void printDifferentiableAttrArguments(
590590
stream << ' ';
591591
stream << "where ";
592592
std::function<Type(Type)> getInterfaceType;
593-
if (!original || !original->getGenericEnvironment()) {
593+
if (!original || !original->getGenericSignature()) {
594594
getInterfaceType = [](Type Ty) -> Type { return Ty; };
595595
} else {
596-
// Use GenericEnvironment to produce user-friendly
596+
// Use GenericSignature to produce user-friendly
597597
// names instead of something like 't_0_0'.
598-
auto *genericEnv = original->getGenericEnvironment();
599-
assert(genericEnv);
598+
auto genericSig = original->getGenericSignature();
599+
assert(genericSig);
600600
getInterfaceType = [=](Type Ty) -> Type {
601-
return genericEnv->getSugaredType(Ty);
601+
return genericSig->getSugaredType(Ty);
602602
};
603603
}
604604
interleave(requirementsToPrint, [&](Requirement req) {
@@ -933,20 +933,20 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
933933

934934
std::function<Type(Type)> GetInterfaceType;
935935
auto *FnDecl = dyn_cast_or_null<AbstractFunctionDecl>(D);
936-
if (!FnDecl || !FnDecl->getGenericEnvironment())
936+
if (!FnDecl || !FnDecl->getGenericSignature())
937937
GetInterfaceType = [](Type Ty) -> Type { return Ty; };
938938
else {
939-
// Use GenericEnvironment to produce user-friendly
939+
// Use GenericSignature to produce user-friendly
940940
// names instead of something like t_0_0.
941-
auto *GenericEnv = FnDecl->getGenericEnvironment();
942-
assert(GenericEnv);
941+
auto GenericSig = FnDecl->getGenericSignature();
942+
assert(GenericSig);
943943
GetInterfaceType = [=](Type Ty) -> Type {
944-
return GenericEnv->getSugaredType(Ty);
944+
return GenericSig->getSugaredType(Ty);
945945
};
946946

947947
if (auto sig = attr->getSpecializedSgnature()) {
948948
requirementsScratch = sig->requirementsNotSatisfiedBy(
949-
GenericEnv->getGenericSignature());
949+
GenericSig);
950950
requirements = requirementsScratch;
951951
}
952952
}

lib/AST/GenericEnvironment.cpp

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -171,27 +171,6 @@ Type GenericEnvironment::mapTypeIntoContext(GenericTypeParamType *type) const {
171171
return result;
172172
}
173173

174-
GenericTypeParamType *GenericEnvironment::getSugaredType(
175-
GenericTypeParamType *type) const {
176-
for (auto *sugaredType : getGenericParams())
177-
if (sugaredType->isEqual(type))
178-
return sugaredType;
179-
180-
llvm_unreachable("missing generic parameter");
181-
}
182-
183-
Type GenericEnvironment::getSugaredType(Type type) const {
184-
if (!type->hasTypeParameter())
185-
return type;
186-
187-
return type.transform([this](Type Ty) -> Type {
188-
if (auto GP = dyn_cast<GenericTypeParamType>(Ty.getPointer())) {
189-
return Type(getSugaredType(GP));
190-
}
191-
return Ty;
192-
});
193-
}
194-
195174
SubstitutionMap GenericEnvironment::getForwardingSubstitutionMap() const {
196175
auto genericSig = getGenericSignature();
197176
return SubstitutionMap::get(genericSig,

lib/AST/GenericSignature.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,10 +1037,27 @@ SubstitutionMap GenericSignatureImpl::getIdentitySubstitutionMap() const {
10371037
MakeAbstractConformanceForGenericType());
10381038
}
10391039

1040+
GenericTypeParamType *GenericSignatureImpl::getSugaredType(
1041+
GenericTypeParamType *type) const {
1042+
unsigned ordinal = getGenericParamOrdinal(type);
1043+
return getGenericParams()[ordinal];
1044+
}
1045+
1046+
Type GenericSignatureImpl::getSugaredType(Type type) const {
1047+
if (!type->hasTypeParameter())
1048+
return type;
1049+
1050+
return type.transform([this](Type Ty) -> Type {
1051+
if (auto GP = dyn_cast<GenericTypeParamType>(Ty.getPointer())) {
1052+
return Type(getSugaredType(GP));
1053+
}
1054+
return Ty;
1055+
});
1056+
}
1057+
10401058
unsigned GenericSignatureImpl::getGenericParamOrdinal(
10411059
GenericTypeParamType *param) const {
1042-
return GenericParamKey(param->getDepth(), param->getIndex())
1043-
.findIndexIn(getGenericParams());
1060+
return GenericParamKey(param).findIndexIn(getGenericParams());
10441061
}
10451062

10461063
bool GenericSignatureImpl::hasTypeVariable() const {

lib/PrintAsObjC/DeclAndTypePrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1909,7 +1909,7 @@ class DeclAndTypePrinter::Implementation
19091909
assert(extension->getGenericSignature().getCanonicalSignature() ==
19101910
extendedClass->getGenericSignature().getCanonicalSignature() &&
19111911
"constrained extensions or custom generic parameters?");
1912-
type = extendedClass->getGenericEnvironment()->getSugaredType(type);
1912+
type = extendedClass->getGenericSignature()->getSugaredType(type);
19131913
decl = type->getDecl();
19141914
}
19151915

lib/SIL/IR/SILPrinter.cpp

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -462,18 +462,20 @@ static void printSILFunctionNameAndType(
462462
llvm::DenseMap<CanType, Identifier> &sugaredTypeNames) {
463463
function->printName(OS);
464464
OS << " : $";
465-
auto genSig =
466-
function->getLoweredFunctionType()->getInvocationGenericSignature();
467465
auto *genEnv = function->getGenericEnvironment();
468-
// If `genSig` and `genEnv` are both defined, get sugared names of generic
466+
const GenericSignatureImpl *genSig = nullptr;
467+
468+
// If `genEnv` is defined, get sugared names of generic
469469
// parameter types for printing.
470-
if (genSig && genEnv) {
470+
if (genEnv) {
471+
genSig = genEnv->getGenericSignature().getPointer();
472+
471473
llvm::DenseSet<Identifier> usedNames;
472474
llvm::SmallString<16> disambiguatedNameBuf;
473475
unsigned disambiguatedNameCounter = 1;
474476
for (auto *paramTy : genSig->getGenericParams()) {
475477
// Get a uniqued sugared name for the generic parameter type.
476-
auto sugaredTy = genEnv->getSugaredType(paramTy);
478+
auto sugaredTy = genEnv->getGenericSignature()->getSugaredType(paramTy);
477479
Identifier name = sugaredTy->getName();
478480
while (!usedNames.insert(name).second) {
479481
disambiguatedNameBuf.clear();
@@ -495,7 +497,7 @@ static void printSILFunctionNameAndType(
495497
}
496498
}
497499
auto printOptions = PrintOptions::printSIL();
498-
printOptions.GenericEnv = genEnv;
500+
printOptions.GenericSig = genSig;
499501
printOptions.AlternativeTypeNames =
500502
sugaredTypeNames.empty() ? nullptr : &sugaredTypeNames;
501503
function->getLoweredFunctionType()->print(OS, printOptions);
@@ -3245,8 +3247,9 @@ void SILWitnessTable::print(llvm::raw_ostream &OS, bool Verbose) const {
32453247
OS << "[serialized] ";
32463248

32473249
getConformance()->printName(OS, Options);
3248-
Options.GenericEnv =
3249-
getConformance()->getDeclContext()->getGenericEnvironmentOfContext();
3250+
Options.GenericSig =
3251+
getConformance()->getDeclContext()->getGenericSignatureOfContext()
3252+
.getPointer();
32503253

32513254
if (isDeclaration()) {
32523255
OS << "\n\n";
@@ -3289,7 +3292,7 @@ void SILDefaultWitnessTable::print(llvm::raw_ostream &OS, bool Verbose) const {
32893292
OS << getProtocol()->getName() << " {\n";
32903293

32913294
PrintOptions options = PrintOptions::printSIL();
3292-
options.GenericEnv = Protocol->getGenericEnvironmentOfContext();
3295+
options.GenericSig = Protocol->getGenericSignatureOfContext().getPointer();
32933296

32943297
for (auto &witness : getEntries()) {
32953298
witness.print(OS, Verbose, options);
@@ -3440,12 +3443,17 @@ void SILSpecializeAttr::print(llvm::raw_ostream &OS) const {
34403443
OS << "exported: " << exported << ", ";
34413444
OS << "kind: " << kind << ", ";
34423445

3446+
auto *genericEnv = getFunction()->getGenericEnvironment();
3447+
GenericSignature genericSig;
3448+
if (genericEnv)
3449+
genericSig = genericEnv->getGenericSignature();
3450+
34433451
ArrayRef<Requirement> requirements;
34443452
SmallVector<Requirement, 4> requirementsScratch;
34453453
if (auto specializedSig = getSpecializedSignature()) {
3446-
if (auto env = getFunction()->getGenericEnvironment()) {
3454+
if (genericSig) {
34473455
requirementsScratch = specializedSig->requirementsNotSatisfiedBy(
3448-
env->getGenericSignature());
3456+
genericSig);
34493457
requirements = requirementsScratch;
34503458
} else {
34513459
requirements = specializedSig->getRequirements();
@@ -3455,19 +3463,18 @@ void SILSpecializeAttr::print(llvm::raw_ostream &OS) const {
34553463
OS << "where ";
34563464
SILFunction *F = getFunction();
34573465
assert(F);
3458-
auto GenericEnv = F->getGenericEnvironment();
34593466
interleave(requirements,
34603467
[&](Requirement req) {
3461-
if (!GenericEnv) {
3468+
if (!genericSig) {
34623469
req.print(OS, SubPrinter);
34633470
return;
34643471
}
34653472
// Use GenericEnvironment to produce user-friendly
34663473
// names instead of something like t_0_0.
3467-
auto FirstTy = GenericEnv->getSugaredType(req.getFirstType());
3474+
auto FirstTy = genericSig->getSugaredType(req.getFirstType());
34683475
if (req.getKind() != RequirementKind::Layout) {
34693476
auto SecondTy =
3470-
GenericEnv->getSugaredType(req.getSecondType());
3477+
genericSig->getSugaredType(req.getSecondType());
34713478
Requirement ReqWithDecls(req.getKind(), FirstTy, SecondTy);
34723479
ReqWithDecls.print(OS, SubPrinter);
34733480
} else {

0 commit comments

Comments
 (0)