vector<Import *> imports = NodeGatherer<Import>().apply(mod.shared);
for(vector<Import *>::iterator i=imports.begin(); i!=imports.end(); ++i)
import(res, (*i)->module);
- NodeRemover(set<Node *>(imports.begin(), imports.end())).apply(mod.shared);
+ NodeRemover().apply(mod.shared, set<Node *>(imports.begin(), imports.end()));
append_stage(mod.shared);
for(list<Stage>::iterator i=mod.stages.begin(); i!=mod.stages.end(); ++i)
{
ConstantConditionEliminator().apply(stage);
- set<FunctionDeclaration *> inlineable = InlineableFunctionLocator().apply(stage);
- FunctionInliner(inlineable).apply(stage);
+ FunctionInliner().apply(stage);
- set<Node *> unused = UnusedVariableLocator().apply(stage);
- set<Node *> unused2 = UnusedFunctionLocator().apply(stage);
- unused.insert(unused2.begin(), unused2.end());
- NodeRemover(unused).apply(stage);
+ bool result = UnusedVariableRemover().apply(stage);
+ result |= UnusedFunctionRemover().apply(stage);
- return !unused.empty();
+ return result;
}
void Compiler::finalize(Stage &stage)
}
-FunctionInliner::FunctionInliner(const set<FunctionDeclaration *> &in):
- inlineable(in),
+FunctionInliner::FunctionInliner():
extract_result(0)
{ }
+void FunctionInliner::apply(Stage &stage)
+{
+ inlineable = InlineableFunctionLocator().apply(stage);
+ visit(stage.content);
+}
+
void FunctionInliner::visit_and_inline(RefPtr<Expression> &ptr)
{
inline_result = 0;
}
-UnusedVariableLocator::VariableInfo::VariableInfo():
+UnusedVariableRemover::VariableInfo::VariableInfo():
local(false),
conditionally_assigned(false),
referenced(false)
{ }
-UnusedVariableLocator::UnusedVariableLocator():
+UnusedVariableRemover::UnusedVariableRemover():
aggregate(0),
assignment(0),
assignment_target(false),
global_scope(true)
{ }
-const set<Node *> &UnusedVariableRemover::apply(Stage &stage)
+bool UnusedVariableRemover::apply(Stage &stage)
{
variables.push_back(BlockVariableMap());
visit(stage.content);
}
variables.pop_back();
- return unused_nodes;
+ NodeRemover().apply(stage, unused_nodes);
+
+ return !unused_nodes.empty();
}
-void UnusedVariableLocator::visit(VariableReference &var)
+void UnusedVariableRemover::visit(VariableReference &var)
{
map<VariableDeclaration *, Node *>::iterator i = aggregates.find(var.declaration);
if(i!=aggregates.end())
}
}
-void UnusedVariableLocator::visit(MemberAccess &memacc)
+void UnusedVariableRemover::visit(MemberAccess &memacc)
{
TraversingVisitor::visit(memacc);
unused_nodes.erase(memacc.declaration);
}
-void UnusedVariableLocator::visit(BinaryExpression &binary)
+void UnusedVariableRemover::visit(BinaryExpression &binary)
{
if(binary.oper=="[")
{
TraversingVisitor::visit(binary);
}
-void UnusedVariableLocator::visit(Assignment &assign)
+void UnusedVariableRemover::visit(Assignment &assign)
{
{
assign_to_subscript = false;
assignment = &assign;
}
-void UnusedVariableLocator::record_assignment(VariableDeclaration &var, Node &node, bool chained)
+void UnusedVariableRemover::record_assignment(VariableDeclaration &var, Node &node, bool chained)
{
VariableInfo &var_info = variables.back()[&var];
if(!chained)
var_info.conditionally_assigned = false;
}
-void UnusedVariableLocator::clear_assignments(VariableInfo &var_info, bool mark_unused)
+void UnusedVariableRemover::clear_assignments(VariableInfo &var_info, bool mark_unused)
{
if(mark_unused)
{
var_info.assignments.clear();
}
-void UnusedVariableLocator::visit(ExpressionStatement &expr)
+void UnusedVariableRemover::visit(ExpressionStatement &expr)
{
assignment = 0;
TraversingVisitor::visit(expr);
record_assignment(*assignment->target_declaration, expr, (assignment->self_referencing || assign_to_subscript));
}
-void UnusedVariableLocator::visit(StructDeclaration &strct)
+void UnusedVariableRemover::visit(StructDeclaration &strct)
{
SetForScope<Node *> set(aggregate, &strct);
unused_nodes.insert(&strct);
TraversingVisitor::visit(strct);
}
-void UnusedVariableLocator::visit(VariableDeclaration &var)
+void UnusedVariableRemover::visit(VariableDeclaration &var)
{
if(aggregate)
aggregates[&var] = aggregate;
TraversingVisitor::visit(var);
}
-void UnusedVariableLocator::visit(InterfaceBlock &iface)
+void UnusedVariableRemover::visit(InterfaceBlock &iface)
{
SetForScope<Node *> set(aggregate, &iface);
unused_nodes.insert(&iface);
TraversingVisitor::visit(iface);
}
-void UnusedVariableLocator::visit(FunctionDeclaration &func)
+void UnusedVariableRemover::visit(FunctionDeclaration &func)
{
variables.push_back(BlockVariableMap());
merge_down_variables();
}
-void UnusedVariableLocator::merge_down_variables()
+void UnusedVariableRemover::merge_down_variables()
{
BlockVariableMap &parent_variables = variables[variables.size()-2];
BlockVariableMap &block_variables = variables.back();
variables.pop_back();
}
-void UnusedVariableLocator::visit(Conditional &cond)
+void UnusedVariableRemover::visit(Conditional &cond)
{
cond.condition->visit(*this);
variables.push_back(BlockVariableMap());
merge_down_variables();
}
-void UnusedVariableLocator::visit(Iteration &iter)
+void UnusedVariableRemover::visit(Iteration &iter)
{
variables.push_back(BlockVariableMap());
TraversingVisitor::visit(iter);
}
-void UnusedFunctionLocator::visit(FunctionCall &call)
+bool UnusedFunctionRemover::apply(Stage &stage)
+{
+ visit(stage.content);
+ NodeRemover().apply(stage, unused_nodes);
+ return !unused_nodes.empty();
+}
+
+void UnusedFunctionRemover::visit(FunctionCall &call)
{
TraversingVisitor::visit(call);
used_definitions.insert(call.declaration->definition);
}
-void UnusedFunctionLocator::visit(FunctionDeclaration &func)
+void UnusedFunctionRemover::visit(FunctionDeclaration &func)
{
TraversingVisitor::visit(func);
RefPtr<Expression> inline_result;
public:
- FunctionInliner(const std::set<FunctionDeclaration *> &);
+ FunctionInliner();
- void apply(Stage &s) { visit(s.content); }
+ void apply(Stage &);
private:
void visit_and_inline(RefPtr<Expression> &);
using BlockModifier::visit;
};
-class UnusedVariableLocator: private TraversingVisitor
+class UnusedVariableRemover: private TraversingVisitor
{
private:
struct VariableInfo
bool global_scope;
public:
- UnusedVariableLocator();
+ UnusedVariableRemover();
- const std::set<Node *> &apply(Stage &);
+ bool apply(Stage &);
private:
virtual void visit(VariableReference &);
using TraversingVisitor::visit;
};
-class UnusedFunctionLocator: private TraversingVisitor
+class UnusedFunctionRemover: private TraversingVisitor
{
private:
std::set<Node *> unused_nodes;
std::set<FunctionDeclaration *> used_definitions;
public:
- const std::set<Node *> &apply(Stage &s) { visit(s.content); return unused_nodes; }
+ bool apply(Stage &s);
private:
virtual void visit(FunctionCall &);
}
-NodeRemover::NodeRemover(const set<Node *> &r):
+NodeRemover::NodeRemover():
stage(0),
- to_remove(r)
+ to_remove(0)
{ }
-void NodeRemover::apply(Stage &s)
+void NodeRemover::apply(Stage &s, const set<Node *> &tr)
{
stage = &s;
+ to_remove = &tr;
visit(s.content);
}
for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); )
{
(*i)->visit(*this);
- if(to_remove.count(i->get()))
+ if(to_remove->count(i->get()))
block.body.erase(i++);
else
++i;
void NodeRemover::visit(VariableDeclaration &var)
{
- if(to_remove.count(&var))
+ if(to_remove->count(&var))
{
stage->in_variables.erase(var.name);
stage->out_variables.erase(var.name);
if(var.linked_declaration)
var.linked_declaration->linked_declaration = 0;
}
- else if(var.init_expression && to_remove.count(var.init_expression.get()))
+ else if(var.init_expression && to_remove->count(var.init_expression.get()))
var.init_expression = 0;
}
void NodeRemover::visit(Iteration &iter)
{
- if(to_remove.count(iter.init_statement.get()))
+ if(to_remove->count(iter.init_statement.get()))
iter.init_statement = 0;
TraversingVisitor::visit(iter);
}