|
| 1 | +#ifndef liblldb_SwiftReflectionContextInterface_h_ |
| 2 | +#define liblldb_SwiftReflectionContextInterface_h_ |
| 3 | + |
| 4 | +#include <mutex> |
| 5 | + |
| 6 | +#include "lldb/lldb-types.h" |
| 7 | +#include "swift/ABI/ObjectFile.h" |
| 8 | +#include "swift/Remote/RemoteAddress.h" |
| 9 | +#include "swift/RemoteInspection/TypeRef.h" |
| 10 | +#include "llvm/ADT/Optional.h" |
| 11 | +#include "llvm/ADT/STLFunctionalExtras.h" |
| 12 | +#include "llvm/ADT/SmallVector.h" |
| 13 | +#include "llvm/ADT/StringRef.h" |
| 14 | +#include "llvm/Support/Memory.h" |
| 15 | + |
| 16 | +namespace swift { |
| 17 | +namespace Demangle { |
| 18 | +class Demangler; |
| 19 | +} // namespace Demangle |
| 20 | +namespace reflection { |
| 21 | +class RecordTypeInfo; |
| 22 | +class TypeInfo; |
| 23 | +} // namespace reflection |
| 24 | +namespace remote { |
| 25 | +class MemoryReader; |
| 26 | +struct TypeInfoProvider; |
| 27 | +} // namespace remote |
| 28 | +} // namespace swift |
| 29 | + |
| 30 | +namespace lldb_private { |
| 31 | +struct SwiftMetadataCache; |
| 32 | + |
| 33 | +/// Returned by \ref ForEachSuperClassType. Not every user of \p |
| 34 | +/// ForEachSuperClassType needs all of these. By returning this |
| 35 | +/// object we call into the runtime only when needed. |
| 36 | +/// Using function objects to avoid instantiating ReflectionContext in this |
| 37 | +/// header. |
| 38 | +struct SuperClassType { |
| 39 | + std::function<const swift::reflection::RecordTypeInfo *()> |
| 40 | + get_record_type_info; |
| 41 | + std::function<const swift::reflection::TypeRef *()> get_typeref; |
| 42 | +}; |
| 43 | + |
| 44 | +/// An abstract interface to swift::reflection::ReflectionContext |
| 45 | +/// objects of varying pointer sizes. This class encapsulates all |
| 46 | +/// traffic to ReflectionContext and abstracts the detail that |
| 47 | +/// ReflectionContext is a template that needs to be specialized for |
| 48 | +/// a specific pointer width. |
| 49 | +class ReflectionContextInterface { |
| 50 | +public: |
| 51 | + /// Return a reflection context. |
| 52 | + static std::unique_ptr<ReflectionContextInterface> CreateReflectionContext( |
| 53 | + uint8_t pointer_size, std::shared_ptr<swift::remote::MemoryReader> reader, |
| 54 | + bool objc_interop, SwiftMetadataCache *swift_metadata_cache); |
| 55 | + |
| 56 | + virtual ~ReflectionContextInterface() = default; |
| 57 | + |
| 58 | + virtual llvm::Optional<uint32_t> AddImage( |
| 59 | + llvm::function_ref<std::pair<swift::remote::RemoteRef<void>, uint64_t>( |
| 60 | + swift::ReflectionSectionKind)> |
| 61 | + find_section, |
| 62 | + llvm::SmallVector<llvm::StringRef, 1> likely_module_names = {}) = 0; |
| 63 | + virtual llvm::Optional<uint32_t> |
| 64 | + AddImage(swift::remote::RemoteAddress image_start, |
| 65 | + llvm::SmallVector<llvm::StringRef, 1> likely_module_names = {}) = 0; |
| 66 | + virtual llvm::Optional<uint32_t> |
| 67 | + ReadELF(swift::remote::RemoteAddress ImageStart, |
| 68 | + llvm::Optional<llvm::sys::MemoryBlock> FileBuffer, |
| 69 | + llvm::SmallVector<llvm::StringRef, 1> likely_module_names = {}) = 0; |
| 70 | + virtual const swift::reflection::TypeRef * |
| 71 | + GetTypeRefOrNull(llvm::StringRef mangled_type_name) = 0; |
| 72 | + virtual const swift::reflection::TypeRef * |
| 73 | + GetTypeRefOrNull(swift::Demangle::Demangler &dem, |
| 74 | + swift::Demangle::NodePointer node) = 0; |
| 75 | + virtual const swift::reflection::TypeInfo * |
| 76 | + GetClassInstanceTypeInfo(const swift::reflection::TypeRef *type_ref, |
| 77 | + swift::remote::TypeInfoProvider *provider) = 0; |
| 78 | + virtual const swift::reflection::TypeInfo * |
| 79 | + GetTypeInfo(const swift::reflection::TypeRef *type_ref, |
| 80 | + swift::remote::TypeInfoProvider *provider) = 0; |
| 81 | + virtual const swift::reflection::TypeInfo * |
| 82 | + GetTypeInfoFromInstance(lldb::addr_t instance, |
| 83 | + swift::remote::TypeInfoProvider *provider) = 0; |
| 84 | + virtual swift::remote::MemoryReader &GetReader() = 0; |
| 85 | + virtual const swift::reflection::TypeRef * |
| 86 | + LookupSuperclass(const swift::reflection::TypeRef *tr) = 0; |
| 87 | + virtual bool |
| 88 | + ForEachSuperClassType(swift::remote::TypeInfoProvider *tip, |
| 89 | + lldb::addr_t pointer, |
| 90 | + std::function<bool(SuperClassType)> fn) = 0; |
| 91 | + virtual llvm::Optional<std::pair<const swift::reflection::TypeRef *, |
| 92 | + swift::remote::RemoteAddress>> |
| 93 | + ProjectExistentialAndUnwrapClass( |
| 94 | + swift::remote::RemoteAddress existential_addess, |
| 95 | + const swift::reflection::TypeRef &existential_tr) = 0; |
| 96 | + virtual llvm::Optional<int32_t> |
| 97 | + ProjectEnumValue(swift::remote::RemoteAddress enum_addr, |
| 98 | + const swift::reflection::TypeRef *enum_type_ref, |
| 99 | + swift::remote::TypeInfoProvider *provider) = 0; |
| 100 | + virtual const swift::reflection::TypeRef * |
| 101 | + ReadTypeFromMetadata(lldb::addr_t metadata_address, |
| 102 | + bool skip_artificial_subclasses = false) = 0; |
| 103 | + virtual const swift::reflection::TypeRef * |
| 104 | + ReadTypeFromInstance(lldb::addr_t instance_address, |
| 105 | + bool skip_artificial_subclasses = false) = 0; |
| 106 | + virtual llvm::Optional<bool> IsValueInlinedInExistentialContainer( |
| 107 | + swift::remote::RemoteAddress existential_address) = 0; |
| 108 | + virtual const swift::reflection::TypeRef * |
| 109 | + ApplySubstitutions(const swift::reflection::TypeRef *type_ref, |
| 110 | + swift::reflection::GenericArgumentMap substitutions) = 0; |
| 111 | + virtual swift::remote::RemoteAbsolutePointer |
| 112 | + StripSignedPointer(swift::remote::RemoteAbsolutePointer pointer) = 0; |
| 113 | +}; |
| 114 | + |
| 115 | +/// A wrapper around TargetReflectionContext, which holds a lock to ensure |
| 116 | +/// exclusive access. |
| 117 | +struct ThreadSafeReflectionContext { |
| 118 | + ThreadSafeReflectionContext(ReflectionContextInterface *reflection_ctx, |
| 119 | + std::recursive_mutex &mutex) |
| 120 | + : m_reflection_ctx(reflection_ctx), m_lock(mutex, std::adopt_lock) {} |
| 121 | + |
| 122 | + static ThreadSafeReflectionContext MakeInvalid() { |
| 123 | + // This exists so we can create an "empty" reflection context in the stub |
| 124 | + // language runtime. |
| 125 | + static std::recursive_mutex mutex; |
| 126 | + return ThreadSafeReflectionContext(nullptr, mutex); |
| 127 | + } |
| 128 | + |
| 129 | + ReflectionContextInterface *operator->() const { return m_reflection_ctx; } |
| 130 | + |
| 131 | + operator bool() const { return m_reflection_ctx != nullptr; } |
| 132 | + |
| 133 | +private: |
| 134 | + ReflectionContextInterface *m_reflection_ctx; |
| 135 | + // This lock operates on a recursive mutex because the initialization |
| 136 | + // of ReflectionContext recursive calls itself (see |
| 137 | + // SwiftLanguageRuntimeImpl::SetupReflection). |
| 138 | + std::lock_guard<std::recursive_mutex> m_lock; |
| 139 | +}; |
| 140 | + |
| 141 | +} // namespace lldb_private |
| 142 | +#endif |
0 commit comments