PR target/54640
[official-gcc.git] / gcc / cp / error.c
blob60119ec6302a4e77eccced8280c94380c30a95d3
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 #define pp_separate_with_comma(PP) pp_cxx_separate_with (PP, ',')
37 #define pp_separate_with_semicolon(PP) pp_cxx_separate_with (PP, ';')
39 /* The global buffer where we dump everything. It is there only for
40 transitional purpose. It is expected, in the near future, to be
41 completely removed. */
42 static cxx_pretty_printer scratch_pretty_printer;
43 #define cxx_pp (&scratch_pretty_printer)
45 /* Translate if being used for diagnostics, but not for dump files or
46 __PRETTY_FUNCTION. */
47 #define M_(msgid) (pp_translate_identifiers (cxx_pp) ? _(msgid) : (msgid))
49 # define NEXT_CODE(T) (TREE_CODE (TREE_TYPE (T)))
51 static const char *args_to_string (tree, int);
52 static const char *assop_to_string (enum tree_code);
53 static const char *code_to_string (enum tree_code);
54 static const char *cv_to_string (tree, int);
55 static const char *decl_to_string (tree, int);
56 static const char *expr_to_string (tree);
57 static const char *fndecl_to_string (tree, int);
58 static const char *op_to_string (enum tree_code);
59 static const char *parm_to_string (int);
60 static const char *type_to_string (tree, int);
62 static void dump_alias_template_specialization (tree, int);
63 static void dump_type (tree, int);
64 static void dump_typename (tree, int);
65 static void dump_simple_decl (tree, tree, int);
66 static void dump_decl (tree, int);
67 static void dump_template_decl (tree, int);
68 static void dump_function_decl (tree, int);
69 static void dump_expr (tree, int);
70 static void dump_unary_op (const char *, tree, int);
71 static void dump_binary_op (const char *, tree, int);
72 static void dump_aggr_type (tree, int);
73 static void dump_type_prefix (tree, int);
74 static void dump_type_suffix (tree, int);
75 static void dump_function_name (tree, int);
76 static void dump_call_expr_args (tree, int, bool);
77 static void dump_aggr_init_expr_args (tree, int, bool);
78 static void dump_expr_list (tree, int);
79 static void dump_global_iord (tree);
80 static void dump_parameters (tree, int);
81 static void dump_exception_spec (tree, int);
82 static void dump_template_argument (tree, int);
83 static void dump_template_argument_list (tree, int);
84 static void dump_template_parameter (tree, int);
85 static void dump_template_bindings (tree, tree, vec<tree, va_gc> *);
86 static void dump_scope (tree, int);
87 static void dump_template_parms (tree, int, int);
88 static int get_non_default_template_args_count (tree, int);
89 static const char *function_category (tree);
90 static void maybe_print_constexpr_context (diagnostic_context *);
91 static void maybe_print_instantiation_context (diagnostic_context *);
92 static void print_instantiation_full_context (diagnostic_context *);
93 static void print_instantiation_partial_context (diagnostic_context *,
94 struct tinst_level *,
95 location_t);
96 static void cp_diagnostic_starter (diagnostic_context *, diagnostic_info *);
97 static void cp_diagnostic_finalizer (diagnostic_context *, diagnostic_info *);
98 static void cp_print_error_function (diagnostic_context *, diagnostic_info *);
100 static bool cp_printer (pretty_printer *, text_info *, const char *,
101 int, bool, bool, bool);
103 void
104 init_error (void)
106 diagnostic_starter (global_dc) = cp_diagnostic_starter;
107 diagnostic_finalizer (global_dc) = cp_diagnostic_finalizer;
108 diagnostic_format_decoder (global_dc) = cp_printer;
110 pp_construct (pp_base (cxx_pp), NULL, 0);
111 pp_cxx_pretty_printer_init (cxx_pp);
114 /* Dump a scope, if deemed necessary. */
116 static void
117 dump_scope (tree scope, int flags)
119 int f = flags & (TFF_SCOPE | TFF_CHASE_TYPEDEF);
121 if (scope == NULL_TREE)
122 return;
124 if (TREE_CODE (scope) == NAMESPACE_DECL)
126 if (scope != global_namespace)
128 dump_decl (scope, f);
129 pp_cxx_colon_colon (cxx_pp);
132 else if (AGGREGATE_TYPE_P (scope))
134 dump_type (scope, f);
135 pp_cxx_colon_colon (cxx_pp);
137 else if ((flags & TFF_SCOPE) && TREE_CODE (scope) == FUNCTION_DECL)
139 dump_function_decl (scope, f);
140 pp_cxx_colon_colon (cxx_pp);
144 /* Dump the template ARGument under control of FLAGS. */
146 static void
147 dump_template_argument (tree arg, int flags)
149 if (ARGUMENT_PACK_P (arg))
150 dump_template_argument_list (ARGUMENT_PACK_ARGS (arg),
151 /* No default args in argument packs. */
152 flags|TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS);
153 else if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
154 dump_type (arg, flags & ~TFF_CLASS_KEY_OR_ENUM);
155 else
157 if (TREE_CODE (arg) == TREE_LIST)
158 arg = TREE_VALUE (arg);
160 dump_expr (arg, (flags | TFF_EXPR_IN_PARENS) & ~TFF_CLASS_KEY_OR_ENUM);
164 /* Count the number of template arguments ARGS whose value does not
165 match the (optional) default template parameter in PARAMS */
167 static int
168 get_non_default_template_args_count (tree args, int flags)
170 int n = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_ARGS (args));
172 if (/* We use this flag when generating debug information. We don't
173 want to expand templates at this point, for this may generate
174 new decls, which gets decl counts out of sync, which may in
175 turn cause codegen differences between compilations with and
176 without -g. */
177 (flags & TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS) != 0
178 || !flag_pretty_templates)
179 return n;
181 return GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (INNERMOST_TEMPLATE_ARGS (args));
184 /* Dump a template-argument-list ARGS (always a TREE_VEC) under control
185 of FLAGS. */
187 static void
188 dump_template_argument_list (tree args, int flags)
190 int n = get_non_default_template_args_count (args, flags);
191 int need_comma = 0;
192 int i;
194 for (i = 0; i < n; ++i)
196 tree arg = TREE_VEC_ELT (args, i);
198 /* Only print a comma if we know there is an argument coming. In
199 the case of an empty template argument pack, no actual
200 argument will be printed. */
201 if (need_comma
202 && (!ARGUMENT_PACK_P (arg)
203 || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) > 0))
204 pp_separate_with_comma (cxx_pp);
206 dump_template_argument (arg, flags);
207 need_comma = 1;
211 /* Dump a template parameter PARM (a TREE_LIST) under control of FLAGS. */
213 static void
214 dump_template_parameter (tree parm, int flags)
216 tree p;
217 tree a;
219 if (parm == error_mark_node)
220 return;
222 p = TREE_VALUE (parm);
223 a = TREE_PURPOSE (parm);
225 if (TREE_CODE (p) == TYPE_DECL)
227 if (flags & TFF_DECL_SPECIFIERS)
229 pp_cxx_ws_string (cxx_pp, "class");
230 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (p)))
231 pp_cxx_ws_string (cxx_pp, "...");
232 if (DECL_NAME (p))
233 pp_cxx_tree_identifier (cxx_pp, DECL_NAME (p));
235 else if (DECL_NAME (p))
236 pp_cxx_tree_identifier (cxx_pp, DECL_NAME (p));
237 else
238 pp_cxx_canonical_template_parameter (cxx_pp, TREE_TYPE (p));
240 else
241 dump_decl (p, flags | TFF_DECL_SPECIFIERS);
243 if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && a != NULL_TREE)
245 pp_cxx_whitespace (cxx_pp);
246 pp_equal (cxx_pp);
247 pp_cxx_whitespace (cxx_pp);
248 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
249 dump_type (a, flags & ~TFF_CHASE_TYPEDEF);
250 else
251 dump_expr (a, flags | TFF_EXPR_IN_PARENS);
255 /* Dump, under control of FLAGS, a template-parameter-list binding.
256 PARMS is a TREE_LIST of TREE_VEC of TREE_LIST and ARGS is a
257 TREE_VEC. */
259 static void
260 dump_template_bindings (tree parms, tree args, vec<tree, va_gc> *typenames)
262 bool need_semicolon = false;
263 int i;
264 tree t;
266 while (parms)
268 tree p = TREE_VALUE (parms);
269 int lvl = TMPL_PARMS_DEPTH (parms);
270 int arg_idx = 0;
271 int i;
272 tree lvl_args = NULL_TREE;
274 /* Don't crash if we had an invalid argument list. */
275 if (TMPL_ARGS_DEPTH (args) >= lvl)
276 lvl_args = TMPL_ARGS_LEVEL (args, lvl);
278 for (i = 0; i < TREE_VEC_LENGTH (p); ++i)
280 tree arg = NULL_TREE;
282 /* Don't crash if we had an invalid argument list. */
283 if (lvl_args && NUM_TMPL_ARGS (lvl_args) > arg_idx)
284 arg = TREE_VEC_ELT (lvl_args, arg_idx);
286 if (need_semicolon)
287 pp_separate_with_semicolon (cxx_pp);
288 dump_template_parameter (TREE_VEC_ELT (p, i), TFF_PLAIN_IDENTIFIER);
289 pp_cxx_whitespace (cxx_pp);
290 pp_equal (cxx_pp);
291 pp_cxx_whitespace (cxx_pp);
292 if (arg)
294 if (ARGUMENT_PACK_P (arg))
295 pp_cxx_left_brace (cxx_pp);
296 dump_template_argument (arg, TFF_PLAIN_IDENTIFIER);
297 if (ARGUMENT_PACK_P (arg))
298 pp_cxx_right_brace (cxx_pp);
300 else
301 pp_string (cxx_pp, M_("<missing>"));
303 ++arg_idx;
304 need_semicolon = true;
307 parms = TREE_CHAIN (parms);
310 /* Don't bother with typenames for a partial instantiation. */
311 if (vec_safe_is_empty (typenames) || uses_template_parms (args))
312 return;
314 FOR_EACH_VEC_SAFE_ELT (typenames, i, t)
316 if (need_semicolon)
317 pp_separate_with_semicolon (cxx_pp);
318 dump_type (t, TFF_PLAIN_IDENTIFIER);
319 pp_cxx_whitespace (cxx_pp);
320 pp_equal (cxx_pp);
321 pp_cxx_whitespace (cxx_pp);
322 push_deferring_access_checks (dk_no_check);
323 t = tsubst (t, args, tf_none, NULL_TREE);
324 pop_deferring_access_checks ();
325 /* Strip typedefs. We can't just use TFF_CHASE_TYPEDEF because
326 pp_simple_type_specifier doesn't know about it. */
327 t = strip_typedefs (t);
328 dump_type (t, TFF_PLAIN_IDENTIFIER);
332 /* Dump a human-readable equivalent of the alias template
333 specialization of T. */
335 static void
336 dump_alias_template_specialization (tree t, int flags)
338 tree name;
340 gcc_assert (alias_template_specialization_p (t));
342 if (!(flags & TFF_UNQUALIFIED_NAME))
343 dump_scope (CP_DECL_CONTEXT (TYPE_NAME (t)), flags);
344 name = TYPE_IDENTIFIER (t);
345 pp_cxx_tree_identifier (cxx_pp, name);
346 dump_template_parms (TYPE_TEMPLATE_INFO (t),
347 /*primary=*/false,
348 flags & ~TFF_TEMPLATE_HEADER);
351 /* Dump a human-readable equivalent of TYPE. FLAGS controls the
352 format. */
354 static void
355 dump_type (tree t, int flags)
357 if (t == NULL_TREE)
358 return;
360 /* Don't print e.g. "struct mytypedef". */
361 if (TYPE_P (t) && typedef_variant_p (t))
363 tree decl = TYPE_NAME (t);
364 if ((flags & TFF_CHASE_TYPEDEF)
365 || DECL_SELF_REFERENCE_P (decl)
366 || (!flag_pretty_templates
367 && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)))
368 t = strip_typedefs (t);
369 else if (alias_template_specialization_p (t))
371 dump_alias_template_specialization (t, flags);
372 return;
374 else if (same_type_p (t, TREE_TYPE (decl)))
375 t = decl;
376 else
378 pp_cxx_cv_qualifier_seq (cxx_pp, t);
379 pp_cxx_tree_identifier (cxx_pp, TYPE_IDENTIFIER (t));
380 return;
384 if (TYPE_PTRMEMFUNC_P (t))
385 goto offset_type;
387 switch (TREE_CODE (t))
389 case LANG_TYPE:
390 if (t == init_list_type_node)
391 pp_string (cxx_pp, M_("<brace-enclosed initializer list>"));
392 else if (t == unknown_type_node)
393 pp_string (cxx_pp, M_("<unresolved overloaded function type>"));
394 else
396 pp_cxx_cv_qualifier_seq (cxx_pp, t);
397 pp_cxx_tree_identifier (cxx_pp, TYPE_IDENTIFIER (t));
399 break;
401 case TREE_LIST:
402 /* A list of function parms. */
403 dump_parameters (t, flags);
404 break;
406 case IDENTIFIER_NODE:
407 pp_cxx_tree_identifier (cxx_pp, t);
408 break;
410 case TREE_BINFO:
411 dump_type (BINFO_TYPE (t), flags);
412 break;
414 case RECORD_TYPE:
415 case UNION_TYPE:
416 case ENUMERAL_TYPE:
417 dump_aggr_type (t, flags);
418 break;
420 case TYPE_DECL:
421 if (flags & TFF_CHASE_TYPEDEF)
423 dump_type (DECL_ORIGINAL_TYPE (t)
424 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t), flags);
425 break;
427 /* Else fall through. */
429 case TEMPLATE_DECL:
430 case NAMESPACE_DECL:
431 dump_decl (t, flags & ~TFF_DECL_SPECIFIERS);
432 break;
434 case INTEGER_TYPE:
435 case REAL_TYPE:
436 case VOID_TYPE:
437 case BOOLEAN_TYPE:
438 case COMPLEX_TYPE:
439 case VECTOR_TYPE:
440 case FIXED_POINT_TYPE:
441 pp_type_specifier_seq (cxx_pp, t);
442 break;
444 case TEMPLATE_TEMPLATE_PARM:
445 /* For parameters inside template signature. */
446 if (TYPE_IDENTIFIER (t))
447 pp_cxx_tree_identifier (cxx_pp, TYPE_IDENTIFIER (t));
448 else
449 pp_cxx_canonical_template_parameter (cxx_pp, t);
450 break;
452 case BOUND_TEMPLATE_TEMPLATE_PARM:
454 tree args = TYPE_TI_ARGS (t);
455 pp_cxx_cv_qualifier_seq (cxx_pp, t);
456 pp_cxx_tree_identifier (cxx_pp, TYPE_IDENTIFIER (t));
457 pp_cxx_begin_template_argument_list (cxx_pp);
458 dump_template_argument_list (args, flags);
459 pp_cxx_end_template_argument_list (cxx_pp);
461 break;
463 case TEMPLATE_TYPE_PARM:
464 pp_cxx_cv_qualifier_seq (cxx_pp, t);
465 if (TYPE_IDENTIFIER (t))
466 pp_cxx_tree_identifier (cxx_pp, TYPE_IDENTIFIER (t));
467 else
468 pp_cxx_canonical_template_parameter
469 (cxx_pp, TEMPLATE_TYPE_PARM_INDEX (t));
470 break;
472 /* This is not always necessary for pointers and such, but doing this
473 reduces code size. */
474 case ARRAY_TYPE:
475 case POINTER_TYPE:
476 case REFERENCE_TYPE:
477 case OFFSET_TYPE:
478 offset_type:
479 case FUNCTION_TYPE:
480 case METHOD_TYPE:
482 dump_type_prefix (t, flags);
483 dump_type_suffix (t, flags);
484 break;
486 case TYPENAME_TYPE:
487 if (! (flags & TFF_CHASE_TYPEDEF)
488 && DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
490 dump_decl (TYPE_NAME (t), TFF_PLAIN_IDENTIFIER);
491 break;
493 pp_cxx_cv_qualifier_seq (cxx_pp, t);
494 pp_cxx_ws_string (cxx_pp,
495 TYPENAME_IS_ENUM_P (t) ? "enum"
496 : TYPENAME_IS_CLASS_P (t) ? "class"
497 : "typename");
498 dump_typename (t, flags);
499 break;
501 case UNBOUND_CLASS_TEMPLATE:
502 if (! (flags & TFF_UNQUALIFIED_NAME))
504 dump_type (TYPE_CONTEXT (t), flags);
505 pp_cxx_colon_colon (cxx_pp);
507 pp_cxx_ws_string (cxx_pp, "template");
508 dump_type (DECL_NAME (TYPE_NAME (t)), flags);
509 break;
511 case TYPEOF_TYPE:
512 pp_cxx_ws_string (cxx_pp, "__typeof__");
513 pp_cxx_whitespace (cxx_pp);
514 pp_cxx_left_paren (cxx_pp);
515 dump_expr (TYPEOF_TYPE_EXPR (t), flags & ~TFF_EXPR_IN_PARENS);
516 pp_cxx_right_paren (cxx_pp);
517 break;
519 case UNDERLYING_TYPE:
520 pp_cxx_ws_string (cxx_pp, "__underlying_type");
521 pp_cxx_whitespace (cxx_pp);
522 pp_cxx_left_paren (cxx_pp);
523 dump_expr (UNDERLYING_TYPE_TYPE (t), flags & ~TFF_EXPR_IN_PARENS);
524 pp_cxx_right_paren (cxx_pp);
525 break;
527 case TYPE_PACK_EXPANSION:
528 dump_type (PACK_EXPANSION_PATTERN (t), flags);
529 pp_cxx_ws_string (cxx_pp, "...");
530 break;
532 case TYPE_ARGUMENT_PACK:
533 dump_template_argument (t, flags);
534 break;
536 case DECLTYPE_TYPE:
537 pp_cxx_ws_string (cxx_pp, "decltype");
538 pp_cxx_whitespace (cxx_pp);
539 pp_cxx_left_paren (cxx_pp);
540 dump_expr (DECLTYPE_TYPE_EXPR (t), flags & ~TFF_EXPR_IN_PARENS);
541 pp_cxx_right_paren (cxx_pp);
542 break;
544 case NULLPTR_TYPE:
545 pp_string (cxx_pp, "std::nullptr_t");
546 break;
548 default:
549 pp_unsupported_tree (cxx_pp, t);
550 /* Fall through to error. */
552 case ERROR_MARK:
553 pp_string (cxx_pp, M_("<type error>"));
554 break;
558 /* Dump a TYPENAME_TYPE. We need to notice when the context is itself
559 a TYPENAME_TYPE. */
561 static void
562 dump_typename (tree t, int flags)
564 tree ctx = TYPE_CONTEXT (t);
566 if (TREE_CODE (ctx) == TYPENAME_TYPE)
567 dump_typename (ctx, flags);
568 else
569 dump_type (ctx, flags & ~TFF_CLASS_KEY_OR_ENUM);
570 pp_cxx_colon_colon (cxx_pp);
571 dump_decl (TYPENAME_TYPE_FULLNAME (t), flags);
574 /* Return the name of the supplied aggregate, or enumeral type. */
576 const char *
577 class_key_or_enum_as_string (tree t)
579 if (TREE_CODE (t) == ENUMERAL_TYPE)
581 if (SCOPED_ENUM_P (t))
582 return "enum class";
583 else
584 return "enum";
586 else if (TREE_CODE (t) == UNION_TYPE)
587 return "union";
588 else if (TYPE_LANG_SPECIFIC (t) && CLASSTYPE_DECLARED_CLASS (t))
589 return "class";
590 else
591 return "struct";
594 /* Print out a class declaration T under the control of FLAGS,
595 in the form `class foo'. */
597 static void
598 dump_aggr_type (tree t, int flags)
600 tree name;
601 const char *variety = class_key_or_enum_as_string (t);
602 int typdef = 0;
603 int tmplate = 0;
605 pp_cxx_cv_qualifier_seq (cxx_pp, t);
607 if (flags & TFF_CLASS_KEY_OR_ENUM)
608 pp_cxx_ws_string (cxx_pp, variety);
610 name = TYPE_NAME (t);
612 if (name)
614 typdef = (!DECL_ARTIFICIAL (name)
615 /* An alias specialization is not considered to be a
616 typedef. */
617 && !alias_template_specialization_p (t));
619 if ((typdef
620 && ((flags & TFF_CHASE_TYPEDEF)
621 || (!flag_pretty_templates && DECL_LANG_SPECIFIC (name)
622 && DECL_TEMPLATE_INFO (name))))
623 || DECL_SELF_REFERENCE_P (name))
625 t = TYPE_MAIN_VARIANT (t);
626 name = TYPE_NAME (t);
627 typdef = 0;
630 tmplate = !typdef && TREE_CODE (t) != ENUMERAL_TYPE
631 && TYPE_LANG_SPECIFIC (t) && CLASSTYPE_TEMPLATE_INFO (t)
632 && (TREE_CODE (CLASSTYPE_TI_TEMPLATE (t)) != TEMPLATE_DECL
633 || PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
635 if (! (flags & TFF_UNQUALIFIED_NAME))
636 dump_scope (CP_DECL_CONTEXT (name), flags | TFF_SCOPE);
637 flags &= ~TFF_UNQUALIFIED_NAME;
638 if (tmplate)
640 /* Because the template names are mangled, we have to locate
641 the most general template, and use that name. */
642 tree tpl = TYPE_TI_TEMPLATE (t);
644 while (DECL_TEMPLATE_INFO (tpl))
645 tpl = DECL_TI_TEMPLATE (tpl);
646 name = tpl;
648 name = DECL_NAME (name);
651 if (name == 0 || ANON_AGGRNAME_P (name))
653 if (flags & TFF_CLASS_KEY_OR_ENUM)
654 pp_string (cxx_pp, M_("<anonymous>"));
655 else
656 pp_printf (pp_base (cxx_pp), M_("<anonymous %s>"), variety);
658 else if (LAMBDA_TYPE_P (name))
660 /* A lambda's "type" is essentially its signature. */
661 pp_string (cxx_pp, M_("<lambda"));
662 if (lambda_function (t))
663 dump_parameters (FUNCTION_FIRST_USER_PARMTYPE (lambda_function (t)),
664 flags);
665 pp_character(cxx_pp, '>');
667 else
668 pp_cxx_tree_identifier (cxx_pp, name);
669 if (tmplate)
670 dump_template_parms (TYPE_TEMPLATE_INFO (t),
671 !CLASSTYPE_USE_TEMPLATE (t),
672 flags & ~TFF_TEMPLATE_HEADER);
675 /* Dump into the obstack the initial part of the output for a given type.
676 This is necessary when dealing with things like functions returning
677 functions. Examples:
679 return type of `int (* fee ())()': pointer -> function -> int. Both
680 pointer (and reference and offset) and function (and member) types must
681 deal with prefix and suffix.
683 Arrays must also do this for DECL nodes, like int a[], and for things like
684 int *[]&. */
686 static void
687 dump_type_prefix (tree t, int flags)
689 if (TYPE_PTRMEMFUNC_P (t))
691 t = TYPE_PTRMEMFUNC_FN_TYPE (t);
692 goto offset_type;
695 switch (TREE_CODE (t))
697 case POINTER_TYPE:
698 case REFERENCE_TYPE:
700 tree sub = TREE_TYPE (t);
702 dump_type_prefix (sub, flags);
703 if (TREE_CODE (sub) == ARRAY_TYPE
704 || TREE_CODE (sub) == FUNCTION_TYPE)
706 pp_cxx_whitespace (cxx_pp);
707 pp_cxx_left_paren (cxx_pp);
708 pp_c_attributes_display (pp_c_base (cxx_pp),
709 TYPE_ATTRIBUTES (sub));
711 if (TREE_CODE (t) == POINTER_TYPE)
712 pp_character(cxx_pp, '*');
713 else if (TREE_CODE (t) == REFERENCE_TYPE)
715 if (TYPE_REF_IS_RVALUE (t))
716 pp_string (cxx_pp, "&&");
717 else
718 pp_character (cxx_pp, '&');
720 pp_base (cxx_pp)->padding = pp_before;
721 pp_cxx_cv_qualifier_seq (cxx_pp, t);
723 break;
725 case OFFSET_TYPE:
726 offset_type:
727 dump_type_prefix (TREE_TYPE (t), flags);
728 if (TREE_CODE (t) == OFFSET_TYPE) /* pmfs deal with this in d_t_p */
730 pp_maybe_space (cxx_pp);
731 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
732 pp_cxx_left_paren (cxx_pp);
733 dump_type (TYPE_OFFSET_BASETYPE (t), flags);
734 pp_cxx_colon_colon (cxx_pp);
736 pp_cxx_star (cxx_pp);
737 pp_cxx_cv_qualifier_seq (cxx_pp, t);
738 pp_base (cxx_pp)->padding = pp_before;
739 break;
741 /* This can be reached without a pointer when dealing with
742 templates, e.g. std::is_function. */
743 case FUNCTION_TYPE:
744 dump_type_prefix (TREE_TYPE (t), flags);
745 break;
747 case METHOD_TYPE:
748 dump_type_prefix (TREE_TYPE (t), flags);
749 pp_maybe_space (cxx_pp);
750 pp_cxx_left_paren (cxx_pp);
751 dump_aggr_type (TYPE_METHOD_BASETYPE (t), flags);
752 pp_cxx_colon_colon (cxx_pp);
753 break;
755 case ARRAY_TYPE:
756 dump_type_prefix (TREE_TYPE (t), flags);
757 break;
759 case ENUMERAL_TYPE:
760 case IDENTIFIER_NODE:
761 case INTEGER_TYPE:
762 case BOOLEAN_TYPE:
763 case REAL_TYPE:
764 case RECORD_TYPE:
765 case TEMPLATE_TYPE_PARM:
766 case TEMPLATE_TEMPLATE_PARM:
767 case BOUND_TEMPLATE_TEMPLATE_PARM:
768 case TREE_LIST:
769 case TYPE_DECL:
770 case TREE_VEC:
771 case UNION_TYPE:
772 case LANG_TYPE:
773 case VOID_TYPE:
774 case TYPENAME_TYPE:
775 case COMPLEX_TYPE:
776 case VECTOR_TYPE:
777 case TYPEOF_TYPE:
778 case UNDERLYING_TYPE:
779 case DECLTYPE_TYPE:
780 case TYPE_PACK_EXPANSION:
781 case FIXED_POINT_TYPE:
782 case NULLPTR_TYPE:
783 dump_type (t, flags);
784 pp_base (cxx_pp)->padding = pp_before;
785 break;
787 default:
788 pp_unsupported_tree (cxx_pp, t);
789 /* fall through. */
790 case ERROR_MARK:
791 pp_string (cxx_pp, M_("<typeprefixerror>"));
792 break;
796 /* Dump the suffix of type T, under control of FLAGS. This is the part
797 which appears after the identifier (or function parms). */
799 static void
800 dump_type_suffix (tree t, int flags)
802 if (TYPE_PTRMEMFUNC_P (t))
803 t = TYPE_PTRMEMFUNC_FN_TYPE (t);
805 switch (TREE_CODE (t))
807 case POINTER_TYPE:
808 case REFERENCE_TYPE:
809 case OFFSET_TYPE:
810 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
811 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
812 pp_cxx_right_paren (cxx_pp);
813 dump_type_suffix (TREE_TYPE (t), flags);
814 break;
816 case FUNCTION_TYPE:
817 case METHOD_TYPE:
819 tree arg;
820 if (TREE_CODE (t) == METHOD_TYPE)
821 /* Can only be reached through a pointer. */
822 pp_cxx_right_paren (cxx_pp);
823 arg = TYPE_ARG_TYPES (t);
824 if (TREE_CODE (t) == METHOD_TYPE)
825 arg = TREE_CHAIN (arg);
827 /* Function pointers don't have default args. Not in standard C++,
828 anyway; they may in g++, but we'll just pretend otherwise. */
829 dump_parameters (arg, flags & ~TFF_FUNCTION_DEFAULT_ARGUMENTS);
831 if (TREE_CODE (t) == METHOD_TYPE)
832 pp_cxx_cv_qualifier_seq (cxx_pp, class_of_this_parm (t));
833 else
834 pp_cxx_cv_qualifier_seq (cxx_pp, t);
835 dump_exception_spec (TYPE_RAISES_EXCEPTIONS (t), flags);
836 dump_type_suffix (TREE_TYPE (t), flags);
837 break;
840 case ARRAY_TYPE:
841 pp_maybe_space (cxx_pp);
842 pp_cxx_left_bracket (cxx_pp);
843 if (TYPE_DOMAIN (t))
845 tree dtype = TYPE_DOMAIN (t);
846 tree max = TYPE_MAX_VALUE (dtype);
847 if (integer_all_onesp (max))
848 pp_character (cxx_pp, '0');
849 else if (host_integerp (max, 0))
850 pp_wide_integer (cxx_pp, tree_low_cst (max, 0) + 1);
851 else if (TREE_CODE (max) == MINUS_EXPR)
852 dump_expr (TREE_OPERAND (max, 0),
853 flags & ~TFF_EXPR_IN_PARENS);
854 else
855 dump_expr (fold_build2_loc (input_location,
856 PLUS_EXPR, dtype, max,
857 build_int_cst (dtype, 1)),
858 flags & ~TFF_EXPR_IN_PARENS);
860 pp_cxx_right_bracket (cxx_pp);
861 dump_type_suffix (TREE_TYPE (t), flags);
862 break;
864 case ENUMERAL_TYPE:
865 case IDENTIFIER_NODE:
866 case INTEGER_TYPE:
867 case BOOLEAN_TYPE:
868 case REAL_TYPE:
869 case RECORD_TYPE:
870 case TEMPLATE_TYPE_PARM:
871 case TEMPLATE_TEMPLATE_PARM:
872 case BOUND_TEMPLATE_TEMPLATE_PARM:
873 case TREE_LIST:
874 case TYPE_DECL:
875 case TREE_VEC:
876 case UNION_TYPE:
877 case LANG_TYPE:
878 case VOID_TYPE:
879 case TYPENAME_TYPE:
880 case COMPLEX_TYPE:
881 case VECTOR_TYPE:
882 case TYPEOF_TYPE:
883 case UNDERLYING_TYPE:
884 case DECLTYPE_TYPE:
885 case TYPE_PACK_EXPANSION:
886 case FIXED_POINT_TYPE:
887 case NULLPTR_TYPE:
888 break;
890 default:
891 pp_unsupported_tree (cxx_pp, t);
892 case ERROR_MARK:
893 /* Don't mark it here, we should have already done in
894 dump_type_prefix. */
895 break;
899 static void
900 dump_global_iord (tree t)
902 const char *p = NULL;
904 if (DECL_GLOBAL_CTOR_P (t))
905 p = M_("(static initializers for %s)");
906 else if (DECL_GLOBAL_DTOR_P (t))
907 p = M_("(static destructors for %s)");
908 else
909 gcc_unreachable ();
911 pp_printf (pp_base (cxx_pp), p, input_filename);
914 static void
915 dump_simple_decl (tree t, tree type, int flags)
917 if (flags & TFF_DECL_SPECIFIERS)
919 if (TREE_CODE (t) == VAR_DECL
920 && DECL_DECLARED_CONSTEXPR_P (t))
921 pp_cxx_ws_string (cxx_pp, "constexpr");
922 dump_type_prefix (type, flags & ~TFF_UNQUALIFIED_NAME);
923 pp_maybe_space (cxx_pp);
925 if (! (flags & TFF_UNQUALIFIED_NAME)
926 && TREE_CODE (t) != PARM_DECL
927 && (!DECL_INITIAL (t)
928 || TREE_CODE (DECL_INITIAL (t)) != TEMPLATE_PARM_INDEX))
929 dump_scope (CP_DECL_CONTEXT (t), flags);
930 flags &= ~TFF_UNQUALIFIED_NAME;
931 if ((flags & TFF_DECL_SPECIFIERS)
932 && DECL_TEMPLATE_PARM_P (t)
933 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (t)))
934 pp_string (cxx_pp, "...");
935 if (DECL_NAME (t))
936 dump_decl (DECL_NAME (t), flags);
937 else
938 pp_string (cxx_pp, M_("<anonymous>"));
939 if (flags & TFF_DECL_SPECIFIERS)
940 dump_type_suffix (type, flags);
943 /* Dump a human readable string for the decl T under control of FLAGS. */
945 static void
946 dump_decl (tree t, int flags)
948 if (t == NULL_TREE)
949 return;
951 /* If doing Objective-C++, give Objective-C a chance to demangle
952 Objective-C method names. */
953 if (c_dialect_objc ())
955 const char *demangled = objc_maybe_printable_name (t, flags);
956 if (demangled)
958 pp_string (cxx_pp, demangled);
959 return;
963 switch (TREE_CODE (t))
965 case TYPE_DECL:
966 /* Don't say 'typedef class A' */
967 if (DECL_ARTIFICIAL (t) && !DECL_SELF_REFERENCE_P (t))
969 if ((flags & TFF_DECL_SPECIFIERS)
970 && TREE_CODE (TREE_TYPE (t)) == TEMPLATE_TYPE_PARM)
972 /* Say `class T' not just `T'. */
973 pp_cxx_ws_string (cxx_pp, "class");
975 /* Emit the `...' for a parameter pack. */
976 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
977 pp_cxx_ws_string (cxx_pp, "...");
980 dump_type (TREE_TYPE (t), flags);
981 break;
983 if (TYPE_DECL_ALIAS_P (t)
984 && (flags & TFF_DECL_SPECIFIERS
985 || flags & TFF_CLASS_KEY_OR_ENUM))
987 pp_cxx_ws_string (cxx_pp, "using");
988 dump_decl (DECL_NAME (t), flags);
989 pp_cxx_whitespace (cxx_pp);
990 pp_cxx_ws_string (cxx_pp, "=");
991 pp_cxx_whitespace (cxx_pp);
992 dump_type (DECL_ORIGINAL_TYPE (t), flags);
993 break;
995 if ((flags & TFF_DECL_SPECIFIERS)
996 && !DECL_SELF_REFERENCE_P (t))
997 pp_cxx_ws_string (cxx_pp, "typedef");
998 dump_simple_decl (t, DECL_ORIGINAL_TYPE (t)
999 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t),
1000 flags);
1001 break;
1003 case VAR_DECL:
1004 if (DECL_NAME (t) && VTABLE_NAME_P (DECL_NAME (t)))
1006 pp_string (cxx_pp, M_("vtable for "));
1007 gcc_assert (TYPE_P (DECL_CONTEXT (t)));
1008 dump_type (DECL_CONTEXT (t), flags);
1009 break;
1011 /* Else fall through. */
1012 case FIELD_DECL:
1013 case PARM_DECL:
1014 dump_simple_decl (t, TREE_TYPE (t), flags);
1015 break;
1017 case RESULT_DECL:
1018 pp_string (cxx_pp, M_("<return value> "));
1019 dump_simple_decl (t, TREE_TYPE (t), flags);
1020 break;
1022 case NAMESPACE_DECL:
1023 if (flags & TFF_DECL_SPECIFIERS)
1024 pp_cxx_declaration (cxx_pp, t);
1025 else
1027 if (! (flags & TFF_UNQUALIFIED_NAME))
1028 dump_scope (CP_DECL_CONTEXT (t), flags);
1029 flags &= ~TFF_UNQUALIFIED_NAME;
1030 if (DECL_NAME (t) == NULL_TREE)
1032 if (!(pp_c_base (cxx_pp)->flags & pp_c_flag_gnu_v3))
1033 pp_cxx_ws_string (cxx_pp, M_("{anonymous}"));
1034 else
1035 pp_cxx_ws_string (cxx_pp, M_("(anonymous namespace)"));
1037 else
1038 pp_cxx_tree_identifier (cxx_pp, DECL_NAME (t));
1040 break;
1042 case SCOPE_REF:
1043 dump_type (TREE_OPERAND (t, 0), flags);
1044 pp_string (cxx_pp, "::");
1045 dump_decl (TREE_OPERAND (t, 1), TFF_UNQUALIFIED_NAME);
1046 break;
1048 case ARRAY_REF:
1049 dump_decl (TREE_OPERAND (t, 0), flags);
1050 pp_cxx_left_bracket (cxx_pp);
1051 dump_decl (TREE_OPERAND (t, 1), flags);
1052 pp_cxx_right_bracket (cxx_pp);
1053 break;
1055 /* So that we can do dump_decl on an aggr type. */
1056 case RECORD_TYPE:
1057 case UNION_TYPE:
1058 case ENUMERAL_TYPE:
1059 dump_type (t, flags);
1060 break;
1062 case BIT_NOT_EXPR:
1063 /* This is a pseudo destructor call which has not been folded into
1064 a PSEUDO_DTOR_EXPR yet. */
1065 pp_cxx_complement (cxx_pp);
1066 dump_type (TREE_OPERAND (t, 0), flags);
1067 break;
1069 case TYPE_EXPR:
1070 gcc_unreachable ();
1071 break;
1073 /* These special cases are duplicated here so that other functions
1074 can feed identifiers to error and get them demangled properly. */
1075 case IDENTIFIER_NODE:
1076 if (IDENTIFIER_TYPENAME_P (t))
1078 pp_cxx_ws_string (cxx_pp, "operator");
1079 /* Not exactly IDENTIFIER_TYPE_VALUE. */
1080 dump_type (TREE_TYPE (t), flags);
1081 break;
1083 else
1084 pp_cxx_tree_identifier (cxx_pp, t);
1085 break;
1087 case OVERLOAD:
1088 if (OVL_CHAIN (t))
1090 t = OVL_CURRENT (t);
1091 if (DECL_CLASS_SCOPE_P (t))
1093 dump_type (DECL_CONTEXT (t), flags);
1094 pp_cxx_colon_colon (cxx_pp);
1096 else if (!DECL_FILE_SCOPE_P (t))
1098 dump_decl (DECL_CONTEXT (t), flags);
1099 pp_cxx_colon_colon (cxx_pp);
1101 dump_decl (DECL_NAME (t), flags);
1102 break;
1105 /* If there's only one function, just treat it like an ordinary
1106 FUNCTION_DECL. */
1107 t = OVL_CURRENT (t);
1108 /* Fall through. */
1110 case FUNCTION_DECL:
1111 if (! DECL_LANG_SPECIFIC (t))
1112 pp_string (cxx_pp, M_("<built-in>"));
1113 else if (DECL_GLOBAL_CTOR_P (t) || DECL_GLOBAL_DTOR_P (t))
1114 dump_global_iord (t);
1115 else
1116 dump_function_decl (t, flags);
1117 break;
1119 case TEMPLATE_DECL:
1120 dump_template_decl (t, flags);
1121 break;
1123 case TEMPLATE_ID_EXPR:
1125 tree name = TREE_OPERAND (t, 0);
1126 tree args = TREE_OPERAND (t, 1);
1128 if (is_overloaded_fn (name))
1129 name = DECL_NAME (get_first_fn (name));
1130 dump_decl (name, flags);
1131 pp_cxx_begin_template_argument_list (cxx_pp);
1132 if (args == error_mark_node)
1133 pp_string (cxx_pp, M_("<template arguments error>"));
1134 else if (args)
1135 dump_template_argument_list (args, flags);
1136 pp_cxx_end_template_argument_list (cxx_pp);
1138 break;
1140 case LABEL_DECL:
1141 pp_cxx_tree_identifier (cxx_pp, DECL_NAME (t));
1142 break;
1144 case CONST_DECL:
1145 if ((TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == ENUMERAL_TYPE)
1146 || (DECL_INITIAL (t) &&
1147 TREE_CODE (DECL_INITIAL (t)) == TEMPLATE_PARM_INDEX))
1148 dump_simple_decl (t, TREE_TYPE (t), flags);
1149 else if (DECL_NAME (t))
1150 dump_decl (DECL_NAME (t), flags);
1151 else if (DECL_INITIAL (t))
1152 dump_expr (DECL_INITIAL (t), flags | TFF_EXPR_IN_PARENS);
1153 else
1154 pp_string (cxx_pp, M_("<enumerator>"));
1155 break;
1157 case USING_DECL:
1158 pp_cxx_ws_string (cxx_pp, "using");
1159 dump_type (USING_DECL_SCOPE (t), flags);
1160 pp_cxx_colon_colon (cxx_pp);
1161 dump_decl (DECL_NAME (t), flags);
1162 break;
1164 case STATIC_ASSERT:
1165 pp_cxx_declaration (cxx_pp, t);
1166 break;
1168 case BASELINK:
1169 dump_decl (BASELINK_FUNCTIONS (t), flags);
1170 break;
1172 case NON_DEPENDENT_EXPR:
1173 dump_expr (t, flags);
1174 break;
1176 case TEMPLATE_TYPE_PARM:
1177 if (flags & TFF_DECL_SPECIFIERS)
1178 pp_cxx_declaration (cxx_pp, t);
1179 else
1180 pp_type_id (cxx_pp, t);
1181 break;
1183 case UNBOUND_CLASS_TEMPLATE:
1184 case TYPE_PACK_EXPANSION:
1185 case TREE_BINFO:
1186 dump_type (t, flags);
1187 break;
1189 default:
1190 pp_unsupported_tree (cxx_pp, t);
1191 /* Fall through to error. */
1193 case ERROR_MARK:
1194 pp_string (cxx_pp, M_("<declaration error>"));
1195 break;
1199 /* Dump a template declaration T under control of FLAGS. This means the
1200 'template <...> leaders plus the 'class X' or 'void fn(...)' part. */
1202 static void
1203 dump_template_decl (tree t, int flags)
1205 tree orig_parms = DECL_TEMPLATE_PARMS (t);
1206 tree parms;
1207 int i;
1209 if (flags & TFF_TEMPLATE_HEADER)
1211 for (parms = orig_parms = nreverse (orig_parms);
1212 parms;
1213 parms = TREE_CHAIN (parms))
1215 tree inner_parms = INNERMOST_TEMPLATE_PARMS (parms);
1216 int len = TREE_VEC_LENGTH (inner_parms);
1218 pp_cxx_ws_string (cxx_pp, "template");
1219 pp_cxx_begin_template_argument_list (cxx_pp);
1221 /* If we've shown the template prefix, we'd better show the
1222 parameters' and decl's type too. */
1223 flags |= TFF_DECL_SPECIFIERS;
1225 for (i = 0; i < len; i++)
1227 if (i)
1228 pp_separate_with_comma (cxx_pp);
1229 dump_template_parameter (TREE_VEC_ELT (inner_parms, i), flags);
1231 pp_cxx_end_template_argument_list (cxx_pp);
1232 pp_cxx_whitespace (cxx_pp);
1234 nreverse(orig_parms);
1236 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
1238 /* Say `template<arg> class TT' not just `template<arg> TT'. */
1239 pp_cxx_ws_string (cxx_pp, "class");
1241 /* If this is a parameter pack, print the ellipsis. */
1242 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
1243 pp_cxx_ws_string (cxx_pp, "...");
1247 if (DECL_CLASS_TEMPLATE_P (t))
1248 dump_type (TREE_TYPE (t),
1249 ((flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1250 | (flags & TFF_DECL_SPECIFIERS ? TFF_CLASS_KEY_OR_ENUM : 0)));
1251 else if (DECL_TEMPLATE_RESULT (t)
1252 && (TREE_CODE (DECL_TEMPLATE_RESULT (t)) == VAR_DECL
1253 /* Alias template. */
1254 || DECL_TYPE_TEMPLATE_P (t)))
1255 dump_decl (DECL_TEMPLATE_RESULT (t), flags | TFF_TEMPLATE_NAME);
1256 else
1258 gcc_assert (TREE_TYPE (t));
1259 switch (NEXT_CODE (t))
1261 case METHOD_TYPE:
1262 case FUNCTION_TYPE:
1263 dump_function_decl (t, flags | TFF_TEMPLATE_NAME);
1264 break;
1265 default:
1266 /* This case can occur with some invalid code. */
1267 dump_type (TREE_TYPE (t),
1268 (flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1269 | (flags & TFF_DECL_SPECIFIERS
1270 ? TFF_CLASS_KEY_OR_ENUM : 0));
1275 /* find_typenames looks through the type of the function template T
1276 and returns a vec containing any typedefs, decltypes or TYPENAME_TYPEs
1277 it finds. */
1279 struct find_typenames_t
1281 struct pointer_set_t *p_set;
1282 vec<tree, va_gc> *typenames;
1285 static tree
1286 find_typenames_r (tree *tp, int *walk_subtrees, void *data)
1288 struct find_typenames_t *d = (struct find_typenames_t *)data;
1289 tree mv = NULL_TREE;
1291 if (TYPE_P (*tp) && is_typedef_decl (TYPE_NAME (*tp)))
1292 /* Add the type of the typedef without any additional cv-quals. */
1293 mv = TREE_TYPE (TYPE_NAME (*tp));
1294 else if (TREE_CODE (*tp) == TYPENAME_TYPE
1295 || TREE_CODE (*tp) == DECLTYPE_TYPE)
1296 /* Add the typename without any cv-qualifiers. */
1297 mv = TYPE_MAIN_VARIANT (*tp);
1299 if (TREE_CODE (*tp) == TYPE_PACK_EXPANSION)
1301 /* Don't mess with parameter packs since we don't remember
1302 the pack expansion context for a particular typename. */
1303 *walk_subtrees = false;
1304 return NULL_TREE;
1307 if (mv && (mv == *tp || !pointer_set_insert (d->p_set, mv)))
1308 vec_safe_push (d->typenames, mv);
1310 /* Search into class template arguments, which cp_walk_subtrees
1311 doesn't do. */
1312 if (CLASS_TYPE_P (*tp) && CLASSTYPE_TEMPLATE_INFO (*tp))
1313 cp_walk_tree (&CLASSTYPE_TI_ARGS (*tp), find_typenames_r,
1314 data, d->p_set);
1316 return NULL_TREE;
1319 static vec<tree, va_gc> *
1320 find_typenames (tree t)
1322 struct find_typenames_t ft;
1323 ft.p_set = pointer_set_create ();
1324 ft.typenames = NULL;
1325 cp_walk_tree (&TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
1326 find_typenames_r, &ft, ft.p_set);
1327 pointer_set_destroy (ft.p_set);
1328 return ft.typenames;
1331 /* Pretty print a function decl. There are several ways we want to print a
1332 function declaration. The TFF_ bits in FLAGS tells us how to behave.
1333 As error can only apply the '#' flag once to give 0 and 1 for V, there
1334 is %D which doesn't print the throw specs, and %F which does. */
1336 static void
1337 dump_function_decl (tree t, int flags)
1339 tree fntype;
1340 tree parmtypes;
1341 tree cname = NULL_TREE;
1342 tree template_args = NULL_TREE;
1343 tree template_parms = NULL_TREE;
1344 int show_return = flags & TFF_RETURN_TYPE || flags & TFF_DECL_SPECIFIERS;
1345 int do_outer_scope = ! (flags & TFF_UNQUALIFIED_NAME);
1346 tree exceptions;
1347 vec<tree, va_gc> *typenames = NULL;
1349 if (DECL_NAME (t) && LAMBDA_FUNCTION_P (t))
1351 /* A lambda's signature is essentially its "type", so defer. */
1352 gcc_assert (LAMBDA_TYPE_P (DECL_CONTEXT (t)));
1353 dump_type (DECL_CONTEXT (t), flags);
1354 return;
1357 flags &= ~(TFF_UNQUALIFIED_NAME | TFF_TEMPLATE_NAME);
1358 if (TREE_CODE (t) == TEMPLATE_DECL)
1359 t = DECL_TEMPLATE_RESULT (t);
1361 /* Save the exceptions, in case t is a specialization and we are
1362 emitting an error about incompatible specifications. */
1363 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (t));
1365 /* Pretty print template instantiations only. */
1366 if (DECL_USE_TEMPLATE (t) && DECL_TEMPLATE_INFO (t)
1367 && flag_pretty_templates)
1369 tree tmpl;
1371 template_args = DECL_TI_ARGS (t);
1372 tmpl = most_general_template (t);
1373 if (tmpl && TREE_CODE (tmpl) == TEMPLATE_DECL)
1375 template_parms = DECL_TEMPLATE_PARMS (tmpl);
1376 t = tmpl;
1377 typenames = find_typenames (t);
1381 fntype = TREE_TYPE (t);
1382 parmtypes = FUNCTION_FIRST_USER_PARMTYPE (t);
1384 if (DECL_CLASS_SCOPE_P (t))
1385 cname = DECL_CONTEXT (t);
1386 /* This is for partially instantiated template methods. */
1387 else if (TREE_CODE (fntype) == METHOD_TYPE)
1388 cname = TREE_TYPE (TREE_VALUE (parmtypes));
1390 if (flags & TFF_DECL_SPECIFIERS)
1392 if (DECL_STATIC_FUNCTION_P (t))
1393 pp_cxx_ws_string (cxx_pp, "static");
1394 else if (DECL_VIRTUAL_P (t))
1395 pp_cxx_ws_string (cxx_pp, "virtual");
1397 if (DECL_DECLARED_CONSTEXPR_P (STRIP_TEMPLATE (t)))
1398 pp_cxx_ws_string (cxx_pp, "constexpr");
1401 /* Print the return type? */
1402 if (show_return)
1403 show_return = !DECL_CONV_FN_P (t) && !DECL_CONSTRUCTOR_P (t)
1404 && !DECL_DESTRUCTOR_P (t);
1405 if (show_return)
1406 dump_type_prefix (TREE_TYPE (fntype), flags);
1408 /* Print the function name. */
1409 if (!do_outer_scope)
1410 /* Nothing. */;
1411 else if (cname)
1413 dump_type (cname, flags);
1414 pp_cxx_colon_colon (cxx_pp);
1416 else
1417 dump_scope (CP_DECL_CONTEXT (t), flags);
1419 dump_function_name (t, flags);
1421 if (!(flags & TFF_NO_FUNCTION_ARGUMENTS))
1423 dump_parameters (parmtypes, flags);
1425 if (TREE_CODE (fntype) == METHOD_TYPE)
1427 pp_base (cxx_pp)->padding = pp_before;
1428 pp_cxx_cv_qualifier_seq (cxx_pp, class_of_this_parm (fntype));
1431 if (flags & TFF_EXCEPTION_SPECIFICATION)
1433 pp_base (cxx_pp)->padding = pp_before;
1434 dump_exception_spec (exceptions, flags);
1437 if (show_return)
1438 dump_type_suffix (TREE_TYPE (fntype), flags);
1440 /* If T is a template instantiation, dump the parameter binding. */
1441 if (template_parms != NULL_TREE && template_args != NULL_TREE)
1443 pp_cxx_whitespace (cxx_pp);
1444 pp_cxx_left_bracket (cxx_pp);
1445 pp_cxx_ws_string (cxx_pp, M_("with"));
1446 pp_cxx_whitespace (cxx_pp);
1447 dump_template_bindings (template_parms, template_args, typenames);
1448 pp_cxx_right_bracket (cxx_pp);
1451 else if (template_args)
1453 bool need_comma = false;
1454 int i;
1455 pp_cxx_begin_template_argument_list (cxx_pp);
1456 template_args = INNERMOST_TEMPLATE_ARGS (template_args);
1457 for (i = 0; i < TREE_VEC_LENGTH (template_args); ++i)
1459 tree arg = TREE_VEC_ELT (template_args, i);
1460 if (need_comma)
1461 pp_separate_with_comma (cxx_pp);
1462 if (ARGUMENT_PACK_P (arg))
1463 pp_cxx_left_brace (cxx_pp);
1464 dump_template_argument (arg, TFF_PLAIN_IDENTIFIER);
1465 if (ARGUMENT_PACK_P (arg))
1466 pp_cxx_right_brace (cxx_pp);
1467 need_comma = true;
1469 pp_cxx_end_template_argument_list (cxx_pp);
1473 /* Print a parameter list. If this is for a member function, the
1474 member object ptr (and any other hidden args) should have
1475 already been removed. */
1477 static void
1478 dump_parameters (tree parmtypes, int flags)
1480 int first = 1;
1481 flags &= ~TFF_SCOPE;
1482 pp_cxx_left_paren (cxx_pp);
1484 for (first = 1; parmtypes != void_list_node;
1485 parmtypes = TREE_CHAIN (parmtypes))
1487 if (!first)
1488 pp_separate_with_comma (cxx_pp);
1489 first = 0;
1490 if (!parmtypes)
1492 pp_cxx_ws_string (cxx_pp, "...");
1493 break;
1496 dump_type (TREE_VALUE (parmtypes), flags);
1498 if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && TREE_PURPOSE (parmtypes))
1500 pp_cxx_whitespace (cxx_pp);
1501 pp_equal (cxx_pp);
1502 pp_cxx_whitespace (cxx_pp);
1503 dump_expr (TREE_PURPOSE (parmtypes), flags | TFF_EXPR_IN_PARENS);
1507 pp_cxx_right_paren (cxx_pp);
1510 /* Print an exception specification. T is the exception specification. */
1512 static void
1513 dump_exception_spec (tree t, int flags)
1515 if (t && TREE_PURPOSE (t))
1517 pp_cxx_ws_string (cxx_pp, "noexcept");
1518 pp_cxx_whitespace (cxx_pp);
1519 pp_cxx_left_paren (cxx_pp);
1520 if (DEFERRED_NOEXCEPT_SPEC_P (t))
1521 pp_cxx_ws_string (cxx_pp, "<uninstantiated>");
1522 else
1523 dump_expr (TREE_PURPOSE (t), flags);
1524 pp_cxx_right_paren (cxx_pp);
1526 else if (t)
1528 pp_cxx_ws_string (cxx_pp, "throw");
1529 pp_cxx_whitespace (cxx_pp);
1530 pp_cxx_left_paren (cxx_pp);
1531 if (TREE_VALUE (t) != NULL_TREE)
1532 while (1)
1534 dump_type (TREE_VALUE (t), flags);
1535 t = TREE_CHAIN (t);
1536 if (!t)
1537 break;
1538 pp_separate_with_comma (cxx_pp);
1540 pp_cxx_right_paren (cxx_pp);
1544 /* Handle the function name for a FUNCTION_DECL node, grokking operators
1545 and destructors properly. */
1547 static void
1548 dump_function_name (tree t, int flags)
1550 tree name = DECL_NAME (t);
1552 /* We can get here with a decl that was synthesized by language-
1553 independent machinery (e.g. coverage.c) in which case it won't
1554 have a lang_specific structure attached and DECL_CONSTRUCTOR_P
1555 will crash. In this case it is safe just to print out the
1556 literal name. */
1557 if (!DECL_LANG_SPECIFIC (t))
1559 pp_cxx_tree_identifier (cxx_pp, name);
1560 return;
1563 if (TREE_CODE (t) == TEMPLATE_DECL)
1564 t = DECL_TEMPLATE_RESULT (t);
1566 /* Don't let the user see __comp_ctor et al. */
1567 if (DECL_CONSTRUCTOR_P (t)
1568 || DECL_DESTRUCTOR_P (t))
1570 if (LAMBDA_TYPE_P (DECL_CONTEXT (t)))
1571 name = get_identifier ("<lambda>");
1572 else if (TYPE_ANONYMOUS_P (DECL_CONTEXT (t)))
1573 name = get_identifier ("<constructor>");
1574 else
1575 name = constructor_name (DECL_CONTEXT (t));
1578 if (DECL_DESTRUCTOR_P (t))
1580 pp_cxx_complement (cxx_pp);
1581 dump_decl (name, TFF_PLAIN_IDENTIFIER);
1583 else if (DECL_CONV_FN_P (t))
1585 /* This cannot use the hack that the operator's return
1586 type is stashed off of its name because it may be
1587 used for error reporting. In the case of conflicting
1588 declarations, both will have the same name, yet
1589 the types will be different, hence the TREE_TYPE field
1590 of the first name will be clobbered by the second. */
1591 pp_cxx_ws_string (cxx_pp, "operator");
1592 dump_type (TREE_TYPE (TREE_TYPE (t)), flags);
1594 else if (name && IDENTIFIER_OPNAME_P (name))
1595 pp_cxx_tree_identifier (cxx_pp, name);
1596 else if (name && UDLIT_OPER_P (name))
1597 pp_cxx_tree_identifier (cxx_pp, name);
1598 else
1599 dump_decl (name, flags);
1601 if (DECL_TEMPLATE_INFO (t)
1602 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (t)
1603 && (TREE_CODE (DECL_TI_TEMPLATE (t)) != TEMPLATE_DECL
1604 || PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
1605 dump_template_parms (DECL_TEMPLATE_INFO (t), !DECL_USE_TEMPLATE (t), flags);
1608 /* Dump the template parameters from the template info INFO under control of
1609 FLAGS. PRIMARY indicates whether this is a primary template decl, or
1610 specialization (partial or complete). For partial specializations we show
1611 the specialized parameter values. For a primary template we show no
1612 decoration. */
1614 static void
1615 dump_template_parms (tree info, int primary, int flags)
1617 tree args = info ? TI_ARGS (info) : NULL_TREE;
1619 if (primary && flags & TFF_TEMPLATE_NAME)
1620 return;
1621 flags &= ~(TFF_CLASS_KEY_OR_ENUM | TFF_TEMPLATE_NAME);
1622 pp_cxx_begin_template_argument_list (cxx_pp);
1624 /* Be careful only to print things when we have them, so as not
1625 to crash producing error messages. */
1626 if (args && !primary)
1628 int len, ix;
1629 len = get_non_default_template_args_count (args, flags);
1631 args = INNERMOST_TEMPLATE_ARGS (args);
1632 for (ix = 0; ix != len; ix++)
1634 tree arg = TREE_VEC_ELT (args, ix);
1636 /* Only print a comma if we know there is an argument coming. In
1637 the case of an empty template argument pack, no actual
1638 argument will be printed. */
1639 if (ix
1640 && (!ARGUMENT_PACK_P (arg)
1641 || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) > 0))
1642 pp_separate_with_comma (cxx_pp);
1644 if (!arg)
1645 pp_string (cxx_pp, M_("<template parameter error>"));
1646 else
1647 dump_template_argument (arg, flags);
1650 else if (primary)
1652 tree tpl = TI_TEMPLATE (info);
1653 tree parms = DECL_TEMPLATE_PARMS (tpl);
1654 int len, ix;
1656 parms = TREE_CODE (parms) == TREE_LIST ? TREE_VALUE (parms) : NULL_TREE;
1657 len = parms ? TREE_VEC_LENGTH (parms) : 0;
1659 for (ix = 0; ix != len; ix++)
1661 tree parm;
1663 if (TREE_VEC_ELT (parms, ix) == error_mark_node)
1665 pp_string (cxx_pp, M_("<template parameter error>"));
1666 continue;
1669 parm = TREE_VALUE (TREE_VEC_ELT (parms, ix));
1671 if (ix)
1672 pp_separate_with_comma (cxx_pp);
1674 dump_decl (parm, flags & ~TFF_DECL_SPECIFIERS);
1677 pp_cxx_end_template_argument_list (cxx_pp);
1680 /* Print out the arguments of CALL_EXPR T as a parenthesized list using
1681 flags FLAGS. Skip over the first argument if SKIPFIRST is true. */
1683 static void
1684 dump_call_expr_args (tree t, int flags, bool skipfirst)
1686 tree arg;
1687 call_expr_arg_iterator iter;
1689 pp_cxx_left_paren (cxx_pp);
1690 FOR_EACH_CALL_EXPR_ARG (arg, iter, t)
1692 if (skipfirst)
1693 skipfirst = false;
1694 else
1696 dump_expr (arg, flags | TFF_EXPR_IN_PARENS);
1697 if (more_call_expr_args_p (&iter))
1698 pp_separate_with_comma (cxx_pp);
1701 pp_cxx_right_paren (cxx_pp);
1704 /* Print out the arguments of AGGR_INIT_EXPR T as a parenthesized list
1705 using flags FLAGS. Skip over the first argument if SKIPFIRST is
1706 true. */
1708 static void
1709 dump_aggr_init_expr_args (tree t, int flags, bool skipfirst)
1711 tree arg;
1712 aggr_init_expr_arg_iterator iter;
1714 pp_cxx_left_paren (cxx_pp);
1715 FOR_EACH_AGGR_INIT_EXPR_ARG (arg, iter, t)
1717 if (skipfirst)
1718 skipfirst = false;
1719 else
1721 dump_expr (arg, flags | TFF_EXPR_IN_PARENS);
1722 if (more_aggr_init_expr_args_p (&iter))
1723 pp_separate_with_comma (cxx_pp);
1726 pp_cxx_right_paren (cxx_pp);
1729 /* Print out a list of initializers (subr of dump_expr). */
1731 static void
1732 dump_expr_list (tree l, int flags)
1734 while (l)
1736 dump_expr (TREE_VALUE (l), flags | TFF_EXPR_IN_PARENS);
1737 l = TREE_CHAIN (l);
1738 if (l)
1739 pp_separate_with_comma (cxx_pp);
1743 /* Print out a vector of initializers (subr of dump_expr). */
1745 static void
1746 dump_expr_init_vec (vec<constructor_elt, va_gc> *v, int flags)
1748 unsigned HOST_WIDE_INT idx;
1749 tree value;
1751 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
1753 dump_expr (value, flags | TFF_EXPR_IN_PARENS);
1754 if (idx != v->length () - 1)
1755 pp_separate_with_comma (cxx_pp);
1760 /* We've gotten an indirect REFERENCE (an OBJ_TYPE_REF) to a virtual
1761 function. Resolve it to a close relative -- in the sense of static
1762 type -- variant being overridden. That is close to what was written in
1763 the source code. Subroutine of dump_expr. */
1765 static tree
1766 resolve_virtual_fun_from_obj_type_ref (tree ref)
1768 tree obj_type = TREE_TYPE (OBJ_TYPE_REF_OBJECT (ref));
1769 HOST_WIDE_INT index = tree_low_cst (OBJ_TYPE_REF_TOKEN (ref), 1);
1770 tree fun = BINFO_VIRTUALS (TYPE_BINFO (TREE_TYPE (obj_type)));
1771 while (index)
1773 fun = TREE_CHAIN (fun);
1774 index -= (TARGET_VTABLE_USES_DESCRIPTORS
1775 ? TARGET_VTABLE_USES_DESCRIPTORS : 1);
1778 return BV_FN (fun);
1781 /* Print out an expression E under control of FLAGS. */
1783 static void
1784 dump_expr (tree t, int flags)
1786 if (t == 0)
1787 return;
1789 if (STATEMENT_CLASS_P (t))
1791 pp_cxx_ws_string (cxx_pp, M_("<statement>"));
1792 return;
1795 switch (TREE_CODE (t))
1797 case VAR_DECL:
1798 case PARM_DECL:
1799 case FIELD_DECL:
1800 case CONST_DECL:
1801 case FUNCTION_DECL:
1802 case TEMPLATE_DECL:
1803 case NAMESPACE_DECL:
1804 case LABEL_DECL:
1805 case OVERLOAD:
1806 case TYPE_DECL:
1807 case IDENTIFIER_NODE:
1808 dump_decl (t, ((flags & ~(TFF_DECL_SPECIFIERS|TFF_RETURN_TYPE
1809 |TFF_TEMPLATE_HEADER))
1810 | TFF_NO_FUNCTION_ARGUMENTS));
1811 break;
1813 case SSA_NAME:
1814 if (SSA_NAME_VAR (t)
1815 && !DECL_ARTIFICIAL (SSA_NAME_VAR (t)))
1816 dump_expr (SSA_NAME_VAR (t), flags);
1817 else
1818 pp_cxx_ws_string (cxx_pp, M_("<unknown>"));
1819 break;
1821 case INTEGER_CST:
1822 case REAL_CST:
1823 case STRING_CST:
1824 case COMPLEX_CST:
1825 pp_constant (cxx_pp, t);
1826 break;
1828 case USERDEF_LITERAL:
1829 pp_cxx_userdef_literal (cxx_pp, t);
1830 break;
1832 case THROW_EXPR:
1833 /* While waiting for caret diagnostics, avoid printing
1834 __cxa_allocate_exception, __cxa_throw, and the like. */
1835 pp_cxx_ws_string (cxx_pp, M_("<throw-expression>"));
1836 break;
1838 case PTRMEM_CST:
1839 pp_ampersand (cxx_pp);
1840 dump_type (PTRMEM_CST_CLASS (t), flags);
1841 pp_cxx_colon_colon (cxx_pp);
1842 pp_cxx_tree_identifier (cxx_pp, DECL_NAME (PTRMEM_CST_MEMBER (t)));
1843 break;
1845 case COMPOUND_EXPR:
1846 pp_cxx_left_paren (cxx_pp);
1847 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1848 pp_separate_with_comma (cxx_pp);
1849 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
1850 pp_cxx_right_paren (cxx_pp);
1851 break;
1853 case COND_EXPR:
1854 pp_cxx_left_paren (cxx_pp);
1855 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1856 pp_string (cxx_pp, " ? ");
1857 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
1858 pp_string (cxx_pp, " : ");
1859 dump_expr (TREE_OPERAND (t, 2), flags | TFF_EXPR_IN_PARENS);
1860 pp_cxx_right_paren (cxx_pp);
1861 break;
1863 case SAVE_EXPR:
1864 if (TREE_HAS_CONSTRUCTOR (t))
1866 pp_cxx_ws_string (cxx_pp, "new");
1867 pp_cxx_whitespace (cxx_pp);
1868 dump_type (TREE_TYPE (TREE_TYPE (t)), flags);
1870 else
1871 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1872 break;
1874 case AGGR_INIT_EXPR:
1876 tree fn = NULL_TREE;
1878 if (TREE_CODE (AGGR_INIT_EXPR_FN (t)) == ADDR_EXPR)
1879 fn = TREE_OPERAND (AGGR_INIT_EXPR_FN (t), 0);
1881 if (fn && TREE_CODE (fn) == FUNCTION_DECL)
1883 if (DECL_CONSTRUCTOR_P (fn))
1884 dump_type (DECL_CONTEXT (fn), flags);
1885 else
1886 dump_decl (fn, 0);
1888 else
1889 dump_expr (AGGR_INIT_EXPR_FN (t), 0);
1891 dump_aggr_init_expr_args (t, flags, true);
1892 break;
1894 case CALL_EXPR:
1896 tree fn = CALL_EXPR_FN (t);
1897 bool skipfirst = false;
1899 if (TREE_CODE (fn) == ADDR_EXPR)
1900 fn = TREE_OPERAND (fn, 0);
1902 /* Nobody is interested in seeing the guts of vcalls. */
1903 if (TREE_CODE (fn) == OBJ_TYPE_REF)
1904 fn = resolve_virtual_fun_from_obj_type_ref (fn);
1906 if (TREE_TYPE (fn) != NULL_TREE
1907 && NEXT_CODE (fn) == METHOD_TYPE
1908 && call_expr_nargs (t))
1910 tree ob = CALL_EXPR_ARG (t, 0);
1911 if (TREE_CODE (ob) == ADDR_EXPR)
1913 dump_expr (TREE_OPERAND (ob, 0), flags | TFF_EXPR_IN_PARENS);
1914 pp_cxx_dot (cxx_pp);
1916 else if (TREE_CODE (ob) != PARM_DECL
1917 || strcmp (IDENTIFIER_POINTER (DECL_NAME (ob)), "this"))
1919 dump_expr (ob, flags | TFF_EXPR_IN_PARENS);
1920 pp_cxx_arrow (cxx_pp);
1922 skipfirst = true;
1924 dump_expr (fn, flags | TFF_EXPR_IN_PARENS);
1925 dump_call_expr_args (t, flags, skipfirst);
1927 break;
1929 case TARGET_EXPR:
1930 /* Note that this only works for G++ target exprs. If somebody
1931 builds a general TARGET_EXPR, there's no way to represent that
1932 it initializes anything other that the parameter slot for the
1933 default argument. Note we may have cleared out the first
1934 operand in expand_expr, so don't go killing ourselves. */
1935 if (TREE_OPERAND (t, 1))
1936 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
1937 break;
1939 case POINTER_PLUS_EXPR:
1940 dump_binary_op ("+", t, flags);
1941 break;
1943 case INIT_EXPR:
1944 case MODIFY_EXPR:
1945 dump_binary_op (assignment_operator_name_info[(int)NOP_EXPR].name,
1946 t, flags);
1947 break;
1949 case PLUS_EXPR:
1950 case MINUS_EXPR:
1951 case MULT_EXPR:
1952 case TRUNC_DIV_EXPR:
1953 case TRUNC_MOD_EXPR:
1954 case MIN_EXPR:
1955 case MAX_EXPR:
1956 case LSHIFT_EXPR:
1957 case RSHIFT_EXPR:
1958 case BIT_IOR_EXPR:
1959 case BIT_XOR_EXPR:
1960 case BIT_AND_EXPR:
1961 case TRUTH_ANDIF_EXPR:
1962 case TRUTH_ORIF_EXPR:
1963 case LT_EXPR:
1964 case LE_EXPR:
1965 case GT_EXPR:
1966 case GE_EXPR:
1967 case EQ_EXPR:
1968 case NE_EXPR:
1969 case EXACT_DIV_EXPR:
1970 dump_binary_op (operator_name_info[(int) TREE_CODE (t)].name, t, flags);
1971 break;
1973 case CEIL_DIV_EXPR:
1974 case FLOOR_DIV_EXPR:
1975 case ROUND_DIV_EXPR:
1976 case RDIV_EXPR:
1977 dump_binary_op ("/", t, flags);
1978 break;
1980 case CEIL_MOD_EXPR:
1981 case FLOOR_MOD_EXPR:
1982 case ROUND_MOD_EXPR:
1983 dump_binary_op ("%", t, flags);
1984 break;
1986 case COMPONENT_REF:
1988 tree ob = TREE_OPERAND (t, 0);
1989 if (TREE_CODE (ob) == INDIRECT_REF)
1991 ob = TREE_OPERAND (ob, 0);
1992 if (TREE_CODE (ob) != PARM_DECL
1993 || (DECL_NAME (ob)
1994 && strcmp (IDENTIFIER_POINTER (DECL_NAME (ob)), "this")))
1996 dump_expr (ob, flags | TFF_EXPR_IN_PARENS);
1997 if (TREE_CODE (TREE_TYPE (ob)) == REFERENCE_TYPE)
1998 pp_cxx_dot (cxx_pp);
1999 else
2000 pp_cxx_arrow (cxx_pp);
2003 else
2005 dump_expr (ob, flags | TFF_EXPR_IN_PARENS);
2006 pp_cxx_dot (cxx_pp);
2008 dump_expr (TREE_OPERAND (t, 1), flags & ~TFF_EXPR_IN_PARENS);
2010 break;
2012 case ARRAY_REF:
2013 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2014 pp_cxx_left_bracket (cxx_pp);
2015 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2016 pp_cxx_right_bracket (cxx_pp);
2017 break;
2019 case UNARY_PLUS_EXPR:
2020 dump_unary_op ("+", t, flags);
2021 break;
2023 case ADDR_EXPR:
2024 if (TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
2025 || TREE_CODE (TREE_OPERAND (t, 0)) == STRING_CST
2026 /* An ADDR_EXPR can have reference type. In that case, we
2027 shouldn't print the `&' doing so indicates to the user
2028 that the expression has pointer type. */
2029 || (TREE_TYPE (t)
2030 && TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE))
2031 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2032 else if (TREE_CODE (TREE_OPERAND (t, 0)) == LABEL_DECL)
2033 dump_unary_op ("&&", t, flags);
2034 else
2035 dump_unary_op ("&", t, flags);
2036 break;
2038 case INDIRECT_REF:
2039 if (TREE_HAS_CONSTRUCTOR (t))
2041 t = TREE_OPERAND (t, 0);
2042 gcc_assert (TREE_CODE (t) == CALL_EXPR);
2043 dump_expr (CALL_EXPR_FN (t), flags | TFF_EXPR_IN_PARENS);
2044 dump_call_expr_args (t, flags, true);
2046 else
2048 if (TREE_OPERAND (t,0) != NULL_TREE
2049 && TREE_TYPE (TREE_OPERAND (t, 0))
2050 && NEXT_CODE (TREE_OPERAND (t, 0)) == REFERENCE_TYPE)
2051 dump_expr (TREE_OPERAND (t, 0), flags);
2052 else
2053 dump_unary_op ("*", t, flags);
2055 break;
2057 case MEM_REF:
2058 if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
2059 && integer_zerop (TREE_OPERAND (t, 1)))
2060 dump_expr (TREE_OPERAND (TREE_OPERAND (t, 0), 0), flags);
2061 else
2063 pp_cxx_star (cxx_pp);
2064 if (!integer_zerop (TREE_OPERAND (t, 1)))
2066 pp_cxx_left_paren (cxx_pp);
2067 if (!integer_onep (TYPE_SIZE_UNIT
2068 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0))))))
2070 pp_cxx_left_paren (cxx_pp);
2071 dump_type (ptr_type_node, flags);
2072 pp_cxx_right_paren (cxx_pp);
2075 dump_expr (TREE_OPERAND (t, 0), flags);
2076 if (!integer_zerop (TREE_OPERAND (t, 1)))
2078 pp_cxx_ws_string (cxx_pp, "+");
2079 dump_expr (fold_convert (ssizetype, TREE_OPERAND (t, 1)), flags);
2080 pp_cxx_right_paren (cxx_pp);
2083 break;
2085 case NEGATE_EXPR:
2086 case BIT_NOT_EXPR:
2087 case TRUTH_NOT_EXPR:
2088 case PREDECREMENT_EXPR:
2089 case PREINCREMENT_EXPR:
2090 dump_unary_op (operator_name_info [(int)TREE_CODE (t)].name, t, flags);
2091 break;
2093 case POSTDECREMENT_EXPR:
2094 case POSTINCREMENT_EXPR:
2095 pp_cxx_left_paren (cxx_pp);
2096 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2097 pp_cxx_ws_string (cxx_pp, operator_name_info[(int)TREE_CODE (t)].name);
2098 pp_cxx_right_paren (cxx_pp);
2099 break;
2101 case NON_LVALUE_EXPR:
2102 /* FIXME: This is a KLUDGE workaround for a parsing problem. There
2103 should be another level of INDIRECT_REF so that I don't have to do
2104 this. */
2105 if (TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == POINTER_TYPE)
2107 tree next = TREE_TYPE (TREE_TYPE (t));
2109 while (TREE_CODE (next) == POINTER_TYPE)
2110 next = TREE_TYPE (next);
2112 if (TREE_CODE (next) == FUNCTION_TYPE)
2114 if (flags & TFF_EXPR_IN_PARENS)
2115 pp_cxx_left_paren (cxx_pp);
2116 pp_cxx_star (cxx_pp);
2117 dump_expr (TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2118 if (flags & TFF_EXPR_IN_PARENS)
2119 pp_cxx_right_paren (cxx_pp);
2120 break;
2122 /* Else fall through. */
2124 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2125 break;
2127 CASE_CONVERT:
2128 case IMPLICIT_CONV_EXPR:
2129 case VIEW_CONVERT_EXPR:
2131 tree op = TREE_OPERAND (t, 0);
2132 tree ttype = TREE_TYPE (t);
2133 tree optype = TREE_TYPE (op);
2135 if (TREE_CODE (ttype) != TREE_CODE (optype)
2136 && POINTER_TYPE_P (ttype)
2137 && POINTER_TYPE_P (optype)
2138 && same_type_p (TREE_TYPE (optype),
2139 TREE_TYPE (ttype)))
2141 if (TREE_CODE (ttype) == REFERENCE_TYPE)
2142 dump_unary_op ("*", t, flags);
2143 else
2144 dump_unary_op ("&", t, flags);
2146 else if (!same_type_p (TREE_TYPE (op), TREE_TYPE (t)))
2148 /* It is a cast, but we cannot tell whether it is a
2149 reinterpret or static cast. Use the C style notation. */
2150 if (flags & TFF_EXPR_IN_PARENS)
2151 pp_cxx_left_paren (cxx_pp);
2152 pp_cxx_left_paren (cxx_pp);
2153 dump_type (TREE_TYPE (t), flags);
2154 pp_cxx_right_paren (cxx_pp);
2155 dump_expr (op, flags | TFF_EXPR_IN_PARENS);
2156 if (flags & TFF_EXPR_IN_PARENS)
2157 pp_cxx_right_paren (cxx_pp);
2159 else
2160 dump_expr (op, flags);
2161 break;
2164 case CONSTRUCTOR:
2165 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
2167 tree idx = build_ptrmemfunc_access_expr (t, pfn_identifier);
2169 if (integer_zerop (idx))
2171 /* A NULL pointer-to-member constant. */
2172 pp_cxx_left_paren (cxx_pp);
2173 pp_cxx_left_paren (cxx_pp);
2174 dump_type (TREE_TYPE (t), flags);
2175 pp_cxx_right_paren (cxx_pp);
2176 pp_character (cxx_pp, '0');
2177 pp_cxx_right_paren (cxx_pp);
2178 break;
2180 else if (host_integerp (idx, 0))
2182 tree virtuals;
2183 unsigned HOST_WIDE_INT n;
2185 t = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
2186 t = TYPE_METHOD_BASETYPE (t);
2187 virtuals = BINFO_VIRTUALS (TYPE_BINFO (TYPE_MAIN_VARIANT (t)));
2189 n = tree_low_cst (idx, 0);
2191 /* Map vtable index back one, to allow for the null pointer to
2192 member. */
2193 --n;
2195 while (n > 0 && virtuals)
2197 --n;
2198 virtuals = TREE_CHAIN (virtuals);
2200 if (virtuals)
2202 dump_expr (BV_FN (virtuals),
2203 flags | TFF_EXPR_IN_PARENS);
2204 break;
2208 if (TREE_TYPE (t) && LAMBDA_TYPE_P (TREE_TYPE (t)))
2209 pp_string (cxx_pp, "<lambda closure object>");
2210 if (TREE_TYPE (t) && EMPTY_CONSTRUCTOR_P (t))
2212 dump_type (TREE_TYPE (t), 0);
2213 pp_cxx_left_paren (cxx_pp);
2214 pp_cxx_right_paren (cxx_pp);
2216 else
2218 if (!BRACE_ENCLOSED_INITIALIZER_P (t))
2219 dump_type (TREE_TYPE (t), 0);
2220 pp_cxx_left_brace (cxx_pp);
2221 dump_expr_init_vec (CONSTRUCTOR_ELTS (t), flags);
2222 pp_cxx_right_brace (cxx_pp);
2225 break;
2227 case OFFSET_REF:
2229 tree ob = TREE_OPERAND (t, 0);
2230 if (is_dummy_object (ob))
2232 t = TREE_OPERAND (t, 1);
2233 if (TREE_CODE (t) == FUNCTION_DECL)
2234 /* A::f */
2235 dump_expr (t, flags | TFF_EXPR_IN_PARENS);
2236 else if (BASELINK_P (t))
2237 dump_expr (OVL_CURRENT (BASELINK_FUNCTIONS (t)),
2238 flags | TFF_EXPR_IN_PARENS);
2239 else
2240 dump_decl (t, flags);
2242 else
2244 if (TREE_CODE (ob) == INDIRECT_REF)
2246 dump_expr (TREE_OPERAND (ob, 0), flags | TFF_EXPR_IN_PARENS);
2247 pp_cxx_arrow (cxx_pp);
2248 pp_cxx_star (cxx_pp);
2250 else
2252 dump_expr (ob, flags | TFF_EXPR_IN_PARENS);
2253 pp_cxx_dot (cxx_pp);
2254 pp_cxx_star (cxx_pp);
2256 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2258 break;
2261 case TEMPLATE_PARM_INDEX:
2262 dump_decl (TEMPLATE_PARM_DECL (t), flags & ~TFF_DECL_SPECIFIERS);
2263 break;
2265 case CAST_EXPR:
2266 if (TREE_OPERAND (t, 0) == NULL_TREE
2267 || TREE_CHAIN (TREE_OPERAND (t, 0)))
2269 dump_type (TREE_TYPE (t), flags);
2270 pp_cxx_left_paren (cxx_pp);
2271 dump_expr_list (TREE_OPERAND (t, 0), flags);
2272 pp_cxx_right_paren (cxx_pp);
2274 else
2276 pp_cxx_left_paren (cxx_pp);
2277 dump_type (TREE_TYPE (t), flags);
2278 pp_cxx_right_paren (cxx_pp);
2279 pp_cxx_left_paren (cxx_pp);
2280 dump_expr_list (TREE_OPERAND (t, 0), flags);
2281 pp_cxx_right_paren (cxx_pp);
2283 break;
2285 case STATIC_CAST_EXPR:
2286 pp_cxx_ws_string (cxx_pp, "static_cast");
2287 goto cast;
2288 case REINTERPRET_CAST_EXPR:
2289 pp_cxx_ws_string (cxx_pp, "reinterpret_cast");
2290 goto cast;
2291 case CONST_CAST_EXPR:
2292 pp_cxx_ws_string (cxx_pp, "const_cast");
2293 goto cast;
2294 case DYNAMIC_CAST_EXPR:
2295 pp_cxx_ws_string (cxx_pp, "dynamic_cast");
2296 cast:
2297 pp_cxx_begin_template_argument_list (cxx_pp);
2298 dump_type (TREE_TYPE (t), flags);
2299 pp_cxx_end_template_argument_list (cxx_pp);
2300 pp_cxx_left_paren (cxx_pp);
2301 dump_expr (TREE_OPERAND (t, 0), flags);
2302 pp_cxx_right_paren (cxx_pp);
2303 break;
2305 case ARROW_EXPR:
2306 dump_expr (TREE_OPERAND (t, 0), flags);
2307 pp_cxx_arrow (cxx_pp);
2308 break;
2310 case SIZEOF_EXPR:
2311 case ALIGNOF_EXPR:
2312 if (TREE_CODE (t) == SIZEOF_EXPR)
2313 pp_cxx_ws_string (cxx_pp, "sizeof");
2314 else
2316 gcc_assert (TREE_CODE (t) == ALIGNOF_EXPR);
2317 pp_cxx_ws_string (cxx_pp, "__alignof__");
2319 pp_cxx_whitespace (cxx_pp);
2320 pp_cxx_left_paren (cxx_pp);
2321 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
2322 dump_type (TREE_TYPE (TREE_OPERAND (t, 0)), flags);
2323 else if (TYPE_P (TREE_OPERAND (t, 0)))
2324 dump_type (TREE_OPERAND (t, 0), flags);
2325 else
2326 dump_expr (TREE_OPERAND (t, 0), flags);
2327 pp_cxx_right_paren (cxx_pp);
2328 break;
2330 case AT_ENCODE_EXPR:
2331 pp_cxx_ws_string (cxx_pp, "@encode");
2332 pp_cxx_whitespace (cxx_pp);
2333 pp_cxx_left_paren (cxx_pp);
2334 dump_type (TREE_OPERAND (t, 0), flags);
2335 pp_cxx_right_paren (cxx_pp);
2336 break;
2338 case NOEXCEPT_EXPR:
2339 pp_cxx_ws_string (cxx_pp, "noexcept");
2340 pp_cxx_whitespace (cxx_pp);
2341 pp_cxx_left_paren (cxx_pp);
2342 dump_expr (TREE_OPERAND (t, 0), flags);
2343 pp_cxx_right_paren (cxx_pp);
2344 break;
2346 case REALPART_EXPR:
2347 case IMAGPART_EXPR:
2348 pp_cxx_ws_string (cxx_pp, operator_name_info[TREE_CODE (t)].name);
2349 pp_cxx_whitespace (cxx_pp);
2350 dump_expr (TREE_OPERAND (t, 0), flags);
2351 break;
2353 case DEFAULT_ARG:
2354 pp_string (cxx_pp, M_("<unparsed>"));
2355 break;
2357 case TRY_CATCH_EXPR:
2358 case WITH_CLEANUP_EXPR:
2359 case CLEANUP_POINT_EXPR:
2360 dump_expr (TREE_OPERAND (t, 0), flags);
2361 break;
2363 case PSEUDO_DTOR_EXPR:
2364 dump_expr (TREE_OPERAND (t, 2), flags);
2365 pp_cxx_dot (cxx_pp);
2366 dump_type (TREE_OPERAND (t, 0), flags);
2367 pp_cxx_colon_colon (cxx_pp);
2368 pp_cxx_complement (cxx_pp);
2369 dump_type (TREE_OPERAND (t, 1), flags);
2370 break;
2372 case TEMPLATE_ID_EXPR:
2373 dump_decl (t, flags);
2374 break;
2376 case BIND_EXPR:
2377 case STMT_EXPR:
2378 case EXPR_STMT:
2379 case STATEMENT_LIST:
2380 /* We don't yet have a way of dumping statements in a
2381 human-readable format. */
2382 pp_string (cxx_pp, "({...})");
2383 break;
2385 case LOOP_EXPR:
2386 pp_string (cxx_pp, "while (1) { ");
2387 dump_expr (TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2388 pp_cxx_right_brace (cxx_pp);
2389 break;
2391 case EXIT_EXPR:
2392 pp_string (cxx_pp, "if (");
2393 dump_expr (TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2394 pp_string (cxx_pp, ") break; ");
2395 break;
2397 case BASELINK:
2398 dump_expr (BASELINK_FUNCTIONS (t), flags & ~TFF_EXPR_IN_PARENS);
2399 break;
2401 case EMPTY_CLASS_EXPR:
2402 dump_type (TREE_TYPE (t), flags);
2403 pp_cxx_left_paren (cxx_pp);
2404 pp_cxx_right_paren (cxx_pp);
2405 break;
2407 case NON_DEPENDENT_EXPR:
2408 dump_expr (TREE_OPERAND (t, 0), flags);
2409 break;
2411 case ARGUMENT_PACK_SELECT:
2412 dump_template_argument (ARGUMENT_PACK_SELECT_FROM_PACK (t), flags);
2413 break;
2415 case RECORD_TYPE:
2416 case UNION_TYPE:
2417 case ENUMERAL_TYPE:
2418 case REAL_TYPE:
2419 case VOID_TYPE:
2420 case BOOLEAN_TYPE:
2421 case INTEGER_TYPE:
2422 case COMPLEX_TYPE:
2423 case VECTOR_TYPE:
2424 pp_type_specifier_seq (cxx_pp, t);
2425 break;
2427 case TYPENAME_TYPE:
2428 /* We get here when we want to print a dependent type as an
2429 id-expression, without any disambiguator decoration. */
2430 pp_id_expression (cxx_pp, t);
2431 break;
2433 case TEMPLATE_TYPE_PARM:
2434 case TEMPLATE_TEMPLATE_PARM:
2435 case BOUND_TEMPLATE_TEMPLATE_PARM:
2436 dump_type (t, flags);
2437 break;
2439 case TRAIT_EXPR:
2440 pp_cxx_trait_expression (cxx_pp, t);
2441 break;
2443 case VA_ARG_EXPR:
2444 pp_cxx_va_arg_expression (cxx_pp, t);
2445 break;
2447 case OFFSETOF_EXPR:
2448 pp_cxx_offsetof_expression (cxx_pp, t);
2449 break;
2451 case SCOPE_REF:
2452 dump_decl (t, flags);
2453 break;
2455 case EXPR_PACK_EXPANSION:
2456 case TYPEID_EXPR:
2457 case MEMBER_REF:
2458 case DOTSTAR_EXPR:
2459 case NEW_EXPR:
2460 case VEC_NEW_EXPR:
2461 case DELETE_EXPR:
2462 case VEC_DELETE_EXPR:
2463 case MODOP_EXPR:
2464 case ABS_EXPR:
2465 case CONJ_EXPR:
2466 case VECTOR_CST:
2467 case FIXED_CST:
2468 case UNORDERED_EXPR:
2469 case ORDERED_EXPR:
2470 case UNLT_EXPR:
2471 case UNLE_EXPR:
2472 case UNGT_EXPR:
2473 case UNGE_EXPR:
2474 case UNEQ_EXPR:
2475 case LTGT_EXPR:
2476 case COMPLEX_EXPR:
2477 case BIT_FIELD_REF:
2478 case FIX_TRUNC_EXPR:
2479 case FLOAT_EXPR:
2480 pp_expression (cxx_pp, t);
2481 break;
2483 case TRUTH_AND_EXPR:
2484 case TRUTH_OR_EXPR:
2485 case TRUTH_XOR_EXPR:
2486 if (flags & TFF_EXPR_IN_PARENS)
2487 pp_cxx_left_paren (cxx_pp);
2488 pp_expression (cxx_pp, t);
2489 if (flags & TFF_EXPR_IN_PARENS)
2490 pp_cxx_right_paren (cxx_pp);
2491 break;
2493 case OBJ_TYPE_REF:
2494 dump_expr (resolve_virtual_fun_from_obj_type_ref (t), flags);
2495 break;
2497 /* This list is incomplete, but should suffice for now.
2498 It is very important that `sorry' does not call
2499 `report_error_function'. That could cause an infinite loop. */
2500 default:
2501 pp_unsupported_tree (cxx_pp, t);
2502 /* fall through to ERROR_MARK... */
2503 case ERROR_MARK:
2504 pp_string (cxx_pp, M_("<expression error>"));
2505 break;
2509 static void
2510 dump_binary_op (const char *opstring, tree t, int flags)
2512 pp_cxx_left_paren (cxx_pp);
2513 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2514 pp_cxx_whitespace (cxx_pp);
2515 if (opstring)
2516 pp_cxx_ws_string (cxx_pp, opstring);
2517 else
2518 pp_string (cxx_pp, M_("<unknown operator>"));
2519 pp_cxx_whitespace (cxx_pp);
2520 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2521 pp_cxx_right_paren (cxx_pp);
2524 static void
2525 dump_unary_op (const char *opstring, tree t, int flags)
2527 if (flags & TFF_EXPR_IN_PARENS)
2528 pp_cxx_left_paren (cxx_pp);
2529 pp_cxx_ws_string (cxx_pp, opstring);
2530 dump_expr (TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2531 if (flags & TFF_EXPR_IN_PARENS)
2532 pp_cxx_right_paren (cxx_pp);
2535 static void
2536 reinit_cxx_pp (void)
2538 pp_clear_output_area (cxx_pp);
2539 pp_base (cxx_pp)->padding = pp_none;
2540 pp_indentation (cxx_pp) = 0;
2541 pp_needs_newline (cxx_pp) = false;
2542 cxx_pp->enclosing_scope = current_function_decl;
2546 /* Exported interface to stringifying types, exprs and decls under TFF_*
2547 control. */
2549 const char *
2550 type_as_string (tree typ, int flags)
2552 reinit_cxx_pp ();
2553 pp_translate_identifiers (cxx_pp) = false;
2554 dump_type (typ, flags);
2555 return pp_formatted_text (cxx_pp);
2558 const char *
2559 type_as_string_translate (tree typ, int flags)
2561 reinit_cxx_pp ();
2562 dump_type (typ, flags);
2563 return pp_formatted_text (cxx_pp);
2566 const char *
2567 expr_as_string (tree decl, int flags)
2569 reinit_cxx_pp ();
2570 pp_translate_identifiers (cxx_pp) = false;
2571 dump_expr (decl, flags);
2572 return pp_formatted_text (cxx_pp);
2575 /* Wrap decl_as_string with options appropriate for dwarf. */
2577 const char *
2578 decl_as_dwarf_string (tree decl, int flags)
2580 const char *name;
2581 /* Curiously, reinit_cxx_pp doesn't reset the flags field, so setting the flag
2582 here will be adequate to get the desired behaviour. */
2583 pp_c_base (cxx_pp)->flags |= pp_c_flag_gnu_v3;
2584 name = decl_as_string (decl, flags);
2585 /* Subsequent calls to the pretty printer shouldn't use this style. */
2586 pp_c_base (cxx_pp)->flags &= ~pp_c_flag_gnu_v3;
2587 return name;
2590 const char *
2591 decl_as_string (tree decl, int flags)
2593 reinit_cxx_pp ();
2594 pp_translate_identifiers (cxx_pp) = false;
2595 dump_decl (decl, flags);
2596 return pp_formatted_text (cxx_pp);
2599 const char *
2600 decl_as_string_translate (tree decl, int flags)
2602 reinit_cxx_pp ();
2603 dump_decl (decl, flags);
2604 return pp_formatted_text (cxx_pp);
2607 /* Wrap lang_decl_name with options appropriate for dwarf. */
2609 const char *
2610 lang_decl_dwarf_name (tree decl, int v, bool translate)
2612 const char *name;
2613 /* Curiously, reinit_cxx_pp doesn't reset the flags field, so setting the flag
2614 here will be adequate to get the desired behaviour. */
2615 pp_c_base (cxx_pp)->flags |= pp_c_flag_gnu_v3;
2616 name = lang_decl_name (decl, v, translate);
2617 /* Subsequent calls to the pretty printer shouldn't use this style. */
2618 pp_c_base (cxx_pp)->flags &= ~pp_c_flag_gnu_v3;
2619 return name;
2622 /* Generate the three forms of printable names for cxx_printable_name. */
2624 const char *
2625 lang_decl_name (tree decl, int v, bool translate)
2627 if (v >= 2)
2628 return (translate
2629 ? decl_as_string_translate (decl, TFF_DECL_SPECIFIERS)
2630 : decl_as_string (decl, TFF_DECL_SPECIFIERS));
2632 reinit_cxx_pp ();
2633 pp_translate_identifiers (cxx_pp) = translate;
2634 if (v == 1
2635 && (DECL_CLASS_SCOPE_P (decl)
2636 || (DECL_NAMESPACE_SCOPE_P (decl)
2637 && CP_DECL_CONTEXT (decl) != global_namespace)))
2639 dump_type (CP_DECL_CONTEXT (decl), TFF_PLAIN_IDENTIFIER);
2640 pp_cxx_colon_colon (cxx_pp);
2643 if (TREE_CODE (decl) == FUNCTION_DECL)
2644 dump_function_name (decl, TFF_PLAIN_IDENTIFIER);
2645 else if ((DECL_NAME (decl) == NULL_TREE)
2646 && TREE_CODE (decl) == NAMESPACE_DECL)
2647 dump_decl (decl, TFF_PLAIN_IDENTIFIER | TFF_UNQUALIFIED_NAME);
2648 else
2649 dump_decl (DECL_NAME (decl), TFF_PLAIN_IDENTIFIER);
2651 return pp_formatted_text (cxx_pp);
2654 /* Return the location of a tree passed to %+ formats. */
2656 location_t
2657 location_of (tree t)
2659 if (TREE_CODE (t) == PARM_DECL && DECL_CONTEXT (t))
2660 t = DECL_CONTEXT (t);
2661 else if (TYPE_P (t))
2663 t = TYPE_MAIN_DECL (t);
2664 if (t == NULL_TREE)
2665 return input_location;
2667 else if (TREE_CODE (t) == OVERLOAD)
2668 t = OVL_FUNCTION (t);
2670 if (DECL_P (t))
2671 return DECL_SOURCE_LOCATION (t);
2672 return EXPR_LOC_OR_HERE (t);
2675 /* Now the interfaces from error et al to dump_type et al. Each takes an
2676 on/off VERBOSE flag and supply the appropriate TFF_ flags to a dump_
2677 function. */
2679 static const char *
2680 decl_to_string (tree decl, int verbose)
2682 int flags = 0;
2684 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == RECORD_TYPE
2685 || TREE_CODE (decl) == UNION_TYPE || TREE_CODE (decl) == ENUMERAL_TYPE)
2686 flags = TFF_CLASS_KEY_OR_ENUM;
2687 if (verbose)
2688 flags |= TFF_DECL_SPECIFIERS;
2689 else if (TREE_CODE (decl) == FUNCTION_DECL)
2690 flags |= TFF_DECL_SPECIFIERS | TFF_RETURN_TYPE;
2691 flags |= TFF_TEMPLATE_HEADER;
2693 reinit_cxx_pp ();
2694 dump_decl (decl, flags);
2695 return pp_formatted_text (cxx_pp);
2698 static const char *
2699 expr_to_string (tree decl)
2701 reinit_cxx_pp ();
2702 dump_expr (decl, 0);
2703 return pp_formatted_text (cxx_pp);
2706 static const char *
2707 fndecl_to_string (tree fndecl, int verbose)
2709 int flags;
2711 flags = TFF_EXCEPTION_SPECIFICATION | TFF_DECL_SPECIFIERS
2712 | TFF_TEMPLATE_HEADER;
2713 if (verbose)
2714 flags |= TFF_FUNCTION_DEFAULT_ARGUMENTS;
2715 reinit_cxx_pp ();
2716 dump_decl (fndecl, flags);
2717 return pp_formatted_text (cxx_pp);
2721 static const char *
2722 code_to_string (enum tree_code c)
2724 return tree_code_name [c];
2727 const char *
2728 language_to_string (enum languages c)
2730 switch (c)
2732 case lang_c:
2733 return "C";
2735 case lang_cplusplus:
2736 return "C++";
2738 case lang_java:
2739 return "Java";
2741 default:
2742 gcc_unreachable ();
2744 return NULL;
2747 /* Return the proper printed version of a parameter to a C++ function. */
2749 static const char *
2750 parm_to_string (int p)
2752 reinit_cxx_pp ();
2753 if (p < 0)
2754 pp_string (cxx_pp, "'this'");
2755 else
2756 pp_decimal_int (cxx_pp, p + 1);
2757 return pp_formatted_text (cxx_pp);
2760 static const char *
2761 op_to_string (enum tree_code p)
2763 tree id = operator_name_info[(int) p].identifier;
2764 return id ? IDENTIFIER_POINTER (id) : M_("<unknown>");
2767 static const char *
2768 type_to_string (tree typ, int verbose)
2770 int flags = 0;
2771 if (verbose)
2772 flags |= TFF_CLASS_KEY_OR_ENUM;
2773 flags |= TFF_TEMPLATE_HEADER;
2775 reinit_cxx_pp ();
2776 dump_type (typ, flags);
2777 /* If we're printing a type that involves typedefs, also print the
2778 stripped version. But sometimes the stripped version looks
2779 exactly the same, so we don't want it after all. To avoid printing
2780 it in that case, we play ugly obstack games. */
2781 if (typ && TYPE_P (typ) && typ != TYPE_CANONICAL (typ)
2782 && !uses_template_parms (typ))
2784 int aka_start; char *p;
2785 struct obstack *ob = pp_base (cxx_pp)->buffer->obstack;
2786 /* Remember the end of the initial dump. */
2787 int len = obstack_object_size (ob);
2788 tree aka = strip_typedefs (typ);
2789 pp_string (cxx_pp, " {aka");
2790 pp_cxx_whitespace (cxx_pp);
2791 /* And remember the start of the aka dump. */
2792 aka_start = obstack_object_size (ob);
2793 dump_type (aka, flags);
2794 pp_character (cxx_pp, '}');
2795 p = (char*)obstack_base (ob);
2796 /* If they are identical, cut off the aka with a NUL. */
2797 if (memcmp (p, p+aka_start, len) == 0)
2798 p[len] = '\0';
2800 return pp_formatted_text (cxx_pp);
2803 static const char *
2804 assop_to_string (enum tree_code p)
2806 tree id = assignment_operator_name_info[(int) p].identifier;
2807 return id ? IDENTIFIER_POINTER (id) : M_("{unknown}");
2810 static const char *
2811 args_to_string (tree p, int verbose)
2813 int flags = 0;
2814 if (verbose)
2815 flags |= TFF_CLASS_KEY_OR_ENUM;
2817 if (p == NULL_TREE)
2818 return "";
2820 if (TYPE_P (TREE_VALUE (p)))
2821 return type_as_string_translate (p, flags);
2823 reinit_cxx_pp ();
2824 for (; p; p = TREE_CHAIN (p))
2826 if (TREE_VALUE (p) == null_node)
2827 pp_cxx_ws_string (cxx_pp, "NULL");
2828 else
2829 dump_type (error_type (TREE_VALUE (p)), flags);
2830 if (TREE_CHAIN (p))
2831 pp_separate_with_comma (cxx_pp);
2833 return pp_formatted_text (cxx_pp);
2836 /* Pretty-print a deduction substitution (from deduction_tsubst_fntype). P
2837 is a TREE_LIST with purpose the TEMPLATE_DECL, value the template
2838 arguments. */
2840 static const char *
2841 subst_to_string (tree p)
2843 tree decl = TREE_PURPOSE (p);
2844 tree targs = TREE_VALUE (p);
2845 tree tparms = DECL_TEMPLATE_PARMS (decl);
2846 int flags = TFF_DECL_SPECIFIERS|TFF_TEMPLATE_HEADER;
2848 if (p == NULL_TREE)
2849 return "";
2851 reinit_cxx_pp ();
2852 dump_template_decl (TREE_PURPOSE (p), flags);
2853 pp_cxx_whitespace (cxx_pp);
2854 pp_cxx_left_bracket (cxx_pp);
2855 pp_cxx_ws_string (cxx_pp, M_("with"));
2856 pp_cxx_whitespace (cxx_pp);
2857 dump_template_bindings (tparms, targs, NULL);
2858 pp_cxx_right_bracket (cxx_pp);
2859 return pp_formatted_text (cxx_pp);
2862 static const char *
2863 cv_to_string (tree p, int v)
2865 reinit_cxx_pp ();
2866 pp_base (cxx_pp)->padding = v ? pp_before : pp_none;
2867 pp_cxx_cv_qualifier_seq (cxx_pp, p);
2868 return pp_formatted_text (cxx_pp);
2871 /* Langhook for print_error_function. */
2872 void
2873 cxx_print_error_function (diagnostic_context *context, const char *file,
2874 diagnostic_info *diagnostic)
2876 lhd_print_error_function (context, file, diagnostic);
2877 pp_base_set_prefix (context->printer, file);
2878 maybe_print_instantiation_context (context);
2881 static void
2882 cp_diagnostic_starter (diagnostic_context *context,
2883 diagnostic_info *diagnostic)
2885 diagnostic_report_current_module (context, diagnostic->location);
2886 cp_print_error_function (context, diagnostic);
2887 maybe_print_instantiation_context (context);
2888 maybe_print_constexpr_context (context);
2889 pp_base_set_prefix (context->printer, diagnostic_build_prefix (context,
2890 diagnostic));
2893 static void
2894 cp_diagnostic_finalizer (diagnostic_context *context,
2895 diagnostic_info *diagnostic)
2897 virt_loc_aware_diagnostic_finalizer (context, diagnostic);
2898 pp_base_destroy_prefix (context->printer);
2901 /* Print current function onto BUFFER, in the process of reporting
2902 a diagnostic message. Called from cp_diagnostic_starter. */
2903 static void
2904 cp_print_error_function (diagnostic_context *context,
2905 diagnostic_info *diagnostic)
2907 /* If we are in an instantiation context, current_function_decl is likely
2908 to be wrong, so just rely on print_instantiation_full_context. */
2909 if (current_instantiation ())
2910 return;
2911 if (diagnostic_last_function_changed (context, diagnostic))
2913 const char *old_prefix = context->printer->prefix;
2914 const char *file = LOCATION_FILE (diagnostic->location);
2915 tree abstract_origin = diagnostic_abstract_origin (diagnostic);
2916 char *new_prefix = (file && abstract_origin == NULL)
2917 ? file_name_as_prefix (file) : NULL;
2919 pp_base_set_prefix (context->printer, new_prefix);
2921 if (current_function_decl == NULL)
2922 pp_base_string (context->printer, _("At global scope:"));
2923 else
2925 tree fndecl, ao;
2927 if (abstract_origin)
2929 ao = BLOCK_ABSTRACT_ORIGIN (abstract_origin);
2930 while (TREE_CODE (ao) == BLOCK
2931 && BLOCK_ABSTRACT_ORIGIN (ao)
2932 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
2933 ao = BLOCK_ABSTRACT_ORIGIN (ao);
2934 gcc_assert (TREE_CODE (ao) == FUNCTION_DECL);
2935 fndecl = ao;
2937 else
2938 fndecl = current_function_decl;
2940 pp_printf (context->printer, function_category (fndecl),
2941 cxx_printable_name_translate (fndecl, 2));
2943 while (abstract_origin)
2945 location_t *locus;
2946 tree block = abstract_origin;
2948 locus = &BLOCK_SOURCE_LOCATION (block);
2949 fndecl = NULL;
2950 block = BLOCK_SUPERCONTEXT (block);
2951 while (block && TREE_CODE (block) == BLOCK
2952 && BLOCK_ABSTRACT_ORIGIN (block))
2954 ao = BLOCK_ABSTRACT_ORIGIN (block);
2956 while (TREE_CODE (ao) == BLOCK
2957 && BLOCK_ABSTRACT_ORIGIN (ao)
2958 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
2959 ao = BLOCK_ABSTRACT_ORIGIN (ao);
2961 if (TREE_CODE (ao) == FUNCTION_DECL)
2963 fndecl = ao;
2964 break;
2966 else if (TREE_CODE (ao) != BLOCK)
2967 break;
2969 block = BLOCK_SUPERCONTEXT (block);
2971 if (fndecl)
2972 abstract_origin = block;
2973 else
2975 while (block && TREE_CODE (block) == BLOCK)
2976 block = BLOCK_SUPERCONTEXT (block);
2978 if (block && TREE_CODE (block) == FUNCTION_DECL)
2979 fndecl = block;
2980 abstract_origin = NULL;
2982 if (fndecl)
2984 expanded_location s = expand_location (*locus);
2985 pp_base_character (context->printer, ',');
2986 pp_base_newline (context->printer);
2987 if (s.file != NULL)
2989 if (context->show_column && s.column != 0)
2990 pp_printf (context->printer,
2991 _(" inlined from %qs at %s:%d:%d"),
2992 cxx_printable_name_translate (fndecl, 2),
2993 s.file, s.line, s.column);
2994 else
2995 pp_printf (context->printer,
2996 _(" inlined from %qs at %s:%d"),
2997 cxx_printable_name_translate (fndecl, 2),
2998 s.file, s.line);
3001 else
3002 pp_printf (context->printer, _(" inlined from %qs"),
3003 cxx_printable_name_translate (fndecl, 2));
3006 pp_base_character (context->printer, ':');
3008 pp_base_newline (context->printer);
3010 diagnostic_set_last_function (context, diagnostic);
3011 pp_base_destroy_prefix (context->printer);
3012 context->printer->prefix = old_prefix;
3016 /* Returns a description of FUNCTION using standard terminology. The
3017 result is a format string of the form "In CATEGORY %qs". */
3018 static const char *
3019 function_category (tree fn)
3021 /* We can get called from the middle-end for diagnostics of function
3022 clones. Make sure we have language specific information before
3023 dereferencing it. */
3024 if (DECL_LANG_SPECIFIC (STRIP_TEMPLATE (fn))
3025 && DECL_FUNCTION_MEMBER_P (fn))
3027 if (DECL_STATIC_FUNCTION_P (fn))
3028 return _("In static member function %qs");
3029 else if (DECL_COPY_CONSTRUCTOR_P (fn))
3030 return _("In copy constructor %qs");
3031 else if (DECL_CONSTRUCTOR_P (fn))
3032 return _("In constructor %qs");
3033 else if (DECL_DESTRUCTOR_P (fn))
3034 return _("In destructor %qs");
3035 else if (LAMBDA_FUNCTION_P (fn))
3036 return _("In lambda function");
3037 else
3038 return _("In member function %qs");
3040 else
3041 return _("In function %qs");
3044 /* Report the full context of a current template instantiation,
3045 onto BUFFER. */
3046 static void
3047 print_instantiation_full_context (diagnostic_context *context)
3049 struct tinst_level *p = current_instantiation ();
3050 location_t location = input_location;
3052 if (p)
3054 pp_verbatim (context->printer,
3055 TREE_CODE (p->decl) == TREE_LIST
3056 ? _("%s: In substitution of %qS:\n")
3057 : _("%s: In instantiation of %q#D:\n"),
3058 LOCATION_FILE (location),
3059 p->decl);
3061 location = p->locus;
3062 p = p->next;
3065 print_instantiation_partial_context (context, p, location);
3068 /* Helper function of print_instantiation_partial_context() that
3069 prints a single line of instantiation context. */
3071 static void
3072 print_instantiation_partial_context_line (diagnostic_context *context,
3073 const struct tinst_level *t,
3074 location_t loc, bool recursive_p)
3076 expanded_location xloc;
3077 xloc = expand_location (loc);
3079 if (context->show_column)
3080 pp_verbatim (context->printer, _("%s:%d:%d: "),
3081 xloc.file, xloc.line, xloc.column);
3082 else
3083 pp_verbatim (context->printer, _("%s:%d: "),
3084 xloc.file, xloc.line);
3086 if (t != NULL)
3088 if (TREE_CODE (t->decl) == TREE_LIST)
3089 pp_verbatim (context->printer,
3090 recursive_p
3091 ? _("recursively required by substitution of %qS\n")
3092 : _("required by substitution of %qS\n"),
3093 t->decl);
3094 else
3095 pp_verbatim (context->printer,
3096 recursive_p
3097 ? _("recursively required from %q#D\n")
3098 : _("required from %q#D\n"),
3099 t->decl);
3101 else
3103 pp_verbatim (context->printer,
3104 recursive_p
3105 ? _("recursively required from here")
3106 : _("required from here"));
3110 /* Same as print_instantiation_full_context but less verbose. */
3112 static void
3113 print_instantiation_partial_context (diagnostic_context *context,
3114 struct tinst_level *t0, location_t loc)
3116 struct tinst_level *t;
3117 int n_total = 0;
3118 int n;
3119 location_t prev_loc = loc;
3121 for (t = t0; t != NULL; t = t->next)
3122 if (prev_loc != t->locus)
3124 prev_loc = t->locus;
3125 n_total++;
3128 t = t0;
3130 if (template_backtrace_limit
3131 && n_total > template_backtrace_limit)
3133 int skip = n_total - template_backtrace_limit;
3134 int head = template_backtrace_limit / 2;
3136 /* Avoid skipping just 1. If so, skip 2. */
3137 if (skip == 1)
3139 skip = 2;
3140 head = (template_backtrace_limit - 1) / 2;
3143 for (n = 0; n < head; n++)
3145 gcc_assert (t != NULL);
3146 if (loc != t->locus)
3147 print_instantiation_partial_context_line (context, t, loc,
3148 /*recursive_p=*/false);
3149 loc = t->locus;
3150 t = t->next;
3152 if (t != NULL && skip > 0)
3154 expanded_location xloc;
3155 xloc = expand_location (loc);
3156 if (context->show_column)
3157 pp_verbatim (context->printer,
3158 _("%s:%d:%d: [ skipping %d instantiation contexts, "
3159 "use -ftemplate-backtrace-limit=0 to disable ]\n"),
3160 xloc.file, xloc.line, xloc.column, skip);
3161 else
3162 pp_verbatim (context->printer,
3163 _("%s:%d: [ skipping %d instantiation contexts, "
3164 "use -ftemplate-backtrace-limit=0 to disable ]\n"),
3165 xloc.file, xloc.line, skip);
3167 do {
3168 loc = t->locus;
3169 t = t->next;
3170 } while (t != NULL && --skip > 0);
3174 while (t != NULL)
3176 while (t->next != NULL && t->locus == t->next->locus)
3178 loc = t->locus;
3179 t = t->next;
3181 print_instantiation_partial_context_line (context, t, loc,
3182 t->locus == loc);
3183 loc = t->locus;
3184 t = t->next;
3186 print_instantiation_partial_context_line (context, NULL, loc,
3187 /*recursive_p=*/false);
3188 pp_base_newline (context->printer);
3191 /* Called from cp_thing to print the template context for an error. */
3192 static void
3193 maybe_print_instantiation_context (diagnostic_context *context)
3195 if (!problematic_instantiation_changed () || current_instantiation () == 0)
3196 return;
3198 record_last_problematic_instantiation ();
3199 print_instantiation_full_context (context);
3202 /* Report the bare minimum context of a template instantiation. */
3203 void
3204 print_instantiation_context (void)
3206 print_instantiation_partial_context
3207 (global_dc, current_instantiation (), input_location);
3208 pp_base_newline (global_dc->printer);
3209 diagnostic_flush_buffer (global_dc);
3212 /* Report what constexpr call(s) we're trying to expand, if any. */
3214 void
3215 maybe_print_constexpr_context (diagnostic_context *context)
3217 vec<tree> call_stack = cx_error_context ();
3218 unsigned ix;
3219 tree t;
3221 FOR_EACH_VEC_ELT (call_stack, ix, t)
3223 expanded_location xloc = expand_location (EXPR_LOCATION (t));
3224 const char *s = expr_as_string (t, 0);
3225 if (context->show_column)
3226 pp_verbatim (context->printer,
3227 _("%s:%d:%d: in constexpr expansion of %qs"),
3228 xloc.file, xloc.line, xloc.column, s);
3229 else
3230 pp_verbatim (context->printer,
3231 _("%s:%d: in constexpr expansion of %qs"),
3232 xloc.file, xloc.line, s);
3233 pp_base_newline (context->printer);
3237 /* Called from output_format -- during diagnostic message processing --
3238 to handle C++ specific format specifier with the following meanings:
3239 %A function argument-list.
3240 %C tree code.
3241 %D declaration.
3242 %E expression.
3243 %F function declaration.
3244 %L language as used in extern "lang".
3245 %O binary operator.
3246 %P function parameter whose position is indicated by an integer.
3247 %Q assignment operator.
3248 %T type.
3249 %V cv-qualifier. */
3250 static bool
3251 cp_printer (pretty_printer *pp, text_info *text, const char *spec,
3252 int precision, bool wide, bool set_locus, bool verbose)
3254 const char *result;
3255 tree t = NULL;
3256 #define next_tree (t = va_arg (*text->args_ptr, tree))
3257 #define next_tcode ((enum tree_code) va_arg (*text->args_ptr, int))
3258 #define next_lang ((enum languages) va_arg (*text->args_ptr, int))
3259 #define next_int va_arg (*text->args_ptr, int)
3261 if (precision != 0 || wide)
3262 return false;
3264 if (text->locus == NULL)
3265 set_locus = false;
3267 switch (*spec)
3269 case 'A': result = args_to_string (next_tree, verbose); break;
3270 case 'C': result = code_to_string (next_tcode); break;
3271 case 'D':
3273 tree temp = next_tree;
3274 if (DECL_P (temp)
3275 && DECL_DEBUG_EXPR_IS_FROM (temp) && DECL_DEBUG_EXPR (temp))
3277 temp = DECL_DEBUG_EXPR (temp);
3278 if (!DECL_P (temp))
3280 result = expr_to_string (temp);
3281 break;
3284 result = decl_to_string (temp, verbose);
3286 break;
3287 case 'E': result = expr_to_string (next_tree); break;
3288 case 'F': result = fndecl_to_string (next_tree, verbose); break;
3289 case 'L': result = language_to_string (next_lang); break;
3290 case 'O': result = op_to_string (next_tcode); break;
3291 case 'P': result = parm_to_string (next_int); break;
3292 case 'Q': result = assop_to_string (next_tcode); break;
3293 case 'S': result = subst_to_string (next_tree); break;
3294 case 'T': result = type_to_string (next_tree, verbose); break;
3295 case 'V': result = cv_to_string (next_tree, verbose); break;
3297 case 'K':
3298 percent_K_format (text);
3299 return true;
3301 default:
3302 return false;
3305 pp_base_string (pp, result);
3306 if (set_locus && t != NULL)
3307 *text->locus = location_of (t);
3308 return true;
3309 #undef next_tree
3310 #undef next_tcode
3311 #undef next_lang
3312 #undef next_int
3315 /* Warn about the use of C++0x features when appropriate. */
3316 void
3317 maybe_warn_cpp0x (cpp0x_warn_str str)
3319 if ((cxx_dialect == cxx98) && !in_system_header)
3320 /* We really want to suppress this warning in system headers,
3321 because libstdc++ uses variadic templates even when we aren't
3322 in C++0x mode. */
3323 switch (str)
3325 case CPP0X_INITIALIZER_LISTS:
3326 pedwarn (input_location, 0,
3327 "extended initializer lists "
3328 "only available with -std=c++11 or -std=gnu++11");
3329 break;
3330 case CPP0X_EXPLICIT_CONVERSION:
3331 pedwarn (input_location, 0,
3332 "explicit conversion operators "
3333 "only available with -std=c++11 or -std=gnu++11");
3334 break;
3335 case CPP0X_VARIADIC_TEMPLATES:
3336 pedwarn (input_location, 0,
3337 "variadic templates "
3338 "only available with -std=c++11 or -std=gnu++11");
3339 break;
3340 case CPP0X_LAMBDA_EXPR:
3341 pedwarn (input_location, 0,
3342 "lambda expressions "
3343 "only available with -std=c++11 or -std=gnu++11");
3344 break;
3345 case CPP0X_AUTO:
3346 pedwarn (input_location, 0,
3347 "C++0x auto only available with -std=c++11 or -std=gnu++11");
3348 break;
3349 case CPP0X_SCOPED_ENUMS:
3350 pedwarn (input_location, 0,
3351 "scoped enums only available with -std=c++11 or -std=gnu++11");
3352 break;
3353 case CPP0X_DEFAULTED_DELETED:
3354 pedwarn (input_location, 0,
3355 "defaulted and deleted functions "
3356 "only available with -std=c++11 or -std=gnu++11");
3357 break;
3358 case CPP0X_INLINE_NAMESPACES:
3359 pedwarn (input_location, OPT_Wpedantic,
3360 "inline namespaces "
3361 "only available with -std=c++11 or -std=gnu++11");
3362 break;
3363 case CPP0X_OVERRIDE_CONTROLS:
3364 pedwarn (input_location, 0,
3365 "override controls (override/final) "
3366 "only available with -std=c++11 or -std=gnu++11");
3367 break;
3368 case CPP0X_NSDMI:
3369 pedwarn (input_location, 0,
3370 "non-static data member initializers "
3371 "only available with -std=c++11 or -std=gnu++11");
3372 break;
3373 case CPP0X_USER_DEFINED_LITERALS:
3374 pedwarn (input_location, 0,
3375 "user-defined literals "
3376 "only available with -std=c++11 or -std=gnu++11");
3377 break;
3378 case CPP0X_DELEGATING_CTORS:
3379 pedwarn (input_location, 0,
3380 "delegating constructors "
3381 "only available with -std=c++11 or -std=gnu++11");
3382 break;
3383 case CPP0X_INHERITING_CTORS:
3384 pedwarn (input_location, 0,
3385 "inheriting constructors "
3386 "only available with -std=c++11 or -std=gnu++11");
3387 break;
3388 case CPP0X_ATTRIBUTES:
3389 pedwarn (input_location, 0,
3390 "c++11 attributes "
3391 "only available with -std=c++11 or -std=gnu++11");
3392 break;
3393 default:
3394 gcc_unreachable ();
3398 /* Warn about the use of variadic templates when appropriate. */
3399 void
3400 maybe_warn_variadic_templates (void)
3402 maybe_warn_cpp0x (CPP0X_VARIADIC_TEMPLATES);
3406 /* Issue an ISO C++98 pedantic warning at LOCATION, conditional on
3407 option OPT with text GMSGID. Use this function to report
3408 diagnostics for constructs that are invalid C++98, but valid
3409 C++0x. */
3410 bool
3411 pedwarn_cxx98 (location_t location, int opt, const char *gmsgid, ...)
3413 diagnostic_info diagnostic;
3414 va_list ap;
3415 bool ret;
3417 va_start (ap, gmsgid);
3418 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
3419 (cxx_dialect == cxx98) ? DK_PEDWARN : DK_WARNING);
3420 diagnostic.option_index = opt;
3421 ret = report_diagnostic (&diagnostic);
3422 va_end (ap);
3423 return ret;
3426 /* Issue a diagnostic that NAME cannot be found in SCOPE. DECL is what
3427 we found when we tried to do the lookup. LOCATION is the location of
3428 the NAME identifier. */
3430 void
3431 qualified_name_lookup_error (tree scope, tree name,
3432 tree decl, location_t location)
3434 if (scope == error_mark_node)
3435 ; /* We already complained. */
3436 else if (TYPE_P (scope))
3438 if (!COMPLETE_TYPE_P (scope))
3439 error_at (location, "incomplete type %qT used in nested name specifier",
3440 scope);
3441 else if (TREE_CODE (decl) == TREE_LIST)
3443 error_at (location, "reference to %<%T::%D%> is ambiguous",
3444 scope, name);
3445 print_candidates (decl);
3447 else
3448 error_at (location, "%qD is not a member of %qT", name, scope);
3450 else if (scope != global_namespace)
3452 error_at (location, "%qD is not a member of %qD", name, scope);
3453 suggest_alternatives_for (location, name);
3455 else
3457 error_at (location, "%<::%D%> has not been declared", name);
3458 suggest_alternatives_for (location, name);