[vala/wip/transform: 20/49] Wrapper methods



commit 8beea79796677745ea1b24cc28cb9b452b992a05
Author: Luca Bruno <lucabru src gnome org>
Date:   Tue Jan 3 11:22:18 2012 +0100

    Wrapper methods

 codegen/valagvarianttransformer.vala |   29 +++++++---
 vala/valacodebuilder.vala            |  110 ++++++++++++++++++++-------------
 vala/valastatementlist.vala          |    9 +++
 3 files changed, 97 insertions(+), 51 deletions(-)
---
diff --git a/codegen/valagvarianttransformer.vala b/codegen/valagvarianttransformer.vala
index 32377c1..e2b71c7 100644
--- a/codegen/valagvarianttransformer.vala
+++ b/codegen/valagvarianttransformer.vala
@@ -155,14 +155,31 @@ public class Vala.GVariantTransformer : CodeTransformer {
 		return ret;
 	}
 
+	Method wrapper_method (DataType return_type) {
+		var name = CodeNode.get_temp_name ().replace (".", "");
+		name = "_vala_func_"+name;
+		var m = new Method (name, return_type, b.source_reference);
+		context.root.add_method (m);
+		m.access = SymbolAccessibility.PRIVATE;
+		return m;
+	}
+
 	Expression serialize_array (ArrayType array_type, Expression array_expr) {
-		string temp = b.add_temp_declaration (array_type, array_expr);
+		var m = wrapper_method (data_type ("GLib.Variant", true));
+		m.add_parameter (new Parameter ("array", copy_type (array_type, false), b.source_reference));
+		b.push_method (m);
 
 		string[] indices = new string[array_type.rank];
 		for (int dim=1; dim <= array_type.rank; dim++) {
 			indices[dim-1] = b.add_temp_declaration (null, new IntegerLiteral ("0"));
 		}
-		return serialize_array_dim (array_type, 1, indices, temp);
+		b.add_return (serialize_array_dim (array_type, 1, indices, "array"));
+
+		b.pop_method ();
+		check (m);
+		var call = (MethodCall) expression (m.name+"()");
+		call.add_argument (array_expr);
+		return call;
 	}
 
 	Expression serialize_array_dim (ArrayType array_type, int dim, string[] indices, string array_var) {
@@ -447,9 +464,7 @@ public class Vala.GVariantTransformer : CodeTransformer {
 		result.target_type = target_type;
 		context.analyzer.replaced_nodes.add (expr);
 		old_parent_node.replace_expression (expr, result);
-		foreach (var node in b.check_nodes) {
-			check (node);
-		}
+		b.check (this);
 		check (result);
 	}
 
@@ -470,9 +485,7 @@ public class Vala.GVariantTransformer : CodeTransformer {
 		result.target_type = target_type;
 		context.analyzer.replaced_nodes.add (expr);
 		old_parent_node.replace_expression (expr, result);
-		foreach (var node in b.check_nodes) {
-			check (node);
-		}
+		b.check (this);
 		check (result);
 	}
 }
diff --git a/vala/valacodebuilder.vala b/vala/valacodebuilder.vala
index c15fc05..c04bf28 100644
--- a/vala/valacodebuilder.vala
+++ b/vala/valacodebuilder.vala
@@ -23,87 +23,111 @@
 using GLib;
 
 public class Vala.CodeBuilder {
-	Block current_block;
-	Statement insert_statement;
-	Block insert_block;
+	class BuildContext {
+		public Block current_block;
+		public Statement insert_statement;
+		public Block insert_block;
+		public ArrayList<Statement> statement_stack = new ArrayList<Statement> ();
+		public ArrayList<CodeNode> check_nodes = new ArrayList<CodeNode> ();
+	}
+
+	BuildContext build_context;
+	ArrayList<BuildContext> context_stack = new ArrayList<BuildContext> ();
 	public SourceReference source_reference;
-	ArrayList<Statement> statement_stack = new ArrayList<Statement> ();
-	public ArrayList<CodeNode> check_nodes = new ArrayList<CodeNode> ();
 
 	public CodeBuilder (CodeContext context, Statement insert_statement, SourceReference source_reference) {
-		this.current_block = new Block (source_reference);
-		this.insert_statement = insert_statement;
-		this.insert_block = context.analyzer.get_insert_block (insert_statement);
+		build_context = new BuildContext ();
+		build_context.current_block = new Block (source_reference);
+		build_context.insert_statement = insert_statement;
+		build_context.insert_block = context.analyzer.get_insert_block (insert_statement);
 		this.source_reference = source_reference;
 
 		var statement_block = context.analyzer.get_current_block (insert_statement);
-		statement_block.insert_before (insert_statement, current_block);
-		this.insert_statement = current_block;
+		statement_block.insert_before (build_context.insert_statement, build_context.current_block);
+		build_context.insert_statement = build_context.current_block;
+		build_context.check_nodes.add (build_context.current_block);
+	}
+
+	public void check (CodeTransformer transformer) {
+		foreach (var node in build_context.check_nodes) {
+			transformer.check (node);
+		}
+	}
+
+	public void push_method (Method m) {
+		context_stack.add (build_context);
+		build_context = new BuildContext ();
+		build_context.insert_block = m.body = new Block (source_reference);
+		build_context.insert_statement = build_context.current_block = new Block (source_reference);
+		m.body.add_statement (build_context.current_block);
+	}
 
-		check_nodes.add (current_block);
+	public void pop_method () {
+		build_context = context_stack[context_stack.size - 1];
+		context_stack.remove_at (context_stack.size - 1);
 	}
 
 	public void open_block () {
-		statement_stack.add (current_block);
-		var parent_block = current_block;
+		build_context.statement_stack.add (build_context.current_block);
+		var parent_block = build_context.current_block;
 
-		current_block = new Block (source_reference);
+		build_context.current_block = new Block (source_reference);
 
-		parent_block.add_statement (current_block);
+		parent_block.add_statement (build_context.current_block);
 	}
 
 	public void open_if (Expression condition) {
-		statement_stack.add (current_block);
-		var parent_block = current_block;
+		build_context.statement_stack.add (build_context.current_block);
+		var parent_block = build_context.current_block;
 
-		current_block = new Block (source_reference);
+		build_context.current_block = new Block (source_reference);
 
-		var stmt = new IfStatement (condition, current_block, null, source_reference);
-		statement_stack.add (stmt);
+		var stmt = new IfStatement (condition, build_context.current_block, null, source_reference);
+		build_context.statement_stack.add (stmt);
 
 		parent_block.add_statement (stmt);
 	}
 
 	public void add_else () {
-		current_block = new Block (source_reference);
+		build_context.current_block = new Block (source_reference);
 
-		var stmt = (IfStatement) statement_stack[statement_stack.size-1];
+		var stmt = (IfStatement) build_context.statement_stack[build_context.statement_stack.size-1];
 		assert (stmt.false_statement == null);
-		stmt.false_statement = current_block;
+		stmt.false_statement = build_context.current_block;
 	}
 
 	public void else_if (Expression condition) {
-		var parent_if = (IfStatement) statement_stack[statement_stack.size - 1];
+		var parent_if = (IfStatement) build_context.statement_stack[build_context.statement_stack.size - 1];
 		assert (parent_if.false_statement == null);
 
-		statement_stack.remove_at (statement_stack.size - 1);
+		build_context.statement_stack.remove_at (build_context.statement_stack.size - 1);
 
-		current_block = new Block (source_reference);
+		build_context.current_block = new Block (source_reference);
 
-		var stmt = new IfStatement (condition, current_block, null, source_reference);
+		var stmt = new IfStatement (condition, build_context.current_block, null, source_reference);
 		var block = new Block (source_reference);
 		block.add_statement (stmt);
 		parent_if.false_statement = block;
-		statement_stack.add (stmt);
+		build_context.statement_stack.add (stmt);
 	}
 
 	public void open_while (Expression condition) {
-		statement_stack.add (current_block);
-		var parent_block = current_block;
+		build_context.statement_stack.add (build_context.current_block);
+		var parent_block = build_context.current_block;
 
-		current_block = new Block (source_reference);
+		build_context.current_block = new Block (source_reference);
 
-		var stmt = new WhileStatement (condition, current_block, source_reference);
+		var stmt = new WhileStatement (condition, build_context.current_block, source_reference);
 		parent_block.add_statement (stmt);
 	}
 
 	public void open_for (Expression? initializer, Expression condition, Expression? iterator) {
-		statement_stack.add (current_block);
-		var parent_block = current_block;
+		build_context.statement_stack.add (build_context.current_block);
+		var parent_block = build_context.current_block;
 
-		current_block = new Block (source_reference);
+		build_context.current_block = new Block (source_reference);
 
-		var stmt = new ForStatement (condition, current_block, source_reference);
+		var stmt = new ForStatement (condition, build_context.current_block, source_reference);
 		if (initializer != null) {
 			stmt.add_initializer (initializer);
 		}
@@ -115,7 +139,7 @@ public class Vala.CodeBuilder {
 	}
 
 	public void add_statement (Statement statement) {
-		current_block.add_statement (statement);
+		build_context.current_block.add_statement (statement);
 	}
 
 	public void add_expression (Expression expression) {
@@ -141,16 +165,16 @@ public class Vala.CodeBuilder {
 	public string add_temp_declaration (DataType? type, Expression? initializer) {
 		var local = new LocalVariable (type, CodeNode.get_temp_name (), initializer, source_reference);
 		var stmt = new DeclarationStatement (local, source_reference);
-		insert_block.insert_before (insert_statement, stmt);
-		check_nodes.insert (0, stmt);
+		build_context.insert_block.insert_before (build_context.insert_statement, stmt);
+		build_context.check_nodes.insert (0, stmt);
 		return local.name;
 	}
 
 	public void close () {
 		do {
-			var top = statement_stack[statement_stack.size - 1];
-			statement_stack.remove_at (statement_stack.size - 1);
-			current_block = top as Block;
-		} while (current_block == null);
+			var top = build_context.statement_stack[build_context.statement_stack.size - 1];
+			build_context.statement_stack.remove_at (build_context.statement_stack.size - 1);
+			build_context.current_block = top as Block;
+		} while (build_context.current_block == null);
 	}
 }
diff --git a/vala/valastatementlist.vala b/vala/valastatementlist.vala
index 5159598..f773a5c 100644
--- a/vala/valastatementlist.vala
+++ b/vala/valastatementlist.vala
@@ -60,6 +60,15 @@ public class Vala.StatementList : CodeNode, Statement {
 		}
 	}
 
+	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);



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