blob: 912858f5485d6895da651e45eb5e678fa7cfb723 [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
23VariantValue::VariantValue(const VariantValue &Other) : Type(VT_Nothing) {
24 *this = Other;
25}
26
Samuel Benzaquen7a337af2013-06-04 15:46:22 +000027VariantValue::VariantValue(unsigned Unsigned) : Type(VT_Nothing) {
28 setUnsigned(Unsigned);
Manuel Klimekf7f295f2013-05-14 09:13:00 +000029}
30
31VariantValue::VariantValue(const std::string &String) : Type(VT_Nothing) {
32 setString(String);
33}
34
Samuel Benzaquen7a337af2013-06-04 15:46:22 +000035VariantValue::VariantValue(const DynTypedMatcher &Matcher) : Type(VT_Nothing) {
36 setMatcher(Matcher);
37}
38
Manuel Klimekf7f295f2013-05-14 09:13:00 +000039VariantValue::~VariantValue() { reset(); }
40
41VariantValue &VariantValue::operator=(const VariantValue &Other) {
42 if (this == &Other) return *this;
43 reset();
44 switch (Other.Type) {
Samuel Benzaquen7a337af2013-06-04 15:46:22 +000045 case VT_Unsigned:
46 setUnsigned(Other.getUnsigned());
47 break;
Manuel Klimekf7f295f2013-05-14 09:13:00 +000048 case VT_String:
49 setString(Other.getString());
50 break;
51 case VT_Matcher:
52 setMatcher(Other.getMatcher());
53 break;
54 case VT_Nothing:
55 Type = VT_Nothing;
56 break;
57 }
58 return *this;
59}
60
61void VariantValue::reset() {
62 switch (Type) {
63 case VT_String:
64 delete Value.String;
65 break;
66 case VT_Matcher:
67 delete Value.Matcher;
68 break;
69 // Cases that do nothing.
Samuel Benzaquen7a337af2013-06-04 15:46:22 +000070 case VT_Unsigned:
Manuel Klimekf7f295f2013-05-14 09:13:00 +000071 case VT_Nothing:
72 break;
73 }
74 Type = VT_Nothing;
75}
76
Samuel Benzaquen7a337af2013-06-04 15:46:22 +000077bool VariantValue::isUnsigned() const {
78 return Type == VT_Unsigned;
79}
80
81unsigned VariantValue::getUnsigned() const {
82 assert(isUnsigned());
83 return Value.Unsigned;
84}
85
86void VariantValue::setUnsigned(unsigned NewValue) {
87 reset();
88 Type = VT_Unsigned;
89 Value.Unsigned = NewValue;
90}
91
Manuel Klimekf7f295f2013-05-14 09:13:00 +000092bool VariantValue::isString() const {
93 return Type == VT_String;
94}
95
96const std::string &VariantValue::getString() const {
97 assert(isString());
98 return *Value.String;
99}
100
101void VariantValue::setString(const std::string &NewValue) {
102 reset();
103 Type = VT_String;
104 Value.String = new std::string(NewValue);
105}
106
107bool VariantValue::isMatcher() const {
108 return Type == VT_Matcher;
109}
110
111const DynTypedMatcher &VariantValue::getMatcher() const {
112 assert(isMatcher());
113 return *Value.Matcher;
114}
115
116void VariantValue::setMatcher(const DynTypedMatcher &NewValue) {
117 reset();
118 Type = VT_Matcher;
119 Value.Matcher = NewValue.clone();
120}
121
122void VariantValue::takeMatcher(DynTypedMatcher *NewValue) {
123 reset();
124 Type = VT_Matcher;
125 Value.Matcher = NewValue;
126}
127
Samuel Benzaquen76c2f922013-06-20 14:28:32 +0000128std::string VariantValue::getTypeAsString() const {
129 switch (Type) {
130 case VT_String: return "String";
131 case VT_Matcher:
132 return (Twine("Matcher<") + getMatcher().getSupportedKind().asStringRef() +
133 ">").str();
134 case VT_Unsigned: return "Unsigned";
135 case VT_Nothing: return "Nothing";
136 }
137 llvm_unreachable("Invalid Type");
138}
139
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000140} // end namespace dynamic
141} // end namespace ast_matchers
142} // end namespace clang