1 # C++ skeleton for Bison
3 # Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 # Free Software Foundation, Inc.
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
24 # b4_symbol_variant(YYTYPE, YYVAL, ACTION, [ARGS])
25 # ------------------------------------------------
26 # Run some ACTION ("build", or "destroy") on YYVAL of symbol type
28 m4_define([b4_symbol_variant],
29 [m4_pushdef([b4_dollar_dollar],
30 [$2.$3< $][3 >(m4_shift3($@))])dnl
33 b4_type_foreach([b4_type_action_])[]dnl
37 m4_popdef([b4_dollar_dollar])dnl
41 # _b4_char_sizeof_counter
42 # -----------------------
43 # A counter used by _b4_char_sizeof_dummy to create fresh symbols.
44 m4_define([_b4_char_sizeof_counter],
47 # _b4_char_sizeof_dummy
48 # ---------------------
49 # At each call return a new C++ identifier.
50 m4_define([_b4_char_sizeof_dummy],
51 [m4_define([_b4_char_sizeof_counter], m4_incr(_b4_char_sizeof_counter))dnl
52 dummy[]_b4_char_sizeof_counter])
55 # b4_char_sizeof_(SYMBOL-NUM)
56 # ---------------------------
57 # A comment describing this symbol.
58 m4_define([b4_char_sizeof_],
59 [ // b4_symbol([$1], [tag])
62 # b4_char_sizeof(SYMBOL-NUMS)
63 # ---------------------------
64 # To be mapped on the list of type names to produce:
66 # char dummy1[sizeof(type_name_1)];
67 # char dummy2[sizeof(type_name_2)];
69 # for defined type names.
70 m4_define([b4_char_sizeof],
71 [b4_symbol_if([$1], [has_type],
73 m4_map([b4_char_sizeof_], [$@])dnl
74 char _b4_char_sizeof_dummy@{sizeof([b4_symbol([$1], [type])])@};
81 m4_define([b4_variant_define],
83 /// A char[S] buffer to store and retrieve objects.
85 /// Sort of a variant, but does not keep track of the nature
86 /// of the stored data, since that knowledge is available
87 /// via the current state.
90 {]b4_parse_assert_if([
91 /// Whether something is contained.
94 /// Empty construction.
96 variant ()]b4_parse_assert_if([
100 /// Instantiate a \a T in here.
101 template <typename T>
104 {]b4_parse_assert_if([
107 return *new (buffer.raw) T;
110 /// Instantiate a \a T in here from \a t.
111 template <typename T>
114 {]b4_parse_assert_if([
117 return *new (buffer.raw) T(t);
120 /// Construct and fill.
121 template <typename T>
123 variant (const T& t)]b4_parse_assert_if([
126 new (buffer.raw) T(t);
129 /// Accessor to a built \a T.
130 template <typename T>
133 {]b4_parse_assert_if([
135 return reinterpret_cast<T&>(buffer.raw);
138 /// Const accessor to a built \a T (for %printer).
139 template <typename T>
142 {]b4_parse_assert_if([
144 return reinterpret_cast<const T&>(buffer.raw);
147 /// Swap the content with \a other.
148 template <typename T>
150 swap (variant<S>& other)
152 std::swap (as<T>(), other.as<T>());
155 /// Assign the content of \a other to this.
156 /// Destroys \a other.
157 template <typename T>
159 build (variant<S>& other)
166 /// Destroy the stored \a T.
167 template <typename T>
171 as<T>().~T();]b4_parse_assert_if([
175 /// A buffer large enough to store any of the semantic values.
176 /// Long double is chosen as it has the strongest alignment
180 long double align_me;
187 ## -------------------------- ##
188 ## Adjustments for variants. ##
189 ## -------------------------- ##
192 # b4_semantic_type_declare
193 # ------------------------
194 # Declare semantic_type.
195 m4_define([b4_semantic_type_declare],
196 [ /// An auxiliary type to compute the largest semantic type.
198 {]b4_type_foreach([b4_char_sizeof])[};
200 /// Symbol semantic values.
201 typedef variant<sizeof(union_type)> semantic_type;])
204 # How the semantic value is extracted when using variants.
206 # b4_symbol_value(VAL, [TYPE])
207 # ----------------------------
208 m4_define([b4_symbol_value],
213 # b4_symbol_value_template(VAL, [TYPE])
214 # -------------------------------------
215 # Same as b4_symbol_value, but used in a template method.
216 m4_define([b4_symbol_value_template],
218 [$1.template as< $2 >()],
228 # b4_symbol_constructor_declare_(SYMBOL-NUMBER)
229 # ---------------------------------------------
230 # Declare the overloaded version of make_symbol for the (common) type of
231 # these SYMBOL-NUMBERS. Use at class-level.
232 m4_define([b4_symbol_constructor_declare_],
233 [b4_symbol_if([$1], [is_token], [b4_symbol_if([$1], [has_id],
236 make_[]b4_symbol_([$1], [id]) (dnl
237 b4_args(b4_symbol_if([$1], [has_type],
238 [const b4_symbol([$1], [type])& v]),
239 b4_locations_if([const location_type& l])));
244 # b4_symbol_constructor_declare
245 # -----------------------------
246 # Declare symbol constructors for all the value types.
247 # Use at class-level.
248 m4_define([b4_symbol_constructor_declare],
249 [ // Symbol constructors declarations.
250 b4_symbol_foreach([b4_symbol_constructor_declare_])])
254 # b4_symbol_constructor_define_(SYMBOL-NUMBER)
255 # --------------------------------------------
256 # Define symbol constructor for this SYMBOL-NUMBER.
257 m4_define([b4_symbol_constructor_define_],
258 [b4_symbol_if([$1], [is_token], [b4_symbol_if([$1], [has_id],
259 [ b4_parser_class_name::symbol_type
260 b4_parser_class_name::make_[]b4_symbol_([$1], [id]) (dnl
261 b4_args(b4_symbol_if([$1], [has_type],
262 [const b4_symbol([$1], [type])& v]),
263 b4_locations_if([const location_type& l])))
265 return symbol_type (b4_args([yytranslate_ (token::b4_symbol([$1], [id]))],
266 b4_symbol_if([$1], [has_type], [v]),
267 b4_locations_if([l])));
273 # b4_symbol_constructor_define
274 # ----------------------------
275 # Define the overloaded versions of make_symbol for all the value types.
276 m4_define([b4_symbol_constructor_define],
277 [ // Implementation of make_symbol for each symbol type.
278 b4_symbol_foreach([b4_symbol_constructor_define_])])