@@ -1251,75 +1251,80 @@ makeBitFieldAccessors(ClangImporter::Implementation &Impl,
1251
1251
return { getterDecl, setterDecl };
1252
1252
}
1253
1253
1254
- // / Create a default constructor that initializes a struct to zero.
1255
- static ConstructorDecl *
1256
- createDefaultConstructor (ClangImporter::Implementation &Impl,
1257
- StructDecl *structDecl) {
1258
- auto &context = Impl.SwiftContext ;
1259
-
1260
- auto emptyPL = ParameterList::createEmpty (context);
1261
-
1262
- // Create the constructor.
1263
- DeclName name (context, DeclBaseName::createConstructor (), emptyPL);
1264
- auto constructor = new (context) ConstructorDecl (
1265
- name, structDecl->getLoc (), OTK_None, /* FailabilityLoc=*/ SourceLoc (),
1266
- /* Throws=*/ false , /* ThrowsLoc=*/ SourceLoc (), emptyPL,
1267
- /* GenericParams=*/ nullptr , structDecl);
1268
-
1269
- // Set the constructor's type.
1270
- constructor->computeType ();
1271
- constructor->setValidationToChecked ();
1272
-
1273
- constructor->setAccess (AccessLevel::Public);
1274
-
1275
- // Mark the constructor transparent so that we inline it away completely.
1276
- constructor->getAttrs ().add (new (context) TransparentAttr (/* implicit*/ true ));
1277
-
1278
- if (Impl.hasFinishedTypeChecking ())
1279
- return constructor;
1254
+ // / Synthesize the body for an struct default initializer.
1255
+ static std::pair<BraceStmt *, bool >
1256
+ synthesizeStructDefaultConstructorBody (AbstractFunctionDecl *afd,
1257
+ void *context) {
1258
+ auto constructor = cast<ConstructorDecl>(afd);
1259
+ ASTContext &ctx = constructor->getASTContext ();
1260
+ auto structDecl = static_cast <StructDecl *>(context);
1280
1261
1281
1262
// Use a builtin to produce a zero initializer, and assign it to self.
1282
1263
1283
1264
// Construct the left-hand reference to self.
1284
1265
auto *selfDecl = constructor->getImplicitSelfDecl ();
1285
- Expr *lhs = new (context) DeclRefExpr (selfDecl,
1286
- DeclNameLoc (), /* Implicit=*/ true );
1266
+ Expr *lhs = new (ctx) DeclRefExpr (selfDecl, DeclNameLoc (), /* Implicit=*/ true );
1287
1267
auto selfType = structDecl->getDeclaredInterfaceType ();
1288
1268
lhs->setType (LValueType::get (selfType));
1289
1269
1290
- auto emptyTuple = TupleType::getEmpty (context );
1270
+ auto emptyTuple = TupleType::getEmpty (ctx );
1291
1271
1292
1272
// Construct the right-hand call to Builtin.zeroInitializer.
1293
- Identifier zeroInitID = context .getIdentifier (" zeroInitializer" );
1273
+ Identifier zeroInitID = ctx .getIdentifier (" zeroInitializer" );
1294
1274
auto zeroInitializerFunc =
1295
- cast<FuncDecl>(getBuiltinValueDecl (context , zeroInitID));
1275
+ cast<FuncDecl>(getBuiltinValueDecl (ctx , zeroInitID));
1296
1276
SubstitutionMap subMap =
1297
1277
SubstitutionMap::get (zeroInitializerFunc->getGenericSignature (),
1298
1278
llvm::makeArrayRef (selfType), { });
1299
1279
ConcreteDeclRef concreteDeclRef (zeroInitializerFunc, subMap);
1300
1280
auto zeroInitializerRef =
1301
- new (context) DeclRefExpr (concreteDeclRef, DeclNameLoc (),
1302
- /* implicit*/ true );
1281
+ new (ctx) DeclRefExpr (concreteDeclRef, DeclNameLoc (), /* implicit*/ true );
1303
1282
zeroInitializerRef->setType (FunctionType::get ({}, selfType));
1304
1283
1305
- auto call = CallExpr::createImplicit (context , zeroInitializerRef, {}, {});
1284
+ auto call = CallExpr::createImplicit (ctx , zeroInitializerRef, {}, {});
1306
1285
call->setType (selfType);
1307
1286
call->setThrows (false );
1308
1287
1309
- auto assign = new (context) AssignExpr (lhs, SourceLoc (), call,
1310
- /* implicit*/ true );
1288
+ auto assign = new (ctx) AssignExpr (lhs, SourceLoc (), call, /* implicit*/ true );
1311
1289
assign->setType (emptyTuple);
1312
1290
1313
- auto result = TupleExpr::createEmpty (context , SourceLoc (), SourceLoc (),
1291
+ auto result = TupleExpr::createEmpty (ctx , SourceLoc (), SourceLoc (),
1314
1292
/* Implicit=*/ true );
1315
1293
result->setType (emptyTuple);
1316
1294
1317
- auto ret = new (context ) ReturnStmt (SourceLoc (), result, /* Implicit=*/ true );
1295
+ auto ret = new (ctx ) ReturnStmt (SourceLoc (), result, /* Implicit=*/ true );
1318
1296
1319
1297
// Create the function body.
1320
- auto body = BraceStmt::create (context, SourceLoc (), {assign, ret},
1321
- SourceLoc ());
1322
- constructor->setBody (body, AbstractFunctionDecl::BodyKind::TypeChecked);
1298
+ auto body = BraceStmt::create (ctx, SourceLoc (), {assign, ret}, SourceLoc ());
1299
+ return { body, /* isTypeChecked=*/ true };
1300
+ }
1301
+
1302
+ // / Create a default constructor that initializes a struct to zero.
1303
+ static ConstructorDecl *
1304
+ createDefaultConstructor (ClangImporter::Implementation &Impl,
1305
+ StructDecl *structDecl) {
1306
+ auto &context = Impl.SwiftContext ;
1307
+
1308
+ auto emptyPL = ParameterList::createEmpty (context);
1309
+
1310
+ // Create the constructor.
1311
+ DeclName name (context, DeclBaseName::createConstructor (), emptyPL);
1312
+ auto constructor = new (context) ConstructorDecl (
1313
+ name, structDecl->getLoc (), OTK_None, /* FailabilityLoc=*/ SourceLoc (),
1314
+ /* Throws=*/ false , /* ThrowsLoc=*/ SourceLoc (), emptyPL,
1315
+ /* GenericParams=*/ nullptr , structDecl);
1316
+
1317
+ // Set the constructor's type.
1318
+ constructor->computeType ();
1319
+ constructor->setValidationToChecked ();
1320
+
1321
+ constructor->setAccess (AccessLevel::Public);
1322
+
1323
+ // Mark the constructor transparent so that we inline it away completely.
1324
+ constructor->getAttrs ().add (new (context) TransparentAttr (/* implicit*/ true ));
1325
+
1326
+ constructor->setBodySynthesizer (synthesizeStructDefaultConstructorBody,
1327
+ structDecl);
1323
1328
1324
1329
// We're done.
1325
1330
return constructor;
0 commit comments