Skip to content

Commit df3c879

Browse files
committed
GSB: PotentialArchetypes now store their canonical type
Instead of recomputing it on every call to getDependentType(), we can just store it in there instead of the GenericParamKey or AssociatedTypeDecl. We still need to 're-sugar' user-visible dependent types to get the right generic parameter name; a new getSugaredDependentType() utility method is called in the right places for that.
1 parent 143b3c1 commit df3c879

File tree

3 files changed

+90
-144
lines changed

3 files changed

+90
-144
lines changed

include/swift/AST/GenericSignatureBuilder.h

Lines changed: 15 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,31 +1477,17 @@ struct GenericSignatureBuilder::Constraint {
14771477
};
14781478

14791479
class GenericSignatureBuilder::PotentialArchetype {
1480-
/// The parent of this potential archetype (for a nested type) or the
1481-
/// ASTContext in which the potential archetype resides.
1482-
llvm::PointerUnion<PotentialArchetype*, ASTContext*> parentOrContext;
1483-
1484-
/// The identifier describing this particular archetype.
1485-
///
1486-
/// \c parentOrBuilder determines whether we have a nested type vs. a root.
1487-
union PAIdentifier {
1488-
/// The associated type for a resolved nested type.
1489-
AssociatedTypeDecl *assocType;
1490-
1491-
/// The generic parameter key for a root.
1492-
GenericParamKey genericParam;
1493-
1494-
PAIdentifier(AssociatedTypeDecl *assocType) : assocType(assocType) {}
1495-
1496-
PAIdentifier(GenericParamKey genericParam) : genericParam(genericParam) { }
1497-
} identifier;
1480+
/// The parent of this potential archetype, for a nested type.
1481+
PotentialArchetype* parent;
14981482

14991483
/// The representative of the equivalence class of potential archetypes
15001484
/// to which this potential archetype belongs, or (for the representative)
15011485
/// the equivalence class itself.
15021486
mutable llvm::PointerUnion<PotentialArchetype *, EquivalenceClass *>
15031487
representativeOrEquivClass;
15041488

1489+
mutable CanType depType;
1490+
15051491
/// A stored nested type.
15061492
struct StoredNestedType {
15071493
/// The potential archetypes describing this nested type, all of which
@@ -1539,7 +1525,7 @@ class GenericSignatureBuilder::PotentialArchetype {
15391525
PotentialArchetype(PotentialArchetype *parent, AssociatedTypeDecl *assocType);
15401526

15411527
/// Construct a new potential archetype for a generic parameter.
1542-
PotentialArchetype(ASTContext &ctx, GenericParamKey genericParam);
1528+
explicit PotentialArchetype(GenericTypeParamType *genericParam);
15431529

15441530
public:
15451531
/// Retrieve the representative for this archetype, performing
@@ -1558,42 +1544,17 @@ class GenericSignatureBuilder::PotentialArchetype {
15581544
/// Retrieve the parent of this potential archetype, which will be non-null
15591545
/// when this potential archetype is an associated type.
15601546
PotentialArchetype *getParent() const {
1561-
return parentOrContext.dyn_cast<PotentialArchetype *>();
1547+
return parent;
15621548
}
15631549

15641550
/// Retrieve the type declaration to which this nested type was resolved.
15651551
AssociatedTypeDecl *getResolvedType() const {
1566-
assert(getParent() && "Not an associated type");
1567-
return identifier.assocType;
1552+
return cast<DependentMemberType>(depType)->getAssocType();
15681553
}
15691554

15701555
/// Determine whether this is a generic parameter.
15711556
bool isGenericParam() const {
1572-
return parentOrContext.is<ASTContext *>();
1573-
}
1574-
1575-
/// Retrieve the generic parameter key for a potential archetype that
1576-
/// represents this potential archetype.
1577-
///
1578-
/// \pre \c isGenericParam()
1579-
GenericParamKey getGenericParamKey() const {
1580-
assert(isGenericParam() && "Not a generic parameter");
1581-
return identifier.genericParam;
1582-
}
1583-
1584-
/// Retrieve the generic parameter key for the generic parameter at the
1585-
/// root of this potential archetype.
1586-
GenericParamKey getRootGenericParamKey() const {
1587-
if (auto parent = getParent())
1588-
return parent->getRootGenericParamKey();
1589-
1590-
return getGenericParamKey();
1591-
}
1592-
1593-
/// Retrieve the name of a nested potential archetype.
1594-
Identifier getNestedName() const {
1595-
assert(getParent() && "Not a nested type");
1596-
return identifier.assocType->getName();
1557+
return parent == nullptr;
15971558
}
15981559

15991560
/// Retrieve the set of nested types.
@@ -1643,12 +1604,18 @@ class GenericSignatureBuilder::PotentialArchetype {
16431604
AssociatedTypeDecl *assocType,
16441605
ArchetypeResolutionKind kind);
16451606

1607+
/// Retrieve the dependent type that describes this potential
1608+
/// archetype.
1609+
CanType getDependentType() const {
1610+
return depType;
1611+
}
1612+
16461613
/// Retrieve the dependent type that describes this potential
16471614
/// archetype.
16481615
///
16491616
/// \param genericParams The set of generic parameters to use in the resulting
16501617
/// dependent type.
1651-
Type getDependentType(TypeArrayView<GenericTypeParamType> genericParams)const;
1618+
Type getDependentType(TypeArrayView<GenericTypeParamType> genericParams) const;
16521619

16531620
/// True if the potential archetype has been bound by a concrete type
16541621
/// constraint.
@@ -1659,9 +1626,6 @@ class GenericSignatureBuilder::PotentialArchetype {
16591626
return false;
16601627
}
16611628

1662-
/// Retrieve the AST context in which this potential archetype resides.
1663-
ASTContext &getASTContext() const;
1664-
16651629
SWIFT_DEBUG_DUMP;
16661630

16671631
void dump(llvm::raw_ostream &Out, SourceManager *SrcMgr,

0 commit comments

Comments
 (0)