1 //===-- OptionValue.h -------------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef liblldb_OptionValue_h_
10 #define liblldb_OptionValue_h_
12 #include "lldb/Core/FormatEntity.h"
13 #include "lldb/Utility/CompletionRequest.h"
14 #include "lldb/Utility/ConstString.h"
15 #include "lldb/Utility/Status.h"
16 #include "lldb/lldb-defines.h"
17 #include "lldb/lldb-private-enumerations.h"
18 #include "lldb/lldb-private-interfaces.h"
20 namespace lldb_private {
49 eDumpOptionName = (1u << 0),
50 eDumpOptionType = (1u << 1),
51 eDumpOptionValue = (1u << 2),
52 eDumpOptionDescription = (1u << 3),
53 eDumpOptionRaw = (1u << 4),
54 eDumpOptionCommand = (1u << 5),
55 eDumpGroupValue = (eDumpOptionName | eDumpOptionType | eDumpOptionValue),
57 (eDumpOptionName | eDumpOptionType | eDumpOptionDescription),
58 eDumpGroupExport = (eDumpOptionCommand | eDumpOptionName | eDumpOptionValue)
62 : m_callback(nullptr), m_baton(nullptr), m_value_was_set(false) {}
64 OptionValue(const OptionValue &rhs)
65 : m_callback(rhs.m_callback), m_baton(rhs.m_baton),
66 m_value_was_set(rhs.m_value_was_set) {}
68 virtual ~OptionValue() = default;
70 // Subclasses should override these functions
71 virtual Type GetType() const = 0;
73 // If this value is always hidden, the avoid showing any info on this value,
74 // just show the info for the child values.
75 virtual bool ValueIsTransparent() const {
76 return GetType() == eTypeProperties;
79 virtual const char *GetTypeAsCString() const {
80 return GetBuiltinTypeAsCString(GetType());
83 static const char *GetBuiltinTypeAsCString(Type t);
85 virtual void DumpValue(const ExecutionContext *exe_ctx, Stream &strm,
86 uint32_t dump_mask) = 0;
89 SetValueFromString(llvm::StringRef value,
90 VarSetOperationType op = eVarSetOperationAssign);
92 virtual bool Clear() = 0;
94 virtual lldb::OptionValueSP DeepCopy() const = 0;
96 virtual size_t AutoComplete(CommandInterpreter &interpreter,
97 CompletionRequest &request);
99 // Subclasses can override these functions
100 virtual lldb::OptionValueSP GetSubValue(const ExecutionContext *exe_ctx,
101 llvm::StringRef name,
103 Status &error) const {
104 error.SetErrorStringWithFormat("'%s' is not a value subvalue", name.str().c_str());
105 return lldb::OptionValueSP();
108 virtual Status SetSubValue(const ExecutionContext *exe_ctx,
109 VarSetOperationType op, llvm::StringRef name,
110 llvm::StringRef value);
112 virtual bool IsAggregateValue() const { return false; }
114 virtual ConstString GetName() const { return ConstString(); }
116 virtual bool DumpQualifiedName(Stream &strm) const;
118 // Subclasses should NOT override these functions as they use the above
119 // functions to implement functionality
120 uint32_t GetTypeAsMask() { return 1u << GetType(); }
122 static uint32_t ConvertTypeToMask(OptionValue::Type type) {
126 static OptionValue::Type ConvertTypeMaskToType(uint32_t type_mask) {
127 // If only one bit is set, then return an appropriate enumeration
129 case 1u << eTypeArch:
131 case 1u << eTypeArgs:
133 case 1u << eTypeArray:
135 case 1u << eTypeBoolean:
137 case 1u << eTypeChar:
139 case 1u << eTypeDictionary:
140 return eTypeDictionary;
141 case 1u << eTypeEnum:
143 case 1u << eTypeFileSpec:
144 return eTypeFileSpec;
145 case 1u << eTypeFileSpecList:
146 return eTypeFileSpecList;
147 case 1u << eTypeFormat:
149 case 1u << eTypeLanguage:
150 return eTypeLanguage;
151 case 1u << eTypePathMap:
153 case 1u << eTypeProperties:
154 return eTypeProperties;
155 case 1u << eTypeRegex:
157 case 1u << eTypeSInt64:
159 case 1u << eTypeString:
161 case 1u << eTypeUInt64:
163 case 1u << eTypeUUID:
166 // Else return invalid
170 static lldb::OptionValueSP
171 CreateValueFromCStringForTypeMask(const char *value_cstr, uint32_t type_mask,
174 // Get this value as a uint64_t value if it is encoded as a boolean, uint64_t
175 // or int64_t. Other types will cause "fail_value" to be returned
176 uint64_t GetUInt64Value(uint64_t fail_value, bool *success_ptr);
178 OptionValueArch *GetAsArch();
180 const OptionValueArch *GetAsArch() const;
182 OptionValueArray *GetAsArray();
184 const OptionValueArray *GetAsArray() const;
186 OptionValueArgs *GetAsArgs();
188 const OptionValueArgs *GetAsArgs() const;
190 OptionValueBoolean *GetAsBoolean();
192 OptionValueChar *GetAsChar();
194 const OptionValueBoolean *GetAsBoolean() const;
196 const OptionValueChar *GetAsChar() const;
198 OptionValueDictionary *GetAsDictionary();
200 const OptionValueDictionary *GetAsDictionary() const;
202 OptionValueEnumeration *GetAsEnumeration();
204 const OptionValueEnumeration *GetAsEnumeration() const;
206 OptionValueFileSpec *GetAsFileSpec();
208 const OptionValueFileSpec *GetAsFileSpec() const;
210 OptionValueFileSpecList *GetAsFileSpecList();
212 const OptionValueFileSpecList *GetAsFileSpecList() const;
214 OptionValueFormat *GetAsFormat();
216 const OptionValueFormat *GetAsFormat() const;
218 OptionValueLanguage *GetAsLanguage();
220 const OptionValueLanguage *GetAsLanguage() const;
222 OptionValuePathMappings *GetAsPathMappings();
224 const OptionValuePathMappings *GetAsPathMappings() const;
226 OptionValueProperties *GetAsProperties();
228 const OptionValueProperties *GetAsProperties() const;
230 OptionValueRegex *GetAsRegex();
232 const OptionValueRegex *GetAsRegex() const;
234 OptionValueSInt64 *GetAsSInt64();
236 const OptionValueSInt64 *GetAsSInt64() const;
238 OptionValueString *GetAsString();
240 const OptionValueString *GetAsString() const;
242 OptionValueUInt64 *GetAsUInt64();
244 const OptionValueUInt64 *GetAsUInt64() const;
246 OptionValueUUID *GetAsUUID();
248 const OptionValueUUID *GetAsUUID() const;
250 OptionValueFormatEntity *GetAsFormatEntity();
252 const OptionValueFormatEntity *GetAsFormatEntity() const;
254 bool GetBooleanValue(bool fail_value = false) const;
256 bool SetBooleanValue(bool new_value);
258 char GetCharValue(char fail_value) const;
260 char SetCharValue(char new_value);
262 int64_t GetEnumerationValue(int64_t fail_value = -1) const;
264 bool SetEnumerationValue(int64_t value);
266 FileSpec GetFileSpecValue() const;
268 bool SetFileSpecValue(const FileSpec &file_spec);
270 FileSpecList GetFileSpecListValue() const;
273 GetFormatValue(lldb::Format fail_value = lldb::eFormatDefault) const;
275 bool SetFormatValue(lldb::Format new_value);
277 lldb::LanguageType GetLanguageValue(
278 lldb::LanguageType fail_value = lldb::eLanguageTypeUnknown) const;
280 bool SetLanguageValue(lldb::LanguageType new_language);
282 const FormatEntity::Entry *GetFormatEntity() const;
284 const RegularExpression *GetRegexValue() const;
286 int64_t GetSInt64Value(int64_t fail_value = 0) const;
288 bool SetSInt64Value(int64_t new_value);
290 llvm::StringRef GetStringValue(llvm::StringRef fail_value) const;
291 llvm::StringRef GetStringValue() const { return GetStringValue(llvm::StringRef()); }
293 bool SetStringValue(llvm::StringRef new_value);
295 uint64_t GetUInt64Value(uint64_t fail_value = 0) const;
297 bool SetUInt64Value(uint64_t new_value);
299 UUID GetUUIDValue() const;
301 bool SetUUIDValue(const UUID &uuid);
303 bool OptionWasSet() const { return m_value_was_set; }
305 void SetOptionWasSet() { m_value_was_set = true; }
307 void SetParent(const lldb::OptionValueSP &parent_sp) {
308 m_parent_wp = parent_sp;
311 void SetValueChangedCallback(OptionValueChangedCallback callback,
313 assert(m_callback == nullptr);
314 m_callback = callback;
318 void NotifyValueChanged() {
320 m_callback(m_baton, this);
324 lldb::OptionValueWP m_parent_wp;
325 OptionValueChangedCallback m_callback;
327 bool m_value_was_set; // This can be used to see if a value has been set
328 // by a call to SetValueFromCString(). It is often
329 // handy to know if an option value was set from the
330 // command line or as a setting, versus if we just have
331 // the default value that was already populated in the
335 } // namespace lldb_private
337 #endif // liblldb_OptionValue_h_