blob: 6fcbe7fc4955eb297af87aeb973e63ecd2e13c9f [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
17namespace clang {
18namespace ast_matchers {
19namespace dynamic {
20
21VariantValue::VariantValue(const VariantValue &Other) : Type(VT_Nothing) {
22 *this = Other;
23}
24
Samuel Benzaquen7a337af2013-06-04 15:46:22 +000025VariantValue::VariantValue(unsigned Unsigned) : Type(VT_Nothing) {
26 setUnsigned(Unsigned);
Manuel Klimekf7f295f2013-05-14 09:13:00 +000027}
28
29VariantValue::VariantValue(const std::string &String) : Type(VT_Nothing) {
30 setString(String);
31}
32
Samuel Benzaquen7a337af2013-06-04 15:46:22 +000033VariantValue::VariantValue(const DynTypedMatcher &Matcher) : Type(VT_Nothing) {
34 setMatcher(Matcher);
35}
36
Manuel Klimekf7f295f2013-05-14 09:13:00 +000037VariantValue::~VariantValue() { reset(); }
38
39VariantValue &VariantValue::operator=(const VariantValue &Other) {
40 if (this == &Other) return *this;
41 reset();
42 switch (Other.Type) {
Samuel Benzaquen7a337af2013-06-04 15:46:22 +000043 case VT_Unsigned:
44 setUnsigned(Other.getUnsigned());
45 break;
Manuel Klimekf7f295f2013-05-14 09:13:00 +000046 case VT_String:
47 setString(Other.getString());
48 break;
49 case VT_Matcher:
50 setMatcher(Other.getMatcher());
51 break;
52 case VT_Nothing:
53 Type = VT_Nothing;
54 break;
55 }
56 return *this;
57}
58
59void VariantValue::reset() {
60 switch (Type) {
61 case VT_String:
62 delete Value.String;
63 break;
64 case VT_Matcher:
65 delete Value.Matcher;
66 break;
67 // Cases that do nothing.
Samuel Benzaquen7a337af2013-06-04 15:46:22 +000068 case VT_Unsigned:
Manuel Klimekf7f295f2013-05-14 09:13:00 +000069 case VT_Nothing:
70 break;
71 }
72 Type = VT_Nothing;
73}
74
Samuel Benzaquen7a337af2013-06-04 15:46:22 +000075bool VariantValue::isUnsigned() const {
76 return Type == VT_Unsigned;
77}
78
79unsigned VariantValue::getUnsigned() const {
80 assert(isUnsigned());
81 return Value.Unsigned;
82}
83
84void VariantValue::setUnsigned(unsigned NewValue) {
85 reset();
86 Type = VT_Unsigned;
87 Value.Unsigned = NewValue;
88}
89
Manuel Klimekf7f295f2013-05-14 09:13:00 +000090bool VariantValue::isString() const {
91 return Type == VT_String;
92}
93
94const std::string &VariantValue::getString() const {
95 assert(isString());
96 return *Value.String;
97}
98
99void VariantValue::setString(const std::string &NewValue) {
100 reset();
101 Type = VT_String;
102 Value.String = new std::string(NewValue);
103}
104
105bool VariantValue::isMatcher() const {
106 return Type == VT_Matcher;
107}
108
109const DynTypedMatcher &VariantValue::getMatcher() const {
110 assert(isMatcher());
111 return *Value.Matcher;
112}
113
114void VariantValue::setMatcher(const DynTypedMatcher &NewValue) {
115 reset();
116 Type = VT_Matcher;
117 Value.Matcher = NewValue.clone();
118}
119
120void VariantValue::takeMatcher(DynTypedMatcher *NewValue) {
121 reset();
122 Type = VT_Matcher;
123 Value.Matcher = NewValue;
124}
125
126} // end namespace dynamic
127} // end namespace ast_matchers
128} // end namespace clang