Skip to content

Commit d702321

Browse files
committed
add c_char type
closes #875
1 parent 60bd13b commit d702321

File tree

11 files changed

+62
-2
lines changed

11 files changed

+62
-2
lines changed

lib/std/start.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ fn callMainWithArgs(argc: usize, argv: [*][*:0]u8, envp: [][*:0]u8) u8 {
512512
return initEventLoopAndCallMain();
513513
}
514514

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

530-
fn mainWithoutEnv(c_argc: c_int, c_argv: [*][*:0]u8) callconv(.C) c_int {
530+
fn mainWithoutEnv(c_argc: c_int, c_argv: [*][*:0]c_char) callconv(.C) c_int {
531531
std.os.argv = @ptrCast([*][*:0]u8, c_argv)[0..@intCast(usize, c_argc)];
532532
return @call(.always_inline, callMain, .{});
533533
}

lib/std/target.zig

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,6 +1905,7 @@ pub const Target = struct {
19051905
}
19061906

19071907
pub const CType = enum {
1908+
char,
19081909
short,
19091910
ushort,
19101911
int,
@@ -1920,6 +1921,7 @@ pub const Target = struct {
19201921

19211922
pub fn c_type_byte_size(t: Target, c_type: CType) u16 {
19221923
return switch (c_type) {
1924+
.char,
19231925
.short,
19241926
.ushort,
19251927
.int,
@@ -1948,28 +1950,33 @@ pub const Target = struct {
19481950
switch (target.os.tag) {
19491951
.freestanding, .other => switch (target.cpu.arch) {
19501952
.msp430 => switch (c_type) {
1953+
.char => return 8,
19511954
.short, .ushort, .int, .uint => return 16,
19521955
.float, .long, .ulong => return 32,
19531956
.longlong, .ulonglong, .double, .longdouble => return 64,
19541957
},
19551958
.avr => switch (c_type) {
1959+
.char => return 8,
19561960
.short, .ushort, .int, .uint => return 16,
19571961
.long, .ulong, .float, .double, .longdouble => return 32,
19581962
.longlong, .ulonglong => return 64,
19591963
},
19601964
.tce, .tcele => switch (c_type) {
1965+
.char => return 8,
19611966
.short, .ushort => return 16,
19621967
.int, .uint, .long, .ulong, .longlong, .ulonglong => return 32,
19631968
.float, .double, .longdouble => return 32,
19641969
},
19651970
.mips64, .mips64el => switch (c_type) {
1971+
.char => return 8,
19661972
.short, .ushort => return 16,
19671973
.int, .uint, .float => return 32,
19681974
.long, .ulong => return if (target.abi != .gnuabin32) 64 else 32,
19691975
.longlong, .ulonglong, .double => return 64,
19701976
.longdouble => return 128,
19711977
},
19721978
.x86_64 => switch (c_type) {
1979+
.char => return 8,
19731980
.short, .ushort => return 16,
19741981
.int, .uint, .float => return 32,
19751982
.long, .ulong => switch (target.abi) {
@@ -1980,6 +1987,7 @@ pub const Target = struct {
19801987
.longdouble => return 80,
19811988
},
19821989
else => switch (c_type) {
1990+
.char => return 8,
19831991
.short, .ushort => return 16,
19841992
.int, .uint, .float => return 32,
19851993
.long, .ulong => return target.cpu.arch.ptrBitWidth(),
@@ -2036,28 +2044,33 @@ pub const Target = struct {
20362044
.minix,
20372045
=> switch (target.cpu.arch) {
20382046
.msp430 => switch (c_type) {
2047+
.char => return 8,
20392048
.short, .ushort, .int, .uint => return 16,
20402049
.long, .ulong, .float => return 32,
20412050
.longlong, .ulonglong, .double, .longdouble => return 64,
20422051
},
20432052
.avr => switch (c_type) {
2053+
.char => return 8,
20442054
.short, .ushort, .int, .uint => return 16,
20452055
.long, .ulong, .float, .double, .longdouble => return 32,
20462056
.longlong, .ulonglong => return 64,
20472057
},
20482058
.tce, .tcele => switch (c_type) {
2059+
.char => return 8,
20492060
.short, .ushort => return 16,
20502061
.int, .uint, .long, .ulong, .longlong, .ulonglong => return 32,
20512062
.float, .double, .longdouble => return 32,
20522063
},
20532064
.mips64, .mips64el => switch (c_type) {
2065+
.char => return 8,
20542066
.short, .ushort => return 16,
20552067
.int, .uint, .float => return 32,
20562068
.long, .ulong => return if (target.abi != .gnuabin32) 64 else 32,
20572069
.longlong, .ulonglong, .double => return 64,
20582070
.longdouble => if (target.os.tag == .freebsd) return 64 else return 128,
20592071
},
20602072
.x86_64 => switch (c_type) {
2073+
.char => return 8,
20612074
.short, .ushort => return 16,
20622075
.int, .uint, .float => return 32,
20632076
.long, .ulong => switch (target.abi) {
@@ -2068,6 +2081,7 @@ pub const Target = struct {
20682081
.longdouble => return 80,
20692082
},
20702083
else => switch (c_type) {
2084+
.char => return 8,
20712085
.short, .ushort => return 16,
20722086
.int, .uint, .float => return 32,
20732087
.long, .ulong => return target.cpu.arch.ptrBitWidth(),
@@ -2128,6 +2142,7 @@ pub const Target = struct {
21282142

21292143
.windows, .uefi => switch (target.cpu.arch) {
21302144
.x86 => switch (c_type) {
2145+
.char => return 8,
21312146
.short, .ushort => return 16,
21322147
.int, .uint, .float => return 32,
21332148
.long, .ulong => return 32,
@@ -2138,6 +2153,7 @@ pub const Target = struct {
21382153
},
21392154
},
21402155
.x86_64 => switch (c_type) {
2156+
.char => return 8,
21412157
.short, .ushort => return 16,
21422158
.int, .uint, .float => return 32,
21432159
.long, .ulong => switch (target.abi) {
@@ -2151,6 +2167,7 @@ pub const Target = struct {
21512167
},
21522168
},
21532169
else => switch (c_type) {
2170+
.char => return 8,
21542171
.short, .ushort => return 16,
21552172
.int, .uint, .float => return 32,
21562173
.long, .ulong => return 32,
@@ -2160,6 +2177,7 @@ pub const Target = struct {
21602177
},
21612178

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

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

21962215
.amdhsa, .amdpal => switch (c_type) {
2216+
.char => return 8,
21972217
.short, .ushort => return 16,
21982218
.int, .uint, .float => return 32,
21992219
.long, .ulong, .longlong, .ulonglong, .double => return 64,

lib/std/zig/primitives.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub const names = std.ComptimeStringMap(void, .{
1111
.{"c_long"},
1212
.{"c_longdouble"},
1313
.{"c_longlong"},
14+
.{"c_char"},
1415
.{"c_short"},
1516
.{"c_uint"},
1617
.{"c_ulong"},

src/AstGen.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9054,6 +9054,7 @@ const primitive_instrs = std.ComptimeStringMap(Zir.Inst.Ref, .{
90549054
.{ "c_long", .c_long_type },
90559055
.{ "c_longdouble", .c_longdouble_type },
90569056
.{ "c_longlong", .c_longlong_type },
9057+
.{ "c_char", .c_char_type },
90579058
.{ "c_short", .c_short_type },
90589059
.{ "c_uint", .c_uint_type },
90599060
.{ "c_ulong", .c_ulong_type },
@@ -9802,6 +9803,7 @@ fn nodeImpliesMoreThanOnePossibleValue(tree: *const Ast, start_node: Ast.Node.In
98029803
.c_long_type,
98039804
.c_longdouble_type,
98049805
.c_longlong_type,
9806+
.c_char_type,
98059807
.c_short_type,
98069808
.c_uint_type,
98079809
.c_ulong_type,
@@ -10047,6 +10049,7 @@ fn nodeImpliesComptimeOnly(tree: *const Ast, start_node: Ast.Node.Index) bool {
1004710049
.c_long_type,
1004810050
.c_longdouble_type,
1004910051
.c_longlong_type,
10052+
.c_char_type,
1005010053
.c_short_type,
1005110054
.c_uint_type,
1005210055
.c_ulong_type,
@@ -10187,6 +10190,7 @@ fn rvalue(
1018710190
as_ty | @enumToInt(Zir.Inst.Ref.i64_type),
1018810191
as_ty | @enumToInt(Zir.Inst.Ref.usize_type),
1018910192
as_ty | @enumToInt(Zir.Inst.Ref.isize_type),
10193+
as_ty | @enumToInt(Zir.Inst.Ref.c_char_type),
1019010194
as_ty | @enumToInt(Zir.Inst.Ref.c_short_type),
1019110195
as_ty | @enumToInt(Zir.Inst.Ref.c_ushort_type),
1019210196
as_ty | @enumToInt(Zir.Inst.Ref.c_int_type),

src/Sema.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30622,6 +30622,7 @@ pub fn resolveTypeRequiresComptime(sema: *Sema, ty: Type) CompileError!bool {
3062230622
.i128,
3062330623
.usize,
3062430624
.isize,
30625+
.c_char,
3062530626
.c_short,
3062630627
.c_ushort,
3062730628
.c_int,
@@ -32010,6 +32011,7 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value {
3201032011
.i128,
3201132012
.usize,
3201232013
.isize,
32014+
.c_char,
3201332015
.c_short,
3201432016
.c_ushort,
3201532017
.c_int,
@@ -32640,6 +32642,7 @@ pub fn typeRequiresComptime(sema: *Sema, ty: Type) CompileError!bool {
3264032642
.i128,
3264132643
.usize,
3264232644
.isize,
32645+
.c_char,
3264332646
.c_short,
3264432647
.c_ushort,
3264532648
.c_int,

src/TypedValue.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ pub fn print(
9292
.i128_type => return writer.writeAll("i128"),
9393
.isize_type => return writer.writeAll("isize"),
9494
.usize_type => return writer.writeAll("usize"),
95+
.c_char_type => return writer.writeAll("c_char"),
9596
.c_short_type => return writer.writeAll("c_short"),
9697
.c_ushort_type => return writer.writeAll("c_ushort"),
9798
.c_int_type => return writer.writeAll("c_int"),

src/Zir.zig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2062,6 +2062,7 @@ pub const Inst = struct {
20622062
i128_type,
20632063
usize_type,
20642064
isize_type,
2065+
c_char_type,
20652066
c_short_type,
20662067
c_ushort_type,
20672068
c_int_type,
@@ -2201,6 +2202,10 @@ pub const Inst = struct {
22012202
.ty = Type.initTag(.type),
22022203
.val = Value.initTag(.isize_type),
22032204
},
2205+
.c_char_type = .{
2206+
.ty = Type.initTag(.type),
2207+
.val = Value.initTag(.c_char_type),
2208+
},
22042209
.c_short_type = .{
22052210
.ty = Type.initTag(.type),
22062211
.val = Value.initTag(.c_short_type),

src/codegen/c/type.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,6 +1358,7 @@ pub const CType = extern union {
13581358
else if (ty.isAbiInt()) switch (ty.tag()) {
13591359
.usize => self.init(.uintptr_t),
13601360
.isize => self.init(.intptr_t),
1361+
.c_char => self.init(.char),
13611362
.c_short => self.init(.short),
13621363
.c_ushort => self.init(.@"unsigned short"),
13631364
.c_int => self.init(.int),

0 commit comments

Comments
 (0)