OSDN Git Service

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