1 #include <msp/core/raii.h>
11 void DeclarationCombiner::apply(Stage &stage)
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::visit(Block &block)
78 block.parent = current_block;
79 TraversingVisitor::visit(block);
82 void BlockResolver::visit(InterfaceBlock &iface)
84 iface.members.anonymous = true;
85 TraversingVisitor::visit(iface);
89 VariableResolver::VariableResolver():
92 self_referencing(false)
95 void VariableResolver::apply(Stage &stage)
97 Stage *builtin_stage = get_builtins(stage.type);
98 builtins = (builtin_stage ? &builtin_stage->content : 0);
102 Block *VariableResolver::next_block(Block &block)
104 return block.parent ? block.parent : &block!=builtins ? builtins : 0;
107 void VariableResolver::visit(Block &block)
109 if(current_block!=&block)
110 block.variables.clear();
112 TraversingVisitor::visit(block);
115 void VariableResolver::visit(VariableReference &var)
119 for(Block *block=current_block; block; block=next_block(*block))
121 map<string, VariableDeclaration *>::iterator j = block->variables.find(var.name);
122 if(j!=block->variables.end())
124 var.declaration = j->second;
125 type = j->second->type_declaration;
132 if(assignment_target)
134 record_target = false;
135 assignment_target = 0;
138 assignment_target = var.declaration;
140 else if(var.declaration && var.declaration==assignment_target)
141 self_referencing = true;
144 void VariableResolver::visit(MemberAccess &memacc)
147 TraversingVisitor::visit(memacc);
148 memacc.declaration = 0;
151 map<string, VariableDeclaration *>::iterator i = type->members.variables.find(memacc.member);
152 if(i!=type->members.variables.end())
154 memacc.declaration = i->second;
155 type = i->second->type_declaration;
162 void VariableResolver::visit(BinaryExpression &binary)
167 SetForScope<bool> set(record_target, false);
168 binary.right->visit(*this);
171 binary.left->visit(*this);
175 TraversingVisitor::visit(binary);
180 void VariableResolver::visit(Assignment &assign)
183 SetFlag set(record_target);
184 assignment_target = 0;
185 assign.left->visit(*this);
188 self_referencing = false;
189 assign.right->visit(*this);
191 assign.self_referencing = (self_referencing || assign.oper!="=");
192 assign.target_declaration = assignment_target;
195 void VariableResolver::visit(StructDeclaration &strct)
197 TraversingVisitor::visit(strct);
198 current_block->types[strct.name] = &strct;
201 void VariableResolver::visit(VariableDeclaration &var)
203 for(Block *block=current_block; block; block=next_block(*block))
205 map<string, StructDeclaration *>::iterator j = block->types.find(var.type);
206 if(j!=block->types.end())
207 var.type_declaration = j->second;
210 if(!block_interface.empty() && var.interface.empty())
211 var.interface = block_interface;
213 TraversingVisitor::visit(var);
214 current_block->variables[var.name] = &var;
215 if(current_block->anonymous && current_block->parent)
216 current_block->parent->variables[var.name] = &var;
219 void VariableResolver::visit(InterfaceBlock &iface)
221 SetForScope<string> set_iface(block_interface, iface.interface);
222 TraversingVisitor::visit(iface);
225 void VariableResolver::visit(FunctionDeclaration &func)
227 SetForScope<Block *> set_block(current_block, &func.body);
228 func.body.variables.clear();
229 TraversingVisitor::visit(func);
232 void VariableResolver::visit(Iteration &iter)
234 SetForScope<Block *> set_block(current_block, &iter.body);
235 iter.body.variables.clear();
236 TraversingVisitor::visit(iter);
240 void FunctionResolver::visit(FunctionCall &call)
242 map<string, vector<FunctionDeclaration *> >::iterator i = functions.find(call.name);
243 if(i!=functions.end())
244 call.declaration = i->second.back();
246 TraversingVisitor::visit(call);
249 void FunctionResolver::visit(FunctionDeclaration &func)
251 vector<FunctionDeclaration *> &decls = functions[func.name];
254 for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
255 (*i)->definition = func.definition;
257 decls.push_back(&func);
259 else if(!decls.empty() && decls.back()->definition)
260 func.definition = decls.back()->definition;
262 decls.push_back(&func);
264 TraversingVisitor::visit(func);
268 InterfaceGenerator::InterfaceGenerator():
272 string InterfaceGenerator::get_out_prefix(Stage::Type type)
274 if(type==Stage::VERTEX)
276 else if(type==Stage::GEOMETRY)
282 void InterfaceGenerator::apply(Stage &s)
286 in_prefix = get_out_prefix(stage->previous->type);
287 out_prefix = get_out_prefix(stage->type);
289 NodeRemover().apply(s, nodes_to_remove);
292 void InterfaceGenerator::visit(Block &block)
294 SetForScope<Block *> set_block(current_block, &block);
295 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
297 assignment_insert_point = i;
298 if(&block==&stage->content)
299 iface_insert_point = i;
305 string InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
307 unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
308 return prefix+name.substr(offset);
311 bool InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
313 const map<string, VariableDeclaration *> &stage_vars = (iface=="in" ? stage->in_variables : stage->out_variables);
314 if(stage_vars.count(name))
317 VariableDeclaration* iface_var = new VariableDeclaration;
318 iface_var->sampling = var.sampling;
319 iface_var->interface = iface;
320 iface_var->type = var.type;
321 iface_var->type_declaration = var.type_declaration;
322 iface_var->name = name;
323 if(stage->type==Stage::GEOMETRY)
324 iface_var->array = ((var.array && var.interface!="in") || iface=="in");
326 iface_var->array = var.array;
328 iface_var->array_size = var.array_size;
330 iface_var->linked_declaration = &var;
331 stage->content.body.insert(iface_insert_point, iface_var);
333 SetForScope<Block *> set_block(current_block, &stage->content);
334 iface_var->visit(*this);
340 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
342 Assignment *assign = new Assignment;
343 VariableReference *ref = new VariableReference;
347 assign->right = right;
349 ExpressionStatement *stmt = new ExpressionStatement;
350 stmt->expression = assign;
351 current_block->body.insert(assignment_insert_point, stmt);
357 void InterfaceGenerator::visit(VariableReference &var)
359 if(var.declaration || !stage->previous)
362 const map<string, VariableDeclaration *> &prev_out = stage->previous->out_variables;
363 map<string, VariableDeclaration *>::const_iterator i = prev_out.find(var.name);
364 if(i==prev_out.end())
365 i = prev_out.find(in_prefix+var.name);
366 if(i!=prev_out.end())
368 generate_interface(*i->second, "in", i->second->name);
369 var.name = i->second->name;
373 void InterfaceGenerator::visit(VariableDeclaration &var)
375 if(var.interface=="out")
377 if(current_block==&stage->content)
378 stage->out_variables[var.name] = &var;
379 else if(generate_interface(var, "out", change_prefix(var.name, string())))
381 nodes_to_remove.insert(&var);
382 if(var.init_expression)
384 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
385 stmt.source = var.source;
386 stmt.line = var.line;
391 else if(var.interface=="in")
393 stage->in_variables[var.name] = &var;
394 if(var.linked_declaration)
395 var.linked_declaration->linked_declaration = &var;
396 else if(stage->previous)
398 const map<string, VariableDeclaration *> &prev_out = stage->previous->out_variables;
399 map<string, VariableDeclaration *>::const_iterator i = prev_out.find(var.name);
400 if(i!=prev_out.end())
402 var.linked_declaration = i->second;
403 i->second->linked_declaration = &var;
408 TraversingVisitor::visit(var);
411 void InterfaceGenerator::visit(Passthrough &pass)
413 vector<VariableDeclaration *> pass_vars;
415 for(map<string, VariableDeclaration *>::const_iterator i=stage->in_variables.begin(); i!=stage->in_variables.end(); ++i)
416 pass_vars.push_back(i->second);
420 const map<string, VariableDeclaration *> &prev_out = stage->previous->out_variables;
421 for(map<string, VariableDeclaration *>::const_iterator i=prev_out.begin(); i!=prev_out.end(); ++i)
424 for(vector<VariableDeclaration *>::const_iterator j=pass_vars.begin(); (!linked && j!=pass_vars.end()); ++j)
425 linked = ((*j)->linked_declaration==i->second);
427 if(!linked && generate_interface(*i->second, "in", i->second->name))
428 pass_vars.push_back(i->second);
432 if(stage->type==Stage::GEOMETRY)
434 VariableReference *ref = new VariableReference;
437 BinaryExpression *subscript = new BinaryExpression;
438 subscript->left = ref;
439 subscript->oper = "[";
440 subscript->right = pass.subscript;
441 subscript->after = "]";
443 MemberAccess *memacc = new MemberAccess;
444 memacc->left = subscript;
445 memacc->member = "gl_Position";
447 insert_assignment("gl_Position", memacc);
450 for(vector<VariableDeclaration *>::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i)
452 string out_name = change_prefix((*i)->name, out_prefix);
453 generate_interface(**i, "out", out_name);
455 VariableReference *ref = new VariableReference;
456 ref->name = (*i)->name;
459 BinaryExpression *subscript = new BinaryExpression;
460 subscript->left = ref;
461 subscript->oper = "[";
462 subscript->right = pass.subscript;
463 subscript->after = "]";
464 insert_assignment(out_name, subscript);
467 insert_assignment(out_name, ref);
470 nodes_to_remove.insert(&pass);
474 DeclarationReorderer::DeclarationReorderer():
478 void DeclarationReorderer::visit(FunctionCall &call)
480 FunctionDeclaration *def = call.declaration;
482 def = def->definition;
483 if(def && !ordered_funcs.count(def))
484 needed_funcs.insert(def);
487 void DeclarationReorderer::visit(Block &block)
490 return TraversingVisitor::visit(block);
492 NodeList<Statement>::iterator struct_insert_point = block.body.end();
493 NodeList<Statement>::iterator variable_insert_point = block.body.end();
494 NodeList<Statement>::iterator function_insert_point = block.body.end();
495 unsigned unordered_func_count = 0;
496 bool ordered_any_funcs = false;
498 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); )
500 kind = NO_DECLARATION;
504 if(kind==STRUCT && struct_insert_point!=block.body.end())
506 block.body.insert(struct_insert_point, *i);
509 else if(kind>STRUCT && struct_insert_point==block.body.end())
510 struct_insert_point = i;
512 if(kind==VARIABLE && variable_insert_point!=block.body.end())
514 block.body.insert(variable_insert_point, *i);
517 else if(kind>VARIABLE && variable_insert_point==block.body.end())
518 variable_insert_point = i;
522 if(function_insert_point==block.body.end())
523 function_insert_point = i;
525 if(needed_funcs.empty())
527 ordered_funcs.insert(i->get());
528 if(i!=function_insert_point)
530 block.body.insert(function_insert_point, *i);
534 ++function_insert_point;
535 ordered_any_funcs = true;
538 ++unordered_func_count;
543 if(function_insert_point==i)
544 ++function_insert_point;
545 block.body.erase(i++);
550 if(i==block.body.end() && unordered_func_count)
552 if(!ordered_any_funcs)
553 // A subset of the remaining functions forms a recursive loop
554 /* TODO pick a function and move it up, adding any necessary
558 i = function_insert_point;
559 unordered_func_count = 0;
564 void DeclarationReorderer::visit(VariableDeclaration &var)
566 TraversingVisitor::visit(var);
570 void DeclarationReorderer::visit(FunctionDeclaration &func)
572 needed_funcs.clear();
573 func.body.visit(*this);
574 needed_funcs.erase(&func);