1 /* valawhilestatement.vala
3 * Copyright (C) 2006-2008 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
20 * Jürg Billeter <j@bitron.ch>
26 * Represents a while iteration statement in the source code.
28 public class Vala
.WhileStatement
: CodeNode
, Statement
{
30 * Specifies the loop condition.
32 public Expression condition
{
38 _condition
.parent_node
= this
;
43 * Specifies the loop body.
51 _body
.parent_node
= this
;
55 private Expression _condition
;
59 * Creates a new while statement.
61 * @param cond loop condition
62 * @param body loop body
63 * @param source reference to source code
64 * @return newly created while statement
66 public WhileStatement (Expression condition
, Block body
, SourceReference? source_reference
= null) {
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 public override void replace_expression (Expression old_node
, Expression new_node
) {
85 if (condition
== old_node
) {
90 public override bool check (SemanticAnalyzer analyzer
) {
97 condition
.check (analyzer
);
99 body
.check (analyzer
);
101 if (condition
.error
) {
102 /* if there was an error in the condition, skip this check */
107 if (!condition
.value_type
.compatible (analyzer
.bool_type
)) {
109 Report
.error (condition
.source_reference
, "Condition must be boolean");
113 add_error_types (condition
.get_error_types ());
114 add_error_types (body
.get_error_types ());
119 public Block
prepare_condition_split (SemanticAnalyzer analyzer
) {
120 // move condition into the loop body to allow split
121 // in multiple statements
123 var if_condition
= new
UnaryExpression (UnaryOperator
.LOGICAL_NEGATION
, condition
, condition
.source_reference
);
124 var true_block
= new
Block (condition
.source_reference
);
125 true_block
.add_statement (new
BreakStatement (condition
.source_reference
));
126 var if_stmt
= new
IfStatement (if_condition
, true_block
, null, condition
.source_reference
);
127 body
.insert_statement (0, if_stmt
);
129 condition
= new
BooleanLiteral (true, source_reference
);
130 condition
.check (analyzer
);