OSDN Git Service

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