]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionVariable.h
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Plugins / ExpressionParser / Clang / ClangExpressionVariable.h
1 //===-- ClangExpressionVariable.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_ClangExpressionVariable_h_
11 #define liblldb_ClangExpressionVariable_h_
12
13 #include <signal.h>
14 #include <stdint.h>
15 #include <string.h>
16
17 #include <map>
18 #include <string>
19 #include <vector>
20
21 #include "llvm/Support/Casting.h"
22
23 #include "lldb/Core/ClangForward.h"
24 #include "lldb/Core/Value.h"
25 #include "lldb/Expression/ExpressionVariable.h"
26 #include "lldb/Symbol/TaggedASTType.h"
27 #include "lldb/Utility/ConstString.h"
28 #include "lldb/lldb-public.h"
29
30 namespace llvm {
31 class Value;
32 }
33
34 namespace lldb_private {
35
36 class ValueObjectConstResult;
37
38 //----------------------------------------------------------------------
39 /// @class ClangExpressionVariable ClangExpressionVariable.h
40 /// "lldb/Expression/ClangExpressionVariable.h" Encapsulates one variable for
41 /// the expression parser.
42 ///
43 /// The expression parser uses variables in three different contexts:
44 ///
45 /// First, it stores persistent variables along with the process for use in
46 /// expressions.  These persistent variables contain their own data and are
47 /// typed.
48 ///
49 /// Second, in an interpreted expression, it stores the local variables for
50 /// the expression along with the expression.  These variables contain their
51 /// own data and are typed.
52 ///
53 /// Third, in a JIT-compiled expression, it stores the variables that the
54 /// expression needs to have materialized and dematerialized at each
55 /// execution.  These do not contain their own data but are named and typed.
56 ///
57 /// This class supports all of these use cases using simple type polymorphism,
58 /// and provides necessary support methods.  Its interface is RTTI-neutral.
59 //----------------------------------------------------------------------
60 class ClangExpressionVariable : public ExpressionVariable {
61 public:
62   ClangExpressionVariable(ExecutionContextScope *exe_scope,
63                           lldb::ByteOrder byte_order, uint32_t addr_byte_size);
64
65   ClangExpressionVariable(ExecutionContextScope *exe_scope, Value &value,
66                           const ConstString &name, uint16_t flags = EVNone);
67
68   ClangExpressionVariable(const lldb::ValueObjectSP &valobj_sp);
69
70   ClangExpressionVariable(ExecutionContextScope *exe_scope,
71                           const ConstString &name,
72                           const TypeFromUser &user_type,
73                           lldb::ByteOrder byte_order, uint32_t addr_byte_size);
74
75   //----------------------------------------------------------------------
76   /// Utility functions for dealing with ExpressionVariableLists in Clang-
77   /// specific ways
78   //----------------------------------------------------------------------
79
80   //----------------------------------------------------------------------
81   /// Finds a variable by NamedDecl in the list.
82   ///
83   /// @param[in] name
84   ///     The name of the requested variable.
85   ///
86   /// @return
87   ///     The variable requested, or NULL if that variable is not in the list.
88   //----------------------------------------------------------------------
89   static ClangExpressionVariable *
90   FindVariableInList(ExpressionVariableList &list, const clang::NamedDecl *decl,
91                      uint64_t parser_id) {
92     lldb::ExpressionVariableSP var_sp;
93     for (size_t index = 0, size = list.GetSize(); index < size; ++index) {
94       var_sp = list.GetVariableAtIndex(index);
95
96       if (ClangExpressionVariable *clang_var =
97               llvm::dyn_cast<ClangExpressionVariable>(var_sp.get())) {
98         ClangExpressionVariable::ParserVars *parser_vars =
99             clang_var->GetParserVars(parser_id);
100
101         if (parser_vars && parser_vars->m_named_decl == decl)
102           return clang_var;
103       }
104     }
105     return nullptr;
106   }
107
108   //----------------------------------------------------------------------
109   /// If the variable contains its own data, make a Value point at it. If \a
110   /// exe_ctx in not NULL, the value will be resolved in with that execution
111   /// context.
112   ///
113   /// @param[in] value
114   ///     The value to point at the data.
115   ///
116   /// @param[in] exe_ctx
117   ///     The execution context to use to resolve \a value.
118   ///
119   /// @return
120   ///     True on success; false otherwise (in particular, if this variable
121   ///     does not contain its own data).
122   //----------------------------------------------------------------------
123   bool PointValueAtData(Value &value, ExecutionContext *exe_ctx);
124
125   //----------------------------------------------------------------------
126   /// The following values should not live beyond parsing
127   //----------------------------------------------------------------------
128   class ParserVars {
129   public:
130     ParserVars()
131         : m_parser_type(), m_named_decl(NULL), m_llvm_value(NULL),
132           m_lldb_value(), m_lldb_var(), m_lldb_sym(NULL) {}
133
134     TypeFromParser
135         m_parser_type; ///< The type of the variable according to the parser
136     const clang::NamedDecl
137         *m_named_decl;         ///< The Decl corresponding to this variable
138     llvm::Value *m_llvm_value; ///< The IR value corresponding to this variable;
139                                ///usually a GlobalValue
140     lldb_private::Value
141         m_lldb_value;            ///< The value found in LLDB for this variable
142     lldb::VariableSP m_lldb_var; ///< The original variable for this variable
143     const lldb_private::Symbol *m_lldb_sym; ///< The original symbol for this
144                                             ///variable, if it was a symbol
145   };
146
147 private:
148   typedef std::map<uint64_t, ParserVars> ParserVarMap;
149   ParserVarMap m_parser_vars;
150
151 public:
152   //----------------------------------------------------------------------
153   /// Make this variable usable by the parser by allocating space for parser-
154   /// specific variables
155   //----------------------------------------------------------------------
156   void EnableParserVars(uint64_t parser_id) {
157     m_parser_vars.insert(std::make_pair(parser_id, ParserVars()));
158   }
159
160   //----------------------------------------------------------------------
161   /// Deallocate parser-specific variables
162   //----------------------------------------------------------------------
163   void DisableParserVars(uint64_t parser_id) { m_parser_vars.erase(parser_id); }
164
165   //----------------------------------------------------------------------
166   /// Access parser-specific variables
167   //----------------------------------------------------------------------
168   ParserVars *GetParserVars(uint64_t parser_id) {
169     ParserVarMap::iterator i = m_parser_vars.find(parser_id);
170
171     if (i == m_parser_vars.end())
172       return NULL;
173     else
174       return &i->second;
175   }
176
177   //----------------------------------------------------------------------
178   /// The following values are valid if the variable is used by JIT code
179   //----------------------------------------------------------------------
180   struct JITVars {
181     JITVars() : m_alignment(0), m_size(0), m_offset(0) {}
182
183     lldb::offset_t
184         m_alignment; ///< The required alignment of the variable, in bytes
185     size_t m_size;   ///< The space required for the variable, in bytes
186     lldb::offset_t
187         m_offset; ///< The offset of the variable in the struct, in bytes
188   };
189
190 private:
191   typedef std::map<uint64_t, JITVars> JITVarMap;
192   JITVarMap m_jit_vars;
193
194 public:
195   //----------------------------------------------------------------------
196   /// Make this variable usable for materializing for the JIT by allocating
197   /// space for JIT-specific variables
198   //----------------------------------------------------------------------
199   void EnableJITVars(uint64_t parser_id) {
200     m_jit_vars.insert(std::make_pair(parser_id, JITVars()));
201   }
202
203   //----------------------------------------------------------------------
204   /// Deallocate JIT-specific variables
205   //----------------------------------------------------------------------
206   void DisableJITVars(uint64_t parser_id) { m_jit_vars.erase(parser_id); }
207
208   JITVars *GetJITVars(uint64_t parser_id) {
209     JITVarMap::iterator i = m_jit_vars.find(parser_id);
210
211     if (i == m_jit_vars.end())
212       return NULL;
213     else
214       return &i->second;
215   }
216
217   TypeFromUser GetTypeFromUser();
218
219   //------------------------------------------------------------------
220   // llvm casting support
221   //------------------------------------------------------------------
222   static bool classof(const ExpressionVariable *ev) {
223     return ev->getKind() == ExpressionVariable::eKindClang;
224   }
225
226   //----------------------------------------------------------------------
227   /// Members
228   //----------------------------------------------------------------------
229   DISALLOW_COPY_AND_ASSIGN(ClangExpressionVariable);
230 };
231
232 } // namespace lldb_private
233
234 #endif // liblldb_ClangExpressionVariable_h_