+ /* 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(RefPtr<Expression> &expr)
+{
+ r_ref_info = 0;
+ expr->visit(*this);
+ if(r_ref_info && r_ref_info->expression && r_ref_info->available)
+ {
+ 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;
+ }
+
+ if(r_ref_info->trivial)
+ inline_expression(*r_ref_info->expression, expr);
+ 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 = &expr;
+ }
+ r_oper = expr->oper;
+ r_ref_info = 0;
+}
+
+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;
+ }
+ }