]> git.saurik.com Git - bison.git/blame - data/c++.m4
api.value.type: implement proper support, check, and document
[bison.git] / data / c++.m4
CommitLineData
08af01c2 1 -*- Autoconf -*-
cf147260
AD
2
3# C++ skeleton for Bison
4
7d6bad19 5# Copyright (C) 2002-2013 Free Software Foundation, Inc.
cf147260 6
f16b0819 7# This program is free software: you can redistribute it and/or modify
cf147260 8# it under the terms of the GNU General Public License as published by
f16b0819 9# the Free Software Foundation, either version 3 of the License, or
cf147260 10# (at your option) any later version.
f16b0819 11#
cf147260
AD
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.
f16b0819 16#
cf147260 17# You should have received a copy of the GNU General Public License
f16b0819 18# along with this program. If not, see <http://www.gnu.org/licenses/>.
cf147260 19
3953ed88
AD
20m4_include(b4_pkgdatadir/[c.m4])
21
38de4e57
AD
22# b4_comment(TEXT, [PREFIX])
23# --------------------------
24# Put TEXT in comment. Prefix all the output lines with PREFIX.
25m4_define([b4_comment],
26[b4_comment_([$1], [$2// ], [$2// ])])
27
28
cf147260
AD
29## ---------------- ##
30## Default values. ##
31## ---------------- ##
32
a4e25e1d 33b4_percent_define_default([[parser_class_name]], [[parser]])
7789b6e3
AD
34
35# Don't do that so that we remember whether we're using a user
36# request, or the default value.
37#
db8ab2be 38# b4_percent_define_default([[api.location.type]], [[location]])
7789b6e3 39
a4e25e1d 40b4_percent_define_default([[filename_type]], [[std::string]])
171ad99d 41b4_percent_define_default([[api.namespace]], m4_defn([b4_prefix]))
67501061 42
c1d19e10 43b4_percent_define_default([[global_tokens_and_yystype]], [[false]])
a4e25e1d
JD
44b4_percent_define_default([[define_location_comparison]],
45 [m4_if(b4_percent_define_get([[filename_type]]),
c1d19e10 46 [std::string], [[true]], [[false]])])
cf147260
AD
47
48
67501061 49
793fbca5
JD
50## ----------- ##
51## Namespace. ##
52## ----------- ##
53
67501061
AD
54m4_define([b4_namespace_ref], [b4_percent_define_get([[api.namespace]])])
55
793fbca5 56
45eebca4
AD
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.
793fbca5
JD
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.
64m4_if(m4_bregexp(b4_namespace_ref, [^[ ]*$]), [-1], [],
67501061 65[b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
793fbca5
JD
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.
75m4_if(m4_bregexp(b4_namespace_ref, [::[ ]*::]), [-1], [],
67501061 76[b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
793fbca5
JD
77 [[namespace reference has consecutive "::"]])])
78m4_if(m4_bregexp(b4_namespace_ref, [::[ ]*$]), [-1], [],
67501061 79[b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
793fbca5
JD
80 [[namespace reference has a trailing "::"]])])
81
82m4_define([b4_namespace_open],
67501061 83[b4_user_code([b4_percent_define_get_syncline([[api.namespace]])
793fbca5
JD
84[namespace ]m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(b4_namespace_ref),
85 [^\(.\)[ ]*::], [\1])),
86 [::], [ { namespace ])[ {]])])
87
88m4_define([b4_namespace_close],
67501061 89[b4_user_code([b4_percent_define_get_syncline([[api.namespace]])
4977e0a7 90m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(b4_namespace_ref[ ]),
b987342b 91 [^\(.\)[ ]*\(::\)?\([^][:]\|:[^:]\)*],
793fbca5 92 [\1])),
b987342b 93 [::\([^][:]\|:[^:]\)*], [} ])[} // ]b4_namespace_ref])])
793fbca5
JD
94
95
e3990e3c
AD
96# b4_token_enums
97# --------------
c095d689 98# Output the definition of the tokens as enums.
cf147260 99m4_define([b4_token_enums],
e3990e3c
AD
100[[enum yytokentype
101 {
102 ]m4_join([,
103 ],
104 b4_symbol_map([b4_token_enum]))[
105 };]dnl
c095d689 106])
cf147260
AD
107
108
ef51bfa7
AD
109
110
cf147260
AD
111## ----------------- ##
112## Semantic Values. ##
113## ----------------- ##
114
7601a471
AD
115
116
117# b4_value_type_declare
118# ---------------------
b9e4eb5b 119# Declare semantic_type.
7601a471 120m4_define([b4_value_type_declare],
6574576c 121[b4_value_type_setup[]dnl
b9e4eb5b 122[ /// Symbol semantic values.
6574576c
AD
123]m4_bmatch(b4_percent_define_get([api.value.type]),
124[^%union\|union$],
125[[ union semantic_type
d2e3c807 126 {
6574576c
AD
127]b4_user_union_members[
128 };]],
129[^$], [],
130[[ typedef ]b4_percent_define_get([api.value.type])[ semantic_type;]])])
b9e4eb5b
AD
131
132
4f84717d
AD
133# b4_public_types_declare
134# -----------------------
135# Define the public types: token, semantic value, location, and so forth.
1d6b689b 136# Depending on %define token_lex, may be output in the header or source file.
4f84717d 137m4_define([b4_public_types_declare],
5458913a 138[[#ifndef ]b4_api_PREFIX[STYPE
7601a471 139]b4_value_type_declare[
4f84717d 140#else
5458913a 141 typedef ]b4_api_PREFIX[STYPE semantic_type;
4f84717d
AD
142#endif]b4_locations_if([
143 /// Symbol locations.
f6b561d9 144 typedef b4_percent_define_get([[api.location.type]],
7789b6e3 145 [[location]]) location_type;])[
1d6b689b 146
ff601366
AD
147 /// Syntax errors thrown from user actions.
148 struct syntax_error : std::runtime_error
149 {
150 syntax_error (]b4_locations_if([const location_type& l, ])[const std::string& m);]b4_locations_if([
151 location_type location;])[
152 };
153
4f84717d
AD
154 /// Tokens.
155 struct token
156 {
e3990e3c 157 ]b4_token_enums[
4f84717d 158 };
1d6b689b 159
26f95f5f 160 /// (External) token type, as returned by yylex.
4f84717d 161 typedef token::yytokentype token_type;
1d6b689b 162
26f95f5f
AD
163 /// Internal symbol number.
164 typedef int symbol_number_type;
165
166 /// Internal symbol number for tokens (subsumed by symbol_number_type).
167 typedef ]b4_int_type_for([b4_translate])[ token_number_type;
168
1dbaf37f
TR
169 /// A complete symbol.
170 ///
171 /// Expects its Base type to provide access to the symbol type
172 /// via type_get().
173 ///
174 /// Provide access to semantic value]b4_locations_if([ and location])[.
175 template <typename Base>
176 struct basic_symbol : Base
1d6b689b 177 {
97ae878e
AD
178 /// Alias to Base.
179 typedef Base super_type;
180
1d6b689b 181 /// Default constructor.
b20e797a 182 basic_symbol ();
1d6b689b 183
1dbaf37f 184 /// Copy constructor.
b20e797a 185 basic_symbol (const basic_symbol& other);
ee9cf8c4
TR
186]b4_variant_if([[
187 /// Constructor for valueless symbols, and symbols from each type.
188]b4_type_foreach([b4_basic_symbol_constructor_declare])], [[
1dbaf37f 189 /// Constructor for valueless symbols.
7fc7df7a 190 basic_symbol (typename Base::kind_type t]b4_locations_if([,
ee9cf8c4 191 const location_type& l])[);]])[
1d6b689b 192
1dbaf37f 193 /// Constructor for symbols with semantic value.
7fc7df7a 194 basic_symbol (typename Base::kind_type t,
b20e797a
TR
195 const semantic_type& v]b4_locations_if([,
196 const location_type& l])[);
1d6b689b 197
97ae878e 198 ~basic_symbol ();
04816a6f 199
5f87211c 200 /// Destructive move, \a s is emptied into this.
b20e797a 201 void move (basic_symbol& s);
1d6b689b
AD
202
203 /// The semantic value.
204 semantic_type value;]b4_locations_if([
205
206 /// The location.
207 location_type location;])[
7d1aa2d6
AD
208
209 private:
210 /// Assignment operator.
211 basic_symbol& operator= (const basic_symbol& other);
1d6b689b
AD
212 };
213
1dbaf37f
TR
214 /// Type access provider for token (enum) based symbols.
215 struct by_type
1d6b689b 216 {
1d6b689b 217 /// Default constructor.
b20e797a 218 by_type ();
1d6b689b 219
1dbaf37f 220 /// Copy constructor.
b20e797a 221 by_type (const by_type& other);
1d6b689b 222
26f95f5f
AD
223 /// The symbol type as needed by the constructor.
224 typedef token_type kind_type;
1d6b689b 225
26f95f5f
AD
226 /// Constructor from (external) token numbers.
227 by_type (kind_type t);
97ae878e 228
26f95f5f
AD
229 /// Steal the symbol type from \a that.
230 void move (by_type& that);
1d6b689b 231
26f95f5f 232 /// The (internal) type number (corresponding to \a type).
97ae878e 233 /// -1 when this symbol is empty.
26f95f5f 234 symbol_number_type type_get () const;
1d6b689b 235
2b08bceb 236 /// The token.
b20e797a 237 token_type token () const;
1dbaf37f 238
675d9fe4
AD
239 enum { empty = 0 };
240
26f95f5f 241 /// The symbol type.
26f95f5f
AD
242 /// -1 when this symbol is empty.
243 token_number_type type;
1d6b689b 244 };
1dbaf37f
TR
245
246 /// "External" symbols: returned by the scanner.
247 typedef basic_symbol<by_type> symbol_type;
248
0623bacc 249]b4_symbol_constructor_declare])
4f84717d
AD
250
251
1d6b689b
AD
252# b4_public_types_define
253# ----------------------
254# Provide the implementation needed by the public types.
255m4_define([b4_public_types_define],
403febca 256[[ inline
ff601366
AD
257 ]b4_parser_class_name[::syntax_error::syntax_error (]b4_locations_if([const location_type& l, ])[const std::string& m)
258 : std::runtime_error (m)]b4_locations_if([
259 , location (l)])[
260 {}
261
1dbaf37f
TR
262 // basic_symbol.
263 template <typename Base>
b20e797a 264 inline
1dbaf37f
TR
265 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol ()
266 : value ()
267 {}
04816a6f 268
1dbaf37f 269 template <typename Base>
b20e797a 270 inline
1dbaf37f
TR
271 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (const basic_symbol& other)
272 : Base (other)
273 , value ()]b4_locations_if([
274 , location (other.location)])[
1d6b689b 275 {
1dbaf37f
TR
276 ]b4_variant_if([b4_symbol_variant([other.type_get ()], [value], [copy],
277 [other.value])],
278 [value = other.value;])[
1d6b689b
AD
279 }
280
1d6b689b 281
1dbaf37f 282 template <typename Base>
b20e797a 283 inline
1dbaf37f 284 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (]b4_join(
7fc7df7a 285 [typename Base::kind_type t],
1dbaf37f
TR
286 [const semantic_type& v],
287 b4_locations_if([const location_type& l]))[)
288 : Base (t)
97ae878e 289 , value (]b4_variant_if([], [v])[)]b4_locations_if([
1dbaf37f 290 , location (l)])[
97ae878e 291 {]b4_variant_if([[
733fb7c5 292 (void) v;
97ae878e 293 ]b4_symbol_variant([this->type_get ()], [value], [copy], [v])])[}
1d6b689b 294
ee9cf8c4
TR
295]b4_variant_if([[
296 // Implementation of basic_symbol constructor for each type.
297]b4_type_foreach([b4_basic_symbol_constructor_define])], [[
298 /// Constructor for valueless symbols.
1dbaf37f 299 template <typename Base>
b20e797a 300 inline
1dbaf37f 301 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (]b4_join(
7fc7df7a 302 [typename Base::kind_type t],
1dbaf37f 303 b4_locations_if([const location_type& l]))[)
97ae878e
AD
304 : Base (t)
305 , value ()]b4_locations_if([
1dbaf37f 306 , location (l)])[
ee9cf8c4 307 {}]])[
1d6b689b 308
97ae878e
AD
309 template <typename Base>
310 inline
311 ]b4_parser_class_name[::basic_symbol<Base>::~basic_symbol ()
312 {]b4_variant_if([[
313 // User destructor.
26f95f5f 314 symbol_number_type yytype = this->type_get ();
97ae878e
AD
315 switch (yytype)
316 {
317]b4_symbol_foreach([b4_symbol_destructor])dnl
318[ default:
319 break;
320 }
321
322 // Type destructor.
323 ]b4_symbol_variant([[yytype]], [[value]], [[template destroy]])])[
324 }
325
1dbaf37f 326 template <typename Base>
b20e797a 327 inline
1dbaf37f
TR
328 void
329 ]b4_parser_class_name[::basic_symbol<Base>::move (basic_symbol& s)
1d6b689b 330 {
97ae878e
AD
331 super_type::move(s);
332 ]b4_variant_if([b4_symbol_variant([this->type_get ()], [value], [move],
1dbaf37f 333 [s.value])],
97ae878e
AD
334 [value = s.value;])[]b4_locations_if([
335 location = s.location;])[
1d6b689b
AD
336 }
337
1dbaf37f 338 // by_type.
b20e797a 339 inline
1dbaf37f 340 ]b4_parser_class_name[::by_type::by_type ()
675d9fe4 341 : type (empty)
1dbaf37f 342 {}
1d6b689b 343
b20e797a 344 inline
1dbaf37f
TR
345 ]b4_parser_class_name[::by_type::by_type (const by_type& other)
346 : type (other.type)
347 {}
1d6b689b 348
b20e797a 349 inline
1dbaf37f
TR
350 ]b4_parser_class_name[::by_type::by_type (token_type t)
351 : type (yytranslate_ (t))
352 {}
7be08dfb 353
97ae878e
AD
354 inline
355 void
356 ]b4_parser_class_name[::by_type::move (by_type& that)
357 {
358 type = that.type;
675d9fe4 359 that.type = empty;
97ae878e
AD
360 }
361
b20e797a 362 inline
1d6b689b 363 int
1dbaf37f 364 ]b4_parser_class_name[::by_type::type_get () const
1d6b689b
AD
365 {
366 return type;
367 }
e36ec1f4 368]b4_token_ctor_if([[
b20e797a 369 inline
1d6b689b 370 ]b4_parser_class_name[::token_type
1dbaf37f 371 ]b4_parser_class_name[::by_type::token () const
1d6b689b
AD
372 {
373 // YYTOKNUM[NUM] -- (External) token number corresponding to the
374 // (internal) symbol number NUM (which must be that of a token). */
375 static
376 const ]b4_int_type_for([b4_toknum])[
377 yytoken_number_[] =
378 {
379 ]b4_toknum[
380 };
381 return static_cast<token_type> (yytoken_number_[type]);
382 }
0623bacc
AD
383]])[]dnl
384b4_symbol_constructor_define])
385
386
387# b4_symbol_constructor_declare
388# b4_symbol_constructor_define
389# -----------------------------
390# Declare/define symbol constructors for all the value types.
391# Use at class-level. Redefined in variant.hh.
392m4_define([b4_symbol_constructor_declare], [])
393m4_define([b4_symbol_constructor_define], [])
394
1d6b689b 395
cbf25ce7
AD
396# b4_yytranslate_define
397# ---------------------
398# Define yytranslate_. Sometimes used in the header file,
399# sometimes in the cc file.
400m4_define([b4_yytranslate_define],
401[[ // Symbol number corresponding to token number t.
b20e797a 402 inline
cbf25ce7 403 ]b4_parser_class_name[::token_number_type
e36ec1f4 404 ]b4_parser_class_name[::yytranslate_ (]b4_token_ctor_if([token_type],
cbf25ce7
AD
405 [int])[ t)
406 {
407 static
408 const token_number_type
409 translate_table[] =
410 {
411]b4_translate[
412 };
413 const unsigned int user_token_number_max_ = ]b4_user_token_number_max[;
414 const token_number_type undef_token_ = ]b4_undef_token_number[;
415
416 if (static_cast<int>(t) <= yyeof_)
417 return yyeof_;
418 else if (static_cast<unsigned int> (t) <= user_token_number_max_)
419 return translate_table[t];
420 else
421 return undef_token_;
422 }
423]])
424
1d6b689b 425
cf147260
AD
426# b4_lhs_value([TYPE])
427# --------------------
428# Expansion of $<TYPE>$.
429m4_define([b4_lhs_value],
1fa5d8bb 430[b4_symbol_value([yyval], [$1])])
cf147260
AD
431
432
433# b4_rhs_value(RULE-LENGTH, NUM, [TYPE])
434# --------------------------------------
435# Expansion of $<TYPE>NUM, where the current rule has RULE-LENGTH
436# symbols on RHS.
437m4_define([b4_rhs_value],
1fa5d8bb
AD
438[b4_symbol_value([yysemantic_stack_@{($1) - ($2)@}], [$3])])
439
cf147260
AD
440
441# b4_lhs_location()
442# -----------------
443# Expansion of @$.
444m4_define([b4_lhs_location],
445[(yyloc)])
446
447
448# b4_rhs_location(RULE-LENGTH, NUM)
449# ---------------------------------
450# Expansion of @NUM, where the current rule has RULE-LENGTH symbols
451# on RHS.
452m4_define([b4_rhs_location],
d1ff7a7c 453[(yylocation_stack_@{($1) - ($2)@})])
cf147260
AD
454
455
456# b4_parse_param_decl
457# -------------------
458# Extra formal arguments of the constructor.
459# Change the parameter names from "foo" into "foo_yyarg", so that
460# there is no collision bw the user chosen attribute name, and the
461# argument name in the constructor.
462m4_define([b4_parse_param_decl],
463[m4_ifset([b4_parse_param],
464 [m4_map_sep([b4_parse_param_decl_1], [, ], [b4_parse_param])])])
465
466m4_define([b4_parse_param_decl_1],
467[$1_yyarg])
468
469
470
471# b4_parse_param_cons
472# -------------------
473# Extra initialisations of the constructor.
474m4_define([b4_parse_param_cons],
475 [m4_ifset([b4_parse_param],
e9690142 476 [
cf147260
AD
477 b4_cc_constructor_calls(b4_parse_param)])])
478m4_define([b4_cc_constructor_calls],
e9690142 479 [m4_map_sep([b4_cc_constructor_call], [,
cf147260
AD
480 ], [$@])])
481m4_define([b4_cc_constructor_call],
e9690142 482 [$2 ($2_yyarg)])
cf147260
AD
483
484# b4_parse_param_vars
485# -------------------
486# Extra instance variables.
487m4_define([b4_parse_param_vars],
488 [m4_ifset([b4_parse_param],
e9690142 489 [
ac826bc4 490 // User arguments.
cf147260
AD
491b4_cc_var_decls(b4_parse_param)])])
492m4_define([b4_cc_var_decls],
e9690142 493 [m4_map_sep([b4_cc_var_decl], [
cf147260
AD
494], [$@])])
495m4_define([b4_cc_var_decl],
e9690142 496 [ $1;])
426903aa
AD
497
498
499## ---------##
500## Values. ##
501## ---------##
502
503# b4_yylloc_default_define
504# ------------------------
505# Define YYLLOC_DEFAULT.
506m4_define([b4_yylloc_default_define],
507[[/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
508 If N is 0, then set CURRENT to the empty location which ends
509 the previous symbol: RHS[0] (always defined). */
510
511# ifndef YYLLOC_DEFAULT
512# define YYLLOC_DEFAULT(Current, Rhs, N) \
513 do \
514 if (N) \
515 { \
516 (Current).begin = YYRHSLOC (Rhs, 1).begin; \
517 (Current).end = YYRHSLOC (Rhs, N).end; \
518 } \
519 else \
520 { \
521 (Current).begin = (Current).end = YYRHSLOC (Rhs, 0).end; \
522 } \
523 while (/*CONSTCOND*/ false)
524# endif
525]])
1dce289a
AD
526
527## -------- ##
528## Checks. ##
529## -------- ##
530
531b4_token_ctor_if([b4_variant_if([],
532 [b4_fatal_at(b4_percent_define_get_loc(api.token.constructor),
533 [cannot use '%s' without '%s'],
534 [%define api.token.constructor],
535 [%define api.value.type variant]))])])