X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fprogramcompiler.cpp;h=98b11e89b834d48a665e8e4059bfe306d882a7af;hb=7cd066816f7faab6f8f0eba1fca4dee67ee5dc3b;hp=bc021c97b04874ccf375d82383218da4d9a4b4d7;hpb=a29cc14162e911b36d18d1d1896216697c7dc0c1;p=libs%2Fgl.git diff --git a/source/programcompiler.cpp b/source/programcompiler.cpp index bc021c97..98b11e89 100644 --- a/source/programcompiler.cpp +++ b/source/programcompiler.cpp @@ -63,15 +63,10 @@ void ProgramCompiler::generate(Stage &stage) { inject_block(stage.content, module->shared.content); - resolve_variables(stage); - - InterfaceGenerator generator; - generator.visit(stage); - - resolve_variables(stage); - - VariableRenamer renamer; - stage.content.visit(renamer); + apply(stage); + apply(stage); + apply(stage); + apply(stage); } void ProgramCompiler::optimize(Stage &stage) @@ -79,11 +74,11 @@ void ProgramCompiler::optimize(Stage &stage) while(1) { UnusedVariableLocator unused_locator; - unused_locator.visit(stage); + unused_locator.apply(stage); NodeRemover remover; remover.to_remove = unused_locator.unused_nodes; - remover.visit(stage); + remover.apply(stage); if(!remover.n_removed) break; @@ -97,20 +92,32 @@ void ProgramCompiler::inject_block(Block &target, const Block &source) target.body.insert(insert_point, (*i)->clone()); } -void ProgramCompiler::resolve_variables(Stage &stage) +template +void ProgramCompiler::apply(Stage &stage) { - VariableResolver resolver; - stage.content.visit(resolver); + T visitor; + visitor.apply(stage); } string ProgramCompiler::create_source(Stage &stage) { Formatter formatter; - stage.content.visit(formatter); + formatter.apply(stage); return formatter.formatted; } +ProgramCompiler::Visitor::Visitor(): + stage(0) +{ } + +void ProgramCompiler::Visitor::apply(Stage &s) +{ + SetForScope set(stage, &s); + stage->content.visit(*this); +} + + ProgramCompiler::Formatter::Formatter(): indent(0), parameter_list(false), @@ -413,7 +420,6 @@ void ProgramCompiler::VariableResolver::visit(InterfaceBlock &iface) ProgramCompiler::InterfaceGenerator::InterfaceGenerator(): - stage(0), scope_level(0), remove_node(false) { } @@ -428,7 +434,7 @@ string ProgramCompiler::InterfaceGenerator::get_out_prefix(StageType type) return string(); } -void ProgramCompiler::InterfaceGenerator::visit(Stage &s) +void ProgramCompiler::InterfaceGenerator::apply(Stage &s) { SetForScope set(stage, &s); if(stage->previous) @@ -446,7 +452,7 @@ void ProgramCompiler::InterfaceGenerator::visit(Block &block) if(scope_level==1) { - for(map >::iterator j=iface_declarations.begin(); j!=iface_declarations.end(); ++j) + for(map::iterator j=iface_declarations.begin(); j!=iface_declarations.end(); ++j) { list >::iterator k = block.body.insert(i, j->second); (*k)->visit(*this); @@ -454,7 +460,7 @@ void ProgramCompiler::InterfaceGenerator::visit(Block &block) iface_declarations.clear(); } - for(list >::iterator j=insert_nodes.begin(); j!=insert_nodes.end(); ++j) + for(list::iterator j=insert_nodes.begin(); j!=insert_nodes.end(); ++j) block.body.insert(i, *j); insert_nodes.clear(); @@ -488,7 +494,7 @@ bool ProgramCompiler::InterfaceGenerator::generate_interface(VariableDeclaration iface_var->array_size = out.array_size; if(iface=="in") iface_var->linked_declaration = &out; - iface_declarations[iface_var->name] = iface_var; + iface_declarations[name] = iface_var; return true; } @@ -602,17 +608,10 @@ void ProgramCompiler::VariableRenamer::visit(VariableDeclaration &var) ProgramCompiler::UnusedVariableLocator::UnusedVariableLocator(): - stage(0), assignment(false), assignment_target(0) { } -void ProgramCompiler::UnusedVariableLocator::visit(Stage &s) -{ - stage = &s; - stage->content.visit(*this); -} - void ProgramCompiler::UnusedVariableLocator::visit(VariableReference &var) { if(assignment) @@ -670,18 +669,11 @@ void ProgramCompiler::UnusedVariableLocator::visit(VariableDeclaration &var) ProgramCompiler::NodeRemover::NodeRemover(): - stage(0), n_removed(0), immutable_block(false), remove_block(false) { } -void ProgramCompiler::NodeRemover::visit(Stage &s) -{ - stage = &s; - stage->content.visit(*this); -} - void ProgramCompiler::NodeRemover::visit(Block &block) { remove_block = immutable_block;