]>
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>]])[ | |
75 | #ifndef YYASSERT | |
76 | # include <cassert> | |
77 | # define YYASSERT assert | |
78 | #endif | |
79 | ]]) | |
80 | ||
5f5a90df AD |
81 | # b4_variant_define |
82 | # ----------------- | |
507aa0e2 | 83 | # Define "variant". |
5f5a90df | 84 | m4_define([b4_variant_define], |
35f70d16 | 85 | [[ /// A char[S] buffer to store and retrieve objects. |
507aa0e2 AD |
86 | /// |
87 | /// Sort of a variant, but does not keep track of the nature | |
88 | /// of the stored data, since that knowledge is available | |
89 | /// via the current state. | |
90 | template <size_t S> | |
91 | struct variant | |
35f70d16 AD |
92 | { |
93 | /// Type of *this. | |
94 | typedef variant<S> self_type; | |
95 | ||
507aa0e2 | 96 | /// Empty construction. |
0c90a1f5 | 97 | variant ()]b4_parse_assert_if([ |
fbecd2ab | 98 | : tname (YY_NULL)])[ |
507aa0e2 AD |
99 | {} |
100 | ||
97ae878e AD |
101 | /// Construct and fill. |
102 | template <typename T> | |
103 | variant (const T& t)]b4_parse_assert_if([ | |
fbecd2ab | 104 | : tname (typeid (T).name ())])[ |
97ae878e AD |
105 | { |
106 | YYASSERT (sizeof (T) <= S); | |
85bc7f54 | 107 | new (as_ <T> ()) T (t); |
97ae878e AD |
108 | } |
109 | ||
110 | /// Destruction, allowed only if empty. | |
111 | ~variant () | |
112 | {]b4_parse_assert_if([ | |
fbecd2ab | 113 | YYASSERT (!tname); |
97ae878e AD |
114 | ])[} |
115 | ||
116 | /// Instantiate an empty \a T in here. | |
507aa0e2 | 117 | template <typename T> |
733fb7c5 | 118 | T& |
507aa0e2 | 119 | build () |
0c90a1f5 | 120 | {]b4_parse_assert_if([ |
97ae878e | 121 | YYASSERT (!tname); |
35f70d16 | 122 | YYASSERT (sizeof (T) <= S); |
35f70d16 | 123 | tname = typeid (T).name ();])[ |
85bc7f54 | 124 | return *new (as_<T> ()) T; |
507aa0e2 AD |
125 | } |
126 | ||
127 | /// Instantiate a \a T in here from \a t. | |
128 | template <typename T> | |
733fb7c5 | 129 | T& |
507aa0e2 | 130 | build (const T& t) |
0c90a1f5 | 131 | {]b4_parse_assert_if([ |
97ae878e | 132 | YYASSERT (!tname); |
35f70d16 | 133 | YYASSERT (sizeof (T) <= S); |
35f70d16 | 134 | tname = typeid (T).name ();])[ |
85bc7f54 | 135 | return *new (as_<T> ()) T (t); |
507aa0e2 AD |
136 | } |
137 | ||
507aa0e2 AD |
138 | /// Accessor to a built \a T. |
139 | template <typename T> | |
733fb7c5 | 140 | T& |
507aa0e2 | 141 | as () |
0c90a1f5 | 142 | {]b4_parse_assert_if([ |
35f70d16 AD |
143 | YYASSERT (tname == typeid (T).name ()); |
144 | YYASSERT (sizeof (T) <= S);])[ | |
85bc7f54 | 145 | return *as_<T> (); |
507aa0e2 AD |
146 | } |
147 | ||
148 | /// Const accessor to a built \a T (for %printer). | |
149 | template <typename T> | |
733fb7c5 | 150 | const T& |
507aa0e2 | 151 | as () const |
0c90a1f5 | 152 | {]b4_parse_assert_if([ |
35f70d16 AD |
153 | YYASSERT (tname == typeid (T).name ()); |
154 | YYASSERT (sizeof (T) <= S);])[ | |
85bc7f54 | 155 | return *as_<T> (); |
507aa0e2 AD |
156 | } |
157 | ||
35f70d16 | 158 | /// Swap the content with \a other, of same type. |
5f87211c | 159 | /// |
6656c9b5 TR |
160 | /// Both variants must be built beforehand, because swapping the actual |
161 | /// data requires reading it (with as()), and this is not possible on | |
162 | /// unconstructed variants: it would require some dynamic testing, which | |
163 | /// should not be the variant's responsability. | |
5f87211c | 164 | /// Swapping between built and (possibly) non-built is done with |
6656c9b5 | 165 | /// variant::move (). |
507aa0e2 | 166 | template <typename T> |
733fb7c5 | 167 | void |
858666c4 | 168 | swap (self_type& other) |
35f70d16 | 169 | {]b4_parse_assert_if([ |
fbecd2ab | 170 | YYASSERT (tname); |
35f70d16 | 171 | YYASSERT (tname == other.tname);])[ |
bb1f0f52 | 172 | std::swap (as<T>(), other.as<T>()); |
507aa0e2 AD |
173 | } |
174 | ||
5f87211c AD |
175 | /// Move the content of \a other to this. |
176 | /// | |
507aa0e2 AD |
177 | /// Destroys \a other. |
178 | template <typename T> | |
733fb7c5 | 179 | void |
858666c4 | 180 | move (self_type& other) |
6656c9b5 | 181 | {]b4_parse_assert_if([ |
fbecd2ab | 182 | YYASSERT (!tname);])[ |
507aa0e2 AD |
183 | build<T>(); |
184 | swap<T>(other); | |
185 | other.destroy<T>(); | |
186 | } | |
187 | ||
7be08dfb | 188 | /// Copy the content of \a other to this. |
7be08dfb | 189 | template <typename T> |
733fb7c5 | 190 | void |
858666c4 | 191 | copy (const self_type& other) |
7be08dfb AD |
192 | { |
193 | build<T> (other.as<T> ()); | |
194 | } | |
195 | ||
507aa0e2 AD |
196 | /// Destroy the stored \a T. |
197 | template <typename T> | |
733fb7c5 | 198 | void |
507aa0e2 AD |
199 | destroy () |
200 | { | |
35f70d16 | 201 | as<T> ().~T ();]b4_parse_assert_if([ |
35f70d16 | 202 | tname = YY_NULL;])[ |
507aa0e2 AD |
203 | } |
204 | ||
7d1aa2d6 | 205 | private: |
7be08dfb | 206 | /// Prohibit blind copies. |
7d1aa2d6 AD |
207 | self_type& operator=(const self_type&); |
208 | variant (const self_type&); | |
7be08dfb | 209 | |
85bc7f54 AD |
210 | /// Accessor to raw memory as \a T. |
211 | template <typename T> | |
212 | T* | |
213 | as_ () | |
214 | { | |
215 | void *yyp = buffer.raw; | |
216 | return static_cast<T*> (yyp); | |
217 | } | |
218 | ||
219 | /// Const accessor to raw memory as \a T. | |
220 | template <typename T> | |
221 | const T* | |
222 | as_ () const | |
223 | { | |
224 | const void *yyp = buffer.raw; | |
225 | return static_cast<const T*> (yyp); | |
226 | } | |
227 | ||
507aa0e2 AD |
228 | /// A buffer large enough to store any of the semantic values. |
229 | /// Long double is chosen as it has the strongest alignment | |
230 | /// constraints. | |
231 | union | |
232 | { | |
233 | long double align_me; | |
234 | char raw[S]; | |
35f70d16 | 235 | } buffer;]b4_parse_assert_if([ |
5f87211c | 236 | |
fbecd2ab | 237 | /// Whether the content is built: if defined, the name of the stored type. |
e96415a5 | 238 | const char *tname;])[ |
507aa0e2 AD |
239 | }; |
240 | ]]) | |
241 | ||
242 | ||
243 | ## -------------------------- ## | |
244 | ## Adjustments for variants. ## | |
245 | ## -------------------------- ## | |
246 | ||
247 | ||
b9e4eb5b AD |
248 | # b4_semantic_type_declare |
249 | # ------------------------ | |
250 | # Declare semantic_type. | |
251 | m4_define([b4_semantic_type_declare], | |
252 | [ /// An auxiliary type to compute the largest semantic type. | |
253 | union union_type | |
254 | {]b4_type_foreach([b4_char_sizeof])[}; | |
255 | ||
256 | /// Symbol semantic values. | |
35f70d16 AD |
257 | typedef variant<sizeof(union_type)> semantic_type;dnl |
258 | ]) | |
b9e4eb5b AD |
259 | |
260 | ||
507aa0e2 AD |
261 | # How the semantic value is extracted when using variants. |
262 | ||
263 | # b4_symbol_value(VAL, [TYPE]) | |
264 | # ---------------------------- | |
265 | m4_define([b4_symbol_value], | |
266 | [m4_ifval([$2], | |
267 | [$1.as< $2 >()], | |
268 | [$1])]) | |
269 | ||
270 | # b4_symbol_value_template(VAL, [TYPE]) | |
271 | # ------------------------------------- | |
272 | # Same as b4_symbol_value, but used in a template method. | |
273 | m4_define([b4_symbol_value_template], | |
274 | [m4_ifval([$2], | |
275 | [$1.template as< $2 >()], | |
276 | [$1])]) | |
0623bacc AD |
277 | |
278 | ||
279 | ||
280 | ## ------------- ## | |
281 | ## make_SYMBOL. ## | |
282 | ## ------------- ## | |
283 | ||
284 | ||
285 | # b4_symbol_constructor_declare_(SYMBOL-NUMBER) | |
286 | # --------------------------------------------- | |
287 | # Declare the overloaded version of make_symbol for the (common) type of | |
288 | # these SYMBOL-NUMBERS. Use at class-level. | |
289 | m4_define([b4_symbol_constructor_declare_], | |
290 | [b4_symbol_if([$1], [is_token], [b4_symbol_if([$1], [has_id], | |
291 | [ static inline | |
292 | symbol_type | |
293 | make_[]b4_symbol_([$1], [id]) (dnl | |
710c4a65 | 294 | b4_join(b4_symbol_if([$1], [has_type], |
0623bacc AD |
295 | [const b4_symbol([$1], [type])& v]), |
296 | b4_locations_if([const location_type& l]))); | |
297 | ||
298 | ])])]) | |
299 | ||
300 | ||
301 | # b4_symbol_constructor_declare | |
302 | # ----------------------------- | |
303 | # Declare symbol constructors for all the value types. | |
304 | # Use at class-level. | |
305 | m4_define([b4_symbol_constructor_declare], | |
306 | [ // Symbol constructors declarations. | |
307 | b4_symbol_foreach([b4_symbol_constructor_declare_])]) | |
308 | ||
309 | ||
310 | ||
311 | # b4_symbol_constructor_define_(SYMBOL-NUMBER) | |
312 | # -------------------------------------------- | |
313 | # Define symbol constructor for this SYMBOL-NUMBER. | |
314 | m4_define([b4_symbol_constructor_define_], | |
315 | [b4_symbol_if([$1], [is_token], [b4_symbol_if([$1], [has_id], | |
316 | [ b4_parser_class_name::symbol_type | |
317 | b4_parser_class_name::make_[]b4_symbol_([$1], [id]) (dnl | |
710c4a65 | 318 | b4_join(b4_symbol_if([$1], [has_type], |
0623bacc | 319 | [const b4_symbol([$1], [type])& v]), |
ee9cf8c4 | 320 | b4_locations_if([const location_type& l]))) |
0623bacc | 321 | { |
ee9cf8c4 TR |
322 | return symbol_type (b4_join([token::b4_symbol([$1], [id])], |
323 | b4_symbol_if([$1], [has_type], [v]), | |
324 | b4_locations_if([l]))); | |
325 | ||
0623bacc AD |
326 | } |
327 | ||
ee9cf8c4 TR |
328 | ])])]) |
329 | ||
0623bacc | 330 | |
ee9cf8c4 TR |
331 | # b4_basic_symbol_constructor_declare |
332 | # ---------------------------------- | |
333 | # Generate a constructor declaration for basic_symbol from given type. | |
334 | m4_define([b4_basic_symbol_constructor_declare], | |
335 | [[ | |
336 | basic_symbol (]b4_join( | |
337 | [typename Base::kind_type t], | |
338 | b4_symbol_if([$1], [has_type], const b4_symbol([$1], [type])[ v]), | |
339 | b4_locations_if([const location_type& l]))[); | |
340 | ]]) | |
341 | ||
342 | # b4_basic_symbol_constructor_define | |
343 | # ---------------------------------- | |
344 | # Generate a constructor implementation for basic_symbol from given type. | |
345 | m4_define([b4_basic_symbol_constructor_define], | |
346 | [[ | |
347 | template <typename Base> | |
348 | ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (]b4_join( | |
349 | [typename Base::kind_type t], | |
350 | b4_symbol_if([$1], [has_type], const b4_symbol([$1], [type])[ v]), | |
351 | b4_locations_if([const location_type& l]))[) | |
352 | : Base (t) | |
353 | , value (]b4_symbol_if([$1], [has_type], [v])[)]b4_locations_if([ | |
354 | , location (l)])[ | |
355 | {} | |
356 | ]]) | |
0623bacc AD |
357 | |
358 | # b4_symbol_constructor_define | |
359 | # ---------------------------- | |
360 | # Define the overloaded versions of make_symbol for all the value types. | |
361 | m4_define([b4_symbol_constructor_define], | |
362 | [ // Implementation of make_symbol for each symbol type. | |
363 | b4_symbol_foreach([b4_symbol_constructor_define_])]) |