1 //===-- OptionValue.cpp -----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Interpreter/OptionValue.h"
14 // Other libraries and framework includes
16 #include "lldb/Interpreter/OptionValues.h"
17 #include "lldb/Utility/StringList.h"
20 using namespace lldb_private;
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) {
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();
44 Status OptionValue::SetSubValue(const ExecutionContext *exe_ctx,
45 VarSetOperationType op, llvm::StringRef name,
46 llvm::StringRef value) {
48 error.SetErrorStringWithFormat("SetSubValue is not supported");
52 OptionValueBoolean *OptionValue::GetAsBoolean() {
53 if (GetType() == OptionValue::eTypeBoolean)
54 return static_cast<OptionValueBoolean *>(this);
58 const OptionValueBoolean *OptionValue::GetAsBoolean() const {
59 if (GetType() == OptionValue::eTypeBoolean)
60 return static_cast<const OptionValueBoolean *>(this);
64 const OptionValueChar *OptionValue::GetAsChar() const {
65 if (GetType() == OptionValue::eTypeChar)
66 return static_cast<const OptionValueChar *>(this);
70 OptionValueChar *OptionValue::GetAsChar() {
71 if (GetType() == OptionValue::eTypeChar)
72 return static_cast<OptionValueChar *>(this);
76 OptionValueFileSpec *OptionValue::GetAsFileSpec() {
77 if (GetType() == OptionValue::eTypeFileSpec)
78 return static_cast<OptionValueFileSpec *>(this);
82 const OptionValueFileSpec *OptionValue::GetAsFileSpec() const {
83 if (GetType() == OptionValue::eTypeFileSpec)
84 return static_cast<const OptionValueFileSpec *>(this);
88 OptionValueFileSpecList *OptionValue::GetAsFileSpecList() {
89 if (GetType() == OptionValue::eTypeFileSpecList)
90 return static_cast<OptionValueFileSpecList *>(this);
94 const OptionValueFileSpecList *OptionValue::GetAsFileSpecList() const {
95 if (GetType() == OptionValue::eTypeFileSpecList)
96 return static_cast<const OptionValueFileSpecList *>(this);
100 OptionValueArch *OptionValue::GetAsArch() {
101 if (GetType() == OptionValue::eTypeArch)
102 return static_cast<OptionValueArch *>(this);
106 const OptionValueArch *OptionValue::GetAsArch() const {
107 if (GetType() == OptionValue::eTypeArch)
108 return static_cast<const OptionValueArch *>(this);
112 OptionValueArray *OptionValue::GetAsArray() {
113 if (GetType() == OptionValue::eTypeArray)
114 return static_cast<OptionValueArray *>(this);
118 const OptionValueArray *OptionValue::GetAsArray() const {
119 if (GetType() == OptionValue::eTypeArray)
120 return static_cast<const OptionValueArray *>(this);
124 OptionValueArgs *OptionValue::GetAsArgs() {
125 if (GetType() == OptionValue::eTypeArgs)
126 return static_cast<OptionValueArgs *>(this);
130 const OptionValueArgs *OptionValue::GetAsArgs() const {
131 if (GetType() == OptionValue::eTypeArgs)
132 return static_cast<const OptionValueArgs *>(this);
136 OptionValueDictionary *OptionValue::GetAsDictionary() {
137 if (GetType() == OptionValue::eTypeDictionary)
138 return static_cast<OptionValueDictionary *>(this);
142 const OptionValueDictionary *OptionValue::GetAsDictionary() const {
143 if (GetType() == OptionValue::eTypeDictionary)
144 return static_cast<const OptionValueDictionary *>(this);
148 OptionValueEnumeration *OptionValue::GetAsEnumeration() {
149 if (GetType() == OptionValue::eTypeEnum)
150 return static_cast<OptionValueEnumeration *>(this);
154 const OptionValueEnumeration *OptionValue::GetAsEnumeration() const {
155 if (GetType() == OptionValue::eTypeEnum)
156 return static_cast<const OptionValueEnumeration *>(this);
160 OptionValueFormat *OptionValue::GetAsFormat() {
161 if (GetType() == OptionValue::eTypeFormat)
162 return static_cast<OptionValueFormat *>(this);
166 const OptionValueFormat *OptionValue::GetAsFormat() const {
167 if (GetType() == OptionValue::eTypeFormat)
168 return static_cast<const OptionValueFormat *>(this);
172 OptionValueLanguage *OptionValue::GetAsLanguage() {
173 if (GetType() == OptionValue::eTypeLanguage)
174 return static_cast<OptionValueLanguage *>(this);
178 const OptionValueLanguage *OptionValue::GetAsLanguage() const {
179 if (GetType() == OptionValue::eTypeLanguage)
180 return static_cast<const OptionValueLanguage *>(this);
184 OptionValueFormatEntity *OptionValue::GetAsFormatEntity() {
185 if (GetType() == OptionValue::eTypeFormatEntity)
186 return static_cast<OptionValueFormatEntity *>(this);
190 const OptionValueFormatEntity *OptionValue::GetAsFormatEntity() const {
191 if (GetType() == OptionValue::eTypeFormatEntity)
192 return static_cast<const OptionValueFormatEntity *>(this);
196 OptionValuePathMappings *OptionValue::GetAsPathMappings() {
197 if (GetType() == OptionValue::eTypePathMap)
198 return static_cast<OptionValuePathMappings *>(this);
202 const OptionValuePathMappings *OptionValue::GetAsPathMappings() const {
203 if (GetType() == OptionValue::eTypePathMap)
204 return static_cast<const OptionValuePathMappings *>(this);
208 OptionValueProperties *OptionValue::GetAsProperties() {
209 if (GetType() == OptionValue::eTypeProperties)
210 return static_cast<OptionValueProperties *>(this);
214 const OptionValueProperties *OptionValue::GetAsProperties() const {
215 if (GetType() == OptionValue::eTypeProperties)
216 return static_cast<const OptionValueProperties *>(this);
220 OptionValueRegex *OptionValue::GetAsRegex() {
221 if (GetType() == OptionValue::eTypeRegex)
222 return static_cast<OptionValueRegex *>(this);
226 const OptionValueRegex *OptionValue::GetAsRegex() const {
227 if (GetType() == OptionValue::eTypeRegex)
228 return static_cast<const OptionValueRegex *>(this);
232 OptionValueSInt64 *OptionValue::GetAsSInt64() {
233 if (GetType() == OptionValue::eTypeSInt64)
234 return static_cast<OptionValueSInt64 *>(this);
238 const OptionValueSInt64 *OptionValue::GetAsSInt64() const {
239 if (GetType() == OptionValue::eTypeSInt64)
240 return static_cast<const OptionValueSInt64 *>(this);
244 OptionValueString *OptionValue::GetAsString() {
245 if (GetType() == OptionValue::eTypeString)
246 return static_cast<OptionValueString *>(this);
250 const OptionValueString *OptionValue::GetAsString() const {
251 if (GetType() == OptionValue::eTypeString)
252 return static_cast<const OptionValueString *>(this);
256 OptionValueUInt64 *OptionValue::GetAsUInt64() {
257 if (GetType() == OptionValue::eTypeUInt64)
258 return static_cast<OptionValueUInt64 *>(this);
262 const OptionValueUInt64 *OptionValue::GetAsUInt64() const {
263 if (GetType() == OptionValue::eTypeUInt64)
264 return static_cast<const OptionValueUInt64 *>(this);
268 OptionValueUUID *OptionValue::GetAsUUID() {
269 if (GetType() == OptionValue::eTypeUUID)
270 return static_cast<OptionValueUUID *>(this);
274 const OptionValueUUID *OptionValue::GetAsUUID() const {
275 if (GetType() == OptionValue::eTypeUUID)
276 return static_cast<const OptionValueUUID *>(this);
280 bool OptionValue::GetBooleanValue(bool fail_value) const {
281 const OptionValueBoolean *option_value = GetAsBoolean();
283 return option_value->GetCurrentValue();
287 bool OptionValue::SetBooleanValue(bool new_value) {
288 OptionValueBoolean *option_value = GetAsBoolean();
290 option_value->SetCurrentValue(new_value);
296 char OptionValue::GetCharValue(char fail_value) const {
297 const OptionValueChar *option_value = GetAsChar();
299 return option_value->GetCurrentValue();
303 char OptionValue::SetCharValue(char new_value) {
304 OptionValueChar *option_value = GetAsChar();
306 option_value->SetCurrentValue(new_value);
312 int64_t OptionValue::GetEnumerationValue(int64_t fail_value) const {
313 const OptionValueEnumeration *option_value = GetAsEnumeration();
315 return option_value->GetCurrentValue();
319 bool OptionValue::SetEnumerationValue(int64_t value) {
320 OptionValueEnumeration *option_value = GetAsEnumeration();
322 option_value->SetCurrentValue(value);
328 FileSpec OptionValue::GetFileSpecValue() const {
329 const OptionValueFileSpec *option_value = GetAsFileSpec();
331 return option_value->GetCurrentValue();
335 bool OptionValue::SetFileSpecValue(const FileSpec &file_spec) {
336 OptionValueFileSpec *option_value = GetAsFileSpec();
338 option_value->SetCurrentValue(file_spec, false);
344 FileSpecList OptionValue::GetFileSpecListValue() const {
345 const OptionValueFileSpecList *option_value = GetAsFileSpecList();
347 return option_value->GetCurrentValue();
348 return FileSpecList();
351 lldb::Format OptionValue::GetFormatValue(lldb::Format fail_value) const {
352 const OptionValueFormat *option_value = GetAsFormat();
354 return option_value->GetCurrentValue();
358 bool OptionValue::SetFormatValue(lldb::Format new_value) {
359 OptionValueFormat *option_value = GetAsFormat();
361 option_value->SetCurrentValue(new_value);
368 OptionValue::GetLanguageValue(lldb::LanguageType fail_value) const {
369 const OptionValueLanguage *option_value = GetAsLanguage();
371 return option_value->GetCurrentValue();
375 bool OptionValue::SetLanguageValue(lldb::LanguageType new_language) {
376 OptionValueLanguage *option_value = GetAsLanguage();
378 option_value->SetCurrentValue(new_language);
384 const FormatEntity::Entry *OptionValue::GetFormatEntity() const {
385 const OptionValueFormatEntity *option_value = GetAsFormatEntity();
387 return &option_value->GetCurrentValue();
391 const RegularExpression *OptionValue::GetRegexValue() const {
392 const OptionValueRegex *option_value = GetAsRegex();
394 return option_value->GetCurrentValue();
398 int64_t OptionValue::GetSInt64Value(int64_t fail_value) const {
399 const OptionValueSInt64 *option_value = GetAsSInt64();
401 return option_value->GetCurrentValue();
405 bool OptionValue::SetSInt64Value(int64_t new_value) {
406 OptionValueSInt64 *option_value = GetAsSInt64();
408 option_value->SetCurrentValue(new_value);
414 llvm::StringRef OptionValue::GetStringValue(llvm::StringRef fail_value) const {
415 const OptionValueString *option_value = GetAsString();
417 return option_value->GetCurrentValueAsRef();
421 bool OptionValue::SetStringValue(llvm::StringRef new_value) {
422 OptionValueString *option_value = GetAsString();
424 option_value->SetCurrentValue(new_value);
430 uint64_t OptionValue::GetUInt64Value(uint64_t fail_value) const {
431 const OptionValueUInt64 *option_value = GetAsUInt64();
433 return option_value->GetCurrentValue();
437 bool OptionValue::SetUInt64Value(uint64_t new_value) {
438 OptionValueUInt64 *option_value = GetAsUInt64();
440 option_value->SetCurrentValue(new_value);
446 UUID OptionValue::GetUUIDValue() const {
447 const OptionValueUUID *option_value = GetAsUUID();
449 return option_value->GetCurrentValue();
453 bool OptionValue::SetUUIDValue(const UUID &uuid) {
454 OptionValueUUID *option_value = GetAsUUID();
456 option_value->SetCurrentValue(uuid);
462 const char *OptionValue::GetBuiltinTypeAsCString(Type t) {
476 case eTypeDictionary:
482 case eTypeFileSpecList:
486 case eTypeFormatEntity:
487 return "format-string";
492 case eTypeProperties:
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;
514 case 1u << eTypeArch:
515 value_sp.reset(new OptionValueArch());
517 case 1u << eTypeBoolean:
518 value_sp.reset(new OptionValueBoolean(false));
520 case 1u << eTypeChar:
521 value_sp.reset(new OptionValueChar('\0'));
523 case 1u << eTypeFileSpec:
524 value_sp.reset(new OptionValueFileSpec());
526 case 1u << eTypeFormat:
527 value_sp.reset(new OptionValueFormat(eFormatInvalid));
529 case 1u << eTypeFormatEntity:
530 value_sp.reset(new OptionValueFormatEntity(NULL));
532 case 1u << eTypeLanguage:
533 value_sp.reset(new OptionValueLanguage(eLanguageTypeUnknown));
535 case 1u << eTypeSInt64:
536 value_sp.reset(new OptionValueSInt64());
538 case 1u << eTypeString:
539 value_sp.reset(new OptionValueString());
541 case 1u << eTypeUInt64:
542 value_sp.reset(new OptionValueUInt64());
544 case 1u << eTypeUUID:
545 value_sp.reset(new OptionValueUUID());
550 error = value_sp->SetValueFromString(
551 llvm::StringRef::withNullAsEmpty(value_cstr), eVarSetOperationAssign);
553 error.SetErrorString("unsupported type mask");
557 bool OptionValue::DumpQualifiedName(Stream &strm) const {
558 bool dumped_something = false;
559 lldb::OptionValueSP m_parent_sp(m_parent_wp.lock());
561 if (m_parent_sp->DumpQualifiedName(strm))
562 dumped_something = true;
564 ConstString name(GetName());
566 if (dumped_something)
569 dumped_something = true;
572 return dumped_something;
575 size_t OptionValue::AutoComplete(CommandInterpreter &interpreter,
576 CompletionRequest &request) {
577 request.SetWordComplete(false);
578 return request.GetNumberOfMatches();
581 Status OptionValue::SetValueFromString(llvm::StringRef value,
582 VarSetOperationType op) {
585 case eVarSetOperationReplace:
586 error.SetErrorStringWithFormat(
587 "%s objects do not support the 'replace' operation",
590 case eVarSetOperationInsertBefore:
591 error.SetErrorStringWithFormat(
592 "%s objects do not support the 'insert-before' operation",
595 case eVarSetOperationInsertAfter:
596 error.SetErrorStringWithFormat(
597 "%s objects do not support the 'insert-after' operation",
600 case eVarSetOperationRemove:
601 error.SetErrorStringWithFormat(
602 "%s objects do not support the 'remove' operation", GetTypeAsCString());
604 case eVarSetOperationAppend:
605 error.SetErrorStringWithFormat(
606 "%s objects do not support the 'append' operation", GetTypeAsCString());
608 case eVarSetOperationClear:
609 error.SetErrorStringWithFormat(
610 "%s objects do not support the 'clear' operation", GetTypeAsCString());
612 case eVarSetOperationAssign:
613 error.SetErrorStringWithFormat(
614 "%s objects do not support the 'assign' operation", GetTypeAsCString());
616 case eVarSetOperationInvalid:
617 error.SetErrorStringWithFormat("invalid operation performed on a %s object",