Skip to content

Commit 6bead0e

Browse files
committed
Use operator names for operator methods
The methods used to implement operators now simply use the name of the operator itself, except for unary -, which is called min to not clash with binary -. Index is called []. Closes #1520
1 parent 3aed4b0 commit 6bead0e

File tree

4 files changed

+40
-35
lines changed

4 files changed

+40
-35
lines changed

src/comp/middle/trans.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -2219,8 +2219,8 @@ fn trans_assign_op(bcx: @block_ctxt, ex: @ast::expr, op: ast::binop,
22192219
let fty = ty::node_id_to_monotype(bcx_tcx(bcx), callee_id);
22202220
ret trans_call_inner(bcx, fty, {|bcx|
22212221
// FIXME provide the already-computed address, not the expr
2222-
trans_impl::trans_method_callee(bcx, callee_id, src, origin)
2223-
}, [dst], ex.id, save_in(lhs_res.val));
2222+
trans_impl::trans_method_callee(bcx, callee_id, dst, origin)
2223+
}, [src], ex.id, save_in(lhs_res.val));
22242224
}
22252225
_ {}
22262226
}
@@ -2333,32 +2333,32 @@ fn trans_lazy_binop(bcx: @block_ctxt, op: ast::binop, a: @ast::expr,
23332333

23342334

23352335

2336-
fn trans_binary(bcx: @block_ctxt, op: ast::binop, a: @ast::expr,
2337-
b: @ast::expr, dest: dest, ex: @ast::expr) -> @block_ctxt {
2336+
fn trans_binary(bcx: @block_ctxt, op: ast::binop, lhs: @ast::expr,
2337+
rhs: @ast::expr, dest: dest, ex: @ast::expr) -> @block_ctxt {
23382338
// User-defined operators
23392339
alt bcx_ccx(bcx).method_map.find(ex.id) {
23402340
some(origin) {
23412341
let callee_id = ast_util::op_expr_callee_id(ex);
23422342
let fty = ty::node_id_to_monotype(bcx_tcx(bcx), callee_id);
23432343
ret trans_call_inner(bcx, fty, {|bcx|
2344-
trans_impl::trans_method_callee(bcx, callee_id, a, origin)
2345-
}, [b], ex.id, dest);
2344+
trans_impl::trans_method_callee(bcx, callee_id, lhs, origin)
2345+
}, [rhs], ex.id, dest);
23462346
}
23472347
_ {}
23482348
}
23492349

23502350
// First couple cases are lazy:
23512351
alt op {
23522352
ast::and | ast::or {
2353-
ret trans_lazy_binop(bcx, op, a, b, dest);
2353+
ret trans_lazy_binop(bcx, op, lhs, rhs, dest);
23542354
}
23552355
_ {
23562356
// Remaining cases are eager:
2357-
let lhs = trans_temp_expr(bcx, a);
2358-
let rhs = trans_temp_expr(lhs.bcx, b);
2359-
ret trans_eager_binop(rhs.bcx, op, lhs.val,
2360-
ty::expr_ty(bcx_tcx(bcx), a), rhs.val,
2361-
ty::expr_ty(bcx_tcx(bcx), b), dest);
2357+
let lhs_res = trans_temp_expr(bcx, lhs);
2358+
let rhs_res = trans_temp_expr(lhs_res.bcx, rhs);
2359+
ret trans_eager_binop(rhs_res.bcx, op, lhs_res.val,
2360+
ty::expr_ty(bcx_tcx(bcx), lhs), rhs_res.val,
2361+
ty::expr_ty(bcx_tcx(bcx), rhs), dest);
23622362
}
23632363
}
23642364
}
@@ -3517,8 +3517,8 @@ fn trans_expr(bcx: @block_ctxt, e: @ast::expr, dest: dest) -> @block_ctxt {
35173517
ast::expr_tup(args) { ret trans_tup(bcx, args, e.id, dest); }
35183518
ast::expr_lit(lit) { ret trans_lit(bcx, *lit, dest); }
35193519
ast::expr_vec(args, _) { ret tvec::trans_vec(bcx, args, e.id, dest); }
3520-
ast::expr_binary(op, x, y) {
3521-
ret trans_binary(bcx, op, x, y, dest, e);
3520+
ast::expr_binary(op, lhs, rhs) {
3521+
ret trans_binary(bcx, op, lhs, rhs, dest, e);
35223522
}
35233523
ast::expr_unary(op, x) {
35243524
assert op != ast::deref; // lvals are handled above

src/comp/middle/typeck.rs

+6-14
Original file line numberDiff line numberDiff line change
@@ -1768,17 +1768,9 @@ fn check_expr_with_unifier(fcx: @fn_ctxt, expr: @ast::expr, unify: unifier,
17681768

17691769
fn binop_method(op: ast::binop) -> option::t<str> {
17701770
alt op {
1771-
ast::add { some("op_add") }
1772-
ast::subtract { some("op_sub") }
1773-
ast::mul { some("op_mul") }
1774-
ast::div { some("op_div") }
1775-
ast::rem { some("op_rem") }
1776-
ast::bitxor { some("op_xor") }
1777-
ast::bitand { some("op_and") }
1778-
ast::bitor { some("op_or") }
1779-
ast::lsl { some("op_shift_left") }
1780-
ast::lsr { some("op_shift_right") }
1781-
ast::asr { some("op_ashift_right") }
1771+
ast::add | ast::subtract | ast::mul | ast::div | ast::rem |
1772+
ast::bitxor | ast::bitand | ast::bitor | ast::lsl | ast::lsr |
1773+
ast::asr { some(ast_util::binop_to_str(op)) }
17821774
_ { none }
17831775
}
17841776
}
@@ -1904,14 +1896,14 @@ fn check_expr_with_unifier(fcx: @fn_ctxt, expr: @ast::expr, unify: unifier,
19041896
oper_t = structurally_resolved_type(fcx, oper.span, oper_t);
19051897
if !(ty::type_is_integral(tcx, oper_t) ||
19061898
ty::struct(tcx, oper_t) == ty::ty_bool) {
1907-
oper_t = check_user_unop(fcx, "!", "op_not", expr, oper_t);
1899+
oper_t = check_user_unop(fcx, "!", "!", expr, oper_t);
19081900
}
19091901
}
19101902
ast::neg {
19111903
oper_t = structurally_resolved_type(fcx, oper.span, oper_t);
19121904
if !(ty::type_is_integral(tcx, oper_t) ||
19131905
ty::type_is_fp(tcx, oper_t)) {
1914-
oper_t = check_user_unop(fcx, "-", "op_neg", expr, oper_t);
1906+
oper_t = check_user_unop(fcx, "-", "neg", expr, oper_t);
19151907
}
19161908
}
19171909
}
@@ -2337,7 +2329,7 @@ fn check_expr_with_unifier(fcx: @fn_ctxt, expr: @ast::expr, unify: unifier,
23372329
_ {
23382330
let resolved = structurally_resolved_type(fcx, expr.span,
23392331
raw_base_t);
2340-
alt lookup_op_method(fcx, expr, resolved, "op_index",
2332+
alt lookup_op_method(fcx, expr, resolved, "[]",
23412333
[some(idx)]) {
23422334
some(ret_ty) { write::ty_only_fixup(fcx, id, ret_ty); }
23432335
_ {

src/comp/syntax/parse/parser.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ fn parse_ty_methods(p: parser) -> [ast::ty_method] {
284284
parse_seq(token::LBRACE, token::RBRACE, seq_sep_none(), {|p|
285285
let flo = p.span.lo;
286286
expect_word(p, "fn");
287-
let ident = parse_value_ident(p);
287+
let ident = parse_method_name(p);
288288
let tps = parse_ty_params(p);
289289
let f = parse_ty_fn(ast::proto_bare, p), fhi = p.last_span.hi;
290290
expect(p, token::SEMI);
@@ -1824,10 +1824,19 @@ fn parse_item_fn(p: parser, purity: ast::purity,
18241824
ast::item_fn(decl, t.tps, body), attrs);
18251825
}
18261826

1827+
fn parse_method_name(p: parser) -> ast::ident {
1828+
alt p.token {
1829+
token::BINOP(op) { p.bump(); token::binop_to_str(op) }
1830+
token::NOT { p.bump(); "!" }
1831+
token::LBRACKET { p.bump(); expect(p, token::RBRACKET); "[]" }
1832+
_ { parse_value_ident(p) }
1833+
}
1834+
}
1835+
18271836
fn parse_method(p: parser) -> @ast::method {
18281837
let lo = p.span.lo;
18291838
expect_word(p, "fn");
1830-
let ident = parse_value_ident(p);
1839+
let ident = parse_method_name(p);
18311840
let tps = parse_ty_params(p);
18321841
let decl = parse_fn_decl(p, ast::impure_fn);
18331842
let body = parse_block(p);

src/test/run-pass/operator-overloading.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
type point = {x: int, y: int};
22

3-
impl add_point for point {
4-
fn op_add(other: point) -> point {
3+
impl point_ops for point {
4+
fn +(other: point) -> point {
55
{x: self.x + other.x, y: self.y + other.y}
66
}
7-
fn op_neg() -> point {
7+
fn -(other: point) -> point {
8+
{x: self.x - other.x, y: self.y - other.y}
9+
}
10+
fn neg() -> point {
811
{x: -self.x, y: -self.y}
912
}
10-
fn op_index(x: bool) -> int {
13+
fn [](x: bool) -> int {
1114
x ? self.x : self.y
1215
}
1316
}
1417

1518
fn main() {
1619
let p = {x: 10, y: 20};
17-
p += {x: 1, y: 2};
20+
p += {x: 101, y: 102};
21+
p -= {x: 100, y: 100};
1822
assert p + {x: 5, y: 5} == {x: 16, y: 27};
1923
assert -p == {x: -11, y: -22};
2024
assert p[true] == 11;

0 commit comments

Comments
 (0)