]>
Commit | Line | Data |
---|---|---|
e1a4dd13 | 1 | /* Implementation of subroutines for the GNU C++ pretty-printer. |
2f990c80 | 2 | Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc. |
e1a4dd13 GDR |
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 2, 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 COPYING. If not, write to the Free | |
1788952f KC |
19 | Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA |
20 | 02110-1301, USA. */ | |
e1a4dd13 GDR |
21 | |
22 | #include "config.h" | |
23 | #include "system.h" | |
24 | #include "coretypes.h" | |
25 | #include "tm.h" | |
26 | #include "real.h" | |
27 | #include "cxx-pretty-print.h" | |
28 | #include "cp-tree.h" | |
12ea3302 | 29 | #include "toplev.h" |
4b780675 GDR |
30 | |
31 | static void pp_cxx_unqualified_id (cxx_pretty_printer *, tree); | |
12ea3302 | 32 | static void pp_cxx_nested_name_specifier (cxx_pretty_printer *, tree); |
4b780675 GDR |
33 | static void pp_cxx_qualified_id (cxx_pretty_printer *, tree); |
34 | static void pp_cxx_assignment_expression (cxx_pretty_printer *, tree); | |
12ea3302 | 35 | static void pp_cxx_expression (cxx_pretty_printer *, tree); |
4b780675 | 36 | static void pp_cxx_template_argument_list (cxx_pretty_printer *, tree); |
12ea3302 GDR |
37 | static void pp_cxx_type_specifier_seq (cxx_pretty_printer *, tree); |
38 | static void pp_cxx_ptr_operator (cxx_pretty_printer *, tree); | |
39 | static void pp_cxx_type_id (cxx_pretty_printer *, tree); | |
40 | static void pp_cxx_direct_abstract_declarator (cxx_pretty_printer *, tree); | |
41 | static void pp_cxx_declarator (cxx_pretty_printer *, tree); | |
7090f4b3 | 42 | static void pp_cxx_parameter_declaration_clause (cxx_pretty_printer *, tree); |
12ea3302 | 43 | static void pp_cxx_abstract_declarator (cxx_pretty_printer *, tree); |
b01150a2 | 44 | static void pp_cxx_statement (cxx_pretty_printer *, tree); |
12ea3302 | 45 | static void pp_cxx_template_parameter (cxx_pretty_printer *, tree); |
392e3d51 | 46 | static void pp_cxx_cast_expression (cxx_pretty_printer *, tree); |
e1a4dd13 | 47 | \f |
4b780675 GDR |
48 | |
49 | static inline void | |
50 | pp_cxx_nonconsecutive_character (cxx_pretty_printer *pp, int c) | |
51 | { | |
52 | const char *p = pp_last_position_in_text (pp); | |
53 | ||
54 | if (p != NULL && *p == c) | |
12ea3302 | 55 | pp_cxx_whitespace (pp); |
4b780675 | 56 | pp_character (pp, c); |
12ea3302 | 57 | pp_base (pp)->padding = pp_none; |
4b780675 GDR |
58 | } |
59 | ||
12ea3302 GDR |
60 | #define pp_cxx_storage_class_specifier(PP, T) \ |
61 | pp_c_storage_class_specifier (pp_c_base (PP), T) | |
62 | #define pp_cxx_expression_list(PP, T) \ | |
63 | pp_c_expression_list (pp_c_base (PP), T) | |
64 | #define pp_cxx_space_for_pointer_operator(PP, T) \ | |
65 | pp_c_space_for_pointer_operator (pp_c_base (PP), T) | |
66 | #define pp_cxx_init_declarator(PP, T) \ | |
67 | pp_c_init_declarator (pp_c_base (PP), T) | |
68 | #define pp_cxx_call_argument_list(PP, T) \ | |
69 | pp_c_call_argument_list (pp_c_base (PP), T) | |
e1a4dd13 | 70 | |
41fd3bac | 71 | void |
12ea3302 | 72 | pp_cxx_colon_colon (cxx_pretty_printer *pp) |
e1a4dd13 | 73 | { |
12ea3302 GDR |
74 | pp_colon_colon (pp); |
75 | pp_base (pp)->padding = pp_none; | |
e1a4dd13 GDR |
76 | } |
77 | ||
41fd3bac GDR |
78 | void |
79 | pp_cxx_begin_template_argument_list (cxx_pretty_printer *pp) | |
80 | { | |
81 | pp_cxx_nonconsecutive_character (pp, '<'); | |
82 | } | |
83 | ||
84 | void | |
85 | pp_cxx_end_template_argument_list (cxx_pretty_printer *pp) | |
86 | { | |
87 | pp_cxx_nonconsecutive_character (pp, '>'); | |
88 | } | |
89 | ||
90 | void | |
91 | pp_cxx_separate_with (cxx_pretty_printer *pp, int c) | |
92 | { | |
93 | pp_separate_with (pp, c); | |
94 | pp_base (pp)->padding = pp_none; | |
95 | } | |
e1a4dd13 | 96 | |
04c06002 | 97 | /* Expressions. */ |
e1a4dd13 | 98 | |
12ea3302 GDR |
99 | static inline bool |
100 | is_destructor_name (tree name) | |
e1a4dd13 | 101 | { |
12ea3302 GDR |
102 | return name == complete_dtor_identifier |
103 | || name == base_dtor_identifier | |
104 | || name == deleting_dtor_identifier; | |
e1a4dd13 GDR |
105 | } |
106 | ||
12ea3302 GDR |
107 | /* conversion-function-id: |
108 | operator conversion-type-id | |
4b780675 | 109 | |
12ea3302 GDR |
110 | conversion-type-id: |
111 | type-specifier-seq conversion-declarator(opt) | |
4b780675 | 112 | |
12ea3302 GDR |
113 | conversion-declarator: |
114 | ptr-operator conversion-declarator(opt) */ | |
b9b44fb9 | 115 | |
12ea3302 GDR |
116 | static inline void |
117 | pp_cxx_conversion_function_id (cxx_pretty_printer *pp, tree t) | |
4b780675 | 118 | { |
12ea3302 GDR |
119 | pp_cxx_identifier (pp, "operator"); |
120 | pp_cxx_type_specifier_seq (pp, TREE_TYPE (t)); | |
4b780675 GDR |
121 | } |
122 | ||
12ea3302 GDR |
123 | static inline void |
124 | pp_cxx_template_id (cxx_pretty_printer *pp, tree t) | |
e1a4dd13 | 125 | { |
12ea3302 GDR |
126 | pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 0)); |
127 | pp_cxx_begin_template_argument_list (pp); | |
128 | pp_cxx_template_argument_list (pp, TREE_OPERAND (t, 1)); | |
129 | pp_cxx_end_template_argument_list (pp); | |
e1a4dd13 GDR |
130 | } |
131 | ||
4b780675 GDR |
132 | /* unqualified-id: |
133 | identifier | |
134 | operator-function-id | |
135 | conversion-function-id | |
136 | ~ class-name | |
137 | template-id */ | |
b9b44fb9 | 138 | |
4b780675 GDR |
139 | static void |
140 | pp_cxx_unqualified_id (cxx_pretty_printer *pp, tree t) | |
141 | { | |
142 | enum tree_code code = TREE_CODE (t); | |
143 | switch (code) | |
144 | { | |
12ea3302 GDR |
145 | case RESULT_DECL: |
146 | pp_cxx_identifier (pp, "<return-value>"); | |
147 | break; | |
148 | ||
149 | case OVERLOAD: | |
c8094d83 | 150 | t = OVL_CURRENT (t); |
4b780675 GDR |
151 | case VAR_DECL: |
152 | case PARM_DECL: | |
153 | case CONST_DECL: | |
154 | case TYPE_DECL: | |
155 | case FUNCTION_DECL: | |
156 | case NAMESPACE_DECL: | |
157 | case FIELD_DECL: | |
158 | case LABEL_DECL: | |
159 | case USING_DECL: | |
12ea3302 | 160 | case TEMPLATE_DECL: |
4b780675 | 161 | t = DECL_NAME (t); |
c8094d83 | 162 | |
4b780675 | 163 | case IDENTIFIER_NODE: |
12ea3302 | 164 | if (t == NULL) |
0cbd7506 | 165 | pp_cxx_identifier (pp, "<unnamed>"); |
12ea3302 | 166 | else if (IDENTIFIER_TYPENAME_P (t)) |
0cbd7506 | 167 | pp_cxx_conversion_function_id (pp, t); |
4b780675 | 168 | else |
0cbd7506 MS |
169 | { |
170 | if (is_destructor_name (t)) | |
171 | { | |
172 | pp_complement (pp); | |
173 | /* FIXME: Why is this necessary? */ | |
174 | if (TREE_TYPE (t)) | |
175 | t = constructor_name (TREE_TYPE (t)); | |
176 | } | |
177 | pp_cxx_tree_identifier (pp, t); | |
178 | } | |
4b780675 GDR |
179 | break; |
180 | ||
181 | case TEMPLATE_ID_EXPR: | |
12ea3302 GDR |
182 | pp_cxx_template_id (pp, t); |
183 | break; | |
184 | ||
3601f003 KL |
185 | case BASELINK: |
186 | pp_cxx_unqualified_id (pp, BASELINK_FUNCTIONS (t)); | |
187 | break; | |
188 | ||
12ea3302 GDR |
189 | case RECORD_TYPE: |
190 | case UNION_TYPE: | |
191 | case ENUMERAL_TYPE: | |
192 | pp_cxx_unqualified_id (pp, TYPE_NAME (t)); | |
193 | break; | |
194 | ||
195 | case TEMPLATE_TYPE_PARM: | |
41fd3bac GDR |
196 | case TEMPLATE_TEMPLATE_PARM: |
197 | if (TYPE_IDENTIFIER (t)) | |
0cbd7506 | 198 | pp_cxx_unqualified_id (pp, TYPE_IDENTIFIER (t)); |
41fd3bac | 199 | else |
0cbd7506 | 200 | pp_cxx_canonical_template_parameter (pp, t); |
41fd3bac GDR |
201 | break; |
202 | ||
12ea3302 GDR |
203 | case TEMPLATE_PARM_INDEX: |
204 | pp_cxx_unqualified_id (pp, TEMPLATE_PARM_DECL (t)); | |
4b780675 GDR |
205 | break; |
206 | ||
207 | default: | |
208 | pp_unsupported_tree (pp, t); | |
12ea3302 GDR |
209 | break; |
210 | } | |
211 | } | |
212 | ||
b9b44fb9 GDR |
213 | /* Pretty-print out the token sequence ":: template" in template codes |
214 | where it is needed to "inline declare" the (following) member as | |
666c27b9 | 215 | a template. This situation arises when SCOPE of T is dependent |
b9b44fb9 GDR |
216 | on template parameters. */ |
217 | ||
12ea3302 GDR |
218 | static inline void |
219 | pp_cxx_template_keyword_if_needed (cxx_pretty_printer *pp, tree scope, tree t) | |
220 | { | |
221 | if (TREE_CODE (t) == TEMPLATE_ID_EXPR | |
222 | && TYPE_P (scope) && dependent_type_p (scope)) | |
223 | pp_cxx_identifier (pp, "template"); | |
224 | } | |
225 | ||
226 | /* nested-name-specifier: | |
227 | class-or-namespace-name :: nested-name-specifier(opt) | |
228 | class-or-namespace-name :: template nested-name-specifier */ | |
b9b44fb9 | 229 | |
12ea3302 GDR |
230 | static void |
231 | pp_cxx_nested_name_specifier (cxx_pretty_printer *pp, tree t) | |
232 | { | |
233 | if (t != NULL && t != pp->enclosing_scope) | |
234 | { | |
235 | tree scope = TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t); | |
236 | pp_cxx_nested_name_specifier (pp, scope); | |
237 | pp_cxx_template_keyword_if_needed (pp, scope, t); | |
238 | pp_cxx_unqualified_id (pp, t); | |
239 | pp_cxx_colon_colon (pp); | |
4b780675 GDR |
240 | } |
241 | } | |
242 | ||
243 | /* qualified-id: | |
244 | nested-name-specifier template(opt) unqualified-id */ | |
b9b44fb9 | 245 | |
4b780675 GDR |
246 | static void |
247 | pp_cxx_qualified_id (cxx_pretty_printer *pp, tree t) | |
248 | { | |
249 | switch (TREE_CODE (t)) | |
250 | { | |
b9b44fb9 | 251 | /* A pointer-to-member is always qualified. */ |
4b780675 | 252 | case PTRMEM_CST: |
12ea3302 | 253 | pp_cxx_nested_name_specifier (pp, PTRMEM_CST_CLASS (t)); |
4b780675 GDR |
254 | pp_cxx_unqualified_id (pp, PTRMEM_CST_MEMBER (t)); |
255 | break; | |
256 | ||
b9b44fb9 | 257 | /* In Standard C++, functions cannot possibly be used as |
0cbd7506 MS |
258 | nested-name-specifiers. However, there are situations where |
259 | is "makes sense" to output the surrounding function name for the | |
260 | purpose of emphasizing on the scope kind. Just printing the | |
261 | function name might not be sufficient as it may be overloaded; so, | |
262 | we decorate the function with its signature too. | |
263 | FIXME: This is probably the wrong pretty-printing for conversion | |
264 | functions and some function templates. */ | |
12ea3302 GDR |
265 | case OVERLOAD: |
266 | t = OVL_CURRENT (t); | |
267 | case FUNCTION_DECL: | |
268 | if (DECL_FUNCTION_MEMBER_P (t)) | |
0cbd7506 | 269 | pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t)); |
12ea3302 | 270 | pp_cxx_unqualified_id |
0cbd7506 | 271 | (pp, DECL_CONSTRUCTOR_P (t) ? DECL_CONTEXT (t) : t); |
b9b44fb9 | 272 | pp_cxx_parameter_declaration_clause (pp, TREE_TYPE (t)); |
12ea3302 GDR |
273 | break; |
274 | ||
4b780675 GDR |
275 | case OFFSET_REF: |
276 | case SCOPE_REF: | |
12ea3302 | 277 | pp_cxx_nested_name_specifier (pp, TREE_OPERAND (t, 0)); |
4b780675 GDR |
278 | pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 1)); |
279 | break; | |
280 | ||
281 | default: | |
282 | { | |
0cbd7506 MS |
283 | tree scope = TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t); |
284 | if (scope != pp->enclosing_scope) | |
285 | { | |
286 | pp_cxx_nested_name_specifier (pp, scope); | |
287 | pp_cxx_template_keyword_if_needed (pp, scope, t); | |
288 | } | |
289 | pp_cxx_unqualified_id (pp, t); | |
4b780675 GDR |
290 | } |
291 | break; | |
292 | } | |
293 | } | |
294 | ||
295 | /* id-expression: | |
cd0be382 | 296 | unqualified-id |
4b780675 | 297 | qualified-id */ |
b9b44fb9 | 298 | |
4b780675 GDR |
299 | static inline void |
300 | pp_cxx_id_expression (cxx_pretty_printer *pp, tree t) | |
301 | { | |
12ea3302 GDR |
302 | if (TREE_CODE (t) == OVERLOAD) |
303 | t = OVL_CURRENT (t); | |
d04a575f | 304 | if (DECL_P (t) && DECL_CONTEXT (t)) |
4b780675 GDR |
305 | pp_cxx_qualified_id (pp, t); |
306 | else | |
307 | pp_cxx_unqualified_id (pp, t); | |
308 | } | |
309 | ||
310 | /* primary-expression: | |
311 | literal | |
312 | this | |
313 | :: identifier | |
314 | :: operator-function-id | |
315 | :: qualifier-id | |
316 | ( expression ) | |
317 | id-expression */ | |
b9b44fb9 | 318 | |
e1a4dd13 GDR |
319 | static void |
320 | pp_cxx_primary_expression (cxx_pretty_printer *pp, tree t) | |
321 | { | |
4b780675 GDR |
322 | switch (TREE_CODE (t)) |
323 | { | |
324 | case STRING_CST: | |
325 | case INTEGER_CST: | |
326 | case REAL_CST: | |
327 | pp_c_constant (pp_c_base (pp), t); | |
328 | break; | |
329 | ||
12ea3302 GDR |
330 | case BASELINK: |
331 | t = BASELINK_FUNCTIONS (t); | |
332 | case VAR_DECL: | |
333 | case PARM_DECL: | |
334 | case FIELD_DECL: | |
335 | case FUNCTION_DECL: | |
336 | case OVERLOAD: | |
337 | case CONST_DECL: | |
338 | case TEMPLATE_DECL: | |
339 | pp_cxx_id_expression (pp, t); | |
340 | break; | |
341 | ||
342 | case RESULT_DECL: | |
343 | case TEMPLATE_TYPE_PARM: | |
41fd3bac | 344 | case TEMPLATE_TEMPLATE_PARM: |
12ea3302 GDR |
345 | case TEMPLATE_PARM_INDEX: |
346 | pp_cxx_unqualified_id (pp, t); | |
347 | break; | |
348 | ||
c3e5898b ILT |
349 | case STMT_EXPR: |
350 | pp_cxx_left_paren (pp); | |
351 | pp_cxx_statement (pp, STMT_EXPR_STMT (t)); | |
352 | pp_cxx_right_paren (pp); | |
353 | break; | |
354 | ||
4b780675 | 355 | default: |
12ea3302 | 356 | pp_c_primary_expression (pp_c_base (pp), t); |
4b780675 GDR |
357 | break; |
358 | } | |
e1a4dd13 GDR |
359 | } |
360 | ||
4b780675 GDR |
361 | /* postfix-expression: |
362 | primary-expression | |
363 | postfix-expression [ expression ] | |
364 | postfix-expression ( expression-list(opt) ) | |
365 | simple-type-specifier ( expression-list(opt) ) | |
366 | typename ::(opt) nested-name-specifier identifier ( expression-list(opt) ) | |
367 | typename ::(opt) nested-name-specifier template(opt) | |
0cbd7506 | 368 | template-id ( expression-list(opt) ) |
4b780675 GDR |
369 | postfix-expression . template(opt) ::(opt) id-expression |
370 | postfix-expression -> template(opt) ::(opt) id-expression | |
371 | postfix-expression . pseudo-destructor-name | |
372 | postfix-expression -> pseudo-destructor-name | |
373 | postfix-expression ++ | |
374 | postfix-expression -- | |
375 | dynamic_cast < type-id > ( expression ) | |
376 | static_cast < type-id > ( expression ) | |
377 | reinterpret_cast < type-id > ( expression ) | |
378 | const_cast < type-id > ( expression ) | |
379 | typeid ( expression ) | |
380 | typeif ( type-id ) */ | |
381 | ||
e1a4dd13 GDR |
382 | static void |
383 | pp_cxx_postfix_expression (cxx_pretty_printer *pp, tree t) | |
384 | { | |
4b780675 | 385 | enum tree_code code = TREE_CODE (t); |
c8094d83 | 386 | |
4b780675 GDR |
387 | switch (code) |
388 | { | |
12ea3302 GDR |
389 | case AGGR_INIT_EXPR: |
390 | case CALL_EXPR: | |
391 | { | |
0cbd7506 MS |
392 | tree fun = TREE_OPERAND (t, 0); |
393 | tree args = TREE_OPERAND (t, 1); | |
394 | tree saved_scope = pp->enclosing_scope; | |
395 | ||
396 | if (TREE_CODE (fun) == ADDR_EXPR) | |
397 | fun = TREE_OPERAND (fun, 0); | |
398 | ||
399 | /* In templates, where there is no way to tell whether a given | |
400 | call uses an actual member function. So the parser builds | |
401 | FUN as a COMPONENT_REF or a plain IDENTIFIER_NODE until | |
402 | instantiation time. */ | |
403 | if (TREE_CODE (fun) != FUNCTION_DECL) | |
404 | ; | |
405 | else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)) | |
406 | { | |
407 | tree object = code == AGGR_INIT_EXPR && AGGR_INIT_VIA_CTOR_P (t) | |
408 | ? TREE_OPERAND (t, 2) | |
409 | : TREE_VALUE (args); | |
410 | ||
411 | while (TREE_CODE (object) == NOP_EXPR) | |
412 | object = TREE_OPERAND (object, 0); | |
413 | ||
414 | if (TREE_CODE (object) == ADDR_EXPR) | |
415 | object = TREE_OPERAND (object, 0); | |
416 | ||
417 | if (TREE_CODE (TREE_TYPE (object)) != POINTER_TYPE) | |
418 | { | |
419 | pp_cxx_postfix_expression (pp, object); | |
420 | pp_cxx_dot (pp); | |
421 | } | |
422 | else | |
423 | { | |
424 | pp_cxx_postfix_expression (pp, object); | |
425 | pp_cxx_arrow (pp); | |
426 | } | |
427 | args = TREE_CHAIN (args); | |
428 | pp->enclosing_scope = strip_pointer_operator (TREE_TYPE (object)); | |
429 | } | |
430 | ||
431 | pp_cxx_postfix_expression (pp, fun); | |
432 | pp->enclosing_scope = saved_scope; | |
433 | pp_cxx_call_argument_list (pp, args); | |
12ea3302 GDR |
434 | } |
435 | if (code == AGGR_INIT_EXPR && AGGR_INIT_VIA_CTOR_P (t)) | |
0cbd7506 MS |
436 | { |
437 | pp_cxx_separate_with (pp, ','); | |
438 | pp_cxx_postfix_expression (pp, TREE_OPERAND (t, 2)); | |
439 | } | |
12ea3302 GDR |
440 | break; |
441 | ||
442 | case BASELINK: | |
443 | case VAR_DECL: | |
444 | case PARM_DECL: | |
445 | case FIELD_DECL: | |
446 | case FUNCTION_DECL: | |
447 | case OVERLOAD: | |
448 | case CONST_DECL: | |
449 | case TEMPLATE_DECL: | |
450 | case RESULT_DECL: | |
451 | pp_cxx_primary_expression (pp, t); | |
452 | break; | |
453 | ||
4b780675 GDR |
454 | case DYNAMIC_CAST_EXPR: |
455 | case STATIC_CAST_EXPR: | |
456 | case REINTERPRET_CAST_EXPR: | |
457 | case CONST_CAST_EXPR: | |
458 | if (code == DYNAMIC_CAST_EXPR) | |
0cbd7506 | 459 | pp_cxx_identifier (pp, "dynamic_cast"); |
4b780675 | 460 | else if (code == STATIC_CAST_EXPR) |
0cbd7506 | 461 | pp_cxx_identifier (pp, "static_cast"); |
4b780675 | 462 | else if (code == REINTERPRET_CAST_EXPR) |
0cbd7506 | 463 | pp_cxx_identifier (pp, "reinterpret_cast"); |
4b780675 | 464 | else |
0cbd7506 | 465 | pp_cxx_identifier (pp, "const_cast"); |
4b780675 GDR |
466 | pp_cxx_begin_template_argument_list (pp); |
467 | pp_cxx_type_id (pp, TREE_TYPE (t)); | |
468 | pp_cxx_end_template_argument_list (pp); | |
469 | pp_left_paren (pp); | |
12ea3302 | 470 | pp_cxx_expression (pp, TREE_OPERAND (t, 0)); |
4b780675 GDR |
471 | pp_right_paren (pp); |
472 | break; | |
473 | ||
474 | case EMPTY_CLASS_EXPR: | |
475 | pp_cxx_type_id (pp, TREE_TYPE (t)); | |
476 | pp_left_paren (pp); | |
477 | pp_right_paren (pp); | |
478 | break; | |
479 | ||
480 | case TYPEID_EXPR: | |
481 | t = TREE_OPERAND (t, 0); | |
12ea3302 | 482 | pp_cxx_identifier (pp, "typeid"); |
4b780675 GDR |
483 | pp_left_paren (pp); |
484 | if (TYPE_P (t)) | |
0cbd7506 | 485 | pp_cxx_type_id (pp, t); |
4b780675 | 486 | else |
0cbd7506 | 487 | pp_cxx_expression (pp, t); |
4b780675 GDR |
488 | pp_right_paren (pp); |
489 | break; | |
490 | ||
491 | case PSEUDO_DTOR_EXPR: | |
492 | pp_cxx_postfix_expression (pp, TREE_OPERAND (t, 0)); | |
12ea3302 | 493 | pp_cxx_dot (pp); |
4b780675 | 494 | pp_cxx_qualified_id (pp, TREE_OPERAND (t, 1)); |
12ea3302 | 495 | pp_cxx_colon_colon (pp); |
4b780675 GDR |
496 | pp_complement (pp); |
497 | pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 2)); | |
498 | break; | |
499 | ||
03a08664 ILT |
500 | case ARROW_EXPR: |
501 | pp_cxx_postfix_expression (pp, TREE_OPERAND (t, 0)); | |
502 | pp_cxx_arrow (pp); | |
503 | break; | |
504 | ||
4b780675 GDR |
505 | default: |
506 | pp_c_postfix_expression (pp_c_base (pp), t); | |
507 | break; | |
508 | } | |
e1a4dd13 GDR |
509 | } |
510 | ||
4b780675 GDR |
511 | /* new-expression: |
512 | ::(opt) new new-placement(opt) new-type-id new-initializer(opt) | |
513 | ::(opt) new new-placement(opt) ( type-id ) new-initializer(opt) | |
514 | ||
515 | new-placement: | |
516 | ( expression-list ) | |
517 | ||
518 | new-type-id: | |
519 | type-specifier-seq new-declarator(opt) | |
520 | ||
521 | new-declarator: | |
522 | ptr-operator new-declarator(opt) | |
523 | direct-new-declarator | |
524 | ||
525 | direct-new-declarator | |
526 | [ expression ] | |
527 | direct-new-declarator [ constant-expression ] | |
528 | ||
529 | new-initializer: | |
530 | ( expression-list(opt) ) */ | |
b9b44fb9 | 531 | |
e1a4dd13 | 532 | static void |
4b780675 | 533 | pp_cxx_new_expression (cxx_pretty_printer *pp, tree t) |
e1a4dd13 | 534 | { |
4b780675 GDR |
535 | enum tree_code code = TREE_CODE (t); |
536 | switch (code) | |
537 | { | |
538 | case NEW_EXPR: | |
539 | case VEC_NEW_EXPR: | |
540 | if (NEW_EXPR_USE_GLOBAL (t)) | |
0cbd7506 | 541 | pp_cxx_colon_colon (pp); |
12ea3302 | 542 | pp_cxx_identifier (pp, "new"); |
4b780675 | 543 | if (TREE_OPERAND (t, 0)) |
0cbd7506 MS |
544 | { |
545 | pp_cxx_call_argument_list (pp, TREE_OPERAND (t, 0)); | |
546 | pp_space (pp); | |
547 | } | |
4b780675 GDR |
548 | /* FIXME: array-types are built with one more element. */ |
549 | pp_cxx_type_id (pp, TREE_OPERAND (t, 1)); | |
550 | if (TREE_OPERAND (t, 2)) | |
0cbd7506 MS |
551 | { |
552 | pp_left_paren (pp); | |
553 | t = TREE_OPERAND (t, 2); | |
554 | if (TREE_CODE (t) == TREE_LIST) | |
555 | pp_c_expression_list (pp_c_base (pp), t); | |
556 | else if (t == void_zero_node) | |
557 | ; /* OK, empty initializer list. */ | |
558 | else | |
559 | pp_cxx_expression (pp, t); | |
560 | pp_right_paren (pp); | |
561 | } | |
4b780675 GDR |
562 | break; |
563 | ||
564 | default: | |
565 | pp_unsupported_tree (pp, t); | |
566 | } | |
e1a4dd13 GDR |
567 | } |
568 | ||
4b780675 GDR |
569 | /* delete-expression: |
570 | ::(opt) delete cast-expression | |
571 | ::(opt) delete [ ] cast-expression */ | |
b9b44fb9 | 572 | |
e1a4dd13 | 573 | static void |
4b780675 | 574 | pp_cxx_delete_expression (cxx_pretty_printer *pp, tree t) |
e1a4dd13 | 575 | { |
4b780675 GDR |
576 | enum tree_code code = TREE_CODE (t); |
577 | switch (code) | |
578 | { | |
579 | case DELETE_EXPR: | |
580 | case VEC_DELETE_EXPR: | |
581 | if (DELETE_EXPR_USE_GLOBAL (t)) | |
0cbd7506 | 582 | pp_cxx_colon_colon (pp); |
12ea3302 | 583 | pp_cxx_identifier (pp, "delete"); |
4b780675 | 584 | if (code == VEC_DELETE_EXPR) |
0cbd7506 MS |
585 | { |
586 | pp_left_bracket (pp); | |
587 | pp_right_bracket (pp); | |
588 | } | |
4b780675 | 589 | pp_c_cast_expression (pp_c_base (pp), TREE_OPERAND (t, 0)); |
c8094d83 MS |
590 | break; |
591 | ||
4b780675 GDR |
592 | default: |
593 | pp_unsupported_tree (pp, t); | |
594 | } | |
e1a4dd13 GDR |
595 | } |
596 | ||
4b780675 GDR |
597 | /* unary-expression: |
598 | postfix-expression | |
599 | ++ cast-expression | |
600 | -- cast-expression | |
601 | unary-operator cast-expression | |
602 | sizeof unary-expression | |
603 | sizeof ( type-id ) | |
604 | new-expression | |
605 | delete-expression | |
606 | ||
607 | unary-operator: one of | |
608 | * & + - ! | |
609 | ||
610 | GNU extensions: | |
611 | __alignof__ unary-expression | |
612 | __alignof__ ( type-id ) */ | |
b9b44fb9 | 613 | |
e1a4dd13 | 614 | static void |
4b780675 | 615 | pp_cxx_unary_expression (cxx_pretty_printer *pp, tree t) |
e1a4dd13 | 616 | { |
4b780675 GDR |
617 | enum tree_code code = TREE_CODE (t); |
618 | switch (code) | |
619 | { | |
620 | case NEW_EXPR: | |
621 | case VEC_NEW_EXPR: | |
622 | pp_cxx_new_expression (pp, t); | |
623 | break; | |
624 | ||
625 | case DELETE_EXPR: | |
626 | case VEC_DELETE_EXPR: | |
627 | pp_cxx_delete_expression (pp, t); | |
628 | break; | |
c8094d83 | 629 | |
03a08664 ILT |
630 | case SIZEOF_EXPR: |
631 | case ALIGNOF_EXPR: | |
632 | pp_cxx_identifier (pp, code == SIZEOF_EXPR ? "sizeof" : "__alignof__"); | |
633 | pp_cxx_whitespace (pp); | |
634 | if (TYPE_P (TREE_OPERAND (t, 0))) | |
635 | { | |
636 | pp_cxx_left_paren (pp); | |
637 | pp_cxx_type_id (pp, TREE_OPERAND (t, 0)); | |
638 | pp_cxx_right_paren (pp); | |
639 | } | |
640 | else | |
641 | pp_unary_expression (pp, TREE_OPERAND (t, 0)); | |
642 | break; | |
643 | ||
392e3d51 RS |
644 | case UNARY_PLUS_EXPR: |
645 | pp_plus (pp); | |
646 | pp_cxx_cast_expression (pp, TREE_OPERAND (t, 0)); | |
647 | break; | |
648 | ||
4b780675 GDR |
649 | default: |
650 | pp_c_unary_expression (pp_c_base (pp), t); | |
651 | break; | |
652 | } | |
e1a4dd13 GDR |
653 | } |
654 | ||
12ea3302 GDR |
655 | /* cast-expression: |
656 | unary-expression | |
657 | ( type-id ) cast-expression */ | |
b9b44fb9 | 658 | |
12ea3302 GDR |
659 | static void |
660 | pp_cxx_cast_expression (cxx_pretty_printer *pp, tree t) | |
661 | { | |
662 | switch (TREE_CODE (t)) | |
663 | { | |
664 | case CAST_EXPR: | |
665 | pp_cxx_type_id (pp, TREE_TYPE (t)); | |
666 | pp_cxx_call_argument_list (pp, TREE_OPERAND (t, 0)); | |
667 | break; | |
668 | ||
669 | default: | |
670 | pp_c_cast_expression (pp_c_base (pp), t); | |
671 | break; | |
672 | } | |
673 | } | |
674 | ||
4b780675 GDR |
675 | /* pm-expression: |
676 | cast-expression | |
677 | pm-expression .* cast-expression | |
678 | pm-expression ->* cast-expression */ | |
b9b44fb9 | 679 | |
e1a4dd13 | 680 | static void |
4b780675 | 681 | pp_cxx_pm_expression (cxx_pretty_printer *pp, tree t) |
e1a4dd13 | 682 | { |
4b780675 GDR |
683 | switch (TREE_CODE (t)) |
684 | { | |
685 | /* Handle unfortunate OFFESET_REF overloading here. */ | |
686 | case OFFSET_REF: | |
687 | if (TYPE_P (TREE_OPERAND (t, 0))) | |
0cbd7506 MS |
688 | { |
689 | pp_cxx_qualified_id (pp, t); | |
690 | break; | |
691 | } | |
f4f206f4 | 692 | /* Else fall through. */ |
4b780675 GDR |
693 | case MEMBER_REF: |
694 | case DOTSTAR_EXPR: | |
695 | pp_cxx_pm_expression (pp, TREE_OPERAND (t, 0)); | |
12ea3302 | 696 | pp_cxx_dot (pp); |
4b780675 | 697 | pp_star(pp); |
12ea3302 | 698 | pp_cxx_cast_expression (pp, TREE_OPERAND (t, 1)); |
4b780675 GDR |
699 | break; |
700 | ||
701 | ||
702 | default: | |
12ea3302 | 703 | pp_cxx_cast_expression (pp, t); |
4b780675 GDR |
704 | break; |
705 | } | |
e1a4dd13 GDR |
706 | } |
707 | ||
4b780675 GDR |
708 | /* multiplicative-expression: |
709 | pm-expression | |
710 | multiplicative-expression * pm-expression | |
711 | multiplicative-expression / pm-expression | |
712 | multiplicative-expression % pm-expression */ | |
b9b44fb9 | 713 | |
e1a4dd13 | 714 | static void |
4b780675 | 715 | pp_cxx_multiplicative_expression (cxx_pretty_printer *pp, tree e) |
e1a4dd13 | 716 | { |
4b780675 GDR |
717 | enum tree_code code = TREE_CODE (e); |
718 | switch (code) | |
719 | { | |
720 | case MULT_EXPR: | |
721 | case TRUNC_DIV_EXPR: | |
722 | case TRUNC_MOD_EXPR: | |
723 | pp_cxx_multiplicative_expression (pp, TREE_OPERAND (e, 0)); | |
724 | pp_space (pp); | |
725 | if (code == MULT_EXPR) | |
726 | pp_star (pp); | |
727 | else if (code == TRUNC_DIV_EXPR) | |
728 | pp_slash (pp); | |
729 | else | |
730 | pp_modulo (pp); | |
731 | pp_space (pp); | |
732 | pp_cxx_pm_expression (pp, TREE_OPERAND (e, 1)); | |
733 | break; | |
734 | ||
735 | default: | |
736 | pp_cxx_pm_expression (pp, e); | |
737 | break; | |
738 | } | |
e1a4dd13 GDR |
739 | } |
740 | ||
4b780675 GDR |
741 | /* conditional-expression: |
742 | logical-or-expression | |
743 | logical-or-expression ? expression : assignment-expression */ | |
b9b44fb9 | 744 | |
4b780675 GDR |
745 | static void |
746 | pp_cxx_conditional_expression (cxx_pretty_printer *pp, tree e) | |
e1a4dd13 | 747 | { |
4b780675 GDR |
748 | if (TREE_CODE (e) == COND_EXPR) |
749 | { | |
750 | pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0)); | |
751 | pp_space (pp); | |
752 | pp_question (pp); | |
753 | pp_space (pp); | |
12ea3302 | 754 | pp_cxx_expression (pp, TREE_OPERAND (e, 1)); |
4b780675 GDR |
755 | pp_space (pp); |
756 | pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 2)); | |
757 | } | |
758 | else | |
759 | pp_c_logical_or_expression (pp_c_base (pp), e); | |
760 | } | |
761 | ||
b9b44fb9 GDR |
762 | /* Pretty-print a compound assignment operator token as indicated by T. */ |
763 | ||
12ea3302 GDR |
764 | static void |
765 | pp_cxx_assignment_operator (cxx_pretty_printer *pp, tree t) | |
766 | { | |
767 | const char *op; | |
768 | ||
769 | switch (TREE_CODE (t)) | |
770 | { | |
771 | case NOP_EXPR: | |
772 | op = "="; | |
773 | break; | |
774 | ||
775 | case PLUS_EXPR: | |
776 | op = "+="; | |
777 | break; | |
778 | ||
779 | case MINUS_EXPR: | |
780 | op = "-="; | |
781 | break; | |
782 | ||
783 | case TRUNC_DIV_EXPR: | |
784 | op = "/="; | |
785 | break; | |
786 | ||
787 | case TRUNC_MOD_EXPR: | |
788 | op = "%="; | |
789 | break; | |
790 | ||
791 | default: | |
792 | op = tree_code_name[TREE_CODE (t)]; | |
793 | break; | |
794 | } | |
795 | ||
796 | pp_cxx_identifier (pp, op); | |
797 | } | |
798 | ||
799 | ||
4b780675 GDR |
800 | /* assignment-expression: |
801 | conditional-expression | |
802 | logical-or-expression assignment-operator assignment-expression | |
12ea3302 GDR |
803 | throw-expression |
804 | ||
805 | throw-expression: | |
806 | throw assignment-expression(opt) | |
4b780675 | 807 | |
12ea3302 | 808 | assignment-operator: one of |
4b780675 | 809 | = *= /= %= += -= >>= <<= &= ^= |= */ |
b9b44fb9 | 810 | |
4b780675 GDR |
811 | static void |
812 | pp_cxx_assignment_expression (cxx_pretty_printer *pp, tree e) | |
813 | { | |
12ea3302 | 814 | switch (TREE_CODE (e)) |
4b780675 | 815 | { |
12ea3302 GDR |
816 | case MODIFY_EXPR: |
817 | case INIT_EXPR: | |
4b780675 GDR |
818 | pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0)); |
819 | pp_space (pp); | |
820 | pp_equal (pp); | |
821 | pp_space (pp); | |
822 | pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 1)); | |
12ea3302 | 823 | break; |
e1a4dd13 | 824 | |
12ea3302 GDR |
825 | case THROW_EXPR: |
826 | pp_cxx_identifier (pp, "throw"); | |
827 | if (TREE_OPERAND (e, 0)) | |
0cbd7506 | 828 | pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 0)); |
12ea3302 | 829 | break; |
e1a4dd13 | 830 | |
12ea3302 GDR |
831 | case MODOP_EXPR: |
832 | pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0)); | |
833 | pp_cxx_assignment_operator (pp, TREE_OPERAND (e, 1)); | |
834 | pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 2)); | |
835 | break; | |
e1a4dd13 | 836 | |
12ea3302 GDR |
837 | default: |
838 | pp_cxx_conditional_expression (pp, e); | |
839 | break; | |
840 | } | |
841 | } | |
842 | ||
843 | static void | |
844 | pp_cxx_expression (cxx_pretty_printer *pp, tree t) | |
845 | { | |
846 | switch (TREE_CODE (t)) | |
847 | { | |
848 | case STRING_CST: | |
849 | case INTEGER_CST: | |
850 | case REAL_CST: | |
851 | pp_c_constant (pp_c_base (pp), t); | |
852 | break; | |
853 | ||
854 | case RESULT_DECL: | |
855 | pp_cxx_unqualified_id (pp, t); | |
856 | break; | |
857 | ||
c8094d83 | 858 | #if 0 |
12ea3302 | 859 | case OFFSET_REF: |
c8094d83 | 860 | #endif |
12ea3302 GDR |
861 | case SCOPE_REF: |
862 | case PTRMEM_CST: | |
863 | pp_cxx_qualified_id (pp, t); | |
864 | break; | |
865 | ||
866 | case OVERLOAD: | |
867 | t = OVL_CURRENT (t); | |
868 | case VAR_DECL: | |
869 | case PARM_DECL: | |
870 | case FIELD_DECL: | |
871 | case CONST_DECL: | |
872 | case FUNCTION_DECL: | |
873 | case BASELINK: | |
874 | case TEMPLATE_DECL: | |
875 | case TEMPLATE_TYPE_PARM: | |
876 | case TEMPLATE_PARM_INDEX: | |
41fd3bac | 877 | case TEMPLATE_TEMPLATE_PARM: |
c3e5898b | 878 | case STMT_EXPR: |
12ea3302 GDR |
879 | pp_cxx_primary_expression (pp, t); |
880 | break; | |
881 | ||
882 | case CALL_EXPR: | |
883 | case DYNAMIC_CAST_EXPR: | |
884 | case STATIC_CAST_EXPR: | |
885 | case REINTERPRET_CAST_EXPR: | |
886 | case CONST_CAST_EXPR: | |
c8094d83 | 887 | #if 0 |
12ea3302 | 888 | case MEMBER_REF: |
c8094d83 | 889 | #endif |
12ea3302 GDR |
890 | case EMPTY_CLASS_EXPR: |
891 | case TYPEID_EXPR: | |
892 | case PSEUDO_DTOR_EXPR: | |
893 | case AGGR_INIT_EXPR: | |
03a08664 | 894 | case ARROW_EXPR: |
12ea3302 GDR |
895 | pp_cxx_postfix_expression (pp, t); |
896 | break; | |
897 | ||
898 | case NEW_EXPR: | |
899 | case VEC_NEW_EXPR: | |
900 | pp_cxx_new_expression (pp, t); | |
901 | break; | |
902 | ||
903 | case DELETE_EXPR: | |
904 | case VEC_DELETE_EXPR: | |
905 | pp_cxx_delete_expression (pp, t); | |
906 | break; | |
907 | ||
03a08664 ILT |
908 | case SIZEOF_EXPR: |
909 | case ALIGNOF_EXPR: | |
910 | pp_cxx_unary_expression (pp, t); | |
911 | break; | |
912 | ||
12ea3302 GDR |
913 | case CAST_EXPR: |
914 | pp_cxx_cast_expression (pp, t); | |
915 | break; | |
916 | ||
917 | case OFFSET_REF: | |
918 | case MEMBER_REF: | |
919 | case DOTSTAR_EXPR: | |
920 | pp_cxx_pm_expression (pp, t); | |
921 | break; | |
922 | ||
923 | case MULT_EXPR: | |
924 | case TRUNC_DIV_EXPR: | |
925 | case TRUNC_MOD_EXPR: | |
926 | pp_cxx_multiplicative_expression (pp, t); | |
927 | break; | |
928 | ||
929 | case COND_EXPR: | |
930 | pp_cxx_conditional_expression (pp, t); | |
931 | break; | |
932 | ||
933 | case MODIFY_EXPR: | |
934 | case INIT_EXPR: | |
935 | case THROW_EXPR: | |
936 | case MODOP_EXPR: | |
937 | pp_cxx_assignment_expression (pp, t); | |
938 | break; | |
939 | ||
b9b44fb9 GDR |
940 | case NON_DEPENDENT_EXPR: |
941 | case MUST_NOT_THROW_EXPR: | |
942 | pp_cxx_expression (pp, t); | |
943 | break; | |
944 | ||
12ea3302 GDR |
945 | default: |
946 | pp_c_expression (pp_c_base (pp), t); | |
c8094d83 | 947 | break; |
12ea3302 GDR |
948 | } |
949 | } | |
950 | ||
951 | ||
952 | /* Declarations. */ | |
953 | ||
954 | /* function-specifier: | |
955 | inline | |
956 | virtual | |
957 | explicit */ | |
b9b44fb9 | 958 | |
12ea3302 GDR |
959 | static void |
960 | pp_cxx_function_specifier (cxx_pretty_printer *pp, tree t) | |
961 | { | |
962 | switch (TREE_CODE (t)) | |
963 | { | |
964 | case FUNCTION_DECL: | |
965 | if (DECL_VIRTUAL_P (t)) | |
0cbd7506 | 966 | pp_cxx_identifier (pp, "virtual"); |
12ea3302 | 967 | else if (DECL_CONSTRUCTOR_P (t) && DECL_NONCONVERTING_P (t)) |
0cbd7506 | 968 | pp_cxx_identifier (pp, "explicit"); |
12ea3302 | 969 | else |
0cbd7506 | 970 | pp_c_function_specifier (pp_c_base (pp), t); |
12ea3302 GDR |
971 | |
972 | default: | |
973 | break; | |
974 | } | |
975 | } | |
976 | ||
977 | /* decl-specifier-seq: | |
978 | decl-specifier-seq(opt) decl-specifier | |
979 | ||
980 | decl-specifier: | |
981 | storage-class-specifier | |
982 | type-specifier | |
983 | function-specifier | |
984 | friend | |
985 | typedef */ | |
b9b44fb9 | 986 | |
12ea3302 GDR |
987 | static void |
988 | pp_cxx_decl_specifier_seq (cxx_pretty_printer *pp, tree t) | |
989 | { | |
990 | switch (TREE_CODE (t)) | |
991 | { | |
992 | case VAR_DECL: | |
993 | case PARM_DECL: | |
994 | case CONST_DECL: | |
995 | case FIELD_DECL: | |
996 | pp_cxx_storage_class_specifier (pp, t); | |
997 | pp_cxx_decl_specifier_seq (pp, TREE_TYPE (t)); | |
998 | break; | |
c8094d83 | 999 | |
12ea3302 GDR |
1000 | case TYPE_DECL: |
1001 | pp_cxx_identifier (pp, "typedef"); | |
1002 | pp_cxx_decl_specifier_seq (pp, TREE_TYPE (t)); | |
1003 | break; | |
1004 | ||
1005 | case RECORD_TYPE: | |
1006 | if (TYPE_PTRMEMFUNC_P (t)) | |
0cbd7506 MS |
1007 | { |
1008 | tree pfm = TYPE_PTRMEMFUNC_FN_TYPE (t); | |
1009 | pp_cxx_decl_specifier_seq (pp, TREE_TYPE (TREE_TYPE (pfm))); | |
1010 | pp_cxx_whitespace (pp); | |
1011 | pp_cxx_ptr_operator (pp, t); | |
1012 | } | |
12ea3302 GDR |
1013 | break; |
1014 | ||
1015 | case FUNCTION_DECL: | |
1016 | /* Constructors don't have return types. And conversion functions | |
0cbd7506 | 1017 | do not have a type-specifier in their return types. */ |
12ea3302 | 1018 | if (DECL_CONSTRUCTOR_P (t) || DECL_CONV_FN_P (t)) |
0cbd7506 | 1019 | pp_cxx_function_specifier (pp, t); |
12ea3302 | 1020 | else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t)) |
0cbd7506 | 1021 | pp_cxx_decl_specifier_seq (pp, TREE_TYPE (TREE_TYPE (t))); |
12ea3302 | 1022 | else |
0cbd7506 | 1023 | default: |
12ea3302 GDR |
1024 | pp_c_declaration_specifiers (pp_c_base (pp), t); |
1025 | break; | |
1026 | } | |
1027 | } | |
1028 | ||
1029 | /* simple-type-specifier: | |
1030 | ::(opt) nested-name-specifier(opt) type-name | |
1031 | ::(opt) nested-name-specifier(opt) template(opt) template-id | |
1032 | char | |
1033 | wchar_t | |
1034 | bool | |
1035 | short | |
1036 | int | |
1037 | long | |
1038 | signed | |
1039 | unsigned | |
1040 | float | |
1041 | double | |
1042 | void */ | |
b9b44fb9 | 1043 | |
12ea3302 GDR |
1044 | static void |
1045 | pp_cxx_simple_type_specifier (cxx_pretty_printer *pp, tree t) | |
1046 | { | |
1047 | switch (TREE_CODE (t)) | |
1048 | { | |
1049 | case RECORD_TYPE: | |
1050 | case UNION_TYPE: | |
1051 | case ENUMERAL_TYPE: | |
1052 | pp_cxx_qualified_id (pp, t); | |
1053 | break; | |
1054 | ||
1055 | case TEMPLATE_TYPE_PARM: | |
41fd3bac | 1056 | case TEMPLATE_TEMPLATE_PARM: |
12ea3302 GDR |
1057 | case TEMPLATE_PARM_INDEX: |
1058 | pp_cxx_unqualified_id (pp, t); | |
1059 | break; | |
1060 | ||
1061 | case TYPENAME_TYPE: | |
1062 | pp_cxx_identifier (pp, "typename"); | |
1063 | pp_cxx_nested_name_specifier (pp, TYPE_CONTEXT (t)); | |
1064 | pp_cxx_unqualified_id (pp, TYPE_NAME (t)); | |
1065 | break; | |
1066 | ||
1067 | default: | |
1068 | pp_c_type_specifier (pp_c_base (pp), t); | |
1069 | break; | |
1070 | } | |
1071 | } | |
1072 | ||
1073 | /* type-specifier-seq: | |
1074 | type-specifier type-specifier-seq(opt) | |
1075 | ||
1076 | type-specifier: | |
1077 | simple-type-specifier | |
1078 | class-specifier | |
1079 | enum-specifier | |
1080 | elaborated-type-specifier | |
cd0be382 | 1081 | cv-qualifier */ |
12ea3302 GDR |
1082 | |
1083 | static void | |
1084 | pp_cxx_type_specifier_seq (cxx_pretty_printer *pp, tree t) | |
1085 | { | |
12ea3302 GDR |
1086 | switch (TREE_CODE (t)) |
1087 | { | |
1088 | case TEMPLATE_DECL: | |
1089 | case TEMPLATE_TYPE_PARM: | |
41fd3bac | 1090 | case TEMPLATE_TEMPLATE_PARM: |
12ea3302 GDR |
1091 | case TYPE_DECL: |
1092 | case BOUND_TEMPLATE_TEMPLATE_PARM: | |
41fd3bac | 1093 | pp_cxx_cv_qualifier_seq (pp, t); |
12ea3302 GDR |
1094 | pp_cxx_simple_type_specifier (pp, t); |
1095 | break; | |
1096 | ||
1097 | case METHOD_TYPE: | |
1098 | pp_cxx_type_specifier_seq (pp, TREE_TYPE (t)); | |
1099 | pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (t)); | |
1100 | pp_cxx_nested_name_specifier (pp, TYPE_METHOD_BASETYPE (t)); | |
1101 | break; | |
1102 | ||
1103 | default: | |
1104 | if (!(TREE_CODE (t) == FUNCTION_DECL && DECL_CONSTRUCTOR_P (t))) | |
0cbd7506 | 1105 | pp_c_specifier_qualifier_list (pp_c_base (pp), t); |
12ea3302 GDR |
1106 | } |
1107 | } | |
1108 | ||
1109 | /* ptr-operator: | |
1110 | * cv-qualifier-seq(opt) | |
1111 | & | |
1112 | ::(opt) nested-name-specifier * cv-qualifier-seq(opt) */ | |
1113 | ||
1114 | static void | |
1115 | pp_cxx_ptr_operator (cxx_pretty_printer *pp, tree t) | |
1116 | { | |
1117 | if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL) | |
1118 | t = TREE_TYPE (t); | |
1119 | switch (TREE_CODE (t)) | |
1120 | { | |
1121 | case REFERENCE_TYPE: | |
1122 | case POINTER_TYPE: | |
1123 | if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE | |
0cbd7506 MS |
1124 | || TYPE_PTR_TO_MEMBER_P (TREE_TYPE (t))) |
1125 | pp_cxx_ptr_operator (pp, TREE_TYPE (t)); | |
12ea3302 | 1126 | if (TREE_CODE (t) == POINTER_TYPE) |
0cbd7506 MS |
1127 | { |
1128 | pp_star (pp); | |
1129 | pp_cxx_cv_qualifier_seq (pp, t); | |
1130 | } | |
12ea3302 | 1131 | else |
0cbd7506 | 1132 | pp_ampersand (pp); |
12ea3302 GDR |
1133 | break; |
1134 | ||
1135 | case RECORD_TYPE: | |
1136 | if (TYPE_PTRMEMFUNC_P (t)) | |
0cbd7506 MS |
1137 | { |
1138 | pp_cxx_left_paren (pp); | |
1139 | pp_cxx_nested_name_specifier (pp, TYPE_PTRMEMFUNC_OBJECT_TYPE (t)); | |
1140 | pp_star (pp); | |
1141 | break; | |
1142 | } | |
12ea3302 GDR |
1143 | case OFFSET_TYPE: |
1144 | if (TYPE_PTR_TO_MEMBER_P (t)) | |
0cbd7506 MS |
1145 | { |
1146 | if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) | |
1147 | pp_cxx_left_paren (pp); | |
1148 | pp_cxx_nested_name_specifier (pp, TYPE_PTRMEM_CLASS_TYPE (t)); | |
1149 | pp_star (pp); | |
1150 | pp_cxx_cv_qualifier_seq (pp, t); | |
1151 | break; | |
1152 | } | |
da1d7781 | 1153 | /* else fall through. */ |
12ea3302 GDR |
1154 | |
1155 | default: | |
1156 | pp_unsupported_tree (pp, t); | |
1157 | break; | |
1158 | } | |
1159 | } | |
1160 | ||
1161 | static inline tree | |
1162 | pp_cxx_implicit_parameter_type (tree mf) | |
1163 | { | |
1164 | return TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (mf)))); | |
1165 | } | |
1166 | ||
1167 | /* | |
1168 | parameter-declaration: | |
1169 | decl-specifier-seq declarator | |
1170 | decl-specifier-seq declarator = assignment-expression | |
1171 | decl-specifier-seq abstract-declarator(opt) | |
1172 | decl-specifier-seq abstract-declarator(opt) assignment-expression */ | |
b9b44fb9 | 1173 | |
12ea3302 GDR |
1174 | static inline void |
1175 | pp_cxx_parameter_declaration (cxx_pretty_printer *pp, tree t) | |
1176 | { | |
1177 | pp_cxx_decl_specifier_seq (pp, t); | |
1178 | if (TYPE_P (t)) | |
1179 | pp_cxx_abstract_declarator (pp, t); | |
1180 | else | |
1181 | pp_cxx_declarator (pp, t); | |
1182 | } | |
1183 | ||
1184 | /* parameter-declaration-clause: | |
1185 | parameter-declaration-list(opt) ...(opt) | |
1186 | parameter-declaration-list , ... | |
1187 | ||
1188 | parameter-declaration-list: | |
1189 | parameter-declaration | |
1190 | parameter-declaration-list , parameter-declaration */ | |
b9b44fb9 | 1191 | |
12ea3302 GDR |
1192 | static void |
1193 | pp_cxx_parameter_declaration_clause (cxx_pretty_printer *pp, tree t) | |
1194 | { | |
1195 | tree args = TYPE_P (t) ? NULL : FUNCTION_FIRST_USER_PARM (t); | |
c8094d83 | 1196 | tree types = |
6615c446 | 1197 | TYPE_P (t) ? TYPE_ARG_TYPES (t) : FUNCTION_FIRST_USER_PARMTYPE (t); |
12ea3302 GDR |
1198 | const bool abstract = args == NULL |
1199 | || pp_c_base (pp)->flags & pp_c_flag_abstract; | |
1200 | bool first = true; | |
1201 | ||
1202 | /* Skip artificial parameter for nonstatic member functions. */ | |
1203 | if (TREE_CODE (t) == METHOD_TYPE) | |
1204 | types = TREE_CHAIN (types); | |
1205 | ||
1206 | pp_cxx_left_paren (pp); | |
1207 | for (; args; args = TREE_CHAIN (args), types = TREE_CHAIN (types)) | |
1208 | { | |
1209 | if (!first) | |
0cbd7506 | 1210 | pp_cxx_separate_with (pp, ','); |
12ea3302 GDR |
1211 | first = false; |
1212 | pp_cxx_parameter_declaration (pp, abstract ? TREE_VALUE (types) : args); | |
1213 | if (!abstract && pp_c_base (pp)->flags & pp_cxx_flag_default_argument) | |
0cbd7506 MS |
1214 | { |
1215 | pp_cxx_whitespace (pp); | |
1216 | pp_equal (pp); | |
1217 | pp_cxx_whitespace (pp); | |
1218 | pp_cxx_assignment_expression (pp, TREE_PURPOSE (types)); | |
1219 | } | |
12ea3302 GDR |
1220 | } |
1221 | pp_cxx_right_paren (pp); | |
1222 | } | |
1223 | ||
1224 | /* exception-specification: | |
1225 | throw ( type-id-list(opt) ) | |
1226 | ||
1227 | type-id-list | |
1228 | type-id | |
1229 | type-id-list , type-id */ | |
b9b44fb9 | 1230 | |
12ea3302 GDR |
1231 | static void |
1232 | pp_cxx_exception_specification (cxx_pretty_printer *pp, tree t) | |
1233 | { | |
1234 | tree ex_spec = TYPE_RAISES_EXCEPTIONS (t); | |
1235 | ||
1236 | if (!TYPE_NOTHROW_P (t) && ex_spec == NULL) | |
1237 | return; | |
1238 | pp_cxx_identifier (pp, "throw"); | |
1239 | pp_cxx_left_paren (pp); | |
1240 | for (; ex_spec && TREE_VALUE (ex_spec); ex_spec = TREE_CHAIN (ex_spec)) | |
1241 | { | |
1242 | pp_cxx_type_id (pp, TREE_VALUE (ex_spec)); | |
1243 | if (TREE_CHAIN (ex_spec)) | |
0cbd7506 | 1244 | pp_cxx_separate_with (pp, ','); |
12ea3302 GDR |
1245 | } |
1246 | pp_cxx_right_paren (pp); | |
1247 | } | |
1248 | ||
1249 | /* direct-declarator: | |
1250 | declarator-id | |
1251 | direct-declarator ( parameter-declaration-clause ) cv-qualifier-seq(opt) | |
0cbd7506 | 1252 | exception-specification(opt) |
12ea3302 GDR |
1253 | direct-declaration [ constant-expression(opt) ] |
1254 | ( declarator ) */ | |
b9b44fb9 | 1255 | |
12ea3302 GDR |
1256 | static void |
1257 | pp_cxx_direct_declarator (cxx_pretty_printer *pp, tree t) | |
1258 | { | |
1259 | switch (TREE_CODE (t)) | |
1260 | { | |
1261 | case VAR_DECL: | |
1262 | case PARM_DECL: | |
1263 | case CONST_DECL: | |
1264 | case FIELD_DECL: | |
1265 | if (DECL_NAME (t)) | |
0cbd7506 MS |
1266 | { |
1267 | pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (t)); | |
1268 | pp_cxx_id_expression (pp, DECL_NAME (t)); | |
1269 | } | |
12ea3302 GDR |
1270 | pp_cxx_abstract_declarator (pp, TREE_TYPE (t)); |
1271 | break; | |
c8094d83 | 1272 | |
12ea3302 GDR |
1273 | case FUNCTION_DECL: |
1274 | pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (TREE_TYPE (t))); | |
1275 | pp_cxx_id_expression (pp, t); | |
1276 | pp_cxx_parameter_declaration_clause (pp, t); | |
c8094d83 | 1277 | |
12ea3302 | 1278 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t)) |
0cbd7506 MS |
1279 | { |
1280 | pp_base (pp)->padding = pp_before; | |
1281 | pp_cxx_cv_qualifier_seq (pp, pp_cxx_implicit_parameter_type (t)); | |
1282 | } | |
12ea3302 GDR |
1283 | |
1284 | pp_cxx_exception_specification (pp, TREE_TYPE (t)); | |
1285 | break; | |
1286 | ||
1287 | case TYPENAME_TYPE: | |
1288 | case TEMPLATE_DECL: | |
1289 | case TEMPLATE_TYPE_PARM: | |
1290 | case TEMPLATE_PARM_INDEX: | |
41fd3bac | 1291 | case TEMPLATE_TEMPLATE_PARM: |
12ea3302 GDR |
1292 | break; |
1293 | ||
1294 | default: | |
1295 | pp_c_direct_declarator (pp_c_base (pp), t); | |
1296 | break; | |
1297 | } | |
1298 | } | |
1299 | ||
1300 | /* declarator: | |
1301 | direct-declarator | |
1302 | ptr-operator declarator */ | |
b9b44fb9 | 1303 | |
12ea3302 GDR |
1304 | static void |
1305 | pp_cxx_declarator (cxx_pretty_printer *pp, tree t) | |
1306 | { | |
1307 | pp_cxx_direct_declarator (pp, t); | |
1308 | } | |
1309 | ||
1310 | /* ctor-initializer: | |
1311 | : mem-initializer-list | |
1312 | ||
1313 | mem-initializer-list: | |
1314 | mem-initializer | |
1315 | mem-initializer , mem-initializer-list | |
1316 | ||
1317 | mem-initializer: | |
1318 | mem-initializer-id ( expression-list(opt) ) | |
1319 | ||
1320 | mem-initializer-id: | |
1321 | ::(opt) nested-name-specifier(opt) class-name | |
1322 | identifier */ | |
b9b44fb9 | 1323 | |
12ea3302 GDR |
1324 | static void |
1325 | pp_cxx_ctor_initializer (cxx_pretty_printer *pp, tree t) | |
1326 | { | |
1327 | t = TREE_OPERAND (t, 0); | |
1328 | pp_cxx_whitespace (pp); | |
1329 | pp_colon (pp); | |
1330 | pp_cxx_whitespace (pp); | |
1331 | for (; t; t = TREE_CHAIN (t)) | |
1332 | { | |
1333 | pp_cxx_primary_expression (pp, TREE_PURPOSE (t)); | |
1334 | pp_cxx_call_argument_list (pp, TREE_VALUE (t)); | |
1335 | if (TREE_CHAIN (t)) | |
0cbd7506 | 1336 | pp_cxx_separate_with (pp, ','); |
12ea3302 GDR |
1337 | } |
1338 | } | |
1339 | ||
1340 | /* function-definition: | |
1341 | decl-specifier-seq(opt) declarator ctor-initializer(opt) function-body | |
1342 | decl-specifier-seq(opt) declarator function-try-block */ | |
1343 | ||
b01150a2 | 1344 | static void |
12ea3302 GDR |
1345 | pp_cxx_function_definition (cxx_pretty_printer *pp, tree t) |
1346 | { | |
1347 | tree saved_scope = pp->enclosing_scope; | |
1348 | pp_cxx_decl_specifier_seq (pp, t); | |
1349 | pp_cxx_declarator (pp, t); | |
1350 | pp_needs_newline (pp) = true; | |
1351 | pp->enclosing_scope = DECL_CONTEXT (t); | |
1352 | if (DECL_SAVED_TREE (t)) | |
5882f0f3 | 1353 | pp_cxx_statement (pp, DECL_SAVED_TREE (t)); |
12ea3302 GDR |
1354 | else |
1355 | { | |
1356 | pp_cxx_semicolon (pp); | |
1357 | pp_needs_newline (pp) = true; | |
1358 | } | |
1359 | pp_flush (pp); | |
1360 | pp->enclosing_scope = saved_scope; | |
1361 | } | |
1362 | ||
1363 | /* abstract-declarator: | |
1364 | ptr-operator abstract-declarator(opt) | |
1365 | direct-abstract-declarator */ | |
b9b44fb9 | 1366 | |
12ea3302 GDR |
1367 | static void |
1368 | pp_cxx_abstract_declarator (cxx_pretty_printer *pp, tree t) | |
1369 | { | |
1370 | if (TYPE_PTRMEM_P (t) || TYPE_PTRMEMFUNC_P (t)) | |
1371 | pp_cxx_right_paren (pp); | |
1372 | else if (POINTER_TYPE_P (t)) | |
1373 | { | |
1374 | if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE | |
0cbd7506 MS |
1375 | || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE) |
1376 | pp_cxx_right_paren (pp); | |
12ea3302 GDR |
1377 | t = TREE_TYPE (t); |
1378 | } | |
1379 | pp_cxx_direct_abstract_declarator (pp, t); | |
1380 | } | |
1381 | ||
1382 | /* direct-abstract-declarator: | |
1383 | direct-abstract-declarator(opt) ( parameter-declaration-clause ) | |
0cbd7506 | 1384 | cv-qualifier-seq(opt) exception-specification(opt) |
12ea3302 GDR |
1385 | direct-abstract-declarator(opt) [ constant-expression(opt) ] |
1386 | ( abstract-declarator ) */ | |
b9b44fb9 | 1387 | |
12ea3302 GDR |
1388 | static void |
1389 | pp_cxx_direct_abstract_declarator (cxx_pretty_printer *pp, tree t) | |
1390 | { | |
1391 | switch (TREE_CODE (t)) | |
1392 | { | |
1393 | case REFERENCE_TYPE: | |
1394 | pp_cxx_abstract_declarator (pp, t); | |
1395 | break; | |
1396 | ||
1397 | case RECORD_TYPE: | |
1398 | if (TYPE_PTRMEMFUNC_P (t)) | |
0cbd7506 | 1399 | pp_cxx_direct_abstract_declarator (pp, TYPE_PTRMEMFUNC_FN_TYPE (t)); |
12ea3302 GDR |
1400 | break; |
1401 | ||
1402 | case METHOD_TYPE: | |
1403 | case FUNCTION_TYPE: | |
1404 | pp_cxx_parameter_declaration_clause (pp, t); | |
1405 | pp_cxx_direct_abstract_declarator (pp, TREE_TYPE (t)); | |
1406 | if (TREE_CODE (t) == METHOD_TYPE) | |
0cbd7506 MS |
1407 | { |
1408 | pp_base (pp)->padding = pp_before; | |
1409 | pp_cxx_cv_qualifier_seq | |
1410 | (pp, TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t)))); | |
1411 | } | |
12ea3302 GDR |
1412 | pp_cxx_exception_specification (pp, t); |
1413 | break; | |
1414 | ||
1415 | case TYPENAME_TYPE: | |
1416 | case TEMPLATE_TYPE_PARM: | |
1417 | case TEMPLATE_TEMPLATE_PARM: | |
1418 | case BOUND_TEMPLATE_TEMPLATE_PARM: | |
1419 | case UNBOUND_CLASS_TEMPLATE: | |
1420 | break; | |
1421 | ||
1422 | default: | |
1423 | pp_c_direct_abstract_declarator (pp_c_base (pp), t); | |
c8094d83 | 1424 | break; |
12ea3302 GDR |
1425 | } |
1426 | } | |
1427 | ||
1428 | /* type-id: | |
1429 | type-specifier-seq abstract-declarator(opt) */ | |
b9b44fb9 | 1430 | |
12ea3302 GDR |
1431 | static void |
1432 | pp_cxx_type_id (cxx_pretty_printer *pp, tree t) | |
1433 | { | |
1434 | pp_flags saved_flags = pp_c_base (pp)->flags; | |
1435 | pp_c_base (pp)->flags |= pp_c_flag_abstract; | |
1436 | ||
1437 | switch (TREE_CODE (t)) | |
1438 | { | |
1439 | case TYPE_DECL: | |
1440 | case UNION_TYPE: | |
1441 | case RECORD_TYPE: | |
1442 | case ENUMERAL_TYPE: | |
1443 | case TYPENAME_TYPE: | |
1444 | case BOUND_TEMPLATE_TEMPLATE_PARM: | |
1445 | case UNBOUND_CLASS_TEMPLATE: | |
1446 | case TEMPLATE_TEMPLATE_PARM: | |
1447 | case TEMPLATE_TYPE_PARM: | |
1448 | case TEMPLATE_PARM_INDEX: | |
1449 | case TEMPLATE_DECL: | |
1450 | case TYPEOF_TYPE: | |
1451 | case TEMPLATE_ID_EXPR: | |
12ea3302 | 1452 | pp_cxx_type_specifier_seq (pp, t); |
12ea3302 GDR |
1453 | break; |
1454 | ||
1455 | default: | |
1456 | pp_c_type_id (pp_c_base (pp), t); | |
1457 | break; | |
1458 | } | |
1459 | ||
1460 | pp_c_base (pp)->flags = saved_flags; | |
1461 | } | |
1462 | ||
1463 | /* template-argument-list: | |
1464 | template-argument | |
1465 | template-argument-list, template-argument | |
1466 | ||
1467 | template-argument: | |
1468 | assignment-expression | |
1469 | type-id | |
1470 | template-name */ | |
b9b44fb9 | 1471 | |
12ea3302 GDR |
1472 | static void |
1473 | pp_cxx_template_argument_list (cxx_pretty_printer *pp, tree t) | |
1474 | { | |
1475 | int i; | |
1476 | if (t == NULL) | |
1477 | return; | |
1478 | for (i = 0; i < TREE_VEC_LENGTH (t); ++i) | |
1479 | { | |
1480 | tree arg = TREE_VEC_ELT (t, i); | |
1481 | if (i != 0) | |
0cbd7506 | 1482 | pp_cxx_separate_with (pp, ','); |
12ea3302 | 1483 | if (TYPE_P (arg) || (TREE_CODE (arg) == TEMPLATE_DECL |
6615c446 | 1484 | && TYPE_P (DECL_TEMPLATE_RESULT (arg)))) |
0cbd7506 | 1485 | pp_cxx_type_id (pp, arg); |
12ea3302 | 1486 | else |
0cbd7506 | 1487 | pp_cxx_expression (pp, arg); |
12ea3302 GDR |
1488 | } |
1489 | } | |
1490 | ||
1491 | ||
1492 | static void | |
1493 | pp_cxx_exception_declaration (cxx_pretty_printer *pp, tree t) | |
1494 | { | |
350fae66 | 1495 | t = DECL_EXPR_DECL (t); |
12ea3302 GDR |
1496 | pp_cxx_type_specifier_seq (pp, t); |
1497 | if (TYPE_P (t)) | |
1498 | pp_cxx_abstract_declarator (pp, t); | |
1499 | else | |
1500 | pp_cxx_declarator (pp, t); | |
1501 | } | |
1502 | ||
1503 | /* Statements. */ | |
1504 | ||
b01150a2 | 1505 | static void |
12ea3302 GDR |
1506 | pp_cxx_statement (cxx_pretty_printer *pp, tree t) |
1507 | { | |
1508 | switch (TREE_CODE (t)) | |
1509 | { | |
5882f0f3 RH |
1510 | case CTOR_INITIALIZER: |
1511 | pp_cxx_ctor_initializer (pp, t); | |
1512 | break; | |
1513 | ||
12ea3302 GDR |
1514 | case USING_STMT: |
1515 | pp_cxx_identifier (pp, "using"); | |
1516 | pp_cxx_identifier (pp, "namespace"); | |
1517 | pp_cxx_qualified_id (pp, USING_STMT_NAMESPACE (t)); | |
1518 | break; | |
1519 | ||
1520 | case USING_DECL: | |
1521 | pp_cxx_identifier (pp, "using"); | |
98ed9dae | 1522 | pp_cxx_nested_name_specifier (pp, USING_DECL_SCOPE (t)); |
12ea3302 GDR |
1523 | pp_cxx_unqualified_id (pp, DECL_NAME (t)); |
1524 | break; | |
1525 | ||
1526 | case EH_SPEC_BLOCK: | |
1527 | break; | |
1528 | ||
1529 | /* try-block: | |
0cbd7506 | 1530 | try compound-statement handler-seq */ |
12ea3302 GDR |
1531 | case TRY_BLOCK: |
1532 | pp_maybe_newline_and_indent (pp, 0); | |
1533 | pp_cxx_identifier (pp, "try"); | |
1534 | pp_newline_and_indent (pp, 3); | |
1535 | pp_cxx_statement (pp, TRY_STMTS (t)); | |
1536 | pp_newline_and_indent (pp, -3); | |
1537 | if (CLEANUP_P (t)) | |
0cbd7506 | 1538 | ; |
12ea3302 | 1539 | else |
0cbd7506 | 1540 | pp_cxx_statement (pp, TRY_HANDLERS (t)); |
12ea3302 GDR |
1541 | break; |
1542 | ||
1543 | /* | |
0cbd7506 MS |
1544 | handler-seq: |
1545 | handler handler-seq(opt) | |
12ea3302 | 1546 | |
0cbd7506 MS |
1547 | handler: |
1548 | catch ( exception-declaration ) compound-statement | |
12ea3302 | 1549 | |
0cbd7506 MS |
1550 | exception-declaration: |
1551 | type-specifier-seq declarator | |
1552 | type-specifier-seq abstract-declarator | |
1553 | ... */ | |
12ea3302 GDR |
1554 | case HANDLER: |
1555 | pp_cxx_identifier (pp, "catch"); | |
1556 | pp_cxx_left_paren (pp); | |
1557 | pp_cxx_exception_declaration (pp, HANDLER_PARMS (t)); | |
1558 | pp_cxx_right_paren (pp); | |
1559 | pp_indentation (pp) += 3; | |
1560 | pp_needs_newline (pp) = true; | |
1561 | pp_cxx_statement (pp, HANDLER_BODY (t)); | |
1562 | pp_indentation (pp) -= 3; | |
1563 | pp_needs_newline (pp) = true; | |
1564 | break; | |
1565 | ||
5a508662 | 1566 | /* selection-statement: |
0cbd7506 MS |
1567 | if ( expression ) statement |
1568 | if ( expression ) statement else statement */ | |
5a508662 RH |
1569 | case IF_STMT: |
1570 | pp_cxx_identifier (pp, "if"); | |
1571 | pp_cxx_whitespace (pp); | |
1572 | pp_cxx_left_paren (pp); | |
1573 | pp_cxx_expression (pp, IF_COND (t)); | |
1574 | pp_cxx_right_paren (pp); | |
1575 | pp_newline_and_indent (pp, 2); | |
1576 | pp_cxx_statement (pp, THEN_CLAUSE (t)); | |
1577 | pp_newline_and_indent (pp, -2); | |
1578 | if (ELSE_CLAUSE (t)) | |
1579 | { | |
1580 | tree else_clause = ELSE_CLAUSE (t); | |
1581 | pp_cxx_identifier (pp, "else"); | |
1582 | if (TREE_CODE (else_clause) == IF_STMT) | |
1583 | pp_cxx_whitespace (pp); | |
1584 | else | |
1585 | pp_newline_and_indent (pp, 2); | |
1586 | pp_cxx_statement (pp, else_clause); | |
1587 | if (TREE_CODE (else_clause) != IF_STMT) | |
1588 | pp_newline_and_indent (pp, -2); | |
1589 | } | |
1590 | break; | |
1591 | ||
fbc315db ILT |
1592 | case SWITCH_STMT: |
1593 | pp_cxx_identifier (pp, "switch"); | |
1594 | pp_space (pp); | |
1595 | pp_cxx_left_paren (pp); | |
934790cc | 1596 | pp_cxx_expression (pp, SWITCH_STMT_COND (t)); |
fbc315db ILT |
1597 | pp_cxx_right_paren (pp); |
1598 | pp_indentation (pp) += 3; | |
1599 | pp_needs_newline (pp) = true; | |
c3e5898b | 1600 | pp_cxx_statement (pp, SWITCH_STMT_BODY (t)); |
fbc315db ILT |
1601 | pp_newline_and_indent (pp, -3); |
1602 | break; | |
1603 | ||
1604 | /* iteration-statement: | |
0cbd7506 MS |
1605 | while ( expression ) statement |
1606 | do statement while ( expression ) ; | |
1607 | for ( expression(opt) ; expression(opt) ; expression(opt) ) statement | |
1608 | for ( declaration expression(opt) ; expression(opt) ) statement */ | |
fbc315db ILT |
1609 | case WHILE_STMT: |
1610 | pp_cxx_identifier (pp, "while"); | |
1611 | pp_space (pp); | |
1612 | pp_cxx_left_paren (pp); | |
934790cc | 1613 | pp_cxx_expression (pp, WHILE_COND (t)); |
fbc315db ILT |
1614 | pp_cxx_right_paren (pp); |
1615 | pp_newline_and_indent (pp, 3); | |
c3e5898b | 1616 | pp_cxx_statement (pp, WHILE_BODY (t)); |
fbc315db ILT |
1617 | pp_indentation (pp) -= 3; |
1618 | pp_needs_newline (pp) = true; | |
1619 | break; | |
1620 | ||
1621 | case DO_STMT: | |
1622 | pp_cxx_identifier (pp, "do"); | |
1623 | pp_newline_and_indent (pp, 3); | |
c3e5898b | 1624 | pp_cxx_statement (pp, DO_BODY (t)); |
fbc315db ILT |
1625 | pp_newline_and_indent (pp, -3); |
1626 | pp_cxx_identifier (pp, "while"); | |
1627 | pp_space (pp); | |
1628 | pp_cxx_left_paren (pp); | |
934790cc | 1629 | pp_cxx_expression (pp, DO_COND (t)); |
fbc315db ILT |
1630 | pp_cxx_right_paren (pp); |
1631 | pp_cxx_semicolon (pp); | |
1632 | pp_needs_newline (pp) = true; | |
1633 | break; | |
1634 | ||
1635 | case FOR_STMT: | |
1636 | pp_cxx_identifier (pp, "for"); | |
1637 | pp_space (pp); | |
1638 | pp_cxx_left_paren (pp); | |
1639 | if (FOR_INIT_STMT (t)) | |
0cbd7506 | 1640 | pp_cxx_statement (pp, FOR_INIT_STMT (t)); |
fbc315db | 1641 | else |
0cbd7506 | 1642 | pp_cxx_semicolon (pp); |
fbc315db ILT |
1643 | pp_needs_newline (pp) = false; |
1644 | pp_cxx_whitespace (pp); | |
1645 | if (FOR_COND (t)) | |
934790cc | 1646 | pp_cxx_expression (pp, FOR_COND (t)); |
fbc315db ILT |
1647 | pp_cxx_semicolon (pp); |
1648 | pp_needs_newline (pp) = false; | |
1649 | pp_cxx_whitespace (pp); | |
1650 | if (FOR_EXPR (t)) | |
934790cc | 1651 | pp_cxx_expression (pp, FOR_EXPR (t)); |
fbc315db ILT |
1652 | pp_cxx_right_paren (pp); |
1653 | pp_newline_and_indent (pp, 3); | |
c3e5898b | 1654 | pp_cxx_statement (pp, FOR_BODY (t)); |
fbc315db ILT |
1655 | pp_indentation (pp) -= 3; |
1656 | pp_needs_newline (pp) = true; | |
1657 | break; | |
1658 | ||
1659 | /* jump-statement: | |
0cbd7506 MS |
1660 | goto identifier; |
1661 | continue ; | |
1662 | return expression(opt) ; */ | |
fbc315db ILT |
1663 | case BREAK_STMT: |
1664 | case CONTINUE_STMT: | |
1665 | pp_identifier (pp, TREE_CODE (t) == BREAK_STMT ? "break" : "continue"); | |
1666 | pp_cxx_semicolon (pp); | |
1667 | pp_needs_newline (pp) = true; | |
1668 | break; | |
1669 | ||
934790cc | 1670 | /* expression-statement: |
0cbd7506 | 1671 | expression(opt) ; */ |
934790cc ILT |
1672 | case EXPR_STMT: |
1673 | pp_cxx_expression (pp, EXPR_STMT_EXPR (t)); | |
1674 | pp_cxx_semicolon (pp); | |
1675 | pp_needs_newline (pp) = true; | |
1676 | break; | |
1677 | ||
5a508662 RH |
1678 | case CLEANUP_STMT: |
1679 | pp_cxx_identifier (pp, "try"); | |
1680 | pp_newline_and_indent (pp, 2); | |
1681 | pp_cxx_statement (pp, CLEANUP_BODY (t)); | |
1682 | pp_newline_and_indent (pp, -2); | |
1683 | pp_cxx_identifier (pp, CLEANUP_EH_ONLY (t) ? "catch" : "finally"); | |
1684 | pp_newline_and_indent (pp, 2); | |
1685 | pp_cxx_statement (pp, CLEANUP_EXPR (t)); | |
1686 | pp_newline_and_indent (pp, -2); | |
1687 | break; | |
1688 | ||
12ea3302 GDR |
1689 | default: |
1690 | pp_c_statement (pp_c_base (pp), t); | |
1691 | break; | |
1692 | } | |
1693 | } | |
1694 | ||
a2a9e21c GDR |
1695 | /* original-namespace-definition: |
1696 | namespace identifier { namespace-body } | |
1697 | ||
1698 | As an edge case, we also handle unnamed namespace definition here. */ | |
1699 | ||
1700 | static void | |
1701 | pp_cxx_original_namespace_definition (cxx_pretty_printer *pp, tree t) | |
1702 | { | |
1703 | pp_cxx_identifier (pp, "namespace"); | |
ed36980c | 1704 | if (DECL_NAME (t)) |
a2a9e21c GDR |
1705 | pp_cxx_unqualified_id (pp, t); |
1706 | pp_cxx_whitespace (pp); | |
1707 | pp_cxx_left_brace (pp); | |
1708 | /* We do not print the namespace-body. */ | |
1709 | pp_cxx_whitespace (pp); | |
1710 | pp_cxx_right_brace (pp); | |
1711 | } | |
1712 | ||
1713 | /* namespace-alias: | |
1714 | identifier | |
1715 | ||
1716 | namespace-alias-definition: | |
1717 | namespace identifier = qualified-namespace-specifier ; | |
1718 | ||
1719 | qualified-namespace-specifier: | |
1720 | ::(opt) nested-name-specifier(opt) namespace-name */ | |
1721 | ||
1722 | static void | |
1723 | pp_cxx_namespace_alias_definition (cxx_pretty_printer *pp, tree t) | |
1724 | { | |
1725 | pp_cxx_identifier (pp, "namespace"); | |
1726 | pp_cxx_unqualified_id (pp, t); | |
1727 | pp_cxx_whitespace (pp); | |
1728 | pp_equal (pp); | |
1729 | pp_cxx_whitespace (pp); | |
1730 | pp_cxx_qualified_id (pp, DECL_NAMESPACE_ALIAS (t)); | |
1731 | pp_cxx_semicolon (pp); | |
1732 | } | |
1733 | ||
12ea3302 GDR |
1734 | /* simple-declaration: |
1735 | decl-specifier-seq(opt) init-declarator-list(opt) */ | |
b9b44fb9 | 1736 | |
12ea3302 GDR |
1737 | static void |
1738 | pp_cxx_simple_declaration (cxx_pretty_printer *pp, tree t) | |
1739 | { | |
1740 | pp_cxx_decl_specifier_seq (pp, t); | |
1741 | pp_cxx_init_declarator (pp, t); | |
1742 | pp_cxx_semicolon (pp); | |
1743 | pp_needs_newline (pp) = true; | |
1744 | } | |
1745 | ||
1746 | /* | |
1747 | template-parameter-list: | |
1748 | template-parameter | |
1749 | template-parameter-list , template-parameter */ | |
1750 | ||
1751 | static inline void | |
1752 | pp_cxx_template_parameter_list (cxx_pretty_printer *pp, tree t) | |
1753 | { | |
1754 | const int n = TREE_VEC_LENGTH (t); | |
1755 | int i; | |
1756 | for (i = 0; i < n; ++i) | |
1757 | { | |
1758 | if (i) | |
0cbd7506 | 1759 | pp_cxx_separate_with (pp, ','); |
12ea3302 GDR |
1760 | pp_cxx_template_parameter (pp, TREE_VEC_ELT (t, i)); |
1761 | } | |
1762 | } | |
1763 | ||
1764 | /* template-parameter: | |
1765 | type-parameter | |
1766 | parameter-declaration | |
1767 | ||
1768 | type-parameter: | |
1769 | class identifier(opt) | |
1770 | class identifier(op) = type-id | |
1771 | typename identifier(opt) | |
1772 | typename identifier(opt) = type-id | |
1773 | template < template-parameter-list > class identifier(opt) | |
1774 | template < template-parameter-list > class identifier(opt) = template-name | |
1775 | */ | |
b9b44fb9 | 1776 | |
12ea3302 GDR |
1777 | static void |
1778 | pp_cxx_template_parameter (cxx_pretty_printer *pp, tree t) | |
1779 | { | |
1780 | tree parameter = TREE_VALUE (t); | |
1781 | switch (TREE_CODE (parameter)) | |
1782 | { | |
1783 | case TYPE_DECL: | |
1784 | pp_cxx_identifier (pp, "class"); | |
1785 | if (DECL_NAME (parameter)) | |
0cbd7506 | 1786 | pp_cxx_tree_identifier (pp, DECL_NAME (parameter)); |
12ea3302 GDR |
1787 | /* FIXME: Chech if we should print also default argument. */ |
1788 | break; | |
1789 | ||
1790 | case PARM_DECL: | |
1791 | pp_cxx_parameter_declaration (pp, parameter); | |
1792 | break; | |
1793 | ||
1794 | case TEMPLATE_DECL: | |
1795 | break; | |
1796 | ||
1797 | default: | |
1798 | pp_unsupported_tree (pp, t); | |
1799 | break; | |
1800 | } | |
1801 | } | |
1802 | ||
b2517173 GDR |
1803 | /* Pretty-print a template parameter in the canonical form |
1804 | "template-parameter-<level>-<position in parameter list>". */ | |
1805 | ||
1806 | void | |
1807 | pp_cxx_canonical_template_parameter (cxx_pretty_printer *pp, tree parm) | |
1808 | { | |
1809 | const enum tree_code code = TREE_CODE (parm); | |
1810 | ||
04c06002 | 1811 | /* Brings type template parameters to the canonical forms. */ |
b2517173 GDR |
1812 | if (code == TEMPLATE_TYPE_PARM || code == TEMPLATE_TEMPLATE_PARM |
1813 | || code == BOUND_TEMPLATE_TEMPLATE_PARM) | |
1814 | parm = TEMPLATE_TYPE_PARM_INDEX (parm); | |
c8094d83 | 1815 | |
b2517173 GDR |
1816 | pp_cxx_begin_template_argument_list (pp); |
1817 | pp_cxx_identifier (pp, "template-parameter-"); | |
1818 | pp_wide_integer (pp, TEMPLATE_PARM_LEVEL (parm)); | |
1819 | pp_minus (pp); | |
1820 | pp_wide_integer (pp, TEMPLATE_PARM_IDX (parm) + 1); | |
1821 | pp_cxx_end_template_argument_list (pp); | |
1822 | } | |
1823 | ||
12ea3302 GDR |
1824 | /* |
1825 | template-declaration: | |
1826 | export(opt) template < template-parameter-list > declaration */ | |
b9b44fb9 | 1827 | |
12ea3302 GDR |
1828 | static void |
1829 | pp_cxx_template_declaration (cxx_pretty_printer *pp, tree t) | |
1830 | { | |
1831 | tree tmpl = most_general_template (t); | |
1832 | tree level; | |
1833 | int i = 0; | |
1834 | ||
1835 | pp_maybe_newline_and_indent (pp, 0); | |
1836 | for (level = DECL_TEMPLATE_PARMS (tmpl); level; level = TREE_CHAIN (level)) | |
1837 | { | |
1838 | pp_cxx_identifier (pp, "template"); | |
1839 | pp_cxx_begin_template_argument_list (pp); | |
1840 | pp_cxx_template_parameter_list (pp, TREE_VALUE (level)); | |
1841 | pp_cxx_end_template_argument_list (pp); | |
1842 | pp_newline_and_indent (pp, 3); | |
1843 | i += 3; | |
1844 | } | |
1845 | if (TREE_CODE (t) == FUNCTION_DECL && DECL_SAVED_TREE (t)) | |
1846 | pp_cxx_function_definition (pp, t); | |
1847 | else | |
1848 | pp_cxx_simple_declaration (pp, t); | |
1849 | } | |
1850 | ||
1851 | static void | |
1852 | pp_cxx_explicit_specialization (cxx_pretty_printer *pp, tree t) | |
1853 | { | |
1854 | pp_unsupported_tree (pp, t); | |
1855 | } | |
1856 | ||
1857 | static void | |
1858 | pp_cxx_explicit_instantiation (cxx_pretty_printer *pp, tree t) | |
1859 | { | |
1860 | pp_unsupported_tree (pp, t); | |
1861 | } | |
1862 | ||
1863 | /* | |
1864 | declaration: | |
1865 | block-declaration | |
1866 | function-definition | |
1867 | template-declaration | |
1868 | explicit-instantiation | |
1869 | explicit-specialization | |
1870 | linkage-specification | |
1871 | namespace-definition | |
1872 | ||
1873 | block-declaration: | |
1874 | simple-declaration | |
1875 | asm-definition | |
1876 | namespace-alias-definition | |
1877 | using-declaration | |
1878 | using-directive */ | |
1879 | void | |
1880 | pp_cxx_declaration (cxx_pretty_printer *pp, tree t) | |
1881 | { | |
1882 | if (!DECL_LANG_SPECIFIC (t)) | |
1883 | pp_cxx_simple_declaration (pp, t); | |
a2a9e21c | 1884 | else if (DECL_USE_TEMPLATE (t)) |
12ea3302 GDR |
1885 | switch (DECL_USE_TEMPLATE (t)) |
1886 | { | |
a2a9e21c | 1887 | case 1: |
0cbd7506 MS |
1888 | pp_cxx_template_declaration (pp, t); |
1889 | break; | |
c8094d83 | 1890 | |
12ea3302 | 1891 | case 2: |
0cbd7506 MS |
1892 | pp_cxx_explicit_specialization (pp, t); |
1893 | break; | |
12ea3302 GDR |
1894 | |
1895 | case 3: | |
0cbd7506 MS |
1896 | pp_cxx_explicit_instantiation (pp, t); |
1897 | break; | |
12ea3302 GDR |
1898 | |
1899 | default: | |
0cbd7506 | 1900 | break; |
12ea3302 | 1901 | } |
12ea3302 GDR |
1902 | else switch (TREE_CODE (t)) |
1903 | { | |
1904 | case VAR_DECL: | |
1905 | case TYPE_DECL: | |
1906 | pp_cxx_simple_declaration (pp, t); | |
1907 | break; | |
c8094d83 | 1908 | |
12ea3302 GDR |
1909 | case FUNCTION_DECL: |
1910 | if (DECL_SAVED_TREE (t)) | |
0cbd7506 | 1911 | pp_cxx_function_definition (pp, t); |
12ea3302 | 1912 | else |
0cbd7506 | 1913 | pp_cxx_simple_declaration (pp, t); |
12ea3302 GDR |
1914 | break; |
1915 | ||
a2a9e21c GDR |
1916 | case NAMESPACE_DECL: |
1917 | if (DECL_NAMESPACE_ALIAS (t)) | |
0cbd7506 | 1918 | pp_cxx_namespace_alias_definition (pp, t); |
a2a9e21c | 1919 | else |
0cbd7506 | 1920 | pp_cxx_original_namespace_definition (pp, t); |
a2a9e21c GDR |
1921 | break; |
1922 | ||
12ea3302 GDR |
1923 | default: |
1924 | pp_unsupported_tree (pp, t); | |
1925 | break; | |
1926 | } | |
1927 | } | |
1928 | ||
1929 | \f | |
1930 | typedef c_pretty_print_fn pp_fun; | |
1931 | ||
b9b44fb9 GDR |
1932 | /* Initialization of a C++ pretty-printer object. */ |
1933 | ||
12ea3302 GDR |
1934 | void |
1935 | pp_cxx_pretty_printer_init (cxx_pretty_printer *pp) | |
1936 | { | |
1937 | pp_c_pretty_printer_init (pp_c_base (pp)); | |
1938 | pp_set_line_maximum_length (pp, 0); | |
e1a4dd13 GDR |
1939 | |
1940 | pp->c_base.declaration = (pp_fun) pp_cxx_declaration; | |
12ea3302 GDR |
1941 | pp->c_base.declaration_specifiers = (pp_fun) pp_cxx_decl_specifier_seq; |
1942 | pp->c_base.function_specifier = (pp_fun) pp_cxx_function_specifier; | |
1943 | pp->c_base.type_specifier_seq = (pp_fun) pp_cxx_type_specifier_seq; | |
e1a4dd13 GDR |
1944 | pp->c_base.declarator = (pp_fun) pp_cxx_declarator; |
1945 | pp->c_base.direct_declarator = (pp_fun) pp_cxx_direct_declarator; | |
12ea3302 | 1946 | pp->c_base.parameter_list = (pp_fun) pp_cxx_parameter_declaration_clause; |
e1a4dd13 | 1947 | pp->c_base.type_id = (pp_fun) pp_cxx_type_id; |
12ea3302 GDR |
1948 | pp->c_base.abstract_declarator = (pp_fun) pp_cxx_abstract_declarator; |
1949 | pp->c_base.direct_abstract_declarator = | |
1950 | (pp_fun) pp_cxx_direct_abstract_declarator; | |
1951 | pp->c_base.simple_type_specifier = (pp_fun)pp_cxx_simple_type_specifier; | |
1952 | ||
1953 | /* pp->c_base.statement = (pp_fun) pp_cxx_statement; */ | |
1954 | ||
4b780675 | 1955 | pp->c_base.id_expression = (pp_fun) pp_cxx_id_expression; |
e1a4dd13 GDR |
1956 | pp->c_base.primary_expression = (pp_fun) pp_cxx_primary_expression; |
1957 | pp->c_base.postfix_expression = (pp_fun) pp_cxx_postfix_expression; | |
1958 | pp->c_base.unary_expression = (pp_fun) pp_cxx_unary_expression; | |
4b780675 | 1959 | pp->c_base.multiplicative_expression = (pp_fun) pp_cxx_multiplicative_expression; |
e1a4dd13 GDR |
1960 | pp->c_base.conditional_expression = (pp_fun) pp_cxx_conditional_expression; |
1961 | pp->c_base.assignment_expression = (pp_fun) pp_cxx_assignment_expression; | |
12ea3302 GDR |
1962 | pp->c_base.expression = (pp_fun) pp_cxx_expression; |
1963 | pp->enclosing_scope = global_namespace; | |
e1a4dd13 | 1964 | } |