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