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"
12 #include "lldb/Interpreter/OptionValues.h"
13 #include "lldb/Utility/StringList.h"
16 using namespace lldb_private;
18 //-------------------------------------------------------------------------
19 // Get this value as a uint64_t value if it is encoded as a boolean, uint64_t
20 // or int64_t. Other types will cause "fail_value" to be returned
21 //-------------------------------------------------------------------------
22 uint64_t OptionValue::GetUInt64Value(uint64_t fail_value, bool *success_ptr) {
26 case OptionValue::eTypeBoolean:
27 return static_cast<OptionValueBoolean *>(this)->GetCurrentValue();
28 case OptionValue::eTypeSInt64:
29 return static_cast<OptionValueSInt64 *>(this)->GetCurrentValue();
30 case OptionValue::eTypeUInt64:
31 return static_cast<OptionValueUInt64 *>(this)->GetCurrentValue();
40 Status OptionValue::SetSubValue(const ExecutionContext *exe_ctx,
41 VarSetOperationType op, llvm::StringRef name,
42 llvm::StringRef value) {
44 error.SetErrorStringWithFormat("SetSubValue is not supported");
48 OptionValueBoolean *OptionValue::GetAsBoolean() {
49 if (GetType() == OptionValue::eTypeBoolean)
50 return static_cast<OptionValueBoolean *>(this);
54 const OptionValueBoolean *OptionValue::GetAsBoolean() const {
55 if (GetType() == OptionValue::eTypeBoolean)
56 return static_cast<const OptionValueBoolean *>(this);
60 const OptionValueChar *OptionValue::GetAsChar() const {
61 if (GetType() == OptionValue::eTypeChar)
62 return static_cast<const OptionValueChar *>(this);
66 OptionValueChar *OptionValue::GetAsChar() {
67 if (GetType() == OptionValue::eTypeChar)
68 return static_cast<OptionValueChar *>(this);
72 OptionValueFileSpec *OptionValue::GetAsFileSpec() {
73 if (GetType() == OptionValue::eTypeFileSpec)
74 return static_cast<OptionValueFileSpec *>(this);
78 const OptionValueFileSpec *OptionValue::GetAsFileSpec() const {
79 if (GetType() == OptionValue::eTypeFileSpec)
80 return static_cast<const OptionValueFileSpec *>(this);
84 OptionValueFileSpecList *OptionValue::GetAsFileSpecList() {
85 if (GetType() == OptionValue::eTypeFileSpecList)
86 return static_cast<OptionValueFileSpecList *>(this);
90 const OptionValueFileSpecList *OptionValue::GetAsFileSpecList() const {
91 if (GetType() == OptionValue::eTypeFileSpecList)
92 return static_cast<const OptionValueFileSpecList *>(this);
96 OptionValueArch *OptionValue::GetAsArch() {
97 if (GetType() == OptionValue::eTypeArch)
98 return static_cast<OptionValueArch *>(this);
102 const OptionValueArch *OptionValue::GetAsArch() const {
103 if (GetType() == OptionValue::eTypeArch)
104 return static_cast<const OptionValueArch *>(this);
108 OptionValueArray *OptionValue::GetAsArray() {
109 if (GetType() == OptionValue::eTypeArray)
110 return static_cast<OptionValueArray *>(this);
114 const OptionValueArray *OptionValue::GetAsArray() const {
115 if (GetType() == OptionValue::eTypeArray)
116 return static_cast<const OptionValueArray *>(this);
120 OptionValueArgs *OptionValue::GetAsArgs() {
121 if (GetType() == OptionValue::eTypeArgs)
122 return static_cast<OptionValueArgs *>(this);
126 const OptionValueArgs *OptionValue::GetAsArgs() const {
127 if (GetType() == OptionValue::eTypeArgs)
128 return static_cast<const OptionValueArgs *>(this);
132 OptionValueDictionary *OptionValue::GetAsDictionary() {
133 if (GetType() == OptionValue::eTypeDictionary)
134 return static_cast<OptionValueDictionary *>(this);
138 const OptionValueDictionary *OptionValue::GetAsDictionary() const {
139 if (GetType() == OptionValue::eTypeDictionary)
140 return static_cast<const OptionValueDictionary *>(this);
144 OptionValueEnumeration *OptionValue::GetAsEnumeration() {
145 if (GetType() == OptionValue::eTypeEnum)
146 return static_cast<OptionValueEnumeration *>(this);
150 const OptionValueEnumeration *OptionValue::GetAsEnumeration() const {
151 if (GetType() == OptionValue::eTypeEnum)
152 return static_cast<const OptionValueEnumeration *>(this);
156 OptionValueFormat *OptionValue::GetAsFormat() {
157 if (GetType() == OptionValue::eTypeFormat)
158 return static_cast<OptionValueFormat *>(this);
162 const OptionValueFormat *OptionValue::GetAsFormat() const {
163 if (GetType() == OptionValue::eTypeFormat)
164 return static_cast<const OptionValueFormat *>(this);
168 OptionValueLanguage *OptionValue::GetAsLanguage() {
169 if (GetType() == OptionValue::eTypeLanguage)
170 return static_cast<OptionValueLanguage *>(this);
174 const OptionValueLanguage *OptionValue::GetAsLanguage() const {
175 if (GetType() == OptionValue::eTypeLanguage)
176 return static_cast<const OptionValueLanguage *>(this);
180 OptionValueFormatEntity *OptionValue::GetAsFormatEntity() {
181 if (GetType() == OptionValue::eTypeFormatEntity)
182 return static_cast<OptionValueFormatEntity *>(this);
186 const OptionValueFormatEntity *OptionValue::GetAsFormatEntity() const {
187 if (GetType() == OptionValue::eTypeFormatEntity)
188 return static_cast<const OptionValueFormatEntity *>(this);
192 OptionValuePathMappings *OptionValue::GetAsPathMappings() {
193 if (GetType() == OptionValue::eTypePathMap)
194 return static_cast<OptionValuePathMappings *>(this);
198 const OptionValuePathMappings *OptionValue::GetAsPathMappings() const {
199 if (GetType() == OptionValue::eTypePathMap)
200 return static_cast<const OptionValuePathMappings *>(this);
204 OptionValueProperties *OptionValue::GetAsProperties() {
205 if (GetType() == OptionValue::eTypeProperties)
206 return static_cast<OptionValueProperties *>(this);
210 const OptionValueProperties *OptionValue::GetAsProperties() const {
211 if (GetType() == OptionValue::eTypeProperties)
212 return static_cast<const OptionValueProperties *>(this);
216 OptionValueRegex *OptionValue::GetAsRegex() {
217 if (GetType() == OptionValue::eTypeRegex)
218 return static_cast<OptionValueRegex *>(this);
222 const OptionValueRegex *OptionValue::GetAsRegex() const {
223 if (GetType() == OptionValue::eTypeRegex)
224 return static_cast<const OptionValueRegex *>(this);
228 OptionValueSInt64 *OptionValue::GetAsSInt64() {
229 if (GetType() == OptionValue::eTypeSInt64)
230 return static_cast<OptionValueSInt64 *>(this);
234 const OptionValueSInt64 *OptionValue::GetAsSInt64() const {
235 if (GetType() == OptionValue::eTypeSInt64)
236 return static_cast<const OptionValueSInt64 *>(this);
240 OptionValueString *OptionValue::GetAsString() {
241 if (GetType() == OptionValue::eTypeString)
242 return static_cast<OptionValueString *>(this);
246 const OptionValueString *OptionValue::GetAsString() const {
247 if (GetType() == OptionValue::eTypeString)
248 return static_cast<const OptionValueString *>(this);
252 OptionValueUInt64 *OptionValue::GetAsUInt64() {
253 if (GetType() == OptionValue::eTypeUInt64)
254 return static_cast<OptionValueUInt64 *>(this);
258 const OptionValueUInt64 *OptionValue::GetAsUInt64() const {
259 if (GetType() == OptionValue::eTypeUInt64)
260 return static_cast<const OptionValueUInt64 *>(this);
264 OptionValueUUID *OptionValue::GetAsUUID() {
265 if (GetType() == OptionValue::eTypeUUID)
266 return static_cast<OptionValueUUID *>(this);
270 const OptionValueUUID *OptionValue::GetAsUUID() const {
271 if (GetType() == OptionValue::eTypeUUID)
272 return static_cast<const OptionValueUUID *>(this);
276 bool OptionValue::GetBooleanValue(bool fail_value) const {
277 const OptionValueBoolean *option_value = GetAsBoolean();
279 return option_value->GetCurrentValue();
283 bool OptionValue::SetBooleanValue(bool new_value) {
284 OptionValueBoolean *option_value = GetAsBoolean();
286 option_value->SetCurrentValue(new_value);
292 char OptionValue::GetCharValue(char fail_value) const {
293 const OptionValueChar *option_value = GetAsChar();
295 return option_value->GetCurrentValue();
299 char OptionValue::SetCharValue(char new_value) {
300 OptionValueChar *option_value = GetAsChar();
302 option_value->SetCurrentValue(new_value);
308 int64_t OptionValue::GetEnumerationValue(int64_t fail_value) const {
309 const OptionValueEnumeration *option_value = GetAsEnumeration();
311 return option_value->GetCurrentValue();
315 bool OptionValue::SetEnumerationValue(int64_t value) {
316 OptionValueEnumeration *option_value = GetAsEnumeration();
318 option_value->SetCurrentValue(value);
324 FileSpec OptionValue::GetFileSpecValue() const {
325 const OptionValueFileSpec *option_value = GetAsFileSpec();
327 return option_value->GetCurrentValue();
331 bool OptionValue::SetFileSpecValue(const FileSpec &file_spec) {
332 OptionValueFileSpec *option_value = GetAsFileSpec();
334 option_value->SetCurrentValue(file_spec, false);
340 FileSpecList OptionValue::GetFileSpecListValue() const {
341 const OptionValueFileSpecList *option_value = GetAsFileSpecList();
343 return option_value->GetCurrentValue();
344 return FileSpecList();
347 lldb::Format OptionValue::GetFormatValue(lldb::Format fail_value) const {
348 const OptionValueFormat *option_value = GetAsFormat();
350 return option_value->GetCurrentValue();
354 bool OptionValue::SetFormatValue(lldb::Format new_value) {
355 OptionValueFormat *option_value = GetAsFormat();
357 option_value->SetCurrentValue(new_value);
364 OptionValue::GetLanguageValue(lldb::LanguageType fail_value) const {
365 const OptionValueLanguage *option_value = GetAsLanguage();
367 return option_value->GetCurrentValue();
371 bool OptionValue::SetLanguageValue(lldb::LanguageType new_language) {
372 OptionValueLanguage *option_value = GetAsLanguage();
374 option_value->SetCurrentValue(new_language);
380 const FormatEntity::Entry *OptionValue::GetFormatEntity() const {
381 const OptionValueFormatEntity *option_value = GetAsFormatEntity();
383 return &option_value->GetCurrentValue();
387 const RegularExpression *OptionValue::GetRegexValue() const {
388 const OptionValueRegex *option_value = GetAsRegex();
390 return option_value->GetCurrentValue();
394 int64_t OptionValue::GetSInt64Value(int64_t fail_value) const {
395 const OptionValueSInt64 *option_value = GetAsSInt64();
397 return option_value->GetCurrentValue();
401 bool OptionValue::SetSInt64Value(int64_t new_value) {
402 OptionValueSInt64 *option_value = GetAsSInt64();
404 option_value->SetCurrentValue(new_value);
410 llvm::StringRef OptionValue::GetStringValue(llvm::StringRef fail_value) const {
411 const OptionValueString *option_value = GetAsString();
413 return option_value->GetCurrentValueAsRef();
417 bool OptionValue::SetStringValue(llvm::StringRef new_value) {
418 OptionValueString *option_value = GetAsString();
420 option_value->SetCurrentValue(new_value);
426 uint64_t OptionValue::GetUInt64Value(uint64_t fail_value) const {
427 const OptionValueUInt64 *option_value = GetAsUInt64();
429 return option_value->GetCurrentValue();
433 bool OptionValue::SetUInt64Value(uint64_t new_value) {
434 OptionValueUInt64 *option_value = GetAsUInt64();
436 option_value->SetCurrentValue(new_value);
442 UUID OptionValue::GetUUIDValue() const {
443 const OptionValueUUID *option_value = GetAsUUID();
445 return option_value->GetCurrentValue();
449 bool OptionValue::SetUUIDValue(const UUID &uuid) {
450 OptionValueUUID *option_value = GetAsUUID();
452 option_value->SetCurrentValue(uuid);
458 const char *OptionValue::GetBuiltinTypeAsCString(Type t) {
472 case eTypeDictionary:
478 case eTypeFileSpecList:
482 case eTypeFormatEntity:
483 return "format-string";
488 case eTypeProperties:
504 lldb::OptionValueSP OptionValue::CreateValueFromCStringForTypeMask(
505 const char *value_cstr, uint32_t type_mask, Status &error) {
506 // If only 1 bit is set in the type mask for a dictionary or array then we
507 // know how to decode a value from a cstring
508 lldb::OptionValueSP value_sp;
510 case 1u << eTypeArch:
511 value_sp.reset(new OptionValueArch());
513 case 1u << eTypeBoolean:
514 value_sp.reset(new OptionValueBoolean(false));
516 case 1u << eTypeChar:
517 value_sp.reset(new OptionValueChar('\0'));
519 case 1u << eTypeFileSpec:
520 value_sp.reset(new OptionValueFileSpec());
522 case 1u << eTypeFormat:
523 value_sp.reset(new OptionValueFormat(eFormatInvalid));
525 case 1u << eTypeFormatEntity:
526 value_sp.reset(new OptionValueFormatEntity(NULL));
528 case 1u << eTypeLanguage:
529 value_sp.reset(new OptionValueLanguage(eLanguageTypeUnknown));
531 case 1u << eTypeSInt64:
532 value_sp.reset(new OptionValueSInt64());
534 case 1u << eTypeString:
535 value_sp.reset(new OptionValueString());
537 case 1u << eTypeUInt64:
538 value_sp.reset(new OptionValueUInt64());
540 case 1u << eTypeUUID:
541 value_sp.reset(new OptionValueUUID());
546 error = value_sp->SetValueFromString(
547 llvm::StringRef::withNullAsEmpty(value_cstr), eVarSetOperationAssign);
549 error.SetErrorString("unsupported type mask");
553 bool OptionValue::DumpQualifiedName(Stream &strm) const {
554 bool dumped_something = false;
555 lldb::OptionValueSP m_parent_sp(m_parent_wp.lock());
557 if (m_parent_sp->DumpQualifiedName(strm))
558 dumped_something = true;
560 ConstString name(GetName());
562 if (dumped_something)
565 dumped_something = true;
568 return dumped_something;
571 size_t OptionValue::AutoComplete(CommandInterpreter &interpreter,
572 CompletionRequest &request) {
573 request.SetWordComplete(false);
574 return request.GetNumberOfMatches();
577 Status OptionValue::SetValueFromString(llvm::StringRef value,
578 VarSetOperationType op) {
581 case eVarSetOperationReplace:
582 error.SetErrorStringWithFormat(
583 "%s objects do not support the 'replace' operation",
586 case eVarSetOperationInsertBefore:
587 error.SetErrorStringWithFormat(
588 "%s objects do not support the 'insert-before' operation",
591 case eVarSetOperationInsertAfter:
592 error.SetErrorStringWithFormat(
593 "%s objects do not support the 'insert-after' operation",
596 case eVarSetOperationRemove:
597 error.SetErrorStringWithFormat(
598 "%s objects do not support the 'remove' operation", GetTypeAsCString());
600 case eVarSetOperationAppend:
601 error.SetErrorStringWithFormat(
602 "%s objects do not support the 'append' operation", GetTypeAsCString());
604 case eVarSetOperationClear:
605 error.SetErrorStringWithFormat(
606 "%s objects do not support the 'clear' operation", GetTypeAsCString());
608 case eVarSetOperationAssign:
609 error.SetErrorStringWithFormat(
610 "%s objects do not support the 'assign' operation", GetTypeAsCString());
612 case eVarSetOperationInvalid:
613 error.SetErrorStringWithFormat("invalid operation performed on a %s object",