Skip to content

ClangImporter: use nameless arguments for anonymous struct/unions in constructors #6935

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1087,12 +1087,20 @@ createValueConstructor(ClangImporter::Implementation &Impl,
// Construct the set of parameters from the list of members.
SmallVector<ParamDecl *, 8> valueParameters;
for (auto var : members) {
// TODO create value constructor with indirect fields instead of the
// generated __Anonymous_field.
if (var->hasClangNode() && isa<clang::IndirectFieldDecl>(var->getClangDecl()))
continue;
bool generateParamName = wantCtorParamNames;

if (var->hasClangNode()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: It seems like we should still honor wantCtorParamNames being false, even if no callers do this now with imported structs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, indeed, good catch. Fixed

// TODO create value constructor with indirect fields instead of the
// generated __Anonymous_field.
if (isa<clang::IndirectFieldDecl>(var->getClangDecl()))
continue;

if (auto clangField = dyn_cast<clang::FieldDecl>(var->getClangDecl()))
if (clangField->isAnonymousStructOrUnion())
generateParamName = false;
}

Identifier argName = wantCtorParamNames ? var->getName() : Identifier();
Identifier argName = generateParamName ? var->getName() : Identifier();
auto param = new (context)
ParamDecl(/*IsLet*/ true, SourceLoc(), SourceLoc(), argName,
SourceLoc(), var->getName(), var->getType(), structDecl);
Expand Down
13 changes: 13 additions & 0 deletions test/ClangImporter/Inputs/custom-modules/IndirectFields.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,16 @@ union UnionWithIndirectField {
};
int c;
};

struct DeepIndirectField {
union {
struct {
int a;
int b;
};
struct {
int c;
int d;
};
};
};
14 changes: 11 additions & 3 deletions test/ClangImporter/indirect_fields.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,25 @@
import IndirectFields

func build_struct(a: Int32, c: Int32, d: Int32) -> StructWithIndirectField {
return StructWithIndirectField(__Anonymous_field0: .init(a: a), c: c, d: d)
return StructWithIndirectField(.init(a: a), c: c, d: d)
}

func build_struct(b: Int32, c: Int32, d: Int32) -> StructWithIndirectField {
return StructWithIndirectField(__Anonymous_field0: .init(b: b), c: c, d: d)
return StructWithIndirectField(.init(b: b), c: c, d: d)
}

func build_union(a: Int32, b: Int32) -> UnionWithIndirectField {
return UnionWithIndirectField(__Anonymous_field0: .init(a: a, b: b))
return UnionWithIndirectField(.init(a: a, b: b))
}

func build_union(c: Int32) -> UnionWithIndirectField {
return UnionWithIndirectField(c: c)
}

func build_deep(a: Int32, b: Int32) -> DeepIndirectField {
return DeepIndirectField(.init(.init(a: a, b: b)))
}

func build_deep(c: Int32, d: Int32) -> DeepIndirectField {
return DeepIndirectField(.init(.init(c: c, d: d)))
}