1 //===-- OptionValue.cpp -----------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Interpreter/OptionValue.h"
11 #include "lldb/Interpreter/OptionValues.h"
12 #include "lldb/Utility/StringList.h"
15 using namespace lldb_private;
17 // Get this value as a uint64_t value if it is encoded as a boolean, uint64_t
18 // or int64_t. Other types will cause "fail_value" to be returned
19 uint64_t OptionValue::GetUInt64Value(uint64_t fail_value, bool *success_ptr) {
23 case OptionValue::eTypeBoolean:
24 return static_cast<OptionValueBoolean *>(this)->GetCurrentValue();
25 case OptionValue::eTypeSInt64:
26 return static_cast<OptionValueSInt64 *>(this)->GetCurrentValue();
27 case OptionValue::eTypeUInt64:
28 return static_cast<OptionValueUInt64 *>(this)->GetCurrentValue();
37 Status OptionValue::SetSubValue(const ExecutionContext *exe_ctx,
38 VarSetOperationType op, llvm::StringRef name,
39 llvm::StringRef value) {
41 error.SetErrorStringWithFormat("SetSubValue is not supported");
45 OptionValueBoolean *OptionValue::GetAsBoolean() {
46 if (GetType() == OptionValue::eTypeBoolean)
47 return static_cast<OptionValueBoolean *>(this);
51 const OptionValueBoolean *OptionValue::GetAsBoolean() const {
52 if (GetType() == OptionValue::eTypeBoolean)
53 return static_cast<const OptionValueBoolean *>(this);
57 const OptionValueChar *OptionValue::GetAsChar() const {
58 if (GetType() == OptionValue::eTypeChar)
59 return static_cast<const OptionValueChar *>(this);
63 OptionValueChar *OptionValue::GetAsChar() {
64 if (GetType() == OptionValue::eTypeChar)
65 return static_cast<OptionValueChar *>(this);
69 OptionValueFileSpec *OptionValue::GetAsFileSpec() {
70 if (GetType() == OptionValue::eTypeFileSpec)
71 return static_cast<OptionValueFileSpec *>(this);
75 const OptionValueFileSpec *OptionValue::GetAsFileSpec() const {
76 if (GetType() == OptionValue::eTypeFileSpec)
77 return static_cast<const OptionValueFileSpec *>(this);
81 OptionValueFileSpecList *OptionValue::GetAsFileSpecList() {
82 if (GetType() == OptionValue::eTypeFileSpecList)
83 return static_cast<OptionValueFileSpecList *>(this);
87 const OptionValueFileSpecList *OptionValue::GetAsFileSpecList() const {
88 if (GetType() == OptionValue::eTypeFileSpecList)
89 return static_cast<const OptionValueFileSpecList *>(this);
93 OptionValueArch *OptionValue::GetAsArch() {
94 if (GetType() == OptionValue::eTypeArch)
95 return static_cast<OptionValueArch *>(this);
99 const OptionValueArch *OptionValue::GetAsArch() const {
100 if (GetType() == OptionValue::eTypeArch)
101 return static_cast<const OptionValueArch *>(this);
105 OptionValueArray *OptionValue::GetAsArray() {
106 if (GetType() == OptionValue::eTypeArray)
107 return static_cast<OptionValueArray *>(this);
111 const OptionValueArray *OptionValue::GetAsArray() const {
112 if (GetType() == OptionValue::eTypeArray)
113 return static_cast<const OptionValueArray *>(this);
117 OptionValueArgs *OptionValue::GetAsArgs() {
118 if (GetType() == OptionValue::eTypeArgs)
119 return static_cast<OptionValueArgs *>(this);
123 const OptionValueArgs *OptionValue::GetAsArgs() const {
124 if (GetType() == OptionValue::eTypeArgs)
125 return static_cast<const OptionValueArgs *>(this);
129 OptionValueDictionary *OptionValue::GetAsDictionary() {
130 if (GetType() == OptionValue::eTypeDictionary)
131 return static_cast<OptionValueDictionary *>(this);
135 const OptionValueDictionary *OptionValue::GetAsDictionary() const {
136 if (GetType() == OptionValue::eTypeDictionary)
137 return static_cast<const OptionValueDictionary *>(this);
141 OptionValueEnumeration *OptionValue::GetAsEnumeration() {
142 if (GetType() == OptionValue::eTypeEnum)
143 return static_cast<OptionValueEnumeration *>(this);
147 const OptionValueEnumeration *OptionValue::GetAsEnumeration() const {
148 if (GetType() == OptionValue::eTypeEnum)
149 return static_cast<const OptionValueEnumeration *>(this);
153 OptionValueFormat *OptionValue::GetAsFormat() {
154 if (GetType() == OptionValue::eTypeFormat)
155 return static_cast<OptionValueFormat *>(this);
159 const OptionValueFormat *OptionValue::GetAsFormat() const {
160 if (GetType() == OptionValue::eTypeFormat)
161 return static_cast<const OptionValueFormat *>(this);
165 OptionValueLanguage *OptionValue::GetAsLanguage() {
166 if (GetType() == OptionValue::eTypeLanguage)
167 return static_cast<OptionValueLanguage *>(this);
171 const OptionValueLanguage *OptionValue::GetAsLanguage() const {
172 if (GetType() == OptionValue::eTypeLanguage)
173 return static_cast<const OptionValueLanguage *>(this);
177 OptionValueFormatEntity *OptionValue::GetAsFormatEntity() {
178 if (GetType() == OptionValue::eTypeFormatEntity)
179 return static_cast<OptionValueFormatEntity *>(this);
183 const OptionValueFormatEntity *OptionValue::GetAsFormatEntity() const {
184 if (GetType() == OptionValue::eTypeFormatEntity)
185 return static_cast<const OptionValueFormatEntity *>(this);
189 OptionValuePathMappings *OptionValue::GetAsPathMappings() {
190 if (GetType() == OptionValue::eTypePathMap)
191 return static_cast<OptionValuePathMappings *>(this);
195 const OptionValuePathMappings *OptionValue::GetAsPathMappings() const {
196 if (GetType() == OptionValue::eTypePathMap)
197 return static_cast<const OptionValuePathMappings *>(this);
201 OptionValueProperties *OptionValue::GetAsProperties() {
202 if (GetType() == OptionValue::eTypeProperties)
203 return static_cast<OptionValueProperties *>(this);
207 const OptionValueProperties *OptionValue::GetAsProperties() const {
208 if (GetType() == OptionValue::eTypeProperties)
209 return static_cast<const OptionValueProperties *>(this);
213 OptionValueRegex *OptionValue::GetAsRegex() {
214 if (GetType() == OptionValue::eTypeRegex)
215 return static_cast<OptionValueRegex *>(this);
219 const OptionValueRegex *OptionValue::GetAsRegex() const {
220 if (GetType() == OptionValue::eTypeRegex)
221 return static_cast<const OptionValueRegex *>(this);
225 OptionValueSInt64 *OptionValue::GetAsSInt64() {
226 if (GetType() == OptionValue::eTypeSInt64)
227 return static_cast<OptionValueSInt64 *>(this);
231 const OptionValueSInt64 *OptionValue::GetAsSInt64() const {
232 if (GetType() == OptionValue::eTypeSInt64)
233 return static_cast<const OptionValueSInt64 *>(this);
237 OptionValueString *OptionValue::GetAsString() {
238 if (GetType() == OptionValue::eTypeString)
239 return static_cast<OptionValueString *>(this);
243 const OptionValueString *OptionValue::GetAsString() const {
244 if (GetType() == OptionValue::eTypeString)
245 return static_cast<const OptionValueString *>(this);
249 OptionValueUInt64 *OptionValue::GetAsUInt64() {
250 if (GetType() == OptionValue::eTypeUInt64)
251 return static_cast<OptionValueUInt64 *>(this);
255 const OptionValueUInt64 *OptionValue::GetAsUInt64() const {
256 if (GetType() == OptionValue::eTypeUInt64)
257 return static_cast<const OptionValueUInt64 *>(this);
261 OptionValueUUID *OptionValue::GetAsUUID() {
262 if (GetType() == OptionValue::eTypeUUID)
263 return static_cast<OptionValueUUID *>(this);
267 const OptionValueUUID *OptionValue::GetAsUUID() const {
268 if (GetType() == OptionValue::eTypeUUID)
269 return static_cast<const OptionValueUUID *>(this);
273 bool OptionValue::GetBooleanValue(bool fail_value) const {
274 const OptionValueBoolean *option_value = GetAsBoolean();
276 return option_value->GetCurrentValue();
280 bool OptionValue::SetBooleanValue(bool new_value) {
281 OptionValueBoolean *option_value = GetAsBoolean();
283 option_value->SetCurrentValue(new_value);
289 char OptionValue::GetCharValue(char fail_value) const {
290 const OptionValueChar *option_value = GetAsChar();
292 return option_value->GetCurrentValue();
296 char OptionValue::SetCharValue(char new_value) {
297 OptionValueChar *option_value = GetAsChar();
299 option_value->SetCurrentValue(new_value);
305 int64_t OptionValue::GetEnumerationValue(int64_t fail_value) const {
306 const OptionValueEnumeration *option_value = GetAsEnumeration();
308 return option_value->GetCurrentValue();
312 bool OptionValue::SetEnumerationValue(int64_t value) {
313 OptionValueEnumeration *option_value = GetAsEnumeration();
315 option_value->SetCurrentValue(value);
321 FileSpec OptionValue::GetFileSpecValue() const {
322 const OptionValueFileSpec *option_value = GetAsFileSpec();
324 return option_value->GetCurrentValue();
328 bool OptionValue::SetFileSpecValue(const FileSpec &file_spec) {
329 OptionValueFileSpec *option_value = GetAsFileSpec();
331 option_value->SetCurrentValue(file_spec, false);
337 FileSpecList OptionValue::GetFileSpecListValue() const {
338 const OptionValueFileSpecList *option_value = GetAsFileSpecList();
340 return option_value->GetCurrentValue();
341 return FileSpecList();
344 lldb::Format OptionValue::GetFormatValue(lldb::Format fail_value) const {
345 const OptionValueFormat *option_value = GetAsFormat();
347 return option_value->GetCurrentValue();
351 bool OptionValue::SetFormatValue(lldb::Format new_value) {
352 OptionValueFormat *option_value = GetAsFormat();
354 option_value->SetCurrentValue(new_value);
361 OptionValue::GetLanguageValue(lldb::LanguageType fail_value) const {
362 const OptionValueLanguage *option_value = GetAsLanguage();
364 return option_value->GetCurrentValue();
368 bool OptionValue::SetLanguageValue(lldb::LanguageType new_language) {
369 OptionValueLanguage *option_value = GetAsLanguage();
371 option_value->SetCurrentValue(new_language);
377 const FormatEntity::Entry *OptionValue::GetFormatEntity() const {
378 const OptionValueFormatEntity *option_value = GetAsFormatEntity();
380 return &option_value->GetCurrentValue();
384 const RegularExpression *OptionValue::GetRegexValue() const {
385 const OptionValueRegex *option_value = GetAsRegex();
387 return option_value->GetCurrentValue();
391 int64_t OptionValue::GetSInt64Value(int64_t fail_value) const {
392 const OptionValueSInt64 *option_value = GetAsSInt64();
394 return option_value->GetCurrentValue();
398 bool OptionValue::SetSInt64Value(int64_t new_value) {
399 OptionValueSInt64 *option_value = GetAsSInt64();
401 option_value->SetCurrentValue(new_value);
407 llvm::StringRef OptionValue::GetStringValue(llvm::StringRef fail_value) const {
408 const OptionValueString *option_value = GetAsString();
410 return option_value->GetCurrentValueAsRef();
414 bool OptionValue::SetStringValue(llvm::StringRef new_value) {
415 OptionValueString *option_value = GetAsString();
417 option_value->SetCurrentValue(new_value);
423 uint64_t OptionValue::GetUInt64Value(uint64_t fail_value) const {
424 const OptionValueUInt64 *option_value = GetAsUInt64();
426 return option_value->GetCurrentValue();
430 bool OptionValue::SetUInt64Value(uint64_t new_value) {
431 OptionValueUInt64 *option_value = GetAsUInt64();
433 option_value->SetCurrentValue(new_value);
439 UUID OptionValue::GetUUIDValue() const {
440 const OptionValueUUID *option_value = GetAsUUID();
442 return option_value->GetCurrentValue();
446 bool OptionValue::SetUUIDValue(const UUID &uuid) {
447 OptionValueUUID *option_value = GetAsUUID();
449 option_value->SetCurrentValue(uuid);
455 const char *OptionValue::GetBuiltinTypeAsCString(Type t) {
469 case eTypeDictionary:
475 case eTypeFileSpecList:
479 case eTypeFormatEntity:
480 return "format-string";
485 case eTypeProperties:
501 lldb::OptionValueSP OptionValue::CreateValueFromCStringForTypeMask(
502 const char *value_cstr, uint32_t type_mask, Status &error) {
503 // If only 1 bit is set in the type mask for a dictionary or array then we
504 // know how to decode a value from a cstring
505 lldb::OptionValueSP value_sp;
507 case 1u << eTypeArch:
508 value_sp.reset(new OptionValueArch());
510 case 1u << eTypeBoolean:
511 value_sp.reset(new OptionValueBoolean(false));
513 case 1u << eTypeChar:
514 value_sp.reset(new OptionValueChar('\0'));
516 case 1u << eTypeFileSpec:
517 value_sp.reset(new OptionValueFileSpec());
519 case 1u << eTypeFormat:
520 value_sp.reset(new OptionValueFormat(eFormatInvalid));
522 case 1u << eTypeFormatEntity:
523 value_sp.reset(new OptionValueFormatEntity(nullptr));
525 case 1u << eTypeLanguage:
526 value_sp.reset(new OptionValueLanguage(eLanguageTypeUnknown));
528 case 1u << eTypeSInt64:
529 value_sp.reset(new OptionValueSInt64());
531 case 1u << eTypeString:
532 value_sp.reset(new OptionValueString());
534 case 1u << eTypeUInt64:
535 value_sp.reset(new OptionValueUInt64());
537 case 1u << eTypeUUID:
538 value_sp.reset(new OptionValueUUID());
543 error = value_sp->SetValueFromString(
544 llvm::StringRef::withNullAsEmpty(value_cstr), eVarSetOperationAssign);
546 error.SetErrorString("unsupported type mask");
550 bool OptionValue::DumpQualifiedName(Stream &strm) const {
551 bool dumped_something = false;
552 lldb::OptionValueSP m_parent_sp(m_parent_wp.lock());
554 if (m_parent_sp->DumpQualifiedName(strm))
555 dumped_something = true;
557 ConstString name(GetName());
559 if (dumped_something)
562 dumped_something = true;
565 return dumped_something;
568 size_t OptionValue::AutoComplete(CommandInterpreter &interpreter,
569 CompletionRequest &request) {
570 request.SetWordComplete(false);
571 return request.GetNumberOfMatches();
574 Status OptionValue::SetValueFromString(llvm::StringRef value,
575 VarSetOperationType op) {
578 case eVarSetOperationReplace:
579 error.SetErrorStringWithFormat(
580 "%s objects do not support the 'replace' operation",
583 case eVarSetOperationInsertBefore:
584 error.SetErrorStringWithFormat(
585 "%s objects do not support the 'insert-before' operation",
588 case eVarSetOperationInsertAfter:
589 error.SetErrorStringWithFormat(
590 "%s objects do not support the 'insert-after' operation",
593 case eVarSetOperationRemove:
594 error.SetErrorStringWithFormat(
595 "%s objects do not support the 'remove' operation", GetTypeAsCString());
597 case eVarSetOperationAppend:
598 error.SetErrorStringWithFormat(
599 "%s objects do not support the 'append' operation", GetTypeAsCString());
601 case eVarSetOperationClear:
602 error.SetErrorStringWithFormat(
603 "%s objects do not support the 'clear' operation", GetTypeAsCString());
605 case eVarSetOperationAssign:
606 error.SetErrorStringWithFormat(
607 "%s objects do not support the 'assign' operation", GetTypeAsCString());
609 case eVarSetOperationInvalid:
610 error.SetErrorStringWithFormat("invalid operation performed on a %s object",