Remove old ABI support.
[official-gcc.git] / gcc / cp / error.c
blob30912362f08d0f360165f5f7398eaafaa9a6a2f3
1 /* Call-backs for C++ error reporting.
2 This code is non-reentrant.
3 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000
4 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC 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.
12 GNU CC 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.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "tree.h"
25 #include "cp-tree.h"
26 #include "obstack.h"
27 #include "toplev.h"
28 #include "diagnostic.h"
30 enum pad { none, before, after };
32 /* This data structure bundles altogether, all the information necessary
33 for pretty-printing a C++ source-level entity represented by a tree. */
34 typedef struct
36 tree decl;
37 int flags;
38 enum pad pad;
39 } tree_formatting_info, *tfi_t;
41 #define tree_being_formatted(TFI) (TFI)->decl
42 #define tree_formatting_flags(TFI) (TFI)->flags
43 #define put_whitespace(TFI) (TFI)->pad
45 #define sorry_for_unsupported_tree(T) \
46 sorry ("`%s' not supported by %s", tree_code_name[(int) TREE_CODE (T)], \
47 __FUNCTION__)
49 #define print_scope_operator(BUFFER) output_add_string (BUFFER, "::")
50 #define print_left_paren(BUFFER) output_add_character (BUFFER, '(')
51 #define print_right_paren(BUFFER) output_add_character (BUFFER, ')')
52 #define print_left_bracket(BUFFER) output_add_character (BUFFER, '[')
53 #define print_right_bracket(BUFFER) output_add_character (BUFFER, ']')
54 #define print_template_argument_list_start(BUFFER) \
55 print_non_consecutive_character (BUFFER, '<')
56 #define print_template_argument_list_end(BUFFER) \
57 print_non_consecutive_character (BUFFER, '>')
58 #define print_whitespace(BUFFER, TFI) \
59 do { \
60 output_add_space (BUFFER); \
61 put_whitespace (TFI) = none; \
62 } while (0)
63 #define print_tree_identifier(BUFFER, TID) \
64 output_add_string (BUFFER, IDENTIFIER_POINTER (TID))
65 #define print_identifier(BUFFER, ID) output_add_string (BUFFER, ID)
66 #define separate_with_comma(BUFFER) output_add_string (BUFFER, ", ")
68 /* The global buffer where we dump everything. It is there only for
69 transitional purpose. It is expected, in the near future, to be
70 completely removed. */
71 static output_buffer scratch_buffer_rec;
72 static output_buffer *scratch_buffer = &scratch_buffer_rec;
74 # define NEXT_CODE(t) (TREE_CODE (TREE_TYPE (t)))
76 #define reinit_global_formatting_buffer() \
77 output_clear_message_text (scratch_buffer)
79 static const char *args_to_string PARAMS ((tree, int));
80 static const char *assop_to_string PARAMS ((enum tree_code, int));
81 static const char *code_to_string PARAMS ((enum tree_code, int));
82 static const char *cv_to_string PARAMS ((tree, int));
83 static const char *decl_to_string PARAMS ((tree, int));
84 static const char *expr_to_string PARAMS ((tree, int));
85 static const char *fndecl_to_string PARAMS ((tree, int));
86 static const char *op_to_string PARAMS ((enum tree_code, int));
87 static const char *parm_to_string PARAMS ((int, int));
88 static const char *type_to_string PARAMS ((tree, int));
90 static void dump_type PARAMS ((tree, int));
91 static void dump_typename PARAMS ((tree, int));
92 static void dump_simple_decl PARAMS ((tree, tree, int));
93 static void dump_decl PARAMS ((tree, int));
94 static void dump_template_decl PARAMS ((tree, int));
95 static void dump_function_decl PARAMS ((tree, int));
96 static void dump_expr PARAMS ((tree, int));
97 static void dump_unary_op PARAMS ((const char *, tree, int));
98 static void dump_binary_op PARAMS ((const char *, tree, int));
99 static void dump_aggr_type PARAMS ((tree, int));
100 static enum pad dump_type_prefix PARAMS ((tree, int));
101 static void dump_type_suffix PARAMS ((tree, int));
102 static void dump_function_name PARAMS ((tree, int));
103 static void dump_expr_list PARAMS ((tree, int));
104 static void dump_global_iord PARAMS ((tree));
105 static enum pad dump_qualifiers PARAMS ((tree, enum pad));
106 static void dump_char PARAMS ((int));
107 static void dump_parameters PARAMS ((tree, int));
108 static void dump_exception_spec PARAMS ((tree, int));
109 static const char *class_key_or_enum PARAMS ((tree));
110 static tree ident_fndecl PARAMS ((tree));
111 static void dump_template_argument PARAMS ((tree, int));
112 static void dump_template_argument_list PARAMS ((tree, int));
113 static void dump_template_parameter PARAMS ((tree, int));
114 static void dump_template_bindings PARAMS ((tree, tree));
115 static void dump_scope PARAMS ((tree, int));
116 static void dump_template_parms PARAMS ((tree, int, int));
118 static const char *function_category PARAMS ((tree));
119 static void lang_print_error_function PARAMS ((const char *));
120 static void maybe_print_instantiation_context PARAMS ((output_buffer *));
121 static void print_instantiation_full_context PARAMS ((output_buffer *));
122 static void print_instantiation_partial_context PARAMS ((output_buffer *, tree,
123 const char *, int));
124 static void cp_diagnostic_starter PARAMS ((output_buffer *,
125 diagnostic_context *));
126 static void cp_diagnostic_finalizer PARAMS ((output_buffer *,
127 diagnostic_context *));
128 static void cp_print_error_function PARAMS ((output_buffer *,
129 diagnostic_context *));
131 static int cp_tree_printer PARAMS ((output_buffer *));
132 static void print_function_argument_list PARAMS ((output_buffer *, tfi_t));
133 static void print_declaration PARAMS ((output_buffer *, tfi_t));
134 static void print_expression PARAMS ((output_buffer *, tfi_t));
135 static void print_integer PARAMS ((output_buffer *, HOST_WIDE_INT));
136 static void print_function_declaration PARAMS ((output_buffer *, tfi_t));
137 static void print_function_parameter PARAMS ((output_buffer *, int));
138 static void print_type_id PARAMS ((output_buffer *, tfi_t));
139 static void print_cv_qualifier_seq PARAMS ((output_buffer *, tfi_t));
140 static void print_type_specifier_seq PARAMS ((output_buffer *, tfi_t));
141 static void print_simple_type_specifier PARAMS ((output_buffer *, tfi_t));
142 static void print_elaborated_type_specifier PARAMS ((output_buffer *, tfi_t));
143 static void print_rest_of_abstract_declarator PARAMS ((output_buffer *,
144 tfi_t));
145 static void print_parameter_declaration_clause PARAMS ((output_buffer *,
146 tfi_t));
147 static void print_exception_specification PARAMS ((output_buffer *, tfi_t));
148 static void print_nested_name_specifier PARAMS ((output_buffer *, tfi_t));
149 static void print_template_id PARAMS ((output_buffer *, tfi_t));
150 static tree typedef_original_name PARAMS ((tree));
151 static void print_non_consecutive_character PARAMS ((output_buffer *, int));
153 #define A args_to_string
154 #define C code_to_string
155 #define D decl_to_string
156 #define E expr_to_string
157 #define F fndecl_to_string
158 #define L language_to_string
159 #define O op_to_string
160 #define P parm_to_string
161 #define Q assop_to_string
162 #define T type_to_string
163 #define V cv_to_string
165 #define o (cp_printer *) 0
166 cp_printer * cp_printers[256] =
168 /*0 1 2 3 4 5 6 7 8 9 A B C D E F */
169 o, o, o, o, o, o, o, o, o, o, o, o, o, o, o, o, /* 0x00 */
170 o, o, o, o, o, o, o, o, o, o, o, o, o, o, o, o, /* 0x10 */
171 o, o, o, o, o, o, o, o, o, o, o, o, o, o, o, o, /* 0x20 */
172 o, o, o, o, o, o, o, o, o, o, o, o, o, o, o, o, /* 0x30 */
173 o, A, o, C, D, E, F, o, o, o, o, o, L, o, o, O, /* 0x40 */
174 P, Q, o, o, T, o, V, o, o, o, o, o, o, o, o, o, /* 0x50 */
175 o, o, o, o, o, o, o, o, o, o, o, o, o, o, o, o, /* 0x60 */
176 o, o, o, o, o, o, o, o, o, o, o, o, o, o, o, o, /* 0x70 */
178 #undef C
179 #undef D
180 #undef E
181 #undef F
182 #undef L
183 #undef O
184 #undef P
185 #undef Q
186 #undef T
187 #undef V
188 #undef o
190 void
191 init_error ()
193 init_output_buffer (scratch_buffer, /* prefix */NULL, /* line-width */0);
195 print_error_function = lang_print_error_function;
196 lang_diagnostic_starter = cp_diagnostic_starter;
197 lang_diagnostic_finalizer = cp_diagnostic_finalizer;
199 lang_printer = cp_tree_printer;
202 /* Dump a scope, if deemed necessary. */
204 static void
205 dump_scope (scope, flags)
206 tree scope;
207 int flags;
209 int f = ~TFF_RETURN_TYPE & (TFF_DECL_SPECIFIERS
210 | (flags & (TFF_SCOPE | TFF_CHASE_TYPEDEF)));
212 if (scope == NULL_TREE)
213 return;
215 if (TREE_CODE (scope) == NAMESPACE_DECL)
217 if (scope != global_namespace)
219 dump_decl (scope, f);
220 print_scope_operator (scratch_buffer);
223 else if (AGGREGATE_TYPE_P (scope))
225 dump_type (scope, f);
226 print_scope_operator (scratch_buffer);
228 else if ((flags & TFF_SCOPE) && TREE_CODE (scope) == FUNCTION_DECL)
230 dump_function_decl (scope, f);
231 print_scope_operator (scratch_buffer);
235 /* Dump type qualifiers, providing padding as requested. Return an
236 indication of whether we dumped something. */
238 static enum pad
239 dump_qualifiers (t, p)
240 tree t;
241 enum pad p;
243 static const int masks[] =
244 {TYPE_QUAL_CONST, TYPE_QUAL_VOLATILE, TYPE_QUAL_RESTRICT};
245 static const char *const names[] =
246 {"const", "volatile", "__restrict"};
247 int ix;
248 int quals = TYPE_QUALS (t);
249 int do_after = p == after;
251 if (quals)
253 for (ix = 0; ix != 3; ix++)
254 if (masks[ix] & quals)
256 if (p == before)
257 output_add_space (scratch_buffer);
258 p = before;
259 print_identifier (scratch_buffer, names[ix]);
261 if (do_after)
262 output_add_space (scratch_buffer);
264 else
265 p = none;
266 return p;
269 /* This must be large enough to hold any printed integer or floating-point
270 value. */
271 static char digit_buffer[128];
273 /* Dump the template ARGument under control of FLAGS. */
275 static void
276 dump_template_argument (arg, flags)
277 tree arg;
278 int flags;
280 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
281 dump_type (arg, flags & ~TFF_CLASS_KEY_OR_ENUM);
282 else
283 dump_expr (arg, (flags | TFF_EXPR_IN_PARENS) & ~TFF_CLASS_KEY_OR_ENUM);
286 /* Dump a template-argument-list ARGS (always a TREE_VEC) under control
287 of FLAGS. */
289 static void
290 dump_template_argument_list (args, flags)
291 tree args;
292 int flags;
294 int n = TREE_VEC_LENGTH (args);
295 int need_comma = 0;
296 int i;
298 for (i = 0; i< n; ++i)
300 if (need_comma)
301 separate_with_comma (scratch_buffer);
302 dump_template_argument (TREE_VEC_ELT (args, i), flags);
303 need_comma = 1;
307 /* Dump a template parameter PARM (a TREE_LIST) under control of FLAGS. */
309 static void
310 dump_template_parameter (parm, flags)
311 tree parm;
312 int flags;
314 tree p = TREE_VALUE (parm);
315 tree a = TREE_PURPOSE (parm);
317 if (TREE_CODE (p) == TYPE_DECL)
319 if (flags & TFF_DECL_SPECIFIERS)
321 print_identifier (scratch_buffer, "class");
322 if (DECL_NAME (p))
324 output_add_space (scratch_buffer);
325 print_tree_identifier (scratch_buffer, DECL_NAME (p));
328 else if (DECL_NAME (p))
329 print_tree_identifier (scratch_buffer, DECL_NAME (p));
330 else
331 print_identifier (scratch_buffer, "{template default argument error}");
333 else
334 dump_decl (p, flags | TFF_DECL_SPECIFIERS);
336 if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && a != NULL_TREE)
338 output_add_string (scratch_buffer, " = ");
339 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
340 dump_type (a, flags & ~TFF_CHASE_TYPEDEF);
341 else
342 dump_expr (a, flags | TFF_EXPR_IN_PARENS);
346 /* Dump, under control of FLAGS, a template-parameter-list binding.
347 PARMS is a TREE_LIST of TREE_VEC of TREE_LIST and ARGS is a
348 TREE_VEC. */
350 static void
351 dump_template_bindings (parms, args)
352 tree parms, args;
354 int need_comma = 0;
356 while (parms)
358 tree p = TREE_VALUE (parms);
359 int lvl = TMPL_PARMS_DEPTH (parms);
360 int arg_idx = 0;
361 int i;
363 for (i = 0; i < TREE_VEC_LENGTH (p); ++i)
365 tree arg = NULL_TREE;
367 /* Don't crash if we had an invalid argument list. */
368 if (TMPL_ARGS_DEPTH (args) >= lvl)
370 tree lvl_args = TMPL_ARGS_LEVEL (args, lvl);
371 if (NUM_TMPL_ARGS (lvl_args) > arg_idx)
372 arg = TREE_VEC_ELT (lvl_args, arg_idx);
375 if (need_comma)
376 separate_with_comma (scratch_buffer);
377 dump_template_parameter (TREE_VEC_ELT (p, i), TFF_PLAIN_IDENTIFIER);
378 output_add_string (scratch_buffer, " = ");
379 if (arg)
380 dump_template_argument (arg, TFF_PLAIN_IDENTIFIER);
381 else
382 print_identifier (scratch_buffer, "<missing>");
384 ++arg_idx;
385 need_comma = 1;
388 parms = TREE_CHAIN (parms);
392 /* Dump into the obstack a human-readable equivalent of TYPE. FLAGS
393 controls the format. */
395 static void
396 dump_type (t, flags)
397 tree t;
398 int flags;
400 if (t == NULL_TREE)
401 return;
403 if (TYPE_PTRMEMFUNC_P (t))
404 goto offset_type;
406 switch (TREE_CODE (t))
408 case UNKNOWN_TYPE:
409 print_identifier (scratch_buffer, "<unknown type>");
410 break;
412 case TREE_LIST:
413 /* A list of function parms. */
414 dump_parameters (t, flags);
415 break;
417 case IDENTIFIER_NODE:
418 print_tree_identifier (scratch_buffer, t);
419 break;
421 case TREE_VEC:
422 dump_type (BINFO_TYPE (t), flags);
423 break;
425 case RECORD_TYPE:
426 case UNION_TYPE:
427 case ENUMERAL_TYPE:
428 dump_aggr_type (t, flags);
429 break;
431 case TYPE_DECL:
432 if (flags & TFF_CHASE_TYPEDEF)
434 dump_type (DECL_ORIGINAL_TYPE (t)
435 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t), flags);
436 break;
438 /* else fallthrough */
440 case TEMPLATE_DECL:
441 case NAMESPACE_DECL:
442 dump_decl (t, flags & ~TFF_DECL_SPECIFIERS);
443 break;
445 case COMPLEX_TYPE:
446 output_add_string (scratch_buffer, "__complex__ ");
447 dump_type (TREE_TYPE (t), flags);
448 break;
450 case VECTOR_TYPE:
451 output_add_string (scratch_buffer, "vector ");
452 dump_type (TREE_TYPE (t), flags);
453 break;
455 case INTEGER_TYPE:
456 if (!TREE_UNSIGNED (TYPE_MAIN_VARIANT (t)) && TREE_UNSIGNED (t))
457 output_add_string (scratch_buffer, "unsigned ");
458 else if (TREE_UNSIGNED (TYPE_MAIN_VARIANT (t)) && !TREE_UNSIGNED (t))
459 output_add_string (scratch_buffer, "signed ");
461 /* fall through. */
462 case REAL_TYPE:
463 case VOID_TYPE:
464 case BOOLEAN_TYPE:
466 tree type;
467 dump_qualifiers (t, after);
468 type = flags & TFF_CHASE_TYPEDEF ? TYPE_MAIN_VARIANT (t) : t;
469 if (TYPE_NAME (type) && TYPE_IDENTIFIER (type))
470 print_tree_identifier (scratch_buffer, TYPE_IDENTIFIER (type));
471 else
472 /* Types like intQI_type_node and friends have no names.
473 These don't come up in user error messages, but it's nice
474 to be able to print them from the debugger. */
475 print_identifier (scratch_buffer, "<anonymous>");
477 break;
479 case TEMPLATE_TEMPLATE_PARM:
480 /* For parameters inside template signature. */
481 if (TYPE_IDENTIFIER (t))
482 print_tree_identifier (scratch_buffer, TYPE_IDENTIFIER (t));
483 else
484 print_identifier
485 (scratch_buffer, "<anonymous template template parameter>");
486 break;
488 case BOUND_TEMPLATE_TEMPLATE_PARM:
490 tree args = TYPE_TI_ARGS (t);
491 print_tree_identifier (scratch_buffer, TYPE_IDENTIFIER (t));
492 print_template_argument_list_start (scratch_buffer);
493 dump_template_argument_list (args, flags);
494 print_template_argument_list_end (scratch_buffer);
496 break;
498 case TEMPLATE_TYPE_PARM:
499 dump_qualifiers (t, after);
500 if (TYPE_IDENTIFIER (t))
501 print_tree_identifier (scratch_buffer, TYPE_IDENTIFIER (t));
502 else
503 print_identifier
504 (scratch_buffer, "<anonymous template type parameter>");
505 break;
507 /* This is not always necessary for pointers and such, but doing this
508 reduces code size. */
509 case ARRAY_TYPE:
510 case POINTER_TYPE:
511 case REFERENCE_TYPE:
512 case OFFSET_TYPE:
513 offset_type:
514 case FUNCTION_TYPE:
515 case METHOD_TYPE:
517 dump_type_prefix (t, flags);
518 dump_type_suffix (t, flags);
519 break;
521 case TYPENAME_TYPE:
522 output_add_string (scratch_buffer, "typename ");
523 dump_typename (t, flags);
524 break;
526 case TYPEOF_TYPE:
527 output_add_string (scratch_buffer, "__typeof (");
528 dump_expr (TYPE_FIELDS (t), flags & ~TFF_EXPR_IN_PARENS);
529 print_left_paren (scratch_buffer);
530 break;
532 default:
533 sorry_for_unsupported_tree (t);
534 /* Fall through to error. */
536 case ERROR_MARK:
537 print_identifier (scratch_buffer, "<type error>");
538 break;
542 /* Dump a TYPENAME_TYPE. We need to notice when the context is itself
543 a TYPENAME_TYPE. */
545 static void
546 dump_typename (t, flags)
547 tree t;
548 int flags;
550 tree ctx = TYPE_CONTEXT (t);
552 if (TREE_CODE (ctx) == TYPENAME_TYPE)
553 dump_typename (ctx, flags);
554 else
555 dump_type (ctx, flags & ~TFF_CLASS_KEY_OR_ENUM);
556 print_scope_operator (scratch_buffer);
557 dump_decl (TYPENAME_TYPE_FULLNAME (t), flags);
560 /* Return the name of the supplied aggregate, or enumeral type. */
562 static const char *
563 class_key_or_enum (t)
564 tree t;
566 if (TREE_CODE (t) == ENUMERAL_TYPE)
567 return "enum";
568 else if (TREE_CODE (t) == UNION_TYPE)
569 return "union";
570 else if (TYPE_LANG_SPECIFIC (t) && CLASSTYPE_DECLARED_CLASS (t))
571 return "class";
572 else
573 return "struct";
576 /* Print out a class declaration T under the control of FLAGS,
577 in the form `class foo'. */
579 static void
580 dump_aggr_type (t, flags)
581 tree t;
582 int flags;
584 tree name;
585 const char *variety = class_key_or_enum (t);
586 int typdef = 0;
587 int tmplate = 0;
589 dump_qualifiers (t, after);
591 if (flags & TFF_CLASS_KEY_OR_ENUM)
593 print_identifier (scratch_buffer, variety);
594 output_add_space (scratch_buffer);
597 if (flags & TFF_CHASE_TYPEDEF)
598 t = TYPE_MAIN_VARIANT (t);
600 name = TYPE_NAME (t);
602 if (name)
604 typdef = !DECL_ARTIFICIAL (name);
605 tmplate = !typdef && TREE_CODE (t) != ENUMERAL_TYPE
606 && TYPE_LANG_SPECIFIC (t) && CLASSTYPE_TEMPLATE_INFO (t)
607 && (CLASSTYPE_TEMPLATE_SPECIALIZATION (t)
608 || TREE_CODE (CLASSTYPE_TI_TEMPLATE (t)) != TEMPLATE_DECL
609 || DECL_TEMPLATE_SPECIALIZATION (CLASSTYPE_TI_TEMPLATE (t))
610 || PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
611 dump_scope (CP_DECL_CONTEXT (name), flags | TFF_SCOPE);
612 if (tmplate)
614 /* Because the template names are mangled, we have to locate
615 the most general template, and use that name. */
616 tree tpl = CLASSTYPE_TI_TEMPLATE (t);
618 while (DECL_TEMPLATE_INFO (tpl))
619 tpl = DECL_TI_TEMPLATE (tpl);
620 name = tpl;
622 name = DECL_NAME (name);
625 if (name == 0 || ANON_AGGRNAME_P (name))
627 if (flags & TFF_CLASS_KEY_OR_ENUM)
628 print_identifier (scratch_buffer, "<anonymous>");
629 else
630 output_printf (scratch_buffer, "<anonymous %s>", variety);
632 else
633 print_tree_identifier (scratch_buffer, name);
634 if (tmplate)
635 dump_template_parms (TYPE_TEMPLATE_INFO (t),
636 !CLASSTYPE_USE_TEMPLATE (t),
637 flags & ~TFF_TEMPLATE_HEADER);
640 /* Dump into the obstack the initial part of the output for a given type.
641 This is necessary when dealing with things like functions returning
642 functions. Examples:
644 return type of `int (* fee ())()': pointer -> function -> int. Both
645 pointer (and reference and offset) and function (and member) types must
646 deal with prefix and suffix.
648 Arrays must also do this for DECL nodes, like int a[], and for things like
649 int *[]&.
651 Return indicates how you should pad an object name after this. I.e. you
652 want to pad non-*, non-& cores, but not pad * or & types. */
654 static enum pad
655 dump_type_prefix (t, flags)
656 tree t;
657 int flags;
659 enum pad padding = before;
661 if (TYPE_PTRMEMFUNC_P (t))
663 t = TYPE_PTRMEMFUNC_FN_TYPE (t);
664 goto offset_type;
667 switch (TREE_CODE (t))
669 case POINTER_TYPE:
670 case REFERENCE_TYPE:
672 tree sub = TREE_TYPE (t);
674 padding = dump_type_prefix (sub, flags);
675 /* A tree for a member pointer looks like pointer to offset,
676 so let the OFFSET_TYPE case handle it. */
677 if (!TYPE_PTRMEM_P (t))
679 if (TREE_CODE (sub) == ARRAY_TYPE)
681 output_add_space (scratch_buffer);
682 print_left_paren (scratch_buffer);
684 output_add_character
685 (scratch_buffer, "&*"[TREE_CODE (t) == POINTER_TYPE]);
686 padding = dump_qualifiers (t, before);
689 break;
691 case OFFSET_TYPE:
692 offset_type:
693 padding = dump_type_prefix (TREE_TYPE (t), flags);
694 if (TREE_CODE (t) == OFFSET_TYPE) /* pmfs deal with this in d_t_p */
696 if (padding != none)
697 output_add_space (scratch_buffer);
698 dump_type (TYPE_OFFSET_BASETYPE (t), flags);
699 print_scope_operator (scratch_buffer);
701 output_add_character (scratch_buffer, '*');
702 padding = dump_qualifiers (t, none);
703 break;
705 /* Can only be reached through function pointer -- this would not be
706 correct if FUNCTION_DECLs used it. */
707 case FUNCTION_TYPE:
708 padding = dump_type_prefix (TREE_TYPE (t), flags);
709 if (padding != none)
710 output_add_space (scratch_buffer);
711 print_left_paren (scratch_buffer);
712 padding = none;
713 break;
715 case METHOD_TYPE:
716 padding = dump_type_prefix (TREE_TYPE (t), flags);
717 if (padding != none)
718 output_add_space (scratch_buffer);
719 print_left_paren (scratch_buffer);
720 padding = none;
721 dump_aggr_type (TYPE_METHOD_BASETYPE (t), flags);
722 print_scope_operator (scratch_buffer);
723 break;
725 case ARRAY_TYPE:
726 padding = dump_type_prefix (TREE_TYPE (t), flags);
727 break;
729 case ENUMERAL_TYPE:
730 case IDENTIFIER_NODE:
731 case INTEGER_TYPE:
732 case BOOLEAN_TYPE:
733 case REAL_TYPE:
734 case RECORD_TYPE:
735 case TEMPLATE_TYPE_PARM:
736 case TEMPLATE_TEMPLATE_PARM:
737 case BOUND_TEMPLATE_TEMPLATE_PARM:
738 case TREE_LIST:
739 case TYPE_DECL:
740 case TREE_VEC:
741 case UNION_TYPE:
742 case UNKNOWN_TYPE:
743 case VOID_TYPE:
744 case TYPENAME_TYPE:
745 case COMPLEX_TYPE:
746 case VECTOR_TYPE:
747 dump_type (t, flags);
748 padding = before;
749 break;
751 default:
752 sorry_for_unsupported_tree (t);
753 /* fall through. */
754 case ERROR_MARK:
755 print_identifier (scratch_buffer, "<typeprefixerror>");
756 break;
758 return padding;
761 /* Dump the suffix of type T, under control of FLAGS. This is the part
762 which appears after the identifier (or function parms). */
764 static void
765 dump_type_suffix (t, flags)
766 tree t;
767 int flags;
769 if (TYPE_PTRMEMFUNC_P (t))
770 t = TYPE_PTRMEMFUNC_FN_TYPE (t);
772 switch (TREE_CODE (t))
774 case POINTER_TYPE:
775 case REFERENCE_TYPE:
776 case OFFSET_TYPE:
777 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
778 print_right_paren (scratch_buffer);
779 dump_type_suffix (TREE_TYPE (t), flags);
780 break;
782 /* Can only be reached through function pointer */
783 case FUNCTION_TYPE:
784 case METHOD_TYPE:
786 tree arg;
787 print_right_paren (scratch_buffer);
788 arg = TYPE_ARG_TYPES (t);
789 if (TREE_CODE (t) == METHOD_TYPE)
790 arg = TREE_CHAIN (arg);
792 /* Function pointers don't have default args. Not in standard C++,
793 anyway; they may in g++, but we'll just pretend otherwise. */
794 dump_parameters (arg, flags & ~TFF_FUNCTION_DEFAULT_ARGUMENTS);
796 if (TREE_CODE (t) == METHOD_TYPE)
797 dump_qualifiers
798 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))), before);
799 dump_type_suffix (TREE_TYPE (t), flags);
800 dump_exception_spec (TYPE_RAISES_EXCEPTIONS (t), flags);
801 break;
804 case ARRAY_TYPE:
805 print_left_bracket (scratch_buffer);
806 if (TYPE_DOMAIN (t))
808 if (host_integerp (TYPE_MAX_VALUE (TYPE_DOMAIN (t)), 0))
809 print_integer
810 (scratch_buffer,
811 tree_low_cst (TYPE_MAX_VALUE (TYPE_DOMAIN (t)), 0) + 1);
812 else if (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (t))) == MINUS_EXPR)
813 dump_expr (TREE_OPERAND (TYPE_MAX_VALUE (TYPE_DOMAIN (t)), 0),
814 flags & ~TFF_EXPR_IN_PARENS);
815 else
816 dump_expr (fold (cp_build_binary_op
817 (PLUS_EXPR, TYPE_MAX_VALUE (TYPE_DOMAIN (t)),
818 integer_one_node)),
819 flags & ~TFF_EXPR_IN_PARENS);
821 print_right_bracket (scratch_buffer);
822 dump_type_suffix (TREE_TYPE (t), flags);
823 break;
825 case ENUMERAL_TYPE:
826 case IDENTIFIER_NODE:
827 case INTEGER_TYPE:
828 case BOOLEAN_TYPE:
829 case REAL_TYPE:
830 case RECORD_TYPE:
831 case TEMPLATE_TYPE_PARM:
832 case TEMPLATE_TEMPLATE_PARM:
833 case BOUND_TEMPLATE_TEMPLATE_PARM:
834 case TREE_LIST:
835 case TYPE_DECL:
836 case TREE_VEC:
837 case UNION_TYPE:
838 case UNKNOWN_TYPE:
839 case VOID_TYPE:
840 case TYPENAME_TYPE:
841 case COMPLEX_TYPE:
842 case VECTOR_TYPE:
843 break;
845 default:
846 sorry_for_unsupported_tree (t);
847 case ERROR_MARK:
848 /* Don't mark it here, we should have already done in
849 dump_type_prefix. */
850 break;
854 /* Return a function declaration which corresponds to the IDENTIFIER_NODE
855 argument. */
857 static tree
858 ident_fndecl (t)
859 tree t;
861 tree n = lookup_name (t, 0);
863 if (n == NULL_TREE)
864 return NULL_TREE;
866 if (TREE_CODE (n) == FUNCTION_DECL)
867 return n;
868 else if (TREE_CODE (n) == TREE_LIST
869 && TREE_CODE (TREE_VALUE (n)) == FUNCTION_DECL)
870 return TREE_VALUE (n);
872 my_friendly_abort (66);
873 return NULL_TREE;
876 #ifndef NO_DOLLAR_IN_LABEL
877 # define GLOBAL_THING "_GLOBAL_$"
878 #else
879 # ifndef NO_DOT_IN_LABEL
880 # define GLOBAL_THING "_GLOBAL_."
881 # else
882 # define GLOBAL_THING "_GLOBAL__"
883 # endif
884 #endif
886 #define GLOBAL_IORD_P(NODE) \
887 ! strncmp (IDENTIFIER_POINTER(NODE), GLOBAL_THING, sizeof (GLOBAL_THING) - 1)
889 static void
890 dump_global_iord (t)
891 tree t;
893 const char *name = IDENTIFIER_POINTER (t);
894 const char *p = NULL;
896 if (name [sizeof (GLOBAL_THING) - 1] == 'I')
897 p = "initializers";
898 else if (name [sizeof (GLOBAL_THING) - 1] == 'D')
899 p = "destructors";
900 else
901 my_friendly_abort (352);
903 output_printf (scratch_buffer, "(static %s for %s)", p, input_filename);
906 static void
907 dump_simple_decl (t, type, flags)
908 tree t;
909 tree type;
910 int flags;
912 if (flags & TFF_DECL_SPECIFIERS)
914 if (dump_type_prefix (type, flags) != none)
915 output_add_space (scratch_buffer);
917 if (!DECL_INITIAL (t) || TREE_CODE (DECL_INITIAL (t)) != TEMPLATE_PARM_INDEX)
918 dump_scope (CP_DECL_CONTEXT (t), flags);
919 if (DECL_NAME (t))
920 dump_decl (DECL_NAME (t), flags);
921 else
922 print_identifier (scratch_buffer, "<anonymous>");
923 if (flags & TFF_DECL_SPECIFIERS)
924 dump_type_suffix (type, flags);
927 /* Dump a human readable string for the decl T under control of FLAGS. */
929 static void
930 dump_decl (t, flags)
931 tree t;
932 int flags;
934 if (t == NULL_TREE)
935 return;
937 switch (TREE_CODE (t))
939 case TYPE_DECL:
941 /* Don't say 'typedef class A' */
942 if (DECL_ARTIFICIAL (t))
944 if ((flags & TFF_DECL_SPECIFIERS)
945 && TREE_CODE (TREE_TYPE (t)) == TEMPLATE_TYPE_PARM)
946 /* Say `class T' not just `T'. */
947 output_add_string (scratch_buffer, "class ");
949 dump_type (TREE_TYPE (t), flags);
950 break;
953 if (flags & TFF_DECL_SPECIFIERS)
954 output_add_string (scratch_buffer, "typedef ");
955 dump_simple_decl (t, DECL_ORIGINAL_TYPE (t)
956 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t),
957 flags);
958 break;
960 case VAR_DECL:
961 if (DECL_NAME (t) && VTABLE_NAME_P (DECL_NAME (t)))
963 output_add_string (scratch_buffer, "vtable for ");
964 if (TYPE_P (DECL_CONTEXT (t)))
965 dump_type (DECL_CONTEXT (t), flags);
966 else
967 /* This case can arise with -fno-vtable-thunks. See
968 expand_upcast_fixups. It's not clear what to print
969 here. */
970 print_identifier (scratch_buffer, "<unknown type>");
971 break;
973 /* else fall through */
974 case FIELD_DECL:
975 case PARM_DECL:
976 dump_simple_decl (t, TREE_TYPE (t), flags);
977 break;
979 case RESULT_DECL:
980 output_add_string (scratch_buffer, "<return value> ");
981 dump_simple_decl (t, TREE_TYPE (t), flags);
982 break;
984 case NAMESPACE_DECL:
985 dump_scope (CP_DECL_CONTEXT (t), flags);
986 if (DECL_NAME (t) == anonymous_namespace_name)
987 print_identifier (scratch_buffer, "<unnamed>");
988 else
989 print_tree_identifier (scratch_buffer, DECL_NAME (t));
990 break;
992 case SCOPE_REF:
993 dump_decl (TREE_OPERAND (t, 0), flags & ~TFF_DECL_SPECIFIERS);
994 print_scope_operator (scratch_buffer);
995 dump_decl (TREE_OPERAND (t, 1), flags);
996 break;
998 case ARRAY_REF:
999 dump_decl (TREE_OPERAND (t, 0), flags);
1000 print_left_bracket (scratch_buffer);
1001 dump_decl (TREE_OPERAND (t, 1), flags);
1002 print_right_bracket (scratch_buffer);
1003 break;
1005 /* So that we can do dump_decl on an aggr type. */
1006 case RECORD_TYPE:
1007 case UNION_TYPE:
1008 case ENUMERAL_TYPE:
1009 dump_type (t, flags);
1010 break;
1012 case TYPE_EXPR:
1013 my_friendly_abort (69);
1014 break;
1016 /* These special cases are duplicated here so that other functions
1017 can feed identifiers to cp_error and get them demangled properly. */
1018 case IDENTIFIER_NODE:
1019 { tree f;
1020 if (DESTRUCTOR_NAME_P (t)
1021 && (f = ident_fndecl (t))
1022 && DECL_LANGUAGE (f) == lang_cplusplus)
1024 output_add_character (scratch_buffer, '~');
1025 dump_decl (DECL_NAME (f), flags);
1027 else if (IDENTIFIER_TYPENAME_P (t))
1029 output_add_string (scratch_buffer, "operator ");
1030 /* Not exactly IDENTIFIER_TYPE_VALUE. */
1031 dump_type (TREE_TYPE (t), flags);
1032 break;
1034 else
1035 print_tree_identifier (scratch_buffer, t);
1037 break;
1039 case OVERLOAD:
1040 t = OVL_CURRENT (t);
1041 /* Fall through. */
1043 case FUNCTION_DECL:
1044 if (GLOBAL_IORD_P (DECL_ASSEMBLER_NAME (t)))
1045 dump_global_iord (DECL_ASSEMBLER_NAME (t));
1046 else if (! DECL_LANG_SPECIFIC (t))
1047 print_identifier (scratch_buffer, "<internal>");
1048 else
1049 dump_function_decl (t, flags);
1050 break;
1052 case TEMPLATE_DECL:
1053 dump_template_decl (t, flags);
1054 break;
1056 case TEMPLATE_ID_EXPR:
1058 tree args;
1059 tree name = TREE_OPERAND (t, 0);
1060 if (is_overloaded_fn (name))
1061 name = DECL_NAME (get_first_fn (name));
1062 dump_decl (name, flags);
1063 print_template_argument_list_start (scratch_buffer);
1064 for (args = TREE_OPERAND (t, 1); args; args = TREE_CHAIN (args))
1066 dump_template_argument (TREE_VALUE (args), flags);
1067 if (TREE_CHAIN (args))
1068 separate_with_comma (scratch_buffer);
1070 print_template_argument_list_end (scratch_buffer);
1072 break;
1074 case LOOKUP_EXPR:
1075 dump_decl (TREE_OPERAND (t, 0), flags);
1076 break;
1078 case LABEL_DECL:
1079 print_tree_identifier (scratch_buffer, DECL_NAME (t));
1080 break;
1082 case CONST_DECL:
1083 if ((TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == ENUMERAL_TYPE)
1084 || (DECL_INITIAL (t) &&
1085 TREE_CODE (DECL_INITIAL (t)) == TEMPLATE_PARM_INDEX))
1086 dump_simple_decl (t, TREE_TYPE (t), flags);
1087 else if (DECL_NAME (t))
1088 dump_decl (DECL_NAME (t), flags);
1089 else if (DECL_INITIAL (t))
1090 dump_expr (DECL_INITIAL (t), flags | TFF_EXPR_IN_PARENS);
1091 else
1092 print_identifier (scratch_buffer, "enumerator");
1093 break;
1095 case USING_DECL:
1096 output_add_string (scratch_buffer, "using ");
1097 dump_type (DECL_INITIAL (t), flags);
1098 print_scope_operator (scratch_buffer);
1099 print_tree_identifier (scratch_buffer, DECL_NAME (t));
1100 break;
1102 default:
1103 sorry_for_unsupported_tree (t);
1104 /* Fallthrough to error. */
1106 case ERROR_MARK:
1107 print_identifier (scratch_buffer, "<declaration error>");
1108 break;
1112 /* Dump a template declaration T under control of FLAGS. This means the
1113 'template <...> leaders plus the 'class X' or 'void fn(...)' part. */
1115 static void
1116 dump_template_decl (t, flags)
1117 tree t;
1118 int flags;
1120 tree orig_parms = DECL_TEMPLATE_PARMS (t);
1121 tree parms;
1122 int i;
1124 if (flags & TFF_TEMPLATE_HEADER)
1126 for (parms = orig_parms = nreverse (orig_parms);
1127 parms;
1128 parms = TREE_CHAIN (parms))
1130 tree inner_parms = INNERMOST_TEMPLATE_PARMS (parms);
1131 int len = TREE_VEC_LENGTH (inner_parms);
1133 output_add_string (scratch_buffer, "template<");
1134 for (i = 0; i < len; i++)
1136 if (i)
1137 separate_with_comma (scratch_buffer);
1138 dump_template_parameter (TREE_VEC_ELT (inner_parms, i), flags);
1140 print_template_argument_list_end (scratch_buffer);
1141 output_add_space (scratch_buffer);
1143 nreverse(orig_parms);
1144 /* If we've shown the template<args> prefix, we'd better show the
1145 decl's type too. */
1146 flags |= TFF_DECL_SPECIFIERS;
1148 if (TREE_CODE (DECL_TEMPLATE_RESULT (t)) == TYPE_DECL)
1149 dump_type (TREE_TYPE (t),
1150 ((flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1151 | (flags & TFF_DECL_SPECIFIERS ? TFF_CLASS_KEY_OR_ENUM : 0)));
1152 else if (TREE_CODE (DECL_TEMPLATE_RESULT (t)) == VAR_DECL)
1153 dump_decl (DECL_TEMPLATE_RESULT (t), flags | TFF_TEMPLATE_NAME);
1154 else if (TREE_TYPE (t) == NULL_TREE)
1155 my_friendly_abort (353);
1156 else
1157 switch (NEXT_CODE (t))
1159 case METHOD_TYPE:
1160 case FUNCTION_TYPE:
1161 dump_function_decl (t, flags | TFF_TEMPLATE_NAME);
1162 break;
1163 default:
1164 /* This case can occur with some illegal code. */
1165 dump_type (TREE_TYPE (t),
1166 (flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1167 | (flags & TFF_DECL_SPECIFIERS ? TFF_CLASS_KEY_OR_ENUM : 0));
1171 /* Pretty print a function decl. There are several ways we want to print a
1172 function declaration. The TFF_ bits in FLAGS tells us how to behave.
1173 As cp_error can only apply the '#' flag once to give 0 and 1 for V, there
1174 is %D which doesn't print the throw specs, and %F which does. */
1176 static void
1177 dump_function_decl (t, flags)
1178 tree t;
1179 int flags;
1181 tree fntype;
1182 tree parmtypes;
1183 tree cname = NULL_TREE;
1184 tree template_args = NULL_TREE;
1185 tree template_parms = NULL_TREE;
1186 int show_return = flags & TFF_RETURN_TYPE || flags & TFF_DECL_SPECIFIERS;
1188 if (TREE_CODE (t) == TEMPLATE_DECL)
1189 t = DECL_TEMPLATE_RESULT (t);
1191 /* Pretty print template instantiations only. */
1192 if (DECL_USE_TEMPLATE (t) && DECL_TEMPLATE_INFO (t))
1194 tree tmpl;
1196 template_args = DECL_TI_ARGS (t);
1197 tmpl = most_general_template (t);
1198 if (tmpl && TREE_CODE (tmpl) == TEMPLATE_DECL)
1200 template_parms = DECL_TEMPLATE_PARMS (tmpl);
1201 t = tmpl;
1205 fntype = TREE_TYPE (t);
1206 parmtypes = TYPE_ARG_TYPES (fntype);
1208 if (DECL_CLASS_SCOPE_P (t))
1209 cname = DECL_CONTEXT (t);
1210 /* this is for partially instantiated template methods */
1211 else if (TREE_CODE (fntype) == METHOD_TYPE)
1212 cname = TREE_TYPE (TREE_VALUE (parmtypes));
1214 if (!(flags & TFF_DECL_SPECIFIERS))
1215 /* OK */;
1216 else if (DECL_STATIC_FUNCTION_P (t))
1217 print_identifier (scratch_buffer, "static ");
1218 else if (DECL_VIRTUAL_P (t))
1219 print_identifier (scratch_buffer, "virtual ");
1221 /* Print the return type? */
1222 if (show_return)
1223 show_return = !DECL_CONV_FN_P (t) && !DECL_CONSTRUCTOR_P (t)
1224 && !DECL_DESTRUCTOR_P (t);
1225 if (show_return)
1227 dump_type_prefix (TREE_TYPE (fntype), flags);
1228 output_add_space (scratch_buffer);
1231 /* Print the function name. */
1232 if (cname)
1234 dump_type (cname, flags);
1235 print_scope_operator (scratch_buffer);
1237 else
1238 dump_scope (CP_DECL_CONTEXT (t), flags);
1240 dump_function_name (t, flags);
1242 if (flags & TFF_DECL_SPECIFIERS)
1244 if (TREE_CODE (fntype) == METHOD_TYPE && parmtypes)
1245 /* Skip "this" parameter. */
1246 parmtypes = TREE_CHAIN (parmtypes);
1248 /* Skip past the "in_charge" parameter. */
1249 if (DECL_HAS_IN_CHARGE_PARM_P (t))
1250 parmtypes = TREE_CHAIN (parmtypes);
1252 dump_parameters (parmtypes, flags);
1254 if (show_return)
1255 dump_type_suffix (TREE_TYPE (fntype), flags);
1257 if (TREE_CODE (fntype) == METHOD_TYPE)
1258 dump_qualifiers (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fntype))),
1259 before);
1261 if (flags & TFF_EXCEPTION_SPECIFICATION)
1262 dump_exception_spec (TYPE_RAISES_EXCEPTIONS (fntype), flags);
1265 /* If T is a template instantiation, dump the parameter binding. */
1266 if (template_parms != NULL_TREE && template_args != NULL_TREE)
1268 output_add_string (scratch_buffer, " [with ");
1269 dump_template_bindings (template_parms, template_args);
1270 print_right_bracket (scratch_buffer);
1274 /* Print a parameter list. If this is for a member function, the
1275 member object ptr (and any other hidden args) should have
1276 already been removed. */
1278 static void
1279 dump_parameters (parmtypes, flags)
1280 tree parmtypes;
1281 int flags;
1283 int first;
1285 print_left_paren (scratch_buffer);
1287 for (first = 1; parmtypes != void_list_node;
1288 parmtypes = TREE_CHAIN (parmtypes))
1290 if (!first)
1291 separate_with_comma (scratch_buffer);
1292 first = 0;
1293 if (!parmtypes)
1295 print_identifier (scratch_buffer, "...");
1296 break;
1298 dump_type (TREE_VALUE (parmtypes), flags);
1300 if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && TREE_PURPOSE (parmtypes))
1302 output_add_string (scratch_buffer, " = ");
1303 dump_expr (TREE_PURPOSE (parmtypes), flags | TFF_EXPR_IN_PARENS);
1307 print_right_paren (scratch_buffer);
1310 /* Print an exception specification. T is the exception specification. */
1312 static void
1313 dump_exception_spec (t, flags)
1314 tree t;
1315 int flags;
1317 if (t)
1319 output_add_string (scratch_buffer, " throw (");
1320 if (TREE_VALUE (t) != NULL_TREE)
1321 while (1)
1323 dump_type (TREE_VALUE (t), flags);
1324 t = TREE_CHAIN (t);
1325 if (!t)
1326 break;
1327 separate_with_comma (scratch_buffer);
1329 print_right_paren (scratch_buffer);
1333 /* Handle the function name for a FUNCTION_DECL node, grokking operators
1334 and destructors properly. */
1336 static void
1337 dump_function_name (t, flags)
1338 tree t;
1339 int flags;
1341 tree name = DECL_NAME (t);
1343 /* Don't let the user see __comp_ctor et al. */
1344 if (DECL_CONSTRUCTOR_P (t)
1345 || DECL_DESTRUCTOR_P (t))
1346 name = constructor_name (DECL_CONTEXT (t));
1348 if (DECL_DESTRUCTOR_P (t))
1350 output_add_character (scratch_buffer, '~');
1351 dump_decl (name, TFF_PLAIN_IDENTIFIER);
1353 else if (DECL_CONV_FN_P (t))
1355 /* This cannot use the hack that the operator's return
1356 type is stashed off of its name because it may be
1357 used for error reporting. In the case of conflicting
1358 declarations, both will have the same name, yet
1359 the types will be different, hence the TREE_TYPE field
1360 of the first name will be clobbered by the second. */
1361 output_add_string (scratch_buffer, "operator ");
1362 dump_type (TREE_TYPE (TREE_TYPE (t)), flags);
1364 else if (IDENTIFIER_OPNAME_P (name))
1365 print_tree_identifier (scratch_buffer, name);
1366 else
1367 dump_decl (name, flags);
1369 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
1370 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (t)
1371 && (DECL_TEMPLATE_SPECIALIZATION (t)
1372 || TREE_CODE (DECL_TI_TEMPLATE (t)) != TEMPLATE_DECL
1373 || DECL_TEMPLATE_SPECIALIZATION (DECL_TI_TEMPLATE (t))
1374 || PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
1375 dump_template_parms (DECL_TEMPLATE_INFO (t), !DECL_USE_TEMPLATE (t), flags);
1378 /* Dump the template parameters from the template info INFO under control of
1379 FLAGS. PRIMARY indicates whether this is a primary template decl, or
1380 specialization (partial or complete). For partial specializations we show
1381 the specialized parameter values. For a primary template we show no
1382 decoration. */
1384 static void
1385 dump_template_parms (info, primary, flags)
1386 tree info;
1387 int primary;
1388 int flags;
1390 tree args = info ? TI_ARGS (info) : NULL_TREE;
1392 if (primary && flags & TFF_TEMPLATE_NAME)
1393 return;
1394 flags &= ~(TFF_CLASS_KEY_OR_ENUM | TFF_TEMPLATE_NAME);
1395 print_template_argument_list_start (scratch_buffer);
1397 /* Be careful only to print things when we have them, so as not
1398 to crash producing error messages. */
1399 if (args && !primary)
1401 int len = 0;
1402 int ix = 0;
1403 int need_comma = 0;
1405 if (TREE_CODE (args) == TREE_VEC)
1407 if (TREE_VEC_LENGTH (args) > 0
1408 && TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
1409 args = TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 1);
1411 len = TREE_VEC_LENGTH (args);
1413 else if (TREE_CODE (args) == TREE_LIST)
1414 len = -1;
1415 while (ix != len && args)
1417 tree arg;
1418 if (len >= 0)
1420 arg = TREE_VEC_ELT (args, ix);
1421 ix++;
1423 else
1425 arg = TREE_VALUE (args);
1426 args = TREE_CHAIN (args);
1428 if (need_comma)
1429 separate_with_comma (scratch_buffer);
1431 if (!arg)
1432 print_identifier (scratch_buffer, "<template parameter error>");
1433 else
1434 dump_template_argument (arg, flags);
1435 need_comma = 1;
1438 else if (primary)
1440 tree tpl = TI_TEMPLATE (info);
1441 tree parms = DECL_TEMPLATE_PARMS (tpl);
1442 int len, ix;
1444 parms = TREE_CODE (parms) == TREE_LIST ? TREE_VALUE (parms) : NULL_TREE;
1445 len = parms ? TREE_VEC_LENGTH (parms) : 0;
1447 for (ix = 0; ix != len; ix++)
1449 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, ix));
1451 if (ix)
1452 separate_with_comma (scratch_buffer);
1454 dump_decl (parm, flags & ~TFF_DECL_SPECIFIERS);
1457 print_template_argument_list_end (scratch_buffer);
1460 static void
1461 dump_char (c)
1462 int c;
1464 switch (c)
1466 case TARGET_NEWLINE:
1467 output_add_string (scratch_buffer, "\\n");
1468 break;
1469 case TARGET_TAB:
1470 output_add_string (scratch_buffer, "\\t");
1471 break;
1472 case TARGET_VT:
1473 output_add_string (scratch_buffer, "\\v");
1474 break;
1475 case TARGET_BS:
1476 output_add_string (scratch_buffer, "\\b");
1477 break;
1478 case TARGET_CR:
1479 output_add_string (scratch_buffer, "\\r");
1480 break;
1481 case TARGET_FF:
1482 output_add_string (scratch_buffer, "\\f");
1483 break;
1484 case TARGET_BELL:
1485 output_add_string (scratch_buffer, "\\a");
1486 break;
1487 case '\\':
1488 output_add_string (scratch_buffer, "\\\\");
1489 break;
1490 case '\'':
1491 output_add_string (scratch_buffer, "\\'");
1492 break;
1493 case '\"':
1494 output_add_string (scratch_buffer, "\\\"");
1495 break;
1496 default:
1497 if (ISPRINT (c))
1498 output_add_character (scratch_buffer, c);
1499 else
1501 sprintf (digit_buffer, "\\%03o", (int) c);
1502 output_add_string (scratch_buffer, digit_buffer);
1507 /* Print out a list of initializers (subr of dump_expr) */
1509 static void
1510 dump_expr_list (l, flags)
1511 tree l;
1512 int flags;
1514 while (l)
1516 dump_expr (TREE_VALUE (l), flags | TFF_EXPR_IN_PARENS);
1517 l = TREE_CHAIN (l);
1518 if (l)
1519 separate_with_comma (scratch_buffer);
1523 /* Print out an expression E under control of FLAGS. */
1525 static void
1526 dump_expr (t, flags)
1527 tree t;
1528 int flags;
1530 switch (TREE_CODE (t))
1532 case VAR_DECL:
1533 case PARM_DECL:
1534 case FIELD_DECL:
1535 case CONST_DECL:
1536 case FUNCTION_DECL:
1537 case TEMPLATE_DECL:
1538 case NAMESPACE_DECL:
1539 case OVERLOAD:
1540 dump_decl (t, flags & ~TFF_DECL_SPECIFIERS);
1541 break;
1543 case INTEGER_CST:
1545 tree type = TREE_TYPE (t);
1546 my_friendly_assert (type != 0, 81);
1548 /* If it's an enum, output its tag, rather than its value. */
1549 if (TREE_CODE (type) == ENUMERAL_TYPE)
1551 tree values = TYPE_VALUES (type);
1553 for (; values;
1554 values = TREE_CHAIN (values))
1555 if (tree_int_cst_equal (TREE_VALUE (values), t))
1556 break;
1558 if (values)
1559 print_tree_identifier (scratch_buffer, TREE_PURPOSE (values));
1560 else
1562 /* Value must have been cast. */
1563 print_left_paren (scratch_buffer);
1564 dump_type (type, flags);
1565 print_right_paren (scratch_buffer);
1566 goto do_int;
1569 else if (type == boolean_type_node)
1571 if (t == boolean_false_node || integer_zerop (t))
1572 print_identifier (scratch_buffer, "false");
1573 else if (t == boolean_true_node)
1574 print_identifier (scratch_buffer, "true");
1576 else if (type == char_type_node)
1578 output_add_character (scratch_buffer, '\'');
1579 dump_char (tree_low_cst (t, 0));
1580 output_add_character (scratch_buffer, '\'');
1582 else
1584 do_int:
1585 if (! host_integerp (t, 0))
1587 tree val = t;
1589 if (tree_int_cst_sgn (val) < 0)
1591 output_add_character (scratch_buffer, '-');
1592 val = build_int_2 (-TREE_INT_CST_LOW (val),
1593 ~TREE_INT_CST_HIGH (val)
1594 + !TREE_INT_CST_LOW (val));
1596 /* Would "%x%0*x" or "%x%*0x" get zero-padding on all
1597 systems? */
1599 static char format[10]; /* "%x%09999x\0" */
1600 if (!format[0])
1601 sprintf (format, "%%x%%0%dx", HOST_BITS_PER_INT / 4);
1602 sprintf (digit_buffer, format, TREE_INT_CST_HIGH (val),
1603 TREE_INT_CST_LOW (val));
1604 output_add_string (scratch_buffer, digit_buffer);
1607 else
1608 print_integer (scratch_buffer, TREE_INT_CST_LOW (t));
1611 break;
1613 case REAL_CST:
1614 #ifndef REAL_IS_NOT_DOUBLE
1615 sprintf (digit_buffer, "%g", TREE_REAL_CST (t));
1616 #else
1618 const unsigned char *p = (const unsigned char *) &TREE_REAL_CST (t);
1619 size_t i;
1620 strcpy (digit_buffer, "0x");
1621 for (i = 0; i < sizeof TREE_REAL_CST (t); i++)
1622 sprintf (digit_buffer + 2 + 2*i, "%02x", *p++);
1624 #endif
1625 output_add_string (scratch_buffer, digit_buffer);
1626 break;
1628 case PTRMEM_CST:
1629 output_add_character (scratch_buffer, '&');
1630 dump_type (PTRMEM_CST_CLASS (t), flags);
1631 print_scope_operator (scratch_buffer);
1632 print_tree_identifier
1633 (scratch_buffer, DECL_NAME (PTRMEM_CST_MEMBER (t)));
1634 break;
1636 case STRING_CST:
1638 const char *p = TREE_STRING_POINTER (t);
1639 int len = TREE_STRING_LENGTH (t) - 1;
1640 int i;
1642 output_add_character (scratch_buffer, '\"');
1643 for (i = 0; i < len; i++)
1644 dump_char (p[i]);
1645 output_add_character (scratch_buffer, '\"');
1647 break;
1649 case COMPOUND_EXPR:
1650 print_left_paren (scratch_buffer);
1651 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1652 separate_with_comma (scratch_buffer);
1653 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
1654 print_right_paren (scratch_buffer);
1655 break;
1657 case COND_EXPR:
1658 print_left_paren (scratch_buffer);
1659 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1660 output_add_string (scratch_buffer, " ? ");
1661 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
1662 output_add_string (scratch_buffer, " : ");
1663 dump_expr (TREE_OPERAND (t, 2), flags | TFF_EXPR_IN_PARENS);
1664 print_right_paren (scratch_buffer);
1665 break;
1667 case SAVE_EXPR:
1668 if (TREE_HAS_CONSTRUCTOR (t))
1670 output_add_string (scratch_buffer, "new ");
1671 dump_type (TREE_TYPE (TREE_TYPE (t)), flags);
1673 else
1675 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1677 break;
1679 case AGGR_INIT_EXPR:
1681 tree fn = NULL_TREE;
1683 if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR)
1684 fn = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
1686 if (fn && TREE_CODE (fn) == FUNCTION_DECL)
1688 if (DECL_CONSTRUCTOR_P (fn))
1689 print_tree_identifier
1690 (scratch_buffer, TYPE_IDENTIFIER (TREE_TYPE (t)));
1691 else
1692 dump_decl (fn, 0);
1694 else
1695 dump_expr (TREE_OPERAND (t, 0), 0);
1697 print_left_paren (scratch_buffer);
1698 if (TREE_OPERAND (t, 1))
1699 dump_expr_list (TREE_CHAIN (TREE_OPERAND (t, 1)), flags);
1700 print_right_paren (scratch_buffer);
1701 break;
1703 case CALL_EXPR:
1705 tree fn = TREE_OPERAND (t, 0);
1706 tree args = TREE_OPERAND (t, 1);
1708 if (TREE_CODE (fn) == ADDR_EXPR)
1709 fn = TREE_OPERAND (fn, 0);
1711 if (TREE_TYPE (fn) != NULL_TREE && NEXT_CODE (fn) == METHOD_TYPE)
1713 tree ob = TREE_VALUE (args);
1714 if (TREE_CODE (ob) == ADDR_EXPR)
1716 dump_expr (TREE_OPERAND (ob, 0), flags | TFF_EXPR_IN_PARENS);
1717 output_add_character (scratch_buffer, '.');
1719 else if (TREE_CODE (ob) != PARM_DECL
1720 || strcmp (IDENTIFIER_POINTER (DECL_NAME (ob)), "this"))
1722 dump_expr (ob, flags | TFF_EXPR_IN_PARENS);
1723 output_add_string (scratch_buffer, "->");
1725 args = TREE_CHAIN (args);
1727 dump_expr (fn, flags | TFF_EXPR_IN_PARENS);
1728 print_left_paren (scratch_buffer);
1729 dump_expr_list (args, flags);
1730 print_right_paren (scratch_buffer);
1732 break;
1734 case NEW_EXPR:
1736 tree type = TREE_OPERAND (t, 1);
1737 if (NEW_EXPR_USE_GLOBAL (t))
1738 print_scope_operator (scratch_buffer);
1739 output_add_string (scratch_buffer, "new ");
1740 if (TREE_OPERAND (t, 0))
1742 print_left_paren (scratch_buffer);
1743 dump_expr_list (TREE_OPERAND (t, 0), flags);
1744 output_add_string (scratch_buffer, ") ");
1746 if (TREE_CODE (type) == ARRAY_REF)
1747 type = build_cplus_array_type
1748 (TREE_OPERAND (type, 0),
1749 build_index_type (fold (build (MINUS_EXPR, integer_type_node,
1750 TREE_OPERAND (type, 1),
1751 integer_one_node))));
1752 dump_type (type, flags);
1753 if (TREE_OPERAND (t, 2))
1755 print_left_paren (scratch_buffer);
1756 dump_expr_list (TREE_OPERAND (t, 2), flags);
1757 print_right_paren (scratch_buffer);
1760 break;
1762 case TARGET_EXPR:
1763 /* Note that this only works for G++ target exprs. If somebody
1764 builds a general TARGET_EXPR, there's no way to represent that
1765 it initializes anything other that the parameter slot for the
1766 default argument. Note we may have cleared out the first
1767 operand in expand_expr, so don't go killing ourselves. */
1768 if (TREE_OPERAND (t, 1))
1769 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
1770 break;
1772 case INIT_EXPR:
1773 case MODIFY_EXPR:
1774 case PLUS_EXPR:
1775 case MINUS_EXPR:
1776 case MULT_EXPR:
1777 case TRUNC_DIV_EXPR:
1778 case TRUNC_MOD_EXPR:
1779 case MIN_EXPR:
1780 case MAX_EXPR:
1781 case LSHIFT_EXPR:
1782 case RSHIFT_EXPR:
1783 case BIT_IOR_EXPR:
1784 case BIT_XOR_EXPR:
1785 case BIT_AND_EXPR:
1786 case BIT_ANDTC_EXPR:
1787 case TRUTH_ANDIF_EXPR:
1788 case TRUTH_ORIF_EXPR:
1789 case LT_EXPR:
1790 case LE_EXPR:
1791 case GT_EXPR:
1792 case GE_EXPR:
1793 case EQ_EXPR:
1794 case NE_EXPR:
1795 case EXACT_DIV_EXPR:
1796 dump_binary_op (operator_name_info[(int) TREE_CODE (t)].name, t, flags);
1797 break;
1799 case CEIL_DIV_EXPR:
1800 case FLOOR_DIV_EXPR:
1801 case ROUND_DIV_EXPR:
1802 dump_binary_op ("/", t, flags);
1803 break;
1805 case CEIL_MOD_EXPR:
1806 case FLOOR_MOD_EXPR:
1807 case ROUND_MOD_EXPR:
1808 dump_binary_op ("%", t, flags);
1809 break;
1811 case COMPONENT_REF:
1813 tree ob = TREE_OPERAND (t, 0);
1814 if (TREE_CODE (ob) == INDIRECT_REF)
1816 ob = TREE_OPERAND (ob, 0);
1817 if (TREE_CODE (ob) != PARM_DECL
1818 || strcmp (IDENTIFIER_POINTER (DECL_NAME (ob)), "this"))
1820 dump_expr (ob, flags | TFF_EXPR_IN_PARENS);
1821 output_add_string (scratch_buffer, "->");
1824 else
1826 dump_expr (ob, flags | TFF_EXPR_IN_PARENS);
1827 output_add_character (scratch_buffer, '.');
1829 dump_expr (TREE_OPERAND (t, 1), flags & ~TFF_EXPR_IN_PARENS);
1831 break;
1833 case ARRAY_REF:
1834 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1835 print_left_bracket (scratch_buffer);
1836 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
1837 print_right_bracket (scratch_buffer);
1838 break;
1840 case CONVERT_EXPR:
1841 if (VOID_TYPE_P (TREE_TYPE (t)))
1843 print_left_paren (scratch_buffer);
1844 dump_type (TREE_TYPE (t), flags);
1845 print_right_paren (scratch_buffer);
1846 dump_expr (TREE_OPERAND (t, 0), flags);
1848 else
1849 dump_unary_op ("+", t, flags);
1850 break;
1852 case ADDR_EXPR:
1853 if (TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
1854 || TREE_CODE (TREE_OPERAND (t, 0)) == STRING_CST
1855 /* An ADDR_EXPR can have reference type. In that case, we
1856 shouldn't print the `&' doing so indicates to the user
1857 that the expression has pointer type. */
1858 || (TREE_TYPE (t)
1859 && TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE))
1860 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1861 else
1862 dump_unary_op ("&", t, flags);
1863 break;
1865 case INDIRECT_REF:
1866 if (TREE_HAS_CONSTRUCTOR (t))
1868 t = TREE_OPERAND (t, 0);
1869 my_friendly_assert (TREE_CODE (t) == CALL_EXPR, 237);
1870 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1871 print_left_paren (scratch_buffer);
1872 dump_expr_list (TREE_CHAIN (TREE_OPERAND (t, 1)), flags);
1873 print_right_paren (scratch_buffer);
1875 else
1877 if (TREE_OPERAND (t,0) != NULL_TREE
1878 && TREE_TYPE (TREE_OPERAND (t, 0))
1879 && NEXT_CODE (TREE_OPERAND (t, 0)) == REFERENCE_TYPE)
1880 dump_expr (TREE_OPERAND (t, 0), flags);
1881 else
1882 dump_unary_op ("*", t, flags);
1884 break;
1886 case NEGATE_EXPR:
1887 case BIT_NOT_EXPR:
1888 case TRUTH_NOT_EXPR:
1889 case PREDECREMENT_EXPR:
1890 case PREINCREMENT_EXPR:
1891 dump_unary_op (operator_name_info [(int)TREE_CODE (t)].name, t, flags);
1892 break;
1894 case POSTDECREMENT_EXPR:
1895 case POSTINCREMENT_EXPR:
1896 print_left_paren (scratch_buffer);
1897 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1898 print_identifier
1899 (scratch_buffer, operator_name_info[(int)TREE_CODE (t)].name);
1900 print_right_paren (scratch_buffer);
1901 break;
1903 case NON_LVALUE_EXPR:
1904 /* FIXME: This is a KLUDGE workaround for a parsing problem. There
1905 should be another level of INDIRECT_REF so that I don't have to do
1906 this. */
1907 if (TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == POINTER_TYPE)
1909 tree next = TREE_TYPE (TREE_TYPE (t));
1911 while (TREE_CODE (next) == POINTER_TYPE)
1912 next = TREE_TYPE (next);
1914 if (TREE_CODE (next) == FUNCTION_TYPE)
1916 if (flags & TFF_EXPR_IN_PARENS)
1917 print_left_paren (scratch_buffer);
1918 output_add_character (scratch_buffer, '*');
1919 dump_expr (TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
1920 if (flags & TFF_EXPR_IN_PARENS)
1921 print_right_paren (scratch_buffer);
1922 break;
1924 /* else FALLTHRU */
1926 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1927 break;
1929 case NOP_EXPR:
1930 dump_expr (TREE_OPERAND (t, 0), flags);
1931 break;
1933 case EXPR_WITH_FILE_LOCATION:
1934 dump_expr (EXPR_WFL_NODE (t), flags);
1935 break;
1937 case CONSTRUCTOR:
1938 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
1940 tree idx = build_component_ref (t, index_identifier, NULL_TREE, 0);
1942 if (integer_all_onesp (idx))
1944 tree pfn = PFN_FROM_PTRMEMFUNC (t);
1945 dump_unary_op ("&", pfn, flags | TFF_EXPR_IN_PARENS);
1946 break;
1948 else if (TREE_CODE (idx) == INTEGER_CST
1949 && tree_int_cst_equal (idx, integer_zero_node))
1951 /* A NULL pointer-to-member constant. */
1952 output_add_string (scratch_buffer, "((");
1953 dump_type (TREE_TYPE (t), flags);
1954 output_add_string (scratch_buffer, ") 0)");
1955 break;
1957 else if (host_integerp (idx, 0))
1959 tree virtuals;
1960 unsigned HOST_WIDE_INT n;
1962 t = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
1963 t = TYPE_METHOD_BASETYPE (t);
1964 virtuals = TYPE_BINFO_VIRTUALS (TYPE_MAIN_VARIANT (t));
1966 n = tree_low_cst (idx, 0);
1968 /* Map vtable index back one, to allow for the null pointer to
1969 member. */
1970 --n;
1972 while (n > 0 && virtuals)
1974 --n;
1975 virtuals = TREE_CHAIN (virtuals);
1977 if (virtuals)
1979 dump_expr (BV_FN (virtuals),
1980 flags | TFF_EXPR_IN_PARENS);
1981 break;
1985 output_add_character (scratch_buffer, '{');
1986 dump_expr_list (CONSTRUCTOR_ELTS (t), flags);
1987 output_add_character (scratch_buffer, '}');
1988 break;
1990 case OFFSET_REF:
1992 tree ob = TREE_OPERAND (t, 0);
1993 if (is_dummy_object (ob))
1995 t = TREE_OPERAND (t, 1);
1996 if (TREE_CODE (t) == FUNCTION_DECL)
1997 /* A::f */
1998 dump_expr (t, flags | TFF_EXPR_IN_PARENS);
1999 else if (BASELINK_P (t))
2000 dump_expr (OVL_CURRENT (TREE_VALUE (t)), flags | TFF_EXPR_IN_PARENS);
2001 else
2002 dump_decl (t, flags);
2004 else
2006 if (TREE_CODE (ob) == INDIRECT_REF)
2008 dump_expr (TREE_OPERAND (ob, 0), flags | TFF_EXPR_IN_PARENS);
2009 output_add_string (scratch_buffer, "->*");
2011 else
2013 dump_expr (ob, flags | TFF_EXPR_IN_PARENS);
2014 output_add_string (scratch_buffer, ".*");
2016 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2018 break;
2021 case TEMPLATE_PARM_INDEX:
2022 dump_decl (TEMPLATE_PARM_DECL (t), flags & ~TFF_DECL_SPECIFIERS);
2023 break;
2025 case IDENTIFIER_NODE:
2026 print_tree_identifier (scratch_buffer, t);
2027 break;
2029 case SCOPE_REF:
2030 dump_type (TREE_OPERAND (t, 0), flags);
2031 print_scope_operator (scratch_buffer);
2032 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2033 break;
2035 case CAST_EXPR:
2036 if (TREE_OPERAND (t, 0) == NULL_TREE
2037 || TREE_CHAIN (TREE_OPERAND (t, 0)))
2039 dump_type (TREE_TYPE (t), flags);
2040 print_left_paren (scratch_buffer);
2041 dump_expr_list (TREE_OPERAND (t, 0), flags);
2042 print_right_paren (scratch_buffer);
2044 else
2046 print_left_paren (scratch_buffer);
2047 dump_type (TREE_TYPE (t), flags);
2048 output_add_string (scratch_buffer, ")(");
2049 dump_expr_list (TREE_OPERAND (t, 0), flags);
2050 print_right_paren (scratch_buffer);
2052 break;
2054 case STATIC_CAST_EXPR:
2055 output_add_string (scratch_buffer, "static_cast<");
2056 goto cast;
2057 case REINTERPRET_CAST_EXPR:
2058 output_add_string (scratch_buffer, "reinterpret_cast<");
2059 goto cast;
2060 case CONST_CAST_EXPR:
2061 output_add_string (scratch_buffer, "const_cast<");
2062 goto cast;
2063 case DYNAMIC_CAST_EXPR:
2064 output_add_string (scratch_buffer, "dynamic_cast<");
2065 cast:
2066 dump_type (TREE_TYPE (t), flags);
2067 output_add_string (scratch_buffer, ">(");
2068 dump_expr (TREE_OPERAND (t, 0), flags);
2069 print_right_paren (scratch_buffer);
2070 break;
2072 case LOOKUP_EXPR:
2073 print_tree_identifier (scratch_buffer, TREE_OPERAND (t, 0));
2074 break;
2076 case ARROW_EXPR:
2077 dump_expr (TREE_OPERAND (t, 0), flags);
2078 output_add_string (scratch_buffer, "->");
2079 break;
2081 case SIZEOF_EXPR:
2082 case ALIGNOF_EXPR:
2083 if (TREE_CODE (t) == SIZEOF_EXPR)
2084 output_add_string (scratch_buffer, "sizeof (");
2085 else
2087 my_friendly_assert (TREE_CODE (t) == ALIGNOF_EXPR, 0);
2088 output_add_string (scratch_buffer, "__alignof__ (");
2090 if (TYPE_P (TREE_OPERAND (t, 0)))
2091 dump_type (TREE_OPERAND (t, 0), flags);
2092 else
2093 dump_unary_op ("*", t, flags | TFF_EXPR_IN_PARENS);
2094 print_right_paren (scratch_buffer);
2095 break;
2097 case DEFAULT_ARG:
2098 print_identifier (scratch_buffer, "<unparsed>");
2099 break;
2101 case TRY_CATCH_EXPR:
2102 case WITH_CLEANUP_EXPR:
2103 case CLEANUP_POINT_EXPR:
2104 dump_expr (TREE_OPERAND (t, 0), flags);
2105 break;
2107 case PSEUDO_DTOR_EXPR:
2108 dump_expr (TREE_OPERAND (t, 2), flags);
2109 output_add_character (scratch_buffer, '.');
2110 dump_type (TREE_OPERAND (t, 0), flags);
2111 output_add_string (scratch_buffer, "::~");
2112 dump_type (TREE_OPERAND (t, 1), flags);
2113 break;
2115 case TEMPLATE_ID_EXPR:
2116 dump_decl (t, flags);
2117 break;
2119 case STMT_EXPR:
2120 /* We don't yet have a way of dumping statements in a
2121 human-readable format. */
2122 output_add_string (scratch_buffer, "({...})");
2123 break;
2125 case BIND_EXPR:
2126 output_add_character (scratch_buffer, '}');
2127 dump_expr (TREE_OPERAND (t, 1), flags & ~TFF_EXPR_IN_PARENS);
2128 output_add_character (scratch_buffer, '}');
2129 break;
2131 case LOOP_EXPR:
2132 output_add_string (scratch_buffer, "while (1) { ");
2133 dump_expr (TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2134 output_add_character (scratch_buffer, '}');
2135 break;
2137 case EXIT_EXPR:
2138 output_add_string (scratch_buffer, "if (");
2139 dump_expr (TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2140 output_add_string (scratch_buffer, ") break; ");
2141 break;
2143 case TREE_LIST:
2144 if (TREE_VALUE (t) && TREE_CODE (TREE_VALUE (t)) == FUNCTION_DECL)
2146 print_tree_identifier (scratch_buffer, DECL_NAME (TREE_VALUE (t)));
2147 break;
2149 /* else fall through */
2151 /* This list is incomplete, but should suffice for now.
2152 It is very important that `sorry' does not call
2153 `report_error_function'. That could cause an infinite loop. */
2154 default:
2155 sorry_for_unsupported_tree (t);
2156 /* fall through to ERROR_MARK... */
2157 case ERROR_MARK:
2158 print_identifier (scratch_buffer, "<expression error>");
2159 break;
2163 static void
2164 dump_binary_op (opstring, t, flags)
2165 const char *opstring;
2166 tree t;
2167 int flags;
2169 print_left_paren (scratch_buffer);
2170 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2171 output_add_space (scratch_buffer);
2172 if (opstring)
2173 print_identifier (scratch_buffer, opstring);
2174 else
2175 print_identifier (scratch_buffer, "<unknown operator>");
2176 output_add_space (scratch_buffer);
2177 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2178 print_right_paren (scratch_buffer);
2181 static void
2182 dump_unary_op (opstring, t, flags)
2183 const char *opstring;
2184 tree t;
2185 int flags;
2187 if (flags & TFF_EXPR_IN_PARENS)
2188 print_left_paren (scratch_buffer);
2189 print_identifier (scratch_buffer, opstring);
2190 dump_expr (TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2191 if (flags & TFF_EXPR_IN_PARENS)
2192 print_right_paren (scratch_buffer);
2195 /* Exported interface to stringifying types, exprs and decls under TFF_*
2196 control. */
2198 const char *
2199 type_as_string (typ, flags)
2200 tree typ;
2201 int flags;
2203 reinit_global_formatting_buffer ();
2205 dump_type (typ, flags);
2207 return output_finalize_message (scratch_buffer);
2210 const char *
2211 expr_as_string (decl, flags)
2212 tree decl;
2213 int flags;
2215 reinit_global_formatting_buffer ();
2217 dump_expr (decl, flags);
2219 return output_finalize_message (scratch_buffer);
2222 const char *
2223 decl_as_string (decl, flags)
2224 tree decl;
2225 int flags;
2227 reinit_global_formatting_buffer ();
2229 dump_decl (decl, flags);
2231 return output_finalize_message (scratch_buffer);
2234 const char *
2235 context_as_string (context, flags)
2236 tree context;
2237 int flags;
2239 reinit_global_formatting_buffer ();
2241 dump_scope (context, flags);
2243 return output_finalize_message (scratch_buffer);
2246 /* Generate the three forms of printable names for lang_printable_name. */
2248 const char *
2249 lang_decl_name (decl, v)
2250 tree decl;
2251 int v;
2253 if (v >= 2)
2254 return decl_as_string (decl, TFF_DECL_SPECIFIERS);
2256 reinit_global_formatting_buffer ();
2258 if (v == 1 && DECL_CLASS_SCOPE_P (decl))
2260 dump_type (CP_DECL_CONTEXT (decl), TFF_PLAIN_IDENTIFIER);
2261 print_scope_operator (scratch_buffer);
2264 if (TREE_CODE (decl) == FUNCTION_DECL)
2265 dump_function_name (decl, TFF_PLAIN_IDENTIFIER);
2266 else
2267 dump_decl (DECL_NAME (decl), TFF_PLAIN_IDENTIFIER);
2269 return output_finalize_message (scratch_buffer);
2272 const char *
2273 cp_file_of (t)
2274 tree t;
2276 if (TREE_CODE (t) == PARM_DECL && DECL_CONTEXT (t))
2277 return DECL_SOURCE_FILE (DECL_CONTEXT (t));
2278 else if (TYPE_P (t))
2279 return DECL_SOURCE_FILE (TYPE_MAIN_DECL (t));
2280 else if (TREE_CODE (t) == OVERLOAD)
2281 return DECL_SOURCE_FILE (OVL_FUNCTION (t));
2282 else
2283 return DECL_SOURCE_FILE (t);
2287 cp_line_of (t)
2288 tree t;
2290 int line = 0;
2291 if (TREE_CODE (t) == PARM_DECL && DECL_CONTEXT (t))
2292 line = DECL_SOURCE_LINE (DECL_CONTEXT (t));
2293 if (TREE_CODE (t) == TYPE_DECL && DECL_ARTIFICIAL (t)
2294 && TYPE_MAIN_DECL (TREE_TYPE (t)))
2295 t = TREE_TYPE (t);
2297 if (TYPE_P (t))
2298 line = DECL_SOURCE_LINE (TYPE_MAIN_DECL (t));
2299 else if (TREE_CODE (t) == OVERLOAD)
2300 line = DECL_SOURCE_LINE (OVL_FUNCTION (t));
2301 else
2302 line = DECL_SOURCE_LINE (t);
2304 if (line == 0)
2305 return lineno;
2307 return line;
2310 /* Now the interfaces from cp_error et al to dump_type et al. Each takes an
2311 on/off VERBOSE flag and supply the appropriate TFF_ flags to a dump_
2312 function. */
2314 static const char *
2315 decl_to_string (decl, verbose)
2316 tree decl;
2317 int verbose;
2319 int flags = 0;
2321 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == RECORD_TYPE
2322 || TREE_CODE (decl) == UNION_TYPE || TREE_CODE (decl) == ENUMERAL_TYPE)
2323 flags = TFF_CLASS_KEY_OR_ENUM;
2324 if (verbose)
2325 flags |= TFF_DECL_SPECIFIERS | TFF_FUNCTION_DEFAULT_ARGUMENTS;
2326 else if (TREE_CODE (decl) == FUNCTION_DECL)
2327 flags |= TFF_DECL_SPECIFIERS | TFF_RETURN_TYPE;
2328 flags |= TFF_TEMPLATE_HEADER;
2330 reinit_global_formatting_buffer ();
2332 dump_decl (decl, flags);
2334 return output_finalize_message (scratch_buffer);
2337 static const char *
2338 expr_to_string (decl, verbose)
2339 tree decl;
2340 int verbose ATTRIBUTE_UNUSED;
2342 reinit_global_formatting_buffer ();
2344 dump_expr (decl, 0);
2346 return output_finalize_message (scratch_buffer);
2349 static const char *
2350 fndecl_to_string (fndecl, verbose)
2351 tree fndecl;
2352 int verbose;
2354 int flags;
2356 flags = TFF_EXCEPTION_SPECIFICATION | TFF_DECL_SPECIFIERS;
2357 if (verbose)
2358 flags |= TFF_FUNCTION_DEFAULT_ARGUMENTS;
2359 reinit_global_formatting_buffer ();
2361 dump_decl (fndecl, flags);
2363 return output_finalize_message (scratch_buffer);
2367 static const char *
2368 code_to_string (c, v)
2369 enum tree_code c;
2370 int v ATTRIBUTE_UNUSED;
2372 return tree_code_name [c];
2375 const char *
2376 language_to_string (c, v)
2377 enum languages c;
2378 int v ATTRIBUTE_UNUSED;
2380 switch (c)
2382 case lang_c:
2383 return "C";
2385 case lang_cplusplus:
2386 return "C++";
2388 case lang_java:
2389 return "Java";
2391 default:
2392 my_friendly_abort (355);
2393 return 0;
2397 /* Return the proper printed version of a parameter to a C++ function. */
2399 static const char *
2400 parm_to_string (p, v)
2401 int p;
2402 int v ATTRIBUTE_UNUSED;
2404 if (p < 0)
2405 return "`this'";
2407 sprintf (digit_buffer, "%d", p+1);
2408 return digit_buffer;
2411 static const char *
2412 op_to_string (p, v)
2413 enum tree_code p;
2414 int v ATTRIBUTE_UNUSED;
2416 tree id;
2418 id = operator_name_info[(int) p].identifier;
2419 return id ? IDENTIFIER_POINTER (id) : "{unknown}";
2422 static const char *
2423 type_to_string (typ, verbose)
2424 tree typ;
2425 int verbose;
2427 int flags;
2429 flags = 0;
2430 if (verbose)
2431 flags |= TFF_CLASS_KEY_OR_ENUM;
2432 flags |= TFF_TEMPLATE_HEADER;
2434 reinit_global_formatting_buffer ();
2436 dump_type (typ, flags);
2438 return output_finalize_message (scratch_buffer);
2441 static const char *
2442 assop_to_string (p, v)
2443 enum tree_code p;
2444 int v ATTRIBUTE_UNUSED;
2446 tree id;
2448 id = assignment_operator_name_info[(int) p].identifier;
2449 return id ? IDENTIFIER_POINTER (id) : "{unknown}";
2452 static const char *
2453 args_to_string (p, verbose)
2454 tree p;
2455 int verbose;
2457 int flags = 0;
2458 if (verbose)
2459 flags |= TFF_CLASS_KEY_OR_ENUM;
2461 if (p == NULL_TREE)
2462 return "";
2464 if (TYPE_P (TREE_VALUE (p)))
2465 return type_as_string (p, flags);
2467 reinit_global_formatting_buffer ();
2468 for (; p; p = TREE_CHAIN (p))
2470 if (TREE_VALUE (p) == null_node)
2471 print_identifier (scratch_buffer, "NULL");
2472 else
2473 dump_type (error_type (TREE_VALUE (p)), flags);
2474 if (TREE_CHAIN (p))
2475 separate_with_comma (scratch_buffer);
2477 return output_finalize_message (scratch_buffer);
2480 static const char *
2481 cv_to_string (p, v)
2482 tree p;
2483 int v ATTRIBUTE_UNUSED;
2485 reinit_global_formatting_buffer ();
2487 dump_qualifiers (p, before);
2489 return output_finalize_message (scratch_buffer);
2492 static void
2493 lang_print_error_function (file)
2494 const char *file;
2496 output_state os;
2498 default_print_error_function (file);
2499 os = output_buffer_state (diagnostic_buffer);
2500 output_set_prefix (diagnostic_buffer, file);
2501 maybe_print_instantiation_context (diagnostic_buffer);
2502 output_buffer_state (diagnostic_buffer) = os;
2505 static void
2506 cp_diagnostic_starter (buffer, dc)
2507 output_buffer *buffer;
2508 diagnostic_context *dc;
2510 report_problematic_module (buffer);
2511 cp_print_error_function (buffer, dc);
2512 maybe_print_instantiation_context (buffer);
2513 output_set_prefix (buffer,
2514 context_as_prefix (diagnostic_file_location (dc),
2515 diagnostic_line_location (dc),
2516 diagnostic_is_warning (dc)));
2519 static void
2520 cp_diagnostic_finalizer (buffer, dc)
2521 output_buffer *buffer;
2522 diagnostic_context *dc __attribute__ ((__unused__));
2524 output_destroy_prefix (buffer);
2527 /* Print current function onto BUFFER, in the process of reporting
2528 a diagnostic message. Called from cp_diagnostic_starter. */
2529 static void
2530 cp_print_error_function (buffer, dc)
2531 output_buffer *buffer;
2532 diagnostic_context *dc;
2534 if (error_function_changed ())
2536 char *prefix = diagnostic_file_location (dc)
2537 ? file_name_as_prefix (diagnostic_file_location (dc))
2538 : NULL;
2539 output_state os;
2541 os = output_buffer_state (buffer);
2542 output_set_prefix (buffer, prefix);
2544 if (current_function_decl == NULL)
2545 output_add_string (buffer, "At global scope:");
2546 else
2547 output_printf
2548 (buffer, "In %s `%s':", function_category (current_function_decl),
2549 (*decl_printable_name) (current_function_decl, 2));
2550 output_add_newline (buffer);
2552 record_last_error_function ();
2553 output_destroy_prefix (buffer);
2554 output_buffer_state (buffer) = os;
2558 /* Returns a description of FUNCTION using standard terminology. */
2559 static const char *
2560 function_category (fn)
2561 tree fn;
2563 if (DECL_FUNCTION_MEMBER_P (fn))
2565 if (DECL_STATIC_FUNCTION_P (fn))
2566 return "static member function";
2567 else if (DECL_COPY_CONSTRUCTOR_P (fn))
2568 return "copy constructor";
2569 else if (DECL_CONSTRUCTOR_P (fn))
2570 return "constructor";
2571 else if (DECL_DESTRUCTOR_P (fn))
2572 return "destructor";
2573 else
2574 return "member function";
2576 else
2577 return "function";
2580 /* Report the full context of a current template instantiation,
2581 onto BUFFER. */
2582 static void
2583 print_instantiation_full_context (buffer)
2584 output_buffer *buffer;
2586 tree p = current_instantiation ();
2587 int line = lineno;
2588 const char *file = input_filename;
2590 if (p)
2592 if (current_function_decl != TINST_DECL (p)
2593 && current_function_decl != NULL_TREE)
2594 /* We can get here during the processing of some synthesized
2595 method. Then, TINST_DECL (p) will be the function that's causing
2596 the synthesis. */
2598 else
2600 if (current_function_decl == TINST_DECL (p))
2601 /* Avoid redundancy with the the "In function" line. */;
2602 else
2603 output_verbatim (buffer, "%s: In instantiation of `%s':\n", file,
2604 decl_as_string (TINST_DECL (p),
2605 TFF_DECL_SPECIFIERS | TFF_RETURN_TYPE));
2607 line = TINST_LINE (p);
2608 file = TINST_FILE (p);
2609 p = TREE_CHAIN (p);
2613 print_instantiation_partial_context (buffer, p, file, line);
2616 /* Same as above but less verbose. */
2617 static void
2618 print_instantiation_partial_context (buffer, t, file, line)
2619 output_buffer *buffer;
2620 tree t;
2621 const char *file;
2622 int line;
2624 for (; t; t = TREE_CHAIN (t))
2626 output_verbatim
2627 (buffer, "%s:%d: instantiated from `%s'\n", file, line,
2628 decl_as_string (TINST_DECL (t), TFF_DECL_SPECIFIERS | TFF_RETURN_TYPE));
2629 line = TINST_LINE (t);
2630 file = TINST_FILE (t);
2632 output_verbatim (buffer, "%s:%d: instantiated from here\n", file, line);
2635 /* Called from cp_thing to print the template context for an error. */
2636 static void
2637 maybe_print_instantiation_context (buffer)
2638 output_buffer *buffer;
2640 if (!problematic_instantiation_changed () || current_instantiation () == 0)
2641 return;
2643 record_last_problematic_instantiation ();
2644 print_instantiation_full_context (buffer);
2647 /* Report the bare minimum context of a template instantiation. */
2648 void
2649 print_instantiation_context ()
2651 print_instantiation_partial_context
2652 (diagnostic_buffer, current_instantiation (), input_filename, lineno);
2653 flush_diagnostic_buffer ();
2656 /* Called from output_format -- during diagnostic message processing --
2657 to handle C++ specific format specifier with the following meanings:
2658 %A function argument-list.
2659 %D declaration.
2660 %E expression.
2661 %F function declaration.
2662 %P function parameter whose position is indicated by an integer.
2663 %T type.
2664 %V cv-qualifier. */
2665 static int
2666 cp_tree_printer (buffer)
2667 output_buffer *buffer;
2669 int be_verbose = 0;
2670 tree_formatting_info tfi;
2672 memset (&tfi, 0, sizeof (tree_formatting_info));
2674 if (*output_buffer_text_cursor (buffer) == '+')
2675 ++output_buffer_text_cursor (buffer);
2676 if (*output_buffer_text_cursor (buffer) == '#')
2678 be_verbose = 1;
2679 ++output_buffer_text_cursor (buffer);
2682 switch (*output_buffer_text_cursor (buffer))
2684 case 'A':
2685 tree_being_formatted (&tfi) =
2686 va_arg (output_buffer_format_args (buffer), tree);
2687 if (be_verbose)
2688 tree_formatting_flags (&tfi) = TFF_SCOPE
2689 | TFF_FUNCTION_DEFAULT_ARGUMENTS;
2690 print_function_argument_list (buffer, &tfi);
2691 break;
2693 case 'D':
2694 tree_being_formatted (&tfi) =
2695 va_arg (output_buffer_format_args (buffer), tree);
2696 if (be_verbose)
2697 tree_formatting_flags (&tfi) = TFF_SCOPE | TFF_DECL_SPECIFIERS
2698 | TFF_CLASS_KEY_OR_ENUM | TFF_RETURN_TYPE
2699 | TFF_FUNCTION_DEFAULT_ARGUMENTS | TFF_TEMPLATE_DEFAULT_ARGUMENTS
2700 | TFF_EXCEPTION_SPECIFICATION | TFF_CHASE_NAMESPACE_ALIAS;
2701 print_declaration (buffer, &tfi);
2702 break;
2704 case 'E':
2705 tree_being_formatted (&tfi) =
2706 va_arg (output_buffer_format_args (buffer), tree);
2707 if (be_verbose)
2708 tree_formatting_flags (&tfi) = TFF_SCOPE;
2709 print_expression (buffer, &tfi);
2710 break;
2712 case 'F':
2713 tree_being_formatted (&tfi) =
2714 va_arg (output_buffer_format_args (buffer), tree);
2715 if (be_verbose)
2716 tree_formatting_flags (&tfi) = TFF_SCOPE | TFF_DECL_SPECIFIERS
2717 | TFF_RETURN_TYPE | TFF_FUNCTION_DEFAULT_ARGUMENTS
2718 | TFF_EXCEPTION_SPECIFICATION;
2719 print_function_declaration (buffer, &tfi);
2720 break;
2722 case 'P':
2723 print_function_parameter
2724 (buffer, va_arg (output_buffer_format_args (buffer), int));
2725 break;
2727 case 'T':
2728 tree_being_formatted (&tfi) =
2729 va_arg (output_buffer_format_args (buffer), tree);
2730 if (be_verbose)
2731 tree_formatting_flags (&tfi) = TFF_SCOPE | TFF_CLASS_KEY_OR_ENUM
2732 | TFF_RETURN_TYPE | TFF_EXCEPTION_SPECIFICATION;
2733 print_type_id (buffer, &tfi);
2734 break;
2736 case 'V':
2737 tree_being_formatted (&tfi) =
2738 va_arg (output_buffer_format_args (buffer), tree);
2739 print_cv_qualifier_seq (buffer, &tfi);
2740 break;
2742 default:
2743 return 0;
2746 return 1;
2749 /* Print a function argument-list represented by tree_being_formatted (TFI)
2750 onto BUFFER. */
2751 static void
2752 print_function_argument_list (buffer, tfi)
2753 output_buffer *buffer __attribute__ ((__unused__));
2754 tfi_t tfi __attribute__ ((__unused__));
2758 /* Print a declaration represented by tree_being_formatted (TFI)
2759 onto buffer. */
2760 static void
2761 print_declaration (buffer, tfi)
2762 output_buffer *buffer __attribute__ ((__unused__));
2763 tfi_t tfi __attribute__ ((__unused__));
2767 /* Print an expression represented by tree_being_formatted (TFI)
2768 onto BUFFER. */
2769 static void
2770 print_expression (buffer, tfi)
2771 output_buffer *buffer __attribute__ ((__unused__));
2772 tfi_t tfi __attribute__ ((__unused__));
2776 static void
2777 print_integer (buffer, i)
2778 output_buffer *buffer;
2779 HOST_WIDE_INT i;
2781 sprintf (digit_buffer, HOST_WIDE_INT_PRINT_DEC, (HOST_WIDE_INT) i);
2782 output_add_string (buffer, digit_buffer);
2785 /* Print a function declaration represented by tree_being_formatted (TFI)
2786 onto BUFFER. */
2787 static void
2788 print_function_declaration (buffer, tfi)
2789 output_buffer *buffer __attribute__ ((__unused__));
2790 tfi_t tfi __attribute__ ((__unused__));
2794 /* Print the N'th function parameter onto BUFFER. A negative value of N
2795 means the implicit "this" parameter of a member function. */
2796 static void
2797 print_function_parameter (buffer, n)
2798 output_buffer *buffer;
2799 int n;
2801 if (n < 0)
2802 print_identifier (buffer, "this");
2803 else
2804 output_decimal (buffer, n + 1);
2807 /* Print a type represented by tree_being_formatted (TFI) onto BUFFER. */
2808 static void
2809 print_type_id (buffer, tfi)
2810 output_buffer *buffer;
2811 tfi_t tfi;
2813 tree t = tree_being_formatted (tfi);
2814 int flags = tree_formatting_flags (tfi);
2815 if (t == NULL_TREE)
2816 return;
2818 if (flags & TFF_CHASE_TYPEDEF)
2819 tree_being_formatted (tfi) =
2820 typedef_original_name (tree_being_formatted (tfi));
2822 /* A type-id is of the form:
2823 type-id:
2824 type-specifier-seq abstract-declarator(opt) */
2825 print_type_specifier_seq (buffer, tfi);
2827 if (TYPE_PTRMEMFUNC_P (t))
2828 goto ptr_mem_fun;
2830 /* For types with abstract-declarator, print_type_specifier_seq prints
2831 the start of the abstract-declarator. Fiinish the job. */
2832 switch (TREE_CODE (t))
2834 case ARRAY_TYPE:
2835 case POINTER_TYPE:
2836 case REFERENCE_TYPE:
2837 case OFFSET_TYPE:
2838 case METHOD_TYPE:
2839 case FUNCTION_TYPE:
2840 ptr_mem_fun:
2841 print_rest_of_abstract_declarator (buffer, tfi);
2843 default:
2844 break;
2847 tree_being_formatted (tfi) = t;
2850 /* Print the type-specifier-seq part of a type-id. If appropriate, print
2851 also the prefix of the abstract-declarator. */
2852 static void
2853 print_type_specifier_seq (buffer, tfi)
2854 output_buffer *buffer;
2855 tfi_t tfi;
2857 int flags = tree_formatting_flags (tfi);
2858 tree t = tree_being_formatted (tfi);
2859 enum tree_code code = TREE_CODE (t);
2861 /* A type-speficier-seq is:
2862 type-specifier type-specifier-seq(opt)
2863 where
2864 type-specifier:
2865 simple-type-specifier
2866 class-specifier
2867 enum-specifier
2868 elaborated-type-specifier
2869 cv-qualifier
2871 We do not, however, pretty-print class-specifier nor enum-specifier. */
2873 switch (code)
2875 case UNKNOWN_TYPE:
2876 case IDENTIFIER_NODE:
2877 case VOID_TYPE:
2878 case INTEGER_TYPE:
2879 case REAL_TYPE:
2880 case COMPLEX_TYPE:
2881 case ENUMERAL_TYPE:
2882 case BOOLEAN_TYPE:
2883 case UNION_TYPE:
2884 case TYPE_DECL:
2885 case TEMPLATE_DECL:
2886 case TEMPLATE_TYPE_PARM:
2887 case TYPEOF_TYPE:
2888 case TEMPLATE_TEMPLATE_PARM:
2889 case TYPENAME_TYPE:
2890 class_type:
2891 print_cv_qualifier_seq (buffer, tfi);
2892 if ((flags & TFF_DECL_SPECIFIERS)
2893 && (code == TYPENAME_TYPE || IS_AGGR_TYPE (t)))
2894 print_elaborated_type_specifier (buffer, tfi);
2895 else
2896 print_simple_type_specifier (buffer, tfi);
2897 break;
2899 /* Because the abstract-declarator can modify the type-specifier-seq
2900 in a highly non linear manner, we pretty-print its prefix here.
2901 The suffix part is handled by print_rest_of_abstract_declarator. */
2903 /* A RECORD_TYPE is also used to represent a pointer to member
2904 function. */
2905 case RECORD_TYPE:
2906 if (TYPE_PTRMEMFUNC_P (t))
2908 /* Print the return type. */
2909 tree_being_formatted (tfi) =
2910 TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (t));
2911 print_type_id (buffer, tfi);
2912 print_whitespace (buffer, tfi);
2914 /* Then the beginning of the abstract-declarator part. */
2915 tree_being_formatted (tfi) =
2916 TYPE_METHOD_BASETYPE (TYPE_PTRMEMFUNC_FN_TYPE (t));
2917 print_left_paren (buffer);
2918 print_nested_name_specifier (buffer, tfi);
2920 else
2921 goto class_type;
2922 break;
2924 case POINTER_TYPE:
2925 if (TYPE_PTRMEM_P (t))
2926 goto ptr_data_member;
2927 else
2928 goto non_ptr_data_member;
2929 break;
2931 case ARRAY_TYPE:
2932 case REFERENCE_TYPE:
2933 case FUNCTION_TYPE:
2934 case METHOD_TYPE:
2935 non_ptr_data_member:
2936 tree_being_formatted (tfi) = TREE_TYPE (t);
2937 print_type_specifier_seq (buffer, tfi);
2938 if (code == POINTER_TYPE || code == REFERENCE_TYPE)
2940 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
2941 print_left_paren (buffer);
2943 else if (code == FUNCTION_TYPE || code == METHOD_TYPE)
2945 print_whitespace (buffer, tfi);
2946 print_left_paren (buffer);
2947 if (code == METHOD_TYPE)
2949 tree_being_formatted (tfi) = TYPE_METHOD_BASETYPE (t);
2950 print_nested_name_specifier (buffer, tfi);
2951 tree_being_formatted (tfi) = t;
2954 tree_being_formatted (tfi) = t;
2955 break;
2957 ptr_data_member:
2958 case OFFSET_TYPE:
2959 /* Firstly, the type of the member. */
2960 tree_being_formatted (tfi) = TREE_TYPE (t);
2961 print_type_id (buffer, tfi);
2962 print_whitespace (buffer, tfi);
2964 /* Then, the containing class. */
2965 tree_being_formatted (tfi) = TYPE_OFFSET_BASETYPE (t);
2966 print_nested_name_specifier (buffer, tfi);
2967 tree_being_formatted (tfi) = t;
2968 break;
2970 default:
2971 sorry_for_unsupported_tree (t);
2972 /* fall throught */
2974 case ERROR_MARK:
2975 print_identifier (buffer, "{type-specifier-seq error}");
2976 break;
2979 tree_being_formatted (tfi) = t;
2982 /* Print the simpe-type-specifier component of a type-specifier. */
2983 static void
2984 print_simple_type_specifier (buffer, tfi)
2985 output_buffer *buffer;
2986 tfi_t tfi;
2988 int flags = tree_formatting_flags (tfi);
2989 tree t = tree_being_formatted (tfi);
2990 enum tree_code code = TREE_CODE (t);
2992 switch (code)
2994 case UNKNOWN_TYPE:
2995 print_identifier (buffer, "{unknown type}");
2996 break;
2998 case IDENTIFIER_NODE:
2999 print_tree_identifier (buffer, t);
3000 break;
3002 case COMPLEX_TYPE:
3003 print_identifier (buffer, "__complex__ ");
3004 tree_being_formatted (tfi) = TREE_TYPE (t);
3005 print_type_id (buffer, tfi);
3006 break;
3008 case TYPENAME_TYPE:
3009 tree_being_formatted (tfi) = TYPE_CONTEXT (t);
3010 print_nested_name_specifier (buffer, tfi);
3011 tree_being_formatted (tfi) = TYPENAME_TYPE_FULLNAME (t);
3012 tree_formatting_flags (tfi) |= ~TFF_CHASE_TYPEDEF;
3013 print_type_id (buffer, tfi);
3014 break;
3016 case TYPEOF_TYPE:
3017 print_identifier (buffer, "__typeof__");
3018 tree_being_formatted (tfi) = TYPE_FIELDS (t);
3019 print_left_paren (buffer);
3020 print_expression (buffer, tfi);
3021 print_right_paren (buffer);
3022 break;
3024 case INTEGER_TYPE:
3025 if (TREE_UNSIGNED (t))
3027 if (TYPE_MAIN_VARIANT (t) == integer_type_node)
3028 /* We don't want pedantry like `unsigned int'. */;
3029 else if (!TREE_UNSIGNED (TYPE_MAIN_VARIANT (t)))
3031 print_identifier (buffer, "unsigned");
3032 print_whitespace (buffer, tfi);
3035 else if (TYPE_MAIN_VARIANT (t) == char_type_node)
3037 print_identifier (buffer, "signed");
3038 print_whitespace (buffer, tfi);
3040 case REAL_TYPE:
3041 case BOOLEAN_TYPE:
3042 case VOID_TYPE:
3044 tree s = (flags & TFF_CHASE_TYPEDEF) ? TYPE_MAIN_VARIANT (t) : t;
3046 if (TYPE_NAME (s) && TYPE_IDENTIFIER (s))
3047 print_tree_identifier (buffer, TYPE_IDENTIFIER (s));
3048 else
3049 /* Types like intQI_type_node and friends have no names.
3050 These don't come up in user error messages, but it's nice
3051 to be able to print them from the debugger. */
3052 print_identifier (buffer, "{anonymous}");
3054 break;
3056 case TEMPLATE_TEMPLATE_PARM:
3057 if (TYPE_IDENTIFIER (t))
3058 print_tree_identifier (buffer, TYPE_IDENTIFIER (t));
3059 else
3060 print_identifier (buffer, "{anonymous template template parameter}");
3061 break;
3063 case TYPE_DECL:
3064 if (flags & TFF_CHASE_TYPEDEF)
3065 print_type_id (buffer, tfi);
3066 else
3067 print_tree_identifier (buffer, DECL_NAME (t));
3068 break;
3070 case BOUND_TEMPLATE_TEMPLATE_PARM:
3071 case TEMPLATE_DECL:
3072 print_template_id (buffer, tfi);
3073 break;
3075 case TEMPLATE_TYPE_PARM:
3076 if (TYPE_IDENTIFIER (t))
3077 print_tree_identifier (buffer, TYPE_IDENTIFIER (t));
3078 else
3079 print_identifier (buffer, "{anonymous template type parameter}");
3080 break;
3082 default:
3083 break;
3086 tree_being_formatted (tfi) = t;
3087 tree_formatting_flags (tfi) = flags;
3090 /* Print the elaborated-type-specifier form of a type-specifier. */
3091 static void
3092 print_elaborated_type_specifier (buffer, tfi)
3093 output_buffer *buffer;
3094 tfi_t tfi;
3096 int flags = tree_formatting_flags (tfi);
3097 tree t = tree_being_formatted (tfi);
3099 switch (TREE_CODE (t))
3101 case TYPENAME_TYPE:
3102 print_identifier (buffer, "typename");
3103 print_whitespace (buffer, tfi);
3104 tree_formatting_flags (tfi) |= ~TFF_DECL_SPECIFIERS;
3105 print_simple_type_specifier (buffer, tfi);
3106 break;
3108 case UNION_TYPE:
3109 case RECORD_TYPE:
3111 tree name = NULL_TREE;
3113 if (flags & TFF_CHASE_TYPEDEF)
3114 tree_being_formatted (tfi) = typedef_original_name (t);
3116 print_identifier
3117 (buffer, class_key_or_enum (tree_being_formatted (tfi)));
3118 print_whitespace (buffer, tfi);
3120 name = TYPE_NAME (tree_being_formatted (tfi));
3121 if (name)
3123 if (flags & TFF_SCOPE)
3125 tree_being_formatted (tfi) = CP_DECL_CONTEXT (name);
3126 print_nested_name_specifier (buffer, tfi);
3128 print_tree_identifier (buffer, DECL_NAME (name));
3130 else
3131 print_identifier (buffer, "{anonymous}");
3133 break;
3135 default:
3136 sorry_for_unsupported_tree (t);
3137 break;
3140 tree_being_formatted (tfi) = t;
3141 tree_formatting_flags (tfi) = flags;
3144 /* Finish the job of printing the abstract-declarator part of a
3145 type-id. */
3146 static void
3147 print_rest_of_abstract_declarator (buffer, tfi)
3148 output_buffer *buffer;
3149 tfi_t tfi;
3151 tree t = tree_being_formatted (tfi);
3152 enum tree_code code = TREE_CODE (t);
3154 /* An abstract-declarator has the form:
3156 abstract-declarator:
3157 ptr-operator abstract-declarator(opt)
3158 direct-abstract-declarator
3160 direct-abstract-declarator:
3161 direct-abstract-declarator(opt)
3162 ( parameter-declaration-clause ) cv-qualifier-seq(opt)
3163 exception-specification(opt)
3164 direct-abstract-declarator(opt) [ constant-expression(opt) ]
3165 ( direct-abstract-declarator ) */
3167 switch (code)
3169 case ARRAY_TYPE:
3170 print_left_bracket (buffer);
3171 if (TYPE_DOMAIN (t))
3173 tree s = TYPE_DOMAIN (t);
3175 if (host_integerp (TYPE_MAX_VALUE (s), 0))
3176 output_decimal (buffer, tree_low_cst (TYPE_MAX_VALUE (s), 0) + 1);
3177 else if (TREE_CODE (TYPE_MAX_VALUE (s)) == MINUS_EXPR)
3179 tree_being_formatted (tfi) =
3180 TREE_OPERAND (TYPE_MAX_VALUE (s), 0);
3181 print_expression (buffer, tfi);
3182 tree_being_formatted (tfi) = t;
3184 else
3186 tree_being_formatted (tfi) = fold
3187 (cp_build_binary_op (PLUS_EXPR, TYPE_MAX_VALUE (s),
3188 integer_one_node));
3189 print_expression (buffer, tfi);
3190 tree_being_formatted (tfi) = t;
3193 print_right_bracket (buffer);
3194 put_whitespace (tfi) = none;
3195 tree_being_formatted (tfi) = TREE_TYPE (t);
3196 print_rest_of_abstract_declarator (buffer, tfi);
3197 tree_being_formatted (tfi) = t;
3198 break;
3200 case POINTER_TYPE:
3201 case REFERENCE_TYPE:
3202 case OFFSET_TYPE:
3203 if (code == POINTER_TYPE || code == REFERENCE_TYPE)
3205 output_add_character (buffer, "&*"[code == POINTER_TYPE]);
3206 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
3207 print_right_paren (buffer);
3209 put_whitespace (tfi) = before;
3210 print_cv_qualifier_seq (buffer, tfi);
3211 tree_being_formatted (tfi) = TREE_TYPE (t);
3212 print_rest_of_abstract_declarator (buffer, tfi);
3213 tree_being_formatted (tfi) = t;
3214 break;
3216 case FUNCTION_TYPE:
3217 case METHOD_TYPE:
3218 print_right_paren (buffer);
3219 print_whitespace (buffer, tfi);
3221 /* Skip the `this' implicit parameter if present. */
3222 tree_being_formatted (tfi) = TYPE_ARG_TYPES (t);
3223 if (code == METHOD_TYPE)
3224 tree_being_formatted (tfi) = TREE_CHAIN (tree_being_formatted (tfi));
3226 /* Print the parameter-list. */
3227 print_left_paren (buffer);
3228 print_parameter_declaration_clause (buffer, tfi);
3229 print_right_paren (buffer);
3231 print_whitespace (buffer, tfi);
3233 if (code == METHOD_TYPE)
3235 tree_being_formatted (tfi) =
3236 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t)));
3237 print_cv_qualifier_seq (buffer, tfi);
3240 /* Finish the abstract-declarator. */
3241 tree_being_formatted (tfi) = TREE_TYPE (t);
3242 print_rest_of_abstract_declarator (buffer, tfi);
3244 /* Print the exception-specification for documentaion purpose. */
3245 tree_being_formatted (tfi) = TYPE_RAISES_EXCEPTIONS (t);
3246 print_exception_specification (buffer, tfi);
3247 tree_being_formatted (tfi) = t;
3248 break;
3250 /* These types don't have abstract-declarator. */
3251 case UNKNOWN_TYPE:
3252 case IDENTIFIER_NODE:
3253 case VOID_TYPE:
3254 case INTEGER_TYPE:
3255 case REAL_TYPE:
3256 case COMPLEX_TYPE:
3257 case ENUMERAL_TYPE:
3258 case BOOLEAN_TYPE:
3259 case UNION_TYPE:
3260 case TYPE_DECL:
3261 case TEMPLATE_DECL:
3262 case TEMPLATE_TYPE_PARM:
3263 case TYPEOF_TYPE:
3264 case TEMPLATE_TEMPLATE_PARM:
3265 case TYPENAME_TYPE:
3266 break;
3268 default:
3269 sorry_for_unsupported_tree (t);
3270 /* fall throught. */
3271 case ERROR_MARK:
3272 break;
3276 /* Print the cv-quafilers of tree_being_formatted (TFI) onto BUFFER. */
3277 static void
3278 print_cv_qualifier_seq (buffer, tfi)
3279 output_buffer *buffer;
3280 tree_formatting_info *tfi;
3282 int cv = TYPE_QUALS (tree_being_formatted (tfi));
3283 int pad_after = after == put_whitespace (tfi);
3284 static const int mask[]
3285 = {TYPE_QUAL_CONST, TYPE_QUAL_VOLATILE, TYPE_QUAL_RESTRICT};
3286 static const char *const qualifier[]
3287 = { "const", "volatile", "__restrict__" };
3289 if (cv != 0)
3291 int i;
3292 for (i = 0; i != 3; ++i)
3293 if (mask[i] & cv)
3295 if (put_whitespace (tfi) == before)
3296 output_add_space (buffer);
3297 print_identifier (buffer, qualifier[i]);
3298 put_whitespace (tfi) = before;
3301 if (pad_after)
3303 output_add_space (buffer);
3304 put_whitespace (tfi) = none;
3309 static void
3310 print_parameter_declaration_clause (buffer, tfi)
3311 output_buffer *buffer __attribute__ ((__unused__));
3312 tfi_t tfi __attribute__ ((__unused__));
3316 static void
3317 print_exception_specification (buffer, tfi)
3318 output_buffer *buffer __attribute__ ((__unused__));
3319 tfi_t tfi __attribute__ ((__unused__));
3323 static void
3324 print_nested_name_specifier (buffer, tfi)
3325 output_buffer *buffer;
3326 tfi_t tfi;
3328 int flags = tree_formatting_flags (tfi);
3329 tree t = tree_being_formatted (tfi);
3330 /* A nested-name-specifier is:
3331 class-or-namespace-name :: nested-name-specifier(opt)
3332 class-or-namespace-name :: template nested-name-specifier
3334 The latter form being the correct syntax for a name designating
3335 a template member, where the preceding class-or-namespace-name part
3336 is name-dependent. For the time being, we do not do such a
3337 sophisticated pretty-printing.
3339 class-or-namespace-name:
3340 class-name
3341 namespace-name */
3343 if (t == NULL_TREE || t == global_namespace)
3344 return;
3346 if (CLASS_TYPE_P (t) && !(flags & TFF_CLASS_SCOPE))
3347 return;
3349 if (TREE_CODE (t) == NAMESPACE_DECL && !(flags & TFF_NAMESPACE_SCOPE))
3350 return;
3352 tree_being_formatted (tfi) = DECL_CONTEXT (t);
3353 print_nested_name_specifier (buffer, tfi);
3354 print_scope_operator (buffer);
3355 if (TREE_CODE (t) == NAMESPACE_DECL)
3356 print_tree_identifier (buffer, DECL_NAME (t));
3357 else if (CLASS_TYPE_P (t))
3359 if (!DECL_USE_TEMPLATE (t))
3360 print_tree_identifier (buffer, TYPE_IDENTIFIER (t));
3361 else
3363 tree_being_formatted (tfi) = t;
3364 print_template_id (buffer, tfi);
3368 tree_being_formatted (tfi) = t;
3371 static void
3372 print_template_id (buffer, tfi)
3373 output_buffer *buffer;
3374 tfi_t tfi __attribute__ ((__unused__));
3376 print_template_argument_list_start (buffer);
3377 /* ... */
3378 print_template_argument_list_end (buffer);
3381 static tree
3382 typedef_original_name (t)
3383 tree t;
3385 return DECL_ORIGINAL_TYPE (t) ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t);
3388 static void
3389 print_non_consecutive_character (buffer, c)
3390 output_buffer *buffer;
3391 int c;
3393 const char *p = output_last_position (buffer);
3395 if (p != NULL && *p == c)
3396 output_add_space (buffer);
3397 output_add_character (buffer, c);