air: parse anonymous type constructors iteratively
This commit is contained in:
1 parent
ba99ad6727
commit
c94e5a0868
4 files changed
+169
-64
No files matched your search
+127
-64
@@ -486,8 +486,33 @@ const Parser = struct {
|
||||
return decl;
|
||||
}
|
||||
|
||||
// Anonymous type syntax is a unary chain. Keep its pending delimiters
|
||||
// in arena storage rather than one native call frame per constructor.
|
||||
fn parseType(p: *Parser) Error!air.Type {
|
||||
const t = p.next();
|
||||
var pending: std.ArrayList(TypeFrame) = .empty;
|
||||
var result: air.Type = undefined;
|
||||
while (true) {
|
||||
const t = p.next();
|
||||
if (t.tag == .word and p.peek().tag == .lt) {
|
||||
switch (try p.beginCompoundType(t)) {
|
||||
.leaf => |ty| {
|
||||
result = ty;
|
||||
break;
|
||||
},
|
||||
.wrapper => |frame| {
|
||||
try pending.append(p.arena, frame);
|
||||
continue;
|
||||
},
|
||||
}
|
||||
}
|
||||
result = try p.parseLeafType(t);
|
||||
break;
|
||||
}
|
||||
while (pending.pop()) |frame| result = try p.finishCompoundType(frame, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
fn parseLeafType(p: *Parser, t: Token) Error!air.Type {
|
||||
switch (t.tag) {
|
||||
.global => {
|
||||
if (indexOf(p.type_names.items, t.text)) |i| return .{ .named = @intCast(i) };
|
||||
@@ -495,7 +520,6 @@ const Parser = struct {
|
||||
return .void;
|
||||
},
|
||||
.word => {
|
||||
if (p.peek().tag == .lt) return p.parseCompoundType(t);
|
||||
if (std.meta.stringToEnum(air.IntType, t.text)) |it| return .{ .int = it };
|
||||
if (std.mem.eql(u8, t.text, "bool")) return .bool;
|
||||
if (std.mem.eql(u8, t.text, "void")) return .void;
|
||||
@@ -534,62 +558,42 @@ const Parser = struct {
|
||||
}
|
||||
}
|
||||
|
||||
/// `local<T>`, `ref<T>`, `mut<T>`, `slice<T>`, `slice_mut<T>`, `array<T, N>` (spec 07 §8.8).
|
||||
fn parseCompoundType(p: *Parser, t: Token) Error!air.Type {
|
||||
const TypeWrapper = enum {
|
||||
local,
|
||||
ref,
|
||||
mut,
|
||||
slice,
|
||||
slice_mut,
|
||||
array,
|
||||
own,
|
||||
own_array,
|
||||
task,
|
||||
sender,
|
||||
receiver,
|
||||
mutex,
|
||||
guard,
|
||||
island,
|
||||
frozen,
|
||||
arc,
|
||||
iref,
|
||||
atomic,
|
||||
buffer,
|
||||
};
|
||||
const TypeFrame = struct { kind: TypeWrapper, inner_span: Span };
|
||||
const CompoundStart = union(enum) { leaf: air.Type, wrapper: TypeFrame };
|
||||
|
||||
fn beginCompoundType(p: *Parser, t: Token) Error!CompoundStart {
|
||||
if (eql(t.text, "file")) {
|
||||
try p.requireFeature("resources", t.span, "file");
|
||||
_ = try p.expect(.lt, "`<`");
|
||||
const mode = try p.expect(.word, "`read`, `write` or `append`");
|
||||
_ = try p.expect(.gt, "`>`");
|
||||
const Mode = enum { read, write, append };
|
||||
return .{ .resource = switch (std.meta.stringToEnum(Mode, mode.text) orelse return p.fail(mode, "`read`, `write` or `append`")) {
|
||||
return .{ .leaf = .{ .resource = switch (std.meta.stringToEnum(Mode, mode.text) orelse return p.fail(mode, "`read`, `write` or `append`")) {
|
||||
.read => .file_read,
|
||||
.write => .file_write,
|
||||
.append => .file_append,
|
||||
} };
|
||||
}
|
||||
if (eql(t.text, "own") or eql(t.text, "own_array")) {
|
||||
try p.requireFeature("heap", t.span, t.text);
|
||||
_ = try p.expect(.lt, "`<`");
|
||||
const inner_span = p.peek().span;
|
||||
const inner = try p.parseType();
|
||||
// Nested linear values need `linear_aggregates` (spec 03 §19.2).
|
||||
try p.gateLinearMember(inner, inner_span);
|
||||
_ = try p.expect(.gt, "`>`");
|
||||
const boxed = try p.arena.create(air.Type);
|
||||
boxed.* = inner;
|
||||
return if (eql(t.text, "own")) .{ .own = boxed } else .{ .own_array = boxed };
|
||||
}
|
||||
const Wrapper = enum { task, sender, receiver, mutex, guard, island, frozen, arc, iref, atomic, buffer };
|
||||
if (std.meta.stringToEnum(Wrapper, t.text)) |which| {
|
||||
try p.requireFeature(switch (which) {
|
||||
.task => "tasks",
|
||||
.sender, .receiver => "channels",
|
||||
.mutex, .guard => "sync",
|
||||
.island, .frozen, .iref => "islands",
|
||||
.arc => "arc",
|
||||
.atomic => "atomics",
|
||||
.buffer => "linear_buffers",
|
||||
}, t.span, t.text);
|
||||
_ = try p.expect(.lt, "`<`");
|
||||
// Element rules (spec 04 §38.1) are checked by the type stage.
|
||||
const inner = try p.parseType();
|
||||
_ = try p.expect(.gt, "`>`");
|
||||
const boxed = try p.arena.create(air.Type);
|
||||
boxed.* = inner;
|
||||
return switch (which) {
|
||||
.task => .{ .task = boxed },
|
||||
.sender => .{ .sender = boxed },
|
||||
.receiver => .{ .receiver = boxed },
|
||||
.mutex => .{ .mutex = boxed },
|
||||
.guard => .{ .guard = boxed },
|
||||
.island => .{ .island = boxed },
|
||||
.frozen => .{ .frozen = boxed },
|
||||
.arc => .{ .arc = boxed },
|
||||
.iref => .{ .iref = boxed },
|
||||
.atomic => .{ .atomic = boxed },
|
||||
.buffer => .{ .buffer = boxed },
|
||||
};
|
||||
} } };
|
||||
}
|
||||
if (eql(t.text, "cap")) {
|
||||
try p.requireFeature("capabilities", t.span, "cap");
|
||||
@@ -600,24 +604,36 @@ const Parser = struct {
|
||||
try p.report("E0004", k.span, "unknown capability kind", &.{.{ .name = "name", .value = .{ .str = k.text } }});
|
||||
break :blk 0;
|
||||
};
|
||||
return .{ .cap = idx };
|
||||
return .{ .leaf = .{ .cap = idx } };
|
||||
}
|
||||
const Kind = enum { local, ref, mut, slice, slice_mut, array };
|
||||
const kind = std.meta.stringToEnum(Kind, t.text) orelse {
|
||||
const kind = std.meta.stringToEnum(TypeWrapper, t.text) orelse {
|
||||
try p.report("E0004", t.span, "unknown type", &.{.{ .name = "name", .value = .{ .str = t.text } }});
|
||||
return error.SyntaxError;
|
||||
};
|
||||
try p.requireFeature(switch (kind) {
|
||||
.local, .ref, .mut => "ownership",
|
||||
.slice, .slice_mut, .array => "arrays",
|
||||
.own, .own_array => "heap",
|
||||
.task => "tasks",
|
||||
.sender, .receiver => "channels",
|
||||
.mutex, .guard => "sync",
|
||||
.island, .frozen, .iref => "islands",
|
||||
.arc => "arc",
|
||||
.atomic => "atomics",
|
||||
.buffer => "linear_buffers",
|
||||
}, t.span, t.text);
|
||||
_ = try p.expect(.lt, "`<`");
|
||||
const inner_span = p.peek().span;
|
||||
const inner = try p.parseType();
|
||||
// Storage and arrays of linear values need `linear_aggregates`; a
|
||||
// borrow of a linear value (`mut<file<read>>`) is fine.
|
||||
if (kind != .ref and kind != .mut) try p.gateLinearMember(inner, inner_span);
|
||||
if (kind == .array) {
|
||||
return .{ .wrapper = .{ .kind = kind, .inner_span = p.peek().span } };
|
||||
}
|
||||
|
||||
fn finishCompoundType(p: *Parser, frame: TypeFrame, inner: air.Type) Error!air.Type {
|
||||
// Preserve the old gate/diagnostic order: member gates precede the
|
||||
// closing delimiter, while general wrappers add no member gate here.
|
||||
switch (frame.kind) {
|
||||
.own, .own_array, .local, .slice, .slice_mut, .array => try p.gateLinearMember(inner, frame.inner_span),
|
||||
else => {},
|
||||
}
|
||||
if (frame.kind == .array) {
|
||||
_ = try p.expect(.comma, "`,`");
|
||||
const n_tok = try p.expect(.number, "array length");
|
||||
const n = std.fmt.parseInt(u64, n_tok.text, 0) catch 0;
|
||||
@@ -632,13 +648,9 @@ const Parser = struct {
|
||||
_ = try p.expect(.gt, "`>`");
|
||||
const boxed = try p.arena.create(air.Type);
|
||||
boxed.* = inner;
|
||||
return switch (kind) {
|
||||
.local => .{ .local = boxed },
|
||||
.ref => .{ .ref = boxed },
|
||||
.mut => .{ .mut = boxed },
|
||||
.slice => .{ .slice = boxed },
|
||||
.slice_mut => .{ .slice_mut = boxed },
|
||||
return switch (frame.kind) {
|
||||
.array => unreachable,
|
||||
inline else => |kind| @unionInit(air.Type, @tagName(kind), boxed),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1777,6 +1789,57 @@ test "parse types, aggregates and calls" {
|
||||
try std.testing.expectEqual(@as(u32, 1), m.functions[0].blocks[2].insts[1].op.call.callee);
|
||||
}
|
||||
|
||||
fn deepTypeSource(arena: Allocator, depth: usize, leaf: []const u8, closed: usize) ![]const u8 {
|
||||
var src: std.ArrayList(u8) = .empty;
|
||||
try src.appendSlice(arena, "module t\nair 0\nlang 0.1\nfeatures [arrays, ownership]\nfn @f(%x: ");
|
||||
for (0..depth) |_| try src.appendSlice(arena, "array<");
|
||||
try src.appendSlice(arena, leaf);
|
||||
for (0..closed) |_| try src.appendSlice(arena, ",1>");
|
||||
try src.appendSlice(arena, ") -> void\neffects []\nentry:\n ret\n");
|
||||
return src.items;
|
||||
}
|
||||
|
||||
test "deep anonymous types parse without native recursion" {
|
||||
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
const a = scratch.allocator();
|
||||
const depth = 32_768;
|
||||
const m = try parseOk(a, try deepTypeSource(a, depth, "u8", depth));
|
||||
var t = m.functions[0].params[0].ty;
|
||||
for (0..depth) |_| {
|
||||
try std.testing.expect(t == .array);
|
||||
try std.testing.expectEqual(@as(u64, 1), t.array.len);
|
||||
t = t.array.elem;
|
||||
}
|
||||
try std.testing.expectEqual(air.IntType.u8, t.int);
|
||||
}
|
||||
|
||||
test "deep unknown leaf retains one resolution diagnostic and its position" {
|
||||
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
const a = scratch.allocator();
|
||||
const depth = 8_192;
|
||||
const r = try parse(a, "deep.air", try deepTypeSource(a, depth, "unknown_type", depth));
|
||||
try std.testing.expect(r.module != null);
|
||||
try std.testing.expectEqual(@as(usize, 1), r.diagnostics.len);
|
||||
try std.testing.expectEqualStrings("E0004", r.diagnostics[0].code.id);
|
||||
try std.testing.expectEqual(@as(u32, 5), r.diagnostics[0].span.?.line);
|
||||
try std.testing.expectEqual(@as(u32, 11 + 6 * depth), r.diagnostics[0].span.?.col);
|
||||
}
|
||||
|
||||
test "deep missing outer array delimiter retains one syntax error" {
|
||||
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
const a = scratch.allocator();
|
||||
const depth = 8_192;
|
||||
const r = try parse(a, "deep.air", try deepTypeSource(a, depth, "u8", depth - 1));
|
||||
try std.testing.expect(r.module == null);
|
||||
try std.testing.expectEqual(@as(usize, 1), r.diagnostics.len);
|
||||
try std.testing.expectEqualStrings("E0001", r.diagnostics[0].code.id);
|
||||
try std.testing.expectEqual(@as(u32, 5), r.diagnostics[0].span.?.line);
|
||||
try std.testing.expectEqual(@as(u32, 11 + 6 * depth + 2 + 3 * (depth - 1)), r.diagnostics[0].span.?.col);
|
||||
}
|
||||
|
||||
test "resolution diagnostics" {
|
||||
try expectCodes(header ++ "fn @f() -> u8\neffects []\nentry:\n ret %x\n", &.{"E5001"});
|
||||
try expectCodes(header ++ "fn @f() -> u8\neffects []\nentry:\n br @nowhere()\n", &.{"E5002"});
|
||||
|
||||
Reference in new issue
Block a user