From 50fedc8f44259f2e08add4dceebb5e12c6232e90 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sat, 24 Feb 2024 14:29:40 -0500 Subject: Fixing up structs --- structs.c | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 99 insertions(+), 13 deletions(-) (limited to 'structs.c') diff --git a/structs.c b/structs.c index d8576df2..154c3b36 100644 --- a/structs.c +++ b/structs.c @@ -11,6 +11,30 @@ #include "typecheck.h" #include "util.h" +static bool is_plain_data(env_t *env, type_t *t) +{ + switch (t->tag) { + case BoolType: case IntType: case NumType: case PointerType: case FunctionType: + return true; + case StructType: { + for (arg_t *arg = Match(t, StructType)->fields; arg; arg = arg->next) { + if (!is_plain_data(env, get_arg_type(env, arg))) + return false; + } + return true; + } + case EnumType: { + for (tag_t *tag = Match(t, EnumType)->tags; tag; tag = tag->next) { + if (!is_plain_data(env, tag->type)) + return false; + } + return true; + } + default: + return false; + } +} + static CORD compile_str_method(env_t *env, ast_t *ast) { auto def = Match(ast, StructDef); @@ -22,8 +46,8 @@ static CORD compile_str_method(env_t *env, ast_t *ast) } else { CORD_appendf(&str_func, "\treturn CORD_all(use_color ? \"\\x1b[0;1m%s\\x1b[m(\" : \"%s(\"", def->name, def->name); for (arg_ast_t *field = def->fields; field; field = field->next) { - type_t *field_t = parse_type_ast(env, field->type); - CORD field_str = expr_as_string(env, CORD_cat("obj->", field->name), field_t, "use_color"); + type_t *field_type = get_arg_ast_type(env, field); + CORD field_str = expr_as_string(env, CORD_cat("obj->", field->name), field_type, "use_color"); CORD_appendf(&str_func, ", \"%s=\", %r", field->name, field_str); if (field->next) CORD_appendf(&str_func, ", \", \""); } @@ -39,15 +63,65 @@ static CORD compile_compare_method(env_t *env, ast_t *ast) "_t *y, const TypeInfo *info) {\n" "int diff;\n"); for (arg_ast_t *field = def->fields; field; field = field->next) { - type_t *field_type = parse_type_ast(env, field->type); - cmp_func = CORD_all(cmp_func, "diff = generic_compare(&x->", field->name, ", &y->", field->name, ", ", - compile_type_info(env, field_type), ");\n" - "if (diff != 0) return diff;\n"); + type_t *field_type = get_arg_ast_type(env, field); + switch (field_type->tag) { + case BoolType: case IntType: case NumType: case PointerType: case FunctionType: + cmp_func = CORD_all(cmp_func, "diff = (x->", field->name, " > y->", field->name, ") - (x->", field->name, " < y->", field->name, ");"); + break; + case StringType: + cmp_func = CORD_all(cmp_func, "diff = CORD_cmp(x->", field->name, ", y->", field->name, ");"); + break; + default: + cmp_func = CORD_all(cmp_func, "diff = generic_compare(&x->", field->name, ", &y->", field->name, ", ", + compile_type_info(env, field_type), ");\n"); + break; + } + cmp_func = CORD_all(cmp_func, "if (diff != 0) return diff;\n"); } cmp_func = CORD_all(cmp_func, "return 0;\n}\n"); return cmp_func; } +static CORD compile_equals_method(env_t *env, ast_t *ast) +{ + auto def = Match(ast, StructDef); + CORD eq_func = CORD_all("static bool ", def->name, "__equal(const ", def->name, "_t *x, const ", def->name, + "_t *y, const TypeInfo *info) {\n"); + for (arg_ast_t *field = def->fields; field; field = field->next) { + type_t *field_type = parse_type_ast(env, field->type); + switch (field_type->tag) { + case BoolType: case IntType: case NumType: case PointerType: case FunctionType: + eq_func = CORD_all(eq_func, "if (x->", field->name, " != y->", field->name, ") return no;\n"); + break; + case StringType: + eq_func = CORD_all(eq_func, "if (CORD_cmp(x->", field->name, ", y->", field->name, ") != 0) return no;\n"); + break; + default: + eq_func = CORD_all(eq_func, "if (!generic_equal(&x->", field->name, ", &y->", field->name, ", ", + compile_type_info(env, field_type), ")) return no;\n"); + break; + } + } + eq_func = CORD_all(eq_func, "return yes;\n}\n"); + return eq_func; +} + +static CORD compile_hash_method(env_t *env, ast_t *ast) +{ + auto def = Match(ast, StructDef); + CORD hash_func = CORD_all("static uint32_t ", def->name, "__hash(const ", def->name, "_t *obj, const TypeInfo *info) {\n" + "uint32_t field_hashes[] = {"); + for (arg_ast_t *field = def->fields; field; field = field->next) { + type_t *field_type = get_arg_ast_type(env, field); + hash_func = CORD_all(hash_func, "\ngeneric_hash(&obj->", field->name, ", ", compile_type_info(env, field_type), "),"); + } + hash_func = CORD_all(hash_func, "};\n" + "uint32_t hash;\n" + "halfsiphash(&field_hashes, sizeof(field_hashes), SSS_HASH_VECTOR, (uint8_t*)&hash, sizeof(hash));\n" + "return hash;\n}\n"); + return hash_func; +} + void compile_struct_def(env_t *env, ast_t *ast) { auto def = Match(ast, StructDef); @@ -66,13 +140,25 @@ void compile_struct_def(env_t *env, ast_t *ast) CORD_appendf(&env->code->typedefs, "typedef struct { TypeInfo type; } %s_namespace_t;\n", def->name); CORD_appendf(&env->code->typedefs, "extern %s_namespace_t %s;\n", def->name, def->name); - env->code->funcs = CORD_all( - env->code->funcs, compile_compare_method(env, ast), compile_str_method(env, ast)); - env->code->typeinfos = CORD_all( - env->code->typeinfos, - "public ", def->name, "_namespace_t ", def->name, " = {{.tag=CustomInfo, .CustomInfo={" - ".as_str=(void*)", def->name, "__as_str, " - ".compare=(void*)", def->name, "__compare}}};\n"); + type_t *t = Table_str_get(env->types, def->name); + CORD typeinfo = CORD_asprintf("public %s_namespace_t %s = {{%zu, %zu, {.tag=CustomInfo, .CustomInfo={", + def->name, def->name, type_size(t), type_align(t)); + + typeinfo = CORD_all(typeinfo, ".as_str=(void*)", def->name, "__as_str, "); + env->code->funcs = CORD_all(env->code->funcs, compile_str_method(env, ast)); + if (!t || !is_plain_data(env, t)) { + env->code->funcs = CORD_all( + env->code->funcs, compile_equals_method(env, ast), compile_compare_method(env, ast), + compile_hash_method(env, ast)); + typeinfo = CORD_all( + typeinfo, + ".as_str=(void*)", def->name, "__as_str, " + ".equal=(void*)", def->name, "__equal, " + ".hash=(void*)", def->name, "__hash, " + ".compare=(void*)", def->name, "__compare"); + } + typeinfo = CORD_cat(typeinfo, "}}}};\n"); + env->code->typeinfos = CORD_all(env->code->typeinfos, typeinfo); } // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 -- cgit v1.2.3