Files
aji/src/check.zig
T
dev eca30f817a Phase 6A: .aji layers 3-4 (addressable values, lexical borrows, slices)
Owner rules: addressability is decided by lowering (a variable that is
borrowed, indexed or written through a place gets frame storage in the
entry block; otherwise SSA; by name, deterministic); borrows are bound
only by 'ref r = place;' / 'mut r = place;' and live to the end of the
block (borrow.end at every normal exit, including break/continue/return);
no shadowing of an active borrow in the same scope; ref/mut as call
arguments are temporaries ended after the call; *r only for scalar and
struct referents, slices via s[i], s[a..b], s.len; writes *r = v,
r.f = v, s[i] = v with the value evaluated first; split_at as the only
multi-result binding; slice.copy(mut dst, ref src) as the only copy;
[e; N] evaluates e once; byte strings as [u8; N] in frame storage with
a fixed escape set; borrow.index.proven only for the verifier's own
fact form (if i < s.len, same SSA values), otherwise checked.

Diagnostics on lowered AIR are deduplicated per code and source
location. aji lower --stats counts code only (no comment lines).

Tests: 10 run (architecture goldens: value-only has no local.alloc,
borrowed has local.alloc, proven indexing), 6 rejects incl.
verifier-caught E2101 (aliasing; parent used after split_at).
Expansion on these programs: 4-9x bytes, 2.2-6.6x lines.
2026-10-05 11:51:04 +03:00

65 lines
2.8 KiB
Zig

//! Front door used by the CLI, the conformance runner and (later) `ajid`:
//! parse → verify, collecting diagnostics (spec 07 §8.6).
const std = @import("std");
const air = @import("air.zig");
const diag = @import("diag.zig");
const verifier = @import("verifier.zig");
const frontend = @import("frontend.zig");
pub const Status = enum {
/// Module verified; no error diagnostics.
accepted,
/// At least one error diagnostic.
rejected,
/// The pipeline cannot yet decide this input.
unimplemented,
};
pub const Report = struct {
status: Status,
diagnostics: []const diag.Diagnostic,
/// Present when `status == .accepted`.
module: ?air.Module = null,
/// Pipeline stage that is missing when `status == .unimplemented`.
missing_stage: []const u8 = "",
/// For `.aji` input: the lowered AIR and its line map (spec 15 §10).
lowered: ?frontend.lower_mod.Output = null,
};
/// Checks a `.aji` module (frontend, then the AIR pipeline, with every
/// diagnostic at its source location) or a text AIR module.
pub fn checkFile(arena: std.mem.Allocator, file_name: []const u8, source: []const u8) !Report {
if (!std.mem.endsWith(u8, file_name, ".aji")) return checkAir(arena, file_name, source);
const lowered = switch (try frontend.compile(arena, file_name, source)) {
.ok => |o| o,
.err => |d| return .{ .status = .rejected, .diagnostics = try arena.dupe(diag.Diagnostic, &.{d}) },
.unimplemented => |what| return .{ .status = .unimplemented, .diagnostics = &.{}, .missing_stage = what },
};
var r = try checkAir(arena, file_name, lowered.air);
// One source construct often lowers to several AIR instructions that the
// verifier reports separately: keep one diagnostic per code and source
// location (the first).
var mapped: std.ArrayList(diag.Diagnostic) = .empty;
for (r.diagnostics) |d| {
const m = try frontend.mapDiagnostic(arena, d, file_name, lowered.lines);
const dup = for (mapped.items) |prev| {
if (prev.code == m.code and prev.span.?.line == m.span.?.line and prev.span.?.col == m.span.?.col) break true;
} else false;
if (!dup) try mapped.append(arena, m);
}
r.diagnostics = mapped.items;
r.lowered = lowered;
return r;
}
/// Checks a text AIR module. All allocations go into `arena`.
pub fn checkAir(arena: std.mem.Allocator, file_name: []const u8, source: []const u8) !Report {
const parsed = try air.parse.parse(arena, file_name, source);
if (parsed.diagnostics.len != 0) return .{ .status = .rejected, .diagnostics = parsed.diagnostics };
const module = parsed.module.?;
const ds = try verifier.verify(arena, file_name, module);
if (ds.len != 0) return .{ .status = .rejected, .diagnostics = ds };
return .{ .status = .accepted, .diagnostics = &.{}, .module = module };
}