OSDN Git Service

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