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