authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-02-20 13:32:07+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-02-20 13:32:07+02:00
log4074e79748ad9ecc39a4127cd1c28c115efff56a
tree4cbf2a7ba6e12926aa9be0e16a2ff8819ee82bcc
parent669c2054a83cac594e5bbca4a7ca677e3b3a1a0d
signaturelock-open Commit is signed but in an unrecognized format.

translate-c: use global scope for typedef/record/enum type translation if needed

If the type is a reference to a global declaration that has not yet been translated we need to use the global scope for translation so that other functions can also reference it.

3 files changed, 37 insertions(+), 5 deletions(-)

src/clang.zig-2
...@@ -697,8 +697,6 @@ pub const ReturnStmt = opaque {...@@ -697,8 +697,6 @@ pub const ReturnStmt = opaque {
697 extern fn ZigClangReturnStmt_getRetValue(*const ReturnStmt) ?*const Expr;697 extern fn ZigClangReturnStmt_getRetValue(*const ReturnStmt) ?*const Expr;
698};698};
699699
700pub const SkipFunctionBodiesScope = opaque {};
701
702pub const SourceManager = opaque {700pub const SourceManager = opaque {
703 pub const getSpellingLoc = ZigClangSourceManager_getSpellingLoc;701 pub const getSpellingLoc = ZigClangSourceManager_getSpellingLoc;
704 extern fn ZigClangSourceManager_getSpellingLoc(*const SourceManager, Loc: SourceLocation) SourceLocation;702 extern fn ZigClangSourceManager_getSpellingLoc(*const SourceManager, Loc: SourceLocation) SourceLocation;
src/translate_c.zig+18-3
...@@ -3741,7 +3741,12 @@ fn transType(c: *Context, scope: *Scope, ty: *const clang.Type, source_loc: clan...@@ -3741,7 +3741,12 @@ fn transType(c: *Context, scope: *Scope, ty: *const clang.Type, source_loc: clan
3741 const typedef_ty = @ptrCast(*const clang.TypedefType, ty);3741 const typedef_ty = @ptrCast(*const clang.TypedefType, ty);
37423742
3743 const typedef_decl = typedef_ty.getDecl();3743 const typedef_decl = typedef_ty.getDecl();
3744 try transTypeDef(c, scope, typedef_decl);3744 var trans_scope = scope;
3745 if (@ptrCast(*const clang.Decl, typedef_decl).castToNamedDecl()) |named_decl| {
3746 const decl_name = try c.str(named_decl.getName_bytes_begin());
3747 if (c.global_names.get(decl_name)) |_| trans_scope = &c.global_scope.base;
3748 }
3749 try transTypeDef(c, trans_scope, typedef_decl);
3745 const name = c.decl_table.get(@ptrToInt(typedef_decl.getCanonicalDecl())).?;3750 const name = c.decl_table.get(@ptrToInt(typedef_decl.getCanonicalDecl())).?;
3746 return Tag.identifier.create(c.arena, name);3751 return Tag.identifier.create(c.arena, name);
3747 },3752 },
...@@ -3749,7 +3754,12 @@ fn transType(c: *Context, scope: *Scope, ty: *const clang.Type, source_loc: clan...@@ -3749,7 +3754,12 @@ fn transType(c: *Context, scope: *Scope, ty: *const clang.Type, source_loc: clan
3749 const record_ty = @ptrCast(*const clang.RecordType, ty);3754 const record_ty = @ptrCast(*const clang.RecordType, ty);
37503755
3751 const record_decl = record_ty.getDecl();3756 const record_decl = record_ty.getDecl();
3752 try transRecordDecl(c, scope, record_decl);3757 var trans_scope = scope;
3758 if (@ptrCast(*const clang.Decl, record_decl).castToNamedDecl()) |named_decl| {
3759 const decl_name = try c.str(named_decl.getName_bytes_begin());
3760 if (c.global_names.get(decl_name)) |_| trans_scope = &c.global_scope.base;
3761 }
3762 try transRecordDecl(c, trans_scope, record_decl);
3753 const name = c.decl_table.get(@ptrToInt(record_decl.getCanonicalDecl())).?;3763 const name = c.decl_table.get(@ptrToInt(record_decl.getCanonicalDecl())).?;
3754 return Tag.identifier.create(c.arena, name);3764 return Tag.identifier.create(c.arena, name);
3755 },3765 },
...@@ -3757,7 +3767,12 @@ fn transType(c: *Context, scope: *Scope, ty: *const clang.Type, source_loc: clan...@@ -3757,7 +3767,12 @@ fn transType(c: *Context, scope: *Scope, ty: *const clang.Type, source_loc: clan
3757 const enum_ty = @ptrCast(*const clang.EnumType, ty);3767 const enum_ty = @ptrCast(*const clang.EnumType, ty);
37583768
3759 const enum_decl = enum_ty.getDecl();3769 const enum_decl = enum_ty.getDecl();
3760 try transEnumDecl(c, scope, enum_decl);3770 var trans_scope = scope;
3771 if (@ptrCast(*const clang.Decl, enum_decl).castToNamedDecl()) |named_decl| {
3772 const decl_name = try c.str(named_decl.getName_bytes_begin());
3773 if (c.global_names.get(decl_name)) |_| trans_scope = &c.global_scope.base;
3774 }
3775 try transEnumDecl(c, trans_scope, enum_decl);
3761 const name = c.decl_table.get(@ptrToInt(enum_decl.getCanonicalDecl())).?;3776 const name = c.decl_table.get(@ptrToInt(enum_decl.getCanonicalDecl())).?;
3762 return Tag.identifier.create(c.arena, name);3777 return Tag.identifier.create(c.arena, name);
3763 },3778 },
test/run_translated_c.zig+19
...@@ -3,6 +3,25 @@ const tests = @import("tests.zig");...@@ -3,6 +3,25 @@ const tests = @import("tests.zig");
3const nl = std.cstr.line_sep;3const nl = std.cstr.line_sep;
44
5pub fn addCases(cases: *tests.RunTranslatedCContext) void {5pub fn addCases(cases: *tests.RunTranslatedCContext) void {
6 cases.add("use global scope for record/enum/typedef type transalation if needed",
7 \\void bar(void);
8 \\void baz(void);
9 \\struct foo { int x; };
10 \\void bar() {
11 \\ struct foo tmp;
12 \\}
13 \\
14 \\void baz() {
15 \\ struct foo tmp;
16 \\}
17 \\
18 \\int main(void) {
19 \\ bar();
20 \\ baz();
21 \\ return 0;
22 \\}
23 , "");
24
6 cases.add("failed macros are only declared once",25 cases.add("failed macros are only declared once",
7 \\#define FOO =26 \\#define FOO =
8 \\#define FOO =27 \\#define FOO =