1 /* valaifstatement.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 an if selection statement in the source code.
28 public class Vala
.IfStatement
: CodeNode
, Statement
{
30 * The boolean condition to evaluate.
32 public Expression condition
{
38 _condition
.parent_node
= this
;
43 * The statement to be evaluated if the condition holds.
45 public Block true_statement
{ get; set; }
48 * The optional statement to be evaluated if the condition doesn't hold.
50 public Block? false_statement
{ get; set; }
52 private Expression _condition
;
55 * Creates a new if statement.
57 * @param cond a boolean condition
58 * @param true_stmt statement to be evaluated if condition is true
59 * @param false_stmt statement to be evaluated if condition is false
60 * @return newly created if statement
62 public IfStatement (Expression cond
, Block true_stmt
, Block? false_stmt
, SourceReference? source
) {
64 true_statement
= true_stmt
;
65 false_statement
= false_stmt
;
66 source_reference
= source
;
69 public override void accept (CodeVisitor visitor
) {
70 visitor
.visit_if_statement (this
);
73 public override void accept_children (CodeVisitor visitor
) {
74 condition
.accept (visitor
);
76 visitor
.visit_end_full_expression (condition
);
78 true_statement
.accept (visitor
);
79 if (false_statement
!= null) {
80 false_statement
.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 true_statement
.check (analyzer
);
100 if (false_statement
!= null) {
101 false_statement
.check (analyzer
);
104 if (condition
.error
) {
105 /* if there was an error in the condition, skip this check */
110 if (!condition
.value_type
.compatible (analyzer
.bool_type
)) {
112 Report
.error (condition
.source_reference
, "Condition must be boolean");
116 add_error_types (condition
.get_error_types ());
117 add_error_types (true_statement
.get_error_types ());
119 if (false_statement
!= null) {
120 add_error_types (false_statement
.get_error_types ());