]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/resolve.cpp
Use standard fixed-size integer types
[libs/gl.git] / source / glsl / resolve.cpp
index 05aead3c5fa369f55529c02c05c34fe7a6997dba..96ca492123962a464e77b4de57f9427f6196bdd0 100644 (file)
@@ -1,6 +1,7 @@
-#include <algorithm>
+#include <msp/core/algorithm.h>
 #include <msp/core/raii.h>
 #include <msp/strings/utils.h>
+#include "reflect.h"
 #include "resolve.h"
 
 using namespace std;
@@ -33,7 +34,7 @@ bool TypeResolver::apply(Stage &s)
 
 TypeDeclaration *TypeResolver::get_or_create_array_type(TypeDeclaration &type)
 {
-       map<TypeDeclaration *, TypeDeclaration *>::iterator i = array_types.find(&type);
+       auto i = array_types.find(&type);
        if(i!=array_types.end())
                return i->second;
 
@@ -51,10 +52,10 @@ TypeDeclaration *TypeResolver::get_or_create_array_type(TypeDeclaration &type)
 void TypeResolver::resolve_type(TypeDeclaration *&type, const string &name, bool array)
 {
        TypeDeclaration *resolved = 0;
-       map<string, TypeDeclaration *>::iterator i = stage->types.find(name);
+       auto i = stage->types.find(name);
        if(i!=stage->types.end())
        {
-               map<TypeDeclaration *, TypeDeclaration *>::iterator j = alias_map.find(i->second);
+               auto j = alias_map.find(i->second);
                resolved = (j!=alias_map.end() ? j->second : i->second);
        }
 
@@ -67,7 +68,7 @@ void TypeResolver::resolve_type(TypeDeclaration *&type, const string &name, bool
 
 void TypeResolver::visit(Block &block)
 {
-       for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
+       for(auto i=block.body.begin(); i!=block.body.end(); ++i)
        {
                if(!block.parent)
                        type_insert_point = i;
@@ -163,8 +164,8 @@ bool VariableResolver::apply(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;
+       for(VariableDeclaration *v: redeclared_builtins)
+               v->source = GENERATED_SOURCE;
        NodeRemover().apply(s, nodes_to_remove);
        return r_any_resolved;
 }
@@ -217,7 +218,7 @@ void VariableResolver::visit(VariableReference &var)
        one. */
        for(Block *block=current_block; (!declaration && block); block=block->parent)
        {
-               map<string, VariableDeclaration *>::iterator i = block->variables.find(var.name);
+               auto i = block->variables.find(var.name);
                if(i!=block->variables.end())
                        declaration = i->second;
        }
@@ -225,29 +226,43 @@ void VariableResolver::visit(VariableReference &var)
        if(!declaration)
        {
                const map<string, InterfaceBlock *> &blocks = stage->interface_blocks;
-               map<string, InterfaceBlock *>::const_iterator i = blocks.find("_"+var.name);
+               auto i = blocks.find(var.name);
+               if(i==blocks.end())
+               {
+                       // Look for the variable in anonymous interface blocks.
+                       for(i=blocks.begin(); i!=blocks.end(); ++i)
+                               if(i->second->instance_name.empty() && i->second->struct_declaration)
+                                       if(i->second->struct_declaration->members.variables.count(var.name))
+                                               break;
+               }
+
                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. */
+                       /* The name refers to either an interface block with an instance name
+                       or a variable declared inside an anonymous interface block.  Prepare
+                       new syntax tree nodes 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;
-                               }
+
+                       if(i->second->instance_name.empty())
+                       {
+                               iface_ref->name = format("%s %s", i->second->interface, i->second->block_name);
+
+                               MemberAccess *memacc = new MemberAccess;
+                               memacc->source = var.source;
+                               memacc->line = var.line;
+                               memacc->left = iface_ref;
+                               memacc->member = var.name;
+
+                               r_replacement_expr = memacc;
+                       }
+                       else
+                       {
+                               iface_ref->name = var.name;
+                               r_replacement_expr = iface_ref;
+                       }
                }
        }
 
@@ -259,7 +274,7 @@ void VariableResolver::visit(VariableReference &var)
 
 void VariableResolver::visit(InterfaceBlockReference &iface)
 {
-       map<string, InterfaceBlock *>::iterator i = stage->interface_blocks.find("_"+iface.name);
+       auto 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;
@@ -267,33 +282,24 @@ void VariableResolver::visit(InterfaceBlockReference &iface)
        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;
+       int index = -1;
        if(StructDeclaration *strct = dynamic_cast<StructDeclaration *>(memacc.left->type))
        {
-               map<string, VariableDeclaration *>::iterator i = strct->members.variables.find(memacc.member);
+               auto i = strct->members.variables.find(memacc.member);
                if(i!=strct->members.variables.end())
                {
                        declaration = i->second;
+                       index = 0;
+                       for(auto j=strct->members.body.begin(); (j!=strct->members.body.end() && j->get()!=i->second); ++j)
+                               ++index;
 
                        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);
-                       }
+                               add_to_chain(r_assignment_target, Assignment::Target::MEMBER, index);
                }
        }
        else if(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(memacc.left->type))
@@ -305,9 +311,9 @@ void VariableResolver::visit(MemberAccess &memacc)
                        static const char component_names[] = { 'x', 'r', 's', 'y', 'g', 't', 'z', 'b', 'p', 'w', 'a', 'q' };
 
                        bool ok = true;
-                       UInt8 components[4] = { };
+                       uint8_t 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);
+                               ok = ((components[i] = (std::find(component_names, component_names+12, memacc.member[i])-component_names)/3) < 4);
 
                        if(ok)
                        {
@@ -324,8 +330,9 @@ void VariableResolver::visit(MemberAccess &memacc)
                }
        }
 
-       r_any_resolved |= (declaration!=memacc.declaration);
+       r_any_resolved |= (declaration!=memacc.declaration || index!=memacc.index);
        memacc.declaration = declaration;
+       memacc.index = index;
 }
 
 void VariableResolver::visit(Swizzle &swizzle)
@@ -337,7 +344,7 @@ void VariableResolver::visit(Swizzle &swizzle)
                unsigned mask = 0;
                for(unsigned i=0; i<swizzle.count; ++i)
                        mask |= 1<<swizzle.components[i];
-               add_to_chain(Assignment::Target::SWIZZLE, mask);
+               add_to_chain(r_assignment_target, Assignment::Target::SWIZZLE, mask);
        }
 }
 
@@ -359,7 +366,7 @@ void VariableResolver::visit(BinaryExpression &binary)
                        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);
+                       add_to_chain(r_assignment_target, Assignment::Target::ARRAY, index);
                }
        }
        else
@@ -383,19 +390,16 @@ void VariableResolver::visit(Assignment &assign)
 
 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)
+       for(const Layout::Qualifier &q: from_layout.qualifiers)
        {
-               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);
+               auto i = find_member(to_layout.qualifiers, q.name, &Layout::Qualifier::name);
+               if(i!=to_layout.qualifiers.end())
+               {
+                       i->has_value = q.value;
+                       i->value = q.value;
+               }
+               else
+                       to_layout.qualifiers.push_back(q);
        }
 }
 
@@ -432,9 +436,9 @@ 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));
+       stage->interface_blocks.insert(make_pair(format("%s %s", iface.interface, iface.block_name), &iface));
        if(!iface.instance_name.empty())
-               stage->interface_blocks.insert(make_pair("_"+iface.instance_name, &iface));
+               stage->interface_blocks.insert(make_pair(iface.instance_name, &iface));
 
        TraversingVisitor::visit(iface);
 }
@@ -453,43 +457,6 @@ bool ExpressionResolver::apply(Stage &s)
        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)
@@ -502,20 +469,18 @@ ExpressionResolver::Compatibility ExpressionResolver::get_compatibility(BasicTyp
                return NOT_COMPATIBLE;
 }
 
-BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration::Kind kind, unsigned size)
+BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration::Kind kind, unsigned size, bool sign)
 {
-       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;
+       auto i = find_if(basic_types,
+               [kind, size, sign](const BasicTypeDeclaration *t){ return t->kind==kind && t->size==size && t->sign==sign; });
+       return (i!=basic_types.end() ? *i : 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;
+       auto i = find_if(basic_types,
+               [&elem_type, kind, size](BasicTypeDeclaration *t){ return get_element_type(*t)==&elem_type && t->kind==kind && t->size==size; });
+       return (i!=basic_types.end() ? *i : 0);
 }
 
 void ExpressionResolver::convert_to(RefPtr<Expression> &expr, BasicTypeDeclaration &type)
@@ -579,7 +544,7 @@ void ExpressionResolver::resolve(Expression &expr, TypeDeclaration *type, bool l
 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)
+       for(auto i=block.body.begin(); i!=block.body.end(); ++i)
        {
                insert_point = i;
                (*i)->visit(*this);
@@ -591,7 +556,9 @@ 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);
+               resolve(literal, find_type(BasicTypeDeclaration::INT, 32, true), false);
+       else if(literal.value.check_type<unsigned>())
+               resolve(literal, find_type(BasicTypeDeclaration::INT, 32, false), false);
        else if(literal.value.check_type<float>())
                resolve(literal, find_type(BasicTypeDeclaration::FLOAT, 32), false);
 }
@@ -739,6 +706,17 @@ void ExpressionResolver::visit(BinaryExpression &binary, bool assign)
                if(!left_size || (left_size==1 && right_size!=1) || (left_size>1 && right_size!=1 && right_size!=left_size))
                        return;
 
+               /* If the left operand is a vector and right is scalar, convert the right
+               operand to a vector too. */
+               if(left_size>1 && right_size==1)
+               {
+                       BasicTypeDeclaration *vec_right = find_type(*elem_right, basic_left->kind, basic_left->size);
+                       if(!vec_right)
+                               return;
+
+                       convert_to(binary.right, *vec_right);
+               }
+
                type = basic_left;
                // Don't perform conversion even if the operands are of different sizes.
                compat = SAME_TYPE;
@@ -874,9 +852,11 @@ void ExpressionResolver::visit_constructor(FunctionCall &call)
        if(call.arguments.empty())
                return;
 
-       map<string, TypeDeclaration *>::const_iterator i = stage->types.find(call.name);
+       auto i = stage->types.find(call.name);
        if(i==stage->types.end())
                return;
+       else if(call.arguments.size()==1 && i->second==call.arguments[0]->type)
+               ;
        else if(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(i->second))
        {
                BasicTypeDeclaration *elem = get_element_type(*basic);
@@ -887,10 +867,10 @@ void ExpressionResolver::visit_constructor(FunctionCall &call)
                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)
+               for(const RefPtr<Expression> &a: call.arguments)
                {
                        ArgumentInfo info;
-                       if(!(info.type=dynamic_cast<BasicTypeDeclaration *>((*j)->type)))
+                       if(!(info.type=dynamic_cast<BasicTypeDeclaration *>(a->type)))
                                return;
                        if(is_scalar(*info.type) || info.type->kind==BasicTypeDeclaration::BOOL)
                                info.component_count = 1;
@@ -1048,12 +1028,12 @@ void ExpressionResolver::visit_constructor(FunctionCall &call)
                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))
+                       for(RefPtr<Expression> &a: call.arguments)
+                               if(BasicTypeDeclaration *basic_arg = dynamic_cast<BasicTypeDeclaration *>(a->type))
                                {
                                        BasicTypeDeclaration *elem_arg = get_element_type(*basic_arg);
                                        if(elem_arg!=elem)
-                                               convert_to_element(*j, *elem);
+                                               convert_to_element(a, *elem);
                                }
                }
        }
@@ -1062,16 +1042,17 @@ void ExpressionResolver::visit_constructor(FunctionCall &call)
                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)
+               auto j = call.arguments.begin();
+               for(const RefPtr<Statement> &s: strct->members.body)
                {
-                       if(VariableDeclaration *var = dynamic_cast<VariableDeclaration *>(j->get()))
+                       if(VariableDeclaration *var = dynamic_cast<VariableDeclaration *>(s.get()))
                        {
-                               if(!call.arguments[k]->type || call.arguments[k]->type!=var->type_declaration)
+                               if(!(*j)->type || (*j)->type!=var->type_declaration)
                                        return;
                        }
                        else
                                return;
+                       ++j;
                }
        }
 
@@ -1119,6 +1100,29 @@ bool FunctionResolver::apply(Stage &s)
        return r_any_resolved;
 }
 
+bool FunctionResolver::can_convert_arguments(const FunctionCall &call, const FunctionDeclaration &decl)
+{
+       if(decl.parameters.size()!=call.arguments.size())
+               return false;
+
+       for(unsigned j=0; j<call.arguments.size(); ++j)
+       {
+               const TypeDeclaration *arg_type = call.arguments[j]->type;
+               const TypeDeclaration *param_type = decl.parameters[j]->type_declaration;
+               if(arg_type==param_type)
+                       continue;
+
+               const BasicTypeDeclaration *arg_basic = dynamic_cast<const BasicTypeDeclaration *>(arg_type);
+               const BasicTypeDeclaration *param_basic = dynamic_cast<const BasicTypeDeclaration *>(param_type);
+               if(arg_basic && param_basic && can_convert(*arg_basic, *param_basic))
+                       continue;
+
+               return false;
+       }
+
+       return true;
+}
+
 void FunctionResolver::visit(FunctionCall &call)
 {
        FunctionDeclaration *declaration = 0;
@@ -1128,7 +1132,7 @@ void FunctionResolver::visit(FunctionCall &call)
        {
                string arg_types;
                bool has_signature = true;
-               for(NodeArray<Expression>::const_iterator i=call.arguments.begin(); (has_signature && i!=call.arguments.end()); ++i)
+               for(auto i=call.arguments.begin(); (has_signature && i!=call.arguments.end()); ++i)
                {
                        if((*i)->type)
                                append(arg_types, ",", (*i)->type->name);
@@ -1138,8 +1142,23 @@ void FunctionResolver::visit(FunctionCall &call)
 
                if(has_signature)
                {
-                       map<string, FunctionDeclaration *>::iterator i = stage->functions.find(format("%s(%s)", call.name, arg_types));
+                       auto i = stage->functions.find(format("%s(%s)", call.name, arg_types));
                        declaration = (i!=stage->functions.end() ? i->second : 0);
+
+                       if(!declaration)
+                       {
+                               for(i=stage->functions.lower_bound(call.name+"("); (i!=stage->functions.end() && i->second->name==call.name); ++i)
+                                       if(can_convert_arguments(call, *i->second))
+                                       {
+                                               if(declaration)
+                                               {
+                                                       declaration = 0;
+                                                       break;
+                                               }
+                                               else
+                                                       declaration = i->second;
+                                       }
+                       }
                }
        }
 
@@ -1154,10 +1173,10 @@ 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)
+               for(const RefPtr<VariableDeclaration> &p: func.parameters)
                {
-                       if((*i)->type_declaration)
-                               append(param_types, ",", (*i)->type_declaration->name);
+                       if(p->type_declaration)
+                               append(param_types, ",", p->type_declaration->name);
                        else
                                return;
                }
@@ -1182,11 +1201,11 @@ void FunctionResolver::visit(FunctionDeclaration &func)
                stage_decl = &func;
 
                // Set all previous declarations to use this definition.
-               for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
+               for(FunctionDeclaration *f: decls)
                {
-                       r_any_resolved |= (func.definition!=(*i)->definition);
-                       (*i)->definition = func.definition;
-                       (*i)->body.body.clear();
+                       r_any_resolved |= (func.definition!=f->definition);
+                       f->definition = func.definition;
+                       f->body.body.clear();
                }
        }
        else