diagnostic.c (diagnostic_set_caret_max_width): Use pp_buffer.
[official-gcc.git] / gcc / cp / error.c
blobc82a0cec766423c0f619cd6cb2eb01fec92e53a2
1 /* Call-backs for C++ error reporting.
2 This code is non-reentrant.
3 Copyright (C) 1993-2013 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 "tm.h"
24 #include "tree.h"
25 #include "cp-tree.h"
26 #include "flags.h"
27 #include "diagnostic.h"
28 #include "tree-diagnostic.h"
29 #include "langhooks-def.h"
30 #include "intl.h"
31 #include "cxx-pretty-print.h"
32 #include "tree-pretty-print.h"
33 #include "pointer-set.h"
34 #include "c-family/c-objc.h"
36 #include <new> // For placement-new.
38 #define pp_separate_with_comma(PP) pp_cxx_separate_with (PP, ',')
39 #define pp_separate_with_semicolon(PP) pp_cxx_separate_with (PP, ';')
41 /* The global buffer where we dump everything. It is there only for
42 transitional purpose. It is expected, in the near future, to be
43 completely removed. */
44 static cxx_pretty_printer scratch_pretty_printer;
45 #define cxx_pp (&scratch_pretty_printer)
47 /* Translate if being used for diagnostics, but not for dump files or
48 __PRETTY_FUNCTION. */
49 #define M_(msgid) (pp_translate_identifiers (cxx_pp) ? _(msgid) : (msgid))
51 # define NEXT_CODE(T) (TREE_CODE (TREE_TYPE (T)))
53 static const char *args_to_string (tree, int);
54 static const char *assop_to_string (enum tree_code);
55 static const char *code_to_string (enum tree_code);
56 static const char *cv_to_string (tree, int);
57 static const char *decl_to_string (tree, int);
58 static const char *expr_to_string (tree);
59 static const char *fndecl_to_string (tree, int);
60 static const char *op_to_string (enum tree_code);
61 static const char *parm_to_string (int);
62 static const char *type_to_string (tree, int);
64 static void dump_alias_template_specialization (cxx_pretty_printer *, tree, int);
65 static void dump_type (cxx_pretty_printer *, tree, int);
66 static void dump_typename (cxx_pretty_printer *, tree, int);
67 static void dump_simple_decl (cxx_pretty_printer *, tree, tree, int);
68 static void dump_decl (cxx_pretty_printer *, tree, int);
69 static void dump_template_decl (cxx_pretty_printer *, tree, int);
70 static void dump_function_decl (cxx_pretty_printer *, tree, int);
71 static void dump_expr (cxx_pretty_printer *, tree, int);
72 static void dump_unary_op (cxx_pretty_printer *, const char *, tree, int);
73 static void dump_binary_op (cxx_pretty_printer *, const char *, tree, int);
74 static void dump_aggr_type (cxx_pretty_printer *, tree, int);
75 static void dump_type_prefix (cxx_pretty_printer *, tree, int);
76 static void dump_type_suffix (cxx_pretty_printer *, tree, int);
77 static void dump_function_name (cxx_pretty_printer *, tree, int);
78 static void dump_call_expr_args (cxx_pretty_printer *, tree, int, bool);
79 static void dump_aggr_init_expr_args (cxx_pretty_printer *, tree, int, bool);
80 static void dump_expr_list (cxx_pretty_printer *, tree, int);
81 static void dump_global_iord (cxx_pretty_printer *, tree);
82 static void dump_parameters (cxx_pretty_printer *, tree, int);
83 static void dump_ref_qualifier (cxx_pretty_printer *, tree, int);
84 static void dump_exception_spec (cxx_pretty_printer *, tree, int);
85 static void dump_template_argument (cxx_pretty_printer *, tree, int);
86 static void dump_template_argument_list (cxx_pretty_printer *, tree, int);
87 static void dump_template_parameter (cxx_pretty_printer *, tree, int);
88 static void dump_template_bindings (cxx_pretty_printer *, tree, tree,
89 vec<tree, va_gc> *);
90 static void dump_scope (cxx_pretty_printer *, tree, int);
91 static void dump_template_parms (cxx_pretty_printer *, tree, int, int);
92 static int get_non_default_template_args_count (tree, int);
93 static const char *function_category (tree);
94 static void maybe_print_constexpr_context (diagnostic_context *);
95 static void maybe_print_instantiation_context (diagnostic_context *);
96 static void print_instantiation_full_context (diagnostic_context *);
97 static void print_instantiation_partial_context (diagnostic_context *,
98 struct tinst_level *,
99 location_t);
100 static void cp_diagnostic_starter (diagnostic_context *, diagnostic_info *);
101 static void cp_diagnostic_finalizer (diagnostic_context *, diagnostic_info *);
102 static void cp_print_error_function (diagnostic_context *, diagnostic_info *);
104 static bool cp_printer (pretty_printer *, text_info *, const char *,
105 int, bool, bool, bool);
107 void
108 init_error (void)
110 diagnostic_starter (global_dc) = cp_diagnostic_starter;
111 diagnostic_finalizer (global_dc) = cp_diagnostic_finalizer;
112 diagnostic_format_decoder (global_dc) = cp_printer;
114 new (cxx_pp) cxx_pretty_printer ();
117 /* Dump a scope, if deemed necessary. */
119 static void
120 dump_scope (cxx_pretty_printer *pp, tree scope, int flags)
122 int f = flags & (TFF_SCOPE | TFF_CHASE_TYPEDEF);
124 if (scope == NULL_TREE)
125 return;
127 if (TREE_CODE (scope) == NAMESPACE_DECL)
129 if (scope != global_namespace)
131 dump_decl (pp, scope, f);
132 pp_cxx_colon_colon (pp);
135 else if (AGGREGATE_TYPE_P (scope))
137 dump_type (pp, scope, f);
138 pp_cxx_colon_colon (pp);
140 else if ((flags & TFF_SCOPE) && TREE_CODE (scope) == FUNCTION_DECL)
142 dump_function_decl (pp, scope, f);
143 pp_cxx_colon_colon (pp);
147 /* Dump the template ARGument under control of FLAGS. */
149 static void
150 dump_template_argument (cxx_pretty_printer *pp, tree arg, int flags)
152 if (ARGUMENT_PACK_P (arg))
153 dump_template_argument_list (pp, ARGUMENT_PACK_ARGS (arg),
154 /* No default args in argument packs. */
155 flags|TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS);
156 else if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
157 dump_type (pp, arg, flags & ~TFF_CLASS_KEY_OR_ENUM);
158 else
160 if (TREE_CODE (arg) == TREE_LIST)
161 arg = TREE_VALUE (arg);
163 dump_expr (pp, arg, (flags | TFF_EXPR_IN_PARENS) & ~TFF_CLASS_KEY_OR_ENUM);
167 /* Count the number of template arguments ARGS whose value does not
168 match the (optional) default template parameter in PARAMS */
170 static int
171 get_non_default_template_args_count (tree args, int flags)
173 int n = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_ARGS (args));
175 if (/* We use this flag when generating debug information. We don't
176 want to expand templates at this point, for this may generate
177 new decls, which gets decl counts out of sync, which may in
178 turn cause codegen differences between compilations with and
179 without -g. */
180 (flags & TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS) != 0
181 || !flag_pretty_templates)
182 return n;
184 return GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (INNERMOST_TEMPLATE_ARGS (args));
187 /* Dump a template-argument-list ARGS (always a TREE_VEC) under control
188 of FLAGS. */
190 static void
191 dump_template_argument_list (cxx_pretty_printer *pp, tree args, int flags)
193 int n = get_non_default_template_args_count (args, flags);
194 int need_comma = 0;
195 int i;
197 for (i = 0; i < n; ++i)
199 tree arg = TREE_VEC_ELT (args, i);
201 /* Only print a comma if we know there is an argument coming. In
202 the case of an empty template argument pack, no actual
203 argument will be printed. */
204 if (need_comma
205 && (!ARGUMENT_PACK_P (arg)
206 || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) > 0))
207 pp_separate_with_comma (pp);
209 dump_template_argument (pp, arg, flags);
210 need_comma = 1;
214 /* Dump a template parameter PARM (a TREE_LIST) under control of FLAGS. */
216 static void
217 dump_template_parameter (cxx_pretty_printer *pp, tree parm, int flags)
219 tree p;
220 tree a;
222 if (parm == error_mark_node)
223 return;
225 p = TREE_VALUE (parm);
226 a = TREE_PURPOSE (parm);
228 if (TREE_CODE (p) == TYPE_DECL)
230 if (flags & TFF_DECL_SPECIFIERS)
232 pp_cxx_ws_string (pp, "class");
233 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (p)))
234 pp_cxx_ws_string (pp, "...");
235 if (DECL_NAME (p))
236 pp_cxx_tree_identifier (pp, DECL_NAME (p));
238 else if (DECL_NAME (p))
239 pp_cxx_tree_identifier (pp, DECL_NAME (p));
240 else
241 pp_cxx_canonical_template_parameter (pp, TREE_TYPE (p));
243 else
244 dump_decl (pp, p, flags | TFF_DECL_SPECIFIERS);
246 if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && a != NULL_TREE)
248 pp_cxx_whitespace (pp);
249 pp_equal (pp);
250 pp_cxx_whitespace (pp);
251 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
252 dump_type (pp, a, flags & ~TFF_CHASE_TYPEDEF);
253 else
254 dump_expr (pp, a, flags | TFF_EXPR_IN_PARENS);
258 /* Dump, under control of FLAGS, a template-parameter-list binding.
259 PARMS is a TREE_LIST of TREE_VEC of TREE_LIST and ARGS is a
260 TREE_VEC. */
262 static void
263 dump_template_bindings (cxx_pretty_printer *pp, tree parms, tree args,
264 vec<tree, va_gc> *typenames)
266 bool need_semicolon = false;
267 int i;
268 tree t;
270 while (parms)
272 tree p = TREE_VALUE (parms);
273 int lvl = TMPL_PARMS_DEPTH (parms);
274 int arg_idx = 0;
275 int i;
276 tree lvl_args = NULL_TREE;
278 /* Don't crash if we had an invalid argument list. */
279 if (TMPL_ARGS_DEPTH (args) >= lvl)
280 lvl_args = TMPL_ARGS_LEVEL (args, lvl);
282 for (i = 0; i < TREE_VEC_LENGTH (p); ++i)
284 tree arg = NULL_TREE;
286 /* Don't crash if we had an invalid argument list. */
287 if (lvl_args && NUM_TMPL_ARGS (lvl_args) > arg_idx)
288 arg = TREE_VEC_ELT (lvl_args, arg_idx);
290 if (need_semicolon)
291 pp_separate_with_semicolon (pp);
292 dump_template_parameter (pp, TREE_VEC_ELT (p, i),
293 TFF_PLAIN_IDENTIFIER);
294 pp_cxx_whitespace (pp);
295 pp_equal (pp);
296 pp_cxx_whitespace (pp);
297 if (arg)
299 if (ARGUMENT_PACK_P (arg))
300 pp_cxx_left_brace (pp);
301 dump_template_argument (pp, arg, TFF_PLAIN_IDENTIFIER);
302 if (ARGUMENT_PACK_P (arg))
303 pp_cxx_right_brace (pp);
305 else
306 pp_string (pp, M_("<missing>"));
308 ++arg_idx;
309 need_semicolon = true;
312 parms = TREE_CHAIN (parms);
315 /* Don't bother with typenames for a partial instantiation. */
316 if (vec_safe_is_empty (typenames) || uses_template_parms (args))
317 return;
319 FOR_EACH_VEC_SAFE_ELT (typenames, i, t)
321 if (need_semicolon)
322 pp_separate_with_semicolon (pp);
323 dump_type (pp, t, TFF_PLAIN_IDENTIFIER);
324 pp_cxx_whitespace (pp);
325 pp_equal (pp);
326 pp_cxx_whitespace (pp);
327 push_deferring_access_checks (dk_no_check);
328 t = tsubst (t, args, tf_none, NULL_TREE);
329 pop_deferring_access_checks ();
330 /* Strip typedefs. We can't just use TFF_CHASE_TYPEDEF because
331 pp_simple_type_specifier doesn't know about it. */
332 t = strip_typedefs (t);
333 dump_type (pp, t, TFF_PLAIN_IDENTIFIER);
337 /* Dump a human-readable equivalent of the alias template
338 specialization of T. */
340 static void
341 dump_alias_template_specialization (cxx_pretty_printer *pp, tree t, int flags)
343 tree name;
345 gcc_assert (alias_template_specialization_p (t));
347 if (!(flags & TFF_UNQUALIFIED_NAME))
348 dump_scope (pp, CP_DECL_CONTEXT (TYPE_NAME (t)), flags);
349 name = TYPE_IDENTIFIER (t);
350 pp_cxx_tree_identifier (pp, name);
351 dump_template_parms (pp, TYPE_TEMPLATE_INFO (t),
352 /*primary=*/false,
353 flags & ~TFF_TEMPLATE_HEADER);
356 /* Dump a human-readable equivalent of TYPE. FLAGS controls the
357 format. */
359 static void
360 dump_type (cxx_pretty_printer *pp, tree t, int flags)
362 if (t == NULL_TREE)
363 return;
365 /* Don't print e.g. "struct mytypedef". */
366 if (TYPE_P (t) && typedef_variant_p (t))
368 tree decl = TYPE_NAME (t);
369 if ((flags & TFF_CHASE_TYPEDEF)
370 || DECL_SELF_REFERENCE_P (decl)
371 || (!flag_pretty_templates
372 && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)))
373 t = strip_typedefs (t);
374 else if (alias_template_specialization_p (t))
376 dump_alias_template_specialization (pp, t, flags);
377 return;
379 else if (same_type_p (t, TREE_TYPE (decl)))
380 t = decl;
381 else
383 pp_cxx_cv_qualifier_seq (pp, t);
384 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
385 return;
389 if (TYPE_PTRMEMFUNC_P (t))
390 goto offset_type;
392 switch (TREE_CODE (t))
394 case LANG_TYPE:
395 if (t == init_list_type_node)
396 pp_string (pp, M_("<brace-enclosed initializer list>"));
397 else if (t == unknown_type_node)
398 pp_string (pp, M_("<unresolved overloaded function type>"));
399 else
401 pp_cxx_cv_qualifier_seq (pp, t);
402 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
404 break;
406 case TREE_LIST:
407 /* A list of function parms. */
408 dump_parameters (pp, t, flags);
409 break;
411 case IDENTIFIER_NODE:
412 pp_cxx_tree_identifier (pp, t);
413 break;
415 case TREE_BINFO:
416 dump_type (pp, BINFO_TYPE (t), flags);
417 break;
419 case RECORD_TYPE:
420 case UNION_TYPE:
421 case ENUMERAL_TYPE:
422 dump_aggr_type (pp, t, flags);
423 break;
425 case TYPE_DECL:
426 if (flags & TFF_CHASE_TYPEDEF)
428 dump_type (pp, DECL_ORIGINAL_TYPE (t)
429 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t), flags);
430 break;
432 /* Else fall through. */
434 case TEMPLATE_DECL:
435 case NAMESPACE_DECL:
436 dump_decl (pp, t, flags & ~TFF_DECL_SPECIFIERS);
437 break;
439 case INTEGER_TYPE:
440 case REAL_TYPE:
441 case VOID_TYPE:
442 case BOOLEAN_TYPE:
443 case COMPLEX_TYPE:
444 case VECTOR_TYPE:
445 case FIXED_POINT_TYPE:
446 pp_type_specifier_seq (pp, t);
447 break;
449 case TEMPLATE_TEMPLATE_PARM:
450 /* For parameters inside template signature. */
451 if (TYPE_IDENTIFIER (t))
452 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
453 else
454 pp_cxx_canonical_template_parameter (pp, t);
455 break;
457 case BOUND_TEMPLATE_TEMPLATE_PARM:
459 tree args = TYPE_TI_ARGS (t);
460 pp_cxx_cv_qualifier_seq (pp, t);
461 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
462 pp_cxx_begin_template_argument_list (pp);
463 dump_template_argument_list (pp, args, flags);
464 pp_cxx_end_template_argument_list (pp);
466 break;
468 case TEMPLATE_TYPE_PARM:
469 pp_cxx_cv_qualifier_seq (pp, t);
470 if (TYPE_IDENTIFIER (t))
471 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
472 else
473 pp_cxx_canonical_template_parameter
474 (pp, TEMPLATE_TYPE_PARM_INDEX (t));
475 break;
477 /* This is not always necessary for pointers and such, but doing this
478 reduces code size. */
479 case ARRAY_TYPE:
480 case POINTER_TYPE:
481 case REFERENCE_TYPE:
482 case OFFSET_TYPE:
483 offset_type:
484 case FUNCTION_TYPE:
485 case METHOD_TYPE:
487 dump_type_prefix (pp, t, flags);
488 dump_type_suffix (pp, t, flags);
489 break;
491 case TYPENAME_TYPE:
492 if (! (flags & TFF_CHASE_TYPEDEF)
493 && DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
495 dump_decl (pp, TYPE_NAME (t), TFF_PLAIN_IDENTIFIER);
496 break;
498 pp_cxx_cv_qualifier_seq (pp, t);
499 pp_cxx_ws_string (pp,
500 TYPENAME_IS_ENUM_P (t) ? "enum"
501 : TYPENAME_IS_CLASS_P (t) ? "class"
502 : "typename");
503 dump_typename (pp, t, flags);
504 break;
506 case UNBOUND_CLASS_TEMPLATE:
507 if (! (flags & TFF_UNQUALIFIED_NAME))
509 dump_type (pp, TYPE_CONTEXT (t), flags);
510 pp_cxx_colon_colon (pp);
512 pp_cxx_ws_string (pp, "template");
513 dump_type (pp, DECL_NAME (TYPE_NAME (t)), flags);
514 break;
516 case TYPEOF_TYPE:
517 pp_cxx_ws_string (pp, "__typeof__");
518 pp_cxx_whitespace (pp);
519 pp_cxx_left_paren (pp);
520 dump_expr (pp, TYPEOF_TYPE_EXPR (t), flags & ~TFF_EXPR_IN_PARENS);
521 pp_cxx_right_paren (pp);
522 break;
524 case UNDERLYING_TYPE:
525 pp_cxx_ws_string (pp, "__underlying_type");
526 pp_cxx_whitespace (pp);
527 pp_cxx_left_paren (pp);
528 dump_expr (pp, UNDERLYING_TYPE_TYPE (t), flags & ~TFF_EXPR_IN_PARENS);
529 pp_cxx_right_paren (pp);
530 break;
532 case TYPE_PACK_EXPANSION:
533 dump_type (pp, PACK_EXPANSION_PATTERN (t), flags);
534 pp_cxx_ws_string (pp, "...");
535 break;
537 case TYPE_ARGUMENT_PACK:
538 dump_template_argument (pp, t, flags);
539 break;
541 case DECLTYPE_TYPE:
542 pp_cxx_ws_string (pp, "decltype");
543 pp_cxx_whitespace (pp);
544 pp_cxx_left_paren (pp);
545 dump_expr (pp, DECLTYPE_TYPE_EXPR (t), flags & ~TFF_EXPR_IN_PARENS);
546 pp_cxx_right_paren (pp);
547 break;
549 case NULLPTR_TYPE:
550 pp_string (pp, "std::nullptr_t");
551 break;
553 default:
554 pp_unsupported_tree (pp, t);
555 /* Fall through to error. */
557 case ERROR_MARK:
558 pp_string (pp, M_("<type error>"));
559 break;
563 /* Dump a TYPENAME_TYPE. We need to notice when the context is itself
564 a TYPENAME_TYPE. */
566 static void
567 dump_typename (cxx_pretty_printer *pp, tree t, int flags)
569 tree ctx = TYPE_CONTEXT (t);
571 if (TREE_CODE (ctx) == TYPENAME_TYPE)
572 dump_typename (pp, ctx, flags);
573 else
574 dump_type (pp, ctx, flags & ~TFF_CLASS_KEY_OR_ENUM);
575 pp_cxx_colon_colon (pp);
576 dump_decl (pp, TYPENAME_TYPE_FULLNAME (t), flags);
579 /* Return the name of the supplied aggregate, or enumeral type. */
581 const char *
582 class_key_or_enum_as_string (tree t)
584 if (TREE_CODE (t) == ENUMERAL_TYPE)
586 if (SCOPED_ENUM_P (t))
587 return "enum class";
588 else
589 return "enum";
591 else if (TREE_CODE (t) == UNION_TYPE)
592 return "union";
593 else if (TYPE_LANG_SPECIFIC (t) && CLASSTYPE_DECLARED_CLASS (t))
594 return "class";
595 else
596 return "struct";
599 /* Print out a class declaration T under the control of FLAGS,
600 in the form `class foo'. */
602 static void
603 dump_aggr_type (cxx_pretty_printer *pp, tree t, int flags)
605 tree name;
606 const char *variety = class_key_or_enum_as_string (t);
607 int typdef = 0;
608 int tmplate = 0;
610 pp_cxx_cv_qualifier_seq (pp, t);
612 if (flags & TFF_CLASS_KEY_OR_ENUM)
613 pp_cxx_ws_string (pp, variety);
615 name = TYPE_NAME (t);
617 if (name)
619 typdef = (!DECL_ARTIFICIAL (name)
620 /* An alias specialization is not considered to be a
621 typedef. */
622 && !alias_template_specialization_p (t));
624 if ((typdef
625 && ((flags & TFF_CHASE_TYPEDEF)
626 || (!flag_pretty_templates && DECL_LANG_SPECIFIC (name)
627 && DECL_TEMPLATE_INFO (name))))
628 || DECL_SELF_REFERENCE_P (name))
630 t = TYPE_MAIN_VARIANT (t);
631 name = TYPE_NAME (t);
632 typdef = 0;
635 tmplate = !typdef && TREE_CODE (t) != ENUMERAL_TYPE
636 && TYPE_LANG_SPECIFIC (t) && CLASSTYPE_TEMPLATE_INFO (t)
637 && (TREE_CODE (CLASSTYPE_TI_TEMPLATE (t)) != TEMPLATE_DECL
638 || PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
640 if (! (flags & TFF_UNQUALIFIED_NAME))
641 dump_scope (pp, CP_DECL_CONTEXT (name), flags | TFF_SCOPE);
642 flags &= ~TFF_UNQUALIFIED_NAME;
643 if (tmplate)
645 /* Because the template names are mangled, we have to locate
646 the most general template, and use that name. */
647 tree tpl = TYPE_TI_TEMPLATE (t);
649 while (DECL_TEMPLATE_INFO (tpl))
650 tpl = DECL_TI_TEMPLATE (tpl);
651 name = tpl;
653 name = DECL_NAME (name);
656 if (name == 0 || ANON_AGGRNAME_P (name))
658 if (flags & TFF_CLASS_KEY_OR_ENUM)
659 pp_string (pp, M_("<anonymous>"));
660 else
661 pp_printf (pp, M_("<anonymous %s>"), variety);
663 else if (LAMBDA_TYPE_P (t))
665 /* A lambda's "type" is essentially its signature. */
666 pp_string (pp, M_("<lambda"));
667 if (lambda_function (t))
668 dump_parameters (pp,
669 FUNCTION_FIRST_USER_PARMTYPE (lambda_function (t)),
670 flags);
671 pp_greater (pp);
673 else
674 pp_cxx_tree_identifier (pp, name);
675 if (tmplate)
676 dump_template_parms (pp, TYPE_TEMPLATE_INFO (t),
677 !CLASSTYPE_USE_TEMPLATE (t),
678 flags & ~TFF_TEMPLATE_HEADER);
681 /* Dump into the obstack the initial part of the output for a given type.
682 This is necessary when dealing with things like functions returning
683 functions. Examples:
685 return type of `int (* fee ())()': pointer -> function -> int. Both
686 pointer (and reference and offset) and function (and member) types must
687 deal with prefix and suffix.
689 Arrays must also do this for DECL nodes, like int a[], and for things like
690 int *[]&. */
692 static void
693 dump_type_prefix (cxx_pretty_printer *pp, tree t, int flags)
695 if (TYPE_PTRMEMFUNC_P (t))
697 t = TYPE_PTRMEMFUNC_FN_TYPE (t);
698 goto offset_type;
701 switch (TREE_CODE (t))
703 case POINTER_TYPE:
704 case REFERENCE_TYPE:
706 tree sub = TREE_TYPE (t);
708 dump_type_prefix (pp, sub, flags);
709 if (TREE_CODE (sub) == ARRAY_TYPE
710 || TREE_CODE (sub) == FUNCTION_TYPE)
712 pp_cxx_whitespace (pp);
713 pp_cxx_left_paren (pp);
714 pp_c_attributes_display (pp, TYPE_ATTRIBUTES (sub));
716 if (TYPE_PTR_P (t))
717 pp_star (pp);
718 else if (TREE_CODE (t) == REFERENCE_TYPE)
720 if (TYPE_REF_IS_RVALUE (t))
721 pp_ampersand_ampersand (pp);
722 else
723 pp_ampersand (pp);
725 pp->padding = pp_before;
726 pp_cxx_cv_qualifier_seq (pp, t);
728 break;
730 case OFFSET_TYPE:
731 offset_type:
732 dump_type_prefix (pp, TREE_TYPE (t), flags);
733 if (TREE_CODE (t) == OFFSET_TYPE) /* pmfs deal with this in d_t_p */
735 pp_maybe_space (pp);
736 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
737 pp_cxx_left_paren (pp);
738 dump_type (pp, TYPE_OFFSET_BASETYPE (t), flags);
739 pp_cxx_colon_colon (pp);
741 pp_cxx_star (pp);
742 pp_cxx_cv_qualifier_seq (pp, t);
743 pp->padding = pp_before;
744 break;
746 /* This can be reached without a pointer when dealing with
747 templates, e.g. std::is_function. */
748 case FUNCTION_TYPE:
749 dump_type_prefix (pp, TREE_TYPE (t), flags);
750 break;
752 case METHOD_TYPE:
753 dump_type_prefix (pp, TREE_TYPE (t), flags);
754 pp_maybe_space (pp);
755 pp_cxx_left_paren (pp);
756 dump_aggr_type (pp, TYPE_METHOD_BASETYPE (t), flags);
757 pp_cxx_colon_colon (pp);
758 break;
760 case ARRAY_TYPE:
761 dump_type_prefix (pp, TREE_TYPE (t), flags);
762 break;
764 case ENUMERAL_TYPE:
765 case IDENTIFIER_NODE:
766 case INTEGER_TYPE:
767 case BOOLEAN_TYPE:
768 case REAL_TYPE:
769 case RECORD_TYPE:
770 case TEMPLATE_TYPE_PARM:
771 case TEMPLATE_TEMPLATE_PARM:
772 case BOUND_TEMPLATE_TEMPLATE_PARM:
773 case TREE_LIST:
774 case TYPE_DECL:
775 case TREE_VEC:
776 case UNION_TYPE:
777 case LANG_TYPE:
778 case VOID_TYPE:
779 case TYPENAME_TYPE:
780 case COMPLEX_TYPE:
781 case VECTOR_TYPE:
782 case TYPEOF_TYPE:
783 case UNDERLYING_TYPE:
784 case DECLTYPE_TYPE:
785 case TYPE_PACK_EXPANSION:
786 case FIXED_POINT_TYPE:
787 case NULLPTR_TYPE:
788 dump_type (pp, t, flags);
789 pp->padding = pp_before;
790 break;
792 default:
793 pp_unsupported_tree (pp, t);
794 /* fall through. */
795 case ERROR_MARK:
796 pp_string (pp, M_("<typeprefixerror>"));
797 break;
801 /* Dump the suffix of type T, under control of FLAGS. This is the part
802 which appears after the identifier (or function parms). */
804 static void
805 dump_type_suffix (cxx_pretty_printer *pp, tree t, int flags)
807 if (TYPE_PTRMEMFUNC_P (t))
808 t = TYPE_PTRMEMFUNC_FN_TYPE (t);
810 switch (TREE_CODE (t))
812 case POINTER_TYPE:
813 case REFERENCE_TYPE:
814 case OFFSET_TYPE:
815 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
816 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
817 pp_cxx_right_paren (pp);
818 dump_type_suffix (pp, TREE_TYPE (t), flags);
819 break;
821 case FUNCTION_TYPE:
822 case METHOD_TYPE:
824 tree arg;
825 if (TREE_CODE (t) == METHOD_TYPE)
826 /* Can only be reached through a pointer. */
827 pp_cxx_right_paren (pp);
828 arg = TYPE_ARG_TYPES (t);
829 if (TREE_CODE (t) == METHOD_TYPE)
830 arg = TREE_CHAIN (arg);
832 /* Function pointers don't have default args. Not in standard C++,
833 anyway; they may in g++, but we'll just pretend otherwise. */
834 dump_parameters (pp, arg, flags & ~TFF_FUNCTION_DEFAULT_ARGUMENTS);
836 pp->padding = pp_before;
837 pp_cxx_cv_qualifiers (pp, type_memfn_quals (t));
838 dump_ref_qualifier (pp, t, flags);
839 dump_exception_spec (pp, TYPE_RAISES_EXCEPTIONS (t), flags);
840 dump_type_suffix (pp, TREE_TYPE (t), flags);
841 break;
844 case ARRAY_TYPE:
845 pp_maybe_space (pp);
846 pp_cxx_left_bracket (pp);
847 if (TYPE_DOMAIN (t))
849 tree dtype = TYPE_DOMAIN (t);
850 tree max = TYPE_MAX_VALUE (dtype);
851 if (integer_all_onesp (max))
852 pp_character (pp, '0');
853 else if (host_integerp (max, 0))
854 pp_wide_integer (pp, tree_low_cst (max, 0) + 1);
855 else
857 STRIP_NOPS (max);
858 if (TREE_CODE (max) == SAVE_EXPR)
859 max = TREE_OPERAND (max, 0);
860 if (TREE_CODE (max) == MINUS_EXPR
861 || TREE_CODE (max) == PLUS_EXPR)
863 max = TREE_OPERAND (max, 0);
864 while (CONVERT_EXPR_P (max))
865 max = TREE_OPERAND (max, 0);
867 else
868 max = fold_build2_loc (input_location,
869 PLUS_EXPR, dtype, max,
870 build_int_cst (dtype, 1));
871 dump_expr (pp, max, flags & ~TFF_EXPR_IN_PARENS);
874 pp_cxx_right_bracket (pp);
875 dump_type_suffix (pp, TREE_TYPE (t), flags);
876 break;
878 case ENUMERAL_TYPE:
879 case IDENTIFIER_NODE:
880 case INTEGER_TYPE:
881 case BOOLEAN_TYPE:
882 case REAL_TYPE:
883 case RECORD_TYPE:
884 case TEMPLATE_TYPE_PARM:
885 case TEMPLATE_TEMPLATE_PARM:
886 case BOUND_TEMPLATE_TEMPLATE_PARM:
887 case TREE_LIST:
888 case TYPE_DECL:
889 case TREE_VEC:
890 case UNION_TYPE:
891 case LANG_TYPE:
892 case VOID_TYPE:
893 case TYPENAME_TYPE:
894 case COMPLEX_TYPE:
895 case VECTOR_TYPE:
896 case TYPEOF_TYPE:
897 case UNDERLYING_TYPE:
898 case DECLTYPE_TYPE:
899 case TYPE_PACK_EXPANSION:
900 case FIXED_POINT_TYPE:
901 case NULLPTR_TYPE:
902 break;
904 default:
905 pp_unsupported_tree (pp, t);
906 case ERROR_MARK:
907 /* Don't mark it here, we should have already done in
908 dump_type_prefix. */
909 break;
913 static void
914 dump_global_iord (cxx_pretty_printer *pp, tree t)
916 const char *p = NULL;
918 if (DECL_GLOBAL_CTOR_P (t))
919 p = M_("(static initializers for %s)");
920 else if (DECL_GLOBAL_DTOR_P (t))
921 p = M_("(static destructors for %s)");
922 else
923 gcc_unreachable ();
925 pp_printf (pp, p, input_filename);
928 static void
929 dump_simple_decl (cxx_pretty_printer *pp, tree t, tree type, int flags)
931 if (flags & TFF_DECL_SPECIFIERS)
933 if (VAR_P (t)
934 && DECL_DECLARED_CONSTEXPR_P (t))
935 pp_cxx_ws_string (pp, "constexpr");
936 dump_type_prefix (pp, type, flags & ~TFF_UNQUALIFIED_NAME);
937 pp_maybe_space (pp);
939 if (! (flags & TFF_UNQUALIFIED_NAME)
940 && TREE_CODE (t) != PARM_DECL
941 && (!DECL_INITIAL (t)
942 || TREE_CODE (DECL_INITIAL (t)) != TEMPLATE_PARM_INDEX))
943 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
944 flags &= ~TFF_UNQUALIFIED_NAME;
945 if ((flags & TFF_DECL_SPECIFIERS)
946 && DECL_TEMPLATE_PARM_P (t)
947 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (t)))
948 pp_string (pp, "...");
949 if (DECL_NAME (t))
951 if (TREE_CODE (t) == FIELD_DECL && DECL_NORMAL_CAPTURE_P (t))
953 pp_less (pp);
954 pp_string (pp, IDENTIFIER_POINTER (DECL_NAME (t)) + 2);
955 pp_string (pp, " capture>");
957 else
958 dump_decl (pp, DECL_NAME (t), flags);
960 else
961 pp_string (pp, M_("<anonymous>"));
962 if (flags & TFF_DECL_SPECIFIERS)
963 dump_type_suffix (pp, type, flags);
966 /* Dump a human readable string for the decl T under control of FLAGS. */
968 static void
969 dump_decl (cxx_pretty_printer *pp, tree t, int flags)
971 if (t == NULL_TREE)
972 return;
974 /* If doing Objective-C++, give Objective-C a chance to demangle
975 Objective-C method names. */
976 if (c_dialect_objc ())
978 const char *demangled = objc_maybe_printable_name (t, flags);
979 if (demangled)
981 pp_string (pp, demangled);
982 return;
986 switch (TREE_CODE (t))
988 case TYPE_DECL:
989 /* Don't say 'typedef class A' */
990 if (DECL_ARTIFICIAL (t) && !DECL_SELF_REFERENCE_P (t))
992 if ((flags & TFF_DECL_SPECIFIERS)
993 && TREE_CODE (TREE_TYPE (t)) == TEMPLATE_TYPE_PARM)
995 /* Say `class T' not just `T'. */
996 pp_cxx_ws_string (pp, "class");
998 /* Emit the `...' for a parameter pack. */
999 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
1000 pp_cxx_ws_string (pp, "...");
1003 dump_type (pp, TREE_TYPE (t), flags);
1004 break;
1006 if (TYPE_DECL_ALIAS_P (t)
1007 && (flags & TFF_DECL_SPECIFIERS
1008 || flags & TFF_CLASS_KEY_OR_ENUM))
1010 pp_cxx_ws_string (pp, "using");
1011 dump_decl (pp, DECL_NAME (t), flags);
1012 pp_cxx_whitespace (pp);
1013 pp_cxx_ws_string (pp, "=");
1014 pp_cxx_whitespace (pp);
1015 dump_type (pp, DECL_ORIGINAL_TYPE (t), flags);
1016 break;
1018 if ((flags & TFF_DECL_SPECIFIERS)
1019 && !DECL_SELF_REFERENCE_P (t))
1020 pp_cxx_ws_string (pp, "typedef");
1021 dump_simple_decl (pp, t, DECL_ORIGINAL_TYPE (t)
1022 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t),
1023 flags);
1024 break;
1026 case VAR_DECL:
1027 if (DECL_NAME (t) && VTABLE_NAME_P (DECL_NAME (t)))
1029 pp_string (pp, M_("vtable for "));
1030 gcc_assert (TYPE_P (DECL_CONTEXT (t)));
1031 dump_type (pp, DECL_CONTEXT (t), flags);
1032 break;
1034 /* Else fall through. */
1035 case FIELD_DECL:
1036 case PARM_DECL:
1037 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1038 break;
1040 case RESULT_DECL:
1041 pp_string (pp, M_("<return value> "));
1042 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1043 break;
1045 case NAMESPACE_DECL:
1046 if (flags & TFF_DECL_SPECIFIERS)
1047 pp_cxx_declaration (pp, t);
1048 else
1050 if (! (flags & TFF_UNQUALIFIED_NAME))
1051 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
1052 flags &= ~TFF_UNQUALIFIED_NAME;
1053 if (DECL_NAME (t) == NULL_TREE)
1055 if (!(pp->flags & pp_c_flag_gnu_v3))
1056 pp_cxx_ws_string (pp, M_("{anonymous}"));
1057 else
1058 pp_cxx_ws_string (pp, M_("(anonymous namespace)"));
1060 else
1061 pp_cxx_tree_identifier (pp, DECL_NAME (t));
1063 break;
1065 case SCOPE_REF:
1066 dump_type (pp, TREE_OPERAND (t, 0), flags);
1067 pp_colon_colon (pp);
1068 dump_decl (pp, TREE_OPERAND (t, 1), TFF_UNQUALIFIED_NAME);
1069 break;
1071 case ARRAY_REF:
1072 dump_decl (pp, TREE_OPERAND (t, 0), flags);
1073 pp_cxx_left_bracket (pp);
1074 dump_decl (pp, TREE_OPERAND (t, 1), flags);
1075 pp_cxx_right_bracket (pp);
1076 break;
1078 case ARRAY_NOTATION_REF:
1079 dump_decl (pp, ARRAY_NOTATION_ARRAY (t), flags | TFF_EXPR_IN_PARENS);
1080 pp_cxx_left_bracket (pp);
1081 dump_decl (pp, ARRAY_NOTATION_START (t), flags | TFF_EXPR_IN_PARENS);
1082 pp_colon (pp);
1083 dump_decl (pp, ARRAY_NOTATION_LENGTH (t), flags | TFF_EXPR_IN_PARENS);
1084 pp_colon (pp);
1085 dump_decl (pp, ARRAY_NOTATION_STRIDE (t), flags | TFF_EXPR_IN_PARENS);
1086 pp_cxx_right_bracket (pp);
1087 break;
1089 /* So that we can do dump_decl on an aggr type. */
1090 case RECORD_TYPE:
1091 case UNION_TYPE:
1092 case ENUMERAL_TYPE:
1093 dump_type (pp, t, flags);
1094 break;
1096 case BIT_NOT_EXPR:
1097 /* This is a pseudo destructor call which has not been folded into
1098 a PSEUDO_DTOR_EXPR yet. */
1099 pp_cxx_complement (pp);
1100 dump_type (pp, TREE_OPERAND (t, 0), flags);
1101 break;
1103 case TYPE_EXPR:
1104 gcc_unreachable ();
1105 break;
1107 /* These special cases are duplicated here so that other functions
1108 can feed identifiers to error and get them demangled properly. */
1109 case IDENTIFIER_NODE:
1110 if (IDENTIFIER_TYPENAME_P (t))
1112 pp_cxx_ws_string (pp, "operator");
1113 /* Not exactly IDENTIFIER_TYPE_VALUE. */
1114 dump_type (pp, TREE_TYPE (t), flags);
1115 break;
1117 else
1118 pp_cxx_tree_identifier (pp, t);
1119 break;
1121 case OVERLOAD:
1122 if (OVL_CHAIN (t))
1124 t = OVL_CURRENT (t);
1125 if (DECL_CLASS_SCOPE_P (t))
1127 dump_type (pp, DECL_CONTEXT (t), flags);
1128 pp_cxx_colon_colon (pp);
1130 else if (!DECL_FILE_SCOPE_P (t))
1132 dump_decl (pp, DECL_CONTEXT (t), flags);
1133 pp_cxx_colon_colon (pp);
1135 dump_decl (pp, DECL_NAME (t), flags);
1136 break;
1139 /* If there's only one function, just treat it like an ordinary
1140 FUNCTION_DECL. */
1141 t = OVL_CURRENT (t);
1142 /* Fall through. */
1144 case FUNCTION_DECL:
1145 if (! DECL_LANG_SPECIFIC (t))
1146 pp_string (pp, M_("<built-in>"));
1147 else if (DECL_GLOBAL_CTOR_P (t) || DECL_GLOBAL_DTOR_P (t))
1148 dump_global_iord (pp, t);
1149 else
1150 dump_function_decl (pp, t, flags);
1151 break;
1153 case TEMPLATE_DECL:
1154 dump_template_decl (pp, t, flags);
1155 break;
1157 case TEMPLATE_ID_EXPR:
1159 tree name = TREE_OPERAND (t, 0);
1160 tree args = TREE_OPERAND (t, 1);
1162 if (is_overloaded_fn (name))
1163 name = DECL_NAME (get_first_fn (name));
1164 dump_decl (pp, name, flags);
1165 pp_cxx_begin_template_argument_list (pp);
1166 if (args == error_mark_node)
1167 pp_string (pp, M_("<template arguments error>"));
1168 else if (args)
1169 dump_template_argument_list (pp, args, flags);
1170 pp_cxx_end_template_argument_list (pp);
1172 break;
1174 case LABEL_DECL:
1175 pp_cxx_tree_identifier (pp, DECL_NAME (t));
1176 break;
1178 case CONST_DECL:
1179 if ((TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == ENUMERAL_TYPE)
1180 || (DECL_INITIAL (t) &&
1181 TREE_CODE (DECL_INITIAL (t)) == TEMPLATE_PARM_INDEX))
1182 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1183 else if (DECL_NAME (t))
1184 dump_decl (pp, DECL_NAME (t), flags);
1185 else if (DECL_INITIAL (t))
1186 dump_expr (pp, DECL_INITIAL (t), flags | TFF_EXPR_IN_PARENS);
1187 else
1188 pp_string (pp, M_("<enumerator>"));
1189 break;
1191 case USING_DECL:
1192 pp_cxx_ws_string (pp, "using");
1193 dump_type (pp, USING_DECL_SCOPE (t), flags);
1194 pp_cxx_colon_colon (pp);
1195 dump_decl (pp, DECL_NAME (t), flags);
1196 break;
1198 case STATIC_ASSERT:
1199 pp_cxx_declaration (pp, t);
1200 break;
1202 case BASELINK:
1203 dump_decl (pp, BASELINK_FUNCTIONS (t), flags);
1204 break;
1206 case NON_DEPENDENT_EXPR:
1207 dump_expr (pp, t, flags);
1208 break;
1210 case TEMPLATE_TYPE_PARM:
1211 if (flags & TFF_DECL_SPECIFIERS)
1212 pp_cxx_declaration (pp, t);
1213 else
1214 pp_type_id (pp, t);
1215 break;
1217 case UNBOUND_CLASS_TEMPLATE:
1218 case TYPE_PACK_EXPANSION:
1219 case TREE_BINFO:
1220 dump_type (pp, t, flags);
1221 break;
1223 default:
1224 pp_unsupported_tree (pp, t);
1225 /* Fall through to error. */
1227 case ERROR_MARK:
1228 pp_string (pp, M_("<declaration error>"));
1229 break;
1233 /* Dump a template declaration T under control of FLAGS. This means the
1234 'template <...> leaders plus the 'class X' or 'void fn(...)' part. */
1236 static void
1237 dump_template_decl (cxx_pretty_printer *pp, tree t, int flags)
1239 tree orig_parms = DECL_TEMPLATE_PARMS (t);
1240 tree parms;
1241 int i;
1243 if (flags & TFF_TEMPLATE_HEADER)
1245 for (parms = orig_parms = nreverse (orig_parms);
1246 parms;
1247 parms = TREE_CHAIN (parms))
1249 tree inner_parms = INNERMOST_TEMPLATE_PARMS (parms);
1250 int len = TREE_VEC_LENGTH (inner_parms);
1252 pp_cxx_ws_string (pp, "template");
1253 pp_cxx_begin_template_argument_list (pp);
1255 /* If we've shown the template prefix, we'd better show the
1256 parameters' and decl's type too. */
1257 flags |= TFF_DECL_SPECIFIERS;
1259 for (i = 0; i < len; i++)
1261 if (i)
1262 pp_separate_with_comma (pp);
1263 dump_template_parameter (pp, TREE_VEC_ELT (inner_parms, i),
1264 flags);
1266 pp_cxx_end_template_argument_list (pp);
1267 pp_cxx_whitespace (pp);
1269 nreverse(orig_parms);
1271 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
1273 /* Say `template<arg> class TT' not just `template<arg> TT'. */
1274 pp_cxx_ws_string (pp, "class");
1276 /* If this is a parameter pack, print the ellipsis. */
1277 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
1278 pp_cxx_ws_string (pp, "...");
1282 if (DECL_CLASS_TEMPLATE_P (t))
1283 dump_type (pp, TREE_TYPE (t),
1284 ((flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1285 | (flags & TFF_DECL_SPECIFIERS ? TFF_CLASS_KEY_OR_ENUM : 0)));
1286 else if (DECL_TEMPLATE_RESULT (t)
1287 && (VAR_P (DECL_TEMPLATE_RESULT (t))
1288 /* Alias template. */
1289 || DECL_TYPE_TEMPLATE_P (t)))
1290 dump_decl (pp, DECL_TEMPLATE_RESULT (t), flags | TFF_TEMPLATE_NAME);
1291 else
1293 gcc_assert (TREE_TYPE (t));
1294 switch (NEXT_CODE (t))
1296 case METHOD_TYPE:
1297 case FUNCTION_TYPE:
1298 dump_function_decl (pp, t, flags | TFF_TEMPLATE_NAME);
1299 break;
1300 default:
1301 /* This case can occur with some invalid code. */
1302 dump_type (pp, TREE_TYPE (t),
1303 (flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1304 | (flags & TFF_DECL_SPECIFIERS
1305 ? TFF_CLASS_KEY_OR_ENUM : 0));
1310 /* find_typenames looks through the type of the function template T
1311 and returns a vec containing any typedefs, decltypes or TYPENAME_TYPEs
1312 it finds. */
1314 struct find_typenames_t
1316 struct pointer_set_t *p_set;
1317 vec<tree, va_gc> *typenames;
1320 static tree
1321 find_typenames_r (tree *tp, int *walk_subtrees, void *data)
1323 struct find_typenames_t *d = (struct find_typenames_t *)data;
1324 tree mv = NULL_TREE;
1326 if (TYPE_P (*tp) && is_typedef_decl (TYPE_NAME (*tp)))
1327 /* Add the type of the typedef without any additional cv-quals. */
1328 mv = TREE_TYPE (TYPE_NAME (*tp));
1329 else if (TREE_CODE (*tp) == TYPENAME_TYPE
1330 || TREE_CODE (*tp) == DECLTYPE_TYPE)
1331 /* Add the typename without any cv-qualifiers. */
1332 mv = TYPE_MAIN_VARIANT (*tp);
1334 if (TREE_CODE (*tp) == TYPE_PACK_EXPANSION)
1336 /* Don't mess with parameter packs since we don't remember
1337 the pack expansion context for a particular typename. */
1338 *walk_subtrees = false;
1339 return NULL_TREE;
1342 if (mv && (mv == *tp || !pointer_set_insert (d->p_set, mv)))
1343 vec_safe_push (d->typenames, mv);
1345 /* Search into class template arguments, which cp_walk_subtrees
1346 doesn't do. */
1347 if (CLASS_TYPE_P (*tp) && CLASSTYPE_TEMPLATE_INFO (*tp))
1348 cp_walk_tree (&CLASSTYPE_TI_ARGS (*tp), find_typenames_r,
1349 data, d->p_set);
1351 return NULL_TREE;
1354 static vec<tree, va_gc> *
1355 find_typenames (tree t)
1357 struct find_typenames_t ft;
1358 ft.p_set = pointer_set_create ();
1359 ft.typenames = NULL;
1360 cp_walk_tree (&TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
1361 find_typenames_r, &ft, ft.p_set);
1362 pointer_set_destroy (ft.p_set);
1363 return ft.typenames;
1366 /* Pretty print a function decl. There are several ways we want to print a
1367 function declaration. The TFF_ bits in FLAGS tells us how to behave.
1368 As error can only apply the '#' flag once to give 0 and 1 for V, there
1369 is %D which doesn't print the throw specs, and %F which does. */
1371 static void
1372 dump_function_decl (cxx_pretty_printer *pp, tree t, int flags)
1374 tree fntype;
1375 tree parmtypes;
1376 tree cname = NULL_TREE;
1377 tree template_args = NULL_TREE;
1378 tree template_parms = NULL_TREE;
1379 int show_return = flags & TFF_RETURN_TYPE || flags & TFF_DECL_SPECIFIERS;
1380 int do_outer_scope = ! (flags & TFF_UNQUALIFIED_NAME);
1381 tree exceptions;
1382 vec<tree, va_gc> *typenames = NULL;
1384 if (DECL_NAME (t) && LAMBDA_FUNCTION_P (t))
1386 /* A lambda's signature is essentially its "type", so defer. */
1387 gcc_assert (LAMBDA_TYPE_P (DECL_CONTEXT (t)));
1388 dump_type (pp, DECL_CONTEXT (t), flags);
1389 return;
1392 flags &= ~(TFF_UNQUALIFIED_NAME | TFF_TEMPLATE_NAME);
1393 if (TREE_CODE (t) == TEMPLATE_DECL)
1394 t = DECL_TEMPLATE_RESULT (t);
1396 /* Save the exceptions, in case t is a specialization and we are
1397 emitting an error about incompatible specifications. */
1398 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (t));
1400 /* Pretty print template instantiations only. */
1401 if (DECL_USE_TEMPLATE (t) && DECL_TEMPLATE_INFO (t)
1402 && flag_pretty_templates)
1404 tree tmpl;
1406 template_args = DECL_TI_ARGS (t);
1407 tmpl = most_general_template (t);
1408 if (tmpl && TREE_CODE (tmpl) == TEMPLATE_DECL)
1410 template_parms = DECL_TEMPLATE_PARMS (tmpl);
1411 t = tmpl;
1412 typenames = find_typenames (t);
1416 fntype = TREE_TYPE (t);
1417 parmtypes = FUNCTION_FIRST_USER_PARMTYPE (t);
1419 if (DECL_CLASS_SCOPE_P (t))
1420 cname = DECL_CONTEXT (t);
1421 /* This is for partially instantiated template methods. */
1422 else if (TREE_CODE (fntype) == METHOD_TYPE)
1423 cname = TREE_TYPE (TREE_VALUE (parmtypes));
1425 if (flags & TFF_DECL_SPECIFIERS)
1427 if (DECL_STATIC_FUNCTION_P (t))
1428 pp_cxx_ws_string (pp, "static");
1429 else if (DECL_VIRTUAL_P (t))
1430 pp_cxx_ws_string (pp, "virtual");
1432 if (DECL_DECLARED_CONSTEXPR_P (STRIP_TEMPLATE (t)))
1433 pp_cxx_ws_string (pp, "constexpr");
1436 /* Print the return type? */
1437 if (show_return)
1438 show_return = !DECL_CONV_FN_P (t) && !DECL_CONSTRUCTOR_P (t)
1439 && !DECL_DESTRUCTOR_P (t);
1440 if (show_return)
1442 tree ret = fndecl_declared_return_type (t);
1443 dump_type_prefix (pp, ret, flags);
1446 /* Print the function name. */
1447 if (!do_outer_scope)
1448 /* Nothing. */;
1449 else if (cname)
1451 dump_type (pp, cname, flags);
1452 pp_cxx_colon_colon (pp);
1454 else
1455 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
1457 dump_function_name (pp, t, flags);
1459 if (!(flags & TFF_NO_FUNCTION_ARGUMENTS))
1461 dump_parameters (pp, parmtypes, flags);
1463 if (TREE_CODE (fntype) == METHOD_TYPE)
1465 pp->padding = pp_before;
1466 pp_cxx_cv_qualifier_seq (pp, class_of_this_parm (fntype));
1467 dump_ref_qualifier (pp, fntype, flags);
1470 if (flags & TFF_EXCEPTION_SPECIFICATION)
1472 pp->padding = pp_before;
1473 dump_exception_spec (pp, exceptions, flags);
1476 if (show_return)
1477 dump_type_suffix (pp, TREE_TYPE (fntype), flags);
1479 /* If T is a template instantiation, dump the parameter binding. */
1480 if (template_parms != NULL_TREE && template_args != NULL_TREE
1481 && !(flags & TFF_NO_TEMPLATE_BINDINGS))
1483 pp_cxx_whitespace (pp);
1484 pp_cxx_left_bracket (pp);
1485 pp_cxx_ws_string (pp, M_("with"));
1486 pp_cxx_whitespace (pp);
1487 dump_template_bindings (pp, template_parms, template_args, typenames);
1488 pp_cxx_right_bracket (pp);
1491 else if (template_args)
1493 bool need_comma = false;
1494 int i;
1495 pp_cxx_begin_template_argument_list (pp);
1496 template_args = INNERMOST_TEMPLATE_ARGS (template_args);
1497 for (i = 0; i < TREE_VEC_LENGTH (template_args); ++i)
1499 tree arg = TREE_VEC_ELT (template_args, i);
1500 if (need_comma)
1501 pp_separate_with_comma (pp);
1502 if (ARGUMENT_PACK_P (arg))
1503 pp_cxx_left_brace (pp);
1504 dump_template_argument (pp, arg, TFF_PLAIN_IDENTIFIER);
1505 if (ARGUMENT_PACK_P (arg))
1506 pp_cxx_right_brace (pp);
1507 need_comma = true;
1509 pp_cxx_end_template_argument_list (pp);
1513 /* Print a parameter list. If this is for a member function, the
1514 member object ptr (and any other hidden args) should have
1515 already been removed. */
1517 static void
1518 dump_parameters (cxx_pretty_printer *pp, tree parmtypes, int flags)
1520 int first = 1;
1521 flags &= ~TFF_SCOPE;
1522 pp_cxx_left_paren (pp);
1524 for (first = 1; parmtypes != void_list_node;
1525 parmtypes = TREE_CHAIN (parmtypes))
1527 if (!first)
1528 pp_separate_with_comma (pp);
1529 first = 0;
1530 if (!parmtypes)
1532 pp_cxx_ws_string (pp, "...");
1533 break;
1536 dump_type (pp, TREE_VALUE (parmtypes), flags);
1538 if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && TREE_PURPOSE (parmtypes))
1540 pp_cxx_whitespace (pp);
1541 pp_equal (pp);
1542 pp_cxx_whitespace (pp);
1543 dump_expr (pp, TREE_PURPOSE (parmtypes), flags | TFF_EXPR_IN_PARENS);
1547 pp_cxx_right_paren (pp);
1550 /* Print ref-qualifier of a FUNCTION_TYPE or METHOD_TYPE. FLAGS are ignored. */
1552 static void
1553 dump_ref_qualifier (cxx_pretty_printer *pp, tree t, int flags ATTRIBUTE_UNUSED)
1555 if (FUNCTION_REF_QUALIFIED (t))
1557 pp->padding = pp_before;
1558 if (FUNCTION_RVALUE_QUALIFIED (t))
1559 pp_cxx_ws_string (pp, "&&");
1560 else
1561 pp_cxx_ws_string (pp, "&");
1565 /* Print an exception specification. T is the exception specification. */
1567 static void
1568 dump_exception_spec (cxx_pretty_printer *pp, tree t, int flags)
1570 if (t && TREE_PURPOSE (t))
1572 pp_cxx_ws_string (pp, "noexcept");
1573 pp_cxx_whitespace (pp);
1574 pp_cxx_left_paren (pp);
1575 if (DEFERRED_NOEXCEPT_SPEC_P (t))
1576 pp_cxx_ws_string (pp, "<uninstantiated>");
1577 else
1578 dump_expr (pp, TREE_PURPOSE (t), flags);
1579 pp_cxx_right_paren (pp);
1581 else if (t)
1583 pp_cxx_ws_string (pp, "throw");
1584 pp_cxx_whitespace (pp);
1585 pp_cxx_left_paren (pp);
1586 if (TREE_VALUE (t) != NULL_TREE)
1587 while (1)
1589 dump_type (pp, TREE_VALUE (t), flags);
1590 t = TREE_CHAIN (t);
1591 if (!t)
1592 break;
1593 pp_separate_with_comma (pp);
1595 pp_cxx_right_paren (pp);
1599 /* Handle the function name for a FUNCTION_DECL node, grokking operators
1600 and destructors properly. */
1602 static void
1603 dump_function_name (cxx_pretty_printer *pp, tree t, int flags)
1605 tree name = DECL_NAME (t);
1607 /* We can get here with a decl that was synthesized by language-
1608 independent machinery (e.g. coverage.c) in which case it won't
1609 have a lang_specific structure attached and DECL_CONSTRUCTOR_P
1610 will crash. In this case it is safe just to print out the
1611 literal name. */
1612 if (!DECL_LANG_SPECIFIC (t))
1614 pp_cxx_tree_identifier (pp, name);
1615 return;
1618 if (TREE_CODE (t) == TEMPLATE_DECL)
1619 t = DECL_TEMPLATE_RESULT (t);
1621 /* Don't let the user see __comp_ctor et al. */
1622 if (DECL_CONSTRUCTOR_P (t)
1623 || DECL_DESTRUCTOR_P (t))
1625 if (LAMBDA_TYPE_P (DECL_CONTEXT (t)))
1626 name = get_identifier ("<lambda>");
1627 else if (TYPE_ANONYMOUS_P (DECL_CONTEXT (t)))
1628 name = get_identifier ("<constructor>");
1629 else
1630 name = constructor_name (DECL_CONTEXT (t));
1633 if (DECL_DESTRUCTOR_P (t))
1635 pp_cxx_complement (pp);
1636 dump_decl (pp, name, TFF_PLAIN_IDENTIFIER);
1638 else if (DECL_CONV_FN_P (t))
1640 /* This cannot use the hack that the operator's return
1641 type is stashed off of its name because it may be
1642 used for error reporting. In the case of conflicting
1643 declarations, both will have the same name, yet
1644 the types will be different, hence the TREE_TYPE field
1645 of the first name will be clobbered by the second. */
1646 pp_cxx_ws_string (pp, "operator");
1647 dump_type (pp, TREE_TYPE (TREE_TYPE (t)), flags);
1649 else if (name && IDENTIFIER_OPNAME_P (name))
1650 pp_cxx_tree_identifier (pp, name);
1651 else if (name && UDLIT_OPER_P (name))
1652 pp_cxx_tree_identifier (pp, name);
1653 else
1654 dump_decl (pp, name, flags);
1656 if (DECL_TEMPLATE_INFO (t)
1657 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (t)
1658 && (TREE_CODE (DECL_TI_TEMPLATE (t)) != TEMPLATE_DECL
1659 || PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
1660 dump_template_parms (pp, DECL_TEMPLATE_INFO (t), !DECL_USE_TEMPLATE (t),
1661 flags);
1664 /* Dump the template parameters from the template info INFO under control of
1665 FLAGS. PRIMARY indicates whether this is a primary template decl, or
1666 specialization (partial or complete). For partial specializations we show
1667 the specialized parameter values. For a primary template we show no
1668 decoration. */
1670 static void
1671 dump_template_parms (cxx_pretty_printer *pp, tree info,
1672 int primary, int flags)
1674 tree args = info ? TI_ARGS (info) : NULL_TREE;
1676 if (primary && flags & TFF_TEMPLATE_NAME)
1677 return;
1678 flags &= ~(TFF_CLASS_KEY_OR_ENUM | TFF_TEMPLATE_NAME);
1679 pp_cxx_begin_template_argument_list (pp);
1681 /* Be careful only to print things when we have them, so as not
1682 to crash producing error messages. */
1683 if (args && !primary)
1685 int len, ix;
1686 len = get_non_default_template_args_count (args, flags);
1688 args = INNERMOST_TEMPLATE_ARGS (args);
1689 for (ix = 0; ix != len; ix++)
1691 tree arg = TREE_VEC_ELT (args, ix);
1693 /* Only print a comma if we know there is an argument coming. In
1694 the case of an empty template argument pack, no actual
1695 argument will be printed. */
1696 if (ix
1697 && (!ARGUMENT_PACK_P (arg)
1698 || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) > 0))
1699 pp_separate_with_comma (pp);
1701 if (!arg)
1702 pp_string (pp, M_("<template parameter error>"));
1703 else
1704 dump_template_argument (pp, arg, flags);
1707 else if (primary)
1709 tree tpl = TI_TEMPLATE (info);
1710 tree parms = DECL_TEMPLATE_PARMS (tpl);
1711 int len, ix;
1713 parms = TREE_CODE (parms) == TREE_LIST ? TREE_VALUE (parms) : NULL_TREE;
1714 len = parms ? TREE_VEC_LENGTH (parms) : 0;
1716 for (ix = 0; ix != len; ix++)
1718 tree parm;
1720 if (TREE_VEC_ELT (parms, ix) == error_mark_node)
1722 pp_string (pp, M_("<template parameter error>"));
1723 continue;
1726 parm = TREE_VALUE (TREE_VEC_ELT (parms, ix));
1728 if (ix)
1729 pp_separate_with_comma (pp);
1731 dump_decl (pp, parm, flags & ~TFF_DECL_SPECIFIERS);
1734 pp_cxx_end_template_argument_list (pp);
1737 /* Print out the arguments of CALL_EXPR T as a parenthesized list using
1738 flags FLAGS. Skip over the first argument if SKIPFIRST is true. */
1740 static void
1741 dump_call_expr_args (cxx_pretty_printer *pp, tree t, int flags, bool skipfirst)
1743 tree arg;
1744 call_expr_arg_iterator iter;
1746 pp_cxx_left_paren (pp);
1747 FOR_EACH_CALL_EXPR_ARG (arg, iter, t)
1749 if (skipfirst)
1750 skipfirst = false;
1751 else
1753 dump_expr (pp, arg, flags | TFF_EXPR_IN_PARENS);
1754 if (more_call_expr_args_p (&iter))
1755 pp_separate_with_comma (pp);
1758 pp_cxx_right_paren (pp);
1761 /* Print out the arguments of AGGR_INIT_EXPR T as a parenthesized list
1762 using flags FLAGS. Skip over the first argument if SKIPFIRST is
1763 true. */
1765 static void
1766 dump_aggr_init_expr_args (cxx_pretty_printer *pp, tree t, int flags,
1767 bool skipfirst)
1769 tree arg;
1770 aggr_init_expr_arg_iterator iter;
1772 pp_cxx_left_paren (pp);
1773 FOR_EACH_AGGR_INIT_EXPR_ARG (arg, iter, t)
1775 if (skipfirst)
1776 skipfirst = false;
1777 else
1779 dump_expr (pp, arg, flags | TFF_EXPR_IN_PARENS);
1780 if (more_aggr_init_expr_args_p (&iter))
1781 pp_separate_with_comma (pp);
1784 pp_cxx_right_paren (pp);
1787 /* Print out a list of initializers (subr of dump_expr). */
1789 static void
1790 dump_expr_list (cxx_pretty_printer *pp, tree l, int flags)
1792 while (l)
1794 dump_expr (pp, TREE_VALUE (l), flags | TFF_EXPR_IN_PARENS);
1795 l = TREE_CHAIN (l);
1796 if (l)
1797 pp_separate_with_comma (pp);
1801 /* Print out a vector of initializers (subr of dump_expr). */
1803 static void
1804 dump_expr_init_vec (cxx_pretty_printer *pp, vec<constructor_elt, va_gc> *v,
1805 int flags)
1807 unsigned HOST_WIDE_INT idx;
1808 tree value;
1810 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
1812 dump_expr (pp, value, flags | TFF_EXPR_IN_PARENS);
1813 if (idx != v->length () - 1)
1814 pp_separate_with_comma (pp);
1819 /* We've gotten an indirect REFERENCE (an OBJ_TYPE_REF) to a virtual
1820 function. Resolve it to a close relative -- in the sense of static
1821 type -- variant being overridden. That is close to what was written in
1822 the source code. Subroutine of dump_expr. */
1824 static tree
1825 resolve_virtual_fun_from_obj_type_ref (tree ref)
1827 tree obj_type = TREE_TYPE (OBJ_TYPE_REF_OBJECT (ref));
1828 HOST_WIDE_INT index = tree_low_cst (OBJ_TYPE_REF_TOKEN (ref), 1);
1829 tree fun = BINFO_VIRTUALS (TYPE_BINFO (TREE_TYPE (obj_type)));
1830 while (index)
1832 fun = TREE_CHAIN (fun);
1833 index -= (TARGET_VTABLE_USES_DESCRIPTORS
1834 ? TARGET_VTABLE_USES_DESCRIPTORS : 1);
1837 return BV_FN (fun);
1840 /* Print out an expression E under control of FLAGS. */
1842 static void
1843 dump_expr (cxx_pretty_printer *pp, tree t, int flags)
1845 tree op;
1847 if (t == 0)
1848 return;
1850 if (STATEMENT_CLASS_P (t))
1852 pp_cxx_ws_string (pp, M_("<statement>"));
1853 return;
1856 switch (TREE_CODE (t))
1858 case VAR_DECL:
1859 case PARM_DECL:
1860 case FIELD_DECL:
1861 case CONST_DECL:
1862 case FUNCTION_DECL:
1863 case TEMPLATE_DECL:
1864 case NAMESPACE_DECL:
1865 case LABEL_DECL:
1866 case OVERLOAD:
1867 case TYPE_DECL:
1868 case IDENTIFIER_NODE:
1869 dump_decl (pp, t, ((flags & ~(TFF_DECL_SPECIFIERS|TFF_RETURN_TYPE
1870 |TFF_TEMPLATE_HEADER))
1871 | TFF_NO_FUNCTION_ARGUMENTS));
1872 break;
1874 case SSA_NAME:
1875 if (SSA_NAME_VAR (t)
1876 && !DECL_ARTIFICIAL (SSA_NAME_VAR (t)))
1877 dump_expr (pp, SSA_NAME_VAR (t), flags);
1878 else
1879 pp_cxx_ws_string (pp, M_("<unknown>"));
1880 break;
1882 case INTEGER_CST:
1883 case REAL_CST:
1884 case STRING_CST:
1885 case COMPLEX_CST:
1886 pp_constant (pp, t);
1887 break;
1889 case USERDEF_LITERAL:
1890 pp_cxx_userdef_literal (pp, t);
1891 break;
1893 case THROW_EXPR:
1894 /* While waiting for caret diagnostics, avoid printing
1895 __cxa_allocate_exception, __cxa_throw, and the like. */
1896 pp_cxx_ws_string (pp, M_("<throw-expression>"));
1897 break;
1899 case PTRMEM_CST:
1900 pp_ampersand (pp);
1901 dump_type (pp, PTRMEM_CST_CLASS (t), flags);
1902 pp_cxx_colon_colon (pp);
1903 pp_cxx_tree_identifier (pp, DECL_NAME (PTRMEM_CST_MEMBER (t)));
1904 break;
1906 case COMPOUND_EXPR:
1907 pp_cxx_left_paren (pp);
1908 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1909 pp_separate_with_comma (pp);
1910 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
1911 pp_cxx_right_paren (pp);
1912 break;
1914 case COND_EXPR:
1915 pp_cxx_left_paren (pp);
1916 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1917 pp_string (pp, " ? ");
1918 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
1919 pp_string (pp, " : ");
1920 dump_expr (pp, TREE_OPERAND (t, 2), flags | TFF_EXPR_IN_PARENS);
1921 pp_cxx_right_paren (pp);
1922 break;
1924 case SAVE_EXPR:
1925 if (TREE_HAS_CONSTRUCTOR (t))
1927 pp_cxx_ws_string (pp, "new");
1928 pp_cxx_whitespace (pp);
1929 dump_type (pp, TREE_TYPE (TREE_TYPE (t)), flags);
1931 else
1932 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1933 break;
1935 case AGGR_INIT_EXPR:
1937 tree fn = NULL_TREE;
1939 if (TREE_CODE (AGGR_INIT_EXPR_FN (t)) == ADDR_EXPR)
1940 fn = TREE_OPERAND (AGGR_INIT_EXPR_FN (t), 0);
1942 if (fn && TREE_CODE (fn) == FUNCTION_DECL)
1944 if (DECL_CONSTRUCTOR_P (fn))
1945 dump_type (pp, DECL_CONTEXT (fn), flags);
1946 else
1947 dump_decl (pp, fn, 0);
1949 else
1950 dump_expr (pp, AGGR_INIT_EXPR_FN (t), 0);
1952 dump_aggr_init_expr_args (pp, t, flags, true);
1953 break;
1955 case CALL_EXPR:
1957 tree fn = CALL_EXPR_FN (t);
1958 bool skipfirst = false;
1960 if (TREE_CODE (fn) == ADDR_EXPR)
1961 fn = TREE_OPERAND (fn, 0);
1963 /* Nobody is interested in seeing the guts of vcalls. */
1964 if (TREE_CODE (fn) == OBJ_TYPE_REF)
1965 fn = resolve_virtual_fun_from_obj_type_ref (fn);
1967 if (TREE_TYPE (fn) != NULL_TREE
1968 && NEXT_CODE (fn) == METHOD_TYPE
1969 && call_expr_nargs (t))
1971 tree ob = CALL_EXPR_ARG (t, 0);
1972 if (TREE_CODE (ob) == ADDR_EXPR)
1974 dump_expr (pp, TREE_OPERAND (ob, 0),
1975 flags | TFF_EXPR_IN_PARENS);
1976 pp_cxx_dot (pp);
1978 else if (TREE_CODE (ob) != PARM_DECL
1979 || strcmp (IDENTIFIER_POINTER (DECL_NAME (ob)), "this"))
1981 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
1982 pp_cxx_arrow (pp);
1984 skipfirst = true;
1986 dump_expr (pp, fn, flags | TFF_EXPR_IN_PARENS);
1987 dump_call_expr_args (pp, t, flags, skipfirst);
1989 break;
1991 case TARGET_EXPR:
1992 /* Note that this only works for G++ target exprs. If somebody
1993 builds a general TARGET_EXPR, there's no way to represent that
1994 it initializes anything other that the parameter slot for the
1995 default argument. Note we may have cleared out the first
1996 operand in expand_expr, so don't go killing ourselves. */
1997 if (TREE_OPERAND (t, 1))
1998 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
1999 break;
2001 case POINTER_PLUS_EXPR:
2002 dump_binary_op (pp, "+", t, flags);
2003 break;
2005 case INIT_EXPR:
2006 case MODIFY_EXPR:
2007 dump_binary_op (pp, assignment_operator_name_info[NOP_EXPR].name,
2008 t, flags);
2009 break;
2011 case PLUS_EXPR:
2012 case MINUS_EXPR:
2013 case MULT_EXPR:
2014 case TRUNC_DIV_EXPR:
2015 case TRUNC_MOD_EXPR:
2016 case MIN_EXPR:
2017 case MAX_EXPR:
2018 case LSHIFT_EXPR:
2019 case RSHIFT_EXPR:
2020 case BIT_IOR_EXPR:
2021 case BIT_XOR_EXPR:
2022 case BIT_AND_EXPR:
2023 case TRUTH_ANDIF_EXPR:
2024 case TRUTH_ORIF_EXPR:
2025 case LT_EXPR:
2026 case LE_EXPR:
2027 case GT_EXPR:
2028 case GE_EXPR:
2029 case EQ_EXPR:
2030 case NE_EXPR:
2031 case EXACT_DIV_EXPR:
2032 dump_binary_op (pp, operator_name_info[TREE_CODE (t)].name, t, flags);
2033 break;
2035 case CEIL_DIV_EXPR:
2036 case FLOOR_DIV_EXPR:
2037 case ROUND_DIV_EXPR:
2038 case RDIV_EXPR:
2039 dump_binary_op (pp, "/", t, flags);
2040 break;
2042 case CEIL_MOD_EXPR:
2043 case FLOOR_MOD_EXPR:
2044 case ROUND_MOD_EXPR:
2045 dump_binary_op (pp, "%", t, flags);
2046 break;
2048 case COMPONENT_REF:
2050 tree ob = TREE_OPERAND (t, 0);
2051 if (INDIRECT_REF_P (ob))
2053 ob = TREE_OPERAND (ob, 0);
2054 if (TREE_CODE (ob) != PARM_DECL
2055 || (DECL_NAME (ob)
2056 && strcmp (IDENTIFIER_POINTER (DECL_NAME (ob)), "this")))
2058 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2059 if (TREE_CODE (TREE_TYPE (ob)) == REFERENCE_TYPE)
2060 pp_cxx_dot (pp);
2061 else
2062 pp_cxx_arrow (pp);
2065 else
2067 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2068 pp_cxx_dot (pp);
2070 dump_expr (pp, TREE_OPERAND (t, 1), flags & ~TFF_EXPR_IN_PARENS);
2072 break;
2074 case ARRAY_REF:
2075 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2076 pp_cxx_left_bracket (pp);
2077 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2078 pp_cxx_right_bracket (pp);
2079 break;
2081 case ARRAY_NOTATION_REF:
2082 dump_expr (pp, ARRAY_NOTATION_ARRAY (t), flags | TFF_EXPR_IN_PARENS);
2083 pp_cxx_left_bracket (pp);
2084 dump_expr (pp, ARRAY_NOTATION_START (t), flags | TFF_EXPR_IN_PARENS);
2085 pp_colon (pp);
2086 dump_expr (pp, ARRAY_NOTATION_LENGTH (t), flags | TFF_EXPR_IN_PARENS);
2087 pp_colon (pp);
2088 dump_expr (pp, ARRAY_NOTATION_STRIDE (t), flags | TFF_EXPR_IN_PARENS);
2089 pp_cxx_right_bracket (pp);
2090 break;
2092 case UNARY_PLUS_EXPR:
2093 dump_unary_op (pp, "+", t, flags);
2094 break;
2096 case ADDR_EXPR:
2097 if (TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
2098 || TREE_CODE (TREE_OPERAND (t, 0)) == STRING_CST
2099 /* An ADDR_EXPR can have reference type. In that case, we
2100 shouldn't print the `&' doing so indicates to the user
2101 that the expression has pointer type. */
2102 || (TREE_TYPE (t)
2103 && TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE))
2104 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2105 else if (TREE_CODE (TREE_OPERAND (t, 0)) == LABEL_DECL)
2106 dump_unary_op (pp, "&&", t, flags);
2107 else
2108 dump_unary_op (pp, "&", t, flags);
2109 break;
2111 case INDIRECT_REF:
2112 if (TREE_HAS_CONSTRUCTOR (t))
2114 t = TREE_OPERAND (t, 0);
2115 gcc_assert (TREE_CODE (t) == CALL_EXPR);
2116 dump_expr (pp, CALL_EXPR_FN (t), flags | TFF_EXPR_IN_PARENS);
2117 dump_call_expr_args (pp, t, flags, true);
2119 else
2121 if (TREE_OPERAND (t,0) != NULL_TREE
2122 && TREE_TYPE (TREE_OPERAND (t, 0))
2123 && NEXT_CODE (TREE_OPERAND (t, 0)) == REFERENCE_TYPE)
2124 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2125 else
2126 dump_unary_op (pp, "*", t, flags);
2128 break;
2130 case MEM_REF:
2131 if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
2132 && integer_zerop (TREE_OPERAND (t, 1)))
2133 dump_expr (pp, TREE_OPERAND (TREE_OPERAND (t, 0), 0), flags);
2134 else
2136 pp_cxx_star (pp);
2137 if (!integer_zerop (TREE_OPERAND (t, 1)))
2139 pp_cxx_left_paren (pp);
2140 if (!integer_onep (TYPE_SIZE_UNIT
2141 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0))))))
2143 pp_cxx_left_paren (pp);
2144 dump_type (pp, ptr_type_node, flags);
2145 pp_cxx_right_paren (pp);
2148 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2149 if (!integer_zerop (TREE_OPERAND (t, 1)))
2151 pp_cxx_ws_string (pp, "+");
2152 dump_expr (pp, fold_convert (ssizetype, TREE_OPERAND (t, 1)),
2153 flags);
2154 pp_cxx_right_paren (pp);
2157 break;
2159 case NEGATE_EXPR:
2160 case BIT_NOT_EXPR:
2161 case TRUTH_NOT_EXPR:
2162 case PREDECREMENT_EXPR:
2163 case PREINCREMENT_EXPR:
2164 dump_unary_op (pp, operator_name_info [TREE_CODE (t)].name, t, flags);
2165 break;
2167 case POSTDECREMENT_EXPR:
2168 case POSTINCREMENT_EXPR:
2169 pp_cxx_left_paren (pp);
2170 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2171 pp_cxx_ws_string (pp, operator_name_info[TREE_CODE (t)].name);
2172 pp_cxx_right_paren (pp);
2173 break;
2175 case NON_LVALUE_EXPR:
2176 /* FIXME: This is a KLUDGE workaround for a parsing problem. There
2177 should be another level of INDIRECT_REF so that I don't have to do
2178 this. */
2179 if (TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == POINTER_TYPE)
2181 tree next = TREE_TYPE (TREE_TYPE (t));
2183 while (TYPE_PTR_P (next))
2184 next = TREE_TYPE (next);
2186 if (TREE_CODE (next) == FUNCTION_TYPE)
2188 if (flags & TFF_EXPR_IN_PARENS)
2189 pp_cxx_left_paren (pp);
2190 pp_cxx_star (pp);
2191 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2192 if (flags & TFF_EXPR_IN_PARENS)
2193 pp_cxx_right_paren (pp);
2194 break;
2196 /* Else fall through. */
2198 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2199 break;
2201 CASE_CONVERT:
2202 case IMPLICIT_CONV_EXPR:
2203 case VIEW_CONVERT_EXPR:
2205 tree op = TREE_OPERAND (t, 0);
2206 tree ttype = TREE_TYPE (t);
2207 tree optype = TREE_TYPE (op);
2209 if (TREE_CODE (ttype) != TREE_CODE (optype)
2210 && POINTER_TYPE_P (ttype)
2211 && POINTER_TYPE_P (optype)
2212 && same_type_p (TREE_TYPE (optype),
2213 TREE_TYPE (ttype)))
2215 if (TREE_CODE (ttype) == REFERENCE_TYPE)
2216 dump_unary_op (pp, "*", t, flags);
2217 else
2218 dump_unary_op (pp, "&", t, flags);
2220 else if (!same_type_p (TREE_TYPE (op), TREE_TYPE (t)))
2222 /* It is a cast, but we cannot tell whether it is a
2223 reinterpret or static cast. Use the C style notation. */
2224 if (flags & TFF_EXPR_IN_PARENS)
2225 pp_cxx_left_paren (pp);
2226 pp_cxx_left_paren (pp);
2227 dump_type (pp, TREE_TYPE (t), flags);
2228 pp_cxx_right_paren (pp);
2229 dump_expr (pp, op, flags | TFF_EXPR_IN_PARENS);
2230 if (flags & TFF_EXPR_IN_PARENS)
2231 pp_cxx_right_paren (pp);
2233 else
2234 dump_expr (pp, op, flags);
2235 break;
2238 case CONSTRUCTOR:
2239 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
2241 tree idx = build_ptrmemfunc_access_expr (t, pfn_identifier);
2243 if (integer_zerop (idx))
2245 /* A NULL pointer-to-member constant. */
2246 pp_cxx_left_paren (pp);
2247 pp_cxx_left_paren (pp);
2248 dump_type (pp, TREE_TYPE (t), flags);
2249 pp_cxx_right_paren (pp);
2250 pp_character (pp, '0');
2251 pp_cxx_right_paren (pp);
2252 break;
2254 else if (host_integerp (idx, 0))
2256 tree virtuals;
2257 unsigned HOST_WIDE_INT n;
2259 t = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
2260 t = TYPE_METHOD_BASETYPE (t);
2261 virtuals = BINFO_VIRTUALS (TYPE_BINFO (TYPE_MAIN_VARIANT (t)));
2263 n = tree_low_cst (idx, 0);
2265 /* Map vtable index back one, to allow for the null pointer to
2266 member. */
2267 --n;
2269 while (n > 0 && virtuals)
2271 --n;
2272 virtuals = TREE_CHAIN (virtuals);
2274 if (virtuals)
2276 dump_expr (pp, BV_FN (virtuals),
2277 flags | TFF_EXPR_IN_PARENS);
2278 break;
2282 if (TREE_TYPE (t) && LAMBDA_TYPE_P (TREE_TYPE (t)))
2283 pp_string (pp, "<lambda closure object>");
2284 if (TREE_TYPE (t) && EMPTY_CONSTRUCTOR_P (t))
2286 dump_type (pp, TREE_TYPE (t), 0);
2287 pp_cxx_left_paren (pp);
2288 pp_cxx_right_paren (pp);
2290 else
2292 if (!BRACE_ENCLOSED_INITIALIZER_P (t))
2293 dump_type (pp, TREE_TYPE (t), 0);
2294 pp_cxx_left_brace (pp);
2295 dump_expr_init_vec (pp, CONSTRUCTOR_ELTS (t), flags);
2296 pp_cxx_right_brace (pp);
2299 break;
2301 case OFFSET_REF:
2303 tree ob = TREE_OPERAND (t, 0);
2304 if (is_dummy_object (ob))
2306 t = TREE_OPERAND (t, 1);
2307 if (TREE_CODE (t) == FUNCTION_DECL)
2308 /* A::f */
2309 dump_expr (pp, t, flags | TFF_EXPR_IN_PARENS);
2310 else if (BASELINK_P (t))
2311 dump_expr (pp, OVL_CURRENT (BASELINK_FUNCTIONS (t)),
2312 flags | TFF_EXPR_IN_PARENS);
2313 else
2314 dump_decl (pp, t, flags);
2316 else
2318 if (INDIRECT_REF_P (ob))
2320 dump_expr (pp, TREE_OPERAND (ob, 0), flags | TFF_EXPR_IN_PARENS);
2321 pp_cxx_arrow (pp);
2322 pp_cxx_star (pp);
2324 else
2326 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2327 pp_cxx_dot (pp);
2328 pp_cxx_star (pp);
2330 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2332 break;
2335 case TEMPLATE_PARM_INDEX:
2336 dump_decl (pp, TEMPLATE_PARM_DECL (t), flags & ~TFF_DECL_SPECIFIERS);
2337 break;
2339 case CAST_EXPR:
2340 if (TREE_OPERAND (t, 0) == NULL_TREE
2341 || TREE_CHAIN (TREE_OPERAND (t, 0)))
2343 dump_type (pp, TREE_TYPE (t), flags);
2344 pp_cxx_left_paren (pp);
2345 dump_expr_list (pp, TREE_OPERAND (t, 0), flags);
2346 pp_cxx_right_paren (pp);
2348 else
2350 pp_cxx_left_paren (pp);
2351 dump_type (pp, TREE_TYPE (t), flags);
2352 pp_cxx_right_paren (pp);
2353 pp_cxx_left_paren (pp);
2354 dump_expr_list (pp, TREE_OPERAND (t, 0), flags);
2355 pp_cxx_right_paren (pp);
2357 break;
2359 case STATIC_CAST_EXPR:
2360 pp_cxx_ws_string (pp, "static_cast");
2361 goto cast;
2362 case REINTERPRET_CAST_EXPR:
2363 pp_cxx_ws_string (pp, "reinterpret_cast");
2364 goto cast;
2365 case CONST_CAST_EXPR:
2366 pp_cxx_ws_string (pp, "const_cast");
2367 goto cast;
2368 case DYNAMIC_CAST_EXPR:
2369 pp_cxx_ws_string (pp, "dynamic_cast");
2370 cast:
2371 pp_cxx_begin_template_argument_list (pp);
2372 dump_type (pp, TREE_TYPE (t), flags);
2373 pp_cxx_end_template_argument_list (pp);
2374 pp_cxx_left_paren (pp);
2375 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2376 pp_cxx_right_paren (pp);
2377 break;
2379 case ARROW_EXPR:
2380 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2381 pp_cxx_arrow (pp);
2382 break;
2384 case SIZEOF_EXPR:
2385 case ALIGNOF_EXPR:
2386 if (TREE_CODE (t) == SIZEOF_EXPR)
2387 pp_cxx_ws_string (pp, "sizeof");
2388 else
2390 gcc_assert (TREE_CODE (t) == ALIGNOF_EXPR);
2391 pp_cxx_ws_string (pp, "__alignof__");
2393 op = TREE_OPERAND (t, 0);
2394 if (PACK_EXPANSION_P (op))
2396 pp_string (pp, "...");
2397 op = PACK_EXPANSION_PATTERN (op);
2399 pp_cxx_whitespace (pp);
2400 pp_cxx_left_paren (pp);
2401 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
2402 dump_type (pp, TREE_TYPE (op), flags);
2403 else if (TYPE_P (TREE_OPERAND (t, 0)))
2404 dump_type (pp, op, flags);
2405 else
2406 dump_expr (pp, op, flags);
2407 pp_cxx_right_paren (pp);
2408 break;
2410 case AT_ENCODE_EXPR:
2411 pp_cxx_ws_string (pp, "@encode");
2412 pp_cxx_whitespace (pp);
2413 pp_cxx_left_paren (pp);
2414 dump_type (pp, TREE_OPERAND (t, 0), flags);
2415 pp_cxx_right_paren (pp);
2416 break;
2418 case NOEXCEPT_EXPR:
2419 pp_cxx_ws_string (pp, "noexcept");
2420 pp_cxx_whitespace (pp);
2421 pp_cxx_left_paren (pp);
2422 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2423 pp_cxx_right_paren (pp);
2424 break;
2426 case REALPART_EXPR:
2427 case IMAGPART_EXPR:
2428 pp_cxx_ws_string (pp, operator_name_info[TREE_CODE (t)].name);
2429 pp_cxx_whitespace (pp);
2430 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2431 break;
2433 case DEFAULT_ARG:
2434 pp_string (pp, M_("<unparsed>"));
2435 break;
2437 case TRY_CATCH_EXPR:
2438 case WITH_CLEANUP_EXPR:
2439 case CLEANUP_POINT_EXPR:
2440 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2441 break;
2443 case PSEUDO_DTOR_EXPR:
2444 dump_expr (pp, TREE_OPERAND (t, 2), flags);
2445 pp_cxx_dot (pp);
2446 dump_type (pp, TREE_OPERAND (t, 0), flags);
2447 pp_cxx_colon_colon (pp);
2448 pp_cxx_complement (pp);
2449 dump_type (pp, TREE_OPERAND (t, 1), flags);
2450 break;
2452 case TEMPLATE_ID_EXPR:
2453 dump_decl (pp, t, flags);
2454 break;
2456 case BIND_EXPR:
2457 case STMT_EXPR:
2458 case EXPR_STMT:
2459 case STATEMENT_LIST:
2460 /* We don't yet have a way of dumping statements in a
2461 human-readable format. */
2462 pp_string (pp, "({...})");
2463 break;
2465 case LOOP_EXPR:
2466 pp_string (pp, "while (1) { ");
2467 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2468 pp_cxx_right_brace (pp);
2469 break;
2471 case EXIT_EXPR:
2472 pp_string (pp, "if (");
2473 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2474 pp_string (pp, ") break; ");
2475 break;
2477 case BASELINK:
2478 dump_expr (pp, BASELINK_FUNCTIONS (t), flags & ~TFF_EXPR_IN_PARENS);
2479 break;
2481 case EMPTY_CLASS_EXPR:
2482 dump_type (pp, TREE_TYPE (t), flags);
2483 pp_cxx_left_paren (pp);
2484 pp_cxx_right_paren (pp);
2485 break;
2487 case NON_DEPENDENT_EXPR:
2488 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2489 break;
2491 case ARGUMENT_PACK_SELECT:
2492 dump_template_argument (pp, ARGUMENT_PACK_SELECT_FROM_PACK (t), flags);
2493 break;
2495 case RECORD_TYPE:
2496 case UNION_TYPE:
2497 case ENUMERAL_TYPE:
2498 case REAL_TYPE:
2499 case VOID_TYPE:
2500 case BOOLEAN_TYPE:
2501 case INTEGER_TYPE:
2502 case COMPLEX_TYPE:
2503 case VECTOR_TYPE:
2504 pp_type_specifier_seq (pp, t);
2505 break;
2507 case TYPENAME_TYPE:
2508 /* We get here when we want to print a dependent type as an
2509 id-expression, without any disambiguator decoration. */
2510 pp_id_expression (pp, t);
2511 break;
2513 case TEMPLATE_TYPE_PARM:
2514 case TEMPLATE_TEMPLATE_PARM:
2515 case BOUND_TEMPLATE_TEMPLATE_PARM:
2516 dump_type (pp, t, flags);
2517 break;
2519 case TRAIT_EXPR:
2520 pp_cxx_trait_expression (pp, t);
2521 break;
2523 case VA_ARG_EXPR:
2524 pp_cxx_va_arg_expression (pp, t);
2525 break;
2527 case OFFSETOF_EXPR:
2528 pp_cxx_offsetof_expression (pp, t);
2529 break;
2531 case SCOPE_REF:
2532 dump_decl (pp, t, flags);
2533 break;
2535 case EXPR_PACK_EXPANSION:
2536 case TYPEID_EXPR:
2537 case MEMBER_REF:
2538 case DOTSTAR_EXPR:
2539 case NEW_EXPR:
2540 case VEC_NEW_EXPR:
2541 case DELETE_EXPR:
2542 case VEC_DELETE_EXPR:
2543 case MODOP_EXPR:
2544 case ABS_EXPR:
2545 case CONJ_EXPR:
2546 case VECTOR_CST:
2547 case FIXED_CST:
2548 case UNORDERED_EXPR:
2549 case ORDERED_EXPR:
2550 case UNLT_EXPR:
2551 case UNLE_EXPR:
2552 case UNGT_EXPR:
2553 case UNGE_EXPR:
2554 case UNEQ_EXPR:
2555 case LTGT_EXPR:
2556 case COMPLEX_EXPR:
2557 case BIT_FIELD_REF:
2558 case FIX_TRUNC_EXPR:
2559 case FLOAT_EXPR:
2560 pp_expression (pp, t);
2561 break;
2563 case TRUTH_AND_EXPR:
2564 case TRUTH_OR_EXPR:
2565 case TRUTH_XOR_EXPR:
2566 if (flags & TFF_EXPR_IN_PARENS)
2567 pp_cxx_left_paren (pp);
2568 pp_expression (pp, t);
2569 if (flags & TFF_EXPR_IN_PARENS)
2570 pp_cxx_right_paren (pp);
2571 break;
2573 case OBJ_TYPE_REF:
2574 dump_expr (pp, resolve_virtual_fun_from_obj_type_ref (t), flags);
2575 break;
2577 case LAMBDA_EXPR:
2578 pp_string (pp, M_("<lambda>"));
2579 break;
2581 case PAREN_EXPR:
2582 pp_cxx_left_paren (pp);
2583 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2584 pp_cxx_right_paren (pp);
2585 break;
2587 /* This list is incomplete, but should suffice for now.
2588 It is very important that `sorry' does not call
2589 `report_error_function'. That could cause an infinite loop. */
2590 default:
2591 pp_unsupported_tree (pp, t);
2592 /* fall through to ERROR_MARK... */
2593 case ERROR_MARK:
2594 pp_string (pp, M_("<expression error>"));
2595 break;
2599 static void
2600 dump_binary_op (cxx_pretty_printer *pp, const char *opstring, tree t,
2601 int flags)
2603 pp_cxx_left_paren (pp);
2604 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2605 pp_cxx_whitespace (pp);
2606 if (opstring)
2607 pp_cxx_ws_string (pp, opstring);
2608 else
2609 pp_string (pp, M_("<unknown operator>"));
2610 pp_cxx_whitespace (pp);
2611 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2612 pp_cxx_right_paren (pp);
2615 static void
2616 dump_unary_op (cxx_pretty_printer *pp, const char *opstring, tree t, int flags)
2618 if (flags & TFF_EXPR_IN_PARENS)
2619 pp_cxx_left_paren (pp);
2620 pp_cxx_ws_string (pp, opstring);
2621 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2622 if (flags & TFF_EXPR_IN_PARENS)
2623 pp_cxx_right_paren (pp);
2626 static void
2627 reinit_cxx_pp (void)
2629 pp_clear_output_area (cxx_pp);
2630 cxx_pp->padding = pp_none;
2631 pp_indentation (cxx_pp) = 0;
2632 pp_needs_newline (cxx_pp) = false;
2633 cxx_pp->enclosing_scope = current_function_decl;
2636 /* Same as pp_formatted_text, except the return string is a separate
2637 copy and has a GGC storage duration, e.g. an indefinite lifetime. */
2639 inline const char *
2640 pp_ggc_formatted_text (pretty_printer *pp)
2642 return ggc_strdup (pp_formatted_text (pp));
2645 /* Exported interface to stringifying types, exprs and decls under TFF_*
2646 control. */
2648 const char *
2649 type_as_string (tree typ, int flags)
2651 reinit_cxx_pp ();
2652 pp_translate_identifiers (cxx_pp) = false;
2653 dump_type (cxx_pp, typ, flags);
2654 return pp_ggc_formatted_text (cxx_pp);
2657 const char *
2658 type_as_string_translate (tree typ, int flags)
2660 reinit_cxx_pp ();
2661 dump_type (cxx_pp, typ, flags);
2662 return pp_ggc_formatted_text (cxx_pp);
2665 const char *
2666 expr_as_string (tree decl, int flags)
2668 reinit_cxx_pp ();
2669 pp_translate_identifiers (cxx_pp) = false;
2670 dump_expr (cxx_pp, decl, flags);
2671 return pp_ggc_formatted_text (cxx_pp);
2674 /* Wrap decl_as_string with options appropriate for dwarf. */
2676 const char *
2677 decl_as_dwarf_string (tree decl, int flags)
2679 const char *name;
2680 /* Curiously, reinit_cxx_pp doesn't reset the flags field, so setting the flag
2681 here will be adequate to get the desired behaviour. */
2682 cxx_pp->flags |= pp_c_flag_gnu_v3;
2683 name = decl_as_string (decl, flags);
2684 /* Subsequent calls to the pretty printer shouldn't use this style. */
2685 cxx_pp->flags &= ~pp_c_flag_gnu_v3;
2686 return name;
2689 const char *
2690 decl_as_string (tree decl, int flags)
2692 reinit_cxx_pp ();
2693 pp_translate_identifiers (cxx_pp) = false;
2694 dump_decl (cxx_pp, decl, flags);
2695 return pp_ggc_formatted_text (cxx_pp);
2698 const char *
2699 decl_as_string_translate (tree decl, int flags)
2701 reinit_cxx_pp ();
2702 dump_decl (cxx_pp, decl, flags);
2703 return pp_ggc_formatted_text (cxx_pp);
2706 /* Wrap lang_decl_name with options appropriate for dwarf. */
2708 const char *
2709 lang_decl_dwarf_name (tree decl, int v, bool translate)
2711 const char *name;
2712 /* Curiously, reinit_cxx_pp doesn't reset the flags field, so setting the flag
2713 here will be adequate to get the desired behaviour. */
2714 cxx_pp->flags |= pp_c_flag_gnu_v3;
2715 name = lang_decl_name (decl, v, translate);
2716 /* Subsequent calls to the pretty printer shouldn't use this style. */
2717 cxx_pp->flags &= ~pp_c_flag_gnu_v3;
2718 return name;
2721 /* Generate the three forms of printable names for cxx_printable_name. */
2723 const char *
2724 lang_decl_name (tree decl, int v, bool translate)
2726 if (v >= 2)
2727 return (translate
2728 ? decl_as_string_translate (decl, TFF_DECL_SPECIFIERS)
2729 : decl_as_string (decl, TFF_DECL_SPECIFIERS));
2731 reinit_cxx_pp ();
2732 pp_translate_identifiers (cxx_pp) = translate;
2733 if (v == 1
2734 && (DECL_CLASS_SCOPE_P (decl)
2735 || (DECL_NAMESPACE_SCOPE_P (decl)
2736 && CP_DECL_CONTEXT (decl) != global_namespace)))
2738 dump_type (cxx_pp, CP_DECL_CONTEXT (decl), TFF_PLAIN_IDENTIFIER);
2739 pp_cxx_colon_colon (cxx_pp);
2742 if (TREE_CODE (decl) == FUNCTION_DECL)
2743 dump_function_name (cxx_pp, decl, TFF_PLAIN_IDENTIFIER);
2744 else if ((DECL_NAME (decl) == NULL_TREE)
2745 && TREE_CODE (decl) == NAMESPACE_DECL)
2746 dump_decl (cxx_pp, decl, TFF_PLAIN_IDENTIFIER | TFF_UNQUALIFIED_NAME);
2747 else
2748 dump_decl (cxx_pp, DECL_NAME (decl), TFF_PLAIN_IDENTIFIER);
2750 return pp_ggc_formatted_text (cxx_pp);
2753 /* Return the location of a tree passed to %+ formats. */
2755 location_t
2756 location_of (tree t)
2758 if (TREE_CODE (t) == PARM_DECL && DECL_CONTEXT (t))
2759 t = DECL_CONTEXT (t);
2760 else if (TYPE_P (t))
2762 t = TYPE_MAIN_DECL (t);
2763 if (t == NULL_TREE)
2764 return input_location;
2766 else if (TREE_CODE (t) == OVERLOAD)
2767 t = OVL_FUNCTION (t);
2769 if (DECL_P (t))
2770 return DECL_SOURCE_LOCATION (t);
2771 return EXPR_LOC_OR_HERE (t);
2774 /* Now the interfaces from error et al to dump_type et al. Each takes an
2775 on/off VERBOSE flag and supply the appropriate TFF_ flags to a dump_
2776 function. */
2778 static const char *
2779 decl_to_string (tree decl, int verbose)
2781 int flags = 0;
2783 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == RECORD_TYPE
2784 || TREE_CODE (decl) == UNION_TYPE || TREE_CODE (decl) == ENUMERAL_TYPE)
2785 flags = TFF_CLASS_KEY_OR_ENUM;
2786 if (verbose)
2787 flags |= TFF_DECL_SPECIFIERS;
2788 else if (TREE_CODE (decl) == FUNCTION_DECL)
2789 flags |= TFF_DECL_SPECIFIERS | TFF_RETURN_TYPE;
2790 flags |= TFF_TEMPLATE_HEADER;
2792 reinit_cxx_pp ();
2793 dump_decl (cxx_pp, decl, flags);
2794 return pp_ggc_formatted_text (cxx_pp);
2797 static const char *
2798 expr_to_string (tree decl)
2800 reinit_cxx_pp ();
2801 dump_expr (cxx_pp, decl, 0);
2802 return pp_ggc_formatted_text (cxx_pp);
2805 static const char *
2806 fndecl_to_string (tree fndecl, int verbose)
2808 int flags;
2810 flags = TFF_EXCEPTION_SPECIFICATION | TFF_DECL_SPECIFIERS
2811 | TFF_TEMPLATE_HEADER;
2812 if (verbose)
2813 flags |= TFF_FUNCTION_DEFAULT_ARGUMENTS;
2814 reinit_cxx_pp ();
2815 dump_decl (cxx_pp, fndecl, flags);
2816 return pp_ggc_formatted_text (cxx_pp);
2820 static const char *
2821 code_to_string (enum tree_code c)
2823 return tree_code_name [c];
2826 const char *
2827 language_to_string (enum languages c)
2829 switch (c)
2831 case lang_c:
2832 return "C";
2834 case lang_cplusplus:
2835 return "C++";
2837 case lang_java:
2838 return "Java";
2840 default:
2841 gcc_unreachable ();
2843 return NULL;
2846 /* Return the proper printed version of a parameter to a C++ function. */
2848 static const char *
2849 parm_to_string (int p)
2851 reinit_cxx_pp ();
2852 if (p < 0)
2853 pp_string (cxx_pp, "'this'");
2854 else
2855 pp_decimal_int (cxx_pp, p + 1);
2856 return pp_ggc_formatted_text (cxx_pp);
2859 static const char *
2860 op_to_string (enum tree_code p)
2862 tree id = operator_name_info[p].identifier;
2863 return id ? IDENTIFIER_POINTER (id) : M_("<unknown>");
2866 static const char *
2867 type_to_string (tree typ, int verbose)
2869 int flags = 0;
2870 if (verbose)
2871 flags |= TFF_CLASS_KEY_OR_ENUM;
2872 flags |= TFF_TEMPLATE_HEADER;
2874 reinit_cxx_pp ();
2875 dump_type (cxx_pp, typ, flags);
2876 /* If we're printing a type that involves typedefs, also print the
2877 stripped version. But sometimes the stripped version looks
2878 exactly the same, so we don't want it after all. To avoid printing
2879 it in that case, we play ugly obstack games. */
2880 if (typ && TYPE_P (typ) && typ != TYPE_CANONICAL (typ)
2881 && !uses_template_parms (typ))
2883 int aka_start; char *p;
2884 struct obstack *ob = pp_buffer (cxx_pp)->obstack;
2885 /* Remember the end of the initial dump. */
2886 int len = obstack_object_size (ob);
2887 tree aka = strip_typedefs (typ);
2888 pp_string (cxx_pp, " {aka");
2889 pp_cxx_whitespace (cxx_pp);
2890 /* And remember the start of the aka dump. */
2891 aka_start = obstack_object_size (ob);
2892 dump_type (cxx_pp, aka, flags);
2893 pp_right_brace (cxx_pp);
2894 p = (char*)obstack_base (ob);
2895 /* If they are identical, cut off the aka with a NUL. */
2896 if (memcmp (p, p+aka_start, len) == 0)
2897 p[len] = '\0';
2899 return pp_ggc_formatted_text (cxx_pp);
2902 static const char *
2903 assop_to_string (enum tree_code p)
2905 tree id = assignment_operator_name_info[(int) p].identifier;
2906 return id ? IDENTIFIER_POINTER (id) : M_("{unknown}");
2909 static const char *
2910 args_to_string (tree p, int verbose)
2912 int flags = 0;
2913 if (verbose)
2914 flags |= TFF_CLASS_KEY_OR_ENUM;
2916 if (p == NULL_TREE)
2917 return "";
2919 if (TYPE_P (TREE_VALUE (p)))
2920 return type_as_string_translate (p, flags);
2922 reinit_cxx_pp ();
2923 for (; p; p = TREE_CHAIN (p))
2925 if (TREE_VALUE (p) == null_node)
2926 pp_cxx_ws_string (cxx_pp, "NULL");
2927 else
2928 dump_type (cxx_pp, error_type (TREE_VALUE (p)), flags);
2929 if (TREE_CHAIN (p))
2930 pp_separate_with_comma (cxx_pp);
2932 return pp_ggc_formatted_text (cxx_pp);
2935 /* Pretty-print a deduction substitution (from deduction_tsubst_fntype). P
2936 is a TREE_LIST with purpose the TEMPLATE_DECL, value the template
2937 arguments. */
2939 static const char *
2940 subst_to_string (tree p)
2942 tree decl = TREE_PURPOSE (p);
2943 tree targs = TREE_VALUE (p);
2944 tree tparms = DECL_TEMPLATE_PARMS (decl);
2945 int flags = (TFF_DECL_SPECIFIERS|TFF_TEMPLATE_HEADER
2946 |TFF_NO_TEMPLATE_BINDINGS);
2948 if (p == NULL_TREE)
2949 return "";
2951 reinit_cxx_pp ();
2952 dump_template_decl (cxx_pp, TREE_PURPOSE (p), flags);
2953 pp_cxx_whitespace (cxx_pp);
2954 pp_cxx_left_bracket (cxx_pp);
2955 pp_cxx_ws_string (cxx_pp, M_("with"));
2956 pp_cxx_whitespace (cxx_pp);
2957 dump_template_bindings (cxx_pp, tparms, targs, NULL);
2958 pp_cxx_right_bracket (cxx_pp);
2959 return pp_ggc_formatted_text (cxx_pp);
2962 static const char *
2963 cv_to_string (tree p, int v)
2965 reinit_cxx_pp ();
2966 cxx_pp->padding = v ? pp_before : pp_none;
2967 pp_cxx_cv_qualifier_seq (cxx_pp, p);
2968 return pp_ggc_formatted_text (cxx_pp);
2971 /* Langhook for print_error_function. */
2972 void
2973 cxx_print_error_function (diagnostic_context *context, const char *file,
2974 diagnostic_info *diagnostic)
2976 lhd_print_error_function (context, file, diagnostic);
2977 pp_set_prefix (context->printer, file);
2978 maybe_print_instantiation_context (context);
2981 static void
2982 cp_diagnostic_starter (diagnostic_context *context,
2983 diagnostic_info *diagnostic)
2985 diagnostic_report_current_module (context, diagnostic->location);
2986 cp_print_error_function (context, diagnostic);
2987 maybe_print_instantiation_context (context);
2988 maybe_print_constexpr_context (context);
2989 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
2990 diagnostic));
2993 static void
2994 cp_diagnostic_finalizer (diagnostic_context *context,
2995 diagnostic_info *diagnostic)
2997 virt_loc_aware_diagnostic_finalizer (context, diagnostic);
2998 pp_destroy_prefix (context->printer);
3001 /* Print current function onto BUFFER, in the process of reporting
3002 a diagnostic message. Called from cp_diagnostic_starter. */
3003 static void
3004 cp_print_error_function (diagnostic_context *context,
3005 diagnostic_info *diagnostic)
3007 /* If we are in an instantiation context, current_function_decl is likely
3008 to be wrong, so just rely on print_instantiation_full_context. */
3009 if (current_instantiation ())
3010 return;
3011 if (diagnostic_last_function_changed (context, diagnostic))
3013 const char *old_prefix = context->printer->prefix;
3014 const char *file = LOCATION_FILE (diagnostic->location);
3015 tree abstract_origin = diagnostic_abstract_origin (diagnostic);
3016 char *new_prefix = (file && abstract_origin == NULL)
3017 ? file_name_as_prefix (context, file) : NULL;
3019 pp_set_prefix (context->printer, new_prefix);
3021 if (current_function_decl == NULL)
3022 pp_string (context->printer, _("At global scope:"));
3023 else
3025 tree fndecl, ao;
3027 if (abstract_origin)
3029 ao = BLOCK_ABSTRACT_ORIGIN (abstract_origin);
3030 while (TREE_CODE (ao) == BLOCK
3031 && BLOCK_ABSTRACT_ORIGIN (ao)
3032 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
3033 ao = BLOCK_ABSTRACT_ORIGIN (ao);
3034 gcc_assert (TREE_CODE (ao) == FUNCTION_DECL);
3035 fndecl = ao;
3037 else
3038 fndecl = current_function_decl;
3040 pp_printf (context->printer, function_category (fndecl),
3041 cxx_printable_name_translate (fndecl, 2));
3043 while (abstract_origin)
3045 location_t *locus;
3046 tree block = abstract_origin;
3048 locus = &BLOCK_SOURCE_LOCATION (block);
3049 fndecl = NULL;
3050 block = BLOCK_SUPERCONTEXT (block);
3051 while (block && TREE_CODE (block) == BLOCK
3052 && BLOCK_ABSTRACT_ORIGIN (block))
3054 ao = BLOCK_ABSTRACT_ORIGIN (block);
3056 while (TREE_CODE (ao) == BLOCK
3057 && BLOCK_ABSTRACT_ORIGIN (ao)
3058 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
3059 ao = BLOCK_ABSTRACT_ORIGIN (ao);
3061 if (TREE_CODE (ao) == FUNCTION_DECL)
3063 fndecl = ao;
3064 break;
3066 else if (TREE_CODE (ao) != BLOCK)
3067 break;
3069 block = BLOCK_SUPERCONTEXT (block);
3071 if (fndecl)
3072 abstract_origin = block;
3073 else
3075 while (block && TREE_CODE (block) == BLOCK)
3076 block = BLOCK_SUPERCONTEXT (block);
3078 if (block && TREE_CODE (block) == FUNCTION_DECL)
3079 fndecl = block;
3080 abstract_origin = NULL;
3082 if (fndecl)
3084 expanded_location s = expand_location (*locus);
3085 pp_character (context->printer, ',');
3086 pp_newline (context->printer);
3087 if (s.file != NULL)
3089 if (context->show_column && s.column != 0)
3090 pp_printf (context->printer,
3091 _(" inlined from %qs at %r%s:%d:%d%R"),
3092 cxx_printable_name_translate (fndecl, 2),
3093 "locus", s.file, s.line, s.column);
3094 else
3095 pp_printf (context->printer,
3096 _(" inlined from %qs at %r%s:%d%R"),
3097 cxx_printable_name_translate (fndecl, 2),
3098 "locus", s.file, s.line);
3101 else
3102 pp_printf (context->printer, _(" inlined from %qs"),
3103 cxx_printable_name_translate (fndecl, 2));
3106 pp_character (context->printer, ':');
3108 pp_newline (context->printer);
3110 diagnostic_set_last_function (context, diagnostic);
3111 pp_destroy_prefix (context->printer);
3112 context->printer->prefix = old_prefix;
3116 /* Returns a description of FUNCTION using standard terminology. The
3117 result is a format string of the form "In CATEGORY %qs". */
3118 static const char *
3119 function_category (tree fn)
3121 /* We can get called from the middle-end for diagnostics of function
3122 clones. Make sure we have language specific information before
3123 dereferencing it. */
3124 if (DECL_LANG_SPECIFIC (STRIP_TEMPLATE (fn))
3125 && DECL_FUNCTION_MEMBER_P (fn))
3127 if (DECL_STATIC_FUNCTION_P (fn))
3128 return _("In static member function %qs");
3129 else if (DECL_COPY_CONSTRUCTOR_P (fn))
3130 return _("In copy constructor %qs");
3131 else if (DECL_CONSTRUCTOR_P (fn))
3132 return _("In constructor %qs");
3133 else if (DECL_DESTRUCTOR_P (fn))
3134 return _("In destructor %qs");
3135 else if (LAMBDA_FUNCTION_P (fn))
3136 return _("In lambda function");
3137 else
3138 return _("In member function %qs");
3140 else
3141 return _("In function %qs");
3144 /* Report the full context of a current template instantiation,
3145 onto BUFFER. */
3146 static void
3147 print_instantiation_full_context (diagnostic_context *context)
3149 struct tinst_level *p = current_instantiation ();
3150 location_t location = input_location;
3152 if (p)
3154 pp_verbatim (context->printer,
3155 TREE_CODE (p->decl) == TREE_LIST
3156 ? _("%s: In substitution of %qS:\n")
3157 : _("%s: In instantiation of %q#D:\n"),
3158 LOCATION_FILE (location),
3159 p->decl);
3161 location = p->locus;
3162 p = p->next;
3165 print_instantiation_partial_context (context, p, location);
3168 /* Helper function of print_instantiation_partial_context() that
3169 prints a single line of instantiation context. */
3171 static void
3172 print_instantiation_partial_context_line (diagnostic_context *context,
3173 const struct tinst_level *t,
3174 location_t loc, bool recursive_p)
3176 expanded_location xloc;
3177 xloc = expand_location (loc);
3179 if (context->show_column)
3180 pp_verbatim (context->printer, _("%r%s:%d:%d:%R "),
3181 "locus", xloc.file, xloc.line, xloc.column);
3182 else
3183 pp_verbatim (context->printer, _("%r%s:%d:%R "),
3184 "locus", xloc.file, xloc.line);
3186 if (t != NULL)
3188 if (TREE_CODE (t->decl) == TREE_LIST)
3189 pp_verbatim (context->printer,
3190 recursive_p
3191 ? _("recursively required by substitution of %qS\n")
3192 : _("required by substitution of %qS\n"),
3193 t->decl);
3194 else
3195 pp_verbatim (context->printer,
3196 recursive_p
3197 ? _("recursively required from %q#D\n")
3198 : _("required from %q#D\n"),
3199 t->decl);
3201 else
3203 pp_verbatim (context->printer,
3204 recursive_p
3205 ? _("recursively required from here")
3206 : _("required from here"));
3210 /* Same as print_instantiation_full_context but less verbose. */
3212 static void
3213 print_instantiation_partial_context (diagnostic_context *context,
3214 struct tinst_level *t0, location_t loc)
3216 struct tinst_level *t;
3217 int n_total = 0;
3218 int n;
3219 location_t prev_loc = loc;
3221 for (t = t0; t != NULL; t = t->next)
3222 if (prev_loc != t->locus)
3224 prev_loc = t->locus;
3225 n_total++;
3228 t = t0;
3230 if (template_backtrace_limit
3231 && n_total > template_backtrace_limit)
3233 int skip = n_total - template_backtrace_limit;
3234 int head = template_backtrace_limit / 2;
3236 /* Avoid skipping just 1. If so, skip 2. */
3237 if (skip == 1)
3239 skip = 2;
3240 head = (template_backtrace_limit - 1) / 2;
3243 for (n = 0; n < head; n++)
3245 gcc_assert (t != NULL);
3246 if (loc != t->locus)
3247 print_instantiation_partial_context_line (context, t, loc,
3248 /*recursive_p=*/false);
3249 loc = t->locus;
3250 t = t->next;
3252 if (t != NULL && skip > 0)
3254 expanded_location xloc;
3255 xloc = expand_location (loc);
3256 if (context->show_column)
3257 pp_verbatim (context->printer,
3258 _("%r%s:%d:%d:%R [ skipping %d instantiation "
3259 "contexts, use -ftemplate-backtrace-limit=0 to "
3260 "disable ]\n"),
3261 "locus", xloc.file, xloc.line, xloc.column, skip);
3262 else
3263 pp_verbatim (context->printer,
3264 _("%r%s:%d:%R [ skipping %d instantiation "
3265 "contexts, use -ftemplate-backtrace-limit=0 to "
3266 "disable ]\n"),
3267 "locus", xloc.file, xloc.line, skip);
3269 do {
3270 loc = t->locus;
3271 t = t->next;
3272 } while (t != NULL && --skip > 0);
3276 while (t != NULL)
3278 while (t->next != NULL && t->locus == t->next->locus)
3280 loc = t->locus;
3281 t = t->next;
3283 print_instantiation_partial_context_line (context, t, loc,
3284 t->locus == loc);
3285 loc = t->locus;
3286 t = t->next;
3288 print_instantiation_partial_context_line (context, NULL, loc,
3289 /*recursive_p=*/false);
3290 pp_newline (context->printer);
3293 /* Called from cp_thing to print the template context for an error. */
3294 static void
3295 maybe_print_instantiation_context (diagnostic_context *context)
3297 if (!problematic_instantiation_changed () || current_instantiation () == 0)
3298 return;
3300 record_last_problematic_instantiation ();
3301 print_instantiation_full_context (context);
3304 /* Report the bare minimum context of a template instantiation. */
3305 void
3306 print_instantiation_context (void)
3308 print_instantiation_partial_context
3309 (global_dc, current_instantiation (), input_location);
3310 pp_newline (global_dc->printer);
3311 diagnostic_flush_buffer (global_dc);
3314 /* Report what constexpr call(s) we're trying to expand, if any. */
3316 void
3317 maybe_print_constexpr_context (diagnostic_context *context)
3319 vec<tree> call_stack = cx_error_context ();
3320 unsigned ix;
3321 tree t;
3323 FOR_EACH_VEC_ELT (call_stack, ix, t)
3325 expanded_location xloc = expand_location (EXPR_LOCATION (t));
3326 const char *s = expr_as_string (t, 0);
3327 if (context->show_column)
3328 pp_verbatim (context->printer,
3329 _("%r%s:%d:%d:%R in constexpr expansion of %qs"),
3330 "locus", xloc.file, xloc.line, xloc.column, s);
3331 else
3332 pp_verbatim (context->printer,
3333 _("%r%s:%d:%R in constexpr expansion of %qs"),
3334 "locus", xloc.file, xloc.line, s);
3335 pp_newline (context->printer);
3339 /* Called from output_format -- during diagnostic message processing --
3340 to handle C++ specific format specifier with the following meanings:
3341 %A function argument-list.
3342 %C tree code.
3343 %D declaration.
3344 %E expression.
3345 %F function declaration.
3346 %L language as used in extern "lang".
3347 %O binary operator.
3348 %P function parameter whose position is indicated by an integer.
3349 %Q assignment operator.
3350 %T type.
3351 %V cv-qualifier. */
3352 static bool
3353 cp_printer (pretty_printer *pp, text_info *text, const char *spec,
3354 int precision, bool wide, bool set_locus, bool verbose)
3356 const char *result;
3357 tree t = NULL;
3358 #define next_tree (t = va_arg (*text->args_ptr, tree))
3359 #define next_tcode ((enum tree_code) va_arg (*text->args_ptr, int))
3360 #define next_lang ((enum languages) va_arg (*text->args_ptr, int))
3361 #define next_int va_arg (*text->args_ptr, int)
3363 if (precision != 0 || wide)
3364 return false;
3366 if (text->locus == NULL)
3367 set_locus = false;
3369 switch (*spec)
3371 case 'A': result = args_to_string (next_tree, verbose); break;
3372 case 'C': result = code_to_string (next_tcode); break;
3373 case 'D':
3375 tree temp = next_tree;
3376 if (VAR_P (temp)
3377 && DECL_HAS_DEBUG_EXPR_P (temp))
3379 temp = DECL_DEBUG_EXPR (temp);
3380 if (!DECL_P (temp))
3382 result = expr_to_string (temp);
3383 break;
3386 result = decl_to_string (temp, verbose);
3388 break;
3389 case 'E': result = expr_to_string (next_tree); break;
3390 case 'F': result = fndecl_to_string (next_tree, verbose); break;
3391 case 'L': result = language_to_string (next_lang); break;
3392 case 'O': result = op_to_string (next_tcode); break;
3393 case 'P': result = parm_to_string (next_int); break;
3394 case 'Q': result = assop_to_string (next_tcode); break;
3395 case 'S': result = subst_to_string (next_tree); break;
3396 case 'T': result = type_to_string (next_tree, verbose); break;
3397 case 'V': result = cv_to_string (next_tree, verbose); break;
3399 case 'K':
3400 percent_K_format (text);
3401 return true;
3403 default:
3404 return false;
3407 pp_string (pp, result);
3408 if (set_locus && t != NULL)
3409 *text->locus = location_of (t);
3410 return true;
3411 #undef next_tree
3412 #undef next_tcode
3413 #undef next_lang
3414 #undef next_int
3417 /* Warn about the use of C++0x features when appropriate. */
3418 void
3419 maybe_warn_cpp0x (cpp0x_warn_str str)
3421 if ((cxx_dialect == cxx98) && !in_system_header)
3422 /* We really want to suppress this warning in system headers,
3423 because libstdc++ uses variadic templates even when we aren't
3424 in C++0x mode. */
3425 switch (str)
3427 case CPP0X_INITIALIZER_LISTS:
3428 pedwarn (input_location, 0,
3429 "extended initializer lists "
3430 "only available with -std=c++11 or -std=gnu++11");
3431 break;
3432 case CPP0X_EXPLICIT_CONVERSION:
3433 pedwarn (input_location, 0,
3434 "explicit conversion operators "
3435 "only available with -std=c++11 or -std=gnu++11");
3436 break;
3437 case CPP0X_VARIADIC_TEMPLATES:
3438 pedwarn (input_location, 0,
3439 "variadic templates "
3440 "only available with -std=c++11 or -std=gnu++11");
3441 break;
3442 case CPP0X_LAMBDA_EXPR:
3443 pedwarn (input_location, 0,
3444 "lambda expressions "
3445 "only available with -std=c++11 or -std=gnu++11");
3446 break;
3447 case CPP0X_AUTO:
3448 pedwarn (input_location, 0,
3449 "C++11 auto only available with -std=c++11 or -std=gnu++11");
3450 break;
3451 case CPP0X_SCOPED_ENUMS:
3452 pedwarn (input_location, 0,
3453 "scoped enums only available with -std=c++11 or -std=gnu++11");
3454 break;
3455 case CPP0X_DEFAULTED_DELETED:
3456 pedwarn (input_location, 0,
3457 "defaulted and deleted functions "
3458 "only available with -std=c++11 or -std=gnu++11");
3459 break;
3460 case CPP0X_INLINE_NAMESPACES:
3461 pedwarn (input_location, OPT_Wpedantic,
3462 "inline namespaces "
3463 "only available with -std=c++11 or -std=gnu++11");
3464 break;
3465 case CPP0X_OVERRIDE_CONTROLS:
3466 pedwarn (input_location, 0,
3467 "override controls (override/final) "
3468 "only available with -std=c++11 or -std=gnu++11");
3469 break;
3470 case CPP0X_NSDMI:
3471 pedwarn (input_location, 0,
3472 "non-static data member initializers "
3473 "only available with -std=c++11 or -std=gnu++11");
3474 break;
3475 case CPP0X_USER_DEFINED_LITERALS:
3476 pedwarn (input_location, 0,
3477 "user-defined literals "
3478 "only available with -std=c++11 or -std=gnu++11");
3479 break;
3480 case CPP0X_DELEGATING_CTORS:
3481 pedwarn (input_location, 0,
3482 "delegating constructors "
3483 "only available with -std=c++11 or -std=gnu++11");
3484 break;
3485 case CPP0X_INHERITING_CTORS:
3486 pedwarn (input_location, 0,
3487 "inheriting constructors "
3488 "only available with -std=c++11 or -std=gnu++11");
3489 break;
3490 case CPP0X_ATTRIBUTES:
3491 pedwarn (input_location, 0,
3492 "c++11 attributes "
3493 "only available with -std=c++11 or -std=gnu++11");
3494 break;
3495 case CPP0X_REF_QUALIFIER:
3496 pedwarn (input_location, 0,
3497 "ref-qualifiers "
3498 "only available with -std=c++11 or -std=gnu++11");
3499 break;
3500 default:
3501 gcc_unreachable ();
3505 /* Warn about the use of variadic templates when appropriate. */
3506 void
3507 maybe_warn_variadic_templates (void)
3509 maybe_warn_cpp0x (CPP0X_VARIADIC_TEMPLATES);
3513 /* Issue an ISO C++98 pedantic warning at LOCATION, conditional on
3514 option OPT with text GMSGID. Use this function to report
3515 diagnostics for constructs that are invalid C++98, but valid
3516 C++0x. */
3517 bool
3518 pedwarn_cxx98 (location_t location, int opt, const char *gmsgid, ...)
3520 diagnostic_info diagnostic;
3521 va_list ap;
3522 bool ret;
3524 va_start (ap, gmsgid);
3525 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
3526 (cxx_dialect == cxx98) ? DK_PEDWARN : DK_WARNING);
3527 diagnostic.option_index = opt;
3528 ret = report_diagnostic (&diagnostic);
3529 va_end (ap);
3530 return ret;
3533 /* Issue a diagnostic that NAME cannot be found in SCOPE. DECL is what
3534 we found when we tried to do the lookup. LOCATION is the location of
3535 the NAME identifier. */
3537 void
3538 qualified_name_lookup_error (tree scope, tree name,
3539 tree decl, location_t location)
3541 if (scope == error_mark_node)
3542 ; /* We already complained. */
3543 else if (TYPE_P (scope))
3545 if (!COMPLETE_TYPE_P (scope))
3546 error_at (location, "incomplete type %qT used in nested name specifier",
3547 scope);
3548 else if (TREE_CODE (decl) == TREE_LIST)
3550 error_at (location, "reference to %<%T::%D%> is ambiguous",
3551 scope, name);
3552 print_candidates (decl);
3554 else
3555 error_at (location, "%qD is not a member of %qT", name, scope);
3557 else if (scope != global_namespace)
3559 error_at (location, "%qD is not a member of %qD", name, scope);
3560 suggest_alternatives_for (location, name);
3562 else
3564 error_at (location, "%<::%D%> has not been declared", name);
3565 suggest_alternatives_for (location, name);