- var.layout->visit(*this);
- if(var.array_size)
- visit_and_replace(var.array_size);
- if(var.init_expression)
- visit_and_replace(var.init_expression);
- current_block->variables.insert(make_pair(var.name, &var));
-}
-
-void VariableResolver::visit(InterfaceBlock &iface)
-{
- /* Block names can be reused in different interfaces. Prefix the name with
- the first character of the interface to avoid conflicts. */
- stage->interface_blocks.insert(make_pair(iface.interface+iface.name, &iface));
- if(!iface.instance_name.empty())
- stage->interface_blocks.insert(make_pair("_"+iface.instance_name, &iface));
-
- TraversingVisitor::visit(iface);
-}
-
-
-ExpressionResolver::ExpressionResolver():
- stage(0),
- r_any_resolved(false)
-{ }
-
-bool ExpressionResolver::apply(Stage &s)
-{
- stage = &s;
- r_any_resolved = false;
- s.content.visit(*this);
- return r_any_resolved;
-}
-
-bool ExpressionResolver::is_scalar(BasicTypeDeclaration &type)
-{
- return (type.kind==BasicTypeDeclaration::INT || type.kind==BasicTypeDeclaration::FLOAT);
-}
-
-bool ExpressionResolver::is_vector_or_matrix(BasicTypeDeclaration &type)
-{
- return (type.kind==BasicTypeDeclaration::VECTOR || type.kind==BasicTypeDeclaration::MATRIX);
-}
-
-BasicTypeDeclaration *ExpressionResolver::get_element_type(BasicTypeDeclaration &type)
-{
- if(is_vector_or_matrix(type) || type.kind==BasicTypeDeclaration::ARRAY)
- {
- BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type);
- return (basic_base ? get_element_type(*basic_base) : 0);
- }
- else
- return &type;
-}
-
-bool ExpressionResolver::can_convert(BasicTypeDeclaration &from, BasicTypeDeclaration &to)
-{
- if(from.kind==BasicTypeDeclaration::INT && to.kind==BasicTypeDeclaration::FLOAT)
- return from.size<=to.size;
- else if(from.kind!=to.kind)
- return false;
- else if((from.kind==BasicTypeDeclaration::VECTOR || from.kind==BasicTypeDeclaration::MATRIX) && from.size==to.size)
- {
- BasicTypeDeclaration *from_base = dynamic_cast<BasicTypeDeclaration *>(from.base_type);
- BasicTypeDeclaration *to_base = dynamic_cast<BasicTypeDeclaration *>(to.base_type);
- return (from_base && to_base && can_convert(*from_base, *to_base));
- }
- else
- return false;
-}
-
-ExpressionResolver::Compatibility ExpressionResolver::get_compatibility(BasicTypeDeclaration &left, BasicTypeDeclaration &right)
-{
- if(&left==&right)
- return SAME_TYPE;
- else if(can_convert(left, right))
- return LEFT_CONVERTIBLE;
- else if(can_convert(right, left))
- return RIGHT_CONVERTIBLE;
- else
- return NOT_COMPATIBLE;
-}
-
-BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration::Kind kind, unsigned size)
-{
- for(vector<BasicTypeDeclaration *>::const_iterator i=basic_types.begin(); i!=basic_types.end(); ++i)
- if((*i)->kind==kind && (*i)->size==size)
- return *i;
- return 0;
-}
-
-BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration &elem_type, BasicTypeDeclaration::Kind kind, unsigned size)
-{
- for(vector<BasicTypeDeclaration *>::const_iterator i=basic_types.begin(); i!=basic_types.end(); ++i)
- if(get_element_type(**i)==&elem_type && (*i)->kind==kind && (*i)->size==size)
- return *i;
- return 0;
-}
-
-void ExpressionResolver::convert_to(RefPtr<Expression> &expr, BasicTypeDeclaration &type)
-{
- RefPtr<FunctionCall> call = new FunctionCall;
- call->name = type.name;
- call->constructor = true;
- call->arguments.push_back(0);
- call->arguments.back() = expr;
- call->type = &type;
- expr = call;
-}
-
-bool ExpressionResolver::convert_to_element(RefPtr<Expression> &expr, BasicTypeDeclaration &elem_type)
-{
- if(BasicTypeDeclaration *expr_type = dynamic_cast<BasicTypeDeclaration *>(expr->type))
- {
- BasicTypeDeclaration *to_type = &elem_type;
- if(is_vector_or_matrix(*expr_type))
- to_type = find_type(elem_type, expr_type->kind, expr_type->size);
- if(to_type)
- {
- convert_to(expr, *to_type);
- return true;
- }
- }
-
- return false;
-}
-
-void ExpressionResolver::resolve(Expression &expr, TypeDeclaration *type, bool lvalue)
-{
- r_any_resolved |= (type!=expr.type || lvalue!=expr.lvalue);
- expr.type = type;
- expr.lvalue = lvalue;
-}
-
-void ExpressionResolver::visit(Literal &literal)
-{
- if(literal.value.check_type<bool>())
- resolve(literal, find_type(BasicTypeDeclaration::BOOL, 1), false);
- else if(literal.value.check_type<int>())
- resolve(literal, find_type(BasicTypeDeclaration::INT, 32), false);
- else if(literal.value.check_type<float>())
- resolve(literal, find_type(BasicTypeDeclaration::FLOAT, 32), false);
-}
-
-void ExpressionResolver::visit(ParenthesizedExpression &parexpr)
-{
- TraversingVisitor::visit(parexpr);
- resolve(parexpr, parexpr.expression->type, parexpr.expression->lvalue);
-}
-
-void ExpressionResolver::visit(VariableReference &var)
-{
- if(var.declaration)
- resolve(var, var.declaration->type_declaration, true);
-}
-
-void ExpressionResolver::visit(InterfaceBlockReference &iface)
-{
- if(iface.declaration)
- resolve(iface, iface.declaration->type_declaration, true);
-}
-
-void ExpressionResolver::visit(MemberAccess &memacc)
-{
- TraversingVisitor::visit(memacc);
-
- if(memacc.declaration)
- resolve(memacc, memacc.declaration->type_declaration, memacc.left->lvalue);
-}
-
-void ExpressionResolver::visit(UnaryExpression &unary)
-{
- TraversingVisitor::visit(unary);
-
- BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(unary.expression->type);
- if(!basic)
- return;
-
- char oper = unary.oper->token[0];
- if(oper=='!')
- {
- if(basic->kind!=BasicTypeDeclaration::BOOL)
- return;
- }
- else if(oper=='~')