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_
16 #include "llvm/ADT/DenseMap.h"
18 #include "lldb/Core/ValueObject.h"
19 #include "lldb/Utility/ConstString.h"
20 #include "lldb/lldb-public.h"
22 namespace lldb_private {
24 class ClangExpressionVariable;
26 class ExpressionVariable
27 : public std::enable_shared_from_this<ExpressionVariable> {
29 //----------------------------------------------------------------------
30 // See TypeSystem.h for how to add subclasses to this.
31 //----------------------------------------------------------------------
32 enum LLVMCastKind { eKindClang, eKindSwift, eKindGo, kNumKinds };
34 LLVMCastKind getKind() const { return m_kind; }
36 ExpressionVariable(LLVMCastKind kind) : m_flags(0), m_kind(kind) {}
38 virtual ~ExpressionVariable();
40 size_t GetByteSize() { return m_frozen_sp->GetByteSize(); }
42 const ConstString &GetName() { return m_frozen_sp->GetName(); }
44 lldb::ValueObjectSP GetValueObject() { return m_frozen_sp; }
46 uint8_t *GetValueBytes();
48 void ValueUpdated() { m_frozen_sp->ValueUpdated(); }
50 RegisterInfo *GetRegisterInfo() {
51 return m_frozen_sp->GetValue().GetRegisterInfo();
54 void SetRegisterInfo(const RegisterInfo *reg_info) {
55 return m_frozen_sp->GetValue().SetContext(
56 Value::eContextTypeRegisterInfo, const_cast<RegisterInfo *>(reg_info));
59 CompilerType GetCompilerType() { return m_frozen_sp->GetCompilerType(); }
61 void SetCompilerType(const CompilerType &compiler_type) {
62 m_frozen_sp->GetValue().SetCompilerType(compiler_type);
65 void SetName(const ConstString &name) { m_frozen_sp->SetName(name); }
67 // this function is used to copy the address-of m_live_sp into m_frozen_sp
68 // this is necessary because the results of certain cast and pointer-
69 // arithmetic operations (such as those described in bugzilla issues 11588
70 // and 11618) generate frozen objects that do not have a valid address-of,
71 // which can be troublesome when using synthetic children providers.
72 // Transferring the address-of the live object solves these issues and
73 // provides the expected user-level behavior
74 void TransferAddress(bool force = false) {
75 if (m_live_sp.get() == nullptr)
78 if (m_frozen_sp.get() == nullptr)
81 if (force || (m_frozen_sp->GetLiveAddress() == LLDB_INVALID_ADDRESS))
82 m_frozen_sp->SetLiveAddress(m_live_sp->GetLiveAddress());
87 EVIsLLDBAllocated = 1 << 0, ///< This variable is resident in a location
88 ///specifically allocated for it by LLDB in the
90 EVIsProgramReference = 1 << 1, ///< This variable is a reference to a
91 ///(possibly invalid) area managed by the
93 EVNeedsAllocation = 1 << 2, ///< Space for this variable has yet to be
94 ///allocated in the target process
95 EVIsFreezeDried = 1 << 3, ///< This variable's authoritative version is in
96 ///m_frozen_sp (for example, for
97 ///statically-computed results)
99 1 << 4, ///< Copy from m_live_sp to m_frozen_sp during dematerialization
100 EVKeepInTarget = 1 << 5, ///< Keep the allocation after the expression is
101 ///complete rather than freeze drying its contents
103 EVTypeIsReference = 1 << 6, ///< The original type of this variable is a
104 ///reference, so materialize the value rather
106 EVUnknownType = 1 << 7, ///< This is a symbol of unknown type, and the type
107 ///must be resolved after parsing is complete
108 EVBareRegister = 1 << 8 ///< This variable is a direct reference to $pc or
109 ///some other entity.
112 typedef uint16_t FlagType;
114 FlagType m_flags; // takes elements of Flags
116 // these should be private
117 lldb::ValueObjectSP m_frozen_sp;
118 lldb::ValueObjectSP m_live_sp;
122 //----------------------------------------------------------------------
123 /// @class ExpressionVariableList ExpressionVariable.h
124 /// "lldb/Expression/ExpressionVariable.h"
125 /// A list of variable references.
127 /// This class stores variables internally, acting as the permanent store.
128 //----------------------------------------------------------------------
129 class ExpressionVariableList {
131 //----------------------------------------------------------------------
132 /// Implementation of methods in ExpressionVariableListBase
133 //----------------------------------------------------------------------
134 size_t GetSize() { return m_variables.size(); }
136 lldb::ExpressionVariableSP GetVariableAtIndex(size_t index) {
137 lldb::ExpressionVariableSP var_sp;
138 if (index < m_variables.size())
139 var_sp = m_variables[index];
143 size_t AddVariable(const lldb::ExpressionVariableSP &var_sp) {
144 m_variables.push_back(var_sp);
145 return m_variables.size() - 1;
148 lldb::ExpressionVariableSP
149 AddNewlyConstructedVariable(ExpressionVariable *var) {
150 lldb::ExpressionVariableSP var_sp(var);
151 m_variables.push_back(var_sp);
152 return m_variables.back();
155 bool ContainsVariable(const lldb::ExpressionVariableSP &var_sp) {
156 const size_t size = m_variables.size();
157 for (size_t index = 0; index < size; ++index) {
158 if (m_variables[index].get() == var_sp.get())
164 //----------------------------------------------------------------------
165 /// Finds a variable by name in the list.
168 /// The name of the requested variable.
171 /// The variable requested, or nullptr if that variable is not in the
173 //----------------------------------------------------------------------
174 lldb::ExpressionVariableSP GetVariable(const ConstString &name) {
175 lldb::ExpressionVariableSP var_sp;
176 for (size_t index = 0, size = GetSize(); index < size; ++index) {
177 var_sp = GetVariableAtIndex(index);
178 if (var_sp->GetName() == name)
185 lldb::ExpressionVariableSP GetVariable(llvm::StringRef name) {
189 for (size_t index = 0, size = GetSize(); index < size; ++index) {
190 auto var_sp = GetVariableAtIndex(index);
191 llvm::StringRef var_name_str = var_sp->GetName().GetStringRef();
192 if (var_name_str == name)
198 void RemoveVariable(lldb::ExpressionVariableSP var_sp) {
199 for (std::vector<lldb::ExpressionVariableSP>::iterator
200 vi = m_variables.begin(),
201 ve = m_variables.end();
203 if (vi->get() == var_sp.get()) {
204 m_variables.erase(vi);
210 void Clear() { m_variables.clear(); }
213 std::vector<lldb::ExpressionVariableSP> m_variables;
216 class PersistentExpressionState : public ExpressionVariableList {
218 //----------------------------------------------------------------------
219 // See TypeSystem.h for how to add subclasses to this.
220 //----------------------------------------------------------------------
221 enum LLVMCastKind { eKindClang, eKindSwift, eKindGo, kNumKinds };
223 LLVMCastKind getKind() const { return m_kind; }
225 PersistentExpressionState(LLVMCastKind kind) : m_kind(kind) {}
227 virtual ~PersistentExpressionState();
229 virtual lldb::ExpressionVariableSP
230 CreatePersistentVariable(const lldb::ValueObjectSP &valobj_sp) = 0;
232 virtual lldb::ExpressionVariableSP
233 CreatePersistentVariable(ExecutionContextScope *exe_scope,
234 const ConstString &name, const CompilerType &type,
235 lldb::ByteOrder byte_order,
236 uint32_t addr_byte_size) = 0;
238 /// Return a new persistent variable name with the specified prefix.
239 ConstString GetNextPersistentVariableName(Target &target,
240 llvm::StringRef prefix);
242 virtual llvm::StringRef
243 GetPersistentVariablePrefix(bool is_error = false) const = 0;
246 RemovePersistentVariable(lldb::ExpressionVariableSP variable) = 0;
248 virtual lldb::addr_t LookupSymbol(const ConstString &name);
250 void RegisterExecutionUnit(lldb::IRExecutionUnitSP &execution_unit_sp);
255 typedef std::set<lldb::IRExecutionUnitSP> ExecutionUnitSet;
257 m_execution_units; ///< The execution units that contain valuable symbols.
259 typedef llvm::DenseMap<const char *, lldb::addr_t> SymbolMap;
261 m_symbol_map; ///< The addresses of the symbols in m_execution_units.
264 } // namespace lldb_private
266 #endif // liblldb_ExpressionVariable_h_