array reflection
This commit is contained in:
+125
-46
@@ -231,7 +231,6 @@ Checker :: struct {
|
||||
constants: []Constant,
|
||||
template_diagnostics: []source.Diagnostic_Id,
|
||||
constant_stack: [dynamic]Constant_Frame,
|
||||
ast_expr_stack: [dynamic]ast.Expr_Id,
|
||||
hir_expr_stack: [dynamic]hir.Expr_Id,
|
||||
infer_stack: [dynamic]Infer_Frame,
|
||||
build_stack: [dynamic]Build_Expr_Frame,
|
||||
@@ -2090,6 +2089,9 @@ explicit_comptime_argument_valid :: proc(
|
||||
_, ok := resolve_type_argument(checker, arg, pkg, file)
|
||||
return ok
|
||||
}
|
||||
if type_pattern_mentions_comptime(checker, function, comptime_param_count(function), param.type) {
|
||||
return true
|
||||
}
|
||||
if is_comptime_string_param(checker, param, function) {
|
||||
_, ok := comptime_string_argument(checker, arg, pkg, file)
|
||||
return ok
|
||||
@@ -2097,9 +2099,6 @@ explicit_comptime_argument_valid :: proc(
|
||||
if types.is_concrete_integer(type_from_syntax(checker, param.type, function.pkg, function.file)) {
|
||||
return eval_integer_constant_in_context(checker, arg, pkg, file).kind == .Value
|
||||
}
|
||||
if type_pattern_mentions_comptime(checker, function, comptime_param_count(function), param.type) {
|
||||
return true
|
||||
}
|
||||
declared := type_from_syntax(checker, param.type, function.pkg, function.file)
|
||||
_, ok := eval_static_comptime_value(checker, param.name, arg, declared, pkg, file)
|
||||
return ok
|
||||
@@ -2307,6 +2306,38 @@ call_argument_mapping :: proc(
|
||||
}
|
||||
}
|
||||
}
|
||||
if selected_index < 0 {
|
||||
// Prefer the mapping that treats an array literal as a dependent aggregate.
|
||||
// It can then report the contextual element error instead of an ambiguous
|
||||
// hidden-comptime-parameter mapping failure.
|
||||
dependent_array_candidate := -1
|
||||
ambiguous := false
|
||||
for candidate, index in candidates {
|
||||
matches := false
|
||||
for arg_id, source_index in args {
|
||||
param_index := call_param_index(candidate, source_index)
|
||||
if param_index < 0 || param_index >= len(function.params) ||
|
||||
!function.params[param_index].comptime_value ||
|
||||
arg_id == ast.INVALID_EXPR || int(arg_id) >= len(checker.ast_module.exprs) ||
|
||||
checker.ast_module.exprs[arg_id].kind != .Array {
|
||||
continue
|
||||
}
|
||||
matches = matches || type_pattern_mentions_comptime(
|
||||
checker, function^, comptime_param_count(function^), function.params[param_index].type,
|
||||
)
|
||||
}
|
||||
if matches {
|
||||
if dependent_array_candidate >= 0 {
|
||||
ambiguous = true
|
||||
} else {
|
||||
dependent_array_candidate = index
|
||||
}
|
||||
}
|
||||
}
|
||||
if dependent_array_candidate >= 0 && !ambiguous {
|
||||
selected_index = dependent_array_candidate
|
||||
}
|
||||
}
|
||||
if selected_index < 0 {
|
||||
if len(failures) > 0 {
|
||||
builder := strings.builder_make(checker.allocator)
|
||||
@@ -2638,7 +2669,10 @@ infer_call_comptime_values :: proc(
|
||||
continue
|
||||
}
|
||||
values[ordinal].name = param.name
|
||||
if is_comptime_type_param(checker, param) {
|
||||
dependent := type_pattern_mentions_comptime(checker, function, ordinal, param.type)
|
||||
if dependent {
|
||||
values[ordinal].kind = .Static
|
||||
} else if is_comptime_type_param(checker, param) {
|
||||
values[ordinal].kind = .Type
|
||||
} else if is_comptime_string_param(checker, param, function) {
|
||||
values[ordinal].kind = .String
|
||||
@@ -2653,6 +2687,65 @@ infer_call_comptime_values :: proc(
|
||||
ordinal += 1
|
||||
}
|
||||
matched := true
|
||||
all_bound := true
|
||||
for value_bound in bound {
|
||||
all_bound = all_bound && value_bound
|
||||
}
|
||||
if !all_bound && is_runtime_type(checker, expected) {
|
||||
if types.is_valid(function.error) && types.kind(expected, &checker.module.types) == .Fallible {
|
||||
matched = match_inferred_type_pattern(
|
||||
checker, function, prefix, function.result,
|
||||
types.fallible_success(expected, &checker.module.types), values, bound,
|
||||
source.Span{}, diagnose,
|
||||
) && matched
|
||||
matched = match_inferred_type_pattern(
|
||||
checker, function, prefix, function.error,
|
||||
types.fallible_error(expected, &checker.module.types), values, bound,
|
||||
source.Span{}, diagnose,
|
||||
) && matched
|
||||
} else if type_pattern_mentions_comptime(checker, function, prefix, function.result) {
|
||||
matched = match_inferred_type_pattern(
|
||||
checker, function, prefix, function.result, expected, values, bound,
|
||||
source.Span{}, diagnose,
|
||||
) && matched
|
||||
}
|
||||
}
|
||||
// A comptime aggregate is evaluated only after its dependent type is known.
|
||||
// Bind a direct `[N]T` parameter's N from an array literal's syntax first;
|
||||
// this supplies the contextual element type for strings and enum literals.
|
||||
for arg_id, source_index in args {
|
||||
param_index := call_param_index(mapping, source_index)
|
||||
if param_index < 0 || param_index >= len(function.params) ||
|
||||
!function.params[param_index].comptime_value ||
|
||||
arg_id == ast.INVALID_EXPR || int(arg_id) >= len(checker.ast_module.exprs) ||
|
||||
checker.ast_module.exprs[arg_id].kind != .Array {
|
||||
continue
|
||||
}
|
||||
pattern, pattern_ok := types.node(&checker.module.types, function.params[param_index].type)
|
||||
if !pattern_ok || pattern.kind != .Array || !pattern.unresolved_count ||
|
||||
pattern.count_expr == u32(ast.INVALID_EXPR) || int(pattern.count_expr) >= len(checker.ast_module.exprs) {
|
||||
continue
|
||||
}
|
||||
count_expr := checker.ast_module.exprs[ast.Expr_Id(pattern.count_expr)]
|
||||
if count_expr.kind != .Name || symbol.is_valid(count_expr.qualifier) {
|
||||
continue
|
||||
}
|
||||
binding_index, is_binding := comptime_binding_index(function, prefix, count_expr.name)
|
||||
if !is_binding || bound[binding_index] {
|
||||
continue
|
||||
}
|
||||
param, param_ok := comptime_param_for_name(function, count_expr.name)
|
||||
if !param_ok || is_comptime_type_param(checker, param) {
|
||||
continue
|
||||
}
|
||||
values[binding_index] = Comptime_Value{
|
||||
name=count_expr.name,
|
||||
type=type_from_syntax(checker, param.type, function.pkg, function.file),
|
||||
value=i128(len(checker.ast_module.exprs[arg_id].args)),
|
||||
kind=.Integer,
|
||||
}
|
||||
bound[binding_index] = true
|
||||
}
|
||||
for arg_id, source_index in args {
|
||||
param_index := call_param_index(mapping, source_index)
|
||||
if param_index < 0 || param_index >= len(function.params) || !function.params[param_index].comptime_value ||
|
||||
@@ -2669,6 +2762,7 @@ infer_call_comptime_values :: proc(
|
||||
matched = false
|
||||
continue
|
||||
}
|
||||
dependent := type_pattern_mentions_comptime(checker, function, prefix, param.type)
|
||||
if is_comptime_type_param(checker, param) {
|
||||
actual, ok := resolve_type_argument(checker, arg_id, pkg, file)
|
||||
if !ok {
|
||||
@@ -2688,7 +2782,7 @@ infer_call_comptime_values :: proc(
|
||||
}
|
||||
values[binding_index] = Comptime_Value{name=param.name, type=actual, kind=.Type}
|
||||
bound[binding_index] = true
|
||||
} else if is_comptime_string_param(checker, param, function) {
|
||||
} else if !dependent && is_comptime_string_param(checker, param, function) {
|
||||
text, text_ok := comptime_string_argument(checker, arg_id, pkg, file)
|
||||
if !text_ok {
|
||||
if failure != nil && len(failure^) == 0 {
|
||||
@@ -2712,7 +2806,7 @@ infer_call_comptime_values :: proc(
|
||||
kind=.String,
|
||||
}
|
||||
bound[binding_index] = true
|
||||
} else if types.is_concrete_integer(type_from_syntax(checker, param.type, function.pkg, function.file)) {
|
||||
} else if !dependent && types.is_concrete_integer(type_from_syntax(checker, param.type, function.pkg, function.file)) {
|
||||
declared := type_from_syntax(checker, param.type, function.pkg, function.file)
|
||||
constant := eval_integer_constant_in_context(checker, arg_id, pkg, file)
|
||||
if constant.kind != .Value {
|
||||
@@ -2777,29 +2871,6 @@ infer_call_comptime_values :: proc(
|
||||
bound[binding_index] = true
|
||||
}
|
||||
}
|
||||
all_bound := true
|
||||
for value_bound in bound {
|
||||
all_bound = all_bound && value_bound
|
||||
}
|
||||
if !all_bound && is_runtime_type(checker, expected) {
|
||||
if types.is_valid(function.error) && types.kind(expected, &checker.module.types) == .Fallible {
|
||||
matched = match_inferred_type_pattern(
|
||||
checker, function, prefix, function.result,
|
||||
types.fallible_success(expected, &checker.module.types), values, bound,
|
||||
source.Span{}, diagnose,
|
||||
) && matched
|
||||
matched = match_inferred_type_pattern(
|
||||
checker, function, prefix, function.error,
|
||||
types.fallible_error(expected, &checker.module.types), values, bound,
|
||||
source.Span{}, diagnose,
|
||||
) && matched
|
||||
} else if type_pattern_mentions_comptime(checker, function, prefix, function.result) {
|
||||
matched = match_inferred_type_pattern(
|
||||
checker, function, prefix, function.result, expected, values, bound,
|
||||
source.Span{}, diagnose,
|
||||
) && matched
|
||||
}
|
||||
}
|
||||
all_bound = true
|
||||
for value_bound in bound {
|
||||
all_bound = all_bound && value_bound
|
||||
@@ -3461,12 +3532,9 @@ contains_name :: proc(names: []symbol.Id, name: symbol.Id) -> bool {
|
||||
}
|
||||
|
||||
mark_expr_imports_used :: proc(checker: ^Checker, expr_id: ast.Expr_Id, file: ast.File_Id) {
|
||||
stack := checker.ast_expr_stack
|
||||
clear_dynamic_array(&stack)
|
||||
defer {
|
||||
clear_dynamic_array(&stack)
|
||||
checker.ast_expr_stack = stack
|
||||
}
|
||||
stack: [dynamic]ast.Expr_Id
|
||||
stack.allocator = checker.allocator
|
||||
defer delete(stack)
|
||||
append(&stack, expr_id)
|
||||
for len(stack) > 0 {
|
||||
id := pop(&stack)
|
||||
@@ -3658,14 +3726,14 @@ validate_declarations :: proc(checker: ^Checker) {
|
||||
locals.allocator = checker.allocator
|
||||
comptime_prefix := 0
|
||||
for param in function.params {
|
||||
dependent := param.comptime_value && type_pattern_mentions_comptime(
|
||||
checker, function, comptime_prefix, param.type,
|
||||
)
|
||||
param_type := types.INVALID
|
||||
if param.comptime_value || !has_comptime {
|
||||
if param.comptime_value && !dependent || !has_comptime {
|
||||
param_type = type_from_syntax(checker, param.type, function.pkg, function.file)
|
||||
}
|
||||
if param.comptime_value && !signature_poisoned {
|
||||
dependent := type_pattern_mentions_comptime(
|
||||
checker, function, comptime_prefix, param.type, type_params_only=true,
|
||||
)
|
||||
if function.c_abi {
|
||||
checker.template_diagnostics[function_id] = source.add(
|
||||
checker.diagnostics,
|
||||
@@ -4091,11 +4159,12 @@ validate_meta_schema :: proc(checker: ^Checker) {
|
||||
return types.find_named(&checker.module.types, u32(pkg), u32(symbol.intern(checker.symbols, name)))
|
||||
}
|
||||
field_info := find(checker, meta_package, "FieldInfo")
|
||||
array_info := find(checker, meta_package, "ArrayInfo")
|
||||
record_info := find(checker, meta_package, "RecordInfo")
|
||||
enum_info := find(checker, meta_package, "EnumInfo")
|
||||
type_info := find(checker, meta_package, "TypeInfo")
|
||||
layout := find(checker, meta_package, "Layout")
|
||||
valid := types.is_valid(field_info) && types.is_valid(record_info) && types.is_valid(enum_info) &&
|
||||
valid := types.is_valid(array_info) && types.is_valid(field_info) && types.is_valid(record_info) && types.is_valid(enum_info) &&
|
||||
types.is_valid(type_info) && types.is_valid(layout)
|
||||
layout_item, layout_ok := types.node(&checker.module.types, layout)
|
||||
layout_members := types.enum_members_for(&checker.module.types, layout)
|
||||
@@ -4104,12 +4173,22 @@ validate_meta_schema :: proc(checker: ^Checker) {
|
||||
valid = symbol_text(checker, symbol.Id(layout_members[0].name)) == "auto" &&
|
||||
symbol_text(checker, symbol.Id(layout_members[1].name)) == "c"
|
||||
}
|
||||
array_item, array_ok := types.node(&checker.module.types, array_info)
|
||||
field_item, field_ok := types.node(&checker.module.types, field_info)
|
||||
record_item, record_ok := types.node(&checker.module.types, record_info)
|
||||
enum_item, enum_ok := types.node(&checker.module.types, enum_info)
|
||||
valid = valid && field_ok && field_item.kind == .Struct && !field_item.tuple && !field_item.c_layout &&
|
||||
valid = valid && array_ok && array_item.kind == .Struct && !array_item.tuple && !array_item.c_layout &&
|
||||
field_ok && field_item.kind == .Struct && !field_item.tuple && !field_item.c_layout &&
|
||||
record_ok && record_item.kind == .Struct && !record_item.tuple && !record_item.c_layout &&
|
||||
enum_ok && enum_item.kind == .Struct && !enum_item.tuple && !enum_item.c_layout
|
||||
array_fields := types.fields_for(&checker.module.types, array_info)
|
||||
valid = valid && len(array_fields) == 2
|
||||
if valid {
|
||||
valid = symbol_text(checker, symbol.Id(array_fields[0].name)) == "child" &&
|
||||
is_type_metatype_syntax(checker, array_fields[0].type) &&
|
||||
symbol_text(checker, symbol.Id(array_fields[1].name)) == "len" &&
|
||||
types.equal(array_fields[1].type, types.USIZE)
|
||||
}
|
||||
field_fields := types.fields_for(&checker.module.types, field_info)
|
||||
valid = valid && len(field_fields) == 3
|
||||
if valid {
|
||||
@@ -4151,9 +4230,10 @@ validate_meta_schema :: proc(checker: ^Checker) {
|
||||
for tag, index in expected_tags {
|
||||
field := type_fields[index]
|
||||
if symbol_text(checker, symbol.Id(field.name)) != tag ||
|
||||
(tag == "array" && !types.equal(field.type, array_info)) ||
|
||||
(tag == "record" && !types.equal(field.type, record_info)) ||
|
||||
(tag == "enum" && !types.equal(field.type, enum_info)) ||
|
||||
(tag != "record" && tag != "enum" && !types.is_void(field.type)) {
|
||||
(tag != "array" && tag != "record" && tag != "enum" && !types.is_void(field.type)) {
|
||||
valid = false
|
||||
break
|
||||
}
|
||||
@@ -4242,7 +4322,7 @@ validate_type_nodes :: proc(checker: ^Checker) {
|
||||
}
|
||||
item_name := symbol_text(checker, symbol.Id(item.name))
|
||||
comptime_meta := meta_package &&
|
||||
(item_name == "FieldInfo" || item_name == "RecordInfo" || item_name == "EnumInfo" || item_name == "TypeInfo")
|
||||
(item_name == "ArrayInfo" || item_name == "FieldInfo" || item_name == "RecordInfo" || item_name == "EnumInfo" || item_name == "TypeInfo")
|
||||
if item.c_layout && !item.opaque && item.field_count == 0 {
|
||||
source.add(
|
||||
checker.diagnostics,
|
||||
@@ -14047,7 +14127,6 @@ check :: proc(
|
||||
append(&checker.module.strings, strings.clone(value, allocator))
|
||||
}
|
||||
checker.constant_stack.allocator = allocator
|
||||
checker.ast_expr_stack.allocator = allocator
|
||||
checker.hir_expr_stack.allocator = allocator
|
||||
checker.infer_stack.allocator = allocator
|
||||
checker.build_stack.allocator = allocator
|
||||
@@ -14120,7 +14199,6 @@ check :: proc(
|
||||
delete(checker.constants, allocator)
|
||||
delete(checker.template_diagnostics, allocator)
|
||||
delete(checker.constant_stack)
|
||||
delete(checker.ast_expr_stack)
|
||||
delete(checker.hir_expr_stack)
|
||||
delete(checker.infer_stack)
|
||||
delete(checker.build_stack)
|
||||
@@ -14154,6 +14232,7 @@ check :: proc(
|
||||
delete(checker.comptime_keys)
|
||||
delete(checker.comptime_static_values)
|
||||
delete(checker.expand_context)
|
||||
delete(checker.anon_globals)
|
||||
}
|
||||
|
||||
for function, index in ast_module.functions {
|
||||
|
||||
+153
-14
@@ -325,6 +325,8 @@ Ct_State :: struct {
|
||||
diagnostic: source.Diagnostic_Id,
|
||||
silent: bool,
|
||||
demanded: ^[dynamic]Spec_Id,
|
||||
promoted_cells: [dynamic]Ct_Cell_Id,
|
||||
promoted_globals: [dynamic]hir.Global_Id,
|
||||
}
|
||||
|
||||
Ct_Defer :: struct {
|
||||
@@ -359,6 +361,8 @@ ct_state_make :: proc(
|
||||
state.bindings.allocator = checker.allocator
|
||||
state.error_refinements.allocator = checker.allocator
|
||||
state.defers.allocator = checker.allocator
|
||||
state.promoted_cells.allocator = checker.allocator
|
||||
state.promoted_globals.allocator = checker.allocator
|
||||
for value in values {
|
||||
if value.kind == .Integer {
|
||||
id := ct_add_value(&state, Ct_Value{kind=.Integer, type=value.type, integer=value.value})
|
||||
@@ -397,6 +401,8 @@ ct_state_destroy :: proc(state: ^Ct_State) {
|
||||
delete(state.bindings)
|
||||
delete(state.error_refinements)
|
||||
delete(state.defers)
|
||||
delete(state.promoted_cells)
|
||||
delete(state.promoted_globals)
|
||||
}
|
||||
|
||||
ct_add_value :: proc(state: ^Ct_State, value: Ct_Value) -> Ct_Value_Id {
|
||||
@@ -703,7 +709,7 @@ ct_coerce_value :: proc(state: ^Ct_State, id: Ct_Value_Id, expected: types.Type,
|
||||
}
|
||||
return INVALID_CT_VALUE, ct_failf(
|
||||
state, .Not_Comptime, span, "cannot implicitly convert %s to %s at comptime",
|
||||
types.name(value.type), types.name(expected),
|
||||
type_label(state.checker, value.type), type_label(state.checker, expected),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -841,6 +847,112 @@ ct_value_references_dead_storage :: proc(state: ^Ct_State, id: Ct_Value_Id) -> b
|
||||
return false
|
||||
}
|
||||
|
||||
ct_materialize_array_pointer :: proc(
|
||||
state: ^Ct_State,
|
||||
value: Ct_Value,
|
||||
span: source.Span,
|
||||
) -> (hir.Expr_Id, bool) {
|
||||
checker := state.checker
|
||||
store := &checker.module.types
|
||||
pointer, pointer_ok := types.node(store, value.type)
|
||||
place_id := Ct_Place_Id(value.index)
|
||||
if !pointer_ok || pointer.kind != .Pointer || pointer.mutable ||
|
||||
place_id == INVALID_CT_PLACE || int(place_id) >= len(state.places) {
|
||||
return hir.INVALID_EXPR, false
|
||||
}
|
||||
place := state.places[place_id]
|
||||
if place.cell == INVALID_CT_CELL || int(place.cell) >= len(state.cells) || len(ct_place_path(state, place)) != 0 {
|
||||
return hir.INVALID_EXPR, false
|
||||
}
|
||||
root_id := state.cells[place.cell].value
|
||||
if root_id == INVALID_CT_VALUE || int(root_id) >= len(state.values) {
|
||||
return hir.INVALID_EXPR, false
|
||||
}
|
||||
root := state.values[root_id]
|
||||
array, array_ok := types.node(store, root.type)
|
||||
if !array_ok || array.kind != .Array ||
|
||||
(pointer.many && (value.active != 0 || !types.equal(pointer.child, array.child))) ||
|
||||
(!pointer.many && (value.active != -1 || !types.equal(pointer.child, root.type))) {
|
||||
return hir.INVALID_EXPR, false
|
||||
}
|
||||
global_id := hir.INVALID_GLOBAL
|
||||
for cell, index in state.promoted_cells {
|
||||
if cell == place.cell {
|
||||
global_id = state.promoted_globals[index]
|
||||
break
|
||||
}
|
||||
}
|
||||
if global_id == hir.INVALID_GLOBAL {
|
||||
root_expr := ct_materialize_value(state, root_id, span, root.type)
|
||||
if root_expr == hir.INVALID_EXPR || expr_problematic(checker, root_expr) {
|
||||
return hir.INVALID_EXPR, false
|
||||
}
|
||||
global_id = hir.Global_Id(len(checker.ast_module.globals)+len(checker.anon_globals))
|
||||
append(&checker.anon_globals, hir.Global{
|
||||
name=symbol.intern(checker.symbols, "__comptime.array"),
|
||||
type=root.type,
|
||||
expr=root_expr,
|
||||
eager=true,
|
||||
writable=false,
|
||||
external=false,
|
||||
diagnostic=source.INVALID_DIAGNOSTIC,
|
||||
})
|
||||
append(&state.promoted_cells, place.cell)
|
||||
append(&state.promoted_globals, global_id)
|
||||
}
|
||||
if checker.current_build_ctx != nil {
|
||||
add_unique_global(checker.current_build_ctx.global_reads, global_id)
|
||||
}
|
||||
global := add_hir_expr(checker, hir.Expr{
|
||||
kind=.Global, span=span, type=root.type, target=hir.global_ref(global_id),
|
||||
left=hir.INVALID_EXPR, right=hir.INVALID_EXPR, diagnostic=source.INVALID_DIAGNOSTIC,
|
||||
})
|
||||
address := add_hir_expr(checker, hir.Expr{
|
||||
kind=.Address, span=span, type=types.pointer(store, root.type, false, false), left=global,
|
||||
target=hir.INVALID_REF, right=hir.INVALID_EXPR, diagnostic=source.INVALID_DIAGNOSTIC,
|
||||
})
|
||||
return coerce_expr(checker, address, value.type, span), true
|
||||
}
|
||||
|
||||
ct_materialize_array_slice :: proc(
|
||||
state: ^Ct_State,
|
||||
value: Ct_Value,
|
||||
span: source.Span,
|
||||
) -> (hir.Expr_Id, bool) {
|
||||
store := &state.checker.module.types
|
||||
slice, slice_ok := types.node(store, value.type)
|
||||
place_id := Ct_Place_Id(value.index)
|
||||
if !slice_ok || slice.kind != .Slice || slice.mutable || value.start != 0 ||
|
||||
place_id == INVALID_CT_PLACE || int(place_id) >= len(state.places) {
|
||||
return hir.INVALID_EXPR, false
|
||||
}
|
||||
place := state.places[place_id]
|
||||
if place.cell == INVALID_CT_CELL || int(place.cell) >= len(state.cells) || len(ct_place_path(state, place)) != 0 {
|
||||
return hir.INVALID_EXPR, false
|
||||
}
|
||||
root_id := state.cells[place.cell].value
|
||||
if root_id == INVALID_CT_VALUE || int(root_id) >= len(state.values) {
|
||||
return hir.INVALID_EXPR, false
|
||||
}
|
||||
root := state.values[root_id]
|
||||
array, array_ok := types.node(store, root.type)
|
||||
if !array_ok || array.kind != .Array || u64(value.count) != array.count ||
|
||||
!types.equal(slice.child, array.child) {
|
||||
return hir.INVALID_EXPR, false
|
||||
}
|
||||
pointer := Ct_Value{
|
||||
kind=.Pointer,
|
||||
type=types.pointer(store, root.type, false, false),
|
||||
index=value.index,
|
||||
active=-1,
|
||||
}
|
||||
address, ok := ct_materialize_array_pointer(state, pointer, span)
|
||||
if !ok {
|
||||
return hir.INVALID_EXPR, false
|
||||
}
|
||||
return coerce_expr(state.checker, address, value.type, span), true
|
||||
}
|
||||
|
||||
ct_materialize_value :: proc(
|
||||
state: ^Ct_State,
|
||||
id: Ct_Value_Id,
|
||||
@@ -893,11 +1005,13 @@ ct_materialize_value :: proc(
|
||||
diagnostic=source.INVALID_DIAGNOSTIC,
|
||||
})
|
||||
case .String:
|
||||
return add_hir_expr(checker, hir.Expr{
|
||||
kind=.String, span=span, type=value.type, integer=i64(value.index),
|
||||
literal_type := string_literal_type(checker, value.index)
|
||||
literal := add_hir_expr(checker, hir.Expr{
|
||||
kind=.String, span=span, type=literal_type, integer=i64(value.index),
|
||||
target=hir.INVALID_REF, left=hir.INVALID_EXPR, right=hir.INVALID_EXPR,
|
||||
diagnostic=source.INVALID_DIAGNOSTIC,
|
||||
})
|
||||
return coerce_expr(checker, literal, value.type, span)
|
||||
case .Range:
|
||||
children := ct_child_slice(state, value)
|
||||
args := make([]hir.Expr_Id, 2, checker.allocator)
|
||||
@@ -942,9 +1056,20 @@ ct_materialize_value :: proc(
|
||||
target=hir.INVALID_REF, left=hir.INVALID_EXPR, right=hir.INVALID_EXPR,
|
||||
diagnostic=source.INVALID_DIAGNOSTIC,
|
||||
})
|
||||
case .Pointer, .Slice:
|
||||
case .Pointer:
|
||||
if result, ok := ct_materialize_array_pointer(state, value, span); ok {
|
||||
return result
|
||||
}
|
||||
if state.diagnostic == source.INVALID_DIAGNOSTIC {
|
||||
state.diagnostic = source.add(checker.diagnostics, span, "comptime storage pointers and slices cannot materialize as runtime memory")
|
||||
state.diagnostic = source.add(checker.diagnostics, span, "only immutable pointers to whole comptime arrays can materialize as runtime memory")
|
||||
}
|
||||
return invalid_hir_expr(checker, span, state.diagnostic, value.type)
|
||||
case .Slice:
|
||||
if result, ok := ct_materialize_array_slice(state, value, span); ok {
|
||||
return result
|
||||
}
|
||||
if state.diagnostic == source.INVALID_DIAGNOSTIC {
|
||||
state.diagnostic = source.add(checker.diagnostics, span, "only immutable full-array comptime slices can materialize as runtime memory")
|
||||
}
|
||||
return invalid_hir_expr(checker, span, state.diagnostic, value.type)
|
||||
case .Function:
|
||||
@@ -1184,6 +1309,9 @@ ct_eval_expr :: proc(
|
||||
if !ok || flow.kind != .Normal {
|
||||
return INVALID_CT_VALUE, flow, ok
|
||||
}
|
||||
if !symbol.is_valid(expr.name) {
|
||||
return ct_eval_tuple_field_value(state, base_id, expr.integer, expr.span)
|
||||
}
|
||||
return ct_eval_field_value(state, base_id, expr.name, expr.span)
|
||||
case .Index:
|
||||
index_id, index_flow, index_ok := ct_eval_expr(state, expr.right, types.USIZE, depth+1)
|
||||
@@ -1419,8 +1547,9 @@ ct_eval_array_expr :: proc(state: ^Ct_State, expr: ast.Expr, expected: types.Typ
|
||||
has_expected = false
|
||||
result_type = types.INVALID
|
||||
}
|
||||
start := u32(len(state.children))
|
||||
for arg in expr.args {
|
||||
values := make([]Ct_Value_Id, len(expr.args), checker.allocator)
|
||||
defer delete(values, checker.allocator)
|
||||
for arg, index in expr.args {
|
||||
value, flow, ok := ct_eval_expr(state, arg, element_type, depth+1)
|
||||
if !ok || flow.kind != .Normal {
|
||||
return INVALID_CT_VALUE, flow, ok
|
||||
@@ -1430,7 +1559,7 @@ ct_eval_array_expr :: proc(state: ^Ct_State, expr: ast.Expr, expected: types.Typ
|
||||
} else if !types.equal(element_type, state.values[value].type) {
|
||||
element_type = types.widest(element_type, state.values[value].type)
|
||||
}
|
||||
append(&state.children, value)
|
||||
values[index] = value
|
||||
}
|
||||
if !types.is_valid(element_type) {
|
||||
element_type = types.I64
|
||||
@@ -1438,14 +1567,15 @@ ct_eval_array_expr :: proc(state: ^Ct_State, expr: ast.Expr, expected: types.Typ
|
||||
if !has_expected {
|
||||
result_type = types.array(store, element_type, u64(len(expr.args)), false)
|
||||
}
|
||||
children := state.children[int(start):int(start)+len(expr.args)]
|
||||
for &child in children {
|
||||
for &child in values {
|
||||
coerced, ok := ct_coerce_value(state, child, element_type, expr.span)
|
||||
if !ok {
|
||||
return INVALID_CT_VALUE, ct_flow(.Normal), false
|
||||
}
|
||||
child = coerced
|
||||
}
|
||||
start := u32(len(state.children))
|
||||
append(&state.children, ..values)
|
||||
return ct_add_value(state, Ct_Value{kind=.Array, type=result_type, start=start, count=u32(len(expr.args))}), ct_flow(.Normal), true
|
||||
}
|
||||
|
||||
@@ -1460,8 +1590,6 @@ ct_eval_struct_expr :: proc(state: ^Ct_State, expr: ast.Expr, expected: types.Ty
|
||||
target_pkg, available := expr_package(checker, expr, state.pkg, state.file, false)
|
||||
struct_type = types.find_named(store, u32(target_pkg), u32(expr.name), file=u32(expr_lookup_file(expr, state.file))) if available else types.INVALID
|
||||
struct_type = types.resolve_alias(struct_type, store)
|
||||
} else if expr.tuple {
|
||||
struct_type = types.INVALID
|
||||
} else {
|
||||
struct_type = types.resolve_alias(expected, store)
|
||||
}
|
||||
@@ -2694,11 +2822,12 @@ ct_typeinfo_value :: proc(state: ^Ct_State, target: types.Type, span: source.Spa
|
||||
checker := state.checker
|
||||
store := &checker.module.types
|
||||
typeinfo_type := std_named_type(checker, "@std/meta", "TypeInfo")
|
||||
arrayinfo_type := std_named_type(checker, "@std/meta", "ArrayInfo")
|
||||
fieldinfo_type := std_named_type(checker, "@std/meta", "FieldInfo")
|
||||
recordinfo_type := std_named_type(checker, "@std/meta", "RecordInfo")
|
||||
enuminfo_type := std_named_type(checker, "@std/meta", "EnumInfo")
|
||||
layout_type := std_named_type(checker, "@std/meta", "Layout")
|
||||
if !types.is_valid(typeinfo_type) || !types.is_valid(fieldinfo_type) ||
|
||||
if !types.is_valid(typeinfo_type) || !types.is_valid(arrayinfo_type) || !types.is_valid(fieldinfo_type) ||
|
||||
!types.is_valid(recordinfo_type) || !types.is_valid(enuminfo_type) || !types.is_valid(layout_type) {
|
||||
return INVALID_CT_VALUE, ct_flow(.Normal), ct_fail(
|
||||
state, .Not_Comptime, span,
|
||||
@@ -2738,6 +2867,16 @@ ct_typeinfo_value :: proc(state: ^Ct_State, target: types.Type, span: source.Spa
|
||||
if !variant_ok {
|
||||
return INVALID_CT_VALUE, ct_flow(.Normal), ct_fail(state, .Not_Comptime, span, "@std/meta TypeInfo is malformed")
|
||||
}
|
||||
if tag == "array" {
|
||||
child_value := ct_add_value(state, Ct_Value{kind=.Type, type=types.INVALID, index=u64(item.child)})
|
||||
len_value := ct_add_value(state, Ct_Value{kind=.Integer, type=types.USIZE, integer=i128(item.count)})
|
||||
array_value := ct_struct_value(state, arrayinfo_type, []Ct_Value_Id{child_value, len_value})
|
||||
payload_start := u32(len(state.children))
|
||||
append(&state.children, array_value)
|
||||
return ct_add_value(state, Ct_Value{
|
||||
kind=.Struct, type=typeinfo_type, start=payload_start, count=1, active=i64(variant_index),
|
||||
}), ct_flow(.Normal), true
|
||||
}
|
||||
if tag == "enum" {
|
||||
members := types.enum_members_for(store, resolved)
|
||||
field_values := make([]Ct_Value_Id, len(members), checker.allocator)
|
||||
@@ -3659,7 +3798,7 @@ eval_static_comptime_value :: proc(
|
||||
values: []Comptime_Value = nil,
|
||||
diagnose := false,
|
||||
) -> (Comptime_Value, bool) {
|
||||
state := ct_state_make(checker, pkg, file, values=values, diagnose=false)
|
||||
state := ct_state_make(checker, pkg, file, values=values, diagnose=diagnose)
|
||||
defer ct_state_destroy(&state)
|
||||
id, flow, ok := ct_eval_expr(&state, expr, declared, 0)
|
||||
if ok && flow.kind == .Normal {
|
||||
|
||||
@@ -271,6 +271,7 @@ Global :: struct {
|
||||
expr: Expr_Id,
|
||||
static_value: i64,
|
||||
is_static: bool,
|
||||
eager: bool,
|
||||
external: bool,
|
||||
writable: bool,
|
||||
dependencies: [dynamic]Global_Id,
|
||||
|
||||
@@ -179,6 +179,7 @@ Global :: struct {
|
||||
link_name: string,
|
||||
type: types.Type,
|
||||
is_static: bool,
|
||||
eager: bool,
|
||||
external: bool,
|
||||
writable: bool,
|
||||
static_value: i64,
|
||||
|
||||
@@ -2642,7 +2642,12 @@ emit_constructor :: proc(emitter: ^Emitter) {
|
||||
)
|
||||
strings.write_string(&emitter.builder, "define internal void @bro.init() {\nentry:\n")
|
||||
for global, global_id in emitter.module.globals {
|
||||
if !global.is_static && !global.external && !global.problematic {
|
||||
if global.eager && !global.is_static && !global.external && !global.problematic {
|
||||
fmt.sbprintf(&emitter.builder, " %%g%d = call %s @bro.get.%d()\n", global_id, llvm_type(global.type, &emitter.module.types), global_id)
|
||||
}
|
||||
}
|
||||
for global, global_id in emitter.module.globals {
|
||||
if !global.eager && !global.is_static && !global.external && !global.problematic {
|
||||
fmt.sbprintf(&emitter.builder, " %%g%d = call %s @bro.get.%d()\n", global_id, llvm_type(global.type, &emitter.module.types), global_id)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1844,6 +1844,7 @@ lower :: proc(hir_module: ^hir.Module, allocator := context.allocator) -> ir.Mod
|
||||
link_name=fmt.aprintf("%s", global.link_name, allocator=allocator),
|
||||
type=global.type,
|
||||
is_static=global.is_static,
|
||||
eager=global.eager,
|
||||
external=global.external,
|
||||
writable=global.writable,
|
||||
static_value=global.static_value,
|
||||
|
||||
Reference in New Issue
Block a user