Function.h revision 1924e2408687e0ee41976010c6b9410bdd01270d
1//===-- Function.h ----------------------------------------------*- 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#ifndef liblldb_Function_h_
11#define liblldb_Function_h_
12
13#include "lldb/Core/AddressRange.h"
14#include "lldb/Symbol/Block.h"
15#include "lldb/Symbol/Declaration.h"
16#include "lldb/Expression/DWARFExpression.h"
17#include "lldb/Core/Mangled.h"
18#include "lldb/Core/UserID.h"
19
20namespace lldb_private {
21
22//----------------------------------------------------------------------
23/// @class FunctionInfo Function.h "lldb/Symbol/Function.h"
24/// @brief A class that contains generic function information.
25///
26/// This provides generic function information that gets resused between
27/// inline functions and function types.
28//----------------------------------------------------------------------
29class FunctionInfo
30{
31public:
32    //------------------------------------------------------------------
33    /// Construct with the function method name and optional declaration
34    /// information.
35    ///
36    /// @param[in] name
37    ///     A C string name for the method name for this function. This
38    ///     value should not be the mangled named, but the simple method
39    ///     name.
40    ///
41    /// @param[in] decl_ptr
42    ///     Optional declaration information that describes where the
43    ///     function was declared. This can be NULL.
44    //------------------------------------------------------------------
45    FunctionInfo (const char *name, const Declaration *decl_ptr);
46
47    //------------------------------------------------------------------
48    /// Construct with the function method name and optional declaration
49    /// information.
50    ///
51    /// @param[in] name
52    ///     A name for the method name for this function. This value
53    ///     should not be the mangled named, but the simple method name.
54    ///
55    /// @param[in] decl_ptr
56    ///     Optional declaration information that describes where the
57    ///     function was declared. This can be NULL.
58    //------------------------------------------------------------------
59    FunctionInfo (const ConstString& name, const Declaration *decl_ptr);
60
61    //------------------------------------------------------------------
62    /// Destructor.
63    ///
64    /// The destructor is virtual since classes inherit from this class.
65    //------------------------------------------------------------------
66    virtual
67    ~FunctionInfo ();
68
69    //------------------------------------------------------------------
70    /// Compare two function information objects.
71    ///
72    /// First compares the method names, and if equal, then compares
73    /// the declaration information.
74    ///
75    /// @param[in] lhs
76    ///     The Left Hand Side const FunctionInfo object reference.
77    ///
78    /// @param[in] rhs
79    ///     The Right Hand Side const FunctionInfo object reference.
80    ///
81    /// @return
82    ///     @li -1 if lhs < rhs
83    ///     @li 0 if lhs == rhs
84    ///     @li 1 if lhs > rhs
85    //------------------------------------------------------------------
86    static int
87    Compare (const FunctionInfo& lhs, const FunctionInfo& rhs);
88
89    //------------------------------------------------------------------
90    /// Dump a description of this object to a Stream.
91    ///
92    /// Dump a description of the contents of this object to the
93    /// supplied stream \a s.
94    ///
95    /// @param[in] s
96    ///     The stream to which to dump the object descripton.
97    //------------------------------------------------------------------
98    void
99    Dump (Stream *s, bool show_fullpaths) const;
100
101    //------------------------------------------------------------------
102    /// Get accessor for the declaration information.
103    ///
104    /// @return
105    ///     A reference to the declaration object.
106    //------------------------------------------------------------------
107    Declaration&
108    GetDeclaration ();
109
110    //------------------------------------------------------------------
111    /// Get const accessor for the declaration information.
112    ///
113    /// @return
114    ///     A const reference to the declaration object.
115    //------------------------------------------------------------------
116    const Declaration&
117    GetDeclaration () const;
118
119    //------------------------------------------------------------------
120    /// Get accessor for the method name.
121    ///
122    /// @return
123    ///     A const reference to the method name object.
124    //------------------------------------------------------------------
125    const ConstString&
126    GetName () const;
127
128    //------------------------------------------------------------------
129    /// Get the memory cost of this object.
130    ///
131    /// @return
132    ///     The number of bytes that this object occupies in memory.
133    ///     The returned value does not include the bytes for any
134    ///     shared string values.
135    ///
136    /// @see ConstString::StaticMemorySize ()
137    //------------------------------------------------------------------
138    virtual size_t
139    MemorySize () const;
140
141protected:
142    //------------------------------------------------------------------
143    // Member variables.
144    //------------------------------------------------------------------
145    ConstString m_name; ///< Function method name (not a mangled name).
146    Declaration m_declaration; ///< Information describing where this function information was defined.
147};
148
149
150//----------------------------------------------------------------------
151/// @class InlineFunctionInfo Function.h "lldb/Symbol/Function.h"
152/// @brief A class that describes information for an inlined function.
153//----------------------------------------------------------------------
154class InlineFunctionInfo : public FunctionInfo
155{
156public:
157    //------------------------------------------------------------------
158    /// Construct with the function method name, mangled name, and
159    /// optional declaration information.
160    ///
161    /// @param[in] name
162    ///     A C string name for the method name for this function. This
163    ///     value should not be the mangled named, but the simple method
164    ///     name.
165    ///
166    /// @param[in] mangled
167    ///     A C string name for the mangled name for this function. This
168    ///     value can be NULL if there is no mangled information.
169    ///
170    /// @param[in] decl_ptr
171    ///     Optional declaration information that describes where the
172    ///     function was declared. This can be NULL.
173    ///
174    /// @param[in] call_decl_ptr
175    ///     Optional calling location declaration information that
176    ///     describes from where this inlined function was called.
177    //------------------------------------------------------------------
178    InlineFunctionInfo(const char *name, const char *mangled, const Declaration *decl_ptr, const Declaration *call_decl_ptr);
179
180    //------------------------------------------------------------------
181    /// Construct with the function method name, mangled name, and
182    /// optional declaration information.
183    ///
184    /// @param[in] name
185    ///     A name for the method name for this function. This value
186    ///     should not be the mangled named, but the simple method name.
187    ///
188    /// @param[in] mangled
189    ///     A name for the mangled name for this function. This value
190    ///     can be empty if there is no mangled information.
191    ///
192    /// @param[in] decl_ptr
193    ///     Optional declaration information that describes where the
194    ///     function was declared. This can be NULL.
195    ///
196    /// @param[in] call_decl_ptr
197    ///     Optional calling location declaration information that
198    ///     describes from where this inlined function was called.
199    //------------------------------------------------------------------
200    InlineFunctionInfo(const ConstString& name, const Mangled &mangled, const Declaration *decl_ptr, const Declaration *call_decl_ptr);
201
202    //------------------------------------------------------------------
203    /// Destructor.
204    //------------------------------------------------------------------
205    ~InlineFunctionInfo();
206
207    //------------------------------------------------------------------
208    /// Compare two inlined function information objects.
209    ///
210    /// First compares the FunctionInfo objects, and if equal,
211    /// compares the mangled names.
212    ///
213    /// @param[in] lhs
214    ///     The Left Hand Side const InlineFunctionInfo object
215    ///     reference.
216    ///
217    /// @param[in] rhs
218    ///     The Right Hand Side const InlineFunctionInfo object
219    ///     reference.
220    ///
221    /// @return
222    ///     @li -1 if lhs < rhs
223    ///     @li 0 if lhs == rhs
224    ///     @li 1 if lhs > rhs
225    //------------------------------------------------------------------
226    int
227    Compare(const InlineFunctionInfo& lhs, const InlineFunctionInfo& rhs);
228
229    //------------------------------------------------------------------
230    /// Dump a description of this object to a Stream.
231    ///
232    /// Dump a description of the contents of this object to the
233    /// supplied stream \a s.
234    ///
235    /// @param[in] s
236    ///     The stream to which to dump the object descripton.
237    //------------------------------------------------------------------
238    void
239    Dump(Stream *s, bool show_fullpaths) const;
240
241    void
242    DumpStopContext (Stream *s) const;
243
244    const ConstString &
245    GetName () const;
246
247    //------------------------------------------------------------------
248    /// Get accessor for the call site declaration information.
249    ///
250    /// @return
251    ///     A reference to the declaration object.
252    //------------------------------------------------------------------
253    Declaration&
254    GetCallSite ();
255
256    //------------------------------------------------------------------
257    /// Get const accessor for the call site declaration information.
258    ///
259    /// @return
260    ///     A const reference to the declaration object.
261    //------------------------------------------------------------------
262    const Declaration&
263    GetCallSite () const;
264
265    //------------------------------------------------------------------
266    /// Get accessor for the mangled name object.
267    ///
268    /// @return
269    ///     A reference to the mangled name object.
270    //------------------------------------------------------------------
271    Mangled&
272    GetMangled();
273
274    //------------------------------------------------------------------
275    /// Get const accessor for the mangled name object.
276    ///
277    /// @return
278    ///     A const reference to the mangled name object.
279    //------------------------------------------------------------------
280    const Mangled&
281    GetMangled() const;
282
283    //------------------------------------------------------------------
284    /// Get the memory cost of this object.
285    ///
286    /// @return
287    ///     The number of bytes that this object occupies in memory.
288    ///     The returned value does not include the bytes for any
289    ///     shared string values.
290    ///
291    /// @see ConstString::StaticMemorySize ()
292    //------------------------------------------------------------------
293    virtual size_t
294    MemorySize() const;
295
296private:
297    //------------------------------------------------------------------
298    // Member variables.
299    //------------------------------------------------------------------
300    Mangled m_mangled; ///< Mangled inlined function name (can be empty if there is no mangled information).
301    Declaration m_call_decl;
302};
303
304//----------------------------------------------------------------------
305/// @class Function Function.h "lldb/Symbol/Function.h"
306/// @brief A class that describes a function.
307///
308/// Functions belong to CompileUnit objects (Function::m_comp_unit),
309/// have unique user IDs (Function::UserID), know how to reconstruct
310/// their symbol context (Function::SymbolContextScope), have a
311/// specific function type (Function::m_type_uid), have a simple
312/// method name (FunctionInfo::m_name), be declared at a specific
313/// location (FunctionInfo::m_declaration), possibly have mangled
314/// names (Function::m_mangled), an optional return type
315/// (Function::m_type), and contains lexical blocks
316/// (Function::m_blocks).
317///
318/// The function inforation is split into a few pieces:
319///     @li The concrete instance information
320///     @li The abstract information
321///
322/// The abstract information is found in the function type (Type) that
323/// describes a function information, return type and parameter types.
324///
325/// The concreate information is the address range information and
326/// specific locations for an instance of this function.
327//----------------------------------------------------------------------
328class Function :
329    public UserID,
330    public SymbolContextScope
331{
332public:
333    //------------------------------------------------------------------
334    /// Construct with a compile unit, function UID, function type UID,
335    /// optional mangled name, function type, and a section offset
336    /// based address range.
337    ///
338    /// @param[in] comp_unit
339    ///     The compile unit to which this function belongs.
340    ///
341    /// @param[in] func_uid
342    ///     The UID for this function. This value is provided by the
343    ///     SymbolFile plug-in and can be any value that allows
344    ///     the plug-in to quickly find and parse more detailed
345    ///     information when and if more information is needed.
346    ///
347    /// @param[in] func_type_uid
348    ///     The type UID for the function Type to allow for lazy type
349    ///     parsing from the debug information.
350    ///
351    /// @param[in] mangled
352    ///     The optional mangled name for this function. If empty, there
353    ///     is no mangled information.
354    ///
355    /// @param[in] func_type
356    ///     The optional function type. If NULL, the function type will
357    ///     be parsed on demand when accessed using the
358    ///     Function::GetType() function by asking the SymbolFile
359    ///     plug-in to get the type for \a func_type_uid.
360    ///
361    /// @param[in] range
362    ///     The section offset based address for this function.
363    //------------------------------------------------------------------
364    Function (
365        CompileUnit *comp_unit,
366        lldb::user_id_t func_uid,
367        lldb::user_id_t func_type_uid,
368        const Mangled &mangled,
369        Type * func_type,
370        const AddressRange& range);
371
372    //------------------------------------------------------------------
373    /// Construct with a compile unit, function UID, function type UID,
374    /// optional mangled name, function type, and a section offset
375    /// based address range.
376    ///
377    /// @param[in] comp_unit
378    ///     The compile unit to which this function belongs.
379    ///
380    /// @param[in] func_uid
381    ///     The UID for this function. This value is provided by the
382    ///     SymbolFile plug-in and can be any value that allows
383    ///     the plug-in to quickly find and parse more detailed
384    ///     information when and if more information is needed.
385    ///
386    /// @param[in] func_type_uid
387    ///     The type UID for the function Type to allow for lazy type
388    ///     parsing from the debug information.
389    ///
390    /// @param[in] mangled
391    ///     The optional mangled name for this function. If empty, there
392    ///     is no mangled information.
393    ///
394    /// @param[in] func_type
395    ///     The optional function type. If NULL, the function type will
396    ///     be parsed on demand when accessed using the
397    ///     Function::GetType() function by asking the SymbolFile
398    ///     plug-in to get the type for \a func_type_uid.
399    ///
400    /// @param[in] range
401    ///     The section offset based address for this function.
402    //------------------------------------------------------------------
403    Function (
404        CompileUnit *comp_unit,
405        lldb::user_id_t func_uid,
406        lldb::user_id_t func_type_uid,
407        const char *mangled,
408        Type * func_type,
409        const AddressRange& range);
410
411    //------------------------------------------------------------------
412    /// Destructor.
413    //------------------------------------------------------------------
414    ~Function ();
415
416    //------------------------------------------------------------------
417    /// @copydoc SymbolContextScope::CalculateSymbolContext(SymbolContext*)
418    ///
419    /// @see SymbolContextScope
420    //------------------------------------------------------------------
421    virtual void
422    CalculateSymbolContext(SymbolContext* sc);
423
424    const AddressRange &
425    GetAddressRange()
426    {
427        return m_range;
428    }
429
430    //------------------------------------------------------------------
431    /// Find the file and line number of the source location of the start
432    /// of the function.  This will use the declaration if present and fall
433    /// back on the line table if that fails.  So there may NOT be a line
434    /// table entry for this source file/line combo.
435    ///
436    /// @param[out] source_file
437    ///     The source file.
438    ///
439    /// @param[out] line_no
440    ///     The line number.
441    //------------------------------------------------------------------
442    void
443    GetStartLineSourceInfo (FileSpec &source_file, uint32_t &line_no);
444
445     //------------------------------------------------------------------
446    /// Find the file and line number of the source location of the end
447    /// of the function.
448    ///
449    ///
450    /// @param[out] source_file
451    ///     The source file.
452    ///
453    /// @param[out] line_no
454    ///     The line number.
455    //------------------------------------------------------------------
456   void
457    GetEndLineSourceInfo (FileSpec &source_file, uint32_t &line_no);
458
459    //------------------------------------------------------------------
460    /// Get accessor for the block list.
461    ///
462    /// @return
463    ///     The block list object that describes all lexical blocks
464    ///     in the function.
465    ///
466    /// @see BlockList
467    //------------------------------------------------------------------
468    Block&
469    GetBlock (bool can_create);
470
471    //------------------------------------------------------------------
472    /// Get accessor for the compile unit that owns this function.
473    ///
474    /// @return
475    ///     A compile unit object pointer.
476    //------------------------------------------------------------------
477    CompileUnit*
478    GetCompileUnit();
479
480    //------------------------------------------------------------------
481    /// Get const accessor for the compile unit that owns this function.
482    ///
483    /// @return
484    ///     A const compile unit object pointer.
485    //------------------------------------------------------------------
486    const CompileUnit*
487    GetCompileUnit() const;
488
489    void
490    GetDescription(Stream *s, lldb::DescriptionLevel level, Target *target);
491
492    //------------------------------------------------------------------
493    /// Get accessor for the frame base location.
494    ///
495    /// @return
496    ///     A location expression that describes the function frame
497    ///     base.
498    //------------------------------------------------------------------
499    DWARFExpression &
500    GetFrameBaseExpression()
501    {
502        return m_frame_base;
503    }
504
505    //------------------------------------------------------------------
506    /// Get const accessor for the frame base location.
507    ///
508    /// @return
509    ///     A const compile unit object pointer.
510    //------------------------------------------------------------------
511    const DWARFExpression &
512    GetFrameBaseExpression() const
513    {
514        return m_frame_base;
515    }
516
517    const Mangled &
518    GetMangled() const
519    {
520        return m_mangled;
521    }
522
523    //------------------------------------------------------------------
524    /// Get accessor for the type that describes the function
525    /// return value type, and paramter types.
526    ///
527    /// @return
528    ///     A type object pointer.
529    //------------------------------------------------------------------
530    Type*
531    GetType();
532
533    //------------------------------------------------------------------
534    /// Get const accessor for the type that describes the function
535    /// return value type, and paramter types.
536    ///
537    /// @return
538    ///     A const type object pointer.
539    //------------------------------------------------------------------
540    const Type*
541    GetType() const;
542
543    Type
544    GetReturnType ();
545
546    // The Number of arguments, or -1 for an unprototyped function.
547    int
548    GetArgumentCount ();
549
550    const Type
551    GetArgumentTypeAtIndex (size_t idx);
552
553    const char *
554    GetArgumentNameAtIndex (size_t idx);
555
556    bool
557    IsVariadic ();
558
559    uint32_t
560    GetPrologueByteSize ();
561
562    //------------------------------------------------------------------
563    /// Dump a description of this object to a Stream.
564    ///
565    /// Dump a description of the contents of this object to the
566    /// supplied stream \a s.
567    ///
568    /// @param[in] s
569    ///     The stream to which to dump the object descripton.
570    ///
571    /// @param[in] show_context
572    ///     If \b true, variables will dump their symbol context
573    ///     information.
574    //------------------------------------------------------------------
575    void
576    Dump(Stream *s, bool show_context) const;
577
578    //------------------------------------------------------------------
579    /// @copydoc SymbolContextScope::DumpSymbolContext(Stream*)
580    ///
581    /// @see SymbolContextScope
582    //------------------------------------------------------------------
583    virtual void
584    DumpSymbolContext(Stream *s);
585
586    //------------------------------------------------------------------
587    /// Get the memory cost of this object.
588    ///
589    /// @return
590    ///     The number of bytes that this object occupies in memory.
591    ///     The returned value does not include the bytes for any
592    ///     shared string values.
593    ///
594    /// @see ConstString::StaticMemorySize ()
595    //------------------------------------------------------------------
596    size_t
597    MemorySize () const;
598
599protected:
600
601    enum
602    {
603        flagsCalculatedPrologueSize = (1 << 0)  ///< Have we already tried to calculate the prologue size?
604    };
605
606
607
608    //------------------------------------------------------------------
609    // Member variables.
610    //------------------------------------------------------------------
611    CompileUnit *m_comp_unit;       ///< The compile unit that owns this function.
612    lldb::user_id_t m_type_uid;     ///< The user ID of for the prototype Type for this function.
613    Type * m_type;                  ///< The function prototype type for this function that include the function info (FunctionInfo), return type and parameters.
614    Mangled m_mangled;              ///< The mangled function name if any, if empty, there is no mangled information.
615    Block m_block;                  ///< All lexical blocks contained in this function.
616    AddressRange m_range;           ///< The function address range that covers the widest range needed to contain all blocks
617    DWARFExpression m_frame_base;   ///< The frame base expression for variables that are relative to the frame pointer.
618    Flags m_flags;
619    uint32_t m_prologue_byte_size;  ///< Compute the prologue size once and cache it
620private:
621    DISALLOW_COPY_AND_ASSIGN(Function);
622};
623
624} // namespace lldb_private
625
626#endif  // liblldb_Function_h_
627