1//===-- ClangExpressionParser.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_ClangExpressionParser_h_
11#define liblldb_ClangExpressionParser_h_
12
13#include "lldb/lldb-public.h"
14#include "lldb/Core/ArchSpec.h"
15#include "lldb/Core/ClangForward.h"
16#include "lldb/Core/Error.h"
17#include "lldb/Expression/IRForTarget.h"
18
19#include <string>
20#include <vector>
21
22namespace lldb_private
23{
24
25class IRExecutionUnit;
26
27//----------------------------------------------------------------------
28/// @class ClangExpressionParser ClangExpressionParser.h "lldb/Expression/ClangExpressionParser.h"
29/// @brief Encapsulates an instance of Clang that can parse expressions.
30///
31/// ClangExpressionParser is responsible for preparing an instance of
32/// ClangExpression for execution.  ClangExpressionParser uses ClangExpression
33/// as a glorified parameter list, performing the required parsing and
34/// conversion to formats (DWARF bytecode, or JIT compiled machine code)
35/// that can be executed.
36//----------------------------------------------------------------------
37class ClangExpressionParser
38{
39public:
40    //------------------------------------------------------------------
41    /// Constructor
42    ///
43    /// Initializes class variabes.
44    ///
45    /// @param[in] exe_scope,
46    ///     If non-NULL, an execution context scope that can help to
47    ///     correctly create an expression with a valid process for
48    ///     optional tuning Objective-C runtime support. Can be NULL.
49    ///
50    /// @param[in] expr
51    ///     The expression to be parsed.
52    //------------------------------------------------------------------
53    ClangExpressionParser (ExecutionContextScope *exe_scope,
54                           ClangExpression &expr);
55
56    //------------------------------------------------------------------
57    /// Destructor
58    //------------------------------------------------------------------
59    ~ClangExpressionParser ();
60
61    //------------------------------------------------------------------
62    /// Parse a single expression and convert it to IR using Clang.  Don't
63    /// wrap the expression in anything at all.
64    ///
65    /// @param[in] stream
66    ///     The stream to print errors to.
67    ///
68    /// @return
69    ///     The number of errors encountered during parsing.  0 means
70    ///     success.
71    //------------------------------------------------------------------
72    unsigned
73    Parse (Stream &stream);
74
75    //------------------------------------------------------------------
76    /// Ready an already-parsed expression for execution, possibly
77    /// evaluating it statically.
78    ///
79    /// @param[out] func_addr
80    ///     The address to which the function has been written.
81    ///
82    /// @param[out] func_end
83    ///     The end of the function's allocated memory region.  (func_addr
84    ///     and func_end do not delimit an allocated region; the allocated
85    ///     region may begin before func_addr.)
86    ///
87    /// @param[in] execution_unit_ap
88    ///     After parsing, ownership of the execution unit for
89    ///     for the expression is handed to this unique pointer.
90    ///
91    /// @param[in] exe_ctx
92    ///     The execution context to write the function into.
93    ///
94    /// @param[out] evaluated_statically
95    ///     Set to true if the expression could be interpreted statically;
96    ///     untouched otherwise.
97    ///
98    /// @param[out] const_result
99    ///     If the result of the expression is constant, and the
100    ///     expression has no side effects, this is set to the result of the
101    ///     expression.
102    ///
103    /// @param[in] execution_policy
104    ///     Determines whether the expression must be JIT-compiled, must be
105    ///     evaluated statically, or whether this decision may be made
106    ///     opportunistically.
107    ///
108    /// @return
109    ///     An error code indicating the success or failure of the operation.
110    ///     Test with Success().
111    //------------------------------------------------------------------
112    Error
113    PrepareForExecution (lldb::addr_t &func_addr,
114                         lldb::addr_t &func_end,
115                         std::unique_ptr<IRExecutionUnit> &execution_unit_ap,
116                         ExecutionContext &exe_ctx,
117                         bool &can_interpret,
118                         lldb_private::ExecutionPolicy execution_policy);
119
120    //------------------------------------------------------------------
121    /// Disassemble the machine code for a JITted function from the target
122    /// process's memory and print the result to a stream.
123    ///
124    /// @param[in] stream
125    ///     The stream to print disassembly to.
126    ///
127    /// @param[in] exc_context
128    ///     The execution context to get the machine code from.
129    ///
130    /// @return
131    ///     The error generated.  If .Success() is true, disassembly succeeded.
132    //------------------------------------------------------------------
133    Error
134    DisassembleFunction (Stream &stream,
135                         ExecutionContext &exe_ctx);
136
137private:
138    ClangExpression &                       m_expr;                 ///< The expression to be parsed
139    std::unique_ptr<llvm::LLVMContext>       m_llvm_context;         ///< The LLVM context to generate IR into
140    std::unique_ptr<clang::FileManager>      m_file_manager;         ///< The Clang file manager object used by the compiler
141    std::unique_ptr<clang::CompilerInstance> m_compiler;             ///< The Clang compiler used to parse expressions into IR
142    std::unique_ptr<clang::Builtin::Context> m_builtin_context;      ///< Context for Clang built-ins
143    std::unique_ptr<clang::SelectorTable>    m_selector_table;       ///< Selector table for Objective-C methods
144    std::unique_ptr<clang::ASTContext>       m_ast_context;          ///< The AST context used to hold types and names for the parser
145    std::unique_ptr<clang::CodeGenerator>    m_code_generator;       ///< The Clang object that generates IR
146    std::unique_ptr<IRExecutionUnit>         m_execution_unit;       ///< The container for the finished Module
147};
148
149}
150
151#endif  // liblldb_ClangExpressionParser_h_
152