-
- stage->types.insert(make_pair(type.name, &type));
-}
-
-void TypeResolver::visit(ImageTypeDeclaration &type)
-{
- map<string, TypeDeclaration *>::iterator i = stage->types.find(type.base);
- type.base_type = (i!=stage->types.end() ? i->second : 0);
-
- 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)
-{
- map<string, TypeDeclaration *>::iterator i = stage->types.find(var.type);
- if(i!=stage->types.end())
- var.type_declaration = i->second;
-}
-
-void TypeResolver::visit(FunctionDeclaration &func)
-{
- map<string, TypeDeclaration *>::iterator i = stage->types.find(func.return_type);
- func.return_type_declaration = (i!=stage->types.end() ? i->second : 0);
- TraversingVisitor::visit(func);
-}
-
-
-VariableResolver::VariableResolver():
- stage(0),
- r_members(0),
- record_target(false),
- r_self_referencing(false),
- r_assignment_target(0)
-{ }
-
-void VariableResolver::apply(Stage &s)
-{
- stage = &s;
- s.interface_blocks.clear();
- s.content.visit(*this);
-}
-
-void VariableResolver::enter(Block &block)
-{
- block.variables.clear();
-}
-
-void VariableResolver::visit(VariableReference &var)
-{
- var.declaration = 0;
- r_members = 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; (!var.declaration && block); block=block->parent)
- {
- map<string, VariableDeclaration *>::iterator i = block->variables.find(var.name);
- if(i!=block->variables.end())
- var.declaration = i->second;
- }
-
- if(!var.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. */
- r_iface_ref = new InterfaceBlockReference;
- r_iface_ref->source = var.source;
- r_iface_ref->line = var.line;
- r_iface_ref->name = var.name;
- r_iface_ref->declaration = i->second;
- r_members = &i->second->members.variables;
- }
- else
- {
- // Look for the variable in anonymous interface blocks.
- for(i=blocks.begin(); (!var.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())
- var.declaration = j->second;
- }
- }
- }
-
- if(var.declaration)
- if(StructDeclaration *strct = dynamic_cast<StructDeclaration *>(var.declaration->type_declaration))
- r_members = &strct->members.variables;
-
- 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)
-{
- iface.declaration = 0;
- for(Block *block=current_block; block; block=block->parent)
- {
- map<string, InterfaceBlock *>::iterator i = stage->interface_blocks.find("_"+iface.name);
- if(i!=stage->interface_blocks.end())
- {
- iface.declaration = i->second;
- r_members = &i->second->members.variables;
- break;
- }
- }
-}
-
-void VariableResolver::visit(MemberAccess &memacc)
-{
- r_members = 0;
- r_iface_ref = 0;
- memacc.left->visit(*this);
-
- if(r_iface_ref)
- memacc.left = r_iface_ref;
- r_iface_ref = 0;
-
- memacc.declaration = 0;
- if(r_members)
- {
- map<string, VariableDeclaration *>::iterator i = r_members->find(memacc.member);
- if(i!=r_members->end())
- {
- memacc.declaration = i->second;
- if(StructDeclaration *strct = dynamic_cast<StructDeclaration *>(i->second->type_declaration))
- r_members = &strct->members.variables;
- }
- else
- r_members = 0;
- }
-}
-
-void VariableResolver::visit(UnaryExpression &unary)
-{
- TraversingVisitor::visit(unary);
- r_members = 0;
- r_iface_ref = 0;
-}
-
-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);
- binary.right->visit(*this);