+ 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;
+ }
+ }
+}
+
+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<VariableDeclaration *, ExpressionInfo>::iterator i=expressions.begin(); i!=expressions.end(); )
+ {
+ map<string, VariableDeclaration *>::iterator j = block.variables.find(i->first->name);
+ if(j!=block.variables.end() && j->second==i->first)
+ {
+ if(i->second.expression && i->second.inline_point)
+ inline_expression(*i->second.expression, *i->second.inline_point, i->second.outer_oper, i->second.inner_oper, i->second.inline_on_rhs);
+
+ expressions.erase(i++);
+ }
+ else
+ {
+ /* The expression was assigned in this block and may depend on local
+ variables of the block. If this is a conditionally executed block,
+ the assignment might not always happen. Mark the expression as not
+ available to any outer blocks. */
+ if(i->second.assign_scope==&block)
+ i->second.available = false;
+
+ ++i;
+ }