1 /* Call-backs for C++ error reporting.
2 This code is non-reentrant.
3 Copyright (C) 1993-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
27 #include "stringpool.h"
30 #include "diagnostic.h"
31 #include "tree-diagnostic.h"
32 #include "langhooks-def.h"
34 #include "cxx-pretty-print.h"
35 #include "tree-pretty-print.h"
36 #include "c-family/c-objc.h"
38 #include "internal-fn.h"
40 #include <new> // For placement-new.
42 #define pp_separate_with_comma(PP) pp_cxx_separate_with (PP, ',')
43 #define pp_separate_with_semicolon(PP) pp_cxx_separate_with (PP, ';')
45 /* cxx_pp is a C++ front-end-specific pretty printer: this is where we
46 dump C++ ASTs as strings. It is mostly used only by the various
47 tree -> string functions that are occasionally called from the
48 debugger or by the front-end for things like
49 __PRETTY_FUNCTION__. */
50 static cxx_pretty_printer actual_pretty_printer
;
51 static cxx_pretty_printer
* const cxx_pp
= &actual_pretty_printer
;
53 /* Translate if being used for diagnostics, but not for dump files or
55 #define M_(msgid) (pp_translate_identifiers (cxx_pp) ? _(msgid) : (msgid))
57 # define NEXT_CODE(T) (TREE_CODE (TREE_TYPE (T)))
59 static const char *args_to_string (tree
, int);
60 static const char *assop_to_string (enum tree_code
);
61 static const char *code_to_string (enum tree_code
);
62 static const char *cv_to_string (tree
, int);
63 static const char *decl_to_string (tree
, int);
64 static const char *expr_to_string (tree
);
65 static const char *fndecl_to_string (tree
, int);
66 static const char *op_to_string (enum tree_code
);
67 static const char *parm_to_string (int);
68 static const char *type_to_string (tree
, int);
70 static void dump_alias_template_specialization (cxx_pretty_printer
*, tree
, int);
71 static void dump_type (cxx_pretty_printer
*, tree
, int);
72 static void dump_typename (cxx_pretty_printer
*, tree
, int);
73 static void dump_simple_decl (cxx_pretty_printer
*, tree
, tree
, int);
74 static void dump_decl (cxx_pretty_printer
*, tree
, int);
75 static void dump_template_decl (cxx_pretty_printer
*, tree
, int);
76 static void dump_function_decl (cxx_pretty_printer
*, tree
, int);
77 static void dump_expr (cxx_pretty_printer
*, tree
, int);
78 static void dump_unary_op (cxx_pretty_printer
*, const char *, tree
, int);
79 static void dump_binary_op (cxx_pretty_printer
*, const char *, tree
, int);
80 static void dump_aggr_type (cxx_pretty_printer
*, tree
, int);
81 static void dump_type_prefix (cxx_pretty_printer
*, tree
, int);
82 static void dump_type_suffix (cxx_pretty_printer
*, tree
, int);
83 static void dump_function_name (cxx_pretty_printer
*, tree
, int);
84 static void dump_call_expr_args (cxx_pretty_printer
*, tree
, int, bool);
85 static void dump_aggr_init_expr_args (cxx_pretty_printer
*, tree
, int, bool);
86 static void dump_expr_list (cxx_pretty_printer
*, tree
, int);
87 static void dump_global_iord (cxx_pretty_printer
*, tree
);
88 static void dump_parameters (cxx_pretty_printer
*, tree
, int);
89 static void dump_ref_qualifier (cxx_pretty_printer
*, tree
, int);
90 static void dump_exception_spec (cxx_pretty_printer
*, tree
, int);
91 static void dump_template_argument (cxx_pretty_printer
*, tree
, int);
92 static void dump_template_argument_list (cxx_pretty_printer
*, tree
, int);
93 static void dump_template_parameter (cxx_pretty_printer
*, tree
, int);
94 static void dump_template_bindings (cxx_pretty_printer
*, tree
, tree
,
96 static void dump_scope (cxx_pretty_printer
*, tree
, int);
97 static void dump_template_parms (cxx_pretty_printer
*, tree
, int, int);
98 static int get_non_default_template_args_count (tree
, int);
99 static const char *function_category (tree
);
100 static void maybe_print_constexpr_context (diagnostic_context
*);
101 static void maybe_print_instantiation_context (diagnostic_context
*);
102 static void print_instantiation_full_context (diagnostic_context
*);
103 static void print_instantiation_partial_context (diagnostic_context
*,
104 struct tinst_level
*,
106 static void cp_diagnostic_starter (diagnostic_context
*, diagnostic_info
*);
107 static void cp_print_error_function (diagnostic_context
*, diagnostic_info
*);
109 static bool cp_printer (pretty_printer
*, text_info
*, const char *,
110 int, bool, bool, bool);
112 /* CONTEXT->printer is a basic pretty printer that was constructed
113 presumably by diagnostic_initialize(), called early in the
114 compiler's initialization process (in general_init) Before the FE
115 is initialized. This (C++) FE-specific diagnostic initializer is
116 thus replacing the basic pretty printer with one that has C++-aware
120 cxx_initialize_diagnostics (diagnostic_context
*context
)
122 pretty_printer
*base
= context
->printer
;
123 cxx_pretty_printer
*pp
= XNEW (cxx_pretty_printer
);
124 context
->printer
= new (pp
) cxx_pretty_printer ();
126 /* It is safe to free this object because it was previously XNEW()'d. */
127 base
->~pretty_printer ();
130 c_common_diagnostics_set_defaults (context
);
131 diagnostic_starter (context
) = cp_diagnostic_starter
;
132 /* diagnostic_finalizer is already c_diagnostic_finalizer. */
133 diagnostic_format_decoder (context
) = cp_printer
;
136 /* Dump a scope, if deemed necessary. */
139 dump_scope (cxx_pretty_printer
*pp
, tree scope
, int flags
)
141 int f
= flags
& (TFF_SCOPE
| TFF_CHASE_TYPEDEF
);
143 if (scope
== NULL_TREE
)
146 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
148 if (scope
!= global_namespace
)
150 dump_decl (pp
, scope
, f
);
151 pp_cxx_colon_colon (pp
);
154 else if (AGGREGATE_TYPE_P (scope
))
156 dump_type (pp
, scope
, f
);
157 pp_cxx_colon_colon (pp
);
159 else if ((flags
& TFF_SCOPE
) && TREE_CODE (scope
) == FUNCTION_DECL
)
161 dump_function_decl (pp
, scope
, f
);
162 pp_cxx_colon_colon (pp
);
166 /* Dump the template ARGument under control of FLAGS. */
169 dump_template_argument (cxx_pretty_printer
*pp
, tree arg
, int flags
)
171 if (ARGUMENT_PACK_P (arg
))
172 dump_template_argument_list (pp
, ARGUMENT_PACK_ARGS (arg
),
173 /* No default args in argument packs. */
174 flags
|TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS
);
175 else if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
176 dump_type (pp
, arg
, flags
& ~TFF_CLASS_KEY_OR_ENUM
);
179 if (TREE_CODE (arg
) == TREE_LIST
)
180 arg
= TREE_VALUE (arg
);
182 dump_expr (pp
, arg
, (flags
| TFF_EXPR_IN_PARENS
) & ~TFF_CLASS_KEY_OR_ENUM
);
186 /* Count the number of template arguments ARGS whose value does not
187 match the (optional) default template parameter in PARAMS */
190 get_non_default_template_args_count (tree args
, int flags
)
192 int n
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_ARGS (args
));
194 if (/* We use this flag when generating debug information. We don't
195 want to expand templates at this point, for this may generate
196 new decls, which gets decl counts out of sync, which may in
197 turn cause codegen differences between compilations with and
199 (flags
& TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS
) != 0
200 || !flag_pretty_templates
)
203 return GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (INNERMOST_TEMPLATE_ARGS (args
));
206 /* Dump a template-argument-list ARGS (always a TREE_VEC) under control
210 dump_template_argument_list (cxx_pretty_printer
*pp
, tree args
, int flags
)
212 int n
= get_non_default_template_args_count (args
, flags
);
216 for (i
= 0; i
< n
; ++i
)
218 tree arg
= TREE_VEC_ELT (args
, i
);
220 /* Only print a comma if we know there is an argument coming. In
221 the case of an empty template argument pack, no actual
222 argument will be printed. */
224 && (!ARGUMENT_PACK_P (arg
)
225 || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) > 0))
226 pp_separate_with_comma (pp
);
228 dump_template_argument (pp
, arg
, flags
);
233 /* Dump a template parameter PARM (a TREE_LIST) under control of FLAGS. */
236 dump_template_parameter (cxx_pretty_printer
*pp
, tree parm
, int flags
)
241 if (parm
== error_mark_node
)
244 p
= TREE_VALUE (parm
);
245 a
= TREE_PURPOSE (parm
);
247 if (TREE_CODE (p
) == TYPE_DECL
)
249 if (flags
& TFF_DECL_SPECIFIERS
)
251 pp_cxx_ws_string (pp
, "class");
252 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (p
)))
253 pp_cxx_ws_string (pp
, "...");
255 pp_cxx_tree_identifier (pp
, DECL_NAME (p
));
257 else if (DECL_NAME (p
))
258 pp_cxx_tree_identifier (pp
, DECL_NAME (p
));
260 pp_cxx_canonical_template_parameter (pp
, TREE_TYPE (p
));
263 dump_decl (pp
, p
, flags
| TFF_DECL_SPECIFIERS
);
265 if ((flags
& TFF_FUNCTION_DEFAULT_ARGUMENTS
) && a
!= NULL_TREE
)
267 pp_cxx_whitespace (pp
);
269 pp_cxx_whitespace (pp
);
270 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
271 dump_type (pp
, a
, flags
& ~TFF_CHASE_TYPEDEF
);
273 dump_expr (pp
, a
, flags
| TFF_EXPR_IN_PARENS
);
277 /* Dump, under control of FLAGS, a template-parameter-list binding.
278 PARMS is a TREE_LIST of TREE_VEC of TREE_LIST and ARGS is a
282 dump_template_bindings (cxx_pretty_printer
*pp
, tree parms
, tree args
,
283 vec
<tree
, va_gc
> *typenames
)
285 bool need_semicolon
= false;
291 tree p
= TREE_VALUE (parms
);
292 int lvl
= TMPL_PARMS_DEPTH (parms
);
295 tree lvl_args
= NULL_TREE
;
297 /* Don't crash if we had an invalid argument list. */
298 if (TMPL_ARGS_DEPTH (args
) >= lvl
)
299 lvl_args
= TMPL_ARGS_LEVEL (args
, lvl
);
301 for (i
= 0; i
< TREE_VEC_LENGTH (p
); ++i
)
303 tree arg
= NULL_TREE
;
305 /* Don't crash if we had an invalid argument list. */
306 if (lvl_args
&& NUM_TMPL_ARGS (lvl_args
) > arg_idx
)
307 arg
= TREE_VEC_ELT (lvl_args
, arg_idx
);
310 pp_separate_with_semicolon (pp
);
311 dump_template_parameter (pp
, TREE_VEC_ELT (p
, i
),
312 TFF_PLAIN_IDENTIFIER
);
313 pp_cxx_whitespace (pp
);
315 pp_cxx_whitespace (pp
);
318 if (ARGUMENT_PACK_P (arg
))
319 pp_cxx_left_brace (pp
);
320 dump_template_argument (pp
, arg
, TFF_PLAIN_IDENTIFIER
);
321 if (ARGUMENT_PACK_P (arg
))
322 pp_cxx_right_brace (pp
);
325 pp_string (pp
, M_("<missing>"));
328 need_semicolon
= true;
331 parms
= TREE_CHAIN (parms
);
334 /* Don't bother with typenames for a partial instantiation. */
335 if (vec_safe_is_empty (typenames
) || uses_template_parms (args
))
338 /* Don't try to print typenames when we're processing a clone. */
339 if (current_function_decl
340 && !DECL_LANG_SPECIFIC (current_function_decl
))
343 FOR_EACH_VEC_SAFE_ELT (typenames
, i
, t
)
346 pp_separate_with_semicolon (pp
);
347 dump_type (pp
, t
, TFF_PLAIN_IDENTIFIER
);
348 pp_cxx_whitespace (pp
);
350 pp_cxx_whitespace (pp
);
351 push_deferring_access_checks (dk_no_check
);
352 t
= tsubst (t
, args
, tf_none
, NULL_TREE
);
353 pop_deferring_access_checks ();
354 /* Strip typedefs. We can't just use TFF_CHASE_TYPEDEF because
355 pp_simple_type_specifier doesn't know about it. */
356 t
= strip_typedefs (t
);
357 dump_type (pp
, t
, TFF_PLAIN_IDENTIFIER
);
361 /* Dump a human-readable equivalent of the alias template
362 specialization of T. */
365 dump_alias_template_specialization (cxx_pretty_printer
*pp
, tree t
, int flags
)
369 gcc_assert (alias_template_specialization_p (t
));
371 if (!(flags
& TFF_UNQUALIFIED_NAME
))
372 dump_scope (pp
, CP_DECL_CONTEXT (TYPE_NAME (t
)), flags
);
373 name
= TYPE_IDENTIFIER (t
);
374 pp_cxx_tree_identifier (pp
, name
);
375 dump_template_parms (pp
, TYPE_TEMPLATE_INFO (t
),
377 flags
& ~TFF_TEMPLATE_HEADER
);
380 /* Dump a human-readable equivalent of TYPE. FLAGS controls the
384 dump_type (cxx_pretty_printer
*pp
, tree t
, int flags
)
389 /* Don't print e.g. "struct mytypedef". */
390 if (TYPE_P (t
) && typedef_variant_p (t
))
392 tree decl
= TYPE_NAME (t
);
393 if ((flags
& TFF_CHASE_TYPEDEF
)
394 || DECL_SELF_REFERENCE_P (decl
)
395 || (!flag_pretty_templates
396 && DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)))
397 t
= strip_typedefs (t
);
398 else if (alias_template_specialization_p (t
))
400 dump_alias_template_specialization (pp
, t
, flags
);
403 else if (same_type_p (t
, TREE_TYPE (decl
)))
407 pp_cxx_cv_qualifier_seq (pp
, t
);
408 pp_cxx_tree_identifier (pp
, TYPE_IDENTIFIER (t
));
413 if (TYPE_PTRMEMFUNC_P (t
))
416 switch (TREE_CODE (t
))
419 if (t
== init_list_type_node
)
420 pp_string (pp
, M_("<brace-enclosed initializer list>"));
421 else if (t
== unknown_type_node
)
422 pp_string (pp
, M_("<unresolved overloaded function type>"));
425 pp_cxx_cv_qualifier_seq (pp
, t
);
426 pp_cxx_tree_identifier (pp
, TYPE_IDENTIFIER (t
));
431 /* A list of function parms. */
432 dump_parameters (pp
, t
, flags
);
435 case IDENTIFIER_NODE
:
436 pp_cxx_tree_identifier (pp
, t
);
440 dump_type (pp
, BINFO_TYPE (t
), flags
);
446 dump_aggr_type (pp
, t
, flags
);
450 if (flags
& TFF_CHASE_TYPEDEF
)
452 dump_type (pp
, DECL_ORIGINAL_TYPE (t
)
453 ? DECL_ORIGINAL_TYPE (t
) : TREE_TYPE (t
), flags
);
456 /* Else fall through. */
460 dump_decl (pp
, t
, flags
& ~TFF_DECL_SPECIFIERS
);
469 case FIXED_POINT_TYPE
:
470 pp_type_specifier_seq (pp
, t
);
473 case TEMPLATE_TEMPLATE_PARM
:
474 /* For parameters inside template signature. */
475 if (TYPE_IDENTIFIER (t
))
476 pp_cxx_tree_identifier (pp
, TYPE_IDENTIFIER (t
));
478 pp_cxx_canonical_template_parameter (pp
, t
);
481 case BOUND_TEMPLATE_TEMPLATE_PARM
:
483 tree args
= TYPE_TI_ARGS (t
);
484 pp_cxx_cv_qualifier_seq (pp
, t
);
485 pp_cxx_tree_identifier (pp
, TYPE_IDENTIFIER (t
));
486 pp_cxx_begin_template_argument_list (pp
);
487 dump_template_argument_list (pp
, args
, flags
);
488 pp_cxx_end_template_argument_list (pp
);
492 case TEMPLATE_TYPE_PARM
:
493 pp_cxx_cv_qualifier_seq (pp
, t
);
494 if (TYPE_IDENTIFIER (t
))
495 pp_cxx_tree_identifier (pp
, TYPE_IDENTIFIER (t
));
497 pp_cxx_canonical_template_parameter
498 (pp
, TEMPLATE_TYPE_PARM_INDEX (t
));
501 /* This is not always necessary for pointers and such, but doing this
502 reduces code size. */
511 dump_type_prefix (pp
, t
, flags
);
512 dump_type_suffix (pp
, t
, flags
);
516 if (! (flags
& TFF_CHASE_TYPEDEF
)
517 && DECL_ORIGINAL_TYPE (TYPE_NAME (t
)))
519 dump_decl (pp
, TYPE_NAME (t
), TFF_PLAIN_IDENTIFIER
);
522 pp_cxx_cv_qualifier_seq (pp
, t
);
523 pp_cxx_ws_string (pp
,
524 TYPENAME_IS_ENUM_P (t
) ? "enum"
525 : TYPENAME_IS_CLASS_P (t
) ? "class"
527 dump_typename (pp
, t
, flags
);
530 case UNBOUND_CLASS_TEMPLATE
:
531 if (! (flags
& TFF_UNQUALIFIED_NAME
))
533 dump_type (pp
, TYPE_CONTEXT (t
), flags
);
534 pp_cxx_colon_colon (pp
);
536 pp_cxx_ws_string (pp
, "template");
537 dump_type (pp
, TYPE_IDENTIFIER (t
), flags
);
541 pp_cxx_ws_string (pp
, "__typeof__");
542 pp_cxx_whitespace (pp
);
543 pp_cxx_left_paren (pp
);
544 dump_expr (pp
, TYPEOF_TYPE_EXPR (t
), flags
& ~TFF_EXPR_IN_PARENS
);
545 pp_cxx_right_paren (pp
);
548 case UNDERLYING_TYPE
:
549 pp_cxx_ws_string (pp
, "__underlying_type");
550 pp_cxx_whitespace (pp
);
551 pp_cxx_left_paren (pp
);
552 dump_expr (pp
, UNDERLYING_TYPE_TYPE (t
), flags
& ~TFF_EXPR_IN_PARENS
);
553 pp_cxx_right_paren (pp
);
556 case TYPE_PACK_EXPANSION
:
557 dump_type (pp
, PACK_EXPANSION_PATTERN (t
), flags
);
558 pp_cxx_ws_string (pp
, "...");
561 case TYPE_ARGUMENT_PACK
:
562 dump_template_argument (pp
, t
, flags
);
566 pp_cxx_ws_string (pp
, "decltype");
567 pp_cxx_whitespace (pp
);
568 pp_cxx_left_paren (pp
);
569 dump_expr (pp
, DECLTYPE_TYPE_EXPR (t
), flags
& ~TFF_EXPR_IN_PARENS
);
570 pp_cxx_right_paren (pp
);
574 pp_string (pp
, "std::nullptr_t");
578 pp_unsupported_tree (pp
, t
);
579 /* Fall through to error. */
582 pp_string (pp
, M_("<type error>"));
587 /* Dump a TYPENAME_TYPE. We need to notice when the context is itself
591 dump_typename (cxx_pretty_printer
*pp
, tree t
, int flags
)
593 tree ctx
= TYPE_CONTEXT (t
);
595 if (TREE_CODE (ctx
) == TYPENAME_TYPE
)
596 dump_typename (pp
, ctx
, flags
);
598 dump_type (pp
, ctx
, flags
& ~TFF_CLASS_KEY_OR_ENUM
);
599 pp_cxx_colon_colon (pp
);
600 dump_decl (pp
, TYPENAME_TYPE_FULLNAME (t
), flags
);
603 /* Return the name of the supplied aggregate, or enumeral type. */
606 class_key_or_enum_as_string (tree t
)
608 if (TREE_CODE (t
) == ENUMERAL_TYPE
)
610 if (SCOPED_ENUM_P (t
))
615 else if (TREE_CODE (t
) == UNION_TYPE
)
617 else if (TYPE_LANG_SPECIFIC (t
) && CLASSTYPE_DECLARED_CLASS (t
))
623 /* Print out a class declaration T under the control of FLAGS,
624 in the form `class foo'. */
627 dump_aggr_type (cxx_pretty_printer
*pp
, tree t
, int flags
)
630 const char *variety
= class_key_or_enum_as_string (t
);
634 pp_cxx_cv_qualifier_seq (pp
, t
);
636 if (flags
& TFF_CLASS_KEY_OR_ENUM
)
637 pp_cxx_ws_string (pp
, variety
);
639 name
= TYPE_NAME (t
);
643 typdef
= (!DECL_ARTIFICIAL (name
)
644 /* An alias specialization is not considered to be a
646 && !alias_template_specialization_p (t
));
649 && ((flags
& TFF_CHASE_TYPEDEF
)
650 || (!flag_pretty_templates
&& DECL_LANG_SPECIFIC (name
)
651 && DECL_TEMPLATE_INFO (name
))))
652 || DECL_SELF_REFERENCE_P (name
))
654 t
= TYPE_MAIN_VARIANT (t
);
655 name
= TYPE_NAME (t
);
659 tmplate
= !typdef
&& TREE_CODE (t
) != ENUMERAL_TYPE
660 && TYPE_LANG_SPECIFIC (t
) && CLASSTYPE_TEMPLATE_INFO (t
)
661 && (TREE_CODE (CLASSTYPE_TI_TEMPLATE (t
)) != TEMPLATE_DECL
662 || PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)));
664 if (! (flags
& TFF_UNQUALIFIED_NAME
))
665 dump_scope (pp
, CP_DECL_CONTEXT (name
), flags
| TFF_SCOPE
);
666 flags
&= ~TFF_UNQUALIFIED_NAME
;
669 /* Because the template names are mangled, we have to locate
670 the most general template, and use that name. */
671 tree tpl
= TYPE_TI_TEMPLATE (t
);
673 while (DECL_TEMPLATE_INFO (tpl
))
674 tpl
= DECL_TI_TEMPLATE (tpl
);
677 name
= DECL_NAME (name
);
680 if (name
== 0 || anon_aggrname_p (name
))
682 if (flags
& TFF_CLASS_KEY_OR_ENUM
)
683 pp_string (pp
, M_("<anonymous>"));
685 pp_printf (pp
, M_("<anonymous %s>"), variety
);
687 else if (LAMBDA_TYPE_P (t
))
689 /* A lambda's "type" is essentially its signature. */
690 pp_string (pp
, M_("<lambda"));
691 if (lambda_function (t
))
693 FUNCTION_FIRST_USER_PARMTYPE (lambda_function (t
)),
698 pp_cxx_tree_identifier (pp
, name
);
700 dump_template_parms (pp
, TYPE_TEMPLATE_INFO (t
),
701 !CLASSTYPE_USE_TEMPLATE (t
),
702 flags
& ~TFF_TEMPLATE_HEADER
);
705 /* Dump into the obstack the initial part of the output for a given type.
706 This is necessary when dealing with things like functions returning
709 return type of `int (* fee ())()': pointer -> function -> int. Both
710 pointer (and reference and offset) and function (and member) types must
711 deal with prefix and suffix.
713 Arrays must also do this for DECL nodes, like int a[], and for things like
717 dump_type_prefix (cxx_pretty_printer
*pp
, tree t
, int flags
)
719 if (TYPE_PTRMEMFUNC_P (t
))
721 t
= TYPE_PTRMEMFUNC_FN_TYPE (t
);
725 switch (TREE_CODE (t
))
730 tree sub
= TREE_TYPE (t
);
732 dump_type_prefix (pp
, sub
, flags
);
733 if (TREE_CODE (sub
) == ARRAY_TYPE
734 || TREE_CODE (sub
) == FUNCTION_TYPE
)
736 pp_cxx_whitespace (pp
);
737 pp_cxx_left_paren (pp
);
738 pp_c_attributes_display (pp
, TYPE_ATTRIBUTES (sub
));
742 else if (TREE_CODE (t
) == REFERENCE_TYPE
)
744 if (TYPE_REF_IS_RVALUE (t
))
745 pp_ampersand_ampersand (pp
);
749 pp
->padding
= pp_before
;
750 pp_cxx_cv_qualifier_seq (pp
, t
);
756 dump_type_prefix (pp
, TREE_TYPE (t
), flags
);
757 if (TREE_CODE (t
) == OFFSET_TYPE
) /* pmfs deal with this in d_t_p */
760 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
761 pp_cxx_left_paren (pp
);
762 dump_type (pp
, TYPE_OFFSET_BASETYPE (t
), flags
);
763 pp_cxx_colon_colon (pp
);
766 pp_cxx_cv_qualifier_seq (pp
, t
);
767 pp
->padding
= pp_before
;
770 /* This can be reached without a pointer when dealing with
771 templates, e.g. std::is_function. */
773 dump_type_prefix (pp
, TREE_TYPE (t
), flags
);
777 dump_type_prefix (pp
, TREE_TYPE (t
), flags
);
779 pp_cxx_left_paren (pp
);
780 dump_aggr_type (pp
, TYPE_METHOD_BASETYPE (t
), flags
);
781 pp_cxx_colon_colon (pp
);
785 dump_type_prefix (pp
, TREE_TYPE (t
), flags
);
789 case IDENTIFIER_NODE
:
794 case TEMPLATE_TYPE_PARM
:
795 case TEMPLATE_TEMPLATE_PARM
:
796 case BOUND_TEMPLATE_TEMPLATE_PARM
:
807 case UNDERLYING_TYPE
:
809 case TYPE_PACK_EXPANSION
:
810 case FIXED_POINT_TYPE
:
812 dump_type (pp
, t
, flags
);
813 pp
->padding
= pp_before
;
817 pp_unsupported_tree (pp
, t
);
820 pp_string (pp
, M_("<typeprefixerror>"));
825 /* Dump the suffix of type T, under control of FLAGS. This is the part
826 which appears after the identifier (or function parms). */
829 dump_type_suffix (cxx_pretty_printer
*pp
, tree t
, int flags
)
831 if (TYPE_PTRMEMFUNC_P (t
))
832 t
= TYPE_PTRMEMFUNC_FN_TYPE (t
);
834 switch (TREE_CODE (t
))
839 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
840 || TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
)
841 pp_cxx_right_paren (pp
);
842 if (TREE_CODE (t
) == POINTER_TYPE
)
843 flags
|= TFF_POINTER
;
844 dump_type_suffix (pp
, TREE_TYPE (t
), flags
);
851 if (TREE_CODE (t
) == METHOD_TYPE
)
852 /* Can only be reached through a pointer. */
853 pp_cxx_right_paren (pp
);
854 arg
= TYPE_ARG_TYPES (t
);
855 if (TREE_CODE (t
) == METHOD_TYPE
)
856 arg
= TREE_CHAIN (arg
);
858 /* Function pointers don't have default args. Not in standard C++,
859 anyway; they may in g++, but we'll just pretend otherwise. */
860 dump_parameters (pp
, arg
, flags
& ~TFF_FUNCTION_DEFAULT_ARGUMENTS
);
862 pp
->padding
= pp_before
;
863 pp_cxx_cv_qualifiers (pp
, type_memfn_quals (t
),
864 TREE_CODE (t
) == FUNCTION_TYPE
865 && (flags
& TFF_POINTER
));
866 dump_ref_qualifier (pp
, t
, flags
);
867 dump_exception_spec (pp
, TYPE_RAISES_EXCEPTIONS (t
), flags
);
868 dump_type_suffix (pp
, TREE_TYPE (t
), flags
);
874 pp_cxx_left_bracket (pp
);
877 tree dtype
= TYPE_DOMAIN (t
);
878 tree max
= TYPE_MAX_VALUE (dtype
);
879 if (integer_all_onesp (max
))
880 pp_character (pp
, '0');
881 else if (tree_fits_shwi_p (max
))
882 pp_wide_integer (pp
, tree_to_shwi (max
) + 1);
886 if (TREE_CODE (max
) == SAVE_EXPR
)
887 max
= TREE_OPERAND (max
, 0);
888 if (TREE_CODE (max
) == MINUS_EXPR
889 || TREE_CODE (max
) == PLUS_EXPR
)
891 max
= TREE_OPERAND (max
, 0);
892 while (CONVERT_EXPR_P (max
))
893 max
= TREE_OPERAND (max
, 0);
896 max
= fold_build2_loc (input_location
,
897 PLUS_EXPR
, dtype
, max
,
898 build_int_cst (dtype
, 1));
899 dump_expr (pp
, max
, flags
& ~TFF_EXPR_IN_PARENS
);
902 pp_cxx_right_bracket (pp
);
903 dump_type_suffix (pp
, TREE_TYPE (t
), flags
);
907 case IDENTIFIER_NODE
:
912 case TEMPLATE_TYPE_PARM
:
913 case TEMPLATE_TEMPLATE_PARM
:
914 case BOUND_TEMPLATE_TEMPLATE_PARM
:
925 case UNDERLYING_TYPE
:
927 case TYPE_PACK_EXPANSION
:
928 case FIXED_POINT_TYPE
:
933 pp_unsupported_tree (pp
, t
);
935 /* Don't mark it here, we should have already done in
942 dump_global_iord (cxx_pretty_printer
*pp
, tree t
)
944 const char *p
= NULL
;
946 if (DECL_GLOBAL_CTOR_P (t
))
947 p
= M_("(static initializers for %s)");
948 else if (DECL_GLOBAL_DTOR_P (t
))
949 p
= M_("(static destructors for %s)");
953 pp_printf (pp
, p
, LOCATION_FILE (input_location
));
957 dump_simple_decl (cxx_pretty_printer
*pp
, tree t
, tree type
, int flags
)
959 if (flags
& TFF_DECL_SPECIFIERS
)
962 && DECL_DECLARED_CONSTEXPR_P (t
))
963 pp_cxx_ws_string (pp
, "constexpr");
964 dump_type_prefix (pp
, type
, flags
& ~TFF_UNQUALIFIED_NAME
);
967 if (! (flags
& TFF_UNQUALIFIED_NAME
)
968 && TREE_CODE (t
) != PARM_DECL
969 && (!DECL_INITIAL (t
)
970 || TREE_CODE (DECL_INITIAL (t
)) != TEMPLATE_PARM_INDEX
))
971 dump_scope (pp
, CP_DECL_CONTEXT (t
), flags
);
972 flags
&= ~TFF_UNQUALIFIED_NAME
;
973 if ((flags
& TFF_DECL_SPECIFIERS
)
974 && DECL_TEMPLATE_PARM_P (t
)
975 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (t
)))
976 pp_string (pp
, "...");
979 if (TREE_CODE (t
) == FIELD_DECL
&& DECL_NORMAL_CAPTURE_P (t
))
982 pp_string (pp
, IDENTIFIER_POINTER (DECL_NAME (t
)) + 2);
983 pp_string (pp
, " capture>");
986 dump_decl (pp
, DECL_NAME (t
), flags
);
989 pp_string (pp
, M_("<anonymous>"));
990 if (flags
& TFF_DECL_SPECIFIERS
)
991 dump_type_suffix (pp
, type
, flags
);
994 /* Dump a human readable string for the decl T under control of FLAGS. */
997 dump_decl (cxx_pretty_printer
*pp
, tree t
, int flags
)
1002 /* If doing Objective-C++, give Objective-C a chance to demangle
1003 Objective-C method names. */
1004 if (c_dialect_objc ())
1006 const char *demangled
= objc_maybe_printable_name (t
, flags
);
1009 pp_string (pp
, demangled
);
1014 switch (TREE_CODE (t
))
1017 /* Don't say 'typedef class A' */
1018 if (DECL_ARTIFICIAL (t
) && !DECL_SELF_REFERENCE_P (t
))
1020 if ((flags
& TFF_DECL_SPECIFIERS
)
1021 && TREE_CODE (TREE_TYPE (t
)) == TEMPLATE_TYPE_PARM
)
1023 /* Say `class T' not just `T'. */
1024 pp_cxx_ws_string (pp
, "class");
1026 /* Emit the `...' for a parameter pack. */
1027 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t
)))
1028 pp_cxx_ws_string (pp
, "...");
1031 dump_type (pp
, TREE_TYPE (t
), flags
);
1034 if (TYPE_DECL_ALIAS_P (t
)
1035 && (flags
& TFF_DECL_SPECIFIERS
1036 || flags
& TFF_CLASS_KEY_OR_ENUM
))
1038 pp_cxx_ws_string (pp
, "using");
1039 dump_decl (pp
, DECL_NAME (t
), flags
);
1040 pp_cxx_whitespace (pp
);
1041 pp_cxx_ws_string (pp
, "=");
1042 pp_cxx_whitespace (pp
);
1043 dump_type (pp
, DECL_ORIGINAL_TYPE (t
), flags
);
1046 if ((flags
& TFF_DECL_SPECIFIERS
)
1047 && !DECL_SELF_REFERENCE_P (t
))
1048 pp_cxx_ws_string (pp
, "typedef");
1049 dump_simple_decl (pp
, t
, DECL_ORIGINAL_TYPE (t
)
1050 ? DECL_ORIGINAL_TYPE (t
) : TREE_TYPE (t
),
1055 if (DECL_NAME (t
) && VTABLE_NAME_P (DECL_NAME (t
)))
1057 pp_string (pp
, M_("vtable for "));
1058 gcc_assert (TYPE_P (DECL_CONTEXT (t
)));
1059 dump_type (pp
, DECL_CONTEXT (t
), flags
);
1062 /* Else fall through. */
1065 dump_simple_decl (pp
, t
, TREE_TYPE (t
), flags
);
1067 /* Handle variable template specializations. */
1068 if (TREE_CODE (t
) == VAR_DECL
1069 && DECL_LANG_SPECIFIC (t
)
1070 && DECL_TEMPLATE_INFO (t
)
1071 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)))
1073 pp_cxx_begin_template_argument_list (pp
);
1074 tree args
= INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t
));
1075 dump_template_argument_list (pp
, args
, flags
);
1076 pp_cxx_end_template_argument_list (pp
);
1081 pp_string (pp
, M_("<return value> "));
1082 dump_simple_decl (pp
, t
, TREE_TYPE (t
), flags
);
1085 case NAMESPACE_DECL
:
1086 if (flags
& TFF_DECL_SPECIFIERS
)
1087 pp
->declaration (t
);
1090 if (! (flags
& TFF_UNQUALIFIED_NAME
))
1091 dump_scope (pp
, CP_DECL_CONTEXT (t
), flags
);
1092 flags
&= ~TFF_UNQUALIFIED_NAME
;
1093 if (DECL_NAME (t
) == NULL_TREE
)
1095 if (!(pp
->flags
& pp_c_flag_gnu_v3
))
1096 pp_cxx_ws_string (pp
, M_("{anonymous}"));
1098 pp_cxx_ws_string (pp
, M_("(anonymous namespace)"));
1101 pp_cxx_tree_identifier (pp
, DECL_NAME (t
));
1106 dump_type (pp
, TREE_OPERAND (t
, 0), flags
);
1107 pp_colon_colon (pp
);
1108 dump_decl (pp
, TREE_OPERAND (t
, 1), TFF_UNQUALIFIED_NAME
);
1112 dump_decl (pp
, TREE_OPERAND (t
, 0), flags
);
1113 pp_cxx_left_bracket (pp
);
1114 dump_decl (pp
, TREE_OPERAND (t
, 1), flags
);
1115 pp_cxx_right_bracket (pp
);
1118 case ARRAY_NOTATION_REF
:
1119 dump_decl (pp
, ARRAY_NOTATION_ARRAY (t
), flags
| TFF_EXPR_IN_PARENS
);
1120 pp_cxx_left_bracket (pp
);
1121 dump_decl (pp
, ARRAY_NOTATION_START (t
), flags
| TFF_EXPR_IN_PARENS
);
1123 dump_decl (pp
, ARRAY_NOTATION_LENGTH (t
), flags
| TFF_EXPR_IN_PARENS
);
1125 dump_decl (pp
, ARRAY_NOTATION_STRIDE (t
), flags
| TFF_EXPR_IN_PARENS
);
1126 pp_cxx_right_bracket (pp
);
1129 /* So that we can do dump_decl on an aggr type. */
1133 dump_type (pp
, t
, flags
);
1137 /* This is a pseudo destructor call which has not been folded into
1138 a PSEUDO_DTOR_EXPR yet. */
1139 pp_cxx_complement (pp
);
1140 dump_type (pp
, TREE_OPERAND (t
, 0), flags
);
1147 /* These special cases are duplicated here so that other functions
1148 can feed identifiers to error and get them demangled properly. */
1149 case IDENTIFIER_NODE
:
1150 if (IDENTIFIER_TYPENAME_P (t
))
1152 pp_cxx_ws_string (pp
, "operator");
1153 /* Not exactly IDENTIFIER_TYPE_VALUE. */
1154 dump_type (pp
, TREE_TYPE (t
), flags
);
1158 pp_cxx_tree_identifier (pp
, t
);
1164 t
= OVL_CURRENT (t
);
1165 if (DECL_CLASS_SCOPE_P (t
))
1167 dump_type (pp
, DECL_CONTEXT (t
), flags
);
1168 pp_cxx_colon_colon (pp
);
1170 else if (!DECL_FILE_SCOPE_P (t
))
1172 dump_decl (pp
, DECL_CONTEXT (t
), flags
);
1173 pp_cxx_colon_colon (pp
);
1175 dump_decl (pp
, DECL_NAME (t
), flags
);
1179 /* If there's only one function, just treat it like an ordinary
1181 t
= OVL_CURRENT (t
);
1185 if (! DECL_LANG_SPECIFIC (t
))
1187 if (DECL_ABSTRACT_ORIGIN (t
))
1188 dump_decl (pp
, DECL_ABSTRACT_ORIGIN (t
), flags
);
1190 pp_string (pp
, M_("<built-in>"));
1192 else if (DECL_GLOBAL_CTOR_P (t
) || DECL_GLOBAL_DTOR_P (t
))
1193 dump_global_iord (pp
, t
);
1195 dump_function_decl (pp
, t
, flags
);
1199 dump_template_decl (pp
, t
, flags
);
1202 case TEMPLATE_ID_EXPR
:
1204 tree name
= TREE_OPERAND (t
, 0);
1205 tree args
= TREE_OPERAND (t
, 1);
1207 if (is_overloaded_fn (name
))
1208 name
= get_first_fn (name
);
1210 name
= DECL_NAME (name
);
1211 dump_decl (pp
, name
, flags
);
1212 pp_cxx_begin_template_argument_list (pp
);
1213 if (args
== error_mark_node
)
1214 pp_string (pp
, M_("<template arguments error>"));
1216 dump_template_argument_list (pp
, args
, flags
);
1217 pp_cxx_end_template_argument_list (pp
);
1222 pp_cxx_tree_identifier (pp
, DECL_NAME (t
));
1226 if ((TREE_TYPE (t
) != NULL_TREE
&& NEXT_CODE (t
) == ENUMERAL_TYPE
)
1227 || (DECL_INITIAL (t
) &&
1228 TREE_CODE (DECL_INITIAL (t
)) == TEMPLATE_PARM_INDEX
))
1229 dump_simple_decl (pp
, t
, TREE_TYPE (t
), flags
);
1230 else if (DECL_NAME (t
))
1231 dump_decl (pp
, DECL_NAME (t
), flags
);
1232 else if (DECL_INITIAL (t
))
1233 dump_expr (pp
, DECL_INITIAL (t
), flags
| TFF_EXPR_IN_PARENS
);
1235 pp_string (pp
, M_("<enumerator>"));
1239 pp_cxx_ws_string (pp
, "using");
1240 dump_type (pp
, USING_DECL_SCOPE (t
), flags
);
1241 pp_cxx_colon_colon (pp
);
1242 dump_decl (pp
, DECL_NAME (t
), flags
);
1246 pp
->declaration (t
);
1250 dump_decl (pp
, BASELINK_FUNCTIONS (t
), flags
);
1253 case NON_DEPENDENT_EXPR
:
1254 dump_expr (pp
, t
, flags
);
1257 case TEMPLATE_TYPE_PARM
:
1258 if (flags
& TFF_DECL_SPECIFIERS
)
1259 pp
->declaration (t
);
1264 case UNBOUND_CLASS_TEMPLATE
:
1265 case TYPE_PACK_EXPANSION
:
1267 dump_type (pp
, t
, flags
);
1271 pp_unsupported_tree (pp
, t
);
1272 /* Fall through to error. */
1275 pp_string (pp
, M_("<declaration error>"));
1280 /* Dump a template declaration T under control of FLAGS. This means the
1281 'template <...> leaders plus the 'class X' or 'void fn(...)' part. */
1284 dump_template_decl (cxx_pretty_printer
*pp
, tree t
, int flags
)
1286 tree orig_parms
= DECL_TEMPLATE_PARMS (t
);
1290 if (flags
& TFF_TEMPLATE_HEADER
)
1292 for (parms
= orig_parms
= nreverse (orig_parms
);
1294 parms
= TREE_CHAIN (parms
))
1296 tree inner_parms
= INNERMOST_TEMPLATE_PARMS (parms
);
1297 int len
= TREE_VEC_LENGTH (inner_parms
);
1299 pp_cxx_ws_string (pp
, "template");
1300 pp_cxx_begin_template_argument_list (pp
);
1302 /* If we've shown the template prefix, we'd better show the
1303 parameters' and decl's type too. */
1304 flags
|= TFF_DECL_SPECIFIERS
;
1306 for (i
= 0; i
< len
; i
++)
1309 pp_separate_with_comma (pp
);
1310 dump_template_parameter (pp
, TREE_VEC_ELT (inner_parms
, i
),
1313 pp_cxx_end_template_argument_list (pp
);
1314 pp_cxx_whitespace (pp
);
1316 nreverse(orig_parms
);
1318 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
1320 /* Say `template<arg> class TT' not just `template<arg> TT'. */
1321 pp_cxx_ws_string (pp
, "class");
1323 /* If this is a parameter pack, print the ellipsis. */
1324 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t
)))
1325 pp_cxx_ws_string (pp
, "...");
1329 if (DECL_CLASS_TEMPLATE_P (t
))
1330 dump_type (pp
, TREE_TYPE (t
),
1331 ((flags
& ~TFF_CLASS_KEY_OR_ENUM
) | TFF_TEMPLATE_NAME
1332 | (flags
& TFF_DECL_SPECIFIERS
? TFF_CLASS_KEY_OR_ENUM
: 0)));
1333 else if (DECL_TEMPLATE_RESULT (t
)
1334 && (VAR_P (DECL_TEMPLATE_RESULT (t
))
1335 /* Alias template. */
1336 || DECL_TYPE_TEMPLATE_P (t
)))
1337 dump_decl (pp
, DECL_TEMPLATE_RESULT (t
), flags
| TFF_TEMPLATE_NAME
);
1340 gcc_assert (TREE_TYPE (t
));
1341 switch (NEXT_CODE (t
))
1345 dump_function_decl (pp
, t
, flags
| TFF_TEMPLATE_NAME
);
1348 /* This case can occur with some invalid code. */
1349 dump_type (pp
, TREE_TYPE (t
),
1350 (flags
& ~TFF_CLASS_KEY_OR_ENUM
) | TFF_TEMPLATE_NAME
1351 | (flags
& TFF_DECL_SPECIFIERS
1352 ? TFF_CLASS_KEY_OR_ENUM
: 0));
1357 /* find_typenames looks through the type of the function template T
1358 and returns a vec containing any typedefs, decltypes or TYPENAME_TYPEs
1361 struct find_typenames_t
1363 hash_set
<tree
> *p_set
;
1364 vec
<tree
, va_gc
> *typenames
;
1368 find_typenames_r (tree
*tp
, int *walk_subtrees
, void *data
)
1370 struct find_typenames_t
*d
= (struct find_typenames_t
*)data
;
1371 tree mv
= NULL_TREE
;
1373 if (TYPE_P (*tp
) && is_typedef_decl (TYPE_NAME (*tp
)))
1374 /* Add the type of the typedef without any additional cv-quals. */
1375 mv
= TREE_TYPE (TYPE_NAME (*tp
));
1376 else if (TREE_CODE (*tp
) == TYPENAME_TYPE
1377 || TREE_CODE (*tp
) == DECLTYPE_TYPE
)
1378 /* Add the typename without any cv-qualifiers. */
1379 mv
= TYPE_MAIN_VARIANT (*tp
);
1381 if (TREE_CODE (*tp
) == TYPE_PACK_EXPANSION
)
1383 /* Don't mess with parameter packs since we don't remember
1384 the pack expansion context for a particular typename. */
1385 *walk_subtrees
= false;
1389 if (mv
&& (mv
== *tp
|| !d
->p_set
->add (mv
)))
1390 vec_safe_push (d
->typenames
, mv
);
1392 /* Search into class template arguments, which cp_walk_subtrees
1394 if (CLASS_TYPE_P (*tp
) && CLASSTYPE_TEMPLATE_INFO (*tp
))
1395 cp_walk_tree (&CLASSTYPE_TI_ARGS (*tp
), find_typenames_r
,
1401 static vec
<tree
, va_gc
> *
1402 find_typenames (tree t
)
1404 struct find_typenames_t ft
;
1405 ft
.p_set
= new hash_set
<tree
>;
1406 ft
.typenames
= NULL
;
1407 cp_walk_tree (&TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
1408 find_typenames_r
, &ft
, ft
.p_set
);
1410 return ft
.typenames
;
1413 /* Output the "[with ...]" clause for a template instantiation T iff
1414 TEMPLATE_PARMS, TEMPLATE_ARGS and FLAGS are suitable. T may be NULL if
1415 formatting a deduction/substitution diagnostic rather than an
1419 dump_substitution (cxx_pretty_printer
*pp
,
1420 tree t
, tree template_parms
, tree template_args
,
1423 if (template_parms
!= NULL_TREE
&& template_args
!= NULL_TREE
1424 && !(flags
& TFF_NO_TEMPLATE_BINDINGS
))
1426 vec
<tree
, va_gc
> *typenames
= t
? find_typenames (t
) : NULL
;
1427 pp_cxx_whitespace (pp
);
1428 pp_cxx_left_bracket (pp
);
1429 pp
->translate_string ("with");
1430 pp_cxx_whitespace (pp
);
1431 dump_template_bindings (pp
, template_parms
, template_args
, typenames
);
1432 pp_cxx_right_bracket (pp
);
1436 /* Dump the lambda function FN including its 'mutable' qualifier and any
1437 template bindings. */
1440 dump_lambda_function (cxx_pretty_printer
*pp
,
1441 tree fn
, tree template_parms
, tree template_args
,
1444 /* A lambda's signature is essentially its "type". */
1445 dump_type (pp
, DECL_CONTEXT (fn
), flags
);
1446 if (!(TYPE_QUALS (class_of_this_parm (TREE_TYPE (fn
))) & TYPE_QUAL_CONST
))
1448 pp
->padding
= pp_before
;
1449 pp_c_ws_string (pp
, "mutable");
1451 dump_substitution (pp
, fn
, template_parms
, template_args
, flags
);
1454 /* Pretty print a function decl. There are several ways we want to print a
1455 function declaration. The TFF_ bits in FLAGS tells us how to behave.
1456 As error can only apply the '#' flag once to give 0 and 1 for V, there
1457 is %D which doesn't print the throw specs, and %F which does. */
1460 dump_function_decl (cxx_pretty_printer
*pp
, tree t
, int flags
)
1464 tree cname
= NULL_TREE
;
1465 tree template_args
= NULL_TREE
;
1466 tree template_parms
= NULL_TREE
;
1467 int show_return
= flags
& TFF_RETURN_TYPE
|| flags
& TFF_DECL_SPECIFIERS
;
1468 int do_outer_scope
= ! (flags
& TFF_UNQUALIFIED_NAME
);
1471 flags
&= ~(TFF_UNQUALIFIED_NAME
| TFF_TEMPLATE_NAME
);
1472 if (TREE_CODE (t
) == TEMPLATE_DECL
)
1473 t
= DECL_TEMPLATE_RESULT (t
);
1475 /* Save the exceptions, in case t is a specialization and we are
1476 emitting an error about incompatible specifications. */
1477 exceptions
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (t
));
1479 /* Pretty print template instantiations only. */
1480 if (DECL_USE_TEMPLATE (t
) && DECL_TEMPLATE_INFO (t
)
1481 && flag_pretty_templates
)
1485 template_args
= DECL_TI_ARGS (t
);
1486 tmpl
= most_general_template (t
);
1487 if (tmpl
&& TREE_CODE (tmpl
) == TEMPLATE_DECL
)
1489 template_parms
= DECL_TEMPLATE_PARMS (tmpl
);
1494 if (DECL_NAME (t
) && LAMBDA_FUNCTION_P (t
))
1495 return dump_lambda_function (pp
, t
, template_parms
, template_args
, flags
);
1497 fntype
= TREE_TYPE (t
);
1498 parmtypes
= FUNCTION_FIRST_USER_PARMTYPE (t
);
1500 if (DECL_CLASS_SCOPE_P (t
))
1501 cname
= DECL_CONTEXT (t
);
1502 /* This is for partially instantiated template methods. */
1503 else if (TREE_CODE (fntype
) == METHOD_TYPE
)
1504 cname
= TREE_TYPE (TREE_VALUE (parmtypes
));
1506 if (flags
& TFF_DECL_SPECIFIERS
)
1508 if (DECL_STATIC_FUNCTION_P (t
))
1509 pp_cxx_ws_string (pp
, "static");
1510 else if (DECL_VIRTUAL_P (t
))
1511 pp_cxx_ws_string (pp
, "virtual");
1513 if (DECL_DECLARED_CONSTEXPR_P (t
))
1514 pp_cxx_ws_string (pp
, "constexpr");
1517 /* Print the return type? */
1519 show_return
= !DECL_CONV_FN_P (t
) && !DECL_CONSTRUCTOR_P (t
)
1520 && !DECL_DESTRUCTOR_P (t
);
1523 tree ret
= fndecl_declared_return_type (t
);
1524 dump_type_prefix (pp
, ret
, flags
);
1527 /* Print the function name. */
1528 if (!do_outer_scope
)
1532 dump_type (pp
, cname
, flags
);
1533 pp_cxx_colon_colon (pp
);
1536 dump_scope (pp
, CP_DECL_CONTEXT (t
), flags
);
1538 dump_function_name (pp
, t
, flags
);
1540 if (!(flags
& TFF_NO_FUNCTION_ARGUMENTS
))
1542 dump_parameters (pp
, parmtypes
, flags
);
1544 if (TREE_CODE (fntype
) == METHOD_TYPE
)
1546 pp
->padding
= pp_before
;
1547 pp_cxx_cv_qualifier_seq (pp
, class_of_this_parm (fntype
));
1548 dump_ref_qualifier (pp
, fntype
, flags
);
1551 if (flags
& TFF_EXCEPTION_SPECIFICATION
)
1553 pp
->padding
= pp_before
;
1554 dump_exception_spec (pp
, exceptions
, flags
);
1558 dump_type_suffix (pp
, TREE_TYPE (fntype
), flags
);
1560 dump_substitution (pp
, t
, template_parms
, template_args
, flags
);
1562 else if (template_args
)
1564 bool need_comma
= false;
1566 pp_cxx_begin_template_argument_list (pp
);
1567 template_args
= INNERMOST_TEMPLATE_ARGS (template_args
);
1568 for (i
= 0; i
< TREE_VEC_LENGTH (template_args
); ++i
)
1570 tree arg
= TREE_VEC_ELT (template_args
, i
);
1572 pp_separate_with_comma (pp
);
1573 if (ARGUMENT_PACK_P (arg
))
1574 pp_cxx_left_brace (pp
);
1575 dump_template_argument (pp
, arg
, TFF_PLAIN_IDENTIFIER
);
1576 if (ARGUMENT_PACK_P (arg
))
1577 pp_cxx_right_brace (pp
);
1580 pp_cxx_end_template_argument_list (pp
);
1584 /* Print a parameter list. If this is for a member function, the
1585 member object ptr (and any other hidden args) should have
1586 already been removed. */
1589 dump_parameters (cxx_pretty_printer
*pp
, tree parmtypes
, int flags
)
1592 flags
&= ~TFF_SCOPE
;
1593 pp_cxx_left_paren (pp
);
1595 for (first
= 1; parmtypes
!= void_list_node
;
1596 parmtypes
= TREE_CHAIN (parmtypes
))
1599 pp_separate_with_comma (pp
);
1603 pp_cxx_ws_string (pp
, "...");
1607 dump_type (pp
, TREE_VALUE (parmtypes
), flags
);
1609 if ((flags
& TFF_FUNCTION_DEFAULT_ARGUMENTS
) && TREE_PURPOSE (parmtypes
))
1611 pp_cxx_whitespace (pp
);
1613 pp_cxx_whitespace (pp
);
1614 dump_expr (pp
, TREE_PURPOSE (parmtypes
), flags
| TFF_EXPR_IN_PARENS
);
1618 pp_cxx_right_paren (pp
);
1621 /* Print ref-qualifier of a FUNCTION_TYPE or METHOD_TYPE. FLAGS are ignored. */
1624 dump_ref_qualifier (cxx_pretty_printer
*pp
, tree t
, int flags ATTRIBUTE_UNUSED
)
1626 if (FUNCTION_REF_QUALIFIED (t
))
1628 pp
->padding
= pp_before
;
1629 if (FUNCTION_RVALUE_QUALIFIED (t
))
1630 pp_cxx_ws_string (pp
, "&&");
1632 pp_cxx_ws_string (pp
, "&");
1636 /* Print an exception specification. T is the exception specification. */
1639 dump_exception_spec (cxx_pretty_printer
*pp
, tree t
, int flags
)
1641 if (t
&& TREE_PURPOSE (t
))
1643 pp_cxx_ws_string (pp
, "noexcept");
1644 if (!integer_onep (TREE_PURPOSE (t
)))
1646 pp_cxx_whitespace (pp
);
1647 pp_cxx_left_paren (pp
);
1648 if (DEFERRED_NOEXCEPT_SPEC_P (t
))
1649 pp_cxx_ws_string (pp
, "<uninstantiated>");
1651 dump_expr (pp
, TREE_PURPOSE (t
), flags
);
1652 pp_cxx_right_paren (pp
);
1657 pp_cxx_ws_string (pp
, "throw");
1658 pp_cxx_whitespace (pp
);
1659 pp_cxx_left_paren (pp
);
1660 if (TREE_VALUE (t
) != NULL_TREE
)
1663 dump_type (pp
, TREE_VALUE (t
), flags
);
1667 pp_separate_with_comma (pp
);
1669 pp_cxx_right_paren (pp
);
1673 /* Handle the function name for a FUNCTION_DECL node, grokking operators
1674 and destructors properly. */
1677 dump_function_name (cxx_pretty_printer
*pp
, tree t
, int flags
)
1679 tree name
= DECL_NAME (t
);
1681 /* We can get here with a decl that was synthesized by language-
1682 independent machinery (e.g. coverage.c) in which case it won't
1683 have a lang_specific structure attached and DECL_CONSTRUCTOR_P
1684 will crash. In this case it is safe just to print out the
1686 if (!DECL_LANG_SPECIFIC (t
))
1688 pp_cxx_tree_identifier (pp
, name
);
1692 if (TREE_CODE (t
) == TEMPLATE_DECL
)
1693 t
= DECL_TEMPLATE_RESULT (t
);
1695 /* Don't let the user see __comp_ctor et al. */
1696 if (DECL_CONSTRUCTOR_P (t
)
1697 || DECL_DESTRUCTOR_P (t
))
1699 if (LAMBDA_TYPE_P (DECL_CONTEXT (t
)))
1700 name
= get_identifier ("<lambda>");
1701 else if (TYPE_ANONYMOUS_P (DECL_CONTEXT (t
)))
1702 name
= get_identifier ("<constructor>");
1704 name
= constructor_name (DECL_CONTEXT (t
));
1707 if (DECL_DESTRUCTOR_P (t
))
1709 pp_cxx_complement (pp
);
1710 dump_decl (pp
, name
, TFF_PLAIN_IDENTIFIER
);
1712 else if (DECL_CONV_FN_P (t
))
1714 /* This cannot use the hack that the operator's return
1715 type is stashed off of its name because it may be
1716 used for error reporting. In the case of conflicting
1717 declarations, both will have the same name, yet
1718 the types will be different, hence the TREE_TYPE field
1719 of the first name will be clobbered by the second. */
1720 pp_cxx_ws_string (pp
, "operator");
1721 dump_type (pp
, TREE_TYPE (TREE_TYPE (t
)), flags
);
1723 else if (name
&& IDENTIFIER_OPNAME_P (name
))
1724 pp_cxx_tree_identifier (pp
, name
);
1725 else if (name
&& UDLIT_OPER_P (name
))
1726 pp_cxx_tree_identifier (pp
, name
);
1728 dump_decl (pp
, name
, flags
);
1730 if (DECL_TEMPLATE_INFO (t
)
1731 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (t
)
1732 && (TREE_CODE (DECL_TI_TEMPLATE (t
)) != TEMPLATE_DECL
1733 || PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
1734 dump_template_parms (pp
, DECL_TEMPLATE_INFO (t
), !DECL_USE_TEMPLATE (t
),
1738 /* Dump the template parameters from the template info INFO under control of
1739 FLAGS. PRIMARY indicates whether this is a primary template decl, or
1740 specialization (partial or complete). For partial specializations we show
1741 the specialized parameter values. For a primary template we show no
1745 dump_template_parms (cxx_pretty_printer
*pp
, tree info
,
1746 int primary
, int flags
)
1748 tree args
= info
? TI_ARGS (info
) : NULL_TREE
;
1750 if (primary
&& flags
& TFF_TEMPLATE_NAME
)
1752 flags
&= ~(TFF_CLASS_KEY_OR_ENUM
| TFF_TEMPLATE_NAME
);
1753 pp_cxx_begin_template_argument_list (pp
);
1755 /* Be careful only to print things when we have them, so as not
1756 to crash producing error messages. */
1757 if (args
&& !primary
)
1760 len
= get_non_default_template_args_count (args
, flags
);
1762 args
= INNERMOST_TEMPLATE_ARGS (args
);
1763 for (ix
= 0; ix
!= len
; ix
++)
1765 tree arg
= TREE_VEC_ELT (args
, ix
);
1767 /* Only print a comma if we know there is an argument coming. In
1768 the case of an empty template argument pack, no actual
1769 argument will be printed. */
1771 && (!ARGUMENT_PACK_P (arg
)
1772 || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) > 0))
1773 pp_separate_with_comma (pp
);
1776 pp_string (pp
, M_("<template parameter error>"));
1778 dump_template_argument (pp
, arg
, flags
);
1783 tree tpl
= TI_TEMPLATE (info
);
1784 tree parms
= DECL_TEMPLATE_PARMS (tpl
);
1787 parms
= TREE_CODE (parms
) == TREE_LIST
? TREE_VALUE (parms
) : NULL_TREE
;
1788 len
= parms
? TREE_VEC_LENGTH (parms
) : 0;
1790 for (ix
= 0; ix
!= len
; ix
++)
1794 if (TREE_VEC_ELT (parms
, ix
) == error_mark_node
)
1796 pp_string (pp
, M_("<template parameter error>"));
1800 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, ix
));
1803 pp_separate_with_comma (pp
);
1805 dump_decl (pp
, parm
, flags
& ~TFF_DECL_SPECIFIERS
);
1808 pp_cxx_end_template_argument_list (pp
);
1811 /* Print out the arguments of CALL_EXPR T as a parenthesized list using
1812 flags FLAGS. Skip over the first argument if SKIPFIRST is true. */
1815 dump_call_expr_args (cxx_pretty_printer
*pp
, tree t
, int flags
, bool skipfirst
)
1818 call_expr_arg_iterator iter
;
1820 pp_cxx_left_paren (pp
);
1821 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, t
)
1827 dump_expr (pp
, arg
, flags
| TFF_EXPR_IN_PARENS
);
1828 if (more_call_expr_args_p (&iter
))
1829 pp_separate_with_comma (pp
);
1832 pp_cxx_right_paren (pp
);
1835 /* Print out the arguments of AGGR_INIT_EXPR T as a parenthesized list
1836 using flags FLAGS. Skip over the first argument if SKIPFIRST is
1840 dump_aggr_init_expr_args (cxx_pretty_printer
*pp
, tree t
, int flags
,
1844 aggr_init_expr_arg_iterator iter
;
1846 pp_cxx_left_paren (pp
);
1847 FOR_EACH_AGGR_INIT_EXPR_ARG (arg
, iter
, t
)
1853 dump_expr (pp
, arg
, flags
| TFF_EXPR_IN_PARENS
);
1854 if (more_aggr_init_expr_args_p (&iter
))
1855 pp_separate_with_comma (pp
);
1858 pp_cxx_right_paren (pp
);
1861 /* Print out a list of initializers (subr of dump_expr). */
1864 dump_expr_list (cxx_pretty_printer
*pp
, tree l
, int flags
)
1868 dump_expr (pp
, TREE_VALUE (l
), flags
| TFF_EXPR_IN_PARENS
);
1871 pp_separate_with_comma (pp
);
1875 /* Print out a vector of initializers (subr of dump_expr). */
1878 dump_expr_init_vec (cxx_pretty_printer
*pp
, vec
<constructor_elt
, va_gc
> *v
,
1881 unsigned HOST_WIDE_INT idx
;
1884 FOR_EACH_CONSTRUCTOR_VALUE (v
, idx
, value
)
1886 dump_expr (pp
, value
, flags
| TFF_EXPR_IN_PARENS
);
1887 if (idx
!= v
->length () - 1)
1888 pp_separate_with_comma (pp
);
1893 /* We've gotten an indirect REFERENCE (an OBJ_TYPE_REF) to a virtual
1894 function. Resolve it to a close relative -- in the sense of static
1895 type -- variant being overridden. That is close to what was written in
1896 the source code. Subroutine of dump_expr. */
1899 resolve_virtual_fun_from_obj_type_ref (tree ref
)
1901 tree obj_type
= TREE_TYPE (OBJ_TYPE_REF_OBJECT (ref
));
1902 HOST_WIDE_INT index
= tree_to_uhwi (OBJ_TYPE_REF_TOKEN (ref
));
1903 tree fun
= BINFO_VIRTUALS (TYPE_BINFO (TREE_TYPE (obj_type
)));
1906 fun
= TREE_CHAIN (fun
);
1907 index
-= (TARGET_VTABLE_USES_DESCRIPTORS
1908 ? TARGET_VTABLE_USES_DESCRIPTORS
: 1);
1914 /* Print out an expression E under control of FLAGS. */
1917 dump_expr (cxx_pretty_printer
*pp
, tree t
, int flags
)
1924 if (STATEMENT_CLASS_P (t
))
1926 pp_cxx_ws_string (pp
, M_("<statement>"));
1930 switch (TREE_CODE (t
))
1938 case NAMESPACE_DECL
:
1942 case IDENTIFIER_NODE
:
1943 dump_decl (pp
, t
, ((flags
& ~(TFF_DECL_SPECIFIERS
|TFF_RETURN_TYPE
1944 |TFF_TEMPLATE_HEADER
))
1945 | TFF_NO_FUNCTION_ARGUMENTS
));
1949 if (SSA_NAME_VAR (t
)
1950 && !DECL_ARTIFICIAL (SSA_NAME_VAR (t
)))
1951 dump_expr (pp
, SSA_NAME_VAR (t
), flags
);
1953 pp_cxx_ws_string (pp
, M_("<unknown>"));
1964 case USERDEF_LITERAL
:
1965 pp_cxx_userdef_literal (pp
, t
);
1969 /* While waiting for caret diagnostics, avoid printing
1970 __cxa_allocate_exception, __cxa_throw, and the like. */
1971 pp_cxx_ws_string (pp
, M_("<throw-expression>"));
1976 dump_type (pp
, PTRMEM_CST_CLASS (t
), flags
);
1977 pp_cxx_colon_colon (pp
);
1978 pp_cxx_tree_identifier (pp
, DECL_NAME (PTRMEM_CST_MEMBER (t
)));
1982 pp_cxx_left_paren (pp
);
1983 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
| TFF_EXPR_IN_PARENS
);
1984 pp_separate_with_comma (pp
);
1985 dump_expr (pp
, TREE_OPERAND (t
, 1), flags
| TFF_EXPR_IN_PARENS
);
1986 pp_cxx_right_paren (pp
);
1990 pp_cxx_left_paren (pp
);
1991 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
| TFF_EXPR_IN_PARENS
);
1992 pp_string (pp
, " ? ");
1993 dump_expr (pp
, TREE_OPERAND (t
, 1), flags
| TFF_EXPR_IN_PARENS
);
1994 pp_string (pp
, " : ");
1995 dump_expr (pp
, TREE_OPERAND (t
, 2), flags
| TFF_EXPR_IN_PARENS
);
1996 pp_cxx_right_paren (pp
);
2000 if (TREE_HAS_CONSTRUCTOR (t
))
2002 pp_cxx_ws_string (pp
, "new");
2003 pp_cxx_whitespace (pp
);
2004 dump_type (pp
, TREE_TYPE (TREE_TYPE (t
)), flags
);
2007 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
| TFF_EXPR_IN_PARENS
);
2010 case AGGR_INIT_EXPR
:
2012 tree fn
= NULL_TREE
;
2014 if (TREE_CODE (AGGR_INIT_EXPR_FN (t
)) == ADDR_EXPR
)
2015 fn
= TREE_OPERAND (AGGR_INIT_EXPR_FN (t
), 0);
2017 if (fn
&& TREE_CODE (fn
) == FUNCTION_DECL
)
2019 if (DECL_CONSTRUCTOR_P (fn
))
2020 dump_type (pp
, DECL_CONTEXT (fn
), flags
);
2022 dump_decl (pp
, fn
, 0);
2025 dump_expr (pp
, AGGR_INIT_EXPR_FN (t
), 0);
2027 dump_aggr_init_expr_args (pp
, t
, flags
, true);
2032 tree fn
= CALL_EXPR_FN (t
);
2033 bool skipfirst
= false;
2035 /* Deal with internal functions. */
2036 if (fn
== NULL_TREE
)
2038 pp_string (pp
, internal_fn_name (CALL_EXPR_IFN (t
)));
2039 dump_call_expr_args (pp
, t
, flags
, skipfirst
);
2043 if (TREE_CODE (fn
) == ADDR_EXPR
)
2044 fn
= TREE_OPERAND (fn
, 0);
2046 /* Nobody is interested in seeing the guts of vcalls. */
2047 if (TREE_CODE (fn
) == OBJ_TYPE_REF
)
2048 fn
= resolve_virtual_fun_from_obj_type_ref (fn
);
2050 if (TREE_TYPE (fn
) != NULL_TREE
2051 && NEXT_CODE (fn
) == METHOD_TYPE
2052 && call_expr_nargs (t
))
2054 tree ob
= CALL_EXPR_ARG (t
, 0);
2055 if (TREE_CODE (ob
) == ADDR_EXPR
)
2057 dump_expr (pp
, TREE_OPERAND (ob
, 0),
2058 flags
| TFF_EXPR_IN_PARENS
);
2061 else if (TREE_CODE (ob
) != PARM_DECL
2062 || strcmp (IDENTIFIER_POINTER (DECL_NAME (ob
)), "this"))
2064 dump_expr (pp
, ob
, flags
| TFF_EXPR_IN_PARENS
);
2069 if (flag_sanitize
& SANITIZE_UNDEFINED
2070 && is_ubsan_builtin_p (fn
))
2072 pp_string (cxx_pp
, M_("<ubsan routine call>"));
2075 dump_expr (pp
, fn
, flags
| TFF_EXPR_IN_PARENS
);
2076 dump_call_expr_args (pp
, t
, flags
, skipfirst
);
2081 /* Note that this only works for G++ target exprs. If somebody
2082 builds a general TARGET_EXPR, there's no way to represent that
2083 it initializes anything other that the parameter slot for the
2084 default argument. Note we may have cleared out the first
2085 operand in expand_expr, so don't go killing ourselves. */
2086 if (TREE_OPERAND (t
, 1))
2087 dump_expr (pp
, TREE_OPERAND (t
, 1), flags
| TFF_EXPR_IN_PARENS
);
2090 case POINTER_PLUS_EXPR
:
2091 dump_binary_op (pp
, "+", t
, flags
);
2096 dump_binary_op (pp
, assignment_operator_name_info
[NOP_EXPR
].name
,
2103 case TRUNC_DIV_EXPR
:
2104 case TRUNC_MOD_EXPR
:
2112 case TRUTH_ANDIF_EXPR
:
2113 case TRUTH_ORIF_EXPR
:
2120 case EXACT_DIV_EXPR
:
2121 dump_binary_op (pp
, operator_name_info
[TREE_CODE (t
)].name
, t
, flags
);
2125 case FLOOR_DIV_EXPR
:
2126 case ROUND_DIV_EXPR
:
2128 dump_binary_op (pp
, "/", t
, flags
);
2132 case FLOOR_MOD_EXPR
:
2133 case ROUND_MOD_EXPR
:
2134 dump_binary_op (pp
, "%", t
, flags
);
2139 tree ob
= TREE_OPERAND (t
, 0);
2140 if (INDIRECT_REF_P (ob
))
2142 ob
= TREE_OPERAND (ob
, 0);
2143 if (TREE_CODE (ob
) != PARM_DECL
2145 && strcmp (IDENTIFIER_POINTER (DECL_NAME (ob
)), "this")))
2147 dump_expr (pp
, ob
, flags
| TFF_EXPR_IN_PARENS
);
2148 if (TREE_CODE (TREE_TYPE (ob
)) == REFERENCE_TYPE
)
2156 dump_expr (pp
, ob
, flags
| TFF_EXPR_IN_PARENS
);
2159 dump_expr (pp
, TREE_OPERAND (t
, 1), flags
& ~TFF_EXPR_IN_PARENS
);
2164 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
| TFF_EXPR_IN_PARENS
);
2165 pp_cxx_left_bracket (pp
);
2166 dump_expr (pp
, TREE_OPERAND (t
, 1), flags
| TFF_EXPR_IN_PARENS
);
2167 pp_cxx_right_bracket (pp
);
2170 case ARRAY_NOTATION_REF
:
2171 dump_expr (pp
, ARRAY_NOTATION_ARRAY (t
), flags
| TFF_EXPR_IN_PARENS
);
2172 pp_cxx_left_bracket (pp
);
2173 dump_expr (pp
, ARRAY_NOTATION_START (t
), flags
| TFF_EXPR_IN_PARENS
);
2175 dump_expr (pp
, ARRAY_NOTATION_LENGTH (t
), flags
| TFF_EXPR_IN_PARENS
);
2177 dump_expr (pp
, ARRAY_NOTATION_STRIDE (t
), flags
| TFF_EXPR_IN_PARENS
);
2178 pp_cxx_right_bracket (pp
);
2181 case UNARY_PLUS_EXPR
:
2182 dump_unary_op (pp
, "+", t
, flags
);
2186 if (TREE_CODE (TREE_OPERAND (t
, 0)) == FUNCTION_DECL
2187 || TREE_CODE (TREE_OPERAND (t
, 0)) == STRING_CST
2188 /* An ADDR_EXPR can have reference type. In that case, we
2189 shouldn't print the `&' doing so indicates to the user
2190 that the expression has pointer type. */
2192 && TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
))
2193 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
| TFF_EXPR_IN_PARENS
);
2194 else if (TREE_CODE (TREE_OPERAND (t
, 0)) == LABEL_DECL
)
2195 dump_unary_op (pp
, "&&", t
, flags
);
2197 dump_unary_op (pp
, "&", t
, flags
);
2201 if (TREE_HAS_CONSTRUCTOR (t
))
2203 t
= TREE_OPERAND (t
, 0);
2204 gcc_assert (TREE_CODE (t
) == CALL_EXPR
);
2205 dump_expr (pp
, CALL_EXPR_FN (t
), flags
| TFF_EXPR_IN_PARENS
);
2206 dump_call_expr_args (pp
, t
, flags
, true);
2210 if (TREE_OPERAND (t
,0) != NULL_TREE
2211 && TREE_TYPE (TREE_OPERAND (t
, 0))
2212 && NEXT_CODE (TREE_OPERAND (t
, 0)) == REFERENCE_TYPE
)
2213 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
);
2215 dump_unary_op (pp
, "*", t
, flags
);
2220 if (TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
2221 && integer_zerop (TREE_OPERAND (t
, 1)))
2222 dump_expr (pp
, TREE_OPERAND (TREE_OPERAND (t
, 0), 0), flags
);
2226 if (!integer_zerop (TREE_OPERAND (t
, 1)))
2228 pp_cxx_left_paren (pp
);
2229 if (!integer_onep (TYPE_SIZE_UNIT
2230 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (t
, 0))))))
2232 pp_cxx_left_paren (pp
);
2233 dump_type (pp
, ptr_type_node
, flags
);
2234 pp_cxx_right_paren (pp
);
2237 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
);
2238 if (!integer_zerop (TREE_OPERAND (t
, 1)))
2240 pp_cxx_ws_string (pp
, "+");
2241 dump_expr (pp
, fold_convert (ssizetype
, TREE_OPERAND (t
, 1)),
2243 pp_cxx_right_paren (pp
);
2250 case TRUTH_NOT_EXPR
:
2251 case PREDECREMENT_EXPR
:
2252 case PREINCREMENT_EXPR
:
2253 dump_unary_op (pp
, operator_name_info
[TREE_CODE (t
)].name
, t
, flags
);
2256 case POSTDECREMENT_EXPR
:
2257 case POSTINCREMENT_EXPR
:
2258 pp_cxx_left_paren (pp
);
2259 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
| TFF_EXPR_IN_PARENS
);
2260 pp_cxx_ws_string (pp
, operator_name_info
[TREE_CODE (t
)].name
);
2261 pp_cxx_right_paren (pp
);
2264 case NON_LVALUE_EXPR
:
2265 /* FIXME: This is a KLUDGE workaround for a parsing problem. There
2266 should be another level of INDIRECT_REF so that I don't have to do
2268 if (TREE_TYPE (t
) != NULL_TREE
&& NEXT_CODE (t
) == POINTER_TYPE
)
2270 tree next
= TREE_TYPE (TREE_TYPE (t
));
2272 while (TYPE_PTR_P (next
))
2273 next
= TREE_TYPE (next
);
2275 if (TREE_CODE (next
) == FUNCTION_TYPE
)
2277 if (flags
& TFF_EXPR_IN_PARENS
)
2278 pp_cxx_left_paren (pp
);
2280 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
& ~TFF_EXPR_IN_PARENS
);
2281 if (flags
& TFF_EXPR_IN_PARENS
)
2282 pp_cxx_right_paren (pp
);
2285 /* Else fall through. */
2287 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
| TFF_EXPR_IN_PARENS
);
2291 case IMPLICIT_CONV_EXPR
:
2292 case VIEW_CONVERT_EXPR
:
2294 tree op
= TREE_OPERAND (t
, 0);
2295 tree ttype
= TREE_TYPE (t
);
2296 tree optype
= TREE_TYPE (op
);
2298 if (TREE_CODE (ttype
) != TREE_CODE (optype
)
2299 && POINTER_TYPE_P (ttype
)
2300 && POINTER_TYPE_P (optype
)
2301 && same_type_p (TREE_TYPE (optype
),
2304 if (TREE_CODE (ttype
) == REFERENCE_TYPE
)
2307 if (TREE_CODE (op
) == ADDR_EXPR
)
2308 dump_expr (pp
, TREE_OPERAND (op
, 0), flags
);
2310 dump_unary_op (pp
, "*", t
, flags
);
2313 dump_unary_op (pp
, "&", t
, flags
);
2315 else if (!same_type_p (TREE_TYPE (op
), TREE_TYPE (t
)))
2317 /* It is a cast, but we cannot tell whether it is a
2318 reinterpret or static cast. Use the C style notation. */
2319 if (flags
& TFF_EXPR_IN_PARENS
)
2320 pp_cxx_left_paren (pp
);
2321 pp_cxx_left_paren (pp
);
2322 dump_type (pp
, TREE_TYPE (t
), flags
);
2323 pp_cxx_right_paren (pp
);
2324 dump_expr (pp
, op
, flags
| TFF_EXPR_IN_PARENS
);
2325 if (flags
& TFF_EXPR_IN_PARENS
)
2326 pp_cxx_right_paren (pp
);
2329 dump_expr (pp
, op
, flags
);
2334 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
)))
2336 tree idx
= build_ptrmemfunc_access_expr (t
, pfn_identifier
);
2338 if (integer_zerop (idx
))
2340 /* A NULL pointer-to-member constant. */
2341 pp_cxx_left_paren (pp
);
2342 pp_cxx_left_paren (pp
);
2343 dump_type (pp
, TREE_TYPE (t
), flags
);
2344 pp_cxx_right_paren (pp
);
2345 pp_character (pp
, '0');
2346 pp_cxx_right_paren (pp
);
2349 else if (tree_fits_shwi_p (idx
))
2352 unsigned HOST_WIDE_INT n
;
2354 t
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
2355 t
= TYPE_METHOD_BASETYPE (t
);
2356 virtuals
= BINFO_VIRTUALS (TYPE_BINFO (TYPE_MAIN_VARIANT (t
)));
2358 n
= tree_to_shwi (idx
);
2360 /* Map vtable index back one, to allow for the null pointer to
2364 while (n
> 0 && virtuals
)
2367 virtuals
= TREE_CHAIN (virtuals
);
2371 dump_expr (pp
, BV_FN (virtuals
),
2372 flags
| TFF_EXPR_IN_PARENS
);
2377 if (TREE_TYPE (t
) && LAMBDA_TYPE_P (TREE_TYPE (t
)))
2378 pp_string (pp
, "<lambda closure object>");
2379 if (TREE_TYPE (t
) && EMPTY_CONSTRUCTOR_P (t
))
2381 dump_type (pp
, TREE_TYPE (t
), 0);
2382 pp_cxx_left_paren (pp
);
2383 pp_cxx_right_paren (pp
);
2387 if (!BRACE_ENCLOSED_INITIALIZER_P (t
))
2388 dump_type (pp
, TREE_TYPE (t
), 0);
2389 pp_cxx_left_brace (pp
);
2390 dump_expr_init_vec (pp
, CONSTRUCTOR_ELTS (t
), flags
);
2391 pp_cxx_right_brace (pp
);
2398 tree ob
= TREE_OPERAND (t
, 0);
2399 if (is_dummy_object (ob
))
2401 t
= TREE_OPERAND (t
, 1);
2402 if (TREE_CODE (t
) == FUNCTION_DECL
)
2404 dump_expr (pp
, t
, flags
| TFF_EXPR_IN_PARENS
);
2405 else if (BASELINK_P (t
))
2406 dump_expr (pp
, OVL_CURRENT (BASELINK_FUNCTIONS (t
)),
2407 flags
| TFF_EXPR_IN_PARENS
);
2409 dump_decl (pp
, t
, flags
);
2413 if (INDIRECT_REF_P (ob
))
2415 dump_expr (pp
, TREE_OPERAND (ob
, 0), flags
| TFF_EXPR_IN_PARENS
);
2421 dump_expr (pp
, ob
, flags
| TFF_EXPR_IN_PARENS
);
2425 dump_expr (pp
, TREE_OPERAND (t
, 1), flags
| TFF_EXPR_IN_PARENS
);
2430 case TEMPLATE_PARM_INDEX
:
2431 dump_decl (pp
, TEMPLATE_PARM_DECL (t
), flags
& ~TFF_DECL_SPECIFIERS
);
2435 if (TREE_OPERAND (t
, 0) == NULL_TREE
2436 || TREE_CHAIN (TREE_OPERAND (t
, 0)))
2438 dump_type (pp
, TREE_TYPE (t
), flags
);
2439 pp_cxx_left_paren (pp
);
2440 dump_expr_list (pp
, TREE_OPERAND (t
, 0), flags
);
2441 pp_cxx_right_paren (pp
);
2445 pp_cxx_left_paren (pp
);
2446 dump_type (pp
, TREE_TYPE (t
), flags
);
2447 pp_cxx_right_paren (pp
);
2448 pp_cxx_left_paren (pp
);
2449 dump_expr_list (pp
, TREE_OPERAND (t
, 0), flags
);
2450 pp_cxx_right_paren (pp
);
2454 case STATIC_CAST_EXPR
:
2455 pp_cxx_ws_string (pp
, "static_cast");
2457 case REINTERPRET_CAST_EXPR
:
2458 pp_cxx_ws_string (pp
, "reinterpret_cast");
2460 case CONST_CAST_EXPR
:
2461 pp_cxx_ws_string (pp
, "const_cast");
2463 case DYNAMIC_CAST_EXPR
:
2464 pp_cxx_ws_string (pp
, "dynamic_cast");
2466 pp_cxx_begin_template_argument_list (pp
);
2467 dump_type (pp
, TREE_TYPE (t
), flags
);
2468 pp_cxx_end_template_argument_list (pp
);
2469 pp_cxx_left_paren (pp
);
2470 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
);
2471 pp_cxx_right_paren (pp
);
2475 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
);
2481 if (TREE_CODE (t
) == SIZEOF_EXPR
)
2482 pp_cxx_ws_string (pp
, "sizeof");
2485 gcc_assert (TREE_CODE (t
) == ALIGNOF_EXPR
);
2486 pp_cxx_ws_string (pp
, "__alignof__");
2488 op
= TREE_OPERAND (t
, 0);
2489 if (PACK_EXPANSION_P (op
))
2491 pp_string (pp
, "...");
2492 op
= PACK_EXPANSION_PATTERN (op
);
2494 pp_cxx_whitespace (pp
);
2495 pp_cxx_left_paren (pp
);
2496 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
2497 dump_type (pp
, TREE_TYPE (op
), flags
);
2498 else if (TYPE_P (TREE_OPERAND (t
, 0)))
2499 dump_type (pp
, op
, flags
);
2501 dump_expr (pp
, op
, flags
);
2502 pp_cxx_right_paren (pp
);
2505 case AT_ENCODE_EXPR
:
2506 pp_cxx_ws_string (pp
, "@encode");
2507 pp_cxx_whitespace (pp
);
2508 pp_cxx_left_paren (pp
);
2509 dump_type (pp
, TREE_OPERAND (t
, 0), flags
);
2510 pp_cxx_right_paren (pp
);
2514 pp_cxx_ws_string (pp
, "noexcept");
2515 pp_cxx_whitespace (pp
);
2516 pp_cxx_left_paren (pp
);
2517 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
);
2518 pp_cxx_right_paren (pp
);
2523 pp_cxx_ws_string (pp
, operator_name_info
[TREE_CODE (t
)].name
);
2524 pp_cxx_whitespace (pp
);
2525 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
);
2529 pp_string (pp
, M_("<unparsed>"));
2532 case TRY_CATCH_EXPR
:
2533 case WITH_CLEANUP_EXPR
:
2534 case CLEANUP_POINT_EXPR
:
2535 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
);
2538 case PSEUDO_DTOR_EXPR
:
2539 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
);
2541 if (TREE_OPERAND (t
, 1))
2543 dump_type (pp
, TREE_OPERAND (t
, 1), flags
);
2544 pp_cxx_colon_colon (pp
);
2546 pp_cxx_complement (pp
);
2547 dump_type (pp
, TREE_OPERAND (t
, 2), flags
);
2550 case TEMPLATE_ID_EXPR
:
2551 dump_decl (pp
, t
, flags
);
2557 case STATEMENT_LIST
:
2558 /* We don't yet have a way of dumping statements in a
2559 human-readable format. */
2560 pp_string (pp
, "({...})");
2564 pp_string (pp
, "while (1) { ");
2565 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
& ~TFF_EXPR_IN_PARENS
);
2566 pp_cxx_right_brace (pp
);
2570 pp_string (pp
, "if (");
2571 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
& ~TFF_EXPR_IN_PARENS
);
2572 pp_string (pp
, ") break; ");
2576 dump_expr (pp
, BASELINK_FUNCTIONS (t
), flags
& ~TFF_EXPR_IN_PARENS
);
2579 case EMPTY_CLASS_EXPR
:
2580 dump_type (pp
, TREE_TYPE (t
), flags
);
2581 pp_cxx_left_paren (pp
);
2582 pp_cxx_right_paren (pp
);
2585 case NON_DEPENDENT_EXPR
:
2586 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
);
2589 case ARGUMENT_PACK_SELECT
:
2590 dump_template_argument (pp
, ARGUMENT_PACK_SELECT_FROM_PACK (t
), flags
);
2602 pp_type_specifier_seq (pp
, t
);
2606 /* We get here when we want to print a dependent type as an
2607 id-expression, without any disambiguator decoration. */
2608 pp
->id_expression (t
);
2611 case TEMPLATE_TYPE_PARM
:
2612 case TEMPLATE_TEMPLATE_PARM
:
2613 case BOUND_TEMPLATE_TEMPLATE_PARM
:
2614 dump_type (pp
, t
, flags
);
2618 pp_cxx_trait_expression (pp
, t
);
2622 pp_cxx_va_arg_expression (pp
, t
);
2626 pp_cxx_offsetof_expression (pp
, t
);
2630 dump_decl (pp
, t
, flags
);
2633 case EXPR_PACK_EXPANSION
:
2640 case VEC_DELETE_EXPR
:
2646 case UNORDERED_EXPR
:
2656 case FIX_TRUNC_EXPR
:
2661 case TRUTH_AND_EXPR
:
2663 case TRUTH_XOR_EXPR
:
2664 if (flags
& TFF_EXPR_IN_PARENS
)
2665 pp_cxx_left_paren (pp
);
2667 if (flags
& TFF_EXPR_IN_PARENS
)
2668 pp_cxx_right_paren (pp
);
2672 dump_expr (pp
, resolve_virtual_fun_from_obj_type_ref (t
), flags
);
2676 pp_string (pp
, M_("<lambda>"));
2680 pp_cxx_left_paren (pp
);
2681 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
| TFF_EXPR_IN_PARENS
);
2682 pp_cxx_right_paren (pp
);
2685 case PLACEHOLDER_EXPR
:
2686 pp_string (pp
, M_("*this"));
2689 /* This list is incomplete, but should suffice for now.
2690 It is very important that `sorry' does not call
2691 `report_error_function'. That could cause an infinite loop. */
2693 pp_unsupported_tree (pp
, t
);
2694 /* fall through to ERROR_MARK... */
2696 pp_string (pp
, M_("<expression error>"));
2702 dump_binary_op (cxx_pretty_printer
*pp
, const char *opstring
, tree t
,
2705 pp_cxx_left_paren (pp
);
2706 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
| TFF_EXPR_IN_PARENS
);
2707 pp_cxx_whitespace (pp
);
2709 pp_cxx_ws_string (pp
, opstring
);
2711 pp_string (pp
, M_("<unknown operator>"));
2712 pp_cxx_whitespace (pp
);
2713 dump_expr (pp
, TREE_OPERAND (t
, 1), flags
| TFF_EXPR_IN_PARENS
);
2714 pp_cxx_right_paren (pp
);
2718 dump_unary_op (cxx_pretty_printer
*pp
, const char *opstring
, tree t
, int flags
)
2720 if (flags
& TFF_EXPR_IN_PARENS
)
2721 pp_cxx_left_paren (pp
);
2722 pp_cxx_ws_string (pp
, opstring
);
2723 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
& ~TFF_EXPR_IN_PARENS
);
2724 if (flags
& TFF_EXPR_IN_PARENS
)
2725 pp_cxx_right_paren (pp
);
2729 reinit_cxx_pp (void)
2731 pp_clear_output_area (cxx_pp
);
2732 cxx_pp
->padding
= pp_none
;
2733 pp_indentation (cxx_pp
) = 0;
2734 pp_needs_newline (cxx_pp
) = false;
2735 cxx_pp
->enclosing_scope
= current_function_decl
;
2738 /* Same as pp_formatted_text, except the return string is a separate
2739 copy and has a GGC storage duration, e.g. an indefinite lifetime. */
2742 pp_ggc_formatted_text (pretty_printer
*pp
)
2744 return ggc_strdup (pp_formatted_text (pp
));
2747 /* Exported interface to stringifying types, exprs and decls under TFF_*
2751 type_as_string (tree typ
, int flags
)
2754 pp_translate_identifiers (cxx_pp
) = false;
2755 dump_type (cxx_pp
, typ
, flags
);
2756 return pp_ggc_formatted_text (cxx_pp
);
2760 type_as_string_translate (tree typ
, int flags
)
2763 dump_type (cxx_pp
, typ
, flags
);
2764 return pp_ggc_formatted_text (cxx_pp
);
2768 expr_as_string (tree decl
, int flags
)
2771 pp_translate_identifiers (cxx_pp
) = false;
2772 dump_expr (cxx_pp
, decl
, flags
);
2773 return pp_ggc_formatted_text (cxx_pp
);
2776 /* Wrap decl_as_string with options appropriate for dwarf. */
2779 decl_as_dwarf_string (tree decl
, int flags
)
2782 /* Curiously, reinit_cxx_pp doesn't reset the flags field, so setting the flag
2783 here will be adequate to get the desired behaviour. */
2784 cxx_pp
->flags
|= pp_c_flag_gnu_v3
;
2785 name
= decl_as_string (decl
, flags
);
2786 /* Subsequent calls to the pretty printer shouldn't use this style. */
2787 cxx_pp
->flags
&= ~pp_c_flag_gnu_v3
;
2792 decl_as_string (tree decl
, int flags
)
2795 pp_translate_identifiers (cxx_pp
) = false;
2796 dump_decl (cxx_pp
, decl
, flags
);
2797 return pp_ggc_formatted_text (cxx_pp
);
2801 decl_as_string_translate (tree decl
, int flags
)
2804 dump_decl (cxx_pp
, decl
, flags
);
2805 return pp_ggc_formatted_text (cxx_pp
);
2808 /* Wrap lang_decl_name with options appropriate for dwarf. */
2811 lang_decl_dwarf_name (tree decl
, int v
, bool translate
)
2814 /* Curiously, reinit_cxx_pp doesn't reset the flags field, so setting the flag
2815 here will be adequate to get the desired behaviour. */
2816 cxx_pp
->flags
|= pp_c_flag_gnu_v3
;
2817 name
= lang_decl_name (decl
, v
, translate
);
2818 /* Subsequent calls to the pretty printer shouldn't use this style. */
2819 cxx_pp
->flags
&= ~pp_c_flag_gnu_v3
;
2823 /* Generate the three forms of printable names for cxx_printable_name. */
2826 lang_decl_name (tree decl
, int v
, bool translate
)
2830 ? decl_as_string_translate (decl
, TFF_DECL_SPECIFIERS
)
2831 : decl_as_string (decl
, TFF_DECL_SPECIFIERS
));
2834 pp_translate_identifiers (cxx_pp
) = translate
;
2836 && (DECL_CLASS_SCOPE_P (decl
)
2837 || (DECL_NAMESPACE_SCOPE_P (decl
)
2838 && CP_DECL_CONTEXT (decl
) != global_namespace
)))
2840 dump_type (cxx_pp
, CP_DECL_CONTEXT (decl
), TFF_PLAIN_IDENTIFIER
);
2841 pp_cxx_colon_colon (cxx_pp
);
2844 if (TREE_CODE (decl
) == FUNCTION_DECL
)
2845 dump_function_name (cxx_pp
, decl
, TFF_PLAIN_IDENTIFIER
);
2846 else if ((DECL_NAME (decl
) == NULL_TREE
)
2847 && TREE_CODE (decl
) == NAMESPACE_DECL
)
2848 dump_decl (cxx_pp
, decl
, TFF_PLAIN_IDENTIFIER
| TFF_UNQUALIFIED_NAME
);
2850 dump_decl (cxx_pp
, DECL_NAME (decl
), TFF_PLAIN_IDENTIFIER
);
2852 return pp_ggc_formatted_text (cxx_pp
);
2855 /* Return the location of a tree passed to %+ formats. */
2858 location_of (tree t
)
2862 t
= TYPE_MAIN_DECL (t
);
2864 return input_location
;
2866 else if (TREE_CODE (t
) == OVERLOAD
)
2867 t
= OVL_FUNCTION (t
);
2870 return DECL_SOURCE_LOCATION (t
);
2871 return EXPR_LOC_OR_LOC (t
, input_location
);
2874 /* Now the interfaces from error et al to dump_type et al. Each takes an
2875 on/off VERBOSE flag and supply the appropriate TFF_ flags to a dump_
2879 decl_to_string (tree decl
, int verbose
)
2883 if (TREE_CODE (decl
) == TYPE_DECL
|| TREE_CODE (decl
) == RECORD_TYPE
2884 || TREE_CODE (decl
) == UNION_TYPE
|| TREE_CODE (decl
) == ENUMERAL_TYPE
)
2885 flags
= TFF_CLASS_KEY_OR_ENUM
;
2887 flags
|= TFF_DECL_SPECIFIERS
;
2888 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
2889 flags
|= TFF_DECL_SPECIFIERS
| TFF_RETURN_TYPE
;
2890 flags
|= TFF_TEMPLATE_HEADER
;
2893 dump_decl (cxx_pp
, decl
, flags
);
2894 return pp_ggc_formatted_text (cxx_pp
);
2898 expr_to_string (tree decl
)
2901 dump_expr (cxx_pp
, decl
, 0);
2902 return pp_ggc_formatted_text (cxx_pp
);
2906 fndecl_to_string (tree fndecl
, int verbose
)
2910 flags
= TFF_EXCEPTION_SPECIFICATION
| TFF_DECL_SPECIFIERS
2911 | TFF_TEMPLATE_HEADER
;
2913 flags
|= TFF_FUNCTION_DEFAULT_ARGUMENTS
;
2915 dump_decl (cxx_pp
, fndecl
, flags
);
2916 return pp_ggc_formatted_text (cxx_pp
);
2921 code_to_string (enum tree_code c
)
2923 return get_tree_code_name (c
);
2927 language_to_string (enum languages c
)
2934 case lang_cplusplus
:
2946 /* Return the proper printed version of a parameter to a C++ function. */
2949 parm_to_string (int p
)
2953 pp_string (cxx_pp
, "'this'");
2955 pp_decimal_int (cxx_pp
, p
+ 1);
2956 return pp_ggc_formatted_text (cxx_pp
);
2960 op_to_string (enum tree_code p
)
2962 tree id
= operator_name_info
[p
].identifier
;
2963 return id
? IDENTIFIER_POINTER (id
) : M_("<unknown>");
2967 type_to_string (tree typ
, int verbose
)
2971 flags
|= TFF_CLASS_KEY_OR_ENUM
;
2972 flags
|= TFF_TEMPLATE_HEADER
;
2975 dump_type (cxx_pp
, typ
, flags
);
2976 /* If we're printing a type that involves typedefs, also print the
2977 stripped version. But sometimes the stripped version looks
2978 exactly the same, so we don't want it after all. To avoid printing
2979 it in that case, we play ugly obstack games. */
2980 if (typ
&& TYPE_P (typ
) && typ
!= TYPE_CANONICAL (typ
)
2981 && !uses_template_parms (typ
))
2983 int aka_start
, aka_len
; char *p
;
2984 struct obstack
*ob
= pp_buffer (cxx_pp
)->obstack
;
2985 /* Remember the end of the initial dump. */
2986 int len
= obstack_object_size (ob
);
2987 tree aka
= strip_typedefs (typ
);
2988 pp_string (cxx_pp
, " {aka");
2989 pp_cxx_whitespace (cxx_pp
);
2990 /* And remember the start of the aka dump. */
2991 aka_start
= obstack_object_size (ob
);
2992 dump_type (cxx_pp
, aka
, flags
);
2993 aka_len
= obstack_object_size (ob
) - aka_start
;
2994 pp_right_brace (cxx_pp
);
2995 p
= (char*)obstack_base (ob
);
2996 /* If they are identical, cut off the aka with a NUL. */
2997 if (len
== aka_len
&& memcmp (p
, p
+aka_start
, len
) == 0)
3000 return pp_ggc_formatted_text (cxx_pp
);
3004 assop_to_string (enum tree_code p
)
3006 tree id
= assignment_operator_name_info
[(int) p
].identifier
;
3007 return id
? IDENTIFIER_POINTER (id
) : M_("{unknown}");
3011 args_to_string (tree p
, int verbose
)
3015 flags
|= TFF_CLASS_KEY_OR_ENUM
;
3020 if (TYPE_P (TREE_VALUE (p
)))
3021 return type_as_string_translate (p
, flags
);
3024 for (; p
; p
= TREE_CHAIN (p
))
3026 if (TREE_VALUE (p
) == null_node
)
3027 pp_cxx_ws_string (cxx_pp
, "NULL");
3029 dump_type (cxx_pp
, error_type (TREE_VALUE (p
)), flags
);
3031 pp_separate_with_comma (cxx_pp
);
3033 return pp_ggc_formatted_text (cxx_pp
);
3036 /* Pretty-print a deduction substitution (from deduction_tsubst_fntype). P
3037 is a TREE_LIST with purpose the TEMPLATE_DECL, value the template
3041 subst_to_string (tree p
)
3043 tree decl
= TREE_PURPOSE (p
);
3044 tree targs
= TREE_VALUE (p
);
3045 tree tparms
= DECL_TEMPLATE_PARMS (decl
);
3046 int flags
= (TFF_DECL_SPECIFIERS
|TFF_TEMPLATE_HEADER
3047 |TFF_NO_TEMPLATE_BINDINGS
);
3053 dump_template_decl (cxx_pp
, TREE_PURPOSE (p
), flags
);
3054 dump_substitution (cxx_pp
, NULL
, tparms
, targs
, /*flags=*/0);
3055 return pp_ggc_formatted_text (cxx_pp
);
3059 cv_to_string (tree p
, int v
)
3062 cxx_pp
->padding
= v
? pp_before
: pp_none
;
3063 pp_cxx_cv_qualifier_seq (cxx_pp
, p
);
3064 return pp_ggc_formatted_text (cxx_pp
);
3068 eh_spec_to_string (tree p
, int /*v*/)
3072 dump_exception_spec (cxx_pp
, p
, flags
);
3073 return pp_ggc_formatted_text (cxx_pp
);
3076 /* Langhook for print_error_function. */
3078 cxx_print_error_function (diagnostic_context
*context
, const char *file
,
3079 diagnostic_info
*diagnostic
)
3081 lhd_print_error_function (context
, file
, diagnostic
);
3082 pp_set_prefix (context
->printer
, file
);
3083 maybe_print_instantiation_context (context
);
3087 cp_diagnostic_starter (diagnostic_context
*context
,
3088 diagnostic_info
*diagnostic
)
3090 diagnostic_report_current_module (context
, diagnostic_location (diagnostic
));
3091 cp_print_error_function (context
, diagnostic
);
3092 maybe_print_instantiation_context (context
);
3093 maybe_print_constexpr_context (context
);
3094 pp_set_prefix (context
->printer
, diagnostic_build_prefix (context
,
3098 /* Print current function onto BUFFER, in the process of reporting
3099 a diagnostic message. Called from cp_diagnostic_starter. */
3101 cp_print_error_function (diagnostic_context
*context
,
3102 diagnostic_info
*diagnostic
)
3104 /* If we are in an instantiation context, current_function_decl is likely
3105 to be wrong, so just rely on print_instantiation_full_context. */
3106 if (current_instantiation ())
3108 if (diagnostic_last_function_changed (context
, diagnostic
))
3110 const char *old_prefix
= context
->printer
->prefix
;
3111 const char *file
= LOCATION_FILE (diagnostic_location (diagnostic
));
3112 tree abstract_origin
= diagnostic_abstract_origin (diagnostic
);
3113 char *new_prefix
= (file
&& abstract_origin
== NULL
)
3114 ? file_name_as_prefix (context
, file
) : NULL
;
3116 pp_set_prefix (context
->printer
, new_prefix
);
3118 if (current_function_decl
== NULL
)
3119 pp_string (context
->printer
, _("At global scope:"));
3124 if (abstract_origin
)
3126 ao
= BLOCK_ABSTRACT_ORIGIN (abstract_origin
);
3127 while (TREE_CODE (ao
) == BLOCK
3128 && BLOCK_ABSTRACT_ORIGIN (ao
)
3129 && BLOCK_ABSTRACT_ORIGIN (ao
) != ao
)
3130 ao
= BLOCK_ABSTRACT_ORIGIN (ao
);
3131 gcc_assert (TREE_CODE (ao
) == FUNCTION_DECL
);
3135 fndecl
= current_function_decl
;
3137 pp_printf (context
->printer
, function_category (fndecl
),
3138 cxx_printable_name_translate (fndecl
, 2));
3140 while (abstract_origin
)
3143 tree block
= abstract_origin
;
3145 locus
= &BLOCK_SOURCE_LOCATION (block
);
3147 block
= BLOCK_SUPERCONTEXT (block
);
3148 while (block
&& TREE_CODE (block
) == BLOCK
3149 && BLOCK_ABSTRACT_ORIGIN (block
))
3151 ao
= BLOCK_ABSTRACT_ORIGIN (block
);
3153 while (TREE_CODE (ao
) == BLOCK
3154 && BLOCK_ABSTRACT_ORIGIN (ao
)
3155 && BLOCK_ABSTRACT_ORIGIN (ao
) != ao
)
3156 ao
= BLOCK_ABSTRACT_ORIGIN (ao
);
3158 if (TREE_CODE (ao
) == FUNCTION_DECL
)
3163 else if (TREE_CODE (ao
) != BLOCK
)
3166 block
= BLOCK_SUPERCONTEXT (block
);
3169 abstract_origin
= block
;
3172 while (block
&& TREE_CODE (block
) == BLOCK
)
3173 block
= BLOCK_SUPERCONTEXT (block
);
3175 if (block
&& TREE_CODE (block
) == FUNCTION_DECL
)
3177 abstract_origin
= NULL
;
3181 expanded_location s
= expand_location (*locus
);
3182 pp_character (context
->printer
, ',');
3183 pp_newline (context
->printer
);
3186 if (context
->show_column
&& s
.column
!= 0)
3187 pp_printf (context
->printer
,
3188 _(" inlined from %qs at %r%s:%d:%d%R"),
3189 cxx_printable_name_translate (fndecl
, 2),
3190 "locus", s
.file
, s
.line
, s
.column
);
3192 pp_printf (context
->printer
,
3193 _(" inlined from %qs at %r%s:%d%R"),
3194 cxx_printable_name_translate (fndecl
, 2),
3195 "locus", s
.file
, s
.line
);
3199 pp_printf (context
->printer
, _(" inlined from %qs"),
3200 cxx_printable_name_translate (fndecl
, 2));
3203 pp_character (context
->printer
, ':');
3205 pp_newline (context
->printer
);
3207 diagnostic_set_last_function (context
, diagnostic
);
3208 pp_destroy_prefix (context
->printer
);
3209 context
->printer
->prefix
= old_prefix
;
3213 /* Returns a description of FUNCTION using standard terminology. The
3214 result is a format string of the form "In CATEGORY %qs". */
3216 function_category (tree fn
)
3218 /* We can get called from the middle-end for diagnostics of function
3219 clones. Make sure we have language specific information before
3220 dereferencing it. */
3221 if (DECL_LANG_SPECIFIC (STRIP_TEMPLATE (fn
))
3222 && DECL_FUNCTION_MEMBER_P (fn
))
3224 if (DECL_STATIC_FUNCTION_P (fn
))
3225 return _("In static member function %qs");
3226 else if (DECL_COPY_CONSTRUCTOR_P (fn
))
3227 return _("In copy constructor %qs");
3228 else if (DECL_CONSTRUCTOR_P (fn
))
3229 return _("In constructor %qs");
3230 else if (DECL_DESTRUCTOR_P (fn
))
3231 return _("In destructor %qs");
3232 else if (LAMBDA_FUNCTION_P (fn
))
3233 return _("In lambda function");
3235 return _("In member function %qs");
3238 return _("In function %qs");
3241 /* Report the full context of a current template instantiation,
3244 print_instantiation_full_context (diagnostic_context
*context
)
3246 struct tinst_level
*p
= current_instantiation ();
3247 location_t location
= input_location
;
3251 pp_verbatim (context
->printer
,
3252 TREE_CODE (p
->decl
) == TREE_LIST
3253 ? _("%s: In substitution of %qS:\n")
3254 : _("%s: In instantiation of %q#D:\n"),
3255 LOCATION_FILE (location
),
3258 location
= p
->locus
;
3262 print_instantiation_partial_context (context
, p
, location
);
3265 /* Helper function of print_instantiation_partial_context() that
3266 prints a single line of instantiation context. */
3269 print_instantiation_partial_context_line (diagnostic_context
*context
,
3270 const struct tinst_level
*t
,
3271 location_t loc
, bool recursive_p
)
3273 if (loc
== UNKNOWN_LOCATION
)
3276 expanded_location xloc
= expand_location (loc
);
3278 if (context
->show_column
)
3279 pp_verbatim (context
->printer
, _("%r%s:%d:%d:%R "),
3280 "locus", xloc
.file
, xloc
.line
, xloc
.column
);
3282 pp_verbatim (context
->printer
, _("%r%s:%d:%R "),
3283 "locus", xloc
.file
, xloc
.line
);
3287 if (TREE_CODE (t
->decl
) == TREE_LIST
)
3288 pp_verbatim (context
->printer
,
3290 ? _("recursively required by substitution of %qS\n")
3291 : _("required by substitution of %qS\n"),
3294 pp_verbatim (context
->printer
,
3296 ? _("recursively required from %q#D\n")
3297 : _("required from %q#D\n"),
3302 pp_verbatim (context
->printer
,
3304 ? _("recursively required from here")
3305 : _("required from here"));
3309 /* Same as print_instantiation_full_context but less verbose. */
3312 print_instantiation_partial_context (diagnostic_context
*context
,
3313 struct tinst_level
*t0
, location_t loc
)
3315 struct tinst_level
*t
;
3318 location_t prev_loc
= loc
;
3320 for (t
= t0
; t
!= NULL
; t
= t
->next
)
3321 if (prev_loc
!= t
->locus
)
3323 prev_loc
= t
->locus
;
3329 if (template_backtrace_limit
3330 && n_total
> template_backtrace_limit
)
3332 int skip
= n_total
- template_backtrace_limit
;
3333 int head
= template_backtrace_limit
/ 2;
3335 /* Avoid skipping just 1. If so, skip 2. */
3339 head
= (template_backtrace_limit
- 1) / 2;
3342 for (n
= 0; n
< head
; n
++)
3344 gcc_assert (t
!= NULL
);
3345 if (loc
!= t
->locus
)
3346 print_instantiation_partial_context_line (context
, t
, loc
,
3347 /*recursive_p=*/false);
3351 if (t
!= NULL
&& skip
> 0)
3353 expanded_location xloc
;
3354 xloc
= expand_location (loc
);
3355 if (context
->show_column
)
3356 pp_verbatim (context
->printer
,
3357 _("%r%s:%d:%d:%R [ skipping %d instantiation "
3358 "contexts, use -ftemplate-backtrace-limit=0 to "
3360 "locus", xloc
.file
, xloc
.line
, xloc
.column
, skip
);
3362 pp_verbatim (context
->printer
,
3363 _("%r%s:%d:%R [ skipping %d instantiation "
3364 "contexts, use -ftemplate-backtrace-limit=0 to "
3366 "locus", xloc
.file
, xloc
.line
, skip
);
3371 } while (t
!= NULL
&& --skip
> 0);
3377 while (t
->next
!= NULL
&& t
->locus
== t
->next
->locus
)
3382 print_instantiation_partial_context_line (context
, t
, loc
,
3387 print_instantiation_partial_context_line (context
, NULL
, loc
,
3388 /*recursive_p=*/false);
3389 pp_newline (context
->printer
);
3392 /* Called from cp_thing to print the template context for an error. */
3394 maybe_print_instantiation_context (diagnostic_context
*context
)
3396 if (!problematic_instantiation_changed () || current_instantiation () == 0)
3399 record_last_problematic_instantiation ();
3400 print_instantiation_full_context (context
);
3403 /* Report what constexpr call(s) we're trying to expand, if any. */
3406 maybe_print_constexpr_context (diagnostic_context
*context
)
3408 vec
<tree
> call_stack
= cx_error_context ();
3412 FOR_EACH_VEC_ELT (call_stack
, ix
, t
)
3414 expanded_location xloc
= expand_location (EXPR_LOCATION (t
));
3415 const char *s
= expr_as_string (t
, 0);
3416 if (context
->show_column
)
3417 pp_verbatim (context
->printer
,
3418 _("%r%s:%d:%d:%R in constexpr expansion of %qs"),
3419 "locus", xloc
.file
, xloc
.line
, xloc
.column
, s
);
3421 pp_verbatim (context
->printer
,
3422 _("%r%s:%d:%R in constexpr expansion of %qs"),
3423 "locus", xloc
.file
, xloc
.line
, s
);
3424 pp_newline (context
->printer
);
3428 /* Called from output_format -- during diagnostic message processing --
3429 to handle C++ specific format specifier with the following meanings:
3430 %A function argument-list.
3434 %F function declaration.
3435 %L language as used in extern "lang".
3437 %P function parameter whose position is indicated by an integer.
3438 %Q assignment operator.
3439 %S substitution (template + args)
3442 %X exception-specification. */
3444 cp_printer (pretty_printer
*pp
, text_info
*text
, const char *spec
,
3445 int precision
, bool wide
, bool set_locus
, bool verbose
)
3449 #define next_tree (t = va_arg (*text->args_ptr, tree))
3450 #define next_tcode ((enum tree_code) va_arg (*text->args_ptr, int))
3451 #define next_lang ((enum languages) va_arg (*text->args_ptr, int))
3452 #define next_int va_arg (*text->args_ptr, int)
3454 if (precision
!= 0 || wide
)
3459 case 'A': result
= args_to_string (next_tree
, verbose
); break;
3460 case 'C': result
= code_to_string (next_tcode
); break;
3463 tree temp
= next_tree
;
3465 && DECL_HAS_DEBUG_EXPR_P (temp
))
3467 temp
= DECL_DEBUG_EXPR (temp
);
3470 result
= expr_to_string (temp
);
3474 result
= decl_to_string (temp
, verbose
);
3477 case 'E': result
= expr_to_string (next_tree
); break;
3478 case 'F': result
= fndecl_to_string (next_tree
, verbose
); break;
3479 case 'L': result
= language_to_string (next_lang
); break;
3480 case 'O': result
= op_to_string (next_tcode
); break;
3481 case 'P': result
= parm_to_string (next_int
); break;
3482 case 'Q': result
= assop_to_string (next_tcode
); break;
3483 case 'S': result
= subst_to_string (next_tree
); break;
3484 case 'T': result
= type_to_string (next_tree
, verbose
); break;
3485 case 'V': result
= cv_to_string (next_tree
, verbose
); break;
3486 case 'X': result
= eh_spec_to_string (next_tree
, verbose
); break;
3489 percent_K_format (text
);
3496 pp_string (pp
, result
);
3497 if (set_locus
&& t
!= NULL
)
3498 text
->set_location (0, location_of (t
));
3506 /* Warn about the use of C++0x features when appropriate. */
3508 maybe_warn_cpp0x (cpp0x_warn_str str
)
3510 if ((cxx_dialect
== cxx98
) && !in_system_header_at (input_location
))
3511 /* We really want to suppress this warning in system headers,
3512 because libstdc++ uses variadic templates even when we aren't
3516 case CPP0X_INITIALIZER_LISTS
:
3517 pedwarn (input_location
, 0,
3518 "extended initializer lists "
3519 "only available with -std=c++11 or -std=gnu++11");
3521 case CPP0X_EXPLICIT_CONVERSION
:
3522 pedwarn (input_location
, 0,
3523 "explicit conversion operators "
3524 "only available with -std=c++11 or -std=gnu++11");
3526 case CPP0X_VARIADIC_TEMPLATES
:
3527 pedwarn (input_location
, 0,
3528 "variadic templates "
3529 "only available with -std=c++11 or -std=gnu++11");
3531 case CPP0X_LAMBDA_EXPR
:
3532 pedwarn (input_location
, 0,
3533 "lambda expressions "
3534 "only available with -std=c++11 or -std=gnu++11");
3537 pedwarn (input_location
, 0,
3538 "C++11 auto only available with -std=c++11 or -std=gnu++11");
3540 case CPP0X_SCOPED_ENUMS
:
3541 pedwarn (input_location
, 0,
3542 "scoped enums only available with -std=c++11 or -std=gnu++11");
3544 case CPP0X_DEFAULTED_DELETED
:
3545 pedwarn (input_location
, 0,
3546 "defaulted and deleted functions "
3547 "only available with -std=c++11 or -std=gnu++11");
3549 case CPP0X_INLINE_NAMESPACES
:
3550 pedwarn (input_location
, OPT_Wpedantic
,
3551 "inline namespaces "
3552 "only available with -std=c++11 or -std=gnu++11");
3554 case CPP0X_OVERRIDE_CONTROLS
:
3555 pedwarn (input_location
, 0,
3556 "override controls (override/final) "
3557 "only available with -std=c++11 or -std=gnu++11");
3560 pedwarn (input_location
, 0,
3561 "non-static data member initializers "
3562 "only available with -std=c++11 or -std=gnu++11");
3564 case CPP0X_USER_DEFINED_LITERALS
:
3565 pedwarn (input_location
, 0,
3566 "user-defined literals "
3567 "only available with -std=c++11 or -std=gnu++11");
3569 case CPP0X_DELEGATING_CTORS
:
3570 pedwarn (input_location
, 0,
3571 "delegating constructors "
3572 "only available with -std=c++11 or -std=gnu++11");
3574 case CPP0X_INHERITING_CTORS
:
3575 pedwarn (input_location
, 0,
3576 "inheriting constructors "
3577 "only available with -std=c++11 or -std=gnu++11");
3579 case CPP0X_ATTRIBUTES
:
3580 pedwarn (input_location
, 0,
3582 "only available with -std=c++11 or -std=gnu++11");
3584 case CPP0X_REF_QUALIFIER
:
3585 pedwarn (input_location
, 0,
3587 "only available with -std=c++11 or -std=gnu++11");
3594 /* Warn about the use of variadic templates when appropriate. */
3596 maybe_warn_variadic_templates (void)
3598 maybe_warn_cpp0x (CPP0X_VARIADIC_TEMPLATES
);
3602 /* Issue an ISO C++98 pedantic warning at LOCATION, conditional on
3603 option OPT with text GMSGID. Use this function to report
3604 diagnostics for constructs that are invalid C++98, but valid
3607 pedwarn_cxx98 (location_t location
, int opt
, const char *gmsgid
, ...)
3609 diagnostic_info diagnostic
;
3613 va_start (ap
, gmsgid
);
3614 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, location
,
3615 (cxx_dialect
== cxx98
) ? DK_PEDWARN
: DK_WARNING
);
3616 diagnostic
.option_index
= opt
;
3617 ret
= report_diagnostic (&diagnostic
);
3622 /* Issue a diagnostic that NAME cannot be found in SCOPE. DECL is what
3623 we found when we tried to do the lookup. LOCATION is the location of
3624 the NAME identifier. */
3627 qualified_name_lookup_error (tree scope
, tree name
,
3628 tree decl
, location_t location
)
3630 if (scope
== error_mark_node
)
3631 ; /* We already complained. */
3632 else if (TYPE_P (scope
))
3634 if (!COMPLETE_TYPE_P (scope
))
3635 error_at (location
, "incomplete type %qT used in nested name specifier",
3637 else if (TREE_CODE (decl
) == TREE_LIST
)
3639 error_at (location
, "reference to %<%T::%D%> is ambiguous",
3641 print_candidates (decl
);
3644 error_at (location
, "%qD is not a member of %qT", name
, scope
);
3646 else if (scope
!= global_namespace
)
3648 error_at (location
, "%qD is not a member of %qD", name
, scope
);
3649 suggest_alternatives_for (location
, name
);
3653 error_at (location
, "%<::%D%> has not been declared", name
);
3654 suggest_alternatives_for (location
, name
);