1 # C++ skeleton for Bison
3 # Copyright (C) 2002-2013 Free Software Foundation, Inc.
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.
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.
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/>.
23 # b4_symbol_variant(YYTYPE, YYVAL, ACTION, [ARGS])
24 # ------------------------------------------------
25 # Run some ACTION ("build", or "destroy") on YYVAL of symbol type
27 m4_define([b4_symbol_variant],
28 [m4_pushdef([b4_dollar_dollar],
29 [$2.$3< $][3 > (m4_shift3($@))])dnl
32 b4_type_foreach([b4_type_action_])[]dnl
36 m4_popdef([b4_dollar_dollar])dnl
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],
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])
54 # b4_char_sizeof(SYMBOL-NUMS)
55 # ---------------------------
56 # To be mapped on the list of type names to produce:
58 # char dummy1[sizeof(type_name_1)];
59 # char dummy2[sizeof(type_name_2)];
61 # for defined type names.
62 m4_define([b4_char_sizeof],
63 [b4_symbol_if([$1], [has_type],
65 m4_map([ b4_symbol_tag_comment], [$@])dnl
66 char _b4_char_sizeof_dummy@{sizeof(b4_symbol([$1], [type]))@};
72 # The needed includes for variants support.
73 m4_define([b4_variant_includes],
74 [b4_parse_assert_if([[#include <typeinfo>]])[
77 # define YYASSERT assert
84 m4_define([b4_variant_define],
85 [[ /// A char[S] buffer to store and retrieve objects.
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.
94 typedef variant<S> self_type;
96 /// Empty construction.
97 variant ()]b4_parse_assert_if([
98 : yytname_ (YY_NULL)])[
101 /// Construct and fill.
102 template <typename T>
103 variant (const T& t)]b4_parse_assert_if([
104 : yytname_ (typeid (T).name ())])[
106 YYASSERT (sizeof (T) <= S);
107 new (yyas_<T> ()) T (t);
110 /// Destruction, allowed only if empty.
112 {]b4_parse_assert_if([
113 YYASSERT (!yytname_);
116 /// Instantiate an empty \a T in here.
117 template <typename T>
120 {]b4_parse_assert_if([
121 YYASSERT (!yytname_);
122 YYASSERT (sizeof (T) <= S);
123 yytname_ = typeid (T).name ();])[
124 return *new (yyas_<T> ()) T;
127 /// Instantiate a \a T in here from \a t.
128 template <typename T>
131 {]b4_parse_assert_if([
132 YYASSERT (!yytname_);
133 YYASSERT (sizeof (T) <= S);
134 yytname_ = typeid (T).name ();])[
135 return *new (yyas_<T> ()) T (t);
138 /// Accessor to a built \a T.
139 template <typename T>
142 {]b4_parse_assert_if([
143 YYASSERT (yytname_ == typeid (T).name ());
144 YYASSERT (sizeof (T) <= S);])[
148 /// Const accessor to a built \a T (for %printer).
149 template <typename T>
152 {]b4_parse_assert_if([
153 YYASSERT (yytname_ == typeid (T).name ());
154 YYASSERT (sizeof (T) <= S);])[
158 /// Swap the content with \a other, of same type.
160 /// Both variants must be built beforehand, because swapping the actual
161 /// data requires reading it (with as()), and this is not possible on
162 /// unconstructed variants: it would require some dynamic testing, which
163 /// should not be the variant's responsability.
164 /// Swapping between built and (possibly) non-built is done with
165 /// variant::move ().
166 template <typename T>
168 swap (self_type& other)
169 {]b4_parse_assert_if([
171 YYASSERT (yytname_ == other.yytname_);])[
172 std::swap (as<T> (), other.as<T> ());
175 /// Move the content of \a other to this.
177 /// Destroys \a other.
178 template <typename T>
180 move (self_type& other)
181 {]b4_parse_assert_if([
182 YYASSERT (!yytname_);])[
188 /// Copy the content of \a other to this.
189 template <typename T>
191 copy (const self_type& other)
193 build<T> (other.as<T> ());
196 /// Destroy the stored \a T.
197 template <typename T>
201 as<T> ().~T ();]b4_parse_assert_if([
202 yytname_ = YY_NULL;])[
206 /// Prohibit blind copies.
207 self_type& operator=(const self_type&);
208 variant (const self_type&);
210 /// Accessor to raw memory as \a T.
211 template <typename T>
215 void *yyp = yybuffer_.yyraw;
216 return static_cast<T*> (yyp);
219 /// Const accessor to raw memory as \a T.
220 template <typename T>
224 const void *yyp = yybuffer_.yyraw;
225 return static_cast<const T*> (yyp);
230 /// Strongest alignment constraints.
231 long double yyalign_me;
232 /// A buffer large enough to store any of the semantic values.
234 } yybuffer_;]b4_parse_assert_if([
236 /// Whether the content is built: if defined, the name of the stored type.
237 const char *yytname_;])[
242 ## -------------------------- ##
243 ## Adjustments for variants. ##
244 ## -------------------------- ##
247 # b4_value_type_declare
248 # ---------------------
249 # Declare semantic_type.
250 m4_define([b4_value_type_declare],
251 [[ /// An auxiliary type to compute the largest semantic type.
253 {]b4_type_foreach([b4_char_sizeof])[};
255 /// Symbol semantic values.
256 typedef variant<sizeof(union_type)> semantic_type;][]dnl
260 # How the semantic value is extracted when using variants.
262 # b4_symbol_value(VAL, [TYPE])
263 # ----------------------------
264 m4_define([b4_symbol_value],
269 # b4_symbol_value_template(VAL, [TYPE])
270 # -------------------------------------
271 # Same as b4_symbol_value, but used in a template method.
272 m4_define([b4_symbol_value_template],
274 [$1.template as< $2 > ()],
284 # b4_symbol_constructor_declare_(SYMBOL-NUMBER)
285 # ---------------------------------------------
286 # Declare the overloaded version of make_symbol for the (common) type of
287 # these SYMBOL-NUMBERS. Use at class-level.
288 m4_define([b4_symbol_constructor_declare_],
289 [b4_symbol_if([$1], [is_token], [b4_symbol_if([$1], [has_id],
292 make_[]b4_symbol_([$1], [id]) (dnl
293 b4_join(b4_symbol_if([$1], [has_type],
294 [const b4_symbol([$1], [type])& v]),
295 b4_locations_if([const location_type& l])));
300 # b4_symbol_constructor_declare
301 # -----------------------------
302 # Declare symbol constructors for all the value types.
303 # Use at class-level.
304 m4_define([b4_symbol_constructor_declare],
305 [ // Symbol constructors declarations.
306 b4_symbol_foreach([b4_symbol_constructor_declare_])])
310 # b4_symbol_constructor_define_(SYMBOL-NUMBER)
311 # --------------------------------------------
312 # Define symbol constructor for this SYMBOL-NUMBER.
313 m4_define([b4_symbol_constructor_define_],
314 [b4_symbol_if([$1], [is_token], [b4_symbol_if([$1], [has_id],
315 [ b4_parser_class_name::symbol_type
316 b4_parser_class_name::make_[]b4_symbol_([$1], [id]) (dnl
317 b4_join(b4_symbol_if([$1], [has_type],
318 [const b4_symbol([$1], [type])& v]),
319 b4_locations_if([const location_type& l])))
321 return symbol_type (b4_join([token::b4_symbol([$1], [id])],
322 b4_symbol_if([$1], [has_type], [v]),
323 b4_locations_if([l])));
330 # b4_basic_symbol_constructor_declare
331 # -----------------------------------
332 # Generate a constructor declaration for basic_symbol from given type.
333 m4_define([b4_basic_symbol_constructor_declare],
335 basic_symbol (]b4_join(
336 [typename Base::kind_type t],
337 b4_symbol_if([$1], [has_type], const b4_symbol([$1], [type])[ v]),
338 b4_locations_if([const location_type& l]))[);
341 # b4_basic_symbol_constructor_define
342 # ----------------------------------
343 # Generate a constructor implementation for basic_symbol from given type.
344 m4_define([b4_basic_symbol_constructor_define],
346 template <typename Base>
347 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (]b4_join(
348 [typename Base::kind_type t],
349 b4_symbol_if([$1], [has_type], const b4_symbol([$1], [type])[ v]),
350 b4_locations_if([const location_type& l]))[)
352 , value (]b4_symbol_if([$1], [has_type], [v])[)]b4_locations_if([
357 # b4_symbol_constructor_define
358 # ----------------------------
359 # Define the overloaded versions of make_symbol for all the value types.
360 m4_define([b4_symbol_constructor_define],
361 [ // Implementation of make_symbol for each symbol type.
362 b4_symbol_foreach([b4_symbol_constructor_define_])])