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
14namespace mlir::query::matcher {
15
16VariantMatcher::Payload::~Payload() = default;
17
18class VariantMatcher::SinglePayload : public VariantMatcher::Payload {
19public:
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
26private:
27 DynMatcher matcher;
28};
29
30VariantMatcher::VariantMatcher() = default;
31
32VariantMatcher VariantMatcher::SingleMatcher(DynMatcher matcher) {
33 return VariantMatcher(std::make_shared<SinglePayload>(args: std::move(matcher)));
34}
35
36std::optional<DynMatcher> VariantMatcher::getDynMatcher() const {
37 return value ? value->getDynMatcher() : std::nullopt;
38}
39
40void VariantMatcher::reset() { value.reset(); }
41
42std::string VariantMatcher::getTypeAsString() const { return "<Nothing>"; }
43
44VariantValue::VariantValue(const VariantValue &other)
45 : type(ValueType::Nothing) {
46 *this = other;
47}
48
49VariantValue::VariantValue(const llvm::StringRef string)
50 : type(ValueType::String) {
51 value.String = new llvm::StringRef(string);
52}
53
54VariantValue::VariantValue(const VariantMatcher &matcher)
55 : type(ValueType::Matcher) {
56 value.Matcher = new VariantMatcher(matcher);
57}
58
59VariantValue::~VariantValue() { reset(); }
60
61VariantValue &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
79void 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
94bool VariantValue::isString() const { return type == ValueType::String; }
95
96const llvm::StringRef &VariantValue::getString() const {
97 assert(isString());
98 return *value.String;
99}
100
101void VariantValue::setString(const llvm::StringRef &newValue) {
102 reset();
103 type = ValueType::String;
104 value.String = new llvm::StringRef(newValue);
105}
106
107bool VariantValue::isMatcher() const { return type == ValueType::Matcher; }
108
109const VariantMatcher &VariantValue::getMatcher() const {
110 assert(isMatcher());
111 return *value.Matcher;
112}
113
114void VariantValue::setMatcher(const VariantMatcher &newValue) {
115 reset();
116 type = ValueType::Matcher;
117 value.Matcher = new VariantMatcher(newValue);
118}
119
120std::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

source code of mlir/lib/Query/Matcher/VariantValue.cpp