Skip to content

GSB: Optimize PotentialArchetype::getDependentType() #35773

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -1042,8 +1042,6 @@ ERROR(expected_else_after_guard,PointsToFirstBadToken,
"expected 'else' after 'guard' condition", ())
ERROR(expected_lbrace_after_guard,PointsToFirstBadToken,
"expected '{' after 'guard' else", ())
ERROR(bound_var_guard_body,none,
"variable declared in 'guard' condition is not usable in its body", ())

// While Statement
ERROR(expected_condition_while,PointsToFirstBadToken,
Expand Down
70 changes: 17 additions & 53 deletions include/swift/AST/GenericSignatureBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ class GenericSignatureBuilder {
/// Describes a component within the graph of same-type constraints within
/// the equivalence class that is held together by derived constraints.
struct DerivedSameTypeComponent {
/// The potential archetype that acts as the anchor for this component.
UnresolvedType anchor;
/// The type that acts as the anchor for this component.
Type type;

/// The (best) requirement source within the component that makes the
/// potential archetypes in this component equivalent to the concrete
Expand Down Expand Up @@ -1477,31 +1477,17 @@ struct GenericSignatureBuilder::Constraint {
};

class GenericSignatureBuilder::PotentialArchetype {
/// The parent of this potential archetype (for a nested type) or the
/// ASTContext in which the potential archetype resides.
llvm::PointerUnion<PotentialArchetype*, ASTContext*> parentOrContext;

/// The identifier describing this particular archetype.
///
/// \c parentOrBuilder determines whether we have a nested type vs. a root.
union PAIdentifier {
/// The associated type for a resolved nested type.
AssociatedTypeDecl *assocType;

/// The generic parameter key for a root.
GenericParamKey genericParam;

PAIdentifier(AssociatedTypeDecl *assocType) : assocType(assocType) {}

PAIdentifier(GenericParamKey genericParam) : genericParam(genericParam) { }
} identifier;
/// The parent of this potential archetype, for a nested type.
PotentialArchetype* parent;

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

mutable CanType depType;

/// A stored nested type.
struct StoredNestedType {
/// The potential archetypes describing this nested type, all of which
Expand Down Expand Up @@ -1539,7 +1525,7 @@ class GenericSignatureBuilder::PotentialArchetype {
PotentialArchetype(PotentialArchetype *parent, AssociatedTypeDecl *assocType);

/// Construct a new potential archetype for a generic parameter.
PotentialArchetype(ASTContext &ctx, GenericParamKey genericParam);
explicit PotentialArchetype(GenericTypeParamType *genericParam);

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

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

/// Determine whether this is a generic parameter.
bool isGenericParam() const {
return parentOrContext.is<ASTContext *>();
}

/// Retrieve the generic parameter key for a potential archetype that
/// represents this potential archetype.
///
/// \pre \c isGenericParam()
GenericParamKey getGenericParamKey() const {
assert(isGenericParam() && "Not a generic parameter");
return identifier.genericParam;
}

/// Retrieve the generic parameter key for the generic parameter at the
/// root of this potential archetype.
GenericParamKey getRootGenericParamKey() const {
if (auto parent = getParent())
return parent->getRootGenericParamKey();

return getGenericParamKey();
}

/// Retrieve the name of a nested potential archetype.
Identifier getNestedName() const {
assert(getParent() && "Not a nested type");
return identifier.assocType->getName();
return parent == nullptr;
}

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

/// Retrieve the dependent type that describes this potential
/// archetype.
CanType getDependentType() const {
return depType;
}

/// Retrieve the dependent type that describes this potential
/// archetype.
///
/// \param genericParams The set of generic parameters to use in the resulting
/// dependent type.
Type getDependentType(TypeArrayView<GenericTypeParamType> genericParams)const;
Type getDependentType(TypeArrayView<GenericTypeParamType> genericParams) const;

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

/// Retrieve the AST context in which this potential archetype resides.
ASTContext &getASTContext() const;

SWIFT_DEBUG_DUMP;

void dump(llvm::raw_ostream &Out, SourceManager *SrcMgr,
Expand Down
3 changes: 1 addition & 2 deletions include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -5659,8 +5659,7 @@ class DependentMemberType : public TypeBase {
}
};
BEGIN_CAN_TYPE_WRAPPER(DependentMemberType, Type)
static CanDependentMemberType get(CanType base, AssociatedTypeDecl *assocType,
const ASTContext &C) {
static CanDependentMemberType get(CanType base, AssociatedTypeDecl *assocType) {
return CanDependentMemberType(DependentMemberType::get(base, assocType));
}

Expand Down
Loading