Fix `this' access in default signal handlers
[vala-lang.git] / vala / valaforstatement.vala
blob38e79bc251597d548dd6912923b4f15a779f8902
1 /* valaforstatement.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;
24 using Gee;
26 /**
27 * Represents a for iteration statement in the source code.
29 public class Vala.ForStatement : CodeNode, Statement {
30 /**
31 * Specifies the loop condition.
33 public Expression? condition {
34 get {
35 return _condition;
37 set {
38 _condition = value;
39 if (_condition != null) {
40 _condition.parent_node = this;
45 /**
46 * Specifies the loop body.
48 public Block body {
49 get {
50 return _body;
52 set {
53 _body = value;
54 _body.parent_node = this;
58 private Gee.List<Expression> initializer = new ArrayList<Expression> ();
59 private Gee.List<Expression> iterator = new ArrayList<Expression> ();
61 private Expression _condition;
62 private Block _body;
64 /**
65 * Creates a new for statement.
67 * @param cond loop condition
68 * @param body loop body
69 * @param source_reference reference to source code
70 * @return newly created for statement
72 public ForStatement (Expression? condition, Block body, SourceReference? source_reference = null) {
73 this.condition = condition;
74 this.body = body;
75 this.source_reference = source_reference;
78 /**
79 * Appends the specified expression to the list of initializers.
81 * @param init an initializer expression
83 public void add_initializer (Expression init) {
84 init.parent_node = this;
85 initializer.add (init);
88 /**
89 * Returns a copy of the list of initializers.
91 * @return initializer list
93 public Gee.List<Expression> get_initializer () {
94 return new ReadOnlyList<Expression> (initializer);
97 /**
98 * Appends the specified expression to the iterator.
100 * @param iter an iterator expression
102 public void add_iterator (Expression iter) {
103 iter.parent_node = this;
104 iterator.add (iter);
108 * Returns a copy of the iterator.
110 * @return iterator
112 public Gee.List<Expression> get_iterator () {
113 return new ReadOnlyList<Expression> (iterator);
116 public override void accept (CodeVisitor visitor) {
117 visitor.visit_for_statement (this);
120 public override void accept_children (CodeVisitor visitor) {
121 foreach (Expression init_expr in initializer) {
122 init_expr.accept (visitor);
123 visitor.visit_end_full_expression (init_expr);
126 if (condition != null) {
127 condition.accept (visitor);
129 visitor.visit_end_full_expression (condition);
132 foreach (Expression it_expr in iterator) {
133 it_expr.accept (visitor);
134 visitor.visit_end_full_expression (it_expr);
137 body.accept (visitor);
140 bool always_true (Expression condition) {
141 var literal = condition as BooleanLiteral;
142 return (literal != null && literal.value);
145 public override bool check (SemanticAnalyzer analyzer) {
146 // convert to simple loop
148 var block = new Block (source_reference);
150 // initializer
151 foreach (var init_expr in initializer) {
152 block.add_statement (new ExpressionStatement (init_expr, init_expr.source_reference));
155 // do not generate if block if condition is always true
156 if (condition != null && !always_true (condition)) {
157 // condition
158 var if_condition = new UnaryExpression (UnaryOperator.LOGICAL_NEGATION, condition, condition.source_reference);
159 var true_block = new Block (condition.source_reference);
160 true_block.add_statement (new BreakStatement (condition.source_reference));
161 var if_stmt = new IfStatement (if_condition, true_block, null, condition.source_reference);
162 body.insert_statement (0, if_stmt);
165 // iterator
166 var first_local = new LocalVariable (analyzer.bool_type.copy (), get_temp_name (), new BooleanLiteral (true, source_reference), source_reference);
167 block.add_statement (new DeclarationStatement (first_local, source_reference));
169 var iterator_block = new Block (source_reference);
170 foreach (var it_expr in iterator) {
171 iterator_block.add_statement (new ExpressionStatement (it_expr, it_expr.source_reference));
174 var first_if = new IfStatement (new UnaryExpression (UnaryOperator.LOGICAL_NEGATION, new MemberAccess.simple (first_local.name, source_reference), source_reference), iterator_block, null, source_reference);
175 body.insert_statement (0, first_if);
176 body.insert_statement (1, new ExpressionStatement (new Assignment (new MemberAccess.simple (first_local.name, source_reference), new BooleanLiteral (false, source_reference), AssignmentOperator.SIMPLE, source_reference), source_reference));
178 block.add_statement (new Loop (body, source_reference));
180 var parent_block = (Block) parent_node;
181 parent_block.replace_statement (this, block);
183 return block.check (analyzer);