HelenOS sources
This source file includes following definitions.
- tdata_is_csi_derived_from_ti
- tdata_is_ti_derived_from_ti
- tdata_item_equal
- tdata_item_subst
- tdata_item_subst_tprimitive
- tdata_item_subst_tobject
- tdata_item_subst_tarray
- tdata_item_subst_tdeleg
- tdata_item_subst_tebase
- tdata_item_subst_tenum
- tdata_item_subst_tfun
- tdata_item_subst_tvref
- tdata_item_subst_fun_sig
- tdata_item_print
- tdata_tprimitive_print
- tdata_tobject_print
- tdata_tarray_print
- tdata_tdeleg_print
- tdata_tebase_print
- tdata_tenum_print
- tdata_tfun_print
- tdata_tvref_print
- tdata_item_new
- tdata_array_new
- tdata_object_new
- tdata_primitive_new
- tdata_deleg_new
- tdata_ebase_new
- tdata_enum_new
- tdata_fun_new
- tdata_vref_new
- tdata_fun_sig_new
- tdata_tvv_new
- tdata_tvv_get_val
- tdata_tvv_set_val
#include <stdlib.h>
#include <assert.h>
#include "intmap.h"
#include "list.h"
#include "mytypes.h"
#include "stree.h"
#include "strtab.h"
#include "symbol.h"
#include "tdata.h"
static void tdata_item_subst_tprimitive(tdata_primitive_t *torig,
tdata_tvv_t *tvv, tdata_item_t **res);
static void tdata_item_subst_tobject(tdata_object_t *torig, tdata_tvv_t *tvv,
tdata_item_t **res);
static void tdata_item_subst_tarray(tdata_array_t *torig, tdata_tvv_t *tvv,
tdata_item_t **res);
static void tdata_item_subst_tdeleg(tdata_deleg_t *torig,
tdata_tvv_t *tvv, tdata_item_t **res);
static void tdata_item_subst_tebase(tdata_ebase_t *tebase,
tdata_tvv_t *tvv, tdata_item_t **res);
static void tdata_item_subst_tenum(tdata_enum_t *tenum,
tdata_tvv_t *tvv, tdata_item_t **res);
static void tdata_item_subst_tfun(tdata_fun_t *torig,
tdata_tvv_t *tvv, tdata_item_t **res);
static void tdata_item_subst_tvref(tdata_vref_t *tvref, tdata_tvv_t *tvv,
tdata_item_t **res);
static void tdata_item_subst_fun_sig(tdata_fun_sig_t *torig, tdata_tvv_t *tvv,
tdata_fun_sig_t **res);
static void tdata_tprimitive_print(tdata_primitive_t *tprimitive);
static void tdata_tobject_print(tdata_object_t *tobject);
static void tdata_tarray_print(tdata_array_t *tarray);
static void tdata_tdeleg_print(tdata_deleg_t *tdeleg);
static void tdata_tebase_print(tdata_ebase_t *tebase);
static void tdata_tenum_print(tdata_enum_t *tenum);
static void tdata_tfun_print(tdata_fun_t *tfun);
static void tdata_tvref_print(tdata_vref_t *tvref);
bool_t tdata_is_csi_derived_from_ti(stree_csi_t *a, tdata_item_t *tb)
{
bool_t res;
switch (tb->tic) {
case tic_tobject:
res = stree_is_csi_derived_from_csi(a, tb->u.tobject->csi);
break;
default:
printf("Error: Base type is not a CSI.\n");
exit(1);
}
return res;
}
bool_t tdata_is_ti_derived_from_ti(tdata_item_t *ta, tdata_item_t *tb)
{
bool_t res;
switch (ta->tic) {
case tic_tobject:
res = tdata_is_csi_derived_from_ti(ta->u.tobject->csi, tb);
break;
default:
printf("Error: Derived type is not a CSI.\n");
exit(1);
}
return res;
}
bool_t tdata_item_equal(tdata_item_t *a, tdata_item_t *b)
{
if (a->tic == tic_tprimitive && a->u.tprimitive->tpc == tpc_nil) {
if (b->tic == tic_tobject)
return b_true;
} else if (b->tic == tic_tprimitive && b->u.tprimitive->tpc == tpc_nil) {
if (a->tic == tic_tobject)
return b_true;
}
if (a->tic != b->tic)
return b_false;
switch (a->tic) {
case tic_tprimitive:
return (a->u.tprimitive->tpc == b->u.tprimitive->tpc);
case tic_tobject:
return (a->u.tobject->csi == b->u.tobject->csi);
case tic_tarray:
if (a->u.tarray->rank != b->u.tarray->rank)
return b_false;
return tdata_item_equal(a->u.tarray->base_ti,
b->u.tarray->base_ti);
case tic_tenum:
return (a->u.tenum->enum_d == b->u.tenum->enum_d);
case tic_tvref:
return (a->u.tvref->targ == b->u.tvref->targ);
default:
printf("Warning: Unimplemented: Compare types '");
tdata_item_print(a);
printf("' and '");
tdata_item_print(b);
printf("'.\n");
return b_true;
}
}
void tdata_item_subst(tdata_item_t *ti, tdata_tvv_t *tvv, tdata_item_t **res)
{
switch (ti->tic) {
case tic_tprimitive:
tdata_item_subst_tprimitive(ti->u.tprimitive, tvv, res);
break;
case tic_tobject:
tdata_item_subst_tobject(ti->u.tobject, tvv, res);
break;
case tic_tarray:
tdata_item_subst_tarray(ti->u.tarray, tvv, res);
break;
case tic_tdeleg:
tdata_item_subst_tdeleg(ti->u.tdeleg, tvv, res);
break;
case tic_tebase:
tdata_item_subst_tebase(ti->u.tebase, tvv, res);
break;
case tic_tenum:
tdata_item_subst_tenum(ti->u.tenum, tvv, res);
break;
case tic_tfun:
tdata_item_subst_tfun(ti->u.tfun, tvv, res);
break;
case tic_tvref:
tdata_item_subst_tvref(ti->u.tvref, tvv, res);
break;
case tic_ignore:
*res = tdata_item_new(tic_ignore);
}
}
static void tdata_item_subst_tprimitive(tdata_primitive_t *torig,
tdata_tvv_t *tvv, tdata_item_t **res)
{
tdata_primitive_t *tnew;
(void) tvv;
tnew = tdata_primitive_new(torig->tpc);
*res = tdata_item_new(tic_tprimitive);
(*res)->u.tprimitive = tnew;
}
static void tdata_item_subst_tobject(tdata_object_t *torig, tdata_tvv_t *tvv,
tdata_item_t **res)
{
tdata_object_t *tnew;
list_node_t *targ_n;
tdata_item_t *targ;
tdata_item_t *new_targ;
tnew = tdata_object_new();
tnew->static_ref = torig->static_ref;
tnew->csi = torig->csi;
list_init(&tnew->targs);
targ_n = list_first(&torig->targs);
while (targ_n != NULL) {
targ = list_node_data(targ_n, tdata_item_t *);
tdata_item_subst(targ, tvv, &new_targ);
list_append(&tnew->targs, new_targ);
targ_n = list_next(&torig->targs, targ_n);
}
*res = tdata_item_new(tic_tobject);
(*res)->u.tobject = tnew;
}
static void tdata_item_subst_tarray(tdata_array_t *torig, tdata_tvv_t *tvv,
tdata_item_t **res)
{
tdata_array_t *tnew;
list_node_t *ext_n;
stree_expr_t *extent;
tnew = tdata_array_new();
tdata_item_subst(torig->base_ti, tvv, &tnew->base_ti);
tnew->rank = torig->rank;
list_init(&tnew->extents);
ext_n = list_first(&torig->extents);
while (ext_n != NULL) {
extent = list_node_data(ext_n, stree_expr_t *);
list_append(&tnew->extents, extent);
ext_n = list_next(&tnew->extents, ext_n);
}
*res = tdata_item_new(tic_tarray);
(*res)->u.tarray = tnew;
}
static void tdata_item_subst_tdeleg(tdata_deleg_t *torig, tdata_tvv_t *tvv,
tdata_item_t **res)
{
tdata_deleg_t *tnew;
tnew = tdata_deleg_new();
tnew->deleg = torig->deleg;
tdata_item_subst_fun_sig(torig->tsig, tvv, &tnew->tsig);
*res = tdata_item_new(tic_tdeleg);
(*res)->u.tdeleg = tnew;
}
static void tdata_item_subst_tebase(tdata_ebase_t *tebase,
tdata_tvv_t *tvv, tdata_item_t **res)
{
tdata_ebase_t *tnew;
(void) tvv;
tnew = tdata_ebase_new();
tnew->enum_d = tebase->enum_d;
*res = tdata_item_new(tic_tebase);
(*res)->u.tebase = tnew;
}
static void tdata_item_subst_tenum(tdata_enum_t *tenum,
tdata_tvv_t *tvv, tdata_item_t **res)
{
tdata_enum_t *tnew;
(void) tvv;
tnew = tdata_enum_new();
tnew->enum_d = tenum->enum_d;
*res = tdata_item_new(tic_tenum);
(*res)->u.tenum = tnew;
}
static void tdata_item_subst_tfun(tdata_fun_t *torig, tdata_tvv_t *tvv,
tdata_item_t **res)
{
tdata_fun_t *tnew;
tnew = tdata_fun_new();
tdata_item_subst_fun_sig(torig->tsig, tvv, &tnew->tsig);
*res = tdata_item_new(tic_tfun);
(*res)->u.tfun = tnew;
}
static void tdata_item_subst_tvref(tdata_vref_t *tvref, tdata_tvv_t *tvv,
tdata_item_t **res)
{
tdata_item_t *ti_new;
ti_new = tdata_tvv_get_val(tvv, tvref->targ->name->sid);
assert(ti_new != NULL);
*res = ti_new;
}
static void tdata_item_subst_fun_sig(tdata_fun_sig_t *torig, tdata_tvv_t *tvv,
tdata_fun_sig_t **res)
{
tdata_fun_sig_t *tnew;
list_node_t *arg_n;
tdata_item_t *arg_ti;
tdata_item_t *narg_ti = NULL;
tnew = tdata_fun_sig_new();
list_init(&tnew->arg_ti);
arg_n = list_first(&torig->arg_ti);
while (arg_n != NULL) {
arg_ti = list_node_data(arg_n, tdata_item_t *);
if (arg_ti == NULL)
narg_ti = NULL;
else
tdata_item_subst(arg_ti, tvv, &narg_ti);
list_append(&tnew->arg_ti, narg_ti);
arg_n = list_next(&torig->arg_ti, arg_n);
}
if (torig->varg_ti != NULL)
tdata_item_subst(torig->varg_ti, tvv, &tnew->varg_ti);
if (torig->rtype != NULL)
tdata_item_subst(torig->rtype, tvv, &tnew->rtype);
*res = tnew;
}
void tdata_item_print(tdata_item_t *titem)
{
if (titem == NULL) {
printf("none");
return;
}
switch (titem->tic) {
case tic_tprimitive:
tdata_tprimitive_print(titem->u.tprimitive);
break;
case tic_tobject:
tdata_tobject_print(titem->u.tobject);
break;
case tic_tarray:
tdata_tarray_print(titem->u.tarray);
break;
case tic_tdeleg:
tdata_tdeleg_print(titem->u.tdeleg);
break;
case tic_tebase:
tdata_tebase_print(titem->u.tebase);
break;
case tic_tenum:
tdata_tenum_print(titem->u.tenum);
break;
case tic_tfun:
tdata_tfun_print(titem->u.tfun);
break;
case tic_tvref:
tdata_tvref_print(titem->u.tvref);
break;
case tic_ignore:
printf("ignore");
break;
}
}
static void tdata_tprimitive_print(tdata_primitive_t *tprimitive)
{
switch (tprimitive->tpc) {
case tpc_bool:
printf("bool");
break;
case tpc_char:
printf("char");
break;
case tpc_int:
printf("int");
break;
case tpc_nil:
printf("nil");
break;
case tpc_string:
printf("string");
break;
case tpc_resource:
printf("resource");
break;
}
}
static void tdata_tobject_print(tdata_object_t *tobject)
{
stree_symbol_t *csi_sym;
list_node_t *arg_n;
tdata_item_t *arg;
csi_sym = csi_to_symbol(tobject->csi);
assert(csi_sym != NULL);
symbol_print_fqn(csi_sym);
arg_n = list_first(&tobject->targs);
while (arg_n != NULL) {
arg = list_node_data(arg_n, tdata_item_t *);
putchar('/');
tdata_item_print(arg);
arg_n = list_next(&tobject->targs, arg_n);
}
}
static void tdata_tarray_print(tdata_array_t *tarray)
{
int i;
tdata_item_print(tarray->base_ti);
printf("[");
for (i = 0; i < tarray->rank - 1; ++i)
printf(",");
printf("]");
}
static void tdata_tdeleg_print(tdata_deleg_t *tdeleg)
{
stree_symbol_t *deleg_sym;
deleg_sym = deleg_to_symbol(tdeleg->deleg);
symbol_print_fqn(deleg_sym);
}
static void tdata_tebase_print(tdata_ebase_t *tebase)
{
stree_symbol_t *enum_sym;
enum_sym = enum_to_symbol(tebase->enum_d);
printf("typeref(");
symbol_print_fqn(enum_sym);
printf(")");
}
static void tdata_tenum_print(tdata_enum_t *tenum)
{
stree_symbol_t *enum_sym;
enum_sym = enum_to_symbol(tenum->enum_d);
symbol_print_fqn(enum_sym);
}
static void tdata_tfun_print(tdata_fun_t *tfun)
{
list_node_t *arg_n;
tdata_item_t *arg_ti;
bool_t first;
printf("fun(");
arg_n = list_first(&tfun->tsig->arg_ti);
first = b_true;
while (arg_n != NULL) {
if (first == b_false)
printf("; ");
else
first = b_false;
arg_ti = list_node_data(arg_n, tdata_item_t *);
tdata_item_print(arg_ti);
arg_n = list_next(&tfun->tsig->arg_ti, arg_n);
}
printf(") : ");
tdata_item_print(tfun->tsig->rtype);
}
static void tdata_tvref_print(tdata_vref_t *tvref)
{
printf("%s", strtab_get_str(tvref->targ->name->sid));
}
tdata_item_t *tdata_item_new(titem_class_t tic)
{
tdata_item_t *titem;
titem = calloc(1, sizeof(tdata_item_t));
if (titem == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
titem->tic = tic;
return titem;
}
tdata_array_t *tdata_array_new(void)
{
tdata_array_t *tarray;
tarray = calloc(1, sizeof(tdata_array_t));
if (tarray == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
return tarray;
}
tdata_object_t *tdata_object_new(void)
{
tdata_object_t *tobject;
tobject = calloc(1, sizeof(tdata_object_t));
if (tobject == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
return tobject;
}
tdata_primitive_t *tdata_primitive_new(tprimitive_class_t tpc)
{
tdata_primitive_t *tprimitive;
tprimitive = calloc(1, sizeof(tdata_primitive_t));
if (tprimitive == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
tprimitive->tpc = tpc;
return tprimitive;
}
tdata_deleg_t *tdata_deleg_new(void)
{
tdata_deleg_t *tdeleg;
tdeleg = calloc(1, sizeof(tdata_deleg_t));
if (tdeleg == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
return tdeleg;
}
tdata_ebase_t *tdata_ebase_new(void)
{
tdata_ebase_t *tebase;
tebase = calloc(1, sizeof(tdata_ebase_t));
if (tebase == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
return tebase;
}
tdata_enum_t *tdata_enum_new(void)
{
tdata_enum_t *tenum;
tenum = calloc(1, sizeof(tdata_enum_t));
if (tenum == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
return tenum;
}
tdata_fun_t *tdata_fun_new(void)
{
tdata_fun_t *tfun;
tfun = calloc(1, sizeof(tdata_fun_t));
if (tfun == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
return tfun;
}
tdata_vref_t *tdata_vref_new(void)
{
tdata_vref_t *tvref;
tvref = calloc(1, sizeof(tdata_vref_t));
if (tvref == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
return tvref;
}
tdata_fun_sig_t *tdata_fun_sig_new(void)
{
tdata_fun_sig_t *tfun_sig;
tfun_sig = calloc(1, sizeof(tdata_fun_sig_t));
if (tfun_sig == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
return tfun_sig;
}
tdata_tvv_t *tdata_tvv_new(void)
{
tdata_tvv_t *tvv;
tvv = calloc(1, sizeof(tdata_tvv_t));
if (tvv == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
return tvv;
}
tdata_item_t *tdata_tvv_get_val(tdata_tvv_t *tvv, sid_t name)
{
return (tdata_item_t *)intmap_get(&tvv->tvv, name);
}
void tdata_tvv_set_val(tdata_tvv_t *tvv, sid_t name, tdata_item_t *tvalue)
{
intmap_set(&tvv->tvv, name, tvalue);
}
HelenOS homepage, sources at GitHub