1 //===-- OptionValue.h -------------------------------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLDB_INTERPRETER_OPTIONVALUE_H
10 #define LLDB_INTERPRETER_OPTIONVALUE_H
11 
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"
19 
20 namespace lldb_private {
21 
22 // OptionValue
23 class OptionValue {
24 public:
25   enum Type {
26     eTypeInvalid = 0,
27     eTypeArch,
28     eTypeArgs,
29     eTypeArray,
30     eTypeBoolean,
31     eTypeChar,
32     eTypeDictionary,
33     eTypeEnum,
34     eTypeFileLineColumn,
35     eTypeFileSpec,
36     eTypeFileSpecList,
37     eTypeFormat,
38     eTypeLanguage,
39     eTypePathMap,
40     eTypeProperties,
41     eTypeRegex,
42     eTypeSInt64,
43     eTypeString,
44     eTypeUInt64,
45     eTypeUUID,
46     eTypeFormatEntity
47   };
48 
49   enum {
50     eDumpOptionName = (1u << 0),
51     eDumpOptionType = (1u << 1),
52     eDumpOptionValue = (1u << 2),
53     eDumpOptionDescription = (1u << 3),
54     eDumpOptionRaw = (1u << 4),
55     eDumpOptionCommand = (1u << 5),
56     eDumpGroupValue = (eDumpOptionName | eDumpOptionType | eDumpOptionValue),
57     eDumpGroupHelp =
58         (eDumpOptionName | eDumpOptionType | eDumpOptionDescription),
59     eDumpGroupExport = (eDumpOptionCommand | eDumpOptionName | eDumpOptionValue)
60   };
61 
62   OptionValue() : m_value_was_set(false) {}
63 
64   virtual ~OptionValue() = default;
65 
66   // Subclasses should override these functions
67   virtual Type GetType() const = 0;
68 
69   // If this value is always hidden, the avoid showing any info on this value,
70   // just show the info for the child values.
71   virtual bool ValueIsTransparent() const {
72     return GetType() == eTypeProperties;
73   }
74 
75   virtual const char *GetTypeAsCString() const {
76     return GetBuiltinTypeAsCString(GetType());
77   }
78 
79   static const char *GetBuiltinTypeAsCString(Type t);
80 
81   virtual void DumpValue(const ExecutionContext *exe_ctx, Stream &strm,
82                          uint32_t dump_mask) = 0;
83 
84   virtual Status
85   SetValueFromString(llvm::StringRef value,
86                      VarSetOperationType op = eVarSetOperationAssign);
87 
88   virtual void Clear() = 0;
89 
90   virtual lldb::OptionValueSP DeepCopy() const = 0;
91 
92   virtual void AutoComplete(CommandInterpreter &interpreter,
93                             CompletionRequest &request);
94 
95   // Subclasses can override these functions
96   virtual lldb::OptionValueSP GetSubValue(const ExecutionContext *exe_ctx,
97                                           llvm::StringRef name,
98                                           bool will_modify,
99                                           Status &error) const {
100     error.SetErrorStringWithFormat("'%s' is not a value subvalue", name.str().c_str());
101     return lldb::OptionValueSP();
102   }
103 
104   virtual Status SetSubValue(const ExecutionContext *exe_ctx,
105                              VarSetOperationType op, llvm::StringRef name,
106                              llvm::StringRef value);
107 
108   virtual bool IsAggregateValue() const { return false; }
109 
110   virtual ConstString GetName() const { return ConstString(); }
111 
112   virtual bool DumpQualifiedName(Stream &strm) const;
113 
114   // Subclasses should NOT override these functions as they use the above
115   // functions to implement functionality
116   uint32_t GetTypeAsMask() { return 1u << GetType(); }
117 
118   static uint32_t ConvertTypeToMask(OptionValue::Type type) {
119     return 1u << type;
120   }
121 
122   static OptionValue::Type ConvertTypeMaskToType(uint32_t type_mask) {
123     // If only one bit is set, then return an appropriate enumeration
124     switch (type_mask) {
125     case 1u << eTypeArch:
126       return eTypeArch;
127     case 1u << eTypeArgs:
128       return eTypeArgs;
129     case 1u << eTypeArray:
130       return eTypeArray;
131     case 1u << eTypeBoolean:
132       return eTypeBoolean;
133     case 1u << eTypeChar:
134       return eTypeChar;
135     case 1u << eTypeDictionary:
136       return eTypeDictionary;
137     case 1u << eTypeEnum:
138       return eTypeEnum;
139     case 1u << eTypeFileLineColumn:
140       return eTypeFileLineColumn;
141     case 1u << eTypeFileSpec:
142       return eTypeFileSpec;
143     case 1u << eTypeFileSpecList:
144       return eTypeFileSpecList;
145     case 1u << eTypeFormat:
146       return eTypeFormat;
147     case 1u << eTypeLanguage:
148       return eTypeLanguage;
149     case 1u << eTypePathMap:
150       return eTypePathMap;
151     case 1u << eTypeProperties:
152       return eTypeProperties;
153     case 1u << eTypeRegex:
154       return eTypeRegex;
155     case 1u << eTypeSInt64:
156       return eTypeSInt64;
157     case 1u << eTypeString:
158       return eTypeString;
159     case 1u << eTypeUInt64:
160       return eTypeUInt64;
161     case 1u << eTypeUUID:
162       return eTypeUUID;
163     }
164     // Else return invalid
165     return eTypeInvalid;
166   }
167 
168   static lldb::OptionValueSP
169   CreateValueFromCStringForTypeMask(const char *value_cstr, uint32_t type_mask,
170                                     Status &error);
171 
172   // Get this value as a uint64_t value if it is encoded as a boolean, uint64_t
173   // or int64_t. Other types will cause "fail_value" to be returned
174   uint64_t GetUInt64Value(uint64_t fail_value, bool *success_ptr);
175 
176   OptionValueArch *GetAsArch();
177 
178   const OptionValueArch *GetAsArch() const;
179 
180   OptionValueArray *GetAsArray();
181 
182   const OptionValueArray *GetAsArray() const;
183 
184   OptionValueArgs *GetAsArgs();
185 
186   const OptionValueArgs *GetAsArgs() const;
187 
188   OptionValueBoolean *GetAsBoolean();
189 
190   OptionValueChar *GetAsChar();
191 
192   const OptionValueBoolean *GetAsBoolean() const;
193 
194   const OptionValueChar *GetAsChar() const;
195 
196   OptionValueDictionary *GetAsDictionary();
197 
198   const OptionValueDictionary *GetAsDictionary() const;
199 
200   OptionValueEnumeration *GetAsEnumeration();
201 
202   const OptionValueEnumeration *GetAsEnumeration() const;
203 
204   OptionValueFileSpec *GetAsFileSpec();
205 
206   const OptionValueFileSpec *GetAsFileSpec() const;
207 
208   OptionValueFileSpecList *GetAsFileSpecList();
209 
210   const OptionValueFileSpecList *GetAsFileSpecList() const;
211 
212   OptionValueFormat *GetAsFormat();
213 
214   const OptionValueFormat *GetAsFormat() const;
215 
216   OptionValueLanguage *GetAsLanguage();
217 
218   const OptionValueLanguage *GetAsLanguage() const;
219 
220   OptionValuePathMappings *GetAsPathMappings();
221 
222   const OptionValuePathMappings *GetAsPathMappings() const;
223 
224   OptionValueProperties *GetAsProperties();
225 
226   const OptionValueProperties *GetAsProperties() const;
227 
228   OptionValueRegex *GetAsRegex();
229 
230   const OptionValueRegex *GetAsRegex() const;
231 
232   OptionValueSInt64 *GetAsSInt64();
233 
234   const OptionValueSInt64 *GetAsSInt64() const;
235 
236   OptionValueString *GetAsString();
237 
238   const OptionValueString *GetAsString() const;
239 
240   OptionValueUInt64 *GetAsUInt64();
241 
242   const OptionValueUInt64 *GetAsUInt64() const;
243 
244   OptionValueUUID *GetAsUUID();
245 
246   const OptionValueUUID *GetAsUUID() const;
247 
248   OptionValueFormatEntity *GetAsFormatEntity();
249 
250   const OptionValueFormatEntity *GetAsFormatEntity() const;
251 
252   bool GetBooleanValue(bool fail_value = false) const;
253 
254   bool SetBooleanValue(bool new_value);
255 
256   char GetCharValue(char fail_value) const;
257 
258   char SetCharValue(char new_value);
259 
260   int64_t GetEnumerationValue(int64_t fail_value = -1) const;
261 
262   bool SetEnumerationValue(int64_t value);
263 
264   FileSpec GetFileSpecValue() const;
265 
266   bool SetFileSpecValue(const FileSpec &file_spec);
267 
268   FileSpecList GetFileSpecListValue() const;
269 
270   lldb::Format
271   GetFormatValue(lldb::Format fail_value = lldb::eFormatDefault) const;
272 
273   bool SetFormatValue(lldb::Format new_value);
274 
275   lldb::LanguageType GetLanguageValue(
276       lldb::LanguageType fail_value = lldb::eLanguageTypeUnknown) const;
277 
278   bool SetLanguageValue(lldb::LanguageType new_language);
279 
280   const FormatEntity::Entry *GetFormatEntity() const;
281 
282   const RegularExpression *GetRegexValue() const;
283 
284   int64_t GetSInt64Value(int64_t fail_value = 0) const;
285 
286   bool SetSInt64Value(int64_t new_value);
287 
288   llvm::StringRef GetStringValue(llvm::StringRef fail_value) const;
289   llvm::StringRef GetStringValue() const { return GetStringValue(llvm::StringRef()); }
290 
291   bool SetStringValue(llvm::StringRef new_value);
292 
293   uint64_t GetUInt64Value(uint64_t fail_value = 0) const;
294 
295   bool SetUInt64Value(uint64_t new_value);
296 
297   UUID GetUUIDValue() const;
298 
299   bool SetUUIDValue(const UUID &uuid);
300 
301   bool OptionWasSet() const { return m_value_was_set; }
302 
303   void SetOptionWasSet() { m_value_was_set = true; }
304 
305   void SetParent(const lldb::OptionValueSP &parent_sp) {
306     m_parent_wp = parent_sp;
307   }
308 
309   void SetValueChangedCallback(std::function<void()> callback) {
310     assert(!m_callback);
311     m_callback = std::move(callback);
312   }
313 
314   void NotifyValueChanged() {
315     if (m_callback)
316       m_callback();
317   }
318 
319 protected:
320   lldb::OptionValueWP m_parent_wp;
321   std::function<void()> m_callback;
322   bool m_value_was_set; // This can be used to see if a value has been set
323                         // by a call to SetValueFromCString(). It is often
324                         // handy to know if an option value was set from the
325                         // command line or as a setting, versus if we just have
326                         // the default value that was already populated in the
327                         // option value.
328 };
329 
330 } // namespace lldb_private
331 
332 #endif // LLDB_INTERPRETER_OPTIONVALUE_H
333