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