From 8286ac511144e4f17d34eac9affb97e50646344a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Frings-F=C3=BCrst?= Date: Wed, 23 Jul 2014 15:25:44 +0200 Subject: Imported Upstream version 4.0.0 --- libcutl/cutl/compiler/traversal.hxx | 171 ++++++++++++++++++++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 libcutl/cutl/compiler/traversal.hxx (limited to 'libcutl/cutl/compiler/traversal.hxx') diff --git a/libcutl/cutl/compiler/traversal.hxx b/libcutl/cutl/compiler/traversal.hxx new file mode 100644 index 0000000..e0b6d77 --- /dev/null +++ b/libcutl/cutl/compiler/traversal.hxx @@ -0,0 +1,171 @@ +// file : cutl/compiler/traversal.hxx +// copyright : Copyright (c) 2009-2013 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; + + struct map_type: std::map + { + map_type () {} + + // Don't copy traverser maps. We do it here instead of in + // traverser_map to pacify GCC's -Wextra insisting we must + // explicitly initialize virtual bases in copy constructor. + // + map_type (map_type const&): std::map () {} + map_type& operator= (map_type const&) {return *this;} + }; + + 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 () + { + this->add (typeid (type), *this); + } + + traverser_impl (traverser_impl const&) + { + this->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.2.3