OSDN Git Service

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