Files
mie/mie/dialect/dialect.c
Max Wash 11fc7a6ca9 mie: implement an interface system for ops
this system allows dialects to register named interfaces, which are collections of function
pointers. an op can then implement this interface, providing callbacks for the interface's
virtual function pointers.

C code can request a pointer to an op's implementation of a given interface and call
virtual functions with no knowledge required about the op itself. this functonality
will also be extended to types, attributes, and dialects themselves.
2026-01-14 18:17:38 +00:00

88 lines
2.7 KiB
C

#include <blue/ds/string.h>
#include <mie/ctx.h>
#include <mie/dialect/dialect.h>
#include <mie/ir/op-definition.h>
#include <mie/type/type-definition.h>
#define TYPE_NS_ID \
MIE_ID(0xe4, 0x99, 0x42, 0x58, 0x2b, 0xdb, 0x45, 0xa3, 0xbd, 0x4b, \
0x17, 0xe3, 0xc4, 0xa9, 0xbc, 0x30)
#define OP_NS_ID \
MIE_ID(0xb9, 0x97, 0xcf, 0xd3, 0x81, 0xd4, 0x45, 0x06, 0x9b, 0x44, \
0x05, 0x9f, 0xb4, 0x76, 0xf1, 0x2d)
#define TRAIT_NS_ID \
MIE_ID(0x5a, 0x11, 0x68, 0x8e, 0x21, 0x1d, 0x4d, 0x5f, 0xb8, 0x6b, \
0x39, 0x73, 0xb0, 0x1f, 0xce, 0xf7)
#define ATTRIBUTE_NS_ID \
MIE_ID(0x86, 0x76, 0xcb, 0xfb, 0xc8, 0xe5, 0x40, 0x7d, 0xa3, 0x84, \
0x93, 0xe3, 0xa5, 0x29, 0x74, 0xfe)
#define INTERFACE_NS_ID \
MIE_ID(0xc5, 0x85, 0x7f, 0x8a, 0x7d, 0xe4, 0x4d, 0x03, 0x88, 0x8d, \
0x34, 0x32, 0xbf, 0x29, 0x5c, 0x72)
struct mie_dialect *mie_dialect_create(
struct mie_ctx *ctx, const char *name, size_t size)
{
if (size < sizeof(struct mie_dialect)) {
return NULL;
}
struct mie_dialect *out = malloc(size);
if (!out) {
return NULL;
}
memset(out, 0x0, size);
out->d_name = b_strdup(name);
if (!out->d_name) {
free(out);
return NULL;
}
mie_id op_ns = OP_NS_ID;
mie_id_map_init(&out->d_ops, &op_ns);
mie_id type_ns = TYPE_NS_ID;
mie_id_map_init(&out->d_types, &type_ns);
mie_id trait_ns = TRAIT_NS_ID;
mie_id_map_init(&out->d_traits, &trait_ns);
mie_id attribute_ns = ATTRIBUTE_NS_ID;
mie_id_map_init(&out->d_attributes, &attribute_ns);
mie_id interface_ns = INTERFACE_NS_ID;
mie_id_map_init(&out->d_interfaces, &interface_ns);
b_rope name_rope = B_ROPE_CSTR(name);
mie_id_map_put(&ctx->ctx_dialects, &out->d_id, &name_rope);
return out;
}
const struct mie_op_definition *mie_dialect_get_op(
const struct mie_dialect *dialect, const char *name)
{
b_rope name_rope = B_ROPE_CSTR(name);
mie_id id;
mie_id_init_ns(&id, mie_id_map_get_ns(&dialect->d_ops), &name_rope);
mie_id *target = mie_id_map_get(&dialect->d_ops, &id);
return b_unbox(struct mie_op_definition, target, op_id);
}
const struct mie_type_definition *mie_dialect_get_type(
const struct mie_dialect *dialect, const char *name)
{
b_rope name_rope = B_ROPE_CSTR(name);
mie_id id;
mie_id_init_ns(&id, mie_id_map_get_ns(&dialect->d_types), &name_rope);
mie_id *target = mie_id_map_get(&dialect->d_types, &id);
return b_unbox(struct mie_type_definition, target, ty_id);
}