ClangUtilityFunction.h revision 1cf3da8b0fb0cabf2431b5fe521842929fca69a3
1//===-- ClangUtilityFunction.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_ClangUtilityFunction_h_
11#define liblldb_ClangUtilityFunction_h_
12
13// C Includes
14// C++ Includes
15#include <string>
16#include <map>
17#include <memory>
18#include <vector>
19
20// Other libraries and framework includes
21// Project includes
22
23#include "lldb/lldb-forward.h"
24#include "lldb/lldb-private.h"
25#include "lldb/Core/ClangForward.h"
26#include "lldb/Expression/ClangExpression.h"
27
28namespace lldb_private
29{
30
31//----------------------------------------------------------------------
32/// @class ClangUtilityFunction ClangUtilityFunction.h "lldb/Expression/ClangUtilityFunction.h"
33/// @brief Encapsulates a single expression for use with Clang
34///
35/// LLDB uses expressions for various purposes, notably to call functions
36/// and as a backend for the expr command.  ClangUtilityFunction encapsulates
37/// a self-contained function meant to be used from other code.  Utility
38/// functions can perform error-checking for ClangUserExpressions,
39//----------------------------------------------------------------------
40class ClangUtilityFunction : public ClangExpression
41{
42public:
43    //------------------------------------------------------------------
44    /// Constructor
45    ///
46    /// @param[in] text
47    ///     The text of the function.  Must be a full translation unit.
48    ///
49    /// @param[in] name
50    ///     The name of the function, as used in the text.
51    //------------------------------------------------------------------
52    ClangUtilityFunction (const char *text,
53                          const char *name);
54
55    virtual
56    ~ClangUtilityFunction ();
57
58    //------------------------------------------------------------------
59    /// Install the utility function into a process
60    ///
61    /// @param[in] error_stream
62    ///     A stream to print parse errors and warnings to.
63    ///
64    /// @param[in] exe_ctx
65    ///     The execution context to install the utility function to.
66    ///
67    /// @return
68    ///     True on success (no errors); false otherwise.
69    //------------------------------------------------------------------
70    bool
71    Install (Stream &error_stream, ExecutionContext &exe_ctx);
72
73    //------------------------------------------------------------------
74    /// Check whether the given PC is inside the function
75    ///
76    /// Especially useful if the function dereferences NULL to indicate a failed
77    /// assert.
78    ///
79    /// @param[in] pc
80    ///     The program counter to check.
81    ///
82    /// @return
83    ///     True if the program counter falls within the function's bounds;
84    ///     false if not (or the function is not JIT compiled)
85    //------------------------------------------------------------------
86    bool
87    ContainsAddress (lldb::addr_t address)
88    {
89        // nothing is both >= LLDB_INVALID_ADDRESS and < LLDB_INVALID_ADDRESS,
90        // so this always returns false if the function is not JIT compiled yet
91        return (address >= m_jit_start_addr && address < m_jit_end_addr);
92    }
93
94
95    //------------------------------------------------------------------
96    /// Return the string that the parser should parse.  Must be a full
97    /// translation unit.
98    //------------------------------------------------------------------
99    const char *
100    Text ()
101    {
102        return m_function_text.c_str();
103    }
104
105    //------------------------------------------------------------------
106    /// Return the function name that should be used for executing the
107    /// expression.  Text() should contain the definition of this
108    /// function.
109    //------------------------------------------------------------------
110    const char *
111    FunctionName ()
112    {
113        return m_function_name.c_str();
114    }
115
116    //------------------------------------------------------------------
117    /// Return the object that the parser should use when resolving external
118    /// values.  May be NULL if everything should be self-contained.
119    //------------------------------------------------------------------
120    ClangExpressionDeclMap *
121    DeclMap ()
122    {
123        return m_expr_decl_map.get();
124    }
125
126    //------------------------------------------------------------------
127    /// Return the object that the parser should use when registering
128    /// local variables.  May be NULL if the Expression doesn't care.
129    //------------------------------------------------------------------
130    ClangExpressionVariableList *
131    LocalVariables ()
132    {
133        return NULL;
134    }
135
136    //------------------------------------------------------------------
137    /// Return the object that the parser should allow to access ASTs.
138    /// May be NULL if the ASTs do not need to be transformed.
139    ///
140    /// @param[in] passthrough
141    ///     The ASTConsumer that the returned transformer should send
142    ///     the ASTs to after transformation.
143    //------------------------------------------------------------------
144    clang::ASTConsumer *
145    ASTTransformer (clang::ASTConsumer *passthrough)
146    {
147        return NULL;
148    }
149
150    //------------------------------------------------------------------
151    /// Return true if validation code should be inserted into the
152    /// expression.
153    //------------------------------------------------------------------
154    bool
155    NeedsValidation ()
156    {
157        return false;
158    }
159
160    //------------------------------------------------------------------
161    /// Return true if external variables in the expression should be
162    /// resolved.
163    //------------------------------------------------------------------
164    bool
165    NeedsVariableResolution ()
166    {
167        return false;
168    }
169
170private:
171    std::auto_ptr<ClangExpressionDeclMap>   m_expr_decl_map;    ///< The map to use when parsing and materializing the expression.
172
173    std::string                             m_function_text;    ///< The text of the function.  Must be a well-formed translation unit.
174    std::string                             m_function_name;    ///< The name of the function.
175};
176
177} // namespace lldb_private
178
179#endif  // liblldb_ClangUtilityFunction_h_
180