OSDN Git Service

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