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