1 /* valaforstatement.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>
27 * Represents a for iteration statement in the source code.
29 public class Vala
.ForStatement
: CodeNode
, Statement
{
31 * Specifies the loop condition.
33 public Expression? condition
{
39 if (_condition
!= null) {
40 _condition
.parent_node
= this
;
46 * Specifies the loop body.
54 _body
.parent_node
= this
;
58 private Gee
.List
<Expression
> initializer
= new ArrayList
<Expression
> ();
59 private Gee
.List
<Expression
> iterator
= new ArrayList
<Expression
> ();
61 private Expression _condition
;
65 * Creates a new for statement.
67 * @param cond loop condition
68 * @param body loop body
69 * @param source_reference reference to source code
70 * @return newly created for statement
72 public ForStatement (Expression? condition
, Block body
, SourceReference? source_reference
= null) {
73 this
.condition
= condition
;
75 this
.source_reference
= source_reference
;
79 * Appends the specified expression to the list of initializers.
81 * @param init an initializer expression
83 public void add_initializer (Expression init
) {
84 init
.parent_node
= this
;
85 initializer
.add (init
);
89 * Returns a copy of the list of initializers.
91 * @return initializer list
93 public Gee
.List
<Expression
> get_initializer () {
94 return new ReadOnlyList
<Expression
> (initializer
);
98 * Appends the specified expression to the iterator.
100 * @param iter an iterator expression
102 public void add_iterator (Expression iter
) {
103 iter
.parent_node
= this
;
108 * Returns a copy of the iterator.
112 public Gee
.List
<Expression
> get_iterator () {
113 return new ReadOnlyList
<Expression
> (iterator
);
116 public override void accept (CodeVisitor visitor
) {
117 visitor
.visit_for_statement (this
);
120 public override void accept_children (CodeVisitor visitor
) {
121 foreach (Expression init_expr
in initializer
) {
122 init_expr
.accept (visitor
);
123 visitor
.visit_end_full_expression (init_expr
);
126 if (condition
!= null) {
127 condition
.accept (visitor
);
129 visitor
.visit_end_full_expression (condition
);
132 foreach (Expression it_expr
in iterator
) {
133 it_expr
.accept (visitor
);
134 visitor
.visit_end_full_expression (it_expr
);
137 body
.accept (visitor
);
140 public override void replace_expression (Expression old_node
, Expression new_node
) {
141 if (condition
== old_node
) {
142 condition
= new_node
;
146 for (int i
= 0; i
< initializer
.size
; i
++) {
147 if (initializer
[i
] == old_node
) {
148 initializer
[i
] = new_node
;
152 for (int i
= 0; i
< iterator
.size
; i
++) {
153 if (iterator
[i
] == old_node
) {
154 iterator
[i
] = new_node
;
160 public override bool check (SemanticAnalyzer analyzer
) {
167 foreach (Expression init_expr
in initializer
) {
168 init_expr
.check (analyzer
);
171 if (condition
!= null) {
172 condition
.check (analyzer
);
175 foreach (Expression it_expr
in iterator
) {
176 it_expr
.check (analyzer
);
179 body
.check (analyzer
);
181 if (condition
!= null && condition
.error
) {
182 /* if there was an error in the condition, skip this check */
187 if (condition
!= null && !condition
.value_type
.compatible (analyzer
.bool_type
)) {
189 Report
.error (condition
.source_reference
, "Condition must be boolean");
193 if (condition
!= null) {
194 add_error_types (condition
.get_error_types ());
197 add_error_types (body
.get_error_types ());
198 foreach (Expression exp
in get_initializer ()) {
199 add_error_types (exp
.get_error_types ());
201 foreach (Expression exp
in get_iterator ()) {
202 add_error_types (exp
.get_error_types ());
208 public Block
prepare_condition_split (SemanticAnalyzer analyzer
) {
209 // move condition into the loop body to allow split
210 // in multiple statements
212 var if_condition
= new
UnaryExpression (UnaryOperator
.LOGICAL_NEGATION
, condition
, condition
.source_reference
);
213 var true_block
= new
Block (condition
.source_reference
);
214 true_block
.add_statement (new
BreakStatement (condition
.source_reference
));
215 var if_stmt
= new
IfStatement (if_condition
, true_block
, null, condition
.source_reference
);
216 body
.insert_statement (0, if_stmt
);
218 condition
= new
BooleanLiteral (true, source_reference
);
219 condition
.check (analyzer
);