codegen: Limit scope of locals freed on errors thrown from catch clauses
[vala-lang.git] / vala / valadostatement.vala
blob6bf72c0c71f7a1a8702ce3ebf5d591dbf7dd0377
1 /* valadostatement.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 do iteration statement in the source code.
28 public class Vala.DoStatement : CodeNode, Statement {
29 /**
30 * Specifies the loop body.
32 public Block body {
33 get {
34 return _body;
36 set {
37 _body = value;
38 _body.parent_node = this;
42 /**
43 * Specifies the loop condition.
45 public Expression condition {
46 get {
47 return _condition;
49 set {
50 _condition = value;
51 _condition.parent_node = this;
55 private Expression _condition;
56 private Block _body;
58 /**
59 * Creates a new do statement.
61 * @param cond loop condition
62 * @param body loop body
63 * @param source reference to source code
64 * @return newly created do statement
66 public DoStatement (Block body, Expression condition, SourceReference? source_reference = null) {
67 this.condition = condition;
68 this.source_reference = source_reference;
69 this.body = body;
72 public override void accept (CodeVisitor visitor) {
73 visitor.visit_do_statement (this);
76 public override void accept_children (CodeVisitor visitor) {
77 body.accept (visitor);
79 condition.accept (visitor);
81 visitor.visit_end_full_expression (condition);
84 bool always_true (Expression condition) {
85 var literal = condition as BooleanLiteral;
86 return (literal != null && literal.value);
89 public override bool check (CodeContext context) {
90 // convert to simple loop
92 // do not generate variable and if block if condition is always true
93 if (always_true (condition)) {
94 var loop = new Loop (body, source_reference);
96 var parent_block = (Block) parent_node;
97 parent_block.replace_statement (this, loop);
99 return loop.check (context);
102 var block = new Block (source_reference);
104 var first_local = new LocalVariable (context.analyzer.bool_type.copy (), get_temp_name (), new BooleanLiteral (true, source_reference), source_reference);
105 block.add_statement (new DeclarationStatement (first_local, source_reference));
107 var if_condition = new UnaryExpression (UnaryOperator.LOGICAL_NEGATION, condition, condition.source_reference);
108 var true_block = new Block (condition.source_reference);
109 true_block.add_statement (new BreakStatement (condition.source_reference));
110 var if_stmt = new IfStatement (if_condition, true_block, null, condition.source_reference);
112 var condition_block = new Block (condition.source_reference);
113 condition_block.add_statement (if_stmt);
115 var first_if = new IfStatement (new UnaryExpression (UnaryOperator.LOGICAL_NEGATION, new MemberAccess.simple (first_local.name, source_reference), source_reference), condition_block, null, source_reference);
116 body.insert_statement (0, first_if);
117 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));
119 block.add_statement (new Loop (body, source_reference));
121 var parent_block = (Block) parent_node;
122 parent_block.replace_statement (this, block);
124 return block.check (context);