Operator overloading (M0.2, part 2)

Member operator functions are ordinary methods; binary, unary, [], ()
and compound-assignment expressions on struct operands resolve to them
(op= falls back to op plus assignment) and lower as method calls on the
first operand.
This commit is contained in:
Peter Li 2026-10-31 11:30:02 -07:00
parent eeff789393
commit 392df21a12
2 changed files with 147 additions and 42 deletions

View File

@ -586,6 +586,10 @@ const Lowerer = struct {
fn rvalue(l: *Lowerer, e: *ast.Expr) Error!Ref {
const t = l.types();
switch (e.kind) {
.binary, .unary, .index, .assign => if (l.s.resolution(e)) |r| if (r == .function) return l.operatorCall(e, r.function),
else => {},
}
switch (e.kind) {
.int_lit => |text| {
const ty = try l.concrete(e.ty);
@ -888,49 +892,79 @@ const Lowerer = struct {
}
// `obj.method(...)`: the object's storage (a temporary for
// rvalues).
var object: ?Ref = null;
var write_back: ?Ref = null;
const owner_ty = if (isMethod(f)) try l.s.types.intern(.{ .@"struct" = f.owner.? }) else TypeTable.void_type;
if (isMethod(f) and callee.kind == .member) {
const base = callee.kind.member.base;
const direct = if (isAddressable(base)) try l.lvalue(base) else null;
const plain = if (direct) |p| l.b.f.inst(p).op == .local_var or l.b.f.inst(p).op == .param else false;
if (direct != null and plain and base.ty == owner_ty) {
object = direct;
} else {
// Through a function-local copy of the method's own
// struct (a base prefix of derived objects), written
// back afterwards to writable storage.
const tmp = try l.b.add(.{ .op = .local_var, .ty = owner_ty, .ptr = .function });
const whole = if (direct) |p| try l.b.load(p) else try l.rvalue(base);
try l.b.store(tmp, try l.basePrefix(whole, base.ty, owner_ty));
object = tmp;
if (direct) |p| switch (l.b.f.inst(p).ptr) {
.function, .private, .groupshared => write_back = p,
else => {},
};
}
}
const res = try l.userCall(f, fi, full, ret_ty, object);
if (write_back) |p| {
const updated = try l.b.load(object.?);
if (callee.kind.member.base.ty == owner_ty) {
try l.b.store(p, updated);
} else {
// Only the base's fields.
for (l.s.types.get(owner_ty).@"struct".fields.items, 0..) |fld, k| {
const idx = try l.b.constInt(try l.types().scalar(.uint), @intCast(k));
const fp = try l.b.add(.{ .op = .{ .access = .{ .base = p, .indices = try l.arena.dupe(Ref, &.{idx}) } }, .ty = fld.type, .ptr = l.b.f.inst(p).ptr });
try l.b.store(fp, try l.b.add(.{ .op = .{ .extract = .{ .agg = updated, .index = @intCast(k) } }, .ty = fld.type }));
}
}
}
return res;
// (A struct value called directly uses its operator().)
const base: ?*ast.Expr = if (!isMethod(f)) null else if (callee.kind == .member) callee.kind.member.base else if (l.s.types.get(callee.ty) == .@"struct") callee else null;
return l.methodCall(f, fi, base, full, ret_ty);
},
else => return l.callOther(e, r, callee, args, ret_ty),
}
}
/// A call to `f` with `base_expr` as the object of a method (null:
/// the current `this`, or no object).
fn methodCall(l: *Lowerer, f: *const semamod.Function, fi: u32, base_expr: ?*ast.Expr, args: []const *ast.Expr, ret_ty: TypeId) Error!Ref {
var object: ?Ref = null;
var write_back: ?Ref = null;
const owner_ty = if (isMethod(f)) try l.s.types.intern(.{ .@"struct" = f.owner.? }) else TypeTable.void_type;
if (base_expr) |base| {
const direct = if (isAddressable(base)) try l.lvalue(base) else null;
const plain = if (direct) |p| l.b.f.inst(p).op == .local_var or l.b.f.inst(p).op == .param else false;
if (direct != null and plain and base.ty == owner_ty) {
object = direct;
} else {
// Through a function-local copy of the method's own
// struct (a base prefix of derived objects), written
// back afterwards to writable storage.
const tmp = try l.b.add(.{ .op = .local_var, .ty = owner_ty, .ptr = .function });
const whole = if (direct) |p| try l.b.load(p) else try l.rvalue(base);
try l.b.store(tmp, try l.basePrefix(whole, base.ty, owner_ty));
object = tmp;
if (direct) |p| switch (l.b.f.inst(p).ptr) {
.function, .private, .groupshared => write_back = p,
else => {},
};
}
}
const res = try l.userCall(f, fi, args, ret_ty, object);
if (write_back) |p| {
const updated = try l.b.load(object.?);
if (base_expr.?.ty == owner_ty) {
try l.b.store(p, updated);
} else {
// Only the base's fields.
for (l.s.types.get(owner_ty).@"struct".fields.items, 0..) |fld, k| {
const idx = try l.b.constInt(try l.types().scalar(.uint), @intCast(k));
const fp = try l.b.add(.{ .op = .{ .access = .{ .base = p, .indices = try l.arena.dupe(Ref, &.{idx}) } }, .ty = fld.type, .ptr = l.b.f.inst(p).ptr });
try l.b.store(fp, try l.b.add(.{ .op = .{ .extract = .{ .agg = updated, .index = @intCast(k) } }, .ty = fld.type }));
}
}
}
return res;
}
/// An overloaded operator: a method call on the first operand.
fn operatorCall(l: *Lowerer, e: *ast.Expr, f: *const semamod.Function) Error!Ref {
const fi = try l.functionIndex(f);
const ret_ty = try l.concrete(f.ret);
switch (e.kind) {
.binary => |x| return l.methodCall(f, fi, x.lhs, &.{x.rhs}, ret_ty),
.unary => |x| return l.methodCall(f, fi, x.operand, &.{}, ret_ty),
.index => |x| return l.methodCall(f, fi, x.base, &.{x.index}, ret_ty),
.call => |x| return l.methodCall(f, fi, x.callee, x.args, ret_ty),
.assign => |x| {
const r = try l.methodCall(f, fi, x.lhs, &.{x.rhs}, ret_ty);
// `a op= b` through operator op: assign the result.
if (!std.mem.endsWith(u8, f.name, "=")) {
const v = try l.convert(r, x.lhs.ty);
try l.storeTo(x.lhs, v);
return v;
}
return r;
},
else => return l.fail(e, "this operator"),
}
}
fn userCall(l: *Lowerer, f: *const semamod.Function, fi: u32, args: []const *ast.Expr, ret_ty: TypeId, object: ?Ref) Error!Ref {
{
{

View File

@ -1882,7 +1882,7 @@ pub const Sema = struct {
fn funcDeclMode(s: *Sema, d: *const ast.Decl, f: *const ast.FuncDecl, owner: ?*types.StructInfo, mode: FuncMode) Allocator.Error!void {
const name = f.name.last();
if (mode == .body_only) {
if (f.template_params.len > 0 or std.mem.startsWith(u8, name, "operator") or f.name.parts.len > 1) return;
if (f.template_params.len > 0 or (std.mem.startsWith(u8, name, "operator") and owner == null) or f.name.parts.len > 1) return;
const sym = s.scope.syms.get(name) orelse return;
if (sym != .functions) return;
for (sym.functions.funcs.items) |func| if (func.decl == f) return s.funcBody(func, f);
@ -1892,8 +1892,9 @@ pub const Sema = struct {
o.has_operators = true;
};
if (f.template_params.len > 0) return s.declareTemplate(name, d);
if (std.mem.startsWith(u8, name, "operator")) {
s.err(d.loc, "operator overloading is not supported yet (planned for milestone 0.2)", .{});
// HLSL 2021 overloads operators as member functions only.
if (std.mem.startsWith(u8, name, "operator") and owner == null) {
s.err(d.loc, "non-member operator overloading is not supported yet", .{});
return;
}
if (f.name.parts.len > 1) return; // out-of-line method definition: not modelled yet
@ -2279,11 +2280,64 @@ pub const Sema = struct {
return err_typed;
}
/// `obj op args`: a call to the struct's `operator<op>` method
/// (recorded on `e`), or null when it declares none.
fn operatorCall(s: *Sema, e: *ast.Expr, obj: TypeId, name: []const u8, args: []const intrinsics.ArgInfo) Allocator.Error!?TypeId {
const info = switch (s.types.get(obj)) {
.@"struct" => |info| info,
else => return null,
};
const ss: *StructSema = @ptrCast(@alignCast(info.sema orelse return null));
const sym = structMember(ss, name) orelse return null;
if (sym != .functions) return null;
return s.resolveUserCall(e, sym.functions, args, e);
}
fn argInfo(s: *Sema, e: *ast.Expr, t: Typed) intrinsics.ArgInfo {
return .{ .ty = t.ty, .lvalue = t.lvalue and !t.read_only, .loc = e.loc, .groupshared = s.isGroupshared(e) };
}
fn binaryOpName(op: ast.BinaryOp) ?[]const u8 {
return switch (op) {
.mul => "operator*",
.div => "operator/",
.rem => "operator%",
.add => "operator+",
.sub => "operator-",
.shl => "operator<<",
.shr => "operator>>",
.lt => "operator<",
.gt => "operator>",
.le => "operator<=",
.ge => "operator>=",
.eq => "operator==",
.ne => "operator!=",
.bit_and => "operator&",
.bit_xor => "operator^",
.bit_or => "operator|",
.log_and => "operator&&",
.log_or => "operator||",
.comma => null,
};
}
/// A port of dxc's CheckUnaryOpForHLSL.
fn unaryExpr(s: *Sema, e: *ast.Expr, op: ast.UnaryOp, operand: *ast.Expr) Allocator.Error!Typed {
const t = try s.expr(operand);
if (t.ty == TypeTable.error_type) return err_typed;
if (s.types.get(t.ty) == .@"struct" and s.types.get(t.ty).@"struct".overloadsOperators()) return err_typed;
if (s.types.get(t.ty) == .@"struct" and s.types.get(t.ty).@"struct".overloadsOperators()) {
const name: []const u8 = switch (op) {
.plus => "operator+",
.minus => "operator-",
.not => "operator!",
.bit_not => "operator~",
.pre_inc, .post_inc => "operator++",
.pre_dec, .post_dec => "operator--",
};
if (try s.operatorCall(e, t.ty, name, &.{})) |rt| return rvalue(rt);
s.err(e.loc, "invalid argument type '{s}' to unary expression", .{s.typeName(t.ty)});
return err_typed;
}
const inc_dec = switch (op) {
.pre_inc, .pre_dec, .post_inc, .post_dec => true,
else => false,
@ -2328,6 +2382,9 @@ pub const Sema = struct {
fn binaryExpr(s: *Sema, e: *ast.Expr, op: ast.BinaryOp, lhs: *ast.Expr, rhs: *ast.Expr) Allocator.Error!Typed {
const l = try s.expr(lhs);
const r = try s.expr(rhs);
if (binaryOpName(op)) |name| if (l.ty != TypeTable.error_type and r.ty != TypeTable.error_type) {
if (try s.operatorCall(e, l.ty, name, &.{s.argInfo(rhs, r)})) |rt| return rvalue(rt);
};
if (op == .comma) return rvalue(r.ty);
if (l.ty == TypeTable.error_type or r.ty == TypeTable.error_type) return err_typed;
return rvalue(try s.binaryOp(e.loc, op, l.ty, r.ty, false, lhs.loc, rhs.loc) orelse return err_typed);
@ -2493,6 +2550,17 @@ pub const Sema = struct {
}
} else {
const r = try s.expr(rhs);
// `a op= b` on a struct: its operator op= (or operator op).
if (s.types.get(l.ty) == .@"struct" and r.ty != TypeTable.error_type) {
const bop = std.meta.stringToEnum(ast.BinaryOp, @tagName(op)).?;
const base = binaryOpName(bop).?;
const compound = try std.fmt.allocPrint(s.arena, "{s}=", .{base});
if (try s.operatorCall(e, l.ty, compound, &.{s.argInfo(rhs, r)})) |rt| return rvalue(rt);
if (try s.operatorCall(e, l.ty, base, &.{s.argInfo(rhs, r)})) |rt| {
if (s.conversionCost(rt, l.ty) == null) s.convertError(e.loc, rt, l.ty);
return .{ .ty = l.ty };
}
}
if (r.ty != TypeTable.error_type) {
const bop = std.meta.stringToEnum(ast.BinaryOp, @tagName(op)).?;
_ = try s.binaryOp(e.loc, bop, l.ty, r.ty, true, lhs.loc, rhs.loc);
@ -2691,6 +2759,7 @@ pub const Sema = struct {
// Structs declaring operator[] were already reported as
// milestone 0.2 at the declaration.
.@"struct" => |info| {
if (i.ty != TypeTable.error_type) if (try s.operatorCall(e, b.ty, "operator[]", &.{s.argInfo(index, i)})) |rt| return rvalue(rt);
if (!info.overloadsOperators()) s.err(e.loc, "type '{s}' does not provide a subscript operator", .{s.typeName(b.ty)});
return err_typed;
},
@ -2750,6 +2819,8 @@ pub const Sema = struct {
}
const target = c.callee orelse {
if (s.types.get(c.ty) == .@"struct") {
for (arg_types) |a| if (a.ty == TypeTable.error_type) return err_typed;
if (try s.operatorCall(e, c.ty, "operator()", arg_types)) |rt| return rvalue(rt);
if (!s.types.get(c.ty).@"struct".overloadsOperators()) s.err(callee.loc, "type '{s}' does not provide a call operator", .{s.typeName(c.ty)});
} else if (c.ty != TypeTable.error_type) s.err(callee.loc, "called object type '{s}' is not a function", .{s.typeName(c.ty)});
return err_typed;