MLIR  21.0.0git
VariantValue.h
Go to the documentation of this file.
1 //===--- VariantValue.h -----------------------------------------*- C++ -*-===//
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 // Supports all the types required for dynamic Matcher construction.
10 // Used by the registry to construct matchers in a generic way.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef MLIR_TOOLS_MLIRQUERY_MATCHER_VARIANTVALUE_H
15 #define MLIR_TOOLS_MLIRQUERY_MATCHER_VARIANTVALUE_H
16 
17 #include "ErrorBuilder.h"
18 #include "MatchersInternal.h"
19 #include "llvm/ADT/StringRef.h"
20 
21 namespace mlir::query::matcher {
22 
23 // All types that VariantValue can contain.
24 enum class ArgKind { Boolean, Matcher, Signed, String };
25 
26 // A variant matcher object to abstract simple and complex matchers into a
27 // single object type.
29  class MatcherOps;
30 
31  // Payload interface to be specialized by each matcher type. It follows a
32  // similar interface as VariantMatcher itself.
33  class Payload {
34  public:
35  virtual ~Payload();
36  virtual std::optional<DynMatcher> getDynMatcher() const = 0;
37  virtual std::string getTypeAsString() const = 0;
38  };
39 
40 public:
41  // A null matcher.
43 
44  // Clones the provided matcher.
45  static VariantMatcher SingleMatcher(DynMatcher matcher);
46 
47  // Makes the matcher the "null" matcher.
48  void reset();
49 
50  // Checks if the matcher is null.
51  bool isNull() const { return !value; }
52 
53  // Returns the matcher
54  std::optional<DynMatcher> getDynMatcher() const;
55 
56  // String representation of the type of the value.
57  std::string getTypeAsString() const;
58 
59 private:
60  explicit VariantMatcher(std::shared_ptr<Payload> value)
61  : value(std::move(value)) {}
62 
63  class SinglePayload;
64 
65  std::shared_ptr<const Payload> value;
66 };
67 
68 // Variant value class with a tagged union with value type semantics. It is used
69 // by the registry as the return value and argument type for the matcher factory
70 // methods. It can be constructed from any of the supported types:
71 // - StringRef
72 // - VariantMatcher
73 class VariantValue {
74 public:
75  VariantValue() : type(ValueType::Nothing) {}
76 
77  VariantValue(const VariantValue &other);
78  ~VariantValue();
79  VariantValue &operator=(const VariantValue &other);
80 
81  // Specific constructors for each supported type.
82  VariantValue(const llvm::StringRef string);
83  VariantValue(const VariantMatcher &matcher);
84  VariantValue(int64_t signedValue);
86 
87  // String value functions.
88  bool isString() const;
89  const llvm::StringRef &getString() const;
90  void setString(const llvm::StringRef &string);
91 
92  // Matcher value functions.
93  bool isMatcher() const;
94  const VariantMatcher &getMatcher() const;
95  void setMatcher(const VariantMatcher &matcher);
96 
97  // Signed value functions.
98  bool isSigned() const;
99  int64_t getSigned() const;
100  void setSigned(int64_t signedValue);
101 
102  // Boolean value functions.
103  bool isBoolean() const;
104  bool getBoolean() const;
105  void setBoolean(bool booleanValue);
106  // String representation of the type of the value.
107  std::string getTypeAsString() const;
108  explicit operator bool() const { return hasValue(); }
109  bool hasValue() const { return type != ValueType::Nothing; }
110 
111 private:
112  void reset();
113 
114  // All supported value types.
115  enum class ValueType {
116  Boolean,
117  Matcher,
118  Nothing,
119  Signed,
120  String,
121  };
122 
123  // All supported value types.
124  union AllValues {
125  bool Boolean;
126  int64_t Signed;
127  llvm::StringRef *String;
128  VariantMatcher *Matcher;
129  };
130 
131  ValueType type;
132  AllValues value;
133 };
134 
135 // A VariantValue instance annotated with its parser context.
136 struct ParserValue {
138  llvm::StringRef text;
141 };
142 
143 } // namespace mlir::query::matcher
144 
145 #endif // MLIR_TOOLS_MLIRQUERY_MATCHER_VARIANTVALUE_H
static VariantMatcher SingleMatcher(DynMatcher matcher)
std::optional< DynMatcher > getDynMatcher() const
VariantValue & operator=(const VariantValue &other)
const VariantMatcher & getMatcher() const
void setMatcher(const VariantMatcher &matcher)
const llvm::StringRef & getString() const
void setSigned(int64_t signedValue)
void setString(const llvm::StringRef &string)
void setBoolean(bool booleanValue)
A matcher encapsulating getBackwardSlice method from SliceAnalysis.h.
Definition: ErrorBuilder.h:20
@ String
A string value.
internal::SourceRange range
Definition: VariantValue.h:139