Skip to content

Commit 15e7d1f

Browse files
committed
[Clang importer] Code re-use and testing.
Incorporates review feedback to re-use code more. Improves swift_newtype testing by also testing non-bridging path.
1 parent 0c67ddd commit 15e7d1f

File tree

3 files changed

+63
-43
lines changed

3 files changed

+63
-43
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,30 @@ static Pattern *createTypedNamedPattern(VarDecl *decl) {
8686
return P;
8787
}
8888

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+
89113
#ifndef NDEBUG
90114
static bool verifyNameMapping(MappedTypeNameKind NameMapping,
91115
StringRef left, StringRef right) {
@@ -1464,28 +1488,15 @@ namespace {
14641488
bool makeUnlabeledValueInit = false,
14651489
bool isImplicit = true) {
14661490
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);
14721492

14731493
// 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);
14851499

1486-
auto patternBinding = PatternBindingDecl::create(
1487-
cxt, SourceLoc(), StaticSpellingKind::None, SourceLoc(), varPattern,
1488-
nullptr, structDecl);
14891500
structDecl->setHasDelayedMembers();
14901501

14911502
// Create constructors to initialize that value from a value of the
@@ -1499,7 +1510,6 @@ namespace {
14991510
createValueConstructor(structDecl, var,
15001511
/*wantCtorParamNames=*/true,
15011512
/*wantBody=*/!Impl.hasFinishedTypeChecking()));
1502-
15031513
structDecl->addMember(patternBinding);
15041514
structDecl->addMember(var);
15051515
}
@@ -1526,30 +1536,18 @@ namespace {
15261536
ArrayRef<KnownProtocolKind> synthesizedProtocolAttrs,
15271537
ProtocolDecl *const(&protocols)[N]) {
15281538
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);
15341541

15351542
auto storedVarName = cxt.getIdentifier("_rawValue");
15361543
auto computedVarName = cxt.Id_rawValue;
15371544

1538-
//
15391545
// 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);
15531551

15541552
//
15551553
// Create a computed value variable
@@ -1599,15 +1597,12 @@ namespace {
15991597
init->getAttrs().add(new (cxt) TransparentAttr(/*implicit*/ true));
16001598
}
16011599

1602-
// Create constructor for computed value
16031600
structDecl->setHasDelayedMembers();
1604-
16051601
structDecl->addMember(init);
1606-
16071602
structDecl->addMember(storedPatternBinding);
16081603
structDecl->addMember(storedVar);
1609-
structDecl->addMember(computedVar);
16101604
structDecl->addMember(computedPatternBinding);
1605+
structDecl->addMember(computedVar);
16111606
}
16121607

16131608
/// \brief Create a constructor that initializes a struct from its members.
@@ -1836,6 +1831,20 @@ namespace {
18361831
nominal->setCheckedInheritanceClause();
18371832
}
18381833

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+
18391848
NominalTypeDecl *importAsOptionSetType(DeclContext *dc,
18401849
Identifier name,
18411850
const clang::EnumDecl *decl) {

test/IDE/Inputs/custom-modules/Newtype.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,7 @@ extern const SNTClosedEnum SNTSecondEntry;
2626
extern const SNTClosedEnum SNTClosedEnumThirdEntry;
2727

2828
typedef NSString * IUONewtype __attribute((swift_newtype(struct)));
29+
30+
typedef float MyFloat __attribute((swift_newtype(struct)));
31+
extern const MyFloat globalFloat;
32+

test/IDE/newtype.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@
3636
// PRINT-NEXT: var _rawValue: NSString!
3737
// PRINT-NEXT: var rawValue: String! { get }
3838
// PRINT-NEXT: }
39+
// PRINT-NEXT: struct MyFloat : RawRepresentable {
40+
// PRINT-NEXT: init(rawValue: Float)
41+
// PRINT-NEXT: let rawValue: Float
42+
// PRINT-NEXT: }
43+
// PRINT-NEXT: extension MyFloat {
44+
// PRINT-NEXT: static let globalFloat: MyFloat
45+
// PRINT-NEXT: }
3946

4047
// RUN: %target-parse-verify-swift -I %S/Inputs/custom-modules
4148
import Newtype

0 commit comments

Comments
 (0)