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(
65406540 const ident_name = try astgen.identifierTokenString(ident_token);
65416541
65426542 if (ident_name_raw[0] != '@') {
6543 if (simple_types.get(ident_name)) |zir_const_ref| {
6543 if (primitives.get(ident_name)) |zir_const_ref| {
65446544 return rvalue(gz, rl, zir_const_ref, ident);
65456545 }
65466546
......@@ -8071,7 +8071,7 @@ fn calleeExpr(
80718071 }
80728072}
80738073
8074pub const simple_types = std.ComptimeStringMap(Zir.Inst.Ref, .{
8074const primitives = std.ComptimeStringMap(Zir.Inst.Ref, .{
80758075 .{ "anyerror", .anyerror_type },
80768076 .{ "anyframe", .anyframe_type },
80778077 .{ "bool", .bool_type },
......@@ -10505,8 +10505,8 @@ fn nullTerminatedString(astgen: AstGen, index: usize) [*:0]const u8 {
1050510505 return @ptrCast([*:0]const u8, astgen.string_bytes.items.ptr) + index;
1050610506}
1050710507
10508fn isPrimitive(name: []const u8) bool {
10509 if (simple_types.get(name) != null) return true;
10508pub fn isPrimitive(name: []const u8) bool {
10509 if (primitives.get(name) != null) return true;
1051010510 if (name.len < 2) return false;
1051110511 const first_c = name[0];
1051210512 if (first_c != 'i' and first_c != 'u') return false;
src/Zir.zig+1-1
......@@ -1644,7 +1644,7 @@ pub const Inst = struct {
16441644 /// be derived by subtracting `typed_value_map.len`.
16451645 ///
16461646 /// When adding a tag to this enum, consider adding a corresponding entry to
1647 /// `simple_types` in astgen.
1647 /// `primitives` in astgen.
16481648 ///
16491649 /// The tag type is specified so that it is safe to bitcast between `[]u32`
16501650 /// and `[]Ref`.
src/translate_c/ast.zig+1-14
......@@ -804,21 +804,8 @@ const Context = struct {
804804 return c.addTokenFmt(tag, "{s}", .{bytes});
805805 }
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
820807 fn addIdentifier(c: *Context, bytes: []const u8) Allocator.Error!TokenIndex {
821 if (isZigPrimitiveType(bytes))
808 if (@import("../AstGen.zig").isPrimitive(bytes))
822809 return c.addTokenFmt(.identifier, "@\"{s}\"", .{bytes});
823810 return c.addTokenFmt(.identifier, "{s}", .{std.zig.fmtId(bytes)});
824811 }