[vala] Add Subroutine class



commit d540dfd41156d01f530bfaa9121c19fdc6b25421
Author: Jürg Billeter <j bitron ch>
Date:   Fri Oct 29 08:01:32 2010 +0200

    Add Subroutine class

 vala/Makefile.am               |    1 +
 vala/valaconstructor.vala      |    7 +----
 vala/valadestructor.vala       |    7 +----
 vala/valaflowanalyzer.vala     |   50 ++++++---------------------------------
 vala/valamethod.vala           |   24 +------------------
 vala/valapropertyaccessor.vala |   27 +--------------------
 vala/valasubroutine.vala       |   50 ++++++++++++++++++++++++++++++++++++++++
 7 files changed, 63 insertions(+), 103 deletions(-)
---
diff --git a/vala/Makefile.am b/vala/Makefile.am
index e66e992..b20c832 100644
--- a/vala/Makefile.am
+++ b/vala/Makefile.am
@@ -136,6 +136,7 @@ libvalacore_la_VALASOURCES = \
 	valastringliteral.vala \
 	valastruct.vala \
 	valastructvaluetype.vala \
+	valasubroutine.vala \
 	valaswitchlabel.vala \
 	valaswitchsection.vala \
 	valaswitchstatement.vala \
diff --git a/vala/valaconstructor.vala b/vala/valaconstructor.vala
index 05a8671..6c6d9dd 100644
--- a/vala/valaconstructor.vala
+++ b/vala/valaconstructor.vala
@@ -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.
 	 */
diff --git a/vala/valadestructor.vala b/vala/valadestructor.vala
index 79b93b5..b051ca5 100644
--- a/vala/valadestructor.vala
+++ b/vala/valadestructor.vala
@@ -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.
 	 */
diff --git a/vala/valaflowanalyzer.vala b/vala/valaflowanalyzer.vala
index 148cc9c..da9c335 100644
--- a/vala/valaflowanalyzer.vala
+++ b/vala/valaflowanalyzer.vala
@@ -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) {
diff --git a/vala/valamethod.vala b/vala/valamethod.vala
index f8fe916..e86741c 100644
--- a/vala/valamethod.vala
+++ b/vala/valamethod.vala
@@ -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
@@ -160,11 +144,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.
 	 */
 	public double cinstance_parameter_position { get; set; }
@@ -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;
diff --git a/vala/valapropertyaccessor.vala b/vala/valapropertyaccessor.vala
index 5579e8b..678eb4c 100644
--- a/vala/valapropertyaccessor.vala
+++ b/vala/valapropertyaccessor.vala
@@ -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.
 	 */
@@ -63,25 +63,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
 	 */
 	public bool automatic_body { get; set; }
@@ -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
index 0000000..c481d15
--- /dev/null
+++ b/vala/valasubroutine.vala
@@ -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;
+			}
+		}
+	}
+}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]