-
- 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)
-{
- block.parent = current_block;
-}
-
-
-TypeResolver::TypeResolver():
- stage(0)
-{ }
-
-void TypeResolver::apply(Stage &s)
-{
- stage = &s;
- s.types.clear();
- s.content.visit(*this);
-}
-
-TypeDeclaration *TypeResolver::resolve_type(const string &name)
-{
- map<string, TypeDeclaration *>::iterator i = stage->types.find(name);
- if(i!=stage->types.end())
- {
- map<TypeDeclaration *, TypeDeclaration *>::iterator j = alias_map.find(i->second);
- return (j!=alias_map.end() ? j->second : i->second);
- }
- else
- return 0;
-}
-
-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)
-{
- type.base_type = resolve_type(type.base);
-
- 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)
-{
- type.base_type = resolve_type(type.base);
- 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)
-{
- TypeDeclaration *type = resolve_type(var.type);
- if(var.array && type)
- {
- map<TypeDeclaration *, TypeDeclaration *>::iterator i = array_types.find(type);
- if(i==array_types.end())
- {
- 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->visit(*this);
- type = array;
- }
- else
- type = i->second;
- }
- var.type_declaration = type;
-}
-
-void TypeResolver::visit(FunctionDeclaration &func)
-{
- func.return_type_declaration = resolve_type(func.return_type);
- 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;