Block.h revision 276479
1139749Simp//===-- Block.h -------------------------------------------------*- C++ -*-===//
275353Smjacob//
375353Smjacob//                     The LLVM Compiler Infrastructure
475353Smjacob//
575353Smjacob// This file is distributed under the University of Illinois Open Source
675353Smjacob// License. See LICENSE.TXT for details.
775353Smjacob//
875353Smjacob//===----------------------------------------------------------------------===//
975353Smjacob
1075353Smjacob#ifndef liblldb_Block_h_
1175353Smjacob#define liblldb_Block_h_
1275353Smjacob
1375353Smjacob#include "lldb/lldb-private.h"
1475353Smjacob#include "lldb/Core/AddressRange.h"
1575353Smjacob#include "lldb/Core/RangeMap.h"
1675353Smjacob#include "lldb/Core/Stream.h"
1775353Smjacob#include "lldb/Core/UserID.h"
1875353Smjacob#include "lldb/Symbol/LineEntry.h"
1975353Smjacob#include "lldb/Symbol/SymbolContext.h"
2075353Smjacob#include "lldb/Symbol/ClangASTType.h"
2175353Smjacob
2275353Smjacobnamespace lldb_private {
2375353Smjacob
2475353Smjacob//----------------------------------------------------------------------
2575353Smjacob/// @class Block Block.h "lldb/Symbol/Block.h"
2675353Smjacob/// @brief A class that describes a single lexical block.
2775353Smjacob///
28220938Smarius/// A Function object owns a BlockList object which owns one or more
2975353Smjacob/// Block objects. The BlockList object contains a section offset
3075353Smjacob/// address range, and Block objects contain one or more ranges
3175353Smjacob/// which are offsets into that range. Blocks are can have discontiguous
32129843Smarius/// ranges within the BlockList address range, and each block can
33129843Smarius/// contain child blocks each with their own sets of ranges.
34129843Smarius///
3575353Smjacob/// Each block has a variable list that represents local, argument, and
3675353Smjacob/// static variables that are scoped to the block.
3775353Smjacob///
3875353Smjacob/// Inlined functions are represented by attaching a
39120281Swilko/// InlineFunctionInfo shared pointer object to a block. Inlined
40120281Swilko/// functions are represented as named blocks.
41120281Swilko//----------------------------------------------------------------------
42120281Swilkoclass Block :
43120281Swilko    public UserID,
44120281Swilko    public SymbolContextScope
45120281Swilko{
4675353Smjacobpublic:
4775353Smjacob    typedef RangeArray<uint32_t, uint32_t, 1> RangeList;
4875353Smjacob    typedef RangeList::Entry Range;
49129876Sphk
5075353Smjacob    //------------------------------------------------------------------
5175353Smjacob    /// Construct with a User ID \a uid, \a depth.
5275353Smjacob    ///
5375353Smjacob    /// Initialize this block with the specified UID \a uid. The
5475353Smjacob    /// \a depth in the \a block_list is used to represent the parent,
5575353Smjacob    /// sibling, and child block information and also allows for partial
5675353Smjacob    /// parsing at the block level.
5775353Smjacob    ///
5875353Smjacob    /// @param[in] uid
59109514Sobrien    ///     The UID for a given block. This value is given by the
6075353Smjacob    ///     SymbolFile plug-in and can be any value that helps the
6175353Smjacob    ///     SymbolFile plug-in to match this block back to the debug
6275353Smjacob    ///     information data that it parses for further or more in
6375353Smjacob    ///     depth parsing. Common values would be the index into a
6475353Smjacob    ///     table, or an offset into the debug information.
65165095Syongari    ///
66165095Syongari    /// @param[in] depth
6775353Smjacob    ///     The integer depth of this block in the block list hierarchy.
6875353Smjacob    ///
6975353Smjacob    /// @param[in] block_list
7075353Smjacob    ///     The block list that this object belongs to.
7175353Smjacob    ///
7295722Sphk    /// @see BlockList
7375353Smjacob    //------------------------------------------------------------------
74227908Smarius    Block (lldb::user_id_t uid);
7575353Smjacob
7675353Smjacob    //------------------------------------------------------------------
7775353Smjacob    /// Destructor.
7875353Smjacob    //------------------------------------------------------------------
79165099Syongari    virtual ~Block ();
80165099Syongari
81221407Smarius    //------------------------------------------------------------------
8275353Smjacob    /// Add a child to this object.
83165099Syongari    ///
8475353Smjacob    /// @param[in] child_block_sp
8575353Smjacob    ///     A shared pointer to a child block that will get added to
8684145Sjlemon    ///     this block.
8784145Sjlemon    //------------------------------------------------------------------
8884145Sjlemon    void
89221407Smarius    AddChild (const lldb::BlockSP &child_block_sp);
9075353Smjacob
91165099Syongari    //------------------------------------------------------------------
92165099Syongari    /// Add a new offset range to this block.
93165099Syongari    ///
94165099Syongari    /// @param[in] start_offset
95165099Syongari    ///     An offset into this Function's address range that
96165099Syongari    ///     describes the start address of a range for this block.
97165099Syongari    ///
98165099Syongari    /// @param[in] end_offset
99165099Syongari    ///     An offset into this Function's address range that
100221407Smarius    ///     describes the end address of a range for this block.
101165099Syongari    //------------------------------------------------------------------
102221407Smarius    void
103221407Smarius    AddRange (const Range& range);
104221407Smarius
105221407Smarius    void
106165099Syongari    FinalizeRanges ();
107221407Smarius
108221407Smarius    //------------------------------------------------------------------
109238874Shrs    /// @copydoc SymbolContextScope::CalculateSymbolContext(SymbolContext*)
110221407Smarius    ///
111242272Sjmallett    /// @see SymbolContextScope
112223688Simp    //------------------------------------------------------------------
113221407Smarius    virtual void
114221407Smarius    CalculateSymbolContext(SymbolContext* sc);
115165099Syongari
116165099Syongari    virtual lldb::ModuleSP
11775353Smjacob    CalculateSymbolContextModule ();
118221407Smarius
119221407Smarius    virtual CompileUnit *
120221407Smarius    CalculateSymbolContextCompileUnit ();
121221407Smarius
122221407Smarius    virtual Function *
123221407Smarius    CalculateSymbolContextFunction ();
12475353Smjacob
12575353Smjacob    virtual Block *
12675353Smjacob    CalculateSymbolContextBlock ();
12775353Smjacob
128165099Syongari    //------------------------------------------------------------------
12975353Smjacob    /// Check if an offset is in one of the block offset ranges.
13075353Smjacob    ///
13175353Smjacob    /// @param[in] range_offset
13275353Smjacob    ///     An offset into the Function's address range.
13375353Smjacob    ///
13475353Smjacob    /// @return
135197590Syongari    ///     Returns \b true if \a range_offset falls in one of this
13675353Smjacob    ///     block's ranges, \b false otherwise.
137221407Smarius    //------------------------------------------------------------------
13875353Smjacob    bool
139221407Smarius    Contains (lldb::addr_t range_offset) const;
14075353Smjacob
141197590Syongari    //------------------------------------------------------------------
142213893Smarius    /// Check if a offset range is in one of the block offset ranges.
143213893Smarius    ///
144213893Smarius    /// @param[in] range
145197590Syongari    ///     An offset range into the Function's address range.
146221407Smarius    ///
147221407Smarius    /// @return
148221407Smarius    ///     Returns \b true if \a range falls in one of this
149165099Syongari    ///     block's ranges, \b false otherwise.
150165099Syongari    //------------------------------------------------------------------
151165099Syongari    bool
152221407Smarius    Contains (const Range& range) const;
153223688Simp
154183966Syongari    //------------------------------------------------------------------
155183966Syongari    /// Check if this object contains "block" as a child block at any
156183966Syongari    /// depth.
157183966Syongari    ///
158183966Syongari    /// @param[in] block
159183966Syongari    ///     A potential child block.
160183966Syongari    ///
161183966Syongari    /// @return
162183966Syongari    ///     Returns \b true if \a block is a child of this block, \b
163183966Syongari    ///     false otherwise.
164183966Syongari    //------------------------------------------------------------------
165183966Syongari    bool
166183966Syongari    Contains (const Block *block) const;
167165099Syongari
168165099Syongari    //------------------------------------------------------------------
169221407Smarius    /// Dump the block contents.
17075353Smjacob    ///
171221407Smarius    /// @param[in] s
172273305Syongari    ///     The stream to which to dump the object description.
173192708Syongari    ///
174273305Syongari    /// @param[in] base_addr
175273305Syongari    ///     The resolved start address of the Function's address
176273305Syongari    ///     range. This should be resolved as the file or load address
177273305Syongari    ///     prior to passing the value into this function for dumping.
17884144Sjlemon    ///
179192708Syongari    /// @param[in] depth
180192708Syongari    ///     Limit the number of levels deep that this function should
18184144Sjlemon    ///     print as this block can contain child blocks. Specify
18275353Smjacob    ///     INT_MAX to dump all child blocks.
183165095Syongari    ///
18475353Smjacob    /// @param[in] show_context
18575353Smjacob    ///     If \b true, variables will dump their context information.
18675353Smjacob    //------------------------------------------------------------------
18775353Smjacob    void
18875353Smjacob    Dump (Stream *s, lldb::addr_t base_addr, int32_t depth, bool show_context) const;
189183493Syongari
19075353Smjacob    //------------------------------------------------------------------
19175353Smjacob    /// @copydoc SymbolContextScope::DumpSymbolContext(Stream*)
192165099Syongari    ///
193165099Syongari    /// @see SymbolContextScope
194165099Syongari    //------------------------------------------------------------------
195221407Smarius    virtual void
196165099Syongari    DumpSymbolContext(Stream *s);
197183493Syongari
198165099Syongari    void
199165099Syongari    DumpAddressRanges (Stream *s,
200165099Syongari                       lldb::addr_t base_addr);
201165099Syongari
202165099Syongari    void
203214566Smarius    GetDescription (Stream *s,
204197590Syongari                    Function *function,
205197590Syongari                    lldb::DescriptionLevel level,
206197590Syongari                    Target *target) const;
207197590Syongari
208197590Syongari    //------------------------------------------------------------------
209197590Syongari    /// Get the parent block.
210183493Syongari    ///
211165099Syongari    /// @return
212165099Syongari    ///     The parent block pointer, or NULL if this block has no
213221407Smarius    ///     parent.
214221407Smarius    //------------------------------------------------------------------
215221407Smarius    Block *
216221407Smarius    GetParent () const;
217238874Shrs
218221407Smarius
219221407Smarius    //------------------------------------------------------------------
220223688Simp    /// Get the inlined block that contains this block.
221221407Smarius    ///
222165099Syongari    /// @return
223165099Syongari    ///     If this block contains inlined function info, it will return
224165099Syongari    ///     this block, else parent blocks will be searched to see if
225238874Shrs    ///     any contain this block. NULL will be returned if this block
226238874Shrs    ///     nor any parent blocks are inlined function blocks.
227173133Syongari    //------------------------------------------------------------------
228192713Syongari    Block *
229165099Syongari    GetContainingInlinedBlock ();
230221407Smarius
231165099Syongari    //------------------------------------------------------------------
232192713Syongari    /// Get the inlined parent block for this block.
233165099Syongari    ///
234221407Smarius    /// @return
235192713Syongari    ///     The parent block pointer, or NULL if this block has no
236192713Syongari    ///     parent.
237192713Syongari    //------------------------------------------------------------------
238192713Syongari    Block *
239192713Syongari    GetInlinedParent ();
240192713Syongari
241192713Syongari    //------------------------------------------------------------------
242165099Syongari    /// Get the sibling block for this block.
243165099Syongari    ///
244192713Syongari    /// @return
245165099Syongari    ///     The sibling block pointer, or NULL if this block has no
246165099Syongari    ///     sibling.
247221407Smarius    //------------------------------------------------------------------
248192713Syongari    Block *
249192713Syongari    GetSibling () const;
250192713Syongari
251165099Syongari    //------------------------------------------------------------------
252173133Syongari    /// Get the first child block.
253221407Smarius    ///
254238874Shrs    /// @return
255223688Simp    ///     The first child block pointer, or NULL if this block has no
256223688Simp    ///     children.
257173133Syongari    //------------------------------------------------------------------
258173133Syongari    Block *
259173133Syongari    GetFirstChild () const
260173133Syongari    {
261196366Syongari        if (m_children.empty())
262173133Syongari            return NULL;
263165099Syongari        return m_children.front().get();
26475353Smjacob    }
265221407Smarius
266221407Smarius    //------------------------------------------------------------------
267221407Smarius    /// Get the variable list for this block only.
268221407Smarius    ///
269193291Syongari    /// @param[in] can_create
270221407Smarius    ///     If \b true, the variables can be parsed if they already
271238874Shrs    ///     haven't been, else the current state of the block will be
272193291Syongari    ///     returned.
273193291Syongari    ///
274193291Syongari    /// @return
275193291Syongari    ///     A variable list shared pointer that contains all variables
276193291Syongari    ///     for this block.
277193291Syongari    //------------------------------------------------------------------
278193291Syongari    lldb::VariableListSP
279193291Syongari    GetBlockVariableList (bool can_create);
280193291Syongari
281193291Syongari
282193291Syongari    //------------------------------------------------------------------
283193291Syongari    /// Get the variable list for this block and optionally all child
284165099Syongari    /// blocks if \a get_child_variables is \b true.
285221407Smarius    ///
286192713Syongari    /// @param[in] get_child_variables
287192713Syongari    ///     If \b true, all variables from all child blocks will be
288192713Syongari    ///     added to the variable list.
289192713Syongari    ///
290192713Syongari    /// @param[in] can_create
291192713Syongari    ///     If \b true, the variables can be parsed if they already
292165099Syongari    ///     haven't been, else the current state of the block will be
293165099Syongari    ///     returned. Passing \b true for this parameter can be used
294165099Syongari    ///     to see the current state of what has been parsed up to this
295165099Syongari    ///     point.
296165099Syongari    ///
297165099Syongari    /// @param[in] add_inline_child_block_variables
298165099Syongari    ///     If this is \b false, no child variables of child blocks
299165099Syongari    ///     that are inlined functions will be gotten. If \b true then
300165099Syongari    ///     all child variables will be added regardless of whether they
30175353Smjacob    ///     come from inlined functions or not.
30275353Smjacob    ///
30375353Smjacob    /// @return
30475353Smjacob    ///     A variable list shared pointer that contains all variables
30575353Smjacob    ///     for this block.
30684145Sjlemon    //------------------------------------------------------------------
30775353Smjacob    uint32_t
30875353Smjacob    AppendBlockVariables (bool can_create,
30975353Smjacob                          bool get_child_block_variables,
310165099Syongari                          bool stop_if_child_block_is_inlined_function,
31175353Smjacob                          VariableList *variable_list);
31275353Smjacob
31375353Smjacob    //------------------------------------------------------------------
31475353Smjacob    /// Appends the variables from this block, and optionally from all
31575353Smjacob    /// parent blocks, to \a variable_list.
31675353Smjacob    ///
31775353Smjacob    /// @param[in] can_create
31875353Smjacob    ///     If \b true, the variables can be parsed if they already
31975353Smjacob    ///     haven't been, else the current state of the block will be
32075353Smjacob    ///     returned. Passing \b true for this parameter can be used
321165095Syongari    ///     to see the current state of what has been parsed up to this
32275353Smjacob    ///     point.
32375353Smjacob    ///
324165099Syongari    /// @param[in] get_parent_variables
325221407Smarius    ///     If \b true, all variables from all parent blocks will be
32675353Smjacob    ///     added to the variable list.
327165099Syongari    ///
32875353Smjacob    /// @param[in] stop_if_block_is_inlined_function
329165099Syongari    ///     If \b true, all variables from all parent blocks will be
330165099Syongari    ///     added to the variable list until there are no parent blocks
33195673Sphk    ///     or the parent block has inlined function info.
332273305Syongari    ///
333165099Syongari    /// @param[in/out] variable_list
334165099Syongari    ///     All variables in this block, and optionally all parent
33575353Smjacob    ///     blocks will be added to this list.
336120281Swilko    ///
337192708Syongari    /// @return
338192708Syongari    ///     The number of variable that were appended to \a
339165099Syongari    ///     variable_list.
340165099Syongari    //------------------------------------------------------------------
341120281Swilko    uint32_t
34275353Smjacob    AppendVariables (bool can_create,
343165099Syongari                     bool get_parent_variables,
34475353Smjacob                     bool stop_if_block_is_inlined_function,
34575353Smjacob                     VariableList *variable_list);
346165099Syongari
34775353Smjacob    //------------------------------------------------------------------
348165099Syongari    /// Get const accessor for any inlined function information.
349165099Syongari    ///
350165099Syongari    /// @return
351165099Syongari    ///     A const pointer to any inlined function information, or NULL
352165099Syongari    ///     if this is a regular block.
35375353Smjacob    //------------------------------------------------------------------
35475353Smjacob    const InlineFunctionInfo*
35575353Smjacob    GetInlinedFunctionInfo () const
35675353Smjacob    {
357217412Smarius        return m_inlineInfoSP.get();
358165099Syongari    }
359165099Syongari
360165099Syongari    clang::DeclContext *
361165099Syongari    GetClangDeclContext();
362165099Syongari
363165099Syongari    //------------------------------------------------------------------
364165099Syongari    /// Get the memory cost of this object.
365165099Syongari    ///
366165099Syongari    /// Returns the cost of this object plus any owned objects from the
367215297Smarius    /// ranges, variables, and inline function information.
368215297Smarius    ///
369273305Syongari    /// @return
370215297Smarius    ///     The number of bytes that this object occupies in memory.
371273305Syongari    //------------------------------------------------------------------
372217412Smarius    size_t
373217412Smarius    MemorySize() const;
374165099Syongari
375165099Syongari    //------------------------------------------------------------------
376165099Syongari    /// Set accessor for any inlined function information.
37775353Smjacob    ///
37875353Smjacob    /// @param[in] name
37975353Smjacob    ///     The method name for the inlined function. This value should
38084145Sjlemon    ///     not be NULL.
38175353Smjacob    ///
38284145Sjlemon    /// @param[in] mangled
38375353Smjacob    ///     The mangled method name for the inlined function. This can
38475353Smjacob    ///     be NULL if there is no mangled name for an inlined function
38575353Smjacob    ///     or if the name is the same as \a name.
38684145Sjlemon    ///
38775353Smjacob    /// @param[in] decl_ptr
388173667Syongari    ///     A optional pointer to declaration information for the
389173667Syongari    ///     inlined function information. This value can be NULL to
39084145Sjlemon    ///     indicate that no declaration information is available.
391173667Syongari    ///
39275353Smjacob    /// @param[in] call_decl_ptr
39375353Smjacob    ///     Optional calling location declaration information that
39484145Sjlemon    ///     describes from where this inlined function was called.
39584145Sjlemon    //------------------------------------------------------------------
39675353Smjacob    void
39784145Sjlemon    SetInlinedFunctionInfo (const char *name,
398165099Syongari                            const char *mangled,
399165099Syongari                            const Declaration *decl_ptr,
40084145Sjlemon                            const Declaration *call_decl_ptr);
401165099Syongari
40275353Smjacob
403165099Syongari    void
404173667Syongari    SetParentScope (SymbolContextScope *parent_scope)
405173667Syongari    {
406165099Syongari        m_parent_scope = parent_scope;
407192709Syongari    }
40875353Smjacob
40984145Sjlemon    //------------------------------------------------------------------
410221407Smarius    /// Set accessor for the variable list.
411221407Smarius    ///
412165099Syongari    /// Called by the SymbolFile plug-ins after they have parsed the
41375353Smjacob    /// variable lists and are ready to hand ownership of the list over
41475353Smjacob    /// to this object.
41575353Smjacob    ///
416221407Smarius    /// @param[in] variable_list_sp
41775353Smjacob    ///     A shared pointer to a VariableList.
41875353Smjacob    //------------------------------------------------------------------
41984145Sjlemon    void
42075353Smjacob    SetVariableList (lldb::VariableListSP& variable_list_sp)
42175353Smjacob    {
42275353Smjacob        m_variable_list_sp = variable_list_sp;
42384145Sjlemon    }
42475353Smjacob
42575353Smjacob
42675353Smjacob
427215297Smarius    bool
42875353Smjacob    BlockInfoHasBeenParsed() const
42975353Smjacob    {
43075353Smjacob        return m_parsed_block_info;
43175353Smjacob    }
43275353Smjacob
43375353Smjacob    void
43475353Smjacob    SetBlockInfoHasBeenParsed (bool b, bool set_children);
43575353Smjacob
43675353Smjacob    Block *
43775353Smjacob    FindBlockByID (lldb::user_id_t block_id);
43875353Smjacob
43975353Smjacob    size_t
44075353Smjacob    GetNumRanges () const
44175353Smjacob    {
442192710Syongari        return m_ranges.GetSize();
443192710Syongari    }
44475353Smjacob
44575353Smjacob    bool
44675353Smjacob    GetRangeContainingOffset (const lldb::addr_t offset, Range &range);
44775353Smjacob
44875353Smjacob    bool
449120281Swilko    GetRangeContainingAddress (const Address& addr, AddressRange &range);
450192710Syongari
451192710Syongari    bool
452120281Swilko    GetRangeContainingLoadAddress (lldb::addr_t load_addr, Target &target, AddressRange &range);
453192710Syongari
454192710Syongari    uint32_t
455120281Swilko    GetRangeIndexContainingAddress (const Address& addr);
456192710Syongari
457192710Syongari    //------------------------------------------------------------------
458120281Swilko    // Since blocks might have multiple discontiguous address ranges,
459192710Syongari    // we need to be able to get at any of the address ranges in a block.
460192710Syongari    //------------------------------------------------------------------
461192710Syongari    bool
462192710Syongari    GetRangeAtIndex (uint32_t range_idx,
463192710Syongari                     AddressRange &range);
464120281Swilko
465197588Syongari    bool
466197588Syongari    GetStartAddress (Address &addr);
467197588Syongari
468197588Syongari    void
469197588Syongari    SetDidParseVariables (bool b, bool set_children);
470120281Swilko
47175353Smjacobprotected:
472215297Smarius    typedef std::vector<lldb::BlockSP> collection;
47375353Smjacob    //------------------------------------------------------------------
474215297Smarius    // Member variables.
475215297Smarius    //------------------------------------------------------------------
476215297Smarius    SymbolContextScope *m_parent_scope;
47775353Smjacob    collection m_children;
47875353Smjacob    RangeList m_ranges;
479215297Smarius    lldb::InlineFunctionInfoSP m_inlineInfoSP; ///< Inlined function information.
480215297Smarius    lldb::VariableListSP m_variable_list_sp; ///< The variable list for all local, static and parameter variables scoped to this block.
481215297Smarius    bool m_parsed_block_info:1,         ///< Set to true if this block and it's children have all been parsed
482215297Smarius         m_parsed_block_variables:1,
48375353Smjacob         m_parsed_child_blocks:1;
48475353Smjacob
48575353Smjacob    // A parent of child blocks can be asked to find a sibling block given
48675353Smjacob    // one of its child blocks
487221407Smarius    Block *
48875353Smjacob    GetSiblingForChild (const Block *child_block) const;
489192711Syongari
49075353Smjacobprivate:
491192711Syongari    DISALLOW_COPY_AND_ASSIGN (Block);
492192711Syongari};
493215923Smarius
494192711Syongari
495215297Smarius} // namespace lldb_private
496215297Smarius
497215297Smarius#endif  // liblldb_Block_h_
498215297Smarius