Skip to content

Commit 935aff5

Browse files
bors[bot]yizhepku
andauthored
Merge #369
369: Implement the inline visitor r=philberty a=YizhePKU This is a clean implementation of the inline visitor. Should be useful for various cleanups such as #366. Co-authored-by: YizhePKU <[email protected]>
2 parents ce593f0 + f5f32fb commit 935aff5

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

gcc/rust/util/rust-inline-visitor.h

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Copyright (C) 2021 Free Software Foundation, Inc.
2+
3+
// This file is part of GCC.
4+
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.
9+
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.
14+
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/>.
18+
19+
// An improved implementation of the inline visitor.
20+
// Original idea from https://members.accu.org/index.php/articles/2021
21+
22+
#ifndef RUST_INLINE_VISITOR
23+
#define RUST_INLINE_VISITOR
24+
25+
#include <utility>
26+
#include <type_traits>
27+
28+
namespace Rust {
29+
30+
// Wrapper for the target Visitor we're matching against.
31+
// Consumes the final nullptr of the _args linked list.
32+
template <typename TargetVisitor> struct EmptyVisitor : TargetVisitor
33+
{
34+
EmptyVisitor (std::nullptr_t ptr) {}
35+
36+
using TargetVisitor::visit;
37+
};
38+
39+
// Wrapper for a (possibly incomplete) Visitor.
40+
template <typename BaseVisitor, typename Args> struct VisitorWrapper
41+
{
42+
// Lambdas are stored in _args as a linked list and passed to the actual
43+
// visitor when end_visitor() is called.
44+
Args _args;
45+
46+
// The actual visitor being created.
47+
// Each visitor inherits from the last one and implements one more visit().
48+
template <typename T, typename F> struct Visitor : BaseVisitor
49+
{
50+
F _f;
51+
52+
Visitor (std::pair<F, Args> &&args)
53+
: BaseVisitor (std::move (args.second)), _f (std::move (args.first))
54+
{}
55+
56+
using BaseVisitor::visit;
57+
virtual void visit (T &t) final override { _f (t); }
58+
};
59+
60+
VisitorWrapper (Args &&args) : _args (std::move (args)) {}
61+
62+
// Add another visit() method to the visitor.
63+
// _args will be moved over, so don't keep the old wrapper around.
64+
template <typename T, typename F>
65+
VisitorWrapper<Visitor<T, F>, std::pair<F, Args>> on (F &&f)
66+
{
67+
return VisitorWrapper<Visitor<T, F>, std::pair<F, Args>> (
68+
std::make_pair (std::move (f), std::move (_args)));
69+
}
70+
71+
// Returns the finished visitor.
72+
// NOTE: The reference implementation has a bug that exposes this method even
73+
// when BaseVisitor is still an abstract class. The C++11 standard states that
74+
// "An abstract class shall not be used [...] as a function return type". GCC
75+
// rejects the buggy code as expected, but Clang accepts the code as long as
76+
// the method is not actually called. Maybe this is a bug in Clang?
77+
template <typename T = BaseVisitor>
78+
typename std::enable_if<std::is_constructible<T, Args>::value, T>::type
79+
end_visitor ()
80+
{
81+
return T (std::move (_args));
82+
}
83+
};
84+
85+
// The entry point.
86+
template <typename TargetVisitor>
87+
VisitorWrapper<EmptyVisitor<TargetVisitor>, std::nullptr_t>
88+
begin_visitor ()
89+
{
90+
return VisitorWrapper<EmptyVisitor<TargetVisitor>, std::nullptr_t> (nullptr);
91+
}
92+
93+
} // namespace Rust
94+
95+
#endif

0 commit comments

Comments
 (0)