]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
Simplify for statements whose condition is false
authorJürg Billeter <j@bitron.ch>
Mon, 22 Mar 2010 19:01:14 +0000 (20:01 +0100)
committerJürg Billeter <j@bitron.ch>
Mon, 22 Mar 2010 19:01:14 +0000 (20:01 +0100)
Fixes bug 601351.

vala/valaforstatement.vala

index 99301fe9f08db526b8c7f45ef37fdf90bf5b4c82..6e8be3d908a92cb3f2970e3981a1014202232b7f 100644 (file)
@@ -1,6 +1,6 @@
 /* valaforstatement.vala
  *
- * Copyright (C) 2006-2009  Jürg Billeter
+ * Copyright (C) 2006-2010  Jürg Billeter
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -141,6 +141,11 @@ public class Vala.ForStatement : CodeNode, Statement {
                return (literal != null && literal.value);
        }
 
+       bool always_false (Expression condition) {
+               var literal = condition as BooleanLiteral;
+               return (literal != null && !literal.value);
+       }
+
        public override bool check (SemanticAnalyzer analyzer) {
                // convert to simple loop
 
@@ -152,7 +157,11 @@ public class Vala.ForStatement : CodeNode, Statement {
                }
 
                // do not generate if block if condition is always true
-               if (condition != null && !always_true (condition)) {
+               if (condition == null || always_true (condition)) {
+               } else if (always_false (condition)) {
+                       // do not generate if block if condition is always false
+                       body.insert_statement (0, new BreakStatement (condition.source_reference));
+               } else {
                        // condition
                        var if_condition = new UnaryExpression (UnaryOperator.LOGICAL_NEGATION, condition, condition.source_reference);
                        var true_block = new Block (condition.source_reference);