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]

Re: [PATCH] New function parser_build_unary_op


On Mon, 9 May 2005, Joseph S. Myers wrote:
> On Sun, 8 May 2005, Roger Sayle wrote:
> > The following patch has been tested on i686-pc-linux-gnu with a full
> > "make bootstrap", all default languages, and regression tested with a
> > top-level "make -k check" with no new failures.
>
> This does not appear to be the version of the patch that passed bootstrap;
> there are too many typos, noted below.

Argh!  Excellent catch; I forgot to rerun "cvs diff" on the tree that
passed bootstrap, after correcting the typos.  My apologies.  Thanks for
spotting this, or I'd have broken mainline by committing a different patch
to the one that was successfully tested.



> > + /* This is the entry point used by the parser to build unary operators
> > +    in the input.  */
>
> Comment should define the interface more precisely, i.e. state what the
> parameters mean (including the use of CONVERT_EXPR for unary plus).


I also took the opportunity of improving the documentation of the existing
parser_build_binary_op, whose terse description I used as basis for the
newly added function.

Sorry again for my procedural mistake, that's usually unlike me.  The
following diff is the tested patch, with your requested changes to the
commentary.



2005-05-09  Roger Sayle  <roger@eyesopen.com>

	* c-tree.h (parser_build_unary_op): New prototype.
	* c-typeck.c (parser_build_unary_op): New function to construct
	a unary operation in the C parser.
	* c-parser.c (c_parser_unary_expression):  Use the new function
	parser_build_unary_op when appropriate.


Index: c-typeck.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-typeck.c,v
retrieving revision 1.439
diff -c -3 -p -r1.439 c-typeck.c
*** c-typeck.c	2 May 2005 16:02:19 -0000	1.439
--- c-typeck.c	9 May 2005 14:35:07 -0000
*************** convert_arguments (tree typelist, tree v
*** 2312,2322 ****
    return nreverse (result);
  }

! /* This is the entry point used by the parser
!    for binary operators in the input.
!    In addition to constructing the expression,
!    we check for operands that were written with other binary operators
!    in a way that is likely to confuse the user.  */

  struct c_expr
  parser_build_binary_op (enum tree_code code, struct c_expr arg1,
--- 2312,2338 ----
    return nreverse (result);
  }

! /* This is the entry point used by the parser to build unary operators
!    in the input.  CODE, a tree_code, specifies the unary operator, and
!    ARG is the operand.  For unary plus, the C parser currently uses
!    CONVERT_EXPR for code.  */
!
! struct c_expr
! parser_build_unary_op (enum tree_code code, struct c_expr arg)
! {
!   struct c_expr result;
!
!   result.original_code = ERROR_MARK;
!   result.value = build_unary_op (code, arg.value, 0);
!   overflow_warning (result.value);
!   return result;
! }
!
! /* This is the entry point used by the parser to build binary operators
!    in the input.  CODE, a tree_code, specifies the binary operator, and
!    ARG1 and ARG2 are the operands.  In addition to constructing the
!    expression, we check for operands that were written with other binary
!    operators in a way that is likely to confuse the user.  */

  struct c_expr
  parser_build_binary_op (enum tree_code code, struct c_expr arg1,
Index: c-tree.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-tree.h,v
retrieving revision 1.202
diff -c -3 -p -r1.202 c-tree.h
*** c-tree.h	2 May 2005 19:38:52 -0000	1.202
--- c-tree.h	9 May 2005 14:35:07 -0000
*************** extern tree build_external_ref (tree, in
*** 531,536 ****
--- 531,537 ----
  extern void pop_maybe_used (bool);
  extern struct c_expr c_expr_sizeof_expr (struct c_expr);
  extern struct c_expr c_expr_sizeof_type (struct c_type_name *);
+ extern struct c_expr parser_build_unary_op (enum tree_code, struct c_expr);
  extern struct c_expr parser_build_binary_op (enum tree_code, struct c_expr,
  					     struct c_expr);
  extern tree build_conditional_expr (tree, tree, tree);
Index: c-parser.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-parser.c,v
retrieving revision 2.12
diff -c -3 -p -r2.12 c-parser.c
*** c-parser.c	23 Apr 2005 21:27:34 -0000	2.12
--- c-parser.c	9 May 2005 14:35:08 -0000
*************** c_parser_unary_expression (c_parser *par
*** 4495,4560 ****
  {
    int ext;
    struct c_expr ret;
-   ret.original_code = ERROR_MARK;
    switch (c_parser_peek_token (parser)->type)
      {
      case CPP_PLUS_PLUS:
        c_parser_consume_token (parser);
!       ret.value
! 	= build_unary_op (PREINCREMENT_EXPR,
! 			  c_parser_cast_expression (parser, NULL).value, 0);
!       overflow_warning (ret.value);
!       return ret;
      case CPP_MINUS_MINUS:
        c_parser_consume_token (parser);
!       ret.value
! 	= build_unary_op (PREDECREMENT_EXPR,
! 			  c_parser_cast_expression (parser, NULL).value, 0);
!       overflow_warning (ret.value);
!       return ret;
      case CPP_AND:
        c_parser_consume_token (parser);
!       ret.value
! 	= build_unary_op (ADDR_EXPR,
! 			  c_parser_cast_expression (parser, NULL).value, 0);
!       overflow_warning (ret.value);
!       return ret;
      case CPP_MULT:
        c_parser_consume_token (parser);
        ret.value
  	= build_indirect_ref (c_parser_cast_expression (parser, NULL).value,
  			      "unary *");
        return ret;
      case CPP_PLUS:
        c_parser_consume_token (parser);
        if (!c_dialect_objc () && warn_traditional && !in_system_header)
  	warning (0, "traditional C rejects the unary plus operator");
!       ret.value
! 	= build_unary_op (CONVERT_EXPR,
! 			  c_parser_cast_expression (parser, NULL).value, 0);
!       overflow_warning (ret.value);
!       return ret;
      case CPP_MINUS:
        c_parser_consume_token (parser);
!       ret.value
! 	= build_unary_op (NEGATE_EXPR,
! 			  c_parser_cast_expression (parser, NULL).value, 0);
!       overflow_warning (ret.value);
!       return ret;
      case CPP_COMPL:
        c_parser_consume_token (parser);
!       ret.value
! 	= build_unary_op (BIT_NOT_EXPR,
! 			  c_parser_cast_expression (parser, NULL).value, 0);
!       overflow_warning (ret.value);
!       return ret;
      case CPP_NOT:
        c_parser_consume_token (parser);
!       ret.value
! 	= build_unary_op (TRUTH_NOT_EXPR,
! 			  c_parser_cast_expression (parser, NULL).value, 0);
!       overflow_warning (ret.value);
!       return ret;
      case CPP_AND_AND:
        /* Refer to the address of a label as a pointer.  */
        c_parser_consume_token (parser);
--- 4495,4539 ----
  {
    int ext;
    struct c_expr ret;
    switch (c_parser_peek_token (parser)->type)
      {
      case CPP_PLUS_PLUS:
        c_parser_consume_token (parser);
!       return parser_build_unary_op (PREINCREMENT_EXPR,
! 				    c_parser_cast_expression (parser, NULL));
      case CPP_MINUS_MINUS:
        c_parser_consume_token (parser);
!       return parser_build_unary_op (PREDECREMENT_EXPR,
! 				    c_parser_cast_expression (parser, NULL));
      case CPP_AND:
        c_parser_consume_token (parser);
!       return parser_build_unary_op (ADDR_EXPR,
! 				    c_parser_cast_expression (parser, NULL));
      case CPP_MULT:
        c_parser_consume_token (parser);
        ret.value
  	= build_indirect_ref (c_parser_cast_expression (parser, NULL).value,
  			      "unary *");
+       ret.original_code = ERROR_MARK;
        return ret;
      case CPP_PLUS:
        c_parser_consume_token (parser);
        if (!c_dialect_objc () && warn_traditional && !in_system_header)
  	warning (0, "traditional C rejects the unary plus operator");
!       return parser_build_unary_op (CONVERT_EXPR,
! 				    c_parser_cast_expression (parser, NULL));
      case CPP_MINUS:
        c_parser_consume_token (parser);
!       return parser_build_unary_op (NEGATE_EXPR,
! 				    c_parser_cast_expression (parser, NULL));
      case CPP_COMPL:
        c_parser_consume_token (parser);
!       return parser_build_unary_op (BIT_NOT_EXPR,
! 				    c_parser_cast_expression (parser, NULL));
      case CPP_NOT:
        c_parser_consume_token (parser);
!       return parser_build_unary_op (TRUTH_NOT_EXPR,
! 				    c_parser_cast_expression (parser, NULL));
      case CPP_AND_AND:
        /* Refer to the address of a label as a pointer.  */
        c_parser_consume_token (parser);
*************** c_parser_unary_expression (c_parser *par
*** 4563,4576 ****
  	  ret.value = finish_label_address_expr
  	    (c_parser_peek_token (parser)->value);
  	  c_parser_consume_token (parser);
- 	  return ret;
  	}
        else
  	{
  	  c_parser_error (parser, "expected identifier");
  	  ret.value = error_mark_node;
- 	  return ret;
  	}
      case CPP_KEYWORD:
        switch (c_parser_peek_token (parser)->keyword)
  	{
--- 4542,4555 ----
  	  ret.value = finish_label_address_expr
  	    (c_parser_peek_token (parser)->value);
  	  c_parser_consume_token (parser);
  	}
        else
  	{
  	  c_parser_error (parser, "expected identifier");
  	  ret.value = error_mark_node;
  	}
+ 	ret.original_code = ERROR_MARK;
+ 	return ret;
      case CPP_KEYWORD:
        switch (c_parser_peek_token (parser)->keyword)
  	{
*************** c_parser_unary_expression (c_parser *par
*** 4586,4603 ****
  	  return ret;
  	case RID_REALPART:
  	  c_parser_consume_token (parser);
! 	  ret.value
! 	    = build_unary_op (REALPART_EXPR,
! 			      c_parser_cast_expression (parser, NULL).value,
! 			      0);
! 	  return ret;
  	case RID_IMAGPART:
  	  c_parser_consume_token (parser);
! 	  ret.value
! 	    = build_unary_op (IMAGPART_EXPR,
! 			      c_parser_cast_expression (parser, NULL).value,
! 			      0);
! 	  return ret;
  	default:
  	  return c_parser_postfix_expression (parser);
  	}
--- 4565,4578 ----
  	  return ret;
  	case RID_REALPART:
  	  c_parser_consume_token (parser);
! 	  return parser_build_unary_op (REALPART_EXPR,
! 					c_parser_cast_expression (parser,
! 								  NULL));
  	case RID_IMAGPART:
  	  c_parser_consume_token (parser);
! 	  return parser_build_unary_op (IMAGPART_EXPR,
! 					c_parser_cast_expression (parser,
! 								  NULL));
  	default:
  	  return c_parser_postfix_expression (parser);
  	}


Roger
--


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