xref: /src/contrib/llvm-project/lldb/source/Symbol/Variable.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1cfca06d7SDimitry Andric //===-- Variable.cpp ------------------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste 
9f034231aSEd Maste #include "lldb/Symbol/Variable.h"
10f034231aSEd Maste 
11f034231aSEd Maste #include "lldb/Core/Module.h"
12f034231aSEd Maste #include "lldb/Core/ValueObject.h"
13f034231aSEd Maste #include "lldb/Core/ValueObjectVariable.h"
14f034231aSEd Maste #include "lldb/Symbol/Block.h"
1514f1b3e8SDimitry Andric #include "lldb/Symbol/CompileUnit.h"
16e81d9d49SDimitry Andric #include "lldb/Symbol/CompilerDecl.h"
17e81d9d49SDimitry Andric #include "lldb/Symbol/CompilerDeclContext.h"
18f034231aSEd Maste #include "lldb/Symbol/Function.h"
19f034231aSEd Maste #include "lldb/Symbol/SymbolContext.h"
20e81d9d49SDimitry Andric #include "lldb/Symbol/SymbolFile.h"
21f034231aSEd Maste #include "lldb/Symbol/Type.h"
22e81d9d49SDimitry Andric #include "lldb/Symbol/TypeSystem.h"
23f034231aSEd Maste #include "lldb/Symbol/VariableList.h"
24f034231aSEd Maste #include "lldb/Target/ABI.h"
25f034231aSEd Maste #include "lldb/Target/Process.h"
26f034231aSEd Maste #include "lldb/Target/RegisterContext.h"
27f034231aSEd Maste #include "lldb/Target/StackFrame.h"
28f034231aSEd Maste #include "lldb/Target/Target.h"
2914f1b3e8SDimitry Andric #include "lldb/Target/Thread.h"
30ac9a064cSDimitry Andric #include "lldb/Utility/LLDBLog.h"
31ac9a064cSDimitry Andric #include "lldb/Utility/Log.h"
3274a628f7SDimitry Andric #include "lldb/Utility/RegularExpression.h"
3374a628f7SDimitry Andric #include "lldb/Utility/Stream.h"
3414f1b3e8SDimitry Andric 
3514f1b3e8SDimitry Andric #include "llvm/ADT/Twine.h"
36f034231aSEd Maste 
37f034231aSEd Maste using namespace lldb;
38f034231aSEd Maste using namespace lldb_private;
39f034231aSEd Maste 
Variable(lldb::user_id_t uid,const char * name,const char * mangled,const lldb::SymbolFileTypeSP & symfile_type_sp,ValueType scope,SymbolContextScope * context,const RangeList & scope_range,Declaration * decl_ptr,const DWARFExpressionList & location_list,bool external,bool artificial,bool location_is_constant_data,bool static_member)40ead24645SDimitry Andric Variable::Variable(lldb::user_id_t uid, const char *name, const char *mangled,
41ead24645SDimitry Andric                    const lldb::SymbolFileTypeSP &symfile_type_sp,
42ead24645SDimitry Andric                    ValueType scope, SymbolContextScope *context,
43ead24645SDimitry Andric                    const RangeList &scope_range, Declaration *decl_ptr,
441f917f69SDimitry Andric                    const DWARFExpressionList &location_list, bool external,
45b60736ecSDimitry Andric                    bool artificial, bool location_is_constant_data,
46b60736ecSDimitry Andric                    bool static_member)
4714f1b3e8SDimitry Andric     : UserID(uid), m_name(name), m_mangled(ConstString(mangled)),
4814f1b3e8SDimitry Andric       m_symfile_type_sp(symfile_type_sp), m_scope(scope),
4914f1b3e8SDimitry Andric       m_owner_scope(context), m_scope_range(scope_range),
501f917f69SDimitry Andric       m_declaration(decl_ptr), m_location_list(location_list), m_external(external),
51b60736ecSDimitry Andric       m_artificial(artificial), m_loc_is_const_data(location_is_constant_data),
5294994d37SDimitry Andric       m_static_member(static_member) {}
53f034231aSEd Maste 
54344a3780SDimitry Andric Variable::~Variable() = default;
55f034231aSEd Maste 
GetLanguage() const5614f1b3e8SDimitry Andric lldb::LanguageType Variable::GetLanguage() const {
57ead24645SDimitry Andric   lldb::LanguageType lang = m_mangled.GuessLanguage();
58ead24645SDimitry Andric   if (lang != lldb::eLanguageTypeUnknown)
59ead24645SDimitry Andric     return lang;
60ead24645SDimitry Andric 
61ead24645SDimitry Andric   if (auto *func = m_owner_scope->CalculateSymbolContextFunction()) {
62ead24645SDimitry Andric     if ((lang = func->GetLanguage()) != lldb::eLanguageTypeUnknown)
63ead24645SDimitry Andric       return lang;
64ead24645SDimitry Andric   } else if (auto *comp_unit =
65ead24645SDimitry Andric                  m_owner_scope->CalculateSymbolContextCompileUnit()) {
66ead24645SDimitry Andric     if ((lang = comp_unit->GetLanguage()) != lldb::eLanguageTypeUnknown)
67ead24645SDimitry Andric       return lang;
68ead24645SDimitry Andric   }
69ead24645SDimitry Andric 
70027f1c96SDimitry Andric   return lldb::eLanguageTypeUnknown;
71027f1c96SDimitry Andric }
72f034231aSEd Maste 
GetName() const7314f1b3e8SDimitry Andric ConstString Variable::GetName() const {
74cfca06d7SDimitry Andric   ConstString name = m_mangled.GetName();
755e95aa85SEd Maste   if (name)
765e95aa85SEd Maste     return name;
77f034231aSEd Maste   return m_name;
78f034231aSEd Maste }
79f034231aSEd Maste 
GetUnqualifiedName() const8014f1b3e8SDimitry Andric ConstString Variable::GetUnqualifiedName() const { return m_name; }
81e81d9d49SDimitry Andric 
NameMatches(ConstString name) const825f29bb8aSDimitry Andric bool Variable::NameMatches(ConstString name) const {
83027f1c96SDimitry Andric   if (m_name == name)
84027f1c96SDimitry Andric     return true;
85027f1c96SDimitry Andric   SymbolContext variable_sc;
86027f1c96SDimitry Andric   m_owner_scope->CalculateSymbolContext(&variable_sc);
87027f1c96SDimitry Andric 
88cfca06d7SDimitry Andric   return m_mangled.NameMatches(name);
89027f1c96SDimitry Andric }
NameMatches(const RegularExpression & regex) const9014f1b3e8SDimitry Andric bool Variable::NameMatches(const RegularExpression &regex) const {
91f034231aSEd Maste   if (regex.Execute(m_name.AsCString()))
92f034231aSEd Maste     return true;
93027f1c96SDimitry Andric   if (m_mangled)
94cfca06d7SDimitry Andric     return m_mangled.NameMatches(regex);
95027f1c96SDimitry Andric   return false;
96f034231aSEd Maste }
97f034231aSEd Maste 
GetType()9814f1b3e8SDimitry Andric Type *Variable::GetType() {
99f034231aSEd Maste   if (m_symfile_type_sp)
100f034231aSEd Maste     return m_symfile_type_sp->GetType();
1010cac4ca3SEd Maste   return nullptr;
102f034231aSEd Maste }
103f034231aSEd Maste 
Dump(Stream * s,bool show_context) const10414f1b3e8SDimitry Andric void Variable::Dump(Stream *s, bool show_context) const {
1050cac4ca3SEd Maste   s->Printf("%p: ", static_cast<const void *>(this));
106f034231aSEd Maste   s->Indent();
107f034231aSEd Maste   *s << "Variable" << (const UserID &)*this;
108f034231aSEd Maste 
109f034231aSEd Maste   if (m_name)
110f034231aSEd Maste     *s << ", name = \"" << m_name << "\"";
111f034231aSEd Maste 
11214f1b3e8SDimitry Andric   if (m_symfile_type_sp) {
113f034231aSEd Maste     Type *type = m_symfile_type_sp->GetType();
11414f1b3e8SDimitry Andric     if (type) {
115706b4fc4SDimitry Andric       s->Format(", type = {{{0:x-16}} {1} (", type->GetID(), type);
116f034231aSEd Maste       type->DumpTypeName(s);
117f034231aSEd Maste       s->PutChar(')');
118f034231aSEd Maste     }
119f034231aSEd Maste   }
120f034231aSEd Maste 
12114f1b3e8SDimitry Andric   if (m_scope != eValueTypeInvalid) {
122f034231aSEd Maste     s->PutCString(", scope = ");
12314f1b3e8SDimitry Andric     switch (m_scope) {
12414f1b3e8SDimitry Andric     case eValueTypeVariableGlobal:
12514f1b3e8SDimitry Andric       s->PutCString(m_external ? "global" : "static");
12614f1b3e8SDimitry Andric       break;
127f3fbd1c0SDimitry Andric     case eValueTypeVariableArgument:
128f3fbd1c0SDimitry Andric       s->PutCString("parameter");
129f3fbd1c0SDimitry Andric       break;
13014f1b3e8SDimitry Andric     case eValueTypeVariableLocal:
13114f1b3e8SDimitry Andric       s->PutCString("local");
13214f1b3e8SDimitry Andric       break;
133f3fbd1c0SDimitry Andric     case eValueTypeVariableThreadLocal:
134f3fbd1c0SDimitry Andric       s->PutCString("thread local");
135f3fbd1c0SDimitry Andric       break;
13614f1b3e8SDimitry Andric     default:
137706b4fc4SDimitry Andric       s->AsRawOstream() << "??? (" << m_scope << ')';
138f034231aSEd Maste     }
139f034231aSEd Maste   }
140f034231aSEd Maste 
14114f1b3e8SDimitry Andric   if (show_context && m_owner_scope != nullptr) {
142f034231aSEd Maste     s->PutCString(", context = ( ");
143f034231aSEd Maste     m_owner_scope->DumpSymbolContext(s);
144f034231aSEd Maste     s->PutCString(" )");
145f034231aSEd Maste   }
146f034231aSEd Maste 
147f034231aSEd Maste   bool show_fullpaths = false;
148f034231aSEd Maste   m_declaration.Dump(s, show_fullpaths);
149f034231aSEd Maste 
1501f917f69SDimitry Andric   if (m_location_list.IsValid()) {
151f034231aSEd Maste     s->PutCString(", location = ");
15294994d37SDimitry Andric     ABISP abi;
15314f1b3e8SDimitry Andric     if (m_owner_scope) {
154f034231aSEd Maste       ModuleSP module_sp(m_owner_scope->CalculateSymbolContextModule());
155f034231aSEd Maste       if (module_sp)
15694994d37SDimitry Andric         abi = ABI::FindPlugin(ProcessSP(), module_sp->GetArchitecture());
157f034231aSEd Maste     }
1581f917f69SDimitry Andric     m_location_list.GetDescription(s, lldb::eDescriptionLevelBrief, abi.get());
159f034231aSEd Maste   }
160f034231aSEd Maste 
161f034231aSEd Maste   if (m_external)
162f034231aSEd Maste     s->PutCString(", external");
163f034231aSEd Maste 
164f034231aSEd Maste   if (m_artificial)
165f034231aSEd Maste     s->PutCString(", artificial");
166f034231aSEd Maste 
167f034231aSEd Maste   s->EOL();
168f034231aSEd Maste }
169f034231aSEd Maste 
DumpDeclaration(Stream * s,bool show_fullpaths,bool show_module)17014f1b3e8SDimitry Andric bool Variable::DumpDeclaration(Stream *s, bool show_fullpaths,
17114f1b3e8SDimitry Andric                                bool show_module) {
172f034231aSEd Maste   bool dumped_declaration_info = false;
17314f1b3e8SDimitry Andric   if (m_owner_scope) {
174f034231aSEd Maste     SymbolContext sc;
175f034231aSEd Maste     m_owner_scope->CalculateSymbolContext(&sc);
1760cac4ca3SEd Maste     sc.block = nullptr;
177f034231aSEd Maste     sc.line_entry.Clear();
178f034231aSEd Maste     bool show_inlined_frames = false;
179205afe67SEd Maste     const bool show_function_arguments = true;
1805e95aa85SEd Maste     const bool show_function_name = true;
181f034231aSEd Maste 
18214f1b3e8SDimitry Andric     dumped_declaration_info = sc.DumpStopContext(
18314f1b3e8SDimitry Andric         s, nullptr, Address(), show_fullpaths, show_module, show_inlined_frames,
18414f1b3e8SDimitry Andric         show_function_arguments, show_function_name);
185f034231aSEd Maste 
186f034231aSEd Maste     if (sc.function)
187f034231aSEd Maste       s->PutChar(':');
188f034231aSEd Maste   }
189f034231aSEd Maste   if (m_declaration.DumpStopContext(s, false))
190f034231aSEd Maste     dumped_declaration_info = true;
191f034231aSEd Maste   return dumped_declaration_info;
192f034231aSEd Maste }
193f034231aSEd Maste 
MemorySize() const19414f1b3e8SDimitry Andric size_t Variable::MemorySize() const { return sizeof(Variable); }
195f034231aSEd Maste 
GetDeclContext()19614f1b3e8SDimitry Andric CompilerDeclContext Variable::GetDeclContext() {
197e81d9d49SDimitry Andric   Type *type = GetType();
198f3fbd1c0SDimitry Andric   if (type)
199e81d9d49SDimitry Andric     return type->GetSymbolFile()->GetDeclContextContainingUID(GetID());
200f3fbd1c0SDimitry Andric   return CompilerDeclContext();
201e81d9d49SDimitry Andric }
202e81d9d49SDimitry Andric 
GetDecl()20314f1b3e8SDimitry Andric CompilerDecl Variable::GetDecl() {
204e81d9d49SDimitry Andric   Type *type = GetType();
205f3fbd1c0SDimitry Andric   return type ? type->GetSymbolFile()->GetDeclForUID(GetID()) : CompilerDecl();
206e81d9d49SDimitry Andric }
207f034231aSEd Maste 
CalculateSymbolContext(SymbolContext * sc)20814f1b3e8SDimitry Andric void Variable::CalculateSymbolContext(SymbolContext *sc) {
20914f1b3e8SDimitry Andric   if (m_owner_scope) {
210f034231aSEd Maste     m_owner_scope->CalculateSymbolContext(sc);
21112bd4897SEd Maste     sc->variable = this;
21214f1b3e8SDimitry Andric   } else
213f034231aSEd Maste     sc->Clear(false);
214f034231aSEd Maste }
215f034231aSEd Maste 
LocationIsValidForFrame(StackFrame * frame)21614f1b3e8SDimitry Andric bool Variable::LocationIsValidForFrame(StackFrame *frame) {
21714f1b3e8SDimitry Andric   if (frame) {
21814f1b3e8SDimitry Andric     Function *function =
21914f1b3e8SDimitry Andric         frame->GetSymbolContext(eSymbolContextFunction).function;
22014f1b3e8SDimitry Andric     if (function) {
221f034231aSEd Maste       TargetSP target_sp(frame->CalculateTarget());
222f034231aSEd Maste 
22314f1b3e8SDimitry Andric       addr_t loclist_base_load_addr =
22414f1b3e8SDimitry Andric           function->GetAddressRange().GetBaseAddress().GetLoadAddress(
22514f1b3e8SDimitry Andric               target_sp.get());
226f034231aSEd Maste       if (loclist_base_load_addr == LLDB_INVALID_ADDRESS)
227f034231aSEd Maste         return false;
228f73363f1SDimitry Andric       // It is a location list. We just need to tell if the location list
229f73363f1SDimitry Andric       // contains the current address when converted to a load address
2301f917f69SDimitry Andric       return m_location_list.ContainsAddress(
23114f1b3e8SDimitry Andric           loclist_base_load_addr,
232b1c73532SDimitry Andric           frame->GetFrameCodeAddressForSymbolication().GetLoadAddress(
233b1c73532SDimitry Andric               target_sp.get()));
234f034231aSEd Maste     }
235f034231aSEd Maste   }
236f034231aSEd Maste   return false;
237f034231aSEd Maste }
238f034231aSEd Maste 
LocationIsValidForAddress(const Address & address)23914f1b3e8SDimitry Andric bool Variable::LocationIsValidForAddress(const Address &address) {
240f73363f1SDimitry Andric   // Be sure to resolve the address to section offset prior to calling this
241f73363f1SDimitry Andric   // function.
24214f1b3e8SDimitry Andric   if (address.IsSectionOffset()) {
243145449b1SDimitry Andric     // We need to check if the address is valid for both scope range and value
244145449b1SDimitry Andric     // range.
245145449b1SDimitry Andric     // Empty scope range means block range.
246145449b1SDimitry Andric     bool valid_in_scope_range =
247145449b1SDimitry Andric         GetScopeRange().IsEmpty() || GetScopeRange().FindEntryThatContains(
248145449b1SDimitry Andric                                          address.GetFileAddress()) != nullptr;
249145449b1SDimitry Andric     if (!valid_in_scope_range)
250145449b1SDimitry Andric       return false;
251f034231aSEd Maste     SymbolContext sc;
252f034231aSEd Maste     CalculateSymbolContext(&sc);
25314f1b3e8SDimitry Andric     if (sc.module_sp == address.GetModule()) {
254f034231aSEd Maste       // Is the variable is described by a single location?
2551f917f69SDimitry Andric       if (m_location_list.IsAlwaysValidSingleExpr()) {
256f034231aSEd Maste         // Yes it is, the location is valid.
257f034231aSEd Maste         return true;
258f034231aSEd Maste       }
259f034231aSEd Maste 
26014f1b3e8SDimitry Andric       if (sc.function) {
26114f1b3e8SDimitry Andric         addr_t loclist_base_file_addr =
26214f1b3e8SDimitry Andric             sc.function->GetAddressRange().GetBaseAddress().GetFileAddress();
263f034231aSEd Maste         if (loclist_base_file_addr == LLDB_INVALID_ADDRESS)
264f034231aSEd Maste           return false;
265f73363f1SDimitry Andric         // It is a location list. We just need to tell if the location list
266f73363f1SDimitry Andric         // contains the current address when converted to a load address
2671f917f69SDimitry Andric         return m_location_list.ContainsAddress(loclist_base_file_addr,
268f034231aSEd Maste                                                address.GetFileAddress());
269f034231aSEd Maste       }
270f034231aSEd Maste     }
271f034231aSEd Maste   }
272f034231aSEd Maste   return false;
273f034231aSEd Maste }
274f034231aSEd Maste 
IsInScope(StackFrame * frame)27514f1b3e8SDimitry Andric bool Variable::IsInScope(StackFrame *frame) {
27614f1b3e8SDimitry Andric   switch (m_scope) {
277f034231aSEd Maste   case eValueTypeRegister:
278f034231aSEd Maste   case eValueTypeRegisterSet:
2790cac4ca3SEd Maste     return frame != nullptr;
280f034231aSEd Maste 
281f034231aSEd Maste   case eValueTypeConstResult:
282f034231aSEd Maste   case eValueTypeVariableGlobal:
283f034231aSEd Maste   case eValueTypeVariableStatic:
284f3fbd1c0SDimitry Andric   case eValueTypeVariableThreadLocal:
285f034231aSEd Maste     return true;
286f034231aSEd Maste 
287f034231aSEd Maste   case eValueTypeVariableArgument:
288f034231aSEd Maste   case eValueTypeVariableLocal:
28914f1b3e8SDimitry Andric     if (frame) {
290f73363f1SDimitry Andric       // We don't have a location list, we just need to see if the block that
291f73363f1SDimitry Andric       // this variable was defined in is currently
29214f1b3e8SDimitry Andric       Block *deepest_frame_block =
29314f1b3e8SDimitry Andric           frame->GetSymbolContext(eSymbolContextBlock).block;
29414f1b3e8SDimitry Andric       if (deepest_frame_block) {
295f034231aSEd Maste         SymbolContext variable_sc;
296f034231aSEd Maste         CalculateSymbolContext(&variable_sc);
297f3fbd1c0SDimitry Andric 
298f034231aSEd Maste         // Check for static or global variable defined at the compile unit
299f034231aSEd Maste         // level that wasn't defined in a block
3000cac4ca3SEd Maste         if (variable_sc.block == nullptr)
301f034231aSEd Maste           return true;
302f034231aSEd Maste 
303f3fbd1c0SDimitry Andric         // Check if the variable is valid in the current block
304f3fbd1c0SDimitry Andric         if (variable_sc.block != deepest_frame_block &&
305f3fbd1c0SDimitry Andric             !variable_sc.block->Contains(deepest_frame_block))
306f3fbd1c0SDimitry Andric           return false;
307f3fbd1c0SDimitry Andric 
30814f1b3e8SDimitry Andric         // If no scope range is specified then it means that the scope is the
309f73363f1SDimitry Andric         // same as the scope of the enclosing lexical block.
310f3fbd1c0SDimitry Andric         if (m_scope_range.IsEmpty())
311f034231aSEd Maste           return true;
312f3fbd1c0SDimitry Andric 
313f3fbd1c0SDimitry Andric         addr_t file_address = frame->GetFrameCodeAddress().GetFileAddress();
314f3fbd1c0SDimitry Andric         return m_scope_range.FindEntryThatContains(file_address) != nullptr;
315f034231aSEd Maste       }
316f034231aSEd Maste     }
317f034231aSEd Maste     break;
318f034231aSEd Maste 
319f034231aSEd Maste   default:
320f034231aSEd Maste     break;
321f034231aSEd Maste   }
322f034231aSEd Maste   return false;
323f034231aSEd Maste }
324f034231aSEd Maste 
GetValuesForVariableExpressionPath(llvm::StringRef variable_expr_path,ExecutionContextScope * scope,GetVariableCallback callback,void * baton,VariableList & variable_list,ValueObjectList & valobj_list)325b76161e4SDimitry Andric Status Variable::GetValuesForVariableExpressionPath(
32614f1b3e8SDimitry Andric     llvm::StringRef variable_expr_path, ExecutionContextScope *scope,
32714f1b3e8SDimitry Andric     GetVariableCallback callback, void *baton, VariableList &variable_list,
32814f1b3e8SDimitry Andric     ValueObjectList &valobj_list) {
329b76161e4SDimitry Andric   Status error;
33014f1b3e8SDimitry Andric   if (!callback || variable_expr_path.empty()) {
33114f1b3e8SDimitry Andric     error.SetErrorString("unknown error");
33214f1b3e8SDimitry Andric     return error;
33314f1b3e8SDimitry Andric   }
33414f1b3e8SDimitry Andric 
33514f1b3e8SDimitry Andric   switch (variable_expr_path.front()) {
336f034231aSEd Maste   case '*':
33714f1b3e8SDimitry Andric     error = Variable::GetValuesForVariableExpressionPath(
33814f1b3e8SDimitry Andric         variable_expr_path.drop_front(), scope, callback, baton, variable_list,
339f034231aSEd Maste         valobj_list);
34014f1b3e8SDimitry Andric     if (error.Fail()) {
34114f1b3e8SDimitry Andric       error.SetErrorString("unknown error");
34214f1b3e8SDimitry Andric       return error;
34314f1b3e8SDimitry Andric     }
34414f1b3e8SDimitry Andric     for (uint32_t i = 0; i < valobj_list.GetSize();) {
345b76161e4SDimitry Andric       Status tmp_error;
34614f1b3e8SDimitry Andric       ValueObjectSP valobj_sp(
34714f1b3e8SDimitry Andric           valobj_list.GetValueObjectAtIndex(i)->Dereference(tmp_error));
34814f1b3e8SDimitry Andric       if (tmp_error.Fail()) {
349f034231aSEd Maste         variable_list.RemoveVariableAtIndex(i);
350f034231aSEd Maste         valobj_list.RemoveValueObjectAtIndex(i);
35114f1b3e8SDimitry Andric       } else {
352f034231aSEd Maste         valobj_list.SetValueObjectAtIndex(i, valobj_sp);
353f034231aSEd Maste         ++i;
354f034231aSEd Maste       }
355f034231aSEd Maste     }
356f034231aSEd Maste     return error;
35714f1b3e8SDimitry Andric   case '&': {
35814f1b3e8SDimitry Andric     error = Variable::GetValuesForVariableExpressionPath(
35914f1b3e8SDimitry Andric         variable_expr_path.drop_front(), scope, callback, baton, variable_list,
360f034231aSEd Maste         valobj_list);
36114f1b3e8SDimitry Andric     if (error.Success()) {
36214f1b3e8SDimitry Andric       for (uint32_t i = 0; i < valobj_list.GetSize();) {
363b76161e4SDimitry Andric         Status tmp_error;
36414f1b3e8SDimitry Andric         ValueObjectSP valobj_sp(
36514f1b3e8SDimitry Andric             valobj_list.GetValueObjectAtIndex(i)->AddressOf(tmp_error));
36614f1b3e8SDimitry Andric         if (tmp_error.Fail()) {
367f034231aSEd Maste           variable_list.RemoveVariableAtIndex(i);
368f034231aSEd Maste           valobj_list.RemoveValueObjectAtIndex(i);
36914f1b3e8SDimitry Andric         } else {
370f034231aSEd Maste           valobj_list.SetValueObjectAtIndex(i, valobj_sp);
371f034231aSEd Maste           ++i;
372f034231aSEd Maste         }
373f034231aSEd Maste       }
37414f1b3e8SDimitry Andric     } else {
375f034231aSEd Maste       error.SetErrorString("unknown error");
376f034231aSEd Maste     }
377f034231aSEd Maste     return error;
37814f1b3e8SDimitry Andric   } break;
379f034231aSEd Maste 
38014f1b3e8SDimitry Andric   default: {
38114f1b3e8SDimitry Andric     static RegularExpression g_regex(
38214f1b3e8SDimitry Andric         llvm::StringRef("^([A-Za-z_:][A-Za-z_0-9:]*)(.*)"));
383ead24645SDimitry Andric     llvm::SmallVector<llvm::StringRef, 2> matches;
384f034231aSEd Maste     variable_list.Clear();
385ead24645SDimitry Andric     if (!g_regex.Execute(variable_expr_path, &matches)) {
3867fa27ce4SDimitry Andric       error.SetErrorStringWithFormatv(
3877fa27ce4SDimitry Andric           "unable to extract a variable name from '{0}'", variable_expr_path);
38814f1b3e8SDimitry Andric       return error;
38914f1b3e8SDimitry Andric     }
390ead24645SDimitry Andric     std::string variable_name = matches[1].str();
39114f1b3e8SDimitry Andric     if (!callback(baton, variable_name.c_str(), variable_list)) {
39214f1b3e8SDimitry Andric       error.SetErrorString("unknown error");
39314f1b3e8SDimitry Andric       return error;
39414f1b3e8SDimitry Andric     }
395f034231aSEd Maste     uint32_t i = 0;
39614f1b3e8SDimitry Andric     while (i < variable_list.GetSize()) {
397f034231aSEd Maste       VariableSP var_sp(variable_list.GetVariableAtIndex(i));
398f034231aSEd Maste       ValueObjectSP valobj_sp;
39914f1b3e8SDimitry Andric       if (!var_sp) {
40014f1b3e8SDimitry Andric         variable_list.RemoveVariableAtIndex(i);
40114f1b3e8SDimitry Andric         continue;
40214f1b3e8SDimitry Andric       }
40314f1b3e8SDimitry Andric       ValueObjectSP variable_valobj_sp(
40414f1b3e8SDimitry Andric           ValueObjectVariable::Create(scope, var_sp));
40514f1b3e8SDimitry Andric       if (!variable_valobj_sp) {
40614f1b3e8SDimitry Andric         variable_list.RemoveVariableAtIndex(i);
40714f1b3e8SDimitry Andric         continue;
40814f1b3e8SDimitry Andric       }
40914f1b3e8SDimitry Andric 
41014f1b3e8SDimitry Andric       llvm::StringRef variable_sub_expr_path =
41114f1b3e8SDimitry Andric           variable_expr_path.drop_front(variable_name.size());
41214f1b3e8SDimitry Andric       if (!variable_sub_expr_path.empty()) {
41314f1b3e8SDimitry Andric         valobj_sp = variable_valobj_sp->GetValueForExpressionPath(
414ef5d0b5eSDimitry Andric             variable_sub_expr_path);
41514f1b3e8SDimitry Andric         if (!valobj_sp) {
4167fa27ce4SDimitry Andric           error.SetErrorStringWithFormatv(
4177fa27ce4SDimitry Andric               "invalid expression path '{0}' for variable '{1}'",
4187fa27ce4SDimitry Andric               variable_sub_expr_path, var_sp->GetName().GetCString());
41914f1b3e8SDimitry Andric           variable_list.RemoveVariableAtIndex(i);
42014f1b3e8SDimitry Andric           continue;
421f034231aSEd Maste         }
42214f1b3e8SDimitry Andric       } else {
423f034231aSEd Maste         // Just the name of a variable with no extras
424f034231aSEd Maste         valobj_sp = variable_valobj_sp;
425f034231aSEd Maste       }
426f034231aSEd Maste 
427f034231aSEd Maste       valobj_list.Append(valobj_sp);
428f034231aSEd Maste       ++i;
429f034231aSEd Maste     }
430f034231aSEd Maste 
43114f1b3e8SDimitry Andric     if (variable_list.GetSize() > 0) {
432f034231aSEd Maste       error.Clear();
433f034231aSEd Maste       return error;
434f034231aSEd Maste     }
43514f1b3e8SDimitry Andric   } break;
436f034231aSEd Maste   }
437f034231aSEd Maste   error.SetErrorString("unknown error");
438f034231aSEd Maste   return error;
439f034231aSEd Maste }
440f034231aSEd Maste 
DumpLocations(Stream * s,const Address & address)441145449b1SDimitry Andric bool Variable::DumpLocations(Stream *s, const Address &address) {
442f034231aSEd Maste   SymbolContext sc;
443f034231aSEd Maste   CalculateSymbolContext(&sc);
44494994d37SDimitry Andric   ABISP abi;
44514f1b3e8SDimitry Andric   if (m_owner_scope) {
446f034231aSEd Maste     ModuleSP module_sp(m_owner_scope->CalculateSymbolContextModule());
447f034231aSEd Maste     if (module_sp)
44894994d37SDimitry Andric       abi = ABI::FindPlugin(ProcessSP(), module_sp->GetArchitecture());
449f034231aSEd Maste   }
450f034231aSEd Maste 
451f034231aSEd Maste   const addr_t file_addr = address.GetFileAddress();
45214f1b3e8SDimitry Andric   if (sc.function) {
45314f1b3e8SDimitry Andric     addr_t loclist_base_file_addr =
45414f1b3e8SDimitry Andric         sc.function->GetAddressRange().GetBaseAddress().GetFileAddress();
455f034231aSEd Maste     if (loclist_base_file_addr == LLDB_INVALID_ADDRESS)
456f034231aSEd Maste       return false;
4571f917f69SDimitry Andric     return m_location_list.DumpLocations(s, eDescriptionLevelBrief,
458145449b1SDimitry Andric                                          loclist_base_file_addr, file_addr,
45994994d37SDimitry Andric                                          abi.get());
460f034231aSEd Maste   }
461f034231aSEd Maste   return false;
462f034231aSEd Maste }
463f034231aSEd Maste 
46414f1b3e8SDimitry Andric static void PrivateAutoComplete(
46514f1b3e8SDimitry Andric     StackFrame *frame, llvm::StringRef partial_path,
46614f1b3e8SDimitry Andric     const llvm::Twine
46714f1b3e8SDimitry Andric         &prefix_path, // Anything that has been resolved already will be in here
468ead24645SDimitry Andric     const CompilerType &compiler_type, CompletionRequest &request);
469f034231aSEd Maste 
PrivateAutoCompleteMembers(StackFrame * frame,const std::string & partial_member_name,llvm::StringRef partial_path,const llvm::Twine & prefix_path,const CompilerType & compiler_type,CompletionRequest & request)47014f1b3e8SDimitry Andric static void PrivateAutoCompleteMembers(
47114f1b3e8SDimitry Andric     StackFrame *frame, const std::string &partial_member_name,
47214f1b3e8SDimitry Andric     llvm::StringRef partial_path,
47314f1b3e8SDimitry Andric     const llvm::Twine
47414f1b3e8SDimitry Andric         &prefix_path, // Anything that has been resolved already will be in here
475ead24645SDimitry Andric     const CompilerType &compiler_type, CompletionRequest &request) {
476f034231aSEd Maste 
477f034231aSEd Maste   // We are in a type parsing child members
478e81d9d49SDimitry Andric   const uint32_t num_bases = compiler_type.GetNumDirectBaseClasses();
479f034231aSEd Maste 
48014f1b3e8SDimitry Andric   if (num_bases > 0) {
48114f1b3e8SDimitry Andric     for (uint32_t i = 0; i < num_bases; ++i) {
48214f1b3e8SDimitry Andric       CompilerType base_class_type =
48314f1b3e8SDimitry Andric           compiler_type.GetDirectBaseClassAtIndex(i, nullptr);
484f034231aSEd Maste 
485ead24645SDimitry Andric       PrivateAutoCompleteMembers(frame, partial_member_name, partial_path,
486ead24645SDimitry Andric                                  prefix_path,
487ead24645SDimitry Andric                                  base_class_type.GetCanonicalType(), request);
488f034231aSEd Maste     }
489f034231aSEd Maste   }
490f034231aSEd Maste 
491e81d9d49SDimitry Andric   const uint32_t num_vbases = compiler_type.GetNumVirtualBaseClasses();
492f034231aSEd Maste 
49314f1b3e8SDimitry Andric   if (num_vbases > 0) {
49414f1b3e8SDimitry Andric     for (uint32_t i = 0; i < num_vbases; ++i) {
49514f1b3e8SDimitry Andric       CompilerType vbase_class_type =
49614f1b3e8SDimitry Andric           compiler_type.GetVirtualBaseClassAtIndex(i, nullptr);
497f034231aSEd Maste 
498ead24645SDimitry Andric       PrivateAutoCompleteMembers(frame, partial_member_name, partial_path,
499ead24645SDimitry Andric                                  prefix_path,
500ead24645SDimitry Andric                                  vbase_class_type.GetCanonicalType(), request);
501f034231aSEd Maste     }
502f034231aSEd Maste   }
503f034231aSEd Maste 
504f034231aSEd Maste   // We are in a type parsing child members
505e81d9d49SDimitry Andric   const uint32_t num_fields = compiler_type.GetNumFields();
506f034231aSEd Maste 
50714f1b3e8SDimitry Andric   if (num_fields > 0) {
50814f1b3e8SDimitry Andric     for (uint32_t i = 0; i < num_fields; ++i) {
509f034231aSEd Maste       std::string member_name;
510f034231aSEd Maste 
51114f1b3e8SDimitry Andric       CompilerType member_compiler_type = compiler_type.GetFieldAtIndex(
51214f1b3e8SDimitry Andric           i, member_name, nullptr, nullptr, nullptr);
513f034231aSEd Maste 
514ac9a064cSDimitry Andric       if (partial_member_name.empty()) {
515ac9a064cSDimitry Andric         request.AddCompletion((prefix_path + member_name).str());
516ac9a064cSDimitry Andric       } else if (llvm::StringRef(member_name)
517ac9a064cSDimitry Andric                      .starts_with(partial_member_name)) {
51814f1b3e8SDimitry Andric         if (member_name == partial_member_name) {
51914f1b3e8SDimitry Andric           PrivateAutoComplete(
52014f1b3e8SDimitry Andric               frame, partial_path,
52114f1b3e8SDimitry Andric               prefix_path + member_name, // Anything that has been resolved
52214f1b3e8SDimitry Andric                                          // already will be in here
523ead24645SDimitry Andric               member_compiler_type.GetCanonicalType(), request);
524ac9a064cSDimitry Andric         } else if (partial_path.empty()) {
525ead24645SDimitry Andric           request.AddCompletion((prefix_path + member_name).str());
526f034231aSEd Maste         }
527f034231aSEd Maste       }
528f034231aSEd Maste     }
529f034231aSEd Maste   }
530f034231aSEd Maste }
531f034231aSEd Maste 
PrivateAutoComplete(StackFrame * frame,llvm::StringRef partial_path,const llvm::Twine & prefix_path,const CompilerType & compiler_type,CompletionRequest & request)53214f1b3e8SDimitry Andric static void PrivateAutoComplete(
53314f1b3e8SDimitry Andric     StackFrame *frame, llvm::StringRef partial_path,
53414f1b3e8SDimitry Andric     const llvm::Twine
53514f1b3e8SDimitry Andric         &prefix_path, // Anything that has been resolved already will be in here
536ead24645SDimitry Andric     const CompilerType &compiler_type, CompletionRequest &request) {
53714f1b3e8SDimitry Andric   //    printf ("\nPrivateAutoComplete()\n\tprefix_path = '%s'\n\tpartial_path =
53814f1b3e8SDimitry Andric   //    '%s'\n", prefix_path.c_str(), partial_path.c_str());
539f034231aSEd Maste   std::string remaining_partial_path;
540f034231aSEd Maste 
541e81d9d49SDimitry Andric   const lldb::TypeClass type_class = compiler_type.GetTypeClass();
54214f1b3e8SDimitry Andric   if (partial_path.empty()) {
54314f1b3e8SDimitry Andric     if (compiler_type.IsValid()) {
54414f1b3e8SDimitry Andric       switch (type_class) {
545f034231aSEd Maste       default:
546f034231aSEd Maste       case eTypeClassArray:
547f034231aSEd Maste       case eTypeClassBlockPointer:
548f034231aSEd Maste       case eTypeClassBuiltin:
549f034231aSEd Maste       case eTypeClassComplexFloat:
550f034231aSEd Maste       case eTypeClassComplexInteger:
551f034231aSEd Maste       case eTypeClassEnumeration:
552f034231aSEd Maste       case eTypeClassFunction:
553f034231aSEd Maste       case eTypeClassMemberPointer:
554f034231aSEd Maste       case eTypeClassReference:
555f034231aSEd Maste       case eTypeClassTypedef:
55614f1b3e8SDimitry Andric       case eTypeClassVector: {
557ead24645SDimitry Andric         request.AddCompletion(prefix_path.str());
55814f1b3e8SDimitry Andric       } break;
559f034231aSEd Maste 
560f034231aSEd Maste       case eTypeClassClass:
561f034231aSEd Maste       case eTypeClassStruct:
562f034231aSEd Maste       case eTypeClassUnion:
56314f1b3e8SDimitry Andric         if (prefix_path.str().back() != '.')
564ead24645SDimitry Andric           request.AddCompletion((prefix_path + ".").str());
565f034231aSEd Maste         break;
566f034231aSEd Maste 
567f034231aSEd Maste       case eTypeClassObjCObject:
568f034231aSEd Maste       case eTypeClassObjCInterface:
569f034231aSEd Maste         break;
570f034231aSEd Maste       case eTypeClassObjCObjectPointer:
57114f1b3e8SDimitry Andric       case eTypeClassPointer: {
572f034231aSEd Maste         bool omit_empty_base_classes = true;
573ac9a064cSDimitry Andric         if (llvm::expectedToStdOptional(
574ac9a064cSDimitry Andric                 compiler_type.GetNumChildren(omit_empty_base_classes, nullptr))
575ac9a064cSDimitry Andric                 .value_or(0))
576ead24645SDimitry Andric           request.AddCompletion((prefix_path + "->").str());
57714f1b3e8SDimitry Andric         else {
578ead24645SDimitry Andric           request.AddCompletion(prefix_path.str());
579f034231aSEd Maste         }
58014f1b3e8SDimitry Andric       } break;
581f034231aSEd Maste       }
58214f1b3e8SDimitry Andric     } else {
58314f1b3e8SDimitry Andric       if (frame) {
584f034231aSEd Maste         const bool get_file_globals = true;
585f034231aSEd Maste 
586e3b55780SDimitry Andric         VariableList *variable_list = frame->GetVariableList(get_file_globals,
587e3b55780SDimitry Andric                                                              nullptr);
588f034231aSEd Maste 
58914f1b3e8SDimitry Andric         if (variable_list) {
590706b4fc4SDimitry Andric           for (const VariableSP &var_sp : *variable_list)
591706b4fc4SDimitry Andric             request.AddCompletion(var_sp->GetName().AsCString());
592f034231aSEd Maste         }
593f034231aSEd Maste       }
594f034231aSEd Maste     }
59514f1b3e8SDimitry Andric   } else {
596f034231aSEd Maste     const char ch = partial_path[0];
59714f1b3e8SDimitry Andric     switch (ch) {
598f034231aSEd Maste     case '*':
59914f1b3e8SDimitry Andric       if (prefix_path.str().empty()) {
60014f1b3e8SDimitry Andric         PrivateAutoComplete(frame, partial_path.substr(1), "*", compiler_type,
601ead24645SDimitry Andric                             request);
602f034231aSEd Maste       }
603f034231aSEd Maste       break;
604f034231aSEd Maste 
605f034231aSEd Maste     case '&':
60614f1b3e8SDimitry Andric       if (prefix_path.isTriviallyEmpty()) {
60714f1b3e8SDimitry Andric         PrivateAutoComplete(frame, partial_path.substr(1), std::string("&"),
608ead24645SDimitry Andric                             compiler_type, request);
609f034231aSEd Maste       }
610f034231aSEd Maste       break;
611f034231aSEd Maste 
612f034231aSEd Maste     case '-':
61339be7ce2SDimitry Andric       if (partial_path.size() > 1 && partial_path[1] == '>' &&
61439be7ce2SDimitry Andric           !prefix_path.str().empty()) {
61514f1b3e8SDimitry Andric         switch (type_class) {
61614f1b3e8SDimitry Andric         case lldb::eTypeClassPointer: {
617e81d9d49SDimitry Andric           CompilerType pointee_type(compiler_type.GetPointeeType());
61839be7ce2SDimitry Andric           if (partial_path.size() > 2 && partial_path[2]) {
619f034231aSEd Maste             // If there is more after the "->", then search deeper
620ead24645SDimitry Andric             PrivateAutoComplete(frame, partial_path.substr(2),
621ead24645SDimitry Andric                                 prefix_path + "->",
622ead24645SDimitry Andric                                 pointee_type.GetCanonicalType(), request);
62314f1b3e8SDimitry Andric           } else {
624f034231aSEd Maste             // Nothing after the "->", so list all members
62514f1b3e8SDimitry Andric             PrivateAutoCompleteMembers(
62614f1b3e8SDimitry Andric                 frame, std::string(), std::string(), prefix_path + "->",
627ead24645SDimitry Andric                 pointee_type.GetCanonicalType(), request);
628f034231aSEd Maste           }
62914f1b3e8SDimitry Andric         } break;
630f034231aSEd Maste         default:
631f034231aSEd Maste           break;
632f034231aSEd Maste         }
633f034231aSEd Maste       }
634f034231aSEd Maste       break;
635f034231aSEd Maste 
636f034231aSEd Maste     case '.':
63714f1b3e8SDimitry Andric       if (compiler_type.IsValid()) {
63814f1b3e8SDimitry Andric         switch (type_class) {
639f034231aSEd Maste         case lldb::eTypeClassUnion:
640f034231aSEd Maste         case lldb::eTypeClassStruct:
641f034231aSEd Maste         case lldb::eTypeClassClass:
64239be7ce2SDimitry Andric           if (partial_path.size() > 1 && partial_path[1]) {
643f034231aSEd Maste             // If there is more after the ".", then search deeper
64414f1b3e8SDimitry Andric             PrivateAutoComplete(frame, partial_path.substr(1),
645ead24645SDimitry Andric                                 prefix_path + ".", compiler_type, request);
646f034231aSEd Maste 
64714f1b3e8SDimitry Andric           } else {
648f034231aSEd Maste             // Nothing after the ".", so list all members
64914f1b3e8SDimitry Andric             PrivateAutoCompleteMembers(frame, std::string(), partial_path,
65014f1b3e8SDimitry Andric                                        prefix_path + ".", compiler_type,
651ead24645SDimitry Andric                                        request);
652f034231aSEd Maste           }
653f3fbd1c0SDimitry Andric           break;
654f034231aSEd Maste         default:
655f034231aSEd Maste           break;
656f034231aSEd Maste         }
657f034231aSEd Maste       }
658f034231aSEd Maste       break;
659f034231aSEd Maste     default:
66014f1b3e8SDimitry Andric       if (isalpha(ch) || ch == '_' || ch == '$') {
661f034231aSEd Maste         const size_t partial_path_len = partial_path.size();
662f034231aSEd Maste         size_t pos = 1;
66314f1b3e8SDimitry Andric         while (pos < partial_path_len) {
664f034231aSEd Maste           const char curr_ch = partial_path[pos];
66514f1b3e8SDimitry Andric           if (isalnum(curr_ch) || curr_ch == '_' || curr_ch == '$') {
666f034231aSEd Maste             ++pos;
667f034231aSEd Maste             continue;
668f034231aSEd Maste           }
669f034231aSEd Maste           break;
670f034231aSEd Maste         }
671f034231aSEd Maste 
672cfca06d7SDimitry Andric         std::string token(std::string(partial_path), 0, pos);
673cfca06d7SDimitry Andric         remaining_partial_path = std::string(partial_path.substr(pos));
674f034231aSEd Maste 
67514f1b3e8SDimitry Andric         if (compiler_type.IsValid()) {
67614f1b3e8SDimitry Andric           PrivateAutoCompleteMembers(frame, token, remaining_partial_path,
677ead24645SDimitry Andric                                      prefix_path, compiler_type, request);
67814f1b3e8SDimitry Andric         } else if (frame) {
679f034231aSEd Maste           // We haven't found our variable yet
680f034231aSEd Maste           const bool get_file_globals = true;
681f034231aSEd Maste 
68214f1b3e8SDimitry Andric           VariableList *variable_list =
683e3b55780SDimitry Andric               frame->GetVariableList(get_file_globals, nullptr);
684f034231aSEd Maste 
685866dcdacSEd Maste           if (!variable_list)
686866dcdacSEd Maste             break;
687866dcdacSEd Maste 
688706b4fc4SDimitry Andric           for (VariableSP var_sp : *variable_list) {
689866dcdacSEd Maste 
690706b4fc4SDimitry Andric             if (!var_sp)
691866dcdacSEd Maste               continue;
692866dcdacSEd Maste 
693706b4fc4SDimitry Andric             llvm::StringRef variable_name = var_sp->GetName().GetStringRef();
694312c0ed1SDimitry Andric             if (variable_name.starts_with(token)) {
695706b4fc4SDimitry Andric               if (variable_name == token) {
696706b4fc4SDimitry Andric                 Type *variable_type = var_sp->GetType();
69714f1b3e8SDimitry Andric                 if (variable_type) {
69814f1b3e8SDimitry Andric                   CompilerType variable_compiler_type(
69914f1b3e8SDimitry Andric                       variable_type->GetForwardCompilerType());
70014f1b3e8SDimitry Andric                   PrivateAutoComplete(
70114f1b3e8SDimitry Andric                       frame, remaining_partial_path,
70214f1b3e8SDimitry Andric                       prefix_path + token, // Anything that has been resolved
70314f1b3e8SDimitry Andric                                            // already will be in here
704ead24645SDimitry Andric                       variable_compiler_type.GetCanonicalType(), request);
70514f1b3e8SDimitry Andric                 } else {
706ead24645SDimitry Andric                   request.AddCompletion((prefix_path + variable_name).str());
707f034231aSEd Maste                 }
70814f1b3e8SDimitry Andric               } else if (remaining_partial_path.empty()) {
709ead24645SDimitry Andric                 request.AddCompletion((prefix_path + variable_name).str());
710f034231aSEd Maste               }
711f034231aSEd Maste             }
712f034231aSEd Maste           }
713f034231aSEd Maste         }
714f034231aSEd Maste       }
715f034231aSEd Maste       break;
716f034231aSEd Maste     }
717f034231aSEd Maste   }
718f034231aSEd Maste }
719f034231aSEd Maste 
AutoComplete(const ExecutionContext & exe_ctx,CompletionRequest & request)720ead24645SDimitry Andric void Variable::AutoComplete(const ExecutionContext &exe_ctx,
721f73363f1SDimitry Andric                             CompletionRequest &request) {
722e81d9d49SDimitry Andric   CompilerType compiler_type;
723f034231aSEd Maste 
724f73363f1SDimitry Andric   PrivateAutoComplete(exe_ctx.GetFramePtr(), request.GetCursorArgumentPrefix(),
725ead24645SDimitry Andric                       "", compiler_type, request);
726f034231aSEd Maste }
727