Skip to content

Commit 9a72c94

Browse files
author
Stephen Gutekanst
committed
generator: fix type method / self method naming conflict
Signed-off-by: Stephen Gutekanst <[email protected]>
1 parent b03a07c commit 9a72c94

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

generator.zig

+22-1
Original file line numberDiff line numberDiff line change
@@ -1186,8 +1186,28 @@ fn Generator(comptime WriterType: type) type {
11861186
return;
11871187
}
11881188

1189+
// Class 'type methods' and 'self methods' can have naming conflicts, e.g. NSCursor pop()
1190+
// which takes a self parameter and NSCursor pop() which is a type method. We prefix the
1191+
// type method one with 'T_' only if there would be a conflict.
1192+
const nameConflict = blk: {
1193+
var count: usize = 0;
1194+
for (container.methods.items) |m| {
1195+
if (std.mem.eql(u8, m.name, method.name)) count += 1;
1196+
if (count >= 2) break :blk true;
1197+
}
1198+
break :blk false;
1199+
};
1200+
var name = method.name;
1201+
if (nameConflict and !method.instance) {
1202+
const new_name = try self.allocator.alloc(u8, method.name.len + 2);
1203+
@memcpy(new_name[2..], method.name);
1204+
new_name[0] = 'T';
1205+
new_name[1] = '_';
1206+
name = new_name;
1207+
}
1208+
11891209
try self.writer.writeAll(" pub fn ");
1190-
try self.generateMethodName(method.name);
1210+
try self.generateMethodName(name);
11911211
try self.writer.print("(", .{});
11921212
try self.generateMethodParams(method);
11931213
try self.writer.print(") ", .{});
@@ -1935,6 +1955,7 @@ fn generateAppKit(generator: anytype) !void {
19351955
[2][]const u8{ "NSCursor", "hide" },
19361956
[2][]const u8{ "NSCursor", "unhide" },
19371957
[2][]const u8{ "NSCursor", "pop" },
1958+
19381959
[2][]const u8{ "NSCursor", "push" },
19391960
[2][]const u8{ "NSCursor", "arrowCursor" },
19401961
[2][]const u8{ "NSCursor", "IBeamCursor" },

src/app_kit.zig

+2-2
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ pub const Event = opaque {
371371
pub fn phase(self_: *@This()) EventPhase {
372372
return objc.msgSend(self_, "phase", EventPhase, .{});
373373
}
374-
pub fn modifierFlags() EventModifierFlags {
374+
pub fn T_modifierFlags() EventModifierFlags {
375375
return objc.msgSend(@This().InternalInfo.class(), "modifierFlags", EventModifierFlags, .{});
376376
}
377377
pub fn pressedMouseButtons() UInteger {
@@ -431,7 +431,7 @@ pub const Cursor = opaque {
431431
pub fn unhide() void {
432432
return objc.msgSend(@This().InternalInfo.class(), "unhide", void, .{});
433433
}
434-
pub fn pop() void {
434+
pub fn T_pop() void {
435435
return objc.msgSend(@This().InternalInfo.class(), "pop", void, .{});
436436
}
437437
pub fn push(self_: *@This()) void {

0 commit comments

Comments
 (0)