]> git.saurik.com Git - bison.git/blame - data/variant.hh
Merge remote-tracking branch 'origin/maint'
[bison.git] / data / variant.hh
CommitLineData
507aa0e2
AD
1# C++ skeleton for Bison
2
34136e65 3# Copyright (C) 2002-2012 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
54# b4_char_sizeof_(SYMBOL-NUM)
55# ---------------------------
56# A comment describing this symbol.
57m4_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.
69m4_define([b4_char_sizeof],
70[b4_symbol_if([$1], [has_type],
71[
72m4_map([b4_char_sizeof_], [$@])dnl
73 char _b4_char_sizeof_dummy@{sizeof([b4_symbol([$1], [type])])@};
74])])
75
76
5f5a90df
AD
77# b4_variant_define
78# -----------------
507aa0e2 79# Define "variant".
5f5a90df 80m4_define([b4_variant_define],
507aa0e2
AD
81[[
82 /// A char[S] buffer to store and retrieve objects.
83 ///
84 /// Sort of a variant, but does not keep track of the nature
85 /// of the stored data, since that knowledge is available
86 /// via the current state.
87 template <size_t S>
88 struct variant
0c90a1f5 89 {]b4_parse_assert_if([
507aa0e2
AD
90 /// Whether something is contained.
91 bool built;
92])[
93 /// Empty construction.
94 inline
0c90a1f5 95 variant ()]b4_parse_assert_if([
507aa0e2
AD
96 : built (false)])[
97 {}
98
99 /// Instantiate a \a T in here.
100 template <typename T>
101 inline T&
102 build ()
0c90a1f5 103 {]b4_parse_assert_if([
507aa0e2
AD
104 assert (!built);
105 built = true;])[
106 return *new (buffer.raw) T;
107 }
108
109 /// Instantiate a \a T in here from \a t.
110 template <typename T>
111 inline T&
112 build (const T& t)
0c90a1f5 113 {]b4_parse_assert_if([
507aa0e2
AD
114 assert(!built);
115 built = true;])[
116 return *new (buffer.raw) T(t);
117 }
118
119 /// Construct and fill.
120 template <typename T>
121 inline
0c90a1f5 122 variant (const T& t)]b4_parse_assert_if([
507aa0e2
AD
123 : built (true)])[
124 {
125 new (buffer.raw) T(t);
126 }
127
128 /// Accessor to a built \a T.
129 template <typename T>
130 inline T&
131 as ()
0c90a1f5 132 {]b4_parse_assert_if([
507aa0e2
AD
133 assert (built);])[
134 return reinterpret_cast<T&>(buffer.raw);
135 }
136
137 /// Const accessor to a built \a T (for %printer).
138 template <typename T>
139 inline const T&
140 as () const
0c90a1f5 141 {]b4_parse_assert_if([
507aa0e2
AD
142 assert(built);])[
143 return reinterpret_cast<const T&>(buffer.raw);
144 }
145
146 /// Swap the content with \a other.
147 template <typename T>
148 inline void
149 swap (variant<S>& other)
150 {
151 std::swap (as<T>(), other.as<T>());
152 }
153
154 /// Assign the content of \a other to this.
155 /// Destroys \a other.
156 template <typename T>
157 inline void
158 build (variant<S>& other)
159 {
160 build<T>();
161 swap<T>(other);
162 other.destroy<T>();
163 }
164
165 /// Destroy the stored \a T.
166 template <typename T>
167 inline void
168 destroy ()
169 {
0c90a1f5 170 as<T>().~T();]b4_parse_assert_if([
507aa0e2
AD
171 built = false;])[
172 }
173
174 /// A buffer large enough to store any of the semantic values.
175 /// Long double is chosen as it has the strongest alignment
176 /// constraints.
177 union
178 {
179 long double align_me;
180 char raw[S];
181 } buffer;
182 };
183]])
184
185
186## -------------------------- ##
187## Adjustments for variants. ##
188## -------------------------- ##
189
190
b9e4eb5b
AD
191# b4_semantic_type_declare
192# ------------------------
193# Declare semantic_type.
194m4_define([b4_semantic_type_declare],
195[ /// An auxiliary type to compute the largest semantic type.
196 union union_type
197 {]b4_type_foreach([b4_char_sizeof])[};
198
199 /// Symbol semantic values.
200 typedef variant<sizeof(union_type)> semantic_type;])
201
202
507aa0e2
AD
203# How the semantic value is extracted when using variants.
204
205# b4_symbol_value(VAL, [TYPE])
206# ----------------------------
207m4_define([b4_symbol_value],
208[m4_ifval([$2],
209 [$1.as< $2 >()],
210 [$1])])
211
212# b4_symbol_value_template(VAL, [TYPE])
213# -------------------------------------
214# Same as b4_symbol_value, but used in a template method.
215m4_define([b4_symbol_value_template],
216[m4_ifval([$2],
217 [$1.template as< $2 >()],
218 [$1])])
0623bacc
AD
219
220
221
222## ------------- ##
223## make_SYMBOL. ##
224## ------------- ##
225
226
227# b4_symbol_constructor_declare_(SYMBOL-NUMBER)
228# ---------------------------------------------
229# Declare the overloaded version of make_symbol for the (common) type of
230# these SYMBOL-NUMBERS. Use at class-level.
231m4_define([b4_symbol_constructor_declare_],
232[b4_symbol_if([$1], [is_token], [b4_symbol_if([$1], [has_id],
233[ static inline
234 symbol_type
235 make_[]b4_symbol_([$1], [id]) (dnl
236b4_args(b4_symbol_if([$1], [has_type],
237 [const b4_symbol([$1], [type])& v]),
238 b4_locations_if([const location_type& l])));
239
240])])])
241
242
243# b4_symbol_constructor_declare
244# -----------------------------
245# Declare symbol constructors for all the value types.
246# Use at class-level.
247m4_define([b4_symbol_constructor_declare],
248[ // Symbol constructors declarations.
249b4_symbol_foreach([b4_symbol_constructor_declare_])])
250
251
252
253# b4_symbol_constructor_define_(SYMBOL-NUMBER)
254# --------------------------------------------
255# Define symbol constructor for this SYMBOL-NUMBER.
256m4_define([b4_symbol_constructor_define_],
257[b4_symbol_if([$1], [is_token], [b4_symbol_if([$1], [has_id],
258[ b4_parser_class_name::symbol_type
259 b4_parser_class_name::make_[]b4_symbol_([$1], [id]) (dnl
260b4_args(b4_symbol_if([$1], [has_type],
261 [const b4_symbol([$1], [type])& v]),
262 b4_locations_if([const location_type& l])))
263 {
2b08bceb 264 return symbol_type (b4_args([token::b4_symbol([$1], [id])],
0623bacc
AD
265 b4_symbol_if([$1], [has_type], [v]),
266 b4_locations_if([l])));
267 }
268
269])])])
270
271
272# b4_symbol_constructor_define
273# ----------------------------
274# Define the overloaded versions of make_symbol for all the value types.
275m4_define([b4_symbol_constructor_define],
276[ // Implementation of make_symbol for each symbol type.
277b4_symbol_foreach([b4_symbol_constructor_define_])])