@@ -6088,11 +6088,13 @@ Type TypeAliasDecl::getStructuralType() const {
6088
6088
GenericTypeParamDecl::GenericTypeParamDecl (DeclContext *dc, Identifier name,
6089
6089
SourceLoc nameLoc,
6090
6090
SourceLoc specifierLoc,
6091
+ TypeLoc defaultType,
6091
6092
unsigned depth, unsigned index,
6092
6093
GenericTypeParamKind paramKind,
6093
6094
bool isOpaqueType,
6094
6095
TypeRepr *opaqueTypeRepr)
6095
- : TypeDecl(DeclKind::GenericTypeParam, dc, name, nameLoc, {}) {
6096
+ : TypeDecl(DeclKind::GenericTypeParam, dc, name, nameLoc, {}),
6097
+ DefaultType(defaultType) {
6096
6098
ASSERT (!(specifierLoc &&
6097
6099
!(paramKind == GenericTypeParamKind::Pack || paramKind == GenericTypeParamKind::Value)) &&
6098
6100
" 'each' or 'let' keyword imply a parameter pack or value generic parameter" );
@@ -6104,6 +6106,7 @@ GenericTypeParamDecl::GenericTypeParamDecl(DeclContext *dc, Identifier name,
6104
6106
assert (Bits.GenericTypeParamDecl .Index == index && " Truncation" );
6105
6107
Bits.GenericTypeParamDecl .ParamKind = (uint8_t ) paramKind;
6106
6108
Bits.GenericTypeParamDecl .IsOpaqueType = isOpaqueType;
6109
+ Bits.GenericTypeParamDecl .IsDefaultTypeComputed = false ;
6107
6110
6108
6111
if (this ->isOpaqueType ())
6109
6112
*getTrailingObjects<TypeRepr *>() = opaqueTypeRepr;
@@ -6117,8 +6120,9 @@ GenericTypeParamDecl::GenericTypeParamDecl(DeclContext *dc, Identifier name,
6117
6120
6118
6121
GenericTypeParamDecl *GenericTypeParamDecl::create (
6119
6122
DeclContext *dc, Identifier name, SourceLoc nameLoc, SourceLoc specifierLoc,
6120
- unsigned depth, unsigned index, GenericTypeParamKind paramKind,
6121
- bool isOpaqueType, TypeRepr *opaqueTypeRepr) {
6123
+ TypeLoc defaultType, unsigned depth, unsigned index,
6124
+ GenericTypeParamKind paramKind, bool isOpaqueType,
6125
+ TypeRepr *opaqueTypeRepr) {
6122
6126
auto &ctx = dc->getASTContext ();
6123
6127
6124
6128
auto numTypeReprs = 0 ;
@@ -6136,37 +6140,39 @@ GenericTypeParamDecl *GenericTypeParamDecl::create(
6136
6140
numSourceLocs);
6137
6141
auto mem = ctx.Allocate (allocSize, alignof (GenericTypeParamDecl));
6138
6142
return new (mem)
6139
- GenericTypeParamDecl (dc, name, nameLoc, specifierLoc, depth, index ,
6140
- paramKind, isOpaqueType, opaqueTypeRepr);
6143
+ GenericTypeParamDecl (dc, name, nameLoc, specifierLoc, defaultType, depth ,
6144
+ index, paramKind, isOpaqueType, opaqueTypeRepr);
6141
6145
}
6142
6146
6143
6147
GenericTypeParamDecl *GenericTypeParamDecl::createDeserialized (
6144
6148
DeclContext *dc, Identifier name, unsigned depth, unsigned index,
6145
6149
GenericTypeParamKind paramKind, bool isOpaqueType) {
6146
6150
return GenericTypeParamDecl::create (dc, name, SourceLoc (), SourceLoc (),
6147
- depth, index, paramKind,
6151
+ TypeLoc (), depth, index, paramKind,
6148
6152
isOpaqueType,
6149
6153
/* opaqueRepr*/ nullptr );
6150
6154
}
6151
6155
6152
6156
GenericTypeParamDecl *
6153
6157
GenericTypeParamDecl::createParsed (DeclContext *dc, Identifier name,
6154
6158
SourceLoc nameLoc, SourceLoc specifierLoc,
6155
- unsigned index,
6159
+ TypeLoc defaultType, unsigned index,
6156
6160
GenericTypeParamKind paramKind) {
6157
6161
// We always create generic type parameters with an invalid depth.
6158
6162
// Semantic analysis fills in the depth when it processes the generic
6159
6163
// parameter list.
6160
6164
return GenericTypeParamDecl::create (
6161
- dc, name, nameLoc, specifierLoc, GenericTypeParamDecl::InvalidDepth,
6162
- index, paramKind, /* isOpaqueType*/ false , /* opaqueTypeRepr*/ nullptr );
6165
+ dc, name, nameLoc, specifierLoc, defaultType,
6166
+ GenericTypeParamDecl::InvalidDepth, index, paramKind,
6167
+ /* isOpaqueType*/ false , /* opaqueTypeRepr*/ nullptr );
6163
6168
}
6164
6169
6165
6170
GenericTypeParamDecl *GenericTypeParamDecl::createImplicit (
6166
6171
DeclContext *dc, Identifier name, unsigned depth, unsigned index,
6167
6172
GenericTypeParamKind paramKind, TypeRepr *opaqueTypeRepr, SourceLoc nameLoc,
6168
6173
SourceLoc specifierLoc) {
6169
6174
auto *param = GenericTypeParamDecl::create (dc, name, nameLoc, specifierLoc,
6175
+ TypeLoc (),
6170
6176
depth, index, paramKind,
6171
6177
(bool )opaqueTypeRepr,
6172
6178
opaqueTypeRepr);
@@ -6184,6 +6190,24 @@ Type GenericTypeParamDecl::getValueType() const {
6184
6190
return ty ? ty : ErrorType::get (ctx);
6185
6191
}
6186
6192
6193
+ Type GenericTypeParamDecl::getDefaultType () const {
6194
+ auto &ctx = getASTContext ();
6195
+ GenericTypeParamDeclDefaultTypeRequest req (const_cast <GenericTypeParamDecl *>(this ));
6196
+ return evaluateOrDefault (ctx.evaluator , req, Type ());
6197
+ }
6198
+
6199
+ std::optional<Type> GenericTypeParamDecl::getCachedDefaultType () const {
6200
+ if (Bits.GenericTypeParamDecl .IsDefaultTypeComputed )
6201
+ return DefaultType.getType ();
6202
+
6203
+ return std::nullopt ;
6204
+ }
6205
+
6206
+ void GenericTypeParamDecl::setDefaultType (Type ty) {
6207
+ DefaultType.setType (ty);
6208
+ Bits.GenericTypeParamDecl .IsDefaultTypeComputed = true ;
6209
+ }
6210
+
6187
6211
SourceRange GenericTypeParamDecl::getSourceRange () const {
6188
6212
auto startLoc = getNameLoc ();
6189
6213
auto endLoc = getNameLoc ();
0 commit comments