ClangUserExpression.h
9.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
//===-- ClangUserExpression.h -----------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGUSEREXPRESSION_H
#define LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGUSEREXPRESSION_H
#include <vector>
#include "ASTResultSynthesizer.h"
#include "ASTStructExtractor.h"
#include "ClangExpressionDeclMap.h"
#include "ClangExpressionHelper.h"
#include "ClangExpressionSourceCode.h"
#include "ClangExpressionVariable.h"
#include "IRForTarget.h"
#include "lldb/Core/Address.h"
#include "lldb/Expression/LLVMUserExpression.h"
#include "lldb/Expression/Materializer.h"
#include "lldb/Target/ExecutionContext.h"
#include "lldb/lldb-forward.h"
#include "lldb/lldb-private.h"
namespace lldb_private {
/// \class ClangUserExpression ClangUserExpression.h
/// "lldb/Expression/ClangUserExpression.h" Encapsulates a single expression
/// for use with Clang
///
/// LLDB uses expressions for various purposes, notably to call functions
/// and as a backend for the expr command. ClangUserExpression encapsulates
/// the objects needed to parse and interpret or JIT an expression. It uses
/// the Clang parser to produce LLVM IR from the expression.
class ClangUserExpression : public LLVMUserExpression {
// LLVM RTTI support
static char ID;
public:
bool isA(const void *ClassID) const override {
return ClassID == &ID || LLVMUserExpression::isA(ClassID);
}
static bool classof(const Expression *obj) { return obj->isA(&ID); }
enum { kDefaultTimeout = 500000u };
class ClangUserExpressionHelper : public ClangExpressionHelper {
public:
ClangUserExpressionHelper(Target &target, bool top_level)
: m_target(target), m_top_level(top_level) {}
~ClangUserExpressionHelper() override = default;
/// Return the object that the parser should use when resolving external
/// values. May be NULL if everything should be self-contained.
ClangExpressionDeclMap *DeclMap() override {
return m_expr_decl_map_up.get();
}
void ResetDeclMap() { m_expr_decl_map_up.reset(); }
void ResetDeclMap(ExecutionContext &exe_ctx,
Materializer::PersistentVariableDelegate &result_delegate,
bool keep_result_in_memory,
ValueObject *ctx_obj);
/// Return the object that the parser should allow to access ASTs. May be
/// NULL if the ASTs do not need to be transformed.
///
/// \param[in] passthrough
/// The ASTConsumer that the returned transformer should send
/// the ASTs to after transformation.
clang::ASTConsumer *
ASTTransformer(clang::ASTConsumer *passthrough) override;
void CommitPersistentDecls() override;
private:
Target &m_target;
std::unique_ptr<ClangExpressionDeclMap> m_expr_decl_map_up;
std::unique_ptr<ASTStructExtractor> m_struct_extractor_up; ///< The class
///that generates
///the argument
///struct layout.
std::unique_ptr<ASTResultSynthesizer> m_result_synthesizer_up;
bool m_top_level;
};
/// Constructor
///
/// \param[in] expr
/// The expression to parse.
///
/// \param[in] prefix
/// If non-NULL, a C string containing translation-unit level
/// definitions to be included when the expression is parsed.
///
/// \param[in] language
/// If not eLanguageTypeUnknown, a language to use when parsing
/// the expression. Currently restricted to those languages
/// supported by Clang.
///
/// \param[in] desired_type
/// If not eResultTypeAny, the type to use for the expression
/// result.
///
/// \param[in] options
/// Additional options for the expression.
///
/// \param[in] ctx_obj
/// The object (if any) in which context the expression
/// must be evaluated. For details see the comment to
/// `UserExpression::Evaluate`.
ClangUserExpression(ExecutionContextScope &exe_scope, llvm::StringRef expr,
llvm::StringRef prefix, lldb::LanguageType language,
ResultType desired_type,
const EvaluateExpressionOptions &options,
ValueObject *ctx_obj);
~ClangUserExpression() override;
/// Parse the expression
///
/// \param[in] diagnostic_manager
/// A diagnostic manager to report parse errors and warnings to.
///
/// \param[in] exe_ctx
/// The execution context to use when looking up entities that
/// are needed for parsing (locations of functions, types of
/// variables, persistent variables, etc.)
///
/// \param[in] execution_policy
/// Determines whether interpretation is possible or mandatory.
///
/// \param[in] keep_result_in_memory
/// True if the resulting persistent variable should reside in
/// target memory, if applicable.
///
/// \return
/// True on success (no errors); false otherwise.
bool Parse(DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx,
lldb_private::ExecutionPolicy execution_policy,
bool keep_result_in_memory, bool generate_debug_info) override;
bool Complete(ExecutionContext &exe_ctx, CompletionRequest &request,
unsigned complete_pos) override;
ExpressionTypeSystemHelper *GetTypeSystemHelper() override {
return &m_type_system_helper;
}
ClangExpressionDeclMap *DeclMap() { return m_type_system_helper.DeclMap(); }
void ResetDeclMap() { m_type_system_helper.ResetDeclMap(); }
void ResetDeclMap(ExecutionContext &exe_ctx,
Materializer::PersistentVariableDelegate &result_delegate,
bool keep_result_in_memory) {
m_type_system_helper.ResetDeclMap(exe_ctx, result_delegate,
keep_result_in_memory,
m_ctx_obj);
}
lldb::ExpressionVariableSP
GetResultAfterDematerialization(ExecutionContextScope *exe_scope) override;
bool DidImportCxxModules() const { return m_imported_cpp_modules; }
private:
/// Populate m_in_cplusplus_method and m_in_objectivec_method based on the
/// environment.
void ScanContext(ExecutionContext &exe_ctx,
lldb_private::Status &err) override;
bool AddArguments(ExecutionContext &exe_ctx, std::vector<lldb::addr_t> &args,
lldb::addr_t struct_address,
DiagnosticManager &diagnostic_manager) override;
void CreateSourceCode(DiagnosticManager &diagnostic_manager,
ExecutionContext &exe_ctx,
std::vector<std::string> modules_to_import,
bool for_completion);
/// Defines how the current expression should be wrapped.
ClangExpressionSourceCode::WrapKind GetWrapKind() const;
bool SetupPersistentState(DiagnosticManager &diagnostic_manager,
ExecutionContext &exe_ctx);
bool PrepareForParsing(DiagnosticManager &diagnostic_manager,
ExecutionContext &exe_ctx, bool for_completion);
ClangUserExpressionHelper m_type_system_helper;
class ResultDelegate : public Materializer::PersistentVariableDelegate {
public:
ResultDelegate(lldb::TargetSP target) : m_target_sp(target) {}
ConstString GetName() override;
void DidDematerialize(lldb::ExpressionVariableSP &variable) override;
void RegisterPersistentState(PersistentExpressionState *persistent_state);
lldb::ExpressionVariableSP &GetVariable();
private:
PersistentExpressionState *m_persistent_state;
lldb::ExpressionVariableSP m_variable;
lldb::TargetSP m_target_sp;
};
/// The include directories that should be used when parsing the expression.
std::vector<std::string> m_include_directories;
/// The absolute character position in the transformed source code where the
/// user code (as typed by the user) starts. If the variable is empty, then we
/// were not able to calculate this position.
llvm::Optional<size_t> m_user_expression_start_pos;
ResultDelegate m_result_delegate;
ClangPersistentVariables *m_clang_state;
std::unique_ptr<ClangExpressionSourceCode> m_source_code;
/// File name used for the expression.
std::string m_filename;
/// The object (if any) in which context the expression is evaluated.
/// See the comment to `UserExpression::Evaluate` for details.
ValueObject *m_ctx_obj;
/// True iff this expression explicitly imported C++ modules.
bool m_imported_cpp_modules = false;
/// True if the expression parser should enforce the presence of a valid class
/// pointer in order to generate the expression as a method.
bool m_enforce_valid_object = true;
/// True if the expression is compiled as a C++ member function (true if it
/// was parsed when exe_ctx was in a C++ method).
bool m_in_cplusplus_method = false;
/// True if the expression is compiled as an Objective-C method (true if it
/// was parsed when exe_ctx was in an Objective-C method).
bool m_in_objectivec_method = false;
/// True if the expression is compiled as a static (or class) method
/// (currently true if it was parsed when exe_ctx was in an Objective-C class
/// method).
bool m_in_static_method = false;
/// True if "this" or "self" must be looked up and passed in. False if the
/// expression doesn't really use them and they can be NULL.
bool m_needs_object_ptr = false;
};
} // namespace lldb_private
#endif // LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGUSEREXPRESSION_H