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,
24 // uint64_t or int64_t. Other types will cause "fail_value" to be
26 //-------------------------------------------------------------------------
27 uint64_t OptionValue::GetUInt64Value(uint64_t fail_value, bool *success_ptr) {
31 case OptionValue::eTypeBoolean:
32 return static_cast<OptionValueBoolean *>(this)->GetCurrentValue();
33 case OptionValue::eTypeSInt64:
34 return static_cast<OptionValueSInt64 *>(this)->GetCurrentValue();
35 case OptionValue::eTypeUInt64:
36 return static_cast<OptionValueUInt64 *>(this)->GetCurrentValue();
45 Status OptionValue::SetSubValue(const ExecutionContext *exe_ctx,
46 VarSetOperationType op, llvm::StringRef name,
47 llvm::StringRef value) {
49 error.SetErrorStringWithFormat("SetSubValue is not supported");
53 OptionValueBoolean *OptionValue::GetAsBoolean() {
54 if (GetType() == OptionValue::eTypeBoolean)
55 return static_cast<OptionValueBoolean *>(this);
59 const OptionValueBoolean *OptionValue::GetAsBoolean() const {
60 if (GetType() == OptionValue::eTypeBoolean)
61 return static_cast<const OptionValueBoolean *>(this);
65 const OptionValueChar *OptionValue::GetAsChar() const {
66 if (GetType() == OptionValue::eTypeChar)
67 return static_cast<const OptionValueChar *>(this);
71 OptionValueChar *OptionValue::GetAsChar() {
72 if (GetType() == OptionValue::eTypeChar)
73 return static_cast<OptionValueChar *>(this);
77 OptionValueFileSpec *OptionValue::GetAsFileSpec() {
78 if (GetType() == OptionValue::eTypeFileSpec)
79 return static_cast<OptionValueFileSpec *>(this);
83 const OptionValueFileSpec *OptionValue::GetAsFileSpec() const {
84 if (GetType() == OptionValue::eTypeFileSpec)
85 return static_cast<const OptionValueFileSpec *>(this);
89 OptionValueFileSpecList *OptionValue::GetAsFileSpecList() {
90 if (GetType() == OptionValue::eTypeFileSpecList)
91 return static_cast<OptionValueFileSpecList *>(this);
95 const OptionValueFileSpecList *OptionValue::GetAsFileSpecList() const {
96 if (GetType() == OptionValue::eTypeFileSpecList)
97 return static_cast<const OptionValueFileSpecList *>(this);
101 OptionValueArch *OptionValue::GetAsArch() {
102 if (GetType() == OptionValue::eTypeArch)
103 return static_cast<OptionValueArch *>(this);
107 const OptionValueArch *OptionValue::GetAsArch() const {
108 if (GetType() == OptionValue::eTypeArch)
109 return static_cast<const OptionValueArch *>(this);
113 OptionValueArray *OptionValue::GetAsArray() {
114 if (GetType() == OptionValue::eTypeArray)
115 return static_cast<OptionValueArray *>(this);
119 const OptionValueArray *OptionValue::GetAsArray() const {
120 if (GetType() == OptionValue::eTypeArray)
121 return static_cast<const OptionValueArray *>(this);
125 OptionValueArgs *OptionValue::GetAsArgs() {
126 if (GetType() == OptionValue::eTypeArgs)
127 return static_cast<OptionValueArgs *>(this);
131 const OptionValueArgs *OptionValue::GetAsArgs() const {
132 if (GetType() == OptionValue::eTypeArgs)
133 return static_cast<const OptionValueArgs *>(this);
137 OptionValueDictionary *OptionValue::GetAsDictionary() {
138 if (GetType() == OptionValue::eTypeDictionary)
139 return static_cast<OptionValueDictionary *>(this);
143 const OptionValueDictionary *OptionValue::GetAsDictionary() const {
144 if (GetType() == OptionValue::eTypeDictionary)
145 return static_cast<const OptionValueDictionary *>(this);
149 OptionValueEnumeration *OptionValue::GetAsEnumeration() {
150 if (GetType() == OptionValue::eTypeEnum)
151 return static_cast<OptionValueEnumeration *>(this);
155 const OptionValueEnumeration *OptionValue::GetAsEnumeration() const {
156 if (GetType() == OptionValue::eTypeEnum)
157 return static_cast<const OptionValueEnumeration *>(this);
161 OptionValueFormat *OptionValue::GetAsFormat() {
162 if (GetType() == OptionValue::eTypeFormat)
163 return static_cast<OptionValueFormat *>(this);
167 const OptionValueFormat *OptionValue::GetAsFormat() const {
168 if (GetType() == OptionValue::eTypeFormat)
169 return static_cast<const OptionValueFormat *>(this);
173 OptionValueLanguage *OptionValue::GetAsLanguage() {
174 if (GetType() == OptionValue::eTypeLanguage)
175 return static_cast<OptionValueLanguage *>(this);
179 const OptionValueLanguage *OptionValue::GetAsLanguage() const {
180 if (GetType() == OptionValue::eTypeLanguage)
181 return static_cast<const OptionValueLanguage *>(this);
185 OptionValueFormatEntity *OptionValue::GetAsFormatEntity() {
186 if (GetType() == OptionValue::eTypeFormatEntity)
187 return static_cast<OptionValueFormatEntity *>(this);
191 const OptionValueFormatEntity *OptionValue::GetAsFormatEntity() const {
192 if (GetType() == OptionValue::eTypeFormatEntity)
193 return static_cast<const OptionValueFormatEntity *>(this);
197 OptionValuePathMappings *OptionValue::GetAsPathMappings() {
198 if (GetType() == OptionValue::eTypePathMap)
199 return static_cast<OptionValuePathMappings *>(this);
203 const OptionValuePathMappings *OptionValue::GetAsPathMappings() const {
204 if (GetType() == OptionValue::eTypePathMap)
205 return static_cast<const OptionValuePathMappings *>(this);
209 OptionValueProperties *OptionValue::GetAsProperties() {
210 if (GetType() == OptionValue::eTypeProperties)
211 return static_cast<OptionValueProperties *>(this);
215 const OptionValueProperties *OptionValue::GetAsProperties() const {
216 if (GetType() == OptionValue::eTypeProperties)
217 return static_cast<const OptionValueProperties *>(this);
221 OptionValueRegex *OptionValue::GetAsRegex() {
222 if (GetType() == OptionValue::eTypeRegex)
223 return static_cast<OptionValueRegex *>(this);
227 const OptionValueRegex *OptionValue::GetAsRegex() const {
228 if (GetType() == OptionValue::eTypeRegex)
229 return static_cast<const OptionValueRegex *>(this);
233 OptionValueSInt64 *OptionValue::GetAsSInt64() {
234 if (GetType() == OptionValue::eTypeSInt64)
235 return static_cast<OptionValueSInt64 *>(this);
239 const OptionValueSInt64 *OptionValue::GetAsSInt64() const {
240 if (GetType() == OptionValue::eTypeSInt64)
241 return static_cast<const OptionValueSInt64 *>(this);
245 OptionValueString *OptionValue::GetAsString() {
246 if (GetType() == OptionValue::eTypeString)
247 return static_cast<OptionValueString *>(this);
251 const OptionValueString *OptionValue::GetAsString() const {
252 if (GetType() == OptionValue::eTypeString)
253 return static_cast<const OptionValueString *>(this);
257 OptionValueUInt64 *OptionValue::GetAsUInt64() {
258 if (GetType() == OptionValue::eTypeUInt64)
259 return static_cast<OptionValueUInt64 *>(this);
263 const OptionValueUInt64 *OptionValue::GetAsUInt64() const {
264 if (GetType() == OptionValue::eTypeUInt64)
265 return static_cast<const OptionValueUInt64 *>(this);
269 OptionValueUUID *OptionValue::GetAsUUID() {
270 if (GetType() == OptionValue::eTypeUUID)
271 return static_cast<OptionValueUUID *>(this);
275 const OptionValueUUID *OptionValue::GetAsUUID() const {
276 if (GetType() == OptionValue::eTypeUUID)
277 return static_cast<const OptionValueUUID *>(this);
281 bool OptionValue::GetBooleanValue(bool fail_value) const {
282 const OptionValueBoolean *option_value = GetAsBoolean();
284 return option_value->GetCurrentValue();
288 bool OptionValue::SetBooleanValue(bool new_value) {
289 OptionValueBoolean *option_value = GetAsBoolean();
291 option_value->SetCurrentValue(new_value);
297 char OptionValue::GetCharValue(char fail_value) const {
298 const OptionValueChar *option_value = GetAsChar();
300 return option_value->GetCurrentValue();
304 char OptionValue::SetCharValue(char new_value) {
305 OptionValueChar *option_value = GetAsChar();
307 option_value->SetCurrentValue(new_value);
313 int64_t OptionValue::GetEnumerationValue(int64_t fail_value) const {
314 const OptionValueEnumeration *option_value = GetAsEnumeration();
316 return option_value->GetCurrentValue();
320 bool OptionValue::SetEnumerationValue(int64_t value) {
321 OptionValueEnumeration *option_value = GetAsEnumeration();
323 option_value->SetCurrentValue(value);
329 FileSpec OptionValue::GetFileSpecValue() const {
330 const OptionValueFileSpec *option_value = GetAsFileSpec();
332 return option_value->GetCurrentValue();
336 bool OptionValue::SetFileSpecValue(const FileSpec &file_spec) {
337 OptionValueFileSpec *option_value = GetAsFileSpec();
339 option_value->SetCurrentValue(file_spec, false);
345 FileSpecList OptionValue::GetFileSpecListValue() const {
346 const OptionValueFileSpecList *option_value = GetAsFileSpecList();
348 return option_value->GetCurrentValue();
349 return FileSpecList();
352 lldb::Format OptionValue::GetFormatValue(lldb::Format fail_value) const {
353 const OptionValueFormat *option_value = GetAsFormat();
355 return option_value->GetCurrentValue();
359 bool OptionValue::SetFormatValue(lldb::Format new_value) {
360 OptionValueFormat *option_value = GetAsFormat();
362 option_value->SetCurrentValue(new_value);
369 OptionValue::GetLanguageValue(lldb::LanguageType fail_value) const {
370 const OptionValueLanguage *option_value = GetAsLanguage();
372 return option_value->GetCurrentValue();
376 bool OptionValue::SetLanguageValue(lldb::LanguageType new_language) {
377 OptionValueLanguage *option_value = GetAsLanguage();
379 option_value->SetCurrentValue(new_language);
385 const FormatEntity::Entry *OptionValue::GetFormatEntity() const {
386 const OptionValueFormatEntity *option_value = GetAsFormatEntity();
388 return &option_value->GetCurrentValue();
392 const RegularExpression *OptionValue::GetRegexValue() const {
393 const OptionValueRegex *option_value = GetAsRegex();
395 return option_value->GetCurrentValue();
399 int64_t OptionValue::GetSInt64Value(int64_t fail_value) const {
400 const OptionValueSInt64 *option_value = GetAsSInt64();
402 return option_value->GetCurrentValue();
406 bool OptionValue::SetSInt64Value(int64_t new_value) {
407 OptionValueSInt64 *option_value = GetAsSInt64();
409 option_value->SetCurrentValue(new_value);
415 llvm::StringRef OptionValue::GetStringValue(llvm::StringRef fail_value) const {
416 const OptionValueString *option_value = GetAsString();
418 return option_value->GetCurrentValueAsRef();
422 bool OptionValue::SetStringValue(llvm::StringRef new_value) {
423 OptionValueString *option_value = GetAsString();
425 option_value->SetCurrentValue(new_value);
431 uint64_t OptionValue::GetUInt64Value(uint64_t fail_value) const {
432 const OptionValueUInt64 *option_value = GetAsUInt64();
434 return option_value->GetCurrentValue();
438 bool OptionValue::SetUInt64Value(uint64_t new_value) {
439 OptionValueUInt64 *option_value = GetAsUInt64();
441 option_value->SetCurrentValue(new_value);
447 UUID OptionValue::GetUUIDValue() const {
448 const OptionValueUUID *option_value = GetAsUUID();
450 return option_value->GetCurrentValue();
454 bool OptionValue::SetUUIDValue(const UUID &uuid) {
455 OptionValueUUID *option_value = GetAsUUID();
457 option_value->SetCurrentValue(uuid);
463 const char *OptionValue::GetBuiltinTypeAsCString(Type t) {
477 case eTypeDictionary:
483 case eTypeFileSpecList:
487 case eTypeFormatEntity:
488 return "format-string";
493 case eTypeProperties:
509 lldb::OptionValueSP OptionValue::CreateValueFromCStringForTypeMask(
510 const char *value_cstr, uint32_t type_mask, Status &error) {
511 // If only 1 bit is set in the type mask for a dictionary or array
512 // then we know how to decode a value from a cstring
513 lldb::OptionValueSP value_sp;
515 case 1u << eTypeArch:
516 value_sp.reset(new OptionValueArch());
518 case 1u << eTypeBoolean:
519 value_sp.reset(new OptionValueBoolean(false));
521 case 1u << eTypeChar:
522 value_sp.reset(new OptionValueChar('\0'));
524 case 1u << eTypeFileSpec:
525 value_sp.reset(new OptionValueFileSpec());
527 case 1u << eTypeFormat:
528 value_sp.reset(new OptionValueFormat(eFormatInvalid));
530 case 1u << eTypeFormatEntity:
531 value_sp.reset(new OptionValueFormatEntity(NULL));
533 case 1u << eTypeLanguage:
534 value_sp.reset(new OptionValueLanguage(eLanguageTypeUnknown));
536 case 1u << eTypeSInt64:
537 value_sp.reset(new OptionValueSInt64());
539 case 1u << eTypeString:
540 value_sp.reset(new OptionValueString());
542 case 1u << eTypeUInt64:
543 value_sp.reset(new OptionValueUInt64());
545 case 1u << eTypeUUID:
546 value_sp.reset(new OptionValueUUID());
551 error = value_sp->SetValueFromString(
552 llvm::StringRef::withNullAsEmpty(value_cstr), eVarSetOperationAssign);
554 error.SetErrorString("unsupported type mask");
558 bool OptionValue::DumpQualifiedName(Stream &strm) const {
559 bool dumped_something = false;
560 lldb::OptionValueSP m_parent_sp(m_parent_wp.lock());
562 if (m_parent_sp->DumpQualifiedName(strm))
563 dumped_something = true;
565 ConstString name(GetName());
567 if (dumped_something)
570 dumped_something = true;
573 return dumped_something;
576 size_t OptionValue::AutoComplete(CommandInterpreter &interpreter,
577 llvm::StringRef s, int match_start_point,
578 int max_return_elements, bool &word_complete,
579 StringList &matches) {
580 word_complete = false;
582 return matches.GetSize();
585 Status OptionValue::SetValueFromString(llvm::StringRef value,
586 VarSetOperationType op) {
589 case eVarSetOperationReplace:
590 error.SetErrorStringWithFormat(
591 "%s objects do not support the 'replace' operation",
594 case eVarSetOperationInsertBefore:
595 error.SetErrorStringWithFormat(
596 "%s objects do not support the 'insert-before' operation",
599 case eVarSetOperationInsertAfter:
600 error.SetErrorStringWithFormat(
601 "%s objects do not support the 'insert-after' operation",
604 case eVarSetOperationRemove:
605 error.SetErrorStringWithFormat(
606 "%s objects do not support the 'remove' operation", GetTypeAsCString());
608 case eVarSetOperationAppend:
609 error.SetErrorStringWithFormat(
610 "%s objects do not support the 'append' operation", GetTypeAsCString());
612 case eVarSetOperationClear:
613 error.SetErrorStringWithFormat(
614 "%s objects do not support the 'clear' operation", GetTypeAsCString());
616 case eVarSetOperationAssign:
617 error.SetErrorStringWithFormat(
618 "%s objects do not support the 'assign' operation", GetTypeAsCString());
620 case eVarSetOperationInvalid:
621 error.SetErrorStringWithFormat("invalid operation performed on a %s object",