]> git.saurik.com Git - bison.git/blame - data/variant.hh
tests: enable support for --debug
[bison.git] / data / variant.hh
CommitLineData
507aa0e2
AD
1# C++ skeleton for Bison
2
7d6bad19 3# Copyright (C) 2002-2013 Free Software Foundation, Inc.
507aa0e2
AD
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.
27m4_define([b4_symbol_variant],
28[m4_pushdef([b4_dollar_dollar],
29 [$2.$3< $][3 >(m4_shift3($@))])dnl
30 switch ($1)
31 {
32b4_type_foreach([b4_type_action_])[]dnl
33 default:
34 break;
35 }
36m4_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.
43m4_define([_b4_char_sizeof_counter],
44[0])
45
46# _b4_char_sizeof_dummy
47# ---------------------
48# At each call return a new C++ identifier.
49m4_define([_b4_char_sizeof_dummy],
50[m4_define([_b4_char_sizeof_counter], m4_incr(_b4_char_sizeof_counter))dnl
51dummy[]_b4_char_sizeof_counter])
52
53
507aa0e2
AD
54# b4_char_sizeof(SYMBOL-NUMS)
55# ---------------------------
56# To be mapped on the list of type names to produce:
57#
58# char dummy1[sizeof(type_name_1)];
59# char dummy2[sizeof(type_name_2)];
60#
61# for defined type names.
62m4_define([b4_char_sizeof],
63[b4_symbol_if([$1], [has_type],
64[
3fd1d6b2 65m4_map([ b4_symbol_tag_comment], [$@])dnl
507aa0e2
AD
66 char _b4_char_sizeof_dummy@{sizeof([b4_symbol([$1], [type])])@};
67])])
68
69
35f70d16
AD
70# b4_variant_includes
71# -------------------
72# The needed includes for variants support.
73m4_define([b4_variant_includes],
74[b4_parse_assert_if([[#include <typeinfo>]])[
7be08dfb 75#include <cstdlib> // abort
35f70d16
AD
76#ifndef YYASSERT
77# include <cassert>
78# define YYASSERT assert
79#endif
80]])
81
5f5a90df
AD
82# b4_variant_define
83# -----------------
507aa0e2 84# Define "variant".
5f5a90df 85m4_define([b4_variant_define],
35f70d16 86[[ /// A char[S] buffer to store and retrieve objects.
507aa0e2
AD
87 ///
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.
91 template <size_t S>
92 struct variant
35f70d16
AD
93 {
94 /// Type of *this.
95 typedef variant<S> self_type;
96
507aa0e2 97 /// Empty construction.
0c90a1f5 98 variant ()]b4_parse_assert_if([
35f70d16
AD
99 : built (false)
100 , tname (YY_NULL)])[
507aa0e2
AD
101 {}
102
97ae878e
AD
103 /// Construct and fill.
104 template <typename T>
105 variant (const T& t)]b4_parse_assert_if([
106 : built (true)
107 , tname (typeid (T).name ())])[
108 {
109 YYASSERT (sizeof (T) <= S);
110 new (buffer.raw) T (t);
111 }
112
113 /// Destruction, allowed only if empty.
114 ~variant ()
115 {]b4_parse_assert_if([
116 YYASSERT (!built);
117 ])[}
118
119 /// Instantiate an empty \a T in here.
507aa0e2 120 template <typename T>
733fb7c5 121 T&
507aa0e2 122 build ()
0c90a1f5 123 {]b4_parse_assert_if([
97ae878e
AD
124 YYASSERT (!built);
125 YYASSERT (!tname);
35f70d16
AD
126 YYASSERT (sizeof (T) <= S);
127 built = true;
128 tname = typeid (T).name ();])[
507aa0e2
AD
129 return *new (buffer.raw) T;
130 }
131
132 /// Instantiate a \a T in here from \a t.
133 template <typename T>
733fb7c5 134 T&
507aa0e2 135 build (const T& t)
0c90a1f5 136 {]b4_parse_assert_if([
97ae878e
AD
137 YYASSERT (!built);
138 YYASSERT (!tname);
35f70d16
AD
139 YYASSERT (sizeof (T) <= S);
140 built = true;
141 tname = typeid (T).name ();])[
142 return *new (buffer.raw) T (t);
507aa0e2
AD
143 }
144
507aa0e2
AD
145 /// Accessor to a built \a T.
146 template <typename T>
733fb7c5 147 T&
507aa0e2 148 as ()
0c90a1f5 149 {]b4_parse_assert_if([
35f70d16
AD
150 YYASSERT (built);
151 YYASSERT (tname == typeid (T).name ());
152 YYASSERT (sizeof (T) <= S);])[
153 return reinterpret_cast<T&> (buffer.raw);
507aa0e2
AD
154 }
155
156 /// Const accessor to a built \a T (for %printer).
157 template <typename T>
733fb7c5 158 const T&
507aa0e2 159 as () const
0c90a1f5 160 {]b4_parse_assert_if([
35f70d16
AD
161 YYASSERT (built);
162 YYASSERT (tname == typeid (T).name ());
163 YYASSERT (sizeof (T) <= S);])[
164 return reinterpret_cast<const T&> (buffer.raw);
507aa0e2
AD
165 }
166
35f70d16 167 /// Swap the content with \a other, of same type.
5f87211c 168 ///
6656c9b5
TR
169 /// Both variants must be built beforehand, because swapping the actual
170 /// data requires reading it (with as()), and this is not possible on
171 /// unconstructed variants: it would require some dynamic testing, which
172 /// should not be the variant's responsability.
5f87211c 173 /// Swapping between built and (possibly) non-built is done with
6656c9b5 174 /// variant::move ().
507aa0e2 175 template <typename T>
733fb7c5 176 void
507aa0e2 177 swap (variant<S>& other)
35f70d16 178 {]b4_parse_assert_if([
bb1f0f52
TR
179 YYASSERT (built);
180 YYASSERT (other.built);
35f70d16 181 YYASSERT (tname == other.tname);])[
bb1f0f52 182 std::swap (as<T>(), other.as<T>());
507aa0e2
AD
183 }
184
5f87211c
AD
185 /// Move the content of \a other to this.
186 ///
507aa0e2
AD
187 /// Destroys \a other.
188 template <typename T>
733fb7c5 189 void
6656c9b5
TR
190 move (variant<S>& other)
191 {]b4_parse_assert_if([
192 YYASSERT (! built);])[
507aa0e2
AD
193 build<T>();
194 swap<T>(other);
195 other.destroy<T>();
196 }
197
7be08dfb 198 /// Copy the content of \a other to this.
7be08dfb 199 template <typename T>
733fb7c5 200 void
7be08dfb
AD
201 copy (const variant<S>& other)
202 {
203 build<T> (other.as<T> ());
204 }
205
507aa0e2
AD
206 /// Destroy the stored \a T.
207 template <typename T>
733fb7c5 208 void
507aa0e2
AD
209 destroy ()
210 {
35f70d16
AD
211 as<T> ().~T ();]b4_parse_assert_if([
212 built = false;
213 tname = YY_NULL;])[
507aa0e2
AD
214 }
215
7be08dfb 216 /// Prohibit blind copies.
04816a6f 217 private:
7be08dfb
AD
218 self_type& operator=(const self_type&)
219 {
220 abort ();
221 }
222
bb1f0f52
TR
223 variant (const self_type&)
224 {
225 abort ();
226 }
227
35f70d16 228 private:
507aa0e2
AD
229 /// A buffer large enough to store any of the semantic values.
230 /// Long double is chosen as it has the strongest alignment
231 /// constraints.
232 union
233 {
234 long double align_me;
235 char raw[S];
35f70d16 236 } buffer;]b4_parse_assert_if([
5f87211c
AD
237
238 /// Whether the content is built.
35f70d16
AD
239 bool built;
240 /// If defined, the name of the stored type.
241 const char* tname;])[
507aa0e2
AD
242 };
243]])
244
245
246## -------------------------- ##
247## Adjustments for variants. ##
248## -------------------------- ##
249
250
b9e4eb5b
AD
251# b4_semantic_type_declare
252# ------------------------
253# Declare semantic_type.
254m4_define([b4_semantic_type_declare],
255[ /// An auxiliary type to compute the largest semantic type.
256 union union_type
257 {]b4_type_foreach([b4_char_sizeof])[};
258
259 /// Symbol semantic values.
35f70d16
AD
260 typedef variant<sizeof(union_type)> semantic_type;dnl
261])
b9e4eb5b
AD
262
263
507aa0e2
AD
264# How the semantic value is extracted when using variants.
265
266# b4_symbol_value(VAL, [TYPE])
267# ----------------------------
268m4_define([b4_symbol_value],
269[m4_ifval([$2],
270 [$1.as< $2 >()],
271 [$1])])
272
273# b4_symbol_value_template(VAL, [TYPE])
274# -------------------------------------
275# Same as b4_symbol_value, but used in a template method.
276m4_define([b4_symbol_value_template],
277[m4_ifval([$2],
278 [$1.template as< $2 >()],
279 [$1])])
0623bacc
AD
280
281
282
283## ------------- ##
284## make_SYMBOL. ##
285## ------------- ##
286
287
288# b4_symbol_constructor_declare_(SYMBOL-NUMBER)
289# ---------------------------------------------
290# Declare the overloaded version of make_symbol for the (common) type of
291# these SYMBOL-NUMBERS. Use at class-level.
292m4_define([b4_symbol_constructor_declare_],
293[b4_symbol_if([$1], [is_token], [b4_symbol_if([$1], [has_id],
294[ static inline
295 symbol_type
296 make_[]b4_symbol_([$1], [id]) (dnl
710c4a65 297b4_join(b4_symbol_if([$1], [has_type],
0623bacc
AD
298 [const b4_symbol([$1], [type])& v]),
299 b4_locations_if([const location_type& l])));
300
301])])])
302
303
304# b4_symbol_constructor_declare
305# -----------------------------
306# Declare symbol constructors for all the value types.
307# Use at class-level.
308m4_define([b4_symbol_constructor_declare],
309[ // Symbol constructors declarations.
310b4_symbol_foreach([b4_symbol_constructor_declare_])])
311
312
313
314# b4_symbol_constructor_define_(SYMBOL-NUMBER)
315# --------------------------------------------
316# Define symbol constructor for this SYMBOL-NUMBER.
317m4_define([b4_symbol_constructor_define_],
318[b4_symbol_if([$1], [is_token], [b4_symbol_if([$1], [has_id],
319[ b4_parser_class_name::symbol_type
320 b4_parser_class_name::make_[]b4_symbol_([$1], [id]) (dnl
710c4a65 321b4_join(b4_symbol_if([$1], [has_type],
0623bacc 322 [const b4_symbol([$1], [type])& v]),
97ae878e 323 b4_locations_if([const location_type& l])))[
0623bacc 324 {
97ae878e
AD
325 symbol_type res (token::]b4_symbol([$1], [id])[]b4_locations_if([, l])[);
326 ]b4_symbol_if([$1], [has_type], [res.value.build (v);])[
327 // ]b4_locations_if([res.location = l;])[
328 return res;
0623bacc
AD
329 }
330
97ae878e 331]])])])
0623bacc
AD
332
333
334# b4_symbol_constructor_define
335# ----------------------------
336# Define the overloaded versions of make_symbol for all the value types.
337m4_define([b4_symbol_constructor_define],
338[ // Implementation of make_symbol for each symbol type.
339b4_symbol_foreach([b4_symbol_constructor_define_])])