summaryrefslogtreecommitdiff
path: root/libfrontend-elements/frontend-elements/traversal.hxx
blob: b3204841cfd12d976fa92a3e25e2f890a5772a45 (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// file      : frontend-elements/traversal.hxx
// author    : Boris Kolpackov <boris@kolpackov.net>
// copyright : Copyright (c) 2005-2010 Boris Kolpackov
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

#ifndef FRONTEND_ELEMENTS_TRAVERSAL_HXX
#define FRONTEND_ELEMENTS_TRAVERSAL_HXX

#include <frontend-elements/types.hxx>

#include <cult/containers/map.hxx>
#include <cult/containers/set.hxx>
#include <cult/containers/vector.hxx>

#include <cult/rtti/type-info.hxx>

#include <cult/trace/stream.hxx>

//@@ Move to trace next time you are about to uncomment this.
//
// #include <iostream>
// using std::wcerr;
// using std::endl;


namespace FrontendElements
{
  namespace Traversal
  {
    //
    //
    template<typename X>
    class TraverserBase
    {
    protected:
      virtual
      ~TraverserBase ();

      virtual Void
      trampoline (X&) = 0;

      virtual Void
      trampoline (X const&) = 0;

      template <typename>
      friend class DispatcherBase;
    };


    //
    //
    template <typename X>
    class DispatcherBase
    {
      typedef
      Cult::RTTI::TypeId
      TypeId;

      typedef
      Cult::RTTI::TypeInfo
      TypeInfo;

    public:
      virtual
      ~DispatcherBase ();

      virtual Void
      dispatch (X&);

      virtual Void
      dispatch (X const&);

      Void
      map (TypeId id, TraverserBase<X>& t)
      {
        //wcerr << "map for " << id.name () << " to " << &t
	//      << " in " << &traversal_map_ << endl;

        Traversers& traversers (traversal_map_[id]);
        traversers.push_back (&t);
      }

    public:
      typedef
      Cult::Containers::Vector<TraverserBase<X>*>
      Traversers;

      typedef
      Cult::Containers::Map<TypeId, Traversers>
      TraversalMap;

      typedef
      typename TraversalMap::ConstIterator
      Iterator;

      Iterator
      begin () const
      {
        return traversal_map_.begin ();
      }

      Iterator
      end () const
      {
        return traversal_map_.end ();
      }

    protected:
      static Cult::Trace::Stream tout;

    private:
      template<typename Y>
      Void
      dispatch_ (Y&);

    private:

      struct TypeInfoComparator
      {
        Boolean
        operator () (TypeInfo const& a, TypeInfo const& b) const
        {
          return a.type_id () < b.type_id ();
        }
      };

      typedef
      Cult::Containers::Map<TypeInfo, UnsignedLong, TypeInfoComparator>
      LevelMap;

      typedef
      Cult::Containers::Set<TypeInfo, TypeInfoComparator>
      TypeInfoSet;

      static UnsignedLong
      compute_levels (TypeInfo const& ti, UnsignedLong cur, LevelMap& map);

      static Void
      flatten_tree (TypeInfo const& ti, TypeInfoSet& set);

    private:
      TraversalMap traversal_map_;
    };


    //
    //
    template <typename X>
    class Dispatcher: public virtual DispatcherBase<X>
    {
    public:
      Dispatcher ()
          : merge_ (true)
      {
      }

      Void
      traverser (DispatcherBase<X>& d)
      {
        for (typename DispatcherBase<X>::Iterator
               i (d.begin ()), e (d.end ()); i != e; ++i)
        {
          for (typename DispatcherBase<X>::Traversers::ConstIterator
                 t (i->second.begin ()), e (i->second.end ()); t != e; ++t)
          {
            dispatcher_.map (i->first, **t);
          }
        }
      }

    public:
      virtual Void
      dispatch (X& x)
      {
        merge ();
        dispatcher_.dispatch (x);
      }

      virtual Void
      dispatch (X const& x)
      {
        merge ();
        dispatcher_.dispatch (x);
      }

      using DispatcherBase<X>::begin;
      using DispatcherBase<X>::end;

    private:
      Void
      merge ()
      {
        if (merge_)
        {
          for (typename DispatcherBase<X>::Iterator
                 i (begin ()), e (end ()); i != e; ++i)
          {
            for (typename DispatcherBase<X>::Traversers::ConstIterator
                   t (i->second.begin ()), e (i->second.end ()); t != e; ++t)
            {
              dispatcher_.map (i->first, **t);
            }
          }

          merge_ = false;
        }
      }

    protected:
      template <typename I, typename Y>
      Void
      iterate_and_dispatch (I begin, I end, DispatcherBase<Y>& d)
      {
        for (; begin != end; ++begin)
        {
          d.dispatch (*begin);
        }
      }

      template <typename T, typename A, typename I, typename Y>
      Void
      iterate_and_dispatch (I begin,
                            I end,
                            DispatcherBase<Y>& d,
                            T& t,
                            Void (T::*next)(A&),
                            A& a)
      {
        for (; begin != end;)
        {
          d.dispatch (*begin);

          if (++begin != end && next != 0)
            (t.*next) (a);
        }
      }

    private:
      Boolean merge_;
      DispatcherBase<X> dispatcher_;
    };


    //
    //
    template <typename T, typename X>
    struct Traverser : TraverserBase<X>, virtual Dispatcher<X>
    {
      typedef T Type;

      Traverser ()
      {
        map (typeid (Type), *this);
      }

      virtual Void
      traverse (Type&)
      {
        abort ();
      }

      virtual Void
      traverse (Type const&)
      {
        abort ();
      }

    protected:
      virtual Void
      trampoline (X& x)
      {
        //wcerr << "trampoline for " << &i << " to type "
        //      << typeid (type).name () << endl;

        traverse (dynamic_cast<Type&> (x));
      }

      virtual Void
      trampoline (X const& x)
      {
        //wcerr << "trampoline for " << &i << " to type "
        //      << typeid (Type).name () << endl;

        traverse (dynamic_cast<Type const&> (x));
      }
    };
  }
}

#include <frontend-elements/traversal.ixx>
#include <frontend-elements/traversal.txx>

#endif  // FRONTEND_ELEMENTS_TRAVERSAL_HXX