Просмотр исходного кода

Add `intrinsics.type_field_index_of`

gingerBill 4 лет назад
Родитель
Сommit
5a28a7e0f5
4 измененных файлов с 51 добавлено и 3 удалено
  1. 3 0
      core/intrinsics/intrinsics.odin
  2. 2 3
      core/runtime/core.odin
  3. 42 0
      src/check_expr.cpp
  4. 4 0
      src/checker_builtin_procs.hpp

+ 3 - 0
core/intrinsics/intrinsics.odin

@@ -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
core/runtime/core.odin

@@ -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 - 0
src/check_expr.cpp

@@ -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 - 0
src/checker_builtin_procs.hpp

@@ -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},
 };