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