+ TraversingVisitor::visit(binary);
+
+ /* 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;
+
+ binary.lvalue = false;
+
+ 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;
+
+ binary.type = basic_left->base_type;
+ binary.lvalue = 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;
+
+ 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;
+
+ binary.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;
+
+ binary.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;
+
+ binary.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;
+
+ binary.type = (compat==LEFT_CONVERTIBLE ? basic_right : basic_left);
+ }
+ else if((oper=='<' || oper=='>') && oper2==oper)
+ {
+ // Shifts only apply to integers.
+ if(basic_left->kind!=BasicTypeDeclaration::INT || basic_right->kind!=BasicTypeDeclaration::INT)
+ return;
+
+ binary.type = basic_left;
+ }
+ else if(oper=='+' || oper=='-' || oper=='*' || oper=='/')
+ {
+ // Arithmetic operators require scalar elements.
+ if(!is_scalar(*elem_left) || !is_scalar(*elem_right))
+ return;
+
+ if(oper=='*' && is_vector_or_matrix(*basic_left) && is_vector_or_matrix(*basic_right) &&
+ (basic_left->kind==BasicTypeDeclaration::MATRIX || basic_right->kind==BasicTypeDeclaration::MATRIX))
+ {
+ /* Multiplication has special rules when at least one operand is a
+ matrix and the other is a vector or a matrix. */
+ unsigned left_columns = basic_left->size&0xFFFF;
+ unsigned right_rows = basic_right->size;
+ if(basic_right->kind==BasicTypeDeclaration::MATRIX)
+ right_rows >>= 16;
+ if(left_columns!=right_rows)
+ return;
+
+ BasicTypeDeclaration *elem_result = (elem_compat==LEFT_CONVERTIBLE ? elem_right : elem_left);
+
+ if(basic_left->kind==BasicTypeDeclaration::VECTOR)
+ binary.type = find_type(*elem_result, BasicTypeDeclaration::VECTOR, basic_right->size&0xFFFF);
+ else if(basic_right->kind==BasicTypeDeclaration::VECTOR)
+ binary.type = find_type(*elem_result, BasicTypeDeclaration::VECTOR, basic_left->size>>16);
+ else
+ binary.type = find_type(*elem_result, BasicTypeDeclaration::MATRIX, (basic_left->size&0xFFFF0000)|(basic_right->size&0xFFFF));
+ }
+ else if(compat==NOT_COMPATIBLE)
+ {
+ // Arithmetic between scalars and matrices or vectors is supported.
+ if(is_scalar(*basic_left) && is_vector_or_matrix(*basic_right))
+ binary.type = (elem_compat==RIGHT_CONVERTIBLE ? find_type(*elem_left, basic_right->kind, basic_right->size) : basic_right);
+ else if(is_vector_or_matrix(*basic_left) && is_scalar(*basic_right))
+ binary.type = (elem_compat==LEFT_CONVERTIBLE ? find_type(*elem_right, basic_left->kind, basic_left->size) : basic_left);
+ else
+ return;
+ }
+ else if(compat==LEFT_CONVERTIBLE)
+ binary.type = basic_right;
+ else
+ binary.type = basic_left;
+ }
+ else
+ return;
+
+ bool converted = true;
+ if(compat==LEFT_CONVERTIBLE)
+ convert_to(binary.left, *basic_right);
+ else if(compat==RIGHT_CONVERTIBLE)
+ convert_to(binary.right, *basic_left);
+ else if(elem_compat==LEFT_CONVERTIBLE)
+ converted = convert_to_element(binary.left, *elem_right);
+ else if(elem_compat==RIGHT_CONVERTIBLE)
+ converted = convert_to_element(binary.right, *elem_left);
+
+ if(!converted)
+ binary.type = 0;
+}
+
+void ExpressionResolver::visit(Assignment &assign)
+{
+ TraversingVisitor::visit(assign);
+ assign.type = assign.left->type;
+ assign.lvalue = true;
+}
+
+void ExpressionResolver::visit(FunctionCall &call)
+{
+ TraversingVisitor::visit(call);
+
+ if(call.declaration)
+ call.type = call.declaration->return_type_declaration;
+ else if(call.constructor)
+ {
+ map<string, TypeDeclaration *>::const_iterator i=stage->types.find(call.name);
+ call.type = (i!=stage->types.end() ? i->second : 0);
+ }
+ call.lvalue = false;