]> git.saurik.com Git - bison.git/blob - data/variant.hh
style: no longer use backquotes
[bison.git] / data / variant.hh
1 # C++ skeleton for Bison
2
3 # Copyright (C) 2002-2013 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-NUMS)
55 # ---------------------------
56 # To be mapped on the list of type names to produce:
57 #
58 # char dummy1[sizeof(type_name_1)];
59 # char dummy2[sizeof(type_name_2)];
60 #
61 # for defined type names.
62 m4_define([b4_char_sizeof],
63 [b4_symbol_if([$1], [has_type],
64 [
65 m4_map([ b4_symbol_tag_comment], [$@])dnl
66 char _b4_char_sizeof_dummy@{sizeof([b4_symbol([$1], [type])])@};
67 ])])
68
69
70 # b4_variant_includes
71 # -------------------
72 # The needed includes for variants support.
73 m4_define([b4_variant_includes],
74 [b4_parse_assert_if([[#include <typeinfo>]])[
75 #ifndef YYASSERT
76 # include <cassert>
77 # define YYASSERT assert
78 #endif
79 ]])
80
81 # b4_variant_define
82 # -----------------
83 # Define "variant".
84 m4_define([b4_variant_define],
85 [[ /// A char[S] buffer to store and retrieve objects.
86 ///
87 /// Sort of a variant, but does not keep track of the nature
88 /// of the stored data, since that knowledge is available
89 /// via the current state.
90 template <size_t S>
91 struct variant
92 {
93 /// Type of *this.
94 typedef variant<S> self_type;
95
96 /// Empty construction.
97 variant ()]b4_parse_assert_if([
98 : tname (YY_NULL)])[
99 {}
100
101 /// Construct and fill.
102 template <typename T>
103 variant (const T& t)]b4_parse_assert_if([
104 : tname (typeid (T).name ())])[
105 {
106 YYASSERT (sizeof (T) <= S);
107 new (buffer.raw) T (t);
108 }
109
110 /// Destruction, allowed only if empty.
111 ~variant ()
112 {]b4_parse_assert_if([
113 YYASSERT (!tname);
114 ])[}
115
116 /// Instantiate an empty \a T in here.
117 template <typename T>
118 T&
119 build ()
120 {]b4_parse_assert_if([
121 YYASSERT (!tname);
122 YYASSERT (sizeof (T) <= S);
123 tname = typeid (T).name ();])[
124 return *new (buffer.raw) T;
125 }
126
127 /// Instantiate a \a T in here from \a t.
128 template <typename T>
129 T&
130 build (const T& t)
131 {]b4_parse_assert_if([
132 YYASSERT (!tname);
133 YYASSERT (sizeof (T) <= S);
134 tname = typeid (T).name ();])[
135 return *new (buffer.raw) T (t);
136 }
137
138 /// Accessor to a built \a T.
139 template <typename T>
140 T&
141 as ()
142 {]b4_parse_assert_if([
143 YYASSERT (tname == typeid (T).name ());
144 YYASSERT (sizeof (T) <= S);])[
145 {
146 void *dummy = buffer.raw;
147 return *static_cast<T*> (dummy);
148 }
149 }
150
151 /// Const accessor to a built \a T (for %printer).
152 template <typename T>
153 const T&
154 as () const
155 {]b4_parse_assert_if([
156 YYASSERT (tname == typeid (T).name ());
157 YYASSERT (sizeof (T) <= S);])[
158 {
159 const void *dummy = buffer.raw;
160 return *static_cast<const T*> (dummy);
161 }
162 }
163
164 /// Swap the content with \a other, of same type.
165 ///
166 /// Both variants must be built beforehand, because swapping the actual
167 /// data requires reading it (with as()), and this is not possible on
168 /// unconstructed variants: it would require some dynamic testing, which
169 /// should not be the variant's responsability.
170 /// Swapping between built and (possibly) non-built is done with
171 /// variant::move ().
172 template <typename T>
173 void
174 swap (self_type& other)
175 {]b4_parse_assert_if([
176 YYASSERT (tname);
177 YYASSERT (tname == other.tname);])[
178 std::swap (as<T>(), other.as<T>());
179 }
180
181 /// Move the content of \a other to this.
182 ///
183 /// Destroys \a other.
184 template <typename T>
185 void
186 move (self_type& other)
187 {]b4_parse_assert_if([
188 YYASSERT (!tname);])[
189 build<T>();
190 swap<T>(other);
191 other.destroy<T>();
192 }
193
194 /// Copy the content of \a other to this.
195 template <typename T>
196 void
197 copy (const self_type& other)
198 {
199 build<T> (other.as<T> ());
200 }
201
202 /// Destroy the stored \a T.
203 template <typename T>
204 void
205 destroy ()
206 {
207 as<T> ().~T ();]b4_parse_assert_if([
208 tname = YY_NULL;])[
209 }
210
211 private:
212 /// Prohibit blind copies.
213 self_type& operator=(const self_type&);
214 variant (const self_type&);
215
216 /// A buffer large enough to store any of the semantic values.
217 /// Long double is chosen as it has the strongest alignment
218 /// constraints.
219 union
220 {
221 long double align_me;
222 char raw[S];
223 } buffer;]b4_parse_assert_if([
224
225 /// Whether the content is built: if defined, the name of the stored type.
226 const char *tname;])[
227 };
228 ]])
229
230
231 ## -------------------------- ##
232 ## Adjustments for variants. ##
233 ## -------------------------- ##
234
235
236 # b4_semantic_type_declare
237 # ------------------------
238 # Declare semantic_type.
239 m4_define([b4_semantic_type_declare],
240 [ /// An auxiliary type to compute the largest semantic type.
241 union union_type
242 {]b4_type_foreach([b4_char_sizeof])[};
243
244 /// Symbol semantic values.
245 typedef variant<sizeof(union_type)> semantic_type;dnl
246 ])
247
248
249 # How the semantic value is extracted when using variants.
250
251 # b4_symbol_value(VAL, [TYPE])
252 # ----------------------------
253 m4_define([b4_symbol_value],
254 [m4_ifval([$2],
255 [$1.as< $2 >()],
256 [$1])])
257
258 # b4_symbol_value_template(VAL, [TYPE])
259 # -------------------------------------
260 # Same as b4_symbol_value, but used in a template method.
261 m4_define([b4_symbol_value_template],
262 [m4_ifval([$2],
263 [$1.template as< $2 >()],
264 [$1])])
265
266
267
268 ## ------------- ##
269 ## make_SYMBOL. ##
270 ## ------------- ##
271
272
273 # b4_symbol_constructor_declare_(SYMBOL-NUMBER)
274 # ---------------------------------------------
275 # Declare the overloaded version of make_symbol for the (common) type of
276 # these SYMBOL-NUMBERS. Use at class-level.
277 m4_define([b4_symbol_constructor_declare_],
278 [b4_symbol_if([$1], [is_token], [b4_symbol_if([$1], [has_id],
279 [ static inline
280 symbol_type
281 make_[]b4_symbol_([$1], [id]) (dnl
282 b4_join(b4_symbol_if([$1], [has_type],
283 [const b4_symbol([$1], [type])& v]),
284 b4_locations_if([const location_type& l])));
285
286 ])])])
287
288
289 # b4_symbol_constructor_declare
290 # -----------------------------
291 # Declare symbol constructors for all the value types.
292 # Use at class-level.
293 m4_define([b4_symbol_constructor_declare],
294 [ // Symbol constructors declarations.
295 b4_symbol_foreach([b4_symbol_constructor_declare_])])
296
297
298
299 # b4_symbol_constructor_define_(SYMBOL-NUMBER)
300 # --------------------------------------------
301 # Define symbol constructor for this SYMBOL-NUMBER.
302 m4_define([b4_symbol_constructor_define_],
303 [b4_symbol_if([$1], [is_token], [b4_symbol_if([$1], [has_id],
304 [ b4_parser_class_name::symbol_type
305 b4_parser_class_name::make_[]b4_symbol_([$1], [id]) (dnl
306 b4_join(b4_symbol_if([$1], [has_type],
307 [const b4_symbol([$1], [type])& v]),
308 b4_locations_if([const location_type& l])))
309 {
310 return symbol_type (b4_join([token::b4_symbol([$1], [id])],
311 b4_symbol_if([$1], [has_type], [v]),
312 b4_locations_if([l])));
313
314 }
315
316 ])])])
317
318
319 # b4_basic_symbol_constructor_declare
320 # ----------------------------------
321 # Generate a constructor declaration for basic_symbol from given type.
322 m4_define([b4_basic_symbol_constructor_declare],
323 [[
324 basic_symbol (]b4_join(
325 [typename Base::kind_type t],
326 b4_symbol_if([$1], [has_type], const b4_symbol([$1], [type])[ v]),
327 b4_locations_if([const location_type& l]))[);
328 ]])
329
330 # b4_basic_symbol_constructor_define
331 # ----------------------------------
332 # Generate a constructor implementation for basic_symbol from given type.
333 m4_define([b4_basic_symbol_constructor_define],
334 [[
335 template <typename Base>
336 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (]b4_join(
337 [typename Base::kind_type t],
338 b4_symbol_if([$1], [has_type], const b4_symbol([$1], [type])[ v]),
339 b4_locations_if([const location_type& l]))[)
340 : Base (t)
341 , value (]b4_symbol_if([$1], [has_type], [v])[)]b4_locations_if([
342 , location (l)])[
343 {}
344 ]])
345
346 # b4_symbol_constructor_define
347 # ----------------------------
348 # Define the overloaded versions of make_symbol for all the value types.
349 m4_define([b4_symbol_constructor_define],
350 [ // Implementation of make_symbol for each symbol type.
351 b4_symbol_foreach([b4_symbol_constructor_define_])])