X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fglsl%2Fresolve.cpp;h=fbe60d79def36ee514e9598541a2ab99f05b4539;hp=bf32326f249defc16224efb11b10ed3a5a6c7244;hb=HEAD;hpb=fdf2d162b21f55ad60bfeec2ad98c58356a083c0 diff --git a/source/glsl/resolve.cpp b/source/glsl/resolve.cpp index bf32326f..1d165b7e 100644 --- a/source/glsl/resolve.cpp +++ b/source/glsl/resolve.cpp @@ -1,6 +1,8 @@ -#include +#include #include +#include #include +#include "reflect.h" #include "resolve.h" using namespace std; @@ -16,12 +18,6 @@ void BlockHierarchyResolver::enter(Block &block) } -TypeResolver::TypeResolver(): - stage(0), - iface_block(0), - r_any_resolved(false) -{ } - bool TypeResolver::apply(Stage &s) { stage = &s; @@ -33,7 +29,9 @@ bool TypeResolver::apply(Stage &s) TypeDeclaration *TypeResolver::get_or_create_array_type(TypeDeclaration &type) { - map::iterator i = array_types.find(&type); + bool extended_alignment = iface_block; + auto key = make_pair(&type, extended_alignment); + auto i = array_types.find(key); if(i!=array_types.end()) return i->second; @@ -41,33 +39,69 @@ TypeDeclaration *TypeResolver::get_or_create_array_type(TypeDeclaration &type) array->source = INTERNAL_SOURCE; array->name = type.name+"[]"; array->kind = BasicTypeDeclaration::ARRAY; + array->extended_alignment = extended_alignment; array->base = type.name; array->base_type = &type; stage->content.body.insert(type_insert_point, array); - array_types[&type] = array; + array_types[key] = array; return array; } -void TypeResolver::resolve_type(TypeDeclaration *&type, const string &name, bool array) +TypeDeclaration *TypeResolver::get_or_create_image_type(ImageTypeDeclaration &type, const std::string &texel_format) +{ + if(texel_format.empty()) + return &type; + + auto key = make_pair(&type, texel_format); + auto i = image_types.find(key); + if(i!=image_types.end()) + return i->second; + + ImageTypeDeclaration *image = new ImageTypeDeclaration(type); + image->source = INTERNAL_SOURCE; + image->name = format("%s_%s", type.name, texel_format); + image->format = texel_format; + image->base_image = &type; + stage->content.body.insert(type_insert_point, image); + image_types[key] = image; + return image; +} + +void TypeResolver::resolve_type(TypeDeclaration *&type, const string &name, bool array, const Layout *layout) { 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); } - if(resolved && array) - resolved = get_or_create_array_type(*resolved); + if(resolved) + { + if(ImageTypeDeclaration *image = dynamic_cast(resolved)) + if(layout) + { + static const Regex r_format("^(r|rg|rgba)(8|16|8_snorm|16_snorm|16f|32f)$"); + for(const Layout::Qualifier &q: layout->qualifiers) + if(r_format.match(q.name)) + { + resolved = get_or_create_image_type(*image, q.name); + break; + } + } + + if(array) + resolved = get_or_create_array_type(*resolved); + } r_any_resolved |= (resolved!=type); - type=resolved; + type = resolved; } 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; @@ -93,7 +127,7 @@ void TypeResolver::visit(BasicTypeDeclaration &type) 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; + array_types[make_pair(type.base_type, type.extended_alignment)] = &type; stage->types.insert(make_pair(type.name, &type)); } @@ -101,46 +135,49 @@ void TypeResolver::visit(BasicTypeDeclaration &type) void TypeResolver::visit(ImageTypeDeclaration &type) { resolve_type(type.base_type, type.base, false); + + if(!type.format.empty() && type.base_image) + image_types[make_pair(type.base_image, type.format)] = &type; + stage->types.insert(make_pair(type.name, &type)); } void TypeResolver::visit(StructDeclaration &strct) { stage->types.insert(make_pair(strct.name, &strct)); - TraversingVisitor::visit(strct); + if(strct.block_name.empty()) + { + SetForScope set_iface(iface_block, strct.block_declaration); + TraversingVisitor::visit(strct); + } + else + block_member_type_ins_pt = type_insert_point; } 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(); -} + resolve_type(var.type_declaration, var.type, var.array, var.layout.get()); -void TypeResolver::visit(InterfaceBlock &iface) -{ - if(iface.members) + var.block_declaration = 0; + if(StructDeclaration *strct = dynamic_cast(get_ultimate_base_type(var.type_declaration))) + if(!strct->block_name.empty()) + { + var.block_declaration = strct; + strct->block_declaration = &var; + strct->extended_alignment = true; + + SetForScope::iterator> set_ins_pt(type_insert_point, block_member_type_ins_pt); + SetForScope set_iface(iface_block, &var); + TraversingVisitor::visit(*strct); + } + + if(iface_block) { - SetForScope 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; + if(var.interface==iface_block->interface) + var.interface.clear(); + if(StructDeclaration *strct = dynamic_cast(var.type_declaration)) + strct->extended_alignment = true; } - - 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) @@ -150,21 +187,14 @@ void TypeResolver::visit(FunctionDeclaration &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::const_iterator i=redeclared_builtins.begin(); i!=redeclared_builtins.end(); ++i) - (*i)->source = GENERATED_SOURCE; + for(Statement *b: redeclared_builtins) + b->source = GENERATED_SOURCE; NodeRemover().apply(s, nodes_to_remove); return r_any_resolved; } @@ -189,7 +219,7 @@ void VariableResolver::visit(RefPtr &expr) r_replacement_expr = 0; } -void VariableResolver::check_assignment_target(Statement *declaration) +void VariableResolver::check_assignment_target(VariableDeclaration *declaration) { if(record_target) { @@ -217,37 +247,23 @@ 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; } if(!declaration) { - const map &blocks = stage->interface_blocks; - map::const_iterator 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 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->declaration = i->second; - - if(i->second->instance_name.empty()) + for(const auto &kvp: stage->interface_blocks) + if(kvp.second->name.find(' ')!=string::npos && kvp.second->block_declaration->members.variables.count(var.name)) { - iface_ref->name = format("%s %s", i->second->interface, i->second->block_name); + /* The name refers a member of an anonymous interface block. Prepare + new syntax tree nodes accordingly. */ + VariableReference *iface_ref = new VariableReference; + iface_ref->name = kvp.first; + iface_ref->source = var.source; + iface_ref->line = var.line; + iface_ref->declaration = kvp.second; MemberAccess *memacc = new MemberAccess; memacc->source = var.source; @@ -256,13 +272,8 @@ void VariableResolver::visit(VariableReference &var) memacc->member = var.name; r_replacement_expr = memacc; + break; } - else - { - iface_ref->name = var.name; - r_replacement_expr = iface_ref; - } - } } r_any_resolved |= (declaration!=var.declaration); @@ -271,16 +282,6 @@ void VariableResolver::visit(VariableReference &var) check_assignment_target(var.declaration); } -void VariableResolver::visit(InterfaceBlockReference &iface) -{ - map::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::visit(MemberAccess &memacc) { TraversingVisitor::visit(memacc); @@ -289,11 +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; - for(NodeList::const_iterator j=strct->members.body.begin(); (j!=strct->members.body.end() && j->get()!=i->second); ++j) + index = 0; + for(auto j=strct->members.body.begin(); (j!=strct->members.body.end() && j->get()!=i->second); ++j) ++index; if(record_target) @@ -309,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; - } + 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); + } +} - if(!found) - to_layout.qualifiers.push_back(*i); +void VariableResolver::redeclare_builtin(VariableDeclaration &existing, VariableDeclaration &var) +{ + if(var.layout) + { + if(existing.layout) + merge_layouts(*existing.layout, *var.layout); + else + existing.layout = var.layout; } + if(var.array_size) + existing.array_size = var.array_size; + + redeclared_builtins.push_back(&existing); } 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) + + auto i = current_block->variables.find(var.name); + VariableDeclaration *existing = 0; + VariableDeclaration *block = 0; + if(i!=current_block->variables.end()) + existing = i->second; + else if(!current_block->parent) { - 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))); + const map &blocks = stage->interface_blocks; + for(auto j=blocks.begin(); j!=blocks.end(); ++j) + if(j->second->name.find(' ')!=string::npos) + { + const map &block_vars = j->second->block_declaration->members.variables; + auto k = block_vars.find(var.name); + if(k!=block_vars.end()) + { + existing = k->second; + block = j->second; + break; + } + } + } - if(var.init_expression) - ptr->init_expression = var.init_expression; - if(var.layout) + if(!existing) + { + current_block->variables.insert(make_pair(var.name, &var)); + if(var.block_declaration) { - if(ptr->layout) - merge_layouts(*ptr->layout, *var.layout); - else - ptr->layout = var.layout; + stage->interface_blocks.insert(make_pair(format("%s %s", var.interface, var.block_declaration->block_name), &var)); + if(var.name.find(' ')==string::npos) + stage->interface_blocks.insert(make_pair(var.name, &var)); } - nodes_to_remove.insert(&var); - - r_any_resolved = true; } -} + else if(!current_block->parent && (block ? block->interface : existing->interface)==var.interface && existing->array==var.array) + { + if(existing->source==BUILTIN_SOURCE) + { + if(var.block_declaration && existing->block_declaration && var.block_declaration->block_name==existing->block_declaration->block_name) + { + const map &vars = var.block_declaration->members.variables; + const map &existing_vars = existing->block_declaration->members.variables; -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(format("%s %s", iface.interface, iface.block_name), &iface)); - if(!iface.instance_name.empty()) - stage->interface_blocks.insert(make_pair(iface.instance_name, &iface)); + bool found_all = true; + for(const auto &kvp: vars) + { + auto j = existing_vars.find(kvp.first); + if(j!=existing_vars.end() && j->second->type==kvp.second->type && j->second->array==kvp.second->array) + redeclare_builtin(*j->second, *kvp.second); + else + found_all = false; + } - TraversingVisitor::visit(iface); -} + if(found_all) + { + redeclared_builtins.push_back(existing); + nodes_to_remove.insert(&var); + // The block struct will be removed during unused type removal + //nodes_to_remove.insert(var.block_declaration); + } + } + else if(!var.block_declaration && !existing->block_declaration && var.type==existing->type) + { + redeclare_builtin(*existing, var); + if(block) + { + /* Cause the block and its members to be marked as not builtin + so it will be emitted in output */ + redeclared_builtins.push_back(block); + for(const auto &kvp: block->block_declaration->members.variables) + redeclared_builtins.push_back(kvp.second); + } + + nodes_to_remove.insert(&var); + r_any_resolved = true; + } + } + else if(existing->array && !existing->array_size && var.type==existing->type && !var.layout && !var.init_expression) + { + existing->array_size = var.array_size; + nodes_to_remove.insert(&var); + r_any_resolved = true; + } + } +} -ExpressionResolver::ExpressionResolver(): - stage(0), - r_any_resolved(false) -{ } bool ExpressionResolver::apply(Stage &s) { @@ -458,43 +516,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(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(from.base_type); - BasicTypeDeclaration *to_base = dynamic_cast(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) @@ -507,20 +528,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) @@ -584,7 +603,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); @@ -596,7 +615,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); } @@ -607,12 +628,6 @@ void ExpressionResolver::visit(VariableReference &var) 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); @@ -744,6 +759,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; @@ -879,9 +905,11 @@ 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) + ; else if(BasicTypeDeclaration *basic = dynamic_cast(i->second)) { BasicTypeDeclaration *elem = get_element_type(*basic); @@ -892,10 +920,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; @@ -946,6 +974,11 @@ void ExpressionResolver::visit_constructor(FunctionCall &call) { unsigned column_count = basic->size&0xFFFF; unsigned row_count = basic->size>>16; + + vector > columns; + columns.reserve(column_count); + bool changed_columns = false; + if(call.arguments.size()==1) { /* A matrix can be constructed from a single element or another @@ -968,8 +1001,6 @@ void ExpressionResolver::visit_constructor(FunctionCall &call) current_block->body.insert(insert_point, temporary); // Create expressions to build each column. - vector > columns; - columns.reserve(column_count); for(unsigned j=0; j ref = new VariableReference; @@ -990,13 +1021,7 @@ void ExpressionResolver::visit_constructor(FunctionCall &call) 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; + changed_columns = true; } else return; @@ -1005,9 +1030,6 @@ void ExpressionResolver::visit_constructor(FunctionCall &call) { /* Construct a matrix from individual components in column-major order. Arguments must align at column boundaries. */ - vector > columns; - columns.reserve(column_count); - vector > column_args; column_args.reserve(row_count); unsigned column_component_count = 0; @@ -1041,11 +1063,24 @@ void ExpressionResolver::visit_constructor(FunctionCall &call) else if(column_component_count>row_count) // Argument alignment mismatch. return; + + changed_columns = true; } } } else return; + + if(changed_columns) + { + 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; @@ -1053,12 +1088,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); } } } @@ -1067,16 +1102,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; } } @@ -1088,7 +1124,36 @@ void ExpressionResolver::visit(FunctionCall &call) TraversingVisitor::visit(call); if(call.declaration) + { + for(unsigned i=0; itype; + TypeDeclaration *param_type = call.declaration->parameters[i]->type_declaration; + if(arg_type==param_type) + continue; + else if(!arg_type || !param_type) + return; + + BasicTypeDeclaration *arg_basic = dynamic_cast(arg_type); + BasicTypeDeclaration *param_basic = dynamic_cast(param_type); + if(arg_basic && param_basic) + { + Compatibility compat = get_compatibility(*param_basic, *arg_basic); + if(compat==RIGHT_CONVERTIBLE) + convert_to(call.arguments[i], *param_basic); + else if(compat!=SAME_TYPE) + return; + } + else + { + ImageTypeDeclaration *arg_image = dynamic_cast(arg_type); + ImageTypeDeclaration *param_image = dynamic_cast(param_type); + if(!arg_image || !param_image || arg_image->base_image!=param_image) + return; + } + } resolve(call, call.declaration->return_type_declaration, false); + } else if(call.constructor) visit_constructor(call); } @@ -1114,6 +1179,28 @@ void ExpressionResolver::visit(VariableDeclaration &var) convert_to(var.init_expression, *var_basic); } +void ExpressionResolver::visit(FunctionDeclaration &func) +{ + SetForScope set_func(current_function, &func); + TraversingVisitor::visit(func); +} + +void ExpressionResolver::visit(Return &ret) +{ + TraversingVisitor::visit(ret); + if(!current_function || !ret.expression) + return; + + BasicTypeDeclaration *ret_basic = dynamic_cast(current_function->return_type_declaration); + BasicTypeDeclaration *expr_basic = dynamic_cast(ret.expression->type); + if(!ret_basic || !expr_basic) + return; + + Compatibility compat = get_compatibility(*ret_basic, *expr_basic); + if(compat==RIGHT_CONVERTIBLE) + convert_to(ret.expression, *ret_basic); +} + bool FunctionResolver::apply(Stage &s) { @@ -1124,6 +1211,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; @@ -1133,18 +1243,38 @@ 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); + if(const TypeDeclaration *type = (*i)->type) + { + if(const ImageTypeDeclaration *image = dynamic_cast(type)) + if(image->base_image) + type = image->base_image; + append(arg_types, ",", type->name); + } else has_signature = false; } 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; + } + } } } @@ -1159,10 +1289,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; } @@ -1187,11 +1317,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