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