HelenOS sources
This source file includes following definitions.
- ancr_module_process
- ancr_csi_dfs
- ancr_csi_process
- ancr_csi_get_pred
- ancr_csi_print_cycle
#include <stdlib.h>
#include <assert.h>
#include "builtin.h"
#include "cspan.h"
#include "list.h"
#include "mytypes.h"
#include "stree.h"
#include "strtab.h"
#include "symbol.h"
#include "ancr.h"
static void ancr_csi_dfs(stree_program_t *prog, stree_csi_t *csi);
static void ancr_csi_process(stree_program_t *prog, stree_csi_t *node);
static stree_csi_t *ancr_csi_get_pred(stree_program_t *prog, stree_csi_t *csi,
stree_texpr_t *pred_ref);
static void ancr_csi_print_cycle(stree_program_t *prog, stree_csi_t *node);
void ancr_module_process(stree_program_t *prog, stree_module_t *module)
{
list_node_t *node;
stree_modm_t *modm;
(void) module;
node = list_first(&prog->module->members);
while (node != NULL) {
modm = list_node_data(node, stree_modm_t *);
switch (modm->mc) {
case mc_csi:
ancr_csi_dfs(prog, modm->u.csi);
break;
case mc_enum:
break;
}
node = list_next(&prog->module->members, node);
}
}
static void ancr_csi_dfs(stree_program_t *prog, stree_csi_t *csi)
{
list_node_t *node;
stree_csimbr_t *csimbr;
ancr_csi_process(prog, csi);
node = list_first(&csi->members);
while (node != NULL) {
csimbr = list_node_data(node, stree_csimbr_t *);
if (csimbr->cc == csimbr_csi)
ancr_csi_dfs(prog, csimbr->u.csi);
node = list_next(&csi->members, node);
}
}
static void ancr_csi_process(stree_program_t *prog, stree_csi_t *csi)
{
stree_csi_t *base_csi, *outer_csi;
stree_csi_t *gf_class;
list_node_t *pred_n;
stree_texpr_t *pred;
stree_csi_t *pred_csi;
if (csi->ancr_state == ws_visited) {
return;
}
if (csi->ancr_state == ws_active) {
printf("Error: Circular class, struct or interface chain: ");
ancr_csi_print_cycle(prog, csi);
printf(".\n");
exit(1);
}
csi->ancr_state = ws_active;
outer_csi = csi_to_symbol(csi)->outer_csi;
gf_class = builtin_get_gf_class(prog->builtin);
if (csi != gf_class) {
base_csi = gf_class;
} else {
base_csi = NULL;
}
if (outer_csi != NULL)
ancr_csi_process(prog, outer_csi);
pred_n = list_first(&csi->inherit);
if (csi->cc == csi_class && pred_n != NULL) {
pred = list_node_data(pred_n, stree_texpr_t *);
pred_csi = ancr_csi_get_pred(prog, csi, pred);
assert(pred_csi != NULL);
if (pred_csi->cc == csi_class) {
base_csi = pred_csi;
ancr_csi_process(prog, pred_csi);
pred_n = list_next(&csi->inherit, pred_n);
}
}
while (pred_n != NULL) {
pred = list_node_data(pred_n, stree_texpr_t *);
pred_csi = ancr_csi_get_pred(prog, csi, pred);
assert(pred_csi != NULL);
ancr_csi_process(prog, pred_csi);
switch (pred_csi->cc) {
case csi_class:
switch (csi->cc) {
case csi_class:
cspan_print(csi->name->cspan);
printf(" Error: Only the first predecessor "
"can be a class. ('");
symbol_print_fqn(csi_to_symbol(csi));
printf("' deriving from '");
symbol_print_fqn(csi_to_symbol(pred_csi));
printf("').\n");
exit(1);
break;
case csi_struct:
assert(b_false);
break;
case csi_interface:
cspan_print(csi->name->cspan);
printf(" Error: Interface predecessor must be "
"an interface ('");
symbol_print_fqn(csi_to_symbol(csi));
printf("' deriving from '");
symbol_print_fqn(csi_to_symbol(pred_csi));
printf("').\n");
exit(1);
break;
}
break;
case csi_struct:
assert(b_false);
break;
case csi_interface:
break;
}
pred_n = list_next(&csi->inherit, pred_n);
}
csi->ancr_state = ws_visited;
csi->base_csi = base_csi;
}
static stree_csi_t *ancr_csi_get_pred(stree_program_t *prog, stree_csi_t *csi,
stree_texpr_t *pred_ref)
{
stree_csi_t *outer_csi;
stree_symbol_t *pred_sym;
stree_csi_t *pred_csi;
outer_csi = csi_to_symbol(csi)->outer_csi;
pred_sym = symbol_xlookup_in_csi(prog, outer_csi, pred_ref);
pred_csi = symbol_to_csi(pred_sym);
assert(pred_csi != NULL);
return pred_csi;
}
static void ancr_csi_print_cycle(stree_program_t *prog, stree_csi_t *node)
{
stree_csi_t *n;
stree_symbol_t *pred_sym, *node_sym;
stree_csi_t *pred_csi, *outer_csi;
stree_texpr_t *pred;
list_node_t *pred_n;
n = node;
do {
node_sym = csi_to_symbol(node);
symbol_print_fqn(node_sym);
printf(", ");
outer_csi = node_sym->outer_csi;
if (outer_csi != NULL && outer_csi->ancr_state == ws_active) {
node = outer_csi;
} else {
node = NULL;
pred_n = list_first(&node->inherit);
while (pred_n != NULL) {
pred = list_node_data(pred_n, stree_texpr_t *);
pred_sym = symbol_xlookup_in_csi(prog,
outer_csi, pred);
pred_csi = symbol_to_csi(pred_sym);
assert(pred_csi != NULL);
if (pred_csi->ancr_state == ws_active) {
node = pred_csi;
break;
}
}
assert(node != NULL);
}
} while (n != node);
node_sym = csi_to_symbol(node);
symbol_print_fqn(node_sym);
}
HelenOS homepage, sources at GitHub