girparser: Rename MethodInfo to ParameterInfo.
[vala-lang.git] / vala / valaconditionalexpression.vala
blob3ae003cd1662c6f1670035273215d61c00e8b26a
1 /* valaconditionalexpression.vala
3 * Copyright (C) 2006-2011 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 visitor.visit_conditional_expression (this);
90 visitor.visit_expression (this);
93 public override void accept_children (CodeVisitor visitor) {
94 condition.accept (visitor);
95 true_expression.accept (visitor);
96 false_expression.accept (visitor);
99 public override bool is_pure () {
100 return condition.is_pure () && true_expression.is_pure () && false_expression.is_pure ();
103 public override bool check (CodeContext context) {
104 if (checked) {
105 return !error;
108 checked = true;
110 if (!(context.analyzer.current_symbol is Block)) {
111 Report.error (source_reference, "Conditional expressions may only be used in blocks");
112 error = true;
113 return false;
116 // convert ternary expression into if statement
117 // required for flow analysis and exception handling
119 string temp_name = get_temp_name ();
121 true_expression.target_type = target_type;
122 false_expression.target_type = target_type;
124 var local = new LocalVariable (null, temp_name, null, source_reference);
125 var decl = new DeclarationStatement (local, source_reference);
127 var true_local = new LocalVariable (null, temp_name, true_expression, true_expression.source_reference);
128 var true_block = new Block (true_expression.source_reference);
129 var true_decl = new DeclarationStatement (true_local, true_expression.source_reference);
130 true_block.add_statement (true_decl);
132 var false_local = new LocalVariable (null, temp_name, false_expression, false_expression.source_reference);
133 var false_block = new Block (false_expression.source_reference);
134 var false_decl = new DeclarationStatement (false_local, false_expression.source_reference);
135 false_block.add_statement (false_decl);
137 var if_stmt = new IfStatement (condition, true_block, false_block, source_reference);
139 insert_statement (context.analyzer.insert_block, decl);
140 insert_statement (context.analyzer.insert_block, if_stmt);
142 if (!if_stmt.check (context) || true_expression.error || false_expression.error) {
143 error = true;
144 return false;
147 true_expression = true_local.initializer;
148 false_expression = false_local.initializer;
150 true_block.remove_local_variable (true_local);
151 false_block.remove_local_variable (false_local);
153 if (false_expression.value_type.compatible (true_expression.value_type)) {
154 value_type = true_expression.value_type.copy ();
155 } else if (true_expression.value_type.compatible (false_expression.value_type)) {
156 value_type = false_expression.value_type.copy ();
157 } else {
158 error = true;
159 Report.error (condition.source_reference, "Incompatible expressions");
160 return false;
163 value_type.value_owned = (true_expression.value_type.value_owned || false_expression.value_type.value_owned);
165 local.variable_type = value_type;
166 decl.check (context);
168 true_expression.target_type = value_type;
169 false_expression.target_type = value_type;
171 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);
172 true_stmt.check (context);
174 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);
175 false_stmt.check (context);
177 true_block.replace_statement (true_decl, true_stmt);
178 false_block.replace_statement (false_decl, false_stmt);
180 var ma = new MemberAccess.simple (local.name, source_reference);
181 ma.formal_target_type = formal_target_type;
182 ma.target_type = target_type;
183 ma.check (context);
185 parent_node.replace_expression (this, ma);
187 return true;