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