]> git.saurik.com Git - bison.git/blame - data/c++.m4
portability: use va_start and va_end in the same function.
[bison.git] / data / c++.m4
CommitLineData
08af01c2 1 -*- Autoconf -*-
cf147260
AD
2
3# C++ skeleton for Bison
4
b47b6ff7
AD
5# Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
6# Free Software Foundation, Inc.
cf147260 7
f16b0819 8# This program is free software: you can redistribute it and/or modify
cf147260 9# it under the terms of the GNU General Public License as published by
f16b0819 10# the Free Software Foundation, either version 3 of the License, or
cf147260 11# (at your option) any later version.
f16b0819 12#
cf147260
AD
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
f16b0819 17#
cf147260 18# You should have received a copy of the GNU General Public License
f16b0819 19# along with this program. If not, see <http://www.gnu.org/licenses/>.
cf147260 20
3953ed88
AD
21m4_include(b4_pkgdatadir/[c.m4])
22
cf147260
AD
23## ---------------- ##
24## Default values. ##
25## ---------------- ##
26
a4e25e1d
JD
27b4_percent_define_default([[parser_class_name]], [[parser]])
28b4_percent_define_default([[location_type]], [[location]])
29b4_percent_define_default([[filename_type]], [[std::string]])
171ad99d 30b4_percent_define_default([[api.namespace]], m4_defn([b4_prefix]))
67501061 31
c1d19e10 32b4_percent_define_default([[global_tokens_and_yystype]], [[false]])
a4e25e1d
JD
33b4_percent_define_default([[define_location_comparison]],
34 [m4_if(b4_percent_define_get([[filename_type]]),
c1d19e10 35 [std::string], [[true]], [[false]])])
cf147260
AD
36
37
67501061 38
793fbca5
JD
39## ----------- ##
40## Namespace. ##
41## ----------- ##
42
67501061
AD
43m4_define([b4_namespace_ref], [b4_percent_define_get([[api.namespace]])])
44
793fbca5
JD
45
46# Don't permit an empty b4_namespace_ref. Any `::parser::foo' appended to it
47# would compile as an absolute reference with `parser' in the global namespace.
48# b4_namespace_open would open an anonymous namespace and thus establish
49# internal linkage. This would compile. However, it's cryptic, and internal
50# linkage for the parser would be specified in all translation units that
51# include the header, which is always generated. If we ever need to permit
52# internal linkage somehow, surely we can find a cleaner approach.
53m4_if(m4_bregexp(b4_namespace_ref, [^[ ]*$]), [-1], [],
67501061 54[b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
793fbca5
JD
55 [[namespace reference is empty]])])
56
57# Instead of assuming the C++ compiler will do it, Bison should reject any
58# invalid b4_namepsace_ref that would be converted to a valid
59# b4_namespace_open. The problem is that Bison doesn't always output
60# b4_namespace_ref to uncommented code but should reserve the ability to do so
61# in future releases without risking breaking any existing user grammars.
62# Specifically, don't allow empty names as b4_namespace_open would just convert
63# those into anonymous namespaces, and that might tempt some users.
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 has consecutive "::"]])])
67m4_if(m4_bregexp(b4_namespace_ref, [::[ ]*$]), [-1], [],
67501061 68[b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
793fbca5
JD
69 [[namespace reference has a trailing "::"]])])
70
71m4_define([b4_namespace_open],
67501061 72[b4_user_code([b4_percent_define_get_syncline([[api.namespace]])
793fbca5
JD
73[namespace ]m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(b4_namespace_ref),
74 [^\(.\)[ ]*::], [\1])),
75 [::], [ { namespace ])[ {]])])
76
77m4_define([b4_namespace_close],
67501061 78[b4_user_code([b4_percent_define_get_syncline([[api.namespace]])
4977e0a7 79m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(b4_namespace_ref[ ]),
b987342b 80 [^\(.\)[ ]*\(::\)?\([^][:]\|:[^:]\)*],
793fbca5 81 [\1])),
b987342b 82 [::\([^][:]\|:[^:]\)*], [} ])[} // ]b4_namespace_ref])])
793fbca5
JD
83
84
cf147260
AD
85# b4_token_enums(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
86# -----------------------------------------------------
c095d689 87# Output the definition of the tokens as enums.
cf147260 88m4_define([b4_token_enums],
cf147260
AD
89[/* Tokens. */
90 enum yytokentype {
91m4_map_sep([ b4_token_enum], [,
92],
93 [$@])
94 };
c095d689 95])
cf147260
AD
96
97
98## ----------------- ##
99## Semantic Values. ##
100## ----------------- ##
101
b9e4eb5b
AD
102# b4_semantic_type_declare
103# ------------------------
104# Declare semantic_type.
105m4_define([b4_semantic_type_declare],
106[ /// Symbol semantic values.
107m4_ifdef([b4_stype],
108[ union semantic_type
109 {b4_user_stype
110 };],
111[m4_if(b4_tag_seen_flag, 0,
112[[ typedef int semantic_type;]],
113[[ typedef YYSTYPE semantic_type;]])])])
114
115
4f84717d
AD
116# b4_public_types_declare
117# -----------------------
118# Define the public types: token, semantic value, location, and so forth.
1d6b689b 119# Depending on %define token_lex, may be output in the header or source file.
4f84717d
AD
120m4_define([b4_public_types_declare],
121[[#ifndef YYSTYPE
122]b4_semantic_type_declare[
123#else
124 typedef YYSTYPE semantic_type;
125#endif]b4_locations_if([
126 /// Symbol locations.
127 typedef b4_percent_define_get([[location_type]]) location_type;])[
1d6b689b 128
ff601366
AD
129 /// Syntax errors thrown from user actions.
130 struct syntax_error : std::runtime_error
131 {
132 syntax_error (]b4_locations_if([const location_type& l, ])[const std::string& m);]b4_locations_if([
133 location_type location;])[
134 };
135
4f84717d
AD
136 /// Tokens.
137 struct token
138 {
139 ]b4_token_enums(b4_tokens)[
140 };
1d6b689b 141
4f84717d
AD
142 /// Token type.
143 typedef token::yytokentype token_type;
1d6b689b
AD
144
145 /// A complete symbol, with its type.
146 template <typename Exact>
147 struct symbol_base_type
148 {
149 /// Default constructor.
150 inline symbol_base_type ();
151
152 /// Constructor.]b4_locations_if([
153 inline symbol_base_type (const location_type& l)])[;
154 inline symbol_base_type (]b4_args(
155 [const semantic_type& v],
156 b4_locations_if([const location_type& l]))[);
157
158 /// Return this with its exact type.
159 const Exact& self () const;
160 Exact& self ();
161
162 /// Return the type of this symbol.
163 int type_get () const;
164
165 /// The semantic value.
166 semantic_type value;]b4_locations_if([
167
168 /// The location.
169 location_type location;])[
170 };
171
172 /// External form of a symbol: its type and attributes.
173 struct symbol_type : symbol_base_type<symbol_type>
174 {
175 /// The parent class.
176 typedef symbol_base_type<symbol_type> super_type;
177
178 /// Default constructor.
179 inline symbol_type ();
180
2b08bceb
AD
181 /// Constructor for tokens with semantic value.
182 inline symbol_type (]b4_args([token_type t],
1d6b689b
AD
183 [const semantic_type& v],
184 b4_locations_if([const location_type& l]))[);
185
2b08bceb
AD
186 /// Constructor for valueless tokens.
187 inline symbol_type (]b4_args([token_type t],
1d6b689b
AD
188 b4_locations_if([const location_type& l]))[);
189
190 /// The symbol type.
191 int type;
192
2b08bceb 193 /// The symbol type.
1d6b689b
AD
194 inline int type_get_ () const;
195
2b08bceb 196 /// The token.
1d6b689b
AD
197 inline token_type token () const;
198 };
0623bacc 199]b4_symbol_constructor_declare])
4f84717d
AD
200
201
1d6b689b
AD
202# b4_public_types_define
203# ----------------------
204# Provide the implementation needed by the public types.
205m4_define([b4_public_types_define],
ff601366
AD
206[[ inline
207 ]b4_parser_class_name[::syntax_error::syntax_error (]b4_locations_if([const location_type& l, ])[const std::string& m)
208 : std::runtime_error (m)]b4_locations_if([
209 , location (l)])[
210 {}
211
212 // symbol_base_type.
1d6b689b 213 template <typename Exact>
ff601366 214 inline
1d6b689b
AD
215 ]b4_parser_class_name[::symbol_base_type<Exact>::symbol_base_type ()
216 : value()]b4_locations_if([
217 , location()])[
218 {
219 }]b4_locations_if([[
220
221 template <typename Exact>
29660062 222 inline
1d6b689b
AD
223 ]b4_parser_class_name[::symbol_base_type<Exact>::symbol_base_type (const location_type& l)
224 : value()
225 , location(l)
226 {
227 }]])[
228
229 template <typename Exact>
29660062 230 inline
1d6b689b
AD
231 ]b4_parser_class_name[::symbol_base_type<Exact>::symbol_base_type (]b4_args(
232 [const semantic_type& v],
233 b4_locations_if([const location_type& l]))[)
234 : value(v)]b4_locations_if([
235 , location(l)])[
236 {
237 }
238
239 template <typename Exact>
29660062 240 inline
1d6b689b
AD
241 const Exact&
242 ]b4_parser_class_name[::symbol_base_type<Exact>::self () const
243 {
244 return static_cast<const Exact&>(*this);
245 }
246
247 template <typename Exact>
29660062 248 inline
1d6b689b
AD
249 Exact&
250 ]b4_parser_class_name[::symbol_base_type<Exact>::self ()
251 {
252 return static_cast<Exact&>(*this);
253 }
254
255 template <typename Exact>
29660062 256 inline
1d6b689b
AD
257 int
258 ]b4_parser_class_name[::symbol_base_type<Exact>::type_get () const
259 {
260 return self ().type_get_ ();
261 }
262
263 // symbol_type.
29660062 264 inline
1d6b689b
AD
265 ]b4_parser_class_name[::symbol_type::symbol_type ()
266 : super_type ()
267 , type ()
268 {
269 }
270
29660062 271 inline
1d6b689b 272 ]b4_parser_class_name[::symbol_type::symbol_type (]b4_args(
2b08bceb 273 [token_type t],
1d6b689b
AD
274 b4_locations_if([const location_type& l]))[)
275 : super_type (]b4_locations_if([l])[)
2b08bceb 276 , type (yytranslate_ (t))
1d6b689b
AD
277 {
278 }
279
29660062 280 inline
1d6b689b 281 ]b4_parser_class_name[::symbol_type::symbol_type (]b4_args(
2b08bceb 282 [token_type t],
1d6b689b
AD
283 [const semantic_type& v],
284 b4_locations_if([const location_type& l]))[)
285 : super_type (v]b4_locations_if([, l])[)
2b08bceb 286 , type (yytranslate_ (t))
1d6b689b
AD
287 {
288 }
289
29660062 290 inline
1d6b689b
AD
291 int
292 ]b4_parser_class_name[::symbol_type::type_get_ () const
293 {
294 return type;
295 }
296]b4_lex_symbol_if([[
29660062 297 inline
1d6b689b
AD
298 ]b4_parser_class_name[::token_type
299 ]b4_parser_class_name[::symbol_type::token () const
300 {
301 // YYTOKNUM[NUM] -- (External) token number corresponding to the
302 // (internal) symbol number NUM (which must be that of a token). */
303 static
304 const ]b4_int_type_for([b4_toknum])[
305 yytoken_number_[] =
306 {
307 ]b4_toknum[
308 };
309 return static_cast<token_type> (yytoken_number_[type]);
310 }
0623bacc
AD
311]])[]dnl
312b4_symbol_constructor_define])
313
314
315# b4_symbol_constructor_declare
316# b4_symbol_constructor_define
317# -----------------------------
318# Declare/define symbol constructors for all the value types.
319# Use at class-level. Redefined in variant.hh.
320m4_define([b4_symbol_constructor_declare], [])
321m4_define([b4_symbol_constructor_define], [])
322
1d6b689b 323
cbf25ce7
AD
324# b4_yytranslate_define
325# ---------------------
326# Define yytranslate_. Sometimes used in the header file,
327# sometimes in the cc file.
328m4_define([b4_yytranslate_define],
329[[ // Symbol number corresponding to token number t.
330 ]b4_parser_class_name[::token_number_type
331 ]b4_parser_class_name[::yytranslate_ (]b4_lex_symbol_if([token_type],
332 [int])[ t)
333 {
334 static
335 const token_number_type
336 translate_table[] =
337 {
338]b4_translate[
339 };
340 const unsigned int user_token_number_max_ = ]b4_user_token_number_max[;
341 const token_number_type undef_token_ = ]b4_undef_token_number[;
342
343 if (static_cast<int>(t) <= yyeof_)
344 return yyeof_;
345 else if (static_cast<unsigned int> (t) <= user_token_number_max_)
346 return translate_table[t];
347 else
348 return undef_token_;
349 }
350]])
351
1d6b689b 352
cf147260
AD
353# b4_lhs_value([TYPE])
354# --------------------
355# Expansion of $<TYPE>$.
356m4_define([b4_lhs_value],
1fa5d8bb 357[b4_symbol_value([yyval], [$1])])
cf147260
AD
358
359
360# b4_rhs_value(RULE-LENGTH, NUM, [TYPE])
361# --------------------------------------
362# Expansion of $<TYPE>NUM, where the current rule has RULE-LENGTH
363# symbols on RHS.
364m4_define([b4_rhs_value],
1fa5d8bb
AD
365[b4_symbol_value([yysemantic_stack_@{($1) - ($2)@}], [$3])])
366
cf147260
AD
367
368# b4_lhs_location()
369# -----------------
370# Expansion of @$.
371m4_define([b4_lhs_location],
372[(yyloc)])
373
374
375# b4_rhs_location(RULE-LENGTH, NUM)
376# ---------------------------------
377# Expansion of @NUM, where the current rule has RULE-LENGTH symbols
378# on RHS.
379m4_define([b4_rhs_location],
d1ff7a7c 380[(yylocation_stack_@{($1) - ($2)@})])
cf147260
AD
381
382
383# b4_parse_param_decl
384# -------------------
385# Extra formal arguments of the constructor.
386# Change the parameter names from "foo" into "foo_yyarg", so that
387# there is no collision bw the user chosen attribute name, and the
388# argument name in the constructor.
389m4_define([b4_parse_param_decl],
390[m4_ifset([b4_parse_param],
391 [m4_map_sep([b4_parse_param_decl_1], [, ], [b4_parse_param])])])
392
393m4_define([b4_parse_param_decl_1],
394[$1_yyarg])
395
396
397
398# b4_parse_param_cons
399# -------------------
400# Extra initialisations of the constructor.
401m4_define([b4_parse_param_cons],
402 [m4_ifset([b4_parse_param],
fa7b79c0 403 [
cf147260
AD
404 b4_cc_constructor_calls(b4_parse_param)])])
405m4_define([b4_cc_constructor_calls],
406 [m4_map_sep([b4_cc_constructor_call], [,
407 ], [$@])])
408m4_define([b4_cc_constructor_call],
409 [$2 ($2_yyarg)])
410
411# b4_parse_param_vars
412# -------------------
413# Extra instance variables.
414m4_define([b4_parse_param_vars],
415 [m4_ifset([b4_parse_param],
416 [
417 /* User arguments. */
418b4_cc_var_decls(b4_parse_param)])])
419m4_define([b4_cc_var_decls],
420 [m4_map_sep([b4_cc_var_decl], [
421], [$@])])
422m4_define([b4_cc_var_decl],
423 [ $1;])