mie: ctx: implement generic (non-parametised) type initialisation

This commit is contained in:
2026-01-04 18:42:12 +00:00
parent 3c9506256d
commit 9126edfd57
6 changed files with 85 additions and 2 deletions

View File

@@ -5,6 +5,7 @@
#include <mie/dialect/dialect.h>
#include <mie/dialect/type.h>
#include <mie/ir/op.h>
#include <mie/type/type.h>
#include <stdlib.h>
#include <string.h>
@@ -391,3 +392,50 @@ struct mie_dialect_type *mie_ctx_get_dialect_type(
return b_unbox(struct mie_dialect_type, target, ty_id);
}
struct mie_type *mie_ctx_get_type(
struct mie_ctx *ctx, const char *dialect_name, const char *type_name)
{
char full_name[256];
snprintf(full_name, sizeof full_name, "%s.%s", dialect_name, dialect_name);
b_rope full_name_rope = B_ROPE_CSTR(full_name);
mie_id id;
mie_id_init_ns(&id, mie_id_map_get_ns(&ctx->ctx_types), &full_name_rope);
mie_id *target = mie_id_map_get(&ctx->ctx_types, &id);
struct mie_type *type = b_unbox(struct mie_type, target, ty_id);
if (type) {
return type;
}
struct mie_dialect_type *type_info
= mie_ctx_get_dialect_type(ctx, dialect_name, type_name);
if (!type_info || (type_info->ty_flags & MIE_DIALECT_TYPE_PARAMETISED)) {
/* cannot initialise unknown or parametised types */
return NULL;
}
if (type_info->ty_data_size < sizeof(struct mie_type)) {
/* invalid type info */
return NULL;
}
type = malloc(type_info->ty_data_size);
if (!type) {
return NULL;
}
memset(type, 0x0, sizeof *type);
type->ty_def = type_info;
type->ty_name = b_bstr_fmt("%s.%s", dialect_name, type_name);
if (type_info->ty_init) {
type_info->ty_init(type_info, type);
}
mie_id_map_put(&ctx->ctx_types, &type->ty_id, &full_name_rope);
return type;
}

View File

@@ -31,6 +31,8 @@ MIE_API struct mie_dialect *mie_ctx_get_dialect(
const struct mie_ctx *ctx, const char *name);
MIE_API struct mie_dialect_type *mie_ctx_get_dialect_type(
const struct mie_ctx *ctx, const char *dialect_name, const char *type_name);
MIE_API struct mie_type *mie_ctx_get_type(
struct mie_ctx *ctx, const char *dialect_name, const char *type_name);
MIE_API struct mie_type *mie_ctx_get_int_type(
struct mie_ctx *ctx, unsigned int nr_bits);

View File

@@ -10,8 +10,13 @@ struct mie_type;
struct mie_parser;
struct mie_dialect;
enum mie_dialect_type_flags {
MIE_DIALECT_TYPE_PARAMETISED = 0x01u,
};
struct mie_dialect_type {
mie_id ty_id;
enum mie_dialect_type_flags ty_flags;
struct mie_dialect *ty_parent;
char *ty_name;

View File

@@ -62,6 +62,7 @@
__MIE_DIALECT_TYPE_BEGIN(c_sym, type)
#define MIE_DIALECT_TYPE_END() __MIE_DIALECT_TYPE_END()
#define MIE_DIALECT_TYPE_STRUCT(name) type->ty_data_size = sizeof(name)
#define MIE_DIALECT_TYPE_INIT(func) type->ty_init = (func)
#define MIE_DIALECT_TYPE_PRINT(func) type->ty_print = (func)
#define MIE_DIALECT_TYPE_PARSE(func) type->ty_parse = (func)
#define MIE_DIALECT_TYPE_INIT(func) type->ty_init = (func)

View File

@@ -8,7 +8,7 @@
struct mie_dialect;
struct mie_dialect_type;
struct mie_type;
struct mie_value;
/* a mie_type is an instance of mie_dialect_type.
* if the mie_dialect_type is a parametised type, the resulting mie_type
@@ -22,8 +22,16 @@ struct mie_type {
/* this pointer is optional. if it is NULL, the name of the type can
* be found in ty_def */
char *ty_name;
struct mie_dialect_type *ty_def;
/* for types that can be instantiated in C (i.e. an instance that can
* be represented by a mie_value), this is the total size of the
* instance data. */
size_t ty_instance_size;
void (*ty_instance_cleanup)(const struct mie_type *, struct mie_value *);
void (*ty_value_print)(
const struct mie_type *, const struct mie_value *, b_stream *);
};
MIE_API struct mie_type *mie_type_create(struct mie_dialect_type *type);

19
mie/type/type.c Normal file
View File

@@ -0,0 +1,19 @@
#include <mie/dialect/type.h>
#include <mie/type/type.h>
struct mie_type *mie_type_create(struct mie_dialect_type *type)
{
if (type->ty_data_size < sizeof(struct mie_type)) {
return NULL;
}
struct mie_type *out = malloc(type->ty_data_size);
if (!out) {
return NULL;
}
memset(out, 0x0, sizeof *out);
out->ty_def = type;
return out;
}