8#ifndef FORTRAN_EVALUATE_MATCH_H_
9#define FORTRAN_EVALUATE_MATCH_H_
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"
25template <
typename,
typename =
void>
27 static constexpr bool value{
false};
32 static constexpr bool value{
true};
37constexpr bool is_operation_v{detail::IsOperation<T>::value};
39template <common::TypeCategory C>
41 if (
auto *parens{std::get_if<Parentheses<Type<C>>>(&x.u)}) {
42 return deparen(parens->template operand<0>());
53const evaluate::Expr<SomeType> &deparen(
54 const evaluate::Expr<SomeType> &) =
delete;
70 using MatchType = llvm::remove_cvref_t<T>;
72 template <
typename U>
bool match(
const U &input)
const {
73 if constexpr (std::is_same_v<MatchType, U>) {
81 mutable const MatchType *ref{
nullptr};
92template <
typename... Patterns>
struct AnyOfPattern {
93 static_assert(
sizeof...(Patterns) != 0);
96 using PatternTuple = std::tuple<Patterns...>;
99 using Pattern =
typename std::tuple_element<I, PatternTuple>::type;
101 template <
size_t... Is,
typename... Ops>
102 AnyOfPattern(std::index_sequence<Is...>,
const Ops &...ops)
103 : patterns(std::make_tuple(Pattern<Is>(ops...)...)) {}
105 template <
typename P,
typename U>
106 bool matchOne(
const P &pattern,
const U &input)
const {
107 if (pattern.match(input)) {
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) || ...);
119 PatternTuple patterns;
122 using Indexes = std::index_sequence_for<Patterns...>;
123 using MatchTypes = std::tuple<
typename Patterns::MatchType...>;
125 template <
typename... Ops>
126 AnyOfPattern(
const Ops &...ops) : AnyOfPattern(Indexes{}, ops...) {}
128 template <
typename U>
bool match(
const U &input)
const {
129 return matchImpl(input, Indexes{});
132 mutable std::variant<
const Patterns *..., std::monostate> ref{
142template <
typename OpType,
typename... Ops>
145 using Indexes = std::index_sequence_for<Ops...>;
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>()) && ...);
159 std::tuple<
const Ops &...> operands;
162 using MatchType = OpType;
164 OperationPattern(
const Ops &...ops, llvm::type_identity<OpType> = {})
165 : operands(ops...) {}
168 return common::visit(
169 [&](
auto &&s) {
return matchImpl(s, Indexes{}); }, deparen(input).u);
172 template <
typename U>
bool match(
const U &input)
const {
178template <
typename OpType,
typename... Ops>
184template <common::LogicalOperator Operator,
typename ValType,
typename... Ops>
185struct LogicalOperationPattern
186 :
public OperationPattern<LogicalOperation, Ops...> {
188 static constexpr common::LogicalOperator opCode{Operator};
192 return op.logicalOperator == opCode;
194 template <
typename U>
bool matchOp(
const U &)
const {
return false; }
197 LogicalOperationPattern(
const Ops &...ops, llvm::type_identity<ValType> = {})
204 if constexpr (std::is_same_v<T, ValType>) {
211 [&](
auto &&s) {
return matchOp(s); }, deparen(input).u)) {
212 return Base::match(input);
218 template <
typename U>
bool match(
const U &input)
const {
230template <
typename OpType,
typename... Ops>
233template <common::LogicalOperator Operator,
typename ValType,
typename... Ops>
236template <common::LogicalOperator Operator,
typename Type,
typename Op0,
238LogicalOp<Operator, Type, Op0, Op1> logical(
const Op0 &op0,
const Op1 &op1) {
239 return LogicalOp<Operator, Type, Op0, Op1>(op0, op1);
242template <
typename Pattern,
typename Input>
243bool match(
const Pattern &pattern,
const Input &input) {
244 return pattern.match(input);
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>;
254 Add(
const Op0 &op0,
const Op1 &op1) : Base(op0, op1) {}
257template <
typename Type,
typename Op0,
typename Op1>
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>;
267 Mul(
const Op0 &op0,
const Op1 &op1) : Base(op0, op1) {}
270template <
typename Type,
typename Op0,
typename Op1>
Definition expression.h:401
Matches evaluate::Expr<T> that contains evaluate::Operation<OpType>.
Definition match.h:143