* cfghooks.c (verify_flow_info): Disable check that all probabilities
[official-gcc.git] / gcc / cp / error.c
blob7a98d2e35944f8e512bade72cd2f21fe83e4e574
1 /* Call-backs for C++ error reporting.
2 This code is non-reentrant.
3 Copyright (C) 1993-2017 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 #include "system.h"
22 #include "coretypes.h"
23 #include "cp-tree.h"
24 #include "stringpool.h"
25 #include "tree-diagnostic.h"
26 #include "langhooks-def.h"
27 #include "intl.h"
28 #include "cxx-pretty-print.h"
29 #include "tree-pretty-print.h"
30 #include "gimple-pretty-print.h"
31 #include "c-family/c-objc.h"
32 #include "ubsan.h"
33 #include "internal-fn.h"
35 #define pp_separate_with_comma(PP) pp_cxx_separate_with (PP, ',')
36 #define pp_separate_with_semicolon(PP) pp_cxx_separate_with (PP, ';')
38 /* cxx_pp is a C++ front-end-specific pretty printer: this is where we
39 dump C++ ASTs as strings. It is mostly used only by the various
40 tree -> string functions that are occasionally called from the
41 debugger or by the front-end for things like
42 __PRETTY_FUNCTION__. */
43 static cxx_pretty_printer actual_pretty_printer;
44 static cxx_pretty_printer * const cxx_pp = &actual_pretty_printer;
46 /* Translate if being used for diagnostics, but not for dump files or
47 __PRETTY_FUNCTION. */
48 #define M_(msgid) (pp_translate_identifiers (cxx_pp) ? _(msgid) : (msgid))
50 # define NEXT_CODE(T) (TREE_CODE (TREE_TYPE (T)))
52 static const char *args_to_string (tree, int);
53 static const char *assop_to_string (enum tree_code);
54 static const char *code_to_string (enum tree_code);
55 static const char *cv_to_string (tree, int);
56 static const char *decl_to_string (tree, int);
57 static const char *expr_to_string (tree);
58 static const char *fndecl_to_string (tree, int);
59 static const char *op_to_string (enum tree_code);
60 static const char *parm_to_string (int);
61 static const char *type_to_string (tree, int);
63 static void dump_alias_template_specialization (cxx_pretty_printer *, tree, int);
64 static void dump_type (cxx_pretty_printer *, tree, int);
65 static void dump_typename (cxx_pretty_printer *, tree, int);
66 static void dump_simple_decl (cxx_pretty_printer *, tree, tree, int);
67 static void dump_decl (cxx_pretty_printer *, tree, int);
68 static void dump_template_decl (cxx_pretty_printer *, tree, int);
69 static void dump_function_decl (cxx_pretty_printer *, tree, int);
70 static void dump_expr (cxx_pretty_printer *, tree, int);
71 static void dump_unary_op (cxx_pretty_printer *, const char *, tree, int);
72 static void dump_binary_op (cxx_pretty_printer *, const char *, tree, int);
73 static void dump_aggr_type (cxx_pretty_printer *, tree, int);
74 static void dump_type_prefix (cxx_pretty_printer *, tree, int);
75 static void dump_type_suffix (cxx_pretty_printer *, tree, int);
76 static void dump_function_name (cxx_pretty_printer *, tree, int);
77 static void dump_call_expr_args (cxx_pretty_printer *, tree, int, bool);
78 static void dump_aggr_init_expr_args (cxx_pretty_printer *, tree, int, bool);
79 static void dump_expr_list (cxx_pretty_printer *, tree, int);
80 static void dump_global_iord (cxx_pretty_printer *, tree);
81 static void dump_parameters (cxx_pretty_printer *, tree, int);
82 static void dump_ref_qualifier (cxx_pretty_printer *, tree, int);
83 static void dump_exception_spec (cxx_pretty_printer *, tree, int);
84 static void dump_template_argument (cxx_pretty_printer *, tree, int);
85 static void dump_template_argument_list (cxx_pretty_printer *, tree, int);
86 static void dump_template_parameter (cxx_pretty_printer *, tree, int);
87 static void dump_template_bindings (cxx_pretty_printer *, tree, tree,
88 vec<tree, va_gc> *);
89 static void dump_scope (cxx_pretty_printer *, tree, int);
90 static void dump_template_parms (cxx_pretty_printer *, tree, int, int);
91 static int get_non_default_template_args_count (tree, int);
92 static const char *function_category (tree);
93 static void maybe_print_constexpr_context (diagnostic_context *);
94 static void maybe_print_instantiation_context (diagnostic_context *);
95 static void print_instantiation_full_context (diagnostic_context *);
96 static void print_instantiation_partial_context (diagnostic_context *,
97 struct tinst_level *,
98 location_t);
99 static void cp_diagnostic_starter (diagnostic_context *, diagnostic_info *);
100 static void cp_print_error_function (diagnostic_context *, diagnostic_info *);
102 static bool cp_printer (pretty_printer *, text_info *, const char *,
103 int, bool, bool, bool, bool, const char **);
105 /* Struct for handling %H or %I, which require delaying printing the
106 type until a postprocessing stage. */
108 struct deferred_printed_type
110 deferred_printed_type ()
111 : m_tree (NULL_TREE), m_buffer_ptr (NULL), m_verbose (false), m_quote (false)
114 deferred_printed_type (tree type, const char **buffer_ptr, bool verbose,
115 bool quote)
116 : m_tree (type), m_buffer_ptr (buffer_ptr), m_verbose (verbose),
117 m_quote (quote)
119 gcc_assert (type);
120 gcc_assert (buffer_ptr);
123 /* The tree is not GTY-marked: they are only non-NULL within a
124 call to pp_format. */
125 tree m_tree;
126 const char **m_buffer_ptr;
127 bool m_verbose;
128 bool m_quote;
131 /* Subclass of format_postprocessor for the C++ frontend.
132 This handles the %H and %I formatting codes, printing them
133 in a postprocessing phase (since they affect each other). */
135 class cxx_format_postprocessor : public format_postprocessor
137 public:
138 cxx_format_postprocessor ()
139 : m_type_a (), m_type_b ()
142 void handle (pretty_printer *pp) FINAL OVERRIDE;
144 deferred_printed_type m_type_a;
145 deferred_printed_type m_type_b;
148 /* CONTEXT->printer is a basic pretty printer that was constructed
149 presumably by diagnostic_initialize(), called early in the
150 compiler's initialization process (in general_init) Before the FE
151 is initialized. This (C++) FE-specific diagnostic initializer is
152 thus replacing the basic pretty printer with one that has C++-aware
153 capacities. */
155 void
156 cxx_initialize_diagnostics (diagnostic_context *context)
158 pretty_printer *base = context->printer;
159 cxx_pretty_printer *pp = XNEW (cxx_pretty_printer);
160 context->printer = new (pp) cxx_pretty_printer ();
162 /* It is safe to free this object because it was previously XNEW()'d. */
163 base->~pretty_printer ();
164 XDELETE (base);
166 c_common_diagnostics_set_defaults (context);
167 diagnostic_starter (context) = cp_diagnostic_starter;
168 /* diagnostic_finalizer is already c_diagnostic_finalizer. */
169 diagnostic_format_decoder (context) = cp_printer;
170 pp->m_format_postprocessor = new cxx_format_postprocessor ();
173 /* Dump a scope, if deemed necessary. */
175 static void
176 dump_scope (cxx_pretty_printer *pp, tree scope, int flags)
178 int f = flags & (TFF_SCOPE | TFF_CHASE_TYPEDEF);
180 if (scope == NULL_TREE)
181 return;
183 if (TREE_CODE (scope) == NAMESPACE_DECL)
185 if (scope != global_namespace)
187 dump_decl (pp, scope, f);
188 pp_cxx_colon_colon (pp);
191 else if (AGGREGATE_TYPE_P (scope))
193 dump_type (pp, scope, f);
194 pp_cxx_colon_colon (pp);
196 else if ((flags & TFF_SCOPE) && TREE_CODE (scope) == FUNCTION_DECL)
198 dump_function_decl (pp, scope, f);
199 pp_cxx_colon_colon (pp);
203 /* Dump the template ARGument under control of FLAGS. */
205 static void
206 dump_template_argument (cxx_pretty_printer *pp, tree arg, int flags)
208 if (ARGUMENT_PACK_P (arg))
209 dump_template_argument_list (pp, ARGUMENT_PACK_ARGS (arg),
210 /* No default args in argument packs. */
211 flags|TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS);
212 else if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
213 dump_type (pp, arg, flags & ~TFF_CLASS_KEY_OR_ENUM);
214 else
216 if (TREE_CODE (arg) == TREE_LIST)
217 arg = TREE_VALUE (arg);
219 /* Strip implicit conversions. */
220 while (CONVERT_EXPR_P (arg))
221 arg = TREE_OPERAND (arg, 0);
223 dump_expr (pp, arg, (flags | TFF_EXPR_IN_PARENS) & ~TFF_CLASS_KEY_OR_ENUM);
227 /* Count the number of template arguments ARGS whose value does not
228 match the (optional) default template parameter in PARAMS */
230 static int
231 get_non_default_template_args_count (tree args, int flags)
233 int n = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_ARGS (args));
235 if (/* We use this flag when generating debug information. We don't
236 want to expand templates at this point, for this may generate
237 new decls, which gets decl counts out of sync, which may in
238 turn cause codegen differences between compilations with and
239 without -g. */
240 (flags & TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS) != 0
241 || !flag_pretty_templates)
242 return n;
244 return GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (INNERMOST_TEMPLATE_ARGS (args));
247 /* Dump a template-argument-list ARGS (always a TREE_VEC) under control
248 of FLAGS. */
250 static void
251 dump_template_argument_list (cxx_pretty_printer *pp, tree args, int flags)
253 int n = get_non_default_template_args_count (args, flags);
254 int need_comma = 0;
255 int i;
257 for (i = 0; i < n; ++i)
259 tree arg = TREE_VEC_ELT (args, i);
261 /* Only print a comma if we know there is an argument coming. In
262 the case of an empty template argument pack, no actual
263 argument will be printed. */
264 if (need_comma
265 && (!ARGUMENT_PACK_P (arg)
266 || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) > 0))
267 pp_separate_with_comma (pp);
269 dump_template_argument (pp, arg, flags);
270 need_comma = 1;
274 /* Dump a template parameter PARM (a TREE_LIST) under control of FLAGS. */
276 static void
277 dump_template_parameter (cxx_pretty_printer *pp, tree parm, int flags)
279 tree p;
280 tree a;
282 if (parm == error_mark_node)
283 return;
285 p = TREE_VALUE (parm);
286 a = TREE_PURPOSE (parm);
288 if (TREE_CODE (p) == TYPE_DECL)
290 if (flags & TFF_DECL_SPECIFIERS)
292 pp_cxx_ws_string (pp, "class");
293 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (p)))
294 pp_cxx_ws_string (pp, "...");
295 if (DECL_NAME (p))
296 pp_cxx_tree_identifier (pp, DECL_NAME (p));
298 else if (DECL_NAME (p))
299 pp_cxx_tree_identifier (pp, DECL_NAME (p));
300 else
301 pp_cxx_canonical_template_parameter (pp, TREE_TYPE (p));
303 else
304 dump_decl (pp, p, flags | TFF_DECL_SPECIFIERS);
306 if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && a != NULL_TREE)
308 pp_cxx_whitespace (pp);
309 pp_equal (pp);
310 pp_cxx_whitespace (pp);
311 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
312 dump_type (pp, a, flags & ~TFF_CHASE_TYPEDEF);
313 else
314 dump_expr (pp, a, flags | TFF_EXPR_IN_PARENS);
318 /* Dump, under control of FLAGS, a template-parameter-list binding.
319 PARMS is a TREE_LIST of TREE_VEC of TREE_LIST and ARGS is a
320 TREE_VEC. */
322 static void
323 dump_template_bindings (cxx_pretty_printer *pp, tree parms, tree args,
324 vec<tree, va_gc> *typenames)
326 bool need_semicolon = false;
327 int i;
328 tree t;
330 while (parms)
332 tree p = TREE_VALUE (parms);
333 int lvl = TMPL_PARMS_DEPTH (parms);
334 int arg_idx = 0;
335 int i;
336 tree lvl_args = NULL_TREE;
338 /* Don't crash if we had an invalid argument list. */
339 if (TMPL_ARGS_DEPTH (args) >= lvl)
340 lvl_args = TMPL_ARGS_LEVEL (args, lvl);
342 for (i = 0; i < TREE_VEC_LENGTH (p); ++i)
344 tree arg = NULL_TREE;
346 /* Don't crash if we had an invalid argument list. */
347 if (lvl_args && NUM_TMPL_ARGS (lvl_args) > arg_idx)
348 arg = TREE_VEC_ELT (lvl_args, arg_idx);
350 if (need_semicolon)
351 pp_separate_with_semicolon (pp);
352 dump_template_parameter (pp, TREE_VEC_ELT (p, i),
353 TFF_PLAIN_IDENTIFIER);
354 pp_cxx_whitespace (pp);
355 pp_equal (pp);
356 pp_cxx_whitespace (pp);
357 if (arg)
359 if (ARGUMENT_PACK_P (arg))
360 pp_cxx_left_brace (pp);
361 dump_template_argument (pp, arg, TFF_PLAIN_IDENTIFIER);
362 if (ARGUMENT_PACK_P (arg))
363 pp_cxx_right_brace (pp);
365 else
366 pp_string (pp, M_("<missing>"));
368 ++arg_idx;
369 need_semicolon = true;
372 parms = TREE_CHAIN (parms);
375 /* Don't bother with typenames for a partial instantiation. */
376 if (vec_safe_is_empty (typenames) || uses_template_parms (args))
377 return;
379 /* Don't try to print typenames when we're processing a clone. */
380 if (current_function_decl
381 && !DECL_LANG_SPECIFIC (current_function_decl))
382 return;
384 /* Don't try to do this once cgraph starts throwing away front-end
385 information. */
386 if (at_eof >= 2)
387 return;
389 FOR_EACH_VEC_SAFE_ELT (typenames, i, t)
391 if (need_semicolon)
392 pp_separate_with_semicolon (pp);
393 dump_type (pp, t, TFF_PLAIN_IDENTIFIER);
394 pp_cxx_whitespace (pp);
395 pp_equal (pp);
396 pp_cxx_whitespace (pp);
397 push_deferring_access_checks (dk_no_check);
398 t = tsubst (t, args, tf_none, NULL_TREE);
399 pop_deferring_access_checks ();
400 /* Strip typedefs. We can't just use TFF_CHASE_TYPEDEF because
401 pp_simple_type_specifier doesn't know about it. */
402 t = strip_typedefs (t);
403 dump_type (pp, t, TFF_PLAIN_IDENTIFIER);
407 /* Dump a human-readable equivalent of the alias template
408 specialization of T. */
410 static void
411 dump_alias_template_specialization (cxx_pretty_printer *pp, tree t, int flags)
413 gcc_assert (alias_template_specialization_p (t));
415 tree decl = TYPE_NAME (t);
416 if (!(flags & TFF_UNQUALIFIED_NAME))
417 dump_scope (pp, CP_DECL_CONTEXT (decl), flags);
418 pp_cxx_tree_identifier (pp, DECL_NAME (decl));
419 dump_template_parms (pp, DECL_TEMPLATE_INFO (decl),
420 /*primary=*/false,
421 flags & ~TFF_TEMPLATE_HEADER);
424 /* Dump a human-readable equivalent of TYPE. FLAGS controls the
425 format. */
427 static void
428 dump_type (cxx_pretty_printer *pp, tree t, int flags)
430 if (t == NULL_TREE)
431 return;
433 /* Don't print e.g. "struct mytypedef". */
434 if (TYPE_P (t) && typedef_variant_p (t))
436 tree decl = TYPE_NAME (t);
437 if ((flags & TFF_CHASE_TYPEDEF)
438 || DECL_SELF_REFERENCE_P (decl)
439 || (!flag_pretty_templates
440 && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)))
441 t = strip_typedefs (t);
442 else if (alias_template_specialization_p (t))
444 dump_alias_template_specialization (pp, t, flags);
445 return;
447 else if (same_type_p (t, TREE_TYPE (decl)))
448 t = decl;
449 else
451 pp_cxx_cv_qualifier_seq (pp, t);
452 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
453 return;
457 if (TYPE_PTRMEMFUNC_P (t))
458 goto offset_type;
460 switch (TREE_CODE (t))
462 case LANG_TYPE:
463 if (t == init_list_type_node)
464 pp_string (pp, M_("<brace-enclosed initializer list>"));
465 else if (t == unknown_type_node)
466 pp_string (pp, M_("<unresolved overloaded function type>"));
467 else
469 pp_cxx_cv_qualifier_seq (pp, t);
470 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
472 break;
474 case TREE_LIST:
475 /* A list of function parms. */
476 dump_parameters (pp, t, flags);
477 break;
479 case IDENTIFIER_NODE:
480 pp_cxx_tree_identifier (pp, t);
481 break;
483 case TREE_BINFO:
484 dump_type (pp, BINFO_TYPE (t), flags);
485 break;
487 case RECORD_TYPE:
488 case UNION_TYPE:
489 case ENUMERAL_TYPE:
490 dump_aggr_type (pp, t, flags);
491 break;
493 case TYPE_DECL:
494 if (flags & TFF_CHASE_TYPEDEF)
496 dump_type (pp, DECL_ORIGINAL_TYPE (t)
497 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t), flags);
498 break;
500 /* Fall through. */
502 case TEMPLATE_DECL:
503 case NAMESPACE_DECL:
504 dump_decl (pp, t, flags & ~TFF_DECL_SPECIFIERS);
505 break;
507 case INTEGER_TYPE:
508 case REAL_TYPE:
509 case VOID_TYPE:
510 case BOOLEAN_TYPE:
511 case COMPLEX_TYPE:
512 case VECTOR_TYPE:
513 case FIXED_POINT_TYPE:
514 pp_type_specifier_seq (pp, t);
515 break;
517 case TEMPLATE_TEMPLATE_PARM:
518 /* For parameters inside template signature. */
519 if (TYPE_IDENTIFIER (t))
520 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
521 else
522 pp_cxx_canonical_template_parameter (pp, t);
523 break;
525 case BOUND_TEMPLATE_TEMPLATE_PARM:
527 tree args = TYPE_TI_ARGS (t);
528 pp_cxx_cv_qualifier_seq (pp, t);
529 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
530 pp_cxx_begin_template_argument_list (pp);
531 dump_template_argument_list (pp, args, flags);
532 pp_cxx_end_template_argument_list (pp);
534 break;
536 case TEMPLATE_TYPE_PARM:
537 pp_cxx_cv_qualifier_seq (pp, t);
538 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
539 pp_cxx_constrained_type_spec (pp, c);
540 else if (TYPE_IDENTIFIER (t))
541 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
542 else
543 pp_cxx_canonical_template_parameter
544 (pp, TEMPLATE_TYPE_PARM_INDEX (t));
545 break;
547 /* This is not always necessary for pointers and such, but doing this
548 reduces code size. */
549 case ARRAY_TYPE:
550 case POINTER_TYPE:
551 case REFERENCE_TYPE:
552 case OFFSET_TYPE:
553 offset_type:
554 case FUNCTION_TYPE:
555 case METHOD_TYPE:
557 dump_type_prefix (pp, t, flags);
558 dump_type_suffix (pp, t, flags);
559 break;
561 case TYPENAME_TYPE:
562 if (! (flags & TFF_CHASE_TYPEDEF)
563 && DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
565 dump_decl (pp, TYPE_NAME (t), TFF_PLAIN_IDENTIFIER);
566 break;
568 pp_cxx_cv_qualifier_seq (pp, t);
569 pp_cxx_ws_string (pp,
570 TYPENAME_IS_ENUM_P (t) ? "enum"
571 : TYPENAME_IS_CLASS_P (t) ? "class"
572 : "typename");
573 dump_typename (pp, t, flags);
574 break;
576 case UNBOUND_CLASS_TEMPLATE:
577 if (! (flags & TFF_UNQUALIFIED_NAME))
579 dump_type (pp, TYPE_CONTEXT (t), flags);
580 pp_cxx_colon_colon (pp);
582 pp_cxx_ws_string (pp, "template");
583 dump_type (pp, TYPE_IDENTIFIER (t), flags);
584 break;
586 case TYPEOF_TYPE:
587 pp_cxx_ws_string (pp, "__typeof__");
588 pp_cxx_whitespace (pp);
589 pp_cxx_left_paren (pp);
590 dump_expr (pp, TYPEOF_TYPE_EXPR (t), flags & ~TFF_EXPR_IN_PARENS);
591 pp_cxx_right_paren (pp);
592 break;
594 case UNDERLYING_TYPE:
595 pp_cxx_ws_string (pp, "__underlying_type");
596 pp_cxx_whitespace (pp);
597 pp_cxx_left_paren (pp);
598 dump_expr (pp, UNDERLYING_TYPE_TYPE (t), flags & ~TFF_EXPR_IN_PARENS);
599 pp_cxx_right_paren (pp);
600 break;
602 case TYPE_PACK_EXPANSION:
603 dump_type (pp, PACK_EXPANSION_PATTERN (t), flags);
604 pp_cxx_ws_string (pp, "...");
605 break;
607 case TYPE_ARGUMENT_PACK:
608 dump_template_argument (pp, t, flags);
609 break;
611 case DECLTYPE_TYPE:
612 pp_cxx_ws_string (pp, "decltype");
613 pp_cxx_whitespace (pp);
614 pp_cxx_left_paren (pp);
615 dump_expr (pp, DECLTYPE_TYPE_EXPR (t), flags & ~TFF_EXPR_IN_PARENS);
616 pp_cxx_right_paren (pp);
617 break;
619 case NULLPTR_TYPE:
620 pp_string (pp, "std::nullptr_t");
621 break;
623 default:
624 pp_unsupported_tree (pp, t);
625 /* Fall through. */
627 case ERROR_MARK:
628 pp_string (pp, M_("<type error>"));
629 break;
633 /* Dump a TYPENAME_TYPE. We need to notice when the context is itself
634 a TYPENAME_TYPE. */
636 static void
637 dump_typename (cxx_pretty_printer *pp, tree t, int flags)
639 tree ctx = TYPE_CONTEXT (t);
641 if (TREE_CODE (ctx) == TYPENAME_TYPE)
642 dump_typename (pp, ctx, flags);
643 else
644 dump_type (pp, ctx, flags & ~TFF_CLASS_KEY_OR_ENUM);
645 pp_cxx_colon_colon (pp);
646 dump_decl (pp, TYPENAME_TYPE_FULLNAME (t), flags);
649 /* Return the name of the supplied aggregate, or enumeral type. */
651 const char *
652 class_key_or_enum_as_string (tree t)
654 if (TREE_CODE (t) == ENUMERAL_TYPE)
656 if (SCOPED_ENUM_P (t))
657 return "enum class";
658 else
659 return "enum";
661 else if (TREE_CODE (t) == UNION_TYPE)
662 return "union";
663 else if (TYPE_LANG_SPECIFIC (t) && CLASSTYPE_DECLARED_CLASS (t))
664 return "class";
665 else
666 return "struct";
669 /* Print out a class declaration T under the control of FLAGS,
670 in the form `class foo'. */
672 static void
673 dump_aggr_type (cxx_pretty_printer *pp, tree t, int flags)
675 tree name;
676 const char *variety = class_key_or_enum_as_string (t);
677 int typdef = 0;
678 int tmplate = 0;
680 pp_cxx_cv_qualifier_seq (pp, t);
682 if (flags & TFF_CLASS_KEY_OR_ENUM)
683 pp_cxx_ws_string (pp, variety);
685 name = TYPE_NAME (t);
687 if (name)
689 typdef = (!DECL_ARTIFICIAL (name)
690 /* An alias specialization is not considered to be a
691 typedef. */
692 && !alias_template_specialization_p (t));
694 if ((typdef
695 && ((flags & TFF_CHASE_TYPEDEF)
696 || (!flag_pretty_templates && DECL_LANG_SPECIFIC (name)
697 && DECL_TEMPLATE_INFO (name))))
698 || DECL_SELF_REFERENCE_P (name))
700 t = TYPE_MAIN_VARIANT (t);
701 name = TYPE_NAME (t);
702 typdef = 0;
705 tmplate = !typdef && TREE_CODE (t) != ENUMERAL_TYPE
706 && TYPE_LANG_SPECIFIC (t) && CLASSTYPE_TEMPLATE_INFO (t)
707 && (TREE_CODE (CLASSTYPE_TI_TEMPLATE (t)) != TEMPLATE_DECL
708 || PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
710 if (! (flags & TFF_UNQUALIFIED_NAME))
711 dump_scope (pp, CP_DECL_CONTEXT (name), flags | TFF_SCOPE);
712 flags &= ~TFF_UNQUALIFIED_NAME;
713 if (tmplate)
715 /* Because the template names are mangled, we have to locate
716 the most general template, and use that name. */
717 tree tpl = TYPE_TI_TEMPLATE (t);
719 while (DECL_TEMPLATE_INFO (tpl))
720 tpl = DECL_TI_TEMPLATE (tpl);
721 name = tpl;
723 name = DECL_NAME (name);
726 if (name == 0 || anon_aggrname_p (name))
728 if (flags & TFF_CLASS_KEY_OR_ENUM)
729 pp_string (pp, M_("<unnamed>"));
730 else
731 pp_printf (pp, M_("<unnamed %s>"), variety);
733 else if (LAMBDA_TYPE_P (t))
735 /* A lambda's "type" is essentially its signature. */
736 pp_string (pp, M_("<lambda"));
737 if (lambda_function (t))
738 dump_parameters (pp,
739 FUNCTION_FIRST_USER_PARMTYPE (lambda_function (t)),
740 flags);
741 pp_greater (pp);
743 else
744 pp_cxx_tree_identifier (pp, name);
745 if (tmplate)
746 dump_template_parms (pp, TYPE_TEMPLATE_INFO (t),
747 !CLASSTYPE_USE_TEMPLATE (t),
748 flags & ~TFF_TEMPLATE_HEADER);
751 /* Dump into the obstack the initial part of the output for a given type.
752 This is necessary when dealing with things like functions returning
753 functions. Examples:
755 return type of `int (* fee ())()': pointer -> function -> int. Both
756 pointer (and reference and offset) and function (and member) types must
757 deal with prefix and suffix.
759 Arrays must also do this for DECL nodes, like int a[], and for things like
760 int *[]&. */
762 static void
763 dump_type_prefix (cxx_pretty_printer *pp, tree t, int flags)
765 if (TYPE_PTRMEMFUNC_P (t))
767 t = TYPE_PTRMEMFUNC_FN_TYPE (t);
768 goto offset_type;
771 switch (TREE_CODE (t))
773 case POINTER_TYPE:
774 case REFERENCE_TYPE:
776 tree sub = TREE_TYPE (t);
778 dump_type_prefix (pp, sub, flags);
779 if (TREE_CODE (sub) == ARRAY_TYPE
780 || TREE_CODE (sub) == FUNCTION_TYPE)
782 pp_cxx_whitespace (pp);
783 pp_cxx_left_paren (pp);
784 pp_c_attributes_display (pp, TYPE_ATTRIBUTES (sub));
786 if (TYPE_PTR_P (t))
787 pp_star (pp);
788 else if (TREE_CODE (t) == REFERENCE_TYPE)
790 if (TYPE_REF_IS_RVALUE (t))
791 pp_ampersand_ampersand (pp);
792 else
793 pp_ampersand (pp);
795 pp->padding = pp_before;
796 pp_cxx_cv_qualifier_seq (pp, t);
798 break;
800 case OFFSET_TYPE:
801 offset_type:
802 dump_type_prefix (pp, TREE_TYPE (t), flags);
803 if (TREE_CODE (t) == OFFSET_TYPE) /* pmfs deal with this in d_t_p */
805 pp_maybe_space (pp);
806 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
807 pp_cxx_left_paren (pp);
808 dump_type (pp, TYPE_OFFSET_BASETYPE (t), flags);
809 pp_cxx_colon_colon (pp);
811 pp_cxx_star (pp);
812 pp_cxx_cv_qualifier_seq (pp, t);
813 pp->padding = pp_before;
814 break;
816 /* This can be reached without a pointer when dealing with
817 templates, e.g. std::is_function. */
818 case FUNCTION_TYPE:
819 dump_type_prefix (pp, TREE_TYPE (t), flags);
820 break;
822 case METHOD_TYPE:
823 dump_type_prefix (pp, TREE_TYPE (t), flags);
824 pp_maybe_space (pp);
825 pp_cxx_left_paren (pp);
826 dump_aggr_type (pp, TYPE_METHOD_BASETYPE (t), flags);
827 pp_cxx_colon_colon (pp);
828 break;
830 case ARRAY_TYPE:
831 dump_type_prefix (pp, TREE_TYPE (t), flags);
832 break;
834 case ENUMERAL_TYPE:
835 case IDENTIFIER_NODE:
836 case INTEGER_TYPE:
837 case BOOLEAN_TYPE:
838 case REAL_TYPE:
839 case RECORD_TYPE:
840 case TEMPLATE_TYPE_PARM:
841 case TEMPLATE_TEMPLATE_PARM:
842 case BOUND_TEMPLATE_TEMPLATE_PARM:
843 case TREE_LIST:
844 case TYPE_DECL:
845 case TREE_VEC:
846 case UNION_TYPE:
847 case LANG_TYPE:
848 case VOID_TYPE:
849 case TYPENAME_TYPE:
850 case COMPLEX_TYPE:
851 case VECTOR_TYPE:
852 case TYPEOF_TYPE:
853 case UNDERLYING_TYPE:
854 case DECLTYPE_TYPE:
855 case TYPE_PACK_EXPANSION:
856 case FIXED_POINT_TYPE:
857 case NULLPTR_TYPE:
858 dump_type (pp, t, flags);
859 pp->padding = pp_before;
860 break;
862 default:
863 pp_unsupported_tree (pp, t);
864 /* fall through. */
865 case ERROR_MARK:
866 pp_string (pp, M_("<typeprefixerror>"));
867 break;
871 /* Dump the suffix of type T, under control of FLAGS. This is the part
872 which appears after the identifier (or function parms). */
874 static void
875 dump_type_suffix (cxx_pretty_printer *pp, tree t, int flags)
877 if (TYPE_PTRMEMFUNC_P (t))
878 t = TYPE_PTRMEMFUNC_FN_TYPE (t);
880 switch (TREE_CODE (t))
882 case POINTER_TYPE:
883 case REFERENCE_TYPE:
884 case OFFSET_TYPE:
885 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
886 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
887 pp_cxx_right_paren (pp);
888 if (TREE_CODE (t) == POINTER_TYPE)
889 flags |= TFF_POINTER;
890 dump_type_suffix (pp, TREE_TYPE (t), flags);
891 break;
893 case FUNCTION_TYPE:
894 case METHOD_TYPE:
896 tree arg;
897 if (TREE_CODE (t) == METHOD_TYPE)
898 /* Can only be reached through a pointer. */
899 pp_cxx_right_paren (pp);
900 arg = TYPE_ARG_TYPES (t);
901 if (TREE_CODE (t) == METHOD_TYPE)
902 arg = TREE_CHAIN (arg);
904 /* Function pointers don't have default args. Not in standard C++,
905 anyway; they may in g++, but we'll just pretend otherwise. */
906 dump_parameters (pp, arg, flags & ~TFF_FUNCTION_DEFAULT_ARGUMENTS);
908 pp->padding = pp_before;
909 pp_cxx_cv_qualifiers (pp, type_memfn_quals (t),
910 TREE_CODE (t) == FUNCTION_TYPE
911 && (flags & TFF_POINTER));
912 dump_ref_qualifier (pp, t, flags);
913 if (tx_safe_fn_type_p (t))
914 pp_cxx_ws_string (pp, "transaction_safe");
915 dump_exception_spec (pp, TYPE_RAISES_EXCEPTIONS (t), flags);
916 dump_type_suffix (pp, TREE_TYPE (t), flags);
917 break;
920 case ARRAY_TYPE:
921 pp_maybe_space (pp);
922 pp_cxx_left_bracket (pp);
923 if (tree dtype = TYPE_DOMAIN (t))
925 tree max = TYPE_MAX_VALUE (dtype);
926 /* Zero-length arrays have an upper bound of SIZE_MAX. */
927 if (integer_all_onesp (max))
928 pp_character (pp, '0');
929 else if (tree_fits_shwi_p (max))
930 pp_wide_integer (pp, tree_to_shwi (max) + 1);
931 else
933 STRIP_NOPS (max);
934 if (TREE_CODE (max) == SAVE_EXPR)
935 max = TREE_OPERAND (max, 0);
936 if (TREE_CODE (max) == MINUS_EXPR
937 || TREE_CODE (max) == PLUS_EXPR)
939 max = TREE_OPERAND (max, 0);
940 while (CONVERT_EXPR_P (max))
941 max = TREE_OPERAND (max, 0);
943 else
944 max = fold_build2_loc (input_location,
945 PLUS_EXPR, dtype, max,
946 build_int_cst (dtype, 1));
947 dump_expr (pp, max, flags & ~TFF_EXPR_IN_PARENS);
950 pp_cxx_right_bracket (pp);
951 dump_type_suffix (pp, TREE_TYPE (t), flags);
952 break;
954 case ENUMERAL_TYPE:
955 case IDENTIFIER_NODE:
956 case INTEGER_TYPE:
957 case BOOLEAN_TYPE:
958 case REAL_TYPE:
959 case RECORD_TYPE:
960 case TEMPLATE_TYPE_PARM:
961 case TEMPLATE_TEMPLATE_PARM:
962 case BOUND_TEMPLATE_TEMPLATE_PARM:
963 case TREE_LIST:
964 case TYPE_DECL:
965 case TREE_VEC:
966 case UNION_TYPE:
967 case LANG_TYPE:
968 case VOID_TYPE:
969 case TYPENAME_TYPE:
970 case COMPLEX_TYPE:
971 case VECTOR_TYPE:
972 case TYPEOF_TYPE:
973 case UNDERLYING_TYPE:
974 case DECLTYPE_TYPE:
975 case TYPE_PACK_EXPANSION:
976 case FIXED_POINT_TYPE:
977 case NULLPTR_TYPE:
978 break;
980 default:
981 pp_unsupported_tree (pp, t);
982 case ERROR_MARK:
983 /* Don't mark it here, we should have already done in
984 dump_type_prefix. */
985 break;
989 static void
990 dump_global_iord (cxx_pretty_printer *pp, tree t)
992 const char *p = NULL;
994 if (DECL_GLOBAL_CTOR_P (t))
995 p = M_("(static initializers for %s)");
996 else if (DECL_GLOBAL_DTOR_P (t))
997 p = M_("(static destructors for %s)");
998 else
999 gcc_unreachable ();
1001 pp_printf (pp, p, DECL_SOURCE_FILE (t));
1004 static void
1005 dump_simple_decl (cxx_pretty_printer *pp, tree t, tree type, int flags)
1007 if (flags & TFF_DECL_SPECIFIERS)
1009 if (VAR_P (t) && DECL_DECLARED_CONSTEXPR_P (t))
1011 if (DECL_LANG_SPECIFIC (t) && DECL_DECLARED_CONCEPT_P (t))
1012 pp_cxx_ws_string (pp, "concept");
1013 else
1014 pp_cxx_ws_string (pp, "constexpr");
1016 dump_type_prefix (pp, type, flags & ~TFF_UNQUALIFIED_NAME);
1017 pp_maybe_space (pp);
1019 if (! (flags & TFF_UNQUALIFIED_NAME)
1020 && TREE_CODE (t) != PARM_DECL
1021 && (!DECL_INITIAL (t)
1022 || TREE_CODE (DECL_INITIAL (t)) != TEMPLATE_PARM_INDEX))
1023 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
1024 flags &= ~TFF_UNQUALIFIED_NAME;
1025 if ((flags & TFF_DECL_SPECIFIERS)
1026 && DECL_TEMPLATE_PARM_P (t)
1027 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (t)))
1028 pp_string (pp, "...");
1029 if (DECL_NAME (t))
1031 if (TREE_CODE (t) == FIELD_DECL && DECL_NORMAL_CAPTURE_P (t))
1033 pp_less (pp);
1034 pp_string (pp, IDENTIFIER_POINTER (DECL_NAME (t)) + 2);
1035 pp_string (pp, " capture>");
1037 else
1038 dump_decl (pp, DECL_NAME (t), flags);
1040 else if (DECL_DECOMPOSITION_P (t))
1041 pp_string (pp, M_("<structured bindings>"));
1042 else
1043 pp_string (pp, M_("<anonymous>"));
1044 if (flags & TFF_DECL_SPECIFIERS)
1045 dump_type_suffix (pp, type, flags);
1048 /* Print an IDENTIFIER_NODE that is the name of a declaration. */
1050 static void
1051 dump_decl_name (cxx_pretty_printer *pp, tree t, int flags)
1053 /* These special cases are duplicated here so that other functions
1054 can feed identifiers to error and get them demangled properly. */
1055 if (IDENTIFIER_CONV_OP_P (t))
1057 pp_cxx_ws_string (pp, "operator");
1058 /* Not exactly IDENTIFIER_TYPE_VALUE. */
1059 dump_type (pp, TREE_TYPE (t), flags);
1060 return;
1062 if (dguide_name_p (t))
1064 dump_decl (pp, CLASSTYPE_TI_TEMPLATE (TREE_TYPE (t)),
1065 TFF_UNQUALIFIED_NAME);
1066 return;
1069 const char *str = IDENTIFIER_POINTER (t);
1070 if (!strncmp (str, "_ZGR", 3))
1072 pp_cxx_ws_string (pp, "<temporary>");
1073 return;
1076 pp_cxx_tree_identifier (pp, t);
1079 /* Dump a human readable string for the decl T under control of FLAGS. */
1081 static void
1082 dump_decl (cxx_pretty_printer *pp, tree t, int flags)
1084 if (t == NULL_TREE)
1085 return;
1087 /* If doing Objective-C++, give Objective-C a chance to demangle
1088 Objective-C method names. */
1089 if (c_dialect_objc ())
1091 const char *demangled = objc_maybe_printable_name (t, flags);
1092 if (demangled)
1094 pp_string (pp, demangled);
1095 return;
1099 switch (TREE_CODE (t))
1101 case TYPE_DECL:
1102 /* Don't say 'typedef class A' */
1103 if (DECL_ARTIFICIAL (t) && !DECL_SELF_REFERENCE_P (t))
1105 if ((flags & TFF_DECL_SPECIFIERS)
1106 && TREE_CODE (TREE_TYPE (t)) == TEMPLATE_TYPE_PARM)
1108 /* Say `class T' not just `T'. */
1109 pp_cxx_ws_string (pp, "class");
1111 /* Emit the `...' for a parameter pack. */
1112 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
1113 pp_cxx_ws_string (pp, "...");
1116 dump_type (pp, TREE_TYPE (t), flags);
1117 break;
1119 if (TYPE_DECL_ALIAS_P (t)
1120 && (flags & TFF_DECL_SPECIFIERS
1121 || flags & TFF_CLASS_KEY_OR_ENUM))
1123 pp_cxx_ws_string (pp, "using");
1124 dump_decl (pp, DECL_NAME (t), flags);
1125 pp_cxx_whitespace (pp);
1126 pp_cxx_ws_string (pp, "=");
1127 pp_cxx_whitespace (pp);
1128 dump_type (pp, (DECL_ORIGINAL_TYPE (t)
1129 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t)),
1130 flags);
1131 break;
1133 if ((flags & TFF_DECL_SPECIFIERS)
1134 && !DECL_SELF_REFERENCE_P (t))
1135 pp_cxx_ws_string (pp, "typedef");
1136 dump_simple_decl (pp, t, DECL_ORIGINAL_TYPE (t)
1137 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t),
1138 flags);
1139 break;
1141 case VAR_DECL:
1142 if (DECL_NAME (t) && VTABLE_NAME_P (DECL_NAME (t)))
1144 pp_string (pp, M_("vtable for "));
1145 gcc_assert (TYPE_P (DECL_CONTEXT (t)));
1146 dump_type (pp, DECL_CONTEXT (t), flags);
1147 break;
1149 /* Fall through. */
1150 case FIELD_DECL:
1151 case PARM_DECL:
1152 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1154 /* Handle variable template specializations. */
1155 if (VAR_P (t)
1156 && DECL_LANG_SPECIFIC (t)
1157 && DECL_TEMPLATE_INFO (t)
1158 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1160 pp_cxx_begin_template_argument_list (pp);
1161 tree args = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1162 dump_template_argument_list (pp, args, flags);
1163 pp_cxx_end_template_argument_list (pp);
1165 break;
1167 case RESULT_DECL:
1168 pp_string (pp, M_("<return value> "));
1169 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1170 break;
1172 case NAMESPACE_DECL:
1173 if (flags & TFF_DECL_SPECIFIERS)
1174 pp->declaration (t);
1175 else
1177 if (! (flags & TFF_UNQUALIFIED_NAME))
1178 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
1179 flags &= ~TFF_UNQUALIFIED_NAME;
1180 if (DECL_NAME (t) == NULL_TREE)
1182 if (!(pp->flags & pp_c_flag_gnu_v3))
1183 pp_cxx_ws_string (pp, M_("{anonymous}"));
1184 else
1185 pp_cxx_ws_string (pp, M_("(anonymous namespace)"));
1187 else
1188 pp_cxx_tree_identifier (pp, DECL_NAME (t));
1190 break;
1192 case SCOPE_REF:
1193 dump_type (pp, TREE_OPERAND (t, 0), flags);
1194 pp_cxx_colon_colon (pp);
1195 dump_decl (pp, TREE_OPERAND (t, 1), TFF_UNQUALIFIED_NAME);
1196 break;
1198 case ARRAY_REF:
1199 dump_decl (pp, TREE_OPERAND (t, 0), flags);
1200 pp_cxx_left_bracket (pp);
1201 dump_decl (pp, TREE_OPERAND (t, 1), flags);
1202 pp_cxx_right_bracket (pp);
1203 break;
1205 case ARRAY_NOTATION_REF:
1206 dump_decl (pp, ARRAY_NOTATION_ARRAY (t), flags | TFF_EXPR_IN_PARENS);
1207 pp_cxx_left_bracket (pp);
1208 dump_decl (pp, ARRAY_NOTATION_START (t), flags | TFF_EXPR_IN_PARENS);
1209 pp_colon (pp);
1210 dump_decl (pp, ARRAY_NOTATION_LENGTH (t), flags | TFF_EXPR_IN_PARENS);
1211 pp_colon (pp);
1212 dump_decl (pp, ARRAY_NOTATION_STRIDE (t), flags | TFF_EXPR_IN_PARENS);
1213 pp_cxx_right_bracket (pp);
1214 break;
1216 /* So that we can do dump_decl on an aggr type. */
1217 case RECORD_TYPE:
1218 case UNION_TYPE:
1219 case ENUMERAL_TYPE:
1220 dump_type (pp, t, flags);
1221 break;
1223 case BIT_NOT_EXPR:
1224 /* This is a pseudo destructor call which has not been folded into
1225 a PSEUDO_DTOR_EXPR yet. */
1226 pp_cxx_complement (pp);
1227 dump_type (pp, TREE_OPERAND (t, 0), flags);
1228 break;
1230 case TYPE_EXPR:
1231 gcc_unreachable ();
1232 break;
1234 case IDENTIFIER_NODE:
1235 dump_decl_name (pp, t, flags);
1236 break;
1238 case OVERLOAD:
1239 if (!OVL_SINGLE_P (t))
1241 tree ctx = ovl_scope (t);
1242 if (ctx != global_namespace)
1244 if (TYPE_P (ctx))
1245 dump_type (pp, ctx, flags);
1246 else
1247 dump_decl (pp, ctx, flags);
1248 pp_cxx_colon_colon (pp);
1250 dump_decl (pp, OVL_NAME (t), flags);
1251 break;
1254 /* If there's only one function, just treat it like an ordinary
1255 FUNCTION_DECL. */
1256 t = OVL_FIRST (t);
1257 /* Fall through. */
1259 case FUNCTION_DECL:
1260 if (! DECL_LANG_SPECIFIC (t))
1262 if (DECL_ABSTRACT_ORIGIN (t)
1263 && DECL_ABSTRACT_ORIGIN (t) != t)
1264 dump_decl (pp, DECL_ABSTRACT_ORIGIN (t), flags);
1265 else
1266 dump_function_name (pp, t, flags);
1268 else if (DECL_GLOBAL_CTOR_P (t) || DECL_GLOBAL_DTOR_P (t))
1269 dump_global_iord (pp, t);
1270 else
1271 dump_function_decl (pp, t, flags);
1272 break;
1274 case TEMPLATE_DECL:
1275 dump_template_decl (pp, t, flags);
1276 break;
1278 case TEMPLATE_ID_EXPR:
1280 tree name = TREE_OPERAND (t, 0);
1281 tree args = TREE_OPERAND (t, 1);
1283 if (!identifier_p (name))
1284 name = OVL_NAME (name);
1285 dump_decl (pp, name, flags);
1286 pp_cxx_begin_template_argument_list (pp);
1287 if (args == error_mark_node)
1288 pp_string (pp, M_("<template arguments error>"));
1289 else if (args)
1290 dump_template_argument_list
1291 (pp, args, flags|TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS);
1292 pp_cxx_end_template_argument_list (pp);
1294 break;
1296 case LABEL_DECL:
1297 pp_cxx_tree_identifier (pp, DECL_NAME (t));
1298 break;
1300 case CONST_DECL:
1301 if ((TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == ENUMERAL_TYPE)
1302 || (DECL_INITIAL (t) &&
1303 TREE_CODE (DECL_INITIAL (t)) == TEMPLATE_PARM_INDEX))
1304 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1305 else if (DECL_NAME (t))
1306 dump_decl (pp, DECL_NAME (t), flags);
1307 else if (DECL_INITIAL (t))
1308 dump_expr (pp, DECL_INITIAL (t), flags | TFF_EXPR_IN_PARENS);
1309 else
1310 pp_string (pp, M_("<enumerator>"));
1311 break;
1313 case USING_DECL:
1315 pp_cxx_ws_string (pp, "using");
1316 tree scope = USING_DECL_SCOPE (t);
1317 bool variadic = false;
1318 if (PACK_EXPANSION_P (scope))
1320 scope = PACK_EXPANSION_PATTERN (scope);
1321 variadic = true;
1323 dump_type (pp, scope, flags);
1324 pp_cxx_colon_colon (pp);
1325 dump_decl (pp, DECL_NAME (t), flags);
1326 if (variadic)
1327 pp_cxx_ws_string (pp, "...");
1329 break;
1331 case STATIC_ASSERT:
1332 pp->declaration (t);
1333 break;
1335 case BASELINK:
1336 dump_decl (pp, BASELINK_FUNCTIONS (t), flags);
1337 break;
1339 case NON_DEPENDENT_EXPR:
1340 dump_expr (pp, t, flags);
1341 break;
1343 case TEMPLATE_TYPE_PARM:
1344 if (flags & TFF_DECL_SPECIFIERS)
1345 pp->declaration (t);
1346 else
1347 pp->type_id (t);
1348 break;
1350 case UNBOUND_CLASS_TEMPLATE:
1351 case TYPE_PACK_EXPANSION:
1352 case TREE_BINFO:
1353 dump_type (pp, t, flags);
1354 break;
1356 default:
1357 pp_unsupported_tree (pp, t);
1358 /* Fall through. */
1360 case ERROR_MARK:
1361 pp_string (pp, M_("<declaration error>"));
1362 break;
1366 /* Dump a template declaration T under control of FLAGS. This means the
1367 'template <...> leaders plus the 'class X' or 'void fn(...)' part. */
1369 static void
1370 dump_template_decl (cxx_pretty_printer *pp, tree t, int flags)
1372 tree orig_parms = DECL_TEMPLATE_PARMS (t);
1373 tree parms;
1374 int i;
1376 if (flags & TFF_TEMPLATE_HEADER)
1378 for (parms = orig_parms = nreverse (orig_parms);
1379 parms;
1380 parms = TREE_CHAIN (parms))
1382 tree inner_parms = INNERMOST_TEMPLATE_PARMS (parms);
1383 int len = TREE_VEC_LENGTH (inner_parms);
1385 if (len == 0)
1387 /* Skip over the dummy template levels of a template template
1388 parm. */
1389 gcc_assert (TREE_CODE (TREE_TYPE (t)) == TEMPLATE_TEMPLATE_PARM);
1390 continue;
1393 pp_cxx_ws_string (pp, "template");
1394 pp_cxx_begin_template_argument_list (pp);
1396 /* If we've shown the template prefix, we'd better show the
1397 parameters' and decl's type too. */
1398 flags |= TFF_DECL_SPECIFIERS;
1400 for (i = 0; i < len; i++)
1402 if (i)
1403 pp_separate_with_comma (pp);
1404 dump_template_parameter (pp, TREE_VEC_ELT (inner_parms, i),
1405 flags);
1407 pp_cxx_end_template_argument_list (pp);
1408 pp_cxx_whitespace (pp);
1410 nreverse(orig_parms);
1412 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
1414 /* Say `template<arg> class TT' not just `template<arg> TT'. */
1415 pp_cxx_ws_string (pp, "class");
1417 /* If this is a parameter pack, print the ellipsis. */
1418 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
1419 pp_cxx_ws_string (pp, "...");
1422 /* Only print the requirements if we're also printing
1423 the template header. */
1424 if (flag_concepts)
1425 if (tree ci = get_constraints (t))
1426 if (check_constraint_info (ci))
1427 if (tree reqs = CI_TEMPLATE_REQS (ci))
1429 pp_cxx_requires_clause (pp, reqs);
1430 pp_cxx_whitespace (pp);
1435 if (DECL_CLASS_TEMPLATE_P (t))
1436 dump_type (pp, TREE_TYPE (t),
1437 ((flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1438 | (flags & TFF_DECL_SPECIFIERS ? TFF_CLASS_KEY_OR_ENUM : 0)));
1439 else if (DECL_TEMPLATE_RESULT (t)
1440 && (VAR_P (DECL_TEMPLATE_RESULT (t))
1441 /* Alias template. */
1442 || DECL_TYPE_TEMPLATE_P (t)))
1443 dump_decl (pp, DECL_TEMPLATE_RESULT (t), flags | TFF_TEMPLATE_NAME);
1444 else
1446 gcc_assert (TREE_TYPE (t));
1447 switch (NEXT_CODE (t))
1449 case METHOD_TYPE:
1450 case FUNCTION_TYPE:
1451 dump_function_decl (pp, t, flags | TFF_TEMPLATE_NAME);
1452 break;
1453 default:
1454 /* This case can occur with some invalid code. */
1455 dump_type (pp, TREE_TYPE (t),
1456 (flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1457 | (flags & TFF_DECL_SPECIFIERS
1458 ? TFF_CLASS_KEY_OR_ENUM : 0));
1463 /* find_typenames looks through the type of the function template T
1464 and returns a vec containing any typedefs, decltypes or TYPENAME_TYPEs
1465 it finds. */
1467 struct find_typenames_t
1469 hash_set<tree> *p_set;
1470 vec<tree, va_gc> *typenames;
1473 static tree
1474 find_typenames_r (tree *tp, int *walk_subtrees, void *data)
1476 struct find_typenames_t *d = (struct find_typenames_t *)data;
1477 tree mv = NULL_TREE;
1479 if (TYPE_P (*tp) && is_typedef_decl (TYPE_NAME (*tp)))
1480 /* Add the type of the typedef without any additional cv-quals. */
1481 mv = TREE_TYPE (TYPE_NAME (*tp));
1482 else if (TREE_CODE (*tp) == TYPENAME_TYPE
1483 || TREE_CODE (*tp) == DECLTYPE_TYPE)
1484 /* Add the typename without any cv-qualifiers. */
1485 mv = TYPE_MAIN_VARIANT (*tp);
1487 if (TREE_CODE (*tp) == TYPE_PACK_EXPANSION)
1489 /* Don't mess with parameter packs since we don't remember
1490 the pack expansion context for a particular typename. */
1491 *walk_subtrees = false;
1492 return NULL_TREE;
1495 if (mv && (mv == *tp || !d->p_set->add (mv)))
1496 vec_safe_push (d->typenames, mv);
1498 /* Search into class template arguments, which cp_walk_subtrees
1499 doesn't do. */
1500 if (CLASS_TYPE_P (*tp) && CLASSTYPE_TEMPLATE_INFO (*tp))
1501 cp_walk_tree (&CLASSTYPE_TI_ARGS (*tp), find_typenames_r,
1502 data, d->p_set);
1504 return NULL_TREE;
1507 static vec<tree, va_gc> *
1508 find_typenames (tree t)
1510 struct find_typenames_t ft;
1511 ft.p_set = new hash_set<tree>;
1512 ft.typenames = NULL;
1513 cp_walk_tree (&TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
1514 find_typenames_r, &ft, ft.p_set);
1515 delete ft.p_set;
1516 return ft.typenames;
1519 /* Output the "[with ...]" clause for a template instantiation T iff
1520 TEMPLATE_PARMS, TEMPLATE_ARGS and FLAGS are suitable. T may be NULL if
1521 formatting a deduction/substitution diagnostic rather than an
1522 instantiation. */
1524 static void
1525 dump_substitution (cxx_pretty_printer *pp,
1526 tree t, tree template_parms, tree template_args,
1527 int flags)
1529 if (template_parms != NULL_TREE && template_args != NULL_TREE
1530 && !(flags & TFF_NO_TEMPLATE_BINDINGS))
1532 vec<tree, va_gc> *typenames = t ? find_typenames (t) : NULL;
1533 pp_cxx_whitespace (pp);
1534 pp_cxx_left_bracket (pp);
1535 pp->translate_string ("with");
1536 pp_cxx_whitespace (pp);
1537 dump_template_bindings (pp, template_parms, template_args, typenames);
1538 pp_cxx_right_bracket (pp);
1542 /* Dump the lambda function FN including its 'mutable' qualifier and any
1543 template bindings. */
1545 static void
1546 dump_lambda_function (cxx_pretty_printer *pp,
1547 tree fn, tree template_parms, tree template_args,
1548 int flags)
1550 /* A lambda's signature is essentially its "type". */
1551 dump_type (pp, DECL_CONTEXT (fn), flags);
1552 if (!(TYPE_QUALS (class_of_this_parm (TREE_TYPE (fn))) & TYPE_QUAL_CONST))
1554 pp->padding = pp_before;
1555 pp_c_ws_string (pp, "mutable");
1557 dump_substitution (pp, fn, template_parms, template_args, flags);
1560 /* Pretty print a function decl. There are several ways we want to print a
1561 function declaration. The TFF_ bits in FLAGS tells us how to behave.
1562 As error can only apply the '#' flag once to give 0 and 1 for V, there
1563 is %D which doesn't print the throw specs, and %F which does. */
1565 static void
1566 dump_function_decl (cxx_pretty_printer *pp, tree t, int flags)
1568 tree fntype;
1569 tree parmtypes;
1570 tree cname = NULL_TREE;
1571 tree template_args = NULL_TREE;
1572 tree template_parms = NULL_TREE;
1573 int show_return = flags & TFF_RETURN_TYPE || flags & TFF_DECL_SPECIFIERS;
1574 int do_outer_scope = ! (flags & TFF_UNQUALIFIED_NAME);
1575 tree exceptions;
1576 bool constexpr_p;
1577 tree ret = NULL_TREE;
1579 flags &= ~(TFF_UNQUALIFIED_NAME | TFF_TEMPLATE_NAME);
1580 if (TREE_CODE (t) == TEMPLATE_DECL)
1581 t = DECL_TEMPLATE_RESULT (t);
1583 /* Save the exceptions, in case t is a specialization and we are
1584 emitting an error about incompatible specifications. */
1585 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (t));
1587 /* Likewise for the constexpr specifier, in case t is a specialization. */
1588 constexpr_p = DECL_DECLARED_CONSTEXPR_P (t);
1590 /* Pretty print template instantiations only. */
1591 if (DECL_USE_TEMPLATE (t) && DECL_TEMPLATE_INFO (t)
1592 && !(flags & TFF_NO_TEMPLATE_BINDINGS)
1593 && flag_pretty_templates)
1595 tree tmpl;
1597 template_args = DECL_TI_ARGS (t);
1598 tmpl = most_general_template (t);
1599 if (tmpl && TREE_CODE (tmpl) == TEMPLATE_DECL)
1601 template_parms = DECL_TEMPLATE_PARMS (tmpl);
1602 t = tmpl;
1606 if (DECL_NAME (t) && LAMBDA_FUNCTION_P (t))
1607 return dump_lambda_function (pp, t, template_parms, template_args, flags);
1609 fntype = TREE_TYPE (t);
1610 parmtypes = FUNCTION_FIRST_USER_PARMTYPE (t);
1612 if (DECL_CLASS_SCOPE_P (t))
1613 cname = DECL_CONTEXT (t);
1614 /* This is for partially instantiated template methods. */
1615 else if (TREE_CODE (fntype) == METHOD_TYPE)
1616 cname = TREE_TYPE (TREE_VALUE (parmtypes));
1618 if (flags & TFF_DECL_SPECIFIERS)
1620 if (DECL_STATIC_FUNCTION_P (t))
1621 pp_cxx_ws_string (pp, "static");
1622 else if (DECL_VIRTUAL_P (t))
1623 pp_cxx_ws_string (pp, "virtual");
1625 if (constexpr_p)
1627 if (DECL_DECLARED_CONCEPT_P (t))
1628 pp_cxx_ws_string (pp, "concept");
1629 else
1630 pp_cxx_ws_string (pp, "constexpr");
1634 /* Print the return type? */
1635 if (show_return)
1636 show_return = (!DECL_CONV_FN_P (t) && !DECL_CONSTRUCTOR_P (t)
1637 && !DECL_DESTRUCTOR_P (t) && !deduction_guide_p (t));
1638 if (show_return)
1640 ret = fndecl_declared_return_type (t);
1641 dump_type_prefix (pp, ret, flags);
1644 /* Print the function name. */
1645 if (!do_outer_scope)
1646 /* Nothing. */;
1647 else if (cname)
1649 dump_type (pp, cname, flags);
1650 pp_cxx_colon_colon (pp);
1652 else
1653 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
1655 dump_function_name (pp, t, flags);
1657 if (!(flags & TFF_NO_FUNCTION_ARGUMENTS))
1659 dump_parameters (pp, parmtypes, flags);
1661 if (TREE_CODE (fntype) == METHOD_TYPE)
1663 pp->padding = pp_before;
1664 pp_cxx_cv_qualifier_seq (pp, class_of_this_parm (fntype));
1665 dump_ref_qualifier (pp, fntype, flags);
1668 if (tx_safe_fn_type_p (fntype))
1670 pp->padding = pp_before;
1671 pp_cxx_ws_string (pp, "transaction_safe");
1674 if (flags & TFF_EXCEPTION_SPECIFICATION)
1676 pp->padding = pp_before;
1677 dump_exception_spec (pp, exceptions, flags);
1680 if (show_return)
1681 dump_type_suffix (pp, ret, flags);
1682 else if (deduction_guide_p (t))
1684 pp_cxx_ws_string (pp, "->");
1685 dump_type (pp, TREE_TYPE (TREE_TYPE (t)), flags);
1688 if (flag_concepts)
1689 if (tree ci = get_constraints (t))
1690 if (tree reqs = CI_DECLARATOR_REQS (ci))
1691 pp_cxx_requires_clause (pp, reqs);
1693 dump_substitution (pp, t, template_parms, template_args, flags);
1695 if (tree base = DECL_INHERITED_CTOR_BASE (t))
1697 pp_cxx_ws_string (pp, "[inherited from");
1698 dump_type (pp, base, TFF_PLAIN_IDENTIFIER);
1699 pp_character (pp, ']');
1702 else if (template_args)
1704 bool need_comma = false;
1705 int i;
1706 pp_cxx_begin_template_argument_list (pp);
1707 template_args = INNERMOST_TEMPLATE_ARGS (template_args);
1708 for (i = 0; i < TREE_VEC_LENGTH (template_args); ++i)
1710 tree arg = TREE_VEC_ELT (template_args, i);
1711 if (need_comma)
1712 pp_separate_with_comma (pp);
1713 if (ARGUMENT_PACK_P (arg))
1714 pp_cxx_left_brace (pp);
1715 dump_template_argument (pp, arg, TFF_PLAIN_IDENTIFIER);
1716 if (ARGUMENT_PACK_P (arg))
1717 pp_cxx_right_brace (pp);
1718 need_comma = true;
1720 pp_cxx_end_template_argument_list (pp);
1724 /* Print a parameter list. If this is for a member function, the
1725 member object ptr (and any other hidden args) should have
1726 already been removed. */
1728 static void
1729 dump_parameters (cxx_pretty_printer *pp, tree parmtypes, int flags)
1731 int first = 1;
1732 flags &= ~TFF_SCOPE;
1733 pp_cxx_left_paren (pp);
1735 for (first = 1; parmtypes != void_list_node;
1736 parmtypes = TREE_CHAIN (parmtypes))
1738 if (!first)
1739 pp_separate_with_comma (pp);
1740 first = 0;
1741 if (!parmtypes)
1743 pp_cxx_ws_string (pp, "...");
1744 break;
1747 dump_type (pp, TREE_VALUE (parmtypes), flags);
1749 if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && TREE_PURPOSE (parmtypes))
1751 pp_cxx_whitespace (pp);
1752 pp_equal (pp);
1753 pp_cxx_whitespace (pp);
1754 dump_expr (pp, TREE_PURPOSE (parmtypes), flags | TFF_EXPR_IN_PARENS);
1758 pp_cxx_right_paren (pp);
1761 /* Print ref-qualifier of a FUNCTION_TYPE or METHOD_TYPE. FLAGS are ignored. */
1763 static void
1764 dump_ref_qualifier (cxx_pretty_printer *pp, tree t, int flags ATTRIBUTE_UNUSED)
1766 if (FUNCTION_REF_QUALIFIED (t))
1768 pp->padding = pp_before;
1769 if (FUNCTION_RVALUE_QUALIFIED (t))
1770 pp_cxx_ws_string (pp, "&&");
1771 else
1772 pp_cxx_ws_string (pp, "&");
1776 /* Print an exception specification. T is the exception specification. */
1778 static void
1779 dump_exception_spec (cxx_pretty_printer *pp, tree t, int flags)
1781 if (t && TREE_PURPOSE (t))
1783 pp_cxx_ws_string (pp, "noexcept");
1784 if (!integer_onep (TREE_PURPOSE (t)))
1786 pp_cxx_whitespace (pp);
1787 pp_cxx_left_paren (pp);
1788 if (DEFERRED_NOEXCEPT_SPEC_P (t))
1789 pp_cxx_ws_string (pp, "<uninstantiated>");
1790 else
1791 dump_expr (pp, TREE_PURPOSE (t), flags);
1792 pp_cxx_right_paren (pp);
1795 else if (t)
1797 pp_cxx_ws_string (pp, "throw");
1798 pp_cxx_whitespace (pp);
1799 pp_cxx_left_paren (pp);
1800 if (TREE_VALUE (t) != NULL_TREE)
1801 while (1)
1803 dump_type (pp, TREE_VALUE (t), flags);
1804 t = TREE_CHAIN (t);
1805 if (!t)
1806 break;
1807 pp_separate_with_comma (pp);
1809 pp_cxx_right_paren (pp);
1813 /* Handle the function name for a FUNCTION_DECL node, grokking operators
1814 and destructors properly. */
1816 static void
1817 dump_function_name (cxx_pretty_printer *pp, tree t, int flags)
1819 tree name = DECL_NAME (t);
1821 /* We can get here with a decl that was synthesized by language-
1822 independent machinery (e.g. coverage.c) in which case it won't
1823 have a lang_specific structure attached and DECL_CONSTRUCTOR_P
1824 will crash. In this case it is safe just to print out the
1825 literal name. */
1826 if (!DECL_LANG_SPECIFIC (t))
1828 pp_cxx_tree_identifier (pp, name);
1829 return;
1832 if (TREE_CODE (t) == TEMPLATE_DECL)
1833 t = DECL_TEMPLATE_RESULT (t);
1835 /* Don't let the user see __comp_ctor et al. */
1836 if (DECL_CONSTRUCTOR_P (t)
1837 || DECL_DESTRUCTOR_P (t))
1839 if (LAMBDA_TYPE_P (DECL_CONTEXT (t)))
1840 name = get_identifier ("<lambda>");
1841 else if (TYPE_UNNAMED_P (DECL_CONTEXT (t)))
1842 name = get_identifier ("<constructor>");
1843 else
1844 name = constructor_name (DECL_CONTEXT (t));
1847 if (DECL_DESTRUCTOR_P (t))
1849 pp_cxx_complement (pp);
1850 dump_decl (pp, name, TFF_PLAIN_IDENTIFIER);
1852 else if (DECL_CONV_FN_P (t))
1854 /* This cannot use the hack that the operator's return
1855 type is stashed off of its name because it may be
1856 used for error reporting. In the case of conflicting
1857 declarations, both will have the same name, yet
1858 the types will be different, hence the TREE_TYPE field
1859 of the first name will be clobbered by the second. */
1860 pp_cxx_ws_string (pp, "operator");
1861 dump_type (pp, TREE_TYPE (TREE_TYPE (t)), flags);
1863 else
1864 dump_decl (pp, name, flags);
1866 if (DECL_TEMPLATE_INFO (t)
1867 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (t)
1868 && (TREE_CODE (DECL_TI_TEMPLATE (t)) != TEMPLATE_DECL
1869 || PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
1870 dump_template_parms (pp, DECL_TEMPLATE_INFO (t), !DECL_USE_TEMPLATE (t),
1871 flags);
1874 /* Dump the template parameters from the template info INFO under control of
1875 FLAGS. PRIMARY indicates whether this is a primary template decl, or
1876 specialization (partial or complete). For partial specializations we show
1877 the specialized parameter values. For a primary template we show no
1878 decoration. */
1880 static void
1881 dump_template_parms (cxx_pretty_printer *pp, tree info,
1882 int primary, int flags)
1884 tree args = info ? TI_ARGS (info) : NULL_TREE;
1886 if (primary && flags & TFF_TEMPLATE_NAME)
1887 return;
1888 flags &= ~(TFF_CLASS_KEY_OR_ENUM | TFF_TEMPLATE_NAME);
1889 pp_cxx_begin_template_argument_list (pp);
1891 /* Be careful only to print things when we have them, so as not
1892 to crash producing error messages. */
1893 if (args && !primary)
1895 int len, ix;
1896 len = get_non_default_template_args_count (args, flags);
1898 args = INNERMOST_TEMPLATE_ARGS (args);
1899 for (ix = 0; ix != len; ix++)
1901 tree arg = TREE_VEC_ELT (args, ix);
1903 /* Only print a comma if we know there is an argument coming. In
1904 the case of an empty template argument pack, no actual
1905 argument will be printed. */
1906 if (ix
1907 && (!ARGUMENT_PACK_P (arg)
1908 || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) > 0))
1909 pp_separate_with_comma (pp);
1911 if (!arg)
1912 pp_string (pp, M_("<template parameter error>"));
1913 else
1914 dump_template_argument (pp, arg, flags);
1917 else if (primary)
1919 tree tpl = TI_TEMPLATE (info);
1920 tree parms = DECL_TEMPLATE_PARMS (tpl);
1921 int len, ix;
1923 parms = TREE_CODE (parms) == TREE_LIST ? TREE_VALUE (parms) : NULL_TREE;
1924 len = parms ? TREE_VEC_LENGTH (parms) : 0;
1926 for (ix = 0; ix != len; ix++)
1928 tree parm;
1930 if (TREE_VEC_ELT (parms, ix) == error_mark_node)
1932 pp_string (pp, M_("<template parameter error>"));
1933 continue;
1936 parm = TREE_VALUE (TREE_VEC_ELT (parms, ix));
1938 if (ix)
1939 pp_separate_with_comma (pp);
1941 dump_decl (pp, parm, flags & ~TFF_DECL_SPECIFIERS);
1944 pp_cxx_end_template_argument_list (pp);
1947 /* Print out the arguments of CALL_EXPR T as a parenthesized list using
1948 flags FLAGS. Skip over the first argument if SKIPFIRST is true. */
1950 static void
1951 dump_call_expr_args (cxx_pretty_printer *pp, tree t, int flags, bool skipfirst)
1953 tree arg;
1954 call_expr_arg_iterator iter;
1956 pp_cxx_left_paren (pp);
1957 FOR_EACH_CALL_EXPR_ARG (arg, iter, t)
1959 if (skipfirst)
1960 skipfirst = false;
1961 else
1963 dump_expr (pp, arg, flags | TFF_EXPR_IN_PARENS);
1964 if (more_call_expr_args_p (&iter))
1965 pp_separate_with_comma (pp);
1968 pp_cxx_right_paren (pp);
1971 /* Print out the arguments of AGGR_INIT_EXPR T as a parenthesized list
1972 using flags FLAGS. Skip over the first argument if SKIPFIRST is
1973 true. */
1975 static void
1976 dump_aggr_init_expr_args (cxx_pretty_printer *pp, tree t, int flags,
1977 bool skipfirst)
1979 tree arg;
1980 aggr_init_expr_arg_iterator iter;
1982 pp_cxx_left_paren (pp);
1983 FOR_EACH_AGGR_INIT_EXPR_ARG (arg, iter, t)
1985 if (skipfirst)
1986 skipfirst = false;
1987 else
1989 dump_expr (pp, arg, flags | TFF_EXPR_IN_PARENS);
1990 if (more_aggr_init_expr_args_p (&iter))
1991 pp_separate_with_comma (pp);
1994 pp_cxx_right_paren (pp);
1997 /* Print out a list of initializers (subr of dump_expr). */
1999 static void
2000 dump_expr_list (cxx_pretty_printer *pp, tree l, int flags)
2002 while (l)
2004 dump_expr (pp, TREE_VALUE (l), flags | TFF_EXPR_IN_PARENS);
2005 l = TREE_CHAIN (l);
2006 if (l)
2007 pp_separate_with_comma (pp);
2011 /* Print out a vector of initializers (subr of dump_expr). */
2013 static void
2014 dump_expr_init_vec (cxx_pretty_printer *pp, vec<constructor_elt, va_gc> *v,
2015 int flags)
2017 unsigned HOST_WIDE_INT idx;
2018 tree value;
2020 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
2022 dump_expr (pp, value, flags | TFF_EXPR_IN_PARENS);
2023 if (idx != v->length () - 1)
2024 pp_separate_with_comma (pp);
2029 /* We've gotten an indirect REFERENCE (an OBJ_TYPE_REF) to a virtual
2030 function. Resolve it to a close relative -- in the sense of static
2031 type -- variant being overridden. That is close to what was written in
2032 the source code. Subroutine of dump_expr. */
2034 static tree
2035 resolve_virtual_fun_from_obj_type_ref (tree ref)
2037 tree obj_type = TREE_TYPE (OBJ_TYPE_REF_OBJECT (ref));
2038 HOST_WIDE_INT index = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (ref));
2039 tree fun = BINFO_VIRTUALS (TYPE_BINFO (TREE_TYPE (obj_type)));
2040 while (index)
2042 fun = TREE_CHAIN (fun);
2043 index -= (TARGET_VTABLE_USES_DESCRIPTORS
2044 ? TARGET_VTABLE_USES_DESCRIPTORS : 1);
2047 return BV_FN (fun);
2050 /* Print out an expression E under control of FLAGS. */
2052 static void
2053 dump_expr (cxx_pretty_printer *pp, tree t, int flags)
2055 tree op;
2057 if (t == 0)
2058 return;
2060 if (STATEMENT_CLASS_P (t))
2062 pp_cxx_ws_string (pp, M_("<statement>"));
2063 return;
2066 switch (TREE_CODE (t))
2068 case VAR_DECL:
2069 case PARM_DECL:
2070 case FIELD_DECL:
2071 case CONST_DECL:
2072 case FUNCTION_DECL:
2073 case TEMPLATE_DECL:
2074 case NAMESPACE_DECL:
2075 case LABEL_DECL:
2076 case OVERLOAD:
2077 case TYPE_DECL:
2078 case IDENTIFIER_NODE:
2079 dump_decl (pp, t, ((flags & ~(TFF_DECL_SPECIFIERS|TFF_RETURN_TYPE
2080 |TFF_TEMPLATE_HEADER))
2081 | TFF_NO_TEMPLATE_BINDINGS
2082 | TFF_NO_FUNCTION_ARGUMENTS));
2083 break;
2085 case SSA_NAME:
2086 if (SSA_NAME_VAR (t)
2087 && !DECL_ARTIFICIAL (SSA_NAME_VAR (t)))
2088 dump_expr (pp, SSA_NAME_VAR (t), flags);
2089 else
2090 pp_cxx_ws_string (pp, M_("<unknown>"));
2091 break;
2093 case VOID_CST:
2094 case INTEGER_CST:
2095 case REAL_CST:
2096 case STRING_CST:
2097 case COMPLEX_CST:
2098 pp->constant (t);
2099 break;
2101 case USERDEF_LITERAL:
2102 pp_cxx_userdef_literal (pp, t);
2103 break;
2105 case THROW_EXPR:
2106 /* While waiting for caret diagnostics, avoid printing
2107 __cxa_allocate_exception, __cxa_throw, and the like. */
2108 pp_cxx_ws_string (pp, M_("<throw-expression>"));
2109 break;
2111 case PTRMEM_CST:
2112 pp_ampersand (pp);
2113 dump_type (pp, PTRMEM_CST_CLASS (t), flags);
2114 pp_cxx_colon_colon (pp);
2115 pp_cxx_tree_identifier (pp, DECL_NAME (PTRMEM_CST_MEMBER (t)));
2116 break;
2118 case COMPOUND_EXPR:
2119 pp_cxx_left_paren (pp);
2120 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2121 pp_separate_with_comma (pp);
2122 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2123 pp_cxx_right_paren (pp);
2124 break;
2126 case COND_EXPR:
2127 case VEC_COND_EXPR:
2128 pp_cxx_left_paren (pp);
2129 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2130 pp_string (pp, " ? ");
2131 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2132 pp_string (pp, " : ");
2133 dump_expr (pp, TREE_OPERAND (t, 2), flags | TFF_EXPR_IN_PARENS);
2134 pp_cxx_right_paren (pp);
2135 break;
2137 case SAVE_EXPR:
2138 if (TREE_HAS_CONSTRUCTOR (t))
2140 pp_cxx_ws_string (pp, "new");
2141 pp_cxx_whitespace (pp);
2142 dump_type (pp, TREE_TYPE (TREE_TYPE (t)), flags);
2144 else
2145 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2146 break;
2148 case AGGR_INIT_EXPR:
2150 tree fn = NULL_TREE;
2152 if (TREE_CODE (AGGR_INIT_EXPR_FN (t)) == ADDR_EXPR)
2153 fn = TREE_OPERAND (AGGR_INIT_EXPR_FN (t), 0);
2155 if (fn && TREE_CODE (fn) == FUNCTION_DECL)
2157 if (DECL_CONSTRUCTOR_P (fn))
2158 dump_type (pp, DECL_CONTEXT (fn), flags);
2159 else
2160 dump_decl (pp, fn, 0);
2162 else
2163 dump_expr (pp, AGGR_INIT_EXPR_FN (t), 0);
2165 dump_aggr_init_expr_args (pp, t, flags, true);
2166 break;
2168 case CALL_EXPR:
2170 tree fn = CALL_EXPR_FN (t);
2171 bool skipfirst = false;
2173 /* Deal with internal functions. */
2174 if (fn == NULL_TREE)
2176 pp_string (pp, internal_fn_name (CALL_EXPR_IFN (t)));
2177 dump_call_expr_args (pp, t, flags, skipfirst);
2178 break;
2181 if (TREE_CODE (fn) == ADDR_EXPR)
2182 fn = TREE_OPERAND (fn, 0);
2184 /* Nobody is interested in seeing the guts of vcalls. */
2185 if (TREE_CODE (fn) == OBJ_TYPE_REF)
2186 fn = resolve_virtual_fun_from_obj_type_ref (fn);
2188 if (TREE_TYPE (fn) != NULL_TREE
2189 && NEXT_CODE (fn) == METHOD_TYPE
2190 && call_expr_nargs (t))
2192 tree ob = CALL_EXPR_ARG (t, 0);
2193 if (TREE_CODE (ob) == ADDR_EXPR)
2195 dump_expr (pp, TREE_OPERAND (ob, 0),
2196 flags | TFF_EXPR_IN_PARENS);
2197 pp_cxx_dot (pp);
2199 else if (!is_this_parameter (ob))
2201 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2202 pp_cxx_arrow (pp);
2204 skipfirst = true;
2206 if (flag_sanitize & SANITIZE_UNDEFINED
2207 && is_ubsan_builtin_p (fn))
2209 pp_string (cxx_pp, M_("<ubsan routine call>"));
2210 break;
2212 dump_expr (pp, fn, flags | TFF_EXPR_IN_PARENS);
2213 dump_call_expr_args (pp, t, flags, skipfirst);
2215 break;
2217 case TARGET_EXPR:
2218 /* Note that this only works for G++ target exprs. If somebody
2219 builds a general TARGET_EXPR, there's no way to represent that
2220 it initializes anything other that the parameter slot for the
2221 default argument. Note we may have cleared out the first
2222 operand in expand_expr, so don't go killing ourselves. */
2223 if (TREE_OPERAND (t, 1))
2224 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2225 break;
2227 case POINTER_PLUS_EXPR:
2228 dump_binary_op (pp, "+", t, flags);
2229 break;
2231 case INIT_EXPR:
2232 case MODIFY_EXPR:
2233 dump_binary_op (pp, assignment_operator_name_info[NOP_EXPR].name,
2234 t, flags);
2235 break;
2237 case PLUS_EXPR:
2238 case MINUS_EXPR:
2239 case MULT_EXPR:
2240 case TRUNC_DIV_EXPR:
2241 case TRUNC_MOD_EXPR:
2242 case MIN_EXPR:
2243 case MAX_EXPR:
2244 case LSHIFT_EXPR:
2245 case RSHIFT_EXPR:
2246 case BIT_IOR_EXPR:
2247 case BIT_XOR_EXPR:
2248 case BIT_AND_EXPR:
2249 case TRUTH_ANDIF_EXPR:
2250 case TRUTH_ORIF_EXPR:
2251 case LT_EXPR:
2252 case LE_EXPR:
2253 case GT_EXPR:
2254 case GE_EXPR:
2255 case EQ_EXPR:
2256 case NE_EXPR:
2257 case EXACT_DIV_EXPR:
2258 dump_binary_op (pp, operator_name_info[TREE_CODE (t)].name, t, flags);
2259 break;
2261 case CEIL_DIV_EXPR:
2262 case FLOOR_DIV_EXPR:
2263 case ROUND_DIV_EXPR:
2264 case RDIV_EXPR:
2265 dump_binary_op (pp, "/", t, flags);
2266 break;
2268 case CEIL_MOD_EXPR:
2269 case FLOOR_MOD_EXPR:
2270 case ROUND_MOD_EXPR:
2271 dump_binary_op (pp, "%", t, flags);
2272 break;
2274 case COMPONENT_REF:
2276 tree ob = TREE_OPERAND (t, 0);
2277 if (INDIRECT_REF_P (ob))
2279 ob = TREE_OPERAND (ob, 0);
2280 if (!is_this_parameter (ob))
2282 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2283 if (TREE_CODE (TREE_TYPE (ob)) == REFERENCE_TYPE)
2284 pp_cxx_dot (pp);
2285 else
2286 pp_cxx_arrow (pp);
2289 else
2291 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2292 if (TREE_CODE (ob) != ARROW_EXPR)
2293 pp_cxx_dot (pp);
2295 dump_expr (pp, TREE_OPERAND (t, 1), flags & ~TFF_EXPR_IN_PARENS);
2297 break;
2299 case ARRAY_REF:
2300 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2301 pp_cxx_left_bracket (pp);
2302 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2303 pp_cxx_right_bracket (pp);
2304 break;
2306 case ARRAY_NOTATION_REF:
2307 dump_expr (pp, ARRAY_NOTATION_ARRAY (t), flags | TFF_EXPR_IN_PARENS);
2308 pp_cxx_left_bracket (pp);
2309 dump_expr (pp, ARRAY_NOTATION_START (t), flags | TFF_EXPR_IN_PARENS);
2310 pp_colon (pp);
2311 dump_expr (pp, ARRAY_NOTATION_LENGTH (t), flags | TFF_EXPR_IN_PARENS);
2312 pp_colon (pp);
2313 dump_expr (pp, ARRAY_NOTATION_STRIDE (t), flags | TFF_EXPR_IN_PARENS);
2314 pp_cxx_right_bracket (pp);
2315 break;
2317 case UNARY_PLUS_EXPR:
2318 dump_unary_op (pp, "+", t, flags);
2319 break;
2321 case ADDR_EXPR:
2322 if (TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
2323 || TREE_CODE (TREE_OPERAND (t, 0)) == STRING_CST
2324 /* An ADDR_EXPR can have reference type. In that case, we
2325 shouldn't print the `&' doing so indicates to the user
2326 that the expression has pointer type. */
2327 || (TREE_TYPE (t)
2328 && TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE))
2329 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2330 else if (TREE_CODE (TREE_OPERAND (t, 0)) == LABEL_DECL)
2331 dump_unary_op (pp, "&&", t, flags);
2332 else
2333 dump_unary_op (pp, "&", t, flags);
2334 break;
2336 case INDIRECT_REF:
2337 if (TREE_HAS_CONSTRUCTOR (t))
2339 t = TREE_OPERAND (t, 0);
2340 gcc_assert (TREE_CODE (t) == CALL_EXPR);
2341 dump_expr (pp, CALL_EXPR_FN (t), flags | TFF_EXPR_IN_PARENS);
2342 dump_call_expr_args (pp, t, flags, true);
2344 else
2346 if (TREE_OPERAND (t,0) != NULL_TREE
2347 && TREE_TYPE (TREE_OPERAND (t, 0))
2348 && NEXT_CODE (TREE_OPERAND (t, 0)) == REFERENCE_TYPE)
2349 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2350 else
2351 dump_unary_op (pp, "*", t, flags);
2353 break;
2355 case MEM_REF:
2356 if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
2357 && integer_zerop (TREE_OPERAND (t, 1)))
2358 dump_expr (pp, TREE_OPERAND (TREE_OPERAND (t, 0), 0), flags);
2359 else
2361 pp_cxx_star (pp);
2362 if (!integer_zerop (TREE_OPERAND (t, 1)))
2364 pp_cxx_left_paren (pp);
2365 if (!integer_onep (TYPE_SIZE_UNIT
2366 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0))))))
2368 pp_cxx_left_paren (pp);
2369 dump_type (pp, ptr_type_node, flags);
2370 pp_cxx_right_paren (pp);
2373 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2374 if (!integer_zerop (TREE_OPERAND (t, 1)))
2376 pp_cxx_ws_string (pp, "+");
2377 dump_expr (pp, fold_convert (ssizetype, TREE_OPERAND (t, 1)),
2378 flags);
2379 pp_cxx_right_paren (pp);
2382 break;
2384 case NEGATE_EXPR:
2385 case BIT_NOT_EXPR:
2386 case TRUTH_NOT_EXPR:
2387 case PREDECREMENT_EXPR:
2388 case PREINCREMENT_EXPR:
2389 dump_unary_op (pp, operator_name_info [TREE_CODE (t)].name, t, flags);
2390 break;
2392 case POSTDECREMENT_EXPR:
2393 case POSTINCREMENT_EXPR:
2394 pp_cxx_left_paren (pp);
2395 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2396 pp_cxx_ws_string (pp, operator_name_info[TREE_CODE (t)].name);
2397 pp_cxx_right_paren (pp);
2398 break;
2400 case NON_LVALUE_EXPR:
2401 /* FIXME: This is a KLUDGE workaround for a parsing problem. There
2402 should be another level of INDIRECT_REF so that I don't have to do
2403 this. */
2404 if (TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == POINTER_TYPE)
2406 tree next = TREE_TYPE (TREE_TYPE (t));
2408 while (TYPE_PTR_P (next))
2409 next = TREE_TYPE (next);
2411 if (TREE_CODE (next) == FUNCTION_TYPE)
2413 if (flags & TFF_EXPR_IN_PARENS)
2414 pp_cxx_left_paren (pp);
2415 pp_cxx_star (pp);
2416 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2417 if (flags & TFF_EXPR_IN_PARENS)
2418 pp_cxx_right_paren (pp);
2419 break;
2421 /* Else fall through. */
2423 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2424 break;
2426 CASE_CONVERT:
2427 case IMPLICIT_CONV_EXPR:
2428 case VIEW_CONVERT_EXPR:
2430 tree op = TREE_OPERAND (t, 0);
2431 tree ttype = TREE_TYPE (t);
2432 tree optype = TREE_TYPE (op);
2434 if (TREE_CODE (ttype) != TREE_CODE (optype)
2435 && POINTER_TYPE_P (ttype)
2436 && POINTER_TYPE_P (optype)
2437 && same_type_p (TREE_TYPE (optype),
2438 TREE_TYPE (ttype)))
2440 if (TREE_CODE (ttype) == REFERENCE_TYPE)
2442 STRIP_NOPS (op);
2443 if (TREE_CODE (op) == ADDR_EXPR)
2444 dump_expr (pp, TREE_OPERAND (op, 0), flags);
2445 else
2446 dump_unary_op (pp, "*", t, flags);
2448 else
2449 dump_unary_op (pp, "&", t, flags);
2451 else if (!same_type_p (TREE_TYPE (op), TREE_TYPE (t)))
2453 /* It is a cast, but we cannot tell whether it is a
2454 reinterpret or static cast. Use the C style notation. */
2455 if (flags & TFF_EXPR_IN_PARENS)
2456 pp_cxx_left_paren (pp);
2457 pp_cxx_left_paren (pp);
2458 dump_type (pp, TREE_TYPE (t), flags);
2459 pp_cxx_right_paren (pp);
2460 dump_expr (pp, op, flags | TFF_EXPR_IN_PARENS);
2461 if (flags & TFF_EXPR_IN_PARENS)
2462 pp_cxx_right_paren (pp);
2464 else
2465 dump_expr (pp, op, flags);
2466 break;
2469 case CONSTRUCTOR:
2470 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
2472 tree idx = build_ptrmemfunc_access_expr (t, pfn_identifier);
2474 if (integer_zerop (idx))
2476 /* A NULL pointer-to-member constant. */
2477 pp_cxx_left_paren (pp);
2478 pp_cxx_left_paren (pp);
2479 dump_type (pp, TREE_TYPE (t), flags);
2480 pp_cxx_right_paren (pp);
2481 pp_character (pp, '0');
2482 pp_cxx_right_paren (pp);
2483 break;
2485 else if (tree_fits_shwi_p (idx))
2487 tree virtuals;
2488 unsigned HOST_WIDE_INT n;
2490 t = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
2491 t = TYPE_METHOD_BASETYPE (t);
2492 virtuals = BINFO_VIRTUALS (TYPE_BINFO (TYPE_MAIN_VARIANT (t)));
2494 n = tree_to_shwi (idx);
2496 /* Map vtable index back one, to allow for the null pointer to
2497 member. */
2498 --n;
2500 while (n > 0 && virtuals)
2502 --n;
2503 virtuals = TREE_CHAIN (virtuals);
2505 if (virtuals)
2507 dump_expr (pp, BV_FN (virtuals),
2508 flags | TFF_EXPR_IN_PARENS);
2509 break;
2513 if (TREE_TYPE (t) && LAMBDA_TYPE_P (TREE_TYPE (t)))
2514 pp_string (pp, "<lambda closure object>");
2515 if (TREE_TYPE (t) && EMPTY_CONSTRUCTOR_P (t))
2517 dump_type (pp, TREE_TYPE (t), 0);
2518 pp_cxx_left_paren (pp);
2519 pp_cxx_right_paren (pp);
2521 else
2523 if (!BRACE_ENCLOSED_INITIALIZER_P (t))
2524 dump_type (pp, TREE_TYPE (t), 0);
2525 pp_cxx_left_brace (pp);
2526 dump_expr_init_vec (pp, CONSTRUCTOR_ELTS (t), flags);
2527 pp_cxx_right_brace (pp);
2530 break;
2532 case OFFSET_REF:
2534 tree ob = TREE_OPERAND (t, 0);
2535 if (is_dummy_object (ob))
2537 t = TREE_OPERAND (t, 1);
2538 if (TREE_CODE (t) == FUNCTION_DECL)
2539 /* A::f */
2540 dump_expr (pp, t, flags | TFF_EXPR_IN_PARENS);
2541 else if (BASELINK_P (t))
2542 dump_expr (pp, OVL_FIRST (BASELINK_FUNCTIONS (t)),
2543 flags | TFF_EXPR_IN_PARENS);
2544 else
2545 dump_decl (pp, t, flags);
2547 else
2549 if (INDIRECT_REF_P (ob))
2551 dump_expr (pp, TREE_OPERAND (ob, 0), flags | TFF_EXPR_IN_PARENS);
2552 pp_cxx_arrow (pp);
2553 pp_cxx_star (pp);
2555 else
2557 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2558 pp_cxx_dot (pp);
2559 pp_cxx_star (pp);
2561 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2563 break;
2566 case TEMPLATE_PARM_INDEX:
2567 dump_decl (pp, TEMPLATE_PARM_DECL (t), flags & ~TFF_DECL_SPECIFIERS);
2568 break;
2570 case CAST_EXPR:
2571 if (TREE_OPERAND (t, 0) == NULL_TREE
2572 || TREE_CHAIN (TREE_OPERAND (t, 0)))
2574 dump_type (pp, TREE_TYPE (t), flags);
2575 pp_cxx_left_paren (pp);
2576 dump_expr_list (pp, TREE_OPERAND (t, 0), flags);
2577 pp_cxx_right_paren (pp);
2579 else
2581 pp_cxx_left_paren (pp);
2582 dump_type (pp, TREE_TYPE (t), flags);
2583 pp_cxx_right_paren (pp);
2584 pp_cxx_left_paren (pp);
2585 dump_expr_list (pp, TREE_OPERAND (t, 0), flags);
2586 pp_cxx_right_paren (pp);
2588 break;
2590 case STATIC_CAST_EXPR:
2591 pp_cxx_ws_string (pp, "static_cast");
2592 goto cast;
2593 case REINTERPRET_CAST_EXPR:
2594 pp_cxx_ws_string (pp, "reinterpret_cast");
2595 goto cast;
2596 case CONST_CAST_EXPR:
2597 pp_cxx_ws_string (pp, "const_cast");
2598 goto cast;
2599 case DYNAMIC_CAST_EXPR:
2600 pp_cxx_ws_string (pp, "dynamic_cast");
2601 cast:
2602 pp_cxx_begin_template_argument_list (pp);
2603 dump_type (pp, TREE_TYPE (t), flags);
2604 pp_cxx_end_template_argument_list (pp);
2605 pp_cxx_left_paren (pp);
2606 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2607 pp_cxx_right_paren (pp);
2608 break;
2610 case ARROW_EXPR:
2611 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2612 pp_cxx_arrow (pp);
2613 break;
2615 case SIZEOF_EXPR:
2616 case ALIGNOF_EXPR:
2617 if (TREE_CODE (t) == SIZEOF_EXPR)
2618 pp_cxx_ws_string (pp, "sizeof");
2619 else
2621 gcc_assert (TREE_CODE (t) == ALIGNOF_EXPR);
2622 pp_cxx_ws_string (pp, "__alignof__");
2624 op = TREE_OPERAND (t, 0);
2625 if (PACK_EXPANSION_P (op))
2627 pp_string (pp, "...");
2628 op = PACK_EXPANSION_PATTERN (op);
2630 pp_cxx_whitespace (pp);
2631 pp_cxx_left_paren (pp);
2632 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
2633 dump_type (pp, TREE_TYPE (op), flags);
2634 else if (TYPE_P (TREE_OPERAND (t, 0)))
2635 dump_type (pp, op, flags);
2636 else
2637 dump_expr (pp, op, flags);
2638 pp_cxx_right_paren (pp);
2639 break;
2641 case AT_ENCODE_EXPR:
2642 pp_cxx_ws_string (pp, "@encode");
2643 pp_cxx_whitespace (pp);
2644 pp_cxx_left_paren (pp);
2645 dump_type (pp, TREE_OPERAND (t, 0), flags);
2646 pp_cxx_right_paren (pp);
2647 break;
2649 case NOEXCEPT_EXPR:
2650 pp_cxx_ws_string (pp, "noexcept");
2651 pp_cxx_whitespace (pp);
2652 pp_cxx_left_paren (pp);
2653 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2654 pp_cxx_right_paren (pp);
2655 break;
2657 case REALPART_EXPR:
2658 case IMAGPART_EXPR:
2659 pp_cxx_ws_string (pp, operator_name_info[TREE_CODE (t)].name);
2660 pp_cxx_whitespace (pp);
2661 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2662 break;
2664 case DEFAULT_ARG:
2665 pp_string (pp, M_("<unparsed>"));
2666 break;
2668 case TRY_CATCH_EXPR:
2669 case CLEANUP_POINT_EXPR:
2670 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2671 break;
2673 case PSEUDO_DTOR_EXPR:
2674 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2675 pp_cxx_dot (pp);
2676 if (TREE_OPERAND (t, 1))
2678 dump_type (pp, TREE_OPERAND (t, 1), flags);
2679 pp_cxx_colon_colon (pp);
2681 pp_cxx_complement (pp);
2682 dump_type (pp, TREE_OPERAND (t, 2), flags);
2683 break;
2685 case TEMPLATE_ID_EXPR:
2686 dump_decl (pp, t, flags);
2687 break;
2689 case BIND_EXPR:
2690 case STMT_EXPR:
2691 case EXPR_STMT:
2692 case STATEMENT_LIST:
2693 /* We don't yet have a way of dumping statements in a
2694 human-readable format. */
2695 pp_string (pp, "({...})");
2696 break;
2698 case LOOP_EXPR:
2699 pp_string (pp, "while (1) { ");
2700 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2701 pp_cxx_right_brace (pp);
2702 break;
2704 case EXIT_EXPR:
2705 pp_string (pp, "if (");
2706 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2707 pp_string (pp, ") break; ");
2708 break;
2710 case BASELINK:
2711 dump_expr (pp, BASELINK_FUNCTIONS (t), flags & ~TFF_EXPR_IN_PARENS);
2712 break;
2714 case EMPTY_CLASS_EXPR:
2715 dump_type (pp, TREE_TYPE (t), flags);
2716 pp_cxx_left_paren (pp);
2717 pp_cxx_right_paren (pp);
2718 break;
2720 case NON_DEPENDENT_EXPR:
2721 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2722 break;
2724 case ARGUMENT_PACK_SELECT:
2725 dump_template_argument (pp, ARGUMENT_PACK_SELECT_FROM_PACK (t), flags);
2726 break;
2728 case RECORD_TYPE:
2729 case UNION_TYPE:
2730 case ENUMERAL_TYPE:
2731 case REAL_TYPE:
2732 case VOID_TYPE:
2733 case BOOLEAN_TYPE:
2734 case INTEGER_TYPE:
2735 case COMPLEX_TYPE:
2736 case VECTOR_TYPE:
2737 pp_type_specifier_seq (pp, t);
2738 break;
2740 case TYPENAME_TYPE:
2741 /* We get here when we want to print a dependent type as an
2742 id-expression, without any disambiguator decoration. */
2743 pp->id_expression (t);
2744 break;
2746 case TEMPLATE_TYPE_PARM:
2747 case TEMPLATE_TEMPLATE_PARM:
2748 case BOUND_TEMPLATE_TEMPLATE_PARM:
2749 dump_type (pp, t, flags);
2750 break;
2752 case TRAIT_EXPR:
2753 pp_cxx_trait_expression (pp, t);
2754 break;
2756 case VA_ARG_EXPR:
2757 pp_cxx_va_arg_expression (pp, t);
2758 break;
2760 case OFFSETOF_EXPR:
2761 pp_cxx_offsetof_expression (pp, t);
2762 break;
2764 case ADDRESSOF_EXPR:
2765 pp_cxx_addressof_expression (pp, t);
2766 break;
2768 case SCOPE_REF:
2769 dump_decl (pp, t, flags);
2770 break;
2772 case EXPR_PACK_EXPANSION:
2773 case UNARY_LEFT_FOLD_EXPR:
2774 case UNARY_RIGHT_FOLD_EXPR:
2775 case BINARY_LEFT_FOLD_EXPR:
2776 case BINARY_RIGHT_FOLD_EXPR:
2777 case TYPEID_EXPR:
2778 case MEMBER_REF:
2779 case DOTSTAR_EXPR:
2780 case NEW_EXPR:
2781 case VEC_NEW_EXPR:
2782 case DELETE_EXPR:
2783 case VEC_DELETE_EXPR:
2784 case MODOP_EXPR:
2785 case ABS_EXPR:
2786 case CONJ_EXPR:
2787 case VECTOR_CST:
2788 case FIXED_CST:
2789 case UNORDERED_EXPR:
2790 case ORDERED_EXPR:
2791 case UNLT_EXPR:
2792 case UNLE_EXPR:
2793 case UNGT_EXPR:
2794 case UNGE_EXPR:
2795 case UNEQ_EXPR:
2796 case LTGT_EXPR:
2797 case COMPLEX_EXPR:
2798 case BIT_FIELD_REF:
2799 case FIX_TRUNC_EXPR:
2800 case FLOAT_EXPR:
2801 pp->expression (t);
2802 break;
2804 case TRUTH_AND_EXPR:
2805 case TRUTH_OR_EXPR:
2806 case TRUTH_XOR_EXPR:
2807 if (flags & TFF_EXPR_IN_PARENS)
2808 pp_cxx_left_paren (pp);
2809 pp->expression (t);
2810 if (flags & TFF_EXPR_IN_PARENS)
2811 pp_cxx_right_paren (pp);
2812 break;
2814 case OBJ_TYPE_REF:
2815 dump_expr (pp, resolve_virtual_fun_from_obj_type_ref (t), flags);
2816 break;
2818 case LAMBDA_EXPR:
2819 pp_string (pp, M_("<lambda>"));
2820 break;
2822 case PAREN_EXPR:
2823 pp_cxx_left_paren (pp);
2824 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2825 pp_cxx_right_paren (pp);
2826 break;
2828 case REQUIRES_EXPR:
2829 pp_cxx_requires_expr (cxx_pp, t);
2830 break;
2832 case SIMPLE_REQ:
2833 pp_cxx_simple_requirement (cxx_pp, t);
2834 break;
2836 case TYPE_REQ:
2837 pp_cxx_type_requirement (cxx_pp, t);
2838 break;
2840 case COMPOUND_REQ:
2841 pp_cxx_compound_requirement (cxx_pp, t);
2842 break;
2844 case NESTED_REQ:
2845 pp_cxx_nested_requirement (cxx_pp, t);
2846 break;
2848 case PRED_CONSTR:
2849 case CHECK_CONSTR:
2850 case EXPR_CONSTR:
2851 case TYPE_CONSTR:
2852 case ICONV_CONSTR:
2853 case DEDUCT_CONSTR:
2854 case EXCEPT_CONSTR:
2855 case PARM_CONSTR:
2856 case CONJ_CONSTR:
2857 case DISJ_CONSTR:
2858 pp_cxx_constraint (cxx_pp, t);
2859 break;
2861 case PLACEHOLDER_EXPR:
2862 pp_string (pp, M_("*this"));
2863 break;
2865 case TREE_LIST:
2866 dump_expr_list (pp, t, flags);
2867 break;
2869 /* This list is incomplete, but should suffice for now.
2870 It is very important that `sorry' does not call
2871 `report_error_function'. That could cause an infinite loop. */
2872 default:
2873 pp_unsupported_tree (pp, t);
2874 /* Fall through. */
2875 case ERROR_MARK:
2876 pp_string (pp, M_("<expression error>"));
2877 break;
2881 static void
2882 dump_binary_op (cxx_pretty_printer *pp, const char *opstring, tree t,
2883 int flags)
2885 pp_cxx_left_paren (pp);
2886 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2887 pp_cxx_whitespace (pp);
2888 if (opstring)
2889 pp_cxx_ws_string (pp, opstring);
2890 else
2891 pp_string (pp, M_("<unknown operator>"));
2892 pp_cxx_whitespace (pp);
2893 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2894 pp_cxx_right_paren (pp);
2897 static void
2898 dump_unary_op (cxx_pretty_printer *pp, const char *opstring, tree t, int flags)
2900 if (flags & TFF_EXPR_IN_PARENS)
2901 pp_cxx_left_paren (pp);
2902 pp_cxx_ws_string (pp, opstring);
2903 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2904 if (flags & TFF_EXPR_IN_PARENS)
2905 pp_cxx_right_paren (pp);
2908 static void
2909 reinit_cxx_pp (void)
2911 pp_clear_output_area (cxx_pp);
2912 cxx_pp->padding = pp_none;
2913 pp_indentation (cxx_pp) = 0;
2914 pp_needs_newline (cxx_pp) = false;
2915 cxx_pp->enclosing_scope = current_function_decl;
2918 /* Same as pp_formatted_text, except the return string is a separate
2919 copy and has a GGC storage duration, e.g. an indefinite lifetime. */
2921 inline const char *
2922 pp_ggc_formatted_text (pretty_printer *pp)
2924 return ggc_strdup (pp_formatted_text (pp));
2927 /* Exported interface to stringifying types, exprs and decls under TFF_*
2928 control. */
2930 const char *
2931 type_as_string (tree typ, int flags)
2933 reinit_cxx_pp ();
2934 pp_translate_identifiers (cxx_pp) = false;
2935 dump_type (cxx_pp, typ, flags);
2936 return pp_ggc_formatted_text (cxx_pp);
2939 const char *
2940 type_as_string_translate (tree typ, int flags)
2942 reinit_cxx_pp ();
2943 dump_type (cxx_pp, typ, flags);
2944 return pp_ggc_formatted_text (cxx_pp);
2947 const char *
2948 expr_as_string (tree decl, int flags)
2950 reinit_cxx_pp ();
2951 pp_translate_identifiers (cxx_pp) = false;
2952 dump_expr (cxx_pp, decl, flags);
2953 return pp_ggc_formatted_text (cxx_pp);
2956 /* Wrap decl_as_string with options appropriate for dwarf. */
2958 const char *
2959 decl_as_dwarf_string (tree decl, int flags)
2961 const char *name;
2962 /* Curiously, reinit_cxx_pp doesn't reset the flags field, so setting the flag
2963 here will be adequate to get the desired behavior. */
2964 cxx_pp->flags |= pp_c_flag_gnu_v3;
2965 name = decl_as_string (decl, flags);
2966 /* Subsequent calls to the pretty printer shouldn't use this style. */
2967 cxx_pp->flags &= ~pp_c_flag_gnu_v3;
2968 return name;
2971 const char *
2972 decl_as_string (tree decl, int flags)
2974 reinit_cxx_pp ();
2975 pp_translate_identifiers (cxx_pp) = false;
2976 dump_decl (cxx_pp, decl, flags);
2977 return pp_ggc_formatted_text (cxx_pp);
2980 const char *
2981 decl_as_string_translate (tree decl, int flags)
2983 reinit_cxx_pp ();
2984 dump_decl (cxx_pp, decl, flags);
2985 return pp_ggc_formatted_text (cxx_pp);
2988 /* Wrap lang_decl_name with options appropriate for dwarf. */
2990 const char *
2991 lang_decl_dwarf_name (tree decl, int v, bool translate)
2993 const char *name;
2994 /* Curiously, reinit_cxx_pp doesn't reset the flags field, so setting the flag
2995 here will be adequate to get the desired behavior. */
2996 cxx_pp->flags |= pp_c_flag_gnu_v3;
2997 name = lang_decl_name (decl, v, translate);
2998 /* Subsequent calls to the pretty printer shouldn't use this style. */
2999 cxx_pp->flags &= ~pp_c_flag_gnu_v3;
3000 return name;
3003 /* Generate the three forms of printable names for cxx_printable_name. */
3005 const char *
3006 lang_decl_name (tree decl, int v, bool translate)
3008 if (v >= 2)
3009 return (translate
3010 ? decl_as_string_translate (decl, TFF_DECL_SPECIFIERS)
3011 : decl_as_string (decl, TFF_DECL_SPECIFIERS));
3013 reinit_cxx_pp ();
3014 pp_translate_identifiers (cxx_pp) = translate;
3015 if (v == 1
3016 && (DECL_CLASS_SCOPE_P (decl)
3017 || (DECL_NAMESPACE_SCOPE_P (decl)
3018 && CP_DECL_CONTEXT (decl) != global_namespace)))
3020 dump_type (cxx_pp, CP_DECL_CONTEXT (decl), TFF_PLAIN_IDENTIFIER);
3021 pp_cxx_colon_colon (cxx_pp);
3024 if (TREE_CODE (decl) == FUNCTION_DECL)
3025 dump_function_name (cxx_pp, decl, TFF_PLAIN_IDENTIFIER);
3026 else if ((DECL_NAME (decl) == NULL_TREE)
3027 && TREE_CODE (decl) == NAMESPACE_DECL)
3028 dump_decl (cxx_pp, decl, TFF_PLAIN_IDENTIFIER | TFF_UNQUALIFIED_NAME);
3029 else
3030 dump_decl (cxx_pp, DECL_NAME (decl), TFF_PLAIN_IDENTIFIER);
3032 return pp_ggc_formatted_text (cxx_pp);
3035 /* Return the location of a tree passed to %+ formats. */
3037 location_t
3038 location_of (tree t)
3040 if (TYPE_P (t))
3042 t = TYPE_MAIN_DECL (t);
3043 if (t == NULL_TREE)
3044 return input_location;
3046 else if (TREE_CODE (t) == OVERLOAD)
3047 t = OVL_FIRST (t);
3049 if (DECL_P (t))
3050 return DECL_SOURCE_LOCATION (t);
3051 if (TREE_CODE (t) == DEFAULT_ARG)
3052 return defarg_location (t);
3053 return EXPR_LOC_OR_LOC (t, input_location);
3056 /* Now the interfaces from error et al to dump_type et al. Each takes an
3057 on/off VERBOSE flag and supply the appropriate TFF_ flags to a dump_
3058 function. */
3060 static const char *
3061 decl_to_string (tree decl, int verbose)
3063 int flags = 0;
3065 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == RECORD_TYPE
3066 || TREE_CODE (decl) == UNION_TYPE || TREE_CODE (decl) == ENUMERAL_TYPE)
3067 flags = TFF_CLASS_KEY_OR_ENUM;
3068 if (verbose)
3069 flags |= TFF_DECL_SPECIFIERS;
3070 else if (TREE_CODE (decl) == FUNCTION_DECL)
3071 flags |= TFF_DECL_SPECIFIERS | TFF_RETURN_TYPE;
3072 flags |= TFF_TEMPLATE_HEADER;
3074 reinit_cxx_pp ();
3075 dump_decl (cxx_pp, decl, flags);
3076 return pp_ggc_formatted_text (cxx_pp);
3079 static const char *
3080 expr_to_string (tree decl)
3082 reinit_cxx_pp ();
3083 dump_expr (cxx_pp, decl, 0);
3084 return pp_ggc_formatted_text (cxx_pp);
3087 static const char *
3088 fndecl_to_string (tree fndecl, int verbose)
3090 int flags;
3092 flags = TFF_EXCEPTION_SPECIFICATION | TFF_DECL_SPECIFIERS
3093 | TFF_TEMPLATE_HEADER;
3094 if (verbose)
3095 flags |= TFF_FUNCTION_DEFAULT_ARGUMENTS;
3096 reinit_cxx_pp ();
3097 dump_decl (cxx_pp, fndecl, flags);
3098 return pp_ggc_formatted_text (cxx_pp);
3102 static const char *
3103 code_to_string (enum tree_code c)
3105 return get_tree_code_name (c);
3108 const char *
3109 language_to_string (enum languages c)
3111 switch (c)
3113 case lang_c:
3114 return "C";
3116 case lang_cplusplus:
3117 return "C++";
3119 default:
3120 gcc_unreachable ();
3122 return NULL;
3125 /* Return the proper printed version of a parameter to a C++ function. */
3127 static const char *
3128 parm_to_string (int p)
3130 reinit_cxx_pp ();
3131 if (p < 0)
3132 pp_string (cxx_pp, "'this'");
3133 else
3134 pp_decimal_int (cxx_pp, p + 1);
3135 return pp_ggc_formatted_text (cxx_pp);
3138 static const char *
3139 op_to_string (enum tree_code p)
3141 tree id = operator_name_info[p].identifier;
3142 return id ? IDENTIFIER_POINTER (id) : M_("<unknown>");
3145 static const char *
3146 type_to_string (tree typ, int verbose)
3148 int flags = 0;
3149 if (verbose)
3150 flags |= TFF_CLASS_KEY_OR_ENUM;
3151 flags |= TFF_TEMPLATE_HEADER;
3153 reinit_cxx_pp ();
3154 dump_type (cxx_pp, typ, flags);
3155 /* If we're printing a type that involves typedefs, also print the
3156 stripped version. But sometimes the stripped version looks
3157 exactly the same, so we don't want it after all. To avoid printing
3158 it in that case, we play ugly obstack games. */
3159 if (typ && TYPE_P (typ) && typ != TYPE_CANONICAL (typ)
3160 && !uses_template_parms (typ))
3162 int aka_start, aka_len; char *p;
3163 struct obstack *ob = pp_buffer (cxx_pp)->obstack;
3164 /* Remember the end of the initial dump. */
3165 int len = obstack_object_size (ob);
3166 tree aka = strip_typedefs (typ);
3167 pp_string (cxx_pp, " {aka");
3168 pp_cxx_whitespace (cxx_pp);
3169 /* And remember the start of the aka dump. */
3170 aka_start = obstack_object_size (ob);
3171 dump_type (cxx_pp, aka, flags);
3172 aka_len = obstack_object_size (ob) - aka_start;
3173 pp_right_brace (cxx_pp);
3174 p = (char*)obstack_base (ob);
3175 /* If they are identical, cut off the aka with a NUL. */
3176 if (len == aka_len && memcmp (p, p+aka_start, len) == 0)
3177 p[len] = '\0';
3179 return pp_ggc_formatted_text (cxx_pp);
3182 static const char *
3183 assop_to_string (enum tree_code p)
3185 tree id = assignment_operator_name_info[(int) p].identifier;
3186 return id ? IDENTIFIER_POINTER (id) : M_("{unknown}");
3189 static const char *
3190 args_to_string (tree p, int verbose)
3192 int flags = 0;
3193 if (verbose)
3194 flags |= TFF_CLASS_KEY_OR_ENUM;
3196 if (p == NULL_TREE)
3197 return "";
3199 if (TYPE_P (TREE_VALUE (p)))
3200 return type_as_string_translate (p, flags);
3202 reinit_cxx_pp ();
3203 for (; p; p = TREE_CHAIN (p))
3205 if (TREE_VALUE (p) == null_node)
3206 pp_cxx_ws_string (cxx_pp, "NULL");
3207 else
3208 dump_type (cxx_pp, error_type (TREE_VALUE (p)), flags);
3209 if (TREE_CHAIN (p))
3210 pp_separate_with_comma (cxx_pp);
3212 return pp_ggc_formatted_text (cxx_pp);
3215 /* Pretty-print a deduction substitution (from deduction_tsubst_fntype). P
3216 is a TREE_LIST with purpose the TEMPLATE_DECL, value the template
3217 arguments. */
3219 static const char *
3220 subst_to_string (tree p)
3222 tree decl = TREE_PURPOSE (p);
3223 tree targs = TREE_VALUE (p);
3224 tree tparms = DECL_TEMPLATE_PARMS (decl);
3225 int flags = (TFF_DECL_SPECIFIERS|TFF_TEMPLATE_HEADER
3226 |TFF_NO_TEMPLATE_BINDINGS);
3228 if (p == NULL_TREE)
3229 return "";
3231 reinit_cxx_pp ();
3232 dump_template_decl (cxx_pp, TREE_PURPOSE (p), flags);
3233 dump_substitution (cxx_pp, NULL, tparms, targs, /*flags=*/0);
3234 return pp_ggc_formatted_text (cxx_pp);
3237 static const char *
3238 cv_to_string (tree p, int v)
3240 reinit_cxx_pp ();
3241 cxx_pp->padding = v ? pp_before : pp_none;
3242 pp_cxx_cv_qualifier_seq (cxx_pp, p);
3243 return pp_ggc_formatted_text (cxx_pp);
3246 static const char *
3247 eh_spec_to_string (tree p, int /*v*/)
3249 int flags = 0;
3250 reinit_cxx_pp ();
3251 dump_exception_spec (cxx_pp, p, flags);
3252 return pp_ggc_formatted_text (cxx_pp);
3255 /* Langhook for print_error_function. */
3256 void
3257 cxx_print_error_function (diagnostic_context *context, const char *file,
3258 diagnostic_info *diagnostic)
3260 lhd_print_error_function (context, file, diagnostic);
3261 pp_set_prefix (context->printer, file);
3262 maybe_print_instantiation_context (context);
3265 static void
3266 cp_diagnostic_starter (diagnostic_context *context,
3267 diagnostic_info *diagnostic)
3269 diagnostic_report_current_module (context, diagnostic_location (diagnostic));
3270 cp_print_error_function (context, diagnostic);
3271 maybe_print_instantiation_context (context);
3272 maybe_print_constexpr_context (context);
3273 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
3274 diagnostic));
3277 /* Print current function onto BUFFER, in the process of reporting
3278 a diagnostic message. Called from cp_diagnostic_starter. */
3279 static void
3280 cp_print_error_function (diagnostic_context *context,
3281 diagnostic_info *diagnostic)
3283 /* If we are in an instantiation context, current_function_decl is likely
3284 to be wrong, so just rely on print_instantiation_full_context. */
3285 if (current_instantiation ())
3286 return;
3287 if (diagnostic_last_function_changed (context, diagnostic))
3289 const char *old_prefix = context->printer->prefix;
3290 const char *file = LOCATION_FILE (diagnostic_location (diagnostic));
3291 tree abstract_origin = diagnostic_abstract_origin (diagnostic);
3292 char *new_prefix = (file && abstract_origin == NULL)
3293 ? file_name_as_prefix (context, file) : NULL;
3295 pp_set_prefix (context->printer, new_prefix);
3297 if (current_function_decl == NULL)
3298 pp_string (context->printer, _("At global scope:"));
3299 else
3301 tree fndecl, ao;
3303 if (abstract_origin)
3305 ao = BLOCK_ABSTRACT_ORIGIN (abstract_origin);
3306 while (TREE_CODE (ao) == BLOCK
3307 && BLOCK_ABSTRACT_ORIGIN (ao)
3308 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
3309 ao = BLOCK_ABSTRACT_ORIGIN (ao);
3310 gcc_assert (TREE_CODE (ao) == FUNCTION_DECL);
3311 fndecl = ao;
3313 else
3314 fndecl = current_function_decl;
3316 pp_printf (context->printer, function_category (fndecl),
3317 cxx_printable_name_translate (fndecl, 2));
3319 while (abstract_origin)
3321 location_t *locus;
3322 tree block = abstract_origin;
3324 locus = &BLOCK_SOURCE_LOCATION (block);
3325 fndecl = NULL;
3326 block = BLOCK_SUPERCONTEXT (block);
3327 while (block && TREE_CODE (block) == BLOCK
3328 && BLOCK_ABSTRACT_ORIGIN (block))
3330 ao = BLOCK_ABSTRACT_ORIGIN (block);
3332 while (TREE_CODE (ao) == BLOCK
3333 && BLOCK_ABSTRACT_ORIGIN (ao)
3334 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
3335 ao = BLOCK_ABSTRACT_ORIGIN (ao);
3337 if (TREE_CODE (ao) == FUNCTION_DECL)
3339 fndecl = ao;
3340 break;
3342 else if (TREE_CODE (ao) != BLOCK)
3343 break;
3345 block = BLOCK_SUPERCONTEXT (block);
3347 if (fndecl)
3348 abstract_origin = block;
3349 else
3351 while (block && TREE_CODE (block) == BLOCK)
3352 block = BLOCK_SUPERCONTEXT (block);
3354 if (block && TREE_CODE (block) == FUNCTION_DECL)
3355 fndecl = block;
3356 abstract_origin = NULL;
3358 if (fndecl)
3360 expanded_location s = expand_location (*locus);
3361 pp_character (context->printer, ',');
3362 pp_newline (context->printer);
3363 if (s.file != NULL)
3365 if (context->show_column && s.column != 0)
3366 pp_printf (context->printer,
3367 _(" inlined from %qs at %r%s:%d:%d%R"),
3368 cxx_printable_name_translate (fndecl, 2),
3369 "locus", s.file, s.line, s.column);
3370 else
3371 pp_printf (context->printer,
3372 _(" inlined from %qs at %r%s:%d%R"),
3373 cxx_printable_name_translate (fndecl, 2),
3374 "locus", s.file, s.line);
3377 else
3378 pp_printf (context->printer, _(" inlined from %qs"),
3379 cxx_printable_name_translate (fndecl, 2));
3382 pp_character (context->printer, ':');
3384 pp_newline (context->printer);
3386 diagnostic_set_last_function (context, diagnostic);
3387 pp_destroy_prefix (context->printer);
3388 context->printer->prefix = old_prefix;
3392 /* Returns a description of FUNCTION using standard terminology. The
3393 result is a format string of the form "In CATEGORY %qs". */
3394 static const char *
3395 function_category (tree fn)
3397 /* We can get called from the middle-end for diagnostics of function
3398 clones. Make sure we have language specific information before
3399 dereferencing it. */
3400 if (DECL_LANG_SPECIFIC (STRIP_TEMPLATE (fn))
3401 && DECL_FUNCTION_MEMBER_P (fn))
3403 if (DECL_STATIC_FUNCTION_P (fn))
3404 return _("In static member function %qs");
3405 else if (DECL_COPY_CONSTRUCTOR_P (fn))
3406 return _("In copy constructor %qs");
3407 else if (DECL_CONSTRUCTOR_P (fn))
3408 return _("In constructor %qs");
3409 else if (DECL_DESTRUCTOR_P (fn))
3410 return _("In destructor %qs");
3411 else if (LAMBDA_FUNCTION_P (fn))
3412 return _("In lambda function");
3413 else
3414 return _("In member function %qs");
3416 else
3417 return _("In function %qs");
3420 /* Report the full context of a current template instantiation,
3421 onto BUFFER. */
3422 static void
3423 print_instantiation_full_context (diagnostic_context *context)
3425 struct tinst_level *p = current_instantiation ();
3426 location_t location = input_location;
3428 if (p)
3430 pp_verbatim (context->printer,
3431 TREE_CODE (p->decl) == TREE_LIST
3432 ? _("%s: In substitution of %qS:\n")
3433 : _("%s: In instantiation of %q#D:\n"),
3434 LOCATION_FILE (location),
3435 p->decl);
3437 location = p->locus;
3438 p = p->next;
3441 print_instantiation_partial_context (context, p, location);
3444 /* Helper function of print_instantiation_partial_context() that
3445 prints a single line of instantiation context. */
3447 static void
3448 print_instantiation_partial_context_line (diagnostic_context *context,
3449 const struct tinst_level *t,
3450 location_t loc, bool recursive_p)
3452 if (loc == UNKNOWN_LOCATION)
3453 return;
3455 expanded_location xloc = expand_location (loc);
3457 if (context->show_column)
3458 pp_verbatim (context->printer, _("%r%s:%d:%d:%R "),
3459 "locus", xloc.file, xloc.line, xloc.column);
3460 else
3461 pp_verbatim (context->printer, _("%r%s:%d:%R "),
3462 "locus", xloc.file, xloc.line);
3464 if (t != NULL)
3466 if (TREE_CODE (t->decl) == TREE_LIST)
3467 pp_verbatim (context->printer,
3468 recursive_p
3469 ? _("recursively required by substitution of %qS\n")
3470 : _("required by substitution of %qS\n"),
3471 t->decl);
3472 else
3473 pp_verbatim (context->printer,
3474 recursive_p
3475 ? _("recursively required from %q#D\n")
3476 : _("required from %q#D\n"),
3477 t->decl);
3479 else
3481 pp_verbatim (context->printer,
3482 recursive_p
3483 ? _("recursively required from here\n")
3484 : _("required from here\n"));
3488 /* Same as print_instantiation_full_context but less verbose. */
3490 static void
3491 print_instantiation_partial_context (diagnostic_context *context,
3492 struct tinst_level *t0, location_t loc)
3494 struct tinst_level *t;
3495 int n_total = 0;
3496 int n;
3497 location_t prev_loc = loc;
3499 for (t = t0; t != NULL; t = t->next)
3500 if (prev_loc != t->locus)
3502 prev_loc = t->locus;
3503 n_total++;
3506 t = t0;
3508 if (template_backtrace_limit
3509 && n_total > template_backtrace_limit)
3511 int skip = n_total - template_backtrace_limit;
3512 int head = template_backtrace_limit / 2;
3514 /* Avoid skipping just 1. If so, skip 2. */
3515 if (skip == 1)
3517 skip = 2;
3518 head = (template_backtrace_limit - 1) / 2;
3521 for (n = 0; n < head; n++)
3523 gcc_assert (t != NULL);
3524 if (loc != t->locus)
3525 print_instantiation_partial_context_line (context, t, loc,
3526 /*recursive_p=*/false);
3527 loc = t->locus;
3528 t = t->next;
3530 if (t != NULL && skip > 0)
3532 expanded_location xloc;
3533 xloc = expand_location (loc);
3534 if (context->show_column)
3535 pp_verbatim (context->printer,
3536 _("%r%s:%d:%d:%R [ skipping %d instantiation "
3537 "contexts, use -ftemplate-backtrace-limit=0 to "
3538 "disable ]\n"),
3539 "locus", xloc.file, xloc.line, xloc.column, skip);
3540 else
3541 pp_verbatim (context->printer,
3542 _("%r%s:%d:%R [ skipping %d instantiation "
3543 "contexts, use -ftemplate-backtrace-limit=0 to "
3544 "disable ]\n"),
3545 "locus", xloc.file, xloc.line, skip);
3547 do {
3548 loc = t->locus;
3549 t = t->next;
3550 } while (t != NULL && --skip > 0);
3554 while (t != NULL)
3556 while (t->next != NULL && t->locus == t->next->locus)
3558 loc = t->locus;
3559 t = t->next;
3561 print_instantiation_partial_context_line (context, t, loc,
3562 t->locus == loc);
3563 loc = t->locus;
3564 t = t->next;
3566 print_instantiation_partial_context_line (context, NULL, loc,
3567 /*recursive_p=*/false);
3570 /* Called from cp_thing to print the template context for an error. */
3571 static void
3572 maybe_print_instantiation_context (diagnostic_context *context)
3574 if (!problematic_instantiation_changed () || current_instantiation () == 0)
3575 return;
3577 record_last_problematic_instantiation ();
3578 print_instantiation_full_context (context);
3581 /* Report what constexpr call(s) we're trying to expand, if any. */
3583 void
3584 maybe_print_constexpr_context (diagnostic_context *context)
3586 vec<tree> call_stack = cx_error_context ();
3587 unsigned ix;
3588 tree t;
3590 FOR_EACH_VEC_ELT (call_stack, ix, t)
3592 expanded_location xloc = expand_location (EXPR_LOCATION (t));
3593 const char *s = expr_as_string (t, 0);
3594 if (context->show_column)
3595 pp_verbatim (context->printer,
3596 _("%r%s:%d:%d:%R in constexpr expansion of %qs"),
3597 "locus", xloc.file, xloc.line, xloc.column, s);
3598 else
3599 pp_verbatim (context->printer,
3600 _("%r%s:%d:%R in constexpr expansion of %qs"),
3601 "locus", xloc.file, xloc.line, s);
3602 pp_newline (context->printer);
3607 /* Return true iff TYPE_A and TYPE_B are template types that are
3608 meaningful to compare. */
3610 static bool
3611 comparable_template_types_p (tree type_a, tree type_b)
3613 if (!CLASS_TYPE_P (type_a))
3614 return false;
3615 if (!CLASS_TYPE_P (type_b))
3616 return false;
3618 tree tinfo_a = TYPE_TEMPLATE_INFO (type_a);
3619 tree tinfo_b = TYPE_TEMPLATE_INFO (type_b);
3620 if (!tinfo_a || !tinfo_b)
3621 return false;
3623 return TI_TEMPLATE (tinfo_a) == TI_TEMPLATE (tinfo_b);
3626 /* Start a new line indented by SPC spaces on PP. */
3628 static void
3629 newline_and_indent (pretty_printer *pp, int spc)
3631 pp_newline (pp);
3632 for (int i = 0; i < spc; i++)
3633 pp_space (pp);
3636 /* Generate a GC-allocated string for ARG, an expression or type. */
3638 static const char *
3639 arg_to_string (tree arg, bool verbose)
3641 if (TYPE_P (arg))
3642 return type_to_string (arg, verbose);
3643 else
3644 return expr_to_string (arg);
3647 /* Subroutine to type_to_string_with_compare and
3648 print_template_tree_comparison.
3650 Print a representation of ARG (an expression or type) to PP,
3651 colorizing it as "type-diff" if PP->show_color. */
3653 static void
3654 print_nonequal_arg (pretty_printer *pp, tree arg, bool verbose)
3656 pp_printf (pp, "%r%s%R",
3657 "type-diff",
3658 (arg
3659 ? arg_to_string (arg, verbose)
3660 : G_("(no argument)")));
3663 /* Recursively print template TYPE_A to PP, as compared to template TYPE_B.
3665 The types must satisfy comparable_template_types_p.
3667 If INDENT is 0, then this is equivalent to type_to_string (TYPE_A), but
3668 potentially colorizing/eliding in comparison with TYPE_B.
3670 For example given types:
3671 vector<map<int,double>>
3673 vector<map<int,float>>
3674 then the result on PP would be:
3675 vector<map<[...],double>>
3676 with type elision, and:
3677 vector<map<int,double>>
3678 without type elision.
3680 In both cases the parts of TYPE that differ from PEER will be colorized
3681 if pp_show_color (pp) is true. In the above example, this would be
3682 "double".
3684 If INDENT is non-zero, then the types are printed in a tree-like form
3685 which shows both types. In the above example, the result on PP would be:
3687 vector<
3688 map<
3689 [...],
3690 [double != float]>>
3692 and without type-elision would be:
3694 vector<
3695 map<
3696 int,
3697 [double != float]>>
3699 As before, the differing parts of the types are colorized if
3700 pp_show_color (pp) is true ("double" and "float" in this example).
3702 Template arguments in which both types are using the default arguments
3703 are not printed; if at least one of the two types is using a non-default
3704 argument, then that argument is printed (or both arguments for the
3705 tree-like print format). */
3707 static void
3708 print_template_differences (pretty_printer *pp, tree type_a, tree type_b,
3709 bool verbose, int indent)
3711 if (indent)
3712 newline_and_indent (pp, indent);
3714 tree tinfo_a = TYPE_TEMPLATE_INFO (type_a);
3715 tree tinfo_b = TYPE_TEMPLATE_INFO (type_b);
3717 pp_printf (pp, "%s<",
3718 IDENTIFIER_POINTER (DECL_NAME (TI_TEMPLATE (tinfo_a))));
3720 tree args_a = TI_ARGS (tinfo_a);
3721 tree args_b = TI_ARGS (tinfo_b);
3722 gcc_assert (TREE_CODE (args_a) == TREE_VEC);
3723 gcc_assert (TREE_CODE (args_b) == TREE_VEC);
3724 int flags = 0;
3725 int len_a = get_non_default_template_args_count (args_a, flags);
3726 args_a = INNERMOST_TEMPLATE_ARGS (args_a);
3727 int len_b = get_non_default_template_args_count (args_b, flags);
3728 args_b = INNERMOST_TEMPLATE_ARGS (args_b);
3729 /* Determine the maximum range of args for which non-default template args
3730 were used; beyond this, only default args (if any) were used, and so
3731 they will be equal from this point onwards.
3732 One of the two peers might have used default arguments within this
3733 range, but the other will be using non-default arguments, and so
3734 it's more readable to print both within this range, to highlight
3735 the differences. */
3736 int len_max = MAX (len_a, len_b);
3737 gcc_assert (TREE_CODE (args_a) == TREE_VEC);
3738 gcc_assert (TREE_CODE (args_b) == TREE_VEC);
3739 for (int idx = 0; idx < len_max; idx++)
3741 if (idx)
3742 pp_character (pp, ',');
3744 tree arg_a = TREE_VEC_ELT (args_a, idx);
3745 tree arg_b = TREE_VEC_ELT (args_b, idx);
3746 if (arg_a == arg_b)
3748 if (indent)
3749 newline_and_indent (pp, indent + 2);
3750 /* Can do elision here, printing "[...]". */
3751 if (flag_elide_type)
3752 pp_string (pp, G_("[...]"));
3753 else
3754 pp_string (pp, arg_to_string (arg_a, verbose));
3756 else
3758 int new_indent = indent ? indent + 2 : 0;
3759 if (comparable_template_types_p (arg_a, arg_b))
3760 print_template_differences (pp, arg_a, arg_b, verbose, new_indent);
3761 else
3762 if (indent)
3764 newline_and_indent (pp, indent + 2);
3765 pp_character (pp, '[');
3766 print_nonequal_arg (pp, arg_a, verbose);
3767 pp_string (pp, " != ");
3768 print_nonequal_arg (pp, arg_b, verbose);
3769 pp_character (pp, ']');
3771 else
3772 print_nonequal_arg (pp, arg_a, verbose);
3775 pp_printf (pp, ">");
3778 /* As type_to_string, but for a template, potentially colorizing/eliding
3779 in comparison with PEER.
3780 For example, if TYPE is map<int,double> and PEER is map<int,int>,
3781 then the resulting string would be:
3782 map<[...],double>
3783 with type elision, and:
3784 map<int,double>
3785 without type elision.
3787 In both cases the parts of TYPE that differ from PEER will be colorized
3788 if SHOW_COLOR is true. In the above example, this would be "double".
3790 Template arguments in which both types are using the default arguments
3791 are not printed; if at least one of the two types is using a non-default
3792 argument, then both arguments are printed.
3794 The resulting string is in a GC-allocated buffer. */
3796 static const char *
3797 type_to_string_with_compare (tree type, tree peer, bool verbose,
3798 bool show_color)
3800 pretty_printer inner_pp;
3801 pretty_printer *pp = &inner_pp;
3802 pp_show_color (pp) = show_color;
3804 print_template_differences (pp, type, peer, verbose, 0);
3805 return pp_ggc_formatted_text (pp);
3808 /* Recursively print a tree-like comparison of TYPE_A and TYPE_B to PP,
3809 indented by INDENT spaces.
3811 For example given types:
3813 vector<map<int,double>>
3817 vector<map<double,float>>
3819 the output with type elision would be:
3821 vector<
3822 map<
3823 [...],
3824 [double != float]>>
3826 and without type-elision would be:
3828 vector<
3829 map<
3830 int,
3831 [double != float]>>
3833 TYPE_A and TYPE_B must both be comparable template types
3834 (as per comparable_template_types_p).
3836 Template arguments in which both types are using the default arguments
3837 are not printed; if at least one of the two types is using a non-default
3838 argument, then both arguments are printed. */
3840 static void
3841 print_template_tree_comparison (pretty_printer *pp, tree type_a, tree type_b,
3842 bool verbose, int indent)
3844 print_template_differences (pp, type_a, type_b, verbose, indent);
3847 /* Subroutine for use in a format_postprocessor::handle
3848 implementation. Adds a chunk to the end of
3849 formatted output, so that it will be printed
3850 by pp_output_formatted_text. */
3852 static void
3853 append_formatted_chunk (pretty_printer *pp, const char *content)
3855 output_buffer *buffer = pp_buffer (pp);
3856 struct chunk_info *chunk_array = buffer->cur_chunk_array;
3857 const char **args = chunk_array->args;
3859 unsigned int chunk_idx;
3860 for (chunk_idx = 0; args[chunk_idx]; chunk_idx++)
3862 args[chunk_idx++] = content;
3863 args[chunk_idx] = NULL;
3866 /* Create a copy of CONTENT, with quotes added, and,
3867 potentially, with colorization.
3868 No escaped is performed on CONTENT.
3869 The result is in a GC-allocated buffer. */
3871 static const char *
3872 add_quotes (const char *content, bool show_color)
3874 pretty_printer tmp_pp;
3875 pp_show_color (&tmp_pp) = show_color;
3877 /* We have to use "%<%s%>" rather than "%qs" here in order to avoid
3878 quoting colorization bytes within the results. */
3879 pp_printf (&tmp_pp, "%<%s%>", content);
3881 return pp_ggc_formatted_text (&tmp_pp);
3884 /* If we had %H and %I, and hence deferred printing them,
3885 print them now, storing the result into the chunk_info
3886 for pp_format. Quote them if 'q' was provided.
3887 Also print the difference in tree form, adding it as
3888 an additional chunk. */
3890 void
3891 cxx_format_postprocessor::handle (pretty_printer *pp)
3893 /* If we have one of %H and %I, the other should have
3894 been present. */
3895 if (m_type_a.m_tree || m_type_b.m_tree)
3897 /* Avoid reentrancy issues by working with a copy of
3898 m_type_a and m_type_b, resetting them now. */
3899 deferred_printed_type type_a = m_type_a;
3900 deferred_printed_type type_b = m_type_b;
3901 m_type_a = deferred_printed_type ();
3902 m_type_b = deferred_printed_type ();
3904 gcc_assert (type_a.m_buffer_ptr);
3905 gcc_assert (type_b.m_buffer_ptr);
3907 bool show_color = pp_show_color (pp);
3909 const char *type_a_text;
3910 const char *type_b_text;
3912 if (comparable_template_types_p (type_a.m_tree, type_b.m_tree))
3914 type_a_text
3915 = type_to_string_with_compare (type_a.m_tree, type_b.m_tree,
3916 type_a.m_verbose, show_color);
3917 type_b_text
3918 = type_to_string_with_compare (type_b.m_tree, type_a.m_tree,
3919 type_b.m_verbose, show_color);
3921 if (flag_diagnostics_show_template_tree)
3923 pretty_printer inner_pp;
3924 pp_show_color (&inner_pp) = pp_show_color (pp);
3925 print_template_tree_comparison
3926 (&inner_pp, type_a.m_tree, type_b.m_tree, type_a.m_verbose, 2);
3927 append_formatted_chunk (pp, pp_ggc_formatted_text (&inner_pp));
3930 else
3932 /* If the types were not comparable, they are printed normally,
3933 and no difference tree is printed. */
3934 type_a_text = type_to_string (type_a.m_tree, type_a.m_verbose);
3935 type_b_text = type_to_string (type_b.m_tree, type_b.m_verbose);
3938 if (type_a.m_quote)
3939 type_a_text = add_quotes (type_a_text, show_color);
3940 *type_a.m_buffer_ptr = type_a_text;
3942 if (type_b.m_quote)
3943 type_b_text = add_quotes (type_b_text, show_color);
3944 *type_b.m_buffer_ptr = type_b_text;
3948 /* Subroutine for handling %H and %I, to support i18n of messages like:
3950 error_at (loc, "could not convert %qE from %qH to %qI",
3951 expr, type_a, type_b);
3953 so that we can print things like:
3955 could not convert 'foo' from 'map<int,double>' to 'map<int,int>'
3957 and, with type-elision:
3959 could not convert 'foo' from 'map<[...],double>' to 'map<[...],int>'
3961 (with color-coding of the differences between the types).
3963 The %H and %I format codes are peers: both must be present,
3964 and they affect each other. Hence to handle them, we must
3965 delay printing until we have both, deferring the printing to
3966 pretty_printer's m_format_postprocessor hook.
3968 This is called in phase 2 of pp_format, when it is accumulating
3969 a series of formatted chunks. We stash the location of the chunk
3970 we're meant to have written to, so that we can write to it in the
3971 m_format_postprocessor hook.
3973 We also need to stash whether a 'q' prefix was provided (the QUOTE
3974 param) so that we can add the quotes when writing out the delayed
3975 chunk. */
3977 static void
3978 defer_phase_2_of_type_diff (deferred_printed_type *deferred,
3979 tree type, const char **buffer_ptr,
3980 bool verbose, bool quote)
3982 gcc_assert (deferred->m_tree == NULL_TREE);
3983 gcc_assert (deferred->m_buffer_ptr == NULL);
3984 *deferred = deferred_printed_type (type, buffer_ptr, verbose, quote);
3988 /* Called from output_format -- during diagnostic message processing --
3989 to handle C++ specific format specifier with the following meanings:
3990 %A function argument-list.
3991 %C tree code.
3992 %D declaration.
3993 %E expression.
3994 %F function declaration.
3995 %L language as used in extern "lang".
3996 %O binary operator.
3997 %P function parameter whose position is indicated by an integer.
3998 %Q assignment operator.
3999 %S substitution (template + args)
4000 %T type.
4001 %V cv-qualifier.
4002 %X exception-specification.
4003 %H type difference (from)
4004 %I type difference (to). */
4005 static bool
4006 cp_printer (pretty_printer *pp, text_info *text, const char *spec,
4007 int precision, bool wide, bool set_locus, bool verbose,
4008 bool quoted, const char **buffer_ptr)
4010 gcc_assert (pp->m_format_postprocessor);
4011 cxx_format_postprocessor *postprocessor
4012 = static_cast <cxx_format_postprocessor *> (pp->m_format_postprocessor);
4014 const char *result;
4015 tree t = NULL;
4016 #define next_tree (t = va_arg (*text->args_ptr, tree))
4017 #define next_tcode ((enum tree_code) va_arg (*text->args_ptr, int))
4018 #define next_lang ((enum languages) va_arg (*text->args_ptr, int))
4019 #define next_int va_arg (*text->args_ptr, int)
4021 if (precision != 0 || wide)
4022 return false;
4024 switch (*spec)
4026 case 'A': result = args_to_string (next_tree, verbose); break;
4027 case 'C': result = code_to_string (next_tcode); break;
4028 case 'D':
4030 tree temp = next_tree;
4031 if (VAR_P (temp)
4032 && DECL_HAS_DEBUG_EXPR_P (temp))
4034 temp = DECL_DEBUG_EXPR (temp);
4035 if (!DECL_P (temp))
4037 result = expr_to_string (temp);
4038 break;
4041 result = decl_to_string (temp, verbose);
4043 break;
4044 case 'E': result = expr_to_string (next_tree); break;
4045 case 'F': result = fndecl_to_string (next_tree, verbose); break;
4046 case 'L': result = language_to_string (next_lang); break;
4047 case 'O': result = op_to_string (next_tcode); break;
4048 case 'P': result = parm_to_string (next_int); break;
4049 case 'Q': result = assop_to_string (next_tcode); break;
4050 case 'S': result = subst_to_string (next_tree); break;
4051 case 'T': result = type_to_string (next_tree, verbose); break;
4052 case 'V': result = cv_to_string (next_tree, verbose); break;
4053 case 'X': result = eh_spec_to_string (next_tree, verbose); break;
4055 case 'G':
4056 percent_G_format (text);
4057 return true;
4059 case 'K':
4060 t = va_arg (*text->args_ptr, tree);
4061 percent_K_format (text, t);
4062 return true;
4064 case 'H':
4066 defer_phase_2_of_type_diff (&postprocessor->m_type_a, next_tree,
4067 buffer_ptr, verbose, quoted);
4068 return true;
4071 case 'I':
4073 defer_phase_2_of_type_diff (&postprocessor->m_type_b, next_tree,
4074 buffer_ptr, verbose, quoted);
4075 return true;
4078 default:
4079 return false;
4082 pp_string (pp, result);
4083 if (set_locus && t != NULL)
4084 text->set_location (0, location_of (t), true);
4085 return true;
4086 #undef next_tree
4087 #undef next_tcode
4088 #undef next_lang
4089 #undef next_int
4092 /* Warn about the use of C++0x features when appropriate. */
4093 void
4094 maybe_warn_cpp0x (cpp0x_warn_str str)
4096 if ((cxx_dialect == cxx98) && !in_system_header_at (input_location))
4097 /* We really want to suppress this warning in system headers,
4098 because libstdc++ uses variadic templates even when we aren't
4099 in C++0x mode. */
4100 switch (str)
4102 case CPP0X_INITIALIZER_LISTS:
4103 pedwarn (input_location, 0,
4104 "extended initializer lists "
4105 "only available with -std=c++11 or -std=gnu++11");
4106 break;
4107 case CPP0X_EXPLICIT_CONVERSION:
4108 pedwarn (input_location, 0,
4109 "explicit conversion operators "
4110 "only available with -std=c++11 or -std=gnu++11");
4111 break;
4112 case CPP0X_VARIADIC_TEMPLATES:
4113 pedwarn (input_location, 0,
4114 "variadic templates "
4115 "only available with -std=c++11 or -std=gnu++11");
4116 break;
4117 case CPP0X_LAMBDA_EXPR:
4118 pedwarn (input_location, 0,
4119 "lambda expressions "
4120 "only available with -std=c++11 or -std=gnu++11");
4121 break;
4122 case CPP0X_AUTO:
4123 pedwarn (input_location, 0,
4124 "C++11 auto only available with -std=c++11 or -std=gnu++11");
4125 break;
4126 case CPP0X_SCOPED_ENUMS:
4127 pedwarn (input_location, 0,
4128 "scoped enums only available with -std=c++11 or -std=gnu++11");
4129 break;
4130 case CPP0X_DEFAULTED_DELETED:
4131 pedwarn (input_location, 0,
4132 "defaulted and deleted functions "
4133 "only available with -std=c++11 or -std=gnu++11");
4134 break;
4135 case CPP0X_INLINE_NAMESPACES:
4136 pedwarn (input_location, OPT_Wpedantic,
4137 "inline namespaces "
4138 "only available with -std=c++11 or -std=gnu++11");
4139 break;
4140 case CPP0X_OVERRIDE_CONTROLS:
4141 pedwarn (input_location, 0,
4142 "override controls (override/final) "
4143 "only available with -std=c++11 or -std=gnu++11");
4144 break;
4145 case CPP0X_NSDMI:
4146 pedwarn (input_location, 0,
4147 "non-static data member initializers "
4148 "only available with -std=c++11 or -std=gnu++11");
4149 break;
4150 case CPP0X_USER_DEFINED_LITERALS:
4151 pedwarn (input_location, 0,
4152 "user-defined literals "
4153 "only available with -std=c++11 or -std=gnu++11");
4154 break;
4155 case CPP0X_DELEGATING_CTORS:
4156 pedwarn (input_location, 0,
4157 "delegating constructors "
4158 "only available with -std=c++11 or -std=gnu++11");
4159 break;
4160 case CPP0X_INHERITING_CTORS:
4161 pedwarn (input_location, 0,
4162 "inheriting constructors "
4163 "only available with -std=c++11 or -std=gnu++11");
4164 break;
4165 case CPP0X_ATTRIBUTES:
4166 pedwarn (input_location, 0,
4167 "c++11 attributes "
4168 "only available with -std=c++11 or -std=gnu++11");
4169 break;
4170 case CPP0X_REF_QUALIFIER:
4171 pedwarn (input_location, 0,
4172 "ref-qualifiers "
4173 "only available with -std=c++11 or -std=gnu++11");
4174 break;
4175 default:
4176 gcc_unreachable ();
4180 /* Warn about the use of variadic templates when appropriate. */
4181 void
4182 maybe_warn_variadic_templates (void)
4184 maybe_warn_cpp0x (CPP0X_VARIADIC_TEMPLATES);
4188 /* Issue an ISO C++98 pedantic warning at LOCATION, conditional on
4189 option OPT with text GMSGID. Use this function to report
4190 diagnostics for constructs that are invalid C++98, but valid
4191 C++0x. */
4192 bool
4193 pedwarn_cxx98 (location_t location, int opt, const char *gmsgid, ...)
4195 diagnostic_info diagnostic;
4196 va_list ap;
4197 bool ret;
4198 rich_location richloc (line_table, location);
4200 va_start (ap, gmsgid);
4201 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
4202 (cxx_dialect == cxx98) ? DK_PEDWARN : DK_WARNING);
4203 diagnostic.option_index = opt;
4204 ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
4205 va_end (ap);
4206 return ret;
4209 /* Issue a diagnostic that NAME cannot be found in SCOPE. DECL is what
4210 we found when we tried to do the lookup. LOCATION is the location of
4211 the NAME identifier. */
4213 void
4214 qualified_name_lookup_error (tree scope, tree name,
4215 tree decl, location_t location)
4217 if (scope == error_mark_node)
4218 ; /* We already complained. */
4219 else if (TYPE_P (scope))
4221 if (!COMPLETE_TYPE_P (scope))
4222 error_at (location, "incomplete type %qT used in nested name specifier",
4223 scope);
4224 else if (TREE_CODE (decl) == TREE_LIST)
4226 error_at (location, "reference to %<%T::%D%> is ambiguous",
4227 scope, name);
4228 print_candidates (decl);
4230 else
4231 error_at (location, "%qD is not a member of %qT", name, scope);
4233 else if (scope != global_namespace)
4235 error_at (location, "%qD is not a member of %qD", name, scope);
4236 if (!suggest_alternative_in_explicit_scope (location, name, scope))
4237 suggest_alternatives_for (location, name, false);
4239 else
4241 error_at (location, "%<::%D%> has not been declared", name);
4242 suggest_alternatives_for (location, name, true);