+ if(iteration_body && !r_ref_info->trivial)
+ {
+ /* Don't inline non-trivial expressions which were assigned outside
+ an iteration statement. The iteration may run multiple times, which
+ would cause the expression to also be evaluated multiple times. */
+ Block *i = r_ref_info->assign_scope;
+ for(; (i && i!=iteration_body); i=i->parent) ;
+ if(!i)
+ return;
+ }
+
+ r_ref_info->outer_oper = outer_oper;
+ if(r_ref_info->trivial)
+ inline_expression(*r_ref_info->expression, ptr, outer_oper, r_ref_info->inner_oper, on_rhs);
+ else
+ {
+ /* Record the inline point for a non-trivial expression but don't
+ inline it yet. It might turn out it shouldn't be inlined after all. */
+ r_ref_info->inline_point = &ptr;
+ r_ref_info->inline_on_rhs = on_rhs;
+ }
+ }
+ r_ref_info = 0;
+}
+
+void ExpressionInliner::inline_expression(Expression &expr, RefPtr<Expression> &ptr, const Operator *outer_oper, const Operator *inner_oper, bool on_rhs)
+{
+ unsigned outer_precedence = (outer_oper ? outer_oper->precedence : 20);
+ unsigned inner_precedence = (inner_oper ? inner_oper->precedence : 0);
+
+ bool needs_parentheses = (inner_precedence>=outer_precedence);
+ if(inner_oper && inner_oper==outer_oper)
+ // Omit parentheses if the operator's natural grouping works out.
+ needs_parentheses = (inner_oper->assoc!=Operator::ASSOCIATIVE && on_rhs!=(inner_oper->assoc==Operator::RIGHT_TO_LEFT));
+
+ if(needs_parentheses)
+ {
+ RefPtr<ParenthesizedExpression> parexpr = new ParenthesizedExpression;
+ parexpr->expression = expr.clone();
+ ptr = parexpr;
+ }
+ else
+ ptr = expr.clone();
+
+ r_any_inlined = true;
+}
+
+void ExpressionInliner::visit(Block &block)
+{
+ TraversingVisitor::visit(block);
+
+ for(map<string, VariableDeclaration *>::iterator i=block.variables.begin(); i!=block.variables.end(); ++i)
+ {
+ map<Assignment::Target, ExpressionInfo>::iterator j = expressions.lower_bound(i->second);
+ for(; (j!=expressions.end() && j->first.declaration==i->second); )
+ {
+ if(j->second.expression && j->second.inline_point)
+ inline_expression(*j->second.expression, *j->second.inline_point, j->second.outer_oper, j->second.inner_oper, j->second.inline_on_rhs);
+
+ expressions.erase(j++);
+ }
+ }
+
+ /* Expressions assigned in this block may depend on local variables of the
+ block. If this is a conditionally executed block, the assignments might not
+ always happen. Mark the expressions as not available to any outer blocks. */
+ for(map<Assignment::Target, ExpressionInfo>::iterator i=expressions.begin(); i!=expressions.end(); ++i)
+ if(i->second.assign_scope==&block)
+ i->second.available = false;
+}
+
+void ExpressionInliner::visit(VariableReference &var)
+{
+ if(var.declaration)
+ {
+ map<Assignment::Target, ExpressionInfo>::iterator i = expressions.find(var.declaration);
+ if(i!=expressions.end())
+ {
+ /* If a non-trivial expression is referenced multiple times, don't
+ inline it. */
+ if(i->second.inline_point && !i->second.trivial)
+ i->second.expression = 0;
+ /* Mutating expressions are analogous to self-referencing assignments
+ and prevent inlining. */
+ if(mutating)
+ i->second.expression = 0;
+ r_ref_info = &i->second;
+ }
+ }
+}
+
+void ExpressionInliner::visit(MemberAccess &memacc)
+{
+ visit_and_record(memacc.left, memacc.oper, false);
+ r_oper = memacc.oper;
+ r_trivial = false;
+}
+
+void ExpressionInliner::visit(Swizzle &swizzle)
+{
+ visit_and_record(swizzle.left, swizzle.oper, false);
+ r_oper = swizzle.oper;
+ r_trivial = false;
+}
+
+void ExpressionInliner::visit(UnaryExpression &unary)
+{
+ SetFlag set_target(mutating, mutating || unary.oper->token[1]=='+' || unary.oper->token[1]=='-');
+ visit_and_record(unary.expression, unary.oper, false);
+ r_oper = unary.oper;
+ r_trivial = false;
+}
+
+void ExpressionInliner::visit(BinaryExpression &binary)
+{
+ visit_and_record(binary.left, binary.oper, false);
+ {
+ SetFlag clear_target(mutating, false);
+ visit_and_record(binary.right, binary.oper, true);
+ }
+ r_oper = binary.oper;
+ r_trivial = false;
+}
+
+void ExpressionInliner::visit(Assignment &assign)
+{
+ {
+ SetFlag set_target(mutating);
+ visit_and_record(assign.left, assign.oper, false);
+ }
+ r_oper = 0;
+ visit_and_record(assign.right, assign.oper, true);
+
+ map<Assignment::Target, ExpressionInfo>::iterator i = expressions.find(assign.target);
+ if(i!=expressions.end())
+ {
+ /* Self-referencing assignments can't be inlined without additional
+ work. Just clear any previous expression. */
+ i->second.expression = (assign.self_referencing ? 0 : assign.right.get());
+ i->second.assign_scope = current_block;
+ i->second.inline_point = 0;
+ i->second.inner_oper = r_oper;
+ i->second.available = true;
+ }
+
+ r_oper = assign.oper;
+ r_trivial = false;
+}
+
+void ExpressionInliner::visit(FunctionCall &call)
+{
+ for(NodeArray<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
+ visit_and_record(*i, 0, false);
+ r_oper = 0;
+ r_trivial = false;
+}
+
+void ExpressionInliner::visit(VariableDeclaration &var)
+{
+ r_oper = 0;
+ r_trivial = true;
+ if(var.init_expression)
+ visit_and_record(var.init_expression, 0, false);
+
+ bool constant = var.constant;
+ if(constant && var.layout)
+ {
+ for(vector<Layout::Qualifier>::const_iterator i=var.layout->qualifiers.begin(); (constant && i!=var.layout->qualifiers.end()); ++i)
+ constant = (i->name!="constant_id");