feat: resolve file-local import namespaces
This commit is contained in:
+26
-10
@@ -171,7 +171,7 @@ check proc(
|
||||
for 0..count |i| {
|
||||
if (cells[i].role != .assignment_stmt) continue
|
||||
target :: ast.assignment_stmt(tree, ast.NodeId(i)).target
|
||||
if (ast.expr_kind(tree, target) != .identifier) continue
|
||||
if (!declaration_reference(tree, target)) continue
|
||||
binding :: sema.binding(resolution, target)
|
||||
if (binding.kind == .invalid) {
|
||||
cells[i].type = invalid_type(binding.data.diagnostic)
|
||||
@@ -185,7 +185,7 @@ check proc(
|
||||
# aliases share a type variable; explicit annotations and parameters are boundaries.
|
||||
for (0..count) |i| {
|
||||
id :: ast.NodeId(i)
|
||||
if (cells[i].role != .expression or ast.expr_kind(tree, id) != .identifier) continue
|
||||
if (cells[i].role != .expression or !declaration_reference(tree, id)) continue
|
||||
binding :: sema.binding(resolution, id)
|
||||
if (binding.kind != .declaration) continue
|
||||
target :: binding.data.declaration
|
||||
@@ -197,7 +197,7 @@ check proc(
|
||||
if (cells[i].role == .declaration and !cells[i].fixed) {
|
||||
join(&state, id, ast.statement_decl(tree, id).initializer)
|
||||
}
|
||||
if (cells[i].role != .expression or ast.expr_kind(tree, id) != .identifier) continue
|
||||
if (cells[i].role != .expression or !declaration_reference(tree, id)) continue
|
||||
binding :: sema.binding(resolution, id)
|
||||
if (binding.kind == .declaration and !cells[i].assignment_value and !cells[usize(binding.data.declaration)].fixed) {
|
||||
join(&state, id, binding.data.declaration)
|
||||
@@ -212,7 +212,10 @@ check proc(
|
||||
if (assignment_values) {
|
||||
for (0..count) |i| {
|
||||
id :: ast.NodeId(i)
|
||||
if (!cells[i].assignment_value or cells[i].role != .expression or ast.expr_kind(tree, id) != .identifier) continue
|
||||
if (!cells[i].assignment_value
|
||||
or cells[i].role != .expression
|
||||
or !declaration_reference(tree, id)
|
||||
) continue
|
||||
binding :: sema.binding(resolution, id)
|
||||
if (binding.kind == .declaration) cells[i].type = types[usize(binding.data.declaration)]
|
||||
}
|
||||
@@ -677,7 +680,7 @@ collect_block proc(state @mut State, id, return_annotation ast.NodeId, in_loop b
|
||||
state.has_assignments = true
|
||||
state.cells[usize(statement)].type = Type{ kind = .void }
|
||||
binding :: sema.binding(state.resolution, assignment.target)
|
||||
if (binding.kind == .builtin) {
|
||||
if (binding.kind == .builtin or binding.kind == .namespace) {
|
||||
state.cells[usize(assignment.target)].role = .expression
|
||||
state.cells[usize(assignment.target)].type = invalid_type(try diagnostics.add(
|
||||
state.diagnostics, .invalid_assignment_target, ast.node_span(tree, assignment.target),
|
||||
@@ -760,11 +763,12 @@ collect_expression proc(state @mut State, id ast.NodeId) void ! (mem.AllocError
|
||||
.literal_float: state.cells[usize(id)].type = Type{ kind = .float }
|
||||
.literal_bool: state.cells[usize(id)].type = Type{ kind = .bool }
|
||||
.literal_string: state.cells[usize(id)].type = Type{ kind = .string }
|
||||
.identifier: {
|
||||
.identifier, .selection: {
|
||||
binding :: sema.binding(state.resolution, id)
|
||||
match binding.kind {
|
||||
.invalid: state.cells[usize(id)].type = invalid_type(binding.data.diagnostic)
|
||||
.builtin: state.cells[usize(id)].type = invalid_type(try diagnostics.add(state.diagnostics, .type_not_value, ast.node_span(tree, id)))
|
||||
.namespace: state.cells[usize(id)].type = invalid_type(try diagnostics.add(state.diagnostics, .namespace_not_value, ast.node_span(tree, id)))
|
||||
else: {}
|
||||
}
|
||||
}
|
||||
@@ -839,6 +843,12 @@ collect_expression proc(state @mut State, id ast.NodeId) void ! (mem.AllocError
|
||||
}
|
||||
}
|
||||
|
||||
@hide:file
|
||||
declaration_reference proc(tree @ast.Tree, id ast.NodeId) bool {
|
||||
kind :: ast.expr_kind(tree, id)
|
||||
return kind == .identifier or kind == .selection
|
||||
}
|
||||
|
||||
@hide:file
|
||||
numeric_kind proc(kind TypeKind) bool {
|
||||
return kind == .int or kind == .float or kind == .usize
|
||||
@@ -1200,7 +1210,7 @@ constant_integer proc(state @mut State, id ast.NodeId) ?ConstantInteger {
|
||||
}
|
||||
return ConstantInteger{ magnitude = value }
|
||||
}
|
||||
.identifier: {
|
||||
.identifier, .selection: {
|
||||
binding :: sema.binding(state.resolution, id)
|
||||
if (binding.kind != .declaration) return null
|
||||
target :: binding.data.declaration
|
||||
@@ -1328,7 +1338,7 @@ infer_shape proc(state @mut State, id ast.NodeId, defaults bool) Type ! (mem.All
|
||||
if (state.cells[index].type.kind == .invalid) return state.cells[index].type
|
||||
state.cells[index].visiting = true
|
||||
defer state.cells[index].visiting = false
|
||||
if (state.cells[usize(id)].role == .expression and ast.expr_kind(state.tree, id) == .identifier) {
|
||||
if (state.cells[usize(id)].role == .expression and declaration_reference(state.tree, id)) {
|
||||
binding :: sema.binding(state.resolution, id)
|
||||
if (binding.kind == .declaration) {
|
||||
target :: binding.data.declaration
|
||||
@@ -1486,7 +1496,10 @@ prepare_ranges proc(state @mut State, assignment_values, constrain bool) void !
|
||||
}
|
||||
for 0..(state.cells.len) |i| {
|
||||
id :: ast.NodeId(i)
|
||||
if (state.cells[i].role != .expression or state.cells[i].assignment_value or ast.expr_kind(tree, id) != .identifier) continue
|
||||
if (state.cells[i].role != .expression
|
||||
or state.cells[i].assignment_value
|
||||
or !declaration_reference(tree, id)
|
||||
) continue
|
||||
binding :: sema.binding(state.resolution, id)
|
||||
if (binding.kind == .declaration and state.cells[usize(binding.data.declaration)].role == .loop_capture) {
|
||||
seed(state, id, group_type(state, binding.data.declaration))
|
||||
@@ -1544,7 +1557,10 @@ specialize_annotations proc(state @mut State) void ! (mem.AllocError | diagnosti
|
||||
}
|
||||
for 0..(state.cells.len) |i| {
|
||||
id :: ast.NodeId(i)
|
||||
if (state.cells[i].role != .expression or state.cells[i].assignment_value or ast.expr_kind(state.tree, id) != .identifier) continue
|
||||
if (state.cells[i].role != .expression
|
||||
or state.cells[i].assignment_value
|
||||
or !declaration_reference(state.tree, id)
|
||||
) continue
|
||||
binding :: sema.binding(state.resolution, id)
|
||||
if (binding.kind == .declaration and state.cells[usize(binding.data.declaration)].fixed) {
|
||||
seed(state, id, state.cells[usize(binding.data.declaration)].type)
|
||||
|
||||
+123
-20
@@ -42,10 +42,11 @@ intern_builtins proc(
|
||||
return ids
|
||||
}
|
||||
|
||||
BindingKind :: enum { none, declaration, builtin, invalid }
|
||||
BindingKind :: enum { none, declaration, namespace, builtin, invalid }
|
||||
|
||||
BindingData :: union {
|
||||
declaration ast.NodeId
|
||||
namespace ast.NodeId
|
||||
builtin BuiltinType
|
||||
diagnostic diagnostics.DiagnosticId
|
||||
}
|
||||
@@ -55,6 +56,14 @@ Binding :: struct {
|
||||
data BindingData = BindingData{ diagnostic = diagnostics.no_diagnostic }
|
||||
}
|
||||
|
||||
#! links a file-local import to a package root; failed loads retain their diagnostic.
|
||||
Import :: struct {
|
||||
node ast.NodeId
|
||||
name strpool.StringId
|
||||
target ?ast.NodeId
|
||||
diagnostic ?diagnostics.DiagnosticId = null
|
||||
}
|
||||
|
||||
@hide:file
|
||||
Storage :: struct {
|
||||
bindings []mut Binding
|
||||
@@ -78,6 +87,7 @@ State :: struct {
|
||||
bindings []mut Binding
|
||||
builtin_ids []strpool.StringId
|
||||
allocator mem.Allocator
|
||||
namespaces hashmap.AutoHashMap(ast.NodeId, Scope)
|
||||
}
|
||||
|
||||
#! borrows `tree`, its identifier `pool`, and `diagnostic_store` during resolution.
|
||||
@@ -88,9 +98,20 @@ resolve proc(
|
||||
tree @ast.Tree,
|
||||
pool @mut strpool.StringPool,
|
||||
diagnostic_store @mut diagnostics.Store,
|
||||
) Resolution ! (mem.AllocError | strpool.InternError | diagnostics.CapacityError) {
|
||||
) Resolution ! (
|
||||
mem.AllocError
|
||||
| strpool.InternError
|
||||
| diagnostics.CapacityError
|
||||
) {
|
||||
roots [1]ast.NodeId :: [tree.root]
|
||||
return try resolve_packages(allocator, tree, roots[..], pool, diagnostic_store)
|
||||
return try resolve_packages(
|
||||
allocator,
|
||||
tree,
|
||||
roots[..],
|
||||
&[],
|
||||
pool,
|
||||
diagnostic_store,
|
||||
)
|
||||
}
|
||||
|
||||
#! resolves each package root in its own namespace; AST IDs share the compilation arena.
|
||||
@@ -98,6 +119,7 @@ resolve_packages proc(
|
||||
allocator mem.Allocator,
|
||||
tree @ast.Tree,
|
||||
roots []ast.NodeId,
|
||||
imports []Import,
|
||||
pool @mut strpool.StringPool,
|
||||
diagnostic_store @mut diagnostics.Store,
|
||||
) Resolution ! (mem.AllocError | strpool.InternError | diagnostics.CapacityError) {
|
||||
@@ -112,9 +134,45 @@ resolve_packages proc(
|
||||
bindings = bindings,
|
||||
builtin_ids = builtin_ids[..],
|
||||
allocator = allocator,
|
||||
namespaces = hashmap.init(allocator),
|
||||
}
|
||||
defer {
|
||||
for (state.namespaces.entries) |entry| {
|
||||
if (entry.hash == 0) continue
|
||||
scope := entry.value
|
||||
hashmap.deinit(&scope.names)
|
||||
}
|
||||
hashmap.deinit(&state.namespaces)
|
||||
}
|
||||
|
||||
for (roots) |root| try resolve_package(&state, root)
|
||||
package_scopes :: try mem.alloc(Scope, allocator, roots.len)
|
||||
defer mem.free(allocator, package_scopes)
|
||||
file_scopes :: try mem.alloc(Scope, allocator, ast.source_files(tree).len)
|
||||
defer mem.free(allocator, file_scopes)
|
||||
for (file_scopes) |@scope| scope^ = Scope{ names = hashmap.init(allocator), parent = null }
|
||||
defer { for (file_scopes) |@scope| hashmap.deinit(&scope.names) }
|
||||
for (roots) |root, index| {
|
||||
package_scopes[index] = Scope{ names = hashmap.init(allocator), parent = null }
|
||||
scope := &package_scopes[index]
|
||||
errdefer hashmap.deinit(&scope.names)
|
||||
for (0..ast.package_decl_count_at(tree, root)) |i| {
|
||||
id :: ast.package_decl_at(tree, root, i)
|
||||
file_scopes[usize(ast.node_span(tree, id).source)].parent = scope
|
||||
match ast.decl_kind(tree, id) {
|
||||
.statement: try predeclare(&state, scope, id, ast.statement_decl(tree, id).name)
|
||||
.function: try predeclare(&state, scope, id, ast.function_decl(tree, id).name)
|
||||
.import, .invalid: {}
|
||||
}
|
||||
}
|
||||
hashmap.put(&state.namespaces, root, scope^) catch |err| {
|
||||
match err {
|
||||
.key_exists: unreachable
|
||||
else: return err
|
||||
}
|
||||
}
|
||||
}
|
||||
for (imports) |imported| try resolve_import(&state, &file_scopes[usize(ast.node_span(tree, imported.node).source)], imported)
|
||||
for (roots) |root| try resolve_package(&state, root, file_scopes)
|
||||
|
||||
return Resolution{ storage = Storage{
|
||||
bindings = bindings,
|
||||
@@ -123,32 +181,53 @@ resolve_packages proc(
|
||||
}
|
||||
|
||||
@hide:file
|
||||
resolve_package proc(state @mut State, root ast.NodeId) void ! (mem.AllocError | diagnostics.CapacityError) {
|
||||
resolve_package proc(
|
||||
state @mut State, root ast.NodeId, file_scopes []Scope,
|
||||
) void ! (mem.AllocError | diagnostics.CapacityError) {
|
||||
tree := state.tree^
|
||||
tree.root = root
|
||||
package_scope := Scope{
|
||||
names = hashmap.init(state.allocator),
|
||||
parent = null,
|
||||
}
|
||||
defer hashmap.deinit(&package_scope.names)
|
||||
for (0..ast.package_decl_count(&tree)) |i| {
|
||||
id :: ast.package_decl(&tree, i)
|
||||
scope :: &file_scopes[usize(ast.node_span(&tree, id).source)]
|
||||
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)
|
||||
.import, .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)
|
||||
.statement: try resolve_statement_decl(state, scope, id)
|
||||
.function: try resolve_function(state, scope, id)
|
||||
.invalid: resolve_invalid(state, id)
|
||||
.import: {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@hide:file
|
||||
resolve_import proc(
|
||||
state @mut State, scope @mut Scope, imported Import,
|
||||
) void ! (mem.AllocError | diagnostics.CapacityError) {
|
||||
span :: ast.import_decl(state.tree, imported.node).span
|
||||
value := if (imported.diagnostic) |diagnostic|
|
||||
Binding{ kind = .invalid, data = BindingData{ diagnostic = diagnostic } }
|
||||
else if (imported.target) |target|
|
||||
Binding{ kind = .namespace, data = BindingData{ namespace = target } }
|
||||
else
|
||||
Binding{ kind = .invalid, data = BindingData{
|
||||
diagnostic = try diagnostics.add(state.diagnostics, .package_unavailable, span),
|
||||
} }
|
||||
if (imported.name == strpool.no_str) {
|
||||
state.bindings[usize(imported.node)] = value
|
||||
return
|
||||
}
|
||||
if hashmap.get(&scope.names, imported.name) |_| {
|
||||
state.bindings[usize(imported.node)] = Binding{ kind = .invalid, data = BindingData{
|
||||
diagnostic = try diagnostics.add(state.diagnostics, .duplicate_name, span),
|
||||
} }
|
||||
return
|
||||
}
|
||||
if lookup(state, scope.parent, imported.name) |_| {
|
||||
value = Binding{ kind = .invalid, data = BindingData{
|
||||
diagnostic = try diagnostics.add(state.diagnostics, .duplicate_name, span),
|
||||
} }
|
||||
}
|
||||
try put_name(scope, imported.name, value)
|
||||
state.bindings[usize(imported.node)] = value
|
||||
}
|
||||
|
||||
#! releases binding storage with the allocator retained by resolve.
|
||||
@@ -349,6 +428,30 @@ resolve_expr proc(
|
||||
}
|
||||
}
|
||||
}
|
||||
.selection: {
|
||||
expression :: ast.selection_expr(tree, id)
|
||||
try resolve_expr(state, scope, expression.base)
|
||||
base :: state.bindings[usize(expression.base)]
|
||||
value := base
|
||||
if (base.kind != .invalid) {
|
||||
if (base.kind == .namespace) {
|
||||
namespace :: hashmap.get(&state.namespaces, base.data.namespace)?
|
||||
if hashmap.get(&namespace.names, ast.name_id(tree, expression.member)) |found| {
|
||||
value = found
|
||||
} else {
|
||||
value = Binding{ kind = .invalid, data = BindingData{
|
||||
diagnostic = try diagnostics.add(state.diagnostics, .unknown_name, ast.node_span(tree, expression.member)),
|
||||
} }
|
||||
}
|
||||
} else {
|
||||
value = Binding{ kind = .invalid, data = BindingData{
|
||||
diagnostic = try diagnostics.add(state.diagnostics, .not_namespace, ast.node_span(tree, expression.base)),
|
||||
} }
|
||||
}
|
||||
}
|
||||
state.bindings[usize(id)] = value
|
||||
state.bindings[usize(expression.member)] = value
|
||||
}
|
||||
.unary: try resolve_expr(state, scope, ast.unary_expr(tree, id).operand)
|
||||
.binary: {
|
||||
expression :: ast.binary_expr(tree, id)
|
||||
|
||||
Reference in New Issue
Block a user