]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Symbol/Variable.cpp
Merge lldb trunk r366426, resolve conflicts, and update FREEBSD-Xlist.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Symbol / Variable.cpp
1 //===-- Variable.cpp --------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "lldb/Symbol/Variable.h"
10
11 #include "lldb/Core/Module.h"
12 #include "lldb/Core/ValueObject.h"
13 #include "lldb/Core/ValueObjectVariable.h"
14 #include "lldb/Symbol/Block.h"
15 #include "lldb/Symbol/CompileUnit.h"
16 #include "lldb/Symbol/CompilerDecl.h"
17 #include "lldb/Symbol/CompilerDeclContext.h"
18 #include "lldb/Symbol/Function.h"
19 #include "lldb/Symbol/SymbolContext.h"
20 #include "lldb/Symbol/SymbolFile.h"
21 #include "lldb/Symbol/Type.h"
22 #include "lldb/Symbol/TypeSystem.h"
23 #include "lldb/Symbol/VariableList.h"
24 #include "lldb/Target/ABI.h"
25 #include "lldb/Target/Process.h"
26 #include "lldb/Target/RegisterContext.h"
27 #include "lldb/Target/StackFrame.h"
28 #include "lldb/Target/Target.h"
29 #include "lldb/Target/Thread.h"
30 #include "lldb/Utility/RegularExpression.h"
31 #include "lldb/Utility/Stream.h"
32
33 #include "llvm/ADT/Twine.h"
34
35 using namespace lldb;
36 using namespace lldb_private;
37
38 // Variable constructor
39 Variable::Variable(
40     lldb::user_id_t uid, const char *name,
41     const char *mangled, // The mangled or fully qualified name of the variable.
42     const lldb::SymbolFileTypeSP &symfile_type_sp, ValueType scope,
43     SymbolContextScope *context, const RangeList &scope_range,
44     Declaration *decl_ptr, const DWARFExpression &location, bool external,
45     bool artificial, bool static_member)
46     : UserID(uid), m_name(name), m_mangled(ConstString(mangled)),
47       m_symfile_type_sp(symfile_type_sp), m_scope(scope),
48       m_owner_scope(context), m_scope_range(scope_range),
49       m_declaration(decl_ptr), m_location(location), m_external(external),
50       m_artificial(artificial), m_loc_is_const_data(false),
51       m_static_member(static_member) {}
52
53 // Destructor
54 Variable::~Variable() {}
55
56 lldb::LanguageType Variable::GetLanguage() const {
57   SymbolContext variable_sc;
58   m_owner_scope->CalculateSymbolContext(&variable_sc);
59   if (variable_sc.comp_unit)
60     return variable_sc.comp_unit->GetLanguage();
61   return lldb::eLanguageTypeUnknown;
62 }
63
64 ConstString Variable::GetName() const {
65   ConstString name = m_mangled.GetName(GetLanguage());
66   if (name)
67     return name;
68   return m_name;
69 }
70
71 ConstString Variable::GetUnqualifiedName() const { return m_name; }
72
73 bool Variable::NameMatches(ConstString name) const {
74   if (m_name == name)
75     return true;
76   SymbolContext variable_sc;
77   m_owner_scope->CalculateSymbolContext(&variable_sc);
78
79   LanguageType language = eLanguageTypeUnknown;
80   if (variable_sc.comp_unit)
81     language = variable_sc.comp_unit->GetLanguage();
82   return m_mangled.NameMatches(name, language);
83 }
84 bool Variable::NameMatches(const RegularExpression &regex) const {
85   if (regex.Execute(m_name.AsCString()))
86     return true;
87   if (m_mangled)
88     return m_mangled.NameMatches(regex, GetLanguage());
89   return false;
90 }
91
92 Type *Variable::GetType() {
93   if (m_symfile_type_sp)
94     return m_symfile_type_sp->GetType();
95   return nullptr;
96 }
97
98 void Variable::Dump(Stream *s, bool show_context) const {
99   s->Printf("%p: ", static_cast<const void *>(this));
100   s->Indent();
101   *s << "Variable" << (const UserID &)*this;
102
103   if (m_name)
104     *s << ", name = \"" << m_name << "\"";
105
106   if (m_symfile_type_sp) {
107     Type *type = m_symfile_type_sp->GetType();
108     if (type) {
109       *s << ", type = {" << type->GetID() << "} " << (void *)type << " (";
110       type->DumpTypeName(s);
111       s->PutChar(')');
112     }
113   }
114
115   if (m_scope != eValueTypeInvalid) {
116     s->PutCString(", scope = ");
117     switch (m_scope) {
118     case eValueTypeVariableGlobal:
119       s->PutCString(m_external ? "global" : "static");
120       break;
121     case eValueTypeVariableArgument:
122       s->PutCString("parameter");
123       break;
124     case eValueTypeVariableLocal:
125       s->PutCString("local");
126       break;
127     case eValueTypeVariableThreadLocal:
128       s->PutCString("thread local");
129       break;
130     default:
131       *s << "??? (" << m_scope << ')';
132     }
133   }
134
135   if (show_context && m_owner_scope != nullptr) {
136     s->PutCString(", context = ( ");
137     m_owner_scope->DumpSymbolContext(s);
138     s->PutCString(" )");
139   }
140
141   bool show_fullpaths = false;
142   m_declaration.Dump(s, show_fullpaths);
143
144   if (m_location.IsValid()) {
145     s->PutCString(", location = ");
146     lldb::addr_t loclist_base_addr = LLDB_INVALID_ADDRESS;
147     if (m_location.IsLocationList()) {
148       SymbolContext variable_sc;
149       m_owner_scope->CalculateSymbolContext(&variable_sc);
150       if (variable_sc.function)
151         loclist_base_addr = variable_sc.function->GetAddressRange()
152                                 .GetBaseAddress()
153                                 .GetFileAddress();
154     }
155     ABISP abi;
156     if (m_owner_scope) {
157       ModuleSP module_sp(m_owner_scope->CalculateSymbolContextModule());
158       if (module_sp)
159         abi = ABI::FindPlugin(ProcessSP(), module_sp->GetArchitecture());
160     }
161     m_location.GetDescription(s, lldb::eDescriptionLevelBrief,
162                               loclist_base_addr, abi.get());
163   }
164
165   if (m_external)
166     s->PutCString(", external");
167
168   if (m_artificial)
169     s->PutCString(", artificial");
170
171   s->EOL();
172 }
173
174 bool Variable::DumpDeclaration(Stream *s, bool show_fullpaths,
175                                bool show_module) {
176   bool dumped_declaration_info = false;
177   if (m_owner_scope) {
178     SymbolContext sc;
179     m_owner_scope->CalculateSymbolContext(&sc);
180     sc.block = nullptr;
181     sc.line_entry.Clear();
182     bool show_inlined_frames = false;
183     const bool show_function_arguments = true;
184     const bool show_function_name = true;
185
186     dumped_declaration_info = sc.DumpStopContext(
187         s, nullptr, Address(), show_fullpaths, show_module, show_inlined_frames,
188         show_function_arguments, show_function_name);
189
190     if (sc.function)
191       s->PutChar(':');
192   }
193   if (m_declaration.DumpStopContext(s, false))
194     dumped_declaration_info = true;
195   return dumped_declaration_info;
196 }
197
198 size_t Variable::MemorySize() const { return sizeof(Variable); }
199
200 CompilerDeclContext Variable::GetDeclContext() {
201   Type *type = GetType();
202   if (type)
203     return type->GetSymbolFile()->GetDeclContextContainingUID(GetID());
204   return CompilerDeclContext();
205 }
206
207 CompilerDecl Variable::GetDecl() {
208   Type *type = GetType();
209   return type ? type->GetSymbolFile()->GetDeclForUID(GetID()) : CompilerDecl();
210 }
211
212 void Variable::CalculateSymbolContext(SymbolContext *sc) {
213   if (m_owner_scope) {
214     m_owner_scope->CalculateSymbolContext(sc);
215     sc->variable = this;
216   } else
217     sc->Clear(false);
218 }
219
220 bool Variable::LocationIsValidForFrame(StackFrame *frame) {
221   // Is the variable is described by a single location?
222   if (!m_location.IsLocationList()) {
223     // Yes it is, the location is valid.
224     return true;
225   }
226
227   if (frame) {
228     Function *function =
229         frame->GetSymbolContext(eSymbolContextFunction).function;
230     if (function) {
231       TargetSP target_sp(frame->CalculateTarget());
232
233       addr_t loclist_base_load_addr =
234           function->GetAddressRange().GetBaseAddress().GetLoadAddress(
235               target_sp.get());
236       if (loclist_base_load_addr == LLDB_INVALID_ADDRESS)
237         return false;
238       // It is a location list. We just need to tell if the location list
239       // contains the current address when converted to a load address
240       return m_location.LocationListContainsAddress(
241           loclist_base_load_addr,
242           frame->GetFrameCodeAddress().GetLoadAddress(target_sp.get()));
243     }
244   }
245   return false;
246 }
247
248 bool Variable::LocationIsValidForAddress(const Address &address) {
249   // Be sure to resolve the address to section offset prior to calling this
250   // function.
251   if (address.IsSectionOffset()) {
252     SymbolContext sc;
253     CalculateSymbolContext(&sc);
254     if (sc.module_sp == address.GetModule()) {
255       // Is the variable is described by a single location?
256       if (!m_location.IsLocationList()) {
257         // Yes it is, the location is valid.
258         return true;
259       }
260
261       if (sc.function) {
262         addr_t loclist_base_file_addr =
263             sc.function->GetAddressRange().GetBaseAddress().GetFileAddress();
264         if (loclist_base_file_addr == LLDB_INVALID_ADDRESS)
265           return false;
266         // It is a location list. We just need to tell if the location list
267         // contains the current address when converted to a load address
268         return m_location.LocationListContainsAddress(loclist_base_file_addr,
269                                                       address.GetFileAddress());
270       }
271     }
272   }
273   return false;
274 }
275
276 bool Variable::IsInScope(StackFrame *frame) {
277   switch (m_scope) {
278   case eValueTypeRegister:
279   case eValueTypeRegisterSet:
280     return frame != nullptr;
281
282   case eValueTypeConstResult:
283   case eValueTypeVariableGlobal:
284   case eValueTypeVariableStatic:
285   case eValueTypeVariableThreadLocal:
286     return true;
287
288   case eValueTypeVariableArgument:
289   case eValueTypeVariableLocal:
290     if (frame) {
291       // We don't have a location list, we just need to see if the block that
292       // this variable was defined in is currently
293       Block *deepest_frame_block =
294           frame->GetSymbolContext(eSymbolContextBlock).block;
295       if (deepest_frame_block) {
296         SymbolContext variable_sc;
297         CalculateSymbolContext(&variable_sc);
298
299         // Check for static or global variable defined at the compile unit
300         // level that wasn't defined in a block
301         if (variable_sc.block == nullptr)
302           return true;
303
304         // Check if the variable is valid in the current block
305         if (variable_sc.block != deepest_frame_block &&
306             !variable_sc.block->Contains(deepest_frame_block))
307           return false;
308
309         // If no scope range is specified then it means that the scope is the
310         // same as the scope of the enclosing lexical block.
311         if (m_scope_range.IsEmpty())
312           return true;
313
314         addr_t file_address = frame->GetFrameCodeAddress().GetFileAddress();
315         return m_scope_range.FindEntryThatContains(file_address) != nullptr;
316       }
317     }
318     break;
319
320   default:
321     break;
322   }
323   return false;
324 }
325
326 Status Variable::GetValuesForVariableExpressionPath(
327     llvm::StringRef variable_expr_path, ExecutionContextScope *scope,
328     GetVariableCallback callback, void *baton, VariableList &variable_list,
329     ValueObjectList &valobj_list) {
330   Status error;
331   if (!callback || variable_expr_path.empty()) {
332     error.SetErrorString("unknown error");
333     return error;
334   }
335
336   switch (variable_expr_path.front()) {
337   case '*':
338     error = Variable::GetValuesForVariableExpressionPath(
339         variable_expr_path.drop_front(), scope, callback, baton, variable_list,
340         valobj_list);
341     if (error.Fail()) {
342       error.SetErrorString("unknown error");
343       return error;
344     }
345     for (uint32_t i = 0; i < valobj_list.GetSize();) {
346       Status tmp_error;
347       ValueObjectSP valobj_sp(
348           valobj_list.GetValueObjectAtIndex(i)->Dereference(tmp_error));
349       if (tmp_error.Fail()) {
350         variable_list.RemoveVariableAtIndex(i);
351         valobj_list.RemoveValueObjectAtIndex(i);
352       } else {
353         valobj_list.SetValueObjectAtIndex(i, valobj_sp);
354         ++i;
355       }
356     }
357     return error;
358   case '&': {
359     error = Variable::GetValuesForVariableExpressionPath(
360         variable_expr_path.drop_front(), scope, callback, baton, variable_list,
361         valobj_list);
362     if (error.Success()) {
363       for (uint32_t i = 0; i < valobj_list.GetSize();) {
364         Status tmp_error;
365         ValueObjectSP valobj_sp(
366             valobj_list.GetValueObjectAtIndex(i)->AddressOf(tmp_error));
367         if (tmp_error.Fail()) {
368           variable_list.RemoveVariableAtIndex(i);
369           valobj_list.RemoveValueObjectAtIndex(i);
370         } else {
371           valobj_list.SetValueObjectAtIndex(i, valobj_sp);
372           ++i;
373         }
374       }
375     } else {
376       error.SetErrorString("unknown error");
377     }
378     return error;
379   } break;
380
381   default: {
382     static RegularExpression g_regex(
383         llvm::StringRef("^([A-Za-z_:][A-Za-z_0-9:]*)(.*)"));
384     RegularExpression::Match regex_match(1);
385     std::string variable_name;
386     variable_list.Clear();
387     if (!g_regex.Execute(variable_expr_path, &regex_match)) {
388       error.SetErrorStringWithFormat(
389           "unable to extract a variable name from '%s'",
390           variable_expr_path.str().c_str());
391       return error;
392     }
393     if (!regex_match.GetMatchAtIndex(variable_expr_path, 1, variable_name)) {
394       error.SetErrorStringWithFormat(
395           "unable to extract a variable name from '%s'",
396           variable_expr_path.str().c_str());
397       return error;
398     }
399     if (!callback(baton, variable_name.c_str(), variable_list)) {
400       error.SetErrorString("unknown error");
401       return error;
402     }
403     uint32_t i = 0;
404     while (i < variable_list.GetSize()) {
405       VariableSP var_sp(variable_list.GetVariableAtIndex(i));
406       ValueObjectSP valobj_sp;
407       if (!var_sp) {
408         variable_list.RemoveVariableAtIndex(i);
409         continue;
410       }
411       ValueObjectSP variable_valobj_sp(
412           ValueObjectVariable::Create(scope, var_sp));
413       if (!variable_valobj_sp) {
414         variable_list.RemoveVariableAtIndex(i);
415         continue;
416       }
417
418       llvm::StringRef variable_sub_expr_path =
419           variable_expr_path.drop_front(variable_name.size());
420       if (!variable_sub_expr_path.empty()) {
421         valobj_sp = variable_valobj_sp->GetValueForExpressionPath(
422             variable_sub_expr_path);
423         if (!valobj_sp) {
424           error.SetErrorStringWithFormat(
425               "invalid expression path '%s' for variable '%s'",
426               variable_sub_expr_path.str().c_str(),
427               var_sp->GetName().GetCString());
428           variable_list.RemoveVariableAtIndex(i);
429           continue;
430         }
431       } else {
432         // Just the name of a variable with no extras
433         valobj_sp = variable_valobj_sp;
434       }
435
436       valobj_list.Append(valobj_sp);
437       ++i;
438     }
439
440     if (variable_list.GetSize() > 0) {
441       error.Clear();
442       return error;
443     }
444   } break;
445   }
446   error.SetErrorString("unknown error");
447   return error;
448 }
449
450 bool Variable::DumpLocationForAddress(Stream *s, const Address &address) {
451   // Be sure to resolve the address to section offset prior to calling this
452   // function.
453   if (address.IsSectionOffset()) {
454     SymbolContext sc;
455     CalculateSymbolContext(&sc);
456     if (sc.module_sp == address.GetModule()) {
457       ABISP abi;
458       if (m_owner_scope) {
459         ModuleSP module_sp(m_owner_scope->CalculateSymbolContextModule());
460         if (module_sp)
461           abi = ABI::FindPlugin(ProcessSP(), module_sp->GetArchitecture());
462       }
463
464       const addr_t file_addr = address.GetFileAddress();
465       if (sc.function) {
466         if (sc.function->GetAddressRange().ContainsFileAddress(address)) {
467           addr_t loclist_base_file_addr =
468               sc.function->GetAddressRange().GetBaseAddress().GetFileAddress();
469           if (loclist_base_file_addr == LLDB_INVALID_ADDRESS)
470             return false;
471           return m_location.DumpLocationForAddress(s, eDescriptionLevelBrief,
472                                                    loclist_base_file_addr,
473                                                    file_addr, abi.get());
474         }
475       }
476       return m_location.DumpLocationForAddress(s, eDescriptionLevelBrief,
477                                                LLDB_INVALID_ADDRESS, file_addr,
478                                                abi.get());
479     }
480   }
481   return false;
482 }
483
484 static void PrivateAutoComplete(
485     StackFrame *frame, llvm::StringRef partial_path,
486     const llvm::Twine
487         &prefix_path, // Anything that has been resolved already will be in here
488     const CompilerType &compiler_type,
489     StringList &matches, bool &word_complete);
490
491 static void PrivateAutoCompleteMembers(
492     StackFrame *frame, const std::string &partial_member_name,
493     llvm::StringRef partial_path,
494     const llvm::Twine
495         &prefix_path, // Anything that has been resolved already will be in here
496     const CompilerType &compiler_type,
497     StringList &matches, bool &word_complete);
498
499 static void PrivateAutoCompleteMembers(
500     StackFrame *frame, const std::string &partial_member_name,
501     llvm::StringRef partial_path,
502     const llvm::Twine
503         &prefix_path, // Anything that has been resolved already will be in here
504     const CompilerType &compiler_type,
505     StringList &matches, bool &word_complete) {
506
507   // We are in a type parsing child members
508   const uint32_t num_bases = compiler_type.GetNumDirectBaseClasses();
509
510   if (num_bases > 0) {
511     for (uint32_t i = 0; i < num_bases; ++i) {
512       CompilerType base_class_type =
513           compiler_type.GetDirectBaseClassAtIndex(i, nullptr);
514
515       PrivateAutoCompleteMembers(
516           frame, partial_member_name, partial_path, prefix_path,
517           base_class_type.GetCanonicalType(), matches, word_complete);
518     }
519   }
520
521   const uint32_t num_vbases = compiler_type.GetNumVirtualBaseClasses();
522
523   if (num_vbases > 0) {
524     for (uint32_t i = 0; i < num_vbases; ++i) {
525       CompilerType vbase_class_type =
526           compiler_type.GetVirtualBaseClassAtIndex(i, nullptr);
527
528       PrivateAutoCompleteMembers(
529           frame, partial_member_name, partial_path, prefix_path,
530           vbase_class_type.GetCanonicalType(), matches, word_complete);
531     }
532   }
533
534   // We are in a type parsing child members
535   const uint32_t num_fields = compiler_type.GetNumFields();
536
537   if (num_fields > 0) {
538     for (uint32_t i = 0; i < num_fields; ++i) {
539       std::string member_name;
540
541       CompilerType member_compiler_type = compiler_type.GetFieldAtIndex(
542           i, member_name, nullptr, nullptr, nullptr);
543
544       if (partial_member_name.empty() ||
545           member_name.find(partial_member_name) == 0) {
546         if (member_name == partial_member_name) {
547           PrivateAutoComplete(
548               frame, partial_path,
549               prefix_path + member_name, // Anything that has been resolved
550                                          // already will be in here
551               member_compiler_type.GetCanonicalType(), matches, word_complete);
552         } else {
553           matches.AppendString((prefix_path + member_name).str());
554         }
555       }
556     }
557   }
558 }
559
560 static void PrivateAutoComplete(
561     StackFrame *frame, llvm::StringRef partial_path,
562     const llvm::Twine
563         &prefix_path, // Anything that has been resolved already will be in here
564     const CompilerType &compiler_type,
565     StringList &matches, bool &word_complete) {
566   //    printf ("\nPrivateAutoComplete()\n\tprefix_path = '%s'\n\tpartial_path =
567   //    '%s'\n", prefix_path.c_str(), partial_path.c_str());
568   std::string remaining_partial_path;
569
570   const lldb::TypeClass type_class = compiler_type.GetTypeClass();
571   if (partial_path.empty()) {
572     if (compiler_type.IsValid()) {
573       switch (type_class) {
574       default:
575       case eTypeClassArray:
576       case eTypeClassBlockPointer:
577       case eTypeClassBuiltin:
578       case eTypeClassComplexFloat:
579       case eTypeClassComplexInteger:
580       case eTypeClassEnumeration:
581       case eTypeClassFunction:
582       case eTypeClassMemberPointer:
583       case eTypeClassReference:
584       case eTypeClassTypedef:
585       case eTypeClassVector: {
586         matches.AppendString(prefix_path.str());
587         word_complete = matches.GetSize() == 1;
588       } break;
589
590       case eTypeClassClass:
591       case eTypeClassStruct:
592       case eTypeClassUnion:
593         if (prefix_path.str().back() != '.')
594           matches.AppendString((prefix_path + ".").str());
595         break;
596
597       case eTypeClassObjCObject:
598       case eTypeClassObjCInterface:
599         break;
600       case eTypeClassObjCObjectPointer:
601       case eTypeClassPointer: {
602         bool omit_empty_base_classes = true;
603         if (compiler_type.GetNumChildren(omit_empty_base_classes, nullptr) > 0)
604           matches.AppendString((prefix_path + "->").str());
605         else {
606           matches.AppendString(prefix_path.str());
607           word_complete = true;
608         }
609       } break;
610       }
611     } else {
612       if (frame) {
613         const bool get_file_globals = true;
614
615         VariableList *variable_list = frame->GetVariableList(get_file_globals);
616
617         if (variable_list) {
618           const size_t num_variables = variable_list->GetSize();
619           for (size_t i = 0; i < num_variables; ++i) {
620             Variable *variable = variable_list->GetVariableAtIndex(i).get();
621             matches.AppendString(variable->GetName().AsCString());
622           }
623         }
624       }
625     }
626   } else {
627     const char ch = partial_path[0];
628     switch (ch) {
629     case '*':
630       if (prefix_path.str().empty()) {
631         PrivateAutoComplete(frame, partial_path.substr(1), "*", compiler_type,
632                             matches, word_complete);
633       }
634       break;
635
636     case '&':
637       if (prefix_path.isTriviallyEmpty()) {
638         PrivateAutoComplete(frame, partial_path.substr(1), std::string("&"),
639                             compiler_type, matches, word_complete);
640       }
641       break;
642
643     case '-':
644       if (partial_path.size() > 1 && partial_path[1] == '>' &&
645           !prefix_path.str().empty()) {
646         switch (type_class) {
647         case lldb::eTypeClassPointer: {
648           CompilerType pointee_type(compiler_type.GetPointeeType());
649           if (partial_path.size() > 2 && partial_path[2]) {
650             // If there is more after the "->", then search deeper
651             PrivateAutoComplete(
652                 frame, partial_path.substr(2), prefix_path + "->",
653                 pointee_type.GetCanonicalType(), matches, word_complete);
654           } else {
655             // Nothing after the "->", so list all members
656             PrivateAutoCompleteMembers(
657                 frame, std::string(), std::string(), prefix_path + "->",
658                 pointee_type.GetCanonicalType(), matches, word_complete);
659           }
660         } break;
661         default:
662           break;
663         }
664       }
665       break;
666
667     case '.':
668       if (compiler_type.IsValid()) {
669         switch (type_class) {
670         case lldb::eTypeClassUnion:
671         case lldb::eTypeClassStruct:
672         case lldb::eTypeClassClass:
673           if (partial_path.size() > 1 && partial_path[1]) {
674             // If there is more after the ".", then search deeper
675             PrivateAutoComplete(frame, partial_path.substr(1),
676                                 prefix_path + ".", compiler_type, matches,
677                                 word_complete);
678
679           } else {
680             // Nothing after the ".", so list all members
681             PrivateAutoCompleteMembers(frame, std::string(), partial_path,
682                                        prefix_path + ".", compiler_type,
683                                        matches, word_complete);
684           }
685           break;
686         default:
687           break;
688         }
689       }
690       break;
691     default:
692       if (isalpha(ch) || ch == '_' || ch == '$') {
693         const size_t partial_path_len = partial_path.size();
694         size_t pos = 1;
695         while (pos < partial_path_len) {
696           const char curr_ch = partial_path[pos];
697           if (isalnum(curr_ch) || curr_ch == '_' || curr_ch == '$') {
698             ++pos;
699             continue;
700           }
701           break;
702         }
703
704         std::string token(partial_path, 0, pos);
705         remaining_partial_path = partial_path.substr(pos);
706
707         if (compiler_type.IsValid()) {
708           PrivateAutoCompleteMembers(frame, token, remaining_partial_path,
709                                      prefix_path, compiler_type, matches,
710                                      word_complete);
711         } else if (frame) {
712           // We haven't found our variable yet
713           const bool get_file_globals = true;
714
715           VariableList *variable_list =
716               frame->GetVariableList(get_file_globals);
717
718           if (!variable_list)
719             break;
720
721           const size_t num_variables = variable_list->GetSize();
722           for (size_t i = 0; i < num_variables; ++i) {
723             Variable *variable = variable_list->GetVariableAtIndex(i).get();
724
725             if (!variable)
726               continue;
727
728             const char *variable_name = variable->GetName().AsCString();
729             if (strstr(variable_name, token.c_str()) == variable_name) {
730               if (strcmp(variable_name, token.c_str()) == 0) {
731                 Type *variable_type = variable->GetType();
732                 if (variable_type) {
733                   CompilerType variable_compiler_type(
734                       variable_type->GetForwardCompilerType());
735                   PrivateAutoComplete(
736                       frame, remaining_partial_path,
737                       prefix_path + token, // Anything that has been resolved
738                                            // already will be in here
739                       variable_compiler_type.GetCanonicalType(), matches,
740                       word_complete);
741                 } else {
742                   matches.AppendString((prefix_path + variable_name).str());
743                 }
744               } else if (remaining_partial_path.empty()) {
745                 matches.AppendString((prefix_path + variable_name).str());
746               }
747             }
748           }
749         }
750       }
751       break;
752     }
753   }
754 }
755
756 size_t Variable::AutoComplete(const ExecutionContext &exe_ctx,
757                               CompletionRequest &request) {
758   CompilerType compiler_type;
759
760   bool word_complete = false;
761   StringList matches;
762   PrivateAutoComplete(exe_ctx.GetFramePtr(), request.GetCursorArgumentPrefix(),
763                       "", compiler_type, matches, word_complete);
764   request.SetWordComplete(word_complete);
765   request.AddCompletions(matches);
766
767   return request.GetNumberOfMatches();
768 }