34
34
#include " swift/AST/TypeAlignments.h"
35
35
#include " swift/AST/TypeExpansionContext.h"
36
36
#include " swift/Basic/ArrayRefView.h"
37
+ #include " swift/Basic/Assertions.h"
37
38
#include " swift/Basic/Debug.h"
38
39
#include " swift/Basic/InlineBitfield.h"
39
40
#include " swift/Basic/UUID.h"
@@ -562,6 +563,13 @@ class alignas(1 << TypeAlignInBits) TypeBase
562
563
assert (Bits.TypeBase .Properties == properties.getBits () && " Bits dropped!" );
563
564
}
564
565
566
+ // / This is used when constructing GenericTypeParamTypes.
567
+ void setCanonicalType (CanType type) {
568
+ DEBUG_ASSERT (!Bits.TypeBase .IsCanonical );
569
+ DEBUG_ASSERT (CanonicalType.isNull ());
570
+ CanonicalType = type;
571
+ }
572
+
565
573
public:
566
574
// / getKind - Return what kind of type this is.
567
575
TypeKind getKind () const { return static_cast <TypeKind>(Bits.TypeBase .Kind ); }
@@ -6966,12 +6974,17 @@ const Type *ArchetypeType::getSubclassTrailingObjects() const {
6966
6974
// / \sa GenericTypeParamDecl
6967
6975
class GenericTypeParamType : public SubstitutableType ,
6968
6976
public llvm::FoldingSetNode {
6969
- static constexpr unsigned TYPE_SEQUENCE_BIT = (1 << 30 );
6970
-
6971
- using DepthIndexTy = llvm::PointerEmbeddedInt<unsigned , 31 >;
6977
+ // / A canonical generic parameter type is given by a depth, index, parameter
6978
+ // / kind, and an optional value type. A sugared generic parameter type stores
6979
+ // / a declaration or an identifier.
6980
+ union {
6981
+ GenericTypeParamDecl *Decl;
6982
+ Identifier Name;
6983
+ };
6972
6984
6973
- // / The generic type parameter or depth/index.
6974
- llvm::PointerUnion<GenericTypeParamDecl *, DepthIndexTy> ParamOrDepthIndex;
6985
+ unsigned Depth : 15 ;
6986
+ unsigned IsDecl : 1 ;
6987
+ unsigned Index : 16 ;
6975
6988
6976
6989
// / The kind of generic type parameter this is.
6977
6990
GenericTypeParamKind ParamKind;
@@ -6984,39 +6997,44 @@ class GenericTypeParamType : public SubstitutableType,
6984
6997
Type ValueType;
6985
6998
6986
6999
public:
7000
+ // / Retrieve a sugared generic type parameter type.
7001
+ // /
7002
+ // / Note: This should only be called by the InterfaceTypeRequest.
7003
+ static GenericTypeParamType *get (GenericTypeParamDecl *decl);
6987
7004
6988
- // / Retrieve a generic type parameter with the given kind, depth, index, and
6989
- // / optional value type.
7005
+ // / Retrieve a sugared generic type parameter at the given depth and index.
7006
+ static GenericTypeParamType *get (Identifier name,
7007
+ GenericTypeParamKind paramKind,
7008
+ unsigned depth, unsigned index,
7009
+ Type valueType, const ASTContext &ctx);
7010
+
7011
+ // / Retrieve a canonical generic type parameter with the given kind, depth,
7012
+ // / index, and optional value type.
6990
7013
static GenericTypeParamType *get (GenericTypeParamKind paramKind,
6991
7014
unsigned depth, unsigned index,
6992
7015
Type valueType, const ASTContext &ctx);
6993
7016
6994
- // / Retrieve a generic type parameter at the given depth and index.
7017
+ // / Retrieve a canonical generic type parameter at the given depth and index.
6995
7018
static GenericTypeParamType *getType (unsigned depth, unsigned index,
6996
7019
const ASTContext &ctx);
6997
7020
6998
- // / Retrieve a generic parameter pack at the given depth and index.
7021
+ // / Retrieve a canonical generic parameter pack at the given depth and index.
6999
7022
static GenericTypeParamType *getPack (unsigned depth, unsigned index,
7000
7023
const ASTContext &ctx);
7001
7024
7002
- // / Retrieve a generic value parameter at the given depth and index with the
7003
- // / given value type.
7025
+ // / Retrieve a canonical generic value parameter at the given depth and index
7026
+ // / with the given value type.
7004
7027
static GenericTypeParamType *getValue (unsigned depth, unsigned index,
7005
7028
Type valueType, const ASTContext &ctx);
7006
7029
7007
- // / Retrieve a generic type parameter for the given generic type param decl.
7008
- // /
7009
- // / Note: This should only be called by the GenericTypeParamDecl constructor.
7010
- static GenericTypeParamType *get (GenericTypeParamDecl *param);
7011
-
7012
7030
// / If this is an opaque parameter, return the declaration of the
7013
7031
// / parameter, otherwise null.
7014
7032
GenericTypeParamDecl *getOpaqueDecl () const ;
7015
7033
7016
7034
// / Retrieve the declaration of the generic type parameter, or null if
7017
7035
// / there is no such declaration.
7018
7036
GenericTypeParamDecl *getDecl () const {
7019
- return ParamOrDepthIndex. dyn_cast <GenericTypeParamDecl *>( );
7037
+ return (IsDecl ? Decl : nullptr );
7020
7038
}
7021
7039
7022
7040
// / Retrieve the kind of generic type parameter this type is referencing.
@@ -7037,7 +7055,9 @@ class GenericTypeParamType : public SubstitutableType,
7037
7055
// / \endcode
7038
7056
// /
7039
7057
// / Here 'T' has depth 0 and 'U' has depth 1. Both have index 0.
7040
- unsigned getDepth () const ;
7058
+ unsigned getDepth () const {
7059
+ return Depth;
7060
+ }
7041
7061
7042
7062
// / The index of this generic type parameter within its generic parameter
7043
7063
// / list.
@@ -7049,7 +7069,9 @@ class GenericTypeParamType : public SubstitutableType,
7049
7069
// / \endcode
7050
7070
// /
7051
7071
// / Here 'T' and 'U' have indexes 0 and 1, respectively. 'V' has index 0.
7052
- unsigned getIndex () const ;
7072
+ unsigned getIndex () const {
7073
+ return Index;
7074
+ }
7053
7075
7054
7076
// / Returns \c true if this type parameter is declared as a pack.
7055
7077
// /
@@ -7073,14 +7095,17 @@ class GenericTypeParamType : public SubstitutableType,
7073
7095
Type getValueType () const ;
7074
7096
7075
7097
void Profile (llvm::FoldingSetNodeID &ID) {
7076
- Profile (ID, getParamKind (), getDepth (), getIndex (), getValueType ());
7098
+ Profile (ID, getParamKind (), getDepth (), getIndex (), getValueType (),
7099
+ getName ());
7077
7100
}
7078
- static void Profile (llvm::FoldingSetNodeID &ID, GenericTypeParamKind paramKind,
7079
- unsigned depth, unsigned index, Type valueType) {
7101
+ static void Profile (llvm::FoldingSetNodeID &ID,
7102
+ GenericTypeParamKind paramKind, unsigned depth,
7103
+ unsigned index, Type valueType, Identifier name) {
7080
7104
ID.AddInteger ((uint8_t )paramKind);
7081
7105
ID.AddInteger (depth);
7082
7106
ID.AddInteger (index);
7083
7107
ID.AddPointer (valueType.getPointer ());
7108
+ ID.AddPointer (name.get ());
7084
7109
}
7085
7110
7086
7111
// Implement isa/cast/dyncast/etc.
@@ -7091,16 +7116,19 @@ class GenericTypeParamType : public SubstitutableType,
7091
7116
private:
7092
7117
friend class GenericTypeParamDecl ;
7093
7118
7094
- explicit GenericTypeParamType (RecursiveTypeProperties props)
7095
- : SubstitutableType(TypeKind::GenericTypeParam, nullptr , props) {}
7119
+ explicit GenericTypeParamType (GenericTypeParamDecl *param,
7120
+ RecursiveTypeProperties props);
7121
+
7122
+ // / Note: We have no way to recover an ASTContext from an Identifier, so the
7123
+ // / initialization of an identifier-sugared generic parameter type receives
7124
+ // / the canonical type.
7125
+ explicit GenericTypeParamType (Identifier name, GenericTypeParamType *canType,
7126
+ const ASTContext &ctx);
7096
7127
7097
- explicit GenericTypeParamType (GenericTypeParamKind paramKind,
7098
- unsigned depth, unsigned index, Type valueType,
7128
+ explicit GenericTypeParamType (GenericTypeParamKind paramKind, unsigned depth,
7129
+ unsigned index, Type valueType,
7099
7130
RecursiveTypeProperties props,
7100
- const ASTContext &ctx)
7101
- : SubstitutableType(TypeKind::GenericTypeParam, &ctx, props),
7102
- ParamOrDepthIndex(depth << 16 | index),
7103
- ParamKind(paramKind), ValueType(valueType) {}
7131
+ const ASTContext &ctx);
7104
7132
};
7105
7133
BEGIN_CAN_TYPE_WRAPPER (GenericTypeParamType, SubstitutableType)
7106
7134
static CanGenericTypeParamType getType(unsigned depth, unsigned index,
0 commit comments