1 //===-- ClangUserExpression.h -----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_ClangUserExpression_h_
11 #define liblldb_ClangUserExpression_h_
17 // Other libraries and framework includes
19 #include "ASTResultSynthesizer.h"
20 #include "ASTStructExtractor.h"
21 #include "ClangExpressionDeclMap.h"
22 #include "ClangExpressionHelper.h"
23 #include "ClangExpressionVariable.h"
24 #include "IRForTarget.h"
26 #include "lldb/Core/Address.h"
27 #include "lldb/Core/ClangForward.h"
28 #include "lldb/Expression/LLVMUserExpression.h"
29 #include "lldb/Expression/Materializer.h"
30 #include "lldb/Target/ExecutionContext.h"
31 #include "lldb/lldb-forward.h"
32 #include "lldb/lldb-private.h"
34 namespace lldb_private {
36 //----------------------------------------------------------------------
37 /// @class ClangUserExpression ClangUserExpression.h
38 /// "lldb/Expression/ClangUserExpression.h" Encapsulates a single expression
39 /// for use with Clang
41 /// LLDB uses expressions for various purposes, notably to call functions
42 /// and as a backend for the expr command. ClangUserExpression encapsulates
43 /// the objects needed to parse and interpret or JIT an expression. It uses
44 /// the Clang parser to produce LLVM IR from the expression.
45 //----------------------------------------------------------------------
46 class ClangUserExpression : public LLVMUserExpression {
48 enum { kDefaultTimeout = 500000u };
50 class ClangUserExpressionHelper : public ClangExpressionHelper {
52 ClangUserExpressionHelper(Target &target, bool top_level)
53 : m_target(target), m_top_level(top_level) {}
55 ~ClangUserExpressionHelper() override = default;
57 //------------------------------------------------------------------
58 /// Return the object that the parser should use when resolving external
59 /// values. May be NULL if everything should be self-contained.
60 //------------------------------------------------------------------
61 ClangExpressionDeclMap *DeclMap() override {
62 return m_expr_decl_map_up.get();
65 void ResetDeclMap() { m_expr_decl_map_up.reset(); }
67 void ResetDeclMap(ExecutionContext &exe_ctx,
68 Materializer::PersistentVariableDelegate &result_delegate,
69 bool keep_result_in_memory);
71 //------------------------------------------------------------------
72 /// Return the object that the parser should allow to access ASTs. May be
73 /// NULL if the ASTs do not need to be transformed.
75 /// @param[in] passthrough
76 /// The ASTConsumer that the returned transformer should send
77 /// the ASTs to after transformation.
78 //------------------------------------------------------------------
80 ASTTransformer(clang::ASTConsumer *passthrough) override;
82 void CommitPersistentDecls() override;
86 std::unique_ptr<ClangExpressionDeclMap> m_expr_decl_map_up;
87 std::unique_ptr<ASTStructExtractor> m_struct_extractor_up; ///< The class
91 std::unique_ptr<ASTResultSynthesizer> m_result_synthesizer_up;
95 //------------------------------------------------------------------
99 /// The expression to parse.
101 /// @param[in] expr_prefix
102 /// If non-NULL, a C string containing translation-unit level
103 /// definitions to be included when the expression is parsed.
105 /// @param[in] language
106 /// If not eLanguageTypeUnknown, a language to use when parsing
107 /// the expression. Currently restricted to those languages
108 /// supported by Clang.
110 /// @param[in] desired_type
111 /// If not eResultTypeAny, the type to use for the expression
113 //------------------------------------------------------------------
114 ClangUserExpression(ExecutionContextScope &exe_scope, llvm::StringRef expr,
115 llvm::StringRef prefix, lldb::LanguageType language,
116 ResultType desired_type,
117 const EvaluateExpressionOptions &options);
119 ~ClangUserExpression() override;
121 //------------------------------------------------------------------
122 /// Parse the expression
124 /// @param[in] diagnostic_manager
125 /// A diagnostic manager to report parse errors and warnings to.
127 /// @param[in] exe_ctx
128 /// The execution context to use when looking up entities that
129 /// are needed for parsing (locations of functions, types of
130 /// variables, persistent variables, etc.)
132 /// @param[in] execution_policy
133 /// Determines whether interpretation is possible or mandatory.
135 /// @param[in] keep_result_in_memory
136 /// True if the resulting persistent variable should reside in
137 /// target memory, if applicable.
140 /// True on success (no errors); false otherwise.
141 //------------------------------------------------------------------
142 bool Parse(DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx,
143 lldb_private::ExecutionPolicy execution_policy,
144 bool keep_result_in_memory, bool generate_debug_info) override;
146 ExpressionTypeSystemHelper *GetTypeSystemHelper() override {
147 return &m_type_system_helper;
150 ClangExpressionDeclMap *DeclMap() { return m_type_system_helper.DeclMap(); }
152 void ResetDeclMap() { m_type_system_helper.ResetDeclMap(); }
154 void ResetDeclMap(ExecutionContext &exe_ctx,
155 Materializer::PersistentVariableDelegate &result_delegate,
156 bool keep_result_in_memory) {
157 m_type_system_helper.ResetDeclMap(exe_ctx, result_delegate,
158 keep_result_in_memory);
161 lldb::ExpressionVariableSP
162 GetResultAfterDematerialization(ExecutionContextScope *exe_scope) override;
165 //------------------------------------------------------------------
166 /// Populate m_in_cplusplus_method and m_in_objectivec_method based on the
168 //------------------------------------------------------------------
170 void ScanContext(ExecutionContext &exe_ctx,
171 lldb_private::Status &err) override;
173 bool AddArguments(ExecutionContext &exe_ctx, std::vector<lldb::addr_t> &args,
174 lldb::addr_t struct_address,
175 DiagnosticManager &diagnostic_manager) override;
177 llvm::Optional<lldb::LanguageType> GetLanguageForExpr(
178 DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx);
179 bool SetupPersistentState(DiagnosticManager &diagnostic_manager,
180 ExecutionContext &exe_ctx);
181 bool PrepareForParsing(DiagnosticManager &diagnostic_manager,
182 ExecutionContext &exe_ctx);
184 ClangUserExpressionHelper m_type_system_helper;
186 class ResultDelegate : public Materializer::PersistentVariableDelegate {
188 ResultDelegate(lldb::TargetSP target) : m_target_sp(target) {}
189 ConstString GetName() override;
190 void DidDematerialize(lldb::ExpressionVariableSP &variable) override;
192 void RegisterPersistentState(PersistentExpressionState *persistent_state);
193 lldb::ExpressionVariableSP &GetVariable();
196 PersistentExpressionState *m_persistent_state;
197 lldb::ExpressionVariableSP m_variable;
198 lldb::TargetSP m_target_sp;
201 ResultDelegate m_result_delegate;
204 } // namespace lldb_private
206 #endif // liblldb_ClangUserExpression_h_