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