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" |
| 16 | |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 17 | #include "clang/Basic/LLVM.h" |
| 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 | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame^] | 23 | MatcherList::MatcherList() : List() {} |
| 24 | |
| 25 | MatcherList::MatcherList(const DynTypedMatcher &Matcher) |
| 26 | : List(1, Matcher.clone()) {} |
| 27 | |
| 28 | MatcherList::MatcherList(const MatcherList& Other) { |
| 29 | *this = Other; |
| 30 | } |
| 31 | |
| 32 | MatcherList::~MatcherList() { |
| 33 | reset(); |
| 34 | } |
| 35 | |
| 36 | MatcherList &MatcherList::operator=(const MatcherList &Other) { |
| 37 | if (this == &Other) return *this; |
| 38 | reset(); |
| 39 | for (size_t i = 0, e = Other.List.size(); i != e; ++i) { |
| 40 | List.push_back(Other.List[i]->clone()); |
| 41 | } |
| 42 | return *this; |
| 43 | } |
| 44 | |
| 45 | void MatcherList::add(const DynTypedMatcher &Matcher) { |
| 46 | List.push_back(Matcher.clone()); |
| 47 | } |
| 48 | |
| 49 | void MatcherList::reset() { |
| 50 | for (size_t i = 0, e = List.size(); i != e; ++i) { |
| 51 | delete List[i]; |
| 52 | } |
| 53 | List.resize(0); |
| 54 | } |
| 55 | |
| 56 | std::string MatcherList::getTypeAsString() const { |
| 57 | std::string Inner; |
| 58 | for (size_t I = 0, E = List.size(); I != E; ++I) { |
| 59 | if (I != 0) Inner += "|"; |
| 60 | Inner += List[I]->getSupportedKind().asStringRef(); |
| 61 | } |
| 62 | return (Twine("Matcher<") + Inner + ">").str(); |
| 63 | } |
| 64 | |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 65 | VariantValue::VariantValue(const VariantValue &Other) : Type(VT_Nothing) { |
| 66 | *this = Other; |
| 67 | } |
| 68 | |
Samuel Benzaquen | 7a337af | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 69 | VariantValue::VariantValue(unsigned Unsigned) : Type(VT_Nothing) { |
| 70 | setUnsigned(Unsigned); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | VariantValue::VariantValue(const std::string &String) : Type(VT_Nothing) { |
| 74 | setString(String); |
| 75 | } |
| 76 | |
Samuel Benzaquen | 7a337af | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 77 | VariantValue::VariantValue(const DynTypedMatcher &Matcher) : Type(VT_Nothing) { |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame^] | 78 | setMatchers(MatcherList(Matcher)); |
| 79 | } |
| 80 | |
| 81 | VariantValue::VariantValue(const MatcherList &Matchers) : Type(VT_Nothing) { |
| 82 | setMatchers(Matchers); |
Samuel Benzaquen | 7a337af | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 85 | VariantValue::~VariantValue() { reset(); } |
| 86 | |
| 87 | VariantValue &VariantValue::operator=(const VariantValue &Other) { |
| 88 | if (this == &Other) return *this; |
| 89 | reset(); |
| 90 | switch (Other.Type) { |
Samuel Benzaquen | 7a337af | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 91 | case VT_Unsigned: |
| 92 | setUnsigned(Other.getUnsigned()); |
| 93 | break; |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 94 | case VT_String: |
| 95 | setString(Other.getString()); |
| 96 | break; |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame^] | 97 | case VT_Matchers: |
| 98 | setMatchers(Other.getMatchers()); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 99 | break; |
| 100 | case VT_Nothing: |
| 101 | Type = VT_Nothing; |
| 102 | break; |
| 103 | } |
| 104 | return *this; |
| 105 | } |
| 106 | |
| 107 | void VariantValue::reset() { |
| 108 | switch (Type) { |
| 109 | case VT_String: |
| 110 | delete Value.String; |
| 111 | break; |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame^] | 112 | case VT_Matchers: |
| 113 | delete Value.Matchers; |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 114 | break; |
| 115 | // Cases that do nothing. |
Samuel Benzaquen | 7a337af | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 116 | case VT_Unsigned: |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 117 | case VT_Nothing: |
| 118 | break; |
| 119 | } |
| 120 | Type = VT_Nothing; |
| 121 | } |
| 122 | |
Samuel Benzaquen | 7a337af | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 123 | bool VariantValue::isUnsigned() const { |
| 124 | return Type == VT_Unsigned; |
| 125 | } |
| 126 | |
| 127 | unsigned VariantValue::getUnsigned() const { |
| 128 | assert(isUnsigned()); |
| 129 | return Value.Unsigned; |
| 130 | } |
| 131 | |
| 132 | void VariantValue::setUnsigned(unsigned NewValue) { |
| 133 | reset(); |
| 134 | Type = VT_Unsigned; |
| 135 | Value.Unsigned = NewValue; |
| 136 | } |
| 137 | |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 138 | bool VariantValue::isString() const { |
| 139 | return Type == VT_String; |
| 140 | } |
| 141 | |
| 142 | const std::string &VariantValue::getString() const { |
| 143 | assert(isString()); |
| 144 | return *Value.String; |
| 145 | } |
| 146 | |
| 147 | void VariantValue::setString(const std::string &NewValue) { |
| 148 | reset(); |
| 149 | Type = VT_String; |
| 150 | Value.String = new std::string(NewValue); |
| 151 | } |
| 152 | |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame^] | 153 | bool VariantValue::isMatchers() const { |
| 154 | return Type == VT_Matchers; |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame^] | 157 | const MatcherList &VariantValue::getMatchers() const { |
| 158 | assert(isMatchers()); |
| 159 | return *Value.Matchers; |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame^] | 162 | void VariantValue::setMatchers(const MatcherList &NewValue) { |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 163 | reset(); |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame^] | 164 | Type = VT_Matchers; |
| 165 | Value.Matchers = new MatcherList(NewValue); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 168 | std::string VariantValue::getTypeAsString() const { |
| 169 | switch (Type) { |
| 170 | case VT_String: return "String"; |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame^] | 171 | case VT_Matchers: return getMatchers().getTypeAsString(); |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 172 | case VT_Unsigned: return "Unsigned"; |
| 173 | case VT_Nothing: return "Nothing"; |
| 174 | } |
| 175 | llvm_unreachable("Invalid Type"); |
| 176 | } |
| 177 | |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 178 | } // end namespace dynamic |
| 179 | } // end namespace ast_matchers |
| 180 | } // end namespace clang |