Do not free values returned via g_object_get prematurely, require
[vala-lang.git] / vala / valaifstatement.vala
blob4a97f81c1896914ca1540178241c1d63d0eb22d9
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
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 { get; set; }
47 /**
48 * The optional statement to be evaluated if the condition doesn't hold.
50 public Block? false_statement { get; set; }
52 private Expression _condition;
54 /**
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) {
63 condition = cond;
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) {
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 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 */
106 error = true;
107 return false;
110 if (!condition.value_type.compatible (analyzer.bool_type)) {
111 error = true;
112 Report.error (condition.source_reference, "Condition must be boolean");
113 return false;
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 ());
123 return !error;