Add intrinsics.type_field_index_of

This commit is contained in:
gingerBill
2020-10-15 16:12:47 +01:00
parent f8e697dbbb
commit 5a28a7e0f5
4 changed files with 51 additions and 3 deletions
+3
View File
@@ -149,3 +149,6 @@ type_proc_return_type :: proc($T: typeid, index: int) -> typeid where type_i
type_polymorphic_record_parameter_count :: proc($T: typeid) -> typeid ---
type_polymorphic_record_parameter_value :: proc($T: typeid, index: int) -> $V ---
type_field_index_of :: proc($T: typeid, $name: string) -> uintptr ---
+2 -3
View File
@@ -323,9 +323,6 @@ Context :: struct {
}
@thread_local global_default_temp_allocator_data: Default_Temp_Allocator;
Raw_String :: struct {
data: ^byte,
len: int,
@@ -524,6 +521,8 @@ __init_context :: proc "contextless" (c: ^Context) {
c.logger.data = nil;
}
@thread_local global_default_temp_allocator_data: Default_Temp_Allocator;
@builtin
init_global_temporary_allocator :: proc(size: int, backup_allocator := context.allocator) {
default_temp_allocator_init(&global_default_temp_allocator_data, size, backup_allocator);
+42
View File
@@ -5997,6 +5997,48 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32
}
break;
case BuiltinProc_type_field_index_of:
{
Operand op = {};
Type *bt = check_type(c, ce->args[0]);
Type *type = base_type(bt);
if (type == nullptr || type == t_invalid) {
error(ce->args[0], "Expected a type for '%.*s'", LIT(builtin_name));
return false;
}
Operand x = {};
check_expr(c, &x, ce->args[1]);
if (!is_type_string(x.type) || x.mode != Addressing_Constant || x.value.kind != ExactValue_String) {
error(ce->args[1], "Expected a const string for field argument");
return false;
}
String field_name = x.value.value_string;
Selection sel = lookup_field(type, field_name, false);
if (sel.entity == nullptr) {
gbString type_str = type_to_string(bt);
error(ce->args[0],
"'%s' has no field named '%.*s'", type_str, LIT(field_name));
gb_string_free(type_str);
return false;
}
if (sel.indirect) {
gbString type_str = type_to_string(bt);
error(ce->args[0],
"Field '%.*s' is embedded via a pointer in '%s'", LIT(field_name), type_str);
gb_string_free(type_str);
return false;
}
operand->mode = Addressing_Constant;
operand->value = exact_value_u64(sel.index[0]);
operand->type = t_uintptr;
break;
}
break;
}
return true;
+4
View File
@@ -181,6 +181,8 @@ BuiltinProc__type_simple_boolean_end,
BuiltinProc_type_polymorphic_record_parameter_count,
BuiltinProc_type_polymorphic_record_parameter_value,
BuiltinProc_type_field_index_of,
BuiltinProc__type_end,
@@ -363,5 +365,7 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_COUNT] = {
{STR_LIT("type_polymorphic_record_parameter_count"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
{STR_LIT("type_polymorphic_record_parameter_value"), 2, false, Expr_Expr, BuiltinProcPkg_intrinsics},
{STR_LIT("type_field_index_of"), 2, false, Expr_Expr, BuiltinProcPkg_intrinsics},
{STR_LIT(""), 0, false, Expr_Stmt, BuiltinProcPkg_intrinsics},
};