xref: /llvm-project/flang/include/flang/Optimizer/Support/Matcher.h (revision 77d8cfb3c50e3341d65af1f9e442004bbd77af9b)
1 //===-- Optimizer/Support/Matcher.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 // Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef FORTRAN_OPTIMIZER_SUPPORT_MATCHER_H
14 #define FORTRAN_OPTIMIZER_SUPPORT_MATCHER_H
15 
16 #include "flang/Common/idioms.h"
17 #include <variant>
18 
19 // Boilerplate CRTP class for a simplified type-casing syntactic sugar. This
20 // lets one write pattern matchers using a more compact syntax.
21 namespace fir::details {
22 // clang-format off
23 template<class... Ts> struct matches : Ts... { using Ts::operator()...; };
24 template<class... Ts> matches(Ts...) -> matches<Ts...>;
25 template<typename N> struct matcher {
matchmatcher26   template<typename... Ts> auto match(Ts... ts) {
27     return Fortran::common::visit(matches{ts...}, static_cast<N*>(this)->matchee());
28   }
matchmatcher29   template<typename... Ts> auto match(Ts... ts) const {
30     return Fortran::common::visit(matches{ts...}, static_cast<N const*>(this)->matchee());
31   }
32 };
33 // clang-format on
34 } // namespace fir::details
35 
36 #endif // FORTRAN_OPTIMIZER_SUPPORT_MATCHER_H
37