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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
// 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_DETAIL_EXPRESSION_HPP_INCLUDED
#define BOOST_YAP_DETAIL_EXPRESSION_HPP_INCLUDED
 
#include <boost/yap/algorithm_fwd.hpp>
 
#include <boost/hana/size.hpp>
#include <boost/hana/tuple.hpp>
 
#include <memory>
#include <type_traits>
 
 
namespace boost { namespace yap { namespace detail {
 
    // static_const
 
    template<typename T>
    struct static_const
    {
        static constexpr T value{};
    };
 
    template<typename T>
    constexpr T static_const<T>::value;
 
 
    // partial_decay
 
    template<typename T>
    struct partial_decay
    {
        using type = T;
    };
 
    template<typename T>
    struct partial_decay<T[]>
    {
        using type = T *;
    };
    template<typename T, std::size_t N>
    struct partial_decay<T[N]>
    {
        using type = T *;
    };
 
    template<typename T>
    struct partial_decay<T (&)[]>
    {
        using type = T *;
    };
    template<typename T, std::size_t N>
    struct partial_decay<T (&)[N]>
    {
        using type = T *;
    };
 
    template<typename R, typename... A>
    struct partial_decay<R(A...)>
    {
        using type = R (*)(A...);
    };
    template<typename R, typename... A>
    struct partial_decay<R(A..., ...)>
    {
        using type = R (*)(A..., ...);
    };
 
    template<typename R, typename... A>
    struct partial_decay<R (&)(A...)>
    {
        using type = R (*)(A...);
    };
    template<typename R, typename... A>
    struct partial_decay<R (&)(A..., ...)>
    {
        using type = R (*)(A..., ...);
    };
 
    template<typename R, typename... A>
    struct partial_decay<R (*&)(A...)>
    {
        using type = R (*)(A...);
    };
    template<typename R, typename... A>
    struct partial_decay<R (*&)(A..., ...)>
    {
        using type = R (*)(A..., ...);
    };
 
 
    // operand_value_type_phase_1
 
    template<
        typename T,
        typename U = typename detail::partial_decay<T>::type,
        bool AddRValueRef = std::is_same<T, U>::value && !std::is_const<U>::value>
    struct operand_value_type_phase_1;
 
    template<typename T, typename U>
    struct operand_value_type_phase_1<T, U, true>
    {
        using type = U &&;
    };
 
    template<typename T, typename U>
    struct operand_value_type_phase_1<T, U, false>
    {
        using type = U;
    };
 
 
    // expr_ref
 
    template<template<expr_kind, class> class ExprTemplate, typename T>
    struct expr_ref
    {
        using type = expression_ref<ExprTemplate, T>;
    };
 
    template<template<expr_kind, class> class ExprTemplate, typename Tuple>
    struct expr_ref<ExprTemplate, ExprTemplate<expr_kind::expr_ref, Tuple> &>
    {
        using type = ExprTemplate<expr_kind::expr_ref, Tuple>;
    };
 
    template<template<expr_kind, class> class ExprTemplate, typename Tuple>
    struct expr_ref<
        ExprTemplate,
        ExprTemplate<expr_kind::expr_ref, Tuple> const &>
    {
        using type = ExprTemplate<expr_kind::expr_ref, Tuple>;
    };
 
    template<template<expr_kind, class> class ExprTemplate, typename T>
    using expr_ref_t = typename expr_ref<ExprTemplate, T>::type;
 
    template<template<expr_kind, class> class ExprTemplate, typename T>
    struct expr_ref_tuple;
 
    template<template<expr_kind, class> class ExprTemplate, typename Tuple>
    struct expr_ref_tuple<
        ExprTemplate,
        ExprTemplate<expr_kind::expr_ref, Tuple>>
    {
        using type = Tuple;
    };
 
    template<template<expr_kind, class> class ExprTemplate, typename T>
    using expr_ref_tuple_t = typename expr_ref_tuple<ExprTemplate, T>::type;
 
 
    // operand_type
 
    template<
        template<expr_kind, class> class ExprTemplate,
        typename T,
        typename U = typename operand_value_type_phase_1<T>::type,
        bool RemoveRefs = std::is_rvalue_reference<U>::value,
        bool IsExpr = is_expr<T>::value,
        bool IsLRef = std::is_lvalue_reference<T>::value>
    struct operand_type;
 
    template<
        template<expr_kind, class> class ExprTemplate,
        typename T,
        typename U,
        bool RemoveRefs>
    struct operand_type<ExprTemplate, T, U, RemoveRefs, true, false>
    {
        using type = remove_cv_ref_t<T>;
    };
 
    template<
        template<expr_kind, class> class ExprTemplate,
        typename T,
        typename U,
        bool RemoveRefs>
    struct operand_type<ExprTemplate, T, U, RemoveRefs, true, true>
    {
        using type = expr_ref_t<ExprTemplate, T>;
    };
 
    template<
        template<expr_kind, class> class ExprTemplate,
        typename T,
        typename U,
        bool RemoveRefs,
        bool IsLRef>
    struct operand_type<ExprTemplate, T, U, RemoveRefs, true, IsLRef>
    {
        using type = remove_cv_ref_t<T>;
    };
 
    template<
        template<expr_kind, class> class ExprTemplate,
        typename T,
        typename U,
        bool IsLRef>
    struct operand_type<ExprTemplate, T, U, true, false, IsLRef>
    {
        using type = terminal<ExprTemplate, std::remove_reference_t<U>>;
    };
 
    template<
        template<expr_kind, class> class ExprTemplate,
        typename T,
        typename U,
        bool IsLRef>
    struct operand_type<ExprTemplate, T, U, false, false, IsLRef>
    {
        using type = terminal<ExprTemplate, U>;
    };
 
    template<template<expr_kind, class> class ExprTemplate, typename T>
    using operand_type_t = typename operand_type<ExprTemplate, T>::type;
 
 
    // make_operand
 
    template<typename T>
    struct make_operand
    {
        template<typename U>
        constexpr auto operator()(U && u)
        {
            return T{static_cast<U &&>(u)};
        }
    };
 
    template<template<expr_kind, class> class ExprTemplate, typename Tuple>
    struct make_operand<ExprTemplate<expr_kind::expr_ref, Tuple>>
    {
        constexpr auto operator()(ExprTemplate<expr_kind::expr_ref, Tuple> expr)
        {
            return expr;
        }
 
        template<typename U>
        constexpr auto operator()(U && u)
        {
            return ExprTemplate<expr_kind::expr_ref, Tuple>{
                Tuple{std::addressof(u)}};
        }
    };
 
 
    // free_binary_op_result
 
    template<
        template<expr_kind, class> class ExprTemplate,
        expr_kind OpKind,
        typename T,
        typename U,
        bool TNonExprUExpr = !is_expr<T>::value && is_expr<U>::value,
        bool ULvalueRef = std::is_lvalue_reference<U>::value>
    struct free_binary_op_result;
 
    template<
        template<expr_kind, class> class ExprTemplate,
        expr_kind OpKind,
        typename T,
        typename U>
    struct free_binary_op_result<ExprTemplate, OpKind, T, U, true, true>
    {
        using lhs_type = operand_type_t<ExprTemplate, T>;
        using rhs_type = expr_ref_t<ExprTemplate, U>;
        using rhs_tuple_type = expr_ref_tuple_t<ExprTemplate, rhs_type>;
        using type = ExprTemplate<OpKind, hana::tuple<lhs_type, rhs_type>>;
    };
 
    template<
        template<expr_kind, class> class ExprTemplate,
        expr_kind OpKind,
        typename T,
        typename U>
    struct free_binary_op_result<ExprTemplate, OpKind, T, U, true, false>
    {
        using lhs_type = operand_type_t<ExprTemplate, T>;
        using rhs_type = remove_cv_ref_t<U>;
        using type = ExprTemplate<OpKind, hana::tuple<lhs_type, rhs_type>>;
    };
 
    template<
        template<expr_kind, class> class ExprTemplate,
        expr_kind OpKind,
        typename T,
        typename U>
    using free_binary_op_result_t =
        typename free_binary_op_result<ExprTemplate, OpKind, T, U>::type;
 
 
    // ternary_op_result
 
    template<
        template<expr_kind, class> class ExprTemplate,
        typename T,
        typename U,
        typename V,
        bool Valid =
            is_expr<T>::value || is_expr<U>::value || is_expr<V>::value>
    struct ternary_op_result;
 
    template<
        template<expr_kind, class> class ExprTemplate,
        typename T,
        typename U,
        typename V>
    struct ternary_op_result<ExprTemplate, T, U, V, true>
    {
        using cond_type = operand_type_t<ExprTemplate, T>;
        using then_type = operand_type_t<ExprTemplate, U>;
        using else_type = operand_type_t<ExprTemplate, V>;
        using type = ExprTemplate<
            expr_kind::if_else,
            hana::tuple<cond_type, then_type, else_type>>;
    };
 
    template<
        template<expr_kind, class> class ExprTemplate,
        typename T,
        typename U,
        typename V>
    using ternary_op_result_t =
        typename ternary_op_result<ExprTemplate, T, U, V>::type;
 
 
    // udt_any_ternary_op_result
 
    template<
        template<expr_kind, class> class ExprTemplate,
        typename T,
        typename U,
        typename V,
        template<class> class UdtTrait,
        bool Valid = !is_expr<T>::value && !is_expr<U>::value &&
                     !is_expr<V>::value &&
                     (UdtTrait<remove_cv_ref_t<T>>::value ||
                      UdtTrait<remove_cv_ref_t<U>>::value ||
                      UdtTrait<remove_cv_ref_t<V>>::value)>
    struct udt_any_ternary_op_result;
 
    template<
        template<expr_kind, class> class ExprTemplate,
        typename T,
        typename U,
        typename V,
        template<class> class UdtTrait>
    struct udt_any_ternary_op_result<ExprTemplate, T, U, V, UdtTrait, true>
    {
        using cond_type = operand_type_t<ExprTemplate, T>;
        using then_type = operand_type_t<ExprTemplate, U>;
        using else_type = operand_type_t<ExprTemplate, V>;
        using type = ExprTemplate<
            expr_kind::if_else,
            hana::tuple<cond_type, then_type, else_type>>;
    };
 
    template<
        template<expr_kind, class> class ExprTemplate,
        typename T,
        typename U,
        typename V,
        template<class> class UdtTrait>
    using udt_any_ternary_op_result_t =
        typename udt_any_ternary_op_result<ExprTemplate, T, U, V, UdtTrait>::
            type;
 
 
    // udt_unary_op_result
 
    template<
        template<expr_kind, class> class ExprTemplate,
        expr_kind OpKind,
        typename T,
        template<class> class UdtTrait,
        bool Valid = !is_expr<T>::value && UdtTrait<remove_cv_ref_t<T>>::value>
    struct udt_unary_op_result;
 
    template<
        template<expr_kind, class> class ExprTemplate,
        expr_kind OpKind,
        typename T,
        template<class> class UdtTrait>
    struct udt_unary_op_result<ExprTemplate, OpKind, T, UdtTrait, true>
    {
        using x_type = operand_type_t<ExprTemplate, T>;
        using type = ExprTemplate<OpKind, hana::tuple<x_type>>;
    };
 
    template<
        template<expr_kind, class> class ExprTemplate,
        expr_kind OpKind,
        typename T,
        template<class> class UdtTrait>
    using udt_unary_op_result_t =
        typename udt_unary_op_result<ExprTemplate, OpKind, T, UdtTrait>::type;
 
 
    // udt_udt_binary_op_result
 
    template<typename T, template<class> class UdtTrait>
    struct is_udt_arg
    {
        static bool const value =
            !is_expr<T>::value && UdtTrait<remove_cv_ref_t<T>>::value;
    };
 
    template<
        template<expr_kind, class> class ExprTemplate,
        expr_kind OpKind,
        typename T,
        typename U,
        template<class> class TUdtTrait,
        template<class> class UUdtTrait,
        bool Valid =
            is_udt_arg<T, TUdtTrait>::value && is_udt_arg<U, UUdtTrait>::value>
    struct udt_udt_binary_op_result;
 
    template<
        template<expr_kind, class> class ExprTemplate,
        expr_kind OpKind,
        typename T,
        typename U,
        template<class> class TUdtTrait,
        template<class> class UUdtTrait>
    struct udt_udt_binary_op_result<
        ExprTemplate,
        OpKind,
        T,
        U,
        TUdtTrait,
        UUdtTrait,
        true>
    {
        using lhs_type = operand_type_t<ExprTemplate, T>;
        using rhs_type = operand_type_t<ExprTemplate, U>;
        using type = ExprTemplate<OpKind, hana::tuple<lhs_type, rhs_type>>;
    };
 
    template<
        template<expr_kind, class> class ExprTemplate,
        expr_kind OpKind,
        typename T,
        typename U,
        template<class> class TUdtTrait,
        template<class> class UUdtTrait>
    using udt_udt_binary_op_result_t = typename udt_udt_binary_op_result<
        ExprTemplate,
        OpKind,
        T,
        U,
        TUdtTrait,
        UUdtTrait>::type;
 
 
    // udt_any_binary_op_result
 
    template<
        template<expr_kind, class> class ExprTemplate,
        expr_kind OpKind,
        typename T,
        typename U,
        template<class> class UdtTrait,
        bool Valid = !is_expr<T>::value && !is_expr<U>::value &&
                     (UdtTrait<remove_cv_ref_t<T>>::value ||
                      UdtTrait<remove_cv_ref_t<U>>::value)>
    struct udt_any_binary_op_result;
 
    template<
        template<expr_kind, class> class ExprTemplate,
        expr_kind OpKind,
        typename T,
        typename U,
        template<class> class UdtTrait>
    struct udt_any_binary_op_result<ExprTemplate, OpKind, T, U, UdtTrait, true>
    {
        using lhs_type = operand_type_t<ExprTemplate, T>;
        using rhs_type = operand_type_t<ExprTemplate, U>;
        using type = ExprTemplate<OpKind, hana::tuple<lhs_type, rhs_type>>;
    };
 
    template<
        template<expr_kind, class> class ExprTemplate,
        expr_kind OpKind,
        typename T,
        typename U,
        template<class> class UdtTrait>
    using udt_any_binary_op_result_t = typename udt_any_binary_op_result<
        ExprTemplate,
        OpKind,
        T,
        U,
        UdtTrait>::type;
 
 
    // not_copy_or_move
 
    template<typename LeftT, typename RightT>
    struct copy_or_move : std::false_type
    {
    };
 
    template<typename T>
    struct copy_or_move<T, T const &> : std::true_type
    {
    };
 
    template<typename T>
    struct copy_or_move<T, T &> : std::true_type
    {
    };
 
    template<typename T>
    struct copy_or_move<T, T &&> : std::true_type
    {
    };
 
 
    // expr_arity
 
    enum class expr_arity { invalid, one, two, three, n };
 
    template<expr_kind Kind>
    constexpr expr_arity arity_of()
    {
        switch (Kind) {
        case expr_kind::expr_ref:
 
        case expr_kind::terminal:
 
        // unary
        case expr_kind::unary_plus:  // +
        case expr_kind::negate:      // -
        case expr_kind::dereference: // *
        case expr_kind::complement:  // ~
        case expr_kind::address_of:  // &
        case expr_kind::logical_not: // !
        case expr_kind::pre_inc:     // ++
        case expr_kind::pre_dec:     // --
        case expr_kind::post_inc:    // ++(int)
        case expr_kind::post_dec:    // --(int)
            return expr_arity::one;
 
        // binary
        case expr_kind::shift_left:         // <<
        case expr_kind::shift_right:        // >>
        case expr_kind::multiplies:         // *
        case expr_kind::divides:            // /
        case expr_kind::modulus:            // %
        case expr_kind::plus:               // +
        case expr_kind::minus:              // -
        case expr_kind::less:               // <
        case expr_kind::greater:            // >
        case expr_kind::less_equal:         // <=
        case expr_kind::greater_equal:      // >=
        case expr_kind::equal_to:           // ==
        case expr_kind::not_equal_to:       // !=
        case expr_kind::logical_or:         // ||
        case expr_kind::logical_and:        // &&
        case expr_kind::bitwise_and:        // &
        case expr_kind::bitwise_or:         // |
        case expr_kind::bitwise_xor:        // ^
        case expr_kind::comma:              // :
        case expr_kind::mem_ptr:            // ->*
        case expr_kind::assign:             // =
        case expr_kind::shift_left_assign:  // <<=
        case expr_kind::shift_right_assign: // >>=
        case expr_kind::multiplies_assign:  // *=
        case expr_kind::divides_assign:     // /=
        case expr_kind::modulus_assign:     // %=
        case expr_kind::plus_assign:        // +=
        case expr_kind::minus_assign:       // -=
        case expr_kind::bitwise_and_assign: // &=
        case expr_kind::bitwise_or_assign:  // |=
        case expr_kind::bitwise_xor_assign: // ^=
        case expr_kind::subscript:          // []
            return expr_arity::two;
 
        // ternary
        case expr_kind::if_else: // (analogous to) ?:
            return expr_arity::three;
 
        // n-ary
        case expr_kind::call: // ()
            return expr_arity::n;
 
        default: return expr_arity::invalid;
        }
    }
 
}}}
 
#endif