Update copyright years.
[gcc.git] / gcc / cp / error.c
1 /* Call-backs for C++ error reporting.
2 This code is non-reentrant.
3 Copyright (C) 1993-2019 Free Software Foundation, Inc.
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
10
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.
15
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/>. */
19
20 #include "config.h"
21 /* For use with name_hint. */
22 #define INCLUDE_UNIQUE_PTR
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "stringpool.h"
27 #include "tree-diagnostic.h"
28 #include "langhooks-def.h"
29 #include "intl.h"
30 #include "cxx-pretty-print.h"
31 #include "tree-pretty-print.h"
32 #include "gimple-pretty-print.h"
33 #include "c-family/c-objc.h"
34 #include "ubsan.h"
35 #include "internal-fn.h"
36 #include "gcc-rich-location.h"
37 #include "cp-name-hint.h"
38
39 #define pp_separate_with_comma(PP) pp_cxx_separate_with (PP, ',')
40 #define pp_separate_with_semicolon(PP) pp_cxx_separate_with (PP, ';')
41
42 /* cxx_pp is a C++ front-end-specific pretty printer: this is where we
43 dump C++ ASTs as strings. It is mostly used only by the various
44 tree -> string functions that are occasionally called from the
45 debugger or by the front-end for things like
46 __PRETTY_FUNCTION__. */
47 static cxx_pretty_printer actual_pretty_printer;
48 static cxx_pretty_printer * const cxx_pp = &actual_pretty_printer;
49
50 /* Translate if being used for diagnostics, but not for dump files or
51 __PRETTY_FUNCTION. */
52 #define M_(msgid) (pp_translate_identifiers (cxx_pp) ? _(msgid) : (msgid))
53
54 # define NEXT_CODE(T) (TREE_CODE (TREE_TYPE (T)))
55
56 static const char *args_to_string (tree, int);
57 static const char *code_to_string (enum tree_code);
58 static const char *cv_to_string (tree, int);
59 static const char *decl_to_string (tree, int);
60 static const char *fndecl_to_string (tree, int);
61 static const char *op_to_string (bool, enum tree_code);
62 static const char *parm_to_string (int);
63 static const char *type_to_string (tree, int, bool, bool *, bool);
64
65 static void dump_alias_template_specialization (cxx_pretty_printer *, tree, int);
66 static void dump_type (cxx_pretty_printer *, tree, int);
67 static void dump_typename (cxx_pretty_printer *, tree, int);
68 static void dump_simple_decl (cxx_pretty_printer *, tree, tree, int);
69 static void dump_decl (cxx_pretty_printer *, tree, int);
70 static void dump_template_decl (cxx_pretty_printer *, tree, int);
71 static void dump_function_decl (cxx_pretty_printer *, tree, int);
72 static void dump_expr (cxx_pretty_printer *, tree, int);
73 static void dump_unary_op (cxx_pretty_printer *, const char *, tree, int);
74 static void dump_binary_op (cxx_pretty_printer *, const char *, tree, int);
75 static void dump_aggr_type (cxx_pretty_printer *, tree, int);
76 static void dump_type_prefix (cxx_pretty_printer *, tree, int);
77 static void dump_type_suffix (cxx_pretty_printer *, tree, int);
78 static void dump_function_name (cxx_pretty_printer *, tree, int);
79 static void dump_call_expr_args (cxx_pretty_printer *, tree, int, bool);
80 static void dump_aggr_init_expr_args (cxx_pretty_printer *, tree, int, bool);
81 static void dump_expr_list (cxx_pretty_printer *, tree, int);
82 static void dump_global_iord (cxx_pretty_printer *, tree);
83 static void dump_parameters (cxx_pretty_printer *, tree, int);
84 static void dump_ref_qualifier (cxx_pretty_printer *, tree, int);
85 static void dump_exception_spec (cxx_pretty_printer *, tree, int);
86 static void dump_template_argument (cxx_pretty_printer *, tree, int);
87 static void dump_template_argument_list (cxx_pretty_printer *, tree, int);
88 static void dump_template_parameter (cxx_pretty_printer *, tree, int);
89 static void dump_template_bindings (cxx_pretty_printer *, tree, tree,
90 vec<tree, va_gc> *);
91 static void dump_scope (cxx_pretty_printer *, tree, int);
92 static void dump_template_parms (cxx_pretty_printer *, tree, int, int);
93 static int get_non_default_template_args_count (tree, int);
94 static const char *function_category (tree);
95 static void maybe_print_constexpr_context (diagnostic_context *);
96 static void maybe_print_instantiation_context (diagnostic_context *);
97 static void print_instantiation_full_context (diagnostic_context *);
98 static void print_instantiation_partial_context (diagnostic_context *,
99 struct tinst_level *,
100 location_t);
101 static void cp_diagnostic_starter (diagnostic_context *, diagnostic_info *);
102 static void cp_print_error_function (diagnostic_context *, diagnostic_info *);
103
104 static bool cp_printer (pretty_printer *, text_info *, const char *,
105 int, bool, bool, bool, bool *, const char **);
106
107 /* Struct for handling %H or %I, which require delaying printing the
108 type until a postprocessing stage. */
109
110 struct deferred_printed_type
111 {
112 deferred_printed_type ()
113 : m_tree (NULL_TREE), m_buffer_ptr (NULL), m_verbose (false), m_quote (false)
114 {}
115
116 deferred_printed_type (tree type, const char **buffer_ptr, bool verbose,
117 bool quote)
118 : m_tree (type), m_buffer_ptr (buffer_ptr), m_verbose (verbose),
119 m_quote (quote)
120 {
121 gcc_assert (type);
122 gcc_assert (buffer_ptr);
123 }
124
125 /* The tree is not GTY-marked: they are only non-NULL within a
126 call to pp_format. */
127 tree m_tree;
128 const char **m_buffer_ptr;
129 bool m_verbose;
130 bool m_quote;
131 };
132
133 /* Subclass of format_postprocessor for the C++ frontend.
134 This handles the %H and %I formatting codes, printing them
135 in a postprocessing phase (since they affect each other). */
136
137 class cxx_format_postprocessor : public format_postprocessor
138 {
139 public:
140 cxx_format_postprocessor ()
141 : m_type_a (), m_type_b ()
142 {}
143
144 void handle (pretty_printer *pp) FINAL OVERRIDE;
145
146 deferred_printed_type m_type_a;
147 deferred_printed_type m_type_b;
148 };
149
150 /* CONTEXT->printer is a basic pretty printer that was constructed
151 presumably by diagnostic_initialize(), called early in the
152 compiler's initialization process (in general_init) Before the FE
153 is initialized. This (C++) FE-specific diagnostic initializer is
154 thus replacing the basic pretty printer with one that has C++-aware
155 capacities. */
156
157 void
158 cxx_initialize_diagnostics (diagnostic_context *context)
159 {
160 pretty_printer *base = context->printer;
161 cxx_pretty_printer *pp = XNEW (cxx_pretty_printer);
162 context->printer = new (pp) cxx_pretty_printer ();
163
164 /* It is safe to free this object because it was previously XNEW()'d. */
165 base->~pretty_printer ();
166 XDELETE (base);
167
168 c_common_diagnostics_set_defaults (context);
169 diagnostic_starter (context) = cp_diagnostic_starter;
170 /* diagnostic_finalizer is already c_diagnostic_finalizer. */
171 diagnostic_format_decoder (context) = cp_printer;
172 pp->m_format_postprocessor = new cxx_format_postprocessor ();
173 }
174
175 /* Dump a scope, if deemed necessary. */
176
177 static void
178 dump_scope (cxx_pretty_printer *pp, tree scope, int flags)
179 {
180 int f = flags & (TFF_SCOPE | TFF_CHASE_TYPEDEF);
181
182 if (scope == NULL_TREE)
183 return;
184
185 /* Enum values within an unscoped enum will be CONST_DECL with an
186 ENUMERAL_TYPE as their "scope". Use CP_TYPE_CONTEXT of the
187 ENUMERAL_TYPE, so as to print any enclosing namespace. */
188 if (UNSCOPED_ENUM_P (scope))
189 scope = CP_TYPE_CONTEXT (scope);
190
191 if (TREE_CODE (scope) == NAMESPACE_DECL)
192 {
193 if (scope != global_namespace)
194 {
195 dump_decl (pp, scope, f);
196 pp_cxx_colon_colon (pp);
197 }
198 }
199 else if (AGGREGATE_TYPE_P (scope)
200 || SCOPED_ENUM_P (scope))
201 {
202 dump_type (pp, scope, f);
203 pp_cxx_colon_colon (pp);
204 }
205 else if ((flags & TFF_SCOPE) && TREE_CODE (scope) == FUNCTION_DECL)
206 {
207 dump_function_decl (pp, scope, f);
208 pp_cxx_colon_colon (pp);
209 }
210 }
211
212 /* Dump the template ARGument under control of FLAGS. */
213
214 static void
215 dump_template_argument (cxx_pretty_printer *pp, tree arg, int flags)
216 {
217 if (ARGUMENT_PACK_P (arg))
218 dump_template_argument_list (pp, ARGUMENT_PACK_ARGS (arg),
219 /* No default args in argument packs. */
220 flags|TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS);
221 else if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
222 dump_type (pp, arg, flags & ~TFF_CLASS_KEY_OR_ENUM);
223 else
224 {
225 if (TREE_CODE (arg) == TREE_LIST)
226 arg = TREE_VALUE (arg);
227
228 /* Strip implicit conversions. */
229 while (CONVERT_EXPR_P (arg))
230 arg = TREE_OPERAND (arg, 0);
231
232 dump_expr (pp, arg, (flags | TFF_EXPR_IN_PARENS) & ~TFF_CLASS_KEY_OR_ENUM);
233 }
234 }
235
236 /* Count the number of template arguments ARGS whose value does not
237 match the (optional) default template parameter in PARAMS */
238
239 static int
240 get_non_default_template_args_count (tree args, int flags)
241 {
242 int n = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_ARGS (args));
243
244 if (/* We use this flag when generating debug information. We don't
245 want to expand templates at this point, for this may generate
246 new decls, which gets decl counts out of sync, which may in
247 turn cause codegen differences between compilations with and
248 without -g. */
249 (flags & TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS) != 0
250 || !flag_pretty_templates)
251 return n;
252
253 return GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (INNERMOST_TEMPLATE_ARGS (args));
254 }
255
256 /* Dump a template-argument-list ARGS (always a TREE_VEC) under control
257 of FLAGS. */
258
259 static void
260 dump_template_argument_list (cxx_pretty_printer *pp, tree args, int flags)
261 {
262 int n = get_non_default_template_args_count (args, flags);
263 int need_comma = 0;
264 int i;
265
266 for (i = 0; i < n; ++i)
267 {
268 tree arg = TREE_VEC_ELT (args, i);
269
270 /* Only print a comma if we know there is an argument coming. In
271 the case of an empty template argument pack, no actual
272 argument will be printed. */
273 if (need_comma
274 && (!ARGUMENT_PACK_P (arg)
275 || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) > 0))
276 pp_separate_with_comma (pp);
277
278 dump_template_argument (pp, arg, flags);
279 need_comma = 1;
280 }
281 }
282
283 /* Dump a template parameter PARM (a TREE_LIST) under control of FLAGS. */
284
285 static void
286 dump_template_parameter (cxx_pretty_printer *pp, tree parm, int flags)
287 {
288 tree p;
289 tree a;
290
291 if (parm == error_mark_node)
292 return;
293
294 p = TREE_VALUE (parm);
295 a = TREE_PURPOSE (parm);
296
297 if (TREE_CODE (p) == TYPE_DECL)
298 {
299 if (flags & TFF_DECL_SPECIFIERS)
300 {
301 pp_cxx_ws_string (pp, "class");
302 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (p)))
303 pp_cxx_ws_string (pp, "...");
304 if (DECL_NAME (p))
305 pp_cxx_tree_identifier (pp, DECL_NAME (p));
306 }
307 else if (DECL_NAME (p))
308 pp_cxx_tree_identifier (pp, DECL_NAME (p));
309 else
310 pp_cxx_canonical_template_parameter (pp, TREE_TYPE (p));
311 }
312 else
313 dump_decl (pp, p, flags | TFF_DECL_SPECIFIERS);
314
315 if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && a != NULL_TREE)
316 {
317 pp_cxx_whitespace (pp);
318 pp_equal (pp);
319 pp_cxx_whitespace (pp);
320 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
321 dump_type (pp, a, flags & ~TFF_CHASE_TYPEDEF);
322 else
323 dump_expr (pp, a, flags | TFF_EXPR_IN_PARENS);
324 }
325 }
326
327 /* Dump, under control of FLAGS, a template-parameter-list binding.
328 PARMS is a TREE_LIST of TREE_VEC of TREE_LIST and ARGS is a
329 TREE_VEC. */
330
331 static void
332 dump_template_bindings (cxx_pretty_printer *pp, tree parms, tree args,
333 vec<tree, va_gc> *typenames)
334 {
335 bool need_semicolon = false;
336 int i;
337 tree t;
338
339 while (parms)
340 {
341 tree p = TREE_VALUE (parms);
342 int lvl = TMPL_PARMS_DEPTH (parms);
343 int arg_idx = 0;
344 int i;
345 tree lvl_args = NULL_TREE;
346
347 /* Don't crash if we had an invalid argument list. */
348 if (TMPL_ARGS_DEPTH (args) >= lvl)
349 lvl_args = TMPL_ARGS_LEVEL (args, lvl);
350
351 for (i = 0; i < TREE_VEC_LENGTH (p); ++i)
352 {
353 tree arg = NULL_TREE;
354
355 /* Don't crash if we had an invalid argument list. */
356 if (lvl_args && NUM_TMPL_ARGS (lvl_args) > arg_idx)
357 arg = TREE_VEC_ELT (lvl_args, arg_idx);
358
359 if (need_semicolon)
360 pp_separate_with_semicolon (pp);
361 dump_template_parameter (pp, TREE_VEC_ELT (p, i),
362 TFF_PLAIN_IDENTIFIER);
363 pp_cxx_whitespace (pp);
364 pp_equal (pp);
365 pp_cxx_whitespace (pp);
366 if (arg)
367 {
368 if (ARGUMENT_PACK_P (arg))
369 pp_cxx_left_brace (pp);
370 dump_template_argument (pp, arg, TFF_PLAIN_IDENTIFIER);
371 if (ARGUMENT_PACK_P (arg))
372 pp_cxx_right_brace (pp);
373 }
374 else
375 pp_string (pp, M_("<missing>"));
376
377 ++arg_idx;
378 need_semicolon = true;
379 }
380
381 parms = TREE_CHAIN (parms);
382 }
383
384 /* Don't bother with typenames for a partial instantiation. */
385 if (vec_safe_is_empty (typenames) || uses_template_parms (args))
386 return;
387
388 /* Don't try to print typenames when we're processing a clone. */
389 if (current_function_decl
390 && !DECL_LANG_SPECIFIC (current_function_decl))
391 return;
392
393 /* Don't try to do this once cgraph starts throwing away front-end
394 information. */
395 if (at_eof >= 2)
396 return;
397
398 FOR_EACH_VEC_SAFE_ELT (typenames, i, t)
399 {
400 if (need_semicolon)
401 pp_separate_with_semicolon (pp);
402 dump_type (pp, t, TFF_PLAIN_IDENTIFIER);
403 pp_cxx_whitespace (pp);
404 pp_equal (pp);
405 pp_cxx_whitespace (pp);
406 push_deferring_access_checks (dk_no_check);
407 t = tsubst (t, args, tf_none, NULL_TREE);
408 pop_deferring_access_checks ();
409 /* Strip typedefs. We can't just use TFF_CHASE_TYPEDEF because
410 pp_simple_type_specifier doesn't know about it. */
411 t = strip_typedefs (t);
412 dump_type (pp, t, TFF_PLAIN_IDENTIFIER);
413 }
414 }
415
416 /* Dump a human-readable equivalent of the alias template
417 specialization of T. */
418
419 static void
420 dump_alias_template_specialization (cxx_pretty_printer *pp, tree t, int flags)
421 {
422 gcc_assert (alias_template_specialization_p (t));
423
424 tree decl = TYPE_NAME (t);
425 if (!(flags & TFF_UNQUALIFIED_NAME))
426 dump_scope (pp, CP_DECL_CONTEXT (decl), flags);
427 pp_cxx_tree_identifier (pp, DECL_NAME (decl));
428 dump_template_parms (pp, DECL_TEMPLATE_INFO (decl),
429 /*primary=*/false,
430 flags & ~TFF_TEMPLATE_HEADER);
431 }
432
433 /* Dump a human-readable equivalent of TYPE. FLAGS controls the
434 format. */
435
436 static void
437 dump_type (cxx_pretty_printer *pp, tree t, int flags)
438 {
439 if (t == NULL_TREE)
440 return;
441
442 /* Don't print e.g. "struct mytypedef". */
443 if (TYPE_P (t) && typedef_variant_p (t))
444 {
445 tree decl = TYPE_NAME (t);
446 if ((flags & TFF_CHASE_TYPEDEF)
447 || DECL_SELF_REFERENCE_P (decl)
448 || (!flag_pretty_templates
449 && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)))
450 t = strip_typedefs (t);
451 else if (alias_template_specialization_p (t))
452 {
453 dump_alias_template_specialization (pp, t, flags);
454 return;
455 }
456 else if (same_type_p (t, TREE_TYPE (decl)))
457 t = decl;
458 else
459 {
460 pp_cxx_cv_qualifier_seq (pp, t);
461 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
462 return;
463 }
464 }
465
466 if (TYPE_PTRMEMFUNC_P (t))
467 goto offset_type;
468
469 switch (TREE_CODE (t))
470 {
471 case LANG_TYPE:
472 if (t == init_list_type_node)
473 pp_string (pp, M_("<brace-enclosed initializer list>"));
474 else if (t == unknown_type_node)
475 pp_string (pp, M_("<unresolved overloaded function type>"));
476 else
477 {
478 pp_cxx_cv_qualifier_seq (pp, t);
479 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
480 }
481 break;
482
483 case TREE_LIST:
484 /* A list of function parms. */
485 dump_parameters (pp, t, flags);
486 break;
487
488 case IDENTIFIER_NODE:
489 pp_cxx_tree_identifier (pp, t);
490 break;
491
492 case TREE_BINFO:
493 dump_type (pp, BINFO_TYPE (t), flags);
494 break;
495
496 case RECORD_TYPE:
497 case UNION_TYPE:
498 case ENUMERAL_TYPE:
499 dump_aggr_type (pp, t, flags);
500 break;
501
502 case TYPE_DECL:
503 if (flags & TFF_CHASE_TYPEDEF)
504 {
505 dump_type (pp, DECL_ORIGINAL_TYPE (t)
506 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t), flags);
507 break;
508 }
509 /* Fall through. */
510
511 case TEMPLATE_DECL:
512 case NAMESPACE_DECL:
513 dump_decl (pp, t, flags & ~TFF_DECL_SPECIFIERS);
514 break;
515
516 case INTEGER_TYPE:
517 case REAL_TYPE:
518 case VOID_TYPE:
519 case BOOLEAN_TYPE:
520 case COMPLEX_TYPE:
521 case VECTOR_TYPE:
522 case FIXED_POINT_TYPE:
523 pp_type_specifier_seq (pp, t);
524 break;
525
526 case TEMPLATE_TEMPLATE_PARM:
527 /* For parameters inside template signature. */
528 if (TYPE_IDENTIFIER (t))
529 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
530 else
531 pp_cxx_canonical_template_parameter (pp, t);
532 break;
533
534 case BOUND_TEMPLATE_TEMPLATE_PARM:
535 {
536 tree args = TYPE_TI_ARGS (t);
537 pp_cxx_cv_qualifier_seq (pp, t);
538 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
539 pp_cxx_begin_template_argument_list (pp);
540 dump_template_argument_list (pp, args, flags);
541 pp_cxx_end_template_argument_list (pp);
542 }
543 break;
544
545 case TEMPLATE_TYPE_PARM:
546 pp_cxx_cv_qualifier_seq (pp, t);
547 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
548 pp_cxx_constrained_type_spec (pp, c);
549 else if (TYPE_IDENTIFIER (t))
550 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
551 else
552 pp_cxx_canonical_template_parameter
553 (pp, TEMPLATE_TYPE_PARM_INDEX (t));
554 break;
555
556 /* This is not always necessary for pointers and such, but doing this
557 reduces code size. */
558 case ARRAY_TYPE:
559 case POINTER_TYPE:
560 case REFERENCE_TYPE:
561 case OFFSET_TYPE:
562 offset_type:
563 case FUNCTION_TYPE:
564 case METHOD_TYPE:
565 {
566 dump_type_prefix (pp, t, flags);
567 dump_type_suffix (pp, t, flags);
568 break;
569 }
570 case TYPENAME_TYPE:
571 if (! (flags & TFF_CHASE_TYPEDEF)
572 && DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
573 {
574 dump_decl (pp, TYPE_NAME (t), TFF_PLAIN_IDENTIFIER);
575 break;
576 }
577 pp_cxx_cv_qualifier_seq (pp, t);
578 pp_cxx_ws_string (pp,
579 TYPENAME_IS_ENUM_P (t) ? "enum"
580 : TYPENAME_IS_CLASS_P (t) ? "class"
581 : "typename");
582 dump_typename (pp, t, flags);
583 break;
584
585 case UNBOUND_CLASS_TEMPLATE:
586 if (! (flags & TFF_UNQUALIFIED_NAME))
587 {
588 dump_type (pp, TYPE_CONTEXT (t), flags);
589 pp_cxx_colon_colon (pp);
590 }
591 pp_cxx_ws_string (pp, "template");
592 dump_type (pp, TYPE_IDENTIFIER (t), flags);
593 break;
594
595 case TYPEOF_TYPE:
596 pp_cxx_ws_string (pp, "__typeof__");
597 pp_cxx_whitespace (pp);
598 pp_cxx_left_paren (pp);
599 dump_expr (pp, TYPEOF_TYPE_EXPR (t), flags & ~TFF_EXPR_IN_PARENS);
600 pp_cxx_right_paren (pp);
601 break;
602
603 case UNDERLYING_TYPE:
604 pp_cxx_ws_string (pp, "__underlying_type");
605 pp_cxx_whitespace (pp);
606 pp_cxx_left_paren (pp);
607 dump_expr (pp, UNDERLYING_TYPE_TYPE (t), flags & ~TFF_EXPR_IN_PARENS);
608 pp_cxx_right_paren (pp);
609 break;
610
611 case TYPE_PACK_EXPANSION:
612 dump_type (pp, PACK_EXPANSION_PATTERN (t), flags);
613 pp_cxx_ws_string (pp, "...");
614 break;
615
616 case TYPE_ARGUMENT_PACK:
617 dump_template_argument (pp, t, flags);
618 break;
619
620 case DECLTYPE_TYPE:
621 pp_cxx_ws_string (pp, "decltype");
622 pp_cxx_whitespace (pp);
623 pp_cxx_left_paren (pp);
624 dump_expr (pp, DECLTYPE_TYPE_EXPR (t), flags & ~TFF_EXPR_IN_PARENS);
625 pp_cxx_right_paren (pp);
626 break;
627
628 case NULLPTR_TYPE:
629 pp_string (pp, "std::nullptr_t");
630 break;
631
632 default:
633 pp_unsupported_tree (pp, t);
634 /* Fall through. */
635
636 case ERROR_MARK:
637 pp_string (pp, M_("<type error>"));
638 break;
639 }
640 }
641
642 /* Dump a TYPENAME_TYPE. We need to notice when the context is itself
643 a TYPENAME_TYPE. */
644
645 static void
646 dump_typename (cxx_pretty_printer *pp, tree t, int flags)
647 {
648 tree ctx = TYPE_CONTEXT (t);
649
650 if (TREE_CODE (ctx) == TYPENAME_TYPE)
651 dump_typename (pp, ctx, flags);
652 else
653 dump_type (pp, ctx, flags & ~TFF_CLASS_KEY_OR_ENUM);
654 pp_cxx_colon_colon (pp);
655 dump_decl (pp, TYPENAME_TYPE_FULLNAME (t), flags);
656 }
657
658 /* Return the name of the supplied aggregate, or enumeral type. */
659
660 const char *
661 class_key_or_enum_as_string (tree t)
662 {
663 if (TREE_CODE (t) == ENUMERAL_TYPE)
664 {
665 if (SCOPED_ENUM_P (t))
666 return "enum class";
667 else
668 return "enum";
669 }
670 else if (TREE_CODE (t) == UNION_TYPE)
671 return "union";
672 else if (TYPE_LANG_SPECIFIC (t) && CLASSTYPE_DECLARED_CLASS (t))
673 return "class";
674 else
675 return "struct";
676 }
677
678 /* Print out a class declaration T under the control of FLAGS,
679 in the form `class foo'. */
680
681 static void
682 dump_aggr_type (cxx_pretty_printer *pp, tree t, int flags)
683 {
684 tree name;
685 const char *variety = class_key_or_enum_as_string (t);
686 int typdef = 0;
687 int tmplate = 0;
688
689 pp_cxx_cv_qualifier_seq (pp, t);
690
691 if (flags & TFF_CLASS_KEY_OR_ENUM)
692 pp_cxx_ws_string (pp, variety);
693
694 name = TYPE_NAME (t);
695
696 if (name)
697 {
698 typdef = (!DECL_ARTIFICIAL (name)
699 /* An alias specialization is not considered to be a
700 typedef. */
701 && !alias_template_specialization_p (t));
702
703 if ((typdef
704 && ((flags & TFF_CHASE_TYPEDEF)
705 || (!flag_pretty_templates && DECL_LANG_SPECIFIC (name)
706 && DECL_TEMPLATE_INFO (name))))
707 || DECL_SELF_REFERENCE_P (name))
708 {
709 t = TYPE_MAIN_VARIANT (t);
710 name = TYPE_NAME (t);
711 typdef = 0;
712 }
713
714 tmplate = !typdef && TREE_CODE (t) != ENUMERAL_TYPE
715 && TYPE_LANG_SPECIFIC (t) && CLASSTYPE_TEMPLATE_INFO (t)
716 && (TREE_CODE (CLASSTYPE_TI_TEMPLATE (t)) != TEMPLATE_DECL
717 || PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
718
719 if (! (flags & TFF_UNQUALIFIED_NAME))
720 dump_scope (pp, CP_DECL_CONTEXT (name), flags | TFF_SCOPE);
721 flags &= ~TFF_UNQUALIFIED_NAME;
722 if (tmplate)
723 {
724 /* Because the template names are mangled, we have to locate
725 the most general template, and use that name. */
726 tree tpl = TYPE_TI_TEMPLATE (t);
727
728 while (DECL_TEMPLATE_INFO (tpl))
729 tpl = DECL_TI_TEMPLATE (tpl);
730 name = tpl;
731 }
732 name = DECL_NAME (name);
733 }
734
735 if (name == 0 || anon_aggrname_p (name))
736 {
737 if (flags & TFF_CLASS_KEY_OR_ENUM)
738 pp_string (pp, M_("<unnamed>"));
739 else
740 pp_printf (pp, M_("<unnamed %s>"), variety);
741 }
742 else if (LAMBDA_TYPE_P (t))
743 {
744 /* A lambda's "type" is essentially its signature. */
745 pp_string (pp, M_("<lambda"));
746 if (lambda_function (t))
747 dump_parameters (pp,
748 FUNCTION_FIRST_USER_PARMTYPE (lambda_function (t)),
749 flags);
750 pp_greater (pp);
751 }
752 else
753 pp_cxx_tree_identifier (pp, name);
754 if (tmplate)
755 dump_template_parms (pp, TYPE_TEMPLATE_INFO (t),
756 !CLASSTYPE_USE_TEMPLATE (t),
757 flags & ~TFF_TEMPLATE_HEADER);
758 }
759
760 /* Dump into the obstack the initial part of the output for a given type.
761 This is necessary when dealing with things like functions returning
762 functions. Examples:
763
764 return type of `int (* fee ())()': pointer -> function -> int. Both
765 pointer (and reference and offset) and function (and member) types must
766 deal with prefix and suffix.
767
768 Arrays must also do this for DECL nodes, like int a[], and for things like
769 int *[]&. */
770
771 static void
772 dump_type_prefix (cxx_pretty_printer *pp, tree t, int flags)
773 {
774 if (TYPE_PTRMEMFUNC_P (t))
775 {
776 t = TYPE_PTRMEMFUNC_FN_TYPE (t);
777 goto offset_type;
778 }
779
780 switch (TREE_CODE (t))
781 {
782 case POINTER_TYPE:
783 case REFERENCE_TYPE:
784 {
785 tree sub = TREE_TYPE (t);
786
787 dump_type_prefix (pp, sub, flags);
788 if (TREE_CODE (sub) == ARRAY_TYPE
789 || TREE_CODE (sub) == FUNCTION_TYPE)
790 {
791 pp_cxx_whitespace (pp);
792 pp_cxx_left_paren (pp);
793 pp_c_attributes_display (pp, TYPE_ATTRIBUTES (sub));
794 }
795 if (TYPE_PTR_P (t))
796 pp_star (pp);
797 else if (TYPE_REF_P (t))
798 {
799 if (TYPE_REF_IS_RVALUE (t))
800 pp_ampersand_ampersand (pp);
801 else
802 pp_ampersand (pp);
803 }
804 pp->padding = pp_before;
805 pp_cxx_cv_qualifier_seq (pp, t);
806 }
807 break;
808
809 case OFFSET_TYPE:
810 offset_type:
811 dump_type_prefix (pp, TREE_TYPE (t), flags);
812 if (TREE_CODE (t) == OFFSET_TYPE) /* pmfs deal with this in d_t_p */
813 {
814 pp_maybe_space (pp);
815 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
816 pp_cxx_left_paren (pp);
817 dump_type (pp, TYPE_OFFSET_BASETYPE (t), flags);
818 pp_cxx_colon_colon (pp);
819 }
820 pp_cxx_star (pp);
821 pp_cxx_cv_qualifier_seq (pp, t);
822 pp->padding = pp_before;
823 break;
824
825 /* This can be reached without a pointer when dealing with
826 templates, e.g. std::is_function. */
827 case FUNCTION_TYPE:
828 dump_type_prefix (pp, TREE_TYPE (t), flags);
829 break;
830
831 case METHOD_TYPE:
832 dump_type_prefix (pp, TREE_TYPE (t), flags);
833 pp_maybe_space (pp);
834 pp_cxx_left_paren (pp);
835 dump_aggr_type (pp, TYPE_METHOD_BASETYPE (t), flags);
836 pp_cxx_colon_colon (pp);
837 break;
838
839 case ARRAY_TYPE:
840 dump_type_prefix (pp, TREE_TYPE (t), flags);
841 break;
842
843 case ENUMERAL_TYPE:
844 case IDENTIFIER_NODE:
845 case INTEGER_TYPE:
846 case BOOLEAN_TYPE:
847 case REAL_TYPE:
848 case RECORD_TYPE:
849 case TEMPLATE_TYPE_PARM:
850 case TEMPLATE_TEMPLATE_PARM:
851 case BOUND_TEMPLATE_TEMPLATE_PARM:
852 case TREE_LIST:
853 case TYPE_DECL:
854 case TREE_VEC:
855 case UNION_TYPE:
856 case LANG_TYPE:
857 case VOID_TYPE:
858 case TYPENAME_TYPE:
859 case COMPLEX_TYPE:
860 case VECTOR_TYPE:
861 case TYPEOF_TYPE:
862 case UNDERLYING_TYPE:
863 case DECLTYPE_TYPE:
864 case TYPE_PACK_EXPANSION:
865 case FIXED_POINT_TYPE:
866 case NULLPTR_TYPE:
867 dump_type (pp, t, flags);
868 pp->padding = pp_before;
869 break;
870
871 default:
872 pp_unsupported_tree (pp, t);
873 /* fall through. */
874 case ERROR_MARK:
875 pp_string (pp, M_("<typeprefixerror>"));
876 break;
877 }
878 }
879
880 /* Dump the suffix of type T, under control of FLAGS. This is the part
881 which appears after the identifier (or function parms). */
882
883 static void
884 dump_type_suffix (cxx_pretty_printer *pp, tree t, int flags)
885 {
886 if (TYPE_PTRMEMFUNC_P (t))
887 t = TYPE_PTRMEMFUNC_FN_TYPE (t);
888
889 switch (TREE_CODE (t))
890 {
891 case POINTER_TYPE:
892 case REFERENCE_TYPE:
893 case OFFSET_TYPE:
894 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
895 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
896 pp_cxx_right_paren (pp);
897 if (TREE_CODE (t) == POINTER_TYPE)
898 flags |= TFF_POINTER;
899 dump_type_suffix (pp, TREE_TYPE (t), flags);
900 break;
901
902 case FUNCTION_TYPE:
903 case METHOD_TYPE:
904 {
905 tree arg;
906 if (TREE_CODE (t) == METHOD_TYPE)
907 /* Can only be reached through a pointer. */
908 pp_cxx_right_paren (pp);
909 arg = TYPE_ARG_TYPES (t);
910 if (TREE_CODE (t) == METHOD_TYPE)
911 arg = TREE_CHAIN (arg);
912
913 /* Function pointers don't have default args. Not in standard C++,
914 anyway; they may in g++, but we'll just pretend otherwise. */
915 dump_parameters (pp, arg, flags & ~TFF_FUNCTION_DEFAULT_ARGUMENTS);
916
917 pp->padding = pp_before;
918 pp_cxx_cv_qualifiers (pp, type_memfn_quals (t),
919 TREE_CODE (t) == FUNCTION_TYPE
920 && (flags & TFF_POINTER));
921 dump_ref_qualifier (pp, t, flags);
922 if (tx_safe_fn_type_p (t))
923 pp_cxx_ws_string (pp, "transaction_safe");
924 dump_exception_spec (pp, TYPE_RAISES_EXCEPTIONS (t), flags);
925 dump_type_suffix (pp, TREE_TYPE (t), flags);
926 break;
927 }
928
929 case ARRAY_TYPE:
930 pp_maybe_space (pp);
931 pp_cxx_left_bracket (pp);
932 if (tree dtype = TYPE_DOMAIN (t))
933 {
934 tree max = TYPE_MAX_VALUE (dtype);
935 /* Zero-length arrays have an upper bound of SIZE_MAX. */
936 if (integer_all_onesp (max))
937 pp_character (pp, '0');
938 else if (tree_fits_shwi_p (max))
939 pp_wide_integer (pp, tree_to_shwi (max) + 1);
940 else
941 {
942 STRIP_NOPS (max);
943 if (TREE_CODE (max) == SAVE_EXPR)
944 max = TREE_OPERAND (max, 0);
945 if (TREE_CODE (max) == MINUS_EXPR
946 || TREE_CODE (max) == PLUS_EXPR)
947 {
948 max = TREE_OPERAND (max, 0);
949 while (CONVERT_EXPR_P (max))
950 max = TREE_OPERAND (max, 0);
951 }
952 else
953 max = fold_build2_loc (input_location,
954 PLUS_EXPR, dtype, max,
955 build_int_cst (dtype, 1));
956 dump_expr (pp, max, flags & ~TFF_EXPR_IN_PARENS);
957 }
958 }
959 pp_cxx_right_bracket (pp);
960 dump_type_suffix (pp, TREE_TYPE (t), flags);
961 break;
962
963 case ENUMERAL_TYPE:
964 case IDENTIFIER_NODE:
965 case INTEGER_TYPE:
966 case BOOLEAN_TYPE:
967 case REAL_TYPE:
968 case RECORD_TYPE:
969 case TEMPLATE_TYPE_PARM:
970 case TEMPLATE_TEMPLATE_PARM:
971 case BOUND_TEMPLATE_TEMPLATE_PARM:
972 case TREE_LIST:
973 case TYPE_DECL:
974 case TREE_VEC:
975 case UNION_TYPE:
976 case LANG_TYPE:
977 case VOID_TYPE:
978 case TYPENAME_TYPE:
979 case COMPLEX_TYPE:
980 case VECTOR_TYPE:
981 case TYPEOF_TYPE:
982 case UNDERLYING_TYPE:
983 case DECLTYPE_TYPE:
984 case TYPE_PACK_EXPANSION:
985 case FIXED_POINT_TYPE:
986 case NULLPTR_TYPE:
987 break;
988
989 default:
990 pp_unsupported_tree (pp, t);
991 case ERROR_MARK:
992 /* Don't mark it here, we should have already done in
993 dump_type_prefix. */
994 break;
995 }
996 }
997
998 static void
999 dump_global_iord (cxx_pretty_printer *pp, tree t)
1000 {
1001 const char *p = NULL;
1002
1003 if (DECL_GLOBAL_CTOR_P (t))
1004 p = M_("(static initializers for %s)");
1005 else if (DECL_GLOBAL_DTOR_P (t))
1006 p = M_("(static destructors for %s)");
1007 else
1008 gcc_unreachable ();
1009
1010 pp_printf (pp, p, DECL_SOURCE_FILE (t));
1011 }
1012
1013 static void
1014 dump_simple_decl (cxx_pretty_printer *pp, tree t, tree type, int flags)
1015 {
1016 if (template_parm_object_p (t))
1017 return dump_expr (pp, DECL_INITIAL (t), flags);
1018
1019 if (flags & TFF_DECL_SPECIFIERS)
1020 {
1021 if (VAR_P (t) && DECL_DECLARED_CONSTEXPR_P (t))
1022 {
1023 if (DECL_LANG_SPECIFIC (t) && DECL_DECLARED_CONCEPT_P (t))
1024 pp_cxx_ws_string (pp, "concept");
1025 else
1026 pp_cxx_ws_string (pp, "constexpr");
1027 }
1028 dump_type_prefix (pp, type, flags & ~TFF_UNQUALIFIED_NAME);
1029 pp_maybe_space (pp);
1030 }
1031 if (! (flags & TFF_UNQUALIFIED_NAME)
1032 && TREE_CODE (t) != PARM_DECL
1033 && (!DECL_INITIAL (t)
1034 || TREE_CODE (DECL_INITIAL (t)) != TEMPLATE_PARM_INDEX))
1035 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
1036 flags &= ~TFF_UNQUALIFIED_NAME;
1037 if ((flags & TFF_DECL_SPECIFIERS)
1038 && DECL_TEMPLATE_PARM_P (t)
1039 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (t)))
1040 pp_string (pp, "...");
1041 if (DECL_NAME (t))
1042 {
1043 if (TREE_CODE (t) == FIELD_DECL && DECL_NORMAL_CAPTURE_P (t))
1044 {
1045 pp_less (pp);
1046 pp_string (pp, IDENTIFIER_POINTER (DECL_NAME (t)) + 2);
1047 pp_string (pp, " capture>");
1048 }
1049 else
1050 dump_decl (pp, DECL_NAME (t), flags);
1051 }
1052 else if (DECL_DECOMPOSITION_P (t))
1053 pp_string (pp, M_("<structured bindings>"));
1054 else
1055 pp_string (pp, M_("<anonymous>"));
1056 if (flags & TFF_DECL_SPECIFIERS)
1057 dump_type_suffix (pp, type, flags);
1058 }
1059
1060 /* Print an IDENTIFIER_NODE that is the name of a declaration. */
1061
1062 static void
1063 dump_decl_name (cxx_pretty_printer *pp, tree t, int flags)
1064 {
1065 /* These special cases are duplicated here so that other functions
1066 can feed identifiers to error and get them demangled properly. */
1067 if (IDENTIFIER_CONV_OP_P (t))
1068 {
1069 pp_cxx_ws_string (pp, "operator");
1070 /* Not exactly IDENTIFIER_TYPE_VALUE. */
1071 dump_type (pp, TREE_TYPE (t), flags);
1072 return;
1073 }
1074 if (dguide_name_p (t))
1075 {
1076 dump_decl (pp, CLASSTYPE_TI_TEMPLATE (TREE_TYPE (t)),
1077 TFF_UNQUALIFIED_NAME);
1078 return;
1079 }
1080
1081 const char *str = IDENTIFIER_POINTER (t);
1082 if (!strncmp (str, "_ZGR", 3))
1083 {
1084 pp_cxx_ws_string (pp, "<temporary>");
1085 return;
1086 }
1087
1088 pp_cxx_tree_identifier (pp, t);
1089 }
1090
1091 /* Dump a human readable string for the decl T under control of FLAGS. */
1092
1093 static void
1094 dump_decl (cxx_pretty_printer *pp, tree t, int flags)
1095 {
1096 if (t == NULL_TREE)
1097 return;
1098
1099 /* If doing Objective-C++, give Objective-C a chance to demangle
1100 Objective-C method names. */
1101 if (c_dialect_objc ())
1102 {
1103 const char *demangled = objc_maybe_printable_name (t, flags);
1104 if (demangled)
1105 {
1106 pp_string (pp, demangled);
1107 return;
1108 }
1109 }
1110
1111 switch (TREE_CODE (t))
1112 {
1113 case TYPE_DECL:
1114 /* Don't say 'typedef class A' */
1115 if (DECL_ARTIFICIAL (t) && !DECL_SELF_REFERENCE_P (t))
1116 {
1117 if ((flags & TFF_DECL_SPECIFIERS)
1118 && TREE_CODE (TREE_TYPE (t)) == TEMPLATE_TYPE_PARM)
1119 {
1120 /* Say `class T' not just `T'. */
1121 pp_cxx_ws_string (pp, "class");
1122
1123 /* Emit the `...' for a parameter pack. */
1124 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
1125 pp_cxx_ws_string (pp, "...");
1126 }
1127
1128 dump_type (pp, TREE_TYPE (t), flags);
1129 break;
1130 }
1131 if (TYPE_DECL_ALIAS_P (t)
1132 && (flags & TFF_DECL_SPECIFIERS
1133 || flags & TFF_CLASS_KEY_OR_ENUM))
1134 {
1135 pp_cxx_ws_string (pp, "using");
1136 dump_decl (pp, DECL_NAME (t), flags);
1137 pp_cxx_whitespace (pp);
1138 pp_cxx_ws_string (pp, "=");
1139 pp_cxx_whitespace (pp);
1140 dump_type (pp, (DECL_ORIGINAL_TYPE (t)
1141 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t)),
1142 flags);
1143 break;
1144 }
1145 if ((flags & TFF_DECL_SPECIFIERS)
1146 && !DECL_SELF_REFERENCE_P (t))
1147 pp_cxx_ws_string (pp, "typedef");
1148 dump_simple_decl (pp, t, DECL_ORIGINAL_TYPE (t)
1149 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t),
1150 flags);
1151 break;
1152
1153 case VAR_DECL:
1154 if (DECL_NAME (t) && VTABLE_NAME_P (DECL_NAME (t)))
1155 {
1156 pp_string (pp, M_("vtable for "));
1157 gcc_assert (TYPE_P (DECL_CONTEXT (t)));
1158 dump_type (pp, DECL_CONTEXT (t), flags);
1159 break;
1160 }
1161 /* Fall through. */
1162 case FIELD_DECL:
1163 case PARM_DECL:
1164 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1165
1166 /* Handle variable template specializations. */
1167 if (VAR_P (t)
1168 && DECL_LANG_SPECIFIC (t)
1169 && DECL_TEMPLATE_INFO (t)
1170 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1171 {
1172 pp_cxx_begin_template_argument_list (pp);
1173 tree args = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1174 dump_template_argument_list (pp, args, flags);
1175 pp_cxx_end_template_argument_list (pp);
1176 }
1177 break;
1178
1179 case RESULT_DECL:
1180 pp_string (pp, M_("<return value> "));
1181 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1182 break;
1183
1184 case NAMESPACE_DECL:
1185 if (flags & TFF_DECL_SPECIFIERS)
1186 pp->declaration (t);
1187 else
1188 {
1189 if (! (flags & TFF_UNQUALIFIED_NAME))
1190 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
1191 flags &= ~TFF_UNQUALIFIED_NAME;
1192 if (DECL_NAME (t) == NULL_TREE)
1193 {
1194 if (!(pp->flags & pp_c_flag_gnu_v3))
1195 pp_cxx_ws_string (pp, M_("{anonymous}"));
1196 else
1197 pp_cxx_ws_string (pp, M_("(anonymous namespace)"));
1198 }
1199 else
1200 pp_cxx_tree_identifier (pp, DECL_NAME (t));
1201 }
1202 break;
1203
1204 case SCOPE_REF:
1205 dump_type (pp, TREE_OPERAND (t, 0), flags);
1206 pp_cxx_colon_colon (pp);
1207 dump_decl (pp, TREE_OPERAND (t, 1), TFF_UNQUALIFIED_NAME);
1208 break;
1209
1210 case ARRAY_REF:
1211 dump_decl (pp, TREE_OPERAND (t, 0), flags);
1212 pp_cxx_left_bracket (pp);
1213 dump_decl (pp, TREE_OPERAND (t, 1), flags);
1214 pp_cxx_right_bracket (pp);
1215 break;
1216
1217 /* So that we can do dump_decl on an aggr type. */
1218 case RECORD_TYPE:
1219 case UNION_TYPE:
1220 case ENUMERAL_TYPE:
1221 dump_type (pp, t, flags);
1222 break;
1223
1224 case BIT_NOT_EXPR:
1225 /* This is a pseudo destructor call which has not been folded into
1226 a PSEUDO_DTOR_EXPR yet. */
1227 pp_cxx_complement (pp);
1228 dump_type (pp, TREE_OPERAND (t, 0), flags);
1229 break;
1230
1231 case TYPE_EXPR:
1232 gcc_unreachable ();
1233 break;
1234
1235 case IDENTIFIER_NODE:
1236 dump_decl_name (pp, t, flags);
1237 break;
1238
1239 case OVERLOAD:
1240 if (!OVL_SINGLE_P (t))
1241 {
1242 tree ctx = ovl_scope (t);
1243 if (ctx != global_namespace)
1244 {
1245 if (TYPE_P (ctx))
1246 dump_type (pp, ctx, flags);
1247 else
1248 dump_decl (pp, ctx, flags);
1249 pp_cxx_colon_colon (pp);
1250 }
1251 dump_decl (pp, OVL_NAME (t), flags);
1252 break;
1253 }
1254
1255 /* If there's only one function, just treat it like an ordinary
1256 FUNCTION_DECL. */
1257 t = OVL_FIRST (t);
1258 /* Fall through. */
1259
1260 case FUNCTION_DECL:
1261 if (! DECL_LANG_SPECIFIC (t))
1262 {
1263 if (DECL_ABSTRACT_ORIGIN (t)
1264 && DECL_ABSTRACT_ORIGIN (t) != t)
1265 dump_decl (pp, DECL_ABSTRACT_ORIGIN (t), flags);
1266 else
1267 dump_function_name (pp, t, flags);
1268 }
1269 else if (DECL_GLOBAL_CTOR_P (t) || DECL_GLOBAL_DTOR_P (t))
1270 dump_global_iord (pp, t);
1271 else
1272 dump_function_decl (pp, t, flags);
1273 break;
1274
1275 case TEMPLATE_DECL:
1276 dump_template_decl (pp, t, flags);
1277 break;
1278
1279 case TEMPLATE_ID_EXPR:
1280 {
1281 tree name = TREE_OPERAND (t, 0);
1282 tree args = TREE_OPERAND (t, 1);
1283
1284 if (!identifier_p (name))
1285 name = OVL_NAME (name);
1286 dump_decl (pp, name, flags);
1287 pp_cxx_begin_template_argument_list (pp);
1288 if (args == error_mark_node)
1289 pp_string (pp, M_("<template arguments error>"));
1290 else if (args)
1291 dump_template_argument_list
1292 (pp, args, flags|TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS);
1293 pp_cxx_end_template_argument_list (pp);
1294 }
1295 break;
1296
1297 case LABEL_DECL:
1298 pp_cxx_tree_identifier (pp, DECL_NAME (t));
1299 break;
1300
1301 case CONST_DECL:
1302 if ((TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == ENUMERAL_TYPE)
1303 || (DECL_INITIAL (t) &&
1304 TREE_CODE (DECL_INITIAL (t)) == TEMPLATE_PARM_INDEX))
1305 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1306 else if (DECL_NAME (t))
1307 dump_decl (pp, DECL_NAME (t), flags);
1308 else if (DECL_INITIAL (t))
1309 dump_expr (pp, DECL_INITIAL (t), flags | TFF_EXPR_IN_PARENS);
1310 else
1311 pp_string (pp, M_("<enumerator>"));
1312 break;
1313
1314 case USING_DECL:
1315 {
1316 pp_cxx_ws_string (pp, "using");
1317 tree scope = USING_DECL_SCOPE (t);
1318 bool variadic = false;
1319 if (PACK_EXPANSION_P (scope))
1320 {
1321 scope = PACK_EXPANSION_PATTERN (scope);
1322 variadic = true;
1323 }
1324 dump_type (pp, scope, flags);
1325 pp_cxx_colon_colon (pp);
1326 dump_decl (pp, DECL_NAME (t), flags);
1327 if (variadic)
1328 pp_cxx_ws_string (pp, "...");
1329 }
1330 break;
1331
1332 case STATIC_ASSERT:
1333 pp->declaration (t);
1334 break;
1335
1336 case BASELINK:
1337 dump_decl (pp, BASELINK_FUNCTIONS (t), flags);
1338 break;
1339
1340 case NON_DEPENDENT_EXPR:
1341 dump_expr (pp, t, flags);
1342 break;
1343
1344 case TEMPLATE_TYPE_PARM:
1345 if (flags & TFF_DECL_SPECIFIERS)
1346 pp->declaration (t);
1347 else
1348 pp->type_id (t);
1349 break;
1350
1351 case UNBOUND_CLASS_TEMPLATE:
1352 case TYPE_PACK_EXPANSION:
1353 case TREE_BINFO:
1354 dump_type (pp, t, flags);
1355 break;
1356
1357 default:
1358 pp_unsupported_tree (pp, t);
1359 /* Fall through. */
1360
1361 case ERROR_MARK:
1362 pp_string (pp, M_("<declaration error>"));
1363 break;
1364 }
1365 }
1366
1367 /* Dump a template declaration T under control of FLAGS. This means the
1368 'template <...> leaders plus the 'class X' or 'void fn(...)' part. */
1369
1370 static void
1371 dump_template_decl (cxx_pretty_printer *pp, tree t, int flags)
1372 {
1373 tree orig_parms = DECL_TEMPLATE_PARMS (t);
1374 tree parms;
1375 int i;
1376
1377 if (flags & TFF_TEMPLATE_HEADER)
1378 {
1379 for (parms = orig_parms = nreverse (orig_parms);
1380 parms;
1381 parms = TREE_CHAIN (parms))
1382 {
1383 tree inner_parms = INNERMOST_TEMPLATE_PARMS (parms);
1384 int len = TREE_VEC_LENGTH (inner_parms);
1385
1386 if (len == 0)
1387 {
1388 /* Skip over the dummy template levels of a template template
1389 parm. */
1390 gcc_assert (TREE_CODE (TREE_TYPE (t)) == TEMPLATE_TEMPLATE_PARM);
1391 continue;
1392 }
1393
1394 pp_cxx_ws_string (pp, "template");
1395 pp_cxx_begin_template_argument_list (pp);
1396
1397 /* If we've shown the template prefix, we'd better show the
1398 parameters' and decl's type too. */
1399 flags |= TFF_DECL_SPECIFIERS;
1400
1401 for (i = 0; i < len; i++)
1402 {
1403 if (i)
1404 pp_separate_with_comma (pp);
1405 dump_template_parameter (pp, TREE_VEC_ELT (inner_parms, i),
1406 flags);
1407 }
1408 pp_cxx_end_template_argument_list (pp);
1409 pp_cxx_whitespace (pp);
1410 }
1411 nreverse(orig_parms);
1412
1413 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
1414 {
1415 /* Say `template<arg> class TT' not just `template<arg> TT'. */
1416 pp_cxx_ws_string (pp, "class");
1417
1418 /* If this is a parameter pack, print the ellipsis. */
1419 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
1420 pp_cxx_ws_string (pp, "...");
1421 }
1422
1423 /* Only print the requirements if we're also printing
1424 the template header. */
1425 if (flag_concepts)
1426 if (tree ci = get_constraints (t))
1427 if (check_constraint_info (ci))
1428 if (tree reqs = CI_TEMPLATE_REQS (ci))
1429 {
1430 pp_cxx_requires_clause (pp, reqs);
1431 pp_cxx_whitespace (pp);
1432 }
1433 }
1434
1435
1436 if (DECL_CLASS_TEMPLATE_P (t))
1437 dump_type (pp, TREE_TYPE (t),
1438 ((flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1439 | (flags & TFF_DECL_SPECIFIERS ? TFF_CLASS_KEY_OR_ENUM : 0)));
1440 else if (DECL_TEMPLATE_RESULT (t)
1441 && (VAR_P (DECL_TEMPLATE_RESULT (t))
1442 /* Alias template. */
1443 || DECL_TYPE_TEMPLATE_P (t)))
1444 dump_decl (pp, DECL_TEMPLATE_RESULT (t), flags | TFF_TEMPLATE_NAME);
1445 else
1446 {
1447 gcc_assert (TREE_TYPE (t));
1448 switch (NEXT_CODE (t))
1449 {
1450 case METHOD_TYPE:
1451 case FUNCTION_TYPE:
1452 dump_function_decl (pp, t, flags | TFF_TEMPLATE_NAME);
1453 break;
1454 default:
1455 /* This case can occur with some invalid code. */
1456 dump_type (pp, TREE_TYPE (t),
1457 (flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1458 | (flags & TFF_DECL_SPECIFIERS
1459 ? TFF_CLASS_KEY_OR_ENUM : 0));
1460 }
1461 }
1462 }
1463
1464 /* find_typenames looks through the type of the function template T
1465 and returns a vec containing any typedefs, decltypes or TYPENAME_TYPEs
1466 it finds. */
1467
1468 struct find_typenames_t
1469 {
1470 hash_set<tree> *p_set;
1471 vec<tree, va_gc> *typenames;
1472 };
1473
1474 static tree
1475 find_typenames_r (tree *tp, int *walk_subtrees, void *data)
1476 {
1477 struct find_typenames_t *d = (struct find_typenames_t *)data;
1478 tree mv = NULL_TREE;
1479
1480 if (TYPE_P (*tp) && is_typedef_decl (TYPE_NAME (*tp)))
1481 /* Add the type of the typedef without any additional cv-quals. */
1482 mv = TREE_TYPE (TYPE_NAME (*tp));
1483 else if (TREE_CODE (*tp) == TYPENAME_TYPE
1484 || TREE_CODE (*tp) == DECLTYPE_TYPE)
1485 /* Add the typename without any cv-qualifiers. */
1486 mv = TYPE_MAIN_VARIANT (*tp);
1487
1488 if (PACK_EXPANSION_P (*tp))
1489 {
1490 /* Don't mess with parameter packs since we don't remember
1491 the pack expansion context for a particular typename. */
1492 *walk_subtrees = false;
1493 return NULL_TREE;
1494 }
1495
1496 if (mv && (mv == *tp || !d->p_set->add (mv)))
1497 vec_safe_push (d->typenames, mv);
1498
1499 /* Search into class template arguments, which cp_walk_subtrees
1500 doesn't do. */
1501 if (CLASS_TYPE_P (*tp) && CLASSTYPE_TEMPLATE_INFO (*tp))
1502 cp_walk_tree (&CLASSTYPE_TI_ARGS (*tp), find_typenames_r,
1503 data, d->p_set);
1504
1505 return NULL_TREE;
1506 }
1507
1508 static vec<tree, va_gc> *
1509 find_typenames (tree t)
1510 {
1511 struct find_typenames_t ft;
1512 ft.p_set = new hash_set<tree>;
1513 ft.typenames = NULL;
1514 cp_walk_tree (&TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
1515 find_typenames_r, &ft, ft.p_set);
1516 delete ft.p_set;
1517 return ft.typenames;
1518 }
1519
1520 /* Output the "[with ...]" clause for a template instantiation T iff
1521 TEMPLATE_PARMS, TEMPLATE_ARGS and FLAGS are suitable. T may be NULL if
1522 formatting a deduction/substitution diagnostic rather than an
1523 instantiation. */
1524
1525 static void
1526 dump_substitution (cxx_pretty_printer *pp,
1527 tree t, tree template_parms, tree template_args,
1528 int flags)
1529 {
1530 if (template_parms != NULL_TREE && template_args != NULL_TREE
1531 && !(flags & TFF_NO_TEMPLATE_BINDINGS))
1532 {
1533 vec<tree, va_gc> *typenames = t ? find_typenames (t) : NULL;
1534 pp_cxx_whitespace (pp);
1535 pp_cxx_left_bracket (pp);
1536 pp->translate_string ("with");
1537 pp_cxx_whitespace (pp);
1538 dump_template_bindings (pp, template_parms, template_args, typenames);
1539 pp_cxx_right_bracket (pp);
1540 }
1541 }
1542
1543 /* Dump the lambda function FN including its 'mutable' qualifier and any
1544 template bindings. */
1545
1546 static void
1547 dump_lambda_function (cxx_pretty_printer *pp,
1548 tree fn, tree template_parms, tree template_args,
1549 int flags)
1550 {
1551 /* A lambda's signature is essentially its "type". */
1552 dump_type (pp, DECL_CONTEXT (fn), flags);
1553 if (!(TYPE_QUALS (class_of_this_parm (TREE_TYPE (fn))) & TYPE_QUAL_CONST))
1554 {
1555 pp->padding = pp_before;
1556 pp_c_ws_string (pp, "mutable");
1557 }
1558 dump_substitution (pp, fn, template_parms, template_args, flags);
1559 }
1560
1561 /* Pretty print a function decl. There are several ways we want to print a
1562 function declaration. The TFF_ bits in FLAGS tells us how to behave.
1563 As error can only apply the '#' flag once to give 0 and 1 for V, there
1564 is %D which doesn't print the throw specs, and %F which does. */
1565
1566 static void
1567 dump_function_decl (cxx_pretty_printer *pp, tree t, int flags)
1568 {
1569 tree fntype;
1570 tree parmtypes;
1571 tree cname = NULL_TREE;
1572 tree template_args = NULL_TREE;
1573 tree template_parms = NULL_TREE;
1574 int show_return = flags & TFF_RETURN_TYPE || flags & TFF_DECL_SPECIFIERS;
1575 int do_outer_scope = ! (flags & TFF_UNQUALIFIED_NAME);
1576 tree exceptions;
1577 bool constexpr_p;
1578 tree ret = NULL_TREE;
1579
1580 flags &= ~(TFF_UNQUALIFIED_NAME | TFF_TEMPLATE_NAME);
1581 if (TREE_CODE (t) == TEMPLATE_DECL)
1582 t = DECL_TEMPLATE_RESULT (t);
1583
1584 /* Save the exceptions, in case t is a specialization and we are
1585 emitting an error about incompatible specifications. */
1586 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (t));
1587
1588 /* Likewise for the constexpr specifier, in case t is a specialization. */
1589 constexpr_p = DECL_DECLARED_CONSTEXPR_P (t);
1590
1591 /* Pretty print template instantiations only. */
1592 if (DECL_USE_TEMPLATE (t) && DECL_TEMPLATE_INFO (t)
1593 && !(flags & TFF_NO_TEMPLATE_BINDINGS)
1594 && flag_pretty_templates)
1595 {
1596 tree tmpl;
1597
1598 template_args = DECL_TI_ARGS (t);
1599 tmpl = most_general_template (t);
1600 if (tmpl && TREE_CODE (tmpl) == TEMPLATE_DECL)
1601 {
1602 template_parms = DECL_TEMPLATE_PARMS (tmpl);
1603 t = tmpl;
1604 }
1605 }
1606
1607 if (DECL_NAME (t) && LAMBDA_FUNCTION_P (t))
1608 return dump_lambda_function (pp, t, template_parms, template_args, flags);
1609
1610 fntype = TREE_TYPE (t);
1611 parmtypes = FUNCTION_FIRST_USER_PARMTYPE (t);
1612
1613 if (DECL_CLASS_SCOPE_P (t))
1614 cname = DECL_CONTEXT (t);
1615 /* This is for partially instantiated template methods. */
1616 else if (TREE_CODE (fntype) == METHOD_TYPE)
1617 cname = TREE_TYPE (TREE_VALUE (parmtypes));
1618
1619 if (flags & TFF_DECL_SPECIFIERS)
1620 {
1621 if (DECL_STATIC_FUNCTION_P (t))
1622 pp_cxx_ws_string (pp, "static");
1623 else if (DECL_VIRTUAL_P (t))
1624 pp_cxx_ws_string (pp, "virtual");
1625
1626 if (constexpr_p)
1627 {
1628 if (DECL_DECLARED_CONCEPT_P (t))
1629 pp_cxx_ws_string (pp, "concept");
1630 else
1631 pp_cxx_ws_string (pp, "constexpr");
1632 }
1633 }
1634
1635 /* Print the return type? */
1636 if (show_return)
1637 show_return = (!DECL_CONV_FN_P (t) && !DECL_CONSTRUCTOR_P (t)
1638 && !DECL_DESTRUCTOR_P (t) && !deduction_guide_p (t));
1639 if (show_return)
1640 {
1641 ret = fndecl_declared_return_type (t);
1642 dump_type_prefix (pp, ret, flags);
1643 }
1644
1645 /* Print the function name. */
1646 if (!do_outer_scope)
1647 /* Nothing. */;
1648 else if (cname)
1649 {
1650 dump_type (pp, cname, flags);
1651 pp_cxx_colon_colon (pp);
1652 }
1653 else
1654 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
1655
1656 dump_function_name (pp, t, flags);
1657
1658 if (!(flags & TFF_NO_FUNCTION_ARGUMENTS))
1659 {
1660 dump_parameters (pp, parmtypes, flags);
1661
1662 if (TREE_CODE (fntype) == METHOD_TYPE)
1663 {
1664 pp->padding = pp_before;
1665 pp_cxx_cv_qualifier_seq (pp, class_of_this_parm (fntype));
1666 dump_ref_qualifier (pp, fntype, flags);
1667 }
1668
1669 if (tx_safe_fn_type_p (fntype))
1670 {
1671 pp->padding = pp_before;
1672 pp_cxx_ws_string (pp, "transaction_safe");
1673 }
1674
1675 if (flags & TFF_EXCEPTION_SPECIFICATION)
1676 {
1677 pp->padding = pp_before;
1678 dump_exception_spec (pp, exceptions, flags);
1679 }
1680
1681 if (show_return)
1682 dump_type_suffix (pp, ret, flags);
1683 else if (deduction_guide_p (t))
1684 {
1685 pp_cxx_ws_string (pp, "->");
1686 dump_type (pp, TREE_TYPE (TREE_TYPE (t)), flags);
1687 }
1688
1689 if (flag_concepts)
1690 if (tree ci = get_constraints (t))
1691 if (tree reqs = CI_DECLARATOR_REQS (ci))
1692 pp_cxx_requires_clause (pp, reqs);
1693
1694 dump_substitution (pp, t, template_parms, template_args, flags);
1695
1696 if (tree base = DECL_INHERITED_CTOR_BASE (t))
1697 {
1698 pp_cxx_ws_string (pp, "[inherited from");
1699 dump_type (pp, base, TFF_PLAIN_IDENTIFIER);
1700 pp_character (pp, ']');
1701 }
1702 }
1703 else if (template_args)
1704 {
1705 bool need_comma = false;
1706 int i;
1707 pp_cxx_begin_template_argument_list (pp);
1708 template_args = INNERMOST_TEMPLATE_ARGS (template_args);
1709 for (i = 0; i < TREE_VEC_LENGTH (template_args); ++i)
1710 {
1711 tree arg = TREE_VEC_ELT (template_args, i);
1712 if (need_comma)
1713 pp_separate_with_comma (pp);
1714 if (ARGUMENT_PACK_P (arg))
1715 pp_cxx_left_brace (pp);
1716 dump_template_argument (pp, arg, TFF_PLAIN_IDENTIFIER);
1717 if (ARGUMENT_PACK_P (arg))
1718 pp_cxx_right_brace (pp);
1719 need_comma = true;
1720 }
1721 pp_cxx_end_template_argument_list (pp);
1722 }
1723 }
1724
1725 /* Print a parameter list. If this is for a member function, the
1726 member object ptr (and any other hidden args) should have
1727 already been removed. */
1728
1729 static void
1730 dump_parameters (cxx_pretty_printer *pp, tree parmtypes, int flags)
1731 {
1732 int first = 1;
1733 flags &= ~TFF_SCOPE;
1734 pp_cxx_left_paren (pp);
1735
1736 for (first = 1; parmtypes != void_list_node;
1737 parmtypes = TREE_CHAIN (parmtypes))
1738 {
1739 if (!first)
1740 pp_separate_with_comma (pp);
1741 first = 0;
1742 if (!parmtypes)
1743 {
1744 pp_cxx_ws_string (pp, "...");
1745 break;
1746 }
1747
1748 dump_type (pp, TREE_VALUE (parmtypes), flags);
1749
1750 if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && TREE_PURPOSE (parmtypes))
1751 {
1752 pp_cxx_whitespace (pp);
1753 pp_equal (pp);
1754 pp_cxx_whitespace (pp);
1755 dump_expr (pp, TREE_PURPOSE (parmtypes), flags | TFF_EXPR_IN_PARENS);
1756 }
1757 }
1758
1759 pp_cxx_right_paren (pp);
1760 }
1761
1762 /* Print ref-qualifier of a FUNCTION_TYPE or METHOD_TYPE. FLAGS are ignored. */
1763
1764 static void
1765 dump_ref_qualifier (cxx_pretty_printer *pp, tree t, int flags ATTRIBUTE_UNUSED)
1766 {
1767 if (FUNCTION_REF_QUALIFIED (t))
1768 {
1769 pp->padding = pp_before;
1770 if (FUNCTION_RVALUE_QUALIFIED (t))
1771 pp_cxx_ws_string (pp, "&&");
1772 else
1773 pp_cxx_ws_string (pp, "&");
1774 }
1775 }
1776
1777 /* Print an exception specification. T is the exception specification. */
1778
1779 static void
1780 dump_exception_spec (cxx_pretty_printer *pp, tree t, int flags)
1781 {
1782 if (t && TREE_PURPOSE (t))
1783 {
1784 pp_cxx_ws_string (pp, "noexcept");
1785 if (!integer_onep (TREE_PURPOSE (t)))
1786 {
1787 pp_cxx_whitespace (pp);
1788 pp_cxx_left_paren (pp);
1789 if (DEFERRED_NOEXCEPT_SPEC_P (t))
1790 pp_cxx_ws_string (pp, "<uninstantiated>");
1791 else
1792 dump_expr (pp, TREE_PURPOSE (t), flags);
1793 pp_cxx_right_paren (pp);
1794 }
1795 }
1796 else if (t)
1797 {
1798 pp_cxx_ws_string (pp, "throw");
1799 pp_cxx_whitespace (pp);
1800 pp_cxx_left_paren (pp);
1801 if (TREE_VALUE (t) != NULL_TREE)
1802 while (1)
1803 {
1804 dump_type (pp, TREE_VALUE (t), flags);
1805 t = TREE_CHAIN (t);
1806 if (!t)
1807 break;
1808 pp_separate_with_comma (pp);
1809 }
1810 pp_cxx_right_paren (pp);
1811 }
1812 }
1813
1814 /* Handle the function name for a FUNCTION_DECL node, grokking operators
1815 and destructors properly. */
1816
1817 static void
1818 dump_function_name (cxx_pretty_printer *pp, tree t, int flags)
1819 {
1820 tree name = DECL_NAME (t);
1821
1822 /* We can get here with a decl that was synthesized by language-
1823 independent machinery (e.g. coverage.c) in which case it won't
1824 have a lang_specific structure attached and DECL_CONSTRUCTOR_P
1825 will crash. In this case it is safe just to print out the
1826 literal name. */
1827 if (!DECL_LANG_SPECIFIC (t))
1828 {
1829 pp_cxx_tree_identifier (pp, name);
1830 return;
1831 }
1832
1833 if (TREE_CODE (t) == TEMPLATE_DECL)
1834 t = DECL_TEMPLATE_RESULT (t);
1835
1836 /* Don't let the user see __comp_ctor et al. */
1837 if (DECL_CONSTRUCTOR_P (t)
1838 || DECL_DESTRUCTOR_P (t))
1839 {
1840 if (LAMBDA_TYPE_P (DECL_CONTEXT (t)))
1841 name = get_identifier ("<lambda>");
1842 else if (TYPE_UNNAMED_P (DECL_CONTEXT (t)))
1843 name = get_identifier ("<constructor>");
1844 else
1845 name = constructor_name (DECL_CONTEXT (t));
1846 }
1847
1848 if (DECL_DESTRUCTOR_P (t))
1849 {
1850 pp_cxx_complement (pp);
1851 dump_decl (pp, name, TFF_PLAIN_IDENTIFIER);
1852 }
1853 else if (DECL_CONV_FN_P (t))
1854 {
1855 /* This cannot use the hack that the operator's return
1856 type is stashed off of its name because it may be
1857 used for error reporting. In the case of conflicting
1858 declarations, both will have the same name, yet
1859 the types will be different, hence the TREE_TYPE field
1860 of the first name will be clobbered by the second. */
1861 pp_cxx_ws_string (pp, "operator");
1862 dump_type (pp, TREE_TYPE (TREE_TYPE (t)), flags);
1863 }
1864 else
1865 dump_decl (pp, name, flags);
1866
1867 if (DECL_TEMPLATE_INFO (t)
1868 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (t)
1869 && (TREE_CODE (DECL_TI_TEMPLATE (t)) != TEMPLATE_DECL
1870 || PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
1871 dump_template_parms (pp, DECL_TEMPLATE_INFO (t), !DECL_USE_TEMPLATE (t),
1872 flags);
1873 }
1874
1875 /* Dump the template parameters from the template info INFO under control of
1876 FLAGS. PRIMARY indicates whether this is a primary template decl, or
1877 specialization (partial or complete). For partial specializations we show
1878 the specialized parameter values. For a primary template we show no
1879 decoration. */
1880
1881 static void
1882 dump_template_parms (cxx_pretty_printer *pp, tree info,
1883 int primary, int flags)
1884 {
1885 tree args = info ? TI_ARGS (info) : NULL_TREE;
1886
1887 if (primary && flags & TFF_TEMPLATE_NAME)
1888 return;
1889 flags &= ~(TFF_CLASS_KEY_OR_ENUM | TFF_TEMPLATE_NAME);
1890 pp_cxx_begin_template_argument_list (pp);
1891
1892 /* Be careful only to print things when we have them, so as not
1893 to crash producing error messages. */
1894 if (args && !primary)
1895 {
1896 int len, ix;
1897 len = get_non_default_template_args_count (args, flags);
1898
1899 args = INNERMOST_TEMPLATE_ARGS (args);
1900 for (ix = 0; ix != len; ix++)
1901 {
1902 tree arg = TREE_VEC_ELT (args, ix);
1903
1904 /* Only print a comma if we know there is an argument coming. In
1905 the case of an empty template argument pack, no actual
1906 argument will be printed. */
1907 if (ix
1908 && (!ARGUMENT_PACK_P (arg)
1909 || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) > 0))
1910 pp_separate_with_comma (pp);
1911
1912 if (!arg)
1913 pp_string (pp, M_("<template parameter error>"));
1914 else
1915 dump_template_argument (pp, arg, flags);
1916 }
1917 }
1918 else if (primary)
1919 {
1920 tree tpl = TI_TEMPLATE (info);
1921 tree parms = DECL_TEMPLATE_PARMS (tpl);
1922 int len, ix;
1923
1924 parms = TREE_CODE (parms) == TREE_LIST ? TREE_VALUE (parms) : NULL_TREE;
1925 len = parms ? TREE_VEC_LENGTH (parms) : 0;
1926
1927 for (ix = 0; ix != len; ix++)
1928 {
1929 tree parm;
1930
1931 if (TREE_VEC_ELT (parms, ix) == error_mark_node)
1932 {
1933 pp_string (pp, M_("<template parameter error>"));
1934 continue;
1935 }
1936
1937 parm = TREE_VALUE (TREE_VEC_ELT (parms, ix));
1938
1939 if (ix)
1940 pp_separate_with_comma (pp);
1941
1942 dump_decl (pp, parm, flags & ~TFF_DECL_SPECIFIERS);
1943 }
1944 }
1945 pp_cxx_end_template_argument_list (pp);
1946 }
1947
1948 /* Print out the arguments of CALL_EXPR T as a parenthesized list using
1949 flags FLAGS. Skip over the first argument if SKIPFIRST is true. */
1950
1951 static void
1952 dump_call_expr_args (cxx_pretty_printer *pp, tree t, int flags, bool skipfirst)
1953 {
1954 tree arg;
1955 call_expr_arg_iterator iter;
1956
1957 pp_cxx_left_paren (pp);
1958 FOR_EACH_CALL_EXPR_ARG (arg, iter, t)
1959 {
1960 if (skipfirst)
1961 skipfirst = false;
1962 else
1963 {
1964 dump_expr (pp, arg, flags | TFF_EXPR_IN_PARENS);
1965 if (more_call_expr_args_p (&iter))
1966 pp_separate_with_comma (pp);
1967 }
1968 }
1969 pp_cxx_right_paren (pp);
1970 }
1971
1972 /* Print out the arguments of AGGR_INIT_EXPR T as a parenthesized list
1973 using flags FLAGS. Skip over the first argument if SKIPFIRST is
1974 true. */
1975
1976 static void
1977 dump_aggr_init_expr_args (cxx_pretty_printer *pp, tree t, int flags,
1978 bool skipfirst)
1979 {
1980 tree arg;
1981 aggr_init_expr_arg_iterator iter;
1982
1983 pp_cxx_left_paren (pp);
1984 FOR_EACH_AGGR_INIT_EXPR_ARG (arg, iter, t)
1985 {
1986 if (skipfirst)
1987 skipfirst = false;
1988 else
1989 {
1990 dump_expr (pp, arg, flags | TFF_EXPR_IN_PARENS);
1991 if (more_aggr_init_expr_args_p (&iter))
1992 pp_separate_with_comma (pp);
1993 }
1994 }
1995 pp_cxx_right_paren (pp);
1996 }
1997
1998 /* Print out a list of initializers (subr of dump_expr). */
1999
2000 static void
2001 dump_expr_list (cxx_pretty_printer *pp, tree l, int flags)
2002 {
2003 while (l)
2004 {
2005 dump_expr (pp, TREE_VALUE (l), flags | TFF_EXPR_IN_PARENS);
2006 l = TREE_CHAIN (l);
2007 if (l)
2008 pp_separate_with_comma (pp);
2009 }
2010 }
2011
2012 /* Print out a vector of initializers (subr of dump_expr). */
2013
2014 static void
2015 dump_expr_init_vec (cxx_pretty_printer *pp, vec<constructor_elt, va_gc> *v,
2016 int flags)
2017 {
2018 unsigned HOST_WIDE_INT idx;
2019 tree value;
2020
2021 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
2022 {
2023 dump_expr (pp, value, flags | TFF_EXPR_IN_PARENS);
2024 if (idx != v->length () - 1)
2025 pp_separate_with_comma (pp);
2026 }
2027 }
2028
2029
2030 /* We've gotten an indirect REFERENCE (an OBJ_TYPE_REF) to a virtual
2031 function. Resolve it to a close relative -- in the sense of static
2032 type -- variant being overridden. That is close to what was written in
2033 the source code. Subroutine of dump_expr. */
2034
2035 static tree
2036 resolve_virtual_fun_from_obj_type_ref (tree ref)
2037 {
2038 tree obj_type = TREE_TYPE (OBJ_TYPE_REF_OBJECT (ref));
2039 HOST_WIDE_INT index = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (ref));
2040 tree fun = BINFO_VIRTUALS (TYPE_BINFO (TREE_TYPE (obj_type)));
2041 while (index)
2042 {
2043 fun = TREE_CHAIN (fun);
2044 index -= (TARGET_VTABLE_USES_DESCRIPTORS
2045 ? TARGET_VTABLE_USES_DESCRIPTORS : 1);
2046 }
2047
2048 return BV_FN (fun);
2049 }
2050
2051 /* Print out an expression E under control of FLAGS. */
2052
2053 static void
2054 dump_expr (cxx_pretty_printer *pp, tree t, int flags)
2055 {
2056 tree op;
2057
2058 if (t == 0)
2059 return;
2060
2061 if (STATEMENT_CLASS_P (t))
2062 {
2063 pp_cxx_ws_string (pp, M_("<statement>"));
2064 return;
2065 }
2066
2067 switch (TREE_CODE (t))
2068 {
2069 case VAR_DECL:
2070 case PARM_DECL:
2071 case FIELD_DECL:
2072 case CONST_DECL:
2073 case FUNCTION_DECL:
2074 case TEMPLATE_DECL:
2075 case NAMESPACE_DECL:
2076 case LABEL_DECL:
2077 case OVERLOAD:
2078 case TYPE_DECL:
2079 case IDENTIFIER_NODE:
2080 dump_decl (pp, t, ((flags & ~(TFF_DECL_SPECIFIERS|TFF_RETURN_TYPE
2081 |TFF_TEMPLATE_HEADER))
2082 | TFF_NO_TEMPLATE_BINDINGS
2083 | TFF_NO_FUNCTION_ARGUMENTS));
2084 break;
2085
2086 case SSA_NAME:
2087 if (SSA_NAME_VAR (t)
2088 && !DECL_ARTIFICIAL (SSA_NAME_VAR (t)))
2089 dump_expr (pp, SSA_NAME_VAR (t), flags);
2090 else
2091 pp_cxx_ws_string (pp, M_("<unknown>"));
2092 break;
2093
2094 case VOID_CST:
2095 case INTEGER_CST:
2096 case REAL_CST:
2097 case STRING_CST:
2098 case COMPLEX_CST:
2099 pp->constant (t);
2100 break;
2101
2102 case USERDEF_LITERAL:
2103 pp_cxx_userdef_literal (pp, t);
2104 break;
2105
2106 case THROW_EXPR:
2107 /* While waiting for caret diagnostics, avoid printing
2108 __cxa_allocate_exception, __cxa_throw, and the like. */
2109 pp_cxx_ws_string (pp, M_("<throw-expression>"));
2110 break;
2111
2112 case PTRMEM_CST:
2113 pp_ampersand (pp);
2114 dump_type (pp, PTRMEM_CST_CLASS (t), flags);
2115 pp_cxx_colon_colon (pp);
2116 pp_cxx_tree_identifier (pp, DECL_NAME (PTRMEM_CST_MEMBER (t)));
2117 break;
2118
2119 case COMPOUND_EXPR:
2120 pp_cxx_left_paren (pp);
2121 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2122 pp_separate_with_comma (pp);
2123 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2124 pp_cxx_right_paren (pp);
2125 break;
2126
2127 case COND_EXPR:
2128 case VEC_COND_EXPR:
2129 pp_cxx_left_paren (pp);
2130 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2131 pp_string (pp, " ? ");
2132 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2133 pp_string (pp, " : ");
2134 dump_expr (pp, TREE_OPERAND (t, 2), flags | TFF_EXPR_IN_PARENS);
2135 pp_cxx_right_paren (pp);
2136 break;
2137
2138 case SAVE_EXPR:
2139 if (TREE_HAS_CONSTRUCTOR (t))
2140 {
2141 pp_cxx_ws_string (pp, "new");
2142 pp_cxx_whitespace (pp);
2143 dump_type (pp, TREE_TYPE (TREE_TYPE (t)), flags);
2144 }
2145 else
2146 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2147 break;
2148
2149 case AGGR_INIT_EXPR:
2150 {
2151 tree fn = NULL_TREE;
2152
2153 if (TREE_CODE (AGGR_INIT_EXPR_FN (t)) == ADDR_EXPR)
2154 fn = TREE_OPERAND (AGGR_INIT_EXPR_FN (t), 0);
2155
2156 if (fn && TREE_CODE (fn) == FUNCTION_DECL)
2157 {
2158 if (DECL_CONSTRUCTOR_P (fn))
2159 dump_type (pp, DECL_CONTEXT (fn), flags);
2160 else
2161 dump_decl (pp, fn, 0);
2162 }
2163 else
2164 dump_expr (pp, AGGR_INIT_EXPR_FN (t), 0);
2165 }
2166 dump_aggr_init_expr_args (pp, t, flags, true);
2167 break;
2168
2169 case CALL_EXPR:
2170 {
2171 tree fn = CALL_EXPR_FN (t);
2172 bool skipfirst = false;
2173
2174 /* Deal with internal functions. */
2175 if (fn == NULL_TREE)
2176 {
2177 pp_string (pp, internal_fn_name (CALL_EXPR_IFN (t)));
2178 dump_call_expr_args (pp, t, flags, skipfirst);
2179 break;
2180 }
2181
2182 if (TREE_CODE (fn) == ADDR_EXPR)
2183 fn = TREE_OPERAND (fn, 0);
2184
2185 /* Nobody is interested in seeing the guts of vcalls. */
2186 if (TREE_CODE (fn) == OBJ_TYPE_REF)
2187 fn = resolve_virtual_fun_from_obj_type_ref (fn);
2188
2189 if (TREE_TYPE (fn) != NULL_TREE
2190 && NEXT_CODE (fn) == METHOD_TYPE
2191 && call_expr_nargs (t))
2192 {
2193 tree ob = CALL_EXPR_ARG (t, 0);
2194 if (TREE_CODE (ob) == ADDR_EXPR)
2195 {
2196 dump_expr (pp, TREE_OPERAND (ob, 0),
2197 flags | TFF_EXPR_IN_PARENS);
2198 pp_cxx_dot (pp);
2199 }
2200 else if (!is_this_parameter (ob))
2201 {
2202 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2203 pp_cxx_arrow (pp);
2204 }
2205 skipfirst = true;
2206 }
2207 if (flag_sanitize & SANITIZE_UNDEFINED
2208 && is_ubsan_builtin_p (fn))
2209 {
2210 pp_string (cxx_pp, M_("<ubsan routine call>"));
2211 break;
2212 }
2213 dump_expr (pp, fn, flags | TFF_EXPR_IN_PARENS);
2214 dump_call_expr_args (pp, t, flags, skipfirst);
2215 }
2216 break;
2217
2218 case TARGET_EXPR:
2219 /* Note that this only works for G++ target exprs. If somebody
2220 builds a general TARGET_EXPR, there's no way to represent that
2221 it initializes anything other that the parameter slot for the
2222 default argument. Note we may have cleared out the first
2223 operand in expand_expr, so don't go killing ourselves. */
2224 if (TREE_OPERAND (t, 1))
2225 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2226 break;
2227
2228 case POINTER_PLUS_EXPR:
2229 dump_binary_op (pp, "+", t, flags);
2230 break;
2231
2232 case POINTER_DIFF_EXPR:
2233 dump_binary_op (pp, "-", t, flags);
2234 break;
2235
2236 case INIT_EXPR:
2237 case MODIFY_EXPR:
2238 dump_binary_op (pp, OVL_OP_INFO (true, NOP_EXPR)->name, t, flags);
2239 break;
2240
2241 case PLUS_EXPR:
2242 case MINUS_EXPR:
2243 case MULT_EXPR:
2244 case TRUNC_DIV_EXPR:
2245 case TRUNC_MOD_EXPR:
2246 case MIN_EXPR:
2247 case MAX_EXPR:
2248 case LSHIFT_EXPR:
2249 case RSHIFT_EXPR:
2250 case BIT_IOR_EXPR:
2251 case BIT_XOR_EXPR:
2252 case BIT_AND_EXPR:
2253 case TRUTH_ANDIF_EXPR:
2254 case TRUTH_ORIF_EXPR:
2255 case LT_EXPR:
2256 case LE_EXPR:
2257 case GT_EXPR:
2258 case GE_EXPR:
2259 case EQ_EXPR:
2260 case NE_EXPR:
2261 case EXACT_DIV_EXPR:
2262 dump_binary_op (pp, OVL_OP_INFO (false, TREE_CODE (t))->name, t, flags);
2263 break;
2264
2265 case CEIL_DIV_EXPR:
2266 case FLOOR_DIV_EXPR:
2267 case ROUND_DIV_EXPR:
2268 case RDIV_EXPR:
2269 dump_binary_op (pp, "/", t, flags);
2270 break;
2271
2272 case CEIL_MOD_EXPR:
2273 case FLOOR_MOD_EXPR:
2274 case ROUND_MOD_EXPR:
2275 dump_binary_op (pp, "%", t, flags);
2276 break;
2277
2278 case COMPONENT_REF:
2279 {
2280 tree ob = TREE_OPERAND (t, 0);
2281 if (INDIRECT_REF_P (ob))
2282 {
2283 ob = TREE_OPERAND (ob, 0);
2284 if (!is_this_parameter (ob))
2285 {
2286 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2287 if (TYPE_REF_P (TREE_TYPE (ob)))
2288 pp_cxx_dot (pp);
2289 else
2290 pp_cxx_arrow (pp);
2291 }
2292 }
2293 else
2294 {
2295 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2296 if (TREE_CODE (ob) != ARROW_EXPR)
2297 pp_cxx_dot (pp);
2298 }
2299 dump_expr (pp, TREE_OPERAND (t, 1), flags & ~TFF_EXPR_IN_PARENS);
2300 }
2301 break;
2302
2303 case ARRAY_REF:
2304 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2305 pp_cxx_left_bracket (pp);
2306 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2307 pp_cxx_right_bracket (pp);
2308 break;
2309
2310 case UNARY_PLUS_EXPR:
2311 dump_unary_op (pp, "+", t, flags);
2312 break;
2313
2314 case ADDR_EXPR:
2315 if (TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
2316 || TREE_CODE (TREE_OPERAND (t, 0)) == STRING_CST
2317 /* An ADDR_EXPR can have reference type. In that case, we
2318 shouldn't print the `&' doing so indicates to the user
2319 that the expression has pointer type. */
2320 || (TREE_TYPE (t)
2321 && TYPE_REF_P (TREE_TYPE (t))))
2322 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2323 else if (TREE_CODE (TREE_OPERAND (t, 0)) == LABEL_DECL)
2324 dump_unary_op (pp, "&&", t, flags);
2325 else
2326 dump_unary_op (pp, "&", t, flags);
2327 break;
2328
2329 case INDIRECT_REF:
2330 if (TREE_HAS_CONSTRUCTOR (t))
2331 {
2332 t = TREE_OPERAND (t, 0);
2333 gcc_assert (TREE_CODE (t) == CALL_EXPR);
2334 dump_expr (pp, CALL_EXPR_FN (t), flags | TFF_EXPR_IN_PARENS);
2335 dump_call_expr_args (pp, t, flags, true);
2336 }
2337 else
2338 {
2339 if (TREE_OPERAND (t,0) != NULL_TREE
2340 && TREE_TYPE (TREE_OPERAND (t, 0))
2341 && NEXT_CODE (TREE_OPERAND (t, 0)) == REFERENCE_TYPE)
2342 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2343 else
2344 dump_unary_op (pp, "*", t, flags);
2345 }
2346 break;
2347
2348 case MEM_REF:
2349 if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
2350 && integer_zerop (TREE_OPERAND (t, 1)))
2351 dump_expr (pp, TREE_OPERAND (TREE_OPERAND (t, 0), 0), flags);
2352 else
2353 {
2354 pp_cxx_star (pp);
2355 if (!integer_zerop (TREE_OPERAND (t, 1)))
2356 {
2357 pp_cxx_left_paren (pp);
2358 if (!integer_onep (TYPE_SIZE_UNIT
2359 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0))))))
2360 {
2361 pp_cxx_left_paren (pp);
2362 dump_type (pp, ptr_type_node, flags);
2363 pp_cxx_right_paren (pp);
2364 }
2365 }
2366 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2367 if (!integer_zerop (TREE_OPERAND (t, 1)))
2368 {
2369 pp_cxx_ws_string (pp, "+");
2370 dump_expr (pp, fold_convert (ssizetype, TREE_OPERAND (t, 1)),
2371 flags);
2372 pp_cxx_right_paren (pp);
2373 }
2374 }
2375 break;
2376
2377 case NEGATE_EXPR:
2378 case BIT_NOT_EXPR:
2379 case TRUTH_NOT_EXPR:
2380 case PREDECREMENT_EXPR:
2381 case PREINCREMENT_EXPR:
2382 dump_unary_op (pp, OVL_OP_INFO (false, TREE_CODE (t))->name, t, flags);
2383 break;
2384
2385 case POSTDECREMENT_EXPR:
2386 case POSTINCREMENT_EXPR:
2387 pp_cxx_left_paren (pp);
2388 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2389 pp_cxx_ws_string (pp, OVL_OP_INFO (false, TREE_CODE (t))->name);
2390 pp_cxx_right_paren (pp);
2391 break;
2392
2393 case NON_LVALUE_EXPR:
2394 /* FIXME: This is a KLUDGE workaround for a parsing problem. There
2395 should be another level of INDIRECT_REF so that I don't have to do
2396 this. */
2397 if (TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == POINTER_TYPE)
2398 {
2399 tree next = TREE_TYPE (TREE_TYPE (t));
2400
2401 while (TYPE_PTR_P (next))
2402 next = TREE_TYPE (next);
2403
2404 if (TREE_CODE (next) == FUNCTION_TYPE)
2405 {
2406 if (flags & TFF_EXPR_IN_PARENS)
2407 pp_cxx_left_paren (pp);
2408 pp_cxx_star (pp);
2409 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2410 if (flags & TFF_EXPR_IN_PARENS)
2411 pp_cxx_right_paren (pp);
2412 break;
2413 }
2414 /* Else fall through. */
2415 }
2416 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2417 break;
2418
2419 CASE_CONVERT:
2420 case IMPLICIT_CONV_EXPR:
2421 case VIEW_CONVERT_EXPR:
2422 {
2423 tree op = TREE_OPERAND (t, 0);
2424 tree ttype = TREE_TYPE (t);
2425 tree optype = TREE_TYPE (op);
2426
2427 if (TREE_CODE (ttype) != TREE_CODE (optype)
2428 && INDIRECT_TYPE_P (ttype)
2429 && INDIRECT_TYPE_P (optype)
2430 && same_type_p (TREE_TYPE (optype),
2431 TREE_TYPE (ttype)))
2432 {
2433 if (TYPE_REF_P (ttype))
2434 {
2435 STRIP_NOPS (op);
2436 if (TREE_CODE (op) == ADDR_EXPR)
2437 dump_expr (pp, TREE_OPERAND (op, 0), flags);
2438 else
2439 dump_unary_op (pp, "*", t, flags);
2440 }
2441 else
2442 dump_unary_op (pp, "&", t, flags);
2443 }
2444 else if (!same_type_p (TREE_TYPE (op), TREE_TYPE (t)))
2445 {
2446 /* It is a cast, but we cannot tell whether it is a
2447 reinterpret or static cast. Use the C style notation. */
2448 if (flags & TFF_EXPR_IN_PARENS)
2449 pp_cxx_left_paren (pp);
2450 pp_cxx_left_paren (pp);
2451 dump_type (pp, TREE_TYPE (t), flags);
2452 pp_cxx_right_paren (pp);
2453 dump_expr (pp, op, flags | TFF_EXPR_IN_PARENS);
2454 if (flags & TFF_EXPR_IN_PARENS)
2455 pp_cxx_right_paren (pp);
2456 }
2457 else
2458 dump_expr (pp, op, flags);
2459 break;
2460 }
2461
2462 case CONSTRUCTOR:
2463 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
2464 {
2465 tree idx = build_ptrmemfunc_access_expr (t, pfn_identifier);
2466
2467 if (integer_zerop (idx))
2468 {
2469 /* A NULL pointer-to-member constant. */
2470 pp_cxx_left_paren (pp);
2471 pp_cxx_left_paren (pp);
2472 dump_type (pp, TREE_TYPE (t), flags);
2473 pp_cxx_right_paren (pp);
2474 pp_character (pp, '0');
2475 pp_cxx_right_paren (pp);
2476 break;
2477 }
2478 else if (tree_fits_shwi_p (idx))
2479 {
2480 tree virtuals;
2481 unsigned HOST_WIDE_INT n;
2482
2483 t = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
2484 t = TYPE_METHOD_BASETYPE (t);
2485 virtuals = BINFO_VIRTUALS (TYPE_BINFO (TYPE_MAIN_VARIANT (t)));
2486
2487 n = tree_to_shwi (idx);
2488
2489 /* Map vtable index back one, to allow for the null pointer to
2490 member. */
2491 --n;
2492
2493 while (n > 0 && virtuals)
2494 {
2495 --n;
2496 virtuals = TREE_CHAIN (virtuals);
2497 }
2498 if (virtuals)
2499 {
2500 dump_expr (pp, BV_FN (virtuals),
2501 flags | TFF_EXPR_IN_PARENS);
2502 break;
2503 }
2504 }
2505 }
2506 if (TREE_TYPE (t) && LAMBDA_TYPE_P (TREE_TYPE (t)))
2507 pp_string (pp, "<lambda closure object>");
2508 if (TREE_TYPE (t) && EMPTY_CONSTRUCTOR_P (t))
2509 {
2510 dump_type (pp, TREE_TYPE (t), 0);
2511 pp_cxx_left_paren (pp);
2512 pp_cxx_right_paren (pp);
2513 }
2514 else
2515 {
2516 if (!BRACE_ENCLOSED_INITIALIZER_P (t))
2517 dump_type (pp, TREE_TYPE (t), 0);
2518 pp_cxx_left_brace (pp);
2519 dump_expr_init_vec (pp, CONSTRUCTOR_ELTS (t), flags);
2520 pp_cxx_right_brace (pp);
2521 }
2522
2523 break;
2524
2525 case OFFSET_REF:
2526 {
2527 tree ob = TREE_OPERAND (t, 0);
2528 if (is_dummy_object (ob))
2529 {
2530 t = TREE_OPERAND (t, 1);
2531 if (TREE_CODE (t) == FUNCTION_DECL)
2532 /* A::f */
2533 dump_expr (pp, t, flags | TFF_EXPR_IN_PARENS);
2534 else if (BASELINK_P (t))
2535 dump_expr (pp, OVL_FIRST (BASELINK_FUNCTIONS (t)),
2536 flags | TFF_EXPR_IN_PARENS);
2537 else
2538 dump_decl (pp, t, flags);
2539 }
2540 else
2541 {
2542 if (INDIRECT_REF_P (ob))
2543 {
2544 dump_expr (pp, TREE_OPERAND (ob, 0), flags | TFF_EXPR_IN_PARENS);
2545 pp_cxx_arrow (pp);
2546 pp_cxx_star (pp);
2547 }
2548 else
2549 {
2550 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2551 pp_cxx_dot (pp);
2552 pp_cxx_star (pp);
2553 }
2554 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2555 }
2556 break;
2557 }
2558
2559 case TEMPLATE_PARM_INDEX:
2560 dump_decl (pp, TEMPLATE_PARM_DECL (t), flags & ~TFF_DECL_SPECIFIERS);
2561 break;
2562
2563 case CAST_EXPR:
2564 if (TREE_OPERAND (t, 0) == NULL_TREE
2565 || TREE_CHAIN (TREE_OPERAND (t, 0)))
2566 {
2567 dump_type (pp, TREE_TYPE (t), flags);
2568 pp_cxx_left_paren (pp);
2569 dump_expr_list (pp, TREE_OPERAND (t, 0), flags);
2570 pp_cxx_right_paren (pp);
2571 }
2572 else
2573 {
2574 pp_cxx_left_paren (pp);
2575 dump_type (pp, TREE_TYPE (t), flags);
2576 pp_cxx_right_paren (pp);
2577 pp_cxx_left_paren (pp);
2578 dump_expr_list (pp, TREE_OPERAND (t, 0), flags);
2579 pp_cxx_right_paren (pp);
2580 }
2581 break;
2582
2583 case STATIC_CAST_EXPR:
2584 pp_cxx_ws_string (pp, "static_cast");
2585 goto cast;
2586 case REINTERPRET_CAST_EXPR:
2587 pp_cxx_ws_string (pp, "reinterpret_cast");
2588 goto cast;
2589 case CONST_CAST_EXPR:
2590 pp_cxx_ws_string (pp, "const_cast");
2591 goto cast;
2592 case DYNAMIC_CAST_EXPR:
2593 pp_cxx_ws_string (pp, "dynamic_cast");
2594 cast:
2595 pp_cxx_begin_template_argument_list (pp);
2596 dump_type (pp, TREE_TYPE (t), flags);
2597 pp_cxx_end_template_argument_list (pp);
2598 pp_cxx_left_paren (pp);
2599 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2600 pp_cxx_right_paren (pp);
2601 break;
2602
2603 case ARROW_EXPR:
2604 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2605 pp_cxx_arrow (pp);
2606 break;
2607
2608 case SIZEOF_EXPR:
2609 case ALIGNOF_EXPR:
2610 if (TREE_CODE (t) == SIZEOF_EXPR)
2611 pp_cxx_ws_string (pp, "sizeof");
2612 else
2613 {
2614 gcc_assert (TREE_CODE (t) == ALIGNOF_EXPR);
2615 pp_cxx_ws_string (pp, "__alignof__");
2616 }
2617 op = TREE_OPERAND (t, 0);
2618 if (PACK_EXPANSION_P (op))
2619 {
2620 pp_string (pp, "...");
2621 op = PACK_EXPANSION_PATTERN (op);
2622 }
2623 pp_cxx_whitespace (pp);
2624 pp_cxx_left_paren (pp);
2625 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
2626 dump_type (pp, TREE_TYPE (op), flags);
2627 else if (TYPE_P (TREE_OPERAND (t, 0)))
2628 dump_type (pp, op, flags);
2629 else
2630 dump_expr (pp, op, flags);
2631 pp_cxx_right_paren (pp);
2632 break;
2633
2634 case AT_ENCODE_EXPR:
2635 pp_cxx_ws_string (pp, "@encode");
2636 pp_cxx_whitespace (pp);
2637 pp_cxx_left_paren (pp);
2638 dump_type (pp, TREE_OPERAND (t, 0), flags);
2639 pp_cxx_right_paren (pp);
2640 break;
2641
2642 case NOEXCEPT_EXPR:
2643 pp_cxx_ws_string (pp, "noexcept");
2644 pp_cxx_whitespace (pp);
2645 pp_cxx_left_paren (pp);
2646 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2647 pp_cxx_right_paren (pp);
2648 break;
2649
2650 case REALPART_EXPR:
2651 case IMAGPART_EXPR:
2652 pp_cxx_ws_string (pp, OVL_OP_INFO (false, TREE_CODE (t))->name);
2653 pp_cxx_whitespace (pp);
2654 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2655 break;
2656
2657 case DEFAULT_ARG:
2658 pp_string (pp, M_("<unparsed>"));
2659 break;
2660
2661 case TRY_CATCH_EXPR:
2662 case CLEANUP_POINT_EXPR:
2663 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2664 break;
2665
2666 case PSEUDO_DTOR_EXPR:
2667 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2668 pp_cxx_dot (pp);
2669 if (TREE_OPERAND (t, 1))
2670 {
2671 dump_type (pp, TREE_OPERAND (t, 1), flags);
2672 pp_cxx_colon_colon (pp);
2673 }
2674 pp_cxx_complement (pp);
2675 dump_type (pp, TREE_OPERAND (t, 2), flags);
2676 break;
2677
2678 case TEMPLATE_ID_EXPR:
2679 dump_decl (pp, t, flags);
2680 break;
2681
2682 case BIND_EXPR:
2683 case STMT_EXPR:
2684 case EXPR_STMT:
2685 case STATEMENT_LIST:
2686 /* We don't yet have a way of dumping statements in a
2687 human-readable format. */
2688 pp_string (pp, "({...})");
2689 break;
2690
2691 case LOOP_EXPR:
2692 pp_string (pp, "while (1) { ");
2693 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2694 pp_cxx_right_brace (pp);
2695 break;
2696
2697 case EXIT_EXPR:
2698 pp_string (pp, "if (");
2699 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2700 pp_string (pp, ") break; ");
2701 break;
2702
2703 case BASELINK:
2704 dump_expr (pp, BASELINK_FUNCTIONS (t), flags & ~TFF_EXPR_IN_PARENS);
2705 break;
2706
2707 case EMPTY_CLASS_EXPR:
2708 dump_type (pp, TREE_TYPE (t), flags);
2709 pp_cxx_left_paren (pp);
2710 pp_cxx_right_paren (pp);
2711 break;
2712
2713 case NON_DEPENDENT_EXPR:
2714 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2715 break;
2716
2717 case ARGUMENT_PACK_SELECT:
2718 dump_template_argument (pp, ARGUMENT_PACK_SELECT_FROM_PACK (t), flags);
2719 break;
2720
2721 case RECORD_TYPE:
2722 case UNION_TYPE:
2723 case ENUMERAL_TYPE:
2724 case REAL_TYPE:
2725 case VOID_TYPE:
2726 case BOOLEAN_TYPE:
2727 case INTEGER_TYPE:
2728 case COMPLEX_TYPE:
2729 case VECTOR_TYPE:
2730 case DECLTYPE_TYPE:
2731 pp_type_specifier_seq (pp, t);
2732 break;
2733
2734 case TYPENAME_TYPE:
2735 /* We get here when we want to print a dependent type as an
2736 id-expression, without any disambiguator decoration. */
2737 pp->id_expression (t);
2738 break;
2739
2740 case TEMPLATE_TYPE_PARM:
2741 case TEMPLATE_TEMPLATE_PARM:
2742 case BOUND_TEMPLATE_TEMPLATE_PARM:
2743 dump_type (pp, t, flags);
2744 break;
2745
2746 case TRAIT_EXPR:
2747 pp_cxx_trait_expression (pp, t);
2748 break;
2749
2750 case VA_ARG_EXPR:
2751 pp_cxx_va_arg_expression (pp, t);
2752 break;
2753
2754 case OFFSETOF_EXPR:
2755 pp_cxx_offsetof_expression (pp, t);
2756 break;
2757
2758 case ADDRESSOF_EXPR:
2759 pp_cxx_addressof_expression (pp, t);
2760 break;
2761
2762 case SCOPE_REF:
2763 dump_decl (pp, t, flags);
2764 break;
2765
2766 case EXPR_PACK_EXPANSION:
2767 case UNARY_LEFT_FOLD_EXPR:
2768 case UNARY_RIGHT_FOLD_EXPR:
2769 case BINARY_LEFT_FOLD_EXPR:
2770 case BINARY_RIGHT_FOLD_EXPR:
2771 case TYPEID_EXPR:
2772 case MEMBER_REF:
2773 case DOTSTAR_EXPR:
2774 case NEW_EXPR:
2775 case VEC_NEW_EXPR:
2776 case DELETE_EXPR:
2777 case VEC_DELETE_EXPR:
2778 case MODOP_EXPR:
2779 case ABS_EXPR:
2780 case ABSU_EXPR:
2781 case CONJ_EXPR:
2782 case VECTOR_CST:
2783 case FIXED_CST:
2784 case UNORDERED_EXPR:
2785 case ORDERED_EXPR:
2786 case UNLT_EXPR:
2787 case UNLE_EXPR:
2788 case UNGT_EXPR:
2789 case UNGE_EXPR:
2790 case UNEQ_EXPR:
2791 case LTGT_EXPR:
2792 case COMPLEX_EXPR:
2793 case BIT_FIELD_REF:
2794 case FIX_TRUNC_EXPR:
2795 case FLOAT_EXPR:
2796 pp->expression (t);
2797 break;
2798
2799 case TRUTH_AND_EXPR:
2800 case TRUTH_OR_EXPR:
2801 case TRUTH_XOR_EXPR:
2802 if (flags & TFF_EXPR_IN_PARENS)
2803 pp_cxx_left_paren (pp);
2804 pp->expression (t);
2805 if (flags & TFF_EXPR_IN_PARENS)
2806 pp_cxx_right_paren (pp);
2807 break;
2808
2809 case OBJ_TYPE_REF:
2810 dump_expr (pp, resolve_virtual_fun_from_obj_type_ref (t), flags);
2811 break;
2812
2813 case LAMBDA_EXPR:
2814 pp_string (pp, M_("<lambda>"));
2815 break;
2816
2817 case PAREN_EXPR:
2818 pp_cxx_left_paren (pp);
2819 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2820 pp_cxx_right_paren (pp);
2821 break;
2822
2823 case REQUIRES_EXPR:
2824 pp_cxx_requires_expr (cxx_pp, t);
2825 break;
2826
2827 case SIMPLE_REQ:
2828 pp_cxx_simple_requirement (cxx_pp, t);
2829 break;
2830
2831 case TYPE_REQ:
2832 pp_cxx_type_requirement (cxx_pp, t);
2833 break;
2834
2835 case COMPOUND_REQ:
2836 pp_cxx_compound_requirement (cxx_pp, t);
2837 break;
2838
2839 case NESTED_REQ:
2840 pp_cxx_nested_requirement (cxx_pp, t);
2841 break;
2842
2843 case PRED_CONSTR:
2844 case CHECK_CONSTR:
2845 case EXPR_CONSTR:
2846 case TYPE_CONSTR:
2847 case ICONV_CONSTR:
2848 case DEDUCT_CONSTR:
2849 case EXCEPT_CONSTR:
2850 case PARM_CONSTR:
2851 case CONJ_CONSTR:
2852 case DISJ_CONSTR:
2853 pp_cxx_constraint (cxx_pp, t);
2854 break;
2855
2856 case PLACEHOLDER_EXPR:
2857 pp_string (pp, M_("*this"));
2858 break;
2859
2860 case TREE_LIST:
2861 dump_expr_list (pp, t, flags);
2862 break;
2863
2864 /* This list is incomplete, but should suffice for now.
2865 It is very important that `sorry' does not call
2866 `report_error_function'. That could cause an infinite loop. */
2867 default:
2868 pp_unsupported_tree (pp, t);
2869 /* Fall through. */
2870 case ERROR_MARK:
2871 pp_string (pp, M_("<expression error>"));
2872 break;
2873 }
2874 }
2875
2876 static void
2877 dump_binary_op (cxx_pretty_printer *pp, const char *opstring, tree t,
2878 int flags)
2879 {
2880 pp_cxx_left_paren (pp);
2881 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2882 pp_cxx_whitespace (pp);
2883 if (opstring)
2884 pp_cxx_ws_string (pp, opstring);
2885 else
2886 pp_string (pp, M_("<unknown operator>"));
2887 pp_cxx_whitespace (pp);
2888 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2889 pp_cxx_right_paren (pp);
2890 }
2891
2892 static void
2893 dump_unary_op (cxx_pretty_printer *pp, const char *opstring, tree t, int flags)
2894 {
2895 if (flags & TFF_EXPR_IN_PARENS)
2896 pp_cxx_left_paren (pp);
2897 pp_cxx_ws_string (pp, opstring);
2898 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2899 if (flags & TFF_EXPR_IN_PARENS)
2900 pp_cxx_right_paren (pp);
2901 }
2902
2903 static void
2904 reinit_cxx_pp (void)
2905 {
2906 pp_clear_output_area (cxx_pp);
2907 cxx_pp->padding = pp_none;
2908 pp_indentation (cxx_pp) = 0;
2909 pp_needs_newline (cxx_pp) = false;
2910 cxx_pp->enclosing_scope = current_function_decl;
2911 }
2912
2913 /* Same as pp_formatted_text, except the return string is a separate
2914 copy and has a GGC storage duration, e.g. an indefinite lifetime. */
2915
2916 inline const char *
2917 pp_ggc_formatted_text (pretty_printer *pp)
2918 {
2919 return ggc_strdup (pp_formatted_text (pp));
2920 }
2921
2922 /* Exported interface to stringifying types, exprs and decls under TFF_*
2923 control. */
2924
2925 const char *
2926 type_as_string (tree typ, int flags)
2927 {
2928 reinit_cxx_pp ();
2929 pp_translate_identifiers (cxx_pp) = false;
2930 dump_type (cxx_pp, typ, flags);
2931 return pp_ggc_formatted_text (cxx_pp);
2932 }
2933
2934 const char *
2935 type_as_string_translate (tree typ, int flags)
2936 {
2937 reinit_cxx_pp ();
2938 dump_type (cxx_pp, typ, flags);
2939 return pp_ggc_formatted_text (cxx_pp);
2940 }
2941
2942 const char *
2943 expr_as_string (tree decl, int flags)
2944 {
2945 reinit_cxx_pp ();
2946 pp_translate_identifiers (cxx_pp) = false;
2947 dump_expr (cxx_pp, decl, flags);
2948 return pp_ggc_formatted_text (cxx_pp);
2949 }
2950
2951 /* Wrap decl_as_string with options appropriate for dwarf. */
2952
2953 const char *
2954 decl_as_dwarf_string (tree decl, int flags)
2955 {
2956 const char *name;
2957 /* Curiously, reinit_cxx_pp doesn't reset the flags field, so setting the flag
2958 here will be adequate to get the desired behavior. */
2959 cxx_pp->flags |= pp_c_flag_gnu_v3;
2960 name = decl_as_string (decl, flags);
2961 /* Subsequent calls to the pretty printer shouldn't use this style. */
2962 cxx_pp->flags &= ~pp_c_flag_gnu_v3;
2963 return name;
2964 }
2965
2966 const char *
2967 decl_as_string (tree decl, int flags)
2968 {
2969 reinit_cxx_pp ();
2970 pp_translate_identifiers (cxx_pp) = false;
2971 dump_decl (cxx_pp, decl, flags);
2972 return pp_ggc_formatted_text (cxx_pp);
2973 }
2974
2975 const char *
2976 decl_as_string_translate (tree decl, int flags)
2977 {
2978 reinit_cxx_pp ();
2979 dump_decl (cxx_pp, decl, flags);
2980 return pp_ggc_formatted_text (cxx_pp);
2981 }
2982
2983 /* Wrap lang_decl_name with options appropriate for dwarf. */
2984
2985 const char *
2986 lang_decl_dwarf_name (tree decl, int v, bool translate)
2987 {
2988 const char *name;
2989 /* Curiously, reinit_cxx_pp doesn't reset the flags field, so setting the flag
2990 here will be adequate to get the desired behavior. */
2991 cxx_pp->flags |= pp_c_flag_gnu_v3;
2992 name = lang_decl_name (decl, v, translate);
2993 /* Subsequent calls to the pretty printer shouldn't use this style. */
2994 cxx_pp->flags &= ~pp_c_flag_gnu_v3;
2995 return name;
2996 }
2997
2998 /* Generate the three forms of printable names for cxx_printable_name. */
2999
3000 const char *
3001 lang_decl_name (tree decl, int v, bool translate)
3002 {
3003 if (v >= 2)
3004 return (translate
3005 ? decl_as_string_translate (decl, TFF_DECL_SPECIFIERS)
3006 : decl_as_string (decl, TFF_DECL_SPECIFIERS));
3007
3008 reinit_cxx_pp ();
3009 pp_translate_identifiers (cxx_pp) = translate;
3010 if (v == 1
3011 && (DECL_CLASS_SCOPE_P (decl)
3012 || (DECL_NAMESPACE_SCOPE_P (decl)
3013 && CP_DECL_CONTEXT (decl) != global_namespace)))
3014 {
3015 dump_type (cxx_pp, CP_DECL_CONTEXT (decl), TFF_PLAIN_IDENTIFIER);
3016 pp_cxx_colon_colon (cxx_pp);
3017 }
3018
3019 if (TREE_CODE (decl) == FUNCTION_DECL)
3020 dump_function_name (cxx_pp, decl, TFF_PLAIN_IDENTIFIER);
3021 else if ((DECL_NAME (decl) == NULL_TREE)
3022 && TREE_CODE (decl) == NAMESPACE_DECL)
3023 dump_decl (cxx_pp, decl, TFF_PLAIN_IDENTIFIER | TFF_UNQUALIFIED_NAME);
3024 else
3025 dump_decl (cxx_pp, DECL_NAME (decl), TFF_PLAIN_IDENTIFIER);
3026
3027 return pp_ggc_formatted_text (cxx_pp);
3028 }
3029
3030 /* Return the location of a tree passed to %+ formats. */
3031
3032 location_t
3033 location_of (tree t)
3034 {
3035 if (TYPE_P (t))
3036 {
3037 t = TYPE_MAIN_DECL (t);
3038 if (t == NULL_TREE)
3039 return input_location;
3040 }
3041 else if (TREE_CODE (t) == OVERLOAD)
3042 t = OVL_FIRST (t);
3043
3044 if (DECL_P (t))
3045 return DECL_SOURCE_LOCATION (t);
3046 if (TREE_CODE (t) == DEFAULT_ARG)
3047 return defarg_location (t);
3048 return cp_expr_loc_or_loc (t, input_location);
3049 }
3050
3051 /* Now the interfaces from error et al to dump_type et al. Each takes an
3052 on/off VERBOSE flag and supply the appropriate TFF_ flags to a dump_
3053 function. */
3054
3055 static const char *
3056 decl_to_string (tree decl, int verbose)
3057 {
3058 int flags = 0;
3059
3060 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == RECORD_TYPE
3061 || TREE_CODE (decl) == UNION_TYPE || TREE_CODE (decl) == ENUMERAL_TYPE)
3062 flags = TFF_CLASS_KEY_OR_ENUM;
3063 if (verbose)
3064 flags |= TFF_DECL_SPECIFIERS;
3065 else if (TREE_CODE (decl) == FUNCTION_DECL)
3066 flags |= TFF_DECL_SPECIFIERS | TFF_RETURN_TYPE;
3067 flags |= TFF_TEMPLATE_HEADER;
3068
3069 reinit_cxx_pp ();
3070 dump_decl (cxx_pp, decl, flags);
3071 return pp_ggc_formatted_text (cxx_pp);
3072 }
3073
3074 const char *
3075 expr_to_string (tree decl)
3076 {
3077 reinit_cxx_pp ();
3078 dump_expr (cxx_pp, decl, 0);
3079 return pp_ggc_formatted_text (cxx_pp);
3080 }
3081
3082 static const char *
3083 fndecl_to_string (tree fndecl, int verbose)
3084 {
3085 int flags;
3086
3087 flags = TFF_EXCEPTION_SPECIFICATION | TFF_DECL_SPECIFIERS
3088 | TFF_TEMPLATE_HEADER;
3089 if (verbose)
3090 flags |= TFF_FUNCTION_DEFAULT_ARGUMENTS;
3091 reinit_cxx_pp ();
3092 dump_decl (cxx_pp, fndecl, flags);
3093 return pp_ggc_formatted_text (cxx_pp);
3094 }
3095
3096
3097 static const char *
3098 code_to_string (enum tree_code c)
3099 {
3100 return get_tree_code_name (c);
3101 }
3102
3103 const char *
3104 language_to_string (enum languages c)
3105 {
3106 switch (c)
3107 {
3108 case lang_c:
3109 return "C";
3110
3111 case lang_cplusplus:
3112 return "C++";
3113
3114 default:
3115 gcc_unreachable ();
3116 }
3117 return NULL;
3118 }
3119
3120 /* Return the proper printed version of a parameter to a C++ function. */
3121
3122 static const char *
3123 parm_to_string (int p)
3124 {
3125 reinit_cxx_pp ();
3126 if (p < 0)
3127 pp_string (cxx_pp, "'this'");
3128 else
3129 pp_decimal_int (cxx_pp, p + 1);
3130 return pp_ggc_formatted_text (cxx_pp);
3131 }
3132
3133 static const char *
3134 op_to_string (bool assop, enum tree_code p)
3135 {
3136 tree id = ovl_op_identifier (assop, p);
3137 return id ? IDENTIFIER_POINTER (id) : M_("<unknown>");
3138 }
3139
3140 /* Return a GC-allocated representation of type TYP, with verbosity VERBOSE.
3141
3142 If QUOTE is non-NULL and if *QUOTE is true, then quotes are added to the
3143 string in appropriate places, and *QUOTE is written to with false
3144 to suppress pp_format's trailing close quote so that e.g.
3145 foo_typedef {aka underlying_foo} {enum}
3146 can be printed by "%qT" as:
3147 `foo_typedef' {aka `underlying_foo'} {enum}
3148 rather than:
3149 `foo_typedef {aka underlying_foo} {enum}'
3150 When adding such quotes, if POSTPROCESSED is true (for handling %H and %I)
3151 then a leading open quote will be added, whereas if POSTPROCESSED is false
3152 (for handling %T) then any leading quote has already been added by
3153 pp_format, or is not needed due to QUOTE being NULL (for template arguments
3154 within %H and %I).
3155
3156 SHOW_COLOR is used to determine the colorization of any quotes that
3157 are added. */
3158
3159 static const char *
3160 type_to_string (tree typ, int verbose, bool postprocessed, bool *quote,
3161 bool show_color)
3162 {
3163 int flags = 0;
3164 if (verbose)
3165 flags |= TFF_CLASS_KEY_OR_ENUM;
3166 flags |= TFF_TEMPLATE_HEADER;
3167
3168 reinit_cxx_pp ();
3169
3170 if (postprocessed && quote && *quote)
3171 pp_begin_quote (cxx_pp, show_color);
3172
3173 struct obstack *ob = pp_buffer (cxx_pp)->obstack;
3174 int type_start, type_len;
3175 type_start = obstack_object_size (ob);
3176
3177 dump_type (cxx_pp, typ, flags);
3178
3179 /* Remember the end of the initial dump. */
3180 type_len = obstack_object_size (ob) - type_start;
3181
3182 /* If we're printing a type that involves typedefs, also print the
3183 stripped version. But sometimes the stripped version looks
3184 exactly the same, so we don't want it after all. To avoid printing
3185 it in that case, we play ugly obstack games. */
3186 if (typ && TYPE_P (typ) && typ != TYPE_CANONICAL (typ)
3187 && !uses_template_parms (typ))
3188 {
3189 int aka_start, aka_len; char *p;
3190 tree aka = strip_typedefs (typ);
3191 if (quote && *quote)
3192 pp_end_quote (cxx_pp, show_color);
3193 pp_string (cxx_pp, " {aka");
3194 pp_cxx_whitespace (cxx_pp);
3195 if (quote && *quote)
3196 pp_begin_quote (cxx_pp, show_color);
3197 /* And remember the start of the aka dump. */
3198 aka_start = obstack_object_size (ob);
3199 dump_type (cxx_pp, aka, flags);
3200 aka_len = obstack_object_size (ob) - aka_start;
3201 if (quote && *quote)
3202 pp_end_quote (cxx_pp, show_color);
3203 pp_right_brace (cxx_pp);
3204 p = (char*)obstack_base (ob);
3205 /* If they are identical, cut off the aka by unwinding the obstack. */
3206 if (type_len == aka_len
3207 && memcmp (p + type_start, p+aka_start, type_len) == 0)
3208 {
3209 /* We can't add a '\0' here, since we may be adding a closing quote
3210 below, and it would be hidden by the '\0'.
3211 Instead, manually unwind the current object within the obstack
3212 so that the insertion point is at the end of the type, before
3213 the "' {aka". */
3214 int delta = type_start + type_len - obstack_object_size (ob);
3215 gcc_assert (delta <= 0);
3216 obstack_blank_fast (ob, delta);
3217 }
3218 else
3219 if (quote)
3220 /* No further closing quotes are needed. */
3221 *quote = false;
3222 }
3223
3224 if (quote && *quote)
3225 {
3226 pp_end_quote (cxx_pp, show_color);
3227 *quote = false;
3228 }
3229 return pp_ggc_formatted_text (cxx_pp);
3230 }
3231
3232 static const char *
3233 args_to_string (tree p, int verbose)
3234 {
3235 int flags = 0;
3236 if (verbose)
3237 flags |= TFF_CLASS_KEY_OR_ENUM;
3238
3239 if (p == NULL_TREE)
3240 return "";
3241
3242 if (TYPE_P (TREE_VALUE (p)))
3243 return type_as_string_translate (p, flags);
3244
3245 reinit_cxx_pp ();
3246 for (; p; p = TREE_CHAIN (p))
3247 {
3248 if (null_node_p (TREE_VALUE (p)))
3249 pp_cxx_ws_string (cxx_pp, "NULL");
3250 else
3251 dump_type (cxx_pp, error_type (TREE_VALUE (p)), flags);
3252 if (TREE_CHAIN (p))
3253 pp_separate_with_comma (cxx_pp);
3254 }
3255 return pp_ggc_formatted_text (cxx_pp);
3256 }
3257
3258 /* Pretty-print a deduction substitution (from deduction_tsubst_fntype). P
3259 is a TREE_LIST with purpose the TEMPLATE_DECL, value the template
3260 arguments. */
3261
3262 static const char *
3263 subst_to_string (tree p)
3264 {
3265 tree decl = TREE_PURPOSE (p);
3266 tree targs = TREE_VALUE (p);
3267 tree tparms = DECL_TEMPLATE_PARMS (decl);
3268 int flags = (TFF_DECL_SPECIFIERS|TFF_TEMPLATE_HEADER
3269 |TFF_NO_TEMPLATE_BINDINGS);
3270
3271 if (p == NULL_TREE)
3272 return "";
3273
3274 reinit_cxx_pp ();
3275 dump_template_decl (cxx_pp, TREE_PURPOSE (p), flags);
3276 dump_substitution (cxx_pp, NULL, tparms, targs, /*flags=*/0);
3277 return pp_ggc_formatted_text (cxx_pp);
3278 }
3279
3280 static const char *
3281 cv_to_string (tree p, int v)
3282 {
3283 reinit_cxx_pp ();
3284 cxx_pp->padding = v ? pp_before : pp_none;
3285 pp_cxx_cv_qualifier_seq (cxx_pp, p);
3286 return pp_ggc_formatted_text (cxx_pp);
3287 }
3288
3289 static const char *
3290 eh_spec_to_string (tree p, int /*v*/)
3291 {
3292 int flags = 0;
3293 reinit_cxx_pp ();
3294 dump_exception_spec (cxx_pp, p, flags);
3295 return pp_ggc_formatted_text (cxx_pp);
3296 }
3297
3298 /* Langhook for print_error_function. */
3299 void
3300 cxx_print_error_function (diagnostic_context *context, const char *file,
3301 diagnostic_info *diagnostic)
3302 {
3303 char *prefix;
3304 if (file)
3305 prefix = xstrdup (file);
3306 else
3307 prefix = NULL;
3308 lhd_print_error_function (context, file, diagnostic);
3309 pp_set_prefix (context->printer, prefix);
3310 maybe_print_instantiation_context (context);
3311 }
3312
3313 static void
3314 cp_diagnostic_starter (diagnostic_context *context,
3315 diagnostic_info *diagnostic)
3316 {
3317 diagnostic_report_current_module (context, diagnostic_location (diagnostic));
3318 cp_print_error_function (context, diagnostic);
3319 maybe_print_instantiation_context (context);
3320 maybe_print_constexpr_context (context);
3321 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
3322 diagnostic));
3323 }
3324
3325 /* Print current function onto BUFFER, in the process of reporting
3326 a diagnostic message. Called from cp_diagnostic_starter. */
3327 static void
3328 cp_print_error_function (diagnostic_context *context,
3329 diagnostic_info *diagnostic)
3330 {
3331 /* If we are in an instantiation context, current_function_decl is likely
3332 to be wrong, so just rely on print_instantiation_full_context. */
3333 if (current_instantiation ())
3334 return;
3335 if (diagnostic_last_function_changed (context, diagnostic))
3336 {
3337 char *old_prefix = pp_take_prefix (context->printer);
3338 const char *file = LOCATION_FILE (diagnostic_location (diagnostic));
3339 tree abstract_origin = diagnostic_abstract_origin (diagnostic);
3340 char *new_prefix = (file && abstract_origin == NULL)
3341 ? file_name_as_prefix (context, file) : NULL;
3342
3343 pp_set_prefix (context->printer, new_prefix);
3344
3345 if (current_function_decl == NULL)
3346 pp_string (context->printer, _("At global scope:"));
3347 else
3348 {
3349 tree fndecl, ao;
3350
3351 if (abstract_origin)
3352 {
3353 ao = BLOCK_ABSTRACT_ORIGIN (abstract_origin);
3354 gcc_assert (TREE_CODE (ao) == FUNCTION_DECL);
3355 fndecl = ao;
3356 }
3357 else
3358 fndecl = current_function_decl;
3359
3360 pp_printf (context->printer, function_category (fndecl),
3361 cxx_printable_name_translate (fndecl, 2));
3362
3363 while (abstract_origin)
3364 {
3365 location_t *locus;
3366 tree block = abstract_origin;
3367
3368 locus = &BLOCK_SOURCE_LOCATION (block);
3369 fndecl = NULL;
3370 block = BLOCK_SUPERCONTEXT (block);
3371 while (block && TREE_CODE (block) == BLOCK
3372 && BLOCK_ABSTRACT_ORIGIN (block))
3373 {
3374 ao = BLOCK_ABSTRACT_ORIGIN (block);
3375 if (TREE_CODE (ao) == FUNCTION_DECL)
3376 {
3377 fndecl = ao;
3378 break;
3379 }
3380 else if (TREE_CODE (ao) != BLOCK)
3381 break;
3382
3383 block = BLOCK_SUPERCONTEXT (block);
3384 }
3385 if (fndecl)
3386 abstract_origin = block;
3387 else
3388 {
3389 while (block && TREE_CODE (block) == BLOCK)
3390 block = BLOCK_SUPERCONTEXT (block);
3391
3392 if (block && TREE_CODE (block) == FUNCTION_DECL)
3393 fndecl = block;
3394 abstract_origin = NULL;
3395 }
3396 if (fndecl)
3397 {
3398 expanded_location s = expand_location (*locus);
3399 pp_character (context->printer, ',');
3400 pp_newline (context->printer);
3401 if (s.file != NULL)
3402 {
3403 if (context->show_column && s.column != 0)
3404 pp_printf (context->printer,
3405 _(" inlined from %qs at %r%s:%d:%d%R"),
3406 cxx_printable_name_translate (fndecl, 2),
3407 "locus", s.file, s.line, s.column);
3408 else
3409 pp_printf (context->printer,
3410 _(" inlined from %qs at %r%s:%d%R"),
3411 cxx_printable_name_translate (fndecl, 2),
3412 "locus", s.file, s.line);
3413
3414 }
3415 else
3416 pp_printf (context->printer, _(" inlined from %qs"),
3417 cxx_printable_name_translate (fndecl, 2));
3418 }
3419 }
3420 pp_character (context->printer, ':');
3421 }
3422 pp_newline (context->printer);
3423
3424 diagnostic_set_last_function (context, diagnostic);
3425 pp_destroy_prefix (context->printer);
3426 context->printer->prefix = old_prefix;
3427 }
3428 }
3429
3430 /* Returns a description of FUNCTION using standard terminology. The
3431 result is a format string of the form "In CATEGORY %qs". */
3432 static const char *
3433 function_category (tree fn)
3434 {
3435 /* We can get called from the middle-end for diagnostics of function
3436 clones. Make sure we have language specific information before
3437 dereferencing it. */
3438 if (DECL_LANG_SPECIFIC (STRIP_TEMPLATE (fn))
3439 && DECL_FUNCTION_MEMBER_P (fn))
3440 {
3441 if (DECL_STATIC_FUNCTION_P (fn))
3442 return _("In static member function %qs");
3443 else if (DECL_COPY_CONSTRUCTOR_P (fn))
3444 return _("In copy constructor %qs");
3445 else if (DECL_CONSTRUCTOR_P (fn))
3446 return _("In constructor %qs");
3447 else if (DECL_DESTRUCTOR_P (fn))
3448 return _("In destructor %qs");
3449 else if (LAMBDA_FUNCTION_P (fn))
3450 return _("In lambda function");
3451 else
3452 return _("In member function %qs");
3453 }
3454 else
3455 return _("In function %qs");
3456 }
3457
3458 /* Report the full context of a current template instantiation,
3459 onto BUFFER. */
3460 static void
3461 print_instantiation_full_context (diagnostic_context *context)
3462 {
3463 struct tinst_level *p = current_instantiation ();
3464 location_t location = input_location;
3465
3466 if (p)
3467 {
3468 pp_verbatim (context->printer,
3469 p->list_p ()
3470 ? _("%s: In substitution of %qS:\n")
3471 : _("%s: In instantiation of %q#D:\n"),
3472 LOCATION_FILE (location),
3473 p->get_node ());
3474
3475 location = p->locus;
3476 p = p->next;
3477 }
3478
3479 print_instantiation_partial_context (context, p, location);
3480 }
3481
3482 /* Helper function of print_instantiation_partial_context() that
3483 prints a single line of instantiation context. */
3484
3485 static void
3486 print_instantiation_partial_context_line (diagnostic_context *context,
3487 struct tinst_level *t,
3488 location_t loc, bool recursive_p)
3489 {
3490 if (loc == UNKNOWN_LOCATION)
3491 return;
3492
3493 expanded_location xloc = expand_location (loc);
3494
3495 if (context->show_column)
3496 pp_verbatim (context->printer, _("%r%s:%d:%d:%R "),
3497 "locus", xloc.file, xloc.line, xloc.column);
3498 else
3499 pp_verbatim (context->printer, _("%r%s:%d:%R "),
3500 "locus", xloc.file, xloc.line);
3501
3502 if (t != NULL)
3503 {
3504 if (t->list_p ())
3505 pp_verbatim (context->printer,
3506 recursive_p
3507 ? _("recursively required by substitution of %qS\n")
3508 : _("required by substitution of %qS\n"),
3509 t->get_node ());
3510 else
3511 pp_verbatim (context->printer,
3512 recursive_p
3513 ? _("recursively required from %q#D\n")
3514 : _("required from %q#D\n"),
3515 t->get_node ());
3516 }
3517 else
3518 {
3519 pp_verbatim (context->printer,
3520 recursive_p
3521 ? _("recursively required from here\n")
3522 : _("required from here\n"));
3523 }
3524 }
3525
3526 /* Same as print_instantiation_full_context but less verbose. */
3527
3528 static void
3529 print_instantiation_partial_context (diagnostic_context *context,
3530 struct tinst_level *t0, location_t loc)
3531 {
3532 struct tinst_level *t;
3533 int n_total = 0;
3534 int n;
3535 location_t prev_loc = loc;
3536
3537 for (t = t0; t != NULL; t = t->next)
3538 if (prev_loc != t->locus)
3539 {
3540 prev_loc = t->locus;
3541 n_total++;
3542 }
3543
3544 t = t0;
3545
3546 if (template_backtrace_limit
3547 && n_total > template_backtrace_limit)
3548 {
3549 int skip = n_total - template_backtrace_limit;
3550 int head = template_backtrace_limit / 2;
3551
3552 /* Avoid skipping just 1. If so, skip 2. */
3553 if (skip == 1)
3554 {
3555 skip = 2;
3556 head = (template_backtrace_limit - 1) / 2;
3557 }
3558
3559 for (n = 0; n < head; n++)
3560 {
3561 gcc_assert (t != NULL);
3562 if (loc != t->locus)
3563 print_instantiation_partial_context_line (context, t, loc,
3564 /*recursive_p=*/false);
3565 loc = t->locus;
3566 t = t->next;
3567 }
3568 if (t != NULL && skip > 0)
3569 {
3570 expanded_location xloc;
3571 xloc = expand_location (loc);
3572 if (context->show_column)
3573 pp_verbatim (context->printer,
3574 _("%r%s:%d:%d:%R [ skipping %d instantiation "
3575 "contexts, use -ftemplate-backtrace-limit=0 to "
3576 "disable ]\n"),
3577 "locus", xloc.file, xloc.line, xloc.column, skip);
3578 else
3579 pp_verbatim (context->printer,
3580 _("%r%s:%d:%R [ skipping %d instantiation "
3581 "contexts, use -ftemplate-backtrace-limit=0 to "
3582 "disable ]\n"),
3583 "locus", xloc.file, xloc.line, skip);
3584
3585 do {
3586 loc = t->locus;
3587 t = t->next;
3588 } while (t != NULL && --skip > 0);
3589 }
3590 }
3591
3592 while (t != NULL)
3593 {
3594 while (t->next != NULL && t->locus == t->next->locus)
3595 {
3596 loc = t->locus;
3597 t = t->next;
3598 }
3599 print_instantiation_partial_context_line (context, t, loc,
3600 t->locus == loc);
3601 loc = t->locus;
3602 t = t->next;
3603 }
3604 print_instantiation_partial_context_line (context, NULL, loc,
3605 /*recursive_p=*/false);
3606 }
3607
3608 /* Called from cp_thing to print the template context for an error. */
3609 static void
3610 maybe_print_instantiation_context (diagnostic_context *context)
3611 {
3612 if (!problematic_instantiation_changed () || current_instantiation () == 0)
3613 return;
3614
3615 record_last_problematic_instantiation ();
3616 print_instantiation_full_context (context);
3617 }
3618 \f
3619 /* Report what constexpr call(s) we're trying to expand, if any. */
3620
3621 void
3622 maybe_print_constexpr_context (diagnostic_context *context)
3623 {
3624 vec<tree> call_stack = cx_error_context ();
3625 unsigned ix;
3626 tree t;
3627
3628 FOR_EACH_VEC_ELT (call_stack, ix, t)
3629 {
3630 expanded_location xloc = expand_location (EXPR_LOCATION (t));
3631 const char *s = expr_as_string (t, 0);
3632 if (context->show_column)
3633 pp_verbatim (context->printer,
3634 _("%r%s:%d:%d:%R in %<constexpr%> expansion of %qs"),
3635 "locus", xloc.file, xloc.line, xloc.column, s);
3636 else
3637 pp_verbatim (context->printer,
3638 _("%r%s:%d:%R in %<constexpr%> expansion of %qs"),
3639 "locus", xloc.file, xloc.line, s);
3640 pp_newline (context->printer);
3641 }
3642 }
3643 \f
3644
3645 /* Return true iff TYPE_A and TYPE_B are template types that are
3646 meaningful to compare. */
3647
3648 static bool
3649 comparable_template_types_p (tree type_a, tree type_b)
3650 {
3651 if (!CLASS_TYPE_P (type_a))
3652 return false;
3653 if (!CLASS_TYPE_P (type_b))
3654 return false;
3655
3656 tree tinfo_a = TYPE_TEMPLATE_INFO (type_a);
3657 tree tinfo_b = TYPE_TEMPLATE_INFO (type_b);
3658 if (!tinfo_a || !tinfo_b)
3659 return false;
3660
3661 return TI_TEMPLATE (tinfo_a) == TI_TEMPLATE (tinfo_b);
3662 }
3663
3664 /* Start a new line indented by SPC spaces on PP. */
3665
3666 static void
3667 newline_and_indent (pretty_printer *pp, int spc)
3668 {
3669 pp_newline (pp);
3670 for (int i = 0; i < spc; i++)
3671 pp_space (pp);
3672 }
3673
3674 /* Generate a GC-allocated string for ARG, an expression or type. */
3675
3676 static const char *
3677 arg_to_string (tree arg, bool verbose)
3678 {
3679 if (TYPE_P (arg))
3680 return type_to_string (arg, verbose, true, NULL, false);
3681 else
3682 return expr_to_string (arg);
3683 }
3684
3685 /* Subroutine to type_to_string_with_compare and
3686 print_template_tree_comparison.
3687
3688 Print a representation of ARG (an expression or type) to PP,
3689 colorizing it as "type-diff" if PP->show_color. */
3690
3691 static void
3692 print_nonequal_arg (pretty_printer *pp, tree arg, bool verbose)
3693 {
3694 pp_printf (pp, "%r%s%R",
3695 "type-diff",
3696 (arg
3697 ? arg_to_string (arg, verbose)
3698 : G_("(no argument)")));
3699 }
3700
3701 /* Recursively print template TYPE_A to PP, as compared to template TYPE_B.
3702
3703 The types must satisfy comparable_template_types_p.
3704
3705 If INDENT is 0, then this is equivalent to type_to_string (TYPE_A), but
3706 potentially colorizing/eliding in comparison with TYPE_B.
3707
3708 For example given types:
3709 vector<map<int,double>>
3710 and
3711 vector<map<int,float>>
3712 then the result on PP would be:
3713 vector<map<[...],double>>
3714 with type elision, and:
3715 vector<map<int,double>>
3716 without type elision.
3717
3718 In both cases the parts of TYPE that differ from PEER will be colorized
3719 if pp_show_color (pp) is true. In the above example, this would be
3720 "double".
3721
3722 If INDENT is non-zero, then the types are printed in a tree-like form
3723 which shows both types. In the above example, the result on PP would be:
3724
3725 vector<
3726 map<
3727 [...],
3728 [double != float]>>
3729
3730 and without type-elision would be:
3731
3732 vector<
3733 map<
3734 int,
3735 [double != float]>>
3736
3737 As before, the differing parts of the types are colorized if
3738 pp_show_color (pp) is true ("double" and "float" in this example).
3739
3740 Template arguments in which both types are using the default arguments
3741 are not printed; if at least one of the two types is using a non-default
3742 argument, then that argument is printed (or both arguments for the
3743 tree-like print format). */
3744
3745 static void
3746 print_template_differences (pretty_printer *pp, tree type_a, tree type_b,
3747 bool verbose, int indent)
3748 {
3749 if (indent)
3750 newline_and_indent (pp, indent);
3751
3752 tree tinfo_a = TYPE_TEMPLATE_INFO (type_a);
3753 tree tinfo_b = TYPE_TEMPLATE_INFO (type_b);
3754
3755 pp_printf (pp, "%s<",
3756 IDENTIFIER_POINTER (DECL_NAME (TI_TEMPLATE (tinfo_a))));
3757
3758 tree args_a = TI_ARGS (tinfo_a);
3759 tree args_b = TI_ARGS (tinfo_b);
3760 gcc_assert (TREE_CODE (args_a) == TREE_VEC);
3761 gcc_assert (TREE_CODE (args_b) == TREE_VEC);
3762 int flags = 0;
3763 int len_a = get_non_default_template_args_count (args_a, flags);
3764 args_a = INNERMOST_TEMPLATE_ARGS (args_a);
3765 int len_b = get_non_default_template_args_count (args_b, flags);
3766 args_b = INNERMOST_TEMPLATE_ARGS (args_b);
3767 /* Determine the maximum range of args for which non-default template args
3768 were used; beyond this, only default args (if any) were used, and so
3769 they will be equal from this point onwards.
3770 One of the two peers might have used default arguments within this
3771 range, but the other will be using non-default arguments, and so
3772 it's more readable to print both within this range, to highlight
3773 the differences. */
3774 int len_max = MAX (len_a, len_b);
3775 gcc_assert (TREE_CODE (args_a) == TREE_VEC);
3776 gcc_assert (TREE_CODE (args_b) == TREE_VEC);
3777 for (int idx = 0; idx < len_max; idx++)
3778 {
3779 if (idx)
3780 pp_character (pp, ',');
3781
3782 tree arg_a = TREE_VEC_ELT (args_a, idx);
3783 tree arg_b = TREE_VEC_ELT (args_b, idx);
3784 if (arg_a == arg_b)
3785 {
3786 if (indent)
3787 newline_and_indent (pp, indent + 2);
3788 /* Can do elision here, printing "[...]". */
3789 if (flag_elide_type)
3790 pp_string (pp, G_("[...]"));
3791 else
3792 pp_string (pp, arg_to_string (arg_a, verbose));
3793 }
3794 else
3795 {
3796 int new_indent = indent ? indent + 2 : 0;
3797 if (comparable_template_types_p (arg_a, arg_b))
3798 print_template_differences (pp, arg_a, arg_b, verbose, new_indent);
3799 else
3800 if (indent)
3801 {
3802 newline_and_indent (pp, indent + 2);
3803 pp_character (pp, '[');
3804 print_nonequal_arg (pp, arg_a, verbose);
3805 pp_string (pp, " != ");
3806 print_nonequal_arg (pp, arg_b, verbose);
3807 pp_character (pp, ']');
3808 }
3809 else
3810 print_nonequal_arg (pp, arg_a, verbose);
3811 }
3812 }
3813 pp_printf (pp, ">");
3814 }
3815
3816 /* As type_to_string, but for a template, potentially colorizing/eliding
3817 in comparison with PEER.
3818 For example, if TYPE is map<int,double> and PEER is map<int,int>,
3819 then the resulting string would be:
3820 map<[...],double>
3821 with type elision, and:
3822 map<int,double>
3823 without type elision.
3824
3825 In both cases the parts of TYPE that differ from PEER will be colorized
3826 if SHOW_COLOR is true. In the above example, this would be "double".
3827
3828 Template arguments in which both types are using the default arguments
3829 are not printed; if at least one of the two types is using a non-default
3830 argument, then both arguments are printed.
3831
3832 The resulting string is in a GC-allocated buffer. */
3833
3834 static const char *
3835 type_to_string_with_compare (tree type, tree peer, bool verbose,
3836 bool show_color)
3837 {
3838 pretty_printer inner_pp;
3839 pretty_printer *pp = &inner_pp;
3840 pp_show_color (pp) = show_color;
3841
3842 print_template_differences (pp, type, peer, verbose, 0);
3843 return pp_ggc_formatted_text (pp);
3844 }
3845
3846 /* Recursively print a tree-like comparison of TYPE_A and TYPE_B to PP,
3847 indented by INDENT spaces.
3848
3849 For example given types:
3850
3851 vector<map<int,double>>
3852
3853 and
3854
3855 vector<map<double,float>>
3856
3857 the output with type elision would be:
3858
3859 vector<
3860 map<
3861 [...],
3862 [double != float]>>
3863
3864 and without type-elision would be:
3865
3866 vector<
3867 map<
3868 int,
3869 [double != float]>>
3870
3871 TYPE_A and TYPE_B must both be comparable template types
3872 (as per comparable_template_types_p).
3873
3874 Template arguments in which both types are using the default arguments
3875 are not printed; if at least one of the two types is using a non-default
3876 argument, then both arguments are printed. */
3877
3878 static void
3879 print_template_tree_comparison (pretty_printer *pp, tree type_a, tree type_b,
3880 bool verbose, int indent)
3881 {
3882 print_template_differences (pp, type_a, type_b, verbose, indent);
3883 }
3884
3885 /* Subroutine for use in a format_postprocessor::handle
3886 implementation. Adds a chunk to the end of
3887 formatted output, so that it will be printed
3888 by pp_output_formatted_text. */
3889
3890 static void
3891 append_formatted_chunk (pretty_printer *pp, const char *content)
3892 {
3893 output_buffer *buffer = pp_buffer (pp);
3894 struct chunk_info *chunk_array = buffer->cur_chunk_array;
3895 const char **args = chunk_array->args;
3896
3897 unsigned int chunk_idx;
3898 for (chunk_idx = 0; args[chunk_idx]; chunk_idx++)
3899 ;
3900 args[chunk_idx++] = content;
3901 args[chunk_idx] = NULL;
3902 }
3903
3904 /* Create a copy of CONTENT, with quotes added, and,
3905 potentially, with colorization.
3906 No escaped is performed on CONTENT.
3907 The result is in a GC-allocated buffer. */
3908
3909 static const char *
3910 add_quotes (const char *content, bool show_color)
3911 {
3912 pretty_printer tmp_pp;
3913 pp_show_color (&tmp_pp) = show_color;
3914
3915 /* We have to use "%<%s%>" rather than "%qs" here in order to avoid
3916 quoting colorization bytes within the results. */
3917 pp_printf (&tmp_pp, "%<%s%>", content);
3918
3919 return pp_ggc_formatted_text (&tmp_pp);
3920 }
3921
3922 /* If we had %H and %I, and hence deferred printing them,
3923 print them now, storing the result into the chunk_info
3924 for pp_format. Quote them if 'q' was provided.
3925 Also print the difference in tree form, adding it as
3926 an additional chunk. */
3927
3928 void
3929 cxx_format_postprocessor::handle (pretty_printer *pp)
3930 {
3931 /* If we have one of %H and %I, the other should have
3932 been present. */
3933 if (m_type_a.m_tree || m_type_b.m_tree)
3934 {
3935 /* Avoid reentrancy issues by working with a copy of
3936 m_type_a and m_type_b, resetting them now. */
3937 deferred_printed_type type_a = m_type_a;
3938 deferred_printed_type type_b = m_type_b;
3939 m_type_a = deferred_printed_type ();
3940 m_type_b = deferred_printed_type ();
3941
3942 gcc_assert (type_a.m_buffer_ptr);
3943 gcc_assert (type_b.m_buffer_ptr);
3944
3945 bool show_color = pp_show_color (pp);
3946
3947 const char *type_a_text;
3948 const char *type_b_text;
3949
3950 if (comparable_template_types_p (type_a.m_tree, type_b.m_tree))
3951 {
3952 type_a_text
3953 = type_to_string_with_compare (type_a.m_tree, type_b.m_tree,
3954 type_a.m_verbose, show_color);
3955 type_b_text
3956 = type_to_string_with_compare (type_b.m_tree, type_a.m_tree,
3957 type_b.m_verbose, show_color);
3958
3959 if (flag_diagnostics_show_template_tree)
3960 {
3961 pretty_printer inner_pp;
3962 pp_show_color (&inner_pp) = pp_show_color (pp);
3963 print_template_tree_comparison
3964 (&inner_pp, type_a.m_tree, type_b.m_tree, type_a.m_verbose, 2);
3965 append_formatted_chunk (pp, pp_ggc_formatted_text (&inner_pp));
3966 }
3967 }
3968 else
3969 {
3970 /* If the types were not comparable (or if only one of %H/%I was
3971 provided), they are printed normally, and no difference tree
3972 is printed. */
3973 type_a_text = type_to_string (type_a.m_tree, type_a.m_verbose,
3974 true, &type_a.m_quote, show_color);
3975 type_b_text = type_to_string (type_b.m_tree, type_b.m_verbose,
3976 true, &type_b.m_quote, show_color);
3977 }
3978
3979 if (type_a.m_quote)
3980 type_a_text = add_quotes (type_a_text, show_color);
3981 *type_a.m_buffer_ptr = type_a_text;
3982
3983 if (type_b.m_quote)
3984 type_b_text = add_quotes (type_b_text, show_color);
3985 *type_b.m_buffer_ptr = type_b_text;
3986 }
3987 }
3988
3989 /* Subroutine for handling %H and %I, to support i18n of messages like:
3990
3991 error_at (loc, "could not convert %qE from %qH to %qI",
3992 expr, type_a, type_b);
3993
3994 so that we can print things like:
3995
3996 could not convert 'foo' from 'map<int,double>' to 'map<int,int>'
3997
3998 and, with type-elision:
3999
4000 could not convert 'foo' from 'map<[...],double>' to 'map<[...],int>'
4001
4002 (with color-coding of the differences between the types).
4003
4004 The %H and %I format codes are peers: both must be present,
4005 and they affect each other. Hence to handle them, we must
4006 delay printing until we have both, deferring the printing to
4007 pretty_printer's m_format_postprocessor hook.
4008
4009 This is called in phase 2 of pp_format, when it is accumulating
4010 a series of formatted chunks. We stash the location of the chunk
4011 we're meant to have written to, so that we can write to it in the
4012 m_format_postprocessor hook.
4013
4014 We also need to stash whether a 'q' prefix was provided (the QUOTE
4015 param) so that we can add the quotes when writing out the delayed
4016 chunk. */
4017
4018 static void
4019 defer_phase_2_of_type_diff (deferred_printed_type *deferred,
4020 tree type, const char **buffer_ptr,
4021 bool verbose, bool quote)
4022 {
4023 gcc_assert (deferred->m_tree == NULL_TREE);
4024 gcc_assert (deferred->m_buffer_ptr == NULL);
4025 *deferred = deferred_printed_type (type, buffer_ptr, verbose, quote);
4026 }
4027
4028
4029 /* Called from output_format -- during diagnostic message processing --
4030 to handle C++ specific format specifier with the following meanings:
4031 %A function argument-list.
4032 %C tree code.
4033 %D declaration.
4034 %E expression.
4035 %F function declaration.
4036 %G gcall *
4037 %H type difference (from).
4038 %I type difference (to).
4039 %K tree
4040 %L language as used in extern "lang".
4041 %O binary operator.
4042 %P function parameter whose position is indicated by an integer.
4043 %Q assignment operator.
4044 %S substitution (template + args)
4045 %T type.
4046 %V cv-qualifier.
4047 %X exception-specification. */
4048 static bool
4049 cp_printer (pretty_printer *pp, text_info *text, const char *spec,
4050 int precision, bool wide, bool set_locus, bool verbose,
4051 bool *quoted, const char **buffer_ptr)
4052 {
4053 gcc_assert (pp->m_format_postprocessor);
4054 cxx_format_postprocessor *postprocessor
4055 = static_cast <cxx_format_postprocessor *> (pp->m_format_postprocessor);
4056
4057 const char *result;
4058 tree t = NULL;
4059 #define next_tree (t = va_arg (*text->args_ptr, tree))
4060 #define next_tcode ((enum tree_code) va_arg (*text->args_ptr, int))
4061 #define next_lang ((enum languages) va_arg (*text->args_ptr, int))
4062 #define next_int va_arg (*text->args_ptr, int)
4063
4064 if (precision != 0 || wide)
4065 return false;
4066
4067 switch (*spec)
4068 {
4069 case 'A': result = args_to_string (next_tree, verbose); break;
4070 case 'C': result = code_to_string (next_tcode); break;
4071 case 'D':
4072 {
4073 tree temp = next_tree;
4074 if (VAR_P (temp)
4075 && DECL_HAS_DEBUG_EXPR_P (temp))
4076 {
4077 temp = DECL_DEBUG_EXPR (temp);
4078 if (!DECL_P (temp))
4079 {
4080 result = expr_to_string (temp);
4081 break;
4082 }
4083 }
4084 result = decl_to_string (temp, verbose);
4085 }
4086 break;
4087 case 'E': result = expr_to_string (next_tree); break;
4088 case 'F': result = fndecl_to_string (next_tree, verbose); break;
4089 case 'G':
4090 percent_G_format (text);
4091 return true;
4092 case 'H':
4093 defer_phase_2_of_type_diff (&postprocessor->m_type_a, next_tree,
4094 buffer_ptr, verbose, *quoted);
4095 return true;
4096 case 'I':
4097 defer_phase_2_of_type_diff (&postprocessor->m_type_b, next_tree,
4098 buffer_ptr, verbose, *quoted);
4099 return true;
4100 case 'K':
4101 t = va_arg (*text->args_ptr, tree);
4102 percent_K_format (text, EXPR_LOCATION (t), TREE_BLOCK (t));
4103 return true;
4104 case 'L': result = language_to_string (next_lang); break;
4105 case 'O': result = op_to_string (false, next_tcode); break;
4106 case 'P': result = parm_to_string (next_int); break;
4107 case 'Q': result = op_to_string (true, next_tcode); break;
4108 case 'S': result = subst_to_string (next_tree); break;
4109 case 'T':
4110 {
4111 result = type_to_string (next_tree, verbose, false, quoted,
4112 pp_show_color (pp));
4113 }
4114 break;
4115 case 'V': result = cv_to_string (next_tree, verbose); break;
4116 case 'X': result = eh_spec_to_string (next_tree, verbose); break;
4117
4118 default:
4119 return false;
4120 }
4121
4122 pp_string (pp, result);
4123 if (set_locus && t != NULL)
4124 text->set_location (0, location_of (t), SHOW_RANGE_WITH_CARET);
4125 return true;
4126 #undef next_tree
4127 #undef next_tcode
4128 #undef next_lang
4129 #undef next_int
4130 }
4131 \f
4132 /* Warn about the use of C++0x features when appropriate. */
4133 void
4134 maybe_warn_cpp0x (cpp0x_warn_str str)
4135 {
4136 if ((cxx_dialect == cxx98) && !in_system_header_at (input_location))
4137 /* We really want to suppress this warning in system headers,
4138 because libstdc++ uses variadic templates even when we aren't
4139 in C++0x mode. */
4140 switch (str)
4141 {
4142 case CPP0X_INITIALIZER_LISTS:
4143 pedwarn (input_location, 0,
4144 "extended initializer lists "
4145 "only available with -std=c++11 or -std=gnu++11");
4146 break;
4147 case CPP0X_EXPLICIT_CONVERSION:
4148 pedwarn (input_location, 0,
4149 "explicit conversion operators "
4150 "only available with -std=c++11 or -std=gnu++11");
4151 break;
4152 case CPP0X_VARIADIC_TEMPLATES:
4153 pedwarn (input_location, 0,
4154 "variadic templates "
4155 "only available with -std=c++11 or -std=gnu++11");
4156 break;
4157 case CPP0X_LAMBDA_EXPR:
4158 pedwarn (input_location, 0,
4159 "lambda expressions "
4160 "only available with -std=c++11 or -std=gnu++11");
4161 break;
4162 case CPP0X_AUTO:
4163 pedwarn (input_location, 0,
4164 "C++11 auto only available with -std=c++11 or -std=gnu++11");
4165 break;
4166 case CPP0X_SCOPED_ENUMS:
4167 pedwarn (input_location, 0,
4168 "scoped enums only available with -std=c++11 or -std=gnu++11");
4169 break;
4170 case CPP0X_DEFAULTED_DELETED:
4171 pedwarn (input_location, 0,
4172 "defaulted and deleted functions "
4173 "only available with -std=c++11 or -std=gnu++11");
4174 break;
4175 case CPP0X_INLINE_NAMESPACES:
4176 pedwarn (input_location, OPT_Wpedantic,
4177 "inline namespaces "
4178 "only available with -std=c++11 or -std=gnu++11");
4179 break;
4180 case CPP0X_OVERRIDE_CONTROLS:
4181 pedwarn (input_location, 0,
4182 "override controls (override/final) "
4183 "only available with -std=c++11 or -std=gnu++11");
4184 break;
4185 case CPP0X_NSDMI:
4186 pedwarn (input_location, 0,
4187 "non-static data member initializers "
4188 "only available with -std=c++11 or -std=gnu++11");
4189 break;
4190 case CPP0X_USER_DEFINED_LITERALS:
4191 pedwarn (input_location, 0,
4192 "user-defined literals "
4193 "only available with -std=c++11 or -std=gnu++11");
4194 break;
4195 case CPP0X_DELEGATING_CTORS:
4196 pedwarn (input_location, 0,
4197 "delegating constructors "
4198 "only available with -std=c++11 or -std=gnu++11");
4199 break;
4200 case CPP0X_INHERITING_CTORS:
4201 pedwarn (input_location, 0,
4202 "inheriting constructors "
4203 "only available with -std=c++11 or -std=gnu++11");
4204 break;
4205 case CPP0X_ATTRIBUTES:
4206 pedwarn (input_location, 0,
4207 "c++11 attributes "
4208 "only available with -std=c++11 or -std=gnu++11");
4209 break;
4210 case CPP0X_REF_QUALIFIER:
4211 pedwarn (input_location, 0,
4212 "ref-qualifiers "
4213 "only available with -std=c++11 or -std=gnu++11");
4214 break;
4215 default:
4216 gcc_unreachable ();
4217 }
4218 }
4219
4220 /* Warn about the use of variadic templates when appropriate. */
4221 void
4222 maybe_warn_variadic_templates (void)
4223 {
4224 maybe_warn_cpp0x (CPP0X_VARIADIC_TEMPLATES);
4225 }
4226
4227
4228 /* Issue an ISO C++98 pedantic warning at LOCATION, conditional on
4229 option OPT with text GMSGID. Use this function to report
4230 diagnostics for constructs that are invalid C++98, but valid
4231 C++0x. */
4232 bool
4233 pedwarn_cxx98 (location_t location, int opt, const char *gmsgid, ...)
4234 {
4235 diagnostic_info diagnostic;
4236 va_list ap;
4237 bool ret;
4238 rich_location richloc (line_table, location);
4239
4240 va_start (ap, gmsgid);
4241 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
4242 (cxx_dialect == cxx98) ? DK_PEDWARN : DK_WARNING);
4243 diagnostic.option_index = opt;
4244 ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
4245 va_end (ap);
4246 return ret;
4247 }
4248
4249 /* Issue a diagnostic that NAME cannot be found in SCOPE. DECL is what
4250 we found when we tried to do the lookup. LOCATION is the location of
4251 the NAME identifier. */
4252
4253 void
4254 qualified_name_lookup_error (tree scope, tree name,
4255 tree decl, location_t location)
4256 {
4257 if (scope == error_mark_node)
4258 ; /* We already complained. */
4259 else if (TYPE_P (scope))
4260 {
4261 if (!COMPLETE_TYPE_P (scope))
4262 error_at (location, "incomplete type %qT used in nested name specifier",
4263 scope);
4264 else if (TREE_CODE (decl) == TREE_LIST)
4265 {
4266 error_at (location, "reference to %<%T::%D%> is ambiguous",
4267 scope, name);
4268 print_candidates (decl);
4269 }
4270 else
4271 {
4272 name_hint hint;
4273 if (SCOPED_ENUM_P (scope))
4274 hint = suggest_alternative_in_scoped_enum (name, scope);
4275 if (const char *suggestion = hint.suggestion ())
4276 {
4277 gcc_rich_location richloc (location);
4278 richloc.add_fixit_replace (suggestion);
4279 error_at (&richloc,
4280 "%qD is not a member of %qT; did you mean %qs?",
4281 name, scope, suggestion);
4282 }
4283 else
4284 error_at (location, "%qD is not a member of %qT", name, scope);
4285 }
4286 }
4287 else if (scope != global_namespace)
4288 {
4289 auto_diagnostic_group d;
4290 bool emit_fixit = true;
4291 name_hint hint
4292 = suggest_alternative_in_explicit_scope (location, name, scope);
4293 if (!hint)
4294 {
4295 hint = suggest_alternatives_in_other_namespaces (location, name);
4296 /* "location" is just the location of the name, not of the explicit
4297 scope, and it's not easy to get at the latter, so we can't issue
4298 fix-it hints for the suggestion. */
4299 emit_fixit = false;
4300 }
4301 if (const char *suggestion = hint.suggestion ())
4302 {
4303 gcc_rich_location richloc (location);
4304 if (emit_fixit)
4305 richloc.add_fixit_replace (suggestion);
4306 error_at (&richloc, "%qD is not a member of %qD; did you mean %qs?",
4307 name, scope, suggestion);
4308 }
4309 else
4310 error_at (location, "%qD is not a member of %qD", name, scope);
4311 }
4312 else
4313 {
4314 auto_diagnostic_group d;
4315 name_hint hint = suggest_alternatives_for (location, name, true);
4316 if (const char *suggestion = hint.suggestion ())
4317 {
4318 gcc_rich_location richloc (location);
4319 richloc.add_fixit_replace (suggestion);
4320 error_at (&richloc,
4321 "%<::%D%> has not been declared; did you mean %qs?",
4322 name, suggestion);
4323 }
4324 else
4325 error_at (location, "%<::%D%> has not been declared", name);
4326 }
4327 }
4328
4329 /* C++-specific implementation of range_label::get_text () vfunc for
4330 range_label_for_type_mismatch.
4331
4332 Compare with print_template_differences above. */
4333
4334 label_text
4335 range_label_for_type_mismatch::get_text (unsigned /*range_idx*/) const
4336 {
4337 if (m_labelled_type == NULL_TREE)
4338 return label_text (NULL, false);
4339
4340 const bool verbose = false;
4341 const bool show_color = false;
4342
4343 const char *result;
4344 if (m_other_type
4345 && comparable_template_types_p (m_labelled_type, m_other_type))
4346 result = type_to_string_with_compare (m_labelled_type, m_other_type,
4347 verbose, show_color);
4348 else
4349 result = type_to_string (m_labelled_type, verbose, true, NULL, show_color);
4350
4351 /* Both of the above return GC-allocated buffers, so the caller mustn't
4352 free them. */
4353 return label_text (const_cast <char *> (result), false);
4354 }