- 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)
-{
- resolve(iface, 0, 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=='~')
- {
- if(basic->kind!=BasicTypeDeclaration::INT)
- return;
- }
- else if(oper=='+' || oper=='-')
- {
- BasicTypeDeclaration *elem = get_element_type(*basic);
- if(!elem || !is_scalar(*elem))
- return;
- }
- resolve(unary, basic, unary.expression->lvalue);
-}
-
-void ExpressionResolver::visit(BinaryExpression &binary, bool assign)
-{
- /* Binary operators are only defined for basic types (not for image or
- structure types). */
- BasicTypeDeclaration *basic_left = dynamic_cast<BasicTypeDeclaration *>(binary.left->type);
- BasicTypeDeclaration *basic_right = dynamic_cast<BasicTypeDeclaration *>(binary.right->type);
- if(!basic_left || !basic_right)
- return;
-
- char oper = binary.oper->token[0];
- if(oper=='[')
- {
- /* Subscripting operates on vectors, matrices and arrays, and the right
- operand must be an integer. */
- if((!is_vector_or_matrix(*basic_left) && basic_left->kind!=BasicTypeDeclaration::ARRAY) || basic_right->kind!=BasicTypeDeclaration::INT)
- return;
-
- resolve(binary, basic_left->base_type, binary.left->lvalue);
- return;
- }
- else if(basic_left->kind==BasicTypeDeclaration::ARRAY || basic_right->kind==BasicTypeDeclaration::ARRAY)
- // No other binary operator can be used with arrays.
- return;
-
- BasicTypeDeclaration *elem_left = get_element_type(*basic_left);
- BasicTypeDeclaration *elem_right = get_element_type(*basic_right);
- if(!elem_left || !elem_right)
- return;
-
- Compatibility compat = get_compatibility(*basic_left, *basic_right);
- Compatibility elem_compat = get_compatibility(*elem_left, *elem_right);
- if(elem_compat==NOT_COMPATIBLE)
- return;
- if(assign && (compat==LEFT_CONVERTIBLE || elem_compat==LEFT_CONVERTIBLE))
- return;
-
- TypeDeclaration *type = 0;
- char oper2 = binary.oper->token[1];
- if((oper=='<' && oper2!='<') || (oper=='>' && oper2!='>'))
- {
- /* Relational operators compare two scalar integer or floating-point
- values. */
- if(!is_scalar(*elem_left) || !is_scalar(*elem_right) || compat==NOT_COMPATIBLE)
- return;
-
- type = find_type(BasicTypeDeclaration::BOOL, 1);
- }
- else if((oper=='=' || oper=='!') && oper2=='=')
- {
- // Equality comparison can be done on any compatible types.
- if(compat==NOT_COMPATIBLE)
- return;
-
- type = find_type(BasicTypeDeclaration::BOOL, 1);
- }
- else if(oper2=='&' || oper2=='|' || oper2=='^')
- {
- // Logical operators can only be applied to booleans.
- if(basic_left->kind!=BasicTypeDeclaration::BOOL || basic_right->kind!=BasicTypeDeclaration::BOOL)
- return;
-
- type = basic_left;
- }
- else if((oper=='&' || oper=='|' || oper=='^' || oper=='%') && !oper2)
- {
- // Bitwise operators and modulo can only be applied to integers.
- if(basic_left->kind!=BasicTypeDeclaration::INT || basic_right->kind!=BasicTypeDeclaration::INT)
- return;
-
- type = (compat==LEFT_CONVERTIBLE ? basic_right : basic_left);
- }
- else if((oper=='<' || oper=='>') && oper2==oper)
- {
- // Shifts apply to integer scalars and vectors, with some restrictions.
- if(elem_left->kind!=BasicTypeDeclaration::INT || elem_right->kind!=BasicTypeDeclaration::INT)
- return;
- unsigned left_size = (basic_left->kind==BasicTypeDeclaration::INT ? 1 : basic_left->kind==BasicTypeDeclaration::VECTOR ? basic_left->size : 0);
- unsigned right_size = (basic_right->kind==BasicTypeDeclaration::INT ? 1 : basic_right->kind==BasicTypeDeclaration::VECTOR ? basic_right->size : 0);
- if(!left_size || (left_size==1 && right_size!=1) || (left_size>1 && right_size!=1 && right_size!=left_size))
- return;
-
- type = basic_left;
- // Don't perform conversion even if the operands are of different sizes.
- compat = SAME_TYPE;
- }
- else if(oper=='+' || oper=='-' || oper=='*' || oper=='/')