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 ConstantSpecializer::ConstantSpecializer():
19 void ConstantSpecializer::apply(Stage &stage, const map<string, int> *v)
22 stage.content.visit(*this);
25 void ConstantSpecializer::visit(VariableDeclaration &var)
27 bool specializable = false;
30 vector<Layout::Qualifier> &qualifiers = var.layout->qualifiers;
31 for(vector<Layout::Qualifier>::iterator i=qualifiers.begin(); i!=qualifiers.end(); ++i)
32 if(i->name=="constant_id")
38 i->value = hash32(var.name)&0x7FFFFFFF;
42 if(qualifiers.empty())
46 if(specializable && values)
48 map<string, int>::const_iterator i = values->find(var.name);
51 RefPtr<Literal> literal = new Literal;
54 literal->token = (i->second ? "true" : "false");
55 literal->value = static_cast<bool>(i->second);
57 else if(var.type=="int")
59 literal->token = lexical_cast<string>(i->second);
60 literal->value = i->second;
62 var.init_expression = literal;
68 void BlockHierarchyResolver::enter(Block &block)
70 r_any_resolved |= (current_block!=block.parent);
71 block.parent = current_block;
75 TypeResolver::TypeResolver():
81 bool TypeResolver::apply(Stage &s)
85 r_any_resolved = false;
86 s.content.visit(*this);
87 return r_any_resolved;
90 TypeDeclaration *TypeResolver::get_or_create_array_type(TypeDeclaration &type)
92 map<TypeDeclaration *, TypeDeclaration *>::iterator i = array_types.find(&type);
93 if(i!=array_types.end())
96 BasicTypeDeclaration *array = new BasicTypeDeclaration;
97 array->source = BUILTIN_SOURCE;
98 array->name = type.name+"[]";
99 array->kind = BasicTypeDeclaration::ARRAY;
100 array->base = type.name;
101 array->base_type = &type;
102 stage->content.body.insert(type_insert_point, array);
103 array_types[&type] = array;
107 void TypeResolver::resolve_type(TypeDeclaration *&type, const string &name, bool array)
109 TypeDeclaration *resolved = 0;
110 map<string, TypeDeclaration *>::iterator i = stage->types.find(name);
111 if(i!=stage->types.end())
113 map<TypeDeclaration *, TypeDeclaration *>::iterator j = alias_map.find(i->second);
114 resolved = (j!=alias_map.end() ? j->second : i->second);
117 if(resolved && array)
118 resolved = get_or_create_array_type(*resolved);
120 r_any_resolved |= (resolved!=type);
124 void TypeResolver::visit(Block &block)
126 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
129 type_insert_point = i;
134 void TypeResolver::visit(BasicTypeDeclaration &type)
136 resolve_type(type.base_type, type.base, false);
138 if(type.kind==BasicTypeDeclaration::VECTOR && type.base_type)
139 if(BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type))
140 if(basic_base->kind==BasicTypeDeclaration::VECTOR)
142 type.kind = BasicTypeDeclaration::MATRIX;
143 /* A matrix's base type is its column vector type. This will put
144 the column vector's size, i.e. the matrix's row count, in the high
146 type.size |= basic_base->size<<16;
149 if(type.kind==BasicTypeDeclaration::ALIAS && type.base_type)
150 alias_map[&type] = type.base_type;
151 else if(type.kind==BasicTypeDeclaration::ARRAY && type.base_type)
152 array_types[type.base_type] = &type;
154 stage->types.insert(make_pair(type.name, &type));
157 void TypeResolver::visit(ImageTypeDeclaration &type)
159 resolve_type(type.base_type, type.base, false);
160 stage->types.insert(make_pair(type.name, &type));
163 void TypeResolver::visit(StructDeclaration &strct)
165 stage->types.insert(make_pair(strct.name, &strct));
166 TraversingVisitor::visit(strct);
169 void TypeResolver::visit(VariableDeclaration &var)
171 resolve_type(var.type_declaration, var.type, var.array);
172 if(iface_block && var.interface==iface_block->interface)
173 var.interface.clear();
176 void TypeResolver::visit(InterfaceBlock &iface)
180 SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
181 iface.members->visit(*this);
183 StructDeclaration *strct = new StructDeclaration;
184 strct->source = INTERNAL_SOURCE;
185 strct->name = format("_%s_%s", iface.interface, iface.block_name);
186 strct->members.body.splice(strct->members.body.begin(), iface.members->body);
187 stage->content.body.insert(type_insert_point, strct);
188 stage->types.insert(make_pair(strct->name, strct));
191 strct->interface_block = &iface;
192 iface.struct_declaration = strct;
195 TypeDeclaration *type = iface.struct_declaration;
196 if(type && iface.array)
197 type = get_or_create_array_type(*type);
198 r_any_resolved = (type!=iface.type_declaration);
199 iface.type_declaration = type;
202 void TypeResolver::visit(FunctionDeclaration &func)
204 resolve_type(func.return_type_declaration, func.return_type, false);
205 TraversingVisitor::visit(func);
209 VariableResolver::VariableResolver():
211 r_any_resolved(false),
212 record_target(false),
213 r_self_referencing(false)
216 bool VariableResolver::apply(Stage &s)
219 s.interface_blocks.clear();
220 r_any_resolved = false;
221 s.content.visit(*this);
222 for(vector<VariableDeclaration *>::const_iterator i=redeclared_builtins.begin(); i!=redeclared_builtins.end(); ++i)
223 (*i)->source = GENERATED_SOURCE;
224 NodeRemover().apply(s, nodes_to_remove);
225 return r_any_resolved;
228 void VariableResolver::enter(Block &block)
230 block.variables.clear();
233 void VariableResolver::visit(RefPtr<Expression> &expr)
235 r_replacement_expr = 0;
237 if(r_replacement_expr)
239 expr = r_replacement_expr;
240 /* Don't record assignment target when doing a replacement, because chain
241 information won't be correct. */
242 r_assignment_target.declaration = 0;
243 r_any_resolved = true;
245 r_replacement_expr = 0;
248 void VariableResolver::check_assignment_target(Statement *declaration)
252 if(r_assignment_target.declaration)
254 /* More than one reference found in assignment target. Unable to
255 determine what the primary target is. */
256 record_target = false;
257 r_assignment_target.declaration = 0;
260 r_assignment_target.declaration = declaration;
262 // TODO This check is overly broad and may prevent some optimizations.
263 else if(declaration && declaration==r_assignment_target.declaration)
264 r_self_referencing = true;
267 void VariableResolver::visit(VariableReference &var)
269 VariableDeclaration *declaration = 0;
271 /* Look for variable declarations in the block hierarchy first. Interface
272 blocks are always defined in the top level so we can't accidentally skip
274 for(Block *block=current_block; (!declaration && block); block=block->parent)
276 map<string, VariableDeclaration *>::iterator i = block->variables.find(var.name);
277 if(i!=block->variables.end())
278 declaration = i->second;
283 const map<string, InterfaceBlock *> &blocks = stage->interface_blocks;
284 map<string, InterfaceBlock *>::const_iterator i = blocks.find("_"+var.name);
287 /* The name refers to an interface block with an instance name rather
288 than a variable. Prepare a new syntax tree node accordingly. */
289 InterfaceBlockReference *iface_ref = new InterfaceBlockReference;
290 iface_ref->source = var.source;
291 iface_ref->line = var.line;
292 iface_ref->name = var.name;
293 iface_ref->declaration = i->second;
294 r_replacement_expr = iface_ref;
298 // Look for the variable in anonymous interface blocks.
299 for(i=blocks.begin(); (!declaration && i!=blocks.end()); ++i)
300 if(i->second->instance_name.empty() && i->second->struct_declaration)
302 const map<string, VariableDeclaration *> &iface_vars = i->second->struct_declaration->members.variables;
303 map<string, VariableDeclaration *>::const_iterator j = iface_vars.find(var.name);
304 if(j!=iface_vars.end())
305 declaration = j->second;
310 r_any_resolved |= (declaration!=var.declaration);
311 var.declaration = declaration;
313 check_assignment_target(var.declaration);
316 void VariableResolver::visit(InterfaceBlockReference &iface)
318 map<string, InterfaceBlock *>::iterator i = stage->interface_blocks.find("_"+iface.name);
319 InterfaceBlock *declaration = (i!=stage->interface_blocks.end() ? i->second : 0);
320 r_any_resolved |= (declaration!=iface.declaration);
321 iface.declaration = declaration;
323 check_assignment_target(iface.declaration);
326 void VariableResolver::add_to_chain(Assignment::Target::ChainType type, unsigned index)
328 if(r_assignment_target.chain_len<7)
329 r_assignment_target.chain[r_assignment_target.chain_len] = type | min<unsigned>(index, 0x3F);
330 ++r_assignment_target.chain_len;
333 void VariableResolver::visit(MemberAccess &memacc)
335 TraversingVisitor::visit(memacc);
337 VariableDeclaration *declaration = 0;
338 if(StructDeclaration *strct = dynamic_cast<StructDeclaration *>(memacc.left->type))
340 map<string, VariableDeclaration *>::iterator i = strct->members.variables.find(memacc.member);
341 if(i!=strct->members.variables.end())
343 declaration = i->second;
348 for(NodeList<Statement>::const_iterator j=strct->members.body.begin(); (j!=strct->members.body.end() && j->get()!=i->second); ++j)
351 add_to_chain(Assignment::Target::MEMBER, index);
355 else if(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(memacc.left->type))
357 bool scalar_swizzle = ((basic->kind==BasicTypeDeclaration::INT || basic->kind==BasicTypeDeclaration::FLOAT) && memacc.member.size()==1);
358 bool vector_swizzle = (basic->kind==BasicTypeDeclaration::VECTOR && memacc.member.size()<=4);
359 if(scalar_swizzle || vector_swizzle)
361 static const char component_names[] = { 'x', 'r', 's', 'y', 'g', 't', 'z', 'b', 'p', 'w', 'a', 'q' };
364 UInt8 components[4] = { };
365 for(unsigned i=0; (ok && i<memacc.member.size()); ++i)
366 ok = ((components[i] = (find(component_names, component_names+12, memacc.member[i])-component_names)/3) < 4);
370 Swizzle *swizzle = new Swizzle;
371 swizzle->source = memacc.source;
372 swizzle->line = memacc.line;
373 swizzle->oper = memacc.oper;
374 swizzle->left = memacc.left;
375 swizzle->component_group = memacc.member;
376 swizzle->count = memacc.member.size();
377 copy(components, components+memacc.member.size(), swizzle->components);
378 r_replacement_expr = swizzle;
383 r_any_resolved |= (declaration!=memacc.declaration);
384 memacc.declaration = declaration;
387 void VariableResolver::visit(Swizzle &swizzle)
389 TraversingVisitor::visit(swizzle);
394 for(unsigned i=0; i<swizzle.count; ++i)
395 mask |= 1<<swizzle.components[i];
396 add_to_chain(Assignment::Target::SWIZZLE, mask);
400 void VariableResolver::visit(BinaryExpression &binary)
402 if(binary.oper->token[0]=='[')
405 /* The subscript expression is not a part of the primary assignment
407 SetFlag set(record_target, false);
414 unsigned index = 0x3F;
415 if(Literal *literal_subscript = dynamic_cast<Literal *>(binary.right.get()))
416 if(literal_subscript->value.check_type<int>())
417 index = literal_subscript->value.value<int>();
418 add_to_chain(Assignment::Target::ARRAY, index);
422 TraversingVisitor::visit(binary);
425 void VariableResolver::visit(Assignment &assign)
428 SetFlag set(record_target);
429 r_assignment_target = Assignment::Target();
431 r_any_resolved |= (r_assignment_target<assign.target || assign.target<r_assignment_target);
432 assign.target = r_assignment_target;
435 r_self_referencing = false;
437 assign.self_referencing = (r_self_referencing || assign.oper->token[0]!='=');
440 void VariableResolver::merge_layouts(Layout &to_layout, const Layout &from_layout)
442 for(vector<Layout::Qualifier>::const_iterator i=from_layout.qualifiers.begin(); i!=from_layout.qualifiers.end(); ++i)
445 for(vector<Layout::Qualifier>::iterator j=to_layout.qualifiers.begin(); (!found && j!=to_layout.qualifiers.end()); ++j)
448 j->has_value = i->value;
454 to_layout.qualifiers.push_back(*i);
458 void VariableResolver::visit(VariableDeclaration &var)
460 TraversingVisitor::visit(var);
461 VariableDeclaration *&ptr = current_block->variables[var.name];
464 else if(!current_block->parent && ptr->interface==var.interface && ptr->type==var.type)
466 if(ptr->source==BUILTIN_SOURCE)
467 redeclared_builtins.push_back(&var);
469 stage->diagnostics.push_back(Diagnostic(Diagnostic::WARN, var.source, var.line,
470 format("Redeclaring non-builtin variable '%s' is deprecated", var.name)));
472 if(var.init_expression)
473 ptr->init_expression = var.init_expression;
477 merge_layouts(*ptr->layout, *var.layout);
479 ptr->layout = var.layout;
481 nodes_to_remove.insert(&var);
483 r_any_resolved = true;
487 void VariableResolver::visit(InterfaceBlock &iface)
489 /* Block names can be reused in different interfaces. Prefix the name with
490 the first character of the interface to avoid conflicts. */
491 stage->interface_blocks.insert(make_pair(iface.interface+iface.block_name, &iface));
492 if(!iface.instance_name.empty())
493 stage->interface_blocks.insert(make_pair("_"+iface.instance_name, &iface));
495 TraversingVisitor::visit(iface);
499 ExpressionResolver::ExpressionResolver():
501 r_any_resolved(false)
504 bool ExpressionResolver::apply(Stage &s)
507 r_any_resolved = false;
508 s.content.visit(*this);
509 return r_any_resolved;
512 bool ExpressionResolver::is_scalar(BasicTypeDeclaration &type)
514 return (type.kind==BasicTypeDeclaration::INT || type.kind==BasicTypeDeclaration::FLOAT);
517 bool ExpressionResolver::is_vector_or_matrix(BasicTypeDeclaration &type)
519 return (type.kind==BasicTypeDeclaration::VECTOR || type.kind==BasicTypeDeclaration::MATRIX);
522 BasicTypeDeclaration *ExpressionResolver::get_element_type(BasicTypeDeclaration &type)
524 if(is_vector_or_matrix(type) || type.kind==BasicTypeDeclaration::ARRAY)
526 BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type);
527 return (basic_base ? get_element_type(*basic_base) : 0);
533 bool ExpressionResolver::can_convert(BasicTypeDeclaration &from, BasicTypeDeclaration &to)
535 if(from.kind==BasicTypeDeclaration::INT && to.kind==BasicTypeDeclaration::FLOAT)
536 return from.size<=to.size;
537 else if(from.kind!=to.kind)
539 else if((from.kind==BasicTypeDeclaration::VECTOR || from.kind==BasicTypeDeclaration::MATRIX) && from.size==to.size)
541 BasicTypeDeclaration *from_base = dynamic_cast<BasicTypeDeclaration *>(from.base_type);
542 BasicTypeDeclaration *to_base = dynamic_cast<BasicTypeDeclaration *>(to.base_type);
543 return (from_base && to_base && can_convert(*from_base, *to_base));
549 ExpressionResolver::Compatibility ExpressionResolver::get_compatibility(BasicTypeDeclaration &left, BasicTypeDeclaration &right)
553 else if(can_convert(left, right))
554 return LEFT_CONVERTIBLE;
555 else if(can_convert(right, left))
556 return RIGHT_CONVERTIBLE;
558 return NOT_COMPATIBLE;
561 BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration::Kind kind, unsigned size)
563 for(vector<BasicTypeDeclaration *>::const_iterator i=basic_types.begin(); i!=basic_types.end(); ++i)
564 if((*i)->kind==kind && (*i)->size==size)
569 BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration &elem_type, BasicTypeDeclaration::Kind kind, unsigned size)
571 for(vector<BasicTypeDeclaration *>::const_iterator i=basic_types.begin(); i!=basic_types.end(); ++i)
572 if(get_element_type(**i)==&elem_type && (*i)->kind==kind && (*i)->size==size)
577 void ExpressionResolver::convert_to(RefPtr<Expression> &expr, BasicTypeDeclaration &type)
579 RefPtr<FunctionCall> call = new FunctionCall;
580 call->name = type.name;
581 call->constructor = true;
582 call->arguments.push_back_nocopy(expr);
587 bool ExpressionResolver::convert_to_element(RefPtr<Expression> &expr, BasicTypeDeclaration &elem_type)
589 if(BasicTypeDeclaration *expr_basic = dynamic_cast<BasicTypeDeclaration *>(expr->type))
591 BasicTypeDeclaration *to_type = &elem_type;
592 if(is_vector_or_matrix(*expr_basic))
593 to_type = find_type(elem_type, expr_basic->kind, expr_basic->size);
596 convert_to(expr, *to_type);
604 bool ExpressionResolver::truncate_vector(RefPtr<Expression> &expr, unsigned size)
606 if(BasicTypeDeclaration *expr_basic = dynamic_cast<BasicTypeDeclaration *>(expr->type))
607 if(BasicTypeDeclaration *expr_elem = get_element_type(*expr_basic))
609 RefPtr<Swizzle> swizzle = new Swizzle;
610 swizzle->left = expr;
611 swizzle->oper = &Operator::get_operator(".", Operator::POSTFIX);
612 swizzle->component_group = string("xyzw", size);
613 swizzle->count = size;
614 for(unsigned i=0; i<size; ++i)
615 swizzle->components[i] = i;
617 swizzle->type = expr_elem;
619 swizzle->type = find_type(*expr_elem, BasicTypeDeclaration::VECTOR, size);
628 void ExpressionResolver::resolve(Expression &expr, TypeDeclaration *type, bool lvalue)
630 r_any_resolved |= (type!=expr.type || lvalue!=expr.lvalue);
632 expr.lvalue = lvalue;
635 void ExpressionResolver::visit(Block &block)
637 SetForScope<Block *> set_block(current_block, &block);
638 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
645 void ExpressionResolver::visit(Literal &literal)
647 if(literal.value.check_type<bool>())
648 resolve(literal, find_type(BasicTypeDeclaration::BOOL, 1), false);
649 else if(literal.value.check_type<int>())
650 resolve(literal, find_type(BasicTypeDeclaration::INT, 32), false);
651 else if(literal.value.check_type<float>())
652 resolve(literal, find_type(BasicTypeDeclaration::FLOAT, 32), false);
655 void ExpressionResolver::visit(VariableReference &var)
658 resolve(var, var.declaration->type_declaration, true);
661 void ExpressionResolver::visit(InterfaceBlockReference &iface)
663 if(iface.declaration)
664 resolve(iface, iface.declaration->type_declaration, true);
667 void ExpressionResolver::visit(MemberAccess &memacc)
669 TraversingVisitor::visit(memacc);
671 if(memacc.declaration)
672 resolve(memacc, memacc.declaration->type_declaration, memacc.left->lvalue);
675 void ExpressionResolver::visit(Swizzle &swizzle)
677 TraversingVisitor::visit(swizzle);
679 if(BasicTypeDeclaration *left_basic = dynamic_cast<BasicTypeDeclaration *>(swizzle.left->type))
681 BasicTypeDeclaration *left_elem = get_element_type(*left_basic);
683 resolve(swizzle, left_elem, swizzle.left->lvalue);
684 else if(left_basic->kind==BasicTypeDeclaration::VECTOR && left_elem)
685 resolve(swizzle, find_type(*left_elem, left_basic->kind, swizzle.count), swizzle.left->lvalue);
689 void ExpressionResolver::visit(UnaryExpression &unary)
691 TraversingVisitor::visit(unary);
693 BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(unary.expression->type);
697 char oper = unary.oper->token[0];
700 if(basic->kind!=BasicTypeDeclaration::BOOL)
705 if(basic->kind!=BasicTypeDeclaration::INT)
708 else if(oper=='+' || oper=='-')
710 BasicTypeDeclaration *elem = get_element_type(*basic);
711 if(!elem || !is_scalar(*elem))
714 resolve(unary, basic, unary.expression->lvalue);
717 void ExpressionResolver::visit(BinaryExpression &binary, bool assign)
719 /* Binary operators are only defined for basic types (not for image or
721 BasicTypeDeclaration *basic_left = dynamic_cast<BasicTypeDeclaration *>(binary.left->type);
722 BasicTypeDeclaration *basic_right = dynamic_cast<BasicTypeDeclaration *>(binary.right->type);
723 if(!basic_left || !basic_right)
726 char oper = binary.oper->token[0];
729 /* Subscripting operates on vectors, matrices and arrays, and the right
730 operand must be an integer. */
731 if((!is_vector_or_matrix(*basic_left) && basic_left->kind!=BasicTypeDeclaration::ARRAY) || basic_right->kind!=BasicTypeDeclaration::INT)
734 resolve(binary, basic_left->base_type, binary.left->lvalue);
737 else if(basic_left->kind==BasicTypeDeclaration::ARRAY || basic_right->kind==BasicTypeDeclaration::ARRAY)
738 // No other binary operator can be used with arrays.
741 BasicTypeDeclaration *elem_left = get_element_type(*basic_left);
742 BasicTypeDeclaration *elem_right = get_element_type(*basic_right);
743 if(!elem_left || !elem_right)
746 Compatibility compat = get_compatibility(*basic_left, *basic_right);
747 Compatibility elem_compat = get_compatibility(*elem_left, *elem_right);
748 if(elem_compat==NOT_COMPATIBLE)
750 if(assign && (compat==LEFT_CONVERTIBLE || elem_compat==LEFT_CONVERTIBLE))
753 TypeDeclaration *type = 0;
754 char oper2 = binary.oper->token[1];
755 if((oper=='<' && oper2!='<') || (oper=='>' && oper2!='>'))
757 /* Relational operators compare two scalar integer or floating-point
759 if(!is_scalar(*elem_left) || !is_scalar(*elem_right) || compat==NOT_COMPATIBLE)
762 type = find_type(BasicTypeDeclaration::BOOL, 1);
764 else if((oper=='=' || oper=='!') && oper2=='=')
766 // Equality comparison can be done on any compatible types.
767 if(compat==NOT_COMPATIBLE)
770 type = find_type(BasicTypeDeclaration::BOOL, 1);
772 else if(oper2=='&' || oper2=='|' || oper2=='^')
774 // Logical operators can only be applied to booleans.
775 if(basic_left->kind!=BasicTypeDeclaration::BOOL || basic_right->kind!=BasicTypeDeclaration::BOOL)
780 else if((oper=='&' || oper=='|' || oper=='^' || oper=='%') && !oper2)
782 // Bitwise operators and modulo can only be applied to integers.
783 if(basic_left->kind!=BasicTypeDeclaration::INT || basic_right->kind!=BasicTypeDeclaration::INT)
786 type = (compat==LEFT_CONVERTIBLE ? basic_right : basic_left);
788 else if((oper=='<' || oper=='>') && oper2==oper)
790 // Shifts apply to integer scalars and vectors, with some restrictions.
791 if(elem_left->kind!=BasicTypeDeclaration::INT || elem_right->kind!=BasicTypeDeclaration::INT)
793 unsigned left_size = (basic_left->kind==BasicTypeDeclaration::INT ? 1 : basic_left->kind==BasicTypeDeclaration::VECTOR ? basic_left->size : 0);
794 unsigned right_size = (basic_right->kind==BasicTypeDeclaration::INT ? 1 : basic_right->kind==BasicTypeDeclaration::VECTOR ? basic_right->size : 0);
795 if(!left_size || (left_size==1 && right_size!=1) || (left_size>1 && right_size!=1 && right_size!=left_size))
799 // Don't perform conversion even if the operands are of different sizes.
802 else if(oper=='+' || oper=='-' || oper=='*' || oper=='/')
804 // Arithmetic operators require scalar elements.
805 if(!is_scalar(*elem_left) || !is_scalar(*elem_right))
808 if(oper=='*' && is_vector_or_matrix(*basic_left) && is_vector_or_matrix(*basic_right) &&
809 (basic_left->kind==BasicTypeDeclaration::MATRIX || basic_right->kind==BasicTypeDeclaration::MATRIX))
811 /* Multiplication has special rules when at least one operand is a
812 matrix and the other is a vector or a matrix. */
813 unsigned left_columns = basic_left->size&0xFFFF;
814 unsigned right_rows = basic_right->size;
815 if(basic_right->kind==BasicTypeDeclaration::MATRIX)
817 if(left_columns!=right_rows)
820 BasicTypeDeclaration *elem_result = (elem_compat==LEFT_CONVERTIBLE ? elem_right : elem_left);
822 if(basic_left->kind==BasicTypeDeclaration::VECTOR)
823 type = find_type(*elem_result, BasicTypeDeclaration::VECTOR, basic_right->size&0xFFFF);
824 else if(basic_right->kind==BasicTypeDeclaration::VECTOR)
825 type = find_type(*elem_result, BasicTypeDeclaration::VECTOR, basic_left->size>>16);
827 type = find_type(*elem_result, BasicTypeDeclaration::MATRIX, (basic_left->size&0xFFFF0000)|(basic_right->size&0xFFFF));
829 else if(compat==NOT_COMPATIBLE)
831 // Arithmetic between scalars and matrices or vectors is supported.
832 if(is_scalar(*basic_left) && is_vector_or_matrix(*basic_right))
833 type = (elem_compat==RIGHT_CONVERTIBLE ? find_type(*elem_left, basic_right->kind, basic_right->size) : basic_right);
834 else if(is_vector_or_matrix(*basic_left) && is_scalar(*basic_right))
835 type = (elem_compat==LEFT_CONVERTIBLE ? find_type(*elem_right, basic_left->kind, basic_left->size) : basic_left);
839 else if(compat==LEFT_CONVERTIBLE)
847 if(assign && type!=basic_left)
850 bool converted = true;
851 if(compat==LEFT_CONVERTIBLE)
852 convert_to(binary.left, *basic_right);
853 else if(compat==RIGHT_CONVERTIBLE)
854 convert_to(binary.right, *basic_left);
855 else if(elem_compat==LEFT_CONVERTIBLE)
856 converted = convert_to_element(binary.left, *elem_right);
857 else if(elem_compat==RIGHT_CONVERTIBLE)
858 converted = convert_to_element(binary.right, *elem_left);
863 resolve(binary, type, assign);
866 void ExpressionResolver::visit(BinaryExpression &binary)
868 TraversingVisitor::visit(binary);
869 visit(binary, false);
872 void ExpressionResolver::visit(Assignment &assign)
874 TraversingVisitor::visit(assign);
876 if(assign.oper->token[0]!='=')
877 return visit(assign, true);
878 else if(assign.left->type!=assign.right->type)
880 BasicTypeDeclaration *basic_left = dynamic_cast<BasicTypeDeclaration *>(assign.left->type);
881 BasicTypeDeclaration *basic_right = dynamic_cast<BasicTypeDeclaration *>(assign.right->type);
882 if(!basic_left || !basic_right)
885 Compatibility compat = get_compatibility(*basic_left, *basic_right);
886 if(compat==RIGHT_CONVERTIBLE)
887 convert_to(assign.right, *basic_left);
888 else if(compat!=SAME_TYPE)
892 resolve(assign, assign.left->type, true);
895 void ExpressionResolver::visit(TernaryExpression &ternary)
897 TraversingVisitor::visit(ternary);
899 BasicTypeDeclaration *basic_cond = dynamic_cast<BasicTypeDeclaration *>(ternary.condition->type);
900 if(!basic_cond || basic_cond->kind!=BasicTypeDeclaration::BOOL)
903 TypeDeclaration *type = 0;
904 if(ternary.true_expr->type==ternary.false_expr->type)
905 type = ternary.true_expr->type;
908 BasicTypeDeclaration *basic_true = dynamic_cast<BasicTypeDeclaration *>(ternary.true_expr->type);
909 BasicTypeDeclaration *basic_false = dynamic_cast<BasicTypeDeclaration *>(ternary.false_expr->type);
910 if(!basic_true || !basic_false)
913 Compatibility compat = get_compatibility(*basic_true, *basic_false);
914 if(compat==NOT_COMPATIBLE)
917 type = (compat==LEFT_CONVERTIBLE ? basic_true : basic_false);
919 if(compat==LEFT_CONVERTIBLE)
920 convert_to(ternary.true_expr, *basic_false);
921 else if(compat==RIGHT_CONVERTIBLE)
922 convert_to(ternary.false_expr, *basic_true);
925 resolve(ternary, type, false);
928 void ExpressionResolver::visit_constructor(FunctionCall &call)
930 if(call.arguments.empty())
933 map<string, TypeDeclaration *>::const_iterator i = stage->types.find(call.name);
934 if(i==stage->types.end())
936 else if(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(i->second))
938 BasicTypeDeclaration *elem = get_element_type(*basic);
942 vector<ArgumentInfo> args;
943 args.reserve(call.arguments.size());
944 unsigned arg_component_total = 0;
945 bool has_matrices = false;
946 for(NodeArray<Expression>::const_iterator j=call.arguments.begin(); j!=call.arguments.end(); ++j)
949 if(!(info.type=dynamic_cast<BasicTypeDeclaration *>((*j)->type)))
951 if(is_scalar(*info.type) || info.type->kind==BasicTypeDeclaration::BOOL)
952 info.component_count = 1;
953 else if(info.type->kind==BasicTypeDeclaration::VECTOR)
954 info.component_count = info.type->size;
955 else if(info.type->kind==BasicTypeDeclaration::MATRIX)
957 info.component_count = (info.type->size>>16)*(info.type->size&0xFFFF);
962 arg_component_total += info.component_count;
963 args.push_back(info);
966 bool convert_args = false;
967 if((is_scalar(*basic) || basic->kind==BasicTypeDeclaration::BOOL) && call.arguments.size()==1 && !has_matrices)
969 if(arg_component_total>1)
970 truncate_vector(call.arguments.front(), 1);
972 /* Single-element type constructors never need to convert their
973 arguments because the constructor *is* the conversion. */
975 else if(basic->kind==BasicTypeDeclaration::VECTOR && !has_matrices)
977 /* Vector constructors need either a single scalar argument or
978 enough components to fill out the vector. */
979 if(arg_component_total!=1 && arg_component_total<basic->size)
982 /* A vector of same size can be converted directly. For other
983 combinations the individual arguments need to be converted. */
984 if(call.arguments.size()==1)
986 if(arg_component_total==1)
988 else if(arg_component_total>basic->size)
989 truncate_vector(call.arguments.front(), basic->size);
991 else if(arg_component_total==basic->size)
996 else if(basic->kind==BasicTypeDeclaration::MATRIX)
998 unsigned column_count = basic->size&0xFFFF;
999 unsigned row_count = basic->size>>16;
1000 if(call.arguments.size()==1)
1002 /* A matrix can be constructed from a single element or another
1003 matrix of sufficient size. */
1004 if(arg_component_total==1)
1005 convert_args = true;
1006 else if(args.front().type->kind==BasicTypeDeclaration::MATRIX)
1008 unsigned arg_columns = args.front().type->size&0xFFFF;
1009 unsigned arg_rows = args.front().type->size>>16;
1010 if(arg_columns<column_count || arg_rows<row_count)
1013 /* Always generate a temporary here and let the optimization
1014 stage inline it if that's reasonable. */
1015 RefPtr<VariableDeclaration> temporary = new VariableDeclaration;
1016 temporary->type = args.front().type->name;
1017 temporary->name = get_unused_variable_name(*current_block, "_temp");
1018 temporary->init_expression = call.arguments.front();
1019 current_block->body.insert(insert_point, temporary);
1021 // Create expressions to build each column.
1022 vector<RefPtr<Expression> > columns;
1023 columns.reserve(column_count);
1024 for(unsigned j=0; j<column_count; ++j)
1026 RefPtr<VariableReference> ref = new VariableReference;
1027 ref->name = temporary->name;
1029 RefPtr<Literal> index = new Literal;
1030 index->token = lexical_cast<string>(j);
1031 index->value = static_cast<int>(j);
1033 RefPtr<BinaryExpression> subscript = new BinaryExpression;
1034 subscript->left = ref;
1035 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
1036 subscript->right = index;
1037 subscript->type = args.front().type->base_type;
1039 columns.push_back(subscript);
1040 if(arg_rows>row_count)
1041 truncate_vector(columns.back(), row_count);
1044 call.arguments.resize(column_count);
1045 copy(columns.begin(), columns.end(), call.arguments.begin());
1047 /* Let VariableResolver process the new nodes and finish
1048 resolving the constructor on the next pass. */
1049 r_any_resolved = true;
1055 else if(arg_component_total==column_count*row_count && !has_matrices)
1057 /* Construct a matrix from individual components in column-major
1058 order. Arguments must align at column boundaries. */
1059 vector<RefPtr<Expression> > columns;
1060 columns.reserve(column_count);
1062 vector<RefPtr<Expression> > column_args;
1063 column_args.reserve(row_count);
1064 unsigned column_component_count = 0;
1066 for(unsigned j=0; j<call.arguments.size(); ++j)
1068 const ArgumentInfo &info = args[j];
1069 if(!column_component_count && info.type->kind==BasicTypeDeclaration::VECTOR && info.component_count==row_count)
1070 // A vector filling the entire column can be used as is.
1071 columns.push_back(call.arguments[j]);
1074 column_args.push_back(call.arguments[j]);
1075 column_component_count += info.component_count;
1076 if(column_component_count==row_count)
1078 /* The column has filled up. Create a vector constructor
1080 RefPtr<FunctionCall> column_call = new FunctionCall;
1081 column_call->name = basic->base_type->name;
1082 column_call->constructor = true;
1083 column_call->arguments.resize(column_args.size());
1084 copy(column_args.begin(), column_args.end(), column_call->arguments.begin());
1085 column_call->type = basic->base_type;
1086 visit_constructor(*column_call);
1087 columns.push_back(column_call);
1089 column_args.clear();
1090 column_component_count = 0;
1092 else if(column_component_count>row_count)
1093 // Argument alignment mismatch.
1106 // The argument list may have changed so can't rely on args.
1107 for(NodeArray<Expression>::iterator j=call.arguments.begin(); j!=call.arguments.end(); ++j)
1108 if(BasicTypeDeclaration *basic_arg = dynamic_cast<BasicTypeDeclaration *>((*j)->type))
1110 BasicTypeDeclaration *elem_arg = get_element_type(*basic_arg);
1112 convert_to_element(*j, *elem);
1116 else if(StructDeclaration *strct = dynamic_cast<StructDeclaration *>(i->second))
1118 if(call.arguments.size()!=strct->members.body.size())
1122 for(NodeList<Statement>::const_iterator j=strct->members.body.begin(); j!=strct->members.body.end(); ++j, ++k)
1124 if(VariableDeclaration *var = dynamic_cast<VariableDeclaration *>(j->get()))
1126 if(!call.arguments[k]->type || call.arguments[k]->type!=var->type_declaration)
1134 resolve(call, i->second, false);
1137 void ExpressionResolver::visit(FunctionCall &call)
1139 TraversingVisitor::visit(call);
1141 if(call.declaration)
1142 resolve(call, call.declaration->return_type_declaration, false);
1143 else if(call.constructor)
1144 visit_constructor(call);
1147 void ExpressionResolver::visit(BasicTypeDeclaration &type)
1149 basic_types.push_back(&type);
1152 void ExpressionResolver::visit(VariableDeclaration &var)
1154 TraversingVisitor::visit(var);
1155 if(!var.init_expression)
1158 BasicTypeDeclaration *var_basic = dynamic_cast<BasicTypeDeclaration *>(var.type_declaration);
1159 BasicTypeDeclaration *init_basic = dynamic_cast<BasicTypeDeclaration *>(var.init_expression->type);
1160 if(!var_basic || !init_basic)
1163 Compatibility compat = get_compatibility(*var_basic, *init_basic);
1164 if(compat==RIGHT_CONVERTIBLE)
1165 convert_to(var.init_expression, *var_basic);
1169 bool FunctionResolver::apply(Stage &s)
1172 s.functions.clear();
1173 r_any_resolved = false;
1174 s.content.visit(*this);
1175 return r_any_resolved;
1178 void FunctionResolver::visit(FunctionCall &call)
1180 FunctionDeclaration *declaration = 0;
1181 if(stage->types.count(call.name))
1182 call.constructor = true;
1186 bool has_signature = true;
1187 for(NodeArray<Expression>::const_iterator i=call.arguments.begin(); (has_signature && i!=call.arguments.end()); ++i)
1190 append(arg_types, ",", (*i)->type->name);
1192 has_signature = false;
1197 map<string, FunctionDeclaration *>::iterator i = stage->functions.find(format("%s(%s)", call.name, arg_types));
1198 declaration = (i!=stage->functions.end() ? i->second : 0);
1202 r_any_resolved |= (declaration!=call.declaration);
1203 call.declaration = declaration;
1205 TraversingVisitor::visit(call);
1208 void FunctionResolver::visit(FunctionDeclaration &func)
1210 if(func.signature.empty())
1213 for(NodeArray<VariableDeclaration>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
1215 if((*i)->type_declaration)
1216 append(param_types, ",", (*i)->type_declaration->name);
1220 func.signature = format("(%s)", param_types);
1221 r_any_resolved = true;
1224 string key = func.name+func.signature;
1225 FunctionDeclaration *&stage_decl = stage->functions[key];
1226 vector<FunctionDeclaration *> &decls = declarations[key];
1227 if(func.definition==&func)
1229 if(stage_decl && stage_decl->definition)
1232 stage->diagnostics.push_back(Diagnostic(Diagnostic::WARN, func.source, func.line,
1233 format("Overriding function '%s' without the override keyword is deprecated", key)));
1234 if(!stage_decl->definition->virtua)
1235 stage->diagnostics.push_back(Diagnostic(Diagnostic::WARN, func.source, func.line,
1236 format("Overriding function '%s' not declared as virtual is deprecated", key)));
1240 // Set all previous declarations to use this definition.
1241 for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
1243 r_any_resolved |= (func.definition!=(*i)->definition);
1244 (*i)->definition = func.definition;
1245 (*i)->body.body.clear();
1250 FunctionDeclaration *definition = (stage_decl ? stage_decl->definition : 0);
1251 r_any_resolved |= (definition!=func.definition);
1252 func.definition = definition;
1257 decls.push_back(&func);
1259 TraversingVisitor::visit(func);
1263 InterfaceGenerator::InterfaceGenerator():
1265 function_scope(false),
1267 iface_target_block(0)
1270 string InterfaceGenerator::get_out_prefix(Stage::Type type)
1272 if(type==Stage::VERTEX)
1274 else if(type==Stage::GEOMETRY)
1280 void InterfaceGenerator::apply(Stage &s)
1283 iface_target_block = &stage->content;
1285 in_prefix = get_out_prefix(stage->previous->type);
1286 out_prefix = get_out_prefix(stage->type);
1287 s.content.visit(*this);
1288 NodeRemover().apply(s, nodes_to_remove);
1291 void InterfaceGenerator::visit(Block &block)
1293 SetForScope<Block *> set_block(current_block, &block);
1294 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
1296 assignment_insert_point = i;
1297 if(&block==&stage->content)
1298 iface_insert_point = i;
1304 string InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
1306 unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
1307 return prefix+name.substr(offset);
1310 VariableDeclaration *InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
1312 if(stage->content.variables.count(name))
1315 if(stage->type==Stage::GEOMETRY && !copy_block && var.interface=="out" && var.array)
1318 VariableDeclaration* iface_var = new VariableDeclaration;
1319 iface_var->sampling = var.sampling;
1320 iface_var->interface = iface;
1321 iface_var->type = var.type;
1322 iface_var->name = name;
1323 /* Geometry shader inputs are always arrays. But if we're bringing in an
1324 entire block, the array is on the block and not individual variables. */
1325 if(stage->type==Stage::GEOMETRY && !copy_block)
1326 iface_var->array = ((var.array && var.interface!="in") || iface=="in");
1328 iface_var->array = var.array;
1329 if(iface_var->array)
1330 iface_var->array_size = var.array_size;
1333 iface_var->layout = var.layout;
1334 iface_var->linked_declaration = &var;
1335 var.linked_declaration = iface_var;
1338 iface_target_block->body.insert(iface_insert_point, iface_var);
1339 iface_target_block->variables.insert(make_pair(name, iface_var));
1340 if(iface_target_block==&stage->content && iface=="in")
1341 declared_inputs.push_back(iface_var);
1346 InterfaceBlock *InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
1348 if(stage->interface_blocks.count("in"+out_block.block_name))
1351 InterfaceBlock *in_block = new InterfaceBlock;
1352 in_block->interface = "in";
1353 in_block->block_name = out_block.block_name;
1354 in_block->members = new Block;
1355 in_block->instance_name = out_block.instance_name;
1356 if(stage->type==Stage::GEOMETRY)
1357 in_block->array = true;
1359 in_block->array = out_block.array;
1360 in_block->linked_block = &out_block;
1361 out_block.linked_block = in_block;
1364 SetFlag set_copy(copy_block, true);
1365 SetForScope<Block *> set_target(iface_target_block, in_block->members.get());
1366 SetForScope<NodeList<Statement>::iterator> set_ins_pt(iface_insert_point, in_block->members->body.end());
1367 if(out_block.struct_declaration)
1368 out_block.struct_declaration->members.visit(*this);
1369 else if(out_block.members)
1370 out_block.members->visit(*this);
1373 iface_target_block->body.insert(iface_insert_point, in_block);
1374 stage->interface_blocks.insert(make_pair("in"+in_block->block_name, in_block));
1375 if(!in_block->instance_name.empty())
1376 stage->interface_blocks.insert(make_pair("_"+in_block->instance_name, in_block));
1378 SetFlag set_scope(function_scope, false);
1379 SetForScope<Block *> set_block(current_block, &stage->content);
1380 in_block->visit(*this);
1385 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
1387 Assignment *assign = new Assignment;
1388 VariableReference *ref = new VariableReference;
1391 assign->oper = &Operator::get_operator("=", Operator::BINARY);
1392 assign->right = right;
1394 ExpressionStatement *stmt = new ExpressionStatement;
1395 stmt->expression = assign;
1396 current_block->body.insert(assignment_insert_point, stmt);
1402 void InterfaceGenerator::visit(VariableReference &var)
1404 if(var.declaration || !stage->previous)
1406 /* Don't pull a variable from previous stage if we just generated an output
1407 interface in this stage */
1408 if(stage->content.variables.count(var.name))
1411 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
1412 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
1413 if(i==prev_vars.end() || i->second->interface!="out")
1414 i = prev_vars.find(in_prefix+var.name);
1415 if(i!=prev_vars.end() && i->second->interface=="out")
1417 if(stage->type==Stage::GEOMETRY && i->second->array)
1418 stage->diagnostics.push_back(Diagnostic(Diagnostic::WARN, var.source, var.line,
1419 format("Can't access '%s' through automatic interface because it's an array", var.name)));
1422 generate_interface(*i->second, "in", i->second->name);
1423 var.name = i->second->name;
1428 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
1429 map<string, InterfaceBlock *>::const_iterator j = prev_blocks.find("_"+var.name);
1430 if(j!=prev_blocks.end() && j->second->interface=="out")
1432 generate_interface(*j->second);
1433 /* Let VariableResolver convert the variable reference into an interface
1438 for(j=prev_blocks.begin(); j!=prev_blocks.end(); ++j)
1439 if(j->second->instance_name.empty() && j->second->struct_declaration)
1441 const map<string, VariableDeclaration *> &iface_vars = j->second->struct_declaration->members.variables;
1442 i = iface_vars.find(var.name);
1443 if(i!=iface_vars.end())
1445 generate_interface(*j->second);
1451 void InterfaceGenerator::visit(VariableDeclaration &var)
1454 generate_interface(var, "in", var.name);
1455 else if(var.interface=="out")
1457 /* For output variables in function scope, generate a global interface
1458 and replace the local declaration with an assignment. */
1459 VariableDeclaration *out_var = 0;
1460 if(function_scope && (out_var=generate_interface(var, "out", var.name)))
1462 out_var->source = var.source;
1463 out_var->line = var.line;
1464 nodes_to_remove.insert(&var);
1465 if(var.init_expression)
1467 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
1468 stmt.source = var.source;
1469 stmt.line = var.line;
1474 else if(var.interface=="in" && current_block==&stage->content)
1476 if(var.name.compare(0, 3, "gl_"))
1477 declared_inputs.push_back(&var);
1479 /* Try to link input variables in global scope with output variables from
1481 if(!var.linked_declaration && stage->previous)
1483 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
1484 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
1485 if(i!=prev_vars.end() && i->second->interface=="out")
1487 var.linked_declaration = i->second;
1488 i->second->linked_declaration = &var;
1493 TraversingVisitor::visit(var);
1496 void InterfaceGenerator::visit(InterfaceBlock &iface)
1498 if(iface.interface=="in")
1500 /* Try to link input blocks with output blocks sharing the same block
1501 name from previous stage. */
1502 if(!iface.linked_block && stage->previous)
1504 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
1505 map<string, InterfaceBlock *>::const_iterator i = prev_blocks.find("out"+iface.block_name);
1506 if(i!=prev_blocks.end())
1508 iface.linked_block = i->second;
1509 i->second->linked_block = &iface;
1514 TraversingVisitor::visit(iface);
1517 void InterfaceGenerator::visit(FunctionDeclaration &func)
1519 SetFlag set_scope(function_scope, true);
1520 // Skip parameters because they're not useful here
1521 func.body.visit(*this);
1524 void InterfaceGenerator::visit(Passthrough &pass)
1526 // Pass through all input variables declared so far.
1527 vector<VariableDeclaration *> pass_vars = declared_inputs;
1531 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
1532 for(map<string, VariableDeclaration *>::const_iterator i=prev_vars.begin(); i!=prev_vars.end(); ++i)
1534 if(i->second->interface!="out")
1537 /* Pass through output variables from the previous stage, but only
1538 those which are not already linked to an input here. */
1539 if(!i->second->linked_declaration && generate_interface(*i->second, "in", i->second->name))
1540 pass_vars.push_back(i->second);
1544 if(stage->type==Stage::GEOMETRY)
1546 /* Special case for geometry shader: copy gl_Position from input to
1548 InterfaceBlockReference *ref = new InterfaceBlockReference;
1549 ref->name = "gl_in";
1551 BinaryExpression *subscript = new BinaryExpression;
1552 subscript->left = ref;
1553 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
1554 subscript->right = pass.subscript;
1556 MemberAccess *memacc = new MemberAccess;
1557 memacc->left = subscript;
1558 memacc->member = "gl_Position";
1560 insert_assignment("gl_Position", memacc);
1563 for(vector<VariableDeclaration *>::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i)
1565 string out_name = change_prefix((*i)->name, out_prefix);
1566 generate_interface(**i, "out", out_name);
1568 VariableReference *ref = new VariableReference;
1569 ref->name = (*i)->name;
1572 BinaryExpression *subscript = new BinaryExpression;
1573 subscript->left = ref;
1574 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
1575 subscript->right = pass.subscript;
1576 insert_assignment(out_name, subscript);
1579 insert_assignment(out_name, ref);
1582 nodes_to_remove.insert(&pass);