]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/Expression/ExpressionVariable.h
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / include / lldb / Expression / ExpressionVariable.h
1 //===-- ExpressionVariable.h ------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef liblldb_ExpressionVariable_h_
11 #define liblldb_ExpressionVariable_h_
12
13 // C Includes
14 // C++ Includes
15 #include <memory>
16 #include <vector>
17
18 // Other libraries and framework includes
19 #include "llvm/ADT/DenseMap.h"
20
21 // Project includes
22 #include "lldb/Core/ValueObject.h"
23 #include "lldb/Utility/ConstString.h"
24 #include "lldb/lldb-public.h"
25
26 namespace lldb_private {
27
28 class ClangExpressionVariable;
29
30 class ExpressionVariable
31     : public std::enable_shared_from_this<ExpressionVariable> {
32 public:
33   //----------------------------------------------------------------------
34   // See TypeSystem.h for how to add subclasses to this.
35   //----------------------------------------------------------------------
36   enum LLVMCastKind { eKindClang, eKindSwift, eKindGo, kNumKinds };
37
38   LLVMCastKind getKind() const { return m_kind; }
39
40   ExpressionVariable(LLVMCastKind kind) : m_flags(0), m_kind(kind) {}
41
42   virtual ~ExpressionVariable();
43
44   size_t GetByteSize() { return m_frozen_sp->GetByteSize(); }
45
46   const ConstString &GetName() { return m_frozen_sp->GetName(); }
47
48   lldb::ValueObjectSP GetValueObject() { return m_frozen_sp; }
49
50   uint8_t *GetValueBytes();
51
52   void ValueUpdated() { m_frozen_sp->ValueUpdated(); }
53
54   RegisterInfo *GetRegisterInfo() {
55     return m_frozen_sp->GetValue().GetRegisterInfo();
56   }
57
58   void SetRegisterInfo(const RegisterInfo *reg_info) {
59     return m_frozen_sp->GetValue().SetContext(
60         Value::eContextTypeRegisterInfo, const_cast<RegisterInfo *>(reg_info));
61   }
62
63   CompilerType GetCompilerType() { return m_frozen_sp->GetCompilerType(); }
64
65   void SetCompilerType(const CompilerType &compiler_type) {
66     m_frozen_sp->GetValue().SetCompilerType(compiler_type);
67   }
68
69   void SetName(const ConstString &name) { m_frozen_sp->SetName(name); }
70
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)
80       return;
81
82     if (m_frozen_sp.get() == nullptr)
83       return;
84
85     if (force || (m_frozen_sp->GetLiveAddress() == LLDB_INVALID_ADDRESS))
86       m_frozen_sp->SetLiveAddress(m_live_sp->GetLiveAddress());
87   }
88
89   enum Flags {
90     EVNone = 0,
91     EVIsLLDBAllocated = 1 << 0, ///< This variable is resident in a location
92                                 ///specifically allocated for it by LLDB in the
93                                 ///target process
94     EVIsProgramReference = 1 << 1, ///< This variable is a reference to a
95                                    ///(possibly invalid) area managed by the
96                                    ///target program
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)
102     EVNeedsFreezeDry =
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
106                              ///and freeing it
107     EVTypeIsReference = 1 << 6, ///< The original type of this variable is a
108                                 ///reference, so materialize the value rather
109                                 ///than the location
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.
114   };
115
116   typedef uint16_t FlagType;
117
118   FlagType m_flags; // takes elements of Flags
119
120   // these should be private
121   lldb::ValueObjectSP m_frozen_sp;
122   lldb::ValueObjectSP m_live_sp;
123   LLVMCastKind m_kind;
124 };
125
126 //----------------------------------------------------------------------
127 /// @class ExpressionVariableList ExpressionVariable.h
128 /// "lldb/Expression/ExpressionVariable.h"
129 /// A list of variable references.
130 ///
131 /// This class stores variables internally, acting as the permanent store.
132 //----------------------------------------------------------------------
133 class ExpressionVariableList {
134 public:
135   //----------------------------------------------------------------------
136   /// Implementation of methods in ExpressionVariableListBase
137   //----------------------------------------------------------------------
138   size_t GetSize() { return m_variables.size(); }
139
140   lldb::ExpressionVariableSP GetVariableAtIndex(size_t index) {
141     lldb::ExpressionVariableSP var_sp;
142     if (index < m_variables.size())
143       var_sp = m_variables[index];
144     return var_sp;
145   }
146
147   size_t AddVariable(const lldb::ExpressionVariableSP &var_sp) {
148     m_variables.push_back(var_sp);
149     return m_variables.size() - 1;
150   }
151
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();
157   }
158
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())
163         return true;
164     }
165     return false;
166   }
167
168   //----------------------------------------------------------------------
169   /// Finds a variable by name in the list.
170   ///
171   /// @param[in] name
172   ///     The name of the requested variable.
173   ///
174   /// @return
175   ///     The variable requested, or nullptr if that variable is not in the
176   ///     list.
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)
183         return var_sp;
184     }
185     var_sp.reset();
186     return var_sp;
187   }
188
189   lldb::ExpressionVariableSP GetVariable(llvm::StringRef name) {
190     if (name.empty())
191       return nullptr;
192
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)
197         return var_sp;
198     }
199     return nullptr;
200   }
201
202   void RemoveVariable(lldb::ExpressionVariableSP var_sp) {
203     for (std::vector<lldb::ExpressionVariableSP>::iterator
204              vi = m_variables.begin(),
205              ve = m_variables.end();
206          vi != ve; ++vi) {
207       if (vi->get() == var_sp.get()) {
208         m_variables.erase(vi);
209         return;
210       }
211     }
212   }
213
214   void Clear() { m_variables.clear(); }
215
216 private:
217   std::vector<lldb::ExpressionVariableSP> m_variables;
218 };
219
220 class PersistentExpressionState : public ExpressionVariableList {
221 public:
222   //----------------------------------------------------------------------
223   // See TypeSystem.h for how to add subclasses to this.
224   //----------------------------------------------------------------------
225   enum LLVMCastKind { eKindClang, eKindSwift, eKindGo, kNumKinds };
226
227   LLVMCastKind getKind() const { return m_kind; }
228
229   PersistentExpressionState(LLVMCastKind kind) : m_kind(kind) {}
230
231   virtual ~PersistentExpressionState();
232
233   virtual lldb::ExpressionVariableSP
234   CreatePersistentVariable(const lldb::ValueObjectSP &valobj_sp) = 0;
235
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;
241
242   /// Return a new persistent variable name with the specified prefix.
243   ConstString GetNextPersistentVariableName(Target &target,
244                                             llvm::StringRef prefix);
245
246   virtual llvm::StringRef
247   GetPersistentVariablePrefix(bool is_error = false) const = 0;
248
249   virtual void
250   RemovePersistentVariable(lldb::ExpressionVariableSP variable) = 0;
251
252   virtual lldb::addr_t LookupSymbol(const ConstString &name);
253
254   void RegisterExecutionUnit(lldb::IRExecutionUnitSP &execution_unit_sp);
255
256 private:
257   LLVMCastKind m_kind;
258
259   typedef std::set<lldb::IRExecutionUnitSP> ExecutionUnitSet;
260   ExecutionUnitSet
261       m_execution_units; ///< The execution units that contain valuable symbols.
262
263   typedef llvm::DenseMap<const char *, lldb::addr_t> SymbolMap;
264   SymbolMap
265       m_symbol_map; ///< The addresses of the symbols in m_execution_units.
266 };
267
268 } // namespace lldb_private
269
270 #endif // liblldb_ExpressionVariable_h_