LLDB  mainline
ClangExpressionVariable.h
Go to the documentation of this file.
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 LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGEXPRESSIONVARIABLE_H
10 #define LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGEXPRESSIONVARIABLE_H
11 
12 #include <csignal>
13 #include <cstdint>
14 #include <cstring>
15 
16 #include <map>
17 #include <string>
18 #include <vector>
19 
20 #include "llvm/Support/Casting.h"
21 
22 #include "lldb/Core/Value.h"
26 #include "lldb/lldb-public.h"
27 
28 namespace llvm {
29 class Value;
30 }
31 
32 namespace clang {
33 class NamedDecl;
34 }
35 
36 namespace lldb_private {
37 
38 class ValueObjectConstResult;
39 
40 /// \class ClangExpressionVariable ClangExpressionVariable.h
41 /// "lldb/Expression/ClangExpressionVariable.h" Encapsulates one variable for
42 /// the expression parser.
43 ///
44 /// The expression parser uses variables in three different contexts:
45 ///
46 /// First, it stores persistent variables along with the process for use in
47 /// expressions. These persistent variables contain their own data and are
48 /// typed.
49 ///
50 /// Second, in an interpreted expression, it stores the local variables for
51 /// the expression along with the expression. These variables contain their
52 /// own data and are typed.
53 ///
54 /// Third, in a JIT-compiled expression, it stores the variables that the
55 /// expression needs to have materialized and dematerialized at each
56 /// execution. These do not contain their own data but are named and typed.
57 ///
58 /// This class supports all of these use cases using simple type polymorphism,
59 /// and provides necessary support methods. Its interface is RTTI-neutral.
61 public:
63  lldb::ByteOrder byte_order, uint32_t addr_byte_size);
64 
66  ConstString name, uint16_t flags = EVNone);
67 
68  ClangExpressionVariable(const lldb::ValueObjectSP &valobj_sp);
69 
71  ConstString name,
72  const TypeFromUser &user_type,
73  lldb::ByteOrder byte_order, uint32_t addr_byte_size);
74 
75  /// Utility functions for dealing with ExpressionVariableLists in Clang-
76  /// specific ways
77 
78  /// Finds a variable by NamedDecl in the list.
79  ///
80  /// \return
81  /// The variable requested, or NULL if that variable is not in the list.
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())) {
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:
120 
121  const clang::NamedDecl *m_named_decl =
122  nullptr; ///< The Decl corresponding to this variable
123  llvm::Value *m_llvm_value =
124  nullptr; ///< The IR value corresponding to this variable;
125  /// usually a GlobalValue
127  m_lldb_value; ///< The value found in LLDB for this variable
128  lldb::VariableSP m_lldb_var; ///< The original variable for this variable
130  nullptr; ///< The original symbol for this
131  /// variable, if it was a symbol
132  };
133 
134 private:
135  typedef std::map<uint64_t, ParserVars> ParserVarMap;
137 
138 public:
139  /// Make this variable usable by the parser by allocating space for parser-
140  /// specific variables
141  void EnableParserVars(uint64_t parser_id) {
142  m_parser_vars.insert(std::make_pair(parser_id, ParserVars()));
143  }
144 
145  /// Deallocate parser-specific variables
146  void DisableParserVars(uint64_t parser_id) { m_parser_vars.erase(parser_id); }
147 
148  /// Access parser-specific variables
149  ParserVars *GetParserVars(uint64_t parser_id) {
150  ParserVarMap::iterator i = m_parser_vars.find(parser_id);
151 
152  if (i == m_parser_vars.end())
153  return nullptr;
154  else
155  return &i->second;
156  }
157 
158  /// The following values are valid if the variable is used by JIT code
159  struct JITVars {
160  JITVars() = default;
161 
163  0; ///< The required alignment of the variable, in bytes
164  size_t m_size = 0; ///< The space required for the variable, in bytes
166  0; ///< The offset of the variable in the struct, in bytes
167  };
168 
169 private:
170  typedef std::map<uint64_t, JITVars> JITVarMap;
172 
173 public:
174  /// Make this variable usable for materializing for the JIT by allocating
175  /// space for JIT-specific variables
176  void EnableJITVars(uint64_t parser_id) {
177  m_jit_vars.insert(std::make_pair(parser_id, JITVars()));
178  }
179 
180  /// Deallocate JIT-specific variables
181  void DisableJITVars(uint64_t parser_id) { m_jit_vars.erase(parser_id); }
182 
183  JITVars *GetJITVars(uint64_t parser_id) {
184  JITVarMap::iterator i = m_jit_vars.find(parser_id);
185 
186  if (i == m_jit_vars.end())
187  return nullptr;
188  else
189  return &i->second;
190  }
191 
193 
194  // llvm casting support
195  static bool classof(const ExpressionVariable *ev) {
196  return ev->getKind() == ExpressionVariable::eKindClang;
197  }
198 
199  /// Members
202  operator=(const ClangExpressionVariable &) = delete;
203 };
204 
205 } // namespace lldb_private
206 
207 #endif // LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGEXPRESSIONVARIABLE_H
lldb_private::ExpressionVariableList::GetSize
size_t GetSize()
Implementation of methods in ExpressionVariableListBase.
Definition: ExpressionVariable.h:123
lldb_private::ClangExpressionVariable::ParserVars::ParserVars
ParserVars()
Definition: ClangExpressionVariable.h:119
lldb_private::ExecutionContext
Definition: ExecutionContext.h:292
lldb-public.h
llvm
Definition: Debugger.h:49
ExpressionVariable.h
lldb_private::ClangExpressionVariable::EnableJITVars
void EnableJITVars(uint64_t parser_id)
Make this variable usable for materializing for the JIT by allocating space for JIT-specific variable...
Definition: ClangExpressionVariable.h:176
lldb_private::ClangExpressionVariable::ParserVars
The following values should not live beyond parsing.
Definition: ClangExpressionVariable.h:117
lldb_private::ClangExpressionVariable::classof
static bool classof(const ExpressionVariable *ev)
Definition: ClangExpressionVariable.h:195
lldb_private::Value
Definition: Value.h:38
lldb_private::Symbol
Definition: Symbol.h:20
lldb_private::ClangExpressionVariable::ParserVarMap
std::map< uint64_t, ParserVars > ParserVarMap
Definition: ClangExpressionVariable.h:135
lldb_private::ClangExpressionVariable::PointValueAtData
bool PointValueAtData(Value &value, ExecutionContext *exe_ctx)
If the variable contains its own data, make a Value point at it.
lldb_private::ExpressionVariable::eKindClang
@ eKindClang
Definition: ExpressionVariable.h:27
lldb_private::ClangExpressionVariable
Definition: ClangExpressionVariable.h:60
lldb_private::ClangExpressionVariable::EnableParserVars
void EnableParserVars(uint64_t parser_id)
Make this variable usable by the parser by allocating space for parser- specific variables.
Definition: ClangExpressionVariable.h:141
lldb_private::ClangExpressionVariable::m_jit_vars
JITVarMap m_jit_vars
Definition: ClangExpressionVariable.h:171
lldb_private::ExpressionVariable::EVNone
@ EVNone
Definition: ExpressionVariable.h:81
lldb::offset_t
uint64_t offset_t
Definition: lldb-types.h:87
lldb_private::ClangExpressionVariable::JITVars::m_size
size_t m_size
The space required for the variable, in bytes.
Definition: ClangExpressionVariable.h:164
lldb_private::ClangExpressionVariable::operator=
const ClangExpressionVariable & operator=(const ClangExpressionVariable &)=delete
lldb_private::ExecutionContextScope
Definition: ExecutionContextScope.h:32
lldb_private::TaggedASTType< 1 >
lldb_private::ClangExpressionVariable::GetParserVars
ParserVars * GetParserVars(uint64_t parser_id)
Access parser-specific variables.
Definition: ClangExpressionVariable.h:149
lldb_private::ClangExpressionVariable::JITVars
The following values are valid if the variable is used by JIT code.
Definition: ClangExpressionVariable.h:159
lldb_private::ClangExpressionVariable::GetJITVars
JITVars * GetJITVars(uint64_t parser_id)
Definition: ClangExpressionVariable.h:183
lldb_private::ConstString
Definition: ConstString.h:40
lldb_private::ExpressionVariable::getKind
LLVMCastKind getKind() const
Definition: ExpressionVariable.h:29
lldb_private::ClangExpressionVariable::ClangExpressionVariable
ClangExpressionVariable(ExecutionContextScope *exe_scope, lldb::ByteOrder byte_order, uint32_t addr_byte_size)
Definition: ClangExpressionVariable.cpp:23
lldb_private::ExpressionVariableList
Definition: ExpressionVariable.h:120
lldb_private::ClangExpressionVariable::JITVars::m_alignment
lldb::offset_t m_alignment
The required alignment of the variable, in bytes.
Definition: ClangExpressionVariable.h:162
lldb_private::ClangExpressionVariable::ParserVars::m_named_decl
const clang::NamedDecl * m_named_decl
The Decl corresponding to this variable.
Definition: ClangExpressionVariable.h:121
lldb_private::ClangExpressionVariable::ParserVars::m_lldb_value
lldb_private::Value m_lldb_value
The value found in LLDB for this variable.
Definition: ClangExpressionVariable.h:127
lldb_private::ClangExpressionVariable::FindVariableInList
static ClangExpressionVariable * FindVariableInList(ExpressionVariableList &list, const clang::NamedDecl *decl, uint64_t parser_id)
Utility functions for dealing with ExpressionVariableLists in Clang- specific ways.
Definition: ClangExpressionVariable.h:83
lldb_private::ExpressionVariable
Definition: ExpressionVariable.h:23
uint32_t
lldb_private::ClangExpressionVariable::ParserVars::m_lldb_var
lldb::VariableSP m_lldb_var
The original variable for this variable.
Definition: ClangExpressionVariable.h:128
TaggedASTType.h
uint16_t
clang
Definition: ASTResultSynthesizer.h:15
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::ClangExpressionVariable::m_parser_vars
ParserVarMap m_parser_vars
Definition: ClangExpressionVariable.h:136
lldb_private::ExpressionVariableList::GetVariableAtIndex
lldb::ExpressionVariableSP GetVariableAtIndex(size_t index)
Definition: ExpressionVariable.h:125
lldb_private::ClangExpressionVariable::ParserVars::m_llvm_value
llvm::Value * m_llvm_value
The IR value corresponding to this variable; usually a GlobalValue.
Definition: ClangExpressionVariable.h:123
ConstString.h
lldb_private::ClangExpressionVariable::JITVarMap
std::map< uint64_t, JITVars > JITVarMap
Definition: ClangExpressionVariable.h:170
lldb_private::ClangExpressionVariable::JITVars::JITVars
JITVars()=default
lldb_private::ClangExpressionVariable::DisableJITVars
void DisableJITVars(uint64_t parser_id)
Deallocate JIT-specific variables.
Definition: ClangExpressionVariable.h:181
lldb_private::ClangExpressionVariable::DisableParserVars
void DisableParserVars(uint64_t parser_id)
Deallocate parser-specific variables.
Definition: ClangExpressionVariable.h:146
lldb_private::ClangExpressionVariable::GetTypeFromUser
TypeFromUser GetTypeFromUser()
Definition: ClangExpressionVariable.cpp:63
Value.h
lldb::ByteOrder
ByteOrder
Byte ordering definitions.
Definition: lldb-enumerations.h:138
lldb_private::ClangExpressionVariable::ParserVars::m_lldb_sym
const lldb_private::Symbol * m_lldb_sym
The original symbol for this variable, if it was a symbol.
Definition: ClangExpressionVariable.h:129
lldb_private::ClangExpressionVariable::JITVars::m_offset
lldb::offset_t m_offset
The offset of the variable in the struct, in bytes.
Definition: ClangExpressionVariable.h:165