Cleanup Slice and Tuple generation

This commit is contained in:
Krzosa Karol
2022-06-16 12:16:15 +02:00
parent c604b44458
commit 5345894e3e
6 changed files with 48 additions and 28 deletions

View File

@@ -65,7 +65,7 @@ string_simple_decl_prefix(Allocator *a, Ast_Type *ast, bool scope_names){
}break; }break;
case TYPE_SLICE:{ case TYPE_SLICE:{
String string = string_simple_decl_prefix(a, ast->base, true); String string = string_simple_decl_prefix(a, ast->base, true);
string = string_fmt(a, "SliceOf%Q", string); string = string_fmt(a, "Slice%llu ", ast->type_id);
return string; return string;
}break; }break;
case TYPE_STRUCT: { case TYPE_STRUCT: {
@@ -727,6 +727,36 @@ typedef struct String{
} }
} }
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;
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;");
global_indent--;
genln("} Slice%llu;", type->type_id);
}
else if(type->kind == TYPE_TUPLE){
genln("typedef struct Tuple%llu{", type->type_id);
global_indent++;
For(type->agg.members){
genln("");
gen_simple_decl(it.type);
gen(" m%llu;", type->agg.members.get_index(&it));
}
global_indent--;
genln("} Tuple%llu;", type->type_id);
}
}
For(pctx->ordered_decls){ For(pctx->ordered_decls){
genln(""); genln("");
gen_ast(it); gen_ast(it);

View File

@@ -180,7 +180,8 @@ struct Parse_Ctx:Lexer{
Allocator *perm; // Stores: AST, tokens, interns Allocator *perm; // Stores: AST, tokens, interns
Allocator *heap; Allocator *heap;
U64 unique_ids; U64 type_ids;
U64 unique_ids; // @Debug
Map type_map; Map type_map;
Ast_Module *builtins; Ast_Module *builtins;
@@ -188,6 +189,7 @@ struct Parse_Ctx:Lexer{
Ast_Scope *currently_parsed_scope; Ast_Scope *currently_parsed_scope;
Ast_File *currently_parsed_file; Ast_File *currently_parsed_file;
Array<Ast_Decl *> ordered_decls; Array<Ast_Decl *> ordered_decls;
Array<Ast_Decl *> c_backend_decls;
S64 indent; S64 indent;
String_Builder gen; String_Builder gen;

View File

@@ -156,6 +156,9 @@ window_procedure :: (hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM): LRE
multiple_return_values :: (i: int): int, int multiple_return_values :: (i: int): int, int
return i, i*2 return i, i*2
slice :: (i: []int)
return
WinMain :: (hInstance: HINSTANCE, hPrevInstance: HINSTANCE, lpCmdLine: LPSTR, nShowCmd: int): int WinMain :: (hInstance: HINSTANCE, hPrevInstance: HINSTANCE, lpCmdLine: LPSTR, nShowCmd: int): int
a, b := multiple_return_values(10) a, b := multiple_return_values(10)
window_name := string_to_string16("Have a wonderful day! 豈 更 車 賈 滑 串 句 龜 ") window_name := string_to_string16("Have a wonderful day! 豈 更 車 賈 滑 串 句 龜 ")

View File

@@ -428,9 +428,8 @@ resolve_stmt(Ast *ast, Ast_Type *ret){
Operand op = resolve_expr(it, AST_CAN_BE_NULL); Operand op = resolve_expr(it, AST_CAN_BE_NULL);
types.add(op.type); types.add(op.type);
} }
Ast_Type *type = type_try_tupling(types, node); Ast_Type *type = type_try_tupling(types, node);
if(!type && ret != type_void)
compiler_error(node->pos, "Function expects a void return value but the returned value is %s", docname(type));
Value value = {}; value.type = type; Value value = {}; value.type = type;
value = convert_untyped_to_typed(node->pos, value, ret); value = convert_untyped_to_typed(node->pos, value, ret);
@@ -781,25 +780,6 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_context){
node->resolved_type = type_array(type.type_val, bigint_as_unsigned(&expr.big_int_val)); node->resolved_type = type_array(type.type_val, bigint_as_unsigned(&expr.big_int_val));
} else{ } else{
node->resolved_type = type_slice(type.type_val, node); node->resolved_type = type_slice(type.type_val, node);
{ // @c_backend
Ast_Scope *scope = ast_decl_scope(0, pctx->heap, (Ast_File *)node->parent_scope->file);
Ast_Decl *len_var = ast_var(0, 0, pctx->intern("len"_s), 0);
len_var->type = type_s64;
scope->decls.add(len_var);
Ast_Decl *pointer_var = ast_var(0, 0, pctx->intern("data"_s), 0);
pointer_var->type = type_pointer(type.type_val);
scope->decls.add(pointer_var);
Ast_Decl *struct_decl = ast_struct(0, scope);
String name = gen_string_simple_decl(pctx->perm, node->resolved_type);
struct_decl->name = pctx->intern(name);
struct_decl->type = type_type;
struct_decl->type_val = node->resolved_type;
pctx->ordered_decls.add(struct_decl);
}
} }

View File

@@ -110,6 +110,7 @@ type_new(Allocator *allocator, Ast_Type_Kind kind, SizeU size, SizeU align){
result->kind = kind; result->kind = kind;
result->size = size; result->size = size;
result->align = align; result->align = align;
result->type_id = pctx->type_ids++;
return result; return result;
} }
@@ -135,11 +136,12 @@ 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);
if(result){ if(result){
assert(result->kind == TYPE_ARRAY); assert(result->kind == TYPE_SLICE);
assert(result->arr.base == base); assert(result->arr.base == base);
return result; return result;
} }
@@ -149,12 +151,13 @@ type_slice(Ast_Type *base, Ast *ast){
result->arr.base = base; result->arr.base = base;
result->arr.slice_hash = hash; result->arr.slice_hash = hash;
result->ast = ast; result->ast = ast;
map_insert(&pctx->type_map, hash, result);
return result; return result;
} }
function Ast_Type * function Ast_Type *
type_try_tupling(Array<Ast_Type *> types, Ast *ast){ type_try_tupling(Array<Ast_Type *> types, Ast *ast){
assert(types.len != 0); if(types.len == 0) return type_void;
if(types.len == 1) return types[0]; if(types.len == 1) return types[0];
U64 hash = 13; U64 hash = 13;
@@ -179,6 +182,7 @@ type_try_tupling(Array<Ast_Type *> types, Ast *ast){
} }
map_insert(&pctx->type_map, hash, result); map_insert(&pctx->type_map, hash, result);
assert(result->agg.members.len > 1); assert(result->agg.members.len > 1);
return result; return result;
} }

View File

@@ -79,6 +79,7 @@ struct Ast_Type{
SizeU size; SizeU size;
SizeU align; SizeU align;
S32 is_unsigned; S32 is_unsigned;
U64 type_id;
Ast *ast; Ast *ast;
union{ union{