From 02743c86d83c504c0acefde21b0d0dba5848d264 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Sun, 19 Jun 2022 16:01:58 +0200 Subject: [PATCH] Rewriting types into ints at typechecking phase --- ccodegen.cpp | 12 +++++------- programs/any.kl | 41 +++++++++++++++++++++++++++++++++++++++-- typechecking.cpp | 19 ++++++++++--------- typechecking.h | 1 - 4 files changed, 54 insertions(+), 19 deletions(-) diff --git a/ccodegen.cpp b/ccodegen.cpp index 9c3d181..25739ee 100644 --- a/ccodegen.cpp +++ b/ccodegen.cpp @@ -825,19 +825,17 @@ typedef struct String{ } } - Scratch scratch; - for(S64 i = 0; i < pctx->type_map.cap; i++){ - Map_Key_Value *it = pctx->type_map.data + i; - if(!it->occupied) continue; - Ast_Type *type = (Ast_Type *)it->value; + For(pctx->all_types){ + Scratch scratch; + Ast_Type *type = it; if(type->kind == TYPE_SLICE){ genln("typedef struct Slice%llu{", type->type_id); global_indent++; genln("S64 len;"); genln(""); - gen_simple_decl(type->base); - gen(" data;"); + gen_simple_decl(type_pointer(type->base), pctx->intern("data"_s)); + gen(";"); global_indent--; genln("} Slice%llu;", type->type_id); } diff --git a/programs/any.kl b/programs/any.kl index 07e0f3c..efd777a 100644 --- a/programs/any.kl +++ b/programs/any.kl @@ -1,9 +1,46 @@ + +main :: (argc: int, argv: **char): int + test_type() + + +test_any :: () + some_int_value := 10 + thing: Any = some_int_value + other_any: Any = thing + imp_any := thing + + Some_Struct :: struct thing: int -main :: (argc: int, argv: **char): int +test_type :: () type1: Type = **[]int type2 := []*[]*Some_Struct type3 := Some_Struct - \ No newline at end of file + type4 := [32]Some_Struct + type5 := [][32]*Some_Struct + type6 := [][32]*[16][]Some_Struct + + t1 := get_type_info(type1) + assert(t1.kind == Type_Info_Kind.POINTER) + + t2 := get_type_info(type2) + assert(t2.kind == Type_Info_Kind.SLICE) + + t3 := get_type_info(type3) + assert(t3.kind == Type_Info_Kind.STRUCT) + + t4 := get_type_info(type4) + assert(t4.array_size == 32) + assert(t4.kind == Type_Info_Kind.ARRAY) + + t5 := get_type_info(type5) + t51 := get_type_info(t5.base_type) + assert(t51.kind == Type_Info_Kind.ARRAY) + + + t6 := get_type_info(type6) + t61 := get_type_info(t6.base_type) + t62 := get_type_info(t61.base_type) + assert(t62.kind == Type_Info_Kind.POINTER) diff --git a/typechecking.cpp b/typechecking.cpp index 69f6020..7a09459 100644 --- a/typechecking.cpp +++ b/typechecking.cpp @@ -362,9 +362,9 @@ make_sure_value_is_compatible_with_type(Token *pos, Operand *expr, Ast_Type *typ assert(type); expr->type = type; } - else if(is_any(type)){ - expr->type = type; - } + // else if(is_any(type)){ + // expr->type = type; + // } else if(is_void_pointer(type) && is_pointer(expr->type)){ expr->type = type; } @@ -888,16 +888,17 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_context){ if(type.type != type_type) compiler_error(node->pos, "Prefix array operator is only allowed on types"); type_complete(type.type_val); + if(node->expr){ + type.type_val = type_array(type.type_val, bigint_as_unsigned(&expr.big_int_val)); + } else{ + type.type_val = type_slice(type.type_val, node); + } + if(!is_flag_set(flags, RESOLVE_TYPESPEC)){ rewrite_into_const(node, Ast_Array, type.value); } - if(node->expr){ - node->resolved_type = type_array(type.type_val, bigint_as_unsigned(&expr.big_int_val)); - } else{ - node->resolved_type = type_slice(type.type_val, node); - } - + node->resolved_type = type.type_val; return operand_type(node->resolved_type); BREAK(); } diff --git a/typechecking.h b/typechecking.h index 096689b..a352c25 100644 --- a/typechecking.h +++ b/typechecking.h @@ -146,7 +146,6 @@ type_pointer(Ast_Type *base){ function Ast_Type * type_slice(Ast_Type *base, Ast *ast){ - assert(!is_array(base)); U64 hash_base = hash_ptr(base); U64 hash = hash_mix(hash_base, hash_u64(ARRAY_SIZE_SLICE)); Ast_Type *result = (Ast_Type *)map_get(&pctx->type_map, hash);