]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
Add Subroutine class
authorJürg Billeter <j@bitron.ch>
Fri, 29 Oct 2010 06:01:32 +0000 (08:01 +0200)
committerJürg Billeter <j@bitron.ch>
Fri, 29 Oct 2010 06:01:32 +0000 (08:01 +0200)
vala/Makefile.am
vala/valaconstructor.vala
vala/valadestructor.vala
vala/valaflowanalyzer.vala
vala/valamethod.vala
vala/valapropertyaccessor.vala
vala/valasubroutine.vala [new file with mode: 0644]

index e66e992ebb85b2eeb8315fb5b4d3ab3a9bed0136..b20c8328e9ad342035c6ebff59a36c0d469a902d 100644 (file)
@@ -136,6 +136,7 @@ libvalacore_la_VALASOURCES = \
        valastringliteral.vala \
        valastruct.vala \
        valastructvaluetype.vala \
+       valasubroutine.vala \
        valaswitchlabel.vala \
        valaswitchsection.vala \
        valaswitchstatement.vala \
index 05a86711c6ddd7c2ea0bc57500c6cb0f15419511..6c6d9ddec782b8262937268ef82ecfb0a8848a33 100644 (file)
@@ -25,12 +25,7 @@ using GLib;
 /**
  * Represents a class or instance constructor.
  */
-public class Vala.Constructor : Symbol {
-       /**
-        * The body of this constructor.
-        */
-       public Block body { get; set; }
-       
+public class Vala.Constructor : Subroutine {
        /**
         * Specifies the generated `this` parameter for instance methods.
         */
index 79b93b5b7885af95b367269211274c8cfb4242ff..b051ca5a0666e84dccea4a132ed8c8224cd5d909 100644 (file)
@@ -25,12 +25,7 @@ using GLib;
 /**
  * Represents a class or instance destructor.
  */
-public class Vala.Destructor : Symbol {
-       /**
-        * The body of this constructor.
-        */
-       public Block body { get; set; }
-       
+public class Vala.Destructor : Subroutine {
        /**
         * Specifies the generated `this` parameter for instance methods.
         */
index 148cc9c680e778edd044be833f004befe284bef4..da9c33500f8b2830a59b660462d9bdc5d2834478 100644 (file)
@@ -173,6 +173,10 @@ public class Vala.FlowAnalyzer : CodeVisitor {
                        }
                }
 
+               visit_subroutine (m);
+       }
+
+       void visit_subroutine (Subroutine m) {
                if (m.body == null) {
                        return;
                }
@@ -183,7 +187,7 @@ public class Vala.FlowAnalyzer : CodeVisitor {
 
                m.return_block.connect (m.exit_block);
 
-               if (context.profile == Profile.DOVA && !(m.return_type is VoidType)) {
+               if (context.profile == Profile.DOVA && m.result_var != null) {
                        // ensure result is defined at end of method
                        var result_ma = new MemberAccess.simple ("result", m.source_reference);
                        result_ma.symbol_reference = m.result_var;
@@ -204,8 +208,8 @@ public class Vala.FlowAnalyzer : CodeVisitor {
                if (current_block != null) {
                        // end of method body reachable
 
-                       if (context.profile != Profile.DOVA && !(m.return_type is VoidType)) {
-                               Report.error (m.source_reference, "missing return statement at end of method or lambda body");
+                       if (context.profile != Profile.DOVA && m.result_var != null) {
+                               Report.error (m.source_reference, "missing return statement at end of subroutine body");
                                m.error = true;
                        }
 
@@ -499,45 +503,7 @@ public class Vala.FlowAnalyzer : CodeVisitor {
        }
 
        public override void visit_property_accessor (PropertyAccessor acc) {
-               if (acc.body == null) {
-                       return;
-               }
-
-               acc.entry_block = new BasicBlock.entry ();
-               acc.return_block = new BasicBlock ();
-               acc.exit_block = new BasicBlock.exit ();
-
-               acc.return_block.connect (acc.exit_block);
-
-               if (context.profile == Profile.DOVA && acc.readable) {
-                       // ensure result is defined at end of method
-                       var result_ma = new MemberAccess.simple ("result", acc.source_reference);
-                       result_ma.symbol_reference = acc.result_var;
-                       acc.return_block.add_node (result_ma);
-               }
-
-               current_block = new BasicBlock ();
-               acc.entry_block.connect (current_block);
-
-               jump_stack.add (new JumpTarget.return_target (acc.return_block));
-               jump_stack.add (new JumpTarget.exit_target (acc.exit_block));
-
-               acc.accept_children (this);
-
-               jump_stack.remove_at (jump_stack.size - 1);
-
-               if (current_block != null) {
-                       // end of property accessor body reachable
-
-                       if (context.profile != Profile.DOVA && acc.readable) {
-                               Report.error (acc.source_reference, "missing return statement at end of property getter body");
-                               acc.error = true;
-                       }
-
-                       current_block.connect (acc.return_block);
-               }
-
-               analyze_body (acc.entry_block);
+               visit_subroutine (acc);
        }
 
        public override void visit_block (Block b) {
index f8fe91633248b9e20249e3b3cdd5bf58b8a6c0de..e86741c010a2be9881cbfe3ea27dbf11787299e9 100644 (file)
@@ -27,7 +27,7 @@ using GLib;
 /**
  * Represents a type or namespace method.
  */
-public class Vala.Method : Symbol {
+public class Vala.Method : Subroutine {
        List<TypeParameter> type_parameters;
 
        public const string DEFAULT_SENTINEL = "NULL";
@@ -42,22 +42,6 @@ public class Vala.Method : Symbol {
                        _return_type.parent_node = this;
                }
        }
-       
-       public Block body {
-               get { return _body; }
-               set {
-                       _body = value;
-                       if (_body != null) {
-                               _body.owner = scope;
-                       }
-               }
-       }
-
-       public BasicBlock entry_block { get; set; }
-
-       public BasicBlock return_block { get; set; }
-
-       public BasicBlock exit_block { get; set; }
 
        /**
         * Specifies whether this method may only be called with an instance of
@@ -159,11 +143,6 @@ public class Vala.Method : Symbol {
         */
        public Parameter this_parameter { get; set; }
 
-       /**
-        * Specifies the generated `result` variable for postconditions.
-        */
-       public LocalVariable result_var { get; set; }
-
        /**
         * Specifies the position of the instance parameter in the C function.
         */
@@ -243,7 +222,6 @@ public class Vala.Method : Symbol {
        private List<Expression> preconditions;
        private List<Expression> postconditions;
        private DataType _return_type;
-       private Block _body;
 
        private weak Method _base_method;
        private Method _base_interface_method;
index 5579e8bd9c81bf64db31c6fa6ea10c5ec9d9f21c..678eb4cf9d53f0cae010783b82f5435d12ddc047 100644 (file)
@@ -25,7 +25,7 @@ using GLib;
 /**
  * Represents a get or set accessor of a property in the source code.
  */
-public class Vala.PropertyAccessor : Symbol {
+public class Vala.PropertyAccessor : Subroutine {
        /**
         * The corresponding property.
         */
@@ -62,25 +62,6 @@ public class Vala.PropertyAccessor : Symbol {
         */
        public bool construction { get; set; }
 
-       /**
-        * The accessor body.
-        */
-       public Block? body {
-               get { return _body; }
-               set {
-                       _body = value;
-                       if (_body != null) {
-                               _body.owner = scope;
-                       }
-               }
-       }
-
-       public BasicBlock entry_block { get; set; }
-
-       public BasicBlock return_block { get; set; }
-
-       public BasicBlock exit_block { get; set; }
-
        /**
         * True if the body was automatically generated
         */
@@ -91,11 +72,6 @@ public class Vala.PropertyAccessor : Symbol {
         */
        public Parameter value_parameter { get; set; }
 
-       /**
-        * Specifies the generated `result' variable in a get accessor.
-        */
-       public LocalVariable? result_var { get; set; }
-
        public virtual string get_default_cname () {
                var t = (TypeSymbol) prop.parent_symbol;
 
@@ -119,7 +95,6 @@ public class Vala.PropertyAccessor : Symbol {
 
        private DataType _value_type;
        private string? _cname;
-       private Block _body;
        
        /**
         * Creates a new property accessor.
diff --git a/vala/valasubroutine.vala b/vala/valasubroutine.vala
new file mode 100644 (file)
index 0000000..c481d15
--- /dev/null
@@ -0,0 +1,50 @@
+/* valasubroutine.vala
+ *
+ * Copyright (C) 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 abstract class Vala.Subroutine : Symbol {
+       Block _body;
+
+       public BasicBlock entry_block { get; set; }
+
+       public BasicBlock return_block { get; set; }
+
+       public BasicBlock exit_block { get; set; }
+
+       /**
+        * Specifies the generated `result` variable for postconditions.
+        */
+       public LocalVariable result_var { get; set; }
+
+       protected Subroutine (string? name, SourceReference? source_reference, Comment? comment = null) {
+               base (name, source_reference, comment);
+       }
+
+       public Block body {
+               get { return _body; }
+               set {
+                       _body = value;
+                       if (_body != null) {
+                               _body.owner = scope;
+                       }
+               }
+       }
+}