1254721Semaste//===-- ClangUtilityFunction.h ----------------------------------*- C++ -*-===//
2254721Semaste//
3254721Semaste//                     The LLVM Compiler Infrastructure
4254721Semaste//
5254721Semaste// This file is distributed under the University of Illinois Open Source
6254721Semaste// License. See LICENSE.TXT for details.
7254721Semaste//
8254721Semaste//===----------------------------------------------------------------------===//
9254721Semaste
10254721Semaste#ifndef liblldb_ClangUtilityFunction_h_
11254721Semaste#define liblldb_ClangUtilityFunction_h_
12254721Semaste
13254721Semaste// C Includes
14254721Semaste// C++ Includes
15254721Semaste#include <string>
16254721Semaste#include <map>
17254721Semaste#include <vector>
18254721Semaste
19254721Semaste// Other libraries and framework includes
20254721Semaste// Project includes
21254721Semaste
22254721Semaste#include "lldb/lldb-forward.h"
23254721Semaste#include "lldb/lldb-private.h"
24254721Semaste#include "lldb/Core/ClangForward.h"
25254721Semaste#include "lldb/Expression/ClangExpression.h"
26254721Semaste
27254721Semastenamespace lldb_private
28254721Semaste{
29254721Semaste
30254721Semaste//----------------------------------------------------------------------
31254721Semaste/// @class ClangUtilityFunction ClangUtilityFunction.h "lldb/Expression/ClangUtilityFunction.h"
32254721Semaste/// @brief Encapsulates a single expression for use with Clang
33254721Semaste///
34254721Semaste/// LLDB uses expressions for various purposes, notably to call functions
35254721Semaste/// and as a backend for the expr command.  ClangUtilityFunction encapsulates
36254721Semaste/// a self-contained function meant to be used from other code.  Utility
37254721Semaste/// functions can perform error-checking for ClangUserExpressions,
38254721Semaste//----------------------------------------------------------------------
39254721Semasteclass ClangUtilityFunction : public ClangExpression
40254721Semaste{
41254721Semastepublic:
42254721Semaste    //------------------------------------------------------------------
43254721Semaste    /// Constructor
44254721Semaste    ///
45254721Semaste    /// @param[in] text
46254721Semaste    ///     The text of the function.  Must be a full translation unit.
47254721Semaste    ///
48254721Semaste    /// @param[in] name
49254721Semaste    ///     The name of the function, as used in the text.
50254721Semaste    //------------------------------------------------------------------
51254721Semaste    ClangUtilityFunction (const char *text,
52254721Semaste                          const char *name);
53254721Semaste
54254721Semaste    virtual
55254721Semaste    ~ClangUtilityFunction ();
56254721Semaste
57254721Semaste    //------------------------------------------------------------------
58254721Semaste    /// Install the utility function into a process
59254721Semaste    ///
60254721Semaste    /// @param[in] error_stream
61254721Semaste    ///     A stream to print parse errors and warnings to.
62254721Semaste    ///
63254721Semaste    /// @param[in] exe_ctx
64254721Semaste    ///     The execution context to install the utility function to.
65254721Semaste    ///
66254721Semaste    /// @return
67254721Semaste    ///     True on success (no errors); false otherwise.
68254721Semaste    //------------------------------------------------------------------
69254721Semaste    bool
70254721Semaste    Install (Stream &error_stream, ExecutionContext &exe_ctx);
71254721Semaste
72254721Semaste    //------------------------------------------------------------------
73254721Semaste    /// Check whether the given PC is inside the function
74254721Semaste    ///
75254721Semaste    /// Especially useful if the function dereferences NULL to indicate a failed
76254721Semaste    /// assert.
77254721Semaste    ///
78254721Semaste    /// @param[in] pc
79254721Semaste    ///     The program counter to check.
80254721Semaste    ///
81254721Semaste    /// @return
82254721Semaste    ///     True if the program counter falls within the function's bounds;
83254721Semaste    ///     false if not (or the function is not JIT compiled)
84254721Semaste    //------------------------------------------------------------------
85254721Semaste    bool
86254721Semaste    ContainsAddress (lldb::addr_t address)
87254721Semaste    {
88254721Semaste        // nothing is both >= LLDB_INVALID_ADDRESS and < LLDB_INVALID_ADDRESS,
89254721Semaste        // so this always returns false if the function is not JIT compiled yet
90254721Semaste        return (address >= m_jit_start_addr && address < m_jit_end_addr);
91254721Semaste    }
92254721Semaste
93254721Semaste
94254721Semaste    //------------------------------------------------------------------
95254721Semaste    /// Return the string that the parser should parse.  Must be a full
96254721Semaste    /// translation unit.
97254721Semaste    //------------------------------------------------------------------
98254721Semaste    const char *
99254721Semaste    Text ()
100254721Semaste    {
101254721Semaste        return m_function_text.c_str();
102254721Semaste    }
103254721Semaste
104254721Semaste    //------------------------------------------------------------------
105254721Semaste    /// Return the function name that should be used for executing the
106254721Semaste    /// expression.  Text() should contain the definition of this
107254721Semaste    /// function.
108254721Semaste    //------------------------------------------------------------------
109254721Semaste    const char *
110254721Semaste    FunctionName ()
111254721Semaste    {
112254721Semaste        return m_function_name.c_str();
113254721Semaste    }
114254721Semaste
115254721Semaste    //------------------------------------------------------------------
116254721Semaste    /// Return the object that the parser should use when resolving external
117254721Semaste    /// values.  May be NULL if everything should be self-contained.
118254721Semaste    //------------------------------------------------------------------
119254721Semaste    ClangExpressionDeclMap *
120254721Semaste    DeclMap ()
121254721Semaste    {
122254721Semaste        return m_expr_decl_map.get();
123254721Semaste    }
124254721Semaste
125254721Semaste    //------------------------------------------------------------------
126254721Semaste    /// Return the object that the parser should use when registering
127254721Semaste    /// local variables.  May be NULL if the Expression doesn't care.
128254721Semaste    //------------------------------------------------------------------
129254721Semaste    ClangExpressionVariableList *
130254721Semaste    LocalVariables ()
131254721Semaste    {
132254721Semaste        return NULL;
133254721Semaste    }
134254721Semaste
135254721Semaste    //------------------------------------------------------------------
136254721Semaste    /// Return the object that the parser should allow to access ASTs.
137254721Semaste    /// May be NULL if the ASTs do not need to be transformed.
138254721Semaste    ///
139254721Semaste    /// @param[in] passthrough
140254721Semaste    ///     The ASTConsumer that the returned transformer should send
141254721Semaste    ///     the ASTs to after transformation.
142254721Semaste    //------------------------------------------------------------------
143254721Semaste    clang::ASTConsumer *
144254721Semaste    ASTTransformer (clang::ASTConsumer *passthrough)
145254721Semaste    {
146254721Semaste        return NULL;
147254721Semaste    }
148254721Semaste
149254721Semaste    //------------------------------------------------------------------
150254721Semaste    /// Return true if validation code should be inserted into the
151254721Semaste    /// expression.
152254721Semaste    //------------------------------------------------------------------
153254721Semaste    bool
154254721Semaste    NeedsValidation ()
155254721Semaste    {
156254721Semaste        return false;
157254721Semaste    }
158254721Semaste
159254721Semaste    //------------------------------------------------------------------
160254721Semaste    /// Return true if external variables in the expression should be
161254721Semaste    /// resolved.
162254721Semaste    //------------------------------------------------------------------
163254721Semaste    bool
164254721Semaste    NeedsVariableResolution ()
165254721Semaste    {
166254721Semaste        return false;
167254721Semaste    }
168254721Semaste
169254721Semasteprivate:
170254721Semaste    std::unique_ptr<ClangExpressionDeclMap>  m_expr_decl_map;    ///< The map to use when parsing and materializing the expression.
171254721Semaste    std::unique_ptr<IRExecutionUnit>         m_execution_unit_ap;
172254721Semaste
173254721Semaste    std::string                             m_function_text;    ///< The text of the function.  Must be a well-formed translation unit.
174254721Semaste    std::string                             m_function_name;    ///< The name of the function.
175254721Semaste};
176254721Semaste
177254721Semaste} // namespace lldb_private
178254721Semaste
179254721Semaste#endif  // liblldb_ClangUtilityFunction_h_
180