OSDN Git Service

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