1 /* Call-backs for C++ error reporting.
2 This code is non-reentrant.
3 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002,
4 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
30 #include "diagnostic.h"
31 #include "langhooks-def.h"
32 #include "cxx-pretty-print.h"
33 #include "pointer-set.h"
35 #define pp_separate_with_comma(PP) pp_cxx_separate_with (PP, ',')
37 /* The global buffer where we dump everything. It is there only for
38 transitional purpose. It is expected, in the near future, to be
39 completely removed. */
40 static cxx_pretty_printer scratch_pretty_printer;
41 #define cxx_pp (&scratch_pretty_printer)
43 # define NEXT_CODE(T) (TREE_CODE (TREE_TYPE (T)))
45 static const char *args_to_string (tree, int);
46 static const char *assop_to_string (enum tree_code);
47 static const char *code_to_string (enum tree_code);
48 static const char *cv_to_string (tree, int);
49 static const char *decl_to_string (tree, int);
50 static const char *expr_to_string (tree);
51 static const char *fndecl_to_string (tree, int);
52 static const char *op_to_string (enum tree_code);
53 static const char *parm_to_string (int);
54 static const char *type_to_string (tree, int);
56 static void dump_type (tree, int);
57 static void dump_typename (tree, int);
58 static void dump_simple_decl (tree, tree, int);
59 static void dump_decl (tree, int);
60 static void dump_template_decl (tree, int);
61 static void dump_function_decl (tree, int);
62 static void dump_expr (tree, int);
63 static void dump_unary_op (const char *, tree, int);
64 static void dump_binary_op (const char *, tree, int);
65 static void dump_aggr_type (tree, int);
66 static void dump_type_prefix (tree, int);
67 static void dump_type_suffix (tree, int);
68 static void dump_function_name (tree, int);
69 static void dump_call_expr_args (tree, int, bool);
70 static void dump_aggr_init_expr_args (tree, int, bool);
71 static void dump_expr_list (tree, int);
72 static void dump_global_iord (tree);
73 static void dump_parameters (tree, int);
74 static void dump_exception_spec (tree, int);
75 static void dump_template_argument (tree, int);
76 static void dump_template_argument_list (tree, int);
77 static void dump_template_parameter (tree, int);
78 static void dump_template_bindings (tree, tree, VEC(tree,gc) *);
79 static void dump_scope (tree, int);
80 static void dump_template_parms (tree, int, int);
82 static const char *function_category (tree);
83 static void maybe_print_instantiation_context (diagnostic_context *);
84 static void print_instantiation_full_context (diagnostic_context *);
85 static void print_instantiation_partial_context (diagnostic_context *,
88 static void cp_diagnostic_starter (diagnostic_context *, diagnostic_info *);
89 static void cp_diagnostic_finalizer (diagnostic_context *, diagnostic_info *);
90 static void cp_print_error_function (diagnostic_context *, diagnostic_info *);
92 static bool cp_printer (pretty_printer *, text_info *, const char *,
93 int, bool, bool, bool);
94 static location_t location_of (tree);
99 diagnostic_starter (global_dc) = cp_diagnostic_starter;
100 diagnostic_finalizer (global_dc) = cp_diagnostic_finalizer;
101 diagnostic_format_decoder (global_dc) = cp_printer;
103 pp_construct (pp_base (cxx_pp), NULL, 0);
104 pp_cxx_pretty_printer_init (cxx_pp);
107 /* Dump a scope, if deemed necessary. */
110 dump_scope (tree scope, int flags)
112 int f = ~TFF_RETURN_TYPE & (flags & (TFF_SCOPE | TFF_CHASE_TYPEDEF));
114 if (scope == NULL_TREE)
117 if (TREE_CODE (scope) == NAMESPACE_DECL)
119 if (scope != global_namespace)
121 dump_decl (scope, f);
122 pp_cxx_colon_colon (cxx_pp);
125 else if (AGGREGATE_TYPE_P (scope))
127 dump_type (scope, f);
128 pp_cxx_colon_colon (cxx_pp);
130 else if ((flags & TFF_SCOPE) && TREE_CODE (scope) == FUNCTION_DECL)
132 dump_function_decl (scope, f);
133 pp_cxx_colon_colon (cxx_pp);
137 /* Dump the template ARGument under control of FLAGS. */
140 dump_template_argument (tree arg, int flags)
142 if (ARGUMENT_PACK_P (arg))
143 dump_template_argument_list (ARGUMENT_PACK_ARGS (arg), flags);
144 else if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
145 dump_type (arg, flags & ~TFF_CLASS_KEY_OR_ENUM);
148 if (TREE_CODE (arg) == TREE_LIST)
149 arg = TREE_VALUE (arg);
151 dump_expr (arg, (flags | TFF_EXPR_IN_PARENS) & ~TFF_CLASS_KEY_OR_ENUM);
155 /* Dump a template-argument-list ARGS (always a TREE_VEC) under control
159 dump_template_argument_list (tree args, int flags)
161 int n = TREE_VEC_LENGTH (args);
165 for (i = 0; i< n; ++i)
167 tree arg = TREE_VEC_ELT (args, i);
169 /* Only print a comma if we know there is an argument coming. In
170 the case of an empty template argument pack, no actual
171 argument will be printed. */
173 && (!ARGUMENT_PACK_P (arg)
174 || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) > 0))
175 pp_separate_with_comma (cxx_pp);
177 dump_template_argument (arg, flags);
182 /* Dump a template parameter PARM (a TREE_LIST) under control of FLAGS. */
185 dump_template_parameter (tree parm, int flags)
190 if (parm == error_mark_node)
193 p = TREE_VALUE (parm);
194 a = TREE_PURPOSE (parm);
196 if (TREE_CODE (p) == TYPE_DECL)
198 if (flags & TFF_DECL_SPECIFIERS)
200 pp_cxx_identifier (cxx_pp, "class");
201 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (p)))
202 pp_cxx_identifier (cxx_pp, "...");
204 pp_cxx_tree_identifier (cxx_pp, DECL_NAME (p));
206 else if (DECL_NAME (p))
207 pp_cxx_tree_identifier (cxx_pp, DECL_NAME (p));
209 pp_cxx_canonical_template_parameter (cxx_pp, TREE_TYPE (p));
212 dump_decl (p, flags | TFF_DECL_SPECIFIERS);
214 if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && a != NULL_TREE)
216 pp_cxx_whitespace (cxx_pp);
218 pp_cxx_whitespace (cxx_pp);
219 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
220 dump_type (a, flags & ~TFF_CHASE_TYPEDEF);
222 dump_expr (a, flags | TFF_EXPR_IN_PARENS);
226 /* Dump, under control of FLAGS, a template-parameter-list binding.
227 PARMS is a TREE_LIST of TREE_VEC of TREE_LIST and ARGS is a
231 dump_template_bindings (tree parms, tree args, VEC(tree,gc)* typenames)
239 tree p = TREE_VALUE (parms);
240 int lvl = TMPL_PARMS_DEPTH (parms);
244 for (i = 0; i < TREE_VEC_LENGTH (p); ++i)
246 tree arg = NULL_TREE;
248 /* Don't crash if we had an invalid argument list. */
249 if (TMPL_ARGS_DEPTH (args) >= lvl)
251 tree lvl_args = TMPL_ARGS_LEVEL (args, lvl);
252 if (NUM_TMPL_ARGS (lvl_args) > arg_idx)
253 arg = TREE_VEC_ELT (lvl_args, arg_idx);
257 pp_separate_with_comma (cxx_pp);
258 dump_template_parameter (TREE_VEC_ELT (p, i), TFF_PLAIN_IDENTIFIER);
259 pp_cxx_whitespace (cxx_pp);
261 pp_cxx_whitespace (cxx_pp);
263 dump_template_argument (arg, TFF_PLAIN_IDENTIFIER);
265 pp_identifier (cxx_pp, "<missing>");
271 parms = TREE_CHAIN (parms);
274 for (i = 0; VEC_iterate (tree, typenames, i, t); ++i)
277 pp_separate_with_comma (cxx_pp);
278 dump_type (t, TFF_PLAIN_IDENTIFIER);
279 pp_cxx_whitespace (cxx_pp);
281 pp_cxx_whitespace (cxx_pp);
282 t = tsubst (t, args, tf_none, NULL_TREE);
283 /* Strip typedefs. We can't just use TFF_CHASE_TYPEDEF because
284 pp_simple_type_specifier doesn't know about it. */
285 t = canonical_type_variant (t);
286 dump_type (t, TFF_PLAIN_IDENTIFIER);
290 /* Dump a human-readable equivalent of TYPE. FLAGS controls the
294 dump_type (tree t, int flags)
299 if (TYPE_PTRMEMFUNC_P (t))
302 switch (TREE_CODE (t))
305 if (t == init_list_type_node)
306 pp_identifier (cxx_pp, "<brace-enclosed initializer list>");
308 pp_identifier (cxx_pp, "<unresolved overloaded function type>");
312 /* A list of function parms. */
313 dump_parameters (t, flags);
316 case IDENTIFIER_NODE:
317 pp_cxx_tree_identifier (cxx_pp, t);
321 dump_type (BINFO_TYPE (t), flags);
327 dump_aggr_type (t, flags);
331 if (flags & TFF_CHASE_TYPEDEF)
333 dump_type (DECL_ORIGINAL_TYPE (t)
334 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t), flags);
337 /* Else fall through. */
341 dump_decl (t, flags & ~TFF_DECL_SPECIFIERS);
350 case FIXED_POINT_TYPE:
351 pp_type_specifier_seq (cxx_pp, t);
354 case TEMPLATE_TEMPLATE_PARM:
355 /* For parameters inside template signature. */
356 if (TYPE_IDENTIFIER (t))
357 pp_cxx_tree_identifier (cxx_pp, TYPE_IDENTIFIER (t));
359 pp_cxx_canonical_template_parameter (cxx_pp, t);
362 case BOUND_TEMPLATE_TEMPLATE_PARM:
364 tree args = TYPE_TI_ARGS (t);
365 pp_cxx_cv_qualifier_seq (cxx_pp, t);
366 pp_cxx_tree_identifier (cxx_pp, TYPE_IDENTIFIER (t));
367 pp_cxx_begin_template_argument_list (cxx_pp);
368 dump_template_argument_list (args, flags);
369 pp_cxx_end_template_argument_list (cxx_pp);
373 case TEMPLATE_TYPE_PARM:
374 pp_cxx_cv_qualifier_seq (cxx_pp, t);
375 if (TYPE_IDENTIFIER (t))
376 pp_cxx_tree_identifier (cxx_pp, TYPE_IDENTIFIER (t));
378 pp_cxx_canonical_template_parameter
379 (cxx_pp, TEMPLATE_TYPE_PARM_INDEX (t));
382 /* This is not always necessary for pointers and such, but doing this
383 reduces code size. */
392 dump_type_prefix (t, flags);
393 dump_type_suffix (t, flags);
397 if (! (flags & TFF_CHASE_TYPEDEF)
398 && DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
400 dump_decl (TYPE_NAME (t), TFF_PLAIN_IDENTIFIER);
403 pp_cxx_cv_qualifier_seq (cxx_pp, t);
404 pp_cxx_identifier (cxx_pp,
405 TYPENAME_IS_ENUM_P (t) ? "enum"
406 : TYPENAME_IS_CLASS_P (t) ? "class"
408 dump_typename (t, flags);
411 case UNBOUND_CLASS_TEMPLATE:
412 dump_type (TYPE_CONTEXT (t), flags);
413 pp_cxx_colon_colon (cxx_pp);
414 pp_cxx_identifier (cxx_pp, "template");
415 dump_type (DECL_NAME (TYPE_NAME (t)), flags);
419 pp_cxx_identifier (cxx_pp, "__typeof__");
420 pp_cxx_whitespace (cxx_pp);
421 pp_cxx_left_paren (cxx_pp);
422 dump_expr (TYPEOF_TYPE_EXPR (t), flags & ~TFF_EXPR_IN_PARENS);
423 pp_cxx_right_paren (cxx_pp);
426 case TYPE_PACK_EXPANSION:
427 dump_type (PACK_EXPANSION_PATTERN (t), flags);
428 pp_cxx_identifier (cxx_pp, "...");
431 case TYPE_ARGUMENT_PACK:
432 dump_template_argument (t, flags);
436 pp_cxx_identifier (cxx_pp, "decltype");
437 pp_cxx_whitespace (cxx_pp);
438 pp_cxx_left_paren (cxx_pp);
439 dump_expr (DECLTYPE_TYPE_EXPR (t), flags & ~TFF_EXPR_IN_PARENS);
440 pp_cxx_right_paren (cxx_pp);
444 pp_unsupported_tree (cxx_pp, t);
445 /* Fall through to error. */
448 pp_identifier (cxx_pp, "<type error>");
453 /* Dump a TYPENAME_TYPE. We need to notice when the context is itself
457 dump_typename (tree t, int flags)
459 tree ctx = TYPE_CONTEXT (t);
461 if (TREE_CODE (ctx) == TYPENAME_TYPE)
462 dump_typename (ctx, flags);
464 dump_type (ctx, flags & ~TFF_CLASS_KEY_OR_ENUM);
465 pp_cxx_colon_colon (cxx_pp);
466 dump_decl (TYPENAME_TYPE_FULLNAME (t), flags);
469 /* Return the name of the supplied aggregate, or enumeral type. */
472 class_key_or_enum_as_string (tree t)
474 if (TREE_CODE (t) == ENUMERAL_TYPE)
476 if (SCOPED_ENUM_P (t))
481 else if (TREE_CODE (t) == UNION_TYPE)
483 else if (TYPE_LANG_SPECIFIC (t) && CLASSTYPE_DECLARED_CLASS (t))
489 /* Print out a class declaration T under the control of FLAGS,
490 in the form `class foo'. */
493 dump_aggr_type (tree t, int flags)
496 const char *variety = class_key_or_enum_as_string (t);
500 pp_cxx_cv_qualifier_seq (cxx_pp, t);
502 if (flags & TFF_CLASS_KEY_OR_ENUM)
503 pp_cxx_identifier (cxx_pp, variety);
505 if (flags & TFF_CHASE_TYPEDEF)
506 t = TYPE_MAIN_VARIANT (t);
508 name = TYPE_NAME (t);
512 typdef = !DECL_ARTIFICIAL (name);
513 tmplate = !typdef && TREE_CODE (t) != ENUMERAL_TYPE
514 && TYPE_LANG_SPECIFIC (t) && CLASSTYPE_TEMPLATE_INFO (t)
515 && (TREE_CODE (CLASSTYPE_TI_TEMPLATE (t)) != TEMPLATE_DECL
516 || PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
518 if (! (flags & TFF_UNQUALIFIED_NAME))
519 dump_scope (CP_DECL_CONTEXT (name), flags | TFF_SCOPE);
520 flags &= ~TFF_UNQUALIFIED_NAME;
523 /* Because the template names are mangled, we have to locate
524 the most general template, and use that name. */
525 tree tpl = CLASSTYPE_TI_TEMPLATE (t);
527 while (DECL_TEMPLATE_INFO (tpl))
528 tpl = DECL_TI_TEMPLATE (tpl);
531 name = DECL_NAME (name);
534 if (name == 0 || ANON_AGGRNAME_P (name))
536 if (flags & TFF_CLASS_KEY_OR_ENUM)
537 pp_identifier (cxx_pp, "<anonymous>");
539 pp_printf (pp_base (cxx_pp), "<anonymous %s>", variety);
542 pp_cxx_tree_identifier (cxx_pp, name);
544 dump_template_parms (TYPE_TEMPLATE_INFO (t),
545 !CLASSTYPE_USE_TEMPLATE (t),
546 flags & ~TFF_TEMPLATE_HEADER);
549 /* Dump into the obstack the initial part of the output for a given type.
550 This is necessary when dealing with things like functions returning
553 return type of `int (* fee ())()': pointer -> function -> int. Both
554 pointer (and reference and offset) and function (and member) types must
555 deal with prefix and suffix.
557 Arrays must also do this for DECL nodes, like int a[], and for things like
561 dump_type_prefix (tree t, int flags)
563 if (TYPE_PTRMEMFUNC_P (t))
565 t = TYPE_PTRMEMFUNC_FN_TYPE (t);
569 switch (TREE_CODE (t))
574 tree sub = TREE_TYPE (t);
576 dump_type_prefix (sub, flags);
577 if (TREE_CODE (sub) == ARRAY_TYPE
578 || TREE_CODE (sub) == FUNCTION_TYPE)
580 pp_cxx_whitespace (cxx_pp);
581 pp_cxx_left_paren (cxx_pp);
583 if (TREE_CODE (t) == POINTER_TYPE)
584 pp_character(cxx_pp, '*');
585 else if (TREE_CODE (t) == REFERENCE_TYPE)
587 if (TYPE_REF_IS_RVALUE (t))
588 pp_string (cxx_pp, "&&");
590 pp_character (cxx_pp, '&');
592 pp_base (cxx_pp)->padding = pp_before;
593 pp_cxx_cv_qualifier_seq (cxx_pp, t);
599 dump_type_prefix (TREE_TYPE (t), flags);
600 if (TREE_CODE (t) == OFFSET_TYPE) /* pmfs deal with this in d_t_p */
602 pp_maybe_space (cxx_pp);
603 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
604 pp_cxx_left_paren (cxx_pp);
605 dump_type (TYPE_OFFSET_BASETYPE (t), flags);
606 pp_cxx_colon_colon (cxx_pp);
608 pp_cxx_star (cxx_pp);
609 pp_cxx_cv_qualifier_seq (cxx_pp, t);
610 pp_base (cxx_pp)->padding = pp_before;
613 /* This can be reached without a pointer when dealing with
614 templates, e.g. std::is_function. */
616 dump_type_prefix (TREE_TYPE (t), flags);
620 dump_type_prefix (TREE_TYPE (t), flags);
621 pp_maybe_space (cxx_pp);
622 pp_cxx_left_paren (cxx_pp);
623 dump_aggr_type (TYPE_METHOD_BASETYPE (t), flags);
624 pp_cxx_colon_colon (cxx_pp);
628 dump_type_prefix (TREE_TYPE (t), flags);
632 case IDENTIFIER_NODE:
637 case TEMPLATE_TYPE_PARM:
638 case TEMPLATE_TEMPLATE_PARM:
639 case BOUND_TEMPLATE_TEMPLATE_PARM:
651 case TYPE_PACK_EXPANSION:
652 case FIXED_POINT_TYPE:
653 dump_type (t, flags);
654 pp_base (cxx_pp)->padding = pp_before;
658 pp_unsupported_tree (cxx_pp, t);
661 pp_identifier (cxx_pp, "<typeprefixerror>");
666 /* Dump the suffix of type T, under control of FLAGS. This is the part
667 which appears after the identifier (or function parms). */
670 dump_type_suffix (tree t, int flags)
672 if (TYPE_PTRMEMFUNC_P (t))
673 t = TYPE_PTRMEMFUNC_FN_TYPE (t);
675 switch (TREE_CODE (t))
680 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
681 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
682 pp_cxx_right_paren (cxx_pp);
683 dump_type_suffix (TREE_TYPE (t), flags);
690 if (TREE_CODE (t) == METHOD_TYPE)
691 /* Can only be reached through a pointer. */
692 pp_cxx_right_paren (cxx_pp);
693 arg = TYPE_ARG_TYPES (t);
694 if (TREE_CODE (t) == METHOD_TYPE)
695 arg = TREE_CHAIN (arg);
697 /* Function pointers don't have default args. Not in standard C++,
698 anyway; they may in g++, but we'll just pretend otherwise. */
699 dump_parameters (arg, flags & ~TFF_FUNCTION_DEFAULT_ARGUMENTS);
701 if (TREE_CODE (t) == METHOD_TYPE)
702 pp_cxx_cv_qualifier_seq
703 (cxx_pp, TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))));
705 pp_cxx_cv_qualifier_seq (cxx_pp, t);
706 dump_exception_spec (TYPE_RAISES_EXCEPTIONS (t), flags);
707 dump_type_suffix (TREE_TYPE (t), flags);
712 pp_maybe_space (cxx_pp);
713 pp_cxx_left_bracket (cxx_pp);
716 tree dtype = TYPE_DOMAIN (t);
717 tree max = TYPE_MAX_VALUE (dtype);
718 if (host_integerp (max, 0))
719 pp_wide_integer (cxx_pp, tree_low_cst (max, 0) + 1);
720 else if (TREE_CODE (max) == MINUS_EXPR)
721 dump_expr (TREE_OPERAND (max, 0),
722 flags & ~TFF_EXPR_IN_PARENS);
724 dump_expr (fold_build2 (PLUS_EXPR, dtype, max,
725 build_int_cst (dtype, 1)),
726 flags & ~TFF_EXPR_IN_PARENS);
728 pp_cxx_right_bracket (cxx_pp);
729 dump_type_suffix (TREE_TYPE (t), flags);
733 case IDENTIFIER_NODE:
738 case TEMPLATE_TYPE_PARM:
739 case TEMPLATE_TEMPLATE_PARM:
740 case BOUND_TEMPLATE_TEMPLATE_PARM:
752 case TYPE_PACK_EXPANSION:
753 case FIXED_POINT_TYPE:
757 pp_unsupported_tree (cxx_pp, t);
759 /* Don't mark it here, we should have already done in
766 dump_global_iord (tree t)
768 const char *p = NULL;
770 if (DECL_GLOBAL_CTOR_P (t))
772 else if (DECL_GLOBAL_DTOR_P (t))
777 pp_printf (pp_base (cxx_pp), "(static %s for %s)", p, input_filename);
781 dump_simple_decl (tree t, tree type, int flags)
783 if (flags & TFF_DECL_SPECIFIERS)
785 dump_type_prefix (type, flags & ~TFF_UNQUALIFIED_NAME);
786 pp_maybe_space (cxx_pp);
788 if (! (flags & TFF_UNQUALIFIED_NAME)
789 && (!DECL_INITIAL (t)
790 || TREE_CODE (DECL_INITIAL (t)) != TEMPLATE_PARM_INDEX))
791 dump_scope (CP_DECL_CONTEXT (t), flags);
792 flags &= ~TFF_UNQUALIFIED_NAME;
793 if ((flags & TFF_DECL_SPECIFIERS)
794 && DECL_TEMPLATE_PARM_P (t)
795 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (t)))
796 pp_identifier (cxx_pp, "...");
798 dump_decl (DECL_NAME (t), flags);
800 pp_identifier (cxx_pp, "<anonymous>");
801 if (flags & TFF_DECL_SPECIFIERS)
802 dump_type_suffix (type, flags);
805 /* Dump a human readable string for the decl T under control of FLAGS. */
808 dump_decl (tree t, int flags)
813 switch (TREE_CODE (t))
816 /* Don't say 'typedef class A' */
817 if (DECL_ARTIFICIAL (t))
819 if ((flags & TFF_DECL_SPECIFIERS)
820 && TREE_CODE (TREE_TYPE (t)) == TEMPLATE_TYPE_PARM)
822 /* Say `class T' not just `T'. */
823 pp_cxx_identifier (cxx_pp, "class");
825 /* Emit the `...' for a parameter pack. */
826 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
827 pp_cxx_identifier (cxx_pp, "...");
830 dump_type (TREE_TYPE (t), flags);
833 if (flags & TFF_DECL_SPECIFIERS)
834 pp_cxx_identifier (cxx_pp, "typedef");
835 dump_simple_decl (t, DECL_ORIGINAL_TYPE (t)
836 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t),
841 if (DECL_NAME (t) && VTABLE_NAME_P (DECL_NAME (t)))
843 pp_string (cxx_pp, "vtable for ");
844 gcc_assert (TYPE_P (DECL_CONTEXT (t)));
845 dump_type (DECL_CONTEXT (t), flags);
848 /* Else fall through. */
851 dump_simple_decl (t, TREE_TYPE (t), flags);
855 pp_string (cxx_pp, "<return value> ");
856 dump_simple_decl (t, TREE_TYPE (t), flags);
860 if (flags & TFF_DECL_SPECIFIERS)
861 pp_cxx_declaration (cxx_pp, t);
864 if (! (flags & TFF_UNQUALIFIED_NAME))
865 dump_scope (CP_DECL_CONTEXT (t), flags);
866 flags &= ~TFF_UNQUALIFIED_NAME;
867 if (DECL_NAME (t) == NULL_TREE)
868 pp_identifier (cxx_pp, "<unnamed>");
870 pp_cxx_tree_identifier (cxx_pp, DECL_NAME (t));
875 pp_expression (cxx_pp, t);
879 dump_decl (TREE_OPERAND (t, 0), flags);
880 pp_cxx_left_bracket (cxx_pp);
881 dump_decl (TREE_OPERAND (t, 1), flags);
882 pp_cxx_right_bracket (cxx_pp);
885 /* So that we can do dump_decl on an aggr type. */
889 dump_type (t, flags);
893 /* This is a pseudo destructor call which has not been folded into
894 a PSEUDO_DTOR_EXPR yet. */
895 pp_cxx_complement (cxx_pp);
896 dump_type (TREE_OPERAND (t, 0), flags);
903 /* These special cases are duplicated here so that other functions
904 can feed identifiers to error and get them demangled properly. */
905 case IDENTIFIER_NODE:
906 if (IDENTIFIER_TYPENAME_P (t))
908 pp_cxx_identifier (cxx_pp, "operator");
909 /* Not exactly IDENTIFIER_TYPE_VALUE. */
910 dump_type (TREE_TYPE (t), flags);
914 pp_cxx_tree_identifier (cxx_pp, t);
921 if (DECL_CLASS_SCOPE_P (t))
923 dump_type (DECL_CONTEXT (t), flags);
924 pp_cxx_colon_colon (cxx_pp);
926 else if (DECL_CONTEXT (t))
928 dump_decl (DECL_CONTEXT (t), flags);
929 pp_cxx_colon_colon (cxx_pp);
931 dump_decl (DECL_NAME (t), flags);
935 /* If there's only one function, just treat it like an ordinary
941 if (! DECL_LANG_SPECIFIC (t))
942 pp_identifier (cxx_pp, "<built-in>");
943 else if (DECL_GLOBAL_CTOR_P (t) || DECL_GLOBAL_DTOR_P (t))
944 dump_global_iord (t);
946 dump_function_decl (t, flags);
950 dump_template_decl (t, flags);
953 case TEMPLATE_ID_EXPR:
955 tree name = TREE_OPERAND (t, 0);
957 if (is_overloaded_fn (name))
958 name = DECL_NAME (get_first_fn (name));
959 dump_decl (name, flags);
960 pp_cxx_begin_template_argument_list (cxx_pp);
961 if (TREE_OPERAND (t, 1))
962 dump_template_argument_list (TREE_OPERAND (t, 1), flags);
963 pp_cxx_end_template_argument_list (cxx_pp);
968 pp_cxx_tree_identifier (cxx_pp, DECL_NAME (t));
972 if ((TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == ENUMERAL_TYPE)
973 || (DECL_INITIAL (t) &&
974 TREE_CODE (DECL_INITIAL (t)) == TEMPLATE_PARM_INDEX))
975 dump_simple_decl (t, TREE_TYPE (t), flags);
976 else if (DECL_NAME (t))
977 dump_decl (DECL_NAME (t), flags);
978 else if (DECL_INITIAL (t))
979 dump_expr (DECL_INITIAL (t), flags | TFF_EXPR_IN_PARENS);
981 pp_identifier (cxx_pp, "<enumerator>");
985 pp_cxx_identifier (cxx_pp, "using");
986 dump_type (USING_DECL_SCOPE (t), flags);
987 pp_cxx_colon_colon (cxx_pp);
988 dump_decl (DECL_NAME (t), flags);
992 pp_cxx_declaration (cxx_pp, t);
996 dump_decl (BASELINK_FUNCTIONS (t), flags);
999 case NON_DEPENDENT_EXPR:
1000 dump_expr (t, flags);
1003 case TEMPLATE_TYPE_PARM:
1004 if (flags & TFF_DECL_SPECIFIERS)
1005 pp_cxx_declaration (cxx_pp, t);
1007 pp_type_id (cxx_pp, t);
1010 case UNBOUND_CLASS_TEMPLATE:
1011 case TYPE_PACK_EXPANSION:
1013 dump_type (t, flags);
1017 pp_unsupported_tree (cxx_pp, t);
1018 /* Fall through to error. */
1021 pp_identifier (cxx_pp, "<declaration error>");
1026 /* Dump a template declaration T under control of FLAGS. This means the
1027 'template <...> leaders plus the 'class X' or 'void fn(...)' part. */
1030 dump_template_decl (tree t, int flags)
1032 tree orig_parms = DECL_TEMPLATE_PARMS (t);
1036 if (flags & TFF_TEMPLATE_HEADER)
1038 for (parms = orig_parms = nreverse (orig_parms);
1040 parms = TREE_CHAIN (parms))
1042 tree inner_parms = INNERMOST_TEMPLATE_PARMS (parms);
1043 int len = TREE_VEC_LENGTH (inner_parms);
1045 pp_cxx_identifier (cxx_pp, "template");
1046 pp_cxx_begin_template_argument_list (cxx_pp);
1048 /* If we've shown the template prefix, we'd better show the
1049 parameters' and decl's type too. */
1050 flags |= TFF_DECL_SPECIFIERS;
1052 for (i = 0; i < len; i++)
1055 pp_separate_with_comma (cxx_pp);
1056 dump_template_parameter (TREE_VEC_ELT (inner_parms, i), flags);
1058 pp_cxx_end_template_argument_list (cxx_pp);
1059 pp_cxx_whitespace (cxx_pp);
1061 nreverse(orig_parms);
1063 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
1065 /* Say `template<arg> class TT' not just `template<arg> TT'. */
1066 pp_cxx_identifier (cxx_pp, "class");
1068 /* If this is a parameter pack, print the ellipsis. */
1069 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
1070 pp_cxx_identifier (cxx_pp, "...");
1074 if (DECL_TEMPLATE_RESULT (t)
1075 && TREE_CODE (DECL_TEMPLATE_RESULT (t)) == TYPE_DECL)
1076 dump_type (TREE_TYPE (t),
1077 ((flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1078 | (flags & TFF_DECL_SPECIFIERS ? TFF_CLASS_KEY_OR_ENUM : 0)));
1079 else if (DECL_TEMPLATE_RESULT (t)
1080 && TREE_CODE (DECL_TEMPLATE_RESULT (t)) == VAR_DECL)
1081 dump_decl (DECL_TEMPLATE_RESULT (t), flags | TFF_TEMPLATE_NAME);
1084 gcc_assert (TREE_TYPE (t));
1085 switch (NEXT_CODE (t))
1089 dump_function_decl (t, flags | TFF_TEMPLATE_NAME);
1092 /* This case can occur with some invalid code. */
1093 dump_type (TREE_TYPE (t),
1094 (flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1095 | (flags & TFF_DECL_SPECIFIERS
1096 ? TFF_CLASS_KEY_OR_ENUM : 0));
1101 /* find_typenames looks through the type of the function template T
1102 and returns a VEC containing any typedefs or TYPENAME_TYPEs it finds. */
1104 struct find_typenames_t
1106 struct pointer_set_t *p_set;
1107 VEC (tree,gc) *typenames;
1111 find_typenames_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED, void *data)
1113 struct find_typenames_t *d = (struct find_typenames_t *)data;
1114 tree mv = NULL_TREE;
1116 if (TYPE_P (*tp) && is_typedef_decl (TYPE_NAME (*tp)))
1117 /* Add the type of the typedef without any additional cv-quals. */
1118 mv = TREE_TYPE (TYPE_NAME (*tp));
1119 else if (TREE_CODE (*tp) == TYPENAME_TYPE)
1120 /* Add the typename without any cv-qualifiers. */
1121 mv = TYPE_MAIN_VARIANT (*tp);
1123 if (mv && (mv == *tp || !pointer_set_insert (d->p_set, mv)))
1124 VEC_safe_push (tree, gc, d->typenames, mv);
1126 /* Search into class template arguments, which cp_walk_subtrees
1128 if (CLASS_TYPE_P (*tp) && CLASSTYPE_TEMPLATE_INFO (*tp))
1129 cp_walk_tree (&CLASSTYPE_TI_ARGS (*tp), find_typenames_r,
1135 static VEC(tree,gc) *
1136 find_typenames (tree t)
1138 struct find_typenames_t ft;
1139 ft.p_set = pointer_set_create ();
1140 ft.typenames = NULL;
1141 cp_walk_tree (&TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
1142 find_typenames_r, &ft, ft.p_set);
1143 pointer_set_destroy (ft.p_set);
1144 return ft.typenames;
1147 /* Pretty print a function decl. There are several ways we want to print a
1148 function declaration. The TFF_ bits in FLAGS tells us how to behave.
1149 As error can only apply the '#' flag once to give 0 and 1 for V, there
1150 is %D which doesn't print the throw specs, and %F which does. */
1153 dump_function_decl (tree t, int flags)
1157 tree cname = NULL_TREE;
1158 tree template_args = NULL_TREE;
1159 tree template_parms = NULL_TREE;
1160 int show_return = flags & TFF_RETURN_TYPE || flags & TFF_DECL_SPECIFIERS;
1161 int do_outer_scope = ! (flags & TFF_UNQUALIFIED_NAME);
1163 VEC(tree,gc) *typenames = NULL;
1165 flags &= ~TFF_UNQUALIFIED_NAME;
1166 if (TREE_CODE (t) == TEMPLATE_DECL)
1167 t = DECL_TEMPLATE_RESULT (t);
1169 /* Save the exceptions, in case t is a specialization and we are
1170 emitting an error about incompatible specifications. */
1171 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (t));
1173 /* Pretty print template instantiations only. */
1174 if (DECL_USE_TEMPLATE (t) && DECL_TEMPLATE_INFO (t))
1178 template_args = DECL_TI_ARGS (t);
1179 tmpl = most_general_template (t);
1180 if (tmpl && TREE_CODE (tmpl) == TEMPLATE_DECL)
1182 template_parms = DECL_TEMPLATE_PARMS (tmpl);
1184 typenames = find_typenames (t);
1188 fntype = TREE_TYPE (t);
1189 parmtypes = FUNCTION_FIRST_USER_PARMTYPE (t);
1191 if (DECL_CLASS_SCOPE_P (t))
1192 cname = DECL_CONTEXT (t);
1193 /* This is for partially instantiated template methods. */
1194 else if (TREE_CODE (fntype) == METHOD_TYPE)
1195 cname = TREE_TYPE (TREE_VALUE (parmtypes));
1197 if (!(flags & TFF_DECL_SPECIFIERS))
1199 else if (DECL_STATIC_FUNCTION_P (t))
1200 pp_cxx_identifier (cxx_pp, "static");
1201 else if (DECL_VIRTUAL_P (t))
1202 pp_cxx_identifier (cxx_pp, "virtual");
1204 /* Print the return type? */
1206 show_return = !DECL_CONV_FN_P (t) && !DECL_CONSTRUCTOR_P (t)
1207 && !DECL_DESTRUCTOR_P (t);
1209 dump_type_prefix (TREE_TYPE (fntype), flags);
1211 /* Print the function name. */
1212 if (!do_outer_scope)
1216 dump_type (cname, flags);
1217 pp_cxx_colon_colon (cxx_pp);
1220 dump_scope (CP_DECL_CONTEXT (t), flags);
1222 dump_function_name (t, flags);
1224 if (!(flags & TFF_NO_FUNCTION_ARGUMENTS))
1226 dump_parameters (parmtypes, flags);
1228 if (TREE_CODE (fntype) == METHOD_TYPE)
1230 pp_base (cxx_pp)->padding = pp_before;
1231 pp_cxx_cv_qualifier_seq
1232 (cxx_pp, TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fntype))));
1235 if (flags & TFF_EXCEPTION_SPECIFICATION)
1237 pp_base (cxx_pp)->padding = pp_before;
1238 dump_exception_spec (exceptions, flags);
1242 dump_type_suffix (TREE_TYPE (fntype), flags);
1245 /* If T is a template instantiation, dump the parameter binding. */
1246 if (template_parms != NULL_TREE && template_args != NULL_TREE)
1248 pp_cxx_whitespace (cxx_pp);
1249 pp_cxx_left_bracket (cxx_pp);
1250 pp_cxx_identifier (cxx_pp, "with");
1251 pp_cxx_whitespace (cxx_pp);
1252 dump_template_bindings (template_parms, template_args, typenames);
1253 pp_cxx_right_bracket (cxx_pp);
1257 /* Print a parameter list. If this is for a member function, the
1258 member object ptr (and any other hidden args) should have
1259 already been removed. */
1262 dump_parameters (tree parmtypes, int flags)
1265 pp_cxx_left_paren (cxx_pp);
1267 for (first = 1; parmtypes != void_list_node;
1268 parmtypes = TREE_CHAIN (parmtypes))
1271 pp_separate_with_comma (cxx_pp);
1275 pp_cxx_identifier (cxx_pp, "...");
1279 dump_type (TREE_VALUE (parmtypes), flags);
1281 if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && TREE_PURPOSE (parmtypes))
1283 pp_cxx_whitespace (cxx_pp);
1285 pp_cxx_whitespace (cxx_pp);
1286 dump_expr (TREE_PURPOSE (parmtypes), flags | TFF_EXPR_IN_PARENS);
1290 pp_cxx_right_paren (cxx_pp);
1293 /* Print an exception specification. T is the exception specification. */
1296 dump_exception_spec (tree t, int flags)
1300 pp_cxx_identifier (cxx_pp, "throw");
1301 pp_cxx_whitespace (cxx_pp);
1302 pp_cxx_left_paren (cxx_pp);
1303 if (TREE_VALUE (t) != NULL_TREE)
1306 dump_type (TREE_VALUE (t), flags);
1310 pp_separate_with_comma (cxx_pp);
1312 pp_cxx_right_paren (cxx_pp);
1316 /* Handle the function name for a FUNCTION_DECL node, grokking operators
1317 and destructors properly. */
1320 dump_function_name (tree t, int flags)
1322 tree name = DECL_NAME (t);
1324 /* We can get here with a decl that was synthesized by language-
1325 independent machinery (e.g. coverage.c) in which case it won't
1326 have a lang_specific structure attached and DECL_CONSTRUCTOR_P
1327 will crash. In this case it is safe just to print out the
1329 if (!DECL_LANG_SPECIFIC (t))
1331 pp_cxx_tree_identifier (cxx_pp, name);
1335 if (TREE_CODE (t) == TEMPLATE_DECL)
1336 t = DECL_TEMPLATE_RESULT (t);
1338 /* Don't let the user see __comp_ctor et al. */
1339 if (DECL_CONSTRUCTOR_P (t)
1340 || DECL_DESTRUCTOR_P (t))
1341 name = constructor_name (DECL_CONTEXT (t));
1343 if (DECL_DESTRUCTOR_P (t))
1345 pp_cxx_complement (cxx_pp);
1346 dump_decl (name, TFF_PLAIN_IDENTIFIER);
1348 else if (DECL_CONV_FN_P (t))
1350 /* This cannot use the hack that the operator's return
1351 type is stashed off of its name because it may be
1352 used for error reporting. In the case of conflicting
1353 declarations, both will have the same name, yet
1354 the types will be different, hence the TREE_TYPE field
1355 of the first name will be clobbered by the second. */
1356 pp_cxx_identifier (cxx_pp, "operator");
1357 dump_type (TREE_TYPE (TREE_TYPE (t)), flags);
1359 else if (IDENTIFIER_OPNAME_P (name))
1360 pp_cxx_tree_identifier (cxx_pp, name);
1362 dump_decl (name, flags);
1364 if (DECL_TEMPLATE_INFO (t)
1365 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (t)
1366 && (TREE_CODE (DECL_TI_TEMPLATE (t)) != TEMPLATE_DECL
1367 || PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
1368 dump_template_parms (DECL_TEMPLATE_INFO (t), !DECL_USE_TEMPLATE (t), flags);
1371 /* Dump the template parameters from the template info INFO under control of
1372 FLAGS. PRIMARY indicates whether this is a primary template decl, or
1373 specialization (partial or complete). For partial specializations we show
1374 the specialized parameter values. For a primary template we show no
1378 dump_template_parms (tree info, int primary, int flags)
1380 tree args = info ? TI_ARGS (info) : NULL_TREE;
1382 if (primary && flags & TFF_TEMPLATE_NAME)
1384 flags &= ~(TFF_CLASS_KEY_OR_ENUM | TFF_TEMPLATE_NAME);
1385 pp_cxx_begin_template_argument_list (cxx_pp);
1387 /* Be careful only to print things when we have them, so as not
1388 to crash producing error messages. */
1389 if (args && !primary)
1393 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
1394 args = TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 1);
1396 len = TREE_VEC_LENGTH (args);
1398 for (ix = 0; ix != len; ix++)
1400 tree arg = TREE_VEC_ELT (args, ix);
1402 /* Only print a comma if we know there is an argument coming. In
1403 the case of an empty template argument pack, no actual
1404 argument will be printed. */
1406 && (!ARGUMENT_PACK_P (arg)
1407 || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) > 0))
1408 pp_separate_with_comma (cxx_pp);
1411 pp_identifier (cxx_pp, "<template parameter error>");
1413 dump_template_argument (arg, flags);
1418 tree tpl = TI_TEMPLATE (info);
1419 tree parms = DECL_TEMPLATE_PARMS (tpl);
1422 parms = TREE_CODE (parms) == TREE_LIST ? TREE_VALUE (parms) : NULL_TREE;
1423 len = parms ? TREE_VEC_LENGTH (parms) : 0;
1425 for (ix = 0; ix != len; ix++)
1429 if (TREE_VEC_ELT (parms, ix) == error_mark_node)
1431 pp_identifier (cxx_pp, "<template parameter error>");
1435 parm = TREE_VALUE (TREE_VEC_ELT (parms, ix));
1438 pp_separate_with_comma (cxx_pp);
1440 dump_decl (parm, flags & ~TFF_DECL_SPECIFIERS);
1443 pp_cxx_end_template_argument_list (cxx_pp);
1446 /* Print out the arguments of CALL_EXPR T as a parenthesized list using
1447 flags FLAGS. Skip over the first argument if SKIPFIRST is true. */
1450 dump_call_expr_args (tree t, int flags, bool skipfirst)
1453 call_expr_arg_iterator iter;
1455 pp_cxx_left_paren (cxx_pp);
1456 FOR_EACH_CALL_EXPR_ARG (arg, iter, t)
1462 dump_expr (arg, flags | TFF_EXPR_IN_PARENS);
1463 if (more_call_expr_args_p (&iter))
1464 pp_separate_with_comma (cxx_pp);
1467 pp_cxx_right_paren (cxx_pp);
1470 /* Print out the arguments of AGGR_INIT_EXPR T as a parenthesized list
1471 using flags FLAGS. Skip over the first argument if SKIPFIRST is
1475 dump_aggr_init_expr_args (tree t, int flags, bool skipfirst)
1478 aggr_init_expr_arg_iterator iter;
1480 pp_cxx_left_paren (cxx_pp);
1481 FOR_EACH_AGGR_INIT_EXPR_ARG (arg, iter, t)
1487 dump_expr (arg, flags | TFF_EXPR_IN_PARENS);
1488 if (more_aggr_init_expr_args_p (&iter))
1489 pp_separate_with_comma (cxx_pp);
1492 pp_cxx_right_paren (cxx_pp);
1495 /* Print out a list of initializers (subr of dump_expr). */
1498 dump_expr_list (tree l, int flags)
1502 dump_expr (TREE_VALUE (l), flags | TFF_EXPR_IN_PARENS);
1505 pp_separate_with_comma (cxx_pp);
1509 /* Print out a vector of initializers (subr of dump_expr). */
1512 dump_expr_init_vec (VEC(constructor_elt,gc) *v, int flags)
1514 unsigned HOST_WIDE_INT idx;
1517 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
1519 dump_expr (value, flags | TFF_EXPR_IN_PARENS);
1520 if (idx != VEC_length (constructor_elt, v) - 1)
1521 pp_separate_with_comma (cxx_pp);
1526 /* We've gotten an indirect REFERENCE (an OBJ_TYPE_REF) to a virtual
1527 function. Resolve it to a close relative -- in the sense of static
1528 type -- variant being overridden. That is close to what was written in
1529 the source code. Subroutine of dump_expr. */
1532 resolve_virtual_fun_from_obj_type_ref (tree ref)
1534 tree obj_type = TREE_TYPE (OBJ_TYPE_REF_OBJECT (ref));
1535 HOST_WIDE_INT index = tree_low_cst (OBJ_TYPE_REF_TOKEN (ref), 1);
1536 tree fun = BINFO_VIRTUALS (TYPE_BINFO (TREE_TYPE (obj_type)));
1539 fun = TREE_CHAIN (fun);
1540 index -= (TARGET_VTABLE_USES_DESCRIPTORS
1541 ? TARGET_VTABLE_USES_DESCRIPTORS : 1);
1547 /* Print out an expression E under control of FLAGS. */
1550 dump_expr (tree t, int flags)
1555 if (STATEMENT_CLASS_P (t))
1557 pp_cxx_identifier (cxx_pp, "<statement>");
1561 switch (TREE_CODE (t))
1569 case NAMESPACE_DECL:
1572 case IDENTIFIER_NODE:
1573 dump_decl (t, (flags & ~TFF_DECL_SPECIFIERS) | TFF_NO_FUNCTION_ARGUMENTS);
1580 pp_constant (cxx_pp, t);
1584 /* While waiting for caret diagnostics, avoid printing
1585 __cxa_allocate_exception, __cxa_throw, and the like. */
1586 pp_cxx_identifier (cxx_pp, "<throw-expression>");
1590 pp_ampersand (cxx_pp);
1591 dump_type (PTRMEM_CST_CLASS (t), flags);
1592 pp_cxx_colon_colon (cxx_pp);
1593 pp_cxx_tree_identifier (cxx_pp, DECL_NAME (PTRMEM_CST_MEMBER (t)));
1597 pp_cxx_left_paren (cxx_pp);
1598 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1599 pp_separate_with_comma (cxx_pp);
1600 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
1601 pp_cxx_right_paren (cxx_pp);
1605 pp_cxx_left_paren (cxx_pp);
1606 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1607 pp_string (cxx_pp, " ? ");
1608 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
1609 pp_string (cxx_pp, " : ");
1610 dump_expr (TREE_OPERAND (t, 2), flags | TFF_EXPR_IN_PARENS);
1611 pp_cxx_right_paren (cxx_pp);
1615 if (TREE_HAS_CONSTRUCTOR (t))
1617 pp_cxx_identifier (cxx_pp, "new");
1618 pp_cxx_whitespace (cxx_pp);
1619 dump_type (TREE_TYPE (TREE_TYPE (t)), flags);
1622 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1625 case AGGR_INIT_EXPR:
1627 tree fn = NULL_TREE;
1629 if (TREE_CODE (AGGR_INIT_EXPR_FN (t)) == ADDR_EXPR)
1630 fn = TREE_OPERAND (AGGR_INIT_EXPR_FN (t), 0);
1632 if (fn && TREE_CODE (fn) == FUNCTION_DECL)
1634 if (DECL_CONSTRUCTOR_P (fn))
1635 dump_type (DECL_CONTEXT (fn), flags);
1640 dump_expr (AGGR_INIT_EXPR_FN (t), 0);
1642 dump_aggr_init_expr_args (t, flags, true);
1647 tree fn = CALL_EXPR_FN (t);
1648 bool skipfirst = false;
1650 if (TREE_CODE (fn) == ADDR_EXPR)
1651 fn = TREE_OPERAND (fn, 0);
1653 /* Nobody is interested in seeing the guts of vcalls. */
1654 if (TREE_CODE (fn) == OBJ_TYPE_REF)
1655 fn = resolve_virtual_fun_from_obj_type_ref (fn);
1657 if (TREE_TYPE (fn) != NULL_TREE && NEXT_CODE (fn) == METHOD_TYPE)
1659 tree ob = CALL_EXPR_ARG (t, 0);
1660 if (TREE_CODE (ob) == ADDR_EXPR)
1662 dump_expr (TREE_OPERAND (ob, 0), flags | TFF_EXPR_IN_PARENS);
1663 pp_cxx_dot (cxx_pp);
1665 else if (TREE_CODE (ob) != PARM_DECL
1666 || strcmp (IDENTIFIER_POINTER (DECL_NAME (ob)), "this"))
1668 dump_expr (ob, flags | TFF_EXPR_IN_PARENS);
1669 pp_cxx_arrow (cxx_pp);
1673 dump_expr (fn, flags | TFF_EXPR_IN_PARENS);
1674 dump_call_expr_args (t, flags, skipfirst);
1679 /* Note that this only works for G++ target exprs. If somebody
1680 builds a general TARGET_EXPR, there's no way to represent that
1681 it initializes anything other that the parameter slot for the
1682 default argument. Note we may have cleared out the first
1683 operand in expand_expr, so don't go killing ourselves. */
1684 if (TREE_OPERAND (t, 1))
1685 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
1688 case POINTER_PLUS_EXPR:
1689 dump_binary_op ("+", t, flags);
1697 case TRUNC_DIV_EXPR:
1698 case TRUNC_MOD_EXPR:
1706 case TRUTH_ANDIF_EXPR:
1707 case TRUTH_ORIF_EXPR:
1714 case EXACT_DIV_EXPR:
1715 dump_binary_op (operator_name_info[(int) TREE_CODE (t)].name, t, flags);
1719 case FLOOR_DIV_EXPR:
1720 case ROUND_DIV_EXPR:
1722 dump_binary_op ("/", t, flags);
1726 case FLOOR_MOD_EXPR:
1727 case ROUND_MOD_EXPR:
1728 dump_binary_op ("%", t, flags);
1733 tree ob = TREE_OPERAND (t, 0);
1734 if (TREE_CODE (ob) == INDIRECT_REF)
1736 ob = TREE_OPERAND (ob, 0);
1737 if (TREE_CODE (ob) != PARM_DECL
1739 && strcmp (IDENTIFIER_POINTER (DECL_NAME (ob)), "this")))
1741 dump_expr (ob, flags | TFF_EXPR_IN_PARENS);
1742 pp_cxx_arrow (cxx_pp);
1747 dump_expr (ob, flags | TFF_EXPR_IN_PARENS);
1748 pp_cxx_dot (cxx_pp);
1750 dump_expr (TREE_OPERAND (t, 1), flags & ~TFF_EXPR_IN_PARENS);
1755 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1756 pp_cxx_left_bracket (cxx_pp);
1757 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
1758 pp_cxx_right_bracket (cxx_pp);
1761 case UNARY_PLUS_EXPR:
1762 dump_unary_op ("+", t, flags);
1766 if (TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
1767 || TREE_CODE (TREE_OPERAND (t, 0)) == STRING_CST
1768 /* An ADDR_EXPR can have reference type. In that case, we
1769 shouldn't print the `&' doing so indicates to the user
1770 that the expression has pointer type. */
1772 && TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE))
1773 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1774 else if (TREE_CODE (TREE_OPERAND (t, 0)) == LABEL_DECL)
1775 dump_unary_op ("&&", t, flags);
1777 dump_unary_op ("&", t, flags);
1781 if (TREE_HAS_CONSTRUCTOR (t))
1783 t = TREE_OPERAND (t, 0);
1784 gcc_assert (TREE_CODE (t) == CALL_EXPR);
1785 dump_expr (CALL_EXPR_FN (t), flags | TFF_EXPR_IN_PARENS);
1786 dump_call_expr_args (t, flags, true);
1790 if (TREE_OPERAND (t,0) != NULL_TREE
1791 && TREE_TYPE (TREE_OPERAND (t, 0))
1792 && NEXT_CODE (TREE_OPERAND (t, 0)) == REFERENCE_TYPE)
1793 dump_expr (TREE_OPERAND (t, 0), flags);
1795 dump_unary_op ("*", t, flags);
1801 case TRUTH_NOT_EXPR:
1802 case PREDECREMENT_EXPR:
1803 case PREINCREMENT_EXPR:
1804 dump_unary_op (operator_name_info [(int)TREE_CODE (t)].name, t, flags);
1807 case POSTDECREMENT_EXPR:
1808 case POSTINCREMENT_EXPR:
1809 pp_cxx_left_paren (cxx_pp);
1810 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1811 pp_cxx_identifier (cxx_pp, operator_name_info[(int)TREE_CODE (t)].name);
1812 pp_cxx_right_paren (cxx_pp);
1815 case NON_LVALUE_EXPR:
1816 /* FIXME: This is a KLUDGE workaround for a parsing problem. There
1817 should be another level of INDIRECT_REF so that I don't have to do
1819 if (TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == POINTER_TYPE)
1821 tree next = TREE_TYPE (TREE_TYPE (t));
1823 while (TREE_CODE (next) == POINTER_TYPE)
1824 next = TREE_TYPE (next);
1826 if (TREE_CODE (next) == FUNCTION_TYPE)
1828 if (flags & TFF_EXPR_IN_PARENS)
1829 pp_cxx_left_paren (cxx_pp);
1830 pp_cxx_star (cxx_pp);
1831 dump_expr (TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
1832 if (flags & TFF_EXPR_IN_PARENS)
1833 pp_cxx_right_paren (cxx_pp);
1836 /* Else fall through. */
1838 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1842 case VIEW_CONVERT_EXPR:
1844 tree op = TREE_OPERAND (t, 0);
1846 if (!same_type_p (TREE_TYPE (op), TREE_TYPE (t)))
1848 /* It is a cast, but we cannot tell whether it is a
1849 reinterpret or static cast. Use the C style notation. */
1850 if (flags & TFF_EXPR_IN_PARENS)
1851 pp_cxx_left_paren (cxx_pp);
1852 pp_cxx_left_paren (cxx_pp);
1853 dump_type (TREE_TYPE (t), flags);
1854 pp_cxx_right_paren (cxx_pp);
1855 dump_expr (op, flags | TFF_EXPR_IN_PARENS);
1856 if (flags & TFF_EXPR_IN_PARENS)
1857 pp_cxx_right_paren (cxx_pp);
1860 dump_expr (op, flags);
1865 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
1867 tree idx = build_ptrmemfunc_access_expr (t, pfn_identifier);
1869 if (integer_zerop (idx))
1871 /* A NULL pointer-to-member constant. */
1872 pp_cxx_left_paren (cxx_pp);
1873 pp_cxx_left_paren (cxx_pp);
1874 dump_type (TREE_TYPE (t), flags);
1875 pp_cxx_right_paren (cxx_pp);
1876 pp_character (cxx_pp, '0');
1877 pp_cxx_right_paren (cxx_pp);
1880 else if (host_integerp (idx, 0))
1883 unsigned HOST_WIDE_INT n;
1885 t = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
1886 t = TYPE_METHOD_BASETYPE (t);
1887 virtuals = BINFO_VIRTUALS (TYPE_BINFO (TYPE_MAIN_VARIANT (t)));
1889 n = tree_low_cst (idx, 0);
1891 /* Map vtable index back one, to allow for the null pointer to
1895 while (n > 0 && virtuals)
1898 virtuals = TREE_CHAIN (virtuals);
1902 dump_expr (BV_FN (virtuals),
1903 flags | TFF_EXPR_IN_PARENS);
1908 if (TREE_TYPE (t) && EMPTY_CONSTRUCTOR_P (t))
1910 dump_type (TREE_TYPE (t), 0);
1911 pp_cxx_left_paren (cxx_pp);
1912 pp_cxx_right_paren (cxx_pp);
1916 pp_cxx_left_brace (cxx_pp);
1917 dump_expr_init_vec (CONSTRUCTOR_ELTS (t), flags);
1918 pp_cxx_right_brace (cxx_pp);
1925 tree ob = TREE_OPERAND (t, 0);
1926 if (is_dummy_object (ob))
1928 t = TREE_OPERAND (t, 1);
1929 if (TREE_CODE (t) == FUNCTION_DECL)
1931 dump_expr (t, flags | TFF_EXPR_IN_PARENS);
1932 else if (BASELINK_P (t))
1933 dump_expr (OVL_CURRENT (BASELINK_FUNCTIONS (t)),
1934 flags | TFF_EXPR_IN_PARENS);
1936 dump_decl (t, flags);
1940 if (TREE_CODE (ob) == INDIRECT_REF)
1942 dump_expr (TREE_OPERAND (ob, 0), flags | TFF_EXPR_IN_PARENS);
1943 pp_cxx_arrow (cxx_pp);
1944 pp_cxx_star (cxx_pp);
1948 dump_expr (ob, flags | TFF_EXPR_IN_PARENS);
1949 pp_cxx_dot (cxx_pp);
1950 pp_cxx_star (cxx_pp);
1952 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
1957 case TEMPLATE_PARM_INDEX:
1958 dump_decl (TEMPLATE_PARM_DECL (t), flags & ~TFF_DECL_SPECIFIERS);
1962 if (TREE_OPERAND (t, 0) == NULL_TREE
1963 || TREE_CHAIN (TREE_OPERAND (t, 0)))
1965 dump_type (TREE_TYPE (t), flags);
1966 pp_cxx_left_paren (cxx_pp);
1967 dump_expr_list (TREE_OPERAND (t, 0), flags);
1968 pp_cxx_right_paren (cxx_pp);
1972 pp_cxx_left_paren (cxx_pp);
1973 dump_type (TREE_TYPE (t), flags);
1974 pp_cxx_right_paren (cxx_pp);
1975 pp_cxx_left_paren (cxx_pp);
1976 dump_expr_list (TREE_OPERAND (t, 0), flags);
1977 pp_cxx_right_paren (cxx_pp);
1981 case STATIC_CAST_EXPR:
1982 pp_cxx_identifier (cxx_pp, "static_cast");
1984 case REINTERPRET_CAST_EXPR:
1985 pp_cxx_identifier (cxx_pp, "reinterpret_cast");
1987 case CONST_CAST_EXPR:
1988 pp_cxx_identifier (cxx_pp, "const_cast");
1990 case DYNAMIC_CAST_EXPR:
1991 pp_cxx_identifier (cxx_pp, "dynamic_cast");
1993 pp_cxx_begin_template_argument_list (cxx_pp);
1994 dump_type (TREE_TYPE (t), flags);
1995 pp_cxx_end_template_argument_list (cxx_pp);
1996 pp_cxx_left_paren (cxx_pp);
1997 dump_expr (TREE_OPERAND (t, 0), flags);
1998 pp_cxx_right_paren (cxx_pp);
2002 dump_expr (TREE_OPERAND (t, 0), flags);
2003 pp_cxx_arrow (cxx_pp);
2008 if (TREE_CODE (t) == SIZEOF_EXPR)
2009 pp_cxx_identifier (cxx_pp, "sizeof");
2012 gcc_assert (TREE_CODE (t) == ALIGNOF_EXPR);
2013 pp_cxx_identifier (cxx_pp, "__alignof__");
2015 pp_cxx_whitespace (cxx_pp);
2016 pp_cxx_left_paren (cxx_pp);
2017 if (TYPE_P (TREE_OPERAND (t, 0)))
2018 dump_type (TREE_OPERAND (t, 0), flags);
2020 dump_expr (TREE_OPERAND (t, 0), flags);
2021 pp_cxx_right_paren (cxx_pp);
2026 pp_cxx_identifier (cxx_pp, operator_name_info[TREE_CODE (t)].name);
2027 pp_cxx_whitespace (cxx_pp);
2028 dump_expr (TREE_OPERAND (t, 0), flags);
2032 pp_identifier (cxx_pp, "<unparsed>");
2035 case TRY_CATCH_EXPR:
2036 case WITH_CLEANUP_EXPR:
2037 case CLEANUP_POINT_EXPR:
2038 dump_expr (TREE_OPERAND (t, 0), flags);
2041 case PSEUDO_DTOR_EXPR:
2042 dump_expr (TREE_OPERAND (t, 2), flags);
2043 pp_cxx_dot (cxx_pp);
2044 dump_type (TREE_OPERAND (t, 0), flags);
2045 pp_cxx_colon_colon (cxx_pp);
2046 pp_cxx_complement (cxx_pp);
2047 dump_type (TREE_OPERAND (t, 1), flags);
2050 case TEMPLATE_ID_EXPR:
2051 dump_decl (t, flags);
2057 case STATEMENT_LIST:
2058 /* We don't yet have a way of dumping statements in a
2059 human-readable format. */
2060 pp_string (cxx_pp, "({...})");
2064 pp_string (cxx_pp, "while (1) { ");
2065 dump_expr (TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2066 pp_cxx_right_brace (cxx_pp);
2070 pp_string (cxx_pp, "if (");
2071 dump_expr (TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2072 pp_string (cxx_pp, ") break; ");
2076 dump_expr (get_first_fn (t), flags & ~TFF_EXPR_IN_PARENS);
2079 case EMPTY_CLASS_EXPR:
2080 dump_type (TREE_TYPE (t), flags);
2081 pp_cxx_left_paren (cxx_pp);
2082 pp_cxx_right_paren (cxx_pp);
2085 case NON_DEPENDENT_EXPR:
2086 dump_expr (TREE_OPERAND (t, 0), flags);
2089 case ARGUMENT_PACK_SELECT:
2090 dump_template_argument (ARGUMENT_PACK_SELECT_FROM_PACK (t), flags);
2102 pp_type_specifier_seq (cxx_pp, t);
2106 /* We get here when we want to print a dependent type as an
2107 id-expression, without any disambiguator decoration. */
2108 pp_id_expression (cxx_pp, t);
2111 case TEMPLATE_TYPE_PARM:
2112 case BOUND_TEMPLATE_TEMPLATE_PARM:
2113 dump_type (t, flags);
2117 pp_cxx_trait_expression (cxx_pp, t);
2121 pp_cxx_va_arg_expression (cxx_pp, t);
2125 pp_cxx_offsetof_expression (cxx_pp, t);
2129 case EXPR_PACK_EXPANSION:
2136 case VEC_DELETE_EXPR:
2142 case UNORDERED_EXPR:
2152 case FIX_TRUNC_EXPR:
2154 pp_expression (cxx_pp, t);
2157 case TRUTH_AND_EXPR:
2159 case TRUTH_XOR_EXPR:
2160 if (flags & TFF_EXPR_IN_PARENS)
2161 pp_cxx_left_paren (cxx_pp);
2162 pp_expression (cxx_pp, t);
2163 if (flags & TFF_EXPR_IN_PARENS)
2164 pp_cxx_right_paren (cxx_pp);
2168 dump_expr (resolve_virtual_fun_from_obj_type_ref (t), flags);
2171 /* This list is incomplete, but should suffice for now.
2172 It is very important that `sorry' does not call
2173 `report_error_function'. That could cause an infinite loop. */
2175 pp_unsupported_tree (cxx_pp, t);
2176 /* fall through to ERROR_MARK... */
2178 pp_identifier (cxx_pp, "<expression error>");
2184 dump_binary_op (const char *opstring, tree t, int flags)
2186 pp_cxx_left_paren (cxx_pp);
2187 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2188 pp_cxx_whitespace (cxx_pp);
2190 pp_cxx_identifier (cxx_pp, opstring);
2192 pp_identifier (cxx_pp, "<unknown operator>");
2193 pp_cxx_whitespace (cxx_pp);
2194 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2195 pp_cxx_right_paren (cxx_pp);
2199 dump_unary_op (const char *opstring, tree t, int flags)
2201 if (flags & TFF_EXPR_IN_PARENS)
2202 pp_cxx_left_paren (cxx_pp);
2203 pp_cxx_identifier (cxx_pp, opstring);
2204 dump_expr (TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2205 if (flags & TFF_EXPR_IN_PARENS)
2206 pp_cxx_right_paren (cxx_pp);
2210 reinit_cxx_pp (void)
2212 pp_clear_output_area (cxx_pp);
2213 pp_base (cxx_pp)->padding = pp_none;
2214 pp_indentation (cxx_pp) = 0;
2215 pp_needs_newline (cxx_pp) = false;
2216 cxx_pp->enclosing_scope = current_function_decl;
2220 /* Exported interface to stringifying types, exprs and decls under TFF_*
2224 type_as_string (tree typ, int flags)
2227 dump_type (typ, flags);
2228 return pp_formatted_text (cxx_pp);
2232 expr_as_string (tree decl, int flags)
2235 dump_expr (decl, flags);
2236 return pp_formatted_text (cxx_pp);
2240 decl_as_string (tree decl, int flags)
2243 dump_decl (decl, flags);
2244 return pp_formatted_text (cxx_pp);
2247 /* Generate the three forms of printable names for cxx_printable_name. */
2250 lang_decl_name (tree decl, int v)
2253 return decl_as_string (decl, TFF_DECL_SPECIFIERS);
2256 if (v == 1 && DECL_CLASS_SCOPE_P (decl))
2258 dump_type (CP_DECL_CONTEXT (decl), TFF_PLAIN_IDENTIFIER);
2259 pp_cxx_colon_colon (cxx_pp);
2262 if (TREE_CODE (decl) == FUNCTION_DECL)
2263 dump_function_name (decl, TFF_PLAIN_IDENTIFIER);
2265 dump_decl (DECL_NAME (decl), TFF_PLAIN_IDENTIFIER);
2267 return pp_formatted_text (cxx_pp);
2270 /* Return the location of a tree passed to %+ formats. */
2273 location_of (tree t)
2275 if (TREE_CODE (t) == PARM_DECL && DECL_CONTEXT (t))
2276 t = DECL_CONTEXT (t);
2277 else if (TYPE_P (t))
2278 t = TYPE_MAIN_DECL (t);
2279 else if (TREE_CODE (t) == OVERLOAD)
2280 t = OVL_FUNCTION (t);
2282 return DECL_SOURCE_LOCATION (t);
2285 /* Now the interfaces from error et al to dump_type et al. Each takes an
2286 on/off VERBOSE flag and supply the appropriate TFF_ flags to a dump_
2290 decl_to_string (tree decl, int verbose)
2294 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == RECORD_TYPE
2295 || TREE_CODE (decl) == UNION_TYPE || TREE_CODE (decl) == ENUMERAL_TYPE)
2296 flags = TFF_CLASS_KEY_OR_ENUM;
2298 flags |= TFF_DECL_SPECIFIERS;
2299 else if (TREE_CODE (decl) == FUNCTION_DECL)
2300 flags |= TFF_DECL_SPECIFIERS | TFF_RETURN_TYPE;
2301 flags |= TFF_TEMPLATE_HEADER;
2304 dump_decl (decl, flags);
2305 return pp_formatted_text (cxx_pp);
2309 expr_to_string (tree decl)
2312 dump_expr (decl, 0);
2313 return pp_formatted_text (cxx_pp);
2317 fndecl_to_string (tree fndecl, int verbose)
2321 flags = TFF_EXCEPTION_SPECIFICATION | TFF_DECL_SPECIFIERS
2322 | TFF_TEMPLATE_HEADER;
2324 flags |= TFF_FUNCTION_DEFAULT_ARGUMENTS;
2326 dump_decl (fndecl, flags);
2327 return pp_formatted_text (cxx_pp);
2332 code_to_string (enum tree_code c)
2334 return tree_code_name [c];
2338 language_to_string (enum languages c)
2345 case lang_cplusplus:
2357 /* Return the proper printed version of a parameter to a C++ function. */
2360 parm_to_string (int p)
2364 pp_string (cxx_pp, "'this'");
2366 pp_decimal_int (cxx_pp, p + 1);
2367 return pp_formatted_text (cxx_pp);
2371 op_to_string (enum tree_code p)
2373 tree id = operator_name_info[(int) p].identifier;
2374 return id ? IDENTIFIER_POINTER (id) : "<unknown>";
2378 type_to_string (tree typ, int verbose)
2382 flags |= TFF_CLASS_KEY_OR_ENUM;
2383 flags |= TFF_TEMPLATE_HEADER;
2386 dump_type (typ, flags);
2387 return pp_formatted_text (cxx_pp);
2391 assop_to_string (enum tree_code p)
2393 tree id = assignment_operator_name_info[(int) p].identifier;
2394 return id ? IDENTIFIER_POINTER (id) : "{unknown}";
2398 args_to_string (tree p, int verbose)
2402 flags |= TFF_CLASS_KEY_OR_ENUM;
2407 if (TYPE_P (TREE_VALUE (p)))
2408 return type_as_string (p, flags);
2411 for (; p; p = TREE_CHAIN (p))
2413 if (TREE_VALUE (p) == null_node)
2414 pp_cxx_identifier (cxx_pp, "NULL");
2416 dump_type (error_type (TREE_VALUE (p)), flags);
2418 pp_separate_with_comma (cxx_pp);
2420 return pp_formatted_text (cxx_pp);
2424 cv_to_string (tree p, int v)
2427 pp_base (cxx_pp)->padding = v ? pp_before : pp_none;
2428 pp_cxx_cv_qualifier_seq (cxx_pp, p);
2429 return pp_formatted_text (cxx_pp);
2432 /* Langhook for print_error_function. */
2434 cxx_print_error_function (diagnostic_context *context, const char *file,
2435 diagnostic_info *diagnostic)
2437 lhd_print_error_function (context, file, diagnostic);
2438 pp_base_set_prefix (context->printer, file);
2439 maybe_print_instantiation_context (context);
2443 cp_diagnostic_starter (diagnostic_context *context,
2444 diagnostic_info *diagnostic)
2446 diagnostic_report_current_module (context);
2447 cp_print_error_function (context, diagnostic);
2448 maybe_print_instantiation_context (context);
2449 pp_base_set_prefix (context->printer, diagnostic_build_prefix (diagnostic));
2453 cp_diagnostic_finalizer (diagnostic_context *context,
2454 diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
2456 pp_base_destroy_prefix (context->printer);
2459 /* Print current function onto BUFFER, in the process of reporting
2460 a diagnostic message. Called from cp_diagnostic_starter. */
2462 cp_print_error_function (diagnostic_context *context,
2463 diagnostic_info *diagnostic)
2465 if (diagnostic_last_function_changed (context, diagnostic))
2467 const char *old_prefix = context->printer->prefix;
2468 const char *file = LOCATION_FILE (diagnostic->location);
2469 tree abstract_origin = diagnostic->abstract_origin;
2470 char *new_prefix = (file && abstract_origin == NULL)
2471 ? file_name_as_prefix (file) : NULL;
2473 pp_base_set_prefix (context->printer, new_prefix);
2475 if (current_function_decl == NULL)
2476 pp_base_string (context->printer, "At global scope:");
2481 if (abstract_origin)
2483 ao = BLOCK_ABSTRACT_ORIGIN (abstract_origin);
2484 while (TREE_CODE (ao) == BLOCK
2485 && BLOCK_ABSTRACT_ORIGIN (ao)
2486 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
2487 ao = BLOCK_ABSTRACT_ORIGIN (ao);
2488 gcc_assert (TREE_CODE (ao) == FUNCTION_DECL);
2492 fndecl = current_function_decl;
2494 pp_printf (context->printer, "In %s %qs",
2495 function_category (fndecl),
2496 cxx_printable_name (fndecl, 2));
2498 while (abstract_origin)
2501 tree block = abstract_origin;
2503 locus = &BLOCK_SOURCE_LOCATION (block);
2505 block = BLOCK_SUPERCONTEXT (block);
2506 while (block && TREE_CODE (block) == BLOCK
2507 && BLOCK_ABSTRACT_ORIGIN (block))
2509 ao = BLOCK_ABSTRACT_ORIGIN (block);
2511 while (TREE_CODE (ao) == BLOCK
2512 && BLOCK_ABSTRACT_ORIGIN (ao)
2513 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
2514 ao = BLOCK_ABSTRACT_ORIGIN (ao);
2516 if (TREE_CODE (ao) == FUNCTION_DECL)
2521 else if (TREE_CODE (ao) != BLOCK)
2524 block = BLOCK_SUPERCONTEXT (block);
2527 abstract_origin = block;
2530 while (block && TREE_CODE (block) == BLOCK)
2531 block = BLOCK_SUPERCONTEXT (block);
2533 if (TREE_CODE (block) == FUNCTION_DECL)
2535 abstract_origin = NULL;
2539 expanded_location s = expand_location (*locus);
2540 pp_base_character (context->printer, ',');
2541 pp_base_newline (context->printer);
2544 if (flag_show_column && s.column != 0)
2545 pp_printf (context->printer,
2546 " inlined from %qs at %s:%d:%d",
2547 cxx_printable_name (fndecl, 2),
2548 s.file, s.line, s.column);
2550 pp_printf (context->printer,
2551 " inlined from %qs at %s:%d",
2552 cxx_printable_name (fndecl, 2),
2557 pp_printf (context->printer, " inlined from %qs",
2558 cxx_printable_name (fndecl, 2));
2561 pp_base_character (context->printer, ':');
2563 pp_base_newline (context->printer);
2565 diagnostic_set_last_function (context, diagnostic);
2566 pp_base_destroy_prefix (context->printer);
2567 context->printer->prefix = old_prefix;
2571 /* Returns a description of FUNCTION using standard terminology. */
2573 function_category (tree fn)
2575 if (DECL_FUNCTION_MEMBER_P (fn))
2577 if (DECL_STATIC_FUNCTION_P (fn))
2578 return "static member function";
2579 else if (DECL_COPY_CONSTRUCTOR_P (fn))
2580 return "copy constructor";
2581 else if (DECL_CONSTRUCTOR_P (fn))
2582 return "constructor";
2583 else if (DECL_DESTRUCTOR_P (fn))
2584 return "destructor";
2586 return "member function";
2592 /* Report the full context of a current template instantiation,
2595 print_instantiation_full_context (diagnostic_context *context)
2597 struct tinst_level *p = current_instantiation ();
2598 location_t location = input_location;
2602 if (current_function_decl != p->decl
2603 && current_function_decl != NULL_TREE)
2604 /* We can get here during the processing of some synthesized
2605 method. Then, P->DECL will be the function that's causing
2610 if (current_function_decl == p->decl)
2611 /* Avoid redundancy with the "In function" line. */;
2613 pp_verbatim (context->printer,
2614 "%s: In instantiation of %qs:\n",
2615 LOCATION_FILE (location),
2616 decl_as_string (p->decl,
2617 TFF_DECL_SPECIFIERS | TFF_RETURN_TYPE));
2619 location = p->locus;
2624 print_instantiation_partial_context (context, p, location);
2627 /* Same as above but less verbose. */
2629 print_instantiation_partial_context (diagnostic_context *context,
2630 struct tinst_level *t, location_t loc)
2632 expanded_location xloc;
2633 for (; ; t = t->next)
2635 xloc = expand_location (loc);
2638 pp_verbatim (context->printer, "%s:%d: instantiated from %qs\n",
2639 xloc.file, xloc.line,
2640 decl_as_string (t->decl,
2641 TFF_DECL_SPECIFIERS | TFF_RETURN_TYPE));
2644 pp_verbatim (context->printer, "%s:%d: instantiated from here",
2645 xloc.file, xloc.line);
2646 pp_base_newline (context->printer);
2649 /* Called from cp_thing to print the template context for an error. */
2651 maybe_print_instantiation_context (diagnostic_context *context)
2653 if (!problematic_instantiation_changed () || current_instantiation () == 0)
2656 record_last_problematic_instantiation ();
2657 print_instantiation_full_context (context);
2660 /* Report the bare minimum context of a template instantiation. */
2662 print_instantiation_context (void)
2664 print_instantiation_partial_context
2665 (global_dc, current_instantiation (), input_location);
2666 diagnostic_flush_buffer (global_dc);
2669 /* Called from output_format -- during diagnostic message processing --
2670 to handle C++ specific format specifier with the following meanings:
2671 %A function argument-list.
2675 %F function declaration.
2676 %L language as used in extern "lang".
2678 %P function parameter whose position is indicated by an integer.
2679 %Q assignment operator.
2683 cp_printer (pretty_printer *pp, text_info *text, const char *spec,
2684 int precision, bool wide, bool set_locus, bool verbose)
2688 #define next_tree (t = va_arg (*text->args_ptr, tree))
2689 #define next_tcode va_arg (*text->args_ptr, enum tree_code)
2690 #define next_lang va_arg (*text->args_ptr, enum languages)
2691 #define next_int va_arg (*text->args_ptr, int)
2693 if (precision != 0 || wide)
2696 if (text->locus == NULL)
2701 case 'A': result = args_to_string (next_tree, verbose); break;
2702 case 'C': result = code_to_string (next_tcode); break;
2705 tree temp = next_tree;
2707 && DECL_DEBUG_EXPR_IS_FROM (temp) && DECL_DEBUG_EXPR (temp))
2709 temp = DECL_DEBUG_EXPR (temp);
2712 result = expr_to_string (temp);
2716 result = decl_to_string (temp, verbose);
2719 case 'E': result = expr_to_string (next_tree); break;
2720 case 'F': result = fndecl_to_string (next_tree, verbose); break;
2721 case 'L': result = language_to_string (next_lang); break;
2722 case 'O': result = op_to_string (next_tcode); break;
2723 case 'P': result = parm_to_string (next_int); break;
2724 case 'Q': result = assop_to_string (next_tcode); break;
2725 case 'T': result = type_to_string (next_tree, verbose); break;
2726 case 'V': result = cv_to_string (next_tree, verbose); break;
2732 pp_base_string (pp, result);
2733 if (set_locus && t != NULL)
2734 *text->locus = location_of (t);
2742 /* Warn about the use of C++0x features when appropriate. */
2744 maybe_warn_cpp0x (const char* str)
2746 if ((cxx_dialect == cxx98) && !in_system_header)
2747 /* We really want to suppress this warning in system headers,
2748 because libstdc++ uses variadic templates even when we aren't
2750 pedwarn (input_location, 0, "%s only available with -std=c++0x or -std=gnu++0x", str);
2753 /* Warn about the use of variadic templates when appropriate. */
2755 maybe_warn_variadic_templates (void)
2757 maybe_warn_cpp0x ("variadic templates");