示例#1
0
void VisitorNodeTyper::visit(ExprSymbol *n){
    // TODO Set flag if global or local
    if (this->cur_class)
    {
        Type* t = this->cur_class->getAttrType(n->getName());   // Get type from class
        if (!t) // If no type returned, it might be an unmangled method name
        {
            if (this->look_for_mangled)
            {
                TypeFunction* tf = new TypeFunction();
                tf->setClass(this->cur_class);
                tf->setName(n->getName());
                t = tf;
            }
            else
            {
                throw __FILE__ "(" QUOTE(__LINE__) "): Attribute " + n->getName() + " not found in class" + this->cur_class->getName() + ".";
            }
        }
        n->setType(t);
    }
    else
    {
        Variable* v = this->symt->get(n->getName());
        if (!v)     // Symbol not found
        {
            if (this->look_for_mangled)     // Look for an unmangled name
            {
                TypeFunction* tf = new TypeFunction();
                tf->setName(n->getName());
                tf->setStatic(true);
                tf->setVirtual(false);
                n->setType(tf);
            }
            else
            {
                throw __FILE__ "(" QUOTE(__LINE__) "): Variable " + n->getName() + " not found.";
            }
        }
        else
        {
            n->setType(v->getType());
        }
    }
}
示例#2
0
void VisitorNodeTyper::visit(class ExprCall *n) {
    ExprSymbol* sym = dynamic_cast<ExprSymbol*>(n->getCallee());
    TypeFunction* tf = 0;

    // If there are arguments, pass through them to find there types
    if (n->getArgs())
    {
        for (std::vector<Expr*>::iterator it = n->getArgs()->begin(); it != n->getArgs()->end(); it++)
        {
            (*it)->accept(*this);
        }
    }

    // Set looking for mangled mode to avoid not in symbol table exception
    bool old_look_for_mangled = this->look_for_mangled;
    this->look_for_mangled = true;
    n->getCallee()->accept(*this);
    this->look_for_mangled = old_look_for_mangled;
    
    tf = dynamic_cast<TypeFunction*>(n->getCallee()->getType());

    std::vector<Signature*>* ss = 0;
    Class* cur_class = 0;

    if (!tf->getClass())
    {
        ss = this->sigs[tf->getName()];
    }
    else
    {
        std::map<std::string, std::vector<Signature*>* >* spc;
        cur_class = tf->getClass();

        while (cur_class != 0)
        {
            spc = this->sigs_per_class[cur_class];
            if (spc)
            {
                ss = (*spc)[tf->getName()];
                if (ss)
                {
                    break;
                }
            }
            cur_class = cur_class->getParent();
        }
        if (!spc)
            throw __FILE__ "(" QUOTE(__LINE__) "): No signature table for class " + tf->getClass()->getName() + ".";
    }

    // TODO optimize
    if (!ss)
        throw __FILE__ "(" QUOTE(__LINE__) "): Symbol " + tf->getName() + " not in signature tables.";

    // Select good function
    std::vector<Signature*>::iterator it;
    bool insert_this = false;
    std::vector<Type*>* choosen_sig = 0;
    for (it = ss->begin(); it != ss->end(); it++)
    {
        // Test for method
        if (areCompatible(n->getArgs(), (*it)->getTypes(), true))
        {
            if (tf->getClass())
            {
                tf = tf->getClass()->getMethodType((*it)->getMangledName());
                n->getCallee()->setType(tf);
            }
            tf->setName((*it)->getMangledName());
            if (choosen_sig)
                throw __FILE__ "(" QUOTE(__LINE__) "): Overloading ambiguity !";
            tf->setStatic(false);
            choosen_sig = (*it)->getTypes();
            // Function is non static, get left part of callee and add it on front of arg stack (this)
            insert_this = true;
        }
        // Test for function OR static method
        else if(areCompatible(n->getArgs(), (*it)->getTypes()))
        {
            if (tf->getClass())
            {
                tf = tf->getClass()->getMethodType((*it)->getMangledName());
                n->getCallee()->setType(tf);
            }
            tf->setName((*it)->getMangledName());
            if (choosen_sig)
                throw __FILE__ "(" QUOTE(__LINE__) "): Overloading ambiguity !";
            choosen_sig = (*it)->getTypes();
            tf->setStatic(true);
            if (sym)
            {
                sym->setName((*it)->getMangledName());
            }
        }
    }
    if (!choosen_sig)
        throw __FILE__ "(" QUOTE(__LINE__) "): No method " + tf->getName()+ " found.";
    if (insert_this)
    {
        ExprOP2* e2 = dynamic_cast<ExprOP2*>(n->getCallee());
        if (!e2)
            throw __FILE__ "(" QUOTE(__LINE__) "): Callee must be OP2.";
        n->getArgs()->insert(n->getArgs()->begin(), e2->getLeft());
    }

    // Set expected types to arguments (derived class to parent class)
    std::vector<Expr*>::iterator ite;
    std::vector<Type*>::iterator itt;
    itt = choosen_sig->begin();
    for (ite = n->getArgs()->begin(); ite != n->getArgs()->end(); ite++, itt++)
    {
        (*ite)->setType(*itt);
    }

    if (sym)
    {
        n->getCallee()->accept(*this);
        tf = dynamic_cast<TypeFunction*>(n->getCallee()->getType());
        if (!tf)
            throw __FILE__ "(" QUOTE(__LINE__) "): Callee value must be of type TypeFunction.";
    }
    else
    {
        tf->setType(tf->getClass()->getMethodReturnType(tf->getName()));
    }
    n->setType(tf->getType());
}