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 type.size |= basic_base->size<<16;
197 if(type.kind==BasicTypeDeclaration::ALIAS && type.base_type)
198 alias_map[&type] = type.base_type;
199 else if(type.kind==BasicTypeDeclaration::ARRAY && type.base_type)
200 array_types[type.base_type] = &type;
202 stage->types.insert(make_pair(type.name, &type));
205 void TypeResolver::visit(ImageTypeDeclaration &type)
207 resolve_type(type.base_type, type.base, false);
208 stage->types.insert(make_pair(type.name, &type));
211 void TypeResolver::visit(StructDeclaration &strct)
213 stage->types.insert(make_pair(strct.name, &strct));
214 TraversingVisitor::visit(strct);
217 void TypeResolver::visit(VariableDeclaration &var)
219 resolve_type(var.type_declaration, var.type, var.array);
220 if(iface_block && var.interface==iface_block->interface)
221 var.interface.clear();
224 void TypeResolver::visit(InterfaceBlock &iface)
228 SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
229 iface.members->visit(*this);
231 StructDeclaration *strct = new StructDeclaration;
232 strct->source = INTERNAL_SOURCE;
233 strct->name = format("_%s_%s", iface.interface, iface.name);
234 strct->members.body.splice(strct->members.body.begin(), iface.members->body);
235 stage->content.body.insert(type_insert_point, strct);
236 stage->types.insert(make_pair(strct->name, strct));
239 strct->interface_block = &iface;
240 iface.struct_declaration = strct;
243 TypeDeclaration *type = iface.struct_declaration;
244 if(type && iface.array)
245 type = get_or_create_array_type(*type);
246 r_any_resolved = (type!=iface.type_declaration);
247 iface.type_declaration = type;
250 void TypeResolver::visit(FunctionDeclaration &func)
252 resolve_type(func.return_type_declaration, func.return_type, false);
253 TraversingVisitor::visit(func);
257 VariableResolver::VariableResolver():
259 r_any_resolved(false),
260 record_target(false),
261 r_self_referencing(false)
264 bool VariableResolver::apply(Stage &s)
267 s.interface_blocks.clear();
268 r_any_resolved = false;
269 s.content.visit(*this);
270 return r_any_resolved;
273 void VariableResolver::enter(Block &block)
275 block.variables.clear();
278 void VariableResolver::visit(RefPtr<Expression> &expr)
280 r_replacement_expr = 0;
282 if(r_replacement_expr)
284 expr = r_replacement_expr;
285 /* Don't record assignment target when doing a replacement, because chain
286 information won't be correct. */
287 r_assignment_target.declaration = 0;
288 r_any_resolved = true;
290 r_replacement_expr = 0;
293 void VariableResolver::check_assignment_target(Statement *declaration)
297 if(r_assignment_target.declaration)
299 /* More than one reference found in assignment target. Unable to
300 determine what the primary target is. */
301 record_target = false;
302 r_assignment_target.declaration = 0;
305 r_assignment_target.declaration = declaration;
307 // TODO This check is overly broad and may prevent some optimizations.
308 else if(declaration && declaration==r_assignment_target.declaration)
309 r_self_referencing = true;
312 void VariableResolver::visit(VariableReference &var)
314 VariableDeclaration *declaration = 0;
316 /* Look for variable declarations in the block hierarchy first. Interface
317 blocks are always defined in the top level so we can't accidentally skip
319 for(Block *block=current_block; (!declaration && block); block=block->parent)
321 map<string, VariableDeclaration *>::iterator i = block->variables.find(var.name);
322 if(i!=block->variables.end())
323 declaration = i->second;
328 const map<string, InterfaceBlock *> &blocks = stage->interface_blocks;
329 map<string, InterfaceBlock *>::const_iterator i = blocks.find("_"+var.name);
332 /* The name refers to an interface block with an instance name rather
333 than a variable. Prepare a new syntax tree node accordingly. */
334 InterfaceBlockReference *iface_ref = new InterfaceBlockReference;
335 iface_ref->source = var.source;
336 iface_ref->line = var.line;
337 iface_ref->name = var.name;
338 iface_ref->declaration = i->second;
339 r_replacement_expr = iface_ref;
343 // Look for the variable in anonymous interface blocks.
344 for(i=blocks.begin(); (!declaration && i!=blocks.end()); ++i)
345 if(i->second->instance_name.empty() && i->second->struct_declaration)
347 const map<string, VariableDeclaration *> &iface_vars = i->second->struct_declaration->members.variables;
348 map<string, VariableDeclaration *>::const_iterator j = iface_vars.find(var.name);
349 if(j!=iface_vars.end())
350 declaration = j->second;
355 r_any_resolved |= (declaration!=var.declaration);
356 var.declaration = declaration;
358 check_assignment_target(var.declaration);
361 void VariableResolver::visit(InterfaceBlockReference &iface)
363 map<string, InterfaceBlock *>::iterator i = stage->interface_blocks.find("_"+iface.name);
364 InterfaceBlock *declaration = (i!=stage->interface_blocks.end() ? i->second : 0);
365 r_any_resolved |= (declaration!=iface.declaration);
366 iface.declaration = declaration;
368 check_assignment_target(iface.declaration);
371 void VariableResolver::add_to_chain(Assignment::Target::ChainType type, unsigned index)
373 if(r_assignment_target.chain_len<7)
374 r_assignment_target.chain[r_assignment_target.chain_len] = type | min<unsigned>(index, 0x3F);
375 ++r_assignment_target.chain_len;
378 void VariableResolver::visit(MemberAccess &memacc)
380 TraversingVisitor::visit(memacc);
382 VariableDeclaration *declaration = 0;
383 if(StructDeclaration *strct = dynamic_cast<StructDeclaration *>(memacc.left->type))
385 map<string, VariableDeclaration *>::iterator i = strct->members.variables.find(memacc.member);
386 if(i!=strct->members.variables.end())
388 declaration = i->second;
393 for(NodeList<Statement>::const_iterator j=strct->members.body.begin(); (j!=strct->members.body.end() && j->get()!=i->second); ++j)
396 add_to_chain(Assignment::Target::MEMBER, index);
400 else if(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(memacc.left->type))
402 bool scalar_swizzle = ((basic->kind==BasicTypeDeclaration::INT || basic->kind==BasicTypeDeclaration::FLOAT) && memacc.member.size()==1);
403 bool vector_swizzle = (basic->kind==BasicTypeDeclaration::VECTOR && memacc.member.size()<=4);
404 if(scalar_swizzle || vector_swizzle)
406 static const char component_names[] = { 'x', 'r', 's', 'y', 'g', 't', 'z', 'b', 'p', 'w', 'a', 'q' };
409 UInt8 components[4] = { };
410 for(unsigned i=0; (ok && i<memacc.member.size()); ++i)
411 ok = ((components[i] = (find(component_names, component_names+12, memacc.member[i])-component_names)/3) < 4);
415 Swizzle *swizzle = new Swizzle;
416 swizzle->source = memacc.source;
417 swizzle->line = memacc.line;
418 swizzle->oper = memacc.oper;
419 swizzle->left = memacc.left;
420 swizzle->component_group = memacc.member;
421 swizzle->count = memacc.member.size();
422 copy(components, components+memacc.member.size(), swizzle->components);
423 r_replacement_expr = swizzle;
428 r_any_resolved |= (declaration!=memacc.declaration);
429 memacc.declaration = declaration;
432 void VariableResolver::visit(Swizzle &swizzle)
434 TraversingVisitor::visit(swizzle);
439 for(unsigned i=0; i<swizzle.count; ++i)
440 mask |= 1<<swizzle.components[i];
441 add_to_chain(Assignment::Target::SWIZZLE, mask);
445 void VariableResolver::visit(BinaryExpression &binary)
447 if(binary.oper->token[0]=='[')
450 /* The subscript expression is not a part of the primary assignment
452 SetFlag set(record_target, false);
459 unsigned index = 0x3F;
460 if(Literal *literal_subscript = dynamic_cast<Literal *>(binary.right.get()))
461 if(literal_subscript->value.check_type<int>())
462 index = literal_subscript->value.value<int>();
463 add_to_chain(Assignment::Target::ARRAY, index);
467 TraversingVisitor::visit(binary);
470 void VariableResolver::visit(Assignment &assign)
473 SetFlag set(record_target);
474 r_assignment_target = Assignment::Target();
476 r_any_resolved |= (r_assignment_target<assign.target || assign.target<r_assignment_target);
477 assign.target = r_assignment_target;
480 r_self_referencing = false;
482 assign.self_referencing = (r_self_referencing || assign.oper->token[0]!='=');
485 void VariableResolver::visit(VariableDeclaration &var)
487 TraversingVisitor::visit(var);
488 current_block->variables.insert(make_pair(var.name, &var));
491 void VariableResolver::visit(InterfaceBlock &iface)
493 /* Block names can be reused in different interfaces. Prefix the name with
494 the first character of the interface to avoid conflicts. */
495 stage->interface_blocks.insert(make_pair(iface.interface+iface.name, &iface));
496 if(!iface.instance_name.empty())
497 stage->interface_blocks.insert(make_pair("_"+iface.instance_name, &iface));
499 TraversingVisitor::visit(iface);
503 ExpressionResolver::ExpressionResolver():
505 r_any_resolved(false)
508 bool ExpressionResolver::apply(Stage &s)
511 r_any_resolved = false;
512 s.content.visit(*this);
513 return r_any_resolved;
516 bool ExpressionResolver::is_scalar(BasicTypeDeclaration &type)
518 return (type.kind==BasicTypeDeclaration::INT || type.kind==BasicTypeDeclaration::FLOAT);
521 bool ExpressionResolver::is_vector_or_matrix(BasicTypeDeclaration &type)
523 return (type.kind==BasicTypeDeclaration::VECTOR || type.kind==BasicTypeDeclaration::MATRIX);
526 BasicTypeDeclaration *ExpressionResolver::get_element_type(BasicTypeDeclaration &type)
528 if(is_vector_or_matrix(type) || type.kind==BasicTypeDeclaration::ARRAY)
530 BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type);
531 return (basic_base ? get_element_type(*basic_base) : 0);
537 bool ExpressionResolver::can_convert(BasicTypeDeclaration &from, BasicTypeDeclaration &to)
539 if(from.kind==BasicTypeDeclaration::INT && to.kind==BasicTypeDeclaration::FLOAT)
540 return from.size<=to.size;
541 else if(from.kind!=to.kind)
543 else if((from.kind==BasicTypeDeclaration::VECTOR || from.kind==BasicTypeDeclaration::MATRIX) && from.size==to.size)
545 BasicTypeDeclaration *from_base = dynamic_cast<BasicTypeDeclaration *>(from.base_type);
546 BasicTypeDeclaration *to_base = dynamic_cast<BasicTypeDeclaration *>(to.base_type);
547 return (from_base && to_base && can_convert(*from_base, *to_base));
553 ExpressionResolver::Compatibility ExpressionResolver::get_compatibility(BasicTypeDeclaration &left, BasicTypeDeclaration &right)
557 else if(can_convert(left, right))
558 return LEFT_CONVERTIBLE;
559 else if(can_convert(right, left))
560 return RIGHT_CONVERTIBLE;
562 return NOT_COMPATIBLE;
565 BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration::Kind kind, unsigned size)
567 for(vector<BasicTypeDeclaration *>::const_iterator i=basic_types.begin(); i!=basic_types.end(); ++i)
568 if((*i)->kind==kind && (*i)->size==size)
573 BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration &elem_type, BasicTypeDeclaration::Kind kind, unsigned size)
575 for(vector<BasicTypeDeclaration *>::const_iterator i=basic_types.begin(); i!=basic_types.end(); ++i)
576 if(get_element_type(**i)==&elem_type && (*i)->kind==kind && (*i)->size==size)
581 void ExpressionResolver::convert_to(RefPtr<Expression> &expr, BasicTypeDeclaration &type)
583 RefPtr<FunctionCall> call = new FunctionCall;
584 call->name = type.name;
585 call->constructor = true;
586 call->arguments.push_back(0);
587 call->arguments.back() = expr;
592 bool ExpressionResolver::convert_to_element(RefPtr<Expression> &expr, BasicTypeDeclaration &elem_type)
594 if(BasicTypeDeclaration *expr_basic = dynamic_cast<BasicTypeDeclaration *>(expr->type))
596 BasicTypeDeclaration *to_type = &elem_type;
597 if(is_vector_or_matrix(*expr_basic))
598 to_type = find_type(elem_type, expr_basic->kind, expr_basic->size);
601 convert_to(expr, *to_type);
609 void ExpressionResolver::resolve(Expression &expr, TypeDeclaration *type, bool lvalue)
611 r_any_resolved |= (type!=expr.type || lvalue!=expr.lvalue);
613 expr.lvalue = lvalue;
616 void ExpressionResolver::visit(Literal &literal)
618 if(literal.value.check_type<bool>())
619 resolve(literal, find_type(BasicTypeDeclaration::BOOL, 1), false);
620 else if(literal.value.check_type<int>())
621 resolve(literal, find_type(BasicTypeDeclaration::INT, 32), false);
622 else if(literal.value.check_type<float>())
623 resolve(literal, find_type(BasicTypeDeclaration::FLOAT, 32), false);
626 void ExpressionResolver::visit(ParenthesizedExpression &parexpr)
628 TraversingVisitor::visit(parexpr);
629 resolve(parexpr, parexpr.expression->type, parexpr.expression->lvalue);
632 void ExpressionResolver::visit(VariableReference &var)
635 resolve(var, var.declaration->type_declaration, true);
638 void ExpressionResolver::visit(InterfaceBlockReference &iface)
640 if(iface.declaration)
641 resolve(iface, iface.declaration->type_declaration, true);
644 void ExpressionResolver::visit(MemberAccess &memacc)
646 TraversingVisitor::visit(memacc);
648 if(memacc.declaration)
649 resolve(memacc, memacc.declaration->type_declaration, memacc.left->lvalue);
652 void ExpressionResolver::visit(Swizzle &swizzle)
654 TraversingVisitor::visit(swizzle);
656 if(BasicTypeDeclaration *left_basic = dynamic_cast<BasicTypeDeclaration *>(swizzle.left->type))
658 BasicTypeDeclaration *left_elem = get_element_type(*left_basic);
660 resolve(swizzle, left_elem, swizzle.left->lvalue);
661 else if(left_basic->kind==BasicTypeDeclaration::VECTOR && left_elem)
662 resolve(swizzle, find_type(*left_elem, left_basic->kind, swizzle.count), swizzle.left->lvalue);
666 void ExpressionResolver::visit(UnaryExpression &unary)
668 TraversingVisitor::visit(unary);
670 BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(unary.expression->type);
674 char oper = unary.oper->token[0];
677 if(basic->kind!=BasicTypeDeclaration::BOOL)
682 if(basic->kind!=BasicTypeDeclaration::INT)
685 else if(oper=='+' || oper=='-')
687 BasicTypeDeclaration *elem = get_element_type(*basic);
688 if(!elem || !is_scalar(*elem))
691 resolve(unary, basic, unary.expression->lvalue);
694 void ExpressionResolver::visit(BinaryExpression &binary, bool assign)
696 /* Binary operators are only defined for basic types (not for image or
698 BasicTypeDeclaration *basic_left = dynamic_cast<BasicTypeDeclaration *>(binary.left->type);
699 BasicTypeDeclaration *basic_right = dynamic_cast<BasicTypeDeclaration *>(binary.right->type);
700 if(!basic_left || !basic_right)
703 char oper = binary.oper->token[0];
706 /* Subscripting operates on vectors, matrices and arrays, and the right
707 operand must be an integer. */
708 if((!is_vector_or_matrix(*basic_left) && basic_left->kind!=BasicTypeDeclaration::ARRAY) || basic_right->kind!=BasicTypeDeclaration::INT)
711 resolve(binary, basic_left->base_type, binary.left->lvalue);
714 else if(basic_left->kind==BasicTypeDeclaration::ARRAY || basic_right->kind==BasicTypeDeclaration::ARRAY)
715 // No other binary operator can be used with arrays.
718 BasicTypeDeclaration *elem_left = get_element_type(*basic_left);
719 BasicTypeDeclaration *elem_right = get_element_type(*basic_right);
720 if(!elem_left || !elem_right)
723 Compatibility compat = get_compatibility(*basic_left, *basic_right);
724 Compatibility elem_compat = get_compatibility(*elem_left, *elem_right);
725 if(elem_compat==NOT_COMPATIBLE)
727 if(assign && (compat==LEFT_CONVERTIBLE || elem_compat==LEFT_CONVERTIBLE))
730 TypeDeclaration *type = 0;
731 char oper2 = binary.oper->token[1];
732 if((oper=='<' && oper2!='<') || (oper=='>' && oper2!='>'))
734 /* Relational operators compare two scalar integer or floating-point
736 if(!is_scalar(*elem_left) || !is_scalar(*elem_right) || compat==NOT_COMPATIBLE)
739 type = find_type(BasicTypeDeclaration::BOOL, 1);
741 else if((oper=='=' || oper=='!') && oper2=='=')
743 // Equality comparison can be done on any compatible types.
744 if(compat==NOT_COMPATIBLE)
747 type = find_type(BasicTypeDeclaration::BOOL, 1);
749 else if(oper2=='&' || oper2=='|' || oper2=='^')
751 // Logical operators can only be applied to booleans.
752 if(basic_left->kind!=BasicTypeDeclaration::BOOL || basic_right->kind!=BasicTypeDeclaration::BOOL)
757 else if((oper=='&' || oper=='|' || oper=='^' || oper=='%') && !oper2)
759 // Bitwise operators and modulo can only be applied to integers.
760 if(basic_left->kind!=BasicTypeDeclaration::INT || basic_right->kind!=BasicTypeDeclaration::INT)
763 type = (compat==LEFT_CONVERTIBLE ? basic_right : basic_left);
765 else if((oper=='<' || oper=='>') && oper2==oper)
767 // Shifts apply to integer scalars and vectors, with some restrictions.
768 if(elem_left->kind!=BasicTypeDeclaration::INT || elem_right->kind!=BasicTypeDeclaration::INT)
770 unsigned left_size = (basic_left->kind==BasicTypeDeclaration::INT ? 1 : basic_left->kind==BasicTypeDeclaration::VECTOR ? basic_left->size : 0);
771 unsigned right_size = (basic_right->kind==BasicTypeDeclaration::INT ? 1 : basic_right->kind==BasicTypeDeclaration::VECTOR ? basic_right->size : 0);
772 if(!left_size || (left_size==1 && right_size!=1) || (left_size>1 && right_size!=1 && right_size!=left_size))
776 // Don't perform conversion even if the operands are of different sizes.
779 else if(oper=='+' || oper=='-' || oper=='*' || oper=='/')
781 // Arithmetic operators require scalar elements.
782 if(!is_scalar(*elem_left) || !is_scalar(*elem_right))
785 if(oper=='*' && is_vector_or_matrix(*basic_left) && is_vector_or_matrix(*basic_right) &&
786 (basic_left->kind==BasicTypeDeclaration::MATRIX || basic_right->kind==BasicTypeDeclaration::MATRIX))
788 /* Multiplication has special rules when at least one operand is a
789 matrix and the other is a vector or a matrix. */
790 unsigned left_columns = basic_left->size&0xFFFF;
791 unsigned right_rows = basic_right->size;
792 if(basic_right->kind==BasicTypeDeclaration::MATRIX)
794 if(left_columns!=right_rows)
797 BasicTypeDeclaration *elem_result = (elem_compat==LEFT_CONVERTIBLE ? elem_right : elem_left);
799 if(basic_left->kind==BasicTypeDeclaration::VECTOR)
800 type = find_type(*elem_result, BasicTypeDeclaration::VECTOR, basic_right->size&0xFFFF);
801 else if(basic_right->kind==BasicTypeDeclaration::VECTOR)
802 type = find_type(*elem_result, BasicTypeDeclaration::VECTOR, basic_left->size>>16);
804 type = find_type(*elem_result, BasicTypeDeclaration::MATRIX, (basic_left->size&0xFFFF0000)|(basic_right->size&0xFFFF));
806 else if(compat==NOT_COMPATIBLE)
808 // Arithmetic between scalars and matrices or vectors is supported.
809 if(is_scalar(*basic_left) && is_vector_or_matrix(*basic_right))
810 type = (elem_compat==RIGHT_CONVERTIBLE ? find_type(*elem_left, basic_right->kind, basic_right->size) : basic_right);
811 else if(is_vector_or_matrix(*basic_left) && is_scalar(*basic_right))
812 type = (elem_compat==LEFT_CONVERTIBLE ? find_type(*elem_right, basic_left->kind, basic_left->size) : basic_left);
816 else if(compat==LEFT_CONVERTIBLE)
824 if(assign && type!=basic_left)
827 bool converted = true;
828 if(compat==LEFT_CONVERTIBLE)
829 convert_to(binary.left, *basic_right);
830 else if(compat==RIGHT_CONVERTIBLE)
831 convert_to(binary.right, *basic_left);
832 else if(elem_compat==LEFT_CONVERTIBLE)
833 converted = convert_to_element(binary.left, *elem_right);
834 else if(elem_compat==RIGHT_CONVERTIBLE)
835 converted = convert_to_element(binary.right, *elem_left);
840 resolve(binary, type, assign);
843 void ExpressionResolver::visit(BinaryExpression &binary)
845 TraversingVisitor::visit(binary);
846 visit(binary, false);
849 void ExpressionResolver::visit(Assignment &assign)
851 TraversingVisitor::visit(assign);
853 if(assign.oper->token[0]!='=')
854 return visit(assign, true);
855 else if(assign.left->type!=assign.right->type)
857 BasicTypeDeclaration *basic_left = dynamic_cast<BasicTypeDeclaration *>(assign.left->type);
858 BasicTypeDeclaration *basic_right = dynamic_cast<BasicTypeDeclaration *>(assign.right->type);
859 if(!basic_left || !basic_right)
862 Compatibility compat = get_compatibility(*basic_left, *basic_right);
863 if(compat==RIGHT_CONVERTIBLE)
864 convert_to(assign.right, *basic_left);
865 else if(compat!=SAME_TYPE)
869 resolve(assign, assign.left->type, true);
872 void ExpressionResolver::visit(FunctionCall &call)
874 TraversingVisitor::visit(call);
876 TypeDeclaration *type = 0;
878 type = call.declaration->return_type_declaration;
879 else if(call.constructor)
881 map<string, TypeDeclaration *>::const_iterator i=stage->types.find(call.name);
882 type = (i!=stage->types.end() ? i->second : 0);
884 resolve(call, type, false);
887 void ExpressionResolver::visit(BasicTypeDeclaration &type)
889 basic_types.push_back(&type);
892 void ExpressionResolver::visit(VariableDeclaration &var)
894 TraversingVisitor::visit(var);
895 if(!var.init_expression)
898 BasicTypeDeclaration *var_basic = dynamic_cast<BasicTypeDeclaration *>(var.type_declaration);
899 BasicTypeDeclaration *init_basic = dynamic_cast<BasicTypeDeclaration *>(var.init_expression->type);
900 if(!var_basic || !init_basic)
903 Compatibility compat = get_compatibility(*var_basic, *init_basic);
904 if(compat==RIGHT_CONVERTIBLE)
905 convert_to(var.init_expression, *var_basic);
909 bool FunctionResolver::apply(Stage &s)
913 r_any_resolved = false;
914 s.content.visit(*this);
915 return r_any_resolved;
918 void FunctionResolver::visit(FunctionCall &call)
921 bool has_signature = true;
922 for(NodeArray<Expression>::const_iterator i=call.arguments.begin(); (has_signature && i!=call.arguments.end()); ++i)
925 append(arg_types, ",", (*i)->type->name);
927 has_signature = false;
930 FunctionDeclaration *declaration = 0;
933 map<string, FunctionDeclaration *>::iterator i = stage->functions.find(format("%s(%s)", call.name, arg_types));
934 declaration = (i!=stage->functions.end() ? i->second : 0);
936 r_any_resolved |= (declaration!=call.declaration);
937 call.declaration = declaration;
939 TraversingVisitor::visit(call);
942 void FunctionResolver::visit(FunctionDeclaration &func)
944 if(func.signature.empty())
947 for(NodeArray<VariableDeclaration>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
949 if((*i)->type_declaration)
950 append(param_types, ",", (*i)->type_declaration->name);
954 func.signature = format("(%s)", param_types);
955 r_any_resolved = true;
958 string key = func.name+func.signature;
959 FunctionDeclaration *&stage_decl = stage->functions[key];
960 vector<FunctionDeclaration *> &decls = declarations[key];
961 if(func.definition==&func)
965 // Set all previous declarations to use this definition.
966 for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
968 r_any_resolved |= (func.definition!=(*i)->definition);
969 (*i)->definition = func.definition;
970 (*i)->body.body.clear();
975 FunctionDeclaration *definition = (stage_decl ? stage_decl->definition : 0);
976 r_any_resolved |= (definition!=func.definition);
977 func.definition = definition;
982 decls.push_back(&func);
984 TraversingVisitor::visit(func);
988 InterfaceGenerator::InterfaceGenerator():
990 function_scope(false),
992 iface_target_block(0)
995 string InterfaceGenerator::get_out_prefix(Stage::Type type)
997 if(type==Stage::VERTEX)
999 else if(type==Stage::GEOMETRY)
1005 void InterfaceGenerator::apply(Stage &s)
1008 iface_target_block = &stage->content;
1010 in_prefix = get_out_prefix(stage->previous->type);
1011 out_prefix = get_out_prefix(stage->type);
1012 s.content.visit(*this);
1013 NodeRemover().apply(s, nodes_to_remove);
1016 void InterfaceGenerator::visit(Block &block)
1018 SetForScope<Block *> set_block(current_block, &block);
1019 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
1021 assignment_insert_point = i;
1022 if(&block==&stage->content)
1023 iface_insert_point = i;
1029 string InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
1031 unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
1032 return prefix+name.substr(offset);
1035 VariableDeclaration *InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
1037 if(stage->content.variables.count(name))
1040 VariableDeclaration* iface_var = new VariableDeclaration;
1041 iface_var->sampling = var.sampling;
1042 iface_var->interface = iface;
1043 iface_var->type = var.type;
1044 iface_var->name = name;
1045 /* Geometry shader inputs are always arrays. But if we're bringing in an
1046 entire block, the array is on the block and not individual variables. */
1047 if(stage->type==Stage::GEOMETRY && !copy_block)
1048 iface_var->array = ((var.array && var.interface!="in") || iface=="in");
1050 iface_var->array = var.array;
1051 if(iface_var->array)
1052 iface_var->array_size = var.array_size;
1055 iface_var->layout = var.layout;
1056 iface_var->linked_declaration = &var;
1057 var.linked_declaration = iface_var;
1060 iface_target_block->body.insert(iface_insert_point, iface_var);
1061 iface_target_block->variables.insert(make_pair(name, iface_var));
1066 InterfaceBlock *InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
1068 if(stage->interface_blocks.count("in"+out_block.name))
1071 InterfaceBlock *in_block = new InterfaceBlock;
1072 in_block->interface = "in";
1073 in_block->name = out_block.name;
1074 in_block->members = new Block;
1075 in_block->instance_name = out_block.instance_name;
1076 if(stage->type==Stage::GEOMETRY)
1077 in_block->array = true;
1079 in_block->array = out_block.array;
1080 in_block->linked_block = &out_block;
1081 out_block.linked_block = in_block;
1084 SetFlag set_copy(copy_block, true);
1085 SetForScope<Block *> set_target(iface_target_block, in_block->members.get());
1086 SetForScope<NodeList<Statement>::iterator> set_ins_pt(iface_insert_point, in_block->members->body.end());
1087 if(out_block.struct_declaration)
1088 out_block.struct_declaration->members.visit(*this);
1089 else if(out_block.members)
1090 out_block.members->visit(*this);
1093 iface_target_block->body.insert(iface_insert_point, in_block);
1094 stage->interface_blocks.insert(make_pair("in"+in_block->name, in_block));
1095 if(!in_block->instance_name.empty())
1096 stage->interface_blocks.insert(make_pair("_"+in_block->instance_name, in_block));
1098 SetFlag set_scope(function_scope, false);
1099 SetForScope<Block *> set_block(current_block, &stage->content);
1100 in_block->visit(*this);
1105 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
1107 Assignment *assign = new Assignment;
1108 VariableReference *ref = new VariableReference;
1111 assign->oper = &Operator::get_operator("=", Operator::BINARY);
1112 assign->right = right;
1114 ExpressionStatement *stmt = new ExpressionStatement;
1115 stmt->expression = assign;
1116 current_block->body.insert(assignment_insert_point, stmt);
1122 void InterfaceGenerator::visit(VariableReference &var)
1124 if(var.declaration || !stage->previous)
1126 /* Don't pull a variable from previous stage if we just generated an output
1127 interface in this stage */
1128 if(stage->content.variables.count(var.name))
1131 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
1132 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
1133 if(i==prev_vars.end() || i->second->interface!="out")
1134 i = prev_vars.find(in_prefix+var.name);
1135 if(i!=prev_vars.end() && i->second->interface=="out")
1137 generate_interface(*i->second, "in", i->second->name);
1138 var.name = i->second->name;
1142 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
1143 map<string, InterfaceBlock *>::const_iterator j = prev_blocks.find("_"+var.name);
1144 if(j!=prev_blocks.end() && j->second->interface=="out")
1146 generate_interface(*j->second);
1147 /* Let VariableResolver convert the variable reference into an interface
1152 for(j=prev_blocks.begin(); j!=prev_blocks.end(); ++j)
1153 if(j->second->instance_name.empty() && j->second->struct_declaration)
1155 const map<string, VariableDeclaration *> &iface_vars = j->second->struct_declaration->members.variables;
1156 i = iface_vars.find(var.name);
1157 if(i!=iface_vars.end())
1159 generate_interface(*j->second);
1165 void InterfaceGenerator::visit(VariableDeclaration &var)
1168 generate_interface(var, "in", var.name);
1169 else if(var.interface=="out")
1171 /* For output variables in function scope, generate a global interface
1172 and replace the local declaration with an assignment. */
1173 VariableDeclaration *out_var = 0;
1174 if(function_scope && (out_var=generate_interface(var, "out", var.name)))
1176 out_var->source = var.source;
1177 out_var->line = var.line;
1178 nodes_to_remove.insert(&var);
1179 if(var.init_expression)
1181 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
1182 stmt.source = var.source;
1183 stmt.line = var.line;
1188 else if(var.interface=="in")
1190 /* Try to link input variables in global scope with output variables from
1192 if(current_block==&stage->content && !var.linked_declaration && stage->previous)
1194 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
1195 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
1196 if(i!=prev_vars.end() && i->second->interface=="out")
1198 var.linked_declaration = i->second;
1199 i->second->linked_declaration = &var;
1204 TraversingVisitor::visit(var);
1207 void InterfaceGenerator::visit(InterfaceBlock &iface)
1209 if(iface.interface=="in")
1211 /* Try to link input blocks with output blocks sharing the same block
1212 name from previous stage. */
1213 if(!iface.linked_block && stage->previous)
1215 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
1216 map<string, InterfaceBlock *>::const_iterator i = prev_blocks.find("out"+iface.name);
1217 if(i!=prev_blocks.end())
1219 iface.linked_block = i->second;
1220 i->second->linked_block = &iface;
1225 TraversingVisitor::visit(iface);
1228 void InterfaceGenerator::visit(FunctionDeclaration &func)
1230 SetFlag set_scope(function_scope, true);
1231 // Skip parameters because they're not useful here
1232 func.body.visit(*this);
1235 void InterfaceGenerator::visit(Passthrough &pass)
1237 vector<VariableDeclaration *> pass_vars;
1239 // Pass through all input variables of this stage.
1240 for(map<string, VariableDeclaration *>::const_iterator i=stage->content.variables.begin(); i!=stage->content.variables.end(); ++i)
1241 if(i->second->interface=="in")
1242 pass_vars.push_back(i->second);
1246 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
1247 for(map<string, VariableDeclaration *>::const_iterator i=prev_vars.begin(); i!=prev_vars.end(); ++i)
1249 if(i->second->interface!="out")
1252 /* Pass through output variables from the previous stage, but only
1253 those which are not already linked to an input here. */
1254 if(!i->second->linked_declaration && generate_interface(*i->second, "in", i->second->name))
1255 pass_vars.push_back(i->second);
1259 if(stage->type==Stage::GEOMETRY)
1261 /* Special case for geometry shader: copy gl_Position from input to
1263 InterfaceBlockReference *ref = new InterfaceBlockReference;
1264 ref->name = "gl_in";
1266 BinaryExpression *subscript = new BinaryExpression;
1267 subscript->left = ref;
1268 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
1269 subscript->right = pass.subscript;
1271 MemberAccess *memacc = new MemberAccess;
1272 memacc->left = subscript;
1273 memacc->member = "gl_Position";
1275 insert_assignment("gl_Position", memacc);
1278 for(vector<VariableDeclaration *>::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i)
1280 string out_name = change_prefix((*i)->name, out_prefix);
1281 generate_interface(**i, "out", out_name);
1283 VariableReference *ref = new VariableReference;
1284 ref->name = (*i)->name;
1287 BinaryExpression *subscript = new BinaryExpression;
1288 subscript->left = ref;
1289 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
1290 subscript->right = pass.subscript;
1291 insert_assignment(out_name, subscript);
1294 insert_assignment(out_name, ref);
1297 nodes_to_remove.insert(&pass);