2012-09-15 Tom de Vries <tom@codesourcery.com>
[official-gcc.git] / gcc / cp / error.c
blob30fa0182a8f468664270f08b8217957f6035dba6
1 /* Call-backs for C++ error reporting.
2 This code is non-reentrant.
3 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2003,
4 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
5 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "cp-tree.h"
28 #include "flags.h"
29 #include "diagnostic.h"
30 #include "tree-diagnostic.h"
31 #include "langhooks-def.h"
32 #include "intl.h"
33 #include "cxx-pretty-print.h"
34 #include "tree-pretty-print.h"
35 #include "pointer-set.h"
36 #include "c-family/c-objc.h"
38 #define pp_separate_with_comma(PP) pp_cxx_separate_with (PP, ',')
39 #define pp_separate_with_semicolon(PP) pp_cxx_separate_with (PP, ';')
41 /* The global buffer where we dump everything. It is there only for
42 transitional purpose. It is expected, in the near future, to be
43 completely removed. */
44 static cxx_pretty_printer scratch_pretty_printer;
45 #define cxx_pp (&scratch_pretty_printer)
47 /* Translate if being used for diagnostics, but not for dump files or
48 __PRETTY_FUNCTION. */
49 #define M_(msgid) (pp_translate_identifiers (cxx_pp) ? _(msgid) : (msgid))
51 # define NEXT_CODE(T) (TREE_CODE (TREE_TYPE (T)))
53 static const char *args_to_string (tree, int);
54 static const char *assop_to_string (enum tree_code);
55 static const char *code_to_string (enum tree_code);
56 static const char *cv_to_string (tree, int);
57 static const char *decl_to_string (tree, int);
58 static const char *expr_to_string (tree);
59 static const char *fndecl_to_string (tree, int);
60 static const char *op_to_string (enum tree_code);
61 static const char *parm_to_string (int);
62 static const char *type_to_string (tree, int);
64 static void dump_alias_template_specialization (tree, int);
65 static void dump_type (tree, int);
66 static void dump_typename (tree, int);
67 static void dump_simple_decl (tree, tree, int);
68 static void dump_decl (tree, int);
69 static void dump_template_decl (tree, int);
70 static void dump_function_decl (tree, int);
71 static void dump_expr (tree, int);
72 static void dump_unary_op (const char *, tree, int);
73 static void dump_binary_op (const char *, tree, int);
74 static void dump_aggr_type (tree, int);
75 static void dump_type_prefix (tree, int);
76 static void dump_type_suffix (tree, int);
77 static void dump_function_name (tree, int);
78 static void dump_call_expr_args (tree, int, bool);
79 static void dump_aggr_init_expr_args (tree, int, bool);
80 static void dump_expr_list (tree, int);
81 static void dump_global_iord (tree);
82 static void dump_parameters (tree, int);
83 static void dump_exception_spec (tree, int);
84 static void dump_template_argument (tree, int);
85 static void dump_template_argument_list (tree, int);
86 static void dump_template_parameter (tree, int);
87 static void dump_template_bindings (tree, tree, VEC(tree,gc) *);
88 static void dump_scope (tree, int);
89 static void dump_template_parms (tree, int, int);
90 static int get_non_default_template_args_count (tree, int);
91 static const char *function_category (tree);
92 static void maybe_print_constexpr_context (diagnostic_context *);
93 static void maybe_print_instantiation_context (diagnostic_context *);
94 static void print_instantiation_full_context (diagnostic_context *);
95 static void print_instantiation_partial_context (diagnostic_context *,
96 struct tinst_level *,
97 location_t);
98 static void cp_diagnostic_starter (diagnostic_context *, diagnostic_info *);
99 static void cp_diagnostic_finalizer (diagnostic_context *, diagnostic_info *);
100 static void cp_print_error_function (diagnostic_context *, diagnostic_info *);
102 static bool cp_printer (pretty_printer *, text_info *, const char *,
103 int, bool, bool, bool);
105 void
106 init_error (void)
108 diagnostic_starter (global_dc) = cp_diagnostic_starter;
109 diagnostic_finalizer (global_dc) = cp_diagnostic_finalizer;
110 diagnostic_format_decoder (global_dc) = cp_printer;
112 pp_construct (pp_base (cxx_pp), NULL, 0);
113 pp_cxx_pretty_printer_init (cxx_pp);
116 /* Dump a scope, if deemed necessary. */
118 static void
119 dump_scope (tree scope, int flags)
121 int f = flags & (TFF_SCOPE | TFF_CHASE_TYPEDEF);
123 if (scope == NULL_TREE)
124 return;
126 if (TREE_CODE (scope) == NAMESPACE_DECL)
128 if (scope != global_namespace)
130 dump_decl (scope, f);
131 pp_cxx_colon_colon (cxx_pp);
134 else if (AGGREGATE_TYPE_P (scope))
136 dump_type (scope, f);
137 pp_cxx_colon_colon (cxx_pp);
139 else if ((flags & TFF_SCOPE) && TREE_CODE (scope) == FUNCTION_DECL)
141 dump_function_decl (scope, f);
142 pp_cxx_colon_colon (cxx_pp);
146 /* Dump the template ARGument under control of FLAGS. */
148 static void
149 dump_template_argument (tree arg, int flags)
151 if (ARGUMENT_PACK_P (arg))
152 dump_template_argument_list (ARGUMENT_PACK_ARGS (arg),
153 /* No default args in argument packs. */
154 flags|TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS);
155 else if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
156 dump_type (arg, flags & ~TFF_CLASS_KEY_OR_ENUM);
157 else
159 if (TREE_CODE (arg) == TREE_LIST)
160 arg = TREE_VALUE (arg);
162 dump_expr (arg, (flags | TFF_EXPR_IN_PARENS) & ~TFF_CLASS_KEY_OR_ENUM);
166 /* Count the number of template arguments ARGS whose value does not
167 match the (optional) default template parameter in PARAMS */
169 static int
170 get_non_default_template_args_count (tree args, int flags)
172 int n = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_ARGS (args));
174 if (/* We use this flag when generating debug information. We don't
175 want to expand templates at this point, for this may generate
176 new decls, which gets decl counts out of sync, which may in
177 turn cause codegen differences between compilations with and
178 without -g. */
179 (flags & TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS) != 0
180 || !flag_pretty_templates)
181 return n;
183 return GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (INNERMOST_TEMPLATE_ARGS (args));
186 /* Dump a template-argument-list ARGS (always a TREE_VEC) under control
187 of FLAGS. */
189 static void
190 dump_template_argument_list (tree args, int flags)
192 int n = get_non_default_template_args_count (args, flags);
193 int need_comma = 0;
194 int i;
196 for (i = 0; i < n; ++i)
198 tree arg = TREE_VEC_ELT (args, i);
200 /* Only print a comma if we know there is an argument coming. In
201 the case of an empty template argument pack, no actual
202 argument will be printed. */
203 if (need_comma
204 && (!ARGUMENT_PACK_P (arg)
205 || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) > 0))
206 pp_separate_with_comma (cxx_pp);
208 dump_template_argument (arg, flags);
209 need_comma = 1;
213 /* Dump a template parameter PARM (a TREE_LIST) under control of FLAGS. */
215 static void
216 dump_template_parameter (tree parm, int flags)
218 tree p;
219 tree a;
221 if (parm == error_mark_node)
222 return;
224 p = TREE_VALUE (parm);
225 a = TREE_PURPOSE (parm);
227 if (TREE_CODE (p) == TYPE_DECL)
229 if (flags & TFF_DECL_SPECIFIERS)
231 pp_cxx_ws_string (cxx_pp, "class");
232 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (p)))
233 pp_cxx_ws_string (cxx_pp, "...");
234 if (DECL_NAME (p))
235 pp_cxx_tree_identifier (cxx_pp, DECL_NAME (p));
237 else if (DECL_NAME (p))
238 pp_cxx_tree_identifier (cxx_pp, DECL_NAME (p));
239 else
240 pp_cxx_canonical_template_parameter (cxx_pp, TREE_TYPE (p));
242 else
243 dump_decl (p, flags | TFF_DECL_SPECIFIERS);
245 if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && a != NULL_TREE)
247 pp_cxx_whitespace (cxx_pp);
248 pp_equal (cxx_pp);
249 pp_cxx_whitespace (cxx_pp);
250 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
251 dump_type (a, flags & ~TFF_CHASE_TYPEDEF);
252 else
253 dump_expr (a, flags | TFF_EXPR_IN_PARENS);
257 /* Dump, under control of FLAGS, a template-parameter-list binding.
258 PARMS is a TREE_LIST of TREE_VEC of TREE_LIST and ARGS is a
259 TREE_VEC. */
261 static void
262 dump_template_bindings (tree parms, tree args, VEC(tree,gc)* typenames)
264 bool need_semicolon = false;
265 int i;
266 tree t;
268 while (parms)
270 tree p = TREE_VALUE (parms);
271 int lvl = TMPL_PARMS_DEPTH (parms);
272 int arg_idx = 0;
273 int i;
274 tree lvl_args = NULL_TREE;
276 /* Don't crash if we had an invalid argument list. */
277 if (TMPL_ARGS_DEPTH (args) >= lvl)
278 lvl_args = TMPL_ARGS_LEVEL (args, lvl);
280 for (i = 0; i < TREE_VEC_LENGTH (p); ++i)
282 tree arg = NULL_TREE;
284 /* Don't crash if we had an invalid argument list. */
285 if (lvl_args && NUM_TMPL_ARGS (lvl_args) > arg_idx)
286 arg = TREE_VEC_ELT (lvl_args, arg_idx);
288 if (need_semicolon)
289 pp_separate_with_semicolon (cxx_pp);
290 dump_template_parameter (TREE_VEC_ELT (p, i), TFF_PLAIN_IDENTIFIER);
291 pp_cxx_whitespace (cxx_pp);
292 pp_equal (cxx_pp);
293 pp_cxx_whitespace (cxx_pp);
294 if (arg)
296 if (ARGUMENT_PACK_P (arg))
297 pp_cxx_left_brace (cxx_pp);
298 dump_template_argument (arg, TFF_PLAIN_IDENTIFIER);
299 if (ARGUMENT_PACK_P (arg))
300 pp_cxx_right_brace (cxx_pp);
302 else
303 pp_string (cxx_pp, M_("<missing>"));
305 ++arg_idx;
306 need_semicolon = true;
309 parms = TREE_CHAIN (parms);
312 /* Don't bother with typenames for a partial instantiation. */
313 if (VEC_empty (tree, typenames) || uses_template_parms (args))
314 return;
316 FOR_EACH_VEC_ELT (tree, typenames, i, t)
318 if (need_semicolon)
319 pp_separate_with_semicolon (cxx_pp);
320 dump_type (t, TFF_PLAIN_IDENTIFIER);
321 pp_cxx_whitespace (cxx_pp);
322 pp_equal (cxx_pp);
323 pp_cxx_whitespace (cxx_pp);
324 push_deferring_access_checks (dk_no_check);
325 t = tsubst (t, args, tf_none, NULL_TREE);
326 pop_deferring_access_checks ();
327 /* Strip typedefs. We can't just use TFF_CHASE_TYPEDEF because
328 pp_simple_type_specifier doesn't know about it. */
329 t = strip_typedefs (t);
330 dump_type (t, TFF_PLAIN_IDENTIFIER);
334 /* Dump a human-readable equivalent of the alias template
335 specialization of T. */
337 static void
338 dump_alias_template_specialization (tree t, int flags)
340 tree name;
342 gcc_assert (alias_template_specialization_p (t));
344 if (!(flags & TFF_UNQUALIFIED_NAME))
345 dump_scope (CP_DECL_CONTEXT (TYPE_NAME (t)), flags);
346 name = TYPE_IDENTIFIER (t);
347 pp_cxx_tree_identifier (cxx_pp, name);
348 dump_template_parms (TYPE_TEMPLATE_INFO (t),
349 /*primary=*/false,
350 flags & ~TFF_TEMPLATE_HEADER);
353 /* Dump a human-readable equivalent of TYPE. FLAGS controls the
354 format. */
356 static void
357 dump_type (tree t, int flags)
359 if (t == NULL_TREE)
360 return;
362 /* Don't print e.g. "struct mytypedef". */
363 if (TYPE_P (t) && typedef_variant_p (t))
365 tree decl = TYPE_NAME (t);
366 if ((flags & TFF_CHASE_TYPEDEF)
367 || DECL_SELF_REFERENCE_P (decl)
368 || (!flag_pretty_templates
369 && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)))
370 t = strip_typedefs (t);
371 else if (alias_template_specialization_p (t))
373 dump_alias_template_specialization (t, flags);
374 return;
376 else if (same_type_p (t, TREE_TYPE (decl)))
377 t = decl;
378 else
380 pp_cxx_cv_qualifier_seq (cxx_pp, t);
381 pp_cxx_tree_identifier (cxx_pp, TYPE_IDENTIFIER (t));
382 return;
386 if (TYPE_PTRMEMFUNC_P (t))
387 goto offset_type;
389 switch (TREE_CODE (t))
391 case LANG_TYPE:
392 if (t == init_list_type_node)
393 pp_string (cxx_pp, M_("<brace-enclosed initializer list>"));
394 else if (t == unknown_type_node)
395 pp_string (cxx_pp, M_("<unresolved overloaded function type>"));
396 else
398 pp_cxx_cv_qualifier_seq (cxx_pp, t);
399 pp_cxx_tree_identifier (cxx_pp, TYPE_IDENTIFIER (t));
401 break;
403 case TREE_LIST:
404 /* A list of function parms. */
405 dump_parameters (t, flags);
406 break;
408 case IDENTIFIER_NODE:
409 pp_cxx_tree_identifier (cxx_pp, t);
410 break;
412 case TREE_BINFO:
413 dump_type (BINFO_TYPE (t), flags);
414 break;
416 case RECORD_TYPE:
417 case UNION_TYPE:
418 case ENUMERAL_TYPE:
419 dump_aggr_type (t, flags);
420 break;
422 case TYPE_DECL:
423 if (flags & TFF_CHASE_TYPEDEF)
425 dump_type (DECL_ORIGINAL_TYPE (t)
426 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t), flags);
427 break;
429 /* Else fall through. */
431 case TEMPLATE_DECL:
432 case NAMESPACE_DECL:
433 dump_decl (t, flags & ~TFF_DECL_SPECIFIERS);
434 break;
436 case INTEGER_TYPE:
437 case REAL_TYPE:
438 case VOID_TYPE:
439 case BOOLEAN_TYPE:
440 case COMPLEX_TYPE:
441 case VECTOR_TYPE:
442 case FIXED_POINT_TYPE:
443 pp_type_specifier_seq (cxx_pp, t);
444 break;
446 case TEMPLATE_TEMPLATE_PARM:
447 /* For parameters inside template signature. */
448 if (TYPE_IDENTIFIER (t))
449 pp_cxx_tree_identifier (cxx_pp, TYPE_IDENTIFIER (t));
450 else
451 pp_cxx_canonical_template_parameter (cxx_pp, t);
452 break;
454 case BOUND_TEMPLATE_TEMPLATE_PARM:
456 tree args = TYPE_TI_ARGS (t);
457 pp_cxx_cv_qualifier_seq (cxx_pp, t);
458 pp_cxx_tree_identifier (cxx_pp, TYPE_IDENTIFIER (t));
459 pp_cxx_begin_template_argument_list (cxx_pp);
460 dump_template_argument_list (args, flags);
461 pp_cxx_end_template_argument_list (cxx_pp);
463 break;
465 case TEMPLATE_TYPE_PARM:
466 pp_cxx_cv_qualifier_seq (cxx_pp, t);
467 if (TYPE_IDENTIFIER (t))
468 pp_cxx_tree_identifier (cxx_pp, TYPE_IDENTIFIER (t));
469 else
470 pp_cxx_canonical_template_parameter
471 (cxx_pp, TEMPLATE_TYPE_PARM_INDEX (t));
472 break;
474 /* This is not always necessary for pointers and such, but doing this
475 reduces code size. */
476 case ARRAY_TYPE:
477 case POINTER_TYPE:
478 case REFERENCE_TYPE:
479 case OFFSET_TYPE:
480 offset_type:
481 case FUNCTION_TYPE:
482 case METHOD_TYPE:
484 dump_type_prefix (t, flags);
485 dump_type_suffix (t, flags);
486 break;
488 case TYPENAME_TYPE:
489 if (! (flags & TFF_CHASE_TYPEDEF)
490 && DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
492 dump_decl (TYPE_NAME (t), TFF_PLAIN_IDENTIFIER);
493 break;
495 pp_cxx_cv_qualifier_seq (cxx_pp, t);
496 pp_cxx_ws_string (cxx_pp,
497 TYPENAME_IS_ENUM_P (t) ? "enum"
498 : TYPENAME_IS_CLASS_P (t) ? "class"
499 : "typename");
500 dump_typename (t, flags);
501 break;
503 case UNBOUND_CLASS_TEMPLATE:
504 if (! (flags & TFF_UNQUALIFIED_NAME))
506 dump_type (TYPE_CONTEXT (t), flags);
507 pp_cxx_colon_colon (cxx_pp);
509 pp_cxx_ws_string (cxx_pp, "template");
510 dump_type (DECL_NAME (TYPE_NAME (t)), flags);
511 break;
513 case TYPEOF_TYPE:
514 pp_cxx_ws_string (cxx_pp, "__typeof__");
515 pp_cxx_whitespace (cxx_pp);
516 pp_cxx_left_paren (cxx_pp);
517 dump_expr (TYPEOF_TYPE_EXPR (t), flags & ~TFF_EXPR_IN_PARENS);
518 pp_cxx_right_paren (cxx_pp);
519 break;
521 case UNDERLYING_TYPE:
522 pp_cxx_ws_string (cxx_pp, "__underlying_type");
523 pp_cxx_whitespace (cxx_pp);
524 pp_cxx_left_paren (cxx_pp);
525 dump_expr (UNDERLYING_TYPE_TYPE (t), flags & ~TFF_EXPR_IN_PARENS);
526 pp_cxx_right_paren (cxx_pp);
527 break;
529 case TYPE_PACK_EXPANSION:
530 dump_type (PACK_EXPANSION_PATTERN (t), flags);
531 pp_cxx_ws_string (cxx_pp, "...");
532 break;
534 case TYPE_ARGUMENT_PACK:
535 dump_template_argument (t, flags);
536 break;
538 case DECLTYPE_TYPE:
539 pp_cxx_ws_string (cxx_pp, "decltype");
540 pp_cxx_whitespace (cxx_pp);
541 pp_cxx_left_paren (cxx_pp);
542 dump_expr (DECLTYPE_TYPE_EXPR (t), flags & ~TFF_EXPR_IN_PARENS);
543 pp_cxx_right_paren (cxx_pp);
544 break;
546 case NULLPTR_TYPE:
547 pp_string (cxx_pp, "std::nullptr_t");
548 break;
550 default:
551 pp_unsupported_tree (cxx_pp, t);
552 /* Fall through to error. */
554 case ERROR_MARK:
555 pp_string (cxx_pp, M_("<type error>"));
556 break;
560 /* Dump a TYPENAME_TYPE. We need to notice when the context is itself
561 a TYPENAME_TYPE. */
563 static void
564 dump_typename (tree t, int flags)
566 tree ctx = TYPE_CONTEXT (t);
568 if (TREE_CODE (ctx) == TYPENAME_TYPE)
569 dump_typename (ctx, flags);
570 else
571 dump_type (ctx, flags & ~TFF_CLASS_KEY_OR_ENUM);
572 pp_cxx_colon_colon (cxx_pp);
573 dump_decl (TYPENAME_TYPE_FULLNAME (t), flags);
576 /* Return the name of the supplied aggregate, or enumeral type. */
578 const char *
579 class_key_or_enum_as_string (tree t)
581 if (TREE_CODE (t) == ENUMERAL_TYPE)
583 if (SCOPED_ENUM_P (t))
584 return "enum class";
585 else
586 return "enum";
588 else if (TREE_CODE (t) == UNION_TYPE)
589 return "union";
590 else if (TYPE_LANG_SPECIFIC (t) && CLASSTYPE_DECLARED_CLASS (t))
591 return "class";
592 else
593 return "struct";
596 /* Print out a class declaration T under the control of FLAGS,
597 in the form `class foo'. */
599 static void
600 dump_aggr_type (tree t, int flags)
602 tree name;
603 const char *variety = class_key_or_enum_as_string (t);
604 int typdef = 0;
605 int tmplate = 0;
607 pp_cxx_cv_qualifier_seq (cxx_pp, t);
609 if (flags & TFF_CLASS_KEY_OR_ENUM)
610 pp_cxx_ws_string (cxx_pp, variety);
612 name = TYPE_NAME (t);
614 if (name)
616 typdef = (!DECL_ARTIFICIAL (name)
617 /* An alias specialization is not considered to be a
618 typedef. */
619 && !alias_template_specialization_p (t));
621 if ((typdef
622 && ((flags & TFF_CHASE_TYPEDEF)
623 || (!flag_pretty_templates && DECL_LANG_SPECIFIC (name)
624 && DECL_TEMPLATE_INFO (name))))
625 || DECL_SELF_REFERENCE_P (name))
627 t = TYPE_MAIN_VARIANT (t);
628 name = TYPE_NAME (t);
629 typdef = 0;
632 tmplate = !typdef && TREE_CODE (t) != ENUMERAL_TYPE
633 && TYPE_LANG_SPECIFIC (t) && CLASSTYPE_TEMPLATE_INFO (t)
634 && (TREE_CODE (CLASSTYPE_TI_TEMPLATE (t)) != TEMPLATE_DECL
635 || PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
637 if (! (flags & TFF_UNQUALIFIED_NAME))
638 dump_scope (CP_DECL_CONTEXT (name), flags | TFF_SCOPE);
639 flags &= ~TFF_UNQUALIFIED_NAME;
640 if (tmplate)
642 /* Because the template names are mangled, we have to locate
643 the most general template, and use that name. */
644 tree tpl = TYPE_TI_TEMPLATE (t);
646 while (DECL_TEMPLATE_INFO (tpl))
647 tpl = DECL_TI_TEMPLATE (tpl);
648 name = tpl;
650 name = DECL_NAME (name);
653 if (name == 0 || ANON_AGGRNAME_P (name))
655 if (flags & TFF_CLASS_KEY_OR_ENUM)
656 pp_string (cxx_pp, M_("<anonymous>"));
657 else
658 pp_printf (pp_base (cxx_pp), M_("<anonymous %s>"), variety);
660 else if (LAMBDA_TYPE_P (name))
662 /* A lambda's "type" is essentially its signature. */
663 pp_string (cxx_pp, M_("<lambda"));
664 if (lambda_function (t))
665 dump_parameters (FUNCTION_FIRST_USER_PARMTYPE (lambda_function (t)),
666 flags);
667 pp_character(cxx_pp, '>');
669 else
670 pp_cxx_tree_identifier (cxx_pp, name);
671 if (tmplate)
672 dump_template_parms (TYPE_TEMPLATE_INFO (t),
673 !CLASSTYPE_USE_TEMPLATE (t),
674 flags & ~TFF_TEMPLATE_HEADER);
677 /* Dump into the obstack the initial part of the output for a given type.
678 This is necessary when dealing with things like functions returning
679 functions. Examples:
681 return type of `int (* fee ())()': pointer -> function -> int. Both
682 pointer (and reference and offset) and function (and member) types must
683 deal with prefix and suffix.
685 Arrays must also do this for DECL nodes, like int a[], and for things like
686 int *[]&. */
688 static void
689 dump_type_prefix (tree t, int flags)
691 if (TYPE_PTRMEMFUNC_P (t))
693 t = TYPE_PTRMEMFUNC_FN_TYPE (t);
694 goto offset_type;
697 switch (TREE_CODE (t))
699 case POINTER_TYPE:
700 case REFERENCE_TYPE:
702 tree sub = TREE_TYPE (t);
704 dump_type_prefix (sub, flags);
705 if (TREE_CODE (sub) == ARRAY_TYPE
706 || TREE_CODE (sub) == FUNCTION_TYPE)
708 pp_cxx_whitespace (cxx_pp);
709 pp_cxx_left_paren (cxx_pp);
710 pp_c_attributes_display (pp_c_base (cxx_pp),
711 TYPE_ATTRIBUTES (sub));
713 if (TREE_CODE (t) == POINTER_TYPE)
714 pp_character(cxx_pp, '*');
715 else if (TREE_CODE (t) == REFERENCE_TYPE)
717 if (TYPE_REF_IS_RVALUE (t))
718 pp_string (cxx_pp, "&&");
719 else
720 pp_character (cxx_pp, '&');
722 pp_base (cxx_pp)->padding = pp_before;
723 pp_cxx_cv_qualifier_seq (cxx_pp, t);
725 break;
727 case OFFSET_TYPE:
728 offset_type:
729 dump_type_prefix (TREE_TYPE (t), flags);
730 if (TREE_CODE (t) == OFFSET_TYPE) /* pmfs deal with this in d_t_p */
732 pp_maybe_space (cxx_pp);
733 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
734 pp_cxx_left_paren (cxx_pp);
735 dump_type (TYPE_OFFSET_BASETYPE (t), flags);
736 pp_cxx_colon_colon (cxx_pp);
738 pp_cxx_star (cxx_pp);
739 pp_cxx_cv_qualifier_seq (cxx_pp, t);
740 pp_base (cxx_pp)->padding = pp_before;
741 break;
743 /* This can be reached without a pointer when dealing with
744 templates, e.g. std::is_function. */
745 case FUNCTION_TYPE:
746 dump_type_prefix (TREE_TYPE (t), flags);
747 break;
749 case METHOD_TYPE:
750 dump_type_prefix (TREE_TYPE (t), flags);
751 pp_maybe_space (cxx_pp);
752 pp_cxx_left_paren (cxx_pp);
753 dump_aggr_type (TYPE_METHOD_BASETYPE (t), flags);
754 pp_cxx_colon_colon (cxx_pp);
755 break;
757 case ARRAY_TYPE:
758 dump_type_prefix (TREE_TYPE (t), flags);
759 break;
761 case ENUMERAL_TYPE:
762 case IDENTIFIER_NODE:
763 case INTEGER_TYPE:
764 case BOOLEAN_TYPE:
765 case REAL_TYPE:
766 case RECORD_TYPE:
767 case TEMPLATE_TYPE_PARM:
768 case TEMPLATE_TEMPLATE_PARM:
769 case BOUND_TEMPLATE_TEMPLATE_PARM:
770 case TREE_LIST:
771 case TYPE_DECL:
772 case TREE_VEC:
773 case UNION_TYPE:
774 case LANG_TYPE:
775 case VOID_TYPE:
776 case TYPENAME_TYPE:
777 case COMPLEX_TYPE:
778 case VECTOR_TYPE:
779 case TYPEOF_TYPE:
780 case UNDERLYING_TYPE:
781 case DECLTYPE_TYPE:
782 case TYPE_PACK_EXPANSION:
783 case FIXED_POINT_TYPE:
784 case NULLPTR_TYPE:
785 dump_type (t, flags);
786 pp_base (cxx_pp)->padding = pp_before;
787 break;
789 default:
790 pp_unsupported_tree (cxx_pp, t);
791 /* fall through. */
792 case ERROR_MARK:
793 pp_string (cxx_pp, M_("<typeprefixerror>"));
794 break;
798 /* Dump the suffix of type T, under control of FLAGS. This is the part
799 which appears after the identifier (or function parms). */
801 static void
802 dump_type_suffix (tree t, int flags)
804 if (TYPE_PTRMEMFUNC_P (t))
805 t = TYPE_PTRMEMFUNC_FN_TYPE (t);
807 switch (TREE_CODE (t))
809 case POINTER_TYPE:
810 case REFERENCE_TYPE:
811 case OFFSET_TYPE:
812 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
813 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
814 pp_cxx_right_paren (cxx_pp);
815 dump_type_suffix (TREE_TYPE (t), flags);
816 break;
818 case FUNCTION_TYPE:
819 case METHOD_TYPE:
821 tree arg;
822 if (TREE_CODE (t) == METHOD_TYPE)
823 /* Can only be reached through a pointer. */
824 pp_cxx_right_paren (cxx_pp);
825 arg = TYPE_ARG_TYPES (t);
826 if (TREE_CODE (t) == METHOD_TYPE)
827 arg = TREE_CHAIN (arg);
829 /* Function pointers don't have default args. Not in standard C++,
830 anyway; they may in g++, but we'll just pretend otherwise. */
831 dump_parameters (arg, flags & ~TFF_FUNCTION_DEFAULT_ARGUMENTS);
833 if (TREE_CODE (t) == METHOD_TYPE)
834 pp_cxx_cv_qualifier_seq (cxx_pp, class_of_this_parm (t));
835 else
836 pp_cxx_cv_qualifier_seq (cxx_pp, t);
837 dump_exception_spec (TYPE_RAISES_EXCEPTIONS (t), flags);
838 dump_type_suffix (TREE_TYPE (t), flags);
839 break;
842 case ARRAY_TYPE:
843 pp_maybe_space (cxx_pp);
844 pp_cxx_left_bracket (cxx_pp);
845 if (TYPE_DOMAIN (t))
847 tree dtype = TYPE_DOMAIN (t);
848 tree max = TYPE_MAX_VALUE (dtype);
849 if (integer_all_onesp (max))
850 pp_character (cxx_pp, '0');
851 else if (host_integerp (max, 0))
852 pp_wide_integer (cxx_pp, tree_low_cst (max, 0) + 1);
853 else if (TREE_CODE (max) == MINUS_EXPR)
854 dump_expr (TREE_OPERAND (max, 0),
855 flags & ~TFF_EXPR_IN_PARENS);
856 else
857 dump_expr (fold_build2_loc (input_location,
858 PLUS_EXPR, dtype, max,
859 build_int_cst (dtype, 1)),
860 flags & ~TFF_EXPR_IN_PARENS);
862 pp_cxx_right_bracket (cxx_pp);
863 dump_type_suffix (TREE_TYPE (t), flags);
864 break;
866 case ENUMERAL_TYPE:
867 case IDENTIFIER_NODE:
868 case INTEGER_TYPE:
869 case BOOLEAN_TYPE:
870 case REAL_TYPE:
871 case RECORD_TYPE:
872 case TEMPLATE_TYPE_PARM:
873 case TEMPLATE_TEMPLATE_PARM:
874 case BOUND_TEMPLATE_TEMPLATE_PARM:
875 case TREE_LIST:
876 case TYPE_DECL:
877 case TREE_VEC:
878 case UNION_TYPE:
879 case LANG_TYPE:
880 case VOID_TYPE:
881 case TYPENAME_TYPE:
882 case COMPLEX_TYPE:
883 case VECTOR_TYPE:
884 case TYPEOF_TYPE:
885 case UNDERLYING_TYPE:
886 case DECLTYPE_TYPE:
887 case TYPE_PACK_EXPANSION:
888 case FIXED_POINT_TYPE:
889 case NULLPTR_TYPE:
890 break;
892 default:
893 pp_unsupported_tree (cxx_pp, t);
894 case ERROR_MARK:
895 /* Don't mark it here, we should have already done in
896 dump_type_prefix. */
897 break;
901 static void
902 dump_global_iord (tree t)
904 const char *p = NULL;
906 if (DECL_GLOBAL_CTOR_P (t))
907 p = M_("(static initializers for %s)");
908 else if (DECL_GLOBAL_DTOR_P (t))
909 p = M_("(static destructors for %s)");
910 else
911 gcc_unreachable ();
913 pp_printf (pp_base (cxx_pp), p, input_filename);
916 static void
917 dump_simple_decl (tree t, tree type, int flags)
919 if (flags & TFF_DECL_SPECIFIERS)
921 if (TREE_CODE (t) == VAR_DECL
922 && DECL_DECLARED_CONSTEXPR_P (t))
923 pp_cxx_ws_string (cxx_pp, "constexpr");
924 dump_type_prefix (type, flags & ~TFF_UNQUALIFIED_NAME);
925 pp_maybe_space (cxx_pp);
927 if (! (flags & TFF_UNQUALIFIED_NAME)
928 && TREE_CODE (t) != PARM_DECL
929 && (!DECL_INITIAL (t)
930 || TREE_CODE (DECL_INITIAL (t)) != TEMPLATE_PARM_INDEX))
931 dump_scope (CP_DECL_CONTEXT (t), flags);
932 flags &= ~TFF_UNQUALIFIED_NAME;
933 if ((flags & TFF_DECL_SPECIFIERS)
934 && DECL_TEMPLATE_PARM_P (t)
935 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (t)))
936 pp_string (cxx_pp, "...");
937 if (DECL_NAME (t))
938 dump_decl (DECL_NAME (t), flags);
939 else
940 pp_string (cxx_pp, M_("<anonymous>"));
941 if (flags & TFF_DECL_SPECIFIERS)
942 dump_type_suffix (type, flags);
945 /* Dump a human readable string for the decl T under control of FLAGS. */
947 static void
948 dump_decl (tree t, int flags)
950 if (t == NULL_TREE)
951 return;
953 /* If doing Objective-C++, give Objective-C a chance to demangle
954 Objective-C method names. */
955 if (c_dialect_objc ())
957 const char *demangled = objc_maybe_printable_name (t, flags);
958 if (demangled)
960 pp_string (cxx_pp, demangled);
961 return;
965 switch (TREE_CODE (t))
967 case TYPE_DECL:
968 /* Don't say 'typedef class A' */
969 if (DECL_ARTIFICIAL (t) && !DECL_SELF_REFERENCE_P (t))
971 if ((flags & TFF_DECL_SPECIFIERS)
972 && TREE_CODE (TREE_TYPE (t)) == TEMPLATE_TYPE_PARM)
974 /* Say `class T' not just `T'. */
975 pp_cxx_ws_string (cxx_pp, "class");
977 /* Emit the `...' for a parameter pack. */
978 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
979 pp_cxx_ws_string (cxx_pp, "...");
982 dump_type (TREE_TYPE (t), flags);
983 break;
985 if (TYPE_DECL_ALIAS_P (t)
986 && (flags & TFF_DECL_SPECIFIERS
987 || flags & TFF_CLASS_KEY_OR_ENUM))
989 pp_cxx_ws_string (cxx_pp, "using");
990 dump_decl (DECL_NAME (t), flags);
991 pp_cxx_whitespace (cxx_pp);
992 pp_cxx_ws_string (cxx_pp, "=");
993 pp_cxx_whitespace (cxx_pp);
994 dump_type (DECL_ORIGINAL_TYPE (t), flags);
995 break;
997 if ((flags & TFF_DECL_SPECIFIERS)
998 && !DECL_SELF_REFERENCE_P (t))
999 pp_cxx_ws_string (cxx_pp, "typedef");
1000 dump_simple_decl (t, DECL_ORIGINAL_TYPE (t)
1001 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t),
1002 flags);
1003 break;
1005 case VAR_DECL:
1006 if (DECL_NAME (t) && VTABLE_NAME_P (DECL_NAME (t)))
1008 pp_string (cxx_pp, M_("vtable for "));
1009 gcc_assert (TYPE_P (DECL_CONTEXT (t)));
1010 dump_type (DECL_CONTEXT (t), flags);
1011 break;
1013 /* Else fall through. */
1014 case FIELD_DECL:
1015 case PARM_DECL:
1016 dump_simple_decl (t, TREE_TYPE (t), flags);
1017 break;
1019 case RESULT_DECL:
1020 pp_string (cxx_pp, M_("<return value> "));
1021 dump_simple_decl (t, TREE_TYPE (t), flags);
1022 break;
1024 case NAMESPACE_DECL:
1025 if (flags & TFF_DECL_SPECIFIERS)
1026 pp_cxx_declaration (cxx_pp, t);
1027 else
1029 if (! (flags & TFF_UNQUALIFIED_NAME))
1030 dump_scope (CP_DECL_CONTEXT (t), flags);
1031 flags &= ~TFF_UNQUALIFIED_NAME;
1032 if (DECL_NAME (t) == NULL_TREE)
1034 if (!(pp_c_base (cxx_pp)->flags & pp_c_flag_gnu_v3))
1035 pp_cxx_ws_string (cxx_pp, M_("{anonymous}"));
1036 else
1037 pp_cxx_ws_string (cxx_pp, M_("(anonymous namespace)"));
1039 else
1040 pp_cxx_tree_identifier (cxx_pp, DECL_NAME (t));
1042 break;
1044 case SCOPE_REF:
1045 dump_type (TREE_OPERAND (t, 0), flags);
1046 pp_string (cxx_pp, "::");
1047 dump_decl (TREE_OPERAND (t, 1), TFF_UNQUALIFIED_NAME);
1048 break;
1050 case ARRAY_REF:
1051 dump_decl (TREE_OPERAND (t, 0), flags);
1052 pp_cxx_left_bracket (cxx_pp);
1053 dump_decl (TREE_OPERAND (t, 1), flags);
1054 pp_cxx_right_bracket (cxx_pp);
1055 break;
1057 /* So that we can do dump_decl on an aggr type. */
1058 case RECORD_TYPE:
1059 case UNION_TYPE:
1060 case ENUMERAL_TYPE:
1061 dump_type (t, flags);
1062 break;
1064 case BIT_NOT_EXPR:
1065 /* This is a pseudo destructor call which has not been folded into
1066 a PSEUDO_DTOR_EXPR yet. */
1067 pp_cxx_complement (cxx_pp);
1068 dump_type (TREE_OPERAND (t, 0), flags);
1069 break;
1071 case TYPE_EXPR:
1072 gcc_unreachable ();
1073 break;
1075 /* These special cases are duplicated here so that other functions
1076 can feed identifiers to error and get them demangled properly. */
1077 case IDENTIFIER_NODE:
1078 if (IDENTIFIER_TYPENAME_P (t))
1080 pp_cxx_ws_string (cxx_pp, "operator");
1081 /* Not exactly IDENTIFIER_TYPE_VALUE. */
1082 dump_type (TREE_TYPE (t), flags);
1083 break;
1085 else
1086 pp_cxx_tree_identifier (cxx_pp, t);
1087 break;
1089 case OVERLOAD:
1090 if (OVL_CHAIN (t))
1092 t = OVL_CURRENT (t);
1093 if (DECL_CLASS_SCOPE_P (t))
1095 dump_type (DECL_CONTEXT (t), flags);
1096 pp_cxx_colon_colon (cxx_pp);
1098 else if (!DECL_FILE_SCOPE_P (t))
1100 dump_decl (DECL_CONTEXT (t), flags);
1101 pp_cxx_colon_colon (cxx_pp);
1103 dump_decl (DECL_NAME (t), flags);
1104 break;
1107 /* If there's only one function, just treat it like an ordinary
1108 FUNCTION_DECL. */
1109 t = OVL_CURRENT (t);
1110 /* Fall through. */
1112 case FUNCTION_DECL:
1113 if (! DECL_LANG_SPECIFIC (t))
1114 pp_string (cxx_pp, M_("<built-in>"));
1115 else if (DECL_GLOBAL_CTOR_P (t) || DECL_GLOBAL_DTOR_P (t))
1116 dump_global_iord (t);
1117 else
1118 dump_function_decl (t, flags);
1119 break;
1121 case TEMPLATE_DECL:
1122 dump_template_decl (t, flags);
1123 break;
1125 case TEMPLATE_ID_EXPR:
1127 tree name = TREE_OPERAND (t, 0);
1128 tree args = TREE_OPERAND (t, 1);
1130 if (is_overloaded_fn (name))
1131 name = DECL_NAME (get_first_fn (name));
1132 dump_decl (name, flags);
1133 pp_cxx_begin_template_argument_list (cxx_pp);
1134 if (args == error_mark_node)
1135 pp_string (cxx_pp, M_("<template arguments error>"));
1136 else if (args)
1137 dump_template_argument_list (args, flags);
1138 pp_cxx_end_template_argument_list (cxx_pp);
1140 break;
1142 case LABEL_DECL:
1143 pp_cxx_tree_identifier (cxx_pp, DECL_NAME (t));
1144 break;
1146 case CONST_DECL:
1147 if ((TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == ENUMERAL_TYPE)
1148 || (DECL_INITIAL (t) &&
1149 TREE_CODE (DECL_INITIAL (t)) == TEMPLATE_PARM_INDEX))
1150 dump_simple_decl (t, TREE_TYPE (t), flags);
1151 else if (DECL_NAME (t))
1152 dump_decl (DECL_NAME (t), flags);
1153 else if (DECL_INITIAL (t))
1154 dump_expr (DECL_INITIAL (t), flags | TFF_EXPR_IN_PARENS);
1155 else
1156 pp_string (cxx_pp, M_("<enumerator>"));
1157 break;
1159 case USING_DECL:
1160 pp_cxx_ws_string (cxx_pp, "using");
1161 dump_type (USING_DECL_SCOPE (t), flags);
1162 pp_cxx_colon_colon (cxx_pp);
1163 dump_decl (DECL_NAME (t), flags);
1164 break;
1166 case STATIC_ASSERT:
1167 pp_cxx_declaration (cxx_pp, t);
1168 break;
1170 case BASELINK:
1171 dump_decl (BASELINK_FUNCTIONS (t), flags);
1172 break;
1174 case NON_DEPENDENT_EXPR:
1175 dump_expr (t, flags);
1176 break;
1178 case TEMPLATE_TYPE_PARM:
1179 if (flags & TFF_DECL_SPECIFIERS)
1180 pp_cxx_declaration (cxx_pp, t);
1181 else
1182 pp_type_id (cxx_pp, t);
1183 break;
1185 case UNBOUND_CLASS_TEMPLATE:
1186 case TYPE_PACK_EXPANSION:
1187 case TREE_BINFO:
1188 dump_type (t, flags);
1189 break;
1191 default:
1192 pp_unsupported_tree (cxx_pp, t);
1193 /* Fall through to error. */
1195 case ERROR_MARK:
1196 pp_string (cxx_pp, M_("<declaration error>"));
1197 break;
1201 /* Dump a template declaration T under control of FLAGS. This means the
1202 'template <...> leaders plus the 'class X' or 'void fn(...)' part. */
1204 static void
1205 dump_template_decl (tree t, int flags)
1207 tree orig_parms = DECL_TEMPLATE_PARMS (t);
1208 tree parms;
1209 int i;
1211 if (flags & TFF_TEMPLATE_HEADER)
1213 for (parms = orig_parms = nreverse (orig_parms);
1214 parms;
1215 parms = TREE_CHAIN (parms))
1217 tree inner_parms = INNERMOST_TEMPLATE_PARMS (parms);
1218 int len = TREE_VEC_LENGTH (inner_parms);
1220 pp_cxx_ws_string (cxx_pp, "template");
1221 pp_cxx_begin_template_argument_list (cxx_pp);
1223 /* If we've shown the template prefix, we'd better show the
1224 parameters' and decl's type too. */
1225 flags |= TFF_DECL_SPECIFIERS;
1227 for (i = 0; i < len; i++)
1229 if (i)
1230 pp_separate_with_comma (cxx_pp);
1231 dump_template_parameter (TREE_VEC_ELT (inner_parms, i), flags);
1233 pp_cxx_end_template_argument_list (cxx_pp);
1234 pp_cxx_whitespace (cxx_pp);
1236 nreverse(orig_parms);
1238 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
1240 /* Say `template<arg> class TT' not just `template<arg> TT'. */
1241 pp_cxx_ws_string (cxx_pp, "class");
1243 /* If this is a parameter pack, print the ellipsis. */
1244 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
1245 pp_cxx_ws_string (cxx_pp, "...");
1249 if (DECL_CLASS_TEMPLATE_P (t))
1250 dump_type (TREE_TYPE (t),
1251 ((flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1252 | (flags & TFF_DECL_SPECIFIERS ? TFF_CLASS_KEY_OR_ENUM : 0)));
1253 else if (DECL_TEMPLATE_RESULT (t)
1254 && (TREE_CODE (DECL_TEMPLATE_RESULT (t)) == VAR_DECL
1255 /* Alias template. */
1256 || DECL_TYPE_TEMPLATE_P (t)))
1257 dump_decl (DECL_TEMPLATE_RESULT (t), flags | TFF_TEMPLATE_NAME);
1258 else
1260 gcc_assert (TREE_TYPE (t));
1261 switch (NEXT_CODE (t))
1263 case METHOD_TYPE:
1264 case FUNCTION_TYPE:
1265 dump_function_decl (t, flags | TFF_TEMPLATE_NAME);
1266 break;
1267 default:
1268 /* This case can occur with some invalid code. */
1269 dump_type (TREE_TYPE (t),
1270 (flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1271 | (flags & TFF_DECL_SPECIFIERS
1272 ? TFF_CLASS_KEY_OR_ENUM : 0));
1277 /* find_typenames looks through the type of the function template T
1278 and returns a VEC containing any typedefs, decltypes or TYPENAME_TYPEs
1279 it finds. */
1281 struct find_typenames_t
1283 struct pointer_set_t *p_set;
1284 VEC (tree,gc) *typenames;
1287 static tree
1288 find_typenames_r (tree *tp, int * /*walk_subtrees*/, void *data)
1290 struct find_typenames_t *d = (struct find_typenames_t *)data;
1291 tree mv = NULL_TREE;
1293 if (TYPE_P (*tp) && is_typedef_decl (TYPE_NAME (*tp)))
1294 /* Add the type of the typedef without any additional cv-quals. */
1295 mv = TREE_TYPE (TYPE_NAME (*tp));
1296 else if (TREE_CODE (*tp) == TYPENAME_TYPE
1297 || TREE_CODE (*tp) == DECLTYPE_TYPE)
1298 /* Add the typename without any cv-qualifiers. */
1299 mv = TYPE_MAIN_VARIANT (*tp);
1301 if (mv && (mv == *tp || !pointer_set_insert (d->p_set, mv)))
1302 VEC_safe_push (tree, gc, d->typenames, mv);
1304 /* Search into class template arguments, which cp_walk_subtrees
1305 doesn't do. */
1306 if (CLASS_TYPE_P (*tp) && CLASSTYPE_TEMPLATE_INFO (*tp))
1307 cp_walk_tree (&CLASSTYPE_TI_ARGS (*tp), find_typenames_r,
1308 data, d->p_set);
1310 return NULL_TREE;
1313 static VEC(tree,gc) *
1314 find_typenames (tree t)
1316 struct find_typenames_t ft;
1317 ft.p_set = pointer_set_create ();
1318 ft.typenames = NULL;
1319 cp_walk_tree (&TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
1320 find_typenames_r, &ft, ft.p_set);
1321 pointer_set_destroy (ft.p_set);
1322 return ft.typenames;
1325 /* Pretty print a function decl. There are several ways we want to print a
1326 function declaration. The TFF_ bits in FLAGS tells us how to behave.
1327 As error can only apply the '#' flag once to give 0 and 1 for V, there
1328 is %D which doesn't print the throw specs, and %F which does. */
1330 static void
1331 dump_function_decl (tree t, int flags)
1333 tree fntype;
1334 tree parmtypes;
1335 tree cname = NULL_TREE;
1336 tree template_args = NULL_TREE;
1337 tree template_parms = NULL_TREE;
1338 int show_return = flags & TFF_RETURN_TYPE || flags & TFF_DECL_SPECIFIERS;
1339 int do_outer_scope = ! (flags & TFF_UNQUALIFIED_NAME);
1340 tree exceptions;
1341 VEC(tree,gc) *typenames = NULL;
1343 if (DECL_NAME (t) && LAMBDA_FUNCTION_P (t))
1345 /* A lambda's signature is essentially its "type", so defer. */
1346 gcc_assert (LAMBDA_TYPE_P (DECL_CONTEXT (t)));
1347 dump_type (DECL_CONTEXT (t), flags);
1348 return;
1351 flags &= ~TFF_UNQUALIFIED_NAME;
1352 if (TREE_CODE (t) == TEMPLATE_DECL)
1353 t = DECL_TEMPLATE_RESULT (t);
1355 /* Save the exceptions, in case t is a specialization and we are
1356 emitting an error about incompatible specifications. */
1357 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (t));
1359 /* Pretty print template instantiations only. */
1360 if (DECL_USE_TEMPLATE (t) && DECL_TEMPLATE_INFO (t)
1361 && flag_pretty_templates)
1363 tree tmpl;
1365 template_args = DECL_TI_ARGS (t);
1366 tmpl = most_general_template (t);
1367 if (tmpl && TREE_CODE (tmpl) == TEMPLATE_DECL)
1369 template_parms = DECL_TEMPLATE_PARMS (tmpl);
1370 t = tmpl;
1371 typenames = find_typenames (t);
1375 fntype = TREE_TYPE (t);
1376 parmtypes = FUNCTION_FIRST_USER_PARMTYPE (t);
1378 if (DECL_CLASS_SCOPE_P (t))
1379 cname = DECL_CONTEXT (t);
1380 /* This is for partially instantiated template methods. */
1381 else if (TREE_CODE (fntype) == METHOD_TYPE)
1382 cname = TREE_TYPE (TREE_VALUE (parmtypes));
1384 if (flags & TFF_DECL_SPECIFIERS)
1386 if (DECL_STATIC_FUNCTION_P (t))
1387 pp_cxx_ws_string (cxx_pp, "static");
1388 else if (DECL_VIRTUAL_P (t))
1389 pp_cxx_ws_string (cxx_pp, "virtual");
1391 if (DECL_DECLARED_CONSTEXPR_P (STRIP_TEMPLATE (t)))
1392 pp_cxx_ws_string (cxx_pp, "constexpr");
1395 /* Print the return type? */
1396 if (show_return)
1397 show_return = !DECL_CONV_FN_P (t) && !DECL_CONSTRUCTOR_P (t)
1398 && !DECL_DESTRUCTOR_P (t);
1399 if (show_return)
1400 dump_type_prefix (TREE_TYPE (fntype), flags);
1402 /* Print the function name. */
1403 if (!do_outer_scope)
1404 /* Nothing. */;
1405 else if (cname)
1407 dump_type (cname, flags);
1408 pp_cxx_colon_colon (cxx_pp);
1410 else
1411 dump_scope (CP_DECL_CONTEXT (t), flags);
1413 dump_function_name (t, flags);
1415 if (!(flags & TFF_NO_FUNCTION_ARGUMENTS))
1417 dump_parameters (parmtypes, flags);
1419 if (TREE_CODE (fntype) == METHOD_TYPE)
1421 pp_base (cxx_pp)->padding = pp_before;
1422 pp_cxx_cv_qualifier_seq (cxx_pp, class_of_this_parm (fntype));
1425 if (flags & TFF_EXCEPTION_SPECIFICATION)
1427 pp_base (cxx_pp)->padding = pp_before;
1428 dump_exception_spec (exceptions, flags);
1431 if (show_return)
1432 dump_type_suffix (TREE_TYPE (fntype), flags);
1434 /* If T is a template instantiation, dump the parameter binding. */
1435 if (template_parms != NULL_TREE && template_args != NULL_TREE)
1437 pp_cxx_whitespace (cxx_pp);
1438 pp_cxx_left_bracket (cxx_pp);
1439 pp_cxx_ws_string (cxx_pp, M_("with"));
1440 pp_cxx_whitespace (cxx_pp);
1441 dump_template_bindings (template_parms, template_args, typenames);
1442 pp_cxx_right_bracket (cxx_pp);
1445 else if (template_args)
1447 bool need_comma = false;
1448 int i;
1449 pp_cxx_begin_template_argument_list (cxx_pp);
1450 template_args = INNERMOST_TEMPLATE_ARGS (template_args);
1451 for (i = 0; i < TREE_VEC_LENGTH (template_args); ++i)
1453 tree arg = TREE_VEC_ELT (template_args, i);
1454 if (need_comma)
1455 pp_separate_with_comma (cxx_pp);
1456 if (ARGUMENT_PACK_P (arg))
1457 pp_cxx_left_brace (cxx_pp);
1458 dump_template_argument (arg, TFF_PLAIN_IDENTIFIER);
1459 if (ARGUMENT_PACK_P (arg))
1460 pp_cxx_right_brace (cxx_pp);
1461 need_comma = true;
1463 pp_cxx_end_template_argument_list (cxx_pp);
1467 /* Print a parameter list. If this is for a member function, the
1468 member object ptr (and any other hidden args) should have
1469 already been removed. */
1471 static void
1472 dump_parameters (tree parmtypes, int flags)
1474 int first = 1;
1475 flags &= ~TFF_SCOPE;
1476 pp_cxx_left_paren (cxx_pp);
1478 for (first = 1; parmtypes != void_list_node;
1479 parmtypes = TREE_CHAIN (parmtypes))
1481 if (!first)
1482 pp_separate_with_comma (cxx_pp);
1483 first = 0;
1484 if (!parmtypes)
1486 pp_cxx_ws_string (cxx_pp, "...");
1487 break;
1490 dump_type (TREE_VALUE (parmtypes), flags);
1492 if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && TREE_PURPOSE (parmtypes))
1494 pp_cxx_whitespace (cxx_pp);
1495 pp_equal (cxx_pp);
1496 pp_cxx_whitespace (cxx_pp);
1497 dump_expr (TREE_PURPOSE (parmtypes), flags | TFF_EXPR_IN_PARENS);
1501 pp_cxx_right_paren (cxx_pp);
1504 /* Print an exception specification. T is the exception specification. */
1506 static void
1507 dump_exception_spec (tree t, int flags)
1509 if (t && TREE_PURPOSE (t))
1511 pp_cxx_ws_string (cxx_pp, "noexcept");
1512 pp_cxx_whitespace (cxx_pp);
1513 pp_cxx_left_paren (cxx_pp);
1514 if (DEFERRED_NOEXCEPT_SPEC_P (t))
1515 pp_cxx_ws_string (cxx_pp, "<uninstantiated>");
1516 else
1517 dump_expr (TREE_PURPOSE (t), flags);
1518 pp_cxx_right_paren (cxx_pp);
1520 else if (t)
1522 pp_cxx_ws_string (cxx_pp, "throw");
1523 pp_cxx_whitespace (cxx_pp);
1524 pp_cxx_left_paren (cxx_pp);
1525 if (TREE_VALUE (t) != NULL_TREE)
1526 while (1)
1528 dump_type (TREE_VALUE (t), flags);
1529 t = TREE_CHAIN (t);
1530 if (!t)
1531 break;
1532 pp_separate_with_comma (cxx_pp);
1534 pp_cxx_right_paren (cxx_pp);
1538 /* Handle the function name for a FUNCTION_DECL node, grokking operators
1539 and destructors properly. */
1541 static void
1542 dump_function_name (tree t, int flags)
1544 tree name = DECL_NAME (t);
1546 /* We can get here with a decl that was synthesized by language-
1547 independent machinery (e.g. coverage.c) in which case it won't
1548 have a lang_specific structure attached and DECL_CONSTRUCTOR_P
1549 will crash. In this case it is safe just to print out the
1550 literal name. */
1551 if (!DECL_LANG_SPECIFIC (t))
1553 pp_cxx_tree_identifier (cxx_pp, name);
1554 return;
1557 if (TREE_CODE (t) == TEMPLATE_DECL)
1558 t = DECL_TEMPLATE_RESULT (t);
1560 /* Don't let the user see __comp_ctor et al. */
1561 if (DECL_CONSTRUCTOR_P (t)
1562 || DECL_DESTRUCTOR_P (t))
1564 if (LAMBDA_TYPE_P (DECL_CONTEXT (t)))
1565 name = get_identifier ("<lambda>");
1566 else if (TYPE_ANONYMOUS_P (DECL_CONTEXT (t)))
1567 name = get_identifier ("<constructor>");
1568 else
1569 name = constructor_name (DECL_CONTEXT (t));
1572 if (DECL_DESTRUCTOR_P (t))
1574 pp_cxx_complement (cxx_pp);
1575 dump_decl (name, TFF_PLAIN_IDENTIFIER);
1577 else if (DECL_CONV_FN_P (t))
1579 /* This cannot use the hack that the operator's return
1580 type is stashed off of its name because it may be
1581 used for error reporting. In the case of conflicting
1582 declarations, both will have the same name, yet
1583 the types will be different, hence the TREE_TYPE field
1584 of the first name will be clobbered by the second. */
1585 pp_cxx_ws_string (cxx_pp, "operator");
1586 dump_type (TREE_TYPE (TREE_TYPE (t)), flags);
1588 else if (name && IDENTIFIER_OPNAME_P (name))
1589 pp_cxx_tree_identifier (cxx_pp, name);
1590 else if (name && UDLIT_OPER_P (name))
1591 pp_cxx_tree_identifier (cxx_pp, name);
1592 else
1593 dump_decl (name, flags);
1595 if (DECL_TEMPLATE_INFO (t)
1596 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (t)
1597 && (TREE_CODE (DECL_TI_TEMPLATE (t)) != TEMPLATE_DECL
1598 || PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
1599 dump_template_parms (DECL_TEMPLATE_INFO (t), !DECL_USE_TEMPLATE (t), flags);
1602 /* Dump the template parameters from the template info INFO under control of
1603 FLAGS. PRIMARY indicates whether this is a primary template decl, or
1604 specialization (partial or complete). For partial specializations we show
1605 the specialized parameter values. For a primary template we show no
1606 decoration. */
1608 static void
1609 dump_template_parms (tree info, int primary, int flags)
1611 tree args = info ? TI_ARGS (info) : NULL_TREE;
1613 if (primary && flags & TFF_TEMPLATE_NAME)
1614 return;
1615 flags &= ~(TFF_CLASS_KEY_OR_ENUM | TFF_TEMPLATE_NAME);
1616 pp_cxx_begin_template_argument_list (cxx_pp);
1618 /* Be careful only to print things when we have them, so as not
1619 to crash producing error messages. */
1620 if (args && !primary)
1622 int len, ix;
1623 len = get_non_default_template_args_count (args, flags);
1625 args = INNERMOST_TEMPLATE_ARGS (args);
1626 for (ix = 0; ix != len; ix++)
1628 tree arg = TREE_VEC_ELT (args, ix);
1630 /* Only print a comma if we know there is an argument coming. In
1631 the case of an empty template argument pack, no actual
1632 argument will be printed. */
1633 if (ix
1634 && (!ARGUMENT_PACK_P (arg)
1635 || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) > 0))
1636 pp_separate_with_comma (cxx_pp);
1638 if (!arg)
1639 pp_string (cxx_pp, M_("<template parameter error>"));
1640 else
1641 dump_template_argument (arg, flags);
1644 else if (primary)
1646 tree tpl = TI_TEMPLATE (info);
1647 tree parms = DECL_TEMPLATE_PARMS (tpl);
1648 int len, ix;
1650 parms = TREE_CODE (parms) == TREE_LIST ? TREE_VALUE (parms) : NULL_TREE;
1651 len = parms ? TREE_VEC_LENGTH (parms) : 0;
1653 for (ix = 0; ix != len; ix++)
1655 tree parm;
1657 if (TREE_VEC_ELT (parms, ix) == error_mark_node)
1659 pp_string (cxx_pp, M_("<template parameter error>"));
1660 continue;
1663 parm = TREE_VALUE (TREE_VEC_ELT (parms, ix));
1665 if (ix)
1666 pp_separate_with_comma (cxx_pp);
1668 dump_decl (parm, flags & ~TFF_DECL_SPECIFIERS);
1671 pp_cxx_end_template_argument_list (cxx_pp);
1674 /* Print out the arguments of CALL_EXPR T as a parenthesized list using
1675 flags FLAGS. Skip over the first argument if SKIPFIRST is true. */
1677 static void
1678 dump_call_expr_args (tree t, int flags, bool skipfirst)
1680 tree arg;
1681 call_expr_arg_iterator iter;
1683 pp_cxx_left_paren (cxx_pp);
1684 FOR_EACH_CALL_EXPR_ARG (arg, iter, t)
1686 if (skipfirst)
1687 skipfirst = false;
1688 else
1690 dump_expr (arg, flags | TFF_EXPR_IN_PARENS);
1691 if (more_call_expr_args_p (&iter))
1692 pp_separate_with_comma (cxx_pp);
1695 pp_cxx_right_paren (cxx_pp);
1698 /* Print out the arguments of AGGR_INIT_EXPR T as a parenthesized list
1699 using flags FLAGS. Skip over the first argument if SKIPFIRST is
1700 true. */
1702 static void
1703 dump_aggr_init_expr_args (tree t, int flags, bool skipfirst)
1705 tree arg;
1706 aggr_init_expr_arg_iterator iter;
1708 pp_cxx_left_paren (cxx_pp);
1709 FOR_EACH_AGGR_INIT_EXPR_ARG (arg, iter, t)
1711 if (skipfirst)
1712 skipfirst = false;
1713 else
1715 dump_expr (arg, flags | TFF_EXPR_IN_PARENS);
1716 if (more_aggr_init_expr_args_p (&iter))
1717 pp_separate_with_comma (cxx_pp);
1720 pp_cxx_right_paren (cxx_pp);
1723 /* Print out a list of initializers (subr of dump_expr). */
1725 static void
1726 dump_expr_list (tree l, int flags)
1728 while (l)
1730 dump_expr (TREE_VALUE (l), flags | TFF_EXPR_IN_PARENS);
1731 l = TREE_CHAIN (l);
1732 if (l)
1733 pp_separate_with_comma (cxx_pp);
1737 /* Print out a vector of initializers (subr of dump_expr). */
1739 static void
1740 dump_expr_init_vec (VEC(constructor_elt,gc) *v, int flags)
1742 unsigned HOST_WIDE_INT idx;
1743 tree value;
1745 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
1747 dump_expr (value, flags | TFF_EXPR_IN_PARENS);
1748 if (idx != VEC_length (constructor_elt, v) - 1)
1749 pp_separate_with_comma (cxx_pp);
1754 /* We've gotten an indirect REFERENCE (an OBJ_TYPE_REF) to a virtual
1755 function. Resolve it to a close relative -- in the sense of static
1756 type -- variant being overridden. That is close to what was written in
1757 the source code. Subroutine of dump_expr. */
1759 static tree
1760 resolve_virtual_fun_from_obj_type_ref (tree ref)
1762 tree obj_type = TREE_TYPE (OBJ_TYPE_REF_OBJECT (ref));
1763 HOST_WIDE_INT index = tree_low_cst (OBJ_TYPE_REF_TOKEN (ref), 1);
1764 tree fun = BINFO_VIRTUALS (TYPE_BINFO (TREE_TYPE (obj_type)));
1765 while (index)
1767 fun = TREE_CHAIN (fun);
1768 index -= (TARGET_VTABLE_USES_DESCRIPTORS
1769 ? TARGET_VTABLE_USES_DESCRIPTORS : 1);
1772 return BV_FN (fun);
1775 /* Print out an expression E under control of FLAGS. */
1777 static void
1778 dump_expr (tree t, int flags)
1780 if (t == 0)
1781 return;
1783 if (STATEMENT_CLASS_P (t))
1785 pp_cxx_ws_string (cxx_pp, M_("<statement>"));
1786 return;
1789 switch (TREE_CODE (t))
1791 case VAR_DECL:
1792 case PARM_DECL:
1793 case FIELD_DECL:
1794 case CONST_DECL:
1795 case FUNCTION_DECL:
1796 case TEMPLATE_DECL:
1797 case NAMESPACE_DECL:
1798 case LABEL_DECL:
1799 case OVERLOAD:
1800 case TYPE_DECL:
1801 case IDENTIFIER_NODE:
1802 dump_decl (t, ((flags & ~(TFF_DECL_SPECIFIERS|TFF_RETURN_TYPE
1803 |TFF_TEMPLATE_HEADER))
1804 | TFF_NO_FUNCTION_ARGUMENTS));
1805 break;
1807 case SSA_NAME:
1808 if (SSA_NAME_VAR (t)
1809 && !DECL_ARTIFICIAL (SSA_NAME_VAR (t)))
1810 dump_expr (SSA_NAME_VAR (t), flags);
1811 else
1812 pp_cxx_ws_string (cxx_pp, M_("<unknown>"));
1813 break;
1815 case INTEGER_CST:
1816 case REAL_CST:
1817 case STRING_CST:
1818 case COMPLEX_CST:
1819 pp_constant (cxx_pp, t);
1820 break;
1822 case USERDEF_LITERAL:
1823 pp_cxx_userdef_literal (cxx_pp, t);
1824 break;
1826 case THROW_EXPR:
1827 /* While waiting for caret diagnostics, avoid printing
1828 __cxa_allocate_exception, __cxa_throw, and the like. */
1829 pp_cxx_ws_string (cxx_pp, M_("<throw-expression>"));
1830 break;
1832 case PTRMEM_CST:
1833 pp_ampersand (cxx_pp);
1834 dump_type (PTRMEM_CST_CLASS (t), flags);
1835 pp_cxx_colon_colon (cxx_pp);
1836 pp_cxx_tree_identifier (cxx_pp, DECL_NAME (PTRMEM_CST_MEMBER (t)));
1837 break;
1839 case COMPOUND_EXPR:
1840 pp_cxx_left_paren (cxx_pp);
1841 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1842 pp_separate_with_comma (cxx_pp);
1843 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
1844 pp_cxx_right_paren (cxx_pp);
1845 break;
1847 case COND_EXPR:
1848 pp_cxx_left_paren (cxx_pp);
1849 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1850 pp_string (cxx_pp, " ? ");
1851 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
1852 pp_string (cxx_pp, " : ");
1853 dump_expr (TREE_OPERAND (t, 2), flags | TFF_EXPR_IN_PARENS);
1854 pp_cxx_right_paren (cxx_pp);
1855 break;
1857 case SAVE_EXPR:
1858 if (TREE_HAS_CONSTRUCTOR (t))
1860 pp_cxx_ws_string (cxx_pp, "new");
1861 pp_cxx_whitespace (cxx_pp);
1862 dump_type (TREE_TYPE (TREE_TYPE (t)), flags);
1864 else
1865 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1866 break;
1868 case AGGR_INIT_EXPR:
1870 tree fn = NULL_TREE;
1872 if (TREE_CODE (AGGR_INIT_EXPR_FN (t)) == ADDR_EXPR)
1873 fn = TREE_OPERAND (AGGR_INIT_EXPR_FN (t), 0);
1875 if (fn && TREE_CODE (fn) == FUNCTION_DECL)
1877 if (DECL_CONSTRUCTOR_P (fn))
1878 dump_type (DECL_CONTEXT (fn), flags);
1879 else
1880 dump_decl (fn, 0);
1882 else
1883 dump_expr (AGGR_INIT_EXPR_FN (t), 0);
1885 dump_aggr_init_expr_args (t, flags, true);
1886 break;
1888 case CALL_EXPR:
1890 tree fn = CALL_EXPR_FN (t);
1891 bool skipfirst = false;
1893 if (TREE_CODE (fn) == ADDR_EXPR)
1894 fn = TREE_OPERAND (fn, 0);
1896 /* Nobody is interested in seeing the guts of vcalls. */
1897 if (TREE_CODE (fn) == OBJ_TYPE_REF)
1898 fn = resolve_virtual_fun_from_obj_type_ref (fn);
1900 if (TREE_TYPE (fn) != NULL_TREE
1901 && NEXT_CODE (fn) == METHOD_TYPE
1902 && call_expr_nargs (t))
1904 tree ob = CALL_EXPR_ARG (t, 0);
1905 if (TREE_CODE (ob) == ADDR_EXPR)
1907 dump_expr (TREE_OPERAND (ob, 0), flags | TFF_EXPR_IN_PARENS);
1908 pp_cxx_dot (cxx_pp);
1910 else if (TREE_CODE (ob) != PARM_DECL
1911 || strcmp (IDENTIFIER_POINTER (DECL_NAME (ob)), "this"))
1913 dump_expr (ob, flags | TFF_EXPR_IN_PARENS);
1914 pp_cxx_arrow (cxx_pp);
1916 skipfirst = true;
1918 dump_expr (fn, flags | TFF_EXPR_IN_PARENS);
1919 dump_call_expr_args (t, flags, skipfirst);
1921 break;
1923 case TARGET_EXPR:
1924 /* Note that this only works for G++ target exprs. If somebody
1925 builds a general TARGET_EXPR, there's no way to represent that
1926 it initializes anything other that the parameter slot for the
1927 default argument. Note we may have cleared out the first
1928 operand in expand_expr, so don't go killing ourselves. */
1929 if (TREE_OPERAND (t, 1))
1930 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
1931 break;
1933 case POINTER_PLUS_EXPR:
1934 dump_binary_op ("+", t, flags);
1935 break;
1937 case INIT_EXPR:
1938 case MODIFY_EXPR:
1939 dump_binary_op (assignment_operator_name_info[(int)NOP_EXPR].name,
1940 t, flags);
1941 break;
1943 case PLUS_EXPR:
1944 case MINUS_EXPR:
1945 case MULT_EXPR:
1946 case TRUNC_DIV_EXPR:
1947 case TRUNC_MOD_EXPR:
1948 case MIN_EXPR:
1949 case MAX_EXPR:
1950 case LSHIFT_EXPR:
1951 case RSHIFT_EXPR:
1952 case BIT_IOR_EXPR:
1953 case BIT_XOR_EXPR:
1954 case BIT_AND_EXPR:
1955 case TRUTH_ANDIF_EXPR:
1956 case TRUTH_ORIF_EXPR:
1957 case LT_EXPR:
1958 case LE_EXPR:
1959 case GT_EXPR:
1960 case GE_EXPR:
1961 case EQ_EXPR:
1962 case NE_EXPR:
1963 case EXACT_DIV_EXPR:
1964 dump_binary_op (operator_name_info[(int) TREE_CODE (t)].name, t, flags);
1965 break;
1967 case CEIL_DIV_EXPR:
1968 case FLOOR_DIV_EXPR:
1969 case ROUND_DIV_EXPR:
1970 case RDIV_EXPR:
1971 dump_binary_op ("/", t, flags);
1972 break;
1974 case CEIL_MOD_EXPR:
1975 case FLOOR_MOD_EXPR:
1976 case ROUND_MOD_EXPR:
1977 dump_binary_op ("%", t, flags);
1978 break;
1980 case COMPONENT_REF:
1982 tree ob = TREE_OPERAND (t, 0);
1983 if (TREE_CODE (ob) == INDIRECT_REF)
1985 ob = TREE_OPERAND (ob, 0);
1986 if (TREE_CODE (ob) != PARM_DECL
1987 || (DECL_NAME (ob)
1988 && strcmp (IDENTIFIER_POINTER (DECL_NAME (ob)), "this")))
1990 dump_expr (ob, flags | TFF_EXPR_IN_PARENS);
1991 if (TREE_CODE (TREE_TYPE (ob)) == REFERENCE_TYPE)
1992 pp_cxx_dot (cxx_pp);
1993 else
1994 pp_cxx_arrow (cxx_pp);
1997 else
1999 dump_expr (ob, flags | TFF_EXPR_IN_PARENS);
2000 pp_cxx_dot (cxx_pp);
2002 dump_expr (TREE_OPERAND (t, 1), flags & ~TFF_EXPR_IN_PARENS);
2004 break;
2006 case ARRAY_REF:
2007 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2008 pp_cxx_left_bracket (cxx_pp);
2009 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2010 pp_cxx_right_bracket (cxx_pp);
2011 break;
2013 case UNARY_PLUS_EXPR:
2014 dump_unary_op ("+", t, flags);
2015 break;
2017 case ADDR_EXPR:
2018 if (TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
2019 || TREE_CODE (TREE_OPERAND (t, 0)) == STRING_CST
2020 /* An ADDR_EXPR can have reference type. In that case, we
2021 shouldn't print the `&' doing so indicates to the user
2022 that the expression has pointer type. */
2023 || (TREE_TYPE (t)
2024 && TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE))
2025 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2026 else if (TREE_CODE (TREE_OPERAND (t, 0)) == LABEL_DECL)
2027 dump_unary_op ("&&", t, flags);
2028 else
2029 dump_unary_op ("&", t, flags);
2030 break;
2032 case INDIRECT_REF:
2033 if (TREE_HAS_CONSTRUCTOR (t))
2035 t = TREE_OPERAND (t, 0);
2036 gcc_assert (TREE_CODE (t) == CALL_EXPR);
2037 dump_expr (CALL_EXPR_FN (t), flags | TFF_EXPR_IN_PARENS);
2038 dump_call_expr_args (t, flags, true);
2040 else
2042 if (TREE_OPERAND (t,0) != NULL_TREE
2043 && TREE_TYPE (TREE_OPERAND (t, 0))
2044 && NEXT_CODE (TREE_OPERAND (t, 0)) == REFERENCE_TYPE)
2045 dump_expr (TREE_OPERAND (t, 0), flags);
2046 else
2047 dump_unary_op ("*", t, flags);
2049 break;
2051 case MEM_REF:
2052 if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
2053 && integer_zerop (TREE_OPERAND (t, 1)))
2054 dump_expr (TREE_OPERAND (TREE_OPERAND (t, 0), 0), flags);
2055 else
2057 pp_cxx_star (cxx_pp);
2058 if (!integer_zerop (TREE_OPERAND (t, 1)))
2060 pp_cxx_left_paren (cxx_pp);
2061 if (!integer_onep (TYPE_SIZE_UNIT
2062 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0))))))
2064 pp_cxx_left_paren (cxx_pp);
2065 dump_type (ptr_type_node, flags);
2066 pp_cxx_right_paren (cxx_pp);
2069 dump_expr (TREE_OPERAND (t, 0), flags);
2070 if (!integer_zerop (TREE_OPERAND (t, 1)))
2072 pp_cxx_ws_string (cxx_pp, "+");
2073 dump_expr (fold_convert (ssizetype, TREE_OPERAND (t, 1)), flags);
2074 pp_cxx_right_paren (cxx_pp);
2077 break;
2079 case NEGATE_EXPR:
2080 case BIT_NOT_EXPR:
2081 case TRUTH_NOT_EXPR:
2082 case PREDECREMENT_EXPR:
2083 case PREINCREMENT_EXPR:
2084 dump_unary_op (operator_name_info [(int)TREE_CODE (t)].name, t, flags);
2085 break;
2087 case POSTDECREMENT_EXPR:
2088 case POSTINCREMENT_EXPR:
2089 pp_cxx_left_paren (cxx_pp);
2090 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2091 pp_cxx_ws_string (cxx_pp, operator_name_info[(int)TREE_CODE (t)].name);
2092 pp_cxx_right_paren (cxx_pp);
2093 break;
2095 case NON_LVALUE_EXPR:
2096 /* FIXME: This is a KLUDGE workaround for a parsing problem. There
2097 should be another level of INDIRECT_REF so that I don't have to do
2098 this. */
2099 if (TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == POINTER_TYPE)
2101 tree next = TREE_TYPE (TREE_TYPE (t));
2103 while (TREE_CODE (next) == POINTER_TYPE)
2104 next = TREE_TYPE (next);
2106 if (TREE_CODE (next) == FUNCTION_TYPE)
2108 if (flags & TFF_EXPR_IN_PARENS)
2109 pp_cxx_left_paren (cxx_pp);
2110 pp_cxx_star (cxx_pp);
2111 dump_expr (TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2112 if (flags & TFF_EXPR_IN_PARENS)
2113 pp_cxx_right_paren (cxx_pp);
2114 break;
2116 /* Else fall through. */
2118 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2119 break;
2121 CASE_CONVERT:
2122 case IMPLICIT_CONV_EXPR:
2123 case VIEW_CONVERT_EXPR:
2125 tree op = TREE_OPERAND (t, 0);
2126 tree ttype = TREE_TYPE (t);
2127 tree optype = TREE_TYPE (op);
2129 if (TREE_CODE (ttype) != TREE_CODE (optype)
2130 && POINTER_TYPE_P (ttype)
2131 && POINTER_TYPE_P (optype)
2132 && same_type_p (TREE_TYPE (optype),
2133 TREE_TYPE (ttype)))
2135 if (TREE_CODE (ttype) == REFERENCE_TYPE)
2136 dump_unary_op ("*", t, flags);
2137 else
2138 dump_unary_op ("&", t, flags);
2140 else if (!same_type_p (TREE_TYPE (op), TREE_TYPE (t)))
2142 /* It is a cast, but we cannot tell whether it is a
2143 reinterpret or static cast. Use the C style notation. */
2144 if (flags & TFF_EXPR_IN_PARENS)
2145 pp_cxx_left_paren (cxx_pp);
2146 pp_cxx_left_paren (cxx_pp);
2147 dump_type (TREE_TYPE (t), flags);
2148 pp_cxx_right_paren (cxx_pp);
2149 dump_expr (op, flags | TFF_EXPR_IN_PARENS);
2150 if (flags & TFF_EXPR_IN_PARENS)
2151 pp_cxx_right_paren (cxx_pp);
2153 else
2154 dump_expr (op, flags);
2155 break;
2158 case CONSTRUCTOR:
2159 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
2161 tree idx = build_ptrmemfunc_access_expr (t, pfn_identifier);
2163 if (integer_zerop (idx))
2165 /* A NULL pointer-to-member constant. */
2166 pp_cxx_left_paren (cxx_pp);
2167 pp_cxx_left_paren (cxx_pp);
2168 dump_type (TREE_TYPE (t), flags);
2169 pp_cxx_right_paren (cxx_pp);
2170 pp_character (cxx_pp, '0');
2171 pp_cxx_right_paren (cxx_pp);
2172 break;
2174 else if (host_integerp (idx, 0))
2176 tree virtuals;
2177 unsigned HOST_WIDE_INT n;
2179 t = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
2180 t = TYPE_METHOD_BASETYPE (t);
2181 virtuals = BINFO_VIRTUALS (TYPE_BINFO (TYPE_MAIN_VARIANT (t)));
2183 n = tree_low_cst (idx, 0);
2185 /* Map vtable index back one, to allow for the null pointer to
2186 member. */
2187 --n;
2189 while (n > 0 && virtuals)
2191 --n;
2192 virtuals = TREE_CHAIN (virtuals);
2194 if (virtuals)
2196 dump_expr (BV_FN (virtuals),
2197 flags | TFF_EXPR_IN_PARENS);
2198 break;
2202 if (TREE_TYPE (t) && LAMBDA_TYPE_P (TREE_TYPE (t)))
2203 pp_string (cxx_pp, "<lambda closure object>");
2204 if (TREE_TYPE (t) && EMPTY_CONSTRUCTOR_P (t))
2206 dump_type (TREE_TYPE (t), 0);
2207 pp_cxx_left_paren (cxx_pp);
2208 pp_cxx_right_paren (cxx_pp);
2210 else
2212 if (!BRACE_ENCLOSED_INITIALIZER_P (t))
2213 dump_type (TREE_TYPE (t), 0);
2214 pp_cxx_left_brace (cxx_pp);
2215 dump_expr_init_vec (CONSTRUCTOR_ELTS (t), flags);
2216 pp_cxx_right_brace (cxx_pp);
2219 break;
2221 case OFFSET_REF:
2223 tree ob = TREE_OPERAND (t, 0);
2224 if (is_dummy_object (ob))
2226 t = TREE_OPERAND (t, 1);
2227 if (TREE_CODE (t) == FUNCTION_DECL)
2228 /* A::f */
2229 dump_expr (t, flags | TFF_EXPR_IN_PARENS);
2230 else if (BASELINK_P (t))
2231 dump_expr (OVL_CURRENT (BASELINK_FUNCTIONS (t)),
2232 flags | TFF_EXPR_IN_PARENS);
2233 else
2234 dump_decl (t, flags);
2236 else
2238 if (TREE_CODE (ob) == INDIRECT_REF)
2240 dump_expr (TREE_OPERAND (ob, 0), flags | TFF_EXPR_IN_PARENS);
2241 pp_cxx_arrow (cxx_pp);
2242 pp_cxx_star (cxx_pp);
2244 else
2246 dump_expr (ob, flags | TFF_EXPR_IN_PARENS);
2247 pp_cxx_dot (cxx_pp);
2248 pp_cxx_star (cxx_pp);
2250 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2252 break;
2255 case TEMPLATE_PARM_INDEX:
2256 dump_decl (TEMPLATE_PARM_DECL (t), flags & ~TFF_DECL_SPECIFIERS);
2257 break;
2259 case CAST_EXPR:
2260 if (TREE_OPERAND (t, 0) == NULL_TREE
2261 || TREE_CHAIN (TREE_OPERAND (t, 0)))
2263 dump_type (TREE_TYPE (t), flags);
2264 pp_cxx_left_paren (cxx_pp);
2265 dump_expr_list (TREE_OPERAND (t, 0), flags);
2266 pp_cxx_right_paren (cxx_pp);
2268 else
2270 pp_cxx_left_paren (cxx_pp);
2271 dump_type (TREE_TYPE (t), flags);
2272 pp_cxx_right_paren (cxx_pp);
2273 pp_cxx_left_paren (cxx_pp);
2274 dump_expr_list (TREE_OPERAND (t, 0), flags);
2275 pp_cxx_right_paren (cxx_pp);
2277 break;
2279 case STATIC_CAST_EXPR:
2280 pp_cxx_ws_string (cxx_pp, "static_cast");
2281 goto cast;
2282 case REINTERPRET_CAST_EXPR:
2283 pp_cxx_ws_string (cxx_pp, "reinterpret_cast");
2284 goto cast;
2285 case CONST_CAST_EXPR:
2286 pp_cxx_ws_string (cxx_pp, "const_cast");
2287 goto cast;
2288 case DYNAMIC_CAST_EXPR:
2289 pp_cxx_ws_string (cxx_pp, "dynamic_cast");
2290 cast:
2291 pp_cxx_begin_template_argument_list (cxx_pp);
2292 dump_type (TREE_TYPE (t), flags);
2293 pp_cxx_end_template_argument_list (cxx_pp);
2294 pp_cxx_left_paren (cxx_pp);
2295 dump_expr (TREE_OPERAND (t, 0), flags);
2296 pp_cxx_right_paren (cxx_pp);
2297 break;
2299 case ARROW_EXPR:
2300 dump_expr (TREE_OPERAND (t, 0), flags);
2301 pp_cxx_arrow (cxx_pp);
2302 break;
2304 case SIZEOF_EXPR:
2305 case ALIGNOF_EXPR:
2306 if (TREE_CODE (t) == SIZEOF_EXPR)
2307 pp_cxx_ws_string (cxx_pp, "sizeof");
2308 else
2310 gcc_assert (TREE_CODE (t) == ALIGNOF_EXPR);
2311 pp_cxx_ws_string (cxx_pp, "__alignof__");
2313 pp_cxx_whitespace (cxx_pp);
2314 pp_cxx_left_paren (cxx_pp);
2315 if (TYPE_P (TREE_OPERAND (t, 0)))
2316 dump_type (TREE_OPERAND (t, 0), flags);
2317 else
2318 dump_expr (TREE_OPERAND (t, 0), flags);
2319 pp_cxx_right_paren (cxx_pp);
2320 break;
2322 case AT_ENCODE_EXPR:
2323 pp_cxx_ws_string (cxx_pp, "@encode");
2324 pp_cxx_whitespace (cxx_pp);
2325 pp_cxx_left_paren (cxx_pp);
2326 dump_type (TREE_OPERAND (t, 0), flags);
2327 pp_cxx_right_paren (cxx_pp);
2328 break;
2330 case NOEXCEPT_EXPR:
2331 pp_cxx_ws_string (cxx_pp, "noexcept");
2332 pp_cxx_whitespace (cxx_pp);
2333 pp_cxx_left_paren (cxx_pp);
2334 dump_expr (TREE_OPERAND (t, 0), flags);
2335 pp_cxx_right_paren (cxx_pp);
2336 break;
2338 case REALPART_EXPR:
2339 case IMAGPART_EXPR:
2340 pp_cxx_ws_string (cxx_pp, operator_name_info[TREE_CODE (t)].name);
2341 pp_cxx_whitespace (cxx_pp);
2342 dump_expr (TREE_OPERAND (t, 0), flags);
2343 break;
2345 case DEFAULT_ARG:
2346 pp_string (cxx_pp, M_("<unparsed>"));
2347 break;
2349 case TRY_CATCH_EXPR:
2350 case WITH_CLEANUP_EXPR:
2351 case CLEANUP_POINT_EXPR:
2352 dump_expr (TREE_OPERAND (t, 0), flags);
2353 break;
2355 case PSEUDO_DTOR_EXPR:
2356 dump_expr (TREE_OPERAND (t, 2), flags);
2357 pp_cxx_dot (cxx_pp);
2358 dump_type (TREE_OPERAND (t, 0), flags);
2359 pp_cxx_colon_colon (cxx_pp);
2360 pp_cxx_complement (cxx_pp);
2361 dump_type (TREE_OPERAND (t, 1), flags);
2362 break;
2364 case TEMPLATE_ID_EXPR:
2365 dump_decl (t, flags);
2366 break;
2368 case BIND_EXPR:
2369 case STMT_EXPR:
2370 case EXPR_STMT:
2371 case STATEMENT_LIST:
2372 /* We don't yet have a way of dumping statements in a
2373 human-readable format. */
2374 pp_string (cxx_pp, "({...})");
2375 break;
2377 case LOOP_EXPR:
2378 pp_string (cxx_pp, "while (1) { ");
2379 dump_expr (TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2380 pp_cxx_right_brace (cxx_pp);
2381 break;
2383 case EXIT_EXPR:
2384 pp_string (cxx_pp, "if (");
2385 dump_expr (TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2386 pp_string (cxx_pp, ") break; ");
2387 break;
2389 case BASELINK:
2390 dump_expr (BASELINK_FUNCTIONS (t), flags & ~TFF_EXPR_IN_PARENS);
2391 break;
2393 case EMPTY_CLASS_EXPR:
2394 dump_type (TREE_TYPE (t), flags);
2395 pp_cxx_left_paren (cxx_pp);
2396 pp_cxx_right_paren (cxx_pp);
2397 break;
2399 case NON_DEPENDENT_EXPR:
2400 dump_expr (TREE_OPERAND (t, 0), flags);
2401 break;
2403 case ARGUMENT_PACK_SELECT:
2404 dump_template_argument (ARGUMENT_PACK_SELECT_FROM_PACK (t), flags);
2405 break;
2407 case RECORD_TYPE:
2408 case UNION_TYPE:
2409 case ENUMERAL_TYPE:
2410 case REAL_TYPE:
2411 case VOID_TYPE:
2412 case BOOLEAN_TYPE:
2413 case INTEGER_TYPE:
2414 case COMPLEX_TYPE:
2415 case VECTOR_TYPE:
2416 pp_type_specifier_seq (cxx_pp, t);
2417 break;
2419 case TYPENAME_TYPE:
2420 /* We get here when we want to print a dependent type as an
2421 id-expression, without any disambiguator decoration. */
2422 pp_id_expression (cxx_pp, t);
2423 break;
2425 case TEMPLATE_TYPE_PARM:
2426 case TEMPLATE_TEMPLATE_PARM:
2427 case BOUND_TEMPLATE_TEMPLATE_PARM:
2428 dump_type (t, flags);
2429 break;
2431 case TRAIT_EXPR:
2432 pp_cxx_trait_expression (cxx_pp, t);
2433 break;
2435 case VA_ARG_EXPR:
2436 pp_cxx_va_arg_expression (cxx_pp, t);
2437 break;
2439 case OFFSETOF_EXPR:
2440 pp_cxx_offsetof_expression (cxx_pp, t);
2441 break;
2443 case SCOPE_REF:
2444 dump_decl (t, flags);
2445 break;
2447 case EXPR_PACK_EXPANSION:
2448 case TYPEID_EXPR:
2449 case MEMBER_REF:
2450 case DOTSTAR_EXPR:
2451 case NEW_EXPR:
2452 case VEC_NEW_EXPR:
2453 case DELETE_EXPR:
2454 case VEC_DELETE_EXPR:
2455 case MODOP_EXPR:
2456 case ABS_EXPR:
2457 case CONJ_EXPR:
2458 case VECTOR_CST:
2459 case FIXED_CST:
2460 case UNORDERED_EXPR:
2461 case ORDERED_EXPR:
2462 case UNLT_EXPR:
2463 case UNLE_EXPR:
2464 case UNGT_EXPR:
2465 case UNGE_EXPR:
2466 case UNEQ_EXPR:
2467 case LTGT_EXPR:
2468 case COMPLEX_EXPR:
2469 case BIT_FIELD_REF:
2470 case FIX_TRUNC_EXPR:
2471 case FLOAT_EXPR:
2472 pp_expression (cxx_pp, t);
2473 break;
2475 case TRUTH_AND_EXPR:
2476 case TRUTH_OR_EXPR:
2477 case TRUTH_XOR_EXPR:
2478 if (flags & TFF_EXPR_IN_PARENS)
2479 pp_cxx_left_paren (cxx_pp);
2480 pp_expression (cxx_pp, t);
2481 if (flags & TFF_EXPR_IN_PARENS)
2482 pp_cxx_right_paren (cxx_pp);
2483 break;
2485 case OBJ_TYPE_REF:
2486 dump_expr (resolve_virtual_fun_from_obj_type_ref (t), flags);
2487 break;
2489 /* This list is incomplete, but should suffice for now.
2490 It is very important that `sorry' does not call
2491 `report_error_function'. That could cause an infinite loop. */
2492 default:
2493 pp_unsupported_tree (cxx_pp, t);
2494 /* fall through to ERROR_MARK... */
2495 case ERROR_MARK:
2496 pp_string (cxx_pp, M_("<expression error>"));
2497 break;
2501 static void
2502 dump_binary_op (const char *opstring, tree t, int flags)
2504 pp_cxx_left_paren (cxx_pp);
2505 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2506 pp_cxx_whitespace (cxx_pp);
2507 if (opstring)
2508 pp_cxx_ws_string (cxx_pp, opstring);
2509 else
2510 pp_string (cxx_pp, M_("<unknown operator>"));
2511 pp_cxx_whitespace (cxx_pp);
2512 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2513 pp_cxx_right_paren (cxx_pp);
2516 static void
2517 dump_unary_op (const char *opstring, tree t, int flags)
2519 if (flags & TFF_EXPR_IN_PARENS)
2520 pp_cxx_left_paren (cxx_pp);
2521 pp_cxx_ws_string (cxx_pp, opstring);
2522 dump_expr (TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2523 if (flags & TFF_EXPR_IN_PARENS)
2524 pp_cxx_right_paren (cxx_pp);
2527 static void
2528 reinit_cxx_pp (void)
2530 pp_clear_output_area (cxx_pp);
2531 pp_base (cxx_pp)->padding = pp_none;
2532 pp_indentation (cxx_pp) = 0;
2533 pp_needs_newline (cxx_pp) = false;
2534 cxx_pp->enclosing_scope = current_function_decl;
2538 /* Exported interface to stringifying types, exprs and decls under TFF_*
2539 control. */
2541 const char *
2542 type_as_string (tree typ, int flags)
2544 reinit_cxx_pp ();
2545 pp_translate_identifiers (cxx_pp) = false;
2546 dump_type (typ, flags);
2547 return pp_formatted_text (cxx_pp);
2550 const char *
2551 type_as_string_translate (tree typ, int flags)
2553 reinit_cxx_pp ();
2554 dump_type (typ, flags);
2555 return pp_formatted_text (cxx_pp);
2558 const char *
2559 expr_as_string (tree decl, int flags)
2561 reinit_cxx_pp ();
2562 pp_translate_identifiers (cxx_pp) = false;
2563 dump_expr (decl, flags);
2564 return pp_formatted_text (cxx_pp);
2567 /* Wrap decl_as_string with options appropriate for dwarf. */
2569 const char *
2570 decl_as_dwarf_string (tree decl, int flags)
2572 const char *name;
2573 /* Curiously, reinit_cxx_pp doesn't reset the flags field, so setting the flag
2574 here will be adequate to get the desired behaviour. */
2575 pp_c_base (cxx_pp)->flags |= pp_c_flag_gnu_v3;
2576 name = decl_as_string (decl, flags);
2577 /* Subsequent calls to the pretty printer shouldn't use this style. */
2578 pp_c_base (cxx_pp)->flags &= ~pp_c_flag_gnu_v3;
2579 return name;
2582 const char *
2583 decl_as_string (tree decl, int flags)
2585 reinit_cxx_pp ();
2586 pp_translate_identifiers (cxx_pp) = false;
2587 dump_decl (decl, flags);
2588 return pp_formatted_text (cxx_pp);
2591 const char *
2592 decl_as_string_translate (tree decl, int flags)
2594 reinit_cxx_pp ();
2595 dump_decl (decl, flags);
2596 return pp_formatted_text (cxx_pp);
2599 /* Wrap lang_decl_name with options appropriate for dwarf. */
2601 const char *
2602 lang_decl_dwarf_name (tree decl, int v, bool translate)
2604 const char *name;
2605 /* Curiously, reinit_cxx_pp doesn't reset the flags field, so setting the flag
2606 here will be adequate to get the desired behaviour. */
2607 pp_c_base (cxx_pp)->flags |= pp_c_flag_gnu_v3;
2608 name = lang_decl_name (decl, v, translate);
2609 /* Subsequent calls to the pretty printer shouldn't use this style. */
2610 pp_c_base (cxx_pp)->flags &= ~pp_c_flag_gnu_v3;
2611 return name;
2614 /* Generate the three forms of printable names for cxx_printable_name. */
2616 const char *
2617 lang_decl_name (tree decl, int v, bool translate)
2619 if (v >= 2)
2620 return (translate
2621 ? decl_as_string_translate (decl, TFF_DECL_SPECIFIERS)
2622 : decl_as_string (decl, TFF_DECL_SPECIFIERS));
2624 reinit_cxx_pp ();
2625 pp_translate_identifiers (cxx_pp) = translate;
2626 if (v == 1
2627 && (DECL_CLASS_SCOPE_P (decl)
2628 || (DECL_NAMESPACE_SCOPE_P (decl)
2629 && CP_DECL_CONTEXT (decl) != global_namespace)))
2631 dump_type (CP_DECL_CONTEXT (decl), TFF_PLAIN_IDENTIFIER);
2632 pp_cxx_colon_colon (cxx_pp);
2635 if (TREE_CODE (decl) == FUNCTION_DECL)
2636 dump_function_name (decl, TFF_PLAIN_IDENTIFIER);
2637 else if ((DECL_NAME (decl) == NULL_TREE)
2638 && TREE_CODE (decl) == NAMESPACE_DECL)
2639 dump_decl (decl, TFF_PLAIN_IDENTIFIER | TFF_UNQUALIFIED_NAME);
2640 else
2641 dump_decl (DECL_NAME (decl), TFF_PLAIN_IDENTIFIER);
2643 return pp_formatted_text (cxx_pp);
2646 /* Return the location of a tree passed to %+ formats. */
2648 location_t
2649 location_of (tree t)
2651 if (TREE_CODE (t) == PARM_DECL && DECL_CONTEXT (t))
2652 t = DECL_CONTEXT (t);
2653 else if (TYPE_P (t))
2655 t = TYPE_MAIN_DECL (t);
2656 if (t == NULL_TREE)
2657 return input_location;
2659 else if (TREE_CODE (t) == OVERLOAD)
2660 t = OVL_FUNCTION (t);
2662 if (DECL_P (t))
2663 return DECL_SOURCE_LOCATION (t);
2664 return EXPR_LOC_OR_HERE (t);
2667 /* Now the interfaces from error et al to dump_type et al. Each takes an
2668 on/off VERBOSE flag and supply the appropriate TFF_ flags to a dump_
2669 function. */
2671 static const char *
2672 decl_to_string (tree decl, int verbose)
2674 int flags = 0;
2676 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == RECORD_TYPE
2677 || TREE_CODE (decl) == UNION_TYPE || TREE_CODE (decl) == ENUMERAL_TYPE)
2678 flags = TFF_CLASS_KEY_OR_ENUM;
2679 if (verbose)
2680 flags |= TFF_DECL_SPECIFIERS;
2681 else if (TREE_CODE (decl) == FUNCTION_DECL)
2682 flags |= TFF_DECL_SPECIFIERS | TFF_RETURN_TYPE;
2683 flags |= TFF_TEMPLATE_HEADER;
2685 reinit_cxx_pp ();
2686 dump_decl (decl, flags);
2687 return pp_formatted_text (cxx_pp);
2690 static const char *
2691 expr_to_string (tree decl)
2693 reinit_cxx_pp ();
2694 dump_expr (decl, 0);
2695 return pp_formatted_text (cxx_pp);
2698 static const char *
2699 fndecl_to_string (tree fndecl, int verbose)
2701 int flags;
2703 flags = TFF_EXCEPTION_SPECIFICATION | TFF_DECL_SPECIFIERS
2704 | TFF_TEMPLATE_HEADER;
2705 if (verbose)
2706 flags |= TFF_FUNCTION_DEFAULT_ARGUMENTS;
2707 reinit_cxx_pp ();
2708 dump_decl (fndecl, flags);
2709 return pp_formatted_text (cxx_pp);
2713 static const char *
2714 code_to_string (enum tree_code c)
2716 return tree_code_name [c];
2719 const char *
2720 language_to_string (enum languages c)
2722 switch (c)
2724 case lang_c:
2725 return "C";
2727 case lang_cplusplus:
2728 return "C++";
2730 case lang_java:
2731 return "Java";
2733 default:
2734 gcc_unreachable ();
2736 return NULL;
2739 /* Return the proper printed version of a parameter to a C++ function. */
2741 static const char *
2742 parm_to_string (int p)
2744 reinit_cxx_pp ();
2745 if (p < 0)
2746 pp_string (cxx_pp, "'this'");
2747 else
2748 pp_decimal_int (cxx_pp, p + 1);
2749 return pp_formatted_text (cxx_pp);
2752 static const char *
2753 op_to_string (enum tree_code p)
2755 tree id = operator_name_info[(int) p].identifier;
2756 return id ? IDENTIFIER_POINTER (id) : M_("<unknown>");
2759 static const char *
2760 type_to_string (tree typ, int verbose)
2762 int flags = 0;
2763 if (verbose)
2764 flags |= TFF_CLASS_KEY_OR_ENUM;
2765 flags |= TFF_TEMPLATE_HEADER;
2767 reinit_cxx_pp ();
2768 dump_type (typ, flags);
2769 /* If we're printing a type that involves typedefs, also print the
2770 stripped version. But sometimes the stripped version looks
2771 exactly the same, so we don't want it after all. To avoid printing
2772 it in that case, we play ugly obstack games. */
2773 if (typ && TYPE_P (typ) && typ != TYPE_CANONICAL (typ)
2774 && !uses_template_parms (typ))
2776 int aka_start; char *p;
2777 struct obstack *ob = pp_base (cxx_pp)->buffer->obstack;
2778 /* Remember the end of the initial dump. */
2779 int len = obstack_object_size (ob);
2780 tree aka = strip_typedefs (typ);
2781 pp_string (cxx_pp, " {aka");
2782 pp_cxx_whitespace (cxx_pp);
2783 /* And remember the start of the aka dump. */
2784 aka_start = obstack_object_size (ob);
2785 dump_type (aka, flags);
2786 pp_character (cxx_pp, '}');
2787 p = (char*)obstack_base (ob);
2788 /* If they are identical, cut off the aka with a NUL. */
2789 if (memcmp (p, p+aka_start, len) == 0)
2790 p[len] = '\0';
2792 return pp_formatted_text (cxx_pp);
2795 static const char *
2796 assop_to_string (enum tree_code p)
2798 tree id = assignment_operator_name_info[(int) p].identifier;
2799 return id ? IDENTIFIER_POINTER (id) : M_("{unknown}");
2802 static const char *
2803 args_to_string (tree p, int verbose)
2805 int flags = 0;
2806 if (verbose)
2807 flags |= TFF_CLASS_KEY_OR_ENUM;
2809 if (p == NULL_TREE)
2810 return "";
2812 if (TYPE_P (TREE_VALUE (p)))
2813 return type_as_string_translate (p, flags);
2815 reinit_cxx_pp ();
2816 for (; p; p = TREE_CHAIN (p))
2818 if (TREE_VALUE (p) == null_node)
2819 pp_cxx_ws_string (cxx_pp, "NULL");
2820 else
2821 dump_type (error_type (TREE_VALUE (p)), flags);
2822 if (TREE_CHAIN (p))
2823 pp_separate_with_comma (cxx_pp);
2825 return pp_formatted_text (cxx_pp);
2828 /* Pretty-print a deduction substitution (from deduction_tsubst_fntype). P
2829 is a TREE_LIST with purpose the TEMPLATE_DECL, value the template
2830 arguments. */
2832 static const char *
2833 subst_to_string (tree p)
2835 tree decl = TREE_PURPOSE (p);
2836 tree targs = TREE_VALUE (p);
2837 tree tparms = DECL_TEMPLATE_PARMS (decl);
2838 int flags = TFF_DECL_SPECIFIERS|TFF_TEMPLATE_HEADER;
2840 if (p == NULL_TREE)
2841 return "";
2843 reinit_cxx_pp ();
2844 dump_template_decl (TREE_PURPOSE (p), flags);
2845 pp_cxx_whitespace (cxx_pp);
2846 pp_cxx_left_bracket (cxx_pp);
2847 pp_cxx_ws_string (cxx_pp, M_("with"));
2848 pp_cxx_whitespace (cxx_pp);
2849 dump_template_bindings (tparms, targs, NULL);
2850 pp_cxx_right_bracket (cxx_pp);
2851 return pp_formatted_text (cxx_pp);
2854 static const char *
2855 cv_to_string (tree p, int v)
2857 reinit_cxx_pp ();
2858 pp_base (cxx_pp)->padding = v ? pp_before : pp_none;
2859 pp_cxx_cv_qualifier_seq (cxx_pp, p);
2860 return pp_formatted_text (cxx_pp);
2863 /* Langhook for print_error_function. */
2864 void
2865 cxx_print_error_function (diagnostic_context *context, const char *file,
2866 diagnostic_info *diagnostic)
2868 lhd_print_error_function (context, file, diagnostic);
2869 pp_base_set_prefix (context->printer, file);
2870 maybe_print_instantiation_context (context);
2873 static void
2874 cp_diagnostic_starter (diagnostic_context *context,
2875 diagnostic_info *diagnostic)
2877 diagnostic_report_current_module (context, diagnostic->location);
2878 cp_print_error_function (context, diagnostic);
2879 maybe_print_instantiation_context (context);
2880 maybe_print_constexpr_context (context);
2881 pp_base_set_prefix (context->printer, diagnostic_build_prefix (context,
2882 diagnostic));
2885 static void
2886 cp_diagnostic_finalizer (diagnostic_context *context,
2887 diagnostic_info *diagnostic)
2889 virt_loc_aware_diagnostic_finalizer (context, diagnostic);
2890 pp_base_destroy_prefix (context->printer);
2893 /* Print current function onto BUFFER, in the process of reporting
2894 a diagnostic message. Called from cp_diagnostic_starter. */
2895 static void
2896 cp_print_error_function (diagnostic_context *context,
2897 diagnostic_info *diagnostic)
2899 /* If we are in an instantiation context, current_function_decl is likely
2900 to be wrong, so just rely on print_instantiation_full_context. */
2901 if (current_instantiation ())
2902 return;
2903 if (diagnostic_last_function_changed (context, diagnostic))
2905 const char *old_prefix = context->printer->prefix;
2906 const char *file = LOCATION_FILE (diagnostic->location);
2907 tree abstract_origin = diagnostic_abstract_origin (diagnostic);
2908 char *new_prefix = (file && abstract_origin == NULL)
2909 ? file_name_as_prefix (file) : NULL;
2911 pp_base_set_prefix (context->printer, new_prefix);
2913 if (current_function_decl == NULL)
2914 pp_base_string (context->printer, _("At global scope:"));
2915 else
2917 tree fndecl, ao;
2919 if (abstract_origin)
2921 ao = BLOCK_ABSTRACT_ORIGIN (abstract_origin);
2922 while (TREE_CODE (ao) == BLOCK
2923 && BLOCK_ABSTRACT_ORIGIN (ao)
2924 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
2925 ao = BLOCK_ABSTRACT_ORIGIN (ao);
2926 gcc_assert (TREE_CODE (ao) == FUNCTION_DECL);
2927 fndecl = ao;
2929 else
2930 fndecl = current_function_decl;
2932 pp_printf (context->printer, function_category (fndecl),
2933 cxx_printable_name_translate (fndecl, 2));
2935 while (abstract_origin)
2937 location_t *locus;
2938 tree block = abstract_origin;
2940 locus = &BLOCK_SOURCE_LOCATION (block);
2941 fndecl = NULL;
2942 block = BLOCK_SUPERCONTEXT (block);
2943 while (block && TREE_CODE (block) == BLOCK
2944 && BLOCK_ABSTRACT_ORIGIN (block))
2946 ao = BLOCK_ABSTRACT_ORIGIN (block);
2948 while (TREE_CODE (ao) == BLOCK
2949 && BLOCK_ABSTRACT_ORIGIN (ao)
2950 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
2951 ao = BLOCK_ABSTRACT_ORIGIN (ao);
2953 if (TREE_CODE (ao) == FUNCTION_DECL)
2955 fndecl = ao;
2956 break;
2958 else if (TREE_CODE (ao) != BLOCK)
2959 break;
2961 block = BLOCK_SUPERCONTEXT (block);
2963 if (fndecl)
2964 abstract_origin = block;
2965 else
2967 while (block && TREE_CODE (block) == BLOCK)
2968 block = BLOCK_SUPERCONTEXT (block);
2970 if (block && TREE_CODE (block) == FUNCTION_DECL)
2971 fndecl = block;
2972 abstract_origin = NULL;
2974 if (fndecl)
2976 expanded_location s = expand_location (*locus);
2977 pp_base_character (context->printer, ',');
2978 pp_base_newline (context->printer);
2979 if (s.file != NULL)
2981 if (context->show_column && s.column != 0)
2982 pp_printf (context->printer,
2983 _(" inlined from %qs at %s:%d:%d"),
2984 cxx_printable_name_translate (fndecl, 2),
2985 s.file, s.line, s.column);
2986 else
2987 pp_printf (context->printer,
2988 _(" inlined from %qs at %s:%d"),
2989 cxx_printable_name_translate (fndecl, 2),
2990 s.file, s.line);
2993 else
2994 pp_printf (context->printer, _(" inlined from %qs"),
2995 cxx_printable_name_translate (fndecl, 2));
2998 pp_base_character (context->printer, ':');
3000 pp_base_newline (context->printer);
3002 diagnostic_set_last_function (context, diagnostic);
3003 pp_base_destroy_prefix (context->printer);
3004 context->printer->prefix = old_prefix;
3008 /* Returns a description of FUNCTION using standard terminology. The
3009 result is a format string of the form "In CATEGORY %qs". */
3010 static const char *
3011 function_category (tree fn)
3013 /* We can get called from the middle-end for diagnostics of function
3014 clones. Make sure we have language specific information before
3015 dereferencing it. */
3016 if (DECL_LANG_SPECIFIC (STRIP_TEMPLATE (fn))
3017 && DECL_FUNCTION_MEMBER_P (fn))
3019 if (DECL_STATIC_FUNCTION_P (fn))
3020 return _("In static member function %qs");
3021 else if (DECL_COPY_CONSTRUCTOR_P (fn))
3022 return _("In copy constructor %qs");
3023 else if (DECL_CONSTRUCTOR_P (fn))
3024 return _("In constructor %qs");
3025 else if (DECL_DESTRUCTOR_P (fn))
3026 return _("In destructor %qs");
3027 else if (LAMBDA_FUNCTION_P (fn))
3028 return _("In lambda function");
3029 else
3030 return _("In member function %qs");
3032 else
3033 return _("In function %qs");
3036 /* Report the full context of a current template instantiation,
3037 onto BUFFER. */
3038 static void
3039 print_instantiation_full_context (diagnostic_context *context)
3041 struct tinst_level *p = current_instantiation ();
3042 location_t location = input_location;
3044 if (p)
3046 pp_verbatim (context->printer,
3047 TREE_CODE (p->decl) == TREE_LIST
3048 ? _("%s: In substitution of %qS:\n")
3049 : _("%s: In instantiation of %q#D:\n"),
3050 LOCATION_FILE (location),
3051 p->decl);
3053 location = p->locus;
3054 p = p->next;
3057 print_instantiation_partial_context (context, p, location);
3060 /* Helper function of print_instantiation_partial_context() that
3061 prints a single line of instantiation context. */
3063 static void
3064 print_instantiation_partial_context_line (diagnostic_context *context,
3065 const struct tinst_level *t,
3066 location_t loc, bool recursive_p)
3068 expanded_location xloc;
3069 xloc = expand_location (loc);
3071 if (context->show_column)
3072 pp_verbatim (context->printer, _("%s:%d:%d: "),
3073 xloc.file, xloc.line, xloc.column);
3074 else
3075 pp_verbatim (context->printer, _("%s:%d: "),
3076 xloc.file, xloc.line);
3078 if (t != NULL)
3080 if (TREE_CODE (t->decl) == TREE_LIST)
3081 pp_verbatim (context->printer,
3082 recursive_p
3083 ? _("recursively required by substitution of %qS\n")
3084 : _("required by substitution of %qS\n"),
3085 t->decl);
3086 else
3087 pp_verbatim (context->printer,
3088 recursive_p
3089 ? _("recursively required from %q#D\n")
3090 : _("required from %q#D\n"),
3091 t->decl);
3093 else
3095 pp_verbatim (context->printer,
3096 recursive_p
3097 ? _("recursively required from here")
3098 : _("required from here"));
3102 /* Same as print_instantiation_full_context but less verbose. */
3104 static void
3105 print_instantiation_partial_context (diagnostic_context *context,
3106 struct tinst_level *t0, location_t loc)
3108 struct tinst_level *t;
3109 int n_total = 0;
3110 int n;
3111 location_t prev_loc = loc;
3113 for (t = t0; t != NULL; t = t->next)
3114 if (prev_loc != t->locus)
3116 prev_loc = t->locus;
3117 n_total++;
3120 t = t0;
3122 if (template_backtrace_limit
3123 && n_total > template_backtrace_limit)
3125 int skip = n_total - template_backtrace_limit;
3126 int head = template_backtrace_limit / 2;
3128 /* Avoid skipping just 1. If so, skip 2. */
3129 if (skip == 1)
3131 skip = 2;
3132 head = (template_backtrace_limit - 1) / 2;
3135 for (n = 0; n < head; n++)
3137 gcc_assert (t != NULL);
3138 if (loc != t->locus)
3139 print_instantiation_partial_context_line (context, t, loc,
3140 /*recursive_p=*/false);
3141 loc = t->locus;
3142 t = t->next;
3144 if (t != NULL && skip > 0)
3146 expanded_location xloc;
3147 xloc = expand_location (loc);
3148 if (context->show_column)
3149 pp_verbatim (context->printer,
3150 _("%s:%d:%d: [ skipping %d instantiation contexts, "
3151 "use -ftemplate-backtrace-limit=0 to disable ]\n"),
3152 xloc.file, xloc.line, xloc.column, skip);
3153 else
3154 pp_verbatim (context->printer,
3155 _("%s:%d: [ skipping %d instantiation contexts, "
3156 "use -ftemplate-backtrace-limit=0 to disable ]\n"),
3157 xloc.file, xloc.line, skip);
3159 do {
3160 loc = t->locus;
3161 t = t->next;
3162 } while (t != NULL && --skip > 0);
3166 while (t != NULL)
3168 while (t->next != NULL && t->locus == t->next->locus)
3170 loc = t->locus;
3171 t = t->next;
3173 print_instantiation_partial_context_line (context, t, loc,
3174 t->locus == loc);
3175 loc = t->locus;
3176 t = t->next;
3178 print_instantiation_partial_context_line (context, NULL, loc,
3179 /*recursive_p=*/false);
3180 pp_base_newline (context->printer);
3183 /* Called from cp_thing to print the template context for an error. */
3184 static void
3185 maybe_print_instantiation_context (diagnostic_context *context)
3187 if (!problematic_instantiation_changed () || current_instantiation () == 0)
3188 return;
3190 record_last_problematic_instantiation ();
3191 print_instantiation_full_context (context);
3194 /* Report the bare minimum context of a template instantiation. */
3195 void
3196 print_instantiation_context (void)
3198 print_instantiation_partial_context
3199 (global_dc, current_instantiation (), input_location);
3200 pp_base_newline (global_dc->printer);
3201 diagnostic_flush_buffer (global_dc);
3204 /* Report what constexpr call(s) we're trying to expand, if any. */
3206 void
3207 maybe_print_constexpr_context (diagnostic_context *context)
3209 VEC(tree,heap) *call_stack = cx_error_context ();
3210 unsigned ix;
3211 tree t;
3213 FOR_EACH_VEC_ELT (tree, call_stack, ix, t)
3215 expanded_location xloc = expand_location (EXPR_LOCATION (t));
3216 const char *s = expr_as_string (t, 0);
3217 if (context->show_column)
3218 pp_verbatim (context->printer,
3219 _("%s:%d:%d: in constexpr expansion of %qs"),
3220 xloc.file, xloc.line, xloc.column, s);
3221 else
3222 pp_verbatim (context->printer,
3223 _("%s:%d: in constexpr expansion of %qs"),
3224 xloc.file, xloc.line, s);
3225 pp_base_newline (context->printer);
3229 /* Called from output_format -- during diagnostic message processing --
3230 to handle C++ specific format specifier with the following meanings:
3231 %A function argument-list.
3232 %C tree code.
3233 %D declaration.
3234 %E expression.
3235 %F function declaration.
3236 %L language as used in extern "lang".
3237 %O binary operator.
3238 %P function parameter whose position is indicated by an integer.
3239 %Q assignment operator.
3240 %T type.
3241 %V cv-qualifier. */
3242 static bool
3243 cp_printer (pretty_printer *pp, text_info *text, const char *spec,
3244 int precision, bool wide, bool set_locus, bool verbose)
3246 const char *result;
3247 tree t = NULL;
3248 #define next_tree (t = va_arg (*text->args_ptr, tree))
3249 #define next_tcode ((enum tree_code) va_arg (*text->args_ptr, int))
3250 #define next_lang ((enum languages) va_arg (*text->args_ptr, int))
3251 #define next_int va_arg (*text->args_ptr, int)
3253 if (precision != 0 || wide)
3254 return false;
3256 if (text->locus == NULL)
3257 set_locus = false;
3259 switch (*spec)
3261 case 'A': result = args_to_string (next_tree, verbose); break;
3262 case 'C': result = code_to_string (next_tcode); break;
3263 case 'D':
3265 tree temp = next_tree;
3266 if (DECL_P (temp)
3267 && DECL_DEBUG_EXPR_IS_FROM (temp) && DECL_DEBUG_EXPR (temp))
3269 temp = DECL_DEBUG_EXPR (temp);
3270 if (!DECL_P (temp))
3272 result = expr_to_string (temp);
3273 break;
3276 result = decl_to_string (temp, verbose);
3278 break;
3279 case 'E': result = expr_to_string (next_tree); break;
3280 case 'F': result = fndecl_to_string (next_tree, verbose); break;
3281 case 'L': result = language_to_string (next_lang); break;
3282 case 'O': result = op_to_string (next_tcode); break;
3283 case 'P': result = parm_to_string (next_int); break;
3284 case 'Q': result = assop_to_string (next_tcode); break;
3285 case 'S': result = subst_to_string (next_tree); break;
3286 case 'T': result = type_to_string (next_tree, verbose); break;
3287 case 'V': result = cv_to_string (next_tree, verbose); break;
3289 case 'K':
3290 percent_K_format (text);
3291 return true;
3293 default:
3294 return false;
3297 pp_base_string (pp, result);
3298 if (set_locus && t != NULL)
3299 *text->locus = location_of (t);
3300 return true;
3301 #undef next_tree
3302 #undef next_tcode
3303 #undef next_lang
3304 #undef next_int
3307 /* Warn about the use of C++0x features when appropriate. */
3308 void
3309 maybe_warn_cpp0x (cpp0x_warn_str str)
3311 if ((cxx_dialect == cxx98) && !in_system_header)
3312 /* We really want to suppress this warning in system headers,
3313 because libstdc++ uses variadic templates even when we aren't
3314 in C++0x mode. */
3315 switch (str)
3317 case CPP0X_INITIALIZER_LISTS:
3318 pedwarn (input_location, 0,
3319 "extended initializer lists "
3320 "only available with -std=c++11 or -std=gnu++11");
3321 break;
3322 case CPP0X_EXPLICIT_CONVERSION:
3323 pedwarn (input_location, 0,
3324 "explicit conversion operators "
3325 "only available with -std=c++11 or -std=gnu++11");
3326 break;
3327 case CPP0X_VARIADIC_TEMPLATES:
3328 pedwarn (input_location, 0,
3329 "variadic templates "
3330 "only available with -std=c++11 or -std=gnu++11");
3331 break;
3332 case CPP0X_LAMBDA_EXPR:
3333 pedwarn (input_location, 0,
3334 "lambda expressions "
3335 "only available with -std=c++11 or -std=gnu++11");
3336 break;
3337 case CPP0X_AUTO:
3338 pedwarn (input_location, 0,
3339 "C++0x auto only available with -std=c++11 or -std=gnu++11");
3340 break;
3341 case CPP0X_SCOPED_ENUMS:
3342 pedwarn (input_location, 0,
3343 "scoped enums only available with -std=c++11 or -std=gnu++11");
3344 break;
3345 case CPP0X_DEFAULTED_DELETED:
3346 pedwarn (input_location, 0,
3347 "defaulted and deleted functions "
3348 "only available with -std=c++11 or -std=gnu++11");
3349 break;
3350 case CPP0X_INLINE_NAMESPACES:
3351 pedwarn (input_location, OPT_Wpedantic,
3352 "inline namespaces "
3353 "only available with -std=c++11 or -std=gnu++11");
3354 break;
3355 case CPP0X_OVERRIDE_CONTROLS:
3356 pedwarn (input_location, 0,
3357 "override controls (override/final) "
3358 "only available with -std=c++11 or -std=gnu++11");
3359 break;
3360 case CPP0X_NSDMI:
3361 pedwarn (input_location, 0,
3362 "non-static data member initializers "
3363 "only available with -std=c++11 or -std=gnu++11");
3364 break;
3365 case CPP0X_USER_DEFINED_LITERALS:
3366 pedwarn (input_location, 0,
3367 "user-defined literals "
3368 "only available with -std=c++11 or -std=gnu++11");
3369 break;
3370 case CPP0X_DELEGATING_CTORS:
3371 pedwarn (input_location, 0,
3372 "delegating constructors "
3373 "only available with -std=c++11 or -std=gnu++11");
3374 break;
3375 default:
3376 gcc_unreachable ();
3380 /* Warn about the use of variadic templates when appropriate. */
3381 void
3382 maybe_warn_variadic_templates (void)
3384 maybe_warn_cpp0x (CPP0X_VARIADIC_TEMPLATES);
3388 /* Issue an ISO C++98 pedantic warning at LOCATION, conditional on
3389 option OPT with text GMSGID. Use this function to report
3390 diagnostics for constructs that are invalid C++98, but valid
3391 C++0x. */
3392 bool
3393 pedwarn_cxx98 (location_t location, int opt, const char *gmsgid, ...)
3395 diagnostic_info diagnostic;
3396 va_list ap;
3397 bool ret;
3399 va_start (ap, gmsgid);
3400 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
3401 (cxx_dialect == cxx98) ? DK_PEDWARN : DK_WARNING);
3402 diagnostic.option_index = opt;
3403 ret = report_diagnostic (&diagnostic);
3404 va_end (ap);
3405 return ret;
3408 /* Issue a diagnostic that NAME cannot be found in SCOPE. DECL is what
3409 we found when we tried to do the lookup. LOCATION is the location of
3410 the NAME identifier. */
3412 void
3413 qualified_name_lookup_error (tree scope, tree name,
3414 tree decl, location_t location)
3416 if (scope == error_mark_node)
3417 ; /* We already complained. */
3418 else if (TYPE_P (scope))
3420 if (!COMPLETE_TYPE_P (scope))
3421 error_at (location, "incomplete type %qT used in nested name specifier",
3422 scope);
3423 else if (TREE_CODE (decl) == TREE_LIST)
3425 error_at (location, "reference to %<%T::%D%> is ambiguous",
3426 scope, name);
3427 print_candidates (decl);
3429 else
3430 error_at (location, "%qD is not a member of %qT", name, scope);
3432 else if (scope != global_namespace)
3434 error_at (location, "%qD is not a member of %qD", name, scope);
3435 suggest_alternatives_for (location, name);
3437 else
3439 error_at (location, "%<::%D%> has not been declared", name);
3440 suggest_alternatives_for (location, name);