@@ -86,6 +86,30 @@ static Pattern *createTypedNamedPattern(VarDecl *decl) {
86
86
return P;
87
87
}
88
88
89
+ // / Create a var member for this struct, along with its pattern binding, and add
90
+ // / it as a member
91
+ static std::pair<VarDecl *, PatternBindingDecl *>
92
+ createVarWithPattern (ASTContext &cxt, DeclContext *dc, Identifier name, Type ty,
93
+ bool isLet, bool isImplicit,
94
+ Accessibility setterAccessibility) {
95
+ // Create a variable to store the underlying value.
96
+ auto var = new (cxt) VarDecl (
97
+ /* static*/ false ,
98
+ /* IsLet*/ isLet, SourceLoc (), name, ty, dc);
99
+ if (isImplicit)
100
+ var->setImplicit ();
101
+ var->setAccessibility (Accessibility::Public);
102
+ var->setSetterAccessibility (setterAccessibility);
103
+
104
+ // Create a pattern binding to describe the variable.
105
+ Pattern *varPattern = createTypedNamedPattern (var);
106
+ auto patternBinding =
107
+ PatternBindingDecl::create (cxt, SourceLoc (), StaticSpellingKind::None,
108
+ SourceLoc (), varPattern, nullptr , dc);
109
+
110
+ return {var, patternBinding};
111
+ }
112
+
89
113
#ifndef NDEBUG
90
114
static bool verifyNameMapping (MappedTypeNameKind NameMapping,
91
115
StringRef left, StringRef right) {
@@ -1464,28 +1488,15 @@ namespace {
1464
1488
bool makeUnlabeledValueInit = false,
1465
1489
bool isImplicit = true) {
1466
1490
auto &cxt = Impl.SwiftContext ;
1467
- populateInheritedTypes (structDecl, protocols);
1468
-
1469
- // Note synthesized protocols
1470
- for (auto kind : synthesizedProtocolAttrs)
1471
- structDecl->getAttrs ().add (new (cxt) SynthesizedProtocolAttr (kind));
1491
+ addProtocolsToStruct (structDecl, synthesizedProtocolAttrs, protocols);
1472
1492
1473
1493
// Create a variable to store the underlying value.
1474
- auto varName = cxt.Id_rawValue ;
1475
- auto var = new (cxt) VarDecl (
1476
- /* static*/ false ,
1477
- /* IsLet*/ isLet, SourceLoc (), varName, underlyingType, structDecl);
1478
- if (isImplicit)
1479
- var->setImplicit ();
1480
- var->setAccessibility (Accessibility::Public);
1481
- var->setSetterAccessibility (setterAccessibility);
1482
-
1483
- // Create a pattern binding to describe the variable.
1484
- Pattern *varPattern = createTypedNamedPattern (var);
1494
+ VarDecl *var;
1495
+ PatternBindingDecl *patternBinding;
1496
+ std::tie (var, patternBinding) =
1497
+ createVarWithPattern (cxt, structDecl, cxt.Id_rawValue , underlyingType,
1498
+ isLet, isImplicit, setterAccessibility);
1485
1499
1486
- auto patternBinding = PatternBindingDecl::create (
1487
- cxt, SourceLoc (), StaticSpellingKind::None, SourceLoc (), varPattern,
1488
- nullptr , structDecl);
1489
1500
structDecl->setHasDelayedMembers ();
1490
1501
1491
1502
// Create constructors to initialize that value from a value of the
@@ -1499,7 +1510,6 @@ namespace {
1499
1510
createValueConstructor (structDecl, var,
1500
1511
/* wantCtorParamNames=*/ true ,
1501
1512
/* wantBody=*/ !Impl.hasFinishedTypeChecking ()));
1502
-
1503
1513
structDecl->addMember (patternBinding);
1504
1514
structDecl->addMember (var);
1505
1515
}
@@ -1526,30 +1536,18 @@ namespace {
1526
1536
ArrayRef<KnownProtocolKind> synthesizedProtocolAttrs,
1527
1537
ProtocolDecl *const (&protocols)[N]) {
1528
1538
auto &cxt = Impl.SwiftContext ;
1529
- populateInheritedTypes (structDecl, protocols);
1530
-
1531
- // Note synthesized protocols
1532
- for (auto kind : synthesizedProtocolAttrs)
1533
- structDecl->getAttrs ().add (new (cxt) SynthesizedProtocolAttr (kind));
1539
+ addProtocolsToStruct (structDecl, synthesizedProtocolAttrs,
1540
+ protocols);
1534
1541
1535
1542
auto storedVarName = cxt.getIdentifier (" _rawValue" );
1536
1543
auto computedVarName = cxt.Id_rawValue ;
1537
1544
1538
- //
1539
1545
// Create a variable to store the underlying value.
1540
- auto storedVar = new (cxt) VarDecl (
1541
- /* static*/ false ,
1542
- /* IsLet*/ false , SourceLoc (), storedVarName, storedUnderlyingType,
1543
- structDecl);
1544
- storedVar->setImplicit ();
1545
- storedVar->setAccessibility (Accessibility::Public);
1546
- storedVar->setSetterAccessibility (Accessibility::Private);
1547
-
1548
- // Create a pattern binding to describe the variable.
1549
- Pattern *storedVarPattern = createTypedNamedPattern (storedVar);
1550
- auto storedPatternBinding = PatternBindingDecl::create (
1551
- cxt, SourceLoc (), StaticSpellingKind::None, SourceLoc (),
1552
- storedVarPattern, nullptr , structDecl);
1546
+ VarDecl *storedVar;
1547
+ PatternBindingDecl *storedPatternBinding;
1548
+ std::tie (storedVar, storedPatternBinding) = createVarWithPattern (
1549
+ cxt, structDecl, storedVarName, storedUnderlyingType, /* isLet=*/ false ,
1550
+ /* isImplicit=*/ true , Accessibility::Private);
1553
1551
1554
1552
//
1555
1553
// Create a computed value variable
@@ -1599,15 +1597,12 @@ namespace {
1599
1597
init->getAttrs ().add (new (cxt) TransparentAttr (/* implicit*/ true ));
1600
1598
}
1601
1599
1602
- // Create constructor for computed value
1603
1600
structDecl->setHasDelayedMembers ();
1604
-
1605
1601
structDecl->addMember (init);
1606
-
1607
1602
structDecl->addMember (storedPatternBinding);
1608
1603
structDecl->addMember (storedVar);
1609
- structDecl->addMember (computedVar);
1610
1604
structDecl->addMember (computedPatternBinding);
1605
+ structDecl->addMember (computedVar);
1611
1606
}
1612
1607
1613
1608
// / \brief Create a constructor that initializes a struct from its members.
@@ -1836,6 +1831,20 @@ namespace {
1836
1831
nominal->setCheckedInheritanceClause ();
1837
1832
}
1838
1833
1834
+ // / Add protocol conformances and synthesized protocol attributes
1835
+ template <unsigned N>
1836
+ void
1837
+ addProtocolsToStruct (StructDecl *structDecl,
1838
+ ArrayRef<KnownProtocolKind> synthesizedProtocolAttrs,
1839
+ ProtocolDecl *const (&protocols)[N]) {
1840
+ populateInheritedTypes (structDecl, protocols);
1841
+
1842
+ // Note synthesized protocols
1843
+ for (auto kind : synthesizedProtocolAttrs)
1844
+ structDecl->getAttrs ().add (new (Impl.SwiftContext )
1845
+ SynthesizedProtocolAttr (kind));
1846
+ }
1847
+
1839
1848
NominalTypeDecl *importAsOptionSetType (DeclContext *dc,
1840
1849
Identifier name,
1841
1850
const clang::EnumDecl *decl) {
0 commit comments