1/* Subroutines common to both C and C++ pretty-printers.
2   Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3   Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 2, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING.  If not, write to the Free
19Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
2002110-1301, USA.  */
21
22#include "config.h"
23#include "system.h"
24#include "coretypes.h"
25#include "tm.h"
26#include "real.h"
27#include "c-pretty-print.h"
28#include "c-tree.h"
29#include "tree-iterator.h"
30#include "diagnostic.h"
31
32/* The pretty-printer code is primarily designed to closely follow
33   (GNU) C and C++ grammars.  That is to be contrasted with spaghetti
34   codes we used to have in the past.  Following a structured
35   approach (preferably the official grammars) is believed to make it
36   much easier to add extensions and nifty pretty-printing effects that
37   takes expression or declaration contexts into account.  */
38
39
40#define pp_c_maybe_whitespace(PP)            \
41   do {                                      \
42     if (pp_base (PP)->padding == pp_before) \
43       pp_c_whitespace (PP);                 \
44   } while (0)
45
46/* literal  */
47static void pp_c_char (c_pretty_printer *, int);
48
49/* postfix-expression  */
50static void pp_c_initializer_list (c_pretty_printer *, tree);
51static void pp_c_brace_enclosed_initializer_list (c_pretty_printer *, tree);
52
53static void pp_c_multiplicative_expression (c_pretty_printer *, tree);
54static void pp_c_additive_expression (c_pretty_printer *, tree);
55static void pp_c_shift_expression (c_pretty_printer *, tree);
56static void pp_c_relational_expression (c_pretty_printer *, tree);
57static void pp_c_equality_expression (c_pretty_printer *, tree);
58static void pp_c_and_expression (c_pretty_printer *, tree);
59static void pp_c_exclusive_or_expression (c_pretty_printer *, tree);
60static void pp_c_inclusive_or_expression (c_pretty_printer *, tree);
61static void pp_c_logical_and_expression (c_pretty_printer *, tree);
62static void pp_c_conditional_expression (c_pretty_printer *, tree);
63static void pp_c_assignment_expression (c_pretty_printer *, tree);
64
65/* declarations.  */
66
67
68/* Helper functions.  */
69
70void
71pp_c_whitespace (c_pretty_printer *pp)
72{
73  pp_space (pp);
74  pp_base (pp)->padding = pp_none;
75}
76
77void
78pp_c_left_paren (c_pretty_printer *pp)
79{
80  pp_left_paren (pp);
81  pp_base (pp)->padding = pp_none;
82}
83
84void
85pp_c_right_paren (c_pretty_printer *pp)
86{
87  pp_right_paren (pp);
88  pp_base (pp)->padding = pp_none;
89}
90
91void
92pp_c_left_brace (c_pretty_printer *pp)
93{
94  pp_left_brace (pp);
95  pp_base (pp)->padding = pp_none;
96}
97
98void
99pp_c_right_brace (c_pretty_printer *pp)
100{
101  pp_right_brace (pp);
102  pp_base (pp)->padding = pp_none;
103}
104
105void
106pp_c_left_bracket (c_pretty_printer *pp)
107{
108  pp_left_bracket (pp);
109  pp_base (pp)->padding = pp_none;
110}
111
112void
113pp_c_right_bracket (c_pretty_printer *pp)
114{
115  pp_right_bracket (pp);
116  pp_base (pp)->padding = pp_none;
117}
118
119void
120pp_c_dot (c_pretty_printer *pp)
121{
122  pp_dot (pp);
123  pp_base (pp)->padding = pp_none;
124}
125
126void
127pp_c_ampersand (c_pretty_printer *pp)
128{
129  pp_ampersand (pp);
130  pp_base (pp)->padding = pp_none;
131}
132
133void
134pp_c_star (c_pretty_printer *pp)
135{
136  pp_star (pp);
137  pp_base (pp)->padding = pp_none;
138}
139
140void
141pp_c_arrow (c_pretty_printer *pp)
142{
143  pp_arrow (pp);
144  pp_base (pp)->padding = pp_none;
145}
146
147void
148pp_c_semicolon (c_pretty_printer *pp)
149{
150  pp_semicolon (pp);
151  pp_base (pp)->padding = pp_none;
152}
153
154void
155pp_c_complement (c_pretty_printer *pp)
156{
157  pp_complement (pp);
158  pp_base (pp)->padding = pp_none;
159}
160
161void
162pp_c_exclamation (c_pretty_printer *pp)
163{
164  pp_exclamation (pp);
165  pp_base (pp)->padding = pp_none;
166}
167
168/* Print out the external representation of CV-QUALIFIER.  */
169
170static void
171pp_c_cv_qualifier (c_pretty_printer *pp, const char *cv)
172{
173  const char *p = pp_last_position_in_text (pp);
174  /* The C programming language does not have references, but it is much
175     simpler to handle those here rather than going through the same
176     logic in the C++ pretty-printer.  */
177  if (p != NULL && (*p == '*' || *p == '&'))
178    pp_c_whitespace (pp);
179  pp_c_identifier (pp, cv);
180}
181
182/* Pretty-print T using the type-cast notation '( type-name )'.  */
183
184static void
185pp_c_type_cast (c_pretty_printer *pp, tree t)
186{
187  pp_c_left_paren (pp);
188  pp_type_id (pp, t);
189  pp_c_right_paren (pp);
190}
191
192/* We're about to pretty-print a pointer type as indicated by T.
193   Output a whitespace, if needed, preparing for subsequent output.  */
194
195void
196pp_c_space_for_pointer_operator (c_pretty_printer *pp, tree t)
197{
198  if (POINTER_TYPE_P (t))
199    {
200      tree pointee = strip_pointer_operator (TREE_TYPE (t));
201      if (TREE_CODE (pointee) != ARRAY_TYPE
202          && TREE_CODE (pointee) != FUNCTION_TYPE)
203        pp_c_whitespace (pp);
204    }
205}
206
207
208/* Declarations.  */
209
210/* C++ cv-qualifiers are called type-qualifiers in C.  Print out the
211   cv-qualifiers of T.  If T is a declaration then it is the cv-qualifier
212   of its type.  Take care of possible extensions.
213
214   type-qualifier-list:
215       type-qualifier
216       type-qualifier-list type-qualifier
217
218   type-qualifier:
219       const
220       restrict                              -- C99
221       __restrict__                          -- GNU C
222       volatile    */
223
224void
225pp_c_type_qualifier_list (c_pretty_printer *pp, tree t)
226{
227   int qualifiers;
228
229  if (!TYPE_P (t))
230    t = TREE_TYPE (t);
231
232  qualifiers = TYPE_QUALS (t);
233  if (qualifiers & TYPE_QUAL_CONST)
234    pp_c_cv_qualifier (pp, "const");
235  if (qualifiers & TYPE_QUAL_VOLATILE)
236    pp_c_cv_qualifier (pp, "volatile");
237  if (qualifiers & TYPE_QUAL_RESTRICT)
238    pp_c_cv_qualifier (pp, flag_isoc99 ? "restrict" : "__restrict__");
239}
240
241/* pointer:
242      * type-qualifier-list(opt)
243      * type-qualifier-list(opt) pointer  */
244
245static void
246pp_c_pointer (c_pretty_printer *pp, tree t)
247{
248  if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
249    t = TREE_TYPE (t);
250  switch (TREE_CODE (t))
251    {
252    case POINTER_TYPE:
253      /* It is easier to handle C++ reference types here.  */
254    case REFERENCE_TYPE:
255      if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
256        pp_c_pointer (pp, TREE_TYPE (t));
257      if (TREE_CODE (t) == POINTER_TYPE)
258        pp_c_star (pp);
259      else
260        pp_c_ampersand (pp);
261      pp_c_type_qualifier_list (pp, t);
262      break;
263
264      /* ??? This node is now in GENERIC and so shouldn't be here.  But
265	 we'll fix that later.  */
266    case DECL_EXPR:
267      pp_declaration (pp, DECL_EXPR_DECL (t));
268      pp_needs_newline (pp) = true;
269      break;
270
271    default:
272      pp_unsupported_tree (pp, t);
273    }
274}
275
276/* type-specifier:
277      void
278      char
279      short
280      int
281      long
282      float
283      double
284      signed
285      unsigned
286      _Bool                          -- C99
287      _Complex                       -- C99
288      _Imaginary                     -- C99
289      struct-or-union-specifier
290      enum-specifier
291      typedef-name.
292
293  GNU extensions.
294  simple-type-specifier:
295      __complex__
296      __vector__   */
297
298void
299pp_c_type_specifier (c_pretty_printer *pp, tree t)
300{
301  const enum tree_code code = TREE_CODE (t);
302  switch (code)
303    {
304    case ERROR_MARK:
305      pp_c_identifier (pp, "<type-error>");
306      break;
307
308    case IDENTIFIER_NODE:
309      pp_c_tree_decl_identifier (pp, t);
310      break;
311
312    case VOID_TYPE:
313    case BOOLEAN_TYPE:
314    case CHAR_TYPE:
315    case INTEGER_TYPE:
316    case REAL_TYPE:
317      if (TYPE_NAME (t))
318	{
319	  t = TYPE_NAME (t);
320	  pp_c_type_specifier (pp, t);
321	}
322      else
323	{
324	  int prec = TYPE_PRECISION (t);
325	  t = c_common_type_for_mode (TYPE_MODE (t), TYPE_UNSIGNED (t));
326	  if (TYPE_NAME (t))
327	    {
328	      pp_c_type_specifier (pp, t);
329	      if (TYPE_PRECISION (t) != prec)
330		{
331		  pp_string (pp, ":");
332		  pp_decimal_int (pp, prec);
333		}
334	    }
335	  else
336	    {
337	      switch (code)
338		{
339		case INTEGER_TYPE:
340		  pp_string (pp, (TYPE_UNSIGNED (t)
341				  ? "<unnamed-unsigned:"
342				  : "<unnamed-signed:"));
343		  break;
344		case REAL_TYPE:
345		  pp_string (pp, "<unnamed-float:");
346		  break;
347		default:
348		  gcc_unreachable ();
349		}
350	      pp_decimal_int (pp, prec);
351	      pp_string (pp, ">");
352	    }
353	}
354      break;
355
356    case TYPE_DECL:
357      if (DECL_NAME (t))
358	pp_id_expression (pp, t);
359      else
360	pp_c_identifier (pp, "<typedef-error>");
361      break;
362
363    case UNION_TYPE:
364    case RECORD_TYPE:
365    case ENUMERAL_TYPE:
366      if (code == UNION_TYPE)
367	pp_c_identifier (pp, "union");
368      else if (code == RECORD_TYPE)
369	pp_c_identifier (pp, "struct");
370      else if (code == ENUMERAL_TYPE)
371	pp_c_identifier (pp, "enum");
372      else
373	pp_c_identifier (pp, "<tag-error>");
374
375      if (TYPE_NAME (t))
376	pp_id_expression (pp, TYPE_NAME (t));
377      else
378	pp_c_identifier (pp, "<anonymous>");
379      break;
380
381    default:
382      pp_unsupported_tree (pp, t);
383      break;
384    }
385}
386
387/* specifier-qualifier-list:
388      type-specifier specifier-qualifier-list-opt
389      type-qualifier specifier-qualifier-list-opt
390
391
392  Implementation note:  Because of the non-linearities in array or
393  function declarations, this routine prints not just the
394  specifier-qualifier-list of such entities or types of such entities,
395  but also the 'pointer' production part of their declarators.  The
396  remaining part is done by pp_declarator or pp_c_abstract_declarator.  */
397
398void
399pp_c_specifier_qualifier_list (c_pretty_printer *pp, tree t)
400{
401  const enum tree_code code = TREE_CODE (t);
402
403  if (TREE_CODE (t) != POINTER_TYPE)
404    pp_c_type_qualifier_list (pp, t);
405  switch (code)
406    {
407    case REFERENCE_TYPE:
408    case POINTER_TYPE:
409      {
410        /* Get the types-specifier of this type.  */
411        tree pointee = strip_pointer_operator (TREE_TYPE (t));
412        pp_c_specifier_qualifier_list (pp, pointee);
413        if (TREE_CODE (pointee) == ARRAY_TYPE
414            || TREE_CODE (pointee) == FUNCTION_TYPE)
415          {
416            pp_c_whitespace (pp);
417            pp_c_left_paren (pp);
418          }
419	else if (!c_dialect_cxx ())
420	  pp_c_whitespace (pp);
421        pp_ptr_operator (pp, t);
422      }
423      break;
424
425    case FUNCTION_TYPE:
426    case ARRAY_TYPE:
427      pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
428      break;
429
430    case VECTOR_TYPE:
431    case COMPLEX_TYPE:
432      pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
433      if (code == COMPLEX_TYPE)
434        pp_c_identifier (pp, flag_isoc99 ? "_Complex" : "__complex__");
435      else if (code == VECTOR_TYPE)
436        pp_c_identifier (pp, "__vector__");
437      break;
438
439    default:
440      pp_simple_type_specifier (pp, t);
441      break;
442    }
443}
444
445/* parameter-type-list:
446      parameter-list
447      parameter-list , ...
448
449   parameter-list:
450      parameter-declaration
451      parameter-list , parameter-declaration
452
453   parameter-declaration:
454      declaration-specifiers declarator
455      declaration-specifiers abstract-declarator(opt)   */
456
457void
458pp_c_parameter_type_list (c_pretty_printer *pp, tree t)
459{
460  bool want_parm_decl = DECL_P (t) && !(pp->flags & pp_c_flag_abstract);
461  tree parms = want_parm_decl ? DECL_ARGUMENTS (t) :  TYPE_ARG_TYPES (t);
462  pp_c_left_paren (pp);
463  if (parms == void_list_node)
464    pp_c_identifier (pp, "void");
465  else
466    {
467      bool first = true;
468      for ( ; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
469        {
470          if (!first)
471            pp_separate_with (pp, ',');
472          first = false;
473          pp_declaration_specifiers
474            (pp, want_parm_decl ? parms : TREE_VALUE (parms));
475          if (want_parm_decl)
476            pp_declarator (pp, parms);
477          else
478            pp_abstract_declarator (pp, TREE_VALUE (parms));
479        }
480    }
481  pp_c_right_paren (pp);
482}
483
484/* abstract-declarator:
485      pointer
486      pointer(opt) direct-abstract-declarator  */
487
488static void
489pp_c_abstract_declarator (c_pretty_printer *pp, tree t)
490{
491  if (TREE_CODE (t) == POINTER_TYPE)
492    {
493      if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
494          || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
495        pp_c_right_paren (pp);
496      t = TREE_TYPE (t);
497    }
498
499  pp_direct_abstract_declarator (pp, t);
500}
501
502/* direct-abstract-declarator:
503      ( abstract-declarator )
504      direct-abstract-declarator(opt) [ assignment-expression(opt) ]
505      direct-abstract-declarator(opt) [ * ]
506      direct-abstract-declarator(opt) ( parameter-type-list(opt) )  */
507
508void
509pp_c_direct_abstract_declarator (c_pretty_printer *pp, tree t)
510{
511  switch (TREE_CODE (t))
512    {
513    case POINTER_TYPE:
514      pp_abstract_declarator (pp, t);
515      break;
516
517    case FUNCTION_TYPE:
518      pp_c_parameter_type_list (pp, t);
519      pp_direct_abstract_declarator (pp, TREE_TYPE (t));
520      break;
521
522    case ARRAY_TYPE:
523      pp_c_left_bracket (pp);
524      if (TYPE_DOMAIN (t) && TYPE_MAX_VALUE (TYPE_DOMAIN (t)))
525	{
526	  tree maxval = TYPE_MAX_VALUE (TYPE_DOMAIN (t));
527	  tree type = TREE_TYPE (maxval);
528
529	  if (host_integerp (maxval, 0))
530	    pp_wide_integer (pp, tree_low_cst (maxval, 0) + 1);
531	  else
532	    pp_expression (pp, fold_build2 (PLUS_EXPR, type, maxval,
533					    build_int_cst (type, 1)));
534	}
535      pp_c_right_bracket (pp);
536      pp_direct_abstract_declarator (pp, TREE_TYPE (t));
537      break;
538
539    case IDENTIFIER_NODE:
540    case VOID_TYPE:
541    case BOOLEAN_TYPE:
542    case INTEGER_TYPE:
543    case REAL_TYPE:
544    case ENUMERAL_TYPE:
545    case RECORD_TYPE:
546    case UNION_TYPE:
547    case VECTOR_TYPE:
548    case COMPLEX_TYPE:
549    case TYPE_DECL:
550      break;
551
552    default:
553      pp_unsupported_tree (pp, t);
554      break;
555    }
556}
557
558/* type-name:
559      specifier-qualifier-list  abstract-declarator(opt)  */
560
561void
562pp_c_type_id (c_pretty_printer *pp, tree t)
563{
564  pp_c_specifier_qualifier_list (pp, t);
565  pp_abstract_declarator (pp, t);
566}
567
568/* storage-class-specifier:
569      typedef
570      extern
571      static
572      auto
573      register  */
574
575void
576pp_c_storage_class_specifier (c_pretty_printer *pp, tree t)
577{
578  if (TREE_CODE (t) == TYPE_DECL)
579    pp_c_identifier (pp, "typedef");
580  else if (DECL_P (t))
581    {
582      if (DECL_REGISTER (t))
583        pp_c_identifier (pp, "register");
584      else if (TREE_STATIC (t) && TREE_CODE (t) == VAR_DECL)
585        pp_c_identifier (pp, "static");
586    }
587}
588
589/* function-specifier:
590      inline   */
591
592void
593pp_c_function_specifier (c_pretty_printer *pp, tree t)
594{
595  if (TREE_CODE (t) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (t))
596    pp_c_identifier (pp, "inline");
597}
598
599/* declaration-specifiers:
600      storage-class-specifier declaration-specifiers(opt)
601      type-specifier declaration-specifiers(opt)
602      type-qualifier declaration-specifiers(opt)
603      function-specifier declaration-specifiers(opt)  */
604
605void
606pp_c_declaration_specifiers (c_pretty_printer *pp, tree t)
607{
608  pp_storage_class_specifier (pp, t);
609  pp_function_specifier (pp, t);
610  pp_c_specifier_qualifier_list (pp, DECL_P (t) ?  TREE_TYPE (t) : t);
611}
612
613/* direct-declarator
614      identifier
615      ( declarator )
616      direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
617      direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
618      direct-declarator [ type-qualifier-list static assignment-expression ]
619      direct-declarator [ type-qualifier-list * ]
620      direct-declarator ( parameter-type-list )
621      direct-declarator ( identifier-list(opt) )  */
622
623void
624pp_c_direct_declarator (c_pretty_printer *pp, tree t)
625{
626  switch (TREE_CODE (t))
627    {
628    case VAR_DECL:
629    case PARM_DECL:
630    case TYPE_DECL:
631    case FIELD_DECL:
632    case LABEL_DECL:
633      pp_c_space_for_pointer_operator (pp, TREE_TYPE (t));
634      pp_c_tree_decl_identifier (pp, t);
635      break;
636
637    case ARRAY_TYPE:
638    case POINTER_TYPE:
639      pp_abstract_declarator (pp, TREE_TYPE (t));
640      break;
641
642    case FUNCTION_TYPE:
643      pp_parameter_list (pp, t);
644      pp_abstract_declarator (pp, TREE_TYPE (t));
645      break;
646
647    case FUNCTION_DECL:
648      pp_c_space_for_pointer_operator (pp, TREE_TYPE (TREE_TYPE (t)));
649      pp_c_tree_decl_identifier (pp, t);
650      if (pp_c_base (pp)->flags & pp_c_flag_abstract)
651        pp_abstract_declarator (pp, TREE_TYPE (t));
652      else
653        {
654          pp_parameter_list (pp, t);
655          pp_abstract_declarator (pp, TREE_TYPE (TREE_TYPE (t)));
656        }
657      break;
658
659    case INTEGER_TYPE:
660    case REAL_TYPE:
661    case ENUMERAL_TYPE:
662    case UNION_TYPE:
663    case RECORD_TYPE:
664      break;
665
666    default:
667      pp_unsupported_tree (pp, t);
668      break;
669    }
670}
671
672
673/* declarator:
674      pointer(opt)  direct-declarator   */
675
676void
677pp_c_declarator (c_pretty_printer *pp, tree t)
678{
679  switch (TREE_CODE (t))
680    {
681    case INTEGER_TYPE:
682    case REAL_TYPE:
683    case ENUMERAL_TYPE:
684    case UNION_TYPE:
685    case RECORD_TYPE:
686      break;
687
688    case VAR_DECL:
689    case PARM_DECL:
690    case FIELD_DECL:
691    case ARRAY_TYPE:
692    case FUNCTION_TYPE:
693    case FUNCTION_DECL:
694    case TYPE_DECL:
695      pp_direct_declarator (pp, t);
696    break;
697
698
699    default:
700      pp_unsupported_tree (pp, t);
701      break;
702    }
703}
704
705/* declaration:
706      declaration-specifiers init-declarator-list(opt) ;  */
707
708void
709pp_c_declaration (c_pretty_printer *pp, tree t)
710{
711  pp_declaration_specifiers (pp, t);
712  pp_c_init_declarator (pp, t);
713}
714
715/* Pretty-print ATTRIBUTES using GNU C extension syntax.  */
716
717void
718pp_c_attributes (c_pretty_printer *pp, tree attributes)
719{
720  if (attributes == NULL_TREE)
721    return;
722
723  pp_c_identifier (pp, "__attribute__");
724  pp_c_left_paren (pp);
725  pp_c_left_paren (pp);
726  for (; attributes != NULL_TREE; attributes = TREE_CHAIN (attributes))
727    {
728      pp_tree_identifier (pp, TREE_PURPOSE (attributes));
729      if (TREE_VALUE (attributes))
730        pp_c_call_argument_list (pp, TREE_VALUE (attributes));
731
732      if (TREE_CHAIN (attributes))
733	pp_separate_with (pp, ',');
734    }
735  pp_c_right_paren (pp);
736  pp_c_right_paren (pp);
737}
738
739/* function-definition:
740      declaration-specifiers declarator compound-statement  */
741
742void
743pp_c_function_definition (c_pretty_printer *pp, tree t)
744{
745  pp_declaration_specifiers (pp, t);
746  pp_declarator (pp, t);
747  pp_needs_newline (pp) = true;
748  pp_statement (pp, DECL_SAVED_TREE (t));
749  pp_newline (pp);
750  pp_flush (pp);
751}
752
753
754/* Expressions.  */
755
756/* Print out a c-char.  This is called solely for characters which are
757   in the *target* execution character set.  We ought to convert them
758   back to the *host* execution character set before printing, but we
759   have no way to do this at present.  A decent compromise is to print
760   all characters as if they were in the host execution character set,
761   and not attempt to recover any named escape characters, but render
762   all unprintables as octal escapes.  If the host and target character
763   sets are the same, this produces relatively readable output.  If they
764   are not the same, strings may appear as gibberish, but that's okay
765   (in fact, it may well be what the reader wants, e.g. if they are looking
766   to see if conversion to the target character set happened correctly).
767
768   A special case: we need to prefix \, ", and ' with backslashes.  It is
769   correct to do so for the *host*'s \, ", and ', because the rest of the
770   file appears in the host character set.  */
771
772static void
773pp_c_char (c_pretty_printer *pp, int c)
774{
775  if (ISPRINT (c))
776    {
777      switch (c)
778	{
779	case '\\': pp_string (pp, "\\\\"); break;
780	case '\'': pp_string (pp, "\\\'"); break;
781	case '\"': pp_string (pp, "\\\""); break;
782	default:   pp_character (pp, c);
783	}
784    }
785  else
786    pp_scalar (pp, "\\%03o", (unsigned) c);
787}
788
789/* Print out a STRING literal.  */
790
791void
792pp_c_string_literal (c_pretty_printer *pp, tree s)
793{
794  const char *p = TREE_STRING_POINTER (s);
795  int n = TREE_STRING_LENGTH (s) - 1;
796  int i;
797  pp_doublequote (pp);
798  for (i = 0; i < n; ++i)
799    pp_c_char (pp, p[i]);
800  pp_doublequote (pp);
801}
802
803/* Pretty-print an INTEGER literal.  */
804
805static void
806pp_c_integer_constant (c_pretty_printer *pp, tree i)
807{
808  tree type = TREE_TYPE (i);
809
810  if (TREE_INT_CST_HIGH (i) == 0)
811    pp_wide_integer (pp, TREE_INT_CST_LOW (i));
812  else
813    {
814      if (tree_int_cst_sgn (i) < 0)
815        {
816          pp_character (pp, '-');
817          i = build_int_cst_wide (NULL_TREE,
818				  -TREE_INT_CST_LOW (i),
819				  ~TREE_INT_CST_HIGH (i)
820				  + !TREE_INT_CST_LOW (i));
821        }
822      sprintf (pp_buffer (pp)->digit_buffer,
823               HOST_WIDE_INT_PRINT_DOUBLE_HEX,
824               TREE_INT_CST_HIGH (i), TREE_INT_CST_LOW (i));
825      pp_string (pp, pp_buffer (pp)->digit_buffer);
826    }
827  if (TYPE_UNSIGNED (type))
828    pp_character (pp, 'u');
829  if (type == long_integer_type_node || type == long_unsigned_type_node)
830    pp_character (pp, 'l');
831  else if (type == long_long_integer_type_node
832           || type == long_long_unsigned_type_node)
833    pp_string (pp, "ll");
834}
835
836/* Print out a CHARACTER literal.  */
837
838static void
839pp_c_character_constant (c_pretty_printer *pp, tree c)
840{
841  tree type = TREE_TYPE (c);
842  if (type == wchar_type_node)
843    pp_character (pp, 'L');
844  pp_quote (pp);
845  if (host_integerp (c, TYPE_UNSIGNED (type)))
846    pp_c_char (pp, tree_low_cst (c, TYPE_UNSIGNED (type)));
847  else
848    pp_scalar (pp, "\\x%x", (unsigned) TREE_INT_CST_LOW (c));
849  pp_quote (pp);
850}
851
852/* Print out a BOOLEAN literal.  */
853
854static void
855pp_c_bool_constant (c_pretty_printer *pp, tree b)
856{
857  if (b == boolean_false_node)
858    {
859      if (c_dialect_cxx ())
860	pp_c_identifier (pp, "false");
861      else if (flag_isoc99)
862	pp_c_identifier (pp, "_False");
863      else
864	pp_unsupported_tree (pp, b);
865    }
866  else if (b == boolean_true_node)
867    {
868      if (c_dialect_cxx ())
869	pp_c_identifier (pp, "true");
870      else if (flag_isoc99)
871	pp_c_identifier (pp, "_True");
872      else
873	pp_unsupported_tree (pp, b);
874    }
875  else if (TREE_CODE (b) == INTEGER_CST)
876    pp_c_integer_constant (pp, b);
877  else
878    pp_unsupported_tree (pp, b);
879}
880
881/* Attempt to print out an ENUMERATOR.  Return true on success.  Else return
882   false; that means the value was obtained by a cast, in which case
883   print out the type-id part of the cast-expression -- the casted value
884   is then printed by pp_c_integer_literal.  */
885
886static bool
887pp_c_enumeration_constant (c_pretty_printer *pp, tree e)
888{
889  bool value_is_named = true;
890  tree type = TREE_TYPE (e);
891  tree value;
892
893  /* Find the name of this constant.  */
894  for (value = TYPE_VALUES (type);
895       value != NULL_TREE && !tree_int_cst_equal (TREE_VALUE (value), e);
896       value = TREE_CHAIN (value))
897    ;
898
899  if (value != NULL_TREE)
900    pp_id_expression (pp, TREE_PURPOSE (value));
901  else
902    {
903      /* Value must have been cast.  */
904      pp_c_type_cast (pp, type);
905      value_is_named = false;
906    }
907
908  return value_is_named;
909}
910
911/* Print out a REAL value as a decimal-floating-constant.  */
912
913static void
914pp_c_floating_constant (c_pretty_printer *pp, tree r)
915{
916  real_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_REAL_CST (r),
917		   sizeof (pp_buffer (pp)->digit_buffer), 0, 1);
918  pp_string (pp, pp_buffer(pp)->digit_buffer);
919  if (TREE_TYPE (r) == float_type_node)
920    pp_character (pp, 'f');
921  else if (TREE_TYPE (r) == long_double_type_node)
922    pp_character (pp, 'l');
923}
924
925/* Pretty-print a compound literal expression.  GNU extensions include
926   vector constants.  */
927
928static void
929pp_c_compound_literal (c_pretty_printer *pp, tree e)
930{
931  tree type = TREE_TYPE (e);
932  pp_c_type_cast (pp, type);
933
934  switch (TREE_CODE (type))
935    {
936    case RECORD_TYPE:
937    case UNION_TYPE:
938    case ARRAY_TYPE:
939    case VECTOR_TYPE:
940    case COMPLEX_TYPE:
941      pp_c_brace_enclosed_initializer_list (pp, e);
942      break;
943
944    default:
945      pp_unsupported_tree (pp, e);
946      break;
947    }
948}
949
950/* constant:
951      integer-constant
952      floating-constant
953      enumeration-constant
954      character-constant   */
955
956void
957pp_c_constant (c_pretty_printer *pp, tree e)
958{
959  const enum tree_code code = TREE_CODE (e);
960
961  switch (code)
962    {
963    case INTEGER_CST:
964      {
965        tree type = TREE_TYPE (e);
966        if (type == boolean_type_node)
967          pp_c_bool_constant (pp, e);
968        else if (type == char_type_node)
969          pp_c_character_constant (pp, e);
970        else if (TREE_CODE (type) == ENUMERAL_TYPE
971                 && pp_c_enumeration_constant (pp, e))
972          ;
973        else
974          pp_c_integer_constant (pp, e);
975      }
976      break;
977
978    case REAL_CST:
979      pp_c_floating_constant (pp, e);
980      break;
981
982    case STRING_CST:
983      pp_c_string_literal (pp, e);
984      break;
985
986    default:
987      pp_unsupported_tree (pp, e);
988      break;
989    }
990}
991
992/* Pretty-print an IDENTIFIER_NODE, preceded by whitespace is necessary.  */
993
994void
995pp_c_identifier (c_pretty_printer *pp, const char *id)
996{
997  pp_c_maybe_whitespace (pp);
998  pp_identifier (pp, id);
999  pp_base (pp)->padding = pp_before;
1000}
1001
1002/* Pretty-print a C primary-expression.
1003   primary-expression:
1004      identifier
1005      constant
1006      string-literal
1007      ( expression )   */
1008
1009void
1010pp_c_primary_expression (c_pretty_printer *pp, tree e)
1011{
1012  switch (TREE_CODE (e))
1013    {
1014    case VAR_DECL:
1015    case PARM_DECL:
1016    case FIELD_DECL:
1017    case CONST_DECL:
1018    case FUNCTION_DECL:
1019    case LABEL_DECL:
1020      pp_c_tree_decl_identifier (pp, e);
1021      break;
1022
1023    case IDENTIFIER_NODE:
1024      pp_c_tree_identifier (pp, e);
1025      break;
1026
1027    case ERROR_MARK:
1028      pp_c_identifier (pp, "<erroneous-expression>");
1029      break;
1030
1031    case RESULT_DECL:
1032      pp_c_identifier (pp, "<return-value>");
1033      break;
1034
1035    case INTEGER_CST:
1036    case REAL_CST:
1037    case STRING_CST:
1038      pp_c_constant (pp, e);
1039      break;
1040
1041    case TARGET_EXPR:
1042      pp_c_identifier (pp, "__builtin_memcpy");
1043      pp_c_left_paren (pp);
1044      pp_ampersand (pp);
1045      pp_primary_expression (pp, TREE_OPERAND (e, 0));
1046      pp_separate_with (pp, ',');
1047      pp_ampersand (pp);
1048      pp_initializer (pp, TREE_OPERAND (e, 1));
1049      if (TREE_OPERAND (e, 2))
1050	{
1051	  pp_separate_with (pp, ',');
1052	  pp_c_expression (pp, TREE_OPERAND (e, 2));
1053	}
1054      pp_c_right_paren (pp);
1055      break;
1056
1057    default:
1058      /* FIXME:  Make sure we won't get into an infinie loop.  */
1059      pp_c_left_paren (pp);
1060      pp_expression (pp, e);
1061      pp_c_right_paren (pp);
1062      break;
1063    }
1064}
1065
1066/* Print out a C initializer -- also support C compound-literals.
1067   initializer:
1068      assignment-expression:
1069      { initializer-list }
1070      { initializer-list , }   */
1071
1072static void
1073pp_c_initializer (c_pretty_printer *pp, tree e)
1074{
1075  if (TREE_CODE (e) == CONSTRUCTOR)
1076    pp_c_brace_enclosed_initializer_list (pp, e);
1077  else
1078    pp_expression (pp, e);
1079}
1080
1081/* init-declarator:
1082      declarator:
1083      declarator = initializer   */
1084
1085void
1086pp_c_init_declarator (c_pretty_printer *pp, tree t)
1087{
1088  pp_declarator (pp, t);
1089  /* We don't want to output function definitions here.  There are handled
1090     elsewhere (and the syntactic form is bogus anyway).  */
1091  if (TREE_CODE (t) != FUNCTION_DECL && DECL_INITIAL (t))
1092    {
1093      tree init = DECL_INITIAL (t);
1094      /* This C++ bit is handled here because it is easier to do so.
1095         In templates, the C++ parser builds a TREE_LIST for a
1096         direct-initialization; the TREE_PURPOSE is the variable to
1097         initialize and the TREE_VALUE is the initializer.  */
1098      if (TREE_CODE (init) == TREE_LIST)
1099        {
1100          pp_c_left_paren (pp);
1101          pp_expression (pp, TREE_VALUE (init));
1102          pp_right_paren (pp);
1103        }
1104      else
1105        {
1106          pp_space (pp);
1107          pp_equal (pp);
1108          pp_space (pp);
1109          pp_c_initializer (pp, init);
1110        }
1111    }
1112}
1113
1114/* initializer-list:
1115      designation(opt) initializer
1116      initializer-list , designation(opt) initializer
1117
1118   designation:
1119      designator-list =
1120
1121   designator-list:
1122      designator
1123      designator-list designator
1124
1125   designator:
1126      [ constant-expression ]
1127      identifier   */
1128
1129static void
1130pp_c_initializer_list (c_pretty_printer *pp, tree e)
1131{
1132  tree type = TREE_TYPE (e);
1133  const enum tree_code code = TREE_CODE (type);
1134
1135  switch (code)
1136    {
1137    case RECORD_TYPE:
1138    case UNION_TYPE:
1139    case ARRAY_TYPE:
1140      {
1141        tree init = TREE_OPERAND (e, 0);
1142        for (; init != NULL_TREE; init = TREE_CHAIN (init))
1143          {
1144            if (code == RECORD_TYPE || code == UNION_TYPE)
1145              {
1146                pp_c_dot (pp);
1147                pp_c_primary_expression (pp, TREE_PURPOSE (init));
1148              }
1149            else
1150              {
1151                pp_c_left_bracket (pp);
1152                if (TREE_PURPOSE (init))
1153                  pp_c_constant (pp, TREE_PURPOSE (init));
1154                pp_c_right_bracket (pp);
1155              }
1156            pp_c_whitespace (pp);
1157            pp_equal (pp);
1158            pp_c_whitespace (pp);
1159            pp_initializer (pp, TREE_VALUE (init));
1160            if (TREE_CHAIN (init))
1161              pp_separate_with (pp, ',');
1162          }
1163      }
1164      return;
1165
1166    case VECTOR_TYPE:
1167      if (TREE_CODE (e) == VECTOR_CST)
1168        pp_c_expression_list (pp, TREE_VECTOR_CST_ELTS (e));
1169      else if (TREE_CODE (e) == CONSTRUCTOR)
1170        pp_c_constructor_elts (pp, CONSTRUCTOR_ELTS (e));
1171      else
1172        break;
1173      return;
1174
1175    case COMPLEX_TYPE:
1176      if (TREE_CODE (e) == CONSTRUCTOR)
1177	pp_c_constructor_elts (pp, CONSTRUCTOR_ELTS (e));
1178      else if (TREE_CODE (e) == COMPLEX_CST || TREE_CODE (e) == COMPLEX_EXPR)
1179	{
1180	  const bool cst = TREE_CODE (e) == COMPLEX_CST;
1181	  pp_expression (pp, cst ? TREE_REALPART (e) : TREE_OPERAND (e, 0));
1182	  pp_separate_with (pp, ',');
1183	  pp_expression (pp, cst ? TREE_IMAGPART (e) : TREE_OPERAND (e, 1));
1184	}
1185      else
1186	break;
1187      return;
1188
1189    default:
1190      break;
1191    }
1192
1193  pp_unsupported_tree (pp, type);
1194}
1195
1196/* Pretty-print a brace-enclosed initializer-list.  */
1197
1198static void
1199pp_c_brace_enclosed_initializer_list (c_pretty_printer *pp, tree l)
1200{
1201  pp_c_left_brace (pp);
1202  pp_c_initializer_list (pp, l);
1203  pp_c_right_brace (pp);
1204}
1205
1206
1207/*  This is a convenient function, used to bridge gap between C and C++
1208    grammars.
1209
1210    id-expression:
1211       identifier  */
1212
1213void
1214pp_c_id_expression (c_pretty_printer *pp, tree t)
1215{
1216  switch (TREE_CODE (t))
1217    {
1218    case VAR_DECL:
1219    case PARM_DECL:
1220    case CONST_DECL:
1221    case TYPE_DECL:
1222    case FUNCTION_DECL:
1223    case FIELD_DECL:
1224    case LABEL_DECL:
1225      pp_c_tree_decl_identifier (pp, t);
1226      break;
1227
1228    case IDENTIFIER_NODE:
1229      pp_c_tree_identifier (pp, t);
1230      break;
1231
1232    default:
1233      pp_unsupported_tree (pp, t);
1234      break;
1235    }
1236}
1237
1238/* postfix-expression:
1239      primary-expression
1240      postfix-expression [ expression ]
1241      postfix-expression ( argument-expression-list(opt) )
1242      postfix-expression . identifier
1243      postfix-expression -> identifier
1244      postfix-expression ++
1245      postfix-expression --
1246      ( type-name ) { initializer-list }
1247      ( type-name ) { initializer-list , }  */
1248
1249void
1250pp_c_postfix_expression (c_pretty_printer *pp, tree e)
1251{
1252  enum tree_code code = TREE_CODE (e);
1253  switch (code)
1254    {
1255    case POSTINCREMENT_EXPR:
1256    case POSTDECREMENT_EXPR:
1257      pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1258      pp_identifier (pp, code == POSTINCREMENT_EXPR ? "++" : "--");
1259      break;
1260
1261    case ARRAY_REF:
1262      pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1263      pp_c_left_bracket (pp);
1264      pp_expression (pp, TREE_OPERAND (e, 1));
1265      pp_c_right_bracket (pp);
1266      break;
1267
1268    case CALL_EXPR:
1269      pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1270      pp_c_call_argument_list (pp, TREE_OPERAND (e, 1));
1271      break;
1272
1273    case UNORDERED_EXPR:
1274      pp_c_identifier (pp, flag_isoc99
1275			   ? "isunordered"
1276			   : "__builtin_isunordered");
1277      goto two_args_fun;
1278
1279    case ORDERED_EXPR:
1280      pp_c_identifier (pp, flag_isoc99
1281			   ? "!isunordered"
1282			   : "!__builtin_isunordered");
1283      goto two_args_fun;
1284
1285    case UNLT_EXPR:
1286      pp_c_identifier (pp, flag_isoc99
1287			   ? "!isgreaterequal"
1288			   : "!__builtin_isgreaterequal");
1289      goto two_args_fun;
1290
1291    case UNLE_EXPR:
1292      pp_c_identifier (pp, flag_isoc99
1293			   ? "!isgreater"
1294			   : "!__builtin_isgreater");
1295      goto two_args_fun;
1296
1297    case UNGT_EXPR:
1298      pp_c_identifier (pp, flag_isoc99
1299			   ? "!islessequal"
1300			   : "!__builtin_islessequal");
1301      goto two_args_fun;
1302
1303    case UNGE_EXPR:
1304      pp_c_identifier (pp, flag_isoc99
1305			   ? "!isless"
1306			   : "!__builtin_isless");
1307      goto two_args_fun;
1308
1309    case UNEQ_EXPR:
1310      pp_c_identifier (pp, flag_isoc99
1311			   ? "!islessgreater"
1312			   : "!__builtin_islessgreater");
1313      goto two_args_fun;
1314
1315    case LTGT_EXPR:
1316      pp_c_identifier (pp, flag_isoc99
1317			   ? "islessgreater"
1318			   : "__builtin_islessgreater");
1319      goto two_args_fun;
1320
1321    two_args_fun:
1322      pp_c_left_paren (pp);
1323      pp_expression (pp, TREE_OPERAND (e, 0));
1324      pp_separate_with (pp, ',');
1325      pp_expression (pp, TREE_OPERAND (e, 1));
1326      pp_c_right_paren (pp);
1327      break;
1328
1329    case ABS_EXPR:
1330      pp_c_identifier (pp, "__builtin_abs");
1331      pp_c_left_paren (pp);
1332      pp_expression (pp, TREE_OPERAND (e, 0));
1333      pp_c_right_paren (pp);
1334      break;
1335
1336    case COMPONENT_REF:
1337      {
1338	tree object = TREE_OPERAND (e, 0);
1339	if (TREE_CODE (object) == INDIRECT_REF)
1340	  {
1341	    pp_postfix_expression (pp, TREE_OPERAND (object, 0));
1342	    pp_c_arrow (pp);
1343	  }
1344	else
1345	  {
1346	    pp_postfix_expression (pp, object);
1347	    pp_c_dot (pp);
1348	  }
1349	pp_expression (pp, TREE_OPERAND (e, 1));
1350      }
1351      break;
1352
1353    case COMPLEX_CST:
1354    case VECTOR_CST:
1355    case COMPLEX_EXPR:
1356      pp_c_compound_literal (pp, e);
1357      break;
1358
1359    case COMPOUND_LITERAL_EXPR:
1360      e = DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e));
1361      /* Fall through.  */
1362    case CONSTRUCTOR:
1363      pp_initializer (pp, e);
1364      break;
1365
1366    case VA_ARG_EXPR:
1367      pp_c_identifier (pp, "__builtin_va_arg");
1368      pp_c_left_paren (pp);
1369      pp_assignment_expression (pp, TREE_OPERAND (e, 0));
1370      pp_separate_with (pp, ',');
1371      pp_type_id (pp, TREE_TYPE (e));
1372      pp_c_right_paren (pp);
1373      break;
1374
1375    case ADDR_EXPR:
1376      if (TREE_CODE (TREE_OPERAND (e, 0)) == FUNCTION_DECL)
1377        {
1378          pp_c_id_expression (pp, TREE_OPERAND (e, 0));
1379          break;
1380        }
1381      /* else fall through.  */
1382
1383    default:
1384      pp_primary_expression (pp, e);
1385      break;
1386    }
1387}
1388
1389/* Print out an expression-list; E is expected to be a TREE_LIST.  */
1390
1391void
1392pp_c_expression_list (c_pretty_printer *pp, tree e)
1393{
1394  for (; e != NULL_TREE; e = TREE_CHAIN (e))
1395    {
1396      pp_expression (pp, TREE_VALUE (e));
1397      if (TREE_CHAIN (e))
1398	pp_separate_with (pp, ',');
1399    }
1400}
1401
1402/* Print out V, which contains the elements of a constructor.  */
1403
1404void
1405pp_c_constructor_elts (c_pretty_printer *pp, VEC(constructor_elt,gc) *v)
1406{
1407  unsigned HOST_WIDE_INT ix;
1408  tree value;
1409
1410  FOR_EACH_CONSTRUCTOR_VALUE (v, ix, value)
1411    {
1412      pp_expression (pp, value);
1413      if (ix != VEC_length (constructor_elt, v) - 1)
1414	pp_separate_with (pp, ',');
1415    }
1416}
1417
1418/* Print out an expression-list in parens, as in a function call.  */
1419
1420void
1421pp_c_call_argument_list (c_pretty_printer *pp, tree t)
1422{
1423  pp_c_left_paren (pp);
1424  if (t && TREE_CODE (t) == TREE_LIST)
1425    pp_c_expression_list (pp, t);
1426  pp_c_right_paren (pp);
1427}
1428
1429/* unary-expression:
1430      postfix-expression
1431      ++ cast-expression
1432      -- cast-expression
1433      unary-operator cast-expression
1434      sizeof unary-expression
1435      sizeof ( type-id )
1436
1437  unary-operator: one of
1438      * &  + - ! ~
1439
1440   GNU extensions.
1441   unary-expression:
1442      __alignof__ unary-expression
1443      __alignof__ ( type-id )
1444      __real__ unary-expression
1445      __imag__ unary-expression  */
1446
1447void
1448pp_c_unary_expression (c_pretty_printer *pp, tree e)
1449{
1450  enum tree_code code = TREE_CODE (e);
1451  switch (code)
1452    {
1453    case PREINCREMENT_EXPR:
1454    case PREDECREMENT_EXPR:
1455      pp_identifier (pp, code == PREINCREMENT_EXPR ? "++" : "--");
1456      pp_c_unary_expression (pp, TREE_OPERAND (e, 0));
1457      break;
1458
1459    case ADDR_EXPR:
1460    case INDIRECT_REF:
1461    case NEGATE_EXPR:
1462    case BIT_NOT_EXPR:
1463    case TRUTH_NOT_EXPR:
1464    case CONJ_EXPR:
1465      /* String literal are used by address.  */
1466      if (code == ADDR_EXPR && TREE_CODE (TREE_OPERAND (e, 0)) != STRING_CST)
1467	pp_ampersand (pp);
1468      else if (code == INDIRECT_REF)
1469	pp_c_star (pp);
1470      else if (code == NEGATE_EXPR)
1471	pp_minus (pp);
1472      else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
1473	pp_complement (pp);
1474      else if (code == TRUTH_NOT_EXPR)
1475	pp_exclamation (pp);
1476      pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1477      break;
1478
1479    case REALPART_EXPR:
1480    case IMAGPART_EXPR:
1481      pp_c_identifier (pp, code == REALPART_EXPR ? "__real__" : "__imag__");
1482      pp_c_whitespace (pp);
1483      pp_unary_expression (pp, TREE_OPERAND (e, 0));
1484      break;
1485
1486    default:
1487      pp_postfix_expression (pp, e);
1488      break;
1489    }
1490}
1491
1492/* cast-expression:
1493      unary-expression
1494      ( type-name ) cast-expression  */
1495
1496void
1497pp_c_cast_expression (c_pretty_printer *pp, tree e)
1498{
1499  switch (TREE_CODE (e))
1500    {
1501    case FLOAT_EXPR:
1502    case FIX_TRUNC_EXPR:
1503    case CONVERT_EXPR:
1504      pp_c_type_cast (pp, TREE_TYPE (e));
1505      pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1506      break;
1507
1508    default:
1509      pp_unary_expression (pp, e);
1510    }
1511}
1512
1513/* multiplicative-expression:
1514      cast-expression
1515      multiplicative-expression * cast-expression
1516      multiplicative-expression / cast-expression
1517      multiplicative-expression % cast-expression   */
1518
1519static void
1520pp_c_multiplicative_expression (c_pretty_printer *pp, tree e)
1521{
1522  enum tree_code code = TREE_CODE (e);
1523  switch (code)
1524    {
1525    case MULT_EXPR:
1526    case TRUNC_DIV_EXPR:
1527    case TRUNC_MOD_EXPR:
1528      pp_multiplicative_expression (pp, TREE_OPERAND (e, 0));
1529      pp_c_whitespace (pp);
1530      if (code == MULT_EXPR)
1531	pp_c_star (pp);
1532      else if (code == TRUNC_DIV_EXPR)
1533	pp_slash (pp);
1534      else
1535	pp_modulo (pp);
1536      pp_c_whitespace (pp);
1537      pp_c_cast_expression (pp, TREE_OPERAND (e, 1));
1538      break;
1539
1540    default:
1541      pp_c_cast_expression (pp, e);
1542      break;
1543    }
1544}
1545
1546/* additive-expression:
1547      multiplicative-expression
1548      additive-expression + multiplicative-expression
1549      additive-expression - multiplicative-expression   */
1550
1551static void
1552pp_c_additive_expression (c_pretty_printer *pp, tree e)
1553{
1554  enum tree_code code = TREE_CODE (e);
1555  switch (code)
1556    {
1557    case PLUS_EXPR:
1558    case MINUS_EXPR:
1559      pp_c_additive_expression (pp, TREE_OPERAND (e, 0));
1560      pp_c_whitespace (pp);
1561      if (code == PLUS_EXPR)
1562	pp_plus (pp);
1563      else
1564	pp_minus (pp);
1565      pp_c_whitespace (pp);
1566      pp_multiplicative_expression (pp, TREE_OPERAND (e, 1));
1567      break;
1568
1569    default:
1570      pp_multiplicative_expression (pp, e);
1571      break;
1572    }
1573}
1574
1575/* additive-expression:
1576      additive-expression
1577      shift-expression << additive-expression
1578      shift-expression >> additive-expression   */
1579
1580static void
1581pp_c_shift_expression (c_pretty_printer *pp, tree e)
1582{
1583  enum tree_code code = TREE_CODE (e);
1584  switch (code)
1585    {
1586    case LSHIFT_EXPR:
1587    case RSHIFT_EXPR:
1588      pp_c_shift_expression (pp, TREE_OPERAND (e, 0));
1589      pp_c_whitespace (pp);
1590      pp_identifier (pp, code == LSHIFT_EXPR ? "<<" : ">>");
1591      pp_c_whitespace (pp);
1592      pp_c_additive_expression (pp, TREE_OPERAND (e, 1));
1593      break;
1594
1595    default:
1596      pp_c_additive_expression (pp, e);
1597    }
1598}
1599
1600/* relational-expression:
1601      shift-expression
1602      relational-expression < shift-expression
1603      relational-expression > shift-expression
1604      relational-expression <= shift-expression
1605      relational-expression >= shift-expression   */
1606
1607static void
1608pp_c_relational_expression (c_pretty_printer *pp, tree e)
1609{
1610  enum tree_code code = TREE_CODE (e);
1611  switch (code)
1612    {
1613    case LT_EXPR:
1614    case GT_EXPR:
1615    case LE_EXPR:
1616    case GE_EXPR:
1617      pp_c_relational_expression (pp, TREE_OPERAND (e, 0));
1618      pp_c_whitespace (pp);
1619      if (code == LT_EXPR)
1620	pp_less (pp);
1621      else if (code == GT_EXPR)
1622	pp_greater (pp);
1623      else if (code == LE_EXPR)
1624	pp_identifier (pp, "<=");
1625      else if (code == GE_EXPR)
1626	pp_identifier (pp, ">=");
1627      pp_c_whitespace (pp);
1628      pp_c_shift_expression (pp, TREE_OPERAND (e, 1));
1629      break;
1630
1631    default:
1632      pp_c_shift_expression (pp, e);
1633      break;
1634    }
1635}
1636
1637/* equality-expression:
1638      relational-expression
1639      equality-expression == relational-expression
1640      equality-equality != relational-expression  */
1641
1642static void
1643pp_c_equality_expression (c_pretty_printer *pp, tree e)
1644{
1645  enum tree_code code = TREE_CODE (e);
1646  switch (code)
1647    {
1648    case EQ_EXPR:
1649    case NE_EXPR:
1650      pp_c_equality_expression (pp, TREE_OPERAND (e, 0));
1651      pp_c_whitespace (pp);
1652      pp_identifier (pp, code == EQ_EXPR ? "==" : "!=");
1653      pp_c_whitespace (pp);
1654      pp_c_relational_expression (pp, TREE_OPERAND (e, 1));
1655      break;
1656
1657    default:
1658      pp_c_relational_expression (pp, e);
1659      break;
1660    }
1661}
1662
1663/* AND-expression:
1664      equality-expression
1665      AND-expression & equality-equality   */
1666
1667static void
1668pp_c_and_expression (c_pretty_printer *pp, tree e)
1669{
1670  if (TREE_CODE (e) == BIT_AND_EXPR)
1671    {
1672      pp_c_and_expression (pp, TREE_OPERAND (e, 0));
1673      pp_c_whitespace (pp);
1674      pp_ampersand (pp);
1675      pp_c_whitespace (pp);
1676      pp_c_equality_expression (pp, TREE_OPERAND (e, 1));
1677    }
1678  else
1679    pp_c_equality_expression (pp, e);
1680}
1681
1682/* exclusive-OR-expression:
1683     AND-expression
1684     exclusive-OR-expression ^ AND-expression  */
1685
1686static void
1687pp_c_exclusive_or_expression (c_pretty_printer *pp, tree e)
1688{
1689  if (TREE_CODE (e) == BIT_XOR_EXPR)
1690    {
1691      pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1692      pp_c_maybe_whitespace (pp);
1693      pp_carret (pp);
1694      pp_c_whitespace (pp);
1695      pp_c_and_expression (pp, TREE_OPERAND (e, 1));
1696    }
1697  else
1698    pp_c_and_expression (pp, e);
1699}
1700
1701/* inclusive-OR-expression:
1702     exclusive-OR-expression
1703     inclusive-OR-expression | exclusive-OR-expression  */
1704
1705static void
1706pp_c_inclusive_or_expression (c_pretty_printer *pp, tree e)
1707{
1708  if (TREE_CODE (e) == BIT_IOR_EXPR)
1709    {
1710      pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1711      pp_c_whitespace (pp);
1712      pp_bar (pp);
1713      pp_c_whitespace (pp);
1714      pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 1));
1715    }
1716  else
1717    pp_c_exclusive_or_expression (pp, e);
1718}
1719
1720/* logical-AND-expression:
1721      inclusive-OR-expression
1722      logical-AND-expression && inclusive-OR-expression  */
1723
1724static void
1725pp_c_logical_and_expression (c_pretty_printer *pp, tree e)
1726{
1727  if (TREE_CODE (e) == TRUTH_ANDIF_EXPR)
1728    {
1729      pp_c_logical_and_expression (pp, TREE_OPERAND (e, 0));
1730      pp_c_whitespace (pp);
1731      pp_identifier (pp, "&&");
1732      pp_c_whitespace (pp);
1733      pp_c_inclusive_or_expression (pp, TREE_OPERAND (e, 1));
1734    }
1735  else
1736    pp_c_inclusive_or_expression (pp, e);
1737}
1738
1739/* logical-OR-expression:
1740      logical-AND-expression
1741      logical-OR-expression || logical-AND-expression  */
1742
1743void
1744pp_c_logical_or_expression (c_pretty_printer *pp, tree e)
1745{
1746  if (TREE_CODE (e) == TRUTH_ORIF_EXPR)
1747    {
1748      pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
1749      pp_c_whitespace (pp);
1750      pp_identifier (pp, "||");
1751      pp_c_whitespace (pp);
1752      pp_c_logical_and_expression (pp, TREE_OPERAND (e, 1));
1753    }
1754  else
1755    pp_c_logical_and_expression (pp, e);
1756}
1757
1758/* conditional-expression:
1759      logical-OR-expression
1760      logical-OR-expression ? expression : conditional-expression  */
1761
1762static void
1763pp_c_conditional_expression (c_pretty_printer *pp, tree e)
1764{
1765  if (TREE_CODE (e) == COND_EXPR)
1766    {
1767      pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
1768      pp_c_whitespace (pp);
1769      pp_question (pp);
1770      pp_c_whitespace (pp);
1771      pp_expression (pp, TREE_OPERAND (e, 1));
1772      pp_c_whitespace (pp);
1773      pp_colon (pp);
1774      pp_c_whitespace (pp);
1775      pp_c_conditional_expression (pp, TREE_OPERAND (e, 2));
1776    }
1777  else
1778    pp_c_logical_or_expression (pp, e);
1779}
1780
1781
1782/* assignment-expression:
1783      conditional-expression
1784      unary-expression assignment-operator  assignment-expression
1785
1786   assignment-expression: one of
1787      =    *=    /=    %=    +=    -=    >>=    <<=    &=    ^=    |=  */
1788
1789static void
1790pp_c_assignment_expression (c_pretty_printer *pp, tree e)
1791{
1792  if (TREE_CODE (e) == MODIFY_EXPR || TREE_CODE (e) == INIT_EXPR)
1793    {
1794      pp_c_unary_expression (pp, TREE_OPERAND (e, 0));
1795      pp_c_whitespace (pp);
1796      pp_equal (pp);
1797      pp_space (pp);
1798      pp_c_expression (pp, TREE_OPERAND (e, 1));
1799    }
1800  else
1801    pp_c_conditional_expression (pp, e);
1802}
1803
1804/* expression:
1805       assignment-expression
1806       expression , assignment-expression
1807
1808  Implementation note:  instead of going through the usual recursion
1809  chain, I take the liberty of dispatching nodes to the appropriate
1810  functions.  This makes some redundancy, but it worths it. That also
1811  prevents a possible infinite recursion between pp_c_primary_expression ()
1812  and pp_c_expression ().  */
1813
1814void
1815pp_c_expression (c_pretty_printer *pp, tree e)
1816{
1817  switch (TREE_CODE (e))
1818    {
1819    case INTEGER_CST:
1820      pp_c_integer_constant (pp, e);
1821      break;
1822
1823    case REAL_CST:
1824      pp_c_floating_constant (pp, e);
1825      break;
1826
1827    case STRING_CST:
1828      pp_c_string_literal (pp, e);
1829      break;
1830
1831    case IDENTIFIER_NODE:
1832    case FUNCTION_DECL:
1833    case VAR_DECL:
1834    case CONST_DECL:
1835    case PARM_DECL:
1836    case RESULT_DECL:
1837    case FIELD_DECL:
1838    case LABEL_DECL:
1839    case ERROR_MARK:
1840      pp_primary_expression (pp, e);
1841      break;
1842
1843    case POSTINCREMENT_EXPR:
1844    case POSTDECREMENT_EXPR:
1845    case ARRAY_REF:
1846    case CALL_EXPR:
1847    case COMPONENT_REF:
1848    case COMPLEX_CST:
1849    case COMPLEX_EXPR:
1850    case VECTOR_CST:
1851    case ORDERED_EXPR:
1852    case UNORDERED_EXPR:
1853    case LTGT_EXPR:
1854    case UNEQ_EXPR:
1855    case UNLE_EXPR:
1856    case UNLT_EXPR:
1857    case UNGE_EXPR:
1858    case UNGT_EXPR:
1859    case ABS_EXPR:
1860    case CONSTRUCTOR:
1861    case COMPOUND_LITERAL_EXPR:
1862    case VA_ARG_EXPR:
1863      pp_postfix_expression (pp, e);
1864      break;
1865
1866    case CONJ_EXPR:
1867    case ADDR_EXPR:
1868    case INDIRECT_REF:
1869    case NEGATE_EXPR:
1870    case BIT_NOT_EXPR:
1871    case TRUTH_NOT_EXPR:
1872    case PREINCREMENT_EXPR:
1873    case PREDECREMENT_EXPR:
1874    case REALPART_EXPR:
1875    case IMAGPART_EXPR:
1876      pp_c_unary_expression (pp, e);
1877      break;
1878
1879    case FLOAT_EXPR:
1880    case FIX_TRUNC_EXPR:
1881    case CONVERT_EXPR:
1882      pp_c_cast_expression (pp, e);
1883      break;
1884
1885    case MULT_EXPR:
1886    case TRUNC_MOD_EXPR:
1887    case TRUNC_DIV_EXPR:
1888      pp_multiplicative_expression (pp, e);
1889      break;
1890
1891    case LSHIFT_EXPR:
1892    case RSHIFT_EXPR:
1893      pp_c_shift_expression (pp, e);
1894      break;
1895
1896    case LT_EXPR:
1897    case GT_EXPR:
1898    case LE_EXPR:
1899    case GE_EXPR:
1900      pp_c_relational_expression (pp, e);
1901      break;
1902
1903    case BIT_AND_EXPR:
1904      pp_c_and_expression (pp, e);
1905      break;
1906
1907    case BIT_XOR_EXPR:
1908      pp_c_exclusive_or_expression (pp, e);
1909      break;
1910
1911    case BIT_IOR_EXPR:
1912      pp_c_inclusive_or_expression (pp, e);
1913      break;
1914
1915    case TRUTH_ANDIF_EXPR:
1916      pp_c_logical_and_expression (pp, e);
1917      break;
1918
1919    case TRUTH_ORIF_EXPR:
1920      pp_c_logical_or_expression (pp, e);
1921      break;
1922
1923    case EQ_EXPR:
1924    case NE_EXPR:
1925      pp_c_equality_expression (pp, e);
1926      break;
1927
1928    case COND_EXPR:
1929      pp_conditional_expression (pp, e);
1930      break;
1931
1932    case PLUS_EXPR:
1933    case MINUS_EXPR:
1934      pp_c_additive_expression (pp, e);
1935      break;
1936
1937    case MODIFY_EXPR:
1938    case INIT_EXPR:
1939      pp_assignment_expression (pp, e);
1940      break;
1941
1942    case COMPOUND_EXPR:
1943      pp_c_left_paren (pp);
1944      pp_expression (pp, TREE_OPERAND (e, 0));
1945      pp_separate_with (pp, ',');
1946      pp_assignment_expression (pp, TREE_OPERAND (e, 1));
1947      pp_c_right_paren (pp);
1948      break;
1949
1950    case NOP_EXPR:
1951    case NON_LVALUE_EXPR:
1952    case SAVE_EXPR:
1953      pp_expression (pp, TREE_OPERAND (e, 0));
1954      break;
1955
1956    case TARGET_EXPR:
1957      pp_postfix_expression (pp, TREE_OPERAND (e, 1));
1958      break;
1959
1960    default:
1961      pp_unsupported_tree (pp, e);
1962      break;
1963    }
1964}
1965
1966
1967
1968/* Statements.  */
1969
1970void
1971pp_c_statement (c_pretty_printer *pp, tree stmt)
1972{
1973  if (stmt == NULL)
1974    return;
1975
1976  if (pp_needs_newline (pp))
1977    pp_newline_and_indent (pp, 0);
1978
1979  dump_generic_node (pp_base (pp), stmt, pp_indentation (pp), 0, true);
1980}
1981
1982
1983/* Initialize the PRETTY-PRINTER for handling C codes.  */
1984
1985void
1986pp_c_pretty_printer_init (c_pretty_printer *pp)
1987{
1988  pp->offset_list               = 0;
1989
1990  pp->declaration               = pp_c_declaration;
1991  pp->declaration_specifiers    = pp_c_declaration_specifiers;
1992  pp->declarator                = pp_c_declarator;
1993  pp->direct_declarator         = pp_c_direct_declarator;
1994  pp->type_specifier_seq        = pp_c_specifier_qualifier_list;
1995  pp->abstract_declarator       = pp_c_abstract_declarator;
1996  pp->direct_abstract_declarator = pp_c_direct_abstract_declarator;
1997  pp->ptr_operator              = pp_c_pointer;
1998  pp->parameter_list            = pp_c_parameter_type_list;
1999  pp->type_id                   = pp_c_type_id;
2000  pp->simple_type_specifier     = pp_c_type_specifier;
2001  pp->function_specifier        = pp_c_function_specifier;
2002  pp->storage_class_specifier   = pp_c_storage_class_specifier;
2003
2004  pp->statement                 = pp_c_statement;
2005
2006  pp->id_expression             = pp_c_id_expression;
2007  pp->primary_expression        = pp_c_primary_expression;
2008  pp->postfix_expression        = pp_c_postfix_expression;
2009  pp->unary_expression          = pp_c_unary_expression;
2010  pp->initializer               = pp_c_initializer;
2011  pp->multiplicative_expression = pp_c_multiplicative_expression;
2012  pp->conditional_expression    = pp_c_conditional_expression;
2013  pp->assignment_expression     = pp_c_assignment_expression;
2014  pp->expression                = pp_c_expression;
2015}
2016
2017
2018/* Print the tree T in full, on file FILE.  */
2019
2020void
2021print_c_tree (FILE *file, tree t)
2022{
2023  static c_pretty_printer pp_rec;
2024  static bool initialized = 0;
2025  c_pretty_printer *pp = &pp_rec;
2026
2027  if (!initialized)
2028    {
2029      initialized = 1;
2030      pp_construct (pp_base (pp), NULL, 0);
2031      pp_c_pretty_printer_init (pp);
2032      pp_needs_newline (pp) = true;
2033    }
2034  pp_base (pp)->buffer->stream = file;
2035
2036  pp_statement (pp, t);
2037
2038  pp_newline (pp);
2039  pp_flush (pp);
2040}
2041
2042/* Print the tree T in full, on stderr.  */
2043
2044void
2045debug_c_tree (tree t)
2046{
2047  print_c_tree (stderr, t);
2048  fputc ('\n', stderr);
2049}
2050
2051/* Output the DECL_NAME of T.  If T has no DECL_NAME, output a string made
2052   up of T's memory address.  */
2053
2054void
2055pp_c_tree_decl_identifier (c_pretty_printer *pp, tree t)
2056{
2057  const char *name;
2058
2059  gcc_assert (DECL_P (t));
2060
2061  if (DECL_NAME (t))
2062    name = IDENTIFIER_POINTER (DECL_NAME (t));
2063  else
2064    {
2065      static char xname[8];
2066      sprintf (xname, "<U%4x>", ((unsigned)((unsigned long)(t) & 0xffff)));
2067      name = xname;
2068    }
2069
2070  pp_c_identifier (pp, name);
2071}
2072