Skip to content

Commit 134f41c

Browse files
committed
[Clang importer] Use OptionSets for makeStructRawValued
NFC
1 parent e0414a4 commit 134f41c

File tree

1 file changed

+46
-20
lines changed

1 file changed

+46
-20
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,35 @@ namespace inferred_attributes {
6464
}
6565
}
6666

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+
}
6789

6890
static bool isInSystemModule(DeclContext *D) {
6991
if (cast<ClangModuleUnit>(D->getModuleScopeContext())->isSystemModule())
7092
return true;
7193
return false;
7294
}
7395

74-
7596
/// Create a typedpattern(namedpattern(decl))
7697
static Pattern *createTypedNamedPattern(VarDecl *decl) {
7798
ASTContext &Ctx = decl->getASTContext();
@@ -1429,9 +1450,12 @@ namespace {
14291450
if (!isBridged) {
14301451
// Simple, our stored type is equivalent to our computed
14311452
// type.
1453+
auto options = getDefaultMakeStructRawValuedOptions();
1454+
if (unlabeledCtor)
1455+
options |= MakeStructRawValuedFlags::MakeUnlabeledValueInit;
1456+
14321457
makeStructRawValued(structDecl, storedUnderlyingType,
1433-
synthesizedProtocols, protocols,
1434-
/*makeUnlabeledValueInit=*/unlabeledCtor);
1458+
synthesizedProtocols, protocols, options);
14351459
} else {
14361460
// We need to make a stored rawValue or storage type, and a
14371461
// computed one of bridged type.
@@ -1703,39 +1727,37 @@ namespace {
17031727
/// \param synthesizedProtocolAttrs synthesized protocol attributes to add
17041728
/// \param protocols the protocols to make this struct conform to
17051729
/// \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
17091730
///
17101731
/// This will perform most of the work involved in making a new Swift struct
17111732
/// be backed by a raw value. This will populated derived protocols and
17121733
/// synthesized protocols, add the new variable and pattern bindings, and
17131734
/// create the inits parameterized over a raw value
17141735
///
17151736
void makeStructRawValued(
1716-
StructDecl *structDecl,
1717-
Type underlyingType,
1737+
StructDecl *structDecl, Type underlyingType,
17181738
ArrayRef<KnownProtocolKind> synthesizedProtocolAttrs,
17191739
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) {
17241743
auto &cxt = Impl.SwiftContext;
17251744
addProtocolsToStruct(structDecl, synthesizedProtocolAttrs, protocols);
17261745

17271746
// Create a variable to store the underlying value.
17281747
VarDecl *var;
17291748
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);
17331754

17341755
structDecl->setHasDelayedMembers();
17351756

17361757
// Create constructors to initialize that value from a value of the
17371758
// underlying type.
1738-
if (makeUnlabeledValueInit)
1759+
if (options.contains(
1760+
MakeStructRawValuedFlags::MakeUnlabeledValueInit))
17391761
structDecl->addMember(createValueConstructor(
17401762
structDecl, var,
17411763
/*wantCtorParamNames=*/false,
@@ -2203,12 +2225,16 @@ namespace {
22032225
ProtocolDecl *protocols[]
22042226
= {cxt.getProtocol(KnownProtocolKind::RawRepresentable),
22052227
cxt.getProtocol(KnownProtocolKind::Equatable)};
2228+
2229+
auto options = getDefaultMakeStructRawValuedOptions();
2230+
options |= MakeStructRawValuedFlags::MakeUnlabeledValueInit;
2231+
options -= MakeStructRawValuedFlags::IsLet;
2232+
options -= MakeStructRawValuedFlags::IsImplicit;
2233+
22062234
makeStructRawValued(structDecl, underlyingType,
22072235
{KnownProtocolKind::RawRepresentable}, protocols,
2208-
/*makeUnlabeledValueInit=*/true,
2209-
/*setterAccessibility=*/Accessibility::Public,
2210-
/*isLet=*/false,
2211-
/*isImplicit=*/false);
2236+
options,
2237+
/*setterAccessibility=*/Accessibility::Public);
22122238

22132239
result = structDecl;
22142240
break;

0 commit comments

Comments
 (0)