@@ -64,14 +64,35 @@ namespace inferred_attributes {
64
64
}
65
65
}
66
66
67
+ namespace {
68
+ enum class MakeStructRawValuedFlags {
69
+ // / whether to also create an unlabeled init
70
+ MakeUnlabeledValueInit = 0x01 ,
71
+
72
+ // / whether the raw value should be a let
73
+ IsLet = 0x02 ,
74
+
75
+ // / whether to mark the rawValue as implicit
76
+ IsImplicit = 0x04 ,
77
+ };
78
+ using MakeStructRawValuedOptions = OptionSet<MakeStructRawValuedFlags>;
79
+ }
80
+
81
+ static MakeStructRawValuedOptions
82
+ getDefaultMakeStructRawValuedOptions () {
83
+ MakeStructRawValuedOptions opts;
84
+ opts -= MakeStructRawValuedFlags::MakeUnlabeledValueInit; // default off
85
+ opts |= MakeStructRawValuedFlags::IsLet; // default on
86
+ opts |= MakeStructRawValuedFlags::IsImplicit; // default on
87
+ return opts;
88
+ }
67
89
68
90
static bool isInSystemModule (DeclContext *D) {
69
91
if (cast<ClangModuleUnit>(D->getModuleScopeContext ())->isSystemModule ())
70
92
return true ;
71
93
return false ;
72
94
}
73
95
74
-
75
96
// / Create a typedpattern(namedpattern(decl))
76
97
static Pattern *createTypedNamedPattern (VarDecl *decl) {
77
98
ASTContext &Ctx = decl->getASTContext ();
@@ -1429,9 +1450,12 @@ namespace {
1429
1450
if (!isBridged) {
1430
1451
// Simple, our stored type is equivalent to our computed
1431
1452
// type.
1453
+ auto options = getDefaultMakeStructRawValuedOptions ();
1454
+ if (unlabeledCtor)
1455
+ options |= MakeStructRawValuedFlags::MakeUnlabeledValueInit;
1456
+
1432
1457
makeStructRawValued (structDecl, storedUnderlyingType,
1433
- synthesizedProtocols, protocols,
1434
- /* makeUnlabeledValueInit=*/ unlabeledCtor);
1458
+ synthesizedProtocols, protocols, options);
1435
1459
} else {
1436
1460
// We need to make a stored rawValue or storage type, and a
1437
1461
// computed one of bridged type.
@@ -1703,39 +1727,37 @@ namespace {
1703
1727
// / \param synthesizedProtocolAttrs synthesized protocol attributes to add
1704
1728
// / \param protocols the protocols to make this struct conform to
1705
1729
// / \param setterAccessibility the accessibility of the raw value's setter
1706
- // / \param isLet whether the raw value should be a let
1707
- // / \param makeUnlabeledValueInit whether to also create an unlabeled init
1708
- // / \param isImplicit whether to mark the rawValue as implicit
1709
1730
// /
1710
1731
// / This will perform most of the work involved in making a new Swift struct
1711
1732
// / be backed by a raw value. This will populated derived protocols and
1712
1733
// / synthesized protocols, add the new variable and pattern bindings, and
1713
1734
// / create the inits parameterized over a raw value
1714
1735
// /
1715
1736
void makeStructRawValued (
1716
- StructDecl *structDecl,
1717
- Type underlyingType,
1737
+ StructDecl *structDecl, Type underlyingType,
1718
1738
ArrayRef<KnownProtocolKind> synthesizedProtocolAttrs,
1719
1739
ArrayRef<ProtocolDecl *> protocols,
1720
- bool makeUnlabeledValueInit = false ,
1721
- Accessibility setterAccessibility = Accessibility::Private,
1722
- bool isLet = true ,
1723
- bool isImplicit = true ) {
1740
+ MakeStructRawValuedOptions options =
1741
+ getDefaultMakeStructRawValuedOptions (),
1742
+ Accessibility setterAccessibility = Accessibility::Private) {
1724
1743
auto &cxt = Impl.SwiftContext ;
1725
1744
addProtocolsToStruct (structDecl, synthesizedProtocolAttrs, protocols);
1726
1745
1727
1746
// Create a variable to store the underlying value.
1728
1747
VarDecl *var;
1729
1748
PatternBindingDecl *patternBinding;
1730
- std::tie (var, patternBinding) =
1731
- createVarWithPattern (cxt, structDecl, cxt.Id_rawValue , underlyingType,
1732
- isLet, isImplicit, setterAccessibility);
1749
+ std::tie (var, patternBinding) = createVarWithPattern (
1750
+ cxt, structDecl, cxt.Id_rawValue , underlyingType,
1751
+ options.contains (MakeStructRawValuedFlags::IsLet),
1752
+ options.contains (MakeStructRawValuedFlags::IsImplicit),
1753
+ setterAccessibility);
1733
1754
1734
1755
structDecl->setHasDelayedMembers ();
1735
1756
1736
1757
// Create constructors to initialize that value from a value of the
1737
1758
// underlying type.
1738
- if (makeUnlabeledValueInit)
1759
+ if (options.contains (
1760
+ MakeStructRawValuedFlags::MakeUnlabeledValueInit))
1739
1761
structDecl->addMember (createValueConstructor (
1740
1762
structDecl, var,
1741
1763
/* wantCtorParamNames=*/ false ,
@@ -2203,12 +2225,16 @@ namespace {
2203
2225
ProtocolDecl *protocols[]
2204
2226
= {cxt.getProtocol (KnownProtocolKind::RawRepresentable),
2205
2227
cxt.getProtocol (KnownProtocolKind::Equatable)};
2228
+
2229
+ auto options = getDefaultMakeStructRawValuedOptions ();
2230
+ options |= MakeStructRawValuedFlags::MakeUnlabeledValueInit;
2231
+ options -= MakeStructRawValuedFlags::IsLet;
2232
+ options -= MakeStructRawValuedFlags::IsImplicit;
2233
+
2206
2234
makeStructRawValued (structDecl, underlyingType,
2207
2235
{KnownProtocolKind::RawRepresentable}, protocols,
2208
- /* makeUnlabeledValueInit=*/ true ,
2209
- /* setterAccessibility=*/ Accessibility::Public,
2210
- /* isLet=*/ false ,
2211
- /* isImplicit=*/ false );
2236
+ options,
2237
+ /* setterAccessibility=*/ Accessibility::Public);
2212
2238
2213
2239
result = structDecl;
2214
2240
break ;
0 commit comments