Fix DelegateType.copy
[vala-lang.git] / vala / valaifstatement.vala
blob7655ffd15d641d095d9576742dd7303abc2dc60d
1 /* valaifstatement.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 an if selection statement in the source code.
28 public class Vala.IfStatement : CodeNode, Statement {
29 /**
30 * The boolean condition to evaluate.
32 public Expression condition {
33 get {
34 return _condition;
36 set {
37 _condition = value;
38 _condition.parent_node = this;
42 /**
43 * The statement to be evaluated if the condition holds.
45 public Block true_statement {
46 get { return _true_statement; }
47 set {
48 _true_statement = value;
49 _true_statement.parent_node = this;
53 /**
54 * The optional statement to be evaluated if the condition doesn't hold.
56 public Block? false_statement {
57 get { return _false_statement; }
58 set {
59 _false_statement = value;
60 if (_false_statement != null)
61 _false_statement.parent_node = this;
65 private Expression _condition;
66 private Block _true_statement;
67 private Block _false_statement;
69 /**
70 * Creates a new if statement.
72 * @param cond a boolean condition
73 * @param true_stmt statement to be evaluated if condition is true
74 * @param false_stmt statement to be evaluated if condition is false
75 * @return newly created if statement
77 public IfStatement (Expression cond, Block true_stmt, Block? false_stmt, SourceReference? source) {
78 condition = cond;
79 true_statement = true_stmt;
80 false_statement = false_stmt;
81 source_reference = source;
84 public override void accept (CodeVisitor visitor) {
85 visitor.visit_if_statement (this);
88 public override void accept_children (CodeVisitor visitor) {
89 condition.accept (visitor);
91 visitor.visit_end_full_expression (condition);
93 true_statement.accept (visitor);
94 if (false_statement != null) {
95 false_statement.accept (visitor);
99 public override void replace_expression (Expression old_node, Expression new_node) {
100 if (condition == old_node) {
101 condition = new_node;
105 public override bool check (CodeContext context) {
106 if (checked) {
107 return !error;
110 checked = true;
112 condition.target_type = context.analyzer.bool_type.copy ();
114 condition.check (context);
116 true_statement.check (context);
117 if (false_statement != null) {
118 false_statement.check (context);
121 if (condition.error) {
122 /* if there was an error in the condition, skip this check */
123 error = true;
124 return false;
127 if (condition.value_type == null || !condition.value_type.compatible (context.analyzer.bool_type)) {
128 error = true;
129 Report.error (condition.source_reference, "Condition must be boolean");
130 return false;
133 add_error_types (condition.get_error_types ());
134 add_error_types (true_statement.get_error_types ());
136 if (false_statement != null) {
137 add_error_types (false_statement.get_error_types ());
140 return !error;
143 public override void emit (CodeGenerator codegen) {
144 condition.emit (codegen);
146 codegen.visit_end_full_expression (condition);
148 codegen.visit_if_statement (this);