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 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 23 | std::string ArgKind::asString() const { |
| 24 | switch (getArgKind()) { |
| 25 | case AK_Matcher: |
| 26 | return (Twine("Matcher<") + MatcherKind.asStringRef() + ">").str(); |
| 27 | case AK_Unsigned: |
| 28 | return "unsigned"; |
| 29 | case AK_String: |
| 30 | return "string"; |
| 31 | } |
| 32 | llvm_unreachable("unhandled ArgKind"); |
| 33 | } |
| 34 | |
| 35 | bool ArgKind::isConvertibleTo(ArgKind To, unsigned *Specificity) const { |
| 36 | if (K != To.K) |
| 37 | return false; |
| 38 | if (K != AK_Matcher) { |
| 39 | if (Specificity) |
| 40 | *Specificity = 1; |
| 41 | return true; |
| 42 | } |
| 43 | unsigned Distance; |
| 44 | if (!MatcherKind.isBaseOf(To.MatcherKind, &Distance)) |
| 45 | return false; |
| 46 | |
| 47 | if (Specificity) |
| 48 | *Specificity = 100 - Distance; |
| 49 | return true; |
| 50 | } |
| 51 | |
| 52 | bool |
| 53 | VariantMatcher::MatcherOps::canConstructFrom(const DynTypedMatcher &Matcher, |
| 54 | bool &IsExactMatch) const { |
| 55 | IsExactMatch = Matcher.getSupportedKind().isSame(NodeKind); |
| 56 | return Matcher.canConvertTo(NodeKind); |
| 57 | } |
| 58 | |
| 59 | llvm::Optional<DynTypedMatcher> |
| 60 | VariantMatcher::MatcherOps::constructVariadicOperator( |
| 61 | DynTypedMatcher::VariadicOperator Op, |
| 62 | ArrayRef<VariantMatcher> InnerMatchers) const { |
| 63 | std::vector<DynTypedMatcher> DynMatchers; |
| 64 | for (const auto &InnerMatcher : InnerMatchers) { |
| 65 | // Abort if any of the inner matchers can't be converted to |
| 66 | // Matcher<T>. |
| 67 | if (!InnerMatcher.Value) |
| 68 | return llvm::None; |
| 69 | llvm::Optional<DynTypedMatcher> Inner = |
| 70 | InnerMatcher.Value->getTypedMatcher(*this); |
| 71 | if (!Inner) |
| 72 | return llvm::None; |
| 73 | DynMatchers.push_back(*Inner); |
| 74 | } |
| 75 | return DynTypedMatcher::constructVariadic(Op, DynMatchers); |
| 76 | } |
| 77 | |
Samuel Benzaquen | 544ae5b | 2013-08-22 16:38:33 +0000 | [diff] [blame] | 78 | VariantMatcher::Payload::~Payload() {} |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame] | 79 | |
Samuel Benzaquen | 544ae5b | 2013-08-22 16:38:33 +0000 | [diff] [blame] | 80 | class VariantMatcher::SinglePayload : public VariantMatcher::Payload { |
| 81 | public: |
Samuel Benzaquen | b7488d7 | 2013-10-29 14:37:15 +0000 | [diff] [blame] | 82 | SinglePayload(const DynTypedMatcher &Matcher) : Matcher(Matcher) {} |
Samuel Benzaquen | 544ae5b | 2013-08-22 16:38:33 +0000 | [diff] [blame] | 83 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 84 | llvm::Optional<DynTypedMatcher> getSingleMatcher() const override { |
Samuel Benzaquen | b7488d7 | 2013-10-29 14:37:15 +0000 | [diff] [blame] | 85 | return Matcher; |
Samuel Benzaquen | 544ae5b | 2013-08-22 16:38:33 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 88 | std::string getTypeAsString() const override { |
Samuel Benzaquen | b7488d7 | 2013-10-29 14:37:15 +0000 | [diff] [blame] | 89 | return (Twine("Matcher<") + Matcher.getSupportedKind().asStringRef() + ">") |
Samuel Benzaquen | 544ae5b | 2013-08-22 16:38:33 +0000 | [diff] [blame] | 90 | .str(); |
| 91 | } |
| 92 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 93 | llvm::Optional<DynTypedMatcher> |
| 94 | getTypedMatcher(const MatcherOps &Ops) const override { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 95 | bool Ignore; |
| 96 | if (Ops.canConstructFrom(Matcher, Ignore)) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 97 | return Matcher; |
| 98 | return llvm::None; |
| 99 | } |
| 100 | |
| 101 | bool isConvertibleTo(ast_type_traits::ASTNodeKind Kind, |
| 102 | unsigned *Specificity) const override { |
| 103 | return ArgKind(Matcher.getSupportedKind()) |
| 104 | .isConvertibleTo(Kind, Specificity); |
Samuel Benzaquen | 544ae5b | 2013-08-22 16:38:33 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | private: |
Samuel Benzaquen | b7488d7 | 2013-10-29 14:37:15 +0000 | [diff] [blame] | 108 | const DynTypedMatcher Matcher; |
Samuel Benzaquen | 544ae5b | 2013-08-22 16:38:33 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | class VariantMatcher::PolymorphicPayload : public VariantMatcher::Payload { |
| 112 | public: |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 113 | PolymorphicPayload(std::vector<DynTypedMatcher> MatchersIn) |
| 114 | : Matchers(std::move(MatchersIn)) {} |
Samuel Benzaquen | 544ae5b | 2013-08-22 16:38:33 +0000 | [diff] [blame] | 115 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 116 | ~PolymorphicPayload() override {} |
Samuel Benzaquen | 544ae5b | 2013-08-22 16:38:33 +0000 | [diff] [blame] | 117 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 118 | llvm::Optional<DynTypedMatcher> getSingleMatcher() const override { |
Samuel Benzaquen | 544ae5b | 2013-08-22 16:38:33 +0000 | [diff] [blame] | 119 | if (Matchers.size() != 1) |
Samuel Benzaquen | b7488d7 | 2013-10-29 14:37:15 +0000 | [diff] [blame] | 120 | return llvm::Optional<DynTypedMatcher>(); |
| 121 | return Matchers[0]; |
Samuel Benzaquen | 544ae5b | 2013-08-22 16:38:33 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 124 | std::string getTypeAsString() const override { |
Samuel Benzaquen | 544ae5b | 2013-08-22 16:38:33 +0000 | [diff] [blame] | 125 | std::string Inner; |
| 126 | for (size_t i = 0, e = Matchers.size(); i != e; ++i) { |
| 127 | if (i != 0) |
| 128 | Inner += "|"; |
Samuel Benzaquen | b7488d7 | 2013-10-29 14:37:15 +0000 | [diff] [blame] | 129 | Inner += Matchers[i].getSupportedKind().asStringRef(); |
Samuel Benzaquen | 544ae5b | 2013-08-22 16:38:33 +0000 | [diff] [blame] | 130 | } |
| 131 | return (Twine("Matcher<") + Inner + ">").str(); |
| 132 | } |
| 133 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 134 | llvm::Optional<DynTypedMatcher> |
| 135 | getTypedMatcher(const MatcherOps &Ops) const override { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 136 | bool FoundIsExact = false; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 137 | const DynTypedMatcher *Found = nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 138 | int NumFound = 0; |
Samuel Benzaquen | 544ae5b | 2013-08-22 16:38:33 +0000 | [diff] [blame] | 139 | for (size_t i = 0, e = Matchers.size(); i != e; ++i) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 140 | bool IsExactMatch; |
| 141 | if (Ops.canConstructFrom(Matchers[i], IsExactMatch)) { |
| 142 | if (Found) { |
| 143 | if (FoundIsExact) { |
| 144 | assert(!IsExactMatch && "We should not have two exact matches."); |
| 145 | continue; |
| 146 | } |
| 147 | } |
Samuel Benzaquen | b7488d7 | 2013-10-29 14:37:15 +0000 | [diff] [blame] | 148 | Found = &Matchers[i]; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 149 | FoundIsExact = IsExactMatch; |
| 150 | ++NumFound; |
Samuel Benzaquen | 544ae5b | 2013-08-22 16:38:33 +0000 | [diff] [blame] | 151 | } |
| 152 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 153 | // We only succeed if we found exactly one, or if we found an exact match. |
| 154 | if (Found && (FoundIsExact || NumFound == 1)) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 155 | return *Found; |
| 156 | return llvm::None; |
| 157 | } |
| 158 | |
| 159 | bool isConvertibleTo(ast_type_traits::ASTNodeKind Kind, |
| 160 | unsigned *Specificity) const override { |
| 161 | unsigned MaxSpecificity = 0; |
| 162 | for (const DynTypedMatcher &Matcher : Matchers) { |
| 163 | unsigned ThisSpecificity; |
| 164 | if (ArgKind(Matcher.getSupportedKind()) |
| 165 | .isConvertibleTo(Kind, &ThisSpecificity)) { |
| 166 | MaxSpecificity = std::max(MaxSpecificity, ThisSpecificity); |
| 167 | } |
| 168 | } |
| 169 | if (Specificity) |
| 170 | *Specificity = MaxSpecificity; |
| 171 | return MaxSpecificity > 0; |
Samuel Benzaquen | a735090 | 2013-08-28 18:42:04 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Samuel Benzaquen | b7488d7 | 2013-10-29 14:37:15 +0000 | [diff] [blame] | 174 | const std::vector<DynTypedMatcher> Matchers; |
Samuel Benzaquen | a735090 | 2013-08-28 18:42:04 +0000 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | class VariantMatcher::VariadicOpPayload : public VariantMatcher::Payload { |
| 178 | public: |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 179 | VariadicOpPayload(DynTypedMatcher::VariadicOperator Op, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 180 | std::vector<VariantMatcher> Args) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 181 | : Op(Op), Args(std::move(Args)) {} |
Samuel Benzaquen | a735090 | 2013-08-28 18:42:04 +0000 | [diff] [blame] | 182 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 183 | llvm::Optional<DynTypedMatcher> getSingleMatcher() const override { |
Samuel Benzaquen | b7488d7 | 2013-10-29 14:37:15 +0000 | [diff] [blame] | 184 | return llvm::Optional<DynTypedMatcher>(); |
Samuel Benzaquen | a735090 | 2013-08-28 18:42:04 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 187 | std::string getTypeAsString() const override { |
Samuel Benzaquen | a735090 | 2013-08-28 18:42:04 +0000 | [diff] [blame] | 188 | std::string Inner; |
| 189 | for (size_t i = 0, e = Args.size(); i != e; ++i) { |
| 190 | if (i != 0) |
| 191 | Inner += "&"; |
| 192 | Inner += Args[i].getTypeAsString(); |
| 193 | } |
| 194 | return Inner; |
| 195 | } |
| 196 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 197 | llvm::Optional<DynTypedMatcher> |
| 198 | getTypedMatcher(const MatcherOps &Ops) const override { |
| 199 | return Ops.constructVariadicOperator(Op, Args); |
| 200 | } |
| 201 | |
| 202 | bool isConvertibleTo(ast_type_traits::ASTNodeKind Kind, |
| 203 | unsigned *Specificity) const override { |
| 204 | for (const VariantMatcher &Matcher : Args) { |
| 205 | if (!Matcher.isConvertibleTo(Kind, Specificity)) |
| 206 | return false; |
| 207 | } |
| 208 | return true; |
Samuel Benzaquen | 544ae5b | 2013-08-22 16:38:33 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | private: |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 212 | const DynTypedMatcher::VariadicOperator Op; |
Samuel Benzaquen | a735090 | 2013-08-28 18:42:04 +0000 | [diff] [blame] | 213 | const std::vector<VariantMatcher> Args; |
Samuel Benzaquen | 544ae5b | 2013-08-22 16:38:33 +0000 | [diff] [blame] | 214 | }; |
| 215 | |
| 216 | VariantMatcher::VariantMatcher() {} |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame] | 217 | |
Samuel Benzaquen | 9d02807 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 218 | VariantMatcher VariantMatcher::SingleMatcher(const DynTypedMatcher &Matcher) { |
Samuel Benzaquen | 544ae5b | 2013-08-22 16:38:33 +0000 | [diff] [blame] | 219 | return VariantMatcher(new SinglePayload(Matcher)); |
Samuel Benzaquen | 9d02807 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | VariantMatcher |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 223 | VariantMatcher::PolymorphicMatcher(std::vector<DynTypedMatcher> Matchers) { |
| 224 | return VariantMatcher(new PolymorphicPayload(std::move(Matchers))); |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Samuel Benzaquen | a735090 | 2013-08-28 18:42:04 +0000 | [diff] [blame] | 227 | VariantMatcher VariantMatcher::VariadicOperatorMatcher( |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 228 | DynTypedMatcher::VariadicOperator Op, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 229 | std::vector<VariantMatcher> Args) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 230 | return VariantMatcher(new VariadicOpPayload(Op, std::move(Args))); |
Samuel Benzaquen | a735090 | 2013-08-28 18:42:04 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Samuel Benzaquen | b7488d7 | 2013-10-29 14:37:15 +0000 | [diff] [blame] | 233 | llvm::Optional<DynTypedMatcher> VariantMatcher::getSingleMatcher() const { |
| 234 | return Value ? Value->getSingleMatcher() : llvm::Optional<DynTypedMatcher>(); |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Samuel Benzaquen | 544ae5b | 2013-08-22 16:38:33 +0000 | [diff] [blame] | 237 | void VariantMatcher::reset() { Value.reset(); } |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame] | 238 | |
Samuel Benzaquen | 9d02807 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 239 | std::string VariantMatcher::getTypeAsString() const { |
Samuel Benzaquen | 544ae5b | 2013-08-22 16:38:33 +0000 | [diff] [blame] | 240 | if (Value) return Value->getTypeAsString(); |
| 241 | return "<Nothing>"; |
Samuel Benzaquen | 9d02807 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 244 | VariantValue::VariantValue(const VariantValue &Other) : Type(VT_Nothing) { |
| 245 | *this = Other; |
| 246 | } |
| 247 | |
Samuel Benzaquen | 7a337af | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 248 | VariantValue::VariantValue(unsigned Unsigned) : Type(VT_Nothing) { |
| 249 | setUnsigned(Unsigned); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | VariantValue::VariantValue(const std::string &String) : Type(VT_Nothing) { |
| 253 | setString(String); |
| 254 | } |
| 255 | |
Samuel Benzaquen | 9d02807 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 256 | VariantValue::VariantValue(const VariantMatcher &Matcher) : Type(VT_Nothing) { |
| 257 | setMatcher(Matcher); |
Samuel Benzaquen | 7a337af | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 260 | VariantValue::~VariantValue() { reset(); } |
| 261 | |
| 262 | VariantValue &VariantValue::operator=(const VariantValue &Other) { |
| 263 | if (this == &Other) return *this; |
| 264 | reset(); |
| 265 | switch (Other.Type) { |
Samuel Benzaquen | 7a337af | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 266 | case VT_Unsigned: |
| 267 | setUnsigned(Other.getUnsigned()); |
| 268 | break; |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 269 | case VT_String: |
| 270 | setString(Other.getString()); |
| 271 | break; |
Samuel Benzaquen | 9d02807 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 272 | case VT_Matcher: |
| 273 | setMatcher(Other.getMatcher()); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 274 | break; |
| 275 | case VT_Nothing: |
| 276 | Type = VT_Nothing; |
| 277 | break; |
| 278 | } |
| 279 | return *this; |
| 280 | } |
| 281 | |
| 282 | void VariantValue::reset() { |
| 283 | switch (Type) { |
| 284 | case VT_String: |
| 285 | delete Value.String; |
| 286 | break; |
Samuel Benzaquen | 9d02807 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 287 | case VT_Matcher: |
| 288 | delete Value.Matcher; |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 289 | break; |
| 290 | // Cases that do nothing. |
Samuel Benzaquen | 7a337af | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 291 | case VT_Unsigned: |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 292 | case VT_Nothing: |
| 293 | break; |
| 294 | } |
| 295 | Type = VT_Nothing; |
| 296 | } |
| 297 | |
Samuel Benzaquen | 7a337af | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 298 | bool VariantValue::isUnsigned() const { |
| 299 | return Type == VT_Unsigned; |
| 300 | } |
| 301 | |
| 302 | unsigned VariantValue::getUnsigned() const { |
| 303 | assert(isUnsigned()); |
| 304 | return Value.Unsigned; |
| 305 | } |
| 306 | |
| 307 | void VariantValue::setUnsigned(unsigned NewValue) { |
| 308 | reset(); |
| 309 | Type = VT_Unsigned; |
| 310 | Value.Unsigned = NewValue; |
| 311 | } |
| 312 | |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 313 | bool VariantValue::isString() const { |
| 314 | return Type == VT_String; |
| 315 | } |
| 316 | |
| 317 | const std::string &VariantValue::getString() const { |
| 318 | assert(isString()); |
| 319 | return *Value.String; |
| 320 | } |
| 321 | |
| 322 | void VariantValue::setString(const std::string &NewValue) { |
| 323 | reset(); |
| 324 | Type = VT_String; |
| 325 | Value.String = new std::string(NewValue); |
| 326 | } |
| 327 | |
Samuel Benzaquen | 9d02807 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 328 | bool VariantValue::isMatcher() const { |
| 329 | return Type == VT_Matcher; |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Samuel Benzaquen | 9d02807 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 332 | const VariantMatcher &VariantValue::getMatcher() const { |
| 333 | assert(isMatcher()); |
| 334 | return *Value.Matcher; |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Samuel Benzaquen | 9d02807 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 337 | void VariantValue::setMatcher(const VariantMatcher &NewValue) { |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 338 | reset(); |
Samuel Benzaquen | 9d02807 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 339 | Type = VT_Matcher; |
| 340 | Value.Matcher = new VariantMatcher(NewValue); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 343 | bool VariantValue::isConvertibleTo(ArgKind Kind, unsigned *Specificity) const { |
| 344 | switch (Kind.getArgKind()) { |
| 345 | case ArgKind::AK_Unsigned: |
| 346 | if (!isUnsigned()) |
| 347 | return false; |
| 348 | *Specificity = 1; |
| 349 | return true; |
| 350 | |
| 351 | case ArgKind::AK_String: |
| 352 | if (!isString()) |
| 353 | return false; |
| 354 | *Specificity = 1; |
| 355 | return true; |
| 356 | |
| 357 | case ArgKind::AK_Matcher: |
| 358 | if (!isMatcher()) |
| 359 | return false; |
| 360 | return getMatcher().isConvertibleTo(Kind.getMatcherKind(), Specificity); |
| 361 | } |
| 362 | llvm_unreachable("Invalid Type"); |
| 363 | } |
| 364 | |
| 365 | bool VariantValue::isConvertibleTo(ArrayRef<ArgKind> Kinds, |
| 366 | unsigned *Specificity) const { |
| 367 | unsigned MaxSpecificity = 0; |
| 368 | for (const ArgKind& Kind : Kinds) { |
| 369 | unsigned ThisSpecificity; |
| 370 | if (!isConvertibleTo(Kind, &ThisSpecificity)) |
| 371 | continue; |
| 372 | MaxSpecificity = std::max(MaxSpecificity, ThisSpecificity); |
| 373 | } |
| 374 | if (Specificity && MaxSpecificity > 0) { |
| 375 | *Specificity = MaxSpecificity; |
| 376 | } |
| 377 | return MaxSpecificity > 0; |
| 378 | } |
| 379 | |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 380 | std::string VariantValue::getTypeAsString() const { |
| 381 | switch (Type) { |
| 382 | case VT_String: return "String"; |
Samuel Benzaquen | 9d02807 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 383 | case VT_Matcher: return getMatcher().getTypeAsString(); |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 384 | case VT_Unsigned: return "Unsigned"; |
| 385 | case VT_Nothing: return "Nothing"; |
| 386 | } |
| 387 | llvm_unreachable("Invalid Type"); |
| 388 | } |
| 389 | |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 390 | } // end namespace dynamic |
| 391 | } // end namespace ast_matchers |
| 392 | } // end namespace clang |