append_scope_free (sym, stop_at);
if (jump_stmt is BreakStatement) {
- if (b.parent_node is Loop ||
+ if (b.parent_node is LoopStatement ||
b.parent_node is ForeachStatement ||
b.parent_node is SwitchStatement) {
return;
}
} else if (jump_stmt is ContinueStatement) {
- if (b.parent_node is Loop ||
+ if (b.parent_node is LoopStatement ||
b.parent_node is ForeachStatement) {
return;
}
}
}
- public override void visit_loop (Loop stmt) {
+ public override void visit_loop_statement (LoopStatement stmt) {
if (context.profile == Profile.GOBJECT) {
ccode.open_while (new CCodeConstant ("TRUE"));
} else {
valalockable.vala \
valalockstatement.vala \
valaloop.vala \
+ valaloopstatement.vala \
valamarkupreader.vala \
valamemberaccess.vala \
valamemberinitializer.vala \
*
* @param stmt a loop
*/
- public virtual void visit_loop (Loop stmt) {
+ public virtual void visit_loop_statement (LoopStatement stmt) {
}
/**
}
}
- public override void visit_loop (Loop stmt) {
+ public override void visit_loop_statement (LoopStatement stmt) {
write_indent ();
write_string ("loop");
stmt.body.accept (this);
/**
* Represents a do iteration statement in the source code.
*/
-public class Vala.DoStatement : CodeNode, Statement {
- /**
- * Specifies the loop body.
- */
- public Block body {
- get {
- return _body;
- }
- private set {
- _body = value;
- _body.parent_node = this;
- }
- }
-
- /**
- * Specifies the loop condition.
- */
- public Expression condition {
- get {
- return _condition;
- }
- private set {
- _condition = value;
- _condition.parent_node = this;
- }
- }
-
- private Expression _condition;
- private Block _body;
-
+public class Vala.DoStatement : Loop, Statement {
/**
* Creates a new do statement.
*
- * @param body loop body
* @param condition loop condition
+ * @param body loop body
* @param source_reference reference to source code
* @return newly created do statement
*/
- public DoStatement (Block body, Expression condition, SourceReference? source_reference = null) {
- this.condition = condition;
- this.source_reference = source_reference;
- this.body = body;
+ public DoStatement (Expression condition, Block body, SourceReference? source_reference = null) {
+ base (condition, body, source_reference);
}
public override void accept (CodeVisitor visitor) {
visitor.visit_end_full_expression (condition);
}
- public override void replace_expression (Expression old_node, Expression new_node) {
- if (condition == old_node) {
- condition = new_node;
- }
- }
-
public override bool check (CodeContext context) {
if (checked) {
return !error;
// do not generate variable and if block if condition is always true
if (condition.is_always_true ()) {
- var loop = new Loop (body, source_reference);
+ var loop = new LoopStatement (body, source_reference);
unowned Block parent_block = (Block) parent_node;
parent_block.replace_statement (this, loop);
body.insert_statement (0, first_if);
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));
- block.add_statement (new Loop (body, source_reference));
+ block.add_statement (new LoopStatement (body, source_reference));
unowned Block parent_block = (Block) parent_node;
parent_block.replace_statement (this, block);
jump_stack.remove_at (jump_stack.size - 1);
}
- public override void visit_loop (Loop stmt) {
+ public override void visit_loop_statement (LoopStatement stmt) {
if (unreachable (stmt)) {
return;
}
/**
* Represents a for iteration statement in the source code.
*/
-public class Vala.ForStatement : CodeNode, Statement {
- /**
- * Specifies the loop condition.
- */
- public Expression? condition {
- get {
- return _condition;
- }
- private set {
- _condition = value;
- if (_condition != null) {
- _condition.parent_node = this;
- }
- }
- }
-
- /**
- * Specifies the loop body.
- */
- public Block body {
- get {
- return _body;
- }
- private set {
- _body = value;
- _body.parent_node = this;
- }
- }
-
+public class Vala.ForStatement : Loop, Statement {
private List<Expression> initializer = new ArrayList<Expression> ();
private List<Expression> iterator = new ArrayList<Expression> ();
- private Expression _condition;
- private Block _body;
-
/**
* Creates a new for statement.
*
* @return newly created for statement
*/
public ForStatement (Expression? condition, Block body, SourceReference? source_reference = null) {
- this.condition = condition;
- this.body = body;
- this.source_reference = source_reference;
+ base (condition, body, source_reference);
}
/**
}
public override void replace_expression (Expression old_node, Expression new_node) {
- if (condition == old_node) {
- condition = new_node;
- }
+ base.replace_expression (old_node, new_node);
+
for (int i=0; i < initializer.size; i++) {
if (initializer[i] == old_node) {
initializer[i] = new_node;
body.insert_statement (0, first_if);
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));
- block.add_statement (new Loop (body, source_reference));
+ block.add_statement (new LoopStatement (body, source_reference));
unowned Block parent_block = (Block) parent_node;
parent_block.replace_statement (this, block);
expect_terminator ();
- return new DoStatement (body, condition, get_src (begin));
+ return new DoStatement (condition, body, get_src (begin));
}
/* valaloop.vala
*
- * Copyright (C) 2009-2010 Jürg Billeter
+ * Copyright (C) 2021 Rico Tzschichholz
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author:
- * Jürg Billeter <j@bitron.ch>
+ * Rico Tzschichholz <ricotz@ubuntu.com>
*/
-using GLib;
-
/**
- * Represents an endless loop.
+ * Base class for all loop statements.
*/
-public class Vala.Loop : CodeNode, Statement {
+public abstract class Vala.Loop : CodeNode {
+ /**
+ * Specifies the loop condition.
+ */
+ public Expression? condition {
+ get {
+ return _condition;
+ }
+ private set {
+ _condition = value;
+ if (_condition != null) {
+ _condition.parent_node = this;
+ }
+ }
+ }
+
/**
* Specifies the loop body.
*/
get {
return _body;
}
- set {
+ private set {
_body = value;
_body.parent_node = this;
}
}
- private Block _body;
+ Expression _condition;
+ Block _body;
- /**
- * Creates a new loop.
- *
- * @param body loop body
- * @param source_reference reference to source code
- * @return newly created while statement
- */
- public Loop (Block body, SourceReference? source_reference = null) {
+ protected Loop (Expression? condition, Block body, SourceReference? source_reference = null) {
+ this.condition = condition;
this.body = body;
this.source_reference = source_reference;
}
- public override void accept (CodeVisitor visitor) {
- visitor.visit_loop (this);
- }
-
- public override void accept_children (CodeVisitor visitor) {
- body.accept (visitor);
- }
-
- public override void get_error_types (Collection<DataType> collection, SourceReference? source_reference = null) {
- body.get_error_types (collection, source_reference);
- }
-
- public override bool check (CodeContext context) {
- if (checked) {
- return !error;
+ public override void replace_expression (Expression old_node, Expression new_node) {
+ if (condition == old_node) {
+ condition = new_node;
}
-
- checked = true;
-
- body.check (context);
-
- return !error;
- }
-
- public override void emit (CodeGenerator codegen) {
- codegen.visit_loop (this);
}
}
-
--- /dev/null
+/* valaloopstatement.vala
+ *
+ * Copyright (C) 2009-2010 Jürg Billeter
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * Author:
+ * Jürg Billeter <j@bitron.ch>
+ */
+
+using GLib;
+
+/**
+ * Represents an endless loop.
+ */
+public class Vala.LoopStatement : Loop, Statement {
+ /**
+ * Creates a new loop.
+ *
+ * @param body loop body
+ * @param source_reference reference to source code
+ * @return newly created while statement
+ */
+ public LoopStatement (Block body, SourceReference? source_reference = null) {
+ base (null, body, source_reference);
+ }
+
+ public override void accept (CodeVisitor visitor) {
+ visitor.visit_loop_statement (this);
+ }
+
+ public override void accept_children (CodeVisitor visitor) {
+ body.accept (visitor);
+ }
+
+ public override void get_error_types (Collection<DataType> collection, SourceReference? source_reference = null) {
+ body.get_error_types (collection, source_reference);
+ }
+
+ public override bool check (CodeContext context) {
+ if (checked) {
+ return !error;
+ }
+
+ checked = true;
+
+ body.check (context);
+
+ return !error;
+ }
+
+ public override void emit (CodeGenerator codegen) {
+ codegen.visit_loop_statement (this);
+ }
+}
+
var condition = parse_expression ();
expect (TokenType.CLOSE_PARENS);
expect (TokenType.SEMICOLON);
- return new DoStatement (body, condition, get_src (begin));
+ return new DoStatement (condition, body, get_src (begin));
}
Statement parse_for_statement () throws ParseError {
label.accept_children (this);
}
- public override void visit_loop (Loop stmt) {
+ public override void visit_loop_statement (LoopStatement stmt) {
if (stmt.checked) {
return;
}
}
}
- public override void visit_loop (Loop loop) {
+ public override void visit_loop_statement (LoopStatement loop) {
if (func (loop) == TraverseStatus.CONTINUE) {
loop.accept_children (this);
}
/**
* Represents a while iteration statement in the source code.
*/
-public class Vala.WhileStatement : CodeNode, Statement {
- /**
- * Specifies the loop condition.
- */
- public Expression condition {
- get {
- return _condition;
- }
- private set {
- _condition = value;
- _condition.parent_node = this;
- }
- }
-
- /**
- * Specifies the loop body.
- */
- public Block body {
- get {
- return _body;
- }
- private set {
- _body = value;
- _body.parent_node = this;
- }
- }
-
- private Expression _condition;
- private Block _body;
-
+public class Vala.WhileStatement : Loop, Statement {
/**
* Creates a new while statement.
*
* @return newly created while statement
*/
public WhileStatement (Expression condition, Block body, SourceReference? source_reference = null) {
- this.body = body;
- this.source_reference = source_reference;
- this.condition = condition;
+ base (condition, body, source_reference);
}
public override void accept (CodeVisitor visitor) {
body.accept (visitor);
}
- public override void replace_expression (Expression old_node, Expression new_node) {
- if (condition == old_node) {
- condition = new_node;
- }
- }
-
public override bool check (CodeContext context) {
if (checked) {
return !error;
body.insert_statement (0, if_stmt);
}
- var loop = new Loop (body, source_reference);
+ var loop = new LoopStatement (body, source_reference);
unowned Block parent_block = (Block) parent_node;
parent_block.replace_statement (this, loop);