Skip to content

Commit e1fc291

Browse files
committed
AST: Add GenericTypeParamType::getWeight()
This is currently always 0, but can be set to 1.
1 parent 115ba5c commit e1fc291

File tree

7 files changed

+68
-29
lines changed

7 files changed

+68
-29
lines changed

include/swift/AST/Types.h

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7254,9 +7254,10 @@ class GenericTypeParamType : public SubstitutableType,
72547254
Identifier Name;
72557255
};
72567256

7257-
unsigned Depth : 15;
72587257
unsigned IsDecl : 1;
7259-
unsigned Index : 16;
7258+
unsigned Depth : 15;
7259+
unsigned Weight : 1;
7260+
unsigned Index : 15;
72607261

72617262
/// The kind of generic type parameter this is.
72627263
GenericTypeParamKind ParamKind;
@@ -7281,15 +7282,21 @@ class GenericTypeParamType : public SubstitutableType,
72817282
Type valueType, const ASTContext &ctx);
72827283

72837284
/// Retrieve a canonical generic type parameter with the given kind, depth,
7284-
/// index, and optional value type.
7285+
/// index, weight, and optional value type.
72857286
static GenericTypeParamType *get(GenericTypeParamKind paramKind,
7286-
unsigned depth, unsigned index,
7287+
unsigned depth, unsigned index, unsigned weight,
72877288
Type valueType, const ASTContext &ctx);
72887289

7289-
/// Retrieve a canonical generic type parameter at the given depth and index.
7290+
/// Retrieve a canonical generic type parameter at the given depth and index,
7291+
/// with weight 0.
72907292
static GenericTypeParamType *getType(unsigned depth, unsigned index,
72917293
const ASTContext &ctx);
72927294

7295+
/// Retrieve a canonical generic type parameter at the given depth and index
7296+
/// for an opaque result type, so with weight 1.
7297+
static GenericTypeParamType *getOpaqueResultType(unsigned depth, unsigned index,
7298+
const ASTContext &ctx);
7299+
72937300
/// Retrieve a canonical generic parameter pack at the given depth and index.
72947301
static GenericTypeParamType *getPack(unsigned depth, unsigned index,
72957302
const ASTContext &ctx);
@@ -7345,6 +7352,14 @@ class GenericTypeParamType : public SubstitutableType,
73457352
return Index;
73467353
}
73477354

7355+
/// The weight of this generic parameter in the type parameter order.
7356+
///
7357+
/// Opaque result types have weight 1, while all other generic parameters
7358+
/// have weight 0.
7359+
unsigned getWeight() const {
7360+
return Weight;
7361+
}
7362+
73487363
/// Returns \c true if this type parameter is declared as a pack.
73497364
///
73507365
/// \code
@@ -7373,15 +7388,17 @@ class GenericTypeParamType : public SubstitutableType,
73737388
// which don't store an identifier we'll go create a tau based form. We
73747389
// really want to just plumb down the null Identifier because that's what's
73757390
// inside the cache.
7376-
Profile(ID, getParamKind(), getDepth(), getIndex(), getValueType(),
7377-
Name);
7391+
Profile(ID, getParamKind(), getDepth(), getIndex(), getWeight(),
7392+
getValueType(), Name);
73787393
}
73797394
static void Profile(llvm::FoldingSetNodeID &ID,
73807395
GenericTypeParamKind paramKind, unsigned depth,
7381-
unsigned index, Type valueType, Identifier name) {
7396+
unsigned index, unsigned weight, Type valueType,
7397+
Identifier name) {
73827398
ID.AddInteger((uint8_t)paramKind);
73837399
ID.AddInteger(depth);
73847400
ID.AddInteger(index);
7401+
ID.AddInteger(weight);
73857402
ID.AddPointer(valueType.getPointer());
73867403
ID.AddPointer(name.get());
73877404
}
@@ -7404,7 +7421,7 @@ class GenericTypeParamType : public SubstitutableType,
74047421
const ASTContext &ctx);
74057422

74067423
explicit GenericTypeParamType(GenericTypeParamKind paramKind, unsigned depth,
7407-
unsigned index, Type valueType,
7424+
unsigned index, unsigned weight, Type valueType,
74087425
RecursiveTypeProperties props,
74097426
const ASTContext &ctx);
74107427
};
@@ -7414,6 +7431,11 @@ static CanGenericTypeParamType getType(unsigned depth, unsigned index,
74147431
return CanGenericTypeParamType(
74157432
GenericTypeParamType::getType(depth, index, C));
74167433
}
7434+
static CanGenericTypeParamType getOpaqueResultType(unsigned depth, unsigned index,
7435+
const ASTContext &C) {
7436+
return CanGenericTypeParamType(
7437+
GenericTypeParamType::getOpaqueResultType(depth, index, C));
7438+
}
74177439
END_CAN_TYPE_WRAPPER(GenericTypeParamType, SubstitutableType)
74187440

74197441
/// A type that refers to a member type of some type that is dependent on a

lib/AST/ASTContext.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5099,8 +5099,8 @@ GenericTypeParamType *GenericTypeParamType::get(Identifier name,
50995099
Type valueType,
51005100
const ASTContext &ctx) {
51015101
llvm::FoldingSetNodeID id;
5102-
GenericTypeParamType::Profile(id, paramKind, depth, index, valueType,
5103-
name);
5102+
GenericTypeParamType::Profile(id, paramKind, depth, index, /*weight=*/0,
5103+
valueType, name);
51045104

51055105
void *insertPos;
51065106
if (auto gpTy = ctx.getImpl().GenericParamTypes.FindNodeOrInsertPos(id, insertPos))
@@ -5110,8 +5110,8 @@ GenericTypeParamType *GenericTypeParamType::get(Identifier name,
51105110
if (paramKind == GenericTypeParamKind::Pack)
51115111
props |= RecursiveTypeProperties::HasParameterPack;
51125112

5113-
auto canType = GenericTypeParamType::get(paramKind, depth, index, valueType,
5114-
ctx);
5113+
auto canType = GenericTypeParamType::get(paramKind, depth, index, /*weight=*/0,
5114+
valueType, ctx);
51155115

51165116
auto result = new (ctx, AllocationArena::Permanent)
51175117
GenericTypeParamType(name, canType, ctx);
@@ -5130,10 +5130,10 @@ GenericTypeParamType *GenericTypeParamType::get(GenericTypeParamDecl *param) {
51305130

51315131
GenericTypeParamType *GenericTypeParamType::get(GenericTypeParamKind paramKind,
51325132
unsigned depth, unsigned index,
5133-
Type valueType,
5133+
unsigned weight, Type valueType,
51345134
const ASTContext &ctx) {
51355135
llvm::FoldingSetNodeID id;
5136-
GenericTypeParamType::Profile(id, paramKind, depth, index, valueType,
5136+
GenericTypeParamType::Profile(id, paramKind, depth, index, weight, valueType,
51375137
Identifier());
51385138

51395139
void *insertPos;
@@ -5145,7 +5145,7 @@ GenericTypeParamType *GenericTypeParamType::get(GenericTypeParamKind paramKind,
51455145
props |= RecursiveTypeProperties::HasParameterPack;
51465146

51475147
auto result = new (ctx, AllocationArena::Permanent)
5148-
GenericTypeParamType(paramKind, depth, index, valueType, props, ctx);
5148+
GenericTypeParamType(paramKind, depth, index, weight, valueType, props, ctx);
51495149
ctx.getImpl().GenericParamTypes.InsertNode(result, insertPos);
51505150
return result;
51515151
}
@@ -5154,22 +5154,29 @@ GenericTypeParamType *GenericTypeParamType::getType(unsigned depth,
51545154
unsigned index,
51555155
const ASTContext &ctx) {
51565156
return GenericTypeParamType::get(GenericTypeParamKind::Type, depth, index,
5157-
/*valueType*/ Type(), ctx);
5157+
/*weight=*/0, /*valueType=*/Type(), ctx);
5158+
}
5159+
5160+
GenericTypeParamType *GenericTypeParamType::getOpaqueResultType(unsigned depth,
5161+
unsigned index,
5162+
const ASTContext &ctx) {
5163+
return GenericTypeParamType::get(GenericTypeParamKind::Type, depth, index,
5164+
/*weight=*/1, /*valueType=*/Type(), ctx);
51585165
}
51595166

51605167
GenericTypeParamType *GenericTypeParamType::getPack(unsigned depth,
51615168
unsigned index,
51625169
const ASTContext &ctx) {
51635170
return GenericTypeParamType::get(GenericTypeParamKind::Pack, depth, index,
5164-
/*valueType*/ Type(), ctx);
5171+
/*weight=*/0, /*valueType=*/Type(), ctx);
51655172
}
51665173

51675174
GenericTypeParamType *GenericTypeParamType::getValue(unsigned depth,
51685175
unsigned index,
51695176
Type valueType,
51705177
const ASTContext &ctx) {
51715178
return GenericTypeParamType::get(GenericTypeParamKind::Value, depth, index,
5172-
valueType, ctx);
5179+
/*weight=*/0, valueType, ctx);
51735180
}
51745181

51755182
ArrayRef<GenericTypeParamType *>

lib/AST/Type.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,7 +1736,8 @@ CanType TypeBase::computeCanonicalType() {
17361736
auto &C = gpDecl->getASTContext();
17371737
Result =
17381738
GenericTypeParamType::get(gp->getParamKind(), gp->getDepth(),
1739-
gp->getIndex(), gp->getValueType(),
1739+
gp->getIndex(), gp->getWeight(),
1740+
gp->getValueType(),
17401741
C);
17411742
break;
17421743
}
@@ -2081,8 +2082,9 @@ GenericTypeParamType::GenericTypeParamType(GenericTypeParamDecl *param,
20812082
: SubstitutableType(TypeKind::GenericTypeParam, nullptr, props),
20822083
Decl(param) {
20832084
ASSERT(param->getDepth() != GenericTypeParamDecl::InvalidDepth);
2084-
Depth = param->getDepth();
20852085
IsDecl = true;
2086+
Depth = param->getDepth();
2087+
Weight = 0;
20862088
Index = param->getIndex();
20872089
ParamKind = param->getParamKind();
20882090
ValueType = param->getValueType();
@@ -2095,8 +2097,9 @@ GenericTypeParamType::GenericTypeParamType(Identifier name,
20952097
canType->getRecursiveProperties()),
20962098
Decl(nullptr) {
20972099
Name = name;
2098-
Depth = canType->getDepth();
20992100
IsDecl = false;
2101+
Depth = canType->getDepth();
2102+
Weight = canType->getWeight();
21002103
Index = canType->getIndex();
21012104
ParamKind = canType->getParamKind();
21022105
ValueType = canType->getValueType();
@@ -2106,13 +2109,14 @@ GenericTypeParamType::GenericTypeParamType(Identifier name,
21062109

21072110
GenericTypeParamType::GenericTypeParamType(GenericTypeParamKind paramKind,
21082111
unsigned depth, unsigned index,
2109-
Type valueType,
2112+
unsigned weight, Type valueType,
21102113
RecursiveTypeProperties props,
21112114
const ASTContext &ctx)
21122115
: SubstitutableType(TypeKind::GenericTypeParam, &ctx, props),
21132116
Decl(nullptr) {
2114-
Depth = depth;
21152117
IsDecl = false;
2118+
Depth = depth;
2119+
Weight = weight;
21162120
Index = index;
21172121
ParamKind = paramKind;
21182122
ValueType = valueType;
@@ -2167,6 +2171,7 @@ GenericTypeParamType *GenericTypeParamType::withDepth(unsigned depth) const {
21672171
return GenericTypeParamType::get(getParamKind(),
21682172
depth,
21692173
getIndex(),
2174+
getWeight(),
21702175
getValueType(),
21712176
getASTContext());
21722177
}

lib/SIL/IR/AbstractionPattern.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2423,7 +2423,8 @@ class SubstFunctionTypePatternVisitor
24232423
paramKind = GenericTypeParamKind::Pack;
24242424
}
24252425

2426-
auto gp = GenericTypeParamType::get(paramKind, 0, paramIndex, Type(),
2426+
auto gp = GenericTypeParamType::get(paramKind, /*depth=*/0, paramIndex,
2427+
/*weight=*/0, /*valueType=*/Type(),
24272428
TC.Context);
24282429
substGenericParams.push_back(gp);
24292430

lib/Serialization/Deserialization.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7621,12 +7621,13 @@ DESERIALIZE_TYPE(GENERIC_TYPE_PARAM_TYPE)(
76217621
unsigned rawParamKind;
76227622
bool hasDecl;
76237623
unsigned depth;
7624+
unsigned weight;
76247625
unsigned index;
76257626
DeclID declOrIdentifier;
76267627
TypeID valueTypeID;
76277628

76287629
decls_block::GenericTypeParamTypeLayout::readRecord(
7629-
scratch, rawParamKind, hasDecl, depth, index, declOrIdentifier,
7630+
scratch, rawParamKind, hasDecl, depth, weight, index, declOrIdentifier,
76307631
valueTypeID);
76317632

76327633
auto paramKind = getActualParamKind(rawParamKind);
@@ -7655,10 +7656,11 @@ DESERIALIZE_TYPE(GENERIC_TYPE_PARAM_TYPE)(
76557656
return valueType.takeError();
76567657

76577658
if (declOrIdentifier == 0) {
7658-
return GenericTypeParamType::get(*paramKind, depth, index, *valueType,
7659+
return GenericTypeParamType::get(*paramKind, depth, index, weight, *valueType,
76597660
MF.getContext());
76607661
}
76617662

7663+
ASSERT(weight == 0);
76627664
auto name = MF.getDeclBaseName(declOrIdentifier).getIdentifier();
76637665
return GenericTypeParamType::get(name, *paramKind, depth, index, *valueType,
76647666
MF.getContext());

lib/Serialization/ModuleFormat.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
5858
/// describe what change you made. The content of this comment isn't important;
5959
/// it just ensures a conflict if two people change the module format.
6060
/// Don't worry about adhering to the 80-column limit for this line.
61-
const uint16_t SWIFTMODULE_VERSION_MINOR = 944; // specialized witness tables
61+
const uint16_t SWIFTMODULE_VERSION_MINOR = 945; // generic parameter weight
6262

6363
/// A standard hash seed used for all string hashes in a serialized module.
6464
///
@@ -1290,7 +1290,8 @@ namespace decls_block {
12901290
GenericParamKindField, // param kind
12911291
BCFixed<1>, // has decl?
12921292
BCFixed<15>, // depth
1293-
BCFixed<16>, // index
1293+
BCFixed<1>, // weight
1294+
BCFixed<15>, // index
12941295
DeclIDField, // generic type parameter decl or identifier
12951296
TypeIDField // value type (if param kind == Value)
12961297
);

lib/Serialization/Serialization.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5812,6 +5812,7 @@ class Serializer::TypeSerializer : public TypeVisitor<TypeSerializer> {
58125812
paramKind,
58135813
hasDecl,
58145814
genericParam->getDepth(),
5815+
genericParam->getWeight(),
58155816
genericParam->getIndex(),
58165817
declOrIdentifier,
58175818
valueTypeID);

0 commit comments

Comments
 (0)