* lib/objc.exp: Add -lposix4 on Solaris 2.6 and Solaris 2.7.
[official-gcc.git] / gcc / cp / error.c
bloba30a4b7067859b52ecfaa158a592e7a81f84b489
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 #define GLOBAL_THING "_GLOBAL__"
878 static void
879 dump_global_iord (t)
880 tree t;
882 const char *p = NULL;
884 if (DECL_GLOBAL_CTOR_P (t))
885 p = "initializers";
886 else if (DECL_GLOBAL_DTOR_P (t))
887 p = "destructors";
888 else
889 my_friendly_abort (352);
891 output_printf (scratch_buffer, "(static %s for %s)", p, input_filename);
894 static void
895 dump_simple_decl (t, type, flags)
896 tree t;
897 tree type;
898 int flags;
900 if (flags & TFF_DECL_SPECIFIERS)
902 if (dump_type_prefix (type, flags) != none)
903 output_add_space (scratch_buffer);
905 if (!DECL_INITIAL (t) || TREE_CODE (DECL_INITIAL (t)) != TEMPLATE_PARM_INDEX)
906 dump_scope (CP_DECL_CONTEXT (t), flags);
907 if (DECL_NAME (t))
908 dump_decl (DECL_NAME (t), flags);
909 else
910 print_identifier (scratch_buffer, "<anonymous>");
911 if (flags & TFF_DECL_SPECIFIERS)
912 dump_type_suffix (type, flags);
915 /* Dump a human readable string for the decl T under control of FLAGS. */
917 static void
918 dump_decl (t, flags)
919 tree t;
920 int flags;
922 if (t == NULL_TREE)
923 return;
925 switch (TREE_CODE (t))
927 case TYPE_DECL:
929 /* Don't say 'typedef class A' */
930 if (DECL_ARTIFICIAL (t))
932 if ((flags & TFF_DECL_SPECIFIERS)
933 && TREE_CODE (TREE_TYPE (t)) == TEMPLATE_TYPE_PARM)
934 /* Say `class T' not just `T'. */
935 output_add_string (scratch_buffer, "class ");
937 dump_type (TREE_TYPE (t), flags);
938 break;
941 if (flags & TFF_DECL_SPECIFIERS)
942 output_add_string (scratch_buffer, "typedef ");
943 dump_simple_decl (t, DECL_ORIGINAL_TYPE (t)
944 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t),
945 flags);
946 break;
948 case VAR_DECL:
949 if (DECL_NAME (t) && VTABLE_NAME_P (DECL_NAME (t)))
951 output_add_string (scratch_buffer, "vtable for ");
952 if (TYPE_P (DECL_CONTEXT (t)))
953 dump_type (DECL_CONTEXT (t), flags);
954 else
955 /* This case can arise with -fno-vtable-thunks. See
956 expand_upcast_fixups. It's not clear what to print
957 here. */
958 print_identifier (scratch_buffer, "<unknown type>");
959 break;
961 /* else fall through */
962 case FIELD_DECL:
963 case PARM_DECL:
964 dump_simple_decl (t, TREE_TYPE (t), flags);
965 break;
967 case RESULT_DECL:
968 output_add_string (scratch_buffer, "<return value> ");
969 dump_simple_decl (t, TREE_TYPE (t), flags);
970 break;
972 case NAMESPACE_DECL:
973 dump_scope (CP_DECL_CONTEXT (t), flags);
974 if (DECL_NAME (t) == anonymous_namespace_name)
975 print_identifier (scratch_buffer, "<unnamed>");
976 else
977 print_tree_identifier (scratch_buffer, DECL_NAME (t));
978 break;
980 case SCOPE_REF:
981 dump_decl (TREE_OPERAND (t, 0), flags & ~TFF_DECL_SPECIFIERS);
982 print_scope_operator (scratch_buffer);
983 dump_decl (TREE_OPERAND (t, 1), flags);
984 break;
986 case ARRAY_REF:
987 dump_decl (TREE_OPERAND (t, 0), flags);
988 print_left_bracket (scratch_buffer);
989 dump_decl (TREE_OPERAND (t, 1), flags);
990 print_right_bracket (scratch_buffer);
991 break;
993 /* So that we can do dump_decl on an aggr type. */
994 case RECORD_TYPE:
995 case UNION_TYPE:
996 case ENUMERAL_TYPE:
997 dump_type (t, flags);
998 break;
1000 case TYPE_EXPR:
1001 my_friendly_abort (69);
1002 break;
1004 /* These special cases are duplicated here so that other functions
1005 can feed identifiers to cp_error and get them demangled properly. */
1006 case IDENTIFIER_NODE:
1007 { tree f;
1008 if (DESTRUCTOR_NAME_P (t)
1009 && (f = ident_fndecl (t))
1010 && DECL_LANGUAGE (f) == lang_cplusplus)
1012 output_add_character (scratch_buffer, '~');
1013 dump_decl (DECL_NAME (f), flags);
1015 else if (IDENTIFIER_TYPENAME_P (t))
1017 output_add_string (scratch_buffer, "operator ");
1018 /* Not exactly IDENTIFIER_TYPE_VALUE. */
1019 dump_type (TREE_TYPE (t), flags);
1020 break;
1022 else
1023 print_tree_identifier (scratch_buffer, t);
1025 break;
1027 case OVERLOAD:
1028 t = OVL_CURRENT (t);
1029 /* Fall through. */
1031 case FUNCTION_DECL:
1032 if (DECL_GLOBAL_CTOR_P (t) || DECL_GLOBAL_DTOR_P (t))
1033 dump_global_iord (t);
1034 else if (! DECL_LANG_SPECIFIC (t))
1035 print_identifier (scratch_buffer, "<internal>");
1036 else
1037 dump_function_decl (t, flags);
1038 break;
1040 case TEMPLATE_DECL:
1041 dump_template_decl (t, flags);
1042 break;
1044 case TEMPLATE_ID_EXPR:
1046 tree args;
1047 tree name = TREE_OPERAND (t, 0);
1048 if (is_overloaded_fn (name))
1049 name = DECL_NAME (get_first_fn (name));
1050 dump_decl (name, flags);
1051 print_template_argument_list_start (scratch_buffer);
1052 for (args = TREE_OPERAND (t, 1); args; args = TREE_CHAIN (args))
1054 dump_template_argument (TREE_VALUE (args), flags);
1055 if (TREE_CHAIN (args))
1056 separate_with_comma (scratch_buffer);
1058 print_template_argument_list_end (scratch_buffer);
1060 break;
1062 case LOOKUP_EXPR:
1063 dump_decl (TREE_OPERAND (t, 0), flags);
1064 break;
1066 case LABEL_DECL:
1067 print_tree_identifier (scratch_buffer, DECL_NAME (t));
1068 break;
1070 case CONST_DECL:
1071 if ((TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == ENUMERAL_TYPE)
1072 || (DECL_INITIAL (t) &&
1073 TREE_CODE (DECL_INITIAL (t)) == TEMPLATE_PARM_INDEX))
1074 dump_simple_decl (t, TREE_TYPE (t), flags);
1075 else if (DECL_NAME (t))
1076 dump_decl (DECL_NAME (t), flags);
1077 else if (DECL_INITIAL (t))
1078 dump_expr (DECL_INITIAL (t), flags | TFF_EXPR_IN_PARENS);
1079 else
1080 print_identifier (scratch_buffer, "enumerator");
1081 break;
1083 case USING_DECL:
1084 output_add_string (scratch_buffer, "using ");
1085 dump_type (DECL_INITIAL (t), flags);
1086 print_scope_operator (scratch_buffer);
1087 print_tree_identifier (scratch_buffer, DECL_NAME (t));
1088 break;
1090 default:
1091 sorry_for_unsupported_tree (t);
1092 /* Fallthrough to error. */
1094 case ERROR_MARK:
1095 print_identifier (scratch_buffer, "<declaration error>");
1096 break;
1100 /* Dump a template declaration T under control of FLAGS. This means the
1101 'template <...> leaders plus the 'class X' or 'void fn(...)' part. */
1103 static void
1104 dump_template_decl (t, flags)
1105 tree t;
1106 int flags;
1108 tree orig_parms = DECL_TEMPLATE_PARMS (t);
1109 tree parms;
1110 int i;
1112 if (flags & TFF_TEMPLATE_HEADER)
1114 for (parms = orig_parms = nreverse (orig_parms);
1115 parms;
1116 parms = TREE_CHAIN (parms))
1118 tree inner_parms = INNERMOST_TEMPLATE_PARMS (parms);
1119 int len = TREE_VEC_LENGTH (inner_parms);
1121 output_add_string (scratch_buffer, "template<");
1122 for (i = 0; i < len; i++)
1124 if (i)
1125 separate_with_comma (scratch_buffer);
1126 dump_template_parameter (TREE_VEC_ELT (inner_parms, i), flags);
1128 print_template_argument_list_end (scratch_buffer);
1129 output_add_space (scratch_buffer);
1131 nreverse(orig_parms);
1132 /* If we've shown the template<args> prefix, we'd better show the
1133 decl's type too. */
1134 flags |= TFF_DECL_SPECIFIERS;
1136 if (TREE_CODE (DECL_TEMPLATE_RESULT (t)) == TYPE_DECL)
1137 dump_type (TREE_TYPE (t),
1138 ((flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1139 | (flags & TFF_DECL_SPECIFIERS ? TFF_CLASS_KEY_OR_ENUM : 0)));
1140 else if (TREE_CODE (DECL_TEMPLATE_RESULT (t)) == VAR_DECL)
1141 dump_decl (DECL_TEMPLATE_RESULT (t), flags | TFF_TEMPLATE_NAME);
1142 else if (TREE_TYPE (t) == NULL_TREE)
1143 my_friendly_abort (353);
1144 else
1145 switch (NEXT_CODE (t))
1147 case METHOD_TYPE:
1148 case FUNCTION_TYPE:
1149 dump_function_decl (t, flags | TFF_TEMPLATE_NAME);
1150 break;
1151 default:
1152 /* This case can occur with some illegal code. */
1153 dump_type (TREE_TYPE (t),
1154 (flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1155 | (flags & TFF_DECL_SPECIFIERS ? TFF_CLASS_KEY_OR_ENUM : 0));
1159 /* Pretty print a function decl. There are several ways we want to print a
1160 function declaration. The TFF_ bits in FLAGS tells us how to behave.
1161 As cp_error can only apply the '#' flag once to give 0 and 1 for V, there
1162 is %D which doesn't print the throw specs, and %F which does. */
1164 static void
1165 dump_function_decl (t, flags)
1166 tree t;
1167 int flags;
1169 tree fntype;
1170 tree parmtypes;
1171 tree cname = NULL_TREE;
1172 tree template_args = NULL_TREE;
1173 tree template_parms = NULL_TREE;
1174 int show_return = flags & TFF_RETURN_TYPE || flags & TFF_DECL_SPECIFIERS;
1176 if (TREE_CODE (t) == TEMPLATE_DECL)
1177 t = DECL_TEMPLATE_RESULT (t);
1179 /* Pretty print template instantiations only. */
1180 if (DECL_USE_TEMPLATE (t) && DECL_TEMPLATE_INFO (t))
1182 tree tmpl;
1184 template_args = DECL_TI_ARGS (t);
1185 tmpl = most_general_template (t);
1186 if (tmpl && TREE_CODE (tmpl) == TEMPLATE_DECL)
1188 template_parms = DECL_TEMPLATE_PARMS (tmpl);
1189 t = tmpl;
1193 fntype = TREE_TYPE (t);
1194 parmtypes = FUNCTION_FIRST_USER_PARMTYPE (t);
1196 if (DECL_CLASS_SCOPE_P (t))
1197 cname = DECL_CONTEXT (t);
1198 /* this is for partially instantiated template methods */
1199 else if (TREE_CODE (fntype) == METHOD_TYPE)
1200 cname = TREE_TYPE (TREE_VALUE (parmtypes));
1202 if (!(flags & TFF_DECL_SPECIFIERS))
1203 /* OK */;
1204 else if (DECL_STATIC_FUNCTION_P (t))
1205 print_identifier (scratch_buffer, "static ");
1206 else if (DECL_VIRTUAL_P (t))
1207 print_identifier (scratch_buffer, "virtual ");
1209 /* Print the return type? */
1210 if (show_return)
1211 show_return = !DECL_CONV_FN_P (t) && !DECL_CONSTRUCTOR_P (t)
1212 && !DECL_DESTRUCTOR_P (t);
1213 if (show_return)
1215 dump_type_prefix (TREE_TYPE (fntype), flags);
1216 output_add_space (scratch_buffer);
1219 /* Print the function name. */
1220 if (cname)
1222 dump_type (cname, flags);
1223 print_scope_operator (scratch_buffer);
1225 else
1226 dump_scope (CP_DECL_CONTEXT (t), flags);
1228 dump_function_name (t, flags);
1230 if (flags & TFF_DECL_SPECIFIERS)
1232 dump_parameters (parmtypes, flags);
1234 if (show_return)
1235 dump_type_suffix (TREE_TYPE (fntype), flags);
1237 if (TREE_CODE (fntype) == METHOD_TYPE)
1238 dump_qualifiers (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fntype))),
1239 before);
1241 if (flags & TFF_EXCEPTION_SPECIFICATION)
1242 dump_exception_spec (TYPE_RAISES_EXCEPTIONS (fntype), flags);
1245 /* If T is a template instantiation, dump the parameter binding. */
1246 if (template_parms != NULL_TREE && template_args != NULL_TREE)
1248 output_add_string (scratch_buffer, " [with ");
1249 dump_template_bindings (template_parms, template_args);
1250 print_right_bracket (scratch_buffer);
1254 /* Print a parameter list. If this is for a member function, the
1255 member object ptr (and any other hidden args) should have
1256 already been removed. */
1258 static void
1259 dump_parameters (parmtypes, flags)
1260 tree parmtypes;
1261 int flags;
1263 int first;
1265 print_left_paren (scratch_buffer);
1267 for (first = 1; parmtypes != void_list_node;
1268 parmtypes = TREE_CHAIN (parmtypes))
1270 if (!first)
1271 separate_with_comma (scratch_buffer);
1272 first = 0;
1273 if (!parmtypes)
1275 print_identifier (scratch_buffer, "...");
1276 break;
1278 dump_type (TREE_VALUE (parmtypes), flags);
1280 if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && TREE_PURPOSE (parmtypes))
1282 output_add_string (scratch_buffer, " = ");
1283 dump_expr (TREE_PURPOSE (parmtypes), flags | TFF_EXPR_IN_PARENS);
1287 print_right_paren (scratch_buffer);
1290 /* Print an exception specification. T is the exception specification. */
1292 static void
1293 dump_exception_spec (t, flags)
1294 tree t;
1295 int flags;
1297 if (t)
1299 output_add_string (scratch_buffer, " throw (");
1300 if (TREE_VALUE (t) != NULL_TREE)
1301 while (1)
1303 dump_type (TREE_VALUE (t), flags);
1304 t = TREE_CHAIN (t);
1305 if (!t)
1306 break;
1307 separate_with_comma (scratch_buffer);
1309 print_right_paren (scratch_buffer);
1313 /* Handle the function name for a FUNCTION_DECL node, grokking operators
1314 and destructors properly. */
1316 static void
1317 dump_function_name (t, flags)
1318 tree t;
1319 int flags;
1321 tree name = DECL_NAME (t);
1323 /* Don't let the user see __comp_ctor et al. */
1324 if (DECL_CONSTRUCTOR_P (t)
1325 || DECL_DESTRUCTOR_P (t))
1326 name = constructor_name (DECL_CONTEXT (t));
1328 if (DECL_DESTRUCTOR_P (t))
1330 output_add_character (scratch_buffer, '~');
1331 dump_decl (name, TFF_PLAIN_IDENTIFIER);
1333 else if (DECL_CONV_FN_P (t))
1335 /* This cannot use the hack that the operator's return
1336 type is stashed off of its name because it may be
1337 used for error reporting. In the case of conflicting
1338 declarations, both will have the same name, yet
1339 the types will be different, hence the TREE_TYPE field
1340 of the first name will be clobbered by the second. */
1341 output_add_string (scratch_buffer, "operator ");
1342 dump_type (TREE_TYPE (TREE_TYPE (t)), flags);
1344 else if (IDENTIFIER_OPNAME_P (name))
1345 print_tree_identifier (scratch_buffer, name);
1346 else
1347 dump_decl (name, flags);
1349 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
1350 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (t)
1351 && (DECL_TEMPLATE_SPECIALIZATION (t)
1352 || TREE_CODE (DECL_TI_TEMPLATE (t)) != TEMPLATE_DECL
1353 || DECL_TEMPLATE_SPECIALIZATION (DECL_TI_TEMPLATE (t))
1354 || PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
1355 dump_template_parms (DECL_TEMPLATE_INFO (t), !DECL_USE_TEMPLATE (t), flags);
1358 /* Dump the template parameters from the template info INFO under control of
1359 FLAGS. PRIMARY indicates whether this is a primary template decl, or
1360 specialization (partial or complete). For partial specializations we show
1361 the specialized parameter values. For a primary template we show no
1362 decoration. */
1364 static void
1365 dump_template_parms (info, primary, flags)
1366 tree info;
1367 int primary;
1368 int flags;
1370 tree args = info ? TI_ARGS (info) : NULL_TREE;
1372 if (primary && flags & TFF_TEMPLATE_NAME)
1373 return;
1374 flags &= ~(TFF_CLASS_KEY_OR_ENUM | TFF_TEMPLATE_NAME);
1375 print_template_argument_list_start (scratch_buffer);
1377 /* Be careful only to print things when we have them, so as not
1378 to crash producing error messages. */
1379 if (args && !primary)
1381 int len = 0;
1382 int ix = 0;
1383 int need_comma = 0;
1385 if (TREE_CODE (args) == TREE_VEC)
1387 if (TREE_VEC_LENGTH (args) > 0
1388 && TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
1389 args = TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 1);
1391 len = TREE_VEC_LENGTH (args);
1393 else if (TREE_CODE (args) == TREE_LIST)
1394 len = -1;
1395 while (ix != len && args)
1397 tree arg;
1398 if (len >= 0)
1400 arg = TREE_VEC_ELT (args, ix);
1401 ix++;
1403 else
1405 arg = TREE_VALUE (args);
1406 args = TREE_CHAIN (args);
1408 if (need_comma)
1409 separate_with_comma (scratch_buffer);
1411 if (!arg)
1412 print_identifier (scratch_buffer, "<template parameter error>");
1413 else
1414 dump_template_argument (arg, flags);
1415 need_comma = 1;
1418 else if (primary)
1420 tree tpl = TI_TEMPLATE (info);
1421 tree parms = DECL_TEMPLATE_PARMS (tpl);
1422 int len, ix;
1424 parms = TREE_CODE (parms) == TREE_LIST ? TREE_VALUE (parms) : NULL_TREE;
1425 len = parms ? TREE_VEC_LENGTH (parms) : 0;
1427 for (ix = 0; ix != len; ix++)
1429 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, ix));
1431 if (ix)
1432 separate_with_comma (scratch_buffer);
1434 dump_decl (parm, flags & ~TFF_DECL_SPECIFIERS);
1437 print_template_argument_list_end (scratch_buffer);
1440 static void
1441 dump_char (c)
1442 int c;
1444 switch (c)
1446 case TARGET_NEWLINE:
1447 output_add_string (scratch_buffer, "\\n");
1448 break;
1449 case TARGET_TAB:
1450 output_add_string (scratch_buffer, "\\t");
1451 break;
1452 case TARGET_VT:
1453 output_add_string (scratch_buffer, "\\v");
1454 break;
1455 case TARGET_BS:
1456 output_add_string (scratch_buffer, "\\b");
1457 break;
1458 case TARGET_CR:
1459 output_add_string (scratch_buffer, "\\r");
1460 break;
1461 case TARGET_FF:
1462 output_add_string (scratch_buffer, "\\f");
1463 break;
1464 case TARGET_BELL:
1465 output_add_string (scratch_buffer, "\\a");
1466 break;
1467 case '\\':
1468 output_add_string (scratch_buffer, "\\\\");
1469 break;
1470 case '\'':
1471 output_add_string (scratch_buffer, "\\'");
1472 break;
1473 case '\"':
1474 output_add_string (scratch_buffer, "\\\"");
1475 break;
1476 default:
1477 if (ISPRINT (c))
1478 output_add_character (scratch_buffer, c);
1479 else
1481 sprintf (digit_buffer, "\\%03o", (int) c);
1482 output_add_string (scratch_buffer, digit_buffer);
1487 /* Print out a list of initializers (subr of dump_expr) */
1489 static void
1490 dump_expr_list (l, flags)
1491 tree l;
1492 int flags;
1494 while (l)
1496 dump_expr (TREE_VALUE (l), flags | TFF_EXPR_IN_PARENS);
1497 l = TREE_CHAIN (l);
1498 if (l)
1499 separate_with_comma (scratch_buffer);
1503 /* Print out an expression E under control of FLAGS. */
1505 static void
1506 dump_expr (t, flags)
1507 tree t;
1508 int flags;
1510 switch (TREE_CODE (t))
1512 case VAR_DECL:
1513 case PARM_DECL:
1514 case FIELD_DECL:
1515 case CONST_DECL:
1516 case FUNCTION_DECL:
1517 case TEMPLATE_DECL:
1518 case NAMESPACE_DECL:
1519 case OVERLOAD:
1520 dump_decl (t, flags & ~TFF_DECL_SPECIFIERS);
1521 break;
1523 case INTEGER_CST:
1525 tree type = TREE_TYPE (t);
1526 my_friendly_assert (type != 0, 81);
1528 /* If it's an enum, output its tag, rather than its value. */
1529 if (TREE_CODE (type) == ENUMERAL_TYPE)
1531 tree values = TYPE_VALUES (type);
1533 for (; values;
1534 values = TREE_CHAIN (values))
1535 if (tree_int_cst_equal (TREE_VALUE (values), t))
1536 break;
1538 if (values)
1539 print_tree_identifier (scratch_buffer, TREE_PURPOSE (values));
1540 else
1542 /* Value must have been cast. */
1543 print_left_paren (scratch_buffer);
1544 dump_type (type, flags);
1545 print_right_paren (scratch_buffer);
1546 goto do_int;
1549 else if (type == boolean_type_node)
1551 if (t == boolean_false_node || integer_zerop (t))
1552 print_identifier (scratch_buffer, "false");
1553 else if (t == boolean_true_node)
1554 print_identifier (scratch_buffer, "true");
1556 else if (type == char_type_node)
1558 output_add_character (scratch_buffer, '\'');
1559 dump_char (tree_low_cst (t, 0));
1560 output_add_character (scratch_buffer, '\'');
1562 else
1564 do_int:
1565 if (! host_integerp (t, 0))
1567 tree val = t;
1569 if (tree_int_cst_sgn (val) < 0)
1571 output_add_character (scratch_buffer, '-');
1572 val = build_int_2 (-TREE_INT_CST_LOW (val),
1573 ~TREE_INT_CST_HIGH (val)
1574 + !TREE_INT_CST_LOW (val));
1576 /* Would "%x%0*x" or "%x%*0x" get zero-padding on all
1577 systems? */
1579 static char format[10]; /* "%x%09999x\0" */
1580 if (!format[0])
1581 sprintf (format, "%%x%%0%dx", HOST_BITS_PER_INT / 4);
1582 sprintf (digit_buffer, format, TREE_INT_CST_HIGH (val),
1583 TREE_INT_CST_LOW (val));
1584 output_add_string (scratch_buffer, digit_buffer);
1587 else
1588 print_integer (scratch_buffer, TREE_INT_CST_LOW (t));
1591 break;
1593 case REAL_CST:
1594 #ifndef REAL_IS_NOT_DOUBLE
1595 sprintf (digit_buffer, "%g", TREE_REAL_CST (t));
1596 #else
1598 const unsigned char *p = (const unsigned char *) &TREE_REAL_CST (t);
1599 size_t i;
1600 strcpy (digit_buffer, "0x");
1601 for (i = 0; i < sizeof TREE_REAL_CST (t); i++)
1602 sprintf (digit_buffer + 2 + 2*i, "%02x", *p++);
1604 #endif
1605 output_add_string (scratch_buffer, digit_buffer);
1606 break;
1608 case PTRMEM_CST:
1609 output_add_character (scratch_buffer, '&');
1610 dump_type (PTRMEM_CST_CLASS (t), flags);
1611 print_scope_operator (scratch_buffer);
1612 print_tree_identifier
1613 (scratch_buffer, DECL_NAME (PTRMEM_CST_MEMBER (t)));
1614 break;
1616 case STRING_CST:
1618 const char *p = TREE_STRING_POINTER (t);
1619 int len = TREE_STRING_LENGTH (t) - 1;
1620 int i;
1622 output_add_character (scratch_buffer, '\"');
1623 for (i = 0; i < len; i++)
1624 dump_char (p[i]);
1625 output_add_character (scratch_buffer, '\"');
1627 break;
1629 case COMPOUND_EXPR:
1630 print_left_paren (scratch_buffer);
1631 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1632 separate_with_comma (scratch_buffer);
1633 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
1634 print_right_paren (scratch_buffer);
1635 break;
1637 case COND_EXPR:
1638 print_left_paren (scratch_buffer);
1639 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1640 output_add_string (scratch_buffer, " ? ");
1641 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
1642 output_add_string (scratch_buffer, " : ");
1643 dump_expr (TREE_OPERAND (t, 2), flags | TFF_EXPR_IN_PARENS);
1644 print_right_paren (scratch_buffer);
1645 break;
1647 case SAVE_EXPR:
1648 if (TREE_HAS_CONSTRUCTOR (t))
1650 output_add_string (scratch_buffer, "new ");
1651 dump_type (TREE_TYPE (TREE_TYPE (t)), flags);
1653 else
1655 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1657 break;
1659 case AGGR_INIT_EXPR:
1661 tree fn = NULL_TREE;
1663 if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR)
1664 fn = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
1666 if (fn && TREE_CODE (fn) == FUNCTION_DECL)
1668 if (DECL_CONSTRUCTOR_P (fn))
1669 print_tree_identifier
1670 (scratch_buffer, TYPE_IDENTIFIER (TREE_TYPE (t)));
1671 else
1672 dump_decl (fn, 0);
1674 else
1675 dump_expr (TREE_OPERAND (t, 0), 0);
1677 print_left_paren (scratch_buffer);
1678 if (TREE_OPERAND (t, 1))
1679 dump_expr_list (TREE_CHAIN (TREE_OPERAND (t, 1)), flags);
1680 print_right_paren (scratch_buffer);
1681 break;
1683 case CALL_EXPR:
1685 tree fn = TREE_OPERAND (t, 0);
1686 tree args = TREE_OPERAND (t, 1);
1688 if (TREE_CODE (fn) == ADDR_EXPR)
1689 fn = TREE_OPERAND (fn, 0);
1691 if (TREE_TYPE (fn) != NULL_TREE && NEXT_CODE (fn) == METHOD_TYPE)
1693 tree ob = TREE_VALUE (args);
1694 if (TREE_CODE (ob) == ADDR_EXPR)
1696 dump_expr (TREE_OPERAND (ob, 0), flags | TFF_EXPR_IN_PARENS);
1697 output_add_character (scratch_buffer, '.');
1699 else if (TREE_CODE (ob) != PARM_DECL
1700 || strcmp (IDENTIFIER_POINTER (DECL_NAME (ob)), "this"))
1702 dump_expr (ob, flags | TFF_EXPR_IN_PARENS);
1703 output_add_string (scratch_buffer, "->");
1705 args = TREE_CHAIN (args);
1707 dump_expr (fn, flags | TFF_EXPR_IN_PARENS);
1708 print_left_paren (scratch_buffer);
1709 dump_expr_list (args, flags);
1710 print_right_paren (scratch_buffer);
1712 break;
1714 case NEW_EXPR:
1716 tree type = TREE_OPERAND (t, 1);
1717 if (NEW_EXPR_USE_GLOBAL (t))
1718 print_scope_operator (scratch_buffer);
1719 output_add_string (scratch_buffer, "new ");
1720 if (TREE_OPERAND (t, 0))
1722 print_left_paren (scratch_buffer);
1723 dump_expr_list (TREE_OPERAND (t, 0), flags);
1724 output_add_string (scratch_buffer, ") ");
1726 if (TREE_CODE (type) == ARRAY_REF)
1727 type = build_cplus_array_type
1728 (TREE_OPERAND (type, 0),
1729 build_index_type (fold (build (MINUS_EXPR, integer_type_node,
1730 TREE_OPERAND (type, 1),
1731 integer_one_node))));
1732 dump_type (type, flags);
1733 if (TREE_OPERAND (t, 2))
1735 print_left_paren (scratch_buffer);
1736 dump_expr_list (TREE_OPERAND (t, 2), flags);
1737 print_right_paren (scratch_buffer);
1740 break;
1742 case TARGET_EXPR:
1743 /* Note that this only works for G++ target exprs. If somebody
1744 builds a general TARGET_EXPR, there's no way to represent that
1745 it initializes anything other that the parameter slot for the
1746 default argument. Note we may have cleared out the first
1747 operand in expand_expr, so don't go killing ourselves. */
1748 if (TREE_OPERAND (t, 1))
1749 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
1750 break;
1752 case INIT_EXPR:
1753 case MODIFY_EXPR:
1754 case PLUS_EXPR:
1755 case MINUS_EXPR:
1756 case MULT_EXPR:
1757 case TRUNC_DIV_EXPR:
1758 case TRUNC_MOD_EXPR:
1759 case MIN_EXPR:
1760 case MAX_EXPR:
1761 case LSHIFT_EXPR:
1762 case RSHIFT_EXPR:
1763 case BIT_IOR_EXPR:
1764 case BIT_XOR_EXPR:
1765 case BIT_AND_EXPR:
1766 case BIT_ANDTC_EXPR:
1767 case TRUTH_ANDIF_EXPR:
1768 case TRUTH_ORIF_EXPR:
1769 case LT_EXPR:
1770 case LE_EXPR:
1771 case GT_EXPR:
1772 case GE_EXPR:
1773 case EQ_EXPR:
1774 case NE_EXPR:
1775 case EXACT_DIV_EXPR:
1776 dump_binary_op (operator_name_info[(int) TREE_CODE (t)].name, t, flags);
1777 break;
1779 case CEIL_DIV_EXPR:
1780 case FLOOR_DIV_EXPR:
1781 case ROUND_DIV_EXPR:
1782 dump_binary_op ("/", t, flags);
1783 break;
1785 case CEIL_MOD_EXPR:
1786 case FLOOR_MOD_EXPR:
1787 case ROUND_MOD_EXPR:
1788 dump_binary_op ("%", t, flags);
1789 break;
1791 case COMPONENT_REF:
1793 tree ob = TREE_OPERAND (t, 0);
1794 if (TREE_CODE (ob) == INDIRECT_REF)
1796 ob = TREE_OPERAND (ob, 0);
1797 if (TREE_CODE (ob) != PARM_DECL
1798 || strcmp (IDENTIFIER_POINTER (DECL_NAME (ob)), "this"))
1800 dump_expr (ob, flags | TFF_EXPR_IN_PARENS);
1801 output_add_string (scratch_buffer, "->");
1804 else
1806 dump_expr (ob, flags | TFF_EXPR_IN_PARENS);
1807 output_add_character (scratch_buffer, '.');
1809 dump_expr (TREE_OPERAND (t, 1), flags & ~TFF_EXPR_IN_PARENS);
1811 break;
1813 case ARRAY_REF:
1814 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1815 print_left_bracket (scratch_buffer);
1816 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
1817 print_right_bracket (scratch_buffer);
1818 break;
1820 case CONVERT_EXPR:
1821 if (VOID_TYPE_P (TREE_TYPE (t)))
1823 print_left_paren (scratch_buffer);
1824 dump_type (TREE_TYPE (t), flags);
1825 print_right_paren (scratch_buffer);
1826 dump_expr (TREE_OPERAND (t, 0), flags);
1828 else
1829 dump_unary_op ("+", t, flags);
1830 break;
1832 case ADDR_EXPR:
1833 if (TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
1834 || TREE_CODE (TREE_OPERAND (t, 0)) == STRING_CST
1835 /* An ADDR_EXPR can have reference type. In that case, we
1836 shouldn't print the `&' doing so indicates to the user
1837 that the expression has pointer type. */
1838 || (TREE_TYPE (t)
1839 && TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE))
1840 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1841 else
1842 dump_unary_op ("&", t, flags);
1843 break;
1845 case INDIRECT_REF:
1846 if (TREE_HAS_CONSTRUCTOR (t))
1848 t = TREE_OPERAND (t, 0);
1849 my_friendly_assert (TREE_CODE (t) == CALL_EXPR, 237);
1850 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1851 print_left_paren (scratch_buffer);
1852 dump_expr_list (TREE_CHAIN (TREE_OPERAND (t, 1)), flags);
1853 print_right_paren (scratch_buffer);
1855 else
1857 if (TREE_OPERAND (t,0) != NULL_TREE
1858 && TREE_TYPE (TREE_OPERAND (t, 0))
1859 && NEXT_CODE (TREE_OPERAND (t, 0)) == REFERENCE_TYPE)
1860 dump_expr (TREE_OPERAND (t, 0), flags);
1861 else
1862 dump_unary_op ("*", t, flags);
1864 break;
1866 case NEGATE_EXPR:
1867 case BIT_NOT_EXPR:
1868 case TRUTH_NOT_EXPR:
1869 case PREDECREMENT_EXPR:
1870 case PREINCREMENT_EXPR:
1871 dump_unary_op (operator_name_info [(int)TREE_CODE (t)].name, t, flags);
1872 break;
1874 case POSTDECREMENT_EXPR:
1875 case POSTINCREMENT_EXPR:
1876 print_left_paren (scratch_buffer);
1877 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1878 print_identifier
1879 (scratch_buffer, operator_name_info[(int)TREE_CODE (t)].name);
1880 print_right_paren (scratch_buffer);
1881 break;
1883 case NON_LVALUE_EXPR:
1884 /* FIXME: This is a KLUDGE workaround for a parsing problem. There
1885 should be another level of INDIRECT_REF so that I don't have to do
1886 this. */
1887 if (TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == POINTER_TYPE)
1889 tree next = TREE_TYPE (TREE_TYPE (t));
1891 while (TREE_CODE (next) == POINTER_TYPE)
1892 next = TREE_TYPE (next);
1894 if (TREE_CODE (next) == FUNCTION_TYPE)
1896 if (flags & TFF_EXPR_IN_PARENS)
1897 print_left_paren (scratch_buffer);
1898 output_add_character (scratch_buffer, '*');
1899 dump_expr (TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
1900 if (flags & TFF_EXPR_IN_PARENS)
1901 print_right_paren (scratch_buffer);
1902 break;
1904 /* else FALLTHRU */
1906 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1907 break;
1909 case NOP_EXPR:
1910 dump_expr (TREE_OPERAND (t, 0), flags);
1911 break;
1913 case EXPR_WITH_FILE_LOCATION:
1914 dump_expr (EXPR_WFL_NODE (t), flags);
1915 break;
1917 case CONSTRUCTOR:
1918 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
1920 tree idx = build_component_ref (t, index_identifier, NULL_TREE, 0);
1922 if (integer_all_onesp (idx))
1924 tree pfn = PFN_FROM_PTRMEMFUNC (t);
1925 dump_unary_op ("&", pfn, flags | TFF_EXPR_IN_PARENS);
1926 break;
1928 else if (TREE_CODE (idx) == INTEGER_CST
1929 && tree_int_cst_equal (idx, integer_zero_node))
1931 /* A NULL pointer-to-member constant. */
1932 output_add_string (scratch_buffer, "((");
1933 dump_type (TREE_TYPE (t), flags);
1934 output_add_string (scratch_buffer, ") 0)");
1935 break;
1937 else if (host_integerp (idx, 0))
1939 tree virtuals;
1940 unsigned HOST_WIDE_INT n;
1942 t = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
1943 t = TYPE_METHOD_BASETYPE (t);
1944 virtuals = TYPE_BINFO_VIRTUALS (TYPE_MAIN_VARIANT (t));
1946 n = tree_low_cst (idx, 0);
1948 /* Map vtable index back one, to allow for the null pointer to
1949 member. */
1950 --n;
1952 while (n > 0 && virtuals)
1954 --n;
1955 virtuals = TREE_CHAIN (virtuals);
1957 if (virtuals)
1959 dump_expr (BV_FN (virtuals),
1960 flags | TFF_EXPR_IN_PARENS);
1961 break;
1965 output_add_character (scratch_buffer, '{');
1966 dump_expr_list (CONSTRUCTOR_ELTS (t), flags);
1967 output_add_character (scratch_buffer, '}');
1968 break;
1970 case OFFSET_REF:
1972 tree ob = TREE_OPERAND (t, 0);
1973 if (is_dummy_object (ob))
1975 t = TREE_OPERAND (t, 1);
1976 if (TREE_CODE (t) == FUNCTION_DECL)
1977 /* A::f */
1978 dump_expr (t, flags | TFF_EXPR_IN_PARENS);
1979 else if (BASELINK_P (t))
1980 dump_expr (OVL_CURRENT (TREE_VALUE (t)), flags | TFF_EXPR_IN_PARENS);
1981 else
1982 dump_decl (t, flags);
1984 else
1986 if (TREE_CODE (ob) == INDIRECT_REF)
1988 dump_expr (TREE_OPERAND (ob, 0), flags | TFF_EXPR_IN_PARENS);
1989 output_add_string (scratch_buffer, "->*");
1991 else
1993 dump_expr (ob, flags | TFF_EXPR_IN_PARENS);
1994 output_add_string (scratch_buffer, ".*");
1996 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
1998 break;
2001 case TEMPLATE_PARM_INDEX:
2002 dump_decl (TEMPLATE_PARM_DECL (t), flags & ~TFF_DECL_SPECIFIERS);
2003 break;
2005 case IDENTIFIER_NODE:
2006 print_tree_identifier (scratch_buffer, t);
2007 break;
2009 case SCOPE_REF:
2010 dump_type (TREE_OPERAND (t, 0), flags);
2011 print_scope_operator (scratch_buffer);
2012 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2013 break;
2015 case CAST_EXPR:
2016 if (TREE_OPERAND (t, 0) == NULL_TREE
2017 || TREE_CHAIN (TREE_OPERAND (t, 0)))
2019 dump_type (TREE_TYPE (t), flags);
2020 print_left_paren (scratch_buffer);
2021 dump_expr_list (TREE_OPERAND (t, 0), flags);
2022 print_right_paren (scratch_buffer);
2024 else
2026 print_left_paren (scratch_buffer);
2027 dump_type (TREE_TYPE (t), flags);
2028 output_add_string (scratch_buffer, ")(");
2029 dump_expr_list (TREE_OPERAND (t, 0), flags);
2030 print_right_paren (scratch_buffer);
2032 break;
2034 case STATIC_CAST_EXPR:
2035 output_add_string (scratch_buffer, "static_cast<");
2036 goto cast;
2037 case REINTERPRET_CAST_EXPR:
2038 output_add_string (scratch_buffer, "reinterpret_cast<");
2039 goto cast;
2040 case CONST_CAST_EXPR:
2041 output_add_string (scratch_buffer, "const_cast<");
2042 goto cast;
2043 case DYNAMIC_CAST_EXPR:
2044 output_add_string (scratch_buffer, "dynamic_cast<");
2045 cast:
2046 dump_type (TREE_TYPE (t), flags);
2047 output_add_string (scratch_buffer, ">(");
2048 dump_expr (TREE_OPERAND (t, 0), flags);
2049 print_right_paren (scratch_buffer);
2050 break;
2052 case LOOKUP_EXPR:
2053 print_tree_identifier (scratch_buffer, TREE_OPERAND (t, 0));
2054 break;
2056 case ARROW_EXPR:
2057 dump_expr (TREE_OPERAND (t, 0), flags);
2058 output_add_string (scratch_buffer, "->");
2059 break;
2061 case SIZEOF_EXPR:
2062 case ALIGNOF_EXPR:
2063 if (TREE_CODE (t) == SIZEOF_EXPR)
2064 output_add_string (scratch_buffer, "sizeof (");
2065 else
2067 my_friendly_assert (TREE_CODE (t) == ALIGNOF_EXPR, 0);
2068 output_add_string (scratch_buffer, "__alignof__ (");
2070 if (TYPE_P (TREE_OPERAND (t, 0)))
2071 dump_type (TREE_OPERAND (t, 0), flags);
2072 else
2073 dump_unary_op ("*", t, flags | TFF_EXPR_IN_PARENS);
2074 print_right_paren (scratch_buffer);
2075 break;
2077 case DEFAULT_ARG:
2078 print_identifier (scratch_buffer, "<unparsed>");
2079 break;
2081 case TRY_CATCH_EXPR:
2082 case WITH_CLEANUP_EXPR:
2083 case CLEANUP_POINT_EXPR:
2084 dump_expr (TREE_OPERAND (t, 0), flags);
2085 break;
2087 case PSEUDO_DTOR_EXPR:
2088 dump_expr (TREE_OPERAND (t, 2), flags);
2089 output_add_character (scratch_buffer, '.');
2090 dump_type (TREE_OPERAND (t, 0), flags);
2091 output_add_string (scratch_buffer, "::~");
2092 dump_type (TREE_OPERAND (t, 1), flags);
2093 break;
2095 case TEMPLATE_ID_EXPR:
2096 dump_decl (t, flags);
2097 break;
2099 case STMT_EXPR:
2100 /* We don't yet have a way of dumping statements in a
2101 human-readable format. */
2102 output_add_string (scratch_buffer, "({...})");
2103 break;
2105 case BIND_EXPR:
2106 output_add_character (scratch_buffer, '{');
2107 dump_expr (TREE_OPERAND (t, 1), flags & ~TFF_EXPR_IN_PARENS);
2108 output_add_character (scratch_buffer, '}');
2109 break;
2111 case LOOP_EXPR:
2112 output_add_string (scratch_buffer, "while (1) { ");
2113 dump_expr (TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2114 output_add_character (scratch_buffer, '}');
2115 break;
2117 case EXIT_EXPR:
2118 output_add_string (scratch_buffer, "if (");
2119 dump_expr (TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2120 output_add_string (scratch_buffer, ") break; ");
2121 break;
2123 case TREE_LIST:
2124 if (TREE_VALUE (t) && TREE_CODE (TREE_VALUE (t)) == FUNCTION_DECL)
2126 print_tree_identifier (scratch_buffer, DECL_NAME (TREE_VALUE (t)));
2127 break;
2129 /* else fall through */
2131 /* This list is incomplete, but should suffice for now.
2132 It is very important that `sorry' does not call
2133 `report_error_function'. That could cause an infinite loop. */
2134 default:
2135 sorry_for_unsupported_tree (t);
2136 /* fall through to ERROR_MARK... */
2137 case ERROR_MARK:
2138 print_identifier (scratch_buffer, "<expression error>");
2139 break;
2143 static void
2144 dump_binary_op (opstring, t, flags)
2145 const char *opstring;
2146 tree t;
2147 int flags;
2149 print_left_paren (scratch_buffer);
2150 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2151 output_add_space (scratch_buffer);
2152 if (opstring)
2153 print_identifier (scratch_buffer, opstring);
2154 else
2155 print_identifier (scratch_buffer, "<unknown operator>");
2156 output_add_space (scratch_buffer);
2157 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2158 print_right_paren (scratch_buffer);
2161 static void
2162 dump_unary_op (opstring, t, flags)
2163 const char *opstring;
2164 tree t;
2165 int flags;
2167 if (flags & TFF_EXPR_IN_PARENS)
2168 print_left_paren (scratch_buffer);
2169 print_identifier (scratch_buffer, opstring);
2170 dump_expr (TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2171 if (flags & TFF_EXPR_IN_PARENS)
2172 print_right_paren (scratch_buffer);
2175 /* Exported interface to stringifying types, exprs and decls under TFF_*
2176 control. */
2178 const char *
2179 type_as_string (typ, flags)
2180 tree typ;
2181 int flags;
2183 reinit_global_formatting_buffer ();
2185 dump_type (typ, flags);
2187 return output_finalize_message (scratch_buffer);
2190 const char *
2191 expr_as_string (decl, flags)
2192 tree decl;
2193 int flags;
2195 reinit_global_formatting_buffer ();
2197 dump_expr (decl, flags);
2199 return output_finalize_message (scratch_buffer);
2202 const char *
2203 decl_as_string (decl, flags)
2204 tree decl;
2205 int flags;
2207 reinit_global_formatting_buffer ();
2209 dump_decl (decl, flags);
2211 return output_finalize_message (scratch_buffer);
2214 const char *
2215 context_as_string (context, flags)
2216 tree context;
2217 int flags;
2219 reinit_global_formatting_buffer ();
2221 dump_scope (context, flags);
2223 return output_finalize_message (scratch_buffer);
2226 /* Generate the three forms of printable names for lang_printable_name. */
2228 const char *
2229 lang_decl_name (decl, v)
2230 tree decl;
2231 int v;
2233 if (v >= 2)
2234 return decl_as_string (decl, TFF_DECL_SPECIFIERS);
2236 reinit_global_formatting_buffer ();
2238 if (v == 1 && DECL_CLASS_SCOPE_P (decl))
2240 dump_type (CP_DECL_CONTEXT (decl), TFF_PLAIN_IDENTIFIER);
2241 print_scope_operator (scratch_buffer);
2244 if (TREE_CODE (decl) == FUNCTION_DECL)
2245 dump_function_name (decl, TFF_PLAIN_IDENTIFIER);
2246 else
2247 dump_decl (DECL_NAME (decl), TFF_PLAIN_IDENTIFIER);
2249 return output_finalize_message (scratch_buffer);
2252 const char *
2253 cp_file_of (t)
2254 tree t;
2256 if (TREE_CODE (t) == PARM_DECL && DECL_CONTEXT (t))
2257 return DECL_SOURCE_FILE (DECL_CONTEXT (t));
2258 else if (TYPE_P (t))
2259 return DECL_SOURCE_FILE (TYPE_MAIN_DECL (t));
2260 else if (TREE_CODE (t) == OVERLOAD)
2261 return DECL_SOURCE_FILE (OVL_FUNCTION (t));
2262 else
2263 return DECL_SOURCE_FILE (t);
2267 cp_line_of (t)
2268 tree t;
2270 int line = 0;
2271 if (TREE_CODE (t) == PARM_DECL && DECL_CONTEXT (t))
2272 line = DECL_SOURCE_LINE (DECL_CONTEXT (t));
2273 if (TREE_CODE (t) == TYPE_DECL && DECL_ARTIFICIAL (t)
2274 && TYPE_MAIN_DECL (TREE_TYPE (t)))
2275 t = TREE_TYPE (t);
2277 if (TYPE_P (t))
2278 line = DECL_SOURCE_LINE (TYPE_MAIN_DECL (t));
2279 else if (TREE_CODE (t) == OVERLOAD)
2280 line = DECL_SOURCE_LINE (OVL_FUNCTION (t));
2281 else
2282 line = DECL_SOURCE_LINE (t);
2284 if (line == 0)
2285 return lineno;
2287 return line;
2290 /* Now the interfaces from cp_error et al to dump_type et al. Each takes an
2291 on/off VERBOSE flag and supply the appropriate TFF_ flags to a dump_
2292 function. */
2294 static const char *
2295 decl_to_string (decl, verbose)
2296 tree decl;
2297 int verbose;
2299 int flags = 0;
2301 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == RECORD_TYPE
2302 || TREE_CODE (decl) == UNION_TYPE || TREE_CODE (decl) == ENUMERAL_TYPE)
2303 flags = TFF_CLASS_KEY_OR_ENUM;
2304 if (verbose)
2305 flags |= TFF_DECL_SPECIFIERS | TFF_FUNCTION_DEFAULT_ARGUMENTS;
2306 else if (TREE_CODE (decl) == FUNCTION_DECL)
2307 flags |= TFF_DECL_SPECIFIERS | TFF_RETURN_TYPE;
2308 flags |= TFF_TEMPLATE_HEADER;
2310 reinit_global_formatting_buffer ();
2312 dump_decl (decl, flags);
2314 return output_finalize_message (scratch_buffer);
2317 static const char *
2318 expr_to_string (decl, verbose)
2319 tree decl;
2320 int verbose ATTRIBUTE_UNUSED;
2322 reinit_global_formatting_buffer ();
2324 dump_expr (decl, 0);
2326 return output_finalize_message (scratch_buffer);
2329 static const char *
2330 fndecl_to_string (fndecl, verbose)
2331 tree fndecl;
2332 int verbose;
2334 int flags;
2336 flags = TFF_EXCEPTION_SPECIFICATION | TFF_DECL_SPECIFIERS;
2337 if (verbose)
2338 flags |= TFF_FUNCTION_DEFAULT_ARGUMENTS;
2339 reinit_global_formatting_buffer ();
2341 dump_decl (fndecl, flags);
2343 return output_finalize_message (scratch_buffer);
2347 static const char *
2348 code_to_string (c, v)
2349 enum tree_code c;
2350 int v ATTRIBUTE_UNUSED;
2352 return tree_code_name [c];
2355 const char *
2356 language_to_string (c, v)
2357 enum languages c;
2358 int v ATTRIBUTE_UNUSED;
2360 switch (c)
2362 case lang_c:
2363 return "C";
2365 case lang_cplusplus:
2366 return "C++";
2368 case lang_java:
2369 return "Java";
2371 default:
2372 my_friendly_abort (355);
2373 return 0;
2377 /* Return the proper printed version of a parameter to a C++ function. */
2379 static const char *
2380 parm_to_string (p, v)
2381 int p;
2382 int v ATTRIBUTE_UNUSED;
2384 if (p < 0)
2385 return "`this'";
2387 sprintf (digit_buffer, "%d", p+1);
2388 return digit_buffer;
2391 static const char *
2392 op_to_string (p, v)
2393 enum tree_code p;
2394 int v ATTRIBUTE_UNUSED;
2396 tree id;
2398 id = operator_name_info[(int) p].identifier;
2399 return id ? IDENTIFIER_POINTER (id) : "{unknown}";
2402 static const char *
2403 type_to_string (typ, verbose)
2404 tree typ;
2405 int verbose;
2407 int flags;
2409 flags = 0;
2410 if (verbose)
2411 flags |= TFF_CLASS_KEY_OR_ENUM;
2412 flags |= TFF_TEMPLATE_HEADER;
2414 reinit_global_formatting_buffer ();
2416 dump_type (typ, flags);
2418 return output_finalize_message (scratch_buffer);
2421 static const char *
2422 assop_to_string (p, v)
2423 enum tree_code p;
2424 int v ATTRIBUTE_UNUSED;
2426 tree id;
2428 id = assignment_operator_name_info[(int) p].identifier;
2429 return id ? IDENTIFIER_POINTER (id) : "{unknown}";
2432 static const char *
2433 args_to_string (p, verbose)
2434 tree p;
2435 int verbose;
2437 int flags = 0;
2438 if (verbose)
2439 flags |= TFF_CLASS_KEY_OR_ENUM;
2441 if (p == NULL_TREE)
2442 return "";
2444 if (TYPE_P (TREE_VALUE (p)))
2445 return type_as_string (p, flags);
2447 reinit_global_formatting_buffer ();
2448 for (; p; p = TREE_CHAIN (p))
2450 if (TREE_VALUE (p) == null_node)
2451 print_identifier (scratch_buffer, "NULL");
2452 else
2453 dump_type (error_type (TREE_VALUE (p)), flags);
2454 if (TREE_CHAIN (p))
2455 separate_with_comma (scratch_buffer);
2457 return output_finalize_message (scratch_buffer);
2460 static const char *
2461 cv_to_string (p, v)
2462 tree p;
2463 int v ATTRIBUTE_UNUSED;
2465 reinit_global_formatting_buffer ();
2467 dump_qualifiers (p, before);
2469 return output_finalize_message (scratch_buffer);
2472 static void
2473 lang_print_error_function (file)
2474 const char *file;
2476 output_state os;
2478 default_print_error_function (file);
2479 os = output_buffer_state (diagnostic_buffer);
2480 output_set_prefix (diagnostic_buffer, file);
2481 maybe_print_instantiation_context (diagnostic_buffer);
2482 output_buffer_state (diagnostic_buffer) = os;
2485 static void
2486 cp_diagnostic_starter (buffer, dc)
2487 output_buffer *buffer;
2488 diagnostic_context *dc;
2490 report_problematic_module (buffer);
2491 cp_print_error_function (buffer, dc);
2492 maybe_print_instantiation_context (buffer);
2493 output_set_prefix (buffer,
2494 context_as_prefix (diagnostic_file_location (dc),
2495 diagnostic_line_location (dc),
2496 diagnostic_is_warning (dc)));
2499 static void
2500 cp_diagnostic_finalizer (buffer, dc)
2501 output_buffer *buffer;
2502 diagnostic_context *dc __attribute__ ((__unused__));
2504 output_destroy_prefix (buffer);
2507 /* Print current function onto BUFFER, in the process of reporting
2508 a diagnostic message. Called from cp_diagnostic_starter. */
2509 static void
2510 cp_print_error_function (buffer, dc)
2511 output_buffer *buffer;
2512 diagnostic_context *dc;
2514 if (error_function_changed ())
2516 char *prefix = diagnostic_file_location (dc)
2517 ? file_name_as_prefix (diagnostic_file_location (dc))
2518 : NULL;
2519 output_state os;
2521 os = output_buffer_state (buffer);
2522 output_set_prefix (buffer, prefix);
2524 if (current_function_decl == NULL)
2525 output_add_string (buffer, "At global scope:");
2526 else
2527 output_printf
2528 (buffer, "In %s `%s':", function_category (current_function_decl),
2529 (*decl_printable_name) (current_function_decl, 2));
2530 output_add_newline (buffer);
2532 record_last_error_function ();
2533 output_destroy_prefix (buffer);
2534 output_buffer_state (buffer) = os;
2538 /* Returns a description of FUNCTION using standard terminology. */
2539 static const char *
2540 function_category (fn)
2541 tree fn;
2543 if (DECL_FUNCTION_MEMBER_P (fn))
2545 if (DECL_STATIC_FUNCTION_P (fn))
2546 return "static member function";
2547 else if (DECL_COPY_CONSTRUCTOR_P (fn))
2548 return "copy constructor";
2549 else if (DECL_CONSTRUCTOR_P (fn))
2550 return "constructor";
2551 else if (DECL_DESTRUCTOR_P (fn))
2552 return "destructor";
2553 else
2554 return "member function";
2556 else
2557 return "function";
2560 /* Report the full context of a current template instantiation,
2561 onto BUFFER. */
2562 static void
2563 print_instantiation_full_context (buffer)
2564 output_buffer *buffer;
2566 tree p = current_instantiation ();
2567 int line = lineno;
2568 const char *file = input_filename;
2570 if (p)
2572 if (current_function_decl != TINST_DECL (p)
2573 && current_function_decl != NULL_TREE)
2574 /* We can get here during the processing of some synthesized
2575 method. Then, TINST_DECL (p) will be the function that's causing
2576 the synthesis. */
2578 else
2580 if (current_function_decl == TINST_DECL (p))
2581 /* Avoid redundancy with the the "In function" line. */;
2582 else
2583 output_verbatim (buffer, "%s: In instantiation of `%s':\n", file,
2584 decl_as_string (TINST_DECL (p),
2585 TFF_DECL_SPECIFIERS | TFF_RETURN_TYPE));
2587 line = TINST_LINE (p);
2588 file = TINST_FILE (p);
2589 p = TREE_CHAIN (p);
2593 print_instantiation_partial_context (buffer, p, file, line);
2596 /* Same as above but less verbose. */
2597 static void
2598 print_instantiation_partial_context (buffer, t, file, line)
2599 output_buffer *buffer;
2600 tree t;
2601 const char *file;
2602 int line;
2604 for (; t; t = TREE_CHAIN (t))
2606 output_verbatim
2607 (buffer, "%s:%d: instantiated from `%s'\n", file, line,
2608 decl_as_string (TINST_DECL (t), TFF_DECL_SPECIFIERS | TFF_RETURN_TYPE));
2609 line = TINST_LINE (t);
2610 file = TINST_FILE (t);
2612 output_verbatim (buffer, "%s:%d: instantiated from here\n", file, line);
2615 /* Called from cp_thing to print the template context for an error. */
2616 static void
2617 maybe_print_instantiation_context (buffer)
2618 output_buffer *buffer;
2620 if (!problematic_instantiation_changed () || current_instantiation () == 0)
2621 return;
2623 record_last_problematic_instantiation ();
2624 print_instantiation_full_context (buffer);
2627 /* Report the bare minimum context of a template instantiation. */
2628 void
2629 print_instantiation_context ()
2631 print_instantiation_partial_context
2632 (diagnostic_buffer, current_instantiation (), input_filename, lineno);
2633 flush_diagnostic_buffer ();
2636 /* Called from output_format -- during diagnostic message processing --
2637 to handle C++ specific format specifier with the following meanings:
2638 %A function argument-list.
2639 %D declaration.
2640 %E expression.
2641 %F function declaration.
2642 %P function parameter whose position is indicated by an integer.
2643 %T type.
2644 %V cv-qualifier. */
2645 static int
2646 cp_tree_printer (buffer)
2647 output_buffer *buffer;
2649 int be_verbose = 0;
2650 tree_formatting_info tfi;
2652 memset (&tfi, 0, sizeof (tree_formatting_info));
2654 if (*output_buffer_text_cursor (buffer) == '+')
2655 ++output_buffer_text_cursor (buffer);
2656 if (*output_buffer_text_cursor (buffer) == '#')
2658 be_verbose = 1;
2659 ++output_buffer_text_cursor (buffer);
2662 switch (*output_buffer_text_cursor (buffer))
2664 case 'A':
2665 tree_being_formatted (&tfi) =
2666 va_arg (output_buffer_format_args (buffer), tree);
2667 if (be_verbose)
2668 tree_formatting_flags (&tfi) = TFF_SCOPE
2669 | TFF_FUNCTION_DEFAULT_ARGUMENTS;
2670 print_function_argument_list (buffer, &tfi);
2671 break;
2673 case 'D':
2674 tree_being_formatted (&tfi) =
2675 va_arg (output_buffer_format_args (buffer), tree);
2676 if (be_verbose)
2677 tree_formatting_flags (&tfi) = TFF_SCOPE | TFF_DECL_SPECIFIERS
2678 | TFF_CLASS_KEY_OR_ENUM | TFF_RETURN_TYPE
2679 | TFF_FUNCTION_DEFAULT_ARGUMENTS | TFF_TEMPLATE_DEFAULT_ARGUMENTS
2680 | TFF_EXCEPTION_SPECIFICATION | TFF_CHASE_NAMESPACE_ALIAS;
2681 print_declaration (buffer, &tfi);
2682 break;
2684 case 'E':
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 print_expression (buffer, &tfi);
2690 break;
2692 case 'F':
2693 tree_being_formatted (&tfi) =
2694 va_arg (output_buffer_format_args (buffer), tree);
2695 if (be_verbose)
2696 tree_formatting_flags (&tfi) = TFF_SCOPE | TFF_DECL_SPECIFIERS
2697 | TFF_RETURN_TYPE | TFF_FUNCTION_DEFAULT_ARGUMENTS
2698 | TFF_EXCEPTION_SPECIFICATION;
2699 print_function_declaration (buffer, &tfi);
2700 break;
2702 case 'P':
2703 print_function_parameter
2704 (buffer, va_arg (output_buffer_format_args (buffer), int));
2705 break;
2707 case 'T':
2708 tree_being_formatted (&tfi) =
2709 va_arg (output_buffer_format_args (buffer), tree);
2710 if (be_verbose)
2711 tree_formatting_flags (&tfi) = TFF_SCOPE | TFF_CLASS_KEY_OR_ENUM
2712 | TFF_RETURN_TYPE | TFF_EXCEPTION_SPECIFICATION;
2713 print_type_id (buffer, &tfi);
2714 break;
2716 case 'V':
2717 tree_being_formatted (&tfi) =
2718 va_arg (output_buffer_format_args (buffer), tree);
2719 print_cv_qualifier_seq (buffer, &tfi);
2720 break;
2722 default:
2723 return 0;
2726 return 1;
2729 /* Print a function argument-list represented by tree_being_formatted (TFI)
2730 onto BUFFER. */
2731 static void
2732 print_function_argument_list (buffer, tfi)
2733 output_buffer *buffer __attribute__ ((__unused__));
2734 tfi_t tfi __attribute__ ((__unused__));
2738 /* Print a declaration represented by tree_being_formatted (TFI)
2739 onto buffer. */
2740 static void
2741 print_declaration (buffer, tfi)
2742 output_buffer *buffer __attribute__ ((__unused__));
2743 tfi_t tfi __attribute__ ((__unused__));
2747 /* Print an expression represented by tree_being_formatted (TFI)
2748 onto BUFFER. */
2749 static void
2750 print_expression (buffer, tfi)
2751 output_buffer *buffer __attribute__ ((__unused__));
2752 tfi_t tfi __attribute__ ((__unused__));
2756 static void
2757 print_integer (buffer, i)
2758 output_buffer *buffer;
2759 HOST_WIDE_INT i;
2761 sprintf (digit_buffer, HOST_WIDE_INT_PRINT_DEC, (HOST_WIDE_INT) i);
2762 output_add_string (buffer, digit_buffer);
2765 /* Print a function declaration represented by tree_being_formatted (TFI)
2766 onto BUFFER. */
2767 static void
2768 print_function_declaration (buffer, tfi)
2769 output_buffer *buffer __attribute__ ((__unused__));
2770 tfi_t tfi __attribute__ ((__unused__));
2774 /* Print the N'th function parameter onto BUFFER. A negative value of N
2775 means the implicit "this" parameter of a member function. */
2776 static void
2777 print_function_parameter (buffer, n)
2778 output_buffer *buffer;
2779 int n;
2781 if (n < 0)
2782 print_identifier (buffer, "this");
2783 else
2784 output_decimal (buffer, n + 1);
2787 /* Print a type represented by tree_being_formatted (TFI) onto BUFFER. */
2788 static void
2789 print_type_id (buffer, tfi)
2790 output_buffer *buffer;
2791 tfi_t tfi;
2793 tree t = tree_being_formatted (tfi);
2794 int flags = tree_formatting_flags (tfi);
2795 if (t == NULL_TREE)
2796 return;
2798 if (flags & TFF_CHASE_TYPEDEF)
2799 tree_being_formatted (tfi) =
2800 typedef_original_name (tree_being_formatted (tfi));
2802 /* A type-id is of the form:
2803 type-id:
2804 type-specifier-seq abstract-declarator(opt) */
2805 print_type_specifier_seq (buffer, tfi);
2807 if (TYPE_PTRMEMFUNC_P (t))
2808 goto ptr_mem_fun;
2810 /* For types with abstract-declarator, print_type_specifier_seq prints
2811 the start of the abstract-declarator. Fiinish the job. */
2812 switch (TREE_CODE (t))
2814 case ARRAY_TYPE:
2815 case POINTER_TYPE:
2816 case REFERENCE_TYPE:
2817 case OFFSET_TYPE:
2818 case METHOD_TYPE:
2819 case FUNCTION_TYPE:
2820 ptr_mem_fun:
2821 print_rest_of_abstract_declarator (buffer, tfi);
2823 default:
2824 break;
2827 tree_being_formatted (tfi) = t;
2830 /* Print the type-specifier-seq part of a type-id. If appropriate, print
2831 also the prefix of the abstract-declarator. */
2832 static void
2833 print_type_specifier_seq (buffer, tfi)
2834 output_buffer *buffer;
2835 tfi_t tfi;
2837 int flags = tree_formatting_flags (tfi);
2838 tree t = tree_being_formatted (tfi);
2839 enum tree_code code = TREE_CODE (t);
2841 /* A type-speficier-seq is:
2842 type-specifier type-specifier-seq(opt)
2843 where
2844 type-specifier:
2845 simple-type-specifier
2846 class-specifier
2847 enum-specifier
2848 elaborated-type-specifier
2849 cv-qualifier
2851 We do not, however, pretty-print class-specifier nor enum-specifier. */
2853 switch (code)
2855 case UNKNOWN_TYPE:
2856 case IDENTIFIER_NODE:
2857 case VOID_TYPE:
2858 case INTEGER_TYPE:
2859 case REAL_TYPE:
2860 case COMPLEX_TYPE:
2861 case ENUMERAL_TYPE:
2862 case BOOLEAN_TYPE:
2863 case UNION_TYPE:
2864 case TYPE_DECL:
2865 case TEMPLATE_DECL:
2866 case TEMPLATE_TYPE_PARM:
2867 case TYPEOF_TYPE:
2868 case TEMPLATE_TEMPLATE_PARM:
2869 case TYPENAME_TYPE:
2870 class_type:
2871 print_cv_qualifier_seq (buffer, tfi);
2872 if ((flags & TFF_DECL_SPECIFIERS)
2873 && (code == TYPENAME_TYPE || IS_AGGR_TYPE (t)))
2874 print_elaborated_type_specifier (buffer, tfi);
2875 else
2876 print_simple_type_specifier (buffer, tfi);
2877 break;
2879 /* Because the abstract-declarator can modify the type-specifier-seq
2880 in a highly non linear manner, we pretty-print its prefix here.
2881 The suffix part is handled by print_rest_of_abstract_declarator. */
2883 /* A RECORD_TYPE is also used to represent a pointer to member
2884 function. */
2885 case RECORD_TYPE:
2886 if (TYPE_PTRMEMFUNC_P (t))
2888 /* Print the return type. */
2889 tree_being_formatted (tfi) =
2890 TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (t));
2891 print_type_id (buffer, tfi);
2892 print_whitespace (buffer, tfi);
2894 /* Then the beginning of the abstract-declarator part. */
2895 tree_being_formatted (tfi) =
2896 TYPE_METHOD_BASETYPE (TYPE_PTRMEMFUNC_FN_TYPE (t));
2897 print_left_paren (buffer);
2898 print_nested_name_specifier (buffer, tfi);
2900 else
2901 goto class_type;
2902 break;
2904 case POINTER_TYPE:
2905 if (TYPE_PTRMEM_P (t))
2906 goto ptr_data_member;
2907 else
2908 goto non_ptr_data_member;
2909 break;
2911 case ARRAY_TYPE:
2912 case REFERENCE_TYPE:
2913 case FUNCTION_TYPE:
2914 case METHOD_TYPE:
2915 non_ptr_data_member:
2916 tree_being_formatted (tfi) = TREE_TYPE (t);
2917 print_type_specifier_seq (buffer, tfi);
2918 if (code == POINTER_TYPE || code == REFERENCE_TYPE)
2920 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
2921 print_left_paren (buffer);
2923 else if (code == FUNCTION_TYPE || code == METHOD_TYPE)
2925 print_whitespace (buffer, tfi);
2926 print_left_paren (buffer);
2927 if (code == METHOD_TYPE)
2929 tree_being_formatted (tfi) = TYPE_METHOD_BASETYPE (t);
2930 print_nested_name_specifier (buffer, tfi);
2931 tree_being_formatted (tfi) = t;
2934 tree_being_formatted (tfi) = t;
2935 break;
2937 ptr_data_member:
2938 case OFFSET_TYPE:
2939 /* Firstly, the type of the member. */
2940 tree_being_formatted (tfi) = TREE_TYPE (t);
2941 print_type_id (buffer, tfi);
2942 print_whitespace (buffer, tfi);
2944 /* Then, the containing class. */
2945 tree_being_formatted (tfi) = TYPE_OFFSET_BASETYPE (t);
2946 print_nested_name_specifier (buffer, tfi);
2947 tree_being_formatted (tfi) = t;
2948 break;
2950 default:
2951 sorry_for_unsupported_tree (t);
2952 /* fall throught */
2954 case ERROR_MARK:
2955 print_identifier (buffer, "{type-specifier-seq error}");
2956 break;
2959 tree_being_formatted (tfi) = t;
2962 /* Print the simpe-type-specifier component of a type-specifier. */
2963 static void
2964 print_simple_type_specifier (buffer, tfi)
2965 output_buffer *buffer;
2966 tfi_t tfi;
2968 int flags = tree_formatting_flags (tfi);
2969 tree t = tree_being_formatted (tfi);
2970 enum tree_code code = TREE_CODE (t);
2972 switch (code)
2974 case UNKNOWN_TYPE:
2975 print_identifier (buffer, "{unknown type}");
2976 break;
2978 case IDENTIFIER_NODE:
2979 print_tree_identifier (buffer, t);
2980 break;
2982 case COMPLEX_TYPE:
2983 print_identifier (buffer, "__complex__ ");
2984 tree_being_formatted (tfi) = TREE_TYPE (t);
2985 print_type_id (buffer, tfi);
2986 break;
2988 case TYPENAME_TYPE:
2989 tree_being_formatted (tfi) = TYPE_CONTEXT (t);
2990 print_nested_name_specifier (buffer, tfi);
2991 tree_being_formatted (tfi) = TYPENAME_TYPE_FULLNAME (t);
2992 tree_formatting_flags (tfi) |= ~TFF_CHASE_TYPEDEF;
2993 print_type_id (buffer, tfi);
2994 break;
2996 case TYPEOF_TYPE:
2997 print_identifier (buffer, "__typeof__");
2998 tree_being_formatted (tfi) = TYPE_FIELDS (t);
2999 print_left_paren (buffer);
3000 print_expression (buffer, tfi);
3001 print_right_paren (buffer);
3002 break;
3004 case INTEGER_TYPE:
3005 if (TREE_UNSIGNED (t))
3007 if (TYPE_MAIN_VARIANT (t) == integer_type_node)
3008 /* We don't want pedantry like `unsigned int'. */;
3009 else if (!TREE_UNSIGNED (TYPE_MAIN_VARIANT (t)))
3011 print_identifier (buffer, "unsigned");
3012 print_whitespace (buffer, tfi);
3015 else if (TYPE_MAIN_VARIANT (t) == char_type_node)
3017 print_identifier (buffer, "signed");
3018 print_whitespace (buffer, tfi);
3020 case REAL_TYPE:
3021 case BOOLEAN_TYPE:
3022 case VOID_TYPE:
3024 tree s = (flags & TFF_CHASE_TYPEDEF) ? TYPE_MAIN_VARIANT (t) : t;
3026 if (TYPE_NAME (s) && TYPE_IDENTIFIER (s))
3027 print_tree_identifier (buffer, TYPE_IDENTIFIER (s));
3028 else
3029 /* Types like intQI_type_node and friends have no names.
3030 These don't come up in user error messages, but it's nice
3031 to be able to print them from the debugger. */
3032 print_identifier (buffer, "{anonymous}");
3034 break;
3036 case TEMPLATE_TEMPLATE_PARM:
3037 if (TYPE_IDENTIFIER (t))
3038 print_tree_identifier (buffer, TYPE_IDENTIFIER (t));
3039 else
3040 print_identifier (buffer, "{anonymous template template parameter}");
3041 break;
3043 case TYPE_DECL:
3044 if (flags & TFF_CHASE_TYPEDEF)
3045 print_type_id (buffer, tfi);
3046 else
3047 print_tree_identifier (buffer, DECL_NAME (t));
3048 break;
3050 case BOUND_TEMPLATE_TEMPLATE_PARM:
3051 case TEMPLATE_DECL:
3052 print_template_id (buffer, tfi);
3053 break;
3055 case TEMPLATE_TYPE_PARM:
3056 if (TYPE_IDENTIFIER (t))
3057 print_tree_identifier (buffer, TYPE_IDENTIFIER (t));
3058 else
3059 print_identifier (buffer, "{anonymous template type parameter}");
3060 break;
3062 default:
3063 break;
3066 tree_being_formatted (tfi) = t;
3067 tree_formatting_flags (tfi) = flags;
3070 /* Print the elaborated-type-specifier form of a type-specifier. */
3071 static void
3072 print_elaborated_type_specifier (buffer, tfi)
3073 output_buffer *buffer;
3074 tfi_t tfi;
3076 int flags = tree_formatting_flags (tfi);
3077 tree t = tree_being_formatted (tfi);
3079 switch (TREE_CODE (t))
3081 case TYPENAME_TYPE:
3082 print_identifier (buffer, "typename");
3083 print_whitespace (buffer, tfi);
3084 tree_formatting_flags (tfi) |= ~TFF_DECL_SPECIFIERS;
3085 print_simple_type_specifier (buffer, tfi);
3086 break;
3088 case UNION_TYPE:
3089 case RECORD_TYPE:
3091 tree name = NULL_TREE;
3093 if (flags & TFF_CHASE_TYPEDEF)
3094 tree_being_formatted (tfi) = typedef_original_name (t);
3096 print_identifier
3097 (buffer, class_key_or_enum (tree_being_formatted (tfi)));
3098 print_whitespace (buffer, tfi);
3100 name = TYPE_NAME (tree_being_formatted (tfi));
3101 if (name)
3103 if (flags & TFF_SCOPE)
3105 tree_being_formatted (tfi) = CP_DECL_CONTEXT (name);
3106 print_nested_name_specifier (buffer, tfi);
3108 print_tree_identifier (buffer, DECL_NAME (name));
3110 else
3111 print_identifier (buffer, "{anonymous}");
3113 break;
3115 default:
3116 sorry_for_unsupported_tree (t);
3117 break;
3120 tree_being_formatted (tfi) = t;
3121 tree_formatting_flags (tfi) = flags;
3124 /* Finish the job of printing the abstract-declarator part of a
3125 type-id. */
3126 static void
3127 print_rest_of_abstract_declarator (buffer, tfi)
3128 output_buffer *buffer;
3129 tfi_t tfi;
3131 tree t = tree_being_formatted (tfi);
3132 enum tree_code code = TREE_CODE (t);
3134 /* An abstract-declarator has the form:
3136 abstract-declarator:
3137 ptr-operator abstract-declarator(opt)
3138 direct-abstract-declarator
3140 direct-abstract-declarator:
3141 direct-abstract-declarator(opt)
3142 ( parameter-declaration-clause ) cv-qualifier-seq(opt)
3143 exception-specification(opt)
3144 direct-abstract-declarator(opt) [ constant-expression(opt) ]
3145 ( direct-abstract-declarator ) */
3147 switch (code)
3149 case ARRAY_TYPE:
3150 print_left_bracket (buffer);
3151 if (TYPE_DOMAIN (t))
3153 tree s = TYPE_DOMAIN (t);
3155 if (host_integerp (TYPE_MAX_VALUE (s), 0))
3156 output_decimal (buffer, tree_low_cst (TYPE_MAX_VALUE (s), 0) + 1);
3157 else if (TREE_CODE (TYPE_MAX_VALUE (s)) == MINUS_EXPR)
3159 tree_being_formatted (tfi) =
3160 TREE_OPERAND (TYPE_MAX_VALUE (s), 0);
3161 print_expression (buffer, tfi);
3162 tree_being_formatted (tfi) = t;
3164 else
3166 tree_being_formatted (tfi) = fold
3167 (cp_build_binary_op (PLUS_EXPR, TYPE_MAX_VALUE (s),
3168 integer_one_node));
3169 print_expression (buffer, tfi);
3170 tree_being_formatted (tfi) = t;
3173 print_right_bracket (buffer);
3174 put_whitespace (tfi) = none;
3175 tree_being_formatted (tfi) = TREE_TYPE (t);
3176 print_rest_of_abstract_declarator (buffer, tfi);
3177 tree_being_formatted (tfi) = t;
3178 break;
3180 case POINTER_TYPE:
3181 case REFERENCE_TYPE:
3182 case OFFSET_TYPE:
3183 if (code == POINTER_TYPE || code == REFERENCE_TYPE)
3185 output_add_character (buffer, "&*"[code == POINTER_TYPE]);
3186 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
3187 print_right_paren (buffer);
3189 put_whitespace (tfi) = before;
3190 print_cv_qualifier_seq (buffer, tfi);
3191 tree_being_formatted (tfi) = TREE_TYPE (t);
3192 print_rest_of_abstract_declarator (buffer, tfi);
3193 tree_being_formatted (tfi) = t;
3194 break;
3196 case FUNCTION_TYPE:
3197 case METHOD_TYPE:
3198 print_right_paren (buffer);
3199 print_whitespace (buffer, tfi);
3201 /* Skip the `this' implicit parameter if present. */
3202 tree_being_formatted (tfi) = TYPE_ARG_TYPES (t);
3203 if (code == METHOD_TYPE)
3204 tree_being_formatted (tfi) = TREE_CHAIN (tree_being_formatted (tfi));
3206 /* Print the parameter-list. */
3207 print_left_paren (buffer);
3208 print_parameter_declaration_clause (buffer, tfi);
3209 print_right_paren (buffer);
3211 print_whitespace (buffer, tfi);
3213 if (code == METHOD_TYPE)
3215 tree_being_formatted (tfi) =
3216 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t)));
3217 print_cv_qualifier_seq (buffer, tfi);
3220 /* Finish the abstract-declarator. */
3221 tree_being_formatted (tfi) = TREE_TYPE (t);
3222 print_rest_of_abstract_declarator (buffer, tfi);
3224 /* Print the exception-specification for documentaion purpose. */
3225 tree_being_formatted (tfi) = TYPE_RAISES_EXCEPTIONS (t);
3226 print_exception_specification (buffer, tfi);
3227 tree_being_formatted (tfi) = t;
3228 break;
3230 /* These types don't have abstract-declarator. */
3231 case UNKNOWN_TYPE:
3232 case IDENTIFIER_NODE:
3233 case VOID_TYPE:
3234 case INTEGER_TYPE:
3235 case REAL_TYPE:
3236 case COMPLEX_TYPE:
3237 case ENUMERAL_TYPE:
3238 case BOOLEAN_TYPE:
3239 case UNION_TYPE:
3240 case TYPE_DECL:
3241 case TEMPLATE_DECL:
3242 case TEMPLATE_TYPE_PARM:
3243 case TYPEOF_TYPE:
3244 case TEMPLATE_TEMPLATE_PARM:
3245 case TYPENAME_TYPE:
3246 break;
3248 default:
3249 sorry_for_unsupported_tree (t);
3250 /* fall throught. */
3251 case ERROR_MARK:
3252 break;
3256 /* Print the cv-quafilers of tree_being_formatted (TFI) onto BUFFER. */
3257 static void
3258 print_cv_qualifier_seq (buffer, tfi)
3259 output_buffer *buffer;
3260 tree_formatting_info *tfi;
3262 int cv = TYPE_QUALS (tree_being_formatted (tfi));
3263 int pad_after = after == put_whitespace (tfi);
3264 static const int mask[]
3265 = {TYPE_QUAL_CONST, TYPE_QUAL_VOLATILE, TYPE_QUAL_RESTRICT};
3266 static const char *const qualifier[]
3267 = { "const", "volatile", "__restrict__" };
3269 if (cv != 0)
3271 int i;
3272 for (i = 0; i != 3; ++i)
3273 if (mask[i] & cv)
3275 if (put_whitespace (tfi) == before)
3276 output_add_space (buffer);
3277 print_identifier (buffer, qualifier[i]);
3278 put_whitespace (tfi) = before;
3281 if (pad_after)
3283 output_add_space (buffer);
3284 put_whitespace (tfi) = none;
3289 static void
3290 print_parameter_declaration_clause (buffer, tfi)
3291 output_buffer *buffer __attribute__ ((__unused__));
3292 tfi_t tfi __attribute__ ((__unused__));
3296 static void
3297 print_exception_specification (buffer, tfi)
3298 output_buffer *buffer __attribute__ ((__unused__));
3299 tfi_t tfi __attribute__ ((__unused__));
3303 static void
3304 print_nested_name_specifier (buffer, tfi)
3305 output_buffer *buffer;
3306 tfi_t tfi;
3308 int flags = tree_formatting_flags (tfi);
3309 tree t = tree_being_formatted (tfi);
3310 /* A nested-name-specifier is:
3311 class-or-namespace-name :: nested-name-specifier(opt)
3312 class-or-namespace-name :: template nested-name-specifier
3314 The latter form being the correct syntax for a name designating
3315 a template member, where the preceding class-or-namespace-name part
3316 is name-dependent. For the time being, we do not do such a
3317 sophisticated pretty-printing.
3319 class-or-namespace-name:
3320 class-name
3321 namespace-name */
3323 if (t == NULL_TREE || t == global_namespace)
3324 return;
3326 if (CLASS_TYPE_P (t) && !(flags & TFF_CLASS_SCOPE))
3327 return;
3329 if (TREE_CODE (t) == NAMESPACE_DECL && !(flags & TFF_NAMESPACE_SCOPE))
3330 return;
3332 tree_being_formatted (tfi) = DECL_CONTEXT (t);
3333 print_nested_name_specifier (buffer, tfi);
3334 print_scope_operator (buffer);
3335 if (TREE_CODE (t) == NAMESPACE_DECL)
3336 print_tree_identifier (buffer, DECL_NAME (t));
3337 else if (CLASS_TYPE_P (t))
3339 if (!DECL_USE_TEMPLATE (t))
3340 print_tree_identifier (buffer, TYPE_IDENTIFIER (t));
3341 else
3343 tree_being_formatted (tfi) = t;
3344 print_template_id (buffer, tfi);
3348 tree_being_formatted (tfi) = t;
3351 static void
3352 print_template_id (buffer, tfi)
3353 output_buffer *buffer;
3354 tfi_t tfi __attribute__ ((__unused__));
3356 print_template_argument_list_start (buffer);
3357 /* ... */
3358 print_template_argument_list_end (buffer);
3361 static tree
3362 typedef_original_name (t)
3363 tree t;
3365 return DECL_ORIGINAL_TYPE (t) ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t);
3368 static void
3369 print_non_consecutive_character (buffer, c)
3370 output_buffer *buffer;
3371 int c;
3373 const char *p = output_last_position (buffer);
3375 if (p != NULL && *p == c)
3376 output_add_space (buffer);
3377 output_add_character (buffer, c);