905 lines
38 KiB
Plaintext
905 lines
38 KiB
Plaintext
import "@std/debug"
|
|
import "@std/mem"
|
|
import "@std/arraylist"
|
|
import "@source/ast"
|
|
import "@source/diagnostics"
|
|
sema :: import "@source/sema"
|
|
|
|
TypeKind :: enum { none, invalid, int, float, bool, string, void, function }
|
|
|
|
Type :: struct {
|
|
kind TypeKind = .none
|
|
function ast.NodeId = ast.NodeId(maxval!(u32))
|
|
diagnostic diagnostics.DiagnosticId = diagnostics.no_diagnostic
|
|
}
|
|
|
|
@hide:file
|
|
Storage :: struct {
|
|
types []mut Type
|
|
allocator mem.Allocator
|
|
}
|
|
|
|
#! owns node-indexed types and retains its allocator; release with deinit.
|
|
#! function IDs refer to the caller-owned AST, not checker-owned signatures.
|
|
Result :: struct { storage Storage }
|
|
|
|
@hide:file
|
|
no_index :: maxval!(usize)
|
|
|
|
@hide:file
|
|
Role :: enum {
|
|
none,
|
|
expression,
|
|
declaration,
|
|
parameter,
|
|
function,
|
|
annotation,
|
|
invalid,
|
|
body,
|
|
return_stmt,
|
|
if_stmt,
|
|
assignment_stmt,
|
|
while_stmt,
|
|
}
|
|
|
|
@hide:file
|
|
Cell :: struct {
|
|
role Role = .none
|
|
parent usize = 0
|
|
size usize = 1
|
|
type Type = Type{}
|
|
numeric bool = false
|
|
anchored bool = false
|
|
demand TypeKind = .none
|
|
fixed bool = false
|
|
local bool = false
|
|
assignment_value bool = false
|
|
first_edge usize = no_index
|
|
return_annotation ast.NodeId = ast.NodeId(maxval!(u32))
|
|
}
|
|
|
|
@hide:file
|
|
Edge :: struct {
|
|
dependent ast.NodeId
|
|
next usize
|
|
}
|
|
|
|
@hide:file
|
|
State :: struct {
|
|
tree @ast.Tree
|
|
resolution @sema.Resolution
|
|
diagnostics @mut diagnostics.Store
|
|
types []mut Type
|
|
cells []mut Cell
|
|
edges arraylist.ArrayList(Edge)
|
|
collecting_assignment bool = false
|
|
has_assignments bool = false
|
|
}
|
|
|
|
#! borrows the AST, resolution, and diagnostic store only during checking.
|
|
#! temporary constraints are freed on every exit; Result owns only its type table.
|
|
check proc(
|
|
allocator mem.Allocator,
|
|
tree @ast.Tree,
|
|
resolution @sema.Resolution,
|
|
diagnostic_store @mut diagnostics.Store,
|
|
) Result ! (mem.AllocError | diagnostics.CapacityError) {
|
|
count :: ast.node_count(tree)
|
|
types :: try mem.alloc(Type, allocator, count)
|
|
errdefer mem.free(allocator, types)
|
|
mem.set(types, Type{})
|
|
cells :: try mem.alloc(Cell, allocator, count)
|
|
defer mem.free(allocator, cells)
|
|
mem.set(cells, Cell{})
|
|
for (0..count) |i| cells[i].parent = i
|
|
queue :: try mem.alloc(ast.NodeId, allocator, count)
|
|
defer mem.free(allocator, queue)
|
|
state := State{
|
|
tree = tree,
|
|
resolution = resolution,
|
|
diagnostics = diagnostic_store,
|
|
types = types,
|
|
cells = cells,
|
|
edges = arraylist.init(allocator),
|
|
}
|
|
defer arraylist.deinit(&state.edges)
|
|
|
|
for (0..ast.package_decl_count(tree)) |i| {
|
|
id :: ast.package_decl(tree, i)
|
|
match ast.decl_kind(tree, id) {
|
|
.statement: try collect_declaration(&state, id)
|
|
.function: try collect_function(&state, id)
|
|
.invalid: collect_invalid(&state, id)
|
|
}
|
|
}
|
|
for (0..count) |i| {
|
|
if (cells[i].role != .assignment_stmt) continue
|
|
target :: ast.assignment_stmt(tree, ast.NodeId(i)).target
|
|
binding :: sema.binding(resolution, target)
|
|
if (binding.kind == .invalid) {
|
|
cells[i].type = invalid_type(binding.data.diagnostic)
|
|
continue
|
|
}
|
|
if (binding.kind != .declaration or mutable_target(&state, target)) continue
|
|
cells[i].type = invalid_type(try diagnostics.add(
|
|
diagnostic_store, .invalid_assignment_target, ast.node_span(tree, target),
|
|
))
|
|
}
|
|
|
|
# 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
|
|
binding :: sema.binding(resolution, id)
|
|
if (binding.kind != .declaration) continue
|
|
target :: binding.data.declaration
|
|
try depend(&state, target, id)
|
|
if (cells[usize(target)].fixed) cells[i].type = cells[usize(target)].type
|
|
}
|
|
for (0..count) |i| {
|
|
id :: ast.NodeId(i)
|
|
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
|
|
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)
|
|
}
|
|
}
|
|
|
|
# assignment inputs observe established declaration types, never contribute
|
|
# constraints back into those declarations.
|
|
for 0..2 |phase| {
|
|
if (phase == 1 and !state.has_assignments) break
|
|
assignment_values :: phase == 1
|
|
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
|
|
binding :: sema.binding(resolution, id)
|
|
if (binding.kind == .declaration) cells[i].type = types[usize(binding.data.declaration)]
|
|
}
|
|
}
|
|
# function aliases are known before calls; scalar return annotations
|
|
# mean a call cannot introduce a function type.
|
|
for (0..count) |i| {
|
|
id :: ast.NodeId(i)
|
|
if (cells[i].role != .expression or cells[i].assignment_value != assignment_values or ast.expr_kind(tree, id) != .call) continue
|
|
callee :: ast.call_expr(tree, id).callee
|
|
signature :: group_type(&state, callee)
|
|
if (signature.function != ast.NodeId(maxval!(u32))) {
|
|
result :: ast.function_decl(tree, signature.function).return_type
|
|
seed(&state, id, types[usize(result)])
|
|
} else if (signature.kind == .invalid) {
|
|
seed(&state, id, signature)
|
|
}
|
|
}
|
|
for (0..count) |i| {
|
|
id :: ast.NodeId(i)
|
|
if (cells[i].role != .expression or cells[i].assignment_value != assignment_values or ast.expr_kind(tree, id) != .call) continue
|
|
callee :: ast.call_expr(tree, id).callee
|
|
signature :: group_type(&state, callee)
|
|
if (signature.function != ast.NodeId(maxval!(u32))) continue
|
|
if (signature.kind == .invalid) {
|
|
seed(&state, id, signature)
|
|
} else if (signature.kind != .none or cells[root(&state, usize(callee))].numeric) {
|
|
seed(&state, id, invalid_type(try diagnostics.add(diagnostic_store, .not_callable, ast.node_span(tree, callee))))
|
|
} else {
|
|
# without a function anchor, share the eventual inference error.
|
|
join(&state, id, callee)
|
|
}
|
|
}
|
|
for (0..count) |i| {
|
|
if (cells[i].role == .none or cells[i].role == .body or cells[i].assignment_value != assignment_values or types[i].kind == .invalid) continue
|
|
types[i] = cells[root(&state, i)].type
|
|
}
|
|
# reject nonnumeric operations before connecting otherwise valid inputs.
|
|
for (0..count) |i| {
|
|
id :: ast.NodeId(i)
|
|
if (cells[i].role != .expression or cells[i].assignment_value != assignment_values) continue
|
|
match ast.expr_kind(tree, id) {
|
|
.unary: {
|
|
expression :: ast.unary_expr(tree, id)
|
|
if (expression.operator == .negate) try require_numeric(&state, id, expression.operand)
|
|
}
|
|
.binary: {
|
|
expression :: ast.binary_expr(tree, id)
|
|
if (logical(expression.operator) or bool_equality(&state, id)) continue
|
|
try require_numeric(&state, id, expression.left)
|
|
try require_numeric(&state, id, expression.right)
|
|
}
|
|
else: {}
|
|
}
|
|
}
|
|
propagate(&state, queue)
|
|
|
|
# numeric edges join flexible operands; rigid inputs supply only demands.
|
|
for (0..count) |i| {
|
|
id :: ast.NodeId(i)
|
|
if (cells[i].role != .expression or cells[i].assignment_value != assignment_values) continue
|
|
match ast.expr_kind(tree, id) {
|
|
.unary: {
|
|
expression :: ast.unary_expr(tree, id)
|
|
if (expression.operator == .negate) try numeric_operand(&state, id, expression.operand)
|
|
}
|
|
.binary: {
|
|
expression :: ast.binary_expr(tree, id)
|
|
if (logical(expression.operator)) continue
|
|
if (comparison(expression.operator)) {
|
|
if (!bool_equality(&state, id)) try comparison_operands(&state, id)
|
|
} else {
|
|
try numeric_operand(&state, id, expression.left)
|
|
try numeric_operand(&state, id, expression.right)
|
|
if (expression.operator == .divide) {
|
|
try impose(&state, id, .float, .integer_division, expression.span)
|
|
}
|
|
}
|
|
}
|
|
else: {}
|
|
}
|
|
}
|
|
for (0..count) |i| {
|
|
id :: ast.NodeId(i)
|
|
if (!assignment_values and cells[i].role == .declaration) {
|
|
declaration :: ast.statement_decl(tree, id)
|
|
if (declaration.type) |annotation| {
|
|
try demand(&state, declaration.initializer, types[usize(annotation)], .type_mismatch)
|
|
}
|
|
}
|
|
if (!assignment_values and cells[i].role == .return_stmt) {
|
|
if (ast.return_stmt(tree, id).value) |value| {
|
|
try demand(&state, value, types[usize(cells[i].return_annotation)], .return_type_mismatch)
|
|
}
|
|
}
|
|
if (assignment_values and cells[i].role == .assignment_stmt) {
|
|
assignment :: ast.assignment_stmt(tree, id)
|
|
if (mutable_target(&state, assignment.target)) {
|
|
try demand(&state, assignment.value, types[usize(assignment.target)], .type_mismatch)
|
|
}
|
|
}
|
|
if (cells[i].role != .expression or cells[i].assignment_value != assignment_values or ast.expr_kind(tree, id) != .call) continue
|
|
signature :: group_type(&state, ast.call_expr(tree, id).callee)
|
|
if (signature.function == ast.NodeId(maxval!(u32))) continue
|
|
parameter_count :: ast.function_param_count(tree, signature.function)
|
|
for (0..ast.call_arg_count(tree, id)) |j| {
|
|
if (j >= parameter_count) continue
|
|
parameter :: ast.function_param(tree, signature.function, j)
|
|
try demand(&state, ast.call_arg(tree, id, j), cells[usize(parameter)].type, .type_mismatch)
|
|
}
|
|
}
|
|
|
|
# defaults follow ordinary program demands, then isolated assignment demands.
|
|
for (0..count) |i| {
|
|
if (root(&state, i) != i or cells[i].assignment_value != assignment_values) continue
|
|
if (cells[i].type.kind == .invalid) continue
|
|
if (cells[i].numeric and cells[i].anchored) {
|
|
kind TypeKind :: if (cells[i].demand == .float) .float else .int
|
|
cells[i].type = Type{ kind = kind }
|
|
}
|
|
}
|
|
for (0..count) |i| {
|
|
id :: ast.NodeId(i)
|
|
if (cells[i].role != .expression or cells[i].assignment_value != assignment_values or ast.expr_kind(tree, id) != .call) continue
|
|
callee :: ast.call_expr(tree, id).callee
|
|
signature :: group_type(&state, callee)
|
|
if (signature.function != ast.NodeId(maxval!(u32)) or signature.kind == .none or signature.kind == .invalid) continue
|
|
if (group_type(&state, id).kind != .invalid) {
|
|
seed(&state, id, invalid_type(try diagnostics.add(diagnostic_store, .not_callable, ast.node_span(tree, callee))))
|
|
}
|
|
}
|
|
for (0..count) |i| {
|
|
if (assignment_values or cells[i].role != .declaration or types[i].kind == .invalid) continue
|
|
index :: root(&state, i)
|
|
if (cells[index].type.kind != .none) continue
|
|
diagnostic :: try diagnostics.add(diagnostic_store, .cannot_infer_type, ast.statement_decl(tree, ast.NodeId(i)).name)
|
|
cells[index].type = invalid_type(diagnostic)
|
|
}
|
|
for (0..count) |i| {
|
|
if (cells[i].role == .none or cells[i].role == .body or cells[i].assignment_value != assignment_values or types[i].kind == .invalid) continue
|
|
types[i] = cells[root(&state, i)].type
|
|
}
|
|
}
|
|
for (0..count) |i| try validate(&state, ast.NodeId(i))
|
|
|
|
# errors travel only from dependencies to users. A rejected use never poisons
|
|
# an explicitly typed declaration, and body errors do not erase signatures.
|
|
propagate(&state, queue)
|
|
# validate conditions after operand errors have reached their users.
|
|
for (0..count) |i| {
|
|
id :: ast.NodeId(i)
|
|
if (cells[i].role != .if_stmt and cells[i].role != .while_stmt) continue
|
|
condition :: if (cells[i].role == .if_stmt) ast.if_stmt(tree, id).condition else ast.while_stmt(tree, id).condition
|
|
actual :: types[usize(condition)]
|
|
if (actual.kind != .invalid and actual.kind != .bool) {
|
|
try fail(&state, condition, .condition_not_bool, ast.node_span(tree, condition))
|
|
}
|
|
}
|
|
propagate(&state, queue)
|
|
for (0..ast.package_decl_count(tree)) |i| {
|
|
id :: ast.package_decl(tree, i)
|
|
if (ast.decl_kind(tree, id) != .function) continue
|
|
function :: ast.function_decl(tree, id)
|
|
result :: types[usize(function.return_type)]
|
|
if (result.kind == .void or result.kind == .invalid or types[usize(id)].kind == .invalid) continue
|
|
if (block_falls_through(&state, function.body)) {
|
|
# lowering must find this diagnostic even when another arm failed.
|
|
diagnostic :: try diagnostics.add(diagnostic_store, .missing_return, ast.node_span(tree, function.body))
|
|
types[usize(function.body)] = invalid_type(diagnostic)
|
|
}
|
|
}
|
|
return Result{ storage = Storage{ types = types, allocator = allocator } }
|
|
}
|
|
|
|
#! returns a type without retaining a reference to checker temporaries.
|
|
type_of proc(result @Result, id ast.NodeId) Type {
|
|
debug.assert(usize(id) < result.storage.types.len)
|
|
return result.storage.types[usize(id)]
|
|
}
|
|
|
|
#! releases type storage using the allocator retained by check.
|
|
deinit proc(result @mut Result) void {
|
|
mem.free(result.storage.allocator, result.storage.types)
|
|
}
|
|
|
|
@hide:file
|
|
propagate proc(state @mut State, queue []mut ast.NodeId) void {
|
|
next := usize(0)
|
|
end := usize(0)
|
|
for (0..(state.types.len)) |i| {
|
|
if (state.types[i].kind != .invalid) continue
|
|
queue[end] = ast.NodeId(i)
|
|
end += 1
|
|
}
|
|
while (next < end) {
|
|
source :: queue[next]
|
|
next += 1
|
|
edge := state.cells[usize(source)].first_edge
|
|
while (edge != no_index) {
|
|
dependent :: state.edges.items[edge].dependent
|
|
if (state.types[usize(dependent)].kind != .invalid) {
|
|
state.types[usize(dependent)] = invalid_type(state.types[usize(source)].diagnostic)
|
|
queue[end] = dependent
|
|
end += 1
|
|
}
|
|
edge = state.edges.items[edge].next
|
|
}
|
|
}
|
|
}
|
|
|
|
@hide:file
|
|
require_numeric proc(state @mut State, expression, operand ast.NodeId) void ! (mem.AllocError | diagnostics.CapacityError) {
|
|
index :: root(state, usize(operand))
|
|
type :: state.cells[index].type
|
|
if (type.kind == .invalid) {
|
|
state.types[usize(expression)] = type
|
|
return
|
|
}
|
|
if (type.kind == .none or state.cells[index].numeric or numeric_kind(type.kind)) return
|
|
try fail(state, expression, .invalid_operand, ast.node_span(state.tree, expression))
|
|
}
|
|
|
|
@hide:file
|
|
invalid_type proc(diagnostic diagnostics.DiagnosticId) Type {
|
|
return Type{ kind = .invalid, diagnostic = diagnostic }
|
|
}
|
|
|
|
@hide:file
|
|
root proc(state @mut State, index usize) usize {
|
|
current := index
|
|
while (state.cells[current].parent != current) current = state.cells[current].parent
|
|
next := index
|
|
while (state.cells[next].parent != next) {
|
|
parent :: state.cells[next].parent
|
|
state.cells[next].parent = current
|
|
next = parent
|
|
}
|
|
return current
|
|
}
|
|
|
|
@hide:file
|
|
group_type proc(state @mut State, id ast.NodeId) Type {
|
|
return state.cells[root(state, usize(id))].type
|
|
}
|
|
|
|
@hide:file
|
|
seed proc(state @mut State, id ast.NodeId, type Type) void {
|
|
index :: root(state, usize(id))
|
|
if (state.cells[index].type.kind != .invalid) state.cells[index].type = type
|
|
}
|
|
|
|
@hide:file
|
|
join proc(state @mut State, left, right ast.NodeId) void {
|
|
a := root(state, usize(left))
|
|
b := root(state, usize(right))
|
|
if (a == b) return
|
|
if (state.cells[a].size < state.cells[b].size) {
|
|
saved :: a
|
|
a = b
|
|
b = saved
|
|
}
|
|
state.cells[b].parent = a
|
|
state.cells[a].size += state.cells[b].size
|
|
if (state.cells[a].type.kind == .none or state.cells[b].type.kind == .invalid) {
|
|
state.cells[a].type = state.cells[b].type
|
|
}
|
|
state.cells[a].numeric = state.cells[a].numeric or state.cells[b].numeric
|
|
state.cells[a].anchored = state.cells[a].anchored or state.cells[b].anchored
|
|
if (state.cells[a].demand == .none) state.cells[a].demand = state.cells[b].demand
|
|
}
|
|
|
|
@hide:file
|
|
depend proc(state @mut State, source, dependent ast.NodeId) void ! mem.AllocError {
|
|
next :: state.cells[usize(source)].first_edge
|
|
try arraylist.append(&state.edges, Edge{ dependent = dependent, next = next })
|
|
state.cells[usize(source)].first_edge = state.edges.items.len - 1
|
|
}
|
|
|
|
@hide:file
|
|
collect_invalid proc(state @mut State, id ast.NodeId) void {
|
|
state.cells[usize(id)].role = .invalid
|
|
state.cells[usize(id)].type = invalid_type(ast.invalid(state.tree, id).diagnostic)
|
|
}
|
|
|
|
@hide:file
|
|
check_annotation proc(state @mut State, id ast.NodeId, allow_void bool) Type ! (mem.AllocError | diagnostics.CapacityError) {
|
|
if (state.cells[usize(id)].role == .annotation) return state.types[usize(id)]
|
|
state.cells[usize(id)].role = .annotation
|
|
binding :: sema.binding(state.resolution, id)
|
|
type := Type{}
|
|
match binding.kind {
|
|
.invalid: type = invalid_type(binding.data.diagnostic)
|
|
.builtin: {
|
|
type.kind = match binding.data.builtin {
|
|
.int: .int
|
|
.float: .float
|
|
.bool: .bool
|
|
.string: .string
|
|
.void: .void
|
|
}
|
|
if (type.kind == .void and !allow_void) {
|
|
type = invalid_type(try diagnostics.add(state.diagnostics, .invalid_void_type, ast.node_span(state.tree, id)))
|
|
}
|
|
}
|
|
else: type = invalid_type(try diagnostics.add(state.diagnostics, .expected_type, ast.node_span(state.tree, id)))
|
|
}
|
|
state.types[usize(id)] = type
|
|
state.cells[usize(id)].type = type
|
|
return type
|
|
}
|
|
|
|
@hide:file
|
|
collect_declaration proc(state @mut State, id ast.NodeId) void ! (mem.AllocError | diagnostics.CapacityError) {
|
|
declaration :: ast.statement_decl(state.tree, id)
|
|
state.cells[usize(id)].role = .declaration
|
|
if (declaration.type) |type| {
|
|
state.cells[usize(id)].fixed = true
|
|
state.cells[usize(id)].type = try check_annotation(state, type, false)
|
|
try depend(state, type, id)
|
|
}
|
|
binding :: sema.binding(state.resolution, id)
|
|
if (binding.kind == .invalid) {
|
|
state.cells[usize(id)].fixed = true
|
|
state.cells[usize(id)].type = invalid_type(binding.data.diagnostic)
|
|
}
|
|
try collect_expression(state, declaration.initializer)
|
|
try depend(state, declaration.initializer, id)
|
|
}
|
|
|
|
@hide:file
|
|
collect_function proc(state @mut State, id ast.NodeId) void ! (mem.AllocError | diagnostics.CapacityError) {
|
|
tree :: state.tree
|
|
function :: ast.function_decl(tree, id)
|
|
state.cells[usize(id)].role = .function
|
|
state.cells[usize(id)].fixed = true
|
|
state.cells[usize(id)].type = Type{ kind = .function, function = id }
|
|
_ = try check_annotation(state, function.return_type, true)
|
|
try depend(state, function.return_type, id)
|
|
for (0..ast.function_param_count(tree, id)) |i| {
|
|
parameter :: ast.function_param(tree, id, i)
|
|
state.cells[usize(parameter)].role = .parameter
|
|
state.cells[usize(parameter)].fixed = true
|
|
state.cells[usize(parameter)].type = try check_annotation(state, ast.param(tree, parameter).type, false)
|
|
binding :: sema.binding(state.resolution, parameter)
|
|
if (binding.kind == .invalid) state.cells[usize(parameter)].type = invalid_type(binding.data.diagnostic)
|
|
try depend(state, ast.param(tree, parameter).type, parameter)
|
|
try depend(state, parameter, id)
|
|
}
|
|
binding :: sema.binding(state.resolution, id)
|
|
if (binding.kind == .invalid) {
|
|
state.cells[usize(id)].type = invalid_type(binding.data.diagnostic)
|
|
state.cells[usize(id)].type.function = id
|
|
}
|
|
try collect_block(state, function.body, function.return_type)
|
|
}
|
|
|
|
@hide:file
|
|
collect_block proc(state @mut State, id, return_annotation ast.NodeId) void ! (mem.AllocError | diagnostics.CapacityError) {
|
|
tree :: state.tree
|
|
state.cells[usize(id)].role = .body
|
|
for (0..ast.block_statement_count(tree, id)) |i| {
|
|
statement :: ast.block_statement(tree, id, i)
|
|
match ast.stmt_kind(tree, statement) {
|
|
.declaration: {
|
|
state.cells[usize(statement)].local = true
|
|
try collect_declaration(state, statement)
|
|
}
|
|
.expression: try collect_expression(state, ast.statement_expr(tree, statement))
|
|
.return_stmt: {
|
|
state.cells[usize(statement)].role = .return_stmt
|
|
state.cells[usize(statement)].type = Type{ kind = .void }
|
|
state.cells[usize(statement)].return_annotation = return_annotation
|
|
if (ast.return_stmt(tree, statement).value) |value| {
|
|
try collect_expression(state, value)
|
|
try depend(state, value, statement)
|
|
}
|
|
}
|
|
.if_stmt: {
|
|
conditional :: ast.if_stmt(tree, statement)
|
|
state.cells[usize(statement)].role = .if_stmt
|
|
state.cells[usize(statement)].type = Type{ kind = .void }
|
|
try collect_expression(state, conditional.condition)
|
|
try depend(state, conditional.condition, statement)
|
|
try collect_block(state, conditional.then_block, return_annotation)
|
|
try depend(state, conditional.then_block, id)
|
|
if (conditional.else_block) |other| {
|
|
try collect_block(state, other, return_annotation)
|
|
try depend(state, other, id)
|
|
}
|
|
}
|
|
.assignment_stmt: {
|
|
assignment :: ast.assignment_stmt(tree, statement)
|
|
state.cells[usize(statement)].role = .assignment_stmt
|
|
state.has_assignments = true
|
|
state.cells[usize(statement)].type = Type{ kind = .void }
|
|
binding :: sema.binding(state.resolution, assignment.target)
|
|
if (binding.kind == .builtin) {
|
|
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),
|
|
))
|
|
} else {
|
|
try collect_expression(state, assignment.target)
|
|
}
|
|
state.collecting_assignment = true
|
|
try collect_expression(state, assignment.value)
|
|
state.collecting_assignment = false
|
|
try depend(state, assignment.target, statement)
|
|
try depend(state, assignment.value, statement)
|
|
}
|
|
.while_stmt: {
|
|
loop :: ast.while_stmt(tree, statement)
|
|
state.cells[usize(statement)].role = .while_stmt
|
|
state.cells[usize(statement)].type = Type{ kind = .void }
|
|
try collect_expression(state, loop.condition)
|
|
try depend(state, loop.condition, statement)
|
|
try collect_block(state, loop.body, return_annotation)
|
|
try depend(state, loop.body, id)
|
|
}
|
|
.invalid: collect_invalid(state, statement)
|
|
}
|
|
source :: if (ast.stmt_kind(tree, statement) == .expression) ast.statement_expr(tree, statement) else statement
|
|
try depend(state, source, id)
|
|
}
|
|
}
|
|
|
|
@hide:file
|
|
collect_expression proc(state @mut State, id ast.NodeId) void ! (mem.AllocError | diagnostics.CapacityError) {
|
|
if (state.cells[usize(id)].role != .none) return
|
|
tree :: state.tree
|
|
state.cells[usize(id)].role = .expression
|
|
state.cells[usize(id)].assignment_value = state.collecting_assignment
|
|
match ast.expr_kind(tree, id) {
|
|
.literal_int: {
|
|
state.cells[usize(id)].numeric = true
|
|
state.cells[usize(id)].anchored = true
|
|
}
|
|
.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: {
|
|
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)))
|
|
else: {}
|
|
}
|
|
}
|
|
.unary: {
|
|
expression :: ast.unary_expr(tree, id)
|
|
if (expression.operator == .logical_not) {
|
|
state.cells[usize(id)].type = Type{ kind = .bool }
|
|
} else {
|
|
state.cells[usize(id)].numeric = true
|
|
}
|
|
operand :: expression.operand
|
|
try collect_expression(state, operand)
|
|
try depend(state, operand, id)
|
|
}
|
|
.binary: {
|
|
expression :: ast.binary_expr(tree, id)
|
|
if (comparison(expression.operator) or logical(expression.operator)) {
|
|
state.cells[usize(id)].type = Type{ kind = .bool }
|
|
} else {
|
|
state.cells[usize(id)].numeric = true
|
|
}
|
|
try collect_expression(state, expression.left)
|
|
try collect_expression(state, expression.right)
|
|
try depend(state, expression.left, id)
|
|
# the RHS may be skipped, so its errors do not erase the bool result.
|
|
if (!logical(expression.operator)) try depend(state, expression.right, id)
|
|
}
|
|
.call: {
|
|
callee :: ast.call_expr(tree, id).callee
|
|
try collect_expression(state, callee)
|
|
try depend(state, callee, id)
|
|
for (0..ast.call_arg_count(tree, id)) |i| {
|
|
argument :: ast.call_arg(tree, id, i)
|
|
try collect_expression(state, argument)
|
|
try depend(state, argument, id)
|
|
}
|
|
}
|
|
.invalid: collect_invalid(state, id)
|
|
}
|
|
}
|
|
|
|
@hide:file
|
|
numeric_kind proc(kind TypeKind) bool {
|
|
return kind == .int or kind == .float
|
|
}
|
|
|
|
@hide:file
|
|
comparison proc(operator ast.BinaryOperator) bool {
|
|
return (
|
|
operator == .equal or operator == .not_equal or operator == .less
|
|
or operator == .less_equal or operator == .greater or operator == .greater_equal
|
|
)
|
|
}
|
|
|
|
@hide:file
|
|
logical proc(operator ast.BinaryOperator) bool {
|
|
return operator == .logical_and or operator == .logical_or
|
|
}
|
|
|
|
@hide:file
|
|
bool_equality proc(state @mut State, id ast.NodeId) bool {
|
|
expression :: ast.binary_expr(state.tree, id)
|
|
return (
|
|
(expression.operator == .equal or expression.operator == .not_equal)
|
|
and group_type(state, expression.left).kind == .bool
|
|
and group_type(state, expression.right).kind == .bool
|
|
)
|
|
}
|
|
|
|
@hide:file
|
|
comparison_operands proc(state @mut State, id ast.NodeId) void ! (mem.AllocError | diagnostics.CapacityError) {
|
|
if (state.types[usize(id)].kind == .invalid) return
|
|
expression :: ast.binary_expr(state.tree, id)
|
|
left :: root(state, usize(expression.left))
|
|
right :: root(state, usize(expression.right))
|
|
# only operand groups receive numeric demands; the result stays bool.
|
|
if (state.cells[left].numeric or state.cells[left].type.kind == .none) {
|
|
state.cells[left].numeric = true
|
|
try numeric_constraint(state, expression.left, expression.right, id)
|
|
} else if (state.cells[right].numeric or state.cells[right].type.kind == .none) {
|
|
state.cells[right].numeric = true
|
|
try numeric_constraint(state, expression.right, expression.left, id)
|
|
}
|
|
}
|
|
|
|
@hide:file
|
|
impose proc(state @mut State, id ast.NodeId, kind TypeKind, code diagnostics.Code, span ast.Span) void ! (mem.AllocError | diagnostics.CapacityError) {
|
|
index :: root(state, usize(id))
|
|
if (state.cells[index].type.kind == .invalid) return
|
|
state.cells[index].anchored = true
|
|
if (state.cells[index].demand == .none) {
|
|
state.cells[index].demand = kind
|
|
} else if (state.cells[index].demand != kind) {
|
|
state.cells[index].type = invalid_type(try diagnostics.add(state.diagnostics, code, span))
|
|
}
|
|
}
|
|
|
|
@hide:file
|
|
numeric_operand proc(state @mut State, expression, operand ast.NodeId) void ! (mem.AllocError | diagnostics.CapacityError) {
|
|
try numeric_constraint(state, expression, operand, expression)
|
|
}
|
|
|
|
@hide:file
|
|
numeric_constraint proc(state @mut State, expression, operand, operation ast.NodeId) void ! (mem.AllocError | diagnostics.CapacityError) {
|
|
index :: root(state, usize(operand))
|
|
type :: state.cells[index].type
|
|
if (state.types[usize(expression)].kind == .invalid or state.types[usize(operand)].kind == .invalid) return
|
|
if (group_type(state, expression).kind == .invalid) return
|
|
if (state.cells[index].numeric or type.kind == .none) {
|
|
expression_demand :: state.cells[root(state, usize(expression))].demand
|
|
operand_demand :: state.cells[index].demand
|
|
join(state, expression, operand)
|
|
if (expression_demand != .none) {
|
|
try impose(state, expression, expression_demand, .invalid_operand, ast.node_span(state.tree, operation))
|
|
}
|
|
if (operand_demand != .none) {
|
|
try impose(state, expression, operand_demand, .invalid_operand, ast.node_span(state.tree, operation))
|
|
}
|
|
} else if (numeric_kind(type.kind)) {
|
|
try impose(state, expression, type.kind, .invalid_operand, ast.node_span(state.tree, operation))
|
|
}
|
|
}
|
|
|
|
@hide:file
|
|
demand proc(state @mut State, id ast.NodeId, expected Type, code diagnostics.Code) void ! (mem.AllocError | diagnostics.CapacityError) {
|
|
if (!numeric_kind(expected.kind) or state.types[usize(id)].kind == .invalid) return
|
|
index :: root(state, usize(id))
|
|
if (!state.cells[index].numeric and state.cells[index].type.kind != .none) return
|
|
state.cells[index].numeric = true
|
|
try impose(state, id, expected.kind, code, ast.node_span(state.tree, id))
|
|
}
|
|
|
|
@hide:file
|
|
fail proc(state @mut State, id ast.NodeId, code diagnostics.Code, span ast.Span) void ! (mem.AllocError | diagnostics.CapacityError) {
|
|
if (state.types[usize(id)].kind == .invalid) return
|
|
state.types[usize(id)] = invalid_type(try diagnostics.add(state.diagnostics, code, span))
|
|
}
|
|
|
|
@hide:file
|
|
require_bool proc(state @mut State, operand ast.NodeId) void ! (mem.AllocError | diagnostics.CapacityError) {
|
|
type :: state.types[usize(operand)]
|
|
if (type.kind != .invalid and type.kind != .bool) {
|
|
try fail(state, operand, .invalid_operand, ast.node_span(state.tree, operand))
|
|
}
|
|
}
|
|
|
|
@hide:file
|
|
mutable_target proc(state @State, id ast.NodeId) bool {
|
|
binding :: sema.binding(state.resolution, id)
|
|
if (binding.kind != .declaration) return false
|
|
target :: binding.data.declaration
|
|
if (state.cells[usize(target)].role != .declaration or !state.cells[usize(target)].local) return false
|
|
return ast.statement_decl(state.tree, target).mutability == .variable
|
|
}
|
|
|
|
@hide:file
|
|
same_signature proc(state @State, left, right ast.NodeId) bool {
|
|
tree :: state.tree
|
|
if (ast.function_param_count(tree, left) != ast.function_param_count(tree, right)) return false
|
|
left_result :: ast.function_decl(tree, left).return_type
|
|
right_result :: ast.function_decl(tree, right).return_type
|
|
if (state.types[usize(left_result)].kind != state.types[usize(right_result)].kind) return false
|
|
for (0..ast.function_param_count(tree, left)) |i| {
|
|
left_param :: ast.function_param(tree, left, i)
|
|
right_param :: ast.function_param(tree, right, i)
|
|
if (state.types[usize(left_param)].kind != state.types[usize(right_param)].kind) return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
@hide:file
|
|
compatible proc(state @mut State, value ast.NodeId, expected Type) void ! (mem.AllocError | diagnostics.CapacityError) {
|
|
actual :: state.types[usize(value)]
|
|
if (actual.kind == .invalid or expected.kind == .invalid) return
|
|
if (actual.kind != expected.kind) {
|
|
try fail(state, value, .type_mismatch, ast.node_span(state.tree, value))
|
|
} else if (actual.kind == .function and !same_signature(state, actual.function, expected.function)) {
|
|
try fail(state, value, .type_mismatch, ast.node_span(state.tree, value))
|
|
}
|
|
}
|
|
|
|
@hide:file
|
|
validate proc(state @mut State, id ast.NodeId) void ! (mem.AllocError | diagnostics.CapacityError) {
|
|
tree :: state.tree
|
|
match state.cells[usize(id)].role {
|
|
.declaration: {
|
|
declaration :: ast.statement_decl(tree, id)
|
|
if (state.types[usize(declaration.initializer)].kind == .void) {
|
|
try fail(state, declaration.initializer, .type_mismatch, ast.node_span(tree, declaration.initializer))
|
|
}
|
|
if (declaration.type) |type| try compatible(state, declaration.initializer, state.types[usize(type)])
|
|
}
|
|
.assignment_stmt: {
|
|
assignment :: ast.assignment_stmt(tree, id)
|
|
if (mutable_target(state, assignment.target)) {
|
|
try compatible(state, assignment.value, state.types[usize(assignment.target)])
|
|
}
|
|
}
|
|
.return_stmt: {
|
|
statement :: ast.return_stmt(tree, id)
|
|
expected :: state.types[usize(state.cells[usize(id)].return_annotation)]
|
|
if (expected.kind == .invalid) return
|
|
if (statement.value) |value| {
|
|
actual :: state.types[usize(value)]
|
|
if (actual.kind == .invalid) {
|
|
state.types[usize(id)] = actual
|
|
} else if (actual.kind != expected.kind) {
|
|
try fail(state, value, .return_type_mismatch, ast.node_span(tree, value))
|
|
state.types[usize(id)] = state.types[usize(value)]
|
|
}
|
|
} else if (expected.kind != .void) {
|
|
try fail(state, id, .return_type_mismatch, statement.span)
|
|
}
|
|
}
|
|
.expression: {
|
|
match ast.expr_kind(tree, id) {
|
|
.unary: {
|
|
expression :: ast.unary_expr(tree, id)
|
|
if (expression.operator == .logical_not) {
|
|
try require_bool(state, expression.operand)
|
|
return
|
|
}
|
|
operand :: state.types[usize(expression.operand)]
|
|
if (operand.kind != .invalid and !numeric_kind(operand.kind)) {
|
|
try fail(state, id, .invalid_operand, ast.node_span(tree, id))
|
|
}
|
|
}
|
|
.binary: {
|
|
expression :: ast.binary_expr(tree, id)
|
|
if (logical(expression.operator)) {
|
|
try require_bool(state, expression.left)
|
|
try require_bool(state, expression.right)
|
|
return
|
|
}
|
|
left :: state.types[usize(expression.left)]
|
|
right :: state.types[usize(expression.right)]
|
|
if (left.kind == .invalid or right.kind == .invalid) return
|
|
if ((expression.operator == .equal or expression.operator == .not_equal)
|
|
and left.kind == .bool and right.kind == .bool) return
|
|
if (!numeric_kind(left.kind) or !numeric_kind(right.kind) or left.kind != right.kind) {
|
|
try fail(state, id, .invalid_operand, expression.span)
|
|
} else if (expression.operator == .divide and left.kind == .int) {
|
|
try fail(state, id, .integer_division, expression.span)
|
|
}
|
|
}
|
|
.call: try validate_call(state, id)
|
|
else: {}
|
|
}
|
|
}
|
|
else: {}
|
|
}
|
|
}
|
|
|
|
@hide:file
|
|
validate_call proc(state @mut State, id ast.NodeId) void ! (mem.AllocError | diagnostics.CapacityError) {
|
|
tree :: state.tree
|
|
expression :: ast.call_expr(tree, id)
|
|
callee :: state.types[usize(expression.callee)]
|
|
signature :: group_type(state, expression.callee)
|
|
if (signature.function == ast.NodeId(maxval!(u32))) {
|
|
if (callee.kind != .invalid) {
|
|
try fail(state, id, .not_callable, ast.node_span(tree, expression.callee))
|
|
}
|
|
return
|
|
}
|
|
parameter_count :: ast.function_param_count(tree, signature.function)
|
|
if (parameter_count != ast.call_arg_count(tree, id)) {
|
|
diagnostic :: try diagnostics.add(state.diagnostics, .argument_count_mismatch, expression.span)
|
|
if (callee.kind != .invalid) state.types[usize(id)] = invalid_type(diagnostic)
|
|
}
|
|
for (0..ast.call_arg_count(tree, id)) |i| {
|
|
if (i >= parameter_count) continue
|
|
parameter :: ast.function_param(tree, signature.function, i)
|
|
try compatible(state, ast.call_arg(tree, id, i), state.types[usize(parameter)])
|
|
}
|
|
}
|
|
|
|
|
|
@hide:file
|
|
block_falls_through proc(state @State, id ast.NodeId) bool {
|
|
tree :: state.tree
|
|
for (0..ast.block_statement_count(tree, id)) |i| {
|
|
statement :: ast.block_statement(tree, id, i)
|
|
kind :: ast.stmt_kind(tree, statement)
|
|
source :: if (kind == .expression) ast.statement_expr(tree, statement) else statement
|
|
if (state.types[usize(source)].kind == .invalid) return false
|
|
match kind {
|
|
.return_stmt, .invalid: return false
|
|
.if_stmt: {
|
|
conditional :: ast.if_stmt(tree, statement)
|
|
then_falls :: block_falls_through(state, conditional.then_block)
|
|
if (conditional.else_block) |other| {
|
|
if (!then_falls and !block_falls_through(state, other)) return false
|
|
}
|
|
}
|
|
else: {}
|
|
}
|
|
}
|
|
return true
|
|
}
|