| author | |
| committer | |
| log | a6f7a9ce2b008ff66ab32c8162d64dd77ffe952f |
| tree | 7e0f1e80cabf9e2869949035aa5ff95e06749d4c |
| parent | d065f297ab6de5ca35636d169195ce5da6235786 |
| signature | Commit is signed but in an unrecognized format. |
See #19647 files changed, 320 insertions(+), 123 deletions(-)
src-self-hosted/clang.zig+13-2| ... | @@ -56,7 +56,6 @@ pub const struct_ZigClangSkipFunctionBodiesScope = @OpaqueType(); | ... | @@ -56,7 +56,6 @@ pub const struct_ZigClangSkipFunctionBodiesScope = @OpaqueType(); |
| 56 | pub const struct_ZigClangSourceManager = @OpaqueType(); | 56 | pub const struct_ZigClangSourceManager = @OpaqueType(); |
| 57 | pub const struct_ZigClangSourceRange = @OpaqueType(); | 57 | pub const struct_ZigClangSourceRange = @OpaqueType(); |
| 58 | pub const struct_ZigClangStmt = @OpaqueType(); | 58 | pub const struct_ZigClangStmt = @OpaqueType(); |
| 59 | pub const struct_ZigClangStorageClass = @OpaqueType(); | ||
| 60 | pub const struct_ZigClangStringLiteral = @OpaqueType(); | 59 | pub const struct_ZigClangStringLiteral = @OpaqueType(); |
| 61 | pub const struct_ZigClangStringRef = @OpaqueType(); | 60 | pub const struct_ZigClangStringRef = @OpaqueType(); |
| 62 | pub const struct_ZigClangSwitchStmt = @OpaqueType(); | 61 | pub const struct_ZigClangSwitchStmt = @OpaqueType(); |
| ... | @@ -513,6 +512,10 @@ pub extern fn ZigClangASTUnit_delete(arg0: ?*struct_ZigClangASTUnit) void; | ... | @@ -513,6 +512,10 @@ pub extern fn ZigClangASTUnit_delete(arg0: ?*struct_ZigClangASTUnit) void; |
| 513 | 512 | ||
| 514 | pub extern fn ZigClangFunctionDecl_getType(self: *const struct_ZigClangFunctionDecl) struct_ZigClangQualType; | 513 | pub extern fn ZigClangFunctionDecl_getType(self: *const struct_ZigClangFunctionDecl) struct_ZigClangQualType; |
| 515 | pub extern fn ZigClangFunctionDecl_getLocation(self: *const struct_ZigClangFunctionDecl) struct_ZigClangSourceLocation; | 514 | pub extern fn ZigClangFunctionDecl_getLocation(self: *const struct_ZigClangFunctionDecl) struct_ZigClangSourceLocation; |
| 515 | pub extern fn ZigClangFunctionDecl_hasBody(self: *const struct_ZigClangFunctionDecl) bool; | ||
| 516 | pub extern fn ZigClangFunctionDecl_getStorageClass(self: *const struct_ZigClangFunctionDecl) ZigClangStorageClass; | ||
| 517 | pub extern fn ZigClangFunctionDecl_getParamDecl(self: *const struct_ZigClangFunctionDecl, i: c_uint) *const struct_ZigClangParmVarDecl; | ||
| 518 | |||
| 516 | pub extern fn ZigClangBuiltinType_getKind(self: *const struct_ZigClangBuiltinType) ZigClangBuiltinTypeKind; | 519 | pub extern fn ZigClangBuiltinType_getKind(self: *const struct_ZigClangBuiltinType) ZigClangBuiltinTypeKind; |
| 517 | 520 | ||
| 518 | pub extern fn ZigClangFunctionType_getNoReturnAttr(self: *const ZigClangFunctionType) bool; | 521 | pub extern fn ZigClangFunctionType_getNoReturnAttr(self: *const ZigClangFunctionType) bool; |
| ... | @@ -584,7 +587,6 @@ pub const ZigClangSkipFunctionBodiesScope = struct_ZigClangSkipFunctionBodiesSco | ... | @@ -584,7 +587,6 @@ pub const ZigClangSkipFunctionBodiesScope = struct_ZigClangSkipFunctionBodiesSco |
| 584 | pub const ZigClangSourceManager = struct_ZigClangSourceManager; | 587 | pub const ZigClangSourceManager = struct_ZigClangSourceManager; |
| 585 | pub const ZigClangSourceRange = struct_ZigClangSourceRange; | 588 | pub const ZigClangSourceRange = struct_ZigClangSourceRange; |
| 586 | pub const ZigClangStmt = struct_ZigClangStmt; | 589 | pub const ZigClangStmt = struct_ZigClangStmt; |
| 587 | pub const ZigClangStorageClass = struct_ZigClangStorageClass; | ||
| 588 | pub const ZigClangStringLiteral = struct_ZigClangStringLiteral; | 590 | pub const ZigClangStringLiteral = struct_ZigClangStringLiteral; |
| 589 | pub const ZigClangStringRef = struct_ZigClangStringRef; | 591 | pub const ZigClangStringRef = struct_ZigClangStringRef; |
| 590 | pub const ZigClangSwitchStmt = struct_ZigClangSwitchStmt; | 592 | pub const ZigClangSwitchStmt = struct_ZigClangSwitchStmt; |
| ... | @@ -849,3 +851,12 @@ pub const ZigClangCallingConv = extern enum { | ... | @@ -849,3 +851,12 @@ pub const ZigClangCallingConv = extern enum { |
| 849 | PreserveAll, | 851 | PreserveAll, |
| 850 | AArch64VectorCall, | 852 | AArch64VectorCall, |
| 851 | }; | 853 | }; |
| 854 | |||
| 855 | pub const ZigClangStorageClass = extern enum { | ||
| 856 | None, | ||
| 857 | Extern, | ||
| 858 | Static, | ||
| 859 | PrivateExtern, | ||
| 860 | Auto, | ||
| 861 | Register, | ||
| 862 | }; |
src-self-hosted/translate_c.zig+207-109| ... | @@ -2,6 +2,7 @@ | ... | @@ -2,6 +2,7 @@ |
| 2 | // and stage2. Currently the only way it is used is with `zig translate-c-2`. | 2 | // and stage2. Currently the only way it is used is with `zig translate-c-2`. |
| 3 | 3 | ||
| 4 | const std = @import("std"); | 4 | const std = @import("std"); |
| 5 | const builtin = @import("builtin"); | ||
| 5 | const ast = std.zig.ast; | 6 | const ast = std.zig.ast; |
| 6 | const Token = std.zig.Token; | 7 | const Token = std.zig.Token; |
| 7 | use @import("clang.zig"); | 8 | use @import("clang.zig"); |
| ... | @@ -12,13 +13,7 @@ pub const Mode = enum { | ... | @@ -12,13 +13,7 @@ pub const Mode = enum { |
| 12 | }; | 13 | }; |
| 13 | 14 | ||
| 14 | // TODO merge with Type.Fn.CallingConvention | 15 | // TODO merge with Type.Fn.CallingConvention |
| 15 | pub const CallingConvention = enum { | 16 | const CallingConvention = builtin.TypeInfo.CallingConvention; |
| 16 | auto, | ||
| 17 | c, | ||
| 18 | cold, | ||
| 19 | naked, | ||
| 20 | stdcall, | ||
| 21 | }; | ||
| 22 | 17 | ||
| 23 | pub const ClangErrMsg = Stage2ErrorMsg; | 18 | pub const ClangErrMsg = Stage2ErrorMsg; |
| 24 | 19 | ||
| ... | @@ -27,11 +22,64 @@ pub const Error = error{ | ... | @@ -27,11 +22,64 @@ pub const Error = error{ |
| 27 | UnsupportedType, | 22 | UnsupportedType, |
| 28 | }; | 23 | }; |
| 29 | 24 | ||
| 25 | const DeclTable = std.HashMap(usize, void, addrHash, addrEql); | ||
| 26 | |||
| 27 | fn addrHash(x: usize) u32 { | ||
| 28 | switch (@typeInfo(usize).Int.bits) { | ||
| 29 | 32 => return x, | ||
| 30 | // pointers are usually aligned so we ignore the bits that are probably all 0 anyway | ||
| 31 | // usually the larger bits of addr space are unused so we just chop em off | ||
| 32 | 64 => return @truncate(u32, x >> 4), | ||
| 33 | else => @compileError("unreachable"), | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | fn addrEql(a: usize, b: usize) bool { | ||
| 38 | return a == b; | ||
| 39 | } | ||
| 40 | |||
| 41 | const Scope = struct { | ||
| 42 | id: Id, | ||
| 43 | parent: ?*Scope, | ||
| 44 | |||
| 45 | const Id = enum { | ||
| 46 | Switch, | ||
| 47 | Var, | ||
| 48 | Block, | ||
| 49 | Root, | ||
| 50 | While, | ||
| 51 | }; | ||
| 52 | const Switch = struct { | ||
| 53 | base: Scope, | ||
| 54 | }; | ||
| 55 | |||
| 56 | const Var = struct { | ||
| 57 | base: Scope, | ||
| 58 | c_name: []const u8, | ||
| 59 | zig_name: []const u8, | ||
| 60 | }; | ||
| 61 | |||
| 62 | const Block = struct { | ||
| 63 | base: Scope, | ||
| 64 | }; | ||
| 65 | |||
| 66 | const Root = struct { | ||
| 67 | base: Scope, | ||
| 68 | }; | ||
| 69 | |||
| 70 | const While = struct { | ||
| 71 | base: Scope, | ||
| 72 | }; | ||
| 73 | }; | ||
| 74 | |||
| 30 | const Context = struct { | 75 | const Context = struct { |
| 31 | tree: *ast.Tree, | 76 | tree: *ast.Tree, |
| 32 | source_buffer: *std.Buffer, | 77 | source_buffer: *std.Buffer, |
| 33 | err: Error, | 78 | err: Error, |
| 34 | source_manager: *ZigClangSourceManager, | 79 | source_manager: *ZigClangSourceManager, |
| 80 | decl_table: DeclTable, | ||
| 81 | global_scope: *Scope.Root, | ||
| 82 | mode: Mode, | ||
| 35 | 83 | ||
| 36 | fn a(c: *Context) *std.mem.Allocator { | 84 | fn a(c: *Context) *std.mem.Allocator { |
| 37 | return &c.tree.arena_allocator.allocator; | 85 | return &c.tree.arena_allocator.allocator; |
| ... | @@ -76,7 +124,7 @@ pub fn translate( | ... | @@ -76,7 +124,7 @@ pub fn translate( |
| 76 | 124 | ||
| 77 | var tree_arena = std.heap.ArenaAllocator.init(backing_allocator); | 125 | var tree_arena = std.heap.ArenaAllocator.init(backing_allocator); |
| 78 | errdefer tree_arena.deinit(); | 126 | errdefer tree_arena.deinit(); |
| 79 | const arena = &tree_arena.allocator; | 127 | var arena = &tree_arena.allocator; |
| 80 | 128 | ||
| 81 | const root_node = try arena.create(ast.Node.Root); | 129 | const root_node = try arena.create(ast.Node.Root); |
| 82 | root_node.* = ast.Node.Root{ | 130 | root_node.* = ast.Node.Root{ |
| ... | @@ -96,14 +144,24 @@ pub fn translate( | ... | @@ -96,14 +144,24 @@ pub fn translate( |
| 96 | .errors = ast.Tree.ErrorList.init(arena), | 144 | .errors = ast.Tree.ErrorList.init(arena), |
| 97 | }; | 145 | }; |
| 98 | tree.arena_allocator = tree_arena; | 146 | tree.arena_allocator = tree_arena; |
| 147 | arena = &tree.arena_allocator.allocator; | ||
| 99 | 148 | ||
| 100 | var source_buffer = try std.Buffer.initSize(&tree.arena_allocator.allocator, 0); | 149 | var source_buffer = try std.Buffer.initSize(arena, 0); |
| 101 | 150 | ||
| 102 | var context = Context{ | 151 | var context = Context{ |
| 103 | .tree = tree, | 152 | .tree = tree, |
| 104 | .source_buffer = &source_buffer, | 153 | .source_buffer = &source_buffer, |
| 105 | .source_manager = ZigClangASTUnit_getSourceManager(ast_unit), | 154 | .source_manager = ZigClangASTUnit_getSourceManager(ast_unit), |
| 106 | .err = undefined, | 155 | .err = undefined, |
| 156 | .decl_table = DeclTable.init(arena), | ||
| 157 | .global_scope = try arena.create(Scope.Root), | ||
| 158 | .mode = mode, | ||
| 159 | }; | ||
| 160 | context.global_scope.* = Scope.Root{ | ||
| 161 | .base = Scope{ | ||
| 162 | .id = Scope.Id.Root, | ||
| 163 | .parent = null, | ||
| 164 | }, | ||
| 107 | }; | 165 | }; |
| 108 | 166 | ||
| 109 | if (!ZigClangASTUnit_visitLocalTopLevelDecls(ast_unit, &context, declVisitorC)) { | 167 | if (!ZigClangASTUnit_visitLocalTopLevelDecls(ast_unit, &context, declVisitorC)) { |
| ... | @@ -149,27 +207,39 @@ fn declVisitor(c: *Context, decl: *const ZigClangDecl) Error!void { | ... | @@ -149,27 +207,39 @@ fn declVisitor(c: *Context, decl: *const ZigClangDecl) Error!void { |
| 149 | } | 207 | } |
| 150 | 208 | ||
| 151 | fn visitFnDecl(c: *Context, fn_decl: *const ZigClangFunctionDecl) Error!void { | 209 | fn visitFnDecl(c: *Context, fn_decl: *const ZigClangFunctionDecl) Error!void { |
| 152 | const fn_name = try c.str(ZigClangDecl_getName_bytes_begin(@ptrCast(*const ZigClangDecl, fn_decl))); | 210 | if (try c.decl_table.put(@ptrToInt(fn_decl), {})) |_| return; // Avoid processing this decl twice |
| 153 | |||
| 154 | // TODO The C++ code has this: | ||
| 155 | //if (get_global(c, fn_name)) { | ||
| 156 | // // we already saw this function | ||
| 157 | // return; | ||
| 158 | //} | ||
| 159 | 211 | ||
| 212 | const fn_name = try c.str(ZigClangDecl_getName_bytes_begin(@ptrCast(*const ZigClangDecl, fn_decl))); | ||
| 160 | const fn_decl_loc = ZigClangFunctionDecl_getLocation(fn_decl); | 213 | const fn_decl_loc = ZigClangFunctionDecl_getLocation(fn_decl); |
| 161 | const proto_node = transQualType(c, ZigClangFunctionDecl_getType(fn_decl), fn_decl_loc) catch |e| switch (e) { | 214 | const fn_qt = ZigClangFunctionDecl_getType(fn_decl); |
| 162 | error.UnsupportedType => { | 215 | const fn_type = ZigClangQualType_getTypePtr(fn_qt); |
| 163 | try failDecl(c, fn_decl_loc, fn_name, "unable to resolve prototype of function"); | 216 | const proto_node = switch (ZigClangType_getTypeClass(fn_type)) { |
| 164 | return; | 217 | .FunctionProto => transFnProto( |
| 218 | c, | ||
| 219 | @ptrCast(*const ZigClangFunctionProtoType, fn_type), | ||
| 220 | fn_decl_loc, | ||
| 221 | fn_decl, | ||
| 222 | fn_name, | ||
| 223 | ) catch |err| switch (err) { | ||
| 224 | error.UnsupportedType => { | ||
| 225 | return failDecl(c, fn_decl_loc, fn_name, "unable to resolve prototype of function"); | ||
| 226 | }, | ||
| 227 | else => return err, | ||
| 165 | }, | 228 | }, |
| 166 | else => return e, | 229 | .FunctionNoProto => return failDecl(c, fn_decl_loc, fn_name, "TODO support functions with no prototype"), |
| 230 | else => unreachable, | ||
| 167 | }; | 231 | }; |
| 168 | const semi_tok = try appendToken(c, .Semicolon, ";"); | ||
| 169 | 232 | ||
| 170 | try emitWarning(c, fn_decl_loc, "TODO implement more translate-c for function decls"); | 233 | if (!ZigClangFunctionDecl_hasBody(fn_decl)) { |
| 234 | const semi_tok = try appendToken(c, .Semicolon, ";"); | ||
| 235 | return addTopLevelDecl(c, fn_name, &proto_node.base); | ||
| 236 | } | ||
| 237 | |||
| 238 | try emitWarning(c, fn_decl_loc, "TODO implement function body translation"); | ||
| 239 | } | ||
| 171 | 240 | ||
| 172 | try c.tree.root_node.decls.push(proto_node); | 241 | fn addTopLevelDecl(c: *Context, name: []const u8, decl_node: *ast.Node) !void { |
| 242 | try c.tree.root_node.decls.push(decl_node); | ||
| 173 | } | 243 | } |
| 174 | 244 | ||
| 175 | fn transQualType(c: *Context, qt: ZigClangQualType, source_loc: ZigClangSourceLocation) !*ast.Node { | 245 | fn transQualType(c: *Context, qt: ZigClangQualType, source_loc: ZigClangSourceLocation) !*ast.Node { |
| ... | @@ -205,91 +275,7 @@ fn transType(c: *Context, ty: *const ZigClangType, source_loc: ZigClangSourceLoc | ... | @@ -205,91 +275,7 @@ fn transType(c: *Context, ty: *const ZigClangType, source_loc: ZigClangSourceLoc |
| 205 | else => return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported builtin type"), | 275 | else => return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported builtin type"), |
| 206 | } | 276 | } |
| 207 | }, | 277 | }, |
| 208 | .FunctionProto => { | 278 | .FunctionProto => return transFnProto(c, @ptrCast(*const ZigClangFunctionType, ty), source_loc, null, false), |
| 209 | const fn_ty = @ptrCast(*const ZigClangFunctionType, ty); | ||
| 210 | const cc = switch (ZigClangFunctionType_getCallConv(fn_ty)) { | ||
| 211 | .C => CallingConvention.c, | ||
| 212 | .X86StdCall => CallingConvention.stdcall, | ||
| 213 | .X86FastCall => return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported calling convention: x86 fastcall"), | ||
| 214 | .X86ThisCall => return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported calling convention: x86 thiscall"), | ||
| 215 | .X86VectorCall => return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported calling convention: x86 vectorcall"), | ||
| 216 | .X86Pascal => return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported calling convention: x86 pascal"), | ||
| 217 | .Win64 => return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported calling convention: win64"), | ||
| 218 | .X86_64SysV => return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported calling convention: x86 64sysv"), | ||
| 219 | .X86RegCall => return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported calling convention: x86 reg"), | ||
| 220 | .AAPCS => return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported calling convention: aapcs"), | ||
| 221 | .AAPCS_VFP => return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported calling convention: aapcs-vfp"), | ||
| 222 | .IntelOclBicc => return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported calling convention: intel_ocl_bicc"), | ||
| 223 | .SpirFunction => return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported calling convention: SPIR function"), | ||
| 224 | .OpenCLKernel => return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported calling convention: OpenCLKernel"), | ||
| 225 | .Swift => return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported calling convention: Swift"), | ||
| 226 | .PreserveMost => return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported calling convention: PreserveMost"), | ||
| 227 | .PreserveAll => return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported calling convention: PreserveAll"), | ||
| 228 | .AArch64VectorCall => return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported calling convention: AArch64VectorCall"), | ||
| 229 | }; | ||
| 230 | |||
| 231 | const fn_proto_ty = @ptrCast(*const ZigClangFunctionProtoType, ty); | ||
| 232 | const is_var_args = ZigClangFunctionProtoType_isVariadic(fn_proto_ty); | ||
| 233 | const param_count: usize = ZigClangFunctionProtoType_getNumParams(fn_proto_ty); | ||
| 234 | var i: usize = 0; | ||
| 235 | while (i < param_count) : (i += 1) { | ||
| 236 | return revertAndWarn(rp, error.UnsupportedType, source_loc, "TODO: implement parameters for FunctionProto in transType"); | ||
| 237 | } | ||
| 238 | // TODO check for always_inline attribute | ||
| 239 | // TODO check for align attribute | ||
| 240 | |||
| 241 | // extern fn (...) T | ||
| 242 | const cc_tok = if (cc == .stdcall) try appendToken(c, .Keyword_stdcallcc, "stdcallcc") else null; | ||
| 243 | const extern_tok = if (cc == .c) try appendToken(c, .Keyword_extern, "extern") else null; | ||
| 244 | const fn_tok = try appendToken(c, .Keyword_fn, "fn"); | ||
| 245 | const lparen_tok = try appendToken(c, .LParen, "("); | ||
| 246 | const var_args_tok = if (is_var_args) try appendToken(c, .Ellipsis3, "...") else null; | ||
| 247 | const rparen_tok = try appendToken(c, .RParen, ")"); | ||
| 248 | |||
| 249 | const return_type_node = blk: { | ||
| 250 | if (ZigClangFunctionType_getNoReturnAttr(fn_ty)) { | ||
| 251 | break :blk try appendIdentifier(c, "noreturn"); | ||
| 252 | } else { | ||
| 253 | return revertAndWarn(rp, error.UnsupportedType, source_loc, "TODO: non-noreturn FunctionProto return type"); | ||
| 254 | //proto_node->data.fn_proto.return_type = trans_qual_type(c, | ||
| 255 | // ZigClangFunctionType_getReturnType(fn_ty), source_loc); | ||
| 256 | //if (proto_node->data.fn_proto.return_type == nullptr) { | ||
| 257 | // emit_warning(c, source_loc, "unsupported function proto return type"); | ||
| 258 | // return nullptr; | ||
| 259 | //} | ||
| 260 | //// convert c_void to actual void (only for return type) | ||
| 261 | //// we do want to look at the AstNode instead of ZigClangQualType, because | ||
| 262 | //// if they do something like: | ||
| 263 | //// typedef Foo void; | ||
| 264 | //// void foo(void) -> Foo; | ||
| 265 | //// we want to keep the return type AST node. | ||
| 266 | //if (is_c_void_type(proto_node->data.fn_proto.return_type)) { | ||
| 267 | // proto_node->data.fn_proto.return_type = trans_create_node_symbol_str(c, "void"); | ||
| 268 | //} | ||
| 269 | } | ||
| 270 | }; | ||
| 271 | |||
| 272 | const fn_proto = try c.a().create(ast.Node.FnProto); | ||
| 273 | fn_proto.* = ast.Node.FnProto{ | ||
| 274 | .base = ast.Node{ .id = ast.Node.Id.FnProto }, | ||
| 275 | .doc_comments = null, | ||
| 276 | .visib_token = null, | ||
| 277 | .fn_token = fn_tok, | ||
| 278 | .name_token = null, | ||
| 279 | .params = ast.Node.FnProto.ParamList.init(c.a()), | ||
| 280 | .return_type = ast.Node.FnProto.ReturnType{ .Explicit = return_type_node }, | ||
| 281 | .var_args_token = var_args_tok, | ||
| 282 | .extern_export_inline_token = extern_tok, | ||
| 283 | .cc_token = cc_tok, | ||
| 284 | .async_attr = null, | ||
| 285 | .body_node = null, | ||
| 286 | .lib_name = null, | ||
| 287 | .align_expr = null, | ||
| 288 | .section_expr = null, | ||
| 289 | }; | ||
| 290 | return &fn_proto.base; | ||
| 291 | }, | ||
| 292 | |||
| 293 | else => { | 279 | else => { |
| 294 | const type_name = c.str(ZigClangType_getTypeClassName(ty)); | 280 | const type_name = c.str(ZigClangType_getTypeClassName(ty)); |
| 295 | return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported type: '{}'", type_name); | 281 | return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported type: '{}'", type_name); |
| ... | @@ -297,6 +283,118 @@ fn transType(c: *Context, ty: *const ZigClangType, source_loc: ZigClangSourceLoc | ... | @@ -297,6 +283,118 @@ fn transType(c: *Context, ty: *const ZigClangType, source_loc: ZigClangSourceLoc |
| 297 | } | 283 | } |
| 298 | } | 284 | } |
| 299 | 285 | ||
| 286 | fn transFnProto( | ||
| 287 | c: *Context, | ||
| 288 | fn_proto_ty: *const ZigClangFunctionProtoType, | ||
| 289 | source_loc: ZigClangSourceLocation, | ||
| 290 | opt_fn_decl: ?*const ZigClangFunctionDecl, | ||
| 291 | fn_name: ?[]const u8, | ||
| 292 | ) !*ast.Node.FnProto { | ||
| 293 | const fn_ty = @ptrCast(*const ZigClangFunctionType, fn_proto_ty); | ||
| 294 | const rp = makeRestorePoint(c); | ||
| 295 | const cc = switch (ZigClangFunctionType_getCallConv(fn_ty)) { | ||
| 296 | .C => CallingConvention.C, | ||
| 297 | .X86StdCall => CallingConvention.Stdcall, | ||
| 298 | .X86FastCall => return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported calling convention: x86 fastcall"), | ||
| 299 | .X86ThisCall => return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported calling convention: x86 thiscall"), | ||
| 300 | .X86VectorCall => return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported calling convention: x86 vectorcall"), | ||
| 301 | .X86Pascal => return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported calling convention: x86 pascal"), | ||
| 302 | .Win64 => return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported calling convention: win64"), | ||
| 303 | .X86_64SysV => return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported calling convention: x86 64sysv"), | ||
| 304 | .X86RegCall => return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported calling convention: x86 reg"), | ||
| 305 | .AAPCS => return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported calling convention: aapcs"), | ||
| 306 | .AAPCS_VFP => return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported calling convention: aapcs-vfp"), | ||
| 307 | .IntelOclBicc => return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported calling convention: intel_ocl_bicc"), | ||
| 308 | .SpirFunction => return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported calling convention: SPIR function"), | ||
| 309 | .OpenCLKernel => return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported calling convention: OpenCLKernel"), | ||
| 310 | .Swift => return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported calling convention: Swift"), | ||
| 311 | .PreserveMost => return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported calling convention: PreserveMost"), | ||
| 312 | .PreserveAll => return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported calling convention: PreserveAll"), | ||
| 313 | .AArch64VectorCall => return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported calling convention: AArch64VectorCall"), | ||
| 314 | }; | ||
| 315 | |||
| 316 | const is_var_args = ZigClangFunctionProtoType_isVariadic(fn_proto_ty); | ||
| 317 | const param_count: usize = ZigClangFunctionProtoType_getNumParams(fn_proto_ty); | ||
| 318 | var i: usize = 0; | ||
| 319 | while (i < param_count) : (i += 1) { | ||
| 320 | return revertAndWarn(rp, error.UnsupportedType, source_loc, "TODO: implement parameters for FunctionProto in transType"); | ||
| 321 | } | ||
| 322 | // TODO check for always_inline attribute | ||
| 323 | // TODO check for align attribute | ||
| 324 | |||
| 325 | // extern fn name(...) T | ||
| 326 | const cc_tok = if (cc == .Stdcall) try appendToken(c, .Keyword_stdcallcc, "stdcallcc") else null; | ||
| 327 | const is_export = exp: { | ||
| 328 | const fn_decl = opt_fn_decl orelse break :exp false; | ||
| 329 | const has_body = ZigClangFunctionDecl_hasBody(fn_decl); | ||
| 330 | const storage_class = ZigClangFunctionDecl_getStorageClass(fn_decl); | ||
| 331 | break :exp switch (storage_class) { | ||
| 332 | .None => switch (c.mode) { | ||
| 333 | .import => false, | ||
| 334 | .translate => has_body, | ||
| 335 | }, | ||
| 336 | .Extern, .Static => false, | ||
| 337 | .PrivateExtern => return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported storage class: private extern"), | ||
| 338 | .Auto => unreachable, // Not legal on functions | ||
| 339 | .Register => unreachable, // Not legal on functions | ||
| 340 | }; | ||
| 341 | }; | ||
| 342 | const extern_export_inline_tok = if (is_export) | ||
| 343 | try appendToken(c, .Keyword_export, "export") | ||
| 344 | else if (cc == .C) | ||
| 345 | try appendToken(c, .Keyword_extern, "extern") | ||
| 346 | else | ||
| 347 | null; | ||
| 348 | const fn_tok = try appendToken(c, .Keyword_fn, "fn"); | ||
| 349 | const name_tok = if (fn_name) |n| try appendToken(c, .Identifier, "{}", n) else null; | ||
| 350 | const lparen_tok = try appendToken(c, .LParen, "("); | ||
| 351 | const var_args_tok = if (is_var_args) try appendToken(c, .Ellipsis3, "...") else null; | ||
| 352 | const rparen_tok = try appendToken(c, .RParen, ")"); | ||
| 353 | |||
| 354 | const return_type_node = blk: { | ||
| 355 | if (ZigClangFunctionType_getNoReturnAttr(fn_ty)) { | ||
| 356 | break :blk try appendIdentifier(c, "noreturn"); | ||
| 357 | } else { | ||
| 358 | return revertAndWarn(rp, error.UnsupportedType, source_loc, "TODO: non-noreturn FunctionProto return type"); | ||
| 359 | //proto_node->data.fn_proto.return_type = trans_qual_type(c, | ||
| 360 | // ZigClangFunctionType_getReturnType(fn_ty), source_loc); | ||
| 361 | //if (proto_node->data.fn_proto.return_type == nullptr) { | ||
| 362 | // emit_warning(c, source_loc, "unsupported function proto return type"); | ||
| 363 | // return nullptr; | ||
| 364 | //} | ||
| 365 | //// convert c_void to actual void (only for return type) | ||
| 366 | //// we do want to look at the AstNode instead of ZigClangQualType, because | ||
| 367 | //// if they do something like: | ||
| 368 | //// typedef Foo void; | ||
| 369 | //// void foo(void) -> Foo; | ||
| 370 | //// we want to keep the return type AST node. | ||
| 371 | //if (is_c_void_type(proto_node->data.fn_proto.return_type)) { | ||
| 372 | // proto_node->data.fn_proto.return_type = trans_create_node_symbol_str(c, "void"); | ||
| 373 | //} | ||
| 374 | } | ||
| 375 | }; | ||
| 376 | |||
| 377 | const fn_proto = try c.a().create(ast.Node.FnProto); | ||
| 378 | fn_proto.* = ast.Node.FnProto{ | ||
| 379 | .base = ast.Node{ .id = ast.Node.Id.FnProto }, | ||
| 380 | .doc_comments = null, | ||
| 381 | .visib_token = null, | ||
| 382 | .fn_token = fn_tok, | ||
| 383 | .name_token = name_tok, | ||
| 384 | .params = ast.Node.FnProto.ParamList.init(c.a()), | ||
| 385 | .return_type = ast.Node.FnProto.ReturnType{ .Explicit = return_type_node }, | ||
| 386 | .var_args_token = var_args_tok, | ||
| 387 | .extern_export_inline_token = extern_export_inline_tok, | ||
| 388 | .cc_token = cc_tok, | ||
| 389 | .async_attr = null, | ||
| 390 | .body_node = null, | ||
| 391 | .lib_name = null, | ||
| 392 | .align_expr = null, | ||
| 393 | .section_expr = null, | ||
| 394 | }; | ||
| 395 | return fn_proto; | ||
| 396 | } | ||
| 397 | |||
| 300 | fn revertAndWarn( | 398 | fn revertAndWarn( |
| 301 | restore_point: RestorePoint, | 399 | restore_point: RestorePoint, |
| 302 | err: var, | 400 | err: var, |
src/translate_c.cpp+9-9| ... | @@ -4038,15 +4038,15 @@ static void visit_fn_decl(Context *c, const ZigClangFunctionDecl *fn_decl) { | ... | @@ -4038,15 +4038,15 @@ static void visit_fn_decl(Context *c, const ZigClangFunctionDecl *fn_decl) { |
| 4038 | } | 4038 | } |
| 4039 | 4039 | ||
| 4040 | proto_node->data.fn_proto.name = fn_name; | 4040 | proto_node->data.fn_proto.name = fn_name; |
| 4041 | proto_node->data.fn_proto.is_extern = !((const clang::FunctionDecl*)fn_decl)->hasBody(); | 4041 | proto_node->data.fn_proto.is_extern = !ZigClangFunctionDecl_hasBody(fn_decl); |
| 4042 | 4042 | ||
| 4043 | clang::StorageClass sc = ((const clang::FunctionDecl*)fn_decl)->getStorageClass(); | 4043 | ZigClangStorageClass sc = ZigClangFunctionDecl_getStorageClass(fn_decl); |
| 4044 | if (sc == clang::SC_None) { | 4044 | if (sc == ZigClangStorageClass_None) { |
| 4045 | proto_node->data.fn_proto.visib_mod = c->visib_mod; | 4045 | proto_node->data.fn_proto.visib_mod = c->visib_mod; |
| 4046 | proto_node->data.fn_proto.is_export = ((const clang::FunctionDecl*)fn_decl)->hasBody() ? c->want_export : false; | 4046 | proto_node->data.fn_proto.is_export = ZigClangFunctionDecl_hasBody(fn_decl) ? c->want_export : false; |
| 4047 | } else if (sc == clang::SC_Extern || sc == clang::SC_Static) { | 4047 | } else if (sc == ZigClangStorageClass_Extern || sc == ZigClangStorageClass_Static) { |
| 4048 | proto_node->data.fn_proto.visib_mod = c->visib_mod; | 4048 | proto_node->data.fn_proto.visib_mod = c->visib_mod; |
| 4049 | } else if (sc == clang::SC_PrivateExtern) { | 4049 | } else if (sc == ZigClangStorageClass_PrivateExtern) { |
| 4050 | emit_warning(c, ZigClangFunctionDecl_getLocation(fn_decl), "unsupported storage class: private extern"); | 4050 | emit_warning(c, ZigClangFunctionDecl_getLocation(fn_decl), "unsupported storage class: private extern"); |
| 4051 | return; | 4051 | return; |
| 4052 | } else { | 4052 | } else { |
| ... | @@ -4058,7 +4058,7 @@ static void visit_fn_decl(Context *c, const ZigClangFunctionDecl *fn_decl) { | ... | @@ -4058,7 +4058,7 @@ static void visit_fn_decl(Context *c, const ZigClangFunctionDecl *fn_decl) { |
| 4058 | 4058 | ||
| 4059 | for (size_t i = 0; i < proto_node->data.fn_proto.params.length; i += 1) { | 4059 | for (size_t i = 0; i < proto_node->data.fn_proto.params.length; i += 1) { |
| 4060 | AstNode *param_node = proto_node->data.fn_proto.params.at(i); | 4060 | AstNode *param_node = proto_node->data.fn_proto.params.at(i); |
| 4061 | const clang::ParmVarDecl *param = ((const clang::FunctionDecl*)fn_decl)->getParamDecl(i); | 4061 | const ZigClangParmVarDecl *param = ZigClangFunctionDecl_getParamDecl(fn_decl, i); |
| 4062 | const char *name = ZigClangDecl_getName_bytes_begin((const ZigClangDecl *)param); | 4062 | const char *name = ZigClangDecl_getName_bytes_begin((const ZigClangDecl *)param); |
| 4063 | 4063 | ||
| 4064 | Buf *proto_param_name; | 4064 | Buf *proto_param_name; |
| ... | @@ -4077,7 +4077,7 @@ static void visit_fn_decl(Context *c, const ZigClangFunctionDecl *fn_decl) { | ... | @@ -4077,7 +4077,7 @@ static void visit_fn_decl(Context *c, const ZigClangFunctionDecl *fn_decl) { |
| 4077 | param_node->data.param_decl.name = scope_var->zig_name; | 4077 | param_node->data.param_decl.name = scope_var->zig_name; |
| 4078 | } | 4078 | } |
| 4079 | 4079 | ||
| 4080 | if (!((const clang::FunctionDecl*)fn_decl)->hasBody()) { | 4080 | if (!ZigClangFunctionDecl_hasBody(fn_decl)) { |
| 4081 | // just a prototype | 4081 | // just a prototype |
| 4082 | add_top_level_decl(c, proto_node->data.fn_proto.name, proto_node); | 4082 | add_top_level_decl(c, proto_node->data.fn_proto.name, proto_node); |
| 4083 | return; | 4083 | return; |
| ... | @@ -4085,7 +4085,7 @@ static void visit_fn_decl(Context *c, const ZigClangFunctionDecl *fn_decl) { | ... | @@ -4085,7 +4085,7 @@ static void visit_fn_decl(Context *c, const ZigClangFunctionDecl *fn_decl) { |
| 4085 | 4085 | ||
| 4086 | // actual function definition with body | 4086 | // actual function definition with body |
| 4087 | c->ptr_params.clear(); | 4087 | c->ptr_params.clear(); |
| 4088 | const ZigClangStmt *body = bitcast(((const clang::FunctionDecl*)fn_decl)->getBody()); | 4088 | const ZigClangStmt *body = ZigClangFunctionDecl_getBody(fn_decl); |
| 4089 | AstNode *actual_body_node; | 4089 | AstNode *actual_body_node; |
| 4090 | TransScope *result_scope = trans_stmt(c, scope, body, &actual_body_node); | 4090 | TransScope *result_scope = trans_stmt(c, scope, body, &actual_body_node); |
| 4091 | if (result_scope == nullptr) { | 4091 | if (result_scope == nullptr) { |
src/zig_clang.cpp+43| ... | @@ -1236,6 +1236,25 @@ static_assert((clang::CallingConv)ZigClangCallingConv_PreserveMost == clang::CC_ | ... | @@ -1236,6 +1236,25 @@ static_assert((clang::CallingConv)ZigClangCallingConv_PreserveMost == clang::CC_ |
| 1236 | static_assert((clang::CallingConv)ZigClangCallingConv_PreserveAll == clang::CC_PreserveAll, ""); | 1236 | static_assert((clang::CallingConv)ZigClangCallingConv_PreserveAll == clang::CC_PreserveAll, ""); |
| 1237 | static_assert((clang::CallingConv)ZigClangCallingConv_AArch64VectorCall == clang::CC_AArch64VectorCall, ""); | 1237 | static_assert((clang::CallingConv)ZigClangCallingConv_AArch64VectorCall == clang::CC_AArch64VectorCall, ""); |
| 1238 | 1238 | ||
| 1239 | void ZigClang_detect_enum_StorageClass(clang::StorageClass x) { | ||
| 1240 | switch (x) { | ||
| 1241 | case clang::SC_None: | ||
| 1242 | case clang::SC_Extern: | ||
| 1243 | case clang::SC_Static: | ||
| 1244 | case clang::SC_PrivateExtern: | ||
| 1245 | case clang::SC_Auto: | ||
| 1246 | case clang::SC_Register: | ||
| 1247 | break; | ||
| 1248 | } | ||
| 1249 | } | ||
| 1250 | |||
| 1251 | static_assert((clang::StorageClass)ZigClangStorageClass_None == clang::SC_None, ""); | ||
| 1252 | static_assert((clang::StorageClass)ZigClangStorageClass_Extern == clang::SC_Extern, ""); | ||
| 1253 | static_assert((clang::StorageClass)ZigClangStorageClass_Static == clang::SC_Static, ""); | ||
| 1254 | static_assert((clang::StorageClass)ZigClangStorageClass_PrivateExtern == clang::SC_PrivateExtern, ""); | ||
| 1255 | static_assert((clang::StorageClass)ZigClangStorageClass_Auto == clang::SC_Auto, ""); | ||
| 1256 | static_assert((clang::StorageClass)ZigClangStorageClass_Register == clang::SC_Register, ""); | ||
| 1257 | |||
| 1239 | 1258 | ||
| 1240 | static_assert(sizeof(ZigClangSourceLocation) == sizeof(clang::SourceLocation), ""); | 1259 | static_assert(sizeof(ZigClangSourceLocation) == sizeof(clang::SourceLocation), ""); |
| 1241 | static ZigClangSourceLocation bitcast(clang::SourceLocation src) { | 1260 | static ZigClangSourceLocation bitcast(clang::SourceLocation src) { |
| ... | @@ -1434,6 +1453,30 @@ struct ZigClangSourceLocation ZigClangFunctionDecl_getLocation(const struct ZigC | ... | @@ -1434,6 +1453,30 @@ struct ZigClangSourceLocation ZigClangFunctionDecl_getLocation(const struct ZigC |
| 1434 | return bitcast(casted->getLocation()); | 1453 | return bitcast(casted->getLocation()); |
| 1435 | } | 1454 | } |
| 1436 | 1455 | ||
| 1456 | bool ZigClangFunctionDecl_hasBody(const struct ZigClangFunctionDecl *self) { | ||
| 1457 | auto casted = reinterpret_cast<const clang::FunctionDecl *>(self); | ||
| 1458 | return casted->hasBody(); | ||
| 1459 | } | ||
| 1460 | |||
| 1461 | enum ZigClangStorageClass ZigClangFunctionDecl_getStorageClass(const struct ZigClangFunctionDecl *self) { | ||
| 1462 | auto casted = reinterpret_cast<const clang::FunctionDecl *>(self); | ||
| 1463 | return (ZigClangStorageClass)casted->getStorageClass(); | ||
| 1464 | } | ||
| 1465 | |||
| 1466 | const struct ZigClangParmVarDecl *ZigClangFunctionDecl_getParamDecl(const struct ZigClangFunctionDecl *self, | ||
| 1467 | unsigned i) | ||
| 1468 | { | ||
| 1469 | auto casted = reinterpret_cast<const clang::FunctionDecl *>(self); | ||
| 1470 | const clang::ParmVarDecl *parm_var_decl = casted->getParamDecl(i); | ||
| 1471 | return reinterpret_cast<const ZigClangParmVarDecl *>(parm_var_decl); | ||
| 1472 | } | ||
| 1473 | |||
| 1474 | const struct ZigClangStmt *ZigClangFunctionDecl_getBody(const struct ZigClangFunctionDecl *self) { | ||
| 1475 | auto casted = reinterpret_cast<const clang::FunctionDecl *>(self); | ||
| 1476 | const clang::Stmt *stmt = casted->getBody(); | ||
| 1477 | return reinterpret_cast<const ZigClangStmt *>(stmt); | ||
| 1478 | } | ||
| 1479 | |||
| 1437 | const ZigClangTypedefNameDecl *ZigClangTypedefType_getDecl(const ZigClangTypedefType *self) { | 1480 | const ZigClangTypedefNameDecl *ZigClangTypedefType_getDecl(const ZigClangTypedefType *self) { |
| 1438 | auto casted = reinterpret_cast<const clang::TypedefType *>(self); | 1481 | auto casted = reinterpret_cast<const clang::TypedefType *>(self); |
| 1439 | const clang::TypedefNameDecl *name_decl = casted->getDecl(); | 1482 | const clang::TypedefNameDecl *name_decl = casted->getDecl(); |
src/zig_clang.h+16-1| ... | @@ -88,7 +88,6 @@ struct ZigClangSkipFunctionBodiesScope; | ... | @@ -88,7 +88,6 @@ struct ZigClangSkipFunctionBodiesScope; |
| 88 | struct ZigClangSourceManager; | 88 | struct ZigClangSourceManager; |
| 89 | struct ZigClangSourceRange; | 89 | struct ZigClangSourceRange; |
| 90 | struct ZigClangStmt; | 90 | struct ZigClangStmt; |
| 91 | struct ZigClangStorageClass; | ||
| 92 | struct ZigClangStringLiteral; | 91 | struct ZigClangStringLiteral; |
| 93 | struct ZigClangStringRef; | 92 | struct ZigClangStringRef; |
| 94 | struct ZigClangSwitchStmt; | 93 | struct ZigClangSwitchStmt; |
| ... | @@ -700,6 +699,18 @@ enum ZigClangCallingConv { | ... | @@ -700,6 +699,18 @@ enum ZigClangCallingConv { |
| 700 | ZigClangCallingConv_AArch64VectorCall, // __attribute__((aarch64_vector_pcs)) | 699 | ZigClangCallingConv_AArch64VectorCall, // __attribute__((aarch64_vector_pcs)) |
| 701 | }; | 700 | }; |
| 702 | 701 | ||
| 702 | enum ZigClangStorageClass { | ||
| 703 | // These are legal on both functions and variables. | ||
| 704 | ZigClangStorageClass_None, | ||
| 705 | ZigClangStorageClass_Extern, | ||
| 706 | ZigClangStorageClass_Static, | ||
| 707 | ZigClangStorageClass_PrivateExtern, | ||
| 708 | |||
| 709 | // These are only legal on variables. | ||
| 710 | ZigClangStorageClass_Auto, | ||
| 711 | ZigClangStorageClass_Register, | ||
| 712 | }; | ||
| 713 | |||
| 703 | ZIG_EXTERN_C struct ZigClangSourceLocation ZigClangSourceManager_getSpellingLoc(const struct ZigClangSourceManager *, | 714 | ZIG_EXTERN_C struct ZigClangSourceLocation ZigClangSourceManager_getSpellingLoc(const struct ZigClangSourceManager *, |
| 704 | struct ZigClangSourceLocation Loc); | 715 | struct ZigClangSourceLocation Loc); |
| 705 | ZIG_EXTERN_C const char *ZigClangSourceManager_getFilename(const struct ZigClangSourceManager *, | 716 | ZIG_EXTERN_C const char *ZigClangSourceManager_getFilename(const struct ZigClangSourceManager *, |
| ... | @@ -742,6 +753,10 @@ ZIG_EXTERN_C struct ZigClangSourceLocation ZigClangDecl_getLocation(const struct | ... | @@ -742,6 +753,10 @@ ZIG_EXTERN_C struct ZigClangSourceLocation ZigClangDecl_getLocation(const struct |
| 742 | 753 | ||
| 743 | ZIG_EXTERN_C struct ZigClangQualType ZigClangFunctionDecl_getType(const struct ZigClangFunctionDecl *); | 754 | ZIG_EXTERN_C struct ZigClangQualType ZigClangFunctionDecl_getType(const struct ZigClangFunctionDecl *); |
| 744 | ZIG_EXTERN_C struct ZigClangSourceLocation ZigClangFunctionDecl_getLocation(const struct ZigClangFunctionDecl *); | 755 | ZIG_EXTERN_C struct ZigClangSourceLocation ZigClangFunctionDecl_getLocation(const struct ZigClangFunctionDecl *); |
| 756 | ZIG_EXTERN_C bool ZigClangFunctionDecl_hasBody(const struct ZigClangFunctionDecl *); | ||
| 757 | ZIG_EXTERN_C enum ZigClangStorageClass ZigClangFunctionDecl_getStorageClass(const struct ZigClangFunctionDecl *); | ||
| 758 | ZIG_EXTERN_C const struct ZigClangParmVarDecl *ZigClangFunctionDecl_getParamDecl(const struct ZigClangFunctionDecl *, unsigned i); | ||
| 759 | ZIG_EXTERN_C const struct ZigClangStmt *ZigClangFunctionDecl_getBody(const struct ZigClangFunctionDecl *); | ||
| 745 | 760 | ||
| 746 | ZIG_EXTERN_C bool ZigClangRecordDecl_isUnion(const struct ZigClangRecordDecl *record_decl); | 761 | ZIG_EXTERN_C bool ZigClangRecordDecl_isUnion(const struct ZigClangRecordDecl *record_decl); |
| 747 | ZIG_EXTERN_C bool ZigClangRecordDecl_isStruct(const struct ZigClangRecordDecl *record_decl); | 762 | ZIG_EXTERN_C bool ZigClangRecordDecl_isStruct(const struct ZigClangRecordDecl *record_decl); |
test/tests.zig+26-2| ... | @@ -903,6 +903,7 @@ pub const TranslateCContext = struct { | ... | @@ -903,6 +903,7 @@ pub const TranslateCContext = struct { |
| 903 | sources: ArrayList(SourceFile), | 903 | sources: ArrayList(SourceFile), |
| 904 | expected_lines: ArrayList([]const u8), | 904 | expected_lines: ArrayList([]const u8), |
| 905 | allow_warnings: bool, | 905 | allow_warnings: bool, |
| 906 | stage2: bool, | ||
| 906 | 907 | ||
| 907 | const SourceFile = struct { | 908 | const SourceFile = struct { |
| 908 | filename: []const u8, | 909 | filename: []const u8, |
| ... | @@ -955,7 +956,8 @@ pub const TranslateCContext = struct { | ... | @@ -955,7 +956,8 @@ pub const TranslateCContext = struct { |
| 955 | var zig_args = ArrayList([]const u8).init(b.allocator); | 956 | var zig_args = ArrayList([]const u8).init(b.allocator); |
| 956 | zig_args.append(b.zig_exe) catch unreachable; | 957 | zig_args.append(b.zig_exe) catch unreachable; |
| 957 | 958 | ||
| 958 | zig_args.append("translate-c") catch unreachable; | 959 | const translate_c_cmd = if (self.case.stage2) "translate-c-2" else "translate-c"; |
| 960 | zig_args.append(translate_c_cmd) catch unreachable; | ||
| 959 | zig_args.append(b.pathFromRoot(root_src)) catch unreachable; | 961 | zig_args.append(b.pathFromRoot(root_src)) catch unreachable; |
| 960 | 962 | ||
| 961 | warn("Test {}/{} {}...", self.test_index + 1, self.context.test_index, self.name); | 963 | warn("Test {}/{} {}...", self.test_index + 1, self.context.test_index, self.name); |
| ... | @@ -1052,6 +1054,7 @@ pub const TranslateCContext = struct { | ... | @@ -1052,6 +1054,7 @@ pub const TranslateCContext = struct { |
| 1052 | .sources = ArrayList(TestCase.SourceFile).init(self.b.allocator), | 1054 | .sources = ArrayList(TestCase.SourceFile).init(self.b.allocator), |
| 1053 | .expected_lines = ArrayList([]const u8).init(self.b.allocator), | 1055 | .expected_lines = ArrayList([]const u8).init(self.b.allocator), |
| 1054 | .allow_warnings = allow_warnings, | 1056 | .allow_warnings = allow_warnings, |
| 1057 | .stage2 = false, | ||
| 1055 | }; | 1058 | }; |
| 1056 | 1059 | ||
| 1057 | tc.addSourceFile(filename, source); | 1060 | tc.addSourceFile(filename, source); |
| ... | @@ -1072,6 +1075,26 @@ pub const TranslateCContext = struct { | ... | @@ -1072,6 +1075,26 @@ pub const TranslateCContext = struct { |
| 1072 | self.addCase(tc); | 1075 | self.addCase(tc); |
| 1073 | } | 1076 | } |
| 1074 | 1077 | ||
| 1078 | pub fn add_both(self: *TranslateCContext, name: []const u8, source: []const u8, expected_lines: ...) void { | ||
| 1079 | for ([]bool{ false, true }) |stage2| { | ||
| 1080 | const tc = self.create(false, "source.c", name, source, expected_lines); | ||
| 1081 | tc.stage2 = stage2; | ||
| 1082 | self.addCase(tc); | ||
| 1083 | } | ||
| 1084 | } | ||
| 1085 | |||
| 1086 | pub fn add_2(self: *TranslateCContext, name: []const u8, source: []const u8, expected_lines: ...) void { | ||
| 1087 | const tc = self.create(false, "source.c", name, source, expected_lines); | ||
| 1088 | tc.stage2 = true; | ||
| 1089 | self.addCase(tc); | ||
| 1090 | } | ||
| 1091 | |||
| 1092 | pub fn addC_2(self: *TranslateCContext, name: []const u8, source: []const u8, expected_lines: ...) void { | ||
| 1093 | const tc = self.create(false, "source.c", name, source, expected_lines); | ||
| 1094 | tc.stage2 = true; | ||
| 1095 | self.addCase(tc); | ||
| 1096 | } | ||
| 1097 | |||
| 1075 | pub fn addAllowWarnings(self: *TranslateCContext, name: []const u8, source: []const u8, expected_lines: ...) void { | 1098 | pub fn addAllowWarnings(self: *TranslateCContext, name: []const u8, source: []const u8, expected_lines: ...) void { |
| 1076 | const tc = self.create(true, "source.h", name, source, expected_lines); | 1099 | const tc = self.create(true, "source.h", name, source, expected_lines); |
| 1077 | self.addCase(tc); | 1100 | self.addCase(tc); |
| ... | @@ -1080,7 +1103,8 @@ pub const TranslateCContext = struct { | ... | @@ -1080,7 +1103,8 @@ pub const TranslateCContext = struct { |
| 1080 | pub fn addCase(self: *TranslateCContext, case: *const TestCase) void { | 1103 | pub fn addCase(self: *TranslateCContext, case: *const TestCase) void { |
| 1081 | const b = self.b; | 1104 | const b = self.b; |
| 1082 | 1105 | ||
| 1083 | const annotated_case_name = fmt.allocPrint(self.b.allocator, "translate-c {}", case.name) catch unreachable; | 1106 | const translate_c_cmd = if (case.stage2) "translate-c-2" else "translate-c"; |
| 1107 | const annotated_case_name = fmt.allocPrint(self.b.allocator, "{} {}", translate_c_cmd, case.name) catch unreachable; | ||
| 1084 | if (self.test_filter) |filter| { | 1108 | if (self.test_filter) |filter| { |
| 1085 | if (mem.indexOf(u8, annotated_case_name, filter) == null) return; | 1109 | if (mem.indexOf(u8, annotated_case_name, filter) == null) return; |
| 1086 | } | 1110 | } |
test/translate_c.zig+6| ... | @@ -2,6 +2,12 @@ const tests = @import("tests.zig"); | ... | @@ -2,6 +2,12 @@ const tests = @import("tests.zig"); |
| 2 | const builtin = @import("builtin"); | 2 | const builtin = @import("builtin"); |
| 3 | 3 | ||
| 4 | pub fn addCases(cases: *tests.TranslateCContext) void { | 4 | pub fn addCases(cases: *tests.TranslateCContext) void { |
| 5 | cases.add_both("simple noreturn fn", | ||
| 6 | \\void __attribute__((noreturn)) foo(void); | ||
| 7 | , | ||
| 8 | \\extern fn foo() noreturn; | ||
| 9 | ); | ||
| 10 | |||
| 5 | cases.add("macro with left shift", | 11 | cases.add("macro with left shift", |
| 6 | \\#define REDISMODULE_READ (1<<0) | 12 | \\#define REDISMODULE_READ (1<<0) |
| 7 | , | 13 | , |