Require target lra in gcc.c-torture/compile/asmgoto-6.c
[official-gcc.git] / gcc / cp / error.cc
blob8a5219a68a1931af62fbda1a2ccfe6d80a953a28
1 /* Call-backs for C++ error reporting.
2 This code is non-reentrant.
3 Copyright (C) 1993-2023 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 *, diagnostic_info *);
105 static void cp_print_error_function (diagnostic_context *, diagnostic_info *);
107 static bool cp_printer (pretty_printer *, text_info *, const char *,
108 int, bool, bool, bool, bool *, const char **);
110 /* Struct for handling %H or %I, which require delaying printing the
111 type until a postprocessing stage. */
113 class deferred_printed_type
115 public:
116 deferred_printed_type ()
117 : m_tree (NULL_TREE), m_buffer_ptr (NULL), m_verbose (false), m_quote (false)
120 deferred_printed_type (tree type, const char **buffer_ptr, bool verbose,
121 bool quote)
122 : m_tree (type), m_buffer_ptr (buffer_ptr), m_verbose (verbose),
123 m_quote (quote)
125 gcc_assert (type);
126 gcc_assert (buffer_ptr);
129 /* The tree is not GTY-marked: they are only non-NULL within a
130 call to pp_format. */
131 tree m_tree;
132 const char **m_buffer_ptr;
133 bool m_verbose;
134 bool m_quote;
137 /* Subclass of format_postprocessor for the C++ frontend.
138 This handles the %H and %I formatting codes, printing them
139 in a postprocessing phase (since they affect each other). */
141 class cxx_format_postprocessor : public format_postprocessor
143 public:
144 cxx_format_postprocessor ()
145 : m_type_a (), m_type_b ()
148 format_postprocessor *clone() const final override
150 return new cxx_format_postprocessor ();
153 void handle (pretty_printer *pp) final override;
155 deferred_printed_type m_type_a;
156 deferred_printed_type m_type_b;
159 /* CONTEXT->printer is a basic pretty printer that was constructed
160 presumably by diagnostic_initialize(), called early in the
161 compiler's initialization process (in general_init) Before the FE
162 is initialized. This (C++) FE-specific diagnostic initializer is
163 thus replacing the basic pretty printer with one that has C++-aware
164 capacities. */
166 void
167 cxx_initialize_diagnostics (diagnostic_context *context)
169 pretty_printer *base = context->printer;
170 cxx_pretty_printer *pp = XNEW (cxx_pretty_printer);
171 context->printer = new (pp) cxx_pretty_printer ();
173 /* It is safe to free this object because it was previously XNEW()'d. */
174 base->~pretty_printer ();
175 XDELETE (base);
177 c_common_diagnostics_set_defaults (context);
178 diagnostic_starter (context) = cp_diagnostic_starter;
179 /* diagnostic_finalizer is already c_diagnostic_finalizer. */
180 diagnostic_format_decoder (context) = cp_printer;
181 pp->m_format_postprocessor = new cxx_format_postprocessor ();
184 /* Dump an '@module' name suffix for DECL, if any. */
186 static void
187 dump_module_suffix (cxx_pretty_printer *pp, tree decl)
189 if (!modules_p ())
190 return;
192 if (!DECL_CONTEXT (decl))
193 return;
195 if (TREE_CODE (decl) != CONST_DECL
196 || !UNSCOPED_ENUM_P (DECL_CONTEXT (decl)))
198 if (!DECL_NAMESPACE_SCOPE_P (decl))
199 return;
201 if (TREE_CODE (decl) == NAMESPACE_DECL
202 && !DECL_NAMESPACE_ALIAS (decl)
203 && (TREE_PUBLIC (decl) || !TREE_PUBLIC (CP_DECL_CONTEXT (decl))))
204 return;
207 if (unsigned m = get_originating_module (decl))
208 if (const char *n = module_name (m, false))
210 pp_character (pp, '@');
211 pp->padding = pp_none;
212 pp_string (pp, n);
216 /* The scope of the declaration we're currently printing, to avoid redundantly
217 dumping the same scope on parameter types. */
218 static tree current_dump_scope;
220 /* Dump a scope, if deemed necessary. */
222 static void
223 dump_scope (cxx_pretty_printer *pp, tree scope, int flags)
225 int f = flags & (TFF_SCOPE | TFF_CHASE_TYPEDEF);
227 if (scope == NULL_TREE || scope == current_dump_scope)
228 return;
230 /* Enum values within an unscoped enum will be CONST_DECL with an
231 ENUMERAL_TYPE as their "scope". Use CP_TYPE_CONTEXT of the
232 ENUMERAL_TYPE, so as to print any enclosing namespace. */
233 if (UNSCOPED_ENUM_P (scope))
234 scope = CP_TYPE_CONTEXT (scope);
236 if (TREE_CODE (scope) == NAMESPACE_DECL)
238 if (scope != global_namespace)
240 dump_decl (pp, scope, f);
241 pp_cxx_colon_colon (pp);
244 else if (AGGREGATE_TYPE_P (scope)
245 || SCOPED_ENUM_P (scope))
247 dump_type (pp, scope, f);
248 pp_cxx_colon_colon (pp);
250 else if ((flags & TFF_SCOPE) && TREE_CODE (scope) == FUNCTION_DECL)
252 dump_function_decl (pp, scope, f | TFF_NO_TEMPLATE_BINDINGS);
253 pp_cxx_colon_colon (pp);
257 /* Dump the template ARGument under control of FLAGS. */
259 static void
260 dump_template_argument (cxx_pretty_printer *pp, tree arg, int flags)
262 if (ARGUMENT_PACK_P (arg))
263 dump_template_argument_list (pp, ARGUMENT_PACK_ARGS (arg),
264 /* No default args in argument packs. */
265 flags|TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS);
266 else if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
267 dump_type (pp, arg, flags & ~TFF_CLASS_KEY_OR_ENUM);
268 else
270 if (TREE_CODE (arg) == TREE_LIST)
271 arg = TREE_VALUE (arg);
273 /* Strip implicit conversions. */
274 while (CONVERT_EXPR_P (arg))
275 arg = TREE_OPERAND (arg, 0);
277 dump_expr (pp, arg, (flags | TFF_EXPR_IN_PARENS) & ~TFF_CLASS_KEY_OR_ENUM);
281 /* Count the number of template arguments ARGS whose value does not
282 match the (optional) default template parameter in PARAMS */
284 static int
285 get_non_default_template_args_count (tree args, int flags)
287 int n = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_ARGS (args));
289 if (/* We use this flag when generating debug information. We don't
290 want to expand templates at this point, for this may generate
291 new decls, which gets decl counts out of sync, which may in
292 turn cause codegen differences between compilations with and
293 without -g. */
294 (flags & TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS) != 0
295 || !flag_pretty_templates)
296 return n;
298 return GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (INNERMOST_TEMPLATE_ARGS (args));
301 /* Dump a template-argument-list ARGS (always a TREE_VEC) under control
302 of FLAGS. */
304 static void
305 dump_template_argument_list (cxx_pretty_printer *pp, tree args, int flags)
307 int n = get_non_default_template_args_count (args, flags);
308 int need_comma = 0;
309 int i;
311 for (i = 0; i < n; ++i)
313 tree arg = TREE_VEC_ELT (args, i);
315 /* Only print a comma if we know there is an argument coming. In
316 the case of an empty template argument pack, no actual
317 argument will be printed. */
318 if (need_comma
319 && (!ARGUMENT_PACK_P (arg)
320 || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) > 0))
321 pp_separate_with_comma (pp);
323 dump_template_argument (pp, arg, flags);
324 need_comma = 1;
328 /* Dump a template parameter PARM (a TREE_LIST) under control of FLAGS. */
330 static void
331 dump_template_parameter (cxx_pretty_printer *pp, tree parm, int flags)
333 tree p;
334 tree a;
336 if (parm == error_mark_node)
337 return;
339 p = TREE_VALUE (parm);
340 a = TREE_PURPOSE (parm);
342 if (TREE_CODE (p) == TYPE_DECL)
344 if (flags & TFF_DECL_SPECIFIERS)
346 pp_cxx_ws_string (pp, "class");
347 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (p)))
348 pp_cxx_ws_string (pp, "...");
349 if (DECL_NAME (p))
350 pp_cxx_tree_identifier (pp, DECL_NAME (p));
352 else if (DECL_NAME (p))
353 pp_cxx_tree_identifier (pp, DECL_NAME (p));
354 else
355 pp_cxx_canonical_template_parameter (pp, TREE_TYPE (p));
357 else
358 dump_decl (pp, p, flags | TFF_DECL_SPECIFIERS);
360 if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && a != NULL_TREE)
362 pp_cxx_whitespace (pp);
363 pp_equal (pp);
364 pp_cxx_whitespace (pp);
365 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
366 dump_type (pp, a, flags & ~TFF_CHASE_TYPEDEF);
367 else
368 dump_expr (pp, a, flags | TFF_EXPR_IN_PARENS);
372 /* Dump, under control of FLAGS, a template-parameter-list binding.
373 PARMS is a TREE_LIST of TREE_VEC of TREE_LIST and ARGS is a
374 TREE_VEC. */
376 static void
377 dump_template_bindings (cxx_pretty_printer *pp, tree parms, tree args,
378 vec<tree, va_gc> *typenames)
380 /* Print "[with" and ']', conditional on whether anything is printed at all.
381 This is tied to whether a semicolon is needed to separate multiple template
382 parameters. */
383 struct prepost_semicolon
385 cxx_pretty_printer *pp;
386 bool need_semicolon;
388 void operator() ()
390 if (need_semicolon)
391 pp_separate_with_semicolon (pp);
392 else
394 pp_cxx_whitespace (pp);
395 pp_string (pp, colorize_start (pp_show_color (pp), "targs"));
396 pp_cxx_left_bracket (pp);
397 pp->translate_string ("with");
398 pp_cxx_whitespace (pp);
399 need_semicolon = true;
403 ~prepost_semicolon ()
405 if (need_semicolon)
407 pp_cxx_right_bracket (pp);
408 pp_string (pp, colorize_stop (pp_show_color (pp)));
411 } semicolon_or_introducer = {pp, false};
413 int i;
414 tree t;
416 while (parms)
418 tree p = TREE_VALUE (parms);
419 int lvl = TMPL_PARMS_DEPTH (parms);
420 int arg_idx = 0;
421 int i;
422 tree lvl_args = NULL_TREE;
424 /* Don't crash if we had an invalid argument list. */
425 if (TMPL_ARGS_DEPTH (args) >= lvl)
426 lvl_args = TMPL_ARGS_LEVEL (args, lvl);
428 for (i = 0; i < TREE_VEC_LENGTH (p); ++i)
430 tree arg = NULL_TREE;
432 /* Don't crash if we had an invalid argument list. */
433 if (lvl_args && NUM_TMPL_ARGS (lvl_args) > arg_idx)
434 arg = TREE_VEC_ELT (lvl_args, arg_idx);
436 tree parm_i = TREE_VEC_ELT (p, i);
437 /* If the template argument repeats the template parameter (T = T),
438 skip the parameter.*/
439 if (arg && TREE_CODE (arg) == TEMPLATE_TYPE_PARM
440 && TREE_CODE (parm_i) == TREE_LIST
441 && TREE_CODE (TREE_VALUE (parm_i)) == TYPE_DECL
442 && TREE_CODE (TREE_TYPE (TREE_VALUE (parm_i)))
443 == TEMPLATE_TYPE_PARM
444 && DECL_NAME (TREE_VALUE (parm_i))
445 == DECL_NAME (TREE_CHAIN (arg)))
446 continue;
448 semicolon_or_introducer ();
449 dump_template_parameter (pp, parm_i, TFF_PLAIN_IDENTIFIER);
450 pp_cxx_whitespace (pp);
451 pp_equal (pp);
452 pp_cxx_whitespace (pp);
453 if (arg)
455 if (ARGUMENT_PACK_P (arg))
456 pp_cxx_left_brace (pp);
457 dump_template_argument (pp, arg, TFF_PLAIN_IDENTIFIER);
458 if (ARGUMENT_PACK_P (arg))
459 pp_cxx_right_brace (pp);
461 else
462 pp_string (pp, M_("<missing>"));
464 ++arg_idx;
467 parms = TREE_CHAIN (parms);
470 /* Don't bother with typenames for a partial instantiation. */
471 if (vec_safe_is_empty (typenames) || uses_template_parms (args))
472 return;
474 /* Don't try to print typenames when we're processing a clone. */
475 if (current_function_decl
476 && !DECL_LANG_SPECIFIC (current_function_decl))
477 return;
479 /* Don't try to do this once cgraph starts throwing away front-end
480 information. */
481 if (at_eof >= 2)
482 return;
484 FOR_EACH_VEC_SAFE_ELT (typenames, i, t)
486 semicolon_or_introducer ();
487 dump_type (pp, t, TFF_PLAIN_IDENTIFIER);
488 pp_cxx_whitespace (pp);
489 pp_equal (pp);
490 pp_cxx_whitespace (pp);
491 push_deferring_access_checks (dk_no_check);
492 t = tsubst (t, args, tf_none, NULL_TREE);
493 pop_deferring_access_checks ();
494 /* Strip typedefs. We can't just use TFF_CHASE_TYPEDEF because
495 pp_simple_type_specifier doesn't know about it. */
496 t = strip_typedefs (t, NULL, STF_USER_VISIBLE);
497 dump_type (pp, t, TFF_PLAIN_IDENTIFIER);
501 /* Dump a human-readable equivalent of the alias template
502 specialization of T. */
504 static void
505 dump_alias_template_specialization (cxx_pretty_printer *pp, tree t, int flags)
507 gcc_assert (alias_template_specialization_p (t, nt_opaque));
509 tree decl = TYPE_NAME (t);
510 if (!(flags & TFF_UNQUALIFIED_NAME))
511 dump_scope (pp, CP_DECL_CONTEXT (decl), flags);
512 pp_cxx_tree_identifier (pp, DECL_NAME (decl));
513 dump_template_parms (pp, DECL_TEMPLATE_INFO (decl),
514 /*primary=*/false,
515 flags & ~TFF_TEMPLATE_HEADER);
518 /* Dump a human-readable equivalent of TYPE. FLAGS controls the
519 format. */
521 static void
522 dump_type (cxx_pretty_printer *pp, tree t, int flags)
524 if (t == NULL_TREE)
525 return;
527 /* Don't print e.g. "struct mytypedef". */
528 if (TYPE_P (t) && typedef_variant_p (t))
530 tree decl = TYPE_NAME (t);
531 if ((flags & TFF_CHASE_TYPEDEF)
532 || DECL_SELF_REFERENCE_P (decl)
533 || (!flag_pretty_templates
534 && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)))
536 unsigned int stf_flags = (!(pp->flags & pp_c_flag_gnu_v3)
537 ? STF_USER_VISIBLE : 0);
538 t = strip_typedefs (t, NULL, stf_flags);
540 else if (alias_template_specialization_p (t, nt_opaque))
542 dump_alias_template_specialization (pp, t, flags);
543 return;
545 else if (same_type_p (t, TREE_TYPE (decl)))
546 t = decl;
547 else
549 pp_cxx_cv_qualifier_seq (pp, t);
550 if (! (flags & TFF_UNQUALIFIED_NAME))
551 dump_scope (pp, CP_DECL_CONTEXT (decl), flags);
552 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
553 return;
557 if (TYPE_PTRMEMFUNC_P (t))
558 goto offset_type;
560 switch (TREE_CODE (t))
562 case LANG_TYPE:
563 if (t == init_list_type_node)
564 pp_string (pp, M_("<brace-enclosed initializer list>"));
565 else if (t == unknown_type_node)
566 pp_string (pp, M_("<unresolved overloaded function type>"));
567 else
569 pp_cxx_cv_qualifier_seq (pp, t);
570 if (tree id = TYPE_IDENTIFIER (t))
571 pp_cxx_tree_identifier (pp, id);
573 break;
575 case TREE_LIST:
576 /* A list of function parms. */
577 dump_parameters (pp, t, flags);
578 break;
580 case IDENTIFIER_NODE:
581 pp_cxx_tree_identifier (pp, t);
582 break;
584 case TREE_BINFO:
585 dump_type (pp, BINFO_TYPE (t), flags);
586 break;
588 case RECORD_TYPE:
589 case UNION_TYPE:
590 case ENUMERAL_TYPE:
591 dump_aggr_type (pp, t, flags);
592 break;
594 case TYPE_DECL:
595 if (flags & TFF_CHASE_TYPEDEF)
597 dump_type (pp, DECL_ORIGINAL_TYPE (t)
598 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t), flags);
599 break;
601 /* Fall through. */
603 case TEMPLATE_DECL:
604 case NAMESPACE_DECL:
605 dump_decl (pp, t, flags & ~TFF_DECL_SPECIFIERS);
606 break;
608 case INTEGER_TYPE:
609 case REAL_TYPE:
610 case VOID_TYPE:
611 case OPAQUE_TYPE:
612 case BOOLEAN_TYPE:
613 case COMPLEX_TYPE:
614 case VECTOR_TYPE:
615 case FIXED_POINT_TYPE:
616 pp_type_specifier_seq (pp, t);
617 break;
619 case TEMPLATE_TEMPLATE_PARM:
620 /* For parameters inside template signature. */
621 if (TYPE_IDENTIFIER (t))
622 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
623 else
624 pp_cxx_canonical_template_parameter (pp, t);
625 break;
627 case BOUND_TEMPLATE_TEMPLATE_PARM:
629 tree args = TYPE_TI_ARGS (t);
630 pp_cxx_cv_qualifier_seq (pp, t);
631 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
632 pp_cxx_begin_template_argument_list (pp);
633 dump_template_argument_list (pp, args, flags);
634 pp_cxx_end_template_argument_list (pp);
636 break;
638 case TEMPLATE_TYPE_PARM:
639 pp_cxx_cv_qualifier_seq (pp, t);
640 if (template_placeholder_p (t))
642 tree tmpl = TREE_TYPE (CLASS_PLACEHOLDER_TEMPLATE (t));
643 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (tmpl));
644 pp_string (pp, "<...auto...>");
646 else if (TYPE_IDENTIFIER (t))
647 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
648 else
649 pp_cxx_canonical_template_parameter
650 (pp, TEMPLATE_TYPE_PARM_INDEX (t));
651 /* If this is a constrained placeholder, add the requirements. */
652 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
653 pp_cxx_constrained_type_spec (pp, c);
654 break;
656 /* This is not always necessary for pointers and such, but doing this
657 reduces code size. */
658 case ARRAY_TYPE:
659 case POINTER_TYPE:
660 case REFERENCE_TYPE:
661 case OFFSET_TYPE:
662 offset_type:
663 case FUNCTION_TYPE:
664 case METHOD_TYPE:
666 dump_type_prefix (pp, t, flags);
667 dump_type_suffix (pp, t, flags);
668 break;
670 case TYPENAME_TYPE:
671 if (! (flags & TFF_CHASE_TYPEDEF)
672 && DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
674 dump_decl (pp, TYPE_NAME (t), TFF_PLAIN_IDENTIFIER);
675 break;
677 pp_cxx_cv_qualifier_seq (pp, t);
678 pp_cxx_ws_string (pp,
679 TYPENAME_IS_ENUM_P (t) ? "enum"
680 : TYPENAME_IS_CLASS_P (t) ? "class"
681 : "typename");
682 dump_typename (pp, t, flags);
683 break;
685 case UNBOUND_CLASS_TEMPLATE:
686 if (! (flags & TFF_UNQUALIFIED_NAME))
688 dump_type (pp, TYPE_CONTEXT (t), flags);
689 pp_cxx_colon_colon (pp);
691 pp_cxx_ws_string (pp, "template");
692 dump_type (pp, TYPE_IDENTIFIER (t), flags);
693 break;
695 case TYPEOF_TYPE:
696 pp_cxx_ws_string (pp, "__typeof__");
697 pp_cxx_whitespace (pp);
698 pp_cxx_left_paren (pp);
699 dump_expr (pp, TYPEOF_TYPE_EXPR (t), flags & ~TFF_EXPR_IN_PARENS);
700 pp_cxx_right_paren (pp);
701 break;
703 case TRAIT_TYPE:
704 pp_cxx_trait (pp, t);
705 break;
707 case TYPE_PACK_EXPANSION:
708 dump_type (pp, PACK_EXPANSION_PATTERN (t), flags);
709 pp_cxx_ws_string (pp, "...");
710 break;
712 case TYPE_ARGUMENT_PACK:
713 dump_template_argument (pp, t, flags);
714 break;
716 case DECLTYPE_TYPE:
717 pp_cxx_ws_string (pp, "decltype");
718 pp_cxx_whitespace (pp);
719 pp_cxx_left_paren (pp);
720 dump_expr (pp, DECLTYPE_TYPE_EXPR (t), flags & ~TFF_EXPR_IN_PARENS);
721 pp_cxx_right_paren (pp);
722 break;
724 case NULLPTR_TYPE:
725 pp_string (pp, "std::nullptr_t");
726 break;
728 default:
729 pp_unsupported_tree (pp, t);
730 /* Fall through. */
732 case ERROR_MARK:
733 pp_string (pp, M_("<type error>"));
734 break;
738 /* Dump a TYPENAME_TYPE. We need to notice when the context is itself
739 a TYPENAME_TYPE. */
741 static void
742 dump_typename (cxx_pretty_printer *pp, tree t, int flags)
744 tree ctx = TYPE_CONTEXT (t);
746 if (TREE_CODE (ctx) == TYPENAME_TYPE)
747 dump_typename (pp, ctx, flags);
748 else
749 dump_type (pp, ctx, flags & ~TFF_CLASS_KEY_OR_ENUM);
750 pp_cxx_colon_colon (pp);
751 dump_decl (pp, TYPENAME_TYPE_FULLNAME (t), flags);
754 /* Return the name of the supplied aggregate, or enumeral type. */
756 const char *
757 class_key_or_enum_as_string (tree t)
759 if (TREE_CODE (t) == ENUMERAL_TYPE)
761 if (SCOPED_ENUM_P (t))
762 return "enum class";
763 else
764 return "enum";
766 else if (TREE_CODE (t) == UNION_TYPE)
767 return "union";
768 else if (TYPE_LANG_SPECIFIC (t) && CLASSTYPE_DECLARED_CLASS (t))
769 return "class";
770 else
771 return "struct";
774 /* Disable warnings about missing quoting in GCC diagnostics for
775 the pp_verbatim call. Their format strings deliberately don't
776 follow GCC diagnostic conventions. */
777 #if __GNUC__ >= 10
778 #pragma GCC diagnostic push
779 #pragma GCC diagnostic ignored "-Wformat-diag"
780 #endif
782 /* Print out a class declaration T under the control of FLAGS,
783 in the form `class foo'. */
785 static void
786 dump_aggr_type (cxx_pretty_printer *pp, tree t, int flags)
788 const char *variety = class_key_or_enum_as_string (t);
789 int typdef = 0;
790 int tmplate = 0;
792 pp_cxx_cv_qualifier_seq (pp, t);
794 if (flags & TFF_CLASS_KEY_OR_ENUM)
795 pp_cxx_ws_string (pp, variety);
797 tree decl = TYPE_NAME (t);
799 if (decl)
801 typdef = (!DECL_ARTIFICIAL (decl)
802 /* An alias specialization is not considered to be a
803 typedef. */
804 && !alias_template_specialization_p (t, nt_opaque));
806 if ((typdef
807 && ((flags & TFF_CHASE_TYPEDEF)
808 || (!flag_pretty_templates && DECL_LANG_SPECIFIC (decl)
809 && DECL_TEMPLATE_INFO (decl))))
810 || DECL_SELF_REFERENCE_P (decl))
812 t = TYPE_MAIN_VARIANT (t);
813 decl = TYPE_NAME (t);
814 typdef = 0;
817 tmplate = !typdef && TREE_CODE (t) != ENUMERAL_TYPE
818 && TYPE_LANG_SPECIFIC (t) && CLASSTYPE_TEMPLATE_INFO (t)
819 && (TREE_CODE (CLASSTYPE_TI_TEMPLATE (t)) != TEMPLATE_DECL
820 || PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
822 if (! (flags & TFF_UNQUALIFIED_NAME))
823 dump_scope (pp, CP_DECL_CONTEXT (decl), flags | TFF_SCOPE);
824 flags &= ~TFF_UNQUALIFIED_NAME;
825 if (tmplate)
827 /* Because the template names are mangled, we have to locate
828 the most general template, and use that name. */
829 tree tpl = TYPE_TI_TEMPLATE (t);
831 while (DECL_TEMPLATE_INFO (tpl))
832 tpl = DECL_TI_TEMPLATE (tpl);
833 decl = tpl;
837 if (LAMBDA_TYPE_P (t))
839 /* A lambda's "type" is essentially its signature. */
840 pp_string (pp, M_("<lambda"));
841 if (lambda_function (t))
842 dump_parameters (pp,
843 FUNCTION_FIRST_USER_PARMTYPE (lambda_function (t)),
844 flags);
845 pp_greater (pp);
847 else if (!decl || IDENTIFIER_ANON_P (DECL_NAME (decl)))
849 if (flags & TFF_CLASS_KEY_OR_ENUM)
850 pp_string (pp, M_("<unnamed>"));
851 else
852 pp_printf (pp, M_("<unnamed %s>"), variety);
854 else
855 pp_cxx_tree_identifier (pp, DECL_NAME (decl));
857 dump_module_suffix (pp, decl);
859 if (tmplate)
860 dump_template_parms (pp, TYPE_TEMPLATE_INFO (t),
861 !CLASSTYPE_USE_TEMPLATE (t),
862 flags & ~TFF_TEMPLATE_HEADER);
865 #if __GNUC__ >= 10
866 #pragma GCC diagnostic pop
867 #endif
869 /* Dump into the obstack the initial part of the output for a given type.
870 This is necessary when dealing with things like functions returning
871 functions. Examples:
873 return type of `int (* fee ())()': pointer -> function -> int. Both
874 pointer (and reference and offset) and function (and member) types must
875 deal with prefix and suffix.
877 Arrays must also do this for DECL nodes, like int a[], and for things like
878 int *[]&. */
880 static void
881 dump_type_prefix (cxx_pretty_printer *pp, tree t, int flags)
883 if (TYPE_PTRMEMFUNC_P (t))
885 t = TYPE_PTRMEMFUNC_FN_TYPE (t);
886 goto offset_type;
889 switch (TREE_CODE (t))
891 case POINTER_TYPE:
892 case REFERENCE_TYPE:
894 tree sub = TREE_TYPE (t);
896 dump_type_prefix (pp, sub, flags);
897 if (TREE_CODE (sub) == ARRAY_TYPE
898 || TREE_CODE (sub) == FUNCTION_TYPE)
900 pp_cxx_whitespace (pp);
901 pp_cxx_left_paren (pp);
902 /* If we're dealing with the GNU form of attributes, print this:
903 void (__attribute__((noreturn)) *f) ();
904 If it is the standard [[]] attribute, we'll print the attribute
905 in dump_type_suffix. */
906 if (!cxx11_attribute_p (TYPE_ATTRIBUTES (sub)))
907 pp_c_attributes_display (pp, TYPE_ATTRIBUTES (sub));
909 if (TYPE_PTR_P (t))
910 pp_star (pp);
911 else if (TYPE_REF_P (t))
913 if (TYPE_REF_IS_RVALUE (t))
914 pp_ampersand_ampersand (pp);
915 else
916 pp_ampersand (pp);
918 pp->padding = pp_before;
919 pp_cxx_cv_qualifier_seq (pp, t);
921 break;
923 case OFFSET_TYPE:
924 offset_type:
925 dump_type_prefix (pp, TREE_TYPE (t), flags);
926 if (TREE_CODE (t) == OFFSET_TYPE) /* pmfs deal with this in d_t_p */
928 pp_maybe_space (pp);
929 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
930 pp_cxx_left_paren (pp);
931 dump_type (pp, TYPE_OFFSET_BASETYPE (t), flags);
932 pp_cxx_colon_colon (pp);
934 pp_cxx_star (pp);
935 pp_cxx_cv_qualifier_seq (pp, t);
936 pp->padding = pp_before;
937 break;
939 /* This can be reached without a pointer when dealing with
940 templates, e.g. std::is_function. */
941 case FUNCTION_TYPE:
942 dump_type_prefix (pp, TREE_TYPE (t), flags);
943 break;
945 case METHOD_TYPE:
946 dump_type_prefix (pp, TREE_TYPE (t), flags);
947 pp_maybe_space (pp);
948 pp_cxx_left_paren (pp);
949 dump_aggr_type (pp, TYPE_METHOD_BASETYPE (t), flags);
950 pp_cxx_colon_colon (pp);
951 break;
953 case ARRAY_TYPE:
954 dump_type_prefix (pp, TREE_TYPE (t), flags);
955 break;
957 case ENUMERAL_TYPE:
958 case IDENTIFIER_NODE:
959 case INTEGER_TYPE:
960 case BOOLEAN_TYPE:
961 case REAL_TYPE:
962 case RECORD_TYPE:
963 case TEMPLATE_TYPE_PARM:
964 case TEMPLATE_TEMPLATE_PARM:
965 case BOUND_TEMPLATE_TEMPLATE_PARM:
966 case TREE_LIST:
967 case TYPE_DECL:
968 case TREE_VEC:
969 case UNION_TYPE:
970 case LANG_TYPE:
971 case VOID_TYPE:
972 case OPAQUE_TYPE:
973 case TYPENAME_TYPE:
974 case COMPLEX_TYPE:
975 case VECTOR_TYPE:
976 case TYPEOF_TYPE:
977 case TRAIT_TYPE:
978 case DECLTYPE_TYPE:
979 case TYPE_PACK_EXPANSION:
980 case FIXED_POINT_TYPE:
981 case NULLPTR_TYPE:
982 dump_type (pp, t, flags);
983 pp->padding = pp_before;
984 break;
986 default:
987 pp_unsupported_tree (pp, t);
988 /* fall through. */
989 case ERROR_MARK:
990 pp_string (pp, M_("<typeprefixerror>"));
991 break;
995 /* Dump the suffix of type T, under control of FLAGS. This is the part
996 which appears after the identifier (or function parms). */
998 static void
999 dump_type_suffix (cxx_pretty_printer *pp, tree t, int flags)
1001 if (TYPE_PTRMEMFUNC_P (t))
1002 t = TYPE_PTRMEMFUNC_FN_TYPE (t);
1004 switch (TREE_CODE (t))
1006 case POINTER_TYPE:
1007 case REFERENCE_TYPE:
1008 case OFFSET_TYPE:
1009 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
1010 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
1011 pp_cxx_right_paren (pp);
1012 if (TREE_CODE (t) == POINTER_TYPE)
1013 flags |= TFF_POINTER;
1014 dump_type_suffix (pp, TREE_TYPE (t), flags);
1015 break;
1017 case FUNCTION_TYPE:
1018 case METHOD_TYPE:
1020 tree arg;
1021 if (TREE_CODE (t) == METHOD_TYPE)
1022 /* Can only be reached through a pointer. */
1023 pp_cxx_right_paren (pp);
1024 arg = TYPE_ARG_TYPES (t);
1025 if (TREE_CODE (t) == METHOD_TYPE)
1026 arg = TREE_CHAIN (arg);
1028 /* Function pointers don't have default args. Not in standard C++,
1029 anyway; they may in g++, but we'll just pretend otherwise. */
1030 dump_parameters (pp, arg, flags & ~TFF_FUNCTION_DEFAULT_ARGUMENTS);
1032 pp->padding = pp_before;
1033 pp_cxx_cv_qualifiers (pp, type_memfn_quals (t),
1034 TREE_CODE (t) == FUNCTION_TYPE
1035 && (flags & TFF_POINTER));
1036 dump_ref_qualifier (pp, t, flags);
1037 if (tx_safe_fn_type_p (t))
1038 pp_cxx_ws_string (pp, "transaction_safe");
1039 dump_exception_spec (pp, TYPE_RAISES_EXCEPTIONS (t), flags);
1040 /* If this is the standard [[]] attribute, print
1041 void (*)() [[noreturn]]; */
1042 if (cxx11_attribute_p (TYPE_ATTRIBUTES (t)))
1044 pp_space (pp);
1045 pp_c_attributes_display (pp, TYPE_ATTRIBUTES (t));
1046 pp->padding = pp_before;
1048 dump_type_suffix (pp, TREE_TYPE (t), flags);
1049 break;
1052 case ARRAY_TYPE:
1053 pp_maybe_space (pp);
1054 pp_cxx_left_bracket (pp);
1055 if (tree dtype = TYPE_DOMAIN (t))
1057 tree max = TYPE_MAX_VALUE (dtype);
1058 /* Zero-length arrays have a null upper bound in C and SIZE_MAX
1059 in C++. Handle both since the type might be constructed by
1060 the middle end and end up here as a result of a warning (see
1061 PR c++/97201). */
1062 if (!max || integer_all_onesp (max))
1063 pp_character (pp, '0');
1064 else if (tree_fits_shwi_p (max))
1065 pp_wide_integer (pp, tree_to_shwi (max) + 1);
1066 else
1068 STRIP_NOPS (max);
1069 if (TREE_CODE (max) == SAVE_EXPR)
1070 max = TREE_OPERAND (max, 0);
1071 if (TREE_CODE (max) == MINUS_EXPR
1072 || TREE_CODE (max) == PLUS_EXPR)
1074 max = TREE_OPERAND (max, 0);
1075 while (CONVERT_EXPR_P (max))
1076 max = TREE_OPERAND (max, 0);
1078 else
1079 max = fold_build2_loc (input_location,
1080 PLUS_EXPR, dtype, max,
1081 build_int_cst (dtype, 1));
1082 dump_expr (pp, max, flags & ~TFF_EXPR_IN_PARENS);
1085 pp_cxx_right_bracket (pp);
1086 dump_type_suffix (pp, TREE_TYPE (t), flags);
1087 break;
1089 case ENUMERAL_TYPE:
1090 case IDENTIFIER_NODE:
1091 case INTEGER_TYPE:
1092 case BOOLEAN_TYPE:
1093 case REAL_TYPE:
1094 case RECORD_TYPE:
1095 case TEMPLATE_TYPE_PARM:
1096 case TEMPLATE_TEMPLATE_PARM:
1097 case BOUND_TEMPLATE_TEMPLATE_PARM:
1098 case TREE_LIST:
1099 case TYPE_DECL:
1100 case TREE_VEC:
1101 case UNION_TYPE:
1102 case LANG_TYPE:
1103 case VOID_TYPE:
1104 case OPAQUE_TYPE:
1105 case TYPENAME_TYPE:
1106 case COMPLEX_TYPE:
1107 case VECTOR_TYPE:
1108 case TYPEOF_TYPE:
1109 case TRAIT_TYPE:
1110 case DECLTYPE_TYPE:
1111 case TYPE_PACK_EXPANSION:
1112 case FIXED_POINT_TYPE:
1113 case NULLPTR_TYPE:
1114 break;
1116 default:
1117 pp_unsupported_tree (pp, t);
1118 case ERROR_MARK:
1119 /* Don't mark it here, we should have already done in
1120 dump_type_prefix. */
1121 break;
1125 static void
1126 dump_global_iord (cxx_pretty_printer *pp, tree t)
1128 const char *p = NULL;
1130 if (DECL_GLOBAL_CTOR_P (t))
1131 p = M_("(static initializers for %s)");
1132 else if (DECL_GLOBAL_DTOR_P (t))
1133 p = M_("(static destructors for %s)");
1134 else
1135 gcc_unreachable ();
1137 pp_printf (pp, p, DECL_SOURCE_FILE (t));
1140 static void
1141 dump_simple_decl (cxx_pretty_printer *pp, tree t, tree type, int flags)
1143 if (VAR_P (t) && DECL_NTTP_OBJECT_P (t))
1144 return dump_expr (pp, DECL_INITIAL (t), flags);
1146 if (flags & TFF_DECL_SPECIFIERS)
1148 if (concept_definition_p (t))
1149 pp_cxx_ws_string (pp, "concept");
1150 else if (VAR_P (t) && DECL_DECLARED_CONSTEXPR_P (t))
1151 pp_cxx_ws_string (pp, "constexpr");
1153 if (!standard_concept_p (t))
1154 dump_type_prefix (pp, type, flags & ~TFF_UNQUALIFIED_NAME);
1155 pp_maybe_space (pp);
1157 if (! (flags & TFF_UNQUALIFIED_NAME)
1158 && TREE_CODE (t) != PARM_DECL
1159 && (!DECL_INITIAL (t)
1160 || TREE_CODE (DECL_INITIAL (t)) != TEMPLATE_PARM_INDEX))
1161 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
1162 flags &= ~TFF_UNQUALIFIED_NAME;
1163 if ((flags & TFF_DECL_SPECIFIERS)
1164 && DECL_TEMPLATE_PARM_P (t)
1165 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (t)))
1166 pp_string (pp, "...");
1167 if (DECL_NAME (t))
1169 if (TREE_CODE (t) == FIELD_DECL && DECL_NORMAL_CAPTURE_P (t))
1171 pp_less (pp);
1172 pp_string (pp, IDENTIFIER_POINTER (DECL_NAME (t)) + 2);
1173 pp_string (pp, " capture>");
1175 else
1176 dump_decl (pp, DECL_NAME (t), flags);
1178 else if (DECL_DECOMPOSITION_P (t))
1179 pp_string (pp, M_("<structured bindings>"));
1180 else if (TREE_CODE (t) == FIELD_DECL && DECL_FIELD_IS_BASE (t))
1181 dump_type (pp, TREE_TYPE (t), flags);
1182 else
1183 pp_string (pp, M_("<anonymous>"));
1185 dump_module_suffix (pp, t);
1187 if (flags & TFF_DECL_SPECIFIERS)
1188 dump_type_suffix (pp, type, flags);
1191 class colorize_guard
1193 bool colorize;
1194 cxx_pretty_printer *pp;
1195 public:
1196 colorize_guard (bool _colorize, cxx_pretty_printer *pp, const char *name)
1197 : colorize (_colorize && pp_show_color (pp)), pp (pp)
1199 pp_string (pp, colorize_start (colorize, name));
1201 ~colorize_guard ()
1203 pp_string (pp, colorize_stop (colorize));
1207 /* Print an IDENTIFIER_NODE that is the name of a declaration. */
1209 static void
1210 dump_decl_name (cxx_pretty_printer *pp, tree t, int flags)
1212 /* These special cases are duplicated here so that other functions
1213 can feed identifiers to error and get them demangled properly. */
1214 if (IDENTIFIER_CONV_OP_P (t))
1216 pp_cxx_ws_string (pp, "operator");
1217 /* Not exactly IDENTIFIER_TYPE_VALUE. */
1218 dump_type (pp, TREE_TYPE (t), flags);
1219 return;
1221 if (dguide_name_p (t))
1223 dump_decl (pp, CLASSTYPE_TI_TEMPLATE (TREE_TYPE (t)),
1224 TFF_UNQUALIFIED_NAME);
1225 return;
1228 const char *str = IDENTIFIER_POINTER (t);
1229 if (startswith (str, "_ZGR"))
1231 pp_cxx_ws_string (pp, "<temporary>");
1232 return;
1235 pp_cxx_tree_identifier (pp, t);
1238 /* Dump a human readable string for the decl T under control of FLAGS. */
1240 static void
1241 dump_decl (cxx_pretty_printer *pp, tree t, int flags)
1243 if (t == NULL_TREE)
1244 return;
1246 /* If doing Objective-C++, give Objective-C a chance to demangle
1247 Objective-C method names. */
1248 if (c_dialect_objc ())
1250 const char *demangled = objc_maybe_printable_name (t, flags);
1251 if (demangled)
1253 pp_string (pp, demangled);
1254 return;
1258 switch (TREE_CODE (t))
1260 case TYPE_DECL:
1261 /* Don't say 'typedef class A' */
1262 if (DECL_ARTIFICIAL (t) && !DECL_SELF_REFERENCE_P (t))
1264 if ((flags & TFF_DECL_SPECIFIERS)
1265 && TREE_CODE (TREE_TYPE (t)) == TEMPLATE_TYPE_PARM)
1267 /* Say `class T' not just `T'. */
1268 pp_cxx_ws_string (pp, "class");
1270 /* Emit the `...' for a parameter pack. */
1271 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
1272 pp_cxx_ws_string (pp, "...");
1275 dump_type (pp, TREE_TYPE (t), flags);
1276 break;
1278 if (TYPE_DECL_ALIAS_P (t)
1279 && (flags & TFF_DECL_SPECIFIERS
1280 || flags & TFF_CLASS_KEY_OR_ENUM))
1282 pp_cxx_ws_string (pp, "using");
1283 if (! (flags & TFF_UNQUALIFIED_NAME))
1284 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
1285 dump_decl (pp, DECL_NAME (t), flags);
1286 pp_cxx_whitespace (pp);
1287 pp_cxx_ws_string (pp, "=");
1288 pp_cxx_whitespace (pp);
1289 dump_type (pp, (DECL_ORIGINAL_TYPE (t)
1290 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t)),
1291 flags);
1292 break;
1294 if ((flags & TFF_DECL_SPECIFIERS)
1295 && !DECL_SELF_REFERENCE_P (t))
1296 pp_cxx_ws_string (pp, "typedef");
1297 dump_simple_decl (pp, t, DECL_ORIGINAL_TYPE (t)
1298 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t),
1299 flags);
1300 break;
1302 case VAR_DECL:
1303 if (DECL_NAME (t) && VTABLE_NAME_P (DECL_NAME (t)))
1305 pp_string (pp, M_("vtable for "));
1306 gcc_assert (TYPE_P (DECL_CONTEXT (t)));
1307 dump_type (pp, DECL_CONTEXT (t), flags);
1308 break;
1310 /* Fall through. */
1311 case FIELD_DECL:
1312 case PARM_DECL:
1313 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1315 /* Handle variable template specializations. */
1316 if (VAR_P (t)
1317 && DECL_LANG_SPECIFIC (t)
1318 && DECL_TEMPLATE_INFO (t)
1319 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1321 pp_cxx_begin_template_argument_list (pp);
1322 tree args = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1323 dump_template_argument_list (pp, args, flags);
1324 pp_cxx_end_template_argument_list (pp);
1326 break;
1328 case RESULT_DECL:
1329 pp_string (pp, M_("<return value> "));
1330 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1331 break;
1333 case NAMESPACE_DECL:
1334 if (flags & TFF_DECL_SPECIFIERS)
1335 pp->declaration (t);
1336 else
1338 if (! (flags & TFF_UNQUALIFIED_NAME))
1339 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
1340 flags &= ~TFF_UNQUALIFIED_NAME;
1341 if (DECL_NAME (t) == NULL_TREE)
1343 if (!(pp->flags & pp_c_flag_gnu_v3))
1344 pp_cxx_ws_string (pp, M_("{anonymous}"));
1345 else
1346 pp_cxx_ws_string (pp, M_("(anonymous namespace)"));
1348 else
1349 pp_cxx_tree_identifier (pp, DECL_NAME (t));
1351 break;
1353 case SCOPE_REF:
1354 dump_type (pp, TREE_OPERAND (t, 0), flags);
1355 pp_cxx_colon_colon (pp);
1356 dump_decl (pp, TREE_OPERAND (t, 1), TFF_UNQUALIFIED_NAME);
1357 break;
1359 case ARRAY_REF:
1360 dump_decl (pp, TREE_OPERAND (t, 0), flags);
1361 pp_cxx_left_bracket (pp);
1362 dump_decl (pp, TREE_OPERAND (t, 1), flags);
1363 pp_cxx_right_bracket (pp);
1364 break;
1366 /* So that we can do dump_decl on an aggr type. */
1367 case RECORD_TYPE:
1368 case UNION_TYPE:
1369 case ENUMERAL_TYPE:
1370 dump_type (pp, t, flags);
1371 break;
1373 case BIT_NOT_EXPR:
1374 /* This is a pseudo destructor call which has not been folded into
1375 a PSEUDO_DTOR_EXPR yet. */
1376 pp_cxx_complement (pp);
1377 dump_type (pp, TREE_OPERAND (t, 0), flags);
1378 break;
1380 case TYPE_EXPR:
1381 gcc_unreachable ();
1382 break;
1384 case IDENTIFIER_NODE:
1385 dump_decl_name (pp, t, flags);
1386 break;
1388 case OVERLOAD:
1389 if (!OVL_SINGLE_P (t))
1391 tree ctx = ovl_scope (t);
1392 if (ctx != global_namespace)
1394 if (TYPE_P (ctx))
1395 dump_type (pp, ctx, flags);
1396 else
1397 dump_decl (pp, ctx, flags);
1398 pp_cxx_colon_colon (pp);
1400 dump_decl (pp, OVL_NAME (t), flags);
1401 break;
1404 /* If there's only one function, just treat it like an ordinary
1405 FUNCTION_DECL. */
1406 t = OVL_FIRST (t);
1407 /* Fall through. */
1409 case FUNCTION_DECL:
1410 if (! DECL_LANG_SPECIFIC (t))
1412 if (DECL_ABSTRACT_ORIGIN (t)
1413 && DECL_ABSTRACT_ORIGIN (t) != t)
1414 dump_decl (pp, DECL_ABSTRACT_ORIGIN (t), flags);
1415 else
1416 dump_function_name (pp, t, flags);
1418 else if (DECL_GLOBAL_CTOR_P (t) || DECL_GLOBAL_DTOR_P (t))
1419 dump_global_iord (pp, t);
1420 else
1421 dump_function_decl (pp, t, flags);
1422 break;
1424 case TEMPLATE_DECL:
1425 dump_template_decl (pp, t, flags);
1426 break;
1428 case CONCEPT_DECL:
1429 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1430 break;
1432 case WILDCARD_DECL:
1433 pp_string (pp, "<wildcard>");
1434 break;
1436 case TEMPLATE_ID_EXPR:
1438 tree name = TREE_OPERAND (t, 0);
1439 tree args = TREE_OPERAND (t, 1);
1441 if (!identifier_p (name))
1442 name = OVL_NAME (name);
1443 dump_decl (pp, name, flags);
1444 pp_cxx_begin_template_argument_list (pp);
1445 if (args == error_mark_node)
1446 pp_string (pp, M_("<template arguments error>"));
1447 else if (args)
1448 dump_template_argument_list
1449 (pp, args, flags|TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS);
1450 pp_cxx_end_template_argument_list (pp);
1452 break;
1454 case LABEL_DECL:
1455 if (DECL_NAME (t))
1456 pp_cxx_tree_identifier (pp, DECL_NAME (t));
1457 else
1458 dump_generic_node (pp, t, 0, TDF_SLIM, false);
1459 break;
1461 case CONST_DECL:
1462 if ((TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == ENUMERAL_TYPE)
1463 || (DECL_INITIAL (t) &&
1464 TREE_CODE (DECL_INITIAL (t)) == TEMPLATE_PARM_INDEX))
1465 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1466 else if (DECL_NAME (t))
1467 dump_decl (pp, DECL_NAME (t), flags);
1468 else if (DECL_INITIAL (t))
1469 dump_expr (pp, DECL_INITIAL (t), flags | TFF_EXPR_IN_PARENS);
1470 else
1471 pp_string (pp, M_("<enumerator>"));
1472 break;
1474 case USING_DECL:
1476 if (flags & TFF_DECL_SPECIFIERS)
1477 pp_cxx_ws_string (pp, "using");
1478 bool variadic = false;
1479 if (!(flags & TFF_UNQUALIFIED_NAME))
1481 tree scope = USING_DECL_SCOPE (t);
1482 tree name = DECL_NAME (t);
1483 if (PACK_EXPANSION_P (scope))
1485 scope = PACK_EXPANSION_PATTERN (scope);
1486 variadic = true;
1488 if (identifier_p (name)
1489 && IDENTIFIER_CONV_OP_P (name)
1490 && PACK_EXPANSION_P (TREE_TYPE (name)))
1492 name = make_conv_op_name (PACK_EXPANSION_PATTERN
1493 (TREE_TYPE (name)));
1494 variadic = true;
1496 dump_type (pp, scope, flags);
1497 pp_cxx_colon_colon (pp);
1499 dump_decl (pp, DECL_NAME (t), flags);
1500 if (variadic)
1501 pp_cxx_ws_string (pp, "...");
1503 break;
1505 case STATIC_ASSERT:
1506 pp->declaration (t);
1507 break;
1509 case BASELINK:
1510 dump_decl (pp, BASELINK_FUNCTIONS (t), flags);
1511 break;
1513 case NON_DEPENDENT_EXPR:
1514 dump_expr (pp, t, flags);
1515 break;
1517 case TEMPLATE_TYPE_PARM:
1518 if (flags & TFF_DECL_SPECIFIERS)
1519 pp->declaration (t);
1520 else
1521 pp->type_id (t);
1522 break;
1524 case UNBOUND_CLASS_TEMPLATE:
1525 case TYPE_PACK_EXPANSION:
1526 case TREE_BINFO:
1527 dump_type (pp, t, flags);
1528 break;
1530 default:
1531 pp_unsupported_tree (pp, t);
1532 /* Fall through. */
1534 case ERROR_MARK:
1535 pp_string (pp, M_("<declaration error>"));
1536 break;
1540 /* Dump a template declaration T under control of FLAGS. This means the
1541 'template <...> leaders plus the 'class X' or 'void fn(...)' part. */
1543 static void
1544 dump_template_decl (cxx_pretty_printer *pp, tree t, int flags)
1546 tree orig_parms = DECL_TEMPLATE_PARMS (t);
1547 tree parms;
1548 int i;
1550 if (flags & TFF_TEMPLATE_HEADER)
1552 for (parms = orig_parms = nreverse (orig_parms);
1553 parms;
1554 parms = TREE_CHAIN (parms))
1556 tree inner_parms = INNERMOST_TEMPLATE_PARMS (parms);
1557 int len = TREE_VEC_LENGTH (inner_parms);
1559 if (len == 0)
1561 /* Skip over the dummy template levels of a template template
1562 parm. */
1563 gcc_assert (TREE_CODE (TREE_TYPE (t)) == TEMPLATE_TEMPLATE_PARM);
1564 continue;
1567 pp_cxx_ws_string (pp, "template");
1568 pp_cxx_begin_template_argument_list (pp);
1570 /* If we've shown the template prefix, we'd better show the
1571 parameters' and decl's type too. */
1572 flags |= TFF_DECL_SPECIFIERS;
1574 for (i = 0; i < len; i++)
1576 if (i)
1577 pp_separate_with_comma (pp);
1578 dump_template_parameter (pp, TREE_VEC_ELT (inner_parms, i),
1579 flags);
1581 pp_cxx_end_template_argument_list (pp);
1582 pp_cxx_whitespace (pp);
1584 nreverse(orig_parms);
1586 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
1588 /* Say `template<arg> class TT' not just `template<arg> TT'. */
1589 pp_cxx_ws_string (pp, "class");
1591 /* If this is a parameter pack, print the ellipsis. */
1592 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
1593 pp_cxx_ws_string (pp, "...");
1596 /* Only print the requirements if we're also printing
1597 the template header. */
1598 if (flag_concepts)
1599 if (tree ci = get_constraints (t))
1600 if (check_constraint_info (ci))
1601 if (tree reqs = CI_TEMPLATE_REQS (ci))
1603 pp_cxx_requires_clause (pp, reqs);
1604 pp_cxx_whitespace (pp);
1609 if (DECL_CLASS_TEMPLATE_P (t))
1610 dump_type (pp, TREE_TYPE (t),
1611 ((flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1612 | (flags & TFF_DECL_SPECIFIERS ? TFF_CLASS_KEY_OR_ENUM : 0)));
1613 else if (DECL_TEMPLATE_RESULT (t)
1614 && (VAR_P (DECL_TEMPLATE_RESULT (t))
1615 /* Alias template. */
1616 || DECL_TYPE_TEMPLATE_P (t)
1617 /* Concept definition. &*/
1618 || TREE_CODE (DECL_TEMPLATE_RESULT (t)) == CONCEPT_DECL))
1619 dump_decl (pp, DECL_TEMPLATE_RESULT (t), flags | TFF_TEMPLATE_NAME);
1620 else
1622 gcc_assert (TREE_TYPE (t));
1623 switch (NEXT_CODE (t))
1625 case METHOD_TYPE:
1626 case FUNCTION_TYPE:
1627 dump_function_decl (pp, t, flags | TFF_TEMPLATE_NAME);
1628 break;
1629 default:
1630 /* This case can occur with some invalid code. */
1631 dump_type (pp, TREE_TYPE (t),
1632 (flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1633 | (flags & TFF_DECL_SPECIFIERS
1634 ? TFF_CLASS_KEY_OR_ENUM : 0));
1639 /* find_typenames looks through the type of the function template T
1640 and returns a vec containing any typedefs, decltypes or TYPENAME_TYPEs
1641 it finds. */
1643 struct find_typenames_t
1645 hash_set<tree> *p_set;
1646 vec<tree, va_gc> *typenames;
1649 static tree
1650 find_typenames_r (tree *tp, int *walk_subtrees, void *data)
1652 struct find_typenames_t *d = (struct find_typenames_t *)data;
1653 tree mv = NULL_TREE;
1655 if (TYPE_P (*tp) && is_typedef_decl (TYPE_NAME (*tp)))
1656 /* Add the type of the typedef without any additional cv-quals. */
1657 mv = TREE_TYPE (TYPE_NAME (*tp));
1658 else if (TREE_CODE (*tp) == TYPENAME_TYPE
1659 || TREE_CODE (*tp) == DECLTYPE_TYPE)
1660 /* Add the typename without any cv-qualifiers. */
1661 mv = TYPE_MAIN_VARIANT (*tp);
1663 if (PACK_EXPANSION_P (*tp))
1665 /* Don't mess with parameter packs since we don't remember
1666 the pack expansion context for a particular typename. */
1667 *walk_subtrees = false;
1668 return NULL_TREE;
1671 if (mv && (mv == *tp || !d->p_set->add (mv)))
1672 vec_safe_push (d->typenames, mv);
1674 return NULL_TREE;
1677 static vec<tree, va_gc> *
1678 find_typenames (tree t)
1680 struct find_typenames_t ft;
1681 ft.p_set = new hash_set<tree>;
1682 ft.typenames = NULL;
1683 cp_walk_tree (&TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
1684 find_typenames_r, &ft, ft.p_set);
1685 delete ft.p_set;
1686 return ft.typenames;
1689 /* Output the "[with ...]" clause for a template instantiation T iff
1690 TEMPLATE_PARMS, TEMPLATE_ARGS and FLAGS are suitable. T may be NULL if
1691 formatting a deduction/substitution diagnostic rather than an
1692 instantiation. */
1694 static void
1695 dump_substitution (cxx_pretty_printer *pp,
1696 tree t, tree template_parms, tree template_args,
1697 int flags)
1699 if (template_parms != NULL_TREE && template_args != NULL_TREE
1700 && !(flags & TFF_NO_TEMPLATE_BINDINGS))
1702 vec<tree, va_gc> *typenames = t ? find_typenames (t) : NULL;
1703 dump_template_bindings (pp, template_parms, template_args, typenames);
1707 /* Dump the lambda function FN including its 'mutable' qualifier and any
1708 template bindings. */
1710 static void
1711 dump_lambda_function (cxx_pretty_printer *pp,
1712 tree fn, tree template_parms, tree template_args,
1713 int flags)
1715 /* A lambda's signature is essentially its "type". */
1716 dump_type (pp, DECL_CONTEXT (fn), flags);
1717 if (TREE_CODE (TREE_TYPE (fn)) == FUNCTION_TYPE)
1719 pp->padding = pp_before;
1720 pp_c_ws_string (pp, "static");
1722 else if (!(TYPE_QUALS (class_of_this_parm (TREE_TYPE (fn)))
1723 & TYPE_QUAL_CONST))
1725 pp->padding = pp_before;
1726 pp_c_ws_string (pp, "mutable");
1728 dump_substitution (pp, fn, template_parms, template_args, flags);
1731 /* Pretty print a function decl. There are several ways we want to print a
1732 function declaration. The TFF_ bits in FLAGS tells us how to behave.
1733 As error can only apply the '#' flag once to give 0 and 1 for V, there
1734 is %D which doesn't print the throw specs, and %F which does. */
1736 static void
1737 dump_function_decl (cxx_pretty_printer *pp, tree t, int flags)
1739 tree fntype;
1740 tree parmtypes;
1741 tree cname = NULL_TREE;
1742 tree template_args = NULL_TREE;
1743 tree template_parms = NULL_TREE;
1744 int show_return = flags & TFF_RETURN_TYPE || flags & TFF_DECL_SPECIFIERS;
1745 int do_outer_scope = ! (flags & TFF_UNQUALIFIED_NAME);
1746 tree exceptions;
1747 bool constexpr_p;
1748 tree ret = NULL_TREE;
1750 int dump_function_name_flags = flags & ~TFF_UNQUALIFIED_NAME;
1751 flags = dump_function_name_flags & ~TFF_TEMPLATE_NAME;
1752 if (TREE_CODE (t) == TEMPLATE_DECL)
1753 t = DECL_TEMPLATE_RESULT (t);
1755 /* Save the exceptions, in case t is a specialization and we are
1756 emitting an error about incompatible specifications. */
1757 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (t));
1759 /* Likewise for the constexpr specifier, in case t is a specialization. */
1760 constexpr_p = (DECL_DECLARED_CONSTEXPR_P (t)
1761 && !decl_implicit_constexpr_p (t));
1763 /* Pretty print template instantiations only. */
1764 if (DECL_USE_TEMPLATE (t) && DECL_TEMPLATE_INFO (t)
1765 && !(flags & TFF_NO_TEMPLATE_BINDINGS)
1766 && flag_pretty_templates)
1768 tree tmpl;
1770 template_args = DECL_TI_ARGS (t);
1771 tmpl = most_general_template (t);
1772 if (tmpl && TREE_CODE (tmpl) == TEMPLATE_DECL)
1774 template_parms = DECL_TEMPLATE_PARMS (tmpl);
1775 t = tmpl;
1779 if (DECL_NAME (t) && LAMBDA_FUNCTION_P (t))
1780 return dump_lambda_function (pp, t, template_parms, template_args, flags);
1782 fntype = TREE_TYPE (t);
1783 parmtypes = FUNCTION_FIRST_USER_PARMTYPE (t);
1785 if (DECL_CLASS_SCOPE_P (t))
1786 cname = DECL_CONTEXT (t);
1787 /* This is for partially instantiated template methods. */
1788 else if (TREE_CODE (fntype) == METHOD_TYPE)
1789 cname = TREE_TYPE (TREE_VALUE (parmtypes));
1791 if (flags & TFF_DECL_SPECIFIERS)
1793 if (DECL_STATIC_FUNCTION_P (t))
1794 pp_cxx_ws_string (pp, "static");
1795 else if (DECL_VIRTUAL_P (t))
1796 pp_cxx_ws_string (pp, "virtual");
1798 if (constexpr_p)
1800 if (DECL_DECLARED_CONCEPT_P (t))
1801 pp_cxx_ws_string (pp, "concept");
1802 else if (DECL_IMMEDIATE_FUNCTION_P (t))
1803 pp_cxx_ws_string (pp, "consteval");
1804 else
1805 pp_cxx_ws_string (pp, "constexpr");
1809 /* Print the return type? */
1810 if (show_return)
1811 show_return = (!DECL_CONV_FN_P (t) && !DECL_CONSTRUCTOR_P (t)
1812 && !DECL_DESTRUCTOR_P (t) && !deduction_guide_p (t));
1813 if (show_return)
1815 ret = fndecl_declared_return_type (t);
1816 dump_type_prefix (pp, ret, flags);
1819 /* Print the function name. */
1820 if (!do_outer_scope)
1821 /* Nothing. */;
1822 else if (cname)
1824 dump_type (pp, cname, flags);
1825 pp_cxx_colon_colon (pp);
1827 else
1828 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
1830 /* Name lookup for the rest of the function declarator is implicitly in the
1831 scope of the function, so avoid printing redundant scope qualifiers. */
1832 auto cds = make_temp_override (current_dump_scope, CP_DECL_CONTEXT (t));
1834 dump_function_name (pp, t, dump_function_name_flags);
1836 if (!(flags & TFF_NO_FUNCTION_ARGUMENTS))
1838 dump_parameters (pp, parmtypes, flags);
1840 if (TREE_CODE (fntype) == METHOD_TYPE)
1842 pp->padding = pp_before;
1843 pp_cxx_cv_qualifier_seq (pp, class_of_this_parm (fntype));
1844 dump_ref_qualifier (pp, fntype, flags);
1847 if (tx_safe_fn_type_p (fntype))
1849 pp->padding = pp_before;
1850 pp_cxx_ws_string (pp, "transaction_safe");
1853 if (flags & TFF_EXCEPTION_SPECIFICATION)
1855 pp->padding = pp_before;
1856 dump_exception_spec (pp, exceptions, flags);
1859 if (show_return)
1860 dump_type_suffix (pp, ret, flags);
1861 else if (deduction_guide_p (t))
1863 pp_cxx_ws_string (pp, "->");
1864 dump_type (pp, TREE_TYPE (TREE_TYPE (t)), flags);
1867 if (flag_concepts)
1868 if (tree ci = get_constraints (t))
1869 if (tree reqs = CI_DECLARATOR_REQS (ci))
1870 pp_cxx_requires_clause (pp, reqs);
1872 dump_substitution (pp, t, template_parms, template_args, flags);
1874 if (tree base = DECL_INHERITED_CTOR_BASE (t))
1876 pp_cxx_ws_string (pp, "[inherited from");
1877 dump_type (pp, base, TFF_PLAIN_IDENTIFIER);
1878 pp_character (pp, ']');
1881 else if (template_args)
1883 bool need_comma = false;
1884 int i;
1885 pp_cxx_begin_template_argument_list (pp);
1886 template_args = INNERMOST_TEMPLATE_ARGS (template_args);
1887 for (i = 0; i < TREE_VEC_LENGTH (template_args); ++i)
1889 tree arg = TREE_VEC_ELT (template_args, i);
1890 if (need_comma)
1891 pp_separate_with_comma (pp);
1892 if (ARGUMENT_PACK_P (arg))
1893 pp_cxx_left_brace (pp);
1894 dump_template_argument (pp, arg, TFF_PLAIN_IDENTIFIER);
1895 if (ARGUMENT_PACK_P (arg))
1896 pp_cxx_right_brace (pp);
1897 need_comma = true;
1899 pp_cxx_end_template_argument_list (pp);
1903 /* Print a parameter list. If this is for a member function, the
1904 member object ptr (and any other hidden args) should have
1905 already been removed. */
1907 static void
1908 dump_parameters (cxx_pretty_printer *pp, tree parmtypes, int flags)
1910 int first = 1;
1911 flags &= ~TFF_SCOPE;
1912 pp_cxx_left_paren (pp);
1914 for (first = 1; parmtypes != void_list_node;
1915 parmtypes = TREE_CHAIN (parmtypes))
1917 if (!first)
1918 pp_separate_with_comma (pp);
1919 first = 0;
1920 if (!parmtypes)
1922 pp_cxx_ws_string (pp, "...");
1923 break;
1926 dump_type (pp, TREE_VALUE (parmtypes), flags);
1928 if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && TREE_PURPOSE (parmtypes))
1930 pp_cxx_whitespace (pp);
1931 pp_equal (pp);
1932 pp_cxx_whitespace (pp);
1933 dump_expr (pp, TREE_PURPOSE (parmtypes), flags | TFF_EXPR_IN_PARENS);
1937 pp_cxx_right_paren (pp);
1940 /* Print ref-qualifier of a FUNCTION_TYPE or METHOD_TYPE. FLAGS are ignored. */
1942 static void
1943 dump_ref_qualifier (cxx_pretty_printer *pp, tree t, int flags ATTRIBUTE_UNUSED)
1945 if (FUNCTION_REF_QUALIFIED (t))
1947 pp->padding = pp_before;
1948 if (FUNCTION_RVALUE_QUALIFIED (t))
1949 pp_cxx_ws_string (pp, "&&");
1950 else
1951 pp_cxx_ws_string (pp, "&");
1955 /* Print an exception specification. T is the exception specification. */
1957 static void
1958 dump_exception_spec (cxx_pretty_printer *pp, tree t, int flags)
1960 if (t && TREE_PURPOSE (t))
1962 pp_cxx_ws_string (pp, "noexcept");
1963 if (!integer_onep (TREE_PURPOSE (t)))
1965 pp_cxx_whitespace (pp);
1966 pp_cxx_left_paren (pp);
1967 if (DEFERRED_NOEXCEPT_SPEC_P (t))
1968 pp_cxx_ws_string (pp, "<uninstantiated>");
1969 else
1970 dump_expr (pp, TREE_PURPOSE (t), flags);
1971 pp_cxx_right_paren (pp);
1974 else if (t)
1976 pp_cxx_ws_string (pp, "throw");
1977 pp_cxx_whitespace (pp);
1978 pp_cxx_left_paren (pp);
1979 if (TREE_VALUE (t) != NULL_TREE)
1980 while (1)
1982 dump_type (pp, TREE_VALUE (t), flags);
1983 t = TREE_CHAIN (t);
1984 if (!t)
1985 break;
1986 pp_separate_with_comma (pp);
1988 pp_cxx_right_paren (pp);
1992 /* Handle the function name for a FUNCTION_DECL node, grokking operators
1993 and destructors properly. */
1995 static void
1996 dump_function_name (cxx_pretty_printer *pp, tree t, int flags)
1998 /* Only colorize when we're printing something before the name; in
1999 particular, not when printing a CALL_EXPR. */
2000 bool colorize = flags & (TFF_DECL_SPECIFIERS | TFF_RETURN_TYPE
2001 | TFF_TEMPLATE_HEADER);
2003 colorize_guard g (colorize, pp, "fnname");
2005 tree name = DECL_NAME (t);
2007 /* We can get here with a decl that was synthesized by language-
2008 independent machinery (e.g. coverage.cc) in which case it won't
2009 have a lang_specific structure attached and DECL_CONSTRUCTOR_P
2010 will crash. In this case it is safe just to print out the
2011 literal name. */
2012 if (!DECL_LANG_SPECIFIC (t))
2014 pp_cxx_tree_identifier (pp, name);
2015 return;
2018 if (TREE_CODE (t) == TEMPLATE_DECL)
2019 t = DECL_TEMPLATE_RESULT (t);
2021 /* Don't let the user see __comp_ctor et al. */
2022 if (DECL_CONSTRUCTOR_P (t)
2023 || DECL_DESTRUCTOR_P (t))
2025 if (LAMBDA_TYPE_P (DECL_CONTEXT (t)))
2026 name = get_identifier ("<lambda>");
2027 else if (TYPE_UNNAMED_P (DECL_CONTEXT (t)))
2028 name = get_identifier ("<constructor>");
2029 else
2030 name = constructor_name (DECL_CONTEXT (t));
2033 if (DECL_DESTRUCTOR_P (t))
2035 pp_cxx_complement (pp);
2036 dump_decl (pp, name, TFF_PLAIN_IDENTIFIER);
2038 else if (DECL_CONV_FN_P (t))
2040 /* This cannot use the hack that the operator's return
2041 type is stashed off of its name because it may be
2042 used for error reporting. In the case of conflicting
2043 declarations, both will have the same name, yet
2044 the types will be different, hence the TREE_TYPE field
2045 of the first name will be clobbered by the second. */
2046 pp_cxx_ws_string (pp, "operator");
2047 dump_type (pp, TREE_TYPE (TREE_TYPE (t)), flags);
2049 else
2050 dump_decl (pp, name, flags);
2052 dump_module_suffix (pp, t);
2054 if (DECL_TEMPLATE_INFO (t)
2055 && !(flags & TFF_TEMPLATE_NAME)
2056 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (t)
2057 && (TREE_CODE (DECL_TI_TEMPLATE (t)) != TEMPLATE_DECL
2058 || PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
2059 dump_template_parms (pp, DECL_TEMPLATE_INFO (t), !DECL_USE_TEMPLATE (t),
2060 flags);
2063 /* Dump the template parameters from the template info INFO under control of
2064 FLAGS. PRIMARY indicates whether this is a primary template decl, or
2065 specialization (partial or complete). For partial specializations we show
2066 the specialized parameter values. For a primary template we show no
2067 decoration. */
2069 static void
2070 dump_template_parms (cxx_pretty_printer *pp, tree info,
2071 int primary, int flags)
2073 tree args = info ? TI_ARGS (info) : NULL_TREE;
2075 if (primary && flags & TFF_TEMPLATE_NAME)
2076 return;
2077 flags &= ~(TFF_CLASS_KEY_OR_ENUM | TFF_TEMPLATE_NAME);
2078 pp_cxx_begin_template_argument_list (pp);
2080 /* Be careful only to print things when we have them, so as not
2081 to crash producing error messages. */
2082 if (args && !primary)
2084 int len, ix;
2085 len = get_non_default_template_args_count (args, flags);
2087 args = INNERMOST_TEMPLATE_ARGS (args);
2088 for (ix = 0; ix != len; ix++)
2090 tree arg = TREE_VEC_ELT (args, ix);
2092 /* Only print a comma if we know there is an argument coming. In
2093 the case of an empty template argument pack, no actual
2094 argument will be printed. */
2095 if (ix
2096 && (!ARGUMENT_PACK_P (arg)
2097 || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) > 0))
2098 pp_separate_with_comma (pp);
2100 if (!arg)
2101 pp_string (pp, M_("<template parameter error>"));
2102 else
2103 dump_template_argument (pp, arg, flags);
2106 else if (primary)
2108 tree tpl = TI_TEMPLATE (info);
2109 tree parms = DECL_TEMPLATE_PARMS (tpl);
2110 int len, ix;
2112 parms = TREE_CODE (parms) == TREE_LIST ? TREE_VALUE (parms) : NULL_TREE;
2113 len = parms ? TREE_VEC_LENGTH (parms) : 0;
2115 for (ix = 0; ix != len; ix++)
2117 tree parm;
2119 if (TREE_VEC_ELT (parms, ix) == error_mark_node)
2121 pp_string (pp, M_("<template parameter error>"));
2122 continue;
2125 parm = TREE_VALUE (TREE_VEC_ELT (parms, ix));
2127 if (ix)
2128 pp_separate_with_comma (pp);
2130 dump_decl (pp, parm, flags & ~TFF_DECL_SPECIFIERS);
2133 pp_cxx_end_template_argument_list (pp);
2136 /* Print out the arguments of CALL_EXPR T as a parenthesized list using
2137 flags FLAGS. Skip over the first argument if SKIPFIRST is true. */
2139 static void
2140 dump_call_expr_args (cxx_pretty_printer *pp, tree t, int flags, bool skipfirst)
2142 tree arg;
2143 call_expr_arg_iterator iter;
2145 pp_cxx_left_paren (pp);
2146 FOR_EACH_CALL_EXPR_ARG (arg, iter, t)
2148 if (skipfirst)
2149 skipfirst = false;
2150 else
2152 dump_expr (pp, arg, flags | TFF_EXPR_IN_PARENS);
2153 if (more_call_expr_args_p (&iter))
2154 pp_separate_with_comma (pp);
2157 pp_cxx_right_paren (pp);
2160 /* Print out the arguments of AGGR_INIT_EXPR T as a parenthesized list
2161 using flags FLAGS. Skip over the first argument if SKIPFIRST is
2162 true. */
2164 static void
2165 dump_aggr_init_expr_args (cxx_pretty_printer *pp, tree t, int flags,
2166 bool skipfirst)
2168 tree arg;
2169 aggr_init_expr_arg_iterator iter;
2171 pp_cxx_left_paren (pp);
2172 FOR_EACH_AGGR_INIT_EXPR_ARG (arg, iter, t)
2174 if (skipfirst)
2175 skipfirst = false;
2176 else
2178 dump_expr (pp, arg, flags | TFF_EXPR_IN_PARENS);
2179 if (more_aggr_init_expr_args_p (&iter))
2180 pp_separate_with_comma (pp);
2183 pp_cxx_right_paren (pp);
2186 /* Print out a list of initializers (subr of dump_expr). */
2188 static void
2189 dump_expr_list (cxx_pretty_printer *pp, tree l, int flags)
2191 while (l)
2193 dump_expr (pp, TREE_VALUE (l), flags | TFF_EXPR_IN_PARENS);
2194 l = TREE_CHAIN (l);
2195 if (l)
2196 pp_separate_with_comma (pp);
2200 /* Print out a vector of initializers (subr of dump_expr). */
2202 static void
2203 dump_expr_init_vec (cxx_pretty_printer *pp, vec<constructor_elt, va_gc> *v,
2204 int flags)
2206 unsigned HOST_WIDE_INT idx;
2207 tree value;
2209 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
2211 dump_expr (pp, value, flags | TFF_EXPR_IN_PARENS);
2212 if (idx != v->length () - 1)
2213 pp_separate_with_comma (pp);
2218 /* We've gotten an indirect REFERENCE (an OBJ_TYPE_REF) to a virtual
2219 function. Resolve it to a close relative -- in the sense of static
2220 type -- variant being overridden. That is close to what was written in
2221 the source code. Subroutine of dump_expr. */
2223 static tree
2224 resolve_virtual_fun_from_obj_type_ref (tree ref)
2226 tree obj_type = TREE_TYPE (OBJ_TYPE_REF_TOKEN (ref));
2227 HOST_WIDE_INT index = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (ref));
2228 tree fun = BINFO_VIRTUALS (TYPE_BINFO (TREE_TYPE (obj_type)));
2229 while (index)
2231 fun = TREE_CHAIN (fun);
2232 index -= (TARGET_VTABLE_USES_DESCRIPTORS
2233 ? TARGET_VTABLE_USES_DESCRIPTORS : 1);
2236 return BV_FN (fun);
2239 /* Print out an expression E under control of FLAGS. */
2241 static void
2242 dump_expr (cxx_pretty_printer *pp, tree t, int flags)
2244 tree op;
2246 if (t == 0)
2247 return;
2249 if (STATEMENT_CLASS_P (t))
2251 pp_cxx_ws_string (pp, M_("<statement>"));
2252 return;
2255 switch (TREE_CODE (t))
2257 case VAR_DECL:
2258 case PARM_DECL:
2259 case FIELD_DECL:
2260 case CONST_DECL:
2261 case FUNCTION_DECL:
2262 case TEMPLATE_DECL:
2263 case NAMESPACE_DECL:
2264 case LABEL_DECL:
2265 case WILDCARD_DECL:
2266 case OVERLOAD:
2267 case TYPE_DECL:
2268 case USING_DECL:
2269 case IDENTIFIER_NODE:
2270 dump_decl (pp, t, ((flags & ~(TFF_DECL_SPECIFIERS|TFF_RETURN_TYPE
2271 |TFF_TEMPLATE_HEADER))
2272 | TFF_NO_TEMPLATE_BINDINGS
2273 | TFF_NO_FUNCTION_ARGUMENTS));
2274 break;
2276 case SSA_NAME:
2277 if (SSA_NAME_VAR (t)
2278 && !DECL_ARTIFICIAL (SSA_NAME_VAR (t)))
2279 dump_expr (pp, SSA_NAME_VAR (t), flags);
2280 else
2281 pp_cxx_ws_string (pp, M_("<unknown>"));
2282 break;
2284 case VOID_CST:
2285 case INTEGER_CST:
2286 case REAL_CST:
2287 case STRING_CST:
2288 case COMPLEX_CST:
2289 pp->constant (t);
2290 break;
2292 case USERDEF_LITERAL:
2293 pp_cxx_userdef_literal (pp, t);
2294 break;
2296 case THROW_EXPR:
2297 /* While waiting for caret diagnostics, avoid printing
2298 __cxa_allocate_exception, __cxa_throw, and the like. */
2299 pp_cxx_ws_string (pp, M_("<throw-expression>"));
2300 break;
2302 case PTRMEM_CST:
2303 pp_ampersand (pp);
2304 dump_type (pp, PTRMEM_CST_CLASS (t), flags);
2305 pp_cxx_colon_colon (pp);
2306 pp_cxx_tree_identifier (pp, DECL_NAME (PTRMEM_CST_MEMBER (t)));
2307 break;
2309 case COMPOUND_EXPR:
2310 pp_cxx_left_paren (pp);
2311 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2312 pp_separate_with_comma (pp);
2313 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2314 pp_cxx_right_paren (pp);
2315 break;
2317 case COND_EXPR:
2318 case VEC_COND_EXPR:
2319 pp_cxx_left_paren (pp);
2320 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2321 pp_string (pp, " ? ");
2322 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2323 pp_string (pp, " : ");
2324 dump_expr (pp, TREE_OPERAND (t, 2), flags | TFF_EXPR_IN_PARENS);
2325 pp_cxx_right_paren (pp);
2326 break;
2328 case SAVE_EXPR:
2329 if (TREE_HAS_CONSTRUCTOR (t))
2331 pp_cxx_ws_string (pp, "new");
2332 pp_cxx_whitespace (pp);
2333 dump_type (pp, TREE_TYPE (TREE_TYPE (t)), flags);
2335 else
2336 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2337 break;
2339 case AGGR_INIT_EXPR:
2341 tree fn = NULL_TREE;
2343 if (TREE_CODE (AGGR_INIT_EXPR_FN (t)) == ADDR_EXPR)
2344 fn = TREE_OPERAND (AGGR_INIT_EXPR_FN (t), 0);
2346 if (fn && TREE_CODE (fn) == FUNCTION_DECL)
2348 if (DECL_CONSTRUCTOR_P (fn))
2349 dump_type (pp, DECL_CONTEXT (fn), flags);
2350 else
2351 dump_decl (pp, fn, 0);
2353 else
2354 dump_expr (pp, AGGR_INIT_EXPR_FN (t), 0);
2356 dump_aggr_init_expr_args (pp, t, flags, true);
2357 break;
2359 case CALL_EXPR:
2361 tree fn = CALL_EXPR_FN (t);
2362 bool skipfirst = false;
2364 /* Deal with internal functions. */
2365 if (fn == NULL_TREE)
2367 pp_string (pp, internal_fn_name (CALL_EXPR_IFN (t)));
2368 dump_call_expr_args (pp, t, flags, skipfirst);
2369 break;
2372 if (TREE_CODE (fn) == ADDR_EXPR)
2373 fn = TREE_OPERAND (fn, 0);
2375 /* Nobody is interested in seeing the guts of vcalls. */
2376 if (TREE_CODE (fn) == OBJ_TYPE_REF)
2377 fn = resolve_virtual_fun_from_obj_type_ref (fn);
2379 if (TREE_TYPE (fn) != NULL_TREE
2380 && NEXT_CODE (fn) == METHOD_TYPE
2381 && call_expr_nargs (t))
2383 tree ob = CALL_EXPR_ARG (t, 0);
2384 if (TREE_CODE (ob) == ADDR_EXPR)
2386 dump_expr (pp, TREE_OPERAND (ob, 0),
2387 flags | TFF_EXPR_IN_PARENS);
2388 pp_cxx_dot (pp);
2390 else if (!is_this_parameter (ob))
2392 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2393 pp_cxx_arrow (pp);
2395 skipfirst = true;
2397 if (flag_sanitize & SANITIZE_UNDEFINED
2398 && is_ubsan_builtin_p (fn))
2400 pp_string (cxx_pp, M_("<ubsan routine call>"));
2401 break;
2403 dump_expr (pp, fn, flags | TFF_EXPR_IN_PARENS);
2404 dump_call_expr_args (pp, t, flags, skipfirst);
2406 break;
2408 case TARGET_EXPR:
2409 /* Note that this only works for G++ target exprs. If somebody
2410 builds a general TARGET_EXPR, there's no way to represent that
2411 it initializes anything other that the parameter slot for the
2412 default argument. Note we may have cleared out the first
2413 operand in expand_expr, so don't go killing ourselves. */
2414 if (TREE_OPERAND (t, 1))
2415 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2416 break;
2418 case POINTER_PLUS_EXPR:
2419 dump_binary_op (pp, "+", t, flags);
2420 break;
2422 case POINTER_DIFF_EXPR:
2423 dump_binary_op (pp, "-", t, flags);
2424 break;
2426 case INIT_EXPR:
2427 case MODIFY_EXPR:
2428 dump_binary_op (pp, OVL_OP_INFO (true, NOP_EXPR)->name, t, flags);
2429 break;
2431 case PLUS_EXPR:
2432 case MINUS_EXPR:
2433 case MULT_EXPR:
2434 case TRUNC_DIV_EXPR:
2435 case TRUNC_MOD_EXPR:
2436 case MIN_EXPR:
2437 case MAX_EXPR:
2438 case LSHIFT_EXPR:
2439 case RSHIFT_EXPR:
2440 case BIT_IOR_EXPR:
2441 case BIT_XOR_EXPR:
2442 case BIT_AND_EXPR:
2443 case TRUTH_ANDIF_EXPR:
2444 case TRUTH_ORIF_EXPR:
2445 case LT_EXPR:
2446 case LE_EXPR:
2447 case GT_EXPR:
2448 case GE_EXPR:
2449 case EQ_EXPR:
2450 case NE_EXPR:
2451 case SPACESHIP_EXPR:
2452 case EXACT_DIV_EXPR:
2453 dump_binary_op (pp, OVL_OP_INFO (false, TREE_CODE (t))->name, t, flags);
2454 break;
2456 case CEIL_DIV_EXPR:
2457 case FLOOR_DIV_EXPR:
2458 case ROUND_DIV_EXPR:
2459 case RDIV_EXPR:
2460 dump_binary_op (pp, "/", t, flags);
2461 break;
2463 case CEIL_MOD_EXPR:
2464 case FLOOR_MOD_EXPR:
2465 case ROUND_MOD_EXPR:
2466 dump_binary_op (pp, "%", t, flags);
2467 break;
2469 case COMPONENT_REF:
2471 tree ob = TREE_OPERAND (t, 0);
2472 if (INDIRECT_REF_P (ob))
2474 ob = TREE_OPERAND (ob, 0);
2475 if (!is_this_parameter (ob)
2476 && !is_dummy_object (ob))
2478 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2479 if (TYPE_REF_P (TREE_TYPE (ob)))
2480 pp_cxx_dot (pp);
2481 else
2482 pp_cxx_arrow (pp);
2485 else
2487 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2488 if (TREE_CODE (ob) != ARROW_EXPR)
2489 pp_cxx_dot (pp);
2491 dump_expr (pp, TREE_OPERAND (t, 1), flags & ~TFF_EXPR_IN_PARENS);
2493 break;
2495 case ARRAY_REF:
2496 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2497 pp_cxx_left_bracket (pp);
2498 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2499 pp_cxx_right_bracket (pp);
2500 break;
2502 case UNARY_PLUS_EXPR:
2503 dump_unary_op (pp, "+", t, flags);
2504 break;
2506 case ADDR_EXPR:
2507 if (TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
2508 || TREE_CODE (TREE_OPERAND (t, 0)) == STRING_CST
2509 /* An ADDR_EXPR can have reference type. In that case, we
2510 shouldn't print the `&' doing so indicates to the user
2511 that the expression has pointer type. */
2512 || (TREE_TYPE (t)
2513 && TYPE_REF_P (TREE_TYPE (t))))
2514 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2515 else if (TREE_CODE (TREE_OPERAND (t, 0)) == LABEL_DECL)
2516 dump_unary_op (pp, "&&", t, flags);
2517 else
2518 dump_unary_op (pp, "&", t, flags);
2519 break;
2521 case INDIRECT_REF:
2522 if (TREE_HAS_CONSTRUCTOR (t))
2524 t = TREE_OPERAND (t, 0);
2525 gcc_assert (TREE_CODE (t) == CALL_EXPR);
2526 dump_expr (pp, CALL_EXPR_FN (t), flags | TFF_EXPR_IN_PARENS);
2527 dump_call_expr_args (pp, t, flags, true);
2529 else
2531 if (TREE_OPERAND (t,0) != NULL_TREE
2532 && TREE_TYPE (TREE_OPERAND (t, 0))
2533 && NEXT_CODE (TREE_OPERAND (t, 0)) == REFERENCE_TYPE)
2534 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2535 else
2536 dump_unary_op (pp, "*", t, flags);
2538 break;
2540 case MEM_REF:
2541 /* Delegate to the base "C" pretty printer. */
2542 pp->c_pretty_printer::unary_expression (t);
2543 break;
2545 case TARGET_MEM_REF:
2546 /* TARGET_MEM_REF can't appear directly from source, but can appear
2547 during late GIMPLE optimizations and through late diagnostic we might
2548 need to support it. Print it as dereferencing of a pointer after
2549 cast to the TARGET_MEM_REF type, with pointer arithmetics on some
2550 pointer to single byte types, so
2551 *(type *)((char *) ptr + step * index + index2) if all the operands
2552 are present and the casts are needed. */
2553 pp_cxx_star (pp);
2554 pp_cxx_left_paren (pp);
2555 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (TMR_BASE (t)))) == NULL_TREE
2556 || !integer_onep (TYPE_SIZE_UNIT
2557 (TREE_TYPE (TREE_TYPE (TMR_BASE (t))))))
2559 if (TYPE_SIZE_UNIT (TREE_TYPE (t))
2560 && integer_onep (TYPE_SIZE_UNIT (TREE_TYPE (t))))
2562 pp_cxx_left_paren (pp);
2563 dump_type (pp, build_pointer_type (TREE_TYPE (t)), flags);
2565 else
2567 dump_type (pp, build_pointer_type (TREE_TYPE (t)), flags);
2568 pp_cxx_right_paren (pp);
2569 pp_cxx_left_paren (pp);
2570 pp_cxx_left_paren (pp);
2571 dump_type (pp, build_pointer_type (char_type_node), flags);
2573 pp_cxx_right_paren (pp);
2575 else if (!same_type_p (TREE_TYPE (t),
2576 TREE_TYPE (TREE_TYPE (TMR_BASE (t)))))
2578 dump_type (pp, build_pointer_type (TREE_TYPE (t)), flags);
2579 pp_cxx_right_paren (pp);
2580 pp_cxx_left_paren (pp);
2582 dump_expr (pp, TMR_BASE (t), flags);
2583 if (TMR_STEP (t) && TMR_INDEX (t))
2585 pp_cxx_ws_string (pp, "+");
2586 dump_expr (pp, TMR_INDEX (t), flags);
2587 pp_cxx_ws_string (pp, "*");
2588 dump_expr (pp, TMR_STEP (t), flags);
2590 if (TMR_INDEX2 (t))
2592 pp_cxx_ws_string (pp, "+");
2593 dump_expr (pp, TMR_INDEX2 (t), flags);
2595 if (!integer_zerop (TMR_OFFSET (t)))
2597 pp_cxx_ws_string (pp, "+");
2598 dump_expr (pp, fold_convert (ssizetype, TMR_OFFSET (t)), flags);
2600 pp_cxx_right_paren (pp);
2601 break;
2603 case NEGATE_EXPR:
2604 case BIT_NOT_EXPR:
2605 case TRUTH_NOT_EXPR:
2606 case PREDECREMENT_EXPR:
2607 case PREINCREMENT_EXPR:
2608 dump_unary_op (pp, OVL_OP_INFO (false, TREE_CODE (t))->name, t, flags);
2609 break;
2611 case POSTDECREMENT_EXPR:
2612 case POSTINCREMENT_EXPR:
2613 pp_cxx_left_paren (pp);
2614 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2615 pp_cxx_ws_string (pp, OVL_OP_INFO (false, TREE_CODE (t))->name);
2616 pp_cxx_right_paren (pp);
2617 break;
2619 case NON_LVALUE_EXPR:
2620 /* FIXME: This is a KLUDGE workaround for a parsing problem. There
2621 should be another level of INDIRECT_REF so that I don't have to do
2622 this. */
2623 if (TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == POINTER_TYPE)
2625 tree next = TREE_TYPE (TREE_TYPE (t));
2627 while (TYPE_PTR_P (next))
2628 next = TREE_TYPE (next);
2630 if (TREE_CODE (next) == FUNCTION_TYPE)
2632 if (flags & TFF_EXPR_IN_PARENS)
2633 pp_cxx_left_paren (pp);
2634 pp_cxx_star (pp);
2635 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2636 if (flags & TFF_EXPR_IN_PARENS)
2637 pp_cxx_right_paren (pp);
2638 break;
2640 /* Else fall through. */
2642 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2643 break;
2645 CASE_CONVERT:
2646 case IMPLICIT_CONV_EXPR:
2647 case VIEW_CONVERT_EXPR:
2649 tree op = TREE_OPERAND (t, 0);
2651 if (location_wrapper_p (t))
2653 dump_expr (pp, op, flags);
2654 break;
2657 tree ttype = TREE_TYPE (t);
2658 tree optype = TREE_TYPE (op);
2660 if (TREE_CODE (ttype) != TREE_CODE (optype)
2661 && INDIRECT_TYPE_P (ttype)
2662 && INDIRECT_TYPE_P (optype)
2663 && same_type_p (TREE_TYPE (optype),
2664 TREE_TYPE (ttype)))
2666 if (TYPE_REF_P (ttype))
2668 STRIP_NOPS (op);
2669 if (TREE_CODE (op) == ADDR_EXPR)
2670 dump_expr (pp, TREE_OPERAND (op, 0), flags);
2671 else
2672 dump_unary_op (pp, "*", t, flags);
2674 else
2675 dump_unary_op (pp, "&", t, flags);
2677 else if (!same_type_p (TREE_TYPE (op), TREE_TYPE (t)))
2679 /* It is a cast, but we cannot tell whether it is a
2680 reinterpret or static cast. Use the C style notation. */
2681 if (flags & TFF_EXPR_IN_PARENS)
2682 pp_cxx_left_paren (pp);
2683 pp_cxx_left_paren (pp);
2684 dump_type (pp, TREE_TYPE (t), flags);
2685 pp_cxx_right_paren (pp);
2686 dump_expr (pp, op, flags | TFF_EXPR_IN_PARENS);
2687 if (flags & TFF_EXPR_IN_PARENS)
2688 pp_cxx_right_paren (pp);
2690 else
2691 dump_expr (pp, op, flags);
2692 break;
2695 case CONSTRUCTOR:
2696 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
2698 tree idx = build_ptrmemfunc_access_expr (t, pfn_identifier);
2700 if (integer_zerop (idx))
2702 /* A NULL pointer-to-member constant. */
2703 pp_cxx_left_paren (pp);
2704 pp_cxx_left_paren (pp);
2705 dump_type (pp, TREE_TYPE (t), flags);
2706 pp_cxx_right_paren (pp);
2707 pp_character (pp, '0');
2708 pp_cxx_right_paren (pp);
2709 break;
2711 else if (tree_fits_shwi_p (idx))
2713 tree virtuals;
2714 unsigned HOST_WIDE_INT n;
2716 t = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
2717 t = TYPE_METHOD_BASETYPE (t);
2718 virtuals = BINFO_VIRTUALS (TYPE_BINFO (TYPE_MAIN_VARIANT (t)));
2720 n = tree_to_shwi (idx);
2722 /* Map vtable index back one, to allow for the null pointer to
2723 member. */
2724 --n;
2726 while (n > 0 && virtuals)
2728 --n;
2729 virtuals = TREE_CHAIN (virtuals);
2731 if (virtuals)
2733 dump_expr (pp, BV_FN (virtuals),
2734 flags | TFF_EXPR_IN_PARENS);
2735 break;
2739 if (TREE_TYPE (t) && LAMBDA_TYPE_P (TREE_TYPE (t)))
2740 pp_string (pp, "<lambda closure object>");
2741 if (TREE_TYPE (t) && EMPTY_CONSTRUCTOR_P (t))
2743 dump_type (pp, TREE_TYPE (t), 0);
2744 pp_cxx_left_paren (pp);
2745 pp_cxx_right_paren (pp);
2747 else
2749 if (!BRACE_ENCLOSED_INITIALIZER_P (t))
2750 dump_type (pp, TREE_TYPE (t), 0);
2751 pp_cxx_left_brace (pp);
2752 dump_expr_init_vec (pp, CONSTRUCTOR_ELTS (t), flags);
2753 pp_cxx_right_brace (pp);
2756 break;
2758 case OFFSET_REF:
2760 tree ob = TREE_OPERAND (t, 0);
2761 if (is_dummy_object (ob))
2763 t = TREE_OPERAND (t, 1);
2764 if (TREE_CODE (t) == FUNCTION_DECL)
2765 /* A::f */
2766 dump_expr (pp, t, flags | TFF_EXPR_IN_PARENS);
2767 else if (BASELINK_P (t))
2768 dump_expr (pp, OVL_FIRST (BASELINK_FUNCTIONS (t)),
2769 flags | TFF_EXPR_IN_PARENS);
2770 else
2771 dump_decl (pp, t, flags);
2773 else
2775 if (INDIRECT_REF_P (ob))
2777 dump_expr (pp, TREE_OPERAND (ob, 0), flags | TFF_EXPR_IN_PARENS);
2778 pp_cxx_arrow (pp);
2779 pp_cxx_star (pp);
2781 else
2783 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2784 pp_cxx_dot (pp);
2785 pp_cxx_star (pp);
2787 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2789 break;
2792 case TEMPLATE_PARM_INDEX:
2793 dump_decl (pp, TEMPLATE_PARM_DECL (t), flags & ~TFF_DECL_SPECIFIERS);
2794 break;
2796 case CAST_EXPR:
2797 if (TREE_OPERAND (t, 0) == NULL_TREE
2798 || TREE_CHAIN (TREE_OPERAND (t, 0)))
2800 dump_type (pp, TREE_TYPE (t), flags);
2801 pp_cxx_left_paren (pp);
2802 dump_expr_list (pp, TREE_OPERAND (t, 0), flags);
2803 pp_cxx_right_paren (pp);
2805 else
2807 pp_cxx_left_paren (pp);
2808 dump_type (pp, TREE_TYPE (t), flags);
2809 pp_cxx_right_paren (pp);
2810 pp_cxx_left_paren (pp);
2811 dump_expr_list (pp, TREE_OPERAND (t, 0), flags);
2812 pp_cxx_right_paren (pp);
2814 break;
2816 case STATIC_CAST_EXPR:
2817 pp_cxx_ws_string (pp, "static_cast");
2818 goto cast;
2819 case REINTERPRET_CAST_EXPR:
2820 pp_cxx_ws_string (pp, "reinterpret_cast");
2821 goto cast;
2822 case CONST_CAST_EXPR:
2823 pp_cxx_ws_string (pp, "const_cast");
2824 goto cast;
2825 case DYNAMIC_CAST_EXPR:
2826 pp_cxx_ws_string (pp, "dynamic_cast");
2827 cast:
2828 pp_cxx_begin_template_argument_list (pp);
2829 dump_type (pp, TREE_TYPE (t), flags);
2830 pp_cxx_end_template_argument_list (pp);
2831 pp_cxx_left_paren (pp);
2832 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2833 pp_cxx_right_paren (pp);
2834 break;
2836 case ARROW_EXPR:
2837 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2838 pp_cxx_arrow (pp);
2839 break;
2841 case SIZEOF_EXPR:
2842 case ALIGNOF_EXPR:
2843 if (TREE_CODE (t) == SIZEOF_EXPR)
2844 pp_cxx_ws_string (pp, "sizeof");
2845 else if (ALIGNOF_EXPR_STD_P (t))
2846 pp_cxx_ws_string (pp, "alignof");
2847 else
2848 pp_cxx_ws_string (pp, "__alignof__");
2849 op = TREE_OPERAND (t, 0);
2850 if (PACK_EXPANSION_P (op))
2852 pp_string (pp, "...");
2853 op = PACK_EXPANSION_PATTERN (op);
2855 pp_cxx_whitespace (pp);
2856 pp_cxx_left_paren (pp);
2857 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
2858 dump_type (pp, TREE_TYPE (op), flags);
2859 else if (TYPE_P (TREE_OPERAND (t, 0)))
2860 dump_type (pp, op, flags);
2861 else
2862 dump_expr (pp, op, flags);
2863 pp_cxx_right_paren (pp);
2864 break;
2866 case AT_ENCODE_EXPR:
2867 pp_cxx_ws_string (pp, "@encode");
2868 pp_cxx_whitespace (pp);
2869 pp_cxx_left_paren (pp);
2870 dump_type (pp, TREE_OPERAND (t, 0), flags);
2871 pp_cxx_right_paren (pp);
2872 break;
2874 case NOEXCEPT_EXPR:
2875 pp_cxx_ws_string (pp, "noexcept");
2876 pp_cxx_whitespace (pp);
2877 pp_cxx_left_paren (pp);
2878 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2879 pp_cxx_right_paren (pp);
2880 break;
2882 case REALPART_EXPR:
2883 case IMAGPART_EXPR:
2884 pp_cxx_ws_string (pp, OVL_OP_INFO (false, TREE_CODE (t))->name);
2885 pp_cxx_whitespace (pp);
2886 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2887 break;
2889 case DEFERRED_PARSE:
2890 pp_string (pp, M_("<unparsed>"));
2891 break;
2893 case TRY_CATCH_EXPR:
2894 case CLEANUP_POINT_EXPR:
2895 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2896 break;
2898 case PSEUDO_DTOR_EXPR:
2899 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2900 pp_cxx_dot (pp);
2901 if (TREE_OPERAND (t, 1))
2903 dump_type (pp, TREE_OPERAND (t, 1), flags);
2904 pp_cxx_colon_colon (pp);
2906 pp_cxx_complement (pp);
2907 dump_type (pp, TREE_OPERAND (t, 2), flags);
2908 break;
2910 case TEMPLATE_ID_EXPR:
2911 dump_decl (pp, t, flags);
2912 break;
2914 case BIND_EXPR:
2915 case STMT_EXPR:
2916 case EXPR_STMT:
2917 case STATEMENT_LIST:
2918 /* We don't yet have a way of dumping statements in a
2919 human-readable format. */
2920 pp_string (pp, "({...})");
2921 break;
2923 case LOOP_EXPR:
2924 pp_string (pp, "while (1) { ");
2925 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2926 pp_cxx_right_brace (pp);
2927 break;
2929 case EXIT_EXPR:
2930 pp_string (pp, "if (");
2931 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2932 pp_string (pp, ") break; ");
2933 break;
2935 case BASELINK:
2936 dump_expr (pp, BASELINK_FUNCTIONS (t), flags & ~TFF_EXPR_IN_PARENS);
2937 break;
2939 case EMPTY_CLASS_EXPR:
2940 dump_type (pp, TREE_TYPE (t), flags);
2941 pp_cxx_left_paren (pp);
2942 pp_cxx_right_paren (pp);
2943 break;
2945 case NON_DEPENDENT_EXPR:
2946 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2947 break;
2949 case ARGUMENT_PACK_SELECT:
2950 dump_template_argument (pp, ARGUMENT_PACK_SELECT_FROM_PACK (t), flags);
2951 break;
2953 case RECORD_TYPE:
2954 case UNION_TYPE:
2955 case ENUMERAL_TYPE:
2956 case REAL_TYPE:
2957 case VOID_TYPE:
2958 case OPAQUE_TYPE:
2959 case BOOLEAN_TYPE:
2960 case INTEGER_TYPE:
2961 case COMPLEX_TYPE:
2962 case VECTOR_TYPE:
2963 case DECLTYPE_TYPE:
2964 pp_type_specifier_seq (pp, t);
2965 break;
2967 case TYPENAME_TYPE:
2968 /* We get here when we want to print a dependent type as an
2969 id-expression, without any disambiguator decoration. */
2970 pp->id_expression (t);
2971 break;
2973 case TEMPLATE_TYPE_PARM:
2974 case TEMPLATE_TEMPLATE_PARM:
2975 case BOUND_TEMPLATE_TEMPLATE_PARM:
2976 dump_type (pp, t, flags);
2977 break;
2979 case TRAIT_EXPR:
2980 pp_cxx_trait (pp, t);
2981 break;
2983 case VA_ARG_EXPR:
2984 pp_cxx_va_arg_expression (pp, t);
2985 break;
2987 case OFFSETOF_EXPR:
2988 pp_cxx_offsetof_expression (pp, t);
2989 break;
2991 case ADDRESSOF_EXPR:
2992 pp_cxx_addressof_expression (pp, t);
2993 break;
2995 case SCOPE_REF:
2996 dump_decl (pp, t, flags);
2997 break;
2999 case EXPR_PACK_EXPANSION:
3000 case UNARY_LEFT_FOLD_EXPR:
3001 case UNARY_RIGHT_FOLD_EXPR:
3002 case BINARY_LEFT_FOLD_EXPR:
3003 case BINARY_RIGHT_FOLD_EXPR:
3004 case TYPEID_EXPR:
3005 case MEMBER_REF:
3006 case DOTSTAR_EXPR:
3007 case NEW_EXPR:
3008 case VEC_NEW_EXPR:
3009 case DELETE_EXPR:
3010 case VEC_DELETE_EXPR:
3011 case MODOP_EXPR:
3012 case ABS_EXPR:
3013 case ABSU_EXPR:
3014 case CONJ_EXPR:
3015 case VECTOR_CST:
3016 case FIXED_CST:
3017 case UNORDERED_EXPR:
3018 case ORDERED_EXPR:
3019 case UNLT_EXPR:
3020 case UNLE_EXPR:
3021 case UNGT_EXPR:
3022 case UNGE_EXPR:
3023 case UNEQ_EXPR:
3024 case LTGT_EXPR:
3025 case COMPLEX_EXPR:
3026 case BIT_FIELD_REF:
3027 case FIX_TRUNC_EXPR:
3028 case FLOAT_EXPR:
3029 pp->expression (t);
3030 break;
3032 case TRUTH_AND_EXPR:
3033 case TRUTH_OR_EXPR:
3034 case TRUTH_XOR_EXPR:
3035 if (flags & TFF_EXPR_IN_PARENS)
3036 pp_cxx_left_paren (pp);
3037 pp->expression (t);
3038 if (flags & TFF_EXPR_IN_PARENS)
3039 pp_cxx_right_paren (pp);
3040 break;
3042 case OBJ_TYPE_REF:
3043 dump_expr (pp, resolve_virtual_fun_from_obj_type_ref (t), flags);
3044 break;
3046 case LAMBDA_EXPR:
3047 pp_string (pp, M_("<lambda>"));
3048 break;
3050 case PAREN_EXPR:
3051 pp_cxx_left_paren (pp);
3052 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
3053 pp_cxx_right_paren (pp);
3054 break;
3056 case REQUIRES_EXPR:
3057 pp_cxx_requires_expr (cxx_pp, t);
3058 break;
3060 case SIMPLE_REQ:
3061 pp_cxx_simple_requirement (cxx_pp, t);
3062 break;
3064 case TYPE_REQ:
3065 pp_cxx_type_requirement (cxx_pp, t);
3066 break;
3068 case COMPOUND_REQ:
3069 pp_cxx_compound_requirement (cxx_pp, t);
3070 break;
3072 case NESTED_REQ:
3073 pp_cxx_nested_requirement (cxx_pp, t);
3074 break;
3076 case ATOMIC_CONSTR:
3077 case CHECK_CONSTR:
3078 case CONJ_CONSTR:
3079 case DISJ_CONSTR:
3081 pp_cxx_constraint (cxx_pp, t);
3082 break;
3085 case PLACEHOLDER_EXPR:
3086 pp_string (pp, M_("*this"));
3087 break;
3089 case TREE_LIST:
3090 dump_expr_list (pp, t, flags);
3091 break;
3093 /* This list is incomplete, but should suffice for now.
3094 It is very important that `sorry' does not call
3095 `report_error_function'. That could cause an infinite loop. */
3096 default:
3097 pp_unsupported_tree (pp, t);
3098 /* Fall through. */
3099 case ERROR_MARK:
3100 pp_string (pp, M_("<expression error>"));
3101 break;
3105 static void
3106 dump_binary_op (cxx_pretty_printer *pp, const char *opstring, tree t,
3107 int flags)
3109 pp_cxx_left_paren (pp);
3110 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
3111 pp_cxx_whitespace (pp);
3112 if (opstring)
3113 pp_cxx_ws_string (pp, opstring);
3114 else
3115 pp_string (pp, M_("<unknown operator>"));
3116 pp_cxx_whitespace (pp);
3117 tree op1 = TREE_OPERAND (t, 1);
3118 if (TREE_CODE (t) == POINTER_PLUS_EXPR
3119 && TREE_CODE (op1) == INTEGER_CST
3120 && tree_int_cst_sign_bit (op1))
3121 /* A pointer minus an integer is represented internally as plus a very
3122 large number, don't expose that to users. */
3123 op1 = convert (ssizetype, op1);
3124 dump_expr (pp, op1, flags | TFF_EXPR_IN_PARENS);
3125 pp_cxx_right_paren (pp);
3128 static void
3129 dump_unary_op (cxx_pretty_printer *pp, const char *opstring, tree t, int flags)
3131 if (flags & TFF_EXPR_IN_PARENS)
3132 pp_cxx_left_paren (pp);
3133 pp_cxx_ws_string (pp, opstring);
3134 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
3135 if (flags & TFF_EXPR_IN_PARENS)
3136 pp_cxx_right_paren (pp);
3139 static void
3140 reinit_cxx_pp (void)
3142 pp_clear_output_area (cxx_pp);
3143 cxx_pp->padding = pp_none;
3144 pp_indentation (cxx_pp) = 0;
3145 pp_needs_newline (cxx_pp) = false;
3146 pp_show_color (cxx_pp) = false;
3147 cxx_pp->enclosing_scope = current_function_decl;
3150 /* Same as pp_formatted_text, except the return string is a separate
3151 copy and has a GGC storage duration, e.g. an indefinite lifetime. */
3153 inline const char *
3154 pp_ggc_formatted_text (pretty_printer *pp)
3156 return ggc_strdup (pp_formatted_text (pp));
3159 /* Exported interface to stringifying types, exprs and decls under TFF_*
3160 control. */
3162 const char *
3163 type_as_string (tree typ, int flags)
3165 reinit_cxx_pp ();
3166 pp_translate_identifiers (cxx_pp) = false;
3167 dump_type (cxx_pp, typ, flags);
3168 return pp_ggc_formatted_text (cxx_pp);
3171 const char *
3172 type_as_string_translate (tree typ, int flags)
3174 reinit_cxx_pp ();
3175 dump_type (cxx_pp, typ, flags);
3176 return pp_ggc_formatted_text (cxx_pp);
3179 const char *
3180 expr_as_string (tree decl, int flags)
3182 reinit_cxx_pp ();
3183 pp_translate_identifiers (cxx_pp) = false;
3184 dump_expr (cxx_pp, decl, flags);
3185 return pp_ggc_formatted_text (cxx_pp);
3188 /* Wrap decl_as_string with options appropriate for dwarf. */
3190 const char *
3191 decl_as_dwarf_string (tree decl, int flags)
3193 const char *name;
3194 /* Curiously, reinit_cxx_pp doesn't reset the flags field, so setting the flag
3195 here will be adequate to get the desired behavior. */
3196 cxx_pp->flags |= pp_c_flag_gnu_v3;
3197 name = decl_as_string (decl, flags);
3198 /* Subsequent calls to the pretty printer shouldn't use this style. */
3199 cxx_pp->flags &= ~pp_c_flag_gnu_v3;
3200 return name;
3203 const char *
3204 decl_as_string (tree decl, int flags)
3206 reinit_cxx_pp ();
3207 pp_translate_identifiers (cxx_pp) = false;
3208 dump_decl (cxx_pp, decl, flags);
3209 return pp_ggc_formatted_text (cxx_pp);
3212 const char *
3213 decl_as_string_translate (tree decl, int flags)
3215 reinit_cxx_pp ();
3216 dump_decl (cxx_pp, decl, flags);
3217 return pp_ggc_formatted_text (cxx_pp);
3220 /* Wrap lang_decl_name with options appropriate for dwarf. */
3222 const char *
3223 lang_decl_dwarf_name (tree decl, int v, bool translate)
3225 const char *name;
3226 /* Curiously, reinit_cxx_pp doesn't reset the flags field, so setting the flag
3227 here will be adequate to get the desired behavior. */
3228 cxx_pp->flags |= pp_c_flag_gnu_v3;
3229 name = lang_decl_name (decl, v, translate);
3230 /* Subsequent calls to the pretty printer shouldn't use this style. */
3231 cxx_pp->flags &= ~pp_c_flag_gnu_v3;
3232 return name;
3235 /* Generate the three forms of printable names for cxx_printable_name. */
3237 const char *
3238 lang_decl_name (tree decl, int v, bool translate)
3240 if (v >= 2)
3241 return (translate
3242 ? decl_as_string_translate (decl, TFF_DECL_SPECIFIERS)
3243 : decl_as_string (decl, TFF_DECL_SPECIFIERS));
3245 reinit_cxx_pp ();
3246 pp_translate_identifiers (cxx_pp) = translate;
3247 if (v == 1
3248 && (DECL_CLASS_SCOPE_P (decl)
3249 || (DECL_NAMESPACE_SCOPE_P (decl)
3250 && CP_DECL_CONTEXT (decl) != global_namespace)))
3252 dump_type (cxx_pp, CP_DECL_CONTEXT (decl), TFF_PLAIN_IDENTIFIER);
3253 pp_cxx_colon_colon (cxx_pp);
3256 if (TREE_CODE (decl) == FUNCTION_DECL)
3257 dump_function_name (cxx_pp, decl, TFF_PLAIN_IDENTIFIER);
3258 else if ((DECL_NAME (decl) == NULL_TREE)
3259 && TREE_CODE (decl) == NAMESPACE_DECL)
3260 dump_decl (cxx_pp, decl, TFF_PLAIN_IDENTIFIER | TFF_UNQUALIFIED_NAME);
3261 else
3262 dump_decl (cxx_pp, DECL_NAME (decl), TFF_PLAIN_IDENTIFIER);
3264 return pp_ggc_formatted_text (cxx_pp);
3267 /* Return the location of a tree passed to %+ formats. */
3269 location_t
3270 location_of (tree t)
3272 if (TYPE_P (t))
3274 t = TYPE_MAIN_DECL (t);
3275 if (t == NULL_TREE)
3276 return input_location;
3278 else if (TREE_CODE (t) == OVERLOAD)
3279 t = OVL_FIRST (t);
3281 if (DECL_P (t))
3282 return DECL_SOURCE_LOCATION (t);
3283 if (TREE_CODE (t) == DEFERRED_PARSE)
3284 return defparse_location (t);
3285 return cp_expr_loc_or_input_loc (t);
3288 /* Now the interfaces from error et al to dump_type et al. Each takes an
3289 on/off VERBOSE flag and supply the appropriate TFF_ flags to a dump_
3290 function. */
3292 static const char *
3293 decl_to_string (tree decl, int verbose, bool show_color)
3295 int flags = 0;
3297 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == RECORD_TYPE
3298 || TREE_CODE (decl) == UNION_TYPE || TREE_CODE (decl) == ENUMERAL_TYPE)
3299 flags = TFF_CLASS_KEY_OR_ENUM;
3300 if (verbose)
3301 flags |= TFF_DECL_SPECIFIERS;
3302 else if (TREE_CODE (decl) == FUNCTION_DECL)
3303 flags |= TFF_DECL_SPECIFIERS | TFF_RETURN_TYPE;
3304 flags |= TFF_TEMPLATE_HEADER;
3306 reinit_cxx_pp ();
3307 pp_show_color (cxx_pp) = show_color;
3308 dump_decl (cxx_pp, decl, flags);
3309 return pp_ggc_formatted_text (cxx_pp);
3312 const char *
3313 expr_to_string (tree decl)
3315 reinit_cxx_pp ();
3316 dump_expr (cxx_pp, decl, 0);
3317 return pp_ggc_formatted_text (cxx_pp);
3320 static const char *
3321 fndecl_to_string (tree fndecl, int verbose)
3323 int flags;
3325 flags = TFF_EXCEPTION_SPECIFICATION | TFF_DECL_SPECIFIERS
3326 | TFF_TEMPLATE_HEADER;
3327 if (verbose)
3328 flags |= TFF_FUNCTION_DEFAULT_ARGUMENTS;
3329 reinit_cxx_pp ();
3330 dump_decl (cxx_pp, fndecl, flags);
3331 return pp_ggc_formatted_text (cxx_pp);
3335 static const char *
3336 code_to_string (enum tree_code c)
3338 return get_tree_code_name (c);
3341 const char *
3342 language_to_string (enum languages c)
3344 switch (c)
3346 case lang_c:
3347 return "C";
3349 case lang_cplusplus:
3350 return "C++";
3352 default:
3353 gcc_unreachable ();
3355 return NULL;
3358 /* Return the proper printed version of a parameter to a C++ function. */
3360 static const char *
3361 parm_to_string (int p)
3363 reinit_cxx_pp ();
3364 if (p < 0)
3365 pp_string (cxx_pp, "'this'");
3366 else
3367 pp_decimal_int (cxx_pp, p + 1);
3368 return pp_ggc_formatted_text (cxx_pp);
3371 static const char *
3372 op_to_string (bool assop, enum tree_code p)
3374 tree id = ovl_op_identifier (assop, p);
3375 return id ? IDENTIFIER_POINTER (id) : M_("<unknown>");
3378 /* Return a GC-allocated representation of type TYP, with verbosity VERBOSE.
3380 If QUOTE is non-NULL and if *QUOTE is true, then quotes are added to the
3381 string in appropriate places, and *QUOTE is written to with false
3382 to suppress pp_format's trailing close quote so that e.g.
3383 foo_typedef {aka underlying_foo} {enum}
3384 can be printed by "%qT" as:
3385 `foo_typedef' {aka `underlying_foo'} {enum}
3386 rather than:
3387 `foo_typedef {aka underlying_foo} {enum}'
3388 When adding such quotes, if POSTPROCESSED is true (for handling %H and %I)
3389 then a leading open quote will be added, whereas if POSTPROCESSED is false
3390 (for handling %T) then any leading quote has already been added by
3391 pp_format, or is not needed due to QUOTE being NULL (for template arguments
3392 within %H and %I).
3394 SHOW_COLOR is used to determine the colorization of any quotes that
3395 are added. */
3397 static const char *
3398 type_to_string (tree typ, int verbose, bool postprocessed, bool *quote,
3399 bool show_color)
3401 int flags = 0;
3402 if (verbose)
3403 flags |= TFF_CLASS_KEY_OR_ENUM;
3404 flags |= TFF_TEMPLATE_HEADER;
3406 reinit_cxx_pp ();
3407 pp_show_color (cxx_pp) = show_color;
3409 if (postprocessed && quote && *quote)
3410 pp_begin_quote (cxx_pp, show_color);
3412 struct obstack *ob = pp_buffer (cxx_pp)->obstack;
3413 int type_start, type_len;
3414 type_start = obstack_object_size (ob);
3416 dump_type (cxx_pp, typ, flags);
3418 /* Remember the end of the initial dump. */
3419 type_len = obstack_object_size (ob) - type_start;
3421 /* If we're printing a type that involves typedefs, also print the
3422 stripped version. But sometimes the stripped version looks
3423 exactly the same, so we don't want it after all. To avoid printing
3424 it in that case, we play ugly obstack games. */
3425 if (typ && TYPE_P (typ) && typ != TYPE_CANONICAL (typ)
3426 && !uses_template_parms (typ))
3428 int aka_start, aka_len; char *p;
3429 tree aka = strip_typedefs (typ, NULL, STF_USER_VISIBLE);
3430 if (quote && *quote)
3431 pp_end_quote (cxx_pp, show_color);
3432 pp_string (cxx_pp, " {aka");
3433 pp_cxx_whitespace (cxx_pp);
3434 if (quote && *quote)
3435 pp_begin_quote (cxx_pp, show_color);
3436 /* And remember the start of the aka dump. */
3437 aka_start = obstack_object_size (ob);
3438 dump_type (cxx_pp, aka, flags);
3439 aka_len = obstack_object_size (ob) - aka_start;
3440 if (quote && *quote)
3441 pp_end_quote (cxx_pp, show_color);
3442 pp_right_brace (cxx_pp);
3443 p = (char*)obstack_base (ob);
3444 /* If they are identical, cut off the aka by unwinding the obstack. */
3445 if (type_len == aka_len
3446 && memcmp (p + type_start, p+aka_start, type_len) == 0)
3448 /* We can't add a '\0' here, since we may be adding a closing quote
3449 below, and it would be hidden by the '\0'.
3450 Instead, manually unwind the current object within the obstack
3451 so that the insertion point is at the end of the type, before
3452 the "' {aka". */
3453 int delta = type_start + type_len - obstack_object_size (ob);
3454 gcc_assert (delta <= 0);
3455 obstack_blank_fast (ob, delta);
3457 else
3458 if (quote)
3459 /* No further closing quotes are needed. */
3460 *quote = false;
3463 if (quote && *quote)
3465 pp_end_quote (cxx_pp, show_color);
3466 *quote = false;
3468 return pp_ggc_formatted_text (cxx_pp);
3471 static const char *
3472 args_to_string (tree p, int verbose)
3474 int flags = 0;
3475 if (verbose)
3476 flags |= TFF_CLASS_KEY_OR_ENUM;
3478 if (p == NULL_TREE)
3479 return "";
3481 if (TYPE_P (TREE_VALUE (p)))
3482 return type_as_string_translate (p, flags);
3484 reinit_cxx_pp ();
3485 for (; p; p = TREE_CHAIN (p))
3487 if (null_node_p (TREE_VALUE (p)))
3488 pp_cxx_ws_string (cxx_pp, "NULL");
3489 else
3490 dump_type (cxx_pp, error_type (TREE_VALUE (p)), flags);
3491 if (TREE_CHAIN (p))
3492 pp_separate_with_comma (cxx_pp);
3494 return pp_ggc_formatted_text (cxx_pp);
3497 /* Pretty-print a deduction substitution (from deduction_tsubst_fntype). P
3498 is a TREE_LIST with purpose the TEMPLATE_DECL, value the template
3499 arguments. */
3501 static const char *
3502 subst_to_string (tree p, bool show_color)
3504 tree decl = TREE_PURPOSE (p);
3505 tree targs = TREE_VALUE (p);
3506 tree tparms = DECL_TEMPLATE_PARMS (decl);
3507 int flags = (TFF_DECL_SPECIFIERS|TFF_TEMPLATE_HEADER
3508 |TFF_NO_TEMPLATE_BINDINGS);
3510 if (p == NULL_TREE)
3511 return "";
3513 reinit_cxx_pp ();
3514 pp_show_color (cxx_pp) = show_color;
3515 dump_template_decl (cxx_pp, TREE_PURPOSE (p), flags);
3516 dump_substitution (cxx_pp, NULL, tparms, targs, /*flags=*/0);
3517 return pp_ggc_formatted_text (cxx_pp);
3520 static const char *
3521 cv_to_string (tree p, int v)
3523 reinit_cxx_pp ();
3524 cxx_pp->padding = v ? pp_before : pp_none;
3525 pp_cxx_cv_qualifier_seq (cxx_pp, p);
3526 return pp_ggc_formatted_text (cxx_pp);
3529 static const char *
3530 eh_spec_to_string (tree p, int /*v*/)
3532 int flags = 0;
3533 reinit_cxx_pp ();
3534 dump_exception_spec (cxx_pp, p, flags);
3535 return pp_ggc_formatted_text (cxx_pp);
3538 /* Langhook for print_error_function. */
3539 void
3540 cxx_print_error_function (diagnostic_context *context, const char *file,
3541 diagnostic_info *diagnostic)
3543 char *prefix;
3544 if (file)
3545 prefix = xstrdup (file);
3546 else
3547 prefix = NULL;
3548 lhd_print_error_function (context, file, diagnostic);
3549 pp_set_prefix (context->printer, prefix);
3550 maybe_print_instantiation_context (context);
3553 static void
3554 cp_diagnostic_starter (diagnostic_context *context,
3555 diagnostic_info *diagnostic)
3557 diagnostic_report_current_module (context, diagnostic_location (diagnostic));
3558 cp_print_error_function (context, diagnostic);
3559 maybe_print_instantiation_context (context);
3560 maybe_print_constexpr_context (context);
3561 maybe_print_constraint_context (context);
3562 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
3563 diagnostic));
3566 /* Print current function onto BUFFER, in the process of reporting
3567 a diagnostic message. Called from cp_diagnostic_starter. */
3568 static void
3569 cp_print_error_function (diagnostic_context *context,
3570 diagnostic_info *diagnostic)
3572 /* If we are in an instantiation context, current_function_decl is likely
3573 to be wrong, so just rely on print_instantiation_full_context. */
3574 if (current_instantiation ())
3575 return;
3576 /* The above is true for constraint satisfaction also. */
3577 if (current_failed_constraint)
3578 return;
3579 if (diagnostic_last_function_changed (context, diagnostic))
3581 char *old_prefix = pp_take_prefix (context->printer);
3582 const char *file = LOCATION_FILE (diagnostic_location (diagnostic));
3583 tree abstract_origin = diagnostic_abstract_origin (diagnostic);
3584 char *new_prefix = (file && abstract_origin == NULL)
3585 ? file_name_as_prefix (context, file) : NULL;
3587 pp_set_prefix (context->printer, new_prefix);
3589 if (current_function_decl == NULL)
3590 pp_string (context->printer, _("At global scope:"));
3591 else
3593 tree fndecl, ao;
3595 if (abstract_origin)
3597 ao = BLOCK_ABSTRACT_ORIGIN (abstract_origin);
3598 gcc_assert (TREE_CODE (ao) == FUNCTION_DECL);
3599 fndecl = ao;
3601 else
3602 fndecl = current_function_decl;
3604 pp_printf (context->printer, function_category (fndecl),
3605 fndecl);
3607 while (abstract_origin)
3609 location_t *locus;
3610 tree block = abstract_origin;
3612 locus = &BLOCK_SOURCE_LOCATION (block);
3613 fndecl = NULL;
3614 block = BLOCK_SUPERCONTEXT (block);
3615 while (block && TREE_CODE (block) == BLOCK
3616 && BLOCK_ABSTRACT_ORIGIN (block))
3618 ao = BLOCK_ABSTRACT_ORIGIN (block);
3619 if (TREE_CODE (ao) == FUNCTION_DECL)
3621 fndecl = ao;
3622 break;
3624 else if (TREE_CODE (ao) != BLOCK)
3625 break;
3627 block = BLOCK_SUPERCONTEXT (block);
3629 if (fndecl)
3630 abstract_origin = block;
3631 else
3633 while (block && TREE_CODE (block) == BLOCK)
3634 block = BLOCK_SUPERCONTEXT (block);
3636 if (block && TREE_CODE (block) == FUNCTION_DECL)
3637 fndecl = block;
3638 abstract_origin = NULL;
3640 if (fndecl)
3642 expanded_location s = expand_location (*locus);
3643 pp_character (context->printer, ',');
3644 pp_newline (context->printer);
3645 if (s.file != NULL)
3647 if (context->show_column && s.column != 0)
3648 pp_printf (context->printer,
3649 _(" inlined from %qD at %r%s:%d:%d%R"),
3650 fndecl,
3651 "locus", s.file, s.line, s.column);
3652 else
3653 pp_printf (context->printer,
3654 _(" inlined from %qD at %r%s:%d%R"),
3655 fndecl,
3656 "locus", s.file, s.line);
3659 else
3660 pp_printf (context->printer, _(" inlined from %qD"),
3661 fndecl);
3664 pp_character (context->printer, ':');
3666 pp_newline (context->printer);
3668 diagnostic_set_last_function (context, diagnostic);
3669 pp_destroy_prefix (context->printer);
3670 context->printer->prefix = old_prefix;
3674 /* Returns a description of FUNCTION using standard terminology. The
3675 result is a format string of the form "In CATEGORY %qD". */
3677 static const char *
3678 function_category (tree fn)
3680 /* We can get called from the middle-end for diagnostics of function
3681 clones. Make sure we have language specific information before
3682 dereferencing it. */
3683 if (DECL_LANG_SPECIFIC (STRIP_TEMPLATE (fn))
3684 && DECL_FUNCTION_MEMBER_P (fn))
3686 if (DECL_STATIC_FUNCTION_P (fn))
3687 return _("In static member function %qD");
3688 else if (DECL_COPY_CONSTRUCTOR_P (fn))
3689 return _("In copy constructor %qD");
3690 else if (DECL_CONSTRUCTOR_P (fn))
3691 return _("In constructor %qD");
3692 else if (DECL_DESTRUCTOR_P (fn))
3693 return _("In destructor %qD");
3694 else if (LAMBDA_FUNCTION_P (fn))
3695 return _("In lambda function");
3696 else
3697 return _("In member function %qD");
3699 else
3700 return _("In function %qD");
3703 /* Disable warnings about missing quoting in GCC diagnostics for
3704 the pp_verbatim calls. Their format strings deliberately don't
3705 follow GCC diagnostic conventions. */
3706 #if __GNUC__ >= 10
3707 #pragma GCC diagnostic push
3708 #pragma GCC diagnostic ignored "-Wformat-diag"
3709 #endif
3711 /* Report the full context of a current template instantiation,
3712 onto BUFFER. */
3713 static void
3714 print_instantiation_full_context (diagnostic_context *context)
3716 struct tinst_level *p = current_instantiation ();
3717 location_t location = input_location;
3719 if (p)
3721 pp_verbatim (context->printer,
3722 p->list_p ()
3723 ? _("%s: In substitution of %qS:\n")
3724 : _("%s: In instantiation of %q#D:\n"),
3725 LOCATION_FILE (location),
3726 p->get_node ());
3728 location = p->locus;
3729 p = p->next;
3732 print_instantiation_partial_context (context, p, location);
3735 /* Helper function of print_instantiation_partial_context() that
3736 prints a single line of instantiation context. */
3738 static void
3739 print_instantiation_partial_context_line (diagnostic_context *context,
3740 struct tinst_level *t,
3741 location_t loc, bool recursive_p)
3743 if (loc == UNKNOWN_LOCATION)
3744 return;
3746 expanded_location xloc = expand_location (loc);
3748 if (context->show_column)
3749 pp_verbatim (context->printer, _("%r%s:%d:%d:%R "),
3750 "locus", xloc.file, xloc.line, xloc.column);
3751 else
3752 pp_verbatim (context->printer, _("%r%s:%d:%R "),
3753 "locus", xloc.file, xloc.line);
3755 if (t != NULL)
3757 if (t->list_p ())
3758 pp_verbatim (context->printer,
3759 recursive_p
3760 ? _("recursively required by substitution of %qS\n")
3761 : _("required by substitution of %qS\n"),
3762 t->get_node ());
3763 else
3764 pp_verbatim (context->printer,
3765 recursive_p
3766 ? _("recursively required from %q#D\n")
3767 : _("required from %q#D\n"),
3768 t->get_node ());
3770 else
3772 pp_verbatim (context->printer,
3773 recursive_p
3774 ? _("recursively required from here\n")
3775 : _("required from here\n"));
3779 /* Same as print_instantiation_full_context but less verbose. */
3781 static void
3782 print_instantiation_partial_context (diagnostic_context *context,
3783 struct tinst_level *t0, location_t loc)
3785 struct tinst_level *t;
3786 int n_total = 0;
3787 int n;
3788 location_t prev_loc = loc;
3790 for (t = t0; t != NULL; t = t->next)
3791 if (prev_loc != t->locus)
3793 prev_loc = t->locus;
3794 n_total++;
3797 t = t0;
3799 if (template_backtrace_limit
3800 && n_total > template_backtrace_limit)
3802 int skip = n_total - template_backtrace_limit;
3803 int head = template_backtrace_limit / 2;
3805 /* Avoid skipping just 1. If so, skip 2. */
3806 if (skip == 1)
3808 skip = 2;
3809 head = (template_backtrace_limit - 1) / 2;
3812 for (n = 0; n < head; n++)
3814 gcc_assert (t != NULL);
3815 if (loc != t->locus)
3816 print_instantiation_partial_context_line (context, t, loc,
3817 /*recursive_p=*/false);
3818 loc = t->locus;
3819 t = t->next;
3821 if (t != NULL && skip > 0)
3823 expanded_location xloc;
3824 xloc = expand_location (loc);
3825 if (context->show_column)
3826 pp_verbatim (context->printer,
3827 _("%r%s:%d:%d:%R [ skipping %d instantiation "
3828 "contexts, use -ftemplate-backtrace-limit=0 to "
3829 "disable ]\n"),
3830 "locus", xloc.file, xloc.line, xloc.column, skip);
3831 else
3832 pp_verbatim (context->printer,
3833 _("%r%s:%d:%R [ skipping %d instantiation "
3834 "contexts, use -ftemplate-backtrace-limit=0 to "
3835 "disable ]\n"),
3836 "locus", xloc.file, xloc.line, skip);
3838 do {
3839 loc = t->locus;
3840 t = t->next;
3841 } while (t != NULL && --skip > 0);
3845 while (t != NULL)
3847 while (t->next != NULL && t->locus == t->next->locus)
3849 loc = t->locus;
3850 t = t->next;
3852 print_instantiation_partial_context_line (context, t, loc,
3853 t->locus == loc);
3854 loc = t->locus;
3855 t = t->next;
3857 print_instantiation_partial_context_line (context, NULL, loc,
3858 /*recursive_p=*/false);
3861 /* Called from cp_thing to print the template context for an error. */
3862 static void
3863 maybe_print_instantiation_context (diagnostic_context *context)
3865 if (!problematic_instantiation_changed () || current_instantiation () == 0)
3866 return;
3868 record_last_problematic_instantiation ();
3869 print_instantiation_full_context (context);
3872 /* Report what constexpr call(s) we're trying to expand, if any. */
3874 void
3875 maybe_print_constexpr_context (diagnostic_context *context)
3877 vec<tree> call_stack = cx_error_context ();
3878 unsigned ix;
3879 tree t;
3881 FOR_EACH_VEC_ELT (call_stack, ix, t)
3883 expanded_location xloc = expand_location (EXPR_LOCATION (t));
3884 const char *s = expr_as_string (t, 0);
3885 if (context->show_column)
3886 pp_verbatim (context->printer,
3887 _("%r%s:%d:%d:%R in %<constexpr%> expansion of %qs"),
3888 "locus", xloc.file, xloc.line, xloc.column, s);
3889 else
3890 pp_verbatim (context->printer,
3891 _("%r%s:%d:%R in %<constexpr%> expansion of %qs"),
3892 "locus", xloc.file, xloc.line, s);
3893 pp_newline (context->printer);
3898 static void
3899 print_location (diagnostic_context *context, location_t loc)
3901 expanded_location xloc = expand_location (loc);
3902 if (context->show_column)
3903 pp_verbatim (context->printer, _("%r%s:%d:%d:%R "),
3904 "locus", xloc.file, xloc.line, xloc.column);
3905 else
3906 pp_verbatim (context->printer, _("%r%s:%d:%R "),
3907 "locus", xloc.file, xloc.line);
3910 static void
3911 print_constrained_decl_info (diagnostic_context *context, tree decl)
3913 print_location (context, DECL_SOURCE_LOCATION (decl));
3914 pp_verbatim (context->printer, "required by the constraints of %q#D\n", decl);
3917 static void
3918 print_concept_check_info (diagnostic_context *context, tree expr, tree map, tree args)
3920 gcc_assert (concept_check_p (expr));
3922 tree id = unpack_concept_check (expr);
3923 tree tmpl = TREE_OPERAND (id, 0);
3924 if (OVL_P (tmpl))
3925 tmpl = OVL_FIRST (tmpl);
3927 print_location (context, DECL_SOURCE_LOCATION (tmpl));
3929 cxx_pretty_printer *pp = (cxx_pretty_printer *)context->printer;
3930 pp_verbatim (pp, "required for the satisfaction of %qE", expr);
3931 if (map && map != error_mark_node)
3933 tree subst_map = tsubst_parameter_mapping (map, args, tf_none, NULL_TREE);
3934 pp_cxx_parameter_mapping (pp, (subst_map != error_mark_node
3935 ? subst_map : map));
3937 pp_newline (pp);
3940 /* Diagnose the entry point into the satisfaction error. Returns the next
3941 context, if any. */
3943 static tree
3944 print_constraint_context_head (diagnostic_context *context, tree cxt, tree args)
3946 tree src = TREE_VALUE (cxt);
3947 if (!src)
3949 print_location (context, input_location);
3950 pp_verbatim (context->printer, "required for constraint satisfaction\n");
3951 return NULL_TREE;
3953 if (DECL_P (src))
3955 print_constrained_decl_info (context, src);
3956 return NULL_TREE;
3958 else
3960 print_concept_check_info (context, src, TREE_PURPOSE (cxt), args);
3961 return TREE_CHAIN (cxt);
3965 static void
3966 print_requires_expression_info (diagnostic_context *context, tree constr, tree args)
3969 tree expr = ATOMIC_CONSTR_EXPR (constr);
3970 tree map = ATOMIC_CONSTR_MAP (constr);
3971 map = tsubst_parameter_mapping (map, args, tf_none, NULL_TREE);
3972 if (map == error_mark_node)
3973 return;
3975 print_location (context, cp_expr_loc_or_input_loc (expr));
3976 pp_verbatim (context->printer, "in requirements ");
3978 tree parms = TREE_OPERAND (expr, 0);
3979 if (parms)
3980 pp_verbatim (context->printer, "with ");
3981 while (parms)
3983 pp_verbatim (context->printer, "%q#D", parms);
3984 if (TREE_CHAIN (parms))
3985 pp_separate_with_comma ((cxx_pretty_printer *)context->printer);
3986 parms = TREE_CHAIN (parms);
3988 pp_cxx_parameter_mapping ((cxx_pretty_printer *)context->printer, map);
3990 pp_verbatim (context->printer, "\n");
3993 void
3994 maybe_print_single_constraint_context (diagnostic_context *context, tree failed)
3996 if (!failed)
3997 return;
3999 tree constr = TREE_VALUE (failed);
4000 if (!constr || constr == error_mark_node)
4001 return;
4002 tree cxt = CONSTR_CONTEXT (constr);
4003 if (!cxt)
4004 return;
4005 tree args = TREE_PURPOSE (failed);
4007 /* Print the stack of requirements. */
4008 cxt = print_constraint_context_head (context, cxt, args);
4009 while (cxt && !DECL_P (TREE_VALUE (cxt)))
4011 tree expr = TREE_VALUE (cxt);
4012 tree map = TREE_PURPOSE (cxt);
4013 print_concept_check_info (context, expr, map, args);
4014 cxt = TREE_CHAIN (cxt);
4017 /* For certain constraints, we can provide additional context. */
4018 if (TREE_CODE (constr) == ATOMIC_CONSTR
4019 && TREE_CODE (ATOMIC_CONSTR_EXPR (constr)) == REQUIRES_EXPR)
4020 print_requires_expression_info (context, constr, args);
4023 void
4024 maybe_print_constraint_context (diagnostic_context *context)
4026 if (!current_failed_constraint)
4027 return;
4029 tree cur = current_failed_constraint;
4031 /* Recursively print nested contexts. */
4032 current_failed_constraint = TREE_CHAIN (current_failed_constraint);
4033 if (current_failed_constraint)
4034 maybe_print_constraint_context (context);
4036 /* Print this context. */
4037 maybe_print_single_constraint_context (context, cur);
4040 /* Return true iff TYPE_A and TYPE_B are template types that are
4041 meaningful to compare. */
4043 static bool
4044 comparable_template_types_p (tree type_a, tree type_b)
4046 if (!CLASS_TYPE_P (type_a))
4047 return false;
4048 if (!CLASS_TYPE_P (type_b))
4049 return false;
4051 tree tinfo_a = TYPE_TEMPLATE_INFO (type_a);
4052 tree tinfo_b = TYPE_TEMPLATE_INFO (type_b);
4053 if (!tinfo_a || !tinfo_b)
4054 return false;
4056 return TI_TEMPLATE (tinfo_a) == TI_TEMPLATE (tinfo_b);
4059 /* Start a new line indented by SPC spaces on PP. */
4061 static void
4062 newline_and_indent (pretty_printer *pp, int spc)
4064 pp_newline (pp);
4065 for (int i = 0; i < spc; i++)
4066 pp_space (pp);
4069 /* Generate a GC-allocated string for ARG, an expression or type. */
4071 static const char *
4072 arg_to_string (tree arg, bool verbose)
4074 if (TYPE_P (arg))
4075 return type_to_string (arg, verbose, true, NULL, false);
4076 else
4077 return expr_to_string (arg);
4080 /* Subroutine to type_to_string_with_compare and
4081 print_template_tree_comparison.
4083 Print a representation of ARG (an expression or type) to PP,
4084 colorizing it as "type-diff" if PP->show_color. */
4086 static void
4087 print_nonequal_arg (pretty_printer *pp, tree arg, bool verbose)
4089 pp_printf (pp, "%r%s%R",
4090 "type-diff",
4091 (arg
4092 ? arg_to_string (arg, verbose)
4093 : G_("(no argument)")));
4096 /* Recursively print template TYPE_A to PP, as compared to template TYPE_B.
4098 The types must satisfy comparable_template_types_p.
4100 If INDENT is 0, then this is equivalent to type_to_string (TYPE_A), but
4101 potentially colorizing/eliding in comparison with TYPE_B.
4103 For example given types:
4104 vector<map<int,double>>
4106 vector<map<int,float>>
4107 then the result on PP would be:
4108 vector<map<[...],double>>
4109 with type elision, and:
4110 vector<map<int,double>>
4111 without type elision.
4113 In both cases the parts of TYPE that differ from PEER will be colorized
4114 if pp_show_color (pp) is true. In the above example, this would be
4115 "double".
4117 If INDENT is non-zero, then the types are printed in a tree-like form
4118 which shows both types. In the above example, the result on PP would be:
4120 vector<
4121 map<
4122 [...],
4123 [double != float]>>
4125 and without type-elision would be:
4127 vector<
4128 map<
4129 int,
4130 [double != float]>>
4132 As before, the differing parts of the types are colorized if
4133 pp_show_color (pp) is true ("double" and "float" in this example).
4135 Template arguments in which both types are using the default arguments
4136 are not printed; if at least one of the two types is using a non-default
4137 argument, then that argument is printed (or both arguments for the
4138 tree-like print format). */
4140 static void
4141 print_template_differences (pretty_printer *pp, tree type_a, tree type_b,
4142 bool verbose, int indent)
4144 if (indent)
4145 newline_and_indent (pp, indent);
4147 tree tinfo_a = TYPE_TEMPLATE_INFO (type_a);
4148 tree tinfo_b = TYPE_TEMPLATE_INFO (type_b);
4150 pp_printf (pp, "%s<",
4151 IDENTIFIER_POINTER (DECL_NAME (TI_TEMPLATE (tinfo_a))));
4153 tree args_a = TI_ARGS (tinfo_a);
4154 tree args_b = TI_ARGS (tinfo_b);
4155 gcc_assert (TREE_CODE (args_a) == TREE_VEC);
4156 gcc_assert (TREE_CODE (args_b) == TREE_VEC);
4157 int flags = 0;
4158 int len_a = get_non_default_template_args_count (args_a, flags);
4159 args_a = INNERMOST_TEMPLATE_ARGS (args_a);
4160 int len_b = get_non_default_template_args_count (args_b, flags);
4161 args_b = INNERMOST_TEMPLATE_ARGS (args_b);
4162 /* Determine the maximum range of args for which non-default template args
4163 were used; beyond this, only default args (if any) were used, and so
4164 they will be equal from this point onwards.
4165 One of the two peers might have used default arguments within this
4166 range, but the other will be using non-default arguments, and so
4167 it's more readable to print both within this range, to highlight
4168 the differences. */
4169 int len_max = MAX (len_a, len_b);
4170 gcc_assert (TREE_CODE (args_a) == TREE_VEC);
4171 gcc_assert (TREE_CODE (args_b) == TREE_VEC);
4172 for (int idx = 0; idx < len_max; idx++)
4174 if (idx)
4175 pp_character (pp, ',');
4177 tree arg_a = TREE_VEC_ELT (args_a, idx);
4178 tree arg_b = TREE_VEC_ELT (args_b, idx);
4179 if (arg_a == arg_b)
4181 if (indent)
4182 newline_and_indent (pp, indent + 2);
4183 /* Can do elision here, printing "[...]". */
4184 if (flag_elide_type)
4185 pp_string (pp, G_("[...]"));
4186 else
4187 pp_string (pp, arg_to_string (arg_a, verbose));
4189 else
4191 int new_indent = indent ? indent + 2 : 0;
4192 if (comparable_template_types_p (arg_a, arg_b))
4193 print_template_differences (pp, arg_a, arg_b, verbose, new_indent);
4194 else
4195 if (indent)
4197 newline_and_indent (pp, indent + 2);
4198 pp_character (pp, '[');
4199 print_nonequal_arg (pp, arg_a, verbose);
4200 pp_string (pp, " != ");
4201 print_nonequal_arg (pp, arg_b, verbose);
4202 pp_character (pp, ']');
4204 else
4205 print_nonequal_arg (pp, arg_a, verbose);
4208 pp_printf (pp, ">");
4211 /* As type_to_string, but for a template, potentially colorizing/eliding
4212 in comparison with PEER.
4213 For example, if TYPE is map<int,double> and PEER is map<int,int>,
4214 then the resulting string would be:
4215 map<[...],double>
4216 with type elision, and:
4217 map<int,double>
4218 without type elision.
4220 In both cases the parts of TYPE that differ from PEER will be colorized
4221 if SHOW_COLOR is true. In the above example, this would be "double".
4223 Template arguments in which both types are using the default arguments
4224 are not printed; if at least one of the two types is using a non-default
4225 argument, then both arguments are printed.
4227 The resulting string is in a GC-allocated buffer. */
4229 static const char *
4230 type_to_string_with_compare (tree type, tree peer, bool verbose,
4231 bool show_color)
4233 pretty_printer inner_pp;
4234 pretty_printer *pp = &inner_pp;
4235 pp_show_color (pp) = show_color;
4237 print_template_differences (pp, type, peer, verbose, 0);
4238 return pp_ggc_formatted_text (pp);
4241 /* Recursively print a tree-like comparison of TYPE_A and TYPE_B to PP,
4242 indented by INDENT spaces.
4244 For example given types:
4246 vector<map<int,double>>
4250 vector<map<double,float>>
4252 the output with type elision would be:
4254 vector<
4255 map<
4256 [...],
4257 [double != float]>>
4259 and without type-elision would be:
4261 vector<
4262 map<
4263 int,
4264 [double != float]>>
4266 TYPE_A and TYPE_B must both be comparable template types
4267 (as per comparable_template_types_p).
4269 Template arguments in which both types are using the default arguments
4270 are not printed; if at least one of the two types is using a non-default
4271 argument, then both arguments are printed. */
4273 static void
4274 print_template_tree_comparison (pretty_printer *pp, tree type_a, tree type_b,
4275 bool verbose, int indent)
4277 print_template_differences (pp, type_a, type_b, verbose, indent);
4280 /* Subroutine for use in a format_postprocessor::handle
4281 implementation. Adds a chunk to the end of
4282 formatted output, so that it will be printed
4283 by pp_output_formatted_text. */
4285 static void
4286 append_formatted_chunk (pretty_printer *pp, const char *content)
4288 output_buffer *buffer = pp_buffer (pp);
4289 struct chunk_info *chunk_array = buffer->cur_chunk_array;
4290 const char **args = chunk_array->args;
4292 unsigned int chunk_idx;
4293 for (chunk_idx = 0; args[chunk_idx]; chunk_idx++)
4295 args[chunk_idx++] = content;
4296 args[chunk_idx] = NULL;
4299 /* Create a copy of CONTENT, with quotes added, and,
4300 potentially, with colorization.
4301 No escaped is performed on CONTENT.
4302 The result is in a GC-allocated buffer. */
4304 static const char *
4305 add_quotes (const char *content, bool show_color)
4307 pretty_printer tmp_pp;
4308 pp_show_color (&tmp_pp) = show_color;
4310 /* We have to use "%<%s%>" rather than "%qs" here in order to avoid
4311 quoting colorization bytes within the results and using either
4312 pp_quote or pp_begin_quote doesn't work the same. */
4313 pp_printf (&tmp_pp, "%<%s%>", content);
4315 return pp_ggc_formatted_text (&tmp_pp);
4318 #if __GNUC__ >= 10
4319 #pragma GCC diagnostic pop
4320 #endif
4322 /* If we had %H and %I, and hence deferred printing them,
4323 print them now, storing the result into the chunk_info
4324 for pp_format. Quote them if 'q' was provided.
4325 Also print the difference in tree form, adding it as
4326 an additional chunk. */
4328 void
4329 cxx_format_postprocessor::handle (pretty_printer *pp)
4331 /* If we have one of %H and %I, the other should have
4332 been present. */
4333 if (m_type_a.m_tree || m_type_b.m_tree)
4335 /* Avoid reentrancy issues by working with a copy of
4336 m_type_a and m_type_b, resetting them now. */
4337 deferred_printed_type type_a = m_type_a;
4338 deferred_printed_type type_b = m_type_b;
4339 m_type_a = deferred_printed_type ();
4340 m_type_b = deferred_printed_type ();
4342 gcc_assert (type_a.m_buffer_ptr);
4343 gcc_assert (type_b.m_buffer_ptr);
4345 bool show_color = pp_show_color (pp);
4347 const char *type_a_text;
4348 const char *type_b_text;
4350 if (comparable_template_types_p (type_a.m_tree, type_b.m_tree))
4352 type_a_text
4353 = type_to_string_with_compare (type_a.m_tree, type_b.m_tree,
4354 type_a.m_verbose, show_color);
4355 type_b_text
4356 = type_to_string_with_compare (type_b.m_tree, type_a.m_tree,
4357 type_b.m_verbose, show_color);
4359 if (flag_diagnostics_show_template_tree)
4361 pretty_printer inner_pp;
4362 pp_show_color (&inner_pp) = pp_show_color (pp);
4363 print_template_tree_comparison
4364 (&inner_pp, type_a.m_tree, type_b.m_tree, type_a.m_verbose, 2);
4365 append_formatted_chunk (pp, pp_ggc_formatted_text (&inner_pp));
4368 else
4370 /* If the types were not comparable (or if only one of %H/%I was
4371 provided), they are printed normally, and no difference tree
4372 is printed. */
4373 type_a_text = type_to_string (type_a.m_tree, type_a.m_verbose,
4374 true, &type_a.m_quote, show_color);
4375 type_b_text = type_to_string (type_b.m_tree, type_b.m_verbose,
4376 true, &type_b.m_quote, show_color);
4379 if (type_a.m_quote)
4380 type_a_text = add_quotes (type_a_text, show_color);
4381 *type_a.m_buffer_ptr = type_a_text;
4383 if (type_b.m_quote)
4384 type_b_text = add_quotes (type_b_text, show_color);
4385 *type_b.m_buffer_ptr = type_b_text;
4389 /* Subroutine for handling %H and %I, to support i18n of messages like:
4391 error_at (loc, "could not convert %qE from %qH to %qI",
4392 expr, type_a, type_b);
4394 so that we can print things like:
4396 could not convert 'foo' from 'map<int,double>' to 'map<int,int>'
4398 and, with type-elision:
4400 could not convert 'foo' from 'map<[...],double>' to 'map<[...],int>'
4402 (with color-coding of the differences between the types).
4404 The %H and %I format codes are peers: both must be present,
4405 and they affect each other. Hence to handle them, we must
4406 delay printing until we have both, deferring the printing to
4407 pretty_printer's m_format_postprocessor hook.
4409 This is called in phase 2 of pp_format, when it is accumulating
4410 a series of formatted chunks. We stash the location of the chunk
4411 we're meant to have written to, so that we can write to it in the
4412 m_format_postprocessor hook.
4414 We also need to stash whether a 'q' prefix was provided (the QUOTE
4415 param) so that we can add the quotes when writing out the delayed
4416 chunk. */
4418 static void
4419 defer_phase_2_of_type_diff (deferred_printed_type *deferred,
4420 tree type, const char **buffer_ptr,
4421 bool verbose, bool quote)
4423 gcc_assert (deferred->m_tree == NULL_TREE);
4424 gcc_assert (deferred->m_buffer_ptr == NULL);
4425 *deferred = deferred_printed_type (type, buffer_ptr, verbose, quote);
4429 /* Called from output_format -- during diagnostic message processing --
4430 to handle C++ specific format specifier with the following meanings:
4431 %A function argument-list.
4432 %C tree code.
4433 %D declaration.
4434 %E expression.
4435 %F function declaration.
4436 %H type difference (from).
4437 %I type difference (to).
4438 %L language as used in extern "lang".
4439 %O binary operator.
4440 %P function parameter whose position is indicated by an integer.
4441 %Q assignment operator.
4442 %S substitution (template + args)
4443 %T type.
4444 %V cv-qualifier.
4445 %X exception-specification. */
4446 static bool
4447 cp_printer (pretty_printer *pp, text_info *text, const char *spec,
4448 int precision, bool wide, bool set_locus, bool verbose,
4449 bool *quoted, const char **buffer_ptr)
4451 gcc_assert (pp->m_format_postprocessor);
4452 cxx_format_postprocessor *postprocessor
4453 = static_cast <cxx_format_postprocessor *> (pp->m_format_postprocessor);
4455 const char *result;
4456 tree t = NULL;
4457 #define next_tree (t = va_arg (*text->args_ptr, tree))
4458 #define next_tcode ((enum tree_code) va_arg (*text->args_ptr, int))
4459 #define next_lang ((enum languages) va_arg (*text->args_ptr, int))
4460 #define next_int va_arg (*text->args_ptr, int)
4462 if (precision != 0 || wide)
4463 return false;
4465 switch (*spec)
4467 case 'A': result = args_to_string (next_tree, verbose); break;
4468 case 'C': result = code_to_string (next_tcode); break;
4469 case 'D':
4471 tree temp = next_tree;
4472 if (VAR_P (temp)
4473 && DECL_HAS_DEBUG_EXPR_P (temp))
4475 temp = DECL_DEBUG_EXPR (temp);
4476 if (!DECL_P (temp))
4478 result = expr_to_string (temp);
4479 break;
4482 result = decl_to_string (temp, verbose, pp_show_color (pp));
4484 break;
4485 case 'E': result = expr_to_string (next_tree); break;
4486 case 'F': result = fndecl_to_string (next_tree, verbose); break;
4487 case 'H':
4488 defer_phase_2_of_type_diff (&postprocessor->m_type_a, next_tree,
4489 buffer_ptr, verbose, *quoted);
4490 return true;
4491 case 'I':
4492 defer_phase_2_of_type_diff (&postprocessor->m_type_b, next_tree,
4493 buffer_ptr, verbose, *quoted);
4494 return true;
4495 case 'L': result = language_to_string (next_lang); break;
4496 case 'O': result = op_to_string (false, next_tcode); break;
4497 case 'P': result = parm_to_string (next_int); break;
4498 case 'Q': result = op_to_string (true, next_tcode); break;
4499 case 'S': result = subst_to_string (next_tree, pp_show_color (pp)); break;
4500 case 'T':
4502 result = type_to_string (next_tree, verbose, false, quoted,
4503 pp_show_color (pp));
4505 break;
4506 case 'V': result = cv_to_string (next_tree, verbose); break;
4507 case 'X': result = eh_spec_to_string (next_tree, verbose); break;
4509 default:
4510 return false;
4513 pp_string (pp, result);
4514 if (set_locus && t != NULL)
4515 text->set_location (0, location_of (t), SHOW_RANGE_WITH_CARET);
4516 return true;
4517 #undef next_tree
4518 #undef next_tcode
4519 #undef next_lang
4520 #undef next_int
4523 /* Warn about the use of C++0x features when appropriate. */
4524 void
4525 maybe_warn_cpp0x (cpp0x_warn_str str, location_t loc/*=input_location*/)
4527 if (cxx_dialect == cxx98)
4528 switch (str)
4530 case CPP0X_INITIALIZER_LISTS:
4531 pedwarn (loc, OPT_Wc__11_extensions,
4532 "extended initializer lists "
4533 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4534 break;
4535 case CPP0X_EXPLICIT_CONVERSION:
4536 pedwarn (loc, OPT_Wc__11_extensions,
4537 "explicit conversion operators "
4538 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4539 break;
4540 case CPP0X_VARIADIC_TEMPLATES:
4541 pedwarn (loc, OPT_Wc__11_extensions,
4542 "variadic templates "
4543 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4544 break;
4545 case CPP0X_LAMBDA_EXPR:
4546 pedwarn (loc, OPT_Wc__11_extensions,
4547 "lambda expressions "
4548 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4549 break;
4550 case CPP0X_AUTO:
4551 pedwarn (loc, OPT_Wc__11_extensions,
4552 "C++11 auto only available with %<-std=c++11%> or "
4553 "%<-std=gnu++11%>");
4554 break;
4555 case CPP0X_SCOPED_ENUMS:
4556 pedwarn (loc, OPT_Wc__11_extensions,
4557 "scoped enums only available with %<-std=c++11%> or "
4558 "%<-std=gnu++11%>");
4559 break;
4560 case CPP0X_DEFAULTED_DELETED:
4561 pedwarn (loc, OPT_Wc__11_extensions,
4562 "defaulted and deleted functions "
4563 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4564 break;
4565 case CPP0X_INLINE_NAMESPACES:
4566 if (pedantic)
4567 pedwarn (loc, OPT_Wc__11_extensions,
4568 "inline namespaces "
4569 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4570 break;
4571 case CPP0X_OVERRIDE_CONTROLS:
4572 pedwarn (loc, OPT_Wc__11_extensions,
4573 "override controls (override/final) "
4574 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4575 break;
4576 case CPP0X_NSDMI:
4577 pedwarn (loc, OPT_Wc__11_extensions,
4578 "non-static data member initializers "
4579 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4580 break;
4581 case CPP0X_USER_DEFINED_LITERALS:
4582 pedwarn (loc, OPT_Wc__11_extensions,
4583 "user-defined literals "
4584 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4585 break;
4586 case CPP0X_DELEGATING_CTORS:
4587 pedwarn (loc, OPT_Wc__11_extensions,
4588 "delegating constructors "
4589 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4590 break;
4591 case CPP0X_INHERITING_CTORS:
4592 pedwarn (loc, OPT_Wc__11_extensions,
4593 "inheriting constructors "
4594 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4595 break;
4596 case CPP0X_ATTRIBUTES:
4597 pedwarn (loc, OPT_Wc__11_extensions,
4598 "C++11 attributes "
4599 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4600 break;
4601 case CPP0X_REF_QUALIFIER:
4602 pedwarn (loc, OPT_Wc__11_extensions,
4603 "ref-qualifiers "
4604 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4605 break;
4606 default:
4607 gcc_unreachable ();
4611 /* Warn about the use of variadic templates when appropriate. */
4612 void
4613 maybe_warn_variadic_templates (void)
4615 maybe_warn_cpp0x (CPP0X_VARIADIC_TEMPLATES);
4619 /* Issue an ISO C++98 pedantic warning at LOCATION, conditional on
4620 option OPT with text GMSGID. Use this function to report
4621 diagnostics for constructs that are invalid C++98, but valid
4622 C++0x. */
4623 bool
4624 pedwarn_cxx98 (location_t location, int opt, const char *gmsgid, ...)
4626 diagnostic_info diagnostic;
4627 va_list ap;
4628 bool ret;
4629 rich_location richloc (line_table, location);
4631 va_start (ap, gmsgid);
4632 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
4633 (cxx_dialect == cxx98) ? DK_PEDWARN : DK_WARNING);
4634 diagnostic.option_index = opt;
4635 ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
4636 va_end (ap);
4637 return ret;
4640 /* Issue a diagnostic that NAME cannot be found in SCOPE. DECL is what
4641 we found when we tried to do the lookup. LOCATION is the location of
4642 the NAME identifier. */
4644 void
4645 qualified_name_lookup_error (tree scope, tree name,
4646 tree decl, location_t location)
4648 if (scope == error_mark_node)
4649 ; /* We already complained. */
4650 else if (TYPE_P (scope))
4652 if (!COMPLETE_TYPE_P (scope))
4653 error_at (location, "incomplete type %qT used in nested name specifier",
4654 scope);
4655 else if (TREE_CODE (decl) == TREE_LIST)
4657 error_at (location, "reference to %<%T::%D%> is ambiguous",
4658 scope, name);
4659 print_candidates (decl);
4661 else
4663 name_hint hint;
4664 if (SCOPED_ENUM_P (scope) && TREE_CODE (name) == IDENTIFIER_NODE)
4665 hint = suggest_alternative_in_scoped_enum (name, scope);
4666 if (const char *suggestion = hint.suggestion ())
4668 gcc_rich_location richloc (location);
4669 richloc.add_fixit_replace (suggestion);
4670 error_at (&richloc,
4671 "%qD is not a member of %qT; did you mean %qs?",
4672 name, scope, suggestion);
4674 else
4675 error_at (location, "%qD is not a member of %qT", name, scope);
4678 else if (scope != global_namespace)
4680 auto_diagnostic_group d;
4681 bool emit_fixit = true;
4682 name_hint hint
4683 = suggest_alternative_in_explicit_scope (location, name, scope);
4684 if (!hint)
4686 hint = suggest_alternatives_in_other_namespaces (location, name);
4687 /* "location" is just the location of the name, not of the explicit
4688 scope, and it's not easy to get at the latter, so we can't issue
4689 fix-it hints for the suggestion. */
4690 emit_fixit = false;
4692 if (const char *suggestion = hint.suggestion ())
4694 gcc_rich_location richloc (location);
4695 if (emit_fixit)
4696 richloc.add_fixit_replace (suggestion);
4697 error_at (&richloc, "%qD is not a member of %qD; did you mean %qs?",
4698 name, scope, suggestion);
4700 else
4701 error_at (location, "%qD is not a member of %qD", name, scope);
4703 else
4705 auto_diagnostic_group d;
4706 name_hint hint = suggest_alternatives_for (location, name, true);
4707 if (const char *suggestion = hint.suggestion ())
4709 gcc_rich_location richloc (location);
4710 richloc.add_fixit_replace (suggestion);
4711 error_at (&richloc,
4712 "%<::%D%> has not been declared; did you mean %qs?",
4713 name, suggestion);
4715 else
4716 error_at (location, "%<::%D%> has not been declared", name);
4720 /* C++-specific implementation of range_label::get_text () vfunc for
4721 range_label_for_type_mismatch.
4723 Compare with print_template_differences above. */
4725 label_text
4726 range_label_for_type_mismatch::get_text (unsigned /*range_idx*/) const
4728 if (m_labelled_type == NULL_TREE)
4729 return label_text::borrow (NULL);
4731 const bool verbose = false;
4732 const bool show_color = false;
4734 const char *result;
4735 if (m_other_type
4736 && comparable_template_types_p (m_labelled_type, m_other_type))
4737 result = type_to_string_with_compare (m_labelled_type, m_other_type,
4738 verbose, show_color);
4739 else
4740 result = type_to_string (m_labelled_type, verbose, true, NULL, show_color);
4742 /* Both of the above return GC-allocated buffers, so the caller mustn't
4743 free them. */
4744 return label_text::borrow (result);