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"
33 #include "cxx-pretty-print.h"
34 #include "pointer-set.h"
36 #define pp_separate_with_comma(PP) pp_cxx_separate_with (PP, ',')
38 /* The global buffer where we dump everything. It is there only for
39 transitional purpose. It is expected, in the near future, to be
40 completely removed. */
41 static cxx_pretty_printer scratch_pretty_printer;
42 #define cxx_pp (&scratch_pretty_printer)
44 /* Translate if being used for diagnostics, but not for dump files or
46 #define M_(msgid) (pp_translate_identifiers (cxx_pp) ? _(msgid) : (msgid))
48 # define NEXT_CODE(T) (TREE_CODE (TREE_TYPE (T)))
50 static const char *args_to_string (tree, int);
51 static const char *assop_to_string (enum tree_code);
52 static const char *code_to_string (enum tree_code);
53 static const char *cv_to_string (tree, int);
54 static const char *decl_to_string (tree, int);
55 static const char *expr_to_string (tree);
56 static const char *fndecl_to_string (tree, int);
57 static const char *op_to_string (enum tree_code);
58 static const char *parm_to_string (int);
59 static const char *type_to_string (tree, int);
61 static void dump_type (tree, int);
62 static void dump_typename (tree, int);
63 static void dump_simple_decl (tree, tree, int);
64 static void dump_decl (tree, int);
65 static void dump_template_decl (tree, int);
66 static void dump_function_decl (tree, int);
67 static void dump_expr (tree, int);
68 static void dump_unary_op (const char *, tree, int);
69 static void dump_binary_op (const char *, tree, int);
70 static void dump_aggr_type (tree, int);
71 static void dump_type_prefix (tree, int);
72 static void dump_type_suffix (tree, int);
73 static void dump_function_name (tree, int);
74 static void dump_call_expr_args (tree, int, bool);
75 static void dump_aggr_init_expr_args (tree, int, bool);
76 static void dump_expr_list (tree, int);
77 static void dump_global_iord (tree);
78 static void dump_parameters (tree, int);
79 static void dump_exception_spec (tree, int);
80 static void dump_template_argument (tree, int);
81 static void dump_template_argument_list (tree, int);
82 static void dump_template_parameter (tree, int);
83 static void dump_template_bindings (tree, tree, VEC(tree,gc) *);
84 static void dump_scope (tree, int);
85 static void dump_template_parms (tree, int, int);
86 static int get_non_default_template_args_count (tree, int);
87 static const char *function_category (tree);
88 static void maybe_print_instantiation_context (diagnostic_context *);
89 static void print_instantiation_full_context (diagnostic_context *);
90 static void print_instantiation_partial_context (diagnostic_context *,
93 static void cp_diagnostic_starter (diagnostic_context *, diagnostic_info *);
94 static void cp_diagnostic_finalizer (diagnostic_context *, diagnostic_info *);
95 static void cp_print_error_function (diagnostic_context *, diagnostic_info *);
97 static bool cp_printer (pretty_printer *, text_info *, const char *,
98 int, bool, bool, bool);
99 static location_t location_of (tree);
104 diagnostic_starter (global_dc) = cp_diagnostic_starter;
105 diagnostic_finalizer (global_dc) = cp_diagnostic_finalizer;
106 diagnostic_format_decoder (global_dc) = cp_printer;
108 pp_construct (pp_base (cxx_pp), NULL, 0);
109 pp_cxx_pretty_printer_init (cxx_pp);
112 /* Dump a scope, if deemed necessary. */
115 dump_scope (tree scope, int flags)
117 int f = ~TFF_RETURN_TYPE & (flags & (TFF_SCOPE | TFF_CHASE_TYPEDEF));
119 if (scope == NULL_TREE)
122 if (TREE_CODE (scope) == NAMESPACE_DECL)
124 if (scope != global_namespace)
126 dump_decl (scope, f);
127 pp_cxx_colon_colon (cxx_pp);
130 else if (AGGREGATE_TYPE_P (scope))
132 dump_type (scope, f);
133 pp_cxx_colon_colon (cxx_pp);
135 else if ((flags & TFF_SCOPE) && TREE_CODE (scope) == FUNCTION_DECL)
137 dump_function_decl (scope, f);
138 pp_cxx_colon_colon (cxx_pp);
142 /* Dump the template ARGument under control of FLAGS. */
145 dump_template_argument (tree arg, int flags)
147 if (ARGUMENT_PACK_P (arg))
148 dump_template_argument_list (ARGUMENT_PACK_ARGS (arg), flags);
149 else if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
150 dump_type (arg, flags & ~TFF_CLASS_KEY_OR_ENUM);
153 if (TREE_CODE (arg) == TREE_LIST)
154 arg = TREE_VALUE (arg);
156 dump_expr (arg, (flags | TFF_EXPR_IN_PARENS) & ~TFF_CLASS_KEY_OR_ENUM);
160 /* Count the number of template arguments ARGS whose value does not
161 match the (optional) default template parameter in PARAMS */
164 get_non_default_template_args_count (tree args, int flags)
166 int n = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_ARGS (args));
168 if (/* We use this flag when generating debug information. We don't
169 want to expand templates at this point, for this may generate
170 new decls, which gets decl counts out of sync, which may in
171 turn cause codegen differences between compilations with and
173 (flags & TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS) != 0
174 || !flag_pretty_templates)
177 return GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (INNERMOST_TEMPLATE_ARGS (args));
180 /* Dump a template-argument-list ARGS (always a TREE_VEC) under control
184 dump_template_argument_list (tree args, int flags)
186 int n = get_non_default_template_args_count (args, flags);
190 for (i = 0; i < n; ++i)
192 tree arg = TREE_VEC_ELT (args, i);
194 /* Only print a comma if we know there is an argument coming. In
195 the case of an empty template argument pack, no actual
196 argument will be printed. */
198 && (!ARGUMENT_PACK_P (arg)
199 || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) > 0))
200 pp_separate_with_comma (cxx_pp);
202 dump_template_argument (arg, flags);
207 /* Dump a template parameter PARM (a TREE_LIST) under control of FLAGS. */
210 dump_template_parameter (tree parm, int flags)
215 if (parm == error_mark_node)
218 p = TREE_VALUE (parm);
219 a = TREE_PURPOSE (parm);
221 if (TREE_CODE (p) == TYPE_DECL)
223 if (flags & TFF_DECL_SPECIFIERS)
225 pp_cxx_ws_string (cxx_pp, "class");
226 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (p)))
227 pp_cxx_ws_string (cxx_pp, "...");
229 pp_cxx_tree_identifier (cxx_pp, DECL_NAME (p));
231 else if (DECL_NAME (p))
232 pp_cxx_tree_identifier (cxx_pp, DECL_NAME (p));
234 pp_cxx_canonical_template_parameter (cxx_pp, TREE_TYPE (p));
237 dump_decl (p, flags | TFF_DECL_SPECIFIERS);
239 if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && a != NULL_TREE)
241 pp_cxx_whitespace (cxx_pp);
243 pp_cxx_whitespace (cxx_pp);
244 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
245 dump_type (a, flags & ~TFF_CHASE_TYPEDEF);
247 dump_expr (a, flags | TFF_EXPR_IN_PARENS);
251 /* Dump, under control of FLAGS, a template-parameter-list binding.
252 PARMS is a TREE_LIST of TREE_VEC of TREE_LIST and ARGS is a
256 dump_template_bindings (tree parms, tree args, VEC(tree,gc)* typenames)
264 tree p = TREE_VALUE (parms);
265 int lvl = TMPL_PARMS_DEPTH (parms);
268 tree lvl_args = NULL_TREE;
270 /* Don't crash if we had an invalid argument list. */
271 if (TMPL_ARGS_DEPTH (args) >= lvl)
272 lvl_args = TMPL_ARGS_LEVEL (args, lvl);
274 for (i = 0; i < TREE_VEC_LENGTH (p); ++i)
276 tree arg = NULL_TREE;
278 /* Don't crash if we had an invalid argument list. */
279 if (lvl_args && NUM_TMPL_ARGS (lvl_args) > arg_idx)
280 arg = TREE_VEC_ELT (lvl_args, arg_idx);
283 pp_separate_with_comma (cxx_pp);
284 dump_template_parameter (TREE_VEC_ELT (p, i), TFF_PLAIN_IDENTIFIER);
285 pp_cxx_whitespace (cxx_pp);
287 pp_cxx_whitespace (cxx_pp);
290 if (ARGUMENT_PACK_P (arg))
291 pp_cxx_left_brace (cxx_pp);
292 dump_template_argument (arg, TFF_PLAIN_IDENTIFIER);
293 if (ARGUMENT_PACK_P (arg))
294 pp_cxx_right_brace (cxx_pp);
297 pp_string (cxx_pp, M_("<missing>"));
303 parms = TREE_CHAIN (parms);
306 for (i = 0; VEC_iterate (tree, typenames, i, t); ++i)
309 pp_separate_with_comma (cxx_pp);
310 dump_type (t, TFF_PLAIN_IDENTIFIER);
311 pp_cxx_whitespace (cxx_pp);
313 pp_cxx_whitespace (cxx_pp);
314 t = tsubst (t, args, tf_none, NULL_TREE);
315 /* Strip typedefs. We can't just use TFF_CHASE_TYPEDEF because
316 pp_simple_type_specifier doesn't know about it. */
317 t = strip_typedefs (t);
318 dump_type (t, TFF_PLAIN_IDENTIFIER);
322 /* Dump a human-readable equivalent of TYPE. FLAGS controls the
326 dump_type (tree t, int flags)
331 if (TYPE_PTRMEMFUNC_P (t))
334 switch (TREE_CODE (t))
337 if (t == init_list_type_node)
338 pp_string (cxx_pp, M_("<brace-enclosed initializer list>"));
339 else if (t == unknown_type_node)
340 pp_string (cxx_pp, M_("<unresolved overloaded function type>"));
343 pp_cxx_cv_qualifier_seq (cxx_pp, t);
344 pp_cxx_tree_identifier (cxx_pp, TYPE_IDENTIFIER (t));
349 /* A list of function parms. */
350 dump_parameters (t, flags);
353 case IDENTIFIER_NODE:
354 pp_cxx_tree_identifier (cxx_pp, t);
358 dump_type (BINFO_TYPE (t), flags);
364 dump_aggr_type (t, flags);
368 if (flags & TFF_CHASE_TYPEDEF)
370 dump_type (DECL_ORIGINAL_TYPE (t)
371 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t), flags);
374 /* Else fall through. */
378 dump_decl (t, flags & ~TFF_DECL_SPECIFIERS);
387 case FIXED_POINT_TYPE:
388 pp_type_specifier_seq (cxx_pp, t);
391 case TEMPLATE_TEMPLATE_PARM:
392 /* For parameters inside template signature. */
393 if (TYPE_IDENTIFIER (t))
394 pp_cxx_tree_identifier (cxx_pp, TYPE_IDENTIFIER (t));
396 pp_cxx_canonical_template_parameter (cxx_pp, t);
399 case BOUND_TEMPLATE_TEMPLATE_PARM:
401 tree args = TYPE_TI_ARGS (t);
402 pp_cxx_cv_qualifier_seq (cxx_pp, t);
403 pp_cxx_tree_identifier (cxx_pp, TYPE_IDENTIFIER (t));
404 pp_cxx_begin_template_argument_list (cxx_pp);
405 dump_template_argument_list (args, flags);
406 pp_cxx_end_template_argument_list (cxx_pp);
410 case TEMPLATE_TYPE_PARM:
411 pp_cxx_cv_qualifier_seq (cxx_pp, t);
412 if (TYPE_IDENTIFIER (t))
413 pp_cxx_tree_identifier (cxx_pp, TYPE_IDENTIFIER (t));
415 pp_cxx_canonical_template_parameter
416 (cxx_pp, TEMPLATE_TYPE_PARM_INDEX (t));
419 /* This is not always necessary for pointers and such, but doing this
420 reduces code size. */
429 dump_type_prefix (t, flags);
430 dump_type_suffix (t, flags);
434 if (! (flags & TFF_CHASE_TYPEDEF)
435 && DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
437 dump_decl (TYPE_NAME (t), TFF_PLAIN_IDENTIFIER);
440 pp_cxx_cv_qualifier_seq (cxx_pp, t);
441 pp_cxx_ws_string (cxx_pp,
442 TYPENAME_IS_ENUM_P (t) ? "enum"
443 : TYPENAME_IS_CLASS_P (t) ? "class"
445 dump_typename (t, flags);
448 case UNBOUND_CLASS_TEMPLATE:
449 if (! (flags & TFF_UNQUALIFIED_NAME))
451 dump_type (TYPE_CONTEXT (t), flags);
452 pp_cxx_colon_colon (cxx_pp);
454 pp_cxx_ws_string (cxx_pp, "template");
455 dump_type (DECL_NAME (TYPE_NAME (t)), flags);
459 pp_cxx_ws_string (cxx_pp, "__typeof__");
460 pp_cxx_whitespace (cxx_pp);
461 pp_cxx_left_paren (cxx_pp);
462 dump_expr (TYPEOF_TYPE_EXPR (t), flags & ~TFF_EXPR_IN_PARENS);
463 pp_cxx_right_paren (cxx_pp);
466 case TYPE_PACK_EXPANSION:
467 dump_type (PACK_EXPANSION_PATTERN (t), flags);
468 pp_cxx_ws_string (cxx_pp, "...");
471 case TYPE_ARGUMENT_PACK:
472 dump_template_argument (t, flags);
476 pp_cxx_ws_string (cxx_pp, "decltype");
477 pp_cxx_whitespace (cxx_pp);
478 pp_cxx_left_paren (cxx_pp);
479 dump_expr (DECLTYPE_TYPE_EXPR (t), flags & ~TFF_EXPR_IN_PARENS);
480 pp_cxx_right_paren (cxx_pp);
484 pp_unsupported_tree (cxx_pp, t);
485 /* Fall through to error. */
488 pp_string (cxx_pp, M_("<type error>"));
493 /* Dump a TYPENAME_TYPE. We need to notice when the context is itself
497 dump_typename (tree t, int flags)
499 tree ctx = TYPE_CONTEXT (t);
501 if (TREE_CODE (ctx) == TYPENAME_TYPE)
502 dump_typename (ctx, flags);
504 dump_type (ctx, flags & ~TFF_CLASS_KEY_OR_ENUM);
505 pp_cxx_colon_colon (cxx_pp);
506 dump_decl (TYPENAME_TYPE_FULLNAME (t), flags);
509 /* Return the name of the supplied aggregate, or enumeral type. */
512 class_key_or_enum_as_string (tree t)
514 if (TREE_CODE (t) == ENUMERAL_TYPE)
516 if (SCOPED_ENUM_P (t))
521 else if (TREE_CODE (t) == UNION_TYPE)
523 else if (TYPE_LANG_SPECIFIC (t) && CLASSTYPE_DECLARED_CLASS (t))
529 /* Print out a class declaration T under the control of FLAGS,
530 in the form `class foo'. */
533 dump_aggr_type (tree t, int flags)
536 const char *variety = class_key_or_enum_as_string (t);
540 pp_cxx_cv_qualifier_seq (cxx_pp, t);
542 if (flags & TFF_CLASS_KEY_OR_ENUM)
543 pp_cxx_ws_string (cxx_pp, variety);
545 name = TYPE_NAME (t);
549 typdef = !DECL_ARTIFICIAL (name);
552 && ((flags & TFF_CHASE_TYPEDEF)
553 || (!flag_pretty_templates && DECL_LANG_SPECIFIC (name)
554 && DECL_TEMPLATE_INFO (name))))
555 || DECL_SELF_REFERENCE_P (name))
557 t = TYPE_MAIN_VARIANT (t);
558 name = TYPE_NAME (t);
562 tmplate = !typdef && TREE_CODE (t) != ENUMERAL_TYPE
563 && TYPE_LANG_SPECIFIC (t) && CLASSTYPE_TEMPLATE_INFO (t)
564 && (TREE_CODE (CLASSTYPE_TI_TEMPLATE (t)) != TEMPLATE_DECL
565 || PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
567 if (! (flags & TFF_UNQUALIFIED_NAME))
568 dump_scope (CP_DECL_CONTEXT (name), flags | TFF_SCOPE);
569 flags &= ~TFF_UNQUALIFIED_NAME;
572 /* Because the template names are mangled, we have to locate
573 the most general template, and use that name. */
574 tree tpl = CLASSTYPE_TI_TEMPLATE (t);
576 while (DECL_TEMPLATE_INFO (tpl))
577 tpl = DECL_TI_TEMPLATE (tpl);
580 name = DECL_NAME (name);
583 if (name == 0 || ANON_AGGRNAME_P (name))
585 if (flags & TFF_CLASS_KEY_OR_ENUM)
586 pp_string (cxx_pp, M_("<anonymous>"));
588 pp_printf (pp_base (cxx_pp), M_("<anonymous %s>"), variety);
590 else if (LAMBDANAME_P (name))
592 /* A lambda's "type" is essentially its signature. */
593 pp_string (cxx_pp, M_("<lambda"));
594 if (lambda_function (t))
595 dump_parameters (FUNCTION_FIRST_USER_PARMTYPE (lambda_function (t)),
597 pp_character(cxx_pp, '>');
600 pp_cxx_tree_identifier (cxx_pp, name);
602 dump_template_parms (TYPE_TEMPLATE_INFO (t),
603 !CLASSTYPE_USE_TEMPLATE (t),
604 flags & ~TFF_TEMPLATE_HEADER);
607 /* Dump into the obstack the initial part of the output for a given type.
608 This is necessary when dealing with things like functions returning
611 return type of `int (* fee ())()': pointer -> function -> int. Both
612 pointer (and reference and offset) and function (and member) types must
613 deal with prefix and suffix.
615 Arrays must also do this for DECL nodes, like int a[], and for things like
619 dump_type_prefix (tree t, int flags)
621 if (TYPE_PTRMEMFUNC_P (t))
623 t = TYPE_PTRMEMFUNC_FN_TYPE (t);
627 switch (TREE_CODE (t))
632 tree sub = TREE_TYPE (t);
634 dump_type_prefix (sub, flags);
635 if (TREE_CODE (sub) == ARRAY_TYPE
636 || TREE_CODE (sub) == FUNCTION_TYPE)
638 pp_cxx_whitespace (cxx_pp);
639 pp_cxx_left_paren (cxx_pp);
641 if (TREE_CODE (t) == POINTER_TYPE)
642 pp_character(cxx_pp, '*');
643 else if (TREE_CODE (t) == REFERENCE_TYPE)
645 if (TYPE_REF_IS_RVALUE (t))
646 pp_string (cxx_pp, "&&");
648 pp_character (cxx_pp, '&');
650 pp_base (cxx_pp)->padding = pp_before;
651 pp_cxx_cv_qualifier_seq (cxx_pp, t);
657 dump_type_prefix (TREE_TYPE (t), flags);
658 if (TREE_CODE (t) == OFFSET_TYPE) /* pmfs deal with this in d_t_p */
660 pp_maybe_space (cxx_pp);
661 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
662 pp_cxx_left_paren (cxx_pp);
663 dump_type (TYPE_OFFSET_BASETYPE (t), flags);
664 pp_cxx_colon_colon (cxx_pp);
666 pp_cxx_star (cxx_pp);
667 pp_cxx_cv_qualifier_seq (cxx_pp, t);
668 pp_base (cxx_pp)->padding = pp_before;
671 /* This can be reached without a pointer when dealing with
672 templates, e.g. std::is_function. */
674 dump_type_prefix (TREE_TYPE (t), flags);
678 dump_type_prefix (TREE_TYPE (t), flags);
679 pp_maybe_space (cxx_pp);
680 pp_cxx_left_paren (cxx_pp);
681 dump_aggr_type (TYPE_METHOD_BASETYPE (t), flags);
682 pp_cxx_colon_colon (cxx_pp);
686 dump_type_prefix (TREE_TYPE (t), flags);
690 case IDENTIFIER_NODE:
695 case TEMPLATE_TYPE_PARM:
696 case TEMPLATE_TEMPLATE_PARM:
697 case BOUND_TEMPLATE_TEMPLATE_PARM:
709 case TYPE_PACK_EXPANSION:
710 case FIXED_POINT_TYPE:
711 dump_type (t, flags);
712 pp_base (cxx_pp)->padding = pp_before;
716 pp_unsupported_tree (cxx_pp, t);
719 pp_string (cxx_pp, M_("<typeprefixerror>"));
724 /* Dump the suffix of type T, under control of FLAGS. This is the part
725 which appears after the identifier (or function parms). */
728 dump_type_suffix (tree t, int flags)
730 if (TYPE_PTRMEMFUNC_P (t))
731 t = TYPE_PTRMEMFUNC_FN_TYPE (t);
733 switch (TREE_CODE (t))
738 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
739 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
740 pp_cxx_right_paren (cxx_pp);
741 dump_type_suffix (TREE_TYPE (t), flags);
748 if (TREE_CODE (t) == METHOD_TYPE)
749 /* Can only be reached through a pointer. */
750 pp_cxx_right_paren (cxx_pp);
751 arg = TYPE_ARG_TYPES (t);
752 if (TREE_CODE (t) == METHOD_TYPE)
753 arg = TREE_CHAIN (arg);
755 /* Function pointers don't have default args. Not in standard C++,
756 anyway; they may in g++, but we'll just pretend otherwise. */
757 dump_parameters (arg, flags & ~TFF_FUNCTION_DEFAULT_ARGUMENTS);
759 if (TREE_CODE (t) == METHOD_TYPE)
760 pp_cxx_cv_qualifier_seq
761 (cxx_pp, TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))));
763 pp_cxx_cv_qualifier_seq (cxx_pp, t);
764 dump_exception_spec (TYPE_RAISES_EXCEPTIONS (t), flags);
765 dump_type_suffix (TREE_TYPE (t), flags);
770 pp_maybe_space (cxx_pp);
771 pp_cxx_left_bracket (cxx_pp);
774 tree dtype = TYPE_DOMAIN (t);
775 tree max = TYPE_MAX_VALUE (dtype);
776 if (host_integerp (max, 0))
777 pp_wide_integer (cxx_pp, tree_low_cst (max, 0) + 1);
778 else if (TREE_CODE (max) == MINUS_EXPR)
779 dump_expr (TREE_OPERAND (max, 0),
780 flags & ~TFF_EXPR_IN_PARENS);
782 dump_expr (fold_build2_loc (input_location,
783 PLUS_EXPR, dtype, max,
784 build_int_cst (dtype, 1)),
785 flags & ~TFF_EXPR_IN_PARENS);
787 pp_cxx_right_bracket (cxx_pp);
788 dump_type_suffix (TREE_TYPE (t), flags);
792 case IDENTIFIER_NODE:
797 case TEMPLATE_TYPE_PARM:
798 case TEMPLATE_TEMPLATE_PARM:
799 case BOUND_TEMPLATE_TEMPLATE_PARM:
811 case TYPE_PACK_EXPANSION:
812 case FIXED_POINT_TYPE:
816 pp_unsupported_tree (cxx_pp, t);
818 /* Don't mark it here, we should have already done in
825 dump_global_iord (tree t)
827 const char *p = NULL;
829 if (DECL_GLOBAL_CTOR_P (t))
830 p = M_("(static initializers for %s)");
831 else if (DECL_GLOBAL_DTOR_P (t))
832 p = M_("(static destructors for %s)");
836 pp_printf (pp_base (cxx_pp), p, input_filename);
840 dump_simple_decl (tree t, tree type, int flags)
842 if (flags & TFF_DECL_SPECIFIERS)
844 dump_type_prefix (type, flags & ~TFF_UNQUALIFIED_NAME);
845 pp_maybe_space (cxx_pp);
847 if (! (flags & TFF_UNQUALIFIED_NAME)
848 && (!DECL_INITIAL (t)
849 || TREE_CODE (DECL_INITIAL (t)) != TEMPLATE_PARM_INDEX))
850 dump_scope (CP_DECL_CONTEXT (t), flags);
851 flags &= ~TFF_UNQUALIFIED_NAME;
852 if ((flags & TFF_DECL_SPECIFIERS)
853 && DECL_TEMPLATE_PARM_P (t)
854 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (t)))
855 pp_string (cxx_pp, "...");
857 dump_decl (DECL_NAME (t), flags);
859 pp_string (cxx_pp, M_("<anonymous>"));
860 if (flags & TFF_DECL_SPECIFIERS)
861 dump_type_suffix (type, flags);
864 /* Dump a human readable string for the decl T under control of FLAGS. */
867 dump_decl (tree t, int flags)
872 switch (TREE_CODE (t))
875 /* Don't say 'typedef class A' */
876 if (DECL_ARTIFICIAL (t) && !DECL_SELF_REFERENCE_P (t))
878 if ((flags & TFF_DECL_SPECIFIERS)
879 && TREE_CODE (TREE_TYPE (t)) == TEMPLATE_TYPE_PARM)
881 /* Say `class T' not just `T'. */
882 pp_cxx_ws_string (cxx_pp, "class");
884 /* Emit the `...' for a parameter pack. */
885 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
886 pp_cxx_ws_string (cxx_pp, "...");
889 dump_type (TREE_TYPE (t), flags);
892 if ((flags & TFF_DECL_SPECIFIERS)
893 && !DECL_SELF_REFERENCE_P (t))
894 pp_cxx_ws_string (cxx_pp, "typedef");
895 dump_simple_decl (t, DECL_ORIGINAL_TYPE (t)
896 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t),
901 if (DECL_NAME (t) && VTABLE_NAME_P (DECL_NAME (t)))
903 pp_string (cxx_pp, M_("vtable for "));
904 gcc_assert (TYPE_P (DECL_CONTEXT (t)));
905 dump_type (DECL_CONTEXT (t), flags);
908 /* Else fall through. */
911 dump_simple_decl (t, TREE_TYPE (t), flags);
915 pp_string (cxx_pp, M_("<return value> "));
916 dump_simple_decl (t, TREE_TYPE (t), flags);
920 if (flags & TFF_DECL_SPECIFIERS)
921 pp_cxx_declaration (cxx_pp, t);
924 if (! (flags & TFF_UNQUALIFIED_NAME))
925 dump_scope (CP_DECL_CONTEXT (t), flags);
926 flags &= ~TFF_UNQUALIFIED_NAME;
927 if (DECL_NAME (t) == NULL_TREE)
928 pp_string (cxx_pp, M_("<unnamed>"));
930 pp_cxx_tree_identifier (cxx_pp, DECL_NAME (t));
935 dump_type (TREE_OPERAND (t, 0), flags);
936 pp_string (cxx_pp, "::");
937 dump_decl (TREE_OPERAND (t, 1), flags|TFF_UNQUALIFIED_NAME);
941 dump_decl (TREE_OPERAND (t, 0), flags);
942 pp_cxx_left_bracket (cxx_pp);
943 dump_decl (TREE_OPERAND (t, 1), flags);
944 pp_cxx_right_bracket (cxx_pp);
947 /* So that we can do dump_decl on an aggr type. */
951 dump_type (t, flags);
955 /* This is a pseudo destructor call which has not been folded into
956 a PSEUDO_DTOR_EXPR yet. */
957 pp_cxx_complement (cxx_pp);
958 dump_type (TREE_OPERAND (t, 0), flags);
965 /* These special cases are duplicated here so that other functions
966 can feed identifiers to error and get them demangled properly. */
967 case IDENTIFIER_NODE:
968 if (IDENTIFIER_TYPENAME_P (t))
970 pp_cxx_ws_string (cxx_pp, "operator");
971 /* Not exactly IDENTIFIER_TYPE_VALUE. */
972 dump_type (TREE_TYPE (t), flags);
976 pp_cxx_tree_identifier (cxx_pp, t);
983 if (DECL_CLASS_SCOPE_P (t))
985 dump_type (DECL_CONTEXT (t), flags);
986 pp_cxx_colon_colon (cxx_pp);
988 else if (DECL_CONTEXT (t))
990 dump_decl (DECL_CONTEXT (t), flags);
991 pp_cxx_colon_colon (cxx_pp);
993 dump_decl (DECL_NAME (t), flags);
997 /* If there's only one function, just treat it like an ordinary
1003 if (! DECL_LANG_SPECIFIC (t))
1004 pp_string (cxx_pp, M_("<built-in>"));
1005 else if (DECL_GLOBAL_CTOR_P (t) || DECL_GLOBAL_DTOR_P (t))
1006 dump_global_iord (t);
1008 dump_function_decl (t, flags);
1012 dump_template_decl (t, flags);
1015 case TEMPLATE_ID_EXPR:
1017 tree name = TREE_OPERAND (t, 0);
1019 if (is_overloaded_fn (name))
1020 name = DECL_NAME (get_first_fn (name));
1021 dump_decl (name, flags);
1022 pp_cxx_begin_template_argument_list (cxx_pp);
1023 if (TREE_OPERAND (t, 1))
1024 dump_template_argument_list (TREE_OPERAND (t, 1), flags);
1025 pp_cxx_end_template_argument_list (cxx_pp);
1030 pp_cxx_tree_identifier (cxx_pp, DECL_NAME (t));
1034 if ((TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == ENUMERAL_TYPE)
1035 || (DECL_INITIAL (t) &&
1036 TREE_CODE (DECL_INITIAL (t)) == TEMPLATE_PARM_INDEX))
1037 dump_simple_decl (t, TREE_TYPE (t), flags);
1038 else if (DECL_NAME (t))
1039 dump_decl (DECL_NAME (t), flags);
1040 else if (DECL_INITIAL (t))
1041 dump_expr (DECL_INITIAL (t), flags | TFF_EXPR_IN_PARENS);
1043 pp_string (cxx_pp, M_("<enumerator>"));
1047 pp_cxx_ws_string (cxx_pp, "using");
1048 dump_type (USING_DECL_SCOPE (t), flags);
1049 pp_cxx_colon_colon (cxx_pp);
1050 dump_decl (DECL_NAME (t), flags);
1054 pp_cxx_declaration (cxx_pp, t);
1058 dump_decl (BASELINK_FUNCTIONS (t), flags);
1061 case NON_DEPENDENT_EXPR:
1062 dump_expr (t, flags);
1065 case TEMPLATE_TYPE_PARM:
1066 if (flags & TFF_DECL_SPECIFIERS)
1067 pp_cxx_declaration (cxx_pp, t);
1069 pp_type_id (cxx_pp, t);
1072 case UNBOUND_CLASS_TEMPLATE:
1073 case TYPE_PACK_EXPANSION:
1075 dump_type (t, flags);
1079 pp_unsupported_tree (cxx_pp, t);
1080 /* Fall through to error. */
1083 pp_string (cxx_pp, M_("<declaration error>"));
1088 /* Dump a template declaration T under control of FLAGS. This means the
1089 'template <...> leaders plus the 'class X' or 'void fn(...)' part. */
1092 dump_template_decl (tree t, int flags)
1094 tree orig_parms = DECL_TEMPLATE_PARMS (t);
1098 if (flags & TFF_TEMPLATE_HEADER)
1100 for (parms = orig_parms = nreverse (orig_parms);
1102 parms = TREE_CHAIN (parms))
1104 tree inner_parms = INNERMOST_TEMPLATE_PARMS (parms);
1105 int len = TREE_VEC_LENGTH (inner_parms);
1107 pp_cxx_ws_string (cxx_pp, "template");
1108 pp_cxx_begin_template_argument_list (cxx_pp);
1110 /* If we've shown the template prefix, we'd better show the
1111 parameters' and decl's type too. */
1112 flags |= TFF_DECL_SPECIFIERS;
1114 for (i = 0; i < len; i++)
1117 pp_separate_with_comma (cxx_pp);
1118 dump_template_parameter (TREE_VEC_ELT (inner_parms, i), flags);
1120 pp_cxx_end_template_argument_list (cxx_pp);
1121 pp_cxx_whitespace (cxx_pp);
1123 nreverse(orig_parms);
1125 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
1127 /* Say `template<arg> class TT' not just `template<arg> TT'. */
1128 pp_cxx_ws_string (cxx_pp, "class");
1130 /* If this is a parameter pack, print the ellipsis. */
1131 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
1132 pp_cxx_ws_string (cxx_pp, "...");
1136 if (DECL_TEMPLATE_RESULT (t)
1137 && TREE_CODE (DECL_TEMPLATE_RESULT (t)) == TYPE_DECL)
1138 dump_type (TREE_TYPE (t),
1139 ((flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1140 | (flags & TFF_DECL_SPECIFIERS ? TFF_CLASS_KEY_OR_ENUM : 0)));
1141 else if (DECL_TEMPLATE_RESULT (t)
1142 && TREE_CODE (DECL_TEMPLATE_RESULT (t)) == VAR_DECL)
1143 dump_decl (DECL_TEMPLATE_RESULT (t), flags | TFF_TEMPLATE_NAME);
1146 gcc_assert (TREE_TYPE (t));
1147 switch (NEXT_CODE (t))
1151 dump_function_decl (t, flags | TFF_TEMPLATE_NAME);
1154 /* This case can occur with some invalid code. */
1155 dump_type (TREE_TYPE (t),
1156 (flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1157 | (flags & TFF_DECL_SPECIFIERS
1158 ? TFF_CLASS_KEY_OR_ENUM : 0));
1163 /* find_typenames looks through the type of the function template T
1164 and returns a VEC containing any typedefs, decltypes or TYPENAME_TYPEs
1167 struct find_typenames_t
1169 struct pointer_set_t *p_set;
1170 VEC (tree,gc) *typenames;
1174 find_typenames_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED, void *data)
1176 struct find_typenames_t *d = (struct find_typenames_t *)data;
1177 tree mv = NULL_TREE;
1179 if (TYPE_P (*tp) && is_typedef_decl (TYPE_NAME (*tp)))
1180 /* Add the type of the typedef without any additional cv-quals. */
1181 mv = TREE_TYPE (TYPE_NAME (*tp));
1182 else if (TREE_CODE (*tp) == TYPENAME_TYPE
1183 || TREE_CODE (*tp) == DECLTYPE_TYPE)
1184 /* Add the typename without any cv-qualifiers. */
1185 mv = TYPE_MAIN_VARIANT (*tp);
1187 if (mv && (mv == *tp || !pointer_set_insert (d->p_set, mv)))
1188 VEC_safe_push (tree, gc, d->typenames, mv);
1190 /* Search into class template arguments, which cp_walk_subtrees
1192 if (CLASS_TYPE_P (*tp) && CLASSTYPE_TEMPLATE_INFO (*tp))
1193 cp_walk_tree (&CLASSTYPE_TI_ARGS (*tp), find_typenames_r,
1199 static VEC(tree,gc) *
1200 find_typenames (tree t)
1202 struct find_typenames_t ft;
1203 ft.p_set = pointer_set_create ();
1204 ft.typenames = NULL;
1205 cp_walk_tree (&TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
1206 find_typenames_r, &ft, ft.p_set);
1207 pointer_set_destroy (ft.p_set);
1208 return ft.typenames;
1211 /* Pretty print a function decl. There are several ways we want to print a
1212 function declaration. The TFF_ bits in FLAGS tells us how to behave.
1213 As error can only apply the '#' flag once to give 0 and 1 for V, there
1214 is %D which doesn't print the throw specs, and %F which does. */
1217 dump_function_decl (tree t, int flags)
1221 tree cname = NULL_TREE;
1222 tree template_args = NULL_TREE;
1223 tree template_parms = NULL_TREE;
1224 int show_return = flags & TFF_RETURN_TYPE || flags & TFF_DECL_SPECIFIERS;
1225 int do_outer_scope = ! (flags & TFF_UNQUALIFIED_NAME);
1227 VEC(tree,gc) *typenames = NULL;
1229 if (LAMBDA_FUNCTION_P (t))
1231 /* A lambda's signature is essentially its "type", so defer. */
1232 gcc_assert (LAMBDA_TYPE_P (DECL_CONTEXT (t)));
1233 dump_type (DECL_CONTEXT (t), flags);
1237 flags &= ~TFF_UNQUALIFIED_NAME;
1238 if (TREE_CODE (t) == TEMPLATE_DECL)
1239 t = DECL_TEMPLATE_RESULT (t);
1241 /* Save the exceptions, in case t is a specialization and we are
1242 emitting an error about incompatible specifications. */
1243 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (t));
1245 /* Pretty print template instantiations only. */
1246 if (DECL_USE_TEMPLATE (t) && DECL_TEMPLATE_INFO (t)
1247 && flag_pretty_templates)
1251 template_args = DECL_TI_ARGS (t);
1252 tmpl = most_general_template (t);
1253 if (tmpl && TREE_CODE (tmpl) == TEMPLATE_DECL)
1255 template_parms = DECL_TEMPLATE_PARMS (tmpl);
1257 typenames = find_typenames (t);
1261 fntype = TREE_TYPE (t);
1262 parmtypes = FUNCTION_FIRST_USER_PARMTYPE (t);
1264 if (DECL_CLASS_SCOPE_P (t))
1265 cname = DECL_CONTEXT (t);
1266 /* This is for partially instantiated template methods. */
1267 else if (TREE_CODE (fntype) == METHOD_TYPE)
1268 cname = TREE_TYPE (TREE_VALUE (parmtypes));
1270 if (!(flags & TFF_DECL_SPECIFIERS))
1272 else if (DECL_STATIC_FUNCTION_P (t))
1273 pp_cxx_ws_string (cxx_pp, "static");
1274 else if (DECL_VIRTUAL_P (t))
1275 pp_cxx_ws_string (cxx_pp, "virtual");
1277 /* Print the return type? */
1279 show_return = !DECL_CONV_FN_P (t) && !DECL_CONSTRUCTOR_P (t)
1280 && !DECL_DESTRUCTOR_P (t);
1282 dump_type_prefix (TREE_TYPE (fntype), flags);
1284 /* Print the function name. */
1285 if (!do_outer_scope)
1289 dump_type (cname, flags);
1290 pp_cxx_colon_colon (cxx_pp);
1293 dump_scope (CP_DECL_CONTEXT (t), flags);
1295 dump_function_name (t, flags);
1297 if (!(flags & TFF_NO_FUNCTION_ARGUMENTS))
1299 dump_parameters (parmtypes, flags);
1301 if (TREE_CODE (fntype) == METHOD_TYPE)
1303 pp_base (cxx_pp)->padding = pp_before;
1304 pp_cxx_cv_qualifier_seq
1305 (cxx_pp, TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fntype))));
1308 if (flags & TFF_EXCEPTION_SPECIFICATION)
1310 pp_base (cxx_pp)->padding = pp_before;
1311 dump_exception_spec (exceptions, flags);
1315 dump_type_suffix (TREE_TYPE (fntype), flags);
1318 /* If T is a template instantiation, dump the parameter binding. */
1319 if (template_parms != NULL_TREE && template_args != NULL_TREE)
1321 pp_cxx_whitespace (cxx_pp);
1322 pp_cxx_left_bracket (cxx_pp);
1323 pp_cxx_ws_string (cxx_pp, M_("with"));
1324 pp_cxx_whitespace (cxx_pp);
1325 dump_template_bindings (template_parms, template_args, typenames);
1326 pp_cxx_right_bracket (cxx_pp);
1330 /* Print a parameter list. If this is for a member function, the
1331 member object ptr (and any other hidden args) should have
1332 already been removed. */
1335 dump_parameters (tree parmtypes, int flags)
1338 pp_cxx_left_paren (cxx_pp);
1340 for (first = 1; parmtypes != void_list_node;
1341 parmtypes = TREE_CHAIN (parmtypes))
1344 pp_separate_with_comma (cxx_pp);
1348 pp_cxx_ws_string (cxx_pp, "...");
1352 dump_type (TREE_VALUE (parmtypes), flags);
1354 if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && TREE_PURPOSE (parmtypes))
1356 pp_cxx_whitespace (cxx_pp);
1358 pp_cxx_whitespace (cxx_pp);
1359 dump_expr (TREE_PURPOSE (parmtypes), flags | TFF_EXPR_IN_PARENS);
1363 pp_cxx_right_paren (cxx_pp);
1366 /* Print an exception specification. T is the exception specification. */
1369 dump_exception_spec (tree t, int flags)
1373 pp_cxx_ws_string (cxx_pp, "throw");
1374 pp_cxx_whitespace (cxx_pp);
1375 pp_cxx_left_paren (cxx_pp);
1376 if (TREE_VALUE (t) != NULL_TREE)
1379 dump_type (TREE_VALUE (t), flags);
1383 pp_separate_with_comma (cxx_pp);
1385 pp_cxx_right_paren (cxx_pp);
1389 /* Handle the function name for a FUNCTION_DECL node, grokking operators
1390 and destructors properly. */
1393 dump_function_name (tree t, int flags)
1395 tree name = DECL_NAME (t);
1397 /* We can get here with a decl that was synthesized by language-
1398 independent machinery (e.g. coverage.c) in which case it won't
1399 have a lang_specific structure attached and DECL_CONSTRUCTOR_P
1400 will crash. In this case it is safe just to print out the
1402 if (!DECL_LANG_SPECIFIC (t))
1404 pp_cxx_tree_identifier (cxx_pp, name);
1408 if (TREE_CODE (t) == TEMPLATE_DECL)
1409 t = DECL_TEMPLATE_RESULT (t);
1411 /* Don't let the user see __comp_ctor et al. */
1412 if (DECL_CONSTRUCTOR_P (t)
1413 || DECL_DESTRUCTOR_P (t))
1415 if (LAMBDA_TYPE_P (DECL_CONTEXT (t)))
1416 name = get_identifier ("<lambda>");
1418 name = constructor_name (DECL_CONTEXT (t));
1421 if (DECL_DESTRUCTOR_P (t))
1423 pp_cxx_complement (cxx_pp);
1424 dump_decl (name, TFF_PLAIN_IDENTIFIER);
1426 else if (DECL_CONV_FN_P (t))
1428 /* This cannot use the hack that the operator's return
1429 type is stashed off of its name because it may be
1430 used for error reporting. In the case of conflicting
1431 declarations, both will have the same name, yet
1432 the types will be different, hence the TREE_TYPE field
1433 of the first name will be clobbered by the second. */
1434 pp_cxx_ws_string (cxx_pp, "operator");
1435 dump_type (TREE_TYPE (TREE_TYPE (t)), flags);
1437 else if (name && IDENTIFIER_OPNAME_P (name))
1438 pp_cxx_tree_identifier (cxx_pp, name);
1440 dump_decl (name, flags);
1442 if (DECL_TEMPLATE_INFO (t)
1443 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (t)
1444 && (TREE_CODE (DECL_TI_TEMPLATE (t)) != TEMPLATE_DECL
1445 || PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
1446 dump_template_parms (DECL_TEMPLATE_INFO (t), !DECL_USE_TEMPLATE (t), flags);
1449 /* Dump the template parameters from the template info INFO under control of
1450 FLAGS. PRIMARY indicates whether this is a primary template decl, or
1451 specialization (partial or complete). For partial specializations we show
1452 the specialized parameter values. For a primary template we show no
1456 dump_template_parms (tree info, int primary, int flags)
1458 tree args = info ? TI_ARGS (info) : NULL_TREE;
1460 if (primary && flags & TFF_TEMPLATE_NAME)
1462 flags &= ~(TFF_CLASS_KEY_OR_ENUM | TFF_TEMPLATE_NAME);
1463 pp_cxx_begin_template_argument_list (cxx_pp);
1465 /* Be careful only to print things when we have them, so as not
1466 to crash producing error messages. */
1467 if (args && !primary)
1470 len = get_non_default_template_args_count (args, flags);
1472 args = INNERMOST_TEMPLATE_ARGS (args);
1473 for (ix = 0; ix != len; ix++)
1475 tree arg = TREE_VEC_ELT (args, ix);
1477 /* Only print a comma if we know there is an argument coming. In
1478 the case of an empty template argument pack, no actual
1479 argument will be printed. */
1481 && (!ARGUMENT_PACK_P (arg)
1482 || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) > 0))
1483 pp_separate_with_comma (cxx_pp);
1486 pp_string (cxx_pp, M_("<template parameter error>"));
1488 dump_template_argument (arg, flags);
1493 tree tpl = TI_TEMPLATE (info);
1494 tree parms = DECL_TEMPLATE_PARMS (tpl);
1497 parms = TREE_CODE (parms) == TREE_LIST ? TREE_VALUE (parms) : NULL_TREE;
1498 len = parms ? TREE_VEC_LENGTH (parms) : 0;
1500 for (ix = 0; ix != len; ix++)
1504 if (TREE_VEC_ELT (parms, ix) == error_mark_node)
1506 pp_string (cxx_pp, M_("<template parameter error>"));
1510 parm = TREE_VALUE (TREE_VEC_ELT (parms, ix));
1513 pp_separate_with_comma (cxx_pp);
1515 dump_decl (parm, flags & ~TFF_DECL_SPECIFIERS);
1518 pp_cxx_end_template_argument_list (cxx_pp);
1521 /* Print out the arguments of CALL_EXPR T as a parenthesized list using
1522 flags FLAGS. Skip over the first argument if SKIPFIRST is true. */
1525 dump_call_expr_args (tree t, int flags, bool skipfirst)
1528 call_expr_arg_iterator iter;
1530 pp_cxx_left_paren (cxx_pp);
1531 FOR_EACH_CALL_EXPR_ARG (arg, iter, t)
1537 dump_expr (arg, flags | TFF_EXPR_IN_PARENS);
1538 if (more_call_expr_args_p (&iter))
1539 pp_separate_with_comma (cxx_pp);
1542 pp_cxx_right_paren (cxx_pp);
1545 /* Print out the arguments of AGGR_INIT_EXPR T as a parenthesized list
1546 using flags FLAGS. Skip over the first argument if SKIPFIRST is
1550 dump_aggr_init_expr_args (tree t, int flags, bool skipfirst)
1553 aggr_init_expr_arg_iterator iter;
1555 pp_cxx_left_paren (cxx_pp);
1556 FOR_EACH_AGGR_INIT_EXPR_ARG (arg, iter, t)
1562 dump_expr (arg, flags | TFF_EXPR_IN_PARENS);
1563 if (more_aggr_init_expr_args_p (&iter))
1564 pp_separate_with_comma (cxx_pp);
1567 pp_cxx_right_paren (cxx_pp);
1570 /* Print out a list of initializers (subr of dump_expr). */
1573 dump_expr_list (tree l, int flags)
1577 dump_expr (TREE_VALUE (l), flags | TFF_EXPR_IN_PARENS);
1580 pp_separate_with_comma (cxx_pp);
1584 /* Print out a vector of initializers (subr of dump_expr). */
1587 dump_expr_init_vec (VEC(constructor_elt,gc) *v, int flags)
1589 unsigned HOST_WIDE_INT idx;
1592 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
1594 dump_expr (value, flags | TFF_EXPR_IN_PARENS);
1595 if (idx != VEC_length (constructor_elt, v) - 1)
1596 pp_separate_with_comma (cxx_pp);
1601 /* We've gotten an indirect REFERENCE (an OBJ_TYPE_REF) to a virtual
1602 function. Resolve it to a close relative -- in the sense of static
1603 type -- variant being overridden. That is close to what was written in
1604 the source code. Subroutine of dump_expr. */
1607 resolve_virtual_fun_from_obj_type_ref (tree ref)
1609 tree obj_type = TREE_TYPE (OBJ_TYPE_REF_OBJECT (ref));
1610 HOST_WIDE_INT index = tree_low_cst (OBJ_TYPE_REF_TOKEN (ref), 1);
1611 tree fun = BINFO_VIRTUALS (TYPE_BINFO (TREE_TYPE (obj_type)));
1614 fun = TREE_CHAIN (fun);
1615 index -= (TARGET_VTABLE_USES_DESCRIPTORS
1616 ? TARGET_VTABLE_USES_DESCRIPTORS : 1);
1622 /* Print out an expression E under control of FLAGS. */
1625 dump_expr (tree t, int flags)
1630 if (STATEMENT_CLASS_P (t))
1632 pp_cxx_ws_string (cxx_pp, M_("<statement>"));
1636 switch (TREE_CODE (t))
1644 case NAMESPACE_DECL:
1647 case IDENTIFIER_NODE:
1648 dump_decl (t, (flags & ~TFF_DECL_SPECIFIERS) | TFF_NO_FUNCTION_ARGUMENTS);
1655 pp_constant (cxx_pp, t);
1659 /* While waiting for caret diagnostics, avoid printing
1660 __cxa_allocate_exception, __cxa_throw, and the like. */
1661 pp_cxx_ws_string (cxx_pp, M_("<throw-expression>"));
1665 pp_ampersand (cxx_pp);
1666 dump_type (PTRMEM_CST_CLASS (t), flags);
1667 pp_cxx_colon_colon (cxx_pp);
1668 pp_cxx_tree_identifier (cxx_pp, DECL_NAME (PTRMEM_CST_MEMBER (t)));
1672 pp_cxx_left_paren (cxx_pp);
1673 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1674 pp_separate_with_comma (cxx_pp);
1675 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
1676 pp_cxx_right_paren (cxx_pp);
1680 pp_cxx_left_paren (cxx_pp);
1681 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1682 pp_string (cxx_pp, " ? ");
1683 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
1684 pp_string (cxx_pp, " : ");
1685 dump_expr (TREE_OPERAND (t, 2), flags | TFF_EXPR_IN_PARENS);
1686 pp_cxx_right_paren (cxx_pp);
1690 if (TREE_HAS_CONSTRUCTOR (t))
1692 pp_cxx_ws_string (cxx_pp, "new");
1693 pp_cxx_whitespace (cxx_pp);
1694 dump_type (TREE_TYPE (TREE_TYPE (t)), flags);
1697 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1700 case AGGR_INIT_EXPR:
1702 tree fn = NULL_TREE;
1704 if (TREE_CODE (AGGR_INIT_EXPR_FN (t)) == ADDR_EXPR)
1705 fn = TREE_OPERAND (AGGR_INIT_EXPR_FN (t), 0);
1707 if (fn && TREE_CODE (fn) == FUNCTION_DECL)
1709 if (DECL_CONSTRUCTOR_P (fn))
1710 dump_type (DECL_CONTEXT (fn), flags);
1715 dump_expr (AGGR_INIT_EXPR_FN (t), 0);
1717 dump_aggr_init_expr_args (t, flags, true);
1722 tree fn = CALL_EXPR_FN (t);
1723 bool skipfirst = false;
1725 if (TREE_CODE (fn) == ADDR_EXPR)
1726 fn = TREE_OPERAND (fn, 0);
1728 /* Nobody is interested in seeing the guts of vcalls. */
1729 if (TREE_CODE (fn) == OBJ_TYPE_REF)
1730 fn = resolve_virtual_fun_from_obj_type_ref (fn);
1732 if (TREE_TYPE (fn) != NULL_TREE && NEXT_CODE (fn) == METHOD_TYPE)
1734 tree ob = CALL_EXPR_ARG (t, 0);
1735 if (TREE_CODE (ob) == ADDR_EXPR)
1737 dump_expr (TREE_OPERAND (ob, 0), flags | TFF_EXPR_IN_PARENS);
1738 pp_cxx_dot (cxx_pp);
1740 else if (TREE_CODE (ob) != PARM_DECL
1741 || strcmp (IDENTIFIER_POINTER (DECL_NAME (ob)), "this"))
1743 dump_expr (ob, flags | TFF_EXPR_IN_PARENS);
1744 pp_cxx_arrow (cxx_pp);
1748 dump_expr (fn, flags | TFF_EXPR_IN_PARENS);
1749 dump_call_expr_args (t, flags, skipfirst);
1754 /* Note that this only works for G++ target exprs. If somebody
1755 builds a general TARGET_EXPR, there's no way to represent that
1756 it initializes anything other that the parameter slot for the
1757 default argument. Note we may have cleared out the first
1758 operand in expand_expr, so don't go killing ourselves. */
1759 if (TREE_OPERAND (t, 1))
1760 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
1763 case POINTER_PLUS_EXPR:
1764 dump_binary_op ("+", t, flags);
1772 case TRUNC_DIV_EXPR:
1773 case TRUNC_MOD_EXPR:
1781 case TRUTH_ANDIF_EXPR:
1782 case TRUTH_ORIF_EXPR:
1789 case EXACT_DIV_EXPR:
1790 dump_binary_op (operator_name_info[(int) TREE_CODE (t)].name, t, flags);
1794 case FLOOR_DIV_EXPR:
1795 case ROUND_DIV_EXPR:
1797 dump_binary_op ("/", t, flags);
1801 case FLOOR_MOD_EXPR:
1802 case ROUND_MOD_EXPR:
1803 dump_binary_op ("%", t, flags);
1808 tree ob = TREE_OPERAND (t, 0);
1809 if (TREE_CODE (ob) == INDIRECT_REF)
1811 ob = TREE_OPERAND (ob, 0);
1812 if (TREE_CODE (ob) != PARM_DECL
1814 && strcmp (IDENTIFIER_POINTER (DECL_NAME (ob)), "this")))
1816 dump_expr (ob, flags | TFF_EXPR_IN_PARENS);
1817 pp_cxx_arrow (cxx_pp);
1822 dump_expr (ob, flags | TFF_EXPR_IN_PARENS);
1823 pp_cxx_dot (cxx_pp);
1825 dump_expr (TREE_OPERAND (t, 1), flags & ~TFF_EXPR_IN_PARENS);
1830 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1831 pp_cxx_left_bracket (cxx_pp);
1832 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
1833 pp_cxx_right_bracket (cxx_pp);
1836 case UNARY_PLUS_EXPR:
1837 dump_unary_op ("+", t, flags);
1841 if (TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
1842 || TREE_CODE (TREE_OPERAND (t, 0)) == STRING_CST
1843 /* An ADDR_EXPR can have reference type. In that case, we
1844 shouldn't print the `&' doing so indicates to the user
1845 that the expression has pointer type. */
1847 && TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE))
1848 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1849 else if (TREE_CODE (TREE_OPERAND (t, 0)) == LABEL_DECL)
1850 dump_unary_op ("&&", t, flags);
1852 dump_unary_op ("&", t, flags);
1856 if (TREE_HAS_CONSTRUCTOR (t))
1858 t = TREE_OPERAND (t, 0);
1859 gcc_assert (TREE_CODE (t) == CALL_EXPR);
1860 dump_expr (CALL_EXPR_FN (t), flags | TFF_EXPR_IN_PARENS);
1861 dump_call_expr_args (t, flags, true);
1865 if (TREE_OPERAND (t,0) != NULL_TREE
1866 && TREE_TYPE (TREE_OPERAND (t, 0))
1867 && NEXT_CODE (TREE_OPERAND (t, 0)) == REFERENCE_TYPE)
1868 dump_expr (TREE_OPERAND (t, 0), flags);
1870 dump_unary_op ("*", t, flags);
1876 case TRUTH_NOT_EXPR:
1877 case PREDECREMENT_EXPR:
1878 case PREINCREMENT_EXPR:
1879 dump_unary_op (operator_name_info [(int)TREE_CODE (t)].name, t, flags);
1882 case POSTDECREMENT_EXPR:
1883 case POSTINCREMENT_EXPR:
1884 pp_cxx_left_paren (cxx_pp);
1885 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1886 pp_cxx_ws_string (cxx_pp, operator_name_info[(int)TREE_CODE (t)].name);
1887 pp_cxx_right_paren (cxx_pp);
1890 case NON_LVALUE_EXPR:
1891 /* FIXME: This is a KLUDGE workaround for a parsing problem. There
1892 should be another level of INDIRECT_REF so that I don't have to do
1894 if (TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == POINTER_TYPE)
1896 tree next = TREE_TYPE (TREE_TYPE (t));
1898 while (TREE_CODE (next) == POINTER_TYPE)
1899 next = TREE_TYPE (next);
1901 if (TREE_CODE (next) == FUNCTION_TYPE)
1903 if (flags & TFF_EXPR_IN_PARENS)
1904 pp_cxx_left_paren (cxx_pp);
1905 pp_cxx_star (cxx_pp);
1906 dump_expr (TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
1907 if (flags & TFF_EXPR_IN_PARENS)
1908 pp_cxx_right_paren (cxx_pp);
1911 /* Else fall through. */
1913 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1917 case VIEW_CONVERT_EXPR:
1919 tree op = TREE_OPERAND (t, 0);
1921 if (!same_type_p (TREE_TYPE (op), TREE_TYPE (t)))
1923 /* It is a cast, but we cannot tell whether it is a
1924 reinterpret or static cast. Use the C style notation. */
1925 if (flags & TFF_EXPR_IN_PARENS)
1926 pp_cxx_left_paren (cxx_pp);
1927 pp_cxx_left_paren (cxx_pp);
1928 dump_type (TREE_TYPE (t), flags);
1929 pp_cxx_right_paren (cxx_pp);
1930 dump_expr (op, flags | TFF_EXPR_IN_PARENS);
1931 if (flags & TFF_EXPR_IN_PARENS)
1932 pp_cxx_right_paren (cxx_pp);
1935 dump_expr (op, flags);
1940 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
1942 tree idx = build_ptrmemfunc_access_expr (t, pfn_identifier);
1944 if (integer_zerop (idx))
1946 /* A NULL pointer-to-member constant. */
1947 pp_cxx_left_paren (cxx_pp);
1948 pp_cxx_left_paren (cxx_pp);
1949 dump_type (TREE_TYPE (t), flags);
1950 pp_cxx_right_paren (cxx_pp);
1951 pp_character (cxx_pp, '0');
1952 pp_cxx_right_paren (cxx_pp);
1955 else if (host_integerp (idx, 0))
1958 unsigned HOST_WIDE_INT n;
1960 t = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
1961 t = TYPE_METHOD_BASETYPE (t);
1962 virtuals = BINFO_VIRTUALS (TYPE_BINFO (TYPE_MAIN_VARIANT (t)));
1964 n = tree_low_cst (idx, 0);
1966 /* Map vtable index back one, to allow for the null pointer to
1970 while (n > 0 && virtuals)
1973 virtuals = TREE_CHAIN (virtuals);
1977 dump_expr (BV_FN (virtuals),
1978 flags | TFF_EXPR_IN_PARENS);
1983 if (TREE_TYPE (t) && EMPTY_CONSTRUCTOR_P (t))
1985 dump_type (TREE_TYPE (t), 0);
1986 pp_cxx_left_paren (cxx_pp);
1987 pp_cxx_right_paren (cxx_pp);
1991 pp_cxx_left_brace (cxx_pp);
1992 dump_expr_init_vec (CONSTRUCTOR_ELTS (t), flags);
1993 pp_cxx_right_brace (cxx_pp);
2000 tree ob = TREE_OPERAND (t, 0);
2001 if (is_dummy_object (ob))
2003 t = TREE_OPERAND (t, 1);
2004 if (TREE_CODE (t) == FUNCTION_DECL)
2006 dump_expr (t, flags | TFF_EXPR_IN_PARENS);
2007 else if (BASELINK_P (t))
2008 dump_expr (OVL_CURRENT (BASELINK_FUNCTIONS (t)),
2009 flags | TFF_EXPR_IN_PARENS);
2011 dump_decl (t, flags);
2015 if (TREE_CODE (ob) == INDIRECT_REF)
2017 dump_expr (TREE_OPERAND (ob, 0), flags | TFF_EXPR_IN_PARENS);
2018 pp_cxx_arrow (cxx_pp);
2019 pp_cxx_star (cxx_pp);
2023 dump_expr (ob, flags | TFF_EXPR_IN_PARENS);
2024 pp_cxx_dot (cxx_pp);
2025 pp_cxx_star (cxx_pp);
2027 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2032 case TEMPLATE_PARM_INDEX:
2033 dump_decl (TEMPLATE_PARM_DECL (t), flags & ~TFF_DECL_SPECIFIERS);
2037 if (TREE_OPERAND (t, 0) == NULL_TREE
2038 || TREE_CHAIN (TREE_OPERAND (t, 0)))
2040 dump_type (TREE_TYPE (t), flags);
2041 pp_cxx_left_paren (cxx_pp);
2042 dump_expr_list (TREE_OPERAND (t, 0), flags);
2043 pp_cxx_right_paren (cxx_pp);
2047 pp_cxx_left_paren (cxx_pp);
2048 dump_type (TREE_TYPE (t), flags);
2049 pp_cxx_right_paren (cxx_pp);
2050 pp_cxx_left_paren (cxx_pp);
2051 dump_expr_list (TREE_OPERAND (t, 0), flags);
2052 pp_cxx_right_paren (cxx_pp);
2056 case STATIC_CAST_EXPR:
2057 pp_cxx_ws_string (cxx_pp, "static_cast");
2059 case REINTERPRET_CAST_EXPR:
2060 pp_cxx_ws_string (cxx_pp, "reinterpret_cast");
2062 case CONST_CAST_EXPR:
2063 pp_cxx_ws_string (cxx_pp, "const_cast");
2065 case DYNAMIC_CAST_EXPR:
2066 pp_cxx_ws_string (cxx_pp, "dynamic_cast");
2068 pp_cxx_begin_template_argument_list (cxx_pp);
2069 dump_type (TREE_TYPE (t), flags);
2070 pp_cxx_end_template_argument_list (cxx_pp);
2071 pp_cxx_left_paren (cxx_pp);
2072 dump_expr (TREE_OPERAND (t, 0), flags);
2073 pp_cxx_right_paren (cxx_pp);
2077 dump_expr (TREE_OPERAND (t, 0), flags);
2078 pp_cxx_arrow (cxx_pp);
2083 if (TREE_CODE (t) == SIZEOF_EXPR)
2084 pp_cxx_ws_string (cxx_pp, "sizeof");
2087 gcc_assert (TREE_CODE (t) == ALIGNOF_EXPR);
2088 pp_cxx_ws_string (cxx_pp, "__alignof__");
2090 pp_cxx_whitespace (cxx_pp);
2091 pp_cxx_left_paren (cxx_pp);
2092 if (TYPE_P (TREE_OPERAND (t, 0)))
2093 dump_type (TREE_OPERAND (t, 0), flags);
2095 dump_expr (TREE_OPERAND (t, 0), flags);
2096 pp_cxx_right_paren (cxx_pp);
2101 pp_cxx_ws_string (cxx_pp, operator_name_info[TREE_CODE (t)].name);
2102 pp_cxx_whitespace (cxx_pp);
2103 dump_expr (TREE_OPERAND (t, 0), flags);
2107 pp_string (cxx_pp, M_("<unparsed>"));
2110 case TRY_CATCH_EXPR:
2111 case WITH_CLEANUP_EXPR:
2112 case CLEANUP_POINT_EXPR:
2113 dump_expr (TREE_OPERAND (t, 0), flags);
2116 case PSEUDO_DTOR_EXPR:
2117 dump_expr (TREE_OPERAND (t, 2), flags);
2118 pp_cxx_dot (cxx_pp);
2119 dump_type (TREE_OPERAND (t, 0), flags);
2120 pp_cxx_colon_colon (cxx_pp);
2121 pp_cxx_complement (cxx_pp);
2122 dump_type (TREE_OPERAND (t, 1), flags);
2125 case TEMPLATE_ID_EXPR:
2126 dump_decl (t, flags);
2132 case STATEMENT_LIST:
2133 /* We don't yet have a way of dumping statements in a
2134 human-readable format. */
2135 pp_string (cxx_pp, "({...})");
2139 pp_string (cxx_pp, "while (1) { ");
2140 dump_expr (TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2141 pp_cxx_right_brace (cxx_pp);
2145 pp_string (cxx_pp, "if (");
2146 dump_expr (TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2147 pp_string (cxx_pp, ") break; ");
2151 dump_expr (get_first_fn (t), flags & ~TFF_EXPR_IN_PARENS);
2154 case EMPTY_CLASS_EXPR:
2155 dump_type (TREE_TYPE (t), flags);
2156 pp_cxx_left_paren (cxx_pp);
2157 pp_cxx_right_paren (cxx_pp);
2160 case NON_DEPENDENT_EXPR:
2161 dump_expr (TREE_OPERAND (t, 0), flags);
2164 case ARGUMENT_PACK_SELECT:
2165 dump_template_argument (ARGUMENT_PACK_SELECT_FROM_PACK (t), flags);
2177 pp_type_specifier_seq (cxx_pp, t);
2181 /* We get here when we want to print a dependent type as an
2182 id-expression, without any disambiguator decoration. */
2183 pp_id_expression (cxx_pp, t);
2186 case TEMPLATE_TYPE_PARM:
2187 case BOUND_TEMPLATE_TEMPLATE_PARM:
2188 dump_type (t, flags);
2192 pp_cxx_trait_expression (cxx_pp, t);
2196 pp_cxx_va_arg_expression (cxx_pp, t);
2200 pp_cxx_offsetof_expression (cxx_pp, t);
2204 dump_decl (t, flags);
2207 case EXPR_PACK_EXPANSION:
2214 case VEC_DELETE_EXPR:
2220 case UNORDERED_EXPR:
2230 case FIX_TRUNC_EXPR:
2232 pp_expression (cxx_pp, t);
2235 case TRUTH_AND_EXPR:
2237 case TRUTH_XOR_EXPR:
2238 if (flags & TFF_EXPR_IN_PARENS)
2239 pp_cxx_left_paren (cxx_pp);
2240 pp_expression (cxx_pp, t);
2241 if (flags & TFF_EXPR_IN_PARENS)
2242 pp_cxx_right_paren (cxx_pp);
2246 dump_expr (resolve_virtual_fun_from_obj_type_ref (t), flags);
2249 /* This list is incomplete, but should suffice for now.
2250 It is very important that `sorry' does not call
2251 `report_error_function'. That could cause an infinite loop. */
2253 pp_unsupported_tree (cxx_pp, t);
2254 /* fall through to ERROR_MARK... */
2256 pp_string (cxx_pp, M_("<expression error>"));
2262 dump_binary_op (const char *opstring, tree t, int flags)
2264 pp_cxx_left_paren (cxx_pp);
2265 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2266 pp_cxx_whitespace (cxx_pp);
2268 pp_cxx_ws_string (cxx_pp, opstring);
2270 pp_string (cxx_pp, M_("<unknown operator>"));
2271 pp_cxx_whitespace (cxx_pp);
2272 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2273 pp_cxx_right_paren (cxx_pp);
2277 dump_unary_op (const char *opstring, tree t, int flags)
2279 if (flags & TFF_EXPR_IN_PARENS)
2280 pp_cxx_left_paren (cxx_pp);
2281 pp_cxx_ws_string (cxx_pp, opstring);
2282 dump_expr (TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2283 if (flags & TFF_EXPR_IN_PARENS)
2284 pp_cxx_right_paren (cxx_pp);
2288 reinit_cxx_pp (void)
2290 pp_clear_output_area (cxx_pp);
2291 pp_base (cxx_pp)->padding = pp_none;
2292 pp_indentation (cxx_pp) = 0;
2293 pp_needs_newline (cxx_pp) = false;
2294 cxx_pp->enclosing_scope = current_function_decl;
2298 /* Exported interface to stringifying types, exprs and decls under TFF_*
2302 type_as_string (tree typ, int flags)
2305 pp_translate_identifiers (cxx_pp) = false;
2306 dump_type (typ, flags);
2307 return pp_formatted_text (cxx_pp);
2311 type_as_string_translate (tree typ, int flags)
2314 dump_type (typ, flags);
2315 return pp_formatted_text (cxx_pp);
2319 expr_as_string (tree decl, int flags)
2322 pp_translate_identifiers (cxx_pp) = false;
2323 dump_expr (decl, flags);
2324 return pp_formatted_text (cxx_pp);
2328 decl_as_string (tree decl, int flags)
2331 pp_translate_identifiers (cxx_pp) = false;
2332 dump_decl (decl, flags);
2333 return pp_formatted_text (cxx_pp);
2337 decl_as_string_translate (tree decl, int flags)
2340 dump_decl (decl, flags);
2341 return pp_formatted_text (cxx_pp);
2344 /* Generate the three forms of printable names for cxx_printable_name. */
2347 lang_decl_name (tree decl, int v, bool translate)
2351 ? decl_as_string_translate (decl, TFF_DECL_SPECIFIERS)
2352 : decl_as_string (decl, TFF_DECL_SPECIFIERS));
2355 pp_translate_identifiers (cxx_pp) = translate;
2357 && (DECL_CLASS_SCOPE_P (decl)
2358 || (DECL_NAMESPACE_SCOPE_P (decl)
2359 && CP_DECL_CONTEXT (decl) != global_namespace)))
2361 dump_type (CP_DECL_CONTEXT (decl), TFF_PLAIN_IDENTIFIER);
2362 pp_cxx_colon_colon (cxx_pp);
2365 if (TREE_CODE (decl) == FUNCTION_DECL)
2366 dump_function_name (decl, TFF_PLAIN_IDENTIFIER);
2368 dump_decl (DECL_NAME (decl), TFF_PLAIN_IDENTIFIER);
2370 return pp_formatted_text (cxx_pp);
2373 /* Return the location of a tree passed to %+ formats. */
2376 location_of (tree t)
2378 if (TREE_CODE (t) == PARM_DECL && DECL_CONTEXT (t))
2379 t = DECL_CONTEXT (t);
2380 else if (TYPE_P (t))
2381 t = TYPE_MAIN_DECL (t);
2382 else if (TREE_CODE (t) == OVERLOAD)
2383 t = OVL_FUNCTION (t);
2385 return DECL_SOURCE_LOCATION (t);
2388 /* Now the interfaces from error et al to dump_type et al. Each takes an
2389 on/off VERBOSE flag and supply the appropriate TFF_ flags to a dump_
2393 decl_to_string (tree decl, int verbose)
2397 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == RECORD_TYPE
2398 || TREE_CODE (decl) == UNION_TYPE || TREE_CODE (decl) == ENUMERAL_TYPE)
2399 flags = TFF_CLASS_KEY_OR_ENUM;
2401 flags |= TFF_DECL_SPECIFIERS;
2402 else if (TREE_CODE (decl) == FUNCTION_DECL)
2403 flags |= TFF_DECL_SPECIFIERS | TFF_RETURN_TYPE;
2404 flags |= TFF_TEMPLATE_HEADER;
2407 dump_decl (decl, flags);
2408 return pp_formatted_text (cxx_pp);
2412 expr_to_string (tree decl)
2415 dump_expr (decl, 0);
2416 return pp_formatted_text (cxx_pp);
2420 fndecl_to_string (tree fndecl, int verbose)
2424 flags = TFF_EXCEPTION_SPECIFICATION | TFF_DECL_SPECIFIERS
2425 | TFF_TEMPLATE_HEADER;
2427 flags |= TFF_FUNCTION_DEFAULT_ARGUMENTS;
2429 dump_decl (fndecl, flags);
2430 return pp_formatted_text (cxx_pp);
2435 code_to_string (enum tree_code c)
2437 return tree_code_name [c];
2441 language_to_string (enum languages c)
2448 case lang_cplusplus:
2460 /* Return the proper printed version of a parameter to a C++ function. */
2463 parm_to_string (int p)
2467 pp_string (cxx_pp, "'this'");
2469 pp_decimal_int (cxx_pp, p + 1);
2470 return pp_formatted_text (cxx_pp);
2474 op_to_string (enum tree_code p)
2476 tree id = operator_name_info[(int) p].identifier;
2477 return id ? IDENTIFIER_POINTER (id) : M_("<unknown>");
2481 type_to_string (tree typ, int verbose)
2485 flags |= TFF_CLASS_KEY_OR_ENUM;
2486 flags |= TFF_TEMPLATE_HEADER;
2489 dump_type (typ, flags);
2490 return pp_formatted_text (cxx_pp);
2494 assop_to_string (enum tree_code p)
2496 tree id = assignment_operator_name_info[(int) p].identifier;
2497 return id ? IDENTIFIER_POINTER (id) : M_("{unknown}");
2501 args_to_string (tree p, int verbose)
2505 flags |= TFF_CLASS_KEY_OR_ENUM;
2510 if (TYPE_P (TREE_VALUE (p)))
2511 return type_as_string_translate (p, flags);
2514 for (; p; p = TREE_CHAIN (p))
2516 if (TREE_VALUE (p) == null_node)
2517 pp_cxx_ws_string (cxx_pp, "NULL");
2519 dump_type (error_type (TREE_VALUE (p)), flags);
2521 pp_separate_with_comma (cxx_pp);
2523 return pp_formatted_text (cxx_pp);
2527 cv_to_string (tree p, int v)
2530 pp_base (cxx_pp)->padding = v ? pp_before : pp_none;
2531 pp_cxx_cv_qualifier_seq (cxx_pp, p);
2532 return pp_formatted_text (cxx_pp);
2535 /* Langhook for print_error_function. */
2537 cxx_print_error_function (diagnostic_context *context, const char *file,
2538 diagnostic_info *diagnostic)
2540 lhd_print_error_function (context, file, diagnostic);
2541 pp_base_set_prefix (context->printer, file);
2542 maybe_print_instantiation_context (context);
2546 cp_diagnostic_starter (diagnostic_context *context,
2547 diagnostic_info *diagnostic)
2549 diagnostic_report_current_module (context);
2550 cp_print_error_function (context, diagnostic);
2551 maybe_print_instantiation_context (context);
2552 pp_base_set_prefix (context->printer, diagnostic_build_prefix (diagnostic));
2556 cp_diagnostic_finalizer (diagnostic_context *context,
2557 diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
2559 pp_base_destroy_prefix (context->printer);
2562 /* Print current function onto BUFFER, in the process of reporting
2563 a diagnostic message. Called from cp_diagnostic_starter. */
2565 cp_print_error_function (diagnostic_context *context,
2566 diagnostic_info *diagnostic)
2568 if (diagnostic_last_function_changed (context, diagnostic))
2570 const char *old_prefix = context->printer->prefix;
2571 const char *file = LOCATION_FILE (diagnostic->location);
2572 tree abstract_origin = diagnostic->abstract_origin;
2573 char *new_prefix = (file && abstract_origin == NULL)
2574 ? file_name_as_prefix (file) : NULL;
2576 pp_base_set_prefix (context->printer, new_prefix);
2578 if (current_function_decl == NULL)
2579 pp_base_string (context->printer, _("At global scope:"));
2584 if (abstract_origin)
2586 ao = BLOCK_ABSTRACT_ORIGIN (abstract_origin);
2587 while (TREE_CODE (ao) == BLOCK
2588 && BLOCK_ABSTRACT_ORIGIN (ao)
2589 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
2590 ao = BLOCK_ABSTRACT_ORIGIN (ao);
2591 gcc_assert (TREE_CODE (ao) == FUNCTION_DECL);
2595 fndecl = current_function_decl;
2597 pp_printf (context->printer, function_category (fndecl),
2598 cxx_printable_name_translate (fndecl, 2));
2600 while (abstract_origin)
2603 tree block = abstract_origin;
2605 locus = &BLOCK_SOURCE_LOCATION (block);
2607 block = BLOCK_SUPERCONTEXT (block);
2608 while (block && TREE_CODE (block) == BLOCK
2609 && BLOCK_ABSTRACT_ORIGIN (block))
2611 ao = BLOCK_ABSTRACT_ORIGIN (block);
2613 while (TREE_CODE (ao) == BLOCK
2614 && BLOCK_ABSTRACT_ORIGIN (ao)
2615 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
2616 ao = BLOCK_ABSTRACT_ORIGIN (ao);
2618 if (TREE_CODE (ao) == FUNCTION_DECL)
2623 else if (TREE_CODE (ao) != BLOCK)
2626 block = BLOCK_SUPERCONTEXT (block);
2629 abstract_origin = block;
2632 while (block && TREE_CODE (block) == BLOCK)
2633 block = BLOCK_SUPERCONTEXT (block);
2635 if (block && TREE_CODE (block) == FUNCTION_DECL)
2637 abstract_origin = NULL;
2641 expanded_location s = expand_location (*locus);
2642 pp_base_character (context->printer, ',');
2643 pp_base_newline (context->printer);
2646 if (flag_show_column && s.column != 0)
2647 pp_printf (context->printer,
2648 _(" inlined from %qs at %s:%d:%d"),
2649 cxx_printable_name_translate (fndecl, 2),
2650 s.file, s.line, s.column);
2652 pp_printf (context->printer,
2653 _(" inlined from %qs at %s:%d"),
2654 cxx_printable_name_translate (fndecl, 2),
2659 pp_printf (context->printer, _(" inlined from %qs"),
2660 cxx_printable_name_translate (fndecl, 2));
2663 pp_base_character (context->printer, ':');
2665 pp_base_newline (context->printer);
2667 diagnostic_set_last_function (context, diagnostic);
2668 pp_base_destroy_prefix (context->printer);
2669 context->printer->prefix = old_prefix;
2673 /* Returns a description of FUNCTION using standard terminology. The
2674 result is a format string of the form "In CATEGORY %qs". */
2676 function_category (tree fn)
2678 /* We can get called from the middle-end for diagnostics of function
2679 clones. Make sure we have language specific information before
2680 dereferencing it. */
2681 if (DECL_LANG_SPECIFIC (STRIP_TEMPLATE (fn))
2682 && DECL_FUNCTION_MEMBER_P (fn))
2684 if (DECL_STATIC_FUNCTION_P (fn))
2685 return _("In static member function %qs");
2686 else if (DECL_COPY_CONSTRUCTOR_P (fn))
2687 return _("In copy constructor %qs");
2688 else if (DECL_CONSTRUCTOR_P (fn))
2689 return _("In constructor %qs");
2690 else if (DECL_DESTRUCTOR_P (fn))
2691 return _("In destructor %qs");
2692 else if (LAMBDA_FUNCTION_P (fn))
2693 return _("In lambda function");
2695 return _("In member function %qs");
2698 return _("In function %qs");
2701 /* Report the full context of a current template instantiation,
2704 print_instantiation_full_context (diagnostic_context *context)
2706 struct tinst_level *p = current_instantiation ();
2707 location_t location = input_location;
2711 if (current_function_decl != p->decl
2712 && current_function_decl != NULL_TREE)
2713 /* We can get here during the processing of some synthesized
2714 method. Then, P->DECL will be the function that's causing
2719 if (current_function_decl == p->decl)
2720 /* Avoid redundancy with the "In function" line. */;
2722 pp_verbatim (context->printer,
2723 _("%s: In instantiation of %qs:\n"),
2724 LOCATION_FILE (location),
2725 decl_as_string_translate (p->decl,
2726 TFF_DECL_SPECIFIERS | TFF_RETURN_TYPE));
2728 location = p->locus;
2733 print_instantiation_partial_context (context, p, location);
2736 /* Helper function of print_instantiation_partial_context() that
2737 prints a single line of instantiation context. */
2740 print_instantiation_partial_context_line (diagnostic_context *context,
2741 const struct tinst_level *t,
2742 location_t loc, bool recursive_p)
2744 expanded_location xloc;
2745 xloc = expand_location (loc);
2750 str = decl_as_string_translate (t->decl,
2751 TFF_DECL_SPECIFIERS | TFF_RETURN_TYPE);
2752 if (flag_show_column)
2753 pp_verbatim (context->printer,
2755 ? _("%s:%d:%d: recursively instantiated from %qs\n")
2756 : _("%s:%d:%d: instantiated from %qs\n"),
2757 xloc.file, xloc.line, xloc.column, str);
2759 pp_verbatim (context->printer,
2761 ? _("%s:%d: recursively instantiated from %qs\n")
2762 : _("%s:%d: recursively instantiated from %qs\n"),
2763 xloc.file, xloc.line, str);
2767 if (flag_show_column)
2768 pp_verbatim (context->printer,
2770 ? _("%s:%d:%d: recursively instantiated from here")
2771 : _("%s:%d:%d: instantiated from here"),
2772 xloc.file, xloc.line, xloc.column);
2774 pp_verbatim (context->printer,
2776 ? _("%s:%d: recursively instantiated from here")
2777 : _("%s:%d: instantiated from here"),
2778 xloc.file, xloc.line);
2782 /* Same as print_instantiation_full_context but less verbose. */
2785 print_instantiation_partial_context (diagnostic_context *context,
2786 struct tinst_level *t0, location_t loc)
2788 struct tinst_level *t;
2791 location_t prev_loc = loc;
2793 for (t = t0; t != NULL; t = t->next)
2794 if (prev_loc != t->locus)
2796 prev_loc = t->locus;
2804 int skip = n_total - 10;
2805 for (n = 0; n < 5; n++)
2807 gcc_assert (t != NULL);
2808 if (loc != t->locus)
2809 print_instantiation_partial_context_line (context, t, loc,
2810 /*recursive_p=*/false);
2814 if (t != NULL && skip > 1)
2816 expanded_location xloc;
2817 xloc = expand_location (loc);
2818 if (flag_show_column)
2819 pp_verbatim (context->printer,
2820 _("%s:%d:%d: [ skipping %d instantiation contexts ]\n"),
2821 xloc.file, xloc.line, xloc.column, skip);
2823 pp_verbatim (context->printer,
2824 _("%s:%d: [ skipping %d instantiation contexts ]\n"),
2825 xloc.file, xloc.line, skip);
2830 } while (t != NULL && --skip > 0);
2836 while (t->next != NULL && t->locus == t->next->locus)
2841 print_instantiation_partial_context_line (context, t, loc,
2846 print_instantiation_partial_context_line (context, NULL, loc,
2847 /*recursive_p=*/false);
2848 pp_base_newline (context->printer);
2851 /* Called from cp_thing to print the template context for an error. */
2853 maybe_print_instantiation_context (diagnostic_context *context)
2855 if (!problematic_instantiation_changed () || current_instantiation () == 0)
2858 record_last_problematic_instantiation ();
2859 print_instantiation_full_context (context);
2862 /* Report the bare minimum context of a template instantiation. */
2864 print_instantiation_context (void)
2866 print_instantiation_partial_context
2867 (global_dc, current_instantiation (), input_location);
2868 diagnostic_flush_buffer (global_dc);
2871 /* Called from output_format -- during diagnostic message processing --
2872 to handle C++ specific format specifier with the following meanings:
2873 %A function argument-list.
2877 %F function declaration.
2878 %L language as used in extern "lang".
2880 %P function parameter whose position is indicated by an integer.
2881 %Q assignment operator.
2885 cp_printer (pretty_printer *pp, text_info *text, const char *spec,
2886 int precision, bool wide, bool set_locus, bool verbose)
2890 #define next_tree (t = va_arg (*text->args_ptr, tree))
2891 #define next_tcode ((enum tree_code) va_arg (*text->args_ptr, int))
2892 #define next_lang ((enum languages) va_arg (*text->args_ptr, int))
2893 #define next_int va_arg (*text->args_ptr, int)
2895 if (precision != 0 || wide)
2898 if (text->locus == NULL)
2903 case 'A': result = args_to_string (next_tree, verbose); break;
2904 case 'C': result = code_to_string (next_tcode); break;
2907 tree temp = next_tree;
2909 && DECL_DEBUG_EXPR_IS_FROM (temp) && DECL_DEBUG_EXPR (temp))
2911 temp = DECL_DEBUG_EXPR (temp);
2914 result = expr_to_string (temp);
2918 result = decl_to_string (temp, verbose);
2921 case 'E': result = expr_to_string (next_tree); break;
2922 case 'F': result = fndecl_to_string (next_tree, verbose); break;
2923 case 'L': result = language_to_string (next_lang); break;
2924 case 'O': result = op_to_string (next_tcode); break;
2925 case 'P': result = parm_to_string (next_int); break;
2926 case 'Q': result = assop_to_string (next_tcode); break;
2927 case 'T': result = type_to_string (next_tree, verbose); break;
2928 case 'V': result = cv_to_string (next_tree, verbose); break;
2934 pp_base_string (pp, result);
2935 if (set_locus && t != NULL)
2936 *text->locus = location_of (t);
2944 /* Warn about the use of C++0x features when appropriate. */
2946 maybe_warn_cpp0x (cpp0x_warn_str str)
2948 if ((cxx_dialect == cxx98) && !in_system_header)
2949 /* We really want to suppress this warning in system headers,
2950 because libstdc++ uses variadic templates even when we aren't
2954 case CPP0X_INITIALIZER_LISTS:
2955 pedwarn (input_location, 0,
2956 "extended initializer lists "
2957 "only available with -std=c++0x or -std=gnu++0x");
2959 case CPP0X_EXPLICIT_CONVERSION:
2960 pedwarn (input_location, 0,
2961 "explicit conversion operators "
2962 "only available with -std=c++0x or -std=gnu++0x");
2964 case CPP0X_VARIADIC_TEMPLATES:
2965 pedwarn (input_location, 0,
2966 "variadic templates "
2967 "only available with -std=c++0x or -std=gnu++0x");
2969 case CPP0X_LAMBDA_EXPR:
2970 pedwarn (input_location, 0,
2971 "lambda expressions "
2972 "only available with -std=c++0x or -std=gnu++0x");
2975 pedwarn (input_location, 0,
2976 "C++0x auto only available with -std=c++0x or -std=gnu++0x");
2978 case CPP0X_SCOPED_ENUMS:
2979 pedwarn (input_location, 0,
2980 "scoped enums only available with -std=c++0x or -std=gnu++0x");
2982 case CPP0X_DEFAULTED_DELETED:
2983 pedwarn (input_location, 0,
2984 "defaulted and deleted functions "
2985 "only available with -std=c++0x or -std=gnu++0x");
2992 /* Warn about the use of variadic templates when appropriate. */
2994 maybe_warn_variadic_templates (void)
2996 maybe_warn_cpp0x (CPP0X_VARIADIC_TEMPLATES);
3000 /* Issue an ISO C++98 pedantic warning at LOCATION, conditional on
3001 option OPT with text GMSGID. Use this function to report
3002 diagnostics for constructs that are invalid C++98, but valid
3005 pedwarn_cxx98 (location_t location, int opt, const char *gmsgid, ...)
3007 diagnostic_info diagnostic;
3010 va_start (ap, gmsgid);
3011 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
3012 (cxx_dialect == cxx98) ? DK_PEDWARN : DK_WARNING);
3013 diagnostic.option_index = opt;
3015 return report_diagnostic (&diagnostic);