mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Following a13c134521, constructors of structures/unions containing
anonymous structures/unions fields include those field in their parameter
list, using the generated field name as parameter name:
typedef struct foo_t {
union {
int a;
int b;
};
} foo_t;
Generates:
struct foo_t {
init(__Anonymous_field0: foo_t.__Unnamed_union__Anonymous_field0)
}
let foo = foo_t(__Anonymous_field0: .init(a: 1))
One important downside here is that the generated field name get exposed
in the API.
An idealistic approach would be to generate the constructors that expose
the fields indirectly inherited from those structures:
struct foo_t {
init(a: Int32)
init(b: Int32)
}
However, this approach requires the generation of a constructor per valid
combination of indirect fields, which might start having a huge
cardinality when we have nested anonymous structures in nested anonymous
unions...
typedef struct bar_t {
union {
struct {
int a;
int b;
};
struct {
int c;
int d;
};
};
union {
int e;
int f;
};
} bar_t;
In this examples, we have 4 constructors to generates, for (a, b, e), (a,
b, f), (c, d, e) and (c, d, f).
The proposed approach is to use a nameless parameter for anonymous
structures/unions, still forcing the user to build that sub-object by
hand, but without exposing the generated field name. This is very similar
to what can be done in C:
foo_t foo = { { .a = 1 } };
let foo = foo_t(.init(a: 1))
Or
bar_t bar = { { { .a = 1, .b = 2 } }, { .e = 1 } };
let bar = bar_t(.init(.init(a: 1, b: 2)), .init(e: 1))
Signed-off-by: Florent Bruneau <florent.bruneau@intersec.com>
28 lines
851 B
Swift
28 lines
851 B
Swift
// RUN: %target-swift-frontend -typecheck -sdk "" -I %t -I %S/Inputs/custom-modules %s -verify
|
|
|
|
import IndirectFields
|
|
|
|
func build_struct(a: Int32, c: Int32, d: Int32) -> StructWithIndirectField {
|
|
return StructWithIndirectField(.init(a: a), c: c, d: d)
|
|
}
|
|
|
|
func build_struct(b: Int32, c: Int32, d: Int32) -> StructWithIndirectField {
|
|
return StructWithIndirectField(.init(b: b), c: c, d: d)
|
|
}
|
|
|
|
func build_union(a: Int32, b: Int32) -> UnionWithIndirectField {
|
|
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)))
|
|
}
|