Fix crash when using out parameters in delegates, fixes bug 563705
[vala-lang.git] / vala / valawhilestatement.vala
blob64c8e1ce251bdb685c95b5fd6ef427ca3a484095
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
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 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) {
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 public override void replace_expression (Expression old_node, Expression new_node) {
85 if (condition == old_node) {
86 condition = new_node;
90 public override bool check (SemanticAnalyzer analyzer) {
91 if (checked) {
92 return !error;
95 checked = true;
97 condition.check (analyzer);
99 body.check (analyzer);
101 if (condition.error) {
102 /* if there was an error in the condition, skip this check */
103 error = true;
104 return false;
107 if (!condition.value_type.compatible (analyzer.bool_type)) {
108 error = true;
109 Report.error (condition.source_reference, "Condition must be boolean");
110 return false;
113 add_error_types (condition.get_error_types ());
114 add_error_types (body.get_error_types ());
116 return !error;
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);
132 return body;