OSDN Git Service

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