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