1 //===-- ExpressionVariable.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_ExpressionVariable_h_
11 #define liblldb_ExpressionVariable_h_
18 // Other libraries and framework includes
19 #include "llvm/ADT/DenseMap.h"
22 #include "lldb/Core/ValueObject.h"
23 #include "lldb/Utility/ConstString.h"
24 #include "lldb/lldb-public.h"
26 namespace lldb_private {
28 class ClangExpressionVariable;
30 class ExpressionVariable
31 : public std::enable_shared_from_this<ExpressionVariable> {
33 //----------------------------------------------------------------------
34 // See TypeSystem.h for how to add subclasses to this.
35 //----------------------------------------------------------------------
36 enum LLVMCastKind { eKindClang, eKindSwift, eKindGo, kNumKinds };
38 LLVMCastKind getKind() const { return m_kind; }
40 ExpressionVariable(LLVMCastKind kind) : m_flags(0), m_kind(kind) {}
42 virtual ~ExpressionVariable();
44 size_t GetByteSize() { return m_frozen_sp->GetByteSize(); }
46 const ConstString &GetName() { return m_frozen_sp->GetName(); }
48 lldb::ValueObjectSP GetValueObject() { return m_frozen_sp; }
50 uint8_t *GetValueBytes();
52 void ValueUpdated() { m_frozen_sp->ValueUpdated(); }
54 RegisterInfo *GetRegisterInfo() {
55 return m_frozen_sp->GetValue().GetRegisterInfo();
58 void SetRegisterInfo(const RegisterInfo *reg_info) {
59 return m_frozen_sp->GetValue().SetContext(
60 Value::eContextTypeRegisterInfo, const_cast<RegisterInfo *>(reg_info));
63 CompilerType GetCompilerType() { return m_frozen_sp->GetCompilerType(); }
65 void SetCompilerType(const CompilerType &compiler_type) {
66 m_frozen_sp->GetValue().SetCompilerType(compiler_type);
69 void SetName(const ConstString &name) { m_frozen_sp->SetName(name); }
71 // this function is used to copy the address-of m_live_sp into m_frozen_sp
72 // this is necessary because the results of certain cast and pointer-
73 // arithmetic operations (such as those described in bugzilla issues 11588
74 // and 11618) generate frozen objects that do not have a valid address-of,
75 // which can be troublesome when using synthetic children providers.
76 // Transferring the address-of the live object solves these issues and
77 // provides the expected user-level behavior
78 void TransferAddress(bool force = false) {
79 if (m_live_sp.get() == nullptr)
82 if (m_frozen_sp.get() == nullptr)
85 if (force || (m_frozen_sp->GetLiveAddress() == LLDB_INVALID_ADDRESS))
86 m_frozen_sp->SetLiveAddress(m_live_sp->GetLiveAddress());
91 EVIsLLDBAllocated = 1 << 0, ///< This variable is resident in a location
92 ///specifically allocated for it by LLDB in the
94 EVIsProgramReference = 1 << 1, ///< This variable is a reference to a
95 ///(possibly invalid) area managed by the
97 EVNeedsAllocation = 1 << 2, ///< Space for this variable has yet to be
98 ///allocated in the target process
99 EVIsFreezeDried = 1 << 3, ///< This variable's authoritative version is in
100 ///m_frozen_sp (for example, for
101 ///statically-computed results)
103 1 << 4, ///< Copy from m_live_sp to m_frozen_sp during dematerialization
104 EVKeepInTarget = 1 << 5, ///< Keep the allocation after the expression is
105 ///complete rather than freeze drying its contents
107 EVTypeIsReference = 1 << 6, ///< The original type of this variable is a
108 ///reference, so materialize the value rather
110 EVUnknownType = 1 << 7, ///< This is a symbol of unknown type, and the type
111 ///must be resolved after parsing is complete
112 EVBareRegister = 1 << 8 ///< This variable is a direct reference to $pc or
113 ///some other entity.
116 typedef uint16_t FlagType;
118 FlagType m_flags; // takes elements of Flags
120 // these should be private
121 lldb::ValueObjectSP m_frozen_sp;
122 lldb::ValueObjectSP m_live_sp;
126 //----------------------------------------------------------------------
127 /// @class ExpressionVariableList ExpressionVariable.h
128 /// "lldb/Expression/ExpressionVariable.h"
129 /// A list of variable references.
131 /// This class stores variables internally, acting as the permanent store.
132 //----------------------------------------------------------------------
133 class ExpressionVariableList {
135 //----------------------------------------------------------------------
136 /// Implementation of methods in ExpressionVariableListBase
137 //----------------------------------------------------------------------
138 size_t GetSize() { return m_variables.size(); }
140 lldb::ExpressionVariableSP GetVariableAtIndex(size_t index) {
141 lldb::ExpressionVariableSP var_sp;
142 if (index < m_variables.size())
143 var_sp = m_variables[index];
147 size_t AddVariable(const lldb::ExpressionVariableSP &var_sp) {
148 m_variables.push_back(var_sp);
149 return m_variables.size() - 1;
152 lldb::ExpressionVariableSP
153 AddNewlyConstructedVariable(ExpressionVariable *var) {
154 lldb::ExpressionVariableSP var_sp(var);
155 m_variables.push_back(var_sp);
156 return m_variables.back();
159 bool ContainsVariable(const lldb::ExpressionVariableSP &var_sp) {
160 const size_t size = m_variables.size();
161 for (size_t index = 0; index < size; ++index) {
162 if (m_variables[index].get() == var_sp.get())
168 //----------------------------------------------------------------------
169 /// Finds a variable by name in the list.
172 /// The name of the requested variable.
175 /// The variable requested, or nullptr if that variable is not in the
177 //----------------------------------------------------------------------
178 lldb::ExpressionVariableSP GetVariable(const ConstString &name) {
179 lldb::ExpressionVariableSP var_sp;
180 for (size_t index = 0, size = GetSize(); index < size; ++index) {
181 var_sp = GetVariableAtIndex(index);
182 if (var_sp->GetName() == name)
189 lldb::ExpressionVariableSP GetVariable(llvm::StringRef name) {
193 for (size_t index = 0, size = GetSize(); index < size; ++index) {
194 auto var_sp = GetVariableAtIndex(index);
195 llvm::StringRef var_name_str = var_sp->GetName().GetStringRef();
196 if (var_name_str == name)
202 void RemoveVariable(lldb::ExpressionVariableSP var_sp) {
203 for (std::vector<lldb::ExpressionVariableSP>::iterator
204 vi = m_variables.begin(),
205 ve = m_variables.end();
207 if (vi->get() == var_sp.get()) {
208 m_variables.erase(vi);
214 void Clear() { m_variables.clear(); }
217 std::vector<lldb::ExpressionVariableSP> m_variables;
220 class PersistentExpressionState : public ExpressionVariableList {
222 //----------------------------------------------------------------------
223 // See TypeSystem.h for how to add subclasses to this.
224 //----------------------------------------------------------------------
225 enum LLVMCastKind { eKindClang, eKindSwift, eKindGo, kNumKinds };
227 LLVMCastKind getKind() const { return m_kind; }
229 PersistentExpressionState(LLVMCastKind kind) : m_kind(kind) {}
231 virtual ~PersistentExpressionState();
233 virtual lldb::ExpressionVariableSP
234 CreatePersistentVariable(const lldb::ValueObjectSP &valobj_sp) = 0;
236 virtual lldb::ExpressionVariableSP
237 CreatePersistentVariable(ExecutionContextScope *exe_scope,
238 const ConstString &name, const CompilerType &type,
239 lldb::ByteOrder byte_order,
240 uint32_t addr_byte_size) = 0;
242 /// Return a new persistent variable name with the specified prefix.
243 ConstString GetNextPersistentVariableName(Target &target,
244 llvm::StringRef prefix);
246 virtual llvm::StringRef
247 GetPersistentVariablePrefix(bool is_error = false) const = 0;
250 RemovePersistentVariable(lldb::ExpressionVariableSP variable) = 0;
252 virtual lldb::addr_t LookupSymbol(const ConstString &name);
254 void RegisterExecutionUnit(lldb::IRExecutionUnitSP &execution_unit_sp);
259 typedef std::set<lldb::IRExecutionUnitSP> ExecutionUnitSet;
261 m_execution_units; ///< The execution units that contain valuable symbols.
263 typedef llvm::DenseMap<const char *, lldb::addr_t> SymbolMap;
265 m_symbol_map; ///< The addresses of the symbols in m_execution_units.
268 } // namespace lldb_private
270 #endif // liblldb_ExpressionVariable_h_