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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [gcc-4.5.1/] [gcc/] [cp/] [error.c] - Blame information for rev 297

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

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

powered by: WebSVN 2.1.0

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