X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fglsl%2Fresolve.cpp;h=96ca492123962a464e77b4de57f9427f6196bdd0;hp=176b13c5f1f55ad13e1613be145676150ef93db5;hb=3a1b9cbe2441ae670a97541dc8ccb0a2860c8302;hpb=523491787b2b0321748a53f139c1a4355d2f9e85 diff --git a/source/glsl/resolve.cpp b/source/glsl/resolve.cpp index 176b13c5..96ca4921 100644 --- a/source/glsl/resolve.cpp +++ b/source/glsl/resolve.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include "reflect.h" @@ -34,7 +34,7 @@ bool TypeResolver::apply(Stage &s) TypeDeclaration *TypeResolver::get_or_create_array_type(TypeDeclaration &type) { - map::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::iterator i = stage->types.find(name); + auto i = stage->types.find(name); if(i!=stage->types.end()) { - map::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::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::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::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 &blocks = stage->interface_blocks; - map::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::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(memacc.left->type)) { - map::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::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::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::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::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::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 &expr, BasicTypeDeclaration &type) @@ -549,7 +544,7 @@ void ExpressionResolver::resolve(Expression &expr, TypeDeclaration *type, bool l void ExpressionResolver::visit(Block &block) { SetForScope set_block(current_block, &block); - for(NodeList::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()) resolve(literal, find_type(BasicTypeDeclaration::BOOL, 1), false); else if(literal.value.check_type()) - resolve(literal, find_type(BasicTypeDeclaration::INT, 32), false); + resolve(literal, find_type(BasicTypeDeclaration::INT, 32, true), false); + else if(literal.value.check_type()) + resolve(literal, find_type(BasicTypeDeclaration::INT, 32, false), false); else if(literal.value.check_type()) resolve(literal, find_type(BasicTypeDeclaration::FLOAT, 32), false); } @@ -855,7 +852,7 @@ void ExpressionResolver::visit_constructor(FunctionCall &call) if(call.arguments.empty()) return; - map::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::const_iterator j=call.arguments.begin(); j!=call.arguments.end(); ++j) + for(const RefPtr &a: call.arguments) { ArgumentInfo info; - if(!(info.type=dynamic_cast((*j)->type))) + if(!(info.type=dynamic_cast(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::iterator j=call.arguments.begin(); j!=call.arguments.end(); ++j) - if(BasicTypeDeclaration *basic_arg = dynamic_cast((*j)->type)) + for(RefPtr &a: call.arguments) + if(BasicTypeDeclaration *basic_arg = dynamic_cast(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::const_iterator j=strct->members.body.begin(); j!=strct->members.body.end(); ++j, ++k) + auto j = call.arguments.begin(); + for(const RefPtr &s: strct->members.body) { - if(VariableDeclaration *var = dynamic_cast(j->get())) + if(VariableDeclaration *var = dynamic_cast(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; jtype; + const TypeDeclaration *param_type = decl.parameters[j]->type_declaration; + if(arg_type==param_type) + continue; + + const BasicTypeDeclaration *arg_basic = dynamic_cast(arg_type); + const BasicTypeDeclaration *param_basic = dynamic_cast(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::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::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::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i) + for(const RefPtr &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::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