authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2021-05-17 17:41:42+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-17 17:15:22-04:00
logfe1a166589db0f2371429c93e1e1e622c19378f1
tree293a4089e582d60e9beed6275579889eab694fc5
parent5414bd48edd460ae8667c811e13aa9b5d9fab919

translate-c: Add `@truncate` where needed

Make getLimitedValue API much easier to use with zig. Fixes the compilation on 32bit hosts.

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

src/clang.zig+4-1
...@@ -1,3 +1,4 @@...@@ -1,3 +1,4 @@
1const std = @import("std");
1pub const builtin = @import("builtin");2pub const builtin = @import("builtin");
23
3pub const SourceLocation = extern struct {4pub const SourceLocation = extern struct {
...@@ -115,7 +116,9 @@ pub const APFloatBaseSemantics = extern enum {...@@ -115,7 +116,9 @@ pub const APFloatBaseSemantics = extern enum {
115};116};
116117
117pub const APInt = opaque {118pub const APInt = opaque {
118 pub const getLimitedValue = ZigClangAPInt_getLimitedValue;119 pub fn getLimitedValue(self: *const APInt, comptime T: type) T {
120 return @truncate(T, ZigClangAPInt_getLimitedValue(self, std.math.maxInt(T)));
121 }
119 extern fn ZigClangAPInt_getLimitedValue(*const APInt, limit: u64) u64;122 extern fn ZigClangAPInt_getLimitedValue(*const APInt, limit: u64) u64;
120};123};
121124
src/translate_c.zig+2-2
...@@ -2341,7 +2341,7 @@ fn transInitListExprArray(...@@ -2341,7 +2341,7 @@ fn transInitListExprArray(
2341 assert(@ptrCast(*const clang.Type, arr_type).isConstantArrayType());2341 assert(@ptrCast(*const clang.Type, arr_type).isConstantArrayType());
2342 const const_arr_ty = @ptrCast(*const clang.ConstantArrayType, arr_type);2342 const const_arr_ty = @ptrCast(*const clang.ConstantArrayType, arr_type);
2343 const size_ap_int = const_arr_ty.getSize();2343 const size_ap_int = const_arr_ty.getSize();
2344 const all_count = size_ap_int.getLimitedValue(math.maxInt(usize));2344 const all_count = size_ap_int.getLimitedValue(usize);
2345 const leftover_count = all_count - init_count;2345 const leftover_count = all_count - init_count;
23462346
2347 if (all_count == 0) {2347 if (all_count == 0) {
...@@ -4266,7 +4266,7 @@ fn transType(c: *Context, scope: *Scope, ty: *const clang.Type, source_loc: clan...@@ -4266,7 +4266,7 @@ fn transType(c: *Context, scope: *Scope, ty: *const clang.Type, source_loc: clan
4266 const const_arr_ty = @ptrCast(*const clang.ConstantArrayType, ty);4266 const const_arr_ty = @ptrCast(*const clang.ConstantArrayType, ty);
42674267
4268 const size_ap_int = const_arr_ty.getSize();4268 const size_ap_int = const_arr_ty.getSize();
4269 const size = size_ap_int.getLimitedValue(math.maxInt(usize));4269 const size = size_ap_int.getLimitedValue(usize);
4270 const elem_type = try transType(c, scope, const_arr_ty.getElementType().getTypePtr(), source_loc);4270 const elem_type = try transType(c, scope, const_arr_ty.getElementType().getTypePtr(), source_loc);
42714271
4272 return Tag.array_type.create(c.arena, .{ .len = size, .elem_type = elem_type });4272 return Tag.array_type.create(c.arena, .{ .len = size, .elem_type = elem_type });