c++: dump alias-declaration scope
[official-gcc.git] / gcc / cp / error.cc
blob2b07136b5ca79feb68c6bda95472f270dec86055
1 /* Call-backs for C++ error reporting.
2 This code is non-reentrant.
3 Copyright (C) 1993-2022 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 "langhooks-def.h"
29 #include "intl.h"
30 #include "cxx-pretty-print.h"
31 #include "tree-pretty-print.h"
32 #include "gimple-pretty-print.h"
33 #include "c-family/c-objc.h"
34 #include "ubsan.h"
35 #include "internal-fn.h"
36 #include "gcc-rich-location.h"
37 #include "cp-name-hint.h"
39 #define pp_separate_with_comma(PP) pp_cxx_separate_with (PP, ',')
40 #define pp_separate_with_semicolon(PP) pp_cxx_separate_with (PP, ';')
42 /* cxx_pp is a C++ front-end-specific pretty printer: this is where we
43 dump C++ ASTs as strings. It is mostly used only by the various
44 tree -> string functions that are occasionally called from the
45 debugger or by the front-end for things like
46 __PRETTY_FUNCTION__. */
47 static cxx_pretty_printer actual_pretty_printer;
48 static cxx_pretty_printer * const cxx_pp = &actual_pretty_printer;
50 /* Translate if being used for diagnostics, but not for dump files or
51 __PRETTY_FUNCTION. */
52 #define M_(msgid) (pp_translate_identifiers (cxx_pp) ? _(msgid) : (msgid))
54 # define NEXT_CODE(T) (TREE_CODE (TREE_TYPE (T)))
56 static const char *args_to_string (tree, int);
57 static const char *code_to_string (enum tree_code);
58 static const char *cv_to_string (tree, int);
59 static const char *decl_to_string (tree, int);
60 static const char *fndecl_to_string (tree, int);
61 static const char *op_to_string (bool, enum tree_code);
62 static const char *parm_to_string (int);
63 static const char *type_to_string (tree, int, bool, bool *, bool);
65 static void dump_alias_template_specialization (cxx_pretty_printer *, tree, int);
66 static void dump_type (cxx_pretty_printer *, tree, int);
67 static void dump_typename (cxx_pretty_printer *, tree, int);
68 static void dump_simple_decl (cxx_pretty_printer *, tree, tree, int);
69 static void dump_decl (cxx_pretty_printer *, tree, int);
70 static void dump_template_decl (cxx_pretty_printer *, tree, int);
71 static void dump_function_decl (cxx_pretty_printer *, tree, int);
72 static void dump_expr (cxx_pretty_printer *, tree, int);
73 static void dump_unary_op (cxx_pretty_printer *, const char *, tree, int);
74 static void dump_binary_op (cxx_pretty_printer *, const char *, tree, int);
75 static void dump_aggr_type (cxx_pretty_printer *, tree, int);
76 static void dump_type_prefix (cxx_pretty_printer *, tree, int);
77 static void dump_type_suffix (cxx_pretty_printer *, tree, int);
78 static void dump_function_name (cxx_pretty_printer *, tree, int);
79 static void dump_call_expr_args (cxx_pretty_printer *, tree, int, bool);
80 static void dump_aggr_init_expr_args (cxx_pretty_printer *, tree, int, bool);
81 static void dump_expr_list (cxx_pretty_printer *, tree, int);
82 static void dump_global_iord (cxx_pretty_printer *, tree);
83 static void dump_parameters (cxx_pretty_printer *, tree, int);
84 static void dump_ref_qualifier (cxx_pretty_printer *, tree, int);
85 static void dump_exception_spec (cxx_pretty_printer *, tree, int);
86 static void dump_template_argument (cxx_pretty_printer *, tree, int);
87 static void dump_template_argument_list (cxx_pretty_printer *, tree, int);
88 static void dump_template_parameter (cxx_pretty_printer *, tree, int);
89 static void dump_template_bindings (cxx_pretty_printer *, tree, tree,
90 vec<tree, va_gc> *);
91 static void dump_scope (cxx_pretty_printer *, tree, int);
92 static void dump_template_parms (cxx_pretty_printer *, tree, int, int);
93 static int get_non_default_template_args_count (tree, int);
94 static const char *function_category (tree);
95 static void maybe_print_constexpr_context (diagnostic_context *);
96 static void maybe_print_instantiation_context (diagnostic_context *);
97 static void print_instantiation_full_context (diagnostic_context *);
98 static void print_instantiation_partial_context (diagnostic_context *,
99 struct tinst_level *,
100 location_t);
101 static void maybe_print_constraint_context (diagnostic_context *);
102 static void cp_diagnostic_starter (diagnostic_context *, diagnostic_info *);
103 static void cp_print_error_function (diagnostic_context *, diagnostic_info *);
105 static bool cp_printer (pretty_printer *, text_info *, const char *,
106 int, bool, bool, bool, bool *, const char **);
108 /* Struct for handling %H or %I, which require delaying printing the
109 type until a postprocessing stage. */
111 class deferred_printed_type
113 public:
114 deferred_printed_type ()
115 : m_tree (NULL_TREE), m_buffer_ptr (NULL), m_verbose (false), m_quote (false)
118 deferred_printed_type (tree type, const char **buffer_ptr, bool verbose,
119 bool quote)
120 : m_tree (type), m_buffer_ptr (buffer_ptr), m_verbose (verbose),
121 m_quote (quote)
123 gcc_assert (type);
124 gcc_assert (buffer_ptr);
127 /* The tree is not GTY-marked: they are only non-NULL within a
128 call to pp_format. */
129 tree m_tree;
130 const char **m_buffer_ptr;
131 bool m_verbose;
132 bool m_quote;
135 /* Subclass of format_postprocessor for the C++ frontend.
136 This handles the %H and %I formatting codes, printing them
137 in a postprocessing phase (since they affect each other). */
139 class cxx_format_postprocessor : public format_postprocessor
141 public:
142 cxx_format_postprocessor ()
143 : m_type_a (), m_type_b ()
146 format_postprocessor *clone() const FINAL OVERRIDE
148 return new cxx_format_postprocessor ();
151 void handle (pretty_printer *pp) FINAL OVERRIDE;
153 deferred_printed_type m_type_a;
154 deferred_printed_type m_type_b;
157 /* CONTEXT->printer is a basic pretty printer that was constructed
158 presumably by diagnostic_initialize(), called early in the
159 compiler's initialization process (in general_init) Before the FE
160 is initialized. This (C++) FE-specific diagnostic initializer is
161 thus replacing the basic pretty printer with one that has C++-aware
162 capacities. */
164 void
165 cxx_initialize_diagnostics (diagnostic_context *context)
167 pretty_printer *base = context->printer;
168 cxx_pretty_printer *pp = XNEW (cxx_pretty_printer);
169 context->printer = new (pp) cxx_pretty_printer ();
171 /* It is safe to free this object because it was previously XNEW()'d. */
172 base->~pretty_printer ();
173 XDELETE (base);
175 c_common_diagnostics_set_defaults (context);
176 diagnostic_starter (context) = cp_diagnostic_starter;
177 /* diagnostic_finalizer is already c_diagnostic_finalizer. */
178 diagnostic_format_decoder (context) = cp_printer;
179 pp->m_format_postprocessor = new cxx_format_postprocessor ();
182 /* Dump an '@module' name suffix for DECL, if any. */
184 static void
185 dump_module_suffix (cxx_pretty_printer *pp, tree decl)
187 if (!modules_p ())
188 return;
190 if (!DECL_CONTEXT (decl))
191 return;
193 if (TREE_CODE (decl) != CONST_DECL
194 || !UNSCOPED_ENUM_P (DECL_CONTEXT (decl)))
196 if (!DECL_NAMESPACE_SCOPE_P (decl))
197 return;
199 if (TREE_CODE (decl) == NAMESPACE_DECL
200 && !DECL_NAMESPACE_ALIAS (decl)
201 && (TREE_PUBLIC (decl) || !TREE_PUBLIC (CP_DECL_CONTEXT (decl))))
202 return;
205 if (unsigned m = get_originating_module (decl))
206 if (const char *n = module_name (m, false))
208 pp_character (pp, '@');
209 pp->padding = pp_none;
210 pp_string (pp, n);
214 /* The scope of the declaration we're currently printing, to avoid redundantly
215 dumping the same scope on parameter types. */
216 static tree current_dump_scope;
218 /* Dump a scope, if deemed necessary. */
220 static void
221 dump_scope (cxx_pretty_printer *pp, tree scope, int flags)
223 int f = flags & (TFF_SCOPE | TFF_CHASE_TYPEDEF);
225 if (scope == NULL_TREE || scope == current_dump_scope)
226 return;
228 /* Enum values within an unscoped enum will be CONST_DECL with an
229 ENUMERAL_TYPE as their "scope". Use CP_TYPE_CONTEXT of the
230 ENUMERAL_TYPE, so as to print any enclosing namespace. */
231 if (UNSCOPED_ENUM_P (scope))
232 scope = CP_TYPE_CONTEXT (scope);
234 if (TREE_CODE (scope) == NAMESPACE_DECL)
236 if (scope != global_namespace)
238 dump_decl (pp, scope, f);
239 pp_cxx_colon_colon (pp);
242 else if (AGGREGATE_TYPE_P (scope)
243 || SCOPED_ENUM_P (scope))
245 dump_type (pp, scope, f);
246 pp_cxx_colon_colon (pp);
248 else if ((flags & TFF_SCOPE) && TREE_CODE (scope) == FUNCTION_DECL)
250 dump_function_decl (pp, scope, f | TFF_NO_TEMPLATE_BINDINGS);
251 pp_cxx_colon_colon (pp);
255 /* Dump the template ARGument under control of FLAGS. */
257 static void
258 dump_template_argument (cxx_pretty_printer *pp, tree arg, int flags)
260 if (ARGUMENT_PACK_P (arg))
261 dump_template_argument_list (pp, ARGUMENT_PACK_ARGS (arg),
262 /* No default args in argument packs. */
263 flags|TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS);
264 else if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
265 dump_type (pp, arg, flags & ~TFF_CLASS_KEY_OR_ENUM);
266 else
268 if (TREE_CODE (arg) == TREE_LIST)
269 arg = TREE_VALUE (arg);
271 /* Strip implicit conversions. */
272 while (CONVERT_EXPR_P (arg))
273 arg = TREE_OPERAND (arg, 0);
275 dump_expr (pp, arg, (flags | TFF_EXPR_IN_PARENS) & ~TFF_CLASS_KEY_OR_ENUM);
279 /* Count the number of template arguments ARGS whose value does not
280 match the (optional) default template parameter in PARAMS */
282 static int
283 get_non_default_template_args_count (tree args, int flags)
285 int n = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_ARGS (args));
287 if (/* We use this flag when generating debug information. We don't
288 want to expand templates at this point, for this may generate
289 new decls, which gets decl counts out of sync, which may in
290 turn cause codegen differences between compilations with and
291 without -g. */
292 (flags & TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS) != 0
293 || !flag_pretty_templates)
294 return n;
296 return GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (INNERMOST_TEMPLATE_ARGS (args));
299 /* Dump a template-argument-list ARGS (always a TREE_VEC) under control
300 of FLAGS. */
302 static void
303 dump_template_argument_list (cxx_pretty_printer *pp, tree args, int flags)
305 int n = get_non_default_template_args_count (args, flags);
306 int need_comma = 0;
307 int i;
309 for (i = 0; i < n; ++i)
311 tree arg = TREE_VEC_ELT (args, i);
313 /* Only print a comma if we know there is an argument coming. In
314 the case of an empty template argument pack, no actual
315 argument will be printed. */
316 if (need_comma
317 && (!ARGUMENT_PACK_P (arg)
318 || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) > 0))
319 pp_separate_with_comma (pp);
321 dump_template_argument (pp, arg, flags);
322 need_comma = 1;
326 /* Dump a template parameter PARM (a TREE_LIST) under control of FLAGS. */
328 static void
329 dump_template_parameter (cxx_pretty_printer *pp, tree parm, int flags)
331 tree p;
332 tree a;
334 if (parm == error_mark_node)
335 return;
337 p = TREE_VALUE (parm);
338 a = TREE_PURPOSE (parm);
340 if (TREE_CODE (p) == TYPE_DECL)
342 if (flags & TFF_DECL_SPECIFIERS)
344 pp_cxx_ws_string (pp, "class");
345 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (p)))
346 pp_cxx_ws_string (pp, "...");
347 if (DECL_NAME (p))
348 pp_cxx_tree_identifier (pp, DECL_NAME (p));
350 else if (DECL_NAME (p))
351 pp_cxx_tree_identifier (pp, DECL_NAME (p));
352 else
353 pp_cxx_canonical_template_parameter (pp, TREE_TYPE (p));
355 else
356 dump_decl (pp, p, flags | TFF_DECL_SPECIFIERS);
358 if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && a != NULL_TREE)
360 pp_cxx_whitespace (pp);
361 pp_equal (pp);
362 pp_cxx_whitespace (pp);
363 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
364 dump_type (pp, a, flags & ~TFF_CHASE_TYPEDEF);
365 else
366 dump_expr (pp, a, flags | TFF_EXPR_IN_PARENS);
370 /* Dump, under control of FLAGS, a template-parameter-list binding.
371 PARMS is a TREE_LIST of TREE_VEC of TREE_LIST and ARGS is a
372 TREE_VEC. */
374 static void
375 dump_template_bindings (cxx_pretty_printer *pp, tree parms, tree args,
376 vec<tree, va_gc> *typenames)
378 /* Print "[with" and ']', conditional on whether anything is printed at all.
379 This is tied to whether a semicolon is needed to separate multiple template
380 parameters. */
381 struct prepost_semicolon
383 cxx_pretty_printer *pp;
384 bool need_semicolon;
386 void operator() ()
388 if (need_semicolon)
389 pp_separate_with_semicolon (pp);
390 else
392 pp_cxx_whitespace (pp);
393 pp_cxx_left_bracket (pp);
394 pp->translate_string ("with");
395 pp_cxx_whitespace (pp);
396 need_semicolon = true;
400 ~prepost_semicolon ()
402 if (need_semicolon)
403 pp_cxx_right_bracket (pp);
405 } semicolon_or_introducer = {pp, false};
407 int i;
408 tree t;
410 while (parms)
412 tree p = TREE_VALUE (parms);
413 int lvl = TMPL_PARMS_DEPTH (parms);
414 int arg_idx = 0;
415 int i;
416 tree lvl_args = NULL_TREE;
418 /* Don't crash if we had an invalid argument list. */
419 if (TMPL_ARGS_DEPTH (args) >= lvl)
420 lvl_args = TMPL_ARGS_LEVEL (args, lvl);
422 for (i = 0; i < TREE_VEC_LENGTH (p); ++i)
424 tree arg = NULL_TREE;
426 /* Don't crash if we had an invalid argument list. */
427 if (lvl_args && NUM_TMPL_ARGS (lvl_args) > arg_idx)
428 arg = TREE_VEC_ELT (lvl_args, arg_idx);
430 tree parm_i = TREE_VEC_ELT (p, i);
431 /* If the template argument repeats the template parameter (T = T),
432 skip the parameter.*/
433 if (arg && TREE_CODE (arg) == TEMPLATE_TYPE_PARM
434 && TREE_CODE (parm_i) == TREE_LIST
435 && TREE_CODE (TREE_VALUE (parm_i)) == TYPE_DECL
436 && TREE_CODE (TREE_TYPE (TREE_VALUE (parm_i)))
437 == TEMPLATE_TYPE_PARM
438 && DECL_NAME (TREE_VALUE (parm_i))
439 == DECL_NAME (TREE_CHAIN (arg)))
440 continue;
442 semicolon_or_introducer ();
443 dump_template_parameter (pp, parm_i, TFF_PLAIN_IDENTIFIER);
444 pp_cxx_whitespace (pp);
445 pp_equal (pp);
446 pp_cxx_whitespace (pp);
447 if (arg)
449 if (ARGUMENT_PACK_P (arg))
450 pp_cxx_left_brace (pp);
451 dump_template_argument (pp, arg, TFF_PLAIN_IDENTIFIER);
452 if (ARGUMENT_PACK_P (arg))
453 pp_cxx_right_brace (pp);
455 else
456 pp_string (pp, M_("<missing>"));
458 ++arg_idx;
461 parms = TREE_CHAIN (parms);
464 /* Don't bother with typenames for a partial instantiation. */
465 if (vec_safe_is_empty (typenames) || uses_template_parms (args))
466 return;
468 /* Don't try to print typenames when we're processing a clone. */
469 if (current_function_decl
470 && !DECL_LANG_SPECIFIC (current_function_decl))
471 return;
473 /* Don't try to do this once cgraph starts throwing away front-end
474 information. */
475 if (at_eof >= 2)
476 return;
478 FOR_EACH_VEC_SAFE_ELT (typenames, i, t)
480 semicolon_or_introducer ();
481 dump_type (pp, t, TFF_PLAIN_IDENTIFIER);
482 pp_cxx_whitespace (pp);
483 pp_equal (pp);
484 pp_cxx_whitespace (pp);
485 push_deferring_access_checks (dk_no_check);
486 t = tsubst (t, args, tf_none, NULL_TREE);
487 pop_deferring_access_checks ();
488 /* Strip typedefs. We can't just use TFF_CHASE_TYPEDEF because
489 pp_simple_type_specifier doesn't know about it. */
490 t = strip_typedefs (t, NULL, STF_USER_VISIBLE);
491 dump_type (pp, t, TFF_PLAIN_IDENTIFIER);
495 /* Dump a human-readable equivalent of the alias template
496 specialization of T. */
498 static void
499 dump_alias_template_specialization (cxx_pretty_printer *pp, tree t, int flags)
501 gcc_assert (alias_template_specialization_p (t, nt_opaque));
503 tree decl = TYPE_NAME (t);
504 if (!(flags & TFF_UNQUALIFIED_NAME))
505 dump_scope (pp, CP_DECL_CONTEXT (decl), flags);
506 pp_cxx_tree_identifier (pp, DECL_NAME (decl));
507 dump_template_parms (pp, DECL_TEMPLATE_INFO (decl),
508 /*primary=*/false,
509 flags & ~TFF_TEMPLATE_HEADER);
512 /* Dump a human-readable equivalent of TYPE. FLAGS controls the
513 format. */
515 static void
516 dump_type (cxx_pretty_printer *pp, tree t, int flags)
518 if (t == NULL_TREE)
519 return;
521 /* Don't print e.g. "struct mytypedef". */
522 if (TYPE_P (t) && typedef_variant_p (t))
524 tree decl = TYPE_NAME (t);
525 if ((flags & TFF_CHASE_TYPEDEF)
526 || DECL_SELF_REFERENCE_P (decl)
527 || (!flag_pretty_templates
528 && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)))
530 unsigned int stf_flags = (!(pp->flags & pp_c_flag_gnu_v3)
531 ? STF_USER_VISIBLE : 0);
532 t = strip_typedefs (t, NULL, stf_flags);
534 else if (alias_template_specialization_p (t, nt_opaque))
536 dump_alias_template_specialization (pp, t, flags);
537 return;
539 else if (same_type_p (t, TREE_TYPE (decl)))
540 t = decl;
541 else
543 pp_cxx_cv_qualifier_seq (pp, t);
544 if (! (flags & TFF_UNQUALIFIED_NAME))
545 dump_scope (pp, CP_DECL_CONTEXT (decl), flags);
546 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
547 return;
551 if (TYPE_PTRMEMFUNC_P (t))
552 goto offset_type;
554 switch (TREE_CODE (t))
556 case LANG_TYPE:
557 if (t == init_list_type_node)
558 pp_string (pp, M_("<brace-enclosed initializer list>"));
559 else if (t == unknown_type_node)
560 pp_string (pp, M_("<unresolved overloaded function type>"));
561 else
563 pp_cxx_cv_qualifier_seq (pp, t);
564 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
566 break;
568 case TREE_LIST:
569 /* A list of function parms. */
570 dump_parameters (pp, t, flags);
571 break;
573 case IDENTIFIER_NODE:
574 pp_cxx_tree_identifier (pp, t);
575 break;
577 case TREE_BINFO:
578 dump_type (pp, BINFO_TYPE (t), flags);
579 break;
581 case RECORD_TYPE:
582 case UNION_TYPE:
583 case ENUMERAL_TYPE:
584 dump_aggr_type (pp, t, flags);
585 break;
587 case TYPE_DECL:
588 if (flags & TFF_CHASE_TYPEDEF)
590 dump_type (pp, DECL_ORIGINAL_TYPE (t)
591 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t), flags);
592 break;
594 /* Fall through. */
596 case TEMPLATE_DECL:
597 case NAMESPACE_DECL:
598 dump_decl (pp, t, flags & ~TFF_DECL_SPECIFIERS);
599 break;
601 case INTEGER_TYPE:
602 case REAL_TYPE:
603 case VOID_TYPE:
604 case OPAQUE_TYPE:
605 case BOOLEAN_TYPE:
606 case COMPLEX_TYPE:
607 case VECTOR_TYPE:
608 case FIXED_POINT_TYPE:
609 pp_type_specifier_seq (pp, t);
610 break;
612 case TEMPLATE_TEMPLATE_PARM:
613 /* For parameters inside template signature. */
614 if (TYPE_IDENTIFIER (t))
615 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
616 else
617 pp_cxx_canonical_template_parameter (pp, t);
618 break;
620 case BOUND_TEMPLATE_TEMPLATE_PARM:
622 tree args = TYPE_TI_ARGS (t);
623 pp_cxx_cv_qualifier_seq (pp, t);
624 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
625 pp_cxx_begin_template_argument_list (pp);
626 dump_template_argument_list (pp, args, flags);
627 pp_cxx_end_template_argument_list (pp);
629 break;
631 case TEMPLATE_TYPE_PARM:
632 pp_cxx_cv_qualifier_seq (pp, t);
633 if (template_placeholder_p (t))
635 t = TREE_TYPE (CLASS_PLACEHOLDER_TEMPLATE (t));
636 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
637 pp_string (pp, "<...auto...>");
639 else if (TYPE_IDENTIFIER (t))
640 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
641 else
642 pp_cxx_canonical_template_parameter
643 (pp, TEMPLATE_TYPE_PARM_INDEX (t));
644 /* If this is a constrained placeholder, add the requirements. */
645 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
646 pp_cxx_constrained_type_spec (pp, c);
647 break;
649 /* This is not always necessary for pointers and such, but doing this
650 reduces code size. */
651 case ARRAY_TYPE:
652 case POINTER_TYPE:
653 case REFERENCE_TYPE:
654 case OFFSET_TYPE:
655 offset_type:
656 case FUNCTION_TYPE:
657 case METHOD_TYPE:
659 dump_type_prefix (pp, t, flags);
660 dump_type_suffix (pp, t, flags);
661 break;
663 case TYPENAME_TYPE:
664 if (! (flags & TFF_CHASE_TYPEDEF)
665 && DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
667 dump_decl (pp, TYPE_NAME (t), TFF_PLAIN_IDENTIFIER);
668 break;
670 pp_cxx_cv_qualifier_seq (pp, t);
671 pp_cxx_ws_string (pp,
672 TYPENAME_IS_ENUM_P (t) ? "enum"
673 : TYPENAME_IS_CLASS_P (t) ? "class"
674 : "typename");
675 dump_typename (pp, t, flags);
676 break;
678 case UNBOUND_CLASS_TEMPLATE:
679 if (! (flags & TFF_UNQUALIFIED_NAME))
681 dump_type (pp, TYPE_CONTEXT (t), flags);
682 pp_cxx_colon_colon (pp);
684 pp_cxx_ws_string (pp, "template");
685 dump_type (pp, TYPE_IDENTIFIER (t), flags);
686 break;
688 case TYPEOF_TYPE:
689 pp_cxx_ws_string (pp, "__typeof__");
690 pp_cxx_whitespace (pp);
691 pp_cxx_left_paren (pp);
692 dump_expr (pp, TYPEOF_TYPE_EXPR (t), flags & ~TFF_EXPR_IN_PARENS);
693 pp_cxx_right_paren (pp);
694 break;
696 case UNDERLYING_TYPE:
697 pp_cxx_ws_string (pp, "__underlying_type");
698 pp_cxx_whitespace (pp);
699 pp_cxx_left_paren (pp);
700 dump_expr (pp, UNDERLYING_TYPE_TYPE (t), flags & ~TFF_EXPR_IN_PARENS);
701 pp_cxx_right_paren (pp);
702 break;
704 case TYPE_PACK_EXPANSION:
705 dump_type (pp, PACK_EXPANSION_PATTERN (t), flags);
706 pp_cxx_ws_string (pp, "...");
707 break;
709 case TYPE_ARGUMENT_PACK:
710 dump_template_argument (pp, t, flags);
711 break;
713 case DECLTYPE_TYPE:
714 pp_cxx_ws_string (pp, "decltype");
715 pp_cxx_whitespace (pp);
716 pp_cxx_left_paren (pp);
717 dump_expr (pp, DECLTYPE_TYPE_EXPR (t), flags & ~TFF_EXPR_IN_PARENS);
718 pp_cxx_right_paren (pp);
719 break;
721 case NULLPTR_TYPE:
722 pp_string (pp, "std::nullptr_t");
723 break;
725 default:
726 pp_unsupported_tree (pp, t);
727 /* Fall through. */
729 case ERROR_MARK:
730 pp_string (pp, M_("<type error>"));
731 break;
735 /* Dump a TYPENAME_TYPE. We need to notice when the context is itself
736 a TYPENAME_TYPE. */
738 static void
739 dump_typename (cxx_pretty_printer *pp, tree t, int flags)
741 tree ctx = TYPE_CONTEXT (t);
743 if (TREE_CODE (ctx) == TYPENAME_TYPE)
744 dump_typename (pp, ctx, flags);
745 else
746 dump_type (pp, ctx, flags & ~TFF_CLASS_KEY_OR_ENUM);
747 pp_cxx_colon_colon (pp);
748 dump_decl (pp, TYPENAME_TYPE_FULLNAME (t), flags);
751 /* Return the name of the supplied aggregate, or enumeral type. */
753 const char *
754 class_key_or_enum_as_string (tree t)
756 if (TREE_CODE (t) == ENUMERAL_TYPE)
758 if (SCOPED_ENUM_P (t))
759 return "enum class";
760 else
761 return "enum";
763 else if (TREE_CODE (t) == UNION_TYPE)
764 return "union";
765 else if (TYPE_LANG_SPECIFIC (t) && CLASSTYPE_DECLARED_CLASS (t))
766 return "class";
767 else
768 return "struct";
771 /* Disable warnings about missing quoting in GCC diagnostics for
772 the pp_verbatim call. Their format strings deliberately don't
773 follow GCC diagnostic conventions. */
774 #if __GNUC__ >= 10
775 #pragma GCC diagnostic push
776 #pragma GCC diagnostic ignored "-Wformat-diag"
777 #endif
779 /* Print out a class declaration T under the control of FLAGS,
780 in the form `class foo'. */
782 static void
783 dump_aggr_type (cxx_pretty_printer *pp, tree t, int flags)
785 const char *variety = class_key_or_enum_as_string (t);
786 int typdef = 0;
787 int tmplate = 0;
789 pp_cxx_cv_qualifier_seq (pp, t);
791 if (flags & TFF_CLASS_KEY_OR_ENUM)
792 pp_cxx_ws_string (pp, variety);
794 tree decl = TYPE_NAME (t);
796 if (decl)
798 typdef = (!DECL_ARTIFICIAL (decl)
799 /* An alias specialization is not considered to be a
800 typedef. */
801 && !alias_template_specialization_p (t, nt_opaque));
803 if ((typdef
804 && ((flags & TFF_CHASE_TYPEDEF)
805 || (!flag_pretty_templates && DECL_LANG_SPECIFIC (decl)
806 && DECL_TEMPLATE_INFO (decl))))
807 || DECL_SELF_REFERENCE_P (decl))
809 t = TYPE_MAIN_VARIANT (t);
810 decl = TYPE_NAME (t);
811 typdef = 0;
814 tmplate = !typdef && TREE_CODE (t) != ENUMERAL_TYPE
815 && TYPE_LANG_SPECIFIC (t) && CLASSTYPE_TEMPLATE_INFO (t)
816 && (TREE_CODE (CLASSTYPE_TI_TEMPLATE (t)) != TEMPLATE_DECL
817 || PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
819 if (! (flags & TFF_UNQUALIFIED_NAME))
820 dump_scope (pp, CP_DECL_CONTEXT (decl), flags | TFF_SCOPE);
821 flags &= ~TFF_UNQUALIFIED_NAME;
822 if (tmplate)
824 /* Because the template names are mangled, we have to locate
825 the most general template, and use that name. */
826 tree tpl = TYPE_TI_TEMPLATE (t);
828 while (DECL_TEMPLATE_INFO (tpl))
829 tpl = DECL_TI_TEMPLATE (tpl);
830 decl = tpl;
834 if (LAMBDA_TYPE_P (t))
836 /* A lambda's "type" is essentially its signature. */
837 pp_string (pp, M_("<lambda"));
838 if (lambda_function (t))
839 dump_parameters (pp,
840 FUNCTION_FIRST_USER_PARMTYPE (lambda_function (t)),
841 flags);
842 pp_greater (pp);
844 else if (!decl || IDENTIFIER_ANON_P (DECL_NAME (decl)))
846 if (flags & TFF_CLASS_KEY_OR_ENUM)
847 pp_string (pp, M_("<unnamed>"));
848 else
849 pp_printf (pp, M_("<unnamed %s>"), variety);
851 else
852 pp_cxx_tree_identifier (pp, DECL_NAME (decl));
854 dump_module_suffix (pp, decl);
856 if (tmplate)
857 dump_template_parms (pp, TYPE_TEMPLATE_INFO (t),
858 !CLASSTYPE_USE_TEMPLATE (t),
859 flags & ~TFF_TEMPLATE_HEADER);
862 #if __GNUC__ >= 10
863 #pragma GCC diagnostic pop
864 #endif
866 /* Dump into the obstack the initial part of the output for a given type.
867 This is necessary when dealing with things like functions returning
868 functions. Examples:
870 return type of `int (* fee ())()': pointer -> function -> int. Both
871 pointer (and reference and offset) and function (and member) types must
872 deal with prefix and suffix.
874 Arrays must also do this for DECL nodes, like int a[], and for things like
875 int *[]&. */
877 static void
878 dump_type_prefix (cxx_pretty_printer *pp, tree t, int flags)
880 if (TYPE_PTRMEMFUNC_P (t))
882 t = TYPE_PTRMEMFUNC_FN_TYPE (t);
883 goto offset_type;
886 switch (TREE_CODE (t))
888 case POINTER_TYPE:
889 case REFERENCE_TYPE:
891 tree sub = TREE_TYPE (t);
893 dump_type_prefix (pp, sub, flags);
894 if (TREE_CODE (sub) == ARRAY_TYPE
895 || TREE_CODE (sub) == FUNCTION_TYPE)
897 pp_cxx_whitespace (pp);
898 pp_cxx_left_paren (pp);
899 pp_c_attributes_display (pp, TYPE_ATTRIBUTES (sub));
901 if (TYPE_PTR_P (t))
902 pp_star (pp);
903 else if (TYPE_REF_P (t))
905 if (TYPE_REF_IS_RVALUE (t))
906 pp_ampersand_ampersand (pp);
907 else
908 pp_ampersand (pp);
910 pp->padding = pp_before;
911 pp_cxx_cv_qualifier_seq (pp, t);
913 break;
915 case OFFSET_TYPE:
916 offset_type:
917 dump_type_prefix (pp, TREE_TYPE (t), flags);
918 if (TREE_CODE (t) == OFFSET_TYPE) /* pmfs deal with this in d_t_p */
920 pp_maybe_space (pp);
921 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
922 pp_cxx_left_paren (pp);
923 dump_type (pp, TYPE_OFFSET_BASETYPE (t), flags);
924 pp_cxx_colon_colon (pp);
926 pp_cxx_star (pp);
927 pp_cxx_cv_qualifier_seq (pp, t);
928 pp->padding = pp_before;
929 break;
931 /* This can be reached without a pointer when dealing with
932 templates, e.g. std::is_function. */
933 case FUNCTION_TYPE:
934 dump_type_prefix (pp, TREE_TYPE (t), flags);
935 break;
937 case METHOD_TYPE:
938 dump_type_prefix (pp, TREE_TYPE (t), flags);
939 pp_maybe_space (pp);
940 pp_cxx_left_paren (pp);
941 dump_aggr_type (pp, TYPE_METHOD_BASETYPE (t), flags);
942 pp_cxx_colon_colon (pp);
943 break;
945 case ARRAY_TYPE:
946 dump_type_prefix (pp, TREE_TYPE (t), flags);
947 break;
949 case ENUMERAL_TYPE:
950 case IDENTIFIER_NODE:
951 case INTEGER_TYPE:
952 case BOOLEAN_TYPE:
953 case REAL_TYPE:
954 case RECORD_TYPE:
955 case TEMPLATE_TYPE_PARM:
956 case TEMPLATE_TEMPLATE_PARM:
957 case BOUND_TEMPLATE_TEMPLATE_PARM:
958 case TREE_LIST:
959 case TYPE_DECL:
960 case TREE_VEC:
961 case UNION_TYPE:
962 case LANG_TYPE:
963 case VOID_TYPE:
964 case OPAQUE_TYPE:
965 case TYPENAME_TYPE:
966 case COMPLEX_TYPE:
967 case VECTOR_TYPE:
968 case TYPEOF_TYPE:
969 case UNDERLYING_TYPE:
970 case DECLTYPE_TYPE:
971 case TYPE_PACK_EXPANSION:
972 case FIXED_POINT_TYPE:
973 case NULLPTR_TYPE:
974 dump_type (pp, t, flags);
975 pp->padding = pp_before;
976 break;
978 default:
979 pp_unsupported_tree (pp, t);
980 /* fall through. */
981 case ERROR_MARK:
982 pp_string (pp, M_("<typeprefixerror>"));
983 break;
987 /* Dump the suffix of type T, under control of FLAGS. This is the part
988 which appears after the identifier (or function parms). */
990 static void
991 dump_type_suffix (cxx_pretty_printer *pp, tree t, int flags)
993 if (TYPE_PTRMEMFUNC_P (t))
994 t = TYPE_PTRMEMFUNC_FN_TYPE (t);
996 switch (TREE_CODE (t))
998 case POINTER_TYPE:
999 case REFERENCE_TYPE:
1000 case OFFSET_TYPE:
1001 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
1002 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
1003 pp_cxx_right_paren (pp);
1004 if (TREE_CODE (t) == POINTER_TYPE)
1005 flags |= TFF_POINTER;
1006 dump_type_suffix (pp, TREE_TYPE (t), flags);
1007 break;
1009 case FUNCTION_TYPE:
1010 case METHOD_TYPE:
1012 tree arg;
1013 if (TREE_CODE (t) == METHOD_TYPE)
1014 /* Can only be reached through a pointer. */
1015 pp_cxx_right_paren (pp);
1016 arg = TYPE_ARG_TYPES (t);
1017 if (TREE_CODE (t) == METHOD_TYPE)
1018 arg = TREE_CHAIN (arg);
1020 /* Function pointers don't have default args. Not in standard C++,
1021 anyway; they may in g++, but we'll just pretend otherwise. */
1022 dump_parameters (pp, arg, flags & ~TFF_FUNCTION_DEFAULT_ARGUMENTS);
1024 pp->padding = pp_before;
1025 pp_cxx_cv_qualifiers (pp, type_memfn_quals (t),
1026 TREE_CODE (t) == FUNCTION_TYPE
1027 && (flags & TFF_POINTER));
1028 dump_ref_qualifier (pp, t, flags);
1029 if (tx_safe_fn_type_p (t))
1030 pp_cxx_ws_string (pp, "transaction_safe");
1031 dump_exception_spec (pp, TYPE_RAISES_EXCEPTIONS (t), flags);
1032 dump_type_suffix (pp, TREE_TYPE (t), flags);
1033 break;
1036 case ARRAY_TYPE:
1037 pp_maybe_space (pp);
1038 pp_cxx_left_bracket (pp);
1039 if (tree dtype = TYPE_DOMAIN (t))
1041 tree max = TYPE_MAX_VALUE (dtype);
1042 /* Zero-length arrays have a null upper bound in C and SIZE_MAX
1043 in C++. Handle both since the type might be constructed by
1044 the middle end and end up here as a result of a warning (see
1045 PR c++/97201). */
1046 if (!max || integer_all_onesp (max))
1047 pp_character (pp, '0');
1048 else if (tree_fits_shwi_p (max))
1049 pp_wide_integer (pp, tree_to_shwi (max) + 1);
1050 else
1052 STRIP_NOPS (max);
1053 if (TREE_CODE (max) == SAVE_EXPR)
1054 max = TREE_OPERAND (max, 0);
1055 if (TREE_CODE (max) == MINUS_EXPR
1056 || TREE_CODE (max) == PLUS_EXPR)
1058 max = TREE_OPERAND (max, 0);
1059 while (CONVERT_EXPR_P (max))
1060 max = TREE_OPERAND (max, 0);
1062 else
1063 max = fold_build2_loc (input_location,
1064 PLUS_EXPR, dtype, max,
1065 build_int_cst (dtype, 1));
1066 dump_expr (pp, max, flags & ~TFF_EXPR_IN_PARENS);
1069 pp_cxx_right_bracket (pp);
1070 dump_type_suffix (pp, TREE_TYPE (t), flags);
1071 break;
1073 case ENUMERAL_TYPE:
1074 case IDENTIFIER_NODE:
1075 case INTEGER_TYPE:
1076 case BOOLEAN_TYPE:
1077 case REAL_TYPE:
1078 case RECORD_TYPE:
1079 case TEMPLATE_TYPE_PARM:
1080 case TEMPLATE_TEMPLATE_PARM:
1081 case BOUND_TEMPLATE_TEMPLATE_PARM:
1082 case TREE_LIST:
1083 case TYPE_DECL:
1084 case TREE_VEC:
1085 case UNION_TYPE:
1086 case LANG_TYPE:
1087 case VOID_TYPE:
1088 case OPAQUE_TYPE:
1089 case TYPENAME_TYPE:
1090 case COMPLEX_TYPE:
1091 case VECTOR_TYPE:
1092 case TYPEOF_TYPE:
1093 case UNDERLYING_TYPE:
1094 case DECLTYPE_TYPE:
1095 case TYPE_PACK_EXPANSION:
1096 case FIXED_POINT_TYPE:
1097 case NULLPTR_TYPE:
1098 break;
1100 default:
1101 pp_unsupported_tree (pp, t);
1102 case ERROR_MARK:
1103 /* Don't mark it here, we should have already done in
1104 dump_type_prefix. */
1105 break;
1109 static void
1110 dump_global_iord (cxx_pretty_printer *pp, tree t)
1112 const char *p = NULL;
1114 if (DECL_GLOBAL_CTOR_P (t))
1115 p = M_("(static initializers for %s)");
1116 else if (DECL_GLOBAL_DTOR_P (t))
1117 p = M_("(static destructors for %s)");
1118 else
1119 gcc_unreachable ();
1121 pp_printf (pp, p, DECL_SOURCE_FILE (t));
1124 static void
1125 dump_simple_decl (cxx_pretty_printer *pp, tree t, tree type, int flags)
1127 if (template_parm_object_p (t))
1128 return dump_expr (pp, DECL_INITIAL (t), flags);
1130 if (flags & TFF_DECL_SPECIFIERS)
1132 if (concept_definition_p (t))
1133 pp_cxx_ws_string (pp, "concept");
1134 else if (VAR_P (t) && DECL_DECLARED_CONSTEXPR_P (t))
1135 pp_cxx_ws_string (pp, "constexpr");
1137 if (!standard_concept_p (t))
1138 dump_type_prefix (pp, type, flags & ~TFF_UNQUALIFIED_NAME);
1139 pp_maybe_space (pp);
1141 if (! (flags & TFF_UNQUALIFIED_NAME)
1142 && TREE_CODE (t) != PARM_DECL
1143 && (!DECL_INITIAL (t)
1144 || TREE_CODE (DECL_INITIAL (t)) != TEMPLATE_PARM_INDEX))
1145 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
1146 flags &= ~TFF_UNQUALIFIED_NAME;
1147 if ((flags & TFF_DECL_SPECIFIERS)
1148 && DECL_TEMPLATE_PARM_P (t)
1149 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (t)))
1150 pp_string (pp, "...");
1151 if (DECL_NAME (t))
1153 if (TREE_CODE (t) == FIELD_DECL && DECL_NORMAL_CAPTURE_P (t))
1155 pp_less (pp);
1156 pp_string (pp, IDENTIFIER_POINTER (DECL_NAME (t)) + 2);
1157 pp_string (pp, " capture>");
1159 else
1160 dump_decl (pp, DECL_NAME (t), flags);
1162 else if (DECL_DECOMPOSITION_P (t))
1163 pp_string (pp, M_("<structured bindings>"));
1164 else
1165 pp_string (pp, M_("<anonymous>"));
1167 dump_module_suffix (pp, t);
1169 if (flags & TFF_DECL_SPECIFIERS)
1170 dump_type_suffix (pp, type, flags);
1173 /* Print an IDENTIFIER_NODE that is the name of a declaration. */
1175 static void
1176 dump_decl_name (cxx_pretty_printer *pp, tree t, int flags)
1178 /* These special cases are duplicated here so that other functions
1179 can feed identifiers to error and get them demangled properly. */
1180 if (IDENTIFIER_CONV_OP_P (t))
1182 pp_cxx_ws_string (pp, "operator");
1183 /* Not exactly IDENTIFIER_TYPE_VALUE. */
1184 dump_type (pp, TREE_TYPE (t), flags);
1185 return;
1187 if (dguide_name_p (t))
1189 dump_decl (pp, CLASSTYPE_TI_TEMPLATE (TREE_TYPE (t)),
1190 TFF_UNQUALIFIED_NAME);
1191 return;
1194 const char *str = IDENTIFIER_POINTER (t);
1195 if (startswith (str, "_ZGR"))
1197 pp_cxx_ws_string (pp, "<temporary>");
1198 return;
1201 pp_cxx_tree_identifier (pp, t);
1204 /* Dump a human readable string for the decl T under control of FLAGS. */
1206 static void
1207 dump_decl (cxx_pretty_printer *pp, tree t, int flags)
1209 if (t == NULL_TREE)
1210 return;
1212 /* If doing Objective-C++, give Objective-C a chance to demangle
1213 Objective-C method names. */
1214 if (c_dialect_objc ())
1216 const char *demangled = objc_maybe_printable_name (t, flags);
1217 if (demangled)
1219 pp_string (pp, demangled);
1220 return;
1224 switch (TREE_CODE (t))
1226 case TYPE_DECL:
1227 /* Don't say 'typedef class A' */
1228 if (DECL_ARTIFICIAL (t) && !DECL_SELF_REFERENCE_P (t))
1230 if ((flags & TFF_DECL_SPECIFIERS)
1231 && TREE_CODE (TREE_TYPE (t)) == TEMPLATE_TYPE_PARM)
1233 /* Say `class T' not just `T'. */
1234 pp_cxx_ws_string (pp, "class");
1236 /* Emit the `...' for a parameter pack. */
1237 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
1238 pp_cxx_ws_string (pp, "...");
1241 dump_type (pp, TREE_TYPE (t), flags);
1242 break;
1244 if (TYPE_DECL_ALIAS_P (t)
1245 && (flags & TFF_DECL_SPECIFIERS
1246 || flags & TFF_CLASS_KEY_OR_ENUM))
1248 pp_cxx_ws_string (pp, "using");
1249 if (! (flags & TFF_UNQUALIFIED_NAME))
1250 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
1251 dump_decl (pp, DECL_NAME (t), flags);
1252 pp_cxx_whitespace (pp);
1253 pp_cxx_ws_string (pp, "=");
1254 pp_cxx_whitespace (pp);
1255 dump_type (pp, (DECL_ORIGINAL_TYPE (t)
1256 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t)),
1257 flags);
1258 break;
1260 if ((flags & TFF_DECL_SPECIFIERS)
1261 && !DECL_SELF_REFERENCE_P (t))
1262 pp_cxx_ws_string (pp, "typedef");
1263 dump_simple_decl (pp, t, DECL_ORIGINAL_TYPE (t)
1264 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t),
1265 flags);
1266 break;
1268 case VAR_DECL:
1269 if (DECL_NAME (t) && VTABLE_NAME_P (DECL_NAME (t)))
1271 pp_string (pp, M_("vtable for "));
1272 gcc_assert (TYPE_P (DECL_CONTEXT (t)));
1273 dump_type (pp, DECL_CONTEXT (t), flags);
1274 break;
1276 /* Fall through. */
1277 case FIELD_DECL:
1278 case PARM_DECL:
1279 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1281 /* Handle variable template specializations. */
1282 if (VAR_P (t)
1283 && DECL_LANG_SPECIFIC (t)
1284 && DECL_TEMPLATE_INFO (t)
1285 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1287 pp_cxx_begin_template_argument_list (pp);
1288 tree args = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1289 dump_template_argument_list (pp, args, flags);
1290 pp_cxx_end_template_argument_list (pp);
1292 break;
1294 case RESULT_DECL:
1295 pp_string (pp, M_("<return value> "));
1296 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1297 break;
1299 case NAMESPACE_DECL:
1300 if (flags & TFF_DECL_SPECIFIERS)
1301 pp->declaration (t);
1302 else
1304 if (! (flags & TFF_UNQUALIFIED_NAME))
1305 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
1306 flags &= ~TFF_UNQUALIFIED_NAME;
1307 if (DECL_NAME (t) == NULL_TREE)
1309 if (!(pp->flags & pp_c_flag_gnu_v3))
1310 pp_cxx_ws_string (pp, M_("{anonymous}"));
1311 else
1312 pp_cxx_ws_string (pp, M_("(anonymous namespace)"));
1314 else
1315 pp_cxx_tree_identifier (pp, DECL_NAME (t));
1317 break;
1319 case SCOPE_REF:
1320 dump_type (pp, TREE_OPERAND (t, 0), flags);
1321 pp_cxx_colon_colon (pp);
1322 dump_decl (pp, TREE_OPERAND (t, 1), TFF_UNQUALIFIED_NAME);
1323 break;
1325 case ARRAY_REF:
1326 dump_decl (pp, TREE_OPERAND (t, 0), flags);
1327 pp_cxx_left_bracket (pp);
1328 dump_decl (pp, TREE_OPERAND (t, 1), flags);
1329 pp_cxx_right_bracket (pp);
1330 break;
1332 /* So that we can do dump_decl on an aggr type. */
1333 case RECORD_TYPE:
1334 case UNION_TYPE:
1335 case ENUMERAL_TYPE:
1336 dump_type (pp, t, flags);
1337 break;
1339 case BIT_NOT_EXPR:
1340 /* This is a pseudo destructor call which has not been folded into
1341 a PSEUDO_DTOR_EXPR yet. */
1342 pp_cxx_complement (pp);
1343 dump_type (pp, TREE_OPERAND (t, 0), flags);
1344 break;
1346 case TYPE_EXPR:
1347 gcc_unreachable ();
1348 break;
1350 case IDENTIFIER_NODE:
1351 dump_decl_name (pp, t, flags);
1352 break;
1354 case OVERLOAD:
1355 if (!OVL_SINGLE_P (t))
1357 tree ctx = ovl_scope (t);
1358 if (ctx != global_namespace)
1360 if (TYPE_P (ctx))
1361 dump_type (pp, ctx, flags);
1362 else
1363 dump_decl (pp, ctx, flags);
1364 pp_cxx_colon_colon (pp);
1366 dump_decl (pp, OVL_NAME (t), flags);
1367 break;
1370 /* If there's only one function, just treat it like an ordinary
1371 FUNCTION_DECL. */
1372 t = OVL_FIRST (t);
1373 /* Fall through. */
1375 case FUNCTION_DECL:
1376 if (! DECL_LANG_SPECIFIC (t))
1378 if (DECL_ABSTRACT_ORIGIN (t)
1379 && DECL_ABSTRACT_ORIGIN (t) != t)
1380 dump_decl (pp, DECL_ABSTRACT_ORIGIN (t), flags);
1381 else
1382 dump_function_name (pp, t, flags);
1384 else if (DECL_GLOBAL_CTOR_P (t) || DECL_GLOBAL_DTOR_P (t))
1385 dump_global_iord (pp, t);
1386 else
1387 dump_function_decl (pp, t, flags);
1388 break;
1390 case TEMPLATE_DECL:
1391 dump_template_decl (pp, t, flags);
1392 break;
1394 case CONCEPT_DECL:
1395 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1396 break;
1398 case WILDCARD_DECL:
1399 pp_string (pp, "<wildcard>");
1400 break;
1402 case TEMPLATE_ID_EXPR:
1404 tree name = TREE_OPERAND (t, 0);
1405 tree args = TREE_OPERAND (t, 1);
1407 if (!identifier_p (name))
1408 name = OVL_NAME (name);
1409 dump_decl (pp, name, flags);
1410 pp_cxx_begin_template_argument_list (pp);
1411 if (args == error_mark_node)
1412 pp_string (pp, M_("<template arguments error>"));
1413 else if (args)
1414 dump_template_argument_list
1415 (pp, args, flags|TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS);
1416 pp_cxx_end_template_argument_list (pp);
1418 break;
1420 case LABEL_DECL:
1421 if (DECL_NAME (t))
1422 pp_cxx_tree_identifier (pp, DECL_NAME (t));
1423 else
1424 dump_generic_node (pp, t, 0, TDF_SLIM, false);
1425 break;
1427 case CONST_DECL:
1428 if ((TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == ENUMERAL_TYPE)
1429 || (DECL_INITIAL (t) &&
1430 TREE_CODE (DECL_INITIAL (t)) == TEMPLATE_PARM_INDEX))
1431 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1432 else if (DECL_NAME (t))
1433 dump_decl (pp, DECL_NAME (t), flags);
1434 else if (DECL_INITIAL (t))
1435 dump_expr (pp, DECL_INITIAL (t), flags | TFF_EXPR_IN_PARENS);
1436 else
1437 pp_string (pp, M_("<enumerator>"));
1438 break;
1440 case USING_DECL:
1442 pp_cxx_ws_string (pp, "using");
1443 tree scope = USING_DECL_SCOPE (t);
1444 bool variadic = false;
1445 if (PACK_EXPANSION_P (scope))
1447 scope = PACK_EXPANSION_PATTERN (scope);
1448 variadic = true;
1450 dump_type (pp, scope, flags);
1451 pp_cxx_colon_colon (pp);
1452 dump_decl (pp, DECL_NAME (t), flags);
1453 if (variadic)
1454 pp_cxx_ws_string (pp, "...");
1456 break;
1458 case STATIC_ASSERT:
1459 pp->declaration (t);
1460 break;
1462 case BASELINK:
1463 dump_decl (pp, BASELINK_FUNCTIONS (t), flags);
1464 break;
1466 case NON_DEPENDENT_EXPR:
1467 dump_expr (pp, t, flags);
1468 break;
1470 case TEMPLATE_TYPE_PARM:
1471 if (flags & TFF_DECL_SPECIFIERS)
1472 pp->declaration (t);
1473 else
1474 pp->type_id (t);
1475 break;
1477 case UNBOUND_CLASS_TEMPLATE:
1478 case TYPE_PACK_EXPANSION:
1479 case TREE_BINFO:
1480 dump_type (pp, t, flags);
1481 break;
1483 default:
1484 pp_unsupported_tree (pp, t);
1485 /* Fall through. */
1487 case ERROR_MARK:
1488 pp_string (pp, M_("<declaration error>"));
1489 break;
1493 /* Dump a template declaration T under control of FLAGS. This means the
1494 'template <...> leaders plus the 'class X' or 'void fn(...)' part. */
1496 static void
1497 dump_template_decl (cxx_pretty_printer *pp, tree t, int flags)
1499 tree orig_parms = DECL_TEMPLATE_PARMS (t);
1500 tree parms;
1501 int i;
1503 if (flags & TFF_TEMPLATE_HEADER)
1505 for (parms = orig_parms = nreverse (orig_parms);
1506 parms;
1507 parms = TREE_CHAIN (parms))
1509 tree inner_parms = INNERMOST_TEMPLATE_PARMS (parms);
1510 int len = TREE_VEC_LENGTH (inner_parms);
1512 if (len == 0)
1514 /* Skip over the dummy template levels of a template template
1515 parm. */
1516 gcc_assert (TREE_CODE (TREE_TYPE (t)) == TEMPLATE_TEMPLATE_PARM);
1517 continue;
1520 pp_cxx_ws_string (pp, "template");
1521 pp_cxx_begin_template_argument_list (pp);
1523 /* If we've shown the template prefix, we'd better show the
1524 parameters' and decl's type too. */
1525 flags |= TFF_DECL_SPECIFIERS;
1527 for (i = 0; i < len; i++)
1529 if (i)
1530 pp_separate_with_comma (pp);
1531 dump_template_parameter (pp, TREE_VEC_ELT (inner_parms, i),
1532 flags);
1534 pp_cxx_end_template_argument_list (pp);
1535 pp_cxx_whitespace (pp);
1537 nreverse(orig_parms);
1539 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
1541 /* Say `template<arg> class TT' not just `template<arg> TT'. */
1542 pp_cxx_ws_string (pp, "class");
1544 /* If this is a parameter pack, print the ellipsis. */
1545 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
1546 pp_cxx_ws_string (pp, "...");
1549 /* Only print the requirements if we're also printing
1550 the template header. */
1551 if (flag_concepts)
1552 if (tree ci = get_constraints (t))
1553 if (check_constraint_info (ci))
1554 if (tree reqs = CI_TEMPLATE_REQS (ci))
1556 pp_cxx_requires_clause (pp, reqs);
1557 pp_cxx_whitespace (pp);
1562 if (DECL_CLASS_TEMPLATE_P (t))
1563 dump_type (pp, TREE_TYPE (t),
1564 ((flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1565 | (flags & TFF_DECL_SPECIFIERS ? TFF_CLASS_KEY_OR_ENUM : 0)));
1566 else if (DECL_TEMPLATE_RESULT (t)
1567 && (VAR_P (DECL_TEMPLATE_RESULT (t))
1568 /* Alias template. */
1569 || DECL_TYPE_TEMPLATE_P (t)
1570 /* Concept definition. &*/
1571 || TREE_CODE (DECL_TEMPLATE_RESULT (t)) == CONCEPT_DECL))
1572 dump_decl (pp, DECL_TEMPLATE_RESULT (t), flags | TFF_TEMPLATE_NAME);
1573 else
1575 gcc_assert (TREE_TYPE (t));
1576 switch (NEXT_CODE (t))
1578 case METHOD_TYPE:
1579 case FUNCTION_TYPE:
1580 dump_function_decl (pp, t, flags | TFF_TEMPLATE_NAME);
1581 break;
1582 default:
1583 /* This case can occur with some invalid code. */
1584 dump_type (pp, TREE_TYPE (t),
1585 (flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1586 | (flags & TFF_DECL_SPECIFIERS
1587 ? TFF_CLASS_KEY_OR_ENUM : 0));
1592 /* find_typenames looks through the type of the function template T
1593 and returns a vec containing any typedefs, decltypes or TYPENAME_TYPEs
1594 it finds. */
1596 struct find_typenames_t
1598 hash_set<tree> *p_set;
1599 vec<tree, va_gc> *typenames;
1602 static tree
1603 find_typenames_r (tree *tp, int *walk_subtrees, void *data)
1605 struct find_typenames_t *d = (struct find_typenames_t *)data;
1606 tree mv = NULL_TREE;
1608 if (TYPE_P (*tp) && is_typedef_decl (TYPE_NAME (*tp)))
1609 /* Add the type of the typedef without any additional cv-quals. */
1610 mv = TREE_TYPE (TYPE_NAME (*tp));
1611 else if (TREE_CODE (*tp) == TYPENAME_TYPE
1612 || TREE_CODE (*tp) == DECLTYPE_TYPE)
1613 /* Add the typename without any cv-qualifiers. */
1614 mv = TYPE_MAIN_VARIANT (*tp);
1616 if (PACK_EXPANSION_P (*tp))
1618 /* Don't mess with parameter packs since we don't remember
1619 the pack expansion context for a particular typename. */
1620 *walk_subtrees = false;
1621 return NULL_TREE;
1624 if (mv && (mv == *tp || !d->p_set->add (mv)))
1625 vec_safe_push (d->typenames, mv);
1627 return NULL_TREE;
1630 static vec<tree, va_gc> *
1631 find_typenames (tree t)
1633 struct find_typenames_t ft;
1634 ft.p_set = new hash_set<tree>;
1635 ft.typenames = NULL;
1636 cp_walk_tree (&TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
1637 find_typenames_r, &ft, ft.p_set);
1638 delete ft.p_set;
1639 return ft.typenames;
1642 /* Output the "[with ...]" clause for a template instantiation T iff
1643 TEMPLATE_PARMS, TEMPLATE_ARGS and FLAGS are suitable. T may be NULL if
1644 formatting a deduction/substitution diagnostic rather than an
1645 instantiation. */
1647 static void
1648 dump_substitution (cxx_pretty_printer *pp,
1649 tree t, tree template_parms, tree template_args,
1650 int flags)
1652 if (template_parms != NULL_TREE && template_args != NULL_TREE
1653 && !(flags & TFF_NO_TEMPLATE_BINDINGS))
1655 vec<tree, va_gc> *typenames = t ? find_typenames (t) : NULL;
1656 dump_template_bindings (pp, template_parms, template_args, typenames);
1660 /* Dump the lambda function FN including its 'mutable' qualifier and any
1661 template bindings. */
1663 static void
1664 dump_lambda_function (cxx_pretty_printer *pp,
1665 tree fn, tree template_parms, tree template_args,
1666 int flags)
1668 /* A lambda's signature is essentially its "type". */
1669 dump_type (pp, DECL_CONTEXT (fn), flags);
1670 if (!(TYPE_QUALS (class_of_this_parm (TREE_TYPE (fn))) & TYPE_QUAL_CONST))
1672 pp->padding = pp_before;
1673 pp_c_ws_string (pp, "mutable");
1675 dump_substitution (pp, fn, template_parms, template_args, flags);
1678 /* Pretty print a function decl. There are several ways we want to print a
1679 function declaration. The TFF_ bits in FLAGS tells us how to behave.
1680 As error can only apply the '#' flag once to give 0 and 1 for V, there
1681 is %D which doesn't print the throw specs, and %F which does. */
1683 static void
1684 dump_function_decl (cxx_pretty_printer *pp, tree t, int flags)
1686 tree fntype;
1687 tree parmtypes;
1688 tree cname = NULL_TREE;
1689 tree template_args = NULL_TREE;
1690 tree template_parms = NULL_TREE;
1691 int show_return = flags & TFF_RETURN_TYPE || flags & TFF_DECL_SPECIFIERS;
1692 int do_outer_scope = ! (flags & TFF_UNQUALIFIED_NAME);
1693 tree exceptions;
1694 bool constexpr_p;
1695 tree ret = NULL_TREE;
1697 int dump_function_name_flags = flags & ~TFF_UNQUALIFIED_NAME;
1698 flags = dump_function_name_flags & ~TFF_TEMPLATE_NAME;
1699 if (TREE_CODE (t) == TEMPLATE_DECL)
1700 t = DECL_TEMPLATE_RESULT (t);
1702 /* Save the exceptions, in case t is a specialization and we are
1703 emitting an error about incompatible specifications. */
1704 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (t));
1706 /* Likewise for the constexpr specifier, in case t is a specialization. */
1707 constexpr_p = (DECL_DECLARED_CONSTEXPR_P (t)
1708 && !decl_implicit_constexpr_p (t));
1710 /* Pretty print template instantiations only. */
1711 if (DECL_USE_TEMPLATE (t) && DECL_TEMPLATE_INFO (t)
1712 && !(flags & TFF_NO_TEMPLATE_BINDINGS)
1713 && flag_pretty_templates)
1715 tree tmpl;
1717 template_args = DECL_TI_ARGS (t);
1718 tmpl = most_general_template (t);
1719 if (tmpl && TREE_CODE (tmpl) == TEMPLATE_DECL)
1721 template_parms = DECL_TEMPLATE_PARMS (tmpl);
1722 t = tmpl;
1726 if (DECL_NAME (t) && LAMBDA_FUNCTION_P (t))
1727 return dump_lambda_function (pp, t, template_parms, template_args, flags);
1729 fntype = TREE_TYPE (t);
1730 parmtypes = FUNCTION_FIRST_USER_PARMTYPE (t);
1732 if (DECL_CLASS_SCOPE_P (t))
1733 cname = DECL_CONTEXT (t);
1734 /* This is for partially instantiated template methods. */
1735 else if (TREE_CODE (fntype) == METHOD_TYPE)
1736 cname = TREE_TYPE (TREE_VALUE (parmtypes));
1738 if (flags & TFF_DECL_SPECIFIERS)
1740 if (DECL_STATIC_FUNCTION_P (t))
1741 pp_cxx_ws_string (pp, "static");
1742 else if (DECL_VIRTUAL_P (t))
1743 pp_cxx_ws_string (pp, "virtual");
1745 if (constexpr_p)
1747 if (DECL_DECLARED_CONCEPT_P (t))
1748 pp_cxx_ws_string (pp, "concept");
1749 else if (DECL_IMMEDIATE_FUNCTION_P (t))
1750 pp_cxx_ws_string (pp, "consteval");
1751 else
1752 pp_cxx_ws_string (pp, "constexpr");
1756 /* Print the return type? */
1757 if (show_return)
1758 show_return = (!DECL_CONV_FN_P (t) && !DECL_CONSTRUCTOR_P (t)
1759 && !DECL_DESTRUCTOR_P (t) && !deduction_guide_p (t));
1760 if (show_return)
1762 ret = fndecl_declared_return_type (t);
1763 dump_type_prefix (pp, ret, flags);
1766 /* Print the function name. */
1767 if (!do_outer_scope)
1768 /* Nothing. */;
1769 else if (cname)
1771 dump_type (pp, cname, flags);
1772 pp_cxx_colon_colon (pp);
1774 else
1775 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
1777 /* Name lookup for the rest of the function declarator is implicitly in the
1778 scope of the function, so avoid printing redundant scope qualifiers. */
1779 auto cds = make_temp_override (current_dump_scope, CP_DECL_CONTEXT (t));
1781 dump_function_name (pp, t, dump_function_name_flags);
1783 if (!(flags & TFF_NO_FUNCTION_ARGUMENTS))
1785 dump_parameters (pp, parmtypes, flags);
1787 if (TREE_CODE (fntype) == METHOD_TYPE)
1789 pp->padding = pp_before;
1790 pp_cxx_cv_qualifier_seq (pp, class_of_this_parm (fntype));
1791 dump_ref_qualifier (pp, fntype, flags);
1794 if (tx_safe_fn_type_p (fntype))
1796 pp->padding = pp_before;
1797 pp_cxx_ws_string (pp, "transaction_safe");
1800 if (flags & TFF_EXCEPTION_SPECIFICATION)
1802 pp->padding = pp_before;
1803 dump_exception_spec (pp, exceptions, flags);
1806 if (show_return)
1807 dump_type_suffix (pp, ret, flags);
1808 else if (deduction_guide_p (t))
1810 pp_cxx_ws_string (pp, "->");
1811 dump_type (pp, TREE_TYPE (TREE_TYPE (t)), flags);
1814 if (flag_concepts)
1815 if (tree ci = get_constraints (t))
1816 if (tree reqs = CI_DECLARATOR_REQS (ci))
1817 pp_cxx_requires_clause (pp, reqs);
1819 dump_substitution (pp, t, template_parms, template_args, flags);
1821 if (tree base = DECL_INHERITED_CTOR_BASE (t))
1823 pp_cxx_ws_string (pp, "[inherited from");
1824 dump_type (pp, base, TFF_PLAIN_IDENTIFIER);
1825 pp_character (pp, ']');
1828 else if (template_args)
1830 bool need_comma = false;
1831 int i;
1832 pp_cxx_begin_template_argument_list (pp);
1833 template_args = INNERMOST_TEMPLATE_ARGS (template_args);
1834 for (i = 0; i < TREE_VEC_LENGTH (template_args); ++i)
1836 tree arg = TREE_VEC_ELT (template_args, i);
1837 if (need_comma)
1838 pp_separate_with_comma (pp);
1839 if (ARGUMENT_PACK_P (arg))
1840 pp_cxx_left_brace (pp);
1841 dump_template_argument (pp, arg, TFF_PLAIN_IDENTIFIER);
1842 if (ARGUMENT_PACK_P (arg))
1843 pp_cxx_right_brace (pp);
1844 need_comma = true;
1846 pp_cxx_end_template_argument_list (pp);
1850 /* Print a parameter list. If this is for a member function, the
1851 member object ptr (and any other hidden args) should have
1852 already been removed. */
1854 static void
1855 dump_parameters (cxx_pretty_printer *pp, tree parmtypes, int flags)
1857 int first = 1;
1858 flags &= ~TFF_SCOPE;
1859 pp_cxx_left_paren (pp);
1861 for (first = 1; parmtypes != void_list_node;
1862 parmtypes = TREE_CHAIN (parmtypes))
1864 if (!first)
1865 pp_separate_with_comma (pp);
1866 first = 0;
1867 if (!parmtypes)
1869 pp_cxx_ws_string (pp, "...");
1870 break;
1873 dump_type (pp, TREE_VALUE (parmtypes), flags);
1875 if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && TREE_PURPOSE (parmtypes))
1877 pp_cxx_whitespace (pp);
1878 pp_equal (pp);
1879 pp_cxx_whitespace (pp);
1880 dump_expr (pp, TREE_PURPOSE (parmtypes), flags | TFF_EXPR_IN_PARENS);
1884 pp_cxx_right_paren (pp);
1887 /* Print ref-qualifier of a FUNCTION_TYPE or METHOD_TYPE. FLAGS are ignored. */
1889 static void
1890 dump_ref_qualifier (cxx_pretty_printer *pp, tree t, int flags ATTRIBUTE_UNUSED)
1892 if (FUNCTION_REF_QUALIFIED (t))
1894 pp->padding = pp_before;
1895 if (FUNCTION_RVALUE_QUALIFIED (t))
1896 pp_cxx_ws_string (pp, "&&");
1897 else
1898 pp_cxx_ws_string (pp, "&");
1902 /* Print an exception specification. T is the exception specification. */
1904 static void
1905 dump_exception_spec (cxx_pretty_printer *pp, tree t, int flags)
1907 if (t && TREE_PURPOSE (t))
1909 pp_cxx_ws_string (pp, "noexcept");
1910 if (!integer_onep (TREE_PURPOSE (t)))
1912 pp_cxx_whitespace (pp);
1913 pp_cxx_left_paren (pp);
1914 if (DEFERRED_NOEXCEPT_SPEC_P (t))
1915 pp_cxx_ws_string (pp, "<uninstantiated>");
1916 else
1917 dump_expr (pp, TREE_PURPOSE (t), flags);
1918 pp_cxx_right_paren (pp);
1921 else if (t)
1923 pp_cxx_ws_string (pp, "throw");
1924 pp_cxx_whitespace (pp);
1925 pp_cxx_left_paren (pp);
1926 if (TREE_VALUE (t) != NULL_TREE)
1927 while (1)
1929 dump_type (pp, TREE_VALUE (t), flags);
1930 t = TREE_CHAIN (t);
1931 if (!t)
1932 break;
1933 pp_separate_with_comma (pp);
1935 pp_cxx_right_paren (pp);
1939 /* Handle the function name for a FUNCTION_DECL node, grokking operators
1940 and destructors properly. */
1942 static void
1943 dump_function_name (cxx_pretty_printer *pp, tree t, int flags)
1945 tree name = DECL_NAME (t);
1947 /* We can get here with a decl that was synthesized by language-
1948 independent machinery (e.g. coverage.cc) in which case it won't
1949 have a lang_specific structure attached and DECL_CONSTRUCTOR_P
1950 will crash. In this case it is safe just to print out the
1951 literal name. */
1952 if (!DECL_LANG_SPECIFIC (t))
1954 pp_cxx_tree_identifier (pp, name);
1955 return;
1958 if (TREE_CODE (t) == TEMPLATE_DECL)
1959 t = DECL_TEMPLATE_RESULT (t);
1961 /* Don't let the user see __comp_ctor et al. */
1962 if (DECL_CONSTRUCTOR_P (t)
1963 || DECL_DESTRUCTOR_P (t))
1965 if (LAMBDA_TYPE_P (DECL_CONTEXT (t)))
1966 name = get_identifier ("<lambda>");
1967 else if (TYPE_UNNAMED_P (DECL_CONTEXT (t)))
1968 name = get_identifier ("<constructor>");
1969 else
1970 name = constructor_name (DECL_CONTEXT (t));
1973 if (DECL_DESTRUCTOR_P (t))
1975 pp_cxx_complement (pp);
1976 dump_decl (pp, name, TFF_PLAIN_IDENTIFIER);
1978 else if (DECL_CONV_FN_P (t))
1980 /* This cannot use the hack that the operator's return
1981 type is stashed off of its name because it may be
1982 used for error reporting. In the case of conflicting
1983 declarations, both will have the same name, yet
1984 the types will be different, hence the TREE_TYPE field
1985 of the first name will be clobbered by the second. */
1986 pp_cxx_ws_string (pp, "operator");
1987 dump_type (pp, TREE_TYPE (TREE_TYPE (t)), flags);
1989 else
1990 dump_decl (pp, name, flags);
1992 dump_module_suffix (pp, t);
1994 if (DECL_TEMPLATE_INFO (t)
1995 && !(flags & TFF_TEMPLATE_NAME)
1996 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (t)
1997 && (TREE_CODE (DECL_TI_TEMPLATE (t)) != TEMPLATE_DECL
1998 || PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
1999 dump_template_parms (pp, DECL_TEMPLATE_INFO (t), !DECL_USE_TEMPLATE (t),
2000 flags);
2003 /* Dump the template parameters from the template info INFO under control of
2004 FLAGS. PRIMARY indicates whether this is a primary template decl, or
2005 specialization (partial or complete). For partial specializations we show
2006 the specialized parameter values. For a primary template we show no
2007 decoration. */
2009 static void
2010 dump_template_parms (cxx_pretty_printer *pp, tree info,
2011 int primary, int flags)
2013 tree args = info ? TI_ARGS (info) : NULL_TREE;
2015 if (primary && flags & TFF_TEMPLATE_NAME)
2016 return;
2017 flags &= ~(TFF_CLASS_KEY_OR_ENUM | TFF_TEMPLATE_NAME);
2018 pp_cxx_begin_template_argument_list (pp);
2020 /* Be careful only to print things when we have them, so as not
2021 to crash producing error messages. */
2022 if (args && !primary)
2024 int len, ix;
2025 len = get_non_default_template_args_count (args, flags);
2027 args = INNERMOST_TEMPLATE_ARGS (args);
2028 for (ix = 0; ix != len; ix++)
2030 tree arg = TREE_VEC_ELT (args, ix);
2032 /* Only print a comma if we know there is an argument coming. In
2033 the case of an empty template argument pack, no actual
2034 argument will be printed. */
2035 if (ix
2036 && (!ARGUMENT_PACK_P (arg)
2037 || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) > 0))
2038 pp_separate_with_comma (pp);
2040 if (!arg)
2041 pp_string (pp, M_("<template parameter error>"));
2042 else
2043 dump_template_argument (pp, arg, flags);
2046 else if (primary)
2048 tree tpl = TI_TEMPLATE (info);
2049 tree parms = DECL_TEMPLATE_PARMS (tpl);
2050 int len, ix;
2052 parms = TREE_CODE (parms) == TREE_LIST ? TREE_VALUE (parms) : NULL_TREE;
2053 len = parms ? TREE_VEC_LENGTH (parms) : 0;
2055 for (ix = 0; ix != len; ix++)
2057 tree parm;
2059 if (TREE_VEC_ELT (parms, ix) == error_mark_node)
2061 pp_string (pp, M_("<template parameter error>"));
2062 continue;
2065 parm = TREE_VALUE (TREE_VEC_ELT (parms, ix));
2067 if (ix)
2068 pp_separate_with_comma (pp);
2070 dump_decl (pp, parm, flags & ~TFF_DECL_SPECIFIERS);
2073 pp_cxx_end_template_argument_list (pp);
2076 /* Print out the arguments of CALL_EXPR T as a parenthesized list using
2077 flags FLAGS. Skip over the first argument if SKIPFIRST is true. */
2079 static void
2080 dump_call_expr_args (cxx_pretty_printer *pp, tree t, int flags, bool skipfirst)
2082 tree arg;
2083 call_expr_arg_iterator iter;
2085 pp_cxx_left_paren (pp);
2086 FOR_EACH_CALL_EXPR_ARG (arg, iter, t)
2088 if (skipfirst)
2089 skipfirst = false;
2090 else
2092 dump_expr (pp, arg, flags | TFF_EXPR_IN_PARENS);
2093 if (more_call_expr_args_p (&iter))
2094 pp_separate_with_comma (pp);
2097 pp_cxx_right_paren (pp);
2100 /* Print out the arguments of AGGR_INIT_EXPR T as a parenthesized list
2101 using flags FLAGS. Skip over the first argument if SKIPFIRST is
2102 true. */
2104 static void
2105 dump_aggr_init_expr_args (cxx_pretty_printer *pp, tree t, int flags,
2106 bool skipfirst)
2108 tree arg;
2109 aggr_init_expr_arg_iterator iter;
2111 pp_cxx_left_paren (pp);
2112 FOR_EACH_AGGR_INIT_EXPR_ARG (arg, iter, t)
2114 if (skipfirst)
2115 skipfirst = false;
2116 else
2118 dump_expr (pp, arg, flags | TFF_EXPR_IN_PARENS);
2119 if (more_aggr_init_expr_args_p (&iter))
2120 pp_separate_with_comma (pp);
2123 pp_cxx_right_paren (pp);
2126 /* Print out a list of initializers (subr of dump_expr). */
2128 static void
2129 dump_expr_list (cxx_pretty_printer *pp, tree l, int flags)
2131 while (l)
2133 dump_expr (pp, TREE_VALUE (l), flags | TFF_EXPR_IN_PARENS);
2134 l = TREE_CHAIN (l);
2135 if (l)
2136 pp_separate_with_comma (pp);
2140 /* Print out a vector of initializers (subr of dump_expr). */
2142 static void
2143 dump_expr_init_vec (cxx_pretty_printer *pp, vec<constructor_elt, va_gc> *v,
2144 int flags)
2146 unsigned HOST_WIDE_INT idx;
2147 tree value;
2149 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
2151 dump_expr (pp, value, flags | TFF_EXPR_IN_PARENS);
2152 if (idx != v->length () - 1)
2153 pp_separate_with_comma (pp);
2158 /* We've gotten an indirect REFERENCE (an OBJ_TYPE_REF) to a virtual
2159 function. Resolve it to a close relative -- in the sense of static
2160 type -- variant being overridden. That is close to what was written in
2161 the source code. Subroutine of dump_expr. */
2163 static tree
2164 resolve_virtual_fun_from_obj_type_ref (tree ref)
2166 tree obj_type = TREE_TYPE (OBJ_TYPE_REF_TOKEN (ref));
2167 HOST_WIDE_INT index = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (ref));
2168 tree fun = BINFO_VIRTUALS (TYPE_BINFO (TREE_TYPE (obj_type)));
2169 while (index)
2171 fun = TREE_CHAIN (fun);
2172 index -= (TARGET_VTABLE_USES_DESCRIPTORS
2173 ? TARGET_VTABLE_USES_DESCRIPTORS : 1);
2176 return BV_FN (fun);
2179 /* Print out an expression E under control of FLAGS. */
2181 static void
2182 dump_expr (cxx_pretty_printer *pp, tree t, int flags)
2184 tree op;
2186 if (t == 0)
2187 return;
2189 if (STATEMENT_CLASS_P (t))
2191 pp_cxx_ws_string (pp, M_("<statement>"));
2192 return;
2195 switch (TREE_CODE (t))
2197 case VAR_DECL:
2198 case PARM_DECL:
2199 case FIELD_DECL:
2200 case CONST_DECL:
2201 case FUNCTION_DECL:
2202 case TEMPLATE_DECL:
2203 case NAMESPACE_DECL:
2204 case LABEL_DECL:
2205 case WILDCARD_DECL:
2206 case OVERLOAD:
2207 case TYPE_DECL:
2208 case USING_DECL:
2209 case IDENTIFIER_NODE:
2210 dump_decl (pp, t, ((flags & ~(TFF_DECL_SPECIFIERS|TFF_RETURN_TYPE
2211 |TFF_TEMPLATE_HEADER))
2212 | TFF_NO_TEMPLATE_BINDINGS
2213 | TFF_NO_FUNCTION_ARGUMENTS));
2214 break;
2216 case SSA_NAME:
2217 if (SSA_NAME_VAR (t)
2218 && !DECL_ARTIFICIAL (SSA_NAME_VAR (t)))
2219 dump_expr (pp, SSA_NAME_VAR (t), flags);
2220 else
2221 pp_cxx_ws_string (pp, M_("<unknown>"));
2222 break;
2224 case VOID_CST:
2225 case INTEGER_CST:
2226 case REAL_CST:
2227 case STRING_CST:
2228 case COMPLEX_CST:
2229 pp->constant (t);
2230 break;
2232 case USERDEF_LITERAL:
2233 pp_cxx_userdef_literal (pp, t);
2234 break;
2236 case THROW_EXPR:
2237 /* While waiting for caret diagnostics, avoid printing
2238 __cxa_allocate_exception, __cxa_throw, and the like. */
2239 pp_cxx_ws_string (pp, M_("<throw-expression>"));
2240 break;
2242 case PTRMEM_CST:
2243 pp_ampersand (pp);
2244 dump_type (pp, PTRMEM_CST_CLASS (t), flags);
2245 pp_cxx_colon_colon (pp);
2246 pp_cxx_tree_identifier (pp, DECL_NAME (PTRMEM_CST_MEMBER (t)));
2247 break;
2249 case COMPOUND_EXPR:
2250 pp_cxx_left_paren (pp);
2251 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2252 pp_separate_with_comma (pp);
2253 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2254 pp_cxx_right_paren (pp);
2255 break;
2257 case COND_EXPR:
2258 case VEC_COND_EXPR:
2259 pp_cxx_left_paren (pp);
2260 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2261 pp_string (pp, " ? ");
2262 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2263 pp_string (pp, " : ");
2264 dump_expr (pp, TREE_OPERAND (t, 2), flags | TFF_EXPR_IN_PARENS);
2265 pp_cxx_right_paren (pp);
2266 break;
2268 case SAVE_EXPR:
2269 if (TREE_HAS_CONSTRUCTOR (t))
2271 pp_cxx_ws_string (pp, "new");
2272 pp_cxx_whitespace (pp);
2273 dump_type (pp, TREE_TYPE (TREE_TYPE (t)), flags);
2275 else
2276 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2277 break;
2279 case AGGR_INIT_EXPR:
2281 tree fn = NULL_TREE;
2283 if (TREE_CODE (AGGR_INIT_EXPR_FN (t)) == ADDR_EXPR)
2284 fn = TREE_OPERAND (AGGR_INIT_EXPR_FN (t), 0);
2286 if (fn && TREE_CODE (fn) == FUNCTION_DECL)
2288 if (DECL_CONSTRUCTOR_P (fn))
2289 dump_type (pp, DECL_CONTEXT (fn), flags);
2290 else
2291 dump_decl (pp, fn, 0);
2293 else
2294 dump_expr (pp, AGGR_INIT_EXPR_FN (t), 0);
2296 dump_aggr_init_expr_args (pp, t, flags, true);
2297 break;
2299 case CALL_EXPR:
2301 tree fn = CALL_EXPR_FN (t);
2302 bool skipfirst = false;
2304 /* Deal with internal functions. */
2305 if (fn == NULL_TREE)
2307 pp_string (pp, internal_fn_name (CALL_EXPR_IFN (t)));
2308 dump_call_expr_args (pp, t, flags, skipfirst);
2309 break;
2312 if (TREE_CODE (fn) == ADDR_EXPR)
2313 fn = TREE_OPERAND (fn, 0);
2315 /* Nobody is interested in seeing the guts of vcalls. */
2316 if (TREE_CODE (fn) == OBJ_TYPE_REF)
2317 fn = resolve_virtual_fun_from_obj_type_ref (fn);
2319 if (TREE_TYPE (fn) != NULL_TREE
2320 && NEXT_CODE (fn) == METHOD_TYPE
2321 && call_expr_nargs (t))
2323 tree ob = CALL_EXPR_ARG (t, 0);
2324 if (TREE_CODE (ob) == ADDR_EXPR)
2326 dump_expr (pp, TREE_OPERAND (ob, 0),
2327 flags | TFF_EXPR_IN_PARENS);
2328 pp_cxx_dot (pp);
2330 else if (!is_this_parameter (ob))
2332 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2333 pp_cxx_arrow (pp);
2335 skipfirst = true;
2337 if (flag_sanitize & SANITIZE_UNDEFINED
2338 && is_ubsan_builtin_p (fn))
2340 pp_string (cxx_pp, M_("<ubsan routine call>"));
2341 break;
2343 dump_expr (pp, fn, flags | TFF_EXPR_IN_PARENS);
2344 dump_call_expr_args (pp, t, flags, skipfirst);
2346 break;
2348 case TARGET_EXPR:
2349 /* Note that this only works for G++ target exprs. If somebody
2350 builds a general TARGET_EXPR, there's no way to represent that
2351 it initializes anything other that the parameter slot for the
2352 default argument. Note we may have cleared out the first
2353 operand in expand_expr, so don't go killing ourselves. */
2354 if (TREE_OPERAND (t, 1))
2355 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2356 break;
2358 case POINTER_PLUS_EXPR:
2359 dump_binary_op (pp, "+", t, flags);
2360 break;
2362 case POINTER_DIFF_EXPR:
2363 dump_binary_op (pp, "-", t, flags);
2364 break;
2366 case INIT_EXPR:
2367 case MODIFY_EXPR:
2368 dump_binary_op (pp, OVL_OP_INFO (true, NOP_EXPR)->name, t, flags);
2369 break;
2371 case PLUS_EXPR:
2372 case MINUS_EXPR:
2373 case MULT_EXPR:
2374 case TRUNC_DIV_EXPR:
2375 case TRUNC_MOD_EXPR:
2376 case MIN_EXPR:
2377 case MAX_EXPR:
2378 case LSHIFT_EXPR:
2379 case RSHIFT_EXPR:
2380 case BIT_IOR_EXPR:
2381 case BIT_XOR_EXPR:
2382 case BIT_AND_EXPR:
2383 case TRUTH_ANDIF_EXPR:
2384 case TRUTH_ORIF_EXPR:
2385 case LT_EXPR:
2386 case LE_EXPR:
2387 case GT_EXPR:
2388 case GE_EXPR:
2389 case EQ_EXPR:
2390 case NE_EXPR:
2391 case SPACESHIP_EXPR:
2392 case EXACT_DIV_EXPR:
2393 dump_binary_op (pp, OVL_OP_INFO (false, TREE_CODE (t))->name, t, flags);
2394 break;
2396 case CEIL_DIV_EXPR:
2397 case FLOOR_DIV_EXPR:
2398 case ROUND_DIV_EXPR:
2399 case RDIV_EXPR:
2400 dump_binary_op (pp, "/", t, flags);
2401 break;
2403 case CEIL_MOD_EXPR:
2404 case FLOOR_MOD_EXPR:
2405 case ROUND_MOD_EXPR:
2406 dump_binary_op (pp, "%", t, flags);
2407 break;
2409 case COMPONENT_REF:
2411 tree ob = TREE_OPERAND (t, 0);
2412 if (INDIRECT_REF_P (ob))
2414 ob = TREE_OPERAND (ob, 0);
2415 if (!is_this_parameter (ob)
2416 && !is_dummy_object (ob))
2418 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2419 if (TYPE_REF_P (TREE_TYPE (ob)))
2420 pp_cxx_dot (pp);
2421 else
2422 pp_cxx_arrow (pp);
2425 else
2427 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2428 if (TREE_CODE (ob) != ARROW_EXPR)
2429 pp_cxx_dot (pp);
2431 dump_expr (pp, TREE_OPERAND (t, 1), flags & ~TFF_EXPR_IN_PARENS);
2433 break;
2435 case ARRAY_REF:
2436 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2437 pp_cxx_left_bracket (pp);
2438 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2439 pp_cxx_right_bracket (pp);
2440 break;
2442 case UNARY_PLUS_EXPR:
2443 dump_unary_op (pp, "+", t, flags);
2444 break;
2446 case ADDR_EXPR:
2447 if (TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
2448 || TREE_CODE (TREE_OPERAND (t, 0)) == STRING_CST
2449 /* An ADDR_EXPR can have reference type. In that case, we
2450 shouldn't print the `&' doing so indicates to the user
2451 that the expression has pointer type. */
2452 || (TREE_TYPE (t)
2453 && TYPE_REF_P (TREE_TYPE (t))))
2454 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2455 else if (TREE_CODE (TREE_OPERAND (t, 0)) == LABEL_DECL)
2456 dump_unary_op (pp, "&&", t, flags);
2457 else
2458 dump_unary_op (pp, "&", t, flags);
2459 break;
2461 case INDIRECT_REF:
2462 if (TREE_HAS_CONSTRUCTOR (t))
2464 t = TREE_OPERAND (t, 0);
2465 gcc_assert (TREE_CODE (t) == CALL_EXPR);
2466 dump_expr (pp, CALL_EXPR_FN (t), flags | TFF_EXPR_IN_PARENS);
2467 dump_call_expr_args (pp, t, flags, true);
2469 else
2471 if (TREE_OPERAND (t,0) != NULL_TREE
2472 && TREE_TYPE (TREE_OPERAND (t, 0))
2473 && NEXT_CODE (TREE_OPERAND (t, 0)) == REFERENCE_TYPE)
2474 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2475 else
2476 dump_unary_op (pp, "*", t, flags);
2478 break;
2480 case MEM_REF:
2481 /* Delegate to the base "C" pretty printer. */
2482 pp->c_pretty_printer::unary_expression (t);
2483 break;
2485 case TARGET_MEM_REF:
2486 /* TARGET_MEM_REF can't appear directly from source, but can appear
2487 during late GIMPLE optimizations and through late diagnostic we might
2488 need to support it. Print it as dereferencing of a pointer after
2489 cast to the TARGET_MEM_REF type, with pointer arithmetics on some
2490 pointer to single byte types, so
2491 *(type *)((char *) ptr + step * index + index2) if all the operands
2492 are present and the casts are needed. */
2493 pp_cxx_star (pp);
2494 pp_cxx_left_paren (pp);
2495 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (TMR_BASE (t)))) == NULL_TREE
2496 || !integer_onep (TYPE_SIZE_UNIT
2497 (TREE_TYPE (TREE_TYPE (TMR_BASE (t))))))
2499 if (TYPE_SIZE_UNIT (TREE_TYPE (t))
2500 && integer_onep (TYPE_SIZE_UNIT (TREE_TYPE (t))))
2502 pp_cxx_left_paren (pp);
2503 dump_type (pp, build_pointer_type (TREE_TYPE (t)), flags);
2505 else
2507 dump_type (pp, build_pointer_type (TREE_TYPE (t)), flags);
2508 pp_cxx_right_paren (pp);
2509 pp_cxx_left_paren (pp);
2510 pp_cxx_left_paren (pp);
2511 dump_type (pp, build_pointer_type (char_type_node), flags);
2513 pp_cxx_right_paren (pp);
2515 else if (!same_type_p (TREE_TYPE (t),
2516 TREE_TYPE (TREE_TYPE (TMR_BASE (t)))))
2518 dump_type (pp, build_pointer_type (TREE_TYPE (t)), flags);
2519 pp_cxx_right_paren (pp);
2520 pp_cxx_left_paren (pp);
2522 dump_expr (pp, TMR_BASE (t), flags);
2523 if (TMR_STEP (t) && TMR_INDEX (t))
2525 pp_cxx_ws_string (pp, "+");
2526 dump_expr (pp, TMR_INDEX (t), flags);
2527 pp_cxx_ws_string (pp, "*");
2528 dump_expr (pp, TMR_STEP (t), flags);
2530 if (TMR_INDEX2 (t))
2532 pp_cxx_ws_string (pp, "+");
2533 dump_expr (pp, TMR_INDEX2 (t), flags);
2535 if (!integer_zerop (TMR_OFFSET (t)))
2537 pp_cxx_ws_string (pp, "+");
2538 dump_expr (pp, fold_convert (ssizetype, TMR_OFFSET (t)), flags);
2540 pp_cxx_right_paren (pp);
2541 break;
2543 case NEGATE_EXPR:
2544 case BIT_NOT_EXPR:
2545 case TRUTH_NOT_EXPR:
2546 case PREDECREMENT_EXPR:
2547 case PREINCREMENT_EXPR:
2548 dump_unary_op (pp, OVL_OP_INFO (false, TREE_CODE (t))->name, t, flags);
2549 break;
2551 case POSTDECREMENT_EXPR:
2552 case POSTINCREMENT_EXPR:
2553 pp_cxx_left_paren (pp);
2554 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2555 pp_cxx_ws_string (pp, OVL_OP_INFO (false, TREE_CODE (t))->name);
2556 pp_cxx_right_paren (pp);
2557 break;
2559 case NON_LVALUE_EXPR:
2560 /* FIXME: This is a KLUDGE workaround for a parsing problem. There
2561 should be another level of INDIRECT_REF so that I don't have to do
2562 this. */
2563 if (TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == POINTER_TYPE)
2565 tree next = TREE_TYPE (TREE_TYPE (t));
2567 while (TYPE_PTR_P (next))
2568 next = TREE_TYPE (next);
2570 if (TREE_CODE (next) == FUNCTION_TYPE)
2572 if (flags & TFF_EXPR_IN_PARENS)
2573 pp_cxx_left_paren (pp);
2574 pp_cxx_star (pp);
2575 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2576 if (flags & TFF_EXPR_IN_PARENS)
2577 pp_cxx_right_paren (pp);
2578 break;
2580 /* Else fall through. */
2582 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2583 break;
2585 CASE_CONVERT:
2586 case IMPLICIT_CONV_EXPR:
2587 case VIEW_CONVERT_EXPR:
2589 tree op = TREE_OPERAND (t, 0);
2591 if (location_wrapper_p (t))
2593 dump_expr (pp, op, flags);
2594 break;
2597 tree ttype = TREE_TYPE (t);
2598 tree optype = TREE_TYPE (op);
2600 if (TREE_CODE (ttype) != TREE_CODE (optype)
2601 && INDIRECT_TYPE_P (ttype)
2602 && INDIRECT_TYPE_P (optype)
2603 && same_type_p (TREE_TYPE (optype),
2604 TREE_TYPE (ttype)))
2606 if (TYPE_REF_P (ttype))
2608 STRIP_NOPS (op);
2609 if (TREE_CODE (op) == ADDR_EXPR)
2610 dump_expr (pp, TREE_OPERAND (op, 0), flags);
2611 else
2612 dump_unary_op (pp, "*", t, flags);
2614 else
2615 dump_unary_op (pp, "&", t, flags);
2617 else if (!same_type_p (TREE_TYPE (op), TREE_TYPE (t)))
2619 /* It is a cast, but we cannot tell whether it is a
2620 reinterpret or static cast. Use the C style notation. */
2621 if (flags & TFF_EXPR_IN_PARENS)
2622 pp_cxx_left_paren (pp);
2623 pp_cxx_left_paren (pp);
2624 dump_type (pp, TREE_TYPE (t), flags);
2625 pp_cxx_right_paren (pp);
2626 dump_expr (pp, op, flags | TFF_EXPR_IN_PARENS);
2627 if (flags & TFF_EXPR_IN_PARENS)
2628 pp_cxx_right_paren (pp);
2630 else
2631 dump_expr (pp, op, flags);
2632 break;
2635 case CONSTRUCTOR:
2636 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
2638 tree idx = build_ptrmemfunc_access_expr (t, pfn_identifier);
2640 if (integer_zerop (idx))
2642 /* A NULL pointer-to-member constant. */
2643 pp_cxx_left_paren (pp);
2644 pp_cxx_left_paren (pp);
2645 dump_type (pp, TREE_TYPE (t), flags);
2646 pp_cxx_right_paren (pp);
2647 pp_character (pp, '0');
2648 pp_cxx_right_paren (pp);
2649 break;
2651 else if (tree_fits_shwi_p (idx))
2653 tree virtuals;
2654 unsigned HOST_WIDE_INT n;
2656 t = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
2657 t = TYPE_METHOD_BASETYPE (t);
2658 virtuals = BINFO_VIRTUALS (TYPE_BINFO (TYPE_MAIN_VARIANT (t)));
2660 n = tree_to_shwi (idx);
2662 /* Map vtable index back one, to allow for the null pointer to
2663 member. */
2664 --n;
2666 while (n > 0 && virtuals)
2668 --n;
2669 virtuals = TREE_CHAIN (virtuals);
2671 if (virtuals)
2673 dump_expr (pp, BV_FN (virtuals),
2674 flags | TFF_EXPR_IN_PARENS);
2675 break;
2679 if (TREE_TYPE (t) && LAMBDA_TYPE_P (TREE_TYPE (t)))
2680 pp_string (pp, "<lambda closure object>");
2681 if (TREE_TYPE (t) && EMPTY_CONSTRUCTOR_P (t))
2683 dump_type (pp, TREE_TYPE (t), 0);
2684 pp_cxx_left_paren (pp);
2685 pp_cxx_right_paren (pp);
2687 else
2689 if (!BRACE_ENCLOSED_INITIALIZER_P (t))
2690 dump_type (pp, TREE_TYPE (t), 0);
2691 pp_cxx_left_brace (pp);
2692 dump_expr_init_vec (pp, CONSTRUCTOR_ELTS (t), flags);
2693 pp_cxx_right_brace (pp);
2696 break;
2698 case OFFSET_REF:
2700 tree ob = TREE_OPERAND (t, 0);
2701 if (is_dummy_object (ob))
2703 t = TREE_OPERAND (t, 1);
2704 if (TREE_CODE (t) == FUNCTION_DECL)
2705 /* A::f */
2706 dump_expr (pp, t, flags | TFF_EXPR_IN_PARENS);
2707 else if (BASELINK_P (t))
2708 dump_expr (pp, OVL_FIRST (BASELINK_FUNCTIONS (t)),
2709 flags | TFF_EXPR_IN_PARENS);
2710 else
2711 dump_decl (pp, t, flags);
2713 else
2715 if (INDIRECT_REF_P (ob))
2717 dump_expr (pp, TREE_OPERAND (ob, 0), flags | TFF_EXPR_IN_PARENS);
2718 pp_cxx_arrow (pp);
2719 pp_cxx_star (pp);
2721 else
2723 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2724 pp_cxx_dot (pp);
2725 pp_cxx_star (pp);
2727 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2729 break;
2732 case TEMPLATE_PARM_INDEX:
2733 dump_decl (pp, TEMPLATE_PARM_DECL (t), flags & ~TFF_DECL_SPECIFIERS);
2734 break;
2736 case CAST_EXPR:
2737 if (TREE_OPERAND (t, 0) == NULL_TREE
2738 || TREE_CHAIN (TREE_OPERAND (t, 0)))
2740 dump_type (pp, TREE_TYPE (t), flags);
2741 pp_cxx_left_paren (pp);
2742 dump_expr_list (pp, TREE_OPERAND (t, 0), flags);
2743 pp_cxx_right_paren (pp);
2745 else
2747 pp_cxx_left_paren (pp);
2748 dump_type (pp, TREE_TYPE (t), flags);
2749 pp_cxx_right_paren (pp);
2750 pp_cxx_left_paren (pp);
2751 dump_expr_list (pp, TREE_OPERAND (t, 0), flags);
2752 pp_cxx_right_paren (pp);
2754 break;
2756 case STATIC_CAST_EXPR:
2757 pp_cxx_ws_string (pp, "static_cast");
2758 goto cast;
2759 case REINTERPRET_CAST_EXPR:
2760 pp_cxx_ws_string (pp, "reinterpret_cast");
2761 goto cast;
2762 case CONST_CAST_EXPR:
2763 pp_cxx_ws_string (pp, "const_cast");
2764 goto cast;
2765 case DYNAMIC_CAST_EXPR:
2766 pp_cxx_ws_string (pp, "dynamic_cast");
2767 cast:
2768 pp_cxx_begin_template_argument_list (pp);
2769 dump_type (pp, TREE_TYPE (t), flags);
2770 pp_cxx_end_template_argument_list (pp);
2771 pp_cxx_left_paren (pp);
2772 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2773 pp_cxx_right_paren (pp);
2774 break;
2776 case ARROW_EXPR:
2777 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2778 pp_cxx_arrow (pp);
2779 break;
2781 case SIZEOF_EXPR:
2782 case ALIGNOF_EXPR:
2783 if (TREE_CODE (t) == SIZEOF_EXPR)
2784 pp_cxx_ws_string (pp, "sizeof");
2785 else
2787 gcc_assert (TREE_CODE (t) == ALIGNOF_EXPR);
2788 pp_cxx_ws_string (pp, "__alignof__");
2790 op = TREE_OPERAND (t, 0);
2791 if (PACK_EXPANSION_P (op))
2793 pp_string (pp, "...");
2794 op = PACK_EXPANSION_PATTERN (op);
2796 pp_cxx_whitespace (pp);
2797 pp_cxx_left_paren (pp);
2798 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
2799 dump_type (pp, TREE_TYPE (op), flags);
2800 else if (TYPE_P (TREE_OPERAND (t, 0)))
2801 dump_type (pp, op, flags);
2802 else
2803 dump_expr (pp, op, flags);
2804 pp_cxx_right_paren (pp);
2805 break;
2807 case AT_ENCODE_EXPR:
2808 pp_cxx_ws_string (pp, "@encode");
2809 pp_cxx_whitespace (pp);
2810 pp_cxx_left_paren (pp);
2811 dump_type (pp, TREE_OPERAND (t, 0), flags);
2812 pp_cxx_right_paren (pp);
2813 break;
2815 case NOEXCEPT_EXPR:
2816 pp_cxx_ws_string (pp, "noexcept");
2817 pp_cxx_whitespace (pp);
2818 pp_cxx_left_paren (pp);
2819 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2820 pp_cxx_right_paren (pp);
2821 break;
2823 case REALPART_EXPR:
2824 case IMAGPART_EXPR:
2825 pp_cxx_ws_string (pp, OVL_OP_INFO (false, TREE_CODE (t))->name);
2826 pp_cxx_whitespace (pp);
2827 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2828 break;
2830 case DEFERRED_PARSE:
2831 pp_string (pp, M_("<unparsed>"));
2832 break;
2834 case TRY_CATCH_EXPR:
2835 case CLEANUP_POINT_EXPR:
2836 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2837 break;
2839 case PSEUDO_DTOR_EXPR:
2840 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2841 pp_cxx_dot (pp);
2842 if (TREE_OPERAND (t, 1))
2844 dump_type (pp, TREE_OPERAND (t, 1), flags);
2845 pp_cxx_colon_colon (pp);
2847 pp_cxx_complement (pp);
2848 dump_type (pp, TREE_OPERAND (t, 2), flags);
2849 break;
2851 case TEMPLATE_ID_EXPR:
2852 dump_decl (pp, t, flags);
2853 break;
2855 case BIND_EXPR:
2856 case STMT_EXPR:
2857 case EXPR_STMT:
2858 case STATEMENT_LIST:
2859 /* We don't yet have a way of dumping statements in a
2860 human-readable format. */
2861 pp_string (pp, "({...})");
2862 break;
2864 case LOOP_EXPR:
2865 pp_string (pp, "while (1) { ");
2866 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2867 pp_cxx_right_brace (pp);
2868 break;
2870 case EXIT_EXPR:
2871 pp_string (pp, "if (");
2872 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2873 pp_string (pp, ") break; ");
2874 break;
2876 case BASELINK:
2877 dump_expr (pp, BASELINK_FUNCTIONS (t), flags & ~TFF_EXPR_IN_PARENS);
2878 break;
2880 case EMPTY_CLASS_EXPR:
2881 dump_type (pp, TREE_TYPE (t), flags);
2882 pp_cxx_left_paren (pp);
2883 pp_cxx_right_paren (pp);
2884 break;
2886 case NON_DEPENDENT_EXPR:
2887 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2888 break;
2890 case ARGUMENT_PACK_SELECT:
2891 dump_template_argument (pp, ARGUMENT_PACK_SELECT_FROM_PACK (t), flags);
2892 break;
2894 case RECORD_TYPE:
2895 case UNION_TYPE:
2896 case ENUMERAL_TYPE:
2897 case REAL_TYPE:
2898 case VOID_TYPE:
2899 case OPAQUE_TYPE:
2900 case BOOLEAN_TYPE:
2901 case INTEGER_TYPE:
2902 case COMPLEX_TYPE:
2903 case VECTOR_TYPE:
2904 case DECLTYPE_TYPE:
2905 pp_type_specifier_seq (pp, t);
2906 break;
2908 case TYPENAME_TYPE:
2909 /* We get here when we want to print a dependent type as an
2910 id-expression, without any disambiguator decoration. */
2911 pp->id_expression (t);
2912 break;
2914 case TEMPLATE_TYPE_PARM:
2915 case TEMPLATE_TEMPLATE_PARM:
2916 case BOUND_TEMPLATE_TEMPLATE_PARM:
2917 dump_type (pp, t, flags);
2918 break;
2920 case TRAIT_EXPR:
2921 pp_cxx_trait_expression (pp, t);
2922 break;
2924 case VA_ARG_EXPR:
2925 pp_cxx_va_arg_expression (pp, t);
2926 break;
2928 case OFFSETOF_EXPR:
2929 pp_cxx_offsetof_expression (pp, t);
2930 break;
2932 case ADDRESSOF_EXPR:
2933 pp_cxx_addressof_expression (pp, t);
2934 break;
2936 case SCOPE_REF:
2937 dump_decl (pp, t, flags);
2938 break;
2940 case EXPR_PACK_EXPANSION:
2941 case UNARY_LEFT_FOLD_EXPR:
2942 case UNARY_RIGHT_FOLD_EXPR:
2943 case BINARY_LEFT_FOLD_EXPR:
2944 case BINARY_RIGHT_FOLD_EXPR:
2945 case TYPEID_EXPR:
2946 case MEMBER_REF:
2947 case DOTSTAR_EXPR:
2948 case NEW_EXPR:
2949 case VEC_NEW_EXPR:
2950 case DELETE_EXPR:
2951 case VEC_DELETE_EXPR:
2952 case MODOP_EXPR:
2953 case ABS_EXPR:
2954 case ABSU_EXPR:
2955 case CONJ_EXPR:
2956 case VECTOR_CST:
2957 case FIXED_CST:
2958 case UNORDERED_EXPR:
2959 case ORDERED_EXPR:
2960 case UNLT_EXPR:
2961 case UNLE_EXPR:
2962 case UNGT_EXPR:
2963 case UNGE_EXPR:
2964 case UNEQ_EXPR:
2965 case LTGT_EXPR:
2966 case COMPLEX_EXPR:
2967 case BIT_FIELD_REF:
2968 case FIX_TRUNC_EXPR:
2969 case FLOAT_EXPR:
2970 pp->expression (t);
2971 break;
2973 case TRUTH_AND_EXPR:
2974 case TRUTH_OR_EXPR:
2975 case TRUTH_XOR_EXPR:
2976 if (flags & TFF_EXPR_IN_PARENS)
2977 pp_cxx_left_paren (pp);
2978 pp->expression (t);
2979 if (flags & TFF_EXPR_IN_PARENS)
2980 pp_cxx_right_paren (pp);
2981 break;
2983 case OBJ_TYPE_REF:
2984 dump_expr (pp, resolve_virtual_fun_from_obj_type_ref (t), flags);
2985 break;
2987 case LAMBDA_EXPR:
2988 pp_string (pp, M_("<lambda>"));
2989 break;
2991 case PAREN_EXPR:
2992 pp_cxx_left_paren (pp);
2993 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2994 pp_cxx_right_paren (pp);
2995 break;
2997 case REQUIRES_EXPR:
2998 pp_cxx_requires_expr (cxx_pp, t);
2999 break;
3001 case SIMPLE_REQ:
3002 pp_cxx_simple_requirement (cxx_pp, t);
3003 break;
3005 case TYPE_REQ:
3006 pp_cxx_type_requirement (cxx_pp, t);
3007 break;
3009 case COMPOUND_REQ:
3010 pp_cxx_compound_requirement (cxx_pp, t);
3011 break;
3013 case NESTED_REQ:
3014 pp_cxx_nested_requirement (cxx_pp, t);
3015 break;
3017 case ATOMIC_CONSTR:
3018 case CHECK_CONSTR:
3019 case CONJ_CONSTR:
3020 case DISJ_CONSTR:
3022 pp_cxx_constraint (cxx_pp, t);
3023 break;
3026 case PLACEHOLDER_EXPR:
3027 pp_string (pp, M_("*this"));
3028 break;
3030 case TREE_LIST:
3031 dump_expr_list (pp, t, flags);
3032 break;
3034 /* This list is incomplete, but should suffice for now.
3035 It is very important that `sorry' does not call
3036 `report_error_function'. That could cause an infinite loop. */
3037 default:
3038 pp_unsupported_tree (pp, t);
3039 /* Fall through. */
3040 case ERROR_MARK:
3041 pp_string (pp, M_("<expression error>"));
3042 break;
3046 static void
3047 dump_binary_op (cxx_pretty_printer *pp, const char *opstring, tree t,
3048 int flags)
3050 pp_cxx_left_paren (pp);
3051 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
3052 pp_cxx_whitespace (pp);
3053 if (opstring)
3054 pp_cxx_ws_string (pp, opstring);
3055 else
3056 pp_string (pp, M_("<unknown operator>"));
3057 pp_cxx_whitespace (pp);
3058 tree op1 = TREE_OPERAND (t, 1);
3059 if (TREE_CODE (t) == POINTER_PLUS_EXPR
3060 && TREE_CODE (op1) == INTEGER_CST
3061 && tree_int_cst_sign_bit (op1))
3062 /* A pointer minus an integer is represented internally as plus a very
3063 large number, don't expose that to users. */
3064 op1 = convert (ssizetype, op1);
3065 dump_expr (pp, op1, flags | TFF_EXPR_IN_PARENS);
3066 pp_cxx_right_paren (pp);
3069 static void
3070 dump_unary_op (cxx_pretty_printer *pp, const char *opstring, tree t, int flags)
3072 if (flags & TFF_EXPR_IN_PARENS)
3073 pp_cxx_left_paren (pp);
3074 pp_cxx_ws_string (pp, opstring);
3075 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
3076 if (flags & TFF_EXPR_IN_PARENS)
3077 pp_cxx_right_paren (pp);
3080 static void
3081 reinit_cxx_pp (void)
3083 pp_clear_output_area (cxx_pp);
3084 cxx_pp->padding = pp_none;
3085 pp_indentation (cxx_pp) = 0;
3086 pp_needs_newline (cxx_pp) = false;
3087 cxx_pp->enclosing_scope = current_function_decl;
3090 /* Same as pp_formatted_text, except the return string is a separate
3091 copy and has a GGC storage duration, e.g. an indefinite lifetime. */
3093 inline const char *
3094 pp_ggc_formatted_text (pretty_printer *pp)
3096 return ggc_strdup (pp_formatted_text (pp));
3099 /* Exported interface to stringifying types, exprs and decls under TFF_*
3100 control. */
3102 const char *
3103 type_as_string (tree typ, int flags)
3105 reinit_cxx_pp ();
3106 pp_translate_identifiers (cxx_pp) = false;
3107 dump_type (cxx_pp, typ, flags);
3108 return pp_ggc_formatted_text (cxx_pp);
3111 const char *
3112 type_as_string_translate (tree typ, int flags)
3114 reinit_cxx_pp ();
3115 dump_type (cxx_pp, typ, flags);
3116 return pp_ggc_formatted_text (cxx_pp);
3119 const char *
3120 expr_as_string (tree decl, int flags)
3122 reinit_cxx_pp ();
3123 pp_translate_identifiers (cxx_pp) = false;
3124 dump_expr (cxx_pp, decl, flags);
3125 return pp_ggc_formatted_text (cxx_pp);
3128 /* Wrap decl_as_string with options appropriate for dwarf. */
3130 const char *
3131 decl_as_dwarf_string (tree decl, int flags)
3133 const char *name;
3134 /* Curiously, reinit_cxx_pp doesn't reset the flags field, so setting the flag
3135 here will be adequate to get the desired behavior. */
3136 cxx_pp->flags |= pp_c_flag_gnu_v3;
3137 name = decl_as_string (decl, flags);
3138 /* Subsequent calls to the pretty printer shouldn't use this style. */
3139 cxx_pp->flags &= ~pp_c_flag_gnu_v3;
3140 return name;
3143 const char *
3144 decl_as_string (tree decl, int flags)
3146 reinit_cxx_pp ();
3147 pp_translate_identifiers (cxx_pp) = false;
3148 dump_decl (cxx_pp, decl, flags);
3149 return pp_ggc_formatted_text (cxx_pp);
3152 const char *
3153 decl_as_string_translate (tree decl, int flags)
3155 reinit_cxx_pp ();
3156 dump_decl (cxx_pp, decl, flags);
3157 return pp_ggc_formatted_text (cxx_pp);
3160 /* Wrap lang_decl_name with options appropriate for dwarf. */
3162 const char *
3163 lang_decl_dwarf_name (tree decl, int v, bool translate)
3165 const char *name;
3166 /* Curiously, reinit_cxx_pp doesn't reset the flags field, so setting the flag
3167 here will be adequate to get the desired behavior. */
3168 cxx_pp->flags |= pp_c_flag_gnu_v3;
3169 name = lang_decl_name (decl, v, translate);
3170 /* Subsequent calls to the pretty printer shouldn't use this style. */
3171 cxx_pp->flags &= ~pp_c_flag_gnu_v3;
3172 return name;
3175 /* Generate the three forms of printable names for cxx_printable_name. */
3177 const char *
3178 lang_decl_name (tree decl, int v, bool translate)
3180 if (v >= 2)
3181 return (translate
3182 ? decl_as_string_translate (decl, TFF_DECL_SPECIFIERS)
3183 : decl_as_string (decl, TFF_DECL_SPECIFIERS));
3185 reinit_cxx_pp ();
3186 pp_translate_identifiers (cxx_pp) = translate;
3187 if (v == 1
3188 && (DECL_CLASS_SCOPE_P (decl)
3189 || (DECL_NAMESPACE_SCOPE_P (decl)
3190 && CP_DECL_CONTEXT (decl) != global_namespace)))
3192 dump_type (cxx_pp, CP_DECL_CONTEXT (decl), TFF_PLAIN_IDENTIFIER);
3193 pp_cxx_colon_colon (cxx_pp);
3196 if (TREE_CODE (decl) == FUNCTION_DECL)
3197 dump_function_name (cxx_pp, decl, TFF_PLAIN_IDENTIFIER);
3198 else if ((DECL_NAME (decl) == NULL_TREE)
3199 && TREE_CODE (decl) == NAMESPACE_DECL)
3200 dump_decl (cxx_pp, decl, TFF_PLAIN_IDENTIFIER | TFF_UNQUALIFIED_NAME);
3201 else
3202 dump_decl (cxx_pp, DECL_NAME (decl), TFF_PLAIN_IDENTIFIER);
3204 return pp_ggc_formatted_text (cxx_pp);
3207 /* Return the location of a tree passed to %+ formats. */
3209 location_t
3210 location_of (tree t)
3212 if (TYPE_P (t))
3214 t = TYPE_MAIN_DECL (t);
3215 if (t == NULL_TREE)
3216 return input_location;
3218 else if (TREE_CODE (t) == OVERLOAD)
3219 t = OVL_FIRST (t);
3221 if (DECL_P (t))
3222 return DECL_SOURCE_LOCATION (t);
3223 if (TREE_CODE (t) == DEFERRED_PARSE)
3224 return defparse_location (t);
3225 return cp_expr_loc_or_input_loc (t);
3228 /* Now the interfaces from error et al to dump_type et al. Each takes an
3229 on/off VERBOSE flag and supply the appropriate TFF_ flags to a dump_
3230 function. */
3232 static const char *
3233 decl_to_string (tree decl, int verbose)
3235 int flags = 0;
3237 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == RECORD_TYPE
3238 || TREE_CODE (decl) == UNION_TYPE || TREE_CODE (decl) == ENUMERAL_TYPE)
3239 flags = TFF_CLASS_KEY_OR_ENUM;
3240 if (verbose)
3241 flags |= TFF_DECL_SPECIFIERS;
3242 else if (TREE_CODE (decl) == FUNCTION_DECL)
3243 flags |= TFF_DECL_SPECIFIERS | TFF_RETURN_TYPE;
3244 flags |= TFF_TEMPLATE_HEADER;
3246 reinit_cxx_pp ();
3247 dump_decl (cxx_pp, decl, flags);
3248 return pp_ggc_formatted_text (cxx_pp);
3251 const char *
3252 expr_to_string (tree decl)
3254 reinit_cxx_pp ();
3255 dump_expr (cxx_pp, decl, 0);
3256 return pp_ggc_formatted_text (cxx_pp);
3259 static const char *
3260 fndecl_to_string (tree fndecl, int verbose)
3262 int flags;
3264 flags = TFF_EXCEPTION_SPECIFICATION | TFF_DECL_SPECIFIERS
3265 | TFF_TEMPLATE_HEADER;
3266 if (verbose)
3267 flags |= TFF_FUNCTION_DEFAULT_ARGUMENTS;
3268 reinit_cxx_pp ();
3269 dump_decl (cxx_pp, fndecl, flags);
3270 return pp_ggc_formatted_text (cxx_pp);
3274 static const char *
3275 code_to_string (enum tree_code c)
3277 return get_tree_code_name (c);
3280 const char *
3281 language_to_string (enum languages c)
3283 switch (c)
3285 case lang_c:
3286 return "C";
3288 case lang_cplusplus:
3289 return "C++";
3291 default:
3292 gcc_unreachable ();
3294 return NULL;
3297 /* Return the proper printed version of a parameter to a C++ function. */
3299 static const char *
3300 parm_to_string (int p)
3302 reinit_cxx_pp ();
3303 if (p < 0)
3304 pp_string (cxx_pp, "'this'");
3305 else
3306 pp_decimal_int (cxx_pp, p + 1);
3307 return pp_ggc_formatted_text (cxx_pp);
3310 static const char *
3311 op_to_string (bool assop, enum tree_code p)
3313 tree id = ovl_op_identifier (assop, p);
3314 return id ? IDENTIFIER_POINTER (id) : M_("<unknown>");
3317 /* Return a GC-allocated representation of type TYP, with verbosity VERBOSE.
3319 If QUOTE is non-NULL and if *QUOTE is true, then quotes are added to the
3320 string in appropriate places, and *QUOTE is written to with false
3321 to suppress pp_format's trailing close quote so that e.g.
3322 foo_typedef {aka underlying_foo} {enum}
3323 can be printed by "%qT" as:
3324 `foo_typedef' {aka `underlying_foo'} {enum}
3325 rather than:
3326 `foo_typedef {aka underlying_foo} {enum}'
3327 When adding such quotes, if POSTPROCESSED is true (for handling %H and %I)
3328 then a leading open quote will be added, whereas if POSTPROCESSED is false
3329 (for handling %T) then any leading quote has already been added by
3330 pp_format, or is not needed due to QUOTE being NULL (for template arguments
3331 within %H and %I).
3333 SHOW_COLOR is used to determine the colorization of any quotes that
3334 are added. */
3336 static const char *
3337 type_to_string (tree typ, int verbose, bool postprocessed, bool *quote,
3338 bool show_color)
3340 int flags = 0;
3341 if (verbose)
3342 flags |= TFF_CLASS_KEY_OR_ENUM;
3343 flags |= TFF_TEMPLATE_HEADER;
3345 reinit_cxx_pp ();
3347 if (postprocessed && quote && *quote)
3348 pp_begin_quote (cxx_pp, show_color);
3350 struct obstack *ob = pp_buffer (cxx_pp)->obstack;
3351 int type_start, type_len;
3352 type_start = obstack_object_size (ob);
3354 dump_type (cxx_pp, typ, flags);
3356 /* Remember the end of the initial dump. */
3357 type_len = obstack_object_size (ob) - type_start;
3359 /* If we're printing a type that involves typedefs, also print the
3360 stripped version. But sometimes the stripped version looks
3361 exactly the same, so we don't want it after all. To avoid printing
3362 it in that case, we play ugly obstack games. */
3363 if (typ && TYPE_P (typ) && typ != TYPE_CANONICAL (typ)
3364 && !uses_template_parms (typ))
3366 int aka_start, aka_len; char *p;
3367 tree aka = strip_typedefs (typ, NULL, STF_USER_VISIBLE);
3368 if (quote && *quote)
3369 pp_end_quote (cxx_pp, show_color);
3370 pp_string (cxx_pp, " {aka");
3371 pp_cxx_whitespace (cxx_pp);
3372 if (quote && *quote)
3373 pp_begin_quote (cxx_pp, show_color);
3374 /* And remember the start of the aka dump. */
3375 aka_start = obstack_object_size (ob);
3376 dump_type (cxx_pp, aka, flags);
3377 aka_len = obstack_object_size (ob) - aka_start;
3378 if (quote && *quote)
3379 pp_end_quote (cxx_pp, show_color);
3380 pp_right_brace (cxx_pp);
3381 p = (char*)obstack_base (ob);
3382 /* If they are identical, cut off the aka by unwinding the obstack. */
3383 if (type_len == aka_len
3384 && memcmp (p + type_start, p+aka_start, type_len) == 0)
3386 /* We can't add a '\0' here, since we may be adding a closing quote
3387 below, and it would be hidden by the '\0'.
3388 Instead, manually unwind the current object within the obstack
3389 so that the insertion point is at the end of the type, before
3390 the "' {aka". */
3391 int delta = type_start + type_len - obstack_object_size (ob);
3392 gcc_assert (delta <= 0);
3393 obstack_blank_fast (ob, delta);
3395 else
3396 if (quote)
3397 /* No further closing quotes are needed. */
3398 *quote = false;
3401 if (quote && *quote)
3403 pp_end_quote (cxx_pp, show_color);
3404 *quote = false;
3406 return pp_ggc_formatted_text (cxx_pp);
3409 static const char *
3410 args_to_string (tree p, int verbose)
3412 int flags = 0;
3413 if (verbose)
3414 flags |= TFF_CLASS_KEY_OR_ENUM;
3416 if (p == NULL_TREE)
3417 return "";
3419 if (TYPE_P (TREE_VALUE (p)))
3420 return type_as_string_translate (p, flags);
3422 reinit_cxx_pp ();
3423 for (; p; p = TREE_CHAIN (p))
3425 if (null_node_p (TREE_VALUE (p)))
3426 pp_cxx_ws_string (cxx_pp, "NULL");
3427 else
3428 dump_type (cxx_pp, error_type (TREE_VALUE (p)), flags);
3429 if (TREE_CHAIN (p))
3430 pp_separate_with_comma (cxx_pp);
3432 return pp_ggc_formatted_text (cxx_pp);
3435 /* Pretty-print a deduction substitution (from deduction_tsubst_fntype). P
3436 is a TREE_LIST with purpose the TEMPLATE_DECL, value the template
3437 arguments. */
3439 static const char *
3440 subst_to_string (tree p)
3442 tree decl = TREE_PURPOSE (p);
3443 tree targs = TREE_VALUE (p);
3444 tree tparms = DECL_TEMPLATE_PARMS (decl);
3445 int flags = (TFF_DECL_SPECIFIERS|TFF_TEMPLATE_HEADER
3446 |TFF_NO_TEMPLATE_BINDINGS);
3448 if (p == NULL_TREE)
3449 return "";
3451 reinit_cxx_pp ();
3452 dump_template_decl (cxx_pp, TREE_PURPOSE (p), flags);
3453 dump_substitution (cxx_pp, NULL, tparms, targs, /*flags=*/0);
3454 return pp_ggc_formatted_text (cxx_pp);
3457 static const char *
3458 cv_to_string (tree p, int v)
3460 reinit_cxx_pp ();
3461 cxx_pp->padding = v ? pp_before : pp_none;
3462 pp_cxx_cv_qualifier_seq (cxx_pp, p);
3463 return pp_ggc_formatted_text (cxx_pp);
3466 static const char *
3467 eh_spec_to_string (tree p, int /*v*/)
3469 int flags = 0;
3470 reinit_cxx_pp ();
3471 dump_exception_spec (cxx_pp, p, flags);
3472 return pp_ggc_formatted_text (cxx_pp);
3475 /* Langhook for print_error_function. */
3476 void
3477 cxx_print_error_function (diagnostic_context *context, const char *file,
3478 diagnostic_info *diagnostic)
3480 char *prefix;
3481 if (file)
3482 prefix = xstrdup (file);
3483 else
3484 prefix = NULL;
3485 lhd_print_error_function (context, file, diagnostic);
3486 pp_set_prefix (context->printer, prefix);
3487 maybe_print_instantiation_context (context);
3490 static void
3491 cp_diagnostic_starter (diagnostic_context *context,
3492 diagnostic_info *diagnostic)
3494 diagnostic_report_current_module (context, diagnostic_location (diagnostic));
3495 cp_print_error_function (context, diagnostic);
3496 maybe_print_instantiation_context (context);
3497 maybe_print_constexpr_context (context);
3498 maybe_print_constraint_context (context);
3499 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
3500 diagnostic));
3503 /* Print current function onto BUFFER, in the process of reporting
3504 a diagnostic message. Called from cp_diagnostic_starter. */
3505 static void
3506 cp_print_error_function (diagnostic_context *context,
3507 diagnostic_info *diagnostic)
3509 /* If we are in an instantiation context, current_function_decl is likely
3510 to be wrong, so just rely on print_instantiation_full_context. */
3511 if (current_instantiation ())
3512 return;
3513 /* The above is true for constraint satisfaction also. */
3514 if (current_failed_constraint)
3515 return;
3516 if (diagnostic_last_function_changed (context, diagnostic))
3518 char *old_prefix = pp_take_prefix (context->printer);
3519 const char *file = LOCATION_FILE (diagnostic_location (diagnostic));
3520 tree abstract_origin = diagnostic_abstract_origin (diagnostic);
3521 char *new_prefix = (file && abstract_origin == NULL)
3522 ? file_name_as_prefix (context, file) : NULL;
3524 pp_set_prefix (context->printer, new_prefix);
3526 if (current_function_decl == NULL)
3527 pp_string (context->printer, _("At global scope:"));
3528 else
3530 tree fndecl, ao;
3532 if (abstract_origin)
3534 ao = BLOCK_ABSTRACT_ORIGIN (abstract_origin);
3535 gcc_assert (TREE_CODE (ao) == FUNCTION_DECL);
3536 fndecl = ao;
3538 else
3539 fndecl = current_function_decl;
3541 pp_printf (context->printer, function_category (fndecl),
3542 cxx_printable_name_translate (fndecl, 2));
3544 while (abstract_origin)
3546 location_t *locus;
3547 tree block = abstract_origin;
3549 locus = &BLOCK_SOURCE_LOCATION (block);
3550 fndecl = NULL;
3551 block = BLOCK_SUPERCONTEXT (block);
3552 while (block && TREE_CODE (block) == BLOCK
3553 && BLOCK_ABSTRACT_ORIGIN (block))
3555 ao = BLOCK_ABSTRACT_ORIGIN (block);
3556 if (TREE_CODE (ao) == FUNCTION_DECL)
3558 fndecl = ao;
3559 break;
3561 else if (TREE_CODE (ao) != BLOCK)
3562 break;
3564 block = BLOCK_SUPERCONTEXT (block);
3566 if (fndecl)
3567 abstract_origin = block;
3568 else
3570 while (block && TREE_CODE (block) == BLOCK)
3571 block = BLOCK_SUPERCONTEXT (block);
3573 if (block && TREE_CODE (block) == FUNCTION_DECL)
3574 fndecl = block;
3575 abstract_origin = NULL;
3577 if (fndecl)
3579 expanded_location s = expand_location (*locus);
3580 pp_character (context->printer, ',');
3581 pp_newline (context->printer);
3582 if (s.file != NULL)
3584 if (context->show_column && s.column != 0)
3585 pp_printf (context->printer,
3586 _(" inlined from %qs at %r%s:%d:%d%R"),
3587 cxx_printable_name_translate (fndecl, 2),
3588 "locus", s.file, s.line, s.column);
3589 else
3590 pp_printf (context->printer,
3591 _(" inlined from %qs at %r%s:%d%R"),
3592 cxx_printable_name_translate (fndecl, 2),
3593 "locus", s.file, s.line);
3596 else
3597 pp_printf (context->printer, _(" inlined from %qs"),
3598 cxx_printable_name_translate (fndecl, 2));
3601 pp_character (context->printer, ':');
3603 pp_newline (context->printer);
3605 diagnostic_set_last_function (context, diagnostic);
3606 pp_destroy_prefix (context->printer);
3607 context->printer->prefix = old_prefix;
3611 /* Returns a description of FUNCTION using standard terminology. The
3612 result is a format string of the form "In CATEGORY %qs". */
3613 static const char *
3614 function_category (tree fn)
3616 /* We can get called from the middle-end for diagnostics of function
3617 clones. Make sure we have language specific information before
3618 dereferencing it. */
3619 if (DECL_LANG_SPECIFIC (STRIP_TEMPLATE (fn))
3620 && DECL_FUNCTION_MEMBER_P (fn))
3622 if (DECL_STATIC_FUNCTION_P (fn))
3623 return _("In static member function %qs");
3624 else if (DECL_COPY_CONSTRUCTOR_P (fn))
3625 return _("In copy constructor %qs");
3626 else if (DECL_CONSTRUCTOR_P (fn))
3627 return _("In constructor %qs");
3628 else if (DECL_DESTRUCTOR_P (fn))
3629 return _("In destructor %qs");
3630 else if (LAMBDA_FUNCTION_P (fn))
3631 return _("In lambda function");
3632 else
3633 return _("In member function %qs");
3635 else
3636 return _("In function %qs");
3639 /* Disable warnings about missing quoting in GCC diagnostics for
3640 the pp_verbatim calls. Their format strings deliberately don't
3641 follow GCC diagnostic conventions. */
3642 #if __GNUC__ >= 10
3643 #pragma GCC diagnostic push
3644 #pragma GCC diagnostic ignored "-Wformat-diag"
3645 #endif
3647 /* Report the full context of a current template instantiation,
3648 onto BUFFER. */
3649 static void
3650 print_instantiation_full_context (diagnostic_context *context)
3652 struct tinst_level *p = current_instantiation ();
3653 location_t location = input_location;
3655 if (p)
3657 pp_verbatim (context->printer,
3658 p->list_p ()
3659 ? _("%s: In substitution of %qS:\n")
3660 : _("%s: In instantiation of %q#D:\n"),
3661 LOCATION_FILE (location),
3662 p->get_node ());
3664 location = p->locus;
3665 p = p->next;
3668 print_instantiation_partial_context (context, p, location);
3671 /* Helper function of print_instantiation_partial_context() that
3672 prints a single line of instantiation context. */
3674 static void
3675 print_instantiation_partial_context_line (diagnostic_context *context,
3676 struct tinst_level *t,
3677 location_t loc, bool recursive_p)
3679 if (loc == UNKNOWN_LOCATION)
3680 return;
3682 expanded_location xloc = expand_location (loc);
3684 if (context->show_column)
3685 pp_verbatim (context->printer, _("%r%s:%d:%d:%R "),
3686 "locus", xloc.file, xloc.line, xloc.column);
3687 else
3688 pp_verbatim (context->printer, _("%r%s:%d:%R "),
3689 "locus", xloc.file, xloc.line);
3691 if (t != NULL)
3693 if (t->list_p ())
3694 pp_verbatim (context->printer,
3695 recursive_p
3696 ? _("recursively required by substitution of %qS\n")
3697 : _("required by substitution of %qS\n"),
3698 t->get_node ());
3699 else
3700 pp_verbatim (context->printer,
3701 recursive_p
3702 ? _("recursively required from %q#D\n")
3703 : _("required from %q#D\n"),
3704 t->get_node ());
3706 else
3708 pp_verbatim (context->printer,
3709 recursive_p
3710 ? _("recursively required from here\n")
3711 : _("required from here\n"));
3715 /* Same as print_instantiation_full_context but less verbose. */
3717 static void
3718 print_instantiation_partial_context (diagnostic_context *context,
3719 struct tinst_level *t0, location_t loc)
3721 struct tinst_level *t;
3722 int n_total = 0;
3723 int n;
3724 location_t prev_loc = loc;
3726 for (t = t0; t != NULL; t = t->next)
3727 if (prev_loc != t->locus)
3729 prev_loc = t->locus;
3730 n_total++;
3733 t = t0;
3735 if (template_backtrace_limit
3736 && n_total > template_backtrace_limit)
3738 int skip = n_total - template_backtrace_limit;
3739 int head = template_backtrace_limit / 2;
3741 /* Avoid skipping just 1. If so, skip 2. */
3742 if (skip == 1)
3744 skip = 2;
3745 head = (template_backtrace_limit - 1) / 2;
3748 for (n = 0; n < head; n++)
3750 gcc_assert (t != NULL);
3751 if (loc != t->locus)
3752 print_instantiation_partial_context_line (context, t, loc,
3753 /*recursive_p=*/false);
3754 loc = t->locus;
3755 t = t->next;
3757 if (t != NULL && skip > 0)
3759 expanded_location xloc;
3760 xloc = expand_location (loc);
3761 if (context->show_column)
3762 pp_verbatim (context->printer,
3763 _("%r%s:%d:%d:%R [ skipping %d instantiation "
3764 "contexts, use -ftemplate-backtrace-limit=0 to "
3765 "disable ]\n"),
3766 "locus", xloc.file, xloc.line, xloc.column, skip);
3767 else
3768 pp_verbatim (context->printer,
3769 _("%r%s:%d:%R [ skipping %d instantiation "
3770 "contexts, use -ftemplate-backtrace-limit=0 to "
3771 "disable ]\n"),
3772 "locus", xloc.file, xloc.line, skip);
3774 do {
3775 loc = t->locus;
3776 t = t->next;
3777 } while (t != NULL && --skip > 0);
3781 while (t != NULL)
3783 while (t->next != NULL && t->locus == t->next->locus)
3785 loc = t->locus;
3786 t = t->next;
3788 print_instantiation_partial_context_line (context, t, loc,
3789 t->locus == loc);
3790 loc = t->locus;
3791 t = t->next;
3793 print_instantiation_partial_context_line (context, NULL, loc,
3794 /*recursive_p=*/false);
3797 /* Called from cp_thing to print the template context for an error. */
3798 static void
3799 maybe_print_instantiation_context (diagnostic_context *context)
3801 if (!problematic_instantiation_changed () || current_instantiation () == 0)
3802 return;
3804 record_last_problematic_instantiation ();
3805 print_instantiation_full_context (context);
3808 /* Report what constexpr call(s) we're trying to expand, if any. */
3810 void
3811 maybe_print_constexpr_context (diagnostic_context *context)
3813 vec<tree> call_stack = cx_error_context ();
3814 unsigned ix;
3815 tree t;
3817 FOR_EACH_VEC_ELT (call_stack, ix, t)
3819 expanded_location xloc = expand_location (EXPR_LOCATION (t));
3820 const char *s = expr_as_string (t, 0);
3821 if (context->show_column)
3822 pp_verbatim (context->printer,
3823 _("%r%s:%d:%d:%R in %<constexpr%> expansion of %qs"),
3824 "locus", xloc.file, xloc.line, xloc.column, s);
3825 else
3826 pp_verbatim (context->printer,
3827 _("%r%s:%d:%R in %<constexpr%> expansion of %qs"),
3828 "locus", xloc.file, xloc.line, s);
3829 pp_newline (context->printer);
3834 static void
3835 print_location (diagnostic_context *context, location_t loc)
3837 expanded_location xloc = expand_location (loc);
3838 if (context->show_column)
3839 pp_verbatim (context->printer, _("%r%s:%d:%d:%R "),
3840 "locus", xloc.file, xloc.line, xloc.column);
3841 else
3842 pp_verbatim (context->printer, _("%r%s:%d:%R "),
3843 "locus", xloc.file, xloc.line);
3846 static void
3847 print_constrained_decl_info (diagnostic_context *context, tree decl)
3849 print_location (context, DECL_SOURCE_LOCATION (decl));
3850 pp_verbatim (context->printer, "required by the constraints of %q#D\n", decl);
3853 static void
3854 print_concept_check_info (diagnostic_context *context, tree expr, tree map, tree args)
3856 gcc_assert (concept_check_p (expr));
3858 tree id = unpack_concept_check (expr);
3859 tree tmpl = TREE_OPERAND (id, 0);
3860 if (OVL_P (tmpl))
3861 tmpl = OVL_FIRST (tmpl);
3863 print_location (context, DECL_SOURCE_LOCATION (tmpl));
3865 cxx_pretty_printer *pp = (cxx_pretty_printer *)context->printer;
3866 pp_verbatim (pp, "required for the satisfaction of %qE", expr);
3867 if (map && map != error_mark_node)
3869 tree subst_map = tsubst_parameter_mapping (map, args, tf_none, NULL_TREE);
3870 pp_cxx_parameter_mapping (pp, (subst_map != error_mark_node
3871 ? subst_map : map));
3873 pp_newline (pp);
3876 /* Diagnose the entry point into the satisfaction error. Returns the next
3877 context, if any. */
3879 static tree
3880 print_constraint_context_head (diagnostic_context *context, tree cxt, tree args)
3882 tree src = TREE_VALUE (cxt);
3883 if (!src)
3885 print_location (context, input_location);
3886 pp_verbatim (context->printer, "required for constraint satisfaction\n");
3887 return NULL_TREE;
3889 if (DECL_P (src))
3891 print_constrained_decl_info (context, src);
3892 return NULL_TREE;
3894 else
3896 print_concept_check_info (context, src, TREE_PURPOSE (cxt), args);
3897 return TREE_CHAIN (cxt);
3901 static void
3902 print_requires_expression_info (diagnostic_context *context, tree constr, tree args)
3905 tree expr = ATOMIC_CONSTR_EXPR (constr);
3906 tree map = ATOMIC_CONSTR_MAP (constr);
3907 map = tsubst_parameter_mapping (map, args, tf_none, NULL_TREE);
3908 if (map == error_mark_node)
3909 return;
3911 print_location (context, cp_expr_loc_or_input_loc (expr));
3912 pp_verbatim (context->printer, "in requirements ");
3914 tree parms = TREE_OPERAND (expr, 0);
3915 if (parms)
3916 pp_verbatim (context->printer, "with ");
3917 while (parms)
3919 pp_verbatim (context->printer, "%q#D", parms);
3920 if (TREE_CHAIN (parms))
3921 pp_separate_with_comma ((cxx_pretty_printer *)context->printer);
3922 parms = TREE_CHAIN (parms);
3924 pp_cxx_parameter_mapping ((cxx_pretty_printer *)context->printer, map);
3926 pp_verbatim (context->printer, "\n");
3929 void
3930 maybe_print_single_constraint_context (diagnostic_context *context, tree failed)
3932 if (!failed)
3933 return;
3935 tree constr = TREE_VALUE (failed);
3936 if (!constr || constr == error_mark_node)
3937 return;
3938 tree cxt = CONSTR_CONTEXT (constr);
3939 if (!cxt)
3940 return;
3941 tree args = TREE_PURPOSE (failed);
3943 /* Print the stack of requirements. */
3944 cxt = print_constraint_context_head (context, cxt, args);
3945 while (cxt && !DECL_P (TREE_VALUE (cxt)))
3947 tree expr = TREE_VALUE (cxt);
3948 tree map = TREE_PURPOSE (cxt);
3949 print_concept_check_info (context, expr, map, args);
3950 cxt = TREE_CHAIN (cxt);
3953 /* For certain constraints, we can provide additional context. */
3954 if (TREE_CODE (constr) == ATOMIC_CONSTR
3955 && TREE_CODE (ATOMIC_CONSTR_EXPR (constr)) == REQUIRES_EXPR)
3956 print_requires_expression_info (context, constr, args);
3959 void
3960 maybe_print_constraint_context (diagnostic_context *context)
3962 if (!current_failed_constraint)
3963 return;
3965 tree cur = current_failed_constraint;
3967 /* Recursively print nested contexts. */
3968 current_failed_constraint = TREE_CHAIN (current_failed_constraint);
3969 if (current_failed_constraint)
3970 maybe_print_constraint_context (context);
3972 /* Print this context. */
3973 maybe_print_single_constraint_context (context, cur);
3976 /* Return true iff TYPE_A and TYPE_B are template types that are
3977 meaningful to compare. */
3979 static bool
3980 comparable_template_types_p (tree type_a, tree type_b)
3982 if (!CLASS_TYPE_P (type_a))
3983 return false;
3984 if (!CLASS_TYPE_P (type_b))
3985 return false;
3987 tree tinfo_a = TYPE_TEMPLATE_INFO (type_a);
3988 tree tinfo_b = TYPE_TEMPLATE_INFO (type_b);
3989 if (!tinfo_a || !tinfo_b)
3990 return false;
3992 return TI_TEMPLATE (tinfo_a) == TI_TEMPLATE (tinfo_b);
3995 /* Start a new line indented by SPC spaces on PP. */
3997 static void
3998 newline_and_indent (pretty_printer *pp, int spc)
4000 pp_newline (pp);
4001 for (int i = 0; i < spc; i++)
4002 pp_space (pp);
4005 /* Generate a GC-allocated string for ARG, an expression or type. */
4007 static const char *
4008 arg_to_string (tree arg, bool verbose)
4010 if (TYPE_P (arg))
4011 return type_to_string (arg, verbose, true, NULL, false);
4012 else
4013 return expr_to_string (arg);
4016 /* Subroutine to type_to_string_with_compare and
4017 print_template_tree_comparison.
4019 Print a representation of ARG (an expression or type) to PP,
4020 colorizing it as "type-diff" if PP->show_color. */
4022 static void
4023 print_nonequal_arg (pretty_printer *pp, tree arg, bool verbose)
4025 pp_printf (pp, "%r%s%R",
4026 "type-diff",
4027 (arg
4028 ? arg_to_string (arg, verbose)
4029 : G_("(no argument)")));
4032 /* Recursively print template TYPE_A to PP, as compared to template TYPE_B.
4034 The types must satisfy comparable_template_types_p.
4036 If INDENT is 0, then this is equivalent to type_to_string (TYPE_A), but
4037 potentially colorizing/eliding in comparison with TYPE_B.
4039 For example given types:
4040 vector<map<int,double>>
4042 vector<map<int,float>>
4043 then the result on PP would be:
4044 vector<map<[...],double>>
4045 with type elision, and:
4046 vector<map<int,double>>
4047 without type elision.
4049 In both cases the parts of TYPE that differ from PEER will be colorized
4050 if pp_show_color (pp) is true. In the above example, this would be
4051 "double".
4053 If INDENT is non-zero, then the types are printed in a tree-like form
4054 which shows both types. In the above example, the result on PP would be:
4056 vector<
4057 map<
4058 [...],
4059 [double != float]>>
4061 and without type-elision would be:
4063 vector<
4064 map<
4065 int,
4066 [double != float]>>
4068 As before, the differing parts of the types are colorized if
4069 pp_show_color (pp) is true ("double" and "float" in this example).
4071 Template arguments in which both types are using the default arguments
4072 are not printed; if at least one of the two types is using a non-default
4073 argument, then that argument is printed (or both arguments for the
4074 tree-like print format). */
4076 static void
4077 print_template_differences (pretty_printer *pp, tree type_a, tree type_b,
4078 bool verbose, int indent)
4080 if (indent)
4081 newline_and_indent (pp, indent);
4083 tree tinfo_a = TYPE_TEMPLATE_INFO (type_a);
4084 tree tinfo_b = TYPE_TEMPLATE_INFO (type_b);
4086 pp_printf (pp, "%s<",
4087 IDENTIFIER_POINTER (DECL_NAME (TI_TEMPLATE (tinfo_a))));
4089 tree args_a = TI_ARGS (tinfo_a);
4090 tree args_b = TI_ARGS (tinfo_b);
4091 gcc_assert (TREE_CODE (args_a) == TREE_VEC);
4092 gcc_assert (TREE_CODE (args_b) == TREE_VEC);
4093 int flags = 0;
4094 int len_a = get_non_default_template_args_count (args_a, flags);
4095 args_a = INNERMOST_TEMPLATE_ARGS (args_a);
4096 int len_b = get_non_default_template_args_count (args_b, flags);
4097 args_b = INNERMOST_TEMPLATE_ARGS (args_b);
4098 /* Determine the maximum range of args for which non-default template args
4099 were used; beyond this, only default args (if any) were used, and so
4100 they will be equal from this point onwards.
4101 One of the two peers might have used default arguments within this
4102 range, but the other will be using non-default arguments, and so
4103 it's more readable to print both within this range, to highlight
4104 the differences. */
4105 int len_max = MAX (len_a, len_b);
4106 gcc_assert (TREE_CODE (args_a) == TREE_VEC);
4107 gcc_assert (TREE_CODE (args_b) == TREE_VEC);
4108 for (int idx = 0; idx < len_max; idx++)
4110 if (idx)
4111 pp_character (pp, ',');
4113 tree arg_a = TREE_VEC_ELT (args_a, idx);
4114 tree arg_b = TREE_VEC_ELT (args_b, idx);
4115 if (arg_a == arg_b)
4117 if (indent)
4118 newline_and_indent (pp, indent + 2);
4119 /* Can do elision here, printing "[...]". */
4120 if (flag_elide_type)
4121 pp_string (pp, G_("[...]"));
4122 else
4123 pp_string (pp, arg_to_string (arg_a, verbose));
4125 else
4127 int new_indent = indent ? indent + 2 : 0;
4128 if (comparable_template_types_p (arg_a, arg_b))
4129 print_template_differences (pp, arg_a, arg_b, verbose, new_indent);
4130 else
4131 if (indent)
4133 newline_and_indent (pp, indent + 2);
4134 pp_character (pp, '[');
4135 print_nonequal_arg (pp, arg_a, verbose);
4136 pp_string (pp, " != ");
4137 print_nonequal_arg (pp, arg_b, verbose);
4138 pp_character (pp, ']');
4140 else
4141 print_nonequal_arg (pp, arg_a, verbose);
4144 pp_printf (pp, ">");
4147 /* As type_to_string, but for a template, potentially colorizing/eliding
4148 in comparison with PEER.
4149 For example, if TYPE is map<int,double> and PEER is map<int,int>,
4150 then the resulting string would be:
4151 map<[...],double>
4152 with type elision, and:
4153 map<int,double>
4154 without type elision.
4156 In both cases the parts of TYPE that differ from PEER will be colorized
4157 if SHOW_COLOR is true. In the above example, this would be "double".
4159 Template arguments in which both types are using the default arguments
4160 are not printed; if at least one of the two types is using a non-default
4161 argument, then both arguments are printed.
4163 The resulting string is in a GC-allocated buffer. */
4165 static const char *
4166 type_to_string_with_compare (tree type, tree peer, bool verbose,
4167 bool show_color)
4169 pretty_printer inner_pp;
4170 pretty_printer *pp = &inner_pp;
4171 pp_show_color (pp) = show_color;
4173 print_template_differences (pp, type, peer, verbose, 0);
4174 return pp_ggc_formatted_text (pp);
4177 /* Recursively print a tree-like comparison of TYPE_A and TYPE_B to PP,
4178 indented by INDENT spaces.
4180 For example given types:
4182 vector<map<int,double>>
4186 vector<map<double,float>>
4188 the output with type elision would be:
4190 vector<
4191 map<
4192 [...],
4193 [double != float]>>
4195 and without type-elision would be:
4197 vector<
4198 map<
4199 int,
4200 [double != float]>>
4202 TYPE_A and TYPE_B must both be comparable template types
4203 (as per comparable_template_types_p).
4205 Template arguments in which both types are using the default arguments
4206 are not printed; if at least one of the two types is using a non-default
4207 argument, then both arguments are printed. */
4209 static void
4210 print_template_tree_comparison (pretty_printer *pp, tree type_a, tree type_b,
4211 bool verbose, int indent)
4213 print_template_differences (pp, type_a, type_b, verbose, indent);
4216 /* Subroutine for use in a format_postprocessor::handle
4217 implementation. Adds a chunk to the end of
4218 formatted output, so that it will be printed
4219 by pp_output_formatted_text. */
4221 static void
4222 append_formatted_chunk (pretty_printer *pp, const char *content)
4224 output_buffer *buffer = pp_buffer (pp);
4225 struct chunk_info *chunk_array = buffer->cur_chunk_array;
4226 const char **args = chunk_array->args;
4228 unsigned int chunk_idx;
4229 for (chunk_idx = 0; args[chunk_idx]; chunk_idx++)
4231 args[chunk_idx++] = content;
4232 args[chunk_idx] = NULL;
4235 /* Create a copy of CONTENT, with quotes added, and,
4236 potentially, with colorization.
4237 No escaped is performed on CONTENT.
4238 The result is in a GC-allocated buffer. */
4240 static const char *
4241 add_quotes (const char *content, bool show_color)
4243 pretty_printer tmp_pp;
4244 pp_show_color (&tmp_pp) = show_color;
4246 /* We have to use "%<%s%>" rather than "%qs" here in order to avoid
4247 quoting colorization bytes within the results and using either
4248 pp_quote or pp_begin_quote doesn't work the same. */
4249 pp_printf (&tmp_pp, "%<%s%>", content);
4251 return pp_ggc_formatted_text (&tmp_pp);
4254 #if __GNUC__ >= 10
4255 #pragma GCC diagnostic pop
4256 #endif
4258 /* If we had %H and %I, and hence deferred printing them,
4259 print them now, storing the result into the chunk_info
4260 for pp_format. Quote them if 'q' was provided.
4261 Also print the difference in tree form, adding it as
4262 an additional chunk. */
4264 void
4265 cxx_format_postprocessor::handle (pretty_printer *pp)
4267 /* If we have one of %H and %I, the other should have
4268 been present. */
4269 if (m_type_a.m_tree || m_type_b.m_tree)
4271 /* Avoid reentrancy issues by working with a copy of
4272 m_type_a and m_type_b, resetting them now. */
4273 deferred_printed_type type_a = m_type_a;
4274 deferred_printed_type type_b = m_type_b;
4275 m_type_a = deferred_printed_type ();
4276 m_type_b = deferred_printed_type ();
4278 gcc_assert (type_a.m_buffer_ptr);
4279 gcc_assert (type_b.m_buffer_ptr);
4281 bool show_color = pp_show_color (pp);
4283 const char *type_a_text;
4284 const char *type_b_text;
4286 if (comparable_template_types_p (type_a.m_tree, type_b.m_tree))
4288 type_a_text
4289 = type_to_string_with_compare (type_a.m_tree, type_b.m_tree,
4290 type_a.m_verbose, show_color);
4291 type_b_text
4292 = type_to_string_with_compare (type_b.m_tree, type_a.m_tree,
4293 type_b.m_verbose, show_color);
4295 if (flag_diagnostics_show_template_tree)
4297 pretty_printer inner_pp;
4298 pp_show_color (&inner_pp) = pp_show_color (pp);
4299 print_template_tree_comparison
4300 (&inner_pp, type_a.m_tree, type_b.m_tree, type_a.m_verbose, 2);
4301 append_formatted_chunk (pp, pp_ggc_formatted_text (&inner_pp));
4304 else
4306 /* If the types were not comparable (or if only one of %H/%I was
4307 provided), they are printed normally, and no difference tree
4308 is printed. */
4309 type_a_text = type_to_string (type_a.m_tree, type_a.m_verbose,
4310 true, &type_a.m_quote, show_color);
4311 type_b_text = type_to_string (type_b.m_tree, type_b.m_verbose,
4312 true, &type_b.m_quote, show_color);
4315 if (type_a.m_quote)
4316 type_a_text = add_quotes (type_a_text, show_color);
4317 *type_a.m_buffer_ptr = type_a_text;
4319 if (type_b.m_quote)
4320 type_b_text = add_quotes (type_b_text, show_color);
4321 *type_b.m_buffer_ptr = type_b_text;
4325 /* Subroutine for handling %H and %I, to support i18n of messages like:
4327 error_at (loc, "could not convert %qE from %qH to %qI",
4328 expr, type_a, type_b);
4330 so that we can print things like:
4332 could not convert 'foo' from 'map<int,double>' to 'map<int,int>'
4334 and, with type-elision:
4336 could not convert 'foo' from 'map<[...],double>' to 'map<[...],int>'
4338 (with color-coding of the differences between the types).
4340 The %H and %I format codes are peers: both must be present,
4341 and they affect each other. Hence to handle them, we must
4342 delay printing until we have both, deferring the printing to
4343 pretty_printer's m_format_postprocessor hook.
4345 This is called in phase 2 of pp_format, when it is accumulating
4346 a series of formatted chunks. We stash the location of the chunk
4347 we're meant to have written to, so that we can write to it in the
4348 m_format_postprocessor hook.
4350 We also need to stash whether a 'q' prefix was provided (the QUOTE
4351 param) so that we can add the quotes when writing out the delayed
4352 chunk. */
4354 static void
4355 defer_phase_2_of_type_diff (deferred_printed_type *deferred,
4356 tree type, const char **buffer_ptr,
4357 bool verbose, bool quote)
4359 gcc_assert (deferred->m_tree == NULL_TREE);
4360 gcc_assert (deferred->m_buffer_ptr == NULL);
4361 *deferred = deferred_printed_type (type, buffer_ptr, verbose, quote);
4365 /* Called from output_format -- during diagnostic message processing --
4366 to handle C++ specific format specifier with the following meanings:
4367 %A function argument-list.
4368 %C tree code.
4369 %D declaration.
4370 %E expression.
4371 %F function declaration.
4372 %H type difference (from).
4373 %I type difference (to).
4374 %L language as used in extern "lang".
4375 %O binary operator.
4376 %P function parameter whose position is indicated by an integer.
4377 %Q assignment operator.
4378 %S substitution (template + args)
4379 %T type.
4380 %V cv-qualifier.
4381 %X exception-specification. */
4382 static bool
4383 cp_printer (pretty_printer *pp, text_info *text, const char *spec,
4384 int precision, bool wide, bool set_locus, bool verbose,
4385 bool *quoted, const char **buffer_ptr)
4387 gcc_assert (pp->m_format_postprocessor);
4388 cxx_format_postprocessor *postprocessor
4389 = static_cast <cxx_format_postprocessor *> (pp->m_format_postprocessor);
4391 const char *result;
4392 tree t = NULL;
4393 #define next_tree (t = va_arg (*text->args_ptr, tree))
4394 #define next_tcode ((enum tree_code) va_arg (*text->args_ptr, int))
4395 #define next_lang ((enum languages) va_arg (*text->args_ptr, int))
4396 #define next_int va_arg (*text->args_ptr, int)
4398 if (precision != 0 || wide)
4399 return false;
4401 switch (*spec)
4403 case 'A': result = args_to_string (next_tree, verbose); break;
4404 case 'C': result = code_to_string (next_tcode); break;
4405 case 'D':
4407 tree temp = next_tree;
4408 if (VAR_P (temp)
4409 && DECL_HAS_DEBUG_EXPR_P (temp))
4411 temp = DECL_DEBUG_EXPR (temp);
4412 if (!DECL_P (temp))
4414 result = expr_to_string (temp);
4415 break;
4418 result = decl_to_string (temp, verbose);
4420 break;
4421 case 'E': result = expr_to_string (next_tree); break;
4422 case 'F': result = fndecl_to_string (next_tree, verbose); break;
4423 case 'H':
4424 defer_phase_2_of_type_diff (&postprocessor->m_type_a, next_tree,
4425 buffer_ptr, verbose, *quoted);
4426 return true;
4427 case 'I':
4428 defer_phase_2_of_type_diff (&postprocessor->m_type_b, next_tree,
4429 buffer_ptr, verbose, *quoted);
4430 return true;
4431 case 'L': result = language_to_string (next_lang); break;
4432 case 'O': result = op_to_string (false, next_tcode); break;
4433 case 'P': result = parm_to_string (next_int); break;
4434 case 'Q': result = op_to_string (true, next_tcode); break;
4435 case 'S': result = subst_to_string (next_tree); break;
4436 case 'T':
4438 result = type_to_string (next_tree, verbose, false, quoted,
4439 pp_show_color (pp));
4441 break;
4442 case 'V': result = cv_to_string (next_tree, verbose); break;
4443 case 'X': result = eh_spec_to_string (next_tree, verbose); break;
4445 default:
4446 return false;
4449 pp_string (pp, result);
4450 if (set_locus && t != NULL)
4451 text->set_location (0, location_of (t), SHOW_RANGE_WITH_CARET);
4452 return true;
4453 #undef next_tree
4454 #undef next_tcode
4455 #undef next_lang
4456 #undef next_int
4459 /* Warn about the use of C++0x features when appropriate. */
4460 void
4461 maybe_warn_cpp0x (cpp0x_warn_str str, location_t loc/*=input_location*/)
4463 if (cxx_dialect == cxx98)
4464 switch (str)
4466 case CPP0X_INITIALIZER_LISTS:
4467 pedwarn (loc, OPT_Wc__11_extensions,
4468 "extended initializer lists "
4469 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4470 break;
4471 case CPP0X_EXPLICIT_CONVERSION:
4472 pedwarn (loc, OPT_Wc__11_extensions,
4473 "explicit conversion operators "
4474 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4475 break;
4476 case CPP0X_VARIADIC_TEMPLATES:
4477 pedwarn (loc, OPT_Wc__11_extensions,
4478 "variadic templates "
4479 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4480 break;
4481 case CPP0X_LAMBDA_EXPR:
4482 pedwarn (loc, OPT_Wc__11_extensions,
4483 "lambda expressions "
4484 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4485 break;
4486 case CPP0X_AUTO:
4487 pedwarn (loc, OPT_Wc__11_extensions,
4488 "C++11 auto only available with %<-std=c++11%> or "
4489 "%<-std=gnu++11%>");
4490 break;
4491 case CPP0X_SCOPED_ENUMS:
4492 pedwarn (loc, OPT_Wc__11_extensions,
4493 "scoped enums only available with %<-std=c++11%> or "
4494 "%<-std=gnu++11%>");
4495 break;
4496 case CPP0X_DEFAULTED_DELETED:
4497 pedwarn (loc, OPT_Wc__11_extensions,
4498 "defaulted and deleted functions "
4499 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4500 break;
4501 case CPP0X_INLINE_NAMESPACES:
4502 if (pedantic)
4503 pedwarn (loc, OPT_Wc__11_extensions,
4504 "inline namespaces "
4505 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4506 break;
4507 case CPP0X_OVERRIDE_CONTROLS:
4508 pedwarn (loc, OPT_Wc__11_extensions,
4509 "override controls (override/final) "
4510 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4511 break;
4512 case CPP0X_NSDMI:
4513 pedwarn (loc, OPT_Wc__11_extensions,
4514 "non-static data member initializers "
4515 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4516 break;
4517 case CPP0X_USER_DEFINED_LITERALS:
4518 pedwarn (loc, OPT_Wc__11_extensions,
4519 "user-defined literals "
4520 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4521 break;
4522 case CPP0X_DELEGATING_CTORS:
4523 pedwarn (loc, OPT_Wc__11_extensions,
4524 "delegating constructors "
4525 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4526 break;
4527 case CPP0X_INHERITING_CTORS:
4528 pedwarn (loc, OPT_Wc__11_extensions,
4529 "inheriting constructors "
4530 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4531 break;
4532 case CPP0X_ATTRIBUTES:
4533 pedwarn (loc, OPT_Wc__11_extensions,
4534 "C++11 attributes "
4535 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4536 break;
4537 case CPP0X_REF_QUALIFIER:
4538 pedwarn (loc, OPT_Wc__11_extensions,
4539 "ref-qualifiers "
4540 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4541 break;
4542 default:
4543 gcc_unreachable ();
4547 /* Warn about the use of variadic templates when appropriate. */
4548 void
4549 maybe_warn_variadic_templates (void)
4551 maybe_warn_cpp0x (CPP0X_VARIADIC_TEMPLATES);
4555 /* Issue an ISO C++98 pedantic warning at LOCATION, conditional on
4556 option OPT with text GMSGID. Use this function to report
4557 diagnostics for constructs that are invalid C++98, but valid
4558 C++0x. */
4559 bool
4560 pedwarn_cxx98 (location_t location, int opt, const char *gmsgid, ...)
4562 diagnostic_info diagnostic;
4563 va_list ap;
4564 bool ret;
4565 rich_location richloc (line_table, location);
4567 va_start (ap, gmsgid);
4568 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
4569 (cxx_dialect == cxx98) ? DK_PEDWARN : DK_WARNING);
4570 diagnostic.option_index = opt;
4571 ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
4572 va_end (ap);
4573 return ret;
4576 /* Issue a diagnostic that NAME cannot be found in SCOPE. DECL is what
4577 we found when we tried to do the lookup. LOCATION is the location of
4578 the NAME identifier. */
4580 void
4581 qualified_name_lookup_error (tree scope, tree name,
4582 tree decl, location_t location)
4584 if (scope == error_mark_node)
4585 ; /* We already complained. */
4586 else if (TYPE_P (scope))
4588 if (!COMPLETE_TYPE_P (scope))
4589 error_at (location, "incomplete type %qT used in nested name specifier",
4590 scope);
4591 else if (TREE_CODE (decl) == TREE_LIST)
4593 error_at (location, "reference to %<%T::%D%> is ambiguous",
4594 scope, name);
4595 print_candidates (decl);
4597 else
4599 name_hint hint;
4600 if (SCOPED_ENUM_P (scope) && TREE_CODE (name) == IDENTIFIER_NODE)
4601 hint = suggest_alternative_in_scoped_enum (name, scope);
4602 if (const char *suggestion = hint.suggestion ())
4604 gcc_rich_location richloc (location);
4605 richloc.add_fixit_replace (suggestion);
4606 error_at (&richloc,
4607 "%qD is not a member of %qT; did you mean %qs?",
4608 name, scope, suggestion);
4610 else
4611 error_at (location, "%qD is not a member of %qT", name, scope);
4614 else if (scope != global_namespace)
4616 auto_diagnostic_group d;
4617 bool emit_fixit = true;
4618 name_hint hint
4619 = suggest_alternative_in_explicit_scope (location, name, scope);
4620 if (!hint)
4622 hint = suggest_alternatives_in_other_namespaces (location, name);
4623 /* "location" is just the location of the name, not of the explicit
4624 scope, and it's not easy to get at the latter, so we can't issue
4625 fix-it hints for the suggestion. */
4626 emit_fixit = false;
4628 if (const char *suggestion = hint.suggestion ())
4630 gcc_rich_location richloc (location);
4631 if (emit_fixit)
4632 richloc.add_fixit_replace (suggestion);
4633 error_at (&richloc, "%qD is not a member of %qD; did you mean %qs?",
4634 name, scope, suggestion);
4636 else
4637 error_at (location, "%qD is not a member of %qD", name, scope);
4639 else
4641 auto_diagnostic_group d;
4642 name_hint hint = suggest_alternatives_for (location, name, true);
4643 if (const char *suggestion = hint.suggestion ())
4645 gcc_rich_location richloc (location);
4646 richloc.add_fixit_replace (suggestion);
4647 error_at (&richloc,
4648 "%<::%D%> has not been declared; did you mean %qs?",
4649 name, suggestion);
4651 else
4652 error_at (location, "%<::%D%> has not been declared", name);
4656 /* C++-specific implementation of range_label::get_text () vfunc for
4657 range_label_for_type_mismatch.
4659 Compare with print_template_differences above. */
4661 label_text
4662 range_label_for_type_mismatch::get_text (unsigned /*range_idx*/) const
4664 if (m_labelled_type == NULL_TREE)
4665 return label_text::borrow (NULL);
4667 const bool verbose = false;
4668 const bool show_color = false;
4670 const char *result;
4671 if (m_other_type
4672 && comparable_template_types_p (m_labelled_type, m_other_type))
4673 result = type_to_string_with_compare (m_labelled_type, m_other_type,
4674 verbose, show_color);
4675 else
4676 result = type_to_string (m_labelled_type, verbose, true, NULL, show_color);
4678 /* Both of the above return GC-allocated buffers, so the caller mustn't
4679 free them. */
4680 return label_text::borrow (result);