blob: 90e3f1d9a991ea1dcccfa72b03617557040dac5a [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 Clayton83c5cd92010-11-14 22:13:40 +000013#include "lldb/Core/RegularExpression.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000014#include "lldb/Core/Stream.h"
Greg Clayton884fb692011-07-08 21:46:14 +000015#include "lldb/Core/ValueObject.h"
16#include "lldb/Core/ValueObjectVariable.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/Symbol/Block.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000018#include "lldb/Symbol/CompileUnit.h"
Paul Hermand628cbb2015-09-15 23:44:17 +000019#include "lldb/Symbol/CompilerDecl.h"
20#include "lldb/Symbol/CompilerDeclContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/Symbol/Function.h"
22#include "lldb/Symbol/SymbolContext.h"
Paul Hermand628cbb2015-09-15 23:44:17 +000023#include "lldb/Symbol/SymbolFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024#include "lldb/Symbol/Type.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000025#include "lldb/Symbol/TypeSystem.h"
Greg Clayton884fb692011-07-08 21:46:14 +000026#include "lldb/Symbol/VariableList.h"
Greg Claytonafacd142011-09-02 01:15:17 +000027#include "lldb/Target/ABI.h"
Greg Claytonf5e56de2010-09-14 23:36:40 +000028#include "lldb/Target/Process.h"
Greg Clayton9da7bd02010-08-24 21:05:24 +000029#include "lldb/Target/RegisterContext.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000030#include "lldb/Target/StackFrame.h"
Greg Claytonf5e56de2010-09-14 23:36:40 +000031#include "lldb/Target/Target.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000032#include "lldb/Target/Thread.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),
53 m_artificial(artificial), m_static_member(static_member) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054
55//----------------------------------------------------------------------
56// Destructor
57//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000058Variable::~Variable() {}
59
60lldb::LanguageType Variable::GetLanguage() const {
61 SymbolContext variable_sc;
62 m_owner_scope->CalculateSymbolContext(&variable_sc);
63 if (variable_sc.comp_unit)
64 return variable_sc.comp_unit->GetLanguage();
65 return lldb::eLanguageTypeUnknown;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066}
67
Kate Stoneb9c1b512016-09-06 20:57:50 +000068ConstString Variable::GetName() const {
69 ConstString name = m_mangled.GetName(GetLanguage());
70 if (name)
71 return name;
72 return m_name;
Greg Claytonddaf6a72015-07-08 22:32:23 +000073}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000074
Kate Stoneb9c1b512016-09-06 20:57:50 +000075ConstString Variable::GetUnqualifiedName() const { return m_name; }
Greg Claytonddaf6a72015-07-08 22:32:23 +000076
Kate Stoneb9c1b512016-09-06 20:57:50 +000077bool Variable::NameMatches(const ConstString &name) const {
78 if (m_name == name)
79 return true;
80 SymbolContext variable_sc;
81 m_owner_scope->CalculateSymbolContext(&variable_sc);
Greg Claytonddaf6a72015-07-08 22:32:23 +000082
Kate Stoneb9c1b512016-09-06 20:57:50 +000083 LanguageType language = eLanguageTypeUnknown;
84 if (variable_sc.comp_unit)
85 language = variable_sc.comp_unit->GetLanguage();
86 return m_mangled.NameMatches(name, language);
87}
88bool Variable::NameMatches(const RegularExpression &regex) const {
89 if (regex.Execute(m_name.AsCString()))
90 return true;
91 if (m_mangled)
92 return m_mangled.NameMatches(regex, GetLanguage());
93 return false;
Greg Clayton83c5cd92010-11-14 22:13:40 +000094}
95
Kate Stoneb9c1b512016-09-06 20:57:50 +000096Type *Variable::GetType() {
97 if (m_symfile_type_sp)
98 return m_symfile_type_sp->GetType();
99 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +0000100}
101
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102void Variable::Dump(Stream *s, bool show_context) const {
103 s->Printf("%p: ", static_cast<const void *>(this));
104 s->Indent();
105 *s << "Variable" << (const UserID &)*this;
Paul Hermand628cbb2015-09-15 23:44:17 +0000106
Kate Stoneb9c1b512016-09-06 20:57:50 +0000107 if (m_name)
108 *s << ", name = \"" << m_name << "\"";
Greg Claytonddaf6a72015-07-08 22:32:23 +0000109
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110 if (m_symfile_type_sp) {
111 Type *type = m_symfile_type_sp->GetType();
112 if (type) {
113 *s << ", type = {" << type->GetID() << "} " << (void *)type << " (";
114 type->DumpTypeName(s);
115 s->PutChar(')');
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000116 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000118
Kate Stoneb9c1b512016-09-06 20:57:50 +0000119 if (m_scope != eValueTypeInvalid) {
120 s->PutCString(", scope = ");
121 switch (m_scope) {
Greg Clayton007d5be2011-05-30 00:49:24 +0000122 case eValueTypeVariableGlobal:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123 s->PutCString(m_external ? "global" : "static");
124 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000125 case eValueTypeVariableArgument:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000126 s->PutCString("parameter");
127 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000128 case eValueTypeVariableLocal:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129 s->PutCString("local");
130 break;
131 case eValueTypeVariableThreadLocal:
132 s->PutCString("thread local");
133 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000134 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000135 *s << "??? (" << m_scope << ')';
136 }
137 }
138
139 if (show_context && m_owner_scope != nullptr) {
140 s->PutCString(", context = ( ");
141 m_owner_scope->DumpSymbolContext(s);
142 s->PutCString(" )");
143 }
144
145 bool show_fullpaths = false;
146 m_declaration.Dump(s, show_fullpaths);
147
148 if (m_location.IsValid()) {
149 s->PutCString(", location = ");
150 lldb::addr_t loclist_base_addr = LLDB_INVALID_ADDRESS;
151 if (m_location.IsLocationList()) {
152 SymbolContext variable_sc;
153 m_owner_scope->CalculateSymbolContext(&variable_sc);
154 if (variable_sc.function)
155 loclist_base_addr = variable_sc.function->GetAddressRange()
156 .GetBaseAddress()
157 .GetFileAddress();
158 }
159 ABI *abi = nullptr;
160 if (m_owner_scope) {
161 ModuleSP module_sp(m_owner_scope->CalculateSymbolContextModule());
162 if (module_sp)
163 abi = ABI::FindPlugin(module_sp->GetArchitecture()).get();
164 }
165 m_location.GetDescription(s, lldb::eDescriptionLevelBrief,
166 loclist_base_addr, abi);
167 }
168
169 if (m_external)
170 s->PutCString(", external");
171
172 if (m_artificial)
173 s->PutCString(", artificial");
174
175 s->EOL();
176}
177
178bool Variable::DumpDeclaration(Stream *s, bool show_fullpaths,
179 bool show_module) {
180 bool dumped_declaration_info = false;
181 if (m_owner_scope) {
182 SymbolContext sc;
183 m_owner_scope->CalculateSymbolContext(&sc);
184 sc.block = nullptr;
185 sc.line_entry.Clear();
186 bool show_inlined_frames = false;
187 const bool show_function_arguments = true;
188 const bool show_function_name = true;
189
190 dumped_declaration_info = sc.DumpStopContext(
191 s, nullptr, Address(), show_fullpaths, show_module, show_inlined_frames,
192 show_function_arguments, show_function_name);
193
194 if (sc.function)
195 s->PutChar(':');
196 }
197 if (m_declaration.DumpStopContext(s, false))
198 dumped_declaration_info = true;
199 return dumped_declaration_info;
200}
201
202size_t Variable::MemorySize() const { return sizeof(Variable); }
203
204CompilerDeclContext Variable::GetDeclContext() {
205 Type *type = GetType();
206 if (type)
207 return type->GetSymbolFile()->GetDeclContextContainingUID(GetID());
208 return CompilerDeclContext();
209}
210
211CompilerDecl Variable::GetDecl() {
212 Type *type = GetType();
213 return type ? type->GetSymbolFile()->GetDeclForUID(GetID()) : CompilerDecl();
214}
215
216void Variable::CalculateSymbolContext(SymbolContext *sc) {
217 if (m_owner_scope) {
218 m_owner_scope->CalculateSymbolContext(sc);
219 sc->variable = this;
220 } else
221 sc->Clear(false);
222}
223
224bool Variable::LocationIsValidForFrame(StackFrame *frame) {
225 // Is the variable is described by a single location?
226 if (!m_location.IsLocationList()) {
227 // Yes it is, the location is valid.
228 return true;
229 }
230
231 if (frame) {
232 Function *function =
233 frame->GetSymbolContext(eSymbolContextFunction).function;
234 if (function) {
235 TargetSP target_sp(frame->CalculateTarget());
236
237 addr_t loclist_base_load_addr =
238 function->GetAddressRange().GetBaseAddress().GetLoadAddress(
239 target_sp.get());
240 if (loclist_base_load_addr == LLDB_INVALID_ADDRESS)
241 return false;
242 // It is a location list. We just need to tell if the location
243 // list contains the current address when converted to a load
244 // address
245 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) {
254 // Be sure to resolve the address to section offset prior to
255 // calling this function.
256 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;
271 // It is a location list. We just need to tell if the location
272 // list contains the current address when converted to a load
273 // address
274 return m_location.LocationListContainsAddress(loclist_base_file_addr,
275 address.GetFileAddress());
276 }
277 }
278 }
279 return false;
280}
281
282bool Variable::IsInScope(StackFrame *frame) {
283 switch (m_scope) {
284 case eValueTypeRegister:
285 case eValueTypeRegisterSet:
286 return frame != nullptr;
287
288 case eValueTypeConstResult:
289 case eValueTypeVariableGlobal:
290 case eValueTypeVariableStatic:
291 case eValueTypeVariableThreadLocal:
292 return true;
293
294 case eValueTypeVariableArgument:
295 case eValueTypeVariableLocal:
296 if (frame) {
297 // We don't have a location list, we just need to see if the block
298 // that this variable was defined in is currently
299 Block *deepest_frame_block =
300 frame->GetSymbolContext(eSymbolContextBlock).block;
301 if (deepest_frame_block) {
302 SymbolContext variable_sc;
303 CalculateSymbolContext(&variable_sc);
304
305 // Check for static or global variable defined at the compile unit
306 // level that wasn't defined in a block
307 if (variable_sc.block == nullptr)
308 return true;
309
310 // Check if the variable is valid in the current block
311 if (variable_sc.block != deepest_frame_block &&
312 !variable_sc.block->Contains(deepest_frame_block))
313 return false;
314
315 // If no scope range is specified then it means that the scope is the
316 // same as the
317 // scope of the enclosing lexical block.
318 if (m_scope_range.IsEmpty())
319 return true;
320
321 addr_t file_address = frame->GetFrameCodeAddress().GetFileAddress();
322 return m_scope_range.FindEntryThatContains(file_address) != nullptr;
323 }
324 }
325 break;
326
327 default:
328 break;
329 }
330 return false;
331}
332
333Error Variable::GetValuesForVariableExpressionPath(
Zachary Turner2a3d10a2016-11-18 19:23:39 +0000334 llvm::StringRef variable_expr_path, ExecutionContextScope *scope,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000335 GetVariableCallback callback, void *baton, VariableList &variable_list,
336 ValueObjectList &valobj_list) {
337 Error error;
Zachary Turner2a3d10a2016-11-18 19:23:39 +0000338 if (!callback || variable_expr_path.empty()) {
339 error.SetErrorString("unknown error");
340 return error;
341 }
342
343 switch (variable_expr_path.front()) {
344 case '*':
345 error = Variable::GetValuesForVariableExpressionPath(
346 variable_expr_path.drop_front(), scope, callback, baton, variable_list,
347 valobj_list);
348 if (error.Fail()) {
349 error.SetErrorString("unknown error");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000350 return error;
Zachary Turner2a3d10a2016-11-18 19:23:39 +0000351 }
352 for (uint32_t i = 0; i < valobj_list.GetSize();) {
353 Error tmp_error;
354 ValueObjectSP valobj_sp(
355 valobj_list.GetValueObjectAtIndex(i)->Dereference(tmp_error));
356 if (tmp_error.Fail()) {
357 variable_list.RemoveVariableAtIndex(i);
358 valobj_list.RemoveValueObjectAtIndex(i);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000359 } else {
Zachary Turner2a3d10a2016-11-18 19:23:39 +0000360 valobj_list.SetValueObjectAtIndex(i, valobj_sp);
361 ++i;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000362 }
Zachary Turner2a3d10a2016-11-18 19:23:39 +0000363 }
364 return error;
365 case '&': {
366 error = Variable::GetValuesForVariableExpressionPath(
367 variable_expr_path.drop_front(), scope, callback, baton, variable_list,
368 valobj_list);
369 if (error.Success()) {
370 for (uint32_t i = 0; i < valobj_list.GetSize();) {
371 Error tmp_error;
372 ValueObjectSP valobj_sp(
373 valobj_list.GetValueObjectAtIndex(i)->AddressOf(tmp_error));
374 if (tmp_error.Fail()) {
375 variable_list.RemoveVariableAtIndex(i);
376 valobj_list.RemoveValueObjectAtIndex(i);
377 } else {
378 valobj_list.SetValueObjectAtIndex(i, valobj_sp);
379 ++i;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000380 }
381 }
Zachary Turner2a3d10a2016-11-18 19:23:39 +0000382 } else {
383 error.SetErrorString("unknown error");
384 }
385 return error;
386 } break;
387
388 default: {
389 static RegularExpression g_regex(
390 llvm::StringRef("^([A-Za-z_:][A-Za-z_0-9:]*)(.*)"));
391 RegularExpression::Match regex_match(1);
392 std::string variable_name;
393 variable_list.Clear();
394 if (!g_regex.Execute(variable_expr_path, &regex_match)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000395 error.SetErrorStringWithFormat(
396 "unable to extract a variable name from '%s'", variable_expr_path);
Zachary Turner2a3d10a2016-11-18 19:23:39 +0000397 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000398 }
Zachary Turner2a3d10a2016-11-18 19:23:39 +0000399 if (!regex_match.GetMatchAtIndex(variable_expr_path, 1, variable_name)) {
400 error.SetErrorStringWithFormat(
401 "unable to extract a variable name from '%s'", variable_expr_path);
402 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()) {
426 ValueObject::ExpressionPathScanEndReason reason_to_stop;
427 ValueObject::ExpressionPathEndResultType final_value_type;
428 ValueObject::GetValueForExpressionPathOptions options;
429 ValueObject::ExpressionPathAftermath final_task_on_target;
430
431 valobj_sp = variable_valobj_sp->GetValueForExpressionPath(
432 variable_sub_expr_path, &reason_to_stop, &final_value_type, options,
433 &final_task_on_target);
434 if (!valobj_sp) {
435 error.SetErrorStringWithFormat(
436 "invalid expression path '%s' for variable '%s'",
437 variable_sub_expr_path, var_sp->GetName().GetCString());
438 variable_list.RemoveVariableAtIndex(i);
439 continue;
440 }
441 } else {
442 // Just the name of a variable with no extras
443 valobj_sp = variable_valobj_sp;
444 }
445
446 valobj_list.Append(valobj_sp);
447 ++i;
448 }
449
450 if (variable_list.GetSize() > 0) {
451 error.Clear();
452 return error;
453 }
454 } break;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000455 }
456 error.SetErrorString("unknown error");
457 return error;
458}
459
460bool Variable::DumpLocationForAddress(Stream *s, const Address &address) {
461 // Be sure to resolve the address to section offset prior to
462 // calling this function.
463 if (address.IsSectionOffset()) {
464 SymbolContext sc;
465 CalculateSymbolContext(&sc);
466 if (sc.module_sp == address.GetModule()) {
467 ABI *abi = nullptr;
468 if (m_owner_scope) {
469 ModuleSP module_sp(m_owner_scope->CalculateSymbolContextModule());
470 if (module_sp)
471 abi = ABI::FindPlugin(module_sp->GetArchitecture()).get();
472 }
473
474 const addr_t file_addr = address.GetFileAddress();
475 if (sc.function) {
476 if (sc.function->GetAddressRange().ContainsFileAddress(address)) {
477 addr_t loclist_base_file_addr =
478 sc.function->GetAddressRange().GetBaseAddress().GetFileAddress();
479 if (loclist_base_file_addr == LLDB_INVALID_ADDRESS)
480 return false;
481 return m_location.DumpLocationForAddress(s, eDescriptionLevelBrief,
482 loclist_base_file_addr,
483 file_addr, abi);
484 }
485 }
486 return m_location.DumpLocationForAddress(
487 s, eDescriptionLevelBrief, LLDB_INVALID_ADDRESS, file_addr, abi);
488 }
489 }
490 return false;
491}
492
493static void PrivateAutoComplete(
Zachary Turner4aa87532016-11-17 01:37:42 +0000494 StackFrame *frame, llvm::StringRef partial_path,
495 const llvm::Twine
Kate Stoneb9c1b512016-09-06 20:57:50 +0000496 &prefix_path, // Anything that has been resolved already will be in here
497 const CompilerType &compiler_type,
498 StringList &matches, bool &word_complete);
499
500static void PrivateAutoCompleteMembers(
501 StackFrame *frame, const std::string &partial_member_name,
Zachary Turner4aa87532016-11-17 01:37:42 +0000502 llvm::StringRef partial_path,
503 const llvm::Twine
Kate Stoneb9c1b512016-09-06 20:57:50 +0000504 &prefix_path, // Anything that has been resolved already will be in here
505 const CompilerType &compiler_type,
506 StringList &matches, bool &word_complete);
507
508static void PrivateAutoCompleteMembers(
509 StackFrame *frame, const std::string &partial_member_name,
Zachary Turner4aa87532016-11-17 01:37:42 +0000510 llvm::StringRef partial_path,
511 const llvm::Twine
Kate Stoneb9c1b512016-09-06 20:57:50 +0000512 &prefix_path, // Anything that has been resolved already will be in here
513 const CompilerType &compiler_type,
514 StringList &matches, bool &word_complete) {
515
516 // We are in a type parsing child members
517 const uint32_t num_bases = compiler_type.GetNumDirectBaseClasses();
518
519 if (num_bases > 0) {
520 for (uint32_t i = 0; i < num_bases; ++i) {
521 CompilerType base_class_type =
522 compiler_type.GetDirectBaseClassAtIndex(i, nullptr);
523
524 PrivateAutoCompleteMembers(
525 frame, partial_member_name, partial_path, prefix_path,
526 base_class_type.GetCanonicalType(), matches, word_complete);
527 }
528 }
529
530 const uint32_t num_vbases = compiler_type.GetNumVirtualBaseClasses();
531
532 if (num_vbases > 0) {
533 for (uint32_t i = 0; i < num_vbases; ++i) {
534 CompilerType vbase_class_type =
535 compiler_type.GetVirtualBaseClassAtIndex(i, nullptr);
536
537 PrivateAutoCompleteMembers(
538 frame, partial_member_name, partial_path, prefix_path,
539 vbase_class_type.GetCanonicalType(), matches, word_complete);
540 }
541 }
542
543 // We are in a type parsing child members
544 const uint32_t num_fields = compiler_type.GetNumFields();
545
546 if (num_fields > 0) {
547 for (uint32_t i = 0; i < num_fields; ++i) {
548 std::string member_name;
549
550 CompilerType member_compiler_type = compiler_type.GetFieldAtIndex(
551 i, member_name, nullptr, nullptr, nullptr);
552
553 if (partial_member_name.empty() ||
554 member_name.find(partial_member_name) == 0) {
555 if (member_name == partial_member_name) {
556 PrivateAutoComplete(
557 frame, partial_path,
558 prefix_path + member_name, // Anything that has been resolved
559 // already will be in here
560 member_compiler_type.GetCanonicalType(), matches, word_complete);
561 } else {
Zachary Turner4aa87532016-11-17 01:37:42 +0000562 matches.AppendString((prefix_path + member_name).str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000563 }
564 }
565 }
566 }
567}
568
569static void PrivateAutoComplete(
Zachary Turner4aa87532016-11-17 01:37:42 +0000570 StackFrame *frame, llvm::StringRef partial_path,
571 const llvm::Twine
Kate Stoneb9c1b512016-09-06 20:57:50 +0000572 &prefix_path, // Anything that has been resolved already will be in here
573 const CompilerType &compiler_type,
574 StringList &matches, bool &word_complete) {
575 // printf ("\nPrivateAutoComplete()\n\tprefix_path = '%s'\n\tpartial_path =
576 // '%s'\n", prefix_path.c_str(), partial_path.c_str());
577 std::string remaining_partial_path;
578
579 const lldb::TypeClass type_class = compiler_type.GetTypeClass();
580 if (partial_path.empty()) {
581 if (compiler_type.IsValid()) {
582 switch (type_class) {
583 default:
584 case eTypeClassArray:
585 case eTypeClassBlockPointer:
586 case eTypeClassBuiltin:
587 case eTypeClassComplexFloat:
588 case eTypeClassComplexInteger:
589 case eTypeClassEnumeration:
590 case eTypeClassFunction:
591 case eTypeClassMemberPointer:
592 case eTypeClassReference:
593 case eTypeClassTypedef:
594 case eTypeClassVector: {
Zachary Turner4aa87532016-11-17 01:37:42 +0000595 matches.AppendString(prefix_path.str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000596 word_complete = matches.GetSize() == 1;
597 } break;
598
599 case eTypeClassClass:
600 case eTypeClassStruct:
601 case eTypeClassUnion:
Zachary Turner4aa87532016-11-17 01:37:42 +0000602 if (prefix_path.str().back() != '.')
603 matches.AppendString((prefix_path + ".").str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000604 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000605
Kate Stoneb9c1b512016-09-06 20:57:50 +0000606 case eTypeClassObjCObject:
607 case eTypeClassObjCInterface:
608 break;
609 case eTypeClassObjCObjectPointer:
610 case eTypeClassPointer: {
611 bool omit_empty_base_classes = true;
612 if (compiler_type.GetNumChildren(omit_empty_base_classes) > 0)
Zachary Turner4aa87532016-11-17 01:37:42 +0000613 matches.AppendString((prefix_path + "->").str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000614 else {
Zachary Turner4aa87532016-11-17 01:37:42 +0000615 matches.AppendString(prefix_path.str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000616 word_complete = true;
617 }
618 } break;
619 }
620 } else {
621 if (frame) {
622 const bool get_file_globals = true;
623
624 VariableList *variable_list = frame->GetVariableList(get_file_globals);
625
626 if (variable_list) {
627 const size_t num_variables = variable_list->GetSize();
628 for (size_t i = 0; i < num_variables; ++i) {
629 Variable *variable = variable_list->GetVariableAtIndex(i).get();
630 matches.AppendString(variable->GetName().AsCString());
631 }
632 }
633 }
634 }
635 } else {
636 const char ch = partial_path[0];
637 switch (ch) {
638 case '*':
Zachary Turner4aa87532016-11-17 01:37:42 +0000639 if (prefix_path.str().empty()) {
640 PrivateAutoComplete(frame, partial_path.substr(1), "*", compiler_type,
641 matches, word_complete);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000642 }
643 break;
644
645 case '&':
Zachary Turner4aa87532016-11-17 01:37:42 +0000646 if (prefix_path.isTriviallyEmpty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000647 PrivateAutoComplete(frame, partial_path.substr(1), std::string("&"),
648 compiler_type, matches, word_complete);
649 }
650 break;
651
652 case '-':
Zachary Turner4aa87532016-11-17 01:37:42 +0000653 if (partial_path[1] == '>' && !prefix_path.str().empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000654 switch (type_class) {
655 case lldb::eTypeClassPointer: {
656 CompilerType pointee_type(compiler_type.GetPointeeType());
657 if (partial_path[2]) {
658 // If there is more after the "->", then search deeper
659 PrivateAutoComplete(
660 frame, partial_path.substr(2), prefix_path + "->",
661 pointee_type.GetCanonicalType(), matches, word_complete);
662 } else {
663 // Nothing after the "->", so list all members
664 PrivateAutoCompleteMembers(
665 frame, std::string(), std::string(), prefix_path + "->",
666 pointee_type.GetCanonicalType(), matches, word_complete);
667 }
668 } break;
Greg Clayton884fb692011-07-08 21:46:14 +0000669 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000670 break;
Greg Clayton884fb692011-07-08 21:46:14 +0000671 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000672 }
673 break;
Greg Clayton884fb692011-07-08 21:46:14 +0000674
Kate Stoneb9c1b512016-09-06 20:57:50 +0000675 case '.':
676 if (compiler_type.IsValid()) {
677 switch (type_class) {
678 case lldb::eTypeClassUnion:
679 case lldb::eTypeClassStruct:
680 case lldb::eTypeClassClass:
681 if (partial_path[1]) {
682 // If there is more after the ".", then search deeper
683 PrivateAutoComplete(frame, partial_path.substr(1),
684 prefix_path + ".", compiler_type, matches,
685 word_complete);
Greg Claytonafacd142011-09-02 01:15:17 +0000686
Kate Stoneb9c1b512016-09-06 20:57:50 +0000687 } else {
688 // Nothing after the ".", so list all members
689 PrivateAutoCompleteMembers(frame, std::string(), partial_path,
690 prefix_path + ".", compiler_type,
691 matches, word_complete);
692 }
693 break;
Greg Claytonf21fead2013-05-14 23:43:18 +0000694 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000695 break;
Greg Claytonf21fead2013-05-14 23:43:18 +0000696 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000697 }
698 break;
699 default:
700 if (isalpha(ch) || ch == '_' || ch == '$') {
701 const size_t partial_path_len = partial_path.size();
702 size_t pos = 1;
703 while (pos < partial_path_len) {
704 const char curr_ch = partial_path[pos];
705 if (isalnum(curr_ch) || curr_ch == '_' || curr_ch == '$') {
706 ++pos;
707 continue;
708 }
709 break;
710 }
711
712 std::string token(partial_path, 0, pos);
713 remaining_partial_path = partial_path.substr(pos);
714
715 if (compiler_type.IsValid()) {
716 PrivateAutoCompleteMembers(frame, token, remaining_partial_path,
717 prefix_path, compiler_type, matches,
718 word_complete);
719 } else if (frame) {
720 // We haven't found our variable yet
721 const bool get_file_globals = true;
722
723 VariableList *variable_list =
724 frame->GetVariableList(get_file_globals);
725
726 if (!variable_list)
727 break;
728
729 const size_t num_variables = variable_list->GetSize();
730 for (size_t i = 0; i < num_variables; ++i) {
731 Variable *variable = variable_list->GetVariableAtIndex(i).get();
732
733 if (!variable)
734 continue;
735
736 const char *variable_name = variable->GetName().AsCString();
737 if (strstr(variable_name, token.c_str()) == variable_name) {
738 if (strcmp(variable_name, token.c_str()) == 0) {
739 Type *variable_type = variable->GetType();
740 if (variable_type) {
741 CompilerType variable_compiler_type(
742 variable_type->GetForwardCompilerType());
743 PrivateAutoComplete(
744 frame, remaining_partial_path,
745 prefix_path + token, // Anything that has been resolved
746 // already will be in here
747 variable_compiler_type.GetCanonicalType(), matches,
748 word_complete);
749 } else {
Zachary Turner4aa87532016-11-17 01:37:42 +0000750 matches.AppendString((prefix_path + variable_name).str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000751 }
752 } else if (remaining_partial_path.empty()) {
Zachary Turner4aa87532016-11-17 01:37:42 +0000753 matches.AppendString((prefix_path + variable_name).str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000754 }
755 }
756 }
757 }
758 }
759 break;
Greg Claytonf21fead2013-05-14 23:43:18 +0000760 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000761 }
Greg Claytonf21fead2013-05-14 23:43:18 +0000762}
763
Kate Stoneb9c1b512016-09-06 20:57:50 +0000764size_t Variable::AutoComplete(const ExecutionContext &exe_ctx,
Zachary Turner4aa87532016-11-17 01:37:42 +0000765 llvm::StringRef partial_path, StringList &matches,
766 bool &word_complete) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000767 word_complete = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000768 CompilerType compiler_type;
Greg Claytonf21fead2013-05-14 23:43:18 +0000769
Zachary Turner4aa87532016-11-17 01:37:42 +0000770 PrivateAutoComplete(exe_ctx.GetFramePtr(), partial_path, "", compiler_type,
771 matches, word_complete);
Greg Claytonf21fead2013-05-14 23:43:18 +0000772
Kate Stoneb9c1b512016-09-06 20:57:50 +0000773 return matches.GetSize();
Greg Claytonc749eb82011-07-11 05:12:02 +0000774}