OSDN Git Service

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