1 #include <msp/core/hash.h>
2 #include <msp/core/raii.h>
3 #include <msp/strings/lexicalcast.h>
13 void DeclarationCombiner::apply(Stage &stage)
15 stage.content.visit(*this);
16 NodeRemover().apply(stage, nodes_to_remove);
19 void DeclarationCombiner::visit(Block &block)
24 TraversingVisitor::visit(block);
27 void DeclarationCombiner::visit(VariableDeclaration &var)
29 VariableDeclaration *&ptr = variables[var.name];
33 if(var.init_expression)
34 ptr->init_expression = var.init_expression;
39 for(vector<Layout::Qualifier>::iterator i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); ++i)
42 for(vector<Layout::Qualifier>::iterator j=ptr->layout->qualifiers.begin(); (!found && j!=ptr->layout->qualifiers.end()); ++j)
45 j->has_value = i->value;
51 ptr->layout->qualifiers.push_back(*i);
55 ptr->layout = var.layout;
57 nodes_to_remove.insert(&var);
64 ConstantSpecializer::ConstantSpecializer():
68 void ConstantSpecializer::apply(Stage &stage, const map<string, int> *v)
71 stage.content.visit(*this);
74 void ConstantSpecializer::visit(VariableDeclaration &var)
76 bool specializable = false;
79 vector<Layout::Qualifier> &qualifiers = var.layout->qualifiers;
80 for(vector<Layout::Qualifier>::iterator i=qualifiers.begin(); i!=qualifiers.end(); ++i)
81 if(i->name=="constant_id")
87 i->value = hash32(var.name)&0x7FFFFFFF;
91 if(qualifiers.empty())
95 if(specializable && values)
97 map<string, int>::const_iterator i = values->find(var.name);
100 RefPtr<Literal> literal = new Literal;
103 literal->token = (i->second ? "true" : "false");
104 literal->value = static_cast<bool>(i->second);
106 else if(var.type=="int")
108 literal->token = lexical_cast<string>(i->second);
109 literal->value = i->second;
111 var.init_expression = literal;
117 void BlockHierarchyResolver::enter(Block &block)
119 r_any_resolved |= (current_block!=block.parent);
120 block.parent = current_block;
124 TypeResolver::TypeResolver():
126 r_any_resolved(false)
129 bool TypeResolver::apply(Stage &s)
133 r_any_resolved = false;
134 s.content.visit(*this);
135 return r_any_resolved;
138 TypeDeclaration *TypeResolver::get_or_create_array_type(TypeDeclaration &type)
140 map<TypeDeclaration *, TypeDeclaration *>::iterator i = array_types.find(&type);
141 if(i!=array_types.end())
144 BasicTypeDeclaration *array = new BasicTypeDeclaration;
145 array->source = BUILTIN_SOURCE;
146 array->name = type.name+"[]";
147 array->kind = BasicTypeDeclaration::ARRAY;
148 array->base = type.name;
149 array->base_type = &type;
150 stage->content.body.insert(type_insert_point, array);
151 array_types[&type] = array;
155 void TypeResolver::resolve_type(TypeDeclaration *&type, const string &name, bool array)
157 TypeDeclaration *resolved = 0;
158 map<string, TypeDeclaration *>::iterator i = stage->types.find(name);
159 if(i!=stage->types.end())
161 map<TypeDeclaration *, TypeDeclaration *>::iterator j = alias_map.find(i->second);
162 resolved = (j!=alias_map.end() ? j->second : i->second);
165 if(resolved && array)
166 resolved = get_or_create_array_type(*resolved);
168 r_any_resolved |= (resolved!=type);
172 void TypeResolver::visit(Block &block)
174 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
177 type_insert_point = i;
182 void TypeResolver::visit(BasicTypeDeclaration &type)
184 resolve_type(type.base_type, type.base, false);
186 if(type.kind==BasicTypeDeclaration::VECTOR && type.base_type)
187 if(BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type))
188 if(basic_base->kind==BasicTypeDeclaration::VECTOR)
190 type.kind = BasicTypeDeclaration::MATRIX;
191 type.size |= basic_base->size<<16;
194 if(type.kind==BasicTypeDeclaration::ALIAS && type.base_type)
195 alias_map[&type] = type.base_type;
196 else if(type.kind==BasicTypeDeclaration::ARRAY && type.base_type)
197 array_types[type.base_type] = &type;
199 stage->types.insert(make_pair(type.name, &type));
202 void TypeResolver::visit(ImageTypeDeclaration &type)
204 resolve_type(type.base_type, type.base, false);
205 stage->types.insert(make_pair(type.name, &type));
208 void TypeResolver::visit(StructDeclaration &strct)
210 stage->types.insert(make_pair(strct.name, &strct));
211 TraversingVisitor::visit(strct);
214 void TypeResolver::visit(VariableDeclaration &var)
216 resolve_type(var.type_declaration, var.type, var.array);
219 void TypeResolver::visit(FunctionDeclaration &func)
221 resolve_type(func.return_type_declaration, func.return_type, false);
222 TraversingVisitor::visit(func);
226 VariableResolver::VariableResolver():
228 r_any_resolved(false),
229 record_target(false),
230 r_self_referencing(false),
231 r_assignment_target(0)
234 bool VariableResolver::apply(Stage &s)
237 s.interface_blocks.clear();
238 r_any_resolved = false;
239 s.content.visit(*this);
240 return r_any_resolved;
243 void VariableResolver::enter(Block &block)
245 block.variables.clear();
248 void VariableResolver::visit_and_replace(RefPtr<Expression> &expr)
250 r_replacement_expr = 0;
252 if(r_replacement_expr)
253 expr = r_replacement_expr;
254 r_replacement_expr = 0;
257 void VariableResolver::visit(VariableReference &var)
259 VariableDeclaration *declaration = 0;
261 /* Look for variable declarations in the block hierarchy first. Interface
262 blocks are always defined in the top level so we can't accidentally skip
264 for(Block *block=current_block; (!declaration && block); block=block->parent)
266 map<string, VariableDeclaration *>::iterator i = block->variables.find(var.name);
267 if(i!=block->variables.end())
268 declaration = i->second;
273 const map<string, InterfaceBlock *> &blocks = stage->interface_blocks;
274 map<string, InterfaceBlock *>::const_iterator i = blocks.find("_"+var.name);
277 /* The name refers to an interface block with an instance name rather
278 than a variable. Prepare a new syntax tree node accordingly. */
279 InterfaceBlockReference *iface_ref = new InterfaceBlockReference;
280 iface_ref->source = var.source;
281 iface_ref->line = var.line;
282 iface_ref->name = var.name;
283 iface_ref->declaration = i->second;
284 r_replacement_expr = iface_ref;
288 // Look for the variable in anonymous interface blocks.
289 for(i=blocks.begin(); (!declaration && i!=blocks.end()); ++i)
290 if(i->second->instance_name.empty())
292 map<string, VariableDeclaration *>::iterator j = i->second->members.variables.find(var.name);
293 if(j!=i->second->members.variables.end())
294 declaration = j->second;
299 r_any_resolved |= (declaration!=var.declaration);
300 var.declaration = declaration;
304 if(r_assignment_target)
306 /* More than one variable reference found in assignment target.
307 Unable to determine what the primary target is. */
308 record_target = false;
309 r_assignment_target = 0;
312 r_assignment_target = var.declaration;
314 else if(var.declaration && var.declaration==r_assignment_target)
315 r_self_referencing = true;
318 void VariableResolver::visit(InterfaceBlockReference &iface)
320 map<string, InterfaceBlock *>::iterator i = stage->interface_blocks.find("_"+iface.name);
321 InterfaceBlock *declaration = (i!=stage->interface_blocks.end() ? i->second : 0);
322 r_any_resolved |= (declaration!=iface.declaration);
323 iface.declaration = declaration;
326 void VariableResolver::visit(MemberAccess &memacc)
328 visit_and_replace(memacc.left);
330 map<string, VariableDeclaration *> *members = 0;
331 if(StructDeclaration *strct = dynamic_cast<StructDeclaration *>(memacc.left->type))
332 members = &strct->members.variables;
333 else if(InterfaceBlockReference *iface_ref = dynamic_cast<InterfaceBlockReference *>(memacc.left.get()))
335 if(iface_ref->declaration)
336 members = &iface_ref->declaration->members.variables;
339 VariableDeclaration *declaration = 0;
342 map<string, VariableDeclaration *>::iterator i = members->find(memacc.member);
343 if(i!=members->end())
344 declaration = i->second;
347 r_any_resolved |= (declaration!=memacc.declaration);
348 memacc.declaration = declaration;
351 void VariableResolver::visit(UnaryExpression &unary)
353 visit_and_replace(unary.expression);
356 void VariableResolver::visit(BinaryExpression &binary)
358 if(binary.oper->token[0]=='[')
361 /* The subscript expression is not a part of the primary assignment
363 SetFlag set(record_target, false);
364 visit_and_replace(binary.right);
366 visit_and_replace(binary.left);
370 visit_and_replace(binary.left);
371 visit_and_replace(binary.right);
375 void VariableResolver::visit(Assignment &assign)
378 SetFlag set(record_target);
379 r_assignment_target = 0;
380 visit_and_replace(assign.left);
381 r_any_resolved |= (r_assignment_target!=assign.target_declaration);
382 assign.target_declaration = r_assignment_target;
385 r_self_referencing = false;
386 visit_and_replace(assign.right);
387 assign.self_referencing = (r_self_referencing || assign.oper->token[0]!='=');
390 void VariableResolver::visit(FunctionCall &call)
392 for(NodeArray<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
393 visit_and_replace(*i);
396 void VariableResolver::visit(VariableDeclaration &var)
398 if(!block_interface.empty() && var.interface.empty())
399 var.interface = block_interface;
402 var.layout->visit(*this);
404 visit_and_replace(var.array_size);
405 if(var.init_expression)
406 visit_and_replace(var.init_expression);
407 current_block->variables.insert(make_pair(var.name, &var));
410 void VariableResolver::visit(InterfaceBlock &iface)
412 /* Block names can be reused in different interfaces. Prefix the name with
413 the first character of the interface to avoid conflicts. */
414 stage->interface_blocks.insert(make_pair(iface.interface+iface.name, &iface));
415 if(!iface.instance_name.empty())
416 stage->interface_blocks.insert(make_pair("_"+iface.instance_name, &iface));
418 SetForScope<string> set_iface(block_interface, iface.interface);
419 TraversingVisitor::visit(iface);
423 ExpressionResolver::ExpressionResolver():
425 r_any_resolved(false)
428 bool ExpressionResolver::apply(Stage &s)
431 r_any_resolved = false;
432 s.content.visit(*this);
433 return r_any_resolved;
436 bool ExpressionResolver::is_scalar(BasicTypeDeclaration &type)
438 return (type.kind==BasicTypeDeclaration::INT || type.kind==BasicTypeDeclaration::FLOAT);
441 bool ExpressionResolver::is_vector_or_matrix(BasicTypeDeclaration &type)
443 return (type.kind==BasicTypeDeclaration::VECTOR || type.kind==BasicTypeDeclaration::MATRIX);
446 BasicTypeDeclaration *ExpressionResolver::get_element_type(BasicTypeDeclaration &type)
448 if(is_vector_or_matrix(type) || type.kind==BasicTypeDeclaration::ARRAY)
450 BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type);
451 return (basic_base ? get_element_type(*basic_base) : 0);
457 bool ExpressionResolver::can_convert(BasicTypeDeclaration &from, BasicTypeDeclaration &to)
459 if(from.kind==BasicTypeDeclaration::INT && to.kind==BasicTypeDeclaration::FLOAT)
460 return from.size<=to.size;
461 else if(from.kind!=to.kind)
463 else if((from.kind==BasicTypeDeclaration::VECTOR || from.kind==BasicTypeDeclaration::MATRIX) && from.size==to.size)
465 BasicTypeDeclaration *from_base = dynamic_cast<BasicTypeDeclaration *>(from.base_type);
466 BasicTypeDeclaration *to_base = dynamic_cast<BasicTypeDeclaration *>(to.base_type);
467 return (from_base && to_base && can_convert(*from_base, *to_base));
473 ExpressionResolver::Compatibility ExpressionResolver::get_compatibility(BasicTypeDeclaration &left, BasicTypeDeclaration &right)
477 else if(can_convert(left, right))
478 return LEFT_CONVERTIBLE;
479 else if(can_convert(right, left))
480 return RIGHT_CONVERTIBLE;
482 return NOT_COMPATIBLE;
485 BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration::Kind kind, unsigned size)
487 for(vector<BasicTypeDeclaration *>::const_iterator i=basic_types.begin(); i!=basic_types.end(); ++i)
488 if((*i)->kind==kind && (*i)->size==size)
493 BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration &elem_type, BasicTypeDeclaration::Kind kind, unsigned size)
495 for(vector<BasicTypeDeclaration *>::const_iterator i=basic_types.begin(); i!=basic_types.end(); ++i)
496 if(get_element_type(**i)==&elem_type && (*i)->kind==kind && (*i)->size==size)
501 void ExpressionResolver::convert_to(RefPtr<Expression> &expr, BasicTypeDeclaration &type)
503 RefPtr<FunctionCall> call = new FunctionCall;
504 call->name = type.name;
505 call->constructor = true;
506 call->arguments.push_back(0);
507 call->arguments.back() = expr;
512 bool ExpressionResolver::convert_to_element(RefPtr<Expression> &expr, BasicTypeDeclaration &elem_type)
514 if(BasicTypeDeclaration *expr_type = dynamic_cast<BasicTypeDeclaration *>(expr->type))
516 BasicTypeDeclaration *to_type = &elem_type;
517 if(is_vector_or_matrix(*expr_type))
518 to_type = find_type(elem_type, expr_type->kind, expr_type->size);
521 convert_to(expr, *to_type);
529 void ExpressionResolver::resolve(Expression &expr, TypeDeclaration *type, bool lvalue)
531 r_any_resolved |= (type!=expr.type || lvalue!=expr.lvalue);
533 expr.lvalue = lvalue;
536 void ExpressionResolver::visit(Literal &literal)
538 if(literal.value.check_type<bool>())
539 resolve(literal, find_type(BasicTypeDeclaration::BOOL, 1), false);
540 else if(literal.value.check_type<int>())
541 resolve(literal, find_type(BasicTypeDeclaration::INT, 32), false);
542 else if(literal.value.check_type<float>())
543 resolve(literal, find_type(BasicTypeDeclaration::FLOAT, 32), false);
546 void ExpressionResolver::visit(ParenthesizedExpression &parexpr)
548 TraversingVisitor::visit(parexpr);
549 resolve(parexpr, parexpr.expression->type, parexpr.expression->lvalue);
552 void ExpressionResolver::visit(VariableReference &var)
555 resolve(var, var.declaration->type_declaration, true);
558 void ExpressionResolver::visit(InterfaceBlockReference &iface)
560 resolve(iface, 0, true);
563 void ExpressionResolver::visit(MemberAccess &memacc)
565 TraversingVisitor::visit(memacc);
567 if(memacc.declaration)
568 resolve(memacc, memacc.declaration->type_declaration, memacc.left->lvalue);
571 void ExpressionResolver::visit(UnaryExpression &unary)
573 TraversingVisitor::visit(unary);
575 BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(unary.expression->type);
579 char oper = unary.oper->token[0];
582 if(basic->kind!=BasicTypeDeclaration::BOOL)
587 if(basic->kind!=BasicTypeDeclaration::INT)
590 else if(oper=='+' || oper=='-')
592 BasicTypeDeclaration *elem = get_element_type(*basic);
593 if(!elem || !is_scalar(*elem))
596 resolve(unary, basic, unary.expression->lvalue);
599 void ExpressionResolver::visit(BinaryExpression &binary, bool assign)
601 /* Binary operators are only defined for basic types (not for image or
603 BasicTypeDeclaration *basic_left = dynamic_cast<BasicTypeDeclaration *>(binary.left->type);
604 BasicTypeDeclaration *basic_right = dynamic_cast<BasicTypeDeclaration *>(binary.right->type);
605 if(!basic_left || !basic_right)
608 char oper = binary.oper->token[0];
611 /* Subscripting operates on vectors, matrices and arrays, and the right
612 operand must be an integer. */
613 if((!is_vector_or_matrix(*basic_left) && basic_left->kind!=BasicTypeDeclaration::ARRAY) || basic_right->kind!=BasicTypeDeclaration::INT)
616 resolve(binary, basic_left->base_type, binary.left->lvalue);
619 else if(basic_left->kind==BasicTypeDeclaration::ARRAY || basic_right->kind==BasicTypeDeclaration::ARRAY)
620 // No other binary operator can be used with arrays.
623 BasicTypeDeclaration *elem_left = get_element_type(*basic_left);
624 BasicTypeDeclaration *elem_right = get_element_type(*basic_right);
625 if(!elem_left || !elem_right)
628 Compatibility compat = get_compatibility(*basic_left, *basic_right);
629 Compatibility elem_compat = get_compatibility(*elem_left, *elem_right);
630 if(elem_compat==NOT_COMPATIBLE)
632 if(assign && (compat==LEFT_CONVERTIBLE || elem_compat==LEFT_CONVERTIBLE))
635 TypeDeclaration *type = 0;
636 char oper2 = binary.oper->token[1];
637 if((oper=='<' && oper2!='<') || (oper=='>' && oper2!='>'))
639 /* Relational operators compare two scalar integer or floating-point
641 if(!is_scalar(*elem_left) || !is_scalar(*elem_right) || compat==NOT_COMPATIBLE)
644 type = find_type(BasicTypeDeclaration::BOOL, 1);
646 else if((oper=='=' || oper=='!') && oper2=='=')
648 // Equality comparison can be done on any compatible types.
649 if(compat==NOT_COMPATIBLE)
652 type = find_type(BasicTypeDeclaration::BOOL, 1);
654 else if(oper2=='&' || oper2=='|' || oper2=='^')
656 // Logical operators can only be applied to booleans.
657 if(basic_left->kind!=BasicTypeDeclaration::BOOL || basic_right->kind!=BasicTypeDeclaration::BOOL)
662 else if((oper=='&' || oper=='|' || oper=='^' || oper=='%') && !oper2)
664 // Bitwise operators and modulo can only be applied to integers.
665 if(basic_left->kind!=BasicTypeDeclaration::INT || basic_right->kind!=BasicTypeDeclaration::INT)
668 type = (compat==LEFT_CONVERTIBLE ? basic_right : basic_left);
670 else if((oper=='<' || oper=='>') && oper2==oper)
672 // Shifts apply to integer scalars and vectors, with some restrictions.
673 if(elem_left->kind!=BasicTypeDeclaration::INT || elem_right->kind!=BasicTypeDeclaration::INT)
675 unsigned left_size = (basic_left->kind==BasicTypeDeclaration::INT ? 1 : basic_left->kind==BasicTypeDeclaration::VECTOR ? basic_left->size : 0);
676 unsigned right_size = (basic_right->kind==BasicTypeDeclaration::INT ? 1 : basic_right->kind==BasicTypeDeclaration::VECTOR ? basic_right->size : 0);
677 if(!left_size || (left_size==1 && right_size!=1) || (left_size>1 && right_size!=1 && right_size!=left_size))
681 // Don't perform conversion even if the operands are of different sizes.
684 else if(oper=='+' || oper=='-' || oper=='*' || oper=='/')
686 // Arithmetic operators require scalar elements.
687 if(!is_scalar(*elem_left) || !is_scalar(*elem_right))
690 if(oper=='*' && is_vector_or_matrix(*basic_left) && is_vector_or_matrix(*basic_right) &&
691 (basic_left->kind==BasicTypeDeclaration::MATRIX || basic_right->kind==BasicTypeDeclaration::MATRIX))
693 /* Multiplication has special rules when at least one operand is a
694 matrix and the other is a vector or a matrix. */
695 unsigned left_columns = basic_left->size&0xFFFF;
696 unsigned right_rows = basic_right->size;
697 if(basic_right->kind==BasicTypeDeclaration::MATRIX)
699 if(left_columns!=right_rows)
702 BasicTypeDeclaration *elem_result = (elem_compat==LEFT_CONVERTIBLE ? elem_right : elem_left);
704 if(basic_left->kind==BasicTypeDeclaration::VECTOR)
705 type = find_type(*elem_result, BasicTypeDeclaration::VECTOR, basic_right->size&0xFFFF);
706 else if(basic_right->kind==BasicTypeDeclaration::VECTOR)
707 type = find_type(*elem_result, BasicTypeDeclaration::VECTOR, basic_left->size>>16);
709 type = find_type(*elem_result, BasicTypeDeclaration::MATRIX, (basic_left->size&0xFFFF0000)|(basic_right->size&0xFFFF));
711 else if(compat==NOT_COMPATIBLE)
713 // Arithmetic between scalars and matrices or vectors is supported.
714 if(is_scalar(*basic_left) && is_vector_or_matrix(*basic_right))
715 type = (elem_compat==RIGHT_CONVERTIBLE ? find_type(*elem_left, basic_right->kind, basic_right->size) : basic_right);
716 else if(is_vector_or_matrix(*basic_left) && is_scalar(*basic_right))
717 type = (elem_compat==LEFT_CONVERTIBLE ? find_type(*elem_right, basic_left->kind, basic_left->size) : basic_left);
721 else if(compat==LEFT_CONVERTIBLE)
729 if(assign && type!=basic_left)
732 bool converted = true;
733 if(compat==LEFT_CONVERTIBLE)
734 convert_to(binary.left, *basic_right);
735 else if(compat==RIGHT_CONVERTIBLE)
736 convert_to(binary.right, *basic_left);
737 else if(elem_compat==LEFT_CONVERTIBLE)
738 converted = convert_to_element(binary.left, *elem_right);
739 else if(elem_compat==RIGHT_CONVERTIBLE)
740 converted = convert_to_element(binary.right, *elem_left);
745 resolve(binary, type, assign);
748 void ExpressionResolver::visit(BinaryExpression &binary)
750 TraversingVisitor::visit(binary);
751 visit(binary, false);
754 void ExpressionResolver::visit(Assignment &assign)
756 TraversingVisitor::visit(assign);
758 if(assign.oper->token[0]!='=')
759 return visit(assign, true);
760 else if(assign.left->type!=assign.right->type)
762 BasicTypeDeclaration *basic_left = dynamic_cast<BasicTypeDeclaration *>(assign.left->type);
763 BasicTypeDeclaration *basic_right = dynamic_cast<BasicTypeDeclaration *>(assign.right->type);
764 if(!basic_left || !basic_right)
767 Compatibility compat = get_compatibility(*basic_left, *basic_right);
768 if(compat==RIGHT_CONVERTIBLE)
769 convert_to(assign.right, *basic_left);
770 else if(compat!=SAME_TYPE)
774 resolve(assign, assign.left->type, true);
777 void ExpressionResolver::visit(FunctionCall &call)
779 TraversingVisitor::visit(call);
781 TypeDeclaration *type = 0;
783 type = call.declaration->return_type_declaration;
784 else if(call.constructor)
786 map<string, TypeDeclaration *>::const_iterator i=stage->types.find(call.name);
787 type = (i!=stage->types.end() ? i->second : 0);
789 resolve(call, type, false);
792 void ExpressionResolver::visit(BasicTypeDeclaration &type)
794 basic_types.push_back(&type);
797 void ExpressionResolver::visit(VariableDeclaration &var)
799 TraversingVisitor::visit(var);
800 if(!var.init_expression)
803 BasicTypeDeclaration *var_basic = dynamic_cast<BasicTypeDeclaration *>(var.type_declaration);
804 BasicTypeDeclaration *init_basic = dynamic_cast<BasicTypeDeclaration *>(var.init_expression->type);
805 if(!var_basic || !init_basic)
808 Compatibility compat = get_compatibility(*var_basic, *init_basic);
809 if(compat==RIGHT_CONVERTIBLE)
810 convert_to(var.init_expression, *var_basic);
814 bool FunctionResolver::apply(Stage &s)
818 r_any_resolved = false;
819 s.content.visit(*this);
820 return r_any_resolved;
823 void FunctionResolver::visit(FunctionCall &call)
825 map<string, FunctionDeclaration *>::iterator i = stage->functions.find(call.name);
826 if(i!=stage->functions.end())
827 call.declaration = i->second;
829 TraversingVisitor::visit(call);
832 void FunctionResolver::visit(FunctionDeclaration &func)
834 FunctionDeclaration *&stage_decl = stage->functions[func.name];
835 vector<FunctionDeclaration *> &decls = declarations[func.name];
836 if(func.definition==&func)
840 // Set all previous declarations to use this definition.
841 for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
843 (*i)->definition = func.definition;
844 (*i)->body.body.clear();
853 func.definition = stage_decl->definition;
855 decls.push_back(&func);
857 TraversingVisitor::visit(func);
861 InterfaceGenerator::InterfaceGenerator():
863 function_scope(false),
866 iface_target_block(0)
869 string InterfaceGenerator::get_out_prefix(Stage::Type type)
871 if(type==Stage::VERTEX)
873 else if(type==Stage::GEOMETRY)
879 void InterfaceGenerator::apply(Stage &s)
882 iface_target_block = &stage->content;
884 in_prefix = get_out_prefix(stage->previous->type);
885 out_prefix = get_out_prefix(stage->type);
886 s.content.visit(*this);
887 NodeRemover().apply(s, nodes_to_remove);
890 void InterfaceGenerator::visit(Block &block)
892 SetForScope<Block *> set_block(current_block, &block);
893 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
895 assignment_insert_point = i;
896 if(&block==&stage->content)
897 iface_insert_point = i;
903 string InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
905 unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
906 return prefix+name.substr(offset);
909 VariableDeclaration *InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
911 if(stage->content.variables.count(name))
914 VariableDeclaration* iface_var = new VariableDeclaration;
915 iface_var->sampling = var.sampling;
916 iface_var->interface = iface;
917 iface_var->type = var.type;
918 iface_var->name = name;
919 /* Geometry shader inputs are always arrays. But if we're bringing in an
920 entire block, the array is on the block and not individual variables. */
921 if(stage->type==Stage::GEOMETRY && !copy_block)
922 iface_var->array = ((var.array && var.interface!="in") || iface=="in");
924 iface_var->array = var.array;
926 iface_var->array_size = var.array_size;
929 iface_var->layout = var.layout;
930 iface_var->linked_declaration = &var;
931 var.linked_declaration = iface_var;
934 iface_target_block->body.insert(iface_insert_point, iface_var);
935 iface_target_block->variables.insert(make_pair(name, iface_var));
940 InterfaceBlock *InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
942 if(stage->interface_blocks.count("in"+out_block.name))
945 InterfaceBlock *in_block = new InterfaceBlock;
946 in_block->interface = "in";
947 in_block->name = out_block.name;
948 in_block->instance_name = out_block.instance_name;
949 if(stage->type==Stage::GEOMETRY)
950 in_block->array = true;
952 in_block->array = out_block.array;
953 in_block->linked_block = &out_block;
954 out_block.linked_block = in_block;
957 SetFlag set_copy(copy_block, true);
958 SetForScope<Block *> set_target(iface_target_block, &in_block->members);
959 SetForScope<NodeList<Statement>::iterator> set_ins_pt(iface_insert_point, in_block->members.body.end());
960 out_block.members.visit(*this);
963 iface_target_block->body.insert(iface_insert_point, in_block);
964 stage->interface_blocks.insert(make_pair("in"+in_block->name, in_block));
965 if(!in_block->instance_name.empty())
966 stage->interface_blocks.insert(make_pair("_"+in_block->instance_name, in_block));
968 SetFlag set_scope(function_scope, false);
969 SetForScope<Block *> set_block(current_block, &stage->content);
970 in_block->visit(*this);
975 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
977 Assignment *assign = new Assignment;
978 VariableReference *ref = new VariableReference;
981 assign->oper = &Operator::get_operator("=", Operator::BINARY);
982 assign->right = right;
984 ExpressionStatement *stmt = new ExpressionStatement;
985 stmt->expression = assign;
986 current_block->body.insert(assignment_insert_point, stmt);
992 void InterfaceGenerator::visit(VariableReference &var)
994 if(var.declaration || !stage->previous)
996 /* Don't pull a variable from previous stage if we just generated an output
997 interface in this stage */
998 if(stage->content.variables.count(var.name))
1001 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
1002 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
1003 if(i==prev_vars.end() || i->second->interface!="out")
1004 i = prev_vars.find(in_prefix+var.name);
1005 if(i!=prev_vars.end() && i->second->interface=="out")
1007 generate_interface(*i->second, "in", i->second->name);
1008 var.name = i->second->name;
1012 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
1013 map<string, InterfaceBlock *>::const_iterator j = prev_blocks.find("_"+var.name);
1014 if(j!=prev_blocks.end() && j->second->interface=="out")
1016 generate_interface(*j->second);
1017 /* Let VariableResolver convert the variable reference into an interface
1022 for(j=prev_blocks.begin(); j!=prev_blocks.end(); ++j)
1023 if(j->second->instance_name.empty())
1025 i = j->second->members.variables.find(var.name);
1026 if(i!=j->second->members.variables.end())
1028 generate_interface(*j->second);
1034 void InterfaceGenerator::visit(VariableDeclaration &var)
1038 generate_interface(var, "in", var.name);
1044 if(iface_block->linked_block)
1046 // Link all variables to their counterparts in the linked block.
1047 const map<string, VariableDeclaration *> &linked_vars = iface_block->linked_block->members.variables;
1048 map<string, VariableDeclaration *>::const_iterator i = linked_vars.find(var.name);
1049 if(i!=linked_vars.end())
1051 var.linked_declaration = i->second;
1052 var.linked_declaration->linked_declaration = &var;
1058 if(var.interface=="out")
1060 /* For output variables in function scope, generate a global interface
1061 and replace the local declaration with an assignment. */
1062 VariableDeclaration *out_var = 0;
1063 if(function_scope && (out_var=generate_interface(var, "out", var.name)))
1065 out_var->source = var.source;
1066 out_var->line = var.line;
1067 nodes_to_remove.insert(&var);
1068 if(var.init_expression)
1070 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
1071 stmt.source = var.source;
1072 stmt.line = var.line;
1077 else if(var.interface=="in")
1079 /* Try to link input variables in global scope with output variables from
1081 if(current_block==&stage->content && !var.linked_declaration && stage->previous)
1083 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
1084 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
1085 if(i!=prev_vars.end() && i->second->interface=="out")
1087 var.linked_declaration = i->second;
1088 i->second->linked_declaration = &var;
1093 TraversingVisitor::visit(var);
1096 void InterfaceGenerator::visit(InterfaceBlock &iface)
1098 if(iface.interface=="in")
1100 /* Try to link input blocks with output blocks sharing the same block
1101 name from previous stage. */
1102 if(!iface.linked_block && stage->previous)
1104 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
1105 map<string, InterfaceBlock *>::const_iterator i = prev_blocks.find("out"+iface.name);
1106 if(i!=prev_blocks.end())
1108 iface.linked_block = i->second;
1109 i->second->linked_block = &iface;
1114 SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
1115 TraversingVisitor::visit(iface);
1118 void InterfaceGenerator::visit(FunctionDeclaration &func)
1120 SetFlag set_scope(function_scope, true);
1121 // Skip parameters because they're not useful here
1122 func.body.visit(*this);
1125 void InterfaceGenerator::visit(Passthrough &pass)
1127 vector<VariableDeclaration *> pass_vars;
1129 // Pass through all input variables of this stage.
1130 for(map<string, VariableDeclaration *>::const_iterator i=stage->content.variables.begin(); i!=stage->content.variables.end(); ++i)
1131 if(i->second->interface=="in")
1132 pass_vars.push_back(i->second);
1136 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
1137 for(map<string, VariableDeclaration *>::const_iterator i=prev_vars.begin(); i!=prev_vars.end(); ++i)
1139 if(i->second->interface!="out")
1142 /* Pass through output variables from the previous stage, but only
1143 those which are not already linked to an input here. */
1144 if(!i->second->linked_declaration && generate_interface(*i->second, "in", i->second->name))
1145 pass_vars.push_back(i->second);
1149 if(stage->type==Stage::GEOMETRY)
1151 /* Special case for geometry shader: copy gl_Position from input to
1153 InterfaceBlockReference *ref = new InterfaceBlockReference;
1154 ref->name = "gl_in";
1156 BinaryExpression *subscript = new BinaryExpression;
1157 subscript->left = ref;
1158 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
1159 subscript->right = pass.subscript;
1161 MemberAccess *memacc = new MemberAccess;
1162 memacc->left = subscript;
1163 memacc->member = "gl_Position";
1165 insert_assignment("gl_Position", memacc);
1168 for(vector<VariableDeclaration *>::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i)
1170 string out_name = change_prefix((*i)->name, out_prefix);
1171 generate_interface(**i, "out", out_name);
1173 VariableReference *ref = new VariableReference;
1174 ref->name = (*i)->name;
1177 BinaryExpression *subscript = new BinaryExpression;
1178 subscript->left = ref;
1179 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
1180 subscript->right = pass.subscript;
1181 insert_assignment(out_name, subscript);
1184 insert_assignment(out_name, ref);
1187 nodes_to_remove.insert(&pass);