Daily bump.
[official-gcc.git] / gcc / rust / util / rust-inline-visitor.h
blobc38724aa3c9a3321acc16e82e442cbed23e505c1
1 // Copyright (C) 2021-2024 Free Software Foundation, Inc.
3 // This file is part of GCC.
5 // GCC is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation; either version 3, or (at your option) any later
8 // version.
10 // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 // for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with GCC; see the file COPYING3. If not see
17 // <http://www.gnu.org/licenses/>.
19 // An improved implementation of the inline visitor.
20 // Original idea from https://members.accu.org/index.php/articles/2021
22 #ifndef RUST_INLINE_VISITOR
23 #define RUST_INLINE_VISITOR
25 namespace Rust {
27 // Wrapper for the target Visitor we're matching against.
28 // Consumes the final nullptr of the _args linked list.
29 template <typename TargetVisitor> struct EmptyVisitor : TargetVisitor
31 EmptyVisitor (std::nullptr_t ptr) {}
33 using TargetVisitor::visit;
36 // Wrapper for a (possibly incomplete) Visitor.
37 template <typename BaseVisitor, typename Args> struct VisitorWrapper
39 // Lambdas are stored in _args as a linked list and passed to the actual
40 // visitor when end_visitor() is called.
41 Args _args;
43 // The actual visitor being created.
44 // Each visitor inherits from the last one and implements one more visit().
45 template <typename T, typename F> struct Visitor : BaseVisitor
47 F _f;
49 Visitor (std::pair<F, Args> &&args)
50 : BaseVisitor (std::move (args.second)), _f (std::move (args.first))
53 using BaseVisitor::visit;
54 virtual void visit (T &t) final override { _f (t); }
57 VisitorWrapper (Args &&args) : _args (std::move (args)) {}
59 // Add another visit() method to the visitor.
60 // _args will be moved over, so don't keep the old wrapper around.
61 template <typename T, typename F>
62 VisitorWrapper<Visitor<T, F>, std::pair<F, Args>> on (F &&f)
64 return VisitorWrapper<Visitor<T, F>, std::pair<F, Args>> (
65 std::make_pair (std::move (f), std::move (_args)));
68 // Returns the finished visitor.
69 // NOTE: The reference implementation has a bug that exposes this method even
70 // when BaseVisitor is still an abstract class. The C++11 standard states that
71 // "An abstract class shall not be used [...] as a function return type". GCC
72 // rejects the buggy code as expected, but Clang accepts the code as long as
73 // the method is not actually called. Maybe this is a bug in Clang?
74 template <typename T = BaseVisitor>
75 typename std::enable_if<std::is_constructible<T, Args>::value, T>::type
76 end_visitor ()
78 return T (std::move (_args));
82 // The entry point.
83 template <typename TargetVisitor>
84 VisitorWrapper<EmptyVisitor<TargetVisitor>, std::nullptr_t>
85 begin_visitor ()
87 return VisitorWrapper<EmptyVisitor<TargetVisitor>, std::nullptr_t> (nullptr);
90 } // namespace Rust
92 #endif