c++: Prevent overwriting arguments when merging duplicates [PR112588]
[official-gcc.git] / gcc / cp / error.cc
blob52e24fb086ca52e86737eae5a59bed23a70bf030
1 /* Call-backs for C++ error reporting.
2 This code is non-reentrant.
3 Copyright (C) 1993-2024 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)
9 any later version.
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/>. */
20 #include "config.h"
21 /* For use with name_hint. */
22 #define INCLUDE_MEMORY
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "stringpool.h"
27 #include "tree-diagnostic.h"
28 #include "diagnostic-color.h"
29 #include "langhooks-def.h"
30 #include "intl.h"
31 #include "cxx-pretty-print.h"
32 #include "tree-pretty-print.h"
33 #include "gimple-pretty-print.h"
34 #include "c-family/c-objc.h"
35 #include "ubsan.h"
36 #include "internal-fn.h"
37 #include "gcc-rich-location.h"
38 #include "cp-name-hint.h"
39 #include "attribs.h"
41 #define pp_separate_with_comma(PP) pp_cxx_separate_with (PP, ',')
42 #define pp_separate_with_semicolon(PP) pp_cxx_separate_with (PP, ';')
44 /* cxx_pp is a C++ front-end-specific pretty printer: this is where we
45 dump C++ ASTs as strings. It is mostly used only by the various
46 tree -> string functions that are occasionally called from the
47 debugger or by the front-end for things like
48 __PRETTY_FUNCTION__. */
49 static cxx_pretty_printer actual_pretty_printer;
50 static cxx_pretty_printer * const cxx_pp = &actual_pretty_printer;
52 /* Translate if being used for diagnostics, but not for dump files or
53 __PRETTY_FUNCTION. */
54 #define M_(msgid) (pp_translate_identifiers (cxx_pp) ? _(msgid) : (msgid))
56 # define NEXT_CODE(T) (TREE_CODE (TREE_TYPE (T)))
58 static const char *args_to_string (tree, int);
59 static const char *code_to_string (enum tree_code);
60 static const char *cv_to_string (tree, int);
61 static const char *decl_to_string (tree, int, bool);
62 static const char *fndecl_to_string (tree, int);
63 static const char *op_to_string (bool, enum tree_code);
64 static const char *parm_to_string (int);
65 static const char *type_to_string (tree, int, bool, bool *, bool);
67 static void dump_alias_template_specialization (cxx_pretty_printer *, tree, int);
68 static void dump_type (cxx_pretty_printer *, tree, int);
69 static void dump_typename (cxx_pretty_printer *, tree, int);
70 static void dump_simple_decl (cxx_pretty_printer *, tree, tree, int);
71 static void dump_decl (cxx_pretty_printer *, tree, int);
72 static void dump_template_decl (cxx_pretty_printer *, tree, int);
73 static void dump_function_decl (cxx_pretty_printer *, tree, int);
74 static void dump_expr (cxx_pretty_printer *, tree, int);
75 static void dump_unary_op (cxx_pretty_printer *, const char *, tree, int);
76 static void dump_binary_op (cxx_pretty_printer *, const char *, tree, int);
77 static void dump_aggr_type (cxx_pretty_printer *, tree, int);
78 static void dump_type_prefix (cxx_pretty_printer *, tree, int);
79 static void dump_type_suffix (cxx_pretty_printer *, tree, int);
80 static void dump_function_name (cxx_pretty_printer *, tree, int);
81 static void dump_call_expr_args (cxx_pretty_printer *, tree, int, bool);
82 static void dump_aggr_init_expr_args (cxx_pretty_printer *, tree, int, bool);
83 static void dump_expr_list (cxx_pretty_printer *, tree, int);
84 static void dump_global_iord (cxx_pretty_printer *, tree);
85 static void dump_parameters (cxx_pretty_printer *, tree, int);
86 static void dump_ref_qualifier (cxx_pretty_printer *, tree, int);
87 static void dump_exception_spec (cxx_pretty_printer *, tree, int);
88 static void dump_template_argument (cxx_pretty_printer *, tree, int);
89 static void dump_template_argument_list (cxx_pretty_printer *, tree, int);
90 static void dump_template_parameter (cxx_pretty_printer *, tree, int);
91 static void dump_template_bindings (cxx_pretty_printer *, tree, tree,
92 vec<tree, va_gc> *);
93 static void dump_scope (cxx_pretty_printer *, tree, int);
94 static void dump_template_parms (cxx_pretty_printer *, tree, int, int);
95 static int get_non_default_template_args_count (tree, int);
96 static const char *function_category (tree);
97 static void maybe_print_constexpr_context (diagnostic_context *);
98 static void maybe_print_instantiation_context (diagnostic_context *);
99 static void print_instantiation_full_context (diagnostic_context *);
100 static void print_instantiation_partial_context (diagnostic_context *,
101 struct tinst_level *,
102 location_t);
103 static void maybe_print_constraint_context (diagnostic_context *);
104 static void cp_diagnostic_starter (diagnostic_context *,
105 const diagnostic_info *);
106 static void cp_print_error_function (diagnostic_context *,
107 const diagnostic_info *);
109 static bool cp_printer (pretty_printer *, text_info *, const char *,
110 int, bool, bool, bool, bool *, const char **);
112 /* Struct for handling %H or %I, which require delaying printing the
113 type until a postprocessing stage. */
115 class deferred_printed_type
117 public:
118 deferred_printed_type ()
119 : m_tree (NULL_TREE), m_buffer_ptr (NULL), m_verbose (false), m_quote (false)
122 deferred_printed_type (tree type, const char **buffer_ptr, bool verbose,
123 bool quote)
124 : m_tree (type), m_buffer_ptr (buffer_ptr), m_verbose (verbose),
125 m_quote (quote)
127 gcc_assert (type);
128 gcc_assert (buffer_ptr);
131 /* The tree is not GTY-marked: they are only non-NULL within a
132 call to pp_format. */
133 tree m_tree;
134 const char **m_buffer_ptr;
135 bool m_verbose;
136 bool m_quote;
139 /* Subclass of format_postprocessor for the C++ frontend.
140 This handles the %H and %I formatting codes, printing them
141 in a postprocessing phase (since they affect each other). */
143 class cxx_format_postprocessor : public format_postprocessor
145 public:
146 cxx_format_postprocessor ()
147 : m_type_a (), m_type_b ()
150 format_postprocessor *clone() const final override
152 return new cxx_format_postprocessor ();
155 void handle (pretty_printer *pp) final override;
157 deferred_printed_type m_type_a;
158 deferred_printed_type m_type_b;
161 /* CONTEXT->printer is a basic pretty printer that was constructed
162 presumably by diagnostic_initialize(), called early in the
163 compiler's initialization process (in general_init) Before the FE
164 is initialized. This (C++) FE-specific diagnostic initializer is
165 thus replacing the basic pretty printer with one that has C++-aware
166 capacities. */
168 void
169 cxx_initialize_diagnostics (diagnostic_context *context)
171 pretty_printer *base = context->printer;
172 cxx_pretty_printer *pp = XNEW (cxx_pretty_printer);
173 context->printer = new (pp) cxx_pretty_printer ();
175 /* It is safe to free this object because it was previously XNEW()'d. */
176 base->~pretty_printer ();
177 XDELETE (base);
179 c_common_diagnostics_set_defaults (context);
180 diagnostic_starter (context) = cp_diagnostic_starter;
181 /* diagnostic_finalizer is already c_diagnostic_finalizer. */
182 diagnostic_format_decoder (context) = cp_printer;
183 pp->m_format_postprocessor = new cxx_format_postprocessor ();
186 /* Dump an '@module' name suffix for DECL, if any. */
188 static void
189 dump_module_suffix (cxx_pretty_printer *pp, tree decl)
191 if (!modules_p ())
192 return;
194 if (!DECL_CONTEXT (decl))
195 return;
197 if (TREE_CODE (decl) != CONST_DECL
198 || !UNSCOPED_ENUM_P (DECL_CONTEXT (decl)))
200 if (!DECL_NAMESPACE_SCOPE_P (decl))
201 return;
203 if (TREE_CODE (decl) == NAMESPACE_DECL
204 && !DECL_NAMESPACE_ALIAS (decl)
205 && (TREE_PUBLIC (decl) || !TREE_PUBLIC (CP_DECL_CONTEXT (decl))))
206 return;
209 if (unsigned m = get_originating_module (decl))
210 if (const char *n = module_name (m, false))
212 pp_character (pp, '@');
213 pp->padding = pp_none;
214 pp_string (pp, n);
218 /* The scope of the declaration we're currently printing, to avoid redundantly
219 dumping the same scope on parameter types. */
220 static tree current_dump_scope;
222 /* Dump a scope, if deemed necessary. */
224 static void
225 dump_scope (cxx_pretty_printer *pp, tree scope, int flags)
227 int f = flags & (TFF_SCOPE | TFF_CHASE_TYPEDEF);
229 if (scope == NULL_TREE || scope == current_dump_scope)
230 return;
232 /* Enum values within an unscoped enum will be CONST_DECL with an
233 ENUMERAL_TYPE as their "scope". Use CP_TYPE_CONTEXT of the
234 ENUMERAL_TYPE, so as to print any enclosing namespace. */
235 if (UNSCOPED_ENUM_P (scope))
236 scope = CP_TYPE_CONTEXT (scope);
238 if (TREE_CODE (scope) == NAMESPACE_DECL)
240 if (scope != global_namespace)
242 dump_decl (pp, scope, f);
243 pp_cxx_colon_colon (pp);
246 else if (AGGREGATE_TYPE_P (scope)
247 || SCOPED_ENUM_P (scope))
249 dump_type (pp, scope, f);
250 pp_cxx_colon_colon (pp);
252 else if ((flags & TFF_SCOPE) && TREE_CODE (scope) == FUNCTION_DECL)
254 dump_function_decl (pp, scope, f | TFF_NO_TEMPLATE_BINDINGS);
255 pp_cxx_colon_colon (pp);
259 /* Dump the template ARGument under control of FLAGS. */
261 static void
262 dump_template_argument (cxx_pretty_printer *pp, tree arg, int flags)
264 if (ARGUMENT_PACK_P (arg))
265 dump_template_argument_list (pp, ARGUMENT_PACK_ARGS (arg),
266 /* No default args in argument packs. */
267 flags|TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS);
268 else if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
269 dump_type (pp, arg, flags & ~TFF_CLASS_KEY_OR_ENUM);
270 else
272 if (TREE_CODE (arg) == TREE_LIST)
273 arg = TREE_VALUE (arg);
275 /* Strip implicit conversions. */
276 while (CONVERT_EXPR_P (arg))
277 arg = TREE_OPERAND (arg, 0);
279 dump_expr (pp, arg, (flags | TFF_EXPR_IN_PARENS) & ~TFF_CLASS_KEY_OR_ENUM);
283 /* Count the number of template arguments ARGS whose value does not
284 match the (optional) default template parameter in PARAMS */
286 static int
287 get_non_default_template_args_count (tree args, int flags)
289 int n = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_ARGS (args));
291 if (/* We use this flag when generating debug information. We don't
292 want to expand templates at this point, for this may generate
293 new decls, which gets decl counts out of sync, which may in
294 turn cause codegen differences between compilations with and
295 without -g. */
296 (flags & TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS) != 0
297 || !flag_pretty_templates)
298 return n;
300 return GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (INNERMOST_TEMPLATE_ARGS (args));
303 /* Dump a template-argument-list ARGS (always a TREE_VEC) under control
304 of FLAGS. */
306 static void
307 dump_template_argument_list (cxx_pretty_printer *pp, tree args, int flags)
309 int n = get_non_default_template_args_count (args, flags);
310 int need_comma = 0;
311 int i;
313 for (i = 0; i < n; ++i)
315 tree arg = TREE_VEC_ELT (args, i);
317 /* Only print a comma if we know there is an argument coming. In
318 the case of an empty template argument pack, no actual
319 argument will be printed. */
320 if (need_comma
321 && (!ARGUMENT_PACK_P (arg)
322 || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) > 0))
323 pp_separate_with_comma (pp);
325 dump_template_argument (pp, arg, flags);
326 need_comma = 1;
330 /* Dump a template parameter PARM (a TREE_LIST) under control of FLAGS. */
332 static void
333 dump_template_parameter (cxx_pretty_printer *pp, tree parm, int flags)
335 tree p;
336 tree a;
338 if (parm == error_mark_node)
339 return;
341 p = TREE_VALUE (parm);
342 a = TREE_PURPOSE (parm);
344 if (TREE_CODE (p) == TYPE_DECL)
346 if (flags & TFF_DECL_SPECIFIERS)
348 pp_cxx_ws_string (pp, "class");
349 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (p)))
350 pp_cxx_ws_string (pp, "...");
351 if (DECL_NAME (p))
352 pp_cxx_tree_identifier (pp, DECL_NAME (p));
354 else if (DECL_NAME (p))
355 pp_cxx_tree_identifier (pp, DECL_NAME (p));
356 else
357 pp_cxx_canonical_template_parameter (pp, TREE_TYPE (p));
359 else
360 dump_decl (pp, p, flags | TFF_DECL_SPECIFIERS);
362 if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && a != NULL_TREE)
364 pp_cxx_whitespace (pp);
365 pp_equal (pp);
366 pp_cxx_whitespace (pp);
367 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
368 dump_type (pp, a, flags & ~TFF_CHASE_TYPEDEF);
369 else
370 dump_expr (pp, a, flags | TFF_EXPR_IN_PARENS);
374 /* Dump, under control of FLAGS, a template-parameter-list binding.
375 PARMS is a TREE_LIST of TREE_VEC of TREE_LIST and ARGS is a
376 TREE_VEC. */
378 static void
379 dump_template_bindings (cxx_pretty_printer *pp, tree parms, tree args,
380 vec<tree, va_gc> *typenames)
382 /* Print "[with" and ']', conditional on whether anything is printed at all.
383 This is tied to whether a semicolon is needed to separate multiple template
384 parameters. */
385 struct prepost_semicolon
387 cxx_pretty_printer *pp;
388 bool need_semicolon;
390 void operator() ()
392 if (need_semicolon)
393 pp_separate_with_semicolon (pp);
394 else
396 pp_cxx_whitespace (pp);
397 pp_string (pp, colorize_start (pp_show_color (pp), "targs"));
398 pp_cxx_left_bracket (pp);
399 pp->translate_string ("with");
400 pp_cxx_whitespace (pp);
401 need_semicolon = true;
405 ~prepost_semicolon ()
407 if (need_semicolon)
409 pp_cxx_right_bracket (pp);
410 pp_string (pp, colorize_stop (pp_show_color (pp)));
413 } semicolon_or_introducer = {pp, false};
415 int i;
416 tree t;
418 while (parms)
420 tree p = TREE_VALUE (parms);
421 int lvl = TMPL_PARMS_DEPTH (parms);
422 int arg_idx = 0;
423 int i;
424 tree lvl_args = NULL_TREE;
426 /* Don't crash if we had an invalid argument list. */
427 if (TMPL_ARGS_DEPTH (args) >= lvl)
428 lvl_args = TMPL_ARGS_LEVEL (args, lvl);
430 for (i = 0; i < TREE_VEC_LENGTH (p); ++i)
432 tree arg = NULL_TREE;
434 /* Don't crash if we had an invalid argument list. */
435 if (lvl_args && NUM_TMPL_ARGS (lvl_args) > arg_idx)
436 arg = TREE_VEC_ELT (lvl_args, arg_idx);
438 tree parm_i = TREE_VEC_ELT (p, i);
439 /* If the template argument repeats the template parameter (T = T),
440 skip the parameter.*/
441 if (arg && TREE_CODE (arg) == TEMPLATE_TYPE_PARM
442 && TREE_CODE (parm_i) == TREE_LIST
443 && TREE_CODE (TREE_VALUE (parm_i)) == TYPE_DECL
444 && TREE_CODE (TREE_TYPE (TREE_VALUE (parm_i)))
445 == TEMPLATE_TYPE_PARM
446 && DECL_NAME (TREE_VALUE (parm_i))
447 == DECL_NAME (TREE_CHAIN (arg)))
448 continue;
450 semicolon_or_introducer ();
451 dump_template_parameter (pp, parm_i, TFF_PLAIN_IDENTIFIER);
452 pp_cxx_whitespace (pp);
453 pp_equal (pp);
454 pp_cxx_whitespace (pp);
455 if (arg)
457 if (ARGUMENT_PACK_P (arg))
458 pp_cxx_left_brace (pp);
459 dump_template_argument (pp, arg, TFF_PLAIN_IDENTIFIER);
460 if (ARGUMENT_PACK_P (arg))
461 pp_cxx_right_brace (pp);
463 else
464 pp_string (pp, M_("<missing>"));
466 ++arg_idx;
469 parms = TREE_CHAIN (parms);
472 /* Don't bother with typenames for a partial instantiation. */
473 if (vec_safe_is_empty (typenames) || uses_template_parms (args))
474 return;
476 /* Don't try to print typenames when we're processing a clone. */
477 if (current_function_decl
478 && !DECL_LANG_SPECIFIC (current_function_decl))
479 return;
481 /* Don't try to do this once cgraph starts throwing away front-end
482 information. */
483 if (at_eof >= 3)
484 return;
486 FOR_EACH_VEC_SAFE_ELT (typenames, i, t)
488 semicolon_or_introducer ();
489 dump_type (pp, t, TFF_PLAIN_IDENTIFIER);
490 pp_cxx_whitespace (pp);
491 pp_equal (pp);
492 pp_cxx_whitespace (pp);
493 push_deferring_access_checks (dk_no_check);
494 t = tsubst (t, args, tf_none, NULL_TREE);
495 pop_deferring_access_checks ();
496 /* Strip typedefs. We can't just use TFF_CHASE_TYPEDEF because
497 pp_simple_type_specifier doesn't know about it. */
498 t = strip_typedefs (t, NULL, STF_USER_VISIBLE);
499 dump_type (pp, t, TFF_PLAIN_IDENTIFIER);
503 /* Dump a human-readable equivalent of the alias template
504 specialization of T. */
506 static void
507 dump_alias_template_specialization (cxx_pretty_printer *pp, tree t, int flags)
509 gcc_assert (alias_template_specialization_p (t, nt_opaque));
511 tree decl = TYPE_NAME (t);
512 if (!(flags & TFF_UNQUALIFIED_NAME))
513 dump_scope (pp, CP_DECL_CONTEXT (decl), flags);
514 pp_cxx_tree_identifier (pp, DECL_NAME (decl));
515 dump_template_parms (pp, DECL_TEMPLATE_INFO (decl),
516 /*primary=*/false,
517 flags & ~TFF_TEMPLATE_HEADER);
520 /* Dump a human-readable equivalent of TYPE. FLAGS controls the
521 format. */
523 static void
524 dump_type (cxx_pretty_printer *pp, tree t, int flags)
526 if (t == NULL_TREE)
527 return;
529 /* Don't print e.g. "struct mytypedef". */
530 if (TYPE_P (t) && typedef_variant_p (t))
532 tree decl = TYPE_NAME (t);
533 if ((flags & TFF_CHASE_TYPEDEF)
534 || DECL_SELF_REFERENCE_P (decl)
535 || (!flag_pretty_templates
536 && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)))
538 unsigned int stf_flags = (!(pp->flags & pp_c_flag_gnu_v3)
539 ? STF_USER_VISIBLE : 0);
540 t = strip_typedefs (t, NULL, stf_flags);
542 else if (alias_template_specialization_p (t, nt_opaque))
544 dump_alias_template_specialization (pp, t, flags);
545 return;
547 else if (same_type_p (t, TREE_TYPE (decl)))
548 t = decl;
549 else
551 pp_cxx_cv_qualifier_seq (pp, t);
552 if (! (flags & TFF_UNQUALIFIED_NAME))
553 dump_scope (pp, CP_DECL_CONTEXT (decl), flags);
554 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
555 return;
559 if (TYPE_PTRMEMFUNC_P (t))
560 goto offset_type;
562 switch (TREE_CODE (t))
564 case LANG_TYPE:
565 if (t == init_list_type_node)
566 pp_string (pp, M_("<brace-enclosed initializer list>"));
567 else if (t == unknown_type_node)
568 pp_string (pp, M_("<unresolved overloaded function type>"));
569 else
571 pp_cxx_cv_qualifier_seq (pp, t);
572 if (tree id = TYPE_IDENTIFIER (t))
573 pp_cxx_tree_identifier (pp, id);
575 break;
577 case TREE_LIST:
578 /* A list of function parms. */
579 dump_parameters (pp, t, flags);
580 break;
582 case IDENTIFIER_NODE:
583 pp_cxx_tree_identifier (pp, t);
584 break;
586 case TREE_BINFO:
587 dump_type (pp, BINFO_TYPE (t), flags);
588 break;
590 case RECORD_TYPE:
591 case UNION_TYPE:
592 case ENUMERAL_TYPE:
593 dump_aggr_type (pp, t, flags);
594 break;
596 case TYPE_DECL:
597 if (flags & TFF_CHASE_TYPEDEF)
599 dump_type (pp, DECL_ORIGINAL_TYPE (t)
600 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t), flags);
601 break;
603 /* Fall through. */
605 case TEMPLATE_DECL:
606 case NAMESPACE_DECL:
607 dump_decl (pp, t, flags & ~TFF_DECL_SPECIFIERS);
608 break;
610 case INTEGER_TYPE:
611 case REAL_TYPE:
612 case VOID_TYPE:
613 case OPAQUE_TYPE:
614 case BOOLEAN_TYPE:
615 case COMPLEX_TYPE:
616 case VECTOR_TYPE:
617 case FIXED_POINT_TYPE:
618 pp_type_specifier_seq (pp, t);
619 break;
621 case TEMPLATE_TEMPLATE_PARM:
622 /* For parameters inside template signature. */
623 if (TYPE_IDENTIFIER (t))
624 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
625 else
626 pp_cxx_canonical_template_parameter (pp, t);
627 break;
629 case BOUND_TEMPLATE_TEMPLATE_PARM:
631 tree args = TYPE_TI_ARGS (t);
632 pp_cxx_cv_qualifier_seq (pp, t);
633 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
634 pp_cxx_begin_template_argument_list (pp);
635 dump_template_argument_list (pp, args, flags);
636 pp_cxx_end_template_argument_list (pp);
638 break;
640 case TEMPLATE_TYPE_PARM:
641 pp_cxx_cv_qualifier_seq (pp, t);
642 if (template_placeholder_p (t))
644 tree tmpl = TREE_TYPE (CLASS_PLACEHOLDER_TEMPLATE (t));
645 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (tmpl));
646 pp_string (pp, "<...auto...>");
648 else if (TYPE_IDENTIFIER (t))
649 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
650 else
651 pp_cxx_canonical_template_parameter
652 (pp, TEMPLATE_TYPE_PARM_INDEX (t));
653 /* If this is a constrained placeholder, add the requirements. */
654 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
655 pp_cxx_constrained_type_spec (pp, c);
656 break;
658 /* This is not always necessary for pointers and such, but doing this
659 reduces code size. */
660 case ARRAY_TYPE:
661 case POINTER_TYPE:
662 case REFERENCE_TYPE:
663 case OFFSET_TYPE:
664 offset_type:
665 case FUNCTION_TYPE:
666 case METHOD_TYPE:
668 dump_type_prefix (pp, t, flags);
669 dump_type_suffix (pp, t, flags);
670 break;
672 case TYPENAME_TYPE:
673 if (! (flags & TFF_CHASE_TYPEDEF)
674 && DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
676 dump_decl (pp, TYPE_NAME (t), TFF_PLAIN_IDENTIFIER);
677 break;
679 pp_cxx_cv_qualifier_seq (pp, t);
680 pp_cxx_ws_string (pp,
681 TYPENAME_IS_ENUM_P (t) ? "enum"
682 : TYPENAME_IS_CLASS_P (t) ? "class"
683 : "typename");
684 dump_typename (pp, t, flags);
685 break;
687 case UNBOUND_CLASS_TEMPLATE:
688 if (! (flags & TFF_UNQUALIFIED_NAME))
690 dump_type (pp, TYPE_CONTEXT (t), flags);
691 pp_cxx_colon_colon (pp);
693 pp_cxx_ws_string (pp, "template");
694 dump_type (pp, TYPE_IDENTIFIER (t), flags);
695 break;
697 case TYPEOF_TYPE:
698 pp_cxx_ws_string (pp, "__typeof__");
699 pp_cxx_whitespace (pp);
700 pp_cxx_left_paren (pp);
701 dump_expr (pp, TYPEOF_TYPE_EXPR (t), flags & ~TFF_EXPR_IN_PARENS);
702 pp_cxx_right_paren (pp);
703 break;
705 case TRAIT_TYPE:
706 pp_cxx_trait (pp, t);
707 break;
709 case TYPE_PACK_EXPANSION:
710 dump_type (pp, PACK_EXPANSION_PATTERN (t), flags);
711 pp_cxx_ws_string (pp, "...");
712 break;
714 case TYPE_ARGUMENT_PACK:
715 dump_template_argument (pp, t, flags);
716 break;
718 case DECLTYPE_TYPE:
719 pp_cxx_ws_string (pp, "decltype");
720 pp_cxx_whitespace (pp);
721 pp_cxx_left_paren (pp);
722 dump_expr (pp, DECLTYPE_TYPE_EXPR (t), flags & ~TFF_EXPR_IN_PARENS);
723 pp_cxx_right_paren (pp);
724 break;
726 case NULLPTR_TYPE:
727 pp_string (pp, "std::nullptr_t");
728 break;
730 default:
731 pp_unsupported_tree (pp, t);
732 /* Fall through. */
734 case ERROR_MARK:
735 pp_string (pp, M_("<type error>"));
736 break;
740 /* Dump a TYPENAME_TYPE. We need to notice when the context is itself
741 a TYPENAME_TYPE. */
743 static void
744 dump_typename (cxx_pretty_printer *pp, tree t, int flags)
746 tree ctx = TYPE_CONTEXT (t);
748 if (TREE_CODE (ctx) == TYPENAME_TYPE)
749 dump_typename (pp, ctx, flags);
750 else
751 dump_type (pp, ctx, flags & ~TFF_CLASS_KEY_OR_ENUM);
752 pp_cxx_colon_colon (pp);
753 dump_decl (pp, TYPENAME_TYPE_FULLNAME (t), flags);
756 /* Return the name of the supplied aggregate, or enumeral type. */
758 const char *
759 class_key_or_enum_as_string (tree t)
761 if (TREE_CODE (t) == ENUMERAL_TYPE)
763 if (SCOPED_ENUM_P (t))
764 return "enum class";
765 else
766 return "enum";
768 else if (TREE_CODE (t) == UNION_TYPE)
769 return "union";
770 else if (TYPE_LANG_SPECIFIC (t) && CLASSTYPE_DECLARED_CLASS (t))
771 return "class";
772 else
773 return "struct";
776 /* Disable warnings about missing quoting in GCC diagnostics for
777 the pp_verbatim call. Their format strings deliberately don't
778 follow GCC diagnostic conventions. */
779 #if __GNUC__ >= 10
780 #pragma GCC diagnostic push
781 #pragma GCC diagnostic ignored "-Wformat-diag"
782 #endif
784 /* Print out a class declaration T under the control of FLAGS,
785 in the form `class foo'. */
787 static void
788 dump_aggr_type (cxx_pretty_printer *pp, tree t, int flags)
790 const char *variety = class_key_or_enum_as_string (t);
791 int typdef = 0;
792 int tmplate = 0;
794 pp_cxx_cv_qualifier_seq (pp, t);
796 if (flags & TFF_CLASS_KEY_OR_ENUM)
797 pp_cxx_ws_string (pp, variety);
799 tree decl = TYPE_NAME (t);
801 if (decl)
803 typdef = (!DECL_ARTIFICIAL (decl)
804 /* An alias specialization is not considered to be a
805 typedef. */
806 && !alias_template_specialization_p (t, nt_opaque));
808 if ((typdef
809 && ((flags & TFF_CHASE_TYPEDEF)
810 || (!flag_pretty_templates && DECL_LANG_SPECIFIC (decl)
811 && DECL_TEMPLATE_INFO (decl))))
812 || DECL_SELF_REFERENCE_P (decl))
814 t = TYPE_MAIN_VARIANT (t);
815 decl = TYPE_NAME (t);
816 typdef = 0;
819 tmplate = !typdef && TREE_CODE (t) != ENUMERAL_TYPE
820 && TYPE_LANG_SPECIFIC (t) && CLASSTYPE_TEMPLATE_INFO (t)
821 && (TREE_CODE (CLASSTYPE_TI_TEMPLATE (t)) != TEMPLATE_DECL
822 || PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
824 if (! (flags & TFF_UNQUALIFIED_NAME))
825 dump_scope (pp, CP_DECL_CONTEXT (decl), flags | TFF_SCOPE);
826 flags &= ~TFF_UNQUALIFIED_NAME;
827 if (tmplate)
829 /* Because the template names are mangled, we have to locate
830 the most general template, and use that name. */
831 tree tpl = TYPE_TI_TEMPLATE (t);
833 while (DECL_TEMPLATE_INFO (tpl))
834 tpl = DECL_TI_TEMPLATE (tpl);
835 decl = tpl;
839 if (LAMBDA_TYPE_P (t))
841 /* A lambda's "type" is essentially its signature. */
842 pp_string (pp, M_("<lambda"));
843 tree const fn = lambda_function (t);
844 if (fn)
846 int const parm_flags
847 = DECL_XOBJ_MEMBER_FUNCTION_P (fn) ? TFF_XOBJ_FUNC | flags
848 : flags;
849 dump_parameters (pp, FUNCTION_FIRST_USER_PARMTYPE (fn), parm_flags);
851 pp_greater (pp);
853 else if (!decl || IDENTIFIER_ANON_P (DECL_NAME (decl)))
855 if (flags & TFF_CLASS_KEY_OR_ENUM)
856 pp_string (pp, M_("<unnamed>"));
857 else
858 pp_printf (pp, M_("<unnamed %s>"), variety);
860 else
861 pp_cxx_tree_identifier (pp, DECL_NAME (decl));
863 dump_module_suffix (pp, decl);
865 if (tmplate)
866 dump_template_parms (pp, TYPE_TEMPLATE_INFO (t),
867 !CLASSTYPE_USE_TEMPLATE (t),
868 flags & ~TFF_TEMPLATE_HEADER);
871 #if __GNUC__ >= 10
872 #pragma GCC diagnostic pop
873 #endif
875 /* Dump into the obstack the initial part of the output for a given type.
876 This is necessary when dealing with things like functions returning
877 functions. Examples:
879 return type of `int (* fee ())()': pointer -> function -> int. Both
880 pointer (and reference and offset) and function (and member) types must
881 deal with prefix and suffix.
883 Arrays must also do this for DECL nodes, like int a[], and for things like
884 int *[]&. */
886 static void
887 dump_type_prefix (cxx_pretty_printer *pp, tree t, int flags)
889 if (TYPE_PTRMEMFUNC_P (t))
891 t = TYPE_PTRMEMFUNC_FN_TYPE (t);
892 goto offset_type;
895 switch (TREE_CODE (t))
897 case POINTER_TYPE:
898 case REFERENCE_TYPE:
900 tree sub = TREE_TYPE (t);
902 dump_type_prefix (pp, sub, flags);
903 if (TREE_CODE (sub) == ARRAY_TYPE
904 || TREE_CODE (sub) == FUNCTION_TYPE)
906 pp_cxx_whitespace (pp);
907 pp_cxx_left_paren (pp);
908 /* If we're dealing with the GNU form of attributes, print this:
909 void (__attribute__((noreturn)) *f) ();
910 If it is the standard [[]] attribute, we'll print the attribute
911 in dump_type_suffix. */
912 if (!cxx11_attribute_p (TYPE_ATTRIBUTES (sub)))
913 pp_c_attributes_display (pp, TYPE_ATTRIBUTES (sub));
915 if (TYPE_PTR_P (t))
916 pp_star (pp);
917 else if (TYPE_REF_P (t))
919 if (TYPE_REF_IS_RVALUE (t))
920 pp_ampersand_ampersand (pp);
921 else
922 pp_ampersand (pp);
924 pp->padding = pp_before;
925 pp_cxx_cv_qualifier_seq (pp, t);
927 break;
929 case OFFSET_TYPE:
930 offset_type:
931 dump_type_prefix (pp, TREE_TYPE (t), flags);
932 if (TREE_CODE (t) == OFFSET_TYPE) /* pmfs deal with this in d_t_p */
934 pp_maybe_space (pp);
935 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
936 pp_cxx_left_paren (pp);
937 dump_type (pp, TYPE_OFFSET_BASETYPE (t), flags);
938 pp_cxx_colon_colon (pp);
940 pp_cxx_star (pp);
941 pp_cxx_cv_qualifier_seq (pp, t);
942 pp->padding = pp_before;
943 break;
945 /* This can be reached without a pointer when dealing with
946 templates, e.g. std::is_function. */
947 case FUNCTION_TYPE:
948 dump_type_prefix (pp, TREE_TYPE (t), flags);
949 break;
951 case METHOD_TYPE:
952 dump_type_prefix (pp, TREE_TYPE (t), flags);
953 pp_maybe_space (pp);
954 pp_cxx_left_paren (pp);
955 dump_aggr_type (pp, TYPE_METHOD_BASETYPE (t), flags);
956 pp_cxx_colon_colon (pp);
957 break;
959 case ARRAY_TYPE:
960 dump_type_prefix (pp, TREE_TYPE (t), flags);
961 break;
963 case ENUMERAL_TYPE:
964 case IDENTIFIER_NODE:
965 case INTEGER_TYPE:
966 case BOOLEAN_TYPE:
967 case REAL_TYPE:
968 case RECORD_TYPE:
969 case TEMPLATE_TYPE_PARM:
970 case TEMPLATE_TEMPLATE_PARM:
971 case BOUND_TEMPLATE_TEMPLATE_PARM:
972 case TREE_LIST:
973 case TYPE_DECL:
974 case TREE_VEC:
975 case UNION_TYPE:
976 case LANG_TYPE:
977 case VOID_TYPE:
978 case OPAQUE_TYPE:
979 case TYPENAME_TYPE:
980 case COMPLEX_TYPE:
981 case VECTOR_TYPE:
982 case TYPEOF_TYPE:
983 case TRAIT_TYPE:
984 case DECLTYPE_TYPE:
985 case TYPE_PACK_EXPANSION:
986 case FIXED_POINT_TYPE:
987 case NULLPTR_TYPE:
988 dump_type (pp, t, flags);
989 pp->padding = pp_before;
990 break;
992 default:
993 pp_unsupported_tree (pp, t);
994 /* fall through. */
995 case ERROR_MARK:
996 pp_string (pp, M_("<typeprefixerror>"));
997 break;
1001 /* Dump the suffix of type T, under control of FLAGS. This is the part
1002 which appears after the identifier (or function parms). */
1004 static void
1005 dump_type_suffix (cxx_pretty_printer *pp, tree t, int flags)
1007 if (TYPE_PTRMEMFUNC_P (t))
1008 t = TYPE_PTRMEMFUNC_FN_TYPE (t);
1010 switch (TREE_CODE (t))
1012 case POINTER_TYPE:
1013 case REFERENCE_TYPE:
1014 case OFFSET_TYPE:
1015 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
1016 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
1017 pp_cxx_right_paren (pp);
1018 if (TREE_CODE (t) == POINTER_TYPE)
1019 flags |= TFF_POINTER;
1020 dump_type_suffix (pp, TREE_TYPE (t), flags);
1021 break;
1023 case FUNCTION_TYPE:
1024 case METHOD_TYPE:
1026 tree arg;
1027 if (TREE_CODE (t) == METHOD_TYPE)
1028 /* Can only be reached through a pointer. */
1029 pp_cxx_right_paren (pp);
1030 arg = TYPE_ARG_TYPES (t);
1031 if (TREE_CODE (t) == METHOD_TYPE)
1032 arg = TREE_CHAIN (arg);
1034 /* Function pointers don't have default args. Not in standard C++,
1035 anyway; they may in g++, but we'll just pretend otherwise. */
1036 dump_parameters (pp, arg, flags & ~TFF_FUNCTION_DEFAULT_ARGUMENTS);
1038 pp->padding = pp_before;
1039 pp_cxx_cv_qualifiers (pp, type_memfn_quals (t),
1040 TREE_CODE (t) == FUNCTION_TYPE
1041 && (flags & TFF_POINTER));
1042 dump_ref_qualifier (pp, t, flags);
1043 if (tx_safe_fn_type_p (t))
1044 pp_cxx_ws_string (pp, "transaction_safe");
1045 dump_exception_spec (pp, TYPE_RAISES_EXCEPTIONS (t), flags);
1046 /* If this is the standard [[]] attribute, print
1047 void (*)() [[noreturn]]; */
1048 if (cxx11_attribute_p (TYPE_ATTRIBUTES (t)))
1050 pp_space (pp);
1051 pp_c_attributes_display (pp, TYPE_ATTRIBUTES (t));
1052 pp->padding = pp_before;
1054 dump_type_suffix (pp, TREE_TYPE (t), flags);
1055 break;
1058 case ARRAY_TYPE:
1059 pp_maybe_space (pp);
1060 pp_cxx_left_bracket (pp);
1061 if (tree dtype = TYPE_DOMAIN (t))
1063 tree max = TYPE_MAX_VALUE (dtype);
1064 /* Zero-length arrays have a null upper bound in C and SIZE_MAX
1065 in C++. Handle both since the type might be constructed by
1066 the middle end and end up here as a result of a warning (see
1067 PR c++/97201). */
1068 if (!max || integer_all_onesp (max))
1069 pp_character (pp, '0');
1070 else if (tree_fits_shwi_p (max))
1071 pp_wide_integer (pp, tree_to_shwi (max) + 1);
1072 else
1074 STRIP_NOPS (max);
1075 if (TREE_CODE (max) == SAVE_EXPR)
1076 max = TREE_OPERAND (max, 0);
1077 if (TREE_CODE (max) == MINUS_EXPR
1078 || TREE_CODE (max) == PLUS_EXPR)
1080 max = TREE_OPERAND (max, 0);
1081 while (CONVERT_EXPR_P (max))
1082 max = TREE_OPERAND (max, 0);
1084 else
1085 max = fold_build2_loc (input_location,
1086 PLUS_EXPR, dtype, max,
1087 build_int_cst (dtype, 1));
1088 dump_expr (pp, max, flags & ~TFF_EXPR_IN_PARENS);
1091 pp_cxx_right_bracket (pp);
1092 dump_type_suffix (pp, TREE_TYPE (t), flags);
1093 break;
1095 case ENUMERAL_TYPE:
1096 case IDENTIFIER_NODE:
1097 case INTEGER_TYPE:
1098 case BOOLEAN_TYPE:
1099 case REAL_TYPE:
1100 case RECORD_TYPE:
1101 case TEMPLATE_TYPE_PARM:
1102 case TEMPLATE_TEMPLATE_PARM:
1103 case BOUND_TEMPLATE_TEMPLATE_PARM:
1104 case TREE_LIST:
1105 case TYPE_DECL:
1106 case TREE_VEC:
1107 case UNION_TYPE:
1108 case LANG_TYPE:
1109 case VOID_TYPE:
1110 case OPAQUE_TYPE:
1111 case TYPENAME_TYPE:
1112 case COMPLEX_TYPE:
1113 case VECTOR_TYPE:
1114 case TYPEOF_TYPE:
1115 case TRAIT_TYPE:
1116 case DECLTYPE_TYPE:
1117 case TYPE_PACK_EXPANSION:
1118 case FIXED_POINT_TYPE:
1119 case NULLPTR_TYPE:
1120 break;
1122 default:
1123 pp_unsupported_tree (pp, t);
1124 case ERROR_MARK:
1125 /* Don't mark it here, we should have already done in
1126 dump_type_prefix. */
1127 break;
1131 static void
1132 dump_global_iord (cxx_pretty_printer *pp, tree t)
1134 const char *p = NULL;
1136 if (DECL_GLOBAL_CTOR_P (t))
1137 p = M_("(static initializers for %s)");
1138 else if (DECL_GLOBAL_DTOR_P (t))
1139 p = M_("(static destructors for %s)");
1140 else
1141 gcc_unreachable ();
1143 pp_printf (pp, p, DECL_SOURCE_FILE (t));
1146 static void
1147 dump_simple_decl (cxx_pretty_printer *pp, tree t, tree type, int flags)
1149 if (VAR_P (t) && DECL_NTTP_OBJECT_P (t))
1150 return dump_expr (pp, DECL_INITIAL (t), flags);
1152 if (flags & TFF_DECL_SPECIFIERS)
1154 if (concept_definition_p (t))
1155 pp_cxx_ws_string (pp, "concept");
1156 else if (VAR_P (t) && DECL_DECLARED_CONSTEXPR_P (t))
1157 pp_cxx_ws_string (pp, "constexpr");
1159 if (!standard_concept_p (t))
1160 dump_type_prefix (pp, type, flags & ~TFF_UNQUALIFIED_NAME);
1161 pp_maybe_space (pp);
1163 if (! (flags & TFF_UNQUALIFIED_NAME)
1164 && TREE_CODE (t) != PARM_DECL
1165 && (!DECL_INITIAL (t)
1166 || TREE_CODE (DECL_INITIAL (t)) != TEMPLATE_PARM_INDEX))
1167 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
1168 flags &= ~TFF_UNQUALIFIED_NAME;
1169 if ((flags & TFF_DECL_SPECIFIERS)
1170 && DECL_TEMPLATE_PARM_P (t)
1171 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (t)))
1172 pp_string (pp, "...");
1173 if (DECL_NAME (t))
1175 if (TREE_CODE (t) == FIELD_DECL && DECL_NORMAL_CAPTURE_P (t))
1177 pp_less (pp);
1178 pp_string (pp, IDENTIFIER_POINTER (DECL_NAME (t)) + 2);
1179 pp_string (pp, " capture>");
1181 else
1182 dump_decl (pp, DECL_NAME (t), flags);
1184 else if (DECL_DECOMPOSITION_P (t))
1185 pp_string (pp, M_("<structured bindings>"));
1186 else if (TREE_CODE (t) == FIELD_DECL && DECL_FIELD_IS_BASE (t))
1187 dump_type (pp, TREE_TYPE (t), flags);
1188 else
1189 pp_string (pp, M_("<anonymous>"));
1191 dump_module_suffix (pp, t);
1193 if (flags & TFF_DECL_SPECIFIERS)
1194 dump_type_suffix (pp, type, flags);
1197 class colorize_guard
1199 bool colorize;
1200 cxx_pretty_printer *pp;
1201 public:
1202 colorize_guard (bool _colorize, cxx_pretty_printer *pp, const char *name)
1203 : colorize (_colorize && pp_show_color (pp)), pp (pp)
1205 pp_string (pp, colorize_start (colorize, name));
1207 ~colorize_guard ()
1209 pp_string (pp, colorize_stop (colorize));
1213 /* Print an IDENTIFIER_NODE that is the name of a declaration. */
1215 static void
1216 dump_decl_name (cxx_pretty_printer *pp, tree t, int flags)
1218 /* These special cases are duplicated here so that other functions
1219 can feed identifiers to error and get them demangled properly. */
1220 if (IDENTIFIER_CONV_OP_P (t))
1222 pp_cxx_ws_string (pp, "operator");
1223 /* Not exactly IDENTIFIER_TYPE_VALUE. */
1224 dump_type (pp, TREE_TYPE (t), flags);
1225 return;
1227 if (dguide_name_p (t))
1229 dump_decl (pp, CLASSTYPE_TI_TEMPLATE (TREE_TYPE (t)),
1230 TFF_UNQUALIFIED_NAME);
1231 return;
1234 const char *str = IDENTIFIER_POINTER (t);
1235 if (startswith (str, "_ZGR"))
1237 pp_cxx_ws_string (pp, "<temporary>");
1238 return;
1241 pp_cxx_tree_identifier (pp, t);
1244 /* Dump a human readable string for the decl T under control of FLAGS. */
1246 static void
1247 dump_decl (cxx_pretty_printer *pp, tree t, int flags)
1249 if (t == NULL_TREE)
1250 return;
1252 /* If doing Objective-C++, give Objective-C a chance to demangle
1253 Objective-C method names. */
1254 if (c_dialect_objc ())
1256 const char *demangled = objc_maybe_printable_name (t, flags);
1257 if (demangled)
1259 pp_string (pp, demangled);
1260 return;
1264 switch (TREE_CODE (t))
1266 case TYPE_DECL:
1267 /* Don't say 'typedef class A' */
1268 if (DECL_ARTIFICIAL (t) && !DECL_SELF_REFERENCE_P (t))
1270 if ((flags & TFF_DECL_SPECIFIERS)
1271 && TREE_CODE (TREE_TYPE (t)) == TEMPLATE_TYPE_PARM)
1273 /* Say `class T' not just `T'. */
1274 pp_cxx_ws_string (pp, "class");
1276 /* Emit the `...' for a parameter pack. */
1277 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
1278 pp_cxx_ws_string (pp, "...");
1281 dump_type (pp, TREE_TYPE (t), flags);
1282 break;
1284 if (TYPE_DECL_ALIAS_P (t)
1285 && (flags & TFF_DECL_SPECIFIERS
1286 || flags & TFF_CLASS_KEY_OR_ENUM))
1288 pp_cxx_ws_string (pp, "using");
1289 if (! (flags & TFF_UNQUALIFIED_NAME))
1290 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
1291 dump_decl (pp, DECL_NAME (t), flags);
1292 pp_cxx_whitespace (pp);
1293 pp_cxx_ws_string (pp, "=");
1294 pp_cxx_whitespace (pp);
1295 dump_type (pp, (DECL_ORIGINAL_TYPE (t)
1296 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t)),
1297 flags);
1298 break;
1300 if ((flags & TFF_DECL_SPECIFIERS)
1301 && !DECL_SELF_REFERENCE_P (t))
1302 pp_cxx_ws_string (pp, "typedef");
1303 dump_simple_decl (pp, t, DECL_ORIGINAL_TYPE (t)
1304 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t),
1305 flags);
1306 break;
1308 case VAR_DECL:
1309 if (DECL_NAME (t) && VTABLE_NAME_P (DECL_NAME (t)))
1311 pp_string (pp, M_("vtable for "));
1312 gcc_assert (TYPE_P (DECL_CONTEXT (t)));
1313 dump_type (pp, DECL_CONTEXT (t), flags);
1314 break;
1316 /* Fall through. */
1317 case FIELD_DECL:
1318 case PARM_DECL:
1319 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1321 /* Handle variable template specializations. */
1322 if (VAR_P (t)
1323 && DECL_LANG_SPECIFIC (t)
1324 && DECL_TEMPLATE_INFO (t)
1325 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1327 pp_cxx_begin_template_argument_list (pp);
1328 tree args = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1329 dump_template_argument_list (pp, args, flags);
1330 pp_cxx_end_template_argument_list (pp);
1332 break;
1334 case RESULT_DECL:
1335 pp_string (pp, M_("<return value> "));
1336 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1337 break;
1339 case NAMESPACE_DECL:
1340 if (flags & TFF_DECL_SPECIFIERS)
1341 pp->declaration (t);
1342 else
1344 if (! (flags & TFF_UNQUALIFIED_NAME))
1345 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
1346 flags &= ~TFF_UNQUALIFIED_NAME;
1347 if (DECL_NAME (t) == NULL_TREE)
1349 if (!(pp->flags & pp_c_flag_gnu_v3))
1350 pp_cxx_ws_string (pp, M_("{anonymous}"));
1351 else
1352 pp_cxx_ws_string (pp, M_("(anonymous namespace)"));
1354 else
1355 pp_cxx_tree_identifier (pp, DECL_NAME (t));
1357 break;
1359 case SCOPE_REF:
1360 dump_type (pp, TREE_OPERAND (t, 0), flags);
1361 pp_cxx_colon_colon (pp);
1362 dump_decl (pp, TREE_OPERAND (t, 1), TFF_UNQUALIFIED_NAME);
1363 break;
1365 case ARRAY_REF:
1366 dump_decl (pp, TREE_OPERAND (t, 0), flags);
1367 pp_cxx_left_bracket (pp);
1368 dump_decl (pp, TREE_OPERAND (t, 1), flags);
1369 pp_cxx_right_bracket (pp);
1370 break;
1372 /* So that we can do dump_decl on an aggr type. */
1373 case RECORD_TYPE:
1374 case UNION_TYPE:
1375 case ENUMERAL_TYPE:
1376 dump_type (pp, t, flags);
1377 break;
1379 case BIT_NOT_EXPR:
1380 /* This is a pseudo destructor call which has not been folded into
1381 a PSEUDO_DTOR_EXPR yet. */
1382 pp_cxx_complement (pp);
1383 dump_type (pp, TREE_OPERAND (t, 0), flags);
1384 break;
1386 case TYPE_EXPR:
1387 gcc_unreachable ();
1388 break;
1390 case IDENTIFIER_NODE:
1391 dump_decl_name (pp, t, flags);
1392 break;
1394 case OVERLOAD:
1395 if (!OVL_SINGLE_P (t))
1397 tree ctx = ovl_scope (t);
1398 if (ctx != global_namespace)
1400 if (TYPE_P (ctx))
1401 dump_type (pp, ctx, flags);
1402 else
1403 dump_decl (pp, ctx, flags);
1404 pp_cxx_colon_colon (pp);
1406 dump_decl (pp, OVL_NAME (t), flags);
1407 break;
1410 /* If there's only one function, just treat it like an ordinary
1411 FUNCTION_DECL. */
1412 t = OVL_FIRST (t);
1413 /* Fall through. */
1415 case FUNCTION_DECL:
1416 if (! DECL_LANG_SPECIFIC (t))
1418 if (DECL_ABSTRACT_ORIGIN (t)
1419 && DECL_ABSTRACT_ORIGIN (t) != t)
1420 dump_decl (pp, DECL_ABSTRACT_ORIGIN (t), flags);
1421 else
1422 dump_function_name (pp, t, flags);
1424 else if (DECL_GLOBAL_CTOR_P (t) || DECL_GLOBAL_DTOR_P (t))
1425 dump_global_iord (pp, t);
1426 else
1427 dump_function_decl (pp, t, flags);
1428 break;
1430 case TEMPLATE_DECL:
1431 dump_template_decl (pp, t, flags);
1432 break;
1434 case CONCEPT_DECL:
1435 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1436 break;
1438 case WILDCARD_DECL:
1439 pp_string (pp, "<wildcard>");
1440 break;
1442 case TEMPLATE_ID_EXPR:
1444 tree name = TREE_OPERAND (t, 0);
1445 tree args = TREE_OPERAND (t, 1);
1447 if (!identifier_p (name))
1448 name = OVL_NAME (name);
1449 dump_decl (pp, name, flags);
1450 pp_cxx_begin_template_argument_list (pp);
1451 if (args == error_mark_node)
1452 pp_string (pp, M_("<template arguments error>"));
1453 else if (args)
1454 dump_template_argument_list
1455 (pp, args, flags|TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS);
1456 pp_cxx_end_template_argument_list (pp);
1458 break;
1460 case LABEL_DECL:
1461 if (DECL_NAME (t))
1462 pp_cxx_tree_identifier (pp, DECL_NAME (t));
1463 else
1464 dump_generic_node (pp, t, 0, TDF_SLIM, false);
1465 break;
1467 case CONST_DECL:
1468 if ((TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == ENUMERAL_TYPE)
1469 || (DECL_INITIAL (t) &&
1470 TREE_CODE (DECL_INITIAL (t)) == TEMPLATE_PARM_INDEX))
1471 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1472 else if (DECL_NAME (t))
1473 dump_decl (pp, DECL_NAME (t), flags);
1474 else if (DECL_INITIAL (t))
1475 dump_expr (pp, DECL_INITIAL (t), flags | TFF_EXPR_IN_PARENS);
1476 else
1477 pp_string (pp, M_("<enumerator>"));
1478 break;
1480 case USING_DECL:
1482 if (flags & TFF_DECL_SPECIFIERS)
1483 pp_cxx_ws_string (pp, "using");
1484 bool variadic = false;
1485 if (!(flags & TFF_UNQUALIFIED_NAME))
1487 tree scope = USING_DECL_SCOPE (t);
1488 tree name = DECL_NAME (t);
1489 if (PACK_EXPANSION_P (scope))
1491 scope = PACK_EXPANSION_PATTERN (scope);
1492 variadic = true;
1494 if (identifier_p (name)
1495 && IDENTIFIER_CONV_OP_P (name)
1496 && PACK_EXPANSION_P (TREE_TYPE (name)))
1498 name = make_conv_op_name (PACK_EXPANSION_PATTERN
1499 (TREE_TYPE (name)));
1500 variadic = true;
1502 dump_type (pp, scope, flags);
1503 pp_cxx_colon_colon (pp);
1505 dump_decl (pp, DECL_NAME (t), flags);
1506 if (variadic)
1507 pp_cxx_ws_string (pp, "...");
1509 break;
1511 case STATIC_ASSERT:
1512 pp->declaration (t);
1513 break;
1515 case BASELINK:
1516 dump_decl (pp, BASELINK_FUNCTIONS (t), flags);
1517 break;
1519 case TEMPLATE_TYPE_PARM:
1520 if (flags & TFF_DECL_SPECIFIERS)
1521 pp->declaration (t);
1522 else
1523 pp->type_id (t);
1524 break;
1526 case UNBOUND_CLASS_TEMPLATE:
1527 case TYPE_PACK_EXPANSION:
1528 case TREE_BINFO:
1529 dump_type (pp, t, flags);
1530 break;
1532 default:
1533 pp_unsupported_tree (pp, t);
1534 /* Fall through. */
1536 case ERROR_MARK:
1537 pp_string (pp, M_("<declaration error>"));
1538 break;
1542 /* Dump a template declaration T under control of FLAGS. This means the
1543 'template <...> leaders plus the 'class X' or 'void fn(...)' part. */
1545 static void
1546 dump_template_decl (cxx_pretty_printer *pp, tree t, int flags)
1548 tree orig_parms = DECL_TEMPLATE_PARMS (t);
1549 tree parms;
1550 int i;
1552 if (flags & TFF_TEMPLATE_HEADER)
1554 for (parms = orig_parms = nreverse (orig_parms);
1555 parms;
1556 parms = TREE_CHAIN (parms))
1558 tree inner_parms = INNERMOST_TEMPLATE_PARMS (parms);
1559 int len = TREE_VEC_LENGTH (inner_parms);
1561 if (len == 0)
1563 /* Skip over the dummy template levels of a template template
1564 parm. */
1565 gcc_assert (TREE_CODE (TREE_TYPE (t)) == TEMPLATE_TEMPLATE_PARM);
1566 continue;
1569 pp_cxx_ws_string (pp, "template");
1570 pp_cxx_begin_template_argument_list (pp);
1572 /* If we've shown the template prefix, we'd better show the
1573 parameters' and decl's type too. */
1574 flags |= TFF_DECL_SPECIFIERS;
1576 for (i = 0; i < len; i++)
1578 if (i)
1579 pp_separate_with_comma (pp);
1580 dump_template_parameter (pp, TREE_VEC_ELT (inner_parms, i),
1581 flags);
1583 pp_cxx_end_template_argument_list (pp);
1584 pp_cxx_whitespace (pp);
1586 nreverse(orig_parms);
1588 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
1590 /* Say `template<arg> class TT' not just `template<arg> TT'. */
1591 pp_cxx_ws_string (pp, "class");
1593 /* If this is a parameter pack, print the ellipsis. */
1594 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
1595 pp_cxx_ws_string (pp, "...");
1598 /* Only print the requirements if we're also printing
1599 the template header. */
1600 if (flag_concepts)
1601 if (tree ci = get_constraints (t))
1602 if (check_constraint_info (ci))
1603 if (tree reqs = CI_TEMPLATE_REQS (ci))
1605 pp_cxx_requires_clause (pp, reqs);
1606 pp_cxx_whitespace (pp);
1611 if (DECL_CLASS_TEMPLATE_P (t))
1612 dump_type (pp, TREE_TYPE (t),
1613 ((flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1614 | (flags & TFF_DECL_SPECIFIERS ? TFF_CLASS_KEY_OR_ENUM : 0)));
1615 else if (DECL_TEMPLATE_RESULT (t)
1616 && (VAR_P (DECL_TEMPLATE_RESULT (t))
1617 /* Alias template. */
1618 || DECL_TYPE_TEMPLATE_P (t)
1619 /* Concept definition. &*/
1620 || TREE_CODE (DECL_TEMPLATE_RESULT (t)) == CONCEPT_DECL))
1621 dump_decl (pp, DECL_TEMPLATE_RESULT (t), flags | TFF_TEMPLATE_NAME);
1622 else
1624 gcc_assert (TREE_TYPE (t));
1625 switch (NEXT_CODE (t))
1627 case METHOD_TYPE:
1628 case FUNCTION_TYPE:
1629 dump_function_decl (pp, t, flags | TFF_TEMPLATE_NAME);
1630 break;
1631 default:
1632 /* This case can occur with some invalid code. */
1633 dump_type (pp, TREE_TYPE (t),
1634 (flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1635 | (flags & TFF_DECL_SPECIFIERS
1636 ? TFF_CLASS_KEY_OR_ENUM : 0));
1641 /* find_typenames looks through the type of the function template T
1642 and returns a vec containing any typedefs, decltypes or TYPENAME_TYPEs
1643 it finds. */
1645 struct find_typenames_t
1647 hash_set<tree> *p_set;
1648 vec<tree, va_gc> *typenames;
1651 static tree
1652 find_typenames_r (tree *tp, int *walk_subtrees, void *data)
1654 struct find_typenames_t *d = (struct find_typenames_t *)data;
1655 tree mv = NULL_TREE;
1657 if (TYPE_P (*tp) && is_typedef_decl (TYPE_NAME (*tp)))
1658 /* Add the type of the typedef without any additional cv-quals. */
1659 mv = TREE_TYPE (TYPE_NAME (*tp));
1660 else if (TREE_CODE (*tp) == TYPENAME_TYPE
1661 || TREE_CODE (*tp) == DECLTYPE_TYPE)
1662 /* Add the typename without any cv-qualifiers. */
1663 mv = TYPE_MAIN_VARIANT (*tp);
1665 if (PACK_EXPANSION_P (*tp))
1667 /* Don't mess with parameter packs since we don't remember
1668 the pack expansion context for a particular typename. */
1669 *walk_subtrees = false;
1670 return NULL_TREE;
1673 if (mv && (mv == *tp || !d->p_set->add (mv)))
1674 vec_safe_push (d->typenames, mv);
1676 return NULL_TREE;
1679 static vec<tree, va_gc> *
1680 find_typenames (tree t)
1682 struct find_typenames_t ft;
1683 ft.p_set = new hash_set<tree>;
1684 ft.typenames = NULL;
1685 cp_walk_tree (&TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
1686 find_typenames_r, &ft, ft.p_set);
1687 delete ft.p_set;
1688 return ft.typenames;
1691 /* Output the "[with ...]" clause for a template instantiation T iff
1692 TEMPLATE_PARMS, TEMPLATE_ARGS and FLAGS are suitable. T may be NULL if
1693 formatting a deduction/substitution diagnostic rather than an
1694 instantiation. */
1696 static void
1697 dump_substitution (cxx_pretty_printer *pp,
1698 tree t, tree template_parms, tree template_args,
1699 int flags)
1701 if (template_parms != NULL_TREE && template_args != NULL_TREE
1702 && !(flags & TFF_NO_TEMPLATE_BINDINGS))
1704 vec<tree, va_gc> *typenames = t ? find_typenames (t) : NULL;
1705 dump_template_bindings (pp, template_parms, template_args, typenames);
1709 /* Dump the lambda function FN including its 'mutable' qualifier and any
1710 template bindings. */
1712 static void
1713 dump_lambda_function (cxx_pretty_printer *pp,
1714 tree fn, tree template_parms, tree template_args,
1715 int flags)
1717 /* A lambda's signature is essentially its "type". */
1718 dump_type (pp, DECL_CONTEXT (fn), flags);
1719 if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
1720 /* Early escape. */;
1721 else if (TREE_CODE (TREE_TYPE (fn)) == FUNCTION_TYPE)
1723 pp->padding = pp_before;
1724 pp_c_ws_string (pp, "static");
1726 else if (!(TYPE_QUALS (class_of_this_parm (TREE_TYPE (fn)))
1727 & TYPE_QUAL_CONST))
1729 pp->padding = pp_before;
1730 pp_c_ws_string (pp, "mutable");
1732 dump_substitution (pp, fn, template_parms, template_args, flags);
1735 /* Pretty print a function decl. There are several ways we want to print a
1736 function declaration. The TFF_ bits in FLAGS tells us how to behave.
1737 As error can only apply the '#' flag once to give 0 and 1 for V, there
1738 is %D which doesn't print the throw specs, and %F which does. */
1740 static void
1741 dump_function_decl (cxx_pretty_printer *pp, tree t, int flags)
1743 tree fntype;
1744 tree parmtypes;
1745 tree cname = NULL_TREE;
1746 tree template_args = NULL_TREE;
1747 tree template_parms = NULL_TREE;
1748 int show_return = flags & TFF_RETURN_TYPE || flags & TFF_DECL_SPECIFIERS;
1749 int do_outer_scope = ! (flags & TFF_UNQUALIFIED_NAME);
1750 tree exceptions;
1751 bool constexpr_p;
1752 tree ret = NULL_TREE;
1754 int dump_function_name_flags = flags & ~TFF_UNQUALIFIED_NAME;
1755 flags = dump_function_name_flags & ~TFF_TEMPLATE_NAME;
1756 if (TREE_CODE (t) == TEMPLATE_DECL)
1757 t = DECL_TEMPLATE_RESULT (t);
1759 /* Save the exceptions, in case t is a specialization and we are
1760 emitting an error about incompatible specifications. */
1761 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (t));
1763 /* Likewise for the constexpr specifier, in case t is a specialization. */
1764 constexpr_p = (DECL_DECLARED_CONSTEXPR_P (t)
1765 && !decl_implicit_constexpr_p (t));
1767 /* Pretty print template instantiations only. */
1768 if (DECL_USE_TEMPLATE (t) && DECL_TEMPLATE_INFO (t)
1769 && !(flags & TFF_NO_TEMPLATE_BINDINGS)
1770 && flag_pretty_templates)
1772 tree tmpl;
1774 template_args = DECL_TI_ARGS (t);
1775 tmpl = most_general_template (t);
1776 if (tmpl && TREE_CODE (tmpl) == TEMPLATE_DECL)
1778 template_parms = DECL_TEMPLATE_PARMS (tmpl);
1779 t = tmpl;
1783 if (DECL_NAME (t) && LAMBDA_FUNCTION_P (t))
1784 return dump_lambda_function (pp, t, template_parms, template_args, flags);
1786 fntype = TREE_TYPE (t);
1787 parmtypes = FUNCTION_FIRST_USER_PARMTYPE (t);
1789 if (DECL_CLASS_SCOPE_P (t))
1790 cname = DECL_CONTEXT (t);
1791 /* This is for partially instantiated template methods. */
1792 else if (TREE_CODE (fntype) == METHOD_TYPE)
1793 cname = TREE_TYPE (TREE_VALUE (parmtypes));
1795 if (flags & TFF_DECL_SPECIFIERS)
1797 if (DECL_STATIC_FUNCTION_P (t))
1798 pp_cxx_ws_string (pp, "static");
1799 else if (DECL_VIRTUAL_P (t))
1800 pp_cxx_ws_string (pp, "virtual");
1802 if (constexpr_p)
1804 if (DECL_DECLARED_CONCEPT_P (t))
1805 pp_cxx_ws_string (pp, "concept");
1806 else if (DECL_IMMEDIATE_FUNCTION_P (t))
1807 pp_cxx_ws_string (pp, "consteval");
1808 else
1809 pp_cxx_ws_string (pp, "constexpr");
1813 /* Print the return type? */
1814 if (show_return)
1815 show_return = (!DECL_CONV_FN_P (t) && !DECL_CONSTRUCTOR_P (t)
1816 && !DECL_DESTRUCTOR_P (t) && !deduction_guide_p (t));
1817 if (show_return)
1819 ret = fndecl_declared_return_type (t);
1820 dump_type_prefix (pp, ret, flags);
1823 /* Print the function name. */
1824 if (!do_outer_scope)
1825 /* Nothing. */;
1826 else if (cname)
1828 dump_type (pp, cname, flags);
1829 pp_cxx_colon_colon (pp);
1831 else
1832 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
1834 /* Name lookup for the rest of the function declarator is implicitly in the
1835 scope of the function, so avoid printing redundant scope qualifiers. */
1836 auto cds = make_temp_override (current_dump_scope, CP_DECL_CONTEXT (t));
1838 dump_function_name (pp, t, dump_function_name_flags);
1840 if (!(flags & TFF_NO_FUNCTION_ARGUMENTS))
1842 int const parm_flags
1843 = DECL_XOBJ_MEMBER_FUNCTION_P (t) ? TFF_XOBJ_FUNC | flags : flags;
1844 dump_parameters (pp, parmtypes, parm_flags);
1846 if (TREE_CODE (fntype) == METHOD_TYPE)
1848 pp->padding = pp_before;
1849 pp_cxx_cv_qualifier_seq (pp, class_of_this_parm (fntype));
1850 dump_ref_qualifier (pp, fntype, flags);
1853 if (tx_safe_fn_type_p (fntype))
1855 pp->padding = pp_before;
1856 pp_cxx_ws_string (pp, "transaction_safe");
1859 if (flags & TFF_EXCEPTION_SPECIFICATION)
1861 pp->padding = pp_before;
1862 dump_exception_spec (pp, exceptions, flags);
1865 if (show_return)
1866 dump_type_suffix (pp, ret, flags);
1867 else if (deduction_guide_p (t))
1869 pp_cxx_ws_string (pp, "->");
1870 dump_type (pp, TREE_TYPE (TREE_TYPE (t)), flags);
1873 if (flag_concepts)
1874 if (tree ci = get_constraints (t))
1875 if (tree reqs = CI_DECLARATOR_REQS (ci))
1876 pp_cxx_requires_clause (pp, reqs);
1878 dump_substitution (pp, t, template_parms, template_args, flags);
1880 if (tree base = DECL_INHERITED_CTOR_BASE (t))
1882 pp_cxx_ws_string (pp, "[inherited from");
1883 dump_type (pp, base, TFF_PLAIN_IDENTIFIER);
1884 pp_character (pp, ']');
1887 else if (template_args)
1889 bool need_comma = false;
1890 int i;
1891 pp_cxx_begin_template_argument_list (pp);
1892 template_args = INNERMOST_TEMPLATE_ARGS (template_args);
1893 for (i = 0; i < TREE_VEC_LENGTH (template_args); ++i)
1895 tree arg = TREE_VEC_ELT (template_args, i);
1896 if (need_comma)
1897 pp_separate_with_comma (pp);
1898 if (ARGUMENT_PACK_P (arg))
1899 pp_cxx_left_brace (pp);
1900 dump_template_argument (pp, arg, TFF_PLAIN_IDENTIFIER);
1901 if (ARGUMENT_PACK_P (arg))
1902 pp_cxx_right_brace (pp);
1903 need_comma = true;
1905 pp_cxx_end_template_argument_list (pp);
1909 /* Print a parameter list. If this is for a member function, the
1910 member object ptr (and any other hidden args) should have
1911 already been removed. */
1913 static void
1914 dump_parameters (cxx_pretty_printer *pp, tree parmtypes, int flags)
1916 int first = 1;
1917 flags &= ~TFF_SCOPE;
1918 pp_cxx_left_paren (pp);
1920 for (first = 1; parmtypes != void_list_node;
1921 parmtypes = TREE_CHAIN (parmtypes))
1923 if (first && flags & TFF_XOBJ_FUNC)
1924 pp_string (pp, "this ");
1925 if (!first)
1926 pp_separate_with_comma (pp);
1927 first = 0;
1928 if (!parmtypes)
1930 pp_cxx_ws_string (pp, "...");
1931 break;
1934 dump_type (pp, TREE_VALUE (parmtypes), flags);
1936 if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && TREE_PURPOSE (parmtypes))
1938 pp_cxx_whitespace (pp);
1939 pp_equal (pp);
1940 pp_cxx_whitespace (pp);
1941 dump_expr (pp, TREE_PURPOSE (parmtypes), flags | TFF_EXPR_IN_PARENS);
1945 pp_cxx_right_paren (pp);
1948 /* Print ref-qualifier of a FUNCTION_TYPE or METHOD_TYPE. FLAGS are ignored. */
1950 static void
1951 dump_ref_qualifier (cxx_pretty_printer *pp, tree t, int flags ATTRIBUTE_UNUSED)
1953 if (FUNCTION_REF_QUALIFIED (t))
1955 pp->padding = pp_before;
1956 if (FUNCTION_RVALUE_QUALIFIED (t))
1957 pp_cxx_ws_string (pp, "&&");
1958 else
1959 pp_cxx_ws_string (pp, "&");
1963 /* Print an exception specification. T is the exception specification. */
1965 static void
1966 dump_exception_spec (cxx_pretty_printer *pp, tree t, int flags)
1968 if (t && TREE_PURPOSE (t))
1970 pp_cxx_ws_string (pp, "noexcept");
1971 if (!integer_onep (TREE_PURPOSE (t)))
1973 pp_cxx_whitespace (pp);
1974 pp_cxx_left_paren (pp);
1975 if (DEFERRED_NOEXCEPT_SPEC_P (t))
1976 pp_cxx_ws_string (pp, "<uninstantiated>");
1977 else
1978 dump_expr (pp, TREE_PURPOSE (t), flags);
1979 pp_cxx_right_paren (pp);
1982 else if (t)
1984 pp_cxx_ws_string (pp, "throw");
1985 pp_cxx_whitespace (pp);
1986 pp_cxx_left_paren (pp);
1987 if (TREE_VALUE (t) != NULL_TREE)
1988 while (1)
1990 dump_type (pp, TREE_VALUE (t), flags);
1991 t = TREE_CHAIN (t);
1992 if (!t)
1993 break;
1994 pp_separate_with_comma (pp);
1996 pp_cxx_right_paren (pp);
2000 /* Handle the function name for a FUNCTION_DECL node, grokking operators
2001 and destructors properly. */
2003 static void
2004 dump_function_name (cxx_pretty_printer *pp, tree t, int flags)
2006 /* Only colorize when we're printing something before the name; in
2007 particular, not when printing a CALL_EXPR. */
2008 bool colorize = flags & (TFF_DECL_SPECIFIERS | TFF_RETURN_TYPE
2009 | TFF_TEMPLATE_HEADER);
2011 colorize_guard g (colorize, pp, "fnname");
2013 tree name = DECL_NAME (t);
2015 /* We can get here with a decl that was synthesized by language-
2016 independent machinery (e.g. coverage.cc) in which case it won't
2017 have a lang_specific structure attached and DECL_CONSTRUCTOR_P
2018 will crash. In this case it is safe just to print out the
2019 literal name. */
2020 if (!DECL_LANG_SPECIFIC (t))
2022 pp_cxx_tree_identifier (pp, name);
2023 return;
2026 if (TREE_CODE (t) == TEMPLATE_DECL)
2027 t = DECL_TEMPLATE_RESULT (t);
2029 /* Don't let the user see __comp_ctor et al. */
2030 if (DECL_CONSTRUCTOR_P (t)
2031 || DECL_DESTRUCTOR_P (t))
2033 if (LAMBDA_TYPE_P (DECL_CONTEXT (t)))
2034 name = get_identifier ("<lambda>");
2035 else if (TYPE_UNNAMED_P (DECL_CONTEXT (t)))
2036 name = get_identifier ("<constructor>");
2037 else
2038 name = constructor_name (DECL_CONTEXT (t));
2041 if (DECL_DESTRUCTOR_P (t))
2043 pp_cxx_complement (pp);
2044 dump_decl (pp, name, TFF_PLAIN_IDENTIFIER);
2046 else if (DECL_CONV_FN_P (t))
2048 /* This cannot use the hack that the operator's return
2049 type is stashed off of its name because it may be
2050 used for error reporting. In the case of conflicting
2051 declarations, both will have the same name, yet
2052 the types will be different, hence the TREE_TYPE field
2053 of the first name will be clobbered by the second. */
2054 pp_cxx_ws_string (pp, "operator");
2055 dump_type (pp, TREE_TYPE (TREE_TYPE (t)), flags);
2057 else
2058 dump_decl (pp, name, flags);
2060 dump_module_suffix (pp, t);
2062 if (DECL_TEMPLATE_INFO (t)
2063 && !(flags & TFF_TEMPLATE_NAME)
2064 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (t)
2065 && (TREE_CODE (DECL_TI_TEMPLATE (t)) != TEMPLATE_DECL
2066 || PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
2067 dump_template_parms (pp, DECL_TEMPLATE_INFO (t), !DECL_USE_TEMPLATE (t),
2068 flags);
2071 /* Dump the template parameters from the template info INFO under control of
2072 FLAGS. PRIMARY indicates whether this is a primary template decl, or
2073 specialization (partial or complete). For partial specializations we show
2074 the specialized parameter values. For a primary template we show no
2075 decoration. */
2077 static void
2078 dump_template_parms (cxx_pretty_printer *pp, tree info,
2079 int primary, int flags)
2081 tree args = info ? TI_ARGS (info) : NULL_TREE;
2083 if (primary && flags & TFF_TEMPLATE_NAME)
2084 return;
2085 flags &= ~(TFF_CLASS_KEY_OR_ENUM | TFF_TEMPLATE_NAME);
2086 pp_cxx_begin_template_argument_list (pp);
2088 /* Be careful only to print things when we have them, so as not
2089 to crash producing error messages. */
2090 if (args && !primary)
2092 int len, ix;
2093 len = get_non_default_template_args_count (args, flags);
2095 args = INNERMOST_TEMPLATE_ARGS (args);
2096 for (ix = 0; ix != len; ix++)
2098 tree arg = TREE_VEC_ELT (args, ix);
2100 /* Only print a comma if we know there is an argument coming. In
2101 the case of an empty template argument pack, no actual
2102 argument will be printed. */
2103 if (ix
2104 && (!ARGUMENT_PACK_P (arg)
2105 || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) > 0))
2106 pp_separate_with_comma (pp);
2108 if (!arg)
2109 pp_string (pp, M_("<template parameter error>"));
2110 else
2111 dump_template_argument (pp, arg, flags);
2114 else if (primary)
2116 tree tpl = TI_TEMPLATE (info);
2117 tree parms = DECL_TEMPLATE_PARMS (tpl);
2118 int len, ix;
2120 parms = TREE_CODE (parms) == TREE_LIST ? TREE_VALUE (parms) : NULL_TREE;
2121 len = parms ? TREE_VEC_LENGTH (parms) : 0;
2123 for (ix = 0; ix != len; ix++)
2125 tree parm;
2127 if (TREE_VEC_ELT (parms, ix) == error_mark_node)
2129 pp_string (pp, M_("<template parameter error>"));
2130 continue;
2133 parm = TREE_VALUE (TREE_VEC_ELT (parms, ix));
2135 if (ix)
2136 pp_separate_with_comma (pp);
2138 dump_decl (pp, parm, flags & ~TFF_DECL_SPECIFIERS);
2141 pp_cxx_end_template_argument_list (pp);
2144 /* Print out the arguments of CALL_EXPR T as a parenthesized list using
2145 flags FLAGS. Skip over the first argument if SKIPFIRST is true. */
2147 static void
2148 dump_call_expr_args (cxx_pretty_printer *pp, tree t, int flags, bool skipfirst)
2150 tree arg;
2151 call_expr_arg_iterator iter;
2153 pp_cxx_left_paren (pp);
2154 FOR_EACH_CALL_EXPR_ARG (arg, iter, t)
2156 if (skipfirst)
2157 skipfirst = false;
2158 else
2160 dump_expr (pp, arg, flags | TFF_EXPR_IN_PARENS);
2161 if (more_call_expr_args_p (&iter))
2162 pp_separate_with_comma (pp);
2165 pp_cxx_right_paren (pp);
2168 /* Print out the arguments of AGGR_INIT_EXPR T as a parenthesized list
2169 using flags FLAGS. Skip over the first argument if SKIPFIRST is
2170 true. */
2172 static void
2173 dump_aggr_init_expr_args (cxx_pretty_printer *pp, tree t, int flags,
2174 bool skipfirst)
2176 tree arg;
2177 aggr_init_expr_arg_iterator iter;
2179 pp_cxx_left_paren (pp);
2180 FOR_EACH_AGGR_INIT_EXPR_ARG (arg, iter, t)
2182 if (skipfirst)
2183 skipfirst = false;
2184 else
2186 dump_expr (pp, arg, flags | TFF_EXPR_IN_PARENS);
2187 if (more_aggr_init_expr_args_p (&iter))
2188 pp_separate_with_comma (pp);
2191 pp_cxx_right_paren (pp);
2194 /* Print out a list of initializers (subr of dump_expr). */
2196 static void
2197 dump_expr_list (cxx_pretty_printer *pp, tree l, int flags)
2199 while (l)
2201 dump_expr (pp, TREE_VALUE (l), flags | TFF_EXPR_IN_PARENS);
2202 l = TREE_CHAIN (l);
2203 if (l)
2204 pp_separate_with_comma (pp);
2208 /* Print out a vector of initializers (subr of dump_expr). */
2210 static void
2211 dump_expr_init_vec (cxx_pretty_printer *pp, vec<constructor_elt, va_gc> *v,
2212 int flags)
2214 unsigned HOST_WIDE_INT idx;
2215 tree value;
2217 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
2219 dump_expr (pp, value, flags | TFF_EXPR_IN_PARENS);
2220 if (idx != v->length () - 1)
2221 pp_separate_with_comma (pp);
2226 /* We've gotten an indirect REFERENCE (an OBJ_TYPE_REF) to a virtual
2227 function. Resolve it to a close relative -- in the sense of static
2228 type -- variant being overridden. That is close to what was written in
2229 the source code. Subroutine of dump_expr. */
2231 static tree
2232 resolve_virtual_fun_from_obj_type_ref (tree ref)
2234 tree obj_type = TREE_TYPE (OBJ_TYPE_REF_TOKEN (ref));
2235 HOST_WIDE_INT index = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (ref));
2236 tree fun = BINFO_VIRTUALS (TYPE_BINFO (TREE_TYPE (obj_type)));
2237 while (index)
2239 fun = TREE_CHAIN (fun);
2240 index -= (TARGET_VTABLE_USES_DESCRIPTORS
2241 ? TARGET_VTABLE_USES_DESCRIPTORS : 1);
2244 return BV_FN (fun);
2247 /* Print out an expression E under control of FLAGS. */
2249 static void
2250 dump_expr (cxx_pretty_printer *pp, tree t, int flags)
2252 tree op;
2254 if (t == 0)
2255 return;
2257 if (STATEMENT_CLASS_P (t))
2259 pp_cxx_ws_string (pp, M_("<statement>"));
2260 return;
2263 switch (TREE_CODE (t))
2265 case VAR_DECL:
2266 case PARM_DECL:
2267 case FIELD_DECL:
2268 case CONST_DECL:
2269 case FUNCTION_DECL:
2270 case TEMPLATE_DECL:
2271 case NAMESPACE_DECL:
2272 case LABEL_DECL:
2273 case WILDCARD_DECL:
2274 case OVERLOAD:
2275 case TYPE_DECL:
2276 case USING_DECL:
2277 case IDENTIFIER_NODE:
2278 dump_decl (pp, t, ((flags & ~(TFF_DECL_SPECIFIERS|TFF_RETURN_TYPE
2279 |TFF_TEMPLATE_HEADER))
2280 | TFF_NO_TEMPLATE_BINDINGS
2281 | TFF_NO_FUNCTION_ARGUMENTS));
2282 break;
2284 case SSA_NAME:
2285 if (SSA_NAME_VAR (t)
2286 && !DECL_ARTIFICIAL (SSA_NAME_VAR (t)))
2287 dump_expr (pp, SSA_NAME_VAR (t), flags);
2288 else
2289 pp_cxx_ws_string (pp, M_("<unknown>"));
2290 break;
2292 case VOID_CST:
2293 case INTEGER_CST:
2294 case REAL_CST:
2295 case STRING_CST:
2296 case COMPLEX_CST:
2297 pp->constant (t);
2298 break;
2300 case USERDEF_LITERAL:
2301 pp_cxx_userdef_literal (pp, t);
2302 break;
2304 case THROW_EXPR:
2305 /* While waiting for caret diagnostics, avoid printing
2306 __cxa_allocate_exception, __cxa_throw, and the like. */
2307 pp_cxx_ws_string (pp, M_("<throw-expression>"));
2308 break;
2310 case PTRMEM_CST:
2311 pp_ampersand (pp);
2312 dump_type (pp, PTRMEM_CST_CLASS (t), flags);
2313 pp_cxx_colon_colon (pp);
2314 pp_cxx_tree_identifier (pp, DECL_NAME (PTRMEM_CST_MEMBER (t)));
2315 break;
2317 case COMPOUND_EXPR:
2318 pp_cxx_left_paren (pp);
2319 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2320 pp_separate_with_comma (pp);
2321 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2322 pp_cxx_right_paren (pp);
2323 break;
2325 case COND_EXPR:
2326 case VEC_COND_EXPR:
2327 pp_cxx_left_paren (pp);
2328 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2329 pp_string (pp, " ? ");
2330 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2331 pp_string (pp, " : ");
2332 dump_expr (pp, TREE_OPERAND (t, 2), flags | TFF_EXPR_IN_PARENS);
2333 pp_cxx_right_paren (pp);
2334 break;
2336 case SAVE_EXPR:
2337 if (TREE_HAS_CONSTRUCTOR (t))
2339 pp_cxx_ws_string (pp, "new");
2340 pp_cxx_whitespace (pp);
2341 dump_type (pp, TREE_TYPE (TREE_TYPE (t)), flags);
2343 else
2344 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2345 break;
2347 case AGGR_INIT_EXPR:
2349 tree fn = NULL_TREE;
2351 if (TREE_CODE (AGGR_INIT_EXPR_FN (t)) == ADDR_EXPR)
2352 fn = TREE_OPERAND (AGGR_INIT_EXPR_FN (t), 0);
2354 if (fn && TREE_CODE (fn) == FUNCTION_DECL)
2356 if (DECL_CONSTRUCTOR_P (fn))
2357 dump_type (pp, DECL_CONTEXT (fn), flags);
2358 else
2359 dump_decl (pp, fn, 0);
2361 else
2362 dump_expr (pp, AGGR_INIT_EXPR_FN (t), 0);
2364 dump_aggr_init_expr_args (pp, t, flags, true);
2365 break;
2367 case CALL_EXPR:
2369 tree fn = CALL_EXPR_FN (t);
2370 bool skipfirst = false;
2372 /* Deal with internal functions. */
2373 if (fn == NULL_TREE)
2375 pp_string (pp, internal_fn_name (CALL_EXPR_IFN (t)));
2376 dump_call_expr_args (pp, t, flags, skipfirst);
2377 break;
2380 if (TREE_CODE (fn) == ADDR_EXPR)
2381 fn = TREE_OPERAND (fn, 0);
2383 /* Nobody is interested in seeing the guts of vcalls. */
2384 if (TREE_CODE (fn) == OBJ_TYPE_REF)
2385 fn = resolve_virtual_fun_from_obj_type_ref (fn);
2387 if (TREE_TYPE (fn) != NULL_TREE
2388 && NEXT_CODE (fn) == METHOD_TYPE
2389 && call_expr_nargs (t))
2391 tree ob = CALL_EXPR_ARG (t, 0);
2392 if (TREE_CODE (ob) == ADDR_EXPR)
2394 dump_expr (pp, TREE_OPERAND (ob, 0),
2395 flags | TFF_EXPR_IN_PARENS);
2396 pp_cxx_dot (pp);
2398 else if (!is_this_parameter (ob))
2400 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2401 pp_cxx_arrow (pp);
2403 skipfirst = true;
2405 if (flag_sanitize & SANITIZE_UNDEFINED
2406 && is_ubsan_builtin_p (fn))
2408 pp_string (cxx_pp, M_("<ubsan routine call>"));
2409 break;
2411 dump_expr (pp, fn, flags | TFF_EXPR_IN_PARENS);
2412 dump_call_expr_args (pp, t, flags, skipfirst);
2414 break;
2416 case TARGET_EXPR:
2417 /* Note that this only works for G++ target exprs. If somebody
2418 builds a general TARGET_EXPR, there's no way to represent that
2419 it initializes anything other that the parameter slot for the
2420 default argument. Note we may have cleared out the first
2421 operand in expand_expr, so don't go killing ourselves. */
2422 if (TREE_OPERAND (t, 1))
2423 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2424 break;
2426 case POINTER_PLUS_EXPR:
2427 dump_binary_op (pp, "+", t, flags);
2428 break;
2430 case POINTER_DIFF_EXPR:
2431 dump_binary_op (pp, "-", t, flags);
2432 break;
2434 case INIT_EXPR:
2435 case MODIFY_EXPR:
2436 dump_binary_op (pp, OVL_OP_INFO (true, NOP_EXPR)->name, t, flags);
2437 break;
2439 case PLUS_EXPR:
2440 case MINUS_EXPR:
2441 case MULT_EXPR:
2442 case TRUNC_DIV_EXPR:
2443 case TRUNC_MOD_EXPR:
2444 case MIN_EXPR:
2445 case MAX_EXPR:
2446 case LSHIFT_EXPR:
2447 case RSHIFT_EXPR:
2448 case BIT_IOR_EXPR:
2449 case BIT_XOR_EXPR:
2450 case BIT_AND_EXPR:
2451 case TRUTH_ANDIF_EXPR:
2452 case TRUTH_ORIF_EXPR:
2453 case LT_EXPR:
2454 case LE_EXPR:
2455 case GT_EXPR:
2456 case GE_EXPR:
2457 case EQ_EXPR:
2458 case NE_EXPR:
2459 case SPACESHIP_EXPR:
2460 case EXACT_DIV_EXPR:
2461 dump_binary_op (pp, OVL_OP_INFO (false, TREE_CODE (t))->name, t, flags);
2462 break;
2464 case CEIL_DIV_EXPR:
2465 case FLOOR_DIV_EXPR:
2466 case ROUND_DIV_EXPR:
2467 case RDIV_EXPR:
2468 dump_binary_op (pp, "/", t, flags);
2469 break;
2471 case CEIL_MOD_EXPR:
2472 case FLOOR_MOD_EXPR:
2473 case ROUND_MOD_EXPR:
2474 dump_binary_op (pp, "%", t, flags);
2475 break;
2477 case COMPONENT_REF:
2479 tree ob = TREE_OPERAND (t, 0);
2480 if (INDIRECT_REF_P (ob))
2482 ob = TREE_OPERAND (ob, 0);
2483 if (!is_this_parameter (ob)
2484 && !is_dummy_object (ob))
2486 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2487 if (TYPE_REF_P (TREE_TYPE (ob)))
2488 pp_cxx_dot (pp);
2489 else
2490 pp_cxx_arrow (pp);
2493 else
2495 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2496 if (TREE_CODE (ob) != ARROW_EXPR)
2497 pp_cxx_dot (pp);
2499 dump_expr (pp, TREE_OPERAND (t, 1), flags & ~TFF_EXPR_IN_PARENS);
2501 break;
2503 case ARRAY_REF:
2504 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2505 pp_cxx_left_bracket (pp);
2506 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2507 pp_cxx_right_bracket (pp);
2508 break;
2510 case OMP_ARRAY_SECTION:
2511 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2512 pp_cxx_left_bracket (pp);
2513 dump_expr (pp, TREE_OPERAND (t, 1), flags);
2514 pp_colon (pp);
2515 dump_expr (pp, TREE_OPERAND (t, 2), flags);
2516 pp_cxx_right_bracket (pp);
2517 break;
2519 case UNARY_PLUS_EXPR:
2520 dump_unary_op (pp, "+", t, flags);
2521 break;
2523 case ADDR_EXPR:
2524 if (TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
2525 || TREE_CODE (TREE_OPERAND (t, 0)) == STRING_CST
2526 /* An ADDR_EXPR can have reference type. In that case, we
2527 shouldn't print the `&' doing so indicates to the user
2528 that the expression has pointer type. */
2529 || (TREE_TYPE (t)
2530 && TYPE_REF_P (TREE_TYPE (t))))
2531 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2532 else if (TREE_CODE (TREE_OPERAND (t, 0)) == LABEL_DECL)
2533 dump_unary_op (pp, "&&", t, flags);
2534 else
2535 dump_unary_op (pp, "&", t, flags);
2536 break;
2538 case INDIRECT_REF:
2539 if (TREE_HAS_CONSTRUCTOR (t))
2541 t = TREE_OPERAND (t, 0);
2542 gcc_assert (TREE_CODE (t) == CALL_EXPR);
2543 dump_expr (pp, CALL_EXPR_FN (t), flags | TFF_EXPR_IN_PARENS);
2544 dump_call_expr_args (pp, t, flags, true);
2546 else
2548 if (TREE_OPERAND (t,0) != NULL_TREE
2549 && TREE_TYPE (TREE_OPERAND (t, 0))
2550 && NEXT_CODE (TREE_OPERAND (t, 0)) == REFERENCE_TYPE)
2551 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2552 else
2553 dump_unary_op (pp, "*", t, flags);
2555 break;
2557 case MEM_REF:
2558 /* Delegate to the base "C" pretty printer. */
2559 pp->c_pretty_printer::unary_expression (t);
2560 break;
2562 case TARGET_MEM_REF:
2563 /* TARGET_MEM_REF can't appear directly from source, but can appear
2564 during late GIMPLE optimizations and through late diagnostic we might
2565 need to support it. Print it as dereferencing of a pointer after
2566 cast to the TARGET_MEM_REF type, with pointer arithmetics on some
2567 pointer to single byte types, so
2568 *(type *)((char *) ptr + step * index + index2) if all the operands
2569 are present and the casts are needed. */
2570 pp_cxx_star (pp);
2571 pp_cxx_left_paren (pp);
2572 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (TMR_BASE (t)))) == NULL_TREE
2573 || !integer_onep (TYPE_SIZE_UNIT
2574 (TREE_TYPE (TREE_TYPE (TMR_BASE (t))))))
2576 if (TYPE_SIZE_UNIT (TREE_TYPE (t))
2577 && integer_onep (TYPE_SIZE_UNIT (TREE_TYPE (t))))
2579 pp_cxx_left_paren (pp);
2580 dump_type (pp, build_pointer_type (TREE_TYPE (t)), flags);
2582 else
2584 dump_type (pp, build_pointer_type (TREE_TYPE (t)), flags);
2585 pp_cxx_right_paren (pp);
2586 pp_cxx_left_paren (pp);
2587 pp_cxx_left_paren (pp);
2588 dump_type (pp, build_pointer_type (char_type_node), flags);
2590 pp_cxx_right_paren (pp);
2592 else if (!same_type_p (TREE_TYPE (t),
2593 TREE_TYPE (TREE_TYPE (TMR_BASE (t)))))
2595 dump_type (pp, build_pointer_type (TREE_TYPE (t)), flags);
2596 pp_cxx_right_paren (pp);
2597 pp_cxx_left_paren (pp);
2599 dump_expr (pp, TMR_BASE (t), flags);
2600 if (TMR_STEP (t) && TMR_INDEX (t))
2602 pp_cxx_ws_string (pp, "+");
2603 dump_expr (pp, TMR_INDEX (t), flags);
2604 pp_cxx_ws_string (pp, "*");
2605 dump_expr (pp, TMR_STEP (t), flags);
2607 if (TMR_INDEX2 (t))
2609 pp_cxx_ws_string (pp, "+");
2610 dump_expr (pp, TMR_INDEX2 (t), flags);
2612 if (!integer_zerop (TMR_OFFSET (t)))
2614 pp_cxx_ws_string (pp, "+");
2615 dump_expr (pp, fold_convert (ssizetype, TMR_OFFSET (t)), flags);
2617 pp_cxx_right_paren (pp);
2618 break;
2620 case NEGATE_EXPR:
2621 case BIT_NOT_EXPR:
2622 case TRUTH_NOT_EXPR:
2623 case PREDECREMENT_EXPR:
2624 case PREINCREMENT_EXPR:
2625 dump_unary_op (pp, OVL_OP_INFO (false, TREE_CODE (t))->name, t, flags);
2626 break;
2628 case POSTDECREMENT_EXPR:
2629 case POSTINCREMENT_EXPR:
2630 pp_cxx_left_paren (pp);
2631 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2632 pp_cxx_ws_string (pp, OVL_OP_INFO (false, TREE_CODE (t))->name);
2633 pp_cxx_right_paren (pp);
2634 break;
2636 case NON_LVALUE_EXPR:
2637 /* FIXME: This is a KLUDGE workaround for a parsing problem. There
2638 should be another level of INDIRECT_REF so that I don't have to do
2639 this. */
2640 if (TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == POINTER_TYPE)
2642 tree next = TREE_TYPE (TREE_TYPE (t));
2644 while (TYPE_PTR_P (next))
2645 next = TREE_TYPE (next);
2647 if (TREE_CODE (next) == FUNCTION_TYPE)
2649 if (flags & TFF_EXPR_IN_PARENS)
2650 pp_cxx_left_paren (pp);
2651 pp_cxx_star (pp);
2652 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2653 if (flags & TFF_EXPR_IN_PARENS)
2654 pp_cxx_right_paren (pp);
2655 break;
2657 /* Else fall through. */
2659 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2660 break;
2662 CASE_CONVERT:
2663 case IMPLICIT_CONV_EXPR:
2664 case VIEW_CONVERT_EXPR:
2666 tree op = TREE_OPERAND (t, 0);
2668 if (location_wrapper_p (t))
2670 dump_expr (pp, op, flags);
2671 break;
2674 tree ttype = TREE_TYPE (t);
2675 tree optype = TREE_TYPE (op);
2677 if (TREE_CODE (ttype) != TREE_CODE (optype)
2678 && INDIRECT_TYPE_P (ttype)
2679 && INDIRECT_TYPE_P (optype)
2680 && same_type_p (TREE_TYPE (optype),
2681 TREE_TYPE (ttype)))
2683 if (TYPE_REF_P (ttype))
2685 STRIP_NOPS (op);
2686 if (TREE_CODE (op) == ADDR_EXPR)
2687 dump_expr (pp, TREE_OPERAND (op, 0), flags);
2688 else
2689 dump_unary_op (pp, "*", t, flags);
2691 else
2692 dump_unary_op (pp, "&", t, flags);
2694 else if (!same_type_p (TREE_TYPE (op), TREE_TYPE (t)))
2696 /* It is a cast, but we cannot tell whether it is a
2697 reinterpret or static cast. Use the C style notation. */
2698 if (flags & TFF_EXPR_IN_PARENS)
2699 pp_cxx_left_paren (pp);
2700 pp_cxx_left_paren (pp);
2701 dump_type (pp, TREE_TYPE (t), flags);
2702 pp_cxx_right_paren (pp);
2703 dump_expr (pp, op, flags | TFF_EXPR_IN_PARENS);
2704 if (flags & TFF_EXPR_IN_PARENS)
2705 pp_cxx_right_paren (pp);
2707 else
2708 dump_expr (pp, op, flags);
2709 break;
2712 case CONSTRUCTOR:
2713 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
2715 tree idx = build_ptrmemfunc_access_expr (t, pfn_identifier);
2717 if (integer_zerop (idx))
2719 /* A NULL pointer-to-member constant. */
2720 pp_cxx_left_paren (pp);
2721 pp_cxx_left_paren (pp);
2722 dump_type (pp, TREE_TYPE (t), flags);
2723 pp_cxx_right_paren (pp);
2724 pp_character (pp, '0');
2725 pp_cxx_right_paren (pp);
2726 break;
2728 else if (tree_fits_shwi_p (idx))
2730 tree virtuals;
2731 unsigned HOST_WIDE_INT n;
2733 t = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
2734 t = TYPE_METHOD_BASETYPE (t);
2735 virtuals = BINFO_VIRTUALS (TYPE_BINFO (TYPE_MAIN_VARIANT (t)));
2737 n = tree_to_shwi (idx);
2739 /* Map vtable index back one, to allow for the null pointer to
2740 member. */
2741 --n;
2743 while (n > 0 && virtuals)
2745 --n;
2746 virtuals = TREE_CHAIN (virtuals);
2748 if (virtuals)
2750 dump_expr (pp, BV_FN (virtuals),
2751 flags | TFF_EXPR_IN_PARENS);
2752 break;
2756 if (TREE_TYPE (t) && LAMBDA_TYPE_P (TREE_TYPE (t)))
2757 pp_string (pp, "<lambda closure object>");
2758 if (TREE_TYPE (t) && EMPTY_CONSTRUCTOR_P (t))
2760 dump_type (pp, TREE_TYPE (t), 0);
2761 pp_cxx_left_paren (pp);
2762 pp_cxx_right_paren (pp);
2764 else
2766 if (!BRACE_ENCLOSED_INITIALIZER_P (t))
2767 dump_type (pp, TREE_TYPE (t), 0);
2768 pp_cxx_left_brace (pp);
2769 dump_expr_init_vec (pp, CONSTRUCTOR_ELTS (t), flags);
2770 pp_cxx_right_brace (pp);
2773 break;
2775 case OFFSET_REF:
2777 tree ob = TREE_OPERAND (t, 0);
2778 if (is_dummy_object (ob))
2780 t = TREE_OPERAND (t, 1);
2781 if (TREE_CODE (t) == FUNCTION_DECL)
2782 /* A::f */
2783 dump_expr (pp, t, flags | TFF_EXPR_IN_PARENS);
2784 else if (BASELINK_P (t))
2785 dump_expr (pp, OVL_FIRST (BASELINK_FUNCTIONS (t)),
2786 flags | TFF_EXPR_IN_PARENS);
2787 else
2788 dump_decl (pp, t, flags);
2790 else
2792 if (INDIRECT_REF_P (ob))
2794 dump_expr (pp, TREE_OPERAND (ob, 0), flags | TFF_EXPR_IN_PARENS);
2795 pp_cxx_arrow (pp);
2796 pp_cxx_star (pp);
2798 else
2800 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2801 pp_cxx_dot (pp);
2802 pp_cxx_star (pp);
2804 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2806 break;
2809 case TEMPLATE_PARM_INDEX:
2810 dump_decl (pp, TEMPLATE_PARM_DECL (t), flags & ~TFF_DECL_SPECIFIERS);
2811 break;
2813 case CAST_EXPR:
2814 if (TREE_OPERAND (t, 0) == NULL_TREE
2815 || TREE_CHAIN (TREE_OPERAND (t, 0)))
2817 dump_type (pp, TREE_TYPE (t), flags);
2818 pp_cxx_left_paren (pp);
2819 dump_expr_list (pp, TREE_OPERAND (t, 0), flags);
2820 pp_cxx_right_paren (pp);
2822 else
2824 pp_cxx_left_paren (pp);
2825 dump_type (pp, TREE_TYPE (t), flags);
2826 pp_cxx_right_paren (pp);
2827 pp_cxx_left_paren (pp);
2828 dump_expr_list (pp, TREE_OPERAND (t, 0), flags);
2829 pp_cxx_right_paren (pp);
2831 break;
2833 case STATIC_CAST_EXPR:
2834 pp_cxx_ws_string (pp, "static_cast");
2835 goto cast;
2836 case REINTERPRET_CAST_EXPR:
2837 pp_cxx_ws_string (pp, "reinterpret_cast");
2838 goto cast;
2839 case CONST_CAST_EXPR:
2840 pp_cxx_ws_string (pp, "const_cast");
2841 goto cast;
2842 case DYNAMIC_CAST_EXPR:
2843 pp_cxx_ws_string (pp, "dynamic_cast");
2844 cast:
2845 pp_cxx_begin_template_argument_list (pp);
2846 dump_type (pp, TREE_TYPE (t), flags);
2847 pp_cxx_end_template_argument_list (pp);
2848 pp_cxx_left_paren (pp);
2849 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2850 pp_cxx_right_paren (pp);
2851 break;
2853 case ARROW_EXPR:
2854 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2855 pp_cxx_arrow (pp);
2856 break;
2858 case SIZEOF_EXPR:
2859 case ALIGNOF_EXPR:
2860 if (TREE_CODE (t) == SIZEOF_EXPR)
2861 pp_cxx_ws_string (pp, "sizeof");
2862 else if (ALIGNOF_EXPR_STD_P (t))
2863 pp_cxx_ws_string (pp, "alignof");
2864 else
2865 pp_cxx_ws_string (pp, "__alignof__");
2866 op = TREE_OPERAND (t, 0);
2867 if (PACK_EXPANSION_P (op))
2869 pp_string (pp, "...");
2870 op = PACK_EXPANSION_PATTERN (op);
2872 pp_cxx_whitespace (pp);
2873 pp_cxx_left_paren (pp);
2874 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
2875 dump_type (pp, TREE_TYPE (op), flags);
2876 else if (TYPE_P (TREE_OPERAND (t, 0)))
2877 dump_type (pp, op, flags);
2878 else
2879 dump_expr (pp, op, flags);
2880 pp_cxx_right_paren (pp);
2881 break;
2883 case AT_ENCODE_EXPR:
2884 pp_cxx_ws_string (pp, "@encode");
2885 pp_cxx_whitespace (pp);
2886 pp_cxx_left_paren (pp);
2887 dump_type (pp, TREE_OPERAND (t, 0), flags);
2888 pp_cxx_right_paren (pp);
2889 break;
2891 case NOEXCEPT_EXPR:
2892 pp_cxx_ws_string (pp, "noexcept");
2893 pp_cxx_whitespace (pp);
2894 pp_cxx_left_paren (pp);
2895 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2896 pp_cxx_right_paren (pp);
2897 break;
2899 case REALPART_EXPR:
2900 case IMAGPART_EXPR:
2901 pp_cxx_ws_string (pp, OVL_OP_INFO (false, TREE_CODE (t))->name);
2902 pp_cxx_whitespace (pp);
2903 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2904 break;
2906 case DEFERRED_PARSE:
2907 pp_string (pp, M_("<unparsed>"));
2908 break;
2910 case TRY_CATCH_EXPR:
2911 case CLEANUP_POINT_EXPR:
2912 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2913 break;
2915 case PSEUDO_DTOR_EXPR:
2916 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2917 pp_cxx_dot (pp);
2918 if (TREE_OPERAND (t, 1))
2920 dump_type (pp, TREE_OPERAND (t, 1), flags);
2921 pp_cxx_colon_colon (pp);
2923 pp_cxx_complement (pp);
2924 dump_type (pp, TREE_OPERAND (t, 2), flags);
2925 break;
2927 case TEMPLATE_ID_EXPR:
2928 dump_decl (pp, t, flags);
2929 break;
2931 case BIND_EXPR:
2932 case STMT_EXPR:
2933 case EXPR_STMT:
2934 case STATEMENT_LIST:
2935 /* We don't yet have a way of dumping statements in a
2936 human-readable format. */
2937 pp_string (pp, "({...})");
2938 break;
2940 case LOOP_EXPR:
2941 pp_string (pp, "while (1) { ");
2942 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2943 pp_cxx_right_brace (pp);
2944 break;
2946 case EXIT_EXPR:
2947 pp_string (pp, "if (");
2948 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2949 pp_string (pp, ") break; ");
2950 break;
2952 case BASELINK:
2953 dump_expr (pp, BASELINK_FUNCTIONS (t), flags & ~TFF_EXPR_IN_PARENS);
2954 break;
2956 case EMPTY_CLASS_EXPR:
2957 dump_type (pp, TREE_TYPE (t), flags);
2958 pp_cxx_left_paren (pp);
2959 pp_cxx_right_paren (pp);
2960 break;
2962 case ARGUMENT_PACK_SELECT:
2963 dump_template_argument (pp, ARGUMENT_PACK_SELECT_FROM_PACK (t), flags);
2964 break;
2966 case RECORD_TYPE:
2967 case UNION_TYPE:
2968 case ENUMERAL_TYPE:
2969 case REAL_TYPE:
2970 case VOID_TYPE:
2971 case OPAQUE_TYPE:
2972 case BOOLEAN_TYPE:
2973 case INTEGER_TYPE:
2974 case COMPLEX_TYPE:
2975 case VECTOR_TYPE:
2976 case DECLTYPE_TYPE:
2977 pp_type_specifier_seq (pp, t);
2978 break;
2980 case TYPENAME_TYPE:
2981 /* We get here when we want to print a dependent type as an
2982 id-expression, without any disambiguator decoration. */
2983 pp->id_expression (t);
2984 break;
2986 case TEMPLATE_TYPE_PARM:
2987 case TEMPLATE_TEMPLATE_PARM:
2988 case BOUND_TEMPLATE_TEMPLATE_PARM:
2989 dump_type (pp, t, flags);
2990 break;
2992 case TRAIT_EXPR:
2993 pp_cxx_trait (pp, t);
2994 break;
2996 case VA_ARG_EXPR:
2997 pp_cxx_va_arg_expression (pp, t);
2998 break;
3000 case OFFSETOF_EXPR:
3001 pp_cxx_offsetof_expression (pp, t);
3002 break;
3004 case ADDRESSOF_EXPR:
3005 pp_cxx_addressof_expression (pp, t);
3006 break;
3008 case SCOPE_REF:
3009 dump_decl (pp, t, flags);
3010 break;
3012 case EXPR_PACK_EXPANSION:
3013 case UNARY_LEFT_FOLD_EXPR:
3014 case UNARY_RIGHT_FOLD_EXPR:
3015 case BINARY_LEFT_FOLD_EXPR:
3016 case BINARY_RIGHT_FOLD_EXPR:
3017 case TYPEID_EXPR:
3018 case MEMBER_REF:
3019 case DOTSTAR_EXPR:
3020 case NEW_EXPR:
3021 case VEC_NEW_EXPR:
3022 case DELETE_EXPR:
3023 case VEC_DELETE_EXPR:
3024 case MODOP_EXPR:
3025 case ABS_EXPR:
3026 case ABSU_EXPR:
3027 case CONJ_EXPR:
3028 case VECTOR_CST:
3029 case FIXED_CST:
3030 case UNORDERED_EXPR:
3031 case ORDERED_EXPR:
3032 case UNLT_EXPR:
3033 case UNLE_EXPR:
3034 case UNGT_EXPR:
3035 case UNGE_EXPR:
3036 case UNEQ_EXPR:
3037 case LTGT_EXPR:
3038 case COMPLEX_EXPR:
3039 case BIT_FIELD_REF:
3040 case FIX_TRUNC_EXPR:
3041 case FLOAT_EXPR:
3042 pp->expression (t);
3043 break;
3045 case TRUTH_AND_EXPR:
3046 case TRUTH_OR_EXPR:
3047 case TRUTH_XOR_EXPR:
3048 if (flags & TFF_EXPR_IN_PARENS)
3049 pp_cxx_left_paren (pp);
3050 pp->expression (t);
3051 if (flags & TFF_EXPR_IN_PARENS)
3052 pp_cxx_right_paren (pp);
3053 break;
3055 case OBJ_TYPE_REF:
3056 dump_expr (pp, resolve_virtual_fun_from_obj_type_ref (t), flags);
3057 break;
3059 case LAMBDA_EXPR:
3060 pp_string (pp, M_("<lambda>"));
3061 break;
3063 case PAREN_EXPR:
3064 pp_cxx_left_paren (pp);
3065 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
3066 pp_cxx_right_paren (pp);
3067 break;
3069 case REQUIRES_EXPR:
3070 pp_cxx_requires_expr (cxx_pp, t);
3071 break;
3073 case SIMPLE_REQ:
3074 pp_cxx_simple_requirement (cxx_pp, t);
3075 break;
3077 case TYPE_REQ:
3078 pp_cxx_type_requirement (cxx_pp, t);
3079 break;
3081 case COMPOUND_REQ:
3082 pp_cxx_compound_requirement (cxx_pp, t);
3083 break;
3085 case NESTED_REQ:
3086 pp_cxx_nested_requirement (cxx_pp, t);
3087 break;
3089 case ATOMIC_CONSTR:
3090 case CHECK_CONSTR:
3091 case CONJ_CONSTR:
3092 case DISJ_CONSTR:
3094 pp_cxx_constraint (cxx_pp, t);
3095 break;
3098 case PLACEHOLDER_EXPR:
3099 pp_string (pp, M_("*this"));
3100 break;
3102 case TREE_LIST:
3103 dump_expr_list (pp, t, flags);
3104 break;
3106 /* This list is incomplete, but should suffice for now.
3107 It is very important that `sorry' does not call
3108 `report_error_function'. That could cause an infinite loop. */
3109 default:
3110 pp_unsupported_tree (pp, t);
3111 /* Fall through. */
3112 case ERROR_MARK:
3113 pp_string (pp, M_("<expression error>"));
3114 break;
3118 static void
3119 dump_binary_op (cxx_pretty_printer *pp, const char *opstring, tree t,
3120 int flags)
3122 pp_cxx_left_paren (pp);
3123 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
3124 pp_cxx_whitespace (pp);
3125 if (opstring)
3126 pp_cxx_ws_string (pp, opstring);
3127 else
3128 pp_string (pp, M_("<unknown operator>"));
3129 pp_cxx_whitespace (pp);
3130 tree op1 = TREE_OPERAND (t, 1);
3131 if (TREE_CODE (t) == POINTER_PLUS_EXPR
3132 && TREE_CODE (op1) == INTEGER_CST
3133 && tree_int_cst_sign_bit (op1))
3134 /* A pointer minus an integer is represented internally as plus a very
3135 large number, don't expose that to users. */
3136 op1 = convert (ssizetype, op1);
3137 dump_expr (pp, op1, flags | TFF_EXPR_IN_PARENS);
3138 pp_cxx_right_paren (pp);
3141 static void
3142 dump_unary_op (cxx_pretty_printer *pp, const char *opstring, tree t, int flags)
3144 if (flags & TFF_EXPR_IN_PARENS)
3145 pp_cxx_left_paren (pp);
3146 pp_cxx_ws_string (pp, opstring);
3147 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
3148 if (flags & TFF_EXPR_IN_PARENS)
3149 pp_cxx_right_paren (pp);
3152 static void
3153 reinit_cxx_pp (void)
3155 pp_clear_output_area (cxx_pp);
3156 cxx_pp->padding = pp_none;
3157 pp_indentation (cxx_pp) = 0;
3158 pp_needs_newline (cxx_pp) = false;
3159 pp_show_color (cxx_pp) = false;
3160 cxx_pp->enclosing_scope = current_function_decl;
3163 /* Same as pp_formatted_text, except the return string is a separate
3164 copy and has a GGC storage duration, e.g. an indefinite lifetime. */
3166 inline const char *
3167 pp_ggc_formatted_text (pretty_printer *pp)
3169 return ggc_strdup (pp_formatted_text (pp));
3172 /* Exported interface to stringifying types, exprs and decls under TFF_*
3173 control. */
3175 const char *
3176 type_as_string (tree typ, int flags)
3178 reinit_cxx_pp ();
3179 pp_translate_identifiers (cxx_pp) = false;
3180 dump_type (cxx_pp, typ, flags);
3181 return pp_ggc_formatted_text (cxx_pp);
3184 const char *
3185 type_as_string_translate (tree typ, int flags)
3187 reinit_cxx_pp ();
3188 dump_type (cxx_pp, typ, flags);
3189 return pp_ggc_formatted_text (cxx_pp);
3192 const char *
3193 expr_as_string (tree decl, int flags)
3195 reinit_cxx_pp ();
3196 pp_translate_identifiers (cxx_pp) = false;
3197 dump_expr (cxx_pp, decl, flags);
3198 return pp_ggc_formatted_text (cxx_pp);
3201 /* Wrap decl_as_string with options appropriate for dwarf. */
3203 const char *
3204 decl_as_dwarf_string (tree decl, int flags)
3206 const char *name;
3207 /* Curiously, reinit_cxx_pp doesn't reset the flags field, so setting the flag
3208 here will be adequate to get the desired behavior. */
3209 cxx_pp->flags |= pp_c_flag_gnu_v3;
3210 name = decl_as_string (decl, flags);
3211 /* Subsequent calls to the pretty printer shouldn't use this style. */
3212 cxx_pp->flags &= ~pp_c_flag_gnu_v3;
3213 return name;
3216 const char *
3217 decl_as_string (tree decl, int flags)
3219 reinit_cxx_pp ();
3220 pp_translate_identifiers (cxx_pp) = false;
3221 dump_decl (cxx_pp, decl, flags);
3222 return pp_ggc_formatted_text (cxx_pp);
3225 const char *
3226 decl_as_string_translate (tree decl, int flags)
3228 reinit_cxx_pp ();
3229 dump_decl (cxx_pp, decl, flags);
3230 return pp_ggc_formatted_text (cxx_pp);
3233 /* Wrap lang_decl_name with options appropriate for dwarf. */
3235 const char *
3236 lang_decl_dwarf_name (tree decl, int v, bool translate)
3238 const char *name;
3239 /* Curiously, reinit_cxx_pp doesn't reset the flags field, so setting the flag
3240 here will be adequate to get the desired behavior. */
3241 cxx_pp->flags |= pp_c_flag_gnu_v3;
3242 name = lang_decl_name (decl, v, translate);
3243 /* Subsequent calls to the pretty printer shouldn't use this style. */
3244 cxx_pp->flags &= ~pp_c_flag_gnu_v3;
3245 return name;
3248 /* Generate the three forms of printable names for cxx_printable_name. */
3250 const char *
3251 lang_decl_name (tree decl, int v, bool translate)
3253 if (v >= 2)
3254 return (translate
3255 ? decl_as_string_translate (decl, TFF_DECL_SPECIFIERS)
3256 : decl_as_string (decl, TFF_DECL_SPECIFIERS));
3258 reinit_cxx_pp ();
3259 pp_translate_identifiers (cxx_pp) = translate;
3260 if (v == 1
3261 && (DECL_CLASS_SCOPE_P (decl)
3262 || (DECL_NAMESPACE_SCOPE_P (decl)
3263 && CP_DECL_CONTEXT (decl) != global_namespace)))
3265 dump_type (cxx_pp, CP_DECL_CONTEXT (decl), TFF_PLAIN_IDENTIFIER);
3266 pp_cxx_colon_colon (cxx_pp);
3269 if (TREE_CODE (decl) == FUNCTION_DECL)
3270 dump_function_name (cxx_pp, decl, TFF_PLAIN_IDENTIFIER);
3271 else if ((DECL_NAME (decl) == NULL_TREE)
3272 && TREE_CODE (decl) == NAMESPACE_DECL)
3273 dump_decl (cxx_pp, decl, TFF_PLAIN_IDENTIFIER | TFF_UNQUALIFIED_NAME);
3274 else
3275 dump_decl (cxx_pp, DECL_NAME (decl), TFF_PLAIN_IDENTIFIER);
3277 return pp_ggc_formatted_text (cxx_pp);
3280 /* Return the location of a tree passed to %+ formats. */
3282 location_t
3283 location_of (tree t)
3285 if (TYPE_P (t))
3287 t = TYPE_MAIN_DECL (t);
3288 if (t == NULL_TREE)
3289 return input_location;
3291 else if (TREE_CODE (t) == OVERLOAD)
3292 t = OVL_FIRST (t);
3294 if (DECL_P (t))
3295 return DECL_SOURCE_LOCATION (t);
3296 if (TREE_CODE (t) == DEFERRED_PARSE)
3297 return defparse_location (t);
3298 return cp_expr_loc_or_input_loc (t);
3301 /* Now the interfaces from error et al to dump_type et al. Each takes an
3302 on/off VERBOSE flag and supply the appropriate TFF_ flags to a dump_
3303 function. */
3305 static const char *
3306 decl_to_string (tree decl, int verbose, bool show_color)
3308 int flags = 0;
3310 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == RECORD_TYPE
3311 || TREE_CODE (decl) == UNION_TYPE || TREE_CODE (decl) == ENUMERAL_TYPE)
3312 flags = TFF_CLASS_KEY_OR_ENUM;
3313 if (verbose)
3314 flags |= TFF_DECL_SPECIFIERS;
3315 else if (TREE_CODE (decl) == FUNCTION_DECL)
3316 flags |= TFF_DECL_SPECIFIERS | TFF_RETURN_TYPE;
3317 flags |= TFF_TEMPLATE_HEADER;
3319 reinit_cxx_pp ();
3320 pp_show_color (cxx_pp) = show_color;
3321 dump_decl (cxx_pp, decl, flags);
3322 return pp_ggc_formatted_text (cxx_pp);
3325 const char *
3326 expr_to_string (tree decl)
3328 reinit_cxx_pp ();
3329 dump_expr (cxx_pp, decl, 0);
3330 return pp_ggc_formatted_text (cxx_pp);
3333 static const char *
3334 fndecl_to_string (tree fndecl, int verbose)
3336 int flags;
3338 flags = TFF_EXCEPTION_SPECIFICATION | TFF_DECL_SPECIFIERS
3339 | TFF_TEMPLATE_HEADER;
3340 if (verbose)
3341 flags |= TFF_FUNCTION_DEFAULT_ARGUMENTS;
3342 reinit_cxx_pp ();
3343 dump_decl (cxx_pp, fndecl, flags);
3344 return pp_ggc_formatted_text (cxx_pp);
3348 static const char *
3349 code_to_string (enum tree_code c)
3351 return get_tree_code_name (c);
3354 const char *
3355 language_to_string (enum languages c)
3357 switch (c)
3359 case lang_c:
3360 return "C";
3362 case lang_cplusplus:
3363 return "C++";
3365 default:
3366 gcc_unreachable ();
3368 return NULL;
3371 /* Return the proper printed version of a parameter to a C++ function. */
3373 static const char *
3374 parm_to_string (int p)
3376 reinit_cxx_pp ();
3377 if (p < 0)
3378 pp_string (cxx_pp, "'this'");
3379 else
3380 pp_decimal_int (cxx_pp, p + 1);
3381 return pp_ggc_formatted_text (cxx_pp);
3384 static const char *
3385 op_to_string (bool assop, enum tree_code p)
3387 tree id = ovl_op_identifier (assop, p);
3388 return id ? IDENTIFIER_POINTER (id) : M_("<unknown>");
3391 /* Return a GC-allocated representation of type TYP, with verbosity VERBOSE.
3393 If QUOTE is non-NULL and if *QUOTE is true, then quotes are added to the
3394 string in appropriate places, and *QUOTE is written to with false
3395 to suppress pp_format's trailing close quote so that e.g.
3396 foo_typedef {aka underlying_foo} {enum}
3397 can be printed by "%qT" as:
3398 `foo_typedef' {aka `underlying_foo'} {enum}
3399 rather than:
3400 `foo_typedef {aka underlying_foo} {enum}'
3401 When adding such quotes, if POSTPROCESSED is true (for handling %H and %I)
3402 then a leading open quote will be added, whereas if POSTPROCESSED is false
3403 (for handling %T) then any leading quote has already been added by
3404 pp_format, or is not needed due to QUOTE being NULL (for template arguments
3405 within %H and %I).
3407 SHOW_COLOR is used to determine the colorization of any quotes that
3408 are added. */
3410 static const char *
3411 type_to_string (tree typ, int verbose, bool postprocessed, bool *quote,
3412 bool show_color)
3414 int flags = 0;
3415 if (verbose)
3416 flags |= TFF_CLASS_KEY_OR_ENUM;
3417 flags |= TFF_TEMPLATE_HEADER;
3419 reinit_cxx_pp ();
3420 pp_show_color (cxx_pp) = show_color;
3422 if (postprocessed && quote && *quote)
3423 pp_begin_quote (cxx_pp, show_color);
3425 struct obstack *ob = pp_buffer (cxx_pp)->obstack;
3426 int type_start, type_len;
3427 type_start = obstack_object_size (ob);
3429 dump_type (cxx_pp, typ, flags);
3431 /* Remember the end of the initial dump. */
3432 type_len = obstack_object_size (ob) - type_start;
3434 /* If we're printing a type that involves typedefs, also print the
3435 stripped version. But sometimes the stripped version looks
3436 exactly the same, so we don't want it after all. To avoid printing
3437 it in that case, we play ugly obstack games. */
3438 if (typ && TYPE_P (typ) && typ != TYPE_CANONICAL (typ)
3439 && !uses_template_parms (typ))
3441 int aka_start, aka_len; char *p;
3442 tree aka = strip_typedefs (typ, NULL, STF_USER_VISIBLE);
3443 if (quote && *quote)
3444 pp_end_quote (cxx_pp, show_color);
3445 pp_string (cxx_pp, " {aka");
3446 pp_cxx_whitespace (cxx_pp);
3447 if (quote && *quote)
3448 pp_begin_quote (cxx_pp, show_color);
3449 /* And remember the start of the aka dump. */
3450 aka_start = obstack_object_size (ob);
3451 dump_type (cxx_pp, aka, flags);
3452 aka_len = obstack_object_size (ob) - aka_start;
3453 if (quote && *quote)
3454 pp_end_quote (cxx_pp, show_color);
3455 pp_right_brace (cxx_pp);
3456 p = (char*)obstack_base (ob);
3457 /* If they are identical, cut off the aka by unwinding the obstack. */
3458 if (type_len == aka_len
3459 && memcmp (p + type_start, p+aka_start, type_len) == 0)
3461 /* We can't add a '\0' here, since we may be adding a closing quote
3462 below, and it would be hidden by the '\0'.
3463 Instead, manually unwind the current object within the obstack
3464 so that the insertion point is at the end of the type, before
3465 the "' {aka". */
3466 int delta = type_start + type_len - obstack_object_size (ob);
3467 gcc_assert (delta <= 0);
3468 obstack_blank_fast (ob, delta);
3470 else
3471 if (quote)
3472 /* No further closing quotes are needed. */
3473 *quote = false;
3476 if (quote && *quote)
3478 pp_end_quote (cxx_pp, show_color);
3479 *quote = false;
3481 return pp_ggc_formatted_text (cxx_pp);
3484 static const char *
3485 args_to_string (tree p, int verbose)
3487 int flags = 0;
3488 if (verbose)
3489 flags |= TFF_CLASS_KEY_OR_ENUM;
3491 if (p == NULL_TREE)
3492 return "";
3494 if (TYPE_P (TREE_VALUE (p)))
3495 return type_as_string_translate (p, flags);
3497 reinit_cxx_pp ();
3498 for (; p; p = TREE_CHAIN (p))
3500 if (null_node_p (TREE_VALUE (p)))
3501 pp_cxx_ws_string (cxx_pp, "NULL");
3502 else
3503 dump_type (cxx_pp, error_type (TREE_VALUE (p)), flags);
3504 if (TREE_CHAIN (p))
3505 pp_separate_with_comma (cxx_pp);
3507 return pp_ggc_formatted_text (cxx_pp);
3510 /* Pretty-print a deduction substitution (from deduction_tsubst_fntype). P
3511 is a TREE_LIST with purpose the TEMPLATE_DECL, value the template
3512 arguments. */
3514 static const char *
3515 subst_to_string (tree p, bool show_color)
3517 tree decl = TREE_PURPOSE (p);
3518 tree targs = TREE_VALUE (p);
3519 tree tparms = DECL_TEMPLATE_PARMS (decl);
3520 int flags = (TFF_DECL_SPECIFIERS|TFF_TEMPLATE_HEADER
3521 |TFF_NO_TEMPLATE_BINDINGS);
3523 if (p == NULL_TREE)
3524 return "";
3526 reinit_cxx_pp ();
3527 pp_show_color (cxx_pp) = show_color;
3528 dump_template_decl (cxx_pp, TREE_PURPOSE (p), flags);
3529 dump_substitution (cxx_pp, NULL, tparms, targs, /*flags=*/0);
3530 return pp_ggc_formatted_text (cxx_pp);
3533 static const char *
3534 cv_to_string (tree p, int v)
3536 reinit_cxx_pp ();
3537 cxx_pp->padding = v ? pp_before : pp_none;
3538 pp_cxx_cv_qualifier_seq (cxx_pp, p);
3539 return pp_ggc_formatted_text (cxx_pp);
3542 static const char *
3543 eh_spec_to_string (tree p, int /*v*/)
3545 int flags = 0;
3546 reinit_cxx_pp ();
3547 dump_exception_spec (cxx_pp, p, flags);
3548 return pp_ggc_formatted_text (cxx_pp);
3551 /* Langhook for print_error_function. */
3552 void
3553 cxx_print_error_function (diagnostic_context *context, const char *file,
3554 const diagnostic_info *diagnostic)
3556 char *prefix;
3557 if (file)
3558 prefix = xstrdup (file);
3559 else
3560 prefix = NULL;
3561 lhd_print_error_function (context, file, diagnostic);
3562 pp_set_prefix (context->printer, prefix);
3563 maybe_print_instantiation_context (context);
3566 static void
3567 cp_diagnostic_starter (diagnostic_context *context,
3568 const diagnostic_info *diagnostic)
3570 diagnostic_report_current_module (context, diagnostic_location (diagnostic));
3571 cp_print_error_function (context, diagnostic);
3572 maybe_print_instantiation_context (context);
3573 maybe_print_constexpr_context (context);
3574 maybe_print_constraint_context (context);
3575 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
3576 diagnostic));
3579 /* Print current function onto BUFFER, in the process of reporting
3580 a diagnostic message. Called from cp_diagnostic_starter. */
3581 static void
3582 cp_print_error_function (diagnostic_context *context,
3583 const diagnostic_info *diagnostic)
3585 /* If we are in an instantiation context, current_function_decl is likely
3586 to be wrong, so just rely on print_instantiation_full_context. */
3587 if (current_instantiation ())
3588 return;
3589 /* The above is true for constraint satisfaction also. */
3590 if (current_failed_constraint)
3591 return;
3592 if (diagnostic_last_function_changed (context, diagnostic))
3594 char *old_prefix = pp_take_prefix (context->printer);
3595 const char *file = LOCATION_FILE (diagnostic_location (diagnostic));
3596 tree abstract_origin = diagnostic_abstract_origin (diagnostic);
3597 char *new_prefix = (file && abstract_origin == NULL)
3598 ? file_name_as_prefix (context, file) : NULL;
3600 pp_set_prefix (context->printer, new_prefix);
3602 if (current_function_decl == NULL)
3603 pp_string (context->printer, _("At global scope:"));
3604 else
3606 tree fndecl, ao;
3608 if (abstract_origin)
3610 ao = BLOCK_ABSTRACT_ORIGIN (abstract_origin);
3611 gcc_assert (TREE_CODE (ao) == FUNCTION_DECL);
3612 fndecl = ao;
3614 else
3615 fndecl = current_function_decl;
3617 pp_printf (context->printer, function_category (fndecl),
3618 fndecl);
3620 while (abstract_origin)
3622 location_t *locus;
3623 tree block = abstract_origin;
3625 locus = &BLOCK_SOURCE_LOCATION (block);
3626 fndecl = NULL;
3627 block = BLOCK_SUPERCONTEXT (block);
3628 while (block && TREE_CODE (block) == BLOCK
3629 && BLOCK_ABSTRACT_ORIGIN (block))
3631 ao = BLOCK_ABSTRACT_ORIGIN (block);
3632 if (TREE_CODE (ao) == FUNCTION_DECL)
3634 fndecl = ao;
3635 break;
3637 else if (TREE_CODE (ao) != BLOCK)
3638 break;
3640 block = BLOCK_SUPERCONTEXT (block);
3642 if (fndecl)
3643 abstract_origin = block;
3644 else
3646 while (block && TREE_CODE (block) == BLOCK)
3647 block = BLOCK_SUPERCONTEXT (block);
3649 if (block && TREE_CODE (block) == FUNCTION_DECL)
3650 fndecl = block;
3651 abstract_origin = NULL;
3653 if (fndecl)
3655 expanded_location s = expand_location (*locus);
3656 pp_character (context->printer, ',');
3657 pp_newline (context->printer);
3658 if (s.file != NULL)
3660 if (context->m_show_column && s.column != 0)
3661 pp_printf (context->printer,
3662 _(" inlined from %qD at %r%s:%d:%d%R"),
3663 fndecl,
3664 "locus", s.file, s.line, s.column);
3665 else
3666 pp_printf (context->printer,
3667 _(" inlined from %qD at %r%s:%d%R"),
3668 fndecl,
3669 "locus", s.file, s.line);
3672 else
3673 pp_printf (context->printer, _(" inlined from %qD"),
3674 fndecl);
3677 pp_character (context->printer, ':');
3679 pp_newline (context->printer);
3681 diagnostic_set_last_function (context, diagnostic);
3682 pp_destroy_prefix (context->printer);
3683 context->printer->prefix = old_prefix;
3687 /* Returns a description of FUNCTION using standard terminology. The
3688 result is a format string of the form "In CATEGORY %qD". */
3690 static const char *
3691 function_category (tree fn)
3693 /* We can get called from the middle-end for diagnostics of function
3694 clones. Make sure we have language specific information before
3695 dereferencing it. */
3696 if (DECL_LANG_SPECIFIC (STRIP_TEMPLATE (fn))
3697 && DECL_FUNCTION_MEMBER_P (fn))
3699 if (DECL_STATIC_FUNCTION_P (fn))
3700 return _("In static member function %qD");
3701 else if (DECL_COPY_CONSTRUCTOR_P (fn))
3702 return _("In copy constructor %qD");
3703 else if (DECL_CONSTRUCTOR_P (fn))
3704 return _("In constructor %qD");
3705 else if (DECL_DESTRUCTOR_P (fn))
3706 return _("In destructor %qD");
3707 else if (LAMBDA_FUNCTION_P (fn))
3708 return _("In lambda function");
3709 else if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
3710 return _("In explicit object member function %qD");
3711 else
3712 return _("In member function %qD");
3714 else
3715 return _("In function %qD");
3718 /* Disable warnings about missing quoting in GCC diagnostics for
3719 the pp_verbatim calls. Their format strings deliberately don't
3720 follow GCC diagnostic conventions. */
3721 #if __GNUC__ >= 10
3722 #pragma GCC diagnostic push
3723 #pragma GCC diagnostic ignored "-Wformat-diag"
3724 #endif
3726 /* Report the full context of a current template instantiation,
3727 onto BUFFER. */
3728 static void
3729 print_instantiation_full_context (diagnostic_context *context)
3731 struct tinst_level *p = current_instantiation ();
3732 location_t location = input_location;
3734 if (p)
3736 pp_verbatim (context->printer,
3737 p->list_p ()
3738 ? _("%s: In substitution of %qS:\n")
3739 : _("%s: In instantiation of %q#D:\n"),
3740 LOCATION_FILE (location),
3741 p->get_node ());
3743 location = p->locus;
3744 p = p->next;
3747 print_instantiation_partial_context (context, p, location);
3750 /* Helper function of print_instantiation_partial_context() that
3751 prints a single line of instantiation context. */
3753 static void
3754 print_instantiation_partial_context_line (diagnostic_context *context,
3755 struct tinst_level *t,
3756 location_t loc, bool recursive_p)
3758 if (loc == UNKNOWN_LOCATION)
3759 return;
3761 expanded_location xloc = expand_location (loc);
3763 if (context->m_show_column)
3764 pp_verbatim (context->printer, _("%r%s:%d:%d:%R "),
3765 "locus", xloc.file, xloc.line, xloc.column);
3766 else
3767 pp_verbatim (context->printer, _("%r%s:%d:%R "),
3768 "locus", xloc.file, xloc.line);
3770 if (t != NULL)
3772 if (t->list_p ())
3773 pp_verbatim (context->printer,
3774 recursive_p
3775 ? _("recursively required by substitution of %qS\n")
3776 : _("required by substitution of %qS\n"),
3777 t->get_node ());
3778 else
3779 pp_verbatim (context->printer,
3780 recursive_p
3781 ? _("recursively required from %q#D\n")
3782 : _("required from %q#D\n"),
3783 t->get_node ());
3785 else
3787 pp_verbatim (context->printer,
3788 recursive_p
3789 ? _("recursively required from here\n")
3790 : _("required from here\n"));
3792 gcc_rich_location rich_loc (loc);
3793 diagnostic_show_locus (context, &rich_loc, DK_NOTE);
3796 /* Same as print_instantiation_full_context but less verbose. */
3798 static void
3799 print_instantiation_partial_context (diagnostic_context *context,
3800 struct tinst_level *t0, location_t loc)
3802 struct tinst_level *t;
3803 int n_total = 0;
3804 int n;
3805 location_t prev_loc = loc;
3807 for (t = t0; t != NULL; t = t->next)
3808 if (prev_loc != t->locus)
3810 prev_loc = t->locus;
3811 n_total++;
3814 t = t0;
3816 if (template_backtrace_limit
3817 && n_total > template_backtrace_limit)
3819 int skip = n_total - template_backtrace_limit;
3820 int head = template_backtrace_limit / 2;
3822 /* Avoid skipping just 1. If so, skip 2. */
3823 if (skip == 1)
3825 skip = 2;
3826 head = (template_backtrace_limit - 1) / 2;
3829 for (n = 0; n < head; n++)
3831 gcc_assert (t != NULL);
3832 if (loc != t->locus)
3833 print_instantiation_partial_context_line (context, t, loc,
3834 /*recursive_p=*/false);
3835 loc = t->locus;
3836 t = t->next;
3838 if (t != NULL && skip > 0)
3840 expanded_location xloc;
3841 xloc = expand_location (loc);
3842 if (context->m_show_column)
3843 pp_verbatim (context->printer,
3844 _("%r%s:%d:%d:%R [ skipping %d instantiation "
3845 "contexts, use -ftemplate-backtrace-limit=0 to "
3846 "disable ]\n"),
3847 "locus", xloc.file, xloc.line, xloc.column, skip);
3848 else
3849 pp_verbatim (context->printer,
3850 _("%r%s:%d:%R [ skipping %d instantiation "
3851 "contexts, use -ftemplate-backtrace-limit=0 to "
3852 "disable ]\n"),
3853 "locus", xloc.file, xloc.line, skip);
3855 do {
3856 loc = t->locus;
3857 t = t->next;
3858 } while (t != NULL && --skip > 0);
3862 while (t != NULL)
3864 while (t->next != NULL && t->locus == t->next->locus)
3866 loc = t->locus;
3867 t = t->next;
3869 print_instantiation_partial_context_line (context, t, loc,
3870 t->locus == loc);
3871 loc = t->locus;
3872 t = t->next;
3874 print_instantiation_partial_context_line (context, NULL, loc,
3875 /*recursive_p=*/false);
3878 /* Called from cp_thing to print the template context for an error. */
3879 static void
3880 maybe_print_instantiation_context (diagnostic_context *context)
3882 if (!problematic_instantiation_changed () || current_instantiation () == 0)
3883 return;
3885 record_last_problematic_instantiation ();
3886 print_instantiation_full_context (context);
3889 /* Report what constexpr call(s) we're trying to expand, if any. */
3891 void
3892 maybe_print_constexpr_context (diagnostic_context *context)
3894 vec<tree> call_stack = cx_error_context ();
3895 unsigned ix;
3896 tree t;
3898 FOR_EACH_VEC_ELT (call_stack, ix, t)
3900 expanded_location xloc = expand_location (EXPR_LOCATION (t));
3901 const char *s = expr_as_string (t, 0);
3902 if (context->m_show_column)
3903 pp_verbatim (context->printer,
3904 _("%r%s:%d:%d:%R in %<constexpr%> expansion of %qs"),
3905 "locus", xloc.file, xloc.line, xloc.column, s);
3906 else
3907 pp_verbatim (context->printer,
3908 _("%r%s:%d:%R in %<constexpr%> expansion of %qs"),
3909 "locus", xloc.file, xloc.line, s);
3910 pp_newline (context->printer);
3915 static void
3916 print_location (diagnostic_context *context, location_t loc)
3918 expanded_location xloc = expand_location (loc);
3919 if (context->m_show_column)
3920 pp_verbatim (context->printer, _("%r%s:%d:%d:%R "),
3921 "locus", xloc.file, xloc.line, xloc.column);
3922 else
3923 pp_verbatim (context->printer, _("%r%s:%d:%R "),
3924 "locus", xloc.file, xloc.line);
3927 static void
3928 print_constrained_decl_info (diagnostic_context *context, tree decl)
3930 print_location (context, DECL_SOURCE_LOCATION (decl));
3931 pp_verbatim (context->printer, "required by the constraints of %q#D\n", decl);
3934 static void
3935 print_concept_check_info (diagnostic_context *context, tree expr, tree map, tree args)
3937 gcc_assert (concept_check_p (expr));
3939 tree id = unpack_concept_check (expr);
3940 tree tmpl = TREE_OPERAND (id, 0);
3941 if (OVL_P (tmpl))
3942 tmpl = OVL_FIRST (tmpl);
3944 print_location (context, DECL_SOURCE_LOCATION (tmpl));
3946 cxx_pretty_printer *pp = (cxx_pretty_printer *)context->printer;
3947 pp_verbatim (pp, "required for the satisfaction of %qE", expr);
3948 if (map && map != error_mark_node)
3950 tree subst_map = tsubst_parameter_mapping (map, args, tf_none, NULL_TREE);
3951 pp_cxx_parameter_mapping (pp, (subst_map != error_mark_node
3952 ? subst_map : map));
3954 pp_newline (pp);
3957 /* Diagnose the entry point into the satisfaction error. Returns the next
3958 context, if any. */
3960 static tree
3961 print_constraint_context_head (diagnostic_context *context, tree cxt, tree args)
3963 tree src = TREE_VALUE (cxt);
3964 if (!src)
3966 print_location (context, input_location);
3967 pp_verbatim (context->printer, "required for constraint satisfaction\n");
3968 return NULL_TREE;
3970 if (DECL_P (src))
3972 print_constrained_decl_info (context, src);
3973 return NULL_TREE;
3975 else
3977 print_concept_check_info (context, src, TREE_PURPOSE (cxt), args);
3978 return TREE_CHAIN (cxt);
3982 static void
3983 print_requires_expression_info (diagnostic_context *context, tree constr, tree args)
3986 tree expr = ATOMIC_CONSTR_EXPR (constr);
3987 tree map = ATOMIC_CONSTR_MAP (constr);
3988 map = tsubst_parameter_mapping (map, args, tf_none, NULL_TREE);
3989 if (map == error_mark_node)
3990 return;
3992 print_location (context, cp_expr_loc_or_input_loc (expr));
3993 pp_verbatim (context->printer, "in requirements ");
3995 tree parms = TREE_OPERAND (expr, 0);
3996 if (parms)
3997 pp_verbatim (context->printer, "with ");
3998 while (parms)
4000 pp_verbatim (context->printer, "%q#D", parms);
4001 if (TREE_CHAIN (parms))
4002 pp_separate_with_comma ((cxx_pretty_printer *)context->printer);
4003 parms = TREE_CHAIN (parms);
4005 pp_cxx_parameter_mapping ((cxx_pretty_printer *)context->printer, map);
4007 pp_verbatim (context->printer, "\n");
4010 void
4011 maybe_print_single_constraint_context (diagnostic_context *context, tree failed)
4013 if (!failed)
4014 return;
4016 tree constr = TREE_VALUE (failed);
4017 if (!constr || constr == error_mark_node)
4018 return;
4019 tree cxt = CONSTR_CONTEXT (constr);
4020 if (!cxt)
4021 return;
4022 tree args = TREE_PURPOSE (failed);
4024 /* Print the stack of requirements. */
4025 cxt = print_constraint_context_head (context, cxt, args);
4026 while (cxt && !DECL_P (TREE_VALUE (cxt)))
4028 tree expr = TREE_VALUE (cxt);
4029 tree map = TREE_PURPOSE (cxt);
4030 print_concept_check_info (context, expr, map, args);
4031 cxt = TREE_CHAIN (cxt);
4034 /* For certain constraints, we can provide additional context. */
4035 if (TREE_CODE (constr) == ATOMIC_CONSTR
4036 && TREE_CODE (ATOMIC_CONSTR_EXPR (constr)) == REQUIRES_EXPR)
4037 print_requires_expression_info (context, constr, args);
4040 void
4041 maybe_print_constraint_context (diagnostic_context *context)
4043 if (!current_failed_constraint)
4044 return;
4046 tree cur = current_failed_constraint;
4048 /* Recursively print nested contexts. */
4049 current_failed_constraint = TREE_CHAIN (current_failed_constraint);
4050 if (current_failed_constraint)
4051 maybe_print_constraint_context (context);
4053 /* Print this context. */
4054 maybe_print_single_constraint_context (context, cur);
4057 /* Return true iff TYPE_A and TYPE_B are template types that are
4058 meaningful to compare. */
4060 static bool
4061 comparable_template_types_p (tree type_a, tree type_b)
4063 if (!CLASS_TYPE_P (type_a))
4064 return false;
4065 if (!CLASS_TYPE_P (type_b))
4066 return false;
4068 tree tinfo_a = TYPE_TEMPLATE_INFO (type_a);
4069 tree tinfo_b = TYPE_TEMPLATE_INFO (type_b);
4070 if (!tinfo_a || !tinfo_b)
4071 return false;
4073 return TI_TEMPLATE (tinfo_a) == TI_TEMPLATE (tinfo_b);
4076 /* Start a new line indented by SPC spaces on PP. */
4078 static void
4079 newline_and_indent (pretty_printer *pp, int spc)
4081 pp_newline (pp);
4082 for (int i = 0; i < spc; i++)
4083 pp_space (pp);
4086 /* Generate a GC-allocated string for ARG, an expression or type. */
4088 static const char *
4089 arg_to_string (tree arg, bool verbose)
4091 if (TYPE_P (arg))
4092 return type_to_string (arg, verbose, true, NULL, false);
4093 else
4094 return expr_to_string (arg);
4097 /* Subroutine to type_to_string_with_compare and
4098 print_template_tree_comparison.
4100 Print a representation of ARG (an expression or type) to PP,
4101 colorizing it as "type-diff" if PP->show_color. */
4103 static void
4104 print_nonequal_arg (pretty_printer *pp, tree arg, bool verbose)
4106 pp_printf (pp, "%r%s%R",
4107 "type-diff",
4108 (arg
4109 ? arg_to_string (arg, verbose)
4110 : G_("(no argument)")));
4113 /* Recursively print template TYPE_A to PP, as compared to template TYPE_B.
4115 The types must satisfy comparable_template_types_p.
4117 If INDENT is 0, then this is equivalent to type_to_string (TYPE_A), but
4118 potentially colorizing/eliding in comparison with TYPE_B.
4120 For example given types:
4121 vector<map<int,double>>
4123 vector<map<int,float>>
4124 then the result on PP would be:
4125 vector<map<[...],double>>
4126 with type elision, and:
4127 vector<map<int,double>>
4128 without type elision.
4130 In both cases the parts of TYPE that differ from PEER will be colorized
4131 if pp_show_color (pp) is true. In the above example, this would be
4132 "double".
4134 If INDENT is non-zero, then the types are printed in a tree-like form
4135 which shows both types. In the above example, the result on PP would be:
4137 vector<
4138 map<
4139 [...],
4140 [double != float]>>
4142 and without type-elision would be:
4144 vector<
4145 map<
4146 int,
4147 [double != float]>>
4149 As before, the differing parts of the types are colorized if
4150 pp_show_color (pp) is true ("double" and "float" in this example).
4152 Template arguments in which both types are using the default arguments
4153 are not printed; if at least one of the two types is using a non-default
4154 argument, then that argument is printed (or both arguments for the
4155 tree-like print format). */
4157 static void
4158 print_template_differences (pretty_printer *pp, tree type_a, tree type_b,
4159 bool verbose, int indent)
4161 if (indent)
4162 newline_and_indent (pp, indent);
4164 tree tinfo_a = TYPE_TEMPLATE_INFO (type_a);
4165 tree tinfo_b = TYPE_TEMPLATE_INFO (type_b);
4167 pp_printf (pp, "%s<",
4168 IDENTIFIER_POINTER (DECL_NAME (TI_TEMPLATE (tinfo_a))));
4170 tree args_a = TI_ARGS (tinfo_a);
4171 tree args_b = TI_ARGS (tinfo_b);
4172 gcc_assert (TREE_CODE (args_a) == TREE_VEC);
4173 gcc_assert (TREE_CODE (args_b) == TREE_VEC);
4174 int flags = 0;
4175 int len_a = get_non_default_template_args_count (args_a, flags);
4176 args_a = INNERMOST_TEMPLATE_ARGS (args_a);
4177 int len_b = get_non_default_template_args_count (args_b, flags);
4178 args_b = INNERMOST_TEMPLATE_ARGS (args_b);
4179 /* Determine the maximum range of args for which non-default template args
4180 were used; beyond this, only default args (if any) were used, and so
4181 they will be equal from this point onwards.
4182 One of the two peers might have used default arguments within this
4183 range, but the other will be using non-default arguments, and so
4184 it's more readable to print both within this range, to highlight
4185 the differences. */
4186 int len_max = MAX (len_a, len_b);
4187 gcc_assert (TREE_CODE (args_a) == TREE_VEC);
4188 gcc_assert (TREE_CODE (args_b) == TREE_VEC);
4189 for (int idx = 0; idx < len_max; idx++)
4191 if (idx)
4192 pp_character (pp, ',');
4194 tree arg_a = TREE_VEC_ELT (args_a, idx);
4195 tree arg_b = TREE_VEC_ELT (args_b, idx);
4196 if (arg_a == arg_b)
4198 if (indent)
4199 newline_and_indent (pp, indent + 2);
4200 /* Can do elision here, printing "[...]". */
4201 if (flag_elide_type)
4202 pp_string (pp, G_("[...]"));
4203 else
4204 pp_string (pp, arg_to_string (arg_a, verbose));
4206 else
4208 int new_indent = indent ? indent + 2 : 0;
4209 if (comparable_template_types_p (arg_a, arg_b))
4210 print_template_differences (pp, arg_a, arg_b, verbose, new_indent);
4211 else
4212 if (indent)
4214 newline_and_indent (pp, indent + 2);
4215 pp_character (pp, '[');
4216 print_nonequal_arg (pp, arg_a, verbose);
4217 pp_string (pp, " != ");
4218 print_nonequal_arg (pp, arg_b, verbose);
4219 pp_character (pp, ']');
4221 else
4222 print_nonequal_arg (pp, arg_a, verbose);
4225 pp_printf (pp, ">");
4228 /* As type_to_string, but for a template, potentially colorizing/eliding
4229 in comparison with PEER.
4230 For example, if TYPE is map<int,double> and PEER is map<int,int>,
4231 then the resulting string would be:
4232 map<[...],double>
4233 with type elision, and:
4234 map<int,double>
4235 without type elision.
4237 In both cases the parts of TYPE that differ from PEER will be colorized
4238 if SHOW_COLOR is true. In the above example, this would be "double".
4240 Template arguments in which both types are using the default arguments
4241 are not printed; if at least one of the two types is using a non-default
4242 argument, then both arguments are printed.
4244 The resulting string is in a GC-allocated buffer. */
4246 static const char *
4247 type_to_string_with_compare (tree type, tree peer, bool verbose,
4248 bool show_color)
4250 pretty_printer inner_pp;
4251 pretty_printer *pp = &inner_pp;
4252 pp_show_color (pp) = show_color;
4254 print_template_differences (pp, type, peer, verbose, 0);
4255 return pp_ggc_formatted_text (pp);
4258 /* Recursively print a tree-like comparison of TYPE_A and TYPE_B to PP,
4259 indented by INDENT spaces.
4261 For example given types:
4263 vector<map<int,double>>
4267 vector<map<double,float>>
4269 the output with type elision would be:
4271 vector<
4272 map<
4273 [...],
4274 [double != float]>>
4276 and without type-elision would be:
4278 vector<
4279 map<
4280 int,
4281 [double != float]>>
4283 TYPE_A and TYPE_B must both be comparable template types
4284 (as per comparable_template_types_p).
4286 Template arguments in which both types are using the default arguments
4287 are not printed; if at least one of the two types is using a non-default
4288 argument, then both arguments are printed. */
4290 static void
4291 print_template_tree_comparison (pretty_printer *pp, tree type_a, tree type_b,
4292 bool verbose, int indent)
4294 print_template_differences (pp, type_a, type_b, verbose, indent);
4297 /* Subroutine for use in a format_postprocessor::handle
4298 implementation. Adds a chunk to the end of
4299 formatted output, so that it will be printed
4300 by pp_output_formatted_text. */
4302 static void
4303 append_formatted_chunk (pretty_printer *pp, const char *content)
4305 output_buffer *buffer = pp_buffer (pp);
4306 struct chunk_info *chunk_array = buffer->cur_chunk_array;
4307 const char **args = chunk_array->args;
4309 unsigned int chunk_idx;
4310 for (chunk_idx = 0; args[chunk_idx]; chunk_idx++)
4312 args[chunk_idx++] = content;
4313 args[chunk_idx] = NULL;
4316 /* Create a copy of CONTENT, with quotes added, and,
4317 potentially, with colorization.
4318 No escaped is performed on CONTENT.
4319 The result is in a GC-allocated buffer. */
4321 static const char *
4322 add_quotes (const char *content, bool show_color)
4324 pretty_printer tmp_pp;
4325 pp_show_color (&tmp_pp) = show_color;
4327 /* We have to use "%<%s%>" rather than "%qs" here in order to avoid
4328 quoting colorization bytes within the results and using either
4329 pp_quote or pp_begin_quote doesn't work the same. */
4330 pp_printf (&tmp_pp, "%<%s%>", content);
4332 return pp_ggc_formatted_text (&tmp_pp);
4335 #if __GNUC__ >= 10
4336 #pragma GCC diagnostic pop
4337 #endif
4339 /* If we had %H and %I, and hence deferred printing them,
4340 print them now, storing the result into the chunk_info
4341 for pp_format. Quote them if 'q' was provided.
4342 Also print the difference in tree form, adding it as
4343 an additional chunk. */
4345 void
4346 cxx_format_postprocessor::handle (pretty_printer *pp)
4348 /* If we have one of %H and %I, the other should have
4349 been present. */
4350 if (m_type_a.m_tree || m_type_b.m_tree)
4352 /* Avoid reentrancy issues by working with a copy of
4353 m_type_a and m_type_b, resetting them now. */
4354 deferred_printed_type type_a = m_type_a;
4355 deferred_printed_type type_b = m_type_b;
4356 m_type_a = deferred_printed_type ();
4357 m_type_b = deferred_printed_type ();
4359 gcc_assert (type_a.m_buffer_ptr);
4360 gcc_assert (type_b.m_buffer_ptr);
4362 bool show_color = pp_show_color (pp);
4364 const char *type_a_text;
4365 const char *type_b_text;
4367 if (comparable_template_types_p (type_a.m_tree, type_b.m_tree))
4369 type_a_text
4370 = type_to_string_with_compare (type_a.m_tree, type_b.m_tree,
4371 type_a.m_verbose, show_color);
4372 type_b_text
4373 = type_to_string_with_compare (type_b.m_tree, type_a.m_tree,
4374 type_b.m_verbose, show_color);
4376 if (flag_diagnostics_show_template_tree)
4378 pretty_printer inner_pp;
4379 pp_show_color (&inner_pp) = pp_show_color (pp);
4380 print_template_tree_comparison
4381 (&inner_pp, type_a.m_tree, type_b.m_tree, type_a.m_verbose, 2);
4382 append_formatted_chunk (pp, pp_ggc_formatted_text (&inner_pp));
4385 else
4387 /* If the types were not comparable (or if only one of %H/%I was
4388 provided), they are printed normally, and no difference tree
4389 is printed. */
4390 type_a_text = type_to_string (type_a.m_tree, type_a.m_verbose,
4391 true, &type_a.m_quote, show_color);
4392 type_b_text = type_to_string (type_b.m_tree, type_b.m_verbose,
4393 true, &type_b.m_quote, show_color);
4396 if (type_a.m_quote)
4397 type_a_text = add_quotes (type_a_text, show_color);
4398 *type_a.m_buffer_ptr = type_a_text;
4400 if (type_b.m_quote)
4401 type_b_text = add_quotes (type_b_text, show_color);
4402 *type_b.m_buffer_ptr = type_b_text;
4406 /* Subroutine for handling %H and %I, to support i18n of messages like:
4408 error_at (loc, "could not convert %qE from %qH to %qI",
4409 expr, type_a, type_b);
4411 so that we can print things like:
4413 could not convert 'foo' from 'map<int,double>' to 'map<int,int>'
4415 and, with type-elision:
4417 could not convert 'foo' from 'map<[...],double>' to 'map<[...],int>'
4419 (with color-coding of the differences between the types).
4421 The %H and %I format codes are peers: both must be present,
4422 and they affect each other. Hence to handle them, we must
4423 delay printing until we have both, deferring the printing to
4424 pretty_printer's m_format_postprocessor hook.
4426 This is called in phase 2 of pp_format, when it is accumulating
4427 a series of formatted chunks. We stash the location of the chunk
4428 we're meant to have written to, so that we can write to it in the
4429 m_format_postprocessor hook.
4431 We also need to stash whether a 'q' prefix was provided (the QUOTE
4432 param) so that we can add the quotes when writing out the delayed
4433 chunk. */
4435 static void
4436 defer_phase_2_of_type_diff (deferred_printed_type *deferred,
4437 tree type, const char **buffer_ptr,
4438 bool verbose, bool quote)
4440 gcc_assert (deferred->m_tree == NULL_TREE);
4441 gcc_assert (deferred->m_buffer_ptr == NULL);
4442 *deferred = deferred_printed_type (type, buffer_ptr, verbose, quote);
4446 /* Called from output_format -- during diagnostic message processing --
4447 to handle C++ specific format specifier with the following meanings:
4448 %A function argument-list.
4449 %C tree code.
4450 %D declaration.
4451 %E expression.
4452 %F function declaration.
4453 %H type difference (from).
4454 %I type difference (to).
4455 %L language as used in extern "lang".
4456 %O binary operator.
4457 %P function parameter whose position is indicated by an integer.
4458 %Q assignment operator.
4459 %S substitution (template + args)
4460 %T type.
4461 %V cv-qualifier.
4462 %X exception-specification. */
4463 static bool
4464 cp_printer (pretty_printer *pp, text_info *text, const char *spec,
4465 int precision, bool wide, bool set_locus, bool verbose,
4466 bool *quoted, const char **buffer_ptr)
4468 gcc_assert (pp->m_format_postprocessor);
4469 cxx_format_postprocessor *postprocessor
4470 = static_cast <cxx_format_postprocessor *> (pp->m_format_postprocessor);
4472 const char *result;
4473 tree t = NULL;
4474 #define next_tree (t = va_arg (*text->m_args_ptr, tree))
4475 #define next_tcode ((enum tree_code) va_arg (*text->m_args_ptr, int))
4476 #define next_lang ((enum languages) va_arg (*text->m_args_ptr, int))
4477 #define next_int va_arg (*text->m_args_ptr, int)
4479 if (precision != 0 || wide)
4480 return false;
4482 switch (*spec)
4484 case 'A': result = args_to_string (next_tree, verbose); break;
4485 case 'C': result = code_to_string (next_tcode); break;
4486 case 'D':
4488 tree temp = next_tree;
4489 if (VAR_P (temp)
4490 && DECL_HAS_DEBUG_EXPR_P (temp))
4492 temp = DECL_DEBUG_EXPR (temp);
4493 if (!DECL_P (temp))
4495 result = expr_to_string (temp);
4496 break;
4499 result = decl_to_string (temp, verbose, pp_show_color (pp));
4501 break;
4502 case 'E': result = expr_to_string (next_tree); break;
4503 case 'F': result = fndecl_to_string (next_tree, verbose); break;
4504 case 'H':
4505 defer_phase_2_of_type_diff (&postprocessor->m_type_a, next_tree,
4506 buffer_ptr, verbose, *quoted);
4507 return true;
4508 case 'I':
4509 defer_phase_2_of_type_diff (&postprocessor->m_type_b, next_tree,
4510 buffer_ptr, verbose, *quoted);
4511 return true;
4512 case 'L': result = language_to_string (next_lang); break;
4513 case 'O': result = op_to_string (false, next_tcode); break;
4514 case 'P': result = parm_to_string (next_int); break;
4515 case 'Q': result = op_to_string (true, next_tcode); break;
4516 case 'S': result = subst_to_string (next_tree, pp_show_color (pp)); break;
4517 case 'T':
4519 result = type_to_string (next_tree, verbose, false, quoted,
4520 pp_show_color (pp));
4522 break;
4523 case 'V': result = cv_to_string (next_tree, verbose); break;
4524 case 'X': result = eh_spec_to_string (next_tree, verbose); break;
4526 default:
4527 return false;
4530 pp_string (pp, result);
4531 if (set_locus && t != NULL)
4532 text->set_location (0, location_of (t), SHOW_RANGE_WITH_CARET);
4533 return true;
4534 #undef next_tree
4535 #undef next_tcode
4536 #undef next_lang
4537 #undef next_int
4540 /* Warn about the use of C++0x features when appropriate. */
4541 void
4542 maybe_warn_cpp0x (cpp0x_warn_str str, location_t loc/*=input_location*/)
4544 if (cxx_dialect == cxx98)
4545 switch (str)
4547 case CPP0X_INITIALIZER_LISTS:
4548 pedwarn (loc, OPT_Wc__11_extensions,
4549 "extended initializer lists "
4550 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4551 break;
4552 case CPP0X_EXPLICIT_CONVERSION:
4553 pedwarn (loc, OPT_Wc__11_extensions,
4554 "explicit conversion operators "
4555 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4556 break;
4557 case CPP0X_VARIADIC_TEMPLATES:
4558 pedwarn (loc, OPT_Wc__11_extensions,
4559 "variadic templates "
4560 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4561 break;
4562 case CPP0X_LAMBDA_EXPR:
4563 pedwarn (loc, OPT_Wc__11_extensions,
4564 "lambda expressions "
4565 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4566 break;
4567 case CPP0X_AUTO:
4568 pedwarn (loc, OPT_Wc__11_extensions,
4569 "C++11 auto only available with %<-std=c++11%> or "
4570 "%<-std=gnu++11%>");
4571 break;
4572 case CPP0X_SCOPED_ENUMS:
4573 pedwarn (loc, OPT_Wc__11_extensions,
4574 "scoped enums only available with %<-std=c++11%> or "
4575 "%<-std=gnu++11%>");
4576 break;
4577 case CPP0X_DEFAULTED_DELETED:
4578 pedwarn (loc, OPT_Wc__11_extensions,
4579 "defaulted and deleted functions "
4580 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4581 break;
4582 case CPP0X_INLINE_NAMESPACES:
4583 if (pedantic)
4584 pedwarn (loc, OPT_Wc__11_extensions,
4585 "inline namespaces "
4586 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4587 break;
4588 case CPP0X_OVERRIDE_CONTROLS:
4589 pedwarn (loc, OPT_Wc__11_extensions,
4590 "override controls (override/final) "
4591 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4592 break;
4593 case CPP0X_NSDMI:
4594 pedwarn (loc, OPT_Wc__11_extensions,
4595 "non-static data member initializers "
4596 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4597 break;
4598 case CPP0X_USER_DEFINED_LITERALS:
4599 pedwarn (loc, OPT_Wc__11_extensions,
4600 "user-defined literals "
4601 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4602 break;
4603 case CPP0X_DELEGATING_CTORS:
4604 pedwarn (loc, OPT_Wc__11_extensions,
4605 "delegating constructors "
4606 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4607 break;
4608 case CPP0X_INHERITING_CTORS:
4609 pedwarn (loc, OPT_Wc__11_extensions,
4610 "inheriting constructors "
4611 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4612 break;
4613 case CPP0X_ATTRIBUTES:
4614 pedwarn (loc, OPT_Wc__11_extensions,
4615 "C++11 attributes "
4616 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4617 break;
4618 case CPP0X_REF_QUALIFIER:
4619 pedwarn (loc, OPT_Wc__11_extensions,
4620 "ref-qualifiers "
4621 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4622 break;
4623 default:
4624 gcc_unreachable ();
4628 /* Warn about the use of variadic templates when appropriate. */
4629 void
4630 maybe_warn_variadic_templates (void)
4632 maybe_warn_cpp0x (CPP0X_VARIADIC_TEMPLATES);
4636 /* Issue an ISO C++98 pedantic warning at LOCATION, conditional on
4637 option OPT with text GMSGID. Use this function to report
4638 diagnostics for constructs that are invalid C++98, but valid
4639 C++0x. */
4640 bool
4641 pedwarn_cxx98 (location_t location, int opt, const char *gmsgid, ...)
4643 diagnostic_info diagnostic;
4644 va_list ap;
4645 bool ret;
4646 rich_location richloc (line_table, location);
4648 va_start (ap, gmsgid);
4649 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
4650 (cxx_dialect == cxx98) ? DK_PEDWARN : DK_WARNING);
4651 diagnostic.option_index = opt;
4652 ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
4653 va_end (ap);
4654 return ret;
4657 /* Issue a diagnostic that NAME cannot be found in SCOPE. DECL is what
4658 we found when we tried to do the lookup. LOCATION is the location of
4659 the NAME identifier. */
4661 void
4662 qualified_name_lookup_error (tree scope, tree name,
4663 tree decl, location_t location)
4665 if (scope == error_mark_node)
4666 ; /* We already complained. */
4667 else if (TYPE_P (scope))
4669 if (!COMPLETE_TYPE_P (scope))
4670 error_at (location, "incomplete type %qT used in nested name specifier",
4671 scope);
4672 else if (TREE_CODE (decl) == TREE_LIST)
4674 error_at (location, "reference to %<%T::%D%> is ambiguous",
4675 scope, name);
4676 print_candidates (decl);
4678 else
4680 name_hint hint;
4681 if (SCOPED_ENUM_P (scope) && TREE_CODE (name) == IDENTIFIER_NODE)
4682 hint = suggest_alternative_in_scoped_enum (name, scope);
4683 if (const char *suggestion = hint.suggestion ())
4685 gcc_rich_location richloc (location);
4686 richloc.add_fixit_replace (suggestion);
4687 error_at (&richloc,
4688 "%qD is not a member of %qT; did you mean %qs?",
4689 name, scope, suggestion);
4691 else
4692 error_at (location, "%qD is not a member of %qT", name, scope);
4695 else if (scope != global_namespace)
4697 auto_diagnostic_group d;
4698 bool emit_fixit = true;
4699 name_hint hint
4700 = suggest_alternative_in_explicit_scope (location, name, scope);
4701 if (!hint)
4703 hint = suggest_alternatives_in_other_namespaces (location, name);
4704 /* "location" is just the location of the name, not of the explicit
4705 scope, and it's not easy to get at the latter, so we can't issue
4706 fix-it hints for the suggestion. */
4707 emit_fixit = false;
4709 if (const char *suggestion = hint.suggestion ())
4711 gcc_rich_location richloc (location);
4712 if (emit_fixit)
4713 richloc.add_fixit_replace (suggestion);
4714 error_at (&richloc, "%qD is not a member of %qD; did you mean %qs?",
4715 name, scope, suggestion);
4717 else
4718 error_at (location, "%qD is not a member of %qD", name, scope);
4720 else
4722 auto_diagnostic_group d;
4723 name_hint hint = suggest_alternatives_for (location, name, true);
4724 if (const char *suggestion = hint.suggestion ())
4726 gcc_rich_location richloc (location);
4727 richloc.add_fixit_replace (suggestion);
4728 error_at (&richloc,
4729 "%<::%D%> has not been declared; did you mean %qs?",
4730 name, suggestion);
4732 else
4733 error_at (location, "%<::%D%> has not been declared", name);
4737 /* C++-specific implementation of range_label::get_text () vfunc for
4738 range_label_for_type_mismatch.
4740 Compare with print_template_differences above. */
4742 label_text
4743 range_label_for_type_mismatch::get_text (unsigned /*range_idx*/) const
4745 if (m_labelled_type == NULL_TREE)
4746 return label_text::borrow (NULL);
4748 const bool verbose = false;
4749 const bool show_color = false;
4751 const char *result;
4752 if (m_other_type
4753 && comparable_template_types_p (m_labelled_type, m_other_type))
4754 result = type_to_string_with_compare (m_labelled_type, m_other_type,
4755 verbose, show_color);
4756 else
4757 result = type_to_string (m_labelled_type, verbose, true, NULL, show_color);
4759 /* Both of the above return GC-allocated buffers, so the caller mustn't
4760 free them. */
4761 return label_text::borrow (result);