]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Interpreter/OptionValue.cpp
MFV r336946: 9238 ZFS Spacemap Encoding V2
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Interpreter / OptionValue.cpp
1 //===-- OptionValue.cpp -----------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "lldb/Interpreter/OptionValue.h"
11
12 // C Includes
13 // C++ Includes
14 // Other libraries and framework includes
15 // Project includes
16 #include "lldb/Interpreter/OptionValues.h"
17 #include "lldb/Utility/StringList.h"
18
19 using namespace lldb;
20 using namespace lldb_private;
21
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
25 // returned
26 //-------------------------------------------------------------------------
27 uint64_t OptionValue::GetUInt64Value(uint64_t fail_value, bool *success_ptr) {
28   if (success_ptr)
29     *success_ptr = true;
30   switch (GetType()) {
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();
37   default:
38     break;
39   }
40   if (success_ptr)
41     *success_ptr = false;
42   return fail_value;
43 }
44
45 Status OptionValue::SetSubValue(const ExecutionContext *exe_ctx,
46                                 VarSetOperationType op, llvm::StringRef name,
47                                 llvm::StringRef value) {
48   Status error;
49   error.SetErrorStringWithFormat("SetSubValue is not supported");
50   return error;
51 }
52
53 OptionValueBoolean *OptionValue::GetAsBoolean() {
54   if (GetType() == OptionValue::eTypeBoolean)
55     return static_cast<OptionValueBoolean *>(this);
56   return nullptr;
57 }
58
59 const OptionValueBoolean *OptionValue::GetAsBoolean() const {
60   if (GetType() == OptionValue::eTypeBoolean)
61     return static_cast<const OptionValueBoolean *>(this);
62   return nullptr;
63 }
64
65 const OptionValueChar *OptionValue::GetAsChar() const {
66   if (GetType() == OptionValue::eTypeChar)
67     return static_cast<const OptionValueChar *>(this);
68   return nullptr;
69 }
70
71 OptionValueChar *OptionValue::GetAsChar() {
72   if (GetType() == OptionValue::eTypeChar)
73     return static_cast<OptionValueChar *>(this);
74   return nullptr;
75 }
76
77 OptionValueFileSpec *OptionValue::GetAsFileSpec() {
78   if (GetType() == OptionValue::eTypeFileSpec)
79     return static_cast<OptionValueFileSpec *>(this);
80   return nullptr;
81 }
82
83 const OptionValueFileSpec *OptionValue::GetAsFileSpec() const {
84   if (GetType() == OptionValue::eTypeFileSpec)
85     return static_cast<const OptionValueFileSpec *>(this);
86   return nullptr;
87 }
88
89 OptionValueFileSpecList *OptionValue::GetAsFileSpecList() {
90   if (GetType() == OptionValue::eTypeFileSpecList)
91     return static_cast<OptionValueFileSpecList *>(this);
92   return nullptr;
93 }
94
95 const OptionValueFileSpecList *OptionValue::GetAsFileSpecList() const {
96   if (GetType() == OptionValue::eTypeFileSpecList)
97     return static_cast<const OptionValueFileSpecList *>(this);
98   return nullptr;
99 }
100
101 OptionValueArch *OptionValue::GetAsArch() {
102   if (GetType() == OptionValue::eTypeArch)
103     return static_cast<OptionValueArch *>(this);
104   return nullptr;
105 }
106
107 const OptionValueArch *OptionValue::GetAsArch() const {
108   if (GetType() == OptionValue::eTypeArch)
109     return static_cast<const OptionValueArch *>(this);
110   return nullptr;
111 }
112
113 OptionValueArray *OptionValue::GetAsArray() {
114   if (GetType() == OptionValue::eTypeArray)
115     return static_cast<OptionValueArray *>(this);
116   return nullptr;
117 }
118
119 const OptionValueArray *OptionValue::GetAsArray() const {
120   if (GetType() == OptionValue::eTypeArray)
121     return static_cast<const OptionValueArray *>(this);
122   return nullptr;
123 }
124
125 OptionValueArgs *OptionValue::GetAsArgs() {
126   if (GetType() == OptionValue::eTypeArgs)
127     return static_cast<OptionValueArgs *>(this);
128   return nullptr;
129 }
130
131 const OptionValueArgs *OptionValue::GetAsArgs() const {
132   if (GetType() == OptionValue::eTypeArgs)
133     return static_cast<const OptionValueArgs *>(this);
134   return nullptr;
135 }
136
137 OptionValueDictionary *OptionValue::GetAsDictionary() {
138   if (GetType() == OptionValue::eTypeDictionary)
139     return static_cast<OptionValueDictionary *>(this);
140   return nullptr;
141 }
142
143 const OptionValueDictionary *OptionValue::GetAsDictionary() const {
144   if (GetType() == OptionValue::eTypeDictionary)
145     return static_cast<const OptionValueDictionary *>(this);
146   return nullptr;
147 }
148
149 OptionValueEnumeration *OptionValue::GetAsEnumeration() {
150   if (GetType() == OptionValue::eTypeEnum)
151     return static_cast<OptionValueEnumeration *>(this);
152   return nullptr;
153 }
154
155 const OptionValueEnumeration *OptionValue::GetAsEnumeration() const {
156   if (GetType() == OptionValue::eTypeEnum)
157     return static_cast<const OptionValueEnumeration *>(this);
158   return nullptr;
159 }
160
161 OptionValueFormat *OptionValue::GetAsFormat() {
162   if (GetType() == OptionValue::eTypeFormat)
163     return static_cast<OptionValueFormat *>(this);
164   return nullptr;
165 }
166
167 const OptionValueFormat *OptionValue::GetAsFormat() const {
168   if (GetType() == OptionValue::eTypeFormat)
169     return static_cast<const OptionValueFormat *>(this);
170   return nullptr;
171 }
172
173 OptionValueLanguage *OptionValue::GetAsLanguage() {
174   if (GetType() == OptionValue::eTypeLanguage)
175     return static_cast<OptionValueLanguage *>(this);
176   return NULL;
177 }
178
179 const OptionValueLanguage *OptionValue::GetAsLanguage() const {
180   if (GetType() == OptionValue::eTypeLanguage)
181     return static_cast<const OptionValueLanguage *>(this);
182   return NULL;
183 }
184
185 OptionValueFormatEntity *OptionValue::GetAsFormatEntity() {
186   if (GetType() == OptionValue::eTypeFormatEntity)
187     return static_cast<OptionValueFormatEntity *>(this);
188   return nullptr;
189 }
190
191 const OptionValueFormatEntity *OptionValue::GetAsFormatEntity() const {
192   if (GetType() == OptionValue::eTypeFormatEntity)
193     return static_cast<const OptionValueFormatEntity *>(this);
194   return nullptr;
195 }
196
197 OptionValuePathMappings *OptionValue::GetAsPathMappings() {
198   if (GetType() == OptionValue::eTypePathMap)
199     return static_cast<OptionValuePathMappings *>(this);
200   return nullptr;
201 }
202
203 const OptionValuePathMappings *OptionValue::GetAsPathMappings() const {
204   if (GetType() == OptionValue::eTypePathMap)
205     return static_cast<const OptionValuePathMappings *>(this);
206   return nullptr;
207 }
208
209 OptionValueProperties *OptionValue::GetAsProperties() {
210   if (GetType() == OptionValue::eTypeProperties)
211     return static_cast<OptionValueProperties *>(this);
212   return nullptr;
213 }
214
215 const OptionValueProperties *OptionValue::GetAsProperties() const {
216   if (GetType() == OptionValue::eTypeProperties)
217     return static_cast<const OptionValueProperties *>(this);
218   return nullptr;
219 }
220
221 OptionValueRegex *OptionValue::GetAsRegex() {
222   if (GetType() == OptionValue::eTypeRegex)
223     return static_cast<OptionValueRegex *>(this);
224   return nullptr;
225 }
226
227 const OptionValueRegex *OptionValue::GetAsRegex() const {
228   if (GetType() == OptionValue::eTypeRegex)
229     return static_cast<const OptionValueRegex *>(this);
230   return nullptr;
231 }
232
233 OptionValueSInt64 *OptionValue::GetAsSInt64() {
234   if (GetType() == OptionValue::eTypeSInt64)
235     return static_cast<OptionValueSInt64 *>(this);
236   return nullptr;
237 }
238
239 const OptionValueSInt64 *OptionValue::GetAsSInt64() const {
240   if (GetType() == OptionValue::eTypeSInt64)
241     return static_cast<const OptionValueSInt64 *>(this);
242   return nullptr;
243 }
244
245 OptionValueString *OptionValue::GetAsString() {
246   if (GetType() == OptionValue::eTypeString)
247     return static_cast<OptionValueString *>(this);
248   return nullptr;
249 }
250
251 const OptionValueString *OptionValue::GetAsString() const {
252   if (GetType() == OptionValue::eTypeString)
253     return static_cast<const OptionValueString *>(this);
254   return nullptr;
255 }
256
257 OptionValueUInt64 *OptionValue::GetAsUInt64() {
258   if (GetType() == OptionValue::eTypeUInt64)
259     return static_cast<OptionValueUInt64 *>(this);
260   return nullptr;
261 }
262
263 const OptionValueUInt64 *OptionValue::GetAsUInt64() const {
264   if (GetType() == OptionValue::eTypeUInt64)
265     return static_cast<const OptionValueUInt64 *>(this);
266   return nullptr;
267 }
268
269 OptionValueUUID *OptionValue::GetAsUUID() {
270   if (GetType() == OptionValue::eTypeUUID)
271     return static_cast<OptionValueUUID *>(this);
272   return nullptr;
273 }
274
275 const OptionValueUUID *OptionValue::GetAsUUID() const {
276   if (GetType() == OptionValue::eTypeUUID)
277     return static_cast<const OptionValueUUID *>(this);
278   return nullptr;
279 }
280
281 bool OptionValue::GetBooleanValue(bool fail_value) const {
282   const OptionValueBoolean *option_value = GetAsBoolean();
283   if (option_value)
284     return option_value->GetCurrentValue();
285   return fail_value;
286 }
287
288 bool OptionValue::SetBooleanValue(bool new_value) {
289   OptionValueBoolean *option_value = GetAsBoolean();
290   if (option_value) {
291     option_value->SetCurrentValue(new_value);
292     return true;
293   }
294   return false;
295 }
296
297 char OptionValue::GetCharValue(char fail_value) const {
298   const OptionValueChar *option_value = GetAsChar();
299   if (option_value)
300     return option_value->GetCurrentValue();
301   return fail_value;
302 }
303
304 char OptionValue::SetCharValue(char new_value) {
305   OptionValueChar *option_value = GetAsChar();
306   if (option_value) {
307     option_value->SetCurrentValue(new_value);
308     return true;
309   }
310   return false;
311 }
312
313 int64_t OptionValue::GetEnumerationValue(int64_t fail_value) const {
314   const OptionValueEnumeration *option_value = GetAsEnumeration();
315   if (option_value)
316     return option_value->GetCurrentValue();
317   return fail_value;
318 }
319
320 bool OptionValue::SetEnumerationValue(int64_t value) {
321   OptionValueEnumeration *option_value = GetAsEnumeration();
322   if (option_value) {
323     option_value->SetCurrentValue(value);
324     return true;
325   }
326   return false;
327 }
328
329 FileSpec OptionValue::GetFileSpecValue() const {
330   const OptionValueFileSpec *option_value = GetAsFileSpec();
331   if (option_value)
332     return option_value->GetCurrentValue();
333   return FileSpec();
334 }
335
336 bool OptionValue::SetFileSpecValue(const FileSpec &file_spec) {
337   OptionValueFileSpec *option_value = GetAsFileSpec();
338   if (option_value) {
339     option_value->SetCurrentValue(file_spec, false);
340     return true;
341   }
342   return false;
343 }
344
345 FileSpecList OptionValue::GetFileSpecListValue() const {
346   const OptionValueFileSpecList *option_value = GetAsFileSpecList();
347   if (option_value)
348     return option_value->GetCurrentValue();
349   return FileSpecList();
350 }
351
352 lldb::Format OptionValue::GetFormatValue(lldb::Format fail_value) const {
353   const OptionValueFormat *option_value = GetAsFormat();
354   if (option_value)
355     return option_value->GetCurrentValue();
356   return fail_value;
357 }
358
359 bool OptionValue::SetFormatValue(lldb::Format new_value) {
360   OptionValueFormat *option_value = GetAsFormat();
361   if (option_value) {
362     option_value->SetCurrentValue(new_value);
363     return true;
364   }
365   return false;
366 }
367
368 lldb::LanguageType
369 OptionValue::GetLanguageValue(lldb::LanguageType fail_value) const {
370   const OptionValueLanguage *option_value = GetAsLanguage();
371   if (option_value)
372     return option_value->GetCurrentValue();
373   return fail_value;
374 }
375
376 bool OptionValue::SetLanguageValue(lldb::LanguageType new_language) {
377   OptionValueLanguage *option_value = GetAsLanguage();
378   if (option_value) {
379     option_value->SetCurrentValue(new_language);
380     return true;
381   }
382   return false;
383 }
384
385 const FormatEntity::Entry *OptionValue::GetFormatEntity() const {
386   const OptionValueFormatEntity *option_value = GetAsFormatEntity();
387   if (option_value)
388     return &option_value->GetCurrentValue();
389   return nullptr;
390 }
391
392 const RegularExpression *OptionValue::GetRegexValue() const {
393   const OptionValueRegex *option_value = GetAsRegex();
394   if (option_value)
395     return option_value->GetCurrentValue();
396   return nullptr;
397 }
398
399 int64_t OptionValue::GetSInt64Value(int64_t fail_value) const {
400   const OptionValueSInt64 *option_value = GetAsSInt64();
401   if (option_value)
402     return option_value->GetCurrentValue();
403   return fail_value;
404 }
405
406 bool OptionValue::SetSInt64Value(int64_t new_value) {
407   OptionValueSInt64 *option_value = GetAsSInt64();
408   if (option_value) {
409     option_value->SetCurrentValue(new_value);
410     return true;
411   }
412   return false;
413 }
414
415 llvm::StringRef OptionValue::GetStringValue(llvm::StringRef fail_value) const {
416   const OptionValueString *option_value = GetAsString();
417   if (option_value)
418     return option_value->GetCurrentValueAsRef();
419   return fail_value;
420 }
421
422 bool OptionValue::SetStringValue(llvm::StringRef new_value) {
423   OptionValueString *option_value = GetAsString();
424   if (option_value) {
425     option_value->SetCurrentValue(new_value);
426     return true;
427   }
428   return false;
429 }
430
431 uint64_t OptionValue::GetUInt64Value(uint64_t fail_value) const {
432   const OptionValueUInt64 *option_value = GetAsUInt64();
433   if (option_value)
434     return option_value->GetCurrentValue();
435   return fail_value;
436 }
437
438 bool OptionValue::SetUInt64Value(uint64_t new_value) {
439   OptionValueUInt64 *option_value = GetAsUInt64();
440   if (option_value) {
441     option_value->SetCurrentValue(new_value);
442     return true;
443   }
444   return false;
445 }
446
447 UUID OptionValue::GetUUIDValue() const {
448   const OptionValueUUID *option_value = GetAsUUID();
449   if (option_value)
450     return option_value->GetCurrentValue();
451   return UUID();
452 }
453
454 bool OptionValue::SetUUIDValue(const UUID &uuid) {
455   OptionValueUUID *option_value = GetAsUUID();
456   if (option_value) {
457     option_value->SetCurrentValue(uuid);
458     return true;
459   }
460   return false;
461 }
462
463 const char *OptionValue::GetBuiltinTypeAsCString(Type t) {
464   switch (t) {
465   case eTypeInvalid:
466     return "invalid";
467   case eTypeArch:
468     return "arch";
469   case eTypeArgs:
470     return "arguments";
471   case eTypeArray:
472     return "array";
473   case eTypeBoolean:
474     return "boolean";
475   case eTypeChar:
476     return "char";
477   case eTypeDictionary:
478     return "dictionary";
479   case eTypeEnum:
480     return "enum";
481   case eTypeFileSpec:
482     return "file";
483   case eTypeFileSpecList:
484     return "file-list";
485   case eTypeFormat:
486     return "format";
487   case eTypeFormatEntity:
488     return "format-string";
489   case eTypeLanguage:
490     return "language";
491   case eTypePathMap:
492     return "path-map";
493   case eTypeProperties:
494     return "properties";
495   case eTypeRegex:
496     return "regex";
497   case eTypeSInt64:
498     return "int";
499   case eTypeString:
500     return "string";
501   case eTypeUInt64:
502     return "unsigned";
503   case eTypeUUID:
504     return "uuid";
505   }
506   return nullptr;
507 }
508
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;
514   switch (type_mask) {
515   case 1u << eTypeArch:
516     value_sp.reset(new OptionValueArch());
517     break;
518   case 1u << eTypeBoolean:
519     value_sp.reset(new OptionValueBoolean(false));
520     break;
521   case 1u << eTypeChar:
522     value_sp.reset(new OptionValueChar('\0'));
523     break;
524   case 1u << eTypeFileSpec:
525     value_sp.reset(new OptionValueFileSpec());
526     break;
527   case 1u << eTypeFormat:
528     value_sp.reset(new OptionValueFormat(eFormatInvalid));
529     break;
530   case 1u << eTypeFormatEntity:
531     value_sp.reset(new OptionValueFormatEntity(NULL));
532     break;
533   case 1u << eTypeLanguage:
534     value_sp.reset(new OptionValueLanguage(eLanguageTypeUnknown));
535     break;
536   case 1u << eTypeSInt64:
537     value_sp.reset(new OptionValueSInt64());
538     break;
539   case 1u << eTypeString:
540     value_sp.reset(new OptionValueString());
541     break;
542   case 1u << eTypeUInt64:
543     value_sp.reset(new OptionValueUInt64());
544     break;
545   case 1u << eTypeUUID:
546     value_sp.reset(new OptionValueUUID());
547     break;
548   }
549
550   if (value_sp)
551     error = value_sp->SetValueFromString(
552         llvm::StringRef::withNullAsEmpty(value_cstr), eVarSetOperationAssign);
553   else
554     error.SetErrorString("unsupported type mask");
555   return value_sp;
556 }
557
558 bool OptionValue::DumpQualifiedName(Stream &strm) const {
559   bool dumped_something = false;
560   lldb::OptionValueSP m_parent_sp(m_parent_wp.lock());
561   if (m_parent_sp) {
562     if (m_parent_sp->DumpQualifiedName(strm))
563       dumped_something = true;
564   }
565   ConstString name(GetName());
566   if (name) {
567     if (dumped_something)
568       strm.PutChar('.');
569     else
570       dumped_something = true;
571     strm << name;
572   }
573   return dumped_something;
574 }
575
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;
581   matches.Clear();
582   return matches.GetSize();
583 }
584
585 Status OptionValue::SetValueFromString(llvm::StringRef value,
586                                        VarSetOperationType op) {
587   Status error;
588   switch (op) {
589   case eVarSetOperationReplace:
590     error.SetErrorStringWithFormat(
591         "%s objects do not support the 'replace' operation",
592         GetTypeAsCString());
593     break;
594   case eVarSetOperationInsertBefore:
595     error.SetErrorStringWithFormat(
596         "%s objects do not support the 'insert-before' operation",
597         GetTypeAsCString());
598     break;
599   case eVarSetOperationInsertAfter:
600     error.SetErrorStringWithFormat(
601         "%s objects do not support the 'insert-after' operation",
602         GetTypeAsCString());
603     break;
604   case eVarSetOperationRemove:
605     error.SetErrorStringWithFormat(
606         "%s objects do not support the 'remove' operation", GetTypeAsCString());
607     break;
608   case eVarSetOperationAppend:
609     error.SetErrorStringWithFormat(
610         "%s objects do not support the 'append' operation", GetTypeAsCString());
611     break;
612   case eVarSetOperationClear:
613     error.SetErrorStringWithFormat(
614         "%s objects do not support the 'clear' operation", GetTypeAsCString());
615     break;
616   case eVarSetOperationAssign:
617     error.SetErrorStringWithFormat(
618         "%s objects do not support the 'assign' operation", GetTypeAsCString());
619     break;
620   case eVarSetOperationInvalid:
621     error.SetErrorStringWithFormat("invalid operation performed on a %s object",
622                                    GetTypeAsCString());
623     break;
624   }
625   return error;
626 }