Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions lib/std/zig/AstGen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9098,15 +9098,16 @@ fn typeOf(
return astgen.failNode(node, "expected at least 1 argument, found 0", .{});
}
const gpa = astgen.gpa;

var typeof_scope = gz.makeSubBlock(scope);
typeof_scope.is_comptime = false;
typeof_scope.is_typeof = true;
typeof_scope.c_import = false;
defer typeof_scope.unstack();

if (args.len == 1) {
const typeof_inst = try gz.makeBlockInst(.typeof_builtin, node);

var typeof_scope = gz.makeSubBlock(scope);
typeof_scope.is_comptime = false;
typeof_scope.is_typeof = true;
typeof_scope.c_import = false;
defer typeof_scope.unstack();

const ty_expr = try reachableExpr(&typeof_scope, &typeof_scope.base, .{ .rl = .none }, args[0], node);
if (!gz.refIsNoReturn(ty_expr)) {
_ = try typeof_scope.addBreak(.break_inline, typeof_inst, ty_expr);
Expand All @@ -9123,9 +9124,6 @@ fn typeOf(

const typeof_inst = try gz.addExtendedMultiOpPayloadIndex(.typeof_peer, payload_index, args.len);

var typeof_scope = gz.makeSubBlock(scope);
typeof_scope.is_comptime = false;

for (args, 0..) |arg, i| {
const param_ref = try reachableExpr(&typeof_scope, &typeof_scope.base, .{ .rl = .none }, arg, node);
astgen.extra.items[args_index + i] = @intFromEnum(param_ref);
Expand All @@ -9141,7 +9139,6 @@ fn typeOf(
});
try astgen.extra.ensureUnusedCapacity(gpa, body_len);
astgen.appendBodyWithFixups(body);
typeof_scope.unstack();

return rvalue(gz, ri, typeof_inst, node);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Air/print.zig
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ const Writer = struct {
}

fn writeType(w: *Writer, s: *std.Io.Writer, ty: Type) !void {
return ty.print(s, w.pt);
return ty.print(s, w.pt, null);
}

fn writeTy(w: *Writer, s: *std.Io.Writer, inst: Air.Inst.Index) Error!void {
Expand Down
812 changes: 468 additions & 344 deletions src/Sema.zig

Large diffs are not rendered by default.

53 changes: 35 additions & 18 deletions src/Type.zig
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,33 @@ pub fn fmt(ty: Type, pt: Zcu.PerThread) Formatter {
return .{ .data = .{
.ty = ty,
.pt = pt,
.opt_sema = null,
} };
}

pub fn fmtSema(ty: Type, pt: Zcu.PerThread, sema: *Sema) Formatter {
return .{ .data = .{
.ty = ty,
.pt = pt,
.opt_sema = sema,
} };
}

pub fn fmtOptSema(ty: Type, pt: Zcu.PerThread, opt_sema: ?*Sema) Formatter {
return .{ .data = .{
.ty = ty,
.pt = pt,
.opt_sema = opt_sema,
} };
}

const Format = struct {
ty: Type,
pt: Zcu.PerThread,
opt_sema: ?*Sema,

fn default(f: Format, writer: *std.Io.Writer) std.Io.Writer.Error!void {
return print(f.ty, writer, f.pt);
return print(f.ty, writer, f.pt, f.opt_sema);
}
};

Expand All @@ -156,8 +174,7 @@ pub fn dump(start_type: Type, writer: *std.Io.Writer) std.Io.Writer.Error!void {
}

/// Prints a name suitable for `@typeName`.
/// TODO: take an `opt_sema` to pass to `fmtValue` when printing sentinels.
pub fn print(ty: Type, writer: *std.Io.Writer, pt: Zcu.PerThread) std.Io.Writer.Error!void {
pub fn print(ty: Type, writer: *std.Io.Writer, pt: Zcu.PerThread, opt_sema: ?*Sema) std.Io.Writer.Error!void {
const zcu = pt.zcu;
const ip = &zcu.intern_pool;
switch (ip.indexToKey(ty.toIntern())) {
Expand All @@ -174,8 +191,8 @@ pub fn print(ty: Type, writer: *std.Io.Writer, pt: Zcu.PerThread) std.Io.Writer.

if (info.sentinel != .none) switch (info.flags.size) {
.one, .c => unreachable,
.many => try writer.print("[*:{f}]", .{Value.fromInterned(info.sentinel).fmtValue(pt)}),
.slice => try writer.print("[:{f}]", .{Value.fromInterned(info.sentinel).fmtValue(pt)}),
.many => try writer.print("[*:{f}]", .{Value.fromInterned(info.sentinel).fmtValueOptSema(pt, opt_sema)}),
.slice => try writer.print("[:{f}]", .{Value.fromInterned(info.sentinel).fmtValueOptSema(pt, opt_sema)}),
} else switch (info.flags.size) {
.one => try writer.writeAll("*"),
.many => try writer.writeAll("[*]"),
Expand Down Expand Up @@ -211,39 +228,39 @@ pub fn print(ty: Type, writer: *std.Io.Writer, pt: Zcu.PerThread) std.Io.Writer.
if (info.flags.is_const) try writer.writeAll("const ");
if (info.flags.is_volatile) try writer.writeAll("volatile ");

try print(Type.fromInterned(info.child), writer, pt);
try print(Type.fromInterned(info.child), writer, pt, opt_sema);
return;
},
.array_type => |array_type| {
if (array_type.sentinel == .none) {
try writer.print("[{d}]", .{array_type.len});
try print(Type.fromInterned(array_type.child), writer, pt);
try print(Type.fromInterned(array_type.child), writer, pt, opt_sema);
} else {
try writer.print("[{d}:{f}]", .{
array_type.len,
Value.fromInterned(array_type.sentinel).fmtValue(pt),
Value.fromInterned(array_type.sentinel).fmtValueOptSema(pt, opt_sema),
});
try print(Type.fromInterned(array_type.child), writer, pt);
try print(Type.fromInterned(array_type.child), writer, pt, opt_sema);
}
return;
},
.vector_type => |vector_type| {
try writer.print("@Vector({d}, ", .{vector_type.len});
try print(Type.fromInterned(vector_type.child), writer, pt);
try print(Type.fromInterned(vector_type.child), writer, pt, opt_sema);
try writer.writeAll(")");
return;
},
.opt_type => |child| {
try writer.writeByte('?');
return print(Type.fromInterned(child), writer, pt);
return print(Type.fromInterned(child), writer, pt, opt_sema);
},
.error_union_type => |error_union_type| {
try print(Type.fromInterned(error_union_type.error_set_type), writer, pt);
try print(Type.fromInterned(error_union_type.error_set_type), writer, pt, opt_sema);
try writer.writeByte('!');
if (error_union_type.payload_type == .generic_poison_type) {
try writer.writeAll("anytype");
} else {
try print(Type.fromInterned(error_union_type.payload_type), writer, pt);
try print(Type.fromInterned(error_union_type.payload_type), writer, pt, opt_sema);
}
return;
},
Expand Down Expand Up @@ -325,8 +342,8 @@ pub fn print(ty: Type, writer: *std.Io.Writer, pt: Zcu.PerThread) std.Io.Writer.
for (tuple.types.get(ip), tuple.values.get(ip), 0..) |field_ty, val, i| {
try writer.writeAll(if (i == 0) " " else ", ");
if (val != .none) try writer.writeAll("comptime ");
try print(Type.fromInterned(field_ty), writer, pt);
if (val != .none) try writer.print(" = {f}", .{Value.fromInterned(val).fmtValue(pt)});
try print(Type.fromInterned(field_ty), writer, pt, opt_sema);
if (val != .none) try writer.print(" = {f}", .{Value.fromInterned(val).fmtValueOptSema(pt, opt_sema)});
}
try writer.writeAll(" }");
},
Expand Down Expand Up @@ -362,7 +379,7 @@ pub fn print(ty: Type, writer: *std.Io.Writer, pt: Zcu.PerThread) std.Io.Writer.
if (param_ty == .generic_poison_type) {
try writer.writeAll("anytype");
} else {
try print(Type.fromInterned(param_ty), writer, pt);
try print(Type.fromInterned(param_ty), writer, pt, opt_sema);
}
}
if (fn_info.is_var_args) {
Expand All @@ -389,13 +406,13 @@ pub fn print(ty: Type, writer: *std.Io.Writer, pt: Zcu.PerThread) std.Io.Writer.
if (fn_info.return_type == .generic_poison_type) {
try writer.writeAll("anytype");
} else {
try print(Type.fromInterned(fn_info.return_type), writer, pt);
try print(Type.fromInterned(fn_info.return_type), writer, pt, opt_sema);
}
},
.anyframe_type => |child| {
if (child == .none) return writer.writeAll("anyframe");
try writer.writeAll("anyframe->");
return print(Type.fromInterned(child), writer, pt);
return print(Type.fromInterned(child), writer, pt, opt_sema);
},

// values, not types
Expand Down
Loading
Loading