+ r_any_resolved |= (declaration!=memacc.declaration);
+ memacc.declaration = declaration;
+}
+
+void VariableResolver::visit(Swizzle &swizzle)
+{
+ TraversingVisitor::visit(swizzle);
+
+ if(record_target)
+ {
+ unsigned mask = 0;
+ for(unsigned i=0; i<swizzle.count; ++i)
+ mask |= 1<<swizzle.components[i];
+ add_to_chain(Assignment::Target::SWIZZLE, mask);
+ }
+}
+
+void VariableResolver::visit(BinaryExpression &binary)
+{
+ if(binary.oper->token[0]=='[')
+ {
+ {
+ /* The subscript expression is not a part of the primary assignment
+ target. */
+ SetFlag set(record_target, false);
+ visit(binary.right);
+ }
+ visit(binary.left);
+
+ if(record_target)
+ {
+ unsigned index = 0x3F;
+ if(Literal *literal_subscript = dynamic_cast<Literal *>(binary.right.get()))
+ if(literal_subscript->value.check_type<int>())
+ index = literal_subscript->value.value<int>();
+ add_to_chain(Assignment::Target::ARRAY, index);
+ }
+ }
+ else
+ TraversingVisitor::visit(binary);
+}
+
+void VariableResolver::visit(Assignment &assign)
+{
+ {
+ SetFlag set(record_target);
+ r_assignment_target = Assignment::Target();
+ visit(assign.left);
+ r_any_resolved |= (r_assignment_target<assign.target || assign.target<r_assignment_target);
+ assign.target = r_assignment_target;
+ }
+
+ r_self_referencing = false;
+ visit(assign.right);
+ assign.self_referencing = (r_self_referencing || assign.oper->token[0]!='=');
+}
+
+void VariableResolver::merge_layouts(Layout &to_layout, const Layout &from_layout)
+{
+ for(vector<Layout::Qualifier>::const_iterator i=from_layout.qualifiers.begin(); i!=from_layout.qualifiers.end(); ++i)
+ {
+ bool found = false;
+ for(vector<Layout::Qualifier>::iterator j=to_layout.qualifiers.begin(); (!found && j!=to_layout.qualifiers.end()); ++j)
+ if(j->name==i->name)
+ {
+ j->has_value = i->value;
+ j->value = i->value;
+ found = true;
+ }
+
+ if(!found)
+ to_layout.qualifiers.push_back(*i);
+ }
+}
+
+void VariableResolver::visit(VariableDeclaration &var)
+{
+ TraversingVisitor::visit(var);
+ VariableDeclaration *&ptr = current_block->variables[var.name];
+ if(!ptr)
+ ptr = &var;
+ else if(!current_block->parent && ptr->interface==var.interface && ptr->type==var.type)
+ {
+ if(ptr->source==BUILTIN_SOURCE)
+ redeclared_builtins.push_back(&var);
+ else
+ stage->diagnostics.push_back(Diagnostic(Diagnostic::WARN, var.source, var.line,
+ format("Redeclaring non-builtin variable '%s' is deprecated", var.name)));
+
+ if(var.init_expression)
+ ptr->init_expression = var.init_expression;
+ if(var.layout)
+ {
+ if(ptr->layout)
+ merge_layouts(*ptr->layout, *var.layout);
+ else
+ ptr->layout = var.layout;
+ }
+ nodes_to_remove.insert(&var);
+
+ r_any_resolved = true;
+ }
+}
+
+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_basic = dynamic_cast<BasicTypeDeclaration *>(expr->type))
+ {
+ BasicTypeDeclaration *to_type = &elem_type;
+ if(is_vector_or_matrix(*expr_basic))
+ to_type = find_type(elem_type, expr_basic->kind, expr_basic->size);
+ if(to_type)
+ {
+ convert_to(expr, *to_type);
+ return true;
+ }
+ }
+
+ return false;
+}
+
+bool ExpressionResolver::truncate_vector(RefPtr<Expression> &expr, unsigned size)
+{
+ if(BasicTypeDeclaration *expr_basic = dynamic_cast<BasicTypeDeclaration *>(expr->type))
+ if(BasicTypeDeclaration *expr_elem = get_element_type(*expr_basic))
+ {
+ RefPtr<Swizzle> swizzle = new Swizzle;
+ swizzle->left = expr;
+ swizzle->oper = &Operator::get_operator(".", Operator::POSTFIX);
+ swizzle->component_group = string("xyzw", size);
+ swizzle->count = size;
+ for(unsigned i=0; i<size; ++i)
+ swizzle->components[i] = i;
+ if(size==1)
+ swizzle->type = expr_elem;
+ else
+ swizzle->type = find_type(*expr_elem, BasicTypeDeclaration::VECTOR, size);
+ expr = swizzle;
+
+ 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(Block &block)
+{
+ SetForScope<Block *> set_block(current_block, &block);
+ for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
+ {
+ insert_point = i;
+ (*i)->visit(*this);
+ }
+}
+
+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(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(Swizzle &swizzle)
+{
+ TraversingVisitor::visit(swizzle);
+
+ if(BasicTypeDeclaration *left_basic = dynamic_cast<BasicTypeDeclaration *>(swizzle.left->type))
+ {
+ BasicTypeDeclaration *left_elem = get_element_type(*left_basic);
+ if(swizzle.count==1)
+ resolve(swizzle, left_elem, swizzle.left->lvalue);
+ else if(left_basic->kind==BasicTypeDeclaration::VECTOR && left_elem)
+ resolve(swizzle, find_type(*left_elem, left_basic->kind, swizzle.count), swizzle.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)