1 #include <msp/core/raii.h>
11 void DeclarationCombiner::apply(Stage &stage)
13 stage.content.visit(*this);
14 NodeRemover().apply(stage, nodes_to_remove);
17 void DeclarationCombiner::visit(Block &block)
22 TraversingVisitor::visit(block);
25 void DeclarationCombiner::visit(FunctionDeclaration &func)
27 vector<FunctionDeclaration *> &decls = functions[func.name];
30 for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
32 (*i)->definition = func.definition;
33 (*i)->body.body.clear();
36 decls.push_back(&func);
39 void DeclarationCombiner::visit(VariableDeclaration &var)
41 VariableDeclaration *&ptr = variables[var.name];
45 if(var.init_expression)
46 ptr->init_expression = var.init_expression;
51 for(vector<Layout::Qualifier>::iterator i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); ++i)
54 for(vector<Layout::Qualifier>::iterator j=ptr->layout->qualifiers.begin(); (!found && j!=ptr->layout->qualifiers.end()); ++j)
57 j->has_value = i->value;
63 ptr->layout->qualifiers.push_back(*i);
67 ptr->layout = var.layout;
69 nodes_to_remove.insert(&var);
76 void BlockResolver::enter(Block &block)
78 block.parent = current_block;
81 void BlockResolver::visit(InterfaceBlock &iface)
83 /* Block names can't be used for any other identifiers so we can put them
84 in the same map with instance names. */
85 current_block->interfaces[iface.name] = &iface;
86 if(!iface.instance_name.empty())
87 current_block->interfaces[iface.instance_name] = &iface;
88 TraversingVisitor::visit(iface);
92 VariableResolver::VariableResolver():
97 self_referencing(false)
100 void VariableResolver::apply(Stage &stage)
102 Stage *builtin_stage = get_builtins(stage.type);
103 builtins = (builtin_stage ? &builtin_stage->content : 0);
104 stage.content.visit(*this);
107 Block *VariableResolver::next_block(Block &block)
109 return block.parent ? block.parent : &block!=builtins ? builtins : 0;
112 void VariableResolver::enter(Block &block)
114 block.variables.clear();
117 void VariableResolver::visit(VariableReference &var)
121 for(Block *block=current_block; block; block=next_block(*block))
123 map<string, VariableDeclaration *>::iterator i = block->variables.find(var.name);
124 if(i!=block->variables.end())
125 var.declaration = i->second;
128 const map<string, InterfaceBlock *> &ifaces = block->interfaces;
129 for(map<string, InterfaceBlock *>::const_iterator j=ifaces.begin(); (!var.declaration && j!=ifaces.end()); ++j)
130 if(j->second->instance_name.empty())
132 i = j->second->members.variables.find(var.name);
133 if(i!=j->second->members.variables.end())
134 var.declaration = i->second;
140 if(var.declaration->type_declaration)
141 members = &var.declaration->type_declaration->members.variables;
145 map<string, InterfaceBlock *>::iterator j = block->interfaces.find(var.name);
146 if(j!=block->interfaces.end() && j->second->instance_name==var.name)
148 iface_ref = new InterfaceBlockReference;
149 iface_ref->name = var.name;
150 iface_ref->declaration = j->second;
151 members = &j->second->members.variables;
158 if(assignment_target)
160 record_target = false;
161 assignment_target = 0;
164 assignment_target = var.declaration;
166 else if(var.declaration && var.declaration==assignment_target)
167 self_referencing = true;
170 void VariableResolver::visit(InterfaceBlockReference &iface)
172 iface.declaration = 0;
173 for(Block *block=current_block; block; block=next_block(*block))
175 map<string, InterfaceBlock *>::iterator j = block->interfaces.find(iface.name);
176 if(j!=block->interfaces.end())
178 iface.declaration = j->second;
179 members = &j->second->members.variables;
185 void VariableResolver::visit(MemberAccess &memacc)
189 memacc.left->visit(*this);
192 memacc.left = iface_ref;
195 memacc.declaration = 0;
198 map<string, VariableDeclaration *>::iterator i = members->find(memacc.member);
199 if(i!=members->end())
201 memacc.declaration = i->second;
202 if(i->second->type_declaration)
203 members = &i->second->type_declaration->members.variables;
210 void VariableResolver::visit(BinaryExpression &binary)
215 SetForScope<bool> set(record_target, false);
216 binary.right->visit(*this);
220 binary.left->visit(*this);
222 binary.left = iface_ref;
227 TraversingVisitor::visit(binary);
232 void VariableResolver::visit(Assignment &assign)
235 SetFlag set(record_target);
236 assignment_target = 0;
237 assign.left->visit(*this);
240 self_referencing = false;
241 assign.right->visit(*this);
243 assign.self_referencing = (self_referencing || assign.oper!="=");
244 assign.target_declaration = assignment_target;
247 void VariableResolver::visit(StructDeclaration &strct)
249 TraversingVisitor::visit(strct);
250 current_block->types[strct.name] = &strct;
253 void VariableResolver::visit(VariableDeclaration &var)
255 for(Block *block=current_block; block; block=next_block(*block))
257 map<string, StructDeclaration *>::iterator j = block->types.find(var.type);
258 if(j!=block->types.end())
259 var.type_declaration = j->second;
262 if(!block_interface.empty() && var.interface.empty())
263 var.interface = block_interface;
265 TraversingVisitor::visit(var);
266 current_block->variables[var.name] = &var;
269 void VariableResolver::visit(InterfaceBlock &iface)
271 SetForScope<string> set_iface(block_interface, iface.interface);
272 TraversingVisitor::visit(iface);
276 void FunctionResolver::visit(FunctionCall &call)
278 map<string, vector<FunctionDeclaration *> >::iterator i = functions.find(call.name);
279 if(i!=functions.end())
280 call.declaration = i->second.back();
282 TraversingVisitor::visit(call);
285 void FunctionResolver::visit(FunctionDeclaration &func)
287 vector<FunctionDeclaration *> &decls = functions[func.name];
290 for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
291 (*i)->definition = func.definition;
293 decls.push_back(&func);
295 else if(!decls.empty() && decls.back()->definition)
296 func.definition = decls.back()->definition;
298 decls.push_back(&func);
300 TraversingVisitor::visit(func);
304 InterfaceGenerator::InterfaceGenerator():
306 function_scope(false),
310 string InterfaceGenerator::get_out_prefix(Stage::Type type)
312 if(type==Stage::VERTEX)
314 else if(type==Stage::GEOMETRY)
320 void InterfaceGenerator::apply(Stage &s)
324 in_prefix = get_out_prefix(stage->previous->type);
325 out_prefix = get_out_prefix(stage->type);
326 s.content.visit(*this);
327 NodeRemover().apply(s, nodes_to_remove);
330 void InterfaceGenerator::visit(Block &block)
332 SetForScope<Block *> set_block(current_block, &block);
333 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
335 assignment_insert_point = i;
336 if(&block==&stage->content)
337 iface_insert_point = i;
343 string InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
345 unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
346 return prefix+name.substr(offset);
349 bool InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
351 if(stage->content.variables.count(name))
354 VariableDeclaration* iface_var = new VariableDeclaration;
355 iface_var->sampling = var.sampling;
356 iface_var->interface = iface;
357 iface_var->type = var.type;
358 iface_var->type_declaration = var.type_declaration;
359 iface_var->name = name;
360 if(stage->type==Stage::GEOMETRY)
361 iface_var->array = ((var.array && var.interface!="in") || iface=="in");
363 iface_var->array = var.array;
365 iface_var->array_size = var.array_size;
368 iface_var->linked_declaration = &var;
369 var.linked_declaration = iface_var;
371 stage->content.body.insert(iface_insert_point, iface_var);
372 stage->content.variables[name] = iface_var;
377 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
379 Assignment *assign = new Assignment;
380 VariableReference *ref = new VariableReference;
384 assign->right = right;
386 ExpressionStatement *stmt = new ExpressionStatement;
387 stmt->expression = assign;
388 current_block->body.insert(assignment_insert_point, stmt);
394 void InterfaceGenerator::visit(VariableReference &var)
396 if(var.declaration || !stage->previous)
398 /* Don't pull a variable from previous stage if we just generated an out
399 interface in this stage */
400 if(stage->content.variables.count(var.name))
403 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
404 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
405 if(i==prev_vars.end() || i->second->interface!="out")
406 i = prev_vars.find(in_prefix+var.name);
407 if(i!=prev_vars.end() && i->second->interface=="out")
409 generate_interface(*i->second, "in", i->second->name);
410 var.name = i->second->name;
414 void InterfaceGenerator::visit(VariableDeclaration &var)
418 if(iface_block->linked_block)
420 const map<string, VariableDeclaration *> &linked_vars = iface_block->linked_block->members.variables;
421 map<string, VariableDeclaration *>::const_iterator i = linked_vars.find(var.name);
422 if(i!=linked_vars.end())
423 var.linked_declaration = i->second;
428 if(var.interface=="out")
430 /* For out variables in function scope, generate a global interface and
431 replace the local declaration with an assignment. */
432 if(function_scope && generate_interface(var, "out", var.name))
434 nodes_to_remove.insert(&var);
435 if(var.init_expression)
437 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
438 stmt.source = var.source;
439 stmt.line = var.line;
444 else if(var.interface=="in")
446 /* Try to link in variables in global scope with out variables from
448 if(current_block==&stage->content && !var.linked_declaration && stage->previous)
450 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
451 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
452 if(i!=prev_vars.end() && i->second->interface=="out")
454 var.linked_declaration = i->second;
455 i->second->linked_declaration = &var;
460 TraversingVisitor::visit(var);
463 void InterfaceGenerator::visit(InterfaceBlock &iface)
465 if(iface.interface=="in")
467 if(!iface.linked_block && stage->previous)
469 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->content.interfaces;
470 map<string, InterfaceBlock *>::const_iterator i = prev_blocks.find(iface.name);
471 if(i!=prev_blocks.end() && i->second->interface=="out" && i->second->name==iface.name)
473 iface.linked_block = i->second;
474 i->second->linked_block = &iface;
479 SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
480 TraversingVisitor::visit(iface);
483 void InterfaceGenerator::visit(FunctionDeclaration &func)
485 SetFlag set_scope(function_scope, true);
486 // Skip parameters because they're not useful here
487 func.body.visit(*this);
490 void InterfaceGenerator::visit(Passthrough &pass)
492 vector<VariableDeclaration *> pass_vars;
494 for(map<string, VariableDeclaration *>::const_iterator i=stage->content.variables.begin(); i!=stage->content.variables.end(); ++i)
495 if(i->second->interface=="in")
496 pass_vars.push_back(i->second);
500 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
501 for(map<string, VariableDeclaration *>::const_iterator i=prev_vars.begin(); i!=prev_vars.end(); ++i)
504 for(vector<VariableDeclaration *>::const_iterator j=pass_vars.begin(); (!linked && j!=pass_vars.end()); ++j)
505 linked = ((*j)->linked_declaration==i->second);
507 if(!linked && generate_interface(*i->second, "in", i->second->name))
508 pass_vars.push_back(i->second);
512 if(stage->type==Stage::GEOMETRY)
514 InterfaceBlockReference *ref = new InterfaceBlockReference;
517 BinaryExpression *subscript = new BinaryExpression;
518 subscript->left = ref;
519 subscript->oper = "[";
520 subscript->right = pass.subscript;
521 subscript->after = "]";
523 MemberAccess *memacc = new MemberAccess;
524 memacc->left = subscript;
525 memacc->member = "gl_Position";
527 insert_assignment("gl_Position", memacc);
530 for(vector<VariableDeclaration *>::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i)
532 string out_name = change_prefix((*i)->name, out_prefix);
533 generate_interface(**i, "out", out_name);
535 VariableReference *ref = new VariableReference;
536 ref->name = (*i)->name;
539 BinaryExpression *subscript = new BinaryExpression;
540 subscript->left = ref;
541 subscript->oper = "[";
542 subscript->right = pass.subscript;
543 subscript->after = "]";
544 insert_assignment(out_name, subscript);
547 insert_assignment(out_name, ref);
550 nodes_to_remove.insert(&pass);
554 DeclarationReorderer::DeclarationReorderer():
558 void DeclarationReorderer::visit(FunctionCall &call)
560 FunctionDeclaration *def = call.declaration;
562 def = def->definition;
563 if(def && !ordered_funcs.count(def))
564 needed_funcs.insert(def);
567 void DeclarationReorderer::visit(Block &block)
570 return TraversingVisitor::visit(block);
572 NodeList<Statement>::iterator struct_insert_point = block.body.end();
573 NodeList<Statement>::iterator variable_insert_point = block.body.end();
574 NodeList<Statement>::iterator function_insert_point = block.body.end();
575 unsigned unordered_func_count = 0;
576 bool ordered_any_funcs = false;
578 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); )
580 kind = NO_DECLARATION;
584 if(kind==STRUCT && struct_insert_point!=block.body.end())
586 block.body.insert(struct_insert_point, *i);
589 else if(kind>STRUCT && struct_insert_point==block.body.end())
590 struct_insert_point = i;
592 if(kind==VARIABLE && variable_insert_point!=block.body.end())
594 block.body.insert(variable_insert_point, *i);
597 else if(kind>VARIABLE && variable_insert_point==block.body.end())
598 variable_insert_point = i;
602 if(function_insert_point==block.body.end())
603 function_insert_point = i;
605 if(needed_funcs.empty())
607 ordered_funcs.insert(i->get());
608 if(i!=function_insert_point)
610 block.body.insert(function_insert_point, *i);
614 ++function_insert_point;
615 ordered_any_funcs = true;
618 ++unordered_func_count;
623 if(function_insert_point==i)
624 ++function_insert_point;
625 block.body.erase(i++);
630 if(i==block.body.end() && unordered_func_count)
632 if(!ordered_any_funcs)
633 // A subset of the remaining functions forms a recursive loop
634 /* TODO pick a function and move it up, adding any necessary
638 i = function_insert_point;
639 unordered_func_count = 0;
644 void DeclarationReorderer::visit(VariableDeclaration &var)
646 TraversingVisitor::visit(var);
650 void DeclarationReorderer::visit(FunctionDeclaration &func)
652 needed_funcs.clear();
653 func.body.visit(*this);
654 needed_funcs.erase(&func);