1//===-- Variable.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_Variable_h_
10#define liblldb_Variable_h_
11
12#include "lldb/Core/Mangled.h"
13#include "lldb/Expression/DWARFExpression.h"
14#include "lldb/Symbol/Declaration.h"
15#include "lldb/Utility/CompletionRequest.h"
16#include "lldb/Utility/RangeMap.h"
17#include "lldb/Utility/UserID.h"
18#include "lldb/lldb-enumerations.h"
19#include "lldb/lldb-private.h"
20#include <memory>
21#include <vector>
22
23namespace lldb_private {
24
25class Variable : public UserID, public std::enable_shared_from_this<Variable> {
26public:
27  typedef RangeVector<lldb::addr_t, lldb::addr_t> RangeList;
28
29  /// Constructors and Destructors.
30  ///
31  /// \param mangled The mangled or fully qualified name of the variable.
32  Variable(lldb::user_id_t uid, const char *name, const char *mangled,
33           const lldb::SymbolFileTypeSP &symfile_type_sp, lldb::ValueType scope,
34           SymbolContextScope *owner_scope, const RangeList &scope_range,
35           Declaration *decl, const DWARFExpression &location, bool external,
36           bool artificial, bool static_member = false);
37
38  virtual ~Variable();
39
40  void Dump(Stream *s, bool show_context) const;
41
42  bool DumpDeclaration(Stream *s, bool show_fullpaths, bool show_module);
43
44  const Declaration &GetDeclaration() const { return m_declaration; }
45
46  ConstString GetName() const;
47
48  ConstString GetUnqualifiedName() const;
49
50  SymbolContextScope *GetSymbolContextScope() const { return m_owner_scope; }
51
52  /// Since a variable can have a basename "i" and also a mangled named
53  /// "_ZN12_GLOBAL__N_11iE" and a demangled mangled name "(anonymous
54  /// namespace)::i", this function will allow a generic match function that can
55  /// be called by commands and expression parsers to make sure we match
56  /// anything we come across.
57  bool NameMatches(ConstString name) const;
58
59  bool NameMatches(const RegularExpression &regex) const;
60
61  Type *GetType();
62
63  lldb::LanguageType GetLanguage() const;
64
65  lldb::ValueType GetScope() const { return m_scope; }
66
67  bool IsExternal() const { return m_external; }
68
69  bool IsArtificial() const { return m_artificial; }
70
71  bool IsStaticMember() const { return m_static_member; }
72
73  DWARFExpression &LocationExpression() { return m_location; }
74
75  const DWARFExpression &LocationExpression() const { return m_location; }
76
77  bool DumpLocationForAddress(Stream *s, const Address &address);
78
79  size_t MemorySize() const;
80
81  void CalculateSymbolContext(SymbolContext *sc);
82
83  bool IsInScope(StackFrame *frame);
84
85  bool LocationIsValidForFrame(StackFrame *frame);
86
87  bool LocationIsValidForAddress(const Address &address);
88
89  bool GetLocationIsConstantValueData() const { return m_loc_is_const_data; }
90
91  void SetLocationIsConstantValueData(bool b) { m_loc_is_const_data = b; }
92
93  typedef size_t (*GetVariableCallback)(void *baton, const char *name,
94                                        VariableList &var_list);
95
96  static Status GetValuesForVariableExpressionPath(
97      llvm::StringRef variable_expr_path, ExecutionContextScope *scope,
98      GetVariableCallback callback, void *baton, VariableList &variable_list,
99      ValueObjectList &valobj_list);
100
101  static void AutoComplete(const ExecutionContext &exe_ctx,
102                           CompletionRequest &request);
103
104  CompilerDeclContext GetDeclContext();
105
106  CompilerDecl GetDecl();
107
108protected:
109  /// The basename of the variable (no namespaces).
110  ConstString m_name;
111  /// The mangled name of the variable.
112  Mangled m_mangled;
113  /// The type pointer of the variable (int, struct, class, etc)
114  /// global, parameter, local.
115  lldb::SymbolFileTypeSP m_symfile_type_sp;
116  lldb::ValueType m_scope;
117  /// The symbol file scope that this variable was defined in
118  SymbolContextScope *m_owner_scope;
119  /// The list of ranges inside the owner's scope where this variable
120  /// is valid.
121  RangeList m_scope_range;
122  /// Declaration location for this item.
123  Declaration m_declaration;
124  /// The location of this variable that can be fed to
125  /// DWARFExpression::Evaluate().
126  DWARFExpression m_location;
127  /// Visible outside the containing compile unit?
128  unsigned m_external : 1;
129  /// Non-zero if the variable is not explicitly declared in source.
130  unsigned m_artificial : 1;
131  /// The m_location expression contains the constant variable value
132  /// data, not a DWARF location.
133  unsigned m_loc_is_const_data : 1;
134  /// Non-zero if variable is static member of a class or struct.
135  unsigned m_static_member : 1;
136
137private:
138  Variable(const Variable &rhs) = delete;
139  Variable &operator=(const Variable &rhs) = delete;
140};
141
142} // namespace lldb_private
143
144#endif // liblldb_Variable_h_
145