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 <variant>
17 
18 // Boilerplate CRTP class for a simplified type-casing syntactic sugar. This
19 // lets one write pattern matchers using a more compact syntax.
20 namespace fir::details {
21 // clang-format off
22 template<class... Ts> struct matches : Ts... { using Ts::operator()...; };
23 template<class... Ts> matches(Ts...) -> matches<Ts...>;
24 template<typename N> struct matcher {
matchmatcher25   template<typename... Ts> auto match(Ts... ts) {
26     return std::visit(matches{ts...}, static_cast<N*>(this)->matchee());
27   }
matchmatcher28   template<typename... Ts> auto match(Ts... ts) const {
29     return std::visit(matches{ts...}, static_cast<N const*>(this)->matchee());
30   }
31 };
32 // clang-format on
33 } // namespace fir::details
34 
35 #endif // FORTRAN_OPTIMIZER_SUPPORT_MATCHER_H
36