]> git.tdb.fi Git - libs/gl.git/blob - source/programcompiler.cpp
Generate a passthrough for gl_Position in geometry shader
[libs/gl.git] / source / programcompiler.cpp
1 #include <msp/core/raii.h>
2 #include <msp/strings/format.h>
3 #include <msp/strings/utils.h>
4 #include "error.h"
5 #include "program.h"
6 #include "programcompiler.h"
7 #include "shader.h"
8
9 using namespace std;
10
11 namespace {
12
13 const char builtins_src[] =
14         "////// vertex\n"
15         "out gl_PerVertex {\n"
16         "  vec4 gl_Position;\n"
17         "  float gl_ClipDistance[];\n"
18         "};"
19         "////// geometry\n"
20         "in gl_PerVertex {\n"
21         "  vec4 gl_Position;\n"
22         "  float gl_ClipDistance[];\n"
23         "} gl_in[];\n"
24         "out gl_PerVertex {\n"
25         "  vec4 gl_Position;\n"
26         "  float gl_ClipDistance[];\n"
27         "};\n";
28
29 }
30
31 namespace Msp {
32 namespace GL {
33
34 using namespace ProgramSyntax;
35
36 ProgramCompiler::ProgramCompiler():
37         module(0)
38 { }
39
40 void ProgramCompiler::compile(const string &source)
41 {
42         module = &parser.parse(source);
43         process();
44 }
45
46 void ProgramCompiler::compile(IO::Base &io)
47 {
48         module = &parser.parse(io);
49         process();
50 }
51
52 void ProgramCompiler::add_shaders(Program &program)
53 {
54         if(!module)
55                 throw invalid_operation("ProgramCompiler::add_shaders");
56
57         string head = "#version 150\n";
58         for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
59         {
60                 if(i->type==VERTEX)
61                         program.attach_shader_owned(new VertexShader(head+create_source(*i)));
62                 else if(i->type==GEOMETRY)
63                         program.attach_shader_owned(new GeometryShader(head+create_source(*i)));
64                 else if(i->type==FRAGMENT)
65                         program.attach_shader_owned(new FragmentShader(head+create_source(*i)));
66         }
67
68         program.bind_attribute(VERTEX4, "vertex");
69         program.bind_attribute(NORMAL3, "normal");
70         program.bind_attribute(COLOR4_FLOAT, "color");
71         program.bind_attribute(TEXCOORD4, "texcoord");
72 }
73
74 Module *ProgramCompiler::create_builtins_module()
75 {
76         ProgramParser parser;
77         Module *module = new Module(parser.parse(builtins_src));
78         for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
79         {
80                 VariableResolver resolver;
81                 i->content.visit(resolver);
82                 for(map<string, VariableDeclaration *>::iterator j=i->content.variables.begin(); j!=i->content.variables.end(); ++j)
83                         j->second->linked_declaration = j->second;
84         }
85         return module;
86 }
87
88 Module &ProgramCompiler::get_builtins_module()
89 {
90         static RefPtr<Module> builtins_module = create_builtins_module();
91         return *builtins_module;
92 }
93
94 Stage *ProgramCompiler::get_builtins(StageType type)
95 {
96         Module &module = get_builtins_module();
97         for(list<Stage>::iterator i=module.stages.begin(); i!=module.stages.end(); ++i)
98                 if(i->type==type)
99                         return &*i;
100         return 0;
101 }
102
103 void ProgramCompiler::process()
104 {
105         for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
106                 generate(*i);
107         for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); )
108         {
109                 if(optimize(*i))
110                         i = module->stages.begin();
111                 else
112                         ++i;
113         }
114 }
115
116 void ProgramCompiler::generate(Stage &stage)
117 {
118         inject_block(stage.content, module->shared.content);
119
120         apply<VariableResolver>(stage);
121         apply<InterfaceGenerator>(stage);
122         apply<VariableResolver>(stage);
123         apply<VariableRenamer>(stage);
124 }
125
126 bool ProgramCompiler::optimize(Stage &stage)
127 {
128         UnusedVariableLocator unused_locator;
129         unused_locator.apply(stage);
130
131         NodeRemover remover;
132         remover.to_remove = unused_locator.unused_nodes;
133         remover.apply(stage);
134
135         return !unused_locator.unused_nodes.empty();
136 }
137
138 void ProgramCompiler::inject_block(Block &target, const Block &source)
139 {
140         list<NodePtr<Node> >::iterator insert_point = target.body.begin();
141         for(list<NodePtr<Node> >::const_iterator i=source.body.begin(); i!=source.body.end(); ++i)
142                 target.body.insert(insert_point, (*i)->clone());
143 }
144
145 template<typename T>
146 void ProgramCompiler::apply(Stage &stage)
147 {
148         T visitor;
149         visitor.apply(stage);
150 }
151
152 string ProgramCompiler::create_source(Stage &stage)
153 {
154         Formatter formatter;
155         formatter.apply(stage);
156         return formatter.formatted;
157 }
158
159
160 ProgramCompiler::Visitor::Visitor():
161         stage(0)
162 { }
163
164 void ProgramCompiler::Visitor::apply(Stage &s)
165 {
166         SetForScope<Stage *> set(stage, &s);
167         stage->content.visit(*this);
168 }
169
170
171 ProgramCompiler::Formatter::Formatter():
172         indent(0),
173         parameter_list(false),
174         else_if(false)
175 { }
176
177 void ProgramCompiler::Formatter::visit(Literal &literal)
178 {
179         formatted += literal.token;
180 }
181
182 void ProgramCompiler::Formatter::visit(ParenthesizedExpression &parexpr)
183 {
184         formatted += '(';
185         parexpr.expression->visit(*this);
186         formatted += ')';
187 }
188
189 void ProgramCompiler::Formatter::visit(VariableReference &var)
190 {
191         formatted += var.name;
192 }
193
194 void ProgramCompiler::Formatter::visit(MemberAccess &memacc)
195 {
196         memacc.left->visit(*this);
197         formatted += format(".%s", memacc.member);
198 }
199
200 void ProgramCompiler::Formatter::visit(UnaryExpression &unary)
201 {
202         if(unary.prefix)
203                 formatted += unary.oper;
204         unary.expression->visit(*this);
205         if(!unary.prefix)
206                 formatted += unary.oper;
207 }
208
209 void ProgramCompiler::Formatter::visit(BinaryExpression &binary)
210 {
211         binary.left->visit(*this);
212         if(binary.assignment)
213                 formatted += format(" %s ", binary.oper);
214         else
215                 formatted += binary.oper;
216         binary.right->visit(*this);
217         formatted += binary.after;
218 }
219
220 void ProgramCompiler::Formatter::visit(FunctionCall &call)
221 {
222         formatted += format("%s(", call.name);
223         for(vector<NodePtr<Expression> >::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
224         {
225                 if(i!=call.arguments.begin())
226                         formatted += ", ";
227                 (*i)->visit(*this);
228         }
229         formatted += ')';
230 }
231
232 void ProgramCompiler::Formatter::visit(ExpressionStatement &expr)
233 {
234         expr.expression->visit(*this);
235         formatted += ';';
236 }
237
238 void ProgramCompiler::Formatter::visit(Block &block)
239 {
240         if(block.use_braces)
241         {
242                 if(else_if)
243                 {
244                         formatted += '\n';
245                         else_if = false;
246                 }
247                 formatted += format("%s{\n", string(indent*2, ' '));
248         }
249
250         bool change_indent = (!formatted.empty() && !else_if);
251         indent += change_indent;
252         string spaces(indent*2, ' ');
253         for(list<NodePtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); ++i)
254         {
255                 if(i!=block.body.begin())
256                         formatted += '\n';
257                 if(!else_if)
258                         formatted += spaces;
259                 (*i)->visit(*this);
260         }
261         indent -= change_indent;
262
263         if(block.use_braces)
264                 formatted += format("\n%s}", string(indent*2, ' '));
265 }
266
267 void ProgramCompiler::Formatter::visit(Layout &layout)
268 {
269         formatted += "layout(";
270         for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i)
271         {
272                 if(i!=layout.qualifiers.begin())
273                         formatted += ", ";
274                 formatted += i->identifier;
275                 if(!i->value.empty())
276                         formatted += format("=%s", i->value);
277         }
278         formatted += format(") %s;", layout.interface);
279 }
280
281 void ProgramCompiler::Formatter::visit(StructDeclaration &strct)
282 {
283         formatted += format("struct %s\n", strct.name);
284         strct.members.visit(*this);
285         formatted += ';';
286 }
287
288 void ProgramCompiler::Formatter::visit(VariableDeclaration &var)
289 {
290         if(var.constant)
291                 formatted += "const ";
292         if(!var.sampling.empty())
293                 formatted += format("%s ", var.sampling);
294         if(!var.interface.empty() && var.interface!=block_interface)
295                 formatted += format("%s ", var.interface);
296         formatted += format("%s %s", var.type, var.name);
297         if(var.array)
298         {
299                 formatted += '[';
300                 if(var.array_size)
301                         var.array_size->visit(*this);
302                 formatted += ']';
303         }
304         if(var.init_expression)
305         {
306                 formatted += " = ";
307                 var.init_expression->visit(*this);
308         }
309         if(!parameter_list)
310                 formatted += ';';
311 }
312
313 void ProgramCompiler::Formatter::visit(InterfaceBlock &iface)
314 {
315         SetForScope<string> set(block_interface, iface.interface);
316         formatted += format("%s %s\n", iface.interface, iface.name);
317         iface.members.visit(*this);
318         formatted += ';';
319 }
320
321 void ProgramCompiler::Formatter::visit(FunctionDeclaration &func)
322 {
323         formatted += format("%s %s(", func.return_type, func.name);
324         for(vector<NodePtr<VariableDeclaration> >::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
325         {
326                 if(i!=func.parameters.begin())
327                         formatted += ", ";
328                 SetFlag set(parameter_list);
329                 (*i)->visit(*this);
330         }
331         formatted += ')';
332         if(func.definition)
333         {
334                 formatted += '\n';
335                 func.body.visit(*this);
336         }
337         else
338                 formatted += ';';
339 }
340
341 void ProgramCompiler::Formatter::visit(Conditional &cond)
342 {
343         if(else_if)
344         {
345                 formatted += ' ';
346                 else_if = false;
347         }
348
349         formatted += "if(";
350         cond.condition->visit(*this);
351         formatted += ")\n";
352
353         cond.body.visit(*this);
354         if(!cond.else_body.body.empty())
355         {
356                 formatted += format("\n%selse", string(indent*2, ' '));
357                 SetFlag set(else_if);
358                 cond.else_body.visit(*this);
359         }
360 }
361
362 void ProgramCompiler::Formatter::visit(Iteration &iter)
363 {
364         formatted += "for(";
365         iter.init_statement->visit(*this);
366         formatted += ' ';
367         iter.condition->visit(*this);
368         formatted += "; ";
369         iter.loop_expression->visit(*this);
370         formatted += ")\n";
371         iter.body.visit(*this);
372 }
373
374 void ProgramCompiler::Formatter::visit(Return &ret)
375 {
376         formatted += "return ";
377         ret.expression->visit(*this);
378         formatted += ';';
379 }
380
381
382 ProgramCompiler::VariableResolver::VariableResolver():
383         anonymous(false)
384 { }
385
386 void ProgramCompiler::VariableResolver::apply(Stage &s)
387 {
388         SetForScope<Stage *> set(stage, &s);
389         Stage *builtins = get_builtins(stage->type);
390         if(builtins)
391                 blocks.push_back(&builtins->content);
392         stage->content.visit(*this);
393         if(builtins)
394                 blocks.pop_back();
395 }
396
397 void ProgramCompiler::VariableResolver::visit(Block &block)
398 {
399         blocks.push_back(&block);
400         block.variables.clear();
401         TraversingVisitor::visit(block);
402         blocks.pop_back();
403 }
404
405 void ProgramCompiler::VariableResolver::visit(VariableReference &var)
406 {
407         var.declaration = 0;
408         type = 0;
409         for(vector<Block *>::iterator i=blocks.end(); i!=blocks.begin(); )
410         {
411                 --i;
412                 map<string, VariableDeclaration *>::iterator j = (*i)->variables.find(var.name);
413                 if(j!=(*i)->variables.end())
414                 {
415                         var.declaration = j->second;
416                         type = j->second->type_declaration;
417                         break;
418                 }
419         }
420 }
421
422 void ProgramCompiler::VariableResolver::visit(MemberAccess &memacc)
423 {
424         type = 0;
425         TraversingVisitor::visit(memacc);
426         memacc.declaration = 0;
427         if(type)
428         {
429                 map<string, VariableDeclaration *>::iterator i = type->members.variables.find(memacc.member);
430                 if(i!=type->members.variables.end())
431                 {
432                         memacc.declaration = i->second;
433                         type = i->second->type_declaration;
434                 }
435                 else
436                         type = 0;
437         }
438 }
439
440 void ProgramCompiler::VariableResolver::visit(BinaryExpression &binary)
441 {
442         if(binary.oper=="[")
443         {
444                 binary.right->visit(*this);
445                 type = 0;
446                 binary.left->visit(*this);
447         }
448         else
449         {
450                 TraversingVisitor::visit(binary);
451                 type = 0;
452         }
453 }
454
455 void ProgramCompiler::VariableResolver::visit(StructDeclaration &strct)
456 {
457         TraversingVisitor::visit(strct);
458         blocks.back()->types[strct.name] = &strct;
459 }
460
461 void ProgramCompiler::VariableResolver::visit(VariableDeclaration &var)
462 {
463         for(vector<Block *>::iterator i=blocks.end(); i!=blocks.begin(); )
464         {
465                 --i;
466                 map<string, StructDeclaration *>::iterator j = (*i)->types.find(var.type);
467                 if(j!=(*i)->types.end())
468                         var.type_declaration = j->second;
469         }
470
471         if(!block_interface.empty() && var.interface.empty())
472                 var.interface = block_interface;
473
474         TraversingVisitor::visit(var);
475         blocks.back()->variables[var.name] = &var;
476         if(anonymous && blocks.size()>1)
477                 blocks[blocks.size()-2]->variables[var.name] = &var;
478 }
479
480 void ProgramCompiler::VariableResolver::visit(InterfaceBlock &iface)
481 {
482         SetFlag set(anonymous);
483         SetForScope<string> set2(block_interface, iface.interface);
484         TraversingVisitor::visit(iface);
485 }
486
487
488 ProgramCompiler::InterfaceGenerator::InterfaceGenerator():
489         scope_level(0),
490         remove_node(false)
491 { }
492
493 string ProgramCompiler::InterfaceGenerator::get_out_prefix(StageType type)
494 {
495         if(type==VERTEX)
496                 return "_vs_out_";
497         else if(type==GEOMETRY)
498                 return "_gs_out_";
499         else
500                 return string();
501 }
502
503 void ProgramCompiler::InterfaceGenerator::apply(Stage &s)
504 {
505         SetForScope<Stage *> set(stage, &s);
506         if(stage->previous)
507                 in_prefix = get_out_prefix(stage->previous->type);
508         out_prefix = get_out_prefix(stage->type);
509         stage->content.visit(*this);
510 }
511
512 void ProgramCompiler::InterfaceGenerator::visit(Block &block)
513 {
514         SetForScope<unsigned> set(scope_level, scope_level+1);
515         for(list<NodePtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); )
516         {
517                 (*i)->visit(*this);
518
519                 if(scope_level==1)
520                 {
521                         for(map<string, VariableDeclaration *>::iterator j=iface_declarations.begin(); j!=iface_declarations.end(); ++j)
522                         {
523                                 list<NodePtr<Node> >::iterator k = block.body.insert(i, j->second);
524                                 (*k)->visit(*this);
525                         }
526                         iface_declarations.clear();
527                 }
528
529                 for(list<Node *>::iterator j=insert_nodes.begin(); j!=insert_nodes.end(); ++j)
530                         block.body.insert(i, *j);
531                 insert_nodes.clear();
532
533                 if(remove_node)
534                         block.body.erase(i++);
535                 else
536                         ++i;
537                 remove_node = false;
538         }
539 }
540
541 string ProgramCompiler::InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
542 {
543         unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
544         return prefix+name.substr(offset);
545 }
546
547 bool ProgramCompiler::InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
548 {
549         const map<string, VariableDeclaration *> &stage_vars = (iface=="in" ? stage->in_variables : stage->out_variables);
550         if(stage_vars.count(name) || iface_declarations.count(name))
551                 return false;
552
553         VariableDeclaration* iface_var = new VariableDeclaration;
554         iface_var->sampling = var.sampling;
555         iface_var->interface = iface;
556         iface_var->type = var.type;
557         iface_var->type_declaration = var.type_declaration;
558         iface_var->name = name;
559         if(stage->type==GEOMETRY)
560                 iface_var->array = ((var.array && var.interface!="in") || iface=="in");
561         else
562                 iface_var->array = var.array;
563         if(iface_var->array)
564                 iface_var->array_size = var.array_size;
565         if(iface=="in")
566                 iface_var->linked_declaration = &var;
567         iface_declarations[name] = iface_var;
568
569         return true;
570 }
571
572 void ProgramCompiler::InterfaceGenerator::insert_assignment(const string &left, ProgramSyntax::Expression *right)
573 {
574         BinaryExpression *assign = new BinaryExpression;
575         VariableReference *ref = new VariableReference;
576         ref->name = left;
577         assign->left = ref;
578         assign->oper = "=";
579         assign->right = right;
580         assign->assignment = true;
581
582         ExpressionStatement *stmt = new ExpressionStatement;
583         stmt->expression = assign;
584         insert_nodes.push_back(stmt);
585 }
586
587 void ProgramCompiler::InterfaceGenerator::visit(VariableReference &var)
588 {
589         if(var.declaration || !stage->previous)
590                 return;
591         if(iface_declarations.count(var.name))
592                 return;
593
594         const map<string, VariableDeclaration *> &prev_out = stage->previous->out_variables;
595         map<string, VariableDeclaration *>::const_iterator i = prev_out.find(var.name);
596         if(i==prev_out.end())
597                 i = prev_out.find(in_prefix+var.name);
598         if(i!=prev_out.end())
599                 generate_interface(*i->second, "in", var.name);
600 }
601
602 void ProgramCompiler::InterfaceGenerator::visit(VariableDeclaration &var)
603 {
604         if(var.interface=="out")
605         {
606                 if(scope_level==1)
607                         stage->out_variables[var.name] = &var;
608                 else if(generate_interface(var, "out", change_prefix(var.name, string())))
609                 {
610                         remove_node = true;
611                         if(var.init_expression)
612                                 insert_assignment(var.name, var.init_expression->clone());
613                 }
614         }
615         else if(var.interface=="in")
616         {
617                 stage->in_variables[var.name] = &var;
618                 if(var.linked_declaration)
619                         var.linked_declaration->linked_declaration = &var;
620                 else if(stage->previous)
621                 {
622                         const map<string, VariableDeclaration *> &prev_out = stage->previous->out_variables;
623                         map<string, VariableDeclaration *>::const_iterator i = prev_out.find(var.name);
624                         if(i!=prev_out.end())
625                         {
626                                 var.linked_declaration = i->second;
627                                 i->second->linked_declaration = &var;
628                         }
629                 }
630         }
631
632         TraversingVisitor::visit(var);
633 }
634
635 void ProgramCompiler::InterfaceGenerator::visit(Passthrough &pass)
636 {
637         vector<VariableDeclaration *> pass_vars;
638
639         for(map<string, VariableDeclaration *>::const_iterator i=stage->in_variables.begin(); i!=stage->in_variables.end(); ++i)
640                 pass_vars.push_back(i->second);
641         for(map<string, VariableDeclaration *>::const_iterator i=iface_declarations.begin(); i!=iface_declarations.end(); ++i)
642                 if(i->second->interface=="in")
643                         pass_vars.push_back(i->second);
644
645         if(stage->previous)
646         {
647                 const map<string, VariableDeclaration *> &prev_out = stage->previous->out_variables;
648                 for(map<string, VariableDeclaration *>::const_iterator i=prev_out.begin(); i!=prev_out.end(); ++i)
649                 {
650                         bool linked = false;
651                         for(vector<VariableDeclaration *>::const_iterator j=pass_vars.begin(); (!linked && j!=pass_vars.end()); ++j)
652                                 linked = ((*j)->linked_declaration==i->second);
653
654                         if(!linked && generate_interface(*i->second, "in", i->second->name))
655                                 pass_vars.push_back(i->second);
656                 }
657         }
658
659         if(stage->type==GEOMETRY)
660         {
661                 VariableReference *ref = new VariableReference;
662                 ref->name = "gl_in";
663
664                 BinaryExpression *subscript = new BinaryExpression;
665                 subscript->left = ref;
666                 subscript->oper = "[";
667                 subscript->right = pass.subscript;
668                 subscript->after = "]";
669
670                 MemberAccess *memacc = new MemberAccess;
671                 memacc->left = subscript;
672                 memacc->member = "gl_Position";
673
674                 insert_assignment("gl_Position", memacc);
675         }
676
677         for(vector<VariableDeclaration *>::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i)
678         {
679                 string out_name = change_prefix((*i)->name, out_prefix);
680                 generate_interface(**i, "out", out_name);
681
682                 VariableReference *ref = new VariableReference;
683                 ref->name = (*i)->name;
684                 if(pass.subscript)
685                 {
686                         BinaryExpression *subscript = new BinaryExpression;
687                         subscript->left = ref;
688                         subscript->oper = "[";
689                         subscript->right = pass.subscript;
690                         subscript->after = "]";
691                         insert_assignment(out_name, subscript);
692                 }
693                 else
694                         insert_assignment(out_name, ref);
695         }
696
697         remove_node = true;
698 }
699
700
701 void ProgramCompiler::VariableRenamer::visit(VariableReference &var)
702 {
703         if(var.declaration)
704                 var.name = var.declaration->name;
705 }
706
707 void ProgramCompiler::VariableRenamer::visit(VariableDeclaration &var)
708 {
709         if(var.linked_declaration)
710                 var.name = var.linked_declaration->name;
711         TraversingVisitor::visit(var);
712 }
713
714
715 ProgramCompiler::UnusedVariableLocator::UnusedVariableLocator():
716         aggregate(0),
717         assignment(false),
718         record_target(false),
719         assignment_target(0),
720         indeterminate_target(false),
721         self_referencing(false)
722 { }
723
724 void ProgramCompiler::UnusedVariableLocator::visit(VariableReference &var)
725 {
726         if(record_target)
727         {
728                 if(assignment_target)
729                         indeterminate_target = true;
730                 else
731                         assignment_target = var.declaration;
732         }
733         else
734         {
735                 unused_nodes.erase(var.declaration);
736
737                 map<VariableDeclaration *, Node *>::iterator i = assignments.find(var.declaration);
738                 if(i!=assignments.end())
739                 {
740                         unused_nodes.erase(i->second);
741                         assignments.erase(i);
742                 }
743
744                 if(assignment && var.declaration==assignment_target)
745                         self_referencing = true;
746         }
747
748         map<VariableDeclaration *, Node *>::iterator i = aggregates.find(var.declaration);
749         if(i!=aggregates.end())
750                 unused_nodes.erase(i->second);
751 }
752
753 void ProgramCompiler::UnusedVariableLocator::visit(MemberAccess &memacc)
754 {
755         TraversingVisitor::visit(memacc);
756         unused_nodes.erase(memacc.declaration);
757 }
758
759 void ProgramCompiler::UnusedVariableLocator::visit(BinaryExpression &binary)
760 {
761         if(binary.assignment)
762         {
763                 assignment = true;
764                 {
765                         SetFlag set(record_target);
766                         binary.left->visit(*this);
767                 }
768                 if(binary.oper!="=")
769                         self_referencing = true;
770                 binary.right->visit(*this);
771         }
772         else if(record_target && binary.oper=="[")
773         {
774                 binary.left->visit(*this);
775                 SetForScope<bool> set(record_target, false);
776                 binary.right->visit(*this);
777         }
778         else
779                 TraversingVisitor::visit(binary);
780 }
781
782 void ProgramCompiler::UnusedVariableLocator::visit(ExpressionStatement &expr)
783 {
784         assignment = false;
785         assignment_target = 0;
786         indeterminate_target = false;
787         self_referencing = false;
788         TraversingVisitor::visit(expr);
789         if(assignment && assignment_target && !indeterminate_target)
790         {
791                 Node *&assign = assignments[assignment_target];
792                 if(self_referencing)
793                         unused_nodes.erase(assign);
794                 else if(assign)
795                         unused_nodes.insert(assign);
796                 assign = &expr;
797                 if(assignment_target->interface=="out" && (stage->type==FRAGMENT || assignment_target->linked_declaration))
798                         unused_nodes.erase(assignment_target);
799                 else
800                         unused_nodes.insert(&expr);
801         }
802         assignment = false;
803 }
804
805 void ProgramCompiler::UnusedVariableLocator::visit(StructDeclaration &strct)
806 {
807         SetForScope<Node *> set(aggregate, &strct);
808         unused_nodes.insert(&strct);
809         TraversingVisitor::visit(strct);
810 }
811
812 void ProgramCompiler::UnusedVariableLocator::visit(VariableDeclaration &var)
813 {
814         if(aggregate)
815                 aggregates[&var] = aggregate;
816         else
817         {
818                 unused_nodes.insert(&var);
819                 if(var.init_expression)
820                 {
821                         unused_nodes.insert(&*var.init_expression);
822                         assignments[&var] = &*var.init_expression;
823                 }
824         }
825         unused_nodes.erase(var.type_declaration);
826         TraversingVisitor::visit(var);
827 }
828
829 void ProgramCompiler::UnusedVariableLocator::visit(InterfaceBlock &iface)
830 {
831         SetForScope<Node *> set(aggregate, &iface);
832         unused_nodes.insert(&iface);
833         TraversingVisitor::visit(iface);
834 }
835
836
837 void ProgramCompiler::NodeRemover::visit(Block &block)
838 {
839         for(list<NodePtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); )
840         {
841                 (*i)->visit(*this);
842                 if(to_remove.count(&**i))
843                         block.body.erase(i++);
844                 else
845                         ++i;
846         }
847 }
848
849 void ProgramCompiler::NodeRemover::visit(VariableDeclaration &var)
850 {
851         if(to_remove.count(&var))
852         {
853                 stage->in_variables.erase(var.name);
854                 stage->out_variables.erase(var.name);
855                 if(var.linked_declaration)
856                         var.linked_declaration->linked_declaration = 0;
857         }
858         else if(var.init_expression && to_remove.count(&*var.init_expression))
859                 var.init_expression = 0;
860 }
861
862 } // namespace GL
863 } // namespace Msp