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