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
20 * Jürg Billeter <j@bitron.ch>
26 * Represents a do iteration statement in the source code.
28 public class Vala
.DoStatement
: CodeNode
, Statement
{
30 * Specifies the loop body.
38 _body
.parent_node
= this
;
43 * Specifies the loop condition.
45 public Expression condition
{
51 _condition
.parent_node
= this
;
55 private Expression _condition
;
59 * Creates a new do statement.
61 * @param body loop body
62 * @param condition loop condition
63 * @param source_reference 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
;
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
);