Skip to content

Commit 9d96ed9

Browse files
committed
AST: Rename 'canonical wrt. generic signature' to 'reduced'
We had two notions of canonical types, one is the structural property where it doesn't contain sugared types, the other one where it does not contain reducible type parameters with respect to a generic signature. Rename the second one to a 'reduced type'.
1 parent 172bcdc commit 9d96ed9

Some content is hidden

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

45 files changed

+180
-194
lines changed

include/swift/AST/GenericSignature.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,9 @@ class GenericSignature {
213213
SmallVector<Requirement, 4>
214214
requirementsNotSatisfiedBy(GenericSignature otherSig) const;
215215

216-
/// Return the canonical version of the given type under this generic
216+
/// Return the reduced version of the given type under this generic
217217
/// signature.
218-
CanType getCanonicalTypeInContext(Type type) const;
218+
CanType getReducedType(Type type) const;
219219

220220
/// Check invariants.
221221
void verify() const;
@@ -383,7 +383,7 @@ class alignas(1 << TypeAlignInBits) GenericSignatureImpl final
383383
/// generic signature.
384384
///
385385
/// The type parameters must be known to not be concrete within the context.
386-
bool areSameTypeParameterInContext(Type type1, Type type2) const;
386+
bool areReducedTypeParametersEqual(Type type1, Type type2) const;
387387

388388
/// Determine if \c sig can prove \c requirement, meaning that it can deduce
389389
/// T: Foo or T == U (etc.) with the information it knows. This includes
@@ -392,11 +392,11 @@ class alignas(1 << TypeAlignInBits) GenericSignatureImpl final
392392
bool isRequirementSatisfied(
393393
Requirement requirement, bool allowMissing = false) const;
394394

395-
bool isCanonicalTypeInContext(Type type) const;
395+
bool isReducedType(Type type) const;
396396

397397
/// Determine whether the given type parameter is defined under this generic
398398
/// signature.
399-
bool isValidTypeInContext(Type type) const;
399+
bool isValidTypeParameter(Type type) const;
400400

401401
/// Retrieve the conformance access path used to extract the conformance of
402402
/// interface \c type to the given \c protocol.
@@ -487,9 +487,9 @@ class alignas(1 << TypeAlignInBits) GenericSignatureImpl final
487487
SmallVector<Requirement, 4>
488488
requirementsNotSatisfiedBy(GenericSignature otherSig) const;
489489

490-
/// Return the canonical version of the given type under this generic
490+
/// Return the reduced version of the given type under this generic
491491
/// signature.
492-
CanType getCanonicalTypeInContext(Type type) const;
492+
CanType getReducedType(Type type) const;
493493
};
494494

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

include/swift/AST/Types.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,9 +503,9 @@ class alignas(1 << TypeAlignInBits) TypeBase
503503
return const_cast<TypeBase*>(this)->computeCanonicalType();
504504
}
505505

506-
/// getCanonicalType - Stronger canonicalization which folds away equivalent
506+
/// getReducedType - Stronger canonicalization which folds away equivalent
507507
/// associated types, or type parameters that have been made concrete.
508-
CanType getCanonicalType(GenericSignature sig);
508+
CanType getReducedType(GenericSignature sig);
509509

510510
/// Canonical protocol composition types are minimized only to a certain
511511
/// degree to preserve ABI compatibility. This routine enables performing

include/swift/SIL/AbstractionPattern.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ class AbstractionPattern {
514514
OrigType = origType;
515515
GenericSig = CanGenericSignature();
516516
if (OrigType->hasTypeParameter()) {
517-
assert(OrigType == signature.getCanonicalTypeInContext(origType));
517+
assert(OrigType == signature.getReducedType(origType));
518518
GenericSig = signature;
519519
}
520520
}

include/swift/SILOptimizer/Analysis/DifferentiableActivityAnalysis.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class DifferentiableActivityInfo {
135135
auto type = value->getType().getASTType();
136136
// Remap archetypes in the derivative generic signature, if it exists.
137137
if (type->hasArchetype()) {
138-
type = derivativeGenericSignature.getCanonicalTypeInContext(
138+
type = derivativeGenericSignature.getReducedType(
139139
type->mapTypeOutOfContext());
140140
}
141141
// Look up conformance in the current module.

include/swift/SILOptimizer/Differentiation/LinearMapInfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ class LinearMapInfo {
165165
derivative->getLoweredFunctionType()->getSubstGenericSignature();
166166
auto *linMapStruct = getLinearMapStruct(origBB);
167167
auto linMapStructType =
168-
linMapStruct->getDeclaredInterfaceType()->getCanonicalType(
168+
linMapStruct->getDeclaredInterfaceType()->getReducedType(
169169
derivativeGenSig);
170170
Lowering::AbstractionPattern pattern(derivativeGenSig, linMapStructType);
171171
return typeConverter.getLoweredType(pattern, linMapStructType,

lib/AST/ASTContext.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3541,15 +3541,15 @@ isGenericFunctionTypeCanonical(GenericSignature sig,
35413541
return false;
35423542

35433543
for (auto param : params) {
3544-
if (!sig->isCanonicalTypeInContext(param.getPlainType()))
3544+
if (!sig->isReducedType(param.getPlainType()))
35453545
return false;
35463546
if (!param.getInternalLabel().empty()) {
35473547
// Canonical types don't have internal labels
35483548
return false;
35493549
}
35503550
}
35513551

3552-
return sig->isCanonicalTypeInContext(result);
3552+
return sig->isReducedType(result);
35533553
}
35543554

35553555
AnyFunctionType *AnyFunctionType::withExtInfo(ExtInfo info) const {
@@ -3771,7 +3771,7 @@ GenericFunctionType *GenericFunctionType::get(GenericSignature sig,
37713771
}
37723772

37733773
// We have to construct this generic function type. Determine whether
3774-
// it's canonical. Unfortunately, isCanonicalTypeInContext can cause
3774+
// it's canonical. Unfortunately, isReducedType() can cause
37753775
// new GenericFunctionTypes to be created and thus invalidate our insertion
37763776
// point.
37773777
bool isCanonical = isGenericFunctionTypeCanonical(sig, params, result);
@@ -3788,7 +3788,7 @@ GenericFunctionType *GenericFunctionType::get(GenericSignature sig,
37883788
if (info.hasValue())
37893789
globalActor = info->getGlobalActor();
37903790

3791-
if (globalActor && !sig->isCanonicalTypeInContext(globalActor))
3791+
if (globalActor && !sig->isReducedType(globalActor))
37923792
isCanonical = false;
37933793

37943794
size_t allocSize = totalSizeToAlloc<AnyFunctionType::Param, Type>(

lib/AST/ASTMangler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3488,7 +3488,7 @@ void ASTMangler::appendConcreteProtocolConformance(
34883488
auto type = conditionalReq.getFirstType();
34893489
if (type->hasArchetype())
34903490
type = type->mapTypeOutOfContext();
3491-
CanType canType = type->getCanonicalType(sig);
3491+
CanType canType = type->getReducedType(sig);
34923492
auto proto = conditionalReq.getProtocolDecl();
34933493

34943494
ProtocolConformanceRef conformance;

lib/AST/Decl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4106,7 +4106,7 @@ findGenericParameterReferences(CanGenericSignature genericSig,
41064106

41074107
// If the type parameter is beyond the domain of the existential generic
41084108
// signature, ignore it.
4109-
if (!genericSig->isValidTypeInContext(type)) {
4109+
if (!genericSig->isValidTypeParameter(type)) {
41104110
return GenericParameterReferenceInfo();
41114111
}
41124112

lib/AST/GenericSignature.cpp

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -357,15 +357,15 @@ LayoutConstraint GenericSignatureImpl::getLayoutConstraint(Type type) const {
357357
return getRequirementMachine()->getLayoutConstraint(type);
358358
}
359359

360-
bool GenericSignatureImpl::areSameTypeParameterInContext(Type type1,
360+
bool GenericSignatureImpl::areReducedTypeParametersEqual(Type type1,
361361
Type type2) const {
362362
assert(type1->isTypeParameter());
363363
assert(type2->isTypeParameter());
364364

365365
if (type1.getPointer() == type2.getPointer())
366366
return true;
367367

368-
return getRequirementMachine()->areSameTypeParameterInContext(type1, type2);
368+
return getRequirementMachine()->areReducedTypeParametersEqual(type1, type2);
369369
}
370370

371371
bool GenericSignatureImpl::isRequirementSatisfied(
@@ -431,43 +431,40 @@ SmallVector<Requirement, 4> GenericSignatureImpl::requirementsNotSatisfiedBy(
431431
return result;
432432
}
433433

434-
bool GenericSignatureImpl::isCanonicalTypeInContext(Type type) const {
435-
// If the type isn't independently canonical, it's certainly not canonical
436-
// in this context.
434+
bool GenericSignatureImpl::isReducedType(Type type) const {
435+
// If the type isn't canonical, it's not reduced.
437436
if (!type->isCanonical())
438437
return false;
439438

440-
// All the contextual canonicality rules apply to type parameters, so if the
441-
// type doesn't involve any type parameters, it's already canonical.
439+
// A fully concrete canonical type is reduced.
442440
if (!type->hasTypeParameter())
443441
return true;
444442

445-
return getRequirementMachine()->isCanonicalTypeInContext(type);
443+
return getRequirementMachine()->isReducedType(type);
446444
}
447445

448-
CanType GenericSignature::getCanonicalTypeInContext(Type type) const {
446+
CanType GenericSignature::getReducedType(Type type) const {
449447
// The null generic signature has no requirements so cannot influence the
450448
// structure of the can type computed here.
451449
if (isNull()) {
452450
return type->getCanonicalType();
453451
}
454-
return getPointer()->getCanonicalTypeInContext(type);
452+
return getPointer()->getReducedType(type);
455453
}
456454

457-
CanType GenericSignatureImpl::getCanonicalTypeInContext(Type type) const {
455+
CanType GenericSignatureImpl::getReducedType(Type type) const {
458456
type = type->getCanonicalType();
459457

460-
// All the contextual canonicality rules apply to type parameters, so if the
461-
// type doesn't involve any type parameters, it's already canonical.
458+
// A fully concrete type is already reduced.
462459
if (!type->hasTypeParameter())
463460
return CanType(type);
464461

465-
return getRequirementMachine()->getCanonicalTypeInContext(
462+
return getRequirementMachine()->getReducedType(
466463
type, { })->getCanonicalType();
467464
}
468465

469-
bool GenericSignatureImpl::isValidTypeInContext(Type type) const {
470-
return getRequirementMachine()->isValidTypeInContext(type);
466+
bool GenericSignatureImpl::isValidTypeParameter(Type type) const {
467+
return getRequirementMachine()->isValidTypeParameter(type);
471468
}
472469

473470
ArrayRef<CanTypeWrapper<GenericTypeParamType>>
@@ -850,8 +847,8 @@ void GenericSignature::verify(ArrayRef<Requirement> reqts) const {
850847
abort();
851848
}
852849

853-
if (!canSig->isCanonicalTypeInContext(reqt.getFirstType())) {
854-
llvm::errs() << "Left-hand side is not canonical: ";
850+
if (!canSig->isReducedType(reqt.getFirstType())) {
851+
llvm::errs() << "Left-hand side is not reduced: ";
855852
reqt.dump(llvm::errs());
856853
llvm::errs() << "\n";
857854
abort();
@@ -861,8 +858,8 @@ void GenericSignature::verify(ArrayRef<Requirement> reqts) const {
861858
// Check canonicalization of requirement itself.
862859
switch (reqt.getKind()) {
863860
case RequirementKind::Superclass:
864-
if (!canSig->isCanonicalTypeInContext(reqt.getSecondType())) {
865-
llvm::errs() << "Right-hand side is not canonical: ";
861+
if (!canSig->isReducedType(reqt.getSecondType())) {
862+
llvm::errs() << "Right-hand side is not reduced: ";
866863
reqt.dump(llvm::errs());
867864
llvm::errs() << "\n";
868865
abort();
@@ -873,9 +870,9 @@ void GenericSignature::verify(ArrayRef<Requirement> reqts) const {
873870
break;
874871

875872
case RequirementKind::SameType: {
876-
auto hasCanonicalOrConcreteParent = [&](Type type) {
873+
auto hasReducedOrConcreteParent = [&](Type type) {
877874
if (auto *dmt = type->getAs<DependentMemberType>()) {
878-
return (canSig->isCanonicalTypeInContext(dmt->getBase()) ||
875+
return (canSig->isReducedType(dmt->getBase()) ||
879876
canSig->isConcreteType(dmt->getBase()));
880877
}
881878
return type->is<GenericTypeParamType>();
@@ -884,19 +881,19 @@ void GenericSignature::verify(ArrayRef<Requirement> reqts) const {
884881
auto firstType = reqt.getFirstType();
885882
auto secondType = reqt.getSecondType();
886883

887-
auto canType = canSig->getCanonicalTypeInContext(firstType);
884+
auto canType = canSig->getReducedType(firstType);
888885
auto &component = sameTypeComponents[canType];
889886

890-
if (!hasCanonicalOrConcreteParent(firstType)) {
891-
llvm::errs() << "Left hand side does not have a canonical parent: ";
887+
if (!hasReducedOrConcreteParent(firstType)) {
888+
llvm::errs() << "Left hand side does not have a reduced parent: ";
892889
reqt.dump(llvm::errs());
893890
llvm::errs() << "\n";
894891
abort();
895892
}
896893

897894
if (reqt.getSecondType()->isTypeParameter()) {
898-
if (!hasCanonicalOrConcreteParent(secondType)) {
899-
llvm::errs() << "Right hand side does not have a canonical parent: ";
895+
if (!hasReducedOrConcreteParent(secondType)) {
896+
llvm::errs() << "Right hand side does not have a reduced parent: ";
900897
reqt.dump(llvm::errs());
901898
llvm::errs() << "\n";
902899
abort();
@@ -920,8 +917,8 @@ void GenericSignature::verify(ArrayRef<Requirement> reqts) const {
920917

921918
component.push_back(secondType);
922919
} else {
923-
if (!canSig->isCanonicalTypeInContext(secondType)) {
924-
llvm::errs() << "Right hand side is not canonical: ";
920+
if (!canSig->isReducedType(secondType)) {
921+
llvm::errs() << "Right hand side is not reduced: ";
925922
reqt.dump(llvm::errs());
926923
llvm::errs() << "\n";
927924
abort();
@@ -1004,9 +1001,9 @@ void GenericSignature::verify(ArrayRef<Requirement> reqts) const {
10041001
// Check same-type components for consistency.
10051002
for (const auto &pair : sameTypeComponents) {
10061003
if (pair.second.front()->isTypeParameter() &&
1007-
!canSig->isCanonicalTypeInContext(pair.second.front())) {
1004+
!canSig->isReducedType(pair.second.front())) {
10081005
llvm::errs() << "Abstract same-type requirement involving concrete types\n";
1009-
llvm::errs() << "Canonical type: " << pair.first << "\n";
1006+
llvm::errs() << "Reduced type: " << pair.first << "\n";
10101007
llvm::errs() << "Left hand side of first requirement: "
10111008
<< pair.second.front() << "\n";
10121009
abort();

0 commit comments

Comments
 (0)