1 //===-- TypeFormat.h ----------------------------------------------*- C++
2 //-*-===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef lldb_TypeFormat_h_
11 #define lldb_TypeFormat_h_
12 
13 
14 #include <functional>
15 #include <string>
16 #include <unordered_map>
17 
18 
19 #include "lldb/lldb-enumerations.h"
20 #include "lldb/lldb-public.h"
21 
22 #include "lldb/Core/ValueObject.h"
23 
24 namespace lldb_private {
25 class TypeFormatImpl {
26 public:
27   class Flags {
28   public:
29     Flags() : m_flags(lldb::eTypeOptionCascade) {}
30 
31     Flags(const Flags &other) : m_flags(other.m_flags) {}
32 
33     Flags(uint32_t value) : m_flags(value) {}
34 
35     Flags &operator=(const Flags &rhs) {
36       if (&rhs != this)
37         m_flags = rhs.m_flags;
38 
39       return *this;
40     }
41 
42     Flags &operator=(const uint32_t &rhs) {
43       m_flags = rhs;
44       return *this;
45     }
46 
47     Flags &Clear() {
48       m_flags = 0;
49       return *this;
50     }
51 
52     bool GetCascades() const {
53       return (m_flags & lldb::eTypeOptionCascade) == lldb::eTypeOptionCascade;
54     }
55 
56     Flags &SetCascades(bool value = true) {
57       if (value)
58         m_flags |= lldb::eTypeOptionCascade;
59       else
60         m_flags &= ~lldb::eTypeOptionCascade;
61       return *this;
62     }
63 
64     bool GetSkipPointers() const {
65       return (m_flags & lldb::eTypeOptionSkipPointers) ==
66              lldb::eTypeOptionSkipPointers;
67     }
68 
69     Flags &SetSkipPointers(bool value = true) {
70       if (value)
71         m_flags |= lldb::eTypeOptionSkipPointers;
72       else
73         m_flags &= ~lldb::eTypeOptionSkipPointers;
74       return *this;
75     }
76 
77     bool GetSkipReferences() const {
78       return (m_flags & lldb::eTypeOptionSkipReferences) ==
79              lldb::eTypeOptionSkipReferences;
80     }
81 
82     Flags &SetSkipReferences(bool value = true) {
83       if (value)
84         m_flags |= lldb::eTypeOptionSkipReferences;
85       else
86         m_flags &= ~lldb::eTypeOptionSkipReferences;
87       return *this;
88     }
89 
90     bool GetNonCacheable() const {
91       return (m_flags & lldb::eTypeOptionNonCacheable) ==
92              lldb::eTypeOptionNonCacheable;
93     }
94 
95     Flags &SetNonCacheable(bool value = true) {
96       if (value)
97         m_flags |= lldb::eTypeOptionNonCacheable;
98       else
99         m_flags &= ~lldb::eTypeOptionNonCacheable;
100       return *this;
101     }
102 
103     uint32_t GetValue() { return m_flags; }
104 
105     void SetValue(uint32_t value) { m_flags = value; }
106 
107   private:
108     uint32_t m_flags;
109   };
110 
111   TypeFormatImpl(const Flags &flags = Flags());
112 
113   typedef std::shared_ptr<TypeFormatImpl> SharedPointer;
114 
115   virtual ~TypeFormatImpl();
116 
117   bool Cascades() const { return m_flags.GetCascades(); }
118 
119   bool SkipsPointers() const { return m_flags.GetSkipPointers(); }
120 
121   bool SkipsReferences() const { return m_flags.GetSkipReferences(); }
122 
123   bool NonCacheable() const { return m_flags.GetNonCacheable(); }
124 
125   void SetCascades(bool value) { m_flags.SetCascades(value); }
126 
127   void SetSkipsPointers(bool value) { m_flags.SetSkipPointers(value); }
128 
129   void SetSkipsReferences(bool value) { m_flags.SetSkipReferences(value); }
130 
131   void SetNonCacheable(bool value) { m_flags.SetNonCacheable(value); }
132 
133   uint32_t GetOptions() { return m_flags.GetValue(); }
134 
135   void SetOptions(uint32_t value) { m_flags.SetValue(value); }
136 
137   uint32_t &GetRevision() { return m_my_revision; }
138 
139   enum class Type { eTypeUnknown, eTypeFormat, eTypeEnum };
140 
141   virtual Type GetType() { return Type::eTypeUnknown; }
142 
143   // we are using a ValueObject* instead of a ValueObjectSP because we do not
144   // need to hold on to this for extended periods of time and we trust the
145   // ValueObject to stay around for as long as it is required for us to
146   // generate its value
147   virtual bool FormatObject(ValueObject *valobj, std::string &dest) const = 0;
148 
149   virtual std::string GetDescription() = 0;
150 
151 protected:
152   Flags m_flags;
153   uint32_t m_my_revision;
154 
155 private:
156   DISALLOW_COPY_AND_ASSIGN(TypeFormatImpl);
157 };
158 
159 class TypeFormatImpl_Format : public TypeFormatImpl {
160 public:
161   TypeFormatImpl_Format(lldb::Format f = lldb::eFormatInvalid,
162                         const TypeFormatImpl::Flags &flags = Flags());
163 
164   typedef std::shared_ptr<TypeFormatImpl_Format> SharedPointer;
165 
166   ~TypeFormatImpl_Format() override;
167 
168   lldb::Format GetFormat() const { return m_format; }
169 
170   void SetFormat(lldb::Format fmt) { m_format = fmt; }
171 
172   TypeFormatImpl::Type GetType() override {
173     return TypeFormatImpl::Type::eTypeFormat;
174   }
175 
176   bool FormatObject(ValueObject *valobj, std::string &dest) const override;
177 
178   std::string GetDescription() override;
179 
180 protected:
181   lldb::Format m_format;
182 
183 private:
184   DISALLOW_COPY_AND_ASSIGN(TypeFormatImpl_Format);
185 };
186 
187 class TypeFormatImpl_EnumType : public TypeFormatImpl {
188 public:
189   TypeFormatImpl_EnumType(ConstString type_name = ConstString(""),
190                           const TypeFormatImpl::Flags &flags = Flags());
191 
192   typedef std::shared_ptr<TypeFormatImpl_EnumType> SharedPointer;
193 
194   ~TypeFormatImpl_EnumType() override;
195 
196   ConstString GetTypeName() { return m_enum_type; }
197 
198   void SetTypeName(ConstString enum_type) { m_enum_type = enum_type; }
199 
200   TypeFormatImpl::Type GetType() override {
201     return TypeFormatImpl::Type::eTypeEnum;
202   }
203 
204   bool FormatObject(ValueObject *valobj, std::string &dest) const override;
205 
206   std::string GetDescription() override;
207 
208 protected:
209   ConstString m_enum_type;
210   mutable std::unordered_map<void *, CompilerType> m_types;
211 
212 private:
213   DISALLOW_COPY_AND_ASSIGN(TypeFormatImpl_EnumType);
214 };
215 } // namespace lldb_private
216 
217 #endif // lldb_TypeFormat_h_
218