blob: ffed48a33bf119ce620804f21eaa5a8898fe5443 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Variable.cpp --------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Symbol/Variable.h"
11
Greg Clayton1f746072012-08-29 21:13:06 +000012#include "lldb/Core/Module.h"
Greg Clayton884fb692011-07-08 21:46:14 +000013#include "lldb/Core/ValueObject.h"
14#include "lldb/Core/ValueObjectVariable.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include "lldb/Symbol/Block.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000016#include "lldb/Symbol/CompileUnit.h"
Paul Hermand628cbb2015-09-15 23:44:17 +000017#include "lldb/Symbol/CompilerDecl.h"
18#include "lldb/Symbol/CompilerDeclContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/Symbol/Function.h"
20#include "lldb/Symbol/SymbolContext.h"
Paul Hermand628cbb2015-09-15 23:44:17 +000021#include "lldb/Symbol/SymbolFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Symbol/Type.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000023#include "lldb/Symbol/TypeSystem.h"
Greg Clayton884fb692011-07-08 21:46:14 +000024#include "lldb/Symbol/VariableList.h"
Greg Claytonafacd142011-09-02 01:15:17 +000025#include "lldb/Target/ABI.h"
Greg Claytonf5e56de2010-09-14 23:36:40 +000026#include "lldb/Target/Process.h"
Greg Clayton9da7bd02010-08-24 21:05:24 +000027#include "lldb/Target/RegisterContext.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000028#include "lldb/Target/StackFrame.h"
Greg Claytonf5e56de2010-09-14 23:36:40 +000029#include "lldb/Target/Target.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000030#include "lldb/Target/Thread.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000031#include "lldb/Utility/RegularExpression.h"
32#include "lldb/Utility/Stream.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033
Zachary Turner4aa87532016-11-17 01:37:42 +000034#include "llvm/ADT/Twine.h"
35
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036using namespace lldb;
37using namespace lldb_private;
38
39//----------------------------------------------------------------------
40// Variable constructor
41//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000042Variable::Variable(
43 lldb::user_id_t uid, const char *name,
44 const char *mangled, // The mangled or fully qualified name of the variable.
45 const lldb::SymbolFileTypeSP &symfile_type_sp, ValueType scope,
46 SymbolContextScope *context, const RangeList &scope_range,
47 Declaration *decl_ptr, const DWARFExpression &location, bool external,
48 bool artificial, bool static_member)
49 : UserID(uid), m_name(name), m_mangled(ConstString(mangled)),
50 m_symfile_type_sp(symfile_type_sp), m_scope(scope),
51 m_owner_scope(context), m_scope_range(scope_range),
52 m_declaration(decl_ptr), m_location(location), m_external(external),
Zachary Turner9d76e422018-12-11 00:15:03 +000053 m_artificial(artificial), m_loc_is_const_data(false),
54 m_static_member(static_member) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055
56//----------------------------------------------------------------------
57// Destructor
58//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000059Variable::~Variable() {}
60
61lldb::LanguageType Variable::GetLanguage() const {
62 SymbolContext variable_sc;
63 m_owner_scope->CalculateSymbolContext(&variable_sc);
64 if (variable_sc.comp_unit)
65 return variable_sc.comp_unit->GetLanguage();
66 return lldb::eLanguageTypeUnknown;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000067}
68
Kate Stoneb9c1b512016-09-06 20:57:50 +000069ConstString Variable::GetName() const {
70 ConstString name = m_mangled.GetName(GetLanguage());
71 if (name)
72 return name;
73 return m_name;
Greg Claytonddaf6a72015-07-08 22:32:23 +000074}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000075
Kate Stoneb9c1b512016-09-06 20:57:50 +000076ConstString Variable::GetUnqualifiedName() const { return m_name; }
Greg Claytonddaf6a72015-07-08 22:32:23 +000077
Kate Stoneb9c1b512016-09-06 20:57:50 +000078bool Variable::NameMatches(const ConstString &name) const {
79 if (m_name == name)
80 return true;
81 SymbolContext variable_sc;
82 m_owner_scope->CalculateSymbolContext(&variable_sc);
Greg Claytonddaf6a72015-07-08 22:32:23 +000083
Kate Stoneb9c1b512016-09-06 20:57:50 +000084 LanguageType language = eLanguageTypeUnknown;
85 if (variable_sc.comp_unit)
86 language = variable_sc.comp_unit->GetLanguage();
87 return m_mangled.NameMatches(name, language);
88}
89bool Variable::NameMatches(const RegularExpression &regex) const {
90 if (regex.Execute(m_name.AsCString()))
91 return true;
92 if (m_mangled)
93 return m_mangled.NameMatches(regex, GetLanguage());
94 return false;
Greg Clayton83c5cd92010-11-14 22:13:40 +000095}
96
Kate Stoneb9c1b512016-09-06 20:57:50 +000097Type *Variable::GetType() {
98 if (m_symfile_type_sp)
99 return m_symfile_type_sp->GetType();
100 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +0000101}
102
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103void Variable::Dump(Stream *s, bool show_context) const {
104 s->Printf("%p: ", static_cast<const void *>(this));
105 s->Indent();
106 *s << "Variable" << (const UserID &)*this;
Paul Hermand628cbb2015-09-15 23:44:17 +0000107
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108 if (m_name)
109 *s << ", name = \"" << m_name << "\"";
Greg Claytonddaf6a72015-07-08 22:32:23 +0000110
Kate Stoneb9c1b512016-09-06 20:57:50 +0000111 if (m_symfile_type_sp) {
112 Type *type = m_symfile_type_sp->GetType();
113 if (type) {
114 *s << ", type = {" << type->GetID() << "} " << (void *)type << " (";
115 type->DumpTypeName(s);
116 s->PutChar(')');
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000117 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000119
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120 if (m_scope != eValueTypeInvalid) {
121 s->PutCString(", scope = ");
122 switch (m_scope) {
Greg Clayton007d5be2011-05-30 00:49:24 +0000123 case eValueTypeVariableGlobal:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124 s->PutCString(m_external ? "global" : "static");
125 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000126 case eValueTypeVariableArgument:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127 s->PutCString("parameter");
128 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000129 case eValueTypeVariableLocal:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130 s->PutCString("local");
131 break;
132 case eValueTypeVariableThreadLocal:
133 s->PutCString("thread local");
134 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000135 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136 *s << "??? (" << m_scope << ')';
137 }
138 }
139
140 if (show_context && m_owner_scope != nullptr) {
141 s->PutCString(", context = ( ");
142 m_owner_scope->DumpSymbolContext(s);
143 s->PutCString(" )");
144 }
145
146 bool show_fullpaths = false;
147 m_declaration.Dump(s, show_fullpaths);
148
149 if (m_location.IsValid()) {
150 s->PutCString(", location = ");
151 lldb::addr_t loclist_base_addr = LLDB_INVALID_ADDRESS;
152 if (m_location.IsLocationList()) {
153 SymbolContext variable_sc;
154 m_owner_scope->CalculateSymbolContext(&variable_sc);
155 if (variable_sc.function)
156 loclist_base_addr = variable_sc.function->GetAddressRange()
157 .GetBaseAddress()
158 .GetFileAddress();
159 }
Zachary Turner95997f62018-11-15 05:06:59 +0000160 ABISP abi;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000161 if (m_owner_scope) {
162 ModuleSP module_sp(m_owner_scope->CalculateSymbolContextModule());
163 if (module_sp)
Zachary Turner95997f62018-11-15 05:06:59 +0000164 abi = ABI::FindPlugin(ProcessSP(), module_sp->GetArchitecture());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000165 }
166 m_location.GetDescription(s, lldb::eDescriptionLevelBrief,
Zachary Turner95997f62018-11-15 05:06:59 +0000167 loclist_base_addr, abi.get());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000168 }
169
170 if (m_external)
171 s->PutCString(", external");
172
173 if (m_artificial)
174 s->PutCString(", artificial");
175
176 s->EOL();
177}
178
179bool Variable::DumpDeclaration(Stream *s, bool show_fullpaths,
180 bool show_module) {
181 bool dumped_declaration_info = false;
182 if (m_owner_scope) {
183 SymbolContext sc;
184 m_owner_scope->CalculateSymbolContext(&sc);
185 sc.block = nullptr;
186 sc.line_entry.Clear();
187 bool show_inlined_frames = false;
188 const bool show_function_arguments = true;
189 const bool show_function_name = true;
190
191 dumped_declaration_info = sc.DumpStopContext(
192 s, nullptr, Address(), show_fullpaths, show_module, show_inlined_frames,
193 show_function_arguments, show_function_name);
194
195 if (sc.function)
196 s->PutChar(':');
197 }
198 if (m_declaration.DumpStopContext(s, false))
199 dumped_declaration_info = true;
200 return dumped_declaration_info;
201}
202
203size_t Variable::MemorySize() const { return sizeof(Variable); }
204
205CompilerDeclContext Variable::GetDeclContext() {
206 Type *type = GetType();
207 if (type)
208 return type->GetSymbolFile()->GetDeclContextContainingUID(GetID());
209 return CompilerDeclContext();
210}
211
212CompilerDecl Variable::GetDecl() {
213 Type *type = GetType();
214 return type ? type->GetSymbolFile()->GetDeclForUID(GetID()) : CompilerDecl();
215}
216
217void Variable::CalculateSymbolContext(SymbolContext *sc) {
218 if (m_owner_scope) {
219 m_owner_scope->CalculateSymbolContext(sc);
220 sc->variable = this;
221 } else
222 sc->Clear(false);
223}
224
225bool Variable::LocationIsValidForFrame(StackFrame *frame) {
226 // Is the variable is described by a single location?
227 if (!m_location.IsLocationList()) {
228 // Yes it is, the location is valid.
229 return true;
230 }
231
232 if (frame) {
233 Function *function =
234 frame->GetSymbolContext(eSymbolContextFunction).function;
235 if (function) {
236 TargetSP target_sp(frame->CalculateTarget());
237
238 addr_t loclist_base_load_addr =
239 function->GetAddressRange().GetBaseAddress().GetLoadAddress(
240 target_sp.get());
241 if (loclist_base_load_addr == LLDB_INVALID_ADDRESS)
242 return false;
Adrian Prantl05097242018-04-30 16:49:04 +0000243 // It is a location list. We just need to tell if the location list
244 // contains the current address when converted to a load address
Kate Stoneb9c1b512016-09-06 20:57:50 +0000245 return m_location.LocationListContainsAddress(
246 loclist_base_load_addr,
247 frame->GetFrameCodeAddress().GetLoadAddress(target_sp.get()));
248 }
249 }
250 return false;
251}
252
253bool Variable::LocationIsValidForAddress(const Address &address) {
Adrian Prantl05097242018-04-30 16:49:04 +0000254 // Be sure to resolve the address to section offset prior to calling this
255 // function.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000256 if (address.IsSectionOffset()) {
257 SymbolContext sc;
258 CalculateSymbolContext(&sc);
259 if (sc.module_sp == address.GetModule()) {
260 // Is the variable is described by a single location?
261 if (!m_location.IsLocationList()) {
262 // Yes it is, the location is valid.
263 return true;
264 }
265
266 if (sc.function) {
267 addr_t loclist_base_file_addr =
268 sc.function->GetAddressRange().GetBaseAddress().GetFileAddress();
269 if (loclist_base_file_addr == LLDB_INVALID_ADDRESS)
270 return false;
Adrian Prantl05097242018-04-30 16:49:04 +0000271 // It is a location list. We just need to tell if the location list
272 // contains the current address when converted to a load address
Kate Stoneb9c1b512016-09-06 20:57:50 +0000273 return m_location.LocationListContainsAddress(loclist_base_file_addr,
274 address.GetFileAddress());
275 }
276 }
277 }
278 return false;
279}
280
281bool Variable::IsInScope(StackFrame *frame) {
282 switch (m_scope) {
283 case eValueTypeRegister:
284 case eValueTypeRegisterSet:
285 return frame != nullptr;
286
287 case eValueTypeConstResult:
288 case eValueTypeVariableGlobal:
289 case eValueTypeVariableStatic:
290 case eValueTypeVariableThreadLocal:
291 return true;
292
293 case eValueTypeVariableArgument:
294 case eValueTypeVariableLocal:
295 if (frame) {
Adrian Prantl05097242018-04-30 16:49:04 +0000296 // We don't have a location list, we just need to see if the block that
297 // this variable was defined in is currently
Kate Stoneb9c1b512016-09-06 20:57:50 +0000298 Block *deepest_frame_block =
299 frame->GetSymbolContext(eSymbolContextBlock).block;
300 if (deepest_frame_block) {
301 SymbolContext variable_sc;
302 CalculateSymbolContext(&variable_sc);
303
304 // Check for static or global variable defined at the compile unit
305 // level that wasn't defined in a block
306 if (variable_sc.block == nullptr)
307 return true;
308
309 // Check if the variable is valid in the current block
310 if (variable_sc.block != deepest_frame_block &&
311 !variable_sc.block->Contains(deepest_frame_block))
312 return false;
313
314 // If no scope range is specified then it means that the scope is the
Adrian Prantl05097242018-04-30 16:49:04 +0000315 // same as the scope of the enclosing lexical block.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000316 if (m_scope_range.IsEmpty())
317 return true;
318
319 addr_t file_address = frame->GetFrameCodeAddress().GetFileAddress();
320 return m_scope_range.FindEntryThatContains(file_address) != nullptr;
321 }
322 }
323 break;
324
325 default:
326 break;
327 }
328 return false;
329}
330
Zachary Turner97206d52017-05-12 04:51:55 +0000331Status Variable::GetValuesForVariableExpressionPath(
Zachary Turner2a3d10a2016-11-18 19:23:39 +0000332 llvm::StringRef variable_expr_path, ExecutionContextScope *scope,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000333 GetVariableCallback callback, void *baton, VariableList &variable_list,
334 ValueObjectList &valobj_list) {
Zachary Turner97206d52017-05-12 04:51:55 +0000335 Status error;
Zachary Turner2a3d10a2016-11-18 19:23:39 +0000336 if (!callback || variable_expr_path.empty()) {
337 error.SetErrorString("unknown error");
338 return error;
339 }
340
341 switch (variable_expr_path.front()) {
342 case '*':
343 error = Variable::GetValuesForVariableExpressionPath(
344 variable_expr_path.drop_front(), scope, callback, baton, variable_list,
345 valobj_list);
346 if (error.Fail()) {
347 error.SetErrorString("unknown error");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000348 return error;
Zachary Turner2a3d10a2016-11-18 19:23:39 +0000349 }
350 for (uint32_t i = 0; i < valobj_list.GetSize();) {
Zachary Turner97206d52017-05-12 04:51:55 +0000351 Status tmp_error;
Zachary Turner2a3d10a2016-11-18 19:23:39 +0000352 ValueObjectSP valobj_sp(
353 valobj_list.GetValueObjectAtIndex(i)->Dereference(tmp_error));
354 if (tmp_error.Fail()) {
355 variable_list.RemoveVariableAtIndex(i);
356 valobj_list.RemoveValueObjectAtIndex(i);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000357 } else {
Zachary Turner2a3d10a2016-11-18 19:23:39 +0000358 valobj_list.SetValueObjectAtIndex(i, valobj_sp);
359 ++i;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000360 }
Zachary Turner2a3d10a2016-11-18 19:23:39 +0000361 }
362 return error;
363 case '&': {
364 error = Variable::GetValuesForVariableExpressionPath(
365 variable_expr_path.drop_front(), scope, callback, baton, variable_list,
366 valobj_list);
367 if (error.Success()) {
368 for (uint32_t i = 0; i < valobj_list.GetSize();) {
Zachary Turner97206d52017-05-12 04:51:55 +0000369 Status tmp_error;
Zachary Turner2a3d10a2016-11-18 19:23:39 +0000370 ValueObjectSP valobj_sp(
371 valobj_list.GetValueObjectAtIndex(i)->AddressOf(tmp_error));
372 if (tmp_error.Fail()) {
373 variable_list.RemoveVariableAtIndex(i);
374 valobj_list.RemoveValueObjectAtIndex(i);
375 } else {
376 valobj_list.SetValueObjectAtIndex(i, valobj_sp);
377 ++i;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000378 }
379 }
Zachary Turner2a3d10a2016-11-18 19:23:39 +0000380 } else {
381 error.SetErrorString("unknown error");
382 }
383 return error;
384 } break;
385
386 default: {
387 static RegularExpression g_regex(
388 llvm::StringRef("^([A-Za-z_:][A-Za-z_0-9:]*)(.*)"));
389 RegularExpression::Match regex_match(1);
390 std::string variable_name;
391 variable_list.Clear();
392 if (!g_regex.Execute(variable_expr_path, &regex_match)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000393 error.SetErrorStringWithFormat(
Zachary Turner72f4997c2016-11-19 00:50:29 +0000394 "unable to extract a variable name from '%s'",
395 variable_expr_path.str().c_str());
Zachary Turner2a3d10a2016-11-18 19:23:39 +0000396 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000397 }
Zachary Turner2a3d10a2016-11-18 19:23:39 +0000398 if (!regex_match.GetMatchAtIndex(variable_expr_path, 1, variable_name)) {
399 error.SetErrorStringWithFormat(
Zachary Turner72f4997c2016-11-19 00:50:29 +0000400 "unable to extract a variable name from '%s'",
401 variable_expr_path.str().c_str());
Zachary Turner2a3d10a2016-11-18 19:23:39 +0000402 return error;
403 }
404 if (!callback(baton, variable_name.c_str(), variable_list)) {
405 error.SetErrorString("unknown error");
406 return error;
407 }
408 uint32_t i = 0;
409 while (i < variable_list.GetSize()) {
410 VariableSP var_sp(variable_list.GetVariableAtIndex(i));
411 ValueObjectSP valobj_sp;
412 if (!var_sp) {
413 variable_list.RemoveVariableAtIndex(i);
414 continue;
415 }
416 ValueObjectSP variable_valobj_sp(
417 ValueObjectVariable::Create(scope, var_sp));
418 if (!variable_valobj_sp) {
419 variable_list.RemoveVariableAtIndex(i);
420 continue;
421 }
422
423 llvm::StringRef variable_sub_expr_path =
424 variable_expr_path.drop_front(variable_name.size());
425 if (!variable_sub_expr_path.empty()) {
Zachary Turner2a3d10a2016-11-18 19:23:39 +0000426 valobj_sp = variable_valobj_sp->GetValueForExpressionPath(
Pavel Labath62ef1852017-12-07 10:38:22 +0000427 variable_sub_expr_path);
Zachary Turner2a3d10a2016-11-18 19:23:39 +0000428 if (!valobj_sp) {
429 error.SetErrorStringWithFormat(
430 "invalid expression path '%s' for variable '%s'",
Zachary Turner72f4997c2016-11-19 00:50:29 +0000431 variable_sub_expr_path.str().c_str(),
432 var_sp->GetName().GetCString());
Zachary Turner2a3d10a2016-11-18 19:23:39 +0000433 variable_list.RemoveVariableAtIndex(i);
434 continue;
435 }
436 } else {
437 // Just the name of a variable with no extras
438 valobj_sp = variable_valobj_sp;
439 }
440
441 valobj_list.Append(valobj_sp);
442 ++i;
443 }
444
445 if (variable_list.GetSize() > 0) {
446 error.Clear();
447 return error;
448 }
449 } break;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000450 }
451 error.SetErrorString("unknown error");
452 return error;
453}
454
455bool Variable::DumpLocationForAddress(Stream *s, const Address &address) {
Adrian Prantl05097242018-04-30 16:49:04 +0000456 // Be sure to resolve the address to section offset prior to calling this
457 // function.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000458 if (address.IsSectionOffset()) {
459 SymbolContext sc;
460 CalculateSymbolContext(&sc);
461 if (sc.module_sp == address.GetModule()) {
Zachary Turner95997f62018-11-15 05:06:59 +0000462 ABISP abi;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000463 if (m_owner_scope) {
464 ModuleSP module_sp(m_owner_scope->CalculateSymbolContextModule());
465 if (module_sp)
Zachary Turner95997f62018-11-15 05:06:59 +0000466 abi = ABI::FindPlugin(ProcessSP(), module_sp->GetArchitecture());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000467 }
468
469 const addr_t file_addr = address.GetFileAddress();
470 if (sc.function) {
471 if (sc.function->GetAddressRange().ContainsFileAddress(address)) {
472 addr_t loclist_base_file_addr =
473 sc.function->GetAddressRange().GetBaseAddress().GetFileAddress();
474 if (loclist_base_file_addr == LLDB_INVALID_ADDRESS)
475 return false;
476 return m_location.DumpLocationForAddress(s, eDescriptionLevelBrief,
477 loclist_base_file_addr,
Zachary Turner95997f62018-11-15 05:06:59 +0000478 file_addr, abi.get());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000479 }
480 }
Zachary Turner95997f62018-11-15 05:06:59 +0000481 return m_location.DumpLocationForAddress(s, eDescriptionLevelBrief,
482 LLDB_INVALID_ADDRESS, file_addr,
483 abi.get());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000484 }
485 }
486 return false;
487}
488
489static void PrivateAutoComplete(
Zachary Turner4aa87532016-11-17 01:37:42 +0000490 StackFrame *frame, llvm::StringRef partial_path,
491 const llvm::Twine
Kate Stoneb9c1b512016-09-06 20:57:50 +0000492 &prefix_path, // Anything that has been resolved already will be in here
493 const CompilerType &compiler_type,
494 StringList &matches, bool &word_complete);
495
496static void PrivateAutoCompleteMembers(
497 StackFrame *frame, const std::string &partial_member_name,
Zachary Turner4aa87532016-11-17 01:37:42 +0000498 llvm::StringRef partial_path,
499 const llvm::Twine
Kate Stoneb9c1b512016-09-06 20:57:50 +0000500 &prefix_path, // Anything that has been resolved already will be in here
501 const CompilerType &compiler_type,
502 StringList &matches, bool &word_complete);
503
504static void PrivateAutoCompleteMembers(
505 StackFrame *frame, const std::string &partial_member_name,
Zachary Turner4aa87532016-11-17 01:37:42 +0000506 llvm::StringRef partial_path,
507 const llvm::Twine
Kate Stoneb9c1b512016-09-06 20:57:50 +0000508 &prefix_path, // Anything that has been resolved already will be in here
509 const CompilerType &compiler_type,
510 StringList &matches, bool &word_complete) {
511
512 // We are in a type parsing child members
513 const uint32_t num_bases = compiler_type.GetNumDirectBaseClasses();
514
515 if (num_bases > 0) {
516 for (uint32_t i = 0; i < num_bases; ++i) {
517 CompilerType base_class_type =
518 compiler_type.GetDirectBaseClassAtIndex(i, nullptr);
519
520 PrivateAutoCompleteMembers(
521 frame, partial_member_name, partial_path, prefix_path,
522 base_class_type.GetCanonicalType(), matches, word_complete);
523 }
524 }
525
526 const uint32_t num_vbases = compiler_type.GetNumVirtualBaseClasses();
527
528 if (num_vbases > 0) {
529 for (uint32_t i = 0; i < num_vbases; ++i) {
530 CompilerType vbase_class_type =
531 compiler_type.GetVirtualBaseClassAtIndex(i, nullptr);
532
533 PrivateAutoCompleteMembers(
534 frame, partial_member_name, partial_path, prefix_path,
535 vbase_class_type.GetCanonicalType(), matches, word_complete);
536 }
537 }
538
539 // We are in a type parsing child members
540 const uint32_t num_fields = compiler_type.GetNumFields();
541
542 if (num_fields > 0) {
543 for (uint32_t i = 0; i < num_fields; ++i) {
544 std::string member_name;
545
546 CompilerType member_compiler_type = compiler_type.GetFieldAtIndex(
547 i, member_name, nullptr, nullptr, nullptr);
548
549 if (partial_member_name.empty() ||
550 member_name.find(partial_member_name) == 0) {
551 if (member_name == partial_member_name) {
552 PrivateAutoComplete(
553 frame, partial_path,
554 prefix_path + member_name, // Anything that has been resolved
555 // already will be in here
556 member_compiler_type.GetCanonicalType(), matches, word_complete);
557 } else {
Zachary Turner4aa87532016-11-17 01:37:42 +0000558 matches.AppendString((prefix_path + member_name).str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000559 }
560 }
561 }
562 }
563}
564
565static void PrivateAutoComplete(
Zachary Turner4aa87532016-11-17 01:37:42 +0000566 StackFrame *frame, llvm::StringRef partial_path,
567 const llvm::Twine
Kate Stoneb9c1b512016-09-06 20:57:50 +0000568 &prefix_path, // Anything that has been resolved already will be in here
569 const CompilerType &compiler_type,
570 StringList &matches, bool &word_complete) {
571 // printf ("\nPrivateAutoComplete()\n\tprefix_path = '%s'\n\tpartial_path =
572 // '%s'\n", prefix_path.c_str(), partial_path.c_str());
573 std::string remaining_partial_path;
574
575 const lldb::TypeClass type_class = compiler_type.GetTypeClass();
576 if (partial_path.empty()) {
577 if (compiler_type.IsValid()) {
578 switch (type_class) {
579 default:
580 case eTypeClassArray:
581 case eTypeClassBlockPointer:
582 case eTypeClassBuiltin:
583 case eTypeClassComplexFloat:
584 case eTypeClassComplexInteger:
585 case eTypeClassEnumeration:
586 case eTypeClassFunction:
587 case eTypeClassMemberPointer:
588 case eTypeClassReference:
589 case eTypeClassTypedef:
590 case eTypeClassVector: {
Zachary Turner4aa87532016-11-17 01:37:42 +0000591 matches.AppendString(prefix_path.str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000592 word_complete = matches.GetSize() == 1;
593 } break;
594
595 case eTypeClassClass:
596 case eTypeClassStruct:
597 case eTypeClassUnion:
Zachary Turner4aa87532016-11-17 01:37:42 +0000598 if (prefix_path.str().back() != '.')
599 matches.AppendString((prefix_path + ".").str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000600 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000601
Kate Stoneb9c1b512016-09-06 20:57:50 +0000602 case eTypeClassObjCObject:
603 case eTypeClassObjCInterface:
604 break;
605 case eTypeClassObjCObjectPointer:
606 case eTypeClassPointer: {
607 bool omit_empty_base_classes = true;
Adrian Prantleca07c52018-11-05 20:49:07 +0000608 if (compiler_type.GetNumChildren(omit_empty_base_classes, nullptr) > 0)
Zachary Turner4aa87532016-11-17 01:37:42 +0000609 matches.AppendString((prefix_path + "->").str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000610 else {
Zachary Turner4aa87532016-11-17 01:37:42 +0000611 matches.AppendString(prefix_path.str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000612 word_complete = true;
613 }
614 } break;
615 }
616 } else {
617 if (frame) {
618 const bool get_file_globals = true;
619
620 VariableList *variable_list = frame->GetVariableList(get_file_globals);
621
622 if (variable_list) {
623 const size_t num_variables = variable_list->GetSize();
624 for (size_t i = 0; i < num_variables; ++i) {
625 Variable *variable = variable_list->GetVariableAtIndex(i).get();
626 matches.AppendString(variable->GetName().AsCString());
627 }
628 }
629 }
630 }
631 } else {
632 const char ch = partial_path[0];
633 switch (ch) {
634 case '*':
Zachary Turner4aa87532016-11-17 01:37:42 +0000635 if (prefix_path.str().empty()) {
636 PrivateAutoComplete(frame, partial_path.substr(1), "*", compiler_type,
637 matches, word_complete);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000638 }
639 break;
640
641 case '&':
Zachary Turner4aa87532016-11-17 01:37:42 +0000642 if (prefix_path.isTriviallyEmpty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000643 PrivateAutoComplete(frame, partial_path.substr(1), std::string("&"),
644 compiler_type, matches, word_complete);
645 }
646 break;
647
648 case '-':
Raphael Isemannd4ff5ba2018-07-27 23:37:08 +0000649 if (partial_path.size() > 1 && partial_path[1] == '>' &&
650 !prefix_path.str().empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000651 switch (type_class) {
652 case lldb::eTypeClassPointer: {
653 CompilerType pointee_type(compiler_type.GetPointeeType());
Raphael Isemannd4ff5ba2018-07-27 23:37:08 +0000654 if (partial_path.size() > 2 && partial_path[2]) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000655 // If there is more after the "->", then search deeper
656 PrivateAutoComplete(
657 frame, partial_path.substr(2), prefix_path + "->",
658 pointee_type.GetCanonicalType(), matches, word_complete);
659 } else {
660 // Nothing after the "->", so list all members
661 PrivateAutoCompleteMembers(
662 frame, std::string(), std::string(), prefix_path + "->",
663 pointee_type.GetCanonicalType(), matches, word_complete);
664 }
665 } break;
Greg Clayton884fb692011-07-08 21:46:14 +0000666 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000667 break;
Greg Clayton884fb692011-07-08 21:46:14 +0000668 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000669 }
670 break;
Greg Clayton884fb692011-07-08 21:46:14 +0000671
Kate Stoneb9c1b512016-09-06 20:57:50 +0000672 case '.':
673 if (compiler_type.IsValid()) {
674 switch (type_class) {
675 case lldb::eTypeClassUnion:
676 case lldb::eTypeClassStruct:
677 case lldb::eTypeClassClass:
Raphael Isemannd4ff5ba2018-07-27 23:37:08 +0000678 if (partial_path.size() > 1 && partial_path[1]) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000679 // If there is more after the ".", then search deeper
680 PrivateAutoComplete(frame, partial_path.substr(1),
681 prefix_path + ".", compiler_type, matches,
682 word_complete);
Greg Claytonafacd142011-09-02 01:15:17 +0000683
Kate Stoneb9c1b512016-09-06 20:57:50 +0000684 } else {
685 // Nothing after the ".", so list all members
686 PrivateAutoCompleteMembers(frame, std::string(), partial_path,
687 prefix_path + ".", compiler_type,
688 matches, word_complete);
689 }
690 break;
Greg Claytonf21fead2013-05-14 23:43:18 +0000691 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000692 break;
Greg Claytonf21fead2013-05-14 23:43:18 +0000693 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000694 }
695 break;
696 default:
697 if (isalpha(ch) || ch == '_' || ch == '$') {
698 const size_t partial_path_len = partial_path.size();
699 size_t pos = 1;
700 while (pos < partial_path_len) {
701 const char curr_ch = partial_path[pos];
702 if (isalnum(curr_ch) || curr_ch == '_' || curr_ch == '$') {
703 ++pos;
704 continue;
705 }
706 break;
707 }
708
709 std::string token(partial_path, 0, pos);
710 remaining_partial_path = partial_path.substr(pos);
711
712 if (compiler_type.IsValid()) {
713 PrivateAutoCompleteMembers(frame, token, remaining_partial_path,
714 prefix_path, compiler_type, matches,
715 word_complete);
716 } else if (frame) {
717 // We haven't found our variable yet
718 const bool get_file_globals = true;
719
720 VariableList *variable_list =
721 frame->GetVariableList(get_file_globals);
722
723 if (!variable_list)
724 break;
725
726 const size_t num_variables = variable_list->GetSize();
727 for (size_t i = 0; i < num_variables; ++i) {
728 Variable *variable = variable_list->GetVariableAtIndex(i).get();
729
730 if (!variable)
731 continue;
732
733 const char *variable_name = variable->GetName().AsCString();
734 if (strstr(variable_name, token.c_str()) == variable_name) {
735 if (strcmp(variable_name, token.c_str()) == 0) {
736 Type *variable_type = variable->GetType();
737 if (variable_type) {
738 CompilerType variable_compiler_type(
739 variable_type->GetForwardCompilerType());
740 PrivateAutoComplete(
741 frame, remaining_partial_path,
742 prefix_path + token, // Anything that has been resolved
743 // already will be in here
744 variable_compiler_type.GetCanonicalType(), matches,
745 word_complete);
746 } else {
Zachary Turner4aa87532016-11-17 01:37:42 +0000747 matches.AppendString((prefix_path + variable_name).str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000748 }
749 } else if (remaining_partial_path.empty()) {
Zachary Turner4aa87532016-11-17 01:37:42 +0000750 matches.AppendString((prefix_path + variable_name).str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000751 }
752 }
753 }
754 }
755 }
756 break;
Greg Claytonf21fead2013-05-14 23:43:18 +0000757 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000758 }
Greg Claytonf21fead2013-05-14 23:43:18 +0000759}
760
Kate Stoneb9c1b512016-09-06 20:57:50 +0000761size_t Variable::AutoComplete(const ExecutionContext &exe_ctx,
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000762 CompletionRequest &request) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000763 CompilerType compiler_type;
Greg Claytonf21fead2013-05-14 23:43:18 +0000764
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000765 bool word_complete = false;
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000766 StringList matches;
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000767 PrivateAutoComplete(exe_ctx.GetFramePtr(), request.GetCursorArgumentPrefix(),
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000768 "", compiler_type, matches, word_complete);
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000769 request.SetWordComplete(word_complete);
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000770 request.AddCompletions(matches);
Greg Claytonf21fead2013-05-14 23:43:18 +0000771
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000772 return request.GetNumberOfMatches();
Greg Claytonc749eb82011-07-11 05:12:02 +0000773}