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),
261 r_assignment_target(0)
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_and_replace(RefPtr<Expression> &expr)
280 r_replacement_expr = 0;
282 if(r_replacement_expr)
284 expr = r_replacement_expr;
285 r_any_resolved = true;
287 r_replacement_expr = 0;
290 void VariableResolver::visit(VariableReference &var)
292 VariableDeclaration *declaration = 0;
294 /* Look for variable declarations in the block hierarchy first. Interface
295 blocks are always defined in the top level so we can't accidentally skip
297 for(Block *block=current_block; (!declaration && block); block=block->parent)
299 map<string, VariableDeclaration *>::iterator i = block->variables.find(var.name);
300 if(i!=block->variables.end())
301 declaration = i->second;
306 const map<string, InterfaceBlock *> &blocks = stage->interface_blocks;
307 map<string, InterfaceBlock *>::const_iterator i = blocks.find("_"+var.name);
310 /* The name refers to an interface block with an instance name rather
311 than a variable. Prepare a new syntax tree node accordingly. */
312 InterfaceBlockReference *iface_ref = new InterfaceBlockReference;
313 iface_ref->source = var.source;
314 iface_ref->line = var.line;
315 iface_ref->name = var.name;
316 iface_ref->declaration = i->second;
317 r_replacement_expr = iface_ref;
321 // Look for the variable in anonymous interface blocks.
322 for(i=blocks.begin(); (!declaration && i!=blocks.end()); ++i)
323 if(i->second->instance_name.empty() && i->second->struct_declaration)
325 const map<string, VariableDeclaration *> &iface_vars = i->second->struct_declaration->members.variables;
326 map<string, VariableDeclaration *>::const_iterator j = iface_vars.find(var.name);
327 if(j!=iface_vars.end())
328 declaration = j->second;
333 r_any_resolved |= (declaration!=var.declaration);
334 var.declaration = declaration;
338 if(r_assignment_target)
340 /* More than one variable reference found in assignment target.
341 Unable to determine what the primary target is. */
342 record_target = false;
343 r_assignment_target = 0;
346 r_assignment_target = var.declaration;
348 else if(var.declaration && var.declaration==r_assignment_target)
349 r_self_referencing = true;
352 void VariableResolver::visit(InterfaceBlockReference &iface)
354 map<string, InterfaceBlock *>::iterator i = stage->interface_blocks.find("_"+iface.name);
355 InterfaceBlock *declaration = (i!=stage->interface_blocks.end() ? i->second : 0);
356 r_any_resolved |= (declaration!=iface.declaration);
357 iface.declaration = declaration;
360 void VariableResolver::visit(MemberAccess &memacc)
362 visit_and_replace(memacc.left);
364 VariableDeclaration *declaration = 0;
365 if(StructDeclaration *strct = dynamic_cast<StructDeclaration *>(memacc.left->type))
367 map<string, VariableDeclaration *>::iterator i = strct->members.variables.find(memacc.member);
368 if(i!=strct->members.variables.end())
369 declaration = i->second;
371 else if(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(memacc.left->type))
373 bool scalar_swizzle = ((basic->kind==BasicTypeDeclaration::INT || basic->kind==BasicTypeDeclaration::FLOAT) && memacc.member.size()==1);
374 bool vector_swizzle = (basic->kind==BasicTypeDeclaration::VECTOR && memacc.member.size()<=4);
375 if(scalar_swizzle || vector_swizzle)
377 static const char component_names[] = { 'x', 'r', 's', 'y', 'g', 't', 'z', 'b', 'p', 'w', 'a', 'q' };
380 UInt8 components[4] = { };
381 for(unsigned i=0; (ok && i<memacc.member.size()); ++i)
382 ok = ((components[i] = (find(component_names, component_names+12, memacc.member[i])-component_names)/3) < 4);
386 Swizzle *swizzle = new Swizzle;
387 swizzle->source = memacc.source;
388 swizzle->line = memacc.line;
389 swizzle->oper = memacc.oper;
390 swizzle->left = memacc.left;
391 swizzle->component_group = memacc.member;
392 swizzle->count = memacc.member.size();
393 copy(components, components+memacc.member.size(), swizzle->components);
394 r_replacement_expr = swizzle;
399 r_any_resolved |= (declaration!=memacc.declaration);
400 memacc.declaration = declaration;
403 void VariableResolver::visit(Swizzle &swizzle)
405 visit_and_replace(swizzle.left);
408 void VariableResolver::visit(UnaryExpression &unary)
410 visit_and_replace(unary.expression);
413 void VariableResolver::visit(BinaryExpression &binary)
415 if(binary.oper->token[0]=='[')
418 /* The subscript expression is not a part of the primary assignment
420 SetFlag set(record_target, false);
421 visit_and_replace(binary.right);
423 visit_and_replace(binary.left);
427 visit_and_replace(binary.left);
428 visit_and_replace(binary.right);
432 void VariableResolver::visit(Assignment &assign)
435 SetFlag set(record_target);
436 r_assignment_target = 0;
437 visit_and_replace(assign.left);
438 r_any_resolved |= (r_assignment_target!=assign.target_declaration);
439 assign.target_declaration = r_assignment_target;
442 r_self_referencing = false;
443 visit_and_replace(assign.right);
444 assign.self_referencing = (r_self_referencing || assign.oper->token[0]!='=');
447 void VariableResolver::visit(FunctionCall &call)
449 for(NodeArray<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
450 visit_and_replace(*i);
453 void VariableResolver::visit(VariableDeclaration &var)
456 var.layout->visit(*this);
458 visit_and_replace(var.array_size);
459 if(var.init_expression)
460 visit_and_replace(var.init_expression);
461 current_block->variables.insert(make_pair(var.name, &var));
464 void VariableResolver::visit(InterfaceBlock &iface)
466 /* Block names can be reused in different interfaces. Prefix the name with
467 the first character of the interface to avoid conflicts. */
468 stage->interface_blocks.insert(make_pair(iface.interface+iface.name, &iface));
469 if(!iface.instance_name.empty())
470 stage->interface_blocks.insert(make_pair("_"+iface.instance_name, &iface));
472 TraversingVisitor::visit(iface);
476 ExpressionResolver::ExpressionResolver():
478 r_any_resolved(false)
481 bool ExpressionResolver::apply(Stage &s)
484 r_any_resolved = false;
485 s.content.visit(*this);
486 return r_any_resolved;
489 bool ExpressionResolver::is_scalar(BasicTypeDeclaration &type)
491 return (type.kind==BasicTypeDeclaration::INT || type.kind==BasicTypeDeclaration::FLOAT);
494 bool ExpressionResolver::is_vector_or_matrix(BasicTypeDeclaration &type)
496 return (type.kind==BasicTypeDeclaration::VECTOR || type.kind==BasicTypeDeclaration::MATRIX);
499 BasicTypeDeclaration *ExpressionResolver::get_element_type(BasicTypeDeclaration &type)
501 if(is_vector_or_matrix(type) || type.kind==BasicTypeDeclaration::ARRAY)
503 BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type);
504 return (basic_base ? get_element_type(*basic_base) : 0);
510 bool ExpressionResolver::can_convert(BasicTypeDeclaration &from, BasicTypeDeclaration &to)
512 if(from.kind==BasicTypeDeclaration::INT && to.kind==BasicTypeDeclaration::FLOAT)
513 return from.size<=to.size;
514 else if(from.kind!=to.kind)
516 else if((from.kind==BasicTypeDeclaration::VECTOR || from.kind==BasicTypeDeclaration::MATRIX) && from.size==to.size)
518 BasicTypeDeclaration *from_base = dynamic_cast<BasicTypeDeclaration *>(from.base_type);
519 BasicTypeDeclaration *to_base = dynamic_cast<BasicTypeDeclaration *>(to.base_type);
520 return (from_base && to_base && can_convert(*from_base, *to_base));
526 ExpressionResolver::Compatibility ExpressionResolver::get_compatibility(BasicTypeDeclaration &left, BasicTypeDeclaration &right)
530 else if(can_convert(left, right))
531 return LEFT_CONVERTIBLE;
532 else if(can_convert(right, left))
533 return RIGHT_CONVERTIBLE;
535 return NOT_COMPATIBLE;
538 BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration::Kind kind, unsigned size)
540 for(vector<BasicTypeDeclaration *>::const_iterator i=basic_types.begin(); i!=basic_types.end(); ++i)
541 if((*i)->kind==kind && (*i)->size==size)
546 BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration &elem_type, BasicTypeDeclaration::Kind kind, unsigned size)
548 for(vector<BasicTypeDeclaration *>::const_iterator i=basic_types.begin(); i!=basic_types.end(); ++i)
549 if(get_element_type(**i)==&elem_type && (*i)->kind==kind && (*i)->size==size)
554 void ExpressionResolver::convert_to(RefPtr<Expression> &expr, BasicTypeDeclaration &type)
556 RefPtr<FunctionCall> call = new FunctionCall;
557 call->name = type.name;
558 call->constructor = true;
559 call->arguments.push_back(0);
560 call->arguments.back() = expr;
565 bool ExpressionResolver::convert_to_element(RefPtr<Expression> &expr, BasicTypeDeclaration &elem_type)
567 if(BasicTypeDeclaration *expr_type = dynamic_cast<BasicTypeDeclaration *>(expr->type))
569 BasicTypeDeclaration *to_type = &elem_type;
570 if(is_vector_or_matrix(*expr_type))
571 to_type = find_type(elem_type, expr_type->kind, expr_type->size);
574 convert_to(expr, *to_type);
582 void ExpressionResolver::resolve(Expression &expr, TypeDeclaration *type, bool lvalue)
584 r_any_resolved |= (type!=expr.type || lvalue!=expr.lvalue);
586 expr.lvalue = lvalue;
589 void ExpressionResolver::visit(Literal &literal)
591 if(literal.value.check_type<bool>())
592 resolve(literal, find_type(BasicTypeDeclaration::BOOL, 1), false);
593 else if(literal.value.check_type<int>())
594 resolve(literal, find_type(BasicTypeDeclaration::INT, 32), false);
595 else if(literal.value.check_type<float>())
596 resolve(literal, find_type(BasicTypeDeclaration::FLOAT, 32), false);
599 void ExpressionResolver::visit(ParenthesizedExpression &parexpr)
601 TraversingVisitor::visit(parexpr);
602 resolve(parexpr, parexpr.expression->type, parexpr.expression->lvalue);
605 void ExpressionResolver::visit(VariableReference &var)
608 resolve(var, var.declaration->type_declaration, true);
611 void ExpressionResolver::visit(InterfaceBlockReference &iface)
613 if(iface.declaration)
614 resolve(iface, iface.declaration->type_declaration, true);
617 void ExpressionResolver::visit(MemberAccess &memacc)
619 TraversingVisitor::visit(memacc);
621 if(memacc.declaration)
622 resolve(memacc, memacc.declaration->type_declaration, memacc.left->lvalue);
625 void ExpressionResolver::visit(Swizzle &swizzle)
627 TraversingVisitor::visit(swizzle);
629 if(BasicTypeDeclaration *left_basic = dynamic_cast<BasicTypeDeclaration *>(swizzle.left->type))
631 BasicTypeDeclaration *left_elem = get_element_type(*left_basic);
633 resolve(swizzle, left_elem, swizzle.left->lvalue);
634 else if(left_basic->kind==BasicTypeDeclaration::VECTOR && left_elem)
635 resolve(swizzle, find_type(*left_elem, left_basic->kind, swizzle.count), swizzle.left->lvalue);
639 void ExpressionResolver::visit(UnaryExpression &unary)
641 TraversingVisitor::visit(unary);
643 BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(unary.expression->type);
647 char oper = unary.oper->token[0];
650 if(basic->kind!=BasicTypeDeclaration::BOOL)
655 if(basic->kind!=BasicTypeDeclaration::INT)
658 else if(oper=='+' || oper=='-')
660 BasicTypeDeclaration *elem = get_element_type(*basic);
661 if(!elem || !is_scalar(*elem))
664 resolve(unary, basic, unary.expression->lvalue);
667 void ExpressionResolver::visit(BinaryExpression &binary, bool assign)
669 /* Binary operators are only defined for basic types (not for image or
671 BasicTypeDeclaration *basic_left = dynamic_cast<BasicTypeDeclaration *>(binary.left->type);
672 BasicTypeDeclaration *basic_right = dynamic_cast<BasicTypeDeclaration *>(binary.right->type);
673 if(!basic_left || !basic_right)
676 char oper = binary.oper->token[0];
679 /* Subscripting operates on vectors, matrices and arrays, and the right
680 operand must be an integer. */
681 if((!is_vector_or_matrix(*basic_left) && basic_left->kind!=BasicTypeDeclaration::ARRAY) || basic_right->kind!=BasicTypeDeclaration::INT)
684 resolve(binary, basic_left->base_type, binary.left->lvalue);
687 else if(basic_left->kind==BasicTypeDeclaration::ARRAY || basic_right->kind==BasicTypeDeclaration::ARRAY)
688 // No other binary operator can be used with arrays.
691 BasicTypeDeclaration *elem_left = get_element_type(*basic_left);
692 BasicTypeDeclaration *elem_right = get_element_type(*basic_right);
693 if(!elem_left || !elem_right)
696 Compatibility compat = get_compatibility(*basic_left, *basic_right);
697 Compatibility elem_compat = get_compatibility(*elem_left, *elem_right);
698 if(elem_compat==NOT_COMPATIBLE)
700 if(assign && (compat==LEFT_CONVERTIBLE || elem_compat==LEFT_CONVERTIBLE))
703 TypeDeclaration *type = 0;
704 char oper2 = binary.oper->token[1];
705 if((oper=='<' && oper2!='<') || (oper=='>' && oper2!='>'))
707 /* Relational operators compare two scalar integer or floating-point
709 if(!is_scalar(*elem_left) || !is_scalar(*elem_right) || compat==NOT_COMPATIBLE)
712 type = find_type(BasicTypeDeclaration::BOOL, 1);
714 else if((oper=='=' || oper=='!') && oper2=='=')
716 // Equality comparison can be done on any compatible types.
717 if(compat==NOT_COMPATIBLE)
720 type = find_type(BasicTypeDeclaration::BOOL, 1);
722 else if(oper2=='&' || oper2=='|' || oper2=='^')
724 // Logical operators can only be applied to booleans.
725 if(basic_left->kind!=BasicTypeDeclaration::BOOL || basic_right->kind!=BasicTypeDeclaration::BOOL)
730 else if((oper=='&' || oper=='|' || oper=='^' || oper=='%') && !oper2)
732 // Bitwise operators and modulo can only be applied to integers.
733 if(basic_left->kind!=BasicTypeDeclaration::INT || basic_right->kind!=BasicTypeDeclaration::INT)
736 type = (compat==LEFT_CONVERTIBLE ? basic_right : basic_left);
738 else if((oper=='<' || oper=='>') && oper2==oper)
740 // Shifts apply to integer scalars and vectors, with some restrictions.
741 if(elem_left->kind!=BasicTypeDeclaration::INT || elem_right->kind!=BasicTypeDeclaration::INT)
743 unsigned left_size = (basic_left->kind==BasicTypeDeclaration::INT ? 1 : basic_left->kind==BasicTypeDeclaration::VECTOR ? basic_left->size : 0);
744 unsigned right_size = (basic_right->kind==BasicTypeDeclaration::INT ? 1 : basic_right->kind==BasicTypeDeclaration::VECTOR ? basic_right->size : 0);
745 if(!left_size || (left_size==1 && right_size!=1) || (left_size>1 && right_size!=1 && right_size!=left_size))
749 // Don't perform conversion even if the operands are of different sizes.
752 else if(oper=='+' || oper=='-' || oper=='*' || oper=='/')
754 // Arithmetic operators require scalar elements.
755 if(!is_scalar(*elem_left) || !is_scalar(*elem_right))
758 if(oper=='*' && is_vector_or_matrix(*basic_left) && is_vector_or_matrix(*basic_right) &&
759 (basic_left->kind==BasicTypeDeclaration::MATRIX || basic_right->kind==BasicTypeDeclaration::MATRIX))
761 /* Multiplication has special rules when at least one operand is a
762 matrix and the other is a vector or a matrix. */
763 unsigned left_columns = basic_left->size&0xFFFF;
764 unsigned right_rows = basic_right->size;
765 if(basic_right->kind==BasicTypeDeclaration::MATRIX)
767 if(left_columns!=right_rows)
770 BasicTypeDeclaration *elem_result = (elem_compat==LEFT_CONVERTIBLE ? elem_right : elem_left);
772 if(basic_left->kind==BasicTypeDeclaration::VECTOR)
773 type = find_type(*elem_result, BasicTypeDeclaration::VECTOR, basic_right->size&0xFFFF);
774 else if(basic_right->kind==BasicTypeDeclaration::VECTOR)
775 type = find_type(*elem_result, BasicTypeDeclaration::VECTOR, basic_left->size>>16);
777 type = find_type(*elem_result, BasicTypeDeclaration::MATRIX, (basic_left->size&0xFFFF0000)|(basic_right->size&0xFFFF));
779 else if(compat==NOT_COMPATIBLE)
781 // Arithmetic between scalars and matrices or vectors is supported.
782 if(is_scalar(*basic_left) && is_vector_or_matrix(*basic_right))
783 type = (elem_compat==RIGHT_CONVERTIBLE ? find_type(*elem_left, basic_right->kind, basic_right->size) : basic_right);
784 else if(is_vector_or_matrix(*basic_left) && is_scalar(*basic_right))
785 type = (elem_compat==LEFT_CONVERTIBLE ? find_type(*elem_right, basic_left->kind, basic_left->size) : basic_left);
789 else if(compat==LEFT_CONVERTIBLE)
797 if(assign && type!=basic_left)
800 bool converted = true;
801 if(compat==LEFT_CONVERTIBLE)
802 convert_to(binary.left, *basic_right);
803 else if(compat==RIGHT_CONVERTIBLE)
804 convert_to(binary.right, *basic_left);
805 else if(elem_compat==LEFT_CONVERTIBLE)
806 converted = convert_to_element(binary.left, *elem_right);
807 else if(elem_compat==RIGHT_CONVERTIBLE)
808 converted = convert_to_element(binary.right, *elem_left);
813 resolve(binary, type, assign);
816 void ExpressionResolver::visit(BinaryExpression &binary)
818 TraversingVisitor::visit(binary);
819 visit(binary, false);
822 void ExpressionResolver::visit(Assignment &assign)
824 TraversingVisitor::visit(assign);
826 if(assign.oper->token[0]!='=')
827 return visit(assign, true);
828 else if(assign.left->type!=assign.right->type)
830 BasicTypeDeclaration *basic_left = dynamic_cast<BasicTypeDeclaration *>(assign.left->type);
831 BasicTypeDeclaration *basic_right = dynamic_cast<BasicTypeDeclaration *>(assign.right->type);
832 if(!basic_left || !basic_right)
835 Compatibility compat = get_compatibility(*basic_left, *basic_right);
836 if(compat==RIGHT_CONVERTIBLE)
837 convert_to(assign.right, *basic_left);
838 else if(compat!=SAME_TYPE)
842 resolve(assign, assign.left->type, true);
845 void ExpressionResolver::visit(FunctionCall &call)
847 TraversingVisitor::visit(call);
849 TypeDeclaration *type = 0;
851 type = call.declaration->return_type_declaration;
852 else if(call.constructor)
854 map<string, TypeDeclaration *>::const_iterator i=stage->types.find(call.name);
855 type = (i!=stage->types.end() ? i->second : 0);
857 resolve(call, type, false);
860 void ExpressionResolver::visit(BasicTypeDeclaration &type)
862 basic_types.push_back(&type);
865 void ExpressionResolver::visit(VariableDeclaration &var)
867 TraversingVisitor::visit(var);
868 if(!var.init_expression)
871 BasicTypeDeclaration *var_basic = dynamic_cast<BasicTypeDeclaration *>(var.type_declaration);
872 BasicTypeDeclaration *init_basic = dynamic_cast<BasicTypeDeclaration *>(var.init_expression->type);
873 if(!var_basic || !init_basic)
876 Compatibility compat = get_compatibility(*var_basic, *init_basic);
877 if(compat==RIGHT_CONVERTIBLE)
878 convert_to(var.init_expression, *var_basic);
882 bool FunctionResolver::apply(Stage &s)
886 r_any_resolved = false;
887 s.content.visit(*this);
888 return r_any_resolved;
891 void FunctionResolver::visit(FunctionCall &call)
893 map<string, FunctionDeclaration *>::iterator i = stage->functions.find(call.name);
894 if(i!=stage->functions.end())
895 call.declaration = i->second;
897 TraversingVisitor::visit(call);
900 void FunctionResolver::visit(FunctionDeclaration &func)
902 FunctionDeclaration *&stage_decl = stage->functions[func.name];
903 vector<FunctionDeclaration *> &decls = declarations[func.name];
904 if(func.definition==&func)
908 // Set all previous declarations to use this definition.
909 for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
911 (*i)->definition = func.definition;
912 (*i)->body.body.clear();
921 func.definition = stage_decl->definition;
923 decls.push_back(&func);
925 TraversingVisitor::visit(func);
929 InterfaceGenerator::InterfaceGenerator():
931 function_scope(false),
933 iface_target_block(0)
936 string InterfaceGenerator::get_out_prefix(Stage::Type type)
938 if(type==Stage::VERTEX)
940 else if(type==Stage::GEOMETRY)
946 void InterfaceGenerator::apply(Stage &s)
949 iface_target_block = &stage->content;
951 in_prefix = get_out_prefix(stage->previous->type);
952 out_prefix = get_out_prefix(stage->type);
953 s.content.visit(*this);
954 NodeRemover().apply(s, nodes_to_remove);
957 void InterfaceGenerator::visit(Block &block)
959 SetForScope<Block *> set_block(current_block, &block);
960 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
962 assignment_insert_point = i;
963 if(&block==&stage->content)
964 iface_insert_point = i;
970 string InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
972 unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
973 return prefix+name.substr(offset);
976 VariableDeclaration *InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
978 if(stage->content.variables.count(name))
981 VariableDeclaration* iface_var = new VariableDeclaration;
982 iface_var->sampling = var.sampling;
983 iface_var->interface = iface;
984 iface_var->type = var.type;
985 iface_var->name = name;
986 /* Geometry shader inputs are always arrays. But if we're bringing in an
987 entire block, the array is on the block and not individual variables. */
988 if(stage->type==Stage::GEOMETRY && !copy_block)
989 iface_var->array = ((var.array && var.interface!="in") || iface=="in");
991 iface_var->array = var.array;
993 iface_var->array_size = var.array_size;
996 iface_var->layout = var.layout;
997 iface_var->linked_declaration = &var;
998 var.linked_declaration = iface_var;
1001 iface_target_block->body.insert(iface_insert_point, iface_var);
1002 iface_target_block->variables.insert(make_pair(name, iface_var));
1007 InterfaceBlock *InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
1009 if(stage->interface_blocks.count("in"+out_block.name))
1012 InterfaceBlock *in_block = new InterfaceBlock;
1013 in_block->interface = "in";
1014 in_block->name = out_block.name;
1015 in_block->members = new Block;
1016 in_block->instance_name = out_block.instance_name;
1017 if(stage->type==Stage::GEOMETRY)
1018 in_block->array = true;
1020 in_block->array = out_block.array;
1021 in_block->linked_block = &out_block;
1022 out_block.linked_block = in_block;
1025 SetFlag set_copy(copy_block, true);
1026 SetForScope<Block *> set_target(iface_target_block, in_block->members.get());
1027 SetForScope<NodeList<Statement>::iterator> set_ins_pt(iface_insert_point, in_block->members->body.end());
1028 if(out_block.struct_declaration)
1029 out_block.struct_declaration->members.visit(*this);
1030 else if(out_block.members)
1031 out_block.members->visit(*this);
1034 iface_target_block->body.insert(iface_insert_point, in_block);
1035 stage->interface_blocks.insert(make_pair("in"+in_block->name, in_block));
1036 if(!in_block->instance_name.empty())
1037 stage->interface_blocks.insert(make_pair("_"+in_block->instance_name, in_block));
1039 SetFlag set_scope(function_scope, false);
1040 SetForScope<Block *> set_block(current_block, &stage->content);
1041 in_block->visit(*this);
1046 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
1048 Assignment *assign = new Assignment;
1049 VariableReference *ref = new VariableReference;
1052 assign->oper = &Operator::get_operator("=", Operator::BINARY);
1053 assign->right = right;
1055 ExpressionStatement *stmt = new ExpressionStatement;
1056 stmt->expression = assign;
1057 current_block->body.insert(assignment_insert_point, stmt);
1063 void InterfaceGenerator::visit(VariableReference &var)
1065 if(var.declaration || !stage->previous)
1067 /* Don't pull a variable from previous stage if we just generated an output
1068 interface in this stage */
1069 if(stage->content.variables.count(var.name))
1072 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
1073 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
1074 if(i==prev_vars.end() || i->second->interface!="out")
1075 i = prev_vars.find(in_prefix+var.name);
1076 if(i!=prev_vars.end() && i->second->interface=="out")
1078 generate_interface(*i->second, "in", i->second->name);
1079 var.name = i->second->name;
1083 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
1084 map<string, InterfaceBlock *>::const_iterator j = prev_blocks.find("_"+var.name);
1085 if(j!=prev_blocks.end() && j->second->interface=="out")
1087 generate_interface(*j->second);
1088 /* Let VariableResolver convert the variable reference into an interface
1093 for(j=prev_blocks.begin(); j!=prev_blocks.end(); ++j)
1094 if(j->second->instance_name.empty() && j->second->struct_declaration)
1096 const map<string, VariableDeclaration *> &iface_vars = j->second->struct_declaration->members.variables;
1097 i = iface_vars.find(var.name);
1098 if(i!=iface_vars.end())
1100 generate_interface(*j->second);
1106 void InterfaceGenerator::visit(VariableDeclaration &var)
1109 generate_interface(var, "in", var.name);
1110 else if(var.interface=="out")
1112 /* For output variables in function scope, generate a global interface
1113 and replace the local declaration with an assignment. */
1114 VariableDeclaration *out_var = 0;
1115 if(function_scope && (out_var=generate_interface(var, "out", var.name)))
1117 out_var->source = var.source;
1118 out_var->line = var.line;
1119 nodes_to_remove.insert(&var);
1120 if(var.init_expression)
1122 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
1123 stmt.source = var.source;
1124 stmt.line = var.line;
1129 else if(var.interface=="in")
1131 /* Try to link input variables in global scope with output variables from
1133 if(current_block==&stage->content && !var.linked_declaration && stage->previous)
1135 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
1136 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
1137 if(i!=prev_vars.end() && i->second->interface=="out")
1139 var.linked_declaration = i->second;
1140 i->second->linked_declaration = &var;
1145 TraversingVisitor::visit(var);
1148 void InterfaceGenerator::visit(InterfaceBlock &iface)
1150 if(iface.interface=="in")
1152 /* Try to link input blocks with output blocks sharing the same block
1153 name from previous stage. */
1154 if(!iface.linked_block && stage->previous)
1156 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
1157 map<string, InterfaceBlock *>::const_iterator i = prev_blocks.find("out"+iface.name);
1158 if(i!=prev_blocks.end())
1160 iface.linked_block = i->second;
1161 i->second->linked_block = &iface;
1166 TraversingVisitor::visit(iface);
1169 void InterfaceGenerator::visit(FunctionDeclaration &func)
1171 SetFlag set_scope(function_scope, true);
1172 // Skip parameters because they're not useful here
1173 func.body.visit(*this);
1176 void InterfaceGenerator::visit(Passthrough &pass)
1178 vector<VariableDeclaration *> pass_vars;
1180 // Pass through all input variables of this stage.
1181 for(map<string, VariableDeclaration *>::const_iterator i=stage->content.variables.begin(); i!=stage->content.variables.end(); ++i)
1182 if(i->second->interface=="in")
1183 pass_vars.push_back(i->second);
1187 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
1188 for(map<string, VariableDeclaration *>::const_iterator i=prev_vars.begin(); i!=prev_vars.end(); ++i)
1190 if(i->second->interface!="out")
1193 /* Pass through output variables from the previous stage, but only
1194 those which are not already linked to an input here. */
1195 if(!i->second->linked_declaration && generate_interface(*i->second, "in", i->second->name))
1196 pass_vars.push_back(i->second);
1200 if(stage->type==Stage::GEOMETRY)
1202 /* Special case for geometry shader: copy gl_Position from input to
1204 InterfaceBlockReference *ref = new InterfaceBlockReference;
1205 ref->name = "gl_in";
1207 BinaryExpression *subscript = new BinaryExpression;
1208 subscript->left = ref;
1209 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
1210 subscript->right = pass.subscript;
1212 MemberAccess *memacc = new MemberAccess;
1213 memacc->left = subscript;
1214 memacc->member = "gl_Position";
1216 insert_assignment("gl_Position", memacc);
1219 for(vector<VariableDeclaration *>::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i)
1221 string out_name = change_prefix((*i)->name, out_prefix);
1222 generate_interface(**i, "out", out_name);
1224 VariableReference *ref = new VariableReference;
1225 ref->name = (*i)->name;
1228 BinaryExpression *subscript = new BinaryExpression;
1229 subscript->left = ref;
1230 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
1231 subscript->right = pass.subscript;
1232 insert_assignment(out_name, subscript);
1235 insert_assignment(out_name, ref);
1238 nodes_to_remove.insert(&pass);