FLANG
match.h
1//===-- include/flang/Evaluate/match.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#ifndef FORTRAN_EVALUATE_MATCH_H_
9#define FORTRAN_EVALUATE_MATCH_H_
10
11#include "flang/Common/Fortran-consts.h"
12#include "flang/Common/visit.h"
13#include "flang/Evaluate/expression.h"
14#include "flang/Support/Fortran.h"
15#include "llvm/ADT/STLExtras.h"
16
17#include <tuple>
18#include <type_traits>
19#include <utility>
20#include <variant>
21
22namespace Fortran::evaluate {
23namespace match {
24namespace detail {
25template <typename, typename = void> //
27 static constexpr bool value{false};
28};
29
30template <typename T>
31struct IsOperation<T, std::void_t<decltype(T::operands)>> {
32 static constexpr bool value{true};
33};
34} // namespace detail
35
36template <typename T>
37constexpr bool is_operation_v{detail::IsOperation<T>::value};
38
39template <common::TypeCategory C>
40const evaluate::Expr<Type<C>> &deparen(const evaluate::Expr<Type<C>> &x) {
41 if (auto *parens{std::get_if<Parentheses<Type<C>>>(&x.u)}) {
42 return deparen(parens->template operand<0>());
43 } else {
44 return x;
45 }
46}
47
48// Some expressions (e.g. TypelessExpression) don't allow parentheses, while
49// those that do have Expr<Type> as the argument to the parentheses. This means
50// that there is no consistent return type that works for all expressions.
51// Delete this overload explicitly so an attempt to use it creates a clearer
52// error message.
53const evaluate::Expr<SomeType> &deparen(
54 const evaluate::Expr<SomeType> &) = delete;
55
56// Expr<T> matchers (patterns)
57//
58// Each pattern should implement
59// bool match(const U &input) const
60// member function that returns `true` when the match was successful,
61// and `false` otherwise.
62//
63// Patterns are intended to be composable, i.e. a pattern can take operands
64// which themselves are patterns. This composition is expected to match if
65// the root pattern and all its operands match given input.
66
69template <typename T> struct TypePattern {
70 using MatchType = llvm::remove_cvref_t<T>;
71
72 template <typename U> bool match(const U &input) const {
73 if constexpr (std::is_same_v<MatchType, U>) {
74 ref = &input;
75 return true;
76 } else {
77 return false;
78 }
79 }
80
81 mutable const MatchType *ref{nullptr};
82};
83
92template <typename... Patterns> struct AnyOfPattern {
93 static_assert(sizeof...(Patterns) != 0);
94
95private:
96 using PatternTuple = std::tuple<Patterns...>;
97
98 template <size_t I>
99 using Pattern = typename std::tuple_element<I, PatternTuple>::type;
100
101 template <size_t... Is, typename... Ops>
102 AnyOfPattern(std::index_sequence<Is...>, const Ops &...ops)
103 : patterns(std::make_tuple(Pattern<Is>(ops...)...)) {}
104
105 template <typename P, typename U>
106 bool matchOne(const P &pattern, const U &input) const {
107 if (pattern.match(input)) {
108 ref = &pattern;
109 return true;
110 }
111 return false;
112 }
113
114 template <typename U, size_t... Is>
115 bool matchImpl(const U &input, std::index_sequence<Is...>) const {
116 return (matchOne(std::get<Is>(patterns), input) || ...);
117 }
118
119 PatternTuple patterns;
120
121public:
122 using Indexes = std::index_sequence_for<Patterns...>;
123 using MatchTypes = std::tuple<typename Patterns::MatchType...>;
124
125 template <typename... Ops>
126 AnyOfPattern(const Ops &...ops) : AnyOfPattern(Indexes{}, ops...) {}
127
128 template <typename U> bool match(const U &input) const {
129 return matchImpl(input, Indexes{});
130 }
131
132 mutable std::variant<const Patterns *..., std::monostate> ref{
133 std::monostate{}};
134};
135
138template <typename T> //
139struct ExprPattern : public TypePattern<evaluate::Expr<T>> {};
140
142template <typename OpType, typename... Ops>
143struct OperationPattern : public TypePattern<OpType> {
144private:
145 using Indexes = std::index_sequence_for<Ops...>;
146
147 template <typename S, size_t... Is>
148 bool matchImpl(const S &op, std::index_sequence<Is...>) const {
149 using TypeS = llvm::remove_cvref_t<S>;
150 if constexpr (is_operation_v<TypeS>) {
151 if constexpr (TypeS::operands == Indexes::size()) {
152 return TypePattern<OpType>::match(op) &&
153 (std::get<Is>(operands).match(op.template operand<Is>()) && ...);
154 }
155 }
156 return false;
157 }
158
159 std::tuple<const Ops &...> operands;
160
161public:
162 using MatchType = OpType;
163
164 OperationPattern(const Ops &...ops, llvm::type_identity<OpType> = {})
165 : operands(ops...) {}
166
167 template <typename T> bool match(const evaluate::Expr<T> &input) const {
168 return common::visit(
169 [&](auto &&s) { return matchImpl(s, Indexes{}); }, deparen(input).u);
170 }
171
172 template <typename U> bool match(const U &input) const {
173 // Only match Expr<T>
174 return false;
175 }
176};
177
178template <typename OpType, typename... Ops>
179OperationPattern(const Ops &..., llvm::type_identity<OpType>)
180 -> OperationPattern<OpType, Ops...>;
181
182// Encode the actual operator in the type, so that the class is constructible
183// only from operand patterns. This will make it usable in AnyOfPattern.
184template <common::LogicalOperator Operator, typename ValType, typename... Ops>
185struct LogicalOperationPattern
186 : public OperationPattern<LogicalOperation, Ops...> {
187 using Base = OperationPattern<LogicalOperation, Ops...>;
188 static constexpr common::LogicalOperator opCode{Operator};
189
190private:
191 bool matchOp(const LogicalOperation &op) const {
192 return op.logicalOperator == opCode;
193 }
194 template <typename U> bool matchOp(const U &) const { return false; }
195
196public:
197 LogicalOperationPattern(const Ops &...ops, llvm::type_identity<ValType> = {})
198 : Base(ops...) {}
199
200 template <typename T> bool match(const evaluate::Expr<T> &input) const {
201 // The kind of a LogicalOperation is only known at runtime and always
202 // equals the kind of its enclosing Expr, so ValType can only be checked
203 // against the result type of the input expression.
204 if constexpr (std::is_same_v<T, ValType>) {
205 // All logical operations have the same operation type
206 // (LogicalOperation), so type-based matching cannot tell specific
207 // operations from one another.
208 // Check the operation code first, if that matches then use the
209 // base class's match.
210 if (common::visit(
211 [&](auto &&s) { return matchOp(s); }, deparen(input).u)) {
212 return Base::match(input);
213 }
214 }
215 return false;
216 }
217
218 template <typename U> bool match(const U &input) const { //
219 return false;
220 }
221};
222
223// No deduction guide for LogicalOperationPattern, since the "Operator"
224// parameter cannot be deduced from the constructor arguments.
225
226// Namespace-level definitions
227
228template <typename T> using Expr = ExprPattern<T>;
229
230template <typename OpType, typename... Ops>
231using Op = OperationPattern<OpType, Ops...>;
232
233template <common::LogicalOperator Operator, typename ValType, typename... Ops>
234using LogicalOp = LogicalOperationPattern<Operator, ValType, Ops...>;
235
236template <common::LogicalOperator Operator, typename Type, typename Op0,
237 typename Op1>
238LogicalOp<Operator, Type, Op0, Op1> logical(const Op0 &op0, const Op1 &op1) {
239 return LogicalOp<Operator, Type, Op0, Op1>(op0, op1);
240}
241
242template <typename Pattern, typename Input>
243bool match(const Pattern &pattern, const Input &input) {
244 return pattern.match(input);
245}
246
247// Specific operation patterns
248
249// -- Add
250template <typename Type, typename Op0, typename Op1>
251struct Add : public Op<evaluate::Add<Type>, Op0, Op1> {
252 using Base = Op<evaluate::Add<Type>, Op0, Op1>;
253
254 Add(const Op0 &op0, const Op1 &op1) : Base(op0, op1) {}
255};
256
257template <typename Type, typename Op0, typename Op1>
258Add<Type, Op0, Op1> add(const Op0 &op0, const Op1 &op1) {
259 return Add<Type, Op0, Op1>(op0, op1);
260}
261
262// -- Mul
263template <typename Type, typename Op0, typename Op1>
264struct Mul : public Op<evaluate::Multiply<Type>, Op0, Op1> {
265 using Base = Op<evaluate::Multiply<Type>, Op0, Op1>;
266
267 Mul(const Op0 &op0, const Op1 &op1) : Base(op0, op1) {}
268};
269
270template <typename Type, typename Op0, typename Op1>
271Mul<Type, Op0, Op1> mul(const Op0 &op0, const Op1 &op1) {
272 return Mul<Type, Op0, Op1>(op0, op1);
273}
274} // namespace match
275} // namespace Fortran::evaluate
276
277#endif // FORTRAN_EVALUATE_MATCH_H_
Definition common.h:222
Definition type.h:61
Definition call.h:34
Definition expression.h:401
Definition match.h:251
Definition match.h:264
Matches evaluate::Expr<T> that contains evaluate::Operation<OpType>.
Definition match.h:143