From cb9ea47e7825b5073d4d645afb94f6326cb7cf4d Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Sun, 6 Sep 2009 12:52:53 +0200 Subject: Start the libcutl repository --- cutl/compiler/traversal.hxx | 155 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100644 cutl/compiler/traversal.hxx (limited to 'cutl/compiler/traversal.hxx') diff --git a/cutl/compiler/traversal.hxx b/cutl/compiler/traversal.hxx new file mode 100644 index 0000000..ad677b0 --- /dev/null +++ b/cutl/compiler/traversal.hxx @@ -0,0 +1,155 @@ +// 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 -- cgit v1.1