示例#1
0
void TypeChecker::after(block_class *node) {
    Expressions body = node->get_body();
    Symbol last_body_type = body->nth(body->len() - 1)->get_type();
    if (!last_body_type) {
        return;
    }
    node->set_type(last_body_type);
}
示例#2
0
void TypeChecker::check_formals(tree_node *node, Symbol name, Symbol callee_type, const vector<Symbol> &formals, Expressions actuals) {
    if (formals.size() != actuals->len()) {
        semant_error.semant_error(type_env.current_class, node) << "Method '" << callee_type << "." << name
        <<"' formal parameter count (" << formals.size()
        << ") does not match actual parameter count (" << actuals->len() << ")" << endl;
    }
    for(int i = actuals->first(); actuals->more(i); i = actuals->next(i)) {
        Expression actual = actuals->nth(i);
        if (i == formals.size() || !actual->get_type()) {
            break;
        }
        Symbol formal = formals[i];
        if (formal == SELF_TYPE) {
            formal = callee_type;
        }
        if (!type_env.class_table.is_subtype(actual->get_type(), formal)) {
            semant_error.semant_error(type_env.current_class, node) << "Method '" << callee_type << "." << name
            << "' parameter #" << i << " type mismatch. Actual: '"
            << actual->get_type() << "' should be a subtype of '" << formal << "'" << endl;
        }
    }

}