OSDN Git Service

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