1 //===-- OptionValue.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/Interpreter/OptionValue.h" 11 12 // C Includes 13 // C++ Includes 14 // Other libraries and framework includes 15 // Project includes 16 #include "lldb/Core/StringList.h" 17 #include "lldb/Interpreter/OptionValues.h" 18 19 using namespace lldb; 20 using namespace lldb_private; 21 22 23 //------------------------------------------------------------------------- 24 // Get this value as a uint64_t value if it is encoded as a boolean, 25 // uint64_t or int64_t. Other types will cause "fail_value" to be 26 // returned 27 //------------------------------------------------------------------------- 28 uint64_t 29 OptionValue::GetUInt64Value (uint64_t fail_value, bool *success_ptr) 30 { 31 if (success_ptr) 32 *success_ptr = true; 33 switch (GetType()) 34 { 35 case OptionValue::eTypeBoolean: return static_cast<OptionValueBoolean *>(this)->GetCurrentValue(); 36 case OptionValue::eTypeSInt64: return static_cast<OptionValueSInt64 *>(this)->GetCurrentValue(); 37 case OptionValue::eTypeUInt64: return static_cast<OptionValueUInt64 *>(this)->GetCurrentValue(); 38 default: 39 break; 40 } 41 if (success_ptr) 42 *success_ptr = false; 43 return fail_value; 44 } 45 46 Error 47 OptionValue::SetSubValue (const ExecutionContext *exe_ctx, 48 VarSetOperationType op, 49 const char *name, 50 const char *value) 51 { 52 Error error; 53 error.SetErrorStringWithFormat("SetSubValue is not supported"); 54 return error; 55 } 56 57 58 OptionValueBoolean * 59 OptionValue::GetAsBoolean () 60 { 61 if (GetType () == OptionValue::eTypeBoolean) 62 return static_cast<OptionValueBoolean *>(this); 63 return nullptr; 64 } 65 66 const OptionValueBoolean * 67 OptionValue::GetAsBoolean () const 68 { 69 if (GetType () == OptionValue::eTypeBoolean) 70 return static_cast<const OptionValueBoolean *>(this); 71 return nullptr; 72 } 73 74 const OptionValueChar * 75 OptionValue::GetAsChar () const 76 { 77 if (GetType () == OptionValue::eTypeChar) 78 return static_cast<const OptionValueChar *>(this); 79 return nullptr; 80 } 81 82 OptionValueChar * 83 OptionValue::GetAsChar () 84 { 85 if (GetType () == OptionValue::eTypeChar) 86 return static_cast<OptionValueChar *>(this); 87 return nullptr; 88 } 89 90 OptionValueFileSpec * 91 OptionValue::GetAsFileSpec () 92 { 93 if (GetType () == OptionValue::eTypeFileSpec) 94 return static_cast<OptionValueFileSpec *>(this); 95 return nullptr; 96 97 } 98 99 const OptionValueFileSpec * 100 OptionValue::GetAsFileSpec () const 101 { 102 if (GetType () == OptionValue::eTypeFileSpec) 103 return static_cast<const OptionValueFileSpec *>(this); 104 return nullptr; 105 106 } 107 108 OptionValueFileSpecList * 109 OptionValue::GetAsFileSpecList () 110 { 111 if (GetType () == OptionValue::eTypeFileSpecList) 112 return static_cast<OptionValueFileSpecList *>(this); 113 return nullptr; 114 115 } 116 117 const OptionValueFileSpecList * 118 OptionValue::GetAsFileSpecList () const 119 { 120 if (GetType () == OptionValue::eTypeFileSpecList) 121 return static_cast<const OptionValueFileSpecList *>(this); 122 return nullptr; 123 124 } 125 126 OptionValueArch * 127 OptionValue::GetAsArch () 128 { 129 if (GetType () == OptionValue::eTypeArch) 130 return static_cast<OptionValueArch *>(this); 131 return nullptr; 132 } 133 134 135 const OptionValueArch * 136 OptionValue::GetAsArch () const 137 { 138 if (GetType () == OptionValue::eTypeArch) 139 return static_cast<const OptionValueArch *>(this); 140 return nullptr; 141 } 142 143 OptionValueArray * 144 OptionValue::GetAsArray () 145 { 146 if (GetType () == OptionValue::eTypeArray) 147 return static_cast<OptionValueArray *>(this); 148 return nullptr; 149 } 150 151 152 const OptionValueArray * 153 OptionValue::GetAsArray () const 154 { 155 if (GetType () == OptionValue::eTypeArray) 156 return static_cast<const OptionValueArray *>(this); 157 return nullptr; 158 } 159 160 OptionValueArgs * 161 OptionValue::GetAsArgs () 162 { 163 if (GetType () == OptionValue::eTypeArgs) 164 return static_cast<OptionValueArgs *>(this); 165 return nullptr; 166 } 167 168 169 const OptionValueArgs * 170 OptionValue::GetAsArgs () const 171 { 172 if (GetType () == OptionValue::eTypeArgs) 173 return static_cast<const OptionValueArgs *>(this); 174 return nullptr; 175 } 176 177 OptionValueDictionary * 178 OptionValue::GetAsDictionary () 179 { 180 if (GetType () == OptionValue::eTypeDictionary) 181 return static_cast<OptionValueDictionary *>(this); 182 return nullptr; 183 } 184 185 const OptionValueDictionary * 186 OptionValue::GetAsDictionary () const 187 { 188 if (GetType () == OptionValue::eTypeDictionary) 189 return static_cast<const OptionValueDictionary *>(this); 190 return nullptr; 191 } 192 193 OptionValueEnumeration * 194 OptionValue::GetAsEnumeration () 195 { 196 if (GetType () == OptionValue::eTypeEnum) 197 return static_cast<OptionValueEnumeration *>(this); 198 return nullptr; 199 } 200 201 const OptionValueEnumeration * 202 OptionValue::GetAsEnumeration () const 203 { 204 if (GetType () == OptionValue::eTypeEnum) 205 return static_cast<const OptionValueEnumeration *>(this); 206 return nullptr; 207 } 208 209 OptionValueFormat * 210 OptionValue::GetAsFormat () 211 { 212 if (GetType () == OptionValue::eTypeFormat) 213 return static_cast<OptionValueFormat *>(this); 214 return nullptr; 215 } 216 217 const OptionValueFormat * 218 OptionValue::GetAsFormat () const 219 { 220 if (GetType () == OptionValue::eTypeFormat) 221 return static_cast<const OptionValueFormat *>(this); 222 return nullptr; 223 } 224 225 OptionValueLanguage * 226 OptionValue::GetAsLanguage () 227 { 228 if (GetType () == OptionValue::eTypeLanguage) 229 return static_cast<OptionValueLanguage *>(this); 230 return NULL; 231 } 232 233 const OptionValueLanguage * 234 OptionValue::GetAsLanguage () const 235 { 236 if (GetType () == OptionValue::eTypeLanguage) 237 return static_cast<const OptionValueLanguage *>(this); 238 return NULL; 239 } 240 241 OptionValueFormatEntity * 242 OptionValue::GetAsFormatEntity () 243 { 244 if (GetType () == OptionValue::eTypeFormatEntity) 245 return static_cast<OptionValueFormatEntity *>(this); 246 return nullptr; 247 } 248 249 const OptionValueFormatEntity * 250 OptionValue::GetAsFormatEntity () const 251 { 252 if (GetType () == OptionValue::eTypeFormatEntity) 253 return static_cast<const OptionValueFormatEntity *>(this); 254 return nullptr; 255 } 256 257 OptionValuePathMappings * 258 OptionValue::GetAsPathMappings () 259 { 260 if (GetType () == OptionValue::eTypePathMap) 261 return static_cast<OptionValuePathMappings *>(this); 262 return nullptr; 263 } 264 265 const OptionValuePathMappings * 266 OptionValue::GetAsPathMappings () const 267 { 268 if (GetType () == OptionValue::eTypePathMap) 269 return static_cast<const OptionValuePathMappings *>(this); 270 return nullptr; 271 } 272 273 OptionValueProperties * 274 OptionValue::GetAsProperties () 275 { 276 if (GetType () == OptionValue::eTypeProperties) 277 return static_cast<OptionValueProperties *>(this); 278 return nullptr; 279 } 280 281 const OptionValueProperties * 282 OptionValue::GetAsProperties () const 283 { 284 if (GetType () == OptionValue::eTypeProperties) 285 return static_cast<const OptionValueProperties *>(this); 286 return nullptr; 287 } 288 289 OptionValueRegex * 290 OptionValue::GetAsRegex () 291 { 292 if (GetType () == OptionValue::eTypeRegex) 293 return static_cast<OptionValueRegex *>(this); 294 return nullptr; 295 } 296 297 const OptionValueRegex * 298 OptionValue::GetAsRegex () const 299 { 300 if (GetType () == OptionValue::eTypeRegex) 301 return static_cast<const OptionValueRegex *>(this); 302 return nullptr; 303 } 304 305 OptionValueSInt64 * 306 OptionValue::GetAsSInt64 () 307 { 308 if (GetType () == OptionValue::eTypeSInt64) 309 return static_cast<OptionValueSInt64 *>(this); 310 return nullptr; 311 } 312 313 const OptionValueSInt64 * 314 OptionValue::GetAsSInt64 () const 315 { 316 if (GetType () == OptionValue::eTypeSInt64) 317 return static_cast<const OptionValueSInt64 *>(this); 318 return nullptr; 319 } 320 321 OptionValueString * 322 OptionValue::GetAsString () 323 { 324 if (GetType () == OptionValue::eTypeString) 325 return static_cast<OptionValueString *>(this); 326 return nullptr; 327 } 328 329 const OptionValueString * 330 OptionValue::GetAsString () const 331 { 332 if (GetType () == OptionValue::eTypeString) 333 return static_cast<const OptionValueString *>(this); 334 return nullptr; 335 } 336 337 OptionValueUInt64 * 338 OptionValue::GetAsUInt64 () 339 { 340 if (GetType () == OptionValue::eTypeUInt64) 341 return static_cast<OptionValueUInt64 *>(this); 342 return nullptr; 343 } 344 345 const OptionValueUInt64 * 346 OptionValue::GetAsUInt64 () const 347 { 348 if (GetType () == OptionValue::eTypeUInt64) 349 return static_cast<const OptionValueUInt64 *>(this); 350 return nullptr; 351 } 352 353 OptionValueUUID * 354 OptionValue::GetAsUUID () 355 { 356 if (GetType () == OptionValue::eTypeUUID) 357 return static_cast<OptionValueUUID *>(this); 358 return nullptr; 359 360 } 361 362 const OptionValueUUID * 363 OptionValue::GetAsUUID () const 364 { 365 if (GetType () == OptionValue::eTypeUUID) 366 return static_cast<const OptionValueUUID *>(this); 367 return nullptr; 368 369 } 370 371 bool 372 OptionValue::GetBooleanValue (bool fail_value) const 373 { 374 const OptionValueBoolean *option_value = GetAsBoolean (); 375 if (option_value) 376 return option_value->GetCurrentValue(); 377 return fail_value; 378 } 379 380 bool 381 OptionValue::SetBooleanValue (bool new_value) 382 { 383 OptionValueBoolean *option_value = GetAsBoolean (); 384 if (option_value) 385 { 386 option_value->SetCurrentValue(new_value); 387 return true; 388 } 389 return false; 390 } 391 392 char 393 OptionValue::GetCharValue(char fail_value) const 394 { 395 const OptionValueChar *option_value = GetAsChar(); 396 if (option_value) 397 return option_value->GetCurrentValue(); 398 return fail_value; 399 } 400 401 char 402 OptionValue::SetCharValue(char new_value) 403 { 404 OptionValueChar *option_value = GetAsChar(); 405 if (option_value) 406 { 407 option_value->SetCurrentValue(new_value); 408 return true; 409 } 410 return false; 411 } 412 413 int64_t 414 OptionValue::GetEnumerationValue (int64_t fail_value) const 415 { 416 const OptionValueEnumeration *option_value = GetAsEnumeration(); 417 if (option_value) 418 return option_value->GetCurrentValue(); 419 return fail_value; 420 } 421 422 bool 423 OptionValue::SetEnumerationValue (int64_t value) 424 { 425 OptionValueEnumeration *option_value = GetAsEnumeration(); 426 if (option_value) 427 { 428 option_value->SetCurrentValue(value); 429 return true; 430 } 431 return false; 432 } 433 434 FileSpec 435 OptionValue::GetFileSpecValue () const 436 { 437 const OptionValueFileSpec *option_value = GetAsFileSpec (); 438 if (option_value) 439 return option_value->GetCurrentValue(); 440 return FileSpec(); 441 } 442 443 444 bool 445 OptionValue::SetFileSpecValue (const FileSpec &file_spec) 446 { 447 OptionValueFileSpec *option_value = GetAsFileSpec (); 448 if (option_value) 449 { 450 option_value->SetCurrentValue(file_spec, false); 451 return true; 452 } 453 return false; 454 } 455 456 FileSpecList 457 OptionValue::GetFileSpecListValue () const 458 { 459 const OptionValueFileSpecList *option_value = GetAsFileSpecList (); 460 if (option_value) 461 return option_value->GetCurrentValue(); 462 return FileSpecList(); 463 } 464 465 466 lldb::Format 467 OptionValue::GetFormatValue (lldb::Format fail_value) const 468 { 469 const OptionValueFormat *option_value = GetAsFormat (); 470 if (option_value) 471 return option_value->GetCurrentValue(); 472 return fail_value; 473 } 474 475 bool 476 OptionValue::SetFormatValue (lldb::Format new_value) 477 { 478 OptionValueFormat *option_value = GetAsFormat (); 479 if (option_value) 480 { 481 option_value->SetCurrentValue(new_value); 482 return true; 483 } 484 return false; 485 } 486 487 lldb::LanguageType 488 OptionValue::GetLanguageValue (lldb::LanguageType fail_value) const 489 { 490 const OptionValueLanguage *option_value = GetAsLanguage (); 491 if (option_value) 492 return option_value->GetCurrentValue(); 493 return fail_value; 494 } 495 496 bool 497 OptionValue::SetLanguageValue (lldb::LanguageType new_language) 498 { 499 OptionValueLanguage *option_value = GetAsLanguage (); 500 if (option_value) 501 { 502 option_value->SetCurrentValue(new_language); 503 return true; 504 } 505 return false; 506 } 507 508 const FormatEntity::Entry * 509 OptionValue::GetFormatEntity () const 510 { 511 const OptionValueFormatEntity *option_value = GetAsFormatEntity(); 512 if (option_value) 513 return &option_value->GetCurrentValue(); 514 return nullptr; 515 } 516 517 const RegularExpression * 518 OptionValue::GetRegexValue () const 519 { 520 const OptionValueRegex *option_value = GetAsRegex (); 521 if (option_value) 522 return option_value->GetCurrentValue(); 523 return nullptr; 524 } 525 526 527 int64_t 528 OptionValue::GetSInt64Value (int64_t fail_value) const 529 { 530 const OptionValueSInt64 *option_value = GetAsSInt64 (); 531 if (option_value) 532 return option_value->GetCurrentValue(); 533 return fail_value; 534 } 535 536 bool 537 OptionValue::SetSInt64Value (int64_t new_value) 538 { 539 OptionValueSInt64 *option_value = GetAsSInt64 (); 540 if (option_value) 541 { 542 option_value->SetCurrentValue(new_value); 543 return true; 544 } 545 return false; 546 } 547 548 const char * 549 OptionValue::GetStringValue (const char *fail_value) const 550 { 551 const OptionValueString *option_value = GetAsString (); 552 if (option_value) 553 return option_value->GetCurrentValue(); 554 return fail_value; 555 } 556 557 bool 558 OptionValue::SetStringValue (const char *new_value) 559 { 560 OptionValueString *option_value = GetAsString (); 561 if (option_value) 562 { 563 option_value->SetCurrentValue(new_value); 564 return true; 565 } 566 return false; 567 } 568 569 uint64_t 570 OptionValue::GetUInt64Value (uint64_t fail_value) const 571 { 572 const OptionValueUInt64 *option_value = GetAsUInt64 (); 573 if (option_value) 574 return option_value->GetCurrentValue(); 575 return fail_value; 576 } 577 578 bool 579 OptionValue::SetUInt64Value (uint64_t new_value) 580 { 581 OptionValueUInt64 *option_value = GetAsUInt64 (); 582 if (option_value) 583 { 584 option_value->SetCurrentValue(new_value); 585 return true; 586 } 587 return false; 588 } 589 590 UUID 591 OptionValue::GetUUIDValue () const 592 { 593 const OptionValueUUID *option_value = GetAsUUID(); 594 if (option_value) 595 return option_value->GetCurrentValue(); 596 return UUID(); 597 } 598 599 bool 600 OptionValue::SetUUIDValue (const UUID &uuid) 601 { 602 OptionValueUUID *option_value = GetAsUUID(); 603 if (option_value) 604 { 605 option_value->SetCurrentValue(uuid); 606 return true; 607 } 608 return false; 609 } 610 611 const char * 612 OptionValue::GetBuiltinTypeAsCString (Type t) 613 { 614 switch (t) 615 { 616 case eTypeInvalid: return "invalid"; 617 case eTypeArch: return "arch"; 618 case eTypeArgs: return "arguments"; 619 case eTypeArray: return "array"; 620 case eTypeBoolean: return "boolean"; 621 case eTypeChar: 622 return "char"; 623 case eTypeDictionary: return "dictionary"; 624 case eTypeEnum: return "enum"; 625 case eTypeFileSpec: return "file"; 626 case eTypeFileSpecList: return "file-list"; 627 case eTypeFormat: return "format"; 628 case eTypeFormatEntity: return "format-string"; 629 case eTypeLanguage: return "language"; 630 case eTypePathMap: return "path-map"; 631 case eTypeProperties: return "properties"; 632 case eTypeRegex: return "regex"; 633 case eTypeSInt64: return "int"; 634 case eTypeString: return "string"; 635 case eTypeUInt64: return "unsigned"; 636 case eTypeUUID: return "uuid"; 637 } 638 return nullptr; 639 } 640 641 642 lldb::OptionValueSP 643 OptionValue::CreateValueFromCStringForTypeMask (const char *value_cstr, uint32_t type_mask, Error &error) 644 { 645 // If only 1 bit is set in the type mask for a dictionary or array 646 // then we know how to decode a value from a cstring 647 lldb::OptionValueSP value_sp; 648 switch (type_mask) 649 { 650 case 1u << eTypeArch: value_sp.reset(new OptionValueArch()); break; 651 case 1u << eTypeBoolean: value_sp.reset(new OptionValueBoolean(false)); break; 652 case 1u << eTypeChar: value_sp.reset(new OptionValueChar('\0')); break; 653 case 1u << eTypeFileSpec: value_sp.reset(new OptionValueFileSpec()); break; 654 case 1u << eTypeFormat: value_sp.reset(new OptionValueFormat(eFormatInvalid)); break; 655 case 1u << eTypeFormatEntity: value_sp.reset(new OptionValueFormatEntity(NULL)); break; 656 case 1u << eTypeLanguage: value_sp.reset(new OptionValueLanguage(eLanguageTypeUnknown)); break; 657 case 1u << eTypeSInt64: value_sp.reset(new OptionValueSInt64()); break; 658 case 1u << eTypeString: value_sp.reset(new OptionValueString()); break; 659 case 1u << eTypeUInt64: value_sp.reset(new OptionValueUInt64()); break; 660 case 1u << eTypeUUID: value_sp.reset(new OptionValueUUID()); break; 661 } 662 663 if (value_sp) 664 error = value_sp->SetValueFromString (value_cstr, eVarSetOperationAssign); 665 else 666 error.SetErrorString("unsupported type mask"); 667 return value_sp; 668 } 669 670 bool 671 OptionValue::DumpQualifiedName (Stream &strm) const 672 { 673 bool dumped_something = false; 674 lldb::OptionValueSP m_parent_sp(m_parent_wp.lock()); 675 if (m_parent_sp) 676 { 677 if (m_parent_sp->DumpQualifiedName(strm)) 678 dumped_something = true; 679 } 680 ConstString name (GetName()); 681 if (name) 682 { 683 if (dumped_something) 684 strm.PutChar('.'); 685 else 686 dumped_something = true; 687 strm << name; 688 } 689 return dumped_something; 690 } 691 692 size_t 693 OptionValue::AutoComplete (CommandInterpreter &interpreter, 694 const char *s, 695 int match_start_point, 696 int max_return_elements, 697 bool &word_complete, 698 StringList &matches) 699 { 700 word_complete = false; 701 matches.Clear(); 702 return matches.GetSize(); 703 } 704 705 Error 706 OptionValue::SetValueFromString (llvm::StringRef value, VarSetOperationType op) 707 { 708 Error error; 709 switch (op) 710 { 711 case eVarSetOperationReplace: 712 error.SetErrorStringWithFormat ("%s objects do not support the 'replace' operation", GetTypeAsCString()); 713 break; 714 case eVarSetOperationInsertBefore: 715 error.SetErrorStringWithFormat ("%s objects do not support the 'insert-before' operation", GetTypeAsCString()); 716 break; 717 case eVarSetOperationInsertAfter: 718 error.SetErrorStringWithFormat ("%s objects do not support the 'insert-after' operation", GetTypeAsCString()); 719 break; 720 case eVarSetOperationRemove: 721 error.SetErrorStringWithFormat ("%s objects do not support the 'remove' operation", GetTypeAsCString()); 722 break; 723 case eVarSetOperationAppend: 724 error.SetErrorStringWithFormat ("%s objects do not support the 'append' operation", GetTypeAsCString()); 725 break; 726 case eVarSetOperationClear: 727 error.SetErrorStringWithFormat ("%s objects do not support the 'clear' operation", GetTypeAsCString()); 728 break; 729 case eVarSetOperationAssign: 730 error.SetErrorStringWithFormat ("%s objects do not support the 'assign' operation", GetTypeAsCString()); 731 break; 732 case eVarSetOperationInvalid: 733 error.SetErrorStringWithFormat ("invalid operation performed on a %s object", GetTypeAsCString()); 734 break; 735 } 736 return error; 737 } 738 739