OSDN Git Service

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