1254721Semaste//===-- ClangExpressionParser.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_ClangExpressionParser_h_
11254721Semaste#define liblldb_ClangExpressionParser_h_
12254721Semaste
13254721Semaste#include "lldb/lldb-public.h"
14254721Semaste#include "lldb/Core/ArchSpec.h"
15254721Semaste#include "lldb/Core/ClangForward.h"
16254721Semaste#include "lldb/Core/Error.h"
17254721Semaste#include "lldb/Expression/IRForTarget.h"
18254721Semaste
19254721Semaste#include <string>
20254721Semaste#include <vector>
21254721Semaste
22254721Semastenamespace lldb_private
23254721Semaste{
24254721Semaste
25254721Semasteclass IRExecutionUnit;
26254721Semaste
27254721Semaste//----------------------------------------------------------------------
28254721Semaste/// @class ClangExpressionParser ClangExpressionParser.h "lldb/Expression/ClangExpressionParser.h"
29254721Semaste/// @brief Encapsulates an instance of Clang that can parse expressions.
30254721Semaste///
31254721Semaste/// ClangExpressionParser is responsible for preparing an instance of
32254721Semaste/// ClangExpression for execution.  ClangExpressionParser uses ClangExpression
33254721Semaste/// as a glorified parameter list, performing the required parsing and
34254721Semaste/// conversion to formats (DWARF bytecode, or JIT compiled machine code)
35254721Semaste/// that can be executed.
36254721Semaste//----------------------------------------------------------------------
37254721Semasteclass ClangExpressionParser
38254721Semaste{
39254721Semastepublic:
40254721Semaste    //------------------------------------------------------------------
41254721Semaste    /// Constructor
42254721Semaste    ///
43254721Semaste    /// Initializes class variabes.
44254721Semaste    ///
45254721Semaste    /// @param[in] exe_scope,
46254721Semaste    ///     If non-NULL, an execution context scope that can help to
47254721Semaste    ///     correctly create an expression with a valid process for
48254721Semaste    ///     optional tuning Objective-C runtime support. Can be NULL.
49254721Semaste    ///
50254721Semaste    /// @param[in] expr
51254721Semaste    ///     The expression to be parsed.
52254721Semaste    //------------------------------------------------------------------
53254721Semaste    ClangExpressionParser (ExecutionContextScope *exe_scope,
54254721Semaste                           ClangExpression &expr);
55254721Semaste
56254721Semaste    //------------------------------------------------------------------
57254721Semaste    /// Destructor
58254721Semaste    //------------------------------------------------------------------
59254721Semaste    ~ClangExpressionParser ();
60254721Semaste
61254721Semaste    //------------------------------------------------------------------
62254721Semaste    /// Parse a single expression and convert it to IR using Clang.  Don't
63254721Semaste    /// wrap the expression in anything at all.
64254721Semaste    ///
65254721Semaste    /// @param[in] stream
66254721Semaste    ///     The stream to print errors to.
67254721Semaste    ///
68254721Semaste    /// @return
69254721Semaste    ///     The number of errors encountered during parsing.  0 means
70254721Semaste    ///     success.
71254721Semaste    //------------------------------------------------------------------
72254721Semaste    unsigned
73254721Semaste    Parse (Stream &stream);
74254721Semaste
75254721Semaste    //------------------------------------------------------------------
76254721Semaste    /// Ready an already-parsed expression for execution, possibly
77254721Semaste    /// evaluating it statically.
78254721Semaste    ///
79254721Semaste    /// @param[out] func_addr
80254721Semaste    ///     The address to which the function has been written.
81254721Semaste    ///
82254721Semaste    /// @param[out] func_end
83254721Semaste    ///     The end of the function's allocated memory region.  (func_addr
84254721Semaste    ///     and func_end do not delimit an allocated region; the allocated
85254721Semaste    ///     region may begin before func_addr.)
86254721Semaste    ///
87254721Semaste    /// @param[in] execution_unit_ap
88254721Semaste    ///     After parsing, ownership of the execution unit for
89254721Semaste    ///     for the expression is handed to this unique pointer.
90254721Semaste    ///
91254721Semaste    /// @param[in] exe_ctx
92254721Semaste    ///     The execution context to write the function into.
93254721Semaste    ///
94254721Semaste    /// @param[out] evaluated_statically
95254721Semaste    ///     Set to true if the expression could be interpreted statically;
96254721Semaste    ///     untouched otherwise.
97254721Semaste    ///
98254721Semaste    /// @param[out] const_result
99254721Semaste    ///     If the result of the expression is constant, and the
100254721Semaste    ///     expression has no side effects, this is set to the result of the
101254721Semaste    ///     expression.
102254721Semaste    ///
103254721Semaste    /// @param[in] execution_policy
104254721Semaste    ///     Determines whether the expression must be JIT-compiled, must be
105254721Semaste    ///     evaluated statically, or whether this decision may be made
106254721Semaste    ///     opportunistically.
107254721Semaste    ///
108254721Semaste    /// @return
109254721Semaste    ///     An error code indicating the success or failure of the operation.
110254721Semaste    ///     Test with Success().
111254721Semaste    //------------------------------------------------------------------
112254721Semaste    Error
113254721Semaste    PrepareForExecution (lldb::addr_t &func_addr,
114254721Semaste                         lldb::addr_t &func_end,
115254721Semaste                         std::unique_ptr<IRExecutionUnit> &execution_unit_ap,
116254721Semaste                         ExecutionContext &exe_ctx,
117254721Semaste                         bool &can_interpret,
118254721Semaste                         lldb_private::ExecutionPolicy execution_policy);
119254721Semaste
120254721Semaste    //------------------------------------------------------------------
121254721Semaste    /// Disassemble the machine code for a JITted function from the target
122254721Semaste    /// process's memory and print the result to a stream.
123254721Semaste    ///
124254721Semaste    /// @param[in] stream
125254721Semaste    ///     The stream to print disassembly to.
126254721Semaste    ///
127254721Semaste    /// @param[in] exc_context
128254721Semaste    ///     The execution context to get the machine code from.
129254721Semaste    ///
130254721Semaste    /// @return
131254721Semaste    ///     The error generated.  If .Success() is true, disassembly succeeded.
132254721Semaste    //------------------------------------------------------------------
133254721Semaste    Error
134254721Semaste    DisassembleFunction (Stream &stream,
135254721Semaste                         ExecutionContext &exe_ctx);
136254721Semaste
137254721Semasteprivate:
138254721Semaste    ClangExpression &                       m_expr;                 ///< The expression to be parsed
139254721Semaste    std::unique_ptr<llvm::LLVMContext>       m_llvm_context;         ///< The LLVM context to generate IR into
140254721Semaste    std::unique_ptr<clang::FileManager>      m_file_manager;         ///< The Clang file manager object used by the compiler
141254721Semaste    std::unique_ptr<clang::CompilerInstance> m_compiler;             ///< The Clang compiler used to parse expressions into IR
142254721Semaste    std::unique_ptr<clang::Builtin::Context> m_builtin_context;      ///< Context for Clang built-ins
143254721Semaste    std::unique_ptr<clang::SelectorTable>    m_selector_table;       ///< Selector table for Objective-C methods
144254721Semaste    std::unique_ptr<clang::ASTContext>       m_ast_context;          ///< The AST context used to hold types and names for the parser
145254721Semaste    std::unique_ptr<clang::CodeGenerator>    m_code_generator;       ///< The Clang object that generates IR
146254721Semaste    std::unique_ptr<IRExecutionUnit>         m_execution_unit;       ///< The container for the finished Module
147254721Semaste};
148254721Semaste
149254721Semaste}
150254721Semaste
151254721Semaste#endif  // liblldb_ClangExpressionParser_h_
152