Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add c_char type #15263

Merged
merged 1 commit into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/std/start.zig
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ fn callMainWithArgs(argc: usize, argv: [*][*:0]u8, envp: [][*:0]u8) u8 {
return initEventLoopAndCallMain();
}

fn main(c_argc: c_int, c_argv: [*][*:0]u8, c_envp: [*:null]?[*:0]u8) callconv(.C) c_int {
fn main(c_argc: c_int, c_argv: [*][*:0]c_char, c_envp: [*:null]?[*:0]c_char) callconv(.C) c_int {
var env_count: usize = 0;
while (c_envp[env_count] != null) : (env_count += 1) {}
const envp = @ptrCast([*][*:0]u8, c_envp)[0..env_count];
Expand All @@ -527,7 +527,7 @@ fn main(c_argc: c_int, c_argv: [*][*:0]u8, c_envp: [*:null]?[*:0]u8) callconv(.C
return @call(.always_inline, callMainWithArgs, .{ @intCast(usize, c_argc), @ptrCast([*][*:0]u8, c_argv), envp });
}

fn mainWithoutEnv(c_argc: c_int, c_argv: [*][*:0]u8) callconv(.C) c_int {
fn mainWithoutEnv(c_argc: c_int, c_argv: [*][*:0]c_char) callconv(.C) c_int {
std.os.argv = @ptrCast([*][*:0]u8, c_argv)[0..@intCast(usize, c_argc)];
return @call(.always_inline, callMain, .{});
}
Expand Down
20 changes: 20 additions & 0 deletions lib/std/target.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1905,6 +1905,7 @@ pub const Target = struct {
}

pub const CType = enum {
char,
short,
ushort,
int,
Expand All @@ -1920,6 +1921,7 @@ pub const Target = struct {

pub fn c_type_byte_size(t: Target, c_type: CType) u16 {
return switch (c_type) {
.char,
.short,
.ushort,
.int,
Expand Down Expand Up @@ -1948,28 +1950,33 @@ pub const Target = struct {
switch (target.os.tag) {
.freestanding, .other => switch (target.cpu.arch) {
.msp430 => switch (c_type) {
.char => return 8,
.short, .ushort, .int, .uint => return 16,
.float, .long, .ulong => return 32,
.longlong, .ulonglong, .double, .longdouble => return 64,
},
.avr => switch (c_type) {
.char => return 8,
.short, .ushort, .int, .uint => return 16,
.long, .ulong, .float, .double, .longdouble => return 32,
.longlong, .ulonglong => return 64,
},
.tce, .tcele => switch (c_type) {
.char => return 8,
.short, .ushort => return 16,
.int, .uint, .long, .ulong, .longlong, .ulonglong => return 32,
.float, .double, .longdouble => return 32,
},
.mips64, .mips64el => switch (c_type) {
.char => return 8,
.short, .ushort => return 16,
.int, .uint, .float => return 32,
.long, .ulong => return if (target.abi != .gnuabin32) 64 else 32,
.longlong, .ulonglong, .double => return 64,
.longdouble => return 128,
},
.x86_64 => switch (c_type) {
.char => return 8,
.short, .ushort => return 16,
.int, .uint, .float => return 32,
.long, .ulong => switch (target.abi) {
Expand All @@ -1980,6 +1987,7 @@ pub const Target = struct {
.longdouble => return 80,
},
else => switch (c_type) {
.char => return 8,
.short, .ushort => return 16,
.int, .uint, .float => return 32,
.long, .ulong => return target.cpu.arch.ptrBitWidth(),
Expand Down Expand Up @@ -2036,28 +2044,33 @@ pub const Target = struct {
.minix,
=> switch (target.cpu.arch) {
.msp430 => switch (c_type) {
.char => return 8,
.short, .ushort, .int, .uint => return 16,
.long, .ulong, .float => return 32,
.longlong, .ulonglong, .double, .longdouble => return 64,
},
.avr => switch (c_type) {
.char => return 8,
.short, .ushort, .int, .uint => return 16,
.long, .ulong, .float, .double, .longdouble => return 32,
.longlong, .ulonglong => return 64,
},
.tce, .tcele => switch (c_type) {
.char => return 8,
.short, .ushort => return 16,
.int, .uint, .long, .ulong, .longlong, .ulonglong => return 32,
.float, .double, .longdouble => return 32,
},
.mips64, .mips64el => switch (c_type) {
.char => return 8,
.short, .ushort => return 16,
.int, .uint, .float => return 32,
.long, .ulong => return if (target.abi != .gnuabin32) 64 else 32,
.longlong, .ulonglong, .double => return 64,
.longdouble => if (target.os.tag == .freebsd) return 64 else return 128,
},
.x86_64 => switch (c_type) {
.char => return 8,
.short, .ushort => return 16,
.int, .uint, .float => return 32,
.long, .ulong => switch (target.abi) {
Expand All @@ -2068,6 +2081,7 @@ pub const Target = struct {
.longdouble => return 80,
},
else => switch (c_type) {
.char => return 8,
.short, .ushort => return 16,
.int, .uint, .float => return 32,
.long, .ulong => return target.cpu.arch.ptrBitWidth(),
Expand Down Expand Up @@ -2128,6 +2142,7 @@ pub const Target = struct {

.windows, .uefi => switch (target.cpu.arch) {
.x86 => switch (c_type) {
.char => return 8,
.short, .ushort => return 16,
.int, .uint, .float => return 32,
.long, .ulong => return 32,
Expand All @@ -2138,6 +2153,7 @@ pub const Target = struct {
},
},
.x86_64 => switch (c_type) {
.char => return 8,
.short, .ushort => return 16,
.int, .uint, .float => return 32,
.long, .ulong => switch (target.abi) {
Expand All @@ -2151,6 +2167,7 @@ pub const Target = struct {
},
},
else => switch (c_type) {
.char => return 8,
.short, .ushort => return 16,
.int, .uint, .float => return 32,
.long, .ulong => return 32,
Expand All @@ -2160,6 +2177,7 @@ pub const Target = struct {
},

.macos, .ios, .tvos, .watchos => switch (c_type) {
.char => return 8,
.short, .ushort => return 16,
.int, .uint, .float => return 32,
.long, .ulong => switch (target.cpu.arch) {
Expand All @@ -2182,6 +2200,7 @@ pub const Target = struct {
},

.nvcl, .cuda => switch (c_type) {
.char => return 8,
.short, .ushort => return 16,
.int, .uint, .float => return 32,
.long, .ulong => switch (target.cpu.arch) {
Expand All @@ -2194,6 +2213,7 @@ pub const Target = struct {
},

.amdhsa, .amdpal => switch (c_type) {
.char => return 8,
.short, .ushort => return 16,
.int, .uint, .float => return 32,
.long, .ulong, .longlong, .ulonglong, .double => return 64,
Expand Down
1 change: 1 addition & 0 deletions lib/std/zig/primitives.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub const names = std.ComptimeStringMap(void, .{
.{"c_long"},
.{"c_longdouble"},
.{"c_longlong"},
.{"c_char"},
.{"c_short"},
.{"c_uint"},
.{"c_ulong"},
Expand Down
4 changes: 4 additions & 0 deletions src/AstGen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9054,6 +9054,7 @@ const primitive_instrs = std.ComptimeStringMap(Zir.Inst.Ref, .{
.{ "c_long", .c_long_type },
.{ "c_longdouble", .c_longdouble_type },
.{ "c_longlong", .c_longlong_type },
.{ "c_char", .c_char_type },
.{ "c_short", .c_short_type },
.{ "c_uint", .c_uint_type },
.{ "c_ulong", .c_ulong_type },
Expand Down Expand Up @@ -9802,6 +9803,7 @@ fn nodeImpliesMoreThanOnePossibleValue(tree: *const Ast, start_node: Ast.Node.In
.c_long_type,
.c_longdouble_type,
.c_longlong_type,
.c_char_type,
.c_short_type,
.c_uint_type,
.c_ulong_type,
Expand Down Expand Up @@ -10047,6 +10049,7 @@ fn nodeImpliesComptimeOnly(tree: *const Ast, start_node: Ast.Node.Index) bool {
.c_long_type,
.c_longdouble_type,
.c_longlong_type,
.c_char_type,
.c_short_type,
.c_uint_type,
.c_ulong_type,
Expand Down Expand Up @@ -10187,6 +10190,7 @@ fn rvalue(
as_ty | @enumToInt(Zir.Inst.Ref.i64_type),
as_ty | @enumToInt(Zir.Inst.Ref.usize_type),
as_ty | @enumToInt(Zir.Inst.Ref.isize_type),
as_ty | @enumToInt(Zir.Inst.Ref.c_char_type),
as_ty | @enumToInt(Zir.Inst.Ref.c_short_type),
as_ty | @enumToInt(Zir.Inst.Ref.c_ushort_type),
as_ty | @enumToInt(Zir.Inst.Ref.c_int_type),
Expand Down
3 changes: 3 additions & 0 deletions src/Sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30622,6 +30622,7 @@ pub fn resolveTypeRequiresComptime(sema: *Sema, ty: Type) CompileError!bool {
.i128,
.usize,
.isize,
.c_char,
.c_short,
.c_ushort,
.c_int,
Expand Down Expand Up @@ -32010,6 +32011,7 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value {
.i128,
.usize,
.isize,
.c_char,
.c_short,
.c_ushort,
.c_int,
Expand Down Expand Up @@ -32640,6 +32642,7 @@ pub fn typeRequiresComptime(sema: *Sema, ty: Type) CompileError!bool {
.i128,
.usize,
.isize,
.c_char,
.c_short,
.c_ushort,
.c_int,
Expand Down
1 change: 1 addition & 0 deletions src/TypedValue.zig
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ pub fn print(
.i128_type => return writer.writeAll("i128"),
.isize_type => return writer.writeAll("isize"),
.usize_type => return writer.writeAll("usize"),
.c_char_type => return writer.writeAll("c_char"),
.c_short_type => return writer.writeAll("c_short"),
.c_ushort_type => return writer.writeAll("c_ushort"),
.c_int_type => return writer.writeAll("c_int"),
Expand Down
5 changes: 5 additions & 0 deletions src/Zir.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2062,6 +2062,7 @@ pub const Inst = struct {
i128_type,
usize_type,
isize_type,
c_char_type,
c_short_type,
c_ushort_type,
c_int_type,
Expand Down Expand Up @@ -2201,6 +2202,10 @@ pub const Inst = struct {
.ty = Type.initTag(.type),
.val = Value.initTag(.isize_type),
},
.c_char_type = .{
.ty = Type.initTag(.type),
.val = Value.initTag(.c_char_type),
},
.c_short_type = .{
.ty = Type.initTag(.type),
.val = Value.initTag(.c_short_type),
Expand Down
1 change: 1 addition & 0 deletions src/codegen/c/type.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1358,6 +1358,7 @@ pub const CType = extern union {
else if (ty.isAbiInt()) switch (ty.tag()) {
.usize => self.init(.uintptr_t),
.isize => self.init(.intptr_t),
.c_char => self.init(.char),
.c_short => self.init(.short),
.c_ushort => self.init(.@"unsigned short"),
.c_int => self.init(.int),
Expand Down
Loading