@@ -431,6 +431,59 @@ static FuncDecl *makeEnumRawValueGetter(ClangImporter::Implementation &Impl,
431
431
return getterDecl;
432
432
}
433
433
434
+ // Build the rawValue getter for a bridged, swift_newtype'd type.
435
+ // struct SomeType: RawRepresentable {
436
+ // var _rawValue: ObjCType
437
+ // var rawValue: SwiftType {
438
+ // return _rawValue as SwiftType
439
+ // }
440
+ // }
441
+ static FuncDecl *makeNewtypeBridgedRawValueGetter (
442
+ ClangImporter::Implementation &Impl,
443
+ StructDecl *structDecl,
444
+ VarDecl *computedVar,
445
+ VarDecl *storedVar) {
446
+ ASTContext &C = Impl.SwiftContext ;
447
+
448
+ auto selfDecl = ParamDecl::createSelf (SourceLoc (), structDecl);
449
+
450
+ ParameterList *params[] = {
451
+ ParameterList::createWithoutLoc (selfDecl),
452
+ ParameterList::createEmpty (C)
453
+ };
454
+
455
+ auto computedType = computedVar->getType ();
456
+
457
+ auto getterDecl =
458
+ FuncDecl::create (C, SourceLoc (), StaticSpellingKind::None, SourceLoc (),
459
+ DeclName (), SourceLoc (), SourceLoc (), SourceLoc (), nullptr ,
460
+ Type (), params,
461
+ TypeLoc::withoutLoc (computedType), structDecl);
462
+ getterDecl->setImplicit ();
463
+ getterDecl->setType (ParameterList::getFullType (computedType, params));
464
+ getterDecl->setBodyResultType (computedType);
465
+ getterDecl->setAccessibility (Accessibility::Public);
466
+
467
+ computedVar->makeComputed (SourceLoc (), getterDecl, nullptr , nullptr ,
468
+ SourceLoc ());
469
+
470
+ // Don't bother synthesizing the body if we've already finished type-checking.
471
+ if (Impl.hasFinishedTypeChecking ())
472
+ return getterDecl;
473
+
474
+ auto selfRef = new (C) DeclRefExpr (selfDecl, DeclNameLoc (), /* implicit*/ true );
475
+ auto storedRef = new (C) MemberRefExpr (selfRef, SourceLoc (), storedVar,
476
+ DeclNameLoc (), /* Implicit=*/ true );
477
+ auto coerce = new (C) CoerceExpr (storedRef, {}, {nullptr , computedType});
478
+ auto ret = new (C) ReturnStmt (SourceLoc (), coerce);
479
+ auto body = BraceStmt::create (C, SourceLoc (), ASTNode (ret), SourceLoc (),
480
+ /* implicit*/ true );
481
+
482
+ getterDecl->setBody (body);
483
+ C.addExternalDecl (getterDecl);
484
+ return getterDecl;
485
+ }
486
+
434
487
static FuncDecl *makeFieldGetterDecl (ClangImporter::Implementation &Impl,
435
488
StructDecl *importedDecl,
436
489
VarDecl *importedFieldDecl,
@@ -1668,18 +1721,21 @@ namespace {
1668
1721
1669
1722
//
1670
1723
// Create a computed value variable
1671
- auto dre = new (cxt) DeclRefExpr (
1672
- storedVar, {}, true , AccessSemantics::Ordinary, storedUnderlyingType);
1673
- auto coerce = new (cxt)
1674
- CoerceExpr (dre, {}, {nullptr , bridgedType});
1675
- auto computedVar = cast<VarDecl>(Impl.createConstant (
1676
- computedVarName, structDecl, bridgedType, coerce,
1677
- ConstantConvertKind::Coerce, false , {}));
1724
+ auto computedVar = new (cxt) VarDecl (/* static*/ false ,
1725
+ /* IsLet*/ false ,
1726
+ SourceLoc (), computedVarName,
1727
+ bridgedType, structDecl);
1678
1728
computedVar->setImplicit ();
1679
1729
computedVar->setAccessibility (Accessibility::Public);
1730
+ computedVar->setSetterAccessibility (Accessibility::Private);
1680
1731
1681
- // Create a pattern binding to describe the variable.
1732
+ // Create the getter for the computed value variable.
1733
+ auto computedVarGetter = makeNewtypeBridgedRawValueGetter (Impl,
1734
+ structDecl,
1735
+ computedVar,
1736
+ storedVar);
1682
1737
1738
+ // Create a pattern binding to describe the variable.
1683
1739
Pattern *computedVarPattern = createTypedNamedPattern (computedVar);
1684
1740
auto computedPatternBinding = PatternBindingDecl::create (
1685
1741
cxt, SourceLoc (), StaticSpellingKind::None, SourceLoc (),
@@ -1711,9 +1767,6 @@ namespace {
1711
1767
/* Implicit=*/ true );
1712
1768
auto body = BraceStmt::create (cxt, SourceLoc (), {assign}, SourceLoc ());
1713
1769
init->setBody (body);
1714
-
1715
- // We want to inline away as much of this as we can
1716
- init->getAttrs ().add (new (cxt) TransparentAttr (/* implicit*/ true ));
1717
1770
}
1718
1771
1719
1772
structDecl->setHasDelayedMembers ();
@@ -1722,6 +1775,7 @@ namespace {
1722
1775
structDecl->addMember (storedVar);
1723
1776
structDecl->addMember (computedPatternBinding);
1724
1777
structDecl->addMember (computedVar);
1778
+ structDecl->addMember (computedVarGetter);
1725
1779
}
1726
1780
1727
1781
// / \brief Create a constructor that initializes a struct from its members.
0 commit comments