mirror of
https://github.com/ziglang/zig.git
synced 2026-02-21 16:54:52 +00:00
Merge pull request #4703 from Vexu/translate-c
Translate-c improvements
This commit is contained in:
commit
9c4dc7b1bb
@ -765,6 +765,7 @@ pub extern fn ZigClangTagDecl_isThisDeclarationADefinition(self: *const ZigClang
|
||||
pub extern fn ZigClangEnumType_getDecl(record_ty: ?*const struct_ZigClangEnumType) *const struct_ZigClangEnumDecl;
|
||||
pub extern fn ZigClangRecordDecl_getCanonicalDecl(record_decl: ?*const struct_ZigClangRecordDecl) ?*const struct_ZigClangTagDecl;
|
||||
pub extern fn ZigClangFieldDecl_getCanonicalDecl(field_decl: ?*const struct_ZigClangFieldDecl) ?*const struct_ZigClangFieldDecl;
|
||||
pub extern fn ZigClangFieldDecl_getAlignedAttribute(field_decl: ?*const struct_ZigClangFieldDecl, *const ZigClangASTContext) c_uint;
|
||||
pub extern fn ZigClangEnumDecl_getCanonicalDecl(self: ?*const struct_ZigClangEnumDecl) ?*const struct_ZigClangTagDecl;
|
||||
pub extern fn ZigClangTypedefNameDecl_getCanonicalDecl(self: ?*const struct_ZigClangTypedefNameDecl) ?*const struct_ZigClangTypedefNameDecl;
|
||||
pub extern fn ZigClangFunctionDecl_getCanonicalDecl(self: ?*const struct_ZigClangFunctionDecl) ?*const struct_ZigClangFunctionDecl;
|
||||
|
||||
@ -560,7 +560,7 @@ fn visitVarDecl(c: *Context, var_decl: *const ZigClangVarDecl) Error!void {
|
||||
|
||||
// TODO https://github.com/ziglang/zig/issues/3756
|
||||
// TODO https://github.com/ziglang/zig/issues/1802
|
||||
const checked_name = if (isZigPrimitiveType(var_name)) try std.fmt.allocPrint(c.a(), "_{}", .{var_name}) else var_name;
|
||||
const checked_name = if (isZigPrimitiveType(var_name)) try std.fmt.allocPrint(c.a(), "{}_{}", .{var_name, c.getMangle()}) else var_name;
|
||||
const var_decl_loc = ZigClangVarDecl_getLocation(var_decl);
|
||||
|
||||
const qual_type = ZigClangVarDecl_getTypeSourceInfo_getType(var_decl);
|
||||
@ -632,7 +632,7 @@ fn visitVarDecl(c: *Context, var_decl: *const ZigClangVarDecl) Error!void {
|
||||
const align_expr = blk: {
|
||||
const alignment = ZigClangVarDecl_getAlignedAttribute(var_decl, rp.c.clang_context);
|
||||
if (alignment != 0) {
|
||||
_ = try appendToken(rp.c, .Keyword_linksection, "align");
|
||||
_ = try appendToken(rp.c, .Keyword_align, "align");
|
||||
_ = try appendToken(rp.c, .LParen, "(");
|
||||
// Clang reports the alignment in bits
|
||||
const expr = try transCreateNodeInt(rp.c, alignment / 8);
|
||||
@ -677,7 +677,7 @@ fn transTypeDef(c: *Context, typedef_decl: *const ZigClangTypedefNameDecl, top_l
|
||||
|
||||
// TODO https://github.com/ziglang/zig/issues/3756
|
||||
// TODO https://github.com/ziglang/zig/issues/1802
|
||||
const checked_name = if (isZigPrimitiveType(typedef_name)) try std.fmt.allocPrint(c.a(), "_{}", .{typedef_name}) else typedef_name;
|
||||
const checked_name = if (isZigPrimitiveType(typedef_name)) try std.fmt.allocPrint(c.a(), "{}_{}", .{typedef_name, c.getMangle()}) else typedef_name;
|
||||
|
||||
if (mem.eql(u8, checked_name, "uint8_t"))
|
||||
return transTypeDefAsBuiltin(c, typedef_decl, "u8")
|
||||
@ -827,6 +827,20 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?*
|
||||
else => |e| return e,
|
||||
};
|
||||
|
||||
const align_expr = blk: {
|
||||
const alignment = ZigClangFieldDecl_getAlignedAttribute(field_decl, rp.c.clang_context);
|
||||
if (alignment != 0) {
|
||||
_ = try appendToken(rp.c, .Keyword_align, "align");
|
||||
_ = try appendToken(rp.c, .LParen, "(");
|
||||
// Clang reports the alignment in bits
|
||||
const expr = try transCreateNodeInt(rp.c, alignment / 8);
|
||||
_ = try appendToken(rp.c, .RParen, ")");
|
||||
|
||||
break :blk expr;
|
||||
}
|
||||
break :blk null;
|
||||
};
|
||||
|
||||
const field_node = try c.a().create(ast.Node.ContainerField);
|
||||
field_node.* = .{
|
||||
.doc_comments = null,
|
||||
@ -834,7 +848,7 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?*
|
||||
.name_token = field_name,
|
||||
.type_expr = field_type,
|
||||
.value_expr = null,
|
||||
.align_expr = null,
|
||||
.align_expr = align_expr,
|
||||
};
|
||||
|
||||
if (is_anon) {
|
||||
@ -4607,7 +4621,7 @@ fn finishTransFnProto(
|
||||
if (fn_decl) |decl| {
|
||||
const alignment = ZigClangFunctionDecl_getAlignedAttribute(decl, rp.c.clang_context);
|
||||
if (alignment != 0) {
|
||||
_ = try appendToken(rp.c, .Keyword_linksection, "align");
|
||||
_ = try appendToken(rp.c, .Keyword_align, "align");
|
||||
_ = try appendToken(rp.c, .LParen, "(");
|
||||
// Clang reports the alignment in bits
|
||||
const expr = try transCreateNodeInt(rp.c, alignment / 8);
|
||||
@ -4857,7 +4871,7 @@ fn transPreprocessorEntities(c: *Context, unit: *ZigClangASTUnit) Error!void {
|
||||
const name = try c.str(raw_name);
|
||||
// TODO https://github.com/ziglang/zig/issues/3756
|
||||
// TODO https://github.com/ziglang/zig/issues/1802
|
||||
const mangled_name = if (isZigPrimitiveType(name)) try std.fmt.allocPrint(c.a(), "_{}", .{name}) else name;
|
||||
const mangled_name = if (isZigPrimitiveType(name)) try std.fmt.allocPrint(c.a(), "{}_{}", .{name, c.getMangle()}) else name;
|
||||
if (scope.containsNow(mangled_name)) {
|
||||
continue;
|
||||
}
|
||||
@ -5437,7 +5451,7 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
|
||||
|
||||
//if (@typeInfo(@TypeOf(x)) == .Pointer)
|
||||
// @ptrCast(dest, x)
|
||||
//else if (@typeInfo(@TypeOf(x)) == .Integer)
|
||||
//else if (@typeInfo(@TypeOf(x)) == .Int and @typeInfo(dest) == .Pointer)
|
||||
// @intToPtr(dest, x)
|
||||
//else
|
||||
// @as(dest, x)
|
||||
@ -5487,6 +5501,25 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
|
||||
.rhs = try transCreateNodeEnumLiteral(c, "Int"),
|
||||
};
|
||||
if_2.condition = &cmp_2.base;
|
||||
const cmp_4 = try c.a().create(ast.Node.InfixOp);
|
||||
cmp_4.* = .{
|
||||
.op_token = try appendToken(c, .Keyword_and, "and"),
|
||||
.lhs = &cmp_2.base,
|
||||
.op = .BoolAnd,
|
||||
.rhs = undefined,
|
||||
};
|
||||
const type_id_3 = try transCreateNodeBuiltinFnCall(c, "@typeInfo");
|
||||
try type_id_3.params.push(inner_node);
|
||||
type_id_3.rparen_token = try appendToken(c, .LParen, ")");
|
||||
const cmp_3 = try c.a().create(ast.Node.InfixOp);
|
||||
cmp_3.* = .{
|
||||
.op_token = try appendToken(c, .EqualEqual, "=="),
|
||||
.lhs = &type_id_3.base,
|
||||
.op = .EqualEqual,
|
||||
.rhs = try transCreateNodeEnumLiteral(c, "Pointer"),
|
||||
};
|
||||
cmp_4.rhs = &cmp_3.base;
|
||||
if_2.condition = &cmp_4.base;
|
||||
else_1.body = &if_2.base;
|
||||
_ = try appendToken(c, .RParen, ")");
|
||||
|
||||
|
||||
@ -1615,6 +1615,16 @@ unsigned ZigClangVarDecl_getAlignedAttribute(const struct ZigClangVarDecl *self,
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned ZigClangFieldDecl_getAlignedAttribute(const struct ZigClangFieldDecl *self, const ZigClangASTContext* ctx) {
|
||||
auto casted_self = reinterpret_cast<const clang::FieldDecl *>(self);
|
||||
auto casted_ctx = const_cast<clang::ASTContext *>(reinterpret_cast<const clang::ASTContext *>(ctx));
|
||||
if (const clang::AlignedAttr *AA = casted_self->getAttr<clang::AlignedAttr>()) {
|
||||
return AA->getAlignment(*casted_ctx);
|
||||
}
|
||||
// Zero means no explicit alignment factor was specified
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned ZigClangFunctionDecl_getAlignedAttribute(const struct ZigClangFunctionDecl *self, const ZigClangASTContext* ctx) {
|
||||
auto casted_self = reinterpret_cast<const clang::FunctionDecl *>(self);
|
||||
auto casted_ctx = const_cast<clang::ASTContext *>(reinterpret_cast<const clang::ASTContext *>(ctx));
|
||||
|
||||
@ -865,6 +865,7 @@ ZIG_EXTERN_C const struct ZigClangVarDecl *ZigClangVarDecl_getCanonicalDecl(cons
|
||||
ZIG_EXTERN_C const char* ZigClangVarDecl_getSectionAttribute(const struct ZigClangVarDecl *self, size_t *len);
|
||||
ZIG_EXTERN_C unsigned ZigClangVarDecl_getAlignedAttribute(const struct ZigClangVarDecl *self, const ZigClangASTContext* ctx);
|
||||
ZIG_EXTERN_C unsigned ZigClangFunctionDecl_getAlignedAttribute(const struct ZigClangFunctionDecl *self, const ZigClangASTContext* ctx);
|
||||
ZIG_EXTERN_C unsigned ZigClangFieldDecl_getAlignedAttribute(const struct ZigClangFieldDecl *self, const ZigClangASTContext* ctx);
|
||||
|
||||
ZIG_EXTERN_C struct ZigClangQualType ZigClangParmVarDecl_getOriginalType(const struct ZigClangParmVarDecl *self);
|
||||
|
||||
|
||||
@ -3,6 +3,16 @@ const std = @import("std");
|
||||
const CrossTarget = std.zig.CrossTarget;
|
||||
|
||||
pub fn addCases(cases: *tests.TranslateCContext) void {
|
||||
cases.add("struct with aligned fields",
|
||||
\\struct foo {
|
||||
\\ __attribute__((aligned(1))) short bar;
|
||||
\\};
|
||||
, &[_][]const u8{
|
||||
\\pub const struct_foo = extern struct {
|
||||
\\ bar: c_short align(1),
|
||||
\\};
|
||||
});
|
||||
|
||||
cases.add("structs with VLAs are rejected",
|
||||
\\struct foo { int x; int y[]; };
|
||||
\\struct bar { int x; int y[0]; };
|
||||
@ -1429,7 +1439,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
|
||||
cases.add("macro pointer cast",
|
||||
\\#define NRF_GPIO ((NRF_GPIO_Type *) NRF_GPIO_BASE)
|
||||
, &[_][]const u8{
|
||||
\\pub const NRF_GPIO = (if (@typeInfo(@TypeOf(NRF_GPIO_BASE)) == .Pointer) @ptrCast([*c]NRF_GPIO_Type, NRF_GPIO_BASE) else if (@typeInfo(@TypeOf(NRF_GPIO_BASE)) == .Int) @intToPtr([*c]NRF_GPIO_Type, NRF_GPIO_BASE) else @as([*c]NRF_GPIO_Type, NRF_GPIO_BASE));
|
||||
\\pub const NRF_GPIO = (if (@typeInfo(@TypeOf(NRF_GPIO_BASE)) == .Pointer) @ptrCast([*c]NRF_GPIO_Type, NRF_GPIO_BASE) else if (@typeInfo(@TypeOf(NRF_GPIO_BASE)) == .Int and @typeInfo([*c]NRF_GPIO_Type) == .Pointer) @intToPtr([*c]NRF_GPIO_Type, NRF_GPIO_BASE) else @as([*c]NRF_GPIO_Type, NRF_GPIO_BASE));
|
||||
});
|
||||
|
||||
cases.add("basic macro function",
|
||||
@ -1601,7 +1611,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
|
||||
cases.add("shadowing primitive types",
|
||||
\\unsigned anyerror = 2;
|
||||
, &[_][]const u8{
|
||||
\\pub export var _anyerror: c_uint = @bitCast(c_uint, @as(c_int, 2));
|
||||
\\pub export var anyerror_1: c_uint = @bitCast(c_uint, @as(c_int, 2));
|
||||
});
|
||||
|
||||
cases.add("floats",
|
||||
@ -2613,11 +2623,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
|
||||
\\#define FOO(bar) baz((void *)(baz))
|
||||
\\#define BAR (void*) a
|
||||
, &[_][]const u8{
|
||||
\\pub inline fn FOO(bar: var) @TypeOf(baz((if (@typeInfo(@TypeOf(baz)) == .Pointer) @ptrCast(*c_void, baz) else if (@typeInfo(@TypeOf(baz)) == .Int) @intToPtr(*c_void, baz) else @as(*c_void, baz)))) {
|
||||
\\ return baz((if (@typeInfo(@TypeOf(baz)) == .Pointer) @ptrCast(*c_void, baz) else if (@typeInfo(@TypeOf(baz)) == .Int) @intToPtr(*c_void, baz) else @as(*c_void, baz)));
|
||||
\\pub inline fn FOO(bar: var) @TypeOf(baz((if (@typeInfo(@TypeOf(baz)) == .Pointer) @ptrCast(*c_void, baz) else if (@typeInfo(@TypeOf(baz)) == .Int and @typeInfo(*c_void) == .Pointer) @intToPtr(*c_void, baz) else @as(*c_void, baz)))) {
|
||||
\\ return baz((if (@typeInfo(@TypeOf(baz)) == .Pointer) @ptrCast(*c_void, baz) else if (@typeInfo(@TypeOf(baz)) == .Int and @typeInfo(*c_void) == .Pointer) @intToPtr(*c_void, baz) else @as(*c_void, baz)));
|
||||
\\}
|
||||
,
|
||||
\\pub const BAR = (if (@typeInfo(@TypeOf(a)) == .Pointer) @ptrCast(*c_void, a) else if (@typeInfo(@TypeOf(a)) == .Int) @intToPtr(*c_void, a) else @as(*c_void, a));
|
||||
\\pub const BAR = (if (@typeInfo(@TypeOf(a)) == .Pointer) @ptrCast(*c_void, a) else if (@typeInfo(@TypeOf(a)) == .Int and @typeInfo(*c_void) == .Pointer) @intToPtr(*c_void, a) else @as(*c_void, a));
|
||||
});
|
||||
|
||||
cases.add("macro conditional operator",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user