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  public:
31  std::optional<DynMatcher>
32  constructVariadicOperator(DynMatcher::VariadicOperator varOp,
33  ArrayRef<VariantMatcher> innerMatchers) const;
34  };
35 
36  // Payload interface to be specialized by each matcher type. It follows a
37  // similar interface as VariantMatcher itself.
38  class Payload {
39  public:
40  virtual ~Payload();
41  virtual std::optional<DynMatcher> getDynMatcher() const = 0;
42  virtual std::string getTypeAsString() const = 0;
43  };
44 
45 public:
46  // A null matcher.
48 
49  // Clones the provided matcher.
50  static VariantMatcher SingleMatcher(DynMatcher matcher);
51  static VariantMatcher
54 
55  // Makes the matcher the "null" matcher.
56  void reset();
57 
58  // Checks if the matcher is null.
59  bool isNull() const { return !value; }
60 
61  // Returns the matcher
62  std::optional<DynMatcher> getDynMatcher() const;
63 
64  // String representation of the type of the value.
65  std::string getTypeAsString() const;
66 
67 private:
68  explicit VariantMatcher(std::shared_ptr<Payload> value)
69  : value(std::move(value)) {}
70 
71  class SinglePayload;
72  class VariadicOpPayload;
73 
74  std::shared_ptr<const Payload> value;
75 };
76 
77 // Variant value class with a tagged union with value type semantics. It is used
78 // by the registry as the return value and argument type for the matcher factory
79 // methods. It can be constructed from any of the supported types:
80 // - StringRef
81 // - VariantMatcher
82 class VariantValue {
83 public:
84  VariantValue() : type(ValueType::Nothing) {}
85 
86  VariantValue(const VariantValue &other);
87  ~VariantValue();
88  VariantValue &operator=(const VariantValue &other);
89 
90  // Specific constructors for each supported type.
91  VariantValue(const llvm::StringRef string);
92  VariantValue(const VariantMatcher &matcher);
93  VariantValue(int64_t signedValue);
95 
96  // String value functions.
97  bool isString() const;
98  const llvm::StringRef &getString() const;
99  void setString(const llvm::StringRef &string);
100 
101  // Matcher value functions.
102  bool isMatcher() const;
103  const VariantMatcher &getMatcher() const;
104  void setMatcher(const VariantMatcher &matcher);
105 
106  // Signed value functions.
107  bool isSigned() const;
108  int64_t getSigned() const;
109  void setSigned(int64_t signedValue);
110 
111  // Boolean value functions.
112  bool isBoolean() const;
113  bool getBoolean() const;
114  void setBoolean(bool booleanValue);
115  // String representation of the type of the value.
116  std::string getTypeAsString() const;
117  explicit operator bool() const { return hasValue(); }
118  bool hasValue() const { return type != ValueType::Nothing; }
119 
120 private:
121  void reset();
122 
123  // All supported value types.
124  enum class ValueType {
125  Boolean,
126  Matcher,
127  Nothing,
128  Signed,
129  String,
130  };
131 
132  // All supported value types.
133  union AllValues {
134  bool Boolean;
135  int64_t Signed;
136  llvm::StringRef *String;
137  VariantMatcher *Matcher;
138  };
139 
140  ValueType type;
141  AllValues value;
142 };
143 
144 // A VariantValue instance annotated with its parser context.
145 struct ParserValue {
147  llvm::StringRef text;
150 };
151 
152 } // namespace mlir::query::matcher
153 
154 #endif // MLIR_TOOLS_MLIRQUERY_MATCHER_VARIANTVALUE_H
static VariantMatcher SingleMatcher(DynMatcher matcher)
std::optional< DynMatcher > getDynMatcher() const
static VariantMatcher VariadicOperatorMatcher(DynMatcher::VariadicOperator varOp, ArrayRef< VariantMatcher > args)
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)
Computes the backward-slice of all transitive defs reachable from rootOp, if innerMatcher matches.
Definition: ErrorBuilder.h:20
@ String
A string value.
internal::SourceRange range
Definition: VariantValue.h:148