namespace GL {
namespace SL {
-DeclarationCombiner::DeclarationCombiner():
- toplevel(true)
-{ }
+void DeclarationCombiner::apply(Stage &stage)
+{
+ stage.content.visit(*this);
+ NodeRemover().apply(stage, nodes_to_remove);
+}
void DeclarationCombiner::visit(Block &block)
{
- if(!toplevel)
+ if(current_block)
return;
- SetForScope<bool> set(toplevel, false);
- BlockModifier::visit(block);
+ TraversingVisitor::visit(block);
}
void DeclarationCombiner::visit(FunctionDeclaration &func)
{
bool found = false;
for(vector<Layout::Qualifier>::iterator j=ptr->layout->qualifiers.begin(); (!found && j!=ptr->layout->qualifiers.end()); ++j)
- if(j->identifier==i->identifier)
+ if(j->name==i->name)
{
+ j->has_value = i->value;
j->value = i->value;
found = true;
}
else
ptr->layout = var.layout;
}
- remove_node = true;
+ nodes_to_remove.insert(&var);
}
else
ptr = &var;
}
+void BlockResolver::enter(Block &block)
+{
+ block.parent = current_block;
+}
+
+void BlockResolver::visit(InterfaceBlock &iface)
+{
+ /* Block names can't be used for any other identifiers so we can put them
+ in the same map with instance names. */
+ current_block->interfaces[iface.name] = &iface;
+ if(!iface.instance_name.empty())
+ current_block->interfaces[iface.instance_name] = &iface;
+ TraversingVisitor::visit(iface);
+}
+
+
VariableResolver::VariableResolver():
- anonymous(false),
+ builtins(0),
+ members(0),
record_target(false),
assignment_target(0),
self_referencing(false)
void VariableResolver::apply(Stage &stage)
{
- Stage *builtins = get_builtins(stage.type);
- if(builtins)
- blocks.push_back(&builtins->content);
- visit(stage.content);
- if(builtins)
- blocks.pop_back();
+ Stage *builtin_stage = get_builtins(stage.type);
+ builtins = (builtin_stage ? &builtin_stage->content : 0);
+ stage.content.visit(*this);
}
-void VariableResolver::visit(Block &block)
+Block *VariableResolver::next_block(Block &block)
+{
+ return block.parent ? block.parent : &block!=builtins ? builtins : 0;
+}
+
+void VariableResolver::enter(Block &block)
{
- blocks.push_back(&block);
block.variables.clear();
- TraversingVisitor::visit(block);
- blocks.pop_back();
}
void VariableResolver::visit(VariableReference &var)
{
var.declaration = 0;
- type = 0;
- for(vector<Block *>::iterator i=blocks.end(); i!=blocks.begin(); )
+ members = 0;
+ for(Block *block=current_block; block; block=next_block(*block))
{
- --i;
- map<string, VariableDeclaration *>::iterator j = (*i)->variables.find(var.name);
- if(j!=(*i)->variables.end())
+ map<string, VariableDeclaration *>::iterator i = block->variables.find(var.name);
+ if(i!=block->variables.end())
+ var.declaration = i->second;
+ else
{
- var.declaration = j->second;
- type = j->second->type_declaration;
+ const map<string, InterfaceBlock *> &ifaces = block->interfaces;
+ for(map<string, InterfaceBlock *>::const_iterator j=ifaces.begin(); (!var.declaration && j!=ifaces.end()); ++j)
+ if(j->second->instance_name.empty())
+ {
+ i = j->second->members.variables.find(var.name);
+ if(i!=j->second->members.variables.end())
+ var.declaration = i->second;
+ }
+ }
+
+ if(var.declaration)
+ {
+ if(var.declaration->type_declaration)
+ members = &var.declaration->type_declaration->members.variables;
+ break;
+ }
+
+ map<string, InterfaceBlock *>::iterator j = block->interfaces.find(var.name);
+ if(j!=block->interfaces.end() && j->second->instance_name==var.name)
+ {
+ iface_ref = new InterfaceBlockReference;
+ iface_ref->name = var.name;
+ iface_ref->declaration = j->second;
+ members = &j->second->members.variables;
break;
}
}
self_referencing = true;
}
+void VariableResolver::visit(InterfaceBlockReference &iface)
+{
+ iface.declaration = 0;
+ for(Block *block=current_block; block; block=next_block(*block))
+ {
+ map<string, InterfaceBlock *>::iterator j = block->interfaces.find(iface.name);
+ if(j!=block->interfaces.end())
+ {
+ iface.declaration = j->second;
+ members = &j->second->members.variables;
+ break;
+ }
+ }
+}
+
void VariableResolver::visit(MemberAccess &memacc)
{
- type = 0;
- TraversingVisitor::visit(memacc);
+ members = 0;
+ iface_ref = 0;
+ memacc.left->visit(*this);
+
+ if(iface_ref)
+ memacc.left = iface_ref;
+ iface_ref = 0;
+
memacc.declaration = 0;
- if(type)
+ if(members)
{
- map<string, VariableDeclaration *>::iterator i = type->members.variables.find(memacc.member);
- if(i!=type->members.variables.end())
+ map<string, VariableDeclaration *>::iterator i = members->find(memacc.member);
+ if(i!=members->end())
{
memacc.declaration = i->second;
- type = i->second->type_declaration;
+ if(i->second->type_declaration)
+ members = &i->second->type_declaration->members.variables;
}
else
- type = 0;
+ members = 0;
}
}
SetForScope<bool> set(record_target, false);
binary.right->visit(*this);
}
- type = 0;
+ members = 0;
+ iface_ref = 0;
binary.left->visit(*this);
+ if(iface_ref)
+ binary.left = iface_ref;
+ iface_ref = 0;
}
else
{
TraversingVisitor::visit(binary);
- type = 0;
+ members = 0;
}
}
void VariableResolver::visit(StructDeclaration &strct)
{
TraversingVisitor::visit(strct);
- blocks.back()->types[strct.name] = &strct;
+ current_block->types[strct.name] = &strct;
}
void VariableResolver::visit(VariableDeclaration &var)
{
- for(vector<Block *>::iterator i=blocks.end(); i!=blocks.begin(); )
+ for(Block *block=current_block; block; block=next_block(*block))
{
- --i;
- map<string, StructDeclaration *>::iterator j = (*i)->types.find(var.type);
- if(j!=(*i)->types.end())
+ map<string, StructDeclaration *>::iterator j = block->types.find(var.type);
+ if(j!=block->types.end())
var.type_declaration = j->second;
}
var.interface = block_interface;
TraversingVisitor::visit(var);
- blocks.back()->variables[var.name] = &var;
- if(anonymous && blocks.size()>1)
- blocks[blocks.size()-2]->variables[var.name] = &var;
+ current_block->variables[var.name] = &var;
}
void VariableResolver::visit(InterfaceBlock &iface)
{
- SetFlag set(anonymous);
- SetForScope<string> set2(block_interface, iface.interface);
+ SetForScope<string> set_iface(block_interface, iface.interface);
TraversingVisitor::visit(iface);
}
InterfaceGenerator::InterfaceGenerator():
stage(0),
- scope_level(0)
+ function_scope(false),
+ iface_block(0)
{ }
string InterfaceGenerator::get_out_prefix(Stage::Type type)
void InterfaceGenerator::apply(Stage &s)
{
- SetForScope<Stage *> set(stage, &s);
+ stage = &s;
if(stage->previous)
in_prefix = get_out_prefix(stage->previous->type);
out_prefix = get_out_prefix(stage->type);
- visit(s.content);
+ s.content.visit(*this);
+ NodeRemover().apply(s, nodes_to_remove);
}
void InterfaceGenerator::visit(Block &block)
{
- SetForScope<unsigned> set(scope_level, scope_level+1);
- for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); )
+ SetForScope<Block *> set_block(current_block, &block);
+ for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
{
- (*i)->visit(*this);
+ assignment_insert_point = i;
+ if(&block==&stage->content)
+ iface_insert_point = i;
- if(scope_level==1)
- {
- for(map<string, RefPtr<VariableDeclaration> >::iterator j=iface_declarations.begin(); j!=iface_declarations.end(); ++j)
- {
- NodeList<Statement>::iterator k = block.body.insert(i, j->second);
- (*k)->visit(*this);
- }
- iface_declarations.clear();
- }
-
- apply_and_increment(block, i);
+ (*i)->visit(*this);
}
}
bool InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
{
- const map<string, VariableDeclaration *> &stage_vars = (iface=="in" ? stage->in_variables : stage->out_variables);
- if(stage_vars.count(name) || iface_declarations.count(name))
+ if(stage->content.variables.count(name))
return false;
VariableDeclaration* iface_var = new VariableDeclaration;
if(iface_var->array)
iface_var->array_size = var.array_size;
if(iface=="in")
+ {
iface_var->linked_declaration = &var;
- iface_declarations[name] = iface_var;
+ var.linked_declaration = iface_var;
+ }
+ stage->content.body.insert(iface_insert_point, iface_var);
+ stage->content.variables[name] = iface_var;
return true;
}
ExpressionStatement *stmt = new ExpressionStatement;
stmt->expression = assign;
+ current_block->body.insert(assignment_insert_point, stmt);
stmt->visit(*this);
- insert_nodes.push_back(stmt);
return *stmt;
}
{
if(var.declaration || !stage->previous)
return;
- if(iface_declarations.count(var.name))
+ /* Don't pull a variable from previous stage if we just generated an out
+ interface in this stage */
+ if(stage->content.variables.count(var.name))
return;
- const map<string, VariableDeclaration *> &prev_out = stage->previous->out_variables;
- map<string, VariableDeclaration *>::const_iterator i = prev_out.find(var.name);
- if(i==prev_out.end())
- i = prev_out.find(in_prefix+var.name);
- if(i!=prev_out.end())
+ const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
+ map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
+ if(i==prev_vars.end() || i->second->interface!="out")
+ i = prev_vars.find(in_prefix+var.name);
+ if(i!=prev_vars.end() && i->second->interface=="out")
{
generate_interface(*i->second, "in", i->second->name);
var.name = i->second->name;
void InterfaceGenerator::visit(VariableDeclaration &var)
{
+ if(iface_block)
+ {
+ if(iface_block->linked_block)
+ {
+ const map<string, VariableDeclaration *> &linked_vars = iface_block->linked_block->members.variables;
+ map<string, VariableDeclaration *>::const_iterator i = linked_vars.find(var.name);
+ if(i!=linked_vars.end())
+ var.linked_declaration = i->second;
+ }
+ return;
+ }
+
if(var.interface=="out")
{
- if(scope_level==1)
- stage->out_variables[var.name] = &var;
- else if(generate_interface(var, "out", change_prefix(var.name, string())))
+ /* For out variables in function scope, generate a global interface and
+ replace the local declaration with an assignment. */
+ if(function_scope && generate_interface(var, "out", var.name))
{
- remove_node = true;
+ nodes_to_remove.insert(&var);
if(var.init_expression)
{
ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
}
else if(var.interface=="in")
{
- stage->in_variables[var.name] = &var;
- if(var.linked_declaration)
- var.linked_declaration->linked_declaration = &var;
- else if(stage->previous)
+ /* Try to link in variables in global scope with out variables from
+ previous stage */
+ if(current_block==&stage->content && !var.linked_declaration && stage->previous)
{
- const map<string, VariableDeclaration *> &prev_out = stage->previous->out_variables;
- map<string, VariableDeclaration *>::const_iterator i = prev_out.find(var.name);
- if(i!=prev_out.end())
+ const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
+ map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
+ if(i!=prev_vars.end() && i->second->interface=="out")
{
var.linked_declaration = i->second;
i->second->linked_declaration = &var;
TraversingVisitor::visit(var);
}
+void InterfaceGenerator::visit(InterfaceBlock &iface)
+{
+ if(iface.interface=="in")
+ {
+ if(!iface.linked_block && stage->previous)
+ {
+ const map<string, InterfaceBlock *> &prev_blocks = stage->previous->content.interfaces;
+ map<string, InterfaceBlock *>::const_iterator i = prev_blocks.find(iface.name);
+ if(i!=prev_blocks.end() && i->second->interface=="out" && i->second->name==iface.name)
+ {
+ iface.linked_block = i->second;
+ i->second->linked_block = &iface;
+ }
+ }
+ }
+
+ SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
+ TraversingVisitor::visit(iface);
+}
+
+void InterfaceGenerator::visit(FunctionDeclaration &func)
+{
+ SetFlag set_scope(function_scope, true);
+ // Skip parameters because they're not useful here
+ func.body.visit(*this);
+}
+
void InterfaceGenerator::visit(Passthrough &pass)
{
vector<VariableDeclaration *> pass_vars;
- for(map<string, VariableDeclaration *>::const_iterator i=stage->in_variables.begin(); i!=stage->in_variables.end(); ++i)
- pass_vars.push_back(i->second);
- for(map<string, RefPtr<VariableDeclaration> >::const_iterator i=iface_declarations.begin(); i!=iface_declarations.end(); ++i)
+ for(map<string, VariableDeclaration *>::const_iterator i=stage->content.variables.begin(); i!=stage->content.variables.end(); ++i)
if(i->second->interface=="in")
- pass_vars.push_back(i->second.get());
+ pass_vars.push_back(i->second);
if(stage->previous)
{
- const map<string, VariableDeclaration *> &prev_out = stage->previous->out_variables;
- for(map<string, VariableDeclaration *>::const_iterator i=prev_out.begin(); i!=prev_out.end(); ++i)
+ const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
+ for(map<string, VariableDeclaration *>::const_iterator i=prev_vars.begin(); i!=prev_vars.end(); ++i)
{
bool linked = false;
for(vector<VariableDeclaration *>::const_iterator j=pass_vars.begin(); (!linked && j!=pass_vars.end()); ++j)
if(stage->type==Stage::GEOMETRY)
{
- VariableReference *ref = new VariableReference;
+ InterfaceBlockReference *ref = new InterfaceBlockReference;
ref->name = "gl_in";
BinaryExpression *subscript = new BinaryExpression;
insert_assignment(out_name, ref);
}
- remove_node = true;
+ nodes_to_remove.insert(&pass);
}
DeclarationReorderer::DeclarationReorderer():
- scope_level(0),
kind(NO_DECLARATION)
{ }
void DeclarationReorderer::visit(Block &block)
{
- SetForScope<unsigned> set(scope_level, scope_level+1);
- if(scope_level>1)
+ if(block.parent)
return TraversingVisitor::visit(block);
NodeList<Statement>::iterator struct_insert_point = block.body.end();