1 //===-- ClangExpressionDeclMap.h --------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef liblldb_ClangExpressionDeclMap_h_
10 #define liblldb_ClangExpressionDeclMap_h_
17 #include "ClangASTSource.h"
18 #include "ClangExpressionVariable.h"
20 #include "lldb/Core/ClangForward.h"
21 #include "lldb/Core/Value.h"
22 #include "lldb/Expression/Materializer.h"
23 #include "lldb/Symbol/SymbolContext.h"
24 #include "lldb/Symbol/TaggedASTType.h"
25 #include "lldb/Target/ExecutionContext.h"
26 #include "lldb/lldb-public.h"
27 #include "clang/AST/Decl.h"
28 #include "llvm/ADT/DenseMap.h"
30 namespace lldb_private {
32 /// \class ClangExpressionDeclMap ClangExpressionDeclMap.h
33 /// "lldb/Expression/ClangExpressionDeclMap.h" Manages named entities that are
34 /// defined in LLDB's debug information.
36 /// The Clang parser uses the ClangASTSource as an interface to request named
37 /// entities from outside an expression. The ClangASTSource reports back,
38 /// listing all possible objects corresponding to a particular name. But it
39 /// in turn relies on ClangExpressionDeclMap, which performs several important
42 /// First, it records what variables and functions were looked up and what
43 /// Decls were returned for them.
45 /// Second, it constructs a struct on behalf of IRForTarget, recording which
46 /// variables should be placed where and relaying this information back so
47 /// that IRForTarget can generate context-independent code.
49 /// Third, it "materializes" this struct on behalf of the expression command,
50 /// finding the current values of each variable and placing them into the
51 /// struct so that it can be passed to the JITted version of the IR.
53 /// Fourth and finally, it "dematerializes" the struct after the JITted code
54 /// has has executed, placing the new values back where it found the old ones.
55 class ClangExpressionDeclMap : public ClangASTSource {
59 /// Initializes class variables.
61 /// \param[in] keep_result_in_memory
62 /// If true, inhibits the normal deallocation of the memory for
63 /// the result persistent variable, and instead marks the variable
66 /// \param[in] delegate
67 /// If non-NULL, use this delegate to report result values. This
68 /// allows the client ClangUserExpression to report a result.
70 /// \param[in] exe_ctx
71 /// The execution context to use when parsing.
73 /// \param[in] ctx_obj
74 /// If not empty, then expression is evaluated in context of this object.
75 /// See the comment to `UserExpression::Evaluate` for details.
76 ClangExpressionDeclMap(
77 bool keep_result_in_memory,
78 Materializer::PersistentVariableDelegate *result_delegate,
79 ExecutionContext &exe_ctx,
80 ValueObject *ctx_obj);
83 ~ClangExpressionDeclMap() override;
85 /// Enable the state needed for parsing and IR transformation.
87 /// \param[in] exe_ctx
88 /// The execution context to use when finding types for variables.
89 /// Also used to find a "scratch" AST context to store result types.
91 /// \param[in] materializer
92 /// If non-NULL, the materializer to populate with information about
93 /// the variables to use
96 /// True if parsing is possible; false if it is unsafe to continue.
97 bool WillParse(ExecutionContext &exe_ctx, Materializer *materializer);
99 void InstallCodeGenerator(clang::ASTConsumer *code_gen);
101 /// [Used by ClangExpressionParser] For each variable that had an unknown
102 /// type at the beginning of parsing, determine its final type now.
105 /// True on success; false otherwise.
106 bool ResolveUnknownTypes();
108 /// Disable the state needed for parsing and IR transformation.
111 /// [Used by IRForTarget] Add a variable to the list of persistent
112 /// variables for the process.
115 /// The Clang declaration for the persistent variable, used for
116 /// lookup during parsing.
119 /// The name of the persistent variable, usually $something.
122 /// The type of the variable, in the Clang parser's context.
125 /// True on success; false otherwise.
126 bool AddPersistentVariable(const clang::NamedDecl *decl,
127 ConstString name, TypeFromParser type,
128 bool is_result, bool is_lvalue);
130 /// [Used by IRForTarget] Add a variable to the struct that needs to
131 /// be materialized each time the expression runs.
134 /// The Clang declaration for the variable.
137 /// The name of the variable.
140 /// The LLVM IR value for this variable.
143 /// The size of the variable in bytes.
145 /// \param[in] alignment
146 /// The required alignment of the variable in bytes.
149 /// True on success; false otherwise.
150 bool AddValueToStruct(const clang::NamedDecl *decl, ConstString name,
151 llvm::Value *value, size_t size,
152 lldb::offset_t alignment);
154 /// [Used by IRForTarget] Finalize the struct, laying out the position of
155 /// each object in it.
158 /// True on success; false otherwise.
159 bool DoStructLayout();
161 /// [Used by IRForTarget] Get general information about the laid-out struct
162 /// after DoStructLayout() has been called.
164 /// \param[out] num_elements
165 /// The number of elements in the struct.
168 /// The size of the struct, in bytes.
170 /// \param[out] alignment
171 /// The alignment of the struct, in bytes.
174 /// True if the information could be retrieved; false otherwise.
175 bool GetStructInfo(uint32_t &num_elements, size_t &size,
176 lldb::offset_t &alignment);
178 /// [Used by IRForTarget] Get specific information about one field of the
179 /// laid-out struct after DoStructLayout() has been called.
182 /// The parsed Decl for the field, as generated by ClangASTSource
183 /// on ClangExpressionDeclMap's behalf. In the case of the result
184 /// value, this will have the name $__lldb_result even if the
185 /// result value ends up having the name $1. This is an
186 /// implementation detail of IRForTarget.
188 /// \param[out] value
189 /// The IR value for the field (usually a GlobalVariable). In
190 /// the case of the result value, this will have the correct
191 /// name ($1, for instance). This is an implementation detail
194 /// \param[out] offset
195 /// The offset of the field from the beginning of the struct.
196 /// As long as the struct is aligned according to its required
197 /// alignment, this offset will align the field correctly.
200 /// The name of the field as used in materialization.
203 /// The index of the field about which information is requested.
206 /// True if the information could be retrieved; false otherwise.
207 bool GetStructElement(const clang::NamedDecl *&decl, llvm::Value *&value,
208 lldb::offset_t &offset, ConstString &name,
211 /// [Used by IRForTarget] Get information about a function given its Decl.
214 /// The parsed Decl for the Function, as generated by ClangASTSource
215 /// on ClangExpressionDeclMap's behalf.
218 /// The absolute address of the function in the target.
221 /// True if the information could be retrieved; false otherwise.
222 bool GetFunctionInfo(const clang::NamedDecl *decl, uint64_t &ptr);
224 /// [Used by IRForTarget] Get the address of a symbol given nothing but its
227 /// \param[in] target
228 /// The target to find the symbol in. If not provided,
229 /// then the current parsing context's Target.
231 /// \param[in] process
232 /// The process to use. For Objective-C symbols, the process's
233 /// Objective-C language runtime may be queried if the process
237 /// The name of the symbol.
239 /// \param[in] module
240 /// The module to limit the search to. This can be NULL
243 /// Valid load address for the symbol
244 lldb::addr_t GetSymbolAddress(Target &target, Process *process,
245 ConstString name, lldb::SymbolType symbol_type,
246 Module *module = nullptr);
248 lldb::addr_t GetSymbolAddress(ConstString name,
249 lldb::SymbolType symbol_type);
251 /// [Used by IRInterpreter] Get basic target information.
253 /// \param[out] byte_order
254 /// The byte order of the target.
256 /// \param[out] address_byte_size
257 /// The size of a pointer in bytes.
260 /// True if the information could be determined; false
263 lldb::ByteOrder byte_order;
264 size_t address_byte_size;
266 TargetInfo() : byte_order(lldb::eByteOrderInvalid), address_byte_size(0) {}
269 return (byte_order != lldb::eByteOrderInvalid && address_byte_size != 0);
272 TargetInfo GetTargetInfo();
274 /// [Used by ClangASTSource] Find all entities matching a given name, using
275 /// a NameSearchContext to make Decls for them.
277 /// \param[in] context
278 /// The NameSearchContext that can construct Decls for this name.
281 /// True on success; false otherwise.
282 void FindExternalVisibleDecls(NameSearchContext &context) override;
284 /// Find all entities matching a given name in a given module/namespace,
285 /// using a NameSearchContext to make Decls for them.
287 /// \param[in] context
288 /// The NameSearchContext that can construct Decls for this name.
290 /// \param[in] module
291 /// If non-NULL, the module to query.
293 /// \param[in] namespace_decl
294 /// If valid and module is non-NULL, the parent namespace.
296 /// \param[in] current_id
297 /// The ID for the current FindExternalVisibleDecls invocation,
298 /// for logging purposes.
301 /// True on success; false otherwise.
302 void FindExternalVisibleDecls(NameSearchContext &context,
303 lldb::ModuleSP module,
304 CompilerDeclContext &namespace_decl,
305 unsigned int current_id);
308 ExpressionVariableList
309 m_found_entities; ///< All entities that were looked up for the parser.
310 ExpressionVariableList
311 m_struct_members; ///< All entities that need to be placed in the struct.
312 bool m_keep_result_in_memory; ///< True if result persistent variables
313 ///generated by this expression should stay in
315 Materializer::PersistentVariableDelegate
316 *m_result_delegate; ///< If non-NULL, used to report expression results to
317 ///ClangUserExpression.
318 ValueObject *m_ctx_obj; ///< If not empty, then expression is
319 ///evaluated in context of this object.
320 ///For details see the comment to
321 ///`UserExpression::Evaluate`.
323 /// The following values should not live beyond parsing
328 Target *GetTarget() {
329 if (m_exe_ctx.GetTargetPtr())
330 return m_exe_ctx.GetTargetPtr();
331 else if (m_sym_ctx.target_sp)
332 m_sym_ctx.target_sp.get();
336 ExecutionContext m_exe_ctx; ///< The execution context to use when parsing.
337 SymbolContext m_sym_ctx; ///< The symbol context to use in finding variables
339 ClangPersistentVariables *m_persistent_vars =
340 nullptr; ///< The persistent variables for the process.
341 bool m_enable_lookups = false; ///< Set to true during parsing if we have
342 ///found the first "$__lldb" name.
343 TargetInfo m_target_info; ///< Basic information about the target.
344 Materializer *m_materializer = nullptr; ///< If non-NULL, the materializer
345 ///to use when reporting used
347 clang::ASTConsumer *m_code_gen = nullptr; ///< If non-NULL, a code generator
348 ///that receives new top-level
351 DISALLOW_COPY_AND_ASSIGN(ParserVars);
354 std::unique_ptr<ParserVars> m_parser_vars;
356 /// Activate parser-specific variables
357 void EnableParserVars() {
358 if (!m_parser_vars.get())
359 m_parser_vars = llvm::make_unique<ParserVars>();
362 /// Deallocate parser-specific variables
363 void DisableParserVars() { m_parser_vars.reset(); }
365 /// The following values contain layout information for the materialized
366 /// struct, but are not specific to a single materialization
369 : m_struct_alignment(0), m_struct_size(0), m_struct_laid_out(false),
370 m_result_name(), m_object_pointer_type(nullptr, nullptr) {}
373 m_struct_alignment; ///< The alignment of the struct in bytes.
374 size_t m_struct_size; ///< The size of the struct in bytes.
375 bool m_struct_laid_out; ///< True if the struct has been laid out and the
376 ///layout is valid (that is, no new fields have been
379 m_result_name; ///< The name of the result variable ($1, for example)
380 TypeFromUser m_object_pointer_type; ///< The type of the "this" variable, if
384 std::unique_ptr<StructVars> m_struct_vars;
386 /// Activate struct variables
387 void EnableStructVars() {
388 if (!m_struct_vars.get())
389 m_struct_vars.reset(new struct StructVars);
392 /// Deallocate struct variables
393 void DisableStructVars() { m_struct_vars.reset(); }
395 /// Get this parser's ID for use in extracting parser- and JIT-specific data
396 /// from persistent variables.
397 uint64_t GetParserID() { return (uint64_t) this; }
399 /// Given a target, find a variable that matches the given name and type.
401 /// \param[in] target
402 /// The target to use as a basis for finding the variable.
404 /// \param[in] module
405 /// If non-NULL, the module to search.
408 /// The name as a plain C string.
410 /// \param[in] namespace_decl
411 /// If non-NULL and module is non-NULL, the parent namespace.
414 /// The required type for the variable. This function may be called
415 /// during parsing, in which case we don't know its type; hence the
419 /// The LLDB Variable found, or NULL if none was found.
420 lldb::VariableSP FindGlobalVariable(Target &target, lldb::ModuleSP &module,
422 CompilerDeclContext *namespace_decl,
423 TypeFromUser *type = nullptr);
425 /// Get the value of a variable in a given execution context and return the
426 /// associated Types if needed.
429 /// The variable to evaluate.
431 /// \param[out] var_location
432 /// The variable location value to fill in
434 /// \param[out] found_type
435 /// The type of the found value, as it was found in the user process.
436 /// This is only useful when the variable is being inspected on behalf
437 /// of the parser, hence the default.
439 /// \param[out] parser_type
440 /// The type of the found value, as it was copied into the parser's
441 /// AST context. This is only useful when the variable is being
442 /// inspected on behalf of the parser, hence the default.
445 /// The Decl to be looked up.
448 /// Return true if the value was successfully filled in.
449 bool GetVariableValue(lldb::VariableSP &var,
450 lldb_private::Value &var_location,
451 TypeFromUser *found_type = nullptr,
452 TypeFromParser *parser_type = nullptr);
454 /// Use the NameSearchContext to generate a Decl for the given LLDB
455 /// Variable, and put it in the Tuple list.
457 /// \param[in] context
458 /// The NameSearchContext to use when constructing the Decl.
461 /// The LLDB Variable that needs a Decl.
463 /// \param[in] valobj
464 /// The LLDB ValueObject for that variable.
465 void AddOneVariable(NameSearchContext &context, lldb::VariableSP var,
466 lldb::ValueObjectSP valobj, unsigned int current_id);
468 /// Use the NameSearchContext to generate a Decl for the given persistent
469 /// variable, and put it in the list of found entities.
471 /// \param[in] context
472 /// The NameSearchContext to use when constructing the Decl.
475 /// The persistent variable that needs a Decl.
477 /// \param[in] current_id
478 /// The ID of the current invocation of FindExternalVisibleDecls
479 /// for logging purposes.
480 void AddOneVariable(NameSearchContext &context,
481 lldb::ExpressionVariableSP &pvar_sp,
482 unsigned int current_id);
484 /// Use the NameSearchContext to generate a Decl for the given LLDB symbol
485 /// (treated as a variable), and put it in the list of found entities.
487 /// \param[in] context
488 /// The NameSearchContext to use when constructing the Decl.
491 /// The LLDB Variable that needs a Decl.
492 void AddOneGenericVariable(NameSearchContext &context, const Symbol &symbol,
493 unsigned int current_id);
495 /// Use the NameSearchContext to generate a Decl for the given function.
496 /// (Functions are not placed in the Tuple list.) Can handle both fully
497 /// typed functions and generic functions.
499 /// \param[in] context
500 /// The NameSearchContext to use when constructing the Decl.
503 /// The Function that needs to be created. If non-NULL, this is
504 /// a fully-typed function.
507 /// The Symbol that corresponds to a function that needs to be
508 /// created with generic type (unitptr_t foo(...)).
509 void AddOneFunction(NameSearchContext &context, Function *fun, Symbol *sym,
510 unsigned int current_id);
512 /// Use the NameSearchContext to generate a Decl for the given register.
514 /// \param[in] context
515 /// The NameSearchContext to use when constructing the Decl.
517 /// \param[in] reg_info
518 /// The information corresponding to that register.
519 void AddOneRegister(NameSearchContext &context, const RegisterInfo *reg_info,
520 unsigned int current_id);
522 /// Use the NameSearchContext to generate a Decl for the given type. (Types
523 /// are not placed in the Tuple list.)
525 /// \param[in] context
526 /// The NameSearchContext to use when constructing the Decl.
529 /// The type that needs to be created.
530 void AddOneType(NameSearchContext &context, const TypeFromUser &type,
531 unsigned int current_id);
533 /// Generate a Decl for "*this" and add a member function declaration to it
534 /// for the expression, then report it.
536 /// \param[in] context
537 /// The NameSearchContext to use when constructing the Decl.
540 /// The type for *this.
541 void AddThisType(NameSearchContext &context, const TypeFromUser &type,
542 unsigned int current_id);
544 /// Move a type out of the current ASTContext into another, but make sure to
545 /// export all components of the type also.
547 /// \param[in] target
548 /// The ClangASTContext to move to.
549 /// \param[in] source
550 /// The ClangASTContext to move from. This is assumed to be going away.
551 /// \param[in] parser_type
552 /// The type as it appears in the source context.
555 /// Returns the moved type, or an empty type if there was a problem.
556 TypeFromUser DeportType(ClangASTContext &target, ClangASTContext &source,
557 TypeFromParser parser_type);
559 ClangASTContext *GetClangASTContext();
562 } // namespace lldb_private
564 #endif // liblldb_ClangExpressionDeclMap_h_