blob: 79c5c8e23a65dff1d8ab2a3a31b08aa0215ec9c2 [file] [log] [blame]
Manuel Klimekf7f295f2013-05-14 09:13:00 +00001//===--- 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 Benzaquen76c2f922013-06-20 14:28:32 +000017#include "clang/Basic/LLVM.h"
18
Manuel Klimekf7f295f2013-05-14 09:13:00 +000019namespace clang {
20namespace ast_matchers {
21namespace dynamic {
22
Samuel Benzaquenef7eb022013-06-21 15:51:31 +000023MatcherList::MatcherList() : List() {}
24
25MatcherList::MatcherList(const DynTypedMatcher &Matcher)
26 : List(1, Matcher.clone()) {}
27
28MatcherList::MatcherList(const MatcherList& Other) {
29 *this = Other;
30}
31
32MatcherList::~MatcherList() {
33 reset();
34}
35
36MatcherList &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
45void MatcherList::add(const DynTypedMatcher &Matcher) {
46 List.push_back(Matcher.clone());
47}
48
49void 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
56std::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 Klimekf7f295f2013-05-14 09:13:00 +000065VariantValue::VariantValue(const VariantValue &Other) : Type(VT_Nothing) {
66 *this = Other;
67}
68
Samuel Benzaquen7a337af2013-06-04 15:46:22 +000069VariantValue::VariantValue(unsigned Unsigned) : Type(VT_Nothing) {
70 setUnsigned(Unsigned);
Manuel Klimekf7f295f2013-05-14 09:13:00 +000071}
72
73VariantValue::VariantValue(const std::string &String) : Type(VT_Nothing) {
74 setString(String);
75}
76
Samuel Benzaquen7a337af2013-06-04 15:46:22 +000077VariantValue::VariantValue(const DynTypedMatcher &Matcher) : Type(VT_Nothing) {
Samuel Benzaquenef7eb022013-06-21 15:51:31 +000078 setMatchers(MatcherList(Matcher));
79}
80
81VariantValue::VariantValue(const MatcherList &Matchers) : Type(VT_Nothing) {
82 setMatchers(Matchers);
Samuel Benzaquen7a337af2013-06-04 15:46:22 +000083}
84
Manuel Klimekf7f295f2013-05-14 09:13:00 +000085VariantValue::~VariantValue() { reset(); }
86
87VariantValue &VariantValue::operator=(const VariantValue &Other) {
88 if (this == &Other) return *this;
89 reset();
90 switch (Other.Type) {
Samuel Benzaquen7a337af2013-06-04 15:46:22 +000091 case VT_Unsigned:
92 setUnsigned(Other.getUnsigned());
93 break;
Manuel Klimekf7f295f2013-05-14 09:13:00 +000094 case VT_String:
95 setString(Other.getString());
96 break;
Samuel Benzaquenef7eb022013-06-21 15:51:31 +000097 case VT_Matchers:
98 setMatchers(Other.getMatchers());
Manuel Klimekf7f295f2013-05-14 09:13:00 +000099 break;
100 case VT_Nothing:
101 Type = VT_Nothing;
102 break;
103 }
104 return *this;
105}
106
107void VariantValue::reset() {
108 switch (Type) {
109 case VT_String:
110 delete Value.String;
111 break;
Samuel Benzaquenef7eb022013-06-21 15:51:31 +0000112 case VT_Matchers:
113 delete Value.Matchers;
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000114 break;
115 // Cases that do nothing.
Samuel Benzaquen7a337af2013-06-04 15:46:22 +0000116 case VT_Unsigned:
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000117 case VT_Nothing:
118 break;
119 }
120 Type = VT_Nothing;
121}
122
Samuel Benzaquen7a337af2013-06-04 15:46:22 +0000123bool VariantValue::isUnsigned() const {
124 return Type == VT_Unsigned;
125}
126
127unsigned VariantValue::getUnsigned() const {
128 assert(isUnsigned());
129 return Value.Unsigned;
130}
131
132void VariantValue::setUnsigned(unsigned NewValue) {
133 reset();
134 Type = VT_Unsigned;
135 Value.Unsigned = NewValue;
136}
137
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000138bool VariantValue::isString() const {
139 return Type == VT_String;
140}
141
142const std::string &VariantValue::getString() const {
143 assert(isString());
144 return *Value.String;
145}
146
147void VariantValue::setString(const std::string &NewValue) {
148 reset();
149 Type = VT_String;
150 Value.String = new std::string(NewValue);
151}
152
Samuel Benzaquenef7eb022013-06-21 15:51:31 +0000153bool VariantValue::isMatchers() const {
154 return Type == VT_Matchers;
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000155}
156
Samuel Benzaquenef7eb022013-06-21 15:51:31 +0000157const MatcherList &VariantValue::getMatchers() const {
158 assert(isMatchers());
159 return *Value.Matchers;
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000160}
161
Samuel Benzaquenef7eb022013-06-21 15:51:31 +0000162void VariantValue::setMatchers(const MatcherList &NewValue) {
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000163 reset();
Samuel Benzaquenef7eb022013-06-21 15:51:31 +0000164 Type = VT_Matchers;
165 Value.Matchers = new MatcherList(NewValue);
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000166}
167
Samuel Benzaquen76c2f922013-06-20 14:28:32 +0000168std::string VariantValue::getTypeAsString() const {
169 switch (Type) {
170 case VT_String: return "String";
Samuel Benzaquenef7eb022013-06-21 15:51:31 +0000171 case VT_Matchers: return getMatchers().getTypeAsString();
Samuel Benzaquen76c2f922013-06-20 14:28:32 +0000172 case VT_Unsigned: return "Unsigned";
173 case VT_Nothing: return "Nothing";
174 }
175 llvm_unreachable("Invalid Type");
176}
177
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000178} // end namespace dynamic
179} // end namespace ast_matchers
180} // end namespace clang