| ... | ... | @@ -2,6 +2,7 @@ |
| 2 | 2 | // and stage2. Currently the only way it is used is with `zig translate-c-2`. |
| 3 | 3 | |
| 4 | 4 | const std = @import("std"); |
| 5 | const builtin = @import("builtin"); |
| 5 | 6 | const ast = std.zig.ast; |
| 6 | 7 | const Token = std.zig.Token; |
| 7 | 8 | use @import("clang.zig"); |
| ... | ... | @@ -12,13 +13,7 @@ pub const Mode = enum { |
| 12 | 13 | }; |
| 13 | 14 | |
| 14 | 15 | // TODO merge with Type.Fn.CallingConvention |
| 15 | | pub const CallingConvention = enum { |
| 16 | | auto, |
| 17 | | c, |
| 18 | | cold, |
| 19 | | naked, |
| 20 | | stdcall, |
| 21 | | }; |
| 16 | const CallingConvention = builtin.TypeInfo.CallingConvention; |
| 22 | 17 | |
| 23 | 18 | pub const ClangErrMsg = Stage2ErrorMsg; |
| 24 | 19 | |
| ... | ... | @@ -27,11 +22,64 @@ pub const Error = error{ |
| 27 | 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 | 75 | const Context = struct { |
| 31 | 76 | tree: *ast.Tree, |
| 32 | 77 | source_buffer: *std.Buffer, |
| 33 | 78 | err: Error, |
| 34 | 79 | source_manager: *ZigClangSourceManager, |
| 80 | decl_table: DeclTable, |
| 81 | global_scope: *Scope.Root, |
| 82 | mode: Mode, |
| 35 | 83 | |
| 36 | 84 | fn a(c: *Context) *std.mem.Allocator { |
| 37 | 85 | return &c.tree.arena_allocator.allocator; |
| ... | ... | @@ -76,7 +124,7 @@ pub fn translate( |
| 76 | 124 | |
| 77 | 125 | var tree_arena = std.heap.ArenaAllocator.init(backing_allocator); |
| 78 | 126 | errdefer tree_arena.deinit(); |
| 79 | | const arena = &tree_arena.allocator; |
| 127 | var arena = &tree_arena.allocator; |
| 80 | 128 | |
| 81 | 129 | const root_node = try arena.create(ast.Node.Root); |
| 82 | 130 | root_node.* = ast.Node.Root{ |
| ... | ... | @@ -96,14 +144,24 @@ pub fn translate( |
| 96 | 144 | .errors = ast.Tree.ErrorList.init(arena), |
| 97 | 145 | }; |
| 98 | 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 | 151 | var context = Context{ |
| 103 | 152 | .tree = tree, |
| 104 | 153 | .source_buffer = &source_buffer, |
| 105 | 154 | .source_manager = ZigClangASTUnit_getSourceManager(ast_unit), |
| 106 | 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 | 167 | if (!ZigClangASTUnit_visitLocalTopLevelDecls(ast_unit, &context, declVisitorC)) { |
| ... | ... | @@ -149,27 +207,39 @@ fn declVisitor(c: *Context, decl: *const ZigClangDecl) Error!void { |
| 149 | 207 | } |
| 150 | 208 | |
| 151 | 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))); |
| 153 | | |
| 154 | | // TODO The C++ code has this: |
| 155 | | //if (get_global(c, fn_name)) { |
| 156 | | // // we already saw this function |
| 157 | | // return; |
| 158 | | //} |
| 210 | if (try c.decl_table.put(@ptrToInt(fn_decl), {})) |_| return; // Avoid processing this decl twice |
| 159 | 211 | |
| 212 | const fn_name = try c.str(ZigClangDecl_getName_bytes_begin(@ptrCast(*const ZigClangDecl, fn_decl))); |
| 160 | 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) { |
| 162 | | error.UnsupportedType => { |
| 163 | | try failDecl(c, fn_decl_loc, fn_name, "unable to resolve prototype of function"); |
| 164 | | return; |
| 214 | const fn_qt = ZigClangFunctionDecl_getType(fn_decl); |
| 215 | const fn_type = ZigClangQualType_getTypePtr(fn_qt); |
| 216 | const proto_node = switch (ZigClangType_getTypeClass(fn_type)) { |
| 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 | 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 | 275 | else => return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported builtin type"), |
| 206 | 276 | } |
| 207 | 277 | }, |
| 208 | | .FunctionProto => { |
| 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 | | |
| 278 | .FunctionProto => return transFnProto(c, @ptrCast(*const ZigClangFunctionType, ty), source_loc, null, false), |
| 293 | 279 | else => { |
| 294 | 280 | const type_name = c.str(ZigClangType_getTypeClassName(ty)); |
| 295 | 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 | 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 | 398 | fn revertAndWarn( |
| 301 | 399 | restore_point: RestorePoint, |
| 302 | 400 | err: var, |