]> git.saurik.com Git - bison.git/blob - data/variant.hh
684f9e35bda0a64f9087ae58b074b08b231df78e
[bison.git] / data / variant.hh
1 # C++ skeleton for Bison
2
3 # Copyright (C) 2002-2012 Free Software Foundation, Inc.
4
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.
9 #
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.
14 #
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/>.
17
18
19 ## --------- ##
20 ## variant. ##
21 ## --------- ##
22
23 # b4_symbol_variant(YYTYPE, YYVAL, ACTION, [ARGS])
24 # ------------------------------------------------
25 # Run some ACTION ("build", or "destroy") on YYVAL of symbol type
26 # YYTYPE.
27 m4_define([b4_symbol_variant],
28 [m4_pushdef([b4_dollar_dollar],
29 [$2.$3< $][3 >(m4_shift3($@))])dnl
30 switch ($1)
31 {
32 b4_type_foreach([b4_type_action_])[]dnl
33 default:
34 break;
35 }
36 m4_popdef([b4_dollar_dollar])dnl
37 ])
38
39
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],
44 [0])
45
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])
52
53
54 # b4_char_sizeof_(SYMBOL-NUM)
55 # ---------------------------
56 # A comment describing this symbol.
57 m4_define([b4_char_sizeof_],
58 [ // b4_symbol([$1], [tag])
59 ])
60
61 # b4_char_sizeof(SYMBOL-NUMS)
62 # ---------------------------
63 # To be mapped on the list of type names to produce:
64 #
65 # char dummy1[sizeof(type_name_1)];
66 # char dummy2[sizeof(type_name_2)];
67 #
68 # for defined type names.
69 m4_define([b4_char_sizeof],
70 [b4_symbol_if([$1], [has_type],
71 [
72 m4_map([b4_char_sizeof_], [$@])dnl
73 char _b4_char_sizeof_dummy@{sizeof([b4_symbol([$1], [type])])@};
74 ])])
75
76
77 # b4_variant_includes
78 # -------------------
79 # The needed includes for variants support.
80 m4_define([b4_variant_includes],
81 [b4_parse_assert_if([[#include <typeinfo>]])[
82 #ifndef YYASSERT
83 # include <cassert>
84 # define YYASSERT assert
85 #endif
86 ]])
87
88 # b4_variant_define
89 # -----------------
90 # Define "variant".
91 m4_define([b4_variant_define],
92 [[ /// A char[S] buffer to store and retrieve objects.
93 ///
94 /// Sort of a variant, but does not keep track of the nature
95 /// of the stored data, since that knowledge is available
96 /// via the current state.
97 template <size_t S>
98 struct variant
99 {
100 /// Type of *this.
101 typedef variant<S> self_type;
102
103 /// Empty construction.
104 inline
105 variant ()]b4_parse_assert_if([
106 : built (false)
107 , tname (YY_NULL)])[
108 {}
109
110 /// Instantiate a \a T in here.
111 template <typename T>
112 inline T&
113 build ()
114 {]b4_parse_assert_if([
115 YYASSERT (!built);
116 YYASSERT (!tname);
117 YYASSERT (sizeof (T) <= S);
118 built = true;
119 tname = typeid (T).name ();])[
120 return *new (buffer.raw) T;
121 }
122
123 /// Instantiate a \a T in here from \a t.
124 template <typename T>
125 inline T&
126 build (const T& t)
127 {]b4_parse_assert_if([
128 YYASSERT (!built);
129 YYASSERT (!tname);
130 YYASSERT (sizeof (T) <= S);
131 built = true;
132 tname = typeid (T).name ();])[
133 return *new (buffer.raw) T (t);
134 }
135
136 /// Construct and fill.
137 template <typename T>
138 inline
139 variant (const T& t)]b4_parse_assert_if([
140 : built (true)
141 , tname (typeid (T).name ())])[
142 {
143 YYASSERT (sizeof (T) <= S);
144 new (buffer.raw) T (t);
145 }
146
147 /// Accessor to a built \a T.
148 template <typename T>
149 inline T&
150 as ()
151 {]b4_parse_assert_if([
152 YYASSERT (built);
153 YYASSERT (tname == typeid (T).name ());
154 YYASSERT (sizeof (T) <= S);])[
155 return reinterpret_cast<T&> (buffer.raw);
156 }
157
158 /// Const accessor to a built \a T (for %printer).
159 template <typename T>
160 inline const T&
161 as () const
162 {]b4_parse_assert_if([
163 YYASSERT (built);
164 YYASSERT (tname == typeid (T).name ());
165 YYASSERT (sizeof (T) <= S);])[
166 return reinterpret_cast<const T&> (buffer.raw);
167 }
168
169 /// Swap the content with \a other, of same type.
170 template <typename T>
171 inline void
172 swap (variant<S>& other)
173 {]b4_parse_assert_if([
174 YYASSERT (tname == other.tname);])[
175 std::swap (as<T>(), other.as<T>());]b4_parse_assert_if([
176 std::swap (built, other.built);
177 std::swap (tname, other.tname);])[
178 }
179
180 /// Assign the content of \a other to this.
181 /// Destroys \a other.
182 template <typename T>
183 inline void
184 build (variant<S>& other)
185 {
186 build<T>();
187 swap<T>(other);
188 other.destroy<T>();
189 }
190
191 /// Destroy the stored \a T.
192 template <typename T>
193 inline void
194 destroy ()
195 {
196 as<T> ().~T ();]b4_parse_assert_if([
197 built = false;
198 tname = YY_NULL;])[
199 }
200
201 private:
202 /// A buffer large enough to store any of the semantic values.
203 /// Long double is chosen as it has the strongest alignment
204 /// constraints.
205 union
206 {
207 long double align_me;
208 char raw[S];
209 } buffer;]b4_parse_assert_if([
210 /// Whether something is contained.
211 bool built;
212 /// If defined, the name of the stored type.
213 const char* tname;])[
214 };
215 ]])
216
217
218 ## -------------------------- ##
219 ## Adjustments for variants. ##
220 ## -------------------------- ##
221
222
223 # b4_semantic_type_declare
224 # ------------------------
225 # Declare semantic_type.
226 m4_define([b4_semantic_type_declare],
227 [ /// An auxiliary type to compute the largest semantic type.
228 union union_type
229 {]b4_type_foreach([b4_char_sizeof])[};
230
231 /// Symbol semantic values.
232 typedef variant<sizeof(union_type)> semantic_type;dnl
233 ])
234
235
236 # How the semantic value is extracted when using variants.
237
238 # b4_symbol_value(VAL, [TYPE])
239 # ----------------------------
240 m4_define([b4_symbol_value],
241 [m4_ifval([$2],
242 [$1.as< $2 >()],
243 [$1])])
244
245 # b4_symbol_value_template(VAL, [TYPE])
246 # -------------------------------------
247 # Same as b4_symbol_value, but used in a template method.
248 m4_define([b4_symbol_value_template],
249 [m4_ifval([$2],
250 [$1.template as< $2 >()],
251 [$1])])
252
253
254
255 ## ------------- ##
256 ## make_SYMBOL. ##
257 ## ------------- ##
258
259
260 # b4_symbol_constructor_declare_(SYMBOL-NUMBER)
261 # ---------------------------------------------
262 # Declare the overloaded version of make_symbol for the (common) type of
263 # these SYMBOL-NUMBERS. Use at class-level.
264 m4_define([b4_symbol_constructor_declare_],
265 [b4_symbol_if([$1], [is_token], [b4_symbol_if([$1], [has_id],
266 [ static inline
267 symbol_type
268 make_[]b4_symbol_([$1], [id]) (dnl
269 b4_join(b4_symbol_if([$1], [has_type],
270 [const b4_symbol([$1], [type])& v]),
271 b4_locations_if([const location_type& l])));
272
273 ])])])
274
275
276 # b4_symbol_constructor_declare
277 # -----------------------------
278 # Declare symbol constructors for all the value types.
279 # Use at class-level.
280 m4_define([b4_symbol_constructor_declare],
281 [ // Symbol constructors declarations.
282 b4_symbol_foreach([b4_symbol_constructor_declare_])])
283
284
285
286 # b4_symbol_constructor_define_(SYMBOL-NUMBER)
287 # --------------------------------------------
288 # Define symbol constructor for this SYMBOL-NUMBER.
289 m4_define([b4_symbol_constructor_define_],
290 [b4_symbol_if([$1], [is_token], [b4_symbol_if([$1], [has_id],
291 [ b4_parser_class_name::symbol_type
292 b4_parser_class_name::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])))
296 {
297 return symbol_type (b4_join([token::b4_symbol([$1], [id])],
298 b4_symbol_if([$1], [has_type], [v]),
299 b4_locations_if([l])));
300 }
301
302 ])])])
303
304
305 # b4_symbol_constructor_define
306 # ----------------------------
307 # Define the overloaded versions of make_symbol for all the value types.
308 m4_define([b4_symbol_constructor_define],
309 [ // Implementation of make_symbol for each symbol type.
310 b4_symbol_foreach([b4_symbol_constructor_define_])])