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
20 * Jürg Billeter <j@bitron.ch>
26 * Represents a conditional expression in the source code.
28 public class Vala
.ConditionalExpression
: Expression
{
32 public Expression condition
{
38 _condition
.parent_node
= this
;
43 * The expression to be evaluated if the condition holds.
45 public Expression true_expression
{
47 return _true_expression
;
50 _true_expression
= value
;
51 _true_expression
.parent_node
= this
;
56 * The expression to be evaluated if the condition doesn't hold.
58 public Expression false_expression
{
60 return _false_expression
;
63 _false_expression
= value
;
64 _false_expression
.parent_node
= this
;
68 Expression _condition
;
69 Expression _true_expression
;
70 Expression _false_expression
;
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
) {
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 is_accessible (Symbol sym
) {
104 return condition
.is_accessible (sym
) && true_expression
.is_accessible (sym
) && false_expression
.is_accessible (sym
);
107 public override bool check (CodeContext context
) {
114 if (!(context
.analyzer
.current_symbol is Block
)) {
115 Report
.error (source_reference
, "Conditional expressions may only be used in blocks");
120 // convert ternary expression into if statement
121 // required for flow analysis and exception handling
123 string temp_name
= get_temp_name ();
125 true_expression
.target_type
= target_type
;
126 false_expression
.target_type
= target_type
;
128 var local
= new
LocalVariable (null, temp_name
, null, source_reference
);
129 var decl
= new
DeclarationStatement (local
, source_reference
);
131 var true_local
= new
LocalVariable (null, temp_name
, true_expression
, true_expression
.source_reference
);
132 var true_block
= new
Block (true_expression
.source_reference
);
133 var true_decl
= new
DeclarationStatement (true_local
, true_expression
.source_reference
);
134 true_block
.add_statement (true_decl
);
136 var false_local
= new
LocalVariable (null, temp_name
, false_expression
, false_expression
.source_reference
);
137 var false_block
= new
Block (false_expression
.source_reference
);
138 var false_decl
= new
DeclarationStatement (false_local
, false_expression
.source_reference
);
139 false_block
.add_statement (false_decl
);
141 var if_stmt
= new
IfStatement (condition
, true_block
, false_block
, source_reference
);
143 insert_statement (context
.analyzer
.insert_block
, decl
);
144 insert_statement (context
.analyzer
.insert_block
, if_stmt
);
146 if (!if_stmt
.check (context
) || true_expression
.error
|| false_expression
.error
) {
151 true_expression
= true_local
.initializer
;
152 false_expression
= false_local
.initializer
;
154 true_block
.remove_local_variable (true_local
);
155 false_block
.remove_local_variable (false_local
);
157 if (false_expression
.value_type
.compatible (true_expression
.value_type
)) {
158 value_type
= true_expression
.value_type
.copy ();
159 } else if (true_expression
.value_type
.compatible (false_expression
.value_type
)) {
160 value_type
= false_expression
.value_type
.copy ();
163 Report
.error (condition
.source_reference
, "Incompatible expressions");
167 value_type
.value_owned
= (true_expression
.value_type
.value_owned
|| false_expression
.value_type
.value_owned
);
169 local
.variable_type
= value_type
;
170 decl
.check (context
);
172 true_expression
.target_type
= value_type
;
173 false_expression
.target_type
= value_type
;
175 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
);
176 true_stmt
.check (context
);
178 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
);
179 false_stmt
.check (context
);
181 true_block
.replace_statement (true_decl
, true_stmt
);
182 false_block
.replace_statement (false_decl
, false_stmt
);
184 var ma
= new MemberAccess
.simple (local
.name
, source_reference
);
185 ma
.formal_target_type
= formal_target_type
;
186 ma
.target_type
= target_type
;
189 parent_node
.replace_expression (this
, ma
);