ClangExpression.h revision 7adfcfdb46018d16393995276699a04505727808
1//===-- ClangExpression.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_ClangExpression_h_
11#define liblldb_ClangExpression_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/Target/Process.h"
27
28namespace lldb_private {
29
30class RecordingMemoryManager;
31
32//----------------------------------------------------------------------
33/// @class ClangExpression ClangExpression.h "lldb/Expression/ClangExpression.h"
34/// @brief Encapsulates a single expression for use with Clang
35///
36/// LLDB uses expressions for various purposes, notably to call functions
37/// and as a backend for the expr command.  ClangExpression encapsulates
38/// the objects needed to parse and interpret or JIT an expression.  It
39/// uses the Clang parser to produce LLVM IR from the expression.
40//----------------------------------------------------------------------
41class ClangExpression
42{
43public:
44    enum ResultType {
45        eResultTypeAny,
46        eResultTypeId
47    };
48
49    ClangExpression () :
50        m_jit_process_wp(),
51        m_jit_alloc (LLDB_INVALID_ADDRESS),
52        m_jit_start_addr (LLDB_INVALID_ADDRESS),
53        m_jit_end_addr (LLDB_INVALID_ADDRESS)
54    {
55    }
56
57    //------------------------------------------------------------------
58    /// Destructor
59    //------------------------------------------------------------------
60    virtual ~ClangExpression ()
61    {
62        DeallocateJITFunction ();
63    }
64
65    //------------------------------------------------------------------
66    /// Return the string that the parser should parse.  Must be a full
67    /// translation unit.
68    //------------------------------------------------------------------
69    virtual const char *
70    Text () = 0;
71
72    //------------------------------------------------------------------
73    /// Return the function name that should be used for executing the
74    /// expression.  Text() should contain the definition of this
75    /// function.
76    //------------------------------------------------------------------
77    virtual const char *
78    FunctionName () = 0;
79
80    //------------------------------------------------------------------
81    /// Return the language that should be used when parsing.  To use
82    /// the default, return eLanguageTypeUnknown.
83    //------------------------------------------------------------------
84    virtual lldb::LanguageType
85    Language ()
86    {
87        return lldb::eLanguageTypeUnknown;
88    }
89
90    //------------------------------------------------------------------
91    /// Return the object that the parser should use when resolving external
92    /// values.  May be NULL if everything should be self-contained.
93    //------------------------------------------------------------------
94    virtual ClangExpressionDeclMap *
95    DeclMap () = 0;
96
97    //------------------------------------------------------------------
98    /// Return the object that the parser should use when registering
99    /// local variables.  May be NULL if the Expression doesn't care.
100    //------------------------------------------------------------------
101    virtual ClangExpressionVariableList *
102    LocalVariables () = 0;
103
104    //------------------------------------------------------------------
105    /// Return the object that the parser should allow to access ASTs.
106    /// May be NULL if the ASTs do not need to be transformed.
107    ///
108    /// @param[in] passthrough
109    ///     The ASTConsumer that the returned transformer should send
110    ///     the ASTs to after transformation.
111    //------------------------------------------------------------------
112    virtual clang::ASTConsumer *
113    ASTTransformer (clang::ASTConsumer *passthrough) = 0;
114
115    //------------------------------------------------------------------
116    /// Return the desired result type of the function, or
117    /// eResultTypeAny if indifferent.
118    //------------------------------------------------------------------
119    virtual ResultType
120    DesiredResultType ()
121    {
122        return eResultTypeAny;
123    }
124
125    //------------------------------------------------------------------
126    /// Flags
127    //------------------------------------------------------------------
128
129    //------------------------------------------------------------------
130    /// Return true if validation code should be inserted into the
131    /// expression.
132    //------------------------------------------------------------------
133    virtual bool
134    NeedsValidation () = 0;
135
136    //------------------------------------------------------------------
137    /// Return true if external variables in the expression should be
138    /// resolved.
139    //------------------------------------------------------------------
140    virtual bool
141    NeedsVariableResolution () = 0;
142
143    void
144    DeallocateJITFunction ()
145    {
146        lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());
147        if (jit_process_sp && m_jit_alloc != LLDB_INVALID_ADDRESS)
148        {
149            jit_process_sp->DeallocateMemory (m_jit_alloc);
150            // If this process is ever used for anything else, we can not clear it
151            // here. For now it is only used in order to deallocate any code if
152            // m_jit_alloc is a valid address.
153            m_jit_alloc = LLDB_INVALID_ADDRESS;
154        }
155    }
156
157    //------------------------------------------------------------------
158    /// Return the address of the function's JIT-compiled code, or
159    /// LLDB_INVALID_ADDRESS if the function is not JIT compiled
160    //------------------------------------------------------------------
161    lldb::addr_t
162    StartAddress ()
163    {
164        return m_jit_start_addr;
165    }
166
167protected:
168
169    lldb::ProcessWP m_jit_process_wp;
170    lldb::addr_t    m_jit_alloc;            ///< The address of the block containing JITted code.  LLDB_INVALID_ADDRESS if invalid.
171    lldb::addr_t    m_jit_start_addr;       ///< The address of the JITted function within the JIT allocation.  LLDB_INVALID_ADDRESS if invalid.
172    lldb::addr_t    m_jit_end_addr;         ///< The address of the JITted function within the JIT allocation.  LLDB_INVALID_ADDRESS if invalid.
173
174};
175
176} // namespace lldb_private
177
178#endif  // liblldb_ClangExpression_h_
179