feat: resolve names without shadowing

This commit is contained in:
2026-10-05 18:15:55 +02:00
parent 50a10bb404
commit 9b273f2471
6 changed files with 521 additions and 4 deletions
+247
View File
@@ -0,0 +1,247 @@
import "@std/debug"
import "@std/mem"
import "@std/hashmap"
import "@std/strmap"
import "@source/ast"
import "@source/diagnostics"
BuiltinType :: enum { int, float, string, void }
@hide:file
builtins strmap.StaticStringMap(BuiltinType) :: strmap.init([
{ "int", .int },
{ "float", .float },
{ "string", .string },
{ "void", .void },
])
BindingKind :: enum { none, declaration, builtin, invalid }
BindingData :: union {
declaration ast.NodeId
builtin BuiltinType
diagnostic diagnostics.DiagnosticId
}
Binding :: struct {
kind BindingKind = .none
data BindingData = BindingData{ diagnostic = diagnostics.NO_DIAGNOSTIC }
}
@hide:file
Storage :: struct {
bindings []mut Binding
allocator mem.Allocator
}
Resolution :: struct { storage Storage }
@hide:file
Scope :: struct {
names hashmap.StringHashMap(Binding)
parent ?@Scope
}
@hide:file
State :: struct {
tree @ast.Tree
diagnostics @mut diagnostics.Store
bindings []mut Binding
allocator mem.Allocator
}
resolve proc(
tree @ast.Tree,
diagnostic_store @mut diagnostics.Store,
allocator mem.Allocator,
) Resolution ! (mem.AllocError | diagnostics.CapacityError) {
bindings :: try mem.alloc(Binding, allocator, ast.node_count(tree))
errdefer mem.free(allocator, bindings)
for (0..bindings.len) |i| bindings[i] = Binding{}
state := State{
tree = tree,
diagnostics = diagnostic_store,
bindings = bindings,
allocator = allocator,
}
package_scope := Scope{ names = hashmap.init(allocator), parent = null }
defer hashmap.deinit(&package_scope.names)
for (0..ast.package_decl_count(tree)) |i| {
id :: ast.package_decl(tree, i)
match ast.decl_kind(tree, id) {
.statement: try predeclare(&state, &package_scope, id, ast.statement_decl(tree, id).name)
.function: try predeclare(&state, &package_scope, id, ast.function_decl(tree, id).name)
.invalid: {}
}
}
for (0..ast.package_decl_count(tree)) |i| {
id :: ast.package_decl(tree, i)
match ast.decl_kind(tree, id) {
.statement: try resolve_statement_decl(&state, &package_scope, id)
.function: try resolve_function(&state, &package_scope, id)
.invalid: resolve_invalid(&state, id)
}
}
return Resolution{ storage = Storage{ bindings = bindings, allocator = allocator } }
}
deinit proc(resolution @mut Resolution) void {
mem.free(resolution.storage.allocator, resolution.storage.bindings)
}
binding proc(resolution @Resolution, id ast.NodeId) Binding {
debug.assert(usize(id) < resolution.storage.bindings.len)
return resolution.storage.bindings[usize(id)]
}
@hide:file
lookup proc(scope ?@Scope, name []u8) ?Binding {
if scope |current| {
if hashmap.get(&current.names, name) |found| return found
return lookup(current.parent, name)
}
if strmap.get(&builtins, name) |type| {
return Binding{ kind = .builtin, data = BindingData{ builtin = type } }
}
return null
}
@hide:file
put_name proc(scope @mut Scope, name []u8, value Binding) void ! mem.AllocError {
hashmap.put(&scope.names, name, value) catch |err| {
match err {
.key_exists: unreachable
else: return err
}
}
}
@hide:file
predeclare proc(
state @mut State,
scope @mut Scope,
id ast.NodeId,
name_span ast.Span,
) void ! (mem.AllocError | diagnostics.CapacityError) {
name :: ast.source_text(state.tree, name_span)
if hashmap.get(&scope.names, name) |_| {
diagnostic :: try diagnostics.add(state.diagnostics, .duplicate_name, name_span)
state.bindings[usize(id)] = Binding{
kind = .invalid,
data = BindingData{ diagnostic = diagnostic },
}
return
}
value := Binding{ kind = .declaration, data = BindingData{ declaration = id } }
if lookup(scope.parent, name) |_| {
diagnostic :: try diagnostics.add(state.diagnostics, .duplicate_name, name_span)
# Poison the inner name rather than silently binding uses to its ancestor.
value = Binding{ kind = .invalid, data = BindingData{ diagnostic = diagnostic } }
}
try put_name(scope, name, value)
state.bindings[usize(id)] = value
}
@hide:file
resolve_function proc(
state @mut State,
package_scope @Scope,
id ast.NodeId,
) void ! (mem.AllocError | diagnostics.CapacityError) {
tree :: state.tree
function :: ast.function_decl(tree, id)
# Signatures cannot see parameters or body-local declarations.
try resolve_expr(state, package_scope, function.return_type)
for (0..ast.function_param_count(tree, id)) |i| {
parameter :: ast.function_param(tree, id, i)
try resolve_expr(state, package_scope, ast.param(tree, parameter).type)
}
scope := Scope{ names = hashmap.init(state.allocator), parent = package_scope }
defer hashmap.deinit(&scope.names)
for (0..ast.function_param_count(tree, id)) |i| {
parameter :: ast.function_param(tree, id, i)
try predeclare(state, &scope, parameter, ast.param(tree, parameter).name)
}
for (0..ast.block_statement_count(tree, function.body)) |i| {
statement :: ast.block_statement(tree, function.body, i)
if ast.stmt_kind(tree, statement) == .declaration {
try predeclare(state, &scope, statement, ast.statement_decl(tree, statement).name)
}
}
for (0..ast.block_statement_count(tree, function.body)) |i| {
statement :: ast.block_statement(tree, function.body, i)
match ast.stmt_kind(tree, statement) {
.declaration: try resolve_statement_decl(state, &scope, statement)
.expression: try resolve_expr(state, &scope, ast.statement_expr(tree, statement))
.invalid: resolve_invalid(state, statement)
}
}
}
@hide:file
resolve_statement_decl proc(
state @mut State,
scope @Scope,
id ast.NodeId,
) void ! (mem.AllocError | diagnostics.CapacityError) {
declaration :: ast.statement_decl(state.tree, id)
if (declaration.type) |type| try resolve_expr(state, scope, type)
try resolve_expr(state, scope, declaration.initializer)
}
@hide:file
resolve_invalid proc(state @mut State, id ast.NodeId) void {
if (state.bindings[usize(id)].kind != .none) return
state.bindings[usize(id)] = Binding{
kind = .invalid,
data = BindingData{ diagnostic = ast.invalid(state.tree, id).diagnostic },
}
}
@hide:file
resolve_expr proc(
state @mut State,
scope @Scope,
id ast.NodeId,
) void ! (mem.AllocError | diagnostics.CapacityError) {
# Grouped parameters share their type node, including any diagnostic it owns.
if (state.bindings[usize(id)].kind != .none) return
tree :: state.tree
match ast.expr_kind(tree, id) {
.literal_int, .literal_float, .literal_string: {}
.identifier: {
span :: ast.node_span(tree, id)
name :: ast.source_text(tree, span)
if lookup(scope, name) |found| {
state.bindings[usize(id)] = found
} else {
diagnostic :: try diagnostics.add(state.diagnostics, .unknown_name, span)
state.bindings[usize(id)] = Binding{
kind = .invalid,
data = BindingData{ diagnostic = diagnostic },
}
}
}
.unary: try resolve_expr(state, scope, ast.unary_expr(tree, id).operand)
.binary: {
expression :: ast.binary_expr(tree, id)
try resolve_expr(state, scope, expression.left)
try resolve_expr(state, scope, expression.right)
}
.call: {
try resolve_expr(state, scope, ast.call_expr(tree, id).callee)
for (0..ast.call_arg_count(tree, id)) |i| {
try resolve_expr(state, scope, ast.call_arg(tree, id, i))
}
}
.invalid: resolve_invalid(state, id)
}
}
+253
View File
@@ -0,0 +1,253 @@
import "@std/mem"
import "@std/testing"
import "@source/diagnostics"
import "@source/strpool"
import "@source/lexer"
import "@source/parser"
import "@source/ast"
resolves_forward_references_recursion_and_builtin_signatures test {
strpool.STRINGS = strpool.init(mem.c_allocator)
defer strpool.deinit(&strpool.STRINGS)
diagnostic_store := diagnostics.init(mem.c_allocator)
defer diagnostics.deinit(&diagnostic_store)
source ::
`first :: later
`alpha proc(p int, q float, s string) void {
` before :: -after * p + beta(after)
` after :: later
`}
`beta proc(p int) void {
` after :: p
` alpha(after, 1.0, "x")
`}
`later int :: 1
scan_state := lexer.init(mem.c_allocator)
defer lexer.deinit(&scan_state)
try lexer.scan(&scan_state, source, &diagnostic_store)
tree := try parser.parse(scan_state.tokens.items, source, &diagnostic_store, mem.c_allocator)
defer ast.deinit(&tree)
resolution := try resolve(&tree, &diagnostic_store, mem.c_allocator)
defer deinit(&resolution)
first :: ast.package_decl(&tree, 0)
alpha :: ast.package_decl(&tree, 1)
beta :: ast.package_decl(&tree, 2)
later :: ast.package_decl(&tree, 3)
alpha_view :: ast.function_decl(&tree, alpha)
beta_view :: ast.function_decl(&tree, beta)
before :: ast.block_statement(&tree, alpha_view.body, 0)
after :: ast.block_statement(&tree, alpha_view.body, 1)
p :: ast.function_param(&tree, alpha, 0)
q :: ast.function_param(&tree, alpha, 1)
s :: ast.function_param(&tree, alpha, 2)
sibling_p :: ast.function_param(&tree, beta, 0)
sibling_after :: ast.block_statement(&tree, beta_view.body, 0)
first_ref :: ast.statement_decl(&tree, first).initializer
after_ref :: ast.statement_decl(&tree, after).initializer
references [2]ast.NodeId :: [first_ref, after_ref]
for references |id| {
try testing.expect_equal(BindingKind.declaration, binding(&resolution, id).kind)
try testing.expect_equal(later, binding(&resolution, id).data.declaration)
}
add :: ast.binary_expr(&tree, ast.statement_decl(&tree, before).initializer)
multiply :: ast.binary_expr(&tree, add.left)
forward_ref :: ast.unary_expr(&tree, multiply.left).operand
try testing.expect_equal(BindingKind.declaration, binding(&resolution, forward_ref).kind)
try testing.expect_equal(after, binding(&resolution, forward_ref).data.declaration)
try testing.expect_equal(BindingKind.declaration, binding(&resolution, multiply.right).kind)
try testing.expect_equal(p, binding(&resolution, multiply.right).data.declaration)
beta_ref :: ast.call_expr(&tree, add.right).callee
try testing.expect_equal(BindingKind.declaration, binding(&resolution, beta_ref).kind)
try testing.expect_equal(beta, binding(&resolution, beta_ref).data.declaration)
argument :: ast.call_arg(&tree, add.right, 0)
try testing.expect_equal(BindingKind.declaration, binding(&resolution, argument).kind)
try testing.expect_equal(after, binding(&resolution, argument).data.declaration)
recursive_call :: ast.statement_expr(&tree, ast.block_statement(&tree, beta_view.body, 1))
alpha_ref :: ast.call_expr(&tree, recursive_call).callee
try testing.expect_equal(BindingKind.declaration, binding(&resolution, alpha_ref).kind)
try testing.expect_equal(alpha, binding(&resolution, alpha_ref).data.declaration)
sibling_ref :: ast.call_arg(&tree, recursive_call, 0)
try testing.expect_equal(BindingKind.declaration, binding(&resolution, sibling_ref).kind)
try testing.expect_equal(sibling_after, binding(&resolution, sibling_ref).data.declaration)
sibling_param_ref :: ast.statement_decl(&tree, sibling_after).initializer
try testing.expect_equal(BindingKind.declaration, binding(&resolution, sibling_param_ref).kind)
try testing.expect_equal(sibling_p, binding(&resolution, sibling_param_ref).data.declaration)
parameter_types [2]ast.NodeId :: [ast.param(&tree, p).type, ast.param(&tree, sibling_p).type]
for parameter_types |id| {
try testing.expect_equal(BindingKind.builtin, binding(&resolution, id).kind)
try testing.expect_equal(BuiltinType.int, binding(&resolution, id).data.builtin)
}
if (ast.statement_decl(&tree, later).type) |type| {
try testing.expect_equal(BindingKind.builtin, binding(&resolution, type).kind)
try testing.expect_equal(BuiltinType.int, binding(&resolution, type).data.builtin)
} else {
return .expectation_failed
}
try testing.expect_equal(BindingKind.builtin, binding(&resolution, ast.param(&tree, q).type).kind)
try testing.expect_equal(BuiltinType.float, binding(&resolution, ast.param(&tree, q).type).data.builtin)
try testing.expect_equal(BindingKind.builtin, binding(&resolution, ast.param(&tree, s).type).kind)
try testing.expect_equal(BuiltinType.string, binding(&resolution, ast.param(&tree, s).type).data.builtin)
return_types [2]ast.NodeId :: [alpha_view.return_type, beta_view.return_type]
for return_types |id| {
try testing.expect_equal(BindingKind.builtin, binding(&resolution, id).kind)
try testing.expect_equal(BuiltinType.void, binding(&resolution, id).data.builtin)
}
try testing.expect_equal(0, diagnostics.count(&diagnostic_store))
}
rejects_shadowing_and_duplicates_without_cascades test {
strpool.STRINGS = strpool.init(mem.c_allocator)
defer strpool.deinit(&strpool.STRINGS)
diagnostic_store := diagnostics.init(mem.c_allocator)
defer diagnostics.deinit(&diagnostic_store)
source ::
`collide proc(outer int, p int, p int) outer {
` outer :: missing
` outer :: 1
` p :: 1
` int :: 1
` pkg :: 1
` outer
` p
` int
` pkg
`}
`outer :: 1
`pkg :: 1
`same :: 1
`same :: same
scan_state := lexer.init(mem.c_allocator)
defer lexer.deinit(&scan_state)
try lexer.scan(&scan_state, source, &diagnostic_store)
tree := try parser.parse(scan_state.tokens.items, source, &diagnostic_store, mem.c_allocator)
defer ast.deinit(&tree)
try testing.expect_equal(0, diagnostics.count(&diagnostic_store))
resolution := try resolve(&tree, &diagnostic_store, mem.c_allocator)
defer deinit(&resolution)
function :: ast.package_decl(&tree, 0)
view :: ast.function_decl(&tree, function)
outer :: ast.package_decl(&tree, 1)
first_same :: ast.package_decl(&tree, 3)
second_same :: ast.package_decl(&tree, 4)
outer_param :: ast.function_param(&tree, function, 0)
p :: ast.function_param(&tree, function, 1)
duplicate_p :: ast.function_param(&tree, function, 2)
first_outer :: ast.block_statement(&tree, view.body, 0)
second_outer :: ast.block_statement(&tree, view.body, 1)
local_p :: ast.block_statement(&tree, view.body, 2)
local_int :: ast.block_statement(&tree, view.body, 3)
local_pkg :: ast.block_statement(&tree, view.body, 4)
rejected [8]ast.NodeId :: [outer_param, duplicate_p, first_outer, second_outer, local_p, local_int, local_pkg, second_same]
for rejected |id| {
invalid_binding :: binding(&resolution, id)
try testing.expect_equal(BindingKind.invalid, invalid_binding.kind)
if (diagnostics.get(&diagnostic_store, invalid_binding.data.diagnostic)) |diagnostic| {
try testing.expect_equal(diagnostics.Code.duplicate_name, diagnostic.code)
name :: if (id == outer_param or id == duplicate_p) ast.param(&tree, id).name else ast.statement_decl(&tree, id).name
try testing.expect_equal(name.start, diagnostic.span.start)
try testing.expect_equal(name.end, diagnostic.span.end)
} else {
return .expectation_failed
}
}
try testing.expect(binding(&resolution, first_outer).data.diagnostic != binding(&resolution, second_outer).data.diagnostic)
try testing.expect(binding(&resolution, outer_param).data.diagnostic != binding(&resolution, first_outer).data.diagnostic)
try testing.expect(binding(&resolution, duplicate_p).data.diagnostic != binding(&resolution, local_p).data.diagnostic)
outer_ref :: ast.statement_expr(&tree, ast.block_statement(&tree, view.body, 5))
try testing.expect_equal(BindingKind.invalid, binding(&resolution, outer_ref).kind)
try testing.expect_equal(binding(&resolution, outer_param).data.diagnostic, binding(&resolution, outer_ref).data.diagnostic)
p_ref :: ast.statement_expr(&tree, ast.block_statement(&tree, view.body, 6))
try testing.expect_equal(BindingKind.declaration, binding(&resolution, p_ref).kind)
try testing.expect_equal(p, binding(&resolution, p_ref).data.declaration)
int_ref :: ast.statement_expr(&tree, ast.block_statement(&tree, view.body, 7))
try testing.expect_equal(BindingKind.invalid, binding(&resolution, int_ref).kind)
try testing.expect_equal(binding(&resolution, local_int).data.diagnostic, binding(&resolution, int_ref).data.diagnostic)
pkg_ref :: ast.statement_expr(&tree, ast.block_statement(&tree, view.body, 8))
try testing.expect_equal(BindingKind.invalid, binding(&resolution, pkg_ref).kind)
try testing.expect_equal(binding(&resolution, local_pkg).data.diagnostic, binding(&resolution, pkg_ref).data.diagnostic)
try testing.expect_equal(BindingKind.declaration, binding(&resolution, view.return_type).kind)
try testing.expect_equal(outer, binding(&resolution, view.return_type).data.declaration)
parameter_type :: ast.param(&tree, outer_param).type
try testing.expect_equal(BindingKind.builtin, binding(&resolution, parameter_type).kind)
try testing.expect_equal(BuiltinType.int, binding(&resolution, parameter_type).data.builtin)
same_ref :: ast.statement_decl(&tree, second_same).initializer
try testing.expect_equal(BindingKind.declaration, binding(&resolution, same_ref).kind)
try testing.expect_equal(first_same, binding(&resolution, same_ref).data.declaration)
missing :: ast.statement_decl(&tree, first_outer).initializer
try testing.expect_equal(BindingKind.invalid, binding(&resolution, missing).kind)
if (diagnostics.get(&diagnostic_store, binding(&resolution, missing).data.diagnostic)) |diagnostic| {
try testing.expect_equal(diagnostics.Code.unknown_name, diagnostic.code)
try testing.expect_equal(ast.node_span(&tree, missing).start, diagnostic.span.start)
try testing.expect_equal(ast.node_span(&tree, missing).end, diagnostic.span.end)
} else {
return .expectation_failed
}
try testing.expect_equal(9, diagnostics.count(&diagnostic_store))
}
preserves_syntax_diagnostics_and_resolves_shared_unknown_types_once test {
strpool.STRINGS = strpool.init(mem.c_allocator)
defer strpool.deinit(&strpool.STRINGS)
diagnostic_store := diagnostics.init(mem.c_allocator)
defer diagnostics.deinit(&diagnostic_store)
source ::
`@ broken
`main proc(a, b Missing) void {
` empty :: ()
` bad :: absent + absent
` valid :: later
` later :: 1
` discarded(1 2)
`}
scan_state := lexer.init(mem.c_allocator)
defer lexer.deinit(&scan_state)
try lexer.scan(&scan_state, source, &diagnostic_store)
tree := try parser.parse(scan_state.tokens.items, source, &diagnostic_store, mem.c_allocator)
defer ast.deinit(&tree)
syntax_count :: diagnostics.count(&diagnostic_store)
invalid_decl :: ast.package_decl(&tree, 0)
try testing.expect_equal(ast.DeclKind.invalid, ast.decl_kind(&tree, invalid_decl))
function :: ast.package_decl(&tree, 1)
view :: ast.function_decl(&tree, function)
empty :: ast.statement_decl(&tree, ast.block_statement(&tree, view.body, 0)).initializer
try testing.expect_equal(ast.ExprKind.invalid, ast.expr_kind(&tree, empty))
invalid_statement :: ast.block_statement(&tree, view.body, 4)
try testing.expect_equal(ast.StmtKind.invalid, ast.stmt_kind(&tree, invalid_statement))
resolution := try resolve(&tree, &diagnostic_store, mem.c_allocator)
defer deinit(&resolution)
invalid_nodes [3]ast.NodeId :: [invalid_decl, empty, invalid_statement]
for invalid_nodes |id| {
invalid_view :: ast.invalid(&tree, id)
invalid_binding :: binding(&resolution, id)
try testing.expect_equal(BindingKind.invalid, invalid_binding.kind)
try testing.expect_equal(invalid_view.diagnostic, invalid_binding.data.diagnostic)
}
a :: ast.function_param(&tree, function, 0)
b :: ast.function_param(&tree, function, 1)
shared_type :: ast.param(&tree, a).type
try testing.expect_equal(binding(&resolution, shared_type).data.diagnostic, binding(&resolution, ast.param(&tree, b).type).data.diagnostic)
bad :: ast.binary_expr(&tree, ast.statement_decl(&tree, ast.block_statement(&tree, view.body, 1)).initializer)
unknown_nodes [3]ast.NodeId :: [shared_type, bad.left, bad.right]
for unknown_nodes |id| {
invalid_binding :: binding(&resolution, id)
try testing.expect_equal(BindingKind.invalid, invalid_binding.kind)
if (diagnostics.get(&diagnostic_store, invalid_binding.data.diagnostic)) |diagnostic| {
try testing.expect_equal(diagnostics.Code.unknown_name, diagnostic.code)
try testing.expect_equal(ast.node_span(&tree, id).start, diagnostic.span.start)
try testing.expect_equal(ast.node_span(&tree, id).end, diagnostic.span.end)
} else {
return .expectation_failed
}
}
try testing.expect(binding(&resolution, bad.left).data.diagnostic != binding(&resolution, bad.right).data.diagnostic)
valid :: ast.block_statement(&tree, view.body, 2)
later :: ast.block_statement(&tree, view.body, 3)
valid_ref :: ast.statement_decl(&tree, valid).initializer
try testing.expect_equal(BindingKind.declaration, binding(&resolution, valid_ref).kind)
try testing.expect_equal(later, binding(&resolution, valid_ref).data.declaration)
try testing.expect_equal(syntax_count + 3, diagnostics.count(&diagnostic_store))
}