Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 1 | //===--- VariantValue.cpp - Polymorphic value type -*- C++ -*-===/ |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// |
| 10 | /// \file |
| 11 | /// \brief Polymorphic value type. |
| 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/ASTMatchers/Dynamic/VariantValue.h" |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 16 | #include "clang/Basic/LLVM.h" |
Samuel Benzaquen | 9d02807 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/STLExtras.h" |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 18 | |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 19 | namespace clang { |
| 20 | namespace ast_matchers { |
| 21 | namespace dynamic { |
| 22 | |
Samuel Benzaquen | 544ae5b | 2013-08-22 16:38:33 +0000 | [diff] [blame] | 23 | VariantMatcher::MatcherOps::~MatcherOps() {} |
| 24 | VariantMatcher::Payload::~Payload() {} |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame] | 25 | |
Samuel Benzaquen | 544ae5b | 2013-08-22 16:38:33 +0000 | [diff] [blame] | 26 | class VariantMatcher::SinglePayload : public VariantMatcher::Payload { |
| 27 | public: |
Samuel Benzaquen | b7488d7 | 2013-10-29 14:37:15 +0000 | [diff] [blame] | 28 | SinglePayload(const DynTypedMatcher &Matcher) : Matcher(Matcher) {} |
Samuel Benzaquen | 544ae5b | 2013-08-22 16:38:33 +0000 | [diff] [blame] | 29 | |
Samuel Benzaquen | b7488d7 | 2013-10-29 14:37:15 +0000 | [diff] [blame] | 30 | virtual llvm::Optional<DynTypedMatcher> getSingleMatcher() const { |
| 31 | return Matcher; |
Samuel Benzaquen | 544ae5b | 2013-08-22 16:38:33 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | virtual std::string getTypeAsString() const { |
Samuel Benzaquen | b7488d7 | 2013-10-29 14:37:15 +0000 | [diff] [blame] | 35 | return (Twine("Matcher<") + Matcher.getSupportedKind().asStringRef() + ">") |
Samuel Benzaquen | 544ae5b | 2013-08-22 16:38:33 +0000 | [diff] [blame] | 36 | .str(); |
| 37 | } |
| 38 | |
Samuel Benzaquen | a735090 | 2013-08-28 18:42:04 +0000 | [diff] [blame] | 39 | virtual void makeTypedMatcher(MatcherOps &Ops) const { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 40 | bool Ignore; |
| 41 | if (Ops.canConstructFrom(Matcher, Ignore)) |
Samuel Benzaquen | b7488d7 | 2013-10-29 14:37:15 +0000 | [diff] [blame] | 42 | Ops.constructFrom(Matcher); |
Samuel Benzaquen | 544ae5b | 2013-08-22 16:38:33 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | private: |
Samuel Benzaquen | b7488d7 | 2013-10-29 14:37:15 +0000 | [diff] [blame] | 46 | const DynTypedMatcher Matcher; |
Samuel Benzaquen | 544ae5b | 2013-08-22 16:38:33 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | class VariantMatcher::PolymorphicPayload : public VariantMatcher::Payload { |
| 50 | public: |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 51 | PolymorphicPayload(std::vector<DynTypedMatcher> MatchersIn) |
| 52 | : Matchers(std::move(MatchersIn)) {} |
Samuel Benzaquen | 544ae5b | 2013-08-22 16:38:33 +0000 | [diff] [blame] | 53 | |
Samuel Benzaquen | b7488d7 | 2013-10-29 14:37:15 +0000 | [diff] [blame] | 54 | virtual ~PolymorphicPayload() {} |
Samuel Benzaquen | 544ae5b | 2013-08-22 16:38:33 +0000 | [diff] [blame] | 55 | |
Samuel Benzaquen | b7488d7 | 2013-10-29 14:37:15 +0000 | [diff] [blame] | 56 | virtual llvm::Optional<DynTypedMatcher> getSingleMatcher() const { |
Samuel Benzaquen | 544ae5b | 2013-08-22 16:38:33 +0000 | [diff] [blame] | 57 | if (Matchers.size() != 1) |
Samuel Benzaquen | b7488d7 | 2013-10-29 14:37:15 +0000 | [diff] [blame] | 58 | return llvm::Optional<DynTypedMatcher>(); |
| 59 | return Matchers[0]; |
Samuel Benzaquen | 544ae5b | 2013-08-22 16:38:33 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | virtual std::string getTypeAsString() const { |
| 63 | std::string Inner; |
| 64 | for (size_t i = 0, e = Matchers.size(); i != e; ++i) { |
| 65 | if (i != 0) |
| 66 | Inner += "|"; |
Samuel Benzaquen | b7488d7 | 2013-10-29 14:37:15 +0000 | [diff] [blame] | 67 | Inner += Matchers[i].getSupportedKind().asStringRef(); |
Samuel Benzaquen | 544ae5b | 2013-08-22 16:38:33 +0000 | [diff] [blame] | 68 | } |
| 69 | return (Twine("Matcher<") + Inner + ">").str(); |
| 70 | } |
| 71 | |
Samuel Benzaquen | a735090 | 2013-08-28 18:42:04 +0000 | [diff] [blame] | 72 | virtual void makeTypedMatcher(MatcherOps &Ops) const { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 73 | bool FoundIsExact = false; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 74 | const DynTypedMatcher *Found = nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 75 | int NumFound = 0; |
Samuel Benzaquen | 544ae5b | 2013-08-22 16:38:33 +0000 | [diff] [blame] | 76 | for (size_t i = 0, e = Matchers.size(); i != e; ++i) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 77 | bool IsExactMatch; |
| 78 | if (Ops.canConstructFrom(Matchers[i], IsExactMatch)) { |
| 79 | if (Found) { |
| 80 | if (FoundIsExact) { |
| 81 | assert(!IsExactMatch && "We should not have two exact matches."); |
| 82 | continue; |
| 83 | } |
| 84 | } |
Samuel Benzaquen | b7488d7 | 2013-10-29 14:37:15 +0000 | [diff] [blame] | 85 | Found = &Matchers[i]; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 86 | FoundIsExact = IsExactMatch; |
| 87 | ++NumFound; |
Samuel Benzaquen | 544ae5b | 2013-08-22 16:38:33 +0000 | [diff] [blame] | 88 | } |
| 89 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 90 | // We only succeed if we found exactly one, or if we found an exact match. |
| 91 | if (Found && (FoundIsExact || NumFound == 1)) |
Samuel Benzaquen | a735090 | 2013-08-28 18:42:04 +0000 | [diff] [blame] | 92 | Ops.constructFrom(*Found); |
| 93 | } |
| 94 | |
Samuel Benzaquen | b7488d7 | 2013-10-29 14:37:15 +0000 | [diff] [blame] | 95 | const std::vector<DynTypedMatcher> Matchers; |
Samuel Benzaquen | a735090 | 2013-08-28 18:42:04 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | class VariantMatcher::VariadicOpPayload : public VariantMatcher::Payload { |
| 99 | public: |
| 100 | VariadicOpPayload(ast_matchers::internal::VariadicOperatorFunction Func, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 101 | std::vector<VariantMatcher> Args) |
| 102 | : Func(Func), Args(std::move(Args)) {} |
Samuel Benzaquen | a735090 | 2013-08-28 18:42:04 +0000 | [diff] [blame] | 103 | |
Samuel Benzaquen | b7488d7 | 2013-10-29 14:37:15 +0000 | [diff] [blame] | 104 | virtual llvm::Optional<DynTypedMatcher> getSingleMatcher() const { |
| 105 | return llvm::Optional<DynTypedMatcher>(); |
Samuel Benzaquen | a735090 | 2013-08-28 18:42:04 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | virtual std::string getTypeAsString() const { |
| 109 | std::string Inner; |
| 110 | for (size_t i = 0, e = Args.size(); i != e; ++i) { |
| 111 | if (i != 0) |
| 112 | Inner += "&"; |
| 113 | Inner += Args[i].getTypeAsString(); |
| 114 | } |
| 115 | return Inner; |
| 116 | } |
| 117 | |
| 118 | virtual void makeTypedMatcher(MatcherOps &Ops) const { |
| 119 | Ops.constructVariadicOperator(Func, Args); |
Samuel Benzaquen | 544ae5b | 2013-08-22 16:38:33 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | private: |
Samuel Benzaquen | a735090 | 2013-08-28 18:42:04 +0000 | [diff] [blame] | 123 | const ast_matchers::internal::VariadicOperatorFunction Func; |
| 124 | const std::vector<VariantMatcher> Args; |
Samuel Benzaquen | 544ae5b | 2013-08-22 16:38:33 +0000 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | VariantMatcher::VariantMatcher() {} |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame] | 128 | |
Samuel Benzaquen | 9d02807 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 129 | VariantMatcher VariantMatcher::SingleMatcher(const DynTypedMatcher &Matcher) { |
Samuel Benzaquen | 544ae5b | 2013-08-22 16:38:33 +0000 | [diff] [blame] | 130 | return VariantMatcher(new SinglePayload(Matcher)); |
Samuel Benzaquen | 9d02807 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | VariantMatcher |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 134 | VariantMatcher::PolymorphicMatcher(std::vector<DynTypedMatcher> Matchers) { |
| 135 | return VariantMatcher(new PolymorphicPayload(std::move(Matchers))); |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Samuel Benzaquen | a735090 | 2013-08-28 18:42:04 +0000 | [diff] [blame] | 138 | VariantMatcher VariantMatcher::VariadicOperatorMatcher( |
| 139 | ast_matchers::internal::VariadicOperatorFunction Func, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 140 | std::vector<VariantMatcher> Args) { |
| 141 | return VariantMatcher(new VariadicOpPayload(Func, std::move(Args))); |
Samuel Benzaquen | a735090 | 2013-08-28 18:42:04 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Samuel Benzaquen | b7488d7 | 2013-10-29 14:37:15 +0000 | [diff] [blame] | 144 | llvm::Optional<DynTypedMatcher> VariantMatcher::getSingleMatcher() const { |
| 145 | return Value ? Value->getSingleMatcher() : llvm::Optional<DynTypedMatcher>(); |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Samuel Benzaquen | 544ae5b | 2013-08-22 16:38:33 +0000 | [diff] [blame] | 148 | void VariantMatcher::reset() { Value.reset(); } |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame] | 149 | |
Samuel Benzaquen | 9d02807 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 150 | std::string VariantMatcher::getTypeAsString() const { |
Samuel Benzaquen | 544ae5b | 2013-08-22 16:38:33 +0000 | [diff] [blame] | 151 | if (Value) return Value->getTypeAsString(); |
| 152 | return "<Nothing>"; |
Samuel Benzaquen | 9d02807 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 155 | VariantValue::VariantValue(const VariantValue &Other) : Type(VT_Nothing) { |
| 156 | *this = Other; |
| 157 | } |
| 158 | |
Samuel Benzaquen | 7a337af | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 159 | VariantValue::VariantValue(unsigned Unsigned) : Type(VT_Nothing) { |
| 160 | setUnsigned(Unsigned); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | VariantValue::VariantValue(const std::string &String) : Type(VT_Nothing) { |
| 164 | setString(String); |
| 165 | } |
| 166 | |
Samuel Benzaquen | 9d02807 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 167 | VariantValue::VariantValue(const VariantMatcher &Matcher) : Type(VT_Nothing) { |
| 168 | setMatcher(Matcher); |
Samuel Benzaquen | 7a337af | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 171 | VariantValue::~VariantValue() { reset(); } |
| 172 | |
| 173 | VariantValue &VariantValue::operator=(const VariantValue &Other) { |
| 174 | if (this == &Other) return *this; |
| 175 | reset(); |
| 176 | switch (Other.Type) { |
Samuel Benzaquen | 7a337af | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 177 | case VT_Unsigned: |
| 178 | setUnsigned(Other.getUnsigned()); |
| 179 | break; |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 180 | case VT_String: |
| 181 | setString(Other.getString()); |
| 182 | break; |
Samuel Benzaquen | 9d02807 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 183 | case VT_Matcher: |
| 184 | setMatcher(Other.getMatcher()); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 185 | break; |
| 186 | case VT_Nothing: |
| 187 | Type = VT_Nothing; |
| 188 | break; |
| 189 | } |
| 190 | return *this; |
| 191 | } |
| 192 | |
| 193 | void VariantValue::reset() { |
| 194 | switch (Type) { |
| 195 | case VT_String: |
| 196 | delete Value.String; |
| 197 | break; |
Samuel Benzaquen | 9d02807 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 198 | case VT_Matcher: |
| 199 | delete Value.Matcher; |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 200 | break; |
| 201 | // Cases that do nothing. |
Samuel Benzaquen | 7a337af | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 202 | case VT_Unsigned: |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 203 | case VT_Nothing: |
| 204 | break; |
| 205 | } |
| 206 | Type = VT_Nothing; |
| 207 | } |
| 208 | |
Samuel Benzaquen | 7a337af | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 209 | bool VariantValue::isUnsigned() const { |
| 210 | return Type == VT_Unsigned; |
| 211 | } |
| 212 | |
| 213 | unsigned VariantValue::getUnsigned() const { |
| 214 | assert(isUnsigned()); |
| 215 | return Value.Unsigned; |
| 216 | } |
| 217 | |
| 218 | void VariantValue::setUnsigned(unsigned NewValue) { |
| 219 | reset(); |
| 220 | Type = VT_Unsigned; |
| 221 | Value.Unsigned = NewValue; |
| 222 | } |
| 223 | |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 224 | bool VariantValue::isString() const { |
| 225 | return Type == VT_String; |
| 226 | } |
| 227 | |
| 228 | const std::string &VariantValue::getString() const { |
| 229 | assert(isString()); |
| 230 | return *Value.String; |
| 231 | } |
| 232 | |
| 233 | void VariantValue::setString(const std::string &NewValue) { |
| 234 | reset(); |
| 235 | Type = VT_String; |
| 236 | Value.String = new std::string(NewValue); |
| 237 | } |
| 238 | |
Samuel Benzaquen | 9d02807 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 239 | bool VariantValue::isMatcher() const { |
| 240 | return Type == VT_Matcher; |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Samuel Benzaquen | 9d02807 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 243 | const VariantMatcher &VariantValue::getMatcher() const { |
| 244 | assert(isMatcher()); |
| 245 | return *Value.Matcher; |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Samuel Benzaquen | 9d02807 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 248 | void VariantValue::setMatcher(const VariantMatcher &NewValue) { |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 249 | reset(); |
Samuel Benzaquen | 9d02807 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 250 | Type = VT_Matcher; |
| 251 | Value.Matcher = new VariantMatcher(NewValue); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 254 | std::string VariantValue::getTypeAsString() const { |
| 255 | switch (Type) { |
| 256 | case VT_String: return "String"; |
Samuel Benzaquen | 9d02807 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 257 | case VT_Matcher: return getMatcher().getTypeAsString(); |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 258 | case VT_Unsigned: return "Unsigned"; |
| 259 | case VT_Nothing: return "Nothing"; |
| 260 | } |
| 261 | llvm_unreachable("Invalid Type"); |
| 262 | } |
| 263 | |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 264 | } // end namespace dynamic |
| 265 | } // end namespace ast_matchers |
| 266 | } // end namespace clang |