]>
Commit | Line | Data |
---|---|---|
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. | |
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 | ||
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. | |
62 | m4_define([b4_char_sizeof], | |
63 | [b4_symbol_if([$1], [has_type], | |
64 | [ | |
3fd1d6b2 | 65 | m4_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. | |
73 | m4_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 | 85 | m4_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 | ||
103 | /// Instantiate a \a T in here. | |
104 | template <typename T> | |
733fb7c5 | 105 | T& |
507aa0e2 | 106 | build () |
0c90a1f5 | 107 | {]b4_parse_assert_if([ |
bb1f0f52 TR |
108 | //YYASSERT (!built); |
109 | //YYASSERT (!tname); | |
35f70d16 AD |
110 | YYASSERT (sizeof (T) <= S); |
111 | built = true; | |
112 | tname = typeid (T).name ();])[ | |
507aa0e2 AD |
113 | return *new (buffer.raw) T; |
114 | } | |
115 | ||
116 | /// Instantiate a \a T in here from \a t. | |
117 | template <typename T> | |
733fb7c5 | 118 | T& |
507aa0e2 | 119 | build (const T& t) |
0c90a1f5 | 120 | {]b4_parse_assert_if([ |
bb1f0f52 TR |
121 | //YYASSERT (!built); |
122 | //YYASSERT (!tname); | |
35f70d16 AD |
123 | YYASSERT (sizeof (T) <= S); |
124 | built = true; | |
125 | tname = typeid (T).name ();])[ | |
126 | return *new (buffer.raw) T (t); | |
507aa0e2 AD |
127 | } |
128 | ||
129 | /// Construct and fill. | |
130 | template <typename T> | |
0c90a1f5 | 131 | variant (const T& t)]b4_parse_assert_if([ |
35f70d16 AD |
132 | : built (true) |
133 | , tname (typeid (T).name ())])[ | |
507aa0e2 | 134 | { |
35f70d16 AD |
135 | YYASSERT (sizeof (T) <= S); |
136 | new (buffer.raw) T (t); | |
507aa0e2 AD |
137 | } |
138 | ||
139 | /// Accessor to a built \a T. | |
140 | template <typename T> | |
733fb7c5 | 141 | T& |
507aa0e2 | 142 | as () |
0c90a1f5 | 143 | {]b4_parse_assert_if([ |
35f70d16 AD |
144 | YYASSERT (built); |
145 | YYASSERT (tname == typeid (T).name ()); | |
146 | YYASSERT (sizeof (T) <= S);])[ | |
147 | return reinterpret_cast<T&> (buffer.raw); | |
507aa0e2 AD |
148 | } |
149 | ||
150 | /// Const accessor to a built \a T (for %printer). | |
151 | template <typename T> | |
733fb7c5 | 152 | const T& |
507aa0e2 | 153 | as () const |
0c90a1f5 | 154 | {]b4_parse_assert_if([ |
35f70d16 AD |
155 | YYASSERT (built); |
156 | YYASSERT (tname == typeid (T).name ()); | |
157 | YYASSERT (sizeof (T) <= S);])[ | |
158 | return reinterpret_cast<const T&> (buffer.raw); | |
507aa0e2 AD |
159 | } |
160 | ||
35f70d16 | 161 | /// Swap the content with \a other, of same type. |
6656c9b5 TR |
162 | /// Both variants must be built beforehand, because swapping the actual |
163 | /// data requires reading it (with as()), and this is not possible on | |
164 | /// unconstructed variants: it would require some dynamic testing, which | |
165 | /// should not be the variant's responsability. | |
166 | /// Swapping between built and ((possibly) non-built is done with | |
167 | /// variant::move (). | |
507aa0e2 | 168 | template <typename T> |
733fb7c5 | 169 | void |
507aa0e2 | 170 | swap (variant<S>& other) |
35f70d16 | 171 | {]b4_parse_assert_if([ |
bb1f0f52 TR |
172 | YYASSERT (built); |
173 | YYASSERT (other.built); | |
35f70d16 | 174 | YYASSERT (tname == other.tname);])[ |
bb1f0f52 | 175 | std::swap (as<T>(), other.as<T>()); |
507aa0e2 AD |
176 | } |
177 | ||
178 | /// Assign the content of \a other to this. | |
179 | /// Destroys \a other. | |
180 | template <typename T> | |
733fb7c5 | 181 | void |
6656c9b5 TR |
182 | move (variant<S>& other) |
183 | {]b4_parse_assert_if([ | |
184 | YYASSERT (! built);])[ | |
507aa0e2 AD |
185 | build<T>(); |
186 | swap<T>(other); | |
187 | other.destroy<T>(); | |
188 | } | |
189 | ||
7be08dfb AD |
190 | /// Copy the content of \a other to this. |
191 | /// Destroys \a other. | |
192 | template <typename T> | |
733fb7c5 | 193 | void |
7be08dfb AD |
194 | copy (const variant<S>& other) |
195 | { | |
196 | build<T> (other.as<T> ()); | |
197 | } | |
198 | ||
507aa0e2 AD |
199 | /// Destroy the stored \a T. |
200 | template <typename T> | |
733fb7c5 | 201 | void |
507aa0e2 AD |
202 | destroy () |
203 | { | |
35f70d16 AD |
204 | as<T> ().~T ();]b4_parse_assert_if([ |
205 | built = false; | |
206 | tname = YY_NULL;])[ | |
507aa0e2 AD |
207 | } |
208 | ||
7be08dfb | 209 | /// Prohibit blind copies. |
04816a6f | 210 | private: |
7be08dfb AD |
211 | self_type& operator=(const self_type&) |
212 | { | |
213 | abort (); | |
214 | } | |
215 | ||
bb1f0f52 TR |
216 | variant (const self_type&) |
217 | { | |
218 | abort (); | |
219 | } | |
220 | ||
35f70d16 | 221 | private: |
507aa0e2 AD |
222 | /// A buffer large enough to store any of the semantic values. |
223 | /// Long double is chosen as it has the strongest alignment | |
224 | /// constraints. | |
225 | union | |
226 | { | |
227 | long double align_me; | |
228 | char raw[S]; | |
35f70d16 AD |
229 | } buffer;]b4_parse_assert_if([ |
230 | /// Whether something is contained. | |
231 | bool built; | |
232 | /// If defined, the name of the stored type. | |
233 | const char* tname;])[ | |
507aa0e2 AD |
234 | }; |
235 | ]]) | |
236 | ||
237 | ||
238 | ## -------------------------- ## | |
239 | ## Adjustments for variants. ## | |
240 | ## -------------------------- ## | |
241 | ||
242 | ||
b9e4eb5b AD |
243 | # b4_semantic_type_declare |
244 | # ------------------------ | |
245 | # Declare semantic_type. | |
246 | m4_define([b4_semantic_type_declare], | |
247 | [ /// An auxiliary type to compute the largest semantic type. | |
248 | union union_type | |
249 | {]b4_type_foreach([b4_char_sizeof])[}; | |
250 | ||
251 | /// Symbol semantic values. | |
35f70d16 AD |
252 | typedef variant<sizeof(union_type)> semantic_type;dnl |
253 | ]) | |
b9e4eb5b AD |
254 | |
255 | ||
507aa0e2 AD |
256 | # How the semantic value is extracted when using variants. |
257 | ||
258 | # b4_symbol_value(VAL, [TYPE]) | |
259 | # ---------------------------- | |
260 | m4_define([b4_symbol_value], | |
261 | [m4_ifval([$2], | |
262 | [$1.as< $2 >()], | |
263 | [$1])]) | |
264 | ||
265 | # b4_symbol_value_template(VAL, [TYPE]) | |
266 | # ------------------------------------- | |
267 | # Same as b4_symbol_value, but used in a template method. | |
268 | m4_define([b4_symbol_value_template], | |
269 | [m4_ifval([$2], | |
270 | [$1.template as< $2 >()], | |
271 | [$1])]) | |
0623bacc AD |
272 | |
273 | ||
274 | ||
275 | ## ------------- ## | |
276 | ## make_SYMBOL. ## | |
277 | ## ------------- ## | |
278 | ||
279 | ||
280 | # b4_symbol_constructor_declare_(SYMBOL-NUMBER) | |
281 | # --------------------------------------------- | |
282 | # Declare the overloaded version of make_symbol for the (common) type of | |
283 | # these SYMBOL-NUMBERS. Use at class-level. | |
284 | m4_define([b4_symbol_constructor_declare_], | |
285 | [b4_symbol_if([$1], [is_token], [b4_symbol_if([$1], [has_id], | |
286 | [ static inline | |
287 | symbol_type | |
288 | make_[]b4_symbol_([$1], [id]) (dnl | |
710c4a65 | 289 | b4_join(b4_symbol_if([$1], [has_type], |
0623bacc AD |
290 | [const b4_symbol([$1], [type])& v]), |
291 | b4_locations_if([const location_type& l]))); | |
292 | ||
293 | ])])]) | |
294 | ||
295 | ||
296 | # b4_symbol_constructor_declare | |
297 | # ----------------------------- | |
298 | # Declare symbol constructors for all the value types. | |
299 | # Use at class-level. | |
300 | m4_define([b4_symbol_constructor_declare], | |
301 | [ // Symbol constructors declarations. | |
302 | b4_symbol_foreach([b4_symbol_constructor_declare_])]) | |
303 | ||
304 | ||
305 | ||
306 | # b4_symbol_constructor_define_(SYMBOL-NUMBER) | |
307 | # -------------------------------------------- | |
308 | # Define symbol constructor for this SYMBOL-NUMBER. | |
309 | m4_define([b4_symbol_constructor_define_], | |
310 | [b4_symbol_if([$1], [is_token], [b4_symbol_if([$1], [has_id], | |
311 | [ b4_parser_class_name::symbol_type | |
312 | b4_parser_class_name::make_[]b4_symbol_([$1], [id]) (dnl | |
710c4a65 | 313 | b4_join(b4_symbol_if([$1], [has_type], |
0623bacc AD |
314 | [const b4_symbol([$1], [type])& v]), |
315 | b4_locations_if([const location_type& l]))) | |
316 | { | |
710c4a65 | 317 | return symbol_type (b4_join([token::b4_symbol([$1], [id])], |
0623bacc AD |
318 | b4_symbol_if([$1], [has_type], [v]), |
319 | b4_locations_if([l]))); | |
320 | } | |
321 | ||
322 | ])])]) | |
323 | ||
324 | ||
325 | # b4_symbol_constructor_define | |
326 | # ---------------------------- | |
327 | # Define the overloaded versions of make_symbol for all the value types. | |
328 | m4_define([b4_symbol_constructor_define], | |
329 | [ // Implementation of make_symbol for each symbol type. | |
330 | b4_symbol_foreach([b4_symbol_constructor_define_])]) |