Cleanup Slice and Tuple generation
This commit is contained in:
32
ccodegen.cpp
32
ccodegen.cpp
@@ -65,7 +65,7 @@ string_simple_decl_prefix(Allocator *a, Ast_Type *ast, bool scope_names){
|
||||
}break;
|
||||
case TYPE_SLICE:{
|
||||
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;
|
||||
}break;
|
||||
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){
|
||||
genln("");
|
||||
gen_ast(it);
|
||||
|
||||
@@ -180,7 +180,8 @@ struct Parse_Ctx:Lexer{
|
||||
Allocator *perm; // Stores: AST, tokens, interns
|
||||
Allocator *heap;
|
||||
|
||||
U64 unique_ids;
|
||||
U64 type_ids;
|
||||
U64 unique_ids; // @Debug
|
||||
Map type_map;
|
||||
|
||||
Ast_Module *builtins;
|
||||
@@ -188,6 +189,7 @@ struct Parse_Ctx:Lexer{
|
||||
Ast_Scope *currently_parsed_scope;
|
||||
Ast_File *currently_parsed_file;
|
||||
Array<Ast_Decl *> ordered_decls;
|
||||
Array<Ast_Decl *> c_backend_decls;
|
||||
|
||||
S64 indent;
|
||||
String_Builder gen;
|
||||
|
||||
@@ -156,6 +156,9 @@ window_procedure :: (hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM): LRE
|
||||
multiple_return_values :: (i: int): int, int
|
||||
return i, i*2
|
||||
|
||||
slice :: (i: []int)
|
||||
return
|
||||
|
||||
WinMain :: (hInstance: HINSTANCE, hPrevInstance: HINSTANCE, lpCmdLine: LPSTR, nShowCmd: int): int
|
||||
a, b := multiple_return_values(10)
|
||||
window_name := string_to_string16("Have a wonderful day! 豈 更 車 賈 滑 串 句 龜 ")
|
||||
|
||||
@@ -428,9 +428,8 @@ resolve_stmt(Ast *ast, Ast_Type *ret){
|
||||
Operand op = resolve_expr(it, AST_CAN_BE_NULL);
|
||||
types.add(op.type);
|
||||
}
|
||||
|
||||
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 = 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));
|
||||
} else{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -110,6 +110,7 @@ type_new(Allocator *allocator, Ast_Type_Kind kind, SizeU size, SizeU align){
|
||||
result->kind = kind;
|
||||
result->size = size;
|
||||
result->align = align;
|
||||
result->type_id = pctx->type_ids++;
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -135,26 +136,28 @@ 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);
|
||||
if(result){
|
||||
assert(result->kind == TYPE_ARRAY);
|
||||
assert(result->kind == TYPE_SLICE);
|
||||
assert(result->arr.base == base);
|
||||
return result;
|
||||
}
|
||||
|
||||
struct Slice{void *p; S64 len;};
|
||||
result = type_new(pctx->perm, TYPE_SLICE, sizeof(Slice), alignof(Slice));
|
||||
result->arr.base = base;
|
||||
result = type_new(pctx->perm, TYPE_SLICE, sizeof(Slice), alignof(Slice));
|
||||
result->arr.base = base;
|
||||
result->arr.slice_hash = hash;
|
||||
result->ast = ast;
|
||||
result->ast = ast;
|
||||
map_insert(&pctx->type_map, hash, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
function Ast_Type *
|
||||
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];
|
||||
|
||||
U64 hash = 13;
|
||||
@@ -179,6 +182,7 @@ type_try_tupling(Array<Ast_Type *> types, Ast *ast){
|
||||
}
|
||||
map_insert(&pctx->type_map, hash, result);
|
||||
assert(result->agg.members.len > 1);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user