+#include <msp/core/raii.h>
#include <msp/strings/format.h>
#include <msp/strings/utils.h>
#include "error.h"
void ProgramCompiler::process(Context &context)
{
inject_block(context.content, module->global_context.content);
+
+ VariableResolver resolver;
+ context.content.visit(resolver);
+
+ while(1)
+ {
+ UnusedVariableLocator unused_locator;
+ context.content.visit(unused_locator);
+
+ NodeRemover remover;
+ remover.to_remove.insert(unused_locator.unused_variables.begin(), unused_locator.unused_variables.end());
+ context.content.visit(remover);
+
+ if(!remover.n_removed)
+ break;
+ }
}
void ProgramCompiler::inject_block(Block &target, const Block &source)
void ProgramCompiler::Formatter::visit(FunctionDeclaration &func)
{
formatted += format("%s %s(", func.return_type, func.name);
- parameter_list = true;
for(vector<NodePtr<VariableDeclaration> >::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
{
if(i!=func.parameters.begin())
formatted += ", ";
+ SetFlag set(parameter_list);
(*i)->visit(*this);
}
- parameter_list = false;
formatted += ')';
if(func.definition)
{
if(!cond.else_body.body.empty())
{
formatted += format("\n%selse", string(indent*2, ' '));
- else_if = true;
+ SetFlag set(else_if);
cond.else_body.visit(*this);
- else_if = false;
}
}
formatted += ';';
}
+
+ProgramCompiler::VariableResolver::VariableResolver():
+ anonymous(false)
+{ }
+
+void ProgramCompiler::VariableResolver::visit(Block &block)
+{
+ blocks.push_back(&block);
+ block.variables.clear();
+ TraversingVisitor::visit(block);
+ blocks.pop_back();
+}
+
+void ProgramCompiler::VariableResolver::visit(VariableReference &var)
+{
+ var.declaration = 0;
+ type = 0;
+ for(vector<Block *>::iterator i=blocks.end(); i!=blocks.begin(); )
+ {
+ --i;
+ map<string, VariableDeclaration *>::iterator j = (*i)->variables.find(var.name);
+ if(j!=(*i)->variables.end())
+ {
+ var.declaration = j->second;
+ type = j->second->type_declaration;
+ break;
+ }
+ }
+}
+
+void ProgramCompiler::VariableResolver::visit(MemberAccess &memacc)
+{
+ type = 0;
+ TraversingVisitor::visit(memacc);
+ memacc.declaration = 0;
+ if(type)
+ {
+ map<string, VariableDeclaration *>::iterator i = type->members.variables.find(memacc.member);
+ if(i!=type->members.variables.end())
+ {
+ memacc.declaration = i->second;
+ type = i->second->type_declaration;
+ }
+ else
+ type = 0;
+ }
+}
+
+void ProgramCompiler::VariableResolver::visit(BinaryExpression &binary)
+{
+ if(binary.oper=="[")
+ {
+ binary.right->visit(*this);
+ type = 0;
+ binary.left->visit(*this);
+ }
+ else
+ {
+ TraversingVisitor::visit(binary);
+ type = 0;
+ }
+}
+
+void ProgramCompiler::VariableResolver::visit(StructDeclaration &strct)
+{
+ TraversingVisitor::visit(strct);
+ blocks.back()->types[strct.name] = &strct;
+}
+
+void ProgramCompiler::VariableResolver::visit(VariableDeclaration &var)
+{
+ for(vector<Block *>::iterator i=blocks.end(); i!=blocks.begin(); )
+ {
+ --i;
+ map<string, StructDeclaration *>::iterator j = (*i)->types.find(var.type);
+ if(j!=(*i)->types.end())
+ var.type_declaration = j->second;
+ }
+
+ TraversingVisitor::visit(var);
+ blocks.back()->variables[var.name] = &var;
+ if(anonymous && blocks.size()>1)
+ blocks[blocks.size()-2]->variables[var.name] = &var;
+}
+
+void ProgramCompiler::VariableResolver::visit(InterfaceBlock &iface)
+{
+ SetFlag set(anonymous);
+ TraversingVisitor::visit(iface);
+}
+
+
+void ProgramCompiler::UnusedVariableLocator::visit(VariableReference &var)
+{
+ unused_variables.erase(var.declaration);
+}
+
+void ProgramCompiler::UnusedVariableLocator::visit(MemberAccess &memacc)
+{
+ TraversingVisitor::visit(memacc);
+ unused_variables.erase(memacc.declaration);
+}
+
+void ProgramCompiler::UnusedVariableLocator::visit(VariableDeclaration &var)
+{
+ unused_variables.insert(&var);
+ TraversingVisitor::visit(var);
+}
+
+
+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 = false;
+ if(to_remove.count(&**i))
+ remove = !immutable_block;
+ else
+ {
+ remove_block = false;
+ (*i)->visit(*this);
+ remove = remove_block;
+ }
+
+ if(remove)
+ block.body.erase(i++);
+ else
+ ++i;
+
+ n_removed += remove;
+ }
+}
+
+void ProgramCompiler::NodeRemover::visit(StructDeclaration &strct)
+{
+ SetFlag set(immutable_block);
+ TraversingVisitor::visit(strct);
+}
+
+void ProgramCompiler::NodeRemover::visit(InterfaceBlock &iface)
+{
+ SetFlag set(immutable_block);
+ TraversingVisitor::visit(iface);
+}
+
} // namespace GL
} // namespace Msp
#ifndef MSP_GL_PROGRAMCOMPILER_H_
#define MSP_GL_PROGRAMCOMPILER_H_
+#include <set>
#include "programparser.h"
#include "programsyntax.h"
};
private:
+ struct VariableResolver: ProgramSyntax::TraversingVisitor
+ {
+ std::vector<ProgramSyntax::Block *> blocks;
+ ProgramSyntax::StructDeclaration *type;
+ bool anonymous;
+
+ VariableResolver();
+
+ virtual void visit(ProgramSyntax::Block &);
+ virtual void visit(ProgramSyntax::VariableReference &);
+ virtual void visit(ProgramSyntax::MemberAccess &);
+ virtual void visit(ProgramSyntax::BinaryExpression &);
+ virtual void visit(ProgramSyntax::StructDeclaration &);
+ virtual void visit(ProgramSyntax::VariableDeclaration &);
+ virtual void visit(ProgramSyntax::InterfaceBlock &);
+ };
+
+ struct UnusedVariableLocator: ProgramSyntax::TraversingVisitor
+ {
+ std::set<ProgramSyntax::VariableDeclaration *> unused_variables;
+
+ virtual void visit(ProgramSyntax::VariableReference &);
+ virtual void visit(ProgramSyntax::MemberAccess &);
+ virtual void visit(ProgramSyntax::VariableDeclaration &);
+ };
+
+ struct NodeRemover: ProgramSyntax::TraversingVisitor
+ {
+ 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::InterfaceBlock &);
+ };
+
ProgramParser parser;
ProgramSyntax::Module *module;
bool ProgramParser::is_type(const string &token)
{
- return is_builtin_type(token) || cur_module->structs.count(token);
+ return is_builtin_type(token) || declared_types.count(token);
}
bool ProgramParser::is_identifier(const string &token)
parse_block(strct->members, true);
expect(";");
- cur_module->structs[strct->name] = strct.get();
+ declared_types.insert(strct->name);
return strct.release();
}
#include <deque>
#include <map>
+#include <set>
#include <string>
#include <msp/io/base.h>
#include "programsyntax.h"
std::deque<std::string> next_tokens;
ProgramSyntax::Module main_module;
ProgramSyntax::Module *cur_module;
+ std::set<std::string> declared_types;
static Operator operators[];
}
+VariableReference::VariableReference():
+ declaration(0)
+{ }
+
void VariableReference::visit(NodeVisitor &visitor)
{
visitor.visit(*this);
VariableDeclaration::VariableDeclaration():
constant(false),
+ type_declaration(0),
array(false)
{ }
}
+void TraversingVisitor::visit(Block &block)
+{
+ for(list<NodePtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); ++i)
+ (*i)->visit(*this);
+}
+
+void TraversingVisitor::visit(ParenthesizedExpression &parexpr)
+{
+ parexpr.expression->visit(*this);
+}
+
+void TraversingVisitor::visit(MemberAccess &memacc)
+{
+ memacc.left->visit(*this);
+}
+
+void TraversingVisitor::visit(UnaryExpression &unary)
+{
+ unary.expression->visit(*this);
+}
+
+void TraversingVisitor::visit(BinaryExpression &binary)
+{
+ binary.left->visit(*this);
+ binary.right->visit(*this);
+}
+
+void TraversingVisitor::visit(FunctionCall &call)
+{
+ for(vector<NodePtr<Expression> >::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
+ (*i)->visit(*this);
+}
+
+void TraversingVisitor::visit(ExpressionStatement &expr)
+{
+ expr.expression->visit(*this);
+}
+
+void TraversingVisitor::visit(StructDeclaration &strct)
+{
+ strct.members.visit(*this);
+}
+
+void TraversingVisitor::visit(VariableDeclaration &var)
+{
+ if(var.init_expression)
+ var.init_expression->visit(*this);
+ if(var.array_size)
+ var.array_size->visit(*this);
+}
+
+void TraversingVisitor::visit(InterfaceBlock &iface)
+{
+ iface.members.visit(*this);
+}
+
+void TraversingVisitor::visit(FunctionDeclaration &func)
+{
+ for(vector<NodePtr<VariableDeclaration> >::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
+ (*i)->visit(*this);
+ func.body.visit(*this);
+}
+
+void TraversingVisitor::visit(Conditional &cond)
+{
+ cond.condition->visit(*this);
+ cond.body.visit(*this);
+ cond.else_body.visit(*this);
+}
+
+void TraversingVisitor::visit(Iteration &iter)
+{
+ iter.init_statement->visit(*this);
+ if(iter.condition)
+ iter.condition->visit(*this);
+ if(iter.loop_expression)
+ iter.loop_expression->visit(*this);
+ iter.body.visit(*this);
+}
+
+void TraversingVisitor::visit(Return &ret)
+{
+ if(ret.expression)
+ ret.expression->visit(*this);
+}
+
+
Context::Context(ContextType t):
type(t),
present(false)
public:
T *operator->() { return node; }
const T *operator->() const { return node; }
+ T &operator*() { return *node; }
+ const T &operator*() const { return *node; }
operator void *() const { return node; }
};
+struct StructDeclaration;
struct VariableDeclaration;
struct Block: Node
{
std::list<NodePtr<Node> > body;
bool use_braces;
+ std::map<std::string, StructDeclaration *> types;
+ std::map<std::string, VariableDeclaration *> variables;
Block();
struct VariableReference: Expression
{
std::string name;
+ VariableDeclaration *declaration;
+
+ VariableReference();
virtual VariableReference *clone() const { return new VariableReference(*this); }
virtual void visit(NodeVisitor &);
{
NodePtr<Expression> left;
std::string member;
+ VariableDeclaration *declaration;
virtual MemberAccess *clone() const { return new MemberAccess(*this); }
virtual void visit(NodeVisitor &);
std::string sampling;
std::string interface;
std::string type;
+ StructDeclaration *type_declaration;
std::string name;
bool array;
NodePtr<Expression> array_size;
virtual void visit(Return &) { }
};
+struct TraversingVisitor: NodeVisitor
+{
+ virtual void visit(Block &);
+ virtual void visit(ParenthesizedExpression &);
+ virtual void visit(MemberAccess &);
+ virtual void visit(UnaryExpression &);
+ virtual void visit(BinaryExpression &);
+ virtual void visit(FunctionCall &);
+ virtual void visit(ExpressionStatement &);
+ virtual void visit(StructDeclaration &);
+ virtual void visit(VariableDeclaration &);
+ virtual void visit(InterfaceBlock &);
+ virtual void visit(FunctionDeclaration &);
+ virtual void visit(Conditional &);
+ virtual void visit(Iteration &);
+ virtual void visit(Return &);
+};
+
enum ContextType
{
GLOBAL,
Context vertex_context;
Context geometry_context;
Context fragment_context;
- std::map<std::string, StructDeclaration *> structs;
Module();
};