]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
DOM-like linked list of statements
authorLuca Bruno <lucabru@src.gnome.org>
Thu, 5 Jan 2012 14:33:32 +0000 (15:33 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Wed, 1 Apr 2020 08:17:51 +0000 (10:17 +0200)
27 files changed:
codegen/valaccodebasemodule.vala
vala/Makefile.am
vala/valabasestatement.vala [new file with mode: 0644]
vala/valablock.vala
vala/valabreakstatement.vala
vala/valacodewriter.vala
vala/valacontinuestatement.vala
vala/valadeclarationstatement.vala
vala/valadeletestatement.vala
vala/valadostatement.vala
vala/valaemptystatement.vala
vala/valaexpressionstatement.vala
vala/valaflowanalyzer.vala
vala/valaforeachstatement.vala
vala/valaforstatement.vala
vala/valaifstatement.vala
vala/valalockstatement.vala
vala/valaloop.vala
vala/valareturnstatement.vala
vala/valastatement.vala
vala/valastatementlist.vala [deleted file]
vala/valaswitchstatement.vala
vala/valathrowstatement.vala
vala/valatrystatement.vala
vala/valaunlockstatement.vala
vala/valawhilestatement.vala
vala/valayieldstatement.vala

index c7066f96c0476903351cd37446d391629ff30762..c68e9863dde4f0fd1f48e7c789845a68cb1f1520 100644 (file)
@@ -2354,7 +2354,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                        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 ();
index 2f0653003df7493913f473348c5694517e4a0c8a..ad17c847c0260d61346fb22845b4e53a71357fd0 100644 (file)
@@ -36,6 +36,7 @@ libvala_la_VALASOURCES = \
        valaassignment.vala \
        valaattribute.vala \
        valabaseaccess.vala \
+       valabasestatement.vala \
        valabasicblock.vala \
        valabinaryexpression.vala \
        valablock.vala \
@@ -151,7 +152,6 @@ libvala_la_VALASOURCES = \
        valasourcelocation.vala \
        valasourcereference.vala \
        valastatement.vala \
-       valastatementlist.vala \
        valastringliteral.vala \
        valastruct.vala \
        valastructvaluetype.vala \
diff --git a/vala/valabasestatement.vala b/vala/valabasestatement.vala
new file mode 100644 (file)
index 0000000..33d2516
--- /dev/null
@@ -0,0 +1,32 @@
+/* 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; }
+}
index 3035bc77bb58f37a9eb561f23d3b5b189b67051f..0a4c544d70fb4ad7264ba24f373b78494f952b40 100644 (file)
@@ -34,7 +34,14 @@ public class Vala.Block : Symbol, Statement {
 
        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> ();
 
@@ -54,32 +61,26 @@ public class Vala.Block : Symbol, Statement {
         */
        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;
        }
 
        /**
@@ -141,7 +142,7 @@ public class Vala.Block : Symbol, Statement {
        }
 
        public override void accept_children (CodeVisitor visitor) {
-               foreach (Statement stmt in statement_list) {
+               foreach (Statement stmt in this) {
                        stmt.accept (visitor);
                }
        }
@@ -155,8 +156,8 @@ public class Vala.Block : Symbol, Statement {
 
                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;
                        }
                }
@@ -174,7 +175,7 @@ public class Vala.Block : Symbol, Statement {
 
        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);
                }
        }
@@ -184,42 +185,55 @@ public class Vala.Block : Symbol, Statement {
        }
 
        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;
        }
 }
index e8f2dd8bf17f8e6bece9a8b7b6ccbb460a383a65..692a077ca880725595f97aeca0b4025fb736ca5d 100644 (file)
@@ -25,7 +25,7 @@ using GLib;
 /**
  * Represents a break statement in the source code.
  */
-public class Vala.BreakStatement : CodeNode, Statement {
+public class Vala.BreakStatement : BaseStatement {
        /**
         * Creates a new break statement.
         *
index 28ad9fd3471b285ac3dbfcf949e25df7896e51b4..f41107b7f6450de284d84ad1c46fb3e10e537650 100644 (file)
@@ -973,7 +973,7 @@ public class Vala.CodeWriter : CodeVisitor {
        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);
                }
 
index dc9c93950794281c49252fe7aaada8a617377ffb..a15f90516f83f82c0879e05c2320d8f6d284d70c 100644 (file)
@@ -25,7 +25,7 @@ using GLib;
 /**
  * Represents a continue statement in the source code.
  */
-public class Vala.ContinueStatement : CodeNode, Statement {
+public class Vala.ContinueStatement : BaseStatement {
        /**
         * Creates a new continue statement.
         *
index ebc2e36247328c13ec414010721230647279ac3c..703065a759b89b135c1137c9d261d5877309a2fa 100644 (file)
@@ -24,7 +24,7 @@
 /**
  * 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.
         */
index 6cac168b2a18e6184396dc355a4f41c61bc2e118..807d94ad039637b1e6aed6c0c481d0a6b750809d 100644 (file)
@@ -23,7 +23,7 @@
 /**
  * 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.
         */
index 23e8982da17b406997a79595c0e4656608500b75..f19f9b62d11e1a55276135751bcf0103ac6a3eb2 100644 (file)
@@ -25,7 +25,7 @@ using GLib;
 /**
  * Represents a do iteration statement in the source code.
  */
-public class Vala.DoStatement : CodeNode, Statement {
+public class Vala.DoStatement : BaseStatement {
        /**
         * Specifies the loop body.
         */
index 259574ac4350d70ee21ec143993a14d8395db443..334b5f6287eafd511f1472854cb65a8670db6e85 100644 (file)
@@ -25,7 +25,7 @@ using GLib;
 /**
  * An empty statement.
  */
-public class Vala.EmptyStatement : CodeNode, Statement {
+public class Vala.EmptyStatement : BaseStatement {
        /**
         * Creates a new empty statement.
         *
index 0de6571f2e99edd160655e20e335c9a445f88546..1de48f7a6ba489f20e154251cda6f819ab4ef1f3 100644 (file)
@@ -25,7 +25,7 @@
  * 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.
         */
index c3531359eb76aa7fef1b64c585b6a5dffe6cb8b6..4b22bccb86854341391fcacab6b133c232eb6b15 100644 (file)
@@ -669,7 +669,7 @@ public class Vala.FlowAnalyzer : CodeVisitor {
                        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);
                        }
 
index 920f7d61f443b3cdc9cb44e798ac30deed6cac76..6b94bfbedbdeb8084255d708b0245604f6efaf2a 100644 (file)
@@ -25,7 +25,7 @@
  * 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.
         */
index 0434ec616fc43c7b88169f4869a587367eb0e0a4..468c21b537766a781b8e4d2b428e4d83a7ef8b92 100644 (file)
@@ -25,7 +25,7 @@ using GLib;
 /**
  * Represents a for iteration statement in the source code.
  */
-public class Vala.ForStatement : CodeNode, Statement {
+public class Vala.ForStatement : BaseStatement {
        /**
         * Specifies the loop condition.
         */
index 9f36ff7d22cd80a9c0459fd10dcf72bc368ead1a..058455d3d2306b5c879c4a64d5df21d1f681951e 100644 (file)
@@ -25,7 +25,7 @@ using GLib;
 /**
  * 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.
         */
index 42363d7df01a8ed7749cb78c35305b35e2670e58..2e5f0e0d49df91d98648fdf4fee9f35820edfaf7 100644 (file)
@@ -32,7 +32,7 @@ using GLib;
  * 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.
         */
index ce6894633d4ac2ce8825e4eb259bdcd3620c96e4..862ef25a4eb85d205eb91e6ebef35e21f4fdca9f 100644 (file)
@@ -25,7 +25,7 @@ using GLib;
 /**
  * Represents an endless loop.
  */
-public class Vala.Loop : CodeNode, Statement {
+public class Vala.Loop : BaseStatement {
        /**
         * Specifies the loop body.
         */
index e79e047a93df7b6057dfdc7dd3d28a81efed3794..012a7be625101c79f459fa1a1cbeac15ba40bd63 100644 (file)
@@ -24,7 +24,7 @@
 /**
  * Represents a return statement in the source code.
  */
-public class Vala.ReturnStatement : CodeNode, Statement {
+public class Vala.ReturnStatement : BaseStatement {
        /**
         * The optional expression to return.
         */
index a35d279561a90bc573e03c16f6b72c2325d74f6c..ccbee29e424c040a5e474a0b4042065079b2ba2f 100644 (file)
@@ -26,4 +26,6 @@ using GLib;
  * Interface for all statement types.
  */
 public interface Vala.Statement : CodeNode {
+       public abstract Statement prev { get; set; }
+       public abstract Statement next { get; set; }
 }
diff --git a/vala/valastatementlist.vala b/vala/valastatementlist.vala
deleted file mode 100644 (file)
index f773a5c..0000000
+++ /dev/null
@@ -1,77 +0,0 @@
-/* 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);
-               }
-       }
-}
index 39c95d0f335908a93bfad800abbba11abe464db1..351b6ea8e9803d0139d8eab842c42f99d48bce8c 100644 (file)
@@ -25,7 +25,7 @@ using GLib;
 /**
  * Represents a switch selection statement in the source code.
  */
-public class Vala.SwitchStatement : CodeNode, Statement {
+public class Vala.SwitchStatement : BaseStatement {
        /**
         * Specifies the switch expression.
         */
index ae73dcb7555dd56c5eff6e1b0b2ec72ed864a746..07eecff45967cc6802b4ad0cb347f9e37b5a1d27 100644 (file)
@@ -24,7 +24,7 @@
 /**
  * Represents a throw statement in the source code.
  */
-public class Vala.ThrowStatement : CodeNode, Statement {
+public class Vala.ThrowStatement : BaseStatement {
        /**
         * The error expression to throw.
         */
index 7cbc78a27d64023fa25387837a30ee5c51064022..ecba490018c0e27fbff9229c79c9abd58c4f4c89 100644 (file)
@@ -25,7 +25,7 @@ using GLib;
 /**
  * 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.
         */
index fb77552c3b9e95a9fff3e31c4b94a63dab824244..f53f5cf49517b6bc4835f7a2bb9dbd9aeffffeaa 100644 (file)
@@ -24,7 +24,7 @@
 /**
  * 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.
         */
index 75db4b96ce3bb5a59f3f715fad024f13c7ec7b13..0f82e4ffa349b359d446cea40ed203bc6805f713 100644 (file)
@@ -25,7 +25,7 @@ using GLib;
 /**
  * Represents a while iteration statement in the source code.
  */
-public class Vala.WhileStatement : CodeNode, Statement {
+public class Vala.WhileStatement : BaseStatement {
        /**
         * Specifies the loop condition.
         */
index 2e9838e92e30933fb3938f1cce186e929162f73e..75e7de417a897f3ddc5963a48089db4d6888de77 100644 (file)
@@ -23,7 +23,7 @@
 /**
  * Represents a yield statement in the source code.
  */
-public class Vala.YieldStatement : CodeNode, Statement {
+public class Vala.YieldStatement : BaseStatement {
        /**
         * Creates a new yield statement.
         *