@@ -2264,44 +2264,42 @@ ConstructorDecl *swift::createImplicitConstructor(TypeChecker &tc,
2264
2264
ctor->getAttrs ().add (new (tc.Context ) OverrideAttr (/* IsImplicit=*/ true ));
2265
2265
}
2266
2266
2267
- // Type-check the constructor declaration.
2268
- tc.validateDecl (ctor);
2269
-
2270
2267
return ctor;
2271
2268
}
2272
2269
2273
2270
// / Create a stub body that emits a fatal error message.
2274
- static void createStubBody (TypeChecker &tc, ConstructorDecl *ctor) {
2275
- auto unimplementedInitDecl = tc.Context .getUnimplementedInitializerDecl ();
2271
+ static void synthesizeStubBody (AbstractFunctionDecl *fn, void *) {
2272
+ auto *ctor = cast<ConstructorDecl>(fn);
2273
+ auto &ctx = ctor->getASTContext ();
2274
+
2275
+ auto unimplementedInitDecl = ctx.getUnimplementedInitializerDecl ();
2276
2276
auto classDecl = ctor->getDeclContext ()->getSelfClassDecl ();
2277
2277
if (!unimplementedInitDecl) {
2278
- tc.diagnose (classDecl->getLoc (), diag::missing_unimplemented_init_runtime);
2278
+ ctx.Diags .diagnose (classDecl->getLoc (),
2279
+ diag::missing_unimplemented_init_runtime);
2279
2280
return ;
2280
2281
}
2281
2282
2282
2283
// Create a call to Swift._unimplementedInitializer
2283
2284
auto loc = classDecl->getLoc ();
2284
- Expr *fn = new (tc. Context ) DeclRefExpr (unimplementedInitDecl,
2285
- DeclNameLoc (loc),
2286
- /* Implicit=*/ true );
2285
+ Expr *ref = new (ctx ) DeclRefExpr (unimplementedInitDecl,
2286
+ DeclNameLoc (loc),
2287
+ /* Implicit=*/ true );
2287
2288
2288
2289
llvm::SmallString<64 > buffer;
2289
- StringRef fullClassName = tc. Context .AllocateCopy (
2290
+ StringRef fullClassName = ctx .AllocateCopy (
2290
2291
(classDecl->getModuleContext ()->getName ().str () +
2291
2292
" ." +
2292
2293
classDecl->getName ().str ()).toStringRef (buffer));
2293
2294
2294
- Expr *className = new (tc. Context ) StringLiteralExpr (fullClassName, loc,
2295
- /* Implicit=*/ true );
2296
- Expr *call = CallExpr::createImplicit (tc. Context , fn , { className },
2297
- { tc. Context .Id_className });
2298
- ctor->setBody (BraceStmt::create (tc. Context , SourceLoc (),
2295
+ Expr *className = new (ctx ) StringLiteralExpr (fullClassName, loc,
2296
+ /* Implicit=*/ true );
2297
+ Expr *call = CallExpr::createImplicit (ctx, ref , { className },
2298
+ { ctx .Id_className });
2299
+ ctor->setBody (BraceStmt::create (ctx , SourceLoc (),
2299
2300
ASTNode (call),
2300
2301
SourceLoc (),
2301
2302
/* implicit=*/ true ));
2302
-
2303
- // Note that this is a stub implementation.
2304
- ctor->setStubImplementation (true );
2305
2303
}
2306
2304
2307
2305
static std::tuple<GenericEnvironment *, GenericParamList *, SubstitutionMap>
@@ -2476,6 +2474,39 @@ configureInheritedDesignatedInitAttributes(TypeChecker &tc,
2476
2474
ctor->getAttrs ().add (new (ctx) NonObjCAttr (/* isImplicit=*/ true ));
2477
2475
}
2478
2476
2477
+ static void synthesizeDesignatedInitOverride (AbstractFunctionDecl *fn,
2478
+ void *context) {
2479
+ auto *ctor = cast<ConstructorDecl>(fn);
2480
+ auto &ctx = ctor->getASTContext ();
2481
+
2482
+ auto *bodyParams = ctor->getParameters ();
2483
+ auto *superclassCtor = (ConstructorDecl *) context;
2484
+
2485
+ // Reference to super.init.
2486
+ auto *selfDecl = ctor->getImplicitSelfDecl ();
2487
+ Expr *superRef = new (ctx) SuperRefExpr (selfDecl, SourceLoc (),
2488
+ /* Implicit=*/ true );
2489
+ Expr *ctorRef = new (ctx) UnresolvedDotExpr (superRef, SourceLoc (),
2490
+ superclassCtor->getFullName (),
2491
+ DeclNameLoc (),
2492
+ /* Implicit=*/ true );
2493
+
2494
+ auto ctorArgs = buildArgumentForwardingExpr (bodyParams->getArray (), ctx);
2495
+
2496
+ Expr *superCall =
2497
+ CallExpr::create (ctx, ctorRef, ctorArgs,
2498
+ superclassCtor->getFullName ().getArgumentNames (), { },
2499
+ /* hasTrailingClosure=*/ false , /* implicit=*/ true );
2500
+ if (superclassCtor->hasThrows ()) {
2501
+ superCall = new (ctx) TryExpr (SourceLoc (), superCall, Type (),
2502
+ /* implicit=*/ true );
2503
+ }
2504
+ ctor->setBody (BraceStmt::create (ctx, SourceLoc (),
2505
+ ASTNode (superCall),
2506
+ SourceLoc (),
2507
+ /* implicit=*/ true ));
2508
+ }
2509
+
2479
2510
ConstructorDecl *
2480
2511
swift::createDesignatedInitOverride (TypeChecker &tc,
2481
2512
ClassDecl *classDecl,
@@ -2556,37 +2587,19 @@ swift::createDesignatedInitOverride(TypeChecker &tc,
2556
2587
2557
2588
if (kind == DesignatedInitKind::Stub) {
2558
2589
// Make this a stub implementation.
2559
- createStubBody (tc, ctor);
2590
+ ctor->setBodySynthesizer (synthesizeStubBody);
2591
+
2592
+ // Note that this is a stub implementation.
2593
+ ctor->setStubImplementation (true );
2594
+
2595
+ // Stub constructors don't appear in the vtable.
2560
2596
ctor->setNeedsNewVTableEntry (false );
2561
2597
return ctor;
2562
2598
}
2563
2599
2564
2600
// Form the body of a chaining designated initializer.
2565
2601
assert (kind == DesignatedInitKind::Chaining);
2566
-
2567
- // Reference to super.init.
2568
- auto *selfDecl = ctor->getImplicitSelfDecl ();
2569
- Expr *superRef = new (ctx) SuperRefExpr (selfDecl, SourceLoc (),
2570
- /* Implicit=*/ true );
2571
- Expr *ctorRef = new (ctx) UnresolvedDotExpr (superRef, SourceLoc (),
2572
- superclassCtor->getFullName (),
2573
- DeclNameLoc (),
2574
- /* Implicit=*/ true );
2575
-
2576
- auto ctorArgs = buildArgumentForwardingExpr (bodyParams->getArray (), ctx);
2577
-
2578
- Expr *superCall =
2579
- CallExpr::create (ctx, ctorRef, ctorArgs,
2580
- superclassCtor->getFullName ().getArgumentNames (), { },
2581
- /* hasTrailingClosure=*/ false , /* implicit=*/ true );
2582
- if (superclassCtor->hasThrows ()) {
2583
- superCall = new (ctx) TryExpr (SourceLoc (), superCall, Type (),
2584
- /* implicit=*/ true );
2585
- }
2586
- ctor->setBody (BraceStmt::create (tc.Context , SourceLoc (),
2587
- ASTNode (superCall),
2588
- SourceLoc (),
2589
- /* implicit=*/ true ));
2602
+ ctor->setBodySynthesizer (synthesizeDesignatedInitOverride, superclassCtor);
2590
2603
2591
2604
return ctor;
2592
2605
}
0 commit comments