Blocks with instance names are now properly supported in the syntax tree.
for(std::map<string, VariableDeclaration *>::const_iterator i=block.variables.begin(); i!=block.variables.end(); ++i)
append(format("Variable: %%%d %s %s", get_label(*i->second), i->second->type, i->first));
- for(std::set<InterfaceBlock *>::const_iterator i=block.interfaces.begin(); i!=block.interfaces.end(); ++i)
- append(format("Interface: %%%d %s %s", get_label(*i->second), i->second->interface, i->second->name));
+ set<InterfaceBlock *> seen_interfaces;
+ for(std::map<string, InterfaceBlock *>::const_iterator i=block.interfaces.begin(); i!=block.interfaces.end(); ++i)
+ if(seen_interfaces.insert(i->second).second)
+ {
+ string text = format("Interface: %%%d %s %s", get_label(*i->second), i->second->interface, i->second->name);
+ if(!i->second->instance_name.empty())
+ text += format(" %s", i->second->instance_name);
+ append(text);
+ }
bool labeled_body = (!block.types.empty() || !block.variables.empty());
if(labeled_body)
string text;
if(var.declaration)
text += format("%%%d ", get_label(*var.declaration));
- text += var.name;
+ text += format("%s (var)", var.name);
+ append(text);
+}
+
+void DumpTree::visit(InterfaceBlockReference &iface)
+{
+ string text;
+ if(iface.declaration)
+ text += format("%%%d ", get_label(*iface.declaration));
+ text += format("%s (iface)", iface.name);
append(text);
}
void DumpTree::visit(InterfaceBlock &block)
{
- annotated_branch(format("%s %s", block.interface, block.name), block.members);
+ string head;
+ if(!block.instance_name.empty())
+ head += format("%%%d ", get_label(block));
+ head += format("%s %s", block.interface, block.name);
+ if(!block.instance_name.empty())
+ head += format(" %s", block.instance_name);
+ if(block.array)
+ head += "[]";
+ if(block.linked_block)
+ head += " (linked)";
+ annotated_branch(head, block.members);
}
void DumpTree::visit(FunctionDeclaration &func)
virtual void visit(Literal &);
virtual void visit(ParenthesizedExpression &);
virtual void visit(VariableReference &);
+ virtual void visit(InterfaceBlockReference &);
virtual void visit(MemberAccess &);
virtual void visit(UnaryExpression &);
virtual void visit(BinaryExpression &);
void BlockResolver::visit(InterfaceBlock &iface)
{
- current_block->interfaces.insert(&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():
+ builtins(0),
+ members(0),
record_target(false),
assignment_target(0),
self_referencing(false)
void VariableResolver::visit(VariableReference &var)
{
var.declaration = 0;
- type = 0;
+ members = 0;
for(Block *block=current_block; block; block=next_block(*block))
{
map<string, VariableDeclaration *>::iterator i = block->variables.find(var.name);
var.declaration = i->second;
else
{
- const set<InterfaceBlock *> &ifaces = block->interfaces;
- for(set<InterfaceBlock *>::const_iterator j=ifaces.begin(); (!var.declaration && j!=ifaces.end()); ++j)
- {
- i = (*j)->members.variables.find(var.name);
- if(i!=(*j)->members.variables.end())
- var.declaration = i->second;
- }
+ 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)
{
- type = var.declaration->type_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;
}
}
InterfaceGenerator::InterfaceGenerator():
stage(0),
- function_scope(false)
+ function_scope(false),
+ iface_block(0)
{ }
string InterfaceGenerator::get_out_prefix(Stage::Type type)
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")
{
/* For out variables in function scope, generate a global interface and
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);
if(stage->type==Stage::GEOMETRY)
{
- VariableReference *ref = new VariableReference;
+ InterfaceBlockReference *ref = new InterfaceBlockReference;
ref->name = "gl_in";
BinaryExpression *subscript = new BinaryExpression;
{
private:
Block *builtins;
- StructDeclaration *type;
+ std::map<std::string, VariableDeclaration *> *members;
+ RefPtr<InterfaceBlockReference> iface_ref;
std::string block_interface;
bool record_target;
VariableDeclaration *assignment_target;
virtual void enter(Block &);
virtual void visit(VariableReference &);
+ virtual void visit(InterfaceBlockReference &);
virtual void visit(MemberAccess &);
virtual void visit(BinaryExpression &);
virtual void visit(Assignment &);
std::string in_prefix;
std::string out_prefix;
bool function_scope;
+ InterfaceBlock *iface_block;
NodeList<Statement>::iterator iface_insert_point;
NodeList<Statement>::iterator assignment_insert_point;
std::set<Node *> nodes_to_remove;
ExpressionStatement &insert_assignment(const std::string &, Expression *);
virtual void visit(VariableReference &);
virtual void visit(VariableDeclaration &);
+ virtual void visit(InterfaceBlock &);
virtual void visit(FunctionDeclaration &);
virtual void visit(Passthrough &);
};
}
}
+void UnusedVariableRemover::visit(InterfaceBlockReference &iface)
+{
+ unused_nodes.erase(iface.declaration);
+}
+
void UnusedVariableRemover::visit(MemberAccess &memacc)
{
TraversingVisitor::visit(memacc);
private:
virtual void visit(VariableReference &);
+ virtual void visit(InterfaceBlockReference &);
virtual void visit(MemberAccess &);
virtual void visit(BinaryExpression &);
virtual void visit(Assignment &);
append(var.name);
}
+void Formatter::visit(InterfaceBlockReference &iface)
+{
+ append(iface.name);
+}
+
void Formatter::visit(MemberAccess &memacc)
{
memacc.left->visit(*this);
SetForScope<string> set(block_interface, iface.interface);
append(format("%s %s\n", iface.interface, iface.name));
iface.members.visit(*this);
+ if(!iface.instance_name.empty())
+ {
+ append(' ');
+ append(iface.instance_name);
+ if(iface.array)
+ append("[]");
+ }
append(';');
}
virtual void visit(Literal &);
virtual void visit(ParenthesizedExpression &);
virtual void visit(VariableReference &);
+ virtual void visit(InterfaceBlockReference &);
virtual void visit(MemberAccess &);
virtual void visit(UnaryExpression &);
virtual void visit(BinaryExpression &);
}
+InterfaceBlockReference::InterfaceBlockReference():
+ declaration(0)
+{ }
+
+InterfaceBlockReference::InterfaceBlockReference(const InterfaceBlockReference &other):
+ name(other.name),
+ declaration(0)
+{ }
+
+void InterfaceBlockReference::visit(NodeVisitor &visitor)
+{
+ visitor.visit(*this);
+}
+
+
MemberAccess::MemberAccess():
declaration(0)
{ }
InterfaceBlock::InterfaceBlock():
- array(false)
+ array(false),
+ linked_block(0)
{
members.use_braces = true;
}
+InterfaceBlock::InterfaceBlock(const InterfaceBlock &other):
+ interface(other.interface),
+ name(other.name),
+ members(other.members),
+ instance_name(other.instance_name),
+ array(other.array),
+ linked_block(0)
+{ }
+
void InterfaceBlock::visit(NodeVisitor &visitor)
{
visitor.visit(*this);
bool use_braces;
std::map<std::string, StructDeclaration *> types;
std::map<std::string, VariableDeclaration *> variables;
- std::set<InterfaceBlock *> interfaces;
+ std::map<std::string, InterfaceBlock *> interfaces;
Block *parent;
Block();
virtual void visit(NodeVisitor &);
};
+struct InterfaceBlockReference: Expression
+{
+ std::string name;
+ InterfaceBlock *declaration;
+
+ InterfaceBlockReference();
+ InterfaceBlockReference(const InterfaceBlockReference &);
+
+ virtual InterfaceBlockReference *clone() const { return new InterfaceBlockReference(*this); }
+ virtual void visit(NodeVisitor &);
+};
+
struct MemberAccess: Expression
{
NodePtr<Expression> left;
Block members;
std::string instance_name;
bool array;
+ InterfaceBlock *linked_block;
InterfaceBlock();
+ InterfaceBlock(const InterfaceBlock &);
virtual InterfaceBlock *clone() const { return new InterfaceBlock(*this); }
virtual void visit(NodeVisitor &);
s.content.visit(*this);
}
-void NodeRemover::remove_variable(map<string, VariableDeclaration *> &vars, VariableDeclaration &decl)
+template<typename T>
+void NodeRemover::remove_from_map(map<string, T *> &vars, const string &key, T &node)
{
- map<string, VariableDeclaration *>::iterator i = vars.find(decl.name);
- if(i!=vars.end() && i->second==&decl)
+ typename map<string, T *>::iterator i = vars.find(key);
+ if(i!=vars.end() && i->second==&node)
vars.erase(i);
}
void NodeRemover::visit(StructDeclaration &strct)
{
if(to_remove->count(&strct))
- current_block->types.erase(strct.name);
+ remove_from_map(current_block->types, strct.name, strct);
}
void NodeRemover::visit(VariableDeclaration &var)
{
if(recursive_remove || to_remove->count(&var))
{
- remove_variable(current_block->variables, var);
+ remove_from_map(current_block->variables, var.name, var);
stage->locations.erase(var.name);
if(var.linked_declaration)
var.linked_declaration->linked_declaration = 0;
void NodeRemover::visit(InterfaceBlock &iface)
{
if(to_remove->count(&iface))
- current_block->interfaces.erase(&iface);
+ {
+ remove_from_map(current_block->interfaces, iface.name, iface);
+ if(!iface.instance_name.empty())
+ remove_from_map(current_block->interfaces, iface.instance_name, iface);
+ }
SetFlag set_recursive(recursive_remove, recursive_remove || to_remove->count(&iface));
TraversingVisitor::visit(iface);
}
virtual void visit(Literal &) { }
virtual void visit(ParenthesizedExpression &) { }
virtual void visit(VariableReference &) { }
+ virtual void visit(InterfaceBlockReference &) { }
virtual void visit(MemberAccess &) { }
virtual void visit(UnaryExpression &) { }
virtual void visit(BinaryExpression &) { }
void apply(Stage &, const std::set<Node *> &);
private:
- void remove_variable(std::map<std::string, VariableDeclaration *> &, VariableDeclaration &);
+ template<typename T>
+ void remove_from_map(std::map<std::string, T *> &, const std::string &, T &);
virtual void visit(Block &);
virtual void visit(StructDeclaration &);