2024-03-18 09:47:07 -07:00
|
|
|
// Logic for compiling tagged unions (enums)
|
2024-02-24 12:24:44 -08:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <gc/cord.h>
|
|
|
|
#include <gc.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "ast.h"
|
2024-09-13 17:18:08 -07:00
|
|
|
#include "stdlib/text.h"
|
2024-02-24 12:24:44 -08:00
|
|
|
#include "compile.h"
|
2024-09-13 11:23:24 -07:00
|
|
|
#include "cordhelpers.h"
|
2024-02-24 12:24:44 -08:00
|
|
|
#include "structs.h"
|
|
|
|
#include "environment.h"
|
|
|
|
#include "typecheck.h"
|
2024-09-13 17:18:08 -07:00
|
|
|
#include "stdlib/util.h"
|
2024-02-24 12:24:44 -08:00
|
|
|
|
2024-09-08 14:17:15 -07:00
|
|
|
PUREFUNC static bool has_extra_data(tag_ast_t *tags)
|
2024-05-21 10:42:33 -07:00
|
|
|
{
|
|
|
|
for (tag_ast_t *tag = tags; tag; tag = tag->next) {
|
|
|
|
if (tag->fields) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-02-24 12:24:44 -08:00
|
|
|
static CORD compile_str_method(env_t *env, ast_t *ast)
|
|
|
|
{
|
|
|
|
auto def = Match(ast, EnumDef);
|
2024-09-24 11:54:22 -07:00
|
|
|
CORD full_name = CORD_cat(namespace_prefix(env, env->namespace), def->name);
|
2024-09-02 16:18:21 -07:00
|
|
|
CORD str_func = CORD_all("static Text_t ", full_name, "$as_text(", full_name, "_t *obj, bool use_color) {\n"
|
2024-09-03 12:00:28 -07:00
|
|
|
"\tif (!obj) return Text(\"", def->name, "\");\n"
|
2024-08-18 17:58:36 -07:00
|
|
|
"switch (obj->tag) {\n");
|
2024-02-24 12:24:44 -08:00
|
|
|
for (tag_ast_t *tag = def->tags; tag; tag = tag->next) {
|
2024-03-17 19:17:21 -07:00
|
|
|
if (!tag->fields) {
|
2024-09-03 12:00:28 -07:00
|
|
|
str_func = CORD_all(str_func, "\tcase ", full_name, "$tag$", tag->name, ": return use_color ? Text(\"\\x1b[36;1m",
|
|
|
|
def->name, ".", tag->name, "\\x1b[m\") : Text(\"", def->name, ".", tag->name, "\");\n");
|
2024-03-17 19:17:21 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-09-03 12:00:28 -07:00
|
|
|
str_func = CORD_all(str_func, "\tcase ", full_name, "$tag$", tag->name, ": return Text$concat(use_color ? Text(\"\\x1b[36;1m",
|
|
|
|
def->name, ".", tag->name, "\\x1b[m(\") : Text(\"", def->name, ".", tag->name, "(\")");
|
2024-02-24 12:24:44 -08:00
|
|
|
|
|
|
|
if (tag->secret) {
|
2024-09-03 12:00:28 -07:00
|
|
|
str_func = CORD_cat(str_func, ", use_color ? Text(\"\\x1b[2m...\\x1b[m\") : Text(\"...\", \")\"));\n");
|
2024-02-24 12:24:44 -08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-09-12 01:09:52 -07:00
|
|
|
if (tag->fields && !tag->fields->next) { // Single-member tags don't need to print member names:
|
|
|
|
type_t *field_t = get_arg_ast_type(env, tag->fields);
|
|
|
|
CORD field_str = expr_as_text(env, CORD_all("obj->$", tag->name, ".$", tag->fields->name), field_t, "use_color");
|
|
|
|
str_func = CORD_all(str_func, ", ", field_str);
|
|
|
|
} else {
|
|
|
|
for (arg_ast_t *field = tag->fields; field; field = field->next) {
|
|
|
|
type_t *field_t = get_arg_ast_type(env, field);
|
|
|
|
CORD field_str = expr_as_text(env, CORD_all("obj->$", tag->name, ".$", field->name), field_t, "use_color");
|
|
|
|
str_func = CORD_all(str_func, ", Text(\"", field->name, "=\"), ", field_str);
|
|
|
|
if (field->next) str_func = CORD_cat(str_func, ", Text(\", \")");
|
|
|
|
}
|
2024-02-24 12:24:44 -08:00
|
|
|
}
|
2024-09-03 12:00:28 -07:00
|
|
|
str_func = CORD_cat(str_func, ", Text(\")\"));\n");
|
2024-02-24 12:24:44 -08:00
|
|
|
}
|
2024-09-02 16:18:21 -07:00
|
|
|
str_func = CORD_cat(str_func, "\tdefault: return (Text_t){.length=0};\n\t}\n}\n");
|
2024-02-24 12:24:44 -08:00
|
|
|
return str_func;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CORD compile_compare_method(env_t *env, ast_t *ast)
|
|
|
|
{
|
|
|
|
auto def = Match(ast, EnumDef);
|
2024-09-24 11:54:22 -07:00
|
|
|
CORD full_name = CORD_cat(namespace_prefix(env, env->namespace), def->name);
|
2024-05-21 10:42:33 -07:00
|
|
|
if (!has_extra_data(def->tags)) {
|
|
|
|
// Comparisons are simpler if there is only a tag, no tagged data:
|
|
|
|
return CORD_all("static int ", full_name, "$compare(const ", full_name, "_t *x, const ", full_name,
|
2024-09-30 11:39:30 -07:00
|
|
|
"_t *y, const TypeInfo_t *info) {\n"
|
2024-05-21 10:42:33 -07:00
|
|
|
"(void)info;\n"
|
2024-08-18 17:58:36 -07:00
|
|
|
"return (x->tag - y->tag);\n"
|
2024-05-21 10:42:33 -07:00
|
|
|
"}\n");
|
|
|
|
}
|
2024-03-17 17:40:40 -07:00
|
|
|
CORD cmp_func = CORD_all("static int ", full_name, "$compare(const ", full_name, "_t *x, const ", full_name,
|
2024-09-30 11:39:30 -07:00
|
|
|
"_t *y, const TypeInfo_t *info) {\n"
|
2024-03-03 12:04:36 -08:00
|
|
|
"(void)info;\n"
|
2024-08-18 17:58:36 -07:00
|
|
|
"int diff = x->tag - y->tag;\n"
|
2024-02-24 12:24:44 -08:00
|
|
|
"if (diff) return diff;\n"
|
2024-08-18 17:58:36 -07:00
|
|
|
"switch (x->tag) {\n");
|
2024-02-24 12:24:44 -08:00
|
|
|
for (tag_ast_t *tag = def->tags; tag; tag = tag->next) {
|
2024-03-17 19:17:21 -07:00
|
|
|
if (tag->fields) {
|
2024-03-29 09:54:31 -07:00
|
|
|
type_t *tag_type = Table$str_get(*env->types, CORD_to_const_char_star(CORD_all(def->name, "$", tag->name)));
|
2024-05-21 17:14:57 -07:00
|
|
|
cmp_func = CORD_all(cmp_func, "\tcase ", full_name, "$tag$", tag->name, ": "
|
2024-08-03 13:39:04 -07:00
|
|
|
"return generic_compare(&x->$", tag->name, ", &y->$", tag->name, ", ", compile_type_info(env, tag_type), ");\n");
|
2024-03-17 19:17:21 -07:00
|
|
|
} else {
|
2024-05-21 17:14:57 -07:00
|
|
|
cmp_func = CORD_all(cmp_func, "\tcase ", full_name, "$tag$", tag->name, ": return 0;\n");
|
2024-03-17 19:17:21 -07:00
|
|
|
}
|
2024-02-24 12:24:44 -08:00
|
|
|
}
|
2024-03-03 12:04:36 -08:00
|
|
|
cmp_func = CORD_all(cmp_func, "default: return 0;\n}\n}\n");
|
2024-02-24 12:24:44 -08:00
|
|
|
return cmp_func;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CORD compile_equals_method(env_t *env, ast_t *ast)
|
|
|
|
{
|
|
|
|
auto def = Match(ast, EnumDef);
|
2024-09-24 11:54:22 -07:00
|
|
|
CORD full_name = CORD_cat(namespace_prefix(env, env->namespace), def->name);
|
2024-05-21 10:42:33 -07:00
|
|
|
if (!has_extra_data(def->tags)) {
|
|
|
|
// Equality is simpler if there is only a tag, no tagged data:
|
|
|
|
return CORD_all("static bool ", full_name, "$equal(const ", full_name, "_t *x, const ", full_name,
|
2024-09-30 11:39:30 -07:00
|
|
|
"_t *y, const TypeInfo_t *info) {\n"
|
2024-05-21 10:42:33 -07:00
|
|
|
"(void)info;\n"
|
2024-08-18 17:58:36 -07:00
|
|
|
"return (x->tag == y->tag);\n"
|
2024-05-21 10:42:33 -07:00
|
|
|
"}\n");
|
|
|
|
}
|
2024-03-17 17:40:40 -07:00
|
|
|
CORD eq_func = CORD_all("static bool ", full_name, "$equal(const ", full_name, "_t *x, const ", full_name,
|
2024-09-30 11:39:30 -07:00
|
|
|
"_t *y, const TypeInfo_t *info) {\n"
|
2024-03-03 12:04:36 -08:00
|
|
|
"(void)info;\n"
|
2024-08-18 17:58:36 -07:00
|
|
|
"if (x->tag != y->tag) return no;\n"
|
|
|
|
"switch (x->tag) {\n");
|
2024-02-24 12:24:44 -08:00
|
|
|
for (tag_ast_t *tag = def->tags; tag; tag = tag->next) {
|
2024-03-17 19:17:21 -07:00
|
|
|
if (tag->fields) {
|
2024-03-29 09:54:31 -07:00
|
|
|
type_t *tag_type = Table$str_get(*env->types, CORD_to_const_char_star(CORD_all(def->name, "$", tag->name)));
|
2024-05-21 17:14:57 -07:00
|
|
|
eq_func = CORD_all(eq_func, "\tcase ", full_name, "$tag$", tag->name, ": "
|
2024-08-03 13:39:04 -07:00
|
|
|
"return generic_equal(&x->$", tag->name, ", &y->$", tag->name, ", ", compile_type_info(env, tag_type), ");\n");
|
2024-03-17 19:17:21 -07:00
|
|
|
} else {
|
2024-05-21 17:14:57 -07:00
|
|
|
eq_func = CORD_all(eq_func, "\tcase ", full_name, "$tag$", tag->name, ": return yes;\n");
|
2024-03-17 19:17:21 -07:00
|
|
|
}
|
2024-02-24 12:24:44 -08:00
|
|
|
}
|
2024-03-03 12:04:36 -08:00
|
|
|
eq_func = CORD_all(eq_func, "default: return 0;\n}\n}\n");
|
2024-02-24 12:24:44 -08:00
|
|
|
return eq_func;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CORD compile_hash_method(env_t *env, ast_t *ast)
|
|
|
|
{
|
|
|
|
auto def = Match(ast, EnumDef);
|
2024-09-24 11:54:22 -07:00
|
|
|
CORD full_name = CORD_cat(namespace_prefix(env, env->namespace), def->name);
|
2024-05-21 10:42:33 -07:00
|
|
|
if (!has_extra_data(def->tags)) {
|
|
|
|
// Hashing is simpler if there is only a tag, no tagged data:
|
2024-09-30 11:39:30 -07:00
|
|
|
return CORD_all("static uint64_t ", full_name, "$hash(const ", full_name, "_t *obj, const TypeInfo_t *info) {\n"
|
2024-05-21 10:42:33 -07:00
|
|
|
"(void)info;\n"
|
2024-09-05 13:23:05 -07:00
|
|
|
"return siphash24((void*)&obj->tag, sizeof(obj->tag));\n"
|
2024-05-21 10:42:33 -07:00
|
|
|
"\n}\n");
|
|
|
|
}
|
2024-09-30 11:39:30 -07:00
|
|
|
CORD hash_func = CORD_all("static uint64_t ", full_name, "$hash(const ", full_name, "_t *obj, const TypeInfo_t *info) {\n"
|
2024-03-03 12:04:36 -08:00
|
|
|
"(void)info;\n"
|
2024-09-05 13:23:05 -07:00
|
|
|
"uint64_t hashes[2] = {(uint64_t)obj->tag, 0};\n"
|
2024-08-18 17:58:36 -07:00
|
|
|
"switch (obj->tag) {\n");
|
2024-02-24 12:24:44 -08:00
|
|
|
for (tag_ast_t *tag = def->tags; tag; tag = tag->next) {
|
2024-03-17 19:17:21 -07:00
|
|
|
if (tag->fields) {
|
2024-03-29 09:54:31 -07:00
|
|
|
type_t *tag_type = Table$str_get(*env->types, CORD_to_const_char_star(CORD_all(def->name, "$", tag->name)));
|
2024-05-21 17:14:57 -07:00
|
|
|
hash_func = CORD_all(hash_func, "\tcase ", full_name, "$tag$", tag->name, ": "
|
2024-08-03 13:39:04 -07:00
|
|
|
"hashes[1] = generic_hash(&obj->$", tag->name, ", ", compile_type_info(env, tag_type), ");\n"
|
2024-03-17 19:17:21 -07:00
|
|
|
"break;\n");
|
|
|
|
} else {
|
2024-05-21 17:14:57 -07:00
|
|
|
hash_func = CORD_all(hash_func, "\tcase ", full_name, "$tag$", tag->name, ": break;\n");
|
2024-03-17 19:17:21 -07:00
|
|
|
}
|
2024-02-24 12:24:44 -08:00
|
|
|
}
|
|
|
|
hash_func = CORD_all(hash_func, "}\n"
|
2024-09-05 13:23:05 -07:00
|
|
|
"return siphash24((void*)&hashes, sizeof(hashes));\n}\n");
|
2024-02-24 12:24:44 -08:00
|
|
|
return hash_func;
|
|
|
|
}
|
|
|
|
|
|
|
|
void compile_enum_def(env_t *env, ast_t *ast)
|
|
|
|
{
|
|
|
|
auto def = Match(ast, EnumDef);
|
2024-09-24 11:54:22 -07:00
|
|
|
CORD full_name = CORD_cat(namespace_prefix(env, env->namespace), def->name);
|
2024-02-24 12:24:44 -08:00
|
|
|
for (tag_ast_t *tag = def->tags; tag; tag = tag->next) {
|
2024-03-17 17:40:40 -07:00
|
|
|
compile_struct_def(env, WrapAST(ast, StructDef, .name=CORD_to_const_char_star(CORD_all(def->name, "$", tag->name)), .fields=tag->fields));
|
2024-05-21 17:14:57 -07:00
|
|
|
if (tag->fields) { // Constructor macros:
|
2024-03-17 19:06:55 -07:00
|
|
|
CORD arg_sig = CORD_EMPTY;
|
|
|
|
for (arg_ast_t *field = tag->fields; field; field = field->next) {
|
|
|
|
type_t *field_t = get_arg_ast_type(env, field);
|
2024-08-03 13:39:04 -07:00
|
|
|
arg_sig = CORD_all(arg_sig, compile_declaration(field_t, CORD_all("$", field->name)));
|
2024-03-17 19:06:55 -07:00
|
|
|
if (field->next) arg_sig = CORD_cat(arg_sig, ", ");
|
|
|
|
}
|
|
|
|
if (arg_sig == CORD_EMPTY) arg_sig = "void";
|
|
|
|
CORD constructor_impl = CORD_all("public inline ", full_name, "_t ", full_name, "$tagged$", tag->name, "(", arg_sig, ") { return (",
|
2024-08-18 17:58:36 -07:00
|
|
|
full_name, "_t){.tag=", full_name, "$tag$", tag->name, ", .$", tag->name, "={");
|
2024-03-17 19:06:55 -07:00
|
|
|
for (arg_ast_t *field = tag->fields; field; field = field->next) {
|
2024-08-03 13:39:04 -07:00
|
|
|
constructor_impl = CORD_all(constructor_impl, "$", field->name);
|
2024-03-17 19:06:55 -07:00
|
|
|
if (field->next) constructor_impl = CORD_cat(constructor_impl, ", ");
|
|
|
|
}
|
|
|
|
constructor_impl = CORD_cat(constructor_impl, "}}; }\n");
|
|
|
|
env->code->funcs = CORD_cat(env->code->funcs, constructor_impl);
|
2024-03-03 11:12:40 -08:00
|
|
|
}
|
2024-02-24 12:24:44 -08:00
|
|
|
}
|
|
|
|
|
2024-03-29 09:54:31 -07:00
|
|
|
type_t *t = Table$str_get(*env->types, def->name);
|
2024-09-30 11:39:30 -07:00
|
|
|
CORD typeinfo = CORD_asprintf("public const TypeInfo_t %s = {%zu, %zu, {.tag=EnumInfo, .CustomInfo={",
|
2024-03-17 17:40:40 -07:00
|
|
|
full_name, type_size(t), type_align(t));
|
2024-02-24 12:24:44 -08:00
|
|
|
|
2024-05-21 10:42:33 -07:00
|
|
|
env->code->funcs = CORD_all(env->code->funcs, compile_str_method(env, ast));
|
|
|
|
if (has_extra_data(def->tags)) {
|
|
|
|
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_text=(void*)", full_name, "$as_text, "
|
|
|
|
".equal=(void*)", full_name, "$equal, "
|
|
|
|
".hash=(void*)", full_name, "$hash, "
|
|
|
|
".compare=(void*)", full_name, "$compare");
|
|
|
|
} else {
|
|
|
|
// No need for custom methods if there is no tagged data
|
|
|
|
typeinfo = CORD_all(typeinfo, ".as_text=(void*)", full_name, "$as_text");
|
|
|
|
}
|
2024-03-03 10:04:50 -08:00
|
|
|
typeinfo = CORD_cat(typeinfo, "}}};\n");
|
2024-02-24 12:24:44 -08:00
|
|
|
env->code->typeinfos = CORD_all(env->code->typeinfos, typeinfo);
|
2024-03-04 10:51:47 -08:00
|
|
|
|
2024-03-06 09:41:18 -08:00
|
|
|
compile_namespace(env, def->name, def->namespace);
|
2024-02-24 12:24:44 -08:00
|
|
|
}
|
|
|
|
|
2024-09-17 21:58:41 -07:00
|
|
|
CORD compile_enum_header(env_t *env, ast_t *ast)
|
2024-06-06 13:28:53 -07:00
|
|
|
{
|
|
|
|
auto def = Match(ast, EnumDef);
|
2024-09-24 11:54:22 -07:00
|
|
|
CORD full_name = CORD_all(namespace_prefix(env, env->namespace), def->name);
|
2024-07-23 16:46:42 -07:00
|
|
|
CORD all_defs = CORD_all("typedef struct ", full_name, "_s ", full_name, "_t;\n");
|
2024-06-06 13:28:53 -07:00
|
|
|
CORD enum_def = CORD_all("struct ", full_name, "_s {\n"
|
2024-09-11 09:29:48 -07:00
|
|
|
"\tenum { ", full_name, "$null=0, ");
|
|
|
|
|
2024-06-06 13:28:53 -07:00
|
|
|
for (tag_ast_t *tag = def->tags; tag; tag = tag->next) {
|
2024-09-11 09:29:48 -07:00
|
|
|
enum_def = CORD_all(enum_def, full_name, "$tag$", tag->name);
|
2024-09-17 21:58:41 -07:00
|
|
|
if (tag->next) enum_def = CORD_all(enum_def, ", ");
|
2024-06-06 13:28:53 -07:00
|
|
|
}
|
2024-09-17 21:58:41 -07:00
|
|
|
enum_def = CORD_all(enum_def, "} tag;\n"
|
2024-06-06 13:28:53 -07:00
|
|
|
"union {\n");
|
|
|
|
for (tag_ast_t *tag = def->tags; tag; tag = tag->next) {
|
2024-09-17 22:01:15 -07:00
|
|
|
CORD field_def = compile_struct_header(env, WrapAST(ast, StructDef, .name=CORD_to_const_char_star(CORD_all(def->name, "$", tag->name)), .fields=tag->fields));
|
2024-07-23 16:46:42 -07:00
|
|
|
all_defs = CORD_all(all_defs, field_def);
|
2024-08-03 13:39:04 -07:00
|
|
|
enum_def = CORD_all(enum_def, full_name, "$", tag->name, "_t $", tag->name, ";\n");
|
2024-07-23 16:46:42 -07:00
|
|
|
}
|
2024-09-17 21:58:41 -07:00
|
|
|
enum_def = CORD_all(enum_def, "};\n};\n");
|
2024-07-23 16:46:42 -07:00
|
|
|
all_defs = CORD_all(all_defs, enum_def);
|
|
|
|
|
2024-09-30 11:39:30 -07:00
|
|
|
all_defs = CORD_all(all_defs, "extern const TypeInfo_t ", full_name, ";\n");
|
2024-07-23 16:46:42 -07:00
|
|
|
for (tag_ast_t *tag = def->tags; tag; tag = tag->next) {
|
|
|
|
all_defs = CORD_all(all_defs,
|
2024-09-30 11:39:30 -07:00
|
|
|
"extern const TypeInfo_t ", namespace_prefix(env, env->namespace), def->name, "$", tag->name, ";\n");
|
2024-06-06 13:28:53 -07:00
|
|
|
if (tag->fields) { // Constructor macros:
|
|
|
|
CORD arg_sig = CORD_EMPTY;
|
|
|
|
for (arg_ast_t *field = tag->fields; field; field = field->next) {
|
|
|
|
type_t *field_t = get_arg_ast_type(env, field);
|
2024-08-03 13:39:04 -07:00
|
|
|
arg_sig = CORD_all(arg_sig, compile_declaration(field_t, CORD_all("$", field->name)));
|
2024-09-17 21:58:41 -07:00
|
|
|
if (field->next) arg_sig = CORD_all(arg_sig, ", ");
|
2024-06-06 13:28:53 -07:00
|
|
|
}
|
|
|
|
if (arg_sig == CORD_EMPTY) arg_sig = "void";
|
|
|
|
CORD constructor_def = CORD_all(full_name, "_t ", full_name, "$tagged$", tag->name, "(", arg_sig, ");\n");
|
2024-09-17 21:58:41 -07:00
|
|
|
all_defs = CORD_all(all_defs, constructor_def);
|
2024-06-06 13:28:53 -07:00
|
|
|
}
|
|
|
|
}
|
2024-09-17 13:20:30 -07:00
|
|
|
return CORD_all(all_defs, compile_namespace_header(env, def->name, def->namespace));
|
2024-06-06 13:28:53 -07:00
|
|
|
}
|
|
|
|
|
2024-02-24 12:24:44 -08:00
|
|
|
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0
|