1 //===-- ClangExpressionVariable.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_ClangExpressionVariable_h_
10 #define liblldb_ClangExpressionVariable_h_
20 #include "llvm/Support/Casting.h"
22 #include "lldb/Core/ClangForward.h"
23 #include "lldb/Core/Value.h"
24 #include "lldb/Expression/ExpressionVariable.h"
25 #include "lldb/Symbol/TaggedASTType.h"
26 #include "lldb/Utility/ConstString.h"
27 #include "lldb/lldb-public.h"
33 namespace lldb_private {
35 class ValueObjectConstResult;
37 /// \class ClangExpressionVariable ClangExpressionVariable.h
38 /// "lldb/Expression/ClangExpressionVariable.h" Encapsulates one variable for
39 /// the expression parser.
41 /// The expression parser uses variables in three different contexts:
43 /// First, it stores persistent variables along with the process for use in
44 /// expressions. These persistent variables contain their own data and are
47 /// Second, in an interpreted expression, it stores the local variables for
48 /// the expression along with the expression. These variables contain their
49 /// own data and are typed.
51 /// Third, in a JIT-compiled expression, it stores the variables that the
52 /// expression needs to have materialized and dematerialized at each
53 /// execution. These do not contain their own data but are named and typed.
55 /// This class supports all of these use cases using simple type polymorphism,
56 /// and provides necessary support methods. Its interface is RTTI-neutral.
57 class ClangExpressionVariable : public ExpressionVariable {
59 ClangExpressionVariable(ExecutionContextScope *exe_scope,
60 lldb::ByteOrder byte_order, uint32_t addr_byte_size);
62 ClangExpressionVariable(ExecutionContextScope *exe_scope, Value &value,
63 ConstString name, uint16_t flags = EVNone);
65 ClangExpressionVariable(const lldb::ValueObjectSP &valobj_sp);
67 ClangExpressionVariable(ExecutionContextScope *exe_scope,
69 const TypeFromUser &user_type,
70 lldb::ByteOrder byte_order, uint32_t addr_byte_size);
72 /// Utility functions for dealing with ExpressionVariableLists in Clang-
75 /// Finds a variable by NamedDecl in the list.
78 /// The name of the requested variable.
81 /// The variable requested, or NULL if that variable is not in the list.
82 static ClangExpressionVariable *
83 FindVariableInList(ExpressionVariableList &list, const clang::NamedDecl *decl,
85 lldb::ExpressionVariableSP var_sp;
86 for (size_t index = 0, size = list.GetSize(); index < size; ++index) {
87 var_sp = list.GetVariableAtIndex(index);
89 if (ClangExpressionVariable *clang_var =
90 llvm::dyn_cast<ClangExpressionVariable>(var_sp.get())) {
91 ClangExpressionVariable::ParserVars *parser_vars =
92 clang_var->GetParserVars(parser_id);
94 if (parser_vars && parser_vars->m_named_decl == decl)
101 /// If the variable contains its own data, make a Value point at it. If \a
102 /// exe_ctx in not NULL, the value will be resolved in with that execution
106 /// The value to point at the data.
108 /// \param[in] exe_ctx
109 /// The execution context to use to resolve \a value.
112 /// True on success; false otherwise (in particular, if this variable
113 /// does not contain its own data).
114 bool PointValueAtData(Value &value, ExecutionContext *exe_ctx);
116 /// The following values should not live beyond parsing
120 : m_parser_type(), m_named_decl(nullptr), m_llvm_value(nullptr),
121 m_lldb_value(), m_lldb_var(), m_lldb_sym(nullptr) {}
124 m_parser_type; ///< The type of the variable according to the parser
125 const clang::NamedDecl
126 *m_named_decl; ///< The Decl corresponding to this variable
127 llvm::Value *m_llvm_value; ///< The IR value corresponding to this variable;
128 ///usually a GlobalValue
130 m_lldb_value; ///< The value found in LLDB for this variable
131 lldb::VariableSP m_lldb_var; ///< The original variable for this variable
132 const lldb_private::Symbol *m_lldb_sym; ///< The original symbol for this
133 ///variable, if it was a symbol
137 typedef std::map<uint64_t, ParserVars> ParserVarMap;
138 ParserVarMap m_parser_vars;
141 /// Make this variable usable by the parser by allocating space for parser-
142 /// specific variables
143 void EnableParserVars(uint64_t parser_id) {
144 m_parser_vars.insert(std::make_pair(parser_id, ParserVars()));
147 /// Deallocate parser-specific variables
148 void DisableParserVars(uint64_t parser_id) { m_parser_vars.erase(parser_id); }
150 /// Access parser-specific variables
151 ParserVars *GetParserVars(uint64_t parser_id) {
152 ParserVarMap::iterator i = m_parser_vars.find(parser_id);
154 if (i == m_parser_vars.end())
160 /// The following values are valid if the variable is used by JIT code
162 JITVars() : m_alignment(0), m_size(0), m_offset(0) {}
165 m_alignment; ///< The required alignment of the variable, in bytes
166 size_t m_size; ///< The space required for the variable, in bytes
168 m_offset; ///< The offset of the variable in the struct, in bytes
172 typedef std::map<uint64_t, JITVars> JITVarMap;
173 JITVarMap m_jit_vars;
176 /// Make this variable usable for materializing for the JIT by allocating
177 /// space for JIT-specific variables
178 void EnableJITVars(uint64_t parser_id) {
179 m_jit_vars.insert(std::make_pair(parser_id, JITVars()));
182 /// Deallocate JIT-specific variables
183 void DisableJITVars(uint64_t parser_id) { m_jit_vars.erase(parser_id); }
185 JITVars *GetJITVars(uint64_t parser_id) {
186 JITVarMap::iterator i = m_jit_vars.find(parser_id);
188 if (i == m_jit_vars.end())
194 TypeFromUser GetTypeFromUser();
196 // llvm casting support
197 static bool classof(const ExpressionVariable *ev) {
198 return ev->getKind() == ExpressionVariable::eKindClang;
202 DISALLOW_COPY_AND_ASSIGN(ClangExpressionVariable);
205 } // namespace lldb_private
207 #endif // liblldb_ClangExpressionVariable_h_