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