OSDN Git Service

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