1 //===-- OptionValue.h -------------------------------------------*- 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 #ifndef liblldb_OptionValue_h_
11 #define liblldb_OptionValue_h_
13 #include "lldb/Core/FormatEntity.h"
14 #include "lldb/Utility/CompletionRequest.h"
15 #include "lldb/Utility/ConstString.h"
16 #include "lldb/Utility/Status.h"
17 #include "lldb/lldb-defines.h"
18 #include "lldb/lldb-private-enumerations.h"
19 #include "lldb/lldb-private-interfaces.h"
21 namespace lldb_private {
23 //---------------------------------------------------------------------
25 //---------------------------------------------------------------------
52 eDumpOptionName = (1u << 0),
53 eDumpOptionType = (1u << 1),
54 eDumpOptionValue = (1u << 2),
55 eDumpOptionDescription = (1u << 3),
56 eDumpOptionRaw = (1u << 4),
57 eDumpOptionCommand = (1u << 5),
58 eDumpGroupValue = (eDumpOptionName | eDumpOptionType | eDumpOptionValue),
60 (eDumpOptionName | eDumpOptionType | eDumpOptionDescription),
61 eDumpGroupExport = (eDumpOptionCommand | eDumpOptionName | eDumpOptionValue)
65 : m_callback(nullptr), m_baton(nullptr), m_value_was_set(false) {}
67 OptionValue(const OptionValue &rhs)
68 : m_callback(rhs.m_callback), m_baton(rhs.m_baton),
69 m_value_was_set(rhs.m_value_was_set) {}
71 virtual ~OptionValue() = default;
73 //-----------------------------------------------------------------
74 // Subclasses should override these functions
75 //-----------------------------------------------------------------
76 virtual Type GetType() const = 0;
78 // If this value is always hidden, the avoid showing any info on this value,
79 // just show the info for the child values.
80 virtual bool ValueIsTransparent() const {
81 return GetType() == eTypeProperties;
84 virtual const char *GetTypeAsCString() const {
85 return GetBuiltinTypeAsCString(GetType());
88 static const char *GetBuiltinTypeAsCString(Type t);
90 virtual void DumpValue(const ExecutionContext *exe_ctx, Stream &strm,
91 uint32_t dump_mask) = 0;
94 SetValueFromString(llvm::StringRef value,
95 VarSetOperationType op = eVarSetOperationAssign);
97 virtual bool Clear() = 0;
99 virtual lldb::OptionValueSP DeepCopy() const = 0;
101 virtual size_t AutoComplete(CommandInterpreter &interpreter,
102 CompletionRequest &request);
104 //-----------------------------------------------------------------
105 // Subclasses can override these functions
106 //-----------------------------------------------------------------
107 virtual lldb::OptionValueSP GetSubValue(const ExecutionContext *exe_ctx,
108 llvm::StringRef name,
110 Status &error) const {
111 error.SetErrorStringWithFormat("'%s' is not a value subvalue", name.str().c_str());
112 return lldb::OptionValueSP();
115 virtual Status SetSubValue(const ExecutionContext *exe_ctx,
116 VarSetOperationType op, llvm::StringRef name,
117 llvm::StringRef value);
119 virtual bool IsAggregateValue() const { return false; }
121 virtual ConstString GetName() const { return ConstString(); }
123 virtual bool DumpQualifiedName(Stream &strm) const;
125 //-----------------------------------------------------------------
126 // Subclasses should NOT override these functions as they use the above
127 // functions to implement functionality
128 //-----------------------------------------------------------------
129 uint32_t GetTypeAsMask() { return 1u << GetType(); }
131 static uint32_t ConvertTypeToMask(OptionValue::Type type) {
135 static OptionValue::Type ConvertTypeMaskToType(uint32_t type_mask) {
136 // If only one bit is set, then return an appropriate enumeration
138 case 1u << eTypeArch:
140 case 1u << eTypeArgs:
142 case 1u << eTypeArray:
144 case 1u << eTypeBoolean:
146 case 1u << eTypeChar:
148 case 1u << eTypeDictionary:
149 return eTypeDictionary;
150 case 1u << eTypeEnum:
152 case 1u << eTypeFileSpec:
153 return eTypeFileSpec;
154 case 1u << eTypeFileSpecList:
155 return eTypeFileSpecList;
156 case 1u << eTypeFormat:
158 case 1u << eTypeLanguage:
159 return eTypeLanguage;
160 case 1u << eTypePathMap:
162 case 1u << eTypeProperties:
163 return eTypeProperties;
164 case 1u << eTypeRegex:
166 case 1u << eTypeSInt64:
168 case 1u << eTypeString:
170 case 1u << eTypeUInt64:
172 case 1u << eTypeUUID:
175 // Else return invalid
179 static lldb::OptionValueSP
180 CreateValueFromCStringForTypeMask(const char *value_cstr, uint32_t type_mask,
183 // Get this value as a uint64_t value if it is encoded as a boolean, uint64_t
184 // or int64_t. Other types will cause "fail_value" to be returned
185 uint64_t GetUInt64Value(uint64_t fail_value, bool *success_ptr);
187 OptionValueArch *GetAsArch();
189 const OptionValueArch *GetAsArch() const;
191 OptionValueArray *GetAsArray();
193 const OptionValueArray *GetAsArray() const;
195 OptionValueArgs *GetAsArgs();
197 const OptionValueArgs *GetAsArgs() const;
199 OptionValueBoolean *GetAsBoolean();
201 OptionValueChar *GetAsChar();
203 const OptionValueBoolean *GetAsBoolean() const;
205 const OptionValueChar *GetAsChar() const;
207 OptionValueDictionary *GetAsDictionary();
209 const OptionValueDictionary *GetAsDictionary() const;
211 OptionValueEnumeration *GetAsEnumeration();
213 const OptionValueEnumeration *GetAsEnumeration() const;
215 OptionValueFileSpec *GetAsFileSpec();
217 const OptionValueFileSpec *GetAsFileSpec() const;
219 OptionValueFileSpecList *GetAsFileSpecList();
221 const OptionValueFileSpecList *GetAsFileSpecList() const;
223 OptionValueFormat *GetAsFormat();
225 const OptionValueFormat *GetAsFormat() const;
227 OptionValueLanguage *GetAsLanguage();
229 const OptionValueLanguage *GetAsLanguage() const;
231 OptionValuePathMappings *GetAsPathMappings();
233 const OptionValuePathMappings *GetAsPathMappings() const;
235 OptionValueProperties *GetAsProperties();
237 const OptionValueProperties *GetAsProperties() const;
239 OptionValueRegex *GetAsRegex();
241 const OptionValueRegex *GetAsRegex() const;
243 OptionValueSInt64 *GetAsSInt64();
245 const OptionValueSInt64 *GetAsSInt64() const;
247 OptionValueString *GetAsString();
249 const OptionValueString *GetAsString() const;
251 OptionValueUInt64 *GetAsUInt64();
253 const OptionValueUInt64 *GetAsUInt64() const;
255 OptionValueUUID *GetAsUUID();
257 const OptionValueUUID *GetAsUUID() const;
259 OptionValueFormatEntity *GetAsFormatEntity();
261 const OptionValueFormatEntity *GetAsFormatEntity() const;
263 bool GetBooleanValue(bool fail_value = false) const;
265 bool SetBooleanValue(bool new_value);
267 char GetCharValue(char fail_value) const;
269 char SetCharValue(char new_value);
271 int64_t GetEnumerationValue(int64_t fail_value = -1) const;
273 bool SetEnumerationValue(int64_t value);
275 FileSpec GetFileSpecValue() const;
277 bool SetFileSpecValue(const FileSpec &file_spec);
279 FileSpecList GetFileSpecListValue() const;
282 GetFormatValue(lldb::Format fail_value = lldb::eFormatDefault) const;
284 bool SetFormatValue(lldb::Format new_value);
286 lldb::LanguageType GetLanguageValue(
287 lldb::LanguageType fail_value = lldb::eLanguageTypeUnknown) const;
289 bool SetLanguageValue(lldb::LanguageType new_language);
291 const FormatEntity::Entry *GetFormatEntity() const;
293 const RegularExpression *GetRegexValue() const;
295 int64_t GetSInt64Value(int64_t fail_value = 0) const;
297 bool SetSInt64Value(int64_t new_value);
299 llvm::StringRef GetStringValue(llvm::StringRef fail_value) const;
300 llvm::StringRef GetStringValue() const { return GetStringValue(llvm::StringRef()); }
302 bool SetStringValue(llvm::StringRef new_value);
304 uint64_t GetUInt64Value(uint64_t fail_value = 0) const;
306 bool SetUInt64Value(uint64_t new_value);
308 UUID GetUUIDValue() const;
310 bool SetUUIDValue(const UUID &uuid);
312 bool OptionWasSet() const { return m_value_was_set; }
314 void SetOptionWasSet() { m_value_was_set = true; }
316 void SetParent(const lldb::OptionValueSP &parent_sp) {
317 m_parent_wp = parent_sp;
320 void SetValueChangedCallback(OptionValueChangedCallback callback,
322 assert(m_callback == nullptr);
323 m_callback = callback;
327 void NotifyValueChanged() {
329 m_callback(m_baton, this);
333 lldb::OptionValueWP m_parent_wp;
334 OptionValueChangedCallback m_callback;
336 bool m_value_was_set; // This can be used to see if a value has been set
337 // by a call to SetValueFromCString(). It is often
338 // handy to know if an option value was set from the
339 // command line or as a setting, versus if we just have
340 // the default value that was already populated in the
344 } // namespace lldb_private
346 #endif // liblldb_OptionValue_h_