Skip to content

[RemoteMirror] Add swift_reflection_interop_projectEnumValue() #38332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ swift_reflection_interop_projectExistential(SwiftReflectionInteropContextRef Con
swift_typeref_interop_t *OutInstanceTypeRef,
swift_addr_t *OutStartOfInstanceData);

static inline int swift_reflection_interop_projectEnum(
SwiftReflectionInteropContextRef ContextRef, swift_addr_t EnumAddress,
swift_typeref_interop_t EnumTypeRef, int *CaseIndex);

static inline void
swift_reflection_interop_dumpTypeRef(SwiftReflectionInteropContextRef ContextRef,
swift_typeref_interop_t OpaqueTypeRef);
Expand Down Expand Up @@ -289,6 +293,10 @@ struct SwiftReflectionFunctions {
swift_typeref_t *OutInstanceTypeRef,
swift_addr_t *OutStartOfInstanceData);

int (*projectEnumValue)(SwiftReflectionContextRef ContextRef,
swift_addr_t EnumAddress, swift_typeref_t EnumTypeRef,
int *CaseIndex);

void (*dumpTypeRef)(swift_typeref_t OpaqueTypeRef);

void (*dumpInfoForTypeRef)(SwiftReflectionContextRef ContextRef,
Expand Down Expand Up @@ -482,6 +490,7 @@ swift_reflection_interop_loadFunctions(struct SwiftReflectionInteropContext *Con
LOAD(genericArgumentCountOfTypeRef);
LOAD(genericArgumentOfTypeRef);
LOAD(projectExistential);
LOAD_OPT(projectEnumValue);
LOAD(dumpTypeRef);
LOAD(dumpInfoForTypeRef);

Expand Down Expand Up @@ -1113,6 +1122,15 @@ swift_reflection_interop_projectExistential(SwiftReflectionInteropContextRef Con
return 1;
}

static inline int swift_reflection_interop_projectEnumValue(
SwiftReflectionInteropContextRef ContextRef, swift_addr_t EnumAddress,
swift_typeref_interop_t EnumTypeRef, int *CaseIndex) {
DECLARE_LIBRARY(EnumTypeRef.Library);
return Library->Functions.projectEnumValue &&
Library->Functions.projectEnumValue(Library->Context, EnumAddress,
EnumTypeRef.Typeref, CaseIndex);
}

static inline void
swift_reflection_interop_dumpTypeRef(SwiftReflectionInteropContextRef ContextRef,
swift_typeref_interop_t OpaqueTypeRef) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,40 @@ int main(int argc, char **argv) {
for (unsigned i = 0; i < TypeInfo.NumFields; ++i) {
swift_childinfo_interop_t ChildInfo = swift_reflection_interop_childOfInstance(
Context, Obj, i);
printf(" [%u]: %s Offset:%u Kind:%u\n", i,
ChildInfo.Name, ChildInfo.Offset, ChildInfo.Kind);
char *TypeName = swift_reflection_interop_copyDemangledNameForTypeRef(Context, ChildInfo.TR);

printf(" [%u]: %s Offset:%u Kind:%u Type:%s ", i,
ChildInfo.Name, ChildInfo.Offset, ChildInfo.Kind, TypeName);


switch (ChildInfo.Kind) {
case SWIFT_BUILTIN:
printf("(Builtin value not displayed)\n");
break;
case SWIFT_NO_PAYLOAD_ENUM:
{
int CaseNdx;

if (swift_reflection_interop_projectEnumValue(Context,
Obj + ChildInfo.Offset,
ChildInfo.TR,
&CaseNdx)) {
swift_childinfo_interop_t CaseInfo
= swift_reflection_interop_childOfTypeRef(Context, ChildInfo.TR,
CaseNdx);

printf("Value: %s (%d)\n", CaseInfo.Name, CaseNdx);
} else {
printf("Value: unknown\n");
}
}
break;
default:
printf("(Value not displayed)\n");
break;
}

free(TypeName);
}
} else {
printf("Unknown typeinfo!\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ def libPath(path):
'-I', '../../../include/',
'-o', '/tmp/test',
'-Wall', '-Wextra',
'-g', 'test.m'])
'-g', 'interop.m'])

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@ public func test() -> UInt {
return unsafeBitCast(c, to: UInt.self)
}

enum E {
case one
case two
case three
}

class C {
let x = "123"
let y = 456
let e = E.two
}

let c = C()
7 changes: 7 additions & 0 deletions test/RemoteMirror/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Remote Mirror Tests
===================

This directory contains tests for the remote mirror code. Note in particular
that the interop test that runs from here only tests the version of Swift that
we are currently building. There is a script in the Inputs folder,
"interop.py", that can be used to test multiple versions together.
18 changes: 18 additions & 0 deletions test/RemoteMirror/interop.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// REQUIRES: OS=macosx

// RUN: %empty-directory(%t)
// RUN: %target-build-swift %S/Inputs/interop.swift -emit-library -module-name InteropTest -o %t/%target-library-name(InteropTest)
// RUN: %target-clang %S/Inputs/interop.m -framework Foundation -I %S/../../include/swift/SwiftRemoteMirror -I %S/../../include/ -o %t/InteropTest
// RUN: %target-codesign %t/%target-library-name(InteropTest)
// RUN: %target-codesign %t/InteropTest
// RUN: %target-run %t/InteropTest %t/%target-library-name(InteropTest) %platform-module-dir/%target-library-name(swiftRemoteMirror) | %FileCheck %s

// CHECK: Kind:13 Size:{{[0-9]+}} Alignment:{{[0-9]+}} Stride:{{[0-9]+}} NumFields:0
// CHECK-NEXT: Demangled name: InteropTest.C
// CHECK-NEXT: Original metadata: 0x{{[0-9A-Fa-f]+}}
// CHECK-NEXT: Looked up metadata: Metadata=0x{{[0-9A-Fa-f]+}} Library={{[0-9]+}}
// CHECK-NEXT: Kind:17 Size:{{[0-9]+}} Alignment:{{[0-9]+}} Stride:{{[0-9]+}} NumFields:3
// CHECK-NEXT: [0]: x Offset:{{[0-9]+}} Kind:4 Type:Swift.String (Value not displayed)
// CHECK-NEXT: [1]: y Offset:{{[0-9]+}} Kind:4 Type:Swift.Int (Value not displayed)
// CHECK-NEXT: [2]: e Offset:{{[0-9]+}} Kind:5 Type:InteropTest.E Value: two (1)