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