1 /* Call-backs for C++ error reporting.
2 This code is non-reentrant.
3 Copyright (C) 1993-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
27 #include "double-int.h"
34 #include "stringpool.h"
37 #include "diagnostic.h"
38 #include "tree-diagnostic.h"
39 #include "langhooks-def.h"
41 #include "cxx-pretty-print.h"
42 #include "tree-pretty-print.h"
43 #include "c-family/c-objc.h"
45 #include "internal-fn.h"
47 #include <new> // For placement-new.
49 #define pp_separate_with_comma(PP) pp_cxx_separate_with (PP, ',')
50 #define pp_separate_with_semicolon(PP) pp_cxx_separate_with (PP, ';')
52 /* cxx_pp is a C++ front-end-specific pretty printer: this is where we
53 dump C++ ASTs as strings. It is mostly used only by the various
54 tree -> string functions that are occasionally called from the
55 debugger or by the front-end for things like
56 __PRETTY_FUNCTION__. */
57 static cxx_pretty_printer scratch_pretty_printer
;
58 static cxx_pretty_printer
* cxx_pp
= &scratch_pretty_printer
;
60 /* Translate if being used for diagnostics, but not for dump files or
62 #define M_(msgid) (pp_translate_identifiers (cxx_pp) ? _(msgid) : (msgid))
64 # define NEXT_CODE(T) (TREE_CODE (TREE_TYPE (T)))
66 static const char *args_to_string (tree
, int);
67 static const char *assop_to_string (enum tree_code
);
68 static const char *code_to_string (enum tree_code
);
69 static const char *cv_to_string (tree
, int);
70 static const char *decl_to_string (tree
, int);
71 static const char *expr_to_string (tree
);
72 static const char *fndecl_to_string (tree
, int);
73 static const char *op_to_string (enum tree_code
);
74 static const char *parm_to_string (int);
75 static const char *type_to_string (tree
, int);
77 static void dump_alias_template_specialization (cxx_pretty_printer
*, tree
, int);
78 static void dump_type (cxx_pretty_printer
*, tree
, int);
79 static void dump_typename (cxx_pretty_printer
*, tree
, int);
80 static void dump_simple_decl (cxx_pretty_printer
*, tree
, tree
, int);
81 static void dump_decl (cxx_pretty_printer
*, tree
, int);
82 static void dump_template_decl (cxx_pretty_printer
*, tree
, int);
83 static void dump_function_decl (cxx_pretty_printer
*, tree
, int);
84 static void dump_expr (cxx_pretty_printer
*, tree
, int);
85 static void dump_unary_op (cxx_pretty_printer
*, const char *, tree
, int);
86 static void dump_binary_op (cxx_pretty_printer
*, const char *, tree
, int);
87 static void dump_aggr_type (cxx_pretty_printer
*, tree
, int);
88 static void dump_type_prefix (cxx_pretty_printer
*, tree
, int);
89 static void dump_type_suffix (cxx_pretty_printer
*, tree
, int);
90 static void dump_function_name (cxx_pretty_printer
*, tree
, int);
91 static void dump_call_expr_args (cxx_pretty_printer
*, tree
, int, bool);
92 static void dump_aggr_init_expr_args (cxx_pretty_printer
*, tree
, int, bool);
93 static void dump_expr_list (cxx_pretty_printer
*, tree
, int);
94 static void dump_global_iord (cxx_pretty_printer
*, tree
);
95 static void dump_parameters (cxx_pretty_printer
*, tree
, int);
96 static void dump_ref_qualifier (cxx_pretty_printer
*, tree
, int);
97 static void dump_exception_spec (cxx_pretty_printer
*, tree
, int);
98 static void dump_template_argument (cxx_pretty_printer
*, tree
, int);
99 static void dump_template_argument_list (cxx_pretty_printer
*, tree
, int);
100 static void dump_template_parameter (cxx_pretty_printer
*, tree
, int);
101 static void dump_template_bindings (cxx_pretty_printer
*, tree
, tree
,
103 static void dump_scope (cxx_pretty_printer
*, tree
, int);
104 static void dump_template_parms (cxx_pretty_printer
*, tree
, int, int);
105 static int get_non_default_template_args_count (tree
, int);
106 static const char *function_category (tree
);
107 static void maybe_print_constexpr_context (diagnostic_context
*);
108 static void maybe_print_instantiation_context (diagnostic_context
*);
109 static void print_instantiation_full_context (diagnostic_context
*);
110 static void print_instantiation_partial_context (diagnostic_context
*,
111 struct tinst_level
*,
113 static void cp_diagnostic_starter (diagnostic_context
*, diagnostic_info
*);
114 static void cp_print_error_function (diagnostic_context
*, diagnostic_info
*);
116 static bool cp_printer (pretty_printer
*, text_info
*, const char *,
117 int, bool, bool, bool);
119 /* CONTEXT->printer is a basic pretty printer that was constructed
120 presumably by diagnostic_initialize(), called early in the
121 compiler's initialization process (in general_init) Before the FE
122 is initialized. This (C++) FE-specific diagnostic initializer is
123 thus replacing the basic pretty printer with one that has C++-aware
127 cxx_initialize_diagnostics (diagnostic_context
*context
)
129 pretty_printer
*base
= context
->printer
;
130 cxx_pretty_printer
*pp
= XNEW (cxx_pretty_printer
);
131 context
->printer
= new (pp
) cxx_pretty_printer ();
133 /* It is safe to free this object because it was previously XNEW()'d. */
134 base
->~pretty_printer ();
137 c_common_diagnostics_set_defaults (context
);
138 diagnostic_starter (context
) = cp_diagnostic_starter
;
139 /* diagnostic_finalizer is already c_diagnostic_finalizer. */
140 diagnostic_format_decoder (context
) = cp_printer
;
143 /* Initialize the global cxx_pp that is used as the memory store for
144 the string representation of C++ AST. See the description of
150 new (cxx_pp
) cxx_pretty_printer ();
153 /* Dump a scope, if deemed necessary. */
156 dump_scope (cxx_pretty_printer
*pp
, tree scope
, int flags
)
158 int f
= flags
& (TFF_SCOPE
| TFF_CHASE_TYPEDEF
);
160 if (scope
== NULL_TREE
)
163 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
165 if (scope
!= global_namespace
)
167 dump_decl (pp
, scope
, f
);
168 pp_cxx_colon_colon (pp
);
171 else if (AGGREGATE_TYPE_P (scope
))
173 dump_type (pp
, scope
, f
);
174 pp_cxx_colon_colon (pp
);
176 else if ((flags
& TFF_SCOPE
) && TREE_CODE (scope
) == FUNCTION_DECL
)
178 dump_function_decl (pp
, scope
, f
);
179 pp_cxx_colon_colon (pp
);
183 /* Dump the template ARGument under control of FLAGS. */
186 dump_template_argument (cxx_pretty_printer
*pp
, tree arg
, int flags
)
188 if (ARGUMENT_PACK_P (arg
))
189 dump_template_argument_list (pp
, ARGUMENT_PACK_ARGS (arg
),
190 /* No default args in argument packs. */
191 flags
|TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS
);
192 else if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
193 dump_type (pp
, arg
, flags
& ~TFF_CLASS_KEY_OR_ENUM
);
196 if (TREE_CODE (arg
) == TREE_LIST
)
197 arg
= TREE_VALUE (arg
);
199 dump_expr (pp
, arg
, (flags
| TFF_EXPR_IN_PARENS
) & ~TFF_CLASS_KEY_OR_ENUM
);
203 /* Count the number of template arguments ARGS whose value does not
204 match the (optional) default template parameter in PARAMS */
207 get_non_default_template_args_count (tree args
, int flags
)
209 int n
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_ARGS (args
));
211 if (/* We use this flag when generating debug information. We don't
212 want to expand templates at this point, for this may generate
213 new decls, which gets decl counts out of sync, which may in
214 turn cause codegen differences between compilations with and
216 (flags
& TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS
) != 0
217 || !flag_pretty_templates
)
220 return GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (INNERMOST_TEMPLATE_ARGS (args
));
223 /* Dump a template-argument-list ARGS (always a TREE_VEC) under control
227 dump_template_argument_list (cxx_pretty_printer
*pp
, tree args
, int flags
)
229 int n
= get_non_default_template_args_count (args
, flags
);
233 for (i
= 0; i
< n
; ++i
)
235 tree arg
= TREE_VEC_ELT (args
, i
);
237 /* Only print a comma if we know there is an argument coming. In
238 the case of an empty template argument pack, no actual
239 argument will be printed. */
241 && (!ARGUMENT_PACK_P (arg
)
242 || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) > 0))
243 pp_separate_with_comma (pp
);
245 dump_template_argument (pp
, arg
, flags
);
250 /* Dump a template parameter PARM (a TREE_LIST) under control of FLAGS. */
253 dump_template_parameter (cxx_pretty_printer
*pp
, tree parm
, int flags
)
258 if (parm
== error_mark_node
)
261 p
= TREE_VALUE (parm
);
262 a
= TREE_PURPOSE (parm
);
264 if (TREE_CODE (p
) == TYPE_DECL
)
266 if (flags
& TFF_DECL_SPECIFIERS
)
268 pp_cxx_ws_string (pp
, "class");
269 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (p
)))
270 pp_cxx_ws_string (pp
, "...");
272 pp_cxx_tree_identifier (pp
, DECL_NAME (p
));
274 else if (DECL_NAME (p
))
275 pp_cxx_tree_identifier (pp
, DECL_NAME (p
));
277 pp_cxx_canonical_template_parameter (pp
, TREE_TYPE (p
));
280 dump_decl (pp
, p
, flags
| TFF_DECL_SPECIFIERS
);
282 if ((flags
& TFF_FUNCTION_DEFAULT_ARGUMENTS
) && a
!= NULL_TREE
)
284 pp_cxx_whitespace (pp
);
286 pp_cxx_whitespace (pp
);
287 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
288 dump_type (pp
, a
, flags
& ~TFF_CHASE_TYPEDEF
);
290 dump_expr (pp
, a
, flags
| TFF_EXPR_IN_PARENS
);
294 /* Dump, under control of FLAGS, a template-parameter-list binding.
295 PARMS is a TREE_LIST of TREE_VEC of TREE_LIST and ARGS is a
299 dump_template_bindings (cxx_pretty_printer
*pp
, tree parms
, tree args
,
300 vec
<tree
, va_gc
> *typenames
)
302 bool need_semicolon
= false;
308 tree p
= TREE_VALUE (parms
);
309 int lvl
= TMPL_PARMS_DEPTH (parms
);
312 tree lvl_args
= NULL_TREE
;
314 /* Don't crash if we had an invalid argument list. */
315 if (TMPL_ARGS_DEPTH (args
) >= lvl
)
316 lvl_args
= TMPL_ARGS_LEVEL (args
, lvl
);
318 for (i
= 0; i
< TREE_VEC_LENGTH (p
); ++i
)
320 tree arg
= NULL_TREE
;
322 /* Don't crash if we had an invalid argument list. */
323 if (lvl_args
&& NUM_TMPL_ARGS (lvl_args
) > arg_idx
)
324 arg
= TREE_VEC_ELT (lvl_args
, arg_idx
);
327 pp_separate_with_semicolon (pp
);
328 dump_template_parameter (pp
, TREE_VEC_ELT (p
, i
),
329 TFF_PLAIN_IDENTIFIER
);
330 pp_cxx_whitespace (pp
);
332 pp_cxx_whitespace (pp
);
335 if (ARGUMENT_PACK_P (arg
))
336 pp_cxx_left_brace (pp
);
337 dump_template_argument (pp
, arg
, TFF_PLAIN_IDENTIFIER
);
338 if (ARGUMENT_PACK_P (arg
))
339 pp_cxx_right_brace (pp
);
342 pp_string (pp
, M_("<missing>"));
345 need_semicolon
= true;
348 parms
= TREE_CHAIN (parms
);
351 /* Don't bother with typenames for a partial instantiation. */
352 if (vec_safe_is_empty (typenames
) || uses_template_parms (args
))
355 /* Don't try to print typenames when we're processing a clone. */
356 if (current_function_decl
357 && !DECL_LANG_SPECIFIC (current_function_decl
))
360 FOR_EACH_VEC_SAFE_ELT (typenames
, i
, t
)
363 pp_separate_with_semicolon (pp
);
364 dump_type (pp
, t
, TFF_PLAIN_IDENTIFIER
);
365 pp_cxx_whitespace (pp
);
367 pp_cxx_whitespace (pp
);
368 push_deferring_access_checks (dk_no_check
);
369 t
= tsubst (t
, args
, tf_none
, NULL_TREE
);
370 pop_deferring_access_checks ();
371 /* Strip typedefs. We can't just use TFF_CHASE_TYPEDEF because
372 pp_simple_type_specifier doesn't know about it. */
373 t
= strip_typedefs (t
);
374 dump_type (pp
, t
, TFF_PLAIN_IDENTIFIER
);
378 /* Dump a human-readable equivalent of the alias template
379 specialization of T. */
382 dump_alias_template_specialization (cxx_pretty_printer
*pp
, tree t
, int flags
)
386 gcc_assert (alias_template_specialization_p (t
));
388 if (!(flags
& TFF_UNQUALIFIED_NAME
))
389 dump_scope (pp
, CP_DECL_CONTEXT (TYPE_NAME (t
)), flags
);
390 name
= TYPE_IDENTIFIER (t
);
391 pp_cxx_tree_identifier (pp
, name
);
392 dump_template_parms (pp
, TYPE_TEMPLATE_INFO (t
),
394 flags
& ~TFF_TEMPLATE_HEADER
);
397 /* Dump a human-readable equivalent of TYPE. FLAGS controls the
401 dump_type (cxx_pretty_printer
*pp
, tree t
, int flags
)
406 /* Don't print e.g. "struct mytypedef". */
407 if (TYPE_P (t
) && typedef_variant_p (t
))
409 tree decl
= TYPE_NAME (t
);
410 if ((flags
& TFF_CHASE_TYPEDEF
)
411 || DECL_SELF_REFERENCE_P (decl
)
412 || (!flag_pretty_templates
413 && DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)))
414 t
= strip_typedefs (t
);
415 else if (alias_template_specialization_p (t
))
417 dump_alias_template_specialization (pp
, t
, flags
);
420 else if (same_type_p (t
, TREE_TYPE (decl
)))
424 pp_cxx_cv_qualifier_seq (pp
, t
);
425 pp_cxx_tree_identifier (pp
, TYPE_IDENTIFIER (t
));
430 if (TYPE_PTRMEMFUNC_P (t
))
433 switch (TREE_CODE (t
))
436 if (t
== init_list_type_node
)
437 pp_string (pp
, M_("<brace-enclosed initializer list>"));
438 else if (t
== unknown_type_node
)
439 pp_string (pp
, M_("<unresolved overloaded function type>"));
442 pp_cxx_cv_qualifier_seq (pp
, t
);
443 pp_cxx_tree_identifier (pp
, TYPE_IDENTIFIER (t
));
448 /* A list of function parms. */
449 dump_parameters (pp
, t
, flags
);
452 case IDENTIFIER_NODE
:
453 pp_cxx_tree_identifier (pp
, t
);
457 dump_type (pp
, BINFO_TYPE (t
), flags
);
463 dump_aggr_type (pp
, t
, flags
);
467 if (flags
& TFF_CHASE_TYPEDEF
)
469 dump_type (pp
, DECL_ORIGINAL_TYPE (t
)
470 ? DECL_ORIGINAL_TYPE (t
) : TREE_TYPE (t
), flags
);
473 /* Else fall through. */
477 dump_decl (pp
, t
, flags
& ~TFF_DECL_SPECIFIERS
);
486 case FIXED_POINT_TYPE
:
487 pp_type_specifier_seq (pp
, t
);
490 case TEMPLATE_TEMPLATE_PARM
:
491 /* For parameters inside template signature. */
492 if (TYPE_IDENTIFIER (t
))
493 pp_cxx_tree_identifier (pp
, TYPE_IDENTIFIER (t
));
495 pp_cxx_canonical_template_parameter (pp
, t
);
498 case BOUND_TEMPLATE_TEMPLATE_PARM
:
500 tree args
= TYPE_TI_ARGS (t
);
501 pp_cxx_cv_qualifier_seq (pp
, t
);
502 pp_cxx_tree_identifier (pp
, TYPE_IDENTIFIER (t
));
503 pp_cxx_begin_template_argument_list (pp
);
504 dump_template_argument_list (pp
, args
, flags
);
505 pp_cxx_end_template_argument_list (pp
);
509 case TEMPLATE_TYPE_PARM
:
510 pp_cxx_cv_qualifier_seq (pp
, t
);
511 if (TYPE_IDENTIFIER (t
))
512 pp_cxx_tree_identifier (pp
, TYPE_IDENTIFIER (t
));
514 pp_cxx_canonical_template_parameter
515 (pp
, TEMPLATE_TYPE_PARM_INDEX (t
));
518 /* This is not always necessary for pointers and such, but doing this
519 reduces code size. */
528 dump_type_prefix (pp
, t
, flags
);
529 dump_type_suffix (pp
, t
, flags
);
533 if (! (flags
& TFF_CHASE_TYPEDEF
)
534 && DECL_ORIGINAL_TYPE (TYPE_NAME (t
)))
536 dump_decl (pp
, TYPE_NAME (t
), TFF_PLAIN_IDENTIFIER
);
539 pp_cxx_cv_qualifier_seq (pp
, t
);
540 pp_cxx_ws_string (pp
,
541 TYPENAME_IS_ENUM_P (t
) ? "enum"
542 : TYPENAME_IS_CLASS_P (t
) ? "class"
544 dump_typename (pp
, t
, flags
);
547 case UNBOUND_CLASS_TEMPLATE
:
548 if (! (flags
& TFF_UNQUALIFIED_NAME
))
550 dump_type (pp
, TYPE_CONTEXT (t
), flags
);
551 pp_cxx_colon_colon (pp
);
553 pp_cxx_ws_string (pp
, "template");
554 dump_type (pp
, TYPE_IDENTIFIER (t
), flags
);
558 pp_cxx_ws_string (pp
, "__typeof__");
559 pp_cxx_whitespace (pp
);
560 pp_cxx_left_paren (pp
);
561 dump_expr (pp
, TYPEOF_TYPE_EXPR (t
), flags
& ~TFF_EXPR_IN_PARENS
);
562 pp_cxx_right_paren (pp
);
565 case UNDERLYING_TYPE
:
566 pp_cxx_ws_string (pp
, "__underlying_type");
567 pp_cxx_whitespace (pp
);
568 pp_cxx_left_paren (pp
);
569 dump_expr (pp
, UNDERLYING_TYPE_TYPE (t
), flags
& ~TFF_EXPR_IN_PARENS
);
570 pp_cxx_right_paren (pp
);
573 case TYPE_PACK_EXPANSION
:
574 dump_type (pp
, PACK_EXPANSION_PATTERN (t
), flags
);
575 pp_cxx_ws_string (pp
, "...");
578 case TYPE_ARGUMENT_PACK
:
579 dump_template_argument (pp
, t
, flags
);
583 pp_cxx_ws_string (pp
, "decltype");
584 pp_cxx_whitespace (pp
);
585 pp_cxx_left_paren (pp
);
586 dump_expr (pp
, DECLTYPE_TYPE_EXPR (t
), flags
& ~TFF_EXPR_IN_PARENS
);
587 pp_cxx_right_paren (pp
);
591 pp_string (pp
, "std::nullptr_t");
595 pp_unsupported_tree (pp
, t
);
596 /* Fall through to error. */
599 pp_string (pp
, M_("<type error>"));
604 /* Dump a TYPENAME_TYPE. We need to notice when the context is itself
608 dump_typename (cxx_pretty_printer
*pp
, tree t
, int flags
)
610 tree ctx
= TYPE_CONTEXT (t
);
612 if (TREE_CODE (ctx
) == TYPENAME_TYPE
)
613 dump_typename (pp
, ctx
, flags
);
615 dump_type (pp
, ctx
, flags
& ~TFF_CLASS_KEY_OR_ENUM
);
616 pp_cxx_colon_colon (pp
);
617 dump_decl (pp
, TYPENAME_TYPE_FULLNAME (t
), flags
);
620 /* Return the name of the supplied aggregate, or enumeral type. */
623 class_key_or_enum_as_string (tree t
)
625 if (TREE_CODE (t
) == ENUMERAL_TYPE
)
627 if (SCOPED_ENUM_P (t
))
632 else if (TREE_CODE (t
) == UNION_TYPE
)
634 else if (TYPE_LANG_SPECIFIC (t
) && CLASSTYPE_DECLARED_CLASS (t
))
640 /* Print out a class declaration T under the control of FLAGS,
641 in the form `class foo'. */
644 dump_aggr_type (cxx_pretty_printer
*pp
, tree t
, int flags
)
647 const char *variety
= class_key_or_enum_as_string (t
);
651 pp_cxx_cv_qualifier_seq (pp
, t
);
653 if (flags
& TFF_CLASS_KEY_OR_ENUM
)
654 pp_cxx_ws_string (pp
, variety
);
656 name
= TYPE_NAME (t
);
660 typdef
= (!DECL_ARTIFICIAL (name
)
661 /* An alias specialization is not considered to be a
663 && !alias_template_specialization_p (t
));
666 && ((flags
& TFF_CHASE_TYPEDEF
)
667 || (!flag_pretty_templates
&& DECL_LANG_SPECIFIC (name
)
668 && DECL_TEMPLATE_INFO (name
))))
669 || DECL_SELF_REFERENCE_P (name
))
671 t
= TYPE_MAIN_VARIANT (t
);
672 name
= TYPE_NAME (t
);
676 tmplate
= !typdef
&& TREE_CODE (t
) != ENUMERAL_TYPE
677 && TYPE_LANG_SPECIFIC (t
) && CLASSTYPE_TEMPLATE_INFO (t
)
678 && (TREE_CODE (CLASSTYPE_TI_TEMPLATE (t
)) != TEMPLATE_DECL
679 || PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)));
681 if (! (flags
& TFF_UNQUALIFIED_NAME
))
682 dump_scope (pp
, CP_DECL_CONTEXT (name
), flags
| TFF_SCOPE
);
683 flags
&= ~TFF_UNQUALIFIED_NAME
;
686 /* Because the template names are mangled, we have to locate
687 the most general template, and use that name. */
688 tree tpl
= TYPE_TI_TEMPLATE (t
);
690 while (DECL_TEMPLATE_INFO (tpl
))
691 tpl
= DECL_TI_TEMPLATE (tpl
);
694 name
= DECL_NAME (name
);
697 if (name
== 0 || ANON_AGGRNAME_P (name
))
699 if (flags
& TFF_CLASS_KEY_OR_ENUM
)
700 pp_string (pp
, M_("<anonymous>"));
702 pp_printf (pp
, M_("<anonymous %s>"), variety
);
704 else if (LAMBDA_TYPE_P (t
))
706 /* A lambda's "type" is essentially its signature. */
707 pp_string (pp
, M_("<lambda"));
708 if (lambda_function (t
))
710 FUNCTION_FIRST_USER_PARMTYPE (lambda_function (t
)),
715 pp_cxx_tree_identifier (pp
, name
);
717 dump_template_parms (pp
, TYPE_TEMPLATE_INFO (t
),
718 !CLASSTYPE_USE_TEMPLATE (t
),
719 flags
& ~TFF_TEMPLATE_HEADER
);
722 /* Dump into the obstack the initial part of the output for a given type.
723 This is necessary when dealing with things like functions returning
726 return type of `int (* fee ())()': pointer -> function -> int. Both
727 pointer (and reference and offset) and function (and member) types must
728 deal with prefix and suffix.
730 Arrays must also do this for DECL nodes, like int a[], and for things like
734 dump_type_prefix (cxx_pretty_printer
*pp
, tree t
, int flags
)
736 if (TYPE_PTRMEMFUNC_P (t
))
738 t
= TYPE_PTRMEMFUNC_FN_TYPE (t
);
742 switch (TREE_CODE (t
))
747 tree sub
= TREE_TYPE (t
);
749 dump_type_prefix (pp
, sub
, flags
);
750 if (TREE_CODE (sub
) == ARRAY_TYPE
751 || TREE_CODE (sub
) == FUNCTION_TYPE
)
753 pp_cxx_whitespace (pp
);
754 pp_cxx_left_paren (pp
);
755 pp_c_attributes_display (pp
, TYPE_ATTRIBUTES (sub
));
759 else if (TREE_CODE (t
) == REFERENCE_TYPE
)
761 if (TYPE_REF_IS_RVALUE (t
))
762 pp_ampersand_ampersand (pp
);
766 pp
->padding
= pp_before
;
767 pp_cxx_cv_qualifier_seq (pp
, t
);
773 dump_type_prefix (pp
, TREE_TYPE (t
), flags
);
774 if (TREE_CODE (t
) == OFFSET_TYPE
) /* pmfs deal with this in d_t_p */
777 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
778 pp_cxx_left_paren (pp
);
779 dump_type (pp
, TYPE_OFFSET_BASETYPE (t
), flags
);
780 pp_cxx_colon_colon (pp
);
783 pp_cxx_cv_qualifier_seq (pp
, t
);
784 pp
->padding
= pp_before
;
787 /* This can be reached without a pointer when dealing with
788 templates, e.g. std::is_function. */
790 dump_type_prefix (pp
, TREE_TYPE (t
), flags
);
794 dump_type_prefix (pp
, TREE_TYPE (t
), flags
);
796 pp_cxx_left_paren (pp
);
797 dump_aggr_type (pp
, TYPE_METHOD_BASETYPE (t
), flags
);
798 pp_cxx_colon_colon (pp
);
802 dump_type_prefix (pp
, TREE_TYPE (t
), flags
);
806 case IDENTIFIER_NODE
:
811 case TEMPLATE_TYPE_PARM
:
812 case TEMPLATE_TEMPLATE_PARM
:
813 case BOUND_TEMPLATE_TEMPLATE_PARM
:
824 case UNDERLYING_TYPE
:
826 case TYPE_PACK_EXPANSION
:
827 case FIXED_POINT_TYPE
:
829 dump_type (pp
, t
, flags
);
830 pp
->padding
= pp_before
;
834 pp_unsupported_tree (pp
, t
);
837 pp_string (pp
, M_("<typeprefixerror>"));
842 /* Dump the suffix of type T, under control of FLAGS. This is the part
843 which appears after the identifier (or function parms). */
846 dump_type_suffix (cxx_pretty_printer
*pp
, tree t
, int flags
)
848 if (TYPE_PTRMEMFUNC_P (t
))
849 t
= TYPE_PTRMEMFUNC_FN_TYPE (t
);
851 switch (TREE_CODE (t
))
856 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
857 || TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
)
858 pp_cxx_right_paren (pp
);
859 if (TREE_CODE (t
) == POINTER_TYPE
)
860 flags
|= TFF_POINTER
;
861 dump_type_suffix (pp
, TREE_TYPE (t
), flags
);
868 if (TREE_CODE (t
) == METHOD_TYPE
)
869 /* Can only be reached through a pointer. */
870 pp_cxx_right_paren (pp
);
871 arg
= TYPE_ARG_TYPES (t
);
872 if (TREE_CODE (t
) == METHOD_TYPE
)
873 arg
= TREE_CHAIN (arg
);
875 /* Function pointers don't have default args. Not in standard C++,
876 anyway; they may in g++, but we'll just pretend otherwise. */
877 dump_parameters (pp
, arg
, flags
& ~TFF_FUNCTION_DEFAULT_ARGUMENTS
);
879 pp
->padding
= pp_before
;
880 pp_cxx_cv_qualifiers (pp
, type_memfn_quals (t
),
881 TREE_CODE (t
) == FUNCTION_TYPE
882 && (flags
& TFF_POINTER
));
883 dump_ref_qualifier (pp
, t
, flags
);
884 dump_exception_spec (pp
, TYPE_RAISES_EXCEPTIONS (t
), flags
);
885 dump_type_suffix (pp
, TREE_TYPE (t
), flags
);
891 pp_cxx_left_bracket (pp
);
894 tree dtype
= TYPE_DOMAIN (t
);
895 tree max
= TYPE_MAX_VALUE (dtype
);
896 if (integer_all_onesp (max
))
897 pp_character (pp
, '0');
898 else if (tree_fits_shwi_p (max
))
899 pp_wide_integer (pp
, tree_to_shwi (max
) + 1);
903 if (TREE_CODE (max
) == SAVE_EXPR
)
904 max
= TREE_OPERAND (max
, 0);
905 if (TREE_CODE (max
) == MINUS_EXPR
906 || TREE_CODE (max
) == PLUS_EXPR
)
908 max
= TREE_OPERAND (max
, 0);
909 while (CONVERT_EXPR_P (max
))
910 max
= TREE_OPERAND (max
, 0);
913 max
= fold_build2_loc (input_location
,
914 PLUS_EXPR
, dtype
, max
,
915 build_int_cst (dtype
, 1));
916 dump_expr (pp
, max
, flags
& ~TFF_EXPR_IN_PARENS
);
919 pp_cxx_right_bracket (pp
);
920 dump_type_suffix (pp
, TREE_TYPE (t
), flags
);
924 case IDENTIFIER_NODE
:
929 case TEMPLATE_TYPE_PARM
:
930 case TEMPLATE_TEMPLATE_PARM
:
931 case BOUND_TEMPLATE_TEMPLATE_PARM
:
942 case UNDERLYING_TYPE
:
944 case TYPE_PACK_EXPANSION
:
945 case FIXED_POINT_TYPE
:
950 pp_unsupported_tree (pp
, t
);
952 /* Don't mark it here, we should have already done in
959 dump_global_iord (cxx_pretty_printer
*pp
, tree t
)
961 const char *p
= NULL
;
963 if (DECL_GLOBAL_CTOR_P (t
))
964 p
= M_("(static initializers for %s)");
965 else if (DECL_GLOBAL_DTOR_P (t
))
966 p
= M_("(static destructors for %s)");
970 pp_printf (pp
, p
, LOCATION_FILE (input_location
));
974 dump_simple_decl (cxx_pretty_printer
*pp
, tree t
, tree type
, int flags
)
976 if (flags
& TFF_DECL_SPECIFIERS
)
979 && DECL_DECLARED_CONSTEXPR_P (t
))
980 pp_cxx_ws_string (pp
, "constexpr");
981 dump_type_prefix (pp
, type
, flags
& ~TFF_UNQUALIFIED_NAME
);
984 if (! (flags
& TFF_UNQUALIFIED_NAME
)
985 && TREE_CODE (t
) != PARM_DECL
986 && (!DECL_INITIAL (t
)
987 || TREE_CODE (DECL_INITIAL (t
)) != TEMPLATE_PARM_INDEX
))
988 dump_scope (pp
, CP_DECL_CONTEXT (t
), flags
);
989 flags
&= ~TFF_UNQUALIFIED_NAME
;
990 if ((flags
& TFF_DECL_SPECIFIERS
)
991 && DECL_TEMPLATE_PARM_P (t
)
992 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (t
)))
993 pp_string (pp
, "...");
996 if (TREE_CODE (t
) == FIELD_DECL
&& DECL_NORMAL_CAPTURE_P (t
))
999 pp_string (pp
, IDENTIFIER_POINTER (DECL_NAME (t
)) + 2);
1000 pp_string (pp
, " capture>");
1003 dump_decl (pp
, DECL_NAME (t
), flags
);
1006 pp_string (pp
, M_("<anonymous>"));
1007 if (flags
& TFF_DECL_SPECIFIERS
)
1008 dump_type_suffix (pp
, type
, flags
);
1011 /* Dump a human readable string for the decl T under control of FLAGS. */
1014 dump_decl (cxx_pretty_printer
*pp
, tree t
, int flags
)
1019 /* If doing Objective-C++, give Objective-C a chance to demangle
1020 Objective-C method names. */
1021 if (c_dialect_objc ())
1023 const char *demangled
= objc_maybe_printable_name (t
, flags
);
1026 pp_string (pp
, demangled
);
1031 switch (TREE_CODE (t
))
1034 /* Don't say 'typedef class A' */
1035 if (DECL_ARTIFICIAL (t
) && !DECL_SELF_REFERENCE_P (t
))
1037 if ((flags
& TFF_DECL_SPECIFIERS
)
1038 && TREE_CODE (TREE_TYPE (t
)) == TEMPLATE_TYPE_PARM
)
1040 /* Say `class T' not just `T'. */
1041 pp_cxx_ws_string (pp
, "class");
1043 /* Emit the `...' for a parameter pack. */
1044 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t
)))
1045 pp_cxx_ws_string (pp
, "...");
1048 dump_type (pp
, TREE_TYPE (t
), flags
);
1051 if (TYPE_DECL_ALIAS_P (t
)
1052 && (flags
& TFF_DECL_SPECIFIERS
1053 || flags
& TFF_CLASS_KEY_OR_ENUM
))
1055 pp_cxx_ws_string (pp
, "using");
1056 dump_decl (pp
, DECL_NAME (t
), flags
);
1057 pp_cxx_whitespace (pp
);
1058 pp_cxx_ws_string (pp
, "=");
1059 pp_cxx_whitespace (pp
);
1060 dump_type (pp
, DECL_ORIGINAL_TYPE (t
), flags
);
1063 if ((flags
& TFF_DECL_SPECIFIERS
)
1064 && !DECL_SELF_REFERENCE_P (t
))
1065 pp_cxx_ws_string (pp
, "typedef");
1066 dump_simple_decl (pp
, t
, DECL_ORIGINAL_TYPE (t
)
1067 ? DECL_ORIGINAL_TYPE (t
) : TREE_TYPE (t
),
1072 if (DECL_NAME (t
) && VTABLE_NAME_P (DECL_NAME (t
)))
1074 pp_string (pp
, M_("vtable for "));
1075 gcc_assert (TYPE_P (DECL_CONTEXT (t
)));
1076 dump_type (pp
, DECL_CONTEXT (t
), flags
);
1079 /* Else fall through. */
1082 dump_simple_decl (pp
, t
, TREE_TYPE (t
), flags
);
1084 /* Handle variable template specializations. */
1085 if (TREE_CODE (t
) == VAR_DECL
1086 && DECL_LANG_SPECIFIC (t
)
1087 && DECL_TEMPLATE_INFO (t
)
1088 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)))
1090 pp_cxx_begin_template_argument_list (pp
);
1091 tree args
= INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t
));
1092 dump_template_argument_list (pp
, args
, flags
);
1093 pp_cxx_end_template_argument_list (pp
);
1098 pp_string (pp
, M_("<return value> "));
1099 dump_simple_decl (pp
, t
, TREE_TYPE (t
), flags
);
1102 case NAMESPACE_DECL
:
1103 if (flags
& TFF_DECL_SPECIFIERS
)
1104 pp
->declaration (t
);
1107 if (! (flags
& TFF_UNQUALIFIED_NAME
))
1108 dump_scope (pp
, CP_DECL_CONTEXT (t
), flags
);
1109 flags
&= ~TFF_UNQUALIFIED_NAME
;
1110 if (DECL_NAME (t
) == NULL_TREE
)
1112 if (!(pp
->flags
& pp_c_flag_gnu_v3
))
1113 pp_cxx_ws_string (pp
, M_("{anonymous}"));
1115 pp_cxx_ws_string (pp
, M_("(anonymous namespace)"));
1118 pp_cxx_tree_identifier (pp
, DECL_NAME (t
));
1123 dump_type (pp
, TREE_OPERAND (t
, 0), flags
);
1124 pp_colon_colon (pp
);
1125 dump_decl (pp
, TREE_OPERAND (t
, 1), TFF_UNQUALIFIED_NAME
);
1129 dump_decl (pp
, TREE_OPERAND (t
, 0), flags
);
1130 pp_cxx_left_bracket (pp
);
1131 dump_decl (pp
, TREE_OPERAND (t
, 1), flags
);
1132 pp_cxx_right_bracket (pp
);
1135 case ARRAY_NOTATION_REF
:
1136 dump_decl (pp
, ARRAY_NOTATION_ARRAY (t
), flags
| TFF_EXPR_IN_PARENS
);
1137 pp_cxx_left_bracket (pp
);
1138 dump_decl (pp
, ARRAY_NOTATION_START (t
), flags
| TFF_EXPR_IN_PARENS
);
1140 dump_decl (pp
, ARRAY_NOTATION_LENGTH (t
), flags
| TFF_EXPR_IN_PARENS
);
1142 dump_decl (pp
, ARRAY_NOTATION_STRIDE (t
), flags
| TFF_EXPR_IN_PARENS
);
1143 pp_cxx_right_bracket (pp
);
1146 /* So that we can do dump_decl on an aggr type. */
1150 dump_type (pp
, t
, flags
);
1154 /* This is a pseudo destructor call which has not been folded into
1155 a PSEUDO_DTOR_EXPR yet. */
1156 pp_cxx_complement (pp
);
1157 dump_type (pp
, TREE_OPERAND (t
, 0), flags
);
1164 /* These special cases are duplicated here so that other functions
1165 can feed identifiers to error and get them demangled properly. */
1166 case IDENTIFIER_NODE
:
1167 if (IDENTIFIER_TYPENAME_P (t
))
1169 pp_cxx_ws_string (pp
, "operator");
1170 /* Not exactly IDENTIFIER_TYPE_VALUE. */
1171 dump_type (pp
, TREE_TYPE (t
), flags
);
1175 pp_cxx_tree_identifier (pp
, t
);
1181 t
= OVL_CURRENT (t
);
1182 if (DECL_CLASS_SCOPE_P (t
))
1184 dump_type (pp
, DECL_CONTEXT (t
), flags
);
1185 pp_cxx_colon_colon (pp
);
1187 else if (!DECL_FILE_SCOPE_P (t
))
1189 dump_decl (pp
, DECL_CONTEXT (t
), flags
);
1190 pp_cxx_colon_colon (pp
);
1192 dump_decl (pp
, DECL_NAME (t
), flags
);
1196 /* If there's only one function, just treat it like an ordinary
1198 t
= OVL_CURRENT (t
);
1202 if (! DECL_LANG_SPECIFIC (t
))
1204 if (DECL_ABSTRACT_ORIGIN (t
))
1205 dump_decl (pp
, DECL_ABSTRACT_ORIGIN (t
), flags
);
1207 pp_string (pp
, M_("<built-in>"));
1209 else if (DECL_GLOBAL_CTOR_P (t
) || DECL_GLOBAL_DTOR_P (t
))
1210 dump_global_iord (pp
, t
);
1212 dump_function_decl (pp
, t
, flags
);
1216 dump_template_decl (pp
, t
, flags
);
1219 case TEMPLATE_ID_EXPR
:
1221 tree name
= TREE_OPERAND (t
, 0);
1222 tree args
= TREE_OPERAND (t
, 1);
1224 if (is_overloaded_fn (name
))
1225 name
= get_first_fn (name
);
1227 name
= DECL_NAME (name
);
1228 dump_decl (pp
, name
, flags
);
1229 pp_cxx_begin_template_argument_list (pp
);
1230 if (args
== error_mark_node
)
1231 pp_string (pp
, M_("<template arguments error>"));
1233 dump_template_argument_list
1234 (pp
, args
, flags
|TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS
);
1235 pp_cxx_end_template_argument_list (pp
);
1240 pp_cxx_tree_identifier (pp
, DECL_NAME (t
));
1244 if ((TREE_TYPE (t
) != NULL_TREE
&& NEXT_CODE (t
) == ENUMERAL_TYPE
)
1245 || (DECL_INITIAL (t
) &&
1246 TREE_CODE (DECL_INITIAL (t
)) == TEMPLATE_PARM_INDEX
))
1247 dump_simple_decl (pp
, t
, TREE_TYPE (t
), flags
);
1248 else if (DECL_NAME (t
))
1249 dump_decl (pp
, DECL_NAME (t
), flags
);
1250 else if (DECL_INITIAL (t
))
1251 dump_expr (pp
, DECL_INITIAL (t
), flags
| TFF_EXPR_IN_PARENS
);
1253 pp_string (pp
, M_("<enumerator>"));
1257 pp_cxx_ws_string (pp
, "using");
1258 dump_type (pp
, USING_DECL_SCOPE (t
), flags
);
1259 pp_cxx_colon_colon (pp
);
1260 dump_decl (pp
, DECL_NAME (t
), flags
);
1264 pp
->declaration (t
);
1268 dump_decl (pp
, BASELINK_FUNCTIONS (t
), flags
);
1271 case NON_DEPENDENT_EXPR
:
1272 dump_expr (pp
, t
, flags
);
1275 case TEMPLATE_TYPE_PARM
:
1276 if (flags
& TFF_DECL_SPECIFIERS
)
1277 pp
->declaration (t
);
1282 case UNBOUND_CLASS_TEMPLATE
:
1283 case TYPE_PACK_EXPANSION
:
1285 dump_type (pp
, t
, flags
);
1289 pp_unsupported_tree (pp
, t
);
1290 /* Fall through to error. */
1293 pp_string (pp
, M_("<declaration error>"));
1298 /* Dump a template declaration T under control of FLAGS. This means the
1299 'template <...> leaders plus the 'class X' or 'void fn(...)' part. */
1302 dump_template_decl (cxx_pretty_printer
*pp
, tree t
, int flags
)
1304 tree orig_parms
= DECL_TEMPLATE_PARMS (t
);
1308 if (flags
& TFF_TEMPLATE_HEADER
)
1310 for (parms
= orig_parms
= nreverse (orig_parms
);
1312 parms
= TREE_CHAIN (parms
))
1314 tree inner_parms
= INNERMOST_TEMPLATE_PARMS (parms
);
1315 int len
= TREE_VEC_LENGTH (inner_parms
);
1317 pp_cxx_ws_string (pp
, "template");
1318 pp_cxx_begin_template_argument_list (pp
);
1320 /* If we've shown the template prefix, we'd better show the
1321 parameters' and decl's type too. */
1322 flags
|= TFF_DECL_SPECIFIERS
;
1324 for (i
= 0; i
< len
; i
++)
1327 pp_separate_with_comma (pp
);
1328 dump_template_parameter (pp
, TREE_VEC_ELT (inner_parms
, i
),
1331 pp_cxx_end_template_argument_list (pp
);
1332 pp_cxx_whitespace (pp
);
1334 nreverse(orig_parms
);
1336 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
1338 /* Say `template<arg> class TT' not just `template<arg> TT'. */
1339 pp_cxx_ws_string (pp
, "class");
1341 /* If this is a parameter pack, print the ellipsis. */
1342 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t
)))
1343 pp_cxx_ws_string (pp
, "...");
1347 if (DECL_CLASS_TEMPLATE_P (t
))
1348 dump_type (pp
, TREE_TYPE (t
),
1349 ((flags
& ~TFF_CLASS_KEY_OR_ENUM
) | TFF_TEMPLATE_NAME
1350 | (flags
& TFF_DECL_SPECIFIERS
? TFF_CLASS_KEY_OR_ENUM
: 0)));
1351 else if (DECL_TEMPLATE_RESULT (t
)
1352 && (VAR_P (DECL_TEMPLATE_RESULT (t
))
1353 /* Alias template. */
1354 || DECL_TYPE_TEMPLATE_P (t
)))
1355 dump_decl (pp
, DECL_TEMPLATE_RESULT (t
), flags
| TFF_TEMPLATE_NAME
);
1358 gcc_assert (TREE_TYPE (t
));
1359 switch (NEXT_CODE (t
))
1363 dump_function_decl (pp
, t
, flags
| TFF_TEMPLATE_NAME
);
1366 /* This case can occur with some invalid code. */
1367 dump_type (pp
, TREE_TYPE (t
),
1368 (flags
& ~TFF_CLASS_KEY_OR_ENUM
) | TFF_TEMPLATE_NAME
1369 | (flags
& TFF_DECL_SPECIFIERS
1370 ? TFF_CLASS_KEY_OR_ENUM
: 0));
1375 /* find_typenames looks through the type of the function template T
1376 and returns a vec containing any typedefs, decltypes or TYPENAME_TYPEs
1379 struct find_typenames_t
1381 hash_set
<tree
> *p_set
;
1382 vec
<tree
, va_gc
> *typenames
;
1386 find_typenames_r (tree
*tp
, int *walk_subtrees
, void *data
)
1388 struct find_typenames_t
*d
= (struct find_typenames_t
*)data
;
1389 tree mv
= NULL_TREE
;
1391 if (TYPE_P (*tp
) && is_typedef_decl (TYPE_NAME (*tp
)))
1392 /* Add the type of the typedef without any additional cv-quals. */
1393 mv
= TREE_TYPE (TYPE_NAME (*tp
));
1394 else if (TREE_CODE (*tp
) == TYPENAME_TYPE
1395 || TREE_CODE (*tp
) == DECLTYPE_TYPE
)
1396 /* Add the typename without any cv-qualifiers. */
1397 mv
= TYPE_MAIN_VARIANT (*tp
);
1399 if (TREE_CODE (*tp
) == TYPE_PACK_EXPANSION
)
1401 /* Don't mess with parameter packs since we don't remember
1402 the pack expansion context for a particular typename. */
1403 *walk_subtrees
= false;
1407 if (mv
&& (mv
== *tp
|| !d
->p_set
->add (mv
)))
1408 vec_safe_push (d
->typenames
, mv
);
1410 /* Search into class template arguments, which cp_walk_subtrees
1412 if (CLASS_TYPE_P (*tp
) && CLASSTYPE_TEMPLATE_INFO (*tp
))
1413 cp_walk_tree (&CLASSTYPE_TI_ARGS (*tp
), find_typenames_r
,
1419 static vec
<tree
, va_gc
> *
1420 find_typenames (tree t
)
1422 struct find_typenames_t ft
;
1423 ft
.p_set
= new hash_set
<tree
>;
1424 ft
.typenames
= NULL
;
1425 cp_walk_tree (&TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
1426 find_typenames_r
, &ft
, ft
.p_set
);
1428 return ft
.typenames
;
1431 /* Output the "[with ...]" clause for a template instantiation T iff
1432 TEMPLATE_PARMS, TEMPLATE_ARGS and FLAGS are suitable. T may be NULL if
1433 formatting a deduction/substitution diagnostic rather than an
1437 dump_substitution (cxx_pretty_printer
*pp
,
1438 tree t
, tree template_parms
, tree template_args
,
1441 if (template_parms
!= NULL_TREE
&& template_args
!= NULL_TREE
1442 && !(flags
& TFF_NO_TEMPLATE_BINDINGS
))
1444 vec
<tree
, va_gc
> *typenames
= t
? find_typenames (t
) : NULL
;
1445 pp_cxx_whitespace (pp
);
1446 pp_cxx_left_bracket (pp
);
1447 pp
->translate_string ("with");
1448 pp_cxx_whitespace (pp
);
1449 dump_template_bindings (pp
, template_parms
, template_args
, typenames
);
1450 pp_cxx_right_bracket (pp
);
1454 /* Dump the lambda function FN including its 'mutable' qualifier and any
1455 template bindings. */
1458 dump_lambda_function (cxx_pretty_printer
*pp
,
1459 tree fn
, tree template_parms
, tree template_args
,
1462 /* A lambda's signature is essentially its "type". */
1463 dump_type (pp
, DECL_CONTEXT (fn
), flags
);
1464 if (!(TYPE_QUALS (class_of_this_parm (TREE_TYPE (fn
))) & TYPE_QUAL_CONST
))
1466 pp
->padding
= pp_before
;
1467 pp_c_ws_string (pp
, "mutable");
1469 dump_substitution (pp
, fn
, template_parms
, template_args
, flags
);
1472 /* Pretty print a function decl. There are several ways we want to print a
1473 function declaration. The TFF_ bits in FLAGS tells us how to behave.
1474 As error can only apply the '#' flag once to give 0 and 1 for V, there
1475 is %D which doesn't print the throw specs, and %F which does. */
1478 dump_function_decl (cxx_pretty_printer
*pp
, tree t
, int flags
)
1482 tree cname
= NULL_TREE
;
1483 tree template_args
= NULL_TREE
;
1484 tree template_parms
= NULL_TREE
;
1485 int show_return
= flags
& TFF_RETURN_TYPE
|| flags
& TFF_DECL_SPECIFIERS
;
1486 int do_outer_scope
= ! (flags
& TFF_UNQUALIFIED_NAME
);
1489 flags
&= ~(TFF_UNQUALIFIED_NAME
| TFF_TEMPLATE_NAME
);
1490 if (TREE_CODE (t
) == TEMPLATE_DECL
)
1491 t
= DECL_TEMPLATE_RESULT (t
);
1493 /* Save the exceptions, in case t is a specialization and we are
1494 emitting an error about incompatible specifications. */
1495 exceptions
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (t
));
1497 /* Pretty print template instantiations only. */
1498 if (DECL_USE_TEMPLATE (t
) && DECL_TEMPLATE_INFO (t
)
1499 && flag_pretty_templates
)
1503 template_args
= DECL_TI_ARGS (t
);
1504 tmpl
= most_general_template (t
);
1505 if (tmpl
&& TREE_CODE (tmpl
) == TEMPLATE_DECL
)
1507 template_parms
= DECL_TEMPLATE_PARMS (tmpl
);
1512 if (DECL_NAME (t
) && LAMBDA_FUNCTION_P (t
))
1513 return dump_lambda_function (pp
, t
, template_parms
, template_args
, flags
);
1515 fntype
= TREE_TYPE (t
);
1516 parmtypes
= FUNCTION_FIRST_USER_PARMTYPE (t
);
1518 if (DECL_CLASS_SCOPE_P (t
))
1519 cname
= DECL_CONTEXT (t
);
1520 /* This is for partially instantiated template methods. */
1521 else if (TREE_CODE (fntype
) == METHOD_TYPE
)
1522 cname
= TREE_TYPE (TREE_VALUE (parmtypes
));
1524 if (flags
& TFF_DECL_SPECIFIERS
)
1526 if (DECL_STATIC_FUNCTION_P (t
))
1527 pp_cxx_ws_string (pp
, "static");
1528 else if (DECL_VIRTUAL_P (t
))
1529 pp_cxx_ws_string (pp
, "virtual");
1531 if (DECL_DECLARED_CONSTEXPR_P (t
))
1532 pp_cxx_ws_string (pp
, "constexpr");
1535 /* Print the return type? */
1537 show_return
= !DECL_CONV_FN_P (t
) && !DECL_CONSTRUCTOR_P (t
)
1538 && !DECL_DESTRUCTOR_P (t
);
1541 tree ret
= fndecl_declared_return_type (t
);
1542 dump_type_prefix (pp
, ret
, flags
);
1545 /* Print the function name. */
1546 if (!do_outer_scope
)
1550 dump_type (pp
, cname
, flags
);
1551 pp_cxx_colon_colon (pp
);
1554 dump_scope (pp
, CP_DECL_CONTEXT (t
), flags
);
1556 dump_function_name (pp
, t
, flags
);
1558 if (!(flags
& TFF_NO_FUNCTION_ARGUMENTS
))
1560 dump_parameters (pp
, parmtypes
, flags
);
1562 if (TREE_CODE (fntype
) == METHOD_TYPE
)
1564 pp
->padding
= pp_before
;
1565 pp_cxx_cv_qualifier_seq (pp
, class_of_this_parm (fntype
));
1566 dump_ref_qualifier (pp
, fntype
, flags
);
1569 if (flags
& TFF_EXCEPTION_SPECIFICATION
)
1571 pp
->padding
= pp_before
;
1572 dump_exception_spec (pp
, exceptions
, flags
);
1576 dump_type_suffix (pp
, TREE_TYPE (fntype
), flags
);
1578 dump_substitution (pp
, t
, template_parms
, template_args
, flags
);
1580 else if (template_args
)
1582 bool need_comma
= false;
1584 pp_cxx_begin_template_argument_list (pp
);
1585 template_args
= INNERMOST_TEMPLATE_ARGS (template_args
);
1586 for (i
= 0; i
< TREE_VEC_LENGTH (template_args
); ++i
)
1588 tree arg
= TREE_VEC_ELT (template_args
, i
);
1590 pp_separate_with_comma (pp
);
1591 if (ARGUMENT_PACK_P (arg
))
1592 pp_cxx_left_brace (pp
);
1593 dump_template_argument (pp
, arg
, TFF_PLAIN_IDENTIFIER
);
1594 if (ARGUMENT_PACK_P (arg
))
1595 pp_cxx_right_brace (pp
);
1598 pp_cxx_end_template_argument_list (pp
);
1602 /* Print a parameter list. If this is for a member function, the
1603 member object ptr (and any other hidden args) should have
1604 already been removed. */
1607 dump_parameters (cxx_pretty_printer
*pp
, tree parmtypes
, int flags
)
1610 flags
&= ~TFF_SCOPE
;
1611 pp_cxx_left_paren (pp
);
1613 for (first
= 1; parmtypes
!= void_list_node
;
1614 parmtypes
= TREE_CHAIN (parmtypes
))
1617 pp_separate_with_comma (pp
);
1621 pp_cxx_ws_string (pp
, "...");
1625 dump_type (pp
, TREE_VALUE (parmtypes
), flags
);
1627 if ((flags
& TFF_FUNCTION_DEFAULT_ARGUMENTS
) && TREE_PURPOSE (parmtypes
))
1629 pp_cxx_whitespace (pp
);
1631 pp_cxx_whitespace (pp
);
1632 dump_expr (pp
, TREE_PURPOSE (parmtypes
), flags
| TFF_EXPR_IN_PARENS
);
1636 pp_cxx_right_paren (pp
);
1639 /* Print ref-qualifier of a FUNCTION_TYPE or METHOD_TYPE. FLAGS are ignored. */
1642 dump_ref_qualifier (cxx_pretty_printer
*pp
, tree t
, int flags ATTRIBUTE_UNUSED
)
1644 if (FUNCTION_REF_QUALIFIED (t
))
1646 pp
->padding
= pp_before
;
1647 if (FUNCTION_RVALUE_QUALIFIED (t
))
1648 pp_cxx_ws_string (pp
, "&&");
1650 pp_cxx_ws_string (pp
, "&");
1654 /* Print an exception specification. T is the exception specification. */
1657 dump_exception_spec (cxx_pretty_printer
*pp
, tree t
, int flags
)
1659 if (t
&& TREE_PURPOSE (t
))
1661 pp_cxx_ws_string (pp
, "noexcept");
1662 if (!integer_onep (TREE_PURPOSE (t
)))
1664 pp_cxx_whitespace (pp
);
1665 pp_cxx_left_paren (pp
);
1666 if (DEFERRED_NOEXCEPT_SPEC_P (t
))
1667 pp_cxx_ws_string (pp
, "<uninstantiated>");
1669 dump_expr (pp
, TREE_PURPOSE (t
), flags
);
1670 pp_cxx_right_paren (pp
);
1675 pp_cxx_ws_string (pp
, "throw");
1676 pp_cxx_whitespace (pp
);
1677 pp_cxx_left_paren (pp
);
1678 if (TREE_VALUE (t
) != NULL_TREE
)
1681 dump_type (pp
, TREE_VALUE (t
), flags
);
1685 pp_separate_with_comma (pp
);
1687 pp_cxx_right_paren (pp
);
1691 /* Handle the function name for a FUNCTION_DECL node, grokking operators
1692 and destructors properly. */
1695 dump_function_name (cxx_pretty_printer
*pp
, tree t
, int flags
)
1697 tree name
= DECL_NAME (t
);
1699 /* We can get here with a decl that was synthesized by language-
1700 independent machinery (e.g. coverage.c) in which case it won't
1701 have a lang_specific structure attached and DECL_CONSTRUCTOR_P
1702 will crash. In this case it is safe just to print out the
1704 if (!DECL_LANG_SPECIFIC (t
))
1706 pp_cxx_tree_identifier (pp
, name
);
1710 if (TREE_CODE (t
) == TEMPLATE_DECL
)
1711 t
= DECL_TEMPLATE_RESULT (t
);
1713 /* Don't let the user see __comp_ctor et al. */
1714 if (DECL_CONSTRUCTOR_P (t
)
1715 || DECL_DESTRUCTOR_P (t
))
1717 if (LAMBDA_TYPE_P (DECL_CONTEXT (t
)))
1718 name
= get_identifier ("<lambda>");
1719 else if (TYPE_ANONYMOUS_P (DECL_CONTEXT (t
)))
1720 name
= get_identifier ("<constructor>");
1722 name
= constructor_name (DECL_CONTEXT (t
));
1725 if (DECL_DESTRUCTOR_P (t
))
1727 pp_cxx_complement (pp
);
1728 dump_decl (pp
, name
, TFF_PLAIN_IDENTIFIER
);
1730 else if (DECL_CONV_FN_P (t
))
1732 /* This cannot use the hack that the operator's return
1733 type is stashed off of its name because it may be
1734 used for error reporting. In the case of conflicting
1735 declarations, both will have the same name, yet
1736 the types will be different, hence the TREE_TYPE field
1737 of the first name will be clobbered by the second. */
1738 pp_cxx_ws_string (pp
, "operator");
1739 dump_type (pp
, TREE_TYPE (TREE_TYPE (t
)), flags
);
1741 else if (name
&& IDENTIFIER_OPNAME_P (name
))
1742 pp_cxx_tree_identifier (pp
, name
);
1743 else if (name
&& UDLIT_OPER_P (name
))
1744 pp_cxx_tree_identifier (pp
, name
);
1746 dump_decl (pp
, name
, flags
);
1748 if (DECL_TEMPLATE_INFO (t
)
1749 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (t
)
1750 && (TREE_CODE (DECL_TI_TEMPLATE (t
)) != TEMPLATE_DECL
1751 || PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
1752 dump_template_parms (pp
, DECL_TEMPLATE_INFO (t
), !DECL_USE_TEMPLATE (t
),
1756 /* Dump the template parameters from the template info INFO under control of
1757 FLAGS. PRIMARY indicates whether this is a primary template decl, or
1758 specialization (partial or complete). For partial specializations we show
1759 the specialized parameter values. For a primary template we show no
1763 dump_template_parms (cxx_pretty_printer
*pp
, tree info
,
1764 int primary
, int flags
)
1766 tree args
= info
? TI_ARGS (info
) : NULL_TREE
;
1768 if (primary
&& flags
& TFF_TEMPLATE_NAME
)
1770 flags
&= ~(TFF_CLASS_KEY_OR_ENUM
| TFF_TEMPLATE_NAME
);
1771 pp_cxx_begin_template_argument_list (pp
);
1773 /* Be careful only to print things when we have them, so as not
1774 to crash producing error messages. */
1775 if (args
&& !primary
)
1778 len
= get_non_default_template_args_count (args
, flags
);
1780 args
= INNERMOST_TEMPLATE_ARGS (args
);
1781 for (ix
= 0; ix
!= len
; ix
++)
1783 tree arg
= TREE_VEC_ELT (args
, ix
);
1785 /* Only print a comma if we know there is an argument coming. In
1786 the case of an empty template argument pack, no actual
1787 argument will be printed. */
1789 && (!ARGUMENT_PACK_P (arg
)
1790 || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) > 0))
1791 pp_separate_with_comma (pp
);
1794 pp_string (pp
, M_("<template parameter error>"));
1796 dump_template_argument (pp
, arg
, flags
);
1801 tree tpl
= TI_TEMPLATE (info
);
1802 tree parms
= DECL_TEMPLATE_PARMS (tpl
);
1805 parms
= TREE_CODE (parms
) == TREE_LIST
? TREE_VALUE (parms
) : NULL_TREE
;
1806 len
= parms
? TREE_VEC_LENGTH (parms
) : 0;
1808 for (ix
= 0; ix
!= len
; ix
++)
1812 if (TREE_VEC_ELT (parms
, ix
) == error_mark_node
)
1814 pp_string (pp
, M_("<template parameter error>"));
1818 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, ix
));
1821 pp_separate_with_comma (pp
);
1823 dump_decl (pp
, parm
, flags
& ~TFF_DECL_SPECIFIERS
);
1826 pp_cxx_end_template_argument_list (pp
);
1829 /* Print out the arguments of CALL_EXPR T as a parenthesized list using
1830 flags FLAGS. Skip over the first argument if SKIPFIRST is true. */
1833 dump_call_expr_args (cxx_pretty_printer
*pp
, tree t
, int flags
, bool skipfirst
)
1836 call_expr_arg_iterator iter
;
1838 pp_cxx_left_paren (pp
);
1839 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, t
)
1845 dump_expr (pp
, arg
, flags
| TFF_EXPR_IN_PARENS
);
1846 if (more_call_expr_args_p (&iter
))
1847 pp_separate_with_comma (pp
);
1850 pp_cxx_right_paren (pp
);
1853 /* Print out the arguments of AGGR_INIT_EXPR T as a parenthesized list
1854 using flags FLAGS. Skip over the first argument if SKIPFIRST is
1858 dump_aggr_init_expr_args (cxx_pretty_printer
*pp
, tree t
, int flags
,
1862 aggr_init_expr_arg_iterator iter
;
1864 pp_cxx_left_paren (pp
);
1865 FOR_EACH_AGGR_INIT_EXPR_ARG (arg
, iter
, t
)
1871 dump_expr (pp
, arg
, flags
| TFF_EXPR_IN_PARENS
);
1872 if (more_aggr_init_expr_args_p (&iter
))
1873 pp_separate_with_comma (pp
);
1876 pp_cxx_right_paren (pp
);
1879 /* Print out a list of initializers (subr of dump_expr). */
1882 dump_expr_list (cxx_pretty_printer
*pp
, tree l
, int flags
)
1886 dump_expr (pp
, TREE_VALUE (l
), flags
| TFF_EXPR_IN_PARENS
);
1889 pp_separate_with_comma (pp
);
1893 /* Print out a vector of initializers (subr of dump_expr). */
1896 dump_expr_init_vec (cxx_pretty_printer
*pp
, vec
<constructor_elt
, va_gc
> *v
,
1899 unsigned HOST_WIDE_INT idx
;
1902 FOR_EACH_CONSTRUCTOR_VALUE (v
, idx
, value
)
1904 dump_expr (pp
, value
, flags
| TFF_EXPR_IN_PARENS
);
1905 if (idx
!= v
->length () - 1)
1906 pp_separate_with_comma (pp
);
1911 /* We've gotten an indirect REFERENCE (an OBJ_TYPE_REF) to a virtual
1912 function. Resolve it to a close relative -- in the sense of static
1913 type -- variant being overridden. That is close to what was written in
1914 the source code. Subroutine of dump_expr. */
1917 resolve_virtual_fun_from_obj_type_ref (tree ref
)
1919 tree obj_type
= TREE_TYPE (OBJ_TYPE_REF_OBJECT (ref
));
1920 HOST_WIDE_INT index
= tree_to_uhwi (OBJ_TYPE_REF_TOKEN (ref
));
1921 tree fun
= BINFO_VIRTUALS (TYPE_BINFO (TREE_TYPE (obj_type
)));
1924 fun
= TREE_CHAIN (fun
);
1925 index
-= (TARGET_VTABLE_USES_DESCRIPTORS
1926 ? TARGET_VTABLE_USES_DESCRIPTORS
: 1);
1932 /* Print out an expression E under control of FLAGS. */
1935 dump_expr (cxx_pretty_printer
*pp
, tree t
, int flags
)
1942 if (STATEMENT_CLASS_P (t
))
1944 pp_cxx_ws_string (pp
, M_("<statement>"));
1948 switch (TREE_CODE (t
))
1956 case NAMESPACE_DECL
:
1960 case IDENTIFIER_NODE
:
1961 dump_decl (pp
, t
, ((flags
& ~(TFF_DECL_SPECIFIERS
|TFF_RETURN_TYPE
1962 |TFF_TEMPLATE_HEADER
))
1963 | TFF_NO_FUNCTION_ARGUMENTS
));
1967 if (SSA_NAME_VAR (t
)
1968 && !DECL_ARTIFICIAL (SSA_NAME_VAR (t
)))
1969 dump_expr (pp
, SSA_NAME_VAR (t
), flags
);
1971 pp_cxx_ws_string (pp
, M_("<unknown>"));
1982 case USERDEF_LITERAL
:
1983 pp_cxx_userdef_literal (pp
, t
);
1987 /* While waiting for caret diagnostics, avoid printing
1988 __cxa_allocate_exception, __cxa_throw, and the like. */
1989 pp_cxx_ws_string (pp
, M_("<throw-expression>"));
1994 dump_type (pp
, PTRMEM_CST_CLASS (t
), flags
);
1995 pp_cxx_colon_colon (pp
);
1996 pp_cxx_tree_identifier (pp
, DECL_NAME (PTRMEM_CST_MEMBER (t
)));
2000 pp_cxx_left_paren (pp
);
2001 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
| TFF_EXPR_IN_PARENS
);
2002 pp_separate_with_comma (pp
);
2003 dump_expr (pp
, TREE_OPERAND (t
, 1), flags
| TFF_EXPR_IN_PARENS
);
2004 pp_cxx_right_paren (pp
);
2008 pp_cxx_left_paren (pp
);
2009 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
| TFF_EXPR_IN_PARENS
);
2010 pp_string (pp
, " ? ");
2011 dump_expr (pp
, TREE_OPERAND (t
, 1), flags
| TFF_EXPR_IN_PARENS
);
2012 pp_string (pp
, " : ");
2013 dump_expr (pp
, TREE_OPERAND (t
, 2), flags
| TFF_EXPR_IN_PARENS
);
2014 pp_cxx_right_paren (pp
);
2018 if (TREE_HAS_CONSTRUCTOR (t
))
2020 pp_cxx_ws_string (pp
, "new");
2021 pp_cxx_whitespace (pp
);
2022 dump_type (pp
, TREE_TYPE (TREE_TYPE (t
)), flags
);
2025 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
| TFF_EXPR_IN_PARENS
);
2028 case AGGR_INIT_EXPR
:
2030 tree fn
= NULL_TREE
;
2032 if (TREE_CODE (AGGR_INIT_EXPR_FN (t
)) == ADDR_EXPR
)
2033 fn
= TREE_OPERAND (AGGR_INIT_EXPR_FN (t
), 0);
2035 if (fn
&& TREE_CODE (fn
) == FUNCTION_DECL
)
2037 if (DECL_CONSTRUCTOR_P (fn
))
2038 dump_type (pp
, DECL_CONTEXT (fn
), flags
);
2040 dump_decl (pp
, fn
, 0);
2043 dump_expr (pp
, AGGR_INIT_EXPR_FN (t
), 0);
2045 dump_aggr_init_expr_args (pp
, t
, flags
, true);
2050 tree fn
= CALL_EXPR_FN (t
);
2051 bool skipfirst
= false;
2053 /* Deal with internal functions. */
2054 if (fn
== NULL_TREE
)
2056 pp_string (pp
, internal_fn_name (CALL_EXPR_IFN (t
)));
2057 dump_call_expr_args (pp
, t
, flags
, skipfirst
);
2061 if (TREE_CODE (fn
) == ADDR_EXPR
)
2062 fn
= TREE_OPERAND (fn
, 0);
2064 /* Nobody is interested in seeing the guts of vcalls. */
2065 if (TREE_CODE (fn
) == OBJ_TYPE_REF
)
2066 fn
= resolve_virtual_fun_from_obj_type_ref (fn
);
2068 if (TREE_TYPE (fn
) != NULL_TREE
2069 && NEXT_CODE (fn
) == METHOD_TYPE
2070 && call_expr_nargs (t
))
2072 tree ob
= CALL_EXPR_ARG (t
, 0);
2073 if (TREE_CODE (ob
) == ADDR_EXPR
)
2075 dump_expr (pp
, TREE_OPERAND (ob
, 0),
2076 flags
| TFF_EXPR_IN_PARENS
);
2079 else if (TREE_CODE (ob
) != PARM_DECL
2080 || strcmp (IDENTIFIER_POINTER (DECL_NAME (ob
)), "this"))
2082 dump_expr (pp
, ob
, flags
| TFF_EXPR_IN_PARENS
);
2087 if (flag_sanitize
& SANITIZE_UNDEFINED
2088 && is_ubsan_builtin_p (fn
))
2090 pp_string (cxx_pp
, M_("<ubsan routine call>"));
2093 dump_expr (pp
, fn
, flags
| TFF_EXPR_IN_PARENS
);
2094 dump_call_expr_args (pp
, t
, flags
, skipfirst
);
2099 /* Note that this only works for G++ target exprs. If somebody
2100 builds a general TARGET_EXPR, there's no way to represent that
2101 it initializes anything other that the parameter slot for the
2102 default argument. Note we may have cleared out the first
2103 operand in expand_expr, so don't go killing ourselves. */
2104 if (TREE_OPERAND (t
, 1))
2105 dump_expr (pp
, TREE_OPERAND (t
, 1), flags
| TFF_EXPR_IN_PARENS
);
2108 case POINTER_PLUS_EXPR
:
2109 dump_binary_op (pp
, "+", t
, flags
);
2114 dump_binary_op (pp
, assignment_operator_name_info
[NOP_EXPR
].name
,
2121 case TRUNC_DIV_EXPR
:
2122 case TRUNC_MOD_EXPR
:
2130 case TRUTH_ANDIF_EXPR
:
2131 case TRUTH_ORIF_EXPR
:
2138 case EXACT_DIV_EXPR
:
2139 dump_binary_op (pp
, operator_name_info
[TREE_CODE (t
)].name
, t
, flags
);
2143 case FLOOR_DIV_EXPR
:
2144 case ROUND_DIV_EXPR
:
2146 dump_binary_op (pp
, "/", t
, flags
);
2150 case FLOOR_MOD_EXPR
:
2151 case ROUND_MOD_EXPR
:
2152 dump_binary_op (pp
, "%", t
, flags
);
2157 tree ob
= TREE_OPERAND (t
, 0);
2158 if (INDIRECT_REF_P (ob
))
2160 ob
= TREE_OPERAND (ob
, 0);
2161 if (TREE_CODE (ob
) != PARM_DECL
2163 && strcmp (IDENTIFIER_POINTER (DECL_NAME (ob
)), "this")))
2165 dump_expr (pp
, ob
, flags
| TFF_EXPR_IN_PARENS
);
2166 if (TREE_CODE (TREE_TYPE (ob
)) == REFERENCE_TYPE
)
2174 dump_expr (pp
, ob
, flags
| TFF_EXPR_IN_PARENS
);
2177 dump_expr (pp
, TREE_OPERAND (t
, 1), flags
& ~TFF_EXPR_IN_PARENS
);
2182 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
| TFF_EXPR_IN_PARENS
);
2183 pp_cxx_left_bracket (pp
);
2184 dump_expr (pp
, TREE_OPERAND (t
, 1), flags
| TFF_EXPR_IN_PARENS
);
2185 pp_cxx_right_bracket (pp
);
2188 case ARRAY_NOTATION_REF
:
2189 dump_expr (pp
, ARRAY_NOTATION_ARRAY (t
), flags
| TFF_EXPR_IN_PARENS
);
2190 pp_cxx_left_bracket (pp
);
2191 dump_expr (pp
, ARRAY_NOTATION_START (t
), flags
| TFF_EXPR_IN_PARENS
);
2193 dump_expr (pp
, ARRAY_NOTATION_LENGTH (t
), flags
| TFF_EXPR_IN_PARENS
);
2195 dump_expr (pp
, ARRAY_NOTATION_STRIDE (t
), flags
| TFF_EXPR_IN_PARENS
);
2196 pp_cxx_right_bracket (pp
);
2199 case UNARY_PLUS_EXPR
:
2200 dump_unary_op (pp
, "+", t
, flags
);
2204 if (TREE_CODE (TREE_OPERAND (t
, 0)) == FUNCTION_DECL
2205 || TREE_CODE (TREE_OPERAND (t
, 0)) == STRING_CST
2206 /* An ADDR_EXPR can have reference type. In that case, we
2207 shouldn't print the `&' doing so indicates to the user
2208 that the expression has pointer type. */
2210 && TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
))
2211 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
| TFF_EXPR_IN_PARENS
);
2212 else if (TREE_CODE (TREE_OPERAND (t
, 0)) == LABEL_DECL
)
2213 dump_unary_op (pp
, "&&", t
, flags
);
2215 dump_unary_op (pp
, "&", t
, flags
);
2219 if (TREE_HAS_CONSTRUCTOR (t
))
2221 t
= TREE_OPERAND (t
, 0);
2222 gcc_assert (TREE_CODE (t
) == CALL_EXPR
);
2223 dump_expr (pp
, CALL_EXPR_FN (t
), flags
| TFF_EXPR_IN_PARENS
);
2224 dump_call_expr_args (pp
, t
, flags
, true);
2228 if (TREE_OPERAND (t
,0) != NULL_TREE
2229 && TREE_TYPE (TREE_OPERAND (t
, 0))
2230 && NEXT_CODE (TREE_OPERAND (t
, 0)) == REFERENCE_TYPE
)
2231 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
);
2233 dump_unary_op (pp
, "*", t
, flags
);
2238 if (TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
2239 && integer_zerop (TREE_OPERAND (t
, 1)))
2240 dump_expr (pp
, TREE_OPERAND (TREE_OPERAND (t
, 0), 0), flags
);
2244 if (!integer_zerop (TREE_OPERAND (t
, 1)))
2246 pp_cxx_left_paren (pp
);
2247 if (!integer_onep (TYPE_SIZE_UNIT
2248 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (t
, 0))))))
2250 pp_cxx_left_paren (pp
);
2251 dump_type (pp
, ptr_type_node
, flags
);
2252 pp_cxx_right_paren (pp
);
2255 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
);
2256 if (!integer_zerop (TREE_OPERAND (t
, 1)))
2258 pp_cxx_ws_string (pp
, "+");
2259 dump_expr (pp
, fold_convert (ssizetype
, TREE_OPERAND (t
, 1)),
2261 pp_cxx_right_paren (pp
);
2268 case TRUTH_NOT_EXPR
:
2269 case PREDECREMENT_EXPR
:
2270 case PREINCREMENT_EXPR
:
2271 dump_unary_op (pp
, operator_name_info
[TREE_CODE (t
)].name
, t
, flags
);
2274 case POSTDECREMENT_EXPR
:
2275 case POSTINCREMENT_EXPR
:
2276 pp_cxx_left_paren (pp
);
2277 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
| TFF_EXPR_IN_PARENS
);
2278 pp_cxx_ws_string (pp
, operator_name_info
[TREE_CODE (t
)].name
);
2279 pp_cxx_right_paren (pp
);
2282 case NON_LVALUE_EXPR
:
2283 /* FIXME: This is a KLUDGE workaround for a parsing problem. There
2284 should be another level of INDIRECT_REF so that I don't have to do
2286 if (TREE_TYPE (t
) != NULL_TREE
&& NEXT_CODE (t
) == POINTER_TYPE
)
2288 tree next
= TREE_TYPE (TREE_TYPE (t
));
2290 while (TYPE_PTR_P (next
))
2291 next
= TREE_TYPE (next
);
2293 if (TREE_CODE (next
) == FUNCTION_TYPE
)
2295 if (flags
& TFF_EXPR_IN_PARENS
)
2296 pp_cxx_left_paren (pp
);
2298 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
& ~TFF_EXPR_IN_PARENS
);
2299 if (flags
& TFF_EXPR_IN_PARENS
)
2300 pp_cxx_right_paren (pp
);
2303 /* Else fall through. */
2305 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
| TFF_EXPR_IN_PARENS
);
2309 case IMPLICIT_CONV_EXPR
:
2310 case VIEW_CONVERT_EXPR
:
2312 tree op
= TREE_OPERAND (t
, 0);
2313 tree ttype
= TREE_TYPE (t
);
2314 tree optype
= TREE_TYPE (op
);
2316 if (TREE_CODE (ttype
) != TREE_CODE (optype
)
2317 && POINTER_TYPE_P (ttype
)
2318 && POINTER_TYPE_P (optype
)
2319 && same_type_p (TREE_TYPE (optype
),
2322 if (TREE_CODE (ttype
) == REFERENCE_TYPE
)
2325 if (TREE_CODE (op
) == ADDR_EXPR
)
2326 dump_expr (pp
, TREE_OPERAND (op
, 0), flags
);
2328 dump_unary_op (pp
, "*", t
, flags
);
2331 dump_unary_op (pp
, "&", t
, flags
);
2333 else if (!same_type_p (TREE_TYPE (op
), TREE_TYPE (t
)))
2335 /* It is a cast, but we cannot tell whether it is a
2336 reinterpret or static cast. Use the C style notation. */
2337 if (flags
& TFF_EXPR_IN_PARENS
)
2338 pp_cxx_left_paren (pp
);
2339 pp_cxx_left_paren (pp
);
2340 dump_type (pp
, TREE_TYPE (t
), flags
);
2341 pp_cxx_right_paren (pp
);
2342 dump_expr (pp
, op
, flags
| TFF_EXPR_IN_PARENS
);
2343 if (flags
& TFF_EXPR_IN_PARENS
)
2344 pp_cxx_right_paren (pp
);
2347 dump_expr (pp
, op
, flags
);
2352 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
)))
2354 tree idx
= build_ptrmemfunc_access_expr (t
, pfn_identifier
);
2356 if (integer_zerop (idx
))
2358 /* A NULL pointer-to-member constant. */
2359 pp_cxx_left_paren (pp
);
2360 pp_cxx_left_paren (pp
);
2361 dump_type (pp
, TREE_TYPE (t
), flags
);
2362 pp_cxx_right_paren (pp
);
2363 pp_character (pp
, '0');
2364 pp_cxx_right_paren (pp
);
2367 else if (tree_fits_shwi_p (idx
))
2370 unsigned HOST_WIDE_INT n
;
2372 t
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
2373 t
= TYPE_METHOD_BASETYPE (t
);
2374 virtuals
= BINFO_VIRTUALS (TYPE_BINFO (TYPE_MAIN_VARIANT (t
)));
2376 n
= tree_to_shwi (idx
);
2378 /* Map vtable index back one, to allow for the null pointer to
2382 while (n
> 0 && virtuals
)
2385 virtuals
= TREE_CHAIN (virtuals
);
2389 dump_expr (pp
, BV_FN (virtuals
),
2390 flags
| TFF_EXPR_IN_PARENS
);
2395 if (TREE_TYPE (t
) && LAMBDA_TYPE_P (TREE_TYPE (t
)))
2396 pp_string (pp
, "<lambda closure object>");
2397 if (TREE_TYPE (t
) && EMPTY_CONSTRUCTOR_P (t
))
2399 dump_type (pp
, TREE_TYPE (t
), 0);
2400 pp_cxx_left_paren (pp
);
2401 pp_cxx_right_paren (pp
);
2405 if (!BRACE_ENCLOSED_INITIALIZER_P (t
))
2406 dump_type (pp
, TREE_TYPE (t
), 0);
2407 pp_cxx_left_brace (pp
);
2408 dump_expr_init_vec (pp
, CONSTRUCTOR_ELTS (t
), flags
);
2409 pp_cxx_right_brace (pp
);
2416 tree ob
= TREE_OPERAND (t
, 0);
2417 if (is_dummy_object (ob
))
2419 t
= TREE_OPERAND (t
, 1);
2420 if (TREE_CODE (t
) == FUNCTION_DECL
)
2422 dump_expr (pp
, t
, flags
| TFF_EXPR_IN_PARENS
);
2423 else if (BASELINK_P (t
))
2424 dump_expr (pp
, OVL_CURRENT (BASELINK_FUNCTIONS (t
)),
2425 flags
| TFF_EXPR_IN_PARENS
);
2427 dump_decl (pp
, t
, flags
);
2431 if (INDIRECT_REF_P (ob
))
2433 dump_expr (pp
, TREE_OPERAND (ob
, 0), flags
| TFF_EXPR_IN_PARENS
);
2439 dump_expr (pp
, ob
, flags
| TFF_EXPR_IN_PARENS
);
2443 dump_expr (pp
, TREE_OPERAND (t
, 1), flags
| TFF_EXPR_IN_PARENS
);
2448 case TEMPLATE_PARM_INDEX
:
2449 dump_decl (pp
, TEMPLATE_PARM_DECL (t
), flags
& ~TFF_DECL_SPECIFIERS
);
2453 if (TREE_OPERAND (t
, 0) == NULL_TREE
2454 || TREE_CHAIN (TREE_OPERAND (t
, 0)))
2456 dump_type (pp
, TREE_TYPE (t
), flags
);
2457 pp_cxx_left_paren (pp
);
2458 dump_expr_list (pp
, TREE_OPERAND (t
, 0), flags
);
2459 pp_cxx_right_paren (pp
);
2463 pp_cxx_left_paren (pp
);
2464 dump_type (pp
, TREE_TYPE (t
), flags
);
2465 pp_cxx_right_paren (pp
);
2466 pp_cxx_left_paren (pp
);
2467 dump_expr_list (pp
, TREE_OPERAND (t
, 0), flags
);
2468 pp_cxx_right_paren (pp
);
2472 case STATIC_CAST_EXPR
:
2473 pp_cxx_ws_string (pp
, "static_cast");
2475 case REINTERPRET_CAST_EXPR
:
2476 pp_cxx_ws_string (pp
, "reinterpret_cast");
2478 case CONST_CAST_EXPR
:
2479 pp_cxx_ws_string (pp
, "const_cast");
2481 case DYNAMIC_CAST_EXPR
:
2482 pp_cxx_ws_string (pp
, "dynamic_cast");
2484 pp_cxx_begin_template_argument_list (pp
);
2485 dump_type (pp
, TREE_TYPE (t
), flags
);
2486 pp_cxx_end_template_argument_list (pp
);
2487 pp_cxx_left_paren (pp
);
2488 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
);
2489 pp_cxx_right_paren (pp
);
2493 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
);
2499 if (TREE_CODE (t
) == SIZEOF_EXPR
)
2500 pp_cxx_ws_string (pp
, "sizeof");
2503 gcc_assert (TREE_CODE (t
) == ALIGNOF_EXPR
);
2504 pp_cxx_ws_string (pp
, "__alignof__");
2506 op
= TREE_OPERAND (t
, 0);
2507 if (PACK_EXPANSION_P (op
))
2509 pp_string (pp
, "...");
2510 op
= PACK_EXPANSION_PATTERN (op
);
2512 pp_cxx_whitespace (pp
);
2513 pp_cxx_left_paren (pp
);
2514 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
2515 dump_type (pp
, TREE_TYPE (op
), flags
);
2516 else if (TYPE_P (TREE_OPERAND (t
, 0)))
2517 dump_type (pp
, op
, flags
);
2519 dump_expr (pp
, op
, flags
);
2520 pp_cxx_right_paren (pp
);
2523 case AT_ENCODE_EXPR
:
2524 pp_cxx_ws_string (pp
, "@encode");
2525 pp_cxx_whitespace (pp
);
2526 pp_cxx_left_paren (pp
);
2527 dump_type (pp
, TREE_OPERAND (t
, 0), flags
);
2528 pp_cxx_right_paren (pp
);
2532 pp_cxx_ws_string (pp
, "noexcept");
2533 pp_cxx_whitespace (pp
);
2534 pp_cxx_left_paren (pp
);
2535 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
);
2536 pp_cxx_right_paren (pp
);
2541 pp_cxx_ws_string (pp
, operator_name_info
[TREE_CODE (t
)].name
);
2542 pp_cxx_whitespace (pp
);
2543 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
);
2547 pp_string (pp
, M_("<unparsed>"));
2550 case TRY_CATCH_EXPR
:
2551 case WITH_CLEANUP_EXPR
:
2552 case CLEANUP_POINT_EXPR
:
2553 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
);
2556 case PSEUDO_DTOR_EXPR
:
2557 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
);
2559 if (TREE_OPERAND (t
, 1))
2561 dump_type (pp
, TREE_OPERAND (t
, 1), flags
);
2562 pp_cxx_colon_colon (pp
);
2564 pp_cxx_complement (pp
);
2565 dump_type (pp
, TREE_OPERAND (t
, 2), flags
);
2568 case TEMPLATE_ID_EXPR
:
2569 dump_decl (pp
, t
, flags
);
2575 case STATEMENT_LIST
:
2576 /* We don't yet have a way of dumping statements in a
2577 human-readable format. */
2578 pp_string (pp
, "({...})");
2582 pp_string (pp
, "while (1) { ");
2583 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
& ~TFF_EXPR_IN_PARENS
);
2584 pp_cxx_right_brace (pp
);
2588 pp_string (pp
, "if (");
2589 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
& ~TFF_EXPR_IN_PARENS
);
2590 pp_string (pp
, ") break; ");
2594 dump_expr (pp
, BASELINK_FUNCTIONS (t
), flags
& ~TFF_EXPR_IN_PARENS
);
2597 case EMPTY_CLASS_EXPR
:
2598 dump_type (pp
, TREE_TYPE (t
), flags
);
2599 pp_cxx_left_paren (pp
);
2600 pp_cxx_right_paren (pp
);
2603 case NON_DEPENDENT_EXPR
:
2604 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
);
2607 case ARGUMENT_PACK_SELECT
:
2608 dump_template_argument (pp
, ARGUMENT_PACK_SELECT_FROM_PACK (t
), flags
);
2620 pp_type_specifier_seq (pp
, t
);
2624 /* We get here when we want to print a dependent type as an
2625 id-expression, without any disambiguator decoration. */
2626 pp
->id_expression (t
);
2629 case TEMPLATE_TYPE_PARM
:
2630 case TEMPLATE_TEMPLATE_PARM
:
2631 case BOUND_TEMPLATE_TEMPLATE_PARM
:
2632 dump_type (pp
, t
, flags
);
2636 pp_cxx_trait_expression (pp
, t
);
2640 pp_cxx_va_arg_expression (pp
, t
);
2644 pp_cxx_offsetof_expression (pp
, t
);
2648 dump_decl (pp
, t
, flags
);
2651 case EXPR_PACK_EXPANSION
:
2658 case VEC_DELETE_EXPR
:
2664 case UNORDERED_EXPR
:
2674 case FIX_TRUNC_EXPR
:
2679 case TRUTH_AND_EXPR
:
2681 case TRUTH_XOR_EXPR
:
2682 if (flags
& TFF_EXPR_IN_PARENS
)
2683 pp_cxx_left_paren (pp
);
2685 if (flags
& TFF_EXPR_IN_PARENS
)
2686 pp_cxx_right_paren (pp
);
2690 dump_expr (pp
, resolve_virtual_fun_from_obj_type_ref (t
), flags
);
2694 pp_string (pp
, M_("<lambda>"));
2698 pp_cxx_left_paren (pp
);
2699 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
| TFF_EXPR_IN_PARENS
);
2700 pp_cxx_right_paren (pp
);
2703 case PLACEHOLDER_EXPR
:
2704 pp_string (pp
, M_("*this"));
2707 /* This list is incomplete, but should suffice for now.
2708 It is very important that `sorry' does not call
2709 `report_error_function'. That could cause an infinite loop. */
2711 pp_unsupported_tree (pp
, t
);
2712 /* fall through to ERROR_MARK... */
2714 pp_string (pp
, M_("<expression error>"));
2720 dump_binary_op (cxx_pretty_printer
*pp
, const char *opstring
, tree t
,
2723 pp_cxx_left_paren (pp
);
2724 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
| TFF_EXPR_IN_PARENS
);
2725 pp_cxx_whitespace (pp
);
2727 pp_cxx_ws_string (pp
, opstring
);
2729 pp_string (pp
, M_("<unknown operator>"));
2730 pp_cxx_whitespace (pp
);
2731 dump_expr (pp
, TREE_OPERAND (t
, 1), flags
| TFF_EXPR_IN_PARENS
);
2732 pp_cxx_right_paren (pp
);
2736 dump_unary_op (cxx_pretty_printer
*pp
, const char *opstring
, tree t
, int flags
)
2738 if (flags
& TFF_EXPR_IN_PARENS
)
2739 pp_cxx_left_paren (pp
);
2740 pp_cxx_ws_string (pp
, opstring
);
2741 dump_expr (pp
, TREE_OPERAND (t
, 0), flags
& ~TFF_EXPR_IN_PARENS
);
2742 if (flags
& TFF_EXPR_IN_PARENS
)
2743 pp_cxx_right_paren (pp
);
2747 reinit_cxx_pp (void)
2749 pp_clear_output_area (cxx_pp
);
2750 cxx_pp
->padding
= pp_none
;
2751 pp_indentation (cxx_pp
) = 0;
2752 pp_needs_newline (cxx_pp
) = false;
2753 cxx_pp
->enclosing_scope
= current_function_decl
;
2756 /* Same as pp_formatted_text, except the return string is a separate
2757 copy and has a GGC storage duration, e.g. an indefinite lifetime. */
2760 pp_ggc_formatted_text (pretty_printer
*pp
)
2762 return ggc_strdup (pp_formatted_text (pp
));
2765 /* Exported interface to stringifying types, exprs and decls under TFF_*
2769 type_as_string (tree typ
, int flags
)
2772 pp_translate_identifiers (cxx_pp
) = false;
2773 dump_type (cxx_pp
, typ
, flags
);
2774 return pp_ggc_formatted_text (cxx_pp
);
2778 type_as_string_translate (tree typ
, int flags
)
2781 dump_type (cxx_pp
, typ
, flags
);
2782 return pp_ggc_formatted_text (cxx_pp
);
2786 expr_as_string (tree decl
, int flags
)
2789 pp_translate_identifiers (cxx_pp
) = false;
2790 dump_expr (cxx_pp
, decl
, flags
);
2791 return pp_ggc_formatted_text (cxx_pp
);
2794 /* Wrap decl_as_string with options appropriate for dwarf. */
2797 decl_as_dwarf_string (tree decl
, int flags
)
2800 /* Curiously, reinit_cxx_pp doesn't reset the flags field, so setting the flag
2801 here will be adequate to get the desired behaviour. */
2802 cxx_pp
->flags
|= pp_c_flag_gnu_v3
;
2803 name
= decl_as_string (decl
, flags
);
2804 /* Subsequent calls to the pretty printer shouldn't use this style. */
2805 cxx_pp
->flags
&= ~pp_c_flag_gnu_v3
;
2810 decl_as_string (tree decl
, int flags
)
2813 pp_translate_identifiers (cxx_pp
) = false;
2814 dump_decl (cxx_pp
, decl
, flags
);
2815 return pp_ggc_formatted_text (cxx_pp
);
2819 decl_as_string_translate (tree decl
, int flags
)
2822 dump_decl (cxx_pp
, decl
, flags
);
2823 return pp_ggc_formatted_text (cxx_pp
);
2826 /* Wrap lang_decl_name with options appropriate for dwarf. */
2829 lang_decl_dwarf_name (tree decl
, int v
, bool translate
)
2832 /* Curiously, reinit_cxx_pp doesn't reset the flags field, so setting the flag
2833 here will be adequate to get the desired behaviour. */
2834 cxx_pp
->flags
|= pp_c_flag_gnu_v3
;
2835 name
= lang_decl_name (decl
, v
, translate
);
2836 /* Subsequent calls to the pretty printer shouldn't use this style. */
2837 cxx_pp
->flags
&= ~pp_c_flag_gnu_v3
;
2841 /* Generate the three forms of printable names for cxx_printable_name. */
2844 lang_decl_name (tree decl
, int v
, bool translate
)
2848 ? decl_as_string_translate (decl
, TFF_DECL_SPECIFIERS
)
2849 : decl_as_string (decl
, TFF_DECL_SPECIFIERS
));
2852 pp_translate_identifiers (cxx_pp
) = translate
;
2854 && (DECL_CLASS_SCOPE_P (decl
)
2855 || (DECL_NAMESPACE_SCOPE_P (decl
)
2856 && CP_DECL_CONTEXT (decl
) != global_namespace
)))
2858 dump_type (cxx_pp
, CP_DECL_CONTEXT (decl
), TFF_PLAIN_IDENTIFIER
);
2859 pp_cxx_colon_colon (cxx_pp
);
2862 if (TREE_CODE (decl
) == FUNCTION_DECL
)
2863 dump_function_name (cxx_pp
, decl
, TFF_PLAIN_IDENTIFIER
);
2864 else if ((DECL_NAME (decl
) == NULL_TREE
)
2865 && TREE_CODE (decl
) == NAMESPACE_DECL
)
2866 dump_decl (cxx_pp
, decl
, TFF_PLAIN_IDENTIFIER
| TFF_UNQUALIFIED_NAME
);
2868 dump_decl (cxx_pp
, DECL_NAME (decl
), TFF_PLAIN_IDENTIFIER
);
2870 return pp_ggc_formatted_text (cxx_pp
);
2873 /* Return the location of a tree passed to %+ formats. */
2876 location_of (tree t
)
2880 t
= TYPE_MAIN_DECL (t
);
2882 return input_location
;
2884 else if (TREE_CODE (t
) == OVERLOAD
)
2885 t
= OVL_FUNCTION (t
);
2888 return DECL_SOURCE_LOCATION (t
);
2889 return EXPR_LOC_OR_LOC (t
, input_location
);
2892 /* Now the interfaces from error et al to dump_type et al. Each takes an
2893 on/off VERBOSE flag and supply the appropriate TFF_ flags to a dump_
2897 decl_to_string (tree decl
, int verbose
)
2901 if (TREE_CODE (decl
) == TYPE_DECL
|| TREE_CODE (decl
) == RECORD_TYPE
2902 || TREE_CODE (decl
) == UNION_TYPE
|| TREE_CODE (decl
) == ENUMERAL_TYPE
)
2903 flags
= TFF_CLASS_KEY_OR_ENUM
;
2905 flags
|= TFF_DECL_SPECIFIERS
;
2906 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
2907 flags
|= TFF_DECL_SPECIFIERS
| TFF_RETURN_TYPE
;
2908 flags
|= TFF_TEMPLATE_HEADER
;
2911 dump_decl (cxx_pp
, decl
, flags
);
2912 return pp_ggc_formatted_text (cxx_pp
);
2916 expr_to_string (tree decl
)
2919 dump_expr (cxx_pp
, decl
, 0);
2920 return pp_ggc_formatted_text (cxx_pp
);
2924 fndecl_to_string (tree fndecl
, int verbose
)
2928 flags
= TFF_EXCEPTION_SPECIFICATION
| TFF_DECL_SPECIFIERS
2929 | TFF_TEMPLATE_HEADER
;
2931 flags
|= TFF_FUNCTION_DEFAULT_ARGUMENTS
;
2933 dump_decl (cxx_pp
, fndecl
, flags
);
2934 return pp_ggc_formatted_text (cxx_pp
);
2939 code_to_string (enum tree_code c
)
2941 return get_tree_code_name (c
);
2945 language_to_string (enum languages c
)
2952 case lang_cplusplus
:
2964 /* Return the proper printed version of a parameter to a C++ function. */
2967 parm_to_string (int p
)
2971 pp_string (cxx_pp
, "'this'");
2973 pp_decimal_int (cxx_pp
, p
+ 1);
2974 return pp_ggc_formatted_text (cxx_pp
);
2978 op_to_string (enum tree_code p
)
2980 tree id
= operator_name_info
[p
].identifier
;
2981 return id
? IDENTIFIER_POINTER (id
) : M_("<unknown>");
2985 type_to_string (tree typ
, int verbose
)
2989 flags
|= TFF_CLASS_KEY_OR_ENUM
;
2990 flags
|= TFF_TEMPLATE_HEADER
;
2993 dump_type (cxx_pp
, typ
, flags
);
2994 /* If we're printing a type that involves typedefs, also print the
2995 stripped version. But sometimes the stripped version looks
2996 exactly the same, so we don't want it after all. To avoid printing
2997 it in that case, we play ugly obstack games. */
2998 if (typ
&& TYPE_P (typ
) && typ
!= TYPE_CANONICAL (typ
)
2999 && !uses_template_parms (typ
))
3001 int aka_start
, aka_len
; char *p
;
3002 struct obstack
*ob
= pp_buffer (cxx_pp
)->obstack
;
3003 /* Remember the end of the initial dump. */
3004 int len
= obstack_object_size (ob
);
3005 tree aka
= strip_typedefs (typ
);
3006 pp_string (cxx_pp
, " {aka");
3007 pp_cxx_whitespace (cxx_pp
);
3008 /* And remember the start of the aka dump. */
3009 aka_start
= obstack_object_size (ob
);
3010 dump_type (cxx_pp
, aka
, flags
);
3011 aka_len
= obstack_object_size (ob
) - aka_start
;
3012 pp_right_brace (cxx_pp
);
3013 p
= (char*)obstack_base (ob
);
3014 /* If they are identical, cut off the aka with a NUL. */
3015 if (len
== aka_len
&& memcmp (p
, p
+aka_start
, len
) == 0)
3018 return pp_ggc_formatted_text (cxx_pp
);
3022 assop_to_string (enum tree_code p
)
3024 tree id
= assignment_operator_name_info
[(int) p
].identifier
;
3025 return id
? IDENTIFIER_POINTER (id
) : M_("{unknown}");
3029 args_to_string (tree p
, int verbose
)
3033 flags
|= TFF_CLASS_KEY_OR_ENUM
;
3038 if (TYPE_P (TREE_VALUE (p
)))
3039 return type_as_string_translate (p
, flags
);
3042 for (; p
; p
= TREE_CHAIN (p
))
3044 if (TREE_VALUE (p
) == null_node
)
3045 pp_cxx_ws_string (cxx_pp
, "NULL");
3047 dump_type (cxx_pp
, error_type (TREE_VALUE (p
)), flags
);
3049 pp_separate_with_comma (cxx_pp
);
3051 return pp_ggc_formatted_text (cxx_pp
);
3054 /* Pretty-print a deduction substitution (from deduction_tsubst_fntype). P
3055 is a TREE_LIST with purpose the TEMPLATE_DECL, value the template
3059 subst_to_string (tree p
)
3061 tree decl
= TREE_PURPOSE (p
);
3062 tree targs
= TREE_VALUE (p
);
3063 tree tparms
= DECL_TEMPLATE_PARMS (decl
);
3064 int flags
= (TFF_DECL_SPECIFIERS
|TFF_TEMPLATE_HEADER
3065 |TFF_NO_TEMPLATE_BINDINGS
);
3071 dump_template_decl (cxx_pp
, TREE_PURPOSE (p
), flags
);
3072 dump_substitution (cxx_pp
, NULL
, tparms
, targs
, /*flags=*/0);
3073 return pp_ggc_formatted_text (cxx_pp
);
3077 cv_to_string (tree p
, int v
)
3080 cxx_pp
->padding
= v
? pp_before
: pp_none
;
3081 pp_cxx_cv_qualifier_seq (cxx_pp
, p
);
3082 return pp_ggc_formatted_text (cxx_pp
);
3086 eh_spec_to_string (tree p
, int /*v*/)
3090 dump_exception_spec (cxx_pp
, p
, flags
);
3091 return pp_ggc_formatted_text (cxx_pp
);
3094 /* Langhook for print_error_function. */
3096 cxx_print_error_function (diagnostic_context
*context
, const char *file
,
3097 diagnostic_info
*diagnostic
)
3099 lhd_print_error_function (context
, file
, diagnostic
);
3100 pp_set_prefix (context
->printer
, file
);
3101 maybe_print_instantiation_context (context
);
3105 cp_diagnostic_starter (diagnostic_context
*context
,
3106 diagnostic_info
*diagnostic
)
3108 diagnostic_report_current_module (context
, diagnostic
->location
);
3109 cp_print_error_function (context
, diagnostic
);
3110 maybe_print_instantiation_context (context
);
3111 maybe_print_constexpr_context (context
);
3112 pp_set_prefix (context
->printer
, diagnostic_build_prefix (context
,
3116 /* Print current function onto BUFFER, in the process of reporting
3117 a diagnostic message. Called from cp_diagnostic_starter. */
3119 cp_print_error_function (diagnostic_context
*context
,
3120 diagnostic_info
*diagnostic
)
3122 /* If we are in an instantiation context, current_function_decl is likely
3123 to be wrong, so just rely on print_instantiation_full_context. */
3124 if (current_instantiation ())
3126 if (diagnostic_last_function_changed (context
, diagnostic
))
3128 const char *old_prefix
= context
->printer
->prefix
;
3129 const char *file
= LOCATION_FILE (diagnostic
->location
);
3130 tree abstract_origin
= diagnostic_abstract_origin (diagnostic
);
3131 char *new_prefix
= (file
&& abstract_origin
== NULL
)
3132 ? file_name_as_prefix (context
, file
) : NULL
;
3134 pp_set_prefix (context
->printer
, new_prefix
);
3136 if (current_function_decl
== NULL
)
3137 pp_string (context
->printer
, _("At global scope:"));
3142 if (abstract_origin
)
3144 ao
= BLOCK_ABSTRACT_ORIGIN (abstract_origin
);
3145 while (TREE_CODE (ao
) == BLOCK
3146 && BLOCK_ABSTRACT_ORIGIN (ao
)
3147 && BLOCK_ABSTRACT_ORIGIN (ao
) != ao
)
3148 ao
= BLOCK_ABSTRACT_ORIGIN (ao
);
3149 gcc_assert (TREE_CODE (ao
) == FUNCTION_DECL
);
3153 fndecl
= current_function_decl
;
3155 pp_printf (context
->printer
, function_category (fndecl
),
3156 cxx_printable_name_translate (fndecl
, 2));
3158 while (abstract_origin
)
3161 tree block
= abstract_origin
;
3163 locus
= &BLOCK_SOURCE_LOCATION (block
);
3165 block
= BLOCK_SUPERCONTEXT (block
);
3166 while (block
&& TREE_CODE (block
) == BLOCK
3167 && BLOCK_ABSTRACT_ORIGIN (block
))
3169 ao
= BLOCK_ABSTRACT_ORIGIN (block
);
3171 while (TREE_CODE (ao
) == BLOCK
3172 && BLOCK_ABSTRACT_ORIGIN (ao
)
3173 && BLOCK_ABSTRACT_ORIGIN (ao
) != ao
)
3174 ao
= BLOCK_ABSTRACT_ORIGIN (ao
);
3176 if (TREE_CODE (ao
) == FUNCTION_DECL
)
3181 else if (TREE_CODE (ao
) != BLOCK
)
3184 block
= BLOCK_SUPERCONTEXT (block
);
3187 abstract_origin
= block
;
3190 while (block
&& TREE_CODE (block
) == BLOCK
)
3191 block
= BLOCK_SUPERCONTEXT (block
);
3193 if (block
&& TREE_CODE (block
) == FUNCTION_DECL
)
3195 abstract_origin
= NULL
;
3199 expanded_location s
= expand_location (*locus
);
3200 pp_character (context
->printer
, ',');
3201 pp_newline (context
->printer
);
3204 if (context
->show_column
&& s
.column
!= 0)
3205 pp_printf (context
->printer
,
3206 _(" inlined from %qs at %r%s:%d:%d%R"),
3207 cxx_printable_name_translate (fndecl
, 2),
3208 "locus", s
.file
, s
.line
, s
.column
);
3210 pp_printf (context
->printer
,
3211 _(" inlined from %qs at %r%s:%d%R"),
3212 cxx_printable_name_translate (fndecl
, 2),
3213 "locus", s
.file
, s
.line
);
3217 pp_printf (context
->printer
, _(" inlined from %qs"),
3218 cxx_printable_name_translate (fndecl
, 2));
3221 pp_character (context
->printer
, ':');
3223 pp_newline (context
->printer
);
3225 diagnostic_set_last_function (context
, diagnostic
);
3226 pp_destroy_prefix (context
->printer
);
3227 context
->printer
->prefix
= old_prefix
;
3231 /* Returns a description of FUNCTION using standard terminology. The
3232 result is a format string of the form "In CATEGORY %qs". */
3234 function_category (tree fn
)
3236 /* We can get called from the middle-end for diagnostics of function
3237 clones. Make sure we have language specific information before
3238 dereferencing it. */
3239 if (DECL_LANG_SPECIFIC (STRIP_TEMPLATE (fn
))
3240 && DECL_FUNCTION_MEMBER_P (fn
))
3242 if (DECL_STATIC_FUNCTION_P (fn
))
3243 return _("In static member function %qs");
3244 else if (DECL_COPY_CONSTRUCTOR_P (fn
))
3245 return _("In copy constructor %qs");
3246 else if (DECL_CONSTRUCTOR_P (fn
))
3247 return _("In constructor %qs");
3248 else if (DECL_DESTRUCTOR_P (fn
))
3249 return _("In destructor %qs");
3250 else if (LAMBDA_FUNCTION_P (fn
))
3251 return _("In lambda function");
3253 return _("In member function %qs");
3256 return _("In function %qs");
3259 /* Report the full context of a current template instantiation,
3262 print_instantiation_full_context (diagnostic_context
*context
)
3264 struct tinst_level
*p
= current_instantiation ();
3265 location_t location
= input_location
;
3269 pp_verbatim (context
->printer
,
3270 TREE_CODE (p
->decl
) == TREE_LIST
3271 ? _("%s: In substitution of %qS:\n")
3272 : _("%s: In instantiation of %q#D:\n"),
3273 LOCATION_FILE (location
),
3276 location
= p
->locus
;
3280 print_instantiation_partial_context (context
, p
, location
);
3283 /* Helper function of print_instantiation_partial_context() that
3284 prints a single line of instantiation context. */
3287 print_instantiation_partial_context_line (diagnostic_context
*context
,
3288 const struct tinst_level
*t
,
3289 location_t loc
, bool recursive_p
)
3291 if (loc
== UNKNOWN_LOCATION
)
3294 expanded_location xloc
= expand_location (loc
);
3296 if (context
->show_column
)
3297 pp_verbatim (context
->printer
, _("%r%s:%d:%d:%R "),
3298 "locus", xloc
.file
, xloc
.line
, xloc
.column
);
3300 pp_verbatim (context
->printer
, _("%r%s:%d:%R "),
3301 "locus", xloc
.file
, xloc
.line
);
3305 if (TREE_CODE (t
->decl
) == TREE_LIST
)
3306 pp_verbatim (context
->printer
,
3308 ? _("recursively required by substitution of %qS\n")
3309 : _("required by substitution of %qS\n"),
3312 pp_verbatim (context
->printer
,
3314 ? _("recursively required from %q#D\n")
3315 : _("required from %q#D\n"),
3320 pp_verbatim (context
->printer
,
3322 ? _("recursively required from here")
3323 : _("required from here"));
3327 /* Same as print_instantiation_full_context but less verbose. */
3330 print_instantiation_partial_context (diagnostic_context
*context
,
3331 struct tinst_level
*t0
, location_t loc
)
3333 struct tinst_level
*t
;
3336 location_t prev_loc
= loc
;
3338 for (t
= t0
; t
!= NULL
; t
= t
->next
)
3339 if (prev_loc
!= t
->locus
)
3341 prev_loc
= t
->locus
;
3347 if (template_backtrace_limit
3348 && n_total
> template_backtrace_limit
)
3350 int skip
= n_total
- template_backtrace_limit
;
3351 int head
= template_backtrace_limit
/ 2;
3353 /* Avoid skipping just 1. If so, skip 2. */
3357 head
= (template_backtrace_limit
- 1) / 2;
3360 for (n
= 0; n
< head
; n
++)
3362 gcc_assert (t
!= NULL
);
3363 if (loc
!= t
->locus
)
3364 print_instantiation_partial_context_line (context
, t
, loc
,
3365 /*recursive_p=*/false);
3369 if (t
!= NULL
&& skip
> 0)
3371 expanded_location xloc
;
3372 xloc
= expand_location (loc
);
3373 if (context
->show_column
)
3374 pp_verbatim (context
->printer
,
3375 _("%r%s:%d:%d:%R [ skipping %d instantiation "
3376 "contexts, use -ftemplate-backtrace-limit=0 to "
3378 "locus", xloc
.file
, xloc
.line
, xloc
.column
, skip
);
3380 pp_verbatim (context
->printer
,
3381 _("%r%s:%d:%R [ skipping %d instantiation "
3382 "contexts, use -ftemplate-backtrace-limit=0 to "
3384 "locus", xloc
.file
, xloc
.line
, skip
);
3389 } while (t
!= NULL
&& --skip
> 0);
3395 while (t
->next
!= NULL
&& t
->locus
== t
->next
->locus
)
3400 print_instantiation_partial_context_line (context
, t
, loc
,
3405 print_instantiation_partial_context_line (context
, NULL
, loc
,
3406 /*recursive_p=*/false);
3407 pp_newline (context
->printer
);
3410 /* Called from cp_thing to print the template context for an error. */
3412 maybe_print_instantiation_context (diagnostic_context
*context
)
3414 if (!problematic_instantiation_changed () || current_instantiation () == 0)
3417 record_last_problematic_instantiation ();
3418 print_instantiation_full_context (context
);
3421 /* Report what constexpr call(s) we're trying to expand, if any. */
3424 maybe_print_constexpr_context (diagnostic_context
*context
)
3426 vec
<tree
> call_stack
= cx_error_context ();
3430 FOR_EACH_VEC_ELT (call_stack
, ix
, t
)
3432 expanded_location xloc
= expand_location (EXPR_LOCATION (t
));
3433 const char *s
= expr_as_string (t
, 0);
3434 if (context
->show_column
)
3435 pp_verbatim (context
->printer
,
3436 _("%r%s:%d:%d:%R in constexpr expansion of %qs"),
3437 "locus", xloc
.file
, xloc
.line
, xloc
.column
, s
);
3439 pp_verbatim (context
->printer
,
3440 _("%r%s:%d:%R in constexpr expansion of %qs"),
3441 "locus", xloc
.file
, xloc
.line
, s
);
3442 pp_newline (context
->printer
);
3446 /* Called from output_format -- during diagnostic message processing --
3447 to handle C++ specific format specifier with the following meanings:
3448 %A function argument-list.
3452 %F function declaration.
3453 %L language as used in extern "lang".
3455 %P function parameter whose position is indicated by an integer.
3456 %Q assignment operator.
3457 %S substitution (template + args)
3460 %X exception-specification. */
3462 cp_printer (pretty_printer
*pp
, text_info
*text
, const char *spec
,
3463 int precision
, bool wide
, bool set_locus
, bool verbose
)
3467 #define next_tree (t = va_arg (*text->args_ptr, tree))
3468 #define next_tcode ((enum tree_code) va_arg (*text->args_ptr, int))
3469 #define next_lang ((enum languages) va_arg (*text->args_ptr, int))
3470 #define next_int va_arg (*text->args_ptr, int)
3472 if (precision
!= 0 || wide
)
3475 if (text
->locus
== NULL
)
3480 case 'A': result
= args_to_string (next_tree
, verbose
); break;
3481 case 'C': result
= code_to_string (next_tcode
); break;
3484 tree temp
= next_tree
;
3486 && DECL_HAS_DEBUG_EXPR_P (temp
))
3488 temp
= DECL_DEBUG_EXPR (temp
);
3491 result
= expr_to_string (temp
);
3495 result
= decl_to_string (temp
, verbose
);
3498 case 'E': result
= expr_to_string (next_tree
); break;
3499 case 'F': result
= fndecl_to_string (next_tree
, verbose
); break;
3500 case 'L': result
= language_to_string (next_lang
); break;
3501 case 'O': result
= op_to_string (next_tcode
); break;
3502 case 'P': result
= parm_to_string (next_int
); break;
3503 case 'Q': result
= assop_to_string (next_tcode
); break;
3504 case 'S': result
= subst_to_string (next_tree
); break;
3505 case 'T': result
= type_to_string (next_tree
, verbose
); break;
3506 case 'V': result
= cv_to_string (next_tree
, verbose
); break;
3507 case 'X': result
= eh_spec_to_string (next_tree
, verbose
); break;
3510 percent_K_format (text
);
3517 pp_string (pp
, result
);
3518 if (set_locus
&& t
!= NULL
)
3519 *text
->locus
= location_of (t
);
3527 /* Warn about the use of C++0x features when appropriate. */
3529 maybe_warn_cpp0x (cpp0x_warn_str str
)
3531 if ((cxx_dialect
== cxx98
) && !in_system_header_at (input_location
))
3532 /* We really want to suppress this warning in system headers,
3533 because libstdc++ uses variadic templates even when we aren't
3537 case CPP0X_INITIALIZER_LISTS
:
3538 pedwarn (input_location
, 0,
3539 "extended initializer lists "
3540 "only available with -std=c++11 or -std=gnu++11");
3542 case CPP0X_EXPLICIT_CONVERSION
:
3543 pedwarn (input_location
, 0,
3544 "explicit conversion operators "
3545 "only available with -std=c++11 or -std=gnu++11");
3547 case CPP0X_VARIADIC_TEMPLATES
:
3548 pedwarn (input_location
, 0,
3549 "variadic templates "
3550 "only available with -std=c++11 or -std=gnu++11");
3552 case CPP0X_LAMBDA_EXPR
:
3553 pedwarn (input_location
, 0,
3554 "lambda expressions "
3555 "only available with -std=c++11 or -std=gnu++11");
3558 pedwarn (input_location
, 0,
3559 "C++11 auto only available with -std=c++11 or -std=gnu++11");
3561 case CPP0X_SCOPED_ENUMS
:
3562 pedwarn (input_location
, 0,
3563 "scoped enums only available with -std=c++11 or -std=gnu++11");
3565 case CPP0X_DEFAULTED_DELETED
:
3566 pedwarn (input_location
, 0,
3567 "defaulted and deleted functions "
3568 "only available with -std=c++11 or -std=gnu++11");
3570 case CPP0X_INLINE_NAMESPACES
:
3571 pedwarn (input_location
, OPT_Wpedantic
,
3572 "inline namespaces "
3573 "only available with -std=c++11 or -std=gnu++11");
3575 case CPP0X_OVERRIDE_CONTROLS
:
3576 pedwarn (input_location
, 0,
3577 "override controls (override/final) "
3578 "only available with -std=c++11 or -std=gnu++11");
3581 pedwarn (input_location
, 0,
3582 "non-static data member initializers "
3583 "only available with -std=c++11 or -std=gnu++11");
3585 case CPP0X_USER_DEFINED_LITERALS
:
3586 pedwarn (input_location
, 0,
3587 "user-defined literals "
3588 "only available with -std=c++11 or -std=gnu++11");
3590 case CPP0X_DELEGATING_CTORS
:
3591 pedwarn (input_location
, 0,
3592 "delegating constructors "
3593 "only available with -std=c++11 or -std=gnu++11");
3595 case CPP0X_INHERITING_CTORS
:
3596 pedwarn (input_location
, 0,
3597 "inheriting constructors "
3598 "only available with -std=c++11 or -std=gnu++11");
3600 case CPP0X_ATTRIBUTES
:
3601 pedwarn (input_location
, 0,
3603 "only available with -std=c++11 or -std=gnu++11");
3605 case CPP0X_REF_QUALIFIER
:
3606 pedwarn (input_location
, 0,
3608 "only available with -std=c++11 or -std=gnu++11");
3615 /* Warn about the use of variadic templates when appropriate. */
3617 maybe_warn_variadic_templates (void)
3619 maybe_warn_cpp0x (CPP0X_VARIADIC_TEMPLATES
);
3623 /* Issue an ISO C++98 pedantic warning at LOCATION, conditional on
3624 option OPT with text GMSGID. Use this function to report
3625 diagnostics for constructs that are invalid C++98, but valid
3628 pedwarn_cxx98 (location_t location
, int opt
, const char *gmsgid
, ...)
3630 diagnostic_info diagnostic
;
3634 va_start (ap
, gmsgid
);
3635 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, location
,
3636 (cxx_dialect
== cxx98
) ? DK_PEDWARN
: DK_WARNING
);
3637 diagnostic
.option_index
= opt
;
3638 ret
= report_diagnostic (&diagnostic
);
3643 /* Issue a diagnostic that NAME cannot be found in SCOPE. DECL is what
3644 we found when we tried to do the lookup. LOCATION is the location of
3645 the NAME identifier. */
3648 qualified_name_lookup_error (tree scope
, tree name
,
3649 tree decl
, location_t location
)
3651 if (scope
== error_mark_node
)
3652 ; /* We already complained. */
3653 else if (TYPE_P (scope
))
3655 if (!COMPLETE_TYPE_P (scope
))
3656 error_at (location
, "incomplete type %qT used in nested name specifier",
3658 else if (TREE_CODE (decl
) == TREE_LIST
)
3660 error_at (location
, "reference to %<%T::%D%> is ambiguous",
3662 print_candidates (decl
);
3665 error_at (location
, "%qD is not a member of %qT", name
, scope
);
3667 else if (scope
!= global_namespace
)
3669 error_at (location
, "%qD is not a member of %qD", name
, scope
);
3670 suggest_alternatives_for (location
, name
);
3674 error_at (location
, "%<::%D%> has not been declared", name
);
3675 suggest_alternatives_for (location
, name
);