| ... | @@ -203,9 +203,7 @@ const Scope = struct { | ... | @@ -203,9 +203,7 @@ const Scope = struct { |
| 203 | /// Check if the global scope contains this name, without looking into the "future", e.g. | 203 | /// Check if the global scope contains this name, without looking into the "future", e.g. |
| 204 | /// ignore the preprocessed decl and macro names. | 204 | /// ignore the preprocessed decl and macro names. |
| 205 | fn containsNow(scope: *Root, name: []const u8) bool { | 205 | fn containsNow(scope: *Root, name: []const u8) bool { |
| 206 | return isZigPrimitiveType(name) or | 206 | return scope.sym_table.contains(name) or scope.macro_table.contains(name); |
| 207 | scope.sym_table.contains(name) or | | |
| 208 | scope.macro_table.contains(name); | | |
| 209 | } | 207 | } |
| 210 | | 208 | |
| 211 | /// Check if the global scope contains the name, includes all decls that haven't been translated yet. | 209 | /// Check if the global scope contains the name, includes all decls that haven't been translated yet. |
| ... | @@ -495,19 +493,17 @@ fn declVisitorNamesOnly(c: *Context, decl: *const clang.Decl) Error!void { | ... | @@ -495,19 +493,17 @@ fn declVisitorNamesOnly(c: *Context, decl: *const clang.Decl) Error!void { |
| 495 | }, | 493 | }, |
| 496 | else => return, | 494 | else => return, |
| 497 | } else unreachable; | 495 | } else unreachable; |
| 498 | // TODO https://github.com/ziglang/zig/issues/3756 | 496 | |
| 499 | // TODO https://github.com/ziglang/zig/issues/1802 | | |
| 500 | const name = if (isZigPrimitiveType(decl_name)) try std.fmt.allocPrint(c.arena, "{s}_{d}", .{ decl_name, c.getMangle() }) else decl_name; | | |
| 501 | const result = try c.unnamed_typedefs.getOrPut(c.gpa, addr); | 497 | const result = try c.unnamed_typedefs.getOrPut(c.gpa, addr); |
| 502 | if (result.found_existing) { | 498 | if (result.found_existing) { |
| 503 | // One typedef can declare multiple names. | 499 | // One typedef can declare multiple names. |
| 504 | // Don't put this one in `decl_table` so it's processed later. | 500 | // Don't put this one in `decl_table` so it's processed later. |
| 505 | return; | 501 | return; |
| 506 | } | 502 | } |
| 507 | result.value_ptr.* = name; | 503 | result.value_ptr.* = decl_name; |
| 508 | // Put this typedef in the decl_table to avoid redefinitions. | 504 | // Put this typedef in the decl_table to avoid redefinitions. |
| 509 | try c.decl_table.putNoClobber(c.gpa, @ptrToInt(typedef_decl.getCanonicalDecl()), name); | 505 | try c.decl_table.putNoClobber(c.gpa, @ptrToInt(typedef_decl.getCanonicalDecl()), decl_name); |
| 510 | try c.typedefs.put(c.gpa, name, {}); | 506 | try c.typedefs.put(c.gpa, decl_name, {}); |
| 511 | } | 507 | } |
| 512 | } | 508 | } |
| 513 | } | 509 | } |
| ... | @@ -752,10 +748,6 @@ fn visitVarDecl(c: *Context, var_decl: *const clang.VarDecl, mangled_name: ?[]co | ... | @@ -752,10 +748,6 @@ fn visitVarDecl(c: *Context, var_decl: *const clang.VarDecl, mangled_name: ?[]co |
| 752 | const is_pub = mangled_name == null; | 748 | const is_pub = mangled_name == null; |
| 753 | const is_threadlocal = var_decl.getTLSKind() != .None; | 749 | const is_threadlocal = var_decl.getTLSKind() != .None; |
| 754 | const scope = &c.global_scope.base; | 750 | const scope = &c.global_scope.base; |
| 755 | | | |
| 756 | // TODO https://github.com/ziglang/zig/issues/3756 | | |
| 757 | // TODO https://github.com/ziglang/zig/issues/1802 | | |
| 758 | const checked_name = if (isZigPrimitiveType(var_name)) try std.fmt.allocPrint(c.arena, "{s}_{d}", .{ var_name, c.getMangle() }) else var_name; | | |
| 759 | const var_decl_loc = var_decl.getLocation(); | 751 | const var_decl_loc = var_decl.getLocation(); |
| 760 | | 752 | |
| 761 | const qual_type = var_decl.getTypeSourceInfo_getType(); | 753 | const qual_type = var_decl.getTypeSourceInfo_getType(); |
| ... | @@ -774,7 +766,7 @@ fn visitVarDecl(c: *Context, var_decl: *const clang.VarDecl, mangled_name: ?[]co | ... | @@ -774,7 +766,7 @@ fn visitVarDecl(c: *Context, var_decl: *const clang.VarDecl, mangled_name: ?[]co |
| 774 | | 766 | |
| 775 | const type_node = transQualTypeMaybeInitialized(c, scope, qual_type, decl_init, var_decl_loc) catch |err| switch (err) { | 767 | const type_node = transQualTypeMaybeInitialized(c, scope, qual_type, decl_init, var_decl_loc) catch |err| switch (err) { |
| 776 | error.UnsupportedTranslation, error.UnsupportedType => { | 768 | error.UnsupportedTranslation, error.UnsupportedType => { |
| 777 | return failDecl(c, var_decl_loc, checked_name, "unable to resolve variable type", .{}); | 769 | return failDecl(c, var_decl_loc, var_name, "unable to resolve variable type", .{}); |
| 778 | }, | 770 | }, |
| 779 | error.OutOfMemory => |e| return e, | 771 | error.OutOfMemory => |e| return e, |
| 780 | }; | 772 | }; |
| ... | @@ -833,11 +825,11 @@ fn visitVarDecl(c: *Context, var_decl: *const clang.VarDecl, mangled_name: ?[]co | ... | @@ -833,11 +825,11 @@ fn visitVarDecl(c: *Context, var_decl: *const clang.VarDecl, mangled_name: ?[]co |
| 833 | .is_threadlocal = is_threadlocal, | 825 | .is_threadlocal = is_threadlocal, |
| 834 | .linksection_string = linksection_string, | 826 | .linksection_string = linksection_string, |
| 835 | .alignment = zigAlignment(var_decl.getAlignedAttribute(c.clang_context)), | 827 | .alignment = zigAlignment(var_decl.getAlignedAttribute(c.clang_context)), |
| 836 | .name = checked_name, | 828 | .name = var_name, |
| 837 | .type = type_node, | 829 | .type = type_node, |
| 838 | .init = init_node, | 830 | .init = init_node, |
| 839 | }); | 831 | }); |
| 840 | return addTopLevelDecl(c, checked_name, node); | 832 | return addTopLevelDecl(c, var_name, node); |
| 841 | } | 833 | } |
| 842 | | 834 | |
| 843 | const builtin_typedef_map = std.ComptimeStringMap([]const u8, .{ | 835 | const builtin_typedef_map = std.ComptimeStringMap([]const u8, .{ |
| ... | @@ -861,11 +853,7 @@ fn transTypeDef(c: *Context, scope: *Scope, typedef_decl: *const clang.TypedefNa | ... | @@ -861,11 +853,7 @@ fn transTypeDef(c: *Context, scope: *Scope, typedef_decl: *const clang.TypedefNa |
| 861 | const toplevel = scope.id == .root; | 853 | const toplevel = scope.id == .root; |
| 862 | const bs: *Scope.Block = if (!toplevel) try scope.findBlockScope(c) else undefined; | 854 | const bs: *Scope.Block = if (!toplevel) try scope.findBlockScope(c) else undefined; |
| 863 | | 855 | |
| 864 | const bare_name = try c.str(@ptrCast(*const clang.NamedDecl, typedef_decl).getName_bytes_begin()); | 856 | var name: []const u8 = try c.str(@ptrCast(*const clang.NamedDecl, typedef_decl).getName_bytes_begin()); |
| 865 | | | |
| 866 | // TODO https://github.com/ziglang/zig/issues/3756 | | |
| 867 | // TODO https://github.com/ziglang/zig/issues/1802 | | |
| 868 | var name: []const u8 = if (isZigPrimitiveType(bare_name)) try std.fmt.allocPrint(c.arena, "{s}_{d}", .{ bare_name, c.getMangle() }) else bare_name; | | |
| 869 | try c.typedefs.put(c.gpa, name, {}); | 857 | try c.typedefs.put(c.gpa, name, {}); |
| 870 | | 858 | |
| 871 | if (builtin_typedef_map.get(name)) |builtin| { | 859 | if (builtin_typedef_map.get(name)) |builtin| { |
| ... | @@ -1535,12 +1523,12 @@ fn transOffsetOfExpr( | ... | @@ -1535,12 +1523,12 @@ fn transOffsetOfExpr( |
| 1535 | /// node -> @bitCast(usize, @intCast(isize, node)) | 1523 | /// node -> @bitCast(usize, @intCast(isize, node)) |
| 1536 | fn usizeCastForWrappingPtrArithmetic(gpa: *mem.Allocator, node: Node) TransError!Node { | 1524 | fn usizeCastForWrappingPtrArithmetic(gpa: *mem.Allocator, node: Node) TransError!Node { |
| 1537 | const intcast_node = try Tag.int_cast.create(gpa, .{ | 1525 | const intcast_node = try Tag.int_cast.create(gpa, .{ |
| 1538 | .lhs = try Tag.identifier.create(gpa, "isize"), | 1526 | .lhs = try Tag.type.create(gpa, "isize"), |
| 1539 | .rhs = node, | 1527 | .rhs = node, |
| 1540 | }); | 1528 | }); |
| 1541 | | 1529 | |
| 1542 | return Tag.bit_cast.create(gpa, .{ | 1530 | return Tag.bit_cast.create(gpa, .{ |
| 1543 | .lhs = try Tag.identifier.create(gpa, "usize"), | 1531 | .lhs = try Tag.type.create(gpa, "usize"), |
| 1544 | .rhs = intcast_node, | 1532 | .rhs = intcast_node, |
| 1545 | }); | 1533 | }); |
| 1546 | } | 1534 | } |
| ... | @@ -3345,7 +3333,7 @@ fn transSignedArrayAccess( | ... | @@ -3345,7 +3333,7 @@ fn transSignedArrayAccess( |
| 3345 | const then_value = try Tag.add.create(c.arena, .{ | 3333 | const then_value = try Tag.add.create(c.arena, .{ |
| 3346 | .lhs = container_node, | 3334 | .lhs = container_node, |
| 3347 | .rhs = try Tag.int_cast.create(c.arena, .{ | 3335 | .rhs = try Tag.int_cast.create(c.arena, .{ |
| 3348 | .lhs = try Tag.identifier.create(c.arena, "usize"), | 3336 | .lhs = try Tag.type.create(c.arena, "usize"), |
| 3349 | .rhs = tmp_ref, | 3337 | .rhs = tmp_ref, |
| 3350 | }), | 3338 | }), |
| 3351 | }); | 3339 | }); |
| ... | @@ -3357,7 +3345,7 @@ fn transSignedArrayAccess( | ... | @@ -3357,7 +3345,7 @@ fn transSignedArrayAccess( |
| 3357 | | 3345 | |
| 3358 | const minuend = container_node; | 3346 | const minuend = container_node; |
| 3359 | const signed_size = try Tag.int_cast.create(c.arena, .{ | 3347 | const signed_size = try Tag.int_cast.create(c.arena, .{ |
| 3360 | .lhs = try Tag.identifier.create(c.arena, "isize"), | 3348 | .lhs = try Tag.type.create(c.arena, "isize"), |
| 3361 | .rhs = tmp_ref, | 3349 | .rhs = tmp_ref, |
| 3362 | }); | 3350 | }); |
| 3363 | const to_cast = try Tag.add_wrap.create(c.arena, .{ | 3351 | const to_cast = try Tag.add_wrap.create(c.arena, .{ |
| ... | @@ -3365,7 +3353,7 @@ fn transSignedArrayAccess( | ... | @@ -3365,7 +3353,7 @@ fn transSignedArrayAccess( |
| 3365 | .rhs = try Tag.negate.create(c.arena, Tag.one_literal.init()), | 3353 | .rhs = try Tag.negate.create(c.arena, Tag.one_literal.init()), |
| 3366 | }); | 3354 | }); |
| 3367 | const bitcast_node = try Tag.bit_cast.create(c.arena, .{ | 3355 | const bitcast_node = try Tag.bit_cast.create(c.arena, .{ |
| 3368 | .lhs = try Tag.identifier.create(c.arena, "usize"), | 3356 | .lhs = try Tag.type.create(c.arena, "usize"), |
| 3369 | .rhs = to_cast, | 3357 | .rhs = to_cast, |
| 3370 | }); | 3358 | }); |
| 3371 | const subtrahend = try Tag.bit_not.create(c.arena, bitcast_node); | 3359 | const subtrahend = try Tag.bit_not.create(c.arena, bitcast_node); |
| ... | @@ -3421,7 +3409,7 @@ fn transArrayAccess(c: *Context, scope: *Scope, stmt: *const clang.ArraySubscrip | ... | @@ -3421,7 +3409,7 @@ fn transArrayAccess(c: *Context, scope: *Scope, stmt: *const clang.ArraySubscrip |
| 3421 | const container_node = try transExpr(c, scope, unwrapped_base, .used); | 3409 | const container_node = try transExpr(c, scope, unwrapped_base, .used); |
| 3422 | const rhs = if (is_longlong or is_signed) blk: { | 3410 | const rhs = if (is_longlong or is_signed) blk: { |
| 3423 | // check if long long first so that signed long long doesn't just become unsigned long long | 3411 | // check if long long first so that signed long long doesn't just become unsigned long long |
| 3424 | const typeid_node = if (is_longlong) try Tag.identifier.create(c.arena, "usize") else try transQualTypeIntWidthOf(c, subscr_qt, false); | 3412 | const typeid_node = if (is_longlong) try Tag.type.create(c.arena, "usize") else try transQualTypeIntWidthOf(c, subscr_qt, false); |
| 3425 | break :blk try Tag.int_cast.create(c.arena, .{ .lhs = typeid_node, .rhs = try transExpr(c, scope, subscr_expr, .used) }); | 3413 | break :blk try Tag.int_cast.create(c.arena, .{ .lhs = typeid_node, .rhs = try transExpr(c, scope, subscr_expr, .used) }); |
| 3426 | } else try transExpr(c, scope, subscr_expr, .used); | 3414 | } else try transExpr(c, scope, subscr_expr, .used); |
| 3427 | | 3415 | |
| ... | @@ -3953,7 +3941,7 @@ fn transFloatingLiteral(c: *Context, scope: *Scope, expr: *const clang.FloatingL | ... | @@ -3953,7 +3941,7 @@ fn transFloatingLiteral(c: *Context, scope: *Scope, expr: *const clang.FloatingL |
| 3953 | | 3941 | |
| 3954 | fn transBinaryConditionalOperator(c: *Context, scope: *Scope, stmt: *const clang.BinaryConditionalOperator, used: ResultUsed) TransError!Node { | 3942 | fn transBinaryConditionalOperator(c: *Context, scope: *Scope, stmt: *const clang.BinaryConditionalOperator, used: ResultUsed) TransError!Node { |
| 3955 | // GNU extension of the ternary operator where the middle expression is | 3943 | // GNU extension of the ternary operator where the middle expression is |
| 3956 | // omitted, the conditition itself is returned if it evaluates to true | 3944 | // omitted, the condition itself is returned if it evaluates to true |
| 3957 | const qt = @ptrCast(*const clang.Expr, stmt).getType(); | 3945 | const qt = @ptrCast(*const clang.Expr, stmt).getType(); |
| 3958 | const res_is_bool = qualTypeIsBoolean(qt); | 3946 | const res_is_bool = qualTypeIsBoolean(qt); |
| 3959 | const casted_stmt = @ptrCast(*const clang.AbstractConditionalOperator, stmt); | 3947 | const casted_stmt = @ptrCast(*const clang.AbstractConditionalOperator, stmt); |
| ... | @@ -4040,7 +4028,7 @@ fn transConditionalOperator(c: *Context, scope: *Scope, stmt: *const clang.Condi | ... | @@ -4040,7 +4028,7 @@ fn transConditionalOperator(c: *Context, scope: *Scope, stmt: *const clang.Condi |
| 4040 | .then = then_body, | 4028 | .then = then_body, |
| 4041 | .@"else" = else_body, | 4029 | .@"else" = else_body, |
| 4042 | }); | 4030 | }); |
| 4043 | // Clang inserts ImplicitCast(ToVoid)'s to both rhs and lhs so we don't need to supress the result here. | 4031 | // Clang inserts ImplicitCast(ToVoid)'s to both rhs and lhs so we don't need to suppress the result here. |
| 4044 | return if_node; | 4032 | return if_node; |
| 4045 | } | 4033 | } |
| 4046 | | 4034 | |
| ... | @@ -4671,6 +4659,7 @@ fn transType(c: *Context, scope: *Scope, ty: *const clang.Type, source_loc: clan | ... | @@ -4671,6 +4659,7 @@ fn transType(c: *Context, scope: *Scope, ty: *const clang.Type, source_loc: clan |
| 4671 | if (@ptrCast(*const clang.Decl, typedef_decl).castToNamedDecl()) |named_decl| { | 4659 | if (@ptrCast(*const clang.Decl, typedef_decl).castToNamedDecl()) |named_decl| { |
| 4672 | const decl_name = try c.str(named_decl.getName_bytes_begin()); | 4660 | const decl_name = try c.str(named_decl.getName_bytes_begin()); |
| 4673 | if (c.global_names.get(decl_name)) |_| trans_scope = &c.global_scope.base; | 4661 | if (c.global_names.get(decl_name)) |_| trans_scope = &c.global_scope.base; |
| | 4662 | if (builtin_typedef_map.get(decl_name)) |builtin| return Tag.type.create(c.arena, builtin); |
| 4674 | } | 4663 | } |
| 4675 | try transTypeDef(c, trans_scope, typedef_decl); | 4664 | try transTypeDef(c, trans_scope, typedef_decl); |
| 4676 | const name = c.decl_table.get(@ptrToInt(typedef_decl.getCanonicalDecl())).?; | 4665 | const name = c.decl_table.get(@ptrToInt(typedef_decl.getCanonicalDecl())).?; |
| ... | @@ -4994,19 +4983,6 @@ pub fn freeErrors(errors: []ClangErrMsg) void { | ... | @@ -4994,19 +4983,6 @@ pub fn freeErrors(errors: []ClangErrMsg) void { |
| 4994 | errors.ptr.delete(errors.len); | 4983 | errors.ptr.delete(errors.len); |
| 4995 | } | 4984 | } |
| 4996 | | 4985 | |
| 4997 | fn isZigPrimitiveType(name: []const u8) bool { | | |
| 4998 | if (name.len > 1 and (name[0] == 'u' or name[0] == 'i')) { | | |
| 4999 | for (name[1..]) |c| { | | |
| 5000 | switch (c) { | | |
| 5001 | '0'...'9' => {}, | | |
| 5002 | else => return false, | | |
| 5003 | } | | |
| 5004 | } | | |
| 5005 | return true; | | |
| 5006 | } | | |
| 5007 | return @import("AstGen.zig").simple_types.has(name); | | |
| 5008 | } | | |
| 5009 | | | |
| 5010 | const PatternList = struct { | 4986 | const PatternList = struct { |
| 5011 | patterns: []Pattern, | 4987 | patterns: []Pattern, |
| 5012 | | 4988 | |
| ... | @@ -5311,10 +5287,7 @@ fn transPreprocessorEntities(c: *Context, unit: *clang.ASTUnit) Error!void { | ... | @@ -5311,10 +5287,7 @@ fn transPreprocessorEntities(c: *Context, unit: *clang.ASTUnit) Error!void { |
| 5311 | const end_loc = clang.Lexer.getLocForEndOfToken(macro.getSourceRange_getEnd(), c.source_manager, unit); | 5287 | const end_loc = clang.Lexer.getLocForEndOfToken(macro.getSourceRange_getEnd(), c.source_manager, unit); |
| 5312 | | 5288 | |
| 5313 | const name = try c.str(raw_name); | 5289 | const name = try c.str(raw_name); |
| 5314 | // TODO https://github.com/ziglang/zig/issues/3756 | 5290 | if (scope.containsNow(name)) { |
| 5315 | // TODO https://github.com/ziglang/zig/issues/1802 | | |
| 5316 | const mangled_name = if (isZigPrimitiveType(name)) try std.fmt.allocPrint(c.arena, "{s}_{d}", .{ name, c.getMangle() }) else name; | | |
| 5317 | if (scope.containsNow(mangled_name)) { | | |
| 5318 | continue; | 5291 | continue; |
| 5319 | } | 5292 | } |
| 5320 | | 5293 | |
| ... | @@ -5328,7 +5301,7 @@ fn transPreprocessorEntities(c: *Context, unit: *clang.ASTUnit) Error!void { | ... | @@ -5328,7 +5301,7 @@ fn transPreprocessorEntities(c: *Context, unit: *clang.ASTUnit) Error!void { |
| 5328 | var macro_ctx = MacroCtx{ | 5301 | var macro_ctx = MacroCtx{ |
| 5329 | .source = slice, | 5302 | .source = slice, |
| 5330 | .list = tok_list.items, | 5303 | .list = tok_list.items, |
| 5331 | .name = mangled_name, | 5304 | .name = name, |
| 5332 | .loc = begin_loc, | 5305 | .loc = begin_loc, |
| 5333 | }; | 5306 | }; |
| 5334 | assert(mem.eql(u8, macro_ctx.slice(), name)); | 5307 | assert(mem.eql(u8, macro_ctx.slice(), name)); |
| ... | @@ -5766,7 +5739,8 @@ fn parseCPrimaryExprInner(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!N | ... | @@ -5766,7 +5739,8 @@ fn parseCPrimaryExprInner(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!N |
| 5766 | try m.fail(c, "TODO implement function '{s}' in std.zig.c_builtins", .{mangled_name}); | 5739 | try m.fail(c, "TODO implement function '{s}' in std.zig.c_builtins", .{mangled_name}); |
| 5767 | return error.ParseError; | 5740 | return error.ParseError; |
| 5768 | } | 5741 | } |
| 5769 | const identifier = try Tag.identifier.create(c.arena, builtin_typedef_map.get(mangled_name) orelse mangled_name); | 5742 | if (builtin_typedef_map.get(mangled_name)) |ty| return Tag.type.create(c.arena, ty); |
| | 5743 | const identifier = try Tag.identifier.create(c.arena, mangled_name); |
| 5770 | scope.skipVariableDiscard(identifier.castTag(.identifier).?.data); | 5744 | scope.skipVariableDiscard(identifier.castTag(.identifier).?.data); |
| 5771 | return identifier; | 5745 | return identifier; |
| 5772 | }, | 5746 | }, |
| ... | @@ -6055,7 +6029,8 @@ fn parseCSpecifierQualifierList(c: *Context, m: *MacroCtx, scope: *Scope, allow_ | ... | @@ -6055,7 +6029,8 @@ fn parseCSpecifierQualifierList(c: *Context, m: *MacroCtx, scope: *Scope, allow_ |
| 6055 | .Identifier => { | 6029 | .Identifier => { |
| 6056 | const mangled_name = scope.getAlias(m.slice()); | 6030 | const mangled_name = scope.getAlias(m.slice()); |
| 6057 | if (!allow_fail or c.typedefs.contains(mangled_name)) { | 6031 | if (!allow_fail or c.typedefs.contains(mangled_name)) { |
| 6058 | return try Tag.identifier.create(c.arena, builtin_typedef_map.get(mangled_name) orelse mangled_name); | 6032 | if (builtin_typedef_map.get(mangled_name)) |ty| return try Tag.type.create(c.arena, ty); |
| | 6033 | return try Tag.identifier.create(c.arena, mangled_name); |
| 6059 | } | 6034 | } |
| 6060 | }, | 6035 | }, |
| 6061 | .Keyword_void => return try Tag.type.create(c.arena, "c_void"), | 6036 | .Keyword_void => return try Tag.type.create(c.arena, "c_void"), |