]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/error.c
This patch rewrites the old VEC macro-based interface into a new one
[thirdparty/gcc.git] / gcc / cp / error.c
CommitLineData
471086d6 1/* Call-backs for C++ error reporting.
2 This code is non-reentrant.
ce084dfc 3 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2003,
7d96d151 4 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
8de5c43e 5 Free Software Foundation, Inc.
6f0d25a6 6 This file is part of GCC.
471086d6 7
6f0d25a6 8GCC is free software; you can redistribute it and/or modify
471086d6 9it under the terms of the GNU General Public License as published by
aa139c3f 10the Free Software Foundation; either version 3, or (at your option)
471086d6 11any later version.
12
6f0d25a6 13GCC is distributed in the hope that it will be useful,
471086d6 14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
aa139c3f 19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
471086d6 21
22#include "config.h"
b3ef7553 23#include "system.h"
805e22b2 24#include "coretypes.h"
25#include "tm.h"
471086d6 26#include "tree.h"
27#include "cp-tree.h"
5124a2c8 28#include "flags.h"
b97edf7e 29#include "diagnostic.h"
ce084dfc 30#include "tree-diagnostic.h"
6c7ff025 31#include "langhooks-def.h"
1e00012f 32#include "intl.h"
0de2b732 33#include "cxx-pretty-print.h"
ce084dfc 34#include "tree-pretty-print.h"
40c700ab 35#include "pointer-set.h"
6c536c4f 36#include "c-family/c-objc.h"
471086d6 37
573c13ef 38#define pp_separate_with_comma(PP) pp_cxx_separate_with (PP, ',')
180b6c86 39#define pp_separate_with_semicolon(PP) pp_cxx_separate_with (PP, ';')
326f388b 40
da901964 41/* The global buffer where we dump everything. It is there only for
42 transitional purpose. It is expected, in the near future, to be
43 completely removed. */
0de2b732 44static cxx_pretty_printer scratch_pretty_printer;
aa6db498 45#define cxx_pp (&scratch_pretty_printer)
cec1f6a6 46
a608187f 47/* Translate if being used for diagnostics, but not for dump files or
48 __PRETTY_FUNCTION. */
49#define M_(msgid) (pp_translate_identifiers (cxx_pp) ? _(msgid) : (msgid))
50
bf16fecd 51# define NEXT_CODE(T) (TREE_CODE (TREE_TYPE (T)))
471086d6 52
f01d4233 53static const char *args_to_string (tree, int);
240171a9 54static const char *assop_to_string (enum tree_code);
55static const char *code_to_string (enum tree_code);
f01d4233 56static const char *cv_to_string (tree, int);
57static const char *decl_to_string (tree, int);
240171a9 58static const char *expr_to_string (tree);
f01d4233 59static const char *fndecl_to_string (tree, int);
240171a9 60static const char *op_to_string (enum tree_code);
61static const char *parm_to_string (int);
f01d4233 62static const char *type_to_string (tree, int);
63
370478b1 64static void dump_alias_template_specialization (tree, int);
f01d4233 65static void dump_type (tree, int);
66static void dump_typename (tree, int);
67static void dump_simple_decl (tree, tree, int);
68static void dump_decl (tree, int);
69static void dump_template_decl (tree, int);
70static void dump_function_decl (tree, int);
71static void dump_expr (tree, int);
72static void dump_unary_op (const char *, tree, int);
73static void dump_binary_op (const char *, tree, int);
74static void dump_aggr_type (tree, int);
69cb846f 75static void dump_type_prefix (tree, int);
f01d4233 76static void dump_type_suffix (tree, int);
77static void dump_function_name (tree, int);
c2f47e15 78static void dump_call_expr_args (tree, int, bool);
79static void dump_aggr_init_expr_args (tree, int, bool);
f01d4233 80static void dump_expr_list (tree, int);
81static void dump_global_iord (tree);
f01d4233 82static void dump_parameters (tree, int);
83static void dump_exception_spec (tree, int);
f01d4233 84static void dump_template_argument (tree, int);
94c17f03 85static void dump_template_argument_list (tree, int);
f01d4233 86static void dump_template_parameter (tree, int);
f1f41a6c 87static void dump_template_bindings (tree, tree, vec<tree, va_gc> *);
f01d4233 88static void dump_scope (tree, int);
89static void dump_template_parms (tree, int, int);
94c17f03 90static int get_non_default_template_args_count (tree, int);
f01d4233 91static const char *function_category (tree);
0a32318d 92static void maybe_print_constexpr_context (diagnostic_context *);
f01d4233 93static void maybe_print_instantiation_context (diagnostic_context *);
94static void print_instantiation_full_context (diagnostic_context *);
95static void print_instantiation_partial_context (diagnostic_context *,
c5dd8e06 96 struct tinst_level *,
97 location_t);
f01d4233 98static void cp_diagnostic_starter (diagnostic_context *, diagnostic_info *);
99static void cp_diagnostic_finalizer (diagnostic_context *, diagnostic_info *);
100static void cp_print_error_function (diagnostic_context *, diagnostic_info *);
101
c907c5b1 102static bool cp_printer (pretty_printer *, text_info *, const char *,
103 int, bool, bool, bool);
471086d6 104
105void
f01d4233 106init_error (void)
471086d6 107{
ce601f29 108 diagnostic_starter (global_dc) = cp_diagnostic_starter;
109 diagnostic_finalizer (global_dc) = cp_diagnostic_finalizer;
5124a2c8 110 diagnostic_format_decoder (global_dc) = cp_printer;
111
0de2b732 112 pp_construct (pp_base (cxx_pp), NULL, 0);
113 pp_cxx_pretty_printer_init (cxx_pp);
471086d6 114}
115
d04f0501 116/* Dump a scope, if deemed necessary. */
4b5bcd15 117
d04f0501 118static void
f01d4233 119dump_scope (tree scope, int flags)
4b5bcd15 120{
6d00b16b 121 int f = flags & (TFF_SCOPE | TFF_CHASE_TYPEDEF);
682959c4 122
d04f0501 123 if (scope == NULL_TREE)
124 return;
50cd3f45 125
d04f0501 126 if (TREE_CODE (scope) == NAMESPACE_DECL)
127 {
128 if (scope != global_namespace)
653e5405 129 {
130 dump_decl (scope, f);
131 pp_cxx_colon_colon (cxx_pp);
132 }
d04f0501 133 }
134 else if (AGGREGATE_TYPE_P (scope))
135 {
682959c4 136 dump_type (scope, f);
573c13ef 137 pp_cxx_colon_colon (cxx_pp);
d04f0501 138 }
682959c4 139 else if ((flags & TFF_SCOPE) && TREE_CODE (scope) == FUNCTION_DECL)
d04f0501 140 {
682959c4 141 dump_function_decl (scope, f);
573c13ef 142 pp_cxx_colon_colon (cxx_pp);
d04f0501 143 }
4b5bcd15 144}
145
a2a627ce 146/* Dump the template ARGument under control of FLAGS. */
85918456 147
148static void
f01d4233 149dump_template_argument (tree arg, int flags)
85918456 150{
d95d815d 151 if (ARGUMENT_PACK_P (arg))
9c82e807 152 dump_template_argument_list (ARGUMENT_PACK_ARGS (arg),
153 /* No default args in argument packs. */
154 flags|TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS);
d95d815d 155 else if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
682959c4 156 dump_type (arg, flags & ~TFF_CLASS_KEY_OR_ENUM);
85918456 157 else
afc0e339 158 {
159 if (TREE_CODE (arg) == TREE_LIST)
160 arg = TREE_VALUE (arg);
161
162 dump_expr (arg, (flags | TFF_EXPR_IN_PARENS) & ~TFF_CLASS_KEY_OR_ENUM);
163 }
85918456 164}
165
2783c778 166/* Count the number of template arguments ARGS whose value does not
167 match the (optional) default template parameter in PARAMS */
168
169static int
94c17f03 170get_non_default_template_args_count (tree args, int flags)
2783c778 171{
94c17f03 172 int n = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_ARGS (args));
2783c778 173
94c17f03 174 if (/* We use this flag when generating debug information. We don't
9845d120 175 want to expand templates at this point, for this may generate
176 new decls, which gets decl counts out of sync, which may in
177 turn cause codegen differences between compilations with and
178 without -g. */
94c17f03 179 (flags & TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS) != 0
9845d120 180 || !flag_pretty_templates)
2783c778 181 return n;
182
94c17f03 183 return GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (INNERMOST_TEMPLATE_ARGS (args));
2783c778 184}
185
a2a627ce 186/* Dump a template-argument-list ARGS (always a TREE_VEC) under control
187 of FLAGS. */
188
189static void
94c17f03 190dump_template_argument_list (tree args, int flags)
a2a627ce 191{
94c17f03 192 int n = get_non_default_template_args_count (args, flags);
a2a627ce 193 int need_comma = 0;
194 int i;
195
2783c778 196 for (i = 0; i < n; ++i)
a2a627ce 197 {
d95d815d 198 tree arg = TREE_VEC_ELT (args, i);
199
200 /* Only print a comma if we know there is an argument coming. In
201 the case of an empty template argument pack, no actual
202 argument will be printed. */
203 if (need_comma
204 && (!ARGUMENT_PACK_P (arg)
205 || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) > 0))
653e5405 206 pp_separate_with_comma (cxx_pp);
d95d815d 207
208 dump_template_argument (arg, flags);
a2a627ce 209 need_comma = 1;
210 }
211}
212
213/* Dump a template parameter PARM (a TREE_LIST) under control of FLAGS. */
214
215static void
f01d4233 216dump_template_parameter (tree parm, int flags)
a2a627ce 217{
508c7aa7 218 tree p;
219 tree a;
220
221 if (parm == error_mark_node)
222 return;
223
224 p = TREE_VALUE (parm);
225 a = TREE_PURPOSE (parm);
a2a627ce 226
227 if (TREE_CODE (p) == TYPE_DECL)
228 {
682959c4 229 if (flags & TFF_DECL_SPECIFIERS)
653e5405 230 {
1e00012f 231 pp_cxx_ws_string (cxx_pp, "class");
d95d815d 232 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (p)))
1e00012f 233 pp_cxx_ws_string (cxx_pp, "...");
653e5405 234 if (DECL_NAME (p))
235 pp_cxx_tree_identifier (cxx_pp, DECL_NAME (p));
236 }
a2a627ce 237 else if (DECL_NAME (p))
653e5405 238 pp_cxx_tree_identifier (cxx_pp, DECL_NAME (p));
a2a627ce 239 else
653e5405 240 pp_cxx_canonical_template_parameter (cxx_pp, TREE_TYPE (p));
a2a627ce 241 }
242 else
682959c4 243 dump_decl (p, flags | TFF_DECL_SPECIFIERS);
a2a627ce 244
682959c4 245 if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && a != NULL_TREE)
a2a627ce 246 {
573c13ef 247 pp_cxx_whitespace (cxx_pp);
248 pp_equal (cxx_pp);
249 pp_cxx_whitespace (cxx_pp);
551b77a5 250 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
653e5405 251 dump_type (a, flags & ~TFF_CHASE_TYPEDEF);
a2a627ce 252 else
653e5405 253 dump_expr (a, flags | TFF_EXPR_IN_PARENS);
a2a627ce 254 }
255}
256
257/* Dump, under control of FLAGS, a template-parameter-list binding.
258 PARMS is a TREE_LIST of TREE_VEC of TREE_LIST and ARGS is a
259 TREE_VEC. */
260
261static void
f1f41a6c 262dump_template_bindings (tree parms, tree args, vec<tree, va_gc> *typenames)
a2a627ce 263{
180b6c86 264 bool need_semicolon = false;
40c700ab 265 int i;
266 tree t;
a2a627ce 267
268 while (parms)
269 {
270 tree p = TREE_VALUE (parms);
a6604172 271 int lvl = TMPL_PARMS_DEPTH (parms);
272 int arg_idx = 0;
a2a627ce 273 int i;
2783c778 274 tree lvl_args = NULL_TREE;
275
276 /* Don't crash if we had an invalid argument list. */
277 if (TMPL_ARGS_DEPTH (args) >= lvl)
278 lvl_args = TMPL_ARGS_LEVEL (args, lvl);
a2a627ce 279
280 for (i = 0; i < TREE_VEC_LENGTH (p); ++i)
a6604172 281 {
f9b9bf39 282 tree arg = NULL_TREE;
283
284 /* Don't crash if we had an invalid argument list. */
2783c778 285 if (lvl_args && NUM_TMPL_ARGS (lvl_args) > arg_idx)
286 arg = TREE_VEC_ELT (lvl_args, arg_idx);
a6604172 287
180b6c86 288 if (need_semicolon)
289 pp_separate_with_semicolon (cxx_pp);
682959c4 290 dump_template_parameter (TREE_VEC_ELT (p, i), TFF_PLAIN_IDENTIFIER);
653e5405 291 pp_cxx_whitespace (cxx_pp);
292 pp_equal (cxx_pp);
293 pp_cxx_whitespace (cxx_pp);
a6604172 294 if (arg)
89cc0cff 295 {
296 if (ARGUMENT_PACK_P (arg))
297 pp_cxx_left_brace (cxx_pp);
298 dump_template_argument (arg, TFF_PLAIN_IDENTIFIER);
299 if (ARGUMENT_PACK_P (arg))
300 pp_cxx_right_brace (cxx_pp);
301 }
a6604172 302 else
a608187f 303 pp_string (cxx_pp, M_("<missing>"));
50cd3f45 304
a6604172 305 ++arg_idx;
180b6c86 306 need_semicolon = true;
a6604172 307 }
a2a627ce 308
309 parms = TREE_CHAIN (parms);
310 }
40c700ab 311
673064cf 312 /* Don't bother with typenames for a partial instantiation. */
f1f41a6c 313 if (vec_safe_is_empty (typenames) || uses_template_parms (args))
673064cf 314 return;
315
f1f41a6c 316 FOR_EACH_VEC_SAFE_ELT (typenames, i, t)
40c700ab 317 {
180b6c86 318 if (need_semicolon)
319 pp_separate_with_semicolon (cxx_pp);
a409f927 320 dump_type (t, TFF_PLAIN_IDENTIFIER);
40c700ab 321 pp_cxx_whitespace (cxx_pp);
322 pp_equal (cxx_pp);
323 pp_cxx_whitespace (cxx_pp);
c9fc4edc 324 push_deferring_access_checks (dk_no_check);
a409f927 325 t = tsubst (t, args, tf_none, NULL_TREE);
c9fc4edc 326 pop_deferring_access_checks ();
a409f927 327 /* Strip typedefs. We can't just use TFF_CHASE_TYPEDEF because
328 pp_simple_type_specifier doesn't know about it. */
2fb61329 329 t = strip_typedefs (t);
a409f927 330 dump_type (t, TFF_PLAIN_IDENTIFIER);
40c700ab 331 }
a2a627ce 332}
333
370478b1 334/* Dump a human-readable equivalent of the alias template
335 specialization of T. */
336
337static void
338dump_alias_template_specialization (tree t, int flags)
339{
340 tree name;
341
342 gcc_assert (alias_template_specialization_p (t));
343
7961105f 344 if (!(flags & TFF_UNQUALIFIED_NAME))
345 dump_scope (CP_DECL_CONTEXT (TYPE_NAME (t)), flags);
370478b1 346 name = TYPE_IDENTIFIER (t);
347 pp_cxx_tree_identifier (cxx_pp, name);
348 dump_template_parms (TYPE_TEMPLATE_INFO (t),
349 /*primary=*/false,
350 flags & ~TFF_TEMPLATE_HEADER);
351}
352
d531c71f 353/* Dump a human-readable equivalent of TYPE. FLAGS controls the
354 format. */
96624a9e 355
471086d6 356static void
f01d4233 357dump_type (tree t, int flags)
471086d6 358{
359 if (t == NULL_TREE)
360 return;
50cd3f45 361
7199a4db 362 /* Don't print e.g. "struct mytypedef". */
363 if (TYPE_P (t) && typedef_variant_p (t))
364 {
365 tree decl = TYPE_NAME (t);
366 if ((flags & TFF_CHASE_TYPEDEF)
370478b1 367 || DECL_SELF_REFERENCE_P (decl)
368 || (!flag_pretty_templates
369 && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)))
7199a4db 370 t = strip_typedefs (t);
370478b1 371 else if (alias_template_specialization_p (t))
372 {
373 dump_alias_template_specialization (t, flags);
374 return;
375 }
7199a4db 376 else if (same_type_p (t, TREE_TYPE (decl)))
377 t = decl;
378 else
379 {
380 pp_cxx_cv_qualifier_seq (cxx_pp, t);
381 pp_cxx_tree_identifier (cxx_pp, TYPE_IDENTIFIER (t));
382 return;
383 }
384 }
385
471086d6 386 if (TYPE_PTRMEMFUNC_P (t))
387 goto offset_type;
388
389 switch (TREE_CODE (t))
390 {
4fdaf896 391 case LANG_TYPE:
f82f1250 392 if (t == init_list_type_node)
a608187f 393 pp_string (cxx_pp, M_("<brace-enclosed initializer list>"));
4fdaf896 394 else if (t == unknown_type_node)
a608187f 395 pp_string (cxx_pp, M_("<unresolved overloaded function type>"));
4fdaf896 396 else
c7ca48ea 397 {
398 pp_cxx_cv_qualifier_seq (cxx_pp, t);
399 pp_cxx_tree_identifier (cxx_pp, TYPE_IDENTIFIER (t));
400 }
471086d6 401 break;
402
1717856a 403 case TREE_LIST:
404 /* A list of function parms. */
d04f0501 405 dump_parameters (t, flags);
1717856a 406 break;
407
471086d6 408 case IDENTIFIER_NODE:
573c13ef 409 pp_cxx_tree_identifier (cxx_pp, t);
471086d6 410 break;
411
f7cfa235 412 case TREE_BINFO:
d04f0501 413 dump_type (BINFO_TYPE (t), flags);
471086d6 414 break;
415
416 case RECORD_TYPE:
417 case UNION_TYPE:
418 case ENUMERAL_TYPE:
d04f0501 419 dump_aggr_type (t, flags);
471086d6 420 break;
421
422 case TYPE_DECL:
682959c4 423 if (flags & TFF_CHASE_TYPEDEF)
653e5405 424 {
425 dump_type (DECL_ORIGINAL_TYPE (t)
426 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t), flags);
427 break;
428 }
331bc0ad 429 /* Else fall through. */
50cd3f45 430
b1ce805f 431 case TEMPLATE_DECL:
380974a6 432 case NAMESPACE_DECL:
682959c4 433 dump_decl (t, flags & ~TFF_DECL_SPECIFIERS);
471086d6 434 break;
50cd3f45 435
471086d6 436 case INTEGER_TYPE:
471086d6 437 case REAL_TYPE:
438 case VOID_TYPE:
bb0726a1 439 case BOOLEAN_TYPE:
d7b8e04e 440 case COMPLEX_TYPE:
441 case VECTOR_TYPE:
d62c98bc 442 case FIXED_POINT_TYPE:
d7b8e04e 443 pp_type_specifier_seq (cxx_pp, t);
471086d6 444 break;
445
cd5dd2d8 446 case TEMPLATE_TEMPLATE_PARM:
47cd6605 447 /* For parameters inside template signature. */
1e93ca27 448 if (TYPE_IDENTIFIER (t))
573c13ef 449 pp_cxx_tree_identifier (cxx_pp, TYPE_IDENTIFIER (t));
cd5dd2d8 450 else
653e5405 451 pp_cxx_canonical_template_parameter (cxx_pp, t);
1e93ca27 452 break;
453
454 case BOUND_TEMPLATE_TEMPLATE_PARM:
455 {
456 tree args = TYPE_TI_ARGS (t);
b1af6b0b 457 pp_cxx_cv_qualifier_seq (cxx_pp, t);
573c13ef 458 pp_cxx_tree_identifier (cxx_pp, TYPE_IDENTIFIER (t));
459 pp_cxx_begin_template_argument_list (cxx_pp);
94c17f03 460 dump_template_argument_list (args, flags);
573c13ef 461 pp_cxx_end_template_argument_list (cxx_pp);
1e93ca27 462 }
cd5dd2d8 463 break;
464
471086d6 465 case TEMPLATE_TYPE_PARM:
69cb846f 466 pp_cxx_cv_qualifier_seq (cxx_pp, t);
dbdf67c6 467 if (TYPE_IDENTIFIER (t))
573c13ef 468 pp_cxx_tree_identifier (cxx_pp, TYPE_IDENTIFIER (t));
dbdf67c6 469 else
653e5405 470 pp_cxx_canonical_template_parameter
471 (cxx_pp, TEMPLATE_TYPE_PARM_INDEX (t));
471086d6 472 break;
473
471086d6 474 /* This is not always necessary for pointers and such, but doing this
475 reduces code size. */
476 case ARRAY_TYPE:
477 case POINTER_TYPE:
478 case REFERENCE_TYPE:
479 case OFFSET_TYPE:
480 offset_type:
481 case FUNCTION_TYPE:
482 case METHOD_TYPE:
d04f0501 483 {
484 dump_type_prefix (t, flags);
485 dump_type_suffix (t, flags);
471086d6 486 break;
d04f0501 487 }
e857e9c7 488 case TYPENAME_TYPE:
a409f927 489 if (! (flags & TFF_CHASE_TYPEDEF)
490 && DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
491 {
492 dump_decl (TYPE_NAME (t), TFF_PLAIN_IDENTIFIER);
493 break;
494 }
69cb846f 495 pp_cxx_cv_qualifier_seq (cxx_pp, t);
1e00012f 496 pp_cxx_ws_string (cxx_pp,
9031d10b 497 TYPENAME_IS_ENUM_P (t) ? "enum"
e2ae55f2 498 : TYPENAME_IS_CLASS_P (t) ? "class"
499 : "typename");
141fe0b5 500 dump_typename (t, flags);
e857e9c7 501 break;
502
47d727d4 503 case UNBOUND_CLASS_TEMPLATE:
5bd9bf06 504 if (! (flags & TFF_UNQUALIFIED_NAME))
505 {
506 dump_type (TYPE_CONTEXT (t), flags);
507 pp_cxx_colon_colon (cxx_pp);
508 }
1e00012f 509 pp_cxx_ws_string (cxx_pp, "template");
47d727d4 510 dump_type (DECL_NAME (TYPE_NAME (t)), flags);
511 break;
512
63e22e88 513 case TYPEOF_TYPE:
1e00012f 514 pp_cxx_ws_string (cxx_pp, "__typeof__");
573c13ef 515 pp_cxx_whitespace (cxx_pp);
516 pp_cxx_left_paren (cxx_pp);
82bb2115 517 dump_expr (TYPEOF_TYPE_EXPR (t), flags & ~TFF_EXPR_IN_PARENS);
573c13ef 518 pp_cxx_right_paren (cxx_pp);
63e22e88 519 break;
520
8de5c43e 521 case UNDERLYING_TYPE:
522 pp_cxx_ws_string (cxx_pp, "__underlying_type");
523 pp_cxx_whitespace (cxx_pp);
524 pp_cxx_left_paren (cxx_pp);
525 dump_expr (UNDERLYING_TYPE_TYPE (t), flags & ~TFF_EXPR_IN_PARENS);
526 pp_cxx_right_paren (cxx_pp);
527 break;
528
d95d815d 529 case TYPE_PACK_EXPANSION:
530 dump_type (PACK_EXPANSION_PATTERN (t), flags);
1e00012f 531 pp_cxx_ws_string (cxx_pp, "...");
d95d815d 532 break;
533
a4f66688 534 case TYPE_ARGUMENT_PACK:
46147ffe 535 dump_template_argument (t, flags);
a4f66688 536 break;
537
34da8800 538 case DECLTYPE_TYPE:
1e00012f 539 pp_cxx_ws_string (cxx_pp, "decltype");
34da8800 540 pp_cxx_whitespace (cxx_pp);
541 pp_cxx_left_paren (cxx_pp);
542 dump_expr (DECLTYPE_TYPE_EXPR (t), flags & ~TFF_EXPR_IN_PARENS);
543 pp_cxx_right_paren (cxx_pp);
544 break;
545
fa67d8e8 546 case NULLPTR_TYPE:
547 pp_string (cxx_pp, "std::nullptr_t");
548 break;
549
471086d6 550 default:
aa6db498 551 pp_unsupported_tree (cxx_pp, t);
47cd6605 552 /* Fall through to error. */
d04f0501 553
554 case ERROR_MARK:
a608187f 555 pp_string (cxx_pp, M_("<type error>"));
d04f0501 556 break;
471086d6 557 }
558}
559
141fe0b5 560/* Dump a TYPENAME_TYPE. We need to notice when the context is itself
561 a TYPENAME_TYPE. */
562
563static void
f01d4233 564dump_typename (tree t, int flags)
141fe0b5 565{
566 tree ctx = TYPE_CONTEXT (t);
50cd3f45 567
141fe0b5 568 if (TREE_CODE (ctx) == TYPENAME_TYPE)
569 dump_typename (ctx, flags);
570 else
682959c4 571 dump_type (ctx, flags & ~TFF_CLASS_KEY_OR_ENUM);
573c13ef 572 pp_cxx_colon_colon (cxx_pp);
141fe0b5 573 dump_decl (TYPENAME_TYPE_FULLNAME (t), flags);
574}
575
d04f0501 576/* Return the name of the supplied aggregate, or enumeral type. */
577
a32f42ef 578const char *
579class_key_or_enum_as_string (tree t)
471086d6 580{
3f00a6c0 581 if (TREE_CODE (t) == ENUMERAL_TYPE)
582 {
583 if (SCOPED_ENUM_P (t))
584 return "enum class";
585 else
586 return "enum";
587 }
471086d6 588 else if (TREE_CODE (t) == UNION_TYPE)
ac9386a0 589 return "union";
471086d6 590 else if (TYPE_LANG_SPECIFIC (t) && CLASSTYPE_DECLARED_CLASS (t))
ac9386a0 591 return "class";
471086d6 592 else
ac9386a0 593 return "struct";
594}
595
d04f0501 596/* Print out a class declaration T under the control of FLAGS,
597 in the form `class foo'. */
96624a9e 598
ac9386a0 599static void
f01d4233 600dump_aggr_type (tree t, int flags)
ac9386a0 601{
602 tree name;
a32f42ef 603 const char *variety = class_key_or_enum_as_string (t);
d04f0501 604 int typdef = 0;
605 int tmplate = 0;
471086d6 606
69cb846f 607 pp_cxx_cv_qualifier_seq (cxx_pp, t);
471086d6 608
682959c4 609 if (flags & TFF_CLASS_KEY_OR_ENUM)
1e00012f 610 pp_cxx_ws_string (cxx_pp, variety);
50cd3f45 611
d04f0501 612 name = TYPE_NAME (t);
613
614 if (name)
471086d6 615 {
370478b1 616 typdef = (!DECL_ARTIFICIAL (name)
617 /* An alias specialization is not considered to be a
618 typedef. */
619 && !alias_template_specialization_p (t));
d0391daf 620
a70e3c37 621 if ((typdef
622 && ((flags & TFF_CHASE_TYPEDEF)
623 || (!flag_pretty_templates && DECL_LANG_SPECIFIC (name)
624 && DECL_TEMPLATE_INFO (name))))
625 || DECL_SELF_REFERENCE_P (name))
d0391daf 626 {
627 t = TYPE_MAIN_VARIANT (t);
628 name = TYPE_NAME (t);
629 typdef = 0;
630 }
631
d04f0501 632 tmplate = !typdef && TREE_CODE (t) != ENUMERAL_TYPE
653e5405 633 && TYPE_LANG_SPECIFIC (t) && CLASSTYPE_TEMPLATE_INFO (t)
a761e730 634 && (TREE_CODE (CLASSTYPE_TI_TEMPLATE (t)) != TEMPLATE_DECL
653e5405 635 || PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
facb12b2 636
637 if (! (flags & TFF_UNQUALIFIED_NAME))
638 dump_scope (CP_DECL_CONTEXT (name), flags | TFF_SCOPE);
639 flags &= ~TFF_UNQUALIFIED_NAME;
d04f0501 640 if (tmplate)
653e5405 641 {
642 /* Because the template names are mangled, we have to locate
643 the most general template, and use that name. */
370478b1 644 tree tpl = TYPE_TI_TEMPLATE (t);
653e5405 645
646 while (DECL_TEMPLATE_INFO (tpl))
647 tpl = DECL_TI_TEMPLATE (tpl);
648 name = tpl;
649 }
d04f0501 650 name = DECL_NAME (name);
471086d6 651 }
652
ce28ee2e 653 if (name == 0 || ANON_AGGRNAME_P (name))
471086d6 654 {
682959c4 655 if (flags & TFF_CLASS_KEY_OR_ENUM)
a608187f 656 pp_string (cxx_pp, M_("<anonymous>"));
da901964 657 else
a608187f 658 pp_printf (pp_base (cxx_pp), M_("<anonymous %s>"), variety);
471086d6 659 }
3910cc8d 660 else if (LAMBDA_TYPE_P (name))
a8b75081 661 {
662 /* A lambda's "type" is essentially its signature. */
663 pp_string (cxx_pp, M_("<lambda"));
664 if (lambda_function (t))
665 dump_parameters (FUNCTION_FIRST_USER_PARMTYPE (lambda_function (t)),
666 flags);
667 pp_character(cxx_pp, '>');
668 }
471086d6 669 else
573c13ef 670 pp_cxx_tree_identifier (cxx_pp, name);
d04f0501 671 if (tmplate)
672 dump_template_parms (TYPE_TEMPLATE_INFO (t),
653e5405 673 !CLASSTYPE_USE_TEMPLATE (t),
674 flags & ~TFF_TEMPLATE_HEADER);
471086d6 675}
676
677/* Dump into the obstack the initial part of the output for a given type.
678 This is necessary when dealing with things like functions returning
679 functions. Examples:
680
681 return type of `int (* fee ())()': pointer -> function -> int. Both
682 pointer (and reference and offset) and function (and member) types must
683 deal with prefix and suffix.
684
685 Arrays must also do this for DECL nodes, like int a[], and for things like
69cb846f 686 int *[]&. */
471086d6 687
9031d10b 688static void
f01d4233 689dump_type_prefix (tree t, int flags)
471086d6 690{
691 if (TYPE_PTRMEMFUNC_P (t))
692 {
693 t = TYPE_PTRMEMFUNC_FN_TYPE (t);
694 goto offset_type;
695 }
50cd3f45 696
471086d6 697 switch (TREE_CODE (t))
698 {
699 case POINTER_TYPE:
3e04bd45 700 case REFERENCE_TYPE:
471086d6 701 {
702 tree sub = TREE_TYPE (t);
50cd3f45 703
69cb846f 704 dump_type_prefix (sub, flags);
75eaa947 705 if (TREE_CODE (sub) == ARRAY_TYPE
706 || TREE_CODE (sub) == FUNCTION_TYPE)
471086d6 707 {
573c13ef 708 pp_cxx_whitespace (cxx_pp);
709 pp_cxx_left_paren (cxx_pp);
ee212425 710 pp_c_attributes_display (pp_c_base (cxx_pp),
711 TYPE_ATTRIBUTES (sub));
471086d6 712 }
63949b38 713 if (TREE_CODE (t) == POINTER_TYPE)
714 pp_character(cxx_pp, '*');
715 else if (TREE_CODE (t) == REFERENCE_TYPE)
716 {
717 if (TYPE_REF_IS_RVALUE (t))
718 pp_string (cxx_pp, "&&");
719 else
720 pp_character (cxx_pp, '&');
721 }
653e5405 722 pp_base (cxx_pp)->padding = pp_before;
723 pp_cxx_cv_qualifier_seq (cxx_pp, t);
471086d6 724 }
471086d6 725 break;
726
727 case OFFSET_TYPE:
728 offset_type:
69cb846f 729 dump_type_prefix (TREE_TYPE (t), flags);
ac9386a0 730 if (TREE_CODE (t) == OFFSET_TYPE) /* pmfs deal with this in d_t_p */
731 {
653e5405 732 pp_maybe_space (cxx_pp);
733 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
734 pp_cxx_left_paren (cxx_pp);
d04f0501 735 dump_type (TYPE_OFFSET_BASETYPE (t), flags);
573c13ef 736 pp_cxx_colon_colon (cxx_pp);
ac9386a0 737 }
573c13ef 738 pp_cxx_star (cxx_pp);
69cb846f 739 pp_cxx_cv_qualifier_seq (cxx_pp, t);
32ce9847 740 pp_base (cxx_pp)->padding = pp_before;
471086d6 741 break;
742
75eaa947 743 /* This can be reached without a pointer when dealing with
744 templates, e.g. std::is_function. */
471086d6 745 case FUNCTION_TYPE:
69cb846f 746 dump_type_prefix (TREE_TYPE (t), flags);
471086d6 747 break;
748
749 case METHOD_TYPE:
69cb846f 750 dump_type_prefix (TREE_TYPE (t), flags);
751 pp_maybe_space (cxx_pp);
573c13ef 752 pp_cxx_left_paren (cxx_pp);
d04f0501 753 dump_aggr_type (TYPE_METHOD_BASETYPE (t), flags);
573c13ef 754 pp_cxx_colon_colon (cxx_pp);
471086d6 755 break;
756
757 case ARRAY_TYPE:
69cb846f 758 dump_type_prefix (TREE_TYPE (t), flags);
471086d6 759 break;
760
761 case ENUMERAL_TYPE:
471086d6 762 case IDENTIFIER_NODE:
763 case INTEGER_TYPE:
bb0726a1 764 case BOOLEAN_TYPE:
471086d6 765 case REAL_TYPE:
766 case RECORD_TYPE:
767 case TEMPLATE_TYPE_PARM:
cd5dd2d8 768 case TEMPLATE_TEMPLATE_PARM:
1e93ca27 769 case BOUND_TEMPLATE_TEMPLATE_PARM:
471086d6 770 case TREE_LIST:
771 case TYPE_DECL:
772 case TREE_VEC:
471086d6 773 case UNION_TYPE:
4fdaf896 774 case LANG_TYPE:
471086d6 775 case VOID_TYPE:
e857e9c7 776 case TYPENAME_TYPE:
c4a8ac95 777 case COMPLEX_TYPE:
90a83261 778 case VECTOR_TYPE:
d3b75e30 779 case TYPEOF_TYPE:
8de5c43e 780 case UNDERLYING_TYPE:
34da8800 781 case DECLTYPE_TYPE:
e4fbeb04 782 case TYPE_PACK_EXPANSION:
6ce18e5b 783 case FIXED_POINT_TYPE:
fa67d8e8 784 case NULLPTR_TYPE:
d04f0501 785 dump_type (t, flags);
69cb846f 786 pp_base (cxx_pp)->padding = pp_before;
471086d6 787 break;
50cd3f45 788
471086d6 789 default:
aa6db498 790 pp_unsupported_tree (cxx_pp, t);
da901964 791 /* fall through. */
d04f0501 792 case ERROR_MARK:
a608187f 793 pp_string (cxx_pp, M_("<typeprefixerror>"));
d04f0501 794 break;
471086d6 795 }
796}
797
d04f0501 798/* Dump the suffix of type T, under control of FLAGS. This is the part
799 which appears after the identifier (or function parms). */
800
471086d6 801static void
f01d4233 802dump_type_suffix (tree t, int flags)
471086d6 803{
804 if (TYPE_PTRMEMFUNC_P (t))
805 t = TYPE_PTRMEMFUNC_FN_TYPE (t);
806
807 switch (TREE_CODE (t))
808 {
809 case POINTER_TYPE:
810 case REFERENCE_TYPE:
811 case OFFSET_TYPE:
75eaa947 812 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
813 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
573c13ef 814 pp_cxx_right_paren (cxx_pp);
d04f0501 815 dump_type_suffix (TREE_TYPE (t), flags);
471086d6 816 break;
817
471086d6 818 case FUNCTION_TYPE:
819 case METHOD_TYPE:
820 {
821 tree arg;
75eaa947 822 if (TREE_CODE (t) == METHOD_TYPE)
823 /* Can only be reached through a pointer. */
824 pp_cxx_right_paren (cxx_pp);
471086d6 825 arg = TYPE_ARG_TYPES (t);
826 if (TREE_CODE (t) == METHOD_TYPE)
827 arg = TREE_CHAIN (arg);
828
8998b619 829 /* Function pointers don't have default args. Not in standard C++,
830 anyway; they may in g++, but we'll just pretend otherwise. */
682959c4 831 dump_parameters (arg, flags & ~TFF_FUNCTION_DEFAULT_ARGUMENTS);
8998b619 832
471086d6 833 if (TREE_CODE (t) == METHOD_TYPE)
a8d966c1 834 pp_cxx_cv_qualifier_seq (cxx_pp, class_of_this_parm (t));
653e5405 835 else
75eaa947 836 pp_cxx_cv_qualifier_seq (cxx_pp, t);
d04f0501 837 dump_exception_spec (TYPE_RAISES_EXCEPTIONS (t), flags);
7f5c5d3e 838 dump_type_suffix (TREE_TYPE (t), flags);
471086d6 839 break;
840 }
841
842 case ARRAY_TYPE:
32ce9847 843 pp_maybe_space (cxx_pp);
573c13ef 844 pp_cxx_left_bracket (cxx_pp);
471086d6 845 if (TYPE_DOMAIN (t))
3cc0b4b9 846 {
c47c77bb 847 tree dtype = TYPE_DOMAIN (t);
848 tree max = TYPE_MAX_VALUE (dtype);
e3a4c2db 849 if (integer_all_onesp (max))
850 pp_character (cxx_pp, '0');
851 else if (host_integerp (max, 0))
c47c77bb 852 pp_wide_integer (cxx_pp, tree_low_cst (max, 0) + 1);
853 else if (TREE_CODE (max) == MINUS_EXPR)
854 dump_expr (TREE_OPERAND (max, 0),
653e5405 855 flags & ~TFF_EXPR_IN_PARENS);
3cc0b4b9 856 else
389dd41b 857 dump_expr (fold_build2_loc (input_location,
858 PLUS_EXPR, dtype, max,
c47c77bb 859 build_int_cst (dtype, 1)),
653e5405 860 flags & ~TFF_EXPR_IN_PARENS);
3cc0b4b9 861 }
573c13ef 862 pp_cxx_right_bracket (cxx_pp);
d04f0501 863 dump_type_suffix (TREE_TYPE (t), flags);
471086d6 864 break;
50cd3f45 865
471086d6 866 case ENUMERAL_TYPE:
471086d6 867 case IDENTIFIER_NODE:
868 case INTEGER_TYPE:
bb0726a1 869 case BOOLEAN_TYPE:
471086d6 870 case REAL_TYPE:
871 case RECORD_TYPE:
872 case TEMPLATE_TYPE_PARM:
cd5dd2d8 873 case TEMPLATE_TEMPLATE_PARM:
c405dc58 874 case BOUND_TEMPLATE_TEMPLATE_PARM:
471086d6 875 case TREE_LIST:
876 case TYPE_DECL:
877 case TREE_VEC:
471086d6 878 case UNION_TYPE:
4fdaf896 879 case LANG_TYPE:
471086d6 880 case VOID_TYPE:
e857e9c7 881 case TYPENAME_TYPE:
c4a8ac95 882 case COMPLEX_TYPE:
90a83261 883 case VECTOR_TYPE:
d3b75e30 884 case TYPEOF_TYPE:
8de5c43e 885 case UNDERLYING_TYPE:
34da8800 886 case DECLTYPE_TYPE:
e4fbeb04 887 case TYPE_PACK_EXPANSION:
6ce18e5b 888 case FIXED_POINT_TYPE:
fa67d8e8 889 case NULLPTR_TYPE:
471086d6 890 break;
891
892 default:
aa6db498 893 pp_unsupported_tree (cxx_pp, t);
d04f0501 894 case ERROR_MARK:
895 /* Don't mark it here, we should have already done in
653e5405 896 dump_type_prefix. */
d04f0501 897 break;
471086d6 898 }
899}
900
71ccdfff 901static void
f01d4233 902dump_global_iord (tree t)
471086d6 903{
da901964 904 const char *p = NULL;
471086d6 905
7e64c604 906 if (DECL_GLOBAL_CTOR_P (t))
a608187f 907 p = M_("(static initializers for %s)");
7e64c604 908 else if (DECL_GLOBAL_DTOR_P (t))
a608187f 909 p = M_("(static destructors for %s)");
471086d6 910 else
092b1d6f 911 gcc_unreachable ();
50cd3f45 912
1e00012f 913 pp_printf (pp_base (cxx_pp), p, input_filename);
471086d6 914}
915
3e97700e 916static void
f01d4233 917dump_simple_decl (tree t, tree type, int flags)
3e97700e 918{
682959c4 919 if (flags & TFF_DECL_SPECIFIERS)
3e97700e 920 {
7f69857d 921 if (TREE_CODE (t) == VAR_DECL
922 && DECL_DECLARED_CONSTEXPR_P (t))
923 pp_cxx_ws_string (cxx_pp, "constexpr");
facb12b2 924 dump_type_prefix (type, flags & ~TFF_UNQUALIFIED_NAME);
69cb846f 925 pp_maybe_space (cxx_pp);
3e97700e 926 }
facb12b2 927 if (! (flags & TFF_UNQUALIFIED_NAME)
6d00b16b 928 && TREE_CODE (t) != PARM_DECL
facb12b2 929 && (!DECL_INITIAL (t)
930 || TREE_CODE (DECL_INITIAL (t)) != TEMPLATE_PARM_INDEX))
d04f0501 931 dump_scope (CP_DECL_CONTEXT (t), flags);
facb12b2 932 flags &= ~TFF_UNQUALIFIED_NAME;
7485db95 933 if ((flags & TFF_DECL_SPECIFIERS)
934 && DECL_TEMPLATE_PARM_P (t)
935 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (t)))
1e00012f 936 pp_string (cxx_pp, "...");
3e97700e 937 if (DECL_NAME (t))
d04f0501 938 dump_decl (DECL_NAME (t), flags);
3e97700e 939 else
a608187f 940 pp_string (cxx_pp, M_("<anonymous>"));
682959c4 941 if (flags & TFF_DECL_SPECIFIERS)
d04f0501 942 dump_type_suffix (type, flags);
3e97700e 943}
944
d04f0501 945/* Dump a human readable string for the decl T under control of FLAGS. */
946
471086d6 947static void
f01d4233 948dump_decl (tree t, int flags)
471086d6 949{
950 if (t == NULL_TREE)
951 return;
952
601154bb 953 /* If doing Objective-C++, give Objective-C a chance to demangle
954 Objective-C method names. */
955 if (c_dialect_objc ())
956 {
957 const char *demangled = objc_maybe_printable_name (t, flags);
958 if (demangled)
959 {
960 pp_string (cxx_pp, demangled);
961 return;
962 }
963 }
964
471086d6 965 switch (TREE_CODE (t))
966 {
b0722fac 967 case TYPE_DECL:
c0f42aca 968 /* Don't say 'typedef class A' */
4ebfe3f3 969 if (DECL_ARTIFICIAL (t) && !DECL_SELF_REFERENCE_P (t))
c0f42aca 970 {
971 if ((flags & TFF_DECL_SPECIFIERS)
972 && TREE_CODE (TREE_TYPE (t)) == TEMPLATE_TYPE_PARM)
7485db95 973 {
974 /* Say `class T' not just `T'. */
1e00012f 975 pp_cxx_ws_string (cxx_pp, "class");
7485db95 976
977 /* Emit the `...' for a parameter pack. */
978 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
1e00012f 979 pp_cxx_ws_string (cxx_pp, "...");
7485db95 980 }
9031d10b 981
c0f42aca 982 dump_type (TREE_TYPE (t), flags);
983 break;
984 }
370478b1 985 if (TYPE_DECL_ALIAS_P (t)
986 && (flags & TFF_DECL_SPECIFIERS
987 || flags & TFF_CLASS_KEY_OR_ENUM))
988 {
989 pp_cxx_ws_string (cxx_pp, "using");
990 dump_decl (DECL_NAME (t), flags);
991 pp_cxx_whitespace (cxx_pp);
992 pp_cxx_ws_string (cxx_pp, "=");
993 pp_cxx_whitespace (cxx_pp);
994 dump_type (DECL_ORIGINAL_TYPE (t), flags);
995 break;
996 }
6dd3a38d 997 if ((flags & TFF_DECL_SPECIFIERS)
998 && !DECL_SELF_REFERENCE_P (t))
1e00012f 999 pp_cxx_ws_string (cxx_pp, "typedef");
50cd3f45 1000 dump_simple_decl (t, DECL_ORIGINAL_TYPE (t)
d04f0501 1001 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t),
653e5405 1002 flags);
b0722fac 1003 break;
50cd3f45 1004
471086d6 1005 case VAR_DECL:
1e66592c 1006 if (DECL_NAME (t) && VTABLE_NAME_P (DECL_NAME (t)))
471086d6 1007 {
a608187f 1008 pp_string (cxx_pp, M_("vtable for "));
b4df430b 1009 gcc_assert (TYPE_P (DECL_CONTEXT (t)));
dc9b5a48 1010 dump_type (DECL_CONTEXT (t), flags);
471086d6 1011 break;
1012 }
331bc0ad 1013 /* Else fall through. */
471086d6 1014 case FIELD_DECL:
1015 case PARM_DECL:
d04f0501 1016 dump_simple_decl (t, TREE_TYPE (t), flags);
471086d6 1017 break;
1018
21854fc6 1019 case RESULT_DECL:
a608187f 1020 pp_string (cxx_pp, M_("<return value> "));
21854fc6 1021 dump_simple_decl (t, TREE_TYPE (t), flags);
1022 break;
1023
d0622bdf 1024 case NAMESPACE_DECL:
2a9d763b 1025 if (flags & TFF_DECL_SPECIFIERS)
653e5405 1026 pp_cxx_declaration (cxx_pp, t);
6378afde 1027 else
653e5405 1028 {
facb12b2 1029 if (! (flags & TFF_UNQUALIFIED_NAME))
1030 dump_scope (CP_DECL_CONTEXT (t), flags);
1031 flags &= ~TFF_UNQUALIFIED_NAME;
653e5405 1032 if (DECL_NAME (t) == NULL_TREE)
8a47db47 1033 {
1034 if (!(pp_c_base (cxx_pp)->flags & pp_c_flag_gnu_v3))
1035 pp_cxx_ws_string (cxx_pp, M_("{anonymous}"));
1036 else
1037 pp_cxx_ws_string (cxx_pp, M_("(anonymous namespace)"));
1038 }
653e5405 1039 else
1040 pp_cxx_tree_identifier (cxx_pp, DECL_NAME (t));
1041 }
d0622bdf 1042 break;
1043
626864c5 1044 case SCOPE_REF:
5bd9bf06 1045 dump_type (TREE_OPERAND (t, 0), flags);
1046 pp_string (cxx_pp, "::");
d0b4bf06 1047 dump_decl (TREE_OPERAND (t, 1), TFF_UNQUALIFIED_NAME);
50cd3f45 1048 break;
626864c5 1049
471086d6 1050 case ARRAY_REF:
d04f0501 1051 dump_decl (TREE_OPERAND (t, 0), flags);
573c13ef 1052 pp_cxx_left_bracket (cxx_pp);
d04f0501 1053 dump_decl (TREE_OPERAND (t, 1), flags);
573c13ef 1054 pp_cxx_right_bracket (cxx_pp);
471086d6 1055 break;
1056
d04f0501 1057 /* So that we can do dump_decl on an aggr type. */
471086d6 1058 case RECORD_TYPE:
1059 case UNION_TYPE:
1060 case ENUMERAL_TYPE:
d04f0501 1061 dump_type (t, flags);
471086d6 1062 break;
1063
63d857a2 1064 case BIT_NOT_EXPR:
071d2c90 1065 /* This is a pseudo destructor call which has not been folded into
653e5405 1066 a PSEUDO_DTOR_EXPR yet. */
573c13ef 1067 pp_cxx_complement (cxx_pp);
63d857a2 1068 dump_type (TREE_OPERAND (t, 0), flags);
1069 break;
1070
471086d6 1071 case TYPE_EXPR:
092b1d6f 1072 gcc_unreachable ();
471086d6 1073 break;
1074
1075 /* These special cases are duplicated here so that other functions
cf103c6c 1076 can feed identifiers to error and get them demangled properly. */
471086d6 1077 case IDENTIFIER_NODE:
90455671 1078 if (IDENTIFIER_TYPENAME_P (t))
1079 {
1e00012f 1080 pp_cxx_ws_string (cxx_pp, "operator");
90455671 1081 /* Not exactly IDENTIFIER_TYPE_VALUE. */
1082 dump_type (TREE_TYPE (t), flags);
1083 break;
1084 }
1085 else
573c13ef 1086 pp_cxx_tree_identifier (cxx_pp, t);
471086d6 1087 break;
1088
1eaf178d 1089 case OVERLOAD:
1cfa8cdd 1090 if (OVL_CHAIN (t))
1091 {
1092 t = OVL_CURRENT (t);
1093 if (DECL_CLASS_SCOPE_P (t))
1094 {
1095 dump_type (DECL_CONTEXT (t), flags);
653e5405 1096 pp_cxx_colon_colon (cxx_pp);
1cfa8cdd 1097 }
d4f83d18 1098 else if (!DECL_FILE_SCOPE_P (t))
1cfa8cdd 1099 {
1100 dump_decl (DECL_CONTEXT (t), flags);
653e5405 1101 pp_cxx_colon_colon (cxx_pp);
1cfa8cdd 1102 }
1103 dump_decl (DECL_NAME (t), flags);
1104 break;
1105 }
9031d10b 1106
1cfa8cdd 1107 /* If there's only one function, just treat it like an ordinary
1108 FUNCTION_DECL. */
1eaf178d 1109 t = OVL_CURRENT (t);
1110 /* Fall through. */
1111
471086d6 1112 case FUNCTION_DECL:
f134e87d 1113 if (! DECL_LANG_SPECIFIC (t))
a608187f 1114 pp_string (cxx_pp, M_("<built-in>"));
f134e87d 1115 else if (DECL_GLOBAL_CTOR_P (t) || DECL_GLOBAL_DTOR_P (t))
1116 dump_global_iord (t);
471086d6 1117 else
653e5405 1118 dump_function_decl (t, flags);
471086d6 1119 break;
1120
1121 case TEMPLATE_DECL:
ed6762c8 1122 dump_template_decl (t, flags);
471086d6 1123 break;
1124
aa8ce852 1125 case TEMPLATE_ID_EXPR:
1126 {
7f634b7a 1127 tree name = TREE_OPERAND (t, 0);
7d96d151 1128 tree args = TREE_OPERAND (t, 1);
9031d10b 1129
7f634b7a 1130 if (is_overloaded_fn (name))
1131 name = DECL_NAME (get_first_fn (name));
d04f0501 1132 dump_decl (name, flags);
573c13ef 1133 pp_cxx_begin_template_argument_list (cxx_pp);
7d96d151 1134 if (args == error_mark_node)
1135 pp_string (cxx_pp, M_("<template arguments error>"));
1136 else if (args)
1137 dump_template_argument_list (args, flags);
1138 pp_cxx_end_template_argument_list (cxx_pp);
aa8ce852 1139 }
1140 break;
1141
471086d6 1142 case LABEL_DECL:
573c13ef 1143 pp_cxx_tree_identifier (cxx_pp, DECL_NAME (t));
471086d6 1144 break;
1145
1146 case CONST_DECL:
596c0ae6 1147 if ((TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == ENUMERAL_TYPE)
9c7a56ae 1148 || (DECL_INITIAL (t) &&
f3110581 1149 TREE_CODE (DECL_INITIAL (t)) == TEMPLATE_PARM_INDEX))
d04f0501 1150 dump_simple_decl (t, TREE_TYPE (t), flags);
c4817b54 1151 else if (DECL_NAME (t))
d04f0501 1152 dump_decl (DECL_NAME (t), flags);
9c7a56ae 1153 else if (DECL_INITIAL (t))
682959c4 1154 dump_expr (DECL_INITIAL (t), flags | TFF_EXPR_IN_PARENS);
9c7a56ae 1155 else
a608187f 1156 pp_string (cxx_pp, M_("<enumerator>"));
471086d6 1157 break;
1158
fff5e605 1159 case USING_DECL:
1e00012f 1160 pp_cxx_ws_string (cxx_pp, "using");
3884f97b 1161 dump_type (USING_DECL_SCOPE (t), flags);
573c13ef 1162 pp_cxx_colon_colon (cxx_pp);
0b0f397c 1163 dump_decl (DECL_NAME (t), flags);
fff5e605 1164 break;
1165
7a05c4b1 1166 case STATIC_ASSERT:
1167 pp_cxx_declaration (cxx_pp, t);
1168 break;
1169
4ac852cb 1170 case BASELINK:
1171 dump_decl (BASELINK_FUNCTIONS (t), flags);
1172 break;
1173
13795292 1174 case NON_DEPENDENT_EXPR:
1175 dump_expr (t, flags);
1176 break;
1177
7dddebd7 1178 case TEMPLATE_TYPE_PARM:
1179 if (flags & TFF_DECL_SPECIFIERS)
653e5405 1180 pp_cxx_declaration (cxx_pp, t);
7dddebd7 1181 else
653e5405 1182 pp_type_id (cxx_pp, t);
7dddebd7 1183 break;
1184
d676ce6e 1185 case UNBOUND_CLASS_TEMPLATE:
f9550563 1186 case TYPE_PACK_EXPANSION:
95540e69 1187 case TREE_BINFO:
d676ce6e 1188 dump_type (t, flags);
1189 break;
1190
471086d6 1191 default:
aa6db498 1192 pp_unsupported_tree (cxx_pp, t);
0f3ccaa3 1193 /* Fall through to error. */
d04f0501 1194
1195 case ERROR_MARK:
a608187f 1196 pp_string (cxx_pp, M_("<declaration error>"));
d04f0501 1197 break;
1198 }
1199}
1200
1201/* Dump a template declaration T under control of FLAGS. This means the
1202 'template <...> leaders plus the 'class X' or 'void fn(...)' part. */
1203
1204static void
f01d4233 1205dump_template_decl (tree t, int flags)
d04f0501 1206{
a6604172 1207 tree orig_parms = DECL_TEMPLATE_PARMS (t);
1208 tree parms;
50cd3f45 1209 int i;
1210
682959c4 1211 if (flags & TFF_TEMPLATE_HEADER)
d04f0501 1212 {
50cd3f45 1213 for (parms = orig_parms = nreverse (orig_parms);
653e5405 1214 parms;
1215 parms = TREE_CHAIN (parms))
1216 {
a6604172 1217 tree inner_parms = INNERMOST_TEMPLATE_PARMS (parms);
653e5405 1218 int len = TREE_VEC_LENGTH (inner_parms);
50cd3f45 1219
1e00012f 1220 pp_cxx_ws_string (cxx_pp, "template");
653e5405 1221 pp_cxx_begin_template_argument_list (cxx_pp);
ed6762c8 1222
1223 /* If we've shown the template prefix, we'd better show the
1224 parameters' and decl's type too. */
1225 flags |= TFF_DECL_SPECIFIERS;
1226
653e5405 1227 for (i = 0; i < len; i++)
1228 {
1229 if (i)
1230 pp_separate_with_comma (cxx_pp);
1231 dump_template_parameter (TREE_VEC_ELT (inner_parms, i), flags);
1232 }
1233 pp_cxx_end_template_argument_list (cxx_pp);
1234 pp_cxx_whitespace (cxx_pp);
1235 }
a6604172 1236 nreverse(orig_parms);
ed6762c8 1237
1238 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
7485db95 1239 {
1240 /* Say `template<arg> class TT' not just `template<arg> TT'. */
1e00012f 1241 pp_cxx_ws_string (cxx_pp, "class");
7485db95 1242
1243 /* If this is a parameter pack, print the ellipsis. */
1244 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
1e00012f 1245 pp_cxx_ws_string (cxx_pp, "...");
7485db95 1246 }
d04f0501 1247 }
ed6762c8 1248
370478b1 1249 if (DECL_CLASS_TEMPLATE_P (t))
d04f0501 1250 dump_type (TREE_TYPE (t),
653e5405 1251 ((flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1252 | (flags & TFF_DECL_SPECIFIERS ? TFF_CLASS_KEY_OR_ENUM : 0)));
f25aa85d 1253 else if (DECL_TEMPLATE_RESULT (t)
370478b1 1254 && (TREE_CODE (DECL_TEMPLATE_RESULT (t)) == VAR_DECL
1255 /* Alias template. */
1256 || DECL_TYPE_TEMPLATE_P (t)))
682959c4 1257 dump_decl (DECL_TEMPLATE_RESULT (t), flags | TFF_TEMPLATE_NAME);
d04f0501 1258 else
d04f0501 1259 {
092b1d6f 1260 gcc_assert (TREE_TYPE (t));
1261 switch (NEXT_CODE (t))
1262 {
1263 case METHOD_TYPE:
1264 case FUNCTION_TYPE:
1265 dump_function_decl (t, flags | TFF_TEMPLATE_NAME);
1266 break;
1267 default:
1268 /* This case can occur with some invalid code. */
1269 dump_type (TREE_TYPE (t),
1270 (flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1271 | (flags & TFF_DECL_SPECIFIERS
1272 ? TFF_CLASS_KEY_OR_ENUM : 0));
1273 }
471086d6 1274 }
1275}
1276
40c700ab 1277/* find_typenames looks through the type of the function template T
f1f41a6c 1278 and returns a vec containing any typedefs, decltypes or TYPENAME_TYPEs
1c1c1457 1279 it finds. */
40c700ab 1280
1281struct find_typenames_t
1282{
1283 struct pointer_set_t *p_set;
f1f41a6c 1284 vec<tree, va_gc> *typenames;
40c700ab 1285};
1286
1287static tree
a49c5913 1288find_typenames_r (tree *tp, int * /*walk_subtrees*/, void *data)
40c700ab 1289{
1290 struct find_typenames_t *d = (struct find_typenames_t *)data;
a409f927 1291 tree mv = NULL_TREE;
1292
1293 if (TYPE_P (*tp) && is_typedef_decl (TYPE_NAME (*tp)))
1294 /* Add the type of the typedef without any additional cv-quals. */
1295 mv = TREE_TYPE (TYPE_NAME (*tp));
1c1c1457 1296 else if (TREE_CODE (*tp) == TYPENAME_TYPE
1297 || TREE_CODE (*tp) == DECLTYPE_TYPE)
a409f927 1298 /* Add the typename without any cv-qualifiers. */
1299 mv = TYPE_MAIN_VARIANT (*tp);
1300
1301 if (mv && (mv == *tp || !pointer_set_insert (d->p_set, mv)))
f1f41a6c 1302 vec_safe_push (d->typenames, mv);
40c700ab 1303
40c700ab 1304 /* Search into class template arguments, which cp_walk_subtrees
1305 doesn't do. */
a409f927 1306 if (CLASS_TYPE_P (*tp) && CLASSTYPE_TEMPLATE_INFO (*tp))
1307 cp_walk_tree (&CLASSTYPE_TI_ARGS (*tp), find_typenames_r,
1308 data, d->p_set);
1309
40c700ab 1310 return NULL_TREE;
1311}
1312
f1f41a6c 1313static vec<tree, va_gc> *
40c700ab 1314find_typenames (tree t)
1315{
1316 struct find_typenames_t ft;
1317 ft.p_set = pointer_set_create ();
1318 ft.typenames = NULL;
1319 cp_walk_tree (&TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
1320 find_typenames_r, &ft, ft.p_set);
1321 pointer_set_destroy (ft.p_set);
1322 return ft.typenames;
1323}
1324
8998b619 1325/* Pretty print a function decl. There are several ways we want to print a
682959c4 1326 function declaration. The TFF_ bits in FLAGS tells us how to behave.
cf103c6c 1327 As error can only apply the '#' flag once to give 0 and 1 for V, there
47cd6605 1328 is %D which doesn't print the throw specs, and %F which does. */
471086d6 1329
1330static void
f01d4233 1331dump_function_decl (tree t, int flags)
471086d6 1332{
64b4f183 1333 tree fntype;
1334 tree parmtypes;
471086d6 1335 tree cname = NULL_TREE;
a2a627ce 1336 tree template_args = NULL_TREE;
1337 tree template_parms = NULL_TREE;
682959c4 1338 int show_return = flags & TFF_RETURN_TYPE || flags & TFF_DECL_SPECIFIERS;
facb12b2 1339 int do_outer_scope = ! (flags & TFF_UNQUALIFIED_NAME);
35f85a5a 1340 tree exceptions;
f1f41a6c 1341 vec<tree, va_gc> *typenames = NULL;
471086d6 1342
1f3c640c 1343 if (DECL_NAME (t) && LAMBDA_FUNCTION_P (t))
a8b75081 1344 {
1345 /* A lambda's signature is essentially its "type", so defer. */
1346 gcc_assert (LAMBDA_TYPE_P (DECL_CONTEXT (t)));
1347 dump_type (DECL_CONTEXT (t), flags);
1348 return;
1349 }
1350
a2dc95eb 1351 flags &= ~(TFF_UNQUALIFIED_NAME | TFF_TEMPLATE_NAME);
64b4f183 1352 if (TREE_CODE (t) == TEMPLATE_DECL)
1353 t = DECL_TEMPLATE_RESULT (t);
1354
35f85a5a 1355 /* Save the exceptions, in case t is a specialization and we are
1356 emitting an error about incompatible specifications. */
1357 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (t));
1358
a2a627ce 1359 /* Pretty print template instantiations only. */
fc8ee10c 1360 if (DECL_USE_TEMPLATE (t) && DECL_TEMPLATE_INFO (t)
1361 && flag_pretty_templates)
a2a627ce 1362 {
a32fe883 1363 tree tmpl;
1364
a2a627ce 1365 template_args = DECL_TI_ARGS (t);
a32fe883 1366 tmpl = most_general_template (t);
1367 if (tmpl && TREE_CODE (tmpl) == TEMPLATE_DECL)
1368 {
1369 template_parms = DECL_TEMPLATE_PARMS (tmpl);
1370 t = tmpl;
40c700ab 1371 typenames = find_typenames (t);
a32fe883 1372 }
a2a627ce 1373 }
1374
64b4f183 1375 fntype = TREE_TYPE (t);
dcbeb3ef 1376 parmtypes = FUNCTION_FIRST_USER_PARMTYPE (t);
64b4f183 1377
caa99b15 1378 if (DECL_CLASS_SCOPE_P (t))
9ba4048d 1379 cname = DECL_CONTEXT (t);
331bc0ad 1380 /* This is for partially instantiated template methods. */
471086d6 1381 else if (TREE_CODE (fntype) == METHOD_TYPE)
1382 cname = TREE_TYPE (TREE_VALUE (parmtypes));
1383
7f69857d 1384 if (flags & TFF_DECL_SPECIFIERS)
1385 {
1386 if (DECL_STATIC_FUNCTION_P (t))
1387 pp_cxx_ws_string (cxx_pp, "static");
1388 else if (DECL_VIRTUAL_P (t))
1389 pp_cxx_ws_string (cxx_pp, "virtual");
1390
1391 if (DECL_DECLARED_CONSTEXPR_P (STRIP_TEMPLATE (t)))
1392 pp_cxx_ws_string (cxx_pp, "constexpr");
1393 }
50cd3f45 1394
d04f0501 1395 /* Print the return type? */
1396 if (show_return)
1397 show_return = !DECL_CONV_FN_P (t) && !DECL_CONSTRUCTOR_P (t)
653e5405 1398 && !DECL_DESTRUCTOR_P (t);
d04f0501 1399 if (show_return)
573c13ef 1400 dump_type_prefix (TREE_TYPE (fntype), flags);
471086d6 1401
477f2174 1402 /* Print the function name. */
facb12b2 1403 if (!do_outer_scope)
1404 /* Nothing. */;
1405 else if (cname)
471086d6 1406 {
d04f0501 1407 dump_type (cname, flags);
573c13ef 1408 pp_cxx_colon_colon (cxx_pp);
caa99b15 1409 }
d04f0501 1410 else
1411 dump_scope (CP_DECL_CONTEXT (t), flags);
471086d6 1412
d04f0501 1413 dump_function_name (t, flags);
50cd3f45 1414
3d2a7578 1415 if (!(flags & TFF_NO_FUNCTION_ARGUMENTS))
a1abbd01 1416 {
a1abbd01 1417 dump_parameters (parmtypes, flags);
50cd3f45 1418
a1abbd01 1419 if (TREE_CODE (fntype) == METHOD_TYPE)
653e5405 1420 {
1421 pp_base (cxx_pp)->padding = pp_before;
a8d966c1 1422 pp_cxx_cv_qualifier_seq (cxx_pp, class_of_this_parm (fntype));
653e5405 1423 }
50cd3f45 1424
682959c4 1425 if (flags & TFF_EXCEPTION_SPECIFICATION)
653e5405 1426 {
1427 pp_base (cxx_pp)->padding = pp_before;
35f85a5a 1428 dump_exception_spec (exceptions, flags);
653e5405 1429 }
7f5c5d3e 1430
1431 if (show_return)
1432 dump_type_suffix (TREE_TYPE (fntype), flags);
a2a627ce 1433
673064cf 1434 /* If T is a template instantiation, dump the parameter binding. */
1435 if (template_parms != NULL_TREE && template_args != NULL_TREE)
1436 {
1437 pp_cxx_whitespace (cxx_pp);
1438 pp_cxx_left_bracket (cxx_pp);
1439 pp_cxx_ws_string (cxx_pp, M_("with"));
1440 pp_cxx_whitespace (cxx_pp);
1441 dump_template_bindings (template_parms, template_args, typenames);
1442 pp_cxx_right_bracket (cxx_pp);
1443 }
1444 }
1445 else if (template_args)
a2a627ce 1446 {
673064cf 1447 bool need_comma = false;
1448 int i;
1449 pp_cxx_begin_template_argument_list (cxx_pp);
1450 template_args = INNERMOST_TEMPLATE_ARGS (template_args);
1451 for (i = 0; i < TREE_VEC_LENGTH (template_args); ++i)
1452 {
1453 tree arg = TREE_VEC_ELT (template_args, i);
1454 if (need_comma)
1455 pp_separate_with_comma (cxx_pp);
1456 if (ARGUMENT_PACK_P (arg))
1457 pp_cxx_left_brace (cxx_pp);
1458 dump_template_argument (arg, TFF_PLAIN_IDENTIFIER);
1459 if (ARGUMENT_PACK_P (arg))
1460 pp_cxx_right_brace (cxx_pp);
1461 need_comma = true;
1462 }
1463 pp_cxx_end_template_argument_list (cxx_pp);
a2a627ce 1464 }
8998b619 1465}
1466
d04f0501 1467/* Print a parameter list. If this is for a member function, the
1468 member object ptr (and any other hidden args) should have
47cd6605 1469 already been removed. */
8998b619 1470
1471static void
f01d4233 1472dump_parameters (tree parmtypes, int flags)
8998b619 1473{
d95d815d 1474 int first = 1;
6d00b16b 1475 flags &= ~TFF_SCOPE;
573c13ef 1476 pp_cxx_left_paren (cxx_pp);
8998b619 1477
1478 for (first = 1; parmtypes != void_list_node;
1479 parmtypes = TREE_CHAIN (parmtypes))
1480 {
1481 if (!first)
653e5405 1482 pp_separate_with_comma (cxx_pp);
8998b619 1483 first = 0;
1484 if (!parmtypes)
653e5405 1485 {
1e00012f 1486 pp_cxx_ws_string (cxx_pp, "...");
653e5405 1487 break;
1488 }
46147ffe 1489
1490 dump_type (TREE_VALUE (parmtypes), flags);
50cd3f45 1491
682959c4 1492 if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && TREE_PURPOSE (parmtypes))
653e5405 1493 {
1494 pp_cxx_whitespace (cxx_pp);
1495 pp_equal (cxx_pp);
1496 pp_cxx_whitespace (cxx_pp);
1497 dump_expr (TREE_PURPOSE (parmtypes), flags | TFF_EXPR_IN_PARENS);
1498 }
8998b619 1499 }
1500
573c13ef 1501 pp_cxx_right_paren (cxx_pp);
8998b619 1502}
1503
47cd6605 1504/* Print an exception specification. T is the exception specification. */
8998b619 1505
1506static void
f01d4233 1507dump_exception_spec (tree t, int flags)
8998b619 1508{
3644efa5 1509 if (t && TREE_PURPOSE (t))
1510 {
1511 pp_cxx_ws_string (cxx_pp, "noexcept");
1512 pp_cxx_whitespace (cxx_pp);
1513 pp_cxx_left_paren (cxx_pp);
6bb4902d 1514 if (DEFERRED_NOEXCEPT_SPEC_P (t))
1515 pp_cxx_ws_string (cxx_pp, "<uninstantiated>");
1516 else
1517 dump_expr (TREE_PURPOSE (t), flags);
3644efa5 1518 pp_cxx_right_paren (cxx_pp);
1519 }
1520 else if (t)
8998b619 1521 {
1e00012f 1522 pp_cxx_ws_string (cxx_pp, "throw");
573c13ef 1523 pp_cxx_whitespace (cxx_pp);
1524 pp_cxx_left_paren (cxx_pp);
8998b619 1525 if (TREE_VALUE (t) != NULL_TREE)
653e5405 1526 while (1)
1527 {
1528 dump_type (TREE_VALUE (t), flags);
1529 t = TREE_CHAIN (t);
1530 if (!t)
1531 break;
1532 pp_separate_with_comma (cxx_pp);
1533 }
573c13ef 1534 pp_cxx_right_paren (cxx_pp);
8998b619 1535 }
471086d6 1536}
1537
1538/* Handle the function name for a FUNCTION_DECL node, grokking operators
1539 and destructors properly. */
96624a9e 1540
471086d6 1541static void
f01d4233 1542dump_function_name (tree t, int flags)
471086d6 1543{
1544 tree name = DECL_NAME (t);
1545
a5a1f3a2 1546 /* We can get here with a decl that was synthesized by language-
1547 independent machinery (e.g. coverage.c) in which case it won't
1548 have a lang_specific structure attached and DECL_CONSTRUCTOR_P
1549 will crash. In this case it is safe just to print out the
1550 literal name. */
1551 if (!DECL_LANG_SPECIFIC (t))
1552 {
573c13ef 1553 pp_cxx_tree_identifier (cxx_pp, name);
a5a1f3a2 1554 return;
1555 }
1556
d5d76adc 1557 if (TREE_CODE (t) == TEMPLATE_DECL)
1558 t = DECL_TEMPLATE_RESULT (t);
1559
3eb89cd8 1560 /* Don't let the user see __comp_ctor et al. */
1561 if (DECL_CONSTRUCTOR_P (t)
1562 || DECL_DESTRUCTOR_P (t))
a8b75081 1563 {
1564 if (LAMBDA_TYPE_P (DECL_CONTEXT (t)))
1565 name = get_identifier ("<lambda>");
7b7a44c9 1566 else if (TYPE_ANONYMOUS_P (DECL_CONTEXT (t)))
1567 name = get_identifier ("<constructor>");
a8b75081 1568 else
1569 name = constructor_name (DECL_CONTEXT (t));
1570 }
3eb89cd8 1571
ff0e1638 1572 if (DECL_DESTRUCTOR_P (t))
471086d6 1573 {
573c13ef 1574 pp_cxx_complement (cxx_pp);
682959c4 1575 dump_decl (name, TFF_PLAIN_IDENTIFIER);
471086d6 1576 }
ff0e1638 1577 else if (DECL_CONV_FN_P (t))
471086d6 1578 {
1579 /* This cannot use the hack that the operator's return
1580 type is stashed off of its name because it may be
1581 used for error reporting. In the case of conflicting
1582 declarations, both will have the same name, yet
1583 the types will be different, hence the TREE_TYPE field
1584 of the first name will be clobbered by the second. */
1e00012f 1585 pp_cxx_ws_string (cxx_pp, "operator");
d04f0501 1586 dump_type (TREE_TYPE (TREE_TYPE (t)), flags);
471086d6 1587 }
39e70cbf 1588 else if (name && IDENTIFIER_OPNAME_P (name))
573c13ef 1589 pp_cxx_tree_identifier (cxx_pp, name);
244db24d 1590 else if (name && UDLIT_OPER_P (name))
1591 pp_cxx_tree_identifier (cxx_pp, name);
471086d6 1592 else
d04f0501 1593 dump_decl (name, flags);
b1cfe2be 1594
a5a1f3a2 1595 if (DECL_TEMPLATE_INFO (t)
ea935d05 1596 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (t)
a761e730 1597 && (TREE_CODE (DECL_TI_TEMPLATE (t)) != TEMPLATE_DECL
34197853 1598 || PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
d04f0501 1599 dump_template_parms (DECL_TEMPLATE_INFO (t), !DECL_USE_TEMPLATE (t), flags);
1600}
668ae905 1601
d04f0501 1602/* Dump the template parameters from the template info INFO under control of
1603 FLAGS. PRIMARY indicates whether this is a primary template decl, or
1604 specialization (partial or complete). For partial specializations we show
1605 the specialized parameter values. For a primary template we show no
1606 decoration. */
1607
1608static void
f01d4233 1609dump_template_parms (tree info, int primary, int flags)
d04f0501 1610{
1611 tree args = info ? TI_ARGS (info) : NULL_TREE;
50cd3f45 1612
682959c4 1613 if (primary && flags & TFF_TEMPLATE_NAME)
d04f0501 1614 return;
682959c4 1615 flags &= ~(TFF_CLASS_KEY_OR_ENUM | TFF_TEMPLATE_NAME);
573c13ef 1616 pp_cxx_begin_template_argument_list (cxx_pp);
d04f0501 1617
1618 /* Be careful only to print things when we have them, so as not
16c89e2f 1619 to crash producing error messages. */
d04f0501 1620 if (args && !primary)
1621 {
b5959ba9 1622 int len, ix;
94c17f03 1623 len = get_non_default_template_args_count (args, flags);
50cd3f45 1624
9e0a6903 1625 args = INNERMOST_TEMPLATE_ARGS (args);
b5959ba9 1626 for (ix = 0; ix != len; ix++)
653e5405 1627 {
1628 tree arg = TREE_VEC_ELT (args, ix);
b5959ba9 1629
d95d815d 1630 /* Only print a comma if we know there is an argument coming. In
1631 the case of an empty template argument pack, no actual
1632 argument will be printed. */
1633 if (ix
1634 && (!ARGUMENT_PACK_P (arg)
1635 || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) > 0))
1636 pp_separate_with_comma (cxx_pp);
1637
1638 if (!arg)
a608187f 1639 pp_string (cxx_pp, M_("<template parameter error>"));
d95d815d 1640 else
1641 dump_template_argument (arg, flags);
1642 }
d04f0501 1643 }
d04f0501 1644 else if (primary)
1645 {
1646 tree tpl = TI_TEMPLATE (info);
1647 tree parms = DECL_TEMPLATE_PARMS (tpl);
1648 int len, ix;
50cd3f45 1649
d04f0501 1650 parms = TREE_CODE (parms) == TREE_LIST ? TREE_VALUE (parms) : NULL_TREE;
1651 len = parms ? TREE_VEC_LENGTH (parms) : 0;
50cd3f45 1652
d04f0501 1653 for (ix = 0; ix != len; ix++)
653e5405 1654 {
6f32df61 1655 tree parm;
1656
1657 if (TREE_VEC_ELT (parms, ix) == error_mark_node)
1658 {
a608187f 1659 pp_string (cxx_pp, M_("<template parameter error>"));
6f32df61 1660 continue;
1661 }
1662
1663 parm = TREE_VALUE (TREE_VEC_ELT (parms, ix));
d04f0501 1664
653e5405 1665 if (ix)
1666 pp_separate_with_comma (cxx_pp);
50cd3f45 1667
653e5405 1668 dump_decl (parm, flags & ~TFF_DECL_SPECIFIERS);
1669 }
b1cfe2be 1670 }
573c13ef 1671 pp_cxx_end_template_argument_list (cxx_pp);
471086d6 1672}
1673
c2f47e15 1674/* Print out the arguments of CALL_EXPR T as a parenthesized list using
1675 flags FLAGS. Skip over the first argument if SKIPFIRST is true. */
1676
1677static void
1678dump_call_expr_args (tree t, int flags, bool skipfirst)
1679{
1680 tree arg;
1681 call_expr_arg_iterator iter;
1682
1683 pp_cxx_left_paren (cxx_pp);
1684 FOR_EACH_CALL_EXPR_ARG (arg, iter, t)
1685 {
1686 if (skipfirst)
1687 skipfirst = false;
1688 else
1689 {
1690 dump_expr (arg, flags | TFF_EXPR_IN_PARENS);
1691 if (more_call_expr_args_p (&iter))
1692 pp_separate_with_comma (cxx_pp);
1693 }
1694 }
1695 pp_cxx_right_paren (cxx_pp);
1696}
1697
1698/* Print out the arguments of AGGR_INIT_EXPR T as a parenthesized list
1699 using flags FLAGS. Skip over the first argument if SKIPFIRST is
1700 true. */
1701
1702static void
1703dump_aggr_init_expr_args (tree t, int flags, bool skipfirst)
1704{
1705 tree arg;
1706 aggr_init_expr_arg_iterator iter;
1707
1708 pp_cxx_left_paren (cxx_pp);
1709 FOR_EACH_AGGR_INIT_EXPR_ARG (arg, iter, t)
1710 {
1711 if (skipfirst)
1712 skipfirst = false;
1713 else
1714 {
1715 dump_expr (arg, flags | TFF_EXPR_IN_PARENS);
1716 if (more_aggr_init_expr_args_p (&iter))
1717 pp_separate_with_comma (cxx_pp);
1718 }
1719 }
1720 pp_cxx_right_paren (cxx_pp);
1721}
1722
331bc0ad 1723/* Print out a list of initializers (subr of dump_expr). */
96624a9e 1724
471086d6 1725static void
f01d4233 1726dump_expr_list (tree l, int flags)
471086d6 1727{
1728 while (l)
1729 {
682959c4 1730 dump_expr (TREE_VALUE (l), flags | TFF_EXPR_IN_PARENS);
471086d6 1731 l = TREE_CHAIN (l);
d04f0501 1732 if (l)
aa6db498 1733 pp_separate_with_comma (cxx_pp);
471086d6 1734 }
1735}
1736
c75b4594 1737/* Print out a vector of initializers (subr of dump_expr). */
1738
1739static void
f1f41a6c 1740dump_expr_init_vec (vec<constructor_elt, va_gc> *v, int flags)
c75b4594 1741{
1742 unsigned HOST_WIDE_INT idx;
1743 tree value;
1744
1745 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
1746 {
1747 dump_expr (value, flags | TFF_EXPR_IN_PARENS);
f1f41a6c 1748 if (idx != v->length () - 1)
c75b4594 1749 pp_separate_with_comma (cxx_pp);
1750 }
1751}
1752
1753
47ee9a82 1754/* We've gotten an indirect REFERENCE (an OBJ_TYPE_REF) to a virtual
1755 function. Resolve it to a close relative -- in the sense of static
1756 type -- variant being overridden. That is close to what was written in
1757 the source code. Subroutine of dump_expr. */
1758
1759static tree
1760resolve_virtual_fun_from_obj_type_ref (tree ref)
1761{
1762 tree obj_type = TREE_TYPE (OBJ_TYPE_REF_OBJECT (ref));
68e4391d 1763 HOST_WIDE_INT index = tree_low_cst (OBJ_TYPE_REF_TOKEN (ref), 1);
47ee9a82 1764 tree fun = BINFO_VIRTUALS (TYPE_BINFO (TREE_TYPE (obj_type)));
68e4391d 1765 while (index)
1766 {
47ee9a82 1767 fun = TREE_CHAIN (fun);
68e4391d 1768 index -= (TARGET_VTABLE_USES_DESCRIPTORS
1769 ? TARGET_VTABLE_USES_DESCRIPTORS : 1);
1770 }
47ee9a82 1771
1772 return BV_FN (fun);
1773}
1774
47cd6605 1775/* Print out an expression E under control of FLAGS. */
96624a9e 1776
471086d6 1777static void
f01d4233 1778dump_expr (tree t, int flags)
471086d6 1779{
dcc587fc 1780 if (t == 0)
1781 return;
9031d10b 1782
3fca6807 1783 if (STATEMENT_CLASS_P (t))
1784 {
a608187f 1785 pp_cxx_ws_string (cxx_pp, M_("<statement>"));
3fca6807 1786 return;
1787 }
1788
471086d6 1789 switch (TREE_CODE (t))
1790 {
1791 case VAR_DECL:
1792 case PARM_DECL:
1793 case FIELD_DECL:
1794 case CONST_DECL:
1795 case FUNCTION_DECL:
dbdf67c6 1796 case TEMPLATE_DECL:
a8adb76d 1797 case NAMESPACE_DECL:
88529a42 1798 case LABEL_DECL:
5b592939 1799 case OVERLOAD:
f91726b4 1800 case TYPE_DECL:
0a3b29ad 1801 case IDENTIFIER_NODE:
673064cf 1802 dump_decl (t, ((flags & ~(TFF_DECL_SPECIFIERS|TFF_RETURN_TYPE
1803 |TFF_TEMPLATE_HEADER))
1804 | TFF_NO_FUNCTION_ARGUMENTS));
471086d6 1805 break;
1806
6aa221fa 1807 case SSA_NAME:
ec11736b 1808 if (SSA_NAME_VAR (t)
1809 && !DECL_ARTIFICIAL (SSA_NAME_VAR (t)))
6aa221fa 1810 dump_expr (SSA_NAME_VAR (t), flags);
1811 else
1812 pp_cxx_ws_string (cxx_pp, M_("<unknown>"));
1813 break;
1814
a9aacc0c 1815 case INTEGER_CST:
471086d6 1816 case REAL_CST:
bb0c6c22 1817 case STRING_CST:
3eae2dbf 1818 case COMPLEX_CST:
bb0c6c22 1819 pp_constant (cxx_pp, t);
471086d6 1820 break;
1821
244db24d 1822 case USERDEF_LITERAL:
1823 pp_cxx_userdef_literal (cxx_pp, t);
1824 break;
1825
891f08be 1826 case THROW_EXPR:
ef8bb1b0 1827 /* While waiting for caret diagnostics, avoid printing
1828 __cxa_allocate_exception, __cxa_throw, and the like. */
a608187f 1829 pp_cxx_ws_string (cxx_pp, M_("<throw-expression>"));
891f08be 1830 break;
1831
0f2952a1 1832 case PTRMEM_CST:
aa6db498 1833 pp_ampersand (cxx_pp);
d04f0501 1834 dump_type (PTRMEM_CST_CLASS (t), flags);
573c13ef 1835 pp_cxx_colon_colon (cxx_pp);
1836 pp_cxx_tree_identifier (cxx_pp, DECL_NAME (PTRMEM_CST_MEMBER (t)));
0f2952a1 1837 break;
1838
471086d6 1839 case COMPOUND_EXPR:
573c13ef 1840 pp_cxx_left_paren (cxx_pp);
68f8f8cc 1841 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1842 pp_separate_with_comma (cxx_pp);
1843 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
573c13ef 1844 pp_cxx_right_paren (cxx_pp);
471086d6 1845 break;
1846
1847 case COND_EXPR:
573c13ef 1848 pp_cxx_left_paren (cxx_pp);
682959c4 1849 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
aa6db498 1850 pp_string (cxx_pp, " ? ");
682959c4 1851 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
aa6db498 1852 pp_string (cxx_pp, " : ");
682959c4 1853 dump_expr (TREE_OPERAND (t, 2), flags | TFF_EXPR_IN_PARENS);
573c13ef 1854 pp_cxx_right_paren (cxx_pp);
471086d6 1855 break;
1856
1857 case SAVE_EXPR:
1858 if (TREE_HAS_CONSTRUCTOR (t))
1859 {
1e00012f 1860 pp_cxx_ws_string (cxx_pp, "new");
653e5405 1861 pp_cxx_whitespace (cxx_pp);
d04f0501 1862 dump_type (TREE_TYPE (TREE_TYPE (t)), flags);
471086d6 1863 }
1864 else
653e5405 1865 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
471086d6 1866 break;
1867
f64b1d68 1868 case AGGR_INIT_EXPR:
a3786328 1869 {
1870 tree fn = NULL_TREE;
50cd3f45 1871
c2f47e15 1872 if (TREE_CODE (AGGR_INIT_EXPR_FN (t)) == ADDR_EXPR)
1873 fn = TREE_OPERAND (AGGR_INIT_EXPR_FN (t), 0);
a3786328 1874
1875 if (fn && TREE_CODE (fn) == FUNCTION_DECL)
1876 {
1877 if (DECL_CONSTRUCTOR_P (fn))
19efe073 1878 dump_type (DECL_CONTEXT (fn), flags);
a3786328 1879 else
1880 dump_decl (fn, 0);
1881 }
1882 else
c2f47e15 1883 dump_expr (AGGR_INIT_EXPR_FN (t), 0);
a3786328 1884 }
1d4336df 1885 dump_aggr_init_expr_args (t, flags, true);
471086d6 1886 break;
1887
1888 case CALL_EXPR:
1889 {
c2f47e15 1890 tree fn = CALL_EXPR_FN (t);
1891 bool skipfirst = false;
50cd3f45 1892
471086d6 1893 if (TREE_CODE (fn) == ADDR_EXPR)
1894 fn = TREE_OPERAND (fn, 0);
1895
074ab442 1896 /* Nobody is interested in seeing the guts of vcalls. */
1897 if (TREE_CODE (fn) == OBJ_TYPE_REF)
1898 fn = resolve_virtual_fun_from_obj_type_ref (fn);
47ee9a82 1899
27d8c1c6 1900 if (TREE_TYPE (fn) != NULL_TREE
1901 && NEXT_CODE (fn) == METHOD_TYPE
1902 && call_expr_nargs (t))
471086d6 1903 {
c2f47e15 1904 tree ob = CALL_EXPR_ARG (t, 0);
471086d6 1905 if (TREE_CODE (ob) == ADDR_EXPR)
1906 {
682959c4 1907 dump_expr (TREE_OPERAND (ob, 0), flags | TFF_EXPR_IN_PARENS);
ab0ab0a6 1908 pp_cxx_dot (cxx_pp);
471086d6 1909 }
1910 else if (TREE_CODE (ob) != PARM_DECL
1911 || strcmp (IDENTIFIER_POINTER (DECL_NAME (ob)), "this"))
1912 {
682959c4 1913 dump_expr (ob, flags | TFF_EXPR_IN_PARENS);
ab0ab0a6 1914 pp_cxx_arrow (cxx_pp);
471086d6 1915 }
c2f47e15 1916 skipfirst = true;
471086d6 1917 }
682959c4 1918 dump_expr (fn, flags | TFF_EXPR_IN_PARENS);
c2f47e15 1919 dump_call_expr_args (t, flags, skipfirst);
471086d6 1920 }
1921 break;
1922
471086d6 1923 case TARGET_EXPR:
1924 /* Note that this only works for G++ target exprs. If somebody
1925 builds a general TARGET_EXPR, there's no way to represent that
1926 it initializes anything other that the parameter slot for the
1927 default argument. Note we may have cleared out the first
1928 operand in expand_expr, so don't go killing ourselves. */
1929 if (TREE_OPERAND (t, 1))
682959c4 1930 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
471086d6 1931 break;
1932
227d73b4 1933 case POINTER_PLUS_EXPR:
1934 dump_binary_op ("+", t, flags);
1935 break;
1936
054a0626 1937 case INIT_EXPR:
471086d6 1938 case MODIFY_EXPR:
4c969cec 1939 dump_binary_op (assignment_operator_name_info[(int)NOP_EXPR].name,
1940 t, flags);
1941 break;
1942
471086d6 1943 case PLUS_EXPR:
1944 case MINUS_EXPR:
1945 case MULT_EXPR:
1946 case TRUNC_DIV_EXPR:
1947 case TRUNC_MOD_EXPR:
1948 case MIN_EXPR:
1949 case MAX_EXPR:
1950 case LSHIFT_EXPR:
1951 case RSHIFT_EXPR:
1952 case BIT_IOR_EXPR:
1953 case BIT_XOR_EXPR:
1954 case BIT_AND_EXPR:
471086d6 1955 case TRUTH_ANDIF_EXPR:
1956 case TRUTH_ORIF_EXPR:
1957 case LT_EXPR:
1958 case LE_EXPR:
1959 case GT_EXPR:
1960 case GE_EXPR:
1961 case EQ_EXPR:
1962 case NE_EXPR:
2d31d4c8 1963 case EXACT_DIV_EXPR:
97cc4539 1964 dump_binary_op (operator_name_info[(int) TREE_CODE (t)].name, t, flags);
471086d6 1965 break;
1966
1967 case CEIL_DIV_EXPR:
1968 case FLOOR_DIV_EXPR:
1969 case ROUND_DIV_EXPR:
7c813b54 1970 case RDIV_EXPR:
d04f0501 1971 dump_binary_op ("/", t, flags);
471086d6 1972 break;
1973
1974 case CEIL_MOD_EXPR:
1975 case FLOOR_MOD_EXPR:
1976 case ROUND_MOD_EXPR:
d04f0501 1977 dump_binary_op ("%", t, flags);
471086d6 1978 break;
1979
1980 case COMPONENT_REF:
1981 {
1982 tree ob = TREE_OPERAND (t, 0);
1983 if (TREE_CODE (ob) == INDIRECT_REF)
1984 {
1985 ob = TREE_OPERAND (ob, 0);
1986 if (TREE_CODE (ob) != PARM_DECL
f242c894 1987 || (DECL_NAME (ob)
1988 && strcmp (IDENTIFIER_POINTER (DECL_NAME (ob)), "this")))
471086d6 1989 {
682959c4 1990 dump_expr (ob, flags | TFF_EXPR_IN_PARENS);
97e439ed 1991 if (TREE_CODE (TREE_TYPE (ob)) == REFERENCE_TYPE)
1992 pp_cxx_dot (cxx_pp);
1993 else
1994 pp_cxx_arrow (cxx_pp);
471086d6 1995 }
1996 }
1997 else
1998 {
682959c4 1999 dump_expr (ob, flags | TFF_EXPR_IN_PARENS);
573c13ef 2000 pp_cxx_dot (cxx_pp);
471086d6 2001 }
682959c4 2002 dump_expr (TREE_OPERAND (t, 1), flags & ~TFF_EXPR_IN_PARENS);
471086d6 2003 }
2004 break;
2005
bea7d742 2006 case ARRAY_REF:
682959c4 2007 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
573c13ef 2008 pp_cxx_left_bracket (cxx_pp);
682959c4 2009 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
573c13ef 2010 pp_cxx_right_bracket (cxx_pp);
bea7d742 2011 break;
2012
97d541d5 2013 case UNARY_PLUS_EXPR:
be9d8f63 2014 dump_unary_op ("+", t, flags);
471086d6 2015 break;
2016
2017 case ADDR_EXPR:
2018 if (TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
bc577f39 2019 || TREE_CODE (TREE_OPERAND (t, 0)) == STRING_CST
2020 /* An ADDR_EXPR can have reference type. In that case, we
2021 shouldn't print the `&' doing so indicates to the user
2022 that the expression has pointer type. */
50cd3f45 2023 || (TREE_TYPE (t)
bc577f39 2024 && TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE))
682959c4 2025 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
88529a42 2026 else if (TREE_CODE (TREE_OPERAND (t, 0)) == LABEL_DECL)
2027 dump_unary_op ("&&", t, flags);
471086d6 2028 else
d04f0501 2029 dump_unary_op ("&", t, flags);
471086d6 2030 break;
2031
2032 case INDIRECT_REF:
2033 if (TREE_HAS_CONSTRUCTOR (t))
2034 {
2035 t = TREE_OPERAND (t, 0);
b4df430b 2036 gcc_assert (TREE_CODE (t) == CALL_EXPR);
c2f47e15 2037 dump_expr (CALL_EXPR_FN (t), flags | TFF_EXPR_IN_PARENS);
2038 dump_call_expr_args (t, flags, true);
471086d6 2039 }
2040 else
2041 {
596c0ae6 2042 if (TREE_OPERAND (t,0) != NULL_TREE
072636f3 2043 && TREE_TYPE (TREE_OPERAND (t, 0))
596c0ae6 2044 && NEXT_CODE (TREE_OPERAND (t, 0)) == REFERENCE_TYPE)
d04f0501 2045 dump_expr (TREE_OPERAND (t, 0), flags);
471086d6 2046 else
d04f0501 2047 dump_unary_op ("*", t, flags);
471086d6 2048 }
2049 break;
2050
b0d55af9 2051 case MEM_REF:
2052 if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
2053 && integer_zerop (TREE_OPERAND (t, 1)))
2054 dump_expr (TREE_OPERAND (TREE_OPERAND (t, 0), 0), flags);
2055 else
2056 {
2057 pp_cxx_star (cxx_pp);
2058 if (!integer_zerop (TREE_OPERAND (t, 1)))
2059 {
2060 pp_cxx_left_paren (cxx_pp);
2061 if (!integer_onep (TYPE_SIZE_UNIT
2062 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0))))))
2063 {
2064 pp_cxx_left_paren (cxx_pp);
2065 dump_type (ptr_type_node, flags);
2066 pp_cxx_right_paren (cxx_pp);
2067 }
2068 }
2069 dump_expr (TREE_OPERAND (t, 0), flags);
2070 if (!integer_zerop (TREE_OPERAND (t, 1)))
2071 {
2072 pp_cxx_ws_string (cxx_pp, "+");
2073 dump_expr (fold_convert (ssizetype, TREE_OPERAND (t, 1)), flags);
2074 pp_cxx_right_paren (cxx_pp);
2075 }
2076 }
2077 break;
2078
471086d6 2079 case NEGATE_EXPR:
2080 case BIT_NOT_EXPR:
2081 case TRUTH_NOT_EXPR:
2082 case PREDECREMENT_EXPR:
2083 case PREINCREMENT_EXPR:
97cc4539 2084 dump_unary_op (operator_name_info [(int)TREE_CODE (t)].name, t, flags);
471086d6 2085 break;
2086
2087 case POSTDECREMENT_EXPR:
2088 case POSTINCREMENT_EXPR:
573c13ef 2089 pp_cxx_left_paren (cxx_pp);
682959c4 2090 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1e00012f 2091 pp_cxx_ws_string (cxx_pp, operator_name_info[(int)TREE_CODE (t)].name);
573c13ef 2092 pp_cxx_right_paren (cxx_pp);
471086d6 2093 break;
2094
2095 case NON_LVALUE_EXPR:
2096 /* FIXME: This is a KLUDGE workaround for a parsing problem. There
2097 should be another level of INDIRECT_REF so that I don't have to do
2098 this. */
596c0ae6 2099 if (TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == POINTER_TYPE)
471086d6 2100 {
2101 tree next = TREE_TYPE (TREE_TYPE (t));
2102
2103 while (TREE_CODE (next) == POINTER_TYPE)
2104 next = TREE_TYPE (next);
50cd3f45 2105
471086d6 2106 if (TREE_CODE (next) == FUNCTION_TYPE)
2107 {
682959c4 2108 if (flags & TFF_EXPR_IN_PARENS)
653e5405 2109 pp_cxx_left_paren (cxx_pp);
573c13ef 2110 pp_cxx_star (cxx_pp);
682959c4 2111 dump_expr (TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2112 if (flags & TFF_EXPR_IN_PARENS)
653e5405 2113 pp_cxx_right_paren (cxx_pp);
471086d6 2114 break;
2115 }
331bc0ad 2116 /* Else fall through. */
471086d6 2117 }
682959c4 2118 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
471086d6 2119 break;
2120
e226f39b 2121 CASE_CONVERT:
91c3ace5 2122 case IMPLICIT_CONV_EXPR:
2d7085a5 2123 case VIEW_CONVERT_EXPR:
aa616daf 2124 {
2125 tree op = TREE_OPERAND (t, 0);
913dab69 2126 tree ttype = TREE_TYPE (t);
2127 tree optype = TREE_TYPE (op);
2128
2129 if (TREE_CODE (ttype) != TREE_CODE (optype)
2130 && POINTER_TYPE_P (ttype)
2131 && POINTER_TYPE_P (optype)
2132 && same_type_p (TREE_TYPE (optype),
2133 TREE_TYPE (ttype)))
2134 {
2135 if (TREE_CODE (ttype) == REFERENCE_TYPE)
2136 dump_unary_op ("*", t, flags);
2137 else
2138 dump_unary_op ("&", t, flags);
2139 }
2140 else if (!same_type_p (TREE_TYPE (op), TREE_TYPE (t)))
aa616daf 2141 {
2142 /* It is a cast, but we cannot tell whether it is a
2143 reinterpret or static cast. Use the C style notation. */
2144 if (flags & TFF_EXPR_IN_PARENS)
573c13ef 2145 pp_cxx_left_paren (cxx_pp);
2146 pp_cxx_left_paren (cxx_pp);
aa616daf 2147 dump_type (TREE_TYPE (t), flags);
573c13ef 2148 pp_cxx_right_paren (cxx_pp);
aa616daf 2149 dump_expr (op, flags | TFF_EXPR_IN_PARENS);
2150 if (flags & TFF_EXPR_IN_PARENS)
573c13ef 2151 pp_cxx_right_paren (cxx_pp);
aa616daf 2152 }
2153 else
2154 dump_expr (op, flags);
2155 break;
2156 }
9031d10b 2157
471086d6 2158 case CONSTRUCTOR:
38281c46 2159 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
2160 {
4ac852cb 2161 tree idx = build_ptrmemfunc_access_expr (t, pfn_identifier);
38281c46 2162
dc9b5a48 2163 if (integer_zerop (idx))
1c16607c 2164 {
2165 /* A NULL pointer-to-member constant. */
653e5405 2166 pp_cxx_left_paren (cxx_pp);
2167 pp_cxx_left_paren (cxx_pp);
d04f0501 2168 dump_type (TREE_TYPE (t), flags);
653e5405 2169 pp_cxx_right_paren (cxx_pp);
2170 pp_character (cxx_pp, '0');
2171 pp_cxx_right_paren (cxx_pp);
1c16607c 2172 break;
2173 }
5d844ba2 2174 else if (host_integerp (idx, 0))
38281c46 2175 {
2176 tree virtuals;
2177 unsigned HOST_WIDE_INT n;
2178
2179 t = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
2180 t = TYPE_METHOD_BASETYPE (t);
2cfde4f3 2181 virtuals = BINFO_VIRTUALS (TYPE_BINFO (TYPE_MAIN_VARIANT (t)));
50cd3f45 2182
606b494c 2183 n = tree_low_cst (idx, 0);
38281c46 2184
2185 /* Map vtable index back one, to allow for the null pointer to
2186 member. */
2187 --n;
2188
2189 while (n > 0 && virtuals)
2190 {
2191 --n;
2192 virtuals = TREE_CHAIN (virtuals);
2193 }
2194 if (virtuals)
2195 {
2b82dde2 2196 dump_expr (BV_FN (virtuals),
653e5405 2197 flags | TFF_EXPR_IN_PARENS);
38281c46 2198 break;
2199 }
2200 }
2201 }
89b7df3a 2202 if (TREE_TYPE (t) && LAMBDA_TYPE_P (TREE_TYPE (t)))
2203 pp_string (cxx_pp, "<lambda closure object>");
c75b4594 2204 if (TREE_TYPE (t) && EMPTY_CONSTRUCTOR_P (t))
92d62798 2205 {
2206 dump_type (TREE_TYPE (t), 0);
653e5405 2207 pp_cxx_left_paren (cxx_pp);
2208 pp_cxx_right_paren (cxx_pp);
92d62798 2209 }
2210 else
2211 {
81d8c61f 2212 if (!BRACE_ENCLOSED_INITIALIZER_P (t))
2213 dump_type (TREE_TYPE (t), 0);
653e5405 2214 pp_cxx_left_brace (cxx_pp);
c75b4594 2215 dump_expr_init_vec (CONSTRUCTOR_ELTS (t), flags);
653e5405 2216 pp_cxx_right_brace (cxx_pp);
92d62798 2217 }
9031d10b 2218
471086d6 2219 break;
2220
ac9386a0 2221 case OFFSET_REF:
2222 {
2223 tree ob = TREE_OPERAND (t, 0);
2917b86f 2224 if (is_dummy_object (ob))
0f2952a1 2225 {
42b9ec6a 2226 t = TREE_OPERAND (t, 1);
2227 if (TREE_CODE (t) == FUNCTION_DECL)
0f2952a1 2228 /* A::f */
682959c4 2229 dump_expr (t, flags | TFF_EXPR_IN_PARENS);
42b9ec6a 2230 else if (BASELINK_P (t))
9031d10b 2231 dump_expr (OVL_CURRENT (BASELINK_FUNCTIONS (t)),
8042247d 2232 flags | TFF_EXPR_IN_PARENS);
0f2952a1 2233 else
d04f0501 2234 dump_decl (t, flags);
0f2952a1 2235 }
ac9386a0 2236 else
2237 {
4b5bcd15 2238 if (TREE_CODE (ob) == INDIRECT_REF)
2239 {
682959c4 2240 dump_expr (TREE_OPERAND (ob, 0), flags | TFF_EXPR_IN_PARENS);
653e5405 2241 pp_cxx_arrow (cxx_pp);
2242 pp_cxx_star (cxx_pp);
4b5bcd15 2243 }
2244 else
2245 {
682959c4 2246 dump_expr (ob, flags | TFF_EXPR_IN_PARENS);
653e5405 2247 pp_cxx_dot (cxx_pp);
2248 pp_cxx_star (cxx_pp);
4b5bcd15 2249 }
682959c4 2250 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
ac9386a0 2251 }
2252 break;
2253 }
2254
f3110581 2255 case TEMPLATE_PARM_INDEX:
682959c4 2256 dump_decl (TEMPLATE_PARM_DECL (t), flags & ~TFF_DECL_SPECIFIERS);
1adc02a5 2257 break;
e857e9c7 2258
e857e9c7 2259 case CAST_EXPR:
1ad432f2 2260 if (TREE_OPERAND (t, 0) == NULL_TREE
2261 || TREE_CHAIN (TREE_OPERAND (t, 0)))
e4ce2dc4 2262 {
d04f0501 2263 dump_type (TREE_TYPE (t), flags);
573c13ef 2264 pp_cxx_left_paren (cxx_pp);
d04f0501 2265 dump_expr_list (TREE_OPERAND (t, 0), flags);
573c13ef 2266 pp_cxx_right_paren (cxx_pp);
e4ce2dc4 2267 }
2268 else
2269 {
573c13ef 2270 pp_cxx_left_paren (cxx_pp);
d04f0501 2271 dump_type (TREE_TYPE (t), flags);
653e5405 2272 pp_cxx_right_paren (cxx_pp);
2273 pp_cxx_left_paren (cxx_pp);
d04f0501 2274 dump_expr_list (TREE_OPERAND (t, 0), flags);
573c13ef 2275 pp_cxx_right_paren (cxx_pp);
e4ce2dc4 2276 }
2277 break;
2278
2a88c9e6 2279 case STATIC_CAST_EXPR:
1e00012f 2280 pp_cxx_ws_string (cxx_pp, "static_cast");
2a88c9e6 2281 goto cast;
2282 case REINTERPRET_CAST_EXPR:
1e00012f 2283 pp_cxx_ws_string (cxx_pp, "reinterpret_cast");
2a88c9e6 2284 goto cast;
2285 case CONST_CAST_EXPR:
1e00012f 2286 pp_cxx_ws_string (cxx_pp, "const_cast");
2a88c9e6 2287 goto cast;
2288 case DYNAMIC_CAST_EXPR:
1e00012f 2289 pp_cxx_ws_string (cxx_pp, "dynamic_cast");
2a88c9e6 2290 cast:
573c13ef 2291 pp_cxx_begin_template_argument_list (cxx_pp);
2a88c9e6 2292 dump_type (TREE_TYPE (t), flags);
573c13ef 2293 pp_cxx_end_template_argument_list (cxx_pp);
2294 pp_cxx_left_paren (cxx_pp);
2a88c9e6 2295 dump_expr (TREE_OPERAND (t, 0), flags);
573c13ef 2296 pp_cxx_right_paren (cxx_pp);
2a88c9e6 2297 break;
2298
c8075df7 2299 case ARROW_EXPR:
d04f0501 2300 dump_expr (TREE_OPERAND (t, 0), flags);
573c13ef 2301 pp_cxx_arrow (cxx_pp);
c8075df7 2302 break;
2303
e4ce2dc4 2304 case SIZEOF_EXPR:
dd53deb4 2305 case ALIGNOF_EXPR:
2306 if (TREE_CODE (t) == SIZEOF_EXPR)
1e00012f 2307 pp_cxx_ws_string (cxx_pp, "sizeof");
50cd3f45 2308 else
dd53deb4 2309 {
b4df430b 2310 gcc_assert (TREE_CODE (t) == ALIGNOF_EXPR);
1e00012f 2311 pp_cxx_ws_string (cxx_pp, "__alignof__");
dd53deb4 2312 }
573c13ef 2313 pp_cxx_whitespace (cxx_pp);
2314 pp_cxx_left_paren (cxx_pp);
121296ee 2315 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
2316 dump_type (TREE_TYPE (TREE_OPERAND (t, 0)), flags);
2317 else if (TYPE_P (TREE_OPERAND (t, 0)))
d04f0501 2318 dump_type (TREE_OPERAND (t, 0), flags);
e4ce2dc4 2319 else
653e5405 2320 dump_expr (TREE_OPERAND (t, 0), flags);
573c13ef 2321 pp_cxx_right_paren (cxx_pp);
e4ce2dc4 2322 break;
e857e9c7 2323
f4babcfa 2324 case AT_ENCODE_EXPR:
2325 pp_cxx_ws_string (cxx_pp, "@encode");
2326 pp_cxx_whitespace (cxx_pp);
2327 pp_cxx_left_paren (cxx_pp);
2328 dump_type (TREE_OPERAND (t, 0), flags);
2329 pp_cxx_right_paren (cxx_pp);
2330 break;
2331
3644efa5 2332 case NOEXCEPT_EXPR:
2333 pp_cxx_ws_string (cxx_pp, "noexcept");
2334 pp_cxx_whitespace (cxx_pp);
2335 pp_cxx_left_paren (cxx_pp);
2336 dump_expr (TREE_OPERAND (t, 0), flags);
2337 pp_cxx_right_paren (cxx_pp);
2338 break;
2339
8ae6c136 2340 case REALPART_EXPR:
2341 case IMAGPART_EXPR:
1e00012f 2342 pp_cxx_ws_string (cxx_pp, operator_name_info[TREE_CODE (t)].name);
573c13ef 2343 pp_cxx_whitespace (cxx_pp);
8ae6c136 2344 dump_expr (TREE_OPERAND (t, 0), flags);
2345 break;
2346
39c8ac16 2347 case DEFAULT_ARG:
a608187f 2348 pp_string (cxx_pp, M_("<unparsed>"));
39c8ac16 2349 break;
2350
a3fc4713 2351 case TRY_CATCH_EXPR:
2352 case WITH_CLEANUP_EXPR:
2353 case CLEANUP_POINT_EXPR:
d04f0501 2354 dump_expr (TREE_OPERAND (t, 0), flags);
a3fc4713 2355 break;
2356
8318ad7a 2357 case PSEUDO_DTOR_EXPR:
d04f0501 2358 dump_expr (TREE_OPERAND (t, 2), flags);
573c13ef 2359 pp_cxx_dot (cxx_pp);
d04f0501 2360 dump_type (TREE_OPERAND (t, 0), flags);
573c13ef 2361 pp_cxx_colon_colon (cxx_pp);
2362 pp_cxx_complement (cxx_pp);
d04f0501 2363 dump_type (TREE_OPERAND (t, 1), flags);
8318ad7a 2364 break;
2365
a0412b00 2366 case TEMPLATE_ID_EXPR:
d04f0501 2367 dump_decl (t, flags);
a0412b00 2368 break;
2369
4cbba981 2370 case BIND_EXPR:
019cf886 2371 case STMT_EXPR:
071dd4a7 2372 case EXPR_STMT:
4cbba981 2373 case STATEMENT_LIST:
019cf886 2374 /* We don't yet have a way of dumping statements in a
2375 human-readable format. */
aa6db498 2376 pp_string (cxx_pp, "({...})");
019cf886 2377 break;
2378
123cc979 2379 case LOOP_EXPR:
aa6db498 2380 pp_string (cxx_pp, "while (1) { ");
682959c4 2381 dump_expr (TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
573c13ef 2382 pp_cxx_right_brace (cxx_pp);
123cc979 2383 break;
2384
2385 case EXIT_EXPR:
aa6db498 2386 pp_string (cxx_pp, "if (");
682959c4 2387 dump_expr (TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
aa6db498 2388 pp_string (cxx_pp, ") break; ");
123cc979 2389 break;
2390
0a3b29ad 2391 case BASELINK:
673064cf 2392 dump_expr (BASELINK_FUNCTIONS (t), flags & ~TFF_EXPR_IN_PARENS);
0a3b29ad 2393 break;
2394
960e38ca 2395 case EMPTY_CLASS_EXPR:
2396 dump_type (TREE_TYPE (t), flags);
573c13ef 2397 pp_cxx_left_paren (cxx_pp);
2398 pp_cxx_right_paren (cxx_pp);
960e38ca 2399 break;
2400
13795292 2401 case NON_DEPENDENT_EXPR:
9b7c46f2 2402 dump_expr (TREE_OPERAND (t, 0), flags);
13795292 2403 break;
6495357a 2404
46147ffe 2405 case ARGUMENT_PACK_SELECT:
2406 dump_template_argument (ARGUMENT_PACK_SELECT_FROM_PACK (t), flags);
2407 break;
2408
d1e981b1 2409 case RECORD_TYPE:
2410 case UNION_TYPE:
2411 case ENUMERAL_TYPE:
2412 case REAL_TYPE:
2413 case VOID_TYPE:
2414 case BOOLEAN_TYPE:
2415 case INTEGER_TYPE:
2416 case COMPLEX_TYPE:
2417 case VECTOR_TYPE:
2418 pp_type_specifier_seq (cxx_pp, t);
2419 break;
2420
2421 case TYPENAME_TYPE:
2422 /* We get here when we want to print a dependent type as an
2423 id-expression, without any disambiguator decoration. */
2424 pp_id_expression (cxx_pp, t);
2425 break;
2426
8d31a12d 2427 case TEMPLATE_TYPE_PARM:
54c12599 2428 case TEMPLATE_TEMPLATE_PARM:
8d31a12d 2429 case BOUND_TEMPLATE_TEMPLATE_PARM:
2430 dump_type (t, flags);
2431 break;
2432
94c6406e 2433 case TRAIT_EXPR:
2434 pp_cxx_trait_expression (cxx_pp, t);
2435 break;
2436
6739b437 2437 case VA_ARG_EXPR:
2438 pp_cxx_va_arg_expression (cxx_pp, t);
2439 break;
2440
c1d5dc55 2441 case OFFSETOF_EXPR:
2442 pp_cxx_offsetof_expression (cxx_pp, t);
2443 break;
2444
7ec8e6a2 2445 case SCOPE_REF:
5bd9bf06 2446 dump_decl (t, flags);
2447 break;
2448
7ec8e6a2 2449 case EXPR_PACK_EXPANSION:
2450 case TYPEID_EXPR:
1167e60f 2451 case MEMBER_REF:
2452 case DOTSTAR_EXPR:
b350f3fc 2453 case NEW_EXPR:
2454 case VEC_NEW_EXPR:
f9a184db 2455 case DELETE_EXPR:
2456 case VEC_DELETE_EXPR:
7ec8e6a2 2457 case MODOP_EXPR:
6da504a8 2458 case ABS_EXPR:
d62c98bc 2459 case CONJ_EXPR:
43fff710 2460 case VECTOR_CST:
d62c98bc 2461 case FIXED_CST:
cc9a8843 2462 case UNORDERED_EXPR:
2463 case ORDERED_EXPR:
2464 case UNLT_EXPR:
2465 case UNLE_EXPR:
2466 case UNGT_EXPR:
2467 case UNGE_EXPR:
2468 case UNEQ_EXPR:
2469 case LTGT_EXPR:
7a4209c5 2470 case COMPLEX_EXPR:
a3efadc7 2471 case BIT_FIELD_REF:
6ce18e5b 2472 case FIX_TRUNC_EXPR:
2473 case FLOAT_EXPR:
7ec8e6a2 2474 pp_expression (cxx_pp, t);
f9a184db 2475 break;
2476
37aea014 2477 case TRUTH_AND_EXPR:
2478 case TRUTH_OR_EXPR:
2479 case TRUTH_XOR_EXPR:
2480 if (flags & TFF_EXPR_IN_PARENS)
2481 pp_cxx_left_paren (cxx_pp);
2482 pp_expression (cxx_pp, t);
2483 if (flags & TFF_EXPR_IN_PARENS)
2484 pp_cxx_right_paren (cxx_pp);
2485 break;
2486
d5a707b4 2487 case OBJ_TYPE_REF:
2488 dump_expr (resolve_virtual_fun_from_obj_type_ref (t), flags);
2489 break;
2490
471086d6 2491 /* This list is incomplete, but should suffice for now.
2492 It is very important that `sorry' does not call
2493 `report_error_function'. That could cause an infinite loop. */
2494 default:
aa6db498 2495 pp_unsupported_tree (cxx_pp, t);
471086d6 2496 /* fall through to ERROR_MARK... */
2497 case ERROR_MARK:
a608187f 2498 pp_string (cxx_pp, M_("<expression error>"));
471086d6 2499 break;
2500 }
2501}
2502
2503static void
f01d4233 2504dump_binary_op (const char *opstring, tree t, int flags)
471086d6 2505{
573c13ef 2506 pp_cxx_left_paren (cxx_pp);
682959c4 2507 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
573c13ef 2508 pp_cxx_whitespace (cxx_pp);
2d31d4c8 2509 if (opstring)
1e00012f 2510 pp_cxx_ws_string (cxx_pp, opstring);
2d31d4c8 2511 else
a608187f 2512 pp_string (cxx_pp, M_("<unknown operator>"));
573c13ef 2513 pp_cxx_whitespace (cxx_pp);
682959c4 2514 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
573c13ef 2515 pp_cxx_right_paren (cxx_pp);
471086d6 2516}
2517
2518static void
f01d4233 2519dump_unary_op (const char *opstring, tree t, int flags)
471086d6 2520{
682959c4 2521 if (flags & TFF_EXPR_IN_PARENS)
573c13ef 2522 pp_cxx_left_paren (cxx_pp);
1e00012f 2523 pp_cxx_ws_string (cxx_pp, opstring);
682959c4 2524 dump_expr (TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2525 if (flags & TFF_EXPR_IN_PARENS)
573c13ef 2526 pp_cxx_right_paren (cxx_pp);
471086d6 2527}
2528
573c13ef 2529static void
2530reinit_cxx_pp (void)
2531{
2532 pp_clear_output_area (cxx_pp);
2533 pp_base (cxx_pp)->padding = pp_none;
2534 pp_indentation (cxx_pp) = 0;
2535 pp_needs_newline (cxx_pp) = false;
5b3aad13 2536 cxx_pp->enclosing_scope = current_function_decl;
573c13ef 2537}
2538
2539
682959c4 2540/* Exported interface to stringifying types, exprs and decls under TFF_*
d04f0501 2541 control. */
8998b619 2542
fe756624 2543const char *
f01d4233 2544type_as_string (tree typ, int flags)
a608187f 2545{
2546 reinit_cxx_pp ();
2547 pp_translate_identifiers (cxx_pp) = false;
2548 dump_type (typ, flags);
2549 return pp_formatted_text (cxx_pp);
2550}
2551
2552const char *
2553type_as_string_translate (tree typ, int flags)
471086d6 2554{
573c13ef 2555 reinit_cxx_pp ();
d04f0501 2556 dump_type (typ, flags);
aa6db498 2557 return pp_formatted_text (cxx_pp);
471086d6 2558}
2559
fe756624 2560const char *
f01d4233 2561expr_as_string (tree decl, int flags)
471086d6 2562{
573c13ef 2563 reinit_cxx_pp ();
a608187f 2564 pp_translate_identifiers (cxx_pp) = false;
d04f0501 2565 dump_expr (decl, flags);
aa6db498 2566 return pp_formatted_text (cxx_pp);
471086d6 2567}
2568
8a47db47 2569/* Wrap decl_as_string with options appropriate for dwarf. */
2570
2571const char *
2572decl_as_dwarf_string (tree decl, int flags)
2573{
2574 const char *name;
2575 /* Curiously, reinit_cxx_pp doesn't reset the flags field, so setting the flag
2576 here will be adequate to get the desired behaviour. */
2577 pp_c_base (cxx_pp)->flags |= pp_c_flag_gnu_v3;
2578 name = decl_as_string (decl, flags);
2579 /* Subsequent calls to the pretty printer shouldn't use this style. */
2580 pp_c_base (cxx_pp)->flags &= ~pp_c_flag_gnu_v3;
2581 return name;
2582}
2583
fe756624 2584const char *
f01d4233 2585decl_as_string (tree decl, int flags)
a608187f 2586{
2587 reinit_cxx_pp ();
2588 pp_translate_identifiers (cxx_pp) = false;
2589 dump_decl (decl, flags);
2590 return pp_formatted_text (cxx_pp);
2591}
2592
2593const char *
2594decl_as_string_translate (tree decl, int flags)
471086d6 2595{
573c13ef 2596 reinit_cxx_pp ();
d04f0501 2597 dump_decl (decl, flags);
aa6db498 2598 return pp_formatted_text (cxx_pp);
471086d6 2599}
2600
8a47db47 2601/* Wrap lang_decl_name with options appropriate for dwarf. */
2602
2603const char *
2604lang_decl_dwarf_name (tree decl, int v, bool translate)
2605{
2606 const char *name;
2607 /* Curiously, reinit_cxx_pp doesn't reset the flags field, so setting the flag
2608 here will be adequate to get the desired behaviour. */
2609 pp_c_base (cxx_pp)->flags |= pp_c_flag_gnu_v3;
2610 name = lang_decl_name (decl, v, translate);
2611 /* Subsequent calls to the pretty printer shouldn't use this style. */
2612 pp_c_base (cxx_pp)->flags &= ~pp_c_flag_gnu_v3;
2613 return name;
2614}
2615
96554925 2616/* Generate the three forms of printable names for cxx_printable_name. */
11066dd5 2617
fe756624 2618const char *
a608187f 2619lang_decl_name (tree decl, int v, bool translate)
11066dd5 2620{
2621 if (v >= 2)
a608187f 2622 return (translate
2623 ? decl_as_string_translate (decl, TFF_DECL_SPECIFIERS)
2624 : decl_as_string (decl, TFF_DECL_SPECIFIERS));
11066dd5 2625
573c13ef 2626 reinit_cxx_pp ();
a608187f 2627 pp_translate_identifiers (cxx_pp) = translate;
6634b72e 2628 if (v == 1
2629 && (DECL_CLASS_SCOPE_P (decl)
2630 || (DECL_NAMESPACE_SCOPE_P (decl)
2631 && CP_DECL_CONTEXT (decl) != global_namespace)))
11066dd5 2632 {
682959c4 2633 dump_type (CP_DECL_CONTEXT (decl), TFF_PLAIN_IDENTIFIER);
573c13ef 2634 pp_cxx_colon_colon (cxx_pp);
11066dd5 2635 }
2636
2637 if (TREE_CODE (decl) == FUNCTION_DECL)
682959c4 2638 dump_function_name (decl, TFF_PLAIN_IDENTIFIER);
8a47db47 2639 else if ((DECL_NAME (decl) == NULL_TREE)
2640 && TREE_CODE (decl) == NAMESPACE_DECL)
ea863de1 2641 dump_decl (decl, TFF_PLAIN_IDENTIFIER | TFF_UNQUALIFIED_NAME);
11066dd5 2642 else
682959c4 2643 dump_decl (DECL_NAME (decl), TFF_PLAIN_IDENTIFIER);
11066dd5 2644
aa6db498 2645 return pp_formatted_text (cxx_pp);
11066dd5 2646}
11066dd5 2647
3cf8b391 2648/* Return the location of a tree passed to %+ formats. */
2649
f91726b4 2650location_t
648a8029 2651location_of (tree t)
471086d6 2652{
ae8eafe0 2653 if (TREE_CODE (t) == PARM_DECL && DECL_CONTEXT (t))
648a8029 2654 t = DECL_CONTEXT (t);
9308e976 2655 else if (TYPE_P (t))
bbd7578e 2656 {
2657 t = TYPE_MAIN_DECL (t);
2658 if (t == NULL_TREE)
2659 return input_location;
2660 }
1eaf178d 2661 else if (TREE_CODE (t) == OVERLOAD)
648a8029 2662 t = OVL_FUNCTION (t);
9031d10b 2663
a1f05651 2664 if (DECL_P (t))
2665 return DECL_SOURCE_LOCATION (t);
2666 return EXPR_LOC_OR_HERE (t);
471086d6 2667}
2668
cf103c6c 2669/* Now the interfaces from error et al to dump_type et al. Each takes an
682959c4 2670 on/off VERBOSE flag and supply the appropriate TFF_ flags to a dump_
d04f0501 2671 function. */
2672
2673static const char *
f01d4233 2674decl_to_string (tree decl, int verbose)
d04f0501 2675{
682959c4 2676 int flags = 0;
32997f84 2677
d04f0501 2678 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == RECORD_TYPE
2679 || TREE_CODE (decl) == UNION_TYPE || TREE_CODE (decl) == ENUMERAL_TYPE)
682959c4 2680 flags = TFF_CLASS_KEY_OR_ENUM;
d04f0501 2681 if (verbose)
a442e18f 2682 flags |= TFF_DECL_SPECIFIERS;
d04f0501 2683 else if (TREE_CODE (decl) == FUNCTION_DECL)
682959c4 2684 flags |= TFF_DECL_SPECIFIERS | TFF_RETURN_TYPE;
2685 flags |= TFF_TEMPLATE_HEADER;
50cd3f45 2686
573c13ef 2687 reinit_cxx_pp ();
d04f0501 2688 dump_decl (decl, flags);
aa6db498 2689 return pp_formatted_text (cxx_pp);
d04f0501 2690}
2691
2692static const char *
240171a9 2693expr_to_string (tree decl)
d04f0501 2694{
573c13ef 2695 reinit_cxx_pp ();
d04f0501 2696 dump_expr (decl, 0);
aa6db498 2697 return pp_formatted_text (cxx_pp);
d04f0501 2698}
2699
2700static const char *
f01d4233 2701fndecl_to_string (tree fndecl, int verbose)
d04f0501 2702{
682959c4 2703 int flags;
50cd3f45 2704
4f7b3f58 2705 flags = TFF_EXCEPTION_SPECIFICATION | TFF_DECL_SPECIFIERS
2706 | TFF_TEMPLATE_HEADER;
d04f0501 2707 if (verbose)
682959c4 2708 flags |= TFF_FUNCTION_DEFAULT_ARGUMENTS;
573c13ef 2709 reinit_cxx_pp ();
d04f0501 2710 dump_decl (fndecl, flags);
aa6db498 2711 return pp_formatted_text (cxx_pp);
d04f0501 2712}
2713
2714
2715static const char *
240171a9 2716code_to_string (enum tree_code c)
471086d6 2717{
2718 return tree_code_name [c];
2719}
2720
f0edcca6 2721const char *
240171a9 2722language_to_string (enum languages c)
471086d6 2723{
2724 switch (c)
2725 {
2726 case lang_c:
2727 return "C";
2728
2729 case lang_cplusplus:
2730 return "C++";
2731
219b5fc8 2732 case lang_java:
2733 return "Java";
2734
471086d6 2735 default:
092b1d6f 2736 gcc_unreachable ();
471086d6 2737 }
d26312d0 2738 return NULL;
471086d6 2739}
2740
2741/* Return the proper printed version of a parameter to a C++ function. */
96624a9e 2742
d04f0501 2743static const char *
240171a9 2744parm_to_string (int p)
471086d6 2745{
573c13ef 2746 reinit_cxx_pp ();
471086d6 2747 if (p < 0)
aa6db498 2748 pp_string (cxx_pp, "'this'");
240171a9 2749 else
aa6db498 2750 pp_decimal_int (cxx_pp, p + 1);
2751 return pp_formatted_text (cxx_pp);
471086d6 2752}
2753
d04f0501 2754static const char *
240171a9 2755op_to_string (enum tree_code p)
471086d6 2756{
aa6db498 2757 tree id = operator_name_info[(int) p].identifier;
a608187f 2758 return id ? IDENTIFIER_POINTER (id) : M_("<unknown>");
471086d6 2759}
2760
d04f0501 2761static const char *
f01d4233 2762type_to_string (tree typ, int verbose)
d04f0501 2763{
aa6db498 2764 int flags = 0;
d04f0501 2765 if (verbose)
682959c4 2766 flags |= TFF_CLASS_KEY_OR_ENUM;
2767 flags |= TFF_TEMPLATE_HEADER;
50cd3f45 2768
573c13ef 2769 reinit_cxx_pp ();
d04f0501 2770 dump_type (typ, flags);
d233cdb5 2771 /* If we're printing a type that involves typedefs, also print the
2772 stripped version. But sometimes the stripped version looks
2773 exactly the same, so we don't want it after all. To avoid printing
2774 it in that case, we play ugly obstack games. */
e43fa5a4 2775 if (typ && TYPE_P (typ) && typ != TYPE_CANONICAL (typ)
2776 && !uses_template_parms (typ))
2777 {
d233cdb5 2778 int aka_start; char *p;
2779 struct obstack *ob = pp_base (cxx_pp)->buffer->obstack;
2780 /* Remember the end of the initial dump. */
2781 int len = obstack_object_size (ob);
e43fa5a4 2782 tree aka = strip_typedefs (typ);
2783 pp_string (cxx_pp, " {aka");
2784 pp_cxx_whitespace (cxx_pp);
d233cdb5 2785 /* And remember the start of the aka dump. */
2786 aka_start = obstack_object_size (ob);
e43fa5a4 2787 dump_type (aka, flags);
2788 pp_character (cxx_pp, '}');
d233cdb5 2789 p = (char*)obstack_base (ob);
2790 /* If they are identical, cut off the aka with a NUL. */
2791 if (memcmp (p, p+aka_start, len) == 0)
2792 p[len] = '\0';
e43fa5a4 2793 }
aa6db498 2794 return pp_formatted_text (cxx_pp);
d04f0501 2795}
2796
2797static const char *
240171a9 2798assop_to_string (enum tree_code p)
1e28ccf1 2799{
aa6db498 2800 tree id = assignment_operator_name_info[(int) p].identifier;
a608187f 2801 return id ? IDENTIFIER_POINTER (id) : M_("{unknown}");
1e28ccf1 2802}
2803
d04f0501 2804static const char *
f01d4233 2805args_to_string (tree p, int verbose)
471086d6 2806{
682959c4 2807 int flags = 0;
d04f0501 2808 if (verbose)
682959c4 2809 flags |= TFF_CLASS_KEY_OR_ENUM;
50cd3f45 2810
471086d6 2811 if (p == NULL_TREE)
ec10e4ad 2812 return "";
471086d6 2813
9308e976 2814 if (TYPE_P (TREE_VALUE (p)))
a608187f 2815 return type_as_string_translate (p, flags);
ec10e4ad 2816
573c13ef 2817 reinit_cxx_pp ();
ec10e4ad 2818 for (; p; p = TREE_CHAIN (p))
2819 {
c4d89d79 2820 if (TREE_VALUE (p) == null_node)
1e00012f 2821 pp_cxx_ws_string (cxx_pp, "NULL");
c4d89d79 2822 else
d04f0501 2823 dump_type (error_type (TREE_VALUE (p)), flags);
ec10e4ad 2824 if (TREE_CHAIN (p))
aa6db498 2825 pp_separate_with_comma (cxx_pp);
ec10e4ad 2826 }
aa6db498 2827 return pp_formatted_text (cxx_pp);
471086d6 2828}
ce28ee2e 2829
8ce59854 2830/* Pretty-print a deduction substitution (from deduction_tsubst_fntype). P
2831 is a TREE_LIST with purpose the TEMPLATE_DECL, value the template
2832 arguments. */
2833
2834static const char *
2835subst_to_string (tree p)
2836{
2837 tree decl = TREE_PURPOSE (p);
2838 tree targs = TREE_VALUE (p);
2839 tree tparms = DECL_TEMPLATE_PARMS (decl);
2840 int flags = TFF_DECL_SPECIFIERS|TFF_TEMPLATE_HEADER;
2841
2842 if (p == NULL_TREE)
2843 return "";
2844
2845 reinit_cxx_pp ();
2846 dump_template_decl (TREE_PURPOSE (p), flags);
2847 pp_cxx_whitespace (cxx_pp);
2848 pp_cxx_left_bracket (cxx_pp);
2849 pp_cxx_ws_string (cxx_pp, M_("with"));
2850 pp_cxx_whitespace (cxx_pp);
2851 dump_template_bindings (tparms, targs, NULL);
2852 pp_cxx_right_bracket (cxx_pp);
2853 return pp_formatted_text (cxx_pp);
2854}
2855
d04f0501 2856static const char *
f01d4233 2857cv_to_string (tree p, int v)
ce28ee2e 2858{
573c13ef 2859 reinit_cxx_pp ();
69cb846f 2860 pp_base (cxx_pp)->padding = v ? pp_before : pp_none;
2861 pp_cxx_cv_qualifier_seq (cxx_pp, p);
aa6db498 2862 return pp_formatted_text (cxx_pp);
ce28ee2e 2863}
b97edf7e 2864
6c7ff025 2865/* Langhook for print_error_function. */
2866void
b8c23db3 2867cxx_print_error_function (diagnostic_context *context, const char *file,
2868 diagnostic_info *diagnostic)
68550df5 2869{
b8c23db3 2870 lhd_print_error_function (context, file, diagnostic);
0de2b732 2871 pp_base_set_prefix (context->printer, file);
25e2ffe1 2872 maybe_print_instantiation_context (context);
68550df5 2873}
2874
b97edf7e 2875static void
f01d4233 2876cp_diagnostic_starter (diagnostic_context *context,
653e5405 2877 diagnostic_info *diagnostic)
b97edf7e 2878{
3de02a0f 2879 diagnostic_report_current_module (context, diagnostic->location);
25e2ffe1 2880 cp_print_error_function (context, diagnostic);
2881 maybe_print_instantiation_context (context);
0a32318d 2882 maybe_print_constexpr_context (context);
2c2efebb 2883 pp_base_set_prefix (context->printer, diagnostic_build_prefix (context,
2884 diagnostic));
b97edf7e 2885}
2886
2887static void
f01d4233 2888cp_diagnostic_finalizer (diagnostic_context *context,
3de02a0f 2889 diagnostic_info *diagnostic)
b97edf7e 2890{
3de02a0f 2891 virt_loc_aware_diagnostic_finalizer (context, diagnostic);
0de2b732 2892 pp_base_destroy_prefix (context->printer);
b97edf7e 2893}
2894
2895/* Print current function onto BUFFER, in the process of reporting
2896 a diagnostic message. Called from cp_diagnostic_starter. */
2897static void
f01d4233 2898cp_print_error_function (diagnostic_context *context,
653e5405 2899 diagnostic_info *diagnostic)
b97edf7e 2900{
5359d2eb 2901 /* If we are in an instantiation context, current_function_decl is likely
2902 to be wrong, so just rely on print_instantiation_full_context. */
2903 if (current_instantiation ())
2904 return;
b8c23db3 2905 if (diagnostic_last_function_changed (context, diagnostic))
b97edf7e 2906 {
aa6db498 2907 const char *old_prefix = context->printer->prefix;
357f7efa 2908 const char *file = LOCATION_FILE (diagnostic->location);
ce084dfc 2909 tree abstract_origin = diagnostic_abstract_origin (diagnostic);
b8c23db3 2910 char *new_prefix = (file && abstract_origin == NULL)
2911 ? file_name_as_prefix (file) : NULL;
b97edf7e 2912
0de2b732 2913 pp_base_set_prefix (context->printer, new_prefix);
50cd3f45 2914
b97edf7e 2915 if (current_function_decl == NULL)
1e00012f 2916 pp_base_string (context->printer, _("At global scope:"));
b97edf7e 2917 else
b8c23db3 2918 {
2919 tree fndecl, ao;
2920
2921 if (abstract_origin)
2922 {
2923 ao = BLOCK_ABSTRACT_ORIGIN (abstract_origin);
c112531f 2924 while (TREE_CODE (ao) == BLOCK
2925 && BLOCK_ABSTRACT_ORIGIN (ao)
2926 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
b8c23db3 2927 ao = BLOCK_ABSTRACT_ORIGIN (ao);
2928 gcc_assert (TREE_CODE (ao) == FUNCTION_DECL);
2929 fndecl = ao;
2930 }
2931 else
2932 fndecl = current_function_decl;
2933
1e00012f 2934 pp_printf (context->printer, function_category (fndecl),
a608187f 2935 cxx_printable_name_translate (fndecl, 2));
b8c23db3 2936
2937 while (abstract_origin)
2938 {
2939 location_t *locus;
2940 tree block = abstract_origin;
2941
2942 locus = &BLOCK_SOURCE_LOCATION (block);
2943 fndecl = NULL;
2944 block = BLOCK_SUPERCONTEXT (block);
2945 while (block && TREE_CODE (block) == BLOCK
2946 && BLOCK_ABSTRACT_ORIGIN (block))
2947 {
2948 ao = BLOCK_ABSTRACT_ORIGIN (block);
2949
c89421d8 2950 while (TREE_CODE (ao) == BLOCK
2951 && BLOCK_ABSTRACT_ORIGIN (ao)
2952 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
b8c23db3 2953 ao = BLOCK_ABSTRACT_ORIGIN (ao);
2954
2955 if (TREE_CODE (ao) == FUNCTION_DECL)
2956 {
2957 fndecl = ao;
2958 break;
2959 }
2960 else if (TREE_CODE (ao) != BLOCK)
2961 break;
2962
2963 block = BLOCK_SUPERCONTEXT (block);
2964 }
2965 if (fndecl)
2966 abstract_origin = block;
2967 else
2968 {
2969 while (block && TREE_CODE (block) == BLOCK)
2970 block = BLOCK_SUPERCONTEXT (block);
2971
abc9203d 2972 if (block && TREE_CODE (block) == FUNCTION_DECL)
b8c23db3 2973 fndecl = block;
2974 abstract_origin = NULL;
2975 }
2976 if (fndecl)
2977 {
2978 expanded_location s = expand_location (*locus);
2979 pp_base_character (context->printer, ',');
2980 pp_base_newline (context->printer);
2981 if (s.file != NULL)
2982 {
2c2efebb 2983 if (context->show_column && s.column != 0)
b8c23db3 2984 pp_printf (context->printer,
1e00012f 2985 _(" inlined from %qs at %s:%d:%d"),
a608187f 2986 cxx_printable_name_translate (fndecl, 2),
b8c23db3 2987 s.file, s.line, s.column);
2988 else
b8c23db3 2989 pp_printf (context->printer,
1e00012f 2990 _(" inlined from %qs at %s:%d"),
a608187f 2991 cxx_printable_name_translate (fndecl, 2),
b8c23db3 2992 s.file, s.line);
2993
2994 }
2995 else
1e00012f 2996 pp_printf (context->printer, _(" inlined from %qs"),
a608187f 2997 cxx_printable_name_translate (fndecl, 2));
b8c23db3 2998 }
2999 }
3000 pp_base_character (context->printer, ':');
3001 }
0de2b732 3002 pp_base_newline (context->printer);
25e2ffe1 3003
b8c23db3 3004 diagnostic_set_last_function (context, diagnostic);
0de2b732 3005 pp_base_destroy_prefix (context->printer);
aa6db498 3006 context->printer->prefix = old_prefix;
b97edf7e 3007 }
3008}
3009
1e00012f 3010/* Returns a description of FUNCTION using standard terminology. The
3011 result is a format string of the form "In CATEGORY %qs". */
b97edf7e 3012static const char *
f01d4233 3013function_category (tree fn)
b97edf7e 3014{
bfec3452 3015 /* We can get called from the middle-end for diagnostics of function
3016 clones. Make sure we have language specific information before
3017 dereferencing it. */
3018 if (DECL_LANG_SPECIFIC (STRIP_TEMPLATE (fn))
3019 && DECL_FUNCTION_MEMBER_P (fn))
b97edf7e 3020 {
3021 if (DECL_STATIC_FUNCTION_P (fn))
1e00012f 3022 return _("In static member function %qs");
b97edf7e 3023 else if (DECL_COPY_CONSTRUCTOR_P (fn))
1e00012f 3024 return _("In copy constructor %qs");
b97edf7e 3025 else if (DECL_CONSTRUCTOR_P (fn))
1e00012f 3026 return _("In constructor %qs");
b97edf7e 3027 else if (DECL_DESTRUCTOR_P (fn))
1e00012f 3028 return _("In destructor %qs");
a8b75081 3029 else if (LAMBDA_FUNCTION_P (fn))
3030 return _("In lambda function");
b97edf7e 3031 else
1e00012f 3032 return _("In member function %qs");
b97edf7e 3033 }
3034 else
1e00012f 3035 return _("In function %qs");
b97edf7e 3036}
3037
3038/* Report the full context of a current template instantiation,
3039 onto BUFFER. */
3040static void
f01d4233 3041print_instantiation_full_context (diagnostic_context *context)
b97edf7e 3042{
c5dd8e06 3043 struct tinst_level *p = current_instantiation ();
6a86d77b 3044 location_t location = input_location;
9031d10b 3045
b97edf7e 3046 if (p)
3047 {
5359d2eb 3048 pp_verbatim (context->printer,
3049 TREE_CODE (p->decl) == TREE_LIST
3050 ? _("%s: In substitution of %qS:\n")
3051 : _("%s: In instantiation of %q#D:\n"),
3052 LOCATION_FILE (location),
3053 p->decl);
3054
3055 location = p->locus;
3056 p = p->next;
b97edf7e 3057 }
50cd3f45 3058
f96e4fb1 3059 print_instantiation_partial_context (context, p, location);
b97edf7e 3060}
3061
24f8939e 3062/* Helper function of print_instantiation_partial_context() that
3063 prints a single line of instantiation context. */
3064
b97edf7e 3065static void
24f8939e 3066print_instantiation_partial_context_line (diagnostic_context *context,
fa494501 3067 const struct tinst_level *t,
3068 location_t loc, bool recursive_p)
b97edf7e 3069{
357f7efa 3070 expanded_location xloc;
24f8939e 3071 xloc = expand_location (loc);
3072
8ce59854 3073 if (context->show_column)
3074 pp_verbatim (context->printer, _("%s:%d:%d: "),
3075 xloc.file, xloc.line, xloc.column);
3076 else
3077 pp_verbatim (context->printer, _("%s:%d: "),
3078 xloc.file, xloc.line);
3079
3080 if (t != NULL)
fa494501 3081 {
8ce59854 3082 if (TREE_CODE (t->decl) == TREE_LIST)
fa494501 3083 pp_verbatim (context->printer,
3084 recursive_p
8ce59854 3085 ? _("recursively required by substitution of %qS\n")
3086 : _("required by substitution of %qS\n"),
3087 t->decl);
fa494501 3088 else
3089 pp_verbatim (context->printer,
3090 recursive_p
8ce59854 3091 ? _("recursively required from %q#D\n")
3092 : _("required from %q#D\n"),
3093 t->decl);
fa494501 3094 }
3095 else
3096 {
8ce59854 3097 pp_verbatim (context->printer,
3098 recursive_p
3099 ? _("recursively required from here")
3100 : _("required from here"));
fa494501 3101 }
24f8939e 3102}
3103
3104/* Same as print_instantiation_full_context but less verbose. */
3105
3106static void
3107print_instantiation_partial_context (diagnostic_context *context,
3108 struct tinst_level *t0, location_t loc)
3109{
3110 struct tinst_level *t;
3111 int n_total = 0;
3112 int n;
fa494501 3113 location_t prev_loc = loc;
24f8939e 3114
3115 for (t = t0; t != NULL; t = t->next)
fa494501 3116 if (prev_loc != t->locus)
3117 {
3118 prev_loc = t->locus;
3119 n_total++;
3120 }
24f8939e 3121
3122 t = t0;
3123
249faa35 3124 if (template_backtrace_limit
3125 && n_total > template_backtrace_limit)
b97edf7e 3126 {
249faa35 3127 int skip = n_total - template_backtrace_limit;
3128 int head = template_backtrace_limit / 2;
3129
3130 /* Avoid skipping just 1. If so, skip 2. */
3131 if (skip == 1)
3132 {
3133 skip = 2;
3134 head = (template_backtrace_limit - 1) / 2;
3135 }
3136
3137 for (n = 0; n < head; n++)
24f8939e 3138 {
3139 gcc_assert (t != NULL);
fa494501 3140 if (loc != t->locus)
3141 print_instantiation_partial_context_line (context, t, loc,
3142 /*recursive_p=*/false);
24f8939e 3143 loc = t->locus;
3144 t = t->next;
3145 }
249faa35 3146 if (t != NULL && skip > 0)
24f8939e 3147 {
3148 expanded_location xloc;
3149 xloc = expand_location (loc);
2c2efebb 3150 if (context->show_column)
24f8939e 3151 pp_verbatim (context->printer,
249faa35 3152 _("%s:%d:%d: [ skipping %d instantiation contexts, "
3153 "use -ftemplate-backtrace-limit=0 to disable ]\n"),
24f8939e 3154 xloc.file, xloc.line, xloc.column, skip);
3155 else
3156 pp_verbatim (context->printer,
249faa35 3157 _("%s:%d: [ skipping %d instantiation contexts, "
3158 "use -ftemplate-backtrace-limit=0 to disable ]\n"),
24f8939e 3159 xloc.file, xloc.line, skip);
3160
3161 do {
fa494501 3162 loc = t->locus;
3163 t = t->next;
3164 } while (t != NULL && --skip > 0);
24f8939e 3165 }
3166 }
3167
fa494501 3168 while (t != NULL)
24f8939e 3169 {
fa494501 3170 while (t->next != NULL && t->locus == t->next->locus)
3171 {
3172 loc = t->locus;
3173 t = t->next;
3174 }
3175 print_instantiation_partial_context_line (context, t, loc,
3176 t->locus == loc);
c5dd8e06 3177 loc = t->locus;
fa494501 3178 t = t->next;
b97edf7e 3179 }
fa494501 3180 print_instantiation_partial_context_line (context, NULL, loc,
3181 /*recursive_p=*/false);
c907c5b1 3182 pp_base_newline (context->printer);
b97edf7e 3183}
3184
3185/* Called from cp_thing to print the template context for an error. */
3186static void
f01d4233 3187maybe_print_instantiation_context (diagnostic_context *context)
b97edf7e 3188{
3189 if (!problematic_instantiation_changed () || current_instantiation () == 0)
3190 return;
3191
3192 record_last_problematic_instantiation ();
25e2ffe1 3193 print_instantiation_full_context (context);
b97edf7e 3194}
3195
3196/* Report the bare minimum context of a template instantiation. */
3197void
f01d4233 3198print_instantiation_context (void)
b97edf7e 3199{
3200 print_instantiation_partial_context
f96e4fb1 3201 (global_dc, current_instantiation (), input_location);
5f7f600e 3202 pp_base_newline (global_dc->printer);
25e2ffe1 3203 diagnostic_flush_buffer (global_dc);
b97edf7e 3204}
326f388b 3205\f
0a32318d 3206/* Report what constexpr call(s) we're trying to expand, if any. */
3207
3208void
3209maybe_print_constexpr_context (diagnostic_context *context)
3210{
f1f41a6c 3211 vec<tree> call_stack = cx_error_context ();
0a32318d 3212 unsigned ix;
3213 tree t;
3214
f1f41a6c 3215 FOR_EACH_VEC_ELT (call_stack, ix, t)
0a32318d 3216 {
3217 expanded_location xloc = expand_location (EXPR_LOCATION (t));
3218 const char *s = expr_as_string (t, 0);
3219 if (context->show_column)
3220 pp_verbatim (context->printer,
3221 _("%s:%d:%d: in constexpr expansion of %qs"),
3222 xloc.file, xloc.line, xloc.column, s);
3223 else
3224 pp_verbatim (context->printer,
3225 _("%s:%d: in constexpr expansion of %qs"),
3226 xloc.file, xloc.line, s);
3227 pp_base_newline (context->printer);
3228 }
3229}
3230\f
326f388b 3231/* Called from output_format -- during diagnostic message processing --
3232 to handle C++ specific format specifier with the following meanings:
3233 %A function argument-list.
5124a2c8 3234 %C tree code.
326f388b 3235 %D declaration.
3236 %E expression.
3237 %F function declaration.
5124a2c8 3238 %L language as used in extern "lang".
3239 %O binary operator.
326f388b 3240 %P function parameter whose position is indicated by an integer.
5124a2c8 3241 %Q assignment operator.
326f388b 3242 %T type.
3243 %V cv-qualifier. */
25e2ffe1 3244static bool
c907c5b1 3245cp_printer (pretty_printer *pp, text_info *text, const char *spec,
3246 int precision, bool wide, bool set_locus, bool verbose)
326f388b 3247{
5124a2c8 3248 const char *result;
c907c5b1 3249 tree t = NULL;
3250#define next_tree (t = va_arg (*text->args_ptr, tree))
d62e827b 3251#define next_tcode ((enum tree_code) va_arg (*text->args_ptr, int))
3252#define next_lang ((enum languages) va_arg (*text->args_ptr, int))
25e2ffe1 3253#define next_int va_arg (*text->args_ptr, int)
3254
c907c5b1 3255 if (precision != 0 || wide)
3256 return false;
3257
3258 if (text->locus == NULL)
3259 set_locus = false;
326f388b 3260
c907c5b1 3261 switch (*spec)
4b63bfb9 3262 {
5124a2c8 3263 case 'A': result = args_to_string (next_tree, verbose); break;
653e5405 3264 case 'C': result = code_to_string (next_tcode); break;
2947608c 3265 case 'D':
3266 {
3267 tree temp = next_tree;
3268 if (DECL_P (temp)
3269 && DECL_DEBUG_EXPR_IS_FROM (temp) && DECL_DEBUG_EXPR (temp))
3270 {
3271 temp = DECL_DEBUG_EXPR (temp);
3272 if (!DECL_P (temp))
3273 {
3274 result = expr_to_string (temp);
3275 break;
3276 }
3277 }
3278 result = decl_to_string (temp, verbose);
3279 }
3280 break;
653e5405 3281 case 'E': result = expr_to_string (next_tree); break;
5124a2c8 3282 case 'F': result = fndecl_to_string (next_tree, verbose); break;
653e5405 3283 case 'L': result = language_to_string (next_lang); break;
3284 case 'O': result = op_to_string (next_tcode); break;
3285 case 'P': result = parm_to_string (next_int); break;
3286 case 'Q': result = assop_to_string (next_tcode); break;
8ce59854 3287 case 'S': result = subst_to_string (next_tree); break;
5124a2c8 3288 case 'T': result = type_to_string (next_tree, verbose); break;
3289 case 'V': result = cv_to_string (next_tree, verbose); break;
9031d10b 3290
ce084dfc 3291 case 'K':
3292 percent_K_format (text);
3293 return true;
3294
4b63bfb9 3295 default:
25e2ffe1 3296 return false;
326f388b 3297 }
50cd3f45 3298
0de2b732 3299 pp_base_string (pp, result);
c907c5b1 3300 if (set_locus && t != NULL)
3301 *text->locus = location_of (t);
25e2ffe1 3302 return true;
5124a2c8 3303#undef next_tree
3304#undef next_tcode
3305#undef next_lang
3306#undef next_int
326f388b 3307}
eb0d20b7 3308\f
f82f1250 3309/* Warn about the use of C++0x features when appropriate. */
d95d815d 3310void
bf8d19fe 3311maybe_warn_cpp0x (cpp0x_warn_str str)
d95d815d 3312{
6dcdb5de 3313 if ((cxx_dialect == cxx98) && !in_system_header)
1034b0d6 3314 /* We really want to suppress this warning in system headers,
d95d815d 3315 because libstdc++ uses variadic templates even when we aren't
3316 in C++0x mode. */
bf8d19fe 3317 switch (str)
3318 {
3319 case CPP0X_INITIALIZER_LISTS:
3320 pedwarn (input_location, 0,
3321 "extended initializer lists "
0d84dc2d 3322 "only available with -std=c++11 or -std=gnu++11");
bf8d19fe 3323 break;
3324 case CPP0X_EXPLICIT_CONVERSION:
3325 pedwarn (input_location, 0,
3326 "explicit conversion operators "
0d84dc2d 3327 "only available with -std=c++11 or -std=gnu++11");
bf8d19fe 3328 break;
3329 case CPP0X_VARIADIC_TEMPLATES:
3330 pedwarn (input_location, 0,
3331 "variadic templates "
0d84dc2d 3332 "only available with -std=c++11 or -std=gnu++11");
bf8d19fe 3333 break;
3334 case CPP0X_LAMBDA_EXPR:
3335 pedwarn (input_location, 0,
3336 "lambda expressions "
0d84dc2d 3337 "only available with -std=c++11 or -std=gnu++11");
bf8d19fe 3338 break;
3339 case CPP0X_AUTO:
3340 pedwarn (input_location, 0,
0d84dc2d 3341 "C++0x auto only available with -std=c++11 or -std=gnu++11");
bf8d19fe 3342 break;
3343 case CPP0X_SCOPED_ENUMS:
3344 pedwarn (input_location, 0,
0d84dc2d 3345 "scoped enums only available with -std=c++11 or -std=gnu++11");
bf8d19fe 3346 break;
3347 case CPP0X_DEFAULTED_DELETED:
3348 pedwarn (input_location, 0,
3349 "defaulted and deleted functions "
0d84dc2d 3350 "only available with -std=c++11 or -std=gnu++11");
af28195d 3351 break;
3352 case CPP0X_INLINE_NAMESPACES:
29438999 3353 pedwarn (input_location, OPT_Wpedantic,
af28195d 3354 "inline namespaces "
0d84dc2d 3355 "only available with -std=c++11 or -std=gnu++11");
244db24d 3356 break;
dcdaa0e3 3357 case CPP0X_OVERRIDE_CONTROLS:
3358 pedwarn (input_location, 0,
3359 "override controls (override/final) "
0d84dc2d 3360 "only available with -std=c++11 or -std=gnu++11");
dcdaa0e3 3361 break;
d9c249a4 3362 case CPP0X_NSDMI:
3363 pedwarn (input_location, 0,
3364 "non-static data member initializers "
0d84dc2d 3365 "only available with -std=c++11 or -std=gnu++11");
d9c249a4 3366 break;
244db24d 3367 case CPP0X_USER_DEFINED_LITERALS:
3368 pedwarn (input_location, 0,
3369 "user-defined literals "
0d84dc2d 3370 "only available with -std=c++11 or -std=gnu++11");
244db24d 3371 break;
90510c63 3372 case CPP0X_DELEGATING_CTORS:
3373 pedwarn (input_location, 0,
3374 "delegating constructors "
3375 "only available with -std=c++11 or -std=gnu++11");
fa6e8832 3376 break;
3377 case CPP0X_INHERITING_CTORS:
3378 pedwarn (input_location, 0,
3379 "inheriting constructors "
3380 "only available with -std=c++11 or -std=gnu++11");
90510c63 3381 break;
ffcdbf9c 3382 case CPP0X_ATTRIBUTES:
3383 pedwarn (input_location, 0,
3384 "c++11 attributes "
3385 "only available with -std=c++11 or -std=gnu++11");
3386 break;
bf8d19fe 3387 default:
244db24d 3388 gcc_unreachable ();
bf8d19fe 3389 }
f82f1250 3390}
3391
3392/* Warn about the use of variadic templates when appropriate. */
3393void
3394maybe_warn_variadic_templates (void)
3395{
bf8d19fe 3396 maybe_warn_cpp0x (CPP0X_VARIADIC_TEMPLATES);
d95d815d 3397}
9ab71c6b 3398
3399
3400/* Issue an ISO C++98 pedantic warning at LOCATION, conditional on
3401 option OPT with text GMSGID. Use this function to report
3402 diagnostics for constructs that are invalid C++98, but valid
3403 C++0x. */
3404bool
3405pedwarn_cxx98 (location_t location, int opt, const char *gmsgid, ...)
3406{
3407 diagnostic_info diagnostic;
3408 va_list ap;
b666516d 3409 bool ret;
9ab71c6b 3410
3411 va_start (ap, gmsgid);
3412 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
3413 (cxx_dialect == cxx98) ? DK_PEDWARN : DK_WARNING);
3414 diagnostic.option_index = opt;
b666516d 3415 ret = report_diagnostic (&diagnostic);
9ab71c6b 3416 va_end (ap);
b666516d 3417 return ret;
9ab71c6b 3418}
d3c44c27 3419
3420/* Issue a diagnostic that NAME cannot be found in SCOPE. DECL is what
3421 we found when we tried to do the lookup. LOCATION is the location of
3422 the NAME identifier. */
3423
3424void
3425qualified_name_lookup_error (tree scope, tree name,
3426 tree decl, location_t location)
3427{
3428 if (scope == error_mark_node)
3429 ; /* We already complained. */
3430 else if (TYPE_P (scope))
3431 {
3432 if (!COMPLETE_TYPE_P (scope))
3433 error_at (location, "incomplete type %qT used in nested name specifier",
3434 scope);
3435 else if (TREE_CODE (decl) == TREE_LIST)
3436 {
3437 error_at (location, "reference to %<%T::%D%> is ambiguous",
3438 scope, name);
3439 print_candidates (decl);
3440 }
3441 else
3442 error_at (location, "%qD is not a member of %qT", name, scope);
3443 }
3444 else if (scope != global_namespace)
3445 {
3446 error_at (location, "%qD is not a member of %qD", name, scope);
3447 suggest_alternatives_for (location, name);
3448 }
3449 else
3450 {
3451 error_at (location, "%<::%D%> has not been declared", name);
3452 suggest_alternatives_for (location, name);
3453 }
3454}