remover.to_remove = unused_locator.unused_nodes;
remover.apply(stage);
- return remover.n_removed;
+ return !unused_locator.unused_nodes.empty();
}
void ProgramCompiler::inject_block(Block &target, const Block &source)
ProgramCompiler::UnusedVariableLocator::UnusedVariableLocator():
+ aggregate(0),
assignment(false),
record_target(false),
assignment_target(0),
if(assignment && var.declaration==assignment_target)
self_referencing = true;
}
+
+ map<VariableDeclaration *, Node *>::iterator i = aggregates.find(var.declaration);
+ if(i!=aggregates.end())
+ unused_nodes.erase(i->second);
}
void ProgramCompiler::UnusedVariableLocator::visit(MemberAccess &memacc)
assignment = false;
}
+void ProgramCompiler::UnusedVariableLocator::visit(StructDeclaration &strct)
+{
+ SetForScope<Node *> set(aggregate, &strct);
+ unused_nodes.insert(&strct);
+ TraversingVisitor::visit(strct);
+}
+
void ProgramCompiler::UnusedVariableLocator::visit(VariableDeclaration &var)
{
- unused_nodes.insert(&var);
- if(var.init_expression)
+ if(aggregate)
+ aggregates[&var] = aggregate;
+ else
{
- unused_nodes.insert(&*var.init_expression);
- assignments[&var] = &*var.init_expression;
+ unused_nodes.insert(&var);
+ if(var.init_expression)
+ {
+ unused_nodes.insert(&*var.init_expression);
+ assignments[&var] = &*var.init_expression;
+ }
}
+ unused_nodes.erase(var.type_declaration);
TraversingVisitor::visit(var);
}
+void ProgramCompiler::UnusedVariableLocator::visit(InterfaceBlock &iface)
+{
+ SetForScope<Node *> set(aggregate, &iface);
+ unused_nodes.insert(&iface);
+ TraversingVisitor::visit(iface);
+}
-ProgramCompiler::NodeRemover::NodeRemover():
- n_removed(0),
- immutable_block(false),
- remove_block(false)
-{ }
void ProgramCompiler::NodeRemover::visit(Block &block)
{
- remove_block = immutable_block;
for(list<NodePtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); )
{
- bool remove = to_remove.count(&**i);
- if(!remove)
- remove_block = false;
(*i)->visit(*this);
-
- if(remove ? !immutable_block : remove_block)
- {
+ if(to_remove.count(&**i))
block.body.erase(i++);
- ++n_removed;
- }
else
++i;
}
}
-void ProgramCompiler::NodeRemover::visit(StructDeclaration &strct)
-{
- SetFlag set(immutable_block);
- TraversingVisitor::visit(strct);
-}
-
void ProgramCompiler::NodeRemover::visit(VariableDeclaration &var)
{
if(to_remove.count(&var))
var.init_expression = 0;
}
-void ProgramCompiler::NodeRemover::visit(InterfaceBlock &iface)
-{
- SetFlag set(immutable_block);
- TraversingVisitor::visit(iface);
-}
-
} // namespace GL
} // namespace Msp
struct UnusedVariableLocator: Visitor
{
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;
virtual void visit(ProgramSyntax::MemberAccess &);
virtual void visit(ProgramSyntax::BinaryExpression &);
virtual void visit(ProgramSyntax::ExpressionStatement &);
+ virtual void visit(ProgramSyntax::StructDeclaration &);
virtual void visit(ProgramSyntax::VariableDeclaration &);
+ virtual void visit(ProgramSyntax::InterfaceBlock &);
};
struct NodeRemover: Visitor
{
std::set<ProgramSyntax::Node *> to_remove;
- unsigned n_removed;
- bool immutable_block;
- bool remove_block;
-
- NodeRemover();
virtual void visit(ProgramSyntax::Block &);
- virtual void visit(ProgramSyntax::StructDeclaration &);
virtual void visit(ProgramSyntax::VariableDeclaration &);
- virtual void visit(ProgramSyntax::InterfaceBlock &);
};
ProgramParser parser;