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])])@};
78 # b4_variant_definition
79 # ---------------------
81 m4_define([b4_variant_definition],
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.
91 /// Whether something is contained.
94 /// Empty construction.
96 variant ()]b4_assert_if([
100 /// Instantiate a \a T in here.
101 template <typename T>
107 return *new (buffer.raw) T;
110 /// Instantiate a \a T in here from \a t.
111 template <typename T>
117 return *new (buffer.raw) T(t);
120 /// Construct and fill.
121 template <typename T>
123 variant (const T& t)]b4_assert_if([
126 new (buffer.raw) T(t);
129 /// Accessor to a built \a T.
130 template <typename T>
135 return reinterpret_cast<T&>(buffer.raw);
138 /// Const accessor to a built \a T (for %printer).
139 template <typename T>
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_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 # How the semantic value is extracted when using variants.
194 # b4_symbol_value(VAL, [TYPE])
195 # ----------------------------
196 m4_define([b4_symbol_value],
201 # b4_symbol_value_template(VAL, [TYPE])
202 # -------------------------------------
203 # Same as b4_symbol_value, but used in a template method.
204 m4_define([b4_symbol_value_template],
206 [$1.template as< $2 >()],