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 
17 #include "clang/Basic/LLVM.h"
18 
19 namespace clang {
20 namespace ast_matchers {
21 namespace dynamic {
22 
23 VariantValue::VariantValue(const VariantValue &Other) : Type(VT_Nothing) {
24   *this = Other;
25 }
26 
27 VariantValue::VariantValue(unsigned Unsigned) : Type(VT_Nothing) {
28   setUnsigned(Unsigned);
29 }
30 
31 VariantValue::VariantValue(const std::string &String) : Type(VT_Nothing) {
32   setString(String);
33 }
34 
35 VariantValue::VariantValue(const DynTypedMatcher &Matcher) : Type(VT_Nothing) {
36   setMatcher(Matcher);
37 }
38 
39 VariantValue::~VariantValue() { reset(); }
40 
41 VariantValue &VariantValue::operator=(const VariantValue &Other) {
42   if (this == &Other) return *this;
43   reset();
44   switch (Other.Type) {
45   case VT_Unsigned:
46     setUnsigned(Other.getUnsigned());
47     break;
48   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 
61 void 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.
70   case VT_Unsigned:
71   case VT_Nothing:
72     break;
73   }
74   Type = VT_Nothing;
75 }
76 
77 bool VariantValue::isUnsigned() const {
78   return Type == VT_Unsigned;
79 }
80 
81 unsigned VariantValue::getUnsigned() const {
82   assert(isUnsigned());
83   return Value.Unsigned;
84 }
85 
86 void VariantValue::setUnsigned(unsigned NewValue) {
87   reset();
88   Type = VT_Unsigned;
89   Value.Unsigned = NewValue;
90 }
91 
92 bool VariantValue::isString() const {
93   return Type == VT_String;
94 }
95 
96 const std::string &VariantValue::getString() const {
97   assert(isString());
98   return *Value.String;
99 }
100 
101 void VariantValue::setString(const std::string &NewValue) {
102   reset();
103   Type = VT_String;
104   Value.String = new std::string(NewValue);
105 }
106 
107 bool VariantValue::isMatcher() const {
108   return Type == VT_Matcher;
109 }
110 
111 const DynTypedMatcher &VariantValue::getMatcher() const {
112   assert(isMatcher());
113   return *Value.Matcher;
114 }
115 
116 void VariantValue::setMatcher(const DynTypedMatcher &NewValue) {
117   reset();
118   Type = VT_Matcher;
119   Value.Matcher = NewValue.clone();
120 }
121 
122 void VariantValue::takeMatcher(DynTypedMatcher *NewValue) {
123   reset();
124   Type = VT_Matcher;
125   Value.Matcher = NewValue;
126 }
127 
128 std::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 
140 } // end namespace dynamic
141 } // end namespace ast_matchers
142 } // end namespace clang
143