cfile.add_function (unref_fun);
}
- foreach (Statement stmt in b.get_statements ()) {
+ foreach (Statement stmt in b) {
push_line (stmt.source_reference);
stmt.emit (this);
pop_line ();
valaassignment.vala \
valaattribute.vala \
valabaseaccess.vala \
+ valabasestatement.vala \
valabasicblock.vala \
valabinaryexpression.vala \
valablock.vala \
valasourcelocation.vala \
valasourcereference.vala \
valastatement.vala \
- valastatementlist.vala \
valastringliteral.vala \
valastruct.vala \
valastructvaluetype.vala \
--- /dev/null
+/* valabasestatement.vala
+ *
+ * Copyright (C) 2012 Luca Bruno
+ *
+ * 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:
+ * Luca Bruno <lucabru@src.gnome.org>
+ */
+
+using GLib;
+
+/**
+ * Simple class implementing Statement.
+ */
+public class Vala.BaseStatement : CodeNode, Statement {
+ public Statement prev { get; set; }
+
+ public Statement next { get; set; }
+}
public bool captured { get; set; }
- private List<Statement> statement_list = new ArrayList<Statement> ();
+ public Statement first_statement { get; private set; }
+
+ public Statement prev { get; set; }
+
+ public Statement next { get; set; }
+
+ private Statement last_statement;
+
private List<LocalVariable> local_variables = new ArrayList<LocalVariable> ();
private List<Constant> local_constants = new ArrayList<Constant> ();
*/
public void add_statement (Statement stmt) {
stmt.parent_node = this;
- statement_list.add (stmt);
+ if (last_statement == null) {
+ first_statement = last_statement = stmt;
+ } else {
+ last_statement.next = stmt;
+ stmt.prev = last_statement;
+ last_statement = stmt;
+ }
}
public void insert_statement (int index, Statement stmt) {
- stmt.parent_node = this;
- statement_list.insert (index, stmt);
- }
-
- /**
- * Returns a copy of the list of statements.
- *
- * @return statement list
- */
- public List<Statement> get_statements () {
- var list = new ArrayList<Statement> ();
- foreach (Statement stmt in statement_list) {
- unowned StatementList? stmt_list = stmt as StatementList;
- if (stmt_list != null) {
- for (int i = 0; i < stmt_list.length; i++) {
- list.add (stmt_list.get (i));
- }
- } else {
- list.add (stmt);
- }
+ Statement iter = first_statement;
+ while (iter != null && index > 0) {
+ index--;
+ iter = iter.next;
+ }
+ if (iter == null) {
+ add_statement (stmt);
+ } else {
+ insert_before (iter, stmt);
}
- return list;
}
/**
}
public override void accept_children (CodeVisitor visitor) {
- foreach (Statement stmt in statement_list) {
+ foreach (Statement stmt in this) {
stmt.accept (visitor);
}
}
owner = context.analyzer.get_current_non_local_symbol (parent_node).scope;
- for (int i = 0; i < statement_list.size; i++) {
- if (!statement_list[i].check (context)) {
+ foreach (Statement stmt in this) {
+ if (!stmt.check (context)) {
error = true;
}
}
public override void get_error_types (Collection<DataType> collection, SourceReference? source_reference = null) {
// use get_statements () instead of statement_list to not miss errors within StatementList objects
- foreach (Statement stmt in get_statements ()) {
+ foreach (Statement stmt in this) {
stmt.get_error_types (collection, source_reference);
}
}
}
public void insert_before (Statement stmt, Statement new_stmt) {
- for (int i = 0; i < statement_list.size; i++) {
- var stmt_list = statement_list[i] as StatementList;
- if (stmt_list != null) {
- for (int j = 0; j < stmt_list.length; j++) {
- if (stmt_list.get (j) == stmt) {
- stmt_list.insert (j, new_stmt);
- new_stmt.parent_node = this;
- break;
- }
- }
- } else if (statement_list[i] == stmt) {
- stmt_list = new StatementList (source_reference);
- stmt_list.add (new_stmt);
- stmt_list.add (stmt);
- statement_list[i] = stmt_list;
- new_stmt.parent_node = this;
- }
+ new_stmt.parent_node = this;
+ new_stmt.prev = stmt.prev;
+ new_stmt.next = stmt;
+
+ if (stmt.prev == null) {
+ first_statement = new_stmt;
+ } else {
+ stmt.prev.next = new_stmt;
}
+ stmt.prev = new_stmt;
}
public void replace_statement (Statement old_stmt, Statement new_stmt) {
- for (int i = 0; i < statement_list.size; i++) {
- var stmt_list = statement_list[i] as StatementList;
- if (stmt_list != null) {
- for (int j = 0; j < stmt_list.length; j++) {
- if (stmt_list.get (j) == old_stmt) {
- stmt_list.set (j, new_stmt);
- new_stmt.parent_node = this;
- break;
- }
- }
- } else if (statement_list[i] == old_stmt) {
- statement_list[i] = new_stmt;
- new_stmt.parent_node = this;
- break;
- }
+ new_stmt.parent_node = this;
+ new_stmt.prev = old_stmt.prev;
+ new_stmt.next = old_stmt.next;
+
+ if (old_stmt.prev == null) {
+ first_statement = new_stmt;
+ } else {
+ old_stmt.prev.next = new_stmt;
+ }
+
+ if (old_stmt.next == null) {
+ last_statement = new_stmt;
+ } else {
+ old_stmt.next.prev = new_stmt;
+ }
+ }
+
+ public StatementIterator iterator () {
+ return new StatementIterator (this);
+ }
+}
+
+public class Vala.StatementIterator {
+ private Block block;
+ private Statement next;
+
+ public StatementIterator (Block block) {
+ this.block = block;
+ this.next = block.first_statement;
+ }
+
+ public Statement? next_value () {
+ var ret = next;
+ if (ret != null) {
+ next = ret.next;
}
+ return ret;
}
}
/**
* Represents a break statement in the source code.
*/
-public class Vala.BreakStatement : CodeNode, Statement {
+public class Vala.BreakStatement : BaseStatement {
/**
* Creates a new break statement.
*
public override void visit_block (Block b) {
write_begin_block ();
- foreach (Statement stmt in b.get_statements ()) {
+ foreach (Statement stmt in b) {
stmt.accept (this);
}
/**
* Represents a continue statement in the source code.
*/
-public class Vala.ContinueStatement : CodeNode, Statement {
+public class Vala.ContinueStatement : BaseStatement {
/**
* Creates a new continue statement.
*
/**
* Represents a local variable or constant declaration statement in the source code.
*/
-public class Vala.DeclarationStatement : CodeNode, Statement {
+public class Vala.DeclarationStatement : BaseStatement {
/**
* The local variable or constant declaration.
*/
/**
* Represents a delete statement e.g. "delete a".
*/
-public class Vala.DeleteStatement : CodeNode, Statement {
+public class Vala.DeleteStatement : BaseStatement {
/**
* Expression representing the instance to be freed.
*/
/**
* Represents a do iteration statement in the source code.
*/
-public class Vala.DoStatement : CodeNode, Statement {
+public class Vala.DoStatement : BaseStatement {
/**
* Specifies the loop body.
*/
/**
* An empty statement.
*/
-public class Vala.EmptyStatement : CodeNode, Statement {
+public class Vala.EmptyStatement : BaseStatement {
/**
* Creates a new empty statement.
*
* A code statement that evaluates a given expression. The value computed by the
* expression, if any, is discarded.
*/
-public class Vala.ExpressionStatement : CodeNode, Statement {
+public class Vala.ExpressionStatement : BaseStatement {
/**
* Specifies the expression to evaluate.
*/
current_block = new BasicBlock ();
all_basic_blocks.add (current_block);
condition_block.connect (current_block);
- foreach (Statement section_stmt in section.get_statements ()) {
+ foreach (Statement section_stmt in section) {
section_stmt.accept (this);
}
* Represents a foreach statement in the source code. Foreach statements iterate
* over the elements of a collection.
*/
-public class Vala.ForeachStatement : CodeNode, Statement {
+public class Vala.ForeachStatement : BaseStatement {
/**
* Specifies the element type.
*/
/**
* Represents a for iteration statement in the source code.
*/
-public class Vala.ForStatement : CodeNode, Statement {
+public class Vala.ForStatement : BaseStatement {
/**
* Specifies the loop condition.
*/
/**
* Represents an if selection statement in the source code.
*/
-public class Vala.IfStatement : CodeNode, Statement {
+public class Vala.IfStatement : BaseStatement {
/**
* The boolean condition to evaluate.
*/
* occurs. Otherwise it's translated into a try/finally statement which unlocks the mutex
* after the block is finished.
*/
-public class Vala.LockStatement : CodeNode, Statement {
+public class Vala.LockStatement : BaseStatement {
/**
* Expression representing the resource to be locked.
*/
/**
* Represents an endless loop.
*/
-public class Vala.Loop : CodeNode, Statement {
+public class Vala.Loop : BaseStatement {
/**
* Specifies the loop body.
*/
/**
* Represents a return statement in the source code.
*/
-public class Vala.ReturnStatement : CodeNode, Statement {
+public class Vala.ReturnStatement : BaseStatement {
/**
* The optional expression to return.
*/
* Interface for all statement types.
*/
public interface Vala.Statement : CodeNode {
+ public abstract Statement prev { get; set; }
+ public abstract Statement next { get; set; }
}
+++ /dev/null
-/* valastatementlist.vala
- *
- * Copyright (C) 2008-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>
- */
-
-
-public class Vala.StatementList : CodeNode, Statement {
- private List<Statement> list = new ArrayList<Statement> ();
-
- public int length {
- get { return list.size; }
- }
-
- public StatementList (SourceReference source_reference) {
- this.source_reference = source_reference;
- }
-
- public Statement get (int index) {
- return list.get (index);
- }
-
- public void set (int index, Statement stmt) {
- list.set (index, stmt);
- }
-
- public void add (Statement stmt) {
- list.add (stmt);
- }
-
- public void insert (int index, Statement stmt) {
- list.insert (index, stmt);
- }
-
- public override void get_error_types (Collection<DataType> collection, SourceReference? source_reference = null) {
- foreach (var stmt in list) {
- stmt.get_error_types (collection, source_reference);
- }
- }
-
- public override void accept (CodeVisitor visitor) {
- foreach (Statement stmt in list) {
- stmt.accept (visitor);
- }
- }
-
- public override bool check (CodeContext context) {
- foreach (Statement stmt in list) {
- if (!stmt.check (context)) {
- return false;
- }
- }
- return true;
- }
-
- public override void emit (CodeGenerator codegen) {
- foreach (Statement stmt in list) {
- stmt.emit (codegen);
- }
- }
-}
/**
* Represents a switch selection statement in the source code.
*/
-public class Vala.SwitchStatement : CodeNode, Statement {
+public class Vala.SwitchStatement : BaseStatement {
/**
* Specifies the switch expression.
*/
/**
* Represents a throw statement in the source code.
*/
-public class Vala.ThrowStatement : CodeNode, Statement {
+public class Vala.ThrowStatement : BaseStatement {
/**
* The error expression to throw.
*/
/**
* Represents a try statement in the source code.
*/
-public class Vala.TryStatement : CodeNode, Statement {
+public class Vala.TryStatement : BaseStatement {
/**
* Specifies the body of the try statement.
*/
/**
* Represents an unlock statement e.g. {{{ unlock (a); }}}.
*/
-public class Vala.UnlockStatement : CodeNode, Statement {
+public class Vala.UnlockStatement : BaseStatement {
/**
* Expression representing the resource to be unlocked.
*/
/**
* Represents a while iteration statement in the source code.
*/
-public class Vala.WhileStatement : CodeNode, Statement {
+public class Vala.WhileStatement : BaseStatement {
/**
* Specifies the loop condition.
*/
/**
* Represents a yield statement in the source code.
*/
-public class Vala.YieldStatement : CodeNode, Statement {
+public class Vala.YieldStatement : BaseStatement {
/**
* Creates a new yield statement.
*