| ... | ... | @@ -2438,12 +2438,11 @@ fn structDeclInner( |
| 2438 | 2438 | |
| 2439 | 2439 | const decl_inst = try gz.addBlock(tag, node); |
| 2440 | 2440 | try gz.instructions.append(gpa, decl_inst); |
| 2441 | | if (field_index != 0) { |
| 2441 | if (block_scope.instructions.items.len != 0) { |
| 2442 | 2442 | _ = try block_scope.addBreak(.break_inline, decl_inst, .void_value); |
| 2443 | 2443 | } |
| 2444 | 2444 | |
| 2445 | | try astgen.extra.ensureCapacity(gpa, astgen.extra.items.len + |
| 2446 | | @typeInfo(Zir.Inst.StructDecl).Struct.fields.len + |
| 2445 | try astgen.extra.ensureUnusedCapacity(gpa, @typeInfo(Zir.Inst.StructDecl).Struct.fields.len + |
| 2447 | 2446 | bit_bag.items.len + @boolToInt(field_index != 0) + fields_data.items.len + |
| 2448 | 2447 | block_scope.instructions.items.len + |
| 2449 | 2448 | wip_decls.bit_bag.items.len + @boolToInt(wip_decls.decl_index != 0) + |
| ... | ... | @@ -2483,6 +2482,7 @@ fn containerDecl( |
| 2483 | 2482 | const tree = &astgen.file.tree; |
| 2484 | 2483 | const token_tags = tree.tokens.items(.tag); |
| 2485 | 2484 | const node_tags = tree.nodes.items(.tag); |
| 2485 | const node_datas = tree.nodes.items(.data); |
| 2486 | 2486 | |
| 2487 | 2487 | // We must not create any types until Sema. Here the goal is only to generate |
| 2488 | 2488 | // ZIR for all the field types, alignments, and default value expressions. |
| ... | ... | @@ -2594,22 +2594,12 @@ fn containerDecl( |
| 2594 | 2594 | }, |
| 2595 | 2595 | ); |
| 2596 | 2596 | } |
| 2597 | | if (counts.values == 0 and counts.decls == 0 and arg_inst == .none) { |
| 2598 | | return astgen.failNode(node, "TODO AstGen simple enums", .{}); |
| 2599 | | } |
| 2600 | 2597 | // In this case we must generate ZIR code for the tag values, similar to |
| 2601 | 2598 | // how structs are handled above. |
| 2602 | 2599 | const tag: Zir.Inst.Tag = if (counts.nonexhaustive_node == 0) |
| 2603 | 2600 | .enum_decl |
| 2604 | 2601 | else |
| 2605 | 2602 | .enum_decl_nonexhaustive; |
| 2606 | | if (counts.total_fields == 0) { |
| 2607 | | return gz.addPlNode(tag, node, Zir.Inst.EnumDecl{ |
| 2608 | | .tag_type = arg_inst, |
| 2609 | | .fields_len = 0, |
| 2610 | | .body_len = 0, |
| 2611 | | }); |
| 2612 | | } |
| 2613 | 2603 | |
| 2614 | 2604 | // The enum_decl instruction introduces a scope in which the decls of the enum |
| 2615 | 2605 | // are in scope, so that tag values can refer to decls within the enum itself. |
| ... | ... | @@ -2621,6 +2611,9 @@ fn containerDecl( |
| 2621 | 2611 | }; |
| 2622 | 2612 | defer block_scope.instructions.deinit(gpa); |
| 2623 | 2613 | |
| 2614 | var wip_decls: WipDecls = .{}; |
| 2615 | defer wip_decls.deinit(gpa); |
| 2616 | |
| 2624 | 2617 | var fields_data = ArrayListUnmanaged(u32){}; |
| 2625 | 2618 | defer fields_data.deinit(gpa); |
| 2626 | 2619 | |
| ... | ... | @@ -2639,7 +2632,81 @@ fn containerDecl( |
| 2639 | 2632 | .container_field_init => tree.containerFieldInit(member_node), |
| 2640 | 2633 | .container_field_align => tree.containerFieldAlign(member_node), |
| 2641 | 2634 | .container_field => tree.containerField(member_node), |
| 2642 | | else => continue, |
| 2635 | |
| 2636 | .fn_decl => { |
| 2637 | const fn_proto = node_datas[member_node].lhs; |
| 2638 | const body = node_datas[member_node].rhs; |
| 2639 | switch (node_tags[fn_proto]) { |
| 2640 | .fn_proto_simple => { |
| 2641 | var params: [1]ast.Node.Index = undefined; |
| 2642 | try astgen.fnDecl(gz, &wip_decls, body, tree.fnProtoSimple(&params, fn_proto)); |
| 2643 | continue; |
| 2644 | }, |
| 2645 | .fn_proto_multi => { |
| 2646 | try astgen.fnDecl(gz, &wip_decls, body, tree.fnProtoMulti(fn_proto)); |
| 2647 | continue; |
| 2648 | }, |
| 2649 | .fn_proto_one => { |
| 2650 | var params: [1]ast.Node.Index = undefined; |
| 2651 | try astgen.fnDecl(gz, &wip_decls, body, tree.fnProtoOne(&params, fn_proto)); |
| 2652 | continue; |
| 2653 | }, |
| 2654 | .fn_proto => { |
| 2655 | try astgen.fnDecl(gz, &wip_decls, body, tree.fnProto(fn_proto)); |
| 2656 | continue; |
| 2657 | }, |
| 2658 | else => unreachable, |
| 2659 | } |
| 2660 | }, |
| 2661 | .fn_proto_simple => { |
| 2662 | var params: [1]ast.Node.Index = undefined; |
| 2663 | try astgen.fnDecl(gz, &wip_decls, 0, tree.fnProtoSimple(&params, member_node)); |
| 2664 | continue; |
| 2665 | }, |
| 2666 | .fn_proto_multi => { |
| 2667 | try astgen.fnDecl(gz, &wip_decls, 0, tree.fnProtoMulti(member_node)); |
| 2668 | continue; |
| 2669 | }, |
| 2670 | .fn_proto_one => { |
| 2671 | var params: [1]ast.Node.Index = undefined; |
| 2672 | try astgen.fnDecl(gz, &wip_decls, 0, tree.fnProtoOne(&params, member_node)); |
| 2673 | continue; |
| 2674 | }, |
| 2675 | .fn_proto => { |
| 2676 | try astgen.fnDecl(gz, &wip_decls, 0, tree.fnProto(member_node)); |
| 2677 | continue; |
| 2678 | }, |
| 2679 | |
| 2680 | .global_var_decl => { |
| 2681 | try astgen.globalVarDecl(gz, scope, &wip_decls, member_node, tree.globalVarDecl(member_node)); |
| 2682 | continue; |
| 2683 | }, |
| 2684 | .local_var_decl => { |
| 2685 | try astgen.globalVarDecl(gz, scope, &wip_decls, member_node, tree.localVarDecl(member_node)); |
| 2686 | continue; |
| 2687 | }, |
| 2688 | .simple_var_decl => { |
| 2689 | try astgen.globalVarDecl(gz, scope, &wip_decls, member_node, tree.simpleVarDecl(member_node)); |
| 2690 | continue; |
| 2691 | }, |
| 2692 | .aligned_var_decl => { |
| 2693 | try astgen.globalVarDecl(gz, scope, &wip_decls, member_node, tree.alignedVarDecl(member_node)); |
| 2694 | continue; |
| 2695 | }, |
| 2696 | |
| 2697 | .@"comptime" => { |
| 2698 | try astgen.comptimeDecl(gz, scope, member_node); |
| 2699 | continue; |
| 2700 | }, |
| 2701 | .@"usingnamespace" => { |
| 2702 | try astgen.usingnamespaceDecl(gz, scope, member_node); |
| 2703 | continue; |
| 2704 | }, |
| 2705 | .test_decl => { |
| 2706 | try astgen.testDecl(gz, scope, member_node); |
| 2707 | continue; |
| 2708 | }, |
| 2709 | else => unreachable, |
| 2643 | 2710 | }; |
| 2644 | 2711 | if (field_index % 32 == 0 and field_index != 0) { |
| 2645 | 2712 | try bit_bag.append(gpa, cur_bit_bag); |
| ... | ... | @@ -2663,27 +2730,45 @@ fn containerDecl( |
| 2663 | 2730 | |
| 2664 | 2731 | field_index += 1; |
| 2665 | 2732 | } |
| 2666 | | const empty_slot_count = 32 - (field_index % 32); |
| 2667 | | cur_bit_bag >>= @intCast(u5, empty_slot_count); |
| 2733 | { |
| 2734 | const empty_slot_count = 32 - (field_index % 32); |
| 2735 | cur_bit_bag >>= @intCast(u5, empty_slot_count); |
| 2736 | } |
| 2737 | |
| 2738 | if (wip_decls.decl_index != 0) { |
| 2739 | const empty_slot_count = 16 - (wip_decls.decl_index % 16); |
| 2740 | wip_decls.cur_bit_bag >>= @intCast(u5, empty_slot_count * 2); |
| 2741 | } |
| 2668 | 2742 | |
| 2669 | 2743 | const decl_inst = try gz.addBlock(tag, node); |
| 2670 | 2744 | try gz.instructions.append(gpa, decl_inst); |
| 2671 | | _ = try block_scope.addBreak(.break_inline, decl_inst, .void_value); |
| 2745 | if (block_scope.instructions.items.len != 0) { |
| 2746 | _ = try block_scope.addBreak(.break_inline, decl_inst, .void_value); |
| 2747 | } |
| 2672 | 2748 | |
| 2673 | | try astgen.extra.ensureCapacity(gpa, astgen.extra.items.len + |
| 2674 | | @typeInfo(Zir.Inst.EnumDecl).Struct.fields.len + |
| 2749 | try astgen.extra.ensureUnusedCapacity(gpa, @typeInfo(Zir.Inst.EnumDecl).Struct.fields.len + |
| 2675 | 2750 | bit_bag.items.len + 1 + fields_data.items.len + |
| 2676 | | block_scope.instructions.items.len); |
| 2751 | block_scope.instructions.items.len + |
| 2752 | wip_decls.bit_bag.items.len + @boolToInt(wip_decls.decl_index != 0) + |
| 2753 | wip_decls.name_and_value.items.len); |
| 2677 | 2754 | const zir_datas = astgen.instructions.items(.data); |
| 2678 | 2755 | zir_datas[decl_inst].pl_node.payload_index = astgen.addExtraAssumeCapacity(Zir.Inst.EnumDecl{ |
| 2679 | 2756 | .tag_type = arg_inst, |
| 2680 | 2757 | .body_len = @intCast(u32, block_scope.instructions.items.len), |
| 2681 | 2758 | .fields_len = @intCast(u32, field_index), |
| 2759 | .decls_len = @intCast(u32, wip_decls.decl_index), |
| 2682 | 2760 | }); |
| 2683 | 2761 | astgen.extra.appendSliceAssumeCapacity(block_scope.instructions.items); |
| 2684 | 2762 | astgen.extra.appendSliceAssumeCapacity(bit_bag.items); // Likely empty. |
| 2685 | 2763 | astgen.extra.appendAssumeCapacity(cur_bit_bag); |
| 2686 | 2764 | astgen.extra.appendSliceAssumeCapacity(fields_data.items); |
| 2765 | |
| 2766 | astgen.extra.appendSliceAssumeCapacity(wip_decls.bit_bag.items); // Likely empty. |
| 2767 | if (wip_decls.decl_index != 0) { |
| 2768 | astgen.extra.appendAssumeCapacity(wip_decls.cur_bit_bag); |
| 2769 | } |
| 2770 | astgen.extra.appendSliceAssumeCapacity(wip_decls.name_and_value.items); |
| 2771 | |
| 2687 | 2772 | return rvalue(gz, scope, rl, gz.indexToRef(decl_inst), node); |
| 2688 | 2773 | }, |
| 2689 | 2774 | .keyword_opaque => { |