1 //===-- OptionValueProperties.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 liblldb_OptionValueProperties_h_
10 #define liblldb_OptionValueProperties_h_
11 
12 #include <vector>
13 
14 #include "lldb/Core/FormatEntity.h"
15 #include "lldb/Core/UniqueCStringMap.h"
16 #include "lldb/Interpreter/OptionValue.h"
17 #include "lldb/Interpreter/Property.h"
18 #include "lldb/Utility/ConstString.h"
19 
20 namespace lldb_private {
21 
22 class OptionValueProperties
23     : public OptionValue,
24       public std::enable_shared_from_this<OptionValueProperties> {
25 public:
26   OptionValueProperties()
27       : OptionValue(), m_name(), m_properties(), m_name_to_index() {}
28 
29   OptionValueProperties(ConstString name);
30 
31   OptionValueProperties(const OptionValueProperties &global_properties);
32 
33   ~OptionValueProperties() override = default;
34 
35   Type GetType() const override { return eTypeProperties; }
36 
37   bool Clear() override;
38 
39   lldb::OptionValueSP DeepCopy() const override;
40 
41   Status
42   SetValueFromString(llvm::StringRef value,
43                      VarSetOperationType op = eVarSetOperationAssign) override;
44 
45   void DumpValue(const ExecutionContext *exe_ctx, Stream &strm,
46                  uint32_t dump_mask) override;
47 
48   ConstString GetName() const override { return m_name; }
49 
50   virtual Status DumpPropertyValue(const ExecutionContext *exe_ctx,
51                                    Stream &strm, llvm::StringRef property_path,
52                                    uint32_t dump_mask);
53 
54   virtual void DumpAllDescriptions(CommandInterpreter &interpreter,
55                                    Stream &strm) const;
56 
57   void Apropos(llvm::StringRef keyword,
58                std::vector<const Property *> &matching_properties) const;
59 
60   void Initialize(const PropertyDefinitions &setting_definitions);
61 
62   //    bool
63   //    GetQualifiedName (Stream &strm);
64 
65   // Subclass specific functions
66 
67   virtual size_t GetNumProperties() const;
68 
69   // Get the index of a property given its exact name in this property
70   // collection, "name" can't be a path to a property path that refers to a
71   // property within a property
72   virtual uint32_t GetPropertyIndex(ConstString name) const;
73 
74   // Get a property by exact name exists in this property collection, name can
75   // not be a path to a property path that refers to a property within a
76   // property
77   virtual const Property *GetProperty(const ExecutionContext *exe_ctx,
78                                       bool will_modify,
79                                       ConstString name) const;
80 
81   virtual const Property *GetPropertyAtIndex(const ExecutionContext *exe_ctx,
82                                              bool will_modify,
83                                              uint32_t idx) const;
84 
85   // Property can be be a property path like
86   // "target.process.extra-startup-command"
87   virtual const Property *GetPropertyAtPath(const ExecutionContext *exe_ctx,
88                                             bool will_modify,
89     llvm::StringRef property_path) const;
90 
91   virtual lldb::OptionValueSP
92   GetPropertyValueAtIndex(const ExecutionContext *exe_ctx, bool will_modify,
93                           uint32_t idx) const;
94 
95   virtual lldb::OptionValueSP GetValueForKey(const ExecutionContext *exe_ctx,
96                                              ConstString key,
97                                              bool value_will_be_modified) const;
98 
99   lldb::OptionValueSP GetSubValue(const ExecutionContext *exe_ctx,
100                                   llvm::StringRef name,
101                                   bool value_will_be_modified,
102                                   Status &error) const override;
103 
104   Status SetSubValue(const ExecutionContext *exe_ctx, VarSetOperationType op,
105                      llvm::StringRef path, llvm::StringRef value) override;
106 
107   virtual bool PredicateMatches(const ExecutionContext *exe_ctx,
108     llvm::StringRef predicate) const {
109     return false;
110   }
111 
112   OptionValueArch *
113   GetPropertyAtIndexAsOptionValueArch(const ExecutionContext *exe_ctx,
114                                       uint32_t idx) const;
115 
116   OptionValueLanguage *
117   GetPropertyAtIndexAsOptionValueLanguage(const ExecutionContext *exe_ctx,
118                                           uint32_t idx) const;
119 
120   bool GetPropertyAtIndexAsArgs(const ExecutionContext *exe_ctx, uint32_t idx,
121                                 Args &args) const;
122 
123   bool SetPropertyAtIndexFromArgs(const ExecutionContext *exe_ctx, uint32_t idx,
124                                   const Args &args);
125 
126   bool GetPropertyAtIndexAsBoolean(const ExecutionContext *exe_ctx,
127                                    uint32_t idx, bool fail_value) const;
128 
129   bool SetPropertyAtIndexAsBoolean(const ExecutionContext *exe_ctx,
130                                    uint32_t idx, bool new_value);
131 
132   OptionValueDictionary *
133   GetPropertyAtIndexAsOptionValueDictionary(const ExecutionContext *exe_ctx,
134                                             uint32_t idx) const;
135 
136   int64_t GetPropertyAtIndexAsEnumeration(const ExecutionContext *exe_ctx,
137                                           uint32_t idx,
138                                           int64_t fail_value) const;
139 
140   bool SetPropertyAtIndexAsEnumeration(const ExecutionContext *exe_ctx,
141                                        uint32_t idx, int64_t new_value);
142 
143   const FormatEntity::Entry *
144   GetPropertyAtIndexAsFormatEntity(const ExecutionContext *exe_ctx,
145                                    uint32_t idx);
146 
147   const RegularExpression *
148   GetPropertyAtIndexAsOptionValueRegex(const ExecutionContext *exe_ctx,
149                                        uint32_t idx) const;
150 
151   OptionValueSInt64 *
152   GetPropertyAtIndexAsOptionValueSInt64(const ExecutionContext *exe_ctx,
153                                         uint32_t idx) const;
154 
155   int64_t GetPropertyAtIndexAsSInt64(const ExecutionContext *exe_ctx,
156                                      uint32_t idx, int64_t fail_value) const;
157 
158   bool SetPropertyAtIndexAsSInt64(const ExecutionContext *exe_ctx, uint32_t idx,
159                                   int64_t new_value);
160 
161   uint64_t GetPropertyAtIndexAsUInt64(const ExecutionContext *exe_ctx,
162                                       uint32_t idx, uint64_t fail_value) const;
163 
164   bool SetPropertyAtIndexAsUInt64(const ExecutionContext *exe_ctx, uint32_t idx,
165                                   uint64_t new_value);
166 
167   llvm::StringRef GetPropertyAtIndexAsString(const ExecutionContext *exe_ctx,
168                                          uint32_t idx,
169                                          llvm::StringRef fail_value) const;
170 
171   bool SetPropertyAtIndexAsString(const ExecutionContext *exe_ctx, uint32_t idx,
172                                   llvm::StringRef new_value);
173 
174   OptionValueString *
175   GetPropertyAtIndexAsOptionValueString(const ExecutionContext *exe_ctx,
176                                         bool will_modify, uint32_t idx) const;
177 
178   OptionValueFileSpec *
179   GetPropertyAtIndexAsOptionValueFileSpec(const ExecutionContext *exe_ctx,
180                                           bool will_modify, uint32_t idx) const;
181 
182   FileSpec GetPropertyAtIndexAsFileSpec(const ExecutionContext *exe_ctx,
183                                         uint32_t idx) const;
184 
185   bool SetPropertyAtIndexAsFileSpec(const ExecutionContext *exe_ctx,
186                                     uint32_t idx, const FileSpec &file_spec);
187 
188   OptionValuePathMappings *GetPropertyAtIndexAsOptionValuePathMappings(
189       const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const;
190 
191   OptionValueFileSpecList *GetPropertyAtIndexAsOptionValueFileSpecList(
192       const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const;
193 
194   void AppendProperty(ConstString name, ConstString desc,
195                       bool is_global, const lldb::OptionValueSP &value_sp);
196 
197   lldb::OptionValuePropertiesSP GetSubProperty(const ExecutionContext *exe_ctx,
198                                                ConstString name);
199 
200   void SetValueChangedCallback(uint32_t property_idx,
201                                std::function<void()> callback);
202 
203 protected:
204   Property *ProtectedGetPropertyAtIndex(uint32_t idx) {
205     return ((idx < m_properties.size()) ? &m_properties[idx] : nullptr);
206   }
207 
208   const Property *ProtectedGetPropertyAtIndex(uint32_t idx) const {
209     return ((idx < m_properties.size()) ? &m_properties[idx] : nullptr);
210   }
211 
212   typedef UniqueCStringMap<size_t> NameToIndex;
213 
214   ConstString m_name;
215   std::vector<Property> m_properties;
216   NameToIndex m_name_to_index;
217 };
218 
219 } // namespace lldb_private
220 
221 #endif // liblldb_OptionValueProperties_h_
222