Rewriting types into ints at typechecking phase

This commit is contained in:
Krzosa Karol
2022-06-19 16:01:58 +02:00
parent 69fe514485
commit 02743c86d8
4 changed files with 54 additions and 19 deletions

View File

@@ -825,19 +825,17 @@ typedef struct String{
} }
} }
Scratch scratch; For(pctx->all_types){
for(S64 i = 0; i < pctx->type_map.cap; i++){ Scratch scratch;
Map_Key_Value *it = pctx->type_map.data + i; Ast_Type *type = it;
if(!it->occupied) continue;
Ast_Type *type = (Ast_Type *)it->value;
if(type->kind == TYPE_SLICE){ if(type->kind == TYPE_SLICE){
genln("typedef struct Slice%llu{", type->type_id); genln("typedef struct Slice%llu{", type->type_id);
global_indent++; global_indent++;
genln("S64 len;"); genln("S64 len;");
genln(""); genln("");
gen_simple_decl(type->base); gen_simple_decl(type_pointer(type->base), pctx->intern("data"_s));
gen(" data;"); gen(";");
global_indent--; global_indent--;
genln("} Slice%llu;", type->type_id); genln("} Slice%llu;", type->type_id);
} }

View File

@@ -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 Some_Struct :: struct
thing: int thing: int
main :: (argc: int, argv: **char): int test_type :: ()
type1: Type = **[]int type1: Type = **[]int
type2 := []*[]*Some_Struct type2 := []*[]*Some_Struct
type3 := Some_Struct type3 := Some_Struct
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)

View File

@@ -362,9 +362,9 @@ make_sure_value_is_compatible_with_type(Token *pos, Operand *expr, Ast_Type *typ
assert(type); assert(type);
expr->type = type; expr->type = type;
} }
else if(is_any(type)){ // else if(is_any(type)){
expr->type = type; // expr->type = type;
} // }
else if(is_void_pointer(type) && is_pointer(expr->type)){ else if(is_void_pointer(type) && is_pointer(expr->type)){
expr->type = 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"); if(type.type != type_type) compiler_error(node->pos, "Prefix array operator is only allowed on types");
type_complete(type.type_val); 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)){ if(!is_flag_set(flags, RESOLVE_TYPESPEC)){
rewrite_into_const(node, Ast_Array, type.value); rewrite_into_const(node, Ast_Array, type.value);
} }
if(node->expr){ node->resolved_type = type.type_val;
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);
}
return operand_type(node->resolved_type); return operand_type(node->resolved_type);
BREAK(); BREAK();
} }

View File

@@ -146,7 +146,6 @@ type_pointer(Ast_Type *base){
function Ast_Type * function Ast_Type *
type_slice(Ast_Type *base, Ast *ast){ type_slice(Ast_Type *base, Ast *ast){
assert(!is_array(base));
U64 hash_base = hash_ptr(base); U64 hash_base = hash_ptr(base);
U64 hash = hash_mix(hash_base, hash_u64(ARRAY_SIZE_SLICE)); U64 hash = hash_mix(hash_base, hash_u64(ARRAY_SIZE_SLICE));
Ast_Type *result = (Ast_Type *)map_get(&pctx->type_map, hash); Ast_Type *result = (Ast_Type *)map_get(&pctx->type_map, hash);