Merge branch 'branch-2.5'
[bison/ericb.git] / data / variant.hh
blob0c29f7f49db1981fbf7bf43f9789d6d93e3165e8
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/>.
20 ## --------- ##
21 ## variant. ##
22 ## --------- ##
24 # b4_symbol_variant(YYTYPE, YYVAL, ACTION, [ARGS])
25 # ------------------------------------------------
26 # Run some ACTION ("build", or "destroy") on YYVAL of symbol type
27 # YYTYPE.
28 m4_define([b4_symbol_variant],
29 [m4_pushdef([b4_dollar_dollar],
30 [$2.$3< $][3 >(m4_shift3($@))])dnl
31 switch ($1)
33 b4_type_foreach([b4_type_action_])[]dnl
34 default:
35 break;
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],
45 [0])
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])])@};
75 ])])
78 # b4_variant_define
79 # -----------------
80 # Define "variant".
81 m4_define([b4_variant_define],
83 /// A char[S] buffer to store and retrieve objects.
84 ///
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.
88 template <size_t S>
89 struct variant
90 {]b4_assert_if([
91 /// Whether something is contained.
92 bool built;
93 ])[
94 /// Empty construction.
95 inline
96 variant ()]b4_assert_if([
97 : built (false)])[
100 /// Instantiate a \a T in here.
101 template <typename T>
102 inline T&
103 build ()
104 {]b4_assert_if([
105 assert (!built);
106 built = true;])[
107 return *new (buffer.raw) T;
110 /// Instantiate a \a T in here from \a t.
111 template <typename T>
112 inline T&
113 build (const T& t)
114 {]b4_assert_if([
115 assert(!built);
116 built = true;])[
117 return *new (buffer.raw) T(t);
120 /// Construct and fill.
121 template <typename T>
122 inline
123 variant (const T& t)]b4_assert_if([
124 : built (true)])[
126 new (buffer.raw) T(t);
129 /// Accessor to a built \a T.
130 template <typename T>
131 inline T&
132 as ()
133 {]b4_assert_if([
134 assert (built);])[
135 return reinterpret_cast<T&>(buffer.raw);
138 /// Const accessor to a built \a T (for %printer).
139 template <typename T>
140 inline const T&
141 as () const
142 {]b4_assert_if([
143 assert(built);])[
144 return reinterpret_cast<const T&>(buffer.raw);
147 /// Swap the content with \a other.
148 template <typename T>
149 inline void
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>
158 inline void
159 build (variant<S>& other)
161 build<T>();
162 swap<T>(other);
163 other.destroy<T>();
166 /// Destroy the stored \a T.
167 template <typename T>
168 inline void
169 destroy ()
171 as<T>().~T();]b4_assert_if([
172 built = false;])[
175 /// A buffer large enough to store any of the semantic values.
176 /// Long double is chosen as it has the strongest alignment
177 /// constraints.
178 union
180 long double align_me;
181 char raw[S];
182 } buffer;
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.
197 union union_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],
209 [m4_ifval([$2],
210 [$1.as< $2 >()],
211 [$1])])
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],
217 [m4_ifval([$2],
218 [$1.template as< $2 >()],
219 [$1])])
223 ## ------------- ##
224 ## make_SYMBOL. ##
225 ## ------------- ##
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],
234 [ static inline
235 symbol_type
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])));
241 ])])])
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])));
270 ])])])
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_])])