+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 = BUILTIN_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.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);
+}
+
+