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