Skip to content

Commit 96dce95

Browse files
committed
[RemoteMirror] Add swift_reflection_interop_projectEnumValue()
Added a counterpart to the swift_reflection_projectEnumValue() API for the legacy interop support in SwiftRemoteMirrorLegacyInterop.h. Also updated the test to use it to extract information from an enum. rdar://62128103
1 parent 57e22bb commit 96dce95

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

include/swift/SwiftRemoteMirror/SwiftRemoteMirrorLegacyInterop.h

+18
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ swift_reflection_interop_projectExistential(SwiftReflectionInteropContextRef Con
149149
swift_typeref_interop_t *OutInstanceTypeRef,
150150
swift_addr_t *OutStartOfInstanceData);
151151

152+
static inline int swift_reflection_interop_projectEnum(
153+
SwiftReflectionInteropContextRef ContextRef, swift_addr_t EnumAddress,
154+
swift_typeref_interop_t EnumTypeRef, int *CaseIndex);
155+
152156
static inline void
153157
swift_reflection_interop_dumpTypeRef(SwiftReflectionInteropContextRef ContextRef,
154158
swift_typeref_interop_t OpaqueTypeRef);
@@ -289,6 +293,10 @@ struct SwiftReflectionFunctions {
289293
swift_typeref_t *OutInstanceTypeRef,
290294
swift_addr_t *OutStartOfInstanceData);
291295

296+
int (*projectEnumValue)(SwiftReflectionContextRef ContextRef,
297+
swift_addr_t EnumAddress, swift_typeref_t EnumTypeRef,
298+
int *CaseIndex);
299+
292300
void (*dumpTypeRef)(swift_typeref_t OpaqueTypeRef);
293301

294302
void (*dumpInfoForTypeRef)(SwiftReflectionContextRef ContextRef,
@@ -482,6 +490,7 @@ swift_reflection_interop_loadFunctions(struct SwiftReflectionInteropContext *Con
482490
LOAD(genericArgumentCountOfTypeRef);
483491
LOAD(genericArgumentOfTypeRef);
484492
LOAD(projectExistential);
493+
LOAD_OPT(projectEnumValue);
485494
LOAD(dumpTypeRef);
486495
LOAD(dumpInfoForTypeRef);
487496

@@ -1113,6 +1122,15 @@ swift_reflection_interop_projectExistential(SwiftReflectionInteropContextRef Con
11131122
return 1;
11141123
}
11151124

1125+
static inline int swift_reflection_interop_projectEnumValue(
1126+
SwiftReflectionInteropContextRef ContextRef, swift_addr_t EnumAddress,
1127+
swift_typeref_interop_t EnumTypeRef, int *CaseIndex) {
1128+
DECLARE_LIBRARY(EnumTypeRef.Library);
1129+
return Library->Functions.projectEnumValue &&
1130+
Library->Functions.projectEnumValue(Library->Context, EnumAddress,
1131+
EnumTypeRef.Typeref, CaseIndex);
1132+
}
1133+
11161134
static inline void
11171135
swift_reflection_interop_dumpTypeRef(SwiftReflectionInteropContextRef ContextRef,
11181136
swift_typeref_interop_t OpaqueTypeRef) {

unittests/Reflection/RemoteMirrorInterop/test.m

+34-2
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,40 @@ int main(int argc, char **argv) {
163163
for (unsigned i = 0; i < TypeInfo.NumFields; ++i) {
164164
swift_childinfo_interop_t ChildInfo = swift_reflection_interop_childOfInstance(
165165
Context, Obj, i);
166-
printf(" [%u]: %s Offset:%u Kind:%u\n", i,
167-
ChildInfo.Name, ChildInfo.Offset, ChildInfo.Kind);
166+
char *TypeName = swift_reflection_interop_copyDemangledNameForTypeRef(Context, ChildInfo.TR);
167+
168+
printf(" [%u]: %s Offset:%u Kind:%u Type:%s ", i,
169+
ChildInfo.Name, ChildInfo.Offset, ChildInfo.Kind, TypeName);
170+
171+
172+
switch (ChildInfo.Kind) {
173+
case SWIFT_BUILTIN:
174+
printf("(Builtin value not displayed)\n");
175+
break;
176+
case SWIFT_NO_PAYLOAD_ENUM:
177+
{
178+
int CaseNdx;
179+
180+
if (swift_reflection_interop_projectEnumValue(Context,
181+
Obj + ChildInfo.Offset,
182+
ChildInfo.TR,
183+
&CaseNdx)) {
184+
swift_childinfo_interop_t CaseInfo
185+
= swift_reflection_interop_childOfTypeRef(Context, ChildInfo.TR,
186+
CaseNdx);
187+
188+
printf("Value: %s (%d)\n", CaseInfo.Name, CaseNdx);
189+
} else {
190+
printf("Value: unknown\n");
191+
}
192+
}
193+
break;
194+
default:
195+
printf("(Value not displayed)\n");
196+
break;
197+
}
198+
199+
free(TypeName);
168200
}
169201
} else {
170202
printf("Unknown typeinfo!\n");

unittests/Reflection/RemoteMirrorInterop/test.swift

+7
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,16 @@ public func test() -> UInt {
33
return unsafeBitCast(c, to: UInt.self)
44
}
55

6+
enum E {
7+
case one
8+
case two
9+
case three
10+
}
11+
612
class C {
713
let x = "123"
814
let y = 456
15+
let e = E.two
916
}
1017

1118
let c = C()

0 commit comments

Comments
 (0)