{
inject_block(stage.content, module->shared.content);
+ apply<FunctionResolver>(stage);
apply<VariableResolver>(stage);
apply<InterfaceGenerator>(stage);
apply<VariableResolver>(stage);
apply<VariableResolver>(stage);
set<Node *> unused = apply<UnusedVariableLocator>(stage);
+ set<Node *> unused2 = apply<UnusedFunctionLocator>(stage);
+ unused.insert(unused2.begin(), unused2.end());
apply<NodeRemover>(stage, unused);
return !unused.empty();
}
+void ProgramCompiler::FunctionResolver::visit(FunctionCall &call)
+{
+ map<string, vector<FunctionDeclaration *> >::iterator i = functions.find(call.name);
+ if(i!=functions.end())
+ call.declaration = i->second.back();
+
+ TraversingVisitor::visit(call);
+}
+
+void ProgramCompiler::FunctionResolver::visit(FunctionDeclaration &func)
+{
+ vector<FunctionDeclaration *> &decls = functions[func.name];
+ if(func.definition)
+ {
+ for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
+ (*i)->definition = func.definition;
+ decls.clear();
+ decls.push_back(&func);
+ }
+ else if(!decls.empty() && decls.back()->definition)
+ func.definition = decls.back()->definition;
+ else
+ decls.push_back(&func);
+
+ TraversingVisitor::visit(func);
+}
+
+
ProgramCompiler::InterfaceGenerator::InterfaceGenerator():
scope_level(0),
remove_node(false)
}
+void ProgramCompiler::UnusedFunctionLocator::visit(FunctionCall &call)
+{
+ TraversingVisitor::visit(call);
+
+ unused_nodes.erase(call.declaration);
+ if(call.declaration && call.declaration->definition!=call.declaration)
+ used_definitions.insert(call.declaration->definition);
+}
+
+void ProgramCompiler::UnusedFunctionLocator::visit(FunctionDeclaration &func)
+{
+ TraversingVisitor::visit(func);
+
+ if(func.name!="main" && !used_definitions.count(&func))
+ unused_nodes.insert(&func);
+}
+
+
ProgramCompiler::NodeRemover::NodeRemover(const set<Node *> &r):
to_remove(r)
{ }
virtual void visit(ProgramSyntax::InterfaceBlock &);
};
+ struct FunctionResolver: Visitor
+ {
+ std::map<std::string, std::vector<ProgramSyntax::FunctionDeclaration *> > functions;
+
+ virtual void visit(ProgramSyntax::FunctionCall &);
+ virtual void visit(ProgramSyntax::FunctionDeclaration &);
+ };
+
struct InterfaceGenerator: Visitor
{
std::string in_prefix;
virtual void visit(ProgramSyntax::Iteration &);
};
+ struct UnusedFunctionLocator: Visitor
+ {
+ typedef std::set<ProgramSyntax::Node *> ResultType;
+
+ std::set<ProgramSyntax::Node *> unused_nodes;
+ std::set<ProgramSyntax::FunctionDeclaration *> used_definitions;
+
+ const ResultType &get_result() const { return unused_nodes; }
+ virtual void visit(ProgramSyntax::FunctionCall &);
+ virtual void visit(ProgramSyntax::FunctionDeclaration &);
+ };
+
struct NodeRemover: Visitor
{
std::set<ProgramSyntax::Node *> to_remove;