summaryrefslogtreecommitdiff
path: root/libcutl/cutl/compiler/traversal.hxx
blob: e0b6d77a50bfc2d1a227598e7e00c04ed61c2ad7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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 <map>
#include <set>
#include <vector>

#include <cutl/compiler/type-info.hxx>

namespace cutl
{
  namespace compiler
  {
    //
    //
    template<typename B>
    class traverser
    {
    public:
      virtual
      ~traverser ();

      virtual void
      trampoline (B&) = 0;
    };

    //
    //
    template<typename B>
    class traverser_map
    {
    public:
      typedef std::vector<traverser<B>*> traversers;

      struct map_type: std::map<type_id, traversers>
      {
        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<type_id, traversers> () {}
        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<B>& t)
      {
        traversers& travs (map_[id]);
        travs.push_back (&t);
      }

    protected:
      map_type map_;
    };

    //
    //
    template <typename X, typename B>
    class traverser_impl: public traverser<B>,
                          public virtual traverser_map<B>
    {
    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 <typename B>
    class dispatcher: public virtual traverser_map<B>
    {
    public:
      virtual
      ~dispatcher ();

      void
      traverser (traverser_map<B>&);

      virtual void
      dispatch (B&);

    public:
      template <typename I, typename X>
      static void
      iterate_and_dispatch (I begin, I end, dispatcher<X>& d)
      {
        for (; begin != end; ++begin)
        {
          d.dispatch (*begin);
        }
      }

      template <typename T, typename A, typename I, typename X>
      static void
      iterate_and_dispatch (I begin,
                            I end,
                            dispatcher<X>& 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<type_info, std::size_t, comparator> level_map;
      typedef std::set<type_info, comparator> 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 <cutl/compiler/traversal.txx>

#endif // CUTL_COMPILER_TRAVERSAL_HXX