This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[C++ PATCH]: Commonize sizeof & alignof handling


Mark,
this patch commonizes the handling of alignof and sizeof by sending them
throught the same two functions, and having the same parser code process
them. It is separated out from my fixing of regression 12037, which got
tangled up in this.

booted & tested on i686-pc-linux-gnu, ok?

nathan
--
Nathan Sidwell    ::   http://www.codesourcery.com   ::     CodeSourcery LLC
         The voices in my head said this was stupid too
nathan@codesourcery.com    ::     http://www.planetfall.pwp.blueyonder.co.uk

2003-09-03  Nathan Sidwell  <nathan@codesourcery.com>

	PR c++/12037
	* cp-tree.h (finish_sizeof, finish_alignof): Remove.
	(expr_sizeof): Replace with ...
	(cxx_sizeof_or_alignof_expr): ... here.
	(cxx_sizeof_or_alignof_type): Make complain parameter a bool.
	* parser.c (cp_parser_unary_expression): Commonize alignof and
	sizeof handling.
	* pt.c (tsubst_copy_and_build): Adjust alignof and sizeof
	substitution.
	* semantics.c (finish_sizeof, finish_alignof): Remove.
	* typeck.c (cxx_sizeof_or_alignof_type): Complain parameter
	becomes bool. Set TREE_READONLY.
	(expr_sizeof): Replace with ...
	(cxx_sizeof_or_alignof_expr): ... here. Clear TREE_SIDE_EFFECTS.

Index: cp/cp-tree.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/cp-tree.h,v
retrieving revision 1.909
diff -c -3 -p -r1.909 cp-tree.h
*** cp/cp-tree.h	1 Sep 2003 19:17:56 -0000	1.909
--- cp/cp-tree.h	3 Sep 2003 09:11:48 -0000
*************** extern tree finish_id_expression        
*** 4132,4139 ****
  						 bool, bool, bool *, 
  						 const char **);
  extern tree finish_typeof			(tree);
- extern tree finish_sizeof			(tree);
- extern tree finish_alignof			(tree);
  extern void finish_decl_cleanup                 (tree, tree);
  extern void finish_eh_cleanup                   (tree);
  extern void expand_body                         (tree);
--- 4134,4139 ----
*************** extern bool comptypes				(tree, tree, in
*** 4246,4253 ****
  extern bool compparms				(tree, tree);
  extern int comp_cv_qualification                (tree, tree);
  extern int comp_cv_qual_signature               (tree, tree);
! extern tree expr_sizeof				(tree);
! extern tree cxx_sizeof_or_alignof_type    (tree, enum tree_code, int);
  #define cxx_sizeof_nowarn(T) cxx_sizeof_or_alignof_type (T, SIZEOF_EXPR, false)
  extern tree inline_conversion			(tree);
  extern tree decay_conversion			(tree);
--- 4246,4253 ----
  extern bool compparms				(tree, tree);
  extern int comp_cv_qualification                (tree, tree);
  extern int comp_cv_qual_signature               (tree, tree);
! extern tree cxx_sizeof_or_alignof_expr    (tree, enum tree_code);
! extern tree cxx_sizeof_or_alignof_type    (tree, enum tree_code, bool);
  #define cxx_sizeof_nowarn(T) cxx_sizeof_or_alignof_type (T, SIZEOF_EXPR, false)
  extern tree inline_conversion			(tree);
  extern tree decay_conversion			(tree);
Index: cp/parser.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/parser.c,v
retrieving revision 1.105
diff -c -3 -p -r1.105 parser.c
*** cp/parser.c	27 Aug 2003 00:49:19 -0000	1.105
--- cp/parser.c	3 Sep 2003 09:12:29 -0000
*************** cp_parser_unary_expression (cp_parser *p
*** 3962,3993 ****
        switch (keyword)
  	{
  	case RID_ALIGNOF:
- 	  {
- 	    /* Consume the `alignof' token.  */
- 	    cp_lexer_consume_token (parser->lexer);
- 	    /* Parse the operand.  */
- 	    return finish_alignof (cp_parser_sizeof_operand 
- 				   (parser, keyword));
- 	  }
- 
  	case RID_SIZEOF:
  	  {
  	    tree operand;
  	    
  	    /* Consume the `sizeof' token.  */
  	    cp_lexer_consume_token (parser->lexer);
  	    /* Parse the operand.  */
  	    operand = cp_parser_sizeof_operand (parser, keyword);
  
! 	    /* If the type of the operand cannot be determined build a
! 	       SIZEOF_EXPR.  */
! 	    if (TYPE_P (operand)
! 		? dependent_type_p (operand)
! 		: type_dependent_expression_p (operand))
! 	      return build_min (SIZEOF_EXPR, size_type_node, operand);
! 	    /* Otherwise, compute the constant value.  */
  	    else
! 	      return finish_sizeof (operand);
  	  }
  
  	case RID_NEW:
--- 3962,3982 ----
        switch (keyword)
  	{
  	case RID_ALIGNOF:
  	case RID_SIZEOF:
  	  {
  	    tree operand;
+ 	    enum tree_code op;
  	    
+ 	    op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
  	    /* Consume the `sizeof' token.  */
  	    cp_lexer_consume_token (parser->lexer);
  	    /* Parse the operand.  */
  	    operand = cp_parser_sizeof_operand (parser, keyword);
  
! 	    if (TYPE_P (operand))
! 	      return cxx_sizeof_or_alignof_type (operand, op, true);
  	    else
! 	      return cxx_sizeof_or_alignof_expr (operand, op);
  	  }
  
  	case RID_NEW:
Index: cp/pt.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/pt.c,v
retrieving revision 1.769
diff -c -3 -p -r1.769 pt.c
*** cp/pt.c	1 Sep 2003 19:17:56 -0000	1.769
--- cp/pt.c	3 Sep 2003 09:12:53 -0000
*************** tsubst_copy_and_build (tree t, 
*** 8045,8054 ****
  	  op1 = RECUR (op1);
  	  --skip_evaluation;
  	}
!       if (TREE_CODE (t) == SIZEOF_EXPR)
! 	return finish_sizeof (op1);
        else
! 	return finish_alignof (op1);
  
      case MODOP_EXPR:
        return build_x_modify_expr
--- 8045,8054 ----
  	  op1 = RECUR (op1);
  	  --skip_evaluation;
  	}
!       if (TYPE_P (op1))
! 	return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), true);
        else
! 	return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t));
  
      case MODOP_EXPR:
        return build_x_modify_expr
Index: cp/semantics.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/semantics.c,v
retrieving revision 1.353
diff -c -3 -p -r1.353 semantics.c
*** cp/semantics.c	29 Aug 2003 23:21:12 -0000	1.353
--- cp/semantics.c	3 Sep 2003 09:13:01 -0000
*************** finish_typeof (tree expr)
*** 2661,2686 ****
    return type;
  }
  
- /* Compute the value of the `sizeof' operator.  */
- 
- tree
- finish_sizeof (tree t)
- {
-   return TYPE_P (t) ? cxx_sizeof (t) : expr_sizeof (t);
- }
- 
- /* Implement the __alignof keyword: Return the minimum required
-    alignment of T, measured in bytes.  */
- 
- tree
- finish_alignof (tree t)
- {
-   if (processing_template_decl)
-     return build_min (ALIGNOF_EXPR, size_type_node, t);
- 
-   return TYPE_P (t) ? cxx_alignof (t) : c_alignof_expr (t);
- }
- 
  /* Generate RTL for the statement T, and its substatements, and any
     other statements at its nesting level.  */
  
--- 2661,2666 ----
Index: cp/typeck.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/typeck.c,v
retrieving revision 1.497
diff -c -3 -p -r1.497 typeck.c
*** cp/typeck.c	1 Sep 2003 19:17:56 -0000	1.497
--- cp/typeck.c	3 Sep 2003 09:13:19 -0000
*************** compparms (tree parms1, tree parms2)
*** 1178,1192 ****
  
  
  tree
! cxx_sizeof_or_alignof_type (tree type, enum tree_code op, int complain)
  {
    enum tree_code type_code;
    tree value;
    const char *op_name;
  
    my_friendly_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR, 20020720);
    if (processing_template_decl)
!     return build_min (op, size_type_node, type);
    
    op_name = operator_name_info[(int) op].name;
  
--- 1178,1199 ----
  
  
  tree
! cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain)
  {
    enum tree_code type_code;
    tree value;
    const char *op_name;
  
    my_friendly_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR, 20020720);
+   if (type == error_mark_node)
+     return error_mark_node;
+   
    if (processing_template_decl)
!     {
!       value = build_min (op, size_type_node, type);
!       TREE_READONLY (value) = 1;
!       return value;
!     }
    
    op_name = operator_name_info[(int) op].name;
  
*************** cxx_sizeof_or_alignof_type (tree type, e
*** 1206,1234 ****
  }
  
  tree
! expr_sizeof (tree e)
  {
    if (processing_template_decl)
!     return build_min (SIZEOF_EXPR, size_type_node, e);
! 
    if (TREE_CODE (e) == COMPONENT_REF
        && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
-     error ("sizeof applied to a bit-field");
-   if (is_overloaded_fn (e))
      {
!       pedwarn ("ISO C++ forbids applying `sizeof' to an expression of function type");
!       return c_sizeof (char_type_node);
      }
    else if (type_unknown_p (e))
      {
        cxx_incomplete_type_error (e, TREE_TYPE (e));
!       return c_sizeof (char_type_node);
      }
! 
!   if (e == error_mark_node)
!     return e;
! 
!   return cxx_sizeof (TREE_TYPE (e));
  }
    
  
--- 1213,1254 ----
  }
  
  tree
! cxx_sizeof_or_alignof_expr (tree e, enum tree_code op)
  {
+   const char *op_name = operator_name_info[(int) op].name;
+   
+   if (e == error_mark_node)
+     return error_mark_node;
+   
    if (processing_template_decl)
!     {
!       e = build_min (op, size_type_node, e);
!       TREE_SIDE_EFFECTS (e) = 0;
!       TREE_READONLY (e) = 1;
!       
!       return e;
!     }
!   
    if (TREE_CODE (e) == COMPONENT_REF
        && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
      {
!       error ("invalid application of `%s' to a bit-field", op_name);
!       e = char_type_node;
!     }
!   else if (is_overloaded_fn (e))
!     {
!       pedwarn ("ISO C++ forbids applying `%s' to an expression of function type", op_name);
!       e = char_type_node;
      }
    else if (type_unknown_p (e))
      {
        cxx_incomplete_type_error (e, TREE_TYPE (e));
!       e = char_type_node;
      }
!   else
!     e = TREE_TYPE (e);
!   
!   return cxx_sizeof_or_alignof_type (e, op, true);
  }
    
  

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]