[vala/wip/transform: 700/793] Move WhileStatement transformation into the code transformer



commit 63ecf9bf4049ad7d5513e1fbecc40331a1ac8df1
Author: Luca Bruno <lucabru src gnome org>
Date:   Tue Aug 30 12:43:41 2011 +0200

    Move WhileStatement transformation into the code transformer

 vala/valacodetransformer.vala |   33 ++++++++++++++++++++++++-
 vala/valaflowanalyzer.vala    |   48 +++++++++++++++++++++++++++++++++++++
 vala/valawhilestatement.vala  |   53 +++++++++++++++++++++-------------------
 3 files changed, 108 insertions(+), 26 deletions(-)
---
diff --git a/vala/valacodetransformer.vala b/vala/valacodetransformer.vala
index 83a1262..aa0c028 100644
--- a/vala/valacodetransformer.vala
+++ b/vala/valacodetransformer.vala
@@ -167,8 +167,39 @@ public class Vala.CodeTransformer : CodeVisitor {
                label.accept_children (this);
        }
 
+       bool always_true (Expression condition) {
+               var literal = condition as BooleanLiteral;
+               return (literal != null && literal.value);
+       }
+
+       bool always_false (Expression condition) {
+               var literal = condition as BooleanLiteral;
+               return (literal != null && !literal.value);
+       }
+
        public override void visit_while_statement (WhileStatement stmt) {
-               stmt.accept_children (this);
+               // convert to simple loop
+
+               if (always_true (stmt.condition)) {
+                       // do not generate if block if condition is always true
+               } else if (always_false (stmt.condition)) {
+                       // do not generate if block if condition is always false
+                       stmt.body.insert_statement (0, new BreakStatement (stmt.condition.source_reference));
+               } else {
+                       var if_condition = new UnaryExpression (UnaryOperator.LOGICAL_NEGATION, 
stmt.condition, stmt.condition.source_reference);
+                       var true_block = new Block (stmt.condition.source_reference);
+                       true_block.add_statement (new BreakStatement (stmt.condition.source_reference));
+                       var if_stmt = new IfStatement (if_condition, true_block, null, 
stmt.condition.source_reference);
+                       stmt.body.insert_statement (0, if_stmt);
+               }
+
+               var loop = new Loop (stmt.body, stmt.source_reference);
+
+               var parent_block = (Block) stmt.parent_node;
+               parent_block.replace_statement (stmt, loop);
+
+               stmt.body.checked = false;
+               check (loop);
        }
 
        public override void visit_do_statement (DoStatement stmt) {
diff --git a/vala/valaflowanalyzer.vala b/vala/valaflowanalyzer.vala
index be733e1..0ed375e 100644
--- a/vala/valaflowanalyzer.vala
+++ b/vala/valaflowanalyzer.vala
@@ -728,6 +728,54 @@ public class Vala.FlowAnalyzer : CodeVisitor {
                jump_stack.remove_at (jump_stack.size - 1);
        }
 
+       public override void visit_while_statement (WhileStatement stmt) {
+               if (unreachable (stmt)) {
+                       return;
+               }
+
+               var loop_block = new BasicBlock ();
+               jump_stack.add (new JumpTarget.continue_target (loop_block));
+               var after_loop_block = new BasicBlock ();
+               jump_stack.add (new JumpTarget.break_target (after_loop_block));
+
+               current_block.connect (loop_block);
+               current_block = loop_block;
+
+               // condition
+               loop_block.add_node (stmt.condition);
+               handle_errors (stmt.condition);
+
+               if (always_false (stmt.condition)) {
+                       mark_unreachable ();
+               } else {
+                       current_block = new BasicBlock ();
+                       loop_block.connect (current_block);
+               }
+
+               if (!always_true (stmt.condition)) {
+                       loop_block.connect (after_loop_block);
+               }
+
+               // loop block
+               stmt.body.accept (this);
+               // end of loop block reachable?
+               if (current_block != null) {
+                       current_block.connect (loop_block);
+               }
+
+               // after loop block reachable?
+               if (after_loop_block.get_predecessors ().size == 0) {
+                       // after loop block not reachable
+                       mark_unreachable ();
+               } else {
+                       // after loop block reachable
+                       current_block = after_loop_block;
+               }
+
+               jump_stack.remove_at (jump_stack.size - 1);
+               jump_stack.remove_at (jump_stack.size - 1);
+       }
+
        public override void visit_foreach_statement (ForeachStatement stmt) {
                if (unreachable (stmt)) {
                        return;
diff --git a/vala/valawhilestatement.vala b/vala/valawhilestatement.vala
index 6a1bcde..1fa17fd 100644
--- a/vala/valawhilestatement.vala
+++ b/vala/valawhilestatement.vala
@@ -81,38 +81,41 @@ public class Vala.WhileStatement : CodeNode, Statement {
                body.accept (visitor);
        }
 
-       bool always_true (Expression condition) {
-               var literal = condition as BooleanLiteral;
-               return (literal != null && literal.value);
-       }
-
-       bool always_false (Expression condition) {
-               var 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;
+               }
        }
 
        public override bool check (CodeContext context) {
-               // convert to simple loop
-
-               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));
-                       var if_stmt = new IfStatement (if_condition, true_block, null, 
condition.source_reference);
-                       body.insert_statement (0, if_stmt);
+               if (checked) {
+                       return !error;
+               }
+
+               checked = true;
+
+               condition.target_type = context.analyzer.bool_type.copy ();
+
+               condition.check (context);
+
+               if (condition.error) {
+                       /* if there was an error in the condition, skip this check */
+                       error = true;
+                       return false;
                }
 
-               var loop = new Loop (body, source_reference);
+               if (condition.value_type == null || !condition.value_type.compatible 
(context.analyzer.bool_type)) {
+                       error = true;
+                       Report.error (condition.source_reference, "Condition must be boolean");
+                       return false;
+               }
 
-               var parent_block = (Block) parent_node;
-               parent_block.replace_statement (this, loop);
+               if (!body.check (context)) {
+                       error = true;
+                       return false;
+               }
 
-               return loop.check (context);
+               return !error;
        }
 }
 


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