]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
Simplify while statements whose condition is false
authorJürg Billeter <j@bitron.ch>
Mon, 22 Mar 2010 10:35:51 +0000 (11:35 +0100)
committerJürg Billeter <j@bitron.ch>
Mon, 22 Mar 2010 10:37:32 +0000 (11:37 +0100)
Fixes bug 601339.

vala/valawhilestatement.vala

index 71f5405943fabe1606f868841ed6430bbef51133..8df427d84bf697725cfd59a7169dd2ee345382f4 100644 (file)
@@ -1,6 +1,6 @@
 /* valawhilestatement.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
@@ -86,11 +86,20 @@ public class Vala.WhileStatement : 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
 
-               // do not generate if block if condition is always true
-               if (!always_true (condition)) {
+               if (always_true (condition)) {
+                       // do not generate if block if condition is always true
+               } 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 {
                        var if_condition = new UnaryExpression (UnaryOperator.LOGICAL_NEGATION, condition, condition.source_reference);
                        var true_block = new Block (condition.source_reference);
                        true_block.add_statement (new BreakStatement (condition.source_reference));