diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-03-17 22:17:21 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-03-17 22:17:21 -0400 |
| commit | 5f3dac786c6b27f04d5cf7f27ea994135d26041e (patch) | |
| tree | f47f27045a838494bcbbeaf9694cc4e03165d31a /enums.c | |
| parent | 0f70411cbf6548febfc0f664db72a246a92d4225 (diff) | |
Clean up codegen for empty tagged data
Diffstat (limited to 'enums.c')
| -rw-r--r-- | enums.c | 40 |
1 files changed, 29 insertions, 11 deletions
@@ -20,6 +20,12 @@ static CORD compile_str_method(env_t *env, ast_t *ast) "\tif (!obj) return \"", def->name, "\";\n" "switch (obj->$tag) {\n"); for (tag_ast_t *tag = def->tags; tag; tag = tag->next) { + if (!tag->fields) { + str_func = CORD_all(str_func, "\tcase $tag$", full_name, "$", tag->name, ": return use_color ? \"\\x1b[36;1m", + def->name, ".", tag->name, "\\x1b[m\" : \"", def->name, ".", tag->name, "\";\n"); + continue; + } + str_func = CORD_all(str_func, "\tcase $tag$", full_name, "$", tag->name, ": return CORD_all(use_color ? \"\\x1b[36;1m", def->name, ".", tag->name, "\\x1b[m(\" : \"", def->name, ".", tag->name, "(\""); @@ -51,9 +57,13 @@ static CORD compile_compare_method(env_t *env, ast_t *ast) "if (diff) return diff;\n" "switch (x->$tag) {\n"); for (tag_ast_t *tag = def->tags; tag; tag = tag->next) { - type_t *tag_type = Table_str_get(*env->types, CORD_to_const_char_star(CORD_all(def->name, "$", tag->name))); - cmp_func = CORD_all(cmp_func, "\tcase $tag$", full_name, "$", tag->name, ": " - "return generic_compare(&x->", tag->name, ", &y->", tag->name, ", ", compile_type_info(env, tag_type), ");\n"); + if (tag->fields) { + type_t *tag_type = Table_str_get(*env->types, CORD_to_const_char_star(CORD_all(def->name, "$", tag->name))); + cmp_func = CORD_all(cmp_func, "\tcase $tag$", full_name, "$", tag->name, ": " + "return generic_compare(&x->", tag->name, ", &y->", tag->name, ", ", compile_type_info(env, tag_type), ");\n"); + } else { + cmp_func = CORD_all(cmp_func, "\tcase $tag$", full_name, "$", tag->name, ": return 0;\n"); + } } cmp_func = CORD_all(cmp_func, "default: return 0;\n}\n}\n"); return cmp_func; @@ -69,9 +79,13 @@ static CORD compile_equals_method(env_t *env, ast_t *ast) "if (x->$tag != y->$tag) return no;\n" "switch (x->$tag) {\n"); for (tag_ast_t *tag = def->tags; tag; tag = tag->next) { - type_t *tag_type = Table_str_get(*env->types, CORD_to_const_char_star(CORD_all(def->name, "$", tag->name))); - eq_func = CORD_all(eq_func, "\tcase $tag$", full_name, "$", tag->name, ": " - "return generic_equal(&x->", tag->name, ", &y->", tag->name, ", ", compile_type_info(env, tag_type), ");\n"); + if (tag->fields) { + type_t *tag_type = Table_str_get(*env->types, CORD_to_const_char_star(CORD_all(def->name, "$", tag->name))); + eq_func = CORD_all(eq_func, "\tcase $tag$", full_name, "$", tag->name, ": " + "return generic_equal(&x->", tag->name, ", &y->", tag->name, ", ", compile_type_info(env, tag_type), ");\n"); + } else { + eq_func = CORD_all(eq_func, "\tcase $tag$", full_name, "$", tag->name, ": return yes;\n"); + } } eq_func = CORD_all(eq_func, "default: return 0;\n}\n}\n"); return eq_func; @@ -83,13 +97,17 @@ static CORD compile_hash_method(env_t *env, ast_t *ast) CORD full_name = CORD_cat(env->file_prefix, def->name); CORD hash_func = CORD_all("static uint32_t ", full_name, "$hash(const ", full_name, "_t *obj, const TypeInfo *info) {\n" "(void)info;\n" - "uint32_t hashes[2] = {(uint32_t)obj->$tag};\n" + "uint32_t hashes[2] = {(uint32_t)obj->$tag, 0};\n" "switch (obj->$tag) {\n"); for (tag_ast_t *tag = def->tags; tag; tag = tag->next) { - type_t *tag_type = Table_str_get(*env->types, CORD_to_const_char_star(CORD_all(def->name, "$", tag->name))); - hash_func = CORD_all(hash_func, "\tcase $tag$", full_name, "$", tag->name, ": " - "hashes[1] = generic_hash(&obj->", tag->name, ", ", compile_type_info(env, tag_type), ");\n" - "break;\n"); + if (tag->fields) { + type_t *tag_type = Table_str_get(*env->types, CORD_to_const_char_star(CORD_all(def->name, "$", tag->name))); + hash_func = CORD_all(hash_func, "\tcase $tag$", full_name, "$", tag->name, ": " + "hashes[1] = generic_hash(&obj->", tag->name, ", ", compile_type_info(env, tag_type), ");\n" + "break;\n"); + } else { + hash_func = CORD_all(hash_func, "\tcase $tag$", full_name, "$", tag->name, ": break;\n"); + } } hash_func = CORD_all(hash_func, "}\n" "uint32_t hash;\n" |
