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