Skip to content

Commit 030e0ff

Browse files
authored
Merge pull request #38332 from al45tair/problems/62128103
[RemoteMirror] Add swift_reflection_interop_projectEnumValue()
2 parents 119e886 + c3df37c commit 030e0ff

File tree

6 files changed

+86
-4
lines changed

6 files changed

+86
-4
lines changed

include/swift/SwiftRemoteMirror/SwiftRemoteMirrorLegacyInterop.h

Lines changed: 18 additions & 0 deletions
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 renamed to test/RemoteMirror/Inputs/interop.m

Lines changed: 34 additions & 2 deletions
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.py renamed to test/RemoteMirror/Inputs/interop.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ def libPath(path):
4545
'-I', '../../../include/',
4646
'-o', '/tmp/test',
4747
'-Wall', '-Wextra',
48-
'-g', 'test.m'])
48+
'-g', 'interop.m'])
4949

5050
# Build a test library with each Swift compiler passed in.
5151
for i, (swiftc, swiftlib) in enumerate(zip(swiftcs, swiftlibs)):
5252
subprocess.check_call(
53-
['xcrun', swiftc, '-emit-library', 'test.swift',
53+
['xcrun', swiftc, '-emit-library', 'interop.swift',
5454
'-o', os.path.join('/tmp', 'libtest' + str(i) + '.dylib'),
5555
'-Xlinker', '-rpath', '-Xlinker', swiftlib])
5656

unittests/Reflection/RemoteMirrorInterop/test.swift renamed to test/RemoteMirror/Inputs/interop.swift

Lines changed: 7 additions & 0 deletions
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()

test/RemoteMirror/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Remote Mirror Tests
2+
===================
3+
4+
This directory contains tests for the remote mirror code. Note in particular
5+
that the interop test that runs from here only tests the version of Swift that
6+
we are currently building. There is a script in the Inputs folder,
7+
"interop.py", that can be used to test multiple versions together.

test/RemoteMirror/interop.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// REQUIRES: OS=macosx
2+
3+
// RUN: %empty-directory(%t)
4+
// RUN: %target-build-swift %S/Inputs/interop.swift -emit-library -module-name InteropTest -o %t/%target-library-name(InteropTest)
5+
// RUN: %target-clang %S/Inputs/interop.m -framework Foundation -I %S/../../include/swift/SwiftRemoteMirror -I %S/../../include/ -o %t/InteropTest
6+
// RUN: %target-codesign %t/%target-library-name(InteropTest)
7+
// RUN: %target-codesign %t/InteropTest
8+
// RUN: %target-run %t/InteropTest %t/%target-library-name(InteropTest) %platform-module-dir/%target-library-name(swiftRemoteMirror) | %FileCheck %s
9+
10+
// CHECK: Kind:13 Size:{{[0-9]+}} Alignment:{{[0-9]+}} Stride:{{[0-9]+}} NumFields:0
11+
// CHECK-NEXT: Demangled name: InteropTest.C
12+
// CHECK-NEXT: Original metadata: 0x{{[0-9A-Fa-f]+}}
13+
// CHECK-NEXT: Looked up metadata: Metadata=0x{{[0-9A-Fa-f]+}} Library={{[0-9]+}}
14+
// CHECK-NEXT: Kind:17 Size:{{[0-9]+}} Alignment:{{[0-9]+}} Stride:{{[0-9]+}} NumFields:3
15+
// CHECK-NEXT: [0]: x Offset:{{[0-9]+}} Kind:4 Type:Swift.String (Value not displayed)
16+
// CHECK-NEXT: [1]: y Offset:{{[0-9]+}} Kind:4 Type:Swift.Int (Value not displayed)
17+
// CHECK-NEXT: [2]: e Offset:{{[0-9]+}} Kind:5 Type:InteropTest.E Value: two (1)
18+

0 commit comments

Comments
 (0)