Add quotes for constexpr keyword.
[official-gcc.git] / gcc / cp / error.c
blob6d1f3da74e2eff5722a2dcd0a9df0d921f427aa7
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 *code_to_string (enum tree_code);
54 static const char *cv_to_string (tree, int);
55 static const char *decl_to_string (tree, int);
56 static const char *expr_to_string (tree);
57 static const char *fndecl_to_string (tree, int);
58 static const char *op_to_string (bool, enum tree_code);
59 static const char *parm_to_string (int);
60 static const char *type_to_string (tree, int);
62 static void dump_alias_template_specialization (cxx_pretty_printer *, tree, int);
63 static void dump_type (cxx_pretty_printer *, tree, int);
64 static void dump_typename (cxx_pretty_printer *, tree, int);
65 static void dump_simple_decl (cxx_pretty_printer *, tree, tree, int);
66 static void dump_decl (cxx_pretty_printer *, tree, int);
67 static void dump_template_decl (cxx_pretty_printer *, tree, int);
68 static void dump_function_decl (cxx_pretty_printer *, tree, int);
69 static void dump_expr (cxx_pretty_printer *, tree, int);
70 static void dump_unary_op (cxx_pretty_printer *, const char *, tree, int);
71 static void dump_binary_op (cxx_pretty_printer *, const char *, tree, int);
72 static void dump_aggr_type (cxx_pretty_printer *, tree, int);
73 static void dump_type_prefix (cxx_pretty_printer *, tree, int);
74 static void dump_type_suffix (cxx_pretty_printer *, tree, int);
75 static void dump_function_name (cxx_pretty_printer *, tree, int);
76 static void dump_call_expr_args (cxx_pretty_printer *, tree, int, bool);
77 static void dump_aggr_init_expr_args (cxx_pretty_printer *, tree, int, bool);
78 static void dump_expr_list (cxx_pretty_printer *, tree, int);
79 static void dump_global_iord (cxx_pretty_printer *, tree);
80 static void dump_parameters (cxx_pretty_printer *, tree, int);
81 static void dump_ref_qualifier (cxx_pretty_printer *, tree, int);
82 static void dump_exception_spec (cxx_pretty_printer *, tree, int);
83 static void dump_template_argument (cxx_pretty_printer *, tree, int);
84 static void dump_template_argument_list (cxx_pretty_printer *, tree, int);
85 static void dump_template_parameter (cxx_pretty_printer *, tree, int);
86 static void dump_template_bindings (cxx_pretty_printer *, tree, tree,
87 vec<tree, va_gc> *);
88 static void dump_scope (cxx_pretty_printer *, tree, int);
89 static void dump_template_parms (cxx_pretty_printer *, tree, int, int);
90 static int get_non_default_template_args_count (tree, int);
91 static const char *function_category (tree);
92 static void maybe_print_constexpr_context (diagnostic_context *);
93 static void maybe_print_instantiation_context (diagnostic_context *);
94 static void print_instantiation_full_context (diagnostic_context *);
95 static void print_instantiation_partial_context (diagnostic_context *,
96 struct tinst_level *,
97 location_t);
98 static void cp_diagnostic_starter (diagnostic_context *, diagnostic_info *);
99 static void cp_print_error_function (diagnostic_context *, diagnostic_info *);
101 static bool cp_printer (pretty_printer *, text_info *, const char *,
102 int, bool, bool, bool, bool, const char **);
104 /* Struct for handling %H or %I, which require delaying printing the
105 type until a postprocessing stage. */
107 struct deferred_printed_type
109 deferred_printed_type ()
110 : m_tree (NULL_TREE), m_buffer_ptr (NULL), m_verbose (false), m_quote (false)
113 deferred_printed_type (tree type, const char **buffer_ptr, bool verbose,
114 bool quote)
115 : m_tree (type), m_buffer_ptr (buffer_ptr), m_verbose (verbose),
116 m_quote (quote)
118 gcc_assert (type);
119 gcc_assert (buffer_ptr);
122 /* The tree is not GTY-marked: they are only non-NULL within a
123 call to pp_format. */
124 tree m_tree;
125 const char **m_buffer_ptr;
126 bool m_verbose;
127 bool m_quote;
130 /* Subclass of format_postprocessor for the C++ frontend.
131 This handles the %H and %I formatting codes, printing them
132 in a postprocessing phase (since they affect each other). */
134 class cxx_format_postprocessor : public format_postprocessor
136 public:
137 cxx_format_postprocessor ()
138 : m_type_a (), m_type_b ()
141 void handle (pretty_printer *pp) FINAL OVERRIDE;
143 deferred_printed_type m_type_a;
144 deferred_printed_type m_type_b;
147 /* CONTEXT->printer is a basic pretty printer that was constructed
148 presumably by diagnostic_initialize(), called early in the
149 compiler's initialization process (in general_init) Before the FE
150 is initialized. This (C++) FE-specific diagnostic initializer is
151 thus replacing the basic pretty printer with one that has C++-aware
152 capacities. */
154 void
155 cxx_initialize_diagnostics (diagnostic_context *context)
157 pretty_printer *base = context->printer;
158 cxx_pretty_printer *pp = XNEW (cxx_pretty_printer);
159 context->printer = new (pp) cxx_pretty_printer ();
161 /* It is safe to free this object because it was previously XNEW()'d. */
162 base->~pretty_printer ();
163 XDELETE (base);
165 c_common_diagnostics_set_defaults (context);
166 diagnostic_starter (context) = cp_diagnostic_starter;
167 /* diagnostic_finalizer is already c_diagnostic_finalizer. */
168 diagnostic_format_decoder (context) = cp_printer;
169 pp->m_format_postprocessor = new cxx_format_postprocessor ();
172 /* Dump a scope, if deemed necessary. */
174 static void
175 dump_scope (cxx_pretty_printer *pp, tree scope, int flags)
177 int f = flags & (TFF_SCOPE | TFF_CHASE_TYPEDEF);
179 if (scope == NULL_TREE)
180 return;
182 if (TREE_CODE (scope) == NAMESPACE_DECL)
184 if (scope != global_namespace)
186 dump_decl (pp, scope, f);
187 pp_cxx_colon_colon (pp);
190 else if (AGGREGATE_TYPE_P (scope))
192 dump_type (pp, scope, f);
193 pp_cxx_colon_colon (pp);
195 else if ((flags & TFF_SCOPE) && TREE_CODE (scope) == FUNCTION_DECL)
197 dump_function_decl (pp, scope, f);
198 pp_cxx_colon_colon (pp);
202 /* Dump the template ARGument under control of FLAGS. */
204 static void
205 dump_template_argument (cxx_pretty_printer *pp, tree arg, int flags)
207 if (ARGUMENT_PACK_P (arg))
208 dump_template_argument_list (pp, ARGUMENT_PACK_ARGS (arg),
209 /* No default args in argument packs. */
210 flags|TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS);
211 else if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
212 dump_type (pp, arg, flags & ~TFF_CLASS_KEY_OR_ENUM);
213 else
215 if (TREE_CODE (arg) == TREE_LIST)
216 arg = TREE_VALUE (arg);
218 /* Strip implicit conversions. */
219 while (CONVERT_EXPR_P (arg))
220 arg = TREE_OPERAND (arg, 0);
222 dump_expr (pp, arg, (flags | TFF_EXPR_IN_PARENS) & ~TFF_CLASS_KEY_OR_ENUM);
226 /* Count the number of template arguments ARGS whose value does not
227 match the (optional) default template parameter in PARAMS */
229 static int
230 get_non_default_template_args_count (tree args, int flags)
232 int n = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_ARGS (args));
234 if (/* We use this flag when generating debug information. We don't
235 want to expand templates at this point, for this may generate
236 new decls, which gets decl counts out of sync, which may in
237 turn cause codegen differences between compilations with and
238 without -g. */
239 (flags & TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS) != 0
240 || !flag_pretty_templates)
241 return n;
243 return GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (INNERMOST_TEMPLATE_ARGS (args));
246 /* Dump a template-argument-list ARGS (always a TREE_VEC) under control
247 of FLAGS. */
249 static void
250 dump_template_argument_list (cxx_pretty_printer *pp, tree args, int flags)
252 int n = get_non_default_template_args_count (args, flags);
253 int need_comma = 0;
254 int i;
256 for (i = 0; i < n; ++i)
258 tree arg = TREE_VEC_ELT (args, i);
260 /* Only print a comma if we know there is an argument coming. In
261 the case of an empty template argument pack, no actual
262 argument will be printed. */
263 if (need_comma
264 && (!ARGUMENT_PACK_P (arg)
265 || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) > 0))
266 pp_separate_with_comma (pp);
268 dump_template_argument (pp, arg, flags);
269 need_comma = 1;
273 /* Dump a template parameter PARM (a TREE_LIST) under control of FLAGS. */
275 static void
276 dump_template_parameter (cxx_pretty_printer *pp, tree parm, int flags)
278 tree p;
279 tree a;
281 if (parm == error_mark_node)
282 return;
284 p = TREE_VALUE (parm);
285 a = TREE_PURPOSE (parm);
287 if (TREE_CODE (p) == TYPE_DECL)
289 if (flags & TFF_DECL_SPECIFIERS)
291 pp_cxx_ws_string (pp, "class");
292 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (p)))
293 pp_cxx_ws_string (pp, "...");
294 if (DECL_NAME (p))
295 pp_cxx_tree_identifier (pp, DECL_NAME (p));
297 else if (DECL_NAME (p))
298 pp_cxx_tree_identifier (pp, DECL_NAME (p));
299 else
300 pp_cxx_canonical_template_parameter (pp, TREE_TYPE (p));
302 else
303 dump_decl (pp, p, flags | TFF_DECL_SPECIFIERS);
305 if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && a != NULL_TREE)
307 pp_cxx_whitespace (pp);
308 pp_equal (pp);
309 pp_cxx_whitespace (pp);
310 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
311 dump_type (pp, a, flags & ~TFF_CHASE_TYPEDEF);
312 else
313 dump_expr (pp, a, flags | TFF_EXPR_IN_PARENS);
317 /* Dump, under control of FLAGS, a template-parameter-list binding.
318 PARMS is a TREE_LIST of TREE_VEC of TREE_LIST and ARGS is a
319 TREE_VEC. */
321 static void
322 dump_template_bindings (cxx_pretty_printer *pp, tree parms, tree args,
323 vec<tree, va_gc> *typenames)
325 bool need_semicolon = false;
326 int i;
327 tree t;
329 while (parms)
331 tree p = TREE_VALUE (parms);
332 int lvl = TMPL_PARMS_DEPTH (parms);
333 int arg_idx = 0;
334 int i;
335 tree lvl_args = NULL_TREE;
337 /* Don't crash if we had an invalid argument list. */
338 if (TMPL_ARGS_DEPTH (args) >= lvl)
339 lvl_args = TMPL_ARGS_LEVEL (args, lvl);
341 for (i = 0; i < TREE_VEC_LENGTH (p); ++i)
343 tree arg = NULL_TREE;
345 /* Don't crash if we had an invalid argument list. */
346 if (lvl_args && NUM_TMPL_ARGS (lvl_args) > arg_idx)
347 arg = TREE_VEC_ELT (lvl_args, arg_idx);
349 if (need_semicolon)
350 pp_separate_with_semicolon (pp);
351 dump_template_parameter (pp, TREE_VEC_ELT (p, i),
352 TFF_PLAIN_IDENTIFIER);
353 pp_cxx_whitespace (pp);
354 pp_equal (pp);
355 pp_cxx_whitespace (pp);
356 if (arg)
358 if (ARGUMENT_PACK_P (arg))
359 pp_cxx_left_brace (pp);
360 dump_template_argument (pp, arg, TFF_PLAIN_IDENTIFIER);
361 if (ARGUMENT_PACK_P (arg))
362 pp_cxx_right_brace (pp);
364 else
365 pp_string (pp, M_("<missing>"));
367 ++arg_idx;
368 need_semicolon = true;
371 parms = TREE_CHAIN (parms);
374 /* Don't bother with typenames for a partial instantiation. */
375 if (vec_safe_is_empty (typenames) || uses_template_parms (args))
376 return;
378 /* Don't try to print typenames when we're processing a clone. */
379 if (current_function_decl
380 && !DECL_LANG_SPECIFIC (current_function_decl))
381 return;
383 /* Don't try to do this once cgraph starts throwing away front-end
384 information. */
385 if (at_eof >= 2)
386 return;
388 FOR_EACH_VEC_SAFE_ELT (typenames, i, t)
390 if (need_semicolon)
391 pp_separate_with_semicolon (pp);
392 dump_type (pp, t, TFF_PLAIN_IDENTIFIER);
393 pp_cxx_whitespace (pp);
394 pp_equal (pp);
395 pp_cxx_whitespace (pp);
396 push_deferring_access_checks (dk_no_check);
397 t = tsubst (t, args, tf_none, NULL_TREE);
398 pop_deferring_access_checks ();
399 /* Strip typedefs. We can't just use TFF_CHASE_TYPEDEF because
400 pp_simple_type_specifier doesn't know about it. */
401 t = strip_typedefs (t);
402 dump_type (pp, t, TFF_PLAIN_IDENTIFIER);
406 /* Dump a human-readable equivalent of the alias template
407 specialization of T. */
409 static void
410 dump_alias_template_specialization (cxx_pretty_printer *pp, tree t, int flags)
412 gcc_assert (alias_template_specialization_p (t));
414 tree decl = TYPE_NAME (t);
415 if (!(flags & TFF_UNQUALIFIED_NAME))
416 dump_scope (pp, CP_DECL_CONTEXT (decl), flags);
417 pp_cxx_tree_identifier (pp, DECL_NAME (decl));
418 dump_template_parms (pp, DECL_TEMPLATE_INFO (decl),
419 /*primary=*/false,
420 flags & ~TFF_TEMPLATE_HEADER);
423 /* Dump a human-readable equivalent of TYPE. FLAGS controls the
424 format. */
426 static void
427 dump_type (cxx_pretty_printer *pp, tree t, int flags)
429 if (t == NULL_TREE)
430 return;
432 /* Don't print e.g. "struct mytypedef". */
433 if (TYPE_P (t) && typedef_variant_p (t))
435 tree decl = TYPE_NAME (t);
436 if ((flags & TFF_CHASE_TYPEDEF)
437 || DECL_SELF_REFERENCE_P (decl)
438 || (!flag_pretty_templates
439 && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)))
440 t = strip_typedefs (t);
441 else if (alias_template_specialization_p (t))
443 dump_alias_template_specialization (pp, t, flags);
444 return;
446 else if (same_type_p (t, TREE_TYPE (decl)))
447 t = decl;
448 else
450 pp_cxx_cv_qualifier_seq (pp, t);
451 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
452 return;
456 if (TYPE_PTRMEMFUNC_P (t))
457 goto offset_type;
459 switch (TREE_CODE (t))
461 case LANG_TYPE:
462 if (t == init_list_type_node)
463 pp_string (pp, M_("<brace-enclosed initializer list>"));
464 else if (t == unknown_type_node)
465 pp_string (pp, M_("<unresolved overloaded function type>"));
466 else
468 pp_cxx_cv_qualifier_seq (pp, t);
469 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
471 break;
473 case TREE_LIST:
474 /* A list of function parms. */
475 dump_parameters (pp, t, flags);
476 break;
478 case IDENTIFIER_NODE:
479 pp_cxx_tree_identifier (pp, t);
480 break;
482 case TREE_BINFO:
483 dump_type (pp, BINFO_TYPE (t), flags);
484 break;
486 case RECORD_TYPE:
487 case UNION_TYPE:
488 case ENUMERAL_TYPE:
489 dump_aggr_type (pp, t, flags);
490 break;
492 case TYPE_DECL:
493 if (flags & TFF_CHASE_TYPEDEF)
495 dump_type (pp, DECL_ORIGINAL_TYPE (t)
496 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t), flags);
497 break;
499 /* Fall through. */
501 case TEMPLATE_DECL:
502 case NAMESPACE_DECL:
503 dump_decl (pp, t, flags & ~TFF_DECL_SPECIFIERS);
504 break;
506 case INTEGER_TYPE:
507 case REAL_TYPE:
508 case VOID_TYPE:
509 case BOOLEAN_TYPE:
510 case COMPLEX_TYPE:
511 case VECTOR_TYPE:
512 case FIXED_POINT_TYPE:
513 pp_type_specifier_seq (pp, t);
514 break;
516 case TEMPLATE_TEMPLATE_PARM:
517 /* For parameters inside template signature. */
518 if (TYPE_IDENTIFIER (t))
519 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
520 else
521 pp_cxx_canonical_template_parameter (pp, t);
522 break;
524 case BOUND_TEMPLATE_TEMPLATE_PARM:
526 tree args = TYPE_TI_ARGS (t);
527 pp_cxx_cv_qualifier_seq (pp, t);
528 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
529 pp_cxx_begin_template_argument_list (pp);
530 dump_template_argument_list (pp, args, flags);
531 pp_cxx_end_template_argument_list (pp);
533 break;
535 case TEMPLATE_TYPE_PARM:
536 pp_cxx_cv_qualifier_seq (pp, t);
537 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
538 pp_cxx_constrained_type_spec (pp, c);
539 else if (TYPE_IDENTIFIER (t))
540 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
541 else
542 pp_cxx_canonical_template_parameter
543 (pp, TEMPLATE_TYPE_PARM_INDEX (t));
544 break;
546 /* This is not always necessary for pointers and such, but doing this
547 reduces code size. */
548 case ARRAY_TYPE:
549 case POINTER_TYPE:
550 case REFERENCE_TYPE:
551 case OFFSET_TYPE:
552 offset_type:
553 case FUNCTION_TYPE:
554 case METHOD_TYPE:
556 dump_type_prefix (pp, t, flags);
557 dump_type_suffix (pp, t, flags);
558 break;
560 case TYPENAME_TYPE:
561 if (! (flags & TFF_CHASE_TYPEDEF)
562 && DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
564 dump_decl (pp, TYPE_NAME (t), TFF_PLAIN_IDENTIFIER);
565 break;
567 pp_cxx_cv_qualifier_seq (pp, t);
568 pp_cxx_ws_string (pp,
569 TYPENAME_IS_ENUM_P (t) ? "enum"
570 : TYPENAME_IS_CLASS_P (t) ? "class"
571 : "typename");
572 dump_typename (pp, t, flags);
573 break;
575 case UNBOUND_CLASS_TEMPLATE:
576 if (! (flags & TFF_UNQUALIFIED_NAME))
578 dump_type (pp, TYPE_CONTEXT (t), flags);
579 pp_cxx_colon_colon (pp);
581 pp_cxx_ws_string (pp, "template");
582 dump_type (pp, TYPE_IDENTIFIER (t), flags);
583 break;
585 case TYPEOF_TYPE:
586 pp_cxx_ws_string (pp, "__typeof__");
587 pp_cxx_whitespace (pp);
588 pp_cxx_left_paren (pp);
589 dump_expr (pp, TYPEOF_TYPE_EXPR (t), flags & ~TFF_EXPR_IN_PARENS);
590 pp_cxx_right_paren (pp);
591 break;
593 case UNDERLYING_TYPE:
594 pp_cxx_ws_string (pp, "__underlying_type");
595 pp_cxx_whitespace (pp);
596 pp_cxx_left_paren (pp);
597 dump_expr (pp, UNDERLYING_TYPE_TYPE (t), flags & ~TFF_EXPR_IN_PARENS);
598 pp_cxx_right_paren (pp);
599 break;
601 case TYPE_PACK_EXPANSION:
602 dump_type (pp, PACK_EXPANSION_PATTERN (t), flags);
603 pp_cxx_ws_string (pp, "...");
604 break;
606 case TYPE_ARGUMENT_PACK:
607 dump_template_argument (pp, t, flags);
608 break;
610 case DECLTYPE_TYPE:
611 pp_cxx_ws_string (pp, "decltype");
612 pp_cxx_whitespace (pp);
613 pp_cxx_left_paren (pp);
614 dump_expr (pp, DECLTYPE_TYPE_EXPR (t), flags & ~TFF_EXPR_IN_PARENS);
615 pp_cxx_right_paren (pp);
616 break;
618 case NULLPTR_TYPE:
619 pp_string (pp, "std::nullptr_t");
620 break;
622 default:
623 pp_unsupported_tree (pp, t);
624 /* Fall through. */
626 case ERROR_MARK:
627 pp_string (pp, M_("<type error>"));
628 break;
632 /* Dump a TYPENAME_TYPE. We need to notice when the context is itself
633 a TYPENAME_TYPE. */
635 static void
636 dump_typename (cxx_pretty_printer *pp, tree t, int flags)
638 tree ctx = TYPE_CONTEXT (t);
640 if (TREE_CODE (ctx) == TYPENAME_TYPE)
641 dump_typename (pp, ctx, flags);
642 else
643 dump_type (pp, ctx, flags & ~TFF_CLASS_KEY_OR_ENUM);
644 pp_cxx_colon_colon (pp);
645 dump_decl (pp, TYPENAME_TYPE_FULLNAME (t), flags);
648 /* Return the name of the supplied aggregate, or enumeral type. */
650 const char *
651 class_key_or_enum_as_string (tree t)
653 if (TREE_CODE (t) == ENUMERAL_TYPE)
655 if (SCOPED_ENUM_P (t))
656 return "enum class";
657 else
658 return "enum";
660 else if (TREE_CODE (t) == UNION_TYPE)
661 return "union";
662 else if (TYPE_LANG_SPECIFIC (t) && CLASSTYPE_DECLARED_CLASS (t))
663 return "class";
664 else
665 return "struct";
668 /* Print out a class declaration T under the control of FLAGS,
669 in the form `class foo'. */
671 static void
672 dump_aggr_type (cxx_pretty_printer *pp, tree t, int flags)
674 tree name;
675 const char *variety = class_key_or_enum_as_string (t);
676 int typdef = 0;
677 int tmplate = 0;
679 pp_cxx_cv_qualifier_seq (pp, t);
681 if (flags & TFF_CLASS_KEY_OR_ENUM)
682 pp_cxx_ws_string (pp, variety);
684 name = TYPE_NAME (t);
686 if (name)
688 typdef = (!DECL_ARTIFICIAL (name)
689 /* An alias specialization is not considered to be a
690 typedef. */
691 && !alias_template_specialization_p (t));
693 if ((typdef
694 && ((flags & TFF_CHASE_TYPEDEF)
695 || (!flag_pretty_templates && DECL_LANG_SPECIFIC (name)
696 && DECL_TEMPLATE_INFO (name))))
697 || DECL_SELF_REFERENCE_P (name))
699 t = TYPE_MAIN_VARIANT (t);
700 name = TYPE_NAME (t);
701 typdef = 0;
704 tmplate = !typdef && TREE_CODE (t) != ENUMERAL_TYPE
705 && TYPE_LANG_SPECIFIC (t) && CLASSTYPE_TEMPLATE_INFO (t)
706 && (TREE_CODE (CLASSTYPE_TI_TEMPLATE (t)) != TEMPLATE_DECL
707 || PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
709 if (! (flags & TFF_UNQUALIFIED_NAME))
710 dump_scope (pp, CP_DECL_CONTEXT (name), flags | TFF_SCOPE);
711 flags &= ~TFF_UNQUALIFIED_NAME;
712 if (tmplate)
714 /* Because the template names are mangled, we have to locate
715 the most general template, and use that name. */
716 tree tpl = TYPE_TI_TEMPLATE (t);
718 while (DECL_TEMPLATE_INFO (tpl))
719 tpl = DECL_TI_TEMPLATE (tpl);
720 name = tpl;
722 name = DECL_NAME (name);
725 if (name == 0 || anon_aggrname_p (name))
727 if (flags & TFF_CLASS_KEY_OR_ENUM)
728 pp_string (pp, M_("<unnamed>"));
729 else
730 pp_printf (pp, M_("<unnamed %s>"), variety);
732 else if (LAMBDA_TYPE_P (t))
734 /* A lambda's "type" is essentially its signature. */
735 pp_string (pp, M_("<lambda"));
736 if (lambda_function (t))
737 dump_parameters (pp,
738 FUNCTION_FIRST_USER_PARMTYPE (lambda_function (t)),
739 flags);
740 pp_greater (pp);
742 else
743 pp_cxx_tree_identifier (pp, name);
744 if (tmplate)
745 dump_template_parms (pp, TYPE_TEMPLATE_INFO (t),
746 !CLASSTYPE_USE_TEMPLATE (t),
747 flags & ~TFF_TEMPLATE_HEADER);
750 /* Dump into the obstack the initial part of the output for a given type.
751 This is necessary when dealing with things like functions returning
752 functions. Examples:
754 return type of `int (* fee ())()': pointer -> function -> int. Both
755 pointer (and reference and offset) and function (and member) types must
756 deal with prefix and suffix.
758 Arrays must also do this for DECL nodes, like int a[], and for things like
759 int *[]&. */
761 static void
762 dump_type_prefix (cxx_pretty_printer *pp, tree t, int flags)
764 if (TYPE_PTRMEMFUNC_P (t))
766 t = TYPE_PTRMEMFUNC_FN_TYPE (t);
767 goto offset_type;
770 switch (TREE_CODE (t))
772 case POINTER_TYPE:
773 case REFERENCE_TYPE:
775 tree sub = TREE_TYPE (t);
777 dump_type_prefix (pp, sub, flags);
778 if (TREE_CODE (sub) == ARRAY_TYPE
779 || TREE_CODE (sub) == FUNCTION_TYPE)
781 pp_cxx_whitespace (pp);
782 pp_cxx_left_paren (pp);
783 pp_c_attributes_display (pp, TYPE_ATTRIBUTES (sub));
785 if (TYPE_PTR_P (t))
786 pp_star (pp);
787 else if (TREE_CODE (t) == REFERENCE_TYPE)
789 if (TYPE_REF_IS_RVALUE (t))
790 pp_ampersand_ampersand (pp);
791 else
792 pp_ampersand (pp);
794 pp->padding = pp_before;
795 pp_cxx_cv_qualifier_seq (pp, t);
797 break;
799 case OFFSET_TYPE:
800 offset_type:
801 dump_type_prefix (pp, TREE_TYPE (t), flags);
802 if (TREE_CODE (t) == OFFSET_TYPE) /* pmfs deal with this in d_t_p */
804 pp_maybe_space (pp);
805 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
806 pp_cxx_left_paren (pp);
807 dump_type (pp, TYPE_OFFSET_BASETYPE (t), flags);
808 pp_cxx_colon_colon (pp);
810 pp_cxx_star (pp);
811 pp_cxx_cv_qualifier_seq (pp, t);
812 pp->padding = pp_before;
813 break;
815 /* This can be reached without a pointer when dealing with
816 templates, e.g. std::is_function. */
817 case FUNCTION_TYPE:
818 dump_type_prefix (pp, TREE_TYPE (t), flags);
819 break;
821 case METHOD_TYPE:
822 dump_type_prefix (pp, TREE_TYPE (t), flags);
823 pp_maybe_space (pp);
824 pp_cxx_left_paren (pp);
825 dump_aggr_type (pp, TYPE_METHOD_BASETYPE (t), flags);
826 pp_cxx_colon_colon (pp);
827 break;
829 case ARRAY_TYPE:
830 dump_type_prefix (pp, TREE_TYPE (t), flags);
831 break;
833 case ENUMERAL_TYPE:
834 case IDENTIFIER_NODE:
835 case INTEGER_TYPE:
836 case BOOLEAN_TYPE:
837 case REAL_TYPE:
838 case RECORD_TYPE:
839 case TEMPLATE_TYPE_PARM:
840 case TEMPLATE_TEMPLATE_PARM:
841 case BOUND_TEMPLATE_TEMPLATE_PARM:
842 case TREE_LIST:
843 case TYPE_DECL:
844 case TREE_VEC:
845 case UNION_TYPE:
846 case LANG_TYPE:
847 case VOID_TYPE:
848 case TYPENAME_TYPE:
849 case COMPLEX_TYPE:
850 case VECTOR_TYPE:
851 case TYPEOF_TYPE:
852 case UNDERLYING_TYPE:
853 case DECLTYPE_TYPE:
854 case TYPE_PACK_EXPANSION:
855 case FIXED_POINT_TYPE:
856 case NULLPTR_TYPE:
857 dump_type (pp, t, flags);
858 pp->padding = pp_before;
859 break;
861 default:
862 pp_unsupported_tree (pp, t);
863 /* fall through. */
864 case ERROR_MARK:
865 pp_string (pp, M_("<typeprefixerror>"));
866 break;
870 /* Dump the suffix of type T, under control of FLAGS. This is the part
871 which appears after the identifier (or function parms). */
873 static void
874 dump_type_suffix (cxx_pretty_printer *pp, tree t, int flags)
876 if (TYPE_PTRMEMFUNC_P (t))
877 t = TYPE_PTRMEMFUNC_FN_TYPE (t);
879 switch (TREE_CODE (t))
881 case POINTER_TYPE:
882 case REFERENCE_TYPE:
883 case OFFSET_TYPE:
884 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
885 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
886 pp_cxx_right_paren (pp);
887 if (TREE_CODE (t) == POINTER_TYPE)
888 flags |= TFF_POINTER;
889 dump_type_suffix (pp, TREE_TYPE (t), flags);
890 break;
892 case FUNCTION_TYPE:
893 case METHOD_TYPE:
895 tree arg;
896 if (TREE_CODE (t) == METHOD_TYPE)
897 /* Can only be reached through a pointer. */
898 pp_cxx_right_paren (pp);
899 arg = TYPE_ARG_TYPES (t);
900 if (TREE_CODE (t) == METHOD_TYPE)
901 arg = TREE_CHAIN (arg);
903 /* Function pointers don't have default args. Not in standard C++,
904 anyway; they may in g++, but we'll just pretend otherwise. */
905 dump_parameters (pp, arg, flags & ~TFF_FUNCTION_DEFAULT_ARGUMENTS);
907 pp->padding = pp_before;
908 pp_cxx_cv_qualifiers (pp, type_memfn_quals (t),
909 TREE_CODE (t) == FUNCTION_TYPE
910 && (flags & TFF_POINTER));
911 dump_ref_qualifier (pp, t, flags);
912 if (tx_safe_fn_type_p (t))
913 pp_cxx_ws_string (pp, "transaction_safe");
914 dump_exception_spec (pp, TYPE_RAISES_EXCEPTIONS (t), flags);
915 dump_type_suffix (pp, TREE_TYPE (t), flags);
916 break;
919 case ARRAY_TYPE:
920 pp_maybe_space (pp);
921 pp_cxx_left_bracket (pp);
922 if (tree dtype = TYPE_DOMAIN (t))
924 tree max = TYPE_MAX_VALUE (dtype);
925 /* Zero-length arrays have an upper bound of SIZE_MAX. */
926 if (integer_all_onesp (max))
927 pp_character (pp, '0');
928 else if (tree_fits_shwi_p (max))
929 pp_wide_integer (pp, tree_to_shwi (max) + 1);
930 else
932 STRIP_NOPS (max);
933 if (TREE_CODE (max) == SAVE_EXPR)
934 max = TREE_OPERAND (max, 0);
935 if (TREE_CODE (max) == MINUS_EXPR
936 || TREE_CODE (max) == PLUS_EXPR)
938 max = TREE_OPERAND (max, 0);
939 while (CONVERT_EXPR_P (max))
940 max = TREE_OPERAND (max, 0);
942 else
943 max = fold_build2_loc (input_location,
944 PLUS_EXPR, dtype, max,
945 build_int_cst (dtype, 1));
946 dump_expr (pp, max, flags & ~TFF_EXPR_IN_PARENS);
949 pp_cxx_right_bracket (pp);
950 dump_type_suffix (pp, TREE_TYPE (t), flags);
951 break;
953 case ENUMERAL_TYPE:
954 case IDENTIFIER_NODE:
955 case INTEGER_TYPE:
956 case BOOLEAN_TYPE:
957 case REAL_TYPE:
958 case RECORD_TYPE:
959 case TEMPLATE_TYPE_PARM:
960 case TEMPLATE_TEMPLATE_PARM:
961 case BOUND_TEMPLATE_TEMPLATE_PARM:
962 case TREE_LIST:
963 case TYPE_DECL:
964 case TREE_VEC:
965 case UNION_TYPE:
966 case LANG_TYPE:
967 case VOID_TYPE:
968 case TYPENAME_TYPE:
969 case COMPLEX_TYPE:
970 case VECTOR_TYPE:
971 case TYPEOF_TYPE:
972 case UNDERLYING_TYPE:
973 case DECLTYPE_TYPE:
974 case TYPE_PACK_EXPANSION:
975 case FIXED_POINT_TYPE:
976 case NULLPTR_TYPE:
977 break;
979 default:
980 pp_unsupported_tree (pp, t);
981 case ERROR_MARK:
982 /* Don't mark it here, we should have already done in
983 dump_type_prefix. */
984 break;
988 static void
989 dump_global_iord (cxx_pretty_printer *pp, tree t)
991 const char *p = NULL;
993 if (DECL_GLOBAL_CTOR_P (t))
994 p = M_("(static initializers for %s)");
995 else if (DECL_GLOBAL_DTOR_P (t))
996 p = M_("(static destructors for %s)");
997 else
998 gcc_unreachable ();
1000 pp_printf (pp, p, DECL_SOURCE_FILE (t));
1003 static void
1004 dump_simple_decl (cxx_pretty_printer *pp, tree t, tree type, int flags)
1006 if (flags & TFF_DECL_SPECIFIERS)
1008 if (VAR_P (t) && DECL_DECLARED_CONSTEXPR_P (t))
1010 if (DECL_LANG_SPECIFIC (t) && DECL_DECLARED_CONCEPT_P (t))
1011 pp_cxx_ws_string (pp, "concept");
1012 else
1013 pp_cxx_ws_string (pp, "constexpr");
1015 dump_type_prefix (pp, type, flags & ~TFF_UNQUALIFIED_NAME);
1016 pp_maybe_space (pp);
1018 if (! (flags & TFF_UNQUALIFIED_NAME)
1019 && TREE_CODE (t) != PARM_DECL
1020 && (!DECL_INITIAL (t)
1021 || TREE_CODE (DECL_INITIAL (t)) != TEMPLATE_PARM_INDEX))
1022 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
1023 flags &= ~TFF_UNQUALIFIED_NAME;
1024 if ((flags & TFF_DECL_SPECIFIERS)
1025 && DECL_TEMPLATE_PARM_P (t)
1026 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (t)))
1027 pp_string (pp, "...");
1028 if (DECL_NAME (t))
1030 if (TREE_CODE (t) == FIELD_DECL && DECL_NORMAL_CAPTURE_P (t))
1032 pp_less (pp);
1033 pp_string (pp, IDENTIFIER_POINTER (DECL_NAME (t)) + 2);
1034 pp_string (pp, " capture>");
1036 else
1037 dump_decl (pp, DECL_NAME (t), flags);
1039 else if (DECL_DECOMPOSITION_P (t))
1040 pp_string (pp, M_("<structured bindings>"));
1041 else
1042 pp_string (pp, M_("<anonymous>"));
1043 if (flags & TFF_DECL_SPECIFIERS)
1044 dump_type_suffix (pp, type, flags);
1047 /* Print an IDENTIFIER_NODE that is the name of a declaration. */
1049 static void
1050 dump_decl_name (cxx_pretty_printer *pp, tree t, int flags)
1052 /* These special cases are duplicated here so that other functions
1053 can feed identifiers to error and get them demangled properly. */
1054 if (IDENTIFIER_CONV_OP_P (t))
1056 pp_cxx_ws_string (pp, "operator");
1057 /* Not exactly IDENTIFIER_TYPE_VALUE. */
1058 dump_type (pp, TREE_TYPE (t), flags);
1059 return;
1061 if (dguide_name_p (t))
1063 dump_decl (pp, CLASSTYPE_TI_TEMPLATE (TREE_TYPE (t)),
1064 TFF_UNQUALIFIED_NAME);
1065 return;
1068 const char *str = IDENTIFIER_POINTER (t);
1069 if (!strncmp (str, "_ZGR", 3))
1071 pp_cxx_ws_string (pp, "<temporary>");
1072 return;
1075 pp_cxx_tree_identifier (pp, t);
1078 /* Dump a human readable string for the decl T under control of FLAGS. */
1080 static void
1081 dump_decl (cxx_pretty_printer *pp, tree t, int flags)
1083 if (t == NULL_TREE)
1084 return;
1086 /* If doing Objective-C++, give Objective-C a chance to demangle
1087 Objective-C method names. */
1088 if (c_dialect_objc ())
1090 const char *demangled = objc_maybe_printable_name (t, flags);
1091 if (demangled)
1093 pp_string (pp, demangled);
1094 return;
1098 switch (TREE_CODE (t))
1100 case TYPE_DECL:
1101 /* Don't say 'typedef class A' */
1102 if (DECL_ARTIFICIAL (t) && !DECL_SELF_REFERENCE_P (t))
1104 if ((flags & TFF_DECL_SPECIFIERS)
1105 && TREE_CODE (TREE_TYPE (t)) == TEMPLATE_TYPE_PARM)
1107 /* Say `class T' not just `T'. */
1108 pp_cxx_ws_string (pp, "class");
1110 /* Emit the `...' for a parameter pack. */
1111 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
1112 pp_cxx_ws_string (pp, "...");
1115 dump_type (pp, TREE_TYPE (t), flags);
1116 break;
1118 if (TYPE_DECL_ALIAS_P (t)
1119 && (flags & TFF_DECL_SPECIFIERS
1120 || flags & TFF_CLASS_KEY_OR_ENUM))
1122 pp_cxx_ws_string (pp, "using");
1123 dump_decl (pp, DECL_NAME (t), flags);
1124 pp_cxx_whitespace (pp);
1125 pp_cxx_ws_string (pp, "=");
1126 pp_cxx_whitespace (pp);
1127 dump_type (pp, (DECL_ORIGINAL_TYPE (t)
1128 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t)),
1129 flags);
1130 break;
1132 if ((flags & TFF_DECL_SPECIFIERS)
1133 && !DECL_SELF_REFERENCE_P (t))
1134 pp_cxx_ws_string (pp, "typedef");
1135 dump_simple_decl (pp, t, DECL_ORIGINAL_TYPE (t)
1136 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t),
1137 flags);
1138 break;
1140 case VAR_DECL:
1141 if (DECL_NAME (t) && VTABLE_NAME_P (DECL_NAME (t)))
1143 pp_string (pp, M_("vtable for "));
1144 gcc_assert (TYPE_P (DECL_CONTEXT (t)));
1145 dump_type (pp, DECL_CONTEXT (t), flags);
1146 break;
1148 /* Fall through. */
1149 case FIELD_DECL:
1150 case PARM_DECL:
1151 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1153 /* Handle variable template specializations. */
1154 if (VAR_P (t)
1155 && DECL_LANG_SPECIFIC (t)
1156 && DECL_TEMPLATE_INFO (t)
1157 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1159 pp_cxx_begin_template_argument_list (pp);
1160 tree args = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1161 dump_template_argument_list (pp, args, flags);
1162 pp_cxx_end_template_argument_list (pp);
1164 break;
1166 case RESULT_DECL:
1167 pp_string (pp, M_("<return value> "));
1168 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1169 break;
1171 case NAMESPACE_DECL:
1172 if (flags & TFF_DECL_SPECIFIERS)
1173 pp->declaration (t);
1174 else
1176 if (! (flags & TFF_UNQUALIFIED_NAME))
1177 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
1178 flags &= ~TFF_UNQUALIFIED_NAME;
1179 if (DECL_NAME (t) == NULL_TREE)
1181 if (!(pp->flags & pp_c_flag_gnu_v3))
1182 pp_cxx_ws_string (pp, M_("{anonymous}"));
1183 else
1184 pp_cxx_ws_string (pp, M_("(anonymous namespace)"));
1186 else
1187 pp_cxx_tree_identifier (pp, DECL_NAME (t));
1189 break;
1191 case SCOPE_REF:
1192 dump_type (pp, TREE_OPERAND (t, 0), flags);
1193 pp_cxx_colon_colon (pp);
1194 dump_decl (pp, TREE_OPERAND (t, 1), TFF_UNQUALIFIED_NAME);
1195 break;
1197 case ARRAY_REF:
1198 dump_decl (pp, TREE_OPERAND (t, 0), flags);
1199 pp_cxx_left_bracket (pp);
1200 dump_decl (pp, TREE_OPERAND (t, 1), flags);
1201 pp_cxx_right_bracket (pp);
1202 break;
1204 case ARRAY_NOTATION_REF:
1205 dump_decl (pp, ARRAY_NOTATION_ARRAY (t), flags | TFF_EXPR_IN_PARENS);
1206 pp_cxx_left_bracket (pp);
1207 dump_decl (pp, ARRAY_NOTATION_START (t), flags | TFF_EXPR_IN_PARENS);
1208 pp_colon (pp);
1209 dump_decl (pp, ARRAY_NOTATION_LENGTH (t), flags | TFF_EXPR_IN_PARENS);
1210 pp_colon (pp);
1211 dump_decl (pp, ARRAY_NOTATION_STRIDE (t), flags | TFF_EXPR_IN_PARENS);
1212 pp_cxx_right_bracket (pp);
1213 break;
1215 /* So that we can do dump_decl on an aggr type. */
1216 case RECORD_TYPE:
1217 case UNION_TYPE:
1218 case ENUMERAL_TYPE:
1219 dump_type (pp, t, flags);
1220 break;
1222 case BIT_NOT_EXPR:
1223 /* This is a pseudo destructor call which has not been folded into
1224 a PSEUDO_DTOR_EXPR yet. */
1225 pp_cxx_complement (pp);
1226 dump_type (pp, TREE_OPERAND (t, 0), flags);
1227 break;
1229 case TYPE_EXPR:
1230 gcc_unreachable ();
1231 break;
1233 case IDENTIFIER_NODE:
1234 dump_decl_name (pp, t, flags);
1235 break;
1237 case OVERLOAD:
1238 if (!OVL_SINGLE_P (t))
1240 tree ctx = ovl_scope (t);
1241 if (ctx != global_namespace)
1243 if (TYPE_P (ctx))
1244 dump_type (pp, ctx, flags);
1245 else
1246 dump_decl (pp, ctx, flags);
1247 pp_cxx_colon_colon (pp);
1249 dump_decl (pp, OVL_NAME (t), flags);
1250 break;
1253 /* If there's only one function, just treat it like an ordinary
1254 FUNCTION_DECL. */
1255 t = OVL_FIRST (t);
1256 /* Fall through. */
1258 case FUNCTION_DECL:
1259 if (! DECL_LANG_SPECIFIC (t))
1261 if (DECL_ABSTRACT_ORIGIN (t)
1262 && DECL_ABSTRACT_ORIGIN (t) != t)
1263 dump_decl (pp, DECL_ABSTRACT_ORIGIN (t), flags);
1264 else
1265 dump_function_name (pp, t, flags);
1267 else if (DECL_GLOBAL_CTOR_P (t) || DECL_GLOBAL_DTOR_P (t))
1268 dump_global_iord (pp, t);
1269 else
1270 dump_function_decl (pp, t, flags);
1271 break;
1273 case TEMPLATE_DECL:
1274 dump_template_decl (pp, t, flags);
1275 break;
1277 case TEMPLATE_ID_EXPR:
1279 tree name = TREE_OPERAND (t, 0);
1280 tree args = TREE_OPERAND (t, 1);
1282 if (!identifier_p (name))
1283 name = OVL_NAME (name);
1284 dump_decl (pp, name, flags);
1285 pp_cxx_begin_template_argument_list (pp);
1286 if (args == error_mark_node)
1287 pp_string (pp, M_("<template arguments error>"));
1288 else if (args)
1289 dump_template_argument_list
1290 (pp, args, flags|TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS);
1291 pp_cxx_end_template_argument_list (pp);
1293 break;
1295 case LABEL_DECL:
1296 pp_cxx_tree_identifier (pp, DECL_NAME (t));
1297 break;
1299 case CONST_DECL:
1300 if ((TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == ENUMERAL_TYPE)
1301 || (DECL_INITIAL (t) &&
1302 TREE_CODE (DECL_INITIAL (t)) == TEMPLATE_PARM_INDEX))
1303 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1304 else if (DECL_NAME (t))
1305 dump_decl (pp, DECL_NAME (t), flags);
1306 else if (DECL_INITIAL (t))
1307 dump_expr (pp, DECL_INITIAL (t), flags | TFF_EXPR_IN_PARENS);
1308 else
1309 pp_string (pp, M_("<enumerator>"));
1310 break;
1312 case USING_DECL:
1314 pp_cxx_ws_string (pp, "using");
1315 tree scope = USING_DECL_SCOPE (t);
1316 bool variadic = false;
1317 if (PACK_EXPANSION_P (scope))
1319 scope = PACK_EXPANSION_PATTERN (scope);
1320 variadic = true;
1322 dump_type (pp, scope, flags);
1323 pp_cxx_colon_colon (pp);
1324 dump_decl (pp, DECL_NAME (t), flags);
1325 if (variadic)
1326 pp_cxx_ws_string (pp, "...");
1328 break;
1330 case STATIC_ASSERT:
1331 pp->declaration (t);
1332 break;
1334 case BASELINK:
1335 dump_decl (pp, BASELINK_FUNCTIONS (t), flags);
1336 break;
1338 case NON_DEPENDENT_EXPR:
1339 dump_expr (pp, t, flags);
1340 break;
1342 case TEMPLATE_TYPE_PARM:
1343 if (flags & TFF_DECL_SPECIFIERS)
1344 pp->declaration (t);
1345 else
1346 pp->type_id (t);
1347 break;
1349 case UNBOUND_CLASS_TEMPLATE:
1350 case TYPE_PACK_EXPANSION:
1351 case TREE_BINFO:
1352 dump_type (pp, t, flags);
1353 break;
1355 default:
1356 pp_unsupported_tree (pp, t);
1357 /* Fall through. */
1359 case ERROR_MARK:
1360 pp_string (pp, M_("<declaration error>"));
1361 break;
1365 /* Dump a template declaration T under control of FLAGS. This means the
1366 'template <...> leaders plus the 'class X' or 'void fn(...)' part. */
1368 static void
1369 dump_template_decl (cxx_pretty_printer *pp, tree t, int flags)
1371 tree orig_parms = DECL_TEMPLATE_PARMS (t);
1372 tree parms;
1373 int i;
1375 if (flags & TFF_TEMPLATE_HEADER)
1377 for (parms = orig_parms = nreverse (orig_parms);
1378 parms;
1379 parms = TREE_CHAIN (parms))
1381 tree inner_parms = INNERMOST_TEMPLATE_PARMS (parms);
1382 int len = TREE_VEC_LENGTH (inner_parms);
1384 if (len == 0)
1386 /* Skip over the dummy template levels of a template template
1387 parm. */
1388 gcc_assert (TREE_CODE (TREE_TYPE (t)) == TEMPLATE_TEMPLATE_PARM);
1389 continue;
1392 pp_cxx_ws_string (pp, "template");
1393 pp_cxx_begin_template_argument_list (pp);
1395 /* If we've shown the template prefix, we'd better show the
1396 parameters' and decl's type too. */
1397 flags |= TFF_DECL_SPECIFIERS;
1399 for (i = 0; i < len; i++)
1401 if (i)
1402 pp_separate_with_comma (pp);
1403 dump_template_parameter (pp, TREE_VEC_ELT (inner_parms, i),
1404 flags);
1406 pp_cxx_end_template_argument_list (pp);
1407 pp_cxx_whitespace (pp);
1409 nreverse(orig_parms);
1411 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
1413 /* Say `template<arg> class TT' not just `template<arg> TT'. */
1414 pp_cxx_ws_string (pp, "class");
1416 /* If this is a parameter pack, print the ellipsis. */
1417 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
1418 pp_cxx_ws_string (pp, "...");
1421 /* Only print the requirements if we're also printing
1422 the template header. */
1423 if (flag_concepts)
1424 if (tree ci = get_constraints (t))
1425 if (check_constraint_info (ci))
1426 if (tree reqs = CI_TEMPLATE_REQS (ci))
1428 pp_cxx_requires_clause (pp, reqs);
1429 pp_cxx_whitespace (pp);
1434 if (DECL_CLASS_TEMPLATE_P (t))
1435 dump_type (pp, TREE_TYPE (t),
1436 ((flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1437 | (flags & TFF_DECL_SPECIFIERS ? TFF_CLASS_KEY_OR_ENUM : 0)));
1438 else if (DECL_TEMPLATE_RESULT (t)
1439 && (VAR_P (DECL_TEMPLATE_RESULT (t))
1440 /* Alias template. */
1441 || DECL_TYPE_TEMPLATE_P (t)))
1442 dump_decl (pp, DECL_TEMPLATE_RESULT (t), flags | TFF_TEMPLATE_NAME);
1443 else
1445 gcc_assert (TREE_TYPE (t));
1446 switch (NEXT_CODE (t))
1448 case METHOD_TYPE:
1449 case FUNCTION_TYPE:
1450 dump_function_decl (pp, t, flags | TFF_TEMPLATE_NAME);
1451 break;
1452 default:
1453 /* This case can occur with some invalid code. */
1454 dump_type (pp, TREE_TYPE (t),
1455 (flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1456 | (flags & TFF_DECL_SPECIFIERS
1457 ? TFF_CLASS_KEY_OR_ENUM : 0));
1462 /* find_typenames looks through the type of the function template T
1463 and returns a vec containing any typedefs, decltypes or TYPENAME_TYPEs
1464 it finds. */
1466 struct find_typenames_t
1468 hash_set<tree> *p_set;
1469 vec<tree, va_gc> *typenames;
1472 static tree
1473 find_typenames_r (tree *tp, int *walk_subtrees, void *data)
1475 struct find_typenames_t *d = (struct find_typenames_t *)data;
1476 tree mv = NULL_TREE;
1478 if (TYPE_P (*tp) && is_typedef_decl (TYPE_NAME (*tp)))
1479 /* Add the type of the typedef without any additional cv-quals. */
1480 mv = TREE_TYPE (TYPE_NAME (*tp));
1481 else if (TREE_CODE (*tp) == TYPENAME_TYPE
1482 || TREE_CODE (*tp) == DECLTYPE_TYPE)
1483 /* Add the typename without any cv-qualifiers. */
1484 mv = TYPE_MAIN_VARIANT (*tp);
1486 if (TREE_CODE (*tp) == TYPE_PACK_EXPANSION)
1488 /* Don't mess with parameter packs since we don't remember
1489 the pack expansion context for a particular typename. */
1490 *walk_subtrees = false;
1491 return NULL_TREE;
1494 if (mv && (mv == *tp || !d->p_set->add (mv)))
1495 vec_safe_push (d->typenames, mv);
1497 /* Search into class template arguments, which cp_walk_subtrees
1498 doesn't do. */
1499 if (CLASS_TYPE_P (*tp) && CLASSTYPE_TEMPLATE_INFO (*tp))
1500 cp_walk_tree (&CLASSTYPE_TI_ARGS (*tp), find_typenames_r,
1501 data, d->p_set);
1503 return NULL_TREE;
1506 static vec<tree, va_gc> *
1507 find_typenames (tree t)
1509 struct find_typenames_t ft;
1510 ft.p_set = new hash_set<tree>;
1511 ft.typenames = NULL;
1512 cp_walk_tree (&TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
1513 find_typenames_r, &ft, ft.p_set);
1514 delete ft.p_set;
1515 return ft.typenames;
1518 /* Output the "[with ...]" clause for a template instantiation T iff
1519 TEMPLATE_PARMS, TEMPLATE_ARGS and FLAGS are suitable. T may be NULL if
1520 formatting a deduction/substitution diagnostic rather than an
1521 instantiation. */
1523 static void
1524 dump_substitution (cxx_pretty_printer *pp,
1525 tree t, tree template_parms, tree template_args,
1526 int flags)
1528 if (template_parms != NULL_TREE && template_args != NULL_TREE
1529 && !(flags & TFF_NO_TEMPLATE_BINDINGS))
1531 vec<tree, va_gc> *typenames = t ? find_typenames (t) : NULL;
1532 pp_cxx_whitespace (pp);
1533 pp_cxx_left_bracket (pp);
1534 pp->translate_string ("with");
1535 pp_cxx_whitespace (pp);
1536 dump_template_bindings (pp, template_parms, template_args, typenames);
1537 pp_cxx_right_bracket (pp);
1541 /* Dump the lambda function FN including its 'mutable' qualifier and any
1542 template bindings. */
1544 static void
1545 dump_lambda_function (cxx_pretty_printer *pp,
1546 tree fn, tree template_parms, tree template_args,
1547 int flags)
1549 /* A lambda's signature is essentially its "type". */
1550 dump_type (pp, DECL_CONTEXT (fn), flags);
1551 if (!(TYPE_QUALS (class_of_this_parm (TREE_TYPE (fn))) & TYPE_QUAL_CONST))
1553 pp->padding = pp_before;
1554 pp_c_ws_string (pp, "mutable");
1556 dump_substitution (pp, fn, template_parms, template_args, flags);
1559 /* Pretty print a function decl. There are several ways we want to print a
1560 function declaration. The TFF_ bits in FLAGS tells us how to behave.
1561 As error can only apply the '#' flag once to give 0 and 1 for V, there
1562 is %D which doesn't print the throw specs, and %F which does. */
1564 static void
1565 dump_function_decl (cxx_pretty_printer *pp, tree t, int flags)
1567 tree fntype;
1568 tree parmtypes;
1569 tree cname = NULL_TREE;
1570 tree template_args = NULL_TREE;
1571 tree template_parms = NULL_TREE;
1572 int show_return = flags & TFF_RETURN_TYPE || flags & TFF_DECL_SPECIFIERS;
1573 int do_outer_scope = ! (flags & TFF_UNQUALIFIED_NAME);
1574 tree exceptions;
1575 bool constexpr_p;
1576 tree ret = NULL_TREE;
1578 flags &= ~(TFF_UNQUALIFIED_NAME | TFF_TEMPLATE_NAME);
1579 if (TREE_CODE (t) == TEMPLATE_DECL)
1580 t = DECL_TEMPLATE_RESULT (t);
1582 /* Save the exceptions, in case t is a specialization and we are
1583 emitting an error about incompatible specifications. */
1584 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (t));
1586 /* Likewise for the constexpr specifier, in case t is a specialization. */
1587 constexpr_p = DECL_DECLARED_CONSTEXPR_P (t);
1589 /* Pretty print template instantiations only. */
1590 if (DECL_USE_TEMPLATE (t) && DECL_TEMPLATE_INFO (t)
1591 && !(flags & TFF_NO_TEMPLATE_BINDINGS)
1592 && flag_pretty_templates)
1594 tree tmpl;
1596 template_args = DECL_TI_ARGS (t);
1597 tmpl = most_general_template (t);
1598 if (tmpl && TREE_CODE (tmpl) == TEMPLATE_DECL)
1600 template_parms = DECL_TEMPLATE_PARMS (tmpl);
1601 t = tmpl;
1605 if (DECL_NAME (t) && LAMBDA_FUNCTION_P (t))
1606 return dump_lambda_function (pp, t, template_parms, template_args, flags);
1608 fntype = TREE_TYPE (t);
1609 parmtypes = FUNCTION_FIRST_USER_PARMTYPE (t);
1611 if (DECL_CLASS_SCOPE_P (t))
1612 cname = DECL_CONTEXT (t);
1613 /* This is for partially instantiated template methods. */
1614 else if (TREE_CODE (fntype) == METHOD_TYPE)
1615 cname = TREE_TYPE (TREE_VALUE (parmtypes));
1617 if (flags & TFF_DECL_SPECIFIERS)
1619 if (DECL_STATIC_FUNCTION_P (t))
1620 pp_cxx_ws_string (pp, "static");
1621 else if (DECL_VIRTUAL_P (t))
1622 pp_cxx_ws_string (pp, "virtual");
1624 if (constexpr_p)
1626 if (DECL_DECLARED_CONCEPT_P (t))
1627 pp_cxx_ws_string (pp, "concept");
1628 else
1629 pp_cxx_ws_string (pp, "constexpr");
1633 /* Print the return type? */
1634 if (show_return)
1635 show_return = (!DECL_CONV_FN_P (t) && !DECL_CONSTRUCTOR_P (t)
1636 && !DECL_DESTRUCTOR_P (t) && !deduction_guide_p (t));
1637 if (show_return)
1639 ret = fndecl_declared_return_type (t);
1640 dump_type_prefix (pp, ret, flags);
1643 /* Print the function name. */
1644 if (!do_outer_scope)
1645 /* Nothing. */;
1646 else if (cname)
1648 dump_type (pp, cname, flags);
1649 pp_cxx_colon_colon (pp);
1651 else
1652 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
1654 dump_function_name (pp, t, flags);
1656 if (!(flags & TFF_NO_FUNCTION_ARGUMENTS))
1658 dump_parameters (pp, parmtypes, flags);
1660 if (TREE_CODE (fntype) == METHOD_TYPE)
1662 pp->padding = pp_before;
1663 pp_cxx_cv_qualifier_seq (pp, class_of_this_parm (fntype));
1664 dump_ref_qualifier (pp, fntype, flags);
1667 if (tx_safe_fn_type_p (fntype))
1669 pp->padding = pp_before;
1670 pp_cxx_ws_string (pp, "transaction_safe");
1673 if (flags & TFF_EXCEPTION_SPECIFICATION)
1675 pp->padding = pp_before;
1676 dump_exception_spec (pp, exceptions, flags);
1679 if (show_return)
1680 dump_type_suffix (pp, ret, flags);
1681 else if (deduction_guide_p (t))
1683 pp_cxx_ws_string (pp, "->");
1684 dump_type (pp, TREE_TYPE (TREE_TYPE (t)), flags);
1687 if (flag_concepts)
1688 if (tree ci = get_constraints (t))
1689 if (tree reqs = CI_DECLARATOR_REQS (ci))
1690 pp_cxx_requires_clause (pp, reqs);
1692 dump_substitution (pp, t, template_parms, template_args, flags);
1694 if (tree base = DECL_INHERITED_CTOR_BASE (t))
1696 pp_cxx_ws_string (pp, "[inherited from");
1697 dump_type (pp, base, TFF_PLAIN_IDENTIFIER);
1698 pp_character (pp, ']');
1701 else if (template_args)
1703 bool need_comma = false;
1704 int i;
1705 pp_cxx_begin_template_argument_list (pp);
1706 template_args = INNERMOST_TEMPLATE_ARGS (template_args);
1707 for (i = 0; i < TREE_VEC_LENGTH (template_args); ++i)
1709 tree arg = TREE_VEC_ELT (template_args, i);
1710 if (need_comma)
1711 pp_separate_with_comma (pp);
1712 if (ARGUMENT_PACK_P (arg))
1713 pp_cxx_left_brace (pp);
1714 dump_template_argument (pp, arg, TFF_PLAIN_IDENTIFIER);
1715 if (ARGUMENT_PACK_P (arg))
1716 pp_cxx_right_brace (pp);
1717 need_comma = true;
1719 pp_cxx_end_template_argument_list (pp);
1723 /* Print a parameter list. If this is for a member function, the
1724 member object ptr (and any other hidden args) should have
1725 already been removed. */
1727 static void
1728 dump_parameters (cxx_pretty_printer *pp, tree parmtypes, int flags)
1730 int first = 1;
1731 flags &= ~TFF_SCOPE;
1732 pp_cxx_left_paren (pp);
1734 for (first = 1; parmtypes != void_list_node;
1735 parmtypes = TREE_CHAIN (parmtypes))
1737 if (!first)
1738 pp_separate_with_comma (pp);
1739 first = 0;
1740 if (!parmtypes)
1742 pp_cxx_ws_string (pp, "...");
1743 break;
1746 dump_type (pp, TREE_VALUE (parmtypes), flags);
1748 if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && TREE_PURPOSE (parmtypes))
1750 pp_cxx_whitespace (pp);
1751 pp_equal (pp);
1752 pp_cxx_whitespace (pp);
1753 dump_expr (pp, TREE_PURPOSE (parmtypes), flags | TFF_EXPR_IN_PARENS);
1757 pp_cxx_right_paren (pp);
1760 /* Print ref-qualifier of a FUNCTION_TYPE or METHOD_TYPE. FLAGS are ignored. */
1762 static void
1763 dump_ref_qualifier (cxx_pretty_printer *pp, tree t, int flags ATTRIBUTE_UNUSED)
1765 if (FUNCTION_REF_QUALIFIED (t))
1767 pp->padding = pp_before;
1768 if (FUNCTION_RVALUE_QUALIFIED (t))
1769 pp_cxx_ws_string (pp, "&&");
1770 else
1771 pp_cxx_ws_string (pp, "&");
1775 /* Print an exception specification. T is the exception specification. */
1777 static void
1778 dump_exception_spec (cxx_pretty_printer *pp, tree t, int flags)
1780 if (t && TREE_PURPOSE (t))
1782 pp_cxx_ws_string (pp, "noexcept");
1783 if (!integer_onep (TREE_PURPOSE (t)))
1785 pp_cxx_whitespace (pp);
1786 pp_cxx_left_paren (pp);
1787 if (DEFERRED_NOEXCEPT_SPEC_P (t))
1788 pp_cxx_ws_string (pp, "<uninstantiated>");
1789 else
1790 dump_expr (pp, TREE_PURPOSE (t), flags);
1791 pp_cxx_right_paren (pp);
1794 else if (t)
1796 pp_cxx_ws_string (pp, "throw");
1797 pp_cxx_whitespace (pp);
1798 pp_cxx_left_paren (pp);
1799 if (TREE_VALUE (t) != NULL_TREE)
1800 while (1)
1802 dump_type (pp, TREE_VALUE (t), flags);
1803 t = TREE_CHAIN (t);
1804 if (!t)
1805 break;
1806 pp_separate_with_comma (pp);
1808 pp_cxx_right_paren (pp);
1812 /* Handle the function name for a FUNCTION_DECL node, grokking operators
1813 and destructors properly. */
1815 static void
1816 dump_function_name (cxx_pretty_printer *pp, tree t, int flags)
1818 tree name = DECL_NAME (t);
1820 /* We can get here with a decl that was synthesized by language-
1821 independent machinery (e.g. coverage.c) in which case it won't
1822 have a lang_specific structure attached and DECL_CONSTRUCTOR_P
1823 will crash. In this case it is safe just to print out the
1824 literal name. */
1825 if (!DECL_LANG_SPECIFIC (t))
1827 pp_cxx_tree_identifier (pp, name);
1828 return;
1831 if (TREE_CODE (t) == TEMPLATE_DECL)
1832 t = DECL_TEMPLATE_RESULT (t);
1834 /* Don't let the user see __comp_ctor et al. */
1835 if (DECL_CONSTRUCTOR_P (t)
1836 || DECL_DESTRUCTOR_P (t))
1838 if (LAMBDA_TYPE_P (DECL_CONTEXT (t)))
1839 name = get_identifier ("<lambda>");
1840 else if (TYPE_UNNAMED_P (DECL_CONTEXT (t)))
1841 name = get_identifier ("<constructor>");
1842 else
1843 name = constructor_name (DECL_CONTEXT (t));
1846 if (DECL_DESTRUCTOR_P (t))
1848 pp_cxx_complement (pp);
1849 dump_decl (pp, name, TFF_PLAIN_IDENTIFIER);
1851 else if (DECL_CONV_FN_P (t))
1853 /* This cannot use the hack that the operator's return
1854 type is stashed off of its name because it may be
1855 used for error reporting. In the case of conflicting
1856 declarations, both will have the same name, yet
1857 the types will be different, hence the TREE_TYPE field
1858 of the first name will be clobbered by the second. */
1859 pp_cxx_ws_string (pp, "operator");
1860 dump_type (pp, TREE_TYPE (TREE_TYPE (t)), flags);
1862 else
1863 dump_decl (pp, name, flags);
1865 if (DECL_TEMPLATE_INFO (t)
1866 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (t)
1867 && (TREE_CODE (DECL_TI_TEMPLATE (t)) != TEMPLATE_DECL
1868 || PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
1869 dump_template_parms (pp, DECL_TEMPLATE_INFO (t), !DECL_USE_TEMPLATE (t),
1870 flags);
1873 /* Dump the template parameters from the template info INFO under control of
1874 FLAGS. PRIMARY indicates whether this is a primary template decl, or
1875 specialization (partial or complete). For partial specializations we show
1876 the specialized parameter values. For a primary template we show no
1877 decoration. */
1879 static void
1880 dump_template_parms (cxx_pretty_printer *pp, tree info,
1881 int primary, int flags)
1883 tree args = info ? TI_ARGS (info) : NULL_TREE;
1885 if (primary && flags & TFF_TEMPLATE_NAME)
1886 return;
1887 flags &= ~(TFF_CLASS_KEY_OR_ENUM | TFF_TEMPLATE_NAME);
1888 pp_cxx_begin_template_argument_list (pp);
1890 /* Be careful only to print things when we have them, so as not
1891 to crash producing error messages. */
1892 if (args && !primary)
1894 int len, ix;
1895 len = get_non_default_template_args_count (args, flags);
1897 args = INNERMOST_TEMPLATE_ARGS (args);
1898 for (ix = 0; ix != len; ix++)
1900 tree arg = TREE_VEC_ELT (args, ix);
1902 /* Only print a comma if we know there is an argument coming. In
1903 the case of an empty template argument pack, no actual
1904 argument will be printed. */
1905 if (ix
1906 && (!ARGUMENT_PACK_P (arg)
1907 || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) > 0))
1908 pp_separate_with_comma (pp);
1910 if (!arg)
1911 pp_string (pp, M_("<template parameter error>"));
1912 else
1913 dump_template_argument (pp, arg, flags);
1916 else if (primary)
1918 tree tpl = TI_TEMPLATE (info);
1919 tree parms = DECL_TEMPLATE_PARMS (tpl);
1920 int len, ix;
1922 parms = TREE_CODE (parms) == TREE_LIST ? TREE_VALUE (parms) : NULL_TREE;
1923 len = parms ? TREE_VEC_LENGTH (parms) : 0;
1925 for (ix = 0; ix != len; ix++)
1927 tree parm;
1929 if (TREE_VEC_ELT (parms, ix) == error_mark_node)
1931 pp_string (pp, M_("<template parameter error>"));
1932 continue;
1935 parm = TREE_VALUE (TREE_VEC_ELT (parms, ix));
1937 if (ix)
1938 pp_separate_with_comma (pp);
1940 dump_decl (pp, parm, flags & ~TFF_DECL_SPECIFIERS);
1943 pp_cxx_end_template_argument_list (pp);
1946 /* Print out the arguments of CALL_EXPR T as a parenthesized list using
1947 flags FLAGS. Skip over the first argument if SKIPFIRST is true. */
1949 static void
1950 dump_call_expr_args (cxx_pretty_printer *pp, tree t, int flags, bool skipfirst)
1952 tree arg;
1953 call_expr_arg_iterator iter;
1955 pp_cxx_left_paren (pp);
1956 FOR_EACH_CALL_EXPR_ARG (arg, iter, t)
1958 if (skipfirst)
1959 skipfirst = false;
1960 else
1962 dump_expr (pp, arg, flags | TFF_EXPR_IN_PARENS);
1963 if (more_call_expr_args_p (&iter))
1964 pp_separate_with_comma (pp);
1967 pp_cxx_right_paren (pp);
1970 /* Print out the arguments of AGGR_INIT_EXPR T as a parenthesized list
1971 using flags FLAGS. Skip over the first argument if SKIPFIRST is
1972 true. */
1974 static void
1975 dump_aggr_init_expr_args (cxx_pretty_printer *pp, tree t, int flags,
1976 bool skipfirst)
1978 tree arg;
1979 aggr_init_expr_arg_iterator iter;
1981 pp_cxx_left_paren (pp);
1982 FOR_EACH_AGGR_INIT_EXPR_ARG (arg, iter, t)
1984 if (skipfirst)
1985 skipfirst = false;
1986 else
1988 dump_expr (pp, arg, flags | TFF_EXPR_IN_PARENS);
1989 if (more_aggr_init_expr_args_p (&iter))
1990 pp_separate_with_comma (pp);
1993 pp_cxx_right_paren (pp);
1996 /* Print out a list of initializers (subr of dump_expr). */
1998 static void
1999 dump_expr_list (cxx_pretty_printer *pp, tree l, int flags)
2001 while (l)
2003 dump_expr (pp, TREE_VALUE (l), flags | TFF_EXPR_IN_PARENS);
2004 l = TREE_CHAIN (l);
2005 if (l)
2006 pp_separate_with_comma (pp);
2010 /* Print out a vector of initializers (subr of dump_expr). */
2012 static void
2013 dump_expr_init_vec (cxx_pretty_printer *pp, vec<constructor_elt, va_gc> *v,
2014 int flags)
2016 unsigned HOST_WIDE_INT idx;
2017 tree value;
2019 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
2021 dump_expr (pp, value, flags | TFF_EXPR_IN_PARENS);
2022 if (idx != v->length () - 1)
2023 pp_separate_with_comma (pp);
2028 /* We've gotten an indirect REFERENCE (an OBJ_TYPE_REF) to a virtual
2029 function. Resolve it to a close relative -- in the sense of static
2030 type -- variant being overridden. That is close to what was written in
2031 the source code. Subroutine of dump_expr. */
2033 static tree
2034 resolve_virtual_fun_from_obj_type_ref (tree ref)
2036 tree obj_type = TREE_TYPE (OBJ_TYPE_REF_OBJECT (ref));
2037 HOST_WIDE_INT index = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (ref));
2038 tree fun = BINFO_VIRTUALS (TYPE_BINFO (TREE_TYPE (obj_type)));
2039 while (index)
2041 fun = TREE_CHAIN (fun);
2042 index -= (TARGET_VTABLE_USES_DESCRIPTORS
2043 ? TARGET_VTABLE_USES_DESCRIPTORS : 1);
2046 return BV_FN (fun);
2049 /* Print out an expression E under control of FLAGS. */
2051 static void
2052 dump_expr (cxx_pretty_printer *pp, tree t, int flags)
2054 tree op;
2056 if (t == 0)
2057 return;
2059 if (STATEMENT_CLASS_P (t))
2061 pp_cxx_ws_string (pp, M_("<statement>"));
2062 return;
2065 switch (TREE_CODE (t))
2067 case VAR_DECL:
2068 case PARM_DECL:
2069 case FIELD_DECL:
2070 case CONST_DECL:
2071 case FUNCTION_DECL:
2072 case TEMPLATE_DECL:
2073 case NAMESPACE_DECL:
2074 case LABEL_DECL:
2075 case OVERLOAD:
2076 case TYPE_DECL:
2077 case IDENTIFIER_NODE:
2078 dump_decl (pp, t, ((flags & ~(TFF_DECL_SPECIFIERS|TFF_RETURN_TYPE
2079 |TFF_TEMPLATE_HEADER))
2080 | TFF_NO_TEMPLATE_BINDINGS
2081 | TFF_NO_FUNCTION_ARGUMENTS));
2082 break;
2084 case SSA_NAME:
2085 if (SSA_NAME_VAR (t)
2086 && !DECL_ARTIFICIAL (SSA_NAME_VAR (t)))
2087 dump_expr (pp, SSA_NAME_VAR (t), flags);
2088 else
2089 pp_cxx_ws_string (pp, M_("<unknown>"));
2090 break;
2092 case VOID_CST:
2093 case INTEGER_CST:
2094 case REAL_CST:
2095 case STRING_CST:
2096 case COMPLEX_CST:
2097 pp->constant (t);
2098 break;
2100 case USERDEF_LITERAL:
2101 pp_cxx_userdef_literal (pp, t);
2102 break;
2104 case THROW_EXPR:
2105 /* While waiting for caret diagnostics, avoid printing
2106 __cxa_allocate_exception, __cxa_throw, and the like. */
2107 pp_cxx_ws_string (pp, M_("<throw-expression>"));
2108 break;
2110 case PTRMEM_CST:
2111 pp_ampersand (pp);
2112 dump_type (pp, PTRMEM_CST_CLASS (t), flags);
2113 pp_cxx_colon_colon (pp);
2114 pp_cxx_tree_identifier (pp, DECL_NAME (PTRMEM_CST_MEMBER (t)));
2115 break;
2117 case COMPOUND_EXPR:
2118 pp_cxx_left_paren (pp);
2119 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2120 pp_separate_with_comma (pp);
2121 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2122 pp_cxx_right_paren (pp);
2123 break;
2125 case COND_EXPR:
2126 case VEC_COND_EXPR:
2127 pp_cxx_left_paren (pp);
2128 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2129 pp_string (pp, " ? ");
2130 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2131 pp_string (pp, " : ");
2132 dump_expr (pp, TREE_OPERAND (t, 2), flags | TFF_EXPR_IN_PARENS);
2133 pp_cxx_right_paren (pp);
2134 break;
2136 case SAVE_EXPR:
2137 if (TREE_HAS_CONSTRUCTOR (t))
2139 pp_cxx_ws_string (pp, "new");
2140 pp_cxx_whitespace (pp);
2141 dump_type (pp, TREE_TYPE (TREE_TYPE (t)), flags);
2143 else
2144 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2145 break;
2147 case AGGR_INIT_EXPR:
2149 tree fn = NULL_TREE;
2151 if (TREE_CODE (AGGR_INIT_EXPR_FN (t)) == ADDR_EXPR)
2152 fn = TREE_OPERAND (AGGR_INIT_EXPR_FN (t), 0);
2154 if (fn && TREE_CODE (fn) == FUNCTION_DECL)
2156 if (DECL_CONSTRUCTOR_P (fn))
2157 dump_type (pp, DECL_CONTEXT (fn), flags);
2158 else
2159 dump_decl (pp, fn, 0);
2161 else
2162 dump_expr (pp, AGGR_INIT_EXPR_FN (t), 0);
2164 dump_aggr_init_expr_args (pp, t, flags, true);
2165 break;
2167 case CALL_EXPR:
2169 tree fn = CALL_EXPR_FN (t);
2170 bool skipfirst = false;
2172 /* Deal with internal functions. */
2173 if (fn == NULL_TREE)
2175 pp_string (pp, internal_fn_name (CALL_EXPR_IFN (t)));
2176 dump_call_expr_args (pp, t, flags, skipfirst);
2177 break;
2180 if (TREE_CODE (fn) == ADDR_EXPR)
2181 fn = TREE_OPERAND (fn, 0);
2183 /* Nobody is interested in seeing the guts of vcalls. */
2184 if (TREE_CODE (fn) == OBJ_TYPE_REF)
2185 fn = resolve_virtual_fun_from_obj_type_ref (fn);
2187 if (TREE_TYPE (fn) != NULL_TREE
2188 && NEXT_CODE (fn) == METHOD_TYPE
2189 && call_expr_nargs (t))
2191 tree ob = CALL_EXPR_ARG (t, 0);
2192 if (TREE_CODE (ob) == ADDR_EXPR)
2194 dump_expr (pp, TREE_OPERAND (ob, 0),
2195 flags | TFF_EXPR_IN_PARENS);
2196 pp_cxx_dot (pp);
2198 else if (!is_this_parameter (ob))
2200 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2201 pp_cxx_arrow (pp);
2203 skipfirst = true;
2205 if (flag_sanitize & SANITIZE_UNDEFINED
2206 && is_ubsan_builtin_p (fn))
2208 pp_string (cxx_pp, M_("<ubsan routine call>"));
2209 break;
2211 dump_expr (pp, fn, flags | TFF_EXPR_IN_PARENS);
2212 dump_call_expr_args (pp, t, flags, skipfirst);
2214 break;
2216 case TARGET_EXPR:
2217 /* Note that this only works for G++ target exprs. If somebody
2218 builds a general TARGET_EXPR, there's no way to represent that
2219 it initializes anything other that the parameter slot for the
2220 default argument. Note we may have cleared out the first
2221 operand in expand_expr, so don't go killing ourselves. */
2222 if (TREE_OPERAND (t, 1))
2223 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2224 break;
2226 case POINTER_PLUS_EXPR:
2227 dump_binary_op (pp, "+", t, flags);
2228 break;
2230 case POINTER_DIFF_EXPR:
2231 dump_binary_op (pp, "-", t, flags);
2232 break;
2234 case INIT_EXPR:
2235 case MODIFY_EXPR:
2236 dump_binary_op (pp, OVL_OP_INFO (true, NOP_EXPR)->name, t, flags);
2237 break;
2239 case PLUS_EXPR:
2240 case MINUS_EXPR:
2241 case MULT_EXPR:
2242 case TRUNC_DIV_EXPR:
2243 case TRUNC_MOD_EXPR:
2244 case MIN_EXPR:
2245 case MAX_EXPR:
2246 case LSHIFT_EXPR:
2247 case RSHIFT_EXPR:
2248 case BIT_IOR_EXPR:
2249 case BIT_XOR_EXPR:
2250 case BIT_AND_EXPR:
2251 case TRUTH_ANDIF_EXPR:
2252 case TRUTH_ORIF_EXPR:
2253 case LT_EXPR:
2254 case LE_EXPR:
2255 case GT_EXPR:
2256 case GE_EXPR:
2257 case EQ_EXPR:
2258 case NE_EXPR:
2259 case EXACT_DIV_EXPR:
2260 dump_binary_op (pp, OVL_OP_INFO (false, TREE_CODE (t))->name, t, flags);
2261 break;
2263 case CEIL_DIV_EXPR:
2264 case FLOOR_DIV_EXPR:
2265 case ROUND_DIV_EXPR:
2266 case RDIV_EXPR:
2267 dump_binary_op (pp, "/", t, flags);
2268 break;
2270 case CEIL_MOD_EXPR:
2271 case FLOOR_MOD_EXPR:
2272 case ROUND_MOD_EXPR:
2273 dump_binary_op (pp, "%", t, flags);
2274 break;
2276 case COMPONENT_REF:
2278 tree ob = TREE_OPERAND (t, 0);
2279 if (INDIRECT_REF_P (ob))
2281 ob = TREE_OPERAND (ob, 0);
2282 if (!is_this_parameter (ob))
2284 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2285 if (TREE_CODE (TREE_TYPE (ob)) == REFERENCE_TYPE)
2286 pp_cxx_dot (pp);
2287 else
2288 pp_cxx_arrow (pp);
2291 else
2293 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2294 if (TREE_CODE (ob) != ARROW_EXPR)
2295 pp_cxx_dot (pp);
2297 dump_expr (pp, TREE_OPERAND (t, 1), flags & ~TFF_EXPR_IN_PARENS);
2299 break;
2301 case ARRAY_REF:
2302 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2303 pp_cxx_left_bracket (pp);
2304 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2305 pp_cxx_right_bracket (pp);
2306 break;
2308 case ARRAY_NOTATION_REF:
2309 dump_expr (pp, ARRAY_NOTATION_ARRAY (t), flags | TFF_EXPR_IN_PARENS);
2310 pp_cxx_left_bracket (pp);
2311 dump_expr (pp, ARRAY_NOTATION_START (t), flags | TFF_EXPR_IN_PARENS);
2312 pp_colon (pp);
2313 dump_expr (pp, ARRAY_NOTATION_LENGTH (t), flags | TFF_EXPR_IN_PARENS);
2314 pp_colon (pp);
2315 dump_expr (pp, ARRAY_NOTATION_STRIDE (t), flags | TFF_EXPR_IN_PARENS);
2316 pp_cxx_right_bracket (pp);
2317 break;
2319 case UNARY_PLUS_EXPR:
2320 dump_unary_op (pp, "+", t, flags);
2321 break;
2323 case ADDR_EXPR:
2324 if (TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
2325 || TREE_CODE (TREE_OPERAND (t, 0)) == STRING_CST
2326 /* An ADDR_EXPR can have reference type. In that case, we
2327 shouldn't print the `&' doing so indicates to the user
2328 that the expression has pointer type. */
2329 || (TREE_TYPE (t)
2330 && TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE))
2331 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2332 else if (TREE_CODE (TREE_OPERAND (t, 0)) == LABEL_DECL)
2333 dump_unary_op (pp, "&&", t, flags);
2334 else
2335 dump_unary_op (pp, "&", t, flags);
2336 break;
2338 case INDIRECT_REF:
2339 if (TREE_HAS_CONSTRUCTOR (t))
2341 t = TREE_OPERAND (t, 0);
2342 gcc_assert (TREE_CODE (t) == CALL_EXPR);
2343 dump_expr (pp, CALL_EXPR_FN (t), flags | TFF_EXPR_IN_PARENS);
2344 dump_call_expr_args (pp, t, flags, true);
2346 else
2348 if (TREE_OPERAND (t,0) != NULL_TREE
2349 && TREE_TYPE (TREE_OPERAND (t, 0))
2350 && NEXT_CODE (TREE_OPERAND (t, 0)) == REFERENCE_TYPE)
2351 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2352 else
2353 dump_unary_op (pp, "*", t, flags);
2355 break;
2357 case MEM_REF:
2358 if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
2359 && integer_zerop (TREE_OPERAND (t, 1)))
2360 dump_expr (pp, TREE_OPERAND (TREE_OPERAND (t, 0), 0), flags);
2361 else
2363 pp_cxx_star (pp);
2364 if (!integer_zerop (TREE_OPERAND (t, 1)))
2366 pp_cxx_left_paren (pp);
2367 if (!integer_onep (TYPE_SIZE_UNIT
2368 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0))))))
2370 pp_cxx_left_paren (pp);
2371 dump_type (pp, ptr_type_node, flags);
2372 pp_cxx_right_paren (pp);
2375 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2376 if (!integer_zerop (TREE_OPERAND (t, 1)))
2378 pp_cxx_ws_string (pp, "+");
2379 dump_expr (pp, fold_convert (ssizetype, TREE_OPERAND (t, 1)),
2380 flags);
2381 pp_cxx_right_paren (pp);
2384 break;
2386 case NEGATE_EXPR:
2387 case BIT_NOT_EXPR:
2388 case TRUTH_NOT_EXPR:
2389 case PREDECREMENT_EXPR:
2390 case PREINCREMENT_EXPR:
2391 dump_unary_op (pp, OVL_OP_INFO (false, TREE_CODE (t))->name, t, flags);
2392 break;
2394 case POSTDECREMENT_EXPR:
2395 case POSTINCREMENT_EXPR:
2396 pp_cxx_left_paren (pp);
2397 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2398 pp_cxx_ws_string (pp, OVL_OP_INFO (false, TREE_CODE (t))->name);
2399 pp_cxx_right_paren (pp);
2400 break;
2402 case NON_LVALUE_EXPR:
2403 /* FIXME: This is a KLUDGE workaround for a parsing problem. There
2404 should be another level of INDIRECT_REF so that I don't have to do
2405 this. */
2406 if (TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == POINTER_TYPE)
2408 tree next = TREE_TYPE (TREE_TYPE (t));
2410 while (TYPE_PTR_P (next))
2411 next = TREE_TYPE (next);
2413 if (TREE_CODE (next) == FUNCTION_TYPE)
2415 if (flags & TFF_EXPR_IN_PARENS)
2416 pp_cxx_left_paren (pp);
2417 pp_cxx_star (pp);
2418 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2419 if (flags & TFF_EXPR_IN_PARENS)
2420 pp_cxx_right_paren (pp);
2421 break;
2423 /* Else fall through. */
2425 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2426 break;
2428 CASE_CONVERT:
2429 case IMPLICIT_CONV_EXPR:
2430 case VIEW_CONVERT_EXPR:
2432 tree op = TREE_OPERAND (t, 0);
2433 tree ttype = TREE_TYPE (t);
2434 tree optype = TREE_TYPE (op);
2436 if (TREE_CODE (ttype) != TREE_CODE (optype)
2437 && POINTER_TYPE_P (ttype)
2438 && POINTER_TYPE_P (optype)
2439 && same_type_p (TREE_TYPE (optype),
2440 TREE_TYPE (ttype)))
2442 if (TREE_CODE (ttype) == REFERENCE_TYPE)
2444 STRIP_NOPS (op);
2445 if (TREE_CODE (op) == ADDR_EXPR)
2446 dump_expr (pp, TREE_OPERAND (op, 0), flags);
2447 else
2448 dump_unary_op (pp, "*", t, flags);
2450 else
2451 dump_unary_op (pp, "&", t, flags);
2453 else if (!same_type_p (TREE_TYPE (op), TREE_TYPE (t)))
2455 /* It is a cast, but we cannot tell whether it is a
2456 reinterpret or static cast. Use the C style notation. */
2457 if (flags & TFF_EXPR_IN_PARENS)
2458 pp_cxx_left_paren (pp);
2459 pp_cxx_left_paren (pp);
2460 dump_type (pp, TREE_TYPE (t), flags);
2461 pp_cxx_right_paren (pp);
2462 dump_expr (pp, op, flags | TFF_EXPR_IN_PARENS);
2463 if (flags & TFF_EXPR_IN_PARENS)
2464 pp_cxx_right_paren (pp);
2466 else
2467 dump_expr (pp, op, flags);
2468 break;
2471 case CONSTRUCTOR:
2472 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
2474 tree idx = build_ptrmemfunc_access_expr (t, pfn_identifier);
2476 if (integer_zerop (idx))
2478 /* A NULL pointer-to-member constant. */
2479 pp_cxx_left_paren (pp);
2480 pp_cxx_left_paren (pp);
2481 dump_type (pp, TREE_TYPE (t), flags);
2482 pp_cxx_right_paren (pp);
2483 pp_character (pp, '0');
2484 pp_cxx_right_paren (pp);
2485 break;
2487 else if (tree_fits_shwi_p (idx))
2489 tree virtuals;
2490 unsigned HOST_WIDE_INT n;
2492 t = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
2493 t = TYPE_METHOD_BASETYPE (t);
2494 virtuals = BINFO_VIRTUALS (TYPE_BINFO (TYPE_MAIN_VARIANT (t)));
2496 n = tree_to_shwi (idx);
2498 /* Map vtable index back one, to allow for the null pointer to
2499 member. */
2500 --n;
2502 while (n > 0 && virtuals)
2504 --n;
2505 virtuals = TREE_CHAIN (virtuals);
2507 if (virtuals)
2509 dump_expr (pp, BV_FN (virtuals),
2510 flags | TFF_EXPR_IN_PARENS);
2511 break;
2515 if (TREE_TYPE (t) && LAMBDA_TYPE_P (TREE_TYPE (t)))
2516 pp_string (pp, "<lambda closure object>");
2517 if (TREE_TYPE (t) && EMPTY_CONSTRUCTOR_P (t))
2519 dump_type (pp, TREE_TYPE (t), 0);
2520 pp_cxx_left_paren (pp);
2521 pp_cxx_right_paren (pp);
2523 else
2525 if (!BRACE_ENCLOSED_INITIALIZER_P (t))
2526 dump_type (pp, TREE_TYPE (t), 0);
2527 pp_cxx_left_brace (pp);
2528 dump_expr_init_vec (pp, CONSTRUCTOR_ELTS (t), flags);
2529 pp_cxx_right_brace (pp);
2532 break;
2534 case OFFSET_REF:
2536 tree ob = TREE_OPERAND (t, 0);
2537 if (is_dummy_object (ob))
2539 t = TREE_OPERAND (t, 1);
2540 if (TREE_CODE (t) == FUNCTION_DECL)
2541 /* A::f */
2542 dump_expr (pp, t, flags | TFF_EXPR_IN_PARENS);
2543 else if (BASELINK_P (t))
2544 dump_expr (pp, OVL_FIRST (BASELINK_FUNCTIONS (t)),
2545 flags | TFF_EXPR_IN_PARENS);
2546 else
2547 dump_decl (pp, t, flags);
2549 else
2551 if (INDIRECT_REF_P (ob))
2553 dump_expr (pp, TREE_OPERAND (ob, 0), flags | TFF_EXPR_IN_PARENS);
2554 pp_cxx_arrow (pp);
2555 pp_cxx_star (pp);
2557 else
2559 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2560 pp_cxx_dot (pp);
2561 pp_cxx_star (pp);
2563 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2565 break;
2568 case TEMPLATE_PARM_INDEX:
2569 dump_decl (pp, TEMPLATE_PARM_DECL (t), flags & ~TFF_DECL_SPECIFIERS);
2570 break;
2572 case CAST_EXPR:
2573 if (TREE_OPERAND (t, 0) == NULL_TREE
2574 || TREE_CHAIN (TREE_OPERAND (t, 0)))
2576 dump_type (pp, TREE_TYPE (t), flags);
2577 pp_cxx_left_paren (pp);
2578 dump_expr_list (pp, TREE_OPERAND (t, 0), flags);
2579 pp_cxx_right_paren (pp);
2581 else
2583 pp_cxx_left_paren (pp);
2584 dump_type (pp, TREE_TYPE (t), flags);
2585 pp_cxx_right_paren (pp);
2586 pp_cxx_left_paren (pp);
2587 dump_expr_list (pp, TREE_OPERAND (t, 0), flags);
2588 pp_cxx_right_paren (pp);
2590 break;
2592 case STATIC_CAST_EXPR:
2593 pp_cxx_ws_string (pp, "static_cast");
2594 goto cast;
2595 case REINTERPRET_CAST_EXPR:
2596 pp_cxx_ws_string (pp, "reinterpret_cast");
2597 goto cast;
2598 case CONST_CAST_EXPR:
2599 pp_cxx_ws_string (pp, "const_cast");
2600 goto cast;
2601 case DYNAMIC_CAST_EXPR:
2602 pp_cxx_ws_string (pp, "dynamic_cast");
2603 cast:
2604 pp_cxx_begin_template_argument_list (pp);
2605 dump_type (pp, TREE_TYPE (t), flags);
2606 pp_cxx_end_template_argument_list (pp);
2607 pp_cxx_left_paren (pp);
2608 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2609 pp_cxx_right_paren (pp);
2610 break;
2612 case ARROW_EXPR:
2613 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2614 pp_cxx_arrow (pp);
2615 break;
2617 case SIZEOF_EXPR:
2618 case ALIGNOF_EXPR:
2619 if (TREE_CODE (t) == SIZEOF_EXPR)
2620 pp_cxx_ws_string (pp, "sizeof");
2621 else
2623 gcc_assert (TREE_CODE (t) == ALIGNOF_EXPR);
2624 pp_cxx_ws_string (pp, "__alignof__");
2626 op = TREE_OPERAND (t, 0);
2627 if (PACK_EXPANSION_P (op))
2629 pp_string (pp, "...");
2630 op = PACK_EXPANSION_PATTERN (op);
2632 pp_cxx_whitespace (pp);
2633 pp_cxx_left_paren (pp);
2634 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
2635 dump_type (pp, TREE_TYPE (op), flags);
2636 else if (TYPE_P (TREE_OPERAND (t, 0)))
2637 dump_type (pp, op, flags);
2638 else
2639 dump_expr (pp, op, flags);
2640 pp_cxx_right_paren (pp);
2641 break;
2643 case AT_ENCODE_EXPR:
2644 pp_cxx_ws_string (pp, "@encode");
2645 pp_cxx_whitespace (pp);
2646 pp_cxx_left_paren (pp);
2647 dump_type (pp, TREE_OPERAND (t, 0), flags);
2648 pp_cxx_right_paren (pp);
2649 break;
2651 case NOEXCEPT_EXPR:
2652 pp_cxx_ws_string (pp, "noexcept");
2653 pp_cxx_whitespace (pp);
2654 pp_cxx_left_paren (pp);
2655 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2656 pp_cxx_right_paren (pp);
2657 break;
2659 case REALPART_EXPR:
2660 case IMAGPART_EXPR:
2661 pp_cxx_ws_string (pp, OVL_OP_INFO (false, TREE_CODE (t))->name);
2662 pp_cxx_whitespace (pp);
2663 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2664 break;
2666 case DEFAULT_ARG:
2667 pp_string (pp, M_("<unparsed>"));
2668 break;
2670 case TRY_CATCH_EXPR:
2671 case CLEANUP_POINT_EXPR:
2672 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2673 break;
2675 case PSEUDO_DTOR_EXPR:
2676 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2677 pp_cxx_dot (pp);
2678 if (TREE_OPERAND (t, 1))
2680 dump_type (pp, TREE_OPERAND (t, 1), flags);
2681 pp_cxx_colon_colon (pp);
2683 pp_cxx_complement (pp);
2684 dump_type (pp, TREE_OPERAND (t, 2), flags);
2685 break;
2687 case TEMPLATE_ID_EXPR:
2688 dump_decl (pp, t, flags);
2689 break;
2691 case BIND_EXPR:
2692 case STMT_EXPR:
2693 case EXPR_STMT:
2694 case STATEMENT_LIST:
2695 /* We don't yet have a way of dumping statements in a
2696 human-readable format. */
2697 pp_string (pp, "({...})");
2698 break;
2700 case LOOP_EXPR:
2701 pp_string (pp, "while (1) { ");
2702 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2703 pp_cxx_right_brace (pp);
2704 break;
2706 case EXIT_EXPR:
2707 pp_string (pp, "if (");
2708 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2709 pp_string (pp, ") break; ");
2710 break;
2712 case BASELINK:
2713 dump_expr (pp, BASELINK_FUNCTIONS (t), flags & ~TFF_EXPR_IN_PARENS);
2714 break;
2716 case EMPTY_CLASS_EXPR:
2717 dump_type (pp, TREE_TYPE (t), flags);
2718 pp_cxx_left_paren (pp);
2719 pp_cxx_right_paren (pp);
2720 break;
2722 case NON_DEPENDENT_EXPR:
2723 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2724 break;
2726 case ARGUMENT_PACK_SELECT:
2727 dump_template_argument (pp, ARGUMENT_PACK_SELECT_FROM_PACK (t), flags);
2728 break;
2730 case RECORD_TYPE:
2731 case UNION_TYPE:
2732 case ENUMERAL_TYPE:
2733 case REAL_TYPE:
2734 case VOID_TYPE:
2735 case BOOLEAN_TYPE:
2736 case INTEGER_TYPE:
2737 case COMPLEX_TYPE:
2738 case VECTOR_TYPE:
2739 pp_type_specifier_seq (pp, t);
2740 break;
2742 case TYPENAME_TYPE:
2743 /* We get here when we want to print a dependent type as an
2744 id-expression, without any disambiguator decoration. */
2745 pp->id_expression (t);
2746 break;
2748 case TEMPLATE_TYPE_PARM:
2749 case TEMPLATE_TEMPLATE_PARM:
2750 case BOUND_TEMPLATE_TEMPLATE_PARM:
2751 dump_type (pp, t, flags);
2752 break;
2754 case TRAIT_EXPR:
2755 pp_cxx_trait_expression (pp, t);
2756 break;
2758 case VA_ARG_EXPR:
2759 pp_cxx_va_arg_expression (pp, t);
2760 break;
2762 case OFFSETOF_EXPR:
2763 pp_cxx_offsetof_expression (pp, t);
2764 break;
2766 case ADDRESSOF_EXPR:
2767 pp_cxx_addressof_expression (pp, t);
2768 break;
2770 case SCOPE_REF:
2771 dump_decl (pp, t, flags);
2772 break;
2774 case EXPR_PACK_EXPANSION:
2775 case UNARY_LEFT_FOLD_EXPR:
2776 case UNARY_RIGHT_FOLD_EXPR:
2777 case BINARY_LEFT_FOLD_EXPR:
2778 case BINARY_RIGHT_FOLD_EXPR:
2779 case TYPEID_EXPR:
2780 case MEMBER_REF:
2781 case DOTSTAR_EXPR:
2782 case NEW_EXPR:
2783 case VEC_NEW_EXPR:
2784 case DELETE_EXPR:
2785 case VEC_DELETE_EXPR:
2786 case MODOP_EXPR:
2787 case ABS_EXPR:
2788 case CONJ_EXPR:
2789 case VECTOR_CST:
2790 case FIXED_CST:
2791 case UNORDERED_EXPR:
2792 case ORDERED_EXPR:
2793 case UNLT_EXPR:
2794 case UNLE_EXPR:
2795 case UNGT_EXPR:
2796 case UNGE_EXPR:
2797 case UNEQ_EXPR:
2798 case LTGT_EXPR:
2799 case COMPLEX_EXPR:
2800 case BIT_FIELD_REF:
2801 case FIX_TRUNC_EXPR:
2802 case FLOAT_EXPR:
2803 pp->expression (t);
2804 break;
2806 case TRUTH_AND_EXPR:
2807 case TRUTH_OR_EXPR:
2808 case TRUTH_XOR_EXPR:
2809 if (flags & TFF_EXPR_IN_PARENS)
2810 pp_cxx_left_paren (pp);
2811 pp->expression (t);
2812 if (flags & TFF_EXPR_IN_PARENS)
2813 pp_cxx_right_paren (pp);
2814 break;
2816 case OBJ_TYPE_REF:
2817 dump_expr (pp, resolve_virtual_fun_from_obj_type_ref (t), flags);
2818 break;
2820 case LAMBDA_EXPR:
2821 pp_string (pp, M_("<lambda>"));
2822 break;
2824 case PAREN_EXPR:
2825 pp_cxx_left_paren (pp);
2826 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2827 pp_cxx_right_paren (pp);
2828 break;
2830 case REQUIRES_EXPR:
2831 pp_cxx_requires_expr (cxx_pp, t);
2832 break;
2834 case SIMPLE_REQ:
2835 pp_cxx_simple_requirement (cxx_pp, t);
2836 break;
2838 case TYPE_REQ:
2839 pp_cxx_type_requirement (cxx_pp, t);
2840 break;
2842 case COMPOUND_REQ:
2843 pp_cxx_compound_requirement (cxx_pp, t);
2844 break;
2846 case NESTED_REQ:
2847 pp_cxx_nested_requirement (cxx_pp, t);
2848 break;
2850 case PRED_CONSTR:
2851 case CHECK_CONSTR:
2852 case EXPR_CONSTR:
2853 case TYPE_CONSTR:
2854 case ICONV_CONSTR:
2855 case DEDUCT_CONSTR:
2856 case EXCEPT_CONSTR:
2857 case PARM_CONSTR:
2858 case CONJ_CONSTR:
2859 case DISJ_CONSTR:
2860 pp_cxx_constraint (cxx_pp, t);
2861 break;
2863 case PLACEHOLDER_EXPR:
2864 pp_string (pp, M_("*this"));
2865 break;
2867 case TREE_LIST:
2868 dump_expr_list (pp, t, flags);
2869 break;
2871 /* This list is incomplete, but should suffice for now.
2872 It is very important that `sorry' does not call
2873 `report_error_function'. That could cause an infinite loop. */
2874 default:
2875 pp_unsupported_tree (pp, t);
2876 /* Fall through. */
2877 case ERROR_MARK:
2878 pp_string (pp, M_("<expression error>"));
2879 break;
2883 static void
2884 dump_binary_op (cxx_pretty_printer *pp, const char *opstring, tree t,
2885 int flags)
2887 pp_cxx_left_paren (pp);
2888 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2889 pp_cxx_whitespace (pp);
2890 if (opstring)
2891 pp_cxx_ws_string (pp, opstring);
2892 else
2893 pp_string (pp, M_("<unknown operator>"));
2894 pp_cxx_whitespace (pp);
2895 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2896 pp_cxx_right_paren (pp);
2899 static void
2900 dump_unary_op (cxx_pretty_printer *pp, const char *opstring, tree t, int flags)
2902 if (flags & TFF_EXPR_IN_PARENS)
2903 pp_cxx_left_paren (pp);
2904 pp_cxx_ws_string (pp, opstring);
2905 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2906 if (flags & TFF_EXPR_IN_PARENS)
2907 pp_cxx_right_paren (pp);
2910 static void
2911 reinit_cxx_pp (void)
2913 pp_clear_output_area (cxx_pp);
2914 cxx_pp->padding = pp_none;
2915 pp_indentation (cxx_pp) = 0;
2916 pp_needs_newline (cxx_pp) = false;
2917 cxx_pp->enclosing_scope = current_function_decl;
2920 /* Same as pp_formatted_text, except the return string is a separate
2921 copy and has a GGC storage duration, e.g. an indefinite lifetime. */
2923 inline const char *
2924 pp_ggc_formatted_text (pretty_printer *pp)
2926 return ggc_strdup (pp_formatted_text (pp));
2929 /* Exported interface to stringifying types, exprs and decls under TFF_*
2930 control. */
2932 const char *
2933 type_as_string (tree typ, int flags)
2935 reinit_cxx_pp ();
2936 pp_translate_identifiers (cxx_pp) = false;
2937 dump_type (cxx_pp, typ, flags);
2938 return pp_ggc_formatted_text (cxx_pp);
2941 const char *
2942 type_as_string_translate (tree typ, int flags)
2944 reinit_cxx_pp ();
2945 dump_type (cxx_pp, typ, flags);
2946 return pp_ggc_formatted_text (cxx_pp);
2949 const char *
2950 expr_as_string (tree decl, int flags)
2952 reinit_cxx_pp ();
2953 pp_translate_identifiers (cxx_pp) = false;
2954 dump_expr (cxx_pp, decl, flags);
2955 return pp_ggc_formatted_text (cxx_pp);
2958 /* Wrap decl_as_string with options appropriate for dwarf. */
2960 const char *
2961 decl_as_dwarf_string (tree decl, int flags)
2963 const char *name;
2964 /* Curiously, reinit_cxx_pp doesn't reset the flags field, so setting the flag
2965 here will be adequate to get the desired behavior. */
2966 cxx_pp->flags |= pp_c_flag_gnu_v3;
2967 name = decl_as_string (decl, flags);
2968 /* Subsequent calls to the pretty printer shouldn't use this style. */
2969 cxx_pp->flags &= ~pp_c_flag_gnu_v3;
2970 return name;
2973 const char *
2974 decl_as_string (tree decl, int flags)
2976 reinit_cxx_pp ();
2977 pp_translate_identifiers (cxx_pp) = false;
2978 dump_decl (cxx_pp, decl, flags);
2979 return pp_ggc_formatted_text (cxx_pp);
2982 const char *
2983 decl_as_string_translate (tree decl, int flags)
2985 reinit_cxx_pp ();
2986 dump_decl (cxx_pp, decl, flags);
2987 return pp_ggc_formatted_text (cxx_pp);
2990 /* Wrap lang_decl_name with options appropriate for dwarf. */
2992 const char *
2993 lang_decl_dwarf_name (tree decl, int v, bool translate)
2995 const char *name;
2996 /* Curiously, reinit_cxx_pp doesn't reset the flags field, so setting the flag
2997 here will be adequate to get the desired behavior. */
2998 cxx_pp->flags |= pp_c_flag_gnu_v3;
2999 name = lang_decl_name (decl, v, translate);
3000 /* Subsequent calls to the pretty printer shouldn't use this style. */
3001 cxx_pp->flags &= ~pp_c_flag_gnu_v3;
3002 return name;
3005 /* Generate the three forms of printable names for cxx_printable_name. */
3007 const char *
3008 lang_decl_name (tree decl, int v, bool translate)
3010 if (v >= 2)
3011 return (translate
3012 ? decl_as_string_translate (decl, TFF_DECL_SPECIFIERS)
3013 : decl_as_string (decl, TFF_DECL_SPECIFIERS));
3015 reinit_cxx_pp ();
3016 pp_translate_identifiers (cxx_pp) = translate;
3017 if (v == 1
3018 && (DECL_CLASS_SCOPE_P (decl)
3019 || (DECL_NAMESPACE_SCOPE_P (decl)
3020 && CP_DECL_CONTEXT (decl) != global_namespace)))
3022 dump_type (cxx_pp, CP_DECL_CONTEXT (decl), TFF_PLAIN_IDENTIFIER);
3023 pp_cxx_colon_colon (cxx_pp);
3026 if (TREE_CODE (decl) == FUNCTION_DECL)
3027 dump_function_name (cxx_pp, decl, TFF_PLAIN_IDENTIFIER);
3028 else if ((DECL_NAME (decl) == NULL_TREE)
3029 && TREE_CODE (decl) == NAMESPACE_DECL)
3030 dump_decl (cxx_pp, decl, TFF_PLAIN_IDENTIFIER | TFF_UNQUALIFIED_NAME);
3031 else
3032 dump_decl (cxx_pp, DECL_NAME (decl), TFF_PLAIN_IDENTIFIER);
3034 return pp_ggc_formatted_text (cxx_pp);
3037 /* Return the location of a tree passed to %+ formats. */
3039 location_t
3040 location_of (tree t)
3042 if (TYPE_P (t))
3044 t = TYPE_MAIN_DECL (t);
3045 if (t == NULL_TREE)
3046 return input_location;
3048 else if (TREE_CODE (t) == OVERLOAD)
3049 t = OVL_FIRST (t);
3051 if (DECL_P (t))
3052 return DECL_SOURCE_LOCATION (t);
3053 if (TREE_CODE (t) == DEFAULT_ARG)
3054 return defarg_location (t);
3055 return EXPR_LOC_OR_LOC (t, input_location);
3058 /* Now the interfaces from error et al to dump_type et al. Each takes an
3059 on/off VERBOSE flag and supply the appropriate TFF_ flags to a dump_
3060 function. */
3062 static const char *
3063 decl_to_string (tree decl, int verbose)
3065 int flags = 0;
3067 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == RECORD_TYPE
3068 || TREE_CODE (decl) == UNION_TYPE || TREE_CODE (decl) == ENUMERAL_TYPE)
3069 flags = TFF_CLASS_KEY_OR_ENUM;
3070 if (verbose)
3071 flags |= TFF_DECL_SPECIFIERS;
3072 else if (TREE_CODE (decl) == FUNCTION_DECL)
3073 flags |= TFF_DECL_SPECIFIERS | TFF_RETURN_TYPE;
3074 flags |= TFF_TEMPLATE_HEADER;
3076 reinit_cxx_pp ();
3077 dump_decl (cxx_pp, decl, flags);
3078 return pp_ggc_formatted_text (cxx_pp);
3081 static const char *
3082 expr_to_string (tree decl)
3084 reinit_cxx_pp ();
3085 dump_expr (cxx_pp, decl, 0);
3086 return pp_ggc_formatted_text (cxx_pp);
3089 static const char *
3090 fndecl_to_string (tree fndecl, int verbose)
3092 int flags;
3094 flags = TFF_EXCEPTION_SPECIFICATION | TFF_DECL_SPECIFIERS
3095 | TFF_TEMPLATE_HEADER;
3096 if (verbose)
3097 flags |= TFF_FUNCTION_DEFAULT_ARGUMENTS;
3098 reinit_cxx_pp ();
3099 dump_decl (cxx_pp, fndecl, flags);
3100 return pp_ggc_formatted_text (cxx_pp);
3104 static const char *
3105 code_to_string (enum tree_code c)
3107 return get_tree_code_name (c);
3110 const char *
3111 language_to_string (enum languages c)
3113 switch (c)
3115 case lang_c:
3116 return "C";
3118 case lang_cplusplus:
3119 return "C++";
3121 default:
3122 gcc_unreachable ();
3124 return NULL;
3127 /* Return the proper printed version of a parameter to a C++ function. */
3129 static const char *
3130 parm_to_string (int p)
3132 reinit_cxx_pp ();
3133 if (p < 0)
3134 pp_string (cxx_pp, "'this'");
3135 else
3136 pp_decimal_int (cxx_pp, p + 1);
3137 return pp_ggc_formatted_text (cxx_pp);
3140 static const char *
3141 op_to_string (bool assop, enum tree_code p)
3143 tree id = ovl_op_identifier (assop, p);
3144 return id ? IDENTIFIER_POINTER (id) : M_("<unknown>");
3147 static const char *
3148 type_to_string (tree typ, int verbose)
3150 int flags = 0;
3151 if (verbose)
3152 flags |= TFF_CLASS_KEY_OR_ENUM;
3153 flags |= TFF_TEMPLATE_HEADER;
3155 reinit_cxx_pp ();
3156 dump_type (cxx_pp, typ, flags);
3157 /* If we're printing a type that involves typedefs, also print the
3158 stripped version. But sometimes the stripped version looks
3159 exactly the same, so we don't want it after all. To avoid printing
3160 it in that case, we play ugly obstack games. */
3161 if (typ && TYPE_P (typ) && typ != TYPE_CANONICAL (typ)
3162 && !uses_template_parms (typ))
3164 int aka_start, aka_len; char *p;
3165 struct obstack *ob = pp_buffer (cxx_pp)->obstack;
3166 /* Remember the end of the initial dump. */
3167 int len = obstack_object_size (ob);
3168 tree aka = strip_typedefs (typ);
3169 pp_string (cxx_pp, " {aka");
3170 pp_cxx_whitespace (cxx_pp);
3171 /* And remember the start of the aka dump. */
3172 aka_start = obstack_object_size (ob);
3173 dump_type (cxx_pp, aka, flags);
3174 aka_len = obstack_object_size (ob) - aka_start;
3175 pp_right_brace (cxx_pp);
3176 p = (char*)obstack_base (ob);
3177 /* If they are identical, cut off the aka with a NUL. */
3178 if (len == aka_len && memcmp (p, p+aka_start, len) == 0)
3179 p[len] = '\0';
3181 return pp_ggc_formatted_text (cxx_pp);
3184 static const char *
3185 args_to_string (tree p, int verbose)
3187 int flags = 0;
3188 if (verbose)
3189 flags |= TFF_CLASS_KEY_OR_ENUM;
3191 if (p == NULL_TREE)
3192 return "";
3194 if (TYPE_P (TREE_VALUE (p)))
3195 return type_as_string_translate (p, flags);
3197 reinit_cxx_pp ();
3198 for (; p; p = TREE_CHAIN (p))
3200 if (TREE_VALUE (p) == null_node)
3201 pp_cxx_ws_string (cxx_pp, "NULL");
3202 else
3203 dump_type (cxx_pp, error_type (TREE_VALUE (p)), flags);
3204 if (TREE_CHAIN (p))
3205 pp_separate_with_comma (cxx_pp);
3207 return pp_ggc_formatted_text (cxx_pp);
3210 /* Pretty-print a deduction substitution (from deduction_tsubst_fntype). P
3211 is a TREE_LIST with purpose the TEMPLATE_DECL, value the template
3212 arguments. */
3214 static const char *
3215 subst_to_string (tree p)
3217 tree decl = TREE_PURPOSE (p);
3218 tree targs = TREE_VALUE (p);
3219 tree tparms = DECL_TEMPLATE_PARMS (decl);
3220 int flags = (TFF_DECL_SPECIFIERS|TFF_TEMPLATE_HEADER
3221 |TFF_NO_TEMPLATE_BINDINGS);
3223 if (p == NULL_TREE)
3224 return "";
3226 reinit_cxx_pp ();
3227 dump_template_decl (cxx_pp, TREE_PURPOSE (p), flags);
3228 dump_substitution (cxx_pp, NULL, tparms, targs, /*flags=*/0);
3229 return pp_ggc_formatted_text (cxx_pp);
3232 static const char *
3233 cv_to_string (tree p, int v)
3235 reinit_cxx_pp ();
3236 cxx_pp->padding = v ? pp_before : pp_none;
3237 pp_cxx_cv_qualifier_seq (cxx_pp, p);
3238 return pp_ggc_formatted_text (cxx_pp);
3241 static const char *
3242 eh_spec_to_string (tree p, int /*v*/)
3244 int flags = 0;
3245 reinit_cxx_pp ();
3246 dump_exception_spec (cxx_pp, p, flags);
3247 return pp_ggc_formatted_text (cxx_pp);
3250 /* Langhook for print_error_function. */
3251 void
3252 cxx_print_error_function (diagnostic_context *context, const char *file,
3253 diagnostic_info *diagnostic)
3255 lhd_print_error_function (context, file, diagnostic);
3256 pp_set_prefix (context->printer, file);
3257 maybe_print_instantiation_context (context);
3260 static void
3261 cp_diagnostic_starter (diagnostic_context *context,
3262 diagnostic_info *diagnostic)
3264 diagnostic_report_current_module (context, diagnostic_location (diagnostic));
3265 cp_print_error_function (context, diagnostic);
3266 maybe_print_instantiation_context (context);
3267 maybe_print_constexpr_context (context);
3268 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
3269 diagnostic));
3272 /* Print current function onto BUFFER, in the process of reporting
3273 a diagnostic message. Called from cp_diagnostic_starter. */
3274 static void
3275 cp_print_error_function (diagnostic_context *context,
3276 diagnostic_info *diagnostic)
3278 /* If we are in an instantiation context, current_function_decl is likely
3279 to be wrong, so just rely on print_instantiation_full_context. */
3280 if (current_instantiation ())
3281 return;
3282 if (diagnostic_last_function_changed (context, diagnostic))
3284 const char *old_prefix = context->printer->prefix;
3285 const char *file = LOCATION_FILE (diagnostic_location (diagnostic));
3286 tree abstract_origin = diagnostic_abstract_origin (diagnostic);
3287 char *new_prefix = (file && abstract_origin == NULL)
3288 ? file_name_as_prefix (context, file) : NULL;
3290 pp_set_prefix (context->printer, new_prefix);
3292 if (current_function_decl == NULL)
3293 pp_string (context->printer, _("At global scope:"));
3294 else
3296 tree fndecl, ao;
3298 if (abstract_origin)
3300 ao = BLOCK_ABSTRACT_ORIGIN (abstract_origin);
3301 while (TREE_CODE (ao) == BLOCK
3302 && BLOCK_ABSTRACT_ORIGIN (ao)
3303 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
3304 ao = BLOCK_ABSTRACT_ORIGIN (ao);
3305 gcc_assert (TREE_CODE (ao) == FUNCTION_DECL);
3306 fndecl = ao;
3308 else
3309 fndecl = current_function_decl;
3311 pp_printf (context->printer, function_category (fndecl),
3312 cxx_printable_name_translate (fndecl, 2));
3314 while (abstract_origin)
3316 location_t *locus;
3317 tree block = abstract_origin;
3319 locus = &BLOCK_SOURCE_LOCATION (block);
3320 fndecl = NULL;
3321 block = BLOCK_SUPERCONTEXT (block);
3322 while (block && TREE_CODE (block) == BLOCK
3323 && BLOCK_ABSTRACT_ORIGIN (block))
3325 ao = BLOCK_ABSTRACT_ORIGIN (block);
3327 while (TREE_CODE (ao) == BLOCK
3328 && BLOCK_ABSTRACT_ORIGIN (ao)
3329 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
3330 ao = BLOCK_ABSTRACT_ORIGIN (ao);
3332 if (TREE_CODE (ao) == FUNCTION_DECL)
3334 fndecl = ao;
3335 break;
3337 else if (TREE_CODE (ao) != BLOCK)
3338 break;
3340 block = BLOCK_SUPERCONTEXT (block);
3342 if (fndecl)
3343 abstract_origin = block;
3344 else
3346 while (block && TREE_CODE (block) == BLOCK)
3347 block = BLOCK_SUPERCONTEXT (block);
3349 if (block && TREE_CODE (block) == FUNCTION_DECL)
3350 fndecl = block;
3351 abstract_origin = NULL;
3353 if (fndecl)
3355 expanded_location s = expand_location (*locus);
3356 pp_character (context->printer, ',');
3357 pp_newline (context->printer);
3358 if (s.file != NULL)
3360 if (context->show_column && s.column != 0)
3361 pp_printf (context->printer,
3362 _(" inlined from %qs at %r%s:%d:%d%R"),
3363 cxx_printable_name_translate (fndecl, 2),
3364 "locus", s.file, s.line, s.column);
3365 else
3366 pp_printf (context->printer,
3367 _(" inlined from %qs at %r%s:%d%R"),
3368 cxx_printable_name_translate (fndecl, 2),
3369 "locus", s.file, s.line);
3372 else
3373 pp_printf (context->printer, _(" inlined from %qs"),
3374 cxx_printable_name_translate (fndecl, 2));
3377 pp_character (context->printer, ':');
3379 pp_newline (context->printer);
3381 diagnostic_set_last_function (context, diagnostic);
3382 pp_destroy_prefix (context->printer);
3383 context->printer->prefix = old_prefix;
3387 /* Returns a description of FUNCTION using standard terminology. The
3388 result is a format string of the form "In CATEGORY %qs". */
3389 static const char *
3390 function_category (tree fn)
3392 /* We can get called from the middle-end for diagnostics of function
3393 clones. Make sure we have language specific information before
3394 dereferencing it. */
3395 if (DECL_LANG_SPECIFIC (STRIP_TEMPLATE (fn))
3396 && DECL_FUNCTION_MEMBER_P (fn))
3398 if (DECL_STATIC_FUNCTION_P (fn))
3399 return _("In static member function %qs");
3400 else if (DECL_COPY_CONSTRUCTOR_P (fn))
3401 return _("In copy constructor %qs");
3402 else if (DECL_CONSTRUCTOR_P (fn))
3403 return _("In constructor %qs");
3404 else if (DECL_DESTRUCTOR_P (fn))
3405 return _("In destructor %qs");
3406 else if (LAMBDA_FUNCTION_P (fn))
3407 return _("In lambda function");
3408 else
3409 return _("In member function %qs");
3411 else
3412 return _("In function %qs");
3415 /* Report the full context of a current template instantiation,
3416 onto BUFFER. */
3417 static void
3418 print_instantiation_full_context (diagnostic_context *context)
3420 struct tinst_level *p = current_instantiation ();
3421 location_t location = input_location;
3423 if (p)
3425 pp_verbatim (context->printer,
3426 TREE_CODE (p->decl) == TREE_LIST
3427 ? _("%s: In substitution of %qS:\n")
3428 : _("%s: In instantiation of %q#D:\n"),
3429 LOCATION_FILE (location),
3430 p->decl);
3432 location = p->locus;
3433 p = p->next;
3436 print_instantiation_partial_context (context, p, location);
3439 /* Helper function of print_instantiation_partial_context() that
3440 prints a single line of instantiation context. */
3442 static void
3443 print_instantiation_partial_context_line (diagnostic_context *context,
3444 const struct tinst_level *t,
3445 location_t loc, bool recursive_p)
3447 if (loc == UNKNOWN_LOCATION)
3448 return;
3450 expanded_location xloc = expand_location (loc);
3452 if (context->show_column)
3453 pp_verbatim (context->printer, _("%r%s:%d:%d:%R "),
3454 "locus", xloc.file, xloc.line, xloc.column);
3455 else
3456 pp_verbatim (context->printer, _("%r%s:%d:%R "),
3457 "locus", xloc.file, xloc.line);
3459 if (t != NULL)
3461 if (TREE_CODE (t->decl) == TREE_LIST)
3462 pp_verbatim (context->printer,
3463 recursive_p
3464 ? _("recursively required by substitution of %qS\n")
3465 : _("required by substitution of %qS\n"),
3466 t->decl);
3467 else
3468 pp_verbatim (context->printer,
3469 recursive_p
3470 ? _("recursively required from %q#D\n")
3471 : _("required from %q#D\n"),
3472 t->decl);
3474 else
3476 pp_verbatim (context->printer,
3477 recursive_p
3478 ? _("recursively required from here\n")
3479 : _("required from here\n"));
3483 /* Same as print_instantiation_full_context but less verbose. */
3485 static void
3486 print_instantiation_partial_context (diagnostic_context *context,
3487 struct tinst_level *t0, location_t loc)
3489 struct tinst_level *t;
3490 int n_total = 0;
3491 int n;
3492 location_t prev_loc = loc;
3494 for (t = t0; t != NULL; t = t->next)
3495 if (prev_loc != t->locus)
3497 prev_loc = t->locus;
3498 n_total++;
3501 t = t0;
3503 if (template_backtrace_limit
3504 && n_total > template_backtrace_limit)
3506 int skip = n_total - template_backtrace_limit;
3507 int head = template_backtrace_limit / 2;
3509 /* Avoid skipping just 1. If so, skip 2. */
3510 if (skip == 1)
3512 skip = 2;
3513 head = (template_backtrace_limit - 1) / 2;
3516 for (n = 0; n < head; n++)
3518 gcc_assert (t != NULL);
3519 if (loc != t->locus)
3520 print_instantiation_partial_context_line (context, t, loc,
3521 /*recursive_p=*/false);
3522 loc = t->locus;
3523 t = t->next;
3525 if (t != NULL && skip > 0)
3527 expanded_location xloc;
3528 xloc = expand_location (loc);
3529 if (context->show_column)
3530 pp_verbatim (context->printer,
3531 _("%r%s:%d:%d:%R [ skipping %d instantiation "
3532 "contexts, use -ftemplate-backtrace-limit=0 to "
3533 "disable ]\n"),
3534 "locus", xloc.file, xloc.line, xloc.column, skip);
3535 else
3536 pp_verbatim (context->printer,
3537 _("%r%s:%d:%R [ skipping %d instantiation "
3538 "contexts, use -ftemplate-backtrace-limit=0 to "
3539 "disable ]\n"),
3540 "locus", xloc.file, xloc.line, skip);
3542 do {
3543 loc = t->locus;
3544 t = t->next;
3545 } while (t != NULL && --skip > 0);
3549 while (t != NULL)
3551 while (t->next != NULL && t->locus == t->next->locus)
3553 loc = t->locus;
3554 t = t->next;
3556 print_instantiation_partial_context_line (context, t, loc,
3557 t->locus == loc);
3558 loc = t->locus;
3559 t = t->next;
3561 print_instantiation_partial_context_line (context, NULL, loc,
3562 /*recursive_p=*/false);
3565 /* Called from cp_thing to print the template context for an error. */
3566 static void
3567 maybe_print_instantiation_context (diagnostic_context *context)
3569 if (!problematic_instantiation_changed () || current_instantiation () == 0)
3570 return;
3572 record_last_problematic_instantiation ();
3573 print_instantiation_full_context (context);
3576 /* Report what constexpr call(s) we're trying to expand, if any. */
3578 void
3579 maybe_print_constexpr_context (diagnostic_context *context)
3581 vec<tree> call_stack = cx_error_context ();
3582 unsigned ix;
3583 tree t;
3585 FOR_EACH_VEC_ELT (call_stack, ix, t)
3587 expanded_location xloc = expand_location (EXPR_LOCATION (t));
3588 const char *s = expr_as_string (t, 0);
3589 if (context->show_column)
3590 pp_verbatim (context->printer,
3591 _("%r%s:%d:%d:%R in %<constexpr%> expansion of %qs"),
3592 "locus", xloc.file, xloc.line, xloc.column, s);
3593 else
3594 pp_verbatim (context->printer,
3595 _("%r%s:%d:%R in %<constexpr%> expansion of %qs"),
3596 "locus", xloc.file, xloc.line, s);
3597 pp_newline (context->printer);
3602 /* Return true iff TYPE_A and TYPE_B are template types that are
3603 meaningful to compare. */
3605 static bool
3606 comparable_template_types_p (tree type_a, tree type_b)
3608 if (!CLASS_TYPE_P (type_a))
3609 return false;
3610 if (!CLASS_TYPE_P (type_b))
3611 return false;
3613 tree tinfo_a = TYPE_TEMPLATE_INFO (type_a);
3614 tree tinfo_b = TYPE_TEMPLATE_INFO (type_b);
3615 if (!tinfo_a || !tinfo_b)
3616 return false;
3618 return TI_TEMPLATE (tinfo_a) == TI_TEMPLATE (tinfo_b);
3621 /* Start a new line indented by SPC spaces on PP. */
3623 static void
3624 newline_and_indent (pretty_printer *pp, int spc)
3626 pp_newline (pp);
3627 for (int i = 0; i < spc; i++)
3628 pp_space (pp);
3631 /* Generate a GC-allocated string for ARG, an expression or type. */
3633 static const char *
3634 arg_to_string (tree arg, bool verbose)
3636 if (TYPE_P (arg))
3637 return type_to_string (arg, verbose);
3638 else
3639 return expr_to_string (arg);
3642 /* Subroutine to type_to_string_with_compare and
3643 print_template_tree_comparison.
3645 Print a representation of ARG (an expression or type) to PP,
3646 colorizing it as "type-diff" if PP->show_color. */
3648 static void
3649 print_nonequal_arg (pretty_printer *pp, tree arg, bool verbose)
3651 pp_printf (pp, "%r%s%R",
3652 "type-diff",
3653 (arg
3654 ? arg_to_string (arg, verbose)
3655 : G_("(no argument)")));
3658 /* Recursively print template TYPE_A to PP, as compared to template TYPE_B.
3660 The types must satisfy comparable_template_types_p.
3662 If INDENT is 0, then this is equivalent to type_to_string (TYPE_A), but
3663 potentially colorizing/eliding in comparison with TYPE_B.
3665 For example given types:
3666 vector<map<int,double>>
3668 vector<map<int,float>>
3669 then the result on PP would be:
3670 vector<map<[...],double>>
3671 with type elision, and:
3672 vector<map<int,double>>
3673 without type elision.
3675 In both cases the parts of TYPE that differ from PEER will be colorized
3676 if pp_show_color (pp) is true. In the above example, this would be
3677 "double".
3679 If INDENT is non-zero, then the types are printed in a tree-like form
3680 which shows both types. In the above example, the result on PP would be:
3682 vector<
3683 map<
3684 [...],
3685 [double != float]>>
3687 and without type-elision would be:
3689 vector<
3690 map<
3691 int,
3692 [double != float]>>
3694 As before, the differing parts of the types are colorized if
3695 pp_show_color (pp) is true ("double" and "float" in this example).
3697 Template arguments in which both types are using the default arguments
3698 are not printed; if at least one of the two types is using a non-default
3699 argument, then that argument is printed (or both arguments for the
3700 tree-like print format). */
3702 static void
3703 print_template_differences (pretty_printer *pp, tree type_a, tree type_b,
3704 bool verbose, int indent)
3706 if (indent)
3707 newline_and_indent (pp, indent);
3709 tree tinfo_a = TYPE_TEMPLATE_INFO (type_a);
3710 tree tinfo_b = TYPE_TEMPLATE_INFO (type_b);
3712 pp_printf (pp, "%s<",
3713 IDENTIFIER_POINTER (DECL_NAME (TI_TEMPLATE (tinfo_a))));
3715 tree args_a = TI_ARGS (tinfo_a);
3716 tree args_b = TI_ARGS (tinfo_b);
3717 gcc_assert (TREE_CODE (args_a) == TREE_VEC);
3718 gcc_assert (TREE_CODE (args_b) == TREE_VEC);
3719 int flags = 0;
3720 int len_a = get_non_default_template_args_count (args_a, flags);
3721 args_a = INNERMOST_TEMPLATE_ARGS (args_a);
3722 int len_b = get_non_default_template_args_count (args_b, flags);
3723 args_b = INNERMOST_TEMPLATE_ARGS (args_b);
3724 /* Determine the maximum range of args for which non-default template args
3725 were used; beyond this, only default args (if any) were used, and so
3726 they will be equal from this point onwards.
3727 One of the two peers might have used default arguments within this
3728 range, but the other will be using non-default arguments, and so
3729 it's more readable to print both within this range, to highlight
3730 the differences. */
3731 int len_max = MAX (len_a, len_b);
3732 gcc_assert (TREE_CODE (args_a) == TREE_VEC);
3733 gcc_assert (TREE_CODE (args_b) == TREE_VEC);
3734 for (int idx = 0; idx < len_max; idx++)
3736 if (idx)
3737 pp_character (pp, ',');
3739 tree arg_a = TREE_VEC_ELT (args_a, idx);
3740 tree arg_b = TREE_VEC_ELT (args_b, idx);
3741 if (arg_a == arg_b)
3743 if (indent)
3744 newline_and_indent (pp, indent + 2);
3745 /* Can do elision here, printing "[...]". */
3746 if (flag_elide_type)
3747 pp_string (pp, G_("[...]"));
3748 else
3749 pp_string (pp, arg_to_string (arg_a, verbose));
3751 else
3753 int new_indent = indent ? indent + 2 : 0;
3754 if (comparable_template_types_p (arg_a, arg_b))
3755 print_template_differences (pp, arg_a, arg_b, verbose, new_indent);
3756 else
3757 if (indent)
3759 newline_and_indent (pp, indent + 2);
3760 pp_character (pp, '[');
3761 print_nonequal_arg (pp, arg_a, verbose);
3762 pp_string (pp, " != ");
3763 print_nonequal_arg (pp, arg_b, verbose);
3764 pp_character (pp, ']');
3766 else
3767 print_nonequal_arg (pp, arg_a, verbose);
3770 pp_printf (pp, ">");
3773 /* As type_to_string, but for a template, potentially colorizing/eliding
3774 in comparison with PEER.
3775 For example, if TYPE is map<int,double> and PEER is map<int,int>,
3776 then the resulting string would be:
3777 map<[...],double>
3778 with type elision, and:
3779 map<int,double>
3780 without type elision.
3782 In both cases the parts of TYPE that differ from PEER will be colorized
3783 if SHOW_COLOR is true. In the above example, this would be "double".
3785 Template arguments in which both types are using the default arguments
3786 are not printed; if at least one of the two types is using a non-default
3787 argument, then both arguments are printed.
3789 The resulting string is in a GC-allocated buffer. */
3791 static const char *
3792 type_to_string_with_compare (tree type, tree peer, bool verbose,
3793 bool show_color)
3795 pretty_printer inner_pp;
3796 pretty_printer *pp = &inner_pp;
3797 pp_show_color (pp) = show_color;
3799 print_template_differences (pp, type, peer, verbose, 0);
3800 return pp_ggc_formatted_text (pp);
3803 /* Recursively print a tree-like comparison of TYPE_A and TYPE_B to PP,
3804 indented by INDENT spaces.
3806 For example given types:
3808 vector<map<int,double>>
3812 vector<map<double,float>>
3814 the output with type elision would be:
3816 vector<
3817 map<
3818 [...],
3819 [double != float]>>
3821 and without type-elision would be:
3823 vector<
3824 map<
3825 int,
3826 [double != float]>>
3828 TYPE_A and TYPE_B must both be comparable template types
3829 (as per comparable_template_types_p).
3831 Template arguments in which both types are using the default arguments
3832 are not printed; if at least one of the two types is using a non-default
3833 argument, then both arguments are printed. */
3835 static void
3836 print_template_tree_comparison (pretty_printer *pp, tree type_a, tree type_b,
3837 bool verbose, int indent)
3839 print_template_differences (pp, type_a, type_b, verbose, indent);
3842 /* Subroutine for use in a format_postprocessor::handle
3843 implementation. Adds a chunk to the end of
3844 formatted output, so that it will be printed
3845 by pp_output_formatted_text. */
3847 static void
3848 append_formatted_chunk (pretty_printer *pp, const char *content)
3850 output_buffer *buffer = pp_buffer (pp);
3851 struct chunk_info *chunk_array = buffer->cur_chunk_array;
3852 const char **args = chunk_array->args;
3854 unsigned int chunk_idx;
3855 for (chunk_idx = 0; args[chunk_idx]; chunk_idx++)
3857 args[chunk_idx++] = content;
3858 args[chunk_idx] = NULL;
3861 /* Create a copy of CONTENT, with quotes added, and,
3862 potentially, with colorization.
3863 No escaped is performed on CONTENT.
3864 The result is in a GC-allocated buffer. */
3866 static const char *
3867 add_quotes (const char *content, bool show_color)
3869 pretty_printer tmp_pp;
3870 pp_show_color (&tmp_pp) = show_color;
3872 /* We have to use "%<%s%>" rather than "%qs" here in order to avoid
3873 quoting colorization bytes within the results. */
3874 pp_printf (&tmp_pp, "%<%s%>", content);
3876 return pp_ggc_formatted_text (&tmp_pp);
3879 /* If we had %H and %I, and hence deferred printing them,
3880 print them now, storing the result into the chunk_info
3881 for pp_format. Quote them if 'q' was provided.
3882 Also print the difference in tree form, adding it as
3883 an additional chunk. */
3885 void
3886 cxx_format_postprocessor::handle (pretty_printer *pp)
3888 /* If we have one of %H and %I, the other should have
3889 been present. */
3890 if (m_type_a.m_tree || m_type_b.m_tree)
3892 /* Avoid reentrancy issues by working with a copy of
3893 m_type_a and m_type_b, resetting them now. */
3894 deferred_printed_type type_a = m_type_a;
3895 deferred_printed_type type_b = m_type_b;
3896 m_type_a = deferred_printed_type ();
3897 m_type_b = deferred_printed_type ();
3899 gcc_assert (type_a.m_buffer_ptr);
3900 gcc_assert (type_b.m_buffer_ptr);
3902 bool show_color = pp_show_color (pp);
3904 const char *type_a_text;
3905 const char *type_b_text;
3907 if (comparable_template_types_p (type_a.m_tree, type_b.m_tree))
3909 type_a_text
3910 = type_to_string_with_compare (type_a.m_tree, type_b.m_tree,
3911 type_a.m_verbose, show_color);
3912 type_b_text
3913 = type_to_string_with_compare (type_b.m_tree, type_a.m_tree,
3914 type_b.m_verbose, show_color);
3916 if (flag_diagnostics_show_template_tree)
3918 pretty_printer inner_pp;
3919 pp_show_color (&inner_pp) = pp_show_color (pp);
3920 print_template_tree_comparison
3921 (&inner_pp, type_a.m_tree, type_b.m_tree, type_a.m_verbose, 2);
3922 append_formatted_chunk (pp, pp_ggc_formatted_text (&inner_pp));
3925 else
3927 /* If the types were not comparable, they are printed normally,
3928 and no difference tree is printed. */
3929 type_a_text = type_to_string (type_a.m_tree, type_a.m_verbose);
3930 type_b_text = type_to_string (type_b.m_tree, type_b.m_verbose);
3933 if (type_a.m_quote)
3934 type_a_text = add_quotes (type_a_text, show_color);
3935 *type_a.m_buffer_ptr = type_a_text;
3937 if (type_b.m_quote)
3938 type_b_text = add_quotes (type_b_text, show_color);
3939 *type_b.m_buffer_ptr = type_b_text;
3943 /* Subroutine for handling %H and %I, to support i18n of messages like:
3945 error_at (loc, "could not convert %qE from %qH to %qI",
3946 expr, type_a, type_b);
3948 so that we can print things like:
3950 could not convert 'foo' from 'map<int,double>' to 'map<int,int>'
3952 and, with type-elision:
3954 could not convert 'foo' from 'map<[...],double>' to 'map<[...],int>'
3956 (with color-coding of the differences between the types).
3958 The %H and %I format codes are peers: both must be present,
3959 and they affect each other. Hence to handle them, we must
3960 delay printing until we have both, deferring the printing to
3961 pretty_printer's m_format_postprocessor hook.
3963 This is called in phase 2 of pp_format, when it is accumulating
3964 a series of formatted chunks. We stash the location of the chunk
3965 we're meant to have written to, so that we can write to it in the
3966 m_format_postprocessor hook.
3968 We also need to stash whether a 'q' prefix was provided (the QUOTE
3969 param) so that we can add the quotes when writing out the delayed
3970 chunk. */
3972 static void
3973 defer_phase_2_of_type_diff (deferred_printed_type *deferred,
3974 tree type, const char **buffer_ptr,
3975 bool verbose, bool quote)
3977 gcc_assert (deferred->m_tree == NULL_TREE);
3978 gcc_assert (deferred->m_buffer_ptr == NULL);
3979 *deferred = deferred_printed_type (type, buffer_ptr, verbose, quote);
3983 /* Called from output_format -- during diagnostic message processing --
3984 to handle C++ specific format specifier with the following meanings:
3985 %A function argument-list.
3986 %C tree code.
3987 %D declaration.
3988 %E expression.
3989 %F function declaration.
3990 %L language as used in extern "lang".
3991 %O binary operator.
3992 %P function parameter whose position is indicated by an integer.
3993 %Q assignment operator.
3994 %S substitution (template + args)
3995 %T type.
3996 %V cv-qualifier.
3997 %X exception-specification.
3998 %H type difference (from)
3999 %I type difference (to). */
4000 static bool
4001 cp_printer (pretty_printer *pp, text_info *text, const char *spec,
4002 int precision, bool wide, bool set_locus, bool verbose,
4003 bool quoted, const char **buffer_ptr)
4005 gcc_assert (pp->m_format_postprocessor);
4006 cxx_format_postprocessor *postprocessor
4007 = static_cast <cxx_format_postprocessor *> (pp->m_format_postprocessor);
4009 const char *result;
4010 tree t = NULL;
4011 #define next_tree (t = va_arg (*text->args_ptr, tree))
4012 #define next_tcode ((enum tree_code) va_arg (*text->args_ptr, int))
4013 #define next_lang ((enum languages) va_arg (*text->args_ptr, int))
4014 #define next_int va_arg (*text->args_ptr, int)
4016 if (precision != 0 || wide)
4017 return false;
4019 switch (*spec)
4021 case 'A': result = args_to_string (next_tree, verbose); break;
4022 case 'C': result = code_to_string (next_tcode); break;
4023 case 'D':
4025 tree temp = next_tree;
4026 if (VAR_P (temp)
4027 && DECL_HAS_DEBUG_EXPR_P (temp))
4029 temp = DECL_DEBUG_EXPR (temp);
4030 if (!DECL_P (temp))
4032 result = expr_to_string (temp);
4033 break;
4036 result = decl_to_string (temp, verbose);
4038 break;
4039 case 'E': result = expr_to_string (next_tree); break;
4040 case 'F': result = fndecl_to_string (next_tree, verbose); break;
4041 case 'L': result = language_to_string (next_lang); break;
4042 case 'O': result = op_to_string (false, next_tcode); break;
4043 case 'P': result = parm_to_string (next_int); break;
4044 case 'Q': result = op_to_string (true, next_tcode); break;
4045 case 'S': result = subst_to_string (next_tree); break;
4046 case 'T': result = type_to_string (next_tree, verbose); break;
4047 case 'V': result = cv_to_string (next_tree, verbose); break;
4048 case 'X': result = eh_spec_to_string (next_tree, verbose); break;
4050 case 'G':
4051 percent_G_format (text);
4052 return true;
4054 case 'K':
4055 t = va_arg (*text->args_ptr, tree);
4056 percent_K_format (text, t);
4057 return true;
4059 case 'H':
4061 defer_phase_2_of_type_diff (&postprocessor->m_type_a, next_tree,
4062 buffer_ptr, verbose, quoted);
4063 return true;
4066 case 'I':
4068 defer_phase_2_of_type_diff (&postprocessor->m_type_b, next_tree,
4069 buffer_ptr, verbose, quoted);
4070 return true;
4073 default:
4074 return false;
4077 pp_string (pp, result);
4078 if (set_locus && t != NULL)
4079 text->set_location (0, location_of (t), true);
4080 return true;
4081 #undef next_tree
4082 #undef next_tcode
4083 #undef next_lang
4084 #undef next_int
4087 /* Warn about the use of C++0x features when appropriate. */
4088 void
4089 maybe_warn_cpp0x (cpp0x_warn_str str)
4091 if ((cxx_dialect == cxx98) && !in_system_header_at (input_location))
4092 /* We really want to suppress this warning in system headers,
4093 because libstdc++ uses variadic templates even when we aren't
4094 in C++0x mode. */
4095 switch (str)
4097 case CPP0X_INITIALIZER_LISTS:
4098 pedwarn (input_location, 0,
4099 "extended initializer lists "
4100 "only available with -std=c++11 or -std=gnu++11");
4101 break;
4102 case CPP0X_EXPLICIT_CONVERSION:
4103 pedwarn (input_location, 0,
4104 "explicit conversion operators "
4105 "only available with -std=c++11 or -std=gnu++11");
4106 break;
4107 case CPP0X_VARIADIC_TEMPLATES:
4108 pedwarn (input_location, 0,
4109 "variadic templates "
4110 "only available with -std=c++11 or -std=gnu++11");
4111 break;
4112 case CPP0X_LAMBDA_EXPR:
4113 pedwarn (input_location, 0,
4114 "lambda expressions "
4115 "only available with -std=c++11 or -std=gnu++11");
4116 break;
4117 case CPP0X_AUTO:
4118 pedwarn (input_location, 0,
4119 "C++11 auto only available with -std=c++11 or -std=gnu++11");
4120 break;
4121 case CPP0X_SCOPED_ENUMS:
4122 pedwarn (input_location, 0,
4123 "scoped enums only available with -std=c++11 or -std=gnu++11");
4124 break;
4125 case CPP0X_DEFAULTED_DELETED:
4126 pedwarn (input_location, 0,
4127 "defaulted and deleted functions "
4128 "only available with -std=c++11 or -std=gnu++11");
4129 break;
4130 case CPP0X_INLINE_NAMESPACES:
4131 pedwarn (input_location, OPT_Wpedantic,
4132 "inline namespaces "
4133 "only available with -std=c++11 or -std=gnu++11");
4134 break;
4135 case CPP0X_OVERRIDE_CONTROLS:
4136 pedwarn (input_location, 0,
4137 "override controls (override/final) "
4138 "only available with -std=c++11 or -std=gnu++11");
4139 break;
4140 case CPP0X_NSDMI:
4141 pedwarn (input_location, 0,
4142 "non-static data member initializers "
4143 "only available with -std=c++11 or -std=gnu++11");
4144 break;
4145 case CPP0X_USER_DEFINED_LITERALS:
4146 pedwarn (input_location, 0,
4147 "user-defined literals "
4148 "only available with -std=c++11 or -std=gnu++11");
4149 break;
4150 case CPP0X_DELEGATING_CTORS:
4151 pedwarn (input_location, 0,
4152 "delegating constructors "
4153 "only available with -std=c++11 or -std=gnu++11");
4154 break;
4155 case CPP0X_INHERITING_CTORS:
4156 pedwarn (input_location, 0,
4157 "inheriting constructors "
4158 "only available with -std=c++11 or -std=gnu++11");
4159 break;
4160 case CPP0X_ATTRIBUTES:
4161 pedwarn (input_location, 0,
4162 "c++11 attributes "
4163 "only available with -std=c++11 or -std=gnu++11");
4164 break;
4165 case CPP0X_REF_QUALIFIER:
4166 pedwarn (input_location, 0,
4167 "ref-qualifiers "
4168 "only available with -std=c++11 or -std=gnu++11");
4169 break;
4170 default:
4171 gcc_unreachable ();
4175 /* Warn about the use of variadic templates when appropriate. */
4176 void
4177 maybe_warn_variadic_templates (void)
4179 maybe_warn_cpp0x (CPP0X_VARIADIC_TEMPLATES);
4183 /* Issue an ISO C++98 pedantic warning at LOCATION, conditional on
4184 option OPT with text GMSGID. Use this function to report
4185 diagnostics for constructs that are invalid C++98, but valid
4186 C++0x. */
4187 bool
4188 pedwarn_cxx98 (location_t location, int opt, const char *gmsgid, ...)
4190 diagnostic_info diagnostic;
4191 va_list ap;
4192 bool ret;
4193 rich_location richloc (line_table, location);
4195 va_start (ap, gmsgid);
4196 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
4197 (cxx_dialect == cxx98) ? DK_PEDWARN : DK_WARNING);
4198 diagnostic.option_index = opt;
4199 ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
4200 va_end (ap);
4201 return ret;
4204 /* Issue a diagnostic that NAME cannot be found in SCOPE. DECL is what
4205 we found when we tried to do the lookup. LOCATION is the location of
4206 the NAME identifier. */
4208 void
4209 qualified_name_lookup_error (tree scope, tree name,
4210 tree decl, location_t location)
4212 if (scope == error_mark_node)
4213 ; /* We already complained. */
4214 else if (TYPE_P (scope))
4216 if (!COMPLETE_TYPE_P (scope))
4217 error_at (location, "incomplete type %qT used in nested name specifier",
4218 scope);
4219 else if (TREE_CODE (decl) == TREE_LIST)
4221 error_at (location, "reference to %<%T::%D%> is ambiguous",
4222 scope, name);
4223 print_candidates (decl);
4225 else
4226 error_at (location, "%qD is not a member of %qT", name, scope);
4228 else if (scope != global_namespace)
4230 error_at (location, "%qD is not a member of %qD", name, scope);
4231 if (!suggest_alternative_in_explicit_scope (location, name, scope))
4232 suggest_alternatives_for (location, name, false);
4234 else
4236 error_at (location, "%<::%D%> has not been declared", name);
4237 suggest_alternatives_for (location, name, true);