1 //===-- BlockPointer.cpp ----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "BlockPointer.h"
12 #include "lldb/Core/ValueObject.h"
13 #include "lldb/DataFormatters/FormattersHelpers.h"
14 #include "lldb/Symbol/ClangASTContext.h"
15 #include "lldb/Symbol/ClangASTImporter.h"
16 #include "lldb/Symbol/CompilerType.h"
17 #include "lldb/Symbol/TypeSystem.h"
18 #include "lldb/Target/Target.h"
20 #include "lldb/Utility/LLDBAssert.h"
23 using namespace lldb_private;
24 using namespace lldb_private::formatters;
26 namespace lldb_private {
27 namespace formatters {
29 class BlockPointerSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
31 BlockPointerSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
32 : SyntheticChildrenFrontEnd(*valobj_sp), m_block_struct_type() {
33 CompilerType block_pointer_type(m_backend.GetCompilerType());
34 CompilerType function_pointer_type;
35 block_pointer_type.IsBlockPointerType(&function_pointer_type);
37 TargetSP target_sp(m_backend.GetTargetSP());
44 TypeSystem *type_system = target_sp->GetScratchTypeSystemForLanguage(
45 &err, lldb::eLanguageTypeC_plus_plus);
47 if (!err.Success() || !type_system) {
51 ClangASTContext *clang_ast_context =
52 llvm::dyn_cast<ClangASTContext>(type_system);
54 if (!clang_ast_context) {
58 ClangASTImporterSP clang_ast_importer = target_sp->GetClangASTImporter();
60 if (!clang_ast_importer) {
64 const char *const isa_name("__isa");
65 const CompilerType isa_type =
66 clang_ast_context->GetBasicType(lldb::eBasicTypeObjCClass);
67 const char *const flags_name("__flags");
68 const CompilerType flags_type =
69 clang_ast_context->GetBasicType(lldb::eBasicTypeInt);
70 const char *const reserved_name("__reserved");
71 const CompilerType reserved_type =
72 clang_ast_context->GetBasicType(lldb::eBasicTypeInt);
73 const char *const FuncPtr_name("__FuncPtr");
74 const CompilerType FuncPtr_type =
75 clang_ast_importer->CopyType(*clang_ast_context, function_pointer_type);
77 m_block_struct_type = clang_ast_context->CreateStructForIdentifier(
78 ConstString(), {{isa_name, isa_type},
79 {flags_name, flags_type},
80 {reserved_name, reserved_type},
81 {FuncPtr_name, FuncPtr_type}});
84 ~BlockPointerSyntheticFrontEnd() override = default;
86 size_t CalculateNumChildren() override {
87 const bool omit_empty_base_classes = false;
88 return m_block_struct_type.GetNumChildren(omit_empty_base_classes, nullptr);
91 lldb::ValueObjectSP GetChildAtIndex(size_t idx) override {
92 if (!m_block_struct_type.IsValid()) {
93 return lldb::ValueObjectSP();
96 if (idx >= CalculateNumChildren()) {
97 return lldb::ValueObjectSP();
100 const bool thread_and_frame_only_if_stopped = true;
101 ExecutionContext exe_ctx = m_backend.GetExecutionContextRef().Lock(
102 thread_and_frame_only_if_stopped);
103 const bool transparent_pointers = false;
104 const bool omit_empty_base_classes = false;
105 const bool ignore_array_bounds = false;
106 ValueObject *value_object = nullptr;
108 std::string child_name;
109 uint32_t child_byte_size = 0;
110 int32_t child_byte_offset = 0;
111 uint32_t child_bitfield_bit_size = 0;
112 uint32_t child_bitfield_bit_offset = 0;
113 bool child_is_base_class = false;
114 bool child_is_deref_of_parent = false;
115 uint64_t language_flags = 0;
117 const CompilerType child_type =
118 m_block_struct_type.GetChildCompilerTypeAtIndex(
119 &exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
120 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
121 child_bitfield_bit_size, child_bitfield_bit_offset,
122 child_is_base_class, child_is_deref_of_parent, value_object,
125 ValueObjectSP struct_pointer_sp =
126 m_backend.Cast(m_block_struct_type.GetPointerType());
128 if (!struct_pointer_sp) {
129 return lldb::ValueObjectSP();
133 ValueObjectSP struct_sp = struct_pointer_sp->Dereference(err);
135 if (!struct_sp || !err.Success()) {
136 return lldb::ValueObjectSP();
139 ValueObjectSP child_sp(struct_sp->GetSyntheticChildAtOffset(
140 child_byte_offset, child_type, true,
141 ConstString(child_name.c_str(), child_name.size())));
146 // return true if this object is now safe to use forever without ever
147 // updating again; the typical (and tested) answer here is 'false'
148 bool Update() override { return false; }
150 // maybe return false if the block pointer is, say, null
151 bool MightHaveChildren() override { return true; }
153 size_t GetIndexOfChildWithName(const ConstString &name) override {
154 if (!m_block_struct_type.IsValid())
157 const bool omit_empty_base_classes = false;
158 return m_block_struct_type.GetIndexOfChildWithName(name.AsCString(),
159 omit_empty_base_classes);
163 CompilerType m_block_struct_type;
166 } // namespace formatters
167 } // namespace lldb_private
169 bool lldb_private::formatters::BlockPointerSummaryProvider(
170 ValueObject &valobj, Stream &s, const TypeSummaryOptions &) {
171 lldb_private::SyntheticChildrenFrontEnd *synthetic_children =
172 BlockPointerSyntheticFrontEndCreator(nullptr, valobj.GetSP());
173 if (!synthetic_children) {
177 synthetic_children->Update();
179 static const ConstString s_FuncPtr_name("__FuncPtr");
181 lldb::ValueObjectSP child_sp = synthetic_children->GetChildAtIndex(
182 synthetic_children->GetIndexOfChildWithName(s_FuncPtr_name));
188 lldb::ValueObjectSP qualified_child_representation_sp =
189 child_sp->GetQualifiedRepresentationIfAvailable(
190 lldb::eDynamicDontRunTarget, true);
192 const char *child_value =
193 qualified_child_representation_sp->GetValueAsCString();
195 s.Printf("%s", child_value);
200 lldb_private::SyntheticChildrenFrontEnd *
201 lldb_private::formatters::BlockPointerSyntheticFrontEndCreator(
202 CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
205 return new BlockPointerSyntheticFrontEnd(valobj_sp);