OptionValue.cpp 17.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
//===-- OptionValue.cpp ---------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "lldb/Interpreter/OptionValue.h"
#include "lldb/Interpreter/OptionValues.h"
#include "lldb/Utility/StringList.h"

#include <memory>

using namespace lldb;
using namespace lldb_private;

// Get this value as a uint64_t value if it is encoded as a boolean, uint64_t
// or int64_t. Other types will cause "fail_value" to be returned
uint64_t OptionValue::GetUInt64Value(uint64_t fail_value, bool *success_ptr) {
  if (success_ptr)
    *success_ptr = true;
  switch (GetType()) {
  case OptionValue::eTypeBoolean:
    return static_cast<OptionValueBoolean *>(this)->GetCurrentValue();
  case OptionValue::eTypeSInt64:
    return static_cast<OptionValueSInt64 *>(this)->GetCurrentValue();
  case OptionValue::eTypeUInt64:
    return static_cast<OptionValueUInt64 *>(this)->GetCurrentValue();
  default:
    break;
  }
  if (success_ptr)
    *success_ptr = false;
  return fail_value;
}

Status OptionValue::SetSubValue(const ExecutionContext *exe_ctx,
                                VarSetOperationType op, llvm::StringRef name,
                                llvm::StringRef value) {
  Status error;
  error.SetErrorString("SetSubValue is not supported");
  return error;
}

OptionValueBoolean *OptionValue::GetAsBoolean() {
  if (GetType() == OptionValue::eTypeBoolean)
    return static_cast<OptionValueBoolean *>(this);
  return nullptr;
}

const OptionValueBoolean *OptionValue::GetAsBoolean() const {
  if (GetType() == OptionValue::eTypeBoolean)
    return static_cast<const OptionValueBoolean *>(this);
  return nullptr;
}

const OptionValueChar *OptionValue::GetAsChar() const {
  if (GetType() == OptionValue::eTypeChar)
    return static_cast<const OptionValueChar *>(this);
  return nullptr;
}

OptionValueChar *OptionValue::GetAsChar() {
  if (GetType() == OptionValue::eTypeChar)
    return static_cast<OptionValueChar *>(this);
  return nullptr;
}

OptionValueFileSpec *OptionValue::GetAsFileSpec() {
  if (GetType() == OptionValue::eTypeFileSpec)
    return static_cast<OptionValueFileSpec *>(this);
  return nullptr;
}

const OptionValueFileSpec *OptionValue::GetAsFileSpec() const {
  if (GetType() == OptionValue::eTypeFileSpec)
    return static_cast<const OptionValueFileSpec *>(this);
  return nullptr;
}

OptionValueFileSpecList *OptionValue::GetAsFileSpecList() {
  if (GetType() == OptionValue::eTypeFileSpecList)
    return static_cast<OptionValueFileSpecList *>(this);
  return nullptr;
}

const OptionValueFileSpecList *OptionValue::GetAsFileSpecList() const {
  if (GetType() == OptionValue::eTypeFileSpecList)
    return static_cast<const OptionValueFileSpecList *>(this);
  return nullptr;
}

OptionValueArch *OptionValue::GetAsArch() {
  if (GetType() == OptionValue::eTypeArch)
    return static_cast<OptionValueArch *>(this);
  return nullptr;
}

const OptionValueArch *OptionValue::GetAsArch() const {
  if (GetType() == OptionValue::eTypeArch)
    return static_cast<const OptionValueArch *>(this);
  return nullptr;
}

OptionValueArray *OptionValue::GetAsArray() {
  if (GetType() == OptionValue::eTypeArray)
    return static_cast<OptionValueArray *>(this);
  return nullptr;
}

const OptionValueArray *OptionValue::GetAsArray() const {
  if (GetType() == OptionValue::eTypeArray)
    return static_cast<const OptionValueArray *>(this);
  return nullptr;
}

OptionValueArgs *OptionValue::GetAsArgs() {
  if (GetType() == OptionValue::eTypeArgs)
    return static_cast<OptionValueArgs *>(this);
  return nullptr;
}

const OptionValueArgs *OptionValue::GetAsArgs() const {
  if (GetType() == OptionValue::eTypeArgs)
    return static_cast<const OptionValueArgs *>(this);
  return nullptr;
}

OptionValueDictionary *OptionValue::GetAsDictionary() {
  if (GetType() == OptionValue::eTypeDictionary)
    return static_cast<OptionValueDictionary *>(this);
  return nullptr;
}

const OptionValueDictionary *OptionValue::GetAsDictionary() const {
  if (GetType() == OptionValue::eTypeDictionary)
    return static_cast<const OptionValueDictionary *>(this);
  return nullptr;
}

OptionValueEnumeration *OptionValue::GetAsEnumeration() {
  if (GetType() == OptionValue::eTypeEnum)
    return static_cast<OptionValueEnumeration *>(this);
  return nullptr;
}

const OptionValueEnumeration *OptionValue::GetAsEnumeration() const {
  if (GetType() == OptionValue::eTypeEnum)
    return static_cast<const OptionValueEnumeration *>(this);
  return nullptr;
}

OptionValueFormat *OptionValue::GetAsFormat() {
  if (GetType() == OptionValue::eTypeFormat)
    return static_cast<OptionValueFormat *>(this);
  return nullptr;
}

const OptionValueFormat *OptionValue::GetAsFormat() const {
  if (GetType() == OptionValue::eTypeFormat)
    return static_cast<const OptionValueFormat *>(this);
  return nullptr;
}

OptionValueLanguage *OptionValue::GetAsLanguage() {
  if (GetType() == OptionValue::eTypeLanguage)
    return static_cast<OptionValueLanguage *>(this);
  return nullptr;
}

const OptionValueLanguage *OptionValue::GetAsLanguage() const {
  if (GetType() == OptionValue::eTypeLanguage)
    return static_cast<const OptionValueLanguage *>(this);
  return nullptr;
}

OptionValueFormatEntity *OptionValue::GetAsFormatEntity() {
  if (GetType() == OptionValue::eTypeFormatEntity)
    return static_cast<OptionValueFormatEntity *>(this);
  return nullptr;
}

const OptionValueFormatEntity *OptionValue::GetAsFormatEntity() const {
  if (GetType() == OptionValue::eTypeFormatEntity)
    return static_cast<const OptionValueFormatEntity *>(this);
  return nullptr;
}

OptionValuePathMappings *OptionValue::GetAsPathMappings() {
  if (GetType() == OptionValue::eTypePathMap)
    return static_cast<OptionValuePathMappings *>(this);
  return nullptr;
}

const OptionValuePathMappings *OptionValue::GetAsPathMappings() const {
  if (GetType() == OptionValue::eTypePathMap)
    return static_cast<const OptionValuePathMappings *>(this);
  return nullptr;
}

OptionValueProperties *OptionValue::GetAsProperties() {
  if (GetType() == OptionValue::eTypeProperties)
    return static_cast<OptionValueProperties *>(this);
  return nullptr;
}

const OptionValueProperties *OptionValue::GetAsProperties() const {
  if (GetType() == OptionValue::eTypeProperties)
    return static_cast<const OptionValueProperties *>(this);
  return nullptr;
}

OptionValueRegex *OptionValue::GetAsRegex() {
  if (GetType() == OptionValue::eTypeRegex)
    return static_cast<OptionValueRegex *>(this);
  return nullptr;
}

const OptionValueRegex *OptionValue::GetAsRegex() const {
  if (GetType() == OptionValue::eTypeRegex)
    return static_cast<const OptionValueRegex *>(this);
  return nullptr;
}

OptionValueSInt64 *OptionValue::GetAsSInt64() {
  if (GetType() == OptionValue::eTypeSInt64)
    return static_cast<OptionValueSInt64 *>(this);
  return nullptr;
}

const OptionValueSInt64 *OptionValue::GetAsSInt64() const {
  if (GetType() == OptionValue::eTypeSInt64)
    return static_cast<const OptionValueSInt64 *>(this);
  return nullptr;
}

OptionValueString *OptionValue::GetAsString() {
  if (GetType() == OptionValue::eTypeString)
    return static_cast<OptionValueString *>(this);
  return nullptr;
}

const OptionValueString *OptionValue::GetAsString() const {
  if (GetType() == OptionValue::eTypeString)
    return static_cast<const OptionValueString *>(this);
  return nullptr;
}

OptionValueUInt64 *OptionValue::GetAsUInt64() {
  if (GetType() == OptionValue::eTypeUInt64)
    return static_cast<OptionValueUInt64 *>(this);
  return nullptr;
}

const OptionValueUInt64 *OptionValue::GetAsUInt64() const {
  if (GetType() == OptionValue::eTypeUInt64)
    return static_cast<const OptionValueUInt64 *>(this);
  return nullptr;
}

OptionValueUUID *OptionValue::GetAsUUID() {
  if (GetType() == OptionValue::eTypeUUID)
    return static_cast<OptionValueUUID *>(this);
  return nullptr;
}

const OptionValueUUID *OptionValue::GetAsUUID() const {
  if (GetType() == OptionValue::eTypeUUID)
    return static_cast<const OptionValueUUID *>(this);
  return nullptr;
}

bool OptionValue::GetBooleanValue(bool fail_value) const {
  const OptionValueBoolean *option_value = GetAsBoolean();
  if (option_value)
    return option_value->GetCurrentValue();
  return fail_value;
}

bool OptionValue::SetBooleanValue(bool new_value) {
  OptionValueBoolean *option_value = GetAsBoolean();
  if (option_value) {
    option_value->SetCurrentValue(new_value);
    return true;
  }
  return false;
}

char OptionValue::GetCharValue(char fail_value) const {
  const OptionValueChar *option_value = GetAsChar();
  if (option_value)
    return option_value->GetCurrentValue();
  return fail_value;
}

char OptionValue::SetCharValue(char new_value) {
  OptionValueChar *option_value = GetAsChar();
  if (option_value) {
    option_value->SetCurrentValue(new_value);
    return true;
  }
  return false;
}

int64_t OptionValue::GetEnumerationValue(int64_t fail_value) const {
  const OptionValueEnumeration *option_value = GetAsEnumeration();
  if (option_value)
    return option_value->GetCurrentValue();
  return fail_value;
}

bool OptionValue::SetEnumerationValue(int64_t value) {
  OptionValueEnumeration *option_value = GetAsEnumeration();
  if (option_value) {
    option_value->SetCurrentValue(value);
    return true;
  }
  return false;
}

FileSpec OptionValue::GetFileSpecValue() const {
  const OptionValueFileSpec *option_value = GetAsFileSpec();
  if (option_value)
    return option_value->GetCurrentValue();
  return FileSpec();
}

bool OptionValue::SetFileSpecValue(const FileSpec &file_spec) {
  OptionValueFileSpec *option_value = GetAsFileSpec();
  if (option_value) {
    option_value->SetCurrentValue(file_spec, false);
    return true;
  }
  return false;
}

FileSpecList OptionValue::GetFileSpecListValue() const {
  const OptionValueFileSpecList *option_value = GetAsFileSpecList();
  if (option_value)
    return option_value->GetCurrentValue();
  return FileSpecList();
}

lldb::Format OptionValue::GetFormatValue(lldb::Format fail_value) const {
  const OptionValueFormat *option_value = GetAsFormat();
  if (option_value)
    return option_value->GetCurrentValue();
  return fail_value;
}

bool OptionValue::SetFormatValue(lldb::Format new_value) {
  OptionValueFormat *option_value = GetAsFormat();
  if (option_value) {
    option_value->SetCurrentValue(new_value);
    return true;
  }
  return false;
}

lldb::LanguageType
OptionValue::GetLanguageValue(lldb::LanguageType fail_value) const {
  const OptionValueLanguage *option_value = GetAsLanguage();
  if (option_value)
    return option_value->GetCurrentValue();
  return fail_value;
}

bool OptionValue::SetLanguageValue(lldb::LanguageType new_language) {
  OptionValueLanguage *option_value = GetAsLanguage();
  if (option_value) {
    option_value->SetCurrentValue(new_language);
    return true;
  }
  return false;
}

const FormatEntity::Entry *OptionValue::GetFormatEntity() const {
  const OptionValueFormatEntity *option_value = GetAsFormatEntity();
  if (option_value)
    return &option_value->GetCurrentValue();
  return nullptr;
}

const RegularExpression *OptionValue::GetRegexValue() const {
  const OptionValueRegex *option_value = GetAsRegex();
  if (option_value)
    return option_value->GetCurrentValue();
  return nullptr;
}

int64_t OptionValue::GetSInt64Value(int64_t fail_value) const {
  const OptionValueSInt64 *option_value = GetAsSInt64();
  if (option_value)
    return option_value->GetCurrentValue();
  return fail_value;
}

bool OptionValue::SetSInt64Value(int64_t new_value) {
  OptionValueSInt64 *option_value = GetAsSInt64();
  if (option_value) {
    option_value->SetCurrentValue(new_value);
    return true;
  }
  return false;
}

llvm::StringRef OptionValue::GetStringValue(llvm::StringRef fail_value) const {
  const OptionValueString *option_value = GetAsString();
  if (option_value)
    return option_value->GetCurrentValueAsRef();
  return fail_value;
}

bool OptionValue::SetStringValue(llvm::StringRef new_value) {
  OptionValueString *option_value = GetAsString();
  if (option_value) {
    option_value->SetCurrentValue(new_value);
    return true;
  }
  return false;
}

uint64_t OptionValue::GetUInt64Value(uint64_t fail_value) const {
  const OptionValueUInt64 *option_value = GetAsUInt64();
  if (option_value)
    return option_value->GetCurrentValue();
  return fail_value;
}

bool OptionValue::SetUInt64Value(uint64_t new_value) {
  OptionValueUInt64 *option_value = GetAsUInt64();
  if (option_value) {
    option_value->SetCurrentValue(new_value);
    return true;
  }
  return false;
}

UUID OptionValue::GetUUIDValue() const {
  const OptionValueUUID *option_value = GetAsUUID();
  if (option_value)
    return option_value->GetCurrentValue();
  return UUID();
}

bool OptionValue::SetUUIDValue(const UUID &uuid) {
  OptionValueUUID *option_value = GetAsUUID();
  if (option_value) {
    option_value->SetCurrentValue(uuid);
    return true;
  }
  return false;
}

const char *OptionValue::GetBuiltinTypeAsCString(Type t) {
  switch (t) {
  case eTypeInvalid:
    return "invalid";
  case eTypeArch:
    return "arch";
  case eTypeArgs:
    return "arguments";
  case eTypeArray:
    return "array";
  case eTypeBoolean:
    return "boolean";
  case eTypeChar:
    return "char";
  case eTypeDictionary:
    return "dictionary";
  case eTypeEnum:
    return "enum";
  case eTypeFileLineColumn:
    return "file:line:column specifier";
  case eTypeFileSpec:
    return "file";
  case eTypeFileSpecList:
    return "file-list";
  case eTypeFormat:
    return "format";
  case eTypeFormatEntity:
    return "format-string";
  case eTypeLanguage:
    return "language";
  case eTypePathMap:
    return "path-map";
  case eTypeProperties:
    return "properties";
  case eTypeRegex:
    return "regex";
  case eTypeSInt64:
    return "int";
  case eTypeString:
    return "string";
  case eTypeUInt64:
    return "unsigned";
  case eTypeUUID:
    return "uuid";
  }
  return nullptr;
}

lldb::OptionValueSP OptionValue::CreateValueFromCStringForTypeMask(
    const char *value_cstr, uint32_t type_mask, Status &error) {
  // If only 1 bit is set in the type mask for a dictionary or array then we
  // know how to decode a value from a cstring
  lldb::OptionValueSP value_sp;
  switch (type_mask) {
  case 1u << eTypeArch:
    value_sp = std::make_shared<OptionValueArch>();
    break;
  case 1u << eTypeBoolean:
    value_sp = std::make_shared<OptionValueBoolean>(false);
    break;
  case 1u << eTypeChar:
    value_sp = std::make_shared<OptionValueChar>('\0');
    break;
  case 1u << eTypeFileSpec:
    value_sp = std::make_shared<OptionValueFileSpec>();
    break;
  case 1u << eTypeFormat:
    value_sp = std::make_shared<OptionValueFormat>(eFormatInvalid);
    break;
  case 1u << eTypeFormatEntity:
    value_sp = std::make_shared<OptionValueFormatEntity>(nullptr);
    break;
  case 1u << eTypeLanguage:
    value_sp = std::make_shared<OptionValueLanguage>(eLanguageTypeUnknown);
    break;
  case 1u << eTypeSInt64:
    value_sp = std::make_shared<OptionValueSInt64>();
    break;
  case 1u << eTypeString:
    value_sp = std::make_shared<OptionValueString>();
    break;
  case 1u << eTypeUInt64:
    value_sp = std::make_shared<OptionValueUInt64>();
    break;
  case 1u << eTypeUUID:
    value_sp = std::make_shared<OptionValueUUID>();
    break;
  }

  if (value_sp)
    error = value_sp->SetValueFromString(
        llvm::StringRef::withNullAsEmpty(value_cstr), eVarSetOperationAssign);
  else
    error.SetErrorString("unsupported type mask");
  return value_sp;
}

bool OptionValue::DumpQualifiedName(Stream &strm) const {
  bool dumped_something = false;
  lldb::OptionValueSP m_parent_sp(m_parent_wp.lock());
  if (m_parent_sp) {
    if (m_parent_sp->DumpQualifiedName(strm))
      dumped_something = true;
  }
  ConstString name(GetName());
  if (name) {
    if (dumped_something)
      strm.PutChar('.');
    else
      dumped_something = true;
    strm << name;
  }
  return dumped_something;
}

void OptionValue::AutoComplete(CommandInterpreter &interpreter,
                               CompletionRequest &request) {}

Status OptionValue::SetValueFromString(llvm::StringRef value,
                                       VarSetOperationType op) {
  Status error;
  switch (op) {
  case eVarSetOperationReplace:
    error.SetErrorStringWithFormat(
        "%s objects do not support the 'replace' operation",
        GetTypeAsCString());
    break;
  case eVarSetOperationInsertBefore:
    error.SetErrorStringWithFormat(
        "%s objects do not support the 'insert-before' operation",
        GetTypeAsCString());
    break;
  case eVarSetOperationInsertAfter:
    error.SetErrorStringWithFormat(
        "%s objects do not support the 'insert-after' operation",
        GetTypeAsCString());
    break;
  case eVarSetOperationRemove:
    error.SetErrorStringWithFormat(
        "%s objects do not support the 'remove' operation", GetTypeAsCString());
    break;
  case eVarSetOperationAppend:
    error.SetErrorStringWithFormat(
        "%s objects do not support the 'append' operation", GetTypeAsCString());
    break;
  case eVarSetOperationClear:
    error.SetErrorStringWithFormat(
        "%s objects do not support the 'clear' operation", GetTypeAsCString());
    break;
  case eVarSetOperationAssign:
    error.SetErrorStringWithFormat(
        "%s objects do not support the 'assign' operation", GetTypeAsCString());
    break;
  case eVarSetOperationInvalid:
    error.SetErrorStringWithFormat("invalid operation performed on a %s object",
                                   GetTypeAsCString());
    break;
  }
  return error;
}