Insert "%s" argument in printf calls with non-literal format string
[vala-lang.git] / vala / valaconditionalexpression.vala
blob6c7e8d66b3fd964bd11402e1f7049bf75c7f1751
1 /* valaconditionalexpression.vala
3 * Copyright (C) 2006-2009 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 // convert ternary expression into if statement
115 // required for flow analysis and exception handling
117 string temp_name = get_temp_name ();
119 true_expression.target_type = target_type;
120 false_expression.target_type = target_type;
122 var local = new LocalVariable (null, temp_name, null, source_reference);
123 var decl = new DeclarationStatement (local, source_reference);
125 var true_local = new LocalVariable (null, temp_name, true_expression, true_expression.source_reference);
126 var true_block = new Block (true_expression.source_reference);
127 var true_decl = new DeclarationStatement (true_local, true_expression.source_reference);
128 true_block.add_statement (true_decl);
130 var false_local = new LocalVariable (null, temp_name, false_expression, false_expression.source_reference);
131 var false_block = new Block (false_expression.source_reference);
132 var false_decl = new DeclarationStatement (false_local, false_expression.source_reference);
133 false_block.add_statement (false_decl);
135 var if_stmt = new IfStatement (condition, true_block, false_block, source_reference);
137 insert_statement (analyzer.insert_block, decl);
138 insert_statement (analyzer.insert_block, if_stmt);
140 if (!if_stmt.check (analyzer) || true_expression.error || false_expression.error) {
141 error = true;
142 return false;
145 true_expression = true_local.initializer;
146 false_expression = false_local.initializer;
148 true_block.remove_local_variable (true_local);
149 false_block.remove_local_variable (false_local);
151 if (false_expression.value_type.compatible (true_expression.value_type)) {
152 value_type = true_expression.value_type.copy ();
153 } else if (true_expression.value_type.compatible (false_expression.value_type)) {
154 value_type = false_expression.value_type.copy ();
155 } else {
156 error = true;
157 Report.error (condition.source_reference, "Incompatible expressions");
158 return false;
161 value_type.value_owned = (true_expression.value_type.value_owned || false_expression.value_type.value_owned);
163 local.variable_type = value_type;
164 decl.check (analyzer);
166 true_expression.target_type = value_type;
167 false_expression.target_type = value_type;
169 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);
170 true_stmt.check (analyzer);
172 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);
173 false_stmt.check (analyzer);
175 true_block.replace_statement (true_decl, true_stmt);
176 false_block.replace_statement (false_decl, false_stmt);
178 var ma = new MemberAccess.simple (local.name, source_reference);
179 ma.target_type = target_type;
180 ma.check (analyzer);
182 parent_node.replace_expression (this, ma);
184 return true;