Fix typo in t-dimode
[official-gcc.git] / gcc / cp / error.c
blob98c1f0e4bdfdf30c1d195eb451cfa5dd98d80a72
1 /* Call-backs for C++ error reporting.
2 This code is non-reentrant.
3 Copyright (C) 1993-2021 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_UNIQUE_PTR
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 /* Dump a scope, if deemed necessary. */
216 static void
217 dump_scope (cxx_pretty_printer *pp, tree scope, int flags)
219 int f = flags & (TFF_SCOPE | TFF_CHASE_TYPEDEF);
221 if (scope == NULL_TREE)
222 return;
224 /* Enum values within an unscoped enum will be CONST_DECL with an
225 ENUMERAL_TYPE as their "scope". Use CP_TYPE_CONTEXT of the
226 ENUMERAL_TYPE, so as to print any enclosing namespace. */
227 if (UNSCOPED_ENUM_P (scope))
228 scope = CP_TYPE_CONTEXT (scope);
230 if (TREE_CODE (scope) == NAMESPACE_DECL)
232 if (scope != global_namespace)
234 dump_decl (pp, scope, f);
235 pp_cxx_colon_colon (pp);
238 else if (AGGREGATE_TYPE_P (scope)
239 || SCOPED_ENUM_P (scope))
241 dump_type (pp, scope, f);
242 pp_cxx_colon_colon (pp);
244 else if ((flags & TFF_SCOPE) && TREE_CODE (scope) == FUNCTION_DECL)
246 dump_function_decl (pp, scope, f | TFF_NO_TEMPLATE_BINDINGS);
247 pp_cxx_colon_colon (pp);
251 /* Dump the template ARGument under control of FLAGS. */
253 static void
254 dump_template_argument (cxx_pretty_printer *pp, tree arg, int flags)
256 if (ARGUMENT_PACK_P (arg))
257 dump_template_argument_list (pp, ARGUMENT_PACK_ARGS (arg),
258 /* No default args in argument packs. */
259 flags|TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS);
260 else if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
261 dump_type (pp, arg, flags & ~TFF_CLASS_KEY_OR_ENUM);
262 else
264 if (TREE_CODE (arg) == TREE_LIST)
265 arg = TREE_VALUE (arg);
267 /* Strip implicit conversions. */
268 while (CONVERT_EXPR_P (arg))
269 arg = TREE_OPERAND (arg, 0);
271 dump_expr (pp, arg, (flags | TFF_EXPR_IN_PARENS) & ~TFF_CLASS_KEY_OR_ENUM);
275 /* Count the number of template arguments ARGS whose value does not
276 match the (optional) default template parameter in PARAMS */
278 static int
279 get_non_default_template_args_count (tree args, int flags)
281 int n = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_ARGS (args));
283 if (/* We use this flag when generating debug information. We don't
284 want to expand templates at this point, for this may generate
285 new decls, which gets decl counts out of sync, which may in
286 turn cause codegen differences between compilations with and
287 without -g. */
288 (flags & TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS) != 0
289 || !flag_pretty_templates)
290 return n;
292 return GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (INNERMOST_TEMPLATE_ARGS (args));
295 /* Dump a template-argument-list ARGS (always a TREE_VEC) under control
296 of FLAGS. */
298 static void
299 dump_template_argument_list (cxx_pretty_printer *pp, tree args, int flags)
301 int n = get_non_default_template_args_count (args, flags);
302 int need_comma = 0;
303 int i;
305 for (i = 0; i < n; ++i)
307 tree arg = TREE_VEC_ELT (args, i);
309 /* Only print a comma if we know there is an argument coming. In
310 the case of an empty template argument pack, no actual
311 argument will be printed. */
312 if (need_comma
313 && (!ARGUMENT_PACK_P (arg)
314 || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) > 0))
315 pp_separate_with_comma (pp);
317 dump_template_argument (pp, arg, flags);
318 need_comma = 1;
322 /* Dump a template parameter PARM (a TREE_LIST) under control of FLAGS. */
324 static void
325 dump_template_parameter (cxx_pretty_printer *pp, tree parm, int flags)
327 tree p;
328 tree a;
330 if (parm == error_mark_node)
331 return;
333 p = TREE_VALUE (parm);
334 a = TREE_PURPOSE (parm);
336 if (TREE_CODE (p) == TYPE_DECL)
338 if (flags & TFF_DECL_SPECIFIERS)
340 pp_cxx_ws_string (pp, "class");
341 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (p)))
342 pp_cxx_ws_string (pp, "...");
343 if (DECL_NAME (p))
344 pp_cxx_tree_identifier (pp, DECL_NAME (p));
346 else if (DECL_NAME (p))
347 pp_cxx_tree_identifier (pp, DECL_NAME (p));
348 else
349 pp_cxx_canonical_template_parameter (pp, TREE_TYPE (p));
351 else
352 dump_decl (pp, p, flags | TFF_DECL_SPECIFIERS);
354 if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && a != NULL_TREE)
356 pp_cxx_whitespace (pp);
357 pp_equal (pp);
358 pp_cxx_whitespace (pp);
359 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
360 dump_type (pp, a, flags & ~TFF_CHASE_TYPEDEF);
361 else
362 dump_expr (pp, a, flags | TFF_EXPR_IN_PARENS);
366 /* Dump, under control of FLAGS, a template-parameter-list binding.
367 PARMS is a TREE_LIST of TREE_VEC of TREE_LIST and ARGS is a
368 TREE_VEC. */
370 static void
371 dump_template_bindings (cxx_pretty_printer *pp, tree parms, tree args,
372 vec<tree, va_gc> *typenames)
374 /* Print "[with" and ']', conditional on whether anything is printed at all.
375 This is tied to whether a semicolon is needed to separate multiple template
376 parameters. */
377 struct prepost_semicolon
379 cxx_pretty_printer *pp;
380 bool need_semicolon;
382 void operator() ()
384 if (need_semicolon)
385 pp_separate_with_semicolon (pp);
386 else
388 pp_cxx_whitespace (pp);
389 pp_cxx_left_bracket (pp);
390 pp->translate_string ("with");
391 pp_cxx_whitespace (pp);
392 need_semicolon = true;
396 ~prepost_semicolon ()
398 if (need_semicolon)
399 pp_cxx_right_bracket (pp);
401 } semicolon_or_introducer = {pp, false};
403 int i;
404 tree t;
406 while (parms)
408 tree p = TREE_VALUE (parms);
409 int lvl = TMPL_PARMS_DEPTH (parms);
410 int arg_idx = 0;
411 int i;
412 tree lvl_args = NULL_TREE;
414 /* Don't crash if we had an invalid argument list. */
415 if (TMPL_ARGS_DEPTH (args) >= lvl)
416 lvl_args = TMPL_ARGS_LEVEL (args, lvl);
418 for (i = 0; i < TREE_VEC_LENGTH (p); ++i)
420 tree arg = NULL_TREE;
422 /* Don't crash if we had an invalid argument list. */
423 if (lvl_args && NUM_TMPL_ARGS (lvl_args) > arg_idx)
424 arg = TREE_VEC_ELT (lvl_args, arg_idx);
426 tree parm_i = TREE_VEC_ELT (p, i);
427 /* If the template argument repeats the template parameter (T = T),
428 skip the parameter.*/
429 if (arg && TREE_CODE (arg) == TEMPLATE_TYPE_PARM
430 && TREE_CODE (parm_i) == TREE_LIST
431 && TREE_CODE (TREE_VALUE (parm_i)) == TYPE_DECL
432 && TREE_CODE (TREE_TYPE (TREE_VALUE (parm_i)))
433 == TEMPLATE_TYPE_PARM
434 && DECL_NAME (TREE_VALUE (parm_i))
435 == DECL_NAME (TREE_CHAIN (arg)))
436 continue;
438 semicolon_or_introducer ();
439 dump_template_parameter (pp, parm_i, TFF_PLAIN_IDENTIFIER);
440 pp_cxx_whitespace (pp);
441 pp_equal (pp);
442 pp_cxx_whitespace (pp);
443 if (arg)
445 if (ARGUMENT_PACK_P (arg))
446 pp_cxx_left_brace (pp);
447 dump_template_argument (pp, arg, TFF_PLAIN_IDENTIFIER);
448 if (ARGUMENT_PACK_P (arg))
449 pp_cxx_right_brace (pp);
451 else
452 pp_string (pp, M_("<missing>"));
454 ++arg_idx;
457 parms = TREE_CHAIN (parms);
460 /* Don't bother with typenames for a partial instantiation. */
461 if (vec_safe_is_empty (typenames) || uses_template_parms (args))
462 return;
464 /* Don't try to print typenames when we're processing a clone. */
465 if (current_function_decl
466 && !DECL_LANG_SPECIFIC (current_function_decl))
467 return;
469 /* Don't try to do this once cgraph starts throwing away front-end
470 information. */
471 if (at_eof >= 2)
472 return;
474 FOR_EACH_VEC_SAFE_ELT (typenames, i, t)
476 semicolon_or_introducer ();
477 dump_type (pp, t, TFF_PLAIN_IDENTIFIER);
478 pp_cxx_whitespace (pp);
479 pp_equal (pp);
480 pp_cxx_whitespace (pp);
481 push_deferring_access_checks (dk_no_check);
482 t = tsubst (t, args, tf_none, NULL_TREE);
483 pop_deferring_access_checks ();
484 /* Strip typedefs. We can't just use TFF_CHASE_TYPEDEF because
485 pp_simple_type_specifier doesn't know about it. */
486 t = strip_typedefs (t, NULL, STF_USER_VISIBLE);
487 dump_type (pp, t, TFF_PLAIN_IDENTIFIER);
491 /* Dump a human-readable equivalent of the alias template
492 specialization of T. */
494 static void
495 dump_alias_template_specialization (cxx_pretty_printer *pp, tree t, int flags)
497 gcc_assert (alias_template_specialization_p (t, nt_opaque));
499 tree decl = TYPE_NAME (t);
500 if (!(flags & TFF_UNQUALIFIED_NAME))
501 dump_scope (pp, CP_DECL_CONTEXT (decl), flags);
502 pp_cxx_tree_identifier (pp, DECL_NAME (decl));
503 dump_template_parms (pp, DECL_TEMPLATE_INFO (decl),
504 /*primary=*/false,
505 flags & ~TFF_TEMPLATE_HEADER);
508 /* Dump a human-readable equivalent of TYPE. FLAGS controls the
509 format. */
511 static void
512 dump_type (cxx_pretty_printer *pp, tree t, int flags)
514 if (t == NULL_TREE)
515 return;
517 /* Don't print e.g. "struct mytypedef". */
518 if (TYPE_P (t) && typedef_variant_p (t))
520 tree decl = TYPE_NAME (t);
521 if ((flags & TFF_CHASE_TYPEDEF)
522 || DECL_SELF_REFERENCE_P (decl)
523 || (!flag_pretty_templates
524 && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)))
526 unsigned int stf_flags = (!(pp->flags & pp_c_flag_gnu_v3)
527 ? STF_USER_VISIBLE : 0);
528 t = strip_typedefs (t, NULL, stf_flags);
530 else if (alias_template_specialization_p (t, nt_opaque))
532 dump_alias_template_specialization (pp, t, flags);
533 return;
535 else if (same_type_p (t, TREE_TYPE (decl)))
536 t = decl;
537 else
539 pp_cxx_cv_qualifier_seq (pp, t);
540 if (! (flags & TFF_UNQUALIFIED_NAME))
541 dump_scope (pp, CP_DECL_CONTEXT (decl), flags);
542 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
543 return;
547 if (TYPE_PTRMEMFUNC_P (t))
548 goto offset_type;
550 switch (TREE_CODE (t))
552 case LANG_TYPE:
553 if (t == init_list_type_node)
554 pp_string (pp, M_("<brace-enclosed initializer list>"));
555 else if (t == unknown_type_node)
556 pp_string (pp, M_("<unresolved overloaded function type>"));
557 else
559 pp_cxx_cv_qualifier_seq (pp, t);
560 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
562 break;
564 case TREE_LIST:
565 /* A list of function parms. */
566 dump_parameters (pp, t, flags);
567 break;
569 case IDENTIFIER_NODE:
570 pp_cxx_tree_identifier (pp, t);
571 break;
573 case TREE_BINFO:
574 dump_type (pp, BINFO_TYPE (t), flags);
575 break;
577 case RECORD_TYPE:
578 case UNION_TYPE:
579 case ENUMERAL_TYPE:
580 dump_aggr_type (pp, t, flags);
581 break;
583 case TYPE_DECL:
584 if (flags & TFF_CHASE_TYPEDEF)
586 dump_type (pp, DECL_ORIGINAL_TYPE (t)
587 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t), flags);
588 break;
590 /* Fall through. */
592 case TEMPLATE_DECL:
593 case NAMESPACE_DECL:
594 dump_decl (pp, t, flags & ~TFF_DECL_SPECIFIERS);
595 break;
597 case INTEGER_TYPE:
598 case REAL_TYPE:
599 case VOID_TYPE:
600 case OPAQUE_TYPE:
601 case BOOLEAN_TYPE:
602 case COMPLEX_TYPE:
603 case VECTOR_TYPE:
604 case FIXED_POINT_TYPE:
605 pp_type_specifier_seq (pp, t);
606 break;
608 case TEMPLATE_TEMPLATE_PARM:
609 /* For parameters inside template signature. */
610 if (TYPE_IDENTIFIER (t))
611 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
612 else
613 pp_cxx_canonical_template_parameter (pp, t);
614 break;
616 case BOUND_TEMPLATE_TEMPLATE_PARM:
618 tree args = TYPE_TI_ARGS (t);
619 pp_cxx_cv_qualifier_seq (pp, t);
620 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
621 pp_cxx_begin_template_argument_list (pp);
622 dump_template_argument_list (pp, args, flags);
623 pp_cxx_end_template_argument_list (pp);
625 break;
627 case TEMPLATE_TYPE_PARM:
628 pp_cxx_cv_qualifier_seq (pp, t);
629 if (template_placeholder_p (t))
631 t = TREE_TYPE (CLASS_PLACEHOLDER_TEMPLATE (t));
632 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
633 pp_string (pp, "<...auto...>");
635 else if (TYPE_IDENTIFIER (t))
636 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
637 else
638 pp_cxx_canonical_template_parameter
639 (pp, TEMPLATE_TYPE_PARM_INDEX (t));
640 /* If this is a constrained placeholder, add the requirements. */
641 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
642 pp_cxx_constrained_type_spec (pp, c);
643 break;
645 /* This is not always necessary for pointers and such, but doing this
646 reduces code size. */
647 case ARRAY_TYPE:
648 case POINTER_TYPE:
649 case REFERENCE_TYPE:
650 case OFFSET_TYPE:
651 offset_type:
652 case FUNCTION_TYPE:
653 case METHOD_TYPE:
655 dump_type_prefix (pp, t, flags);
656 dump_type_suffix (pp, t, flags);
657 break;
659 case TYPENAME_TYPE:
660 if (! (flags & TFF_CHASE_TYPEDEF)
661 && DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
663 dump_decl (pp, TYPE_NAME (t), TFF_PLAIN_IDENTIFIER);
664 break;
666 pp_cxx_cv_qualifier_seq (pp, t);
667 pp_cxx_ws_string (pp,
668 TYPENAME_IS_ENUM_P (t) ? "enum"
669 : TYPENAME_IS_CLASS_P (t) ? "class"
670 : "typename");
671 dump_typename (pp, t, flags);
672 break;
674 case UNBOUND_CLASS_TEMPLATE:
675 if (! (flags & TFF_UNQUALIFIED_NAME))
677 dump_type (pp, TYPE_CONTEXT (t), flags);
678 pp_cxx_colon_colon (pp);
680 pp_cxx_ws_string (pp, "template");
681 dump_type (pp, TYPE_IDENTIFIER (t), flags);
682 break;
684 case TYPEOF_TYPE:
685 pp_cxx_ws_string (pp, "__typeof__");
686 pp_cxx_whitespace (pp);
687 pp_cxx_left_paren (pp);
688 dump_expr (pp, TYPEOF_TYPE_EXPR (t), flags & ~TFF_EXPR_IN_PARENS);
689 pp_cxx_right_paren (pp);
690 break;
692 case UNDERLYING_TYPE:
693 pp_cxx_ws_string (pp, "__underlying_type");
694 pp_cxx_whitespace (pp);
695 pp_cxx_left_paren (pp);
696 dump_expr (pp, UNDERLYING_TYPE_TYPE (t), flags & ~TFF_EXPR_IN_PARENS);
697 pp_cxx_right_paren (pp);
698 break;
700 case TYPE_PACK_EXPANSION:
701 dump_type (pp, PACK_EXPANSION_PATTERN (t), flags);
702 pp_cxx_ws_string (pp, "...");
703 break;
705 case TYPE_ARGUMENT_PACK:
706 dump_template_argument (pp, t, flags);
707 break;
709 case DECLTYPE_TYPE:
710 pp_cxx_ws_string (pp, "decltype");
711 pp_cxx_whitespace (pp);
712 pp_cxx_left_paren (pp);
713 dump_expr (pp, DECLTYPE_TYPE_EXPR (t), flags & ~TFF_EXPR_IN_PARENS);
714 pp_cxx_right_paren (pp);
715 break;
717 case NULLPTR_TYPE:
718 pp_string (pp, "std::nullptr_t");
719 break;
721 default:
722 pp_unsupported_tree (pp, t);
723 /* Fall through. */
725 case ERROR_MARK:
726 pp_string (pp, M_("<type error>"));
727 break;
731 /* Dump a TYPENAME_TYPE. We need to notice when the context is itself
732 a TYPENAME_TYPE. */
734 static void
735 dump_typename (cxx_pretty_printer *pp, tree t, int flags)
737 tree ctx = TYPE_CONTEXT (t);
739 if (TREE_CODE (ctx) == TYPENAME_TYPE)
740 dump_typename (pp, ctx, flags);
741 else
742 dump_type (pp, ctx, flags & ~TFF_CLASS_KEY_OR_ENUM);
743 pp_cxx_colon_colon (pp);
744 dump_decl (pp, TYPENAME_TYPE_FULLNAME (t), flags);
747 /* Return the name of the supplied aggregate, or enumeral type. */
749 const char *
750 class_key_or_enum_as_string (tree t)
752 if (TREE_CODE (t) == ENUMERAL_TYPE)
754 if (SCOPED_ENUM_P (t))
755 return "enum class";
756 else
757 return "enum";
759 else if (TREE_CODE (t) == UNION_TYPE)
760 return "union";
761 else if (TYPE_LANG_SPECIFIC (t) && CLASSTYPE_DECLARED_CLASS (t))
762 return "class";
763 else
764 return "struct";
767 /* Print out a class declaration T under the control of FLAGS,
768 in the form `class foo'. */
770 static void
771 dump_aggr_type (cxx_pretty_printer *pp, tree t, int flags)
773 const char *variety = class_key_or_enum_as_string (t);
774 int typdef = 0;
775 int tmplate = 0;
777 pp_cxx_cv_qualifier_seq (pp, t);
779 if (flags & TFF_CLASS_KEY_OR_ENUM)
780 pp_cxx_ws_string (pp, variety);
782 tree decl = TYPE_NAME (t);
784 if (decl)
786 typdef = (!DECL_ARTIFICIAL (decl)
787 /* An alias specialization is not considered to be a
788 typedef. */
789 && !alias_template_specialization_p (t, nt_opaque));
791 if ((typdef
792 && ((flags & TFF_CHASE_TYPEDEF)
793 || (!flag_pretty_templates && DECL_LANG_SPECIFIC (decl)
794 && DECL_TEMPLATE_INFO (decl))))
795 || DECL_SELF_REFERENCE_P (decl))
797 t = TYPE_MAIN_VARIANT (t);
798 decl = TYPE_NAME (t);
799 typdef = 0;
802 tmplate = !typdef && TREE_CODE (t) != ENUMERAL_TYPE
803 && TYPE_LANG_SPECIFIC (t) && CLASSTYPE_TEMPLATE_INFO (t)
804 && (TREE_CODE (CLASSTYPE_TI_TEMPLATE (t)) != TEMPLATE_DECL
805 || PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
807 if (! (flags & TFF_UNQUALIFIED_NAME))
808 dump_scope (pp, CP_DECL_CONTEXT (decl), flags | TFF_SCOPE);
809 flags &= ~TFF_UNQUALIFIED_NAME;
810 if (tmplate)
812 /* Because the template names are mangled, we have to locate
813 the most general template, and use that name. */
814 tree tpl = TYPE_TI_TEMPLATE (t);
816 while (DECL_TEMPLATE_INFO (tpl))
817 tpl = DECL_TI_TEMPLATE (tpl);
818 decl = tpl;
822 if (LAMBDA_TYPE_P (t))
824 /* A lambda's "type" is essentially its signature. */
825 pp_string (pp, M_("<lambda"));
826 if (lambda_function (t))
827 dump_parameters (pp,
828 FUNCTION_FIRST_USER_PARMTYPE (lambda_function (t)),
829 flags);
830 pp_greater (pp);
832 else if (!decl || IDENTIFIER_ANON_P (DECL_NAME (decl)))
834 if (flags & TFF_CLASS_KEY_OR_ENUM)
835 pp_string (pp, M_("<unnamed>"));
836 else
837 pp_printf (pp, M_("<unnamed %s>"), variety);
839 else
840 pp_cxx_tree_identifier (pp, DECL_NAME (decl));
842 dump_module_suffix (pp, decl);
844 if (tmplate)
845 dump_template_parms (pp, TYPE_TEMPLATE_INFO (t),
846 !CLASSTYPE_USE_TEMPLATE (t),
847 flags & ~TFF_TEMPLATE_HEADER);
850 /* Dump into the obstack the initial part of the output for a given type.
851 This is necessary when dealing with things like functions returning
852 functions. Examples:
854 return type of `int (* fee ())()': pointer -> function -> int. Both
855 pointer (and reference and offset) and function (and member) types must
856 deal with prefix and suffix.
858 Arrays must also do this for DECL nodes, like int a[], and for things like
859 int *[]&. */
861 static void
862 dump_type_prefix (cxx_pretty_printer *pp, tree t, int flags)
864 if (TYPE_PTRMEMFUNC_P (t))
866 t = TYPE_PTRMEMFUNC_FN_TYPE (t);
867 goto offset_type;
870 switch (TREE_CODE (t))
872 case POINTER_TYPE:
873 case REFERENCE_TYPE:
875 tree sub = TREE_TYPE (t);
877 dump_type_prefix (pp, sub, flags);
878 if (TREE_CODE (sub) == ARRAY_TYPE
879 || TREE_CODE (sub) == FUNCTION_TYPE)
881 pp_cxx_whitespace (pp);
882 pp_cxx_left_paren (pp);
883 pp_c_attributes_display (pp, TYPE_ATTRIBUTES (sub));
885 if (TYPE_PTR_P (t))
886 pp_star (pp);
887 else if (TYPE_REF_P (t))
889 if (TYPE_REF_IS_RVALUE (t))
890 pp_ampersand_ampersand (pp);
891 else
892 pp_ampersand (pp);
894 pp->padding = pp_before;
895 pp_cxx_cv_qualifier_seq (pp, t);
897 break;
899 case OFFSET_TYPE:
900 offset_type:
901 dump_type_prefix (pp, TREE_TYPE (t), flags);
902 if (TREE_CODE (t) == OFFSET_TYPE) /* pmfs deal with this in d_t_p */
904 pp_maybe_space (pp);
905 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
906 pp_cxx_left_paren (pp);
907 dump_type (pp, TYPE_OFFSET_BASETYPE (t), flags);
908 pp_cxx_colon_colon (pp);
910 pp_cxx_star (pp);
911 pp_cxx_cv_qualifier_seq (pp, t);
912 pp->padding = pp_before;
913 break;
915 /* This can be reached without a pointer when dealing with
916 templates, e.g. std::is_function. */
917 case FUNCTION_TYPE:
918 dump_type_prefix (pp, TREE_TYPE (t), flags);
919 break;
921 case METHOD_TYPE:
922 dump_type_prefix (pp, TREE_TYPE (t), flags);
923 pp_maybe_space (pp);
924 pp_cxx_left_paren (pp);
925 dump_aggr_type (pp, TYPE_METHOD_BASETYPE (t), flags);
926 pp_cxx_colon_colon (pp);
927 break;
929 case ARRAY_TYPE:
930 dump_type_prefix (pp, TREE_TYPE (t), flags);
931 break;
933 case ENUMERAL_TYPE:
934 case IDENTIFIER_NODE:
935 case INTEGER_TYPE:
936 case BOOLEAN_TYPE:
937 case REAL_TYPE:
938 case RECORD_TYPE:
939 case TEMPLATE_TYPE_PARM:
940 case TEMPLATE_TEMPLATE_PARM:
941 case BOUND_TEMPLATE_TEMPLATE_PARM:
942 case TREE_LIST:
943 case TYPE_DECL:
944 case TREE_VEC:
945 case UNION_TYPE:
946 case LANG_TYPE:
947 case VOID_TYPE:
948 case OPAQUE_TYPE:
949 case TYPENAME_TYPE:
950 case COMPLEX_TYPE:
951 case VECTOR_TYPE:
952 case TYPEOF_TYPE:
953 case UNDERLYING_TYPE:
954 case DECLTYPE_TYPE:
955 case TYPE_PACK_EXPANSION:
956 case FIXED_POINT_TYPE:
957 case NULLPTR_TYPE:
958 dump_type (pp, t, flags);
959 pp->padding = pp_before;
960 break;
962 default:
963 pp_unsupported_tree (pp, t);
964 /* fall through. */
965 case ERROR_MARK:
966 pp_string (pp, M_("<typeprefixerror>"));
967 break;
971 /* Dump the suffix of type T, under control of FLAGS. This is the part
972 which appears after the identifier (or function parms). */
974 static void
975 dump_type_suffix (cxx_pretty_printer *pp, tree t, int flags)
977 if (TYPE_PTRMEMFUNC_P (t))
978 t = TYPE_PTRMEMFUNC_FN_TYPE (t);
980 switch (TREE_CODE (t))
982 case POINTER_TYPE:
983 case REFERENCE_TYPE:
984 case OFFSET_TYPE:
985 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
986 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
987 pp_cxx_right_paren (pp);
988 if (TREE_CODE (t) == POINTER_TYPE)
989 flags |= TFF_POINTER;
990 dump_type_suffix (pp, TREE_TYPE (t), flags);
991 break;
993 case FUNCTION_TYPE:
994 case METHOD_TYPE:
996 tree arg;
997 if (TREE_CODE (t) == METHOD_TYPE)
998 /* Can only be reached through a pointer. */
999 pp_cxx_right_paren (pp);
1000 arg = TYPE_ARG_TYPES (t);
1001 if (TREE_CODE (t) == METHOD_TYPE)
1002 arg = TREE_CHAIN (arg);
1004 /* Function pointers don't have default args. Not in standard C++,
1005 anyway; they may in g++, but we'll just pretend otherwise. */
1006 dump_parameters (pp, arg, flags & ~TFF_FUNCTION_DEFAULT_ARGUMENTS);
1008 pp->padding = pp_before;
1009 pp_cxx_cv_qualifiers (pp, type_memfn_quals (t),
1010 TREE_CODE (t) == FUNCTION_TYPE
1011 && (flags & TFF_POINTER));
1012 dump_ref_qualifier (pp, t, flags);
1013 if (tx_safe_fn_type_p (t))
1014 pp_cxx_ws_string (pp, "transaction_safe");
1015 dump_exception_spec (pp, TYPE_RAISES_EXCEPTIONS (t), flags);
1016 dump_type_suffix (pp, TREE_TYPE (t), flags);
1017 break;
1020 case ARRAY_TYPE:
1021 pp_maybe_space (pp);
1022 pp_cxx_left_bracket (pp);
1023 if (tree dtype = TYPE_DOMAIN (t))
1025 tree max = TYPE_MAX_VALUE (dtype);
1026 /* Zero-length arrays have a null upper bound in C and SIZE_MAX
1027 in C++. Handle both since the type might be constructed by
1028 the middle end and end up here as a result of a warning (see
1029 PR c++/97201). */
1030 if (!max || integer_all_onesp (max))
1031 pp_character (pp, '0');
1032 else if (tree_fits_shwi_p (max))
1033 pp_wide_integer (pp, tree_to_shwi (max) + 1);
1034 else
1036 STRIP_NOPS (max);
1037 if (TREE_CODE (max) == SAVE_EXPR)
1038 max = TREE_OPERAND (max, 0);
1039 if (TREE_CODE (max) == MINUS_EXPR
1040 || TREE_CODE (max) == PLUS_EXPR)
1042 max = TREE_OPERAND (max, 0);
1043 while (CONVERT_EXPR_P (max))
1044 max = TREE_OPERAND (max, 0);
1046 else
1047 max = fold_build2_loc (input_location,
1048 PLUS_EXPR, dtype, max,
1049 build_int_cst (dtype, 1));
1050 dump_expr (pp, max, flags & ~TFF_EXPR_IN_PARENS);
1053 pp_cxx_right_bracket (pp);
1054 dump_type_suffix (pp, TREE_TYPE (t), flags);
1055 break;
1057 case ENUMERAL_TYPE:
1058 case IDENTIFIER_NODE:
1059 case INTEGER_TYPE:
1060 case BOOLEAN_TYPE:
1061 case REAL_TYPE:
1062 case RECORD_TYPE:
1063 case TEMPLATE_TYPE_PARM:
1064 case TEMPLATE_TEMPLATE_PARM:
1065 case BOUND_TEMPLATE_TEMPLATE_PARM:
1066 case TREE_LIST:
1067 case TYPE_DECL:
1068 case TREE_VEC:
1069 case UNION_TYPE:
1070 case LANG_TYPE:
1071 case VOID_TYPE:
1072 case OPAQUE_TYPE:
1073 case TYPENAME_TYPE:
1074 case COMPLEX_TYPE:
1075 case VECTOR_TYPE:
1076 case TYPEOF_TYPE:
1077 case UNDERLYING_TYPE:
1078 case DECLTYPE_TYPE:
1079 case TYPE_PACK_EXPANSION:
1080 case FIXED_POINT_TYPE:
1081 case NULLPTR_TYPE:
1082 break;
1084 default:
1085 pp_unsupported_tree (pp, t);
1086 case ERROR_MARK:
1087 /* Don't mark it here, we should have already done in
1088 dump_type_prefix. */
1089 break;
1093 static void
1094 dump_global_iord (cxx_pretty_printer *pp, tree t)
1096 const char *p = NULL;
1098 if (DECL_GLOBAL_CTOR_P (t))
1099 p = M_("(static initializers for %s)");
1100 else if (DECL_GLOBAL_DTOR_P (t))
1101 p = M_("(static destructors for %s)");
1102 else
1103 gcc_unreachable ();
1105 pp_printf (pp, p, DECL_SOURCE_FILE (t));
1108 static void
1109 dump_simple_decl (cxx_pretty_printer *pp, tree t, tree type, int flags)
1111 if (template_parm_object_p (t))
1112 return dump_expr (pp, DECL_INITIAL (t), flags);
1114 if (flags & TFF_DECL_SPECIFIERS)
1116 if (concept_definition_p (t))
1117 pp_cxx_ws_string (pp, "concept");
1118 else if (VAR_P (t) && DECL_DECLARED_CONSTEXPR_P (t))
1119 pp_cxx_ws_string (pp, "constexpr");
1121 if (!standard_concept_p (t))
1122 dump_type_prefix (pp, type, flags & ~TFF_UNQUALIFIED_NAME);
1123 pp_maybe_space (pp);
1125 if (! (flags & TFF_UNQUALIFIED_NAME)
1126 && TREE_CODE (t) != PARM_DECL
1127 && (!DECL_INITIAL (t)
1128 || TREE_CODE (DECL_INITIAL (t)) != TEMPLATE_PARM_INDEX))
1129 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
1130 flags &= ~TFF_UNQUALIFIED_NAME;
1131 if ((flags & TFF_DECL_SPECIFIERS)
1132 && DECL_TEMPLATE_PARM_P (t)
1133 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (t)))
1134 pp_string (pp, "...");
1135 if (DECL_NAME (t))
1137 if (TREE_CODE (t) == FIELD_DECL && DECL_NORMAL_CAPTURE_P (t))
1139 pp_less (pp);
1140 pp_string (pp, IDENTIFIER_POINTER (DECL_NAME (t)) + 2);
1141 pp_string (pp, " capture>");
1143 else
1144 dump_decl (pp, DECL_NAME (t), flags);
1146 else if (DECL_DECOMPOSITION_P (t))
1147 pp_string (pp, M_("<structured bindings>"));
1148 else
1149 pp_string (pp, M_("<anonymous>"));
1151 dump_module_suffix (pp, t);
1153 if (flags & TFF_DECL_SPECIFIERS)
1154 dump_type_suffix (pp, type, flags);
1157 /* Print an IDENTIFIER_NODE that is the name of a declaration. */
1159 static void
1160 dump_decl_name (cxx_pretty_printer *pp, tree t, int flags)
1162 /* These special cases are duplicated here so that other functions
1163 can feed identifiers to error and get them demangled properly. */
1164 if (IDENTIFIER_CONV_OP_P (t))
1166 pp_cxx_ws_string (pp, "operator");
1167 /* Not exactly IDENTIFIER_TYPE_VALUE. */
1168 dump_type (pp, TREE_TYPE (t), flags);
1169 return;
1171 if (dguide_name_p (t))
1173 dump_decl (pp, CLASSTYPE_TI_TEMPLATE (TREE_TYPE (t)),
1174 TFF_UNQUALIFIED_NAME);
1175 return;
1178 const char *str = IDENTIFIER_POINTER (t);
1179 if (startswith (str, "_ZGR"))
1181 pp_cxx_ws_string (pp, "<temporary>");
1182 return;
1185 pp_cxx_tree_identifier (pp, t);
1188 /* Dump a human readable string for the decl T under control of FLAGS. */
1190 static void
1191 dump_decl (cxx_pretty_printer *pp, tree t, int flags)
1193 if (t == NULL_TREE)
1194 return;
1196 /* If doing Objective-C++, give Objective-C a chance to demangle
1197 Objective-C method names. */
1198 if (c_dialect_objc ())
1200 const char *demangled = objc_maybe_printable_name (t, flags);
1201 if (demangled)
1203 pp_string (pp, demangled);
1204 return;
1208 switch (TREE_CODE (t))
1210 case TYPE_DECL:
1211 /* Don't say 'typedef class A' */
1212 if (DECL_ARTIFICIAL (t) && !DECL_SELF_REFERENCE_P (t))
1214 if ((flags & TFF_DECL_SPECIFIERS)
1215 && TREE_CODE (TREE_TYPE (t)) == TEMPLATE_TYPE_PARM)
1217 /* Say `class T' not just `T'. */
1218 pp_cxx_ws_string (pp, "class");
1220 /* Emit the `...' for a parameter pack. */
1221 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
1222 pp_cxx_ws_string (pp, "...");
1225 dump_type (pp, TREE_TYPE (t), flags);
1226 break;
1228 if (TYPE_DECL_ALIAS_P (t)
1229 && (flags & TFF_DECL_SPECIFIERS
1230 || flags & TFF_CLASS_KEY_OR_ENUM))
1232 pp_cxx_ws_string (pp, "using");
1233 dump_decl (pp, DECL_NAME (t), flags);
1234 pp_cxx_whitespace (pp);
1235 pp_cxx_ws_string (pp, "=");
1236 pp_cxx_whitespace (pp);
1237 dump_type (pp, (DECL_ORIGINAL_TYPE (t)
1238 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t)),
1239 flags);
1240 break;
1242 if ((flags & TFF_DECL_SPECIFIERS)
1243 && !DECL_SELF_REFERENCE_P (t))
1244 pp_cxx_ws_string (pp, "typedef");
1245 dump_simple_decl (pp, t, DECL_ORIGINAL_TYPE (t)
1246 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t),
1247 flags);
1248 break;
1250 case VAR_DECL:
1251 if (DECL_NAME (t) && VTABLE_NAME_P (DECL_NAME (t)))
1253 pp_string (pp, M_("vtable for "));
1254 gcc_assert (TYPE_P (DECL_CONTEXT (t)));
1255 dump_type (pp, DECL_CONTEXT (t), flags);
1256 break;
1258 /* Fall through. */
1259 case FIELD_DECL:
1260 case PARM_DECL:
1261 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1263 /* Handle variable template specializations. */
1264 if (VAR_P (t)
1265 && DECL_LANG_SPECIFIC (t)
1266 && DECL_TEMPLATE_INFO (t)
1267 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1269 pp_cxx_begin_template_argument_list (pp);
1270 tree args = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1271 dump_template_argument_list (pp, args, flags);
1272 pp_cxx_end_template_argument_list (pp);
1274 break;
1276 case RESULT_DECL:
1277 pp_string (pp, M_("<return value> "));
1278 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1279 break;
1281 case NAMESPACE_DECL:
1282 if (flags & TFF_DECL_SPECIFIERS)
1283 pp->declaration (t);
1284 else
1286 if (! (flags & TFF_UNQUALIFIED_NAME))
1287 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
1288 flags &= ~TFF_UNQUALIFIED_NAME;
1289 if (DECL_NAME (t) == NULL_TREE)
1291 if (!(pp->flags & pp_c_flag_gnu_v3))
1292 pp_cxx_ws_string (pp, M_("{anonymous}"));
1293 else
1294 pp_cxx_ws_string (pp, M_("(anonymous namespace)"));
1296 else
1297 pp_cxx_tree_identifier (pp, DECL_NAME (t));
1299 break;
1301 case SCOPE_REF:
1302 dump_type (pp, TREE_OPERAND (t, 0), flags);
1303 pp_cxx_colon_colon (pp);
1304 dump_decl (pp, TREE_OPERAND (t, 1), TFF_UNQUALIFIED_NAME);
1305 break;
1307 case ARRAY_REF:
1308 dump_decl (pp, TREE_OPERAND (t, 0), flags);
1309 pp_cxx_left_bracket (pp);
1310 dump_decl (pp, TREE_OPERAND (t, 1), flags);
1311 pp_cxx_right_bracket (pp);
1312 break;
1314 /* So that we can do dump_decl on an aggr type. */
1315 case RECORD_TYPE:
1316 case UNION_TYPE:
1317 case ENUMERAL_TYPE:
1318 dump_type (pp, t, flags);
1319 break;
1321 case BIT_NOT_EXPR:
1322 /* This is a pseudo destructor call which has not been folded into
1323 a PSEUDO_DTOR_EXPR yet. */
1324 pp_cxx_complement (pp);
1325 dump_type (pp, TREE_OPERAND (t, 0), flags);
1326 break;
1328 case TYPE_EXPR:
1329 gcc_unreachable ();
1330 break;
1332 case IDENTIFIER_NODE:
1333 dump_decl_name (pp, t, flags);
1334 break;
1336 case OVERLOAD:
1337 if (!OVL_SINGLE_P (t))
1339 tree ctx = ovl_scope (t);
1340 if (ctx != global_namespace)
1342 if (TYPE_P (ctx))
1343 dump_type (pp, ctx, flags);
1344 else
1345 dump_decl (pp, ctx, flags);
1346 pp_cxx_colon_colon (pp);
1348 dump_decl (pp, OVL_NAME (t), flags);
1349 break;
1352 /* If there's only one function, just treat it like an ordinary
1353 FUNCTION_DECL. */
1354 t = OVL_FIRST (t);
1355 /* Fall through. */
1357 case FUNCTION_DECL:
1358 if (! DECL_LANG_SPECIFIC (t))
1360 if (DECL_ABSTRACT_ORIGIN (t)
1361 && DECL_ABSTRACT_ORIGIN (t) != t)
1362 dump_decl (pp, DECL_ABSTRACT_ORIGIN (t), flags);
1363 else
1364 dump_function_name (pp, t, flags);
1366 else if (DECL_GLOBAL_CTOR_P (t) || DECL_GLOBAL_DTOR_P (t))
1367 dump_global_iord (pp, t);
1368 else
1369 dump_function_decl (pp, t, flags);
1370 break;
1372 case TEMPLATE_DECL:
1373 dump_template_decl (pp, t, flags);
1374 break;
1376 case CONCEPT_DECL:
1377 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1378 break;
1380 case WILDCARD_DECL:
1381 pp_string (pp, "<wildcard>");
1382 break;
1384 case TEMPLATE_ID_EXPR:
1386 tree name = TREE_OPERAND (t, 0);
1387 tree args = TREE_OPERAND (t, 1);
1389 if (!identifier_p (name))
1390 name = OVL_NAME (name);
1391 dump_decl (pp, name, flags);
1392 pp_cxx_begin_template_argument_list (pp);
1393 if (args == error_mark_node)
1394 pp_string (pp, M_("<template arguments error>"));
1395 else if (args)
1396 dump_template_argument_list
1397 (pp, args, flags|TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS);
1398 pp_cxx_end_template_argument_list (pp);
1400 break;
1402 case LABEL_DECL:
1403 if (DECL_NAME (t))
1404 pp_cxx_tree_identifier (pp, DECL_NAME (t));
1405 else
1406 dump_generic_node (pp, t, 0, TDF_SLIM, false);
1407 break;
1409 case CONST_DECL:
1410 if ((TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == ENUMERAL_TYPE)
1411 || (DECL_INITIAL (t) &&
1412 TREE_CODE (DECL_INITIAL (t)) == TEMPLATE_PARM_INDEX))
1413 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1414 else if (DECL_NAME (t))
1415 dump_decl (pp, DECL_NAME (t), flags);
1416 else if (DECL_INITIAL (t))
1417 dump_expr (pp, DECL_INITIAL (t), flags | TFF_EXPR_IN_PARENS);
1418 else
1419 pp_string (pp, M_("<enumerator>"));
1420 break;
1422 case USING_DECL:
1424 pp_cxx_ws_string (pp, "using");
1425 tree scope = USING_DECL_SCOPE (t);
1426 bool variadic = false;
1427 if (PACK_EXPANSION_P (scope))
1429 scope = PACK_EXPANSION_PATTERN (scope);
1430 variadic = true;
1432 dump_type (pp, scope, flags);
1433 pp_cxx_colon_colon (pp);
1434 dump_decl (pp, DECL_NAME (t), flags);
1435 if (variadic)
1436 pp_cxx_ws_string (pp, "...");
1438 break;
1440 case STATIC_ASSERT:
1441 pp->declaration (t);
1442 break;
1444 case BASELINK:
1445 dump_decl (pp, BASELINK_FUNCTIONS (t), flags);
1446 break;
1448 case NON_DEPENDENT_EXPR:
1449 dump_expr (pp, t, flags);
1450 break;
1452 case TEMPLATE_TYPE_PARM:
1453 if (flags & TFF_DECL_SPECIFIERS)
1454 pp->declaration (t);
1455 else
1456 pp->type_id (t);
1457 break;
1459 case UNBOUND_CLASS_TEMPLATE:
1460 case TYPE_PACK_EXPANSION:
1461 case TREE_BINFO:
1462 dump_type (pp, t, flags);
1463 break;
1465 default:
1466 pp_unsupported_tree (pp, t);
1467 /* Fall through. */
1469 case ERROR_MARK:
1470 pp_string (pp, M_("<declaration error>"));
1471 break;
1475 /* Dump a template declaration T under control of FLAGS. This means the
1476 'template <...> leaders plus the 'class X' or 'void fn(...)' part. */
1478 static void
1479 dump_template_decl (cxx_pretty_printer *pp, tree t, int flags)
1481 tree orig_parms = DECL_TEMPLATE_PARMS (t);
1482 tree parms;
1483 int i;
1485 if (flags & TFF_TEMPLATE_HEADER)
1487 for (parms = orig_parms = nreverse (orig_parms);
1488 parms;
1489 parms = TREE_CHAIN (parms))
1491 tree inner_parms = INNERMOST_TEMPLATE_PARMS (parms);
1492 int len = TREE_VEC_LENGTH (inner_parms);
1494 if (len == 0)
1496 /* Skip over the dummy template levels of a template template
1497 parm. */
1498 gcc_assert (TREE_CODE (TREE_TYPE (t)) == TEMPLATE_TEMPLATE_PARM);
1499 continue;
1502 pp_cxx_ws_string (pp, "template");
1503 pp_cxx_begin_template_argument_list (pp);
1505 /* If we've shown the template prefix, we'd better show the
1506 parameters' and decl's type too. */
1507 flags |= TFF_DECL_SPECIFIERS;
1509 for (i = 0; i < len; i++)
1511 if (i)
1512 pp_separate_with_comma (pp);
1513 dump_template_parameter (pp, TREE_VEC_ELT (inner_parms, i),
1514 flags);
1516 pp_cxx_end_template_argument_list (pp);
1517 pp_cxx_whitespace (pp);
1519 nreverse(orig_parms);
1521 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
1523 /* Say `template<arg> class TT' not just `template<arg> TT'. */
1524 pp_cxx_ws_string (pp, "class");
1526 /* If this is a parameter pack, print the ellipsis. */
1527 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
1528 pp_cxx_ws_string (pp, "...");
1531 /* Only print the requirements if we're also printing
1532 the template header. */
1533 if (flag_concepts)
1534 if (tree ci = get_constraints (t))
1535 if (check_constraint_info (ci))
1536 if (tree reqs = CI_TEMPLATE_REQS (ci))
1538 pp_cxx_requires_clause (pp, reqs);
1539 pp_cxx_whitespace (pp);
1544 if (DECL_CLASS_TEMPLATE_P (t))
1545 dump_type (pp, TREE_TYPE (t),
1546 ((flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1547 | (flags & TFF_DECL_SPECIFIERS ? TFF_CLASS_KEY_OR_ENUM : 0)));
1548 else if (DECL_TEMPLATE_RESULT (t)
1549 && (VAR_P (DECL_TEMPLATE_RESULT (t))
1550 /* Alias template. */
1551 || DECL_TYPE_TEMPLATE_P (t)
1552 /* Concept definition. &*/
1553 || TREE_CODE (DECL_TEMPLATE_RESULT (t)) == CONCEPT_DECL))
1554 dump_decl (pp, DECL_TEMPLATE_RESULT (t), flags | TFF_TEMPLATE_NAME);
1555 else
1557 gcc_assert (TREE_TYPE (t));
1558 switch (NEXT_CODE (t))
1560 case METHOD_TYPE:
1561 case FUNCTION_TYPE:
1562 dump_function_decl (pp, t, flags | TFF_TEMPLATE_NAME);
1563 break;
1564 default:
1565 /* This case can occur with some invalid code. */
1566 dump_type (pp, TREE_TYPE (t),
1567 (flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1568 | (flags & TFF_DECL_SPECIFIERS
1569 ? TFF_CLASS_KEY_OR_ENUM : 0));
1574 /* find_typenames looks through the type of the function template T
1575 and returns a vec containing any typedefs, decltypes or TYPENAME_TYPEs
1576 it finds. */
1578 struct find_typenames_t
1580 hash_set<tree> *p_set;
1581 vec<tree, va_gc> *typenames;
1584 static tree
1585 find_typenames_r (tree *tp, int *walk_subtrees, void *data)
1587 struct find_typenames_t *d = (struct find_typenames_t *)data;
1588 tree mv = NULL_TREE;
1590 if (TYPE_P (*tp) && is_typedef_decl (TYPE_NAME (*tp)))
1591 /* Add the type of the typedef without any additional cv-quals. */
1592 mv = TREE_TYPE (TYPE_NAME (*tp));
1593 else if (TREE_CODE (*tp) == TYPENAME_TYPE
1594 || TREE_CODE (*tp) == DECLTYPE_TYPE)
1595 /* Add the typename without any cv-qualifiers. */
1596 mv = TYPE_MAIN_VARIANT (*tp);
1598 if (PACK_EXPANSION_P (*tp))
1600 /* Don't mess with parameter packs since we don't remember
1601 the pack expansion context for a particular typename. */
1602 *walk_subtrees = false;
1603 return NULL_TREE;
1606 if (mv && (mv == *tp || !d->p_set->add (mv)))
1607 vec_safe_push (d->typenames, mv);
1609 return NULL_TREE;
1612 static vec<tree, va_gc> *
1613 find_typenames (tree t)
1615 struct find_typenames_t ft;
1616 ft.p_set = new hash_set<tree>;
1617 ft.typenames = NULL;
1618 cp_walk_tree (&TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
1619 find_typenames_r, &ft, ft.p_set);
1620 delete ft.p_set;
1621 return ft.typenames;
1624 /* Output the "[with ...]" clause for a template instantiation T iff
1625 TEMPLATE_PARMS, TEMPLATE_ARGS and FLAGS are suitable. T may be NULL if
1626 formatting a deduction/substitution diagnostic rather than an
1627 instantiation. */
1629 static void
1630 dump_substitution (cxx_pretty_printer *pp,
1631 tree t, tree template_parms, tree template_args,
1632 int flags)
1634 if (template_parms != NULL_TREE && template_args != NULL_TREE
1635 && !(flags & TFF_NO_TEMPLATE_BINDINGS))
1637 vec<tree, va_gc> *typenames = t ? find_typenames (t) : NULL;
1638 dump_template_bindings (pp, template_parms, template_args, typenames);
1642 /* Dump the lambda function FN including its 'mutable' qualifier and any
1643 template bindings. */
1645 static void
1646 dump_lambda_function (cxx_pretty_printer *pp,
1647 tree fn, tree template_parms, tree template_args,
1648 int flags)
1650 /* A lambda's signature is essentially its "type". */
1651 dump_type (pp, DECL_CONTEXT (fn), flags);
1652 if (!(TYPE_QUALS (class_of_this_parm (TREE_TYPE (fn))) & TYPE_QUAL_CONST))
1654 pp->padding = pp_before;
1655 pp_c_ws_string (pp, "mutable");
1657 dump_substitution (pp, fn, template_parms, template_args, flags);
1660 /* Pretty print a function decl. There are several ways we want to print a
1661 function declaration. The TFF_ bits in FLAGS tells us how to behave.
1662 As error can only apply the '#' flag once to give 0 and 1 for V, there
1663 is %D which doesn't print the throw specs, and %F which does. */
1665 static void
1666 dump_function_decl (cxx_pretty_printer *pp, tree t, int flags)
1668 tree fntype;
1669 tree parmtypes;
1670 tree cname = NULL_TREE;
1671 tree template_args = NULL_TREE;
1672 tree template_parms = NULL_TREE;
1673 int show_return = flags & TFF_RETURN_TYPE || flags & TFF_DECL_SPECIFIERS;
1674 int do_outer_scope = ! (flags & TFF_UNQUALIFIED_NAME);
1675 tree exceptions;
1676 bool constexpr_p;
1677 tree ret = NULL_TREE;
1679 int dump_function_name_flags = flags & ~TFF_UNQUALIFIED_NAME;
1680 flags = dump_function_name_flags & ~TFF_TEMPLATE_NAME;
1681 if (TREE_CODE (t) == TEMPLATE_DECL)
1682 t = DECL_TEMPLATE_RESULT (t);
1684 /* Save the exceptions, in case t is a specialization and we are
1685 emitting an error about incompatible specifications. */
1686 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (t));
1688 /* Likewise for the constexpr specifier, in case t is a specialization. */
1689 constexpr_p = (DECL_DECLARED_CONSTEXPR_P (t)
1690 && !decl_implicit_constexpr_p (t));
1692 /* Pretty print template instantiations only. */
1693 if (DECL_USE_TEMPLATE (t) && DECL_TEMPLATE_INFO (t)
1694 && !(flags & TFF_NO_TEMPLATE_BINDINGS)
1695 && flag_pretty_templates)
1697 tree tmpl;
1699 template_args = DECL_TI_ARGS (t);
1700 tmpl = most_general_template (t);
1701 if (tmpl && TREE_CODE (tmpl) == TEMPLATE_DECL)
1703 template_parms = DECL_TEMPLATE_PARMS (tmpl);
1704 t = tmpl;
1708 if (DECL_NAME (t) && LAMBDA_FUNCTION_P (t))
1709 return dump_lambda_function (pp, t, template_parms, template_args, flags);
1711 fntype = TREE_TYPE (t);
1712 parmtypes = FUNCTION_FIRST_USER_PARMTYPE (t);
1714 if (DECL_CLASS_SCOPE_P (t))
1715 cname = DECL_CONTEXT (t);
1716 /* This is for partially instantiated template methods. */
1717 else if (TREE_CODE (fntype) == METHOD_TYPE)
1718 cname = TREE_TYPE (TREE_VALUE (parmtypes));
1720 if (flags & TFF_DECL_SPECIFIERS)
1722 if (DECL_STATIC_FUNCTION_P (t))
1723 pp_cxx_ws_string (pp, "static");
1724 else if (DECL_VIRTUAL_P (t))
1725 pp_cxx_ws_string (pp, "virtual");
1727 if (constexpr_p)
1729 if (DECL_DECLARED_CONCEPT_P (t))
1730 pp_cxx_ws_string (pp, "concept");
1731 else if (DECL_IMMEDIATE_FUNCTION_P (t))
1732 pp_cxx_ws_string (pp, "consteval");
1733 else
1734 pp_cxx_ws_string (pp, "constexpr");
1738 /* Print the return type? */
1739 if (show_return)
1740 show_return = (!DECL_CONV_FN_P (t) && !DECL_CONSTRUCTOR_P (t)
1741 && !DECL_DESTRUCTOR_P (t) && !deduction_guide_p (t));
1742 if (show_return)
1744 ret = fndecl_declared_return_type (t);
1745 dump_type_prefix (pp, ret, flags);
1748 /* Print the function name. */
1749 if (!do_outer_scope)
1750 /* Nothing. */;
1751 else if (cname)
1753 dump_type (pp, cname, flags);
1754 pp_cxx_colon_colon (pp);
1756 else
1757 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
1759 dump_function_name (pp, t, dump_function_name_flags);
1761 if (!(flags & TFF_NO_FUNCTION_ARGUMENTS))
1763 dump_parameters (pp, parmtypes, flags);
1765 if (TREE_CODE (fntype) == METHOD_TYPE)
1767 pp->padding = pp_before;
1768 pp_cxx_cv_qualifier_seq (pp, class_of_this_parm (fntype));
1769 dump_ref_qualifier (pp, fntype, flags);
1772 if (tx_safe_fn_type_p (fntype))
1774 pp->padding = pp_before;
1775 pp_cxx_ws_string (pp, "transaction_safe");
1778 if (flags & TFF_EXCEPTION_SPECIFICATION)
1780 pp->padding = pp_before;
1781 dump_exception_spec (pp, exceptions, flags);
1784 if (show_return)
1785 dump_type_suffix (pp, ret, flags);
1786 else if (deduction_guide_p (t))
1788 pp_cxx_ws_string (pp, "->");
1789 dump_type (pp, TREE_TYPE (TREE_TYPE (t)), flags);
1792 if (flag_concepts)
1793 if (tree ci = get_constraints (t))
1794 if (tree reqs = CI_DECLARATOR_REQS (ci))
1795 pp_cxx_requires_clause (pp, reqs);
1797 dump_substitution (pp, t, template_parms, template_args, flags);
1799 if (tree base = DECL_INHERITED_CTOR_BASE (t))
1801 pp_cxx_ws_string (pp, "[inherited from");
1802 dump_type (pp, base, TFF_PLAIN_IDENTIFIER);
1803 pp_character (pp, ']');
1806 else if (template_args)
1808 bool need_comma = false;
1809 int i;
1810 pp_cxx_begin_template_argument_list (pp);
1811 template_args = INNERMOST_TEMPLATE_ARGS (template_args);
1812 for (i = 0; i < TREE_VEC_LENGTH (template_args); ++i)
1814 tree arg = TREE_VEC_ELT (template_args, i);
1815 if (need_comma)
1816 pp_separate_with_comma (pp);
1817 if (ARGUMENT_PACK_P (arg))
1818 pp_cxx_left_brace (pp);
1819 dump_template_argument (pp, arg, TFF_PLAIN_IDENTIFIER);
1820 if (ARGUMENT_PACK_P (arg))
1821 pp_cxx_right_brace (pp);
1822 need_comma = true;
1824 pp_cxx_end_template_argument_list (pp);
1828 /* Print a parameter list. If this is for a member function, the
1829 member object ptr (and any other hidden args) should have
1830 already been removed. */
1832 static void
1833 dump_parameters (cxx_pretty_printer *pp, tree parmtypes, int flags)
1835 int first = 1;
1836 flags &= ~TFF_SCOPE;
1837 pp_cxx_left_paren (pp);
1839 for (first = 1; parmtypes != void_list_node;
1840 parmtypes = TREE_CHAIN (parmtypes))
1842 if (!first)
1843 pp_separate_with_comma (pp);
1844 first = 0;
1845 if (!parmtypes)
1847 pp_cxx_ws_string (pp, "...");
1848 break;
1851 dump_type (pp, TREE_VALUE (parmtypes), flags);
1853 if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && TREE_PURPOSE (parmtypes))
1855 pp_cxx_whitespace (pp);
1856 pp_equal (pp);
1857 pp_cxx_whitespace (pp);
1858 dump_expr (pp, TREE_PURPOSE (parmtypes), flags | TFF_EXPR_IN_PARENS);
1862 pp_cxx_right_paren (pp);
1865 /* Print ref-qualifier of a FUNCTION_TYPE or METHOD_TYPE. FLAGS are ignored. */
1867 static void
1868 dump_ref_qualifier (cxx_pretty_printer *pp, tree t, int flags ATTRIBUTE_UNUSED)
1870 if (FUNCTION_REF_QUALIFIED (t))
1872 pp->padding = pp_before;
1873 if (FUNCTION_RVALUE_QUALIFIED (t))
1874 pp_cxx_ws_string (pp, "&&");
1875 else
1876 pp_cxx_ws_string (pp, "&");
1880 /* Print an exception specification. T is the exception specification. */
1882 static void
1883 dump_exception_spec (cxx_pretty_printer *pp, tree t, int flags)
1885 if (t && TREE_PURPOSE (t))
1887 pp_cxx_ws_string (pp, "noexcept");
1888 if (!integer_onep (TREE_PURPOSE (t)))
1890 pp_cxx_whitespace (pp);
1891 pp_cxx_left_paren (pp);
1892 if (DEFERRED_NOEXCEPT_SPEC_P (t))
1893 pp_cxx_ws_string (pp, "<uninstantiated>");
1894 else
1895 dump_expr (pp, TREE_PURPOSE (t), flags);
1896 pp_cxx_right_paren (pp);
1899 else if (t)
1901 pp_cxx_ws_string (pp, "throw");
1902 pp_cxx_whitespace (pp);
1903 pp_cxx_left_paren (pp);
1904 if (TREE_VALUE (t) != NULL_TREE)
1905 while (1)
1907 dump_type (pp, TREE_VALUE (t), flags);
1908 t = TREE_CHAIN (t);
1909 if (!t)
1910 break;
1911 pp_separate_with_comma (pp);
1913 pp_cxx_right_paren (pp);
1917 /* Handle the function name for a FUNCTION_DECL node, grokking operators
1918 and destructors properly. */
1920 static void
1921 dump_function_name (cxx_pretty_printer *pp, tree t, int flags)
1923 tree name = DECL_NAME (t);
1925 /* We can get here with a decl that was synthesized by language-
1926 independent machinery (e.g. coverage.c) in which case it won't
1927 have a lang_specific structure attached and DECL_CONSTRUCTOR_P
1928 will crash. In this case it is safe just to print out the
1929 literal name. */
1930 if (!DECL_LANG_SPECIFIC (t))
1932 pp_cxx_tree_identifier (pp, name);
1933 return;
1936 if (TREE_CODE (t) == TEMPLATE_DECL)
1937 t = DECL_TEMPLATE_RESULT (t);
1939 /* Don't let the user see __comp_ctor et al. */
1940 if (DECL_CONSTRUCTOR_P (t)
1941 || DECL_DESTRUCTOR_P (t))
1943 if (LAMBDA_TYPE_P (DECL_CONTEXT (t)))
1944 name = get_identifier ("<lambda>");
1945 else if (TYPE_UNNAMED_P (DECL_CONTEXT (t)))
1946 name = get_identifier ("<constructor>");
1947 else
1948 name = constructor_name (DECL_CONTEXT (t));
1951 if (DECL_DESTRUCTOR_P (t))
1953 pp_cxx_complement (pp);
1954 dump_decl (pp, name, TFF_PLAIN_IDENTIFIER);
1956 else if (DECL_CONV_FN_P (t))
1958 /* This cannot use the hack that the operator's return
1959 type is stashed off of its name because it may be
1960 used for error reporting. In the case of conflicting
1961 declarations, both will have the same name, yet
1962 the types will be different, hence the TREE_TYPE field
1963 of the first name will be clobbered by the second. */
1964 pp_cxx_ws_string (pp, "operator");
1965 dump_type (pp, TREE_TYPE (TREE_TYPE (t)), flags);
1967 else
1968 dump_decl (pp, name, flags);
1970 dump_module_suffix (pp, t);
1972 if (DECL_TEMPLATE_INFO (t)
1973 && !(flags & TFF_TEMPLATE_NAME)
1974 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (t)
1975 && (TREE_CODE (DECL_TI_TEMPLATE (t)) != TEMPLATE_DECL
1976 || PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
1977 dump_template_parms (pp, DECL_TEMPLATE_INFO (t), !DECL_USE_TEMPLATE (t),
1978 flags);
1981 /* Dump the template parameters from the template info INFO under control of
1982 FLAGS. PRIMARY indicates whether this is a primary template decl, or
1983 specialization (partial or complete). For partial specializations we show
1984 the specialized parameter values. For a primary template we show no
1985 decoration. */
1987 static void
1988 dump_template_parms (cxx_pretty_printer *pp, tree info,
1989 int primary, int flags)
1991 tree args = info ? TI_ARGS (info) : NULL_TREE;
1993 if (primary && flags & TFF_TEMPLATE_NAME)
1994 return;
1995 flags &= ~(TFF_CLASS_KEY_OR_ENUM | TFF_TEMPLATE_NAME);
1996 pp_cxx_begin_template_argument_list (pp);
1998 /* Be careful only to print things when we have them, so as not
1999 to crash producing error messages. */
2000 if (args && !primary)
2002 int len, ix;
2003 len = get_non_default_template_args_count (args, flags);
2005 args = INNERMOST_TEMPLATE_ARGS (args);
2006 for (ix = 0; ix != len; ix++)
2008 tree arg = TREE_VEC_ELT (args, ix);
2010 /* Only print a comma if we know there is an argument coming. In
2011 the case of an empty template argument pack, no actual
2012 argument will be printed. */
2013 if (ix
2014 && (!ARGUMENT_PACK_P (arg)
2015 || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) > 0))
2016 pp_separate_with_comma (pp);
2018 if (!arg)
2019 pp_string (pp, M_("<template parameter error>"));
2020 else
2021 dump_template_argument (pp, arg, flags);
2024 else if (primary)
2026 tree tpl = TI_TEMPLATE (info);
2027 tree parms = DECL_TEMPLATE_PARMS (tpl);
2028 int len, ix;
2030 parms = TREE_CODE (parms) == TREE_LIST ? TREE_VALUE (parms) : NULL_TREE;
2031 len = parms ? TREE_VEC_LENGTH (parms) : 0;
2033 for (ix = 0; ix != len; ix++)
2035 tree parm;
2037 if (TREE_VEC_ELT (parms, ix) == error_mark_node)
2039 pp_string (pp, M_("<template parameter error>"));
2040 continue;
2043 parm = TREE_VALUE (TREE_VEC_ELT (parms, ix));
2045 if (ix)
2046 pp_separate_with_comma (pp);
2048 dump_decl (pp, parm, flags & ~TFF_DECL_SPECIFIERS);
2051 pp_cxx_end_template_argument_list (pp);
2054 /* Print out the arguments of CALL_EXPR T as a parenthesized list using
2055 flags FLAGS. Skip over the first argument if SKIPFIRST is true. */
2057 static void
2058 dump_call_expr_args (cxx_pretty_printer *pp, tree t, int flags, bool skipfirst)
2060 tree arg;
2061 call_expr_arg_iterator iter;
2063 pp_cxx_left_paren (pp);
2064 FOR_EACH_CALL_EXPR_ARG (arg, iter, t)
2066 if (skipfirst)
2067 skipfirst = false;
2068 else
2070 dump_expr (pp, arg, flags | TFF_EXPR_IN_PARENS);
2071 if (more_call_expr_args_p (&iter))
2072 pp_separate_with_comma (pp);
2075 pp_cxx_right_paren (pp);
2078 /* Print out the arguments of AGGR_INIT_EXPR T as a parenthesized list
2079 using flags FLAGS. Skip over the first argument if SKIPFIRST is
2080 true. */
2082 static void
2083 dump_aggr_init_expr_args (cxx_pretty_printer *pp, tree t, int flags,
2084 bool skipfirst)
2086 tree arg;
2087 aggr_init_expr_arg_iterator iter;
2089 pp_cxx_left_paren (pp);
2090 FOR_EACH_AGGR_INIT_EXPR_ARG (arg, iter, t)
2092 if (skipfirst)
2093 skipfirst = false;
2094 else
2096 dump_expr (pp, arg, flags | TFF_EXPR_IN_PARENS);
2097 if (more_aggr_init_expr_args_p (&iter))
2098 pp_separate_with_comma (pp);
2101 pp_cxx_right_paren (pp);
2104 /* Print out a list of initializers (subr of dump_expr). */
2106 static void
2107 dump_expr_list (cxx_pretty_printer *pp, tree l, int flags)
2109 while (l)
2111 dump_expr (pp, TREE_VALUE (l), flags | TFF_EXPR_IN_PARENS);
2112 l = TREE_CHAIN (l);
2113 if (l)
2114 pp_separate_with_comma (pp);
2118 /* Print out a vector of initializers (subr of dump_expr). */
2120 static void
2121 dump_expr_init_vec (cxx_pretty_printer *pp, vec<constructor_elt, va_gc> *v,
2122 int flags)
2124 unsigned HOST_WIDE_INT idx;
2125 tree value;
2127 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
2129 dump_expr (pp, value, flags | TFF_EXPR_IN_PARENS);
2130 if (idx != v->length () - 1)
2131 pp_separate_with_comma (pp);
2136 /* We've gotten an indirect REFERENCE (an OBJ_TYPE_REF) to a virtual
2137 function. Resolve it to a close relative -- in the sense of static
2138 type -- variant being overridden. That is close to what was written in
2139 the source code. Subroutine of dump_expr. */
2141 static tree
2142 resolve_virtual_fun_from_obj_type_ref (tree ref)
2144 tree obj_type = TREE_TYPE (OBJ_TYPE_REF_OBJECT (ref));
2145 HOST_WIDE_INT index = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (ref));
2146 tree fun = BINFO_VIRTUALS (TYPE_BINFO (TREE_TYPE (obj_type)));
2147 while (index)
2149 fun = TREE_CHAIN (fun);
2150 index -= (TARGET_VTABLE_USES_DESCRIPTORS
2151 ? TARGET_VTABLE_USES_DESCRIPTORS : 1);
2154 return BV_FN (fun);
2157 /* Print out an expression E under control of FLAGS. */
2159 static void
2160 dump_expr (cxx_pretty_printer *pp, tree t, int flags)
2162 tree op;
2164 if (t == 0)
2165 return;
2167 if (STATEMENT_CLASS_P (t))
2169 pp_cxx_ws_string (pp, M_("<statement>"));
2170 return;
2173 switch (TREE_CODE (t))
2175 case VAR_DECL:
2176 case PARM_DECL:
2177 case FIELD_DECL:
2178 case CONST_DECL:
2179 case FUNCTION_DECL:
2180 case TEMPLATE_DECL:
2181 case NAMESPACE_DECL:
2182 case LABEL_DECL:
2183 case WILDCARD_DECL:
2184 case OVERLOAD:
2185 case TYPE_DECL:
2186 case IDENTIFIER_NODE:
2187 dump_decl (pp, t, ((flags & ~(TFF_DECL_SPECIFIERS|TFF_RETURN_TYPE
2188 |TFF_TEMPLATE_HEADER))
2189 | TFF_NO_TEMPLATE_BINDINGS
2190 | TFF_NO_FUNCTION_ARGUMENTS));
2191 break;
2193 case SSA_NAME:
2194 if (SSA_NAME_VAR (t)
2195 && !DECL_ARTIFICIAL (SSA_NAME_VAR (t)))
2196 dump_expr (pp, SSA_NAME_VAR (t), flags);
2197 else
2198 pp_cxx_ws_string (pp, M_("<unknown>"));
2199 break;
2201 case VOID_CST:
2202 case INTEGER_CST:
2203 case REAL_CST:
2204 case STRING_CST:
2205 case COMPLEX_CST:
2206 pp->constant (t);
2207 break;
2209 case USERDEF_LITERAL:
2210 pp_cxx_userdef_literal (pp, t);
2211 break;
2213 case THROW_EXPR:
2214 /* While waiting for caret diagnostics, avoid printing
2215 __cxa_allocate_exception, __cxa_throw, and the like. */
2216 pp_cxx_ws_string (pp, M_("<throw-expression>"));
2217 break;
2219 case PTRMEM_CST:
2220 pp_ampersand (pp);
2221 dump_type (pp, PTRMEM_CST_CLASS (t), flags);
2222 pp_cxx_colon_colon (pp);
2223 pp_cxx_tree_identifier (pp, DECL_NAME (PTRMEM_CST_MEMBER (t)));
2224 break;
2226 case COMPOUND_EXPR:
2227 pp_cxx_left_paren (pp);
2228 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2229 pp_separate_with_comma (pp);
2230 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2231 pp_cxx_right_paren (pp);
2232 break;
2234 case COND_EXPR:
2235 case VEC_COND_EXPR:
2236 pp_cxx_left_paren (pp);
2237 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2238 pp_string (pp, " ? ");
2239 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2240 pp_string (pp, " : ");
2241 dump_expr (pp, TREE_OPERAND (t, 2), flags | TFF_EXPR_IN_PARENS);
2242 pp_cxx_right_paren (pp);
2243 break;
2245 case SAVE_EXPR:
2246 if (TREE_HAS_CONSTRUCTOR (t))
2248 pp_cxx_ws_string (pp, "new");
2249 pp_cxx_whitespace (pp);
2250 dump_type (pp, TREE_TYPE (TREE_TYPE (t)), flags);
2252 else
2253 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2254 break;
2256 case AGGR_INIT_EXPR:
2258 tree fn = NULL_TREE;
2260 if (TREE_CODE (AGGR_INIT_EXPR_FN (t)) == ADDR_EXPR)
2261 fn = TREE_OPERAND (AGGR_INIT_EXPR_FN (t), 0);
2263 if (fn && TREE_CODE (fn) == FUNCTION_DECL)
2265 if (DECL_CONSTRUCTOR_P (fn))
2266 dump_type (pp, DECL_CONTEXT (fn), flags);
2267 else
2268 dump_decl (pp, fn, 0);
2270 else
2271 dump_expr (pp, AGGR_INIT_EXPR_FN (t), 0);
2273 dump_aggr_init_expr_args (pp, t, flags, true);
2274 break;
2276 case CALL_EXPR:
2278 tree fn = CALL_EXPR_FN (t);
2279 bool skipfirst = false;
2281 /* Deal with internal functions. */
2282 if (fn == NULL_TREE)
2284 pp_string (pp, internal_fn_name (CALL_EXPR_IFN (t)));
2285 dump_call_expr_args (pp, t, flags, skipfirst);
2286 break;
2289 if (TREE_CODE (fn) == ADDR_EXPR)
2290 fn = TREE_OPERAND (fn, 0);
2292 /* Nobody is interested in seeing the guts of vcalls. */
2293 if (TREE_CODE (fn) == OBJ_TYPE_REF)
2294 fn = resolve_virtual_fun_from_obj_type_ref (fn);
2296 if (TREE_TYPE (fn) != NULL_TREE
2297 && NEXT_CODE (fn) == METHOD_TYPE
2298 && call_expr_nargs (t))
2300 tree ob = CALL_EXPR_ARG (t, 0);
2301 if (TREE_CODE (ob) == ADDR_EXPR)
2303 dump_expr (pp, TREE_OPERAND (ob, 0),
2304 flags | TFF_EXPR_IN_PARENS);
2305 pp_cxx_dot (pp);
2307 else if (!is_this_parameter (ob))
2309 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2310 pp_cxx_arrow (pp);
2312 skipfirst = true;
2314 if (flag_sanitize & SANITIZE_UNDEFINED
2315 && is_ubsan_builtin_p (fn))
2317 pp_string (cxx_pp, M_("<ubsan routine call>"));
2318 break;
2320 dump_expr (pp, fn, flags | TFF_EXPR_IN_PARENS);
2321 dump_call_expr_args (pp, t, flags, skipfirst);
2323 break;
2325 case TARGET_EXPR:
2326 /* Note that this only works for G++ target exprs. If somebody
2327 builds a general TARGET_EXPR, there's no way to represent that
2328 it initializes anything other that the parameter slot for the
2329 default argument. Note we may have cleared out the first
2330 operand in expand_expr, so don't go killing ourselves. */
2331 if (TREE_OPERAND (t, 1))
2332 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2333 break;
2335 case POINTER_PLUS_EXPR:
2336 dump_binary_op (pp, "+", t, flags);
2337 break;
2339 case POINTER_DIFF_EXPR:
2340 dump_binary_op (pp, "-", t, flags);
2341 break;
2343 case INIT_EXPR:
2344 case MODIFY_EXPR:
2345 dump_binary_op (pp, OVL_OP_INFO (true, NOP_EXPR)->name, t, flags);
2346 break;
2348 case PLUS_EXPR:
2349 case MINUS_EXPR:
2350 case MULT_EXPR:
2351 case TRUNC_DIV_EXPR:
2352 case TRUNC_MOD_EXPR:
2353 case MIN_EXPR:
2354 case MAX_EXPR:
2355 case LSHIFT_EXPR:
2356 case RSHIFT_EXPR:
2357 case BIT_IOR_EXPR:
2358 case BIT_XOR_EXPR:
2359 case BIT_AND_EXPR:
2360 case TRUTH_ANDIF_EXPR:
2361 case TRUTH_ORIF_EXPR:
2362 case LT_EXPR:
2363 case LE_EXPR:
2364 case GT_EXPR:
2365 case GE_EXPR:
2366 case EQ_EXPR:
2367 case NE_EXPR:
2368 case SPACESHIP_EXPR:
2369 case EXACT_DIV_EXPR:
2370 dump_binary_op (pp, OVL_OP_INFO (false, TREE_CODE (t))->name, t, flags);
2371 break;
2373 case CEIL_DIV_EXPR:
2374 case FLOOR_DIV_EXPR:
2375 case ROUND_DIV_EXPR:
2376 case RDIV_EXPR:
2377 dump_binary_op (pp, "/", t, flags);
2378 break;
2380 case CEIL_MOD_EXPR:
2381 case FLOOR_MOD_EXPR:
2382 case ROUND_MOD_EXPR:
2383 dump_binary_op (pp, "%", t, flags);
2384 break;
2386 case COMPONENT_REF:
2388 tree ob = TREE_OPERAND (t, 0);
2389 if (INDIRECT_REF_P (ob))
2391 ob = TREE_OPERAND (ob, 0);
2392 if (!is_this_parameter (ob)
2393 && !is_dummy_object (ob))
2395 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2396 if (TYPE_REF_P (TREE_TYPE (ob)))
2397 pp_cxx_dot (pp);
2398 else
2399 pp_cxx_arrow (pp);
2402 else
2404 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2405 if (TREE_CODE (ob) != ARROW_EXPR)
2406 pp_cxx_dot (pp);
2408 dump_expr (pp, TREE_OPERAND (t, 1), flags & ~TFF_EXPR_IN_PARENS);
2410 break;
2412 case ARRAY_REF:
2413 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2414 pp_cxx_left_bracket (pp);
2415 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2416 pp_cxx_right_bracket (pp);
2417 break;
2419 case UNARY_PLUS_EXPR:
2420 dump_unary_op (pp, "+", t, flags);
2421 break;
2423 case ADDR_EXPR:
2424 if (TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
2425 || TREE_CODE (TREE_OPERAND (t, 0)) == STRING_CST
2426 /* An ADDR_EXPR can have reference type. In that case, we
2427 shouldn't print the `&' doing so indicates to the user
2428 that the expression has pointer type. */
2429 || (TREE_TYPE (t)
2430 && TYPE_REF_P (TREE_TYPE (t))))
2431 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2432 else if (TREE_CODE (TREE_OPERAND (t, 0)) == LABEL_DECL)
2433 dump_unary_op (pp, "&&", t, flags);
2434 else
2435 dump_unary_op (pp, "&", t, flags);
2436 break;
2438 case INDIRECT_REF:
2439 if (TREE_HAS_CONSTRUCTOR (t))
2441 t = TREE_OPERAND (t, 0);
2442 gcc_assert (TREE_CODE (t) == CALL_EXPR);
2443 dump_expr (pp, CALL_EXPR_FN (t), flags | TFF_EXPR_IN_PARENS);
2444 dump_call_expr_args (pp, t, flags, true);
2446 else
2448 if (TREE_OPERAND (t,0) != NULL_TREE
2449 && TREE_TYPE (TREE_OPERAND (t, 0))
2450 && NEXT_CODE (TREE_OPERAND (t, 0)) == REFERENCE_TYPE)
2451 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2452 else
2453 dump_unary_op (pp, "*", t, flags);
2455 break;
2457 case MEM_REF:
2458 /* Delegate to the base "C" pretty printer. */
2459 pp->c_pretty_printer::unary_expression (t);
2460 break;
2462 case TARGET_MEM_REF:
2463 /* TARGET_MEM_REF can't appear directly from source, but can appear
2464 during late GIMPLE optimizations and through late diagnostic we might
2465 need to support it. Print it as dereferencing of a pointer after
2466 cast to the TARGET_MEM_REF type, with pointer arithmetics on some
2467 pointer to single byte types, so
2468 *(type *)((char *) ptr + step * index + index2) if all the operands
2469 are present and the casts are needed. */
2470 pp_cxx_star (pp);
2471 pp_cxx_left_paren (pp);
2472 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (TMR_BASE (t)))) == NULL_TREE
2473 || !integer_onep (TYPE_SIZE_UNIT
2474 (TREE_TYPE (TREE_TYPE (TMR_BASE (t))))))
2476 if (TYPE_SIZE_UNIT (TREE_TYPE (t))
2477 && integer_onep (TYPE_SIZE_UNIT (TREE_TYPE (t))))
2479 pp_cxx_left_paren (pp);
2480 dump_type (pp, build_pointer_type (TREE_TYPE (t)), flags);
2482 else
2484 dump_type (pp, build_pointer_type (TREE_TYPE (t)), flags);
2485 pp_cxx_right_paren (pp);
2486 pp_cxx_left_paren (pp);
2487 pp_cxx_left_paren (pp);
2488 dump_type (pp, build_pointer_type (char_type_node), flags);
2490 pp_cxx_right_paren (pp);
2492 else if (!same_type_p (TREE_TYPE (t),
2493 TREE_TYPE (TREE_TYPE (TMR_BASE (t)))))
2495 dump_type (pp, build_pointer_type (TREE_TYPE (t)), flags);
2496 pp_cxx_right_paren (pp);
2497 pp_cxx_left_paren (pp);
2499 dump_expr (pp, TMR_BASE (t), flags);
2500 if (TMR_STEP (t) && TMR_INDEX (t))
2502 pp_cxx_ws_string (pp, "+");
2503 dump_expr (pp, TMR_INDEX (t), flags);
2504 pp_cxx_ws_string (pp, "*");
2505 dump_expr (pp, TMR_STEP (t), flags);
2507 if (TMR_INDEX2 (t))
2509 pp_cxx_ws_string (pp, "+");
2510 dump_expr (pp, TMR_INDEX2 (t), flags);
2512 if (!integer_zerop (TMR_OFFSET (t)))
2514 pp_cxx_ws_string (pp, "+");
2515 dump_expr (pp, fold_convert (ssizetype, TMR_OFFSET (t)), flags);
2517 pp_cxx_right_paren (pp);
2518 break;
2520 case NEGATE_EXPR:
2521 case BIT_NOT_EXPR:
2522 case TRUTH_NOT_EXPR:
2523 case PREDECREMENT_EXPR:
2524 case PREINCREMENT_EXPR:
2525 dump_unary_op (pp, OVL_OP_INFO (false, TREE_CODE (t))->name, t, flags);
2526 break;
2528 case POSTDECREMENT_EXPR:
2529 case POSTINCREMENT_EXPR:
2530 pp_cxx_left_paren (pp);
2531 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2532 pp_cxx_ws_string (pp, OVL_OP_INFO (false, TREE_CODE (t))->name);
2533 pp_cxx_right_paren (pp);
2534 break;
2536 case NON_LVALUE_EXPR:
2537 /* FIXME: This is a KLUDGE workaround for a parsing problem. There
2538 should be another level of INDIRECT_REF so that I don't have to do
2539 this. */
2540 if (TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == POINTER_TYPE)
2542 tree next = TREE_TYPE (TREE_TYPE (t));
2544 while (TYPE_PTR_P (next))
2545 next = TREE_TYPE (next);
2547 if (TREE_CODE (next) == FUNCTION_TYPE)
2549 if (flags & TFF_EXPR_IN_PARENS)
2550 pp_cxx_left_paren (pp);
2551 pp_cxx_star (pp);
2552 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2553 if (flags & TFF_EXPR_IN_PARENS)
2554 pp_cxx_right_paren (pp);
2555 break;
2557 /* Else fall through. */
2559 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2560 break;
2562 CASE_CONVERT:
2563 case IMPLICIT_CONV_EXPR:
2564 case VIEW_CONVERT_EXPR:
2566 tree op = TREE_OPERAND (t, 0);
2567 tree ttype = TREE_TYPE (t);
2568 tree optype = TREE_TYPE (op);
2570 if (TREE_CODE (ttype) != TREE_CODE (optype)
2571 && INDIRECT_TYPE_P (ttype)
2572 && INDIRECT_TYPE_P (optype)
2573 && same_type_p (TREE_TYPE (optype),
2574 TREE_TYPE (ttype)))
2576 if (TYPE_REF_P (ttype))
2578 STRIP_NOPS (op);
2579 if (TREE_CODE (op) == ADDR_EXPR)
2580 dump_expr (pp, TREE_OPERAND (op, 0), flags);
2581 else
2582 dump_unary_op (pp, "*", t, flags);
2584 else
2585 dump_unary_op (pp, "&", t, flags);
2587 else if (!same_type_p (TREE_TYPE (op), TREE_TYPE (t)))
2589 /* It is a cast, but we cannot tell whether it is a
2590 reinterpret or static cast. Use the C style notation. */
2591 if (flags & TFF_EXPR_IN_PARENS)
2592 pp_cxx_left_paren (pp);
2593 pp_cxx_left_paren (pp);
2594 dump_type (pp, TREE_TYPE (t), flags);
2595 pp_cxx_right_paren (pp);
2596 dump_expr (pp, op, flags | TFF_EXPR_IN_PARENS);
2597 if (flags & TFF_EXPR_IN_PARENS)
2598 pp_cxx_right_paren (pp);
2600 else
2601 dump_expr (pp, op, flags);
2602 break;
2605 case CONSTRUCTOR:
2606 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
2608 tree idx = build_ptrmemfunc_access_expr (t, pfn_identifier);
2610 if (integer_zerop (idx))
2612 /* A NULL pointer-to-member constant. */
2613 pp_cxx_left_paren (pp);
2614 pp_cxx_left_paren (pp);
2615 dump_type (pp, TREE_TYPE (t), flags);
2616 pp_cxx_right_paren (pp);
2617 pp_character (pp, '0');
2618 pp_cxx_right_paren (pp);
2619 break;
2621 else if (tree_fits_shwi_p (idx))
2623 tree virtuals;
2624 unsigned HOST_WIDE_INT n;
2626 t = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
2627 t = TYPE_METHOD_BASETYPE (t);
2628 virtuals = BINFO_VIRTUALS (TYPE_BINFO (TYPE_MAIN_VARIANT (t)));
2630 n = tree_to_shwi (idx);
2632 /* Map vtable index back one, to allow for the null pointer to
2633 member. */
2634 --n;
2636 while (n > 0 && virtuals)
2638 --n;
2639 virtuals = TREE_CHAIN (virtuals);
2641 if (virtuals)
2643 dump_expr (pp, BV_FN (virtuals),
2644 flags | TFF_EXPR_IN_PARENS);
2645 break;
2649 if (TREE_TYPE (t) && LAMBDA_TYPE_P (TREE_TYPE (t)))
2650 pp_string (pp, "<lambda closure object>");
2651 if (TREE_TYPE (t) && EMPTY_CONSTRUCTOR_P (t))
2653 dump_type (pp, TREE_TYPE (t), 0);
2654 pp_cxx_left_paren (pp);
2655 pp_cxx_right_paren (pp);
2657 else
2659 if (!BRACE_ENCLOSED_INITIALIZER_P (t))
2660 dump_type (pp, TREE_TYPE (t), 0);
2661 pp_cxx_left_brace (pp);
2662 dump_expr_init_vec (pp, CONSTRUCTOR_ELTS (t), flags);
2663 pp_cxx_right_brace (pp);
2666 break;
2668 case OFFSET_REF:
2670 tree ob = TREE_OPERAND (t, 0);
2671 if (is_dummy_object (ob))
2673 t = TREE_OPERAND (t, 1);
2674 if (TREE_CODE (t) == FUNCTION_DECL)
2675 /* A::f */
2676 dump_expr (pp, t, flags | TFF_EXPR_IN_PARENS);
2677 else if (BASELINK_P (t))
2678 dump_expr (pp, OVL_FIRST (BASELINK_FUNCTIONS (t)),
2679 flags | TFF_EXPR_IN_PARENS);
2680 else
2681 dump_decl (pp, t, flags);
2683 else
2685 if (INDIRECT_REF_P (ob))
2687 dump_expr (pp, TREE_OPERAND (ob, 0), flags | TFF_EXPR_IN_PARENS);
2688 pp_cxx_arrow (pp);
2689 pp_cxx_star (pp);
2691 else
2693 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2694 pp_cxx_dot (pp);
2695 pp_cxx_star (pp);
2697 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2699 break;
2702 case TEMPLATE_PARM_INDEX:
2703 dump_decl (pp, TEMPLATE_PARM_DECL (t), flags & ~TFF_DECL_SPECIFIERS);
2704 break;
2706 case CAST_EXPR:
2707 if (TREE_OPERAND (t, 0) == NULL_TREE
2708 || TREE_CHAIN (TREE_OPERAND (t, 0)))
2710 dump_type (pp, TREE_TYPE (t), flags);
2711 pp_cxx_left_paren (pp);
2712 dump_expr_list (pp, TREE_OPERAND (t, 0), flags);
2713 pp_cxx_right_paren (pp);
2715 else
2717 pp_cxx_left_paren (pp);
2718 dump_type (pp, TREE_TYPE (t), flags);
2719 pp_cxx_right_paren (pp);
2720 pp_cxx_left_paren (pp);
2721 dump_expr_list (pp, TREE_OPERAND (t, 0), flags);
2722 pp_cxx_right_paren (pp);
2724 break;
2726 case STATIC_CAST_EXPR:
2727 pp_cxx_ws_string (pp, "static_cast");
2728 goto cast;
2729 case REINTERPRET_CAST_EXPR:
2730 pp_cxx_ws_string (pp, "reinterpret_cast");
2731 goto cast;
2732 case CONST_CAST_EXPR:
2733 pp_cxx_ws_string (pp, "const_cast");
2734 goto cast;
2735 case DYNAMIC_CAST_EXPR:
2736 pp_cxx_ws_string (pp, "dynamic_cast");
2737 cast:
2738 pp_cxx_begin_template_argument_list (pp);
2739 dump_type (pp, TREE_TYPE (t), flags);
2740 pp_cxx_end_template_argument_list (pp);
2741 pp_cxx_left_paren (pp);
2742 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2743 pp_cxx_right_paren (pp);
2744 break;
2746 case ARROW_EXPR:
2747 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2748 pp_cxx_arrow (pp);
2749 break;
2751 case SIZEOF_EXPR:
2752 case ALIGNOF_EXPR:
2753 if (TREE_CODE (t) == SIZEOF_EXPR)
2754 pp_cxx_ws_string (pp, "sizeof");
2755 else
2757 gcc_assert (TREE_CODE (t) == ALIGNOF_EXPR);
2758 pp_cxx_ws_string (pp, "__alignof__");
2760 op = TREE_OPERAND (t, 0);
2761 if (PACK_EXPANSION_P (op))
2763 pp_string (pp, "...");
2764 op = PACK_EXPANSION_PATTERN (op);
2766 pp_cxx_whitespace (pp);
2767 pp_cxx_left_paren (pp);
2768 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
2769 dump_type (pp, TREE_TYPE (op), flags);
2770 else if (TYPE_P (TREE_OPERAND (t, 0)))
2771 dump_type (pp, op, flags);
2772 else
2773 dump_expr (pp, op, flags);
2774 pp_cxx_right_paren (pp);
2775 break;
2777 case AT_ENCODE_EXPR:
2778 pp_cxx_ws_string (pp, "@encode");
2779 pp_cxx_whitespace (pp);
2780 pp_cxx_left_paren (pp);
2781 dump_type (pp, TREE_OPERAND (t, 0), flags);
2782 pp_cxx_right_paren (pp);
2783 break;
2785 case NOEXCEPT_EXPR:
2786 pp_cxx_ws_string (pp, "noexcept");
2787 pp_cxx_whitespace (pp);
2788 pp_cxx_left_paren (pp);
2789 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2790 pp_cxx_right_paren (pp);
2791 break;
2793 case REALPART_EXPR:
2794 case IMAGPART_EXPR:
2795 pp_cxx_ws_string (pp, OVL_OP_INFO (false, TREE_CODE (t))->name);
2796 pp_cxx_whitespace (pp);
2797 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2798 break;
2800 case DEFERRED_PARSE:
2801 pp_string (pp, M_("<unparsed>"));
2802 break;
2804 case TRY_CATCH_EXPR:
2805 case CLEANUP_POINT_EXPR:
2806 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2807 break;
2809 case PSEUDO_DTOR_EXPR:
2810 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2811 pp_cxx_dot (pp);
2812 if (TREE_OPERAND (t, 1))
2814 dump_type (pp, TREE_OPERAND (t, 1), flags);
2815 pp_cxx_colon_colon (pp);
2817 pp_cxx_complement (pp);
2818 dump_type (pp, TREE_OPERAND (t, 2), flags);
2819 break;
2821 case TEMPLATE_ID_EXPR:
2822 dump_decl (pp, t, flags);
2823 break;
2825 case BIND_EXPR:
2826 case STMT_EXPR:
2827 case EXPR_STMT:
2828 case STATEMENT_LIST:
2829 /* We don't yet have a way of dumping statements in a
2830 human-readable format. */
2831 pp_string (pp, "({...})");
2832 break;
2834 case LOOP_EXPR:
2835 pp_string (pp, "while (1) { ");
2836 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2837 pp_cxx_right_brace (pp);
2838 break;
2840 case EXIT_EXPR:
2841 pp_string (pp, "if (");
2842 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2843 pp_string (pp, ") break; ");
2844 break;
2846 case BASELINK:
2847 dump_expr (pp, BASELINK_FUNCTIONS (t), flags & ~TFF_EXPR_IN_PARENS);
2848 break;
2850 case EMPTY_CLASS_EXPR:
2851 dump_type (pp, TREE_TYPE (t), flags);
2852 pp_cxx_left_paren (pp);
2853 pp_cxx_right_paren (pp);
2854 break;
2856 case NON_DEPENDENT_EXPR:
2857 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2858 break;
2860 case ARGUMENT_PACK_SELECT:
2861 dump_template_argument (pp, ARGUMENT_PACK_SELECT_FROM_PACK (t), flags);
2862 break;
2864 case RECORD_TYPE:
2865 case UNION_TYPE:
2866 case ENUMERAL_TYPE:
2867 case REAL_TYPE:
2868 case VOID_TYPE:
2869 case OPAQUE_TYPE:
2870 case BOOLEAN_TYPE:
2871 case INTEGER_TYPE:
2872 case COMPLEX_TYPE:
2873 case VECTOR_TYPE:
2874 case DECLTYPE_TYPE:
2875 pp_type_specifier_seq (pp, t);
2876 break;
2878 case TYPENAME_TYPE:
2879 /* We get here when we want to print a dependent type as an
2880 id-expression, without any disambiguator decoration. */
2881 pp->id_expression (t);
2882 break;
2884 case TEMPLATE_TYPE_PARM:
2885 case TEMPLATE_TEMPLATE_PARM:
2886 case BOUND_TEMPLATE_TEMPLATE_PARM:
2887 dump_type (pp, t, flags);
2888 break;
2890 case TRAIT_EXPR:
2891 pp_cxx_trait_expression (pp, t);
2892 break;
2894 case VA_ARG_EXPR:
2895 pp_cxx_va_arg_expression (pp, t);
2896 break;
2898 case OFFSETOF_EXPR:
2899 pp_cxx_offsetof_expression (pp, t);
2900 break;
2902 case ADDRESSOF_EXPR:
2903 pp_cxx_addressof_expression (pp, t);
2904 break;
2906 case SCOPE_REF:
2907 dump_decl (pp, t, flags);
2908 break;
2910 case EXPR_PACK_EXPANSION:
2911 case UNARY_LEFT_FOLD_EXPR:
2912 case UNARY_RIGHT_FOLD_EXPR:
2913 case BINARY_LEFT_FOLD_EXPR:
2914 case BINARY_RIGHT_FOLD_EXPR:
2915 case TYPEID_EXPR:
2916 case MEMBER_REF:
2917 case DOTSTAR_EXPR:
2918 case NEW_EXPR:
2919 case VEC_NEW_EXPR:
2920 case DELETE_EXPR:
2921 case VEC_DELETE_EXPR:
2922 case MODOP_EXPR:
2923 case ABS_EXPR:
2924 case ABSU_EXPR:
2925 case CONJ_EXPR:
2926 case VECTOR_CST:
2927 case FIXED_CST:
2928 case UNORDERED_EXPR:
2929 case ORDERED_EXPR:
2930 case UNLT_EXPR:
2931 case UNLE_EXPR:
2932 case UNGT_EXPR:
2933 case UNGE_EXPR:
2934 case UNEQ_EXPR:
2935 case LTGT_EXPR:
2936 case COMPLEX_EXPR:
2937 case BIT_FIELD_REF:
2938 case FIX_TRUNC_EXPR:
2939 case FLOAT_EXPR:
2940 pp->expression (t);
2941 break;
2943 case TRUTH_AND_EXPR:
2944 case TRUTH_OR_EXPR:
2945 case TRUTH_XOR_EXPR:
2946 if (flags & TFF_EXPR_IN_PARENS)
2947 pp_cxx_left_paren (pp);
2948 pp->expression (t);
2949 if (flags & TFF_EXPR_IN_PARENS)
2950 pp_cxx_right_paren (pp);
2951 break;
2953 case OBJ_TYPE_REF:
2954 dump_expr (pp, resolve_virtual_fun_from_obj_type_ref (t), flags);
2955 break;
2957 case LAMBDA_EXPR:
2958 pp_string (pp, M_("<lambda>"));
2959 break;
2961 case PAREN_EXPR:
2962 pp_cxx_left_paren (pp);
2963 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2964 pp_cxx_right_paren (pp);
2965 break;
2967 case REQUIRES_EXPR:
2968 pp_cxx_requires_expr (cxx_pp, t);
2969 break;
2971 case SIMPLE_REQ:
2972 pp_cxx_simple_requirement (cxx_pp, t);
2973 break;
2975 case TYPE_REQ:
2976 pp_cxx_type_requirement (cxx_pp, t);
2977 break;
2979 case COMPOUND_REQ:
2980 pp_cxx_compound_requirement (cxx_pp, t);
2981 break;
2983 case NESTED_REQ:
2984 pp_cxx_nested_requirement (cxx_pp, t);
2985 break;
2987 case ATOMIC_CONSTR:
2988 case CHECK_CONSTR:
2989 case CONJ_CONSTR:
2990 case DISJ_CONSTR:
2992 pp_cxx_constraint (cxx_pp, t);
2993 break;
2996 case PLACEHOLDER_EXPR:
2997 pp_string (pp, M_("*this"));
2998 break;
3000 case TREE_LIST:
3001 dump_expr_list (pp, t, flags);
3002 break;
3004 /* This list is incomplete, but should suffice for now.
3005 It is very important that `sorry' does not call
3006 `report_error_function'. That could cause an infinite loop. */
3007 default:
3008 pp_unsupported_tree (pp, t);
3009 /* Fall through. */
3010 case ERROR_MARK:
3011 pp_string (pp, M_("<expression error>"));
3012 break;
3016 static void
3017 dump_binary_op (cxx_pretty_printer *pp, const char *opstring, tree t,
3018 int flags)
3020 pp_cxx_left_paren (pp);
3021 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
3022 pp_cxx_whitespace (pp);
3023 if (opstring)
3024 pp_cxx_ws_string (pp, opstring);
3025 else
3026 pp_string (pp, M_("<unknown operator>"));
3027 pp_cxx_whitespace (pp);
3028 tree op1 = TREE_OPERAND (t, 1);
3029 if (TREE_CODE (t) == POINTER_PLUS_EXPR
3030 && TREE_CODE (op1) == INTEGER_CST
3031 && tree_int_cst_sign_bit (op1))
3032 /* A pointer minus an integer is represented internally as plus a very
3033 large number, don't expose that to users. */
3034 op1 = convert (ssizetype, op1);
3035 dump_expr (pp, op1, flags | TFF_EXPR_IN_PARENS);
3036 pp_cxx_right_paren (pp);
3039 static void
3040 dump_unary_op (cxx_pretty_printer *pp, const char *opstring, tree t, int flags)
3042 if (flags & TFF_EXPR_IN_PARENS)
3043 pp_cxx_left_paren (pp);
3044 pp_cxx_ws_string (pp, opstring);
3045 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
3046 if (flags & TFF_EXPR_IN_PARENS)
3047 pp_cxx_right_paren (pp);
3050 static void
3051 reinit_cxx_pp (void)
3053 pp_clear_output_area (cxx_pp);
3054 cxx_pp->padding = pp_none;
3055 pp_indentation (cxx_pp) = 0;
3056 pp_needs_newline (cxx_pp) = false;
3057 cxx_pp->enclosing_scope = current_function_decl;
3060 /* Same as pp_formatted_text, except the return string is a separate
3061 copy and has a GGC storage duration, e.g. an indefinite lifetime. */
3063 inline const char *
3064 pp_ggc_formatted_text (pretty_printer *pp)
3066 return ggc_strdup (pp_formatted_text (pp));
3069 /* Exported interface to stringifying types, exprs and decls under TFF_*
3070 control. */
3072 const char *
3073 type_as_string (tree typ, int flags)
3075 reinit_cxx_pp ();
3076 pp_translate_identifiers (cxx_pp) = false;
3077 dump_type (cxx_pp, typ, flags);
3078 return pp_ggc_formatted_text (cxx_pp);
3081 const char *
3082 type_as_string_translate (tree typ, int flags)
3084 reinit_cxx_pp ();
3085 dump_type (cxx_pp, typ, flags);
3086 return pp_ggc_formatted_text (cxx_pp);
3089 const char *
3090 expr_as_string (tree decl, int flags)
3092 reinit_cxx_pp ();
3093 pp_translate_identifiers (cxx_pp) = false;
3094 dump_expr (cxx_pp, decl, flags);
3095 return pp_ggc_formatted_text (cxx_pp);
3098 /* Wrap decl_as_string with options appropriate for dwarf. */
3100 const char *
3101 decl_as_dwarf_string (tree decl, int flags)
3103 const char *name;
3104 /* Curiously, reinit_cxx_pp doesn't reset the flags field, so setting the flag
3105 here will be adequate to get the desired behavior. */
3106 cxx_pp->flags |= pp_c_flag_gnu_v3;
3107 name = decl_as_string (decl, flags);
3108 /* Subsequent calls to the pretty printer shouldn't use this style. */
3109 cxx_pp->flags &= ~pp_c_flag_gnu_v3;
3110 return name;
3113 const char *
3114 decl_as_string (tree decl, int flags)
3116 reinit_cxx_pp ();
3117 pp_translate_identifiers (cxx_pp) = false;
3118 dump_decl (cxx_pp, decl, flags);
3119 return pp_ggc_formatted_text (cxx_pp);
3122 const char *
3123 decl_as_string_translate (tree decl, int flags)
3125 reinit_cxx_pp ();
3126 dump_decl (cxx_pp, decl, flags);
3127 return pp_ggc_formatted_text (cxx_pp);
3130 /* Wrap lang_decl_name with options appropriate for dwarf. */
3132 const char *
3133 lang_decl_dwarf_name (tree decl, int v, bool translate)
3135 const char *name;
3136 /* Curiously, reinit_cxx_pp doesn't reset the flags field, so setting the flag
3137 here will be adequate to get the desired behavior. */
3138 cxx_pp->flags |= pp_c_flag_gnu_v3;
3139 name = lang_decl_name (decl, v, translate);
3140 /* Subsequent calls to the pretty printer shouldn't use this style. */
3141 cxx_pp->flags &= ~pp_c_flag_gnu_v3;
3142 return name;
3145 /* Generate the three forms of printable names for cxx_printable_name. */
3147 const char *
3148 lang_decl_name (tree decl, int v, bool translate)
3150 if (v >= 2)
3151 return (translate
3152 ? decl_as_string_translate (decl, TFF_DECL_SPECIFIERS)
3153 : decl_as_string (decl, TFF_DECL_SPECIFIERS));
3155 reinit_cxx_pp ();
3156 pp_translate_identifiers (cxx_pp) = translate;
3157 if (v == 1
3158 && (DECL_CLASS_SCOPE_P (decl)
3159 || (DECL_NAMESPACE_SCOPE_P (decl)
3160 && CP_DECL_CONTEXT (decl) != global_namespace)))
3162 dump_type (cxx_pp, CP_DECL_CONTEXT (decl), TFF_PLAIN_IDENTIFIER);
3163 pp_cxx_colon_colon (cxx_pp);
3166 if (TREE_CODE (decl) == FUNCTION_DECL)
3167 dump_function_name (cxx_pp, decl, TFF_PLAIN_IDENTIFIER);
3168 else if ((DECL_NAME (decl) == NULL_TREE)
3169 && TREE_CODE (decl) == NAMESPACE_DECL)
3170 dump_decl (cxx_pp, decl, TFF_PLAIN_IDENTIFIER | TFF_UNQUALIFIED_NAME);
3171 else
3172 dump_decl (cxx_pp, DECL_NAME (decl), TFF_PLAIN_IDENTIFIER);
3174 return pp_ggc_formatted_text (cxx_pp);
3177 /* Return the location of a tree passed to %+ formats. */
3179 location_t
3180 location_of (tree t)
3182 if (TYPE_P (t))
3184 t = TYPE_MAIN_DECL (t);
3185 if (t == NULL_TREE)
3186 return input_location;
3188 else if (TREE_CODE (t) == OVERLOAD)
3189 t = OVL_FIRST (t);
3191 if (DECL_P (t))
3192 return DECL_SOURCE_LOCATION (t);
3193 if (TREE_CODE (t) == DEFERRED_PARSE)
3194 return defparse_location (t);
3195 return cp_expr_loc_or_input_loc (t);
3198 /* Now the interfaces from error et al to dump_type et al. Each takes an
3199 on/off VERBOSE flag and supply the appropriate TFF_ flags to a dump_
3200 function. */
3202 static const char *
3203 decl_to_string (tree decl, int verbose)
3205 int flags = 0;
3207 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == RECORD_TYPE
3208 || TREE_CODE (decl) == UNION_TYPE || TREE_CODE (decl) == ENUMERAL_TYPE)
3209 flags = TFF_CLASS_KEY_OR_ENUM;
3210 if (verbose)
3211 flags |= TFF_DECL_SPECIFIERS;
3212 else if (TREE_CODE (decl) == FUNCTION_DECL)
3213 flags |= TFF_DECL_SPECIFIERS | TFF_RETURN_TYPE;
3214 flags |= TFF_TEMPLATE_HEADER;
3216 reinit_cxx_pp ();
3217 dump_decl (cxx_pp, decl, flags);
3218 return pp_ggc_formatted_text (cxx_pp);
3221 const char *
3222 expr_to_string (tree decl)
3224 reinit_cxx_pp ();
3225 dump_expr (cxx_pp, decl, 0);
3226 return pp_ggc_formatted_text (cxx_pp);
3229 static const char *
3230 fndecl_to_string (tree fndecl, int verbose)
3232 int flags;
3234 flags = TFF_EXCEPTION_SPECIFICATION | TFF_DECL_SPECIFIERS
3235 | TFF_TEMPLATE_HEADER;
3236 if (verbose)
3237 flags |= TFF_FUNCTION_DEFAULT_ARGUMENTS;
3238 reinit_cxx_pp ();
3239 dump_decl (cxx_pp, fndecl, flags);
3240 return pp_ggc_formatted_text (cxx_pp);
3244 static const char *
3245 code_to_string (enum tree_code c)
3247 return get_tree_code_name (c);
3250 const char *
3251 language_to_string (enum languages c)
3253 switch (c)
3255 case lang_c:
3256 return "C";
3258 case lang_cplusplus:
3259 return "C++";
3261 default:
3262 gcc_unreachable ();
3264 return NULL;
3267 /* Return the proper printed version of a parameter to a C++ function. */
3269 static const char *
3270 parm_to_string (int p)
3272 reinit_cxx_pp ();
3273 if (p < 0)
3274 pp_string (cxx_pp, "'this'");
3275 else
3276 pp_decimal_int (cxx_pp, p + 1);
3277 return pp_ggc_formatted_text (cxx_pp);
3280 static const char *
3281 op_to_string (bool assop, enum tree_code p)
3283 tree id = ovl_op_identifier (assop, p);
3284 return id ? IDENTIFIER_POINTER (id) : M_("<unknown>");
3287 /* Return a GC-allocated representation of type TYP, with verbosity VERBOSE.
3289 If QUOTE is non-NULL and if *QUOTE is true, then quotes are added to the
3290 string in appropriate places, and *QUOTE is written to with false
3291 to suppress pp_format's trailing close quote so that e.g.
3292 foo_typedef {aka underlying_foo} {enum}
3293 can be printed by "%qT" as:
3294 `foo_typedef' {aka `underlying_foo'} {enum}
3295 rather than:
3296 `foo_typedef {aka underlying_foo} {enum}'
3297 When adding such quotes, if POSTPROCESSED is true (for handling %H and %I)
3298 then a leading open quote will be added, whereas if POSTPROCESSED is false
3299 (for handling %T) then any leading quote has already been added by
3300 pp_format, or is not needed due to QUOTE being NULL (for template arguments
3301 within %H and %I).
3303 SHOW_COLOR is used to determine the colorization of any quotes that
3304 are added. */
3306 static const char *
3307 type_to_string (tree typ, int verbose, bool postprocessed, bool *quote,
3308 bool show_color)
3310 int flags = 0;
3311 if (verbose)
3312 flags |= TFF_CLASS_KEY_OR_ENUM;
3313 flags |= TFF_TEMPLATE_HEADER;
3315 reinit_cxx_pp ();
3317 if (postprocessed && quote && *quote)
3318 pp_begin_quote (cxx_pp, show_color);
3320 struct obstack *ob = pp_buffer (cxx_pp)->obstack;
3321 int type_start, type_len;
3322 type_start = obstack_object_size (ob);
3324 dump_type (cxx_pp, typ, flags);
3326 /* Remember the end of the initial dump. */
3327 type_len = obstack_object_size (ob) - type_start;
3329 /* If we're printing a type that involves typedefs, also print the
3330 stripped version. But sometimes the stripped version looks
3331 exactly the same, so we don't want it after all. To avoid printing
3332 it in that case, we play ugly obstack games. */
3333 if (typ && TYPE_P (typ) && typ != TYPE_CANONICAL (typ)
3334 && !uses_template_parms (typ))
3336 int aka_start, aka_len; char *p;
3337 tree aka = strip_typedefs (typ, NULL, STF_USER_VISIBLE);
3338 if (quote && *quote)
3339 pp_end_quote (cxx_pp, show_color);
3340 pp_string (cxx_pp, " {aka");
3341 pp_cxx_whitespace (cxx_pp);
3342 if (quote && *quote)
3343 pp_begin_quote (cxx_pp, show_color);
3344 /* And remember the start of the aka dump. */
3345 aka_start = obstack_object_size (ob);
3346 dump_type (cxx_pp, aka, flags);
3347 aka_len = obstack_object_size (ob) - aka_start;
3348 if (quote && *quote)
3349 pp_end_quote (cxx_pp, show_color);
3350 pp_right_brace (cxx_pp);
3351 p = (char*)obstack_base (ob);
3352 /* If they are identical, cut off the aka by unwinding the obstack. */
3353 if (type_len == aka_len
3354 && memcmp (p + type_start, p+aka_start, type_len) == 0)
3356 /* We can't add a '\0' here, since we may be adding a closing quote
3357 below, and it would be hidden by the '\0'.
3358 Instead, manually unwind the current object within the obstack
3359 so that the insertion point is at the end of the type, before
3360 the "' {aka". */
3361 int delta = type_start + type_len - obstack_object_size (ob);
3362 gcc_assert (delta <= 0);
3363 obstack_blank_fast (ob, delta);
3365 else
3366 if (quote)
3367 /* No further closing quotes are needed. */
3368 *quote = false;
3371 if (quote && *quote)
3373 pp_end_quote (cxx_pp, show_color);
3374 *quote = false;
3376 return pp_ggc_formatted_text (cxx_pp);
3379 static const char *
3380 args_to_string (tree p, int verbose)
3382 int flags = 0;
3383 if (verbose)
3384 flags |= TFF_CLASS_KEY_OR_ENUM;
3386 if (p == NULL_TREE)
3387 return "";
3389 if (TYPE_P (TREE_VALUE (p)))
3390 return type_as_string_translate (p, flags);
3392 reinit_cxx_pp ();
3393 for (; p; p = TREE_CHAIN (p))
3395 if (null_node_p (TREE_VALUE (p)))
3396 pp_cxx_ws_string (cxx_pp, "NULL");
3397 else
3398 dump_type (cxx_pp, error_type (TREE_VALUE (p)), flags);
3399 if (TREE_CHAIN (p))
3400 pp_separate_with_comma (cxx_pp);
3402 return pp_ggc_formatted_text (cxx_pp);
3405 /* Pretty-print a deduction substitution (from deduction_tsubst_fntype). P
3406 is a TREE_LIST with purpose the TEMPLATE_DECL, value the template
3407 arguments. */
3409 static const char *
3410 subst_to_string (tree p)
3412 tree decl = TREE_PURPOSE (p);
3413 tree targs = TREE_VALUE (p);
3414 tree tparms = DECL_TEMPLATE_PARMS (decl);
3415 int flags = (TFF_DECL_SPECIFIERS|TFF_TEMPLATE_HEADER
3416 |TFF_NO_TEMPLATE_BINDINGS);
3418 if (p == NULL_TREE)
3419 return "";
3421 reinit_cxx_pp ();
3422 dump_template_decl (cxx_pp, TREE_PURPOSE (p), flags);
3423 dump_substitution (cxx_pp, NULL, tparms, targs, /*flags=*/0);
3424 return pp_ggc_formatted_text (cxx_pp);
3427 static const char *
3428 cv_to_string (tree p, int v)
3430 reinit_cxx_pp ();
3431 cxx_pp->padding = v ? pp_before : pp_none;
3432 pp_cxx_cv_qualifier_seq (cxx_pp, p);
3433 return pp_ggc_formatted_text (cxx_pp);
3436 static const char *
3437 eh_spec_to_string (tree p, int /*v*/)
3439 int flags = 0;
3440 reinit_cxx_pp ();
3441 dump_exception_spec (cxx_pp, p, flags);
3442 return pp_ggc_formatted_text (cxx_pp);
3445 /* Langhook for print_error_function. */
3446 void
3447 cxx_print_error_function (diagnostic_context *context, const char *file,
3448 diagnostic_info *diagnostic)
3450 char *prefix;
3451 if (file)
3452 prefix = xstrdup (file);
3453 else
3454 prefix = NULL;
3455 lhd_print_error_function (context, file, diagnostic);
3456 pp_set_prefix (context->printer, prefix);
3457 maybe_print_instantiation_context (context);
3460 static void
3461 cp_diagnostic_starter (diagnostic_context *context,
3462 diagnostic_info *diagnostic)
3464 diagnostic_report_current_module (context, diagnostic_location (diagnostic));
3465 cp_print_error_function (context, diagnostic);
3466 maybe_print_instantiation_context (context);
3467 maybe_print_constexpr_context (context);
3468 maybe_print_constraint_context (context);
3469 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
3470 diagnostic));
3473 /* Print current function onto BUFFER, in the process of reporting
3474 a diagnostic message. Called from cp_diagnostic_starter. */
3475 static void
3476 cp_print_error_function (diagnostic_context *context,
3477 diagnostic_info *diagnostic)
3479 /* If we are in an instantiation context, current_function_decl is likely
3480 to be wrong, so just rely on print_instantiation_full_context. */
3481 if (current_instantiation ())
3482 return;
3483 /* The above is true for constraint satisfaction also. */
3484 if (current_failed_constraint)
3485 return;
3486 if (diagnostic_last_function_changed (context, diagnostic))
3488 char *old_prefix = pp_take_prefix (context->printer);
3489 const char *file = LOCATION_FILE (diagnostic_location (diagnostic));
3490 tree abstract_origin = diagnostic_abstract_origin (diagnostic);
3491 char *new_prefix = (file && abstract_origin == NULL)
3492 ? file_name_as_prefix (context, file) : NULL;
3494 pp_set_prefix (context->printer, new_prefix);
3496 if (current_function_decl == NULL)
3497 pp_string (context->printer, _("At global scope:"));
3498 else
3500 tree fndecl, ao;
3502 if (abstract_origin)
3504 ao = BLOCK_ABSTRACT_ORIGIN (abstract_origin);
3505 gcc_assert (TREE_CODE (ao) == FUNCTION_DECL);
3506 fndecl = ao;
3508 else
3509 fndecl = current_function_decl;
3511 pp_printf (context->printer, function_category (fndecl),
3512 cxx_printable_name_translate (fndecl, 2));
3514 while (abstract_origin)
3516 location_t *locus;
3517 tree block = abstract_origin;
3519 locus = &BLOCK_SOURCE_LOCATION (block);
3520 fndecl = NULL;
3521 block = BLOCK_SUPERCONTEXT (block);
3522 while (block && TREE_CODE (block) == BLOCK
3523 && BLOCK_ABSTRACT_ORIGIN (block))
3525 ao = BLOCK_ABSTRACT_ORIGIN (block);
3526 if (TREE_CODE (ao) == FUNCTION_DECL)
3528 fndecl = ao;
3529 break;
3531 else if (TREE_CODE (ao) != BLOCK)
3532 break;
3534 block = BLOCK_SUPERCONTEXT (block);
3536 if (fndecl)
3537 abstract_origin = block;
3538 else
3540 while (block && TREE_CODE (block) == BLOCK)
3541 block = BLOCK_SUPERCONTEXT (block);
3543 if (block && TREE_CODE (block) == FUNCTION_DECL)
3544 fndecl = block;
3545 abstract_origin = NULL;
3547 if (fndecl)
3549 expanded_location s = expand_location (*locus);
3550 pp_character (context->printer, ',');
3551 pp_newline (context->printer);
3552 if (s.file != NULL)
3554 if (context->show_column && s.column != 0)
3555 pp_printf (context->printer,
3556 _(" inlined from %qs at %r%s:%d:%d%R"),
3557 cxx_printable_name_translate (fndecl, 2),
3558 "locus", s.file, s.line, s.column);
3559 else
3560 pp_printf (context->printer,
3561 _(" inlined from %qs at %r%s:%d%R"),
3562 cxx_printable_name_translate (fndecl, 2),
3563 "locus", s.file, s.line);
3566 else
3567 pp_printf (context->printer, _(" inlined from %qs"),
3568 cxx_printable_name_translate (fndecl, 2));
3571 pp_character (context->printer, ':');
3573 pp_newline (context->printer);
3575 diagnostic_set_last_function (context, diagnostic);
3576 pp_destroy_prefix (context->printer);
3577 context->printer->prefix = old_prefix;
3581 /* Returns a description of FUNCTION using standard terminology. The
3582 result is a format string of the form "In CATEGORY %qs". */
3583 static const char *
3584 function_category (tree fn)
3586 /* We can get called from the middle-end for diagnostics of function
3587 clones. Make sure we have language specific information before
3588 dereferencing it. */
3589 if (DECL_LANG_SPECIFIC (STRIP_TEMPLATE (fn))
3590 && DECL_FUNCTION_MEMBER_P (fn))
3592 if (DECL_STATIC_FUNCTION_P (fn))
3593 return _("In static member function %qs");
3594 else if (DECL_COPY_CONSTRUCTOR_P (fn))
3595 return _("In copy constructor %qs");
3596 else if (DECL_CONSTRUCTOR_P (fn))
3597 return _("In constructor %qs");
3598 else if (DECL_DESTRUCTOR_P (fn))
3599 return _("In destructor %qs");
3600 else if (LAMBDA_FUNCTION_P (fn))
3601 return _("In lambda function");
3602 else
3603 return _("In member function %qs");
3605 else
3606 return _("In function %qs");
3609 /* Disable warnings about missing quoting in GCC diagnostics for
3610 the pp_verbatim calls. Their format strings deliberately don't
3611 follow GCC diagnostic conventions. */
3612 #if __GNUC__ >= 10
3613 # pragma GCC diagnostic push
3614 # pragma GCC diagnostic ignored "-Wformat-diag"
3615 #endif
3617 /* Report the full context of a current template instantiation,
3618 onto BUFFER. */
3619 static void
3620 print_instantiation_full_context (diagnostic_context *context)
3622 struct tinst_level *p = current_instantiation ();
3623 location_t location = input_location;
3625 if (p)
3627 pp_verbatim (context->printer,
3628 p->list_p ()
3629 ? _("%s: In substitution of %qS:\n")
3630 : _("%s: In instantiation of %q#D:\n"),
3631 LOCATION_FILE (location),
3632 p->get_node ());
3634 location = p->locus;
3635 p = p->next;
3638 print_instantiation_partial_context (context, p, location);
3641 /* Helper function of print_instantiation_partial_context() that
3642 prints a single line of instantiation context. */
3644 static void
3645 print_instantiation_partial_context_line (diagnostic_context *context,
3646 struct tinst_level *t,
3647 location_t loc, bool recursive_p)
3649 if (loc == UNKNOWN_LOCATION)
3650 return;
3652 expanded_location xloc = expand_location (loc);
3654 if (context->show_column)
3655 pp_verbatim (context->printer, _("%r%s:%d:%d:%R "),
3656 "locus", xloc.file, xloc.line, xloc.column);
3657 else
3658 pp_verbatim (context->printer, _("%r%s:%d:%R "),
3659 "locus", xloc.file, xloc.line);
3661 if (t != NULL)
3663 if (t->list_p ())
3664 pp_verbatim (context->printer,
3665 recursive_p
3666 ? _("recursively required by substitution of %qS\n")
3667 : _("required by substitution of %qS\n"),
3668 t->get_node ());
3669 else
3670 pp_verbatim (context->printer,
3671 recursive_p
3672 ? _("recursively required from %q#D\n")
3673 : _("required from %q#D\n"),
3674 t->get_node ());
3676 else
3678 pp_verbatim (context->printer,
3679 recursive_p
3680 ? _("recursively required from here\n")
3681 : _("required from here\n"));
3685 /* Same as print_instantiation_full_context but less verbose. */
3687 static void
3688 print_instantiation_partial_context (diagnostic_context *context,
3689 struct tinst_level *t0, location_t loc)
3691 struct tinst_level *t;
3692 int n_total = 0;
3693 int n;
3694 location_t prev_loc = loc;
3696 for (t = t0; t != NULL; t = t->next)
3697 if (prev_loc != t->locus)
3699 prev_loc = t->locus;
3700 n_total++;
3703 t = t0;
3705 if (template_backtrace_limit
3706 && n_total > template_backtrace_limit)
3708 int skip = n_total - template_backtrace_limit;
3709 int head = template_backtrace_limit / 2;
3711 /* Avoid skipping just 1. If so, skip 2. */
3712 if (skip == 1)
3714 skip = 2;
3715 head = (template_backtrace_limit - 1) / 2;
3718 for (n = 0; n < head; n++)
3720 gcc_assert (t != NULL);
3721 if (loc != t->locus)
3722 print_instantiation_partial_context_line (context, t, loc,
3723 /*recursive_p=*/false);
3724 loc = t->locus;
3725 t = t->next;
3727 if (t != NULL && skip > 0)
3729 expanded_location xloc;
3730 xloc = expand_location (loc);
3731 if (context->show_column)
3732 pp_verbatim (context->printer,
3733 _("%r%s:%d:%d:%R [ skipping %d instantiation "
3734 "contexts, use -ftemplate-backtrace-limit=0 to "
3735 "disable ]\n"),
3736 "locus", xloc.file, xloc.line, xloc.column, skip);
3737 else
3738 pp_verbatim (context->printer,
3739 _("%r%s:%d:%R [ skipping %d instantiation "
3740 "contexts, use -ftemplate-backtrace-limit=0 to "
3741 "disable ]\n"),
3742 "locus", xloc.file, xloc.line, skip);
3744 do {
3745 loc = t->locus;
3746 t = t->next;
3747 } while (t != NULL && --skip > 0);
3751 while (t != NULL)
3753 while (t->next != NULL && t->locus == t->next->locus)
3755 loc = t->locus;
3756 t = t->next;
3758 print_instantiation_partial_context_line (context, t, loc,
3759 t->locus == loc);
3760 loc = t->locus;
3761 t = t->next;
3763 print_instantiation_partial_context_line (context, NULL, loc,
3764 /*recursive_p=*/false);
3767 /* Called from cp_thing to print the template context for an error. */
3768 static void
3769 maybe_print_instantiation_context (diagnostic_context *context)
3771 if (!problematic_instantiation_changed () || current_instantiation () == 0)
3772 return;
3774 record_last_problematic_instantiation ();
3775 print_instantiation_full_context (context);
3778 /* Report what constexpr call(s) we're trying to expand, if any. */
3780 void
3781 maybe_print_constexpr_context (diagnostic_context *context)
3783 vec<tree> call_stack = cx_error_context ();
3784 unsigned ix;
3785 tree t;
3787 FOR_EACH_VEC_ELT (call_stack, ix, t)
3789 expanded_location xloc = expand_location (EXPR_LOCATION (t));
3790 const char *s = expr_as_string (t, 0);
3791 if (context->show_column)
3792 pp_verbatim (context->printer,
3793 _("%r%s:%d:%d:%R in %<constexpr%> expansion of %qs"),
3794 "locus", xloc.file, xloc.line, xloc.column, s);
3795 else
3796 pp_verbatim (context->printer,
3797 _("%r%s:%d:%R in %<constexpr%> expansion of %qs"),
3798 "locus", xloc.file, xloc.line, s);
3799 pp_newline (context->printer);
3804 static void
3805 print_location (diagnostic_context *context, location_t loc)
3807 expanded_location xloc = expand_location (loc);
3808 if (context->show_column)
3809 pp_verbatim (context->printer, _("%r%s:%d:%d:%R "),
3810 "locus", xloc.file, xloc.line, xloc.column);
3811 else
3812 pp_verbatim (context->printer, _("%r%s:%d:%R "),
3813 "locus", xloc.file, xloc.line);
3816 static void
3817 print_constrained_decl_info (diagnostic_context *context, tree decl)
3819 print_location (context, DECL_SOURCE_LOCATION (decl));
3820 pp_verbatim (context->printer, "required by the constraints of %q#D\n", decl);
3823 static void
3824 print_concept_check_info (diagnostic_context *context, tree expr, tree map, tree args)
3826 gcc_assert (concept_check_p (expr));
3828 tree id = unpack_concept_check (expr);
3829 tree tmpl = TREE_OPERAND (id, 0);
3830 if (OVL_P (tmpl))
3831 tmpl = OVL_FIRST (tmpl);
3833 print_location (context, DECL_SOURCE_LOCATION (tmpl));
3835 cxx_pretty_printer *pp = (cxx_pretty_printer *)context->printer;
3836 pp_verbatim (pp, "required for the satisfaction of %qE", expr);
3837 if (map && map != error_mark_node)
3839 tree subst_map = tsubst_parameter_mapping (map, args, tf_none, NULL_TREE);
3840 pp_cxx_parameter_mapping (pp, (subst_map != error_mark_node
3841 ? subst_map : map));
3843 pp_newline (pp);
3846 /* Diagnose the entry point into the satisfaction error. Returns the next
3847 context, if any. */
3849 static tree
3850 print_constraint_context_head (diagnostic_context *context, tree cxt, tree args)
3852 tree src = TREE_VALUE (cxt);
3853 if (!src)
3855 print_location (context, input_location);
3856 pp_verbatim (context->printer, "required for constraint satisfaction\n");
3857 return NULL_TREE;
3859 if (DECL_P (src))
3861 print_constrained_decl_info (context, src);
3862 return NULL_TREE;
3864 else
3866 print_concept_check_info (context, src, TREE_PURPOSE (cxt), args);
3867 return TREE_CHAIN (cxt);
3871 static void
3872 print_requires_expression_info (diagnostic_context *context, tree constr, tree args)
3875 tree expr = ATOMIC_CONSTR_EXPR (constr);
3876 tree map = ATOMIC_CONSTR_MAP (constr);
3877 map = tsubst_parameter_mapping (map, args, tf_none, NULL_TREE);
3878 if (map == error_mark_node)
3879 return;
3881 print_location (context, cp_expr_loc_or_input_loc (expr));
3882 pp_verbatim (context->printer, "in requirements ");
3884 tree parms = TREE_OPERAND (expr, 0);
3885 if (parms)
3886 pp_verbatim (context->printer, "with ");
3887 while (parms)
3889 pp_verbatim (context->printer, "%q#D", parms);
3890 if (TREE_CHAIN (parms))
3891 pp_separate_with_comma ((cxx_pretty_printer *)context->printer);
3892 parms = TREE_CHAIN (parms);
3894 pp_cxx_parameter_mapping ((cxx_pretty_printer *)context->printer, map);
3896 pp_verbatim (context->printer, "\n");
3899 void
3900 maybe_print_single_constraint_context (diagnostic_context *context, tree failed)
3902 if (!failed)
3903 return;
3905 tree constr = TREE_VALUE (failed);
3906 if (!constr || constr == error_mark_node)
3907 return;
3908 tree cxt = CONSTR_CONTEXT (constr);
3909 if (!cxt)
3910 return;
3911 tree args = TREE_PURPOSE (failed);
3913 /* Print the stack of requirements. */
3914 cxt = print_constraint_context_head (context, cxt, args);
3915 while (cxt && !DECL_P (TREE_VALUE (cxt)))
3917 tree expr = TREE_VALUE (cxt);
3918 tree map = TREE_PURPOSE (cxt);
3919 print_concept_check_info (context, expr, map, args);
3920 cxt = TREE_CHAIN (cxt);
3923 /* For certain constraints, we can provide additional context. */
3924 if (TREE_CODE (constr) == ATOMIC_CONSTR
3925 && TREE_CODE (ATOMIC_CONSTR_EXPR (constr)) == REQUIRES_EXPR)
3926 print_requires_expression_info (context, constr, args);
3929 void
3930 maybe_print_constraint_context (diagnostic_context *context)
3932 if (!current_failed_constraint)
3933 return;
3935 tree cur = current_failed_constraint;
3937 /* Recursively print nested contexts. */
3938 current_failed_constraint = TREE_CHAIN (current_failed_constraint);
3939 if (current_failed_constraint)
3940 maybe_print_constraint_context (context);
3942 /* Print this context. */
3943 maybe_print_single_constraint_context (context, cur);
3946 /* Return true iff TYPE_A and TYPE_B are template types that are
3947 meaningful to compare. */
3949 static bool
3950 comparable_template_types_p (tree type_a, tree type_b)
3952 if (!CLASS_TYPE_P (type_a))
3953 return false;
3954 if (!CLASS_TYPE_P (type_b))
3955 return false;
3957 tree tinfo_a = TYPE_TEMPLATE_INFO (type_a);
3958 tree tinfo_b = TYPE_TEMPLATE_INFO (type_b);
3959 if (!tinfo_a || !tinfo_b)
3960 return false;
3962 return TI_TEMPLATE (tinfo_a) == TI_TEMPLATE (tinfo_b);
3965 /* Start a new line indented by SPC spaces on PP. */
3967 static void
3968 newline_and_indent (pretty_printer *pp, int spc)
3970 pp_newline (pp);
3971 for (int i = 0; i < spc; i++)
3972 pp_space (pp);
3975 /* Generate a GC-allocated string for ARG, an expression or type. */
3977 static const char *
3978 arg_to_string (tree arg, bool verbose)
3980 if (TYPE_P (arg))
3981 return type_to_string (arg, verbose, true, NULL, false);
3982 else
3983 return expr_to_string (arg);
3986 /* Subroutine to type_to_string_with_compare and
3987 print_template_tree_comparison.
3989 Print a representation of ARG (an expression or type) to PP,
3990 colorizing it as "type-diff" if PP->show_color. */
3992 static void
3993 print_nonequal_arg (pretty_printer *pp, tree arg, bool verbose)
3995 pp_printf (pp, "%r%s%R",
3996 "type-diff",
3997 (arg
3998 ? arg_to_string (arg, verbose)
3999 : G_("(no argument)")));
4002 /* Recursively print template TYPE_A to PP, as compared to template TYPE_B.
4004 The types must satisfy comparable_template_types_p.
4006 If INDENT is 0, then this is equivalent to type_to_string (TYPE_A), but
4007 potentially colorizing/eliding in comparison with TYPE_B.
4009 For example given types:
4010 vector<map<int,double>>
4012 vector<map<int,float>>
4013 then the result on PP would be:
4014 vector<map<[...],double>>
4015 with type elision, and:
4016 vector<map<int,double>>
4017 without type elision.
4019 In both cases the parts of TYPE that differ from PEER will be colorized
4020 if pp_show_color (pp) is true. In the above example, this would be
4021 "double".
4023 If INDENT is non-zero, then the types are printed in a tree-like form
4024 which shows both types. In the above example, the result on PP would be:
4026 vector<
4027 map<
4028 [...],
4029 [double != float]>>
4031 and without type-elision would be:
4033 vector<
4034 map<
4035 int,
4036 [double != float]>>
4038 As before, the differing parts of the types are colorized if
4039 pp_show_color (pp) is true ("double" and "float" in this example).
4041 Template arguments in which both types are using the default arguments
4042 are not printed; if at least one of the two types is using a non-default
4043 argument, then that argument is printed (or both arguments for the
4044 tree-like print format). */
4046 static void
4047 print_template_differences (pretty_printer *pp, tree type_a, tree type_b,
4048 bool verbose, int indent)
4050 if (indent)
4051 newline_and_indent (pp, indent);
4053 tree tinfo_a = TYPE_TEMPLATE_INFO (type_a);
4054 tree tinfo_b = TYPE_TEMPLATE_INFO (type_b);
4056 pp_printf (pp, "%s<",
4057 IDENTIFIER_POINTER (DECL_NAME (TI_TEMPLATE (tinfo_a))));
4059 tree args_a = TI_ARGS (tinfo_a);
4060 tree args_b = TI_ARGS (tinfo_b);
4061 gcc_assert (TREE_CODE (args_a) == TREE_VEC);
4062 gcc_assert (TREE_CODE (args_b) == TREE_VEC);
4063 int flags = 0;
4064 int len_a = get_non_default_template_args_count (args_a, flags);
4065 args_a = INNERMOST_TEMPLATE_ARGS (args_a);
4066 int len_b = get_non_default_template_args_count (args_b, flags);
4067 args_b = INNERMOST_TEMPLATE_ARGS (args_b);
4068 /* Determine the maximum range of args for which non-default template args
4069 were used; beyond this, only default args (if any) were used, and so
4070 they will be equal from this point onwards.
4071 One of the two peers might have used default arguments within this
4072 range, but the other will be using non-default arguments, and so
4073 it's more readable to print both within this range, to highlight
4074 the differences. */
4075 int len_max = MAX (len_a, len_b);
4076 gcc_assert (TREE_CODE (args_a) == TREE_VEC);
4077 gcc_assert (TREE_CODE (args_b) == TREE_VEC);
4078 for (int idx = 0; idx < len_max; idx++)
4080 if (idx)
4081 pp_character (pp, ',');
4083 tree arg_a = TREE_VEC_ELT (args_a, idx);
4084 tree arg_b = TREE_VEC_ELT (args_b, idx);
4085 if (arg_a == arg_b)
4087 if (indent)
4088 newline_and_indent (pp, indent + 2);
4089 /* Can do elision here, printing "[...]". */
4090 if (flag_elide_type)
4091 pp_string (pp, G_("[...]"));
4092 else
4093 pp_string (pp, arg_to_string (arg_a, verbose));
4095 else
4097 int new_indent = indent ? indent + 2 : 0;
4098 if (comparable_template_types_p (arg_a, arg_b))
4099 print_template_differences (pp, arg_a, arg_b, verbose, new_indent);
4100 else
4101 if (indent)
4103 newline_and_indent (pp, indent + 2);
4104 pp_character (pp, '[');
4105 print_nonequal_arg (pp, arg_a, verbose);
4106 pp_string (pp, " != ");
4107 print_nonequal_arg (pp, arg_b, verbose);
4108 pp_character (pp, ']');
4110 else
4111 print_nonequal_arg (pp, arg_a, verbose);
4114 pp_printf (pp, ">");
4117 /* As type_to_string, but for a template, potentially colorizing/eliding
4118 in comparison with PEER.
4119 For example, if TYPE is map<int,double> and PEER is map<int,int>,
4120 then the resulting string would be:
4121 map<[...],double>
4122 with type elision, and:
4123 map<int,double>
4124 without type elision.
4126 In both cases the parts of TYPE that differ from PEER will be colorized
4127 if SHOW_COLOR is true. In the above example, this would be "double".
4129 Template arguments in which both types are using the default arguments
4130 are not printed; if at least one of the two types is using a non-default
4131 argument, then both arguments are printed.
4133 The resulting string is in a GC-allocated buffer. */
4135 static const char *
4136 type_to_string_with_compare (tree type, tree peer, bool verbose,
4137 bool show_color)
4139 pretty_printer inner_pp;
4140 pretty_printer *pp = &inner_pp;
4141 pp_show_color (pp) = show_color;
4143 print_template_differences (pp, type, peer, verbose, 0);
4144 return pp_ggc_formatted_text (pp);
4147 /* Recursively print a tree-like comparison of TYPE_A and TYPE_B to PP,
4148 indented by INDENT spaces.
4150 For example given types:
4152 vector<map<int,double>>
4156 vector<map<double,float>>
4158 the output with type elision would be:
4160 vector<
4161 map<
4162 [...],
4163 [double != float]>>
4165 and without type-elision would be:
4167 vector<
4168 map<
4169 int,
4170 [double != float]>>
4172 TYPE_A and TYPE_B must both be comparable template types
4173 (as per comparable_template_types_p).
4175 Template arguments in which both types are using the default arguments
4176 are not printed; if at least one of the two types is using a non-default
4177 argument, then both arguments are printed. */
4179 static void
4180 print_template_tree_comparison (pretty_printer *pp, tree type_a, tree type_b,
4181 bool verbose, int indent)
4183 print_template_differences (pp, type_a, type_b, verbose, indent);
4186 /* Subroutine for use in a format_postprocessor::handle
4187 implementation. Adds a chunk to the end of
4188 formatted output, so that it will be printed
4189 by pp_output_formatted_text. */
4191 static void
4192 append_formatted_chunk (pretty_printer *pp, const char *content)
4194 output_buffer *buffer = pp_buffer (pp);
4195 struct chunk_info *chunk_array = buffer->cur_chunk_array;
4196 const char **args = chunk_array->args;
4198 unsigned int chunk_idx;
4199 for (chunk_idx = 0; args[chunk_idx]; chunk_idx++)
4201 args[chunk_idx++] = content;
4202 args[chunk_idx] = NULL;
4205 /* Create a copy of CONTENT, with quotes added, and,
4206 potentially, with colorization.
4207 No escaped is performed on CONTENT.
4208 The result is in a GC-allocated buffer. */
4210 static const char *
4211 add_quotes (const char *content, bool show_color)
4213 pretty_printer tmp_pp;
4214 pp_show_color (&tmp_pp) = show_color;
4216 /* We have to use "%<%s%>" rather than "%qs" here in order to avoid
4217 quoting colorization bytes within the results and using either
4218 pp_quote or pp_begin_quote doesn't work the same. */
4219 pp_printf (&tmp_pp, "%<%s%>", content);
4221 return pp_ggc_formatted_text (&tmp_pp);
4224 #if __GNUC__ >= 10
4225 # pragma GCC diagnostic pop
4226 #endif
4228 /* If we had %H and %I, and hence deferred printing them,
4229 print them now, storing the result into the chunk_info
4230 for pp_format. Quote them if 'q' was provided.
4231 Also print the difference in tree form, adding it as
4232 an additional chunk. */
4234 void
4235 cxx_format_postprocessor::handle (pretty_printer *pp)
4237 /* If we have one of %H and %I, the other should have
4238 been present. */
4239 if (m_type_a.m_tree || m_type_b.m_tree)
4241 /* Avoid reentrancy issues by working with a copy of
4242 m_type_a and m_type_b, resetting them now. */
4243 deferred_printed_type type_a = m_type_a;
4244 deferred_printed_type type_b = m_type_b;
4245 m_type_a = deferred_printed_type ();
4246 m_type_b = deferred_printed_type ();
4248 gcc_assert (type_a.m_buffer_ptr);
4249 gcc_assert (type_b.m_buffer_ptr);
4251 bool show_color = pp_show_color (pp);
4253 const char *type_a_text;
4254 const char *type_b_text;
4256 if (comparable_template_types_p (type_a.m_tree, type_b.m_tree))
4258 type_a_text
4259 = type_to_string_with_compare (type_a.m_tree, type_b.m_tree,
4260 type_a.m_verbose, show_color);
4261 type_b_text
4262 = type_to_string_with_compare (type_b.m_tree, type_a.m_tree,
4263 type_b.m_verbose, show_color);
4265 if (flag_diagnostics_show_template_tree)
4267 pretty_printer inner_pp;
4268 pp_show_color (&inner_pp) = pp_show_color (pp);
4269 print_template_tree_comparison
4270 (&inner_pp, type_a.m_tree, type_b.m_tree, type_a.m_verbose, 2);
4271 append_formatted_chunk (pp, pp_ggc_formatted_text (&inner_pp));
4274 else
4276 /* If the types were not comparable (or if only one of %H/%I was
4277 provided), they are printed normally, and no difference tree
4278 is printed. */
4279 type_a_text = type_to_string (type_a.m_tree, type_a.m_verbose,
4280 true, &type_a.m_quote, show_color);
4281 type_b_text = type_to_string (type_b.m_tree, type_b.m_verbose,
4282 true, &type_b.m_quote, show_color);
4285 if (type_a.m_quote)
4286 type_a_text = add_quotes (type_a_text, show_color);
4287 *type_a.m_buffer_ptr = type_a_text;
4289 if (type_b.m_quote)
4290 type_b_text = add_quotes (type_b_text, show_color);
4291 *type_b.m_buffer_ptr = type_b_text;
4295 /* Subroutine for handling %H and %I, to support i18n of messages like:
4297 error_at (loc, "could not convert %qE from %qH to %qI",
4298 expr, type_a, type_b);
4300 so that we can print things like:
4302 could not convert 'foo' from 'map<int,double>' to 'map<int,int>'
4304 and, with type-elision:
4306 could not convert 'foo' from 'map<[...],double>' to 'map<[...],int>'
4308 (with color-coding of the differences between the types).
4310 The %H and %I format codes are peers: both must be present,
4311 and they affect each other. Hence to handle them, we must
4312 delay printing until we have both, deferring the printing to
4313 pretty_printer's m_format_postprocessor hook.
4315 This is called in phase 2 of pp_format, when it is accumulating
4316 a series of formatted chunks. We stash the location of the chunk
4317 we're meant to have written to, so that we can write to it in the
4318 m_format_postprocessor hook.
4320 We also need to stash whether a 'q' prefix was provided (the QUOTE
4321 param) so that we can add the quotes when writing out the delayed
4322 chunk. */
4324 static void
4325 defer_phase_2_of_type_diff (deferred_printed_type *deferred,
4326 tree type, const char **buffer_ptr,
4327 bool verbose, bool quote)
4329 gcc_assert (deferred->m_tree == NULL_TREE);
4330 gcc_assert (deferred->m_buffer_ptr == NULL);
4331 *deferred = deferred_printed_type (type, buffer_ptr, verbose, quote);
4335 /* Called from output_format -- during diagnostic message processing --
4336 to handle C++ specific format specifier with the following meanings:
4337 %A function argument-list.
4338 %C tree code.
4339 %D declaration.
4340 %E expression.
4341 %F function declaration.
4342 %H type difference (from).
4343 %I type difference (to).
4344 %L language as used in extern "lang".
4345 %O binary operator.
4346 %P function parameter whose position is indicated by an integer.
4347 %Q assignment operator.
4348 %S substitution (template + args)
4349 %T type.
4350 %V cv-qualifier.
4351 %X exception-specification. */
4352 static bool
4353 cp_printer (pretty_printer *pp, text_info *text, const char *spec,
4354 int precision, bool wide, bool set_locus, bool verbose,
4355 bool *quoted, const char **buffer_ptr)
4357 gcc_assert (pp->m_format_postprocessor);
4358 cxx_format_postprocessor *postprocessor
4359 = static_cast <cxx_format_postprocessor *> (pp->m_format_postprocessor);
4361 const char *result;
4362 tree t = NULL;
4363 #define next_tree (t = va_arg (*text->args_ptr, tree))
4364 #define next_tcode ((enum tree_code) va_arg (*text->args_ptr, int))
4365 #define next_lang ((enum languages) va_arg (*text->args_ptr, int))
4366 #define next_int va_arg (*text->args_ptr, int)
4368 if (precision != 0 || wide)
4369 return false;
4371 switch (*spec)
4373 case 'A': result = args_to_string (next_tree, verbose); break;
4374 case 'C': result = code_to_string (next_tcode); break;
4375 case 'D':
4377 tree temp = next_tree;
4378 if (VAR_P (temp)
4379 && DECL_HAS_DEBUG_EXPR_P (temp))
4381 temp = DECL_DEBUG_EXPR (temp);
4382 if (!DECL_P (temp))
4384 result = expr_to_string (temp);
4385 break;
4388 result = decl_to_string (temp, verbose);
4390 break;
4391 case 'E': result = expr_to_string (next_tree); break;
4392 case 'F': result = fndecl_to_string (next_tree, verbose); break;
4393 case 'H':
4394 defer_phase_2_of_type_diff (&postprocessor->m_type_a, next_tree,
4395 buffer_ptr, verbose, *quoted);
4396 return true;
4397 case 'I':
4398 defer_phase_2_of_type_diff (&postprocessor->m_type_b, next_tree,
4399 buffer_ptr, verbose, *quoted);
4400 return true;
4401 case 'L': result = language_to_string (next_lang); break;
4402 case 'O': result = op_to_string (false, next_tcode); break;
4403 case 'P': result = parm_to_string (next_int); break;
4404 case 'Q': result = op_to_string (true, next_tcode); break;
4405 case 'S': result = subst_to_string (next_tree); break;
4406 case 'T':
4408 result = type_to_string (next_tree, verbose, false, quoted,
4409 pp_show_color (pp));
4411 break;
4412 case 'V': result = cv_to_string (next_tree, verbose); break;
4413 case 'X': result = eh_spec_to_string (next_tree, verbose); break;
4415 default:
4416 return false;
4419 pp_string (pp, result);
4420 if (set_locus && t != NULL)
4421 text->set_location (0, location_of (t), SHOW_RANGE_WITH_CARET);
4422 return true;
4423 #undef next_tree
4424 #undef next_tcode
4425 #undef next_lang
4426 #undef next_int
4429 /* Warn about the use of C++0x features when appropriate. */
4430 void
4431 maybe_warn_cpp0x (cpp0x_warn_str str, location_t loc/*=input_location*/)
4433 if (cxx_dialect == cxx98)
4434 switch (str)
4436 case CPP0X_INITIALIZER_LISTS:
4437 pedwarn (loc, OPT_Wc__11_extensions,
4438 "extended initializer lists "
4439 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4440 break;
4441 case CPP0X_EXPLICIT_CONVERSION:
4442 pedwarn (loc, OPT_Wc__11_extensions,
4443 "explicit conversion operators "
4444 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4445 break;
4446 case CPP0X_VARIADIC_TEMPLATES:
4447 pedwarn (loc, OPT_Wc__11_extensions,
4448 "variadic templates "
4449 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4450 break;
4451 case CPP0X_LAMBDA_EXPR:
4452 pedwarn (loc, OPT_Wc__11_extensions,
4453 "lambda expressions "
4454 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4455 break;
4456 case CPP0X_AUTO:
4457 pedwarn (loc, OPT_Wc__11_extensions,
4458 "C++11 auto only available with %<-std=c++11%> or "
4459 "%<-std=gnu++11%>");
4460 break;
4461 case CPP0X_SCOPED_ENUMS:
4462 pedwarn (loc, OPT_Wc__11_extensions,
4463 "scoped enums only available with %<-std=c++11%> or "
4464 "%<-std=gnu++11%>");
4465 break;
4466 case CPP0X_DEFAULTED_DELETED:
4467 pedwarn (loc, OPT_Wc__11_extensions,
4468 "defaulted and deleted functions "
4469 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4470 break;
4471 case CPP0X_INLINE_NAMESPACES:
4472 if (pedantic)
4473 pedwarn (loc, OPT_Wc__11_extensions,
4474 "inline namespaces "
4475 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4476 break;
4477 case CPP0X_OVERRIDE_CONTROLS:
4478 pedwarn (loc, OPT_Wc__11_extensions,
4479 "override controls (override/final) "
4480 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4481 break;
4482 case CPP0X_NSDMI:
4483 pedwarn (loc, OPT_Wc__11_extensions,
4484 "non-static data member initializers "
4485 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4486 break;
4487 case CPP0X_USER_DEFINED_LITERALS:
4488 pedwarn (loc, OPT_Wc__11_extensions,
4489 "user-defined literals "
4490 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4491 break;
4492 case CPP0X_DELEGATING_CTORS:
4493 pedwarn (loc, OPT_Wc__11_extensions,
4494 "delegating constructors "
4495 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4496 break;
4497 case CPP0X_INHERITING_CTORS:
4498 pedwarn (loc, OPT_Wc__11_extensions,
4499 "inheriting constructors "
4500 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4501 break;
4502 case CPP0X_ATTRIBUTES:
4503 pedwarn (loc, OPT_Wc__11_extensions,
4504 "C++11 attributes "
4505 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4506 break;
4507 case CPP0X_REF_QUALIFIER:
4508 pedwarn (loc, OPT_Wc__11_extensions,
4509 "ref-qualifiers "
4510 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4511 break;
4512 default:
4513 gcc_unreachable ();
4517 /* Warn about the use of variadic templates when appropriate. */
4518 void
4519 maybe_warn_variadic_templates (void)
4521 maybe_warn_cpp0x (CPP0X_VARIADIC_TEMPLATES);
4525 /* Issue an ISO C++98 pedantic warning at LOCATION, conditional on
4526 option OPT with text GMSGID. Use this function to report
4527 diagnostics for constructs that are invalid C++98, but valid
4528 C++0x. */
4529 bool
4530 pedwarn_cxx98 (location_t location, int opt, const char *gmsgid, ...)
4532 diagnostic_info diagnostic;
4533 va_list ap;
4534 bool ret;
4535 rich_location richloc (line_table, location);
4537 va_start (ap, gmsgid);
4538 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
4539 (cxx_dialect == cxx98) ? DK_PEDWARN : DK_WARNING);
4540 diagnostic.option_index = opt;
4541 ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
4542 va_end (ap);
4543 return ret;
4546 /* Issue a diagnostic that NAME cannot be found in SCOPE. DECL is what
4547 we found when we tried to do the lookup. LOCATION is the location of
4548 the NAME identifier. */
4550 void
4551 qualified_name_lookup_error (tree scope, tree name,
4552 tree decl, location_t location)
4554 if (scope == error_mark_node)
4555 ; /* We already complained. */
4556 else if (TYPE_P (scope))
4558 if (!COMPLETE_TYPE_P (scope))
4559 error_at (location, "incomplete type %qT used in nested name specifier",
4560 scope);
4561 else if (TREE_CODE (decl) == TREE_LIST)
4563 error_at (location, "reference to %<%T::%D%> is ambiguous",
4564 scope, name);
4565 print_candidates (decl);
4567 else
4569 name_hint hint;
4570 if (SCOPED_ENUM_P (scope) && TREE_CODE (name) == IDENTIFIER_NODE)
4571 hint = suggest_alternative_in_scoped_enum (name, scope);
4572 if (const char *suggestion = hint.suggestion ())
4574 gcc_rich_location richloc (location);
4575 richloc.add_fixit_replace (suggestion);
4576 error_at (&richloc,
4577 "%qD is not a member of %qT; did you mean %qs?",
4578 name, scope, suggestion);
4580 else
4581 error_at (location, "%qD is not a member of %qT", name, scope);
4584 else if (scope != global_namespace)
4586 auto_diagnostic_group d;
4587 bool emit_fixit = true;
4588 name_hint hint
4589 = suggest_alternative_in_explicit_scope (location, name, scope);
4590 if (!hint)
4592 hint = suggest_alternatives_in_other_namespaces (location, name);
4593 /* "location" is just the location of the name, not of the explicit
4594 scope, and it's not easy to get at the latter, so we can't issue
4595 fix-it hints for the suggestion. */
4596 emit_fixit = false;
4598 if (const char *suggestion = hint.suggestion ())
4600 gcc_rich_location richloc (location);
4601 if (emit_fixit)
4602 richloc.add_fixit_replace (suggestion);
4603 error_at (&richloc, "%qD is not a member of %qD; did you mean %qs?",
4604 name, scope, suggestion);
4606 else
4607 error_at (location, "%qD is not a member of %qD", name, scope);
4609 else
4611 auto_diagnostic_group d;
4612 name_hint hint = suggest_alternatives_for (location, name, true);
4613 if (const char *suggestion = hint.suggestion ())
4615 gcc_rich_location richloc (location);
4616 richloc.add_fixit_replace (suggestion);
4617 error_at (&richloc,
4618 "%<::%D%> has not been declared; did you mean %qs?",
4619 name, suggestion);
4621 else
4622 error_at (location, "%<::%D%> has not been declared", name);
4626 /* C++-specific implementation of range_label::get_text () vfunc for
4627 range_label_for_type_mismatch.
4629 Compare with print_template_differences above. */
4631 label_text
4632 range_label_for_type_mismatch::get_text (unsigned /*range_idx*/) const
4634 if (m_labelled_type == NULL_TREE)
4635 return label_text::borrow (NULL);
4637 const bool verbose = false;
4638 const bool show_color = false;
4640 const char *result;
4641 if (m_other_type
4642 && comparable_template_types_p (m_labelled_type, m_other_type))
4643 result = type_to_string_with_compare (m_labelled_type, m_other_type,
4644 verbose, show_color);
4645 else
4646 result = type_to_string (m_labelled_type, verbose, true, NULL, show_color);
4648 /* Both of the above return GC-allocated buffers, so the caller mustn't
4649 free them. */
4650 return label_text::borrow (result);