- r_assignment_target.declaration = declaration;
- }
- // TODO This check is overly broad and may prevent some optimizations.
- else if(declaration && declaration==r_assignment_target.declaration)
- r_self_referencing = true;
-}
-
-void VariableResolver::visit(VariableReference &var)
-{
- VariableDeclaration *declaration = 0;
-
- /* Look for variable declarations in the block hierarchy first. Interface
- blocks are always defined in the top level so we can't accidentally skip
- one. */
- for(Block *block=current_block; (!declaration && block); block=block->parent)
- {
- map<string, VariableDeclaration *>::iterator i = block->variables.find(var.name);
- if(i!=block->variables.end())
- declaration = i->second;
- }
-
- if(!declaration)
- {
- const map<string, InterfaceBlock *> &blocks = stage->interface_blocks;
- map<string, InterfaceBlock *>::const_iterator i = blocks.find("_"+var.name);
- if(i!=blocks.end())
- {
- /* The name refers to an interface block with an instance name rather
- than a variable. Prepare a new syntax tree node accordingly. */
- InterfaceBlockReference *iface_ref = new InterfaceBlockReference;
- iface_ref->source = var.source;
- iface_ref->line = var.line;
- iface_ref->name = var.name;
- iface_ref->declaration = i->second;
- r_replacement_expr = iface_ref;
- }
- else
- {
- // Look for the variable in anonymous interface blocks.
- for(i=blocks.begin(); (!declaration && i!=blocks.end()); ++i)
- if(i->second->instance_name.empty() && i->second->struct_declaration)
- {
- const map<string, VariableDeclaration *> &iface_vars = i->second->struct_declaration->members.variables;
- map<string, VariableDeclaration *>::const_iterator j = iface_vars.find(var.name);
- if(j!=iface_vars.end())
- declaration = j->second;
- }
- }
- }
-
- r_any_resolved |= (declaration!=var.declaration);
- var.declaration = declaration;
-
- check_assignment_target(var.declaration);
-}
-
-void VariableResolver::visit(InterfaceBlockReference &iface)
-{
- map<string, InterfaceBlock *>::iterator i = stage->interface_blocks.find("_"+iface.name);
- InterfaceBlock *declaration = (i!=stage->interface_blocks.end() ? i->second : 0);
- r_any_resolved |= (declaration!=iface.declaration);
- iface.declaration = declaration;
-
- check_assignment_target(iface.declaration);
-}
-
-void VariableResolver::add_to_chain(Assignment::Target::ChainType type, unsigned index)
-{
- if(r_assignment_target.chain_len<7)
- r_assignment_target.chain[r_assignment_target.chain_len] = type | min<unsigned>(index, 0x3F);
- ++r_assignment_target.chain_len;
-}
-
-void VariableResolver::visit(MemberAccess &memacc)
-{
- TraversingVisitor::visit(memacc);
-
- VariableDeclaration *declaration = 0;
- if(StructDeclaration *strct = dynamic_cast<StructDeclaration *>(memacc.left->type))
- {
- map<string, VariableDeclaration *>::iterator i = strct->members.variables.find(memacc.member);
- if(i!=strct->members.variables.end())
- {
- declaration = i->second;
-
- if(record_target)
- {
- unsigned index = 0;
- for(NodeList<Statement>::const_iterator j=strct->members.body.begin(); (j!=strct->members.body.end() && j->get()!=i->second); ++j)
- ++index;
-
- add_to_chain(Assignment::Target::MEMBER, index);
- }
- }
- }
- else if(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(memacc.left->type))
- {
- bool scalar_swizzle = ((basic->kind==BasicTypeDeclaration::INT || basic->kind==BasicTypeDeclaration::FLOAT) && memacc.member.size()==1);
- bool vector_swizzle = (basic->kind==BasicTypeDeclaration::VECTOR && memacc.member.size()<=4);
- if(scalar_swizzle || vector_swizzle)
- {
- static const char component_names[] = { 'x', 'r', 's', 'y', 'g', 't', 'z', 'b', 'p', 'w', 'a', 'q' };
-
- bool ok = true;
- UInt8 components[4] = { };
- for(unsigned i=0; (ok && i<memacc.member.size()); ++i)
- ok = ((components[i] = (find(component_names, component_names+12, memacc.member[i])-component_names)/3) < 4);
-
- if(ok)
- {
- Swizzle *swizzle = new Swizzle;
- swizzle->source = memacc.source;
- swizzle->line = memacc.line;
- swizzle->oper = memacc.oper;
- swizzle->left = memacc.left;
- swizzle->component_group = memacc.member;
- swizzle->count = memacc.member.size();
- copy(components, components+memacc.member.size(), swizzle->components);
- r_replacement_expr = swizzle;
- }
- }
- }
-
- 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::visit(VariableDeclaration &var)
-{
- TraversingVisitor::visit(var);
- 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)