liuxiaolong
2021-07-20 58d904a328c0d849769b483e901a0be9426b8209
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// Copyright (C) 2016-2018 T. Zachary Laine
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_YAP_PRINT_HPP_INCLUDED
#define BOOST_YAP_PRINT_HPP_INCLUDED
 
#include <boost/yap/algorithm_fwd.hpp>
 
#include <boost/hana/for_each.hpp>
#include <boost/type_index.hpp>
#include <iostream>
 
 
namespace boost { namespace yap {
 
    /** Returns the <code>char const *</code> string for the spelling of the
        C++ operator associated with \a kind.  It returns the special values
        "ref" and "term" for the non-operator kinds
        <code>expr_kind::expr_ref</code> amd <code>expr_kind::terminal</code>,
        respectively.*/
    inline constexpr char const * op_string(expr_kind kind)
    {
        switch (kind) {
        case expr_kind::expr_ref: return "ref";
 
        case expr_kind::terminal: return "term";
 
        case expr_kind::unary_plus: return "+";
        case expr_kind::negate: return "-";
        case expr_kind::dereference: return "*";
        case expr_kind::complement: return "~";
        case expr_kind::address_of: return "&";
        case expr_kind::logical_not: return "!";
        case expr_kind::pre_inc: return "++";
        case expr_kind::pre_dec: return "--";
        case expr_kind::post_inc: return "++(int)";
        case expr_kind::post_dec: return "--(int)";
 
        case expr_kind::shift_left: return "<<";
        case expr_kind::shift_right: return ">>";
        case expr_kind::multiplies: return "*";
        case expr_kind::divides: return "/";
        case expr_kind::modulus: return "%";
        case expr_kind::plus: return "+";
        case expr_kind::minus: return "-";
        case expr_kind::less: return "<";
        case expr_kind::greater: return ">";
        case expr_kind::less_equal: return "<=";
        case expr_kind::greater_equal: return ">=";
        case expr_kind::equal_to: return "==";
        case expr_kind::not_equal_to: return "!=";
        case expr_kind::logical_or: return "||";
        case expr_kind::logical_and: return "&&";
        case expr_kind::bitwise_and: return "&";
        case expr_kind::bitwise_or: return "|";
        case expr_kind::bitwise_xor: return "^";
        case expr_kind::comma: return ",";
        case expr_kind::mem_ptr: return "->*";
        case expr_kind::assign: return "=";
        case expr_kind::shift_left_assign: return "<<=";
        case expr_kind::shift_right_assign: return ">>=";
        case expr_kind::multiplies_assign: return "*=";
        case expr_kind::divides_assign: return "/=";
        case expr_kind::modulus_assign: return "%=";
        case expr_kind::plus_assign: return "+=";
        case expr_kind::minus_assign: return "-=";
        case expr_kind::bitwise_and_assign: return "&=";
        case expr_kind::bitwise_or_assign: return "|=";
        case expr_kind::bitwise_xor_assign: return "^=";
        case expr_kind::subscript: return "[]";
 
        case expr_kind::if_else: return "?:";
 
        case expr_kind::call: return "()";
 
        default: return "** ERROR: UNKNOWN OPERATOR! **";
        }
    }
 
    namespace detail {
 
        inline std::ostream & print_kind(std::ostream & os, expr_kind kind)
        {
            return os << op_string(kind);
        }
 
        template<typename T, typename = void_t<>>
        struct printer
        {
            std::ostream & operator()(std::ostream & os, T const &)
            {
                return os << "<<unprintable-value>>";
            }
        };
 
        template<typename T>
        struct printer<
            T,
            void_t<decltype(
                std::declval<std::ostream &>() << std::declval<T const &>())>>
        {
            std::ostream & operator()(std::ostream & os, T const & x)
            {
                return os << x;
            }
        };
 
        template<typename T>
        inline std::ostream & print_value(std::ostream & os, T const & x)
        {
            return printer<T>{}(os, x);
        }
 
        template<long long I>
        inline std::ostream & print_value(std::ostream & os, hana::llong<I>)
        {
            return os << I << "_p";
        }
 
        template<typename T>
        std::ostream & print_type(std::ostream & os, hana::tuple<T> const &)
        {
            os << typeindex::type_id<T>().pretty_name();
            if (std::is_const<std::remove_reference_t<T>>::value)
                os << " const";
            if (std::is_volatile<std::remove_reference_t<T>>::value)
                os << " volatile";
            if (std::is_lvalue_reference<T>::value)
                os << " &";
            if (std::is_rvalue_reference<T>::value)
                os << " &&";
            return os;
        }
 
        template<typename T>
        bool is_const_expr_ref(T const &)
        {
            return false;
        }
        template<typename T, template<expr_kind, class> class expr_template>
        bool is_const_expr_ref(
            expr_template<expr_kind::expr_ref, hana::tuple<T const *>> const &)
        {
            return true;
        }
 
#ifdef BOOST_NO_CONSTEXPR_IF
 
        template<expr_kind Kind>
        struct print_impl
        {
            template<typename Expr>
            std::ostream & operator()(
                std::ostream & os,
                Expr const & expr,
                int indent,
                char const * indent_str,
                bool is_ref = false,
                bool is_const_ref = false)
            {
                for (int i = 0; i < indent; ++i) {
                    os << indent_str;
                }
 
                os << "expr<";
                ::boost::yap::detail::print_kind(os, Expr::kind);
                os << ">";
                if (is_const_ref)
                    os << " const &";
                else if (is_ref)
                    os << " &";
                os << "\n";
                hana::for_each(
                    expr.elements,
                    [&os, indent, indent_str](auto const & element) {
                        using element_type = decltype(element);
                        constexpr expr_kind kind =
                            detail::remove_cv_ref_t<element_type>::kind;
                        print_impl<kind>{}(os, element, indent + 1, indent_str);
                    });
 
                return os;
            }
        };
 
        template<>
        struct print_impl<expr_kind::expr_ref>
        {
            template<typename Expr>
            std::ostream & operator()(
                std::ostream & os,
                Expr const & expr,
                int indent,
                char const * indent_str,
                bool is_ref = false,
                bool is_const_ref = false)
            {
                using ref_type = decltype(::boost::yap::deref(expr));
                constexpr expr_kind ref_kind =
                    detail::remove_cv_ref_t<ref_type>::kind;
                print_impl<ref_kind>{}(
                    os,
                    ::boost::yap::deref(expr),
                    indent,
                    indent_str,
                    true,
                    ::boost::yap::detail::is_const_expr_ref(expr));
                return os;
            }
        };
 
        template<>
        struct print_impl<expr_kind::terminal>
        {
            template<typename Expr>
            std::ostream & operator()(
                std::ostream & os,
                Expr const & expr,
                int indent,
                char const * indent_str,
                bool is_ref = false,
                bool is_const_ref = false)
            {
                for (int i = 0; i < indent; ++i) {
                    os << indent_str;
                }
 
                os << "term<";
                ::boost::yap::detail::print_type(os, expr.elements);
                os << ">[=";
                ::boost::yap::detail::print_value(
                    os, ::boost::yap::value(expr));
                os << "]";
                if (is_const_ref)
                    os << " const &";
                else if (is_ref)
                    os << " &";
                os << "\n";
 
                return os;
            }
        };
 
#else
 
        template<typename Expr>
        std::ostream & print_impl(
            std::ostream & os,
            Expr const & expr,
            int indent,
            char const * indent_str,
            bool is_ref = false,
            bool is_const_ref = false)
        {
            if constexpr (Expr::kind == expr_kind::expr_ref) {
                print_impl(
                    os,
                    ::boost::yap::deref(expr),
                    indent,
                    indent_str,
                    true,
                    ::boost::yap::detail::is_const_expr_ref(expr));
            } else {
                for (int i = 0; i < indent; ++i) {
                    os << indent_str;
                }
 
                if constexpr (Expr::kind == expr_kind::terminal) {
                    os << "term<";
                    ::boost::yap::detail::print_type(os, expr.elements);
                    os << ">[=";
                    ::boost::yap::detail::print_value(
                        os, ::boost::yap::value(expr));
                    os << "]";
                    if (is_const_ref)
                        os << " const &";
                    else if (is_ref)
                        os << " &";
                    os << "\n";
                } else {
                    os << "expr<";
                    ::boost::yap::detail::print_kind(os, Expr::kind);
                    os << ">";
                    if (is_const_ref)
                        os << " const &";
                    else if (is_ref)
                        os << " &";
                    os << "\n";
                    hana::for_each(
                        expr.elements,
                        [&os, indent, indent_str](auto const & element) {
                            ::boost::yap::detail::print_impl(
                                os, element, indent + 1, indent_str);
                        });
                }
            }
 
            return os;
        }
 
#endif // BOOST_NO_CONSTEXPR_IF
    }
 
    /** Prints expression \a expr to stream \a os.  Returns \a os. */
    template<typename Expr>
    std::ostream & print(std::ostream & os, Expr const & expr)
    {
#ifdef BOOST_NO_CONSTEXPR_IF
        return detail::print_impl<detail::remove_cv_ref_t<Expr>::kind>{}(
            os, expr, 0, "    ");
#else
        return detail::print_impl(os, expr, 0, "    ");
#endif
    }
 
}}
 
#endif