| ... | ... | @@ -35,10 +35,10 @@ string_bytes: ArrayListUnmanaged(u8) = .{}, |
| 35 | 35 | arena: *Allocator, |
| 36 | 36 | string_table: std.StringHashMapUnmanaged(u32) = .{}, |
| 37 | 37 | compile_errors: ArrayListUnmanaged(Zir.Inst.CompileErrors.Item) = .{}, |
| 38 | | /// String table indexes, keeps track of all `@import` operands. |
| 39 | | imports: std.AutoArrayHashMapUnmanaged(u32, void) = .{}, |
| 40 | 38 | /// The topmost block of the current function. |
| 41 | 39 | fn_block: ?*GenZir = null, |
| 40 | /// String table indexes, keeps track of all `@import` operands. |
| 41 | imports: std.AutoArrayHashMapUnmanaged(u32, void) = .{}, |
| 42 | 42 | |
| 43 | 43 | pub fn addExtra(astgen: *AstGen, extra: anytype) Allocator.Error!u32 { |
| 44 | 44 | const fields = std.meta.fields(@TypeOf(extra)); |
| ... | ... | @@ -1078,6 +1078,7 @@ pub fn fnProtoExpr( |
| 1078 | 1078 | .lib_name = 0, |
| 1079 | 1079 | .is_var_args = is_var_args, |
| 1080 | 1080 | .is_inferred_error = false, |
| 1081 | .is_test = false, |
| 1081 | 1082 | }); |
| 1082 | 1083 | return rvalue(gz, scope, rl, result, fn_proto.ast.proto_node); |
| 1083 | 1084 | } |
| ... | ... | @@ -2610,6 +2611,26 @@ const WipDecls = struct { |
| 2610 | 2611 | const bits_per_field = 4; |
| 2611 | 2612 | const fields_per_u32 = 32 / bits_per_field; |
| 2612 | 2613 | |
| 2614 | fn next( |
| 2615 | wip_decls: *WipDecls, |
| 2616 | gpa: *Allocator, |
| 2617 | is_pub: bool, |
| 2618 | is_export: bool, |
| 2619 | has_align: bool, |
| 2620 | has_section: bool, |
| 2621 | ) Allocator.Error!void { |
| 2622 | if (wip_decls.decl_index % fields_per_u32 == 0 and wip_decls.decl_index != 0) { |
| 2623 | try wip_decls.bit_bag.append(gpa, wip_decls.cur_bit_bag); |
| 2624 | wip_decls.cur_bit_bag = 0; |
| 2625 | } |
| 2626 | wip_decls.cur_bit_bag = (wip_decls.cur_bit_bag >> bits_per_field) | |
| 2627 | (@as(u32, @boolToInt(is_pub)) << 28) | |
| 2628 | (@as(u32, @boolToInt(is_export)) << 29) | |
| 2629 | (@as(u32, @boolToInt(has_align)) << 30) | |
| 2630 | (@as(u32, @boolToInt(has_section)) << 31); |
| 2631 | wip_decls.decl_index += 1; |
| 2632 | } |
| 2633 | |
| 2613 | 2634 | fn deinit(wip_decls: *WipDecls, gpa: *Allocator) void { |
| 2614 | 2635 | wip_decls.bit_bag.deinit(gpa); |
| 2615 | 2636 | wip_decls.payload.deinit(gpa); |
| ... | ... | @@ -2652,16 +2673,7 @@ fn fnDecl( |
| 2652 | 2673 | break :inst try comptimeExpr(&decl_gz, &decl_gz.base, .{ .ty = .const_slice_u8_type }, fn_proto.ast.section_expr); |
| 2653 | 2674 | }; |
| 2654 | 2675 | |
| 2655 | | if (wip_decls.decl_index % WipDecls.fields_per_u32 == 0 and wip_decls.decl_index != 0) { |
| 2656 | | try wip_decls.bit_bag.append(gpa, wip_decls.cur_bit_bag); |
| 2657 | | wip_decls.cur_bit_bag = 0; |
| 2658 | | } |
| 2659 | | wip_decls.cur_bit_bag = (wip_decls.cur_bit_bag >> WipDecls.bits_per_field) | |
| 2660 | | (@as(u32, @boolToInt(is_pub)) << 28) | |
| 2661 | | (@as(u32, @boolToInt(is_export)) << 29) | |
| 2662 | | (@as(u32, @boolToInt(align_inst != .none)) << 30) | |
| 2663 | | (@as(u32, @boolToInt(section_inst != .none)) << 31); |
| 2664 | | wip_decls.decl_index += 1; |
| 2676 | try wip_decls.next(gpa, is_pub, is_export, align_inst != .none, section_inst != .none); |
| 2665 | 2677 | |
| 2666 | 2678 | // The AST params array does not contain anytype and ... parameters. |
| 2667 | 2679 | // We must iterate to count how many param types to allocate. |
| ... | ... | @@ -2750,6 +2762,7 @@ fn fnDecl( |
| 2750 | 2762 | .lib_name = lib_name, |
| 2751 | 2763 | .is_var_args = is_var_args, |
| 2752 | 2764 | .is_inferred_error = false, |
| 2765 | .is_test = false, |
| 2753 | 2766 | }); |
| 2754 | 2767 | } else func: { |
| 2755 | 2768 | if (is_var_args) { |
| ... | ... | @@ -2821,6 +2834,7 @@ fn fnDecl( |
| 2821 | 2834 | .lib_name = lib_name, |
| 2822 | 2835 | .is_var_args = is_var_args, |
| 2823 | 2836 | .is_inferred_error = is_inferred_error, |
| 2837 | .is_test = false, |
| 2824 | 2838 | }); |
| 2825 | 2839 | }; |
| 2826 | 2840 | |
| ... | ... | @@ -2833,7 +2847,12 @@ fn fnDecl( |
| 2833 | 2847 | _ = try decl_gz.addBreak(.break_inline, block_inst, func_inst); |
| 2834 | 2848 | try decl_gz.setBlockBody(block_inst); |
| 2835 | 2849 | |
| 2836 | | try wip_decls.payload.ensureUnusedCapacity(gpa, 4); |
| 2850 | try wip_decls.payload.ensureUnusedCapacity(gpa, 8); |
| 2851 | { |
| 2852 | const contents_hash = std.zig.hashSrc(tree.getNodeSource(fn_proto.ast.proto_node)); |
| 2853 | const casted = @bitCast([4]u32, contents_hash); |
| 2854 | wip_decls.payload.appendSliceAssumeCapacity(&casted); |
| 2855 | } |
| 2837 | 2856 | wip_decls.payload.appendAssumeCapacity(fn_name_str_index); |
| 2838 | 2857 | wip_decls.payload.appendAssumeCapacity(block_inst); |
| 2839 | 2858 | if (align_inst != .none) { |
| ... | ... | @@ -2879,16 +2898,7 @@ fn globalVarDecl( |
| 2879 | 2898 | const section_inst: Zir.Inst.Ref = if (var_decl.ast.section_node == 0) .none else inst: { |
| 2880 | 2899 | break :inst try comptimeExpr(&block_scope, &block_scope.base, .{ .ty = .const_slice_u8_type }, var_decl.ast.section_node); |
| 2881 | 2900 | }; |
| 2882 | | if (wip_decls.decl_index % WipDecls.fields_per_u32 == 0 and wip_decls.decl_index != 0) { |
| 2883 | | try wip_decls.bit_bag.append(gpa, wip_decls.cur_bit_bag); |
| 2884 | | wip_decls.cur_bit_bag = 0; |
| 2885 | | } |
| 2886 | | wip_decls.cur_bit_bag = (wip_decls.cur_bit_bag >> WipDecls.bits_per_field) | |
| 2887 | | (@as(u32, @boolToInt(is_pub)) << 28) | |
| 2888 | | (@as(u32, @boolToInt(is_export)) << 29) | |
| 2889 | | (@as(u32, @boolToInt(align_inst != .none)) << 30) | |
| 2890 | | (@as(u32, @boolToInt(section_inst != .none)) << 31); |
| 2891 | | wip_decls.decl_index += 1; |
| 2901 | try wip_decls.next(gpa, is_pub, is_export, align_inst != .none, section_inst != .none); |
| 2892 | 2902 | |
| 2893 | 2903 | const is_mutable = token_tags[var_decl.ast.mut_token] == .keyword_var; |
| 2894 | 2904 | const is_threadlocal = if (var_decl.threadlocal_token) |tok| blk: { |
| ... | ... | @@ -2950,7 +2960,12 @@ fn globalVarDecl( |
| 2950 | 2960 | const name_token = var_decl.ast.mut_token + 1; |
| 2951 | 2961 | const name_str_index = try gz.identAsString(name_token); |
| 2952 | 2962 | |
| 2953 | | try wip_decls.payload.ensureUnusedCapacity(gpa, 4); |
| 2963 | try wip_decls.payload.ensureUnusedCapacity(gpa, 8); |
| 2964 | { |
| 2965 | const contents_hash = std.zig.hashSrc(tree.getNodeSource(node)); |
| 2966 | const casted = @bitCast([4]u32, contents_hash); |
| 2967 | wip_decls.payload.appendSliceAssumeCapacity(&casted); |
| 2968 | } |
| 2954 | 2969 | wip_decls.payload.appendAssumeCapacity(name_str_index); |
| 2955 | 2970 | wip_decls.payload.appendAssumeCapacity(var_inst); |
| 2956 | 2971 | if (align_inst != .none) { |
| ... | ... | @@ -2965,21 +2980,48 @@ fn comptimeDecl( |
| 2965 | 2980 | astgen: *AstGen, |
| 2966 | 2981 | gz: *GenZir, |
| 2967 | 2982 | scope: *Scope, |
| 2983 | wip_decls: *WipDecls, |
| 2968 | 2984 | node: ast.Node.Index, |
| 2969 | 2985 | ) InnerError!void { |
| 2986 | const gpa = astgen.gpa; |
| 2970 | 2987 | const tree = &astgen.file.tree; |
| 2971 | 2988 | const node_datas = tree.nodes.items(.data); |
| 2972 | | const block_expr = node_datas[node].lhs; |
| 2973 | | // TODO probably we want to put these into a block and store a list of them |
| 2974 | | _ = try expr(gz, scope, .none, block_expr); |
| 2989 | const body_node = node_datas[node].lhs; |
| 2990 | |
| 2991 | // Up top so the ZIR instruction index marks the start range of this |
| 2992 | // top-level declaration. |
| 2993 | const block_inst = try gz.addBlock(.block_inline, node); |
| 2994 | try wip_decls.next(gpa, false, false, false, false); |
| 2995 | |
| 2996 | var decl_block: GenZir = .{ |
| 2997 | .force_comptime = true, |
| 2998 | .decl_node_index = node, |
| 2999 | .parent = scope, |
| 3000 | .astgen = astgen, |
| 3001 | }; |
| 3002 | defer decl_block.instructions.deinit(gpa); |
| 3003 | |
| 3004 | _ = try expr(&decl_block, &decl_block.base, .none, body_node); |
| 3005 | try decl_block.setBlockBody(block_inst); |
| 3006 | |
| 3007 | try wip_decls.payload.ensureUnusedCapacity(gpa, 6); |
| 3008 | { |
| 3009 | const contents_hash = std.zig.hashSrc(tree.getNodeSource(node)); |
| 3010 | const casted = @bitCast([4]u32, contents_hash); |
| 3011 | wip_decls.payload.appendSliceAssumeCapacity(&casted); |
| 3012 | } |
| 3013 | wip_decls.payload.appendAssumeCapacity(0); |
| 3014 | wip_decls.payload.appendAssumeCapacity(block_inst); |
| 2975 | 3015 | } |
| 2976 | 3016 | |
| 2977 | 3017 | fn usingnamespaceDecl( |
| 2978 | 3018 | astgen: *AstGen, |
| 2979 | 3019 | gz: *GenZir, |
| 2980 | 3020 | scope: *Scope, |
| 3021 | wip_decls: *WipDecls, |
| 2981 | 3022 | node: ast.Node.Index, |
| 2982 | 3023 | ) InnerError!void { |
| 3024 | const gpa = astgen.gpa; |
| 2983 | 3025 | const tree = &astgen.file.tree; |
| 2984 | 3026 | const node_datas = tree.nodes.items(.data); |
| 2985 | 3027 | |
| ... | ... | @@ -2990,14 +3032,38 @@ fn usingnamespaceDecl( |
| 2990 | 3032 | const main_token = main_tokens[node]; |
| 2991 | 3033 | break :blk (main_token > 0 and token_tags[main_token - 1] == .keyword_pub); |
| 2992 | 3034 | }; |
| 2993 | | // TODO probably we want to put these into a block and store a list of them |
| 2994 | | const namespace_inst = try expr(gz, scope, .{ .ty = .type_type }, type_expr); |
| 3035 | // Up top so the ZIR instruction index marks the start range of this |
| 3036 | // top-level declaration. |
| 3037 | const block_inst = try gz.addBlock(.block_inline, node); |
| 3038 | try wip_decls.next(gpa, is_pub, true, false, false); |
| 3039 | |
| 3040 | var decl_block: GenZir = .{ |
| 3041 | .force_comptime = true, |
| 3042 | .decl_node_index = node, |
| 3043 | .parent = scope, |
| 3044 | .astgen = astgen, |
| 3045 | }; |
| 3046 | defer decl_block.instructions.deinit(gpa); |
| 3047 | |
| 3048 | const namespace_inst = try typeExpr(&decl_block, &decl_block.base, type_expr); |
| 3049 | _ = try decl_block.addBreak(.break_inline, block_inst, namespace_inst); |
| 3050 | try decl_block.setBlockBody(block_inst); |
| 3051 | |
| 3052 | try wip_decls.payload.ensureUnusedCapacity(gpa, 6); |
| 3053 | { |
| 3054 | const contents_hash = std.zig.hashSrc(tree.getNodeSource(node)); |
| 3055 | const casted = @bitCast([4]u32, contents_hash); |
| 3056 | wip_decls.payload.appendSliceAssumeCapacity(&casted); |
| 3057 | } |
| 3058 | wip_decls.payload.appendAssumeCapacity(0); |
| 3059 | wip_decls.payload.appendAssumeCapacity(block_inst); |
| 2995 | 3060 | } |
| 2996 | 3061 | |
| 2997 | 3062 | fn testDecl( |
| 2998 | 3063 | astgen: *AstGen, |
| 2999 | 3064 | gz: *GenZir, |
| 3000 | 3065 | scope: *Scope, |
| 3066 | wip_decls: *WipDecls, |
| 3001 | 3067 | node: ast.Node.Index, |
| 3002 | 3068 | ) InnerError!void { |
| 3003 | 3069 | const gpa = astgen.gpa; |
| ... | ... | @@ -3005,10 +3071,16 @@ fn testDecl( |
| 3005 | 3071 | const node_datas = tree.nodes.items(.data); |
| 3006 | 3072 | const body_node = node_datas[node].rhs; |
| 3007 | 3073 | |
| 3074 | // Up top so the ZIR instruction index marks the start range of this |
| 3075 | // top-level declaration. |
| 3076 | const block_inst = try gz.addBlock(.block_inline, node); |
| 3077 | |
| 3078 | try wip_decls.next(gpa, false, false, false, false); |
| 3079 | |
| 3008 | 3080 | var decl_block: GenZir = .{ |
| 3009 | 3081 | .force_comptime = true, |
| 3010 | 3082 | .decl_node_index = node, |
| 3011 | | .parent = &gz.base, |
| 3083 | .parent = scope, |
| 3012 | 3084 | .astgen = astgen, |
| 3013 | 3085 | }; |
| 3014 | 3086 | defer decl_block.instructions.deinit(gpa); |
| ... | ... | @@ -3053,15 +3125,20 @@ fn testDecl( |
| 3053 | 3125 | .lib_name = 0, |
| 3054 | 3126 | .is_var_args = false, |
| 3055 | 3127 | .is_inferred_error = true, |
| 3128 | .is_test = true, |
| 3056 | 3129 | }); |
| 3057 | 3130 | |
| 3058 | | const block_inst = try gz.addBlock(.block_inline, node); |
| 3059 | 3131 | _ = try decl_block.addBreak(.break_inline, block_inst, func_inst); |
| 3060 | 3132 | try decl_block.setBlockBody(block_inst); |
| 3061 | 3133 | |
| 3062 | | // TODO collect these into a test decl list |
| 3063 | | _ = test_name; |
| 3064 | | _ = block_inst; |
| 3134 | try wip_decls.payload.ensureUnusedCapacity(gpa, 6); |
| 3135 | { |
| 3136 | const contents_hash = std.zig.hashSrc(tree.getNodeSource(node)); |
| 3137 | const casted = @bitCast([4]u32, contents_hash); |
| 3138 | wip_decls.payload.appendSliceAssumeCapacity(&casted); |
| 3139 | } |
| 3140 | wip_decls.payload.appendAssumeCapacity(test_name); |
| 3141 | wip_decls.payload.appendAssumeCapacity(block_inst); |
| 3065 | 3142 | } |
| 3066 | 3143 | |
| 3067 | 3144 | fn structDeclInner( |
| ... | ... | @@ -3179,15 +3256,15 @@ fn structDeclInner( |
| 3179 | 3256 | }, |
| 3180 | 3257 | |
| 3181 | 3258 | .@"comptime" => { |
| 3182 | | try astgen.comptimeDecl(gz, scope, member_node); |
| 3259 | try astgen.comptimeDecl(gz, scope, &wip_decls, member_node); |
| 3183 | 3260 | continue; |
| 3184 | 3261 | }, |
| 3185 | 3262 | .@"usingnamespace" => { |
| 3186 | | try astgen.usingnamespaceDecl(gz, scope, member_node); |
| 3263 | try astgen.usingnamespaceDecl(gz, scope, &wip_decls, member_node); |
| 3187 | 3264 | continue; |
| 3188 | 3265 | }, |
| 3189 | 3266 | .test_decl => { |
| 3190 | | try astgen.testDecl(gz, scope, member_node); |
| 3267 | try astgen.testDecl(gz, scope, &wip_decls, member_node); |
| 3191 | 3268 | continue; |
| 3192 | 3269 | }, |
| 3193 | 3270 | else => unreachable, |
| ... | ... | @@ -3382,15 +3459,15 @@ fn unionDeclInner( |
| 3382 | 3459 | }, |
| 3383 | 3460 | |
| 3384 | 3461 | .@"comptime" => { |
| 3385 | | try astgen.comptimeDecl(gz, scope, member_node); |
| 3462 | try astgen.comptimeDecl(gz, scope, &wip_decls, member_node); |
| 3386 | 3463 | continue; |
| 3387 | 3464 | }, |
| 3388 | 3465 | .@"usingnamespace" => { |
| 3389 | | try astgen.usingnamespaceDecl(gz, scope, member_node); |
| 3466 | try astgen.usingnamespaceDecl(gz, scope, &wip_decls, member_node); |
| 3390 | 3467 | continue; |
| 3391 | 3468 | }, |
| 3392 | 3469 | .test_decl => { |
| 3393 | | try astgen.testDecl(gz, scope, member_node); |
| 3470 | try astgen.testDecl(gz, scope, &wip_decls, member_node); |
| 3394 | 3471 | continue; |
| 3395 | 3472 | }, |
| 3396 | 3473 | else => unreachable, |
| ... | ... | @@ -3731,15 +3808,15 @@ fn containerDecl( |
| 3731 | 3808 | }, |
| 3732 | 3809 | |
| 3733 | 3810 | .@"comptime" => { |
| 3734 | | try astgen.comptimeDecl(gz, scope, member_node); |
| 3811 | try astgen.comptimeDecl(gz, scope, &wip_decls, member_node); |
| 3735 | 3812 | continue; |
| 3736 | 3813 | }, |
| 3737 | 3814 | .@"usingnamespace" => { |
| 3738 | | try astgen.usingnamespaceDecl(gz, scope, member_node); |
| 3815 | try astgen.usingnamespaceDecl(gz, scope, &wip_decls, member_node); |
| 3739 | 3816 | continue; |
| 3740 | 3817 | }, |
| 3741 | 3818 | .test_decl => { |
| 3742 | | try astgen.testDecl(gz, scope, member_node); |
| 3819 | try astgen.testDecl(gz, scope, &wip_decls, member_node); |
| 3743 | 3820 | continue; |
| 3744 | 3821 | }, |
| 3745 | 3822 | else => unreachable, |
| ... | ... | @@ -3896,15 +3973,15 @@ fn containerDecl( |
| 3896 | 3973 | }, |
| 3897 | 3974 | |
| 3898 | 3975 | .@"comptime" => { |
| 3899 | | try astgen.comptimeDecl(gz, scope, member_node); |
| 3976 | try astgen.comptimeDecl(gz, scope, &wip_decls, member_node); |
| 3900 | 3977 | continue; |
| 3901 | 3978 | }, |
| 3902 | 3979 | .@"usingnamespace" => { |
| 3903 | | try astgen.usingnamespaceDecl(gz, scope, member_node); |
| 3980 | try astgen.usingnamespaceDecl(gz, scope, &wip_decls, member_node); |
| 3904 | 3981 | continue; |
| 3905 | 3982 | }, |
| 3906 | 3983 | .test_decl => { |
| 3907 | | try astgen.testDecl(gz, scope, member_node); |
| 3984 | try astgen.testDecl(gz, scope, &wip_decls, member_node); |
| 3908 | 3985 | continue; |
| 3909 | 3986 | }, |
| 3910 | 3987 | else => unreachable, |