2 #include <msp/core/hash.h>
3 #include <msp/core/raii.h>
4 #include <msp/strings/lexicalcast.h>
5 #include <msp/strings/utils.h>
15 void DeclarationCombiner::apply(Stage &stage)
17 stage.content.visit(*this);
18 NodeRemover().apply(stage, nodes_to_remove);
21 void DeclarationCombiner::visit(Block &block)
26 TraversingVisitor::visit(block);
29 void DeclarationCombiner::visit(VariableDeclaration &var)
31 VariableDeclaration *&ptr = variables[var.name];
35 if(var.init_expression)
36 ptr->init_expression = var.init_expression;
41 for(vector<Layout::Qualifier>::iterator i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); ++i)
44 for(vector<Layout::Qualifier>::iterator j=ptr->layout->qualifiers.begin(); (!found && j!=ptr->layout->qualifiers.end()); ++j)
47 j->has_value = i->value;
53 ptr->layout->qualifiers.push_back(*i);
57 ptr->layout = var.layout;
59 nodes_to_remove.insert(&var);
66 ConstantSpecializer::ConstantSpecializer():
70 void ConstantSpecializer::apply(Stage &stage, const map<string, int> *v)
73 stage.content.visit(*this);
76 void ConstantSpecializer::visit(VariableDeclaration &var)
78 bool specializable = false;
81 vector<Layout::Qualifier> &qualifiers = var.layout->qualifiers;
82 for(vector<Layout::Qualifier>::iterator i=qualifiers.begin(); i!=qualifiers.end(); ++i)
83 if(i->name=="constant_id")
89 i->value = hash32(var.name)&0x7FFFFFFF;
93 if(qualifiers.empty())
97 if(specializable && values)
99 map<string, int>::const_iterator i = values->find(var.name);
102 RefPtr<Literal> literal = new Literal;
105 literal->token = (i->second ? "true" : "false");
106 literal->value = static_cast<bool>(i->second);
108 else if(var.type=="int")
110 literal->token = lexical_cast<string>(i->second);
111 literal->value = i->second;
113 var.init_expression = literal;
119 void BlockHierarchyResolver::enter(Block &block)
121 r_any_resolved |= (current_block!=block.parent);
122 block.parent = current_block;
126 TypeResolver::TypeResolver():
129 r_any_resolved(false)
132 bool TypeResolver::apply(Stage &s)
136 r_any_resolved = false;
137 s.content.visit(*this);
138 return r_any_resolved;
141 TypeDeclaration *TypeResolver::get_or_create_array_type(TypeDeclaration &type)
143 map<TypeDeclaration *, TypeDeclaration *>::iterator i = array_types.find(&type);
144 if(i!=array_types.end())
147 BasicTypeDeclaration *array = new BasicTypeDeclaration;
148 array->source = BUILTIN_SOURCE;
149 array->name = type.name+"[]";
150 array->kind = BasicTypeDeclaration::ARRAY;
151 array->base = type.name;
152 array->base_type = &type;
153 stage->content.body.insert(type_insert_point, array);
154 array_types[&type] = array;
158 void TypeResolver::resolve_type(TypeDeclaration *&type, const string &name, bool array)
160 TypeDeclaration *resolved = 0;
161 map<string, TypeDeclaration *>::iterator i = stage->types.find(name);
162 if(i!=stage->types.end())
164 map<TypeDeclaration *, TypeDeclaration *>::iterator j = alias_map.find(i->second);
165 resolved = (j!=alias_map.end() ? j->second : i->second);
168 if(resolved && array)
169 resolved = get_or_create_array_type(*resolved);
171 r_any_resolved |= (resolved!=type);
175 void TypeResolver::visit(Block &block)
177 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
180 type_insert_point = i;
185 void TypeResolver::visit(BasicTypeDeclaration &type)
187 resolve_type(type.base_type, type.base, false);
189 if(type.kind==BasicTypeDeclaration::VECTOR && type.base_type)
190 if(BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type))
191 if(basic_base->kind==BasicTypeDeclaration::VECTOR)
193 type.kind = BasicTypeDeclaration::MATRIX;
194 /* A matrix's base type is its column vector type. This will put
195 the column vector's size, i.e. the matrix's row count, in the high
197 type.size |= basic_base->size<<16;
200 if(type.kind==BasicTypeDeclaration::ALIAS && type.base_type)
201 alias_map[&type] = type.base_type;
202 else if(type.kind==BasicTypeDeclaration::ARRAY && type.base_type)
203 array_types[type.base_type] = &type;
205 stage->types.insert(make_pair(type.name, &type));
208 void TypeResolver::visit(ImageTypeDeclaration &type)
210 resolve_type(type.base_type, type.base, false);
211 stage->types.insert(make_pair(type.name, &type));
214 void TypeResolver::visit(StructDeclaration &strct)
216 stage->types.insert(make_pair(strct.name, &strct));
217 TraversingVisitor::visit(strct);
220 void TypeResolver::visit(VariableDeclaration &var)
222 resolve_type(var.type_declaration, var.type, var.array);
223 if(iface_block && var.interface==iface_block->interface)
224 var.interface.clear();
227 void TypeResolver::visit(InterfaceBlock &iface)
231 SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
232 iface.members->visit(*this);
234 StructDeclaration *strct = new StructDeclaration;
235 strct->source = INTERNAL_SOURCE;
236 strct->name = format("_%s_%s", iface.interface, iface.name);
237 strct->members.body.splice(strct->members.body.begin(), iface.members->body);
238 stage->content.body.insert(type_insert_point, strct);
239 stage->types.insert(make_pair(strct->name, strct));
242 strct->interface_block = &iface;
243 iface.struct_declaration = strct;
246 TypeDeclaration *type = iface.struct_declaration;
247 if(type && iface.array)
248 type = get_or_create_array_type(*type);
249 r_any_resolved = (type!=iface.type_declaration);
250 iface.type_declaration = type;
253 void TypeResolver::visit(FunctionDeclaration &func)
255 resolve_type(func.return_type_declaration, func.return_type, false);
256 TraversingVisitor::visit(func);
260 VariableResolver::VariableResolver():
262 r_any_resolved(false),
263 record_target(false),
264 r_self_referencing(false)
267 bool VariableResolver::apply(Stage &s)
270 s.interface_blocks.clear();
271 r_any_resolved = false;
272 s.content.visit(*this);
273 return r_any_resolved;
276 void VariableResolver::enter(Block &block)
278 block.variables.clear();
281 void VariableResolver::visit(RefPtr<Expression> &expr)
283 r_replacement_expr = 0;
285 if(r_replacement_expr)
287 expr = r_replacement_expr;
288 /* Don't record assignment target when doing a replacement, because chain
289 information won't be correct. */
290 r_assignment_target.declaration = 0;
291 r_any_resolved = true;
293 r_replacement_expr = 0;
296 void VariableResolver::check_assignment_target(Statement *declaration)
300 if(r_assignment_target.declaration)
302 /* More than one reference found in assignment target. Unable to
303 determine what the primary target is. */
304 record_target = false;
305 r_assignment_target.declaration = 0;
308 r_assignment_target.declaration = declaration;
310 // TODO This check is overly broad and may prevent some optimizations.
311 else if(declaration && declaration==r_assignment_target.declaration)
312 r_self_referencing = true;
315 void VariableResolver::visit(VariableReference &var)
317 VariableDeclaration *declaration = 0;
319 /* Look for variable declarations in the block hierarchy first. Interface
320 blocks are always defined in the top level so we can't accidentally skip
322 for(Block *block=current_block; (!declaration && block); block=block->parent)
324 map<string, VariableDeclaration *>::iterator i = block->variables.find(var.name);
325 if(i!=block->variables.end())
326 declaration = i->second;
331 const map<string, InterfaceBlock *> &blocks = stage->interface_blocks;
332 map<string, InterfaceBlock *>::const_iterator i = blocks.find("_"+var.name);
335 /* The name refers to an interface block with an instance name rather
336 than a variable. Prepare a new syntax tree node accordingly. */
337 InterfaceBlockReference *iface_ref = new InterfaceBlockReference;
338 iface_ref->source = var.source;
339 iface_ref->line = var.line;
340 iface_ref->name = var.name;
341 iface_ref->declaration = i->second;
342 r_replacement_expr = iface_ref;
346 // Look for the variable in anonymous interface blocks.
347 for(i=blocks.begin(); (!declaration && i!=blocks.end()); ++i)
348 if(i->second->instance_name.empty() && i->second->struct_declaration)
350 const map<string, VariableDeclaration *> &iface_vars = i->second->struct_declaration->members.variables;
351 map<string, VariableDeclaration *>::const_iterator j = iface_vars.find(var.name);
352 if(j!=iface_vars.end())
353 declaration = j->second;
358 r_any_resolved |= (declaration!=var.declaration);
359 var.declaration = declaration;
361 check_assignment_target(var.declaration);
364 void VariableResolver::visit(InterfaceBlockReference &iface)
366 map<string, InterfaceBlock *>::iterator i = stage->interface_blocks.find("_"+iface.name);
367 InterfaceBlock *declaration = (i!=stage->interface_blocks.end() ? i->second : 0);
368 r_any_resolved |= (declaration!=iface.declaration);
369 iface.declaration = declaration;
371 check_assignment_target(iface.declaration);
374 void VariableResolver::add_to_chain(Assignment::Target::ChainType type, unsigned index)
376 if(r_assignment_target.chain_len<7)
377 r_assignment_target.chain[r_assignment_target.chain_len] = type | min<unsigned>(index, 0x3F);
378 ++r_assignment_target.chain_len;
381 void VariableResolver::visit(MemberAccess &memacc)
383 TraversingVisitor::visit(memacc);
385 VariableDeclaration *declaration = 0;
386 if(StructDeclaration *strct = dynamic_cast<StructDeclaration *>(memacc.left->type))
388 map<string, VariableDeclaration *>::iterator i = strct->members.variables.find(memacc.member);
389 if(i!=strct->members.variables.end())
391 declaration = i->second;
396 for(NodeList<Statement>::const_iterator j=strct->members.body.begin(); (j!=strct->members.body.end() && j->get()!=i->second); ++j)
399 add_to_chain(Assignment::Target::MEMBER, index);
403 else if(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(memacc.left->type))
405 bool scalar_swizzle = ((basic->kind==BasicTypeDeclaration::INT || basic->kind==BasicTypeDeclaration::FLOAT) && memacc.member.size()==1);
406 bool vector_swizzle = (basic->kind==BasicTypeDeclaration::VECTOR && memacc.member.size()<=4);
407 if(scalar_swizzle || vector_swizzle)
409 static const char component_names[] = { 'x', 'r', 's', 'y', 'g', 't', 'z', 'b', 'p', 'w', 'a', 'q' };
412 UInt8 components[4] = { };
413 for(unsigned i=0; (ok && i<memacc.member.size()); ++i)
414 ok = ((components[i] = (find(component_names, component_names+12, memacc.member[i])-component_names)/3) < 4);
418 Swizzle *swizzle = new Swizzle;
419 swizzle->source = memacc.source;
420 swizzle->line = memacc.line;
421 swizzle->oper = memacc.oper;
422 swizzle->left = memacc.left;
423 swizzle->component_group = memacc.member;
424 swizzle->count = memacc.member.size();
425 copy(components, components+memacc.member.size(), swizzle->components);
426 r_replacement_expr = swizzle;
431 r_any_resolved |= (declaration!=memacc.declaration);
432 memacc.declaration = declaration;
435 void VariableResolver::visit(Swizzle &swizzle)
437 TraversingVisitor::visit(swizzle);
442 for(unsigned i=0; i<swizzle.count; ++i)
443 mask |= 1<<swizzle.components[i];
444 add_to_chain(Assignment::Target::SWIZZLE, mask);
448 void VariableResolver::visit(BinaryExpression &binary)
450 if(binary.oper->token[0]=='[')
453 /* The subscript expression is not a part of the primary assignment
455 SetFlag set(record_target, false);
462 unsigned index = 0x3F;
463 if(Literal *literal_subscript = dynamic_cast<Literal *>(binary.right.get()))
464 if(literal_subscript->value.check_type<int>())
465 index = literal_subscript->value.value<int>();
466 add_to_chain(Assignment::Target::ARRAY, index);
470 TraversingVisitor::visit(binary);
473 void VariableResolver::visit(Assignment &assign)
476 SetFlag set(record_target);
477 r_assignment_target = Assignment::Target();
479 r_any_resolved |= (r_assignment_target<assign.target || assign.target<r_assignment_target);
480 assign.target = r_assignment_target;
483 r_self_referencing = false;
485 assign.self_referencing = (r_self_referencing || assign.oper->token[0]!='=');
488 void VariableResolver::visit(VariableDeclaration &var)
490 TraversingVisitor::visit(var);
491 current_block->variables.insert(make_pair(var.name, &var));
494 void VariableResolver::visit(InterfaceBlock &iface)
496 /* Block names can be reused in different interfaces. Prefix the name with
497 the first character of the interface to avoid conflicts. */
498 stage->interface_blocks.insert(make_pair(iface.interface+iface.name, &iface));
499 if(!iface.instance_name.empty())
500 stage->interface_blocks.insert(make_pair("_"+iface.instance_name, &iface));
502 TraversingVisitor::visit(iface);
506 ExpressionResolver::ExpressionResolver():
508 r_any_resolved(false)
511 bool ExpressionResolver::apply(Stage &s)
514 r_any_resolved = false;
515 s.content.visit(*this);
516 return r_any_resolved;
519 bool ExpressionResolver::is_scalar(BasicTypeDeclaration &type)
521 return (type.kind==BasicTypeDeclaration::INT || type.kind==BasicTypeDeclaration::FLOAT);
524 bool ExpressionResolver::is_vector_or_matrix(BasicTypeDeclaration &type)
526 return (type.kind==BasicTypeDeclaration::VECTOR || type.kind==BasicTypeDeclaration::MATRIX);
529 BasicTypeDeclaration *ExpressionResolver::get_element_type(BasicTypeDeclaration &type)
531 if(is_vector_or_matrix(type) || type.kind==BasicTypeDeclaration::ARRAY)
533 BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type);
534 return (basic_base ? get_element_type(*basic_base) : 0);
540 bool ExpressionResolver::can_convert(BasicTypeDeclaration &from, BasicTypeDeclaration &to)
542 if(from.kind==BasicTypeDeclaration::INT && to.kind==BasicTypeDeclaration::FLOAT)
543 return from.size<=to.size;
544 else if(from.kind!=to.kind)
546 else if((from.kind==BasicTypeDeclaration::VECTOR || from.kind==BasicTypeDeclaration::MATRIX) && from.size==to.size)
548 BasicTypeDeclaration *from_base = dynamic_cast<BasicTypeDeclaration *>(from.base_type);
549 BasicTypeDeclaration *to_base = dynamic_cast<BasicTypeDeclaration *>(to.base_type);
550 return (from_base && to_base && can_convert(*from_base, *to_base));
556 ExpressionResolver::Compatibility ExpressionResolver::get_compatibility(BasicTypeDeclaration &left, BasicTypeDeclaration &right)
560 else if(can_convert(left, right))
561 return LEFT_CONVERTIBLE;
562 else if(can_convert(right, left))
563 return RIGHT_CONVERTIBLE;
565 return NOT_COMPATIBLE;
568 BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration::Kind kind, unsigned size)
570 for(vector<BasicTypeDeclaration *>::const_iterator i=basic_types.begin(); i!=basic_types.end(); ++i)
571 if((*i)->kind==kind && (*i)->size==size)
576 BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration &elem_type, BasicTypeDeclaration::Kind kind, unsigned size)
578 for(vector<BasicTypeDeclaration *>::const_iterator i=basic_types.begin(); i!=basic_types.end(); ++i)
579 if(get_element_type(**i)==&elem_type && (*i)->kind==kind && (*i)->size==size)
584 void ExpressionResolver::convert_to(RefPtr<Expression> &expr, BasicTypeDeclaration &type)
586 RefPtr<FunctionCall> call = new FunctionCall;
587 call->name = type.name;
588 call->constructor = true;
589 call->arguments.push_back(0);
590 call->arguments.back() = expr;
595 bool ExpressionResolver::convert_to_element(RefPtr<Expression> &expr, BasicTypeDeclaration &elem_type)
597 if(BasicTypeDeclaration *expr_basic = dynamic_cast<BasicTypeDeclaration *>(expr->type))
599 BasicTypeDeclaration *to_type = &elem_type;
600 if(is_vector_or_matrix(*expr_basic))
601 to_type = find_type(elem_type, expr_basic->kind, expr_basic->size);
604 convert_to(expr, *to_type);
612 void ExpressionResolver::resolve(Expression &expr, TypeDeclaration *type, bool lvalue)
614 r_any_resolved |= (type!=expr.type || lvalue!=expr.lvalue);
616 expr.lvalue = lvalue;
619 void ExpressionResolver::visit(Literal &literal)
621 if(literal.value.check_type<bool>())
622 resolve(literal, find_type(BasicTypeDeclaration::BOOL, 1), false);
623 else if(literal.value.check_type<int>())
624 resolve(literal, find_type(BasicTypeDeclaration::INT, 32), false);
625 else if(literal.value.check_type<float>())
626 resolve(literal, find_type(BasicTypeDeclaration::FLOAT, 32), false);
629 void ExpressionResolver::visit(ParenthesizedExpression &parexpr)
631 TraversingVisitor::visit(parexpr);
632 resolve(parexpr, parexpr.expression->type, parexpr.expression->lvalue);
635 void ExpressionResolver::visit(VariableReference &var)
638 resolve(var, var.declaration->type_declaration, true);
641 void ExpressionResolver::visit(InterfaceBlockReference &iface)
643 if(iface.declaration)
644 resolve(iface, iface.declaration->type_declaration, true);
647 void ExpressionResolver::visit(MemberAccess &memacc)
649 TraversingVisitor::visit(memacc);
651 if(memacc.declaration)
652 resolve(memacc, memacc.declaration->type_declaration, memacc.left->lvalue);
655 void ExpressionResolver::visit(Swizzle &swizzle)
657 TraversingVisitor::visit(swizzle);
659 if(BasicTypeDeclaration *left_basic = dynamic_cast<BasicTypeDeclaration *>(swizzle.left->type))
661 BasicTypeDeclaration *left_elem = get_element_type(*left_basic);
663 resolve(swizzle, left_elem, swizzle.left->lvalue);
664 else if(left_basic->kind==BasicTypeDeclaration::VECTOR && left_elem)
665 resolve(swizzle, find_type(*left_elem, left_basic->kind, swizzle.count), swizzle.left->lvalue);
669 void ExpressionResolver::visit(UnaryExpression &unary)
671 TraversingVisitor::visit(unary);
673 BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(unary.expression->type);
677 char oper = unary.oper->token[0];
680 if(basic->kind!=BasicTypeDeclaration::BOOL)
685 if(basic->kind!=BasicTypeDeclaration::INT)
688 else if(oper=='+' || oper=='-')
690 BasicTypeDeclaration *elem = get_element_type(*basic);
691 if(!elem || !is_scalar(*elem))
694 resolve(unary, basic, unary.expression->lvalue);
697 void ExpressionResolver::visit(BinaryExpression &binary, bool assign)
699 /* Binary operators are only defined for basic types (not for image or
701 BasicTypeDeclaration *basic_left = dynamic_cast<BasicTypeDeclaration *>(binary.left->type);
702 BasicTypeDeclaration *basic_right = dynamic_cast<BasicTypeDeclaration *>(binary.right->type);
703 if(!basic_left || !basic_right)
706 char oper = binary.oper->token[0];
709 /* Subscripting operates on vectors, matrices and arrays, and the right
710 operand must be an integer. */
711 if((!is_vector_or_matrix(*basic_left) && basic_left->kind!=BasicTypeDeclaration::ARRAY) || basic_right->kind!=BasicTypeDeclaration::INT)
714 resolve(binary, basic_left->base_type, binary.left->lvalue);
717 else if(basic_left->kind==BasicTypeDeclaration::ARRAY || basic_right->kind==BasicTypeDeclaration::ARRAY)
718 // No other binary operator can be used with arrays.
721 BasicTypeDeclaration *elem_left = get_element_type(*basic_left);
722 BasicTypeDeclaration *elem_right = get_element_type(*basic_right);
723 if(!elem_left || !elem_right)
726 Compatibility compat = get_compatibility(*basic_left, *basic_right);
727 Compatibility elem_compat = get_compatibility(*elem_left, *elem_right);
728 if(elem_compat==NOT_COMPATIBLE)
730 if(assign && (compat==LEFT_CONVERTIBLE || elem_compat==LEFT_CONVERTIBLE))
733 TypeDeclaration *type = 0;
734 char oper2 = binary.oper->token[1];
735 if((oper=='<' && oper2!='<') || (oper=='>' && oper2!='>'))
737 /* Relational operators compare two scalar integer or floating-point
739 if(!is_scalar(*elem_left) || !is_scalar(*elem_right) || compat==NOT_COMPATIBLE)
742 type = find_type(BasicTypeDeclaration::BOOL, 1);
744 else if((oper=='=' || oper=='!') && oper2=='=')
746 // Equality comparison can be done on any compatible types.
747 if(compat==NOT_COMPATIBLE)
750 type = find_type(BasicTypeDeclaration::BOOL, 1);
752 else if(oper2=='&' || oper2=='|' || oper2=='^')
754 // Logical operators can only be applied to booleans.
755 if(basic_left->kind!=BasicTypeDeclaration::BOOL || basic_right->kind!=BasicTypeDeclaration::BOOL)
760 else if((oper=='&' || oper=='|' || oper=='^' || oper=='%') && !oper2)
762 // Bitwise operators and modulo can only be applied to integers.
763 if(basic_left->kind!=BasicTypeDeclaration::INT || basic_right->kind!=BasicTypeDeclaration::INT)
766 type = (compat==LEFT_CONVERTIBLE ? basic_right : basic_left);
768 else if((oper=='<' || oper=='>') && oper2==oper)
770 // Shifts apply to integer scalars and vectors, with some restrictions.
771 if(elem_left->kind!=BasicTypeDeclaration::INT || elem_right->kind!=BasicTypeDeclaration::INT)
773 unsigned left_size = (basic_left->kind==BasicTypeDeclaration::INT ? 1 : basic_left->kind==BasicTypeDeclaration::VECTOR ? basic_left->size : 0);
774 unsigned right_size = (basic_right->kind==BasicTypeDeclaration::INT ? 1 : basic_right->kind==BasicTypeDeclaration::VECTOR ? basic_right->size : 0);
775 if(!left_size || (left_size==1 && right_size!=1) || (left_size>1 && right_size!=1 && right_size!=left_size))
779 // Don't perform conversion even if the operands are of different sizes.
782 else if(oper=='+' || oper=='-' || oper=='*' || oper=='/')
784 // Arithmetic operators require scalar elements.
785 if(!is_scalar(*elem_left) || !is_scalar(*elem_right))
788 if(oper=='*' && is_vector_or_matrix(*basic_left) && is_vector_or_matrix(*basic_right) &&
789 (basic_left->kind==BasicTypeDeclaration::MATRIX || basic_right->kind==BasicTypeDeclaration::MATRIX))
791 /* Multiplication has special rules when at least one operand is a
792 matrix and the other is a vector or a matrix. */
793 unsigned left_columns = basic_left->size&0xFFFF;
794 unsigned right_rows = basic_right->size;
795 if(basic_right->kind==BasicTypeDeclaration::MATRIX)
797 if(left_columns!=right_rows)
800 BasicTypeDeclaration *elem_result = (elem_compat==LEFT_CONVERTIBLE ? elem_right : elem_left);
802 if(basic_left->kind==BasicTypeDeclaration::VECTOR)
803 type = find_type(*elem_result, BasicTypeDeclaration::VECTOR, basic_right->size&0xFFFF);
804 else if(basic_right->kind==BasicTypeDeclaration::VECTOR)
805 type = find_type(*elem_result, BasicTypeDeclaration::VECTOR, basic_left->size>>16);
807 type = find_type(*elem_result, BasicTypeDeclaration::MATRIX, (basic_left->size&0xFFFF0000)|(basic_right->size&0xFFFF));
809 else if(compat==NOT_COMPATIBLE)
811 // Arithmetic between scalars and matrices or vectors is supported.
812 if(is_scalar(*basic_left) && is_vector_or_matrix(*basic_right))
813 type = (elem_compat==RIGHT_CONVERTIBLE ? find_type(*elem_left, basic_right->kind, basic_right->size) : basic_right);
814 else if(is_vector_or_matrix(*basic_left) && is_scalar(*basic_right))
815 type = (elem_compat==LEFT_CONVERTIBLE ? find_type(*elem_right, basic_left->kind, basic_left->size) : basic_left);
819 else if(compat==LEFT_CONVERTIBLE)
827 if(assign && type!=basic_left)
830 bool converted = true;
831 if(compat==LEFT_CONVERTIBLE)
832 convert_to(binary.left, *basic_right);
833 else if(compat==RIGHT_CONVERTIBLE)
834 convert_to(binary.right, *basic_left);
835 else if(elem_compat==LEFT_CONVERTIBLE)
836 converted = convert_to_element(binary.left, *elem_right);
837 else if(elem_compat==RIGHT_CONVERTIBLE)
838 converted = convert_to_element(binary.right, *elem_left);
843 resolve(binary, type, assign);
846 void ExpressionResolver::visit(BinaryExpression &binary)
848 TraversingVisitor::visit(binary);
849 visit(binary, false);
852 void ExpressionResolver::visit(Assignment &assign)
854 TraversingVisitor::visit(assign);
856 if(assign.oper->token[0]!='=')
857 return visit(assign, true);
858 else if(assign.left->type!=assign.right->type)
860 BasicTypeDeclaration *basic_left = dynamic_cast<BasicTypeDeclaration *>(assign.left->type);
861 BasicTypeDeclaration *basic_right = dynamic_cast<BasicTypeDeclaration *>(assign.right->type);
862 if(!basic_left || !basic_right)
865 Compatibility compat = get_compatibility(*basic_left, *basic_right);
866 if(compat==RIGHT_CONVERTIBLE)
867 convert_to(assign.right, *basic_left);
868 else if(compat!=SAME_TYPE)
872 resolve(assign, assign.left->type, true);
875 void ExpressionResolver::visit(FunctionCall &call)
877 TraversingVisitor::visit(call);
879 TypeDeclaration *type = 0;
881 type = call.declaration->return_type_declaration;
882 else if(call.constructor)
884 map<string, TypeDeclaration *>::const_iterator i=stage->types.find(call.name);
885 type = (i!=stage->types.end() ? i->second : 0);
887 resolve(call, type, false);
890 void ExpressionResolver::visit(BasicTypeDeclaration &type)
892 basic_types.push_back(&type);
895 void ExpressionResolver::visit(VariableDeclaration &var)
897 TraversingVisitor::visit(var);
898 if(!var.init_expression)
901 BasicTypeDeclaration *var_basic = dynamic_cast<BasicTypeDeclaration *>(var.type_declaration);
902 BasicTypeDeclaration *init_basic = dynamic_cast<BasicTypeDeclaration *>(var.init_expression->type);
903 if(!var_basic || !init_basic)
906 Compatibility compat = get_compatibility(*var_basic, *init_basic);
907 if(compat==RIGHT_CONVERTIBLE)
908 convert_to(var.init_expression, *var_basic);
912 bool FunctionResolver::apply(Stage &s)
916 r_any_resolved = false;
917 s.content.visit(*this);
918 return r_any_resolved;
921 void FunctionResolver::visit(FunctionCall &call)
924 bool has_signature = true;
925 for(NodeArray<Expression>::const_iterator i=call.arguments.begin(); (has_signature && i!=call.arguments.end()); ++i)
928 append(arg_types, ",", (*i)->type->name);
930 has_signature = false;
933 FunctionDeclaration *declaration = 0;
936 map<string, FunctionDeclaration *>::iterator i = stage->functions.find(format("%s(%s)", call.name, arg_types));
937 declaration = (i!=stage->functions.end() ? i->second : 0);
939 r_any_resolved |= (declaration!=call.declaration);
940 call.declaration = declaration;
942 TraversingVisitor::visit(call);
945 void FunctionResolver::visit(FunctionDeclaration &func)
947 if(func.signature.empty())
950 for(NodeArray<VariableDeclaration>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
952 if((*i)->type_declaration)
953 append(param_types, ",", (*i)->type_declaration->name);
957 func.signature = format("(%s)", param_types);
958 r_any_resolved = true;
961 string key = func.name+func.signature;
962 FunctionDeclaration *&stage_decl = stage->functions[key];
963 vector<FunctionDeclaration *> &decls = declarations[key];
964 if(func.definition==&func)
968 // Set all previous declarations to use this definition.
969 for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
971 r_any_resolved |= (func.definition!=(*i)->definition);
972 (*i)->definition = func.definition;
973 (*i)->body.body.clear();
978 FunctionDeclaration *definition = (stage_decl ? stage_decl->definition : 0);
979 r_any_resolved |= (definition!=func.definition);
980 func.definition = definition;
985 decls.push_back(&func);
987 TraversingVisitor::visit(func);
991 InterfaceGenerator::InterfaceGenerator():
993 function_scope(false),
995 iface_target_block(0)
998 string InterfaceGenerator::get_out_prefix(Stage::Type type)
1000 if(type==Stage::VERTEX)
1002 else if(type==Stage::GEOMETRY)
1008 void InterfaceGenerator::apply(Stage &s)
1011 iface_target_block = &stage->content;
1013 in_prefix = get_out_prefix(stage->previous->type);
1014 out_prefix = get_out_prefix(stage->type);
1015 s.content.visit(*this);
1016 NodeRemover().apply(s, nodes_to_remove);
1019 void InterfaceGenerator::visit(Block &block)
1021 SetForScope<Block *> set_block(current_block, &block);
1022 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
1024 assignment_insert_point = i;
1025 if(&block==&stage->content)
1026 iface_insert_point = i;
1032 string InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
1034 unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
1035 return prefix+name.substr(offset);
1038 VariableDeclaration *InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
1040 if(stage->content.variables.count(name))
1043 if(stage->type==Stage::GEOMETRY && !copy_block && var.interface=="out" && var.array)
1046 VariableDeclaration* iface_var = new VariableDeclaration;
1047 iface_var->sampling = var.sampling;
1048 iface_var->interface = iface;
1049 iface_var->type = var.type;
1050 iface_var->name = name;
1051 /* Geometry shader inputs are always arrays. But if we're bringing in an
1052 entire block, the array is on the block and not individual variables. */
1053 if(stage->type==Stage::GEOMETRY && !copy_block)
1054 iface_var->array = ((var.array && var.interface!="in") || iface=="in");
1056 iface_var->array = var.array;
1057 if(iface_var->array)
1058 iface_var->array_size = var.array_size;
1061 iface_var->layout = var.layout;
1062 iface_var->linked_declaration = &var;
1063 var.linked_declaration = iface_var;
1066 iface_target_block->body.insert(iface_insert_point, iface_var);
1067 iface_target_block->variables.insert(make_pair(name, iface_var));
1072 InterfaceBlock *InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
1074 if(stage->interface_blocks.count("in"+out_block.name))
1077 InterfaceBlock *in_block = new InterfaceBlock;
1078 in_block->interface = "in";
1079 in_block->name = out_block.name;
1080 in_block->members = new Block;
1081 in_block->instance_name = out_block.instance_name;
1082 if(stage->type==Stage::GEOMETRY)
1083 in_block->array = true;
1085 in_block->array = out_block.array;
1086 in_block->linked_block = &out_block;
1087 out_block.linked_block = in_block;
1090 SetFlag set_copy(copy_block, true);
1091 SetForScope<Block *> set_target(iface_target_block, in_block->members.get());
1092 SetForScope<NodeList<Statement>::iterator> set_ins_pt(iface_insert_point, in_block->members->body.end());
1093 if(out_block.struct_declaration)
1094 out_block.struct_declaration->members.visit(*this);
1095 else if(out_block.members)
1096 out_block.members->visit(*this);
1099 iface_target_block->body.insert(iface_insert_point, in_block);
1100 stage->interface_blocks.insert(make_pair("in"+in_block->name, in_block));
1101 if(!in_block->instance_name.empty())
1102 stage->interface_blocks.insert(make_pair("_"+in_block->instance_name, in_block));
1104 SetFlag set_scope(function_scope, false);
1105 SetForScope<Block *> set_block(current_block, &stage->content);
1106 in_block->visit(*this);
1111 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
1113 Assignment *assign = new Assignment;
1114 VariableReference *ref = new VariableReference;
1117 assign->oper = &Operator::get_operator("=", Operator::BINARY);
1118 assign->right = right;
1120 ExpressionStatement *stmt = new ExpressionStatement;
1121 stmt->expression = assign;
1122 current_block->body.insert(assignment_insert_point, stmt);
1128 void InterfaceGenerator::visit(VariableReference &var)
1130 if(var.declaration || !stage->previous)
1132 /* Don't pull a variable from previous stage if we just generated an output
1133 interface in this stage */
1134 if(stage->content.variables.count(var.name))
1137 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
1138 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
1139 if(i==prev_vars.end() || i->second->interface!="out")
1140 i = prev_vars.find(in_prefix+var.name);
1141 if(i!=prev_vars.end() && i->second->interface=="out")
1143 if(stage->type==Stage::GEOMETRY && i->second->array)
1144 stage->diagnostics.push_back(Diagnostic(Diagnostic::WARN, var.source, var.line,
1145 format("Can't access '%s' through automatic interface because it's an array", var.name)));
1148 generate_interface(*i->second, "in", i->second->name);
1149 var.name = i->second->name;
1154 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
1155 map<string, InterfaceBlock *>::const_iterator j = prev_blocks.find("_"+var.name);
1156 if(j!=prev_blocks.end() && j->second->interface=="out")
1158 generate_interface(*j->second);
1159 /* Let VariableResolver convert the variable reference into an interface
1164 for(j=prev_blocks.begin(); j!=prev_blocks.end(); ++j)
1165 if(j->second->instance_name.empty() && j->second->struct_declaration)
1167 const map<string, VariableDeclaration *> &iface_vars = j->second->struct_declaration->members.variables;
1168 i = iface_vars.find(var.name);
1169 if(i!=iface_vars.end())
1171 generate_interface(*j->second);
1177 void InterfaceGenerator::visit(VariableDeclaration &var)
1180 generate_interface(var, "in", var.name);
1181 else if(var.interface=="out")
1183 /* For output variables in function scope, generate a global interface
1184 and replace the local declaration with an assignment. */
1185 VariableDeclaration *out_var = 0;
1186 if(function_scope && (out_var=generate_interface(var, "out", var.name)))
1188 out_var->source = var.source;
1189 out_var->line = var.line;
1190 nodes_to_remove.insert(&var);
1191 if(var.init_expression)
1193 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
1194 stmt.source = var.source;
1195 stmt.line = var.line;
1200 else if(var.interface=="in")
1202 /* Try to link input variables in global scope with output variables from
1204 if(current_block==&stage->content && !var.linked_declaration && stage->previous)
1206 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
1207 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
1208 if(i!=prev_vars.end() && i->second->interface=="out")
1210 var.linked_declaration = i->second;
1211 i->second->linked_declaration = &var;
1216 TraversingVisitor::visit(var);
1219 void InterfaceGenerator::visit(InterfaceBlock &iface)
1221 if(iface.interface=="in")
1223 /* Try to link input blocks with output blocks sharing the same block
1224 name from previous stage. */
1225 if(!iface.linked_block && stage->previous)
1227 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
1228 map<string, InterfaceBlock *>::const_iterator i = prev_blocks.find("out"+iface.name);
1229 if(i!=prev_blocks.end())
1231 iface.linked_block = i->second;
1232 i->second->linked_block = &iface;
1237 TraversingVisitor::visit(iface);
1240 void InterfaceGenerator::visit(FunctionDeclaration &func)
1242 SetFlag set_scope(function_scope, true);
1243 // Skip parameters because they're not useful here
1244 func.body.visit(*this);
1247 void InterfaceGenerator::visit(Passthrough &pass)
1249 vector<VariableDeclaration *> pass_vars;
1251 // Pass through all input variables of this stage.
1252 for(map<string, VariableDeclaration *>::const_iterator i=stage->content.variables.begin(); i!=stage->content.variables.end(); ++i)
1253 if(i->second->interface=="in")
1254 pass_vars.push_back(i->second);
1258 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
1259 for(map<string, VariableDeclaration *>::const_iterator i=prev_vars.begin(); i!=prev_vars.end(); ++i)
1261 if(i->second->interface!="out")
1264 /* Pass through output variables from the previous stage, but only
1265 those which are not already linked to an input here. */
1266 if(!i->second->linked_declaration && generate_interface(*i->second, "in", i->second->name))
1267 pass_vars.push_back(i->second);
1271 if(stage->type==Stage::GEOMETRY)
1273 /* Special case for geometry shader: copy gl_Position from input to
1275 InterfaceBlockReference *ref = new InterfaceBlockReference;
1276 ref->name = "gl_in";
1278 BinaryExpression *subscript = new BinaryExpression;
1279 subscript->left = ref;
1280 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
1281 subscript->right = pass.subscript;
1283 MemberAccess *memacc = new MemberAccess;
1284 memacc->left = subscript;
1285 memacc->member = "gl_Position";
1287 insert_assignment("gl_Position", memacc);
1290 for(vector<VariableDeclaration *>::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i)
1292 string out_name = change_prefix((*i)->name, out_prefix);
1293 generate_interface(**i, "out", out_name);
1295 VariableReference *ref = new VariableReference;
1296 ref->name = (*i)->name;
1299 BinaryExpression *subscript = new BinaryExpression;
1300 subscript->left = ref;
1301 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
1302 subscript->right = pass.subscript;
1303 insert_assignment(out_name, subscript);
1306 insert_assignment(out_name, ref);
1309 nodes_to_remove.insert(&pass);