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