]> git.saurik.com Git - bison.git/blob - data/c++.m4
c++: privatize variant blind copies
[bison.git] / data / c++.m4
1 -*- Autoconf -*-
2
3 # C++ skeleton for Bison
4
5 # Copyright (C) 2002-2012 Free Software Foundation, Inc.
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 m4_include(b4_pkgdatadir/[c.m4])
21
22 # b4_comment(TEXT, [PREFIX])
23 # --------------------------
24 # Put TEXT in comment. Prefix all the output lines with PREFIX.
25 m4_define([b4_comment],
26 [b4_comment_([$1], [$2// ], [$2// ])])
27
28
29 ## ---------------- ##
30 ## Default values. ##
31 ## ---------------- ##
32
33 b4_percent_define_default([[parser_class_name]], [[parser]])
34
35 # Don't do that so that we remember whether we're using a user
36 # request, or the default value.
37 #
38 # b4_percent_define_default([[api.location.type]], [[location]])
39
40 b4_percent_define_default([[filename_type]], [[std::string]])
41 b4_percent_define_default([[api.namespace]], m4_defn([b4_prefix]))
42
43 b4_percent_define_default([[global_tokens_and_yystype]], [[false]])
44 b4_percent_define_default([[define_location_comparison]],
45 [m4_if(b4_percent_define_get([[filename_type]]),
46 [std::string], [[true]], [[false]])])
47
48
49
50 ## ----------- ##
51 ## Namespace. ##
52 ## ----------- ##
53
54 m4_define([b4_namespace_ref], [b4_percent_define_get([[api.namespace]])])
55
56
57 # Don't permit an empty b4_namespace_ref. Any `::parser::foo' appended to it
58 # would compile as an absolute reference with `parser' in the global namespace.
59 # b4_namespace_open would open an anonymous namespace and thus establish
60 # internal linkage. This would compile. However, it's cryptic, and internal
61 # linkage for the parser would be specified in all translation units that
62 # include the header, which is always generated. If we ever need to permit
63 # internal linkage somehow, surely we can find a cleaner approach.
64 m4_if(m4_bregexp(b4_namespace_ref, [^[ ]*$]), [-1], [],
65 [b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
66 [[namespace reference is empty]])])
67
68 # Instead of assuming the C++ compiler will do it, Bison should reject any
69 # invalid b4_namepsace_ref that would be converted to a valid
70 # b4_namespace_open. The problem is that Bison doesn't always output
71 # b4_namespace_ref to uncommented code but should reserve the ability to do so
72 # in future releases without risking breaking any existing user grammars.
73 # Specifically, don't allow empty names as b4_namespace_open would just convert
74 # those into anonymous namespaces, and that might tempt some users.
75 m4_if(m4_bregexp(b4_namespace_ref, [::[ ]*::]), [-1], [],
76 [b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
77 [[namespace reference has consecutive "::"]])])
78 m4_if(m4_bregexp(b4_namespace_ref, [::[ ]*$]), [-1], [],
79 [b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
80 [[namespace reference has a trailing "::"]])])
81
82 m4_define([b4_namespace_open],
83 [b4_user_code([b4_percent_define_get_syncline([[api.namespace]])
84 [namespace ]m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(b4_namespace_ref),
85 [^\(.\)[ ]*::], [\1])),
86 [::], [ { namespace ])[ {]])])
87
88 m4_define([b4_namespace_close],
89 [b4_user_code([b4_percent_define_get_syncline([[api.namespace]])
90 m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(b4_namespace_ref[ ]),
91 [^\(.\)[ ]*\(::\)?\([^][:]\|:[^:]\)*],
92 [\1])),
93 [::\([^][:]\|:[^:]\)*], [} ])[} // ]b4_namespace_ref])])
94
95
96 # b4_token_enums
97 # --------------
98 # Output the definition of the tokens as enums.
99 m4_define([b4_token_enums],
100 [[enum yytokentype
101 {
102 ]m4_join([,
103 ],
104 b4_symbol_map([b4_token_enum]))[
105 };]dnl
106 ])
107
108
109
110
111 ## ----------------- ##
112 ## Semantic Values. ##
113 ## ----------------- ##
114
115 # b4_semantic_type_declare
116 # ------------------------
117 # Declare semantic_type.
118 m4_define([b4_semantic_type_declare],
119 [ /// Symbol semantic values.
120 m4_ifdef([b4_stype],
121 [ union semantic_type
122 {
123 b4_user_stype
124 };],
125 [m4_if(b4_tag_seen_flag, 0,
126 [[ typedef int semantic_type;]],
127 [[ typedef ]b4_api_PREFIX[STYPE semantic_type;]])])])
128
129
130 # b4_public_types_declare
131 # -----------------------
132 # Define the public types: token, semantic value, location, and so forth.
133 # Depending on %define token_lex, may be output in the header or source file.
134 m4_define([b4_public_types_declare],
135 [[#ifndef ]b4_api_PREFIX[STYPE
136 ]b4_semantic_type_declare[
137 #else
138 typedef ]b4_api_PREFIX[STYPE semantic_type;
139 #endif]b4_locations_if([
140 /// Symbol locations.
141 typedef b4_percent_define_get([[api.location.type]],
142 [[location]]) location_type;])[
143
144 /// Syntax errors thrown from user actions.
145 struct syntax_error : std::runtime_error
146 {
147 syntax_error (]b4_locations_if([const location_type& l, ])[const std::string& m);]b4_locations_if([
148 location_type location;])[
149 };
150
151 /// Tokens.
152 struct token
153 {
154 ]b4_token_enums[
155 };
156
157 /// Token type.
158 typedef token::yytokentype token_type;
159
160 /// A complete symbol.
161 ///
162 /// Expects its Base type to provide access to the symbol type
163 /// via type_get().
164 ///
165 /// Provide access to semantic value]b4_locations_if([ and location])[.
166 template <typename Base>
167 struct basic_symbol : Base
168 {
169 /// Default constructor.
170 inline basic_symbol ();
171 ]b4_locations_if([
172 /// Constructor.
173 inline basic_symbol (const location_type& l);])[
174
175 /// Copy constructor.
176 inline basic_symbol (const basic_symbol& other);
177
178 /// Constructor for valueless symbols.
179 inline basic_symbol (]b4_join(
180 [typename Base::value_type t],
181 b4_locations_if([const location_type& l]))[);
182
183 /// Constructor for symbols with semantic value.
184 inline basic_symbol (]b4_join(
185 [typename Base::value_type t],
186 [const semantic_type& v],
187 b4_locations_if([const location_type& l]))[);
188
189 /// Assignment operator.
190 inline basic_symbol& operator= (const basic_symbol& other);
191
192 /// Destructive move, \a s is emptied.
193 inline void move (basic_symbol& s);
194
195 /// The semantic value.
196 semantic_type value;]b4_locations_if([
197
198 /// The location.
199 location_type location;])[
200 };
201
202 /// Type access provider for token (enum) based symbols.
203 struct by_type
204 {
205 /// Default constructor.
206 inline by_type ();
207
208 /// Copy constructor.
209 inline by_type (const by_type& other);
210
211 /// Constructor.
212 inline by_type (token_type t);
213
214 /// The symbol type.
215 int type;
216
217 /// The type (corresponding to \a type).
218 inline int type_get () const;
219
220 /// The token.
221 inline token_type token () const;
222
223 typedef token_type value_type;
224 };
225
226 /// "External" symbols: returned by the scanner.
227 typedef basic_symbol<by_type> symbol_type;
228
229 ]b4_symbol_constructor_declare])
230
231
232 # b4_public_types_define
233 # ----------------------
234 # Provide the implementation needed by the public types.
235 m4_define([b4_public_types_define],
236 [[inline
237 ]b4_parser_class_name[::syntax_error::syntax_error (]b4_locations_if([const location_type& l, ])[const std::string& m)
238 : std::runtime_error (m)]b4_locations_if([
239 , location (l)])[
240 {}
241
242 // basic_symbol.
243 template <typename Base>
244 inline
245 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol ()
246 : value ()
247 {}
248 ]b4_locations_if([
249 template <typename Base>
250 inline
251 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (const location_type& l)
252 : value ()
253 , location (l)
254 {}])[
255
256 template <typename Base>
257 inline
258 ]b4_parser_class_name[::basic_symbol<Base>&
259 ]b4_parser_class_name[::basic_symbol<Base>::operator= (const basic_symbol& other)
260 {
261 abort ();
262 }
263
264 template <typename Base>
265 inline
266 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (const basic_symbol& other)
267 : Base (other)
268 , value ()]b4_locations_if([
269 , location (other.location)])[
270 {
271 ]b4_variant_if([b4_symbol_variant([other.type_get ()], [value], [copy],
272 [other.value])],
273 [value = other.value;])[
274 }
275
276
277 template <typename Base>
278 inline
279 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (]b4_join(
280 [typename Base::value_type t],
281 [const semantic_type& v],
282 b4_locations_if([const location_type& l]))[)
283 : Base (t)
284 , value ()]b4_locations_if([
285 , location (l)])[
286 {
287 (void) v; /* FIXME: */
288 ]b4_variant_if([b4_symbol_variant([this->type_get ()], [value], [copy],
289 [v])],
290 [value = v;])[
291 }
292
293 template <typename Base>
294 inline
295 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (]b4_join(
296 [typename Base::value_type t],
297 b4_locations_if([const location_type& l]))[)
298 : Base (t)]b4_locations_if([
299 , location (l)])[
300 {}
301
302 template <typename Base>
303 inline
304 void
305 ]b4_parser_class_name[::basic_symbol<Base>::move (basic_symbol& s)
306 {
307 this->type = s.type_get ();]b4_locations_if([
308 location = s.location;])[
309 ]b4_variant_if([b4_symbol_variant([s.type_get ()], [value], [build],
310 [s.value])],
311 [value = s.value;])[
312 }
313
314 // by_type.
315 inline
316 ]b4_parser_class_name[::by_type::by_type ()
317 : type ()
318 {}
319
320 inline
321 ]b4_parser_class_name[::by_type::by_type (const by_type& other)
322 : type (other.type)
323 {}
324
325 inline
326 ]b4_parser_class_name[::by_type::by_type (token_type t)
327 : type (yytranslate_ (t))
328 {}
329
330 inline
331 int
332 ]b4_parser_class_name[::by_type::type_get () const
333 {
334 return type;
335 }
336 ]b4_token_ctor_if([[
337 inline
338 ]b4_parser_class_name[::token_type
339 ]b4_parser_class_name[::by_type::token () const
340 {
341 // YYTOKNUM[NUM] -- (External) token number corresponding to the
342 // (internal) symbol number NUM (which must be that of a token). */
343 static
344 const ]b4_int_type_for([b4_toknum])[
345 yytoken_number_[] =
346 {
347 ]b4_toknum[
348 };
349 return static_cast<token_type> (yytoken_number_[type]);
350 }
351 ]])[]dnl
352 b4_symbol_constructor_define])
353
354
355 # b4_symbol_constructor_declare
356 # b4_symbol_constructor_define
357 # -----------------------------
358 # Declare/define symbol constructors for all the value types.
359 # Use at class-level. Redefined in variant.hh.
360 m4_define([b4_symbol_constructor_declare], [])
361 m4_define([b4_symbol_constructor_define], [])
362
363
364 # b4_yytranslate_define
365 # ---------------------
366 # Define yytranslate_. Sometimes used in the header file,
367 # sometimes in the cc file.
368 m4_define([b4_yytranslate_define],
369 [[ // Symbol number corresponding to token number t.
370 ]b4_parser_class_name[::token_number_type
371 ]b4_parser_class_name[::yytranslate_ (]b4_token_ctor_if([token_type],
372 [int])[ t)
373 {
374 static
375 const token_number_type
376 translate_table[] =
377 {
378 ]b4_translate[
379 };
380 const unsigned int user_token_number_max_ = ]b4_user_token_number_max[;
381 const token_number_type undef_token_ = ]b4_undef_token_number[;
382
383 if (static_cast<int>(t) <= yyeof_)
384 return yyeof_;
385 else if (static_cast<unsigned int> (t) <= user_token_number_max_)
386 return translate_table[t];
387 else
388 return undef_token_;
389 }
390 ]])
391
392
393 # b4_lhs_value([TYPE])
394 # --------------------
395 # Expansion of $<TYPE>$.
396 m4_define([b4_lhs_value],
397 [b4_symbol_value([yyval], [$1])])
398
399
400 # b4_rhs_value(RULE-LENGTH, NUM, [TYPE])
401 # --------------------------------------
402 # Expansion of $<TYPE>NUM, where the current rule has RULE-LENGTH
403 # symbols on RHS.
404 m4_define([b4_rhs_value],
405 [b4_symbol_value([yysemantic_stack_@{($1) - ($2)@}], [$3])])
406
407
408 # b4_lhs_location()
409 # -----------------
410 # Expansion of @$.
411 m4_define([b4_lhs_location],
412 [(yyloc)])
413
414
415 # b4_rhs_location(RULE-LENGTH, NUM)
416 # ---------------------------------
417 # Expansion of @NUM, where the current rule has RULE-LENGTH symbols
418 # on RHS.
419 m4_define([b4_rhs_location],
420 [(yylocation_stack_@{($1) - ($2)@})])
421
422
423 # b4_parse_param_decl
424 # -------------------
425 # Extra formal arguments of the constructor.
426 # Change the parameter names from "foo" into "foo_yyarg", so that
427 # there is no collision bw the user chosen attribute name, and the
428 # argument name in the constructor.
429 m4_define([b4_parse_param_decl],
430 [m4_ifset([b4_parse_param],
431 [m4_map_sep([b4_parse_param_decl_1], [, ], [b4_parse_param])])])
432
433 m4_define([b4_parse_param_decl_1],
434 [$1_yyarg])
435
436
437
438 # b4_parse_param_cons
439 # -------------------
440 # Extra initialisations of the constructor.
441 m4_define([b4_parse_param_cons],
442 [m4_ifset([b4_parse_param],
443 [
444 b4_cc_constructor_calls(b4_parse_param)])])
445 m4_define([b4_cc_constructor_calls],
446 [m4_map_sep([b4_cc_constructor_call], [,
447 ], [$@])])
448 m4_define([b4_cc_constructor_call],
449 [$2 ($2_yyarg)])
450
451 # b4_parse_param_vars
452 # -------------------
453 # Extra instance variables.
454 m4_define([b4_parse_param_vars],
455 [m4_ifset([b4_parse_param],
456 [
457 // User arguments.
458 b4_cc_var_decls(b4_parse_param)])])
459 m4_define([b4_cc_var_decls],
460 [m4_map_sep([b4_cc_var_decl], [
461 ], [$@])])
462 m4_define([b4_cc_var_decl],
463 [ $1;])
464
465
466 ## ---------##
467 ## Values. ##
468 ## ---------##
469
470 # b4_yylloc_default_define
471 # ------------------------
472 # Define YYLLOC_DEFAULT.
473 m4_define([b4_yylloc_default_define],
474 [[/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
475 If N is 0, then set CURRENT to the empty location which ends
476 the previous symbol: RHS[0] (always defined). */
477
478 # ifndef YYLLOC_DEFAULT
479 # define YYLLOC_DEFAULT(Current, Rhs, N) \
480 do \
481 if (N) \
482 { \
483 (Current).begin = YYRHSLOC (Rhs, 1).begin; \
484 (Current).end = YYRHSLOC (Rhs, N).end; \
485 } \
486 else \
487 { \
488 (Current).begin = (Current).end = YYRHSLOC (Rhs, 0).end; \
489 } \
490 while (/*CONSTCOND*/ false)
491 # endif
492 ]])