Debian Codegraph @

cutl/compiler/traversal.hxx

raw
1 // file : cutl/compiler/traversal.hxx
2 // copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC
3 // license : MIT; see accompanying LICENSE file
4
5 #ifndef CUTL_COMPILER_TRAVERSAL_HXX
6 #define CUTL_COMPILER_TRAVERSAL_HXX
7
8 #include <map>
9 #include <set>
10 #include <vector>
11
12 #include <cutl/compiler/type-info.hxx>
13
14 namespace cutl
15 {
16 namespace compiler
17 {
18 //
19 //
20 template<typename B>
21 class traverser
22 {
23 public:
24 virtual
25 ~traverser ();
26
27 virtual void
28 trampoline (B&) = 0;
29 };
30
31 //
32 //
33 template<typename B>
34 class traverser_map
35 {
36 public:
37 typedef std::vector<traverser<B>*> traversers;
38
39 struct map_type: std::map<type_id, traversers>
40 {
41 map_type () {}
42
43 // Don't copy traverser maps. We do it here instead of in
44 // traverser_map to pacify GCC's -Wextra insisting we must
45 // explicitly initialize virtual bases in copy constructor.
46 //
47 map_type (map_type const&): std::map<type_id, traversers> () {}
48 map_type& operator= (map_type const&) {return *this;}
49 };
50
51 typedef typename map_type::const_iterator iterator;
52
53 iterator
54 begin () const
55 {
56 return map_.begin ();
57 }
58
59 iterator
60 end () const
61 {
62 return map_.end ();
63 }
64
65 void
66 add (type_id const& id, traverser<B>& t)
67 {
68 traversers& travs (map_[id]);
69 travs.push_back (&t);
70 }
71
72 protected:
73 map_type map_;
74 };
75
76 //
77 //
78 template <typename X, typename B>
79 class traverser_impl: public traverser<B>,
80 public virtual traverser_map<B>
81 {
82 public:
83 typedef X type;
84
85 traverser_impl ()
86 {
87 this->add (typeid (type), *this);
88 }
89
90 traverser_impl (traverser_impl const&)
91 {
92 this->add (typeid (type), *this);
93 }
94
95 virtual void
96 traverse (type&) = 0;
97
98 public:
99 virtual void
100 trampoline (B&);
101 };
102
103 //
104 //
105 template <typename B>
106 class dispatcher: public virtual traverser_map<B>
107 {
108 public:
109 virtual
110 ~dispatcher ();
111
112 void
113 traverser (traverser_map<B>&);
114
115 virtual void
116 dispatch (B&);
117
118 public:
119 template <typename I, typename X>
120 static void
121 iterate_and_dispatch (I begin, I end, dispatcher<X>& d)
122 {
123 for (; begin != end; ++begin)
124 {
125 d.dispatch (*begin);
126 }
127 }
128
129 template <typename T, typename A, typename I, typename X>
130 static void
131 iterate_and_dispatch (I begin,
132 I end,
133 dispatcher<X>& d,
134 T& t,
135 void (T::*next)(A&),
136 A& a)
137 {
138 for (; begin != end;)
139 {
140 d.dispatch (*begin);
141
142 if (++begin != end && next != 0)
143 (t.*next) (a);
144 }
145 }
146
147 private:
148 struct comparator
149 {
150 bool
151 operator () (type_info const& a, type_info const& b) const
152 {
153 return a.type_id () < b.type_id ();
154 }
155 };
156
157 typedef std::map<type_info, std::size_t, comparator> level_map;
158 typedef std::set<type_info, comparator> type_info_set;
159
160 static std::size_t
161 compute_levels (type_info const&, std::size_t current, level_map&);
162
163 static void
164 flatten_tree (type_info const&, type_info_set&);
165 };
166 }
167 }
168
169 #include <cutl/compiler/traversal.txx>
170
171 #endif // CUTL_COMPILER_TRAVERSAL_HXX