arg2 = evaluate_subexp (exp, pos, noside);
if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
return arg1;
- return value_assign (arg1, arg2);
+ if (binop_must_be_user_defined (arg1, arg2))
+ return value_x_binop (arg1, arg2, op, 0);
+ else
+ return value_assign (arg1, arg2);
case BINOP_ASSIGN_MODIFY:
(*pos) += 2;
if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
return arg1;
op = exp->elts[pc + 1].opcode;
- if (op == BINOP_ADD)
+ if (binop_must_be_user_defined (arg1, arg2))
+ return value_x_binop (arg1, arg2, BINOP_ASSIGN_MODIFY, op);
+ else if (op == BINOP_ADD)
arg2 = value_add (arg1, arg2);
else if (op == BINOP_SUB)
arg2 = value_sub (arg1, arg2);
arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
if (noside == EVAL_SKIP)
goto nosideret;
- return value_add (arg1, arg2);
+ if (binop_must_be_user_defined (arg1, arg2))
+ return value_x_binop (arg1, arg2, op, 0);
+ else
+ return value_add (arg1, arg2);
case BINOP_SUB:
arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
if (noside == EVAL_SKIP)
goto nosideret;
- return value_sub (arg1, arg2);
+ if (binop_must_be_user_defined (arg1, arg2))
+ return value_x_binop (arg1, arg2, op, 0);
+ else
+ return value_sub (arg1, arg2);
case BINOP_MUL:
case BINOP_DIV:
arg2 = evaluate_subexp (exp, pos, noside);
if (noside == EVAL_SKIP)
goto nosideret;
- return value_binop (arg1, arg2, op);
+ if (binop_must_be_user_defined (arg1, arg2))
+ return value_x_binop (arg1, arg2, op, 0);
+ else
+ return value_binop (arg1, arg2, op);
case BINOP_SUBSCRIPT:
arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
if (noside == EVAL_SKIP)
goto nosideret;
- return value_subscript (arg1, arg2, op);
+ if (binop_must_be_user_defined (arg1, arg2))
+ return value_x_binop (arg1, arg2, op, 0);
+ else
+ return value_subscript (arg1, arg2, op);
case BINOP_AND:
arg1 = evaluate_subexp (exp, pos, noside);
- tem = value_zerop (arg1);
- arg2 = evaluate_subexp (exp, pos,
- (tem ? EVAL_SKIP : noside));
- return value_from_long (builtin_type_int,
- !tem && !value_zerop (arg2));
+ if (binop_must_be_user_defined (arg1, arg2))
+ {
+ arg2 = evaluate_subexp (exp, pos, noside);
+ return value_x_binop (arg1, arg2, op, 0);
+ }
+ else
+ {
+ tem = value_zerop (arg1);
+ arg2 = evaluate_subexp (exp, pos,
+ (tem ? EVAL_SKIP : noside));
+ return value_from_long (builtin_type_int,
+ !tem && !value_zerop (arg2));
+ }
case BINOP_OR:
arg1 = evaluate_subexp (exp, pos, noside);
- tem = value_zerop (arg1);
- arg2 = evaluate_subexp (exp, pos,
- (!tem ? EVAL_SKIP : noside));
- return value_from_long (builtin_type_int,
- !tem || !value_zerop (arg2));
+ if (binop_must_be_user_defined (arg1, arg2))
+ {
+ arg2 = evaluate_subexp (exp, pos, noside);
+ return value_x_binop (arg1, arg2, op, 0);
+ }
+ else
+ {
+ tem = value_zerop (arg1);
+ arg2 = evaluate_subexp (exp, pos,
+ (!tem ? EVAL_SKIP : noside));
+ return value_from_long (builtin_type_int,
+ !tem || !value_zerop (arg2));
+ }
case BINOP_EQUAL:
arg1 = evaluate_subexp (exp, pos, noside);
arg2 = evaluate_subexp (exp, pos, noside);
if (noside == EVAL_SKIP)
goto nosideret;
- tem = value_equal (arg1, arg2);
- return value_from_long (builtin_type_int, tem);
+ if (binop_must_be_user_defined (arg1, arg2))
+ {
+ return value_x_binop (arg1, arg2, op, 0);
+ }
+ else
+ {
+ tem = value_equal (arg1, arg2);
+ return value_from_long (builtin_type_int, tem);
+ }
case BINOP_NOTEQUAL:
arg1 = evaluate_subexp (exp, pos, noside);
arg2 = evaluate_subexp (exp, pos, noside);
if (noside == EVAL_SKIP)
goto nosideret;
- tem = value_equal (arg1, arg2);
- return value_from_long (builtin_type_int, ! tem);
+ if (binop_must_be_user_defined (arg1, arg2))
+ {
+ return value_x_binop (arg1, arg2, op, 0);
+ }
+ else
+ {
+ tem = value_equal (arg1, arg2);
+ return value_from_long (builtin_type_int, ! tem);
+ }
case BINOP_LESS:
arg1 = evaluate_subexp (exp, pos, noside);
arg2 = evaluate_subexp (exp, pos, noside);
if (noside == EVAL_SKIP)
goto nosideret;
- tem = value_less (arg1, arg2);
- return value_from_long (builtin_type_int, tem);
+ if (binop_must_be_user_defined (arg1, arg2))
+ {
+ return value_x_binop (arg1, arg2, op, 0);
+ }
+ else
+ {
+ tem = value_less (arg1, arg2);
+ return value_from_long (builtin_type_int, tem);
+ }
case BINOP_GTR:
arg1 = evaluate_subexp (exp, pos, noside);
arg2 = evaluate_subexp (exp, pos, noside);
if (noside == EVAL_SKIP)
goto nosideret;
- tem = value_less (arg2, arg1);
- return value_from_long (builtin_type_int, tem);
+ if (binop_must_be_user_defined (arg1, arg2))
+ {
+ return value_x_binop (arg1, arg2, op, 0);
+ }
+ else
+ {
+ tem = value_less (arg2, arg1);
+ return value_from_long (builtin_type_int, tem);
+ }
case BINOP_GEQ:
arg1 = evaluate_subexp (exp, pos, noside);
arg2 = evaluate_subexp (exp, pos, noside);
if (noside == EVAL_SKIP)
goto nosideret;
- tem = value_less (arg1, arg2);
- return value_from_long (builtin_type_int, ! tem);
+ if (binop_must_be_user_defined (arg1, arg2))
+ {
+ return value_x_binop (arg1, arg2, op, 0);
+ }
+ else
+ {
+ tem = value_less (arg1, arg2);
+ return value_from_long (builtin_type_int, ! tem);
+ }
case BINOP_LEQ:
arg1 = evaluate_subexp (exp, pos, noside);
arg2 = evaluate_subexp (exp, pos, noside);
if (noside == EVAL_SKIP)
goto nosideret;
- tem = value_less (arg2, arg1);
- return value_from_long (builtin_type_int, ! tem);
+ if (binop_must_be_user_defined (arg1, arg2))
+ {
+ return value_x_binop (arg1, arg2, op, 0);
+ }
+ else
+ {
+ tem = value_less (arg2, arg1);
+ return value_from_long (builtin_type_int, ! tem);
+ }
case BINOP_REPEAT:
arg1 = evaluate_subexp (exp, pos, noside);
arg1 = evaluate_subexp (exp, pos, noside);
if (noside == EVAL_SKIP)
goto nosideret;
- return value_neg (arg1);
+ if (unop_must_be_user_defined (arg1))
+ return value_x_unop (arg1, op, 0);
+ else
+ return value_neg (arg1);
case UNOP_LOGNOT:
arg1 = evaluate_subexp (exp, pos, noside);
if (noside == EVAL_SKIP)
goto nosideret;
- return value_lognot (arg1);
+ if (unop_must_be_user_defined (arg1))
+ return value_x_unop (arg1, op, 0);
+ else
+ return value_lognot (arg1);
case UNOP_ZEROP:
arg1 = evaluate_subexp (exp, pos, noside);
if (noside == EVAL_SKIP)
goto nosideret;
- return value_from_long (builtin_type_int, value_zerop (arg1));
+ if (unop_must_be_user_defined (arg1))
+ return value_x_unop (arg1, op, 0);
+ else
+ return value_from_long (builtin_type_int, value_zerop (arg1));
case UNOP_IND:
arg1 = evaluate_subexp (exp, pos, noside);
case UNOP_PREINCREMENT:
arg1 = evaluate_subexp (exp, pos, noside);
- arg2 = value_add (arg1, value_from_long (builtin_type_char, 1));
if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
return arg1;
- return value_assign (arg1, arg2);
+ else if (unop_must_be_user_defined (arg1))
+ {
+ return value_x_unop (arg1, op, 0);
+ }
+ else
+ {
+ arg2 = value_add (arg1, value_from_long (builtin_type_char, 1));
+ return value_assign (arg1, arg2);
+ }
case UNOP_PREDECREMENT:
arg1 = evaluate_subexp (exp, pos, noside);
- arg2 = value_sub (arg1, value_from_long (builtin_type_char, 1));
if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
return arg1;
- return value_assign (arg1, arg2);
+ else if (unop_must_be_user_defined (arg1))
+ {
+ return value_x_unop (arg1, op, 0);
+ }
+ else
+ {
+ arg2 = value_sub (arg1, value_from_long (builtin_type_char, 1));
+ return value_assign (arg1, arg2);
+ }
case UNOP_POSTINCREMENT:
arg1 = evaluate_subexp (exp, pos, noside);
- arg2 = value_add (arg1, value_from_long (builtin_type_char, 1));
if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
return arg1;
- value_assign (arg1, arg2);
- return arg1;
+ else if (unop_must_be_user_defined (arg1))
+ {
+ return value_x_unop (arg1, op, 0);
+ }
+ else
+ {
+ arg2 = value_add (arg1, value_from_long (builtin_type_char, 1));
+ value_assign (arg1, arg2);
+ return arg1;
+ }
case UNOP_POSTDECREMENT:
arg1 = evaluate_subexp (exp, pos, noside);
- arg2 = value_sub (arg1, value_from_long (builtin_type_char, 1));
if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
return arg1;
- value_assign (arg1, arg2);
- return arg1;
-
+ else if (unop_must_be_user_defined (arg1))
+ {
+ return value_x_unop (arg1, op, 0);
+ }
+ else
+ {
+ arg2 = value_sub (arg1, value_from_long (builtin_type_char, 1));
+ value_assign (arg1, arg2);
+ return arg1;
+ }
+
case OP_THIS:
(*pos) += 1;
return value_of_this (1);
START_FILE
\f
-value value_x_binop ();
-
value
value_add (arg1, arg2)
value arg1, arg2;
return val;
}
- return value_x_binop (arg1, arg2, BINOP_ADD);
+ return value_binop (arg1, arg2, BINOP_ADD);
}
value
return val;
}
- return value_x_binop (arg1, arg2, BINOP_SUB);
+ return value_binop (arg1, arg2, BINOP_SUB);
}
/* Return the value of ARRAY[IDX]. */
return value_ind (value_add (array, idx));
}
\f
-/* Check to see if either argument is a structure. If so, then
- create an argument vector that calls arg1.operator @ (arg1,arg2)
- and return that value (where '@' is any binary operator which
- is legal for GNU C++). If both args are scalar types then just
- return value_binop(). */
+/* Check to see if either argument is a structure. This is called so
+ we know whether to go ahead with the normal binop or look for a
+ user defined function instead */
+
+int
+binop_must_be_user_defined (arg1, arg2)
+ value arg1, arg2;
+{
+ return (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_STRUCT
+ || TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_STRUCT
+ || (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_REF
+ && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1))) == TYPE_CODE_STRUCT)
+ || (TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_REF
+ && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2))) == TYPE_CODE_STRUCT));
+}
+
+/* Check to see if argument is a structure. This is called so
+ we know whether to go ahead with the normal unop or look for a
+ user defined function instead */
+
+int unop_must_be_user_defined (arg1)
+ value arg1;
+{
+ return (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_STRUCT
+ || (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_REF
+ && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1))) == TYPE_CODE_STRUCT));
+}
+
+/* We know either arg1 or arg2 is a structure, so try to find the right
+ user defined function. Create an argument vector that calls
+ arg1.operator @ (arg1,arg2) and return that value (where '@' is any
+ binary operator which is legal for GNU C++). */
value
-value_x_binop (arg1, arg2, op)
+value_x_binop (arg1, arg2, op, otherop)
value arg1, arg2;
- int op;
+ int op, otherop;
{
value * argvec;
char *ptr;
COERCE_ENUM (arg1);
COERCE_ENUM (arg2);
- if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_STRUCT
- || TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_STRUCT)
- {
- /* now we know that what we have to do is construct our
- arg vector and find the right function to call it with. */
+ /* now we know that what we have to do is construct our
+ arg vector and find the right function to call it with. */
- if (TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_STRUCT)
- error ("friend functions not implemented yet");
+ if (TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_STRUCT)
+ error ("friend functions not implemented yet");
- argvec = (value *) alloca (sizeof (value) * 4);
- argvec[1] = value_addr (arg1);
- argvec[2] = arg2;
- argvec[3] = 0;
+ argvec = (value *) alloca (sizeof (value) * 4);
+ argvec[1] = value_addr (arg1);
+ argvec[2] = arg2;
+ argvec[3] = 0;
- /* make the right function name up */
- strcpy(tstr,"operator __");
- ptr = tstr+9;
- switch (op)
+ /* make the right function name up */
+ strcpy(tstr, "operator __");
+ ptr = tstr+9;
+ switch (op)
+ {
+ case BINOP_ADD: strcpy(ptr,"+"); break;
+ case BINOP_SUB: strcpy(ptr,"-"); break;
+ case BINOP_MUL: strcpy(ptr,"*"); break;
+ case BINOP_DIV: strcpy(ptr,"/"); break;
+ case BINOP_REM: strcpy(ptr,"%"); break;
+ case BINOP_LSH: strcpy(ptr,"<<"); break;
+ case BINOP_RSH: strcpy(ptr,">>"); break;
+ case BINOP_LOGAND: strcpy(ptr,"&"); break;
+ case BINOP_LOGIOR: strcpy(ptr,"|"); break;
+ case BINOP_LOGXOR: strcpy(ptr,"^"); break;
+ case BINOP_AND: strcpy(ptr,"&&"); break;
+ case BINOP_OR: strcpy(ptr,"||"); break;
+ case BINOP_MIN: strcpy(ptr,"<?"); break;
+ case BINOP_MAX: strcpy(ptr,">?"); break;
+ case BINOP_ASSIGN: strcpy(ptr,"="); break;
+ case BINOP_ASSIGN_MODIFY:
+ switch (otherop)
{
- case BINOP_ADD: *ptr++ = '+'; *ptr = '\0'; break;
- case BINOP_SUB: *ptr++ = '-'; *ptr = '\0'; break;
- case BINOP_MUL: *ptr++ = '*'; *ptr = '\0'; break;
- case BINOP_DIV: *ptr++ = '/'; *ptr = '\0'; break;
- case BINOP_REM: *ptr++ = '%'; *ptr = '\0';break;
- case BINOP_LSH: *ptr++ = '<'; *ptr = '<'; break;
- case BINOP_RSH: *ptr++ = '>'; *ptr = '>'; break;
- case BINOP_LOGAND: *ptr++ = '&'; *ptr = '\0'; break;
- case BINOP_LOGIOR: *ptr++ = '|'; *ptr = '\0'; break;
- case BINOP_LOGXOR: *ptr++ = '^'; *ptr = '\0'; break;
- case BINOP_AND: *ptr++ = '&'; *ptr = '&'; break;
- case BINOP_OR: *ptr++ = '|'; *ptr = '|'; break;
- case BINOP_MIN: *ptr++ = '<'; *ptr = '?'; break;
- case BINOP_MAX: *ptr++ = '>'; *ptr = '?'; break;
+ case BINOP_ADD: strcpy(ptr,"+="); break;
+ case BINOP_SUB: strcpy(ptr,"-="); break;
+ case BINOP_MUL: strcpy(ptr,"*="); break;
+ case BINOP_DIV: strcpy(ptr,"/="); break;
+ case BINOP_REM: strcpy(ptr,"%="); break;
+ case BINOP_LOGAND: strcpy(ptr,"&="); break;
+ case BINOP_LOGIOR: strcpy(ptr,"|="); break;
+ case BINOP_LOGXOR: strcpy(ptr,"^="); break;
default:
error ("Invalid binary operation specified.");
}
- argvec[0] = value_struct_elt (arg1, argvec+1, tstr, "structure");
- if (argvec[0])
- return call_function (argvec[0], 2, argvec + 1);
- else error ("member function %s not found", tstr);
+ break;
+ case BINOP_SUBSCRIPT: strcpy(ptr,"[]"); break;
+ case BINOP_EQUAL: strcpy(ptr,"=="); break;
+ case BINOP_NOTEQUAL: strcpy(ptr,"!="); break;
+ case BINOP_LESS: strcpy(ptr,"<"); break;
+ case BINOP_GTR: strcpy(ptr,">"); break;
+ case BINOP_GEQ: strcpy(ptr,">="); break;
+ case BINOP_LEQ: strcpy(ptr,"<="); break;
+ default:
+ error ("Invalid binary operation specified.");
}
+ argvec[0] = value_struct_elt (arg1, argvec+1, tstr, "structure");
+ if (argvec[0])
+ return call_function (argvec[0], 2, argvec + 1);
+ else error ("member function %s not found", tstr);
+}
- return value_binop(arg1, arg2, op);
+/* We know that arg1 is a structure, so try to find a unary user
+ defined operator that matches the operator in question.
+ Create an argument vector that calls arg1.operator @ (arg1)
+ and return that value (where '@' is (almost) any unary operator which
+ is legal for GNU C++). */
+
+value
+value_x_unop (arg1, op)
+ value arg1;
+ int op;
+{
+ value * argvec;
+ char *ptr;
+ char tstr[13];
+
+ COERCE_ENUM (arg1);
+
+ /* now we know that what we have to do is construct our
+ arg vector and find the right function to call it with. */
+
+ if (TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_STRUCT)
+ error ("friend functions not implemented yet");
+
+ argvec = (value *) alloca (sizeof (value) * 3);
+ argvec[1] = value_addr (arg1);
+ argvec[2] = 0;
+
+ /* make the right function name up */
+ strcpy(tstr,"operator __");
+ ptr = tstr+9;
+ switch (op)
+ {
+ case UNOP_PREINCREMENT: strcpy(ptr,"++"); break;
+ case UNOP_PREDECREMENT: strcpy(ptr,"++"); break;
+ case UNOP_POSTINCREMENT: strcpy(ptr,"++"); break;
+ case UNOP_POSTDECREMENT: strcpy(ptr,"++"); break;
+ case UNOP_ZEROP: strcpy(ptr,"!"); break;
+ case UNOP_LOGNOT: strcpy(ptr,"~"); break;
+ case UNOP_NEG: strcpy(ptr,"-"); break;
+ default:
+ error ("Invalid binary operation specified.");
+ }
+ argvec[0] = value_struct_elt (arg1, argvec+1, tstr, "structure");
+ if (argvec[0])
+ return call_function (argvec[0], 1, argvec + 1);
+ else error ("member function %s not found", tstr);
}
\f
/* Perform a binary operation on two integers or two floats.