to become unused. */
bool any_removed = UnusedVariableRemover().apply(stage);
any_removed |= UnusedFunctionRemover().apply(stage);
+ any_removed |= UnusedTypeRemover().apply(stage);
return any_removed ? REDO_PREVIOUS : any_inlined ? REDO_STAGE : NEXT_STAGE;
}
{ }
+bool UnusedTypeRemover::apply(Stage &stage)
+{
+ stage.content.visit(*this);
+ NodeRemover().apply(stage, unused_nodes);
+ return !unused_nodes.empty();
+}
+
+void UnusedTypeRemover::visit(BasicTypeDeclaration &type)
+{
+ if(type.base_type)
+ unused_nodes.erase(type.base_type);
+ unused_nodes.insert(&type);
+}
+
+void UnusedTypeRemover::visit(ImageTypeDeclaration &type)
+{
+ if(type.base_type)
+ unused_nodes.erase(type.base_type);
+ unused_nodes.insert(&type);
+}
+
+void UnusedTypeRemover::visit(StructDeclaration &strct)
+{
+ unused_nodes.insert(&strct);
+}
+
+void UnusedTypeRemover::visit(VariableDeclaration &var)
+{
+ unused_nodes.erase(var.type_declaration);
+}
+
+
UnusedVariableRemover::UnusedVariableRemover():
aggregate(0),
r_assignment(0),
void UnusedVariableRemover::visit(StructDeclaration &strct)
{
SetForScope<Node *> set(aggregate, &strct);
- unused_nodes.insert(&strct);
TraversingVisitor::visit(strct);
}
if(var.init_expression)
record_assignment(var, *var.init_expression, false);
}
- unused_nodes.erase(var.type_declaration);
TraversingVisitor::visit(var);
}
virtual void visit(Iteration &);
};
+/** Removes types which are not used anywhere. */
+class UnusedTypeRemover: private TraversingVisitor
+{
+private:
+ std::set<Node *> unused_nodes;
+
+public:
+ bool apply(Stage &);
+
+private:
+ virtual void visit(BasicTypeDeclaration &);
+ virtual void visit(ImageTypeDeclaration &);
+ virtual void visit(StructDeclaration &);
+ virtual void visit(VariableDeclaration &);
+};
+
/** Removes variable declarations with no references to them. Assignment
statements where the result is not used are also removed. */
class UnusedVariableRemover: private TraversingVisitor