1 //===-- TypeValidator.h ------------------------------------------*- C++ 2 //-*-===// 3 // 4 // The LLVM Compiler Infrastructure 5 // 6 // This file is distributed under the University of Illinois Open Source 7 // License. See LICENSE.TXT for details. 8 // 9 //===----------------------------------------------------------------------===// 10 11 #ifndef lldb_TypeValidator_h_ 12 #define lldb_TypeValidator_h_ 13 14 15 #include <functional> 16 #include <string> 17 18 19 #include "lldb/lldb-enumerations.h" 20 #include "lldb/lldb-private-enumerations.h" 21 #include "lldb/lldb-public.h" 22 23 namespace lldb_private { 24 25 class TypeValidatorImpl { 26 public: 27 class Flags { 28 public: Flags()29 Flags() : m_flags(lldb::eTypeOptionCascade) {} 30 Flags(const Flags & other)31 Flags(const Flags &other) : m_flags(other.m_flags) {} 32 Flags(uint32_t value)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 Clear()47 Flags &Clear() { 48 m_flags = 0; 49 return *this; 50 } 51 GetCascades()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 GetSkipPointers()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 GetSkipReferences()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 GetNonCacheable()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 GetValue()103 uint32_t GetValue() { return m_flags; } 104 SetValue(uint32_t value)105 void SetValue(uint32_t value) { m_flags = value; } 106 107 private: 108 uint32_t m_flags; 109 }; 110 111 TypeValidatorImpl(const Flags &flags = Flags()); 112 113 typedef std::shared_ptr<TypeValidatorImpl> SharedPointer; 114 115 virtual ~TypeValidatorImpl(); 116 Cascades()117 bool Cascades() const { return m_flags.GetCascades(); } SkipsPointers()118 bool SkipsPointers() const { return m_flags.GetSkipPointers(); } SkipsReferences()119 bool SkipsReferences() const { return m_flags.GetSkipReferences(); } NonCacheable()120 bool NonCacheable() const { return m_flags.GetNonCacheable(); } 121 SetCascades(bool value)122 void SetCascades(bool value) { m_flags.SetCascades(value); } 123 SetSkipsPointers(bool value)124 void SetSkipsPointers(bool value) { m_flags.SetSkipPointers(value); } 125 SetSkipsReferences(bool value)126 void SetSkipsReferences(bool value) { m_flags.SetSkipReferences(value); } 127 SetNonCacheable(bool value)128 void SetNonCacheable(bool value) { m_flags.SetNonCacheable(value); } 129 GetOptions()130 uint32_t GetOptions() { return m_flags.GetValue(); } 131 SetOptions(uint32_t value)132 void SetOptions(uint32_t value) { m_flags.SetValue(value); } 133 GetRevision()134 uint32_t &GetRevision() { return m_my_revision; } 135 136 enum class Type { eTypeUnknown, eTypeCXX }; 137 138 struct ValidationResult { 139 TypeValidatorResult m_result; 140 std::string m_message; 141 }; 142 GetType()143 virtual Type GetType() { return Type::eTypeUnknown; } 144 145 // we are using a ValueObject* instead of a ValueObjectSP because we do not 146 // need to hold on to this for extended periods of time and we trust the 147 // ValueObject to stay around for as long as it is required for us to 148 // generate its value 149 virtual ValidationResult FormatObject(ValueObject *valobj) const = 0; 150 151 virtual std::string GetDescription() = 0; 152 153 static ValidationResult Success(); 154 155 static ValidationResult Failure(std::string message); 156 157 protected: 158 Flags m_flags; 159 uint32_t m_my_revision; 160 161 private: 162 DISALLOW_COPY_AND_ASSIGN(TypeValidatorImpl); 163 }; 164 165 class TypeValidatorImpl_CXX : public TypeValidatorImpl { 166 public: 167 typedef std::function<TypeValidatorImpl::ValidationResult( 168 ValueObject *valobj)> 169 ValidatorFunction; 170 171 TypeValidatorImpl_CXX(ValidatorFunction f, std::string d, 172 const TypeValidatorImpl::Flags &flags = Flags()); 173 174 typedef std::shared_ptr<TypeValidatorImpl_CXX> SharedPointer; 175 176 ~TypeValidatorImpl_CXX() override; 177 GetValidatorFunction()178 ValidatorFunction GetValidatorFunction() const { 179 return m_validator_function; 180 } 181 SetValidatorFunction(ValidatorFunction f)182 void SetValidatorFunction(ValidatorFunction f) { m_validator_function = f; } 183 GetType()184 TypeValidatorImpl::Type GetType() override { 185 return TypeValidatorImpl::Type::eTypeCXX; 186 } 187 188 ValidationResult FormatObject(ValueObject *valobj) const override; 189 190 std::string GetDescription() override; 191 192 protected: 193 std::string m_description; 194 ValidatorFunction m_validator_function; 195 196 private: 197 DISALLOW_COPY_AND_ASSIGN(TypeValidatorImpl_CXX); 198 }; 199 200 } // namespace lldb_private 201 202 #endif // lldb_TypeValidator_h_ 203