| ... | ... | @@ -1485,6 +1485,7 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool { |
| 1485 | 1485 | const type_node = var_decl.getTrailer("type_node") orelse |
| 1486 | 1486 | break :blk null; |
| 1487 | 1487 | |
| 1488 | // Temporary arena for the zir instructions. |
| 1488 | 1489 | var type_scope_arena = std.heap.ArenaAllocator.init(self.gpa); |
| 1489 | 1490 | defer type_scope_arena.deinit(); |
| 1490 | 1491 | var type_scope: Scope.GenZIR = .{ |
| ... | ... | @@ -1539,7 +1540,8 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool { |
| 1539 | 1540 | try self.coerce(&inner_block.base, some, ret.operand) |
| 1540 | 1541 | else |
| 1541 | 1542 | ret.operand; |
| 1542 | | const val = try self.resolveConstValue(&inner_block.base, coerced); |
| 1543 | const val = coerced.value() orelse |
| 1544 | return self.fail(&block_scope.base, inst.src, "unable to resolve comptime value", .{}); |
| 1543 | 1545 | |
| 1544 | 1546 | var_type = explicit_type orelse try ret.operand.ty.copy(block_scope.arena); |
| 1545 | 1547 | break :blk try val.copy(block_scope.arena); |
| ... | ... | @@ -1603,7 +1605,41 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool { |
| 1603 | 1605 | } |
| 1604 | 1606 | return type_changed; |
| 1605 | 1607 | }, |
| 1606 | | .Comptime => @panic("TODO comptime decl"), |
| 1608 | .Comptime => { |
| 1609 | const comptime_decl = @fieldParentPtr(ast.Node.Comptime, "base", ast_node); |
| 1610 | |
| 1611 | decl.analysis = .in_progress; |
| 1612 | |
| 1613 | // A comptime decl does not store any value so we can just deinit this arena after analysis is done. |
| 1614 | var analysis_arena = std.heap.ArenaAllocator.init(self.gpa); |
| 1615 | defer analysis_arena.deinit(); |
| 1616 | var gen_scope: Scope.GenZIR = .{ |
| 1617 | .decl = decl, |
| 1618 | .arena = &analysis_arena.allocator, |
| 1619 | .parent = decl.scope, |
| 1620 | }; |
| 1621 | defer gen_scope.instructions.deinit(self.gpa); |
| 1622 | |
| 1623 | // TODO comptime scope here |
| 1624 | _ = try astgen.expr(self, &gen_scope.base, .none, comptime_decl.expr); |
| 1625 | |
| 1626 | var block_scope: Scope.Block = .{ |
| 1627 | .parent = null, |
| 1628 | .func = null, |
| 1629 | .decl = decl, |
| 1630 | .instructions = .{}, |
| 1631 | .arena = &analysis_arena.allocator, |
| 1632 | }; |
| 1633 | defer block_scope.instructions.deinit(self.gpa); |
| 1634 | |
| 1635 | _ = try zir_sema.analyzeBody(self, &block_scope.base, .{ |
| 1636 | .instructions = gen_scope.instructions.items, |
| 1637 | }); |
| 1638 | |
| 1639 | decl.analysis = .complete; |
| 1640 | decl.generation = self.generation; |
| 1641 | return true; |
| 1642 | }, |
| 1607 | 1643 | .Use => @panic("TODO usingnamespace decl"), |
| 1608 | 1644 | else => unreachable, |
| 1609 | 1645 | } |
| ... | ... | @@ -1794,7 +1830,16 @@ fn analyzeRootSrcFile(self: *Module, root_scope: *Scope.File) !void { |
| 1794 | 1830 | } |
| 1795 | 1831 | } |
| 1796 | 1832 | } else if (src_decl.castTag(.Comptime)) |comptime_node| { |
| 1797 | | log.err("TODO: analyze comptime decl", .{}); |
| 1833 | const name_index = self.getNextAnonNameIndex(); |
| 1834 | const name = try std.fmt.allocPrint(self.gpa, "__comptime_{}", .{name_index}); |
| 1835 | defer self.gpa.free(name); |
| 1836 | |
| 1837 | const name_hash = root_scope.fullyQualifiedNameHash(name); |
| 1838 | const contents_hash = std.zig.hashSrc(tree.getNodeSource(src_decl)); |
| 1839 | |
| 1840 | const new_decl = try self.createNewDecl(&root_scope.base, name, decl_i, name_hash, contents_hash); |
| 1841 | root_scope.decls.appendAssumeCapacity(new_decl); |
| 1842 | self.work_queue.writeItemAssumeCapacity(.{ .analyze_decl = new_decl }); |
| 1798 | 1843 | } else if (src_decl.castTag(.ContainerField)) |container_field| { |
| 1799 | 1844 | log.err("TODO: analyze container field", .{}); |
| 1800 | 1845 | } else if (src_decl.castTag(.TestDecl)) |test_decl| { |