]> 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 176b13c5f1f55ad13e1613be145676150ef93db5..96ca492123962a464e77b4de57f9427f6196bdd0 100644 (file)
@@ -1,4 +1,4 @@
-#include <algorithm>
+#include <msp/core/algorithm.h>
 #include <msp/core/raii.h>
 #include <msp/strings/utils.h>
 #include "reflect.h"
@@ -34,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;
 
@@ -52,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);
        }
 
@@ -68,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;
@@ -164,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;
 }
@@ -218,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;
        }
@@ -226,7 +226,7 @@ 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.
@@ -274,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;
@@ -290,12 +290,12 @@ void VariableResolver::visit(MemberAccess &memacc)
        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(NodeList<Statement>::const_iterator j=strct->members.body.begin(); (j!=strct->members.body.end() && j->get()!=i->second); ++j)
+                       for(auto j=strct->members.body.begin(); (j!=strct->members.body.end() && j->get()!=i->second); ++j)
                                ++index;
 
                        if(record_target)
@@ -311,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)
                        {
@@ -390,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);
        }
 }
 
@@ -472,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)
@@ -549,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);
@@ -561,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);
 }
@@ -855,7 +852,7 @@ 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)
@@ -870,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;
@@ -1031,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);
                                }
                }
        }
@@ -1045,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;
                }
        }
 
@@ -1102,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;
@@ -1111,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);
@@ -1121,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;
+                                       }
+                       }
                }
        }
 
@@ -1137,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;
                }
@@ -1165,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