1 /* Call-backs for C++ error reporting.
2 This code is non-reentrant.
3 Copyright (C) 1993-2018 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/>. */
21 /* For use with name_hint. */
22 #define INCLUDE_UNIQUE_PTR
24 #include "coretypes.h"
26 #include "stringpool.h"
27 #include "tree-diagnostic.h"
28 #include "langhooks-def.h"
30 #include "cxx-pretty-print.h"
31 #include "tree-pretty-print.h"
32 #include "gimple-pretty-print.h"
33 #include "c-family/c-objc.h"
35 #include "internal-fn.h"
36 #include "gcc-rich-location.h"
37 #include "cp-name-hint.h"
39 #define pp_separate_with_comma(PP) pp_cxx_separate_with (PP, ',')
40 #define pp_separate_with_semicolon(PP) pp_cxx_separate_with (PP, ';')
42 /* cxx_pp is a C++ front-end-specific pretty printer: this is where we
43 dump C++ ASTs as strings. It is mostly used only by the various
44 tree -> string functions that are occasionally called from the
45 debugger or by the front-end for things like
46 __PRETTY_FUNCTION__. */
47 static cxx_pretty_printer actual_pretty_printer
;
48 static cxx_pretty_printer
* const cxx_pp
= &actual_pretty_printer
;
50 /* Translate if being used for diagnostics, but not for dump files or
52 #define M_(msgid) (pp_translate_identifiers (cxx_pp) ? _(msgid) : (msgid))
54 # define NEXT_CODE(T) (TREE_CODE (TREE_TYPE (T)))
56 static const char *args_to_string (tree
, int);
57 static const char *code_to_string (enum tree_code
);
58 static const char *cv_to_string (tree
, int);
59 static const char *decl_to_string (tree
, int);
60 static const char *fndecl_to_string (tree
, int);
61 static const char *op_to_string (bool, enum tree_code
);
62 static const char *parm_to_string (int);
63 static const char *type_to_string (tree
, int, bool, bool *, bool);
65 static void dump_alias_template_specialization (cxx_pretty_printer
*, tree
, int);
66 static void dump_type (cxx_pretty_printer
*, tree
, int);
67 static void dump_typename (cxx_pretty_printer
*, tree
, int);
68 static void dump_simple_decl (cxx_pretty_printer
*, tree
, tree
, int);
69 static void dump_decl (cxx_pretty_printer
*, tree
, int);
70 static void dump_template_decl (cxx_pretty_printer
*, tree
, int);
71 static void dump_function_decl (cxx_pretty_printer
*, tree
, int);
72 static void dump_expr (cxx_pretty_printer
*, tree
, int);
73 static void dump_unary_op (cxx_pretty_printer
*, const char *, tree
, int);
74 static void dump_binary_op (cxx_pretty_printer
*, const char *, tree
, int);
75 static void dump_aggr_type (cxx_pretty_printer
*, tree
, int);
76 static void dump_type_prefix (cxx_pretty_printer
*, tree
, int);
77 static void dump_type_suffix (cxx_pretty_printer
*, tree
, int);
78 static void dump_function_name (cxx_pretty_printer
*, tree
, int);
79 static void dump_call_expr_args (cxx_pretty_printer
*, tree
, int, bool);
80 static void dump_aggr_init_expr_args (cxx_pretty_printer
*, tree
, int, bool);
81 static void dump_expr_list (cxx_pretty_printer
*, tree
, int);
82 static void dump_global_iord (cxx_pretty_printer
*, tree
);
83 static void dump_parameters (cxx_pretty_printer
*, tree
, int);
84 static void dump_ref_qualifier (cxx_pretty_printer
*, tree
, int);
85 static void dump_exception_spec (cxx_pretty_printer
*, tree
, int);
86 static void dump_template_argument (cxx_pretty_printer
*, tree
, int);
87 static void dump_template_argument_list (cxx_pretty_printer
*, tree
, int);
88 static void dump_template_parameter (cxx_pretty_printer
*, tree
, int);
89 static void dump_template_bindings (cxx_pretty_printer
*, tree
, tree
,
91 static void dump_scope (cxx_pretty_printer
*, tree
, int);
92 static void dump_template_parms (cxx_pretty_printer
*, tree
, int, int);
93 static int get_non_default_template_args_count (tree
, int);
94 static const char *function_category (tree
);
95 static void maybe_print_constexpr_context (diagnostic_context
*);
96 static void maybe_print_instantiation_context (diagnostic_context
*);
97 static void print_instantiation_full_context (diagnostic_context
*);
98 static void print_instantiation_partial_context (diagnostic_context
*,
101 static void cp_diagnostic_starter (diagnostic_context
*, diagnostic_info
*);
102 static void cp_print_error_function (diagnostic_context
*, diagnostic_info
*);
104 static bool cp_printer (pretty_printer
*, text_info
*, const char *,
105 int, bool, bool, bool, bool *, const char **);
107 /* Struct for handling %H or %I, which require delaying printing the
108 type until a postprocessing stage. */
110 struct deferred_printed_type
112 deferred_printed_type ()
113 : m_tree (NULL_TREE
), m_buffer_ptr (NULL
), m_verbose (false), m_quote (false)
116 deferred_printed_type (tree type
, const char **buffer_ptr
, bool verbose
,
118 : m_tree (type
), m_buffer_ptr (buffer_ptr
), m_verbose (verbose
),
122 gcc_assert (buffer_ptr
);
125 /* The tree is not GTY-marked: they are only non-NULL within a
126 call to pp_format. */
128 const char **m_buffer_ptr
;
133 /* Subclass of format_postprocessor for the C++ frontend.
134 This handles the %H and %I formatting codes, printing them
135 in a postprocessing phase (since they affect each other). */
137 class cxx_format_postprocessor
: public format_postprocessor
140 cxx_format_postprocessor ()
141 : m_type_a (), m_type_b ()
144 void handle (pretty_printer
*pp
) FINAL OVERRIDE
;
146 deferred_printed_type m_type_a
;
147 deferred_printed_type m_type_b
;
150 /* CONTEXT->printer is a basic pretty printer that was constructed
151 presumably by diagnostic_initialize(), called early in the
152 compiler's initialization process (in general_init) Before the FE
153 is initialized. This (C++) FE-specific diagnostic initializer is
154 thus replacing the basic pretty printer with one that has C++-aware
158 cxx_initialize_diagnostics (diagnostic_context
*context
)
160 pretty_printer
*base
= context
->printer
;
161 cxx_pretty_printer
*pp
= XNEW (cxx_pretty_printer
);
162 context
->printer
= new (pp
) cxx_pretty_printer ();
164 /* It is safe to free this object because it was previously XNEW()'d. */
165 base
->~pretty_printer ();
168 c_common_diagnostics_set_defaults (context
);
169 diagnostic_starter (context
) = cp_diagnostic_starter
;
170 /* diagnostic_finalizer is already c_diagnostic_finalizer. */
171 diagnostic_format_decoder (context
) = cp_printer
;
172 pp
->m_format_postprocessor
= new cxx_format_postprocessor ();
175 /* Dump a scope, if deemed necessary. */
178 dump_scope (cxx_pretty_printer
*pp
, tree scope
, int flags
)
180 int f
= flags
& (TFF_SCOPE
| TFF_CHASE_TYPEDEF
);
182 if (scope
== NULL_TREE
)
185 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
187 if (scope
!= global_namespace
)
189 dump_decl (pp
, scope
, f
);
190 pp_cxx_colon_colon (pp
);
193 else if (AGGREGATE_TYPE_P (scope
))
195 dump_type (pp
, scope
, f
);
196 pp_cxx_colon_colon (pp
);
198 else if ((flags
& TFF_SCOPE
) && TREE_CODE (scope
) == FUNCTION_DECL
)
200 dump_function_decl (pp
, scope
, f
);
201 pp_cxx_colon_colon (pp
);
205 /* Dump the template ARGument under control of FLAGS. */
208 dump_template_argument (cxx_pretty_printer
*pp
, tree arg
, int flags
)
210 if (ARGUMENT_PACK_P (arg
))
211 dump_template_argument_list (pp
, ARGUMENT_PACK_ARGS (arg
),
212 /* No default args in argument packs. */
213 flags
|TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS
);
214 else if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
215 dump_type (pp
, arg
, flags
& ~TFF_CLASS_KEY_OR_ENUM
);
218 if (TREE_CODE (arg
) == TREE_LIST
)
219 arg
= TREE_VALUE (arg
);
221 /* Strip implicit conversions. */
222 while (CONVERT_EXPR_P (arg
))
223 arg
= TREE_OPERAND (arg
, 0);
225 dump_expr (pp
, arg
, (flags
| TFF_EXPR_IN_PARENS
) & ~TFF_CLASS_KEY_OR_ENUM
);
229 /* Count the number of template arguments ARGS whose value does not
230 match the (optional) default template parameter in PARAMS */
233 get_non_default_template_args_count (tree args
, int flags
)
235 int n
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_ARGS (args
));
237 if (/* We use this flag when generating debug information. We don't
238 want to expand templates at this point, for this may generate
239 new decls, which gets decl counts out of sync, which may in
240 turn cause codegen differences between compilations with and
242 (flags
& TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS
) != 0
243 || !flag_pretty_templates
)
246 return GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (INNERMOST_TEMPLATE_ARGS (args
));
249 /* Dump a template-argument-list ARGS (always a TREE_VEC) under control
253 dump_template_argument_list (cxx_pretty_printer
*pp
, tree args
, int flags
)
255 int n
= get_non_default_template_args_count (args
, flags
);
259 for (i
= 0; i
< n
; ++i
)
261 tree arg
= TREE_VEC_ELT (args
, i
);
263 /* Only print a comma if we know there is an argument coming. In
264 the case of an empty template argument pack, no actual
265 argument will be printed. */
267 && (!ARGUMENT_PACK_P (arg
)
268 || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) > 0))
269 pp_separate_with_comma (pp
);
271 dump_template_argument (pp
, arg
, flags
);
276 /* Dump a template parameter PARM (a TREE_LIST) under control of FLAGS. */
279 dump_template_parameter (cxx_pretty_printer
*pp
, tree parm
, int flags
)
284 if (parm
== error_mark_node
)
287 p
= TREE_VALUE (parm
);
288 a
= TREE_PURPOSE (parm
);
290 if (TREE_CODE (p
) == TYPE_DECL
)
292 if (flags
& TFF_DECL_SPECIFIERS
)
294 pp_cxx_ws_string (pp
, "class");
295 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (p
)))
296 pp_cxx_ws_string (pp
, "...");
298 pp_cxx_tree_identifier (pp
, DECL_NAME (p
));
300 else if (DECL_NAME (p
))
301 pp_cxx_tree_identifier (pp
, DECL_NAME (p
));
303 pp_cxx_canonical_template_parameter (pp
, TREE_TYPE (p
));
306 dump_decl (pp
, p
, flags
| TFF_DECL_SPECIFIERS
);
308 if ((flags
& TFF_FUNCTION_DEFAULT_ARGUMENTS
) && a
!= NULL_TREE
)
310 pp_cxx_whitespace (pp
);
312 pp_cxx_whitespace (pp
);
313 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
314 dump_type (pp
, a
, flags
& ~TFF_CHASE_TYPEDEF
);
316 dump_expr (pp
, a
, flags
| TFF_EXPR_IN_PARENS
);
320 /* Dump, under control of FLAGS, a template-parameter-list binding.
321 PARMS is a TREE_LIST of TREE_VEC of TREE_LIST and ARGS is a
325 dump_template_bindings (cxx_pretty_printer
*pp
, tree parms
, tree args
,
326 vec
<tree
, va_gc
> *typenames
)
328 bool need_semicolon
= false;
334 tree p
= TREE_VALUE (parms
);
335 int lvl
= TMPL_PARMS_DEPTH (parms
);
338 tree lvl_args
= NULL_TREE
;
340 /* Don't crash if we had an invalid argument list. */
341 if (TMPL_ARGS_DEPTH (args
) >= lvl
)
342 lvl_args
= TMPL_ARGS_LEVEL (args
, lvl
);
344 for (i
= 0; i
< TREE_VEC_LENGTH (p
); ++i
)
346 tree arg
= NULL_TREE
;
348 /* Don't crash if we had an invalid argument list. */
349 if (lvl_args
&& NUM_TMPL_ARGS (lvl_args
) > arg_idx
)
350 arg
= TREE_VEC_ELT (lvl_args
, arg_idx
);
353 pp_separate_with_semicolon (pp
);
354 dump_template_parameter (pp
, TREE_VEC_ELT (p
, i
),
355 TFF_PLAIN_IDENTIFIER
);
356 pp_cxx_whitespace (pp
);
358 pp_cxx_whitespace (pp
);
361 if (ARGUMENT_PACK_P (arg
))
362 pp_cxx_left_brace (pp
);
363 dump_template_argument (pp
, arg
, TFF_PLAIN_IDENTIFIER
);
364 if (ARGUMENT_PACK_P (arg
))
365 pp_cxx_right_brace (pp
);
368 pp_string (pp
, M_("<missing>"));
371 need_semicolon
= true;
374 parms
= TREE_CHAIN (parms
);
377 /* Don't bother with typenames for a partial instantiation. */
378 if (vec_safe_is_empty (typenames
) || uses_template_parms (args
))
381 /* Don't try to print typenames when we're processing a clone. */
382 if (current_function_decl
383 && !DECL_LANG_SPECIFIC (current_function_decl
))
386 /* Don't try to do this once cgraph starts throwing away front-end
391 FOR_EACH_VEC_SAFE_ELT (typenames
, i
, t
)
394 pp_separate_with_semicolon (pp
);
395 dump_type (pp
, t
, TFF_PLAIN_IDENTIFIER
);
396 pp_cxx_whitespace (pp
);
398 pp_cxx_whitespace (pp
);
399 push_deferring_access_checks (dk_no_check
);
400 t
= tsubst (t
, args
, tf_none
, NULL_TREE
);
401 pop_deferring_access_checks ();
402 /* Strip typedefs. We can't just use TFF_CHASE_TYPEDEF because
403 pp_simple_type_specifier doesn't know about it. */
404 t
= strip_typedefs (t
);
405 dump_type (pp
, t
, TFF_PLAIN_IDENTIFIER
);
409 /* Dump a human-readable equivalent of the alias template
410 specialization of T. */
413 dump_alias_template_specialization (cxx_pretty_printer
*pp
, tree t
, int flags
)
415 gcc_assert (alias_template_specialization_p (t
));
417 tree decl
= TYPE_NAME (t
);
418 if (!(flags
& TFF_UNQUALIFIED_NAME
))
419 dump_scope (pp
, CP_DECL_CONTEXT (decl
), flags
);
420 pp_cxx_tree_identifier (pp
, DECL_NAME (decl
));
421 dump_template_parms (pp
, DECL_TEMPLATE_INFO (decl
),
423 flags
& ~TFF_TEMPLATE_HEADER
);
426 /* Dump a human-readable equivalent of TYPE. FLAGS controls the
430 dump_type (cxx_pretty_printer
*pp
, tree t
, int flags
)
435 /* Don't print e.g. "struct mytypedef". */
436 if (TYPE_P (t
) && typedef_variant_p (t
))
438 tree decl
= TYPE_NAME (t
);
439 if ((flags
& TFF_CHASE_TYPEDEF
)
440 || DECL_SELF_REFERENCE_P (decl
)
441 || (!flag_pretty_templates
442 && DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)))
443 t
= strip_typedefs (t
);
444 else if (alias_template_specialization_p (t
))
446 dump_alias_template_specialization (pp
, t
, flags
);
449 else if (same_type_p (t
, TREE_TYPE (decl
)))
453 pp_cxx_cv_qualifier_seq (pp
, t
);
454 pp_cxx_tree_identifier (pp
, TYPE_IDENTIFIER (t
));
459 if (TYPE_PTRMEMFUNC_P (t
))
462 switch (TREE_CODE (t
))
465 if (t
== init_list_type_node
)
466 pp_string (pp
, M_("<brace-enclosed initializer list>"));
467 else if (t
== unknown_type_node
)
468 pp_string (pp
, M_("<unresolved overloaded function type>"));
471 pp_cxx_cv_qualifier_seq (pp
, t
);
472 pp_cxx_tree_identifier (pp
, TYPE_IDENTIFIER (t
));
477 /* A list of function parms. */
478 dump_parameters (pp
, t
, flags
);
481 case IDENTIFIER_NODE
:
482 pp_cxx_tree_identifier (pp
, t
);
486 dump_type (pp
, BINFO_TYPE (t
), flags
);
492 dump_aggr_type (pp
, t
, flags
);
496 if (flags
& TFF_CHASE_TYPEDEF
)
498 dump_type (pp
, DECL_ORIGINAL_TYPE (t
)
499 ? DECL_ORIGINAL_TYPE (t
) : TREE_TYPE (t
), flags
);
506 dump_decl (pp
, t
, flags
& ~TFF_DECL_SPECIFIERS
);
515 case FIXED_POINT_TYPE
:
516 pp_type_specifier_seq (pp
, t
);
519 case TEMPLATE_TEMPLATE_PARM
:
520 /* For parameters inside template signature. */
521 if (TYPE_IDENTIFIER (t
))
522 pp_cxx_tree_identifier (pp
, TYPE_IDENTIFIER (t
));
524 pp_cxx_canonical_template_parameter (pp
, t
);
527 case BOUND_TEMPLATE_TEMPLATE_PARM
:
529 tree args
= TYPE_TI_ARGS (t
);
530 pp_cxx_cv_qualifier_seq (pp
, t
);
531 pp_cxx_tree_identifier (pp
, TYPE_IDENTIFIER (t
));
532 pp_cxx_begin_template_argument_list (pp
);
533 dump_template_argument_list (pp
, args
, flags
);
534 pp_cxx_end_template_argument_list (pp
);
538 case TEMPLATE_TYPE_PARM
:
539 pp_cxx_cv_qualifier_seq (pp
, t
);
540 if (tree c
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
541 pp_cxx_constrained_type_spec (pp
, c
);
542 else if (TYPE_IDENTIFIER (t
))
543 pp_cxx_tree_identifier (pp
, TYPE_IDENTIFIER (t
));
545 pp_cxx_canonical_template_parameter
546 (pp
, TEMPLATE_TYPE_PARM_INDEX (t
));
549 /* This is not always necessary for pointers and such, but doing this
550 reduces code size. */
559 dump_type_prefix (pp
, t
, flags
);
560 dump_type_suffix (pp
, t
, flags
);
564 if (! (flags
& TFF_CHASE_TYPEDEF
)
565 && DECL_ORIGINAL_TYPE (TYPE_NAME (t
)))
567 dump_decl (pp
, TYPE_NAME (t
), TFF_PLAIN_IDENTIFIER
);
570 pp_cxx_cv_qualifier_seq (pp
, t
);
571 pp_cxx_ws_string (pp
,
572 TYPENAME_IS_ENUM_P (t
) ? "enum"
573 : TYPENAME_IS_CLASS_P (t
) ? "class"
575 dump_typename (pp
, t
, flags
);
578 case UNBOUND_CLASS_TEMPLATE
:
579 if (! (flags
& TFF_UNQUALIFIED_NAME
))
581 dump_type (pp
, TYPE_CONTEXT (t
), flags
);
582 pp_cxx_colon_colon (pp
);
584 pp_cxx_ws_string (pp
, "template");
585 dump_type (pp
, TYPE_IDENTIFIER (t
), flags
);
589 pp_cxx_ws_string (pp
, "__typeof__");
590 pp_cxx_whitespace (pp
);
591 pp_cxx_left_paren (pp
);
592 dump_expr (pp
, TYPEOF_TYPE_EXPR (t
), flags
& ~TFF_EXPR_IN_PARENS
);
593 pp_cxx_right_paren (pp
);
596 case UNDERLYING_TYPE
:
597 pp_cxx_ws_string (pp
, "__underlying_type");
598 pp_cxx_whitespace (pp
);
599 pp_cxx_left_paren (pp
);
600 dump_expr (pp
, UNDERLYING_TYPE_TYPE (t
), flags
& ~TFF_EXPR_IN_PARENS
);
601 pp_cxx_right_paren (pp
);
604 case TYPE_PACK_EXPANSION
:
605 dump_type (pp
, PACK_EXPANSION_PATTERN (t
), flags
);
606 pp_cxx_ws_string (pp
, "...");
609 case TYPE_ARGUMENT_PACK
:
610 dump_template_argument (pp
, t
, flags
);
614 pp_cxx_ws_string (pp
, "decltype");
615 pp_cxx_whitespace (pp
);
616 pp_cxx_left_paren (pp
);
617 dump_expr (pp
, DECLTYPE_TYPE_EXPR (t
), flags
& ~TFF_EXPR_IN_PARENS
);
618 pp_cxx_right_paren (pp
);
622 pp_string (pp
, "std::nullptr_t");
626 pp_unsupported_tree (pp
, t
);
630 pp_string (pp
, M_("<type error>"));
635 /* Dump a TYPENAME_TYPE. We need to notice when the context is itself
639 dump_typename (cxx_pretty_printer
*pp
, tree t
, int flags
)
641 tree ctx
= TYPE_CONTEXT (t
);
643 if (TREE_CODE (ctx
) == TYPENAME_TYPE
)
644 dump_typename (pp
, ctx
, flags
);
646 dump_type (pp
, ctx
, flags
& ~TFF_CLASS_KEY_OR_ENUM
);
647 pp_cxx_colon_colon (pp
);
648 dump_decl (pp
, TYPENAME_TYPE_FULLNAME (t
), flags
);
651 /* Return the name of the supplied aggregate, or enumeral type. */
654 class_key_or_enum_as_string (tree t
)
656 if (TREE_CODE (t
) == ENUMERAL_TYPE
)
658 if (SCOPED_ENUM_P (t
))
663 else if (TREE_CODE (t
) == UNION_TYPE
)
665 else if (TYPE_LANG_SPECIFIC (t
) && CLASSTYPE_DECLARED_CLASS (t
))
671 /* Print out a class declaration T under the control of FLAGS,
672 in the form `class foo'. */
675 dump_aggr_type (cxx_pretty_printer
*pp
, tree t
, int flags
)
678 const char *variety
= class_key_or_enum_as_string (t
);
682 pp_cxx_cv_qualifier_seq (pp
, t
);
684 if (flags
& TFF_CLASS_KEY_OR_ENUM
)
685 pp_cxx_ws_string (pp
, variety
);
687 name
= TYPE_NAME (t
);
691 typdef
= (!DECL_ARTIFICIAL (name
)
692 /* An alias specialization is not considered to be a
694 && !alias_template_specialization_p (t
));
697 && ((flags
& TFF_CHASE_TYPEDEF
)
698 || (!flag_pretty_templates
&& DECL_LANG_SPECIFIC (name
)
699 && DECL_TEMPLATE_INFO (name
))))
700 || DECL_SELF_REFERENCE_P (name
))
702 t
= TYPE_MAIN_VARIANT (t
);
703 name
= TYPE_NAME (t
);
707 tmplate
= !typdef
&& TREE_CODE (t
) != ENUMERAL_TYPE
708 && TYPE_LANG_SPECIFIC (t
) && CLASSTYPE_TEMPLATE_INFO (t
)
709 && (TREE_CODE (CLASSTYPE_TI_TEMPLATE (t
)) != TEMPLATE_DECL
710 || PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)));
712 if (! (flags
& TFF_UNQUALIFIED_NAME
))
713 dump_scope (pp
, CP_DECL_CONTEXT (name
), flags
| TFF_SCOPE
);
714 flags
&= ~TFF_UNQUALIFIED_NAME
;
717 /* Because the template names are mangled, we have to locate
718 the most general template, and use that name. */
719 tree tpl
= TYPE_TI_TEMPLATE (t
);
721 while (DECL_TEMPLATE_INFO (tpl
))
722 tpl
= DECL_TI_TEMPLATE (tpl
);
725 name
= DECL_NAME (name
);
728 if (name
== 0 || anon_aggrname_p (name
))
730 if (flags
& TFF_CLASS_KEY_OR_ENUM
)
731 pp_string (pp
, M_("<unnamed>"));
733 pp_printf (pp
, M_("<unnamed %s>"), variety
);
735 else if (LAMBDA_TYPE_P (t
))
737 /* A lambda's "type" is essentially its signature. */
738 pp_string (pp
, M_("<lambda"));
739 if (lambda_function (t
))
741 FUNCTION_FIRST_USER_PARMTYPE (lambda_function (t
)),
746 pp_cxx_tree_identifier (pp
, name
);
748 dump_template_parms (pp
, TYPE_TEMPLATE_INFO (t
),
749 !CLASSTYPE_USE_TEMPLATE (t
),
750 flags
& ~TFF_TEMPLATE_HEADER
);
753 /* Dump into the obstack the initial part of the output for a given type.
754 This is necessary when dealing with things like functions returning
757 return type of `int (* fee ())()': pointer -> function -> int. Both
758 pointer (and reference and offset) and function (and member) types must
759 deal with prefix and suffix.
761 Arrays must also do this for DECL nodes, like int a[], and for things like
765 dump_type_prefix (cxx_pretty_printer
*pp
, tree t
, int flags
)
767 if (TYPE_PTRMEMFUNC_P (t
))
769 t
= TYPE_PTRMEMFUNC_FN_TYPE (t
);
773 switch (TREE_CODE (t
))
778 tree sub
= TREE_TYPE (t
);
780 dump_type_prefix (pp
, sub
, flags
);
781 if (TREE_CODE (sub
) == ARRAY_TYPE
782 || TREE_CODE (sub
) == FUNCTION_TYPE
)
784 pp_cxx_whitespace (pp
);
785 pp_cxx_left_paren (pp
);
786 pp_c_attributes_display (pp
, TYPE_ATTRIBUTES (sub
));
790 else if (TYPE_REF_P (t
))
792 if (TYPE_REF_IS_RVALUE (t
))
793 pp_ampersand_ampersand (pp
);
797 pp
->padding
= pp_before
;
798 pp_cxx_cv_qualifier_seq (pp
, t
);
804 dump_type_prefix (pp
, TREE_TYPE (t
), flags
);
805 if (TREE_CODE (t
) == OFFSET_TYPE
) /* pmfs deal with this in d_t_p */
808 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
809 pp_cxx_left_paren (pp
);
810 dump_type (pp
, TYPE_OFFSET_BASETYPE (t
), flags
);
811 pp_cxx_colon_colon (pp
);
814 pp_cxx_cv_qualifier_seq (pp
, t
);
815 pp
->padding
= pp_before
;
818 /* This can be reached without a pointer when dealing with
819 templates, e.g. std::is_function. */
821 dump_type_prefix (pp
, TREE_TYPE (t
), flags
);
825 dump_type_prefix (pp
, TREE_TYPE (t
), flags
);
827 pp_cxx_left_paren (pp
);
828 dump_aggr_type (pp
, TYPE_METHOD_BASETYPE (t
), flags
);
829 pp_cxx_colon_colon (pp
);
833 dump_type_prefix (pp
, TREE_TYPE (t
), flags
);
837 case IDENTIFIER_NODE
:
842 case TEMPLATE_TYPE_PARM
:
843 case TEMPLATE_TEMPLATE_PARM
:
844 case BOUND_TEMPLATE_TEMPLATE_PARM
:
855 case UNDERLYING_TYPE
:
857 case TYPE_PACK_EXPANSION
:
858 case FIXED_POINT_TYPE
:
860 dump_type (pp
, t
, flags
);
861 pp
->padding
= pp_before
;
865 pp_unsupported_tree (pp
, t
);
868 pp_string (pp
, M_("<typeprefixerror>"));
873 /* Dump the suffix of type T, under control of FLAGS. This is the part
874 which appears after the identifier (or function parms). */
877 dump_type_suffix (cxx_pretty_printer
*pp
, tree t
, int flags
)
879 if (TYPE_PTRMEMFUNC_P (t
))
880 t
= TYPE_PTRMEMFUNC_FN_TYPE (t
);
882 switch (TREE_CODE (t
))
887 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
888 || TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
)
889 pp_cxx_right_paren (pp
);
890 if (TREE_CODE (t
) == POINTER_TYPE
)
891 flags
|= TFF_POINTER
;
892 dump_type_suffix (pp
, TREE_TYPE (t
), flags
);
899 if (TREE_CODE (t
) == METHOD_TYPE
)
900 /* Can only be reached through a pointer. */
901 pp_cxx_right_paren (pp
);
902 arg
= TYPE_ARG_TYPES (t
);
903 if (TREE_CODE (t
) == METHOD_TYPE
)
904 arg
= TREE_CHAIN (arg
);
906 /* Function pointers don't have default args. Not in standard C++,
907 anyway; they may in g++, but we'll just pretend otherwise. */
908 dump_parameters (pp
, arg
, flags
& ~TFF_FUNCTION_DEFAULT_ARGUMENTS
);
910 pp
->padding
= pp_before
;
911 pp_cxx_cv_qualifiers (pp
, type_memfn_quals (t
),
912 TREE_CODE (t
) == FUNCTION_TYPE
913 && (flags
& TFF_POINTER
));
914 dump_ref_qualifier (pp
, t
, flags
);
915 if (tx_safe_fn_type_p (t
))
916 pp_cxx_ws_string (pp
, "transaction_safe");
917 dump_exception_spec (pp
, TYPE_RAISES_EXCEPTIONS (t
), flags
);
918 dump_type_suffix (pp
, TREE_TYPE (t
), flags
);
924 pp_cxx_left_bracket (pp
);
925 if (tree dtype
= TYPE_DOMAIN (t
))
927 tree max
= TYPE_MAX_VALUE (dtype
);
928 /* Zero-length arrays have an upper bound of SIZE_MAX. */
929 if (integer_all_onesp (max
))
930 pp_character (pp
, '0');
931 else if (tree_fits_shwi_p (max
))
932 pp_wide_integer (pp
, tree_to_shwi (max
) + 1);
936 if (TREE_CODE (max
) == SAVE_EXPR
)
937 max
= TREE_OPERAND (max
, 0);
938 if (TREE_CODE (max
) == MINUS_EXPR
939 || TREE_CODE (max
) == PLUS_EXPR
)
941 max
= TREE_OPERAND (max
, 0);
942 while (CONVERT_EXPR_P (max
))
943 max
= TREE_OPERAND (max
, 0);
946 max
= fold_build2_loc (input_location
,
947 PLUS_EXPR
, dtype
, max
,
948 build_int_cst (dtype
, 1));
949 dump_expr (pp
, max
, flags
& ~TFF_EXPR_IN_PARENS
);
952 pp_cxx_right_bracket (pp
);
953 dump_type_suffix (pp
, TREE_TYPE (t
), flags
);
957 case IDENTIFIER_NODE
:
962 case TEMPLATE_TYPE_PARM
:
963 case TEMPLATE_TEMPLATE_PARM
:
964 case BOUND_TEMPLATE_TEMPLATE_PARM
:
975 case UNDERLYING_TYPE
:
977 case TYPE_PACK_EXPANSION
:
978 case FIXED_POINT_TYPE
:
983 pp_unsupported_tree (pp
, t
);
985 /* Don't mark it here, we should have already done in
992 dump_global_iord (cxx_pretty_printer
*pp
, tree t
)
994 const char *p
= NULL
;
996 if (DECL_GLOBAL_CTOR_P (t
))
997 p
= M_("(static initializers for %s)");
998 else if (DECL_GLOBAL_DTOR_P (t
))
999 p
= M_("(static destructors for %s)");
1003 pp_printf (pp
, p
, DECL_SOURCE_FILE (t
));
1007 dump_simple_decl (cxx_pretty_printer
*pp
, tree t
, tree type
, int flags
)
1009 if (template_parm_object_p (t
))
1010 return dump_expr (pp
, DECL_INITIAL (t
), flags
);
1012 if (flags
& TFF_DECL_SPECIFIERS
)
1014 if (VAR_P (t
) && DECL_DECLARED_CONSTEXPR_P (t
))
1016 if (DECL_LANG_SPECIFIC (t
) && DECL_DECLARED_CONCEPT_P (t
))
1017 pp_cxx_ws_string (pp
, "concept");
1019 pp_cxx_ws_string (pp
, "constexpr");
1021 dump_type_prefix (pp
, type
, flags
& ~TFF_UNQUALIFIED_NAME
);
1022 pp_maybe_space (pp
);
1024 if (! (flags
& TFF_UNQUALIFIED_NAME
)
1025 && TREE_CODE (t
) != PARM_DECL
1026 && (!DECL_INITIAL (t
)
1027 || TREE_CODE (DECL_INITIAL (t
)) != TEMPLATE_PARM_INDEX
))
1028 dump_scope (pp
, CP_DECL_CONTEXT (t
), flags
);
1029 flags
&= ~TFF_UNQUALIFIED_NAME
;
1030 if ((flags
& TFF_DECL_SPECIFIERS
)
1031 && DECL_TEMPLATE_PARM_P (t
)
1032 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (t
)))
1033 pp_string (pp
, "...");
1036 if (TREE_CODE (t
) == FIELD_DECL
&& DECL_NORMAL_CAPTURE_P (t
))
1039 pp_string (pp
, IDENTIFIER_POINTER (DECL_NAME (t
)) + 2);
1040 pp_string (pp
, " capture>");
1043 dump_decl (pp
, DECL_NAME (t
), flags
);
1045 else if (DECL_DECOMPOSITION_P (t
))
1046 pp_string (pp
, M_("<structured bindings>"));
1048 pp_string (pp
, M_("<anonymous>"));
1049 if (flags
& TFF_DECL_SPECIFIERS
)
1050 dump_type_suffix (pp
, type
, flags
);
1053 /* Print an IDENTIFIER_NODE that is the name of a declaration. */
1056 dump_decl_name (cxx_pretty_printer
*pp
, tree t
, int flags
)
1058 /* These special cases are duplicated here so that other functions
1059 can feed identifiers to error and get them demangled properly. */
1060 if (IDENTIFIER_CONV_OP_P (t
))
1062 pp_cxx_ws_string (pp
, "operator");
1063 /* Not exactly IDENTIFIER_TYPE_VALUE. */
1064 dump_type (pp
, TREE_TYPE (t
), flags
);
1067 if (dguide_name_p (t
))
1069 dump_decl (pp
, CLASSTYPE_TI_TEMPLATE (TREE_TYPE (t
)),
1070 TFF_UNQUALIFIED_NAME
);
1074 const char *str
= IDENTIFIER_POINTER (t
);
1075 if (!strncmp (str
, "_ZGR", 3))
1077 pp_cxx_ws_string (pp
, "<temporary>");
1081 pp_cxx_tree_identifier (pp
, t
);
1084 /* Dump a human readable string for the decl T under control of FLAGS. */
1087 dump_decl (cxx_pretty_printer
*pp
, tree t
, int flags
)
1092 /* If doing Objective-C++, give Objective-C a chance to demangle
1093 Objective-C method names. */
1094 if (c_dialect_objc ())
1096 const char *demangled
= objc_maybe_printable_name (t
, flags
);
1099 pp_string (pp
, demangled
);
1104 switch (TREE_CODE (t
))
1107 /* Don't say 'typedef class A' */
1108 if (DECL_ARTIFICIAL (t
) && !DECL_SELF_REFERENCE_P (t
))
1110 if ((flags
& TFF_DECL_SPECIFIERS
)
1111 && TREE_CODE (TREE_TYPE (t
)) == TEMPLATE_TYPE_PARM
)
1113 /* Say `class T' not just `T'. */
1114 pp_cxx_ws_string (pp
, "class");
1116 /* Emit the `...' for a parameter pack. */
1117 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t
)))
1118 pp_cxx_ws_string (pp
, "...");
1121 dump_type (pp
, TREE_TYPE (t
), flags
);
1124 if (TYPE_DECL_ALIAS_P (t
)
1125 && (flags
& TFF_DECL_SPECIFIERS
1126 || flags
& TFF_CLASS_KEY_OR_ENUM
))
1128 pp_cxx_ws_string (pp
, "using");
1129 dump_decl (pp
, DECL_NAME (t
), flags
);
1130 pp_cxx_whitespace (pp
);
1131 pp_cxx_ws_string (pp
, "=");
1132 pp_cxx_whitespace (pp
);
1133 dump_type (pp
, (DECL_ORIGINAL_TYPE (t
)
1134 ? DECL_ORIGINAL_TYPE (t
) : TREE_TYPE (t
)),
1138 if ((flags
& TFF_DECL_SPECIFIERS
)
1139 && !DECL_SELF_REFERENCE_P (t
))
1140 pp_cxx_ws_string (pp
, "typedef");
1141 dump_simple_decl (pp
, t
, DECL_ORIGINAL_TYPE (t
)
1142 ? DECL_ORIGINAL_TYPE (t
) : TREE_TYPE (t
),
1147 if (DECL_NAME (t
) && VTABLE_NAME_P (DECL_NAME (t
)))
1149 pp_string (pp
, M_("vtable for "));
1150 gcc_assert (TYPE_P (DECL_CONTEXT (t
)));
1151 dump_type (pp
, DECL_CONTEXT (t
), flags
);
1157 dump_simple_decl (pp
, t
, TREE_TYPE (t
), flags
);
1159 /* Handle variable template specializations. */
1161 && DECL_LANG_SPECIFIC (t
)
1162 && DECL_TEMPLATE_INFO (t
)
1163 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)))
1165 pp_cxx_begin_template_argument_list (pp
);
1166 tree args
= INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t
));
1167 dump_template_argument_list (pp
, args
, flags
);
1168 pp_cxx_end_template_argument_list (pp
);
1173 pp_string (pp
, M_("<return value> "));
1174 dump_simple_decl (pp
, t
, TREE_TYPE (t
), flags
);
1177 case NAMESPACE_DECL
:
1178 if (flags
& TFF_DECL_SPECIFIERS
)
1179 pp
->declaration (t
);
1182 if (! (flags
& TFF_UNQUALIFIED_NAME
))
1183 dump_scope (pp
, CP_DECL_CONTEXT (t
), flags
);
1184 flags
&= ~TFF_UNQUALIFIED_NAME
;
1185 if (DECL_NAME (t
) == NULL_TREE
)
1187 if (!(pp
->flags
& pp_c_flag_gnu_v3
))
1188 pp_cxx_ws_string (pp
, M_("{anonymous}"));
1190 pp_cxx_ws_string (pp
, M_("(anonymous namespace)"));
1193 pp_cxx_tree_identifier (pp
, DECL_NAME (t
));
1198 dump_type (pp
, TREE_OPERAND (t
, 0), flags
);
1199 pp_cxx_colon_colon (pp
);
1200 dump_decl (pp
, TREE_OPERAND (t
, 1), TFF_UNQUALIFIED_NAME
);
1204 dump_decl (pp
, TREE_OPERAND (t
, 0), flags
);
1205 pp_cxx_left_bracket (pp
);
1206 dump_decl (pp
, TREE_OPERAND (t
, 1), flags
);
1207 pp_cxx_right_bracket (pp
);
1210 /* So that we can do dump_decl on an aggr type. */
1214 dump_type (pp
, t
, flags
);
1218 /* This is a pseudo destructor call which has not been folded into
1219 a PSEUDO_DTOR_EXPR yet. */
1220 pp_cxx_complement (pp
);
1221 dump_type (pp
, TREE_OPERAND (t
, 0), flags
);
1228 case IDENTIFIER_NODE
:
1229 dump_decl_name (pp
, t
, flags
);
1233 if (!OVL_SINGLE_P (t
))
1235 tree ctx
= ovl_scope (t
);
1236 if (ctx
!= global_namespace
)
1239 dump_type (pp
, ctx
, flags
);
1241 dump_decl (pp
, ctx
, flags
);
1242 pp_cxx_colon_colon (pp
);
1244 dump_decl (pp
, OVL_NAME (t
), flags
);
1248 /* If there's only one function, just treat it like an ordinary
1254 if (! DECL_LANG_SPECIFIC (t
))
1256 if (DECL_ABSTRACT_ORIGIN (t
)
1257 && DECL_ABSTRACT_ORIGIN (t
) != t
)
1258 dump_decl (pp
, DECL_ABSTRACT_ORIGIN (t
), flags
);
1260 dump_function_name (pp
, t
, flags
);
1262 else if (DECL_GLOBAL_CTOR_P (t
) || DECL_GLOBAL_DTOR_P (t
))
1263 dump_global_iord (pp
, t
);
1265 dump_function_decl (pp
, t
, flags
);
1269 dump_template_decl (pp
, t
, flags
);
1272 case TEMPLATE_ID_EXPR
:
1274 tree name
= TREE_OPERAND (t
, 0);
1275 tree args
= TREE_OPERAND (t
, 1);
1277 if (!identifier_p (name
))
1278 name
= OVL_NAME (name
);
1279 dump_decl (pp
, name
, flags
);
1280 pp_cxx_begin_template_argument_list (pp
);
1281 if (args
== error_mark_node
)
1282 pp_string (pp
, M_("<template arguments error>"));
1284 dump_template_argument_list
1285 (pp
, args
, flags
|TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS
);
1286 pp_cxx_end_template_argument_list (pp
);
1291 pp_cxx_tree_identifier (pp
, DECL_NAME (t
));
1295 if ((TREE_TYPE (t
) != NULL_TREE
&& NEXT_CODE (t
) == ENUMERAL_TYPE
)
1296 || (DECL_INITIAL (t
) &&
1297 TREE_CODE (DECL_INITIAL (t
)) == TEMPLATE_PARM_INDEX
))
1298 dump_simple_decl (pp
, t
, TREE_TYPE (t
), flags
);
1299 else if (DECL_NAME (t
))
1300 dump_decl (pp
, DECL_NAME (t
), flags
);
1301 else if (DECL_INITIAL (t
))
1302 dump_expr (pp
, DECL_INITIAL (t
), flags
| TFF_EXPR_IN_PARENS
);
1304 pp_string (pp
, M_("<enumerator>"));
1309 pp_cxx_ws_string (pp
, "using");
1310 tree scope
= USING_DECL_SCOPE (t
);
1311 bool variadic
= false;
1312 if (PACK_EXPANSION_P (scope
))
1314 scope
= PACK_EXPANSION_PATTERN (scope
);
1317 dump_type (pp
, scope
, flags
);
1318 pp_cxx_colon_colon (pp
);
1319 dump_decl (pp
, DECL_NAME (t
), flags
);
1321 pp_cxx_ws_string (pp
, "...");
1326 pp
->declaration (t
);
1330 dump_decl (pp
, BASELINK_FUNCTIONS (t
), flags
);
1333 case NON_DEPENDENT_EXPR
:
1334 dump_expr (pp
, t
, flags
);
1337 case TEMPLATE_TYPE_PARM
:
1338 if (flags
& TFF_DECL_SPECIFIERS
)
1339 pp
->declaration (t
);
1344 case UNBOUND_CLASS_TEMPLATE
:
1345 case TYPE_PACK_EXPANSION
:
1347 dump_type (pp
, t
, flags
);
1351 pp_unsupported_tree (pp
, t
);
1355 pp_string (pp
, M_("<declaration error>"));
1360 /* Dump a template declaration T under control of FLAGS. This means the
1361 'template <...> leaders plus the 'class X' or 'void fn(...)' part. */
1364 dump_template_decl (cxx_pretty_printer
*pp
, tree t
, int flags
)
1366 tree orig_parms
= DECL_TEMPLATE_PARMS (t
);
1370 if (flags
& TFF_TEMPLATE_HEADER
)
1372 for (parms
= orig_parms
= nreverse (orig_parms
);
1374 parms
= TREE_CHAIN (parms
))
1376 tree inner_parms
= INNERMOST_TEMPLATE_PARMS (parms
);
1377 int len
= TREE_VEC_LENGTH (inner_parms
);
1381 /* Skip over the dummy template levels of a template template
1383 gcc_assert (TREE_CODE (TREE_TYPE (t
)) == TEMPLATE_TEMPLATE_PARM
);
1387 pp_cxx_ws_string (pp
, "template");
1388 pp_cxx_begin_template_argument_list (pp
);
1390 /* If we've shown the template prefix, we'd better show the
1391 parameters' and decl's type too. */
1392 flags
|= TFF_DECL_SPECIFIERS
;
1394 for (i
= 0; i
< len
; i
++)
1397 pp_separate_with_comma (pp
);
1398 dump_template_parameter (pp
, TREE_VEC_ELT (inner_parms
, i
),
1401 pp_cxx_end_template_argument_list (pp
);
1402 pp_cxx_whitespace (pp
);
1404 nreverse(orig_parms
);
1406 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
1408 /* Say `template<arg> class TT' not just `template<arg> TT'. */
1409 pp_cxx_ws_string (pp
, "class");
1411 /* If this is a parameter pack, print the ellipsis. */
1412 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t
)))
1413 pp_cxx_ws_string (pp
, "...");
1416 /* Only print the requirements if we're also printing
1417 the template header. */
1419 if (tree ci
= get_constraints (t
))
1420 if (check_constraint_info (ci
))
1421 if (tree reqs
= CI_TEMPLATE_REQS (ci
))
1423 pp_cxx_requires_clause (pp
, reqs
);
1424 pp_cxx_whitespace (pp
);
1429 if (DECL_CLASS_TEMPLATE_P (t
))
1430 dump_type (pp
, TREE_TYPE (t
),
1431 ((flags
& ~TFF_CLASS_KEY_OR_ENUM
) | TFF_TEMPLATE_NAME
1432 | (flags
& TFF_DECL_SPECIFIERS
? TFF_CLASS_KEY_OR_ENUM
: 0)));
1433 else if (DECL_TEMPLATE_RESULT (t
)
1434 && (VAR_P (DECL_TEMPLATE_RESULT (t
))
1435 /* Alias template. */
1436 || DECL_TYPE_TEMPLATE_P (t
)))
1437 dump_decl (pp
, DECL_TEMPLATE_RESULT (t
), flags
| TFF_TEMPLATE_NAME
);
1440 gcc_assert (TREE_TYPE (t
));
1441 switch (NEXT_CODE (t
))
1445 dump_function_decl (pp
, t
, flags
| TFF_TEMPLATE_NAME
);
1448 /* This case can occur with some invalid code. */
1449 dump_type (pp
, TREE_TYPE (t
),
1450 (flags
& ~TFF_CLASS_KEY_OR_ENUM
) | TFF_TEMPLATE_NAME
1451 | (flags
& TFF_DECL_SPECIFIERS
1452 ? TFF_CLASS_KEY_OR_ENUM
: 0));
1457 /* find_typenames looks through the type of the function template T
1458 and returns a vec containing any typedefs, decltypes or TYPENAME_TYPEs
1461 struct find_typenames_t
1463 hash_set
<tree
> *p_set
;
1464 vec
<tree
, va_gc
> *typenames
;
1468 find_typenames_r (tree
*tp
, int *walk_subtrees
, void *data
)
1470 struct find_typenames_t
*d
= (struct find_typenames_t
*)data
;
1471 tree mv
= NULL_TREE
;
1473 if (TYPE_P (*tp
) && is_typedef_decl (TYPE_NAME (*tp
)))
1474 /* Add the type of the typedef without any additional cv-quals. */
1475 mv
= TREE_TYPE (TYPE_NAME (*tp
));
1476 else if (TREE_CODE (*tp
) == TYPENAME_TYPE
1477 || TREE_CODE (*tp
) == DECLTYPE_TYPE
)
1478 /* Add the typename without any cv-qualifiers. */
1479 mv
= TYPE_MAIN_VARIANT (*tp
);
1481 if (PACK_EXPANSION_P (*tp
))
1483 /* Don't mess with parameter packs since we don't remember
1484 the pack expansion context for a particular typename. */
1485 *walk_subtrees
= false;
1489 if (mv
&& (mv
== *tp
|| !d
->p_set
->add (mv
)))
1490 vec_safe_push (d
->typenames
, mv
);
1492 /* Search into class template arguments, which cp_walk_subtrees
1494 if (CLASS_TYPE_P (*tp
) && CLASSTYPE_TEMPLATE_INFO (*tp
))
1495 cp_walk_tree (&CLASSTYPE_TI_ARGS (*tp
), find_typenames_r
,
1501 static vec
<tree
, va_gc
> *
1502 find_typenames (tree t
)
1504 struct find_typenames_t ft
;
1505 ft
.p_set
= new hash_set
<tree
>;
1506 ft
.typenames
= NULL
;
1507 cp_walk_tree (&TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
1508 find_typenames_r
, &ft
, ft
.p_set
);
1510 return ft
.typenames
;
1513 /* Output the "[with ...]" clause for a template instantiation T iff
1514 TEMPLATE_PARMS, TEMPLATE_ARGS and FLAGS are suitable. T may be NULL if
1515 formatting a deduction/substitution diagnostic rather than an
1519 dump_substitution (cxx_pretty_printer
*pp
,
1520 tree t
, tree template_parms
, tree template_args
,
1523 if (template_parms
!= NULL_TREE
&& template_args
!= NULL_TREE
1524 && !(flags
& TFF_NO_TEMPLATE_BINDINGS
))
1526 vec
<tree
, va_gc
> *typenames
= t
? find_typenames (t
) : NULL
;
1527 pp_cxx_whitespace (pp
);
1528 pp_cxx_left_bracket (pp
);
1529 pp
->translate_string ("with");
1530 pp_cxx_whitespace (pp
);
1531 dump_template_bindings (pp
, template_parms
, template_args
, typenames
);
1532 pp_cxx_right_bracket (pp
);
1536 /* Dump the lambda function FN including its 'mutable' qualifier and any
1537 template bindings. */
1540 dump_lambda_function (cxx_pretty_printer
*pp
,
1541 tree fn
, tree template_parms
, tree template_args
,
1544 /* A lambda's signature is essentially its "type". */
1545 dump_type (pp
, DECL_CONTEXT (fn
), flags
);
1546 if (!(TYPE_QUALS (class_of_this_parm (TREE_TYPE (fn
))) & TYPE_QUAL_CONST
))
1548 pp
->padding
= pp_before
;
1549 pp_c_ws_string (pp
, "mutable");
1551 dump_substitution (pp
, fn
, template_parms
, template_args
, flags
);
1554 /* Pretty print a function decl. There are several ways we want to print a
1555 function declaration. The TFF_ bits in FLAGS tells us how to behave.
1556 As error can only apply the '#' flag once to give 0 and 1 for V, there
1557 is %D which doesn't print the throw specs, and %F which does. */
1560 dump_function_decl (cxx_pretty_printer
*pp
, tree t
, int flags
)
1564 tree cname
= NULL_TREE
;
1565 tree template_args
= NULL_TREE
;
1566 tree template_parms
= NULL_TREE
;
1567 int show_return
= flags
& TFF_RETURN_TYPE
|| flags
& TFF_DECL_SPECIFIERS
;
1568 int do_outer_scope
= ! (flags
& TFF_UNQUALIFIED_NAME
);
1571 tree ret
= NULL_TREE
;
1573 flags
&= ~(TFF_UNQUALIFIED_NAME
| TFF_TEMPLATE_NAME
);
1574 if (TREE_CODE (t
) == TEMPLATE_DECL
)
1575 t
= DECL_TEMPLATE_RESULT (t
);
1577 /* Save the exceptions, in case t is a specialization and we are
1578 emitting an error about incompatible specifications. */
1579 exceptions
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (t
));
1581 /* Likewise for the constexpr specifier, in case t is a specialization. */
1582 constexpr_p
= DECL_DECLARED_CONSTEXPR_P (t
);
1584 /* Pretty print template instantiations only. */
1585 if (DECL_USE_TEMPLATE (t
) && DECL_TEMPLATE_INFO (t
)
1586 && !(flags
& TFF_NO_TEMPLATE_BINDINGS
)
1587 && flag_pretty_templates
)
1591 template_args
= DECL_TI_ARGS (t
);
1592 tmpl
= most_general_template (t
);
1593 if (tmpl
&& TREE_CODE (tmpl
) == TEMPLATE_DECL
)
1595 template_parms
= DECL_TEMPLATE_PARMS (tmpl
);
1600 if (DECL_NAME (t
) && LAMBDA_FUNCTION_P (t
))
1601 return dump_lambda_function (pp
, t
, template_parms
, template_args
, flags
);
1603 fntype
= TREE_TYPE (t
);
1604 parmtypes
= FUNCTION_FIRST_USER_PARMTYPE (t
);
1606 if (DECL_CLASS_SCOPE_P (t
))
1607 cname
= DECL_CONTEXT (t
);
1608 /* This is for partially instantiated template methods. */
1609 else if (TREE_CODE (fntype
) == METHOD_TYPE
)
1610 cname
= TREE_TYPE (TREE_VALUE (parmtypes
));
1612 if (flags
& TFF_DECL_SPECIFIERS
)
1614 if (DECL_STATIC_FUNCTION_P (t
))
1615 pp_cxx_ws_string (pp
, "static");
1616 else if (DECL_VIRTUAL_P (t
))
1617 pp_cxx_ws_string (pp
, "virtual");
1621 if (DECL_DECLARED_CONCEPT_P (t
))
1622 pp_cxx_ws_string (pp
, "concept");
1624 pp_cxx_ws_string (pp
, "constexpr");
1628 /* Print the return type? */
1630 show_return
= (!DECL_CONV_FN_P (t
) && !DECL_CONSTRUCTOR_P (t
)
1631 && !DECL_DESTRUCTOR_P (t
) && !deduction_guide_p (t
));
1634 ret
= fndecl_declared_return_type (t
);
1635 dump_type_prefix (pp
, ret
, flags
);
1638 /* Print the function name. */
1639 if (!do_outer_scope
)
1643 dump_type (pp
, cname
, flags
);
1644 pp_cxx_colon_colon (pp
);
1647 dump_scope (pp
, CP_DECL_CONTEXT (t
), flags
);
1649 dump_function_name (pp
, t
, flags
);
1651 if (!(flags
& TFF_NO_FUNCTION_ARGUMENTS
))
1653 dump_parameters (pp
, parmtypes
, flags
);
1655 if (TREE_CODE (fntype
) == METHOD_TYPE
)
1657 pp
->padding
= pp_before
;
1658 pp_cxx_cv_qualifier_seq (pp
, class_of_this_parm (fntype
));
1659 dump_ref_qualifier (pp
, fntype
, flags
);
1662 if (tx_safe_fn_type_p (fntype
))
1664 pp
->padding
= pp_before
;
1665 pp_cxx_ws_string (pp
, "transaction_safe");
1668 if (flags
& TFF_EXCEPTION_SPECIFICATION
)
1670 pp
->padding
= pp_before
;
1671 dump_exception_spec (pp
, exceptions
, flags
);
1675 dump_type_suffix (pp
, ret
, flags
);
1676 else if (deduction_guide_p (t
))
1678 pp_cxx_ws_string (pp
, "->");
1679 dump_type (pp
, TREE_TYPE (TREE_TYPE (t
)), flags
);
1683 if (tree ci
= get_constraints (t
))
1684 if (tree reqs
= CI_DECLARATOR_REQS (ci
))
1685 pp_cxx_requires_clause (pp
, reqs
);
1687 dump_substitution (pp
, t
, template_parms
, template_args
, flags
);
1689 if (tree base
= DECL_INHERITED_CTOR_BASE (t
))
1691 pp_cxx_ws_string (pp
, "[inherited from");
1692 dump_type (pp
, base
, TFF_PLAIN_IDENTIFIER
);
1693 pp_character (pp
, ']');
1696 else if (template_args
)
1698 bool need_comma
= false;
1700 pp_cxx_begin_template_argument_list (pp
);
1701 template_args
= INNERMOST_TEMPLATE_ARGS (template_args
);
1702 for (i
= 0; i
< TREE_VEC_LENGTH (template_args
); ++i
)
1704 tree arg
= TREE_VEC_ELT (template_args
, i
);
1706 pp_separate_with_comma (pp
);
1707 if (ARGUMENT_PACK_P (arg
))
1708 pp_cxx_left_brace (pp
);
1709 dump_template_argument (pp
, arg
, TFF_PLAIN_IDENTIFIER
);
1710 if (ARGUMENT_PACK_P (arg
))
1711 pp_cxx_right_brace (pp
);
1714 pp_cxx_end_template_argument_list (pp
);
1718 /* Print a parameter list. If this is for a member function, the
1719 member object ptr (and any other hidden args) should have
1720 already been removed. */
1723 dump_parameters (cxx_pretty_printer
*pp
, tree parmtypes
, int flags
)
1726 flags
&= ~TFF_SCOPE
;
1727 pp_cxx_left_paren (pp
);
1729 for (first
= 1; parmtypes
!= void_list_node
;
1730 parmtypes
= TREE_CHAIN (parmtypes
))
1733 pp_separate_with_comma (pp
);
1737 pp_cxx_ws_string (pp
, "...");
1741 dump_type (pp
, TREE_VALUE (parmtypes
), flags
);
1743 if ((flags
& TFF_FUNCTION_DEFAULT_ARGUMENTS
) && TREE_PURPOSE (parmtypes
))
1745 pp_cxx_whitespace (pp
);
1747 pp_cxx_whitespace (pp
);
1748 dump_expr (pp
, TREE_PURPOSE (parmtypes
), flags
| TFF_EXPR_IN_PARENS
);
1752 pp_cxx_right_paren (pp
);
1755 /* Print ref-qualifier of a FUNCTION_TYPE or METHOD_TYPE. FLAGS are ignored. */
1758 dump_ref_qualifier (cxx_pretty_printer
*pp
, tree t
, int flags ATTRIBUTE_UNUSED
)
1760 if (FUNCTION_REF_QUALIFIED (t
))
1762 pp
->padding
= pp_before
;
1763 if (FUNCTION_RVALUE_QUALIFIED (t
))
1764 pp_cxx_ws_string (pp
, "&&");
1766 pp_cxx_ws_string (pp
, "&");
1770 /* Print an exception specification. T is the exception specification. */
1773 dump_exception_spec (cxx_pretty_printer
*pp
, tree t
, int flags
)
1775 if (t
&& TREE_PURPOSE (t
))
1777 pp_cxx_ws_string (pp
, "noexcept");
1778 if (!integer_onep (TREE_PURPOSE (t
)))
1780 pp_cxx_whitespace (pp
);
1781 pp_cxx_left_paren (pp
);
1782 if (DEFERRED_NOEXCEPT_SPEC_P (t
))
1783 pp_cxx_ws_string (pp
, "<uninstantiated>");
1785 dump_expr (pp
, TREE_PURPOSE (t
), flags
);
1786 pp_cxx_right_paren (pp
);
1791 pp_cxx_ws_string (pp
, "throw");
1792 pp_cxx_whitespace (pp
);
1793 pp_cxx_left_paren (pp
);
1794 if (TREE_VALUE (t
) != NULL_TREE
)
1797 dump_type (pp
, TREE_VALUE (t
), flags
);
1801 pp_separate_with_comma (pp
);
1803 pp_cxx_right_paren (pp
);
1807 /* Handle the function name for a FUNCTION_DECL node, grokking operators
1808 and destructors properly. */
1811 dump_function_name (cxx_pretty_printer
*pp
, tree t
, int flags
)
1813 tree name
= DECL_NAME (t
);
1815 /* We can get here with a decl that was synthesized by language-
1816 independent machinery (e.g. coverage.c) in which case it won't
1817 have a lang_specific structure attached and DECL_CONSTRUCTOR_P
1818 will crash. In this case it is safe just to print out the
1820 if (!DECL_LANG_SPECIFIC (t
))
1822 pp_cxx_tree_identifier (pp
, name
);
1826 if (TREE_CODE (t
) == TEMPLATE_DECL
)
1827 t
= DECL_TEMPLATE_RESULT (t
);
1829 /* Don't let the user see __comp_ctor et al. */
1830 if (DECL_CONSTRUCTOR_P (t
)
1831 || DECL_DESTRUCTOR_P (t
))
1833 if (LAMBDA_TYPE_P (DECL_CONTEXT (t
)))
1834 name
= get_identifier ("<lambda>");
1835 else if (TYPE_UNNAMED_P (DECL_CONTEXT (t
)))
1836 name
= get_identifier ("<constructor>");
1838 name
= constructor_name (DECL_CONTEXT (t
));
1841 if (DECL_DESTRUCTOR_P (t
))
1843 pp_cxx_complement (pp
);
1844 dump_decl (pp
, name
, TFF_PLAIN_IDENTIFIER
);
1846 else if (DECL_CONV_FN_P (t
))
1848 /* This cannot use the hack that the operator's return
1849 type is stashed off of its name because it may be
1850 used for error reporting. In the case of conflicting
1851 declarations, both will have the same name, yet
1852 the types will be different, hence the TREE_TYPE field
1853 of the first name will be clobbered by the second. */
1854 pp_cxx_ws_string (pp
, "operator");
1855 dump_type (pp
, TREE_TYPE (TREE_TYPE (t
)), flags
);
1858 dump_decl (pp
, name
, flags
);
1860 if (DECL_TEMPLATE_INFO (t
)
1861 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (t
)
1862 && (TREE_CODE (DECL_TI_TEMPLATE (t
)) != TEMPLATE_DECL
1863 || PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
1864 dump_template_parms (pp
, DECL_TEMPLATE_INFO (t
), !DECL_USE_TEMPLATE (t
),
1868 /* Dump the template parameters from the template info INFO under control of
1869 FLAGS. PRIMARY indicates whether this is a primary template decl, or
1870 specialization (partial or complete). For partial specializations we show
1871 the specialized parameter values. For a primary template we show no
1875 dump_template_parms (cxx_pretty_printer
*pp
, tree info
,
1876 int primary
, int flags
)
1878 tree args
= info
? TI_ARGS (info
) : NULL_TREE
;
1880 if (primary
&& flags
& TFF_TEMPLATE_NAME
)
1882 flags
&= ~(TFF_CLASS_KEY_OR_ENUM
| TFF_TEMPLATE_NAME
);
1883 pp_cxx_begin_template_argument_list (pp
);
1885 /* Be careful only to print things when we have them, so as not
1886 to crash producing error messages. */
1887 if (args
&& !primary
)
1890 len
= get_non_default_template_args_count (args
, flags
);
1892 args
= INNERMOST_TEMPLATE_ARGS (args
);
1893 for (ix
= 0; ix
!= len
; ix
++)
1895 tree arg
= TREE_VEC_ELT (args
, ix
);
1897 /* Only print a comma if we know there is an argument coming. In
1898 the case of an empty template argument pack, no actual
1899 argument will be printed. */
1901 && (!ARGUMENT_PACK_P (arg
)
1902 || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) > 0))
1903 pp_separate_with_comma (pp
);
1906 pp_string (pp
, M_("<template parameter error>"));
1908 dump_template_argument (pp
, arg
, flags
);
1913 tree tpl
= TI_TEMPLATE (info
);
1914 tree parms
= DECL_TEMPLATE_PARMS (tpl
);
1917 parms
= TREE_CODE (parms
) == TREE_LIST
? TREE_VALUE (parms
) : NULL_TREE
;
1918 len
= parms
? TREE_VEC_LENGTH (parms
) : 0;
1920 for (ix
= 0; ix
!= len
; ix
++)
1924 if (TREE_VEC_ELT (parms
, ix
) == error_mark_node
)
1926 pp_string (pp
, M_("<template parameter error>"));
1930 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, ix
));
1933 pp_separate_with_comma (pp
);
1935 dump_decl (pp
, parm
, flags
& ~TFF_DECL_SPECIFIERS
);
1938 pp_cxx_end_template_argument_list (pp
);
1941 /* Print out the arguments of CALL_EXPR T as a parenthesized list using
1942 flags FLAGS. Skip over the first argument if SKIPFIRST is true. */
1945 dump_call_expr_args (cxx_pretty_printer
*pp
, tree t
, int flags
, bool skipfirst
)
1948 call_expr_arg_iterator iter
;
1950 pp_cxx_left_paren (pp
);
1951 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, t
)
1957 dump_expr (pp
, arg
, flags
| TFF_EXPR_IN_PARENS
);
1958 if (more_call_expr_args_p (&iter
))
1959 pp_separate_with_comma (pp
);
1962 pp_cxx_right_paren (pp
);
1965 /* Print out the arguments of AGGR_INIT_EXPR T as a parenthesized list
1966 using flags FLAGS. Skip over the first argument if SKIPFIRST is
1970 dump_aggr_init_expr_args (cxx_pretty_printer
*pp
, tree t
, int flags
,
1974 aggr_init_expr_arg_iterator iter
;
1976 pp_cxx_left_paren (pp
);
1977 FOR_EACH_AGGR_INIT_EXPR_ARG (arg
, iter
, t
)
1983 dump_expr (pp
, arg
, flags
| TFF_EXPR_IN_PARENS
);
1984 if (more_aggr_init_expr_args_p (&iter
))
1985 pp_separate_with_comma (pp
);
1988 pp_cxx_right_paren (pp
);
1991 /* Print out a list of initializers (subr of dump_expr). */
1994 dump_expr_list (cxx_pretty_printer
*pp
, tree l
, int flags
)
1998 dump_expr (pp
, TREE_VALUE (l
), flags
| TFF_EXPR_IN_PARENS
);
2001 pp_separate_with_comma (pp
);
2005 /* Print out a vector of initializers (subr of dump_expr). */
2008 dump_expr_init_vec (cxx_pretty_printer
*pp
, vec
<constructor_elt
, va_gc
> *v
,
2011 unsigned HOST_WIDE_INT idx
;
2014 FOR_EACH_CONSTRUCTOR_VALUE (v
, idx
, value
)
2016 dump_expr (pp
, value
, flags
| TFF_EXPR_IN_PARENS
);
2017 if (idx
!= v
->length () - 1)
2018 pp_separate_with_comma (pp
);
2023 /* We've gotten an indirect REFERENCE (an OBJ_TYPE_REF) to a virtual
2024 function. Resolve it to a close relative -- in the sense of static
2025 type -- variant being overridden. That is close to what was written in
2026 the source code. Subroutine of dump_expr. */
2029 resolve_virtual_fun_from_obj_type_ref (tree ref
)
2031 tree obj_type
= TREE_TYPE (OBJ_TYPE_REF_OBJECT (ref
));
2032 HOST_WIDE_INT index
= tree_to_uhwi (OBJ_TYPE_REF_TOKEN (ref
));
2033 tree fun
= BINFO_VIRTUALS (TYPE_BINFO (TREE_TYPE (obj_type
)));
2036 fun
= TREE_CHAIN (fun
);
2037 index
-= (TARGET_VTABLE_USES_DESCRIPTORS
2038 ? TARGET_VTABLE_USES_DESCRIPTORS
: 1);
2044 /* Print out an expression E under control of FLAGS. */
2047 dump_expr (cxx_pretty_printer
*pp
, tree t
, int flags
)
2054 if (STATEMENT_CLASS_P (t
))
2056 pp_cxx_ws_string (pp
, M_("<statement>"));
2060 switch (TREE_CODE (t
))
2068 case NAMESPACE_DECL
:
2072 case IDENTIFIER_NODE
:
2073 dump_decl (pp
, t
, ((flags
& ~(TFF_DECL_SPECIFIERS
|TFF_RETURN_TYPE
2074 |TFF_TEMPLATE_HEADER
))
2075 | TFF_NO_TEMPLATE_BINDINGS
2076 | TFF_NO_FUNCTION_ARGUMENTS
));
2080 if (SSA_NAME_VAR (t
)
2081 && !DECL_ARTIFICIAL (SSA_NAME_VAR (t
)))
2082 dump_expr (pp
, SSA_NAME_VAR (t
), flags
);
2084 pp_cxx_ws_string (pp
, M_("<unknown>"));
2095 case USERDEF_LITERAL
:
2096 pp_cxx_userdef_literal (pp
, t
);
2100 /* While waiting for caret diagnostics, avoid printing
2101 __cxa_allocate_exception, __cxa_throw, and the like. */
2102 pp_cxx_ws_string (pp
, M_("<throw-expression>"));
2107 dump_type (pp
, PTRMEM_CST_CLASS (t
), flags
);
2108 pp_cxx_colon_colon (pp
);
2109 pp_cxx_tree_identifier (pp
, DECL_NAME (PTRMEM_CST_MEMBER (t
)));
2113 pp_cxx_left_paren (pp
);
2114 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
| TFF_EXPR_IN_PARENS
);
2115 pp_separate_with_comma (pp
);
2116 dump_expr (pp
, TREE_OPERAND (t
, 1), flags
| TFF_EXPR_IN_PARENS
);
2117 pp_cxx_right_paren (pp
);
2122 pp_cxx_left_paren (pp
);
2123 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
| TFF_EXPR_IN_PARENS
);
2124 pp_string (pp
, " ? ");
2125 dump_expr (pp
, TREE_OPERAND (t
, 1), flags
| TFF_EXPR_IN_PARENS
);
2126 pp_string (pp
, " : ");
2127 dump_expr (pp
, TREE_OPERAND (t
, 2), flags
| TFF_EXPR_IN_PARENS
);
2128 pp_cxx_right_paren (pp
);
2132 if (TREE_HAS_CONSTRUCTOR (t
))
2134 pp_cxx_ws_string (pp
, "new");
2135 pp_cxx_whitespace (pp
);
2136 dump_type (pp
, TREE_TYPE (TREE_TYPE (t
)), flags
);
2139 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
| TFF_EXPR_IN_PARENS
);
2142 case AGGR_INIT_EXPR
:
2144 tree fn
= NULL_TREE
;
2146 if (TREE_CODE (AGGR_INIT_EXPR_FN (t
)) == ADDR_EXPR
)
2147 fn
= TREE_OPERAND (AGGR_INIT_EXPR_FN (t
), 0);
2149 if (fn
&& TREE_CODE (fn
) == FUNCTION_DECL
)
2151 if (DECL_CONSTRUCTOR_P (fn
))
2152 dump_type (pp
, DECL_CONTEXT (fn
), flags
);
2154 dump_decl (pp
, fn
, 0);
2157 dump_expr (pp
, AGGR_INIT_EXPR_FN (t
), 0);
2159 dump_aggr_init_expr_args (pp
, t
, flags
, true);
2164 tree fn
= CALL_EXPR_FN (t
);
2165 bool skipfirst
= false;
2167 /* Deal with internal functions. */
2168 if (fn
== NULL_TREE
)
2170 pp_string (pp
, internal_fn_name (CALL_EXPR_IFN (t
)));
2171 dump_call_expr_args (pp
, t
, flags
, skipfirst
);
2175 if (TREE_CODE (fn
) == ADDR_EXPR
)
2176 fn
= TREE_OPERAND (fn
, 0);
2178 /* Nobody is interested in seeing the guts of vcalls. */
2179 if (TREE_CODE (fn
) == OBJ_TYPE_REF
)
2180 fn
= resolve_virtual_fun_from_obj_type_ref (fn
);
2182 if (TREE_TYPE (fn
) != NULL_TREE
2183 && NEXT_CODE (fn
) == METHOD_TYPE
2184 && call_expr_nargs (t
))
2186 tree ob
= CALL_EXPR_ARG (t
, 0);
2187 if (TREE_CODE (ob
) == ADDR_EXPR
)
2189 dump_expr (pp
, TREE_OPERAND (ob
, 0),
2190 flags
| TFF_EXPR_IN_PARENS
);
2193 else if (!is_this_parameter (ob
))
2195 dump_expr (pp
, ob
, flags
| TFF_EXPR_IN_PARENS
);
2200 if (flag_sanitize
& SANITIZE_UNDEFINED
2201 && is_ubsan_builtin_p (fn
))
2203 pp_string (cxx_pp
, M_("<ubsan routine call>"));
2206 dump_expr (pp
, fn
, flags
| TFF_EXPR_IN_PARENS
);
2207 dump_call_expr_args (pp
, t
, flags
, skipfirst
);
2212 /* Note that this only works for G++ target exprs. If somebody
2213 builds a general TARGET_EXPR, there's no way to represent that
2214 it initializes anything other that the parameter slot for the
2215 default argument. Note we may have cleared out the first
2216 operand in expand_expr, so don't go killing ourselves. */
2217 if (TREE_OPERAND (t
, 1))
2218 dump_expr (pp
, TREE_OPERAND (t
, 1), flags
| TFF_EXPR_IN_PARENS
);
2221 case POINTER_PLUS_EXPR
:
2222 dump_binary_op (pp
, "+", t
, flags
);
2225 case POINTER_DIFF_EXPR
:
2226 dump_binary_op (pp
, "-", t
, flags
);
2231 dump_binary_op (pp
, OVL_OP_INFO (true, NOP_EXPR
)->name
, t
, flags
);
2237 case TRUNC_DIV_EXPR
:
2238 case TRUNC_MOD_EXPR
:
2246 case TRUTH_ANDIF_EXPR
:
2247 case TRUTH_ORIF_EXPR
:
2254 case EXACT_DIV_EXPR
:
2255 dump_binary_op (pp
, OVL_OP_INFO (false, TREE_CODE (t
))->name
, t
, flags
);
2259 case FLOOR_DIV_EXPR
:
2260 case ROUND_DIV_EXPR
:
2262 dump_binary_op (pp
, "/", t
, flags
);
2266 case FLOOR_MOD_EXPR
:
2267 case ROUND_MOD_EXPR
:
2268 dump_binary_op (pp
, "%", t
, flags
);
2273 tree ob
= TREE_OPERAND (t
, 0);
2274 if (INDIRECT_REF_P (ob
))
2276 ob
= TREE_OPERAND (ob
, 0);
2277 if (!is_this_parameter (ob
))
2279 dump_expr (pp
, ob
, flags
| TFF_EXPR_IN_PARENS
);
2280 if (TYPE_REF_P (TREE_TYPE (ob
)))
2288 dump_expr (pp
, ob
, flags
| TFF_EXPR_IN_PARENS
);
2289 if (TREE_CODE (ob
) != ARROW_EXPR
)
2292 dump_expr (pp
, TREE_OPERAND (t
, 1), flags
& ~TFF_EXPR_IN_PARENS
);
2297 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
| TFF_EXPR_IN_PARENS
);
2298 pp_cxx_left_bracket (pp
);
2299 dump_expr (pp
, TREE_OPERAND (t
, 1), flags
| TFF_EXPR_IN_PARENS
);
2300 pp_cxx_right_bracket (pp
);
2303 case UNARY_PLUS_EXPR
:
2304 dump_unary_op (pp
, "+", t
, flags
);
2308 if (TREE_CODE (TREE_OPERAND (t
, 0)) == FUNCTION_DECL
2309 || TREE_CODE (TREE_OPERAND (t
, 0)) == STRING_CST
2310 /* An ADDR_EXPR can have reference type. In that case, we
2311 shouldn't print the `&' doing so indicates to the user
2312 that the expression has pointer type. */
2314 && TYPE_REF_P (TREE_TYPE (t
))))
2315 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
| TFF_EXPR_IN_PARENS
);
2316 else if (TREE_CODE (TREE_OPERAND (t
, 0)) == LABEL_DECL
)
2317 dump_unary_op (pp
, "&&", t
, flags
);
2319 dump_unary_op (pp
, "&", t
, flags
);
2323 if (TREE_HAS_CONSTRUCTOR (t
))
2325 t
= TREE_OPERAND (t
, 0);
2326 gcc_assert (TREE_CODE (t
) == CALL_EXPR
);
2327 dump_expr (pp
, CALL_EXPR_FN (t
), flags
| TFF_EXPR_IN_PARENS
);
2328 dump_call_expr_args (pp
, t
, flags
, true);
2332 if (TREE_OPERAND (t
,0) != NULL_TREE
2333 && TREE_TYPE (TREE_OPERAND (t
, 0))
2334 && NEXT_CODE (TREE_OPERAND (t
, 0)) == REFERENCE_TYPE
)
2335 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
);
2337 dump_unary_op (pp
, "*", t
, flags
);
2342 if (TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
2343 && integer_zerop (TREE_OPERAND (t
, 1)))
2344 dump_expr (pp
, TREE_OPERAND (TREE_OPERAND (t
, 0), 0), flags
);
2348 if (!integer_zerop (TREE_OPERAND (t
, 1)))
2350 pp_cxx_left_paren (pp
);
2351 if (!integer_onep (TYPE_SIZE_UNIT
2352 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (t
, 0))))))
2354 pp_cxx_left_paren (pp
);
2355 dump_type (pp
, ptr_type_node
, flags
);
2356 pp_cxx_right_paren (pp
);
2359 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
);
2360 if (!integer_zerop (TREE_OPERAND (t
, 1)))
2362 pp_cxx_ws_string (pp
, "+");
2363 dump_expr (pp
, fold_convert (ssizetype
, TREE_OPERAND (t
, 1)),
2365 pp_cxx_right_paren (pp
);
2372 case TRUTH_NOT_EXPR
:
2373 case PREDECREMENT_EXPR
:
2374 case PREINCREMENT_EXPR
:
2375 dump_unary_op (pp
, OVL_OP_INFO (false, TREE_CODE (t
))->name
, t
, flags
);
2378 case POSTDECREMENT_EXPR
:
2379 case POSTINCREMENT_EXPR
:
2380 pp_cxx_left_paren (pp
);
2381 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
| TFF_EXPR_IN_PARENS
);
2382 pp_cxx_ws_string (pp
, OVL_OP_INFO (false, TREE_CODE (t
))->name
);
2383 pp_cxx_right_paren (pp
);
2386 case NON_LVALUE_EXPR
:
2387 /* FIXME: This is a KLUDGE workaround for a parsing problem. There
2388 should be another level of INDIRECT_REF so that I don't have to do
2390 if (TREE_TYPE (t
) != NULL_TREE
&& NEXT_CODE (t
) == POINTER_TYPE
)
2392 tree next
= TREE_TYPE (TREE_TYPE (t
));
2394 while (TYPE_PTR_P (next
))
2395 next
= TREE_TYPE (next
);
2397 if (TREE_CODE (next
) == FUNCTION_TYPE
)
2399 if (flags
& TFF_EXPR_IN_PARENS
)
2400 pp_cxx_left_paren (pp
);
2402 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
& ~TFF_EXPR_IN_PARENS
);
2403 if (flags
& TFF_EXPR_IN_PARENS
)
2404 pp_cxx_right_paren (pp
);
2407 /* Else fall through. */
2409 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
| TFF_EXPR_IN_PARENS
);
2413 case IMPLICIT_CONV_EXPR
:
2414 case VIEW_CONVERT_EXPR
:
2416 tree op
= TREE_OPERAND (t
, 0);
2417 tree ttype
= TREE_TYPE (t
);
2418 tree optype
= TREE_TYPE (op
);
2420 if (TREE_CODE (ttype
) != TREE_CODE (optype
)
2421 && INDIRECT_TYPE_P (ttype
)
2422 && INDIRECT_TYPE_P (optype
)
2423 && same_type_p (TREE_TYPE (optype
),
2426 if (TYPE_REF_P (ttype
))
2429 if (TREE_CODE (op
) == ADDR_EXPR
)
2430 dump_expr (pp
, TREE_OPERAND (op
, 0), flags
);
2432 dump_unary_op (pp
, "*", t
, flags
);
2435 dump_unary_op (pp
, "&", t
, flags
);
2437 else if (!same_type_p (TREE_TYPE (op
), TREE_TYPE (t
)))
2439 /* It is a cast, but we cannot tell whether it is a
2440 reinterpret or static cast. Use the C style notation. */
2441 if (flags
& TFF_EXPR_IN_PARENS
)
2442 pp_cxx_left_paren (pp
);
2443 pp_cxx_left_paren (pp
);
2444 dump_type (pp
, TREE_TYPE (t
), flags
);
2445 pp_cxx_right_paren (pp
);
2446 dump_expr (pp
, op
, flags
| TFF_EXPR_IN_PARENS
);
2447 if (flags
& TFF_EXPR_IN_PARENS
)
2448 pp_cxx_right_paren (pp
);
2451 dump_expr (pp
, op
, flags
);
2456 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
)))
2458 tree idx
= build_ptrmemfunc_access_expr (t
, pfn_identifier
);
2460 if (integer_zerop (idx
))
2462 /* A NULL pointer-to-member constant. */
2463 pp_cxx_left_paren (pp
);
2464 pp_cxx_left_paren (pp
);
2465 dump_type (pp
, TREE_TYPE (t
), flags
);
2466 pp_cxx_right_paren (pp
);
2467 pp_character (pp
, '0');
2468 pp_cxx_right_paren (pp
);
2471 else if (tree_fits_shwi_p (idx
))
2474 unsigned HOST_WIDE_INT n
;
2476 t
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
2477 t
= TYPE_METHOD_BASETYPE (t
);
2478 virtuals
= BINFO_VIRTUALS (TYPE_BINFO (TYPE_MAIN_VARIANT (t
)));
2480 n
= tree_to_shwi (idx
);
2482 /* Map vtable index back one, to allow for the null pointer to
2486 while (n
> 0 && virtuals
)
2489 virtuals
= TREE_CHAIN (virtuals
);
2493 dump_expr (pp
, BV_FN (virtuals
),
2494 flags
| TFF_EXPR_IN_PARENS
);
2499 if (TREE_TYPE (t
) && LAMBDA_TYPE_P (TREE_TYPE (t
)))
2500 pp_string (pp
, "<lambda closure object>");
2501 if (TREE_TYPE (t
) && EMPTY_CONSTRUCTOR_P (t
))
2503 dump_type (pp
, TREE_TYPE (t
), 0);
2504 pp_cxx_left_paren (pp
);
2505 pp_cxx_right_paren (pp
);
2509 if (!BRACE_ENCLOSED_INITIALIZER_P (t
))
2510 dump_type (pp
, TREE_TYPE (t
), 0);
2511 pp_cxx_left_brace (pp
);
2512 dump_expr_init_vec (pp
, CONSTRUCTOR_ELTS (t
), flags
);
2513 pp_cxx_right_brace (pp
);
2520 tree ob
= TREE_OPERAND (t
, 0);
2521 if (is_dummy_object (ob
))
2523 t
= TREE_OPERAND (t
, 1);
2524 if (TREE_CODE (t
) == FUNCTION_DECL
)
2526 dump_expr (pp
, t
, flags
| TFF_EXPR_IN_PARENS
);
2527 else if (BASELINK_P (t
))
2528 dump_expr (pp
, OVL_FIRST (BASELINK_FUNCTIONS (t
)),
2529 flags
| TFF_EXPR_IN_PARENS
);
2531 dump_decl (pp
, t
, flags
);
2535 if (INDIRECT_REF_P (ob
))
2537 dump_expr (pp
, TREE_OPERAND (ob
, 0), flags
| TFF_EXPR_IN_PARENS
);
2543 dump_expr (pp
, ob
, flags
| TFF_EXPR_IN_PARENS
);
2547 dump_expr (pp
, TREE_OPERAND (t
, 1), flags
| TFF_EXPR_IN_PARENS
);
2552 case TEMPLATE_PARM_INDEX
:
2553 dump_decl (pp
, TEMPLATE_PARM_DECL (t
), flags
& ~TFF_DECL_SPECIFIERS
);
2557 if (TREE_OPERAND (t
, 0) == NULL_TREE
2558 || TREE_CHAIN (TREE_OPERAND (t
, 0)))
2560 dump_type (pp
, TREE_TYPE (t
), flags
);
2561 pp_cxx_left_paren (pp
);
2562 dump_expr_list (pp
, TREE_OPERAND (t
, 0), flags
);
2563 pp_cxx_right_paren (pp
);
2567 pp_cxx_left_paren (pp
);
2568 dump_type (pp
, TREE_TYPE (t
), flags
);
2569 pp_cxx_right_paren (pp
);
2570 pp_cxx_left_paren (pp
);
2571 dump_expr_list (pp
, TREE_OPERAND (t
, 0), flags
);
2572 pp_cxx_right_paren (pp
);
2576 case STATIC_CAST_EXPR
:
2577 pp_cxx_ws_string (pp
, "static_cast");
2579 case REINTERPRET_CAST_EXPR
:
2580 pp_cxx_ws_string (pp
, "reinterpret_cast");
2582 case CONST_CAST_EXPR
:
2583 pp_cxx_ws_string (pp
, "const_cast");
2585 case DYNAMIC_CAST_EXPR
:
2586 pp_cxx_ws_string (pp
, "dynamic_cast");
2588 pp_cxx_begin_template_argument_list (pp
);
2589 dump_type (pp
, TREE_TYPE (t
), flags
);
2590 pp_cxx_end_template_argument_list (pp
);
2591 pp_cxx_left_paren (pp
);
2592 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
);
2593 pp_cxx_right_paren (pp
);
2597 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
);
2603 if (TREE_CODE (t
) == SIZEOF_EXPR
)
2604 pp_cxx_ws_string (pp
, "sizeof");
2607 gcc_assert (TREE_CODE (t
) == ALIGNOF_EXPR
);
2608 pp_cxx_ws_string (pp
, "__alignof__");
2610 op
= TREE_OPERAND (t
, 0);
2611 if (PACK_EXPANSION_P (op
))
2613 pp_string (pp
, "...");
2614 op
= PACK_EXPANSION_PATTERN (op
);
2616 pp_cxx_whitespace (pp
);
2617 pp_cxx_left_paren (pp
);
2618 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
2619 dump_type (pp
, TREE_TYPE (op
), flags
);
2620 else if (TYPE_P (TREE_OPERAND (t
, 0)))
2621 dump_type (pp
, op
, flags
);
2623 dump_expr (pp
, op
, flags
);
2624 pp_cxx_right_paren (pp
);
2627 case AT_ENCODE_EXPR
:
2628 pp_cxx_ws_string (pp
, "@encode");
2629 pp_cxx_whitespace (pp
);
2630 pp_cxx_left_paren (pp
);
2631 dump_type (pp
, TREE_OPERAND (t
, 0), flags
);
2632 pp_cxx_right_paren (pp
);
2636 pp_cxx_ws_string (pp
, "noexcept");
2637 pp_cxx_whitespace (pp
);
2638 pp_cxx_left_paren (pp
);
2639 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
);
2640 pp_cxx_right_paren (pp
);
2645 pp_cxx_ws_string (pp
, OVL_OP_INFO (false, TREE_CODE (t
))->name
);
2646 pp_cxx_whitespace (pp
);
2647 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
);
2651 pp_string (pp
, M_("<unparsed>"));
2654 case TRY_CATCH_EXPR
:
2655 case CLEANUP_POINT_EXPR
:
2656 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
);
2659 case PSEUDO_DTOR_EXPR
:
2660 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
);
2662 if (TREE_OPERAND (t
, 1))
2664 dump_type (pp
, TREE_OPERAND (t
, 1), flags
);
2665 pp_cxx_colon_colon (pp
);
2667 pp_cxx_complement (pp
);
2668 dump_type (pp
, TREE_OPERAND (t
, 2), flags
);
2671 case TEMPLATE_ID_EXPR
:
2672 dump_decl (pp
, t
, flags
);
2678 case STATEMENT_LIST
:
2679 /* We don't yet have a way of dumping statements in a
2680 human-readable format. */
2681 pp_string (pp
, "({...})");
2685 pp_string (pp
, "while (1) { ");
2686 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
& ~TFF_EXPR_IN_PARENS
);
2687 pp_cxx_right_brace (pp
);
2691 pp_string (pp
, "if (");
2692 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
& ~TFF_EXPR_IN_PARENS
);
2693 pp_string (pp
, ") break; ");
2697 dump_expr (pp
, BASELINK_FUNCTIONS (t
), flags
& ~TFF_EXPR_IN_PARENS
);
2700 case EMPTY_CLASS_EXPR
:
2701 dump_type (pp
, TREE_TYPE (t
), flags
);
2702 pp_cxx_left_paren (pp
);
2703 pp_cxx_right_paren (pp
);
2706 case NON_DEPENDENT_EXPR
:
2707 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
);
2710 case ARGUMENT_PACK_SELECT
:
2711 dump_template_argument (pp
, ARGUMENT_PACK_SELECT_FROM_PACK (t
), flags
);
2724 pp_type_specifier_seq (pp
, t
);
2728 /* We get here when we want to print a dependent type as an
2729 id-expression, without any disambiguator decoration. */
2730 pp
->id_expression (t
);
2733 case TEMPLATE_TYPE_PARM
:
2734 case TEMPLATE_TEMPLATE_PARM
:
2735 case BOUND_TEMPLATE_TEMPLATE_PARM
:
2736 dump_type (pp
, t
, flags
);
2740 pp_cxx_trait_expression (pp
, t
);
2744 pp_cxx_va_arg_expression (pp
, t
);
2748 pp_cxx_offsetof_expression (pp
, t
);
2751 case ADDRESSOF_EXPR
:
2752 pp_cxx_addressof_expression (pp
, t
);
2756 dump_decl (pp
, t
, flags
);
2759 case EXPR_PACK_EXPANSION
:
2760 case UNARY_LEFT_FOLD_EXPR
:
2761 case UNARY_RIGHT_FOLD_EXPR
:
2762 case BINARY_LEFT_FOLD_EXPR
:
2763 case BINARY_RIGHT_FOLD_EXPR
:
2770 case VEC_DELETE_EXPR
:
2777 case UNORDERED_EXPR
:
2787 case FIX_TRUNC_EXPR
:
2792 case TRUTH_AND_EXPR
:
2794 case TRUTH_XOR_EXPR
:
2795 if (flags
& TFF_EXPR_IN_PARENS
)
2796 pp_cxx_left_paren (pp
);
2798 if (flags
& TFF_EXPR_IN_PARENS
)
2799 pp_cxx_right_paren (pp
);
2803 dump_expr (pp
, resolve_virtual_fun_from_obj_type_ref (t
), flags
);
2807 pp_string (pp
, M_("<lambda>"));
2811 pp_cxx_left_paren (pp
);
2812 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
| TFF_EXPR_IN_PARENS
);
2813 pp_cxx_right_paren (pp
);
2817 pp_cxx_requires_expr (cxx_pp
, t
);
2821 pp_cxx_simple_requirement (cxx_pp
, t
);
2825 pp_cxx_type_requirement (cxx_pp
, t
);
2829 pp_cxx_compound_requirement (cxx_pp
, t
);
2833 pp_cxx_nested_requirement (cxx_pp
, t
);
2846 pp_cxx_constraint (cxx_pp
, t
);
2849 case PLACEHOLDER_EXPR
:
2850 pp_string (pp
, M_("*this"));
2854 dump_expr_list (pp
, t
, flags
);
2857 /* This list is incomplete, but should suffice for now.
2858 It is very important that `sorry' does not call
2859 `report_error_function'. That could cause an infinite loop. */
2861 pp_unsupported_tree (pp
, t
);
2864 pp_string (pp
, M_("<expression error>"));
2870 dump_binary_op (cxx_pretty_printer
*pp
, const char *opstring
, tree t
,
2873 pp_cxx_left_paren (pp
);
2874 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
| TFF_EXPR_IN_PARENS
);
2875 pp_cxx_whitespace (pp
);
2877 pp_cxx_ws_string (pp
, opstring
);
2879 pp_string (pp
, M_("<unknown operator>"));
2880 pp_cxx_whitespace (pp
);
2881 dump_expr (pp
, TREE_OPERAND (t
, 1), flags
| TFF_EXPR_IN_PARENS
);
2882 pp_cxx_right_paren (pp
);
2886 dump_unary_op (cxx_pretty_printer
*pp
, const char *opstring
, tree t
, int flags
)
2888 if (flags
& TFF_EXPR_IN_PARENS
)
2889 pp_cxx_left_paren (pp
);
2890 pp_cxx_ws_string (pp
, opstring
);
2891 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
& ~TFF_EXPR_IN_PARENS
);
2892 if (flags
& TFF_EXPR_IN_PARENS
)
2893 pp_cxx_right_paren (pp
);
2897 reinit_cxx_pp (void)
2899 pp_clear_output_area (cxx_pp
);
2900 cxx_pp
->padding
= pp_none
;
2901 pp_indentation (cxx_pp
) = 0;
2902 pp_needs_newline (cxx_pp
) = false;
2903 cxx_pp
->enclosing_scope
= current_function_decl
;
2906 /* Same as pp_formatted_text, except the return string is a separate
2907 copy and has a GGC storage duration, e.g. an indefinite lifetime. */
2910 pp_ggc_formatted_text (pretty_printer
*pp
)
2912 return ggc_strdup (pp_formatted_text (pp
));
2915 /* Exported interface to stringifying types, exprs and decls under TFF_*
2919 type_as_string (tree typ
, int flags
)
2922 pp_translate_identifiers (cxx_pp
) = false;
2923 dump_type (cxx_pp
, typ
, flags
);
2924 return pp_ggc_formatted_text (cxx_pp
);
2928 type_as_string_translate (tree typ
, int flags
)
2931 dump_type (cxx_pp
, typ
, flags
);
2932 return pp_ggc_formatted_text (cxx_pp
);
2936 expr_as_string (tree decl
, int flags
)
2939 pp_translate_identifiers (cxx_pp
) = false;
2940 dump_expr (cxx_pp
, decl
, flags
);
2941 return pp_ggc_formatted_text (cxx_pp
);
2944 /* Wrap decl_as_string with options appropriate for dwarf. */
2947 decl_as_dwarf_string (tree decl
, int flags
)
2950 /* Curiously, reinit_cxx_pp doesn't reset the flags field, so setting the flag
2951 here will be adequate to get the desired behavior. */
2952 cxx_pp
->flags
|= pp_c_flag_gnu_v3
;
2953 name
= decl_as_string (decl
, flags
);
2954 /* Subsequent calls to the pretty printer shouldn't use this style. */
2955 cxx_pp
->flags
&= ~pp_c_flag_gnu_v3
;
2960 decl_as_string (tree decl
, int flags
)
2963 pp_translate_identifiers (cxx_pp
) = false;
2964 dump_decl (cxx_pp
, decl
, flags
);
2965 return pp_ggc_formatted_text (cxx_pp
);
2969 decl_as_string_translate (tree decl
, int flags
)
2972 dump_decl (cxx_pp
, decl
, flags
);
2973 return pp_ggc_formatted_text (cxx_pp
);
2976 /* Wrap lang_decl_name with options appropriate for dwarf. */
2979 lang_decl_dwarf_name (tree decl
, int v
, bool translate
)
2982 /* Curiously, reinit_cxx_pp doesn't reset the flags field, so setting the flag
2983 here will be adequate to get the desired behavior. */
2984 cxx_pp
->flags
|= pp_c_flag_gnu_v3
;
2985 name
= lang_decl_name (decl
, v
, translate
);
2986 /* Subsequent calls to the pretty printer shouldn't use this style. */
2987 cxx_pp
->flags
&= ~pp_c_flag_gnu_v3
;
2991 /* Generate the three forms of printable names for cxx_printable_name. */
2994 lang_decl_name (tree decl
, int v
, bool translate
)
2998 ? decl_as_string_translate (decl
, TFF_DECL_SPECIFIERS
)
2999 : decl_as_string (decl
, TFF_DECL_SPECIFIERS
));
3002 pp_translate_identifiers (cxx_pp
) = translate
;
3004 && (DECL_CLASS_SCOPE_P (decl
)
3005 || (DECL_NAMESPACE_SCOPE_P (decl
)
3006 && CP_DECL_CONTEXT (decl
) != global_namespace
)))
3008 dump_type (cxx_pp
, CP_DECL_CONTEXT (decl
), TFF_PLAIN_IDENTIFIER
);
3009 pp_cxx_colon_colon (cxx_pp
);
3012 if (TREE_CODE (decl
) == FUNCTION_DECL
)
3013 dump_function_name (cxx_pp
, decl
, TFF_PLAIN_IDENTIFIER
);
3014 else if ((DECL_NAME (decl
) == NULL_TREE
)
3015 && TREE_CODE (decl
) == NAMESPACE_DECL
)
3016 dump_decl (cxx_pp
, decl
, TFF_PLAIN_IDENTIFIER
| TFF_UNQUALIFIED_NAME
);
3018 dump_decl (cxx_pp
, DECL_NAME (decl
), TFF_PLAIN_IDENTIFIER
);
3020 return pp_ggc_formatted_text (cxx_pp
);
3023 /* Return the location of a tree passed to %+ formats. */
3026 location_of (tree t
)
3030 t
= TYPE_MAIN_DECL (t
);
3032 return input_location
;
3034 else if (TREE_CODE (t
) == OVERLOAD
)
3038 return DECL_SOURCE_LOCATION (t
);
3039 if (TREE_CODE (t
) == DEFAULT_ARG
)
3040 return defarg_location (t
);
3041 return cp_expr_loc_or_loc (t
, input_location
);
3044 /* Now the interfaces from error et al to dump_type et al. Each takes an
3045 on/off VERBOSE flag and supply the appropriate TFF_ flags to a dump_
3049 decl_to_string (tree decl
, int verbose
)
3053 if (TREE_CODE (decl
) == TYPE_DECL
|| TREE_CODE (decl
) == RECORD_TYPE
3054 || TREE_CODE (decl
) == UNION_TYPE
|| TREE_CODE (decl
) == ENUMERAL_TYPE
)
3055 flags
= TFF_CLASS_KEY_OR_ENUM
;
3057 flags
|= TFF_DECL_SPECIFIERS
;
3058 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3059 flags
|= TFF_DECL_SPECIFIERS
| TFF_RETURN_TYPE
;
3060 flags
|= TFF_TEMPLATE_HEADER
;
3063 dump_decl (cxx_pp
, decl
, flags
);
3064 return pp_ggc_formatted_text (cxx_pp
);
3068 expr_to_string (tree decl
)
3071 dump_expr (cxx_pp
, decl
, 0);
3072 return pp_ggc_formatted_text (cxx_pp
);
3076 fndecl_to_string (tree fndecl
, int verbose
)
3080 flags
= TFF_EXCEPTION_SPECIFICATION
| TFF_DECL_SPECIFIERS
3081 | TFF_TEMPLATE_HEADER
;
3083 flags
|= TFF_FUNCTION_DEFAULT_ARGUMENTS
;
3085 dump_decl (cxx_pp
, fndecl
, flags
);
3086 return pp_ggc_formatted_text (cxx_pp
);
3091 code_to_string (enum tree_code c
)
3093 return get_tree_code_name (c
);
3097 language_to_string (enum languages c
)
3104 case lang_cplusplus
:
3113 /* Return the proper printed version of a parameter to a C++ function. */
3116 parm_to_string (int p
)
3120 pp_string (cxx_pp
, "'this'");
3122 pp_decimal_int (cxx_pp
, p
+ 1);
3123 return pp_ggc_formatted_text (cxx_pp
);
3127 op_to_string (bool assop
, enum tree_code p
)
3129 tree id
= ovl_op_identifier (assop
, p
);
3130 return id
? IDENTIFIER_POINTER (id
) : M_("<unknown>");
3133 /* Return a GC-allocated representation of type TYP, with verbosity VERBOSE.
3135 If QUOTE is non-NULL and if *QUOTE is true, then quotes are added to the
3136 string in appropriate places, and *QUOTE is written to with false
3137 to suppress pp_format's trailing close quote so that e.g.
3138 foo_typedef {aka underlying_foo} {enum}
3139 can be printed by "%qT" as:
3140 `foo_typedef' {aka `underlying_foo'} {enum}
3142 `foo_typedef {aka underlying_foo} {enum}'
3143 When adding such quotes, if POSTPROCESSED is true (for handling %H and %I)
3144 then a leading open quote will be added, whereas if POSTPROCESSED is false
3145 (for handling %T) then any leading quote has already been added by
3146 pp_format, or is not needed due to QUOTE being NULL (for template arguments
3149 SHOW_COLOR is used to determine the colorization of any quotes that
3153 type_to_string (tree typ
, int verbose
, bool postprocessed
, bool *quote
,
3158 flags
|= TFF_CLASS_KEY_OR_ENUM
;
3159 flags
|= TFF_TEMPLATE_HEADER
;
3163 if (postprocessed
&& quote
&& *quote
)
3164 pp_begin_quote (cxx_pp
, show_color
);
3166 struct obstack
*ob
= pp_buffer (cxx_pp
)->obstack
;
3167 int type_start
, type_len
;
3168 type_start
= obstack_object_size (ob
);
3170 dump_type (cxx_pp
, typ
, flags
);
3172 /* Remember the end of the initial dump. */
3173 type_len
= obstack_object_size (ob
) - type_start
;
3175 /* If we're printing a type that involves typedefs, also print the
3176 stripped version. But sometimes the stripped version looks
3177 exactly the same, so we don't want it after all. To avoid printing
3178 it in that case, we play ugly obstack games. */
3179 if (typ
&& TYPE_P (typ
) && typ
!= TYPE_CANONICAL (typ
)
3180 && !uses_template_parms (typ
))
3182 int aka_start
, aka_len
; char *p
;
3183 tree aka
= strip_typedefs (typ
);
3184 if (quote
&& *quote
)
3185 pp_end_quote (cxx_pp
, show_color
);
3186 pp_string (cxx_pp
, " {aka");
3187 pp_cxx_whitespace (cxx_pp
);
3188 if (quote
&& *quote
)
3189 pp_begin_quote (cxx_pp
, show_color
);
3190 /* And remember the start of the aka dump. */
3191 aka_start
= obstack_object_size (ob
);
3192 dump_type (cxx_pp
, aka
, flags
);
3193 aka_len
= obstack_object_size (ob
) - aka_start
;
3194 if (quote
&& *quote
)
3195 pp_end_quote (cxx_pp
, show_color
);
3196 pp_right_brace (cxx_pp
);
3197 p
= (char*)obstack_base (ob
);
3198 /* If they are identical, cut off the aka by unwinding the obstack. */
3199 if (type_len
== aka_len
3200 && memcmp (p
+ type_start
, p
+aka_start
, type_len
) == 0)
3202 /* We can't add a '\0' here, since we may be adding a closing quote
3203 below, and it would be hidden by the '\0'.
3204 Instead, manually unwind the current object within the obstack
3205 so that the insertion point is at the end of the type, before
3207 int delta
= type_start
+ type_len
- obstack_object_size (ob
);
3208 gcc_assert (delta
<= 0);
3209 obstack_blank_fast (ob
, delta
);
3213 /* No further closing quotes are needed. */
3217 if (quote
&& *quote
)
3219 pp_end_quote (cxx_pp
, show_color
);
3222 return pp_ggc_formatted_text (cxx_pp
);
3226 args_to_string (tree p
, int verbose
)
3230 flags
|= TFF_CLASS_KEY_OR_ENUM
;
3235 if (TYPE_P (TREE_VALUE (p
)))
3236 return type_as_string_translate (p
, flags
);
3239 for (; p
; p
= TREE_CHAIN (p
))
3241 if (null_node_p (TREE_VALUE (p
)))
3242 pp_cxx_ws_string (cxx_pp
, "NULL");
3244 dump_type (cxx_pp
, error_type (TREE_VALUE (p
)), flags
);
3246 pp_separate_with_comma (cxx_pp
);
3248 return pp_ggc_formatted_text (cxx_pp
);
3251 /* Pretty-print a deduction substitution (from deduction_tsubst_fntype). P
3252 is a TREE_LIST with purpose the TEMPLATE_DECL, value the template
3256 subst_to_string (tree p
)
3258 tree decl
= TREE_PURPOSE (p
);
3259 tree targs
= TREE_VALUE (p
);
3260 tree tparms
= DECL_TEMPLATE_PARMS (decl
);
3261 int flags
= (TFF_DECL_SPECIFIERS
|TFF_TEMPLATE_HEADER
3262 |TFF_NO_TEMPLATE_BINDINGS
);
3268 dump_template_decl (cxx_pp
, TREE_PURPOSE (p
), flags
);
3269 dump_substitution (cxx_pp
, NULL
, tparms
, targs
, /*flags=*/0);
3270 return pp_ggc_formatted_text (cxx_pp
);
3274 cv_to_string (tree p
, int v
)
3277 cxx_pp
->padding
= v
? pp_before
: pp_none
;
3278 pp_cxx_cv_qualifier_seq (cxx_pp
, p
);
3279 return pp_ggc_formatted_text (cxx_pp
);
3283 eh_spec_to_string (tree p
, int /*v*/)
3287 dump_exception_spec (cxx_pp
, p
, flags
);
3288 return pp_ggc_formatted_text (cxx_pp
);
3291 /* Langhook for print_error_function. */
3293 cxx_print_error_function (diagnostic_context
*context
, const char *file
,
3294 diagnostic_info
*diagnostic
)
3298 prefix
= xstrdup (file
);
3301 lhd_print_error_function (context
, file
, diagnostic
);
3302 pp_set_prefix (context
->printer
, prefix
);
3303 maybe_print_instantiation_context (context
);
3307 cp_diagnostic_starter (diagnostic_context
*context
,
3308 diagnostic_info
*diagnostic
)
3310 diagnostic_report_current_module (context
, diagnostic_location (diagnostic
));
3311 cp_print_error_function (context
, diagnostic
);
3312 maybe_print_instantiation_context (context
);
3313 maybe_print_constexpr_context (context
);
3314 pp_set_prefix (context
->printer
, diagnostic_build_prefix (context
,
3318 /* Print current function onto BUFFER, in the process of reporting
3319 a diagnostic message. Called from cp_diagnostic_starter. */
3321 cp_print_error_function (diagnostic_context
*context
,
3322 diagnostic_info
*diagnostic
)
3324 /* If we are in an instantiation context, current_function_decl is likely
3325 to be wrong, so just rely on print_instantiation_full_context. */
3326 if (current_instantiation ())
3328 if (diagnostic_last_function_changed (context
, diagnostic
))
3330 char *old_prefix
= pp_take_prefix (context
->printer
);
3331 const char *file
= LOCATION_FILE (diagnostic_location (diagnostic
));
3332 tree abstract_origin
= diagnostic_abstract_origin (diagnostic
);
3333 char *new_prefix
= (file
&& abstract_origin
== NULL
)
3334 ? file_name_as_prefix (context
, file
) : NULL
;
3336 pp_set_prefix (context
->printer
, new_prefix
);
3338 if (current_function_decl
== NULL
)
3339 pp_string (context
->printer
, _("At global scope:"));
3344 if (abstract_origin
)
3346 ao
= BLOCK_ABSTRACT_ORIGIN (abstract_origin
);
3347 gcc_assert (TREE_CODE (ao
) == FUNCTION_DECL
);
3351 fndecl
= current_function_decl
;
3353 pp_printf (context
->printer
, function_category (fndecl
),
3354 cxx_printable_name_translate (fndecl
, 2));
3356 while (abstract_origin
)
3359 tree block
= abstract_origin
;
3361 locus
= &BLOCK_SOURCE_LOCATION (block
);
3363 block
= BLOCK_SUPERCONTEXT (block
);
3364 while (block
&& TREE_CODE (block
) == BLOCK
3365 && BLOCK_ABSTRACT_ORIGIN (block
))
3367 ao
= BLOCK_ABSTRACT_ORIGIN (block
);
3368 if (TREE_CODE (ao
) == FUNCTION_DECL
)
3373 else if (TREE_CODE (ao
) != BLOCK
)
3376 block
= BLOCK_SUPERCONTEXT (block
);
3379 abstract_origin
= block
;
3382 while (block
&& TREE_CODE (block
) == BLOCK
)
3383 block
= BLOCK_SUPERCONTEXT (block
);
3385 if (block
&& TREE_CODE (block
) == FUNCTION_DECL
)
3387 abstract_origin
= NULL
;
3391 expanded_location s
= expand_location (*locus
);
3392 pp_character (context
->printer
, ',');
3393 pp_newline (context
->printer
);
3396 if (context
->show_column
&& s
.column
!= 0)
3397 pp_printf (context
->printer
,
3398 _(" inlined from %qs at %r%s:%d:%d%R"),
3399 cxx_printable_name_translate (fndecl
, 2),
3400 "locus", s
.file
, s
.line
, s
.column
);
3402 pp_printf (context
->printer
,
3403 _(" inlined from %qs at %r%s:%d%R"),
3404 cxx_printable_name_translate (fndecl
, 2),
3405 "locus", s
.file
, s
.line
);
3409 pp_printf (context
->printer
, _(" inlined from %qs"),
3410 cxx_printable_name_translate (fndecl
, 2));
3413 pp_character (context
->printer
, ':');
3415 pp_newline (context
->printer
);
3417 diagnostic_set_last_function (context
, diagnostic
);
3418 pp_destroy_prefix (context
->printer
);
3419 context
->printer
->prefix
= old_prefix
;
3423 /* Returns a description of FUNCTION using standard terminology. The
3424 result is a format string of the form "In CATEGORY %qs". */
3426 function_category (tree fn
)
3428 /* We can get called from the middle-end for diagnostics of function
3429 clones. Make sure we have language specific information before
3430 dereferencing it. */
3431 if (DECL_LANG_SPECIFIC (STRIP_TEMPLATE (fn
))
3432 && DECL_FUNCTION_MEMBER_P (fn
))
3434 if (DECL_STATIC_FUNCTION_P (fn
))
3435 return _("In static member function %qs");
3436 else if (DECL_COPY_CONSTRUCTOR_P (fn
))
3437 return _("In copy constructor %qs");
3438 else if (DECL_CONSTRUCTOR_P (fn
))
3439 return _("In constructor %qs");
3440 else if (DECL_DESTRUCTOR_P (fn
))
3441 return _("In destructor %qs");
3442 else if (LAMBDA_FUNCTION_P (fn
))
3443 return _("In lambda function");
3445 return _("In member function %qs");
3448 return _("In function %qs");
3451 /* Report the full context of a current template instantiation,
3454 print_instantiation_full_context (diagnostic_context
*context
)
3456 struct tinst_level
*p
= current_instantiation ();
3457 location_t location
= input_location
;
3461 pp_verbatim (context
->printer
,
3463 ? _("%s: In substitution of %qS:\n")
3464 : _("%s: In instantiation of %q#D:\n"),
3465 LOCATION_FILE (location
),
3468 location
= p
->locus
;
3472 print_instantiation_partial_context (context
, p
, location
);
3475 /* Helper function of print_instantiation_partial_context() that
3476 prints a single line of instantiation context. */
3479 print_instantiation_partial_context_line (diagnostic_context
*context
,
3480 struct tinst_level
*t
,
3481 location_t loc
, bool recursive_p
)
3483 if (loc
== UNKNOWN_LOCATION
)
3486 expanded_location xloc
= expand_location (loc
);
3488 if (context
->show_column
)
3489 pp_verbatim (context
->printer
, _("%r%s:%d:%d:%R "),
3490 "locus", xloc
.file
, xloc
.line
, xloc
.column
);
3492 pp_verbatim (context
->printer
, _("%r%s:%d:%R "),
3493 "locus", xloc
.file
, xloc
.line
);
3498 pp_verbatim (context
->printer
,
3500 ? _("recursively required by substitution of %qS\n")
3501 : _("required by substitution of %qS\n"),
3504 pp_verbatim (context
->printer
,
3506 ? _("recursively required from %q#D\n")
3507 : _("required from %q#D\n"),
3512 pp_verbatim (context
->printer
,
3514 ? _("recursively required from here\n")
3515 : _("required from here\n"));
3519 /* Same as print_instantiation_full_context but less verbose. */
3522 print_instantiation_partial_context (diagnostic_context
*context
,
3523 struct tinst_level
*t0
, location_t loc
)
3525 struct tinst_level
*t
;
3528 location_t prev_loc
= loc
;
3530 for (t
= t0
; t
!= NULL
; t
= t
->next
)
3531 if (prev_loc
!= t
->locus
)
3533 prev_loc
= t
->locus
;
3539 if (template_backtrace_limit
3540 && n_total
> template_backtrace_limit
)
3542 int skip
= n_total
- template_backtrace_limit
;
3543 int head
= template_backtrace_limit
/ 2;
3545 /* Avoid skipping just 1. If so, skip 2. */
3549 head
= (template_backtrace_limit
- 1) / 2;
3552 for (n
= 0; n
< head
; n
++)
3554 gcc_assert (t
!= NULL
);
3555 if (loc
!= t
->locus
)
3556 print_instantiation_partial_context_line (context
, t
, loc
,
3557 /*recursive_p=*/false);
3561 if (t
!= NULL
&& skip
> 0)
3563 expanded_location xloc
;
3564 xloc
= expand_location (loc
);
3565 if (context
->show_column
)
3566 pp_verbatim (context
->printer
,
3567 _("%r%s:%d:%d:%R [ skipping %d instantiation "
3568 "contexts, use -ftemplate-backtrace-limit=0 to "
3570 "locus", xloc
.file
, xloc
.line
, xloc
.column
, skip
);
3572 pp_verbatim (context
->printer
,
3573 _("%r%s:%d:%R [ skipping %d instantiation "
3574 "contexts, use -ftemplate-backtrace-limit=0 to "
3576 "locus", xloc
.file
, xloc
.line
, skip
);
3581 } while (t
!= NULL
&& --skip
> 0);
3587 while (t
->next
!= NULL
&& t
->locus
== t
->next
->locus
)
3592 print_instantiation_partial_context_line (context
, t
, loc
,
3597 print_instantiation_partial_context_line (context
, NULL
, loc
,
3598 /*recursive_p=*/false);
3601 /* Called from cp_thing to print the template context for an error. */
3603 maybe_print_instantiation_context (diagnostic_context
*context
)
3605 if (!problematic_instantiation_changed () || current_instantiation () == 0)
3608 record_last_problematic_instantiation ();
3609 print_instantiation_full_context (context
);
3612 /* Report what constexpr call(s) we're trying to expand, if any. */
3615 maybe_print_constexpr_context (diagnostic_context
*context
)
3617 vec
<tree
> call_stack
= cx_error_context ();
3621 FOR_EACH_VEC_ELT (call_stack
, ix
, t
)
3623 expanded_location xloc
= expand_location (EXPR_LOCATION (t
));
3624 const char *s
= expr_as_string (t
, 0);
3625 if (context
->show_column
)
3626 pp_verbatim (context
->printer
,
3627 _("%r%s:%d:%d:%R in %<constexpr%> expansion of %qs"),
3628 "locus", xloc
.file
, xloc
.line
, xloc
.column
, s
);
3630 pp_verbatim (context
->printer
,
3631 _("%r%s:%d:%R in %<constexpr%> expansion of %qs"),
3632 "locus", xloc
.file
, xloc
.line
, s
);
3633 pp_newline (context
->printer
);
3638 /* Return true iff TYPE_A and TYPE_B are template types that are
3639 meaningful to compare. */
3642 comparable_template_types_p (tree type_a
, tree type_b
)
3644 if (!CLASS_TYPE_P (type_a
))
3646 if (!CLASS_TYPE_P (type_b
))
3649 tree tinfo_a
= TYPE_TEMPLATE_INFO (type_a
);
3650 tree tinfo_b
= TYPE_TEMPLATE_INFO (type_b
);
3651 if (!tinfo_a
|| !tinfo_b
)
3654 return TI_TEMPLATE (tinfo_a
) == TI_TEMPLATE (tinfo_b
);
3657 /* Start a new line indented by SPC spaces on PP. */
3660 newline_and_indent (pretty_printer
*pp
, int spc
)
3663 for (int i
= 0; i
< spc
; i
++)
3667 /* Generate a GC-allocated string for ARG, an expression or type. */
3670 arg_to_string (tree arg
, bool verbose
)
3673 return type_to_string (arg
, verbose
, true, NULL
, false);
3675 return expr_to_string (arg
);
3678 /* Subroutine to type_to_string_with_compare and
3679 print_template_tree_comparison.
3681 Print a representation of ARG (an expression or type) to PP,
3682 colorizing it as "type-diff" if PP->show_color. */
3685 print_nonequal_arg (pretty_printer
*pp
, tree arg
, bool verbose
)
3687 pp_printf (pp
, "%r%s%R",
3690 ? arg_to_string (arg
, verbose
)
3691 : G_("(no argument)")));
3694 /* Recursively print template TYPE_A to PP, as compared to template TYPE_B.
3696 The types must satisfy comparable_template_types_p.
3698 If INDENT is 0, then this is equivalent to type_to_string (TYPE_A), but
3699 potentially colorizing/eliding in comparison with TYPE_B.
3701 For example given types:
3702 vector<map<int,double>>
3704 vector<map<int,float>>
3705 then the result on PP would be:
3706 vector<map<[...],double>>
3707 with type elision, and:
3708 vector<map<int,double>>
3709 without type elision.
3711 In both cases the parts of TYPE that differ from PEER will be colorized
3712 if pp_show_color (pp) is true. In the above example, this would be
3715 If INDENT is non-zero, then the types are printed in a tree-like form
3716 which shows both types. In the above example, the result on PP would be:
3723 and without type-elision would be:
3730 As before, the differing parts of the types are colorized if
3731 pp_show_color (pp) is true ("double" and "float" in this example).
3733 Template arguments in which both types are using the default arguments
3734 are not printed; if at least one of the two types is using a non-default
3735 argument, then that argument is printed (or both arguments for the
3736 tree-like print format). */
3739 print_template_differences (pretty_printer
*pp
, tree type_a
, tree type_b
,
3740 bool verbose
, int indent
)
3743 newline_and_indent (pp
, indent
);
3745 tree tinfo_a
= TYPE_TEMPLATE_INFO (type_a
);
3746 tree tinfo_b
= TYPE_TEMPLATE_INFO (type_b
);
3748 pp_printf (pp
, "%s<",
3749 IDENTIFIER_POINTER (DECL_NAME (TI_TEMPLATE (tinfo_a
))));
3751 tree args_a
= TI_ARGS (tinfo_a
);
3752 tree args_b
= TI_ARGS (tinfo_b
);
3753 gcc_assert (TREE_CODE (args_a
) == TREE_VEC
);
3754 gcc_assert (TREE_CODE (args_b
) == TREE_VEC
);
3756 int len_a
= get_non_default_template_args_count (args_a
, flags
);
3757 args_a
= INNERMOST_TEMPLATE_ARGS (args_a
);
3758 int len_b
= get_non_default_template_args_count (args_b
, flags
);
3759 args_b
= INNERMOST_TEMPLATE_ARGS (args_b
);
3760 /* Determine the maximum range of args for which non-default template args
3761 were used; beyond this, only default args (if any) were used, and so
3762 they will be equal from this point onwards.
3763 One of the two peers might have used default arguments within this
3764 range, but the other will be using non-default arguments, and so
3765 it's more readable to print both within this range, to highlight
3767 int len_max
= MAX (len_a
, len_b
);
3768 gcc_assert (TREE_CODE (args_a
) == TREE_VEC
);
3769 gcc_assert (TREE_CODE (args_b
) == TREE_VEC
);
3770 for (int idx
= 0; idx
< len_max
; idx
++)
3773 pp_character (pp
, ',');
3775 tree arg_a
= TREE_VEC_ELT (args_a
, idx
);
3776 tree arg_b
= TREE_VEC_ELT (args_b
, idx
);
3780 newline_and_indent (pp
, indent
+ 2);
3781 /* Can do elision here, printing "[...]". */
3782 if (flag_elide_type
)
3783 pp_string (pp
, G_("[...]"));
3785 pp_string (pp
, arg_to_string (arg_a
, verbose
));
3789 int new_indent
= indent
? indent
+ 2 : 0;
3790 if (comparable_template_types_p (arg_a
, arg_b
))
3791 print_template_differences (pp
, arg_a
, arg_b
, verbose
, new_indent
);
3795 newline_and_indent (pp
, indent
+ 2);
3796 pp_character (pp
, '[');
3797 print_nonequal_arg (pp
, arg_a
, verbose
);
3798 pp_string (pp
, " != ");
3799 print_nonequal_arg (pp
, arg_b
, verbose
);
3800 pp_character (pp
, ']');
3803 print_nonequal_arg (pp
, arg_a
, verbose
);
3806 pp_printf (pp
, ">");
3809 /* As type_to_string, but for a template, potentially colorizing/eliding
3810 in comparison with PEER.
3811 For example, if TYPE is map<int,double> and PEER is map<int,int>,
3812 then the resulting string would be:
3814 with type elision, and:
3816 without type elision.
3818 In both cases the parts of TYPE that differ from PEER will be colorized
3819 if SHOW_COLOR is true. In the above example, this would be "double".
3821 Template arguments in which both types are using the default arguments
3822 are not printed; if at least one of the two types is using a non-default
3823 argument, then both arguments are printed.
3825 The resulting string is in a GC-allocated buffer. */
3828 type_to_string_with_compare (tree type
, tree peer
, bool verbose
,
3831 pretty_printer inner_pp
;
3832 pretty_printer
*pp
= &inner_pp
;
3833 pp_show_color (pp
) = show_color
;
3835 print_template_differences (pp
, type
, peer
, verbose
, 0);
3836 return pp_ggc_formatted_text (pp
);
3839 /* Recursively print a tree-like comparison of TYPE_A and TYPE_B to PP,
3840 indented by INDENT spaces.
3842 For example given types:
3844 vector<map<int,double>>
3848 vector<map<double,float>>
3850 the output with type elision would be:
3857 and without type-elision would be:
3864 TYPE_A and TYPE_B must both be comparable template types
3865 (as per comparable_template_types_p).
3867 Template arguments in which both types are using the default arguments
3868 are not printed; if at least one of the two types is using a non-default
3869 argument, then both arguments are printed. */
3872 print_template_tree_comparison (pretty_printer
*pp
, tree type_a
, tree type_b
,
3873 bool verbose
, int indent
)
3875 print_template_differences (pp
, type_a
, type_b
, verbose
, indent
);
3878 /* Subroutine for use in a format_postprocessor::handle
3879 implementation. Adds a chunk to the end of
3880 formatted output, so that it will be printed
3881 by pp_output_formatted_text. */
3884 append_formatted_chunk (pretty_printer
*pp
, const char *content
)
3886 output_buffer
*buffer
= pp_buffer (pp
);
3887 struct chunk_info
*chunk_array
= buffer
->cur_chunk_array
;
3888 const char **args
= chunk_array
->args
;
3890 unsigned int chunk_idx
;
3891 for (chunk_idx
= 0; args
[chunk_idx
]; chunk_idx
++)
3893 args
[chunk_idx
++] = content
;
3894 args
[chunk_idx
] = NULL
;
3897 /* Create a copy of CONTENT, with quotes added, and,
3898 potentially, with colorization.
3899 No escaped is performed on CONTENT.
3900 The result is in a GC-allocated buffer. */
3903 add_quotes (const char *content
, bool show_color
)
3905 pretty_printer tmp_pp
;
3906 pp_show_color (&tmp_pp
) = show_color
;
3908 /* We have to use "%<%s%>" rather than "%qs" here in order to avoid
3909 quoting colorization bytes within the results. */
3910 pp_printf (&tmp_pp
, "%<%s%>", content
);
3912 return pp_ggc_formatted_text (&tmp_pp
);
3915 /* If we had %H and %I, and hence deferred printing them,
3916 print them now, storing the result into the chunk_info
3917 for pp_format. Quote them if 'q' was provided.
3918 Also print the difference in tree form, adding it as
3919 an additional chunk. */
3922 cxx_format_postprocessor::handle (pretty_printer
*pp
)
3924 /* If we have one of %H and %I, the other should have
3926 if (m_type_a
.m_tree
|| m_type_b
.m_tree
)
3928 /* Avoid reentrancy issues by working with a copy of
3929 m_type_a and m_type_b, resetting them now. */
3930 deferred_printed_type type_a
= m_type_a
;
3931 deferred_printed_type type_b
= m_type_b
;
3932 m_type_a
= deferred_printed_type ();
3933 m_type_b
= deferred_printed_type ();
3935 gcc_assert (type_a
.m_buffer_ptr
);
3936 gcc_assert (type_b
.m_buffer_ptr
);
3938 bool show_color
= pp_show_color (pp
);
3940 const char *type_a_text
;
3941 const char *type_b_text
;
3943 if (comparable_template_types_p (type_a
.m_tree
, type_b
.m_tree
))
3946 = type_to_string_with_compare (type_a
.m_tree
, type_b
.m_tree
,
3947 type_a
.m_verbose
, show_color
);
3949 = type_to_string_with_compare (type_b
.m_tree
, type_a
.m_tree
,
3950 type_b
.m_verbose
, show_color
);
3952 if (flag_diagnostics_show_template_tree
)
3954 pretty_printer inner_pp
;
3955 pp_show_color (&inner_pp
) = pp_show_color (pp
);
3956 print_template_tree_comparison
3957 (&inner_pp
, type_a
.m_tree
, type_b
.m_tree
, type_a
.m_verbose
, 2);
3958 append_formatted_chunk (pp
, pp_ggc_formatted_text (&inner_pp
));
3963 /* If the types were not comparable (or if only one of %H/%I was
3964 provided), they are printed normally, and no difference tree
3966 type_a_text
= type_to_string (type_a
.m_tree
, type_a
.m_verbose
,
3967 true, &type_a
.m_quote
, show_color
);
3968 type_b_text
= type_to_string (type_b
.m_tree
, type_b
.m_verbose
,
3969 true, &type_b
.m_quote
, show_color
);
3973 type_a_text
= add_quotes (type_a_text
, show_color
);
3974 *type_a
.m_buffer_ptr
= type_a_text
;
3977 type_b_text
= add_quotes (type_b_text
, show_color
);
3978 *type_b
.m_buffer_ptr
= type_b_text
;
3982 /* Subroutine for handling %H and %I, to support i18n of messages like:
3984 error_at (loc, "could not convert %qE from %qH to %qI",
3985 expr, type_a, type_b);
3987 so that we can print things like:
3989 could not convert 'foo' from 'map<int,double>' to 'map<int,int>'
3991 and, with type-elision:
3993 could not convert 'foo' from 'map<[...],double>' to 'map<[...],int>'
3995 (with color-coding of the differences between the types).
3997 The %H and %I format codes are peers: both must be present,
3998 and they affect each other. Hence to handle them, we must
3999 delay printing until we have both, deferring the printing to
4000 pretty_printer's m_format_postprocessor hook.
4002 This is called in phase 2 of pp_format, when it is accumulating
4003 a series of formatted chunks. We stash the location of the chunk
4004 we're meant to have written to, so that we can write to it in the
4005 m_format_postprocessor hook.
4007 We also need to stash whether a 'q' prefix was provided (the QUOTE
4008 param) so that we can add the quotes when writing out the delayed
4012 defer_phase_2_of_type_diff (deferred_printed_type
*deferred
,
4013 tree type
, const char **buffer_ptr
,
4014 bool verbose
, bool quote
)
4016 gcc_assert (deferred
->m_tree
== NULL_TREE
);
4017 gcc_assert (deferred
->m_buffer_ptr
== NULL
);
4018 *deferred
= deferred_printed_type (type
, buffer_ptr
, verbose
, quote
);
4022 /* Called from output_format -- during diagnostic message processing --
4023 to handle C++ specific format specifier with the following meanings:
4024 %A function argument-list.
4028 %F function declaration.
4030 %H type difference (from).
4031 %I type difference (to).
4033 %L language as used in extern "lang".
4035 %P function parameter whose position is indicated by an integer.
4036 %Q assignment operator.
4037 %S substitution (template + args)
4040 %X exception-specification. */
4042 cp_printer (pretty_printer
*pp
, text_info
*text
, const char *spec
,
4043 int precision
, bool wide
, bool set_locus
, bool verbose
,
4044 bool *quoted
, const char **buffer_ptr
)
4046 gcc_assert (pp
->m_format_postprocessor
);
4047 cxx_format_postprocessor
*postprocessor
4048 = static_cast <cxx_format_postprocessor
*> (pp
->m_format_postprocessor
);
4052 #define next_tree (t = va_arg (*text->args_ptr, tree))
4053 #define next_tcode ((enum tree_code) va_arg (*text->args_ptr, int))
4054 #define next_lang ((enum languages) va_arg (*text->args_ptr, int))
4055 #define next_int va_arg (*text->args_ptr, int)
4057 if (precision
!= 0 || wide
)
4062 case 'A': result
= args_to_string (next_tree
, verbose
); break;
4063 case 'C': result
= code_to_string (next_tcode
); break;
4066 tree temp
= next_tree
;
4068 && DECL_HAS_DEBUG_EXPR_P (temp
))
4070 temp
= DECL_DEBUG_EXPR (temp
);
4073 result
= expr_to_string (temp
);
4077 result
= decl_to_string (temp
, verbose
);
4080 case 'E': result
= expr_to_string (next_tree
); break;
4081 case 'F': result
= fndecl_to_string (next_tree
, verbose
); break;
4083 percent_G_format (text
);
4086 defer_phase_2_of_type_diff (&postprocessor
->m_type_a
, next_tree
,
4087 buffer_ptr
, verbose
, *quoted
);
4090 defer_phase_2_of_type_diff (&postprocessor
->m_type_b
, next_tree
,
4091 buffer_ptr
, verbose
, *quoted
);
4094 t
= va_arg (*text
->args_ptr
, tree
);
4095 percent_K_format (text
, EXPR_LOCATION (t
), TREE_BLOCK (t
));
4097 case 'L': result
= language_to_string (next_lang
); break;
4098 case 'O': result
= op_to_string (false, next_tcode
); break;
4099 case 'P': result
= parm_to_string (next_int
); break;
4100 case 'Q': result
= op_to_string (true, next_tcode
); break;
4101 case 'S': result
= subst_to_string (next_tree
); break;
4104 result
= type_to_string (next_tree
, verbose
, false, quoted
,
4105 pp_show_color (pp
));
4108 case 'V': result
= cv_to_string (next_tree
, verbose
); break;
4109 case 'X': result
= eh_spec_to_string (next_tree
, verbose
); break;
4115 pp_string (pp
, result
);
4116 if (set_locus
&& t
!= NULL
)
4117 text
->set_location (0, location_of (t
), SHOW_RANGE_WITH_CARET
);
4125 /* Warn about the use of C++0x features when appropriate. */
4127 maybe_warn_cpp0x (cpp0x_warn_str str
)
4129 if ((cxx_dialect
== cxx98
) && !in_system_header_at (input_location
))
4130 /* We really want to suppress this warning in system headers,
4131 because libstdc++ uses variadic templates even when we aren't
4135 case CPP0X_INITIALIZER_LISTS
:
4136 pedwarn (input_location
, 0,
4137 "extended initializer lists "
4138 "only available with -std=c++11 or -std=gnu++11");
4140 case CPP0X_EXPLICIT_CONVERSION
:
4141 pedwarn (input_location
, 0,
4142 "explicit conversion operators "
4143 "only available with -std=c++11 or -std=gnu++11");
4145 case CPP0X_VARIADIC_TEMPLATES
:
4146 pedwarn (input_location
, 0,
4147 "variadic templates "
4148 "only available with -std=c++11 or -std=gnu++11");
4150 case CPP0X_LAMBDA_EXPR
:
4151 pedwarn (input_location
, 0,
4152 "lambda expressions "
4153 "only available with -std=c++11 or -std=gnu++11");
4156 pedwarn (input_location
, 0,
4157 "C++11 auto only available with -std=c++11 or -std=gnu++11");
4159 case CPP0X_SCOPED_ENUMS
:
4160 pedwarn (input_location
, 0,
4161 "scoped enums only available with -std=c++11 or -std=gnu++11");
4163 case CPP0X_DEFAULTED_DELETED
:
4164 pedwarn (input_location
, 0,
4165 "defaulted and deleted functions "
4166 "only available with -std=c++11 or -std=gnu++11");
4168 case CPP0X_INLINE_NAMESPACES
:
4169 pedwarn (input_location
, OPT_Wpedantic
,
4170 "inline namespaces "
4171 "only available with -std=c++11 or -std=gnu++11");
4173 case CPP0X_OVERRIDE_CONTROLS
:
4174 pedwarn (input_location
, 0,
4175 "override controls (override/final) "
4176 "only available with -std=c++11 or -std=gnu++11");
4179 pedwarn (input_location
, 0,
4180 "non-static data member initializers "
4181 "only available with -std=c++11 or -std=gnu++11");
4183 case CPP0X_USER_DEFINED_LITERALS
:
4184 pedwarn (input_location
, 0,
4185 "user-defined literals "
4186 "only available with -std=c++11 or -std=gnu++11");
4188 case CPP0X_DELEGATING_CTORS
:
4189 pedwarn (input_location
, 0,
4190 "delegating constructors "
4191 "only available with -std=c++11 or -std=gnu++11");
4193 case CPP0X_INHERITING_CTORS
:
4194 pedwarn (input_location
, 0,
4195 "inheriting constructors "
4196 "only available with -std=c++11 or -std=gnu++11");
4198 case CPP0X_ATTRIBUTES
:
4199 pedwarn (input_location
, 0,
4201 "only available with -std=c++11 or -std=gnu++11");
4203 case CPP0X_REF_QUALIFIER
:
4204 pedwarn (input_location
, 0,
4206 "only available with -std=c++11 or -std=gnu++11");
4213 /* Warn about the use of variadic templates when appropriate. */
4215 maybe_warn_variadic_templates (void)
4217 maybe_warn_cpp0x (CPP0X_VARIADIC_TEMPLATES
);
4221 /* Issue an ISO C++98 pedantic warning at LOCATION, conditional on
4222 option OPT with text GMSGID. Use this function to report
4223 diagnostics for constructs that are invalid C++98, but valid
4226 pedwarn_cxx98 (location_t location
, int opt
, const char *gmsgid
, ...)
4228 diagnostic_info diagnostic
;
4231 rich_location
richloc (line_table
, location
);
4233 va_start (ap
, gmsgid
);
4234 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, &richloc
,
4235 (cxx_dialect
== cxx98
) ? DK_PEDWARN
: DK_WARNING
);
4236 diagnostic
.option_index
= opt
;
4237 ret
= diagnostic_report_diagnostic (global_dc
, &diagnostic
);
4242 /* Issue a diagnostic that NAME cannot be found in SCOPE. DECL is what
4243 we found when we tried to do the lookup. LOCATION is the location of
4244 the NAME identifier. */
4247 qualified_name_lookup_error (tree scope
, tree name
,
4248 tree decl
, location_t location
)
4250 if (scope
== error_mark_node
)
4251 ; /* We already complained. */
4252 else if (TYPE_P (scope
))
4254 if (!COMPLETE_TYPE_P (scope
))
4255 error_at (location
, "incomplete type %qT used in nested name specifier",
4257 else if (TREE_CODE (decl
) == TREE_LIST
)
4259 error_at (location
, "reference to %<%T::%D%> is ambiguous",
4261 print_candidates (decl
);
4264 error_at (location
, "%qD is not a member of %qT", name
, scope
);
4266 else if (scope
!= global_namespace
)
4268 auto_diagnostic_group d
;
4269 bool emit_fixit
= true;
4271 = suggest_alternative_in_explicit_scope (location
, name
, scope
);
4274 hint
= suggest_alternatives_in_other_namespaces (location
, name
);
4275 /* "location" is just the location of the name, not of the explicit
4276 scope, and it's not easy to get at the latter, so we can't issue
4277 fix-it hints for the suggestion. */
4280 if (const char *suggestion
= hint
.suggestion ())
4282 gcc_rich_location
richloc (location
);
4284 richloc
.add_fixit_replace (suggestion
);
4285 error_at (&richloc
, "%qD is not a member of %qD; did you mean %qs?",
4286 name
, scope
, suggestion
);
4289 error_at (location
, "%qD is not a member of %qD", name
, scope
);
4293 auto_diagnostic_group d
;
4294 name_hint hint
= suggest_alternatives_for (location
, name
, true);
4295 if (const char *suggestion
= hint
.suggestion ())
4297 gcc_rich_location
richloc (location
);
4298 richloc
.add_fixit_replace (suggestion
);
4300 "%<::%D%> has not been declared; did you mean %qs?",
4304 error_at (location
, "%<::%D%> has not been declared", name
);
4308 /* C++-specific implementation of range_label::get_text () vfunc for
4309 range_label_for_type_mismatch.
4311 Compare with print_template_differences above. */
4314 range_label_for_type_mismatch::get_text (unsigned /*range_idx*/) const
4316 if (m_labelled_type
== NULL_TREE
)
4317 return label_text (NULL
, false);
4319 const bool verbose
= false;
4320 const bool show_color
= false;
4324 && comparable_template_types_p (m_labelled_type
, m_other_type
))
4325 result
= type_to_string_with_compare (m_labelled_type
, m_other_type
,
4326 verbose
, show_color
);
4328 result
= type_to_string (m_labelled_type
, verbose
, true, NULL
, show_color
);
4330 /* Both of the above return GC-allocated buffers, so the caller mustn't
4332 return label_text (const_cast <char *> (result
), false);