void ProgramCompiler::Formatter::visit(BinaryExpression &binary)
{
binary.left->visit(*this);
- if(binary.assignment)
- formatted += format(" %s ", binary.oper);
- else
- formatted += binary.oper;
+ formatted += binary.oper;
binary.right->visit(*this);
formatted += binary.after;
}
+void ProgramCompiler::Formatter::visit(Assignment &assign)
+{
+ assign.left->visit(*this);
+ formatted += format(" %s ", assign.oper);
+ assign.right->visit(*this);
+}
+
void ProgramCompiler::Formatter::visit(FunctionCall &call)
{
formatted += format("%s(", call.name);
ProgramCompiler::VariableResolver::VariableResolver():
- anonymous(false)
+ anonymous(false),
+ record_target(false),
+ assignment_target(0),
+ self_referencing(false)
{ }
void ProgramCompiler::VariableResolver::apply(Stage &s)
break;
}
}
+
+ if(record_target)
+ {
+ if(assignment_target)
+ {
+ record_target = false;
+ assignment_target = 0;
+ }
+ else
+ assignment_target = var.declaration;
+ }
+ else if(var.declaration && var.declaration==assignment_target)
+ self_referencing = true;
}
void ProgramCompiler::VariableResolver::visit(MemberAccess &memacc)
{
if(binary.oper=="[")
{
- binary.right->visit(*this);
+ {
+ SetForScope<bool> set(record_target, false);
+ binary.right->visit(*this);
+ }
type = 0;
binary.left->visit(*this);
}
}
}
+void ProgramCompiler::VariableResolver::visit(Assignment &assign)
+{
+ {
+ SetFlag set(record_target);
+ assignment_target = 0;
+ assign.left->visit(*this);
+ }
+
+ self_referencing = false;
+ assign.right->visit(*this);
+
+ assign.self_referencing = (self_referencing || assign.oper!="=");
+ assign.target_declaration = assignment_target;
+}
+
void ProgramCompiler::VariableResolver::visit(StructDeclaration &strct)
{
TraversingVisitor::visit(strct);
void ProgramCompiler::InterfaceGenerator::insert_assignment(const string &left, ProgramSyntax::Expression *right)
{
- BinaryExpression *assign = new BinaryExpression;
+ Assignment *assign = new Assignment;
VariableReference *ref = new VariableReference;
ref->name = left;
assign->left = ref;
assign->oper = "=";
assign->right = right;
- assign->assignment = true;
ExpressionStatement *stmt = new ExpressionStatement;
stmt->expression = assign;
ProgramCompiler::UnusedVariableLocator::UnusedVariableLocator():
aggregate(0),
- assignment(false),
- record_target(false),
- assignment_target(0),
- indeterminate_target(false),
- self_referencing(false)
+ assignment(0),
+ assignment_target(false)
{ }
-void ProgramCompiler::UnusedVariableLocator::visit(VariableReference &var)
+void ProgramCompiler::UnusedVariableLocator::apply(Stage &s)
{
- if(record_target)
- {
- if(assignment_target)
- indeterminate_target = true;
- else
- assignment_target = var.declaration;
- }
- else
- {
- unused_nodes.erase(var.declaration);
-
- map<VariableDeclaration *, Node *>::iterator i = assignments.find(var.declaration);
- if(i!=assignments.end())
- {
- unused_nodes.erase(i->second);
- assignments.erase(i);
- }
+ assignments.push_back(BlockAssignmentMap());
+ Visitor::apply(s);
+ assignments.pop_back();
+}
- if(assignment && var.declaration==assignment_target)
- self_referencing = true;
- }
+void ProgramCompiler::UnusedVariableLocator::visit(VariableReference &var)
+{
+ unused_nodes.erase(var.declaration);
map<VariableDeclaration *, Node *>::iterator i = aggregates.find(var.declaration);
if(i!=aggregates.end())
unused_nodes.erase(i->second);
+
+ if(assignment_target)
+ return;
+
+ for(vector<BlockAssignmentMap>::iterator j=assignments.end(); j!=assignments.begin(); )
+ {
+ --j;
+ BlockAssignmentMap::iterator k = j->find(var.declaration);
+ if(k!=j->end())
+ {
+ for(vector<Node *>::iterator l=k->second.nodes.begin(); l!=k->second.nodes.end(); ++l)
+ unused_nodes.erase(*l);
+ j->erase(k);
+ break;
+ }
+ }
}
void ProgramCompiler::UnusedVariableLocator::visit(MemberAccess &memacc)
void ProgramCompiler::UnusedVariableLocator::visit(BinaryExpression &binary)
{
- if(binary.assignment)
- {
- assignment = true;
- {
- SetFlag set(record_target);
- binary.left->visit(*this);
- }
- if(binary.oper!="=")
- self_referencing = true;
- binary.right->visit(*this);
- }
- else if(record_target && binary.oper=="[")
+ if(binary.oper=="[")
{
binary.left->visit(*this);
- SetForScope<bool> set(record_target, false);
+ SetForScope<bool> set(assignment_target, false);
binary.right->visit(*this);
}
else
TraversingVisitor::visit(binary);
}
+void ProgramCompiler::UnusedVariableLocator::visit(Assignment &assign)
+{
+ {
+ SetForScope<bool> set(assignment_target, !assign.self_referencing);
+ assign.left->visit(*this);
+ }
+ assign.right->visit(*this);
+ assignment = &assign;
+}
+
+void ProgramCompiler::UnusedVariableLocator::record_assignment(VariableDeclaration &var, Node &node, bool self_ref)
+{
+ unused_nodes.insert(&node);
+ BlockAssignmentMap &block_assignments = assignments.back();
+ AssignmentList &var_assignments = block_assignments[&var];
+ if(!self_ref)
+ var_assignments.nodes.clear();
+ var_assignments.nodes.push_back(&node);
+ var_assignments.conditional = false;
+ var_assignments.self_referencing = self_ref;
+}
+
void ProgramCompiler::UnusedVariableLocator::visit(ExpressionStatement &expr)
{
- assignment = false;
- assignment_target = 0;
- indeterminate_target = false;
- self_referencing = false;
+ assignment = 0;
TraversingVisitor::visit(expr);
- if(assignment && assignment_target && !indeterminate_target)
- {
- Node *&assign = assignments[assignment_target];
- if(self_referencing)
- unused_nodes.erase(assign);
- else if(assign)
- unused_nodes.insert(assign);
- assign = &expr;
- if(assignment_target->interface=="out" && (stage->type==FRAGMENT || assignment_target->linked_declaration))
- unused_nodes.erase(assignment_target);
- else
- unused_nodes.insert(&expr);
- }
- assignment = false;
+ if(assignment && assignment->target_declaration)
+ record_assignment(*assignment->target_declaration, expr, assignment->self_referencing);
}
void ProgramCompiler::UnusedVariableLocator::visit(StructDeclaration &strct)
{
unused_nodes.insert(&var);
if(var.init_expression)
- {
- unused_nodes.insert(&*var.init_expression);
- assignments[&var] = &*var.init_expression;
- }
+ record_assignment(var, *var.init_expression, false);
}
unused_nodes.erase(var.type_declaration);
TraversingVisitor::visit(var);
TraversingVisitor::visit(iface);
}
+void ProgramCompiler::UnusedVariableLocator::visit(FunctionDeclaration &func)
+{
+ assignments.push_back(BlockAssignmentMap());
+
+ for(vector<NodePtr<VariableDeclaration> >::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
+ (*i)->visit(*this);
+ func.body.visit(*this);
+
+ BlockAssignmentMap &block_assignments = assignments.back();
+ for(map<string, VariableDeclaration *>::iterator i=func.body.variables.begin(); i!=func.body.variables.end(); ++i)
+ block_assignments.erase(i->second);
+ for(BlockAssignmentMap::iterator i=block_assignments.begin(); i!=block_assignments.end(); ++i)
+ {
+ if(i->first->interface=="out" && stage->type!=FRAGMENT && !i->first->linked_declaration)
+ continue;
+
+ for(vector<Node *>::iterator j=i->second.nodes.begin(); j!=i->second.nodes.end(); ++j)
+ unused_nodes.erase(*j);
+ }
+
+ assignments.pop_back();
+}
+
+void ProgramCompiler::UnusedVariableLocator::merge_down_assignments()
+{
+ BlockAssignmentMap &parent_assignments = assignments[assignments.size()-2];
+ BlockAssignmentMap &block_assignments = assignments.back();
+ for(BlockAssignmentMap::iterator i=block_assignments.begin(); i!=block_assignments.end(); ++i)
+ {
+ BlockAssignmentMap::iterator j = parent_assignments.find(i->first);
+ if(j==parent_assignments.end())
+ parent_assignments.insert(*i);
+ else if(i->second.self_referencing || i->second.conditional)
+ {
+ j->second.nodes.insert(j->second.nodes.end(), i->second.nodes.begin(), i->second.nodes.end());
+ j->second.conditional |= i->second.conditional;
+ j->second.self_referencing |= i->second.self_referencing;
+ }
+ else
+ j->second = i->second;
+ }
+ assignments.pop_back();
+}
+
+void ProgramCompiler::UnusedVariableLocator::visit(Conditional &cond)
+{
+ cond.condition->visit(*this);
+ assignments.push_back(BlockAssignmentMap());
+ cond.body.visit(*this);
+
+ BlockAssignmentMap if_assignments;
+ swap(assignments.back(), if_assignments);
+ cond.else_body.visit(*this);
+
+ BlockAssignmentMap &else_assignments = assignments.back();
+ for(BlockAssignmentMap::iterator i=else_assignments.begin(); i!=else_assignments.end(); ++i)
+ {
+ BlockAssignmentMap::iterator j = if_assignments.find(i->first);
+ if(j!=if_assignments.end())
+ {
+ i->second.nodes.insert(i->second.nodes.end(), j->second.nodes.begin(), j->second.nodes.end());
+ i->second.conditional |= j->second.conditional;
+ i->second.self_referencing |= j->second.self_referencing;
+ if_assignments.erase(j);
+ }
+ else
+ i->second.conditional = true;
+ }
+
+ for(BlockAssignmentMap::iterator i=if_assignments.begin(); i!=if_assignments.end(); ++i)
+ {
+ i->second.conditional = true;
+ else_assignments.insert(*i);
+ }
+
+ merge_down_assignments();
+}
+
+void ProgramCompiler::UnusedVariableLocator::visit(Iteration &iter)
+{
+ assignments.push_back(BlockAssignmentMap());
+ TraversingVisitor::visit(iter);
+ merge_down_assignments();
+}
+
void ProgramCompiler::NodeRemover::visit(Block &block)
{
virtual void visit(ProgramSyntax::MemberAccess &);
virtual void visit(ProgramSyntax::UnaryExpression &);
virtual void visit(ProgramSyntax::BinaryExpression &);
+ virtual void visit(ProgramSyntax::Assignment &);
virtual void visit(ProgramSyntax::FunctionCall &);
virtual void visit(ProgramSyntax::ExpressionStatement &);
virtual void visit(ProgramSyntax::Layout &);
ProgramSyntax::StructDeclaration *type;
bool anonymous;
std::string block_interface;
+ bool record_target;
+ ProgramSyntax::VariableDeclaration *assignment_target;
+ bool self_referencing;
VariableResolver();
virtual void visit(ProgramSyntax::VariableReference &);
virtual void visit(ProgramSyntax::MemberAccess &);
virtual void visit(ProgramSyntax::BinaryExpression &);
+ virtual void visit(ProgramSyntax::Assignment &);
virtual void visit(ProgramSyntax::StructDeclaration &);
virtual void visit(ProgramSyntax::VariableDeclaration &);
virtual void visit(ProgramSyntax::InterfaceBlock &);
struct UnusedVariableLocator: Visitor
{
+ struct AssignmentList
+ {
+ std::vector<ProgramSyntax::Node *> nodes;
+ bool conditional;
+ bool self_referencing;
+ };
+
+ typedef std::map<ProgramSyntax::VariableDeclaration *, AssignmentList> BlockAssignmentMap;
+
std::set<ProgramSyntax::Node *> unused_nodes;
std::map<ProgramSyntax::VariableDeclaration *, ProgramSyntax::Node *> aggregates;
ProgramSyntax::Node *aggregate;
- std::map<ProgramSyntax::VariableDeclaration *, ProgramSyntax::Node *> assignments;
- bool assignment;
- bool record_target;
- ProgramSyntax::VariableDeclaration *assignment_target;
- bool indeterminate_target;
- bool self_referencing;
+ std::vector<BlockAssignmentMap> assignments;
+ ProgramSyntax::Assignment *assignment;
+ bool assignment_target;
UnusedVariableLocator();
+ virtual void apply(ProgramSyntax::Stage &);
virtual void visit(ProgramSyntax::VariableReference &);
virtual void visit(ProgramSyntax::MemberAccess &);
virtual void visit(ProgramSyntax::BinaryExpression &);
+ virtual void visit(ProgramSyntax::Assignment &);
+ void record_assignment(ProgramSyntax::VariableDeclaration &, ProgramSyntax::Node &, bool);
virtual void visit(ProgramSyntax::ExpressionStatement &);
virtual void visit(ProgramSyntax::StructDeclaration &);
virtual void visit(ProgramSyntax::VariableDeclaration &);
virtual void visit(ProgramSyntax::InterfaceBlock &);
+ virtual void visit(ProgramSyntax::FunctionDeclaration &);
+ void merge_down_assignments();
+ virtual void visit(ProgramSyntax::Conditional &);
+ virtual void visit(ProgramSyntax::Iteration &);
};
struct NodeRemover: Visitor