This is the mail archive of the gdb-patches@sources.redhat.com mailing list for the GDB 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] Unary plus


Daniel Jacobowitz wrote:
Two comments:

- Could you add a non-tracepoint testcase for this?  I think one good
place would be gdb.cp/userdef.exp.
done. As this overloads operator+, I introduced a new class for
the breakpoint test.


!   if (TYPE_CODE (type) == TYPE_CODE_FLT)
!     return arg1;


- IIUC, that's not quite right - we need an rvalue here.

Like this?


built and tested on i686-pc-linux-gnu, ok?

nathan

--
Nathan Sidwell    ::   http://www.codesourcery.com   ::     CodeSourcery LLC
nathan@codesourcery.com    ::     http://www.planetfall.pwp.blueyonder.co.uk

2005-03-08  Nathan Sidwell  <nathan@codesourcery.com>

	* ax-gdb.c (gen_expr): Add UNOP_PLUS case.
	* c-exp.y (exp): Add unary plus.
	* eval.c (evaluate_subexp_standard): Add UNOP_PLUS case.
	* valarith.c (value_x_unop): Add UNOP_PLUS case.
	(value_pos): New.
	* value.h (value_pos): Declare.

	* gdb.cp/userdef.cc (A1::operator+): New unary plus.
	(A2): New class.
	(main): Test operator+.
	* gdb.cp/userdef.exp: Test unary plus.  Use A2::operator+ for
	breakpoint test.

Index: ax-gdb.c
===================================================================
RCS file: /cvs/src/src/gdb/ax-gdb.c,v
retrieving revision 1.28
diff -c -3 -p -r1.28 ax-gdb.c
*** ax-gdb.c	7 Feb 2005 23:51:02 -0000	1.28
--- ax-gdb.c	8 Mar 2005 10:40:15 -0000
*************** gen_expr (union exp_element **pc, struct
*** 1642,1647 ****
--- 1642,1654 ----
        }
        break;
  
+     case UNOP_PLUS:
+       (*pc)++;
+       /* + FOO is equivalent to 0 + FOO, which can be optimized. */
+       gen_expr (pc, ax, value);
+       gen_usual_unary (ax, value);
+       break;
+       
      case UNOP_NEG:
        (*pc)++;
        /* -FOO is equivalent to 0 - FOO.  */
Index: c-exp.y
===================================================================
RCS file: /cvs/src/src/gdb/c-exp.y,v
retrieving revision 1.28
diff -c -3 -p -r1.28 c-exp.y
*** c-exp.y	7 Aug 2004 19:45:45 -0000	1.28
--- c-exp.y	8 Mar 2005 10:40:20 -0000
*************** exp	:	'-' exp    %prec UNARY
*** 256,261 ****
--- 256,265 ----
  			{ write_exp_elt_opcode (UNOP_NEG); }
  	;
  
+ exp	:	'+' exp    %prec UNARY
+ 			{ write_exp_elt_opcode (UNOP_PLUS); }
+ 	;
+ 
  exp	:	'!' exp    %prec UNARY
  			{ write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
  	;
Index: eval.c
===================================================================
RCS file: /cvs/src/src/gdb/eval.c,v
retrieving revision 1.53
diff -c -3 -p -r1.53 eval.c
*** eval.c	11 Feb 2005 04:05:46 -0000	1.53
--- eval.c	8 Mar 2005 10:40:25 -0000
*************** evaluate_subexp_standard (struct type *e
*** 1855,1860 ****
--- 1855,1869 ----
        evaluate_subexp (NULL_TYPE, exp, pos, noside);
        return evaluate_subexp (NULL_TYPE, exp, pos, noside);
  
+     case UNOP_PLUS:
+       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
+       if (noside == EVAL_SKIP)
+ 	goto nosideret;
+       if (unop_user_defined_p (op, arg1))
+ 	return value_x_unop (arg1, op, noside);
+       else
+ 	return value_pos (arg1);
+       
      case UNOP_NEG:
        arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
        if (noside == EVAL_SKIP)
Index: valarith.c
===================================================================
RCS file: /cvs/src/src/gdb/valarith.c,v
retrieving revision 1.38
diff -c -3 -p -r1.38 valarith.c
*** valarith.c	11 Feb 2005 04:06:08 -0000	1.38
--- valarith.c	8 Mar 2005 10:40:29 -0000
*************** value_x_unop (struct value *arg1, enum e
*** 560,565 ****
--- 560,568 ----
      case UNOP_NEG:
        strcpy (ptr, "-");
        break;
+     case UNOP_PLUS:
+       strcpy (ptr, "+");
+       break;
      case UNOP_IND:
        strcpy (ptr, "*");
        break;
*************** value_less (struct value *arg1, struct v
*** 1313,1319 ****
      }
  }
  
! /* The unary operators - and ~.  Both free the argument ARG1.  */
  
  struct value *
  value_neg (struct value *arg1)
--- 1316,1350 ----
      }
  }
  
! /* The unary operators +, - and ~.  They free the argument ARG1
!    (unless it is returned).  */
! 
! struct value *
! value_pos (struct value *arg1)
! {
!   struct type *type;
! 
!   arg1 = coerce_ref (arg1);
! 
!   type = check_typedef (value_type (arg1));
! 
!   if (TYPE_CODE (type) == TYPE_CODE_FLT)
!     return value_from_double (type, value_as_double (arg1));
!   else if (is_integral_type (type))
!     {
!       /* Perform integral promotion for ANSI C/C++.  FIXME: What about
!          FORTRAN and (the deleted) chill ?  */
!       if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int))
! 	type = builtin_type_int;
! 
!       return value_from_longest (type, value_as_long (arg1));
!     }
!   else
!     {
!       error ("Argument to positive operation not a number.");
!       return 0;			/* For lint -- never reached */
!     }
! }
  
  struct value *
  value_neg (struct value *arg1)
Index: value.h
===================================================================
RCS file: /cvs/src/src/gdb/value.h,v
retrieving revision 1.84
diff -c -3 -p -r1.84 value.h
*** value.h	28 Feb 2005 17:00:49 -0000	1.84
--- value.h	8 Mar 2005 10:40:30 -0000
*************** extern struct value *value_addr (struct 
*** 326,331 ****
--- 326,333 ----
  extern struct value *value_assign (struct value *toval,
  				   struct value *fromval);
  
+ extern struct value *value_pos (struct value *arg1);
+ 
  extern struct value *value_neg (struct value *arg1);
  
  extern struct value *value_complement (struct value *arg1);
Index: testsuite/gdb.cp/userdef.cc
===================================================================
RCS file: /cvs/src/src/gdb/testsuite/gdb.cp/userdef.cc,v
retrieving revision 1.2
diff -c -3 -p -r1.2 userdef.cc
*** testsuite/gdb.cp/userdef.cc	12 Jun 2004 16:45:49 -0000	1.2
--- testsuite/gdb.cp/userdef.cc	8 Mar 2005 10:40:35 -0000
*************** A1 operator/(const A1&);
*** 63,68 ****
--- 63,69 ----
  A1 operator=(const A1&);
  
  A1 operator~();
+ A1 operator+();
  A1 operator-();
  int operator!();
  A1 operator++();
*************** A1 A1::operator-(void)
*** 225,230 ****
--- 226,240 ----
   return (neg);
  }
  
+ A1 A1::operator+(void)
+ {
+  A1 pos(0,0);
+  pos.x = +x;
+  pos.y = +y;
+ 
+  return (pos);
+ }
+ 
  A1 A1::operator~(void)
  {
   A1 acompl(0,0);
*************** ostream& operator<<(ostream& outs, A1 on
*** 286,291 ****
--- 296,312 ----
   return (outs << endl << "x = " << one.x << endl << "y = " << one.y << endl << "-------" << endl); 
  }
  
+ class A2 {
+   public:
+ A2 operator+();
+ };
+ 
+ A2 A2::operator+()
+ {
+   return A2 ();
+ }
+ 
+ 
  int main (void)
  {
   A1 one(2,3);
*************** int main (void)
*** 342,347 ****
--- 363,370 ----
   
   val = (!one);
   cout << "! " << val << endl << "-----"<<endl;
+  three = (+one);
+  cout << "+ " << three;
   three = (-one);
   cout << "- " << three;
   three = (~one);
Index: testsuite/gdb.cp/userdef.exp
===================================================================
RCS file: /cvs/src/src/gdb/testsuite/gdb.cp/userdef.exp,v
retrieving revision 1.2
diff -c -3 -p -r1.2 userdef.exp
*** testsuite/gdb.cp/userdef.exp	11 Feb 2004 14:01:25 -0000	1.2
--- testsuite/gdb.cp/userdef.exp	8 Mar 2005 10:40:35 -0000
*************** gdb_test "print one >> 31" "\\\$\[0-9\]*
*** 121,126 ****
--- 121,128 ----
  gdb_test "print !one" "\\\$\[0-9\]* = 0\[\r\n\]"
  
  # Assumes 2's complement.  So does everything...
+ gdb_test "print +one" "\\\$\[0-9\]* = {x = 2, y = 3}"
+ 
  gdb_test "print ~one" "\\\$\[0-9\]* = {x = -3, y = -4}"
  
  gdb_test "print -one" "\\\$\[0-9\]* = {x = -2, y = -3}"
*************** gdb_test "print one += 7" "\\\$\[0-9\]* 
*** 138,145 ****
  gdb_test "print two = one" "\\\$\[0-9\]* = {x = 9, y = 10}"
  
  # Check that GDB tolerates whitespace in operator names.
! gdb_test "break A1::'operator+'" ".*Breakpoint $decimal at.*"
! gdb_test "break A1::'operator +'" ".*Breakpoint $decimal at.*"
  
  gdb_exit
  return 0
--- 140,147 ----
  gdb_test "print two = one" "\\\$\[0-9\]* = {x = 9, y = 10}"
  
  # Check that GDB tolerates whitespace in operator names.
! gdb_test "break A2::'operator+'" ".*Breakpoint $decimal at.*"
! gdb_test "break A2::'operator +'" ".*Breakpoint $decimal at.*"
  
  gdb_exit
  return 0

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