+ string result_name = InlineContentInjector().apply(*stage, *current_function, *current_block, insert_point, call);
+
+ // This will later get removed by UnusedVariableRemover.
+ if(result_name.empty())
+ result_name = "_msp_unused_from_inline";
+
+ RefPtr<VariableReference> ref = new VariableReference;
+ ref->name = result_name;
+ r_inline_result = ref;
+
+ /* Inlined variables need to be resolved before this function can be
+ inlined further. */
+ inlineable.erase(current_function);
+ r_inlined_here = true;
+ }
+}
+
+void FunctionInliner::visit(FunctionDeclaration &func)
+{
+ SetForScope<FunctionDeclaration *> set_func(current_function, &func);
+ TraversingVisitor::visit(func);
+ r_inlined_here = false;
+}
+
+void FunctionInliner::visit(Iteration &iter)
+{
+ /* Visit the initialization statement before entering the loop body so the
+ inlined statements get inserted outside. */
+ if(iter.init_statement)
+ iter.init_statement->visit(*this);
+
+ SetForScope<Block *> set_block(current_block, &iter.body);
+ /* Skip the condition and loop expression parts because they're not properly
+ inside the body block. Inlining anything into them will require a more
+ comprehensive transformation. */
+ iter.body.visit(*this);
+}
+
+
+ExpressionInliner::ExpressionInliner():
+ r_ref_info(0),
+ r_trivial(false),
+ access_read(true),
+ access_write(false),
+ iteration_init(false),
+ iteration_body(0),
+ r_oper(0)
+{ }
+
+bool ExpressionInliner::apply(Stage &s)
+{
+ s.content.visit(*this);
+
+ bool any_inlined = false;
+ for(ExpressionInfo &e: expressions)
+ if(e.expression && (e.trivial || e.uses.size()==1))
+ {
+ for(ExpressionUse &u: e.uses)
+ if(!u.blocked)
+ {
+ *u.reference = e.expression->clone();
+ any_inlined = true;
+ }
+ }
+
+ return any_inlined;
+}
+
+void ExpressionInliner::visit(RefPtr<Expression> &expr)
+{
+ r_ref_info = 0;
+ expr->visit(*this);
+ if(r_ref_info && r_ref_info->expression)
+ {
+ ExpressionUse use;
+ use.reference = &expr;
+ use.ref_scope = current_block;
+ use.blocked = access_write;
+
+ if(iteration_body && !r_ref_info->trivial)
+ {
+ /* Block inlining of non-trivial expressions assigned outside an
+ iteration statement. The iteration may run multiple times, which
+ would cause the expression to also be evaluated multiple times. */
+ for(Block *i=iteration_body->parent; (!use.blocked && i); i=i->parent)
+ use.blocked = (i==r_ref_info->assign_scope);
+ }
+
+ /* Block inlining assignments from from inner scopes. The assignment may
+ depend on local variables of that scope or may not always be executed. */
+ for(Block *i=r_ref_info->assign_scope->parent; (!use.blocked && i); i=i->parent)
+ use.blocked = (i==current_block);
+
+ r_ref_info->uses.push_back(use);
+ }
+ r_oper = expr->oper;
+ r_ref_info = 0;
+}
+
+void ExpressionInliner::visit(VariableReference &var)
+{
+ if(var.declaration && access_read)
+ {
+ auto i = assignments.find(var.declaration);
+ if(i!=assignments.end())
+ r_ref_info = i->second;
+ }
+}
+
+void ExpressionInliner::visit(MemberAccess &memacc)
+{
+ visit(memacc.left);
+ r_trivial = false;
+}
+
+void ExpressionInliner::visit(Swizzle &swizzle)
+{
+ visit(swizzle.left);
+ r_trivial = false;
+}
+
+void ExpressionInliner::visit(UnaryExpression &unary)
+{
+ SetFlag set_write(access_write, access_write || unary.oper->token[1]=='+' || unary.oper->token[1]=='-');
+ visit(unary.expression);
+ r_trivial = false;
+}
+
+void ExpressionInliner::visit(BinaryExpression &binary)
+{
+ visit(binary.left);
+ {
+ SetFlag clear_write(access_write, false);
+ visit(binary.right);
+ }
+ r_trivial = false;
+}
+
+void ExpressionInliner::visit(Assignment &assign)
+{
+ {
+ SetFlag set_read(access_read, assign.oper->token[0]!='=');
+ SetFlag set_write(access_write);
+ visit(assign.left);
+ }
+ r_oper = 0;
+ r_trivial = true;
+ visit(assign.right);
+
+ auto i = assignments.find(assign.target);
+ if(i!=assignments.end())
+ {
+ if(iteration_body && i->second->expression)
+ {
+ /* Block inlining into previous references within the iteration
+ statement. On iterations after the first they would refer to the
+ assignment within the iteration. */
+ for(ExpressionUse &u: i->second->uses)
+ for(Block *k=u.ref_scope; (!u.blocked && k); k=k->parent)
+ u.blocked = (k==iteration_body);
+ }
+
+ expressions.push_back(ExpressionInfo());
+ ExpressionInfo &info = expressions.back();
+ info.target = assign.target;
+ // Self-referencing assignments can't be inlined without additional work.
+ if(!assign.self_referencing)
+ info.expression = assign.right;
+ info.assign_scope = current_block;
+ info.trivial = r_trivial;
+
+ i->second = &info;
+ }
+
+ r_trivial = false;
+}
+
+void ExpressionInliner::visit(TernaryExpression &ternary)
+{
+ visit(ternary.condition);
+ visit(ternary.true_expr);
+ visit(ternary.false_expr);
+ r_trivial = false;
+}
+
+void ExpressionInliner::visit(FunctionCall &call)
+{
+ TraversingVisitor::visit(call);
+ r_trivial = false;
+}
+
+void ExpressionInliner::visit(VariableDeclaration &var)
+{
+ r_oper = 0;
+ r_trivial = true;
+ TraversingVisitor::visit(var);
+
+ bool constant = var.constant;
+ if(constant && var.layout)
+ {
+ constant = !any_of(var.layout->qualifiers.begin(), var.layout->qualifiers.end(),
+ [](const Layout::Qualifier &q){ return q.name=="constant_id"; });
+ }
+
+ /* Only inline global variables if they're constant and have trivial
+ initializers. Non-constant variables could change in ways which are hard to
+ analyze and non-trivial expressions could be expensive to inline. */
+ if((current_block->parent || (constant && r_trivial)) && var.interface.empty())
+ {
+ expressions.push_back(ExpressionInfo());
+ ExpressionInfo &info = expressions.back();
+ info.target = &var;
+ /* Assume variables declared in an iteration initialization statement
+ will have their values change throughout the iteration. */
+ if(!iteration_init)
+ info.expression = var.init_expression;
+ info.assign_scope = current_block;
+ info.trivial = r_trivial;
+
+ assignments[&var] = &info;
+ }
+}
+
+void ExpressionInliner::visit(Iteration &iter)
+{
+ SetForScope<Block *> set_block(current_block, &iter.body);
+ if(iter.init_statement)
+ {
+ SetFlag set_init(iteration_init);
+ iter.init_statement->visit(*this);
+ }
+
+ SetForScope<Block *> set_body(iteration_body, &iter.body);
+ if(iter.condition)
+ visit(iter.condition);
+ iter.body.visit(*this);
+ if(iter.loop_expression)
+ visit(iter.loop_expression);
+}
+
+
+template<typename T>
+T ConstantFolder::evaluate_logical(char oper, T left, T right)
+{
+ switch(oper)
+ {
+ case '&': return left&right;
+ case '|': return left|right;
+ case '^': return left^right;
+ default: return T();
+ }
+}
+
+template<typename T>
+bool ConstantFolder::evaluate_relation(const char *oper, T left, T right)
+{
+ switch(oper[0]|oper[1])
+ {
+ case '<': return left<right;
+ case '<'|'=': return left<=right;
+ case '>': return left>right;
+ case '>'|'=': return left>=right;
+ default: return false;
+ }
+}
+
+template<typename T>
+T ConstantFolder::evaluate_arithmetic(char oper, T left, T right)
+{
+ switch(oper)
+ {
+ case '+': return left+right;
+ case '-': return left-right;
+ case '*': return left*right;
+ case '/': return left/right;
+ default: return T();
+ }
+}
+
+template<typename T>
+T ConstantFolder::evaluate_int_special_op(char oper, T left, T right)
+{
+ switch(oper)
+ {
+ case '%': return left%right;
+ case '<': return left<<right;
+ case '>': return left>>right;
+ default: return T();
+ }
+}
+
+template<typename T>
+void ConstantFolder::convert_to_result(const Variant &value)
+{
+ if(value.check_type<bool>())
+ set_result(static_cast<T>(value.value<bool>()));
+ else if(value.check_type<int>())
+ set_result(static_cast<T>(value.value<int>()));
+ else if(value.check_type<unsigned>())
+ set_result(static_cast<T>(value.value<unsigned>()));
+ else if(value.check_type<float>())
+ set_result(static_cast<T>(value.value<float>()));
+}
+
+void ConstantFolder::set_result(const Variant &value, bool literal)
+{
+ r_constant_value = value;
+ r_constant = true;
+ r_literal = literal;
+}
+
+void ConstantFolder::visit(RefPtr<Expression> &expr)
+{
+ r_constant_value = Variant();
+ r_constant = false;
+ r_literal = false;
+ r_uses_iter_var = false;
+ expr->visit(*this);
+ /* Don't replace literals since they'd only be replaced with an identical
+ literal. Also skip anything that uses an iteration variable, but pass on
+ the result so the Iteration visiting function can handle it. */
+ if(!r_constant || r_literal || r_uses_iter_var)
+ return;
+
+ RefPtr<Literal> literal = new Literal;
+ if(r_constant_value.check_type<bool>())
+ literal->token = (r_constant_value.value<bool>() ? "true" : "false");
+ else if(r_constant_value.check_type<int>())
+ literal->token = lexical_cast<string>(r_constant_value.value<int>());
+ else if(r_constant_value.check_type<unsigned>())
+ literal->token = lexical_cast<string>(r_constant_value.value<unsigned>())+"u";
+ else if(r_constant_value.check_type<float>())
+ {
+ literal->token = lexical_cast<string>(r_constant_value.value<float>(), Fmt().precision(8));
+ if(literal->token.find('.')==string::npos && literal->token.find('e')==string::npos)
+ literal->token += ".0";