]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
vala: Add Expression.is_always_true/false() helpers
authorRico Tzschichholz <ricotz@ubuntu.com>
Sat, 17 Nov 2018 18:23:28 +0000 (19:23 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Wed, 25 Mar 2020 11:28:50 +0000 (12:28 +0100)
vala/valadostatement.vala
vala/valaexpression.vala
vala/valaflowanalyzer.vala
vala/valaforstatement.vala
vala/valawhilestatement.vala

index d994b5abdf0a8b67e4ccec880baaac321d2955b2..80df5849ea719c30b9c44f4d87aca6baa9283d1c 100644 (file)
@@ -81,11 +81,6 @@ public class Vala.DoStatement : CodeNode, Statement {
                visitor.visit_end_full_expression (condition);
        }
 
-       bool always_true (Expression condition) {
-               unowned BooleanLiteral? literal = condition as BooleanLiteral;
-               return (literal != null && literal.value);
-       }
-
        public override void replace_expression (Expression old_node, Expression new_node) {
                if (condition == old_node) {
                        condition = new_node;
@@ -102,7 +97,7 @@ public class Vala.DoStatement : CodeNode, Statement {
                // convert to simple loop
 
                // do not generate variable and if block if condition is always true
-               if (always_true (condition)) {
+               if (condition.is_always_true ()) {
                        var loop = new Loop (body, source_reference);
 
                        unowned Block parent_block = (Block) parent_node;
index 2b9170e427562ad8725bdeb6603ae696eeb73ead..a02cc1c1f6e7d1125d66e0f464cd3128169dab68 100644 (file)
@@ -86,6 +86,22 @@ public abstract class Vala.Expression : CodeNode {
                return true;
        }
 
+       /**
+        * Check whether this expression is always true.
+        */
+       public bool is_always_true () {
+               unowned BooleanLiteral? literal = this as BooleanLiteral;
+               return (literal != null && literal.value);
+       }
+
+       /**
+        * Check whether this expression is always false.
+        */
+       public bool is_always_false () {
+               unowned BooleanLiteral? literal = this as BooleanLiteral;
+               return (literal != null && !literal.value);
+       }
+
        public Statement? parent_statement {
                get {
                        unowned Expression? expr = parent_node as Expression;
index 03aae237f982cb18d7e370bc388ea5daf066d56b..ea0df7a83c150d7ce01201eb55d98d3604329e68 100644 (file)
@@ -599,16 +599,6 @@ public class Vala.FlowAnalyzer : CodeVisitor {
                }
        }
 
-       bool always_true (Expression condition) {
-               unowned BooleanLiteral? literal = condition as BooleanLiteral;
-               return (literal != null && literal.value);
-       }
-
-       bool always_false (Expression condition) {
-               unowned BooleanLiteral? literal = condition as BooleanLiteral;
-               return (literal != null && !literal.value);
-       }
-
        public override void visit_if_statement (IfStatement stmt) {
                if (unreachable (stmt)) {
                        return;
@@ -621,7 +611,7 @@ public class Vala.FlowAnalyzer : CodeVisitor {
 
                // true block
                var last_block = current_block;
-               if (always_false (stmt.condition)) {
+               if (stmt.condition.is_always_false ()) {
                        mark_unreachable ();
                } else {
                        current_block = new BasicBlock ();
@@ -632,7 +622,7 @@ public class Vala.FlowAnalyzer : CodeVisitor {
 
                // false block
                var last_true_block = current_block;
-               if (always_true (stmt.condition)) {
+               if (stmt.condition.is_always_true ()) {
                        mark_unreachable ();
                } else {
                        current_block = new BasicBlock ();
index e5276975ba2e86e10ae4a345db38f14c305d0787..dc721163ca10094ddac81868f6f67df1fd92c526 100644 (file)
@@ -154,16 +154,6 @@ public class Vala.ForStatement : CodeNode, Statement {
                }
        }
 
-       bool always_true (Expression condition) {
-               unowned BooleanLiteral? literal = condition as BooleanLiteral;
-               return (literal != null && literal.value);
-       }
-
-       bool always_false (Expression condition) {
-               unowned BooleanLiteral? literal = condition as BooleanLiteral;
-               return (literal != null && !literal.value);
-       }
-
        public override bool check (CodeContext context) {
                if (checked) {
                        return !error;
@@ -181,8 +171,8 @@ public class Vala.ForStatement : CodeNode, Statement {
                }
 
                // do not generate if block if condition is always true
-               if (condition == null || always_true (condition)) {
-               } else if (always_false (condition)) {
+               if (condition == null || condition.is_always_true ()) {
+               } else if (condition.is_always_false ()) {
                        // do not generate if block if condition is always false
                        body.insert_statement (0, new BreakStatement (condition.source_reference));
                } else {
index 9c333960aa45af9ef20188f95c3ee2ef7405d850..16125221969de7c151a8f024fcd8824eda3cb7af 100644 (file)
@@ -81,16 +81,6 @@ public class Vala.WhileStatement : CodeNode, Statement {
                body.accept (visitor);
        }
 
-       bool always_true (Expression condition) {
-               unowned BooleanLiteral? literal = condition as BooleanLiteral;
-               return (literal != null && literal.value);
-       }
-
-       bool always_false (Expression condition) {
-               unowned BooleanLiteral? literal = condition as BooleanLiteral;
-               return (literal != null && !literal.value);
-       }
-
        public override void replace_expression (Expression old_node, Expression new_node) {
                if (condition == old_node) {
                        condition = new_node;
@@ -106,9 +96,9 @@ public class Vala.WhileStatement : CodeNode, Statement {
 
                // convert to simple loop
 
-               if (always_true (condition)) {
+               if (condition.is_always_true ()) {
                        // do not generate if block if condition is always true
-               } else if (always_false (condition)) {
+               } else if (condition.is_always_false ()) {
                        // do not generate if block if condition is always false
                        body.insert_statement (0, new BreakStatement (condition.source_reference));
                } else {