namespace GL {
namespace SL {
-DeclarationCombiner::DeclarationCombiner():
- toplevel(true)
-{ }
-
void DeclarationCombiner::apply(Stage &stage)
{
visit(stage.content);
void DeclarationCombiner::visit(Block &block)
{
- if(!toplevel)
+ if(current_block)
return;
- SetForScope<bool> set(toplevel, false);
TraversingVisitor::visit(block);
}
}
+void BlockResolver::visit(Block &block)
+{
+ block.parent = current_block;
+ TraversingVisitor::visit(block);
+}
+
+void BlockResolver::visit(InterfaceBlock &iface)
+{
+ iface.members.anonymous = true;
+ TraversingVisitor::visit(iface);
+}
+
+
VariableResolver::VariableResolver():
- anonymous(false),
record_target(false),
assignment_target(0),
self_referencing(false)
void VariableResolver::apply(Stage &stage)
{
- Stage *builtins = get_builtins(stage.type);
- if(builtins)
- blocks.push_back(&builtins->content);
+ Stage *builtin_stage = get_builtins(stage.type);
+ builtins = (builtin_stage ? &builtin_stage->content : 0);
visit(stage.content);
- if(builtins)
- blocks.pop_back();
+}
+
+Block *VariableResolver::next_block(Block &block)
+{
+ return block.parent ? block.parent : &block!=builtins ? builtins : 0;
}
void VariableResolver::visit(Block &block)
{
- if(!blocks.empty() && blocks.back()==&block)
- return TraversingVisitor::visit(block);
+ if(current_block!=&block)
+ block.variables.clear();
- blocks.push_back(&block);
- block.variables.clear();
TraversingVisitor::visit(block);
- blocks.pop_back();
}
void VariableResolver::visit(VariableReference &var)
{
var.declaration = 0;
type = 0;
- for(vector<Block *>::iterator i=blocks.end(); i!=blocks.begin(); )
+ for(Block *block=current_block; block; block=next_block(*block))
{
- --i;
- map<string, VariableDeclaration *>::iterator j = (*i)->variables.find(var.name);
- if(j!=(*i)->variables.end())
+ map<string, VariableDeclaration *>::iterator j = block->variables.find(var.name);
+ if(j!=block->variables.end())
{
var.declaration = j->second;
type = j->second->type_declaration;
void VariableResolver::visit(StructDeclaration &strct)
{
TraversingVisitor::visit(strct);
- blocks.back()->types[strct.name] = &strct;
+ current_block->types[strct.name] = &strct;
}
void VariableResolver::visit(VariableDeclaration &var)
{
- for(vector<Block *>::iterator i=blocks.end(); i!=blocks.begin(); )
+ for(Block *block=current_block; block; block=next_block(*block))
{
- --i;
- map<string, StructDeclaration *>::iterator j = (*i)->types.find(var.type);
- if(j!=(*i)->types.end())
+ map<string, StructDeclaration *>::iterator j = block->types.find(var.type);
+ if(j!=block->types.end())
var.type_declaration = j->second;
}
var.interface = block_interface;
TraversingVisitor::visit(var);
- blocks.back()->variables[var.name] = &var;
- if(anonymous && blocks.size()>1)
- blocks[blocks.size()-2]->variables[var.name] = &var;
+ current_block->variables[var.name] = &var;
+ if(current_block->anonymous && current_block->parent)
+ current_block->parent->variables[var.name] = &var;
}
void VariableResolver::visit(InterfaceBlock &iface)
{
- SetFlag set(anonymous);
- SetForScope<string> set2(block_interface, iface.interface);
+ SetForScope<string> set_iface(block_interface, iface.interface);
TraversingVisitor::visit(iface);
}
void VariableResolver::visit(FunctionDeclaration &func)
{
- blocks.push_back(&func.body);
+ SetForScope<Block *> set_block(current_block, &func.body);
func.body.variables.clear();
TraversingVisitor::visit(func);
- blocks.pop_back();
}
void VariableResolver::visit(Iteration &iter)
{
- blocks.push_back(&iter.body);
+ SetForScope<Block *> set_block(current_block, &iter.body);
iter.body.variables.clear();
TraversingVisitor::visit(iter);
- blocks.pop_back();
}
InterfaceGenerator::InterfaceGenerator():
- stage(0),
- scope_level(0),
- current_block(0)
+ stage(0)
{ }
string InterfaceGenerator::get_out_prefix(Stage::Type type)
void InterfaceGenerator::visit(Block &block)
{
- SetForScope<unsigned> set(scope_level, scope_level+1);
SetForScope<Block *> set_block(current_block, &block);
for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
{
assignment_insert_point = i;
- if(scope_level==1)
+ if(&block==&stage->content)
iface_insert_point = i;
(*i)->visit(*this);
iface_var->linked_declaration = &var;
stage->content.body.insert(iface_insert_point, iface_var);
{
- SetForScope<unsigned> set_level(scope_level, 1);
SetForScope<Block *> set_block(current_block, &stage->content);
iface_var->visit(*this);
}
{
if(var.interface=="out")
{
- if(scope_level==1)
+ if(current_block==&stage->content)
stage->out_variables[var.name] = &var;
else if(generate_interface(var, "out", change_prefix(var.name, string())))
{
DeclarationReorderer::DeclarationReorderer():
- scope_level(0),
kind(NO_DECLARATION)
{ }
void DeclarationReorderer::visit(Block &block)
{
- SetForScope<unsigned> set(scope_level, scope_level+1);
- if(scope_level>1)
+ if(block.parent)
return TraversingVisitor::visit(block);
NodeList<Statement>::iterator struct_insert_point = block.body.end();
class DeclarationCombiner: private TraversingVisitor
{
private:
- bool toplevel;
std::map<std::string, std::vector<FunctionDeclaration *> > functions;
std::map<std::string, VariableDeclaration *> variables;
std::set<Node *> nodes_to_remove;
public:
- DeclarationCombiner();
-
void apply(Stage &);
private:
using TraversingVisitor::visit;
};
+class BlockResolver: private TraversingVisitor
+{
+public:
+ void apply(Stage &s) { visit(s.content); }
+
+private:
+ virtual void visit(Block &);
+ virtual void visit(InterfaceBlock &);
+ using TraversingVisitor::visit;
+};
+
class VariableResolver: private TraversingVisitor
{
private:
- std::vector<Block *> blocks;
+ Block *builtins;
StructDeclaration *type;
- bool anonymous;
std::string block_interface;
bool record_target;
VariableDeclaration *assignment_target;
void apply(Stage &);
private:
+ Block *next_block(Block &);
+
virtual void visit(Block &);
virtual void visit(VariableReference &);
virtual void visit(MemberAccess &);
Stage *stage;
std::string in_prefix;
std::string out_prefix;
- unsigned scope_level;
- Block *current_block;
NodeList<Statement>::iterator iface_insert_point;
NodeList<Statement>::iterator assignment_insert_point;
std::set<Node *> nodes_to_remove;
FUNCTION
};
- unsigned scope_level;
DeclarationKind kind;
std::set<Node *> ordered_funcs;
std::set<Node *> needed_funcs;
ConstantConditionEliminator::ConstantConditionEliminator():
- scope_level(0),
- current_block(0),
record_only(false)
{ }
void ConstantConditionEliminator::visit(Block &block)
{
- SetForScope<unsigned> set(scope_level, scope_level+1);
SetForScope<Block *> set_block(current_block, &block);
for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
{
void ConstantConditionEliminator::visit(VariableDeclaration &var)
{
- if(var.constant || scope_level>1)
+ if(var.constant || current_block->parent)
variable_values[&var] = var.init_expression.get();
}
aggregate(0),
assignment(0),
assignment_target(false),
- assign_to_subscript(false),
- global_scope(true)
+ assign_to_subscript(false)
{ }
bool UnusedVariableRemover::apply(Stage &stage)
{
variables.push_back(BlockVariableMap());
- {
- SetForScope<bool> set(global_scope, false);
- for(NodeArray<VariableDeclaration>::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
- (*i)->visit(*this);
- func.body.visit(*this);
- }
+ for(NodeArray<VariableDeclaration>::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
+ (*i)->visit(*this);
+ func.body.visit(*this);
BlockVariableMap &block_variables = variables.back();
for(BlockVariableMap::iterator i=block_variables.begin(); i!=block_variables.end(); ++i)