]> git.tdb.fi Git - libs/gl.git/commitdiff
Split glsl/generate.cpp in two
authorMikko Rasa <tdb@tdb.fi>
Thu, 1 Apr 2021 19:26:09 +0000 (22:26 +0300)
committerMikko Rasa <tdb@tdb.fi>
Thu, 1 Apr 2021 19:29:06 +0000 (22:29 +0300)
source/glsl/compiler.cpp
source/glsl/generate.cpp
source/glsl/generate.h
source/glsl/resolve.cpp [new file with mode: 0644]
source/glsl/resolve.h [new file with mode: 0644]

index d8b060ea4e3873285980373e45974ee1b6b84950..b810e52bef5e0af758379f4c12a086a13d22a8e5 100644 (file)
@@ -10,6 +10,7 @@
 #include "glsl_error.h"
 #include "optimize.h"
 #include "output.h"
+#include "resolve.h"
 #include "resources.h"
 #include "validate.h"
 
index 923468a1d59ec905b45cba4a4dd4e88dc2b36636..13fb72c53039bfbd7e9fed3779bc718bdc6801d7 100644 (file)
@@ -1,9 +1,5 @@
-#include <algorithm>
 #include <msp/core/hash.h>
 #include <msp/core/raii.h>
-#include <msp/strings/lexicalcast.h>
-#include <msp/strings/utils.h>
-#include "builtin.h"
 #include "generate.h"
 
 using namespace std;
@@ -65,1201 +61,6 @@ void ConstantSpecializer::visit(VariableDeclaration &var)
 }
 
 
-void BlockHierarchyResolver::enter(Block &block)
-{
-       r_any_resolved |= (current_block!=block.parent);
-       block.parent = current_block;
-}
-
-
-TypeResolver::TypeResolver():
-       stage(0),
-       iface_block(0),
-       r_any_resolved(false)
-{ }
-
-bool TypeResolver::apply(Stage &s)
-{
-       stage = &s;
-       s.types.clear();
-       r_any_resolved = false;
-       s.content.visit(*this);
-       return r_any_resolved;
-}
-
-TypeDeclaration *TypeResolver::get_or_create_array_type(TypeDeclaration &type)
-{
-       map<TypeDeclaration *, TypeDeclaration *>::iterator i = array_types.find(&type);
-       if(i!=array_types.end())
-               return i->second;
-
-       BasicTypeDeclaration *array = new BasicTypeDeclaration;
-       array->source = INTERNAL_SOURCE;
-       array->name = type.name+"[]";
-       array->kind = BasicTypeDeclaration::ARRAY;
-       array->base = type.name;
-       array->base_type = &type;
-       stage->content.body.insert(type_insert_point, array);
-       array_types[&type] = array;
-       return array;
-}
-
-void TypeResolver::resolve_type(TypeDeclaration *&type, const string &name, bool array)
-{
-       TypeDeclaration *resolved = 0;
-       map<string, TypeDeclaration *>::iterator i = stage->types.find(name);
-       if(i!=stage->types.end())
-       {
-               map<TypeDeclaration *, TypeDeclaration *>::iterator j = alias_map.find(i->second);
-               resolved = (j!=alias_map.end() ? j->second : i->second);
-       }
-
-       if(resolved && array)
-               resolved = get_or_create_array_type(*resolved);
-
-       r_any_resolved |= (resolved!=type);
-       type=resolved;
-}
-
-void TypeResolver::visit(Block &block)
-{
-       for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
-       {
-               if(!block.parent)
-                       type_insert_point = i;
-               (*i)->visit(*this);
-       }
-}
-
-void TypeResolver::visit(BasicTypeDeclaration &type)
-{
-       resolve_type(type.base_type, type.base, false);
-
-       if(type.kind==BasicTypeDeclaration::VECTOR && type.base_type)
-               if(BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type))
-                       if(basic_base->kind==BasicTypeDeclaration::VECTOR)
-                       {
-                               type.kind = BasicTypeDeclaration::MATRIX;
-                               /* A matrix's base type is its column vector type.  This will put
-                               the column vector's size, i.e. the matrix's row count, in the high
-                               half of the size. */
-                               type.size |= basic_base->size<<16;
-                       }
-
-       if(type.kind==BasicTypeDeclaration::ALIAS && type.base_type)
-               alias_map[&type] = type.base_type;
-       else if(type.kind==BasicTypeDeclaration::ARRAY && type.base_type)
-               array_types[type.base_type] = &type;
-
-       stage->types.insert(make_pair(type.name, &type));
-}
-
-void TypeResolver::visit(ImageTypeDeclaration &type)
-{
-       resolve_type(type.base_type, type.base, false);
-       stage->types.insert(make_pair(type.name, &type));
-}
-
-void TypeResolver::visit(StructDeclaration &strct)
-{
-       stage->types.insert(make_pair(strct.name, &strct));
-       TraversingVisitor::visit(strct);
-}
-
-void TypeResolver::visit(VariableDeclaration &var)
-{
-       resolve_type(var.type_declaration, var.type, var.array);
-       if(iface_block && var.interface==iface_block->interface)
-               var.interface.clear();
-}
-
-void TypeResolver::visit(InterfaceBlock &iface)
-{
-       if(iface.members)
-       {
-               SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
-               iface.members->visit(*this);
-
-               StructDeclaration *strct = new StructDeclaration;
-               strct->source = INTERNAL_SOURCE;
-               strct->name = format("_%s_%s", iface.interface, iface.block_name);
-               strct->members.body.splice(strct->members.body.begin(), iface.members->body);
-               stage->content.body.insert(type_insert_point, strct);
-               stage->types.insert(make_pair(strct->name, strct));
-
-               iface.members = 0;
-               strct->interface_block = &iface;
-               iface.struct_declaration = strct;
-       }
-
-       TypeDeclaration *type = iface.struct_declaration;
-       if(type && iface.array)
-               type = get_or_create_array_type(*type);
-       r_any_resolved = (type!=iface.type_declaration);
-       iface.type_declaration = type;
-}
-
-void TypeResolver::visit(FunctionDeclaration &func)
-{
-       resolve_type(func.return_type_declaration, func.return_type, false);
-       TraversingVisitor::visit(func);
-}
-
-
-VariableResolver::VariableResolver():
-       stage(0),
-       r_any_resolved(false),
-       record_target(false),
-       r_self_referencing(false)
-{ }
-
-bool VariableResolver::apply(Stage &s)
-{
-       stage = &s;
-       s.interface_blocks.clear();
-       r_any_resolved = false;
-       s.content.visit(*this);
-       for(vector<VariableDeclaration *>::const_iterator i=redeclared_builtins.begin(); i!=redeclared_builtins.end(); ++i)
-               (*i)->source = GENERATED_SOURCE;
-       NodeRemover().apply(s, nodes_to_remove);
-       return r_any_resolved;
-}
-
-void VariableResolver::enter(Block &block)
-{
-       block.variables.clear();
-}
-
-void VariableResolver::visit(RefPtr<Expression> &expr)
-{
-       r_replacement_expr = 0;
-       expr->visit(*this);
-       if(r_replacement_expr)
-       {
-               expr = r_replacement_expr;
-               /* Don't record assignment target when doing a replacement, because chain
-               information won't be correct. */
-               r_assignment_target.declaration = 0;
-               r_any_resolved = true;
-       }
-       r_replacement_expr = 0;
-}
-
-void VariableResolver::check_assignment_target(Statement *declaration)
-{
-       if(record_target)
-       {
-               if(r_assignment_target.declaration)
-               {
-                       /* More than one reference found in assignment target.  Unable to
-                       determine what the primary target is. */
-                       record_target = false;
-                       r_assignment_target.declaration = 0;
-               }
-               else
-                       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::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.block_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_nocopy(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)
-       {
-               // 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=='/')
-       {
-               // 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)
-                               type = find_type(*elem_result, BasicTypeDeclaration::VECTOR, basic_right->size&0xFFFF);
-                       else if(basic_right->kind==BasicTypeDeclaration::VECTOR)
-                               type = find_type(*elem_result, BasicTypeDeclaration::VECTOR, basic_left->size>>16);
-                       else
-                               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))
-                               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))
-                               type = (elem_compat==LEFT_CONVERTIBLE ? find_type(*elem_right, basic_left->kind, basic_left->size) : basic_left);
-                       else
-                               return;
-               }
-               else if(compat==LEFT_CONVERTIBLE)
-                       type = basic_right;
-               else
-                       type = basic_left;
-       }
-       else
-               return;
-
-       if(assign && type!=basic_left)
-               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)
-               type = 0;
-
-       resolve(binary, type, assign);
-}
-
-void ExpressionResolver::visit(BinaryExpression &binary)
-{
-       TraversingVisitor::visit(binary);
-       visit(binary, false);
-}
-
-void ExpressionResolver::visit(Assignment &assign)
-{
-       TraversingVisitor::visit(assign);
-
-       if(assign.oper->token[0]!='=')
-               return visit(assign, true);
-       else if(assign.left->type!=assign.right->type)
-       {
-               BasicTypeDeclaration *basic_left = dynamic_cast<BasicTypeDeclaration *>(assign.left->type);
-               BasicTypeDeclaration *basic_right = dynamic_cast<BasicTypeDeclaration *>(assign.right->type);
-               if(!basic_left || !basic_right)
-                       return;
-
-               Compatibility compat = get_compatibility(*basic_left, *basic_right);
-               if(compat==RIGHT_CONVERTIBLE)
-                       convert_to(assign.right, *basic_left);
-               else if(compat!=SAME_TYPE)
-                       return;
-       }
-
-       resolve(assign, assign.left->type, true);
-}
-
-void ExpressionResolver::visit(TernaryExpression &ternary)
-{
-       TraversingVisitor::visit(ternary);
-
-       BasicTypeDeclaration *basic_cond = dynamic_cast<BasicTypeDeclaration *>(ternary.condition->type);
-       if(!basic_cond || basic_cond->kind!=BasicTypeDeclaration::BOOL)
-               return;
-
-       TypeDeclaration *type = 0;
-       if(ternary.true_expr->type==ternary.false_expr->type)
-               type = ternary.true_expr->type;
-       else
-       {
-               BasicTypeDeclaration *basic_true = dynamic_cast<BasicTypeDeclaration *>(ternary.true_expr->type);
-               BasicTypeDeclaration *basic_false = dynamic_cast<BasicTypeDeclaration *>(ternary.false_expr->type);
-               if(!basic_true || !basic_false)
-                       return;
-
-               Compatibility compat = get_compatibility(*basic_true, *basic_false);
-               if(compat==NOT_COMPATIBLE)
-                       return;
-
-               type = (compat==LEFT_CONVERTIBLE ? basic_true : basic_false);
-
-               if(compat==LEFT_CONVERTIBLE)
-                       convert_to(ternary.true_expr, *basic_false);
-               else if(compat==RIGHT_CONVERTIBLE)
-                       convert_to(ternary.false_expr, *basic_true);
-       }
-
-       resolve(ternary, type, false);
-}
-
-void ExpressionResolver::visit_constructor(FunctionCall &call)
-{
-       if(call.arguments.empty())
-               return;
-
-       map<string, TypeDeclaration *>::const_iterator i = stage->types.find(call.name);
-       if(i==stage->types.end())
-               return;
-       else if(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(i->second))
-       {
-               BasicTypeDeclaration *elem = get_element_type(*basic);
-               if(!elem)
-                       return;
-
-               vector<ArgumentInfo> args;
-               args.reserve(call.arguments.size());
-               unsigned arg_component_total = 0;
-               bool has_matrices = false;
-               for(NodeArray<Expression>::const_iterator j=call.arguments.begin(); j!=call.arguments.end(); ++j)
-               {
-                       ArgumentInfo info;
-                       if(!(info.type=dynamic_cast<BasicTypeDeclaration *>((*j)->type)))
-                               return;
-                       if(is_scalar(*info.type) || info.type->kind==BasicTypeDeclaration::BOOL)
-                               info.component_count = 1;
-                       else if(info.type->kind==BasicTypeDeclaration::VECTOR)
-                               info.component_count = info.type->size;
-                       else if(info.type->kind==BasicTypeDeclaration::MATRIX)
-                       {
-                               info.component_count = (info.type->size>>16)*(info.type->size&0xFFFF);
-                               has_matrices = true;
-                       }
-                       else
-                               return;
-                       arg_component_total += info.component_count;
-                       args.push_back(info);
-               }
-
-               bool convert_args = false;
-               if((is_scalar(*basic) || basic->kind==BasicTypeDeclaration::BOOL) && call.arguments.size()==1 && !has_matrices)
-               {
-                       if(arg_component_total>1)
-                               truncate_vector(call.arguments.front(), 1);
-
-                       /* Single-element type constructors never need to convert their
-                       arguments because the constructor *is* the conversion. */
-               }
-               else if(basic->kind==BasicTypeDeclaration::VECTOR && !has_matrices)
-               {
-                       /* Vector constructors need either a single scalar argument or
-                       enough components to fill out the vector. */
-                       if(arg_component_total!=1 && arg_component_total<basic->size)
-                               return;
-
-                       /* A vector of same size can be converted directly.  For other
-                       combinations the individual arguments need to be converted. */
-                       if(call.arguments.size()==1)
-                       {
-                               if(arg_component_total==1)
-                                       convert_args = true;
-                               else if(arg_component_total>basic->size)
-                                       truncate_vector(call.arguments.front(), basic->size);
-                       }
-                       else if(arg_component_total==basic->size)
-                               convert_args = true;
-                       else
-                               return;
-               }
-               else if(basic->kind==BasicTypeDeclaration::MATRIX)
-               {
-                       unsigned column_count = basic->size&0xFFFF;
-                       unsigned row_count = basic->size>>16;
-                       if(call.arguments.size()==1)
-                       {
-                               /* A matrix can be constructed from a single element or another
-                               matrix of sufficient size. */
-                               if(arg_component_total==1)
-                                       convert_args = true;
-                               else if(args.front().type->kind==BasicTypeDeclaration::MATRIX)
-                               {
-                                       unsigned arg_columns = args.front().type->size&0xFFFF;
-                                       unsigned arg_rows = args.front().type->size>>16;
-                                       if(arg_columns<column_count || arg_rows<row_count)
-                                               return;
-
-                                       /* Always generate a temporary here and let the optimization
-                                       stage inline it if that's reasonable. */
-                                       RefPtr<VariableDeclaration> temporary = new VariableDeclaration;
-                                       temporary->type = args.front().type->name;
-                                       temporary->name = get_unused_variable_name(*current_block, "_temp");
-                                       temporary->init_expression = call.arguments.front();
-                                       current_block->body.insert(insert_point, temporary);
-
-                                       // Create expressions to build each column.
-                                       vector<RefPtr<Expression> > columns;
-                                       columns.reserve(column_count);
-                                       for(unsigned j=0; j<column_count; ++j)
-                                       {
-                                               RefPtr<VariableReference> ref = new VariableReference;
-                                               ref->name = temporary->name;
-
-                                               RefPtr<Literal> index = new Literal;
-                                               index->token = lexical_cast<string>(j);
-                                               index->value = static_cast<int>(j);
-
-                                               RefPtr<BinaryExpression> subscript = new BinaryExpression;
-                                               subscript->left = ref;
-                                               subscript->oper = &Operator::get_operator("[", Operator::BINARY);
-                                               subscript->right = index;
-                                               subscript->type = args.front().type->base_type;
-
-                                               columns.push_back(subscript);
-                                               if(arg_rows>row_count)
-                                                       truncate_vector(columns.back(), row_count);
-                                       }
-
-                                       call.arguments.resize(column_count);
-                                       copy(columns.begin(), columns.end(), call.arguments.begin());
-
-                                       /* Let VariableResolver process the new nodes and finish
-                                       resolving the constructor on the next pass. */
-                                       r_any_resolved = true;
-                                       return;
-                               }
-                               else
-                                       return;
-                       }
-                       else if(arg_component_total==column_count*row_count && !has_matrices)
-                       {
-                               /* Construct a matrix from individual components in column-major
-                               order.  Arguments must align at column boundaries. */
-                               vector<RefPtr<Expression> > columns;
-                               columns.reserve(column_count);
-
-                               vector<RefPtr<Expression> > column_args;
-                               column_args.reserve(row_count);
-                               unsigned column_component_count = 0;
-
-                               for(unsigned j=0; j<call.arguments.size(); ++j)
-                               {
-                                       const ArgumentInfo &info = args[j];
-                                       if(!column_component_count && info.type->kind==BasicTypeDeclaration::VECTOR && info.component_count==row_count)
-                                               // A vector filling the entire column can be used as is.
-                                               columns.push_back(call.arguments[j]);
-                                       else
-                                       {
-                                               column_args.push_back(call.arguments[j]);
-                                               column_component_count += info.component_count;
-                                               if(column_component_count==row_count)
-                                               {
-                                                       /* The column has filled up.  Create a vector constructor
-                                                       for it.*/
-                                                       RefPtr<FunctionCall> column_call = new FunctionCall;
-                                                       column_call->name = basic->base_type->name;
-                                                       column_call->constructor = true;
-                                                       column_call->arguments.resize(column_args.size());
-                                                       copy(column_args.begin(), column_args.end(), column_call->arguments.begin());
-                                                       column_call->type = basic->base_type;
-                                                       visit_constructor(*column_call);
-                                                       columns.push_back(column_call);
-
-                                                       column_args.clear();
-                                                       column_component_count = 0;
-                                               }
-                                               else if(column_component_count>row_count)
-                                                       // Argument alignment mismatch.
-                                                       return;
-                                       }
-                               }
-                       }
-                       else
-                               return;
-               }
-               else
-                       return;
-
-               if(convert_args)
-               {
-                       // The argument list may have changed so can't rely on args.
-                       for(NodeArray<Expression>::iterator j=call.arguments.begin(); j!=call.arguments.end(); ++j)
-                               if(BasicTypeDeclaration *basic_arg = dynamic_cast<BasicTypeDeclaration *>((*j)->type))
-                               {
-                                       BasicTypeDeclaration *elem_arg = get_element_type(*basic_arg);
-                                       if(elem_arg!=elem)
-                                               convert_to_element(*j, *elem);
-                               }
-               }
-       }
-       else if(StructDeclaration *strct = dynamic_cast<StructDeclaration *>(i->second))
-       {
-               if(call.arguments.size()!=strct->members.body.size())
-                       return;
-
-               unsigned k = 0;
-               for(NodeList<Statement>::const_iterator j=strct->members.body.begin(); j!=strct->members.body.end(); ++j, ++k)
-               {
-                       if(VariableDeclaration *var = dynamic_cast<VariableDeclaration *>(j->get()))
-                       {
-                               if(!call.arguments[k]->type || call.arguments[k]->type!=var->type_declaration)
-                                       return;
-                       }
-                       else
-                               return;
-               }
-       }
-
-       resolve(call, i->second, false);
-}
-
-void ExpressionResolver::visit(FunctionCall &call)
-{
-       TraversingVisitor::visit(call);
-
-       if(call.declaration)
-               resolve(call, call.declaration->return_type_declaration, false);
-       else if(call.constructor)
-               visit_constructor(call);
-}
-
-void ExpressionResolver::visit(BasicTypeDeclaration &type)
-{
-       basic_types.push_back(&type);
-}
-
-void ExpressionResolver::visit(VariableDeclaration &var)
-{
-       TraversingVisitor::visit(var);
-       if(!var.init_expression)
-               return;
-
-       BasicTypeDeclaration *var_basic = dynamic_cast<BasicTypeDeclaration *>(var.type_declaration);
-       BasicTypeDeclaration *init_basic = dynamic_cast<BasicTypeDeclaration *>(var.init_expression->type);
-       if(!var_basic || !init_basic)
-               return;
-
-       Compatibility compat = get_compatibility(*var_basic, *init_basic);
-       if(compat==RIGHT_CONVERTIBLE)
-               convert_to(var.init_expression, *var_basic);
-}
-
-
-bool FunctionResolver::apply(Stage &s)
-{
-       stage = &s;
-       s.functions.clear();
-       r_any_resolved = false;
-       s.content.visit(*this);
-       return r_any_resolved;
-}
-
-void FunctionResolver::visit(FunctionCall &call)
-{
-       FunctionDeclaration *declaration = 0;
-       if(stage->types.count(call.name))
-               call.constructor = true;
-       else
-       {
-               string arg_types;
-               bool has_signature = true;
-               for(NodeArray<Expression>::const_iterator i=call.arguments.begin(); (has_signature && i!=call.arguments.end()); ++i)
-               {
-                       if((*i)->type)
-                               append(arg_types, ",", (*i)->type->name);
-                       else
-                               has_signature = false;
-               }
-
-               if(has_signature)
-               {
-                       map<string, FunctionDeclaration *>::iterator i = stage->functions.find(format("%s(%s)", call.name, arg_types));
-                       declaration = (i!=stage->functions.end() ? i->second : 0);
-               }
-       }
-
-       r_any_resolved |= (declaration!=call.declaration);
-       call.declaration = declaration;
-
-       TraversingVisitor::visit(call);
-}
-
-void FunctionResolver::visit(FunctionDeclaration &func)
-{
-       if(func.signature.empty())
-       {
-               string param_types;
-               for(NodeArray<VariableDeclaration>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
-               {
-                       if((*i)->type_declaration)
-                               append(param_types, ",", (*i)->type_declaration->name);
-                       else
-                               return;
-               }
-               func.signature = format("(%s)", param_types);
-               r_any_resolved = true;
-       }
-
-       string key = func.name+func.signature;
-       FunctionDeclaration *&stage_decl = stage->functions[key];
-       vector<FunctionDeclaration *> &decls = declarations[key];
-       if(func.definition==&func)
-       {
-               if(stage_decl && stage_decl->definition)
-               {
-                       if(!func.overrd)
-                               stage->diagnostics.push_back(Diagnostic(Diagnostic::WARN, func.source, func.line,
-                                       format("Overriding function '%s' without the override keyword is deprecated", key)));
-                       if(!stage_decl->definition->virtua)
-                               stage->diagnostics.push_back(Diagnostic(Diagnostic::WARN, func.source, func.line,
-                                       format("Overriding function '%s' not declared as virtual is deprecated", key)));
-               }
-               stage_decl = &func;
-
-               // Set all previous declarations to use this definition.
-               for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
-               {
-                       r_any_resolved |= (func.definition!=(*i)->definition);
-                       (*i)->definition = func.definition;
-                       (*i)->body.body.clear();
-               }
-       }
-       else
-       {
-               FunctionDeclaration *definition = (stage_decl ? stage_decl->definition : 0);
-               r_any_resolved |= (definition!=func.definition);
-               func.definition = definition;
-
-               if(!stage_decl)
-                       stage_decl = &func;
-       }
-       decls.push_back(&func);
-
-       TraversingVisitor::visit(func);
-}
-
-
 InterfaceGenerator::InterfaceGenerator():
        stage(0),
        function_scope(false),
index 58bc4ca202e6bdbf60f5d625c1f5b5ba2004ef14..ef8ca10e35b198b5da4a38abc09708b08455f267 100644 (file)
@@ -28,158 +28,6 @@ private:
        virtual void visit(VariableDeclaration &);
 };
 
-/** Forms links between nested blocks in the syntax tree. */
-class BlockHierarchyResolver: private TraversingVisitor
-{
-private:
-       bool r_any_resolved;
-
-public:
-       BlockHierarchyResolver(): r_any_resolved(false) { }
-
-       bool apply(Stage &s) { r_any_resolved = false; s.content.visit(*this); return r_any_resolved; }
-
-private:
-       virtual void enter(Block &);
-};
-
-/** Resolves types of variables and base types of other types. */
-class TypeResolver: private TraversingVisitor
-{
-private:
-       Stage *stage;
-       std::map<TypeDeclaration *, TypeDeclaration *> alias_map;
-       std::map<TypeDeclaration *, TypeDeclaration *> array_types;
-       NodeList<Statement>::iterator type_insert_point;
-       InterfaceBlock *iface_block;
-       bool r_any_resolved;
-
-public:
-       TypeResolver();
-
-       bool apply(Stage &);
-
-private:
-       TypeDeclaration *get_or_create_array_type(TypeDeclaration &);
-       void resolve_type(TypeDeclaration *&, const std::string &, bool);
-       virtual void visit(Block &);
-       virtual void visit(BasicTypeDeclaration &);
-       virtual void visit(ImageTypeDeclaration &);
-       virtual void visit(StructDeclaration &);
-       virtual void visit(VariableDeclaration &);
-       virtual void visit(InterfaceBlock &);
-       virtual void visit(FunctionDeclaration &);
-};
-
-/** Resolves variable references.  Variable references which match the name
-of an interface block are turned into interface block references. */
-class VariableResolver: private TraversingVisitor
-{
-private:
-       Stage *stage;
-       RefPtr<Expression> r_replacement_expr;
-       bool r_any_resolved;
-       bool record_target;
-       bool r_self_referencing;
-       Assignment::Target r_assignment_target;
-       std::vector<VariableDeclaration *> redeclared_builtins;
-       std::set<Node *> nodes_to_remove;
-
-public:
-       VariableResolver();
-
-       bool apply(Stage &);
-
-private:
-       virtual void enter(Block &);
-       virtual void visit(RefPtr<Expression> &);
-       void check_assignment_target(Statement *);
-       virtual void visit(VariableReference &);
-       virtual void visit(InterfaceBlockReference &);
-       void add_to_chain(Assignment::Target::ChainType, unsigned);
-       virtual void visit(MemberAccess &);
-       virtual void visit(Swizzle &);
-       virtual void visit(BinaryExpression &);
-       virtual void visit(Assignment &);
-       void merge_layouts(Layout &, const Layout &);
-       virtual void visit(VariableDeclaration &);
-       virtual void visit(InterfaceBlock &);
-};
-
-/** Resolves types and lvalueness of expressions. */
-class ExpressionResolver: private TraversingVisitor
-{
-private:
-       enum Compatibility
-       {
-               NOT_COMPATIBLE,
-               LEFT_CONVERTIBLE,
-               RIGHT_CONVERTIBLE,
-               SAME_TYPE
-       };
-
-       struct ArgumentInfo
-       {
-               BasicTypeDeclaration *type;
-               unsigned component_count;
-       };
-
-       Stage *stage;
-       std::vector<BasicTypeDeclaration *> basic_types;
-       NodeList<Statement>::iterator insert_point;
-       bool r_any_resolved;
-
-public:
-       ExpressionResolver();
-
-       bool apply(Stage &);
-
-private:
-       static bool is_scalar(BasicTypeDeclaration &);
-       static bool is_vector_or_matrix(BasicTypeDeclaration &);
-       static BasicTypeDeclaration *get_element_type(BasicTypeDeclaration &);
-       static bool can_convert(BasicTypeDeclaration &, BasicTypeDeclaration &);
-       static Compatibility get_compatibility(BasicTypeDeclaration &, BasicTypeDeclaration &);
-       BasicTypeDeclaration *find_type(BasicTypeDeclaration::Kind, unsigned);
-       BasicTypeDeclaration *find_type(BasicTypeDeclaration &, BasicTypeDeclaration::Kind, unsigned);
-       void convert_to(RefPtr<Expression> &, BasicTypeDeclaration &);
-       bool convert_to_element(RefPtr<Expression> &, BasicTypeDeclaration &);
-       bool truncate_vector(RefPtr<Expression> &, unsigned);
-       void resolve(Expression &, TypeDeclaration *, bool);
-
-       virtual void visit(Block &);
-       virtual void visit(Literal &);
-       virtual void visit(VariableReference &);
-       virtual void visit(InterfaceBlockReference &);
-       virtual void visit(MemberAccess &);
-       virtual void visit(Swizzle &);
-       virtual void visit(UnaryExpression &);
-       void visit(BinaryExpression &, bool);
-       virtual void visit(BinaryExpression &);
-       virtual void visit(Assignment &);
-       virtual void visit(TernaryExpression &);
-       void visit_constructor(FunctionCall &);
-       virtual void visit(FunctionCall &);
-       virtual void visit(BasicTypeDeclaration &);
-       virtual void visit(VariableDeclaration &);
-};
-
-/** Resolves function declarations and calls. */
-class FunctionResolver: private TraversingVisitor
-{
-private:
-       Stage *stage;
-       std::map<std::string, std::vector<FunctionDeclaration *> > declarations;
-       bool r_any_resolved;
-
-public:
-       bool apply(Stage &);
-
-private:
-       virtual void visit(FunctionCall &);
-       virtual void visit(FunctionDeclaration &);
-};
-
 /** Materializes implicitly declared interfaces.
 
 Out variable declarations inside functions are moved to the global scope.
diff --git a/source/glsl/resolve.cpp b/source/glsl/resolve.cpp
new file mode 100644 (file)
index 0000000..05aead3
--- /dev/null
@@ -0,0 +1,1208 @@
+#include <algorithm>
+#include <msp/core/raii.h>
+#include <msp/strings/utils.h>
+#include "resolve.h"
+
+using namespace std;
+
+namespace Msp {
+namespace GL {
+namespace SL {
+
+void BlockHierarchyResolver::enter(Block &block)
+{
+       r_any_resolved |= (current_block!=block.parent);
+       block.parent = current_block;
+}
+
+
+TypeResolver::TypeResolver():
+       stage(0),
+       iface_block(0),
+       r_any_resolved(false)
+{ }
+
+bool TypeResolver::apply(Stage &s)
+{
+       stage = &s;
+       s.types.clear();
+       r_any_resolved = false;
+       s.content.visit(*this);
+       return r_any_resolved;
+}
+
+TypeDeclaration *TypeResolver::get_or_create_array_type(TypeDeclaration &type)
+{
+       map<TypeDeclaration *, TypeDeclaration *>::iterator i = array_types.find(&type);
+       if(i!=array_types.end())
+               return i->second;
+
+       BasicTypeDeclaration *array = new BasicTypeDeclaration;
+       array->source = INTERNAL_SOURCE;
+       array->name = type.name+"[]";
+       array->kind = BasicTypeDeclaration::ARRAY;
+       array->base = type.name;
+       array->base_type = &type;
+       stage->content.body.insert(type_insert_point, array);
+       array_types[&type] = array;
+       return array;
+}
+
+void TypeResolver::resolve_type(TypeDeclaration *&type, const string &name, bool array)
+{
+       TypeDeclaration *resolved = 0;
+       map<string, TypeDeclaration *>::iterator i = stage->types.find(name);
+       if(i!=stage->types.end())
+       {
+               map<TypeDeclaration *, TypeDeclaration *>::iterator j = alias_map.find(i->second);
+               resolved = (j!=alias_map.end() ? j->second : i->second);
+       }
+
+       if(resolved && array)
+               resolved = get_or_create_array_type(*resolved);
+
+       r_any_resolved |= (resolved!=type);
+       type=resolved;
+}
+
+void TypeResolver::visit(Block &block)
+{
+       for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
+       {
+               if(!block.parent)
+                       type_insert_point = i;
+               (*i)->visit(*this);
+       }
+}
+
+void TypeResolver::visit(BasicTypeDeclaration &type)
+{
+       resolve_type(type.base_type, type.base, false);
+
+       if(type.kind==BasicTypeDeclaration::VECTOR && type.base_type)
+               if(BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type))
+                       if(basic_base->kind==BasicTypeDeclaration::VECTOR)
+                       {
+                               type.kind = BasicTypeDeclaration::MATRIX;
+                               /* A matrix's base type is its column vector type.  This will put
+                               the column vector's size, i.e. the matrix's row count, in the high
+                               half of the size. */
+                               type.size |= basic_base->size<<16;
+                       }
+
+       if(type.kind==BasicTypeDeclaration::ALIAS && type.base_type)
+               alias_map[&type] = type.base_type;
+       else if(type.kind==BasicTypeDeclaration::ARRAY && type.base_type)
+               array_types[type.base_type] = &type;
+
+       stage->types.insert(make_pair(type.name, &type));
+}
+
+void TypeResolver::visit(ImageTypeDeclaration &type)
+{
+       resolve_type(type.base_type, type.base, false);
+       stage->types.insert(make_pair(type.name, &type));
+}
+
+void TypeResolver::visit(StructDeclaration &strct)
+{
+       stage->types.insert(make_pair(strct.name, &strct));
+       TraversingVisitor::visit(strct);
+}
+
+void TypeResolver::visit(VariableDeclaration &var)
+{
+       resolve_type(var.type_declaration, var.type, var.array);
+       if(iface_block && var.interface==iface_block->interface)
+               var.interface.clear();
+}
+
+void TypeResolver::visit(InterfaceBlock &iface)
+{
+       if(iface.members)
+       {
+               SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
+               iface.members->visit(*this);
+
+               StructDeclaration *strct = new StructDeclaration;
+               strct->source = INTERNAL_SOURCE;
+               strct->name = format("_%s_%s", iface.interface, iface.block_name);
+               strct->members.body.splice(strct->members.body.begin(), iface.members->body);
+               stage->content.body.insert(type_insert_point, strct);
+               stage->types.insert(make_pair(strct->name, strct));
+
+               iface.members = 0;
+               strct->interface_block = &iface;
+               iface.struct_declaration = strct;
+       }
+
+       TypeDeclaration *type = iface.struct_declaration;
+       if(type && iface.array)
+               type = get_or_create_array_type(*type);
+       r_any_resolved = (type!=iface.type_declaration);
+       iface.type_declaration = type;
+}
+
+void TypeResolver::visit(FunctionDeclaration &func)
+{
+       resolve_type(func.return_type_declaration, func.return_type, false);
+       TraversingVisitor::visit(func);
+}
+
+
+VariableResolver::VariableResolver():
+       stage(0),
+       r_any_resolved(false),
+       record_target(false),
+       r_self_referencing(false)
+{ }
+
+bool VariableResolver::apply(Stage &s)
+{
+       stage = &s;
+       s.interface_blocks.clear();
+       r_any_resolved = false;
+       s.content.visit(*this);
+       for(vector<VariableDeclaration *>::const_iterator i=redeclared_builtins.begin(); i!=redeclared_builtins.end(); ++i)
+               (*i)->source = GENERATED_SOURCE;
+       NodeRemover().apply(s, nodes_to_remove);
+       return r_any_resolved;
+}
+
+void VariableResolver::enter(Block &block)
+{
+       block.variables.clear();
+}
+
+void VariableResolver::visit(RefPtr<Expression> &expr)
+{
+       r_replacement_expr = 0;
+       expr->visit(*this);
+       if(r_replacement_expr)
+       {
+               expr = r_replacement_expr;
+               /* Don't record assignment target when doing a replacement, because chain
+               information won't be correct. */
+               r_assignment_target.declaration = 0;
+               r_any_resolved = true;
+       }
+       r_replacement_expr = 0;
+}
+
+void VariableResolver::check_assignment_target(Statement *declaration)
+{
+       if(record_target)
+       {
+               if(r_assignment_target.declaration)
+               {
+                       /* More than one reference found in assignment target.  Unable to
+                       determine what the primary target is. */
+                       record_target = false;
+                       r_assignment_target.declaration = 0;
+               }
+               else
+                       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::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.block_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_nocopy(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)
+       {
+               // 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=='/')
+       {
+               // 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)
+                               type = find_type(*elem_result, BasicTypeDeclaration::VECTOR, basic_right->size&0xFFFF);
+                       else if(basic_right->kind==BasicTypeDeclaration::VECTOR)
+                               type = find_type(*elem_result, BasicTypeDeclaration::VECTOR, basic_left->size>>16);
+                       else
+                               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))
+                               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))
+                               type = (elem_compat==LEFT_CONVERTIBLE ? find_type(*elem_right, basic_left->kind, basic_left->size) : basic_left);
+                       else
+                               return;
+               }
+               else if(compat==LEFT_CONVERTIBLE)
+                       type = basic_right;
+               else
+                       type = basic_left;
+       }
+       else
+               return;
+
+       if(assign && type!=basic_left)
+               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)
+               type = 0;
+
+       resolve(binary, type, assign);
+}
+
+void ExpressionResolver::visit(BinaryExpression &binary)
+{
+       TraversingVisitor::visit(binary);
+       visit(binary, false);
+}
+
+void ExpressionResolver::visit(Assignment &assign)
+{
+       TraversingVisitor::visit(assign);
+
+       if(assign.oper->token[0]!='=')
+               return visit(assign, true);
+       else if(assign.left->type!=assign.right->type)
+       {
+               BasicTypeDeclaration *basic_left = dynamic_cast<BasicTypeDeclaration *>(assign.left->type);
+               BasicTypeDeclaration *basic_right = dynamic_cast<BasicTypeDeclaration *>(assign.right->type);
+               if(!basic_left || !basic_right)
+                       return;
+
+               Compatibility compat = get_compatibility(*basic_left, *basic_right);
+               if(compat==RIGHT_CONVERTIBLE)
+                       convert_to(assign.right, *basic_left);
+               else if(compat!=SAME_TYPE)
+                       return;
+       }
+
+       resolve(assign, assign.left->type, true);
+}
+
+void ExpressionResolver::visit(TernaryExpression &ternary)
+{
+       TraversingVisitor::visit(ternary);
+
+       BasicTypeDeclaration *basic_cond = dynamic_cast<BasicTypeDeclaration *>(ternary.condition->type);
+       if(!basic_cond || basic_cond->kind!=BasicTypeDeclaration::BOOL)
+               return;
+
+       TypeDeclaration *type = 0;
+       if(ternary.true_expr->type==ternary.false_expr->type)
+               type = ternary.true_expr->type;
+       else
+       {
+               BasicTypeDeclaration *basic_true = dynamic_cast<BasicTypeDeclaration *>(ternary.true_expr->type);
+               BasicTypeDeclaration *basic_false = dynamic_cast<BasicTypeDeclaration *>(ternary.false_expr->type);
+               if(!basic_true || !basic_false)
+                       return;
+
+               Compatibility compat = get_compatibility(*basic_true, *basic_false);
+               if(compat==NOT_COMPATIBLE)
+                       return;
+
+               type = (compat==LEFT_CONVERTIBLE ? basic_true : basic_false);
+
+               if(compat==LEFT_CONVERTIBLE)
+                       convert_to(ternary.true_expr, *basic_false);
+               else if(compat==RIGHT_CONVERTIBLE)
+                       convert_to(ternary.false_expr, *basic_true);
+       }
+
+       resolve(ternary, type, false);
+}
+
+void ExpressionResolver::visit_constructor(FunctionCall &call)
+{
+       if(call.arguments.empty())
+               return;
+
+       map<string, TypeDeclaration *>::const_iterator i = stage->types.find(call.name);
+       if(i==stage->types.end())
+               return;
+       else if(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(i->second))
+       {
+               BasicTypeDeclaration *elem = get_element_type(*basic);
+               if(!elem)
+                       return;
+
+               vector<ArgumentInfo> args;
+               args.reserve(call.arguments.size());
+               unsigned arg_component_total = 0;
+               bool has_matrices = false;
+               for(NodeArray<Expression>::const_iterator j=call.arguments.begin(); j!=call.arguments.end(); ++j)
+               {
+                       ArgumentInfo info;
+                       if(!(info.type=dynamic_cast<BasicTypeDeclaration *>((*j)->type)))
+                               return;
+                       if(is_scalar(*info.type) || info.type->kind==BasicTypeDeclaration::BOOL)
+                               info.component_count = 1;
+                       else if(info.type->kind==BasicTypeDeclaration::VECTOR)
+                               info.component_count = info.type->size;
+                       else if(info.type->kind==BasicTypeDeclaration::MATRIX)
+                       {
+                               info.component_count = (info.type->size>>16)*(info.type->size&0xFFFF);
+                               has_matrices = true;
+                       }
+                       else
+                               return;
+                       arg_component_total += info.component_count;
+                       args.push_back(info);
+               }
+
+               bool convert_args = false;
+               if((is_scalar(*basic) || basic->kind==BasicTypeDeclaration::BOOL) && call.arguments.size()==1 && !has_matrices)
+               {
+                       if(arg_component_total>1)
+                               truncate_vector(call.arguments.front(), 1);
+
+                       /* Single-element type constructors never need to convert their
+                       arguments because the constructor *is* the conversion. */
+               }
+               else if(basic->kind==BasicTypeDeclaration::VECTOR && !has_matrices)
+               {
+                       /* Vector constructors need either a single scalar argument or
+                       enough components to fill out the vector. */
+                       if(arg_component_total!=1 && arg_component_total<basic->size)
+                               return;
+
+                       /* A vector of same size can be converted directly.  For other
+                       combinations the individual arguments need to be converted. */
+                       if(call.arguments.size()==1)
+                       {
+                               if(arg_component_total==1)
+                                       convert_args = true;
+                               else if(arg_component_total>basic->size)
+                                       truncate_vector(call.arguments.front(), basic->size);
+                       }
+                       else if(arg_component_total==basic->size)
+                               convert_args = true;
+                       else
+                               return;
+               }
+               else if(basic->kind==BasicTypeDeclaration::MATRIX)
+               {
+                       unsigned column_count = basic->size&0xFFFF;
+                       unsigned row_count = basic->size>>16;
+                       if(call.arguments.size()==1)
+                       {
+                               /* A matrix can be constructed from a single element or another
+                               matrix of sufficient size. */
+                               if(arg_component_total==1)
+                                       convert_args = true;
+                               else if(args.front().type->kind==BasicTypeDeclaration::MATRIX)
+                               {
+                                       unsigned arg_columns = args.front().type->size&0xFFFF;
+                                       unsigned arg_rows = args.front().type->size>>16;
+                                       if(arg_columns<column_count || arg_rows<row_count)
+                                               return;
+
+                                       /* Always generate a temporary here and let the optimization
+                                       stage inline it if that's reasonable. */
+                                       RefPtr<VariableDeclaration> temporary = new VariableDeclaration;
+                                       temporary->type = args.front().type->name;
+                                       temporary->name = get_unused_variable_name(*current_block, "_temp");
+                                       temporary->init_expression = call.arguments.front();
+                                       current_block->body.insert(insert_point, temporary);
+
+                                       // Create expressions to build each column.
+                                       vector<RefPtr<Expression> > columns;
+                                       columns.reserve(column_count);
+                                       for(unsigned j=0; j<column_count; ++j)
+                                       {
+                                               RefPtr<VariableReference> ref = new VariableReference;
+                                               ref->name = temporary->name;
+
+                                               RefPtr<Literal> index = new Literal;
+                                               index->token = lexical_cast<string>(j);
+                                               index->value = static_cast<int>(j);
+
+                                               RefPtr<BinaryExpression> subscript = new BinaryExpression;
+                                               subscript->left = ref;
+                                               subscript->oper = &Operator::get_operator("[", Operator::BINARY);
+                                               subscript->right = index;
+                                               subscript->type = args.front().type->base_type;
+
+                                               columns.push_back(subscript);
+                                               if(arg_rows>row_count)
+                                                       truncate_vector(columns.back(), row_count);
+                                       }
+
+                                       call.arguments.resize(column_count);
+                                       copy(columns.begin(), columns.end(), call.arguments.begin());
+
+                                       /* Let VariableResolver process the new nodes and finish
+                                       resolving the constructor on the next pass. */
+                                       r_any_resolved = true;
+                                       return;
+                               }
+                               else
+                                       return;
+                       }
+                       else if(arg_component_total==column_count*row_count && !has_matrices)
+                       {
+                               /* Construct a matrix from individual components in column-major
+                               order.  Arguments must align at column boundaries. */
+                               vector<RefPtr<Expression> > columns;
+                               columns.reserve(column_count);
+
+                               vector<RefPtr<Expression> > column_args;
+                               column_args.reserve(row_count);
+                               unsigned column_component_count = 0;
+
+                               for(unsigned j=0; j<call.arguments.size(); ++j)
+                               {
+                                       const ArgumentInfo &info = args[j];
+                                       if(!column_component_count && info.type->kind==BasicTypeDeclaration::VECTOR && info.component_count==row_count)
+                                               // A vector filling the entire column can be used as is.
+                                               columns.push_back(call.arguments[j]);
+                                       else
+                                       {
+                                               column_args.push_back(call.arguments[j]);
+                                               column_component_count += info.component_count;
+                                               if(column_component_count==row_count)
+                                               {
+                                                       /* The column has filled up.  Create a vector constructor
+                                                       for it.*/
+                                                       RefPtr<FunctionCall> column_call = new FunctionCall;
+                                                       column_call->name = basic->base_type->name;
+                                                       column_call->constructor = true;
+                                                       column_call->arguments.resize(column_args.size());
+                                                       copy(column_args.begin(), column_args.end(), column_call->arguments.begin());
+                                                       column_call->type = basic->base_type;
+                                                       visit_constructor(*column_call);
+                                                       columns.push_back(column_call);
+
+                                                       column_args.clear();
+                                                       column_component_count = 0;
+                                               }
+                                               else if(column_component_count>row_count)
+                                                       // Argument alignment mismatch.
+                                                       return;
+                                       }
+                               }
+                       }
+                       else
+                               return;
+               }
+               else
+                       return;
+
+               if(convert_args)
+               {
+                       // The argument list may have changed so can't rely on args.
+                       for(NodeArray<Expression>::iterator j=call.arguments.begin(); j!=call.arguments.end(); ++j)
+                               if(BasicTypeDeclaration *basic_arg = dynamic_cast<BasicTypeDeclaration *>((*j)->type))
+                               {
+                                       BasicTypeDeclaration *elem_arg = get_element_type(*basic_arg);
+                                       if(elem_arg!=elem)
+                                               convert_to_element(*j, *elem);
+                               }
+               }
+       }
+       else if(StructDeclaration *strct = dynamic_cast<StructDeclaration *>(i->second))
+       {
+               if(call.arguments.size()!=strct->members.body.size())
+                       return;
+
+               unsigned k = 0;
+               for(NodeList<Statement>::const_iterator j=strct->members.body.begin(); j!=strct->members.body.end(); ++j, ++k)
+               {
+                       if(VariableDeclaration *var = dynamic_cast<VariableDeclaration *>(j->get()))
+                       {
+                               if(!call.arguments[k]->type || call.arguments[k]->type!=var->type_declaration)
+                                       return;
+                       }
+                       else
+                               return;
+               }
+       }
+
+       resolve(call, i->second, false);
+}
+
+void ExpressionResolver::visit(FunctionCall &call)
+{
+       TraversingVisitor::visit(call);
+
+       if(call.declaration)
+               resolve(call, call.declaration->return_type_declaration, false);
+       else if(call.constructor)
+               visit_constructor(call);
+}
+
+void ExpressionResolver::visit(BasicTypeDeclaration &type)
+{
+       basic_types.push_back(&type);
+}
+
+void ExpressionResolver::visit(VariableDeclaration &var)
+{
+       TraversingVisitor::visit(var);
+       if(!var.init_expression)
+               return;
+
+       BasicTypeDeclaration *var_basic = dynamic_cast<BasicTypeDeclaration *>(var.type_declaration);
+       BasicTypeDeclaration *init_basic = dynamic_cast<BasicTypeDeclaration *>(var.init_expression->type);
+       if(!var_basic || !init_basic)
+               return;
+
+       Compatibility compat = get_compatibility(*var_basic, *init_basic);
+       if(compat==RIGHT_CONVERTIBLE)
+               convert_to(var.init_expression, *var_basic);
+}
+
+
+bool FunctionResolver::apply(Stage &s)
+{
+       stage = &s;
+       s.functions.clear();
+       r_any_resolved = false;
+       s.content.visit(*this);
+       return r_any_resolved;
+}
+
+void FunctionResolver::visit(FunctionCall &call)
+{
+       FunctionDeclaration *declaration = 0;
+       if(stage->types.count(call.name))
+               call.constructor = true;
+       else
+       {
+               string arg_types;
+               bool has_signature = true;
+               for(NodeArray<Expression>::const_iterator i=call.arguments.begin(); (has_signature && i!=call.arguments.end()); ++i)
+               {
+                       if((*i)->type)
+                               append(arg_types, ",", (*i)->type->name);
+                       else
+                               has_signature = false;
+               }
+
+               if(has_signature)
+               {
+                       map<string, FunctionDeclaration *>::iterator i = stage->functions.find(format("%s(%s)", call.name, arg_types));
+                       declaration = (i!=stage->functions.end() ? i->second : 0);
+               }
+       }
+
+       r_any_resolved |= (declaration!=call.declaration);
+       call.declaration = declaration;
+
+       TraversingVisitor::visit(call);
+}
+
+void FunctionResolver::visit(FunctionDeclaration &func)
+{
+       if(func.signature.empty())
+       {
+               string param_types;
+               for(NodeArray<VariableDeclaration>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
+               {
+                       if((*i)->type_declaration)
+                               append(param_types, ",", (*i)->type_declaration->name);
+                       else
+                               return;
+               }
+               func.signature = format("(%s)", param_types);
+               r_any_resolved = true;
+       }
+
+       string key = func.name+func.signature;
+       FunctionDeclaration *&stage_decl = stage->functions[key];
+       vector<FunctionDeclaration *> &decls = declarations[key];
+       if(func.definition==&func)
+       {
+               if(stage_decl && stage_decl->definition)
+               {
+                       if(!func.overrd)
+                               stage->diagnostics.push_back(Diagnostic(Diagnostic::WARN, func.source, func.line,
+                                       format("Overriding function '%s' without the override keyword is deprecated", key)));
+                       if(!stage_decl->definition->virtua)
+                               stage->diagnostics.push_back(Diagnostic(Diagnostic::WARN, func.source, func.line,
+                                       format("Overriding function '%s' not declared as virtual is deprecated", key)));
+               }
+               stage_decl = &func;
+
+               // Set all previous declarations to use this definition.
+               for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
+               {
+                       r_any_resolved |= (func.definition!=(*i)->definition);
+                       (*i)->definition = func.definition;
+                       (*i)->body.body.clear();
+               }
+       }
+       else
+       {
+               FunctionDeclaration *definition = (stage_decl ? stage_decl->definition : 0);
+               r_any_resolved |= (definition!=func.definition);
+               func.definition = definition;
+
+               if(!stage_decl)
+                       stage_decl = &func;
+       }
+       decls.push_back(&func);
+
+       TraversingVisitor::visit(func);
+}
+
+} // namespace SL
+} // namespace GL
+} // namespace Msp
diff --git a/source/glsl/resolve.h b/source/glsl/resolve.h
new file mode 100644 (file)
index 0000000..ca9c4f3
--- /dev/null
@@ -0,0 +1,170 @@
+#ifndef MSP_GL_SL_RESOLVE_H_
+#define MSP_GL_SL_RESOLVE_H_
+
+#include <map>
+#include <set>
+#include <string>
+#include <vector>
+#include "visitor.h"
+
+namespace Msp {
+namespace GL {
+namespace SL {
+
+/** Forms links between nested blocks in the syntax tree. */
+class BlockHierarchyResolver: private TraversingVisitor
+{
+private:
+       bool r_any_resolved;
+
+public:
+       BlockHierarchyResolver(): r_any_resolved(false) { }
+
+       bool apply(Stage &s) { r_any_resolved = false; s.content.visit(*this); return r_any_resolved; }
+
+private:
+       virtual void enter(Block &);
+};
+
+/** Resolves types of variables and base types of other types. */
+class TypeResolver: private TraversingVisitor
+{
+private:
+       Stage *stage;
+       std::map<TypeDeclaration *, TypeDeclaration *> alias_map;
+       std::map<TypeDeclaration *, TypeDeclaration *> array_types;
+       NodeList<Statement>::iterator type_insert_point;
+       InterfaceBlock *iface_block;
+       bool r_any_resolved;
+
+public:
+       TypeResolver();
+
+       bool apply(Stage &);
+
+private:
+       TypeDeclaration *get_or_create_array_type(TypeDeclaration &);
+       void resolve_type(TypeDeclaration *&, const std::string &, bool);
+       virtual void visit(Block &);
+       virtual void visit(BasicTypeDeclaration &);
+       virtual void visit(ImageTypeDeclaration &);
+       virtual void visit(StructDeclaration &);
+       virtual void visit(VariableDeclaration &);
+       virtual void visit(InterfaceBlock &);
+       virtual void visit(FunctionDeclaration &);
+};
+
+/** Resolves variable references.  Variable references which match the name
+of an interface block are turned into interface block references. */
+class VariableResolver: private TraversingVisitor
+{
+private:
+       Stage *stage;
+       RefPtr<Expression> r_replacement_expr;
+       bool r_any_resolved;
+       bool record_target;
+       bool r_self_referencing;
+       Assignment::Target r_assignment_target;
+       std::vector<VariableDeclaration *> redeclared_builtins;
+       std::set<Node *> nodes_to_remove;
+
+public:
+       VariableResolver();
+
+       bool apply(Stage &);
+
+private:
+       virtual void enter(Block &);
+       virtual void visit(RefPtr<Expression> &);
+       void check_assignment_target(Statement *);
+       virtual void visit(VariableReference &);
+       virtual void visit(InterfaceBlockReference &);
+       void add_to_chain(Assignment::Target::ChainType, unsigned);
+       virtual void visit(MemberAccess &);
+       virtual void visit(Swizzle &);
+       virtual void visit(BinaryExpression &);
+       virtual void visit(Assignment &);
+       void merge_layouts(Layout &, const Layout &);
+       virtual void visit(VariableDeclaration &);
+       virtual void visit(InterfaceBlock &);
+};
+
+/** Resolves types and lvalueness of expressions. */
+class ExpressionResolver: private TraversingVisitor
+{
+private:
+       enum Compatibility
+       {
+               NOT_COMPATIBLE,
+               LEFT_CONVERTIBLE,
+               RIGHT_CONVERTIBLE,
+               SAME_TYPE
+       };
+
+       struct ArgumentInfo
+       {
+               BasicTypeDeclaration *type;
+               unsigned component_count;
+       };
+
+       Stage *stage;
+       std::vector<BasicTypeDeclaration *> basic_types;
+       NodeList<Statement>::iterator insert_point;
+       bool r_any_resolved;
+
+public:
+       ExpressionResolver();
+
+       bool apply(Stage &);
+
+private:
+       static bool is_scalar(BasicTypeDeclaration &);
+       static bool is_vector_or_matrix(BasicTypeDeclaration &);
+       static BasicTypeDeclaration *get_element_type(BasicTypeDeclaration &);
+       static bool can_convert(BasicTypeDeclaration &, BasicTypeDeclaration &);
+       static Compatibility get_compatibility(BasicTypeDeclaration &, BasicTypeDeclaration &);
+       BasicTypeDeclaration *find_type(BasicTypeDeclaration::Kind, unsigned);
+       BasicTypeDeclaration *find_type(BasicTypeDeclaration &, BasicTypeDeclaration::Kind, unsigned);
+       void convert_to(RefPtr<Expression> &, BasicTypeDeclaration &);
+       bool convert_to_element(RefPtr<Expression> &, BasicTypeDeclaration &);
+       bool truncate_vector(RefPtr<Expression> &, unsigned);
+       void resolve(Expression &, TypeDeclaration *, bool);
+
+       virtual void visit(Block &);
+       virtual void visit(Literal &);
+       virtual void visit(VariableReference &);
+       virtual void visit(InterfaceBlockReference &);
+       virtual void visit(MemberAccess &);
+       virtual void visit(Swizzle &);
+       virtual void visit(UnaryExpression &);
+       void visit(BinaryExpression &, bool);
+       virtual void visit(BinaryExpression &);
+       virtual void visit(Assignment &);
+       virtual void visit(TernaryExpression &);
+       void visit_constructor(FunctionCall &);
+       virtual void visit(FunctionCall &);
+       virtual void visit(BasicTypeDeclaration &);
+       virtual void visit(VariableDeclaration &);
+};
+
+/** Resolves function declarations and calls. */
+class FunctionResolver: private TraversingVisitor
+{
+private:
+       Stage *stage;
+       std::map<std::string, std::vector<FunctionDeclaration *> > declarations;
+       bool r_any_resolved;
+
+public:
+       bool apply(Stage &);
+
+private:
+       virtual void visit(FunctionCall &);
+       virtual void visit(FunctionDeclaration &);
+};
+
+} // namespace SL
+} // namespace GL
+} // namespace Msp
+
+#endif