Skip to content

Commit

Permalink
Remove Type as type holder
Browse files Browse the repository at this point in the history
  • Loading branch information
Victorious3 committed Oct 13, 2024
1 parent be3954c commit c75102f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/builtins.pr
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 1 addition & 2 deletions src/compiler.pr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion src/consteval.pr
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"))
}
}

Expand Down
34 changes: 17 additions & 17 deletions src/typechecking.pr
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c75102f

Please sign in to comment.