blob: 9d50c5cb7c9a7dc9365f35ad8bf4a8da45e49762 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Block.cpp -----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Symbol/Block.h"
Greg Clayton6c7f5612011-09-29 23:41:34 +000011
Greg Clayton6c7f5612011-09-29 23:41:34 +000012#include "lldb/Core/Log.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000013#include "lldb/Core/Module.h"
14#include "lldb/Core/Section.h"
Greg Clayton6c7f5612011-09-29 23:41:34 +000015#include "lldb/Symbol/Function.h"
Sean Callanan72e49402011-08-05 23:43:37 +000016#include "lldb/Symbol/SymbolFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/Symbol/SymbolVendor.h"
18#include "lldb/Symbol/VariableList.h"
19
20using namespace lldb;
21using namespace lldb_private;
22
Greg Clayton0b76a2c2010-08-21 02:22:51 +000023Block::Block(lldb::user_id_t uid) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024 UserID(uid),
Ed Masted4612ad2014-04-20 13:17:36 +000025 m_parent_scope (nullptr),
Greg Clayton0b76a2c2010-08-21 02:22:51 +000026 m_children (),
27 m_ranges (),
28 m_inlineInfoSP (),
Greg Clayton9da7bd02010-08-24 21:05:24 +000029 m_variable_list_sp (),
Greg Clayton0b76a2c2010-08-21 02:22:51 +000030 m_parsed_block_info (false),
31 m_parsed_block_variables (false),
32 m_parsed_child_blocks (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033{
34}
35
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036Block::~Block ()
37{
38}
39
40void
Greg Claytonf5e56de2010-09-14 23:36:40 +000041Block::GetDescription(Stream *s, Function *function, lldb::DescriptionLevel level, Target *target) const
Greg Clayton0c5cd902010-06-28 21:30:43 +000042{
Greg Claytonc9800662010-09-10 01:30:46 +000043 *s << "id = " << ((const UserID&)*this);
44
Greg Claytonea3e7d52011-10-08 00:49:15 +000045 size_t num_ranges = m_ranges.GetSize();
46 if (num_ranges > 0)
Greg Clayton0c5cd902010-06-28 21:30:43 +000047 {
48
49 addr_t base_addr = LLDB_INVALID_ADDRESS;
Greg Claytonf5e56de2010-09-14 23:36:40 +000050 if (target)
51 base_addr = function->GetAddressRange().GetBaseAddress().GetLoadAddress(target);
Greg Clayton0c5cd902010-06-28 21:30:43 +000052 if (base_addr == LLDB_INVALID_ADDRESS)
Greg Clayton0b76a2c2010-08-21 02:22:51 +000053 base_addr = function->GetAddressRange().GetBaseAddress().GetFileAddress();
Greg Clayton0c5cd902010-06-28 21:30:43 +000054
Greg Claytonc9800662010-09-10 01:30:46 +000055 s->Printf(", range%s = ", num_ranges > 1 ? "s" : "");
Greg Claytonea3e7d52011-10-08 00:49:15 +000056 for (size_t i=0; i<num_ranges; ++i)
57 {
58 const Range &range = m_ranges.GetEntryRef(i);
59 s->AddressRange(base_addr + range.GetRangeBase(), base_addr + range.GetRangeEnd(), 4);
60 }
Greg Clayton0c5cd902010-06-28 21:30:43 +000061 }
Greg Clayton0c5cd902010-06-28 21:30:43 +000062
Ed Masted4612ad2014-04-20 13:17:36 +000063 if (m_inlineInfoSP.get() != nullptr)
Greg Clayton6dbd3982010-09-15 05:51:24 +000064 {
65 bool show_fullpaths = (level == eDescriptionLevelVerbose);
66 m_inlineInfoSP->Dump(s, show_fullpaths);
67 }
Greg Clayton0c5cd902010-06-28 21:30:43 +000068}
69
70void
Chris Lattner30fdc8d2010-06-08 16:52:24 +000071Block::Dump(Stream *s, addr_t base_addr, int32_t depth, bool show_context) const
72{
73 if (depth < 0)
74 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +000075 Block *parent = GetParent();
76 if (parent)
77 {
78 // We have a depth that is less than zero, print our parent blocks
79 // first
80 parent->Dump(s, base_addr, depth + 1, show_context);
81 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000082 }
83
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000084 s->Printf("%p: ", static_cast<const void*>(this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +000085 s->Indent();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000086 *s << "Block" << static_cast<const UserID&>(*this);
Greg Clayton0c5cd902010-06-28 21:30:43 +000087 const Block* parent_block = GetParent();
88 if (parent_block)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000089 {
Daniel Malead01b2952012-11-29 21:49:15 +000090 s->Printf(", parent = {0x%8.8" PRIx64 "}", parent_block->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000091 }
Ed Masted4612ad2014-04-20 13:17:36 +000092 if (m_inlineInfoSP.get() != nullptr)
Greg Clayton6dbd3982010-09-15 05:51:24 +000093 {
94 bool show_fullpaths = false;
95 m_inlineInfoSP->Dump(s, show_fullpaths);
96 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000097
Greg Claytonea3e7d52011-10-08 00:49:15 +000098 if (!m_ranges.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +000099 {
100 *s << ", ranges =";
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000101
Greg Claytonea3e7d52011-10-08 00:49:15 +0000102 size_t num_ranges = m_ranges.GetSize();
103 for (size_t i=0; i<num_ranges; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000104 {
Greg Claytonea3e7d52011-10-08 00:49:15 +0000105 const Range &range = m_ranges.GetEntryRef(i);
Ed Masted4612ad2014-04-20 13:17:36 +0000106 if (parent_block != nullptr && parent_block->Contains(range) == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000107 *s << '!';
108 else
109 *s << ' ';
Greg Claytonea3e7d52011-10-08 00:49:15 +0000110 s->AddressRange(base_addr + range.GetRangeBase(), base_addr + range.GetRangeEnd(), 4);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000111 }
112 }
113 s->EOL();
114
115 if (depth > 0)
116 {
117 s->IndentMore();
118
Greg Clayton9da7bd02010-08-24 21:05:24 +0000119 if (m_variable_list_sp.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000120 {
Greg Clayton9da7bd02010-08-24 21:05:24 +0000121 m_variable_list_sp->Dump(s, show_context);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000122 }
123
Greg Clayton624784a2011-09-29 21:19:25 +0000124 collection::const_iterator pos, end = m_children.end();
125 for (pos = m_children.begin(); pos != end; ++pos)
126 (*pos)->Dump(s, base_addr, depth - 1, show_context);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000127
128 s->IndentLess();
129 }
130
131}
132
133
Greg Clayton0b76a2c2010-08-21 02:22:51 +0000134Block *
135Block::FindBlockByID (user_id_t block_id)
136{
137 if (block_id == GetID())
138 return this;
139
Ed Masted4612ad2014-04-20 13:17:36 +0000140 Block *matching_block = nullptr;
Greg Clayton624784a2011-09-29 21:19:25 +0000141 collection::const_iterator pos, end = m_children.end();
142 for (pos = m_children.begin(); pos != end; ++pos)
Greg Clayton0b76a2c2010-08-21 02:22:51 +0000143 {
Greg Clayton624784a2011-09-29 21:19:25 +0000144 matching_block = (*pos)->FindBlockByID (block_id);
Greg Clayton0b76a2c2010-08-21 02:22:51 +0000145 if (matching_block)
146 break;
147 }
148 return matching_block;
149}
150
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000151void
Greg Clayton9da7bd02010-08-24 21:05:24 +0000152Block::CalculateSymbolContext (SymbolContext* sc)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000153{
Greg Clayton0b76a2c2010-08-21 02:22:51 +0000154 if (m_parent_scope)
155 m_parent_scope->CalculateSymbolContext(sc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000156 sc->block = this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000157}
158
Greg Claytone72dfb32012-02-24 01:59:29 +0000159lldb::ModuleSP
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000160Block::CalculateSymbolContextModule ()
161{
162 if (m_parent_scope)
163 return m_parent_scope->CalculateSymbolContextModule ();
Greg Claytone72dfb32012-02-24 01:59:29 +0000164 return lldb::ModuleSP();
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000165}
166
167CompileUnit *
168Block::CalculateSymbolContextCompileUnit ()
169{
170 if (m_parent_scope)
171 return m_parent_scope->CalculateSymbolContextCompileUnit ();
Ed Masted4612ad2014-04-20 13:17:36 +0000172 return nullptr;
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000173}
174
175Function *
176Block::CalculateSymbolContextFunction ()
177{
178 if (m_parent_scope)
179 return m_parent_scope->CalculateSymbolContextFunction ();
Ed Masted4612ad2014-04-20 13:17:36 +0000180 return nullptr;
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000181}
182
183Block *
184Block::CalculateSymbolContextBlock ()
185{
186 return this;
187}
188
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000189void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000190Block::DumpSymbolContext(Stream *s)
191{
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000192 Function *function = CalculateSymbolContextFunction();
193 if (function)
194 function->DumpSymbolContext(s);
Daniel Malead01b2952012-11-29 21:49:15 +0000195 s->Printf(", Block{0x%8.8" PRIx64 "}", GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000196}
197
Caroline Ticedde9cff2010-09-20 05:20:02 +0000198void
199Block::DumpAddressRanges (Stream *s, lldb::addr_t base_addr)
200{
Greg Claytonea3e7d52011-10-08 00:49:15 +0000201 if (!m_ranges.IsEmpty())
Caroline Ticedde9cff2010-09-20 05:20:02 +0000202 {
Greg Claytonea3e7d52011-10-08 00:49:15 +0000203 size_t num_ranges = m_ranges.GetSize();
204 for (size_t i=0; i<num_ranges; ++i)
205 {
206 const Range &range = m_ranges.GetEntryRef(i);
207 s->AddressRange(base_addr + range.GetRangeBase(), base_addr + range.GetRangeEnd(), 4);
208 }
Caroline Ticedde9cff2010-09-20 05:20:02 +0000209 }
210}
211
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000212bool
213Block::Contains (addr_t range_offset) const
214{
Ed Masted4612ad2014-04-20 13:17:36 +0000215 return m_ranges.FindEntryThatContains(range_offset) != nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000216}
217
218bool
Greg Clayton6dadd502010-09-02 21:44:10 +0000219Block::Contains (const Block *block) const
220{
Greg Clayton6dadd502010-09-02 21:44:10 +0000221 if (this == block)
Greg Clayton6f00abd2010-09-14 03:16:58 +0000222 return false; // This block doesn't contain itself...
Greg Clayton6dadd502010-09-02 21:44:10 +0000223
Greg Clayton6f00abd2010-09-14 03:16:58 +0000224 // Walk the parent chain for "block" and see if any if them match this block
Greg Clayton6dadd502010-09-02 21:44:10 +0000225 const Block *block_parent;
226 for (block_parent = block->GetParent();
Ed Masted4612ad2014-04-20 13:17:36 +0000227 block_parent != nullptr;
Greg Clayton6dadd502010-09-02 21:44:10 +0000228 block_parent = block_parent->GetParent())
229 {
Greg Clayton6f00abd2010-09-14 03:16:58 +0000230 if (this == block_parent)
231 return true; // One of the parents of "block" is this object!
Greg Clayton6dadd502010-09-02 21:44:10 +0000232 }
233 return false;
234}
235
236bool
Greg Claytonea3e7d52011-10-08 00:49:15 +0000237Block::Contains (const Range& range) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000238{
Ed Masted4612ad2014-04-20 13:17:36 +0000239 return m_ranges.FindEntryThatContains (range) != nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000240}
241
Greg Clayton0b76a2c2010-08-21 02:22:51 +0000242Block *
243Block::GetParent () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000244{
Greg Clayton0b76a2c2010-08-21 02:22:51 +0000245 if (m_parent_scope)
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000246 return m_parent_scope->CalculateSymbolContextBlock();
Ed Masted4612ad2014-04-20 13:17:36 +0000247 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000248}
249
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000250Block *
Greg Clayton9da7bd02010-08-24 21:05:24 +0000251Block::GetContainingInlinedBlock ()
252{
Greg Clayton95897c62010-09-07 04:20:48 +0000253 if (GetInlinedFunctionInfo())
Greg Clayton9da7bd02010-08-24 21:05:24 +0000254 return this;
255 return GetInlinedParent ();
256}
257
258Block *
259Block::GetInlinedParent ()
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000260{
261 Block *parent_block = GetParent ();
262 if (parent_block)
263 {
Greg Clayton95897c62010-09-07 04:20:48 +0000264 if (parent_block->GetInlinedFunctionInfo())
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000265 return parent_block;
266 else
267 return parent_block->GetInlinedParent();
268 }
Ed Masted4612ad2014-04-20 13:17:36 +0000269 return nullptr;
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000270}
271
272
273bool
Greg Claytonea3e7d52011-10-08 00:49:15 +0000274Block::GetRangeContainingOffset (const addr_t offset, Range &range)
Greg Clayton9da7bd02010-08-24 21:05:24 +0000275{
Greg Claytonea3e7d52011-10-08 00:49:15 +0000276 const Range *range_ptr = m_ranges.FindEntryThatContains (offset);
277 if (range_ptr)
Greg Clayton9da7bd02010-08-24 21:05:24 +0000278 {
Greg Claytonea3e7d52011-10-08 00:49:15 +0000279 range = *range_ptr;
Greg Clayton9da7bd02010-08-24 21:05:24 +0000280 return true;
281 }
282 range.Clear();
283 return false;
284}
285
286
287bool
Greg Claytonea3e7d52011-10-08 00:49:15 +0000288Block::GetRangeContainingAddress (const Address& addr, AddressRange &range)
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000289{
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000290 Function *function = CalculateSymbolContextFunction();
291 if (function)
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000292 {
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000293 const AddressRange &func_range = function->GetAddressRange();
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000294 if (addr.GetSection() == func_range.GetBaseAddress().GetSection())
295 {
296 const addr_t addr_offset = addr.GetOffset();
297 const addr_t func_offset = func_range.GetBaseAddress().GetOffset();
298 if (addr_offset >= func_offset && addr_offset < func_offset + func_range.GetByteSize())
299 {
300 addr_t offset = addr_offset - func_offset;
301
Greg Claytonea3e7d52011-10-08 00:49:15 +0000302 const Range *range_ptr = m_ranges.FindEntryThatContains (offset);
303
304 if (range_ptr)
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000305 {
306 range.GetBaseAddress() = func_range.GetBaseAddress();
Greg Claytonea3e7d52011-10-08 00:49:15 +0000307 range.GetBaseAddress().SetOffset(func_offset + range_ptr->GetRangeBase());
308 range.SetByteSize(range_ptr->GetByteSize());
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000309 return true;
310 }
311 }
312 }
313 }
314 range.Clear();
315 return false;
316}
317
Jim Inghamfcb59bc2012-08-31 23:49:32 +0000318bool
319Block::GetRangeContainingLoadAddress (lldb::addr_t load_addr, Target &target, AddressRange &range)
320{
321 Address load_address;
322 load_address.SetLoadAddress(load_addr, &target);
323 AddressRange containing_range;
324 return GetRangeContainingAddress(load_address, containing_range);
325}
326
327
Greg Claytonea3e7d52011-10-08 00:49:15 +0000328uint32_t
329Block::GetRangeIndexContainingAddress (const Address& addr)
330{
331 Function *function = CalculateSymbolContextFunction();
332 if (function)
333 {
334 const AddressRange &func_range = function->GetAddressRange();
335 if (addr.GetSection() == func_range.GetBaseAddress().GetSection())
336 {
337 const addr_t addr_offset = addr.GetOffset();
338 const addr_t func_offset = func_range.GetBaseAddress().GetOffset();
339 if (addr_offset >= func_offset && addr_offset < func_offset + func_range.GetByteSize())
340 {
341 addr_t offset = addr_offset - func_offset;
342 return m_ranges.FindEntryIndexThatContains (offset);
343 }
344 }
345 }
346 return UINT32_MAX;
347}
348
Greg Clayton7e14f912011-04-23 02:04:55 +0000349bool
350Block::GetRangeAtIndex (uint32_t range_idx, AddressRange &range)
351{
Greg Claytonea3e7d52011-10-08 00:49:15 +0000352 if (range_idx < m_ranges.GetSize())
Greg Clayton7e14f912011-04-23 02:04:55 +0000353 {
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000354 Function *function = CalculateSymbolContextFunction();
355 if (function)
Greg Clayton7e14f912011-04-23 02:04:55 +0000356 {
Greg Claytonea3e7d52011-10-08 00:49:15 +0000357 const Range &vm_range = m_ranges.GetEntryRef(range_idx);
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000358 range.GetBaseAddress() = function->GetAddressRange().GetBaseAddress();
Greg Claytonea3e7d52011-10-08 00:49:15 +0000359 range.GetBaseAddress().Slide(vm_range.GetRangeBase ());
360 range.SetByteSize (vm_range.GetByteSize());
Greg Clayton7e14f912011-04-23 02:04:55 +0000361 return true;
362 }
363 }
364 return false;
365}
Greg Claytond7e05462010-11-14 00:22:48 +0000366
367bool
368Block::GetStartAddress (Address &addr)
369{
Greg Claytonea3e7d52011-10-08 00:49:15 +0000370 if (m_ranges.IsEmpty())
Greg Claytond7e05462010-11-14 00:22:48 +0000371 return false;
372
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000373 Function *function = CalculateSymbolContextFunction();
374 if (function)
Greg Claytond7e05462010-11-14 00:22:48 +0000375 {
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000376 addr = function->GetAddressRange().GetBaseAddress();
Greg Claytonea3e7d52011-10-08 00:49:15 +0000377 addr.Slide(m_ranges.GetEntryRef(0).GetRangeBase ());
Greg Claytond7e05462010-11-14 00:22:48 +0000378 return true;
379 }
380 return false;
381}
382
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000383void
Greg Claytonea3e7d52011-10-08 00:49:15 +0000384Block::FinalizeRanges ()
385{
386 m_ranges.Sort();
387 m_ranges.CombineConsecutiveRanges ();
388}
389
390void
391Block::AddRange (const Range& range)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000392{
Greg Clayton6c7f5612011-09-29 23:41:34 +0000393 Block *parent_block = GetParent ();
Greg Claytonea3e7d52011-10-08 00:49:15 +0000394 if (parent_block && !parent_block->Contains(range))
Greg Clayton6c7f5612011-09-29 23:41:34 +0000395 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000396 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
Greg Clayton6c7f5612011-09-29 23:41:34 +0000397 if (log)
398 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000399 ModuleSP module_sp (m_parent_scope->CalculateSymbolContextModule());
Greg Clayton6c7f5612011-09-29 23:41:34 +0000400 Function *function = m_parent_scope->CalculateSymbolContextFunction();
401 const addr_t function_file_addr = function->GetAddressRange().GetBaseAddress().GetFileAddress();
Greg Claytonea3e7d52011-10-08 00:49:15 +0000402 const addr_t block_start_addr = function_file_addr + range.GetRangeBase ();
403 const addr_t block_end_addr = function_file_addr + range.GetRangeEnd ();
Greg Clayton6c7f5612011-09-29 23:41:34 +0000404 Type *func_type = function->GetType();
405
406 const Declaration &func_decl = func_type->GetDeclaration();
407 if (func_decl.GetLine())
408 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000409 log->Printf ("warning: %s:%u block {0x%8.8" PRIx64 "} has range[%u] [0x%" PRIx64 " - 0x%" PRIx64 ") which is not contained in parent block {0x%8.8" PRIx64 "} in function {0x%8.8" PRIx64 "} from %s",
410 func_decl.GetFile().GetPath().c_str(),
Greg Clayton6c7f5612011-09-29 23:41:34 +0000411 func_decl.GetLine(),
412 GetID(),
Greg Claytonea3e7d52011-10-08 00:49:15 +0000413 (uint32_t)m_ranges.GetSize(),
Greg Clayton6c7f5612011-09-29 23:41:34 +0000414 block_start_addr,
415 block_end_addr,
416 parent_block->GetID(),
417 function->GetID(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000418 module_sp->GetFileSpec().GetPath().c_str());
Greg Clayton6c7f5612011-09-29 23:41:34 +0000419 }
420 else
421 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000422 log->Printf ("warning: block {0x%8.8" PRIx64 "} has range[%u] [0x%" PRIx64 " - 0x%" PRIx64 ") which is not contained in parent block {0x%8.8" PRIx64 "} in function {0x%8.8" PRIx64 "} from %s",
Greg Clayton6c7f5612011-09-29 23:41:34 +0000423 GetID(),
Greg Claytonea3e7d52011-10-08 00:49:15 +0000424 (uint32_t)m_ranges.GetSize(),
Greg Clayton6c7f5612011-09-29 23:41:34 +0000425 block_start_addr,
426 block_end_addr,
427 parent_block->GetID(),
428 function->GetID(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000429 module_sp->GetFileSpec().GetPath().c_str());
Greg Clayton6c7f5612011-09-29 23:41:34 +0000430 }
431 }
Greg Claytonea3e7d52011-10-08 00:49:15 +0000432 parent_block->AddRange (range);
Greg Clayton6c7f5612011-09-29 23:41:34 +0000433 }
Greg Claytonea3e7d52011-10-08 00:49:15 +0000434 m_ranges.Append(range);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000435}
436
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000437// Return the current number of bytes that this object occupies in memory
438size_t
439Block::MemorySize() const
440{
Greg Claytonea3e7d52011-10-08 00:49:15 +0000441 size_t mem_size = sizeof(Block) + m_ranges.GetSize() * sizeof(Range);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000442 if (m_inlineInfoSP.get())
443 mem_size += m_inlineInfoSP->MemorySize();
Greg Clayton9da7bd02010-08-24 21:05:24 +0000444 if (m_variable_list_sp.get())
445 mem_size += m_variable_list_sp->MemorySize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000446 return mem_size;
447
448}
449
Greg Clayton0b76a2c2010-08-21 02:22:51 +0000450void
451Block::AddChild(const BlockSP &child_block_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000452{
Greg Clayton0b76a2c2010-08-21 02:22:51 +0000453 if (child_block_sp)
454 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +0000455 child_block_sp->SetParentScope (this);
456 m_children.push_back (child_block_sp);
Greg Clayton0b76a2c2010-08-21 02:22:51 +0000457 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000458}
459
460void
461Block::SetInlinedFunctionInfo(const char *name, const char *mangled, const Declaration *decl_ptr, const Declaration *call_decl_ptr)
462{
463 m_inlineInfoSP.reset(new InlineFunctionInfo(name, mangled, decl_ptr, call_decl_ptr));
464}
465
Jim Inghame1be14d2010-08-18 19:29:16 +0000466
467
468VariableListSP
Greg Claytonc662ec82011-06-17 22:10:16 +0000469Block::GetBlockVariableList (bool can_create)
Jim Inghame1be14d2010-08-18 19:29:16 +0000470{
Greg Clayton0b76a2c2010-08-21 02:22:51 +0000471 if (m_parsed_block_variables == false)
Jim Inghame1be14d2010-08-18 19:29:16 +0000472 {
Ed Masted4612ad2014-04-20 13:17:36 +0000473 if (m_variable_list_sp.get() == nullptr && can_create)
Greg Clayton0b76a2c2010-08-21 02:22:51 +0000474 {
475 m_parsed_block_variables = true;
476 SymbolContext sc;
477 CalculateSymbolContext(&sc);
478 assert(sc.module_sp);
479 sc.module_sp->GetSymbolVendor()->ParseVariablesForContext(sc);
480 }
Jim Inghame1be14d2010-08-18 19:29:16 +0000481 }
Greg Claytonc662ec82011-06-17 22:10:16 +0000482 return m_variable_list_sp;
483}
Jim Inghame1be14d2010-08-18 19:29:16 +0000484
Greg Claytonc662ec82011-06-17 22:10:16 +0000485uint32_t
486Block::AppendBlockVariables (bool can_create,
487 bool get_child_block_variables,
488 bool stop_if_child_block_is_inlined_function,
Tamas Berghammer72ac8a82016-02-25 12:23:37 +0000489 const std::function<bool(Variable*)>& filter,
Greg Claytonc662ec82011-06-17 22:10:16 +0000490 VariableList *variable_list)
491{
492 uint32_t num_variables_added = 0;
493 VariableList *block_var_list = GetBlockVariableList (can_create).get();
494 if (block_var_list)
Jim Inghame1be14d2010-08-18 19:29:16 +0000495 {
Tamas Berghammer72ac8a82016-02-25 12:23:37 +0000496 for (size_t i = 0; i < block_var_list->GetSize(); ++i)
497 {
498 VariableSP variable = block_var_list->GetVariableAtIndex(i);
499 if (filter(variable.get()))
500 {
501 num_variables_added++;
502 variable_list->AddVariable(variable);
503 }
504 }
Greg Claytonc662ec82011-06-17 22:10:16 +0000505 }
Tamas Berghammer72ac8a82016-02-25 12:23:37 +0000506
Greg Claytonc662ec82011-06-17 22:10:16 +0000507 if (get_child_block_variables)
508 {
Greg Clayton624784a2011-09-29 21:19:25 +0000509 collection::const_iterator pos, end = m_children.end();
510 for (pos = m_children.begin(); pos != end; ++pos)
511 {
512 Block *child_block = pos->get();
Greg Claytonc662ec82011-06-17 22:10:16 +0000513 if (stop_if_child_block_is_inlined_function == false ||
Ed Masted4612ad2014-04-20 13:17:36 +0000514 child_block->GetInlinedFunctionInfo() == nullptr)
Greg Claytonc662ec82011-06-17 22:10:16 +0000515 {
516 num_variables_added += child_block->AppendBlockVariables (can_create,
517 get_child_block_variables,
518 stop_if_child_block_is_inlined_function,
Tamas Berghammer72ac8a82016-02-25 12:23:37 +0000519 filter,
Greg Claytonc662ec82011-06-17 22:10:16 +0000520 variable_list);
Jim Inghame1be14d2010-08-18 19:29:16 +0000521 }
522 }
523 }
Greg Claytonc662ec82011-06-17 22:10:16 +0000524 return num_variables_added;
Jim Inghame1be14d2010-08-18 19:29:16 +0000525}
526
527uint32_t
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000528Block::AppendVariables
529(
530 bool can_create,
531 bool get_parent_variables,
532 bool stop_if_block_is_inlined_function,
Tamas Berghammer72ac8a82016-02-25 12:23:37 +0000533 const std::function<bool(Variable*)>& filter,
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000534 VariableList *variable_list
535)
Jim Inghame1be14d2010-08-18 19:29:16 +0000536{
537 uint32_t num_variables_added = 0;
Greg Claytonc662ec82011-06-17 22:10:16 +0000538 VariableListSP variable_list_sp(GetBlockVariableList(can_create));
Jim Inghame1be14d2010-08-18 19:29:16 +0000539
Ed Masted4612ad2014-04-20 13:17:36 +0000540 bool is_inlined_function = GetInlinedFunctionInfo() != nullptr;
Tamas Berghammer72ac8a82016-02-25 12:23:37 +0000541 if (variable_list_sp)
Jim Inghame1be14d2010-08-18 19:29:16 +0000542 {
Tamas Berghammer72ac8a82016-02-25 12:23:37 +0000543 for (size_t i = 0; i < variable_list_sp->GetSize(); ++i)
544 {
545 VariableSP variable = variable_list_sp->GetVariableAtIndex(i);
546 if (filter(variable.get()))
547 {
548 num_variables_added++;
549 variable_list->AddVariable(variable);
550 }
551 }
Jim Inghame1be14d2010-08-18 19:29:16 +0000552 }
Tamas Berghammer72ac8a82016-02-25 12:23:37 +0000553
Jim Inghame1be14d2010-08-18 19:29:16 +0000554 if (get_parent_variables)
555 {
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000556 if (stop_if_block_is_inlined_function && is_inlined_function)
557 return num_variables_added;
558
Jim Inghame1be14d2010-08-18 19:29:16 +0000559 Block* parent_block = GetParent();
560 if (parent_block)
Tamas Berghammer72ac8a82016-02-25 12:23:37 +0000561 num_variables_added += parent_block->AppendVariables(can_create,
562 get_parent_variables,
563 stop_if_block_is_inlined_function,
564 filter,
565 variable_list);
Jim Inghame1be14d2010-08-18 19:29:16 +0000566 }
567 return num_variables_added;
568}
569
Greg Clayton99558cc42015-08-24 23:46:31 +0000570CompilerDeclContext
571Block::GetDeclContext()
Sean Callanan72e49402011-08-05 23:43:37 +0000572{
Greg Clayton99558cc42015-08-24 23:46:31 +0000573 ModuleSP module_sp = CalculateSymbolContextModule ();
574
575 if (module_sp)
576 {
577 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
Sean Callanan72e49402011-08-05 23:43:37 +0000578
Greg Clayton99558cc42015-08-24 23:46:31 +0000579 if (sym_vendor)
580 {
581 SymbolFile *sym_file = sym_vendor->GetSymbolFile();
Sean Callanan72e49402011-08-05 23:43:37 +0000582
Greg Clayton99558cc42015-08-24 23:46:31 +0000583 if (sym_file)
584 return sym_file->GetDeclContextForUID (GetID());
585 }
586 }
587 return CompilerDeclContext();
Sean Callanan72e49402011-08-05 23:43:37 +0000588}
589
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000590void
Greg Clayton0b76a2c2010-08-21 02:22:51 +0000591Block::SetBlockInfoHasBeenParsed (bool b, bool set_children)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000592{
Greg Clayton0b76a2c2010-08-21 02:22:51 +0000593 m_parsed_block_info = b;
594 if (set_children)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000595 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +0000596 m_parsed_child_blocks = true;
Greg Clayton624784a2011-09-29 21:19:25 +0000597 collection::const_iterator pos, end = m_children.end();
598 for (pos = m_children.begin(); pos != end; ++pos)
599 (*pos)->SetBlockInfoHasBeenParsed (b, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000600 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000601}
Greg Claytonc662ec82011-06-17 22:10:16 +0000602
603void
604Block::SetDidParseVariables (bool b, bool set_children)
605{
606 m_parsed_block_variables = b;
607 if (set_children)
608 {
Greg Clayton624784a2011-09-29 21:19:25 +0000609 collection::const_iterator pos, end = m_children.end();
610 for (pos = m_children.begin(); pos != end; ++pos)
611 (*pos)->SetDidParseVariables (b, true);
Greg Claytonc662ec82011-06-17 22:10:16 +0000612 }
613}
614
Greg Clayton624784a2011-09-29 21:19:25 +0000615
616Block *
617Block::GetSibling() const
618{
619 if (m_parent_scope)
620 {
Greg Clayton6c7f5612011-09-29 23:41:34 +0000621 Block *parent_block = GetParent();
Greg Clayton624784a2011-09-29 21:19:25 +0000622 if (parent_block)
623 return parent_block->GetSiblingForChild (this);
624 }
Ed Masted4612ad2014-04-20 13:17:36 +0000625 return nullptr;
Greg Clayton624784a2011-09-29 21:19:25 +0000626}
627// A parent of child blocks can be asked to find a sibling block given
628// one of its child blocks
629Block *
630Block::GetSiblingForChild (const Block *child_block) const
631{
632 if (!m_children.empty())
633 {
634 collection::const_iterator pos, end = m_children.end();
635 for (pos = m_children.begin(); pos != end; ++pos)
636 {
637 if (pos->get() == child_block)
638 {
639 if (++pos != end)
640 return pos->get();
641 break;
642 }
643 }
644 }
Ed Masted4612ad2014-04-20 13:17:36 +0000645 return nullptr;
Greg Clayton624784a2011-09-29 21:19:25 +0000646}
647