- stage.content.visit(*this);
- NodeRemover().apply(stage, nodes_to_remove);
-}
-
-void DeclarationCombiner::visit(Block &block)
-{
- if(current_block)
- return;
-
- TraversingVisitor::visit(block);
-}
-
-void DeclarationCombiner::visit(VariableDeclaration &var)
-{
- VariableDeclaration *&ptr = variables[var.name];
- if(ptr)
- {
- ptr->type = var.type;
- if(var.init_expression)
- ptr->init_expression = var.init_expression;
- if(var.layout)
- {
- if(ptr->layout)
- {
- for(vector<Layout::Qualifier>::iterator i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); ++i)
- {
- bool found = false;
- for(vector<Layout::Qualifier>::iterator j=ptr->layout->qualifiers.begin(); (!found && j!=ptr->layout->qualifiers.end()); ++j)
- if(j->name==i->name)
- {
- j->has_value = i->value;
- j->value = i->value;
- found = true;
- }
-
- if(!found)
- ptr->layout->qualifiers.push_back(*i);
- }
- }
- else
- ptr->layout = var.layout;
- }
- nodes_to_remove.insert(&var);
- }
- else
- ptr = &var;
-}
-
-
-ConstantSpecializer::ConstantSpecializer():
- values(0)
-{ }
-
-void ConstantSpecializer::apply(Stage &stage, const map<string, int> *v)
-{
- values = v;
- stage.content.visit(*this);
-}
-
-void ConstantSpecializer::visit(VariableDeclaration &var)
-{
- bool specializable = false;
- if(var.layout)
- {
- vector<Layout::Qualifier> &qualifiers = var.layout->qualifiers;
- for(vector<Layout::Qualifier>::iterator i=qualifiers.begin(); i!=qualifiers.end(); ++i)
- if(i->name=="constant_id")
- {
- specializable = true;
- if(values)
- qualifiers.erase(i);
- else if(i->value==-1)
- i->value = hash32(var.name)&0x7FFFFFFF;
- break;
- }
-
- if(qualifiers.empty())
- var.layout = 0;
- }
-
- if(specializable && values)
- {
- map<string, int>::const_iterator i = values->find(var.name);
- if(i!=values->end())
- {
- RefPtr<Literal> literal = new Literal;
- if(var.type=="bool")
- {
- literal->token = (i->second ? "true" : "false");
- literal->value = static_cast<bool>(i->second);
- }
- else if(var.type=="int")
- {
- literal->token = lexical_cast<string>(i->second);
- literal->value = i->second;
- }
- var.init_expression = literal;
- }
- }
-}
-
-
-void BlockHierarchyResolver::enter(Block &block)
-{
- r_any_resolved |= (current_block!=block.parent);
- block.parent = current_block;
-}
-
-
-TypeResolver::TypeResolver():
- stage(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;
- 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);
-}
-
-void TypeResolver::visit(FunctionDeclaration &func)
-{
- resolve_type(func.return_type_declaration, func.return_type, false);
- TraversingVisitor::visit(func);
-}
-
-
-VariableResolver::VariableResolver():
- stage(0),
- r_any_resolved(false),
- record_target(false),
- r_self_referencing(false),
- r_assignment_target(0)
-{ }
-
-bool VariableResolver::apply(Stage &s)
-{
- stage = &s;
- s.interface_blocks.clear();
- r_any_resolved = false;
- s.content.visit(*this);
- return r_any_resolved;
-}
-
-void VariableResolver::enter(Block &block)
-{
- block.variables.clear();
-}
-
-void VariableResolver::visit_and_replace(RefPtr<Expression> &expr)
-{
- r_replacement_expr = 0;
- expr->visit(*this);
- if(r_replacement_expr)
- expr = r_replacement_expr;
- r_replacement_expr = 0;
-}
-
-void VariableResolver::visit(VariableReference &var)
-{
- VariableDeclaration *declaration = 0;
-
- /* Look for variable declarations in the block hierarchy first. Interface
- blocks are always defined in the top level so we can't accidentally skip
- one. */
- for(Block *block=current_block; (!declaration && block); block=block->parent)
- {
- map<string, VariableDeclaration *>::iterator i = block->variables.find(var.name);
- if(i!=block->variables.end())
- declaration = i->second;
- }
-
- if(!declaration)
- {
- const map<string, InterfaceBlock *> &blocks = stage->interface_blocks;
- map<string, InterfaceBlock *>::const_iterator i = blocks.find("_"+var.name);
- 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. */
- 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())
- {
- map<string, VariableDeclaration *>::iterator j = i->second->members.variables.find(var.name);
- if(j!=i->second->members.variables.end())
- declaration = j->second;
- }
- }
- }
-
- r_any_resolved |= (declaration!=var.declaration);
- var.declaration = declaration;
-
- if(record_target)
- {
- if(r_assignment_target)
- {
- /* More than one variable reference found in assignment target.
- Unable to determine what the primary target is. */
- record_target = false;
- r_assignment_target = 0;
- }
- else
- r_assignment_target = var.declaration;
- }
- else if(var.declaration && var.declaration==r_assignment_target)
- r_self_referencing = true;
-}
-
-void VariableResolver::visit(InterfaceBlockReference &iface)
-{
- map<string, InterfaceBlock *>::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;
-}
-
-void VariableResolver::visit(MemberAccess &memacc)
-{
- visit_and_replace(memacc.left);
-
- map<string, VariableDeclaration *> *members = 0;
- if(StructDeclaration *strct = dynamic_cast<StructDeclaration *>(memacc.left->type))
- members = &strct->members.variables;
- else if(InterfaceBlockReference *iface_ref = dynamic_cast<InterfaceBlockReference *>(memacc.left.get()))
- {
- if(iface_ref->declaration)
- members = &iface_ref->declaration->members.variables;
- }
-
- VariableDeclaration *declaration = 0;
- if(members)
- {
- map<string, VariableDeclaration *>::iterator i = members->find(memacc.member);
- if(i!=members->end())
- declaration = i->second;
- }
-
- r_any_resolved |= (declaration!=memacc.declaration);
- memacc.declaration = declaration;
-}
-
-void VariableResolver::visit(UnaryExpression &unary)
-{
- visit_and_replace(unary.expression);
-}
-
-void VariableResolver::visit(BinaryExpression &binary)
-{
- if(binary.oper->token[0]=='[')
- {
- {
- /* The subscript expression is not a part of the primary assignment
- target. */
- SetFlag set(record_target, false);
- visit_and_replace(binary.right);
- }
- visit_and_replace(binary.left);
- }
- else
- {
- visit_and_replace(binary.left);
- visit_and_replace(binary.right);
- }
-}
-
-void VariableResolver::visit(Assignment &assign)
-{
- {
- SetFlag set(record_target);
- r_assignment_target = 0;
- visit_and_replace(assign.left);
- r_any_resolved |= (r_assignment_target!=assign.target_declaration);
- assign.target_declaration = r_assignment_target;
- }
-
- r_self_referencing = false;
- visit_and_replace(assign.right);
- assign.self_referencing = (r_self_referencing || assign.oper->token[0]!='=');
-}
-
-void VariableResolver::visit(FunctionCall &call)
-{
- for(NodeArray<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
- visit_and_replace(*i);
-}
-
-void VariableResolver::visit(VariableDeclaration &var)
-{
- if(!block_interface.empty() && var.interface.empty())
- var.interface = block_interface;
-
- TraversingVisitor::visit(var);
- current_block->variables.insert(make_pair(var.name, &var));
-}
-
-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.name, &iface));
- if(!iface.instance_name.empty())
- stage->interface_blocks.insert(make_pair("_"+iface.instance_name, &iface));
-
- SetForScope<string> set_iface(block_interface, iface.interface);
- TraversingVisitor::visit(iface);
-}
-
-
-ExpressionResolver::ExpressionResolver():
- stage(0),
- r_any_resolved(false)
-{ }
-
-bool ExpressionResolver::apply(Stage &s)
-{
- stage = &s;
- r_any_resolved = false;
- s.content.visit(*this);
- return r_any_resolved;
-}
-
-bool ExpressionResolver::is_scalar(BasicTypeDeclaration &type)
-{
- return (type.kind==BasicTypeDeclaration::INT || type.kind==BasicTypeDeclaration::FLOAT);
-}