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