1 # C++ skeleton for Bison
3 # Copyright (C) 2002-2012 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>]])[
75 #include <cstdlib> // abort
78 # define YYASSERT assert
85 m4_define([b4_variant_define],
86 [[ /// A char[S] buffer to store and retrieve objects.
88 /// Sort of a variant, but does not keep track of the nature
89 /// of the stored data, since that knowledge is available
90 /// via the current state.
95 typedef variant<S> self_type;
97 /// Empty construction.
99 variant ()]b4_parse_assert_if([
104 /// Instantiate a \a T in here.
105 template <typename T>
108 {]b4_parse_assert_if([
111 YYASSERT (sizeof (T) <= S);
113 tname = typeid (T).name ();])[
114 return *new (buffer.raw) T;
117 /// Instantiate a \a T in here from \a t.
118 template <typename T>
121 {]b4_parse_assert_if([
124 YYASSERT (sizeof (T) <= S);
126 tname = typeid (T).name ();])[
127 return *new (buffer.raw) T (t);
130 /// Construct and fill.
131 template <typename T>
133 variant (const T& t)]b4_parse_assert_if([
135 , tname (typeid (T).name ())])[
137 YYASSERT (sizeof (T) <= S);
138 new (buffer.raw) T (t);
141 /// Accessor to a built \a T.
142 template <typename T>
145 {]b4_parse_assert_if([
147 YYASSERT (tname == typeid (T).name ());
148 YYASSERT (sizeof (T) <= S);])[
149 return reinterpret_cast<T&> (buffer.raw);
152 /// Const accessor to a built \a T (for %printer).
153 template <typename T>
156 {]b4_parse_assert_if([
158 YYASSERT (tname == typeid (T).name ());
159 YYASSERT (sizeof (T) <= S);])[
160 return reinterpret_cast<const T&> (buffer.raw);
163 /// Swap the content with \a other, of same type.
164 /// Both variants must be built beforehand.
165 template <typename T>
167 swap (variant<S>& other)
168 {]b4_parse_assert_if([
170 YYASSERT (other.built);
171 YYASSERT (tname == other.tname);])[
172 std::swap (as<T>(), other.as<T>());
175 /// Assign the content of \a other to this.
176 /// Destroys \a other.
177 template <typename T>
179 build (variant<S>& other)
186 /// Copy the content of \a other to this.
187 /// Destroys \a other.
188 template <typename T>
190 copy (const variant<S>& other)
192 build<T> (other.as<T> ());
195 /// Destroy the stored \a T.
196 template <typename T>
200 as<T> ().~T ();]b4_parse_assert_if([
205 /// Prohibit blind copies.
207 self_type& operator=(const self_type&)
212 variant (const self_type&)
218 /// A buffer large enough to store any of the semantic values.
219 /// Long double is chosen as it has the strongest alignment
223 long double align_me;
225 } buffer;]b4_parse_assert_if([
226 /// Whether something is contained.
228 /// If defined, the name of the stored type.
229 const char* tname;])[
234 ## -------------------------- ##
235 ## Adjustments for variants. ##
236 ## -------------------------- ##
239 # b4_semantic_type_declare
240 # ------------------------
241 # Declare semantic_type.
242 m4_define([b4_semantic_type_declare],
243 [ /// An auxiliary type to compute the largest semantic type.
245 {]b4_type_foreach([b4_char_sizeof])[};
247 /// Symbol semantic values.
248 typedef variant<sizeof(union_type)> semantic_type;dnl
252 # How the semantic value is extracted when using variants.
254 # b4_symbol_value(VAL, [TYPE])
255 # ----------------------------
256 m4_define([b4_symbol_value],
261 # b4_symbol_value_template(VAL, [TYPE])
262 # -------------------------------------
263 # Same as b4_symbol_value, but used in a template method.
264 m4_define([b4_symbol_value_template],
266 [$1.template as< $2 >()],
276 # b4_symbol_constructor_declare_(SYMBOL-NUMBER)
277 # ---------------------------------------------
278 # Declare the overloaded version of make_symbol for the (common) type of
279 # these SYMBOL-NUMBERS. Use at class-level.
280 m4_define([b4_symbol_constructor_declare_],
281 [b4_symbol_if([$1], [is_token], [b4_symbol_if([$1], [has_id],
284 make_[]b4_symbol_([$1], [id]) (dnl
285 b4_join(b4_symbol_if([$1], [has_type],
286 [const b4_symbol([$1], [type])& v]),
287 b4_locations_if([const location_type& l])));
292 # b4_symbol_constructor_declare
293 # -----------------------------
294 # Declare symbol constructors for all the value types.
295 # Use at class-level.
296 m4_define([b4_symbol_constructor_declare],
297 [ // Symbol constructors declarations.
298 b4_symbol_foreach([b4_symbol_constructor_declare_])])
302 # b4_symbol_constructor_define_(SYMBOL-NUMBER)
303 # --------------------------------------------
304 # Define symbol constructor for this SYMBOL-NUMBER.
305 m4_define([b4_symbol_constructor_define_],
306 [b4_symbol_if([$1], [is_token], [b4_symbol_if([$1], [has_id],
307 [ b4_parser_class_name::symbol_type
308 b4_parser_class_name::make_[]b4_symbol_([$1], [id]) (dnl
309 b4_join(b4_symbol_if([$1], [has_type],
310 [const b4_symbol([$1], [type])& v]),
311 b4_locations_if([const location_type& l])))
313 return symbol_type (b4_join([token::b4_symbol([$1], [id])],
314 b4_symbol_if([$1], [has_type], [v]),
315 b4_locations_if([l])));
321 # b4_symbol_constructor_define
322 # ----------------------------
323 # Define the overloaded versions of make_symbol for all the value types.
324 m4_define([b4_symbol_constructor_define],
325 [ // Implementation of make_symbol for each symbol type.
326 b4_symbol_foreach([b4_symbol_constructor_define_])])