//===--- SwiftRemoteMirror.cpp - C wrapper for Reflection API -------------===// // // This source file is part of the Swift.org open source project // // Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See http://swift.org/LICENSE.txt for license information // See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors // //===----------------------------------------------------------------------===// #include "swift/Reflection/ReflectionContext.h" #include "swift/Reflection/TypeLowering.h" #include "swift/Remote/CMemoryReader.h" #include "swift/SwiftRemoteMirror/SwiftRemoteMirror.h" using namespace swift; using namespace swift::reflection; using namespace swift::remote; using NativeReflectionContext = ReflectionContext>>; SwiftReflectionContextRef swift_reflection_createReflectionContext(PointerSizeFunction getPointerSize, SizeSizeFunction getSizeSize, ReadBytesFunction readBytes, GetStringLengthFunction getStringLength, GetSymbolAddressFunction getSymbolAddress) { MemoryReaderImpl ReaderImpl { getPointerSize, getSizeSize, readBytes, getStringLength, getSymbolAddress }; auto Reader = std::make_shared(ReaderImpl); auto Context = new ReflectionContext>>(Reader); return reinterpret_cast(Context); } void swift_reflection_destroyReflectionContext(SwiftReflectionContextRef ContextRef) { auto Context = reinterpret_cast *>(ContextRef); delete Context; } void swift_reflection_addReflectionInfo(SwiftReflectionContextRef ContextRef, const char *ImageName, swift_reflection_section_t fieldmd, swift_reflection_section_t assocty, swift_reflection_section_t builtin, swift_reflection_section_t typeref, swift_reflection_section_t reflstr) { ReflectionInfo Info { ImageName, FieldSection(fieldmd.Begin, fieldmd.End), AssociatedTypeSection(assocty.Begin, assocty.End), BuiltinTypeSection(builtin.Begin, builtin.End), GenericSection(typeref.Begin, typeref.End), GenericSection(reflstr.Begin, reflstr.End) }; auto Context = reinterpret_cast(ContextRef); Context->addReflectionInfo(Info); } swift_typeref_t swift_reflection_typeRefForMetadata(SwiftReflectionContextRef ContextRef, uintptr_t metadata) { auto Context = reinterpret_cast(ContextRef); auto TR = Context->readTypeFromMetadata(metadata); return reinterpret_cast(TR); } swift_typeref_t swift_reflection_genericArgumentOfTypeRef(swift_typeref_t OpaqueTypeRef, unsigned Index) { auto TR = reinterpret_cast(OpaqueTypeRef); if (auto BG = dyn_cast(TR)) { auto &Params = BG->getGenericParams(); if (Index < Params.size()) { return reinterpret_cast(Params[Index]); } } return 0; } swift_typeinfo_t swift_reflection_infoForTypeRef(SwiftReflectionContextRef ContextRef, swift_typeref_t OpaqueTypeRef) { auto Context = reinterpret_cast(ContextRef); auto TR = reinterpret_cast(OpaqueTypeRef); auto TI = Context->getTypeInfo(TR); // TODO (void) TI; return { SWIFT_UNKNOWN, NULL, 0, 0, 0 }; } swift_childinfo_t swift_reflection_infoForChild(SwiftReflectionContextRef ContextRef, swift_typeref_t OpaqueTypeRef, unsigned Index) { auto Context = reinterpret_cast(ContextRef); auto TR = reinterpret_cast(OpaqueTypeRef); auto TI = Context->getTypeInfo(TR); // TODO (void) TI; return { 0, NULL, 0, }; }