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/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 
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 DWARFExpression &location, bool external,
36            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 
45   const Declaration &GetDeclaration() const { return m_declaration; }
46 
47   ConstString GetName() const;
48 
49   ConstString GetUnqualifiedName() const;
50 
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 
66   lldb::ValueType GetScope() const { return m_scope; }
67 
68   const RangeList &GetScopeRange() const { return m_scope_range; }
69 
70   bool IsExternal() const { return m_external; }
71 
72   bool IsArtificial() const { return m_artificial; }
73 
74   bool IsStaticMember() const { return m_static_member; }
75 
76   DWARFExpression &LocationExpression() { return m_location; }
77 
78   const DWARFExpression &LocationExpression() const { return m_location; }
79 
80   bool DumpLocationForAddress(Stream *s, const Address &address);
81 
82   size_t MemorySize() const;
83 
84   void CalculateSymbolContext(SymbolContext *sc);
85 
86   bool IsInScope(StackFrame *frame);
87 
88   bool LocationIsValidForFrame(StackFrame *frame);
89 
90   bool LocationIsValidForAddress(const Address &address);
91 
92   bool GetLocationIsConstantValueData() const { return m_loc_is_const_data; }
93 
94   void SetLocationIsConstantValueData(bool b) { m_loc_is_const_data = b; }
95 
96   typedef size_t (*GetVariableCallback)(void *baton, const char *name,
97                                         VariableList &var_list);
98 
99   static Status GetValuesForVariableExpressionPath(
100       llvm::StringRef variable_expr_path, ExecutionContextScope *scope,
101       GetVariableCallback callback, void *baton, VariableList &variable_list,
102       ValueObjectList &valobj_list);
103 
104   static void AutoComplete(const ExecutionContext &exe_ctx,
105                            CompletionRequest &request);
106 
107   CompilerDeclContext GetDeclContext();
108 
109   CompilerDecl GetDecl();
110 
111 protected:
112   /// The basename of the variable (no namespaces).
113   ConstString m_name;
114   /// The mangled name of the variable.
115   Mangled m_mangled;
116   /// The type pointer of the variable (int, struct, class, etc)
117   /// global, parameter, local.
118   lldb::SymbolFileTypeSP m_symfile_type_sp;
119   lldb::ValueType m_scope;
120   /// The symbol file scope that this variable was defined in
121   SymbolContextScope *m_owner_scope;
122   /// The list of ranges inside the owner's scope where this variable
123   /// is valid.
124   RangeList m_scope_range;
125   /// Declaration location for this item.
126   Declaration m_declaration;
127   /// The location of this variable that can be fed to
128   /// DWARFExpression::Evaluate().
129   DWARFExpression m_location;
130   /// Visible outside the containing compile unit?
131   unsigned m_external : 1;
132   /// Non-zero if the variable is not explicitly declared in source.
133   unsigned m_artificial : 1;
134   /// The m_location expression contains the constant variable value
135   /// data, not a DWARF location.
136   unsigned m_loc_is_const_data : 1;
137   /// Non-zero if variable is static member of a class or struct.
138   unsigned m_static_member : 1;
139 
140 private:
141   Variable(const Variable &rhs) = delete;
142   Variable &operator=(const Variable &rhs) = delete;
143 };
144 
145 } // namespace lldb_private
146 
147 #endif // LLDB_SYMBOL_VARIABLE_H
148