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