From c75102f66e8160a5a94276d840055bde1f1213d5 Mon Sep 17 00:00:00 2001 From: Vic Nightfall Date: Sun, 13 Oct 2024 12:34:54 +0200 Subject: [PATCH] Remove Type as type holder --- src/builtins.pr | 1 + src/compiler.pr | 3 +-- src/consteval.pr | 4 +++- src/typechecking.pr | 34 +++++++++++++++++----------------- 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/builtins.pr b/src/builtins.pr index 5e97e27..5ed5997 100644 --- a/src/builtins.pr +++ b/src/builtins.pr @@ -138,5 +138,6 @@ export var Function_: &typechecking::Type = null export var Generator_: &typechecking::Type = null export var TestEnvironment_: &typechecking::Type = null +export var TypeT_: &typechecking::Type = null export var Field_: &typechecking::Type = null export var EnumValue_: &typechecking::Type = null \ No newline at end of file diff --git a/src/compiler.pr b/src/compiler.pr index 34faf06..7975f0e 100644 --- a/src/compiler.pr +++ b/src/compiler.pr @@ -8894,7 +8894,6 @@ def change_value_to_type(tpe: &typechecking::Type, state: &State) -> Value { if not value_tpe { return NO_VALUE } let reflection = toolchain::find_module("reflection") - let type_t = reflection.scope.get_type(parser::make_identifier("Type")) let fun = reflection.scope.get(parser::make_identifier("type_id")) if consteval::is_static { consteval::compile_function(fun, state.scope) @@ -8903,7 +8902,7 @@ def change_value_to_type(tpe: &typechecking::Type, state: &State) -> Value { state.module.imported.add(fun.tpe.type_name) let hash = md5::high(md5::md5(debug::type_to_str(tpe, full_name = true))) - return state.call(fun.tpe.type_name, type_t, [[ tpe = builtins::uint64_, kind = ValueKind::INT, i = hash ] !Value]) + return state.call(fun.tpe.type_name, builtins::TypeT_, [[ tpe = builtins::uint64_, kind = ValueKind::INT, i = hash ] !Value]) } def make_global_data(name: Str, value: &ByteStream, state: &State) { diff --git a/src/consteval.pr b/src/consteval.pr index 419952b..9447574 100644 --- a/src/consteval.pr +++ b/src/consteval.pr @@ -343,7 +343,7 @@ export def walk_Def(node: &parser::Node, state: &typechecking::State) { var tpe: &typechecking::Type = null if param.value.param.kw == parser::VarDecl::TYPE { - tpe = typechecking::copy(builtins::type_) + tpe = make_type_raw(typechecking::TypeKind::TYPE) if not param.value.param.name { continue } tpe.type_name = tpe.name = scope::last_path_element(param.value.param.name) } else { @@ -1166,6 +1166,8 @@ export def consteval(module: &toolchain::Module) { if module.module == "std" { toolchain::load_file_type() + } else if module.module == "reflection" { + builtins::TypeT_ = module.scope.get_type(parser::make_identifier("Type")) } } diff --git a/src/typechecking.pr b/src/typechecking.pr index 6e53fda..f2f119a 100644 --- a/src/typechecking.pr +++ b/src/typechecking.pr @@ -1640,7 +1640,7 @@ export def convert_type_score(a: &Type, b: &Type, module: &toolchain::Module, is if a.kind == TypeKind::TYPE_DEF { return convert_type_score(a.tpe, b, module, true) } - if a.kind == TypeKind::TYPE and (equals(b, pointer(builtins::Type_)) or b.may_be_type) { + if a.kind == TypeKind::TYPE and b.kind == TypeKind::TYPE or b.may_be_type { return 4 } if (is_pointer(a) or a.kind == TypeKind::REFERENCE or a.kind == TypeKind::WEAK_REF) and (@b).kind == TypeKind::NULL { @@ -2170,7 +2170,7 @@ export def overload_score( } else if right.tpe.may_be_type.tpe.tpe and equals(left.tpe.tpe, right.tpe.may_be_type.tpe.tpe) { score = 0 } - } else if equals(right.tpe, pointer(builtins::Type_)) { + /*} else if equals(right.tpe, pointer(builtins::Type_)) { if left.value { if equals(left.value.value_tpe, right.tpe.tpe.tpe) { score = 0 @@ -2181,7 +2181,7 @@ export def overload_score( } else if not left.tpe.tpe { score = 0 } - } + }*/ } else if is_type(right.tpe) { if left.value and right.value and equals(left.value.value_tpe, right.value.value_tpe) { score = 0 @@ -3231,17 +3231,17 @@ def walk_ArrayStaticT(node: &parser::Node, state: &State) { size = value.i } - let tpe2 = copy(builtins::Type_) // TODO Use TypeKind::Type + let tpe2 = make_type_raw(TypeKind::TYPE) tpe2._tpe = type_lookup(node, state) - node.tpe = pointer(tpe2) + node.tpe = tpe2 } def walk_TypeOfT(node: &parser::Node, state: &State) { let expr = node.value.expr walk(node, expr, state) - let tpe = copy(builtins::Type_) // TODO Use TypeKind::Type + let tpe = make_type_raw(TypeKind::TYPE) tpe._tpe = expr.tpe - node.tpe = pointer(tpe) + node.tpe = tpe } def walk_Null(node: &parser::Node, state: &State) { @@ -3361,9 +3361,9 @@ def walk_Identifier(node: &parser::Node, state: &State) { scope::add_reference(value, node) if value.tpe and value.tpe.kind == TypeKind::TYPE { - let tpe = copy(builtins::Type_) // TODO Use TypeKind::Type + let tpe = make_type_raw(TypeKind::TYPE) tpe._tpe = value.value.value_tpe - node.tpe = pointer(tpe) + node.tpe = tpe } else { node.tpe = value.tpe } @@ -5183,7 +5183,7 @@ export def walk_Call(node: &parser::Node, dry_run: bool, state: &State) -> bool node = n ] !NamedParameter - if equals(np.tpe, pointer(builtins::Type_)) { + if np.tpe and np.tpe.kind == TypeKind::TYPE { np.value = [ kind = compiler::ValueKind::TYPE, tpe = builtins::type_, value_tpe = np.tpe.tpe.tpe ] !&compiler::Value } @@ -5217,7 +5217,7 @@ export def walk_Call(node: &parser::Node, dry_run: bool, state: &State) -> bool node = (@n).value.named_arg.name ] !NamedParameter - if equals(np.tpe, builtins::Type_) { + if np.tpe and np.tpe.kind == TypeKind::TYPE { np.value = [ kind = compiler::ValueKind::TYPE, tpe = builtins::type_, value_tpe = np.tpe.tpe.tpe ] !&compiler::Value } @@ -5688,7 +5688,7 @@ def walk_Ptr(node: &parser::Node, state: &State) { node.tpe = pointer(tpe, tpe.kw) - if equals(tpe, pointer(builtins::Type_)) or tpe.may_be_type { + if tpe and tpe.kind == TypeKind::TYPE or tpe.may_be_type { node.tpe.may_be_type = convert_ambiguous_expr_to_type(node, state).tpe } } @@ -5815,7 +5815,7 @@ def walk_MemberAccess(node: &parser::Node, state: &State) { if tpe and not is_ref_or_weak(tpe) and tpe.kind != TypeKind::TUNION { errors::errorn(node, "Can't get type from `" + debug::type_to_str(tpe) + "`, only references or variants allowed") } else { - node.tpe = pointer(builtins::Type_) + node.tpe = make_type_raw(TypeKind::TYPE) } return } @@ -5988,7 +5988,7 @@ def walk_StructLit(node: &parser::Node, state: &State) { ret_tpe.align = curtpe.align // Check if we have a type array, in this case it might also be a type - if equals(curtpe, pointer(builtins::Type_)) or curtpe.may_be_type { + if curtpe and curtpe.kind == TypeKind::TYPE or curtpe.may_be_type { ret_tpe.may_be_type = convert_ambiguous_expr_to_type(node, state).tpe } @@ -6133,7 +6133,7 @@ def walk_ComparisionOp(node: &parser::Node, state: &State) { } if node.kind != parser::NodeKind::FUNC_CALL { - if builtins::Type_ and equals(left.tpe, pointer(builtins::Type_)) { + if left.tpe and left.tpe.kind == TypeKind::TYPE { let equals = scope::get(toolchain::runtime_.scope, parser::make_identifier("equals"), not consteval::is_static) consteval::compile_function(equals, toolchain::runtime_.scope) } @@ -6326,9 +6326,9 @@ export def walk(parent: &parser::Node, node: &parser::Node, state: &State) { parser::NodeKind::ARRAY_T, parser::NodeKind::WEAK_REF_T, parser::NodeKind::TYPE_CONSTRUCTOR, parser::NodeKind::FUNCTION_T, parser::NodeKind::CLOSURE_T, parser::NodeKind::TUPLE_T - let tpe = copy(builtins::Type_) // TODO use TypeKind::TYPE + let tpe = make_type_raw(TypeKind::TYPE) tpe._tpe = type_lookup(node, state) - node.tpe = pointer(tpe) + node.tpe = tpe case parser::NodeKind::ARRAY_STATIC_T walk_ArrayStaticT(node, state) case parser::NodeKind::TYPE_OF_T