Files
fx/fx/type.c
T
2026-05-02 21:01:17 +01:00

358 lines
8.1 KiB
C

#include "type.h"
#include "class.h"
#include "object.h"
#include <fx/bst.h>
#include <fx/endian.h>
#include <fx/object.h>
#include <fx/type.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static struct fx_bst type_list = FX_BST_INIT;
static union fx_type zero_id = {0};
struct type_init_ctx {
size_t ctx_class_offset;
size_t ctx_instance_offset;
};
static inline int registration_compare(
const struct fx_type_registration *a,
const struct fx_type_registration *b)
{
return fx_type_id_compare(&a->r_info->t_id, &b->r_info->t_id);
}
static inline int component_compare(
const struct fx_type_component *a,
const struct fx_type_component *b)
{
return fx_type_id_compare(
&a->c_type->r_info->t_id,
&b->c_type->r_info->t_id);
}
FX_BST_DEFINE_INSERT(
struct fx_type_registration,
r_node,
r_info->r_id,
put_type,
registration_compare)
FX_BST_DEFINE_INSERT(
struct fx_type_component,
c_node,
&c_type->r_info->t_id,
put_type_component,
component_compare)
static struct fx_type_registration *get_type(
const fx_bst *tree,
const union fx_type *key)
{
fx_bst_node *cur = tree->bst_root;
while (cur) {
struct fx_type_registration *cur_node
= fx_unbox(struct fx_type_registration, cur, r_node);
int cmp = fx_type_id_compare(key, &cur_node->r_info->t_id);
if (cmp > 0) {
cur = fx_bst_right(cur);
} else if (cmp < 0) {
cur = fx_bst_left(cur);
} else {
return cur_node;
}
}
return NULL;
}
struct fx_type_component *fx_type_get_component(
const fx_bst *tree,
const union fx_type *key)
{
fx_bst_node *cur = tree->bst_root;
while (cur) {
struct fx_type_component *cur_node
= fx_unbox(struct fx_type_component, cur, c_node);
int cmp = fx_type_id_compare(
key,
&cur_node->c_type->r_info->t_id);
if (cmp > 0) {
cur = fx_bst_right(cur);
} else if (cmp < 0) {
cur = fx_bst_left(cur);
} else {
return cur_node;
}
}
return NULL;
}
static struct fx_type_component *create_type_component(
const struct fx_type_registration *type_reg)
{
struct fx_type_component *c = malloc(sizeof *c);
if (!c) {
return NULL;
}
memset(c, 0x0, sizeof *c);
c->c_type = type_reg;
return c;
}
void fx_type_id_init(
union fx_type *out,
uint32_t a,
uint16_t b,
uint16_t c,
uint16_t d,
uint64_t e)
{
fx_i32 x_a = fx_i32_htob(a);
fx_i16 x_b = fx_i16_htob(b);
fx_i16 x_c = fx_i16_htob(c);
fx_i16 x_d = fx_i16_htob(d);
fx_i64 x_e = fx_i64_htob(e);
memcpy(&out->b[0], x_a.i_bytes, sizeof x_a.i_bytes);
memcpy(&out->b[4], x_b.i_bytes, sizeof x_b.i_bytes);
memcpy(&out->b[6], x_c.i_bytes, sizeof x_c.i_bytes);
memcpy(&out->b[8], x_d.i_bytes, sizeof x_d.i_bytes);
memcpy(&out->b[10], &x_e.i_bytes[2], sizeof x_e.i_bytes - 2);
}
static void initialise_type_component(
struct fx_type_component *comp,
const struct fx_type_info *info,
struct type_init_ctx *init_ctx)
{
comp->c_class_data_offset = init_ctx->ctx_class_offset;
comp->c_class_data_size = info->t_class_size;
init_ctx->ctx_class_offset += comp->c_class_data_size;
comp->c_instance_private_data_offset = init_ctx->ctx_instance_offset;
comp->c_instance_private_data_size = info->t_instance_private_size;
init_ctx->ctx_instance_offset += comp->c_instance_private_data_size;
comp->c_instance_protected_data_offset = init_ctx->ctx_instance_offset;
comp->c_instance_protected_data_size = info->t_instance_protected_size;
init_ctx->ctx_instance_offset += comp->c_instance_protected_data_size;
}
static fx_result locate_interface(
fx_type interface_id,
struct fx_type_registration *dest,
struct type_init_ctx *init_ctx)
{
struct fx_type_component *interface_comp
= fx_type_get_component(&dest->r_components, interface_id);
if (interface_comp) {
return FX_RESULT_SUCCESS;
}
struct fx_type_registration *interface_reg
= get_type(&type_list, interface_id);
if (!interface_reg) {
return FX_RESULT_ERR(NO_ENTRY);
}
interface_comp = create_type_component(interface_reg);
if (!interface_comp) {
return FX_RESULT_ERR(NO_MEMORY);
}
initialise_type_component(
interface_comp,
interface_reg->r_info,
init_ctx);
put_type_component(&dest->r_components, interface_comp);
return FX_RESULT_SUCCESS;
}
static fx_result locate_interfaces(
const union fx_type *interfaces,
size_t nr_interfaces,
struct fx_type_registration *dest,
struct type_init_ctx *init_ctx)
{
fx_result result = FX_RESULT_SUCCESS;
for (size_t i = 0; i < nr_interfaces; i++) {
fx_type interface_id = &interfaces[i];
result = locate_interface(interface_id, dest, init_ctx);
if (fx_result_is_error(result)) {
break;
}
}
return result;
}
static fx_result find_type_components(struct fx_type_registration *reg)
{
const struct fx_type_info *current = reg->r_info;
struct fx_type_component *comp = create_type_component(reg);
if (!comp) {
return FX_RESULT_ERR(NO_MEMORY);
}
struct type_init_ctx init_ctx = {
.ctx_instance_offset = sizeof(struct _fx_object),
.ctx_class_offset = sizeof(struct _fx_class),
};
put_type_component(&reg->r_components, comp);
fx_queue_push_front(&reg->r_class_hierarchy, &comp->c_entry);
fx_result result = locate_interfaces(
current->t_interfaces,
current->t_nr_interfaces,
reg,
&init_ctx);
if (fx_result_is_error(result)) {
return result;
}
fx_type current_id = &current->t_parent_id;
if (!current_id || fx_type_id_compare(current_id, &zero_id) == 0) {
goto skip_class_hierarchy;
}
while (1) {
struct fx_type_registration *dep_class
= get_type(&type_list, current_id);
if (!dep_class) {
return FX_RESULT_ERR(NO_ENTRY);
}
comp = fx_type_get_component(&reg->r_components, current_id);
if (comp) {
/* circular class dependency */
// result = FX_RESULT_ERR(INVALID_ARGUMENT);
// break;
current_id = &dep_class->r_info->t_parent_id;
continue;
}
comp = create_type_component(dep_class);
result = locate_interfaces(
dep_class->r_info->t_interfaces,
dep_class->r_info->t_nr_interfaces,
reg,
&init_ctx);
if (fx_result_is_error(result)) {
break;
}
put_type_component(&reg->r_components, comp);
fx_queue_push_front(&reg->r_class_hierarchy, &comp->c_entry);
if (fx_type_id_compare(current_id, FX_TYPE_OBJECT) == 0) {
break;
}
current_id = &dep_class->r_info->t_parent_id;
}
fx_queue_entry *entry = fx_queue_first(&reg->r_class_hierarchy);
while (entry) {
comp = fx_unbox(struct fx_type_component, entry, c_entry);
initialise_type_component(
comp,
comp->c_type->r_info,
&init_ctx);
entry = fx_queue_next(entry);
}
fx_bst_node *node = fx_bst_first(&reg->r_components);
while (node) {
comp = fx_unbox(struct fx_type_component, node, c_node);
if (comp->c_type->r_category == FX_TYPE_CLASS) {
/* this component was already initialised above */
node = fx_bst_next(node);
continue;
}
initialise_type_component(
comp,
comp->c_type->r_info,
&init_ctx);
node = fx_bst_next(node);
}
skip_class_hierarchy:
reg->r_instance_size = init_ctx.ctx_instance_offset;
reg->r_class_size = init_ctx.ctx_class_offset;
return result;
}
static bool type_has_base_class(struct fx_type_info *info)
{
if (fx_type_id_compare(&info->t_id, FX_TYPE_OBJECT) == 0) {
return true;
}
return fx_type_id_compare(&info->t_parent_id, &zero_id) != 0;
}
fx_result fx_type_register(struct fx_type_info *info)
{
if (!type_has_base_class(info)) {
fx_type_id_copy(FX_TYPE_OBJECT, &info->t_parent_id);
}
struct fx_type_registration *r = get_type(&type_list, &info->t_id);
if (r) {
return FX_RESULT_ERR(NAME_EXISTS);
}
r = malloc(sizeof *r);
if (!r) {
return FX_RESULT_ERR(NO_MEMORY);
}
memset(r, 0x0, sizeof *r);
r->r_category = FX_TYPE_CLASS;
r->r_info = info;
fx_result result = find_type_components(r);
if (fx_result_is_error(result)) {
free(r);
return fx_result_propagate(result);
}
result = fx_class_instantiate(r, &r->r_class);
if (!r->r_class) {
free(r);
return fx_error_with_msg_template_caused_by_error(
FX_ERRORS_BUILTIN,
FX_ERR_TYPE_REGISTRATION_FAILURE,
result,
FX_MSG_TYPE_REGISTRATION_FAILURE,
FX_ERROR_PARAM("typename", info->t_name));
}
put_type(&type_list, r);
return FX_RESULT_SUCCESS;
}
struct fx_type_registration *fx_type_get_registration(fx_type id)
{
return get_type(&type_list, id);
}