+ set_result(literal.value, true);
+}
+
+void ConstantFolder::visit(VariableReference &var)
+{
+ /* If an iteration variable is initialized with a constant value, return
+ that value here for the purpose of evaluating the loop condition for the
+ first iteration. */
+ if(var.declaration==iteration_var)
+ {
+ set_result(iter_init_value);
+ r_uses_iter_var = true;
+ }
+}
+
+void ConstantFolder::visit(MemberAccess &memacc)
+{
+ TraversingVisitor::visit(memacc);
+ r_constant = false;
+}
+
+void ConstantFolder::visit(Swizzle &swizzle)
+{
+ TraversingVisitor::visit(swizzle);
+ r_constant = false;
+}
+
+void ConstantFolder::visit(UnaryExpression &unary)
+{
+ TraversingVisitor::visit(unary);
+ bool can_fold = r_constant;
+ r_constant = false;
+ if(!can_fold)
+ return;
+
+ BasicTypeDeclaration::Kind kind = get_value_kind(r_constant_value);
+
+ char oper = unary.oper->token[0];
+ char oper2 = unary.oper->token[1];
+ if(oper=='!')
+ {
+ if(kind==BasicTypeDeclaration::BOOL)
+ set_result(!r_constant_value.value<bool>());
+ }
+ else if(oper=='~')
+ {
+ if(kind==BasicTypeDeclaration::INT)
+ set_result(~r_constant_value.value<int>());
+ }
+ else if(oper=='-' && !oper2)
+ {
+ if(kind==BasicTypeDeclaration::INT)
+ set_result(-r_constant_value.value<int>());
+ else if(kind==BasicTypeDeclaration::FLOAT)
+ set_result(-r_constant_value.value<float>());
+ }
+}
+
+void ConstantFolder::visit(BinaryExpression &binary)
+{
+ visit(binary.left);
+ bool left_constant = r_constant;
+ bool left_iter_var = r_uses_iter_var;
+ Variant left_value = r_constant_value;
+ visit(binary.right);
+ if(left_iter_var)
+ r_uses_iter_var = true;
+
+ bool can_fold = (left_constant && r_constant);
+ r_constant = false;
+ if(!can_fold)
+ return;
+
+ BasicTypeDeclaration::Kind left_kind = get_value_kind(left_value);
+ BasicTypeDeclaration::Kind right_kind = get_value_kind(r_constant_value);
+ // Currently only expressions with both sides of equal types are handled.
+ if(left_kind!=right_kind)
+ return;
+
+ char oper = binary.oper->token[0];
+ char oper2 = binary.oper->token[1];
+ if(oper=='&' || oper=='|' || oper=='^')
+ {
+ if(oper2==oper && left_kind==BasicTypeDeclaration::BOOL)
+ set_result(evaluate_logical(oper, left_value.value<bool>(), r_constant_value.value<bool>()));
+ else if(!oper2 && left_kind==BasicTypeDeclaration::INT)
+ set_result(evaluate_logical(oper, left_value.value<int>(), r_constant_value.value<int>()));
+ }
+ else if((oper=='<' || oper=='>') && oper2!=oper)
+ {
+ if(left_kind==BasicTypeDeclaration::INT)
+ set_result(evaluate_relation(binary.oper->token, left_value.value<int>(), r_constant_value.value<int>()));
+ else if(left_kind==BasicTypeDeclaration::FLOAT)
+ set_result(evaluate_relation(binary.oper->token, left_value.value<float>(), r_constant_value.value<float>()));
+ }
+ else if((oper=='=' || oper=='!') && oper2=='=')
+ {
+ if(left_kind==BasicTypeDeclaration::INT)
+ set_result((left_value.value<int>()==r_constant_value.value<int>()) == (oper=='='));
+ if(left_kind==BasicTypeDeclaration::FLOAT)
+ set_result((left_value.value<float>()==r_constant_value.value<float>()) == (oper=='='));
+ }
+ else if(oper=='+' || oper=='-' || oper=='*' || oper=='/')
+ {
+ if(left_kind==BasicTypeDeclaration::INT)
+ set_result(evaluate_arithmetic(oper, left_value.value<int>(), r_constant_value.value<int>()));
+ else if(left_kind==BasicTypeDeclaration::FLOAT)
+ set_result(evaluate_arithmetic(oper, left_value.value<float>(), r_constant_value.value<float>()));
+ }
+ else if(oper=='%' || ((oper=='<' || oper=='>') && oper2==oper))
+ {
+ if(left_kind!=BasicTypeDeclaration::INT)
+ return;
+
+ if(oper=='%')
+ set_result(left_value.value<int>()%r_constant_value.value<int>());
+ else if(oper=='<')
+ set_result(left_value.value<int>()<<r_constant_value.value<int>());
+ else if(oper=='>')
+ set_result(left_value.value<int>()>>r_constant_value.value<int>());
+ }
+}
+
+void ConstantFolder::visit(Assignment &assign)
+{
+ TraversingVisitor::visit(assign);
+ r_constant = false;
+}
+
+void ConstantFolder::visit(TernaryExpression &ternary)
+{
+ TraversingVisitor::visit(ternary);
+ r_constant = false;
+}
+
+void ConstantFolder::visit(FunctionCall &call)
+{
+ TraversingVisitor::visit(call);
+ r_constant = false;
+}
+
+void ConstantFolder::visit(VariableDeclaration &var)
+{
+ if(iteration_init && var.init_expression)
+ {
+ visit(var.init_expression);
+ if(r_constant)
+ {
+ /* Record the value of a constant initialization expression of an
+ iteration, so it can be used to evaluate the loop condition. */
+ iteration_var = &var;
+ iter_init_value = r_constant_value;
+ }
+ }
+ else
+ TraversingVisitor::visit(var);
+}
+
+void ConstantFolder::visit(Iteration &iter)
+{
+ SetForScope<Block *> set_block(current_block, &iter.body);
+
+ /* The iteration variable is not normally inlined into expressions, so we
+ process it specially here. If the initial value causes the loop condition
+ to evaluate to false, then the expression can be folded. */
+ iteration_var = 0;
+ if(iter.init_statement)
+ {
+ SetFlag set_init(iteration_init);
+ iter.init_statement->visit(*this);
+ }
+
+ if(iter.condition)
+ {
+ visit(iter.condition);
+ if(r_constant && r_constant_value.check_type<bool>() && !r_constant_value.value<bool>())
+ {
+ RefPtr<Literal> literal = new Literal;
+ literal->token = "false";
+ literal->value = r_constant_value;
+ iter.condition = literal;
+ }
+ }
+ iteration_var = 0;
+
+ iter.body.visit(*this);
+ if(iter.loop_expression)
+ visit(iter.loop_expression);