@@ -1557,60 +1557,72 @@ static void makeStructRawValued(
1557
1557
Impl.RawTypes [structDecl] = underlyingType;
1558
1558
}
1559
1559
1560
+ // / Synthesizer callback for a raw value bridging constructor body.
1561
+ static std::pair<BraceStmt *, bool >
1562
+ synthesizeRawValueBridgingConstructorBody (AbstractFunctionDecl *afd,
1563
+ void *context) {
1564
+ auto init = cast<ConstructorDecl>(afd);
1565
+ VarDecl *storedRawValue = static_cast <VarDecl *>(context);
1566
+
1567
+ ASTContext &ctx = init->getASTContext ();
1568
+
1569
+ auto selfDecl = init->getImplicitSelfDecl ();
1570
+ auto storedType = storedRawValue->getInterfaceType ();
1571
+
1572
+ // Construct left-hand side.
1573
+ Expr *lhs = new (ctx) DeclRefExpr (selfDecl, DeclNameLoc (),
1574
+ /* Implicit=*/ true );
1575
+ lhs->setType (LValueType::get (selfDecl->getType ()));
1576
+
1577
+ lhs = new (ctx) MemberRefExpr (lhs, SourceLoc (), storedRawValue,
1578
+ DeclNameLoc (), /* Implicit=*/ true ,
1579
+ AccessSemantics::DirectToStorage);
1580
+ lhs->setType (LValueType::get (storedType));
1581
+
1582
+ // Construct right-hand side.
1583
+ // FIXME: get the parameter from the init, and plug it in here.
1584
+ auto *paramDecl = init->getParameters ()->get (0 );
1585
+ auto *paramRef = new (ctx) DeclRefExpr (
1586
+ paramDecl, DeclNameLoc (), /* Implicit=*/ true );
1587
+ paramRef->setType (paramDecl->getType ());
1588
+
1589
+ Expr *rhs = paramRef;
1590
+ if (!storedRawValue->getInterfaceType ()->isEqual (paramDecl->getType ())) {
1591
+ auto bridge = new (ctx) BridgeToObjCExpr (paramRef, storedType);
1592
+ bridge->setType (storedType);
1593
+
1594
+ auto coerce = new (ctx) CoerceExpr (bridge, SourceLoc (),
1595
+ {nullptr , storedType});
1596
+ coerce->setType (storedType);
1597
+
1598
+ rhs = coerce;
1599
+ }
1600
+
1601
+ // Add assignment.
1602
+ auto assign = new (ctx) AssignExpr (lhs, SourceLoc (), rhs,
1603
+ /* Implicit=*/ true );
1604
+ assign->setType (TupleType::getEmpty (ctx));
1605
+
1606
+ auto result = TupleExpr::createEmpty (ctx, SourceLoc (), SourceLoc (),
1607
+ /* Implicit=*/ true );
1608
+ auto ret = new (ctx) ReturnStmt (SourceLoc (), result, /* Implicit=*/ true );
1609
+
1610
+ auto body = BraceStmt::create (ctx, SourceLoc (), {assign, ret}, SourceLoc ());
1611
+ return { body, /* isTypeChecked=*/ true };
1612
+ }
1613
+
1560
1614
// / Create a rawValue-ed constructor that bridges to its underlying storage.
1561
1615
static ConstructorDecl *createRawValueBridgingConstructor (
1562
1616
ClangImporter::Implementation &Impl, StructDecl *structDecl,
1563
1617
VarDecl *computedRawValue, VarDecl *storedRawValue, bool wantLabel,
1564
1618
bool wantBody) {
1565
- auto &ctx = Impl.SwiftContext ;
1566
1619
auto init = createValueConstructor (Impl, structDecl, computedRawValue,
1567
1620
/* wantCtorParamNames=*/ wantLabel,
1568
1621
/* wantBody=*/ false );
1569
1622
// Insert our custom init body
1570
1623
if (wantBody) {
1571
- auto selfDecl = init->getImplicitSelfDecl ();
1572
- auto storedType = storedRawValue->getInterfaceType ();
1573
-
1574
- // Construct left-hand side.
1575
- Expr *lhs = new (ctx) DeclRefExpr (selfDecl, DeclNameLoc (),
1576
- /* Implicit=*/ true );
1577
- lhs->setType (LValueType::get (selfDecl->getType ()));
1578
-
1579
- lhs = new (ctx) MemberRefExpr (lhs, SourceLoc (), storedRawValue,
1580
- DeclNameLoc (), /* Implicit=*/ true ,
1581
- AccessSemantics::DirectToStorage);
1582
- lhs->setType (LValueType::get (storedType));
1583
-
1584
- // Construct right-hand side.
1585
- // FIXME: get the parameter from the init, and plug it in here.
1586
- auto *paramDecl = init->getParameters ()->get (0 );
1587
- auto *paramRef = new (ctx) DeclRefExpr (
1588
- paramDecl, DeclNameLoc (), /* Implicit=*/ true );
1589
- paramRef->setType (paramDecl->getType ());
1590
-
1591
- Expr *rhs = paramRef;
1592
- if (!storedRawValue->getInterfaceType ()->isEqual (paramDecl->getType ())) {
1593
- auto bridge = new (ctx) BridgeToObjCExpr (paramRef, storedType);
1594
- bridge->setType (storedType);
1595
-
1596
- auto coerce = new (ctx) CoerceExpr (bridge, SourceLoc (),
1597
- {nullptr , storedType});
1598
- coerce->setType (storedType);
1599
-
1600
- rhs = coerce;
1601
- }
1602
-
1603
- // Add assignment.
1604
- auto assign = new (ctx) AssignExpr (lhs, SourceLoc (), rhs,
1605
- /* Implicit=*/ true );
1606
- assign->setType (TupleType::getEmpty (ctx));
1607
-
1608
- auto result = TupleExpr::createEmpty (ctx, SourceLoc (), SourceLoc (),
1609
- /* Implicit=*/ true );
1610
- auto ret = new (ctx) ReturnStmt (SourceLoc (), result, /* Implicit=*/ true );
1611
-
1612
- auto body = BraceStmt::create (ctx, SourceLoc (), {assign, ret}, SourceLoc ());
1613
- init->setBody (body, AbstractFunctionDecl::BodyKind::TypeChecked);
1624
+ init->setBodySynthesizer (synthesizeRawValueBridgingConstructorBody,
1625
+ storedRawValue);
1614
1626
}
1615
1627
1616
1628
return init;
@@ -1681,7 +1693,7 @@ static void makeStructRawValuedWithBridge(
1681
1693
if (makeUnlabeledValueInit)
1682
1694
unlabeledCtor = createRawValueBridgingConstructor (
1683
1695
Impl, structDecl, computedVar, storedVar,
1684
- /* wantLabel*/ false , /* wantBody*/ !Impl. hasFinishedTypeChecking () );
1696
+ /* wantLabel*/ false , /* wantBody*/ true );
1685
1697
1686
1698
if (unlabeledCtor)
1687
1699
structDecl->addMember (unlabeledCtor);
0 commit comments