translate-c: use Aro's tokenizer

This commit is contained in:
Veikka Tuominen
2023-11-24 20:11:11 +02:00
parent d2a8660d04
commit 74010fecc7
9 changed files with 324 additions and 1872 deletions

View File

@@ -218,7 +218,6 @@ set(ZIG_STAGE2_SOURCES
"${CMAKE_SOURCE_DIR}/lib/std/builtin.zig"
"${CMAKE_SOURCE_DIR}/lib/std/c.zig"
"${CMAKE_SOURCE_DIR}/lib/std/c/linux.zig"
"${CMAKE_SOURCE_DIR}/lib/std/c/tokenizer.zig"
"${CMAKE_SOURCE_DIR}/lib/std/child_process.zig"
"${CMAKE_SOURCE_DIR}/lib/std/coff.zig"
"${CMAKE_SOURCE_DIR}/lib/std/comptime_string_map.zig"

View File

@@ -5,14 +5,6 @@ const page_size = std.mem.page_size;
const iovec = std.os.iovec;
const iovec_const = std.os.iovec_const;
test {
_ = tokenizer;
}
pub const tokenizer = @import("c/tokenizer.zig");
pub const Token = tokenizer.Token;
pub const Tokenizer = tokenizer.Tokenizer;
/// The return type is `type` to force comptime function call execution.
/// TODO: https://github.com/ziglang/zig/issues/425
/// If not linking libc, returns struct{pub const ok = false;}

File diff suppressed because it is too large Load Diff

View File

@@ -252,7 +252,7 @@ test "sizeof" {
try testing.expect(sizeof(anyopaque) == 1);
}
pub const CIntLiteralBase = enum { decimal, octal, hexadecimal };
pub const CIntLiteralBase = enum { decimal, octal, hex };
/// Deprecated: use `CIntLiteralBase`
pub const CIntLiteralRadix = CIntLiteralBase;
@@ -289,13 +289,13 @@ pub fn promoteIntLiteral(
}
test "promoteIntLiteral" {
const signed_hex = promoteIntLiteral(c_int, math.maxInt(c_int) + 1, .hexadecimal);
const signed_hex = promoteIntLiteral(c_int, math.maxInt(c_int) + 1, .hex);
try testing.expectEqual(c_uint, @TypeOf(signed_hex));
if (math.maxInt(c_longlong) == math.maxInt(c_int)) return;
const signed_decimal = promoteIntLiteral(c_int, math.maxInt(c_int) + 1, .decimal);
const unsigned = promoteIntLiteral(c_uint, math.maxInt(c_uint) + 1, .hexadecimal);
const unsigned = promoteIntLiteral(c_uint, math.maxInt(c_uint) + 1, .hex);
if (math.maxInt(c_long) > math.maxInt(c_int)) {
try testing.expectEqual(c_long, @TypeOf(signed_decimal));

View File

@@ -4194,7 +4194,7 @@ pub const CImportResult = struct {
/// This API is currently coupled pretty tightly to stage1's needs; it will need to be reworked
/// a bit when we want to start using it from self-hosted.
pub fn cImport(comp: *Compilation, c_src: []const u8) !CImportResult {
if (build_options.only_c) unreachable; // @cImport is not needed for bootstrapping
if (build_options.only_core_functionality) @panic("@cImport is not available in a zig2.c build");
const tracy_trace = trace(@src());
defer tracy_trace.end();

View File

@@ -4286,7 +4286,7 @@ fn updateModule(comp: *Compilation) !void {
}
fn cmdTranslateC(comp: *Compilation, arena: Allocator, fancy_output: ?*Compilation.CImportResult) !void {
if (build_options.only_c) unreachable; // translate-c is not needed for bootstrapping
if (build_options.only_core_functionality) @panic("@translate-c is not available in a zig2.c build");
assert(comp.c_source_files.len == 1);
const c_source_file = comp.c_source_files[0];

View File

@@ -22,7 +22,9 @@ pub fn with(comptime Properties: type) type {
return .{};
}
pub fn tagFromName(name: []const u8) ?Tag {
return @enumFromInt(name.len);
var res: u16 = 0;
for (name) |c| res +%= c;
return @enumFromInt(res);
}
pub const NameBuf = struct {
pub fn span(_: *const NameBuf) []const u8 {

File diff suppressed because it is too large Load Diff

View File

@@ -424,7 +424,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\ });
\\}
,
\\pub const B = A(@as(f32, 0.0));
\\pub const B = A(@as(f32, 0));
});
cases.add("complex switch",
@@ -633,7 +633,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
cases.add("#define hex literal with capital X",
\\#define VAL 0XF00D
, &[_][]const u8{
\\pub const VAL = @import("std").zig.c_translation.promoteIntLiteral(c_int, 0xF00D, .hexadecimal);
\\pub const VAL = @import("std").zig.c_translation.promoteIntLiteral(c_int, 0xF00D, .hex);
});
cases.add("anonymous struct & unions",
@@ -1243,12 +1243,12 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\extern const long double my_extended_precision_longdouble = 1.0000000000000003l;
, &([_][]const u8{
"pub const foo = @as(f32, 3.14);",
"pub const bar = @as(c_longdouble, 16.0e-2);",
"pub const bar = @as(c_longdouble, 16.e-2);",
"pub const FOO = @as(f64, 0.12345);",
"pub const BAR = @as(f64, 0.12345);",
"pub const baz = @as(f64, 1e1);",
"pub const BAZ = @as(f32, 42e-3);",
"pub const foobar = -@as(c_longdouble, 73.0);",
"pub const foobar = -@as(c_longdouble, 73);",
"pub export const my_float: f32 = 1.0;",
"pub export const my_double: f64 = 1.0;",
"pub export const my_longdouble: c_longdouble = 1.0;",
@@ -1272,7 +1272,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
"pub const BAR = -@as(f32, 0x8F.BP5);",
"pub const FOOBAR = @as(f64, 0x0P+0);",
"pub const BAZ = -@as(f64, 0x0.0a5dp+12);",
"pub const FOOBAZ = @as(c_longdouble, 0xfE.0P-1);",
"pub const FOOBAZ = @as(c_longdouble, 0xfE.P-1);",
});
cases.add("comments",
@@ -3730,7 +3730,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
, &[_][]const u8{
\\pub const NULL = @import("std").zig.c_translation.cast(?*anyopaque, @as(c_int, 0));
,
\\pub const FOO = @import("std").zig.c_translation.cast(c_int, @import("std").zig.c_translation.promoteIntLiteral(c_int, 0x8000, .hexadecimal));
\\pub const FOO = @import("std").zig.c_translation.cast(c_int, @import("std").zig.c_translation.promoteIntLiteral(c_int, 0x8000, .hex));
});
if (builtin.abi == .msvc) {
@@ -3812,7 +3812,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\pub const MAY_NEED_PROMOTION_1 = @import("std").zig.c_translation.promoteIntLiteral(c_int, 10241024, .decimal);
\\pub const MAY_NEED_PROMOTION_2 = @import("std").zig.c_translation.promoteIntLiteral(c_long, 307230723072, .decimal);
\\pub const MAY_NEED_PROMOTION_3 = @import("std").zig.c_translation.promoteIntLiteral(c_ulong, 819281928192, .decimal);
\\pub const MAY_NEED_PROMOTION_HEX = @import("std").zig.c_translation.promoteIntLiteral(c_int, 0x80000000, .hexadecimal);
\\pub const MAY_NEED_PROMOTION_HEX = @import("std").zig.c_translation.promoteIntLiteral(c_int, 0x80000000, .hex);
\\pub const MAY_NEED_PROMOTION_OCT = @import("std").zig.c_translation.promoteIntLiteral(c_int, 0o20000000000, .octal);
});