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