diff options
author | gdr <gdr@138bc75d-0d04-0410-961f-82ee72b054a4> | 2003-08-25 19:10:50 +0000 |
---|---|---|
committer | gdr <gdr@138bc75d-0d04-0410-961f-82ee72b054a4> | 2003-08-25 19:10:50 +0000 |
commit | c10de5e7c4a9a60bfa37f03d03d855452df6a6b9 (patch) | |
tree | 288d0b8f94a131ce00ffd87de8db46e6bffbbee5 | |
parent | 8c8125ea529bfedac13ece8d1769d2097760052b (diff) | |
download | gcc-c10de5e7c4a9a60bfa37f03d03d855452df6a6b9.tar.gz |
* pretty-print.h (pp_maybe_newline_and_indent): New macro.
* c-pretty-print.h (c_pretty_printer): Now typedef to the
structure. Be consistent with pretty-print.h abd cxx-pretty-print.h
(struct c_pretty_print_info): Document. Add new fields.
(pp_type_specifier_seq): Rename from pp_c_type_specifier.
(pp_direct_abstract_declarator): New macro.
(pp_ptr_operator): Likewise.
(pp_simple_type_specifier): Likewise.
(pp_expression): Likewise.
(pp_parameter_list): Rename from pp_parameter_declaration.
* c-pretty-print.c (pp_c_whitespace): Now a function.
(pp_c_left_paren): Likewise.
(pp_c_right_paren): Likewise.
(pp_c_dot): Likewise.
(pp_c_ampersand): Likewise.
(pp_c_arrow): Likewise.
(pp_c_semicolon): Likewise.
(pp_c_type_cast): New function.
(pp_c_space_for_pointer_operator): Likewise.
(pp_c_call_argument_list): Likewise.
(pp_c_cv_qualifier): Adjust prototype.
(pp_c_type_qualifier_list): Likewise.
(pp_c_pointer): Likewise. Handle REFERENCE_TYPE here.
(pp_c_type_specifier): Rename from pp_c_simple_type_specifier.
Adjust to follow standard grammar.
(pp_c_specifier_qualifier_list): Adjusr prototype. Handle
REFERENCE_TYPE. Tidy.
(pp_c_parameter_type_list): Adjust prototype. Tidy.
(pp_c_parameter_declaration): Remove.
(pp_c_abstract_declarator): Adjust prototype.
(pp_c_direct_abstract_declarator): Likewise.
(pp_c_type_id): Likewise.
(pp_c_storage_class_specifier): Likewise.
(pp_c_function_specifier): Likewise.
(pp_c_declaration_specifiers): Likewise.
(pp_c_direct_declarator): Likewise.
(pp_c_declarator): Likewise.
(pp_c_declarator): Likewise.
(pp_c_declaration): Likewise.
(pp_c_attributes): Likewise. Tidy.
(pp_c_function_definition): Adjust prototype.
(pp_c_char): Likewise.
(pp_c_string_literal): Likewise.
(pp_c_integer_constant): Likewise.
(pp_c_character_constant): Likewise.
(pp_c_bool_constant): Likewise.
(pp_c_enumeration_constant): Likewise.
(pp_c_floating_constant): Likewise.
(pp_c_constant): Likewise.
(pp_c_identifier): Likewise.
(pp_c_primary_expression): Likewise. Remove TARGET_EXPR case. Tidy.
(pp_c_initializer): Adjust prototype.
(pp_c_init_declarator): Likewise.
(pp_c_initializer_list): Likewise.
(pp_c_id_expression): Likewise.
(pp_c_postfix_expression): Likewise.
(pp_c_expression_list): Likewise.
(pp_c_unary_expression): Likewise.
(pp_c_cast_expression): Likewise.
(pp_c_multiplicative_expression): Likewise.
(pp_c_additive_expression): Likewise.
(pp_c_shift_expression): Likewise.
(pp_c_relational_expression): Likewise.
(pp_c_equality_expression): Likewise.
(pp_c_and_expression): Likewise.
(pp_c_exclusive_or_expression): Likewise.
(pp_c_inclusive_or_expression): Likewise.
(pp_c_logical_and_expression): Likewise.
(pp_c_logical_or_expression): Likewise.
(pp_c_conditional_expression): Likewise.
(pp_c_assignment_expression): Likewise.
(pp_c_expression): Likewise. Tidy.
(pp_c_statement): Likewise. Document.
(pp_c_pretty_printer_init): Adjust prototype. Tidy.
* c-lang.c (c_initialize_diagnostics): Update.
* c-common.h (strip_pointer_operator): Declare.
* c-common.c (strip_pointer_operator): Define.
cp/
* cxx-pretty-print.h (pp_cxx_flag_default_argument): New flag.
(cxx_pretty_printer): Adjust base type.
(pp_cxx_function_specifier): Declare.
* cxx-pretty-print.c (pp_cxx_whitespace): New macro.
(pp_cxx_left_paren): Likewise.
(pp_cxx_right_paren): Likewise.
(pp_cxx_dot): Likewise.
(pp_cxx_arrow): Likewise.
(pp_cxx_semicolon): Likewise.
(pp_cxx_identifier): Likewise.
(pp_cxx_cv_qualifier_seq): Likewise.
(pp_cxx_storage_class_specifier): Likewise.
(pp_cxx_expression_list): Likewise.
(pp_cxx_space_for_pointer_operator): Likewise.
(pp_cxx_init_declarator): Likewise.
(pp_cxx_call_argument_list): Likewise.
(pp_cxx_nonconsecutive_character): Tidy.
(pp_cxx_conversion_function_id): New function.
(pp_cxx_template_id): Likewise.
(pp_cxx_class_name): Likewise.
(pp_cxx_template_keyword_if_needed): Likewise.
(pp_cxx_nested_name_specifier): Likewise.
(pp_cxx_unqualified_id): Tidy
(pp_cxx_qualified_id): Handle more nodes.
(pp_cxx_primary_expression): Tidy.
(pp_cxx_postfix_expression): Likewise.
(pp_cxx_new_expression): Tidy.
(pp_cxx_delete_expression): Likewise.
(pp_cxx_cast_expression): New function.
(pp_cxx_pm_expression): Tidy.
(pp_cxx_conditional_expression): Likewise.
(pp_cxx_assignment_operator): New function.
(pp_cxx_assignment_expression): Tidy.
(pp_cxx_expression): New function.
(pp_cxx_function_specifier): Likewise.
(pp_cxx_decl_specifier_seq): Likewise.
(pp_cxx_simple_type_specifier): Tidy.
(pp_cxx_type_specifier_seq): Likewise.
(pp_cxx_ptr_operator): New function.
(pp_cxx_implicit_parameter_type): Likewise.
(pp_cxx_parameter_declaration): Tidy.
(pp_cxx_parameter_declaration_clause): New function.
(pp_cxx_exception_specification): Likewise.
(pp_cxx_direct_declarator): Tidy.
(pp_cxx_declarator): Likewise.
(pp_cxx_ctor_initializer): New function.
(pp_cxx_function_definition): Likewise.
(pp_cxx_abstract_declarator): Tidy.
(pp_cxx_direct_abstract_declarator): Likewise.
(pp_cxx_type_id): Likewise.
(pp_cxx_exception_declaration): New function.
(pp_cxx_statement): Likewise.
(pp_cxx_simple_declaration): Likewise.
(pp_cxx_template_parameter_list): Likewise.
(pp_cxx_template_parameter): Likewise.
(pp_cxx_template_declaration): Likewise.
(pp_cxx_explicit_specialization): Likewise.
(pp_cxx_explicit_instantiation): Likewise.
(pp_cxx_declaration): Tidy.
(pp_cxx_pretty_printer_init): Initialize more fields.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@70777 138bc75d-0d04-0410-961f-82ee72b054a4
-rw-r--r-- | gcc/ChangeLog | 81 | ||||
-rw-r--r-- | gcc/c-common.c | 9 | ||||
-rw-r--r-- | gcc/c-common.h | 1 | ||||
-rw-r--r-- | gcc/c-lang.c | 2 | ||||
-rw-r--r-- | gcc/c-pretty-print.c | 1370 | ||||
-rw-r--r-- | gcc/c-pretty-print.h | 106 | ||||
-rw-r--r-- | gcc/cp/ChangeLog | 63 | ||||
-rw-r--r-- | gcc/cp/cxx-pretty-print.c | 1406 | ||||
-rw-r--r-- | gcc/cp/cxx-pretty-print.h | 6 | ||||
-rw-r--r-- | gcc/pretty-print.h | 2 |
10 files changed, 2173 insertions, 873 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 8563818e949..ff2e17591a3 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,84 @@ +2003-08-25 Gabriel Dos Reis <gdr@integrable-solutions.net> + + * pretty-print.h (pp_maybe_newline_and_indent): New macro. + * c-pretty-print.h (c_pretty_printer): Now typedef to the + structure. Be consistent with pretty-print.h abd cxx-pretty-print.h + (struct c_pretty_print_info): Document. Add new fields. + (pp_type_specifier_seq): Rename from pp_c_type_specifier. + (pp_direct_abstract_declarator): New macro. + (pp_ptr_operator): Likewise. + (pp_simple_type_specifier): Likewise. + (pp_expression): Likewise. + (pp_parameter_list): Rename from pp_parameter_declaration. + * c-pretty-print.c (pp_c_whitespace): Now a function. + (pp_c_left_paren): Likewise. + (pp_c_right_paren): Likewise. + (pp_c_dot): Likewise. + (pp_c_ampersand): Likewise. + (pp_c_arrow): Likewise. + (pp_c_semicolon): Likewise. + (pp_c_type_cast): New function. + (pp_c_space_for_pointer_operator): Likewise. + (pp_c_call_argument_list): Likewise. + (pp_c_cv_qualifier): Adjust prototype. + (pp_c_type_qualifier_list): Likewise. + (pp_c_pointer): Likewise. Handle REFERENCE_TYPE here. + (pp_c_type_specifier): Rename from pp_c_simple_type_specifier. + Adjust to follow standard grammar. + (pp_c_specifier_qualifier_list): Adjusr prototype. Handle + REFERENCE_TYPE. Tidy. + (pp_c_parameter_type_list): Adjust prototype. Tidy. + (pp_c_parameter_declaration): Remove. + (pp_c_abstract_declarator): Adjust prototype. + (pp_c_direct_abstract_declarator): Likewise. + (pp_c_type_id): Likewise. + (pp_c_storage_class_specifier): Likewise. + (pp_c_function_specifier): Likewise. + (pp_c_declaration_specifiers): Likewise. + (pp_c_direct_declarator): Likewise. + (pp_c_declarator): Likewise. + (pp_c_declarator): Likewise. + (pp_c_declaration): Likewise. + (pp_c_attributes): Likewise. Tidy. + (pp_c_function_definition): Adjust prototype. + (pp_c_char): Likewise. + (pp_c_string_literal): Likewise. + (pp_c_integer_constant): Likewise. + (pp_c_character_constant): Likewise. + (pp_c_bool_constant): Likewise. + (pp_c_enumeration_constant): Likewise. + (pp_c_floating_constant): Likewise. + (pp_c_constant): Likewise. + (pp_c_identifier): Likewise. + (pp_c_primary_expression): Likewise. Remove TARGET_EXPR case. Tidy. + (pp_c_initializer): Adjust prototype. + (pp_c_init_declarator): Likewise. + (pp_c_initializer_list): Likewise. + (pp_c_id_expression): Likewise. + (pp_c_postfix_expression): Likewise. + (pp_c_expression_list): Likewise. + (pp_c_unary_expression): Likewise. + (pp_c_cast_expression): Likewise. + (pp_c_multiplicative_expression): Likewise. + (pp_c_additive_expression): Likewise. + (pp_c_shift_expression): Likewise. + (pp_c_relational_expression): Likewise. + (pp_c_equality_expression): Likewise. + (pp_c_and_expression): Likewise. + (pp_c_exclusive_or_expression): Likewise. + (pp_c_inclusive_or_expression): Likewise. + (pp_c_logical_and_expression): Likewise. + (pp_c_logical_or_expression): Likewise. + (pp_c_conditional_expression): Likewise. + (pp_c_assignment_expression): Likewise. + (pp_c_expression): Likewise. Tidy. + (pp_c_statement): Likewise. Document. + (pp_c_pretty_printer_init): Adjust prototype. Tidy. + + * c-lang.c (c_initialize_diagnostics): Update. + * c-common.h (strip_pointer_operator): Declare. + * c-common.c (strip_pointer_operator): Define. + 2003-08-25 Mark Mitchell <mark@codesourcery.com> PR c++/8795 diff --git a/gcc/c-common.c b/gcc/c-common.c index b4151faf765..9dd18264ff6 100644 --- a/gcc/c-common.c +++ b/gcc/c-common.c @@ -3547,6 +3547,15 @@ strip_array_types (tree type) return type; } +/* Recursively remove any '*' or '&' operator from TYPE. */ +tree +strip_pointer_operator (tree t) +{ + while (POINTER_TYPE_P (t)) + t = TREE_TYPE (t); + return t; +} + static tree expand_unordered_cmp (tree, tree, enum tree_code, enum tree_code); /* Expand a call to an unordered comparison function such as diff --git a/gcc/c-common.h b/gcc/c-common.h index 73a932cce8e..e7dabd39651 100644 --- a/gcc/c-common.h +++ b/gcc/c-common.h @@ -951,6 +951,7 @@ extern HOST_WIDE_INT c_common_get_alias_set (tree); extern bool c_promoting_integer_type_p (tree); extern int self_promoting_args_p (tree); extern tree strip_array_types (tree); +extern tree strip_pointer_operator (tree); /* This function resets the parsers' state in preparation for parsing a new file. */ diff --git a/gcc/c-lang.c b/gcc/c-lang.c index f8ab4165f4e..93437a3733d 100644 --- a/gcc/c-lang.c +++ b/gcc/c-lang.c @@ -230,7 +230,7 @@ static void c_initialize_diagnostics (diagnostic_context *context) { pretty_printer *base = context->printer; - c_pretty_printer pp = xmalloc (sizeof *pp); + c_pretty_printer *pp = xmalloc (sizeof (c_pretty_printer)); memcpy (pp_base (pp), base, sizeof (pretty_printer)); pp_c_pretty_printer_init (pp); context->printer = (pretty_printer *) pp; diff --git a/gcc/c-pretty-print.c b/gcc/c-pretty-print.c index 6e12d6134d8..922d987ab28 100644 --- a/gcc/c-pretty-print.c +++ b/gcc/c-pretty-print.c @@ -35,29 +35,12 @@ Software Foundation, 59 Temple Place - Suite 330, Boston, MA takes expression or declaration contexts into account. */ -#define pp_c_whitespace(PP) \ - do { \ - pp_space (PP); \ - pp_base (PP)->padding = pp_none; \ - } while (0) #define pp_c_maybe_whitespace(PP) \ do { \ if (pp_base (PP)->padding == pp_before) \ pp_c_whitespace (PP); \ } while (0) -#define pp_c_left_paren(PP) \ - do { \ - pp_left_paren (PP); \ - pp_base (PP)->padding = pp_none; \ - } while (0) - -#define pp_c_right_paren(PP) \ - do { \ - pp_right_paren (PP); \ - pp_base (PP)->padding = pp_none; \ - } while (0) - #define pp_c_left_brace(PP) \ do { \ pp_left_brace (PP); \ @@ -82,64 +65,86 @@ Software Foundation, 59 Temple Place - Suite 330, Boston, MA pp_base (PP)->padding = pp_none; \ } while (0) -#define pp_c_arrow(PP) \ - do { \ - pp_arrow (PP); \ - pp_base (PP)->padding = pp_none; \ - } while (0) - #define pp_c_star(PP) \ do { \ pp_star (PP); \ pp_base (PP)->padding = pp_none; \ } while (0) -#define pp_c_dot(PP) \ - do { \ - pp_dot (PP); \ - pp_base (PP)->padding = pp_none; \ - } while (0) - -#define pp_c_semicolon(PP) \ - do { \ - pp_semicolon (PP); \ - pp_base (PP)->padding = pp_none; \ - } while (0) - /* literal */ -static void pp_c_char (c_pretty_printer, int); -static void pp_c_primary_expression (c_pretty_printer, tree); +static void pp_c_char (c_pretty_printer *, int); /* postfix-expression */ -static void pp_c_initializer_list (c_pretty_printer, tree); - -static void pp_c_multiplicative_expression (c_pretty_printer, tree); -static void pp_c_additive_expression (c_pretty_printer, tree); -static void pp_c_shift_expression (c_pretty_printer, tree); -static void pp_c_relational_expression (c_pretty_printer, tree); -static void pp_c_equality_expression (c_pretty_printer, tree); -static void pp_c_and_expression (c_pretty_printer, tree); -static void pp_c_exclusive_or_expression (c_pretty_printer, tree); -static void pp_c_inclusive_or_expression (c_pretty_printer, tree); -static void pp_c_logical_and_expression (c_pretty_printer, tree); -static void pp_c_conditional_expression (c_pretty_printer, tree); -static void pp_c_assignment_expression (c_pretty_printer, tree); +static void pp_c_initializer_list (c_pretty_printer *, tree); + +static void pp_c_multiplicative_expression (c_pretty_printer *, tree); +static void pp_c_additive_expression (c_pretty_printer *, tree); +static void pp_c_shift_expression (c_pretty_printer *, tree); +static void pp_c_relational_expression (c_pretty_printer *, tree); +static void pp_c_equality_expression (c_pretty_printer *, tree); +static void pp_c_and_expression (c_pretty_printer *, tree); +static void pp_c_exclusive_or_expression (c_pretty_printer *, tree); +static void pp_c_inclusive_or_expression (c_pretty_printer *, tree); +static void pp_c_logical_and_expression (c_pretty_printer *, tree); +static void pp_c_conditional_expression (c_pretty_printer *, tree); +static void pp_c_assignment_expression (c_pretty_printer *, tree); /* declarations. */ -static void pp_c_declaration_specifiers (c_pretty_printer, tree); -static void pp_c_direct_abstract_declarator (c_pretty_printer, tree); -static void pp_c_init_declarator (c_pretty_printer, tree); -static void pp_c_simple_type_specifier (c_pretty_printer, tree); -static void pp_c_parameter_declaration (c_pretty_printer, tree); -static void pp_c_storage_class_specifier (c_pretty_printer, tree); -static void pp_c_function_specifier (c_pretty_printer, tree); - -/* Declarations. */ +/* Helper functions. */ + +void +pp_c_whitespace (c_pretty_printer *pp) +{ + pp_space (pp); + pp_base (pp)->padding = pp_none; +} + +void +pp_c_left_paren (c_pretty_printer *pp) +{ + pp_left_paren (pp); + pp_base (pp)->padding = pp_none; +} + +void +pp_c_right_paren (c_pretty_printer *pp) +{ + pp_right_paren (pp); + pp_base (pp)->padding = pp_none; +} + +void +pp_c_dot (c_pretty_printer *pp) +{ + pp_dot (pp); + pp_base (pp)->padding = pp_none; +} + +void +pp_c_ampersand (c_pretty_printer *pp) +{ + pp_ampersand (pp); + pp_base (pp)->padding = pp_none; +} + +void +pp_c_arrow (c_pretty_printer *pp) +{ + pp_arrow (pp); + pp_base (pp)->padding = pp_none; +} + +void +pp_c_semicolon(c_pretty_printer *pp) +{ + pp_semicolon (pp); + pp_base (pp)->padding = pp_none; +} static void -pp_c_cv_qualifier (c_pretty_printer pp, const char *cv) +pp_c_cv_qualifier (c_pretty_printer *pp, const char *cv) { const char *p = pp_last_position_in_text (pp); if (p != NULL && *p == '*') @@ -147,16 +152,45 @@ pp_c_cv_qualifier (c_pretty_printer pp, const char *cv) pp_c_identifier (pp, cv); } +/* Pretty-print T using the type-cast notation '( type-name )'. */ +static inline void +pp_c_type_cast (c_pretty_printer *pp, tree t) +{ + pp_c_left_paren (pp); + pp_type_id (pp, t); + pp_c_right_paren (pp); +} + +void +pp_c_space_for_pointer_operator (c_pretty_printer *pp, tree t) +{ + if (POINTER_TYPE_P (t)) + { + tree pointee = strip_pointer_operator (TREE_TYPE (t)); + if (TREE_CODE (pointee) != ARRAY_TYPE + && TREE_CODE (pointee) != FUNCTION_TYPE) + pp_c_whitespace (pp); + } +} + + +/* Declarations. */ + /* C++ cv-qualifiers are called type-qualifiers in C. Print out the cv-qualifiers of T. If T is a declaration then it is the cv-qualifier of its type. Take care of possible extensions. - cv-qualifier: + + type-qualifier-list: + type-qualifier + type-qualifier-list type-qualifier + + type-qualifier: const - volatile - restrict - __restrict__ */ + restrict -- C99 + __restrict__ -- GNU C + volatile */ void -pp_c_type_qualifier_list (c_pretty_printer pp, tree t) +pp_c_type_qualifier_list (c_pretty_printer *pp, tree t) { int qualifiers; @@ -176,16 +210,21 @@ pp_c_type_qualifier_list (c_pretty_printer pp, tree t) * type-qualifier-list(opt) * type-qualifier-list(opt) pointer */ static void -pp_c_pointer (c_pretty_printer pp, tree t) +pp_c_pointer (c_pretty_printer *pp, tree t) { if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL) t = TREE_TYPE (t); switch (TREE_CODE (t)) { case POINTER_TYPE: + /* It is easier to handle C++ reference types here. */ + case REFERENCE_TYPE: if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE) pp_c_pointer (pp, TREE_TYPE (t)); - pp_c_star (pp); + if (TREE_CODE (t) == POINTER_TYPE) + pp_c_star (pp); + else + pp_c_ampersand (pp); pp_c_type_qualifier_list (pp, t); break; @@ -194,49 +233,39 @@ pp_c_pointer (c_pretty_printer pp, tree t) } } -/* - simple-type-specifier: - void - char - short - int - long - float - double - signed - unsigned - _Bool -- C99 - _Complex -- C99 - _Imaginary -- C99 - typedef-name. +/* type-specifier: + void + char + short + int + long + float + double + signed + unsigned + _Bool -- C99 + _Complex -- C99 + _Imaginary -- C99 + struct-or-union-specifier + enum-specifier + typedef-name. GNU extensions. simple-type-specifier: __complex__ __vector__ */ -static void -pp_c_simple_type_specifier (c_pretty_printer ppi, tree t) +void +pp_c_type_specifier (c_pretty_printer *pp, tree t) { - enum tree_code code; - - if (DECL_P (t) && TREE_CODE (t) != TYPE_DECL) - t = TREE_TYPE (t); - - code = TREE_CODE (t); + const enum tree_code code = TREE_CODE (t); switch (code) { case ERROR_MARK: - pp_c_identifier (ppi, "<type-error>"); - break; - -#if 0 - case UNKNOWN_TYPE: - pp_c_identifier (ppi, "<unkown-type>"); + pp_c_identifier (pp, "<type-error>"); break; -#endif case IDENTIFIER_NODE: - pp_c_tree_identifier (ppi, t); + pp_c_tree_identifier (pp, t); break; case VOID_TYPE: @@ -244,58 +273,43 @@ pp_c_simple_type_specifier (c_pretty_printer ppi, tree t) case CHAR_TYPE: case INTEGER_TYPE: case REAL_TYPE: - pp_c_simple_type_specifier (ppi, TYPE_NAME (t)); - break; - - case COMPLEX_TYPE: - case VECTOR_TYPE: - pp_c_simple_type_specifier (ppi, TYPE_MAIN_VARIANT (TREE_TYPE (t))); - if (code == COMPLEX_TYPE) - pp_c_identifier (ppi, flag_isoc99 ? "_Complex" : "__complex__"); - else if (code == VECTOR_TYPE) - pp_c_identifier (ppi, "__vector__"); + pp_c_type_specifier (pp, TYPE_NAME (t)); break; case TYPE_DECL: if (DECL_NAME (t)) - pp_id_expression (ppi, t); + pp_id_expression (pp, t); else - pp_c_identifier (ppi, "<typedef-error>"); + pp_c_identifier (pp, "<typedef-error>"); break; case UNION_TYPE: case RECORD_TYPE: case ENUMERAL_TYPE: if (code == UNION_TYPE) - pp_c_identifier (ppi, "union"); + pp_c_identifier (pp, "union"); else if (code == RECORD_TYPE) - pp_c_identifier (ppi, "struct"); + pp_c_identifier (pp, "struct"); else if (code == ENUMERAL_TYPE) - pp_c_identifier (ppi, "enum"); + pp_c_identifier (pp, "enum"); else - pp_c_identifier (ppi, "<tag-error>"); + pp_c_identifier (pp, "<tag-error>"); if (TYPE_NAME (t)) - pp_id_expression (ppi, TYPE_NAME (t)); + pp_id_expression (pp, TYPE_NAME (t)); else - pp_c_identifier (ppi, "<anonymous>"); - break; - - case POINTER_TYPE: - case ARRAY_TYPE: - case FUNCTION_TYPE: - pp_c_simple_type_specifier (ppi, TREE_TYPE (t)); + pp_c_identifier (pp, "<anonymous>"); break; default: - pp_unsupported_tree (ppi, t); + pp_unsupported_tree (pp, t); break; } } /* specifier-qualifier-list: type-specifier specifier-qualifier-list-opt - cv-qualifier specifier-qualifier-list-opt + type-qualifier specifier-qualifier-list-opt Implementation note: Because of the non-linearities in array or @@ -304,18 +318,19 @@ pp_c_simple_type_specifier (c_pretty_printer ppi, tree t) but also the 'pointer' production part of their declarators. The remaining part is done by pp_declarator or pp_c_abstract_declarator. */ void -pp_c_specifier_qualifier_list (c_pretty_printer pp, tree t) +pp_c_specifier_qualifier_list (c_pretty_printer *pp, tree t) { + const enum tree_code code = TREE_CODE (t); + if (TREE_CODE (t) != POINTER_TYPE) pp_c_type_qualifier_list (pp, t); - switch (TREE_CODE (t)) + switch (code) { + case REFERENCE_TYPE: case POINTER_TYPE: { /* Get the types-specifier of this type. */ - tree pointee = TREE_TYPE (t); - while (TREE_CODE (pointee) == POINTER_TYPE) - pointee = TREE_TYPE (pointee); + tree pointee = strip_pointer_operator (TREE_TYPE (t)); pp_c_specifier_qualifier_list (pp, pointee); if (TREE_CODE (pointee) == ARRAY_TYPE || TREE_CODE (pointee) == FUNCTION_TYPE) @@ -323,10 +338,7 @@ pp_c_specifier_qualifier_list (c_pretty_printer pp, tree t) pp_c_whitespace (pp); pp_c_left_paren (pp); } - pp_c_pointer (pp, t); - if (TREE_CODE (pointee) != FUNCTION_TYPE - && TREE_CODE (pointee) != ARRAY_TYPE) - pp_c_whitespace (pp); + pp_ptr_operator (pp, t); } break; @@ -338,12 +350,14 @@ pp_c_specifier_qualifier_list (c_pretty_printer pp, tree t) case VECTOR_TYPE: case COMPLEX_TYPE: pp_c_specifier_qualifier_list (pp, TREE_TYPE (t)); - pp_space (pp); - pp_c_simple_type_specifier (pp, t); + if (code == COMPLEX_TYPE) + pp_c_identifier (pp, flag_isoc99 ? "_Complex" : "__complex__"); + else if (code == VECTOR_TYPE) + pp_c_identifier (pp, "__vector__"); break; default: - pp_c_simple_type_specifier (pp, t); + pp_simple_type_specifier (pp, t); break; } } @@ -359,26 +373,28 @@ pp_c_specifier_qualifier_list (c_pretty_printer pp, tree t) parameter-declaration: declaration-specifiers declarator declaration-specifiers abstract-declarator(opt) */ -static void -pp_c_parameter_type_list (c_pretty_printer pp, tree t) +void +pp_c_parameter_type_list (c_pretty_printer *pp, tree t) { + bool want_parm_decl = DECL_P (t) && !(pp->flags & pp_c_flag_abstract); + tree parms = want_parm_decl ? DECL_ARGUMENTS (t) : TYPE_ARG_TYPES (t); pp_c_left_paren (pp); - if (t == void_list_node) + if (parms == void_list_node) pp_c_identifier (pp, "void"); else { bool first = true; - bool want_parm_decl = t && DECL_P (t); - for ( ; t != NULL && t != void_list_node; t = TREE_CHAIN (t)) + for ( ; parms && parms != void_list_node; parms = TREE_CHAIN (parms)) { if (!first) pp_separate_with (pp, ','); first = false; - pp_declaration_specifiers (pp, want_parm_decl ? t : TREE_VALUE (t)); + pp_declaration_specifiers + (pp, want_parm_decl ? parms : TREE_VALUE (parms)); if (want_parm_decl) - pp_declarator (pp, t); + pp_declarator (pp, parms); else - pp_abstract_declarator (pp, TREE_VALUE (t)); + pp_abstract_declarator (pp, TREE_VALUE (parms)); } } pp_c_right_paren (pp); @@ -388,7 +404,7 @@ pp_c_parameter_type_list (c_pretty_printer pp, tree t) pointer pointer(opt) direct-abstract-declarator */ static inline void -pp_c_abstract_declarator (c_pretty_printer pp, tree t) +pp_c_abstract_declarator (c_pretty_printer *pp, tree t) { if (TREE_CODE (t) == POINTER_TYPE) { @@ -398,7 +414,7 @@ pp_c_abstract_declarator (c_pretty_printer pp, tree t) t = TREE_TYPE (t); } - pp_c_direct_abstract_declarator (pp, t); + pp_direct_abstract_declarator (pp, t); } /* direct-abstract-declarator: @@ -406,26 +422,26 @@ pp_c_abstract_declarator (c_pretty_printer pp, tree t) direct-abstract-declarator(opt) [ assignment-expression(opt) ] direct-abstract-declarator(opt) [ * ] direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */ -static void -pp_c_direct_abstract_declarator (c_pretty_printer pp, tree t) +void +pp_c_direct_abstract_declarator (c_pretty_printer *pp, tree t) { switch (TREE_CODE (t)) { case POINTER_TYPE: - pp_c_abstract_declarator (pp, t); + pp_abstract_declarator (pp, t); break; case FUNCTION_TYPE: - pp_c_parameter_type_list (pp, TYPE_ARG_TYPES (t)); - pp_c_direct_abstract_declarator (pp, TREE_TYPE (t)); + pp_c_parameter_type_list (pp, t); + pp_direct_abstract_declarator (pp, TREE_TYPE (t)); break; case ARRAY_TYPE: pp_c_left_bracket (pp); if (TYPE_DOMAIN (t)) - pp_c_expression (pp, TYPE_MAX_VALUE (TYPE_DOMAIN (t))); + pp_expression (pp, TYPE_MAX_VALUE (TYPE_DOMAIN (t))); pp_c_right_bracket (pp); - pp_c_direct_abstract_declarator (pp, TREE_TYPE (t)); + pp_direct_abstract_declarator (pp, TREE_TYPE (t)); break; case IDENTIFIER_NODE: @@ -447,15 +463,23 @@ pp_c_direct_abstract_declarator (c_pretty_printer pp, tree t) } } +/* type-name: + specifier-qualifier-list abstract-declarator(opt) */ void -pp_c_type_id (c_pretty_printer ppi, tree t) +pp_c_type_id (c_pretty_printer *pp, tree t) { - pp_c_specifier_qualifier_list (ppi, t); - pp_abstract_declarator (ppi, t); + pp_c_specifier_qualifier_list (pp, t); + pp_abstract_declarator (pp, t); } -static inline void -pp_c_storage_class_specifier (c_pretty_printer pp, tree t) +/* storage-class-specifier: + typedef + extern + static + auto + register */ +void +pp_c_storage_class_specifier (c_pretty_printer *pp, tree t) { if (TREE_CODE (t) == TYPE_DECL) pp_c_identifier (pp, "typedef"); @@ -468,8 +492,10 @@ pp_c_storage_class_specifier (c_pretty_printer pp, tree t) } } -static inline void -pp_c_function_specifier (c_pretty_printer pp, tree t) +/* function-specifier: + inline */ +void +pp_c_function_specifier (c_pretty_printer *pp, tree t) { if (TREE_CODE (t) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (t)) pp_c_identifier (pp, "inline"); @@ -480,8 +506,8 @@ pp_c_function_specifier (c_pretty_printer pp, tree t) type-specifier declaration-specifiers(opt) type-qualifier declaration-specifiers(opt) function-specifier declaration-specifiers(opt) */ -static inline void -pp_c_declaration_specifiers (c_pretty_printer pp, tree t) +void +pp_c_declaration_specifiers (c_pretty_printer *pp, tree t) { pp_storage_class_specifier (pp, t); pp_function_specifier (pp, t); @@ -498,7 +524,7 @@ pp_c_declaration_specifiers (c_pretty_printer pp, tree t) direct-declaratpr ( parameter-type-list ) direct-declarator ( identifier-list(opt) ) */ void -pp_c_direct_declarator (c_pretty_printer pp, tree t) +pp_c_direct_declarator (c_pretty_printer *pp, tree t) { switch (TREE_CODE (t)) { @@ -507,25 +533,30 @@ pp_c_direct_declarator (c_pretty_printer pp, tree t) case TYPE_DECL: case FIELD_DECL: case LABEL_DECL: - pp_c_tree_identifier (pp, DECL_NAME (t)); + if (DECL_NAME (t)) + { + pp_c_space_for_pointer_operator (pp, TREE_TYPE (t)); + pp_c_tree_identifier (pp, DECL_NAME (t)); + } case ARRAY_TYPE: case POINTER_TYPE: pp_abstract_declarator (pp, TREE_TYPE (t)); break; case FUNCTION_TYPE: - pp_c_parameter_type_list (pp, TYPE_ARG_TYPES (t)); - pp_c_abstract_declarator (pp, TREE_TYPE (t)); + pp_parameter_list (pp, t); + pp_abstract_declarator (pp, TREE_TYPE (t)); break; case FUNCTION_DECL: + pp_c_space_for_pointer_operator (pp, TREE_TYPE (TREE_TYPE (t))); pp_c_tree_identifier (pp, DECL_NAME (t)); if (pp_c_base (pp)->flags & pp_c_flag_abstract) - pp_c_abstract_declarator (pp, TREE_TYPE (t)); + pp_abstract_declarator (pp, TREE_TYPE (t)); else { - pp_c_parameter_type_list (pp, DECL_ARGUMENTS (t)); - pp_c_abstract_declarator (pp, TREE_TYPE (TREE_TYPE (t))); + pp_parameter_list (pp, t); + pp_abstract_declarator (pp, TREE_TYPE (TREE_TYPE (t))); } break; @@ -546,7 +577,7 @@ pp_c_direct_declarator (c_pretty_printer pp, tree t) /* declarator: pointer(opt) direct-declarator */ void -pp_c_declarator (c_pretty_printer pp, tree t) +pp_c_declarator (c_pretty_printer *pp, tree t) { switch (TREE_CODE (t)) { @@ -574,41 +605,18 @@ pp_c_declarator (c_pretty_printer pp, tree t) } } -/* init-declarator: - declarator: - declarator = initializer */ -static inline void -pp_c_init_declarator (c_pretty_printer pp, tree t) -{ - pp_declarator (pp, t); - if (DECL_INITIAL (t)) - { - pp_space (pp); - pp_equal (pp); - pp_space (pp); - pp_c_initializer (pp, DECL_INITIAL (t)); - } -} - /* declaration: declaration-specifiers init-declarator-list(opt) ; */ - void -pp_c_declaration (c_pretty_printer pp, tree t) +pp_c_declaration (c_pretty_printer *pp, tree t) { pp_declaration_specifiers (pp, t); pp_c_init_declarator (pp, t); } -static void -pp_c_parameter_declaration (c_pretty_printer pp, tree t) -{ - pp_unsupported_tree (pp, t); -} - /* Pretty-print ATTRIBUTES using GNU C extension syntax. */ void -pp_c_attributes (c_pretty_printer pp, tree attributes) +pp_c_attributes (c_pretty_printer *pp, tree attributes) { if (attributes == NULL_TREE) return; @@ -620,11 +628,7 @@ pp_c_attributes (c_pretty_printer pp, tree attributes) { pp_tree_identifier (pp, TREE_PURPOSE (attributes)); if (TREE_VALUE (attributes)) - { - pp_c_left_paren (pp); - pp_c_expression_list (pp, TREE_VALUE (attributes)); - pp_c_right_paren (pp); - } + pp_c_call_argument_list (pp, TREE_VALUE (attributes)); if (TREE_CHAIN (attributes)) pp_separate_with (pp, ','); @@ -636,7 +640,7 @@ pp_c_attributes (c_pretty_printer pp, tree attributes) /* function-definition: declaration-specifiers declarator compound-statement */ void -pp_c_function_definition (c_pretty_printer pp, tree t) +pp_c_function_definition (c_pretty_printer *pp, tree t) { pp_declaration_specifiers (pp, t); pp_declarator (pp, t); @@ -651,64 +655,64 @@ pp_c_function_definition (c_pretty_printer pp, tree t) /* Print out a c-char. */ static void -pp_c_char (c_pretty_printer ppi, int c) +pp_c_char (c_pretty_printer *pp, int c) { switch (c) { case TARGET_NEWLINE: - pp_string (ppi, "\\n"); + pp_string (pp, "\\n"); break; case TARGET_TAB: - pp_string (ppi, "\\t"); + pp_string (pp, "\\t"); break; case TARGET_VT: - pp_string (ppi, "\\v"); + pp_string (pp, "\\v"); break; case TARGET_BS: - pp_string (ppi, "\\b"); + pp_string (pp, "\\b"); break; case TARGET_CR: - pp_string (ppi, "\\r"); + pp_string (pp, "\\r"); break; case TARGET_FF: - pp_string (ppi, "\\f"); + pp_string (pp, "\\f"); break; case TARGET_BELL: - pp_string (ppi, "\\a"); + pp_string (pp, "\\a"); break; case '\\': - pp_string (ppi, "\\\\"); + pp_string (pp, "\\\\"); break; case '\'': - pp_string (ppi, "\\'"); + pp_string (pp, "\\'"); break; case '\"': - pp_string (ppi, "\\\""); + pp_string (pp, "\\\""); break; default: if (ISPRINT (c)) - pp_character (ppi, c); + pp_character (pp, c); else - pp_scalar (ppi, "\\%03o", (unsigned) c); + pp_scalar (pp, "\\%03o", (unsigned) c); break; } } /* Print out a STRING literal. */ void -pp_c_string_literal (c_pretty_printer ppi, tree s) +pp_c_string_literal (c_pretty_printer *pp, tree s) { const char *p = TREE_STRING_POINTER (s); int n = TREE_STRING_LENGTH (s) - 1; int i; - pp_doublequote (ppi); + pp_doublequote (pp); for (i = 0; i < n; ++i) - pp_c_char (ppi, p[i]); - pp_doublequote (ppi); + pp_c_char (pp, p[i]); + pp_doublequote (pp); } static void -pp_c_integer_constant (c_pretty_printer pp, tree i) +pp_c_integer_constant (c_pretty_printer *pp, tree i) { if (host_integerp (i, 0)) pp_wide_integer (pp, TREE_INT_CST_LOW (i)); @@ -729,7 +733,7 @@ pp_c_integer_constant (c_pretty_printer pp, tree i) /* Print out a CHARACTER literal. */ static inline void -pp_c_character_constant (c_pretty_printer pp, tree c) +pp_c_character_constant (c_pretty_printer *pp, tree c) { tree type = TREE_TYPE (c); if (type == wchar_type_node) @@ -744,7 +748,7 @@ pp_c_character_constant (c_pretty_printer pp, tree c) /* Print out a BOOLEAN literal. */ static void -pp_c_bool_constant (c_pretty_printer pp, tree b) +pp_c_bool_constant (c_pretty_printer *pp, tree b) { if (b == boolean_false_node) { @@ -775,7 +779,7 @@ pp_c_bool_constant (c_pretty_printer pp, tree b) print out the type-id part of the cast-expression -- the casted value is then printed by pp_c_integer_literal. */ static bool -pp_c_enumeration_constant (c_pretty_printer ppi, tree e) +pp_c_enumeration_constant (c_pretty_printer *pp, tree e) { bool value_is_named = true; tree type = TREE_TYPE (e); @@ -788,13 +792,11 @@ pp_c_enumeration_constant (c_pretty_printer ppi, tree e) ; if (value != NULL_TREE) - pp_id_expression (ppi, TREE_PURPOSE (value)); + pp_id_expression (pp, TREE_PURPOSE (value)); else { /* Value must have been cast. */ - pp_c_left_paren (ppi); - pp_type_id (ppi, type); - pp_c_right_paren (ppi); + pp_c_type_cast (pp, type); value_is_named = false; } @@ -803,7 +805,7 @@ pp_c_enumeration_constant (c_pretty_printer ppi, tree e) /* Print out a REAL value. */ static inline void -pp_c_floating_constant (c_pretty_printer pp, tree r) +pp_c_floating_constant (c_pretty_printer *pp, tree r) { real_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_REAL_CST (r), sizeof (pp_buffer (pp)->digit_buffer), 0, 1); @@ -816,7 +818,7 @@ pp_c_floating_constant (c_pretty_printer pp, tree r) enumeration-constant chatracter-constant */ void -pp_c_constant (c_pretty_printer pp, tree e) +pp_c_constant (c_pretty_printer *pp, tree e) { switch (TREE_CODE (e)) { @@ -850,7 +852,7 @@ pp_c_constant (c_pretty_printer pp, tree e) } void -pp_c_identifier (c_pretty_printer pp, const char *id) +pp_c_identifier (c_pretty_printer *pp, const char *id) { pp_c_maybe_whitespace (pp); pp_identifier (pp, id); @@ -863,8 +865,8 @@ pp_c_identifier (c_pretty_printer pp, const char *id) constant string-literal ( expression ) */ -static void -pp_c_primary_expression (c_pretty_printer ppi, tree e) +void +pp_c_primary_expression (c_pretty_printer *pp, tree e) { switch (TREE_CODE (e)) { @@ -877,50 +879,34 @@ pp_c_primary_expression (c_pretty_printer ppi, tree e) e = DECL_NAME (e); /* Fall through. */ case IDENTIFIER_NODE: - pp_c_tree_identifier (ppi, e); + pp_c_tree_identifier (pp, e); break; case ERROR_MARK: - pp_c_identifier (ppi, "<erroneous-expression>"); + pp_c_identifier (pp, "<erroneous-expression>"); break; case RESULT_DECL: - pp_c_identifier (ppi, "<return-value>"); + pp_c_identifier (pp, "<return-value>"); break; case INTEGER_CST: case REAL_CST: case STRING_CST: - pp_c_constant (ppi, e); + pp_c_constant (pp, e); break; - case TARGET_EXPR: - pp_c_left_paren (ppi); - pp_c_identifier (ppi, "__builtin_memcpy"); - pp_c_left_paren (ppi); - pp_ampersand (ppi); - pp_primary_expression (ppi, TREE_OPERAND (e, 0)); - pp_separate_with (ppi, ','); - pp_ampersand (ppi); - pp_initializer (ppi, TREE_OPERAND (e, 1)); - if (TREE_OPERAND (e, 2)) - { - pp_separate_with (ppi, ','); - pp_c_expression (ppi, TREE_OPERAND (e, 2)); - } - pp_c_right_paren (ppi); - case STMT_EXPR: - pp_c_left_paren (ppi); - pp_statement (ppi, STMT_EXPR_STMT (e)); - pp_c_right_paren (ppi); + pp_c_left_paren (pp); + pp_statement (pp, STMT_EXPR_STMT (e)); + pp_c_right_paren (pp); break; default: /* FIXME: Make sure we won't get into an infinie loop. */ - pp_c_left_paren (ppi); - pp_c_expression (ppi, e); - pp_c_right_paren (ppi); + pp_c_left_paren (pp); + pp_expression (pp, e); + pp_c_right_paren (pp); break; } } @@ -931,23 +917,53 @@ pp_c_primary_expression (c_pretty_printer ppi, tree e) { initializer-list } { initializer-list , } */ -void -pp_c_initializer (c_pretty_printer ppi, tree e) +static void +pp_c_initializer (c_pretty_printer *pp, tree e) { if (TREE_CODE (e) == CONSTRUCTOR) { enum tree_code code = TREE_CODE (TREE_TYPE (e)); if (code == RECORD_TYPE || code == UNION_TYPE || code == ARRAY_TYPE) { - pp_c_left_brace (ppi); - pp_c_initializer_list (ppi, e); - pp_c_right_brace (ppi); + pp_c_left_brace (pp); + pp_c_initializer_list (pp, e); + pp_c_right_brace (pp); } else - pp_unsupported_tree (ppi, TREE_OPERAND (e, 1)); + pp_unsupported_tree (pp, TREE_OPERAND (e, 1)); } else - pp_assignment_expression (ppi, e); + pp_expression (pp, e); +} + +/* init-declarator: + declarator: + declarator = initializer */ +void +pp_c_init_declarator (c_pretty_printer *pp, tree t) +{ + pp_declarator (pp, t); + if (DECL_INITIAL (t)) + { + tree init = DECL_INITIAL (t); + /* This C++ bit is handled here because it is easier to do so. + In templates, the C++ parser builds a TREE_LIST for a + direct-initialization; the TREE_PURPOSE is the variable to + initialize and the TREE_VALUE is the initializer. */ + if (TREE_CODE (init) == TREE_LIST) + { + pp_c_left_paren (pp); + pp_expression (pp, TREE_VALUE (init)); + pp_right_paren (pp); + } + else + { + pp_space (pp); + pp_equal (pp); + pp_space (pp); + pp_c_initializer (pp, init); + } + } } /* initializer-list: @@ -965,7 +981,7 @@ pp_c_initializer (c_pretty_printer ppi, tree e) [ constant-expression ] identifier */ static void -pp_c_initializer_list (c_pretty_printer ppi, tree e) +pp_c_initializer_list (c_pretty_printer *pp, tree e) { tree type = TREE_TYPE (e); const enum tree_code code = TREE_CODE (type); @@ -977,26 +993,26 @@ pp_c_initializer_list (c_pretty_printer ppi, tree e) { if (code == RECORD_TYPE || code == UNION_TYPE) { - pp_c_dot (ppi); - pp_c_primary_expression (ppi, TREE_PURPOSE (init)); + pp_c_dot (pp); + pp_c_primary_expression (pp, TREE_PURPOSE (init)); } else { - pp_c_left_bracket (ppi); + pp_c_left_bracket (pp); if (TREE_PURPOSE (init)) - pp_c_constant (ppi, TREE_PURPOSE (init)); - pp_c_right_bracket (ppi); + pp_c_constant (pp, TREE_PURPOSE (init)); + pp_c_right_bracket (pp); } - pp_c_whitespace (ppi); - pp_equal (ppi); - pp_c_whitespace (ppi); - pp_initializer (ppi, TREE_VALUE (init)); + pp_c_whitespace (pp); + pp_equal (pp); + pp_c_whitespace (pp); + pp_initializer (pp, TREE_VALUE (init)); if (TREE_CHAIN (init)) - pp_separate_with (ppi, ','); + pp_separate_with (pp, ','); } } else - pp_unsupported_tree (ppi, type); + pp_unsupported_tree (pp, type); } /* This is a convenient function, used to bridge gap between C and C++ @@ -1005,7 +1021,7 @@ pp_c_initializer_list (c_pretty_printer ppi, tree e) id-expression: identifier */ void -pp_c_id_expression (c_pretty_printer pp, tree t) +pp_c_id_expression (c_pretty_printer *pp, tree t) { switch (TREE_CODE (t)) { @@ -1038,43 +1054,41 @@ pp_c_id_expression (c_pretty_printer pp, tree t) ( type-name ) { initializer-list } ( type-name ) { initializer-list , } */ void -pp_c_postfix_expression (c_pretty_printer ppi, tree e) +pp_c_postfix_expression (c_pretty_printer *pp, tree e) { enum tree_code code = TREE_CODE (e); switch (code) { case POSTINCREMENT_EXPR: case POSTDECREMENT_EXPR: - pp_postfix_expression (ppi, TREE_OPERAND (e, 0)); - pp_identifier (ppi, code == POSTINCREMENT_EXPR ? "++" : "--"); + pp_postfix_expression (pp, TREE_OPERAND (e, 0)); + pp_identifier (pp, code == POSTINCREMENT_EXPR ? "++" : "--"); break; case ARROW_EXPR: - pp_postfix_expression (ppi, TREE_OPERAND (e, 0)); - pp_c_arrow (ppi); + pp_postfix_expression (pp, TREE_OPERAND (e, 0)); + pp_c_arrow (pp); break; case ARRAY_REF: - pp_postfix_expression (ppi, TREE_OPERAND (e, 0)); - pp_c_left_bracket (ppi); - pp_c_expression (ppi, TREE_OPERAND (e, 1)); - pp_c_right_bracket (ppi); + pp_postfix_expression (pp, TREE_OPERAND (e, 0)); + pp_c_left_bracket (pp); + pp_expression (pp, TREE_OPERAND (e, 1)); + pp_c_right_bracket (pp); break; case CALL_EXPR: - pp_postfix_expression (ppi, TREE_OPERAND (e, 0)); - pp_c_left_paren (ppi); - pp_c_expression_list (ppi, TREE_OPERAND (e, 1)); - pp_c_right_paren (ppi); + pp_postfix_expression (pp, TREE_OPERAND (e, 0)); + pp_c_call_argument_list (pp, TREE_OPERAND (e, 1)); break; case ABS_EXPR: case FFS_EXPR: - pp_c_identifier (ppi, + pp_c_identifier (pp, code == ABS_EXPR ? "__builtin_abs" : "__builtin_ffs"); - pp_c_left_paren (ppi); - pp_c_expression (ppi, TREE_OPERAND (e, 0)); - pp_c_right_paren (ppi); + pp_c_left_paren (pp); + pp_expression (pp, TREE_OPERAND (e, 0)); + pp_c_right_paren (pp); break; case COMPONENT_REF: @@ -1082,86 +1096,94 @@ pp_c_postfix_expression (c_pretty_printer ppi, tree e) tree object = TREE_OPERAND (e, 0); if (TREE_CODE (object) == INDIRECT_REF) { - pp_postfix_expression (ppi, TREE_OPERAND (object, 0)); - pp_c_arrow (ppi); + pp_postfix_expression (pp, TREE_OPERAND (object, 0)); + pp_c_arrow (pp); } else { - pp_postfix_expression (ppi, object); - pp_c_dot (ppi); + pp_postfix_expression (pp, object); + pp_c_dot (pp); } - pp_c_expression (ppi, TREE_OPERAND (e, 1)); + pp_expression (pp, TREE_OPERAND (e, 1)); } break; case COMPLEX_CST: case VECTOR_CST: case COMPLEX_EXPR: - pp_c_left_paren (ppi); - pp_type_id (ppi, TREE_TYPE (e)); - pp_c_right_paren (ppi); - pp_c_left_brace (ppi); + pp_c_type_cast (pp, TREE_TYPE (e)); + pp_c_left_brace (pp); if (code == COMPLEX_CST) { - pp_c_expression (ppi, TREE_REALPART (e)); - pp_separate_with (ppi, ','); - pp_c_expression (ppi, TREE_IMAGPART (e)); + pp_expression (pp, TREE_REALPART (e)); + pp_separate_with (pp, ','); + pp_expression (pp, TREE_IMAGPART (e)); } else if (code == VECTOR_CST) - pp_c_expression_list (ppi, TREE_VECTOR_CST_ELTS (e)); + pp_c_expression_list (pp, TREE_VECTOR_CST_ELTS (e)); else if (code == COMPLEX_EXPR) { - pp_c_expression (ppi, TREE_OPERAND (e, 0)); - pp_separate_with (ppi, ','); - pp_c_expression (ppi, TREE_OPERAND (e, 1)); + pp_expression (pp, TREE_OPERAND (e, 0)); + pp_separate_with (pp, ','); + pp_expression (pp, TREE_OPERAND (e, 1)); } - pp_c_right_brace (ppi); + pp_c_right_brace (pp); break; case COMPOUND_LITERAL_EXPR: e = DECL_INITIAL (e); /* Fall through. */ case CONSTRUCTOR: - pp_initializer (ppi, e); + pp_initializer (pp, e); break; case VA_ARG_EXPR: - pp_c_identifier (ppi, "__builtin_va_arg"); - pp_c_left_paren (ppi); - pp_assignment_expression (ppi, TREE_OPERAND (e, 0)); - pp_separate_with (ppi, ','); - pp_type_id (ppi, TREE_TYPE (e)); - pp_c_right_paren (ppi); + pp_c_identifier (pp, "__builtin_va_arg"); + pp_c_left_paren (pp); + pp_assignment_expression (pp, TREE_OPERAND (e, 0)); + pp_separate_with (pp, ','); + pp_type_id (pp, TREE_TYPE (e)); + pp_c_right_paren (pp); break; case ADDR_EXPR: if (TREE_CODE (TREE_OPERAND (e, 0)) == FUNCTION_DECL) { - pp_c_id_expression (ppi, TREE_OPERAND (e, 0)); + pp_c_id_expression (pp, TREE_OPERAND (e, 0)); break; } /* else fall through. */ default: - pp_primary_expression (ppi, e); + pp_primary_expression (pp, e); break; } } /* Print out an expression-list; E is expected to be a TREE_LIST */ void -pp_c_expression_list (c_pretty_printer ppi, tree e) +pp_c_expression_list (c_pretty_printer *pp, tree e) { for (; e != NULL_TREE; e = TREE_CHAIN (e)) { - pp_c_assignment_expression (ppi, TREE_VALUE (e)); + pp_expression (pp, TREE_VALUE (e)); if (TREE_CHAIN (e)) - pp_separate_with (ppi, ','); + pp_separate_with (pp, ','); } } +/* Print out an expression-list in parens, as in a function call. */ +void +pp_c_call_argument_list (c_pretty_printer *pp, tree t) +{ + pp_c_left_paren (pp); + if (t && TREE_CODE (t) == TREE_LIST) + pp_c_expression_list (pp, t); + pp_c_right_paren (pp); +} + /* unary-expression: postfix-expression ++ cast-expression @@ -1180,15 +1202,15 @@ pp_c_expression_list (c_pretty_printer ppi, tree e) __real__ unary-expression __imag__ unary-expression */ void -pp_c_unary_expression (c_pretty_printer ppi, tree e) +pp_c_unary_expression (c_pretty_printer *pp, tree e) { enum tree_code code = TREE_CODE (e); switch (code) { case PREINCREMENT_EXPR: case PREDECREMENT_EXPR: - pp_identifier (ppi, code == PREINCREMENT_EXPR ? "++" : "--"); - pp_c_unary_expression (ppi, TREE_OPERAND (e, 0)); + pp_identifier (pp, code == PREINCREMENT_EXPR ? "++" : "--"); + pp_c_unary_expression (pp, TREE_OPERAND (e, 0)); break; case ADDR_EXPR: @@ -1199,66 +1221,68 @@ pp_c_unary_expression (c_pretty_printer ppi, tree e) case CONJ_EXPR: /* String literal are used by address. */ if (code == ADDR_EXPR && TREE_CODE (TREE_OPERAND (e, 0)) != STRING_CST) - pp_ampersand (ppi); + pp_ampersand (pp); else if (code == INDIRECT_REF) - pp_c_star (ppi); + pp_c_star (pp); else if (code == NEGATE_EXPR) - pp_minus (ppi); + pp_minus (pp); else if (code == BIT_NOT_EXPR || code == CONJ_EXPR) - pp_complement (ppi); + pp_complement (pp); else if (code == TRUTH_NOT_EXPR) - pp_exclamation (ppi); - pp_c_cast_expression (ppi, TREE_OPERAND (e, 0)); + pp_exclamation (pp); + pp_c_cast_expression (pp, TREE_OPERAND (e, 0)); break; case SIZEOF_EXPR: case ALIGNOF_EXPR: - pp_c_identifier (ppi, code == SIZEOF_EXPR ? "sizeof" : "__alignof__"); - pp_c_whitespace (ppi); + pp_c_identifier (pp, code == SIZEOF_EXPR ? "sizeof" : "__alignof__"); + pp_c_whitespace (pp); if (TYPE_P (TREE_OPERAND (e, 0))) - { - pp_c_left_paren (ppi); - pp_type_id (ppi, TREE_OPERAND (e, 0)); - pp_c_right_paren (ppi); - } + pp_c_type_cast (pp, TREE_OPERAND (e, 0)); else - pp_unary_expression (ppi, TREE_OPERAND (e, 0)); + pp_unary_expression (pp, TREE_OPERAND (e, 0)); break; case REALPART_EXPR: case IMAGPART_EXPR: - pp_c_identifier (ppi, code == REALPART_EXPR ? "__real__" : "__imag__"); - pp_c_whitespace (ppi); - pp_unary_expression (ppi, TREE_OPERAND (e, 0)); + pp_c_identifier (pp, code == REALPART_EXPR ? "__real__" : "__imag__"); + pp_c_whitespace (pp); + pp_unary_expression (pp, TREE_OPERAND (e, 0)); break; default: - pp_postfix_expression (ppi, e); + pp_postfix_expression (pp, e); break; } } +/* cast-expression: + unary-expression + ( type-name ) cast-expression */ void -pp_c_cast_expression (c_pretty_printer ppi, tree e) +pp_c_cast_expression (c_pretty_printer *pp, tree e) { switch (TREE_CODE (e)) { case FLOAT_EXPR: case FIX_TRUNC_EXPR: case CONVERT_EXPR: - pp_c_left_paren (ppi); - pp_type_id (ppi, TREE_TYPE (e)); - pp_c_right_paren (ppi); - pp_c_cast_expression (ppi, TREE_OPERAND (e, 0)); + pp_c_type_cast (pp, TREE_TYPE (e)); + pp_c_cast_expression (pp, TREE_OPERAND (e, 0)); break; default: - pp_unary_expression (ppi, e); + pp_unary_expression (pp, e); } } +/* multiplicative-expression: + cast-expression + multiplicative-expression * cast-expression + multiplicative-expression / cast-expression + multiplicative-expression % cast-expression */ static void -pp_c_multiplicative_expression (c_pretty_printer ppi, tree e) +pp_c_multiplicative_expression (c_pretty_printer *pp, tree e) { enum tree_code code = TREE_CODE (e); switch (code) @@ -1266,70 +1290,84 @@ pp_c_multiplicative_expression (c_pretty_printer ppi, tree e) case MULT_EXPR: case TRUNC_DIV_EXPR: case TRUNC_MOD_EXPR: - pp_multiplicative_expression (ppi, TREE_OPERAND (e, 0)); - pp_c_whitespace (ppi); + pp_multiplicative_expression (pp, TREE_OPERAND (e, 0)); + pp_c_whitespace (pp); if (code == MULT_EXPR) - pp_c_star (ppi); + pp_c_star (pp); else if (code == TRUNC_DIV_EXPR) - pp_slash (ppi); + pp_slash (pp); else - pp_modulo (ppi); - pp_c_whitespace (ppi); - pp_c_cast_expression (ppi, TREE_OPERAND (e, 1)); + pp_modulo (pp); + pp_c_whitespace (pp); + pp_c_cast_expression (pp, TREE_OPERAND (e, 1)); break; default: - pp_c_cast_expression (ppi, e); + pp_c_cast_expression (pp, e); break; } } +/* additive-expression: + multiplicative-expression + additive-expression + multiplicative-expression + additive-expression - multiplicative-expression */ static inline void -pp_c_additive_expression (c_pretty_printer ppi, tree e) +pp_c_additive_expression (c_pretty_printer *pp, tree e) { enum tree_code code = TREE_CODE (e); switch (code) { case PLUS_EXPR: case MINUS_EXPR: - pp_c_additive_expression (ppi, TREE_OPERAND (e, 0)); - pp_c_whitespace (ppi); + pp_c_additive_expression (pp, TREE_OPERAND (e, 0)); + pp_c_whitespace (pp); if (code == PLUS_EXPR) - pp_plus (ppi); + pp_plus (pp); else - pp_minus (ppi); - pp_c_whitespace (ppi); - pp_multiplicative_expression (ppi, TREE_OPERAND (e, 1)); + pp_minus (pp); + pp_c_whitespace (pp); + pp_multiplicative_expression (pp, TREE_OPERAND (e, 1)); break; default: - pp_multiplicative_expression (ppi, e); + pp_multiplicative_expression (pp, e); break; } } +/* additive-expression: + additive-expression + shift-expression << additive-expression + shift-expression >> additive-expression */ static inline void -pp_c_shift_expression (c_pretty_printer ppi, tree e) +pp_c_shift_expression (c_pretty_printer *pp, tree e) { enum tree_code code = TREE_CODE (e); switch (code) { case LSHIFT_EXPR: case RSHIFT_EXPR: - pp_c_shift_expression (ppi, TREE_OPERAND (e, 0)); - pp_c_whitespace (ppi); - pp_identifier (ppi, code == LSHIFT_EXPR ? "<<" : ">>"); - pp_c_whitespace (ppi); - pp_c_additive_expression (ppi, TREE_OPERAND (e, 1)); + pp_c_shift_expression (pp, TREE_OPERAND (e, 0)); + pp_c_whitespace (pp); + pp_identifier (pp, code == LSHIFT_EXPR ? "<<" : ">>"); + pp_c_whitespace (pp); + pp_c_additive_expression (pp, TREE_OPERAND (e, 1)); break; default: - pp_c_additive_expression (ppi, e); + pp_c_additive_expression (pp, e); } } +/* relational-expression: + shift-expression + relational-expression < shift-expression + relational-expression > shift-expression + relational-expression <= shift-expression + relational-expression >= shift-expression */ static void -pp_c_relational_expression (c_pretty_printer ppi, tree e) +pp_c_relational_expression (c_pretty_printer *pp, tree e) { enum tree_code code = TREE_CODE (e); switch (code) @@ -1338,176 +1376,212 @@ pp_c_relational_expression (c_pretty_printer ppi, tree e) case GT_EXPR: case LE_EXPR: case GE_EXPR: - pp_c_relational_expression (ppi, TREE_OPERAND (e, 0)); - pp_c_whitespace (ppi); + pp_c_relational_expression (pp, TREE_OPERAND (e, 0)); + pp_c_whitespace (pp); if (code == LT_EXPR) - pp_less (ppi); + pp_less (pp); else if (code == GT_EXPR) - pp_greater (ppi); + pp_greater (pp); else if (code == LE_EXPR) - pp_identifier (ppi, "<="); + pp_identifier (pp, "<="); else if (code == GE_EXPR) - pp_identifier (ppi, ">="); - pp_c_whitespace (ppi); - pp_c_shift_expression (ppi, TREE_OPERAND (e, 1)); + pp_identifier (pp, ">="); + pp_c_whitespace (pp); + pp_c_shift_expression (pp, TREE_OPERAND (e, 1)); break; default: - pp_c_shift_expression (ppi, e); + pp_c_shift_expression (pp, e); break; } } +/* equality-expression: + relational-expression + equality-expression == relational-expression + equality-equality != relational-expression */ static inline void -pp_c_equality_expression (c_pretty_printer ppi, tree e) +pp_c_equality_expression (c_pretty_printer *pp, tree e) { enum tree_code code = TREE_CODE (e); switch (code) { case EQ_EXPR: case NE_EXPR: - pp_c_equality_expression (ppi, TREE_OPERAND (e, 0)); - pp_c_whitespace (ppi); - pp_identifier (ppi, code == EQ_EXPR ? "==" : "!="); - pp_c_whitespace (ppi); - pp_c_relational_expression (ppi, TREE_OPERAND (e, 1)); + pp_c_equality_expression (pp, TREE_OPERAND (e, 0)); + pp_c_whitespace (pp); + pp_identifier (pp, code == EQ_EXPR ? "==" : "!="); + pp_c_whitespace (pp); + pp_c_relational_expression (pp, TREE_OPERAND (e, 1)); break; default: - pp_c_relational_expression (ppi, e); + pp_c_relational_expression (pp, e); break; } } +/* AND-expression: + equality-expression + AND-expression & equality-equality */ static inline void -pp_c_and_expression (c_pretty_printer ppi, tree e) +pp_c_and_expression (c_pretty_printer *pp, tree e) { if (TREE_CODE (e) == BIT_AND_EXPR) { - pp_c_and_expression (ppi, TREE_OPERAND (e, 0)); - pp_c_whitespace (ppi); - pp_ampersand (ppi); - pp_c_whitespace (ppi); - pp_c_equality_expression (ppi, TREE_OPERAND (e, 1)); + pp_c_and_expression (pp, TREE_OPERAND (e, 0)); + pp_c_whitespace (pp); + pp_ampersand (pp); + pp_c_whitespace (pp); + pp_c_equality_expression (pp, TREE_OPERAND (e, 1)); } else - pp_c_equality_expression (ppi, e); + pp_c_equality_expression (pp, e); } +/* exclusive-OR-expression: + AND-expression + exclusive-OR-expression ^ AND-expression */ static inline void -pp_c_exclusive_or_expression (c_pretty_printer ppi, tree e) +pp_c_exclusive_or_expression (c_pretty_printer *pp, tree e) { if (TREE_CODE (e) == BIT_XOR_EXPR) { - pp_c_exclusive_or_expression (ppi, TREE_OPERAND (e, 0)); - pp_c_maybe_whitespace (ppi); - pp_carret (ppi); - pp_c_whitespace (ppi); - pp_c_and_expression (ppi, TREE_OPERAND (e, 1)); + pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0)); + pp_c_maybe_whitespace (pp); + pp_carret (pp); + pp_c_whitespace (pp); + pp_c_and_expression (pp, TREE_OPERAND (e, 1)); } else - pp_c_and_expression (ppi, e); + pp_c_and_expression (pp, e); } +/* inclusive-OR-expression: + exclusive-OR-expression + inclusive-OR-expression | exclusive-OR-expression */ static inline void -pp_c_inclusive_or_expression (c_pretty_printer ppi, tree e) +pp_c_inclusive_or_expression (c_pretty_printer *pp, tree e) { if (TREE_CODE (e) == BIT_IOR_EXPR) { - pp_c_exclusive_or_expression (ppi, TREE_OPERAND (e, 0)); - pp_c_whitespace (ppi); - pp_bar (ppi); - pp_c_whitespace (ppi); - pp_c_exclusive_or_expression (ppi, TREE_OPERAND (e, 1)); + pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0)); + pp_c_whitespace (pp); + pp_bar (pp); + pp_c_whitespace (pp); + pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 1)); } else - pp_c_exclusive_or_expression (ppi, e); + pp_c_exclusive_or_expression (pp, e); } +/* logical-AND-expression: + inclusive-OR-expression + logical-AND-expression && inclusive-OR-expression */ static inline void -pp_c_logical_and_expression (c_pretty_printer ppi, tree e) +pp_c_logical_and_expression (c_pretty_printer *pp, tree e) { if (TREE_CODE (e) == TRUTH_ANDIF_EXPR) { - pp_c_logical_and_expression (ppi, TREE_OPERAND (e, 0)); - pp_c_whitespace (ppi); - pp_identifier (ppi, "&&"); - pp_c_whitespace (ppi); - pp_c_inclusive_or_expression (ppi, TREE_OPERAND (e, 1)); + pp_c_logical_and_expression (pp, TREE_OPERAND (e, 0)); + pp_c_whitespace (pp); + pp_identifier (pp, "&&"); + pp_c_whitespace (pp); + pp_c_inclusive_or_expression (pp, TREE_OPERAND (e, 1)); } else - pp_c_inclusive_or_expression (ppi, e); + pp_c_inclusive_or_expression (pp, e); } +/* logical-OR-expression: + logical-AND-expression + logical-OR-expression || logical-AND-expression */ void -pp_c_logical_or_expression (c_pretty_printer ppi, tree e) +pp_c_logical_or_expression (c_pretty_printer *pp, tree e) { if (TREE_CODE (e) == TRUTH_ORIF_EXPR) { - pp_c_logical_or_expression (ppi, TREE_OPERAND (e, 0)); - pp_c_whitespace (ppi); - pp_identifier (ppi, "||"); - pp_c_whitespace (ppi); - pp_c_logical_and_expression (ppi, TREE_OPERAND (e, 1)); + pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0)); + pp_c_whitespace (pp); + pp_identifier (pp, "||"); + pp_c_whitespace (pp); + pp_c_logical_and_expression (pp, TREE_OPERAND (e, 1)); } else - pp_c_logical_and_expression (ppi, e); + pp_c_logical_and_expression (pp, e); } +/* conditional-expression: + logical-OR-expression + logical-OR-expression ? expression : conditional-expression */ static void -pp_c_conditional_expression (c_pretty_printer ppi, tree e) +pp_c_conditional_expression (c_pretty_printer *pp, tree e) { if (TREE_CODE (e) == COND_EXPR) { - pp_c_logical_or_expression (ppi, TREE_OPERAND (e, 0)); - pp_c_whitespace (ppi); - pp_question (ppi); - pp_c_whitespace (ppi); - pp_c_expression (ppi, TREE_OPERAND (e, 1)); - pp_c_maybe_whitespace (ppi); - pp_colon (ppi); - pp_c_whitespace (ppi); - pp_c_conditional_expression (ppi, TREE_OPERAND (e, 2)); + pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0)); + pp_c_whitespace (pp); + pp_question (pp); + pp_c_whitespace (pp); + pp_expression (pp, TREE_OPERAND (e, 1)); + pp_c_whitespace (pp); + pp_colon (pp); + pp_c_whitespace (pp); + pp_c_conditional_expression (pp, TREE_OPERAND (e, 2)); } else - pp_c_logical_or_expression (ppi, e); + pp_c_logical_or_expression (pp, e); } -/* Pretty-print a C assignment-expression. */ +/* assignment-expression: + conditional-expression + unary-expression assignment-operator assignment-expression + + assignment-expression: one of + = *= /= %= += -= >>= <<= &= ^= |= */ static void -pp_c_assignment_expression (c_pretty_printer ppi, tree e) +pp_c_assignment_expression (c_pretty_printer *pp, tree e) { if (TREE_CODE (e) == MODIFY_EXPR || TREE_CODE (e) == INIT_EXPR) { - pp_c_unary_expression (ppi, TREE_OPERAND (e, 0)); - pp_c_maybe_whitespace (ppi); - pp_equal (ppi); - pp_space (ppi); - pp_c_assignment_expression (ppi, TREE_OPERAND (e, 1)); + pp_c_unary_expression (pp, TREE_OPERAND (e, 0)); + pp_c_whitespace (pp); + pp_equal (pp); + pp_space (pp); + pp_c_expression (pp, TREE_OPERAND (e, 1)); } else - pp_c_conditional_expression (ppi, e); + pp_c_conditional_expression (pp, e); } -/* Pretty-print an expression. */ +/* expression: + assignment-expression + expression , assignment-expression + + Implementation note: instead of going through the usual recursion + chain, I take the liberty of dispatching nodes to the appropriate + functions. This makes some redundancy, but it worths it. That also + prevents a possible infinite recursion between pp_c_primary_expression () + and pp_c_expression (). */ void -pp_c_expression (c_pretty_printer ppi, tree e) +pp_c_expression (c_pretty_printer *pp, tree e) { switch (TREE_CODE (e)) { case INTEGER_CST: - pp_c_integer_constant (ppi, e); + pp_c_integer_constant (pp, e); break; case REAL_CST: - pp_c_floating_constant (ppi, e); + pp_c_floating_constant (pp, e); break; case STRING_CST: - pp_c_string_literal (ppi, e); + pp_c_string_literal (pp, e); break; + case IDENTIFIER_NODE: case FUNCTION_DECL: case VAR_DECL: case CONST_DECL: @@ -1516,9 +1590,8 @@ pp_c_expression (c_pretty_printer ppi, tree e) case FIELD_DECL: case LABEL_DECL: case ERROR_MARK: - case TARGET_EXPR: case STMT_EXPR: - pp_primary_expression (ppi, e); + pp_primary_expression (pp, e); break; case POSTINCREMENT_EXPR: @@ -1535,7 +1608,7 @@ pp_c_expression (c_pretty_printer ppi, tree e) case COMPOUND_LITERAL_EXPR: case COMPLEX_EXPR: case VA_ARG_EXPR: - pp_postfix_expression (ppi, e); + pp_postfix_expression (pp, e); break; case CONJ_EXPR: @@ -1550,89 +1623,93 @@ pp_c_expression (c_pretty_printer ppi, tree e) case ALIGNOF_EXPR: case REALPART_EXPR: case IMAGPART_EXPR: - pp_c_unary_expression (ppi, e); + pp_c_unary_expression (pp, e); break; case FLOAT_EXPR: case FIX_TRUNC_EXPR: case CONVERT_EXPR: - pp_c_cast_expression (ppi, e); + pp_c_cast_expression (pp, e); break; case MULT_EXPR: case TRUNC_MOD_EXPR: case TRUNC_DIV_EXPR: - pp_multiplicative_expression (ppi, e); + pp_multiplicative_expression (pp, e); break; case LSHIFT_EXPR: case RSHIFT_EXPR: - pp_c_shift_expression (ppi, e); + pp_c_shift_expression (pp, e); break; case LT_EXPR: case GT_EXPR: case LE_EXPR: case GE_EXPR: - pp_c_relational_expression (ppi, e); + pp_c_relational_expression (pp, e); break; case BIT_AND_EXPR: - pp_c_and_expression (ppi, e); + pp_c_and_expression (pp, e); break; case BIT_XOR_EXPR: - pp_c_exclusive_or_expression (ppi, e); + pp_c_exclusive_or_expression (pp, e); break; case BIT_IOR_EXPR: - pp_c_inclusive_or_expression (ppi, e); + pp_c_inclusive_or_expression (pp, e); break; case TRUTH_ANDIF_EXPR: - pp_c_logical_and_expression (ppi, e); + pp_c_logical_and_expression (pp, e); break; case TRUTH_ORIF_EXPR: - pp_c_logical_or_expression (ppi, e); + pp_c_logical_or_expression (pp, e); break; case EQ_EXPR: case NE_EXPR: - pp_c_equality_expression (ppi, e); + pp_c_equality_expression (pp, e); break; case COND_EXPR: - pp_conditional_expression (ppi, e); + pp_conditional_expression (pp, e); break; case PLUS_EXPR: case MINUS_EXPR: - pp_c_additive_expression (ppi, e); + pp_c_additive_expression (pp, e); break; case MODIFY_EXPR: case INIT_EXPR: - pp_assignment_expression (ppi, e); + pp_assignment_expression (pp, e); break; case COMPOUND_EXPR: - pp_c_left_paren (ppi); - pp_c_expression (ppi, TREE_OPERAND (e, 0)); - pp_separate_with (ppi, ','); - pp_assignment_expression (ppi, TREE_OPERAND (e, 1)); - pp_c_right_paren (ppi); + pp_c_left_paren (pp); + pp_expression (pp, TREE_OPERAND (e, 0)); + pp_separate_with (pp, ','); + pp_assignment_expression (pp, TREE_OPERAND (e, 1)); + pp_c_right_paren (pp); break; case NOP_EXPR: case NON_LVALUE_EXPR: case SAVE_EXPR: case UNSAVE_EXPR: - pp_c_expression (ppi, TREE_OPERAND (e, 0)); + pp_expression (pp, TREE_OPERAND (e, 0)); + break; + + case TARGET_EXPR: + pp_postfix_expression (pp, TREE_OPERAND (e, 1)); break; default: - pp_unsupported_tree (ppi, e); + pp_unsupported_tree (pp, e); break; } } @@ -1640,8 +1717,16 @@ pp_c_expression (c_pretty_printer ppi, tree e) /* Statements. */ + +/* statement: + labeled-statement + coumpound-statement + expression-statement + selection-statement + iteration-statement + jump-statement */ void -pp_c_statement (c_pretty_printer ppi, tree stmt) +pp_c_statement (c_pretty_printer *pp, tree stmt) { enum tree_code code; @@ -1651,164 +1736,193 @@ pp_c_statement (c_pretty_printer ppi, tree stmt) code = TREE_CODE (stmt); switch (code) { + /* labeled-statement: + identifier : statement + case constant-expression : statement + default : statement */ case LABEL_STMT: case CASE_LABEL: - if (pp_needs_newline (ppi)) - pp_newline_and_indent (ppi, -3); + if (pp_needs_newline (pp)) + pp_newline_and_indent (pp, -3); else - pp_indentation (ppi) -= 3; + pp_indentation (pp) -= 3; if (code == LABEL_STMT) - pp_tree_identifier (ppi, DECL_NAME (LABEL_STMT_LABEL (stmt))); + pp_tree_identifier (pp, DECL_NAME (LABEL_STMT_LABEL (stmt))); else if (code == CASE_LABEL) { if (CASE_LOW (stmt) == NULL_TREE) - pp_identifier (ppi, "default"); + pp_identifier (pp, "default"); else { - pp_c_identifier (ppi, "case"); - pp_c_whitespace (ppi); - pp_conditional_expression (ppi, CASE_LOW (stmt)); + pp_c_identifier (pp, "case"); + pp_c_whitespace (pp); + pp_conditional_expression (pp, CASE_LOW (stmt)); if (CASE_HIGH (stmt)) { - pp_identifier (ppi, "..."); - pp_conditional_expression (ppi, CASE_HIGH (stmt)); + pp_identifier (pp, "..."); + pp_conditional_expression (pp, CASE_HIGH (stmt)); } } } - pp_colon (ppi); - pp_indentation (ppi) += 3; - pp_needs_newline (ppi) = true; + pp_colon (pp); + pp_indentation (pp) += 3; + pp_needs_newline (pp) = true; break; + /* coumpound-statement: + { block-item-list(opt) } + + block-item-list: + block-item + block-item-list block-item + + block-item: + declaration + statement */ case COMPOUND_STMT: - if (pp_needs_newline (ppi)) - pp_newline_and_indent (ppi, 0); - pp_c_left_brace (ppi); - pp_newline_and_indent (ppi, 3); + if (pp_needs_newline (pp)) + pp_newline_and_indent (pp, 0); + pp_c_left_brace (pp); + pp_newline_and_indent (pp, 3); for (stmt = COMPOUND_BODY (stmt); stmt; stmt = TREE_CHAIN (stmt)) - pp_c_statement (ppi, stmt); - pp_newline_and_indent (ppi, -3); - pp_c_right_brace (ppi); - pp_needs_newline (ppi) = true; + pp_statement (pp, stmt); + pp_newline_and_indent (pp, -3); + pp_c_right_brace (pp); + pp_needs_newline (pp) = true; break; + /* expression-statement: + expression(opt) ; */ case EXPR_STMT: case CLEANUP_STMT: - if (pp_needs_newline (ppi)) - pp_newline_and_indent (ppi, 0); + if (pp_needs_newline (pp)) + pp_newline_and_indent (pp, 0); { tree e = code == EXPR_STMT ? EXPR_STMT_EXPR (stmt) : CLEANUP_EXPR (stmt); if (e) - pp_c_expression (ppi, e); + pp_expression (pp, e); } - pp_c_semicolon (ppi); - pp_needs_newline (ppi) = true; + pp_c_semicolon (pp); + pp_needs_newline (pp) = true; break; + /* selection-statement: + if ( expression ) statement + if ( expression ) statement else statement + switch ( expression ) statement */ case IF_STMT: - if (pp_needs_newline (ppi)) - pp_newline_and_indent (ppi, 0); - pp_c_identifier (ppi, "if"); - pp_c_whitespace (ppi); - pp_c_left_paren (ppi); - pp_c_expression (ppi, IF_COND (stmt)); - pp_c_right_paren (ppi); - pp_newline_and_indent (ppi, 3); - pp_statement (ppi, THEN_CLAUSE (stmt)); - pp_newline_and_indent (ppi, -3); + if (pp_needs_newline (pp)) + pp_newline_and_indent (pp, 0); + pp_c_identifier (pp, "if"); + pp_c_whitespace (pp); + pp_c_left_paren (pp); + pp_expression (pp, IF_COND (stmt)); + pp_c_right_paren (pp); + pp_newline_and_indent (pp, 3); + pp_statement (pp, THEN_CLAUSE (stmt)); + pp_newline_and_indent (pp, -3); if (ELSE_CLAUSE (stmt)) { tree else_clause = ELSE_CLAUSE (stmt); - pp_c_identifier (ppi, "else"); + pp_c_identifier (pp, "else"); if (TREE_CODE (else_clause) == IF_STMT) - pp_c_whitespace (ppi); + pp_c_whitespace (pp); else - pp_newline_and_indent (ppi, 3); - pp_statement (ppi, else_clause); + pp_newline_and_indent (pp, 3); + pp_statement (pp, else_clause); if (TREE_CODE (else_clause) != IF_STMT) - pp_newline_and_indent (ppi, -3); + pp_newline_and_indent (pp, -3); } break; case SWITCH_STMT: - if (pp_needs_newline (ppi)) - pp_newline_and_indent (ppi, 0); - pp_c_identifier (ppi, "switch"); - pp_space (ppi); - pp_c_left_paren (ppi); - pp_c_expression (ppi, SWITCH_COND (stmt)); - pp_c_right_paren (ppi); - pp_indentation (ppi) += 3; - pp_needs_newline (ppi) = true; - pp_statement (ppi, SWITCH_BODY (stmt)); - pp_newline_and_indent (ppi, -3); - break; - + if (pp_needs_newline (pp)) + pp_newline_and_indent (pp, 0); + pp_c_identifier (pp, "switch"); + pp_space (pp); + pp_c_left_paren (pp); + pp_expression (pp, SWITCH_COND (stmt)); + pp_c_right_paren (pp); + pp_indentation (pp) += 3; + pp_needs_newline (pp) = true; + pp_statement (pp, SWITCH_BODY (stmt)); + pp_newline_and_indent (pp, -3); + break; + + /* iteration-statement: + while ( expression ) statement + do statement while ( expression ) ; + for ( expression(opt) ; expression(opt) ; expression(opt) ) statement + for ( declaration expression(opt) ; expression(opt) ) statement */ case WHILE_STMT: - if (pp_needs_newline (ppi)) - pp_newline_and_indent (ppi, 0); - pp_c_identifier (ppi, "while"); - pp_space (ppi); - pp_c_left_paren (ppi); - pp_c_expression (ppi, WHILE_COND (stmt)); - pp_c_right_paren (ppi); - pp_newline_and_indent (ppi, 3); - pp_statement (ppi, WHILE_BODY (stmt)); - pp_indentation (ppi) -= 3; - pp_needs_newline (ppi) = true; + if (pp_needs_newline (pp)) + pp_newline_and_indent (pp, 0); + pp_c_identifier (pp, "while"); + pp_space (pp); + pp_c_left_paren (pp); + pp_expression (pp, WHILE_COND (stmt)); + pp_c_right_paren (pp); + pp_newline_and_indent (pp, 3); + pp_statement (pp, WHILE_BODY (stmt)); + pp_indentation (pp) -= 3; + pp_needs_newline (pp) = true; break; case DO_STMT: - if (pp_needs_newline (ppi)) - pp_newline_and_indent (ppi, 0); - pp_c_identifier (ppi, "do"); - pp_newline_and_indent (ppi, 3); - pp_statement (ppi, DO_BODY (stmt)); - pp_newline_and_indent (ppi, -3); - pp_c_identifier (ppi, "while"); - pp_space (ppi); - pp_c_left_paren (ppi); - pp_c_expression (ppi, DO_COND (stmt)); - pp_c_right_paren (ppi); - pp_c_semicolon (ppi); - pp_needs_newline (ppi) = true; + if (pp_needs_newline (pp)) + pp_newline_and_indent (pp, 0); + pp_c_identifier (pp, "do"); + pp_newline_and_indent (pp, 3); + pp_statement (pp, DO_BODY (stmt)); + pp_newline_and_indent (pp, -3); + pp_c_identifier (pp, "while"); + pp_space (pp); + pp_c_left_paren (pp); + pp_expression (pp, DO_COND (stmt)); + pp_c_right_paren (pp); + pp_c_semicolon (pp); + pp_needs_newline (pp) = true; break; case FOR_STMT: - if (pp_needs_newline (ppi)) - pp_newline_and_indent (ppi, 0); - pp_c_identifier (ppi, "for"); - pp_space (ppi); - pp_c_left_paren (ppi); + if (pp_needs_newline (pp)) + pp_newline_and_indent (pp, 0); + pp_c_identifier (pp, "for"); + pp_space (pp); + pp_c_left_paren (pp); if (FOR_INIT_STMT (stmt)) - pp_statement (ppi, FOR_INIT_STMT (stmt)); + pp_statement (pp, FOR_INIT_STMT (stmt)); else - pp_c_semicolon (ppi); - pp_needs_newline (ppi) = false; - pp_c_whitespace (ppi); + pp_c_semicolon (pp); + pp_needs_newline (pp) = false; + pp_c_whitespace (pp); if (FOR_COND (stmt)) - pp_c_expression (ppi, FOR_COND (stmt)); - pp_c_semicolon (ppi); - pp_needs_newline (ppi) = false; - pp_c_whitespace (ppi); + pp_expression (pp, FOR_COND (stmt)); + pp_c_semicolon (pp); + pp_needs_newline (pp) = false; + pp_c_whitespace (pp); if (FOR_EXPR (stmt)) - pp_c_expression (ppi, FOR_EXPR (stmt)); - pp_c_right_paren (ppi); - pp_newline_and_indent (ppi, 3); - pp_statement (ppi, FOR_BODY (stmt)); - pp_indentation (ppi) -= 3; - pp_needs_newline (ppi) = true; - break; - + pp_expression (pp, FOR_EXPR (stmt)); + pp_c_right_paren (pp); + pp_newline_and_indent (pp, 3); + pp_statement (pp, FOR_BODY (stmt)); + pp_indentation (pp) -= 3; + pp_needs_newline (pp) = true; + break; + + /* jump-statement: + goto identifier; + continue ; + return expression(opt) ; */ case BREAK_STMT: case CONTINUE_STMT: - if (pp_needs_newline (ppi)) - pp_newline_and_indent (ppi, 0); - pp_identifier (ppi, code == BREAK_STMT ? "break" : "continue"); - pp_c_semicolon (ppi); - pp_needs_newline (ppi) = true; + if (pp_needs_newline (pp)) + pp_newline_and_indent (pp, 0); + pp_identifier (pp, code == BREAK_STMT ? "break" : "continue"); + pp_c_semicolon (pp); + pp_needs_newline (pp) = true; break; case RETURN_STMT: @@ -1817,13 +1931,19 @@ pp_c_statement (c_pretty_printer ppi, tree stmt) tree e = code == RETURN_STMT ? RETURN_STMT_EXPR (stmt) : GOTO_DESTINATION (stmt); - if (pp_needs_newline (ppi)) - pp_newline_and_indent (ppi, 0); - pp_c_identifier (ppi, code == RETURN_STMT ? "return" : "goto"); + if (pp_needs_newline (pp)) + pp_newline_and_indent (pp, 0); + pp_c_identifier (pp, code == RETURN_STMT ? "return" : "goto"); + pp_c_whitespace (pp); if (e) - pp_c_expression (ppi, e); - pp_c_semicolon (ppi); - pp_needs_newline (ppi) = true; + { + if (TREE_CODE (e) == INIT_EXPR + && TREE_CODE (TREE_OPERAND (e, 0)) == RESULT_DECL) + e = TREE_OPERAND (e, 1); + pp_expression (pp, e); + } + pp_c_semicolon (pp); + pp_needs_newline (pp) = true; } break; @@ -1831,29 +1951,28 @@ pp_c_statement (c_pretty_printer ppi, tree stmt) if (!SCOPE_NULLIFIED_P (stmt) && SCOPE_NO_CLEANUPS_P (stmt)) { int i = 0; - if (pp_needs_newline (ppi)) - pp_newline_and_indent (ppi, 0); + if (pp_needs_newline (pp)) + pp_newline_and_indent (pp, 0); if (SCOPE_BEGIN_P (stmt)) { - pp_left_brace (ppi); + pp_left_brace (pp); i = 3; } else if (SCOPE_END_P (stmt)) { - pp_right_brace (ppi); + pp_right_brace (pp); i = -3; } - pp_indentation (ppi) += i; - pp_needs_newline (ppi) = true; + pp_indentation (pp) += i; + pp_needs_newline (pp) = true; } break; case DECL_STMT: - if (pp_needs_newline (ppi)) - pp_newline_and_indent (ppi, 0); - pp_declaration (ppi, DECL_STMT_DECL (stmt)); - pp_c_semicolon (ppi); - pp_needs_newline (ppi) = true; + if (pp_needs_newline (pp)) + pp_newline_and_indent (pp, 0); + pp_declaration (pp, DECL_STMT_DECL (stmt)); + pp_needs_newline (pp) = true; break; case ASM_STMT: @@ -1861,52 +1980,51 @@ pp_c_statement (c_pretty_printer ppi, tree stmt) bool has_volatile_p = ASM_VOLATILE_P (stmt); bool is_extended = has_volatile_p || ASM_INPUTS (stmt) || ASM_OUTPUTS (stmt) || ASM_CLOBBERS (stmt); - pp_c_identifier (ppi, is_extended ? "__asm__" : "asm"); + pp_c_identifier (pp, is_extended ? "__asm__" : "asm"); if (has_volatile_p) - pp_c_identifier (ppi, "__volatile__"); - pp_space (ppi); - pp_c_left_paren (ppi); - pp_c_string_literal (ppi, ASM_STRING (stmt)); + pp_c_identifier (pp, "__volatile__"); + pp_space (pp); + pp_c_left_paren (pp); + pp_c_string_literal (pp, ASM_STRING (stmt)); if (is_extended) { - pp_space (ppi); - pp_separate_with (ppi, ':'); + pp_space (pp); + pp_separate_with (pp, ':'); if (ASM_OUTPUTS (stmt)) - pp_c_expression (ppi, ASM_OUTPUTS (stmt)); - pp_space (ppi); - pp_separate_with (ppi, ':'); + pp_expression (pp, ASM_OUTPUTS (stmt)); + pp_space (pp); + pp_separate_with (pp, ':'); if (ASM_INPUTS (stmt)) - pp_c_expression (ppi, ASM_INPUTS (stmt)); - pp_space (ppi); - pp_separate_with (ppi, ':'); + pp_expression (pp, ASM_INPUTS (stmt)); + pp_space (pp); + pp_separate_with (pp, ':'); if (ASM_CLOBBERS (stmt)) - pp_c_expression (ppi, ASM_CLOBBERS (stmt)); + pp_expression (pp, ASM_CLOBBERS (stmt)); } - pp_c_right_paren (ppi); - pp_newline (ppi); + pp_c_right_paren (pp); + pp_newline (pp); } break; case FILE_STMT: - pp_c_identifier (ppi, "__FILE__"); - pp_space (ppi); - pp_equal (ppi); - pp_c_whitespace (ppi); - pp_c_identifier (ppi, FILE_STMT_FILENAME (stmt)); - pp_c_semicolon (ppi); - pp_needs_newline (ppi) = true; + pp_c_identifier (pp, "__FILE__"); + pp_space (pp); + pp_equal (pp); + pp_c_whitespace (pp); + pp_c_identifier (pp, FILE_STMT_FILENAME (stmt)); + pp_c_semicolon (pp); + pp_needs_newline (pp) = true; break; default: - pp_unsupported_tree (ppi, stmt); + pp_unsupported_tree (pp, stmt); } - } /* Initialize the PRETTY-PRINTER for handling C codes. */ void -pp_c_pretty_printer_init (c_pretty_printer pp) +pp_c_pretty_printer_init (c_pretty_printer *pp) { pp->offset_list = 0; @@ -1914,10 +2032,13 @@ pp_c_pretty_printer_init (c_pretty_printer pp) pp->declaration_specifiers = pp_c_declaration_specifiers; pp->declarator = pp_c_declarator; pp->direct_declarator = pp_c_direct_declarator; - pp->type_specifier = pp_c_simple_type_specifier; + pp->type_specifier_seq = pp_c_specifier_qualifier_list; pp->abstract_declarator = pp_c_abstract_declarator; - pp->parameter_declaration = pp_c_parameter_declaration; + pp->direct_abstract_declarator = pp_c_direct_abstract_declarator; + pp->ptr_operator = pp_c_pointer; + pp->parameter_list = pp_c_parameter_type_list; pp->type_id = pp_c_type_id; + pp->simple_type_specifier = pp_c_type_specifier; pp->function_specifier = pp_c_function_specifier; pp->storage_class_specifier = pp_c_storage_class_specifier; @@ -1931,4 +2052,5 @@ pp_c_pretty_printer_init (c_pretty_printer pp) pp->multiplicative_expression = pp_c_multiplicative_expression; pp->conditional_expression = pp_c_conditional_expression; pp->assignment_expression = pp_c_assignment_expression; + pp->expression = pp_c_expression; } diff --git a/gcc/c-pretty-print.h b/gcc/c-pretty-print.h index 8052662c764..66ca33c7d5e 100644 --- a/gcc/c-pretty-print.h +++ b/gcc/c-pretty-print.h @@ -36,14 +36,25 @@ typedef enum /* The data type used to bundle information necessary for pretty-printing a C or C++ entity. */ -typedef struct c_pretty_print_info *c_pretty_printer; +typedef struct c_pretty_print_info c_pretty_printer; /* The type of a C pretty-printer 'member' function. */ -typedef void (*c_pretty_print_fn) (c_pretty_printer, tree); - +typedef void (*c_pretty_print_fn) (c_pretty_printer *, tree); + +/* The datatype that contains information necessary for pretty-printing + a tree that represents a C construct. Any pretty-printer for a + language using C/c++ syntax can derive from this datatype and reuse + facilities provided here. It can do so by having a subobject of type + c_pretty_printer and override the macro pp_c_base to return a pointer + to that subobject. Such a pretty-printer has the responsability to + initialize the pp_base() part, then call pp_c_pretty_printer_init + to set up the components that are specific to the C pretty-printer. + A derived pretty-printer can override any function listed in the + vtable below. See cp/cxx-pretty-print.h and cp/cxx-pretty-print.c + for an example of derivation. */ struct c_pretty_print_info { - struct pretty_print_info base; + pretty_printer base; /* Points to the first element of an array of offset-list. Not used yet. */ int *offset_list; @@ -56,12 +67,16 @@ struct c_pretty_print_info c_pretty_print_fn declaration_specifiers; c_pretty_print_fn declarator; c_pretty_print_fn abstract_declarator; - c_pretty_print_fn type_specifier; + c_pretty_print_fn direct_abstract_declarator; + c_pretty_print_fn type_specifier_seq; c_pretty_print_fn direct_declarator; - c_pretty_print_fn parameter_declaration; + c_pretty_print_fn ptr_operator; + c_pretty_print_fn parameter_list; c_pretty_print_fn type_id; + c_pretty_print_fn simple_type_specifier; c_pretty_print_fn function_specifier; c_pretty_print_fn storage_class_specifier; + c_pretty_print_fn initializer; c_pretty_print_fn statement; @@ -69,12 +84,14 @@ struct c_pretty_print_info c_pretty_print_fn primary_expression; c_pretty_print_fn postfix_expression; c_pretty_print_fn unary_expression; - c_pretty_print_fn initializer; c_pretty_print_fn multiplicative_expression; c_pretty_print_fn conditional_expression; c_pretty_print_fn assignment_expression; + c_pretty_print_fn expression; }; +/* Override the pp_base macro. Derived pretty-printers should not + touch this macro. Instead they should override pp_c_base instead. */ #undef pp_base #define pp_base(PP) (&pp_c_base (PP)->base) @@ -88,16 +105,22 @@ struct c_pretty_print_info pp_c_base (PPI)->declaration_specifiers (pp_c_base (PPI), D) #define pp_abstract_declarator(PP, D) \ pp_c_base (PP)->abstract_declarator (pp_c_base (PP), D) -#define pp_type_specifier(PPI, D) \ - pp_c_base (PPI)->type_specifier (pp_c_base (PPI), D) +#define pp_type_specifier_seq(PPI, D) \ + pp_c_base (PPI)->type_specifie_seqr (pp_c_base (PPI), D) #define pp_declarator(PPI, D) \ pp_c_base (PPI)->declarator (pp_c_base (PPI), D) #define pp_direct_declarator(PPI, D) \ pp_c_base (PPI)->direct_declarator (pp_c_base (PPI), D) -#define pp_parameter_declaration(PPI, T) \ - pp_c_base (PPI)->parameter_declaration (pp_c_base (PPI), T) +#define pp_direct_abstract_declarator(PP, D) \ + pp_c_base (PP)->direct_abstract_declarator (pp_c_base (PP), D) +#define pp_ptr_operator(PP, D) \ + pp_c_base (PP)->ptr_operator (pp_c_base (PP), D) +#define pp_parameter_list(PPI, T) \ + pp_c_base (PPI)->parameter_list (pp_c_base (PPI), T) #define pp_type_id(PPI, D) \ pp_c_base (PPI)->type_id (pp_c_base (PPI), D) +#define pp_simple_type_specifier(PP, T) \ + pp_c_base (PP)->simple_type_specifier (pp_c_base (PP), T) #define pp_function_specifier(PP, D) \ pp_c_base (PP)->function_specifier (pp_c_base (PP), D) #define pp_storage_class_specifier(PP, D) \ @@ -122,37 +145,54 @@ struct c_pretty_print_info pp_c_base (PPI)->conditional_expression (pp_c_base (PPI), E) #define pp_assignment_expression(PPI, E) \ pp_c_base (PPI)->assignment_expression (pp_c_base (PPI), E) +#define pp_expression(PP, E) \ + pp_c_base (PP)->expression (pp_c_base (PP), E) /* Returns the c_pretty_printer base object of PRETTY-PRINTER. This macro must be overridden by any subclass of c_pretty_print_info. */ #define pp_c_base(PP) (PP) -extern void pp_c_pretty_printer_init (c_pretty_printer); +extern void pp_c_pretty_printer_init (c_pretty_printer *); +void pp_c_whitespace (c_pretty_printer *); +void pp_c_left_paren (c_pretty_printer *); +void pp_c_right_paren (c_pretty_printer *); +void pp_c_dot (c_pretty_printer *); +void pp_c_ampersand (c_pretty_printer *); +void pp_c_arrow (c_pretty_printer *); +void pp_c_semicolon (c_pretty_printer *); +void pp_c_space_for_pointer_operator (c_pretty_printer *, tree); /* Declarations. */ -void pp_c_function_definition (c_pretty_printer, tree); -void pp_c_attributes (c_pretty_printer, tree); -void pp_c_type_qualifier_list (c_pretty_printer, tree); -void pp_c_parameter_declaration_clause (c_pretty_printer, tree); -void pp_c_declaration (c_pretty_printer, tree); -void pp_c_declarator (c_pretty_printer, tree); -void pp_c_direct_declarator (c_pretty_printer, tree); -void pp_c_specifier_qualifier_list (c_pretty_printer, tree); -void pp_c_type_id (c_pretty_printer, tree); +void pp_c_function_definition (c_pretty_printer *, tree); +void pp_c_attributes (c_pretty_printer *, tree); +void pp_c_type_qualifier_list (c_pretty_printer *, tree); +void pp_c_parameter_type_list (c_pretty_printer *, tree); +void pp_c_declaration (c_pretty_printer *, tree); +void pp_c_declaration_specifiers (c_pretty_printer *, tree); +void pp_c_declarator (c_pretty_printer *, tree); +void pp_c_direct_declarator (c_pretty_printer *, tree); +void pp_c_specifier_qualifier_list (c_pretty_printer *, tree); +void pp_c_function_specifier (c_pretty_printer *, tree); +void pp_c_type_id (c_pretty_printer *, tree); +void pp_c_direct_abstract_declarator (c_pretty_printer *, tree); +void pp_c_type_specifier (c_pretty_printer *, tree); +void pp_c_storage_class_specifier (c_pretty_printer *, tree); /* Statements. */ -void pp_c_statement (c_pretty_printer, tree); +void pp_c_statement (c_pretty_printer *, tree); /* Expressions. */ -void pp_c_expression (c_pretty_printer, tree); -void pp_c_logical_or_expression (c_pretty_printer, tree); -void pp_c_expression_list (c_pretty_printer, tree); -void pp_c_unary_expression (c_pretty_printer, tree); -void pp_c_cast_expression (c_pretty_printer, tree); -void pp_c_postfix_expression (c_pretty_printer, tree); -void pp_c_initializer (c_pretty_printer, tree); -void pp_c_constant (c_pretty_printer, tree); -void pp_c_id_expression (c_pretty_printer, tree); -void pp_c_identifier (c_pretty_printer, const char *); -void pp_c_string_literal (c_pretty_printer, tree); +void pp_c_expression (c_pretty_printer *, tree); +void pp_c_logical_or_expression (c_pretty_printer *, tree); +void pp_c_expression_list (c_pretty_printer *, tree); +void pp_c_call_argument_list (c_pretty_printer *, tree); +void pp_c_unary_expression (c_pretty_printer *, tree); +void pp_c_cast_expression (c_pretty_printer *, tree); +void pp_c_postfix_expression (c_pretty_printer *, tree); +void pp_c_primary_expression (c_pretty_printer *, tree); +void pp_c_init_declarator (c_pretty_printer *, tree); +void pp_c_constant (c_pretty_printer *, tree); +void pp_c_id_expression (c_pretty_printer *, tree); +void pp_c_identifier (c_pretty_printer *, const char *); +void pp_c_string_literal (c_pretty_printer *, tree); #endif /* GCC_C_PRETTY_PRINTER */ diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index fc987c82631..d56c7a5753f 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,66 @@ +2003-08-25 Gabriel Dos Reis <gdr@integrable-solutions.net> + + * cxx-pretty-print.h (pp_cxx_flag_default_argument): New flag. + (cxx_pretty_printer): Adjust base type. + (pp_cxx_function_specifier): Declare. + * cxx-pretty-print.c (pp_cxx_whitespace): New macro. + (pp_cxx_left_paren): Likewise. + (pp_cxx_right_paren): Likewise. + (pp_cxx_dot): Likewise. + (pp_cxx_arrow): Likewise. + (pp_cxx_semicolon): Likewise. + (pp_cxx_identifier): Likewise. + (pp_cxx_cv_qualifier_seq): Likewise. + (pp_cxx_storage_class_specifier): Likewise. + (pp_cxx_expression_list): Likewise. + (pp_cxx_space_for_pointer_operator): Likewise. + (pp_cxx_init_declarator): Likewise. + (pp_cxx_call_argument_list): Likewise. + (pp_cxx_nonconsecutive_character): Tidy. + (pp_cxx_conversion_function_id): New function. + (pp_cxx_template_id): Likewise. + (pp_cxx_class_name): Likewise. + (pp_cxx_template_keyword_if_needed): Likewise. + (pp_cxx_nested_name_specifier): Likewise. + (pp_cxx_unqualified_id): Tidy + (pp_cxx_qualified_id): Handle more nodes. + (pp_cxx_primary_expression): Tidy. + (pp_cxx_postfix_expression): Likewise. + (pp_cxx_new_expression): Tidy. + (pp_cxx_delete_expression): Likewise. + (pp_cxx_cast_expression): New function. + (pp_cxx_pm_expression): Tidy. + (pp_cxx_conditional_expression): Likewise. + (pp_cxx_assignment_operator): New function. + (pp_cxx_assignment_expression): Tidy. + (pp_cxx_expression): New function. + (pp_cxx_function_specifier): Likewise. + (pp_cxx_decl_specifier_seq): Likewise. + (pp_cxx_simple_type_specifier): Tidy. + (pp_cxx_type_specifier_seq): Likewise. + (pp_cxx_ptr_operator): New function. + (pp_cxx_implicit_parameter_type): Likewise. + (pp_cxx_parameter_declaration): Tidy. + (pp_cxx_parameter_declaration_clause): New function. + (pp_cxx_exception_specification): Likewise. + (pp_cxx_direct_declarator): Tidy. + (pp_cxx_declarator): Likewise. + (pp_cxx_ctor_initializer): New function. + (pp_cxx_function_definition): Likewise. + (pp_cxx_abstract_declarator): Tidy. + (pp_cxx_direct_abstract_declarator): Likewise. + (pp_cxx_type_id): Likewise. + (pp_cxx_exception_declaration): New function. + (pp_cxx_statement): Likewise. + (pp_cxx_simple_declaration): Likewise. + (pp_cxx_template_parameter_list): Likewise. + (pp_cxx_template_parameter): Likewise. + (pp_cxx_template_declaration): Likewise. + (pp_cxx_explicit_specialization): Likewise. + (pp_cxx_explicit_instantiation): Likewise. + (pp_cxx_declaration): Tidy. + (pp_cxx_pretty_printer_init): Initialize more fields. + 2003-08-25 Mark Mitchell <mark@codesourcery.com> PR c++/8795 diff --git a/gcc/cp/cxx-pretty-print.c b/gcc/cp/cxx-pretty-print.c index 83e43812012..a8e931c856b 100644 --- a/gcc/cp/cxx-pretty-print.c +++ b/gcc/cp/cxx-pretty-print.c @@ -26,12 +26,28 @@ Software Foundation, 59 Temple Place - Suite 330, Boston, MA #include "real.h" #include "cxx-pretty-print.h" #include "cp-tree.h" +#include "toplev.h" static void pp_cxx_unqualified_id (cxx_pretty_printer *, tree); +static void pp_cxx_nested_name_specifier (cxx_pretty_printer *, tree); static void pp_cxx_qualified_id (cxx_pretty_printer *, tree); static void pp_cxx_assignment_expression (cxx_pretty_printer *, tree); +static void pp_cxx_expression (cxx_pretty_printer *, tree); static void pp_cxx_template_argument_list (cxx_pretty_printer *, tree); +static void pp_cxx_type_specifier_seq (cxx_pretty_printer *, tree); +static void pp_cxx_ptr_operator (cxx_pretty_printer *, tree); +static void pp_cxx_type_id (cxx_pretty_printer *, tree); +static void pp_cxx_direct_abstract_declarator (cxx_pretty_printer *, tree); +static void pp_cxx_declarator (cxx_pretty_printer *, tree); +static void pp_cxx_abstract_declarator (cxx_pretty_printer *, tree); +static void pp_cxx_template_parameter (cxx_pretty_printer *, tree); +#define pp_cxx_whitespace(PP) pp_c_whitespace (pp_c_base (PP)) +#define pp_cxx_left_paren(PP) pp_c_left_paren (pp_c_base (PP)) +#define pp_cxx_right_paren(PP) pp_c_right_paren (pp_c_base (PP)) +#define pp_cxx_dot(PP) pp_c_dot (pp_c_base (PP)) +#define pp_cxx_arrow(PP) pp_c_arrow (pp_c_base (PP)) +#define pp_cxx_semicolon(PP) pp_c_semicolon (pp_c_base (PP)) static inline void pp_cxx_nonconsecutive_character (cxx_pretty_printer *pp, int c) @@ -39,8 +55,9 @@ pp_cxx_nonconsecutive_character (cxx_pretty_printer *pp, int c) const char *p = pp_last_position_in_text (pp); if (p != NULL && *p == c) - pp_space (pp); + pp_cxx_whitespace (pp); pp_character (pp, c); + pp_base (pp)->padding = pp_none; } #define pp_cxx_begin_template_argument_list(PP) \ @@ -48,164 +65,68 @@ pp_cxx_nonconsecutive_character (cxx_pretty_printer *pp, int c) #define pp_cxx_end_template_argument_list(PP) \ pp_cxx_nonconsecutive_character (PP, '>') -/* Declarations. */ - -void -pp_cxx_declaration (cxx_pretty_printer *pp, tree t) -{ - pp_unsupported_tree (pp, t); -} +#define pp_cxx_identifier(PP, ID) pp_c_identifier (pp_c_base (PP), ID) +#define pp_cxx_tree_identifier(PP, T) pp_c_tree_identifier (pp_c_base (PP), T) + +#define pp_cxx_cv_qualifier_seq(PP, T) \ + pp_c_type_qualifier_list (pp_c_base (PP), T) +#define pp_cxx_storage_class_specifier(PP, T) \ + pp_c_storage_class_specifier (pp_c_base (PP), T) +#define pp_cxx_expression_list(PP, T) \ + pp_c_expression_list (pp_c_base (PP), T) +#define pp_cxx_space_for_pointer_operator(PP, T) \ + pp_c_space_for_pointer_operator (pp_c_base (PP), T) +#define pp_cxx_init_declarator(PP, T) \ + pp_c_init_declarator (pp_c_base (PP), T) +#define pp_cxx_call_argument_list(PP, T) \ + pp_c_call_argument_list (pp_c_base (PP), T) static void -pp_cxx_declaration_specifiers (cxx_pretty_printer *pp, tree t) +pp_cxx_colon_colon (cxx_pretty_printer *pp) { - pp_unsupported_tree (pp, t); + pp_colon_colon (pp); + pp_base (pp)->padding = pp_none; } -/* type-specifier: - simple-type-specifier - class-specifier - enum-specifier - elaborated-type-specifier - cv-qualifer */ -static void -pp_cxx_type_specifier (cxx_pretty_printer *pp, tree t) -{ - pp_c_type_qualifier_list (pp_c_base (pp), t); - switch (TREE_CODE (t)) - { - case TEMPLATE_DECL: - case TYPE_DECL: - if (pp->enclosing_scope != DECL_CONTEXT (t)) - pp_cxx_qualified_id (pp, t); - else - pp_cxx_unqualified_id (pp, t); - break; - - case TYPEOF_TYPE: - pp_c_identifier (pp_c_base (pp), "__typeof__"); - pp_left_paren (pp); - pp_c_expression (pp_c_base (pp), TYPE_FIELDS (t)); - pp_right_paren (pp); - break; - - default: - pp_c_specifier_qualifier_list (pp_c_base (pp), t); - } -} - -static void -pp_cxx_declarator (cxx_pretty_printer *pp, tree t) -{ - switch (TREE_CODE (t)) - { - default: - pp_c_declarator (pp_c_base (pp), t); - break; - } -} - -static void -pp_cxx_direct_declarator (cxx_pretty_printer *pp, tree t) -{ - pp_c_direct_declarator (pp_c_base (pp), t); -} +/* Expressions. */ -static void -pp_cxx_parameter_declaration (cxx_pretty_printer *pp, tree t) +static inline bool +is_destructor_name (tree name) { - pp_unsupported_tree (pp, t); + return name == complete_dtor_identifier + || name == base_dtor_identifier + || name == deleting_dtor_identifier; } +/* conversion-function-id: + operator conversion-type-id + conversion-type-id: + type-specifier-seq conversion-declarator(opt) -/* type-id: - type-specifier-seq abstract-declarator(opt) */ -static void -pp_cxx_type_id (cxx_pretty_printer *pp, tree t) + conversion-declarator: + ptr-operator conversion-declarator(opt) */ +static inline void +pp_cxx_conversion_function_id (cxx_pretty_printer *pp, tree t) { - pp_flags saved_flags = pp_c_base (pp)->flags; - pp_c_base (pp)->flags |= pp_c_flag_abstract; - - switch (TREE_CODE (t)) - { - case TYPE_DECL: - case UNION_TYPE: - case RECORD_TYPE: - case ENUMERAL_TYPE: - case TYPENAME_TYPE: - case BOUND_TEMPLATE_TEMPLATE_PARM: - case UNBOUND_CLASS_TEMPLATE: - case TEMPLATE_TEMPLATE_PARM: - case TEMPLATE_TYPE_PARM: - case TEMPLATE_PARM_INDEX: - case TEMPLATE_DECL: - case TYPEOF_TYPE: - case TEMPLATE_ID_EXPR: - /* FIXME: Should be pp_cxx_type_specifier_seq. */ - pp_cxx_type_specifier (pp, t); - pp_cxx_declarator (pp, t); - break; - - default: - pp_c_type_id (pp_c_base (pp), t); - break; - } - - pp_c_base (pp)->flags = saved_flags; + pp_cxx_identifier (pp, "operator"); + pp_cxx_type_specifier_seq (pp, TREE_TYPE (t)); } -static void -pp_cxx_initializer (cxx_pretty_printer *pp, tree t) +static inline void +pp_cxx_template_id (cxx_pretty_printer *pp, tree t) { - pp_unsupported_tree (pp, t); + pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 0)); + pp_cxx_begin_template_argument_list (pp); + pp_cxx_template_argument_list (pp, TREE_OPERAND (t, 1)); + pp_cxx_end_template_argument_list (pp); } -/* template-argument-list: - template-argument - template-argument-list, template-argument - - template-argument: - assignment-expression - type-id - template-name */ static void -pp_cxx_template_argument_list (cxx_pretty_printer *pp, tree t) +pp_cxx_class_name (cxx_pretty_printer *pp, tree t) { - int i; - if (t == NULL) - return; - for (i = 0; i < TREE_VEC_LENGTH (t); ++i) - { - tree arg = TREE_VEC_ELT (t, i); - if (i != 0) - pp_separate_with (pp, ','); - if (TYPE_P (arg) || (TREE_CODE (arg) == TEMPLATE_DECL - && TYPE_P (DECL_TEMPLATE_RESULT (arg)))) - pp_cxx_type_id (pp, arg); - else - pp_c_expression (pp_c_base (pp), arg); - } -} - - -/* Statements. */ - -void -pp_cxx_statement (cxx_pretty_printer *pp, tree t) -{ - pp_unsupported_tree (pp, t); -} - -/* Expressions. */ - -static inline bool -is_destructor_name (tree name) -{ - return name == complete_dtor_identifier - || name == base_dtor_identifier - || name == deleting_dtor_identifier; + pp_cxx_unqualified_id (pp, TYPE_NAME (t)); } /* unqualified-id: @@ -220,6 +141,12 @@ pp_cxx_unqualified_id (cxx_pretty_printer *pp, tree t) enum tree_code code = TREE_CODE (t); switch (code) { + case RESULT_DECL: + pp_cxx_identifier (pp, "<return-value>"); + break; + + case OVERLOAD: + t = OVL_CURRENT (t); case VAR_DECL: case PARM_DECL: case CONST_DECL: @@ -229,46 +156,70 @@ pp_cxx_unqualified_id (cxx_pretty_printer *pp, tree t) case FIELD_DECL: case LABEL_DECL: case USING_DECL: + case TEMPLATE_DECL: t = DECL_NAME (t); case IDENTIFIER_NODE: - if (IDENTIFIER_TYPENAME_P (t)) - { - pp_c_identifier (pp_c_base (pp), "operator"); - /* FIXME: should be pp_cxx_type_specifier_seq */ - pp_cxx_type_specifier (pp, TREE_TYPE (t)); - /* FIXME: should be pp_cxx_conversion_declarator. */ - pp_cxx_type_id (pp, TREE_TYPE (t)); - } - else if (IDENTIFIER_OPNAME_P (t)) - { - const char *name = operator_name_info[TREE_CODE (t)].name; - pp_c_identifier (pp_c_base (pp), "operator"); - if (ISALPHA (name[0])) - pp_space (pp); - pp_base (pp)->padding = pp_none; - pp_c_identifier (pp_c_base (pp), name); - } + if (t == NULL) + pp_cxx_identifier (pp, "<anonymous>"); + else if (IDENTIFIER_TYPENAME_P (t)) + pp_cxx_conversion_function_id (pp, t); else { if (is_destructor_name (t)) { pp_complement (pp); - t = constructor_name (TREE_TYPE (t)); + /* FIXME: Why is this necessary? */ + if (TREE_TYPE (t)) + t = constructor_name (TREE_TYPE (t)); } - pp_c_tree_identifier (pp_c_base (pp), t); + pp_cxx_tree_identifier (pp, t); } break; case TEMPLATE_ID_EXPR: - pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 0)); - pp_less (pp); - pp_cxx_template_argument_list (pp, TREE_OPERAND (t, 1)); - pp_greater (pp); + pp_cxx_template_id (pp, t); + break; + + case RECORD_TYPE: + case UNION_TYPE: + case ENUMERAL_TYPE: + pp_cxx_unqualified_id (pp, TYPE_NAME (t)); + break; + + case TEMPLATE_TYPE_PARM: + t = TYPE_FIELDS (t); + case TEMPLATE_PARM_INDEX: + pp_cxx_unqualified_id (pp, TEMPLATE_PARM_DECL (t)); break; default: pp_unsupported_tree (pp, t); + break; + } +} + +static inline void +pp_cxx_template_keyword_if_needed (cxx_pretty_printer *pp, tree scope, tree t) +{ + if (TREE_CODE (t) == TEMPLATE_ID_EXPR + && TYPE_P (scope) && dependent_type_p (scope)) + pp_cxx_identifier (pp, "template"); +} + +/* nested-name-specifier: + class-or-namespace-name :: nested-name-specifier(opt) + class-or-namespace-name :: template nested-name-specifier */ +static void +pp_cxx_nested_name_specifier (cxx_pretty_printer *pp, tree t) +{ + if (t != NULL && t != pp->enclosing_scope) + { + tree scope = TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t); + pp_cxx_nested_name_specifier (pp, scope); + pp_cxx_template_keyword_if_needed (pp, scope, t); + pp_cxx_unqualified_id (pp, t); + pp_cxx_colon_colon (pp); } } @@ -280,32 +231,34 @@ pp_cxx_qualified_id (cxx_pretty_printer *pp, tree t) switch (TREE_CODE (t)) { case PTRMEM_CST: - pp_cxx_qualified_id (pp, PTRMEM_CST_CLASS (t)); - pp_colon_colon (pp); + pp_cxx_nested_name_specifier (pp, PTRMEM_CST_CLASS (t)); pp_cxx_unqualified_id (pp, PTRMEM_CST_MEMBER (t)); break; + case OVERLOAD: + t = OVL_CURRENT (t); + case FUNCTION_DECL: + if (DECL_FUNCTION_MEMBER_P (t)) + pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t)); + pp_cxx_unqualified_id + (pp, DECL_CONSTRUCTOR_P (t) ? DECL_CONTEXT (t) : t); + break; + case OFFSET_REF: case SCOPE_REF: - pp_cxx_qualified_id (pp, TREE_OPERAND (t, 0)); - pp_colon_colon (pp); + pp_cxx_nested_name_specifier (pp, TREE_OPERAND (t, 0)); pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 1)); break; default: { - tree scope = DECL_CONTEXT (t); - if (scope == pp->enclosing_scope) - pp_cxx_unqualified_id (pp, t); - else + tree scope = TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t); + if (scope != pp->enclosing_scope) { - pp_cxx_qualified_id (pp, scope); - pp_colon_colon (pp); - if (TYPE_P (scope) && dependent_type_p (scope) - && TREE_CODE (t) == TEMPLATE_DECL) - pp_c_identifier (pp_c_base (pp), "template"); - pp_cxx_unqualified_id (pp, t); + pp_cxx_nested_name_specifier (pp, scope); + pp_cxx_template_keyword_if_needed (pp, scope, t); } + pp_cxx_unqualified_id (pp, t); } break; } @@ -317,8 +270,11 @@ pp_cxx_qualified_id (cxx_pretty_printer *pp, tree t) static inline void pp_cxx_id_expression (cxx_pretty_printer *pp, tree t) { - if (pp_c_base (pp)->flags - & (pp_cxx_flag_qualified_id | pp_cxx_flag_global_scope)) + if (TREE_CODE (t) == OVERLOAD) + t = OVL_CURRENT (t); + if ((TREE_CODE (t) == FUNCTION_DECL && DECL_FUNCTION_MEMBER_P (t)) + || (pp_c_base (pp)->flags + & (pp_cxx_flag_qualified_id | pp_cxx_flag_global_scope))) pp_cxx_qualified_id (pp, t); else pp_cxx_unqualified_id (pp, t); @@ -343,15 +299,26 @@ pp_cxx_primary_expression (cxx_pretty_printer *pp, tree t) pp_c_constant (pp_c_base (pp), t); break; + case BASELINK: + t = BASELINK_FUNCTIONS (t); + case VAR_DECL: + case PARM_DECL: + case FIELD_DECL: + case FUNCTION_DECL: + case OVERLOAD: + case CONST_DECL: + case TEMPLATE_DECL: + pp_cxx_id_expression (pp, t); + break; + + case RESULT_DECL: + case TEMPLATE_TYPE_PARM: + case TEMPLATE_PARM_INDEX: + pp_cxx_unqualified_id (pp, t); + break; + default: - if (pp_c_base (pp)->flags & pp_cxx_flag_qualified_id) - pp_cxx_qualified_id (pp, t); - else - { - if (pp_c_base (pp)->flags & pp_cxx_flag_global_scope) - pp_colon_colon (pp); - pp_cxx_unqualified_id (pp, t); - } + pp_c_primary_expression (pp_c_base (pp), t); break; } } @@ -384,6 +351,71 @@ pp_cxx_postfix_expression (cxx_pretty_printer *pp, tree t) switch (code) { + case AGGR_INIT_EXPR: + case CALL_EXPR: + { + tree fun = TREE_OPERAND (t, 0); + tree args = TREE_OPERAND (t, 1); + tree saved_scope = pp->enclosing_scope; + + if (TREE_CODE (fun) == ADDR_EXPR) + fun = TREE_OPERAND (fun, 0); + + /* In templates, where there is no way to tell whether a given + call uses an actual member function. So the parser builds + FUN as a COMPONENT_REF or a plain IDENTIFIER_NODE until + instantiation time. */ + if (TREE_CODE (fun) != FUNCTION_DECL) + ; + else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)) + { + tree object = code == AGGR_INIT_EXPR && AGGR_INIT_VIA_CTOR_P (t) + ? TREE_OPERAND (t, 2) + : TREE_VALUE (args); + + while (TREE_CODE (object) == NOP_EXPR) + object = TREE_OPERAND (object, 0); + + if (TREE_CODE (object) == ADDR_EXPR) + object = TREE_OPERAND (object, 0); + + if (TREE_CODE (TREE_TYPE (object)) != POINTER_TYPE) + { + pp_cxx_postfix_expression (pp, object); + pp_cxx_dot (pp); + } + else + { + pp_cxx_postfix_expression (pp, object); + pp_cxx_arrow (pp); + } + args = TREE_CHAIN (args); + pp->enclosing_scope = strip_pointer_operator (TREE_TYPE (object)); + } + + pp_cxx_postfix_expression (pp, fun); + pp->enclosing_scope = saved_scope; + pp_cxx_call_argument_list (pp, args); + } + if (code == AGGR_INIT_EXPR && AGGR_INIT_VIA_CTOR_P (t)) + { + pp_separate_with (pp, ','); + pp_cxx_postfix_expression (pp, TREE_OPERAND (t, 2)); + } + break; + + case BASELINK: + case VAR_DECL: + case PARM_DECL: + case FIELD_DECL: + case FUNCTION_DECL: + case OVERLOAD: + case CONST_DECL: + case TEMPLATE_DECL: + case RESULT_DECL: + pp_cxx_primary_expression (pp, t); + break; + case DYNAMIC_CAST_EXPR: case STATIC_CAST_EXPR: case REINTERPRET_CAST_EXPR: @@ -400,7 +432,7 @@ pp_cxx_postfix_expression (cxx_pretty_printer *pp, tree t) pp_cxx_type_id (pp, TREE_TYPE (t)); pp_cxx_end_template_argument_list (pp); pp_left_paren (pp); - pp_c_expression (pp_c_base (pp), TREE_OPERAND (t, 0)); + pp_cxx_expression (pp, TREE_OPERAND (t, 0)); pp_right_paren (pp); break; @@ -412,20 +444,20 @@ pp_cxx_postfix_expression (cxx_pretty_printer *pp, tree t) case TYPEID_EXPR: t = TREE_OPERAND (t, 0); - pp_c_identifier (pp_c_base (pp), "typeid"); + pp_cxx_identifier (pp, "typeid"); pp_left_paren (pp); if (TYPE_P (t)) pp_cxx_type_id (pp, t); else - pp_c_expression (pp_c_base (pp), t); + pp_cxx_expression (pp, t); pp_right_paren (pp); break; case PSEUDO_DTOR_EXPR: pp_cxx_postfix_expression (pp, TREE_OPERAND (t, 0)); - pp_dot (pp); + pp_cxx_dot (pp); pp_cxx_qualified_id (pp, TREE_OPERAND (t, 1)); - pp_colon_colon (pp); + pp_cxx_colon_colon (pp); pp_complement (pp); pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 2)); break; @@ -465,13 +497,11 @@ pp_cxx_new_expression (cxx_pretty_printer *pp, tree t) case NEW_EXPR: case VEC_NEW_EXPR: if (NEW_EXPR_USE_GLOBAL (t)) - pp_colon_colon (pp); - pp_c_identifier (pp_c_base (pp), "new"); + pp_cxx_colon_colon (pp); + pp_cxx_identifier (pp, "new"); if (TREE_OPERAND (t, 0)) { - pp_left_paren (pp); - pp_c_expression_list (pp_c_base (pp), TREE_OPERAND (t, 0)); - pp_right_paren (pp); + pp_cxx_call_argument_list (pp, TREE_OPERAND (t, 0)); pp_space (pp); } /* FIXME: array-types are built with one more element. */ @@ -485,7 +515,7 @@ pp_cxx_new_expression (cxx_pretty_printer *pp, tree t) else if (t == void_zero_node) ; /* OK, empty initializer list. */ else - pp_c_expression (pp_c_base (pp), t); + pp_cxx_expression (pp, t); pp_right_paren (pp); } break; @@ -507,8 +537,8 @@ pp_cxx_delete_expression (cxx_pretty_printer *pp, tree t) case DELETE_EXPR: case VEC_DELETE_EXPR: if (DELETE_EXPR_USE_GLOBAL (t)) - pp_colon_colon (pp); - pp_c_identifier (pp_c_base (pp), "delete"); + pp_cxx_colon_colon (pp); + pp_cxx_identifier (pp, "delete"); if (code == VEC_DELETE_EXPR) { pp_left_bracket (pp); @@ -560,6 +590,25 @@ pp_cxx_unary_expression (cxx_pretty_printer *pp, tree t) } } +/* cast-expression: + unary-expression + ( type-id ) cast-expression */ +static void +pp_cxx_cast_expression (cxx_pretty_printer *pp, tree t) +{ + switch (TREE_CODE (t)) + { + case CAST_EXPR: + pp_cxx_type_id (pp, TREE_TYPE (t)); + pp_cxx_call_argument_list (pp, TREE_OPERAND (t, 0)); + break; + + default: + pp_c_cast_expression (pp_c_base (pp), t); + break; + } +} + /* pm-expression: cast-expression pm-expression .* cast-expression @@ -580,14 +629,14 @@ pp_cxx_pm_expression (cxx_pretty_printer *pp, tree t) case MEMBER_REF: case DOTSTAR_EXPR: pp_cxx_pm_expression (pp, TREE_OPERAND (t, 0)); - pp_dot (pp); + pp_cxx_dot (pp); pp_star(pp); - pp_c_cast_expression (pp_c_base (pp), TREE_OPERAND (t, 1)); + pp_cxx_cast_expression (pp, TREE_OPERAND (t, 1)); break; default: - pp_c_cast_expression (pp_c_base (pp), t); + pp_cxx_cast_expression (pp, t); break; } } @@ -624,7 +673,6 @@ pp_cxx_multiplicative_expression (cxx_pretty_printer *pp, tree e) } } - /* conditional-expression: logical-or-expression logical-or-expression ? expression : assignment-expression */ @@ -637,7 +685,7 @@ pp_cxx_conditional_expression (cxx_pretty_printer *pp, tree e) pp_space (pp); pp_question (pp); pp_space (pp); - pp_c_expression (pp_c_base (pp), TREE_OPERAND (e, 1)); + pp_cxx_expression (pp, TREE_OPERAND (e, 1)); pp_space (pp); pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 2)); } @@ -645,27 +693,951 @@ pp_cxx_conditional_expression (cxx_pretty_printer *pp, tree e) pp_c_logical_or_expression (pp_c_base (pp), e); } +static void +pp_cxx_assignment_operator (cxx_pretty_printer *pp, tree t) +{ + const char *op; + + switch (TREE_CODE (t)) + { + case NOP_EXPR: + op = "="; + break; + + case PLUS_EXPR: + op = "+="; + break; + + case MINUS_EXPR: + op = "-="; + break; + + case TRUNC_DIV_EXPR: + op = "/="; + break; + + case TRUNC_MOD_EXPR: + op = "%="; + break; + + default: + op = tree_code_name[TREE_CODE (t)]; + break; + } + + pp_cxx_identifier (pp, op); +} + + /* assignment-expression: conditional-expression logical-or-expression assignment-operator assignment-expression + throw-expression - assignment-expression: one of + throw-expression: + throw assignment-expression(opt) + + assignment-operator: one of = *= /= %= += -= >>= <<= &= ^= |= */ static void pp_cxx_assignment_expression (cxx_pretty_printer *pp, tree e) { - if (TREE_CODE (e) == MODIFY_EXPR) + switch (TREE_CODE (e)) { + case MODIFY_EXPR: + case INIT_EXPR: pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0)); pp_space (pp); pp_equal (pp); pp_space (pp); pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 1)); + break; + + case THROW_EXPR: + pp_cxx_identifier (pp, "throw"); + if (TREE_OPERAND (e, 0)) + pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 0)); + break; + + case MODOP_EXPR: + pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0)); + pp_cxx_assignment_operator (pp, TREE_OPERAND (e, 1)); + pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 2)); + break; + + default: + pp_cxx_conditional_expression (pp, e); + break; } +} + +static void +pp_cxx_expression (cxx_pretty_printer *pp, tree t) +{ + switch (TREE_CODE (t)) + { + case STRING_CST: + case INTEGER_CST: + case REAL_CST: + pp_c_constant (pp_c_base (pp), t); + break; + + case RESULT_DECL: + pp_cxx_unqualified_id (pp, t); + break; + +#if 0 + case OFFSET_REF: +#endif + case SCOPE_REF: + case PTRMEM_CST: + pp_cxx_qualified_id (pp, t); + break; + + case OVERLOAD: + t = OVL_CURRENT (t); + case VAR_DECL: + case PARM_DECL: + case FIELD_DECL: + case CONST_DECL: + case FUNCTION_DECL: + case BASELINK: + case TEMPLATE_DECL: + case TEMPLATE_TYPE_PARM: + case TEMPLATE_PARM_INDEX: + pp_cxx_primary_expression (pp, t); + break; + + case CALL_EXPR: + case DYNAMIC_CAST_EXPR: + case STATIC_CAST_EXPR: + case REINTERPRET_CAST_EXPR: + case CONST_CAST_EXPR: +#if 0 + case MEMBER_REF: +#endif + case EMPTY_CLASS_EXPR: + case TYPEID_EXPR: + case PSEUDO_DTOR_EXPR: + case AGGR_INIT_EXPR: + pp_cxx_postfix_expression (pp, t); + break; + + case NEW_EXPR: + case VEC_NEW_EXPR: + pp_cxx_new_expression (pp, t); + break; + + case DELETE_EXPR: + case VEC_DELETE_EXPR: + pp_cxx_delete_expression (pp, t); + break; + + case CAST_EXPR: + pp_cxx_cast_expression (pp, t); + break; + + case OFFSET_REF: + case MEMBER_REF: + case DOTSTAR_EXPR: + pp_cxx_pm_expression (pp, t); + break; + + case MULT_EXPR: + case TRUNC_DIV_EXPR: + case TRUNC_MOD_EXPR: + pp_cxx_multiplicative_expression (pp, t); + break; + + case COND_EXPR: + pp_cxx_conditional_expression (pp, t); + break; + + case MODIFY_EXPR: + case INIT_EXPR: + case THROW_EXPR: + case MODOP_EXPR: + pp_cxx_assignment_expression (pp, t); + break; + + default: + pp_c_expression (pp_c_base (pp), t); + break; + } +} + + +/* Declarations. */ + +/* function-specifier: + inline + virtual + explicit */ +static void +pp_cxx_function_specifier (cxx_pretty_printer *pp, tree t) +{ + switch (TREE_CODE (t)) + { + case FUNCTION_DECL: + if (DECL_VIRTUAL_P (t)) + pp_cxx_identifier (pp, "virtual"); + else if (DECL_CONSTRUCTOR_P (t) && DECL_NONCONVERTING_P (t)) + pp_cxx_identifier (pp, "explicit"); + else + pp_c_function_specifier (pp_c_base (pp), t); + + default: + break; + } +} + +/* decl-specifier-seq: + decl-specifier-seq(opt) decl-specifier + + decl-specifier: + storage-class-specifier + type-specifier + function-specifier + friend + typedef */ +static void +pp_cxx_decl_specifier_seq (cxx_pretty_printer *pp, tree t) +{ + switch (TREE_CODE (t)) + { + case VAR_DECL: + case PARM_DECL: + case CONST_DECL: + case FIELD_DECL: + pp_cxx_storage_class_specifier (pp, t); + pp_cxx_decl_specifier_seq (pp, TREE_TYPE (t)); + break; + + case TYPE_DECL: + pp_cxx_identifier (pp, "typedef"); + pp_cxx_decl_specifier_seq (pp, TREE_TYPE (t)); + break; + + case RECORD_TYPE: + if (TYPE_PTRMEMFUNC_P (t)) + { + tree pfm = TYPE_PTRMEMFUNC_FN_TYPE (t); + pp_cxx_decl_specifier_seq (pp, TREE_TYPE (TREE_TYPE (pfm))); + pp_cxx_whitespace (pp); + pp_cxx_ptr_operator (pp, t); + } + break; + + case FUNCTION_DECL: + /* Constructors don't have return types. And conversion functions + do not have a type-specifier in their return types. */ + if (DECL_CONSTRUCTOR_P (t) || DECL_CONV_FN_P (t)) + pp_cxx_function_specifier (pp, t); + else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t)) + pp_cxx_decl_specifier_seq (pp, TREE_TYPE (TREE_TYPE (t))); + else + default: + pp_c_declaration_specifiers (pp_c_base (pp), t); + break; + } +} + +/* simple-type-specifier: + ::(opt) nested-name-specifier(opt) type-name + ::(opt) nested-name-specifier(opt) template(opt) template-id + char + wchar_t + bool + short + int + long + signed + unsigned + float + double + void */ +static void +pp_cxx_simple_type_specifier (cxx_pretty_printer *pp, tree t) +{ + switch (TREE_CODE (t)) + { + case RECORD_TYPE: + case UNION_TYPE: + case ENUMERAL_TYPE: + pp_cxx_qualified_id (pp, t); + break; + + case TEMPLATE_TYPE_PARM: + case TEMPLATE_PARM_INDEX: + pp_cxx_unqualified_id (pp, t); + break; + + case TYPENAME_TYPE: + pp_cxx_identifier (pp, "typename"); + pp_cxx_nested_name_specifier (pp, TYPE_CONTEXT (t)); + pp_cxx_unqualified_id (pp, TYPE_NAME (t)); + break; + + default: + pp_c_type_specifier (pp_c_base (pp), t); + break; + } +} + +/* type-specifier-seq: + type-specifier type-specifier-seq(opt) + + type-specifier: + simple-type-specifier + class-specifier + enum-specifier + elaborated-type-specifier + cv-qualifer */ + +static void +pp_cxx_type_specifier_seq (cxx_pretty_printer *pp, tree t) +{ + pp_c_type_qualifier_list (pp_c_base (pp), t); + + switch (TREE_CODE (t)) + { + case TEMPLATE_DECL: + case TEMPLATE_TYPE_PARM: + case TYPE_DECL: + case BOUND_TEMPLATE_TEMPLATE_PARM: + pp_cxx_simple_type_specifier (pp, t); + break; + + case METHOD_TYPE: + pp_cxx_type_specifier_seq (pp, TREE_TYPE (t)); + pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (t)); + pp_cxx_nested_name_specifier (pp, TYPE_METHOD_BASETYPE (t)); + break; + + default: + if (!(TREE_CODE (t) == FUNCTION_DECL && DECL_CONSTRUCTOR_P (t))) + pp_c_specifier_qualifier_list (pp_c_base (pp), t); + } +} + +/* ptr-operator: + * cv-qualifier-seq(opt) + & + ::(opt) nested-name-specifier * cv-qualifier-seq(opt) */ + +static void +pp_cxx_ptr_operator (cxx_pretty_printer *pp, tree t) +{ + if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL) + t = TREE_TYPE (t); + switch (TREE_CODE (t)) + { + case REFERENCE_TYPE: + case POINTER_TYPE: + if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE + || TYPE_PTR_TO_MEMBER_P (TREE_TYPE (t))) + pp_cxx_ptr_operator (pp, TREE_TYPE (t)); + if (TREE_CODE (t) == POINTER_TYPE) + { + pp_star (pp); + pp_cxx_cv_qualifier_seq (pp, t); + } + else + pp_ampersand (pp); + break; + + case RECORD_TYPE: + if (TYPE_PTRMEMFUNC_P (t)) + { + pp_cxx_left_paren (pp); + pp_cxx_nested_name_specifier (pp, TYPE_PTRMEMFUNC_OBJECT_TYPE (t)); + pp_star (pp); + break; + } + case OFFSET_TYPE: + if (TYPE_PTR_TO_MEMBER_P (t)) + { + pp_cxx_nested_name_specifier (pp, TYPE_PTRMEM_CLASS_TYPE (t)); + pp_star (pp); + pp_cxx_cv_qualifier_seq (pp, t); + break; + } + /* else fall trhough. */ + + default: + pp_unsupported_tree (pp, t); + break; + } +} + +static inline tree +pp_cxx_implicit_parameter_type (tree mf) +{ + return TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (mf)))); +} + +/* + parameter-declaration: + decl-specifier-seq declarator + decl-specifier-seq declarator = assignment-expression + decl-specifier-seq abstract-declarator(opt) + decl-specifier-seq abstract-declarator(opt) assignment-expression */ +static inline void +pp_cxx_parameter_declaration (cxx_pretty_printer *pp, tree t) +{ + pp_cxx_decl_specifier_seq (pp, t); + if (TYPE_P (t)) + pp_cxx_abstract_declarator (pp, t); + else + pp_cxx_declarator (pp, t); +} + +/* parameter-declaration-clause: + parameter-declaration-list(opt) ...(opt) + parameter-declaration-list , ... + + parameter-declaration-list: + parameter-declaration + parameter-declaration-list , parameter-declaration */ +static void +pp_cxx_parameter_declaration_clause (cxx_pretty_printer *pp, tree t) +{ + tree args = TYPE_P (t) ? NULL : FUNCTION_FIRST_USER_PARM (t); + tree types = TYPE_P (t) ? TYPE_ARG_TYPES (t) : FUNCTION_FIRST_USER_PARMTYPE (t); + const bool abstract = args == NULL + || pp_c_base (pp)->flags & pp_c_flag_abstract; + bool first = true; + + /* Skip artificial parameter for nonstatic member functions. */ + if (TREE_CODE (t) == METHOD_TYPE) + types = TREE_CHAIN (types); + + pp_cxx_left_paren (pp); + for (; args; args = TREE_CHAIN (args), types = TREE_CHAIN (types)) + { + if (!first) + pp_separate_with (pp, ','); + first = false; + pp_cxx_parameter_declaration (pp, abstract ? TREE_VALUE (types) : args); + if (!abstract && pp_c_base (pp)->flags & pp_cxx_flag_default_argument) + { + pp_cxx_whitespace (pp); + pp_equal (pp); + pp_cxx_whitespace (pp); + pp_cxx_assignment_expression (pp, TREE_PURPOSE (types)); + } + } + pp_cxx_right_paren (pp); +} + +/* exception-specification: + throw ( type-id-list(opt) ) + + type-id-list + type-id + type-id-list , type-id */ +static void +pp_cxx_exception_specification (cxx_pretty_printer *pp, tree t) +{ + tree ex_spec = TYPE_RAISES_EXCEPTIONS (t); + + if (!TYPE_NOTHROW_P (t) && ex_spec == NULL) + return; + pp_cxx_identifier (pp, "throw"); + pp_cxx_left_paren (pp); + for (; ex_spec && TREE_VALUE (ex_spec); ex_spec = TREE_CHAIN (ex_spec)) + { + pp_cxx_type_id (pp, TREE_VALUE (ex_spec)); + if (TREE_CHAIN (ex_spec)) + pp_separate_with (pp, ','); + } + pp_cxx_right_paren (pp); +} + +/* direct-declarator: + declarator-id + direct-declarator ( parameter-declaration-clause ) cv-qualifier-seq(opt) + exception-specification(opt) + direct-declaration [ constant-expression(opt) ] + ( declarator ) */ +static void +pp_cxx_direct_declarator (cxx_pretty_printer *pp, tree t) +{ + switch (TREE_CODE (t)) + { + case VAR_DECL: + case PARM_DECL: + case CONST_DECL: + case FIELD_DECL: + if (DECL_NAME (t)) + { + pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (t)); + pp_cxx_id_expression (pp, DECL_NAME (t)); + } + pp_cxx_abstract_declarator (pp, TREE_TYPE (t)); + break; + + case FUNCTION_DECL: + pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (TREE_TYPE (t))); + pp_cxx_id_expression (pp, t); + pp_cxx_parameter_declaration_clause (pp, t); + + if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t)) + { + pp_base (pp)->padding = pp_before; + pp_cxx_cv_qualifier_seq (pp, pp_cxx_implicit_parameter_type (t)); + } + + pp_cxx_exception_specification (pp, TREE_TYPE (t)); + break; + + case TYPENAME_TYPE: + case TEMPLATE_DECL: + case TEMPLATE_TYPE_PARM: + case TEMPLATE_PARM_INDEX: + break; + + default: + pp_c_direct_declarator (pp_c_base (pp), t); + break; + } +} + +/* declarator: + direct-declarator + ptr-operator declarator */ +static void +pp_cxx_declarator (cxx_pretty_printer *pp, tree t) +{ + pp_cxx_direct_declarator (pp, t); +} + +/* ctor-initializer: + : mem-initializer-list + + mem-initializer-list: + mem-initializer + mem-initializer , mem-initializer-list + + mem-initializer: + mem-initializer-id ( expression-list(opt) ) + + mem-initializer-id: + ::(opt) nested-name-specifier(opt) class-name + identifier */ +static void +pp_cxx_ctor_initializer (cxx_pretty_printer *pp, tree t) +{ + t = TREE_OPERAND (t, 0); + pp_cxx_whitespace (pp); + pp_colon (pp); + pp_cxx_whitespace (pp); + for (; t; t = TREE_CHAIN (t)) + { + pp_cxx_primary_expression (pp, TREE_PURPOSE (t)); + pp_cxx_call_argument_list (pp, TREE_VALUE (t)); + if (TREE_CHAIN (t)) + pp_separate_with (pp, ','); + } +} + +/* function-definition: + decl-specifier-seq(opt) declarator ctor-initializer(opt) function-body + decl-specifier-seq(opt) declarator function-try-block */ + +void +pp_cxx_function_definition (cxx_pretty_printer *pp, tree t) +{ + tree saved_scope = pp->enclosing_scope; + pp_cxx_decl_specifier_seq (pp, t); + pp_cxx_declarator (pp, t); + pp_needs_newline (pp) = true; + pp->enclosing_scope = DECL_CONTEXT (t); + if (DECL_SAVED_TREE (t)) + { + tree body = DECL_SAVED_TREE (t); + if (TREE_CODE (body) == COMPOUND_STMT + && TREE_CODE (COMPOUND_BODY (body)) == CTOR_INITIALIZER) + { + body = COMPOUND_BODY (body); + pp_cxx_ctor_initializer (pp, body); + body = TREE_CHAIN (body); + } + pp_cxx_statement (pp, body); + } + else + { + pp_cxx_semicolon (pp); + pp_needs_newline (pp) = true; + } + pp_flush (pp); + pp->enclosing_scope = saved_scope; +} + +/* abstract-declarator: + ptr-operator abstract-declarator(opt) + direct-abstract-declarator */ +static void +pp_cxx_abstract_declarator (cxx_pretty_printer *pp, tree t) +{ + if (TYPE_PTRMEM_P (t) || TYPE_PTRMEMFUNC_P (t)) + pp_cxx_right_paren (pp); + else if (POINTER_TYPE_P (t)) + { + if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE + || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE) + pp_cxx_right_paren (pp); + t = TREE_TYPE (t); + } + pp_cxx_direct_abstract_declarator (pp, t); +} + +/* direct-abstract-declarator: + direct-abstract-declarator(opt) ( parameter-declaration-clause ) + cv-quafilier-seq(opt) exception-specification(opt) + direct-abstract-declarator(opt) [ constant-expression(opt) ] + ( abstract-declarator ) */ +static void +pp_cxx_direct_abstract_declarator (cxx_pretty_printer *pp, tree t) +{ + switch (TREE_CODE (t)) + { + case REFERENCE_TYPE: + pp_cxx_abstract_declarator (pp, t); + break; + + case RECORD_TYPE: + if (TYPE_PTRMEMFUNC_P (t)) + pp_cxx_direct_abstract_declarator (pp, TYPE_PTRMEMFUNC_FN_TYPE (t)); + break; + + case METHOD_TYPE: + case FUNCTION_TYPE: + pp_cxx_parameter_declaration_clause (pp, t); + pp_cxx_direct_abstract_declarator (pp, TREE_TYPE (t)); + if (TREE_CODE (t) == METHOD_TYPE) + { + pp_base (pp)->padding = pp_before; + pp_cxx_cv_qualifier_seq + (pp, TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t)))); + } + pp_cxx_exception_specification (pp, t); + break; + + case TYPENAME_TYPE: + case TEMPLATE_TYPE_PARM: + case TEMPLATE_TEMPLATE_PARM: + case BOUND_TEMPLATE_TEMPLATE_PARM: + case UNBOUND_CLASS_TEMPLATE: + break; + + default: + pp_c_direct_abstract_declarator (pp_c_base (pp), t); + break; + } +} + +/* type-id: + type-specifier-seq abstract-declarator(opt) */ +static void +pp_cxx_type_id (cxx_pretty_printer *pp, tree t) +{ + pp_flags saved_flags = pp_c_base (pp)->flags; + pp_c_base (pp)->flags |= pp_c_flag_abstract; + + switch (TREE_CODE (t)) + { + case TYPE_DECL: + case UNION_TYPE: + case RECORD_TYPE: + case ENUMERAL_TYPE: + case TYPENAME_TYPE: + case BOUND_TEMPLATE_TEMPLATE_PARM: + case UNBOUND_CLASS_TEMPLATE: + case TEMPLATE_TEMPLATE_PARM: + case TEMPLATE_TYPE_PARM: + case TEMPLATE_PARM_INDEX: + case TEMPLATE_DECL: + case TYPEOF_TYPE: + case TEMPLATE_ID_EXPR: + /* FIXME: Should be pp_cxx_type_specifier_seq. */ + pp_cxx_type_specifier_seq (pp, t); + pp_cxx_declarator (pp, t); + break; + + default: + pp_c_type_id (pp_c_base (pp), t); + break; + } + + pp_c_base (pp)->flags = saved_flags; +} + +/* template-argument-list: + template-argument + template-argument-list, template-argument + + template-argument: + assignment-expression + type-id + template-name */ +static void +pp_cxx_template_argument_list (cxx_pretty_printer *pp, tree t) +{ + int i; + if (t == NULL) + return; + for (i = 0; i < TREE_VEC_LENGTH (t); ++i) + { + tree arg = TREE_VEC_ELT (t, i); + if (i != 0) + pp_separate_with (pp, ','); + if (TYPE_P (arg) || (TREE_CODE (arg) == TEMPLATE_DECL + && TYPE_P (DECL_TEMPLATE_RESULT (arg)))) + pp_cxx_type_id (pp, arg); + else + pp_cxx_expression (pp, arg); + } +} + + +static void +pp_cxx_exception_declaration (cxx_pretty_printer *pp, tree t) +{ + t = DECL_STMT_DECL (t); + pp_cxx_type_specifier_seq (pp, t); + if (TYPE_P (t)) + pp_cxx_abstract_declarator (pp, t); else - pp_cxx_conditional_expression (pp, e); + pp_cxx_declarator (pp, t); } +/* Statements. */ + +void +pp_cxx_statement (cxx_pretty_printer *pp, tree t) +{ + switch (TREE_CODE (t)) + { + case USING_STMT: + pp_cxx_identifier (pp, "using"); + pp_cxx_identifier (pp, "namespace"); + pp_cxx_qualified_id (pp, USING_STMT_NAMESPACE (t)); + break; + + case USING_DECL: + pp_cxx_identifier (pp, "using"); + pp_cxx_nested_name_specifier (pp, DECL_INITIAL (t)); + pp_cxx_unqualified_id (pp, DECL_NAME (t)); + break; + + case EH_SPEC_BLOCK: + break; + + /* try-block: + try compound-statement handler-seq */ + case TRY_BLOCK: + pp_maybe_newline_and_indent (pp, 0); + pp_cxx_identifier (pp, "try"); + pp_newline_and_indent (pp, 3); + pp_cxx_statement (pp, TRY_STMTS (t)); + pp_newline_and_indent (pp, -3); + if (CLEANUP_P (t)) + ; + else + pp_cxx_statement (pp, TRY_HANDLERS (t)); + break; + + /* + handler-seq: + handler handler-seq(opt) + + handler: + catch ( exception-declaration ) compound-statement + + exception-declaration: + type-specifier-seq declarator + type-specifier-seq abstract-declarator + ... */ + case HANDLER: + pp_cxx_identifier (pp, "catch"); + pp_cxx_left_paren (pp); + pp_cxx_exception_declaration (pp, HANDLER_PARMS (t)); + pp_cxx_right_paren (pp); + pp_indentation (pp) += 3; + pp_needs_newline (pp) = true; + pp_cxx_statement (pp, HANDLER_BODY (t)); + pp_indentation (pp) -= 3; + pp_needs_newline (pp) = true; + break; + + default: + pp_c_statement (pp_c_base (pp), t); + break; + } +} + +/* simple-declaration: + decl-specifier-seq(opt) init-declarator-list(opt) */ +static void +pp_cxx_simple_declaration (cxx_pretty_printer *pp, tree t) +{ + pp_cxx_decl_specifier_seq (pp, t); + pp_cxx_init_declarator (pp, t); + pp_cxx_semicolon (pp); + pp_needs_newline (pp) = true; +} + +/* + template-parameter-list: + template-parameter + template-parameter-list , template-parameter */ + +static inline void +pp_cxx_template_parameter_list (cxx_pretty_printer *pp, tree t) +{ + const int n = TREE_VEC_LENGTH (t); + int i; + for (i = 0; i < n; ++i) + { + if (i) + pp_separate_with (pp, ','); + pp_cxx_template_parameter (pp, TREE_VEC_ELT (t, i)); + } +} + +/* template-parameter: + type-parameter + parameter-declaration + + type-parameter: + class identifier(opt) + class identifier(op) = type-id + typename identifier(opt) + typename identifier(opt) = type-id + template < template-parameter-list > class identifier(opt) + template < template-parameter-list > class identifier(opt) = template-name +*/ +static void +pp_cxx_template_parameter (cxx_pretty_printer *pp, tree t) +{ + tree parameter = TREE_VALUE (t); + switch (TREE_CODE (parameter)) + { + case TYPE_DECL: + pp_cxx_identifier (pp, "class"); + if (DECL_NAME (parameter)) + pp_cxx_tree_identifier (pp, DECL_NAME (parameter)); + /* FIXME: Chech if we should print also default argument. */ + break; + + case PARM_DECL: + pp_cxx_parameter_declaration (pp, parameter); + break; + + case TEMPLATE_DECL: + break; + + default: + pp_unsupported_tree (pp, t); + break; + } +} + +/* + template-declaration: + export(opt) template < template-parameter-list > declaration */ +static void +pp_cxx_template_declaration (cxx_pretty_printer *pp, tree t) +{ + tree tmpl = most_general_template (t); + tree level; + int i = 0; + + pp_maybe_newline_and_indent (pp, 0); + for (level = DECL_TEMPLATE_PARMS (tmpl); level; level = TREE_CHAIN (level)) + { + pp_cxx_identifier (pp, "template"); + pp_cxx_begin_template_argument_list (pp); + pp_cxx_template_parameter_list (pp, TREE_VALUE (level)); + pp_cxx_end_template_argument_list (pp); + pp_newline_and_indent (pp, 3); + i += 3; + } + if (TREE_CODE (t) == FUNCTION_DECL && DECL_SAVED_TREE (t)) + pp_cxx_function_definition (pp, t); + else + pp_cxx_simple_declaration (pp, t); +} + +static void +pp_cxx_explicit_specialization (cxx_pretty_printer *pp, tree t) +{ + pp_unsupported_tree (pp, t); +} + +static void +pp_cxx_explicit_instantiation (cxx_pretty_printer *pp, tree t) +{ + pp_unsupported_tree (pp, t); +} + +/* + declaration: + block-declaration + function-definition + template-declaration + explicit-instantiation + explicit-specialization + linkage-specification + namespace-definition + + block-declaration: + simple-declaration + asm-definition + namespace-alias-definition + using-declaration + using-directive */ +void +pp_cxx_declaration (cxx_pretty_printer *pp, tree t) +{ + if (!DECL_LANG_SPECIFIC (t)) + pp_cxx_simple_declaration (pp, t); + else if (DECL_USE_TEMPLATE (t) > 1) + switch (DECL_USE_TEMPLATE (t)) + { + case 2: + pp_cxx_explicit_specialization (pp, t); + break; + + case 3: + pp_cxx_explicit_instantiation (pp, t); + break; + + default: + break; + } + else if (DECL_TEMPLATE_INFO (t)) + pp_cxx_template_declaration (pp, t); + else switch (TREE_CODE (t)) + { + case VAR_DECL: + case TYPE_DECL: + pp_cxx_simple_declaration (pp, t); + break; + + case FUNCTION_DECL: + if (DECL_SAVED_TREE (t)) + pp_cxx_function_definition (pp, t); + else + pp_cxx_simple_declaration (pp, t); + break; + + default: + pp_unsupported_tree (pp, t); + break; + } +} typedef c_pretty_print_fn pp_fun; @@ -674,22 +1646,30 @@ void pp_cxx_pretty_printer_init (cxx_pretty_printer *pp) { pp_c_pretty_printer_init (pp_c_base (pp)); + pp_set_line_maximum_length (pp, 0); pp->c_base.declaration = (pp_fun) pp_cxx_declaration; - pp->c_base.declaration_specifiers = (pp_fun) pp_cxx_declaration_specifiers; - pp->c_base.type_specifier = (pp_fun) pp_cxx_type_specifier; + pp->c_base.declaration_specifiers = (pp_fun) pp_cxx_decl_specifier_seq; + pp->c_base.function_specifier = (pp_fun) pp_cxx_function_specifier; + pp->c_base.type_specifier_seq = (pp_fun) pp_cxx_type_specifier_seq; pp->c_base.declarator = (pp_fun) pp_cxx_declarator; pp->c_base.direct_declarator = (pp_fun) pp_cxx_direct_declarator; - pp->c_base.parameter_declaration = (pp_fun) pp_cxx_parameter_declaration; + pp->c_base.parameter_list = (pp_fun) pp_cxx_parameter_declaration_clause; pp->c_base.type_id = (pp_fun) pp_cxx_type_id; - pp->c_base.statement = (pp_fun) pp_cxx_statement; + pp->c_base.abstract_declarator = (pp_fun) pp_cxx_abstract_declarator; + pp->c_base.direct_abstract_declarator = + (pp_fun) pp_cxx_direct_abstract_declarator; + pp->c_base.simple_type_specifier = (pp_fun)pp_cxx_simple_type_specifier; + + /* pp->c_base.statement = (pp_fun) pp_cxx_statement; */ + pp->c_base.id_expression = (pp_fun) pp_cxx_id_expression; pp->c_base.primary_expression = (pp_fun) pp_cxx_primary_expression; pp->c_base.postfix_expression = (pp_fun) pp_cxx_postfix_expression; pp->c_base.unary_expression = (pp_fun) pp_cxx_unary_expression; - pp->c_base.initializer = (pp_fun) pp_cxx_initializer; pp->c_base.multiplicative_expression = (pp_fun) pp_cxx_multiplicative_expression; pp->c_base.conditional_expression = (pp_fun) pp_cxx_conditional_expression; pp->c_base.assignment_expression = (pp_fun) pp_cxx_assignment_expression; - pp->enclosing_scope = NULL; + pp->c_base.expression = (pp_fun) pp_cxx_expression; + pp->enclosing_scope = global_namespace; } diff --git a/gcc/cp/cxx-pretty-print.h b/gcc/cp/cxx-pretty-print.h index 514f2534b40..1fa0b99b6ff 100644 --- a/gcc/cp/cxx-pretty-print.h +++ b/gcc/cp/cxx-pretty-print.h @@ -31,13 +31,14 @@ typedef enum { /* Ask for an qualified-id. */ pp_cxx_flag_qualified_id = 1 << pp_c_flag_last_bit, - pp_cxx_flag_global_scope = 1 << (pp_c_flag_last_bit + 1) + pp_cxx_flag_global_scope = 1 << (pp_c_flag_last_bit + 1), + pp_cxx_flag_default_argument = 1 << (pp_c_flag_last_bit + 2) } cxx_pretty_printer_flags; typedef struct { - struct c_pretty_print_info c_base; + c_pretty_printer c_base; /* This is the enclosing scope of the entity being pretty-printed. */ tree enclosing_scope; } cxx_pretty_printer; @@ -45,6 +46,7 @@ typedef struct void pp_cxx_pretty_printer_init (cxx_pretty_printer *); void pp_cxx_declaration (cxx_pretty_printer *, tree); +void pp_cxx_function_definition (cxx_pretty_printer *, tree); void pp_cxx_statement (cxx_pretty_printer *, tree); diff --git a/gcc/pretty-print.h b/gcc/pretty-print.h index 0fd00b33a27..80affe83e6f 100644 --- a/gcc/pretty-print.h +++ b/gcc/pretty-print.h @@ -203,6 +203,8 @@ struct pretty_print_info pp_base_indent (pp_base (PP)); \ pp_needs_newline (PP) = false; \ } while (0) +#define pp_maybe_newline_and_indent(PP, N) \ + if (pp_needs_newline (PP)) pp_newline_and_indent (PP, N) #define pp_separate_with(PP, C) \ do { \ pp_character (PP, C); \ |