summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRico Tzschichholz <ricotz@ubuntu.com>2018-11-17 19:23:28 +0100
committerRico Tzschichholz <ricotz@ubuntu.com>2020-03-25 12:28:50 +0100
commit4d8fc65d54c27b95ec1ceeb5d3c79f44a6affe3a (patch)
tree13745b37a76d3fae5d234aeeefb8a5edc86384bd
parent69bb186dc5b1962be317bf5ee88077c7fac35214 (diff)
downloadvala-4d8fc65d54c27b95ec1ceeb5d3c79f44a6affe3a.tar.gz
vala: Add Expression.is_always_true/false() helpers
-rw-r--r--vala/valadostatement.vala7
-rw-r--r--vala/valaexpression.vala16
-rw-r--r--vala/valaflowanalyzer.vala14
-rw-r--r--vala/valaforstatement.vala14
-rw-r--r--vala/valawhilestatement.vala14
5 files changed, 23 insertions, 42 deletions
diff --git a/vala/valadostatement.vala b/vala/valadostatement.vala
index d994b5abd..80df5849e 100644
--- a/vala/valadostatement.vala
+++ b/vala/valadostatement.vala
@@ -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;
diff --git a/vala/valaexpression.vala b/vala/valaexpression.vala
index 2b9170e42..a02cc1c1f 100644
--- a/vala/valaexpression.vala
+++ b/vala/valaexpression.vala
@@ -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;
diff --git a/vala/valaflowanalyzer.vala b/vala/valaflowanalyzer.vala
index 03aae237f..ea0df7a83 100644
--- a/vala/valaflowanalyzer.vala
+++ b/vala/valaflowanalyzer.vala
@@ -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 ();
diff --git a/vala/valaforstatement.vala b/vala/valaforstatement.vala
index e5276975b..dc721163c 100644
--- a/vala/valaforstatement.vala
+++ b/vala/valaforstatement.vala
@@ -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 {
diff --git a/vala/valawhilestatement.vala b/vala/valawhilestatement.vala
index 9c333960a..161252219 100644
--- a/vala/valawhilestatement.vala
+++ b/vala/valawhilestatement.vala
@@ -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 {