OSDN Git Service

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