]> git.saurik.com Git - bison.git/blob - data/variant.hh
c++: use // comments in the output
[bison.git] / data / variant.hh
1 # C++ skeleton for Bison
2
3 # Copyright (C) 2002-2012 Free Software Foundation, Inc.
4
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18
19 ## --------- ##
20 ## variant. ##
21 ## --------- ##
22
23 # b4_symbol_variant(YYTYPE, YYVAL, ACTION, [ARGS])
24 # ------------------------------------------------
25 # Run some ACTION ("build", or "destroy") on YYVAL of symbol type
26 # YYTYPE.
27 m4_define([b4_symbol_variant],
28 [m4_pushdef([b4_dollar_dollar],
29 [$2.$3< $][3 >(m4_shift3($@))])dnl
30 switch ($1)
31 {
32 b4_type_foreach([b4_type_action_])[]dnl
33 default:
34 break;
35 }
36 m4_popdef([b4_dollar_dollar])dnl
37 ])
38
39
40 # _b4_char_sizeof_counter
41 # -----------------------
42 # A counter used by _b4_char_sizeof_dummy to create fresh symbols.
43 m4_define([_b4_char_sizeof_counter],
44 [0])
45
46 # _b4_char_sizeof_dummy
47 # ---------------------
48 # At each call return a new C++ identifier.
49 m4_define([_b4_char_sizeof_dummy],
50 [m4_define([_b4_char_sizeof_counter], m4_incr(_b4_char_sizeof_counter))dnl
51 dummy[]_b4_char_sizeof_counter])
52
53
54 # b4_char_sizeof_(SYMBOL-NUM)
55 # ---------------------------
56 # A comment describing this symbol.
57 m4_define([b4_char_sizeof_],
58 [ // b4_symbol([$1], [tag])
59 ])
60
61 # b4_char_sizeof(SYMBOL-NUMS)
62 # ---------------------------
63 # To be mapped on the list of type names to produce:
64 #
65 # char dummy1[sizeof(type_name_1)];
66 # char dummy2[sizeof(type_name_2)];
67 #
68 # for defined type names.
69 m4_define([b4_char_sizeof],
70 [b4_symbol_if([$1], [has_type],
71 [
72 m4_map([b4_char_sizeof_], [$@])dnl
73 char _b4_char_sizeof_dummy@{sizeof([b4_symbol([$1], [type])])@};
74 ])])
75
76
77 # b4_variant_includes
78 # -------------------
79 # The needed includes for variants support.
80 m4_define([b4_variant_includes],
81 [b4_parse_assert_if([[#include <typeinfo>]])[
82 #include <cstdlib> // abort
83 #ifndef YYASSERT
84 # include <cassert>
85 # define YYASSERT assert
86 #endif
87 ]])
88
89 # b4_variant_define
90 # -----------------
91 # Define "variant".
92 m4_define([b4_variant_define],
93 [[ /// A char[S] buffer to store and retrieve objects.
94 ///
95 /// Sort of a variant, but does not keep track of the nature
96 /// of the stored data, since that knowledge is available
97 /// via the current state.
98 template <size_t S>
99 struct variant
100 {
101 /// Type of *this.
102 typedef variant<S> self_type;
103
104 /// Empty construction.
105 inline
106 variant ()]b4_parse_assert_if([
107 : built (false)
108 , tname (YY_NULL)])[
109 {}
110
111 /// Instantiate a \a T in here.
112 template <typename T>
113 inline T&
114 build ()
115 {]b4_parse_assert_if([
116 YYASSERT (!built);
117 YYASSERT (!tname);
118 YYASSERT (sizeof (T) <= S);
119 built = true;
120 tname = typeid (T).name ();])[
121 return *new (buffer.raw) T;
122 }
123
124 /// Instantiate a \a T in here from \a t.
125 template <typename T>
126 inline T&
127 build (const T& t)
128 {]b4_parse_assert_if([
129 YYASSERT (!built);
130 YYASSERT (!tname);
131 YYASSERT (sizeof (T) <= S);
132 built = true;
133 tname = typeid (T).name ();])[
134 return *new (buffer.raw) T (t);
135 }
136
137 /// Construct and fill.
138 template <typename T>
139 inline
140 variant (const T& t)]b4_parse_assert_if([
141 : built (true)
142 , tname (typeid (T).name ())])[
143 {
144 YYASSERT (sizeof (T) <= S);
145 new (buffer.raw) T (t);
146 }
147
148 /// Accessor to a built \a T.
149 template <typename T>
150 inline T&
151 as ()
152 {]b4_parse_assert_if([
153 YYASSERT (built);
154 YYASSERT (tname == typeid (T).name ());
155 YYASSERT (sizeof (T) <= S);])[
156 return reinterpret_cast<T&> (buffer.raw);
157 }
158
159 /// Const accessor to a built \a T (for %printer).
160 template <typename T>
161 inline const T&
162 as () const
163 {]b4_parse_assert_if([
164 YYASSERT (built);
165 YYASSERT (tname == typeid (T).name ());
166 YYASSERT (sizeof (T) <= S);])[
167 return reinterpret_cast<const T&> (buffer.raw);
168 }
169
170 /// Swap the content with \a other, of same type.
171 template <typename T>
172 inline void
173 swap (variant<S>& other)
174 {]b4_parse_assert_if([
175 YYASSERT (tname == other.tname);])[
176 std::swap (as<T>(), other.as<T>());]b4_parse_assert_if([
177 std::swap (built, other.built);
178 std::swap (tname, other.tname);])[
179 }
180
181 /// Assign the content of \a other to this.
182 /// Destroys \a other.
183 template <typename T>
184 inline void
185 build (variant<S>& other)
186 {
187 build<T>();
188 swap<T>(other);
189 other.destroy<T>();
190 }
191
192 /// Copy the content of \a other to this.
193 /// Destroys \a other.
194 template <typename T>
195 inline void
196 copy (const variant<S>& other)
197 {
198 build<T> (other.as<T> ());
199 }
200
201 /// Destroy the stored \a T.
202 template <typename T>
203 inline void
204 destroy ()
205 {
206 as<T> ().~T ();]b4_parse_assert_if([
207 built = false;
208 tname = YY_NULL;])[
209 }
210
211 /// Prohibit blind copies.
212 // private:
213 self_type& operator=(const self_type&)
214 {
215 abort ();
216 }
217
218 private:
219 /// A buffer large enough to store any of the semantic values.
220 /// Long double is chosen as it has the strongest alignment
221 /// constraints.
222 union
223 {
224 long double align_me;
225 char raw[S];
226 } buffer;]b4_parse_assert_if([
227 /// Whether something is contained.
228 bool built;
229 /// If defined, the name of the stored type.
230 const char* tname;])[
231 };
232 ]])
233
234
235 ## -------------------------- ##
236 ## Adjustments for variants. ##
237 ## -------------------------- ##
238
239
240 # b4_semantic_type_declare
241 # ------------------------
242 # Declare semantic_type.
243 m4_define([b4_semantic_type_declare],
244 [ /// An auxiliary type to compute the largest semantic type.
245 union union_type
246 {]b4_type_foreach([b4_char_sizeof])[};
247
248 /// Symbol semantic values.
249 typedef variant<sizeof(union_type)> semantic_type;dnl
250 ])
251
252
253 # How the semantic value is extracted when using variants.
254
255 # b4_symbol_value(VAL, [TYPE])
256 # ----------------------------
257 m4_define([b4_symbol_value],
258 [m4_ifval([$2],
259 [$1.as< $2 >()],
260 [$1])])
261
262 # b4_symbol_value_template(VAL, [TYPE])
263 # -------------------------------------
264 # Same as b4_symbol_value, but used in a template method.
265 m4_define([b4_symbol_value_template],
266 [m4_ifval([$2],
267 [$1.template as< $2 >()],
268 [$1])])
269
270
271
272 ## ------------- ##
273 ## make_SYMBOL. ##
274 ## ------------- ##
275
276
277 # b4_symbol_constructor_declare_(SYMBOL-NUMBER)
278 # ---------------------------------------------
279 # Declare the overloaded version of make_symbol for the (common) type of
280 # these SYMBOL-NUMBERS. Use at class-level.
281 m4_define([b4_symbol_constructor_declare_],
282 [b4_symbol_if([$1], [is_token], [b4_symbol_if([$1], [has_id],
283 [ static inline
284 symbol_type
285 make_[]b4_symbol_([$1], [id]) (dnl
286 b4_join(b4_symbol_if([$1], [has_type],
287 [const b4_symbol([$1], [type])& v]),
288 b4_locations_if([const location_type& l])));
289
290 ])])])
291
292
293 # b4_symbol_constructor_declare
294 # -----------------------------
295 # Declare symbol constructors for all the value types.
296 # Use at class-level.
297 m4_define([b4_symbol_constructor_declare],
298 [ // Symbol constructors declarations.
299 b4_symbol_foreach([b4_symbol_constructor_declare_])])
300
301
302
303 # b4_symbol_constructor_define_(SYMBOL-NUMBER)
304 # --------------------------------------------
305 # Define symbol constructor for this SYMBOL-NUMBER.
306 m4_define([b4_symbol_constructor_define_],
307 [b4_symbol_if([$1], [is_token], [b4_symbol_if([$1], [has_id],
308 [ b4_parser_class_name::symbol_type
309 b4_parser_class_name::make_[]b4_symbol_([$1], [id]) (dnl
310 b4_join(b4_symbol_if([$1], [has_type],
311 [const b4_symbol([$1], [type])& v]),
312 b4_locations_if([const location_type& l])))
313 {
314 return symbol_type (b4_join([token::b4_symbol([$1], [id])],
315 b4_symbol_if([$1], [has_type], [v]),
316 b4_locations_if([l])));
317 }
318
319 ])])])
320
321
322 # b4_symbol_constructor_define
323 # ----------------------------
324 # Define the overloaded versions of make_symbol for all the value types.
325 m4_define([b4_symbol_constructor_define],
326 [ // Implementation of make_symbol for each symbol type.
327 b4_symbol_foreach([b4_symbol_constructor_define_])])