// file : cutl/compiler/traversal.hxx // author : Boris Kolpackov // copyright : Copyright (c) 2009 Code Synthesis Tools CC // license : MIT; see accompanying LICENSE file #ifndef CUTL_COMPILER_TRAVERSAL_HXX #define CUTL_COMPILER_TRAVERSAL_HXX #include #include #include #include namespace cutl { namespace compiler { // // template class traverser { public: virtual ~traverser (); virtual void trampoline (B&) = 0; }; // // template class traverser_map { public: typedef std::vector*> traversers; typedef std::map map_type; typedef typename map_type::const_iterator iterator; iterator begin () const { return map_.begin (); } iterator end () const { return map_.end (); } void add (type_id const& id, traverser& t) { traversers& travs (map_[id]); travs.push_back (&t); } protected: map_type map_; }; // // template class traverser_impl: public traverser, public virtual traverser_map { public: typedef X type; traverser_impl () { add (typeid (type), *this); } virtual void traverse (type&) = 0; public: virtual void trampoline (B&); }; // // template class dispatcher: public virtual traverser_map { public: virtual ~dispatcher (); void traverser (traverser_map&); virtual void dispatch (B&); public: template static void iterate_and_dispatch (I begin, I end, dispatcher& d) { for (; begin != end; ++begin) { d.dispatch (*begin); } } template static void iterate_and_dispatch (I begin, I end, dispatcher& d, T& t, void (T::*next)(A&), A& a) { for (; begin != end;) { d.dispatch (*begin); if (++begin != end && next != 0) (t.*next) (a); } } private: struct comparator { bool operator () (type_info const& a, type_info const& b) const { return a.type_id () < b.type_id (); } }; typedef std::map level_map; typedef std::set type_info_set; static std::size_t compute_levels (type_info const&, std::size_t current, level_map&); static void flatten_tree (type_info const&, type_info_set&); }; } } #include #endif // CUTL_COMPILER_TRAVERSAL_HXX