OSDN Git Service

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