Skip to content

Commit b645e63

Browse files
committed
[AST] NFC: Refactor GenericTypeParamDecl construction
Add distinct overloads for the parser, deserialization and code synthesis.
1 parent 1c9b77e commit b645e63

File tree

13 files changed

+176
-94
lines changed

13 files changed

+176
-94
lines changed

include/swift/AST/Decl.h

Lines changed: 85 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3183,11 +3183,18 @@ class GenericTypeParamDecl final :
31833183
///
31843184
/// \param name The name of the generic parameter.
31853185
/// \param nameLoc The location of the name.
3186+
/// \param depth The generic signature depth.
3187+
/// \param index The index of the parameter in the generic signature.
3188+
/// \param isParameterPack Whether the generic parameter is for a type
3189+
/// parameter pack, denoted by \c <T...>.
3190+
/// \param isOpaqueType Whether the generic parameter is written as an opaque
3191+
/// parameter e.g 'some Collection'.
3192+
/// \param opaqueTypeRepr The TypeRepr of an opaque generic parameter.
3193+
///
31863194
GenericTypeParamDecl(DeclContext *dc, Identifier name, SourceLoc nameLoc,
3187-
bool isParameterPack, unsigned depth, unsigned index,
3188-
bool isOpaqueType, TypeRepr *typeRepr);
3195+
unsigned depth, unsigned index, bool isParameterPack,
3196+
bool isOpaqueType, TypeRepr *opaqueTypeRepr);
31893197

3190-
public:
31913198
/// Construct a new generic type parameter.
31923199
///
31933200
/// \param dc The DeclContext in which the generic type parameter's owner
@@ -3196,17 +3203,85 @@ class GenericTypeParamDecl final :
31963203
///
31973204
/// \param name The name of the generic parameter.
31983205
/// \param nameLoc The location of the name.
3199-
GenericTypeParamDecl(DeclContext *dc, Identifier name, SourceLoc nameLoc,
3200-
bool isParameterPack, unsigned depth, unsigned index)
3201-
: GenericTypeParamDecl(dc, name, nameLoc, isParameterPack, depth, index,
3202-
false, nullptr) { }
3206+
/// \param depth The generic signature depth.
3207+
/// \param index The index of the parameter in the generic signature.
3208+
/// \param isParameterPack Whether the generic parameter is for a type
3209+
/// parameter pack, denoted by \c <T...>.
3210+
/// \param isOpaqueType Whether the generic parameter is written as an opaque
3211+
/// parameter e.g 'some Collection'.
3212+
/// \param opaqueTypeRepr The TypeRepr of an opaque generic parameter.
3213+
///
3214+
static GenericTypeParamDecl *create(DeclContext *dc, Identifier name,
3215+
SourceLoc nameLoc, unsigned depth,
3216+
unsigned index, bool isParameterPack,
3217+
bool isOpaqueType,
3218+
TypeRepr *opaqueTypeRepr);
32033219

3220+
public:
32043221
static const unsigned InvalidDepth = 0xFFFF;
32053222

3223+
/// Construct a new generic type parameter. This should only be used by the
3224+
/// ClangImporter, use \c GenericTypeParamDecl::create[...] instead.
3225+
GenericTypeParamDecl(DeclContext *dc, Identifier name, SourceLoc nameLoc,
3226+
unsigned depth, unsigned index, bool isParameterPack)
3227+
: GenericTypeParamDecl(dc, name, nameLoc, depth, index, isParameterPack,
3228+
/*isOpaqueType*/ false, nullptr) {}
3229+
3230+
/// Construct a deserialized generic type parameter.
3231+
///
3232+
/// \param dc The DeclContext in which the generic type parameter's owner
3233+
/// occurs. This should later be overwritten with the actual declaration
3234+
/// context that owns the type parameter.
3235+
///
3236+
/// \param name The name of the generic parameter.
3237+
/// \param depth The generic signature depth.
3238+
/// \param index The index of the parameter in the generic signature.
3239+
/// \param isParameterPack Whether the generic parameter is for a type
3240+
/// parameter pack, denoted by \c <T...>.
3241+
/// \param isOpaqueType Whether the generic parameter is written as an opaque
3242+
/// parameter e.g 'some Collection'.
3243+
///
3244+
static GenericTypeParamDecl *
3245+
createDeserialized(DeclContext *dc, Identifier name, unsigned depth,
3246+
unsigned index, bool isParameterPack, bool isOpaqueType);
3247+
3248+
/// Construct a new parsed generic type parameter.
3249+
///
3250+
/// \param dc The DeclContext in which the generic type parameter's owner
3251+
/// occurs. This should later be overwritten with the actual declaration
3252+
/// context that owns the type parameter.
3253+
///
3254+
/// \param name The name of the generic parameter.
3255+
/// \param nameLoc The location of the name.
3256+
/// \param index The index of the parameter in the generic signature.
3257+
/// \param isParameterPack Whether the generic parameter is for a type
3258+
/// parameter pack, denoted by \c <T...>.
3259+
///
3260+
static GenericTypeParamDecl *
3261+
createParsed(DeclContext *dc, Identifier name, SourceLoc nameLoc,
3262+
unsigned index, bool isParameterPack);
3263+
3264+
/// Construct a new implicit generic type parameter.
3265+
///
3266+
/// \param dc The DeclContext in which the generic type parameter's owner
3267+
/// occurs. This should later be overwritten with the actual declaration
3268+
/// context that owns the type parameter.
3269+
///
3270+
/// \param name The name of the generic parameter.
3271+
/// \param depth The generic signature depth.
3272+
/// \param index The index of the parameter in the generic signature.
3273+
/// \param isParameterPack Whether the generic parameter is for a type
3274+
/// parameter pack, denoted by \c <T...>.
3275+
/// \param isOpaqueType Whether the generic parameter is written as an opaque
3276+
/// parameter e.g 'some Collection'.
3277+
/// \param opaqueTypeRepr The TypeRepr of an opaque generic parameter.
3278+
/// \param nameLoc The location of the name.
3279+
///
32063280
static GenericTypeParamDecl *
3207-
create(DeclContext *dc, Identifier name, SourceLoc nameLoc,
3208-
bool isParameterPack, unsigned depth, unsigned index,
3209-
bool isOpaqueType, TypeRepr *typeRepr);
3281+
createImplicit(DeclContext *dc, Identifier name, unsigned depth,
3282+
unsigned index, bool isParameterPack = false,
3283+
bool isOpaqueType = false, TypeRepr *opaqueTypeRepr = nullptr,
3284+
SourceLoc nameLoc = SourceLoc());
32103285

32113286
/// The depth of this generic type parameter, i.e., the number of outer
32123287
/// levels of generic parameter lists that enclose this type parameter.

lib/AST/ASTContext.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5459,10 +5459,8 @@ GenericParamList *ASTContext::getSelfGenericParamList(DeclContext *dc) const {
54595459
// Note: we always return a GenericParamList rooted at the first
54605460
// DeclContext this was called with. Since this is just a giant
54615461
// hack for SIL mode, that should be OK.
5462-
auto *selfParam = GenericTypeParamDecl::create(
5463-
dc, Id_Self, SourceLoc(),
5464-
/*isParameterPack=*/false, /*depth=*/0, /*index=*/0,
5465-
/*isOpaqueType=*/false, /*opaqueTypeRepr=*/nullptr);
5462+
auto *selfParam = GenericTypeParamDecl::createImplicit(
5463+
dc, Id_Self, /*depth*/ 0, /*index*/ 0);
54665464

54675465
theParamList = GenericParamList::create(
54685466
const_cast<ASTContext &>(*this), SourceLoc(), {selfParam}, SourceLoc());

lib/AST/Builtins.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,8 @@ static GenericTypeParamDecl*
234234
createGenericParam(ASTContext &ctx, const char *name, unsigned index) {
235235
ModuleDecl *M = ctx.TheBuiltinModule;
236236
Identifier ident = ctx.getIdentifier(name);
237-
auto genericParam = GenericTypeParamDecl::create(
238-
&M->getMainFile(FileUnitKind::Builtin), ident, SourceLoc(),
239-
/*isParameterPack*/ false, 0, index, /*opaque type=*/false, nullptr);
240-
return genericParam;
237+
return GenericTypeParamDecl::createImplicit(
238+
&M->getMainFile(FileUnitKind::Builtin), ident, /*depth*/ 0, index);
241239
}
242240

243241
/// Create a generic parameter list with multiple generic parameters.

lib/AST/Decl.cpp

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4595,19 +4595,19 @@ AbstractTypeParamDecl::getConformingProtocols() const {
45954595
}
45964596

45974597
GenericTypeParamDecl::GenericTypeParamDecl(
4598-
DeclContext *dc, Identifier name, SourceLoc nameLoc,
4599-
bool isParameterPack, unsigned depth, unsigned index,
4600-
bool isOpaqueType, TypeRepr *typeRepr
4601-
) : AbstractTypeParamDecl(DeclKind::GenericTypeParam, dc, name, nameLoc) {
4598+
DeclContext *dc, Identifier name, SourceLoc nameLoc, unsigned depth,
4599+
unsigned index, bool isParameterPack, bool isOpaqueType,
4600+
TypeRepr *opaqueTypeRepr)
4601+
: AbstractTypeParamDecl(DeclKind::GenericTypeParam, dc, name, nameLoc) {
46024602
Bits.GenericTypeParamDecl.Depth = depth;
46034603
assert(Bits.GenericTypeParamDecl.Depth == depth && "Truncation");
46044604
Bits.GenericTypeParamDecl.Index = index;
46054605
assert(Bits.GenericTypeParamDecl.Index == index && "Truncation");
46064606
Bits.GenericTypeParamDecl.ParameterPack = isParameterPack;
46074607
Bits.GenericTypeParamDecl.IsOpaqueType = isOpaqueType;
4608-
assert(isOpaqueType || !typeRepr);
4608+
assert(isOpaqueType || !opaqueTypeRepr);
46094609
if (isOpaqueType)
4610-
*getTrailingObjects<TypeRepr *>() = typeRepr;
4610+
*getTrailingObjects<TypeRepr *>() = opaqueTypeRepr;
46114611

46124612
auto &ctx = dc->getASTContext();
46134613
RecursiveTypeProperties props = RecursiveTypeProperties::HasTypeParameter;
@@ -4617,27 +4617,55 @@ GenericTypeParamDecl::GenericTypeParamDecl(
46174617
setInterfaceType(MetatypeType::get(type, ctx));
46184618
}
46194619

4620-
GenericTypeParamDecl *
4621-
GenericTypeParamDecl::create(
4622-
DeclContext *dc, Identifier name, SourceLoc nameLoc,
4623-
bool isParameterPack, unsigned depth, unsigned index,
4624-
bool isOpaqueType, TypeRepr *typeRepr) {
4625-
ASTContext &ctx = dc->getASTContext();
4626-
auto mem = ctx.Allocate(
4627-
totalSizeToAlloc<TypeRepr *>(isOpaqueType ? 1 : 0),
4628-
alignof(GenericTypeParamDecl));
4629-
return new (mem) GenericTypeParamDecl(
4630-
dc, name, nameLoc, isParameterPack, depth, index, isOpaqueType,
4631-
typeRepr);
4620+
GenericTypeParamDecl *GenericTypeParamDecl::create(
4621+
DeclContext *dc, Identifier name, SourceLoc nameLoc, unsigned depth,
4622+
unsigned index, bool isParameterPack, bool isOpaqueType,
4623+
TypeRepr *opaqueTypeRepr) {
4624+
auto &ctx = dc->getASTContext();
4625+
auto allocSize = totalSizeToAlloc<TypeRepr *>(isOpaqueType ? 1 : 0);
4626+
auto mem = ctx.Allocate(allocSize, alignof(GenericTypeParamDecl));
4627+
return new (mem)
4628+
GenericTypeParamDecl(dc, name, nameLoc, depth, index,
4629+
isParameterPack, isOpaqueType, opaqueTypeRepr);
46324630
}
46334631

4632+
GenericTypeParamDecl *GenericTypeParamDecl::createDeserialized(
4633+
DeclContext *dc, Identifier name, unsigned depth, unsigned index,
4634+
bool isParameterPack, bool isOpaqueType) {
4635+
return GenericTypeParamDecl::create(dc, name, SourceLoc(), depth, index,
4636+
isParameterPack, isOpaqueType,
4637+
/*opaqueRepr*/ nullptr);
4638+
}
4639+
4640+
GenericTypeParamDecl *
4641+
GenericTypeParamDecl::createParsed(DeclContext *dc, Identifier name,
4642+
SourceLoc nameLoc, unsigned index,
4643+
bool isParameterPack) {
4644+
// We always create generic type parameters with an invalid depth.
4645+
// Semantic analysis fills in the depth when it processes the generic
4646+
// parameter list.
4647+
return GenericTypeParamDecl::create(
4648+
dc, name, nameLoc, GenericTypeParamDecl::InvalidDepth, index,
4649+
isParameterPack, /*isOpaqueType*/ false, /*opaqueTypeRepr*/ nullptr);
4650+
}
4651+
4652+
GenericTypeParamDecl *GenericTypeParamDecl::createImplicit(
4653+
DeclContext *dc, Identifier name, unsigned depth, unsigned index,
4654+
bool isParameterPack, bool isOpaqueType, TypeRepr *opaqueTypeRepr,
4655+
SourceLoc nameLoc) {
4656+
auto *param = GenericTypeParamDecl::create(dc, name, nameLoc, depth, index,
4657+
isParameterPack, isOpaqueType,
4658+
opaqueTypeRepr);
4659+
param->setImplicit();
4660+
return param;
4661+
}
46344662

46354663
SourceRange GenericTypeParamDecl::getSourceRange() const {
46364664
SourceLoc endLoc = getNameLoc();
46374665

4638-
if (!getInherited().empty()) {
4666+
if (!getInherited().empty())
46394667
endLoc = getInherited().back().getSourceRange().End;
4640-
}
4668+
46414669
return SourceRange(getNameLoc(), endLoc);
46424670
}
46434671

lib/AST/GenericParamList.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,10 @@ GenericParamList::clone(DeclContext *dc) const {
7373
auto &ctx = dc->getASTContext();
7474
SmallVector<GenericTypeParamDecl *, 2> params;
7575
for (auto param : getParams()) {
76-
auto *newParam = GenericTypeParamDecl::create(
77-
dc, param->getName(), SourceLoc(), param->isParameterPack(),
78-
GenericTypeParamDecl::InvalidDepth, param->getIndex(),
79-
param->isOpaqueType(), param->getOpaqueTypeRepr());
80-
newParam->setImplicit(true);
76+
auto *newParam = GenericTypeParamDecl::createImplicit(
77+
dc, param->getName(), GenericTypeParamDecl::InvalidDepth,
78+
param->getIndex(), param->isParameterPack(), param->isOpaqueType(),
79+
param->getOpaqueTypeRepr());
8180
params.push_back(newParam);
8281
}
8382

lib/AST/NameLookup.cpp

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2870,11 +2870,9 @@ createOpaqueParameterGenericParams(GenericContext *genericContext, GenericParamL
28702870
for (auto repr : typeReprs) {
28712871

28722872
// Allocate a new generic parameter to represent this opaque type.
2873-
auto gp = GenericTypeParamDecl::create(
2874-
dc, Identifier(), SourceLoc(), /*isParameterPack=*/false,
2875-
GenericTypeParamDecl::InvalidDepth, index++, /*isOpaqueType=*/true,
2876-
repr);
2877-
gp->setImplicit();
2873+
auto *gp = GenericTypeParamDecl::createImplicit(
2874+
dc, Identifier(), GenericTypeParamDecl::InvalidDepth, index++,
2875+
/*isParameterPack*/ false, /*isOpaqueType*/ true, repr);
28782876

28792877
// Use the underlying constraint as the constraint on the generic parameter.
28802878
// The underlying constraint is only present for OpaqueReturnTypeReprs
@@ -2902,10 +2900,9 @@ GenericParamListRequest::evaluate(Evaluator &evaluator, GenericContext *value) c
29022900
auto &ctx = value->getASTContext();
29032901

29042902
// Builtin.TheTupleType has a single pack generic parameter: <Elements...>
2905-
auto *genericParam = GenericTypeParamDecl::create(
2906-
tupleDecl->getDeclContext(), ctx.Id_Elements, SourceLoc(),
2907-
/*isParameterPack=*/true, /*depth=*/0, /*depth=*/0,
2908-
/*isOpaqueType=*/false, /*opaqueTypeRepr=*/nullptr);
2903+
auto *genericParam = GenericTypeParamDecl::createImplicit(
2904+
tupleDecl->getDeclContext(), ctx.Id_Elements, /*depth*/ 0, /*index*/ 0,
2905+
/*isParameterPack*/ true);
29092906

29102907
return GenericParamList::create(ctx, SourceLoc(), genericParam,
29112908
SourceLoc());
@@ -2945,10 +2942,8 @@ GenericParamListRequest::evaluate(Evaluator &evaluator, GenericContext *value) c
29452942
// The generic parameter 'Self'.
29462943
auto &ctx = value->getASTContext();
29472944
auto selfId = ctx.Id_Self;
2948-
auto selfDecl = GenericTypeParamDecl::create(
2949-
proto, selfId, SourceLoc(), /*isParameterPack=*/false,
2950-
/*depth=*/0, /*index=*/0, /*opaque type=*/false,
2951-
/*opaque type repr=*/nullptr);
2945+
auto selfDecl = GenericTypeParamDecl::createImplicit(
2946+
proto, selfId, /*depth*/ 0, /*index*/ 0);
29522947
auto protoType = proto->getDeclaredInterfaceType();
29532948
InheritedEntry selfInherited[1] = {
29542949
InheritedEntry(TypeLoc::withoutLoc(protoType)) };

lib/ClangImporter/ImportDecl.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3027,8 +3027,9 @@ namespace {
30273027

30283028
auto *typeParam = Impl.createDeclWithClangNode<GenericTypeParamDecl>(
30293029
param, AccessLevel::Public, dc,
3030-
Impl.SwiftContext.getIdentifier(param->getName()), SourceLoc(),
3031-
/*isParameterPack*/ false, /*depth*/ 0, /*index*/ i);
3030+
Impl.SwiftContext.getIdentifier(param->getName()),
3031+
/*nameLoc*/ SourceLoc(),
3032+
/*depth*/ 0, /*index*/ i, /*isParameterPack*/ false);
30323033
templateParams.push_back(typeParam);
30333034
(void)++i;
30343035
}
@@ -3477,8 +3478,8 @@ namespace {
34773478
param, AccessLevel::Public, dc,
34783479
Impl.SwiftContext.getIdentifier(param->getName()),
34793480
Impl.importSourceLoc(param->getLocation()),
3480-
/*isParameterPack*/ false, /*depth*/ 0,
3481-
/*index*/ genericParams.size());
3481+
/*depth*/ 0, /*index*/ genericParams.size(),
3482+
/*isParameterPack*/ false);
34823483
genericParams.push_back(genericParamDecl);
34833484
}
34843485
auto genericParamList = GenericParamList::create(
@@ -6590,7 +6591,7 @@ Optional<GenericParamList *> SwiftDeclConverter::importObjCGenericParams(
65906591
objcGenericParam, AccessLevel::Public, dc,
65916592
Impl.SwiftContext.getIdentifier(objcGenericParam->getName()),
65926593
Impl.importSourceLoc(objcGenericParam->getLocation()),
6593-
/*isParameterPack*/ false, /*depth*/ 0, /*index*/ genericParams.size());
6594+
/*depth*/ 0, /*index*/ genericParams.size(), /*isParameterPack*/ false);
65946595
// NOTE: depth is always 0 for ObjC generic type arguments, since only
65956596
// classes may have generic types in ObjC, and ObjC classes cannot be
65966597
// nested.

lib/Parse/ParseGeneric.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,11 @@ Parser::parseGenericParametersBeforeWhere(SourceLoc LAngleLoc,
100100
Inherited.push_back({Ty.get()});
101101
}
102102

103-
// We always create generic type parameters with an invalid depth.
104-
// Semantic analysis fills in the depth when it processes the generic
105-
// parameter list.
106103
const bool isParameterPack =
107104
attributes.getAttribute<TypeSequenceAttr>() != nullptr;
108-
auto Param = GenericTypeParamDecl::create(
109-
CurDeclContext, Name, NameLoc, isParameterPack,
110-
GenericTypeParamDecl::InvalidDepth, GenericParams.size(),
111-
/*isOpaqueType=*/false, /*opaqueTypeRepr=*/nullptr);
105+
auto *Param = GenericTypeParamDecl::createParsed(
106+
CurDeclContext, Name, NameLoc, /*index*/ GenericParams.size(),
107+
isParameterPack);
112108
if (!Inherited.empty())
113109
Param->setInherited(Context.AllocateCopy(Inherited));
114110
GenericParams.push_back(Param);

lib/SILOptimizer/Differentiation/LinearMapInfo.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,10 @@ static GenericParamList *cloneGenericParameters(ASTContext &ctx,
3838
CanGenericSignature sig) {
3939
SmallVector<GenericTypeParamDecl *, 2> clonedParams;
4040
for (auto paramType : sig.getGenericParams()) {
41-
auto clonedParam = GenericTypeParamDecl::create(
42-
dc, paramType->getName(), SourceLoc(), paramType->isParameterPack(),
43-
paramType->getDepth(), paramType->getIndex(),
44-
/*isOpaqueType=*/false, /*opaqueTypeRepr=*/nullptr);
41+
auto *clonedParam = GenericTypeParamDecl::createImplicit(
42+
dc, paramType->getName(), paramType->getDepth(), paramType->getIndex(),
43+
paramType->isParameterPack());
4544
clonedParam->setDeclContext(dc);
46-
clonedParam->setImplicit(true);
4745
clonedParams.push_back(clonedParam);
4846
}
4947
return GenericParamList::create(ctx, SourceLoc(), clonedParams, SourceLoc());

lib/Sema/CodeSynthesis.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -585,10 +585,9 @@ createDesignatedInitOverrideGenericParams(ASTContext &ctx,
585585

586586
SmallVector<GenericTypeParamDecl *, 4> newParams;
587587
for (auto *param : genericParams->getParams()) {
588-
auto *newParam = GenericTypeParamDecl::create(
589-
classDecl, param->getName(), SourceLoc(), param->isParameterPack(),
590-
depth, param->getIndex(), param->isOpaqueType(),
591-
/*opaqueTypeRepr=*/nullptr);
588+
auto *newParam = GenericTypeParamDecl::createImplicit(
589+
classDecl, param->getName(), depth, param->getIndex(),
590+
param->isParameterPack(), param->isOpaqueType());
592591
newParams.push_back(newParam);
593592
}
594593

lib/Sema/DerivedConformanceDistributedActor.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,8 @@ static FuncDecl* createLocalFunc_doInvokeOnReturn(
214214
// <R: Self.SerializationRequirement>
215215
// We create the generic param at invalid depth, which means it'll be filled
216216
// by semantic analysis.
217-
auto resultGenericParamDecl = GenericTypeParamDecl::create(
218-
parentFunc, C.getIdentifier("R"), sloc, /*isParameterPack=*/false,
219-
/*depth=*/0, /*index=*/0,
220-
/*isOpaqueType=*/false,
221-
/*opaqueTypeRepr=*/nullptr);
217+
auto *resultGenericParamDecl = GenericTypeParamDecl::createImplicit(
218+
parentFunc, C.getIdentifier("R"), /*depth*/ 0, /*index*/ 0);
222219
GenericParamList *doInvokeGenericParamList =
223220
GenericParamList::create(C, sloc, {resultGenericParamDecl}, sloc);
224221

0 commit comments

Comments
 (0)