]> 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>
Mon, 11 Mar 2019 12:52:38 +0000 (13:52 +0100)
28 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/valaswitchsection.vala
vala/valaswitchstatement.vala
vala/valathrowstatement.vala
vala/valatrystatement.vala
vala/valaunlockstatement.vala
vala/valawhilestatement.vala
vala/valayieldstatement.vala

index a7d867af418c063b023c560f1d344d45d928daef..d407b0738a4ddb6e056587ac1afa514da021f3d4 100644 (file)
@@ -2309,7 +2309,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 d341e76599f9582c5f2a35af3aba50d4dea6d86f..2c13f48d63d90d9339aab2d00d8f405ac2fd32d9 100644 (file)
@@ -35,6 +35,7 @@ libvala_la_VALASOURCES = \
        valaassignment.vala \
        valaattribute.vala \
        valabaseaccess.vala \
+       valabasestatement.vala \
        valabasicblock.vala \
        valabinaryexpression.vala \
        valablock.vala \
@@ -146,7 +147,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 72e7801e2e66e4faefb45c992eb9fa2952e08e6e..44d8f8424a8c7ebe57b80ee9910b94b83d360fbe 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) {
-                       var 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++) {
-                       statement_list[i].check (context);
+               foreach (var stmt in this) {
+                       stmt.check (context);
                }
 
                foreach (LocalVariable local in get_local_variables ()) {
@@ -172,7 +173,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);
                }
        }
@@ -182,42 +183,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 f3967a15c6623b6f223d27e6264b5a484ef0c53c..a2325e3799559f29890ed9d54888c784ae9ef738 100644 (file)
@@ -916,7 +916,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 ab652e90673b5488227f4355195069fa60cda3ac..1fea1cfbbf8c30090dfaa7576cc3f643cd5e23cc 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 ff3ff68913f507d03fd53fcc05e26d359acefdfe..426db5df67ecfad03ad375a0201df0b9b8debeff 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 b65ac2b7f2409c7de286c00705d7f037fb734533..dfe96102df8eb25afd58df392fe29b614208b196 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 43cebbe626d14d27089cc03248a1e0760d1f7b39..736c6b7fe3e9127b55ed88d7377751506ca94cd8 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 b21707e884bee8f24588f9c7db7e58f70d21664e..3810cffe0214586a0adbbfeb96897c9eb3273ae9 100644 (file)
@@ -682,7 +682,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 f8b3e6cc510f37ef0213a4c3f7d0a10c6c0f1ba4..f03102441c087a27612b23962ce922ba3d27ac48 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 380effcc5e248f5218ea9ad4594074e6eb6beaf6..31c79abfc9a72588a4494cea5d62a6cd886e1d88 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 61c87ba3fe90dee4c3bacdfa9750338d557b9e90..b251ab3109240b2524a8572995d8b1ae4ccf1ebc 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 be942acee71b04da5102ab02e988f14bb60da2c6..b6b99c3490b3da16d8d78175e662c2dbadcde566 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 e4fa6b506421c3481d71e6d71a1471c3ef246a3e..7db428f05e2a417a68bf5e8c89c9779050e92e7d 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 9e94edea10741cadbdc944a6dba2175bdbbe174b..3141436cd6d144159ece44b347b9e08e83dd77ee 100644 (file)
@@ -80,14 +80,14 @@ public class Vala.SwitchSection : Block {
                        label.accept (visitor);
                }
 
-               foreach (Statement st in get_statements ()) {
+               foreach (Statement st in this) {
                        st.accept (visitor);
                }
        }
 
        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 (var stmt in get_statements ()) {
+               foreach (var stmt in this) {
                        stmt.get_error_types (collection, source_reference);
                }
        }
@@ -105,7 +105,7 @@ public class Vala.SwitchSection : Block {
                        label.check (context);
                }
 
-               foreach (Statement st in get_statements ()) {
+               foreach (Statement st in this) {
                        st.check (context);
                }
 
index 78cfa3d82fcf19f54b8dd146660f1c1a6d994482..b571541081e9de864adfe2e0a6d1e9d88cdfe6b9 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 9349b18188cf3917a89c2f515c31a269211926be..5061a2c0dec64165d86d574088d25a863a636335 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 fcacfc7fba3c2df114e417160d988f2d7462d4fd..d28e6535e2ce55cfb02095656ba93320378e619b 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 e97458ea8e8648698b184239ac029e4b9d97bddb..045b0150e4d64266185ab6b7d4244d35742921c0 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 963764bb9dbdbc4a7f92749121e436546cdfbfbb..ee5b4e43c91ce43fd81d0bbe6686a2045b6ac614 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 795de27610417c7cc3815de08e6e1c8c4673c76d..f114b48fb1a9b50fc3897028df34f1ebc5881047 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.
         *