Re: [rfc][08/37] Eliminate builtin_type_ macros: Make pointer arithmetic explicit

Daniel Jacobowitz
2008-09-02T12:38:21Z

On Sun, Aug 31, 2008 at 07:50:53PM +0200, Ulrich Weigand wrote:
> Hello,
>
> this patch removes the functions value_add and value_sub which handle
> both integer and pointer arithmetic, and replaces them by functions
> value_ptradd, value_ptrsub, and value_ptrdiff that solely take care
> of C-style pointer arithmetic. (For integer arithmetic, value_binop
> can be used.)
>
> This moves the type to be used for pointer difference operations up
> eval.c (this should at some point be replaces by a per-gdbarch
> "ptrdiff_t" type). It also makes the subsequent patches to remove
> current_gdbarch references from value_binop simpler.

I've got to say I don't like the direction of this change :-(

Smarts in the expression parser are not available to reuse by other
code that constructs values - which is something I think we should be
doing more often. Why should the caller have to know that the two
values are pointers?

> @@ -1531,8 +1556,19 @@ evaluate_subexp_standard (struct type *e
> goto nosideret;
> if (binop_user_defined_p (op, arg1, arg2))
> return value_x_binop (arg1, arg2, op, OP_NULL, noside);
> + else if (ptrmath_type_p (value_type (arg1))
> + && ptrmath_type_p (value_type (arg2)))
> + {
> + /* FIXME -- should be ptrdiff_t */
> + type = builtin_type (exp->gdbarch)->builtin_long;
> + return value_from_longest (type, value_ptrdiff (arg1, arg2));
> + }
> + else if (ptrmath_type_p (value_type (arg1)))
> + return value_ptrsub (arg1, arg2);
> + else if (ptrmath_type_p (value_type (arg2)))
> + return value_ptrsub (arg2, arg1);
> else
> - return value_sub (arg1, arg2);
> + return value_binop (arg1, arg2, BINOP_SUB);
>
> case BINOP_EXP:
> case BINOP_MUL:

There's something wrong in the last else if; arg1 - arg2 != arg2 - arg1.

--
Daniel Jacobowitz
CodeSourcery