Release 0.41.92
[vala-gnome.git] / vala / valawhilestatement.vala
blob9c4e65e6c0a04c3335d6af415b645b32c4f0a4be
1 /* valawhilestatement.vala
3 * Copyright (C) 2006-2010 Jürg Billeter
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 * Author:
20 * Jürg Billeter <j@bitron.ch>
23 using GLib;
25 /**
26 * Represents a while iteration statement in the source code.
28 public class Vala.WhileStatement : CodeNode, Statement {
29 /**
30 * Specifies the loop condition.
32 public Expression condition {
33 get {
34 return _condition;
36 set {
37 _condition = value;
38 _condition.parent_node = this;
42 /**
43 * Specifies the loop body.
45 public Block body {
46 get {
47 return _body;
49 set {
50 _body = value;
51 _body.parent_node = this;
55 private Expression _condition;
56 private Block _body;
58 /**
59 * Creates a new while statement.
61 * @param condition loop condition
62 * @param body loop body
63 * @param source_reference reference to source code
64 * @return newly created while statement
66 public WhileStatement (Expression condition, Block body, SourceReference? source_reference = null) {
67 this.body = body;
68 this.source_reference = source_reference;
69 this.condition = condition;
72 public override void accept (CodeVisitor visitor) {
73 visitor.visit_while_statement (this);
76 public override void accept_children (CodeVisitor visitor) {
77 condition.accept (visitor);
79 visitor.visit_end_full_expression (condition);
81 body.accept (visitor);
84 bool always_true (Expression condition) {
85 var literal = condition as BooleanLiteral;
86 return (literal != null && literal.value);
89 bool always_false (Expression condition) {
90 var literal = condition as BooleanLiteral;
91 return (literal != null && !literal.value);
94 public override bool check (CodeContext context) {
95 // convert to simple loop
97 if (always_true (condition)) {
98 // do not generate if block if condition is always true
99 } else if (always_false (condition)) {
100 // do not generate if block if condition is always false
101 body.insert_statement (0, new BreakStatement (condition.source_reference));
102 } else {
103 var if_condition = new UnaryExpression (UnaryOperator.LOGICAL_NEGATION, condition, condition.source_reference);
104 var true_block = new Block (condition.source_reference);
105 true_block.add_statement (new BreakStatement (condition.source_reference));
106 var if_stmt = new IfStatement (if_condition, true_block, null, condition.source_reference);
107 body.insert_statement (0, if_stmt);
110 var loop = new Loop (body, source_reference);
112 var parent_block = (Block) parent_node;
113 parent_block.replace_statement (this, loop);
115 return loop.check (context);