Skip to content

Commit 8f05d64

Browse files
committed
[Reflection] Add support for imported structs with recorded fields
Update IRGen to trigger generation of type metadata for foreign struct types found in fields. And fix TypeRefBuilder to handle the case where struct has fields but at the same time has opaque metadata.
1 parent 478ba26 commit 8f05d64

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

include/swift/Reflection/Records.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ class FieldDescriptor {
186186
Kind == FieldDescriptorKind::ObjCProtocol);
187187
}
188188

189+
bool isStruct() const {
190+
return Kind == FieldDescriptorKind::Struct;
191+
}
192+
189193
const_iterator begin() const {
190194
auto Begin = getFieldRecordBuffer();
191195
auto End = Begin + NumFields;

stdlib/public/Reflection/TypeLowering.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,16 +1057,18 @@ class LowerType
10571057

10581058
const TypeInfo *visitAnyNominalTypeRef(const TypeRef *TR) {
10591059
const auto &FD = TC.getBuilder().getFieldTypeInfo(TR);
1060-
if (FD.first == nullptr) {
1060+
if (FD.first == nullptr || FD.first->isStruct()) {
10611061
// Maybe this type is opaque -- look for a builtin
10621062
// descriptor to see if we at least know its size
10631063
// and alignment.
10641064
if (auto ImportedTypeDescriptor = TC.getBuilder().getBuiltinTypeInfo(TR))
10651065
return TC.makeTypeInfo<BuiltinTypeInfo>(ImportedTypeDescriptor);
10661066

10671067
// Otherwise, we're out of luck.
1068-
DEBUG(std::cerr << "No TypeInfo for nominal type: "; TR->dump());
1069-
return nullptr;
1068+
if (FD.first == nullptr) {
1069+
DEBUG(std::cerr << "No TypeInfo for nominal type: "; TR->dump());
1070+
return nullptr;
1071+
}
10701072
}
10711073

10721074
switch (FD.first->Kind) {

test/Reflection/typeref_decoding_imported.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@
2020
// CHECK-32: mce: __C.MyCEnum
2121
// CHECK-32: (struct __C.MyCEnum)
2222

23+
// CHECK-32: __C.MyCStruct
24+
// CHECK-32: -------------
25+
// CHECK-32: i: Swift.Int32
26+
// CHECK-32: (struct Swift.Int32)
27+
28+
// CHECK-32: ip: Swift.Optional<Swift.UnsafeMutablePointer<Swift.Int32>>
29+
// CHECK-32: (bound_generic_enum Swift.Optional
30+
// CHECK-32: (bound_generic_struct Swift.UnsafeMutablePointer
31+
// CHECK-32: (struct Swift.Int32)))
32+
33+
// CHECK-32: c: Swift.Int8
34+
// CHECK-32: (struct Swift.Int8)
35+
2336
// CHECK-32: TypesToReflect.AlsoHasCTypes
2437
// CHECK-32: ----------------------------
2538

@@ -82,6 +95,19 @@
8295
// CHECK-64: mcu: __C.MyCUnion
8396
// CHECK-64: (struct __C.MyCUnion)
8497

98+
// CHECK-64: __C.MyCStruct
99+
// CHECK-64: -------------
100+
// CHECK-64: i: Swift.Int32
101+
// CHECK-64: (struct Swift.Int32)
102+
103+
// CHECK-64: ip: Swift.Optional<Swift.UnsafeMutablePointer<Swift.Int32>>
104+
// CHECK-64: (bound_generic_enum Swift.Optional
105+
// CHECK-64: (bound_generic_struct Swift.UnsafeMutablePointer
106+
// CHECK-64: (struct Swift.Int32)))
107+
108+
// CHECK-64: c: Swift.Int8
109+
// CHECK-64: (struct Swift.Int8)
110+
85111
// CHECK-64: TypesToReflect.AlsoHasCTypes
86112
// CHECK-64: ----------------------------
87113

0 commit comments

Comments
 (0)