Warn when using result variable with incompatible type to prepare possible
[vala-lang.git] / vala / valaconditionalexpression.vala
blob191d0fc0cbcae9a1183fce07e3a1b167098a825e
1 /* valaconditionalexpression.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 conditional expression in the source code.
28 public class Vala.ConditionalExpression : Expression {
29 /**
30 * The condition.
32 public Expression condition {
33 get {
34 return _condition;
36 set {
37 _condition = value;
38 _condition.parent_node = this;
42 /**
43 * The expression to be evaluated if the condition holds.
45 public Expression true_expression {
46 get {
47 return _true_expression;
49 set {
50 _true_expression = value;
51 _true_expression.parent_node = this;
55 /**
56 * The expression to be evaluated if the condition doesn't hold.
58 public Expression false_expression {
59 get {
60 return _false_expression;
62 set {
63 _false_expression = value;
64 _false_expression.parent_node = this;
68 Expression _condition;
69 Expression _true_expression;
70 Expression _false_expression;
72 /**
73 * Creates a new conditional expression.
75 * @param cond a condition
76 * @param true_expr expression to be evaluated if condition is true
77 * @param false_expr expression to be evaluated if condition is false
78 * @return newly created conditional expression
80 public ConditionalExpression (Expression cond, Expression true_expr, Expression false_expr, SourceReference source) {
81 condition = cond;
82 true_expression = true_expr;
83 false_expression = false_expr;
84 source_reference = source;
87 public override void accept (CodeVisitor visitor) {
88 condition.accept (visitor);
89 true_expression.accept (visitor);
90 false_expression.accept (visitor);
92 visitor.visit_conditional_expression (this);
94 visitor.visit_expression (this);
97 public override bool is_pure () {
98 return condition.is_pure () && true_expression.is_pure () && false_expression.is_pure ();
101 public override bool check (SemanticAnalyzer analyzer) {
102 if (checked) {
103 return !error;
106 checked = true;
108 if (!(analyzer.current_symbol is Block)) {
109 Report.error (source_reference, "Conditional expressions may only be used in blocks");
110 error = true;
111 return false;
114 var old_insert_block = analyzer.insert_block;
115 analyzer.insert_block = prepare_condition_split (analyzer);
117 // convert ternary expression into if statement
118 // required for flow analysis and exception handling
120 string temp_name = get_temp_name ();
122 true_expression.target_type = target_type;
123 false_expression.target_type = target_type;
125 var local = new LocalVariable (null, temp_name, null, source_reference);
126 var decl = new DeclarationStatement (local, source_reference);
128 var true_local = new LocalVariable (null, temp_name, true_expression, true_expression.source_reference);
129 var true_block = new Block (true_expression.source_reference);
130 var true_decl = new DeclarationStatement (true_local, true_expression.source_reference);
131 true_block.add_statement (true_decl);
133 var false_local = new LocalVariable (null, temp_name, false_expression, false_expression.source_reference);
134 var false_block = new Block (false_expression.source_reference);
135 var false_decl = new DeclarationStatement (false_local, false_expression.source_reference);
136 false_block.add_statement (false_decl);
138 var if_stmt = new IfStatement (condition, true_block, false_block, source_reference);
140 insert_statement (analyzer.insert_block, decl);
141 insert_statement (analyzer.insert_block, if_stmt);
143 if (!if_stmt.check (analyzer)) {
144 return false;
146 analyzer.insert_block = old_insert_block;
148 true_expression = true_local.initializer;
149 false_expression = false_local.initializer;
151 true_block.remove_local_variable (true_local);
152 false_block.remove_local_variable (false_local);
154 if (false_expression.value_type.compatible (true_expression.value_type)) {
155 value_type = true_expression.value_type.copy ();
156 } else if (true_expression.value_type.compatible (false_expression.value_type)) {
157 value_type = false_expression.value_type.copy ();
158 } else {
159 error = true;
160 Report.error (condition.source_reference, "Incompatible expressions");
161 return false;
164 value_type.value_owned = (true_expression.value_type.value_owned || false_expression.value_type.value_owned);
166 local.variable_type = value_type;
167 decl.check (analyzer);
169 true_expression.target_type = value_type;
170 false_expression.target_type = value_type;
172 var true_stmt = new ExpressionStatement (new Assignment (new MemberAccess.simple (local.name, true_expression.source_reference), true_expression, AssignmentOperator.SIMPLE, true_expression.source_reference), true_expression.source_reference);
173 true_stmt.check (analyzer);
175 var false_stmt = new ExpressionStatement (new Assignment (new MemberAccess.simple (local.name, false_expression.source_reference), false_expression, AssignmentOperator.SIMPLE, false_expression.source_reference), false_expression.source_reference);
176 false_stmt.check (analyzer);
178 true_block.replace_statement (true_decl, true_stmt);
179 false_block.replace_statement (false_decl, false_stmt);
181 var ma = new MemberAccess.simple (local.name, source_reference);
182 ma.target_type = target_type;
183 ma.check (analyzer);
185 parent_node.replace_expression (this, ma);
187 return true;