2 #include <msp/core/hash.h>
3 #include <msp/core/raii.h>
4 #include <msp/strings/lexicalcast.h>
14 void DeclarationCombiner::apply(Stage &stage)
16 stage.content.visit(*this);
17 NodeRemover().apply(stage, nodes_to_remove);
20 void DeclarationCombiner::visit(Block &block)
25 TraversingVisitor::visit(block);
28 void DeclarationCombiner::visit(VariableDeclaration &var)
30 VariableDeclaration *&ptr = variables[var.name];
34 if(var.init_expression)
35 ptr->init_expression = var.init_expression;
40 for(vector<Layout::Qualifier>::iterator i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); ++i)
43 for(vector<Layout::Qualifier>::iterator j=ptr->layout->qualifiers.begin(); (!found && j!=ptr->layout->qualifiers.end()); ++j)
46 j->has_value = i->value;
52 ptr->layout->qualifiers.push_back(*i);
56 ptr->layout = var.layout;
58 nodes_to_remove.insert(&var);
65 ConstantSpecializer::ConstantSpecializer():
69 void ConstantSpecializer::apply(Stage &stage, const map<string, int> *v)
72 stage.content.visit(*this);
75 void ConstantSpecializer::visit(VariableDeclaration &var)
77 bool specializable = false;
80 vector<Layout::Qualifier> &qualifiers = var.layout->qualifiers;
81 for(vector<Layout::Qualifier>::iterator i=qualifiers.begin(); i!=qualifiers.end(); ++i)
82 if(i->name=="constant_id")
88 i->value = hash32(var.name)&0x7FFFFFFF;
92 if(qualifiers.empty())
96 if(specializable && values)
98 map<string, int>::const_iterator i = values->find(var.name);
101 RefPtr<Literal> literal = new Literal;
104 literal->token = (i->second ? "true" : "false");
105 literal->value = static_cast<bool>(i->second);
107 else if(var.type=="int")
109 literal->token = lexical_cast<string>(i->second);
110 literal->value = i->second;
112 var.init_expression = literal;
118 void BlockHierarchyResolver::enter(Block &block)
120 r_any_resolved |= (current_block!=block.parent);
121 block.parent = current_block;
125 TypeResolver::TypeResolver():
128 r_any_resolved(false)
131 bool TypeResolver::apply(Stage &s)
135 r_any_resolved = false;
136 s.content.visit(*this);
137 return r_any_resolved;
140 TypeDeclaration *TypeResolver::get_or_create_array_type(TypeDeclaration &type)
142 map<TypeDeclaration *, TypeDeclaration *>::iterator i = array_types.find(&type);
143 if(i!=array_types.end())
146 BasicTypeDeclaration *array = new BasicTypeDeclaration;
147 array->source = BUILTIN_SOURCE;
148 array->name = type.name+"[]";
149 array->kind = BasicTypeDeclaration::ARRAY;
150 array->base = type.name;
151 array->base_type = &type;
152 stage->content.body.insert(type_insert_point, array);
153 array_types[&type] = array;
157 void TypeResolver::resolve_type(TypeDeclaration *&type, const string &name, bool array)
159 TypeDeclaration *resolved = 0;
160 map<string, TypeDeclaration *>::iterator i = stage->types.find(name);
161 if(i!=stage->types.end())
163 map<TypeDeclaration *, TypeDeclaration *>::iterator j = alias_map.find(i->second);
164 resolved = (j!=alias_map.end() ? j->second : i->second);
167 if(resolved && array)
168 resolved = get_or_create_array_type(*resolved);
170 r_any_resolved |= (resolved!=type);
174 void TypeResolver::visit(Block &block)
176 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
179 type_insert_point = i;
184 void TypeResolver::visit(BasicTypeDeclaration &type)
186 resolve_type(type.base_type, type.base, false);
188 if(type.kind==BasicTypeDeclaration::VECTOR && type.base_type)
189 if(BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type))
190 if(basic_base->kind==BasicTypeDeclaration::VECTOR)
192 type.kind = BasicTypeDeclaration::MATRIX;
193 type.size |= basic_base->size<<16;
196 if(type.kind==BasicTypeDeclaration::ALIAS && type.base_type)
197 alias_map[&type] = type.base_type;
198 else if(type.kind==BasicTypeDeclaration::ARRAY && type.base_type)
199 array_types[type.base_type] = &type;
201 stage->types.insert(make_pair(type.name, &type));
204 void TypeResolver::visit(ImageTypeDeclaration &type)
206 resolve_type(type.base_type, type.base, false);
207 stage->types.insert(make_pair(type.name, &type));
210 void TypeResolver::visit(StructDeclaration &strct)
212 stage->types.insert(make_pair(strct.name, &strct));
213 TraversingVisitor::visit(strct);
216 void TypeResolver::visit(VariableDeclaration &var)
218 resolve_type(var.type_declaration, var.type, var.array);
219 if(iface_block && var.interface==iface_block->interface)
220 var.interface.clear();
223 void TypeResolver::visit(InterfaceBlock &iface)
227 SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
228 iface.members->visit(*this);
230 StructDeclaration *strct = new StructDeclaration;
231 strct->source = INTERNAL_SOURCE;
232 strct->name = format("_%s_%s", iface.interface, iface.name);
233 strct->members.body.splice(strct->members.body.begin(), iface.members->body);
234 stage->content.body.insert(type_insert_point, strct);
235 stage->types.insert(make_pair(strct->name, strct));
238 strct->interface_block = &iface;
239 iface.struct_declaration = strct;
242 TypeDeclaration *type = iface.struct_declaration;
243 if(type && iface.array)
244 type = get_or_create_array_type(*type);
245 r_any_resolved = (type!=iface.type_declaration);
246 iface.type_declaration = type;
249 void TypeResolver::visit(FunctionDeclaration &func)
251 resolve_type(func.return_type_declaration, func.return_type, false);
252 TraversingVisitor::visit(func);
256 VariableResolver::VariableResolver():
258 r_any_resolved(false),
259 record_target(false),
260 r_self_referencing(false)
263 bool VariableResolver::apply(Stage &s)
266 s.interface_blocks.clear();
267 r_any_resolved = false;
268 s.content.visit(*this);
269 return r_any_resolved;
272 void VariableResolver::enter(Block &block)
274 block.variables.clear();
277 void VariableResolver::visit(RefPtr<Expression> &expr)
279 r_replacement_expr = 0;
281 if(r_replacement_expr)
283 expr = r_replacement_expr;
284 /* Don't record assignment target when doing a replacement, because chain
285 information won't be correct. */
286 r_assignment_target.declaration = 0;
287 r_any_resolved = true;
289 r_replacement_expr = 0;
292 void VariableResolver::check_assignment_target(Statement *declaration)
296 if(r_assignment_target.declaration)
298 /* More than one reference found in assignment target. Unable to
299 determine what the primary target is. */
300 record_target = false;
301 r_assignment_target.declaration = 0;
304 r_assignment_target.declaration = declaration;
306 // TODO This check is overly broad and may prevent some optimizations.
307 else if(declaration && declaration==r_assignment_target.declaration)
308 r_self_referencing = true;
311 void VariableResolver::visit(VariableReference &var)
313 VariableDeclaration *declaration = 0;
315 /* Look for variable declarations in the block hierarchy first. Interface
316 blocks are always defined in the top level so we can't accidentally skip
318 for(Block *block=current_block; (!declaration && block); block=block->parent)
320 map<string, VariableDeclaration *>::iterator i = block->variables.find(var.name);
321 if(i!=block->variables.end())
322 declaration = i->second;
327 const map<string, InterfaceBlock *> &blocks = stage->interface_blocks;
328 map<string, InterfaceBlock *>::const_iterator i = blocks.find("_"+var.name);
331 /* The name refers to an interface block with an instance name rather
332 than a variable. Prepare a new syntax tree node accordingly. */
333 InterfaceBlockReference *iface_ref = new InterfaceBlockReference;
334 iface_ref->source = var.source;
335 iface_ref->line = var.line;
336 iface_ref->name = var.name;
337 iface_ref->declaration = i->second;
338 r_replacement_expr = iface_ref;
342 // Look for the variable in anonymous interface blocks.
343 for(i=blocks.begin(); (!declaration && i!=blocks.end()); ++i)
344 if(i->second->instance_name.empty() && i->second->struct_declaration)
346 const map<string, VariableDeclaration *> &iface_vars = i->second->struct_declaration->members.variables;
347 map<string, VariableDeclaration *>::const_iterator j = iface_vars.find(var.name);
348 if(j!=iface_vars.end())
349 declaration = j->second;
354 r_any_resolved |= (declaration!=var.declaration);
355 var.declaration = declaration;
357 check_assignment_target(var.declaration);
360 void VariableResolver::visit(InterfaceBlockReference &iface)
362 map<string, InterfaceBlock *>::iterator i = stage->interface_blocks.find("_"+iface.name);
363 InterfaceBlock *declaration = (i!=stage->interface_blocks.end() ? i->second : 0);
364 r_any_resolved |= (declaration!=iface.declaration);
365 iface.declaration = declaration;
367 check_assignment_target(iface.declaration);
370 void VariableResolver::add_to_chain(Assignment::Target::ChainType type, unsigned index)
372 if(r_assignment_target.chain_len<7)
373 r_assignment_target.chain[r_assignment_target.chain_len] = type | min<unsigned>(index, 0x3F);
374 ++r_assignment_target.chain_len;
377 void VariableResolver::visit(MemberAccess &memacc)
379 TraversingVisitor::visit(memacc);
381 VariableDeclaration *declaration = 0;
382 if(StructDeclaration *strct = dynamic_cast<StructDeclaration *>(memacc.left->type))
384 map<string, VariableDeclaration *>::iterator i = strct->members.variables.find(memacc.member);
385 if(i!=strct->members.variables.end())
387 declaration = i->second;
392 for(NodeList<Statement>::const_iterator j=strct->members.body.begin(); (j!=strct->members.body.end() && j->get()!=i->second); ++j)
395 add_to_chain(Assignment::Target::MEMBER, index);
399 else if(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(memacc.left->type))
401 bool scalar_swizzle = ((basic->kind==BasicTypeDeclaration::INT || basic->kind==BasicTypeDeclaration::FLOAT) && memacc.member.size()==1);
402 bool vector_swizzle = (basic->kind==BasicTypeDeclaration::VECTOR && memacc.member.size()<=4);
403 if(scalar_swizzle || vector_swizzle)
405 static const char component_names[] = { 'x', 'r', 's', 'y', 'g', 't', 'z', 'b', 'p', 'w', 'a', 'q' };
408 UInt8 components[4] = { };
409 for(unsigned i=0; (ok && i<memacc.member.size()); ++i)
410 ok = ((components[i] = (find(component_names, component_names+12, memacc.member[i])-component_names)/3) < 4);
414 Swizzle *swizzle = new Swizzle;
415 swizzle->source = memacc.source;
416 swizzle->line = memacc.line;
417 swizzle->oper = memacc.oper;
418 swizzle->left = memacc.left;
419 swizzle->component_group = memacc.member;
420 swizzle->count = memacc.member.size();
421 copy(components, components+memacc.member.size(), swizzle->components);
422 r_replacement_expr = swizzle;
427 r_any_resolved |= (declaration!=memacc.declaration);
428 memacc.declaration = declaration;
431 void VariableResolver::visit(Swizzle &swizzle)
433 TraversingVisitor::visit(swizzle);
438 for(unsigned i=0; i<swizzle.count; ++i)
439 mask |= 1<<swizzle.components[i];
440 add_to_chain(Assignment::Target::SWIZZLE, mask);
444 void VariableResolver::visit(BinaryExpression &binary)
446 if(binary.oper->token[0]=='[')
449 /* The subscript expression is not a part of the primary assignment
451 SetFlag set(record_target, false);
458 unsigned index = 0x3F;
459 if(Literal *literal_subscript = dynamic_cast<Literal *>(binary.right.get()))
460 if(literal_subscript->value.check_type<int>())
461 index = literal_subscript->value.value<int>();
462 add_to_chain(Assignment::Target::ARRAY, index);
466 TraversingVisitor::visit(binary);
469 void VariableResolver::visit(Assignment &assign)
472 SetFlag set(record_target);
473 r_assignment_target = Assignment::Target();
475 r_any_resolved |= (r_assignment_target<assign.target || assign.target<r_assignment_target);
476 assign.target = r_assignment_target;
479 r_self_referencing = false;
481 assign.self_referencing = (r_self_referencing || assign.oper->token[0]!='=');
484 void VariableResolver::visit(VariableDeclaration &var)
486 TraversingVisitor::visit(var);
487 current_block->variables.insert(make_pair(var.name, &var));
490 void VariableResolver::visit(InterfaceBlock &iface)
492 /* Block names can be reused in different interfaces. Prefix the name with
493 the first character of the interface to avoid conflicts. */
494 stage->interface_blocks.insert(make_pair(iface.interface+iface.name, &iface));
495 if(!iface.instance_name.empty())
496 stage->interface_blocks.insert(make_pair("_"+iface.instance_name, &iface));
498 TraversingVisitor::visit(iface);
502 ExpressionResolver::ExpressionResolver():
504 r_any_resolved(false)
507 bool ExpressionResolver::apply(Stage &s)
510 r_any_resolved = false;
511 s.content.visit(*this);
512 return r_any_resolved;
515 bool ExpressionResolver::is_scalar(BasicTypeDeclaration &type)
517 return (type.kind==BasicTypeDeclaration::INT || type.kind==BasicTypeDeclaration::FLOAT);
520 bool ExpressionResolver::is_vector_or_matrix(BasicTypeDeclaration &type)
522 return (type.kind==BasicTypeDeclaration::VECTOR || type.kind==BasicTypeDeclaration::MATRIX);
525 BasicTypeDeclaration *ExpressionResolver::get_element_type(BasicTypeDeclaration &type)
527 if(is_vector_or_matrix(type) || type.kind==BasicTypeDeclaration::ARRAY)
529 BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type);
530 return (basic_base ? get_element_type(*basic_base) : 0);
536 bool ExpressionResolver::can_convert(BasicTypeDeclaration &from, BasicTypeDeclaration &to)
538 if(from.kind==BasicTypeDeclaration::INT && to.kind==BasicTypeDeclaration::FLOAT)
539 return from.size<=to.size;
540 else if(from.kind!=to.kind)
542 else if((from.kind==BasicTypeDeclaration::VECTOR || from.kind==BasicTypeDeclaration::MATRIX) && from.size==to.size)
544 BasicTypeDeclaration *from_base = dynamic_cast<BasicTypeDeclaration *>(from.base_type);
545 BasicTypeDeclaration *to_base = dynamic_cast<BasicTypeDeclaration *>(to.base_type);
546 return (from_base && to_base && can_convert(*from_base, *to_base));
552 ExpressionResolver::Compatibility ExpressionResolver::get_compatibility(BasicTypeDeclaration &left, BasicTypeDeclaration &right)
556 else if(can_convert(left, right))
557 return LEFT_CONVERTIBLE;
558 else if(can_convert(right, left))
559 return RIGHT_CONVERTIBLE;
561 return NOT_COMPATIBLE;
564 BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration::Kind kind, unsigned size)
566 for(vector<BasicTypeDeclaration *>::const_iterator i=basic_types.begin(); i!=basic_types.end(); ++i)
567 if((*i)->kind==kind && (*i)->size==size)
572 BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration &elem_type, BasicTypeDeclaration::Kind kind, unsigned size)
574 for(vector<BasicTypeDeclaration *>::const_iterator i=basic_types.begin(); i!=basic_types.end(); ++i)
575 if(get_element_type(**i)==&elem_type && (*i)->kind==kind && (*i)->size==size)
580 void ExpressionResolver::convert_to(RefPtr<Expression> &expr, BasicTypeDeclaration &type)
582 RefPtr<FunctionCall> call = new FunctionCall;
583 call->name = type.name;
584 call->constructor = true;
585 call->arguments.push_back(0);
586 call->arguments.back() = expr;
591 bool ExpressionResolver::convert_to_element(RefPtr<Expression> &expr, BasicTypeDeclaration &elem_type)
593 if(BasicTypeDeclaration *expr_type = dynamic_cast<BasicTypeDeclaration *>(expr->type))
595 BasicTypeDeclaration *to_type = &elem_type;
596 if(is_vector_or_matrix(*expr_type))
597 to_type = find_type(elem_type, expr_type->kind, expr_type->size);
600 convert_to(expr, *to_type);
608 void ExpressionResolver::resolve(Expression &expr, TypeDeclaration *type, bool lvalue)
610 r_any_resolved |= (type!=expr.type || lvalue!=expr.lvalue);
612 expr.lvalue = lvalue;
615 void ExpressionResolver::visit(Literal &literal)
617 if(literal.value.check_type<bool>())
618 resolve(literal, find_type(BasicTypeDeclaration::BOOL, 1), false);
619 else if(literal.value.check_type<int>())
620 resolve(literal, find_type(BasicTypeDeclaration::INT, 32), false);
621 else if(literal.value.check_type<float>())
622 resolve(literal, find_type(BasicTypeDeclaration::FLOAT, 32), false);
625 void ExpressionResolver::visit(ParenthesizedExpression &parexpr)
627 TraversingVisitor::visit(parexpr);
628 resolve(parexpr, parexpr.expression->type, parexpr.expression->lvalue);
631 void ExpressionResolver::visit(VariableReference &var)
634 resolve(var, var.declaration->type_declaration, true);
637 void ExpressionResolver::visit(InterfaceBlockReference &iface)
639 if(iface.declaration)
640 resolve(iface, iface.declaration->type_declaration, true);
643 void ExpressionResolver::visit(MemberAccess &memacc)
645 TraversingVisitor::visit(memacc);
647 if(memacc.declaration)
648 resolve(memacc, memacc.declaration->type_declaration, memacc.left->lvalue);
651 void ExpressionResolver::visit(Swizzle &swizzle)
653 TraversingVisitor::visit(swizzle);
655 if(BasicTypeDeclaration *left_basic = dynamic_cast<BasicTypeDeclaration *>(swizzle.left->type))
657 BasicTypeDeclaration *left_elem = get_element_type(*left_basic);
659 resolve(swizzle, left_elem, swizzle.left->lvalue);
660 else if(left_basic->kind==BasicTypeDeclaration::VECTOR && left_elem)
661 resolve(swizzle, find_type(*left_elem, left_basic->kind, swizzle.count), swizzle.left->lvalue);
665 void ExpressionResolver::visit(UnaryExpression &unary)
667 TraversingVisitor::visit(unary);
669 BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(unary.expression->type);
673 char oper = unary.oper->token[0];
676 if(basic->kind!=BasicTypeDeclaration::BOOL)
681 if(basic->kind!=BasicTypeDeclaration::INT)
684 else if(oper=='+' || oper=='-')
686 BasicTypeDeclaration *elem = get_element_type(*basic);
687 if(!elem || !is_scalar(*elem))
690 resolve(unary, basic, unary.expression->lvalue);
693 void ExpressionResolver::visit(BinaryExpression &binary, bool assign)
695 /* Binary operators are only defined for basic types (not for image or
697 BasicTypeDeclaration *basic_left = dynamic_cast<BasicTypeDeclaration *>(binary.left->type);
698 BasicTypeDeclaration *basic_right = dynamic_cast<BasicTypeDeclaration *>(binary.right->type);
699 if(!basic_left || !basic_right)
702 char oper = binary.oper->token[0];
705 /* Subscripting operates on vectors, matrices and arrays, and the right
706 operand must be an integer. */
707 if((!is_vector_or_matrix(*basic_left) && basic_left->kind!=BasicTypeDeclaration::ARRAY) || basic_right->kind!=BasicTypeDeclaration::INT)
710 resolve(binary, basic_left->base_type, binary.left->lvalue);
713 else if(basic_left->kind==BasicTypeDeclaration::ARRAY || basic_right->kind==BasicTypeDeclaration::ARRAY)
714 // No other binary operator can be used with arrays.
717 BasicTypeDeclaration *elem_left = get_element_type(*basic_left);
718 BasicTypeDeclaration *elem_right = get_element_type(*basic_right);
719 if(!elem_left || !elem_right)
722 Compatibility compat = get_compatibility(*basic_left, *basic_right);
723 Compatibility elem_compat = get_compatibility(*elem_left, *elem_right);
724 if(elem_compat==NOT_COMPATIBLE)
726 if(assign && (compat==LEFT_CONVERTIBLE || elem_compat==LEFT_CONVERTIBLE))
729 TypeDeclaration *type = 0;
730 char oper2 = binary.oper->token[1];
731 if((oper=='<' && oper2!='<') || (oper=='>' && oper2!='>'))
733 /* Relational operators compare two scalar integer or floating-point
735 if(!is_scalar(*elem_left) || !is_scalar(*elem_right) || compat==NOT_COMPATIBLE)
738 type = find_type(BasicTypeDeclaration::BOOL, 1);
740 else if((oper=='=' || oper=='!') && oper2=='=')
742 // Equality comparison can be done on any compatible types.
743 if(compat==NOT_COMPATIBLE)
746 type = find_type(BasicTypeDeclaration::BOOL, 1);
748 else if(oper2=='&' || oper2=='|' || oper2=='^')
750 // Logical operators can only be applied to booleans.
751 if(basic_left->kind!=BasicTypeDeclaration::BOOL || basic_right->kind!=BasicTypeDeclaration::BOOL)
756 else if((oper=='&' || oper=='|' || oper=='^' || oper=='%') && !oper2)
758 // Bitwise operators and modulo can only be applied to integers.
759 if(basic_left->kind!=BasicTypeDeclaration::INT || basic_right->kind!=BasicTypeDeclaration::INT)
762 type = (compat==LEFT_CONVERTIBLE ? basic_right : basic_left);
764 else if((oper=='<' || oper=='>') && oper2==oper)
766 // Shifts apply to integer scalars and vectors, with some restrictions.
767 if(elem_left->kind!=BasicTypeDeclaration::INT || elem_right->kind!=BasicTypeDeclaration::INT)
769 unsigned left_size = (basic_left->kind==BasicTypeDeclaration::INT ? 1 : basic_left->kind==BasicTypeDeclaration::VECTOR ? basic_left->size : 0);
770 unsigned right_size = (basic_right->kind==BasicTypeDeclaration::INT ? 1 : basic_right->kind==BasicTypeDeclaration::VECTOR ? basic_right->size : 0);
771 if(!left_size || (left_size==1 && right_size!=1) || (left_size>1 && right_size!=1 && right_size!=left_size))
775 // Don't perform conversion even if the operands are of different sizes.
778 else if(oper=='+' || oper=='-' || oper=='*' || oper=='/')
780 // Arithmetic operators require scalar elements.
781 if(!is_scalar(*elem_left) || !is_scalar(*elem_right))
784 if(oper=='*' && is_vector_or_matrix(*basic_left) && is_vector_or_matrix(*basic_right) &&
785 (basic_left->kind==BasicTypeDeclaration::MATRIX || basic_right->kind==BasicTypeDeclaration::MATRIX))
787 /* Multiplication has special rules when at least one operand is a
788 matrix and the other is a vector or a matrix. */
789 unsigned left_columns = basic_left->size&0xFFFF;
790 unsigned right_rows = basic_right->size;
791 if(basic_right->kind==BasicTypeDeclaration::MATRIX)
793 if(left_columns!=right_rows)
796 BasicTypeDeclaration *elem_result = (elem_compat==LEFT_CONVERTIBLE ? elem_right : elem_left);
798 if(basic_left->kind==BasicTypeDeclaration::VECTOR)
799 type = find_type(*elem_result, BasicTypeDeclaration::VECTOR, basic_right->size&0xFFFF);
800 else if(basic_right->kind==BasicTypeDeclaration::VECTOR)
801 type = find_type(*elem_result, BasicTypeDeclaration::VECTOR, basic_left->size>>16);
803 type = find_type(*elem_result, BasicTypeDeclaration::MATRIX, (basic_left->size&0xFFFF0000)|(basic_right->size&0xFFFF));
805 else if(compat==NOT_COMPATIBLE)
807 // Arithmetic between scalars and matrices or vectors is supported.
808 if(is_scalar(*basic_left) && is_vector_or_matrix(*basic_right))
809 type = (elem_compat==RIGHT_CONVERTIBLE ? find_type(*elem_left, basic_right->kind, basic_right->size) : basic_right);
810 else if(is_vector_or_matrix(*basic_left) && is_scalar(*basic_right))
811 type = (elem_compat==LEFT_CONVERTIBLE ? find_type(*elem_right, basic_left->kind, basic_left->size) : basic_left);
815 else if(compat==LEFT_CONVERTIBLE)
823 if(assign && type!=basic_left)
826 bool converted = true;
827 if(compat==LEFT_CONVERTIBLE)
828 convert_to(binary.left, *basic_right);
829 else if(compat==RIGHT_CONVERTIBLE)
830 convert_to(binary.right, *basic_left);
831 else if(elem_compat==LEFT_CONVERTIBLE)
832 converted = convert_to_element(binary.left, *elem_right);
833 else if(elem_compat==RIGHT_CONVERTIBLE)
834 converted = convert_to_element(binary.right, *elem_left);
839 resolve(binary, type, assign);
842 void ExpressionResolver::visit(BinaryExpression &binary)
844 TraversingVisitor::visit(binary);
845 visit(binary, false);
848 void ExpressionResolver::visit(Assignment &assign)
850 TraversingVisitor::visit(assign);
852 if(assign.oper->token[0]!='=')
853 return visit(assign, true);
854 else if(assign.left->type!=assign.right->type)
856 BasicTypeDeclaration *basic_left = dynamic_cast<BasicTypeDeclaration *>(assign.left->type);
857 BasicTypeDeclaration *basic_right = dynamic_cast<BasicTypeDeclaration *>(assign.right->type);
858 if(!basic_left || !basic_right)
861 Compatibility compat = get_compatibility(*basic_left, *basic_right);
862 if(compat==RIGHT_CONVERTIBLE)
863 convert_to(assign.right, *basic_left);
864 else if(compat!=SAME_TYPE)
868 resolve(assign, assign.left->type, true);
871 void ExpressionResolver::visit(FunctionCall &call)
873 TraversingVisitor::visit(call);
875 TypeDeclaration *type = 0;
877 type = call.declaration->return_type_declaration;
878 else if(call.constructor)
880 map<string, TypeDeclaration *>::const_iterator i=stage->types.find(call.name);
881 type = (i!=stage->types.end() ? i->second : 0);
883 resolve(call, type, false);
886 void ExpressionResolver::visit(BasicTypeDeclaration &type)
888 basic_types.push_back(&type);
891 void ExpressionResolver::visit(VariableDeclaration &var)
893 TraversingVisitor::visit(var);
894 if(!var.init_expression)
897 BasicTypeDeclaration *var_basic = dynamic_cast<BasicTypeDeclaration *>(var.type_declaration);
898 BasicTypeDeclaration *init_basic = dynamic_cast<BasicTypeDeclaration *>(var.init_expression->type);
899 if(!var_basic || !init_basic)
902 Compatibility compat = get_compatibility(*var_basic, *init_basic);
903 if(compat==RIGHT_CONVERTIBLE)
904 convert_to(var.init_expression, *var_basic);
908 bool FunctionResolver::apply(Stage &s)
912 r_any_resolved = false;
913 s.content.visit(*this);
914 return r_any_resolved;
917 void FunctionResolver::visit(FunctionCall &call)
919 map<string, FunctionDeclaration *>::iterator i = stage->functions.find(call.name);
920 if(i!=stage->functions.end())
921 call.declaration = i->second;
923 TraversingVisitor::visit(call);
926 void FunctionResolver::visit(FunctionDeclaration &func)
928 FunctionDeclaration *&stage_decl = stage->functions[func.name];
929 vector<FunctionDeclaration *> &decls = declarations[func.name];
930 if(func.definition==&func)
934 // Set all previous declarations to use this definition.
935 for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
937 (*i)->definition = func.definition;
938 (*i)->body.body.clear();
947 func.definition = stage_decl->definition;
949 decls.push_back(&func);
951 TraversingVisitor::visit(func);
955 InterfaceGenerator::InterfaceGenerator():
957 function_scope(false),
959 iface_target_block(0)
962 string InterfaceGenerator::get_out_prefix(Stage::Type type)
964 if(type==Stage::VERTEX)
966 else if(type==Stage::GEOMETRY)
972 void InterfaceGenerator::apply(Stage &s)
975 iface_target_block = &stage->content;
977 in_prefix = get_out_prefix(stage->previous->type);
978 out_prefix = get_out_prefix(stage->type);
979 s.content.visit(*this);
980 NodeRemover().apply(s, nodes_to_remove);
983 void InterfaceGenerator::visit(Block &block)
985 SetForScope<Block *> set_block(current_block, &block);
986 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
988 assignment_insert_point = i;
989 if(&block==&stage->content)
990 iface_insert_point = i;
996 string InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
998 unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
999 return prefix+name.substr(offset);
1002 VariableDeclaration *InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
1004 if(stage->content.variables.count(name))
1007 VariableDeclaration* iface_var = new VariableDeclaration;
1008 iface_var->sampling = var.sampling;
1009 iface_var->interface = iface;
1010 iface_var->type = var.type;
1011 iface_var->name = name;
1012 /* Geometry shader inputs are always arrays. But if we're bringing in an
1013 entire block, the array is on the block and not individual variables. */
1014 if(stage->type==Stage::GEOMETRY && !copy_block)
1015 iface_var->array = ((var.array && var.interface!="in") || iface=="in");
1017 iface_var->array = var.array;
1018 if(iface_var->array)
1019 iface_var->array_size = var.array_size;
1022 iface_var->layout = var.layout;
1023 iface_var->linked_declaration = &var;
1024 var.linked_declaration = iface_var;
1027 iface_target_block->body.insert(iface_insert_point, iface_var);
1028 iface_target_block->variables.insert(make_pair(name, iface_var));
1033 InterfaceBlock *InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
1035 if(stage->interface_blocks.count("in"+out_block.name))
1038 InterfaceBlock *in_block = new InterfaceBlock;
1039 in_block->interface = "in";
1040 in_block->name = out_block.name;
1041 in_block->members = new Block;
1042 in_block->instance_name = out_block.instance_name;
1043 if(stage->type==Stage::GEOMETRY)
1044 in_block->array = true;
1046 in_block->array = out_block.array;
1047 in_block->linked_block = &out_block;
1048 out_block.linked_block = in_block;
1051 SetFlag set_copy(copy_block, true);
1052 SetForScope<Block *> set_target(iface_target_block, in_block->members.get());
1053 SetForScope<NodeList<Statement>::iterator> set_ins_pt(iface_insert_point, in_block->members->body.end());
1054 if(out_block.struct_declaration)
1055 out_block.struct_declaration->members.visit(*this);
1056 else if(out_block.members)
1057 out_block.members->visit(*this);
1060 iface_target_block->body.insert(iface_insert_point, in_block);
1061 stage->interface_blocks.insert(make_pair("in"+in_block->name, in_block));
1062 if(!in_block->instance_name.empty())
1063 stage->interface_blocks.insert(make_pair("_"+in_block->instance_name, in_block));
1065 SetFlag set_scope(function_scope, false);
1066 SetForScope<Block *> set_block(current_block, &stage->content);
1067 in_block->visit(*this);
1072 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
1074 Assignment *assign = new Assignment;
1075 VariableReference *ref = new VariableReference;
1078 assign->oper = &Operator::get_operator("=", Operator::BINARY);
1079 assign->right = right;
1081 ExpressionStatement *stmt = new ExpressionStatement;
1082 stmt->expression = assign;
1083 current_block->body.insert(assignment_insert_point, stmt);
1089 void InterfaceGenerator::visit(VariableReference &var)
1091 if(var.declaration || !stage->previous)
1093 /* Don't pull a variable from previous stage if we just generated an output
1094 interface in this stage */
1095 if(stage->content.variables.count(var.name))
1098 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
1099 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
1100 if(i==prev_vars.end() || i->second->interface!="out")
1101 i = prev_vars.find(in_prefix+var.name);
1102 if(i!=prev_vars.end() && i->second->interface=="out")
1104 generate_interface(*i->second, "in", i->second->name);
1105 var.name = i->second->name;
1109 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
1110 map<string, InterfaceBlock *>::const_iterator j = prev_blocks.find("_"+var.name);
1111 if(j!=prev_blocks.end() && j->second->interface=="out")
1113 generate_interface(*j->second);
1114 /* Let VariableResolver convert the variable reference into an interface
1119 for(j=prev_blocks.begin(); j!=prev_blocks.end(); ++j)
1120 if(j->second->instance_name.empty() && j->second->struct_declaration)
1122 const map<string, VariableDeclaration *> &iface_vars = j->second->struct_declaration->members.variables;
1123 i = iface_vars.find(var.name);
1124 if(i!=iface_vars.end())
1126 generate_interface(*j->second);
1132 void InterfaceGenerator::visit(VariableDeclaration &var)
1135 generate_interface(var, "in", var.name);
1136 else if(var.interface=="out")
1138 /* For output variables in function scope, generate a global interface
1139 and replace the local declaration with an assignment. */
1140 VariableDeclaration *out_var = 0;
1141 if(function_scope && (out_var=generate_interface(var, "out", var.name)))
1143 out_var->source = var.source;
1144 out_var->line = var.line;
1145 nodes_to_remove.insert(&var);
1146 if(var.init_expression)
1148 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
1149 stmt.source = var.source;
1150 stmt.line = var.line;
1155 else if(var.interface=="in")
1157 /* Try to link input variables in global scope with output variables from
1159 if(current_block==&stage->content && !var.linked_declaration && stage->previous)
1161 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
1162 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
1163 if(i!=prev_vars.end() && i->second->interface=="out")
1165 var.linked_declaration = i->second;
1166 i->second->linked_declaration = &var;
1171 TraversingVisitor::visit(var);
1174 void InterfaceGenerator::visit(InterfaceBlock &iface)
1176 if(iface.interface=="in")
1178 /* Try to link input blocks with output blocks sharing the same block
1179 name from previous stage. */
1180 if(!iface.linked_block && stage->previous)
1182 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
1183 map<string, InterfaceBlock *>::const_iterator i = prev_blocks.find("out"+iface.name);
1184 if(i!=prev_blocks.end())
1186 iface.linked_block = i->second;
1187 i->second->linked_block = &iface;
1192 TraversingVisitor::visit(iface);
1195 void InterfaceGenerator::visit(FunctionDeclaration &func)
1197 SetFlag set_scope(function_scope, true);
1198 // Skip parameters because they're not useful here
1199 func.body.visit(*this);
1202 void InterfaceGenerator::visit(Passthrough &pass)
1204 vector<VariableDeclaration *> pass_vars;
1206 // Pass through all input variables of this stage.
1207 for(map<string, VariableDeclaration *>::const_iterator i=stage->content.variables.begin(); i!=stage->content.variables.end(); ++i)
1208 if(i->second->interface=="in")
1209 pass_vars.push_back(i->second);
1213 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
1214 for(map<string, VariableDeclaration *>::const_iterator i=prev_vars.begin(); i!=prev_vars.end(); ++i)
1216 if(i->second->interface!="out")
1219 /* Pass through output variables from the previous stage, but only
1220 those which are not already linked to an input here. */
1221 if(!i->second->linked_declaration && generate_interface(*i->second, "in", i->second->name))
1222 pass_vars.push_back(i->second);
1226 if(stage->type==Stage::GEOMETRY)
1228 /* Special case for geometry shader: copy gl_Position from input to
1230 InterfaceBlockReference *ref = new InterfaceBlockReference;
1231 ref->name = "gl_in";
1233 BinaryExpression *subscript = new BinaryExpression;
1234 subscript->left = ref;
1235 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
1236 subscript->right = pass.subscript;
1238 MemberAccess *memacc = new MemberAccess;
1239 memacc->left = subscript;
1240 memacc->member = "gl_Position";
1242 insert_assignment("gl_Position", memacc);
1245 for(vector<VariableDeclaration *>::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i)
1247 string out_name = change_prefix((*i)->name, out_prefix);
1248 generate_interface(**i, "out", out_name);
1250 VariableReference *ref = new VariableReference;
1251 ref->name = (*i)->name;
1254 BinaryExpression *subscript = new BinaryExpression;
1255 subscript->left = ref;
1256 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
1257 subscript->right = pass.subscript;
1258 insert_assignment(out_name, subscript);
1261 insert_assignment(out_name, ref);
1264 nodes_to_remove.insert(&pass);