OSDN Git Service

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