PATCH RFA: Move EXPR_STMT to C++ frontend

Ian Lance Taylor ian@airs.com
Mon Apr 25 14:53:00 GMT 2005


The EXPR_STMT tree code is currently defined in the C/C++ common
frontend, but it is only used by the C++ frontend.  This patch moves
it into the C++ frontend.

The C++ frontend appears to use EXPR_STMT in a few places where it is
not needed.  I believe it is OK to pass an arbitrary expression code
to add_stmt--certainly the C frontend does so.  I think the only
interesting use of EXPR_STMT in the C++ frontend is to carry the
EXPR_STMT_STMT_EXPR_RESULT flag, which is only checked when expanding
a template.  That suggests that perhaps the C++ frontend should only
geneate EXPR_STMT when processing a template in finish_stmt_expr_expr.
However, I haven't tried to modify the C++ frontend in this way.

The only interesting aspect to this patch is the removal of
c_common_stmt_codes, which means that STATEMENT_CODE_P now always
returns false in the C frontend.  It happens that STATEMENT_CODE_P in
the C frontend is used only to set a flag which is used only by the
C++ frontend.  So it should now be possible to move that handling
entirely into the C++ frontend, which in the C frontend will save a
function call for each call to add_stmt.  That is for a followup
patch, though.

This patch also corrects a few calls which I moved into
cxx-pretty-print.c to call pp_cxx_expression rather than
pp_expression.

Tested with bootstrap and testsuite run on i686-pc-linux-gnu.  OK for
mainline?

Ian


./ChangeLog:
2005-04-25  Ian Lance Taylor  <ian@airs.com>

	* c-common.def (EXPR_STMT): Remove, moved to C++ frontend.
	* c-common.h (EXPR_STMT_EXPR): Don't define.
	(c_common_stmt_codes): Don't define.
	* c-dump.c (c_dump_tree): Remove EXPR_STMT case.
	* c-gimplify.c (gimplify_expr_stmt): Remove.
	(c_gimplify_expr): Remove EXPR_STMT case.
	* c-objc-common.c (c_objc_common_init): Remove stmt_codes and call
	to INIT_STATEMENT_CODES.
	* c-pretty-print.c (pp_c_statement): Just call dump_generic_node.

cp/ChangeLog:
2005-04-25  Ian Lance Taylor  <ian@airs.com>

	* cp-tree.def: Add EXPR_STMT.
	* cp-tree.h (cp_stmt_codes): Add EXPR_STMT.
	(EXPR_STMT_EXPR): Define.
	* cp-gimplify.c: Include "flags.h".
	(gimplify_expr_stmt): New static function.
	(cp_gimplify_expr): Handle EXPR_STMT.
	* cxx-pretty-print.c (pp_cxx_statement): Use pp_cxx_expression
	rather than pp_expression.
	(pp_cxx_statement): Handle EXPR_STMT.
	* dump.c (cp_dump_tree): Handle EXPR_STMT.
	* lex.c (cxx_init): Don't use c_common_stmt_codes in stmt_codes
	initializer.


Index: c-common.def
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-common.def,v
retrieving revision 1.29
diff -u -r1.29 c-common.def
--- c-common.def	20 Apr 2005 19:59:07 -0000	1.29
+++ c-common.def	25 Apr 2005 13:14:03 -0000
@@ -1,5 +1,5 @@
 /* This file contains the definitions and documentation for the
-   additional tree codes used in the GNU C++ compiler (see tree.def
+   additional tree codes used in the GNU C compiler (see tree.def
    for the standard codes).
    Copyright (C) 1987, 1988, 1990, 1993, 1997, 1998,
    1999, 2000, 2001, 2004, 2005 Free Software Foundation, Inc.
@@ -22,12 +22,8 @@
 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 02111-1307, USA.  */
 
-/* Tree nodes relevant to both C and C++. These were originally in
-cp-tree.def in the cp subdir.  */
-
-/* Used to represent an expression statement.  Use `EXPR_STMT_EXPR' to
-   obtain the expression.  */
-DEFTREECODE (EXPR_STMT, "expr_stmt", tcc_expression, 1)
+/* Tree nodes used in the C frontend.  These are also shared with the
+   C++ and Objective C frontends.  */
 
 /* A COMPOUND_LITERAL_EXPR represents a C99 compound literal.  The
    COMPOUND_LITERAL_EXPR_DECL_STMT is the a DECL_STMT containing the decl
Index: c-common.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-common.h,v
retrieving revision 1.287
diff -u -r1.287 c-common.h
--- c-common.h	21 Apr 2005 21:31:38 -0000	1.287
+++ c-common.h	25 Apr 2005 13:14:03 -0000
@@ -708,10 +708,6 @@
 #define STATEMENT_LIST_HAS_LABEL(NODE) \
   TREE_LANG_FLAG_3 (STATEMENT_LIST_CHECK (NODE))
 
-/* EXPR_STMT accessor. This gives the expression associated with an
-   expression statement.  */
-#define EXPR_STMT_EXPR(NODE)    TREE_OPERAND (EXPR_STMT_CHECK (NODE), 0)
-
 /* COMPOUND_LITERAL_EXPR accessors.  */
 #define COMPOUND_LITERAL_EXPR_DECL_STMT(NODE)		\
   TREE_OPERAND (COMPOUND_LITERAL_EXPR_CHECK (NODE), 0)
@@ -728,9 +724,6 @@
 
 #undef DEFTREECODE
 
-#define c_common_stmt_codes				\
-   EXPR_STMT
-
 /* TRUE if a code represents a statement.  The front end init
    langhook should take care of initialization of this array.  */
 extern bool statement_code_p[MAX_TREE_CODES];
Index: c-dump.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-dump.c,v
retrieving revision 1.31
diff -u -r1.31 c-dump.c
--- c-dump.c	20 Apr 2005 19:59:07 -0000	1.31
+++ c-dump.c	25 Apr 2005 13:14:03 -0000
@@ -54,11 +54,6 @@
 	dump_string (di, "bitfield");
       break;
 
-    case EXPR_STMT:
-      dump_stmt (di, t);
-      dump_child ("expr", EXPR_STMT_EXPR (t));
-      break;
-
     default:
       break;
     }
Index: c-gimplify.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-gimplify.c,v
retrieving revision 2.28
diff -u -r2.28 c-gimplify.c
--- c-gimplify.c	23 Apr 2005 21:27:31 -0000	2.28
+++ c-gimplify.c	25 Apr 2005 13:14:03 -0000
@@ -173,56 +173,11 @@
   return bind;
 }
 
-/*  Gimplify an EXPR_STMT node.
-
-    STMT is the statement node.
-
-    PRE_P points to the list where side effects that must happen before
-	STMT should be stored.
-
-    POST_P points to the list where side effects that must happen after
-	STMT should be stored.  */
-
-static enum gimplify_status
-gimplify_expr_stmt (tree *stmt_p)
-{
-  tree stmt = EXPR_STMT_EXPR (*stmt_p);
-
-  if (stmt == error_mark_node)
-    stmt = NULL;
-
-  /* Gimplification of a statement expression will nullify the
-     statement if all its side effects are moved to *PRE_P and *POST_P.
-
-     In this case we will not want to emit the gimplified statement.
-     However, we may still want to emit a warning, so we do that before
-     gimplification.  */
-  if (stmt && (extra_warnings || warn_unused_value))
-    {
-      if (!TREE_SIDE_EFFECTS (stmt))
-	{
-	  if (!IS_EMPTY_STMT (stmt)
-	      && !VOID_TYPE_P (TREE_TYPE (stmt))
-	      && !TREE_NO_WARNING (stmt))
-	    warning (0, "statement with no effect");
-	}
-      else if (warn_unused_value)
-	warn_if_unused_value (stmt, input_location);
-    }
-
-  if (stmt == NULL_TREE)
-    stmt = alloc_stmt_list ();
-
-  *stmt_p = stmt;
-
-  return GS_OK;
-}
-
 /* Gimplification of expression trees.  */
 
-/* Gimplify a C99 compound literal expression.  This just means adding the
-   DECL_EXPR before the current EXPR_STMT and using its anonymous decl
-   instead.  */
+/* Gimplify a C99 compound literal expression.  This just means adding
+   the DECL_EXPR before the current statement and using its anonymous
+   decl instead.  */
 
 static enum gimplify_status
 gimplify_compound_literal_expr (tree *expr_p, tree *pre_p)
@@ -266,9 +221,6 @@
     case COMPOUND_LITERAL_EXPR:
       return gimplify_compound_literal_expr (expr_p, pre_p);
 
-    case EXPR_STMT:
-      return gimplify_expr_stmt (expr_p);
-
     default:
       return GS_UNHANDLED;
     }
Index: c-objc-common.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-objc-common.c,v
retrieving revision 1.64
diff -u -r1.64 c-objc-common.c
--- c-objc-common.c	23 Apr 2005 21:27:32 -0000	1.64
+++ c-objc-common.c	25 Apr 2005 13:14:03 -0000
@@ -124,12 +124,6 @@
 bool
 c_objc_common_init (void)
 {
-  static const enum tree_code stmt_codes[] = {
-    c_common_stmt_codes
-  };
-
-  INIT_STATEMENT_CODES (stmt_codes);
-
   c_init_decl_processing ();
 
   if (c_common_init () == false)
Index: c-pretty-print.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-pretty-print.c,v
retrieving revision 1.62
diff -u -r1.62 c-pretty-print.c
--- c-pretty-print.c	20 Apr 2005 19:59:07 -0000	1.62
+++ c-pretty-print.c	25 Apr 2005 13:14:03 -0000
@@ -1910,40 +1910,16 @@
 
 /* Statements.  */
 
-/* statement:
-      labeled-statement
-      compound-statement
-      expression-statement
-      selection-statement
-      iteration-statement
-      jump-statement   */
-
 void
 pp_c_statement (c_pretty_printer *pp, tree stmt)
 {
-  enum tree_code code;
-
   if (stmt == NULL)
     return;
 
   if (pp_needs_newline (pp))
     pp_newline_and_indent (pp, 0);
 
-  code = TREE_CODE (stmt);
-  switch (code)
-    {
-      /* expression-statement:
-            expression(opt) ;  */
-    case EXPR_STMT:
-      pp_expression (pp, EXPR_STMT_EXPR (stmt));
-      pp_c_semicolon (pp);
-      pp_needs_newline (pp) = true;
-      break;
-
-    default:
-      dump_generic_node (pp_base (pp), stmt, pp_indentation (pp), 0, true);
-      break;
-    }
+  dump_generic_node (pp_base (pp), stmt, pp_indentation (pp), 0, true);
 }
 
 
Index: cp/cp-gimplify.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/cp-gimplify.c,v
retrieving revision 1.18
diff -u -r1.18 cp-gimplify.c
--- cp/cp-gimplify.c	9 Apr 2005 03:18:13 -0000	1.18
+++ cp/cp-gimplify.c	25 Apr 2005 13:14:04 -0000
@@ -31,6 +31,7 @@
 #include "tree-gimple.h"
 #include "hashtab.h"
 #include "pointer-set.h"
+#include "flags.h"
 
 /* Local declarations.  */
 
@@ -338,6 +339,41 @@
   *stmt_p = finish_bc_block (bc_break, break_block, *stmt_p);
 }
 
+/*  Gimplify an EXPR_STMT node.  */
+
+static void
+gimplify_expr_stmt (tree *stmt_p)
+{
+  tree stmt = EXPR_STMT_EXPR (*stmt_p);
+
+  if (stmt == error_mark_node)
+    stmt = NULL;
+
+  /* Gimplification of a statement expression will nullify the
+     statement if all its side effects are moved to *PRE_P and *POST_P.
+
+     In this case we will not want to emit the gimplified statement.
+     However, we may still want to emit a warning, so we do that before
+     gimplification.  */
+  if (stmt && (extra_warnings || warn_unused_value))
+    {
+      if (!TREE_SIDE_EFFECTS (stmt))
+	{
+	  if (!IS_EMPTY_STMT (stmt)
+	      && !VOID_TYPE_P (TREE_TYPE (stmt))
+	      && !TREE_NO_WARNING (stmt))
+	    warning (0, "statement with no effect");
+	}
+      else if (warn_unused_value)
+	warn_if_unused_value (stmt, input_location);
+    }
+
+  if (stmt == NULL_TREE)
+    stmt = alloc_stmt_list ();
+
+  *stmt_p = stmt;
+}
+
 /* Gimplify initialization from an AGGR_INIT_EXPR.  */
 
 static void
@@ -516,6 +552,11 @@
       ret = GS_ALL_DONE;
       break;
 
+    case EXPR_STMT:
+      gimplify_expr_stmt (expr_p);
+      ret = GS_OK;
+      break;
+
     default:
       ret = c_gimplify_expr (expr_p, pre_p, post_p);
       break;
Index: cp/cp-tree.def
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/cp-tree.def,v
retrieving revision 1.100
diff -u -r1.100 cp-tree.def
--- cp/cp-tree.def	24 Apr 2005 13:38:19 -0000	1.100
+++ cp/cp-tree.def	25 Apr 2005 13:14:04 -0000
@@ -305,6 +305,10 @@
    SWITCH_STMT_COND, SWITCH_STMT_BODY and SWITCH_STMT_TYPE, respectively.  */
 DEFTREECODE (SWITCH_STMT, "switch_stmt", tcc_statement, 3)
 
+/* Used to represent an expression statement.  Use `EXPR_STMT_EXPR' to
+   obtain the expression.  */
+DEFTREECODE (EXPR_STMT, "expr_stmt", tcc_expression, 1)
+
 DEFTREECODE (TAG_DEFN, "tag_defn", tcc_expression, 0)
 
 /* Template instantiation level node.
Index: cp/cp-tree.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/cp-tree.h,v
retrieving revision 1.1130
diff -u -r1.1130 cp-tree.h
--- cp/cp-tree.h	21 Apr 2005 14:01:56 -0000	1.1130
+++ cp/cp-tree.h	25 Apr 2005 13:14:05 -0000
@@ -852,7 +852,7 @@
    EH_SPEC_BLOCK,	USING_STMT,	TAG_DEFN,	\
    IF_STMT,		CLEANUP_STMT,	FOR_STMT,	\
    WHILE_STMT,		DO_STMT,	BREAK_STMT,	\
-   CONTINUE_STMT,	SWITCH_STMT
+   CONTINUE_STMT,	SWITCH_STMT,	EXPR_STMT
 enum languages { lang_c, lang_cplusplus, lang_java };
 
 /* Macros to make error reporting functions' lives easier.  */
@@ -2949,6 +2949,10 @@
 /* STMT_EXPR accessor.  */
 #define STMT_EXPR_STMT(NODE)    TREE_OPERAND (STMT_EXPR_CHECK (NODE), 0)
 
+/* EXPR_STMT accessor. This gives the expression associated with an
+   expression statement.  */
+#define EXPR_STMT_EXPR(NODE)    TREE_OPERAND (EXPR_STMT_CHECK (NODE), 0)
+
 /* An enumeration of the kind of tags that C++ accepts.  */
 enum tag_types {
   none_type = 0, /* Not a tag type.  */
Index: cp/cxx-pretty-print.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/cxx-pretty-print.c,v
retrieving revision 1.32
diff -u -r1.32 cxx-pretty-print.c
--- cp/cxx-pretty-print.c	20 Apr 2005 19:59:16 -0000	1.32
+++ cp/cxx-pretty-print.c	25 Apr 2005 13:14:05 -0000
@@ -1587,7 +1587,7 @@
       pp_cxx_identifier (pp, "switch");
       pp_space (pp);
       pp_cxx_left_paren (pp);
-      pp_expression (pp, SWITCH_STMT_COND (t));
+      pp_cxx_expression (pp, SWITCH_STMT_COND (t));
       pp_cxx_right_paren (pp);
       pp_indentation (pp) += 3;
       pp_needs_newline (pp) = true;
@@ -1604,7 +1604,7 @@
       pp_cxx_identifier (pp, "while");
       pp_space (pp);
       pp_cxx_left_paren (pp);
-      pp_expression (pp, WHILE_COND (t));
+      pp_cxx_expression (pp, WHILE_COND (t));
       pp_cxx_right_paren (pp);
       pp_newline_and_indent (pp, 3);
       pp_cxx_statement (pp, WHILE_BODY (t));
@@ -1620,7 +1620,7 @@
       pp_cxx_identifier (pp, "while");
       pp_space (pp);
       pp_cxx_left_paren (pp);
-      pp_expression (pp, DO_COND (t));
+      pp_cxx_expression (pp, DO_COND (t));
       pp_cxx_right_paren (pp);
       pp_cxx_semicolon (pp);
       pp_needs_newline (pp) = true;
@@ -1637,12 +1637,12 @@
       pp_needs_newline (pp) = false;
       pp_cxx_whitespace (pp);
       if (FOR_COND (t))
-	pp_expression (pp, FOR_COND (t));
+	pp_cxx_expression (pp, FOR_COND (t));
       pp_cxx_semicolon (pp);
       pp_needs_newline (pp) = false;
       pp_cxx_whitespace (pp);
       if (FOR_EXPR (t))
-	pp_expression (pp, FOR_EXPR (t));
+	pp_cxx_expression (pp, FOR_EXPR (t));
       pp_cxx_right_paren (pp);
       pp_newline_and_indent (pp, 3);
       pp_cxx_statement (pp, FOR_BODY (t));
@@ -1661,6 +1661,14 @@
       pp_needs_newline (pp) = true;
       break;
 
+      /* expression-statement:
+            expression(opt) ;  */
+    case EXPR_STMT:
+      pp_cxx_expression (pp, EXPR_STMT_EXPR (t));
+      pp_cxx_semicolon (pp);
+      pp_needs_newline (pp) = true;
+      break;
+
     case CLEANUP_STMT:
       pp_cxx_identifier (pp, "try");
       pp_newline_and_indent (pp, 2);
Index: cp/dump.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/dump.c,v
retrieving revision 1.83
diff -u -r1.83 dump.c
--- cp/dump.c	20 Apr 2005 19:59:16 -0000	1.83
+++ cp/dump.c	25 Apr 2005 13:14:05 -0000
@@ -459,6 +459,11 @@
       dump_child ("stmt", STMT_EXPR_STMT (t));
       break;
 
+    case EXPR_STMT:
+      dump_stmt (di, t);
+      dump_child ("expr", EXPR_STMT_EXPR (t));
+      break;
+
     default:
       break;
     }
Index: cp/lex.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/lex.c,v
retrieving revision 1.354
diff -u -r1.354 lex.c
--- cp/lex.c	23 Apr 2005 21:28:54 -0000	1.354
+++ cp/lex.c	25 Apr 2005 13:14:05 -0000
@@ -320,7 +320,6 @@
 cxx_init (void)
 {
   static const enum tree_code stmt_codes[] = {
-    c_common_stmt_codes,
     cp_stmt_codes
   };
 



More information about the Gcc-patches mailing list