Merge pull request #9873 from SpexGuy/fix-struct-namespaces

Stage 2: A bunch of cleaning up
This commit is contained in:
Andrew Kelley 2021-10-02 16:25:15 -04:00 committed by GitHub
commit ac52e00564
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 1813 additions and 1888 deletions

View File

@ -54,7 +54,7 @@ c_object_work_queue: std.fifo.LinearFifo(*CObject, .Dynamic),
/// These jobs are to tokenize, parse, and astgen files, which may be outdated
/// since the last compilation, as well as scan for `@import` and queue up
/// additional jobs corresponding to those new files.
astgen_work_queue: std.fifo.LinearFifo(*Module.Scope.File, .Dynamic),
astgen_work_queue: std.fifo.LinearFifo(*Module.File, .Dynamic),
/// The ErrorMsg memory is owned by the `CObject`, using Compilation's general purpose allocator.
/// This data is accessed by multiple threads and is protected by `mutex`.
@ -446,7 +446,7 @@ pub const AllErrors = struct {
pub fn addZir(
arena: *Allocator,
errors: *std.ArrayList(Message),
file: *Module.Scope.File,
file: *Module.File,
) !void {
assert(file.zir_loaded);
assert(file.tree_loaded);
@ -1444,7 +1444,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
.emit_docs = options.emit_docs,
.work_queue = std.fifo.LinearFifo(Job, .Dynamic).init(gpa),
.c_object_work_queue = std.fifo.LinearFifo(*CObject, .Dynamic).init(gpa),
.astgen_work_queue = std.fifo.LinearFifo(*Module.Scope.File, .Dynamic).init(gpa),
.astgen_work_queue = std.fifo.LinearFifo(*Module.File, .Dynamic).init(gpa),
.keep_source_files_loaded = options.keep_source_files_loaded,
.use_clang = use_clang,
.clang_argv = options.clang_argv,
@ -1770,7 +1770,7 @@ pub fn update(self: *Compilation) !void {
assert(decl.deletion_flag);
assert(decl.dependants.count() == 0);
const is_anon = if (decl.zir_decl_index == 0) blk: {
break :blk decl.namespace.anon_decls.swapRemove(decl);
break :blk decl.src_namespace.anon_decls.swapRemove(decl);
} else false;
try module.clearDecl(decl, null);
@ -1889,13 +1889,13 @@ pub fn totalErrorCount(self: *Compilation) usize {
// the previous parse success, including compile errors, but we cannot
// emit them until the file succeeds parsing.
for (module.failed_decls.keys()) |key| {
if (key.namespace.file_scope.okToReportErrors()) {
if (key.getFileScope().okToReportErrors()) {
total += 1;
}
}
if (module.emit_h) |emit_h| {
for (emit_h.failed_decls.keys()) |key| {
if (key.namespace.file_scope.okToReportErrors()) {
if (key.getFileScope().okToReportErrors()) {
total += 1;
}
}
@ -1968,7 +1968,7 @@ pub fn getAllErrorsAlloc(self: *Compilation) !AllErrors {
while (it.next()) |entry| {
// Skip errors for Decls within files that had a parse failure.
// We'll try again once parsing succeeds.
if (entry.key_ptr.*.namespace.file_scope.okToReportErrors()) {
if (entry.key_ptr.*.getFileScope().okToReportErrors()) {
try AllErrors.add(module, &arena, &errors, entry.value_ptr.*.*);
}
}
@ -1978,7 +1978,7 @@ pub fn getAllErrorsAlloc(self: *Compilation) !AllErrors {
while (it.next()) |entry| {
// Skip errors for Decls within files that had a parse failure.
// We'll try again once parsing succeeds.
if (entry.key_ptr.*.namespace.file_scope.okToReportErrors()) {
if (entry.key_ptr.*.getFileScope().okToReportErrors()) {
try AllErrors.add(module, &arena, &errors, entry.value_ptr.*.*);
}
}
@ -2169,12 +2169,12 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor
defer air.deinit(gpa);
log.debug("analyze liveness of {s}", .{decl.name});
var liveness = try Liveness.analyze(gpa, air, decl.namespace.file_scope.zir);
var liveness = try Liveness.analyze(gpa, air, decl.getFileScope().zir);
defer liveness.deinit(gpa);
if (builtin.mode == .Debug and self.verbose_air) {
std.debug.print("# Begin Function AIR: {s}:\n", .{decl.name});
@import("print_air.zig").dump(gpa, air, decl.namespace.file_scope.zir, liveness);
@import("print_air.zig").dump(gpa, air, decl.getFileScope().zir, liveness);
std.debug.print("# End Function AIR: {s}\n\n", .{decl.name});
}
@ -2465,14 +2465,14 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor
const AstGenSrc = union(enum) {
root,
import: struct {
importing_file: *Module.Scope.File,
importing_file: *Module.File,
import_tok: std.zig.Ast.TokenIndex,
},
};
fn workerAstGenFile(
comp: *Compilation,
file: *Module.Scope.File,
file: *Module.File,
prog_node: *std.Progress.Node,
wg: *WaitGroup,
src: AstGenSrc,
@ -2742,7 +2742,7 @@ fn reportRetryableCObjectError(
fn reportRetryableAstGenError(
comp: *Compilation,
src: AstGenSrc,
file: *Module.Scope.File,
file: *Module.File,
err: anyerror,
) error{OutOfMemory}!void {
const mod = comp.bin_file.options.module.?;

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -2584,7 +2584,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
fn genArgDbgInfo(self: *Self, inst: Air.Inst.Index, mcv: MCValue) !void {
const ty_str = self.air.instructions.items(.data)[inst].ty_str;
const zir = &self.mod_fn.owner_decl.namespace.file_scope.zir;
const zir = &self.mod_fn.owner_decl.getFileScope().zir;
const name = zir.nullTerminatedString(ty_str.str);
const name_with_null = name.ptr[0 .. name.len + 1];
const ty = self.air.getRefType(ty_str.ty);
@ -3834,7 +3834,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
fn airAsm(self: *Self, inst: Air.Inst.Index) !void {
const air_datas = self.air.instructions.items(.data);
const air_extra = self.air.extraData(Air.Asm, air_datas[inst].ty_pl.payload);
const zir = self.mod_fn.owner_decl.namespace.file_scope.zir;
const zir = self.mod_fn.owner_decl.getFileScope().zir;
const extended = zir.instructions.items(.data)[air_extra.data.zir_index].extended;
const zir_extra = zir.extraData(Zir.Inst.Asm, extended.operand);
const asm_source = zir.nullTerminatedString(zir_extra.data.asm_source);

View File

@ -246,7 +246,7 @@ pub const DeclGen = struct {
fn fail(dg: *DeclGen, comptime format: []const u8, args: anytype) error{ AnalysisFail, OutOfMemory } {
@setCold(true);
const src: LazySrcLoc = .{ .node_offset = 0 };
const src_loc = src.toSrcLocWithDecl(dg.decl);
const src_loc = src.toSrcLoc(dg.decl);
dg.error_msg = try Module.ErrorMsg.create(dg.module.gpa, src_loc, format, args);
return error.AnalysisFail;
}
@ -1696,7 +1696,7 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index) !CValue {
fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
const air_datas = f.air.instructions.items(.data);
const air_extra = f.air.extraData(Air.Asm, air_datas[inst].ty_pl.payload);
const zir = f.object.dg.decl.namespace.file_scope.zir;
const zir = f.object.dg.decl.getFileScope().zir;
const extended = zir.instructions.items(.data)[air_extra.data.zir_index].extended;
const zir_extra = zir.extraData(Zir.Inst.Asm, extended.operand);
const asm_source = zir.nullTerminatedString(zir_extra.data.asm_source);

View File

@ -557,7 +557,7 @@ pub const DeclGen = struct {
fn todo(self: *DeclGen, comptime format: []const u8, args: anytype) error{ OutOfMemory, CodegenFail } {
@setCold(true);
assert(self.err_msg == null);
const src_loc = @as(LazySrcLoc, .{ .node_offset = 0 }).toSrcLocWithDecl(self.decl);
const src_loc = @as(LazySrcLoc, .{ .node_offset = 0 }).toSrcLoc(self.decl);
self.err_msg = try Module.ErrorMsg.create(self.gpa, src_loc, "TODO (LLVM): " ++ format, args);
return error.CodegenFail;
}
@ -1819,7 +1819,7 @@ pub const FuncGen = struct {
const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
const air_asm = self.air.extraData(Air.Asm, ty_pl.payload);
const zir = self.dg.decl.namespace.file_scope.zir;
const zir = self.dg.decl.getFileScope().zir;
const extended = zir.instructions.items(.data)[air_asm.data.zir_index].extended;
const zir_extra = zir.extraData(Zir.Inst.Asm, extended.operand);
const asm_source = zir.nullTerminatedString(zir_extra.data.asm_source);

View File

@ -139,7 +139,7 @@ pub const SPIRVModule = struct {
}
fn resolveSourceFileName(self: *SPIRVModule, decl: *Decl) !ResultId {
const path = decl.namespace.file_scope.sub_file_path;
const path = decl.getFileScope().sub_file_path;
const result = try self.file_names.getOrPut(path);
if (!result.found_existing) {
result.value_ptr.* = self.allocResultId();
@ -295,7 +295,7 @@ pub const DeclGen = struct {
fn fail(self: *DeclGen, comptime format: []const u8, args: anytype) Error {
@setCold(true);
const src: LazySrcLoc = .{ .node_offset = 0 };
const src_loc = src.toSrcLocWithDecl(self.decl);
const src_loc = src.toSrcLoc(self.decl);
self.error_msg = try Module.ErrorMsg.create(self.spv.module.gpa, src_loc, format, args);
return error.AnalysisFail;
}

View File

@ -540,7 +540,7 @@ pub const Context = struct {
/// Sets `err_msg` on `Context` and returns `error.CodegemFail` which is caught in link/Wasm.zig
fn fail(self: *Context, comptime fmt: []const u8, args: anytype) InnerError {
const src: LazySrcLoc = .{ .node_offset = 0 };
const src_loc = src.toSrcLocWithDecl(self.decl);
const src_loc = src.toSrcLoc(self.decl);
self.err_msg = try Module.ErrorMsg.create(self.gpa, src_loc, fmt, args);
return error.CodegenFail;
}

View File

@ -8,6 +8,7 @@ const print_zir = @import("print_zir.zig");
const Module = @import("Module.zig");
const Sema = @import("Sema.zig");
const Zir = @import("Zir.zig");
const Decl = Module.Decl;
pub const is_enabled = builtin.mode == .Debug;
@ -36,7 +37,7 @@ fn en(val: anytype) En(@TypeOf(val)) {
pub const AnalyzeBody = struct {
parent: if (is_enabled) ?*AnalyzeBody else void,
sema: En(*Sema),
block: En(*Module.Scope.Block),
block: En(*Sema.Block),
body: En([]const Zir.Inst.Index),
body_index: En(usize),
@ -64,7 +65,7 @@ pub const AnalyzeBody = struct {
threadlocal var zir_state: ?*AnalyzeBody = if (is_enabled) null else @compileError("Cannot use zir_state if crash_report is disabled.");
pub fn prepAnalyzeBody(sema: *Sema, block: *Module.Scope.Block, body: []const Zir.Inst.Index) AnalyzeBody {
pub fn prepAnalyzeBody(sema: *Sema, block: *Sema.Block, body: []const Zir.Inst.Index) AnalyzeBody {
if (is_enabled) {
return .{
.parent = null,
@ -87,7 +88,7 @@ fn dumpStatusReport() !void {
const allocator = &fba.allocator;
const stderr = io.getStdErr().writer();
const block: *Scope.Block = anal.block;
const block: *Sema.Block = anal.block;
try stderr.writeAll("Analyzing ");
try writeFullyQualifiedDeclWithFile(block.src_decl, stderr);
@ -97,7 +98,7 @@ fn dumpStatusReport() !void {
allocator,
anal.body,
anal.body_index,
block.src_decl.getFileScope(),
block.namespace.file_scope,
block.src_decl.src_node,
6, // indent
stderr,
@ -106,7 +107,7 @@ fn dumpStatusReport() !void {
else => |e| return e,
};
try stderr.writeAll(" For full context, use the command\n zig ast-check -t ");
try writeFilePath(block.src_decl.getFileScope(), stderr);
try writeFilePath(block.namespace.file_scope, stderr);
try stderr.writeAll("\n\n");
var parent = anal.parent;
@ -118,7 +119,7 @@ fn dumpStatusReport() !void {
print_zir.renderSingleInstruction(
allocator,
curr.body[curr.body_index],
curr.block.src_decl.getFileScope(),
curr.block.namespace.file_scope,
curr.block.src_decl.src_node,
6, // indent
stderr,
@ -134,12 +135,9 @@ fn dumpStatusReport() !void {
try stderr.writeAll("\n");
}
const Scope = Module.Scope;
const Decl = Module.Decl;
var crash_heap: [16 * 4096]u8 = undefined;
fn writeFilePath(file: *Scope.File, stream: anytype) !void {
fn writeFilePath(file: *Module.File, stream: anytype) !void {
if (file.pkg.root_src_directory.path) |path| {
try stream.writeAll(path);
try stream.writeAll(std.fs.path.sep_str);
@ -150,7 +148,7 @@ fn writeFilePath(file: *Scope.File, stream: anytype) !void {
fn writeFullyQualifiedDeclWithFile(decl: *Decl, stream: anytype) !void {
try writeFilePath(decl.getFileScope(), stream);
try stream.writeAll(": ");
try decl.namespace.renderFullyQualifiedName(std.mem.sliceTo(decl.name, 0), stream);
try decl.renderFullyQualifiedDebugName(stream);
}
fn compilerPanic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace) noreturn {

View File

@ -57,7 +57,7 @@ path_arena: std.heap.ArenaAllocator,
/// of the function to know what file it came from.
/// If we group the decls by file, it makes it really easy to do this (put the symbol in the correct place)
fn_decl_table: std.AutoArrayHashMapUnmanaged(
*Module.Scope.File,
*Module.File,
struct { sym_index: u32, functions: std.AutoArrayHashMapUnmanaged(*Module.Decl, FnDeclOutput) = .{} },
) = .{},
data_decl_table: std.AutoArrayHashMapUnmanaged(*Module.Decl, []const u8) = .{},
@ -163,11 +163,11 @@ pub fn createEmpty(gpa: *Allocator, options: link.Options) !*Plan9 {
fn putFn(self: *Plan9, decl: *Module.Decl, out: FnDeclOutput) !void {
const gpa = self.base.allocator;
const fn_map_res = try self.fn_decl_table.getOrPut(gpa, decl.namespace.file_scope);
const fn_map_res = try self.fn_decl_table.getOrPut(gpa, decl.getFileScope());
if (fn_map_res.found_existing) {
try fn_map_res.value_ptr.functions.put(gpa, decl, out);
} else {
const file = decl.namespace.file_scope;
const file = decl.getFileScope();
const arena = &self.path_arena.allocator;
// each file gets a symbol
fn_map_res.value_ptr.* = .{
@ -548,7 +548,7 @@ pub fn freeDecl(self: *Plan9, decl: *Module.Decl) void {
const is_fn = (decl.val.tag() == .function);
if (is_fn) {
var symidx_and_submap =
self.fn_decl_table.get(decl.namespace.file_scope).?;
self.fn_decl_table.get(decl.getFileScope()).?;
var submap = symidx_and_submap.functions;
_ = submap.swapRemove(decl);
if (submap.count() == 0) {

View File

@ -3233,7 +3233,7 @@ pub fn cmdFmt(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !voi
const Module = @import("Module.zig");
const AstGen = @import("AstGen.zig");
var file: Module.Scope.File = .{
var file: Module.File = .{
.status = .never_loaded,
.source_loaded = true,
.zir_loaded = false,
@ -3429,7 +3429,7 @@ fn fmtPathFile(
const Module = @import("Module.zig");
const AstGen = @import("AstGen.zig");
var file: Module.Scope.File = .{
var file: Module.File = .{
.status = .never_loaded,
.source_loaded = true,
.zir_loaded = false,
@ -4019,7 +4019,7 @@ pub fn cmdAstCheck(
}
}
var file: Module.Scope.File = .{
var file: Module.File = .{
.status = .never_loaded,
.source_loaded = false,
.tree_loaded = false,
@ -4170,7 +4170,7 @@ pub fn cmdChangelist(
if (stat.size > max_src_size)
return error.FileTooBig;
var file: Module.Scope.File = .{
var file: Module.File = .{
.status = .never_loaded,
.source_loaded = false,
.tree_loaded = false,

View File

@ -11,7 +11,7 @@ const LazySrcLoc = Module.LazySrcLoc;
/// Write human-readable, debug formatted ZIR code to a file.
pub fn renderAsTextToFile(
gpa: *Allocator,
scope_file: *Module.Scope.File,
scope_file: *Module.File,
fs_file: std.fs.File,
) !void {
var arena = std.heap.ArenaAllocator.init(gpa);
@ -64,7 +64,7 @@ pub fn renderInstructionContext(
gpa: *Allocator,
block: []const Zir.Inst.Index,
block_index: usize,
scope_file: *Module.Scope.File,
scope_file: *Module.File,
parent_decl_node: Ast.Node.Index,
indent: u32,
stream: anytype,
@ -96,7 +96,7 @@ pub fn renderInstructionContext(
pub fn renderSingleInstruction(
gpa: *Allocator,
inst: Zir.Inst.Index,
scope_file: *Module.Scope.File,
scope_file: *Module.File,
parent_decl_node: Ast.Node.Index,
indent: u32,
stream: anytype,
@ -122,7 +122,7 @@ pub fn renderSingleInstruction(
const Writer = struct {
gpa: *Allocator,
arena: *Allocator,
file: *Module.Scope.File,
file: *Module.File,
code: Zir,
indent: u32,
parent_decl_node: Ast.Node.Index,

View File

@ -3067,7 +3067,7 @@ pub const Type = extern union {
}
/// Returns null if the type has no namespace.
pub fn getNamespace(self: Type) ?*Module.Scope.Namespace {
pub fn getNamespace(self: Type) ?*Module.Namespace {
return switch (self.tag()) {
.@"struct" => &self.castTag(.@"struct").?.data.namespace,
.enum_full => &self.castTag(.enum_full).?.data.namespace,
@ -3833,12 +3833,12 @@ pub const Type = extern union {
/// Most commonly used for files.
pub const ContainerScope = struct {
base: Payload,
data: *Module.Scope.Namespace,
data: *Module.Namespace,
};
pub const Opaque = struct {
base: Payload = .{ .tag = .@"opaque" },
data: Module.Scope.Namespace,
data: Module.Namespace,
};
pub const Struct = struct {

View File

@ -1238,9 +1238,24 @@ pub const Value = extern union {
const b_field_index = b.castTag(.enum_field_index).?.data;
return a_field_index == b_field_index;
},
.elem_ptr => @panic("TODO: Implement more pointer eql cases"),
.field_ptr => @panic("TODO: Implement more pointer eql cases"),
.eu_payload_ptr => @panic("TODO: Implement more pointer eql cases"),
.opt_payload_ptr => @panic("TODO: Implement more pointer eql cases"),
else => {},
}
}
if (a.pointerDecl()) |a_decl| {
if (b.pointerDecl()) |b_decl| {
return a_decl == b_decl;
} else {
return false;
}
} else if (b.pointerDecl()) |_| {
return false;
}
if (ty.zigTypeTag() == .Type) {
var buf_a: ToTypeBuffer = undefined;
var buf_b: ToTypeBuffer = undefined;
@ -1285,8 +1300,28 @@ pub const Value = extern union {
const float = val.toFloat(f128);
std.hash.autoHash(hasher, @bitCast(u128, float));
},
.Pointer => {
@panic("TODO implement hashing pointer values");
.Pointer => switch (val.tag()) {
.decl_ref_mut,
.extern_fn,
.decl_ref,
.function,
.variable,
=> std.hash.autoHash(hasher, val.pointerDecl().?),
.elem_ptr => @panic("TODO: Implement more pointer hashing cases"),
.field_ptr => @panic("TODO: Implement more pointer hashing cases"),
.eu_payload_ptr => @panic("TODO: Implement more pointer hashing cases"),
.opt_payload_ptr => @panic("TODO: Implement more pointer hashing cases"),
.zero,
.one,
.int_u64,
.int_i64,
.int_big_positive,
.int_big_negative,
=> @panic("TODO: Implement pointer hashing for int pointers"),
else => unreachable,
},
.Array, .Vector => {
@panic("TODO implement hashing array/vector values");
@ -1432,6 +1467,19 @@ pub const Value = extern union {
return sub_val;
}
/// Gets the decl referenced by this pointer. If the pointer does not point
/// to a decl, or if it points to some part of a decl (like field_ptr or element_ptr),
/// this function returns null.
pub fn pointerDecl(self: Value) ?*Module.Decl {
return switch (self.tag()) {
.decl_ref_mut => self.castTag(.decl_ref_mut).?.data.decl,
.extern_fn, .decl_ref => self.cast(Payload.Decl).?.data,
.function => self.castTag(.function).?.data.owner_decl,
.variable => self.castTag(.variable).?.data.owner_decl,
else => null,
};
}
pub fn sliceLen(val: Value) u64 {
return switch (val.tag()) {
.empty_array => 0,