OptionValue.h 9.6 KB

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