1 | //===--- Variantvalue.cpp -------------------------------------------------===// |
---|---|
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // |
10 | //===----------------------------------------------------------------------===// |
11 | |
12 | #include "mlir/Query/Matcher/VariantValue.h" |
13 | |
14 | namespace mlir::query::matcher { |
15 | |
16 | VariantMatcher::Payload::~Payload() = default; |
17 | |
18 | class VariantMatcher::SinglePayload : public VariantMatcher::Payload { |
19 | public: |
20 | explicit SinglePayload(DynMatcher matcher) : matcher(std::move(matcher)) {} |
21 | |
22 | std::optional<DynMatcher> getDynMatcher() const override { return matcher; } |
23 | |
24 | std::string getTypeAsString() const override { return "Matcher"; } |
25 | |
26 | private: |
27 | DynMatcher matcher; |
28 | }; |
29 | |
30 | VariantMatcher::VariantMatcher() = default; |
31 | |
32 | VariantMatcher VariantMatcher::SingleMatcher(DynMatcher matcher) { |
33 | return VariantMatcher(std::make_shared<SinglePayload>(args: std::move(matcher))); |
34 | } |
35 | |
36 | std::optional<DynMatcher> VariantMatcher::getDynMatcher() const { |
37 | return value ? value->getDynMatcher() : std::nullopt; |
38 | } |
39 | |
40 | void VariantMatcher::reset() { value.reset(); } |
41 | |
42 | std::string VariantMatcher::getTypeAsString() const { return "<Nothing>"; } |
43 | |
44 | VariantValue::VariantValue(const VariantValue &other) |
45 | : type(ValueType::Nothing) { |
46 | *this = other; |
47 | } |
48 | |
49 | VariantValue::VariantValue(const llvm::StringRef string) |
50 | : type(ValueType::String) { |
51 | value.String = new llvm::StringRef(string); |
52 | } |
53 | |
54 | VariantValue::VariantValue(const VariantMatcher &matcher) |
55 | : type(ValueType::Matcher) { |
56 | value.Matcher = new VariantMatcher(matcher); |
57 | } |
58 | |
59 | VariantValue::~VariantValue() { reset(); } |
60 | |
61 | VariantValue &VariantValue::operator=(const VariantValue &other) { |
62 | if (this == &other) |
63 | return *this; |
64 | reset(); |
65 | switch (other.type) { |
66 | case ValueType::String: |
67 | setString(other.getString()); |
68 | break; |
69 | case ValueType::Matcher: |
70 | setMatcher(other.getMatcher()); |
71 | break; |
72 | case ValueType::Nothing: |
73 | type = ValueType::Nothing; |
74 | break; |
75 | } |
76 | return *this; |
77 | } |
78 | |
79 | void VariantValue::reset() { |
80 | switch (type) { |
81 | case ValueType::String: |
82 | delete value.String; |
83 | break; |
84 | case ValueType::Matcher: |
85 | delete value.Matcher; |
86 | break; |
87 | // Cases that do nothing. |
88 | case ValueType::Nothing: |
89 | break; |
90 | } |
91 | type = ValueType::Nothing; |
92 | } |
93 | |
94 | bool VariantValue::isString() const { return type == ValueType::String; } |
95 | |
96 | const llvm::StringRef &VariantValue::getString() const { |
97 | assert(isString()); |
98 | return *value.String; |
99 | } |
100 | |
101 | void VariantValue::setString(const llvm::StringRef &newValue) { |
102 | reset(); |
103 | type = ValueType::String; |
104 | value.String = new llvm::StringRef(newValue); |
105 | } |
106 | |
107 | bool VariantValue::isMatcher() const { return type == ValueType::Matcher; } |
108 | |
109 | const VariantMatcher &VariantValue::getMatcher() const { |
110 | assert(isMatcher()); |
111 | return *value.Matcher; |
112 | } |
113 | |
114 | void VariantValue::setMatcher(const VariantMatcher &newValue) { |
115 | reset(); |
116 | type = ValueType::Matcher; |
117 | value.Matcher = new VariantMatcher(newValue); |
118 | } |
119 | |
120 | std::string VariantValue::getTypeAsString() const { |
121 | switch (type) { |
122 | case ValueType::String: |
123 | return "String"; |
124 | case ValueType::Matcher: |
125 | return "Matcher"; |
126 | case ValueType::Nothing: |
127 | return "Nothing"; |
128 | } |
129 | llvm_unreachable("Invalid Type"); |
130 | } |
131 | |
132 | } // namespace mlir::query::matcher |
133 |