1 ///////////////////////////////////////////////////////////////////////////////
3 /// Utilities for debugging Proto expression trees
5 // Copyright 2008 Eric Niebler. Distributed under the Boost
6 // Software License, Version 1.0. (See accompanying file
7 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 #ifndef BOOST_PROTO_DEBUG_HPP_EAN_12_31_2006
10 #define BOOST_PROTO_DEBUG_HPP_EAN_12_31_2006
13 #include <boost/preprocessor/stringize.hpp>
14 #include <boost/core/ref.hpp>
15 #include <boost/core/typeinfo.hpp>
16 #include <boost/mpl/assert.hpp>
17 #include <boost/proto/proto_fwd.hpp>
18 #include <boost/proto/traits.hpp>
19 #include <boost/proto/matches.hpp>
20 #include <boost/proto/fusion.hpp>
21 #include <boost/fusion/algorithm/iteration/for_each.hpp>
23 namespace boost { namespace proto
25 namespace tagns_ { namespace tag
27 #define BOOST_PROTO_DEFINE_TAG_INSERTION(Tag) \
28 /** \brief INTERNAL ONLY */ \
29 inline std::ostream &operator <<(std::ostream &sout, Tag const &) \
31 return sout << BOOST_PP_STRINGIZE(Tag); \
35 BOOST_PROTO_DEFINE_TAG_INSERTION(terminal)
36 BOOST_PROTO_DEFINE_TAG_INSERTION(unary_plus)
37 BOOST_PROTO_DEFINE_TAG_INSERTION(negate)
38 BOOST_PROTO_DEFINE_TAG_INSERTION(dereference)
39 BOOST_PROTO_DEFINE_TAG_INSERTION(complement)
40 BOOST_PROTO_DEFINE_TAG_INSERTION(address_of)
41 BOOST_PROTO_DEFINE_TAG_INSERTION(logical_not)
42 BOOST_PROTO_DEFINE_TAG_INSERTION(pre_inc)
43 BOOST_PROTO_DEFINE_TAG_INSERTION(pre_dec)
44 BOOST_PROTO_DEFINE_TAG_INSERTION(post_inc)
45 BOOST_PROTO_DEFINE_TAG_INSERTION(post_dec)
46 BOOST_PROTO_DEFINE_TAG_INSERTION(shift_left)
47 BOOST_PROTO_DEFINE_TAG_INSERTION(shift_right)
48 BOOST_PROTO_DEFINE_TAG_INSERTION(multiplies)
49 BOOST_PROTO_DEFINE_TAG_INSERTION(divides)
50 BOOST_PROTO_DEFINE_TAG_INSERTION(modulus)
51 BOOST_PROTO_DEFINE_TAG_INSERTION(plus)
52 BOOST_PROTO_DEFINE_TAG_INSERTION(minus)
53 BOOST_PROTO_DEFINE_TAG_INSERTION(less)
54 BOOST_PROTO_DEFINE_TAG_INSERTION(greater)
55 BOOST_PROTO_DEFINE_TAG_INSERTION(less_equal)
56 BOOST_PROTO_DEFINE_TAG_INSERTION(greater_equal)
57 BOOST_PROTO_DEFINE_TAG_INSERTION(equal_to)
58 BOOST_PROTO_DEFINE_TAG_INSERTION(not_equal_to)
59 BOOST_PROTO_DEFINE_TAG_INSERTION(logical_or)
60 BOOST_PROTO_DEFINE_TAG_INSERTION(logical_and)
61 BOOST_PROTO_DEFINE_TAG_INSERTION(bitwise_and)
62 BOOST_PROTO_DEFINE_TAG_INSERTION(bitwise_or)
63 BOOST_PROTO_DEFINE_TAG_INSERTION(bitwise_xor)
64 BOOST_PROTO_DEFINE_TAG_INSERTION(comma)
65 BOOST_PROTO_DEFINE_TAG_INSERTION(mem_ptr)
66 BOOST_PROTO_DEFINE_TAG_INSERTION(assign)
67 BOOST_PROTO_DEFINE_TAG_INSERTION(shift_left_assign)
68 BOOST_PROTO_DEFINE_TAG_INSERTION(shift_right_assign)
69 BOOST_PROTO_DEFINE_TAG_INSERTION(multiplies_assign)
70 BOOST_PROTO_DEFINE_TAG_INSERTION(divides_assign)
71 BOOST_PROTO_DEFINE_TAG_INSERTION(modulus_assign)
72 BOOST_PROTO_DEFINE_TAG_INSERTION(plus_assign)
73 BOOST_PROTO_DEFINE_TAG_INSERTION(minus_assign)
74 BOOST_PROTO_DEFINE_TAG_INSERTION(bitwise_and_assign)
75 BOOST_PROTO_DEFINE_TAG_INSERTION(bitwise_or_assign)
76 BOOST_PROTO_DEFINE_TAG_INSERTION(bitwise_xor_assign)
77 BOOST_PROTO_DEFINE_TAG_INSERTION(subscript)
78 BOOST_PROTO_DEFINE_TAG_INSERTION(member)
79 BOOST_PROTO_DEFINE_TAG_INSERTION(if_else_)
80 BOOST_PROTO_DEFINE_TAG_INSERTION(function)
82 #undef BOOST_PROTO_DEFINE_TAG_INSERTION
85 namespace hidden_detail_
87 struct ostream_wrapper
89 ostream_wrapper(std::ostream &sout)
95 BOOST_DELETED_FUNCTION(ostream_wrapper &operator =(ostream_wrapper const &))
102 : name_(BOOST_CORE_TYPEID(T).name())
108 inline std::ostream &operator <<(ostream_wrapper sout_wrap, named_any t)
110 return sout_wrap.sout_ << t.name_;
116 // copyable functor to pass by value to fusion::foreach
117 struct display_expr_impl;
118 struct display_expr_impl_functor
120 display_expr_impl_functor(display_expr_impl const& impl): impl_(impl)
123 template<typename Expr>
124 void operator()(Expr const &expr) const
130 display_expr_impl const& impl_;
133 struct display_expr_impl
135 explicit display_expr_impl(std::ostream &sout, int depth = 0)
141 template<typename Expr>
142 void operator()(Expr const &expr) const
144 this->impl(expr, mpl::long_<arity_of<Expr>::value>());
147 BOOST_DELETED_FUNCTION(display_expr_impl(display_expr_impl const &))
148 BOOST_DELETED_FUNCTION(display_expr_impl &operator =(display_expr_impl const &))
151 template<typename Expr>
152 void impl(Expr const &expr, mpl::long_<0>) const
154 using namespace hidden_detail_;
155 typedef typename tag_of<Expr>::type tag;
156 this->sout_.width(this->depth_);
157 this->sout_ << (this->first_? "" : ", ");
158 this->sout_ << tag() << "(" << proto::value(expr) << ")\n";
159 this->first_ = false;
162 template<typename Expr, typename Arity>
163 void impl(Expr const &expr, Arity) const
165 using namespace hidden_detail_;
166 typedef typename tag_of<Expr>::type tag;
167 this->sout_.width(this->depth_);
168 this->sout_ << (this->first_? "" : ", ");
169 this->sout_ << tag() << "(\n";
170 display_expr_impl display(this->sout_, this->depth_ + 4);
171 fusion::for_each(expr, display_expr_impl_functor(display));
172 this->sout_.width(this->depth_);
173 this->sout_ << "" << ")\n";
174 this->first_ = false;
185 /// \brief Pretty-print a Proto expression tree.
187 /// A PolymorphicFunctionObject which accepts a Proto expression
188 /// tree and pretty-prints it to an \c ostream for debugging
192 BOOST_PROTO_CALLABLE()
194 typedef void result_type;
196 /// \param sout The \c ostream to which the expression tree
198 /// \param depth The starting indentation depth for this node.
199 /// Children nodes will be displayed at a starting
200 /// depth of <tt>depth+4</tt>.
201 explicit display_expr(std::ostream &sout = std::cout, int depth = 0)
206 /// \brief Pretty-print the current node in a Proto expression
208 template<typename Expr>
209 void operator()(Expr const &expr) const
211 detail::display_expr_impl(this->sout_, this->depth_)(expr);
216 reference_wrapper<std::ostream> sout_;
220 /// \brief Pretty-print a Proto expression tree.
222 /// \note Equivalent to <tt>functional::display_expr(0, sout)(expr)</tt>
223 /// \param expr The Proto expression tree to pretty-print
224 /// \param sout The \c ostream to which the output should be
225 /// written. If not specified, defaults to
226 /// <tt>std::cout</tt>.
227 template<typename Expr>
228 void display_expr(Expr const &expr, std::ostream &sout)
230 functional::display_expr(sout, 0)(expr);
235 template<typename Expr>
236 void display_expr(Expr const &expr)
238 functional::display_expr()(expr);
241 /// \brief Assert at compile time that a particular expression
242 /// matches the specified grammar.
244 /// \note Equivalent to <tt>BOOST_MPL_ASSERT((proto::matches\<Expr, Grammar\>))</tt>
245 /// \param expr The Proto expression to check againts <tt>Grammar</tt>
246 template<typename Grammar, typename Expr>
247 void assert_matches(Expr const & /*expr*/)
249 BOOST_MPL_ASSERT((proto::matches<Expr, Grammar>));
252 /// \brief Assert at compile time that a particular expression
253 /// does not match the specified grammar.
255 /// \note Equivalent to <tt>BOOST_MPL_ASSERT_NOT((proto::matches\<Expr, Grammar\>))</tt>
256 /// \param expr The Proto expression to check againts <tt>Grammar</tt>
257 template<typename Grammar, typename Expr>
258 void assert_matches_not(Expr const & /*expr*/)
260 BOOST_MPL_ASSERT_NOT((proto::matches<Expr, Grammar>));
263 /// \brief Assert at compile time that a particular expression
264 /// matches the specified grammar.
266 /// \note Equivalent to <tt>proto::assert_matches\<Grammar\>(Expr)</tt>
267 /// \param Expr The Proto expression to check againts <tt>Grammar</tt>
268 /// \param Grammar The grammar used to validate Expr.
269 #define BOOST_PROTO_ASSERT_MATCHES(Expr, Grammar) \
270 (true ? (void)0 : boost::proto::assert_matches<Grammar>(Expr))
272 /// \brief Assert at compile time that a particular expression
273 /// does not match the specified grammar.
275 /// \note Equivalent to <tt>proto::assert_matches_not\<Grammar\>(Expr)</tt>
276 /// \param Expr The Proto expression to check againts <tt>Grammar</tt>
277 /// \param Grammar The grammar used to validate Expr.
278 #define BOOST_PROTO_ASSERT_MATCHES_NOT(Expr, Grammar) \
279 (true ? (void)0 : boost::proto::assert_matches_not<Grammar>(Expr))