File indexing completed on 2024-11-15 09:29:28
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef BOOST_PROTO_TRANSFORM_DEFAULT_HPP_EAN_04_04_2008
0011 #define BOOST_PROTO_TRANSFORM_DEFAULT_HPP_EAN_04_04_2008
0012
0013 #include <boost/preprocessor/iteration/iterate.hpp>
0014 #include <boost/preprocessor/repetition/repeat.hpp>
0015 #include <boost/preprocessor/arithmetic/add.hpp>
0016 #include <boost/preprocessor/arithmetic/sub.hpp>
0017 #include <boost/preprocessor/repetition/enum.hpp>
0018 #include <boost/preprocessor/repetition/enum_shifted.hpp>
0019 #include <boost/preprocessor/repetition/enum_shifted_params.hpp>
0020 #include <boost/ref.hpp>
0021 #include <boost/get_pointer.hpp>
0022 #include <boost/utility/enable_if.hpp>
0023 #include <boost/type_traits/is_member_pointer.hpp>
0024 #include <boost/type_traits/is_member_object_pointer.hpp>
0025 #include <boost/type_traits/is_member_function_pointer.hpp>
0026 #include <boost/proto/proto_fwd.hpp>
0027 #include <boost/proto/traits.hpp>
0028 #include <boost/proto/transform/impl.hpp>
0029 #include <boost/proto/transform/arg.hpp>
0030 #include <boost/proto/detail/decltype.hpp>
0031
0032 namespace boost { namespace proto
0033 {
0034 namespace detail
0035 {
0036 template<typename Grammar, typename Tag>
0037 struct default_case
0038 : not_<_>
0039 {};
0040
0041 template<typename Grammar>
0042 struct default_case<Grammar, tag::terminal>
0043 : when<terminal<_>, _value>
0044 {};
0045
0046 template<typename Grammar>
0047 struct default_cases
0048 {
0049 template<typename Tag>
0050 struct case_
0051 : default_case<Grammar, Tag>
0052 {};
0053 };
0054
0055 #define BOOST_PROTO_UNARY_DEFAULT_EVAL(OP, TAG, MAKE) \
0056 template<typename Grammar> \
0057 struct BOOST_PP_CAT(default_, TAG) \
0058 : transform<BOOST_PP_CAT(default_, TAG)<Grammar> > \
0059 { \
0060 template<typename Expr, typename State, typename Data> \
0061 struct impl \
0062 : transform_impl<Expr, State, Data> \
0063 { \
0064 private: \
0065 typedef typename result_of::child_c<Expr, 0>::type e0; \
0066 typedef typename Grammar::template impl<e0, State, Data>::result_type r0; \
0067 public: \
0068 BOOST_PROTO_DECLTYPE_(OP proto::detail::MAKE<r0>(), result_type) \
0069 result_type operator ()( \
0070 typename impl::expr_param e \
0071 , typename impl::state_param s \
0072 , typename impl::data_param d \
0073 ) const \
0074 { \
0075 typename Grammar::template impl<e0, State, Data> t0; \
0076 return OP t0(proto::child_c<0>(e), s, d); \
0077 } \
0078 }; \
0079 }; \
0080 \
0081 template<typename Grammar> \
0082 struct default_case<Grammar, tag::TAG> \
0083 : when<unary_expr<tag::TAG, Grammar>, BOOST_PP_CAT(default_, TAG)<Grammar> > \
0084 {}; \
0085
0086
0087 #define BOOST_PROTO_BINARY_DEFAULT_EVAL(OP, TAG, LMAKE, RMAKE) \
0088 template<typename Grammar> \
0089 struct BOOST_PP_CAT(default_, TAG) \
0090 : transform<BOOST_PP_CAT(default_, TAG)<Grammar> > \
0091 { \
0092 template<typename Expr, typename State, typename Data> \
0093 struct impl \
0094 : transform_impl<Expr, State, Data> \
0095 { \
0096 private: \
0097 typedef typename result_of::child_c<Expr, 0>::type e0; \
0098 typedef typename result_of::child_c<Expr, 1>::type e1; \
0099 typedef typename Grammar::template impl<e0, State, Data>::result_type r0; \
0100 typedef typename Grammar::template impl<e1, State, Data>::result_type r1; \
0101 public: \
0102 BOOST_PROTO_DECLTYPE_( \
0103 proto::detail::LMAKE<r0>() OP proto::detail::RMAKE<r1>() \
0104 , result_type \
0105 ) \
0106 result_type operator ()( \
0107 typename impl::expr_param e \
0108 , typename impl::state_param s \
0109 , typename impl::data_param d \
0110 ) const \
0111 { \
0112 typename Grammar::template impl<e0, State, Data> t0; \
0113 typename Grammar::template impl<e1, State, Data> t1; \
0114 return t0(proto::child_c<0>(e), s, d) \
0115 OP t1(proto::child_c<1>(e), s, d); \
0116 } \
0117 }; \
0118 }; \
0119 \
0120 template<typename Grammar> \
0121 struct default_case<Grammar, tag::TAG> \
0122 : when<binary_expr<tag::TAG, Grammar, Grammar>, BOOST_PP_CAT(default_, TAG)<Grammar> > \
0123 {}; \
0124
0125
0126 BOOST_PROTO_UNARY_DEFAULT_EVAL(+, unary_plus, make)
0127 BOOST_PROTO_UNARY_DEFAULT_EVAL(-, negate, make)
0128 BOOST_PROTO_UNARY_DEFAULT_EVAL(*, dereference, make)
0129 BOOST_PROTO_UNARY_DEFAULT_EVAL(~, complement, make)
0130 BOOST_PROTO_UNARY_DEFAULT_EVAL(&, address_of, make)
0131 BOOST_PROTO_UNARY_DEFAULT_EVAL(!, logical_not, make)
0132 BOOST_PROTO_UNARY_DEFAULT_EVAL(++, pre_inc, make_mutable)
0133 BOOST_PROTO_UNARY_DEFAULT_EVAL(--, pre_dec, make_mutable)
0134
0135 BOOST_PROTO_BINARY_DEFAULT_EVAL(<<, shift_left, make_mutable, make)
0136 BOOST_PROTO_BINARY_DEFAULT_EVAL(>>, shift_right, make_mutable, make_mutable)
0137 BOOST_PROTO_BINARY_DEFAULT_EVAL(*, multiplies, make, make)
0138 BOOST_PROTO_BINARY_DEFAULT_EVAL(/, divides, make, make)
0139 BOOST_PROTO_BINARY_DEFAULT_EVAL(%, modulus, make, make)
0140 BOOST_PROTO_BINARY_DEFAULT_EVAL(+, plus, make, make)
0141 BOOST_PROTO_BINARY_DEFAULT_EVAL(-, minus, make, make)
0142 BOOST_PROTO_BINARY_DEFAULT_EVAL(<, less, make, make)
0143 BOOST_PROTO_BINARY_DEFAULT_EVAL(>, greater, make, make)
0144 BOOST_PROTO_BINARY_DEFAULT_EVAL(<=, less_equal, make, make)
0145 BOOST_PROTO_BINARY_DEFAULT_EVAL(>=, greater_equal, make, make)
0146 BOOST_PROTO_BINARY_DEFAULT_EVAL(==, equal_to, make, make)
0147 BOOST_PROTO_BINARY_DEFAULT_EVAL(!=, not_equal_to, make, make)
0148 BOOST_PROTO_BINARY_DEFAULT_EVAL(||, logical_or, make, make)
0149 BOOST_PROTO_BINARY_DEFAULT_EVAL(&&, logical_and, make, make)
0150 BOOST_PROTO_BINARY_DEFAULT_EVAL(&, bitwise_and, make, make)
0151 BOOST_PROTO_BINARY_DEFAULT_EVAL(|, bitwise_or, make, make)
0152 BOOST_PROTO_BINARY_DEFAULT_EVAL(^, bitwise_xor, make, make)
0153
0154 BOOST_PROTO_BINARY_DEFAULT_EVAL(=, assign, make_mutable, make)
0155 BOOST_PROTO_BINARY_DEFAULT_EVAL(<<=, shift_left_assign, make_mutable, make)
0156 BOOST_PROTO_BINARY_DEFAULT_EVAL(>>=, shift_right_assign, make_mutable, make)
0157 BOOST_PROTO_BINARY_DEFAULT_EVAL(*=, multiplies_assign, make_mutable, make)
0158 BOOST_PROTO_BINARY_DEFAULT_EVAL(/=, divides_assign, make_mutable, make)
0159 BOOST_PROTO_BINARY_DEFAULT_EVAL(%=, modulus_assign, make_mutable, make)
0160 BOOST_PROTO_BINARY_DEFAULT_EVAL(+=, plus_assign, make_mutable, make)
0161 BOOST_PROTO_BINARY_DEFAULT_EVAL(-=, minus_assign, make_mutable, make)
0162 BOOST_PROTO_BINARY_DEFAULT_EVAL(&=, bitwise_and_assign, make_mutable, make)
0163 BOOST_PROTO_BINARY_DEFAULT_EVAL(|=, bitwise_or_assign, make_mutable, make)
0164 BOOST_PROTO_BINARY_DEFAULT_EVAL(^=, bitwise_xor_assign, make_mutable, make)
0165
0166 #undef BOOST_PROTO_UNARY_DEFAULT_EVAL
0167 #undef BOOST_PROTO_BINARY_DEFAULT_EVAL
0168
0169
0170 template<typename Grammar, typename Expr, typename State, typename Data>
0171 struct is_member_function_invocation
0172 : is_member_function_pointer<
0173 typename uncvref<
0174 typename Grammar::template impl<
0175 typename result_of::child_c<Expr, 1>::type
0176 , State
0177 , Data
0178 >::result_type
0179 >::type
0180 >
0181 {};
0182
0183
0184 template<typename Grammar, typename Expr, typename State, typename Data, bool IsMemFunCall>
0185 struct default_mem_ptr_impl
0186 : transform_impl<Expr, State, Data>
0187 {
0188 private:
0189 typedef typename result_of::child_c<Expr, 0>::type e0;
0190 typedef typename result_of::child_c<Expr, 1>::type e1;
0191 typedef typename Grammar::template impl<e0, State, Data>::result_type r0;
0192 typedef typename Grammar::template impl<e1, State, Data>::result_type r1;
0193 public:
0194 typedef typename detail::mem_ptr_fun<r0, r1>::result_type result_type;
0195 result_type operator ()(
0196 typename default_mem_ptr_impl::expr_param e
0197 , typename default_mem_ptr_impl::state_param s
0198 , typename default_mem_ptr_impl::data_param d
0199 ) const
0200 {
0201 typename Grammar::template impl<e0, State, Data> t0;
0202 typename Grammar::template impl<e1, State, Data> t1;
0203 return detail::mem_ptr_fun<r0, r1>()(
0204 t0(proto::child_c<0>(e), s, d)
0205 , t1(proto::child_c<1>(e), s, d)
0206 );
0207 }
0208 };
0209
0210
0211 template<typename Grammar, typename Expr, typename State, typename Data>
0212 struct default_mem_ptr_impl<Grammar, Expr, State, Data, true>
0213 : transform_impl<Expr, State, Data>
0214 {
0215 private:
0216 typedef typename result_of::child_c<Expr, 0>::type e0;
0217 typedef typename result_of::child_c<Expr, 1>::type e1;
0218 typedef typename Grammar::template impl<e0, State, Data>::result_type r0;
0219 typedef typename Grammar::template impl<e1, State, Data>::result_type r1;
0220 public:
0221 typedef detail::memfun<r0, r1> result_type;
0222 result_type const operator ()(
0223 typename default_mem_ptr_impl::expr_param e
0224 , typename default_mem_ptr_impl::state_param s
0225 , typename default_mem_ptr_impl::data_param d
0226 ) const
0227 {
0228 typename Grammar::template impl<e0, State, Data> t0;
0229 typename Grammar::template impl<e1, State, Data> t1;
0230 return detail::memfun<r0, r1>(
0231 t0(proto::child_c<0>(e), s, d)
0232 , t1(proto::child_c<1>(e), s, d)
0233 );
0234 }
0235 };
0236
0237 template<typename Grammar>
0238 struct default_mem_ptr
0239 : transform<default_mem_ptr<Grammar> >
0240 {
0241 template<typename Expr, typename State, typename Data>
0242 struct impl
0243 : default_mem_ptr_impl<
0244 Grammar
0245 , Expr
0246 , State
0247 , Data
0248 , is_member_function_invocation<Grammar, Expr, State, Data>::value
0249 >
0250 {};
0251 };
0252
0253 template<typename Grammar>
0254 struct default_case<Grammar, tag::mem_ptr>
0255 : when<mem_ptr<Grammar, Grammar>, default_mem_ptr<Grammar> >
0256 {};
0257
0258 template<typename Grammar>
0259 struct default_post_inc
0260 : transform<default_post_inc<Grammar> >
0261 {
0262 template<typename Expr, typename State, typename Data>
0263 struct impl
0264 : transform_impl<Expr, State, Data>
0265 {
0266 private:
0267 typedef typename result_of::child_c<Expr, 0>::type e0;
0268 typedef typename Grammar::template impl<e0, State, Data>::result_type r0;
0269 public:
0270 BOOST_PROTO_DECLTYPE_(proto::detail::make_mutable<r0>() ++, result_type)
0271 result_type operator ()(
0272 typename impl::expr_param e
0273 , typename impl::state_param s
0274 , typename impl::data_param d
0275 ) const
0276 {
0277 typename Grammar::template impl<e0, State, Data> t0;
0278 return t0(proto::child_c<0>(e), s, d) ++;
0279 }
0280 };
0281 };
0282
0283 template<typename Grammar>
0284 struct default_case<Grammar, tag::post_inc>
0285 : when<post_inc<Grammar>, default_post_inc<Grammar> >
0286 {};
0287
0288 template<typename Grammar>
0289 struct default_post_dec
0290 : transform<default_post_dec<Grammar> >
0291 {
0292 template<typename Expr, typename State, typename Data>
0293 struct impl
0294 : transform_impl<Expr, State, Data>
0295 {
0296 private:
0297 typedef typename result_of::child_c<Expr, 0>::type e0;
0298 typedef typename Grammar::template impl<e0, State, Data>::result_type r0;
0299 public:
0300 BOOST_PROTO_DECLTYPE_(proto::detail::make_mutable<r0>() --, result_type)
0301 result_type operator ()(
0302 typename impl::expr_param e
0303 , typename impl::state_param s
0304 , typename impl::data_param d
0305 ) const
0306 {
0307 typename Grammar::template impl<e0, State, Data> t0;
0308 return t0(proto::child_c<0>(e), s, d) --;
0309 }
0310 };
0311 };
0312
0313 template<typename Grammar>
0314 struct default_case<Grammar, tag::post_dec>
0315 : when<post_dec<Grammar>, default_post_dec<Grammar> >
0316 {};
0317
0318 template<typename Grammar>
0319 struct default_subscript
0320 : transform<default_subscript<Grammar> >
0321 {
0322 template<typename Expr, typename State, typename Data>
0323 struct impl
0324 : transform_impl<Expr, State, Data>
0325 {
0326 private:
0327 typedef typename result_of::child_c<Expr, 0>::type e0;
0328 typedef typename result_of::child_c<Expr, 1>::type e1;
0329 typedef typename Grammar::template impl<e0, State, Data>::result_type r0;
0330 typedef typename Grammar::template impl<e1, State, Data>::result_type r1;
0331 public:
0332 BOOST_PROTO_DECLTYPE_(
0333 proto::detail::make_subscriptable<r0>() [ proto::detail::make<r1>() ]
0334 , result_type
0335 )
0336 result_type operator ()(
0337 typename impl::expr_param e
0338 , typename impl::state_param s
0339 , typename impl::data_param d
0340 ) const
0341 {
0342 typename Grammar::template impl<e0, State, Data> t0;
0343 typename Grammar::template impl<e1, State, Data> t1;
0344 return t0(proto::child_c<0>(e), s, d) [
0345 t1(proto::child_c<1>(e), s, d) ];
0346 }
0347 };
0348 };
0349
0350 template<typename Grammar>
0351 struct default_case<Grammar, tag::subscript>
0352 : when<subscript<Grammar, Grammar>, default_subscript<Grammar> >
0353 {};
0354
0355 template<typename Grammar>
0356 struct default_if_else_
0357 {
0358 template<typename Expr, typename State, typename Data>
0359 struct impl
0360 : transform_impl<Expr, State, Data>
0361 {
0362 private:
0363 typedef typename result_of::child_c<Expr, 0>::type e0;
0364 typedef typename result_of::child_c<Expr, 1>::type e1;
0365 typedef typename result_of::child_c<Expr, 2>::type e2;
0366 typedef typename Grammar::template impl<e0, State, Data>::result_type r0;
0367 typedef typename Grammar::template impl<e1, State, Data>::result_type r1;
0368 typedef typename Grammar::template impl<e2, State, Data>::result_type r2;
0369 public:
0370 BOOST_PROTO_DECLTYPE_(
0371 proto::detail::make<r0>()
0372 ? proto::detail::make<r1>()
0373 : proto::detail::make<r2>()
0374 , result_type
0375 )
0376 result_type operator ()(
0377 typename impl::expr_param e
0378 , typename impl::state_param s
0379 , typename impl::data_param d
0380 ) const
0381 {
0382 typename Grammar::template impl<e0, State, Data> t0;
0383 typename Grammar::template impl<e1, State, Data> t1;
0384 typename Grammar::template impl<e2, State, Data> t2;
0385 return t0(proto::child_c<0>(e), s, d)
0386 ? t1(proto::child_c<1>(e), s, d)
0387 : t2(proto::child_c<2>(e), s, d);
0388 }
0389 };
0390 };
0391
0392 template<typename Grammar>
0393 struct default_case<Grammar, tag::if_else_>
0394 : when<if_else_<Grammar, Grammar, Grammar>, default_if_else_<Grammar> >
0395 {};
0396
0397 template<typename Grammar>
0398 struct default_comma
0399 : transform<default_comma<Grammar> >
0400 {
0401 template<typename Expr, typename State, typename Data>
0402 struct impl
0403 : transform_impl<Expr, State, Data>
0404 {
0405 private:
0406 typedef typename result_of::child_c<Expr, 0>::type e0;
0407 typedef typename result_of::child_c<Expr, 1>::type e1;
0408 typedef typename Grammar::template impl<e0, State, Data>::result_type r0;
0409 typedef typename Grammar::template impl<e1, State, Data>::result_type r1;
0410 public:
0411 typedef typename proto::detail::comma_result<r0, r1>::type result_type;
0412 result_type operator ()(
0413 typename impl::expr_param e
0414 , typename impl::state_param s
0415 , typename impl::data_param d
0416 ) const
0417 {
0418 typename Grammar::template impl<e0, State, Data> t0;
0419 typename Grammar::template impl<e1, State, Data> t1;
0420 return t0(proto::child_c<0>(e), s, d)
0421 , t1(proto::child_c<1>(e), s, d);
0422 }
0423 };
0424 };
0425
0426 template<typename Grammar>
0427 struct default_case<Grammar, tag::comma>
0428 : when<comma<Grammar, Grammar>, default_comma<Grammar> >
0429 {};
0430
0431 template<typename Grammar, typename Expr, typename State, typename Data, long Arity>
0432 struct default_function_impl;
0433
0434 template<typename Grammar>
0435 struct default_function
0436 : transform<default_function<Grammar> >
0437 {
0438 template<typename Expr, typename State, typename Data>
0439 struct impl
0440 : default_function_impl<
0441 Grammar
0442 , Expr
0443 , State
0444 , Data
0445 , transform_impl<Expr, State, Data>::expr::proto_arity_c
0446 >
0447 {};
0448 };
0449
0450 template<typename Grammar>
0451 struct default_case<Grammar, tag::function>
0452 : when<function<Grammar, vararg<Grammar> >, default_function<Grammar> >
0453 {};
0454
0455 #define BOOST_PROTO_DEFAULT_EVAL_TYPE(Z, N, DATA) \
0456 typedef \
0457 typename result_of::child_c<DATA, N>::type \
0458 BOOST_PP_CAT(e, N); \
0459 \
0460 typedef \
0461 typename Grammar::template impl<BOOST_PP_CAT(e, N), State, Data>::result_type \
0462 BOOST_PP_CAT(r, N); \
0463
0464
0465 #define BOOST_PROTO_DEFAULT_EVAL(Z, N, DATA) \
0466 typename Grammar::template impl<BOOST_PP_CAT(e, N), State, Data>()( \
0467 proto::child_c<N>(DATA), s, d \
0468 ) \
0469
0470
0471 template<typename Grammar, typename Expr, typename State, typename Data>
0472 struct default_function_impl<Grammar, Expr, State, Data, 1>
0473 : transform_impl<Expr, State, Data>
0474 {
0475 BOOST_PROTO_DEFAULT_EVAL_TYPE(~, 0, Expr)
0476
0477 typedef
0478 typename proto::detail::result_of_fixup<r0>::type
0479 function_type;
0480
0481 typedef
0482 typename BOOST_PROTO_RESULT_OF<function_type()>::type
0483 result_type;
0484
0485 result_type operator ()(
0486 typename default_function_impl::expr_param e
0487 , typename default_function_impl::state_param s
0488 , typename default_function_impl::data_param d
0489 ) const
0490 {
0491 return BOOST_PROTO_DEFAULT_EVAL(~, 0, e)();
0492 }
0493 };
0494
0495 template<typename Grammar, typename Expr, typename State, typename Data>
0496 struct default_function_impl<Grammar, Expr, State, Data, 2>
0497 : transform_impl<Expr, State, Data>
0498 {
0499 BOOST_PROTO_DEFAULT_EVAL_TYPE(~, 0, Expr)
0500 BOOST_PROTO_DEFAULT_EVAL_TYPE(~, 1, Expr)
0501
0502 typedef
0503 typename proto::detail::result_of_fixup<r0>::type
0504 function_type;
0505
0506 typedef
0507 typename detail::result_of_<function_type(r1)>::type
0508 result_type;
0509
0510 result_type operator ()(
0511 typename default_function_impl::expr_param e
0512 , typename default_function_impl::state_param s
0513 , typename default_function_impl::data_param d
0514 ) const
0515 {
0516 return this->invoke(
0517 e
0518 , s
0519 , d
0520 , is_member_function_pointer<function_type>()
0521 , is_member_object_pointer<function_type>()
0522 );
0523 }
0524
0525 private:
0526 result_type invoke(
0527 typename default_function_impl::expr_param e
0528 , typename default_function_impl::state_param s
0529 , typename default_function_impl::data_param d
0530 , mpl::false_
0531 , mpl::false_
0532 ) const
0533 {
0534 return BOOST_PROTO_DEFAULT_EVAL(~, 0, e)(BOOST_PROTO_DEFAULT_EVAL(~, 1, e));
0535 }
0536
0537 result_type invoke(
0538 typename default_function_impl::expr_param e
0539 , typename default_function_impl::state_param s
0540 , typename default_function_impl::data_param d
0541 , mpl::true_
0542 , mpl::false_
0543 ) const
0544 {
0545 BOOST_PROTO_USE_GET_POINTER();
0546 typedef typename detail::class_member_traits<function_type>::class_type class_type;
0547 return (
0548 BOOST_PROTO_GET_POINTER(class_type, (BOOST_PROTO_DEFAULT_EVAL(~, 1, e))) ->*
0549 BOOST_PROTO_DEFAULT_EVAL(~, 0, e)
0550 )();
0551 }
0552
0553 result_type invoke(
0554 typename default_function_impl::expr_param e
0555 , typename default_function_impl::state_param s
0556 , typename default_function_impl::data_param d
0557 , mpl::false_
0558 , mpl::true_
0559 ) const
0560 {
0561 BOOST_PROTO_USE_GET_POINTER();
0562 typedef typename detail::class_member_traits<function_type>::class_type class_type;
0563 return (
0564 BOOST_PROTO_GET_POINTER(class_type, (BOOST_PROTO_DEFAULT_EVAL(~, 1, e))) ->*
0565 BOOST_PROTO_DEFAULT_EVAL(~, 0, e)
0566 );
0567 }
0568 };
0569
0570 #include <boost/proto/transform/detail/default_function_impl.hpp>
0571
0572 #undef BOOST_PROTO_DEFAULT_EVAL_TYPE
0573 #undef BOOST_PROTO_DEFAULT_EVAL
0574 }
0575
0576 template<typename Grammar >
0577 struct _default
0578 : switch_<detail::default_cases<Grammar> >
0579 {};
0580
0581 template<typename Grammar>
0582 struct is_callable<_default<Grammar> >
0583 : mpl::true_
0584 {};
0585
0586 namespace detail
0587 {
0588
0589
0590 struct _default
0591 : proto::_default<>
0592 {};
0593 }
0594
0595 }}
0596
0597 #endif
0598