Skip to content

Commit 56189ad

Browse files
authored
Merge pull request #6985 from Fruneau/anonymous-field-constructor-master
ClangImporter: use nameless arguments for anonymous struct/unions in constructors.
2 parents a67f4bd + b6b6336 commit 56189ad

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,12 +1088,20 @@ createValueConstructor(ClangImporter::Implementation &Impl,
10881088
// Construct the set of parameters from the list of members.
10891089
SmallVector<ParamDecl *, 8> valueParameters;
10901090
for (auto var : members) {
1091-
// TODO create value constructor with indirect fields instead of the
1092-
// generated __Anonymous_field.
1093-
if (var->hasClangNode() && isa<clang::IndirectFieldDecl>(var->getClangDecl()))
1094-
continue;
1091+
bool generateParamName = wantCtorParamNames;
1092+
1093+
if (var->hasClangNode()) {
1094+
// TODO create value constructor with indirect fields instead of the
1095+
// generated __Anonymous_field.
1096+
if (isa<clang::IndirectFieldDecl>(var->getClangDecl()))
1097+
continue;
1098+
1099+
if (auto clangField = dyn_cast<clang::FieldDecl>(var->getClangDecl()))
1100+
if (clangField->isAnonymousStructOrUnion())
1101+
generateParamName = false;
1102+
}
10951103

1096-
Identifier argName = wantCtorParamNames ? var->getName() : Identifier();
1104+
Identifier argName = generateParamName ? var->getName() : Identifier();
10971105
auto param = new (context)
10981106
ParamDecl(/*IsLet*/ true, SourceLoc(), SourceLoc(), argName,
10991107
SourceLoc(), var->getName(), var->getType(), structDecl);

test/ClangImporter/Inputs/custom-modules/IndirectFields.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,16 @@ union UnionWithIndirectField {
1414
};
1515
int c;
1616
};
17+
18+
struct DeepIndirectField {
19+
union {
20+
struct {
21+
int a;
22+
int b;
23+
};
24+
struct {
25+
int c;
26+
int d;
27+
};
28+
};
29+
};

test/ClangImporter/indirect_fields.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,25 @@
33
import IndirectFields
44

55
func build_struct(a: Int32, c: Int32, d: Int32) -> StructWithIndirectField {
6-
return StructWithIndirectField(__Anonymous_field0: .init(a: a), c: c, d: d)
6+
return StructWithIndirectField(.init(a: a), c: c, d: d)
77
}
88

99
func build_struct(b: Int32, c: Int32, d: Int32) -> StructWithIndirectField {
10-
return StructWithIndirectField(__Anonymous_field0: .init(b: b), c: c, d: d)
10+
return StructWithIndirectField(.init(b: b), c: c, d: d)
1111
}
1212

1313
func build_union(a: Int32, b: Int32) -> UnionWithIndirectField {
14-
return UnionWithIndirectField(__Anonymous_field0: .init(a: a, b: b))
14+
return UnionWithIndirectField(.init(a: a, b: b))
1515
}
1616

1717
func build_union(c: Int32) -> UnionWithIndirectField {
1818
return UnionWithIndirectField(c: c)
1919
}
20+
21+
func build_deep(a: Int32, b: Int32) -> DeepIndirectField {
22+
return DeepIndirectField(.init(.init(a: a, b: b)))
23+
}
24+
25+
func build_deep(c: Int32, d: Int32) -> DeepIndirectField {
26+
return DeepIndirectField(.init(.init(c: c, d: d)))
27+
}

0 commit comments

Comments
 (0)