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