OSDN Git Service

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