@@ -6088,11 +6088,13 @@ Type TypeAliasDecl::getStructuralType() const {
60886088GenericTypeParamDecl::GenericTypeParamDecl (DeclContext *dc, Identifier name,
60896089 SourceLoc nameLoc,
60906090 SourceLoc specifierLoc,
6091+ TypeLoc defaultType,
60916092 unsigned depth, unsigned index,
60926093 GenericTypeParamKind paramKind,
60936094 bool isOpaqueType,
60946095 TypeRepr *opaqueTypeRepr)
6095- : TypeDecl(DeclKind::GenericTypeParam, dc, name, nameLoc, {}) {
6096+ : TypeDecl(DeclKind::GenericTypeParam, dc, name, nameLoc, {}),
6097+ DefaultType(defaultType) {
60966098 ASSERT (!(specifierLoc &&
60976099 !(paramKind == GenericTypeParamKind::Pack || paramKind == GenericTypeParamKind::Value)) &&
60986100 " 'each' or 'let' keyword imply a parameter pack or value generic parameter" );
@@ -6104,6 +6106,7 @@ GenericTypeParamDecl::GenericTypeParamDecl(DeclContext *dc, Identifier name,
61046106 assert (Bits.GenericTypeParamDecl .Index == index && " Truncation" );
61056107 Bits.GenericTypeParamDecl .ParamKind = (uint8_t ) paramKind;
61066108 Bits.GenericTypeParamDecl .IsOpaqueType = isOpaqueType;
6109+ Bits.GenericTypeParamDecl .IsDefaultTypeComputed = false ;
61076110
61086111 if (this ->isOpaqueType ())
61096112 *getTrailingObjects<TypeRepr *>() = opaqueTypeRepr;
@@ -6117,8 +6120,9 @@ GenericTypeParamDecl::GenericTypeParamDecl(DeclContext *dc, Identifier name,
61176120
61186121GenericTypeParamDecl *GenericTypeParamDecl::create (
61196122 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) {
61226126 auto &ctx = dc->getASTContext ();
61236127
61246128 auto numTypeReprs = 0 ;
@@ -6136,37 +6140,39 @@ GenericTypeParamDecl *GenericTypeParamDecl::create(
61366140 numSourceLocs);
61376141 auto mem = ctx.Allocate (allocSize, alignof (GenericTypeParamDecl));
61386142 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);
61416145}
61426146
61436147GenericTypeParamDecl *GenericTypeParamDecl::createDeserialized (
61446148 DeclContext *dc, Identifier name, unsigned depth, unsigned index,
61456149 GenericTypeParamKind paramKind, bool isOpaqueType) {
61466150 return GenericTypeParamDecl::create (dc, name, SourceLoc (), SourceLoc (),
6147- depth, index, paramKind,
6151+ TypeLoc (), depth, index, paramKind,
61486152 isOpaqueType,
61496153 /* opaqueRepr*/ nullptr );
61506154}
61516155
61526156GenericTypeParamDecl *
61536157GenericTypeParamDecl::createParsed (DeclContext *dc, Identifier name,
61546158 SourceLoc nameLoc, SourceLoc specifierLoc,
6155- unsigned index,
6159+ TypeLoc defaultType, unsigned index,
61566160 GenericTypeParamKind paramKind) {
61576161 // We always create generic type parameters with an invalid depth.
61586162 // Semantic analysis fills in the depth when it processes the generic
61596163 // parameter list.
61606164 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 );
61636168}
61646169
61656170GenericTypeParamDecl *GenericTypeParamDecl::createImplicit (
61666171 DeclContext *dc, Identifier name, unsigned depth, unsigned index,
61676172 GenericTypeParamKind paramKind, TypeRepr *opaqueTypeRepr, SourceLoc nameLoc,
61686173 SourceLoc specifierLoc) {
61696174 auto *param = GenericTypeParamDecl::create (dc, name, nameLoc, specifierLoc,
6175+ TypeLoc (),
61706176 depth, index, paramKind,
61716177 (bool )opaqueTypeRepr,
61726178 opaqueTypeRepr);
@@ -6184,6 +6190,24 @@ Type GenericTypeParamDecl::getValueType() const {
61846190 return ty ? ty : ErrorType::get (ctx);
61856191}
61866192
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+
61876211SourceRange GenericTypeParamDecl::getSourceRange () const {
61886212 auto startLoc = getNameLoc ();
61896213 auto endLoc = getNameLoc ();
0 commit comments