diff options
author | Gabriel Dos Reis <gdr@nerim.net> | 2002-07-29 19:31:24 +0000 |
---|---|---|
committer | Gabriel Dos Reis <gdr@gcc.gnu.org> | 2002-07-29 19:31:24 +0000 |
commit | 61ccbcfd47fc677635169421f8ecb079fa99c5f8 (patch) | |
tree | 34373384a7a2f7e16ad1e90fbdd702a5943f0598 /gcc | |
parent | 09101f56b17ce1a29aee10e48eefa3cdd8a6be47 (diff) | |
download | gcc-61ccbcfd47fc677635169421f8ecb079fa99c5f8.zip gcc-61ccbcfd47fc677635169421f8ecb079fa99c5f8.tar.gz gcc-61ccbcfd47fc677635169421f8ecb079fa99c5f8.tar.bz2 |
Makefile.in (C_OBJS): Include c-pretty-print.o
2002-07-29 Gabriel Dos Reis <gdr@nerim.net>
* Makefile.in (C_OBJS): Include c-pretty-print.o
(c-pretty-print.o): Add depency rule.
* pretty-print.h: Add more macros.
* c-pretty-print.c: New file.
* c-pretty-print.h: Likewise.
cp/
2002-07-29 Gabriel Dos Reis <gdr@nerim.net>
* Make-lang.in (CXX_C_OBJS): Include.
From-SVN: r55845
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/ChangeLog | 8 | ||||
-rw-r--r-- | gcc/Makefile.in | 4 | ||||
-rw-r--r-- | gcc/c-pretty-print.c | 835 | ||||
-rw-r--r-- | gcc/c-pretty-print.h | 139 | ||||
-rw-r--r-- | gcc/cp/Make-lang.in | 2 | ||||
-rw-r--r-- | gcc/pretty-print.h | 15 |
6 files changed, 1000 insertions, 3 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 5afbebf..b7a5e9b 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,11 @@ +2002-07-29 Gabriel Dos Reis <gdr@nerim.net> + + * Makefile.in (C_OBJS): Include c-pretty-print.o + (c-pretty-print.o): Add depency rule. + * pretty-print.h: Add more macros. + * c-pretty-print.c: New file. + * c-pretty-print.h: Likewise. + 2002-07-29 Aldy Hernandez <aldyh@redhat.com> * config/rs6000/spe.h (__internal_ev_mwhgumian): Cast vector diff --git a/gcc/Makefile.in b/gcc/Makefile.in index 3fdd302..0ebba1e 100644 --- a/gcc/Makefile.in +++ b/gcc/Makefile.in @@ -718,7 +718,7 @@ C_AND_OBJC_OBJS = attribs.o c-errors.o c-lex.o c-pragma.o c-decl.o c-typeck.o \ c-objc-common.o c-dump.o libcpp.a $(C_TARGET_OBJS) # Language-specific object files for C. -C_OBJS = c-parse.o c-lang.o $(C_AND_OBJC_OBJS) +C_OBJS = c-parse.o c-lang.o c-pretty-print.o $(C_AND_OBJC_OBJS) # Language-independent object files. @@ -1230,6 +1230,8 @@ c-common.o : c-common.c $(CONFIG_H) $(SYSTEM_H) $(TREE_H) $(OBSTACK_H) \ $(C_COMMON_H) flags.h toplev.h output.h c-pragma.h $(RTL_H) $(GGC_H) \ $(EXPR_H) $(TM_P_H) builtin-types.def builtin-attrs.def $(TARGET_H) \ diagnostic.h tree-inline.h except.h gt-c-common.h real.h langhooks.h +c-pretty-print.o : c-pretty-print.c c-pretty-print.h pretty-print.h \ + $(C_COMMON_H) $(CONFIG_H) $(SYSTEM_H) real.h # A file used by all variants of C and some other languages. diff --git a/gcc/c-pretty-print.c b/gcc/c-pretty-print.c new file mode 100644 index 0000000..c859cd7 --- /dev/null +++ b/gcc/c-pretty-print.c @@ -0,0 +1,835 @@ +/* Subroutines common to both C and C++ pretty-printers. + Copyright (C) 2002 Free Software Foundation, Inc. + Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net> + +This file is part of GCC. + +GCC is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free +Software Foundation; either version 2, or (at your option) any later +version. + +GCC is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with GCC; see the file COPYING. If not, write to the Free +Software Foundation, 59 Temple Place - Suite 330, Boston, MA +02111-1307, USA. */ + +#include "config.h" +#include "system.h" +#include "real.h" +#include "c-pretty-print.h" + +/* literal */ +static void pp_c_char PARAMS ((c_pretty_print_info *, int)); +static void pp_c_character_literal PARAMS ((c_pretty_print_info *, tree)); +static void pp_c_bool_literal PARAMS ((c_pretty_print_info *, tree)); +static bool pp_c_enumerator PARAMS ((c_pretty_print_info *, tree)); +static void pp_c_integer_literal PARAMS ((c_pretty_print_info *, tree)); +static void pp_c_real_literal PARAMS ((c_pretty_print_info *, tree)); +static void pp_c_string_literal PARAMS ((c_pretty_print_info *, tree)); + +static void pp_c_primary_expression PARAMS ((c_pretty_print_info *, tree)); + +static void pp_c_unary_expression PARAMS ((c_pretty_print_info *, tree)); +static void pp_c_multiplicative_expression PARAMS ((c_pretty_print_info *, + tree)); +static void pp_c_additive_expression PARAMS ((c_pretty_print_info *, tree)); +static void pp_c_shift_expression PARAMS ((c_pretty_print_info *, tree)); +static void pp_c_relational_expression PARAMS ((c_pretty_print_info *, tree)); +static void pp_c_equality_expression PARAMS ((c_pretty_print_info *, tree)); +static void pp_c_and_expression PARAMS ((c_pretty_print_info *, tree)); +static void pp_c_exclusive_or_expression PARAMS ((c_pretty_print_info *, + tree)); +static void pp_c_inclusive_or_expression PARAMS ((c_pretty_print_info *, + tree)); +static void pp_c_logical_and_expression PARAMS ((c_pretty_print_info *, tree)); +static void pp_c_conditional_expression PARAMS ((c_pretty_print_info *, tree)); +static void pp_c_assignment_expression PARAMS ((c_pretty_print_info *, tree)); + +/* Declarations. */ + +/* Print out CV-qualifiers. Take care of possible extension. */ +void +pp_c_cv_qualifier (ppi, cv) + c_pretty_print_info *ppi; + int cv; +{ + if (cv & TYPE_QUAL_CONST) + pp_c_identifier (ppi, "const"); + if (cv & TYPE_QUAL_VOLATILE) + pp_c_identifier (ppi, "volatile"); + if (cv & TYPE_QUAL_RESTRICT) + pp_c_identifier (ppi, flag_isoc99 ? "restrict" : "__restrict__"); +} + + +/* Statements. */ + + +/* Expressions. */ + +/* Print out a c-char. */ +static void +pp_c_char (ppi, c) + c_pretty_print_info *ppi; + int c; +{ + switch (c) + { + case TARGET_NEWLINE: + pp_identifier (ppi, "\\n"); + break; + case TARGET_TAB: + pp_identifier (ppi, "\\t"); + break; + case TARGET_VT: + pp_identifier (ppi, "\\v"); + break; + case TARGET_BS: + pp_identifier (ppi, "\\b"); + break; + case TARGET_CR: + pp_identifier (ppi, "\\r"); + break; + case TARGET_FF: + pp_identifier (ppi, "\\f"); + break; + case TARGET_BELL: + pp_identifier (ppi, "\\a"); + break; + case '\\': + pp_identifier (ppi, "\\\\"); + break; + case '\'': + pp_identifier (ppi, "\\'"); + break; + case '\"': + pp_identifier (ppi, "\\\""); + break; + default: + if (ISPRINT (c)) + pp_character (ppi, c); + else + pp_format_integer (ppi, "\\%03o", (unsigned) c); + break; + } +} + +/* Print out a STRING literal. */ +static inline void +pp_c_string_literal (ppi, s) + c_pretty_print_info *ppi; + tree s; +{ + const char *p = TREE_STRING_POINTER (s); + int n = TREE_STRING_LENGTH (s) - 1; + int i; + pp_doublequote (ppi); + for (i = 0; i < n; ++i) + pp_c_char (ppi, p[i]); + pp_doublequote (ppi); +} + +/* Print out a CHARACTER literal. */ +static inline void +pp_c_character_literal (ppi, c) + c_pretty_print_info *ppi; + tree c; +{ + pp_quote (ppi); + pp_c_char (ppi, tree_low_cst (c, 0)); + pp_quote (ppi); +} + +/* Print out a BOOLEAN literal. */ +static inline void +pp_c_bool_literal (ppi, b) + c_pretty_print_info *ppi; + tree b; +{ + if (b == boolean_false_node || integer_zerop (b)) + { + if (c_language == clk_cplusplus) + pp_c_identifier (ppi, "false"); + else if (c_language == clk_c && flag_isoc99) + pp_c_identifier (ppi, "_False"); + else + pp_unsupported_tree (ppi, b); + } + else if (b == boolean_true_node) + { + if (c_language == clk_cplusplus) + pp_c_identifier (ppi, "true"); + else if (c_language == clk_c && flag_isoc99) + pp_c_identifier (ppi, "_True"); + else + pp_unsupported_tree (ppi, b); + } + else + pp_unsupported_tree (ppi, b); +} + +/* Attempt to print out an ENUMERATOR. Return true on success. Else return + false; that means the value was obtained by a cast, in which case + 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_enumerator (ppi, e) + c_pretty_print_info *ppi; + tree e; +{ + tree type = TREE_TYPE (e); + tree value; + + /* Find the name of this constant. */ + for (value = TYPE_VALUES (type); + value != NULL_TREE && !tree_int_cst_equal (TREE_VALUE (value), e); + value = TREE_CHAIN (value)) + ; + + if (value != NULL_TREE) + pp_c_tree_identifier (ppi, TREE_PURPOSE (value)); + else + { + /* Value must have been cast. */ + pp_c_left_paren (ppi); + pp_type_id (ppi, type); + pp_c_right_paren (ppi); + return false; + } + + return true; +} + +/* Print out an INTEGER constant value. */ +static void +pp_c_integer_literal (ppi, i) + c_pretty_print_info *ppi; + tree i; +{ + tree type = TREE_TYPE (i); + + if (type == boolean_type_node) + pp_c_bool_literal (ppi, i); + else if (type == char_type_node) + pp_c_character_literal (ppi, i); + else if (TREE_CODE (type) == ENUMERAL_TYPE + && pp_c_enumerator (ppi, i)) + ; + else + { + if (host_integerp (i, 0)) + pp_wide_integer (ppi, TREE_INT_CST_LOW (i)); + else + { + if (tree_int_cst_sgn (i) < 0) + { + static char format[10]; /* "%x%09999x\0" */ + if (!format[0]) + sprintf (format, "%%x%%0%dx", HOST_BITS_PER_INT / 4); + + pp_c_char (ppi, '-'); + i = build_int_2 (-TREE_INT_CST_LOW (i), + ~TREE_INT_CST_HIGH (i) + !TREE_INT_CST_LOW (i)); + sprintf (pp_buffer (ppi)->digit_buffer, format, + TREE_INT_CST_HIGH (i), TREE_INT_CST_LOW (i)); + pp_identifier (ppi, pp_buffer (ppi)->digit_buffer); + + } + } + } +} + +/* Print out a REAL value. */ +static inline void +pp_c_real_literal (ppi, r) + c_pretty_print_info *ppi; + tree r; +{ + REAL_VALUE_TO_DECIMAL (TREE_REAL_CST (r), "%.16g", + pp_buffer (ppi)->digit_buffer); + pp_identifier (ppi, pp_buffer(ppi)->digit_buffer); +} + + +void +pp_c_literal (ppi, e) + c_pretty_print_info *ppi; + tree e; +{ + switch (TREE_CODE (e)) + { + case INTEGER_CST: + pp_c_integer_literal (ppi, e); + break; + + case REAL_CST: + pp_c_real_literal (ppi, e); + break; + + case STRING_CST: + pp_c_string_literal (ppi, e); + break; + + default: + pp_unsupported_tree (ppi, e); + break; + } +} + +/* Pretty-print a C primary-expression. */ +static void +pp_c_primary_expression (ppi, e) + c_pretty_print_info *ppi; + tree e; +{ + switch (TREE_CODE (e)) + { + case VAR_DECL: + case PARM_DECL: + case FIELD_DECL: + case CONST_DECL: + case FUNCTION_DECL: + case LABEL_DECL: + e = DECL_NAME (e); + /* Fall through. */ + case IDENTIFIER_NODE: + pp_c_tree_identifier (ppi, e); + break; + + case ERROR_MARK: + pp_c_identifier (ppi, "<erroneous-expression>"); + break; + + case RESULT_DECL: + pp_c_identifier (ppi, "<return-value>"); + break; + + case INTEGER_CST: + case REAL_CST: + case STRING_CST: + pp_c_literal (ppi, e); + break; + + default: + /* Make sure this call won't cause any infinite loop. */ + pp_c_left_paren (ppi); + pp_c_expression (ppi, e); + pp_c_right_paren (ppi); + break; + } +} + +void +pp_c_postfix_expression (ppi, e) + c_pretty_print_info *ppi; + 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 ? "++" : "--"); + 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); + 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); + break; + + case COMPONENT_REF: + { + tree object = TREE_OPERAND (e, 0); + if (TREE_CODE (object) == INDIRECT_REF) + { + pp_postfix_expression (ppi, TREE_OPERAND (object, 0)); + pp_arrow (ppi); + } + else + { + pp_postfix_expression (ppi, object); + pp_dot (ppi); + } + pp_c_expression (ppi, TREE_OPERAND (e, 1)); + } + break; + + case CONSTRUCTOR: + case COMPLEX_CST: + case VECTOR_CST: + pp_unsupported_tree (ppi, e); + break; + + default: + pp_primary_expression (ppi, e); + break; + } +} + +/* Print out an expession-list; E is expected to be a TREE_LIST */ +void +pp_c_expression_list (ppi, e) + c_pretty_print_info *ppi; + tree e; +{ + for (; e != NULL_TREE; e = TREE_CHAIN (e)) + { + pp_c_assignment_expression (ppi, TREE_VALUE (e)); + if (TREE_CHAIN (e)) + pp_separate_with (ppi, ','); + } +} + +static void +pp_c_unary_expression (ppi, e) + c_pretty_print_info *ppi; + 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)); + break; + + case ADDR_EXPR: + case INDIRECT_REF: + case CONVERT_EXPR: + case NEGATE_EXPR: + case BIT_NOT_EXPR: + case TRUTH_NOT_EXPR: + if (code == ADDR_EXPR) + pp_ampersand (ppi); + else if (code == INDIRECT_REF) + pp_star (ppi); + else if (code == NEGATE_EXPR) + pp_minus (ppi); + else if (code == BIT_NOT_EXPR) + pp_complement (ppi); + else if (code == TRUTH_NOT_EXPR) + pp_exclamation (ppi); + pp_c_cast_expression (ppi, TREE_OPERAND (e, 0)); + break; + + case SIZEOF_EXPR: + case ALIGNOF_EXPR: + pp_c_identifier (ppi, code == SIZEOF_EXPR ? "sizeof" : "__alignof__"); + pp_c_whitespace (ppi); + 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); + } + else + pp_c_unary_expression (ppi, TREE_OPERAND (e, 0)); + break; + + default: + pp_postfix_expression (ppi, e); + break; + } +} + +void +pp_c_cast_expression (ppi, e) + c_pretty_print_info *ppi; + tree e; +{ + if (TREE_CODE (e) == 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)); + } + else + pp_unary_expression (ppi, e); +} + +static void +pp_c_multiplicative_expression (ppi, e) + c_pretty_print_info *ppi; + tree e; +{ + enum tree_code code = TREE_CODE (e); + switch (code) + { + case MULT_EXPR: + case TRUNC_DIV_EXPR: + case TRUNC_MOD_EXPR: + pp_c_multiplicative_expression (ppi, TREE_OPERAND (e, 0)); + pp_c_whitespace (ppi); + if (code == MULT_EXPR) + pp_star (ppi); + else if (code == TRUNC_DIV_EXPR) + pp_slash (ppi); + else + pp_modulo (ppi); + pp_c_whitespace (ppi); + pp_c_cast_expression (ppi, TREE_OPERAND (e, 1)); + break; + + default: + pp_c_cast_expression (ppi, e); + break; + } +} + +static inline void +pp_c_additive_expression (ppi, e) + c_pretty_print_info *ppi; + 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); + if (code == PLUS_EXPR) + pp_plus (ppi); + else + pp_minus (ppi); + pp_c_whitespace (ppi); + pp_multiplicative_expression (ppi, TREE_OPERAND (e, 1)); + break; + + default: + pp_multiplicative_expression (ppi, e); + break; + } +} + +static inline void +pp_c_shift_expression (ppi, e) + c_pretty_print_info *ppi; + 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)); + break; + + default: + pp_c_additive_expression (ppi, e); + } +} + +static void +pp_c_relational_expression (ppi, e) + c_pretty_print_info *ppi; + tree e; +{ + enum tree_code code = TREE_CODE (e); + switch (code) + { + case LT_EXPR: + case GT_EXPR: + case LE_EXPR: + case GE_EXPR: + pp_c_relational_expression (ppi, TREE_OPERAND (e, 0)); + pp_c_whitespace (ppi); + if (code == LT_EXPR) + pp_less (ppi); + else if (code == GT_EXPR) + pp_greater (ppi); + else if (code == LE_EXPR) + pp_identifier (ppi, "<="); + else if (code == GE_EXPR) + pp_identifier (ppi, ">="); + pp_c_whitespace (ppi); + pp_c_shift_expression (ppi, TREE_OPERAND (e, 1)); + break; + + default: + pp_c_shift_expression (ppi, e); + break; + } +} + +static inline void +pp_c_equality_expression (ppi, e) + c_pretty_print_info *ppi; + 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_maybe_whitespace (ppi); + pp_identifier (ppi, code == EQ_EXPR ? "==" : "!="); + pp_c_whitespace (ppi); + pp_c_relational_expression (ppi, TREE_OPERAND (e, 1)); + break; + + default: + pp_c_relational_expression (ppi, e); + break; + } +} + +static inline void +pp_c_and_expression (ppi, e) + c_pretty_print_info *ppi; + tree e; +{ + if (TREE_CODE (e) == BIT_AND_EXPR) + { + pp_c_and_expression (ppi, TREE_OPERAND (e, 0)); + pp_c_maybe_whitespace (ppi); + pp_ampersand (ppi); + pp_c_whitespace (ppi); + pp_c_equality_expression (ppi, TREE_OPERAND (e, 1)); + } + else + pp_c_equality_expression (ppi, e); +} + +static inline void +pp_c_exclusive_or_expression (ppi, e) + c_pretty_print_info *ppi; + 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)); + } + else + pp_c_and_expression (ppi, e); +} + +static inline void +pp_c_inclusive_or_expression (ppi, e) + c_pretty_print_info *ppi; + tree e; +{ + if (TREE_CODE (e) == BIT_IOR_EXPR) + { + pp_c_exclusive_or_expression (ppi, TREE_OPERAND (e, 0)); + pp_c_maybe_whitespace (ppi); + pp_bar (ppi); + pp_c_whitespace (ppi); + pp_c_exclusive_or_expression (ppi, TREE_OPERAND (e, 1)); + } + else + pp_c_exclusive_or_expression (ppi, e); +} + +static inline void +pp_c_logical_and_expression (ppi, e) + c_pretty_print_info *ppi; + tree e; +{ + if (TREE_CODE (e) == TRUTH_ANDIF_EXPR) + { + pp_c_logical_and_expression (ppi, TREE_OPERAND (e, 0)); + pp_c_maybe_whitespace (ppi); + pp_identifier (ppi, "&&"); + pp_c_whitespace (ppi); + pp_c_inclusive_or_expression (ppi, TREE_OPERAND (e, 1)); + } + else + pp_c_inclusive_or_expression (ppi, e); +} + +void +pp_c_logical_or_expression (ppi, e) + c_pretty_print_info *ppi; + tree e; +{ + if (TREE_CODE (e) == TRUTH_ORIF_EXPR) + { + pp_c_logical_or_expression (ppi, TREE_OPERAND (e, 0)); + pp_c_maybe_whitespace (ppi); + pp_identifier (ppi, "||"); + pp_c_whitespace (ppi); + pp_c_logical_and_expression (ppi, TREE_OPERAND (e, 1)); + } + else + pp_c_logical_and_expression (ppi, e); +} + +static void +pp_c_conditional_expression (ppi, e) + c_pretty_print_info *ppi; + tree e; +{ + if (TREE_CODE (e) == COND_EXPR) + { + pp_c_logical_or_expression (ppi, TREE_OPERAND (e, 0)); + pp_c_maybe_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)); + } + else + pp_c_logical_or_expression (ppi, e); +} + + +/* Pretty-print a C assignment-expression. */ +static void +pp_c_assignment_expression (ppi, e) + c_pretty_print_info *ppi; + tree e; +{ + if (TREE_CODE (e) == MODIFY_EXPR) + { + pp_c_unary_expression (ppi, TREE_OPERAND (e, 0)); + pp_c_maybe_whitespace (ppi); + pp_equal (ppi); + pp_whitespace (ppi); + pp_c_assignment_expression (ppi, TREE_OPERAND (e, 1)); + } + else + pp_c_conditional_expression (ppi, e); +} + +/* Pretty-print an expression. */ +void +pp_c_expression (ppi, e) + c_pretty_print_info *ppi; + tree e; +{ + switch (TREE_CODE (e)) + { + case INTEGER_CST: + pp_c_integer_literal (ppi, e); + break; + + case REAL_CST: + pp_c_real_literal (ppi, e); + break; + + case STRING_CST: + pp_c_string_literal (ppi, e); + break; + + case FUNCTION_DECL: + case VAR_DECL: + case CONST_DECL: + case PARM_DECL: + case RESULT_DECL: + case FIELD_DECL: + case LABEL_DECL: + case ERROR_MARK: + pp_c_primary_expression (ppi, e); + break; + + case POSTINCREMENT_EXPR: + case POSTDECREMENT_EXPR: + case ARRAY_REF: + case CALL_EXPR: + case COMPONENT_REF: + case CONSTRUCTOR: + case COMPLEX_CST: + case VECTOR_CST: + pp_c_postfix_expression (ppi, e); + break; + + case CONVERT_EXPR: + pp_c_cast_expression (ppi, e); + break; + + case MULT_EXPR: + case TRUNC_MOD_EXPR: + case TRUNC_DIV_EXPR: + pp_c_multiplicative_expression (ppi, e); + break; + + case LSHIFT_EXPR: + case RSHIFT_EXPR: + pp_c_shift_expression (ppi, e); + break; + + case LT_EXPR: + case GT_EXPR: + case LE_EXPR: + case GE_EXPR: + pp_c_relational_expression (ppi, e); + break; + + case BIT_AND_EXPR: + pp_c_and_expression (ppi, e); + break; + + case BIT_XOR_EXPR: + pp_c_exclusive_or_expression (ppi, e); + break; + + case BIT_IOR_EXPR: + pp_c_inclusive_or_expression (ppi, e); + break; + + case TRUTH_ANDIF_EXPR: + pp_c_logical_and_expression (ppi, e); + break; + + case TRUTH_ORIF_EXPR: + pp_c_logical_or_expression (ppi, e); + break; + + case COND_EXPR: + pp_c_conditional_expression (ppi, e); + break; + + case MODIFY_EXPR: + pp_c_assignment_expression (ppi, e); + break; + + case NOP_EXPR: + pp_c_expression (ppi, TREE_OPERAND (e, 0)); + 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); + break; + + + default: + pp_unsupported_tree (ppi, e); + break; + } +} + diff --git a/gcc/c-pretty-print.h b/gcc/c-pretty-print.h new file mode 100644 index 0000000..f9b1d6b --- /dev/null +++ b/gcc/c-pretty-print.h @@ -0,0 +1,139 @@ +/* Various declarations for the C and C++ pretty-printers. + Copyright (C) 2002 Free Software Foundation, Inc. + Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net> + +This file is part of GCC. + +GCC is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free +Software Foundation; either version 2, or (at your option) any later +version. + +GCC is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with GCC; see the file COPYING. If not, write to the Free +Software Foundation, 59 Temple Place - Suite 330, Boston, MA +02111-1307, USA. */ + +#include "tree.h" +#include "c-common.h" +#include "pretty-print.h" + + +/* The data type used to bundle information necessary for pretty-printing + a C or C++ entity. */ +typedef struct c_pretty_print_info c_pretty_print_info; + +/* The type of a C pretty-printer 'member' function. */ +typedef void (*c_pretty_print_fn) PARAMS ((c_pretty_print_info *, tree)); + +struct c_pretty_print_info +{ + struct pretty_print_info base; + /* Points to the first element of an array of offset-list. + Not used yet. */ + int *offset_list; + + /* These must be overriden by each of the C and C++ front-end to + reflect their understanding of syntatic productions when they differ. */ + c_pretty_print_fn declaration; + c_pretty_print_fn declaration_specifiers; + c_pretty_print_fn type_specifier; + c_pretty_print_fn declarator; + c_pretty_print_fn direct_declarator; + c_pretty_print_fn parameter_declaration; + c_pretty_print_fn type_id; + + c_pretty_print_fn statement; + + c_pretty_print_fn primary_expression; + c_pretty_print_fn postfix_expression; + c_pretty_print_fn unary_expression; + c_pretty_print_fn multiplicative_expression; + c_pretty_print_fn conditional_expression; + c_pretty_print_fn assignment_expression; +}; + +#define pp_buffer(PPI) (PPI)->base.buffer +#define pp_c_left_paren(PPI) \ + do { \ + pp_left_paren (PPI); \ + (PPI)->base.padding = pp_none; \ + } while (0) +#define pp_c_right_paren(PPI) \ + do { \ + pp_right_paren (PPI); \ + (PPI)->base.padding = pp_none; \ + } while (0) +#define pp_c_left_bracket(PPI) \ + do { \ + pp_left_bracket (PPI); \ + (PPI)->base.padding = pp_none; \ + } while (0) +#define pp_c_right_bracket(PPI) \ + do { \ + pp_right_bracket (PPI); \ + (PPI)->base.padding = pp_none; \ + } while (0) +#define pp_c_whitespace(PPI) \ + do { \ + pp_whitespace (PPI); \ + (PPI)->base.padding = pp_none; \ + } while (0) +#define pp_c_maybe_whitespace(PPI) \ + do { \ + if ((PPI)->base.padding != pp_none) \ + pp_c_whitespace (PPI); \ + } while (0) +#define pp_c_identifier(PPI, ID) \ + do { \ + pp_c_maybe_whitespace (PPI); \ + pp_identifier (PPI, ID); \ + (PPI)->base.padding = pp_before; \ + } while (0) + +#define pp_c_tree_identifier(PPI, ID) \ + pp_c_identifier (PPI, IDENTIFIER_POINTER (ID)) + + +#define pp_declaration(PPI, T) (*(PPI)->declaration) (PPI, T) +#define pp_declaration_specifiers(PPI, D) \ + (*(PPI)->declaration_specifiers) (PPI, D) +#define pp_type_specifier(PPI, D) (*(PPI)->type_specifier) (PPI, D) +#define pp_declarator(PPI, D) (*(PPI)->declarator) (PPI, D) +#define pp_direct_declarator(PPI, D) (*(PPI)->direct_declarator) (PPI, D) +#define pp_parameter_declaration(PPI, T) \ + (*(PPI)->parameter_declaration) (PPI, T) +#define pp_type_id(PPI, D) (*(PPI)->type_id) (PPI, D) + +#define pp_statement(PPI, S) (*(PPI)->statement) (PPI, S) + +#define pp_primary_expression(PPI, E) (*(PPI)->primary_expression) (PPI, E) +#define pp_postfix_expression(PPI, E) (*(PPI)->postfix_expression) (PPI, E) +#define pp_unary_expression(PPI, E) (*(PPI)->unary_expression) (PPI, E) +#define pp_multiplicative_expression(PPI, E)\ + (*(PPI)->multiplicative_expression) (PPI, E) +#define pp_conditional_expession(PPI, E) \ + (*(PPI)->conditional_expression (PPI, E)) +#define pp_assignment_expression(PPI, E) \ + (*(PPI)->assignment_expression) (PPI, E) + + +/* Declarations. */ +void pp_c_cv_qualifier PARAMS ((c_pretty_print_info *, int)); +void pp_c_parameter_declaration_clause PARAMS ((c_pretty_print_info *, tree)); +void pp_c_declaration PARAMS ((c_pretty_print_info *, tree)); +void pp_c_statement PARAMS ((c_pretty_print_info *, tree)); +void pp_c_expression PARAMS ((c_pretty_print_info *, tree)); + +/* Expressions. */ +void pp_c_expression PARAMS ((c_pretty_print_info *, tree)); +void pp_c_logical_or_expression PARAMS ((c_pretty_print_info *, tree)); +void pp_c_expression_list PARAMS ((c_pretty_print_info *, tree)); +void pp_c_cast_expression PARAMS ((c_pretty_print_info *, tree)); +void pp_c_postfix_expression PARAMS ((c_pretty_print_info *, tree)); +void pp_c_literal PARAMS ((c_pretty_print_info *, tree)); diff --git a/gcc/cp/Make-lang.in b/gcc/cp/Make-lang.in index 44966f4..549380b 100644 --- a/gcc/cp/Make-lang.in +++ b/gcc/cp/Make-lang.in @@ -96,7 +96,7 @@ $(DEMANGLER_PROG): cxxmain.o underscore.o $(LIBDEPS) # The compiler itself. # Shared with C front end: CXX_C_OBJS = attribs.o c-common.o c-format.o c-pragma.o c-semantics.o c-lex.o \ - c-dump.o $(CXX_TARGET_OBJS) + c-dump.o $(CXX_TARGET_OBJS) c-pretty-print.o # Language-specific object files. CXX_OBJS = cp/call.o cp/decl.o cp/expr.o cp/pt.o cp/typeck2.o \ diff --git a/gcc/pretty-print.h b/gcc/pretty-print.h index d0e52de..958e1ee 100644 --- a/gcc/pretty-print.h +++ b/gcc/pretty-print.h @@ -53,7 +53,20 @@ struct pretty_print_info #define pp_colon(PPI) output_add_character (pp_buffer (PPI), ':') #define pp_colon_colon(PPI) output_add_string (pp_buffer (PPI), "::") #define pp_arrow(PPI) output_add_string (pp_buffer (PPI), "->") +#define pp_equal(PPI) output_add_character (pp_buffer (PPI), '=') +#define pp_question(PPI) output_add_character (pp_buffer (PPI), '?') +#define pp_bar(PPI) output_add_character (pp_buffer (PPI), '|') +#define pp_carret(PPI) output_add_character (pp_buffer (PPI), '^') +#define pp_ampersand(PPI) output_add_character (pp_buffer (PPI), '&') +#define pp_less(PPI) output_add_character (pp_buffer (PPI), '<') +#define pp_greater(PPI) output_add_character (pp_buffer (PPI), '>') +#define pp_plus(PPI) output_add_character (pp_buffer (PPI), '+') +#define pp_minus(PPI) output_add_character (pp_buffer (PPI), '-') #define pp_star(PPI) output_add_character (pp_buffer (PPI), '*') +#define pp_slash(PPI) output_add_character (pp_buffer (PPI), '/') +#define pp_modulo(PPI) output_add_character (pp_buffer (PPI), '%') +#define pp_exclamation(PPI) output_add_character (pp_buffer (PPI), '!') +#define pp_complement(PPI) output_add_character (pp_buffer (PPI), '~') #define pp_quote(PPI) output_add_character (pp_buffer (PPI), '\'') #define pp_backquote(PPI) output_add_character (pp_buffer (PPI), '`') #define pp_doublequote(PPI) output_add_character (pp_buffer (PPI), '"') @@ -80,7 +93,7 @@ struct pretty_print_info #define pp_tree_identifier(PPI, T) pp_identifier(PPI, IDENTIFIER_POINTER (T)) #define pp_unsupported_tree(PPI, T) \ - output_verbatim (pp_buffer((PPI), "#`%s' not supported by %s#",\ + output_verbatim (pp_buffer(PPI), "#`%s' not supported by %s#",\ tree_code_name[(int) TREE_CODE (T)], __FUNCTION__) #endif /* GCC_PRETTY_PRINT_H */ |