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