Skip to content

Commit 936d4e5

Browse files
committed
Reflection: Flesh out SwiftRemoteMirror C API
- Add swift_reflection_genericArgumentCountOfTypeRef() - Flesh out swift_reflection_infoForTypeRef() - Flesh out swift_reflection_infoForChild()
1 parent 285e1b4 commit 936d4e5

File tree

3 files changed

+100
-36
lines changed

3 files changed

+100
-36
lines changed

include/swift/SwiftRemoteMirror/SwiftRemoteMirror.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ swift_reflection_infoForField(SwiftReflectionContextRef ContextRef,
7474
swift_typeref_t OpaqueTypeRef,
7575
unsigned Index);
7676

77+
/// Returns a fully instantiated typeref for a generic argument by index.
78+
unsigned
79+
swift_reflection_genericArgumentCountOfTypeRef(swift_typeref_t OpaqueTypeRef);
80+
7781
/// Returns a fully instantiated typeref for a generic argument by index.
7882
swift_typeref_t
7983
swift_reflection_genericArgumentOfTypeRef(swift_typeref_t OpaqueTypeRef,

include/swift/SwiftRemoteMirror/SwiftRemoteMirrorTypes.h

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,35 +40,38 @@ typedef struct swift_reflection_section_t {
4040

4141
/// The kind of a Swift type.
4242
typedef enum swift_layout_kind_t {
43-
SWIFT_STRONG_POINTER,
44-
SWIFT_WEAK_POINTER,
45-
SWIFT_UNOWNED_POINTER,
46-
SWIFT_UNMANAGED_POINTER,
47-
SWIFT_STRUCT,
43+
SWIFT_UNKNOWN,
44+
45+
SWIFT_BUILTIN,
46+
4847
SWIFT_TUPLE,
48+
SWIFT_STRUCT,
4949
SWIFT_THICK_FUNCTION,
50-
SWIFT_THIN_FUNCTION,
51-
OBJC_BLOCK,
52-
SWIFT_CLOSURE_CONTEXT,
53-
SWIFT_BOX,
54-
SWIFT_UNKNOWN
50+
51+
SWIFT_STRONG_REFERENCE,
52+
SWIFT_UNOWNED_REFERENCE,
53+
SWIFT_WEAK_REFERENCE,
54+
SWIFT_UNMANAGED_REFERENCE,
5555
} swift_layout_kind_t;
5656

57-
/// A basic description of a Swift type's.
58-
typedef struct swift_typeinfo_t {
57+
struct swift_childinfo;
58+
59+
/// A description of the memory layout of a type or field of a type.
60+
typedef struct swift_typeinfo {
5961
swift_layout_kind_t LayoutKind;
6062

61-
const char *MangledTypeName;
63+
unsigned Size;
64+
unsigned Alignment;
65+
unsigned Stride;
6266

63-
size_t Size;
64-
size_t InstanceSize;
65-
size_t Alignment;
67+
unsigned NumFields;
6668
} swift_typeinfo_t;
6769

68-
typedef struct swift_childinfo_t {
69-
swift_typeref_t TypeRef;
70+
typedef struct swift_childinfo {
71+
/// The memory for Name is owned by the reflection context.
7072
const char *Name;
71-
size_t Offset;
73+
unsigned Offset;
74+
swift_typeref_t TR;
7275
} swift_childinfo_t;
7376

7477
/// \brief An opaque pointer to a context which maintains state and

stdlib/public/SwiftRemoteMirror/SwiftRemoteMirror.cpp

Lines changed: 74 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,19 @@ swift_reflection_genericArgumentOfTypeRef(swift_typeref_t OpaqueTypeRef,
8282

8383
if (auto BG = dyn_cast<BoundGenericTypeRef>(TR)) {
8484
auto &Params = BG->getGenericParams();
85-
if (Index < Params.size()) {
86-
return reinterpret_cast<swift_typeref_t>(Params[Index]);
87-
}
85+
assert(Index < Params.size());
86+
return reinterpret_cast<swift_typeref_t>(Params[Index]);
87+
}
88+
return 0;
89+
}
90+
91+
unsigned
92+
swift_reflection_genericArgumentCountOfTypeRef(swift_typeref_t OpaqueTypeRef) {
93+
auto TR = reinterpret_cast<const TypeRef *>(OpaqueTypeRef);
94+
95+
if (auto BG = dyn_cast<BoundGenericTypeRef>(TR)) {
96+
auto &Params = BG->getGenericParams();
97+
return Params.size();
8898
}
8999
return 0;
90100
}
@@ -96,15 +106,64 @@ swift_reflection_infoForTypeRef(SwiftReflectionContextRef ContextRef,
96106
auto TR = reinterpret_cast<const TypeRef *>(OpaqueTypeRef);
97107
auto TI = Context->getTypeInfo(TR);
98108

99-
// TODO
100-
(void) TI;
109+
if (TI == nullptr) {
110+
return {
111+
SWIFT_UNKNOWN,
112+
0,
113+
0,
114+
0,
115+
0
116+
};
117+
}
118+
119+
swift_layout_kind_t Kind;
120+
unsigned NumFields = 0;
121+
122+
switch (TI->getKind()) {
123+
case TypeInfoKind::Builtin:
124+
Kind = SWIFT_BUILTIN;
125+
break;
126+
case TypeInfoKind::Record: {
127+
auto *RecordTI = cast<RecordTypeInfo>(TI);
128+
switch (RecordTI->getRecordKind()) {
129+
case RecordKind::Tuple:
130+
Kind = SWIFT_TUPLE;
131+
break;
132+
case RecordKind::Struct:
133+
Kind = SWIFT_STRUCT;
134+
break;
135+
case RecordKind::ThickFunction:
136+
Kind = SWIFT_THICK_FUNCTION;
137+
break;
138+
}
139+
NumFields = RecordTI->getNumFields();
140+
break;
141+
}
142+
case TypeInfoKind::Reference: {
143+
auto *ReferenceTI = cast<ReferenceTypeInfo>(TI);
144+
switch (ReferenceTI->getReferenceKind()) {
145+
case ReferenceKind::Strong:
146+
Kind = SWIFT_STRONG_REFERENCE;
147+
break;
148+
case ReferenceKind::Unowned:
149+
Kind = SWIFT_UNOWNED_REFERENCE;
150+
break;
151+
case ReferenceKind::Weak:
152+
Kind = SWIFT_WEAK_REFERENCE;
153+
break;
154+
case ReferenceKind::Unmanaged:
155+
Kind = SWIFT_UNMANAGED_REFERENCE;
156+
break;
157+
}
158+
}
159+
}
101160

102161
return {
103-
SWIFT_UNKNOWN,
104-
NULL,
105-
0,
106-
0,
107-
0
162+
Kind,
163+
TI->getSize(),
164+
TI->getAlignment(),
165+
TI->getStride(),
166+
NumFields
108167
};
109168
}
110169

@@ -114,15 +173,13 @@ swift_reflection_infoForChild(SwiftReflectionContextRef ContextRef,
114173
unsigned Index) {
115174
auto Context = reinterpret_cast<NativeReflectionContext *>(ContextRef);
116175
auto TR = reinterpret_cast<const TypeRef *>(OpaqueTypeRef);
117-
auto TI = Context->getTypeInfo(TR);
118176

119-
// TODO
120-
(void) TI;
177+
auto *RecordTI = cast<RecordTypeInfo>(Context->getTypeInfo(TR));
178+
auto FieldInfo = RecordTI->getFields()[Index];
121179

122180
return {
123-
0,
124-
NULL,
125-
0,
181+
FieldInfo.Name.c_str(),
182+
FieldInfo.Offset,
183+
reinterpret_cast<swift_typeref_t>(FieldInfo.TR),
126184
};
127185
}
128-

0 commit comments

Comments
 (0)