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