1 //===-- BlockPointer.cpp ----------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "BlockPointer.h"
11 #include "lldb/Core/ValueObject.h"
12 #include "lldb/DataFormatters/FormattersHelpers.h"
13 #include "lldb/Symbol/ClangASTContext.h"
14 #include "lldb/Symbol/ClangASTImporter.h"
15 #include "lldb/Symbol/CompilerType.h"
16 #include "lldb/Symbol/TypeSystem.h"
17 #include "lldb/Target/Target.h"
19 #include "lldb/Utility/LLDBAssert.h"
22 using namespace lldb_private;
23 using namespace lldb_private::formatters;
25 namespace lldb_private {
26 namespace formatters {
28 class BlockPointerSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
30 BlockPointerSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
31 : SyntheticChildrenFrontEnd(*valobj_sp), m_block_struct_type() {
32 CompilerType block_pointer_type(m_backend.GetCompilerType());
33 CompilerType function_pointer_type;
34 block_pointer_type.IsBlockPointerType(&function_pointer_type);
36 TargetSP target_sp(m_backend.GetTargetSP());
43 TypeSystem *type_system = target_sp->GetScratchTypeSystemForLanguage(
44 &err, lldb::eLanguageTypeC_plus_plus);
46 if (!err.Success() || !type_system) {
50 ClangASTContext *clang_ast_context =
51 llvm::dyn_cast<ClangASTContext>(type_system);
53 if (!clang_ast_context) {
57 ClangASTImporterSP clang_ast_importer = target_sp->GetClangASTImporter();
59 if (!clang_ast_importer) {
63 const char *const isa_name("__isa");
64 const CompilerType isa_type =
65 clang_ast_context->GetBasicType(lldb::eBasicTypeObjCClass);
66 const char *const flags_name("__flags");
67 const CompilerType flags_type =
68 clang_ast_context->GetBasicType(lldb::eBasicTypeInt);
69 const char *const reserved_name("__reserved");
70 const CompilerType reserved_type =
71 clang_ast_context->GetBasicType(lldb::eBasicTypeInt);
72 const char *const FuncPtr_name("__FuncPtr");
73 const CompilerType FuncPtr_type =
74 clang_ast_importer->CopyType(*clang_ast_context, function_pointer_type);
76 m_block_struct_type = clang_ast_context->CreateStructForIdentifier(
77 ConstString(), {{isa_name, isa_type},
78 {flags_name, flags_type},
79 {reserved_name, reserved_type},
80 {FuncPtr_name, FuncPtr_type}});
83 ~BlockPointerSyntheticFrontEnd() override = default;
85 size_t CalculateNumChildren() override {
86 const bool omit_empty_base_classes = false;
87 return m_block_struct_type.GetNumChildren(omit_empty_base_classes, nullptr);
90 lldb::ValueObjectSP GetChildAtIndex(size_t idx) override {
91 if (!m_block_struct_type.IsValid()) {
92 return lldb::ValueObjectSP();
95 if (idx >= CalculateNumChildren()) {
96 return lldb::ValueObjectSP();
99 const bool thread_and_frame_only_if_stopped = true;
100 ExecutionContext exe_ctx = m_backend.GetExecutionContextRef().Lock(
101 thread_and_frame_only_if_stopped);
102 const bool transparent_pointers = false;
103 const bool omit_empty_base_classes = false;
104 const bool ignore_array_bounds = false;
105 ValueObject *value_object = nullptr;
107 std::string child_name;
108 uint32_t child_byte_size = 0;
109 int32_t child_byte_offset = 0;
110 uint32_t child_bitfield_bit_size = 0;
111 uint32_t child_bitfield_bit_offset = 0;
112 bool child_is_base_class = false;
113 bool child_is_deref_of_parent = false;
114 uint64_t language_flags = 0;
116 const CompilerType child_type =
117 m_block_struct_type.GetChildCompilerTypeAtIndex(
118 &exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
119 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
120 child_bitfield_bit_size, child_bitfield_bit_offset,
121 child_is_base_class, child_is_deref_of_parent, value_object,
124 ValueObjectSP struct_pointer_sp =
125 m_backend.Cast(m_block_struct_type.GetPointerType());
127 if (!struct_pointer_sp) {
128 return lldb::ValueObjectSP();
132 ValueObjectSP struct_sp = struct_pointer_sp->Dereference(err);
134 if (!struct_sp || !err.Success()) {
135 return lldb::ValueObjectSP();
138 ValueObjectSP child_sp(struct_sp->GetSyntheticChildAtOffset(
139 child_byte_offset, child_type, true,
140 ConstString(child_name.c_str(), child_name.size())));
145 // return true if this object is now safe to use forever without ever
146 // updating again; the typical (and tested) answer here is 'false'
147 bool Update() override { return false; }
149 // maybe return false if the block pointer is, say, null
150 bool MightHaveChildren() override { return true; }
152 size_t GetIndexOfChildWithName(ConstString name) override {
153 if (!m_block_struct_type.IsValid())
156 const bool omit_empty_base_classes = false;
157 return m_block_struct_type.GetIndexOfChildWithName(name.AsCString(),
158 omit_empty_base_classes);
162 CompilerType m_block_struct_type;
165 } // namespace formatters
166 } // namespace lldb_private
168 bool lldb_private::formatters::BlockPointerSummaryProvider(
169 ValueObject &valobj, Stream &s, const TypeSummaryOptions &) {
170 lldb_private::SyntheticChildrenFrontEnd *synthetic_children =
171 BlockPointerSyntheticFrontEndCreator(nullptr, valobj.GetSP());
172 if (!synthetic_children) {
176 synthetic_children->Update();
178 static const ConstString s_FuncPtr_name("__FuncPtr");
180 lldb::ValueObjectSP child_sp = synthetic_children->GetChildAtIndex(
181 synthetic_children->GetIndexOfChildWithName(s_FuncPtr_name));
187 lldb::ValueObjectSP qualified_child_representation_sp =
188 child_sp->GetQualifiedRepresentationIfAvailable(
189 lldb::eDynamicDontRunTarget, true);
191 const char *child_value =
192 qualified_child_representation_sp->GetValueAsCString();
194 s.Printf("%s", child_value);
199 lldb_private::SyntheticChildrenFrontEnd *
200 lldb_private::formatters::BlockPointerSyntheticFrontEndCreator(
201 CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
204 return new BlockPointerSyntheticFrontEnd(valobj_sp);