[vala] Simplify for statements whose condition is false



commit c18da22b46b393fdcae604ca155ac7328eee871f
Author: Jürg Billeter <j bitron ch>
Date:   Mon Mar 22 20:01:14 2010 +0100

    Simplify for statements whose condition is false
    
    Fixes bug 601351.

 vala/valaforstatement.vala |   13 +++++++++++--
 1 files changed, 11 insertions(+), 2 deletions(-)
---
diff --git a/vala/valaforstatement.vala b/vala/valaforstatement.vala
index 99301fe..6e8be3d 100644
--- a/vala/valaforstatement.vala
+++ b/vala/valaforstatement.vala
@@ -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);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]