OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [gcc/] [cp/] [error.c] - Blame information for rev 731

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 710 jeremybenn
/* Call-backs for C++ error reporting.
2
   This code is non-reentrant.
3
   Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2003,
4
   2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
5
   Free Software Foundation, Inc.
6
   This file is part of GCC.
7
 
8
GCC is free software; you can redistribute it and/or modify
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation; either version 3, or (at your option)
11
any later version.
12
 
13
GCC is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
GNU General Public License for more details.
17
 
18
You should have received a copy of the GNU General Public License
19
along with GCC; see the file COPYING3.  If not see
20
<http://www.gnu.org/licenses/>.  */
21
 
22
#include "config.h"
23
#include "system.h"
24
#include "coretypes.h"
25
#include "tm.h"
26
#include "tree.h"
27
#include "cp-tree.h"
28
#include "flags.h"
29
#include "diagnostic.h"
30
#include "tree-diagnostic.h"
31
#include "langhooks-def.h"
32
#include "intl.h"
33
#include "cxx-pretty-print.h"
34
#include "tree-pretty-print.h"
35
#include "pointer-set.h"
36
#include "c-family/c-objc.h"
37
 
38
#define pp_separate_with_comma(PP) pp_cxx_separate_with (PP, ',')
39
#define pp_separate_with_semicolon(PP) pp_cxx_separate_with (PP, ';')
40
 
41
/* The global buffer where we dump everything.  It is there only for
42
   transitional purpose.  It is expected, in the near future, to be
43
   completely removed.  */
44
static cxx_pretty_printer scratch_pretty_printer;
45
#define cxx_pp (&scratch_pretty_printer)
46
 
47
/* Translate if being used for diagnostics, but not for dump files or
48
   __PRETTY_FUNCTION.  */
49
#define M_(msgid) (pp_translate_identifiers (cxx_pp) ? _(msgid) : (msgid))
50
 
51
# define NEXT_CODE(T) (TREE_CODE (TREE_TYPE (T)))
52
 
53
static const char *args_to_string (tree, int);
54
static const char *assop_to_string (enum tree_code);
55
static const char *code_to_string (enum tree_code);
56
static const char *cv_to_string (tree, int);
57
static const char *decl_to_string (tree, int);
58
static const char *expr_to_string (tree);
59
static const char *fndecl_to_string (tree, int);
60
static const char *op_to_string (enum tree_code);
61
static const char *parm_to_string (int);
62
static const char *type_to_string (tree, int);
63
 
64
static void dump_alias_template_specialization (tree, int);
65
static void dump_type (tree, int);
66
static void dump_typename (tree, int);
67
static void dump_simple_decl (tree, tree, int);
68
static void dump_decl (tree, int);
69
static void dump_template_decl (tree, int);
70
static void dump_function_decl (tree, int);
71
static void dump_expr (tree, int);
72
static void dump_unary_op (const char *, tree, int);
73
static void dump_binary_op (const char *, tree, int);
74
static void dump_aggr_type (tree, int);
75
static void dump_type_prefix (tree, int);
76
static void dump_type_suffix (tree, int);
77
static void dump_function_name (tree, int);
78
static void dump_call_expr_args (tree, int, bool);
79
static void dump_aggr_init_expr_args (tree, int, bool);
80
static void dump_expr_list (tree, int);
81
static void dump_global_iord (tree);
82
static void dump_parameters (tree, int);
83
static void dump_exception_spec (tree, int);
84
static void dump_template_argument (tree, int);
85
static void dump_template_argument_list (tree, int);
86
static void dump_template_parameter (tree, int);
87
static void dump_template_bindings (tree, tree, VEC(tree,gc) *);
88
static void dump_scope (tree, int);
89
static void dump_template_parms (tree, int, int);
90
static int get_non_default_template_args_count (tree, int);
91
static const char *function_category (tree);
92
static void maybe_print_constexpr_context (diagnostic_context *);
93
static void maybe_print_instantiation_context (diagnostic_context *);
94
static void print_instantiation_full_context (diagnostic_context *);
95
static void print_instantiation_partial_context (diagnostic_context *,
96
                                                 struct tinst_level *,
97
                                                 location_t);
98
static void cp_diagnostic_starter (diagnostic_context *, diagnostic_info *);
99
static void cp_diagnostic_finalizer (diagnostic_context *, diagnostic_info *);
100
static void cp_print_error_function (diagnostic_context *, diagnostic_info *);
101
 
102
static bool cp_printer (pretty_printer *, text_info *, const char *,
103
                        int, bool, bool, bool);
104
 
105
void
106
init_error (void)
107
{
108
  diagnostic_starter (global_dc) = cp_diagnostic_starter;
109
  diagnostic_finalizer (global_dc) = cp_diagnostic_finalizer;
110
  diagnostic_format_decoder (global_dc) = cp_printer;
111
 
112
  pp_construct (pp_base (cxx_pp), NULL, 0);
113
  pp_cxx_pretty_printer_init (cxx_pp);
114
}
115
 
116
/* Dump a scope, if deemed necessary.  */
117
 
118
static void
119
dump_scope (tree scope, int flags)
120
{
121
  int f = flags & (TFF_SCOPE | TFF_CHASE_TYPEDEF);
122
 
123
  if (scope == NULL_TREE)
124
    return;
125
 
126
  if (TREE_CODE (scope) == NAMESPACE_DECL)
127
    {
128
      if (scope != global_namespace)
129
        {
130
          dump_decl (scope, f);
131
          pp_cxx_colon_colon (cxx_pp);
132
        }
133
    }
134
  else if (AGGREGATE_TYPE_P (scope))
135
    {
136
      dump_type (scope, f);
137
      pp_cxx_colon_colon (cxx_pp);
138
    }
139
  else if ((flags & TFF_SCOPE) && TREE_CODE (scope) == FUNCTION_DECL)
140
    {
141
      dump_function_decl (scope, f);
142
      pp_cxx_colon_colon (cxx_pp);
143
    }
144
}
145
 
146
/* Dump the template ARGument under control of FLAGS.  */
147
 
148
static void
149
dump_template_argument (tree arg, int flags)
150
{
151
  if (ARGUMENT_PACK_P (arg))
152
    dump_template_argument_list (ARGUMENT_PACK_ARGS (arg),
153
                                 /* No default args in argument packs.  */
154
                                 flags|TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS);
155
  else if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
156
    dump_type (arg, flags & ~TFF_CLASS_KEY_OR_ENUM);
157
  else
158
    {
159
      if (TREE_CODE (arg) == TREE_LIST)
160
        arg = TREE_VALUE (arg);
161
 
162
      dump_expr (arg, (flags | TFF_EXPR_IN_PARENS) & ~TFF_CLASS_KEY_OR_ENUM);
163
    }
164
}
165
 
166
/* Count the number of template arguments ARGS whose value does not
167
   match the (optional) default template parameter in PARAMS  */
168
 
169
static int
170
get_non_default_template_args_count (tree args, int flags)
171
{
172
  int n = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_ARGS (args));
173
 
174
  if (/* We use this flag when generating debug information.  We don't
175
         want to expand templates at this point, for this may generate
176
         new decls, which gets decl counts out of sync, which may in
177
         turn cause codegen differences between compilations with and
178
         without -g.  */
179
      (flags & TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS) != 0
180
      || !flag_pretty_templates)
181
    return n;
182
 
183
  return GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (INNERMOST_TEMPLATE_ARGS (args));
184
}
185
 
186
/* Dump a template-argument-list ARGS (always a TREE_VEC) under control
187
   of FLAGS.  */
188
 
189
static void
190
dump_template_argument_list (tree args, int flags)
191
{
192
  int n = get_non_default_template_args_count (args, flags);
193
  int need_comma = 0;
194
  int i;
195
 
196
  for (i = 0; i < n; ++i)
197
    {
198
      tree arg = TREE_VEC_ELT (args, i);
199
 
200
      /* Only print a comma if we know there is an argument coming. In
201
         the case of an empty template argument pack, no actual
202
         argument will be printed.  */
203
      if (need_comma
204
          && (!ARGUMENT_PACK_P (arg)
205
              || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) > 0))
206
        pp_separate_with_comma (cxx_pp);
207
 
208
      dump_template_argument (arg, flags);
209
      need_comma = 1;
210
    }
211
}
212
 
213
/* Dump a template parameter PARM (a TREE_LIST) under control of FLAGS.  */
214
 
215
static void
216
dump_template_parameter (tree parm, int flags)
217
{
218
  tree p;
219
  tree a;
220
 
221
  if (parm == error_mark_node)
222
   return;
223
 
224
  p = TREE_VALUE (parm);
225
  a = TREE_PURPOSE (parm);
226
 
227
  if (TREE_CODE (p) == TYPE_DECL)
228
    {
229
      if (flags & TFF_DECL_SPECIFIERS)
230
        {
231
          pp_cxx_ws_string (cxx_pp, "class");
232
          if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (p)))
233
            pp_cxx_ws_string (cxx_pp, "...");
234
          if (DECL_NAME (p))
235
            pp_cxx_tree_identifier (cxx_pp, DECL_NAME (p));
236
        }
237
      else if (DECL_NAME (p))
238
        pp_cxx_tree_identifier (cxx_pp, DECL_NAME (p));
239
      else
240
        pp_cxx_canonical_template_parameter (cxx_pp, TREE_TYPE (p));
241
    }
242
  else
243
    dump_decl (p, flags | TFF_DECL_SPECIFIERS);
244
 
245
  if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && a != NULL_TREE)
246
    {
247
      pp_cxx_whitespace (cxx_pp);
248
      pp_equal (cxx_pp);
249
      pp_cxx_whitespace (cxx_pp);
250
      if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
251
        dump_type (a, flags & ~TFF_CHASE_TYPEDEF);
252
      else
253
        dump_expr (a, flags | TFF_EXPR_IN_PARENS);
254
    }
255
}
256
 
257
/* Dump, under control of FLAGS, a template-parameter-list binding.
258
   PARMS is a TREE_LIST of TREE_VEC of TREE_LIST and ARGS is a
259
   TREE_VEC.  */
260
 
261
static void
262
dump_template_bindings (tree parms, tree args, VEC(tree,gc)* typenames)
263
{
264
  bool need_semicolon = false;
265
  int i;
266
  tree t;
267
 
268
  while (parms)
269
    {
270
      tree p = TREE_VALUE (parms);
271
      int lvl = TMPL_PARMS_DEPTH (parms);
272
      int arg_idx = 0;
273
      int i;
274
      tree lvl_args = NULL_TREE;
275
 
276
      /* Don't crash if we had an invalid argument list.  */
277
      if (TMPL_ARGS_DEPTH (args) >= lvl)
278
        lvl_args = TMPL_ARGS_LEVEL (args, lvl);
279
 
280
      for (i = 0; i < TREE_VEC_LENGTH (p); ++i)
281
        {
282
          tree arg = NULL_TREE;
283
 
284
          /* Don't crash if we had an invalid argument list.  */
285
          if (lvl_args && NUM_TMPL_ARGS (lvl_args) > arg_idx)
286
            arg = TREE_VEC_ELT (lvl_args, arg_idx);
287
 
288
          if (need_semicolon)
289
            pp_separate_with_semicolon (cxx_pp);
290
          dump_template_parameter (TREE_VEC_ELT (p, i), TFF_PLAIN_IDENTIFIER);
291
          pp_cxx_whitespace (cxx_pp);
292
          pp_equal (cxx_pp);
293
          pp_cxx_whitespace (cxx_pp);
294
          if (arg)
295
            {
296
              if (ARGUMENT_PACK_P (arg))
297
                pp_cxx_left_brace (cxx_pp);
298
              dump_template_argument (arg, TFF_PLAIN_IDENTIFIER);
299
              if (ARGUMENT_PACK_P (arg))
300
                pp_cxx_right_brace (cxx_pp);
301
            }
302
          else
303
            pp_string (cxx_pp, M_("<missing>"));
304
 
305
          ++arg_idx;
306
          need_semicolon = true;
307
        }
308
 
309
      parms = TREE_CHAIN (parms);
310
    }
311
 
312
  /* Don't bother with typenames for a partial instantiation.  */
313
  if (VEC_empty (tree, typenames) || uses_template_parms (args))
314
    return;
315
 
316
  FOR_EACH_VEC_ELT (tree, typenames, i, t)
317
    {
318
      if (need_semicolon)
319
        pp_separate_with_semicolon (cxx_pp);
320
      dump_type (t, TFF_PLAIN_IDENTIFIER);
321
      pp_cxx_whitespace (cxx_pp);
322
      pp_equal (cxx_pp);
323
      pp_cxx_whitespace (cxx_pp);
324
      push_deferring_access_checks (dk_no_check);
325
      t = tsubst (t, args, tf_none, NULL_TREE);
326
      pop_deferring_access_checks ();
327
      /* Strip typedefs.  We can't just use TFF_CHASE_TYPEDEF because
328
         pp_simple_type_specifier doesn't know about it.  */
329
      t = strip_typedefs (t);
330
      dump_type (t, TFF_PLAIN_IDENTIFIER);
331
    }
332
}
333
 
334
/* Dump a human-readable equivalent of the alias template
335
   specialization of T.  */
336
 
337
static void
338
dump_alias_template_specialization (tree t, int flags)
339
{
340
  tree name;
341
 
342
  gcc_assert (alias_template_specialization_p (t));
343
 
344
  if (!(flags & TFF_UNQUALIFIED_NAME))
345
    dump_scope (CP_DECL_CONTEXT (TYPE_NAME (t)), flags);
346
  name = TYPE_IDENTIFIER (t);
347
  pp_cxx_tree_identifier (cxx_pp, name);
348
  dump_template_parms (TYPE_TEMPLATE_INFO (t),
349
                       /*primary=*/false,
350
                       flags & ~TFF_TEMPLATE_HEADER);
351
}
352
 
353
/* Dump a human-readable equivalent of TYPE.  FLAGS controls the
354
   format.  */
355
 
356
static void
357
dump_type (tree t, int flags)
358
{
359
  if (t == NULL_TREE)
360
    return;
361
 
362
  /* Don't print e.g. "struct mytypedef".  */
363
  if (TYPE_P (t) && typedef_variant_p (t))
364
    {
365
      tree decl = TYPE_NAME (t);
366
      if ((flags & TFF_CHASE_TYPEDEF)
367
               || DECL_SELF_REFERENCE_P (decl)
368
               || (!flag_pretty_templates
369
                   && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)))
370
        t = strip_typedefs (t);
371
      else if (alias_template_specialization_p (t))
372
        {
373
          dump_alias_template_specialization (t, flags);
374
          return;
375
        }
376
      else if (same_type_p (t, TREE_TYPE (decl)))
377
        t = decl;
378
      else
379
        {
380
          pp_cxx_cv_qualifier_seq (cxx_pp, t);
381
          pp_cxx_tree_identifier (cxx_pp, TYPE_IDENTIFIER (t));
382
          return;
383
        }
384
    }
385
 
386
  if (TYPE_PTRMEMFUNC_P (t))
387
    goto offset_type;
388
 
389
  switch (TREE_CODE (t))
390
    {
391
    case LANG_TYPE:
392
      if (t == init_list_type_node)
393
        pp_string (cxx_pp, M_("<brace-enclosed initializer list>"));
394
      else if (t == unknown_type_node)
395
        pp_string (cxx_pp, M_("<unresolved overloaded function type>"));
396
      else
397
        {
398
          pp_cxx_cv_qualifier_seq (cxx_pp, t);
399
          pp_cxx_tree_identifier (cxx_pp, TYPE_IDENTIFIER (t));
400
        }
401
      break;
402
 
403
    case TREE_LIST:
404
      /* A list of function parms.  */
405
      dump_parameters (t, flags);
406
      break;
407
 
408
    case IDENTIFIER_NODE:
409
      pp_cxx_tree_identifier (cxx_pp, t);
410
      break;
411
 
412
    case TREE_BINFO:
413
      dump_type (BINFO_TYPE (t), flags);
414
      break;
415
 
416
    case RECORD_TYPE:
417
    case UNION_TYPE:
418
    case ENUMERAL_TYPE:
419
      dump_aggr_type (t, flags);
420
      break;
421
 
422
    case TYPE_DECL:
423
      if (flags & TFF_CHASE_TYPEDEF)
424
        {
425
          dump_type (DECL_ORIGINAL_TYPE (t)
426
                     ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t), flags);
427
          break;
428
        }
429
      /* Else fall through.  */
430
 
431
    case TEMPLATE_DECL:
432
    case NAMESPACE_DECL:
433
      dump_decl (t, flags & ~TFF_DECL_SPECIFIERS);
434
      break;
435
 
436
    case INTEGER_TYPE:
437
    case REAL_TYPE:
438
    case VOID_TYPE:
439
    case BOOLEAN_TYPE:
440
    case COMPLEX_TYPE:
441
    case VECTOR_TYPE:
442
    case FIXED_POINT_TYPE:
443
      pp_type_specifier_seq (cxx_pp, t);
444
      break;
445
 
446
    case TEMPLATE_TEMPLATE_PARM:
447
      /* For parameters inside template signature.  */
448
      if (TYPE_IDENTIFIER (t))
449
        pp_cxx_tree_identifier (cxx_pp, TYPE_IDENTIFIER (t));
450
      else
451
        pp_cxx_canonical_template_parameter (cxx_pp, t);
452
      break;
453
 
454
    case BOUND_TEMPLATE_TEMPLATE_PARM:
455
      {
456
        tree args = TYPE_TI_ARGS (t);
457
        pp_cxx_cv_qualifier_seq (cxx_pp, t);
458
        pp_cxx_tree_identifier (cxx_pp, TYPE_IDENTIFIER (t));
459
        pp_cxx_begin_template_argument_list (cxx_pp);
460
        dump_template_argument_list (args, flags);
461
        pp_cxx_end_template_argument_list (cxx_pp);
462
      }
463
      break;
464
 
465
    case TEMPLATE_TYPE_PARM:
466
      pp_cxx_cv_qualifier_seq (cxx_pp, t);
467
      if (TYPE_IDENTIFIER (t))
468
        pp_cxx_tree_identifier (cxx_pp, TYPE_IDENTIFIER (t));
469
      else
470
        pp_cxx_canonical_template_parameter
471
          (cxx_pp, TEMPLATE_TYPE_PARM_INDEX (t));
472
      break;
473
 
474
      /* This is not always necessary for pointers and such, but doing this
475
         reduces code size.  */
476
    case ARRAY_TYPE:
477
    case POINTER_TYPE:
478
    case REFERENCE_TYPE:
479
    case OFFSET_TYPE:
480
    offset_type:
481
    case FUNCTION_TYPE:
482
    case METHOD_TYPE:
483
    {
484
      dump_type_prefix (t, flags);
485
      dump_type_suffix (t, flags);
486
      break;
487
    }
488
    case TYPENAME_TYPE:
489
      if (! (flags & TFF_CHASE_TYPEDEF)
490
          && DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
491
        {
492
          dump_decl (TYPE_NAME (t), TFF_PLAIN_IDENTIFIER);
493
          break;
494
        }
495
      pp_cxx_cv_qualifier_seq (cxx_pp, t);
496
      pp_cxx_ws_string (cxx_pp,
497
                         TYPENAME_IS_ENUM_P (t) ? "enum"
498
                         : TYPENAME_IS_CLASS_P (t) ? "class"
499
                         : "typename");
500
      dump_typename (t, flags);
501
      break;
502
 
503
    case UNBOUND_CLASS_TEMPLATE:
504
      if (! (flags & TFF_UNQUALIFIED_NAME))
505
        {
506
          dump_type (TYPE_CONTEXT (t), flags);
507
          pp_cxx_colon_colon (cxx_pp);
508
        }
509
      pp_cxx_ws_string (cxx_pp, "template");
510
      dump_type (DECL_NAME (TYPE_NAME (t)), flags);
511
      break;
512
 
513
    case TYPEOF_TYPE:
514
      pp_cxx_ws_string (cxx_pp, "__typeof__");
515
      pp_cxx_whitespace (cxx_pp);
516
      pp_cxx_left_paren (cxx_pp);
517
      dump_expr (TYPEOF_TYPE_EXPR (t), flags & ~TFF_EXPR_IN_PARENS);
518
      pp_cxx_right_paren (cxx_pp);
519
      break;
520
 
521
    case UNDERLYING_TYPE:
522
      pp_cxx_ws_string (cxx_pp, "__underlying_type");
523
      pp_cxx_whitespace (cxx_pp);
524
      pp_cxx_left_paren (cxx_pp);
525
      dump_expr (UNDERLYING_TYPE_TYPE (t), flags & ~TFF_EXPR_IN_PARENS);
526
      pp_cxx_right_paren (cxx_pp);
527
      break;
528
 
529
    case TYPE_PACK_EXPANSION:
530
      dump_type (PACK_EXPANSION_PATTERN (t), flags);
531
      pp_cxx_ws_string (cxx_pp, "...");
532
      break;
533
 
534
    case TYPE_ARGUMENT_PACK:
535
      dump_template_argument (t, flags);
536
      break;
537
 
538
    case DECLTYPE_TYPE:
539
      pp_cxx_ws_string (cxx_pp, "decltype");
540
      pp_cxx_whitespace (cxx_pp);
541
      pp_cxx_left_paren (cxx_pp);
542
      dump_expr (DECLTYPE_TYPE_EXPR (t), flags & ~TFF_EXPR_IN_PARENS);
543
      pp_cxx_right_paren (cxx_pp);
544
      break;
545
 
546
    case NULLPTR_TYPE:
547
      pp_string (cxx_pp, "std::nullptr_t");
548
      break;
549
 
550
    default:
551
      pp_unsupported_tree (cxx_pp, t);
552
      /* Fall through to error.  */
553
 
554
    case ERROR_MARK:
555
      pp_string (cxx_pp, M_("<type error>"));
556
      break;
557
    }
558
}
559
 
560
/* Dump a TYPENAME_TYPE. We need to notice when the context is itself
561
   a TYPENAME_TYPE.  */
562
 
563
static void
564
dump_typename (tree t, int flags)
565
{
566
  tree ctx = TYPE_CONTEXT (t);
567
 
568
  if (TREE_CODE (ctx) == TYPENAME_TYPE)
569
    dump_typename (ctx, flags);
570
  else
571
    dump_type (ctx, flags & ~TFF_CLASS_KEY_OR_ENUM);
572
  pp_cxx_colon_colon (cxx_pp);
573
  dump_decl (TYPENAME_TYPE_FULLNAME (t), flags);
574
}
575
 
576
/* Return the name of the supplied aggregate, or enumeral type.  */
577
 
578
const char *
579
class_key_or_enum_as_string (tree t)
580
{
581
  if (TREE_CODE (t) == ENUMERAL_TYPE)
582
    {
583
      if (SCOPED_ENUM_P (t))
584
        return "enum class";
585
      else
586
        return "enum";
587
    }
588
  else if (TREE_CODE (t) == UNION_TYPE)
589
    return "union";
590
  else if (TYPE_LANG_SPECIFIC (t) && CLASSTYPE_DECLARED_CLASS (t))
591
    return "class";
592
  else
593
    return "struct";
594
}
595
 
596
/* Print out a class declaration T under the control of FLAGS,
597
   in the form `class foo'.  */
598
 
599
static void
600
dump_aggr_type (tree t, int flags)
601
{
602
  tree name;
603
  const char *variety = class_key_or_enum_as_string (t);
604
  int typdef = 0;
605
  int tmplate = 0;
606
 
607
  pp_cxx_cv_qualifier_seq (cxx_pp, t);
608
 
609
  if (flags & TFF_CLASS_KEY_OR_ENUM)
610
    pp_cxx_ws_string (cxx_pp, variety);
611
 
612
  name = TYPE_NAME (t);
613
 
614
  if (name)
615
    {
616
      typdef = (!DECL_ARTIFICIAL (name)
617
                /* An alias specialization is not considered to be a
618
                   typedef.  */
619
                && !alias_template_specialization_p (t));
620
 
621
      if ((typdef
622
           && ((flags & TFF_CHASE_TYPEDEF)
623
               || (!flag_pretty_templates && DECL_LANG_SPECIFIC (name)
624
                   && DECL_TEMPLATE_INFO (name))))
625
          || DECL_SELF_REFERENCE_P (name))
626
        {
627
          t = TYPE_MAIN_VARIANT (t);
628
          name = TYPE_NAME (t);
629
          typdef = 0;
630
        }
631
 
632
      tmplate = !typdef && TREE_CODE (t) != ENUMERAL_TYPE
633
                && TYPE_LANG_SPECIFIC (t) && CLASSTYPE_TEMPLATE_INFO (t)
634
                && (TREE_CODE (CLASSTYPE_TI_TEMPLATE (t)) != TEMPLATE_DECL
635
                    || PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
636
 
637
      if (! (flags & TFF_UNQUALIFIED_NAME))
638
        dump_scope (CP_DECL_CONTEXT (name), flags | TFF_SCOPE);
639
      flags &= ~TFF_UNQUALIFIED_NAME;
640
      if (tmplate)
641
        {
642
          /* Because the template names are mangled, we have to locate
643
             the most general template, and use that name.  */
644
          tree tpl = TYPE_TI_TEMPLATE (t);
645
 
646
          while (DECL_TEMPLATE_INFO (tpl))
647
            tpl = DECL_TI_TEMPLATE (tpl);
648
          name = tpl;
649
        }
650
      name = DECL_NAME (name);
651
    }
652
 
653
  if (name == 0 || ANON_AGGRNAME_P (name))
654
    {
655
      if (flags & TFF_CLASS_KEY_OR_ENUM)
656
        pp_string (cxx_pp, M_("<anonymous>"));
657
      else
658
        pp_printf (pp_base (cxx_pp), M_("<anonymous %s>"), variety);
659
    }
660
  else if (LAMBDANAME_P (name))
661
    {
662
      /* A lambda's "type" is essentially its signature.  */
663
      pp_string (cxx_pp, M_("<lambda"));
664
      if (lambda_function (t))
665
        dump_parameters (FUNCTION_FIRST_USER_PARMTYPE (lambda_function (t)),
666
                         flags);
667
      pp_character(cxx_pp, '>');
668
    }
669
  else
670
    pp_cxx_tree_identifier (cxx_pp, name);
671
  if (tmplate)
672
    dump_template_parms (TYPE_TEMPLATE_INFO (t),
673
                         !CLASSTYPE_USE_TEMPLATE (t),
674
                         flags & ~TFF_TEMPLATE_HEADER);
675
}
676
 
677
/* Dump into the obstack the initial part of the output for a given type.
678
   This is necessary when dealing with things like functions returning
679
   functions.  Examples:
680
 
681
   return type of `int (* fee ())()': pointer -> function -> int.  Both
682
   pointer (and reference and offset) and function (and member) types must
683
   deal with prefix and suffix.
684
 
685
   Arrays must also do this for DECL nodes, like int a[], and for things like
686
   int *[]&.  */
687
 
688
static void
689
dump_type_prefix (tree t, int flags)
690
{
691
  if (TYPE_PTRMEMFUNC_P (t))
692
    {
693
      t = TYPE_PTRMEMFUNC_FN_TYPE (t);
694
      goto offset_type;
695
    }
696
 
697
  switch (TREE_CODE (t))
698
    {
699
    case POINTER_TYPE:
700
    case REFERENCE_TYPE:
701
      {
702
        tree sub = TREE_TYPE (t);
703
 
704
        dump_type_prefix (sub, flags);
705
        if (TREE_CODE (sub) == ARRAY_TYPE
706
            || TREE_CODE (sub) == FUNCTION_TYPE)
707
          {
708
            pp_cxx_whitespace (cxx_pp);
709
            pp_cxx_left_paren (cxx_pp);
710
            pp_c_attributes_display (pp_c_base (cxx_pp),
711
                                     TYPE_ATTRIBUTES (sub));
712
          }
713
        if (TREE_CODE (t) == POINTER_TYPE)
714
          pp_character(cxx_pp, '*');
715
        else if (TREE_CODE (t) == REFERENCE_TYPE)
716
        {
717
          if (TYPE_REF_IS_RVALUE (t))
718
            pp_string (cxx_pp, "&&");
719
          else
720
            pp_character (cxx_pp, '&');
721
        }
722
        pp_base (cxx_pp)->padding = pp_before;
723
        pp_cxx_cv_qualifier_seq (cxx_pp, t);
724
      }
725
      break;
726
 
727
    case OFFSET_TYPE:
728
    offset_type:
729
      dump_type_prefix (TREE_TYPE (t), flags);
730
      if (TREE_CODE (t) == OFFSET_TYPE) /* pmfs deal with this in d_t_p */
731
        {
732
          pp_maybe_space (cxx_pp);
733
          if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
734
             pp_cxx_left_paren (cxx_pp);
735
          dump_type (TYPE_OFFSET_BASETYPE (t), flags);
736
          pp_cxx_colon_colon (cxx_pp);
737
        }
738
      pp_cxx_star (cxx_pp);
739
      pp_cxx_cv_qualifier_seq (cxx_pp, t);
740
      pp_base (cxx_pp)->padding = pp_before;
741
      break;
742
 
743
      /* This can be reached without a pointer when dealing with
744
         templates, e.g. std::is_function.  */
745
    case FUNCTION_TYPE:
746
      dump_type_prefix (TREE_TYPE (t), flags);
747
      break;
748
 
749
    case METHOD_TYPE:
750
      dump_type_prefix (TREE_TYPE (t), flags);
751
      pp_maybe_space (cxx_pp);
752
      pp_cxx_left_paren (cxx_pp);
753
      dump_aggr_type (TYPE_METHOD_BASETYPE (t), flags);
754
      pp_cxx_colon_colon (cxx_pp);
755
      break;
756
 
757
    case ARRAY_TYPE:
758
      dump_type_prefix (TREE_TYPE (t), flags);
759
      break;
760
 
761
    case ENUMERAL_TYPE:
762
    case IDENTIFIER_NODE:
763
    case INTEGER_TYPE:
764
    case BOOLEAN_TYPE:
765
    case REAL_TYPE:
766
    case RECORD_TYPE:
767
    case TEMPLATE_TYPE_PARM:
768
    case TEMPLATE_TEMPLATE_PARM:
769
    case BOUND_TEMPLATE_TEMPLATE_PARM:
770
    case TREE_LIST:
771
    case TYPE_DECL:
772
    case TREE_VEC:
773
    case UNION_TYPE:
774
    case LANG_TYPE:
775
    case VOID_TYPE:
776
    case TYPENAME_TYPE:
777
    case COMPLEX_TYPE:
778
    case VECTOR_TYPE:
779
    case TYPEOF_TYPE:
780
    case UNDERLYING_TYPE:
781
    case DECLTYPE_TYPE:
782
    case TYPE_PACK_EXPANSION:
783
    case FIXED_POINT_TYPE:
784
    case NULLPTR_TYPE:
785
      dump_type (t, flags);
786
      pp_base (cxx_pp)->padding = pp_before;
787
      break;
788
 
789
    default:
790
      pp_unsupported_tree (cxx_pp, t);
791
      /* fall through.  */
792
    case ERROR_MARK:
793
      pp_string (cxx_pp, M_("<typeprefixerror>"));
794
      break;
795
    }
796
}
797
 
798
/* Dump the suffix of type T, under control of FLAGS.  This is the part
799
   which appears after the identifier (or function parms).  */
800
 
801
static void
802
dump_type_suffix (tree t, int flags)
803
{
804
  if (TYPE_PTRMEMFUNC_P (t))
805
    t = TYPE_PTRMEMFUNC_FN_TYPE (t);
806
 
807
  switch (TREE_CODE (t))
808
    {
809
    case POINTER_TYPE:
810
    case REFERENCE_TYPE:
811
    case OFFSET_TYPE:
812
      if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
813
          || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
814
        pp_cxx_right_paren (cxx_pp);
815
      dump_type_suffix (TREE_TYPE (t), flags);
816
      break;
817
 
818
    case FUNCTION_TYPE:
819
    case METHOD_TYPE:
820
      {
821
        tree arg;
822
        if (TREE_CODE (t) == METHOD_TYPE)
823
          /* Can only be reached through a pointer.  */
824
          pp_cxx_right_paren (cxx_pp);
825
        arg = TYPE_ARG_TYPES (t);
826
        if (TREE_CODE (t) == METHOD_TYPE)
827
          arg = TREE_CHAIN (arg);
828
 
829
        /* Function pointers don't have default args.  Not in standard C++,
830
           anyway; they may in g++, but we'll just pretend otherwise.  */
831
        dump_parameters (arg, flags & ~TFF_FUNCTION_DEFAULT_ARGUMENTS);
832
 
833
        if (TREE_CODE (t) == METHOD_TYPE)
834
          pp_cxx_cv_qualifier_seq (cxx_pp, class_of_this_parm (t));
835
        else
836
          pp_cxx_cv_qualifier_seq (cxx_pp, t);
837
        dump_exception_spec (TYPE_RAISES_EXCEPTIONS (t), flags);
838
        dump_type_suffix (TREE_TYPE (t), flags);
839
        break;
840
      }
841
 
842
    case ARRAY_TYPE:
843
      pp_maybe_space (cxx_pp);
844
      pp_cxx_left_bracket (cxx_pp);
845
      if (TYPE_DOMAIN (t))
846
        {
847
          tree dtype = TYPE_DOMAIN (t);
848
          tree max = TYPE_MAX_VALUE (dtype);
849
          if (host_integerp (max, 0))
850
            pp_wide_integer (cxx_pp, tree_low_cst (max, 0) + 1);
851
          else if (TREE_CODE (max) == MINUS_EXPR)
852
            dump_expr (TREE_OPERAND (max, 0),
853
                       flags & ~TFF_EXPR_IN_PARENS);
854
          else
855
            dump_expr (fold_build2_loc (input_location,
856
                                    PLUS_EXPR, dtype, max,
857
                                    build_int_cst (dtype, 1)),
858
                       flags & ~TFF_EXPR_IN_PARENS);
859
        }
860
      pp_cxx_right_bracket (cxx_pp);
861
      dump_type_suffix (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
      break;
889
 
890
    default:
891
      pp_unsupported_tree (cxx_pp, t);
892
    case ERROR_MARK:
893
      /* Don't mark it here, we should have already done in
894
         dump_type_prefix.  */
895
      break;
896
    }
897
}
898
 
899
static void
900
dump_global_iord (tree t)
901
{
902
  const char *p = NULL;
903
 
904
  if (DECL_GLOBAL_CTOR_P (t))
905
    p = M_("(static initializers for %s)");
906
  else if (DECL_GLOBAL_DTOR_P (t))
907
    p = M_("(static destructors for %s)");
908
  else
909
    gcc_unreachable ();
910
 
911
  pp_printf (pp_base (cxx_pp), p, input_filename);
912
}
913
 
914
static void
915
dump_simple_decl (tree t, tree type, int flags)
916
{
917
  if (flags & TFF_DECL_SPECIFIERS)
918
    {
919
      if (TREE_CODE (t) == VAR_DECL
920
          && DECL_DECLARED_CONSTEXPR_P (t))
921
        pp_cxx_ws_string (cxx_pp, "constexpr");
922
      dump_type_prefix (type, flags & ~TFF_UNQUALIFIED_NAME);
923
      pp_maybe_space (cxx_pp);
924
    }
925
  if (! (flags & TFF_UNQUALIFIED_NAME)
926
      && TREE_CODE (t) != PARM_DECL
927
      && (!DECL_INITIAL (t)
928
          || TREE_CODE (DECL_INITIAL (t)) != TEMPLATE_PARM_INDEX))
929
    dump_scope (CP_DECL_CONTEXT (t), flags);
930
  flags &= ~TFF_UNQUALIFIED_NAME;
931
  if ((flags & TFF_DECL_SPECIFIERS)
932
      && DECL_TEMPLATE_PARM_P (t)
933
      && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (t)))
934
    pp_string (cxx_pp, "...");
935
  if (DECL_NAME (t))
936
    dump_decl (DECL_NAME (t), flags);
937
  else
938
    pp_string (cxx_pp, M_("<anonymous>"));
939
  if (flags & TFF_DECL_SPECIFIERS)
940
    dump_type_suffix (type, flags);
941
}
942
 
943
/* Dump a human readable string for the decl T under control of FLAGS.  */
944
 
945
static void
946
dump_decl (tree t, int flags)
947
{
948
  if (t == NULL_TREE)
949
    return;
950
 
951
  /* If doing Objective-C++, give Objective-C a chance to demangle
952
     Objective-C method names.  */
953
  if (c_dialect_objc ())
954
    {
955
      const char *demangled = objc_maybe_printable_name (t, flags);
956
      if (demangled)
957
        {
958
          pp_string (cxx_pp, demangled);
959
          return;
960
        }
961
    }
962
 
963
  switch (TREE_CODE (t))
964
    {
965
    case TYPE_DECL:
966
      /* Don't say 'typedef class A' */
967
      if (DECL_ARTIFICIAL (t) && !DECL_SELF_REFERENCE_P (t))
968
        {
969
          if ((flags & TFF_DECL_SPECIFIERS)
970
              && TREE_CODE (TREE_TYPE (t)) == TEMPLATE_TYPE_PARM)
971
            {
972
              /* Say `class T' not just `T'.  */
973
              pp_cxx_ws_string (cxx_pp, "class");
974
 
975
              /* Emit the `...' for a parameter pack.  */
976
              if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
977
                pp_cxx_ws_string (cxx_pp, "...");
978
            }
979
 
980
          dump_type (TREE_TYPE (t), flags);
981
          break;
982
        }
983
      if (TYPE_DECL_ALIAS_P (t)
984
          && (flags & TFF_DECL_SPECIFIERS
985
              || flags & TFF_CLASS_KEY_OR_ENUM))
986
        {
987
          pp_cxx_ws_string (cxx_pp, "using");
988
          dump_decl (DECL_NAME (t), flags);
989
          pp_cxx_whitespace (cxx_pp);
990
          pp_cxx_ws_string (cxx_pp, "=");
991
          pp_cxx_whitespace (cxx_pp);
992
          dump_type (DECL_ORIGINAL_TYPE (t), flags);
993
          break;
994
        }
995
      if ((flags & TFF_DECL_SPECIFIERS)
996
          && !DECL_SELF_REFERENCE_P (t))
997
        pp_cxx_ws_string (cxx_pp, "typedef");
998
      dump_simple_decl (t, DECL_ORIGINAL_TYPE (t)
999
                        ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t),
1000
                        flags);
1001
      break;
1002
 
1003
    case VAR_DECL:
1004
      if (DECL_NAME (t) && VTABLE_NAME_P (DECL_NAME (t)))
1005
        {
1006
          pp_string (cxx_pp, M_("vtable for "));
1007
          gcc_assert (TYPE_P (DECL_CONTEXT (t)));
1008
          dump_type (DECL_CONTEXT (t), flags);
1009
          break;
1010
        }
1011
      /* Else fall through.  */
1012
    case FIELD_DECL:
1013
    case PARM_DECL:
1014
      dump_simple_decl (t, TREE_TYPE (t), flags);
1015
      break;
1016
 
1017
    case RESULT_DECL:
1018
      pp_string (cxx_pp, M_("<return value> "));
1019
      dump_simple_decl (t, TREE_TYPE (t), flags);
1020
      break;
1021
 
1022
    case NAMESPACE_DECL:
1023
      if (flags & TFF_DECL_SPECIFIERS)
1024
        pp_cxx_declaration (cxx_pp, t);
1025
      else
1026
        {
1027
          if (! (flags & TFF_UNQUALIFIED_NAME))
1028
            dump_scope (CP_DECL_CONTEXT (t), flags);
1029
          flags &= ~TFF_UNQUALIFIED_NAME;
1030
          if (DECL_NAME (t) == NULL_TREE)
1031
            pp_cxx_ws_string (cxx_pp, M_("{anonymous}"));
1032
          else
1033
            pp_cxx_tree_identifier (cxx_pp, DECL_NAME (t));
1034
        }
1035
      break;
1036
 
1037
    case SCOPE_REF:
1038
      dump_type (TREE_OPERAND (t, 0), flags);
1039
      pp_string (cxx_pp, "::");
1040
      dump_decl (TREE_OPERAND (t, 1), flags|TFF_UNQUALIFIED_NAME);
1041
      break;
1042
 
1043
    case ARRAY_REF:
1044
      dump_decl (TREE_OPERAND (t, 0), flags);
1045
      pp_cxx_left_bracket (cxx_pp);
1046
      dump_decl (TREE_OPERAND (t, 1), flags);
1047
      pp_cxx_right_bracket (cxx_pp);
1048
      break;
1049
 
1050
      /* So that we can do dump_decl on an aggr type.  */
1051
    case RECORD_TYPE:
1052
    case UNION_TYPE:
1053
    case ENUMERAL_TYPE:
1054
      dump_type (t, flags);
1055
      break;
1056
 
1057
    case BIT_NOT_EXPR:
1058
      /* This is a pseudo destructor call which has not been folded into
1059
         a PSEUDO_DTOR_EXPR yet.  */
1060
      pp_cxx_complement (cxx_pp);
1061
      dump_type (TREE_OPERAND (t, 0), flags);
1062
      break;
1063
 
1064
    case TYPE_EXPR:
1065
      gcc_unreachable ();
1066
      break;
1067
 
1068
      /* These special cases are duplicated here so that other functions
1069
         can feed identifiers to error and get them demangled properly.  */
1070
    case IDENTIFIER_NODE:
1071
      if (IDENTIFIER_TYPENAME_P (t))
1072
        {
1073
          pp_cxx_ws_string (cxx_pp, "operator");
1074
          /* Not exactly IDENTIFIER_TYPE_VALUE.  */
1075
          dump_type (TREE_TYPE (t), flags);
1076
          break;
1077
        }
1078
      else
1079
        pp_cxx_tree_identifier (cxx_pp, t);
1080
      break;
1081
 
1082
    case OVERLOAD:
1083
      if (OVL_CHAIN (t))
1084
        {
1085
          t = OVL_CURRENT (t);
1086
          if (DECL_CLASS_SCOPE_P (t))
1087
            {
1088
              dump_type (DECL_CONTEXT (t), flags);
1089
              pp_cxx_colon_colon (cxx_pp);
1090
            }
1091
          else if (!DECL_FILE_SCOPE_P (t))
1092
            {
1093
              dump_decl (DECL_CONTEXT (t), flags);
1094
              pp_cxx_colon_colon (cxx_pp);
1095
            }
1096
          dump_decl (DECL_NAME (t), flags);
1097
          break;
1098
        }
1099
 
1100
      /* If there's only one function, just treat it like an ordinary
1101
         FUNCTION_DECL.  */
1102
      t = OVL_CURRENT (t);
1103
      /* Fall through.  */
1104
 
1105
    case FUNCTION_DECL:
1106
      if (! DECL_LANG_SPECIFIC (t))
1107
        pp_string (cxx_pp, M_("<built-in>"));
1108
      else if (DECL_GLOBAL_CTOR_P (t) || DECL_GLOBAL_DTOR_P (t))
1109
        dump_global_iord (t);
1110
      else
1111
        dump_function_decl (t, flags);
1112
      break;
1113
 
1114
    case TEMPLATE_DECL:
1115
      dump_template_decl (t, flags);
1116
      break;
1117
 
1118
    case TEMPLATE_ID_EXPR:
1119
      {
1120
        tree name = TREE_OPERAND (t, 0);
1121
        tree args = TREE_OPERAND (t, 1);
1122
 
1123
        if (is_overloaded_fn (name))
1124
          name = DECL_NAME (get_first_fn (name));
1125
        dump_decl (name, flags);
1126
        pp_cxx_begin_template_argument_list (cxx_pp);
1127
        if (args == error_mark_node)
1128
          pp_string (cxx_pp, M_("<template arguments error>"));
1129
        else if (args)
1130
          dump_template_argument_list (args, flags);
1131
        pp_cxx_end_template_argument_list (cxx_pp);
1132
      }
1133
      break;
1134
 
1135
    case LABEL_DECL:
1136
      pp_cxx_tree_identifier (cxx_pp, DECL_NAME (t));
1137
      break;
1138
 
1139
    case CONST_DECL:
1140
      if ((TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == ENUMERAL_TYPE)
1141
          || (DECL_INITIAL (t) &&
1142
              TREE_CODE (DECL_INITIAL (t)) == TEMPLATE_PARM_INDEX))
1143
        dump_simple_decl (t, TREE_TYPE (t), flags);
1144
      else if (DECL_NAME (t))
1145
        dump_decl (DECL_NAME (t), flags);
1146
      else if (DECL_INITIAL (t))
1147
        dump_expr (DECL_INITIAL (t), flags | TFF_EXPR_IN_PARENS);
1148
      else
1149
        pp_string (cxx_pp, M_("<enumerator>"));
1150
      break;
1151
 
1152
    case USING_DECL:
1153
      pp_cxx_ws_string (cxx_pp, "using");
1154
      dump_type (USING_DECL_SCOPE (t), flags);
1155
      pp_cxx_colon_colon (cxx_pp);
1156
      dump_decl (DECL_NAME (t), flags);
1157
      break;
1158
 
1159
    case STATIC_ASSERT:
1160
      pp_cxx_declaration (cxx_pp, t);
1161
      break;
1162
 
1163
    case BASELINK:
1164
      dump_decl (BASELINK_FUNCTIONS (t), flags);
1165
      break;
1166
 
1167
    case NON_DEPENDENT_EXPR:
1168
      dump_expr (t, flags);
1169
      break;
1170
 
1171
    case TEMPLATE_TYPE_PARM:
1172
      if (flags & TFF_DECL_SPECIFIERS)
1173
        pp_cxx_declaration (cxx_pp, t);
1174
      else
1175
        pp_type_id (cxx_pp, t);
1176
      break;
1177
 
1178
    case UNBOUND_CLASS_TEMPLATE:
1179
    case TYPE_PACK_EXPANSION:
1180
    case TREE_BINFO:
1181
      dump_type (t, flags);
1182
      break;
1183
 
1184
    default:
1185
      pp_unsupported_tree (cxx_pp, t);
1186
      /* Fall through to error.  */
1187
 
1188
    case ERROR_MARK:
1189
      pp_string (cxx_pp, M_("<declaration error>"));
1190
      break;
1191
    }
1192
}
1193
 
1194
/* Dump a template declaration T under control of FLAGS. This means the
1195
   'template <...> leaders plus the 'class X' or 'void fn(...)' part.  */
1196
 
1197
static void
1198
dump_template_decl (tree t, int flags)
1199
{
1200
  tree orig_parms = DECL_TEMPLATE_PARMS (t);
1201
  tree parms;
1202
  int i;
1203
 
1204
  if (flags & TFF_TEMPLATE_HEADER)
1205
    {
1206
      for (parms = orig_parms = nreverse (orig_parms);
1207
           parms;
1208
           parms = TREE_CHAIN (parms))
1209
        {
1210
          tree inner_parms = INNERMOST_TEMPLATE_PARMS (parms);
1211
          int len = TREE_VEC_LENGTH (inner_parms);
1212
 
1213
          pp_cxx_ws_string (cxx_pp, "template");
1214
          pp_cxx_begin_template_argument_list (cxx_pp);
1215
 
1216
          /* If we've shown the template prefix, we'd better show the
1217
             parameters' and decl's type too.  */
1218
            flags |= TFF_DECL_SPECIFIERS;
1219
 
1220
          for (i = 0; i < len; i++)
1221
            {
1222
              if (i)
1223
                pp_separate_with_comma (cxx_pp);
1224
              dump_template_parameter (TREE_VEC_ELT (inner_parms, i), flags);
1225
            }
1226
          pp_cxx_end_template_argument_list (cxx_pp);
1227
          pp_cxx_whitespace (cxx_pp);
1228
        }
1229
      nreverse(orig_parms);
1230
 
1231
      if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
1232
        {
1233
          /* Say `template<arg> class TT' not just `template<arg> TT'.  */
1234
          pp_cxx_ws_string (cxx_pp, "class");
1235
 
1236
          /* If this is a parameter pack, print the ellipsis.  */
1237
          if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
1238
            pp_cxx_ws_string (cxx_pp, "...");
1239
        }
1240
    }
1241
 
1242
  if (DECL_CLASS_TEMPLATE_P (t))
1243
    dump_type (TREE_TYPE (t),
1244
               ((flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1245
                | (flags & TFF_DECL_SPECIFIERS ? TFF_CLASS_KEY_OR_ENUM : 0)));
1246
  else if (DECL_TEMPLATE_RESULT (t)
1247
           && (TREE_CODE (DECL_TEMPLATE_RESULT (t)) == VAR_DECL
1248
               /* Alias template.  */
1249
               || DECL_TYPE_TEMPLATE_P (t)))
1250
    dump_decl (DECL_TEMPLATE_RESULT (t), flags | TFF_TEMPLATE_NAME);
1251
  else
1252
    {
1253
      gcc_assert (TREE_TYPE (t));
1254
      switch (NEXT_CODE (t))
1255
        {
1256
        case METHOD_TYPE:
1257
        case FUNCTION_TYPE:
1258
          dump_function_decl (t, flags | TFF_TEMPLATE_NAME);
1259
          break;
1260
        default:
1261
          /* This case can occur with some invalid code.  */
1262
          dump_type (TREE_TYPE (t),
1263
                     (flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1264
                     | (flags & TFF_DECL_SPECIFIERS
1265
                        ? TFF_CLASS_KEY_OR_ENUM : 0));
1266
        }
1267
    }
1268
}
1269
 
1270
/* find_typenames looks through the type of the function template T
1271
   and returns a VEC containing any typedefs, decltypes or TYPENAME_TYPEs
1272
   it finds.  */
1273
 
1274
struct find_typenames_t
1275
{
1276
  struct pointer_set_t *p_set;
1277
  VEC (tree,gc) *typenames;
1278
};
1279
 
1280
static tree
1281
find_typenames_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED, void *data)
1282
{
1283
  struct find_typenames_t *d = (struct find_typenames_t *)data;
1284
  tree mv = NULL_TREE;
1285
 
1286
  if (TYPE_P (*tp) && is_typedef_decl (TYPE_NAME (*tp)))
1287
    /* Add the type of the typedef without any additional cv-quals.  */
1288
    mv = TREE_TYPE (TYPE_NAME (*tp));
1289
  else if (TREE_CODE (*tp) == TYPENAME_TYPE
1290
           || TREE_CODE (*tp) == DECLTYPE_TYPE)
1291
    /* Add the typename without any cv-qualifiers.  */
1292
    mv = TYPE_MAIN_VARIANT (*tp);
1293
 
1294
  if (mv && (mv == *tp || !pointer_set_insert (d->p_set, mv)))
1295
    VEC_safe_push (tree, gc, d->typenames, mv);
1296
 
1297
  /* Search into class template arguments, which cp_walk_subtrees
1298
     doesn't do.  */
1299
  if (CLASS_TYPE_P (*tp) && CLASSTYPE_TEMPLATE_INFO (*tp))
1300
    cp_walk_tree (&CLASSTYPE_TI_ARGS (*tp), find_typenames_r,
1301
                  data, d->p_set);
1302
 
1303
  return NULL_TREE;
1304
}
1305
 
1306
static VEC(tree,gc) *
1307
find_typenames (tree t)
1308
{
1309
  struct find_typenames_t ft;
1310
  ft.p_set = pointer_set_create ();
1311
  ft.typenames = NULL;
1312
  cp_walk_tree (&TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
1313
                find_typenames_r, &ft, ft.p_set);
1314
  pointer_set_destroy (ft.p_set);
1315
  return ft.typenames;
1316
}
1317
 
1318
/* Pretty print a function decl. There are several ways we want to print a
1319
   function declaration. The TFF_ bits in FLAGS tells us how to behave.
1320
   As error can only apply the '#' flag once to give 0 and 1 for V, there
1321
   is %D which doesn't print the throw specs, and %F which does.  */
1322
 
1323
static void
1324
dump_function_decl (tree t, int flags)
1325
{
1326
  tree fntype;
1327
  tree parmtypes;
1328
  tree cname = NULL_TREE;
1329
  tree template_args = NULL_TREE;
1330
  tree template_parms = NULL_TREE;
1331
  int show_return = flags & TFF_RETURN_TYPE || flags & TFF_DECL_SPECIFIERS;
1332
  int do_outer_scope = ! (flags & TFF_UNQUALIFIED_NAME);
1333
  tree exceptions;
1334
  VEC(tree,gc) *typenames = NULL;
1335
 
1336
  if (DECL_NAME (t) && LAMBDA_FUNCTION_P (t))
1337
    {
1338
      /* A lambda's signature is essentially its "type", so defer.  */
1339
      gcc_assert (LAMBDA_TYPE_P (DECL_CONTEXT (t)));
1340
      dump_type (DECL_CONTEXT (t), flags);
1341
      return;
1342
    }
1343
 
1344
  flags &= ~TFF_UNQUALIFIED_NAME;
1345
  if (TREE_CODE (t) == TEMPLATE_DECL)
1346
    t = DECL_TEMPLATE_RESULT (t);
1347
 
1348
  /* Save the exceptions, in case t is a specialization and we are
1349
     emitting an error about incompatible specifications.  */
1350
  exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (t));
1351
 
1352
  /* Pretty print template instantiations only.  */
1353
  if (DECL_USE_TEMPLATE (t) && DECL_TEMPLATE_INFO (t)
1354
      && flag_pretty_templates)
1355
    {
1356
      tree tmpl;
1357
 
1358
      template_args = DECL_TI_ARGS (t);
1359
      tmpl = most_general_template (t);
1360
      if (tmpl && TREE_CODE (tmpl) == TEMPLATE_DECL)
1361
        {
1362
          template_parms = DECL_TEMPLATE_PARMS (tmpl);
1363
          t = tmpl;
1364
          typenames = find_typenames (t);
1365
        }
1366
    }
1367
 
1368
  fntype = TREE_TYPE (t);
1369
  parmtypes = FUNCTION_FIRST_USER_PARMTYPE (t);
1370
 
1371
  if (DECL_CLASS_SCOPE_P (t))
1372
    cname = DECL_CONTEXT (t);
1373
  /* This is for partially instantiated template methods.  */
1374
  else if (TREE_CODE (fntype) == METHOD_TYPE)
1375
    cname = TREE_TYPE (TREE_VALUE (parmtypes));
1376
 
1377
  if (flags & TFF_DECL_SPECIFIERS)
1378
    {
1379
      if (DECL_STATIC_FUNCTION_P (t))
1380
        pp_cxx_ws_string (cxx_pp, "static");
1381
      else if (DECL_VIRTUAL_P (t))
1382
        pp_cxx_ws_string (cxx_pp, "virtual");
1383
 
1384
      if (DECL_DECLARED_CONSTEXPR_P (STRIP_TEMPLATE (t)))
1385
        pp_cxx_ws_string (cxx_pp, "constexpr");
1386
    }
1387
 
1388
  /* Print the return type?  */
1389
  if (show_return)
1390
    show_return = !DECL_CONV_FN_P (t)  && !DECL_CONSTRUCTOR_P (t)
1391
                  && !DECL_DESTRUCTOR_P (t);
1392
  if (show_return)
1393
    dump_type_prefix (TREE_TYPE (fntype), flags);
1394
 
1395
  /* Print the function name.  */
1396
  if (!do_outer_scope)
1397
    /* Nothing.  */;
1398
  else if (cname)
1399
    {
1400
      dump_type (cname, flags);
1401
      pp_cxx_colon_colon (cxx_pp);
1402
    }
1403
  else
1404
    dump_scope (CP_DECL_CONTEXT (t), flags);
1405
 
1406
  dump_function_name (t, flags);
1407
 
1408
  if (!(flags & TFF_NO_FUNCTION_ARGUMENTS))
1409
    {
1410
      dump_parameters (parmtypes, flags);
1411
 
1412
      if (TREE_CODE (fntype) == METHOD_TYPE)
1413
        {
1414
          pp_base (cxx_pp)->padding = pp_before;
1415
          pp_cxx_cv_qualifier_seq (cxx_pp, class_of_this_parm (fntype));
1416
        }
1417
 
1418
      if (flags & TFF_EXCEPTION_SPECIFICATION)
1419
        {
1420
          pp_base (cxx_pp)->padding = pp_before;
1421
          dump_exception_spec (exceptions, flags);
1422
        }
1423
 
1424
      if (show_return)
1425
        dump_type_suffix (TREE_TYPE (fntype), flags);
1426
 
1427
      /* If T is a template instantiation, dump the parameter binding.  */
1428
      if (template_parms != NULL_TREE && template_args != NULL_TREE)
1429
        {
1430
          pp_cxx_whitespace (cxx_pp);
1431
          pp_cxx_left_bracket (cxx_pp);
1432
          pp_cxx_ws_string (cxx_pp, M_("with"));
1433
          pp_cxx_whitespace (cxx_pp);
1434
          dump_template_bindings (template_parms, template_args, typenames);
1435
          pp_cxx_right_bracket (cxx_pp);
1436
        }
1437
    }
1438
  else if (template_args)
1439
    {
1440
      bool need_comma = false;
1441
      int i;
1442
      pp_cxx_begin_template_argument_list (cxx_pp);
1443
      template_args = INNERMOST_TEMPLATE_ARGS (template_args);
1444
      for (i = 0; i < TREE_VEC_LENGTH (template_args); ++i)
1445
        {
1446
          tree arg = TREE_VEC_ELT (template_args, i);
1447
          if (need_comma)
1448
            pp_separate_with_comma (cxx_pp);
1449
          if (ARGUMENT_PACK_P (arg))
1450
            pp_cxx_left_brace (cxx_pp);
1451
          dump_template_argument (arg, TFF_PLAIN_IDENTIFIER);
1452
          if (ARGUMENT_PACK_P (arg))
1453
            pp_cxx_right_brace (cxx_pp);
1454
          need_comma = true;
1455
        }
1456
      pp_cxx_end_template_argument_list (cxx_pp);
1457
    }
1458
}
1459
 
1460
/* Print a parameter list. If this is for a member function, the
1461
   member object ptr (and any other hidden args) should have
1462
   already been removed.  */
1463
 
1464
static void
1465
dump_parameters (tree parmtypes, int flags)
1466
{
1467
  int first = 1;
1468
  flags &= ~TFF_SCOPE;
1469
  pp_cxx_left_paren (cxx_pp);
1470
 
1471
  for (first = 1; parmtypes != void_list_node;
1472
       parmtypes = TREE_CHAIN (parmtypes))
1473
    {
1474
      if (!first)
1475
        pp_separate_with_comma (cxx_pp);
1476
      first = 0;
1477
      if (!parmtypes)
1478
        {
1479
          pp_cxx_ws_string (cxx_pp, "...");
1480
          break;
1481
        }
1482
 
1483
      dump_type (TREE_VALUE (parmtypes), flags);
1484
 
1485
      if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && TREE_PURPOSE (parmtypes))
1486
        {
1487
          pp_cxx_whitespace (cxx_pp);
1488
          pp_equal (cxx_pp);
1489
          pp_cxx_whitespace (cxx_pp);
1490
          dump_expr (TREE_PURPOSE (parmtypes), flags | TFF_EXPR_IN_PARENS);
1491
        }
1492
    }
1493
 
1494
  pp_cxx_right_paren (cxx_pp);
1495
}
1496
 
1497
/* Print an exception specification. T is the exception specification.  */
1498
 
1499
static void
1500
dump_exception_spec (tree t, int flags)
1501
{
1502
  if (t && TREE_PURPOSE (t))
1503
    {
1504
      pp_cxx_ws_string (cxx_pp, "noexcept");
1505
      pp_cxx_whitespace (cxx_pp);
1506
      pp_cxx_left_paren (cxx_pp);
1507
      if (DEFERRED_NOEXCEPT_SPEC_P (t))
1508
        pp_cxx_ws_string (cxx_pp, "<uninstantiated>");
1509
      else
1510
        dump_expr (TREE_PURPOSE (t), flags);
1511
      pp_cxx_right_paren (cxx_pp);
1512
    }
1513
  else if (t)
1514
    {
1515
      pp_cxx_ws_string (cxx_pp, "throw");
1516
      pp_cxx_whitespace (cxx_pp);
1517
      pp_cxx_left_paren (cxx_pp);
1518
      if (TREE_VALUE (t) != NULL_TREE)
1519
        while (1)
1520
          {
1521
            dump_type (TREE_VALUE (t), flags);
1522
            t = TREE_CHAIN (t);
1523
            if (!t)
1524
              break;
1525
            pp_separate_with_comma (cxx_pp);
1526
          }
1527
      pp_cxx_right_paren (cxx_pp);
1528
    }
1529
}
1530
 
1531
/* Handle the function name for a FUNCTION_DECL node, grokking operators
1532
   and destructors properly.  */
1533
 
1534
static void
1535
dump_function_name (tree t, int flags)
1536
{
1537
  tree name = DECL_NAME (t);
1538
 
1539
  /* We can get here with a decl that was synthesized by language-
1540
     independent machinery (e.g. coverage.c) in which case it won't
1541
     have a lang_specific structure attached and DECL_CONSTRUCTOR_P
1542
     will crash.  In this case it is safe just to print out the
1543
     literal name.  */
1544
  if (!DECL_LANG_SPECIFIC (t))
1545
    {
1546
      pp_cxx_tree_identifier (cxx_pp, name);
1547
      return;
1548
    }
1549
 
1550
  if (TREE_CODE (t) == TEMPLATE_DECL)
1551
    t = DECL_TEMPLATE_RESULT (t);
1552
 
1553
  /* Don't let the user see __comp_ctor et al.  */
1554
  if (DECL_CONSTRUCTOR_P (t)
1555
      || DECL_DESTRUCTOR_P (t))
1556
    {
1557
      if (LAMBDA_TYPE_P (DECL_CONTEXT (t)))
1558
        name = get_identifier ("<lambda>");
1559
      else
1560
        name = constructor_name (DECL_CONTEXT (t));
1561
    }
1562
 
1563
  if (DECL_DESTRUCTOR_P (t))
1564
    {
1565
      pp_cxx_complement (cxx_pp);
1566
      dump_decl (name, TFF_PLAIN_IDENTIFIER);
1567
    }
1568
  else if (DECL_CONV_FN_P (t))
1569
    {
1570
      /* This cannot use the hack that the operator's return
1571
         type is stashed off of its name because it may be
1572
         used for error reporting.  In the case of conflicting
1573
         declarations, both will have the same name, yet
1574
         the types will be different, hence the TREE_TYPE field
1575
         of the first name will be clobbered by the second.  */
1576
      pp_cxx_ws_string (cxx_pp, "operator");
1577
      dump_type (TREE_TYPE (TREE_TYPE (t)), flags);
1578
    }
1579
  else if (name && IDENTIFIER_OPNAME_P (name))
1580
    pp_cxx_tree_identifier (cxx_pp, name);
1581
  else if (name && UDLIT_OPER_P (name))
1582
    pp_cxx_tree_identifier (cxx_pp, name);
1583
  else
1584
    dump_decl (name, flags);
1585
 
1586
  if (DECL_TEMPLATE_INFO (t)
1587
      && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (t)
1588
      && (TREE_CODE (DECL_TI_TEMPLATE (t)) != TEMPLATE_DECL
1589
          || PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
1590
    dump_template_parms (DECL_TEMPLATE_INFO (t), !DECL_USE_TEMPLATE (t), flags);
1591
}
1592
 
1593
/* Dump the template parameters from the template info INFO under control of
1594
   FLAGS. PRIMARY indicates whether this is a primary template decl, or
1595
   specialization (partial or complete). For partial specializations we show
1596
   the specialized parameter values. For a primary template we show no
1597
   decoration.  */
1598
 
1599
static void
1600
dump_template_parms (tree info, int primary, int flags)
1601
{
1602
  tree args = info ? TI_ARGS (info) : NULL_TREE;
1603
 
1604
  if (primary && flags & TFF_TEMPLATE_NAME)
1605
    return;
1606
  flags &= ~(TFF_CLASS_KEY_OR_ENUM | TFF_TEMPLATE_NAME);
1607
  pp_cxx_begin_template_argument_list (cxx_pp);
1608
 
1609
  /* Be careful only to print things when we have them, so as not
1610
     to crash producing error messages.  */
1611
  if (args && !primary)
1612
    {
1613
      int len, ix;
1614
      len = get_non_default_template_args_count (args, flags);
1615
 
1616
      args = INNERMOST_TEMPLATE_ARGS (args);
1617
      for (ix = 0; ix != len; ix++)
1618
        {
1619
          tree arg = TREE_VEC_ELT (args, ix);
1620
 
1621
          /* Only print a comma if we know there is an argument coming. In
1622
             the case of an empty template argument pack, no actual
1623
             argument will be printed.  */
1624
          if (ix
1625
              && (!ARGUMENT_PACK_P (arg)
1626
                  || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) > 0))
1627
            pp_separate_with_comma (cxx_pp);
1628
 
1629
          if (!arg)
1630
            pp_string (cxx_pp, M_("<template parameter error>"));
1631
          else
1632
            dump_template_argument (arg, flags);
1633
        }
1634
    }
1635
  else if (primary)
1636
    {
1637
      tree tpl = TI_TEMPLATE (info);
1638
      tree parms = DECL_TEMPLATE_PARMS (tpl);
1639
      int len, ix;
1640
 
1641
      parms = TREE_CODE (parms) == TREE_LIST ? TREE_VALUE (parms) : NULL_TREE;
1642
      len = parms ? TREE_VEC_LENGTH (parms) : 0;
1643
 
1644
      for (ix = 0; ix != len; ix++)
1645
        {
1646
          tree parm;
1647
 
1648
          if (TREE_VEC_ELT (parms, ix) == error_mark_node)
1649
            {
1650
              pp_string (cxx_pp, M_("<template parameter error>"));
1651
              continue;
1652
            }
1653
 
1654
          parm = TREE_VALUE (TREE_VEC_ELT (parms, ix));
1655
 
1656
          if (ix)
1657
            pp_separate_with_comma (cxx_pp);
1658
 
1659
          dump_decl (parm, flags & ~TFF_DECL_SPECIFIERS);
1660
        }
1661
    }
1662
  pp_cxx_end_template_argument_list (cxx_pp);
1663
}
1664
 
1665
/* Print out the arguments of CALL_EXPR T as a parenthesized list using
1666
   flags FLAGS.  Skip over the first argument if SKIPFIRST is true.  */
1667
 
1668
static void
1669
dump_call_expr_args (tree t, int flags, bool skipfirst)
1670
{
1671
  tree arg;
1672
  call_expr_arg_iterator iter;
1673
 
1674
  pp_cxx_left_paren (cxx_pp);
1675
  FOR_EACH_CALL_EXPR_ARG (arg, iter, t)
1676
    {
1677
      if (skipfirst)
1678
        skipfirst = false;
1679
      else
1680
        {
1681
          dump_expr (arg, flags | TFF_EXPR_IN_PARENS);
1682
          if (more_call_expr_args_p (&iter))
1683
            pp_separate_with_comma (cxx_pp);
1684
        }
1685
    }
1686
  pp_cxx_right_paren (cxx_pp);
1687
}
1688
 
1689
/* Print out the arguments of AGGR_INIT_EXPR T as a parenthesized list
1690
   using flags FLAGS.  Skip over the first argument if SKIPFIRST is
1691
   true.  */
1692
 
1693
static void
1694
dump_aggr_init_expr_args (tree t, int flags, bool skipfirst)
1695
{
1696
  tree arg;
1697
  aggr_init_expr_arg_iterator iter;
1698
 
1699
  pp_cxx_left_paren (cxx_pp);
1700
  FOR_EACH_AGGR_INIT_EXPR_ARG (arg, iter, t)
1701
    {
1702
      if (skipfirst)
1703
        skipfirst = false;
1704
      else
1705
        {
1706
          dump_expr (arg, flags | TFF_EXPR_IN_PARENS);
1707
          if (more_aggr_init_expr_args_p (&iter))
1708
            pp_separate_with_comma (cxx_pp);
1709
        }
1710
    }
1711
  pp_cxx_right_paren (cxx_pp);
1712
}
1713
 
1714
/* Print out a list of initializers (subr of dump_expr).  */
1715
 
1716
static void
1717
dump_expr_list (tree l, int flags)
1718
{
1719
  while (l)
1720
    {
1721
      dump_expr (TREE_VALUE (l), flags | TFF_EXPR_IN_PARENS);
1722
      l = TREE_CHAIN (l);
1723
      if (l)
1724
        pp_separate_with_comma (cxx_pp);
1725
    }
1726
}
1727
 
1728
/* Print out a vector of initializers (subr of dump_expr).  */
1729
 
1730
static void
1731
dump_expr_init_vec (VEC(constructor_elt,gc) *v, int flags)
1732
{
1733
  unsigned HOST_WIDE_INT idx;
1734
  tree value;
1735
 
1736
  FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
1737
    {
1738
      dump_expr (value, flags | TFF_EXPR_IN_PARENS);
1739
      if (idx != VEC_length (constructor_elt, v) - 1)
1740
        pp_separate_with_comma (cxx_pp);
1741
    }
1742
}
1743
 
1744
 
1745
/* We've gotten an indirect REFERENCE (an OBJ_TYPE_REF) to a virtual
1746
   function.  Resolve it to a close relative -- in the sense of static
1747
   type -- variant being overridden.  That is close to what was written in
1748
   the source code.  Subroutine of dump_expr.  */
1749
 
1750
static tree
1751
resolve_virtual_fun_from_obj_type_ref (tree ref)
1752
{
1753
  tree obj_type = TREE_TYPE (OBJ_TYPE_REF_OBJECT (ref));
1754
  HOST_WIDE_INT index = tree_low_cst (OBJ_TYPE_REF_TOKEN (ref), 1);
1755
  tree fun = BINFO_VIRTUALS (TYPE_BINFO (TREE_TYPE (obj_type)));
1756
  while (index)
1757
    {
1758
      fun = TREE_CHAIN (fun);
1759
      index -= (TARGET_VTABLE_USES_DESCRIPTORS
1760
                ? TARGET_VTABLE_USES_DESCRIPTORS : 1);
1761
    }
1762
 
1763
  return BV_FN (fun);
1764
}
1765
 
1766
/* Print out an expression E under control of FLAGS.  */
1767
 
1768
static void
1769
dump_expr (tree t, int flags)
1770
{
1771
  if (t == 0)
1772
    return;
1773
 
1774
  if (STATEMENT_CLASS_P (t))
1775
    {
1776
      pp_cxx_ws_string (cxx_pp, M_("<statement>"));
1777
      return;
1778
    }
1779
 
1780
  switch (TREE_CODE (t))
1781
    {
1782
    case VAR_DECL:
1783
    case PARM_DECL:
1784
    case FIELD_DECL:
1785
    case CONST_DECL:
1786
    case FUNCTION_DECL:
1787
    case TEMPLATE_DECL:
1788
    case NAMESPACE_DECL:
1789
    case LABEL_DECL:
1790
    case OVERLOAD:
1791
    case TYPE_DECL:
1792
    case IDENTIFIER_NODE:
1793
      dump_decl (t, ((flags & ~(TFF_DECL_SPECIFIERS|TFF_RETURN_TYPE
1794
                                |TFF_TEMPLATE_HEADER))
1795
                     | TFF_NO_FUNCTION_ARGUMENTS));
1796
      break;
1797
 
1798
    case SSA_NAME:
1799
      if (!DECL_ARTIFICIAL (SSA_NAME_VAR (t)))
1800
        dump_expr (SSA_NAME_VAR (t), flags);
1801
      else
1802
        pp_cxx_ws_string (cxx_pp, M_("<unknown>"));
1803
      break;
1804
 
1805
    case INTEGER_CST:
1806
    case REAL_CST:
1807
    case STRING_CST:
1808
    case COMPLEX_CST:
1809
      pp_constant (cxx_pp, t);
1810
      break;
1811
 
1812
    case USERDEF_LITERAL:
1813
      pp_cxx_userdef_literal (cxx_pp, t);
1814
      break;
1815
 
1816
    case THROW_EXPR:
1817
      /* While waiting for caret diagnostics, avoid printing
1818
         __cxa_allocate_exception, __cxa_throw, and the like.  */
1819
      pp_cxx_ws_string (cxx_pp, M_("<throw-expression>"));
1820
      break;
1821
 
1822
    case PTRMEM_CST:
1823
      pp_ampersand (cxx_pp);
1824
      dump_type (PTRMEM_CST_CLASS (t), flags);
1825
      pp_cxx_colon_colon (cxx_pp);
1826
      pp_cxx_tree_identifier (cxx_pp, DECL_NAME (PTRMEM_CST_MEMBER (t)));
1827
      break;
1828
 
1829
    case COMPOUND_EXPR:
1830
      pp_cxx_left_paren (cxx_pp);
1831
      dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1832
      pp_separate_with_comma (cxx_pp);
1833
      dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
1834
      pp_cxx_right_paren (cxx_pp);
1835
      break;
1836
 
1837
    case COND_EXPR:
1838
      pp_cxx_left_paren (cxx_pp);
1839
      dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1840
      pp_string (cxx_pp, " ? ");
1841
      dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
1842
      pp_string (cxx_pp, " : ");
1843
      dump_expr (TREE_OPERAND (t, 2), flags | TFF_EXPR_IN_PARENS);
1844
      pp_cxx_right_paren (cxx_pp);
1845
      break;
1846
 
1847
    case SAVE_EXPR:
1848
      if (TREE_HAS_CONSTRUCTOR (t))
1849
        {
1850
          pp_cxx_ws_string (cxx_pp, "new");
1851
          pp_cxx_whitespace (cxx_pp);
1852
          dump_type (TREE_TYPE (TREE_TYPE (t)), flags);
1853
        }
1854
      else
1855
        dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1856
      break;
1857
 
1858
    case AGGR_INIT_EXPR:
1859
      {
1860
        tree fn = NULL_TREE;
1861
 
1862
        if (TREE_CODE (AGGR_INIT_EXPR_FN (t)) == ADDR_EXPR)
1863
          fn = TREE_OPERAND (AGGR_INIT_EXPR_FN (t), 0);
1864
 
1865
        if (fn && TREE_CODE (fn) == FUNCTION_DECL)
1866
          {
1867
            if (DECL_CONSTRUCTOR_P (fn))
1868
              dump_type (DECL_CONTEXT (fn), flags);
1869
            else
1870
              dump_decl (fn, 0);
1871
          }
1872
        else
1873
          dump_expr (AGGR_INIT_EXPR_FN (t), 0);
1874
      }
1875
      dump_aggr_init_expr_args (t, flags, true);
1876
      break;
1877
 
1878
    case CALL_EXPR:
1879
      {
1880
        tree fn = CALL_EXPR_FN (t);
1881
        bool skipfirst = false;
1882
 
1883
        if (TREE_CODE (fn) == ADDR_EXPR)
1884
          fn = TREE_OPERAND (fn, 0);
1885
 
1886
        /* Nobody is interested in seeing the guts of vcalls.  */
1887
        if (TREE_CODE (fn) == OBJ_TYPE_REF)
1888
          fn = resolve_virtual_fun_from_obj_type_ref (fn);
1889
 
1890
        if (TREE_TYPE (fn) != NULL_TREE
1891
            && NEXT_CODE (fn) == METHOD_TYPE
1892
            && call_expr_nargs (t))
1893
          {
1894
            tree ob = CALL_EXPR_ARG (t, 0);
1895
            if (TREE_CODE (ob) == ADDR_EXPR)
1896
              {
1897
                dump_expr (TREE_OPERAND (ob, 0), flags | TFF_EXPR_IN_PARENS);
1898
                pp_cxx_dot (cxx_pp);
1899
              }
1900
            else if (TREE_CODE (ob) != PARM_DECL
1901
                     || strcmp (IDENTIFIER_POINTER (DECL_NAME (ob)), "this"))
1902
              {
1903
                dump_expr (ob, flags | TFF_EXPR_IN_PARENS);
1904
                pp_cxx_arrow (cxx_pp);
1905
              }
1906
            skipfirst = true;
1907
          }
1908
        dump_expr (fn, flags | TFF_EXPR_IN_PARENS);
1909
        dump_call_expr_args (t, flags, skipfirst);
1910
      }
1911
      break;
1912
 
1913
    case TARGET_EXPR:
1914
      /* Note that this only works for G++ target exprs.  If somebody
1915
         builds a general TARGET_EXPR, there's no way to represent that
1916
         it initializes anything other that the parameter slot for the
1917
         default argument.  Note we may have cleared out the first
1918
         operand in expand_expr, so don't go killing ourselves.  */
1919
      if (TREE_OPERAND (t, 1))
1920
        dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
1921
      break;
1922
 
1923
    case POINTER_PLUS_EXPR:
1924
      dump_binary_op ("+", t, flags);
1925
      break;
1926
 
1927
    case INIT_EXPR:
1928
    case MODIFY_EXPR:
1929
      dump_binary_op (assignment_operator_name_info[(int)NOP_EXPR].name,
1930
                      t, flags);
1931
      break;
1932
 
1933
    case PLUS_EXPR:
1934
    case MINUS_EXPR:
1935
    case MULT_EXPR:
1936
    case TRUNC_DIV_EXPR:
1937
    case TRUNC_MOD_EXPR:
1938
    case MIN_EXPR:
1939
    case MAX_EXPR:
1940
    case LSHIFT_EXPR:
1941
    case RSHIFT_EXPR:
1942
    case BIT_IOR_EXPR:
1943
    case BIT_XOR_EXPR:
1944
    case BIT_AND_EXPR:
1945
    case TRUTH_ANDIF_EXPR:
1946
    case TRUTH_ORIF_EXPR:
1947
    case LT_EXPR:
1948
    case LE_EXPR:
1949
    case GT_EXPR:
1950
    case GE_EXPR:
1951
    case EQ_EXPR:
1952
    case NE_EXPR:
1953
    case EXACT_DIV_EXPR:
1954
      dump_binary_op (operator_name_info[(int) TREE_CODE (t)].name, t, flags);
1955
      break;
1956
 
1957
    case CEIL_DIV_EXPR:
1958
    case FLOOR_DIV_EXPR:
1959
    case ROUND_DIV_EXPR:
1960
    case RDIV_EXPR:
1961
      dump_binary_op ("/", t, flags);
1962
      break;
1963
 
1964
    case CEIL_MOD_EXPR:
1965
    case FLOOR_MOD_EXPR:
1966
    case ROUND_MOD_EXPR:
1967
      dump_binary_op ("%", t, flags);
1968
      break;
1969
 
1970
    case COMPONENT_REF:
1971
      {
1972
        tree ob = TREE_OPERAND (t, 0);
1973
        if (TREE_CODE (ob) == INDIRECT_REF)
1974
          {
1975
            ob = TREE_OPERAND (ob, 0);
1976
            if (TREE_CODE (ob) != PARM_DECL
1977
                || (DECL_NAME (ob)
1978
                    && strcmp (IDENTIFIER_POINTER (DECL_NAME (ob)), "this")))
1979
              {
1980
                dump_expr (ob, flags | TFF_EXPR_IN_PARENS);
1981
                if (TREE_CODE (TREE_TYPE (ob)) == REFERENCE_TYPE)
1982
                  pp_cxx_dot (cxx_pp);
1983
                else
1984
                  pp_cxx_arrow (cxx_pp);
1985
              }
1986
          }
1987
        else
1988
          {
1989
            dump_expr (ob, flags | TFF_EXPR_IN_PARENS);
1990
            pp_cxx_dot (cxx_pp);
1991
          }
1992
        dump_expr (TREE_OPERAND (t, 1), flags & ~TFF_EXPR_IN_PARENS);
1993
      }
1994
      break;
1995
 
1996
    case ARRAY_REF:
1997
      dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1998
      pp_cxx_left_bracket (cxx_pp);
1999
      dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2000
      pp_cxx_right_bracket (cxx_pp);
2001
      break;
2002
 
2003
    case UNARY_PLUS_EXPR:
2004
      dump_unary_op ("+", t, flags);
2005
      break;
2006
 
2007
    case ADDR_EXPR:
2008
      if (TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
2009
          || TREE_CODE (TREE_OPERAND (t, 0)) == STRING_CST
2010
          /* An ADDR_EXPR can have reference type.  In that case, we
2011
             shouldn't print the `&' doing so indicates to the user
2012
             that the expression has pointer type.  */
2013
          || (TREE_TYPE (t)
2014
              && TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE))
2015
        dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2016
      else if (TREE_CODE (TREE_OPERAND (t, 0)) == LABEL_DECL)
2017
        dump_unary_op ("&&", t, flags);
2018
      else
2019
        dump_unary_op ("&", t, flags);
2020
      break;
2021
 
2022
    case INDIRECT_REF:
2023
      if (TREE_HAS_CONSTRUCTOR (t))
2024
        {
2025
          t = TREE_OPERAND (t, 0);
2026
          gcc_assert (TREE_CODE (t) == CALL_EXPR);
2027
          dump_expr (CALL_EXPR_FN (t), flags | TFF_EXPR_IN_PARENS);
2028
          dump_call_expr_args (t, flags, true);
2029
        }
2030
      else
2031
        {
2032
          if (TREE_OPERAND (t,0) != NULL_TREE
2033
              && TREE_TYPE (TREE_OPERAND (t, 0))
2034
              && NEXT_CODE (TREE_OPERAND (t, 0)) == REFERENCE_TYPE)
2035
            dump_expr (TREE_OPERAND (t, 0), flags);
2036
          else
2037
            dump_unary_op ("*", t, flags);
2038
        }
2039
      break;
2040
 
2041
    case MEM_REF:
2042
      if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
2043
          && integer_zerop (TREE_OPERAND (t, 1)))
2044
        dump_expr (TREE_OPERAND (TREE_OPERAND (t, 0), 0), flags);
2045
      else
2046
        {
2047
          pp_cxx_star (cxx_pp);
2048
          if (!integer_zerop (TREE_OPERAND (t, 1)))
2049
            {
2050
              pp_cxx_left_paren (cxx_pp);
2051
              if (!integer_onep (TYPE_SIZE_UNIT
2052
                                 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0))))))
2053
                {
2054
                  pp_cxx_left_paren (cxx_pp);
2055
                  dump_type (ptr_type_node, flags);
2056
                  pp_cxx_right_paren (cxx_pp);
2057
                }
2058
            }
2059
          dump_expr (TREE_OPERAND (t, 0), flags);
2060
          if (!integer_zerop (TREE_OPERAND (t, 1)))
2061
            {
2062
              pp_cxx_ws_string (cxx_pp, "+");
2063
              dump_expr (fold_convert (ssizetype, TREE_OPERAND (t, 1)), flags);
2064
              pp_cxx_right_paren (cxx_pp);
2065
            }
2066
        }
2067
      break;
2068
 
2069
    case NEGATE_EXPR:
2070
    case BIT_NOT_EXPR:
2071
    case TRUTH_NOT_EXPR:
2072
    case PREDECREMENT_EXPR:
2073
    case PREINCREMENT_EXPR:
2074
      dump_unary_op (operator_name_info [(int)TREE_CODE (t)].name, t, flags);
2075
      break;
2076
 
2077
    case POSTDECREMENT_EXPR:
2078
    case POSTINCREMENT_EXPR:
2079
      pp_cxx_left_paren (cxx_pp);
2080
      dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2081
      pp_cxx_ws_string (cxx_pp, operator_name_info[(int)TREE_CODE (t)].name);
2082
      pp_cxx_right_paren (cxx_pp);
2083
      break;
2084
 
2085
    case NON_LVALUE_EXPR:
2086
      /* FIXME: This is a KLUDGE workaround for a parsing problem.  There
2087
         should be another level of INDIRECT_REF so that I don't have to do
2088
         this.  */
2089
      if (TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == POINTER_TYPE)
2090
        {
2091
          tree next = TREE_TYPE (TREE_TYPE (t));
2092
 
2093
          while (TREE_CODE (next) == POINTER_TYPE)
2094
            next = TREE_TYPE (next);
2095
 
2096
          if (TREE_CODE (next) == FUNCTION_TYPE)
2097
            {
2098
              if (flags & TFF_EXPR_IN_PARENS)
2099
                pp_cxx_left_paren (cxx_pp);
2100
              pp_cxx_star (cxx_pp);
2101
              dump_expr (TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2102
              if (flags & TFF_EXPR_IN_PARENS)
2103
                pp_cxx_right_paren (cxx_pp);
2104
              break;
2105
            }
2106
          /* Else fall through.  */
2107
        }
2108
      dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2109
      break;
2110
 
2111
    CASE_CONVERT:
2112
    case IMPLICIT_CONV_EXPR:
2113
    case VIEW_CONVERT_EXPR:
2114
      {
2115
        tree op = TREE_OPERAND (t, 0);
2116
        tree ttype = TREE_TYPE (t);
2117
        tree optype = TREE_TYPE (op);
2118
 
2119
        if (TREE_CODE (ttype) != TREE_CODE (optype)
2120
            && POINTER_TYPE_P (ttype)
2121
            && POINTER_TYPE_P (optype)
2122
            && same_type_p (TREE_TYPE (optype),
2123
                            TREE_TYPE (ttype)))
2124
          {
2125
            if (TREE_CODE (ttype) == REFERENCE_TYPE)
2126
              dump_unary_op ("*", t, flags);
2127
            else
2128
              dump_unary_op ("&", t, flags);
2129
          }
2130
        else if (!same_type_p (TREE_TYPE (op), TREE_TYPE (t)))
2131
          {
2132
            /* It is a cast, but we cannot tell whether it is a
2133
               reinterpret or static cast. Use the C style notation.  */
2134
            if (flags & TFF_EXPR_IN_PARENS)
2135
              pp_cxx_left_paren (cxx_pp);
2136
            pp_cxx_left_paren (cxx_pp);
2137
            dump_type (TREE_TYPE (t), flags);
2138
            pp_cxx_right_paren (cxx_pp);
2139
            dump_expr (op, flags | TFF_EXPR_IN_PARENS);
2140
            if (flags & TFF_EXPR_IN_PARENS)
2141
              pp_cxx_right_paren (cxx_pp);
2142
          }
2143
        else
2144
          dump_expr (op, flags);
2145
        break;
2146
      }
2147
 
2148
    case CONSTRUCTOR:
2149
      if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
2150
        {
2151
          tree idx = build_ptrmemfunc_access_expr (t, pfn_identifier);
2152
 
2153
          if (integer_zerop (idx))
2154
            {
2155
              /* A NULL pointer-to-member constant.  */
2156
              pp_cxx_left_paren (cxx_pp);
2157
              pp_cxx_left_paren (cxx_pp);
2158
              dump_type (TREE_TYPE (t), flags);
2159
              pp_cxx_right_paren (cxx_pp);
2160
              pp_character (cxx_pp, '0');
2161
              pp_cxx_right_paren (cxx_pp);
2162
              break;
2163
            }
2164
          else if (host_integerp (idx, 0))
2165
            {
2166
              tree virtuals;
2167
              unsigned HOST_WIDE_INT n;
2168
 
2169
              t = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
2170
              t = TYPE_METHOD_BASETYPE (t);
2171
              virtuals = BINFO_VIRTUALS (TYPE_BINFO (TYPE_MAIN_VARIANT (t)));
2172
 
2173
              n = tree_low_cst (idx, 0);
2174
 
2175
              /* Map vtable index back one, to allow for the null pointer to
2176
                 member.  */
2177
              --n;
2178
 
2179
              while (n > 0 && virtuals)
2180
                {
2181
                  --n;
2182
                  virtuals = TREE_CHAIN (virtuals);
2183
                }
2184
              if (virtuals)
2185
                {
2186
                  dump_expr (BV_FN (virtuals),
2187
                             flags | TFF_EXPR_IN_PARENS);
2188
                  break;
2189
                }
2190
            }
2191
        }
2192
      if (TREE_TYPE (t) && LAMBDA_TYPE_P (TREE_TYPE (t)))
2193
        pp_string (cxx_pp, "<lambda closure object>");
2194
      if (TREE_TYPE (t) && EMPTY_CONSTRUCTOR_P (t))
2195
        {
2196
          dump_type (TREE_TYPE (t), 0);
2197
          pp_cxx_left_paren (cxx_pp);
2198
          pp_cxx_right_paren (cxx_pp);
2199
        }
2200
      else
2201
        {
2202
          if (!BRACE_ENCLOSED_INITIALIZER_P (t))
2203
            dump_type (TREE_TYPE (t), 0);
2204
          pp_cxx_left_brace (cxx_pp);
2205
          dump_expr_init_vec (CONSTRUCTOR_ELTS (t), flags);
2206
          pp_cxx_right_brace (cxx_pp);
2207
        }
2208
 
2209
      break;
2210
 
2211
    case OFFSET_REF:
2212
      {
2213
        tree ob = TREE_OPERAND (t, 0);
2214
        if (is_dummy_object (ob))
2215
          {
2216
            t = TREE_OPERAND (t, 1);
2217
            if (TREE_CODE (t) == FUNCTION_DECL)
2218
              /* A::f */
2219
              dump_expr (t, flags | TFF_EXPR_IN_PARENS);
2220
            else if (BASELINK_P (t))
2221
              dump_expr (OVL_CURRENT (BASELINK_FUNCTIONS (t)),
2222
                         flags | TFF_EXPR_IN_PARENS);
2223
            else
2224
              dump_decl (t, flags);
2225
          }
2226
        else
2227
          {
2228
            if (TREE_CODE (ob) == INDIRECT_REF)
2229
              {
2230
                dump_expr (TREE_OPERAND (ob, 0), flags | TFF_EXPR_IN_PARENS);
2231
                pp_cxx_arrow (cxx_pp);
2232
                pp_cxx_star (cxx_pp);
2233
              }
2234
            else
2235
              {
2236
                dump_expr (ob, flags | TFF_EXPR_IN_PARENS);
2237
                pp_cxx_dot (cxx_pp);
2238
                pp_cxx_star (cxx_pp);
2239
              }
2240
            dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2241
          }
2242
        break;
2243
      }
2244
 
2245
    case TEMPLATE_PARM_INDEX:
2246
      dump_decl (TEMPLATE_PARM_DECL (t), flags & ~TFF_DECL_SPECIFIERS);
2247
      break;
2248
 
2249
    case CAST_EXPR:
2250
      if (TREE_OPERAND (t, 0) == NULL_TREE
2251
          || TREE_CHAIN (TREE_OPERAND (t, 0)))
2252
        {
2253
          dump_type (TREE_TYPE (t), flags);
2254
          pp_cxx_left_paren (cxx_pp);
2255
          dump_expr_list (TREE_OPERAND (t, 0), flags);
2256
          pp_cxx_right_paren (cxx_pp);
2257
        }
2258
      else
2259
        {
2260
          pp_cxx_left_paren (cxx_pp);
2261
          dump_type (TREE_TYPE (t), flags);
2262
          pp_cxx_right_paren (cxx_pp);
2263
          pp_cxx_left_paren (cxx_pp);
2264
          dump_expr_list (TREE_OPERAND (t, 0), flags);
2265
          pp_cxx_right_paren (cxx_pp);
2266
        }
2267
      break;
2268
 
2269
    case STATIC_CAST_EXPR:
2270
      pp_cxx_ws_string (cxx_pp, "static_cast");
2271
      goto cast;
2272
    case REINTERPRET_CAST_EXPR:
2273
      pp_cxx_ws_string (cxx_pp, "reinterpret_cast");
2274
      goto cast;
2275
    case CONST_CAST_EXPR:
2276
      pp_cxx_ws_string (cxx_pp, "const_cast");
2277
      goto cast;
2278
    case DYNAMIC_CAST_EXPR:
2279
      pp_cxx_ws_string (cxx_pp, "dynamic_cast");
2280
    cast:
2281
      pp_cxx_begin_template_argument_list (cxx_pp);
2282
      dump_type (TREE_TYPE (t), flags);
2283
      pp_cxx_end_template_argument_list (cxx_pp);
2284
      pp_cxx_left_paren (cxx_pp);
2285
      dump_expr (TREE_OPERAND (t, 0), flags);
2286
      pp_cxx_right_paren (cxx_pp);
2287
      break;
2288
 
2289
    case ARROW_EXPR:
2290
      dump_expr (TREE_OPERAND (t, 0), flags);
2291
      pp_cxx_arrow (cxx_pp);
2292
      break;
2293
 
2294
    case SIZEOF_EXPR:
2295
    case ALIGNOF_EXPR:
2296
      if (TREE_CODE (t) == SIZEOF_EXPR)
2297
        pp_cxx_ws_string (cxx_pp, "sizeof");
2298
      else
2299
        {
2300
          gcc_assert (TREE_CODE (t) == ALIGNOF_EXPR);
2301
          pp_cxx_ws_string (cxx_pp, "__alignof__");
2302
        }
2303
      pp_cxx_whitespace (cxx_pp);
2304
      pp_cxx_left_paren (cxx_pp);
2305
      if (TYPE_P (TREE_OPERAND (t, 0)))
2306
        dump_type (TREE_OPERAND (t, 0), flags);
2307
      else
2308
        dump_expr (TREE_OPERAND (t, 0), flags);
2309
      pp_cxx_right_paren (cxx_pp);
2310
      break;
2311
 
2312
    case AT_ENCODE_EXPR:
2313
      pp_cxx_ws_string (cxx_pp, "@encode");
2314
      pp_cxx_whitespace (cxx_pp);
2315
      pp_cxx_left_paren (cxx_pp);
2316
      dump_type (TREE_OPERAND (t, 0), flags);
2317
      pp_cxx_right_paren (cxx_pp);
2318
      break;
2319
 
2320
    case NOEXCEPT_EXPR:
2321
      pp_cxx_ws_string (cxx_pp, "noexcept");
2322
      pp_cxx_whitespace (cxx_pp);
2323
      pp_cxx_left_paren (cxx_pp);
2324
      dump_expr (TREE_OPERAND (t, 0), flags);
2325
      pp_cxx_right_paren (cxx_pp);
2326
      break;
2327
 
2328
    case REALPART_EXPR:
2329
    case IMAGPART_EXPR:
2330
      pp_cxx_ws_string (cxx_pp, operator_name_info[TREE_CODE (t)].name);
2331
      pp_cxx_whitespace (cxx_pp);
2332
      dump_expr (TREE_OPERAND (t, 0), flags);
2333
      break;
2334
 
2335
    case DEFAULT_ARG:
2336
      pp_string (cxx_pp, M_("<unparsed>"));
2337
      break;
2338
 
2339
    case TRY_CATCH_EXPR:
2340
    case WITH_CLEANUP_EXPR:
2341
    case CLEANUP_POINT_EXPR:
2342
      dump_expr (TREE_OPERAND (t, 0), flags);
2343
      break;
2344
 
2345
    case PSEUDO_DTOR_EXPR:
2346
      dump_expr (TREE_OPERAND (t, 2), flags);
2347
      pp_cxx_dot (cxx_pp);
2348
      dump_type (TREE_OPERAND (t, 0), flags);
2349
      pp_cxx_colon_colon (cxx_pp);
2350
      pp_cxx_complement (cxx_pp);
2351
      dump_type (TREE_OPERAND (t, 1), flags);
2352
      break;
2353
 
2354
    case TEMPLATE_ID_EXPR:
2355
      dump_decl (t, flags);
2356
      break;
2357
 
2358
    case BIND_EXPR:
2359
    case STMT_EXPR:
2360
    case EXPR_STMT:
2361
    case STATEMENT_LIST:
2362
      /* We don't yet have a way of dumping statements in a
2363
         human-readable format.  */
2364
      pp_string (cxx_pp, "({...})");
2365
      break;
2366
 
2367
    case LOOP_EXPR:
2368
      pp_string (cxx_pp, "while (1) { ");
2369
      dump_expr (TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2370
      pp_cxx_right_brace (cxx_pp);
2371
      break;
2372
 
2373
    case EXIT_EXPR:
2374
      pp_string (cxx_pp, "if (");
2375
      dump_expr (TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2376
      pp_string (cxx_pp, ") break; ");
2377
      break;
2378
 
2379
    case BASELINK:
2380
      dump_expr (BASELINK_FUNCTIONS (t), flags & ~TFF_EXPR_IN_PARENS);
2381
      break;
2382
 
2383
    case EMPTY_CLASS_EXPR:
2384
      dump_type (TREE_TYPE (t), flags);
2385
      pp_cxx_left_paren (cxx_pp);
2386
      pp_cxx_right_paren (cxx_pp);
2387
      break;
2388
 
2389
    case NON_DEPENDENT_EXPR:
2390
      dump_expr (TREE_OPERAND (t, 0), flags);
2391
      break;
2392
 
2393
    case ARGUMENT_PACK_SELECT:
2394
      dump_template_argument (ARGUMENT_PACK_SELECT_FROM_PACK (t), flags);
2395
      break;
2396
 
2397
    case RECORD_TYPE:
2398
    case UNION_TYPE:
2399
    case ENUMERAL_TYPE:
2400
    case REAL_TYPE:
2401
    case VOID_TYPE:
2402
    case BOOLEAN_TYPE:
2403
    case INTEGER_TYPE:
2404
    case COMPLEX_TYPE:
2405
    case VECTOR_TYPE:
2406
      pp_type_specifier_seq (cxx_pp, t);
2407
      break;
2408
 
2409
    case TYPENAME_TYPE:
2410
      /* We get here when we want to print a dependent type as an
2411
         id-expression, without any disambiguator decoration.  */
2412
      pp_id_expression (cxx_pp, t);
2413
      break;
2414
 
2415
    case TEMPLATE_TYPE_PARM:
2416
    case TEMPLATE_TEMPLATE_PARM:
2417
    case BOUND_TEMPLATE_TEMPLATE_PARM:
2418
      dump_type (t, flags);
2419
      break;
2420
 
2421
    case TRAIT_EXPR:
2422
      pp_cxx_trait_expression (cxx_pp, t);
2423
      break;
2424
 
2425
    case VA_ARG_EXPR:
2426
      pp_cxx_va_arg_expression (cxx_pp, t);
2427
      break;
2428
 
2429
    case OFFSETOF_EXPR:
2430
      pp_cxx_offsetof_expression (cxx_pp, t);
2431
      break;
2432
 
2433
    case SCOPE_REF:
2434
      dump_decl (t, flags);
2435
      break;
2436
 
2437
    case EXPR_PACK_EXPANSION:
2438
    case TYPEID_EXPR:
2439
    case MEMBER_REF:
2440
    case DOTSTAR_EXPR:
2441
    case NEW_EXPR:
2442
    case VEC_NEW_EXPR:
2443
    case DELETE_EXPR:
2444
    case VEC_DELETE_EXPR:
2445
    case MODOP_EXPR:
2446
    case ABS_EXPR:
2447
    case CONJ_EXPR:
2448
    case VECTOR_CST:
2449
    case FIXED_CST:
2450
    case UNORDERED_EXPR:
2451
    case ORDERED_EXPR:
2452
    case UNLT_EXPR:
2453
    case UNLE_EXPR:
2454
    case UNGT_EXPR:
2455
    case UNGE_EXPR:
2456
    case UNEQ_EXPR:
2457
    case LTGT_EXPR:
2458
    case COMPLEX_EXPR:
2459
    case BIT_FIELD_REF:
2460
    case FIX_TRUNC_EXPR:
2461
    case FLOAT_EXPR:
2462
      pp_expression (cxx_pp, t);
2463
      break;
2464
 
2465
    case TRUTH_AND_EXPR:
2466
    case TRUTH_OR_EXPR:
2467
    case TRUTH_XOR_EXPR:
2468
      if (flags & TFF_EXPR_IN_PARENS)
2469
        pp_cxx_left_paren (cxx_pp);
2470
      pp_expression (cxx_pp, t);
2471
      if (flags & TFF_EXPR_IN_PARENS)
2472
        pp_cxx_right_paren (cxx_pp);
2473
      break;
2474
 
2475
    case OBJ_TYPE_REF:
2476
      dump_expr (resolve_virtual_fun_from_obj_type_ref (t), flags);
2477
      break;
2478
 
2479
      /*  This list is incomplete, but should suffice for now.
2480
          It is very important that `sorry' does not call
2481
          `report_error_function'.  That could cause an infinite loop.  */
2482
    default:
2483
      pp_unsupported_tree (cxx_pp, t);
2484
      /* fall through to ERROR_MARK...  */
2485
    case ERROR_MARK:
2486
      pp_string (cxx_pp, M_("<expression error>"));
2487
      break;
2488
    }
2489
}
2490
 
2491
static void
2492
dump_binary_op (const char *opstring, tree t, int flags)
2493
{
2494
  pp_cxx_left_paren (cxx_pp);
2495
  dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2496
  pp_cxx_whitespace (cxx_pp);
2497
  if (opstring)
2498
    pp_cxx_ws_string (cxx_pp, opstring);
2499
  else
2500
    pp_string (cxx_pp, M_("<unknown operator>"));
2501
  pp_cxx_whitespace (cxx_pp);
2502
  dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2503
  pp_cxx_right_paren (cxx_pp);
2504
}
2505
 
2506
static void
2507
dump_unary_op (const char *opstring, tree t, int flags)
2508
{
2509
  if (flags & TFF_EXPR_IN_PARENS)
2510
    pp_cxx_left_paren (cxx_pp);
2511
  pp_cxx_ws_string (cxx_pp, opstring);
2512
  dump_expr (TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2513
  if (flags & TFF_EXPR_IN_PARENS)
2514
    pp_cxx_right_paren (cxx_pp);
2515
}
2516
 
2517
static void
2518
reinit_cxx_pp (void)
2519
{
2520
  pp_clear_output_area (cxx_pp);
2521
  pp_base (cxx_pp)->padding = pp_none;
2522
  pp_indentation (cxx_pp) = 0;
2523
  pp_needs_newline (cxx_pp) = false;
2524
  cxx_pp->enclosing_scope = current_function_decl;
2525
}
2526
 
2527
 
2528
/* Exported interface to stringifying types, exprs and decls under TFF_*
2529
   control.  */
2530
 
2531
const char *
2532
type_as_string (tree typ, int flags)
2533
{
2534
  reinit_cxx_pp ();
2535
  pp_translate_identifiers (cxx_pp) = false;
2536
  dump_type (typ, flags);
2537
  return pp_formatted_text (cxx_pp);
2538
}
2539
 
2540
const char *
2541
type_as_string_translate (tree typ, int flags)
2542
{
2543
  reinit_cxx_pp ();
2544
  dump_type (typ, flags);
2545
  return pp_formatted_text (cxx_pp);
2546
}
2547
 
2548
const char *
2549
expr_as_string (tree decl, int flags)
2550
{
2551
  reinit_cxx_pp ();
2552
  pp_translate_identifiers (cxx_pp) = false;
2553
  dump_expr (decl, flags);
2554
  return pp_formatted_text (cxx_pp);
2555
}
2556
 
2557
const char *
2558
decl_as_string (tree decl, int flags)
2559
{
2560
  reinit_cxx_pp ();
2561
  pp_translate_identifiers (cxx_pp) = false;
2562
  dump_decl (decl, flags);
2563
  return pp_formatted_text (cxx_pp);
2564
}
2565
 
2566
const char *
2567
decl_as_string_translate (tree decl, int flags)
2568
{
2569
  reinit_cxx_pp ();
2570
  dump_decl (decl, flags);
2571
  return pp_formatted_text (cxx_pp);
2572
}
2573
 
2574
/* Generate the three forms of printable names for cxx_printable_name.  */
2575
 
2576
const char *
2577
lang_decl_name (tree decl, int v, bool translate)
2578
{
2579
  if (v >= 2)
2580
    return (translate
2581
            ? decl_as_string_translate (decl, TFF_DECL_SPECIFIERS)
2582
            : decl_as_string (decl, TFF_DECL_SPECIFIERS));
2583
 
2584
  reinit_cxx_pp ();
2585
  pp_translate_identifiers (cxx_pp) = translate;
2586
  if (v == 1
2587
      && (DECL_CLASS_SCOPE_P (decl)
2588
          || (DECL_NAMESPACE_SCOPE_P (decl)
2589
              && CP_DECL_CONTEXT (decl) != global_namespace)))
2590
    {
2591
      dump_type (CP_DECL_CONTEXT (decl), TFF_PLAIN_IDENTIFIER);
2592
      pp_cxx_colon_colon (cxx_pp);
2593
    }
2594
 
2595
  if (TREE_CODE (decl) == FUNCTION_DECL)
2596
    dump_function_name (decl, TFF_PLAIN_IDENTIFIER);
2597
  else
2598
    dump_decl (DECL_NAME (decl), TFF_PLAIN_IDENTIFIER);
2599
 
2600
  return pp_formatted_text (cxx_pp);
2601
}
2602
 
2603
/* Return the location of a tree passed to %+ formats.  */
2604
 
2605
location_t
2606
location_of (tree t)
2607
{
2608
  if (TREE_CODE (t) == PARM_DECL && DECL_CONTEXT (t))
2609
    t = DECL_CONTEXT (t);
2610
  else if (TYPE_P (t))
2611
    {
2612
      t = TYPE_MAIN_DECL (t);
2613
      if (t == NULL_TREE)
2614
        return input_location;
2615
    }
2616
  else if (TREE_CODE (t) == OVERLOAD)
2617
    t = OVL_FUNCTION (t);
2618
 
2619
  if (DECL_P (t))
2620
    return DECL_SOURCE_LOCATION (t);
2621
  return EXPR_LOC_OR_HERE (t);
2622
}
2623
 
2624
/* Now the interfaces from error et al to dump_type et al. Each takes an
2625
   on/off VERBOSE flag and supply the appropriate TFF_ flags to a dump_
2626
   function.  */
2627
 
2628
static const char *
2629
decl_to_string (tree decl, int verbose)
2630
{
2631
  int flags = 0;
2632
 
2633
  if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == RECORD_TYPE
2634
      || TREE_CODE (decl) == UNION_TYPE || TREE_CODE (decl) == ENUMERAL_TYPE)
2635
    flags = TFF_CLASS_KEY_OR_ENUM;
2636
  if (verbose)
2637
    flags |= TFF_DECL_SPECIFIERS;
2638
  else if (TREE_CODE (decl) == FUNCTION_DECL)
2639
    flags |= TFF_DECL_SPECIFIERS | TFF_RETURN_TYPE;
2640
  flags |= TFF_TEMPLATE_HEADER;
2641
 
2642
  reinit_cxx_pp ();
2643
  dump_decl (decl, flags);
2644
  return pp_formatted_text (cxx_pp);
2645
}
2646
 
2647
static const char *
2648
expr_to_string (tree decl)
2649
{
2650
  reinit_cxx_pp ();
2651
  dump_expr (decl, 0);
2652
  return pp_formatted_text (cxx_pp);
2653
}
2654
 
2655
static const char *
2656
fndecl_to_string (tree fndecl, int verbose)
2657
{
2658
  int flags;
2659
 
2660
  flags = TFF_EXCEPTION_SPECIFICATION | TFF_DECL_SPECIFIERS
2661
    | TFF_TEMPLATE_HEADER;
2662
  if (verbose)
2663
    flags |= TFF_FUNCTION_DEFAULT_ARGUMENTS;
2664
  reinit_cxx_pp ();
2665
  dump_decl (fndecl, flags);
2666
  return pp_formatted_text (cxx_pp);
2667
}
2668
 
2669
 
2670
static const char *
2671
code_to_string (enum tree_code c)
2672
{
2673
  return tree_code_name [c];
2674
}
2675
 
2676
const char *
2677
language_to_string (enum languages c)
2678
{
2679
  switch (c)
2680
    {
2681
    case lang_c:
2682
      return "C";
2683
 
2684
    case lang_cplusplus:
2685
      return "C++";
2686
 
2687
    case lang_java:
2688
      return "Java";
2689
 
2690
    default:
2691
      gcc_unreachable ();
2692
    }
2693
  return NULL;
2694
}
2695
 
2696
/* Return the proper printed version of a parameter to a C++ function.  */
2697
 
2698
static const char *
2699
parm_to_string (int p)
2700
{
2701
  reinit_cxx_pp ();
2702
  if (p < 0)
2703
    pp_string (cxx_pp, "'this'");
2704
  else
2705
    pp_decimal_int (cxx_pp, p + 1);
2706
  return pp_formatted_text (cxx_pp);
2707
}
2708
 
2709
static const char *
2710
op_to_string (enum tree_code p)
2711
{
2712
  tree id = operator_name_info[(int) p].identifier;
2713
  return id ? IDENTIFIER_POINTER (id) : M_("<unknown>");
2714
}
2715
 
2716
static const char *
2717
type_to_string (tree typ, int verbose)
2718
{
2719
  int flags = 0;
2720
  if (verbose)
2721
    flags |= TFF_CLASS_KEY_OR_ENUM;
2722
  flags |= TFF_TEMPLATE_HEADER;
2723
 
2724
  reinit_cxx_pp ();
2725
  dump_type (typ, flags);
2726
  /* If we're printing a type that involves typedefs, also print the
2727
     stripped version.  But sometimes the stripped version looks
2728
     exactly the same, so we don't want it after all.  To avoid printing
2729
     it in that case, we play ugly obstack games.  */
2730
  if (typ && TYPE_P (typ) && typ != TYPE_CANONICAL (typ)
2731
      && !uses_template_parms (typ))
2732
    {
2733
      int aka_start; char *p;
2734
      struct obstack *ob = pp_base (cxx_pp)->buffer->obstack;
2735
      /* Remember the end of the initial dump.  */
2736
      int len = obstack_object_size (ob);
2737
      tree aka = strip_typedefs (typ);
2738
      pp_string (cxx_pp, " {aka");
2739
      pp_cxx_whitespace (cxx_pp);
2740
      /* And remember the start of the aka dump.  */
2741
      aka_start = obstack_object_size (ob);
2742
      dump_type (aka, flags);
2743
      pp_character (cxx_pp, '}');
2744
      p = (char*)obstack_base (ob);
2745
      /* If they are identical, cut off the aka with a NUL.  */
2746
      if (memcmp (p, p+aka_start, len) == 0)
2747
        p[len] = '\0';
2748
    }
2749
  return pp_formatted_text (cxx_pp);
2750
}
2751
 
2752
static const char *
2753
assop_to_string (enum tree_code p)
2754
{
2755
  tree id = assignment_operator_name_info[(int) p].identifier;
2756
  return id ? IDENTIFIER_POINTER (id) : M_("{unknown}");
2757
}
2758
 
2759
static const char *
2760
args_to_string (tree p, int verbose)
2761
{
2762
  int flags = 0;
2763
  if (verbose)
2764
    flags |= TFF_CLASS_KEY_OR_ENUM;
2765
 
2766
  if (p == NULL_TREE)
2767
    return "";
2768
 
2769
  if (TYPE_P (TREE_VALUE (p)))
2770
    return type_as_string_translate (p, flags);
2771
 
2772
  reinit_cxx_pp ();
2773
  for (; p; p = TREE_CHAIN (p))
2774
    {
2775
      if (TREE_VALUE (p) == null_node)
2776
        pp_cxx_ws_string (cxx_pp, "NULL");
2777
      else
2778
        dump_type (error_type (TREE_VALUE (p)), flags);
2779
      if (TREE_CHAIN (p))
2780
        pp_separate_with_comma (cxx_pp);
2781
    }
2782
  return pp_formatted_text (cxx_pp);
2783
}
2784
 
2785
/* Pretty-print a deduction substitution (from deduction_tsubst_fntype).  P
2786
   is a TREE_LIST with purpose the TEMPLATE_DECL, value the template
2787
   arguments.  */
2788
 
2789
static const char *
2790
subst_to_string (tree p)
2791
{
2792
  tree decl = TREE_PURPOSE (p);
2793
  tree targs = TREE_VALUE (p);
2794
  tree tparms = DECL_TEMPLATE_PARMS (decl);
2795
  int flags = TFF_DECL_SPECIFIERS|TFF_TEMPLATE_HEADER;
2796
 
2797
  if (p == NULL_TREE)
2798
    return "";
2799
 
2800
  reinit_cxx_pp ();
2801
  dump_template_decl (TREE_PURPOSE (p), flags);
2802
  pp_cxx_whitespace (cxx_pp);
2803
  pp_cxx_left_bracket (cxx_pp);
2804
  pp_cxx_ws_string (cxx_pp, M_("with"));
2805
  pp_cxx_whitespace (cxx_pp);
2806
  dump_template_bindings (tparms, targs, NULL);
2807
  pp_cxx_right_bracket (cxx_pp);
2808
  return pp_formatted_text (cxx_pp);
2809
}
2810
 
2811
static const char *
2812
cv_to_string (tree p, int v)
2813
{
2814
  reinit_cxx_pp ();
2815
  pp_base (cxx_pp)->padding = v ? pp_before : pp_none;
2816
  pp_cxx_cv_qualifier_seq (cxx_pp, p);
2817
  return pp_formatted_text (cxx_pp);
2818
}
2819
 
2820
/* Langhook for print_error_function.  */
2821
void
2822
cxx_print_error_function (diagnostic_context *context, const char *file,
2823
                          diagnostic_info *diagnostic)
2824
{
2825
  lhd_print_error_function (context, file, diagnostic);
2826
  pp_base_set_prefix (context->printer, file);
2827
  maybe_print_instantiation_context (context);
2828
}
2829
 
2830
static void
2831
cp_diagnostic_starter (diagnostic_context *context,
2832
                       diagnostic_info *diagnostic)
2833
{
2834
  diagnostic_report_current_module (context, diagnostic->location);
2835
  cp_print_error_function (context, diagnostic);
2836
  maybe_print_instantiation_context (context);
2837
  maybe_print_constexpr_context (context);
2838
  pp_base_set_prefix (context->printer, diagnostic_build_prefix (context,
2839
                                                                 diagnostic));
2840
}
2841
 
2842
static void
2843
cp_diagnostic_finalizer (diagnostic_context *context,
2844
                         diagnostic_info *diagnostic)
2845
{
2846
  virt_loc_aware_diagnostic_finalizer (context, diagnostic);
2847
  pp_base_destroy_prefix (context->printer);
2848
}
2849
 
2850
/* Print current function onto BUFFER, in the process of reporting
2851
   a diagnostic message.  Called from cp_diagnostic_starter.  */
2852
static void
2853
cp_print_error_function (diagnostic_context *context,
2854
                         diagnostic_info *diagnostic)
2855
{
2856
  /* If we are in an instantiation context, current_function_decl is likely
2857
     to be wrong, so just rely on print_instantiation_full_context.  */
2858
  if (current_instantiation ())
2859
    return;
2860
  if (diagnostic_last_function_changed (context, diagnostic))
2861
    {
2862
      const char *old_prefix = context->printer->prefix;
2863
      const char *file = LOCATION_FILE (diagnostic->location);
2864
      tree abstract_origin = diagnostic_abstract_origin (diagnostic);
2865
      char *new_prefix = (file && abstract_origin == NULL)
2866
                         ? file_name_as_prefix (file) : NULL;
2867
 
2868
      pp_base_set_prefix (context->printer, new_prefix);
2869
 
2870
      if (current_function_decl == NULL)
2871
        pp_base_string (context->printer, _("At global scope:"));
2872
      else
2873
        {
2874
          tree fndecl, ao;
2875
 
2876
          if (abstract_origin)
2877
            {
2878
              ao = BLOCK_ABSTRACT_ORIGIN (abstract_origin);
2879
              while (TREE_CODE (ao) == BLOCK
2880
                     && BLOCK_ABSTRACT_ORIGIN (ao)
2881
                     && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
2882
                ao = BLOCK_ABSTRACT_ORIGIN (ao);
2883
              gcc_assert (TREE_CODE (ao) == FUNCTION_DECL);
2884
              fndecl = ao;
2885
            }
2886
          else
2887
            fndecl = current_function_decl;
2888
 
2889
          pp_printf (context->printer, function_category (fndecl),
2890
                     cxx_printable_name_translate (fndecl, 2));
2891
 
2892
          while (abstract_origin)
2893
            {
2894
              location_t *locus;
2895
              tree block = abstract_origin;
2896
 
2897
              locus = &BLOCK_SOURCE_LOCATION (block);
2898
              fndecl = NULL;
2899
              block = BLOCK_SUPERCONTEXT (block);
2900
              while (block && TREE_CODE (block) == BLOCK
2901
                     && BLOCK_ABSTRACT_ORIGIN (block))
2902
                {
2903
                  ao = BLOCK_ABSTRACT_ORIGIN (block);
2904
 
2905
                  while (TREE_CODE (ao) == BLOCK
2906
                         && BLOCK_ABSTRACT_ORIGIN (ao)
2907
                         && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
2908
                    ao = BLOCK_ABSTRACT_ORIGIN (ao);
2909
 
2910
                  if (TREE_CODE (ao) == FUNCTION_DECL)
2911
                    {
2912
                      fndecl = ao;
2913
                      break;
2914
                    }
2915
                  else if (TREE_CODE (ao) != BLOCK)
2916
                    break;
2917
 
2918
                  block = BLOCK_SUPERCONTEXT (block);
2919
                }
2920
              if (fndecl)
2921
                abstract_origin = block;
2922
              else
2923
                {
2924
                  while (block && TREE_CODE (block) == BLOCK)
2925
                    block = BLOCK_SUPERCONTEXT (block);
2926
 
2927
                  if (block && TREE_CODE (block) == FUNCTION_DECL)
2928
                    fndecl = block;
2929
                  abstract_origin = NULL;
2930
                }
2931
              if (fndecl)
2932
                {
2933
                  expanded_location s = expand_location (*locus);
2934
                  pp_base_character (context->printer, ',');
2935
                  pp_base_newline (context->printer);
2936
                  if (s.file != NULL)
2937
                    {
2938
                      if (context->show_column && s.column != 0)
2939
                        pp_printf (context->printer,
2940
                                   _("    inlined from %qs at %s:%d:%d"),
2941
                                   cxx_printable_name_translate (fndecl, 2),
2942
                                   s.file, s.line, s.column);
2943
                      else
2944
                        pp_printf (context->printer,
2945
                                   _("    inlined from %qs at %s:%d"),
2946
                                   cxx_printable_name_translate (fndecl, 2),
2947
                                   s.file, s.line);
2948
 
2949
                    }
2950
                  else
2951
                    pp_printf (context->printer, _("    inlined from %qs"),
2952
                               cxx_printable_name_translate (fndecl, 2));
2953
                }
2954
            }
2955
          pp_base_character (context->printer, ':');
2956
        }
2957
      pp_base_newline (context->printer);
2958
 
2959
      diagnostic_set_last_function (context, diagnostic);
2960
      pp_base_destroy_prefix (context->printer);
2961
      context->printer->prefix = old_prefix;
2962
    }
2963
}
2964
 
2965
/* Returns a description of FUNCTION using standard terminology.  The
2966
   result is a format string of the form "In CATEGORY %qs".  */
2967
static const char *
2968
function_category (tree fn)
2969
{
2970
  /* We can get called from the middle-end for diagnostics of function
2971
     clones.  Make sure we have language specific information before
2972
     dereferencing it.  */
2973
  if (DECL_LANG_SPECIFIC (STRIP_TEMPLATE (fn))
2974
      && DECL_FUNCTION_MEMBER_P (fn))
2975
    {
2976
      if (DECL_STATIC_FUNCTION_P (fn))
2977
        return _("In static member function %qs");
2978
      else if (DECL_COPY_CONSTRUCTOR_P (fn))
2979
        return _("In copy constructor %qs");
2980
      else if (DECL_CONSTRUCTOR_P (fn))
2981
        return _("In constructor %qs");
2982
      else if (DECL_DESTRUCTOR_P (fn))
2983
        return _("In destructor %qs");
2984
      else if (LAMBDA_FUNCTION_P (fn))
2985
        return _("In lambda function");
2986
      else
2987
        return _("In member function %qs");
2988
    }
2989
  else
2990
    return _("In function %qs");
2991
}
2992
 
2993
/* Report the full context of a current template instantiation,
2994
   onto BUFFER.  */
2995
static void
2996
print_instantiation_full_context (diagnostic_context *context)
2997
{
2998
  struct tinst_level *p = current_instantiation ();
2999
  location_t location = input_location;
3000
 
3001
  if (p)
3002
    {
3003
      pp_verbatim (context->printer,
3004
                   TREE_CODE (p->decl) == TREE_LIST
3005
                   ? _("%s: In substitution of %qS:\n")
3006
                   : _("%s: In instantiation of %q#D:\n"),
3007
                   LOCATION_FILE (location),
3008
                   p->decl);
3009
 
3010
      location = p->locus;
3011
      p = p->next;
3012
    }
3013
 
3014
  print_instantiation_partial_context (context, p, location);
3015
}
3016
 
3017
/* Helper function of print_instantiation_partial_context() that
3018
   prints a single line of instantiation context.  */
3019
 
3020
static void
3021
print_instantiation_partial_context_line (diagnostic_context *context,
3022
                                          const struct tinst_level *t,
3023
                                          location_t loc, bool recursive_p)
3024
{
3025
  expanded_location xloc;
3026
  xloc = expand_location (loc);
3027
 
3028
  if (context->show_column)
3029
    pp_verbatim (context->printer, _("%s:%d:%d:   "),
3030
                 xloc.file, xloc.line, xloc.column);
3031
  else
3032
    pp_verbatim (context->printer, _("%s:%d:   "),
3033
                 xloc.file, xloc.line);
3034
 
3035
  if (t != NULL)
3036
    {
3037
      if (TREE_CODE (t->decl) == TREE_LIST)
3038
        pp_verbatim (context->printer,
3039
                     recursive_p
3040
                     ? _("recursively required by substitution of %qS\n")
3041
                     : _("required by substitution of %qS\n"),
3042
                     t->decl);
3043
      else
3044
        pp_verbatim (context->printer,
3045
                     recursive_p
3046
                     ? _("recursively required from %q#D\n")
3047
                     : _("required from %q#D\n"),
3048
                     t->decl);
3049
    }
3050
  else
3051
    {
3052
      pp_verbatim (context->printer,
3053
                   recursive_p
3054
                   ? _("recursively required from here")
3055
                   : _("required from here"));
3056
    }
3057
}
3058
 
3059
/* Same as print_instantiation_full_context but less verbose.  */
3060
 
3061
static void
3062
print_instantiation_partial_context (diagnostic_context *context,
3063
                                     struct tinst_level *t0, location_t loc)
3064
{
3065
  struct tinst_level *t;
3066
  int n_total = 0;
3067
  int n;
3068
  location_t prev_loc = loc;
3069
 
3070
  for (t = t0; t != NULL; t = t->next)
3071
    if (prev_loc != t->locus)
3072
      {
3073
        prev_loc = t->locus;
3074
        n_total++;
3075
      }
3076
 
3077
  t = t0;
3078
 
3079
  if (n_total >= 12)
3080
    {
3081
      int skip = n_total - 10;
3082
      for (n = 0; n < 5; n++)
3083
        {
3084
          gcc_assert (t != NULL);
3085
          if (loc != t->locus)
3086
            print_instantiation_partial_context_line (context, t, loc,
3087
                                                      /*recursive_p=*/false);
3088
          loc = t->locus;
3089
          t = t->next;
3090
        }
3091
      if (t != NULL && skip > 1)
3092
        {
3093
          expanded_location xloc;
3094
          xloc = expand_location (loc);
3095
          if (context->show_column)
3096
            pp_verbatim (context->printer,
3097
                         _("%s:%d:%d:   [ skipping %d instantiation contexts ]\n"),
3098
                         xloc.file, xloc.line, xloc.column, skip);
3099
          else
3100
            pp_verbatim (context->printer,
3101
                         _("%s:%d:   [ skipping %d instantiation contexts ]\n"),
3102
                         xloc.file, xloc.line, skip);
3103
 
3104
          do {
3105
            loc = t->locus;
3106
            t = t->next;
3107
          } while (t != NULL && --skip > 0);
3108
        }
3109
    }
3110
 
3111
  while (t != NULL)
3112
    {
3113
      while (t->next != NULL && t->locus == t->next->locus)
3114
        {
3115
          loc = t->locus;
3116
          t = t->next;
3117
        }
3118
      print_instantiation_partial_context_line (context, t, loc,
3119
                                                t->locus == loc);
3120
      loc = t->locus;
3121
      t = t->next;
3122
    }
3123
  print_instantiation_partial_context_line (context, NULL, loc,
3124
                                            /*recursive_p=*/false);
3125
  pp_base_newline (context->printer);
3126
}
3127
 
3128
/* Called from cp_thing to print the template context for an error.  */
3129
static void
3130
maybe_print_instantiation_context (diagnostic_context *context)
3131
{
3132
  if (!problematic_instantiation_changed () || current_instantiation () == 0)
3133
    return;
3134
 
3135
  record_last_problematic_instantiation ();
3136
  print_instantiation_full_context (context);
3137
}
3138
 
3139
/* Report the bare minimum context of a template instantiation.  */
3140
void
3141
print_instantiation_context (void)
3142
{
3143
  print_instantiation_partial_context
3144
    (global_dc, current_instantiation (), input_location);
3145
  diagnostic_flush_buffer (global_dc);
3146
}
3147
 
3148
/* Report what constexpr call(s) we're trying to expand, if any.  */
3149
 
3150
void
3151
maybe_print_constexpr_context (diagnostic_context *context)
3152
{
3153
  VEC(tree,heap) *call_stack = cx_error_context ();
3154
  unsigned ix;
3155
  tree t;
3156
 
3157
  FOR_EACH_VEC_ELT (tree, call_stack, ix, t)
3158
    {
3159
      expanded_location xloc = expand_location (EXPR_LOCATION (t));
3160
      const char *s = expr_as_string (t, 0);
3161
      if (context->show_column)
3162
        pp_verbatim (context->printer,
3163
                     _("%s:%d:%d:   in constexpr expansion of %qs"),
3164
                     xloc.file, xloc.line, xloc.column, s);
3165
      else
3166
        pp_verbatim (context->printer,
3167
                     _("%s:%d:   in constexpr expansion of %qs"),
3168
                     xloc.file, xloc.line, s);
3169
      pp_base_newline (context->printer);
3170
    }
3171
}
3172
 
3173
/* Called from output_format -- during diagnostic message processing --
3174
   to handle C++ specific format specifier with the following meanings:
3175
   %A   function argument-list.
3176
   %C   tree code.
3177
   %D   declaration.
3178
   %E   expression.
3179
   %F   function declaration.
3180
   %L   language as used in extern "lang".
3181
   %O   binary operator.
3182
   %P   function parameter whose position is indicated by an integer.
3183
   %Q   assignment operator.
3184
   %T   type.
3185
   %V   cv-qualifier.  */
3186
static bool
3187
cp_printer (pretty_printer *pp, text_info *text, const char *spec,
3188
            int precision, bool wide, bool set_locus, bool verbose)
3189
{
3190
  const char *result;
3191
  tree t = NULL;
3192
#define next_tree    (t = va_arg (*text->args_ptr, tree))
3193
#define next_tcode   ((enum tree_code) va_arg (*text->args_ptr, int))
3194
#define next_lang    ((enum languages) va_arg (*text->args_ptr, int))
3195
#define next_int     va_arg (*text->args_ptr, int)
3196
 
3197
  if (precision != 0 || wide)
3198
    return false;
3199
 
3200
  if (text->locus == NULL)
3201
    set_locus = false;
3202
 
3203
  switch (*spec)
3204
    {
3205
    case 'A': result = args_to_string (next_tree, verbose);     break;
3206
    case 'C': result = code_to_string (next_tcode);             break;
3207
    case 'D':
3208
      {
3209
        tree temp = next_tree;
3210
        if (DECL_P (temp)
3211
            && DECL_DEBUG_EXPR_IS_FROM (temp) && DECL_DEBUG_EXPR (temp))
3212
          {
3213
            temp = DECL_DEBUG_EXPR (temp);
3214
            if (!DECL_P (temp))
3215
              {
3216
                result = expr_to_string (temp);
3217
                break;
3218
              }
3219
          }
3220
        result = decl_to_string (temp, verbose);
3221
      }
3222
      break;
3223
    case 'E': result = expr_to_string (next_tree);              break;
3224
    case 'F': result = fndecl_to_string (next_tree, verbose);   break;
3225
    case 'L': result = language_to_string (next_lang);          break;
3226
    case 'O': result = op_to_string (next_tcode);               break;
3227
    case 'P': result = parm_to_string (next_int);               break;
3228
    case 'Q': result = assop_to_string (next_tcode);            break;
3229
    case 'S': result = subst_to_string (next_tree);             break;
3230
    case 'T': result = type_to_string (next_tree, verbose);     break;
3231
    case 'V': result = cv_to_string (next_tree, verbose);       break;
3232
 
3233
    case 'K':
3234
      percent_K_format (text);
3235
      return true;
3236
 
3237
    default:
3238
      return false;
3239
    }
3240
 
3241
  pp_base_string (pp, result);
3242
  if (set_locus && t != NULL)
3243
    *text->locus = location_of (t);
3244
  return true;
3245
#undef next_tree
3246
#undef next_tcode
3247
#undef next_lang
3248
#undef next_int
3249
}
3250
 
3251
/* Warn about the use of C++0x features when appropriate.  */
3252
void
3253
maybe_warn_cpp0x (cpp0x_warn_str str)
3254
{
3255
  if ((cxx_dialect == cxx98) && !in_system_header)
3256
    /* We really want to suppress this warning in system headers,
3257
       because libstdc++ uses variadic templates even when we aren't
3258
       in C++0x mode. */
3259
    switch (str)
3260
      {
3261
      case CPP0X_INITIALIZER_LISTS:
3262
        pedwarn (input_location, 0,
3263
                 "extended initializer lists "
3264
                 "only available with -std=c++11 or -std=gnu++11");
3265
        break;
3266
      case CPP0X_EXPLICIT_CONVERSION:
3267
        pedwarn (input_location, 0,
3268
                 "explicit conversion operators "
3269
                 "only available with -std=c++11 or -std=gnu++11");
3270
        break;
3271
      case CPP0X_VARIADIC_TEMPLATES:
3272
        pedwarn (input_location, 0,
3273
                 "variadic templates "
3274
                 "only available with -std=c++11 or -std=gnu++11");
3275
        break;
3276
      case CPP0X_LAMBDA_EXPR:
3277
        pedwarn (input_location, 0,
3278
                 "lambda expressions "
3279
                  "only available with -std=c++11 or -std=gnu++11");
3280
        break;
3281
      case CPP0X_AUTO:
3282
        pedwarn (input_location, 0,
3283
                 "C++0x auto only available with -std=c++11 or -std=gnu++11");
3284
        break;
3285
      case CPP0X_SCOPED_ENUMS:
3286
        pedwarn (input_location, 0,
3287
                 "scoped enums only available with -std=c++11 or -std=gnu++11");
3288
        break;
3289
      case CPP0X_DEFAULTED_DELETED:
3290
        pedwarn (input_location, 0,
3291
                 "defaulted and deleted functions "
3292
                 "only available with -std=c++11 or -std=gnu++11");
3293
        break;
3294
      case CPP0X_INLINE_NAMESPACES:
3295
        pedwarn (input_location, OPT_pedantic,
3296
                 "inline namespaces "
3297
                 "only available with -std=c++11 or -std=gnu++11");
3298
        break;
3299
      case CPP0X_OVERRIDE_CONTROLS:
3300
        pedwarn (input_location, 0,
3301
                 "override controls (override/final) "
3302
                 "only available with -std=c++11 or -std=gnu++11");
3303
        break;
3304
      case CPP0X_NSDMI:
3305
        pedwarn (input_location, 0,
3306
                 "non-static data member initializers "
3307
                 "only available with -std=c++11 or -std=gnu++11");
3308
        break;
3309
      case CPP0X_USER_DEFINED_LITERALS:
3310
        pedwarn (input_location, 0,
3311
                 "user-defined literals "
3312
                 "only available with -std=c++11 or -std=gnu++11");
3313
        break;
3314
      case CPP0X_DELEGATING_CTORS:
3315
        pedwarn (input_location, 0,
3316
                 "delegating constructors "
3317
                 "only available with -std=c++11 or -std=gnu++11");
3318
        break;
3319
      default:
3320
        gcc_unreachable ();
3321
      }
3322
}
3323
 
3324
/* Warn about the use of variadic templates when appropriate.  */
3325
void
3326
maybe_warn_variadic_templates (void)
3327
{
3328
  maybe_warn_cpp0x (CPP0X_VARIADIC_TEMPLATES);
3329
}
3330
 
3331
 
3332
/* Issue an ISO C++98 pedantic warning at LOCATION, conditional on
3333
   option OPT with text GMSGID.  Use this function to report
3334
   diagnostics for constructs that are invalid C++98, but valid
3335
   C++0x.  */
3336
bool
3337
pedwarn_cxx98 (location_t location, int opt, const char *gmsgid, ...)
3338
{
3339
  diagnostic_info diagnostic;
3340
  va_list ap;
3341
 
3342
  va_start (ap, gmsgid);
3343
  diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
3344
                       (cxx_dialect == cxx98) ? DK_PEDWARN : DK_WARNING);
3345
  diagnostic.option_index = opt;
3346
  va_end (ap);
3347
  return report_diagnostic (&diagnostic);
3348
}
3349
 
3350
/* Issue a diagnostic that NAME cannot be found in SCOPE.  DECL is what
3351
   we found when we tried to do the lookup.  LOCATION is the location of
3352
   the NAME identifier.  */
3353
 
3354
void
3355
qualified_name_lookup_error (tree scope, tree name,
3356
                             tree decl, location_t location)
3357
{
3358
  if (scope == error_mark_node)
3359
    ; /* We already complained.  */
3360
  else if (TYPE_P (scope))
3361
    {
3362
      if (!COMPLETE_TYPE_P (scope))
3363
        error_at (location, "incomplete type %qT used in nested name specifier",
3364
                  scope);
3365
      else if (TREE_CODE (decl) == TREE_LIST)
3366
        {
3367
          error_at (location, "reference to %<%T::%D%> is ambiguous",
3368
                    scope, name);
3369
          print_candidates (decl);
3370
        }
3371
      else
3372
        error_at (location, "%qD is not a member of %qT", name, scope);
3373
    }
3374
  else if (scope != global_namespace)
3375
    {
3376
      error_at (location, "%qD is not a member of %qD", name, scope);
3377
      suggest_alternatives_for (location, name);
3378
    }
3379
  else
3380
    {
3381
      error_at (location, "%<::%D%> has not been declared", name);
3382
      suggest_alternatives_for (location, name);
3383
    }
3384
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.