forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 342
[lldb][NFC] Expose GetReflectionContext in SwiftLanguageRuntime's API #7658
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
augusto2112
merged 1 commit into
swiftlang:stable/20230725
from
augusto2112:refactor-get-reflection-context
Oct 24, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
lldb/source/Plugins/LanguageRuntime/Swift/ReflectionContextInterface.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
#ifndef liblldb_SwiftReflectionContextInterface_h_ | ||
#define liblldb_SwiftReflectionContextInterface_h_ | ||
|
||
#include <mutex> | ||
|
||
#include "lldb/lldb-types.h" | ||
#include "swift/ABI/ObjectFile.h" | ||
#include "swift/Remote/RemoteAddress.h" | ||
#include "swift/RemoteInspection/TypeRef.h" | ||
#include "llvm/ADT/Optional.h" | ||
#include "llvm/ADT/STLFunctionalExtras.h" | ||
#include "llvm/ADT/SmallVector.h" | ||
#include "llvm/ADT/StringRef.h" | ||
#include "llvm/Support/Memory.h" | ||
|
||
namespace swift { | ||
namespace Demangle { | ||
class Demangler; | ||
} // namespace Demangle | ||
namespace reflection { | ||
class RecordTypeInfo; | ||
class TypeInfo; | ||
} // namespace reflection | ||
namespace remote { | ||
class MemoryReader; | ||
struct TypeInfoProvider; | ||
} // namespace remote | ||
} // namespace swift | ||
|
||
namespace lldb_private { | ||
struct SwiftMetadataCache; | ||
|
||
/// Returned by \ref ForEachSuperClassType. Not every user of \p | ||
/// ForEachSuperClassType needs all of these. By returning this | ||
/// object we call into the runtime only when needed. | ||
/// Using function objects to avoid instantiating ReflectionContext in this | ||
/// header. | ||
struct SuperClassType { | ||
std::function<const swift::reflection::RecordTypeInfo *()> | ||
get_record_type_info; | ||
std::function<const swift::reflection::TypeRef *()> get_typeref; | ||
}; | ||
|
||
/// An abstract interface to swift::reflection::ReflectionContext | ||
/// objects of varying pointer sizes. This class encapsulates all | ||
/// traffic to ReflectionContext and abstracts the detail that | ||
/// ReflectionContext is a template that needs to be specialized for | ||
/// a specific pointer width. | ||
class ReflectionContextInterface { | ||
public: | ||
/// Return a reflection context. | ||
static std::unique_ptr<ReflectionContextInterface> CreateReflectionContext( | ||
uint8_t pointer_size, std::shared_ptr<swift::remote::MemoryReader> reader, | ||
bool objc_interop, SwiftMetadataCache *swift_metadata_cache); | ||
|
||
virtual ~ReflectionContextInterface() = default; | ||
|
||
virtual llvm::Optional<uint32_t> AddImage( | ||
llvm::function_ref<std::pair<swift::remote::RemoteRef<void>, uint64_t>( | ||
swift::ReflectionSectionKind)> | ||
find_section, | ||
llvm::SmallVector<llvm::StringRef, 1> likely_module_names = {}) = 0; | ||
virtual llvm::Optional<uint32_t> | ||
AddImage(swift::remote::RemoteAddress image_start, | ||
llvm::SmallVector<llvm::StringRef, 1> likely_module_names = {}) = 0; | ||
virtual llvm::Optional<uint32_t> | ||
ReadELF(swift::remote::RemoteAddress ImageStart, | ||
llvm::Optional<llvm::sys::MemoryBlock> FileBuffer, | ||
llvm::SmallVector<llvm::StringRef, 1> likely_module_names = {}) = 0; | ||
virtual const swift::reflection::TypeRef * | ||
GetTypeRefOrNull(llvm::StringRef mangled_type_name) = 0; | ||
virtual const swift::reflection::TypeRef * | ||
GetTypeRefOrNull(swift::Demangle::Demangler &dem, | ||
swift::Demangle::NodePointer node) = 0; | ||
virtual const swift::reflection::TypeInfo * | ||
GetClassInstanceTypeInfo(const swift::reflection::TypeRef *type_ref, | ||
swift::remote::TypeInfoProvider *provider) = 0; | ||
virtual const swift::reflection::TypeInfo * | ||
GetTypeInfo(const swift::reflection::TypeRef *type_ref, | ||
swift::remote::TypeInfoProvider *provider) = 0; | ||
virtual const swift::reflection::TypeInfo * | ||
GetTypeInfoFromInstance(lldb::addr_t instance, | ||
swift::remote::TypeInfoProvider *provider) = 0; | ||
virtual swift::remote::MemoryReader &GetReader() = 0; | ||
virtual const swift::reflection::TypeRef * | ||
LookupSuperclass(const swift::reflection::TypeRef *tr) = 0; | ||
virtual bool | ||
ForEachSuperClassType(swift::remote::TypeInfoProvider *tip, | ||
lldb::addr_t pointer, | ||
std::function<bool(SuperClassType)> fn) = 0; | ||
virtual llvm::Optional<std::pair<const swift::reflection::TypeRef *, | ||
swift::remote::RemoteAddress>> | ||
ProjectExistentialAndUnwrapClass( | ||
swift::remote::RemoteAddress existential_addess, | ||
const swift::reflection::TypeRef &existential_tr) = 0; | ||
virtual llvm::Optional<int32_t> | ||
ProjectEnumValue(swift::remote::RemoteAddress enum_addr, | ||
const swift::reflection::TypeRef *enum_type_ref, | ||
swift::remote::TypeInfoProvider *provider) = 0; | ||
virtual const swift::reflection::TypeRef * | ||
ReadTypeFromMetadata(lldb::addr_t metadata_address, | ||
bool skip_artificial_subclasses = false) = 0; | ||
virtual const swift::reflection::TypeRef * | ||
ReadTypeFromInstance(lldb::addr_t instance_address, | ||
bool skip_artificial_subclasses = false) = 0; | ||
virtual llvm::Optional<bool> IsValueInlinedInExistentialContainer( | ||
swift::remote::RemoteAddress existential_address) = 0; | ||
virtual const swift::reflection::TypeRef * | ||
ApplySubstitutions(const swift::reflection::TypeRef *type_ref, | ||
swift::reflection::GenericArgumentMap substitutions) = 0; | ||
virtual swift::remote::RemoteAbsolutePointer | ||
StripSignedPointer(swift::remote::RemoteAbsolutePointer pointer) = 0; | ||
}; | ||
|
||
/// A wrapper around TargetReflectionContext, which holds a lock to ensure | ||
/// exclusive access. | ||
struct ThreadSafeReflectionContext { | ||
ThreadSafeReflectionContext(ReflectionContextInterface *reflection_ctx, | ||
std::recursive_mutex &mutex) | ||
: m_reflection_ctx(reflection_ctx), m_lock(mutex, std::adopt_lock) {} | ||
|
||
static ThreadSafeReflectionContext MakeInvalid() { | ||
// This exists so we can create an "empty" reflection context in the stub | ||
// language runtime. | ||
static std::recursive_mutex mutex; | ||
return ThreadSafeReflectionContext(nullptr, mutex); | ||
} | ||
|
||
ReflectionContextInterface *operator->() const { return m_reflection_ctx; } | ||
|
||
operator bool() const { return m_reflection_ctx != nullptr; } | ||
|
||
private: | ||
ReflectionContextInterface *m_reflection_ctx; | ||
// This lock operates on a recursive mutex because the initialization | ||
// of ReflectionContext recursive calls itself (see | ||
// SwiftLanguageRuntimeImpl::SetupReflection). | ||
std::lock_guard<std::recursive_mutex> m_lock; | ||
}; | ||
|
||
} // namespace lldb_private | ||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
clang-format