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