authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-10-10 11:39:35-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-10-10 11:41:07-07:00
logd1fd864da7859989828feb8be806634606cc761c
tree8d4bcd3470852ecdd9e9edc5810fa1de1fe31174
parenta5ecffa4617ea5ceed1edfe76b74896e7c249a2d

translate-c: fix logic for checking primitive names

isZigPrimitiveType had a bug where it checked the integer names (e.g. u32) before primitives, leading it to incorrectly return `false` for `undefined` which starts with `u`. Related: #9928

3 files changed, 6 insertions(+), 19 deletions(-)

src/AstGen.zig+4-4
...@@ -6540,7 +6540,7 @@ fn identifier(...@@ -6540,7 +6540,7 @@ fn identifier(
6540 const ident_name = try astgen.identifierTokenString(ident_token);6540 const ident_name = try astgen.identifierTokenString(ident_token);
65416541
6542 if (ident_name_raw[0] != '@') {6542 if (ident_name_raw[0] != '@') {
6543 if (simple_types.get(ident_name)) |zir_const_ref| {6543 if (primitives.get(ident_name)) |zir_const_ref| {
6544 return rvalue(gz, rl, zir_const_ref, ident);6544 return rvalue(gz, rl, zir_const_ref, ident);
6545 }6545 }
65466546
...@@ -8071,7 +8071,7 @@ fn calleeExpr(...@@ -8071,7 +8071,7 @@ fn calleeExpr(
8071 }8071 }
8072}8072}
80738073
8074pub const simple_types = std.ComptimeStringMap(Zir.Inst.Ref, .{8074const primitives = std.ComptimeStringMap(Zir.Inst.Ref, .{
8075 .{ "anyerror", .anyerror_type },8075 .{ "anyerror", .anyerror_type },
8076 .{ "anyframe", .anyframe_type },8076 .{ "anyframe", .anyframe_type },
8077 .{ "bool", .bool_type },8077 .{ "bool", .bool_type },
...@@ -10505,8 +10505,8 @@ fn nullTerminatedString(astgen: AstGen, index: usize) [*:0]const u8 {...@@ -10505,8 +10505,8 @@ fn nullTerminatedString(astgen: AstGen, index: usize) [*:0]const u8 {
10505 return @ptrCast([*:0]const u8, astgen.string_bytes.items.ptr) + index;10505 return @ptrCast([*:0]const u8, astgen.string_bytes.items.ptr) + index;
10506}10506}
1050710507
10508fn isPrimitive(name: []const u8) bool {10508pub fn isPrimitive(name: []const u8) bool {
10509 if (simple_types.get(name) != null) return true;10509 if (primitives.get(name) != null) return true;
10510 if (name.len < 2) return false;10510 if (name.len < 2) return false;
10511 const first_c = name[0];10511 const first_c = name[0];
10512 if (first_c != 'i' and first_c != 'u') return false;10512 if (first_c != 'i' and first_c != 'u') return false;
src/Zir.zig+1-1
...@@ -1644,7 +1644,7 @@ pub const Inst = struct {...@@ -1644,7 +1644,7 @@ pub const Inst = struct {
1644 /// be derived by subtracting `typed_value_map.len`.1644 /// be derived by subtracting `typed_value_map.len`.
1645 ///1645 ///
1646 /// When adding a tag to this enum, consider adding a corresponding entry to1646 /// When adding a tag to this enum, consider adding a corresponding entry to
1647 /// `simple_types` in astgen.1647 /// `primitives` in astgen.
1648 ///1648 ///
1649 /// The tag type is specified so that it is safe to bitcast between `[]u32`1649 /// The tag type is specified so that it is safe to bitcast between `[]u32`
1650 /// and `[]Ref`.1650 /// and `[]Ref`.
src/translate_c/ast.zig+1-14
...@@ -804,21 +804,8 @@ const Context = struct {...@@ -804,21 +804,8 @@ const Context = struct {
804 return c.addTokenFmt(tag, "{s}", .{bytes});804 return c.addTokenFmt(tag, "{s}", .{bytes});
805 }805 }
806806
807 fn isZigPrimitiveType(name: []const u8) bool {
808 if (name.len > 1 and (name[0] == 'u' or name[0] == 'i')) {
809 for (name[1..]) |c| {
810 switch (c) {
811 '0'...'9' => {},
812 else => return false,
813 }
814 }
815 return true;
816 }
817 return @import("../AstGen.zig").simple_types.has(name);
818 }
819
820 fn addIdentifier(c: *Context, bytes: []const u8) Allocator.Error!TokenIndex {807 fn addIdentifier(c: *Context, bytes: []const u8) Allocator.Error!TokenIndex {
821 if (isZigPrimitiveType(bytes))808 if (@import("../AstGen.zig").isPrimitive(bytes))
822 return c.addTokenFmt(.identifier, "@\"{s}\"", .{bytes});809 return c.addTokenFmt(.identifier, "@\"{s}\"", .{bytes});
823 return c.addTokenFmt(.identifier, "{s}", .{std.zig.fmtId(bytes)});810 return c.addTokenFmt(.identifier, "{s}", .{std.zig.fmtId(bytes)});
824 }811 }