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