0068. Global spans and per-function source files
- Status: Accepted
- Date: 2026-10-31
- Plan: M9.1 Step 1
- Review: Compiler and backend
Context
Modules need unique spans and expression IDs without replacing the existing span-keyed type tables or source slices. Both backends must retain the owning file for traps. This supersedes only ADR-0016's single-file source identity; its structured IR, evaluation order and other invariants remain in force.
Decision
SourceFile concatenates root-first file texts,
inserting one newline sentinel before each subsequent file. Its
file table records name, byte base, exclusive end and first global
line. Spans stay global byte offsets; line/column lookups are
file-relative. The sentinel belongs to the preceding file's EOF,
so an old EOF span keeps its owner. Source-line rendering stops at
that file's end.
The parser accepts a byte base and expression-ID seed, rebases token spans and decoded-string offset maps, and returns the next unused ID. Single-file parsing keeps the zero-base/zero-seed entry point.
IR uses Program.files and Func.file.
Lowering assigns the declaration's file, including generic
instances; synthetic startup inherits main's file. No current IR
pass inlines functions across file boundaries: non-escaping blocks
lower inside their containing function. A future cross-function
inliner must retain each trap's original file explicitly.
C remains one translation unit, switching HN_FILE
and resetting #line mapping when the function's file
changes. The interpreter retains the active frame's file on a
trap, including entry fuel exhaustion. IR inspection prints
function files only for multi-file programs. All single-file
output stays byte-identical.
Alternatives considered
- Per-file spans and phase-local source maps: would require changing every span-keyed table and direct source slice throughout the compiler.
- Separate C translation units: whole-program typing and specialization still need all declarations; separate compilation is outside this refactor.
- A filename on every IR position: redundant while functions are not inlined across source files; a per-function index suffices for both backends.
Consequences
This is source plumbing only. Module loading, import syntax, visibility and name rewriting follow in later M9.1 steps. Single-file goldens need no updates.