#include "swift/Reflection/ReflectionContext.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 typeref, swift_reflection_section_t reflstr, swift_reflection_section_t assocty) { ReflectionInfo Info { ImageName, FieldSection(fieldmd.Begin, fieldmd.End), AssociatedTypeSection(assocty.Begin, assocty.End), GenericSection(reflstr.Begin, reflstr.End), GenericSection(typeref.Begin, typeref.End) }; auto Context = reinterpret_cast(ContextRef); Context->addReflectionInfo(Info); } void swift_reflection_clearCaches(SwiftReflectionContextRef ContextRef) { auto Context = reinterpret_cast(ContextRef); Context->clear(); } swift_typeref_t swift_reflection_typeRefForMetadata(SwiftReflectionContextRef ContextRef, uintptr_t metadata) { auto Context = reinterpret_cast(ContextRef); auto TR = Context->getTypeRef(metadata); return reinterpret_cast(TR.get()); } 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].get()); } } return 0; } swift_typeinfo_t swift_reflection_infoForTypeRef(SwiftReflectionContextRef ContextRef, swift_typeref_t OpaqueTypeRef) { auto Context = reinterpret_cast(ContextRef); auto TR = reinterpret_cast(OpaqueTypeRef); return Context->getInfoForTypeRef(TR); } swift_fieldinfo_t swift_reflection_infoForField(SwiftReflectionContextRef ContextRef, swift_typeref_t OpaqueTypeRef, unsigned Index) { auto Context = reinterpret_cast(ContextRef); auto TR = reinterpret_cast(OpaqueTypeRef); return Context->getInfoForField(TR, Index); }