matches.hpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  1. ///////////////////////////////////////////////////////////////////////////////
  2. /// \file matches.hpp
  3. /// Contains definition of matches\<\> metafunction for determining if
  4. /// a given expression matches a given pattern.
  5. //
  6. // Copyright 2008 Eric Niebler. Distributed under the Boost
  7. // Software License, Version 1.0. (See accompanying file
  8. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. #ifndef BOOST_PROTO_MATCHES_HPP_EAN_11_03_2006
  10. #define BOOST_PROTO_MATCHES_HPP_EAN_11_03_2006
  11. #include <boost/config.hpp>
  12. #include <boost/detail/workaround.hpp>
  13. #include <boost/preprocessor/cat.hpp>
  14. #include <boost/preprocessor/arithmetic/dec.hpp>
  15. #include <boost/preprocessor/arithmetic/sub.hpp>
  16. #include <boost/preprocessor/iteration/iterate.hpp>
  17. #include <boost/preprocessor/facilities/intercept.hpp>
  18. #include <boost/preprocessor/punctuation/comma_if.hpp>
  19. #include <boost/preprocessor/repetition/enum.hpp>
  20. #include <boost/preprocessor/repetition/enum_params.hpp>
  21. #include <boost/preprocessor/repetition/enum_shifted.hpp>
  22. #include <boost/preprocessor/repetition/enum_binary_params.hpp>
  23. #include <boost/preprocessor/repetition/enum_shifted_params.hpp>
  24. #include <boost/preprocessor/repetition/enum_trailing_params.hpp>
  25. #include <boost/preprocessor/repetition/enum_params_with_a_default.hpp>
  26. #include <boost/preprocessor/repetition/repeat.hpp>
  27. #include <boost/config.hpp>
  28. #include <boost/mpl/logical.hpp>
  29. #include <boost/mpl/eval_if.hpp>
  30. #include <boost/proto/detail/template_arity.hpp>
  31. #include <boost/utility/enable_if.hpp>
  32. #if BOOST_WORKAROUND(BOOST_MSVC, == 1310)
  33. #include <boost/type_traits/is_array.hpp>
  34. #endif
  35. #include <boost/type_traits/is_const.hpp>
  36. #include <boost/type_traits/is_convertible.hpp>
  37. #include <boost/type_traits/is_reference.hpp>
  38. #include <boost/type_traits/is_pointer.hpp>
  39. #include <boost/proto/proto_fwd.hpp>
  40. #include <boost/proto/traits.hpp>
  41. #include <boost/proto/transform/when.hpp>
  42. #include <boost/proto/transform/impl.hpp>
  43. #if defined(_MSC_VER)
  44. # pragma warning(push)
  45. # pragma warning(disable:4305) // 'specialization' : truncation from 'const int' to 'bool'
  46. #endif
  47. #define BOOST_PROTO_LOGICAL_typename_G BOOST_PP_ENUM_PARAMS(BOOST_PROTO_MAX_LOGICAL_ARITY, typename G)
  48. #define BOOST_PROTO_LOGICAL_G BOOST_PP_ENUM_PARAMS(BOOST_PROTO_MAX_LOGICAL_ARITY, G)
  49. namespace boost { namespace proto
  50. {
  51. namespace detail
  52. {
  53. template<typename Expr, typename BasicExpr, typename Grammar>
  54. struct matches_;
  55. template<bool B, typename Pred>
  56. struct and_2;
  57. template<typename And, typename Expr, typename State, typename Data>
  58. struct _and_impl;
  59. template<typename T, typename U>
  60. struct array_matches
  61. : mpl::false_
  62. {};
  63. template<typename T, std::size_t M>
  64. struct array_matches<T[M], T *>
  65. : mpl::true_
  66. {};
  67. template<typename T, std::size_t M>
  68. struct array_matches<T[M], T const *>
  69. : mpl::true_
  70. {};
  71. template<typename T, std::size_t M>
  72. struct array_matches<T[M], T[proto::N]>
  73. : mpl::true_
  74. {};
  75. template<typename T, typename U
  76. BOOST_PROTO_TEMPLATE_ARITY_PARAM(long Arity = detail::template_arity<U>::value)
  77. >
  78. struct lambda_matches
  79. : mpl::false_
  80. {};
  81. template<typename T>
  82. struct lambda_matches<T, proto::_ BOOST_PROTO_TEMPLATE_ARITY_PARAM(-1)>
  83. : mpl::true_
  84. {};
  85. template<typename T>
  86. struct lambda_matches<T, T BOOST_PROTO_TEMPLATE_ARITY_PARAM(-1)>
  87. : mpl::true_
  88. {};
  89. template<typename T, std::size_t M, typename U>
  90. struct lambda_matches<T[M], U BOOST_PROTO_TEMPLATE_ARITY_PARAM(-1)>
  91. : array_matches<T[M], U>
  92. {};
  93. template<typename T, std::size_t M>
  94. struct lambda_matches<T[M], _ BOOST_PROTO_TEMPLATE_ARITY_PARAM(-1)>
  95. : mpl::true_
  96. {};
  97. template<typename T, std::size_t M>
  98. struct lambda_matches<T[M], T[M] BOOST_PROTO_TEMPLATE_ARITY_PARAM(-1)>
  99. : mpl::true_
  100. {};
  101. template<template<typename> class T, typename Expr0, typename Grammar0>
  102. struct lambda_matches<T<Expr0>, T<Grammar0> BOOST_PROTO_TEMPLATE_ARITY_PARAM(1) >
  103. : lambda_matches<Expr0, Grammar0>
  104. {};
  105. // vararg_matches_impl
  106. template<typename Args1, typename Back, long From, long To>
  107. struct vararg_matches_impl;
  108. // vararg_matches
  109. template<typename Expr, typename Args1, typename Args2, typename Back, bool Can, bool Zero, typename Void = void>
  110. struct vararg_matches
  111. : mpl::false_
  112. {};
  113. template<typename Expr, typename Args1, typename Args2, typename Back>
  114. struct vararg_matches<Expr, Args1, Args2, Back, true, true, typename Back::proto_is_vararg_>
  115. : matches_<
  116. Expr
  117. , proto::basic_expr<ignore, Args1, Args1::arity>
  118. , proto::basic_expr<ignore, Args2, Args1::arity>
  119. >
  120. {};
  121. template<typename Expr, typename Args1, typename Args2, typename Back>
  122. struct vararg_matches<Expr, Args1, Args2, Back, true, false, typename Back::proto_is_vararg_>
  123. : and_2<
  124. matches_<
  125. Expr
  126. , proto::basic_expr<ignore, Args1, Args2::arity>
  127. , proto::basic_expr<ignore, Args2, Args2::arity>
  128. >::value
  129. , vararg_matches_impl<Args1, typename Back::proto_grammar, Args2::arity + 1, Args1::arity>
  130. >
  131. {};
  132. // How terminal_matches<> handles references and cv-qualifiers.
  133. // The cv and ref matter *only* if the grammar has a top-level ref.
  134. //
  135. // Expr | Grammar | Matches?
  136. // -------------------------------------
  137. // T T yes
  138. // T & T yes
  139. // T const & T yes
  140. // T T & no
  141. // T & T & yes
  142. // T const & T & no
  143. // T T const & no
  144. // T & T const & no
  145. // T const & T const & yes
  146. template<typename T, typename U>
  147. struct is_cv_ref_compatible
  148. : mpl::true_
  149. {};
  150. template<typename T, typename U>
  151. struct is_cv_ref_compatible<T, U &>
  152. : mpl::false_
  153. {};
  154. template<typename T, typename U>
  155. struct is_cv_ref_compatible<T &, U &>
  156. : mpl::bool_<is_const<T>::value == is_const<U>::value>
  157. {};
  158. #if BOOST_WORKAROUND(BOOST_MSVC, == 1310)
  159. // MSVC-7.1 has lots of problems with array types that have been
  160. // deduced. Partially specializing terminal_matches<> on array types
  161. // doesn't seem to work.
  162. template<
  163. typename T
  164. , typename U
  165. , bool B = is_array<BOOST_PROTO_UNCVREF(T)>::value
  166. >
  167. struct terminal_array_matches
  168. : mpl::false_
  169. {};
  170. template<typename T, typename U, std::size_t M>
  171. struct terminal_array_matches<T, U(&)[M], true>
  172. : is_convertible<T, U(&)[M]>
  173. {};
  174. template<typename T, typename U>
  175. struct terminal_array_matches<T, U(&)[proto::N], true>
  176. : is_convertible<T, U *>
  177. {};
  178. template<typename T, typename U>
  179. struct terminal_array_matches<T, U *, true>
  180. : is_convertible<T, U *>
  181. {};
  182. // terminal_matches
  183. template<typename T, typename U>
  184. struct terminal_matches
  185. : mpl::or_<
  186. mpl::and_<
  187. is_cv_ref_compatible<T, U>
  188. , lambda_matches<
  189. BOOST_PROTO_UNCVREF(T)
  190. , BOOST_PROTO_UNCVREF(U)
  191. >
  192. >
  193. , terminal_array_matches<T, U>
  194. >
  195. {};
  196. #else
  197. // terminal_matches
  198. template<typename T, typename U>
  199. struct terminal_matches
  200. : mpl::and_<
  201. is_cv_ref_compatible<T, U>
  202. , lambda_matches<
  203. BOOST_PROTO_UNCVREF(T)
  204. , BOOST_PROTO_UNCVREF(U)
  205. >
  206. >
  207. {};
  208. template<typename T, std::size_t M>
  209. struct terminal_matches<T(&)[M], T(&)[proto::N]>
  210. : mpl::true_
  211. {};
  212. template<typename T, std::size_t M>
  213. struct terminal_matches<T(&)[M], T *>
  214. : mpl::true_
  215. {};
  216. // Avoid ambiguity errors on MSVC
  217. #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1500))
  218. template<typename T, std::size_t M>
  219. struct terminal_matches<T const (&)[M], T const[M]>
  220. : mpl::true_
  221. {};
  222. #endif
  223. #endif
  224. template<typename T>
  225. struct terminal_matches<T, T>
  226. : mpl::true_
  227. {};
  228. template<typename T>
  229. struct terminal_matches<T &, T>
  230. : mpl::true_
  231. {};
  232. template<typename T>
  233. struct terminal_matches<T const &, T>
  234. : mpl::true_
  235. {};
  236. template<typename T>
  237. struct terminal_matches<T, proto::_>
  238. : mpl::true_
  239. {};
  240. template<typename T>
  241. struct terminal_matches<T, exact<T> >
  242. : mpl::true_
  243. {};
  244. template<typename T, typename U>
  245. struct terminal_matches<T, proto::convertible_to<U> >
  246. : is_convertible<T, U>
  247. {};
  248. // matches_
  249. template<typename Expr, typename BasicExpr, typename Grammar>
  250. struct matches_
  251. : mpl::false_
  252. {};
  253. template<typename Expr, typename BasicExpr>
  254. struct matches_< Expr, BasicExpr, proto::_ >
  255. : mpl::true_
  256. {};
  257. template<typename Expr, typename Tag, typename Args1, long N1, typename Args2, long N2>
  258. struct matches_< Expr, proto::basic_expr<Tag, Args1, N1>, proto::basic_expr<Tag, Args2, N2> >
  259. : vararg_matches< Expr, Args1, Args2, typename Args2::back_, (N1+2 > N2), (N2 > N1) >
  260. {};
  261. template<typename Expr, typename Tag, typename Args1, long N1, typename Args2, long N2>
  262. struct matches_< Expr, proto::basic_expr<Tag, Args1, N1>, proto::basic_expr<proto::_, Args2, N2> >
  263. : vararg_matches< Expr, Args1, Args2, typename Args2::back_, (N1+2 > N2), (N2 > N1) >
  264. {};
  265. template<typename Expr, typename Tag, typename Args1, typename Args2>
  266. struct matches_< Expr, proto::basic_expr<Tag, Args1, 0>, proto::basic_expr<Tag, Args2, 0> >
  267. : terminal_matches<typename Args1::child0, typename Args2::child0>
  268. {};
  269. template<typename Expr, typename Tag, typename Args1, typename Args2, long N2>
  270. struct matches_< Expr, proto::basic_expr<Tag, Args1, 0>, proto::basic_expr<proto::_, Args2, N2> >
  271. : mpl::false_
  272. {};
  273. template<typename Expr, typename Tag, typename Args1, typename Args2>
  274. struct matches_< Expr, proto::basic_expr<Tag, Args1, 0>, proto::basic_expr<proto::_, Args2, 0> >
  275. : terminal_matches<typename Args1::child0, typename Args2::child0>
  276. {};
  277. template<typename Expr, typename Tag, typename Args1, typename Args2>
  278. struct matches_< Expr, proto::basic_expr<Tag, Args1, 1>, proto::basic_expr<Tag, Args2, 1> >
  279. : matches_<
  280. typename detail::expr_traits<typename Args1::child0>::value_type::proto_derived_expr
  281. , typename detail::expr_traits<typename Args1::child0>::value_type::proto_grammar
  282. , typename Args2::child0::proto_grammar
  283. >
  284. {};
  285. template<typename Expr, typename Tag, typename Args1, typename Args2>
  286. struct matches_< Expr, proto::basic_expr<Tag, Args1, 1>, proto::basic_expr<proto::_, Args2, 1> >
  287. : matches_<
  288. typename detail::expr_traits<typename Args1::child0>::value_type::proto_derived_expr
  289. , typename detail::expr_traits<typename Args1::child0>::value_type::proto_grammar
  290. , typename Args2::child0::proto_grammar
  291. >
  292. {};
  293. #include <boost/proto/detail/and_n.hpp>
  294. #include <boost/proto/detail/or_n.hpp>
  295. #include <boost/proto/detail/matches_.hpp>
  296. #include <boost/proto/detail/vararg_matches_impl.hpp>
  297. #include <boost/proto/detail/lambda_matches.hpp>
  298. // handle proto::if_
  299. template<typename Expr, typename Tag, typename Args, long Arity, typename If, typename Then, typename Else>
  300. struct matches_<Expr, proto::basic_expr<Tag, Args, Arity>, proto::if_<If, Then, Else> >
  301. : mpl::eval_if_c<
  302. static_cast<bool>(
  303. remove_reference<
  304. typename when<_, If>::template impl<Expr, int, int>::result_type
  305. >::type::value
  306. )
  307. , matches_<Expr, proto::basic_expr<Tag, Args, Arity>, typename Then::proto_grammar>
  308. , matches_<Expr, proto::basic_expr<Tag, Args, Arity>, typename Else::proto_grammar>
  309. >::type
  310. {
  311. typedef
  312. typename mpl::if_c<
  313. static_cast<bool>(
  314. remove_reference<
  315. typename when<_, If>::template impl<Expr, int, int>::result_type
  316. >::type::value
  317. )
  318. , Then
  319. , Else
  320. >::type
  321. which;
  322. };
  323. // handle degenerate cases of proto::or_
  324. template<typename Expr, typename BasicExpr>
  325. struct matches_<Expr, BasicExpr, or_<> >
  326. : mpl::false_
  327. {
  328. typedef not_<_> which;
  329. };
  330. template<typename Expr, typename BasicExpr, typename G0>
  331. struct matches_<Expr, BasicExpr, or_<G0> >
  332. : matches_<Expr, BasicExpr, typename G0::proto_grammar>
  333. {
  334. typedef G0 which;
  335. };
  336. // handle degenerate cases of proto::and_
  337. template<typename Expr, typename BasicExpr>
  338. struct matches_<Expr, BasicExpr, and_<> >
  339. : mpl::true_
  340. {};
  341. template<typename Expr, typename BasicExpr, typename G0>
  342. struct matches_<Expr, BasicExpr, and_<G0> >
  343. : matches_<Expr, BasicExpr, typename G0::proto_grammar>
  344. {};
  345. // handle proto::not_
  346. template<typename Expr, typename BasicExpr, typename Grammar>
  347. struct matches_<Expr, BasicExpr, not_<Grammar> >
  348. : mpl::not_<matches_<Expr, BasicExpr, typename Grammar::proto_grammar> >
  349. {};
  350. // handle proto::switch_
  351. template<typename Expr, typename Tag, typename Args, long Arity, typename Cases, typename Transform>
  352. struct matches_<Expr, proto::basic_expr<Tag, Args, Arity>, switch_<Cases, Transform> >
  353. : matches_<
  354. Expr
  355. , proto::basic_expr<Tag, Args, Arity>
  356. , typename Cases::template case_<
  357. typename when<_,Transform>::template impl<Expr,int,int>::result_type
  358. >::proto_grammar
  359. >
  360. {
  361. typedef
  362. typename Cases::template case_<
  363. typename when<_, Transform>::template impl<Expr, int, int>::result_type
  364. >
  365. which;
  366. };
  367. // handle proto::switch_ with the default Transform for specially for better compile times
  368. template<typename Expr, typename Tag, typename Args, long Arity, typename Cases>
  369. struct matches_<Expr, proto::basic_expr<Tag, Args, Arity>, switch_<Cases> >
  370. : matches_<
  371. Expr
  372. , proto::basic_expr<Tag, Args, Arity>
  373. , typename Cases::template case_<Tag>::proto_grammar
  374. >
  375. {
  376. typedef typename Cases::template case_<Tag> which;
  377. };
  378. }
  379. /// \brief A Boolean metafunction that evaluates whether a given
  380. /// expression type matches a grammar.
  381. ///
  382. /// <tt>matches\<Expr,Grammar\></tt> inherits (indirectly) from
  383. /// \c mpl::true_ if <tt>Expr::proto_grammar</tt> matches
  384. /// <tt>Grammar::proto_grammar</tt>, and from \c mpl::false_
  385. /// otherwise.
  386. ///
  387. /// Non-terminal expressions are matched against a grammar
  388. /// according to the following rules:
  389. ///
  390. /// \li The wildcard pattern, \c _, matches any expression.
  391. /// \li An expression <tt>expr\<AT, listN\<A0,A1,...An\> \></tt>
  392. /// matches a grammar <tt>expr\<BT, listN\<B0,B1,...Bn\> \></tt>
  393. /// if \c BT is \c _ or \c AT, and if \c Ax matches \c Bx for
  394. /// each \c x in <tt>[0,n)</tt>.
  395. /// \li An expression <tt>expr\<AT, listN\<A0,...An,U0,...Um\> \></tt>
  396. /// matches a grammar <tt>expr\<BT, listM\<B0,...Bn,vararg\<V\> \> \></tt>
  397. /// if \c BT is \c _ or \c AT, and if \c Ax matches \c Bx
  398. /// for each \c x in <tt>[0,n)</tt> and if \c Ux matches \c V
  399. /// for each \c x in <tt>[0,m)</tt>.
  400. /// \li An expression \c E matches <tt>or_\<B0,B1,...Bn\></tt> if \c E
  401. /// matches some \c Bx for \c x in <tt>[0,n)</tt>.
  402. /// \li An expression \c E matches <tt>and_\<B0,B1,...Bn\></tt> if \c E
  403. /// matches all \c Bx for \c x in <tt>[0,n)</tt>.
  404. /// \li An expression \c E matches <tt>if_\<T,U,V\></tt> if
  405. /// <tt>boost::result_of\<when\<_,T\>(E,int,int)\>::type::value</tt>
  406. /// is \c true and \c E matches \c U; or, if
  407. /// <tt>boost::result_of\<when\<_,T\>(E,int,int)\>::type::value</tt>
  408. /// is \c false and \c E matches \c V. (Note: \c U defaults to \c _
  409. /// and \c V defaults to \c not_\<_\>.)
  410. /// \li An expression \c E matches <tt>not_\<T\></tt> if \c E does
  411. /// not match \c T.
  412. /// \li An expression \c E matches <tt>switch_\<C,T\></tt> if
  413. /// \c E matches <tt>C::case_\<boost::result_of\<T(E)\>::type\></tt>.
  414. /// (Note: T defaults to <tt>tag_of\<_\>()</tt>.)
  415. ///
  416. /// A terminal expression <tt>expr\<AT,term\<A\> \></tt> matches
  417. /// a grammar <tt>expr\<BT,term\<B\> \></tt> if \c BT is \c AT or
  418. /// \c proto::_ and if one of the following is true:
  419. ///
  420. /// \li \c B is the wildcard pattern, \c _
  421. /// \li \c A is \c B
  422. /// \li \c A is <tt>B &</tt>
  423. /// \li \c A is <tt>B const &</tt>
  424. /// \li \c B is <tt>exact\<A\></tt>
  425. /// \li \c B is <tt>convertible_to\<X\></tt> and
  426. /// <tt>is_convertible\<A,X\>::value</tt> is \c true.
  427. /// \li \c A is <tt>X[M]</tt> or <tt>X(&)[M]</tt> and
  428. /// \c B is <tt>X[proto::N]</tt>.
  429. /// \li \c A is <tt>X(&)[M]</tt> and \c B is <tt>X(&)[proto::N]</tt>.
  430. /// \li \c A is <tt>X[M]</tt> or <tt>X(&)[M]</tt> and
  431. /// \c B is <tt>X*</tt>.
  432. /// \li \c B lambda-matches \c A (see below).
  433. ///
  434. /// A type \c B lambda-matches \c A if one of the following is true:
  435. ///
  436. /// \li \c B is \c A
  437. /// \li \c B is the wildcard pattern, \c _
  438. /// \li \c B is <tt>T\<B0,B1,...Bn\></tt> and \c A is
  439. /// <tt>T\<A0,A1,...An\></tt> and for each \c x in
  440. /// <tt>[0,n)</tt>, \c Ax and \c Bx are types
  441. /// such that \c Ax lambda-matches \c Bx
  442. template<typename Expr, typename Grammar>
  443. struct matches
  444. : detail::matches_<
  445. typename Expr::proto_derived_expr
  446. , typename Expr::proto_grammar
  447. , typename Grammar::proto_grammar
  448. >
  449. {};
  450. /// INTERNAL ONLY
  451. ///
  452. template<typename Expr, typename Grammar>
  453. struct matches<Expr &, Grammar>
  454. : detail::matches_<
  455. typename Expr::proto_derived_expr
  456. , typename Expr::proto_grammar
  457. , typename Grammar::proto_grammar
  458. >
  459. {};
  460. /// \brief A wildcard grammar element that matches any expression,
  461. /// and a transform that returns the current expression unchanged.
  462. ///
  463. /// The wildcard type, \c _, is a grammar element such that
  464. /// <tt>matches\<E,_\>::value</tt> is \c true for any expression
  465. /// type \c E.
  466. ///
  467. /// The wildcard can also be used as a stand-in for a template
  468. /// argument when matching terminals. For instance, the following
  469. /// is a grammar that will match any <tt>std::complex\<\></tt>
  470. /// terminal:
  471. ///
  472. /// \code
  473. /// BOOST_MPL_ASSERT((
  474. /// matches<
  475. /// terminal<std::complex<double> >::type
  476. /// , terminal<std::complex< _ > >
  477. /// >
  478. /// ));
  479. /// \endcode
  480. ///
  481. /// When used as a transform, \c _ returns the current expression
  482. /// unchanged. For instance, in the following, \c _ is used with
  483. /// the \c fold\<\> transform to fold the children of a node:
  484. ///
  485. /// \code
  486. /// struct CountChildren
  487. /// : or_<
  488. /// // Terminals have no children
  489. /// when<terminal<_>, mpl::int_<0>()>
  490. /// // Use fold<> to count the children of non-terminals
  491. /// , otherwise<
  492. /// fold<
  493. /// _ // <-- fold the current expression
  494. /// , mpl::int_<0>()
  495. /// , mpl::plus<_state, mpl::int_<1> >()
  496. /// >
  497. /// >
  498. /// >
  499. /// {};
  500. /// \endcode
  501. struct _ : transform<_>
  502. {
  503. typedef _ proto_grammar;
  504. template<typename Expr, typename State, typename Data>
  505. struct impl : transform_impl<Expr, State, Data>
  506. {
  507. typedef Expr result_type;
  508. /// \param expr An expression
  509. /// \return \c e
  510. BOOST_PROTO_RETURN_TYPE_STRICT_LOOSE(result_type, typename impl::expr_param)
  511. operator()(
  512. typename impl::expr_param e
  513. , typename impl::state_param
  514. , typename impl::data_param
  515. ) const
  516. {
  517. return e;
  518. }
  519. };
  520. };
  521. namespace detail
  522. {
  523. template<typename Expr, typename State, typename Data>
  524. struct _and_impl<proto::and_<>, Expr, State, Data>
  525. : proto::_::impl<Expr, State, Data>
  526. {};
  527. template<typename G0, typename Expr, typename State, typename Data>
  528. struct _and_impl<proto::and_<G0>, Expr, State, Data>
  529. : proto::when<proto::_, G0>::template impl<Expr, State, Data>
  530. {};
  531. }
  532. /// \brief Inverts the set of expressions matched by a grammar. When
  533. /// used as a transform, \c not_\<\> returns the current expression
  534. /// unchanged.
  535. ///
  536. /// If an expression type \c E does not match a grammar \c G, then
  537. /// \c E \e does match <tt>not_\<G\></tt>. For example,
  538. /// <tt>not_\<terminal\<_\> \></tt> will match any non-terminal.
  539. template<typename Grammar>
  540. struct not_ : transform<not_<Grammar> >
  541. {
  542. typedef not_ proto_grammar;
  543. template<typename Expr, typename State, typename Data>
  544. struct impl : transform_impl<Expr, State, Data>
  545. {
  546. typedef Expr result_type;
  547. /// \param e An expression
  548. /// \pre <tt>matches\<Expr,not_\>::value</tt> is \c true.
  549. /// \return \c e
  550. BOOST_PROTO_RETURN_TYPE_STRICT_LOOSE(result_type, typename impl::expr_param)
  551. operator()(
  552. typename impl::expr_param e
  553. , typename impl::state_param
  554. , typename impl::data_param
  555. ) const
  556. {
  557. return e;
  558. }
  559. };
  560. };
  561. /// \brief Used to select one grammar or another based on the result
  562. /// of a compile-time Boolean. When used as a transform, \c if_\<\>
  563. /// selects between two transforms based on a compile-time Boolean.
  564. ///
  565. /// When <tt>if_\<If,Then,Else\></tt> is used as a grammar, \c If
  566. /// must be a Proto transform and \c Then and \c Else must be grammars.
  567. /// An expression type \c E matches <tt>if_\<If,Then,Else\></tt> if
  568. /// <tt>boost::result_of\<when\<_,If\>(E,int,int)\>::type::value</tt>
  569. /// is \c true and \c E matches \c U; or, if
  570. /// <tt>boost::result_of\<when\<_,If\>(E,int,int)\>::type::value</tt>
  571. /// is \c false and \c E matches \c V.
  572. ///
  573. /// The template parameter \c Then defaults to \c _
  574. /// and \c Else defaults to \c not\<_\>, so an expression type \c E
  575. /// will match <tt>if_\<If\></tt> if and only if
  576. /// <tt>boost::result_of\<when\<_,If\>(E,int,int)\>::type::value</tt>
  577. /// is \c true.
  578. ///
  579. /// \code
  580. /// // A grammar that only matches integral terminals,
  581. /// // using is_integral<> from Boost.Type_traits.
  582. /// struct IsIntegral
  583. /// : and_<
  584. /// terminal<_>
  585. /// , if_< is_integral<_value>() >
  586. /// >
  587. /// {};
  588. /// \endcode
  589. ///
  590. /// When <tt>if_\<If,Then,Else\></tt> is used as a transform, \c If,
  591. /// \c Then and \c Else must be Proto transforms. When applying
  592. /// the transform to an expression \c E, state \c S and data \c V,
  593. /// if <tt>boost::result_of\<when\<_,If\>(E,S,V)\>::type::value</tt>
  594. /// is \c true then the \c Then transform is applied; otherwise
  595. /// the \c Else transform is applied.
  596. ///
  597. /// \code
  598. /// // Match a terminal. If the terminal is integral, return
  599. /// // mpl::true_; otherwise, return mpl::false_.
  600. /// struct IsIntegral2
  601. /// : when<
  602. /// terminal<_>
  603. /// , if_<
  604. /// is_integral<_value>()
  605. /// , mpl::true_()
  606. /// , mpl::false_()
  607. /// >
  608. /// >
  609. /// {};
  610. /// \endcode
  611. template<
  612. typename If
  613. , typename Then // = _
  614. , typename Else // = not_<_>
  615. >
  616. struct if_ : transform<if_<If, Then, Else> >
  617. {
  618. typedef if_ proto_grammar;
  619. template<typename Expr, typename State, typename Data>
  620. struct impl : transform_impl<Expr, State, Data>
  621. {
  622. typedef
  623. typename when<_, If>::template impl<Expr, State, Data>::result_type
  624. condition;
  625. typedef
  626. typename mpl::if_c<
  627. static_cast<bool>(remove_reference<condition>::type::value)
  628. , when<_, Then>
  629. , when<_, Else>
  630. >::type
  631. which;
  632. typedef typename which::template impl<Expr, State, Data>::result_type result_type;
  633. /// \param e An expression
  634. /// \param s The current state
  635. /// \param d A data of arbitrary type
  636. /// \return <tt>which::impl<Expr, State, Data>()(e, s, d)</tt>
  637. result_type operator ()(
  638. typename impl::expr_param e
  639. , typename impl::state_param s
  640. , typename impl::data_param d
  641. ) const
  642. {
  643. return typename which::template impl<Expr, State, Data>()(e, s, d);
  644. }
  645. };
  646. };
  647. /// \brief For matching one of a set of alternate grammars. Alternates
  648. /// tried in order to avoid ambiguity. When used as a transform, \c or_\<\>
  649. /// applies the transform associated with the first grammar that matches
  650. /// the expression.
  651. ///
  652. /// An expression type \c E matches <tt>or_\<B0,B1,...Bn\></tt> if \c E
  653. /// matches any \c Bx for \c x in <tt>[0,n)</tt>.
  654. ///
  655. /// When applying <tt>or_\<B0,B1,...Bn\></tt> as a transform with an
  656. /// expression \c e of type \c E, state \c s and data \c d, it is
  657. /// equivalent to <tt>Bx()(e, s, d)</tt>, where \c x is the lowest
  658. /// number such that <tt>matches\<E,Bx\>::value</tt> is \c true.
  659. template<BOOST_PROTO_LOGICAL_typename_G>
  660. struct or_ : transform<or_<BOOST_PROTO_LOGICAL_G> >
  661. {
  662. typedef or_ proto_grammar;
  663. /// \param e An expression
  664. /// \param s The current state
  665. /// \param d A data of arbitrary type
  666. /// \pre <tt>matches\<Expr,or_\>::value</tt> is \c true.
  667. /// \return <tt>which()(e, s, d)</tt>, where <tt>which</tt> is the
  668. /// sub-grammar that matched <tt>Expr</tt>.
  669. template<typename Expr, typename State, typename Data>
  670. struct impl
  671. : detail::matches_<
  672. typename Expr::proto_derived_expr
  673. , typename Expr::proto_grammar
  674. , or_
  675. >::which::template impl<Expr, State, Data>
  676. {};
  677. template<typename Expr, typename State, typename Data>
  678. struct impl<Expr &, State, Data>
  679. : detail::matches_<
  680. typename Expr::proto_derived_expr
  681. , typename Expr::proto_grammar
  682. , or_
  683. >::which::template impl<Expr &, State, Data>
  684. {};
  685. };
  686. /// \brief For matching all of a set of grammars. When used as a
  687. /// transform, \c and_\<\> applies the transforms associated with
  688. /// the each grammar in the set, and returns the result of the last.
  689. ///
  690. /// An expression type \c E matches <tt>and_\<B0,B1,...Bn\></tt> if \c E
  691. /// matches all \c Bx for \c x in <tt>[0,n)</tt>.
  692. ///
  693. /// When applying <tt>and_\<B0,B1,...Bn\></tt> as a transform with an
  694. /// expression \c e, state \c s and data \c d, it is
  695. /// equivalent to <tt>(B0()(e, s, d),B1()(e, s, d),...Bn()(e, s, d))</tt>.
  696. template<BOOST_PROTO_LOGICAL_typename_G>
  697. struct and_ : transform<and_<BOOST_PROTO_LOGICAL_G> >
  698. {
  699. typedef and_ proto_grammar;
  700. template<typename Expr, typename State, typename Data>
  701. struct impl
  702. : detail::_and_impl<and_, Expr, State, Data>
  703. {};
  704. };
  705. /// \brief For matching one of a set of alternate grammars, which
  706. /// are looked up based on some property of an expression. The
  707. /// property on which to dispatch is specified by the \c Transform
  708. /// template parameter, which defaults to <tt>tag_of\<_\>()</tt>.
  709. /// That is, when the \c Trannsform is not specified, the alternate
  710. /// grammar is looked up using the tag type of the current expression.
  711. ///
  712. /// When used as a transform, \c switch_\<\> applies the transform
  713. /// associated with the grammar that matches the expression.
  714. ///
  715. /// \note \c switch_\<\> is functionally identical to \c or_\<\> but
  716. /// is often more efficient. It does a fast, O(1) lookup using the
  717. /// result of the specified transform to find a sub-grammar that may
  718. /// potentially match the expression.
  719. ///
  720. /// An expression type \c E matches <tt>switch_\<C,T\></tt> if \c E
  721. /// matches <tt>C::case_\<boost::result_of\<T(E)\>::type\></tt>.
  722. ///
  723. /// When applying <tt>switch_\<C,T\></tt> as a transform with an
  724. /// expression \c e of type \c E, state \c s of type \S and data
  725. /// \c d of type \c D, it is equivalent to
  726. /// <tt>C::case_\<boost::result_of\<T(E,S,D)\>::type\>()(e, s, d)</tt>.
  727. template<typename Cases, typename Transform>
  728. struct switch_ : transform<switch_<Cases, Transform> >
  729. {
  730. typedef switch_ proto_grammar;
  731. template<typename Expr, typename State, typename Data>
  732. struct impl
  733. : Cases::template case_<
  734. typename when<_, Transform>::template impl<Expr, State, Data>::result_type
  735. >::template impl<Expr, State, Data>
  736. {};
  737. };
  738. /// INTERNAL ONLY (This is merely a compile-time optimization for the common case)
  739. ///
  740. template<typename Cases>
  741. struct switch_<Cases> : transform<switch_<Cases> >
  742. {
  743. typedef switch_ proto_grammar;
  744. template<typename Expr, typename State, typename Data>
  745. struct impl
  746. : Cases::template case_<typename Expr::proto_tag>::template impl<Expr, State, Data>
  747. {};
  748. template<typename Expr, typename State, typename Data>
  749. struct impl<Expr &, State, Data>
  750. : Cases::template case_<typename Expr::proto_tag>::template impl<Expr &, State, Data>
  751. {};
  752. };
  753. /// \brief For forcing exact matches of terminal types.
  754. ///
  755. /// By default, matching terminals ignores references and
  756. /// cv-qualifiers. For instance, a terminal expression of
  757. /// type <tt>terminal\<int const &\>::type</tt> will match
  758. /// the grammar <tt>terminal\<int\></tt>. If that is not
  759. /// desired, you can force an exact match with
  760. /// <tt>terminal\<exact\<int\> \></tt>. This will only
  761. /// match integer terminals where the terminal is held by
  762. /// value.
  763. template<typename T>
  764. struct exact
  765. {};
  766. /// \brief For matching terminals that are convertible to
  767. /// a type.
  768. ///
  769. /// Use \c convertible_to\<\> to match a terminal that is
  770. /// convertible to some type. For example, the grammar
  771. /// <tt>terminal\<convertible_to\<int\> \></tt> will match
  772. /// any terminal whose argument is convertible to an integer.
  773. ///
  774. /// \note The trait \c is_convertible\<\> from Boost.Type_traits
  775. /// is used to determinal convertibility.
  776. template<typename T>
  777. struct convertible_to
  778. {};
  779. /// \brief For matching a Grammar to a variable number of
  780. /// sub-expressions.
  781. ///
  782. /// An expression type <tt>expr\<AT, listN\<A0,...An,U0,...Um\> \></tt>
  783. /// matches a grammar <tt>expr\<BT, listM\<B0,...Bn,vararg\<V\> \> \></tt>
  784. /// if \c BT is \c _ or \c AT, and if \c Ax matches \c Bx
  785. /// for each \c x in <tt>[0,n)</tt> and if \c Ux matches \c V
  786. /// for each \c x in <tt>[0,m)</tt>.
  787. ///
  788. /// For example:
  789. ///
  790. /// \code
  791. /// // Match any function call expression, irregardless
  792. /// // of the number of function arguments:
  793. /// struct Function
  794. /// : function< vararg<_> >
  795. /// {};
  796. /// \endcode
  797. ///
  798. /// When used as a transform, <tt>vararg\<G\></tt> applies
  799. /// <tt>G</tt>'s transform.
  800. template<typename Grammar>
  801. struct vararg
  802. : Grammar
  803. {
  804. /// INTERNAL ONLY
  805. typedef void proto_is_vararg_;
  806. };
  807. /// INTERNAL ONLY
  808. ///
  809. template<BOOST_PROTO_LOGICAL_typename_G>
  810. struct is_callable<or_<BOOST_PROTO_LOGICAL_G> >
  811. : mpl::true_
  812. {};
  813. /// INTERNAL ONLY
  814. ///
  815. template<BOOST_PROTO_LOGICAL_typename_G>
  816. struct is_callable<and_<BOOST_PROTO_LOGICAL_G> >
  817. : mpl::true_
  818. {};
  819. /// INTERNAL ONLY
  820. ///
  821. template<typename Grammar>
  822. struct is_callable<not_<Grammar> >
  823. : mpl::true_
  824. {};
  825. /// INTERNAL ONLY
  826. ///
  827. template<typename If, typename Then, typename Else>
  828. struct is_callable<if_<If, Then, Else> >
  829. : mpl::true_
  830. {};
  831. /// INTERNAL ONLY
  832. ///
  833. template<typename Grammar>
  834. struct is_callable<vararg<Grammar> >
  835. : mpl::true_
  836. {};
  837. /// INTERNAL ONLY
  838. ///
  839. template<typename Cases, typename Transform>
  840. struct is_callable<switch_<Cases, Transform> >
  841. : mpl::true_
  842. {};
  843. }}
  844. #undef BOOST_PROTO_LOGICAL_typename_G
  845. #undef BOOST_PROTO_LOGICAL_G
  846. #if defined(_MSC_VER)
  847. # pragma warning(pop)
  848. #endif
  849. #endif