[vala] codegen: Use builder API for array length



commit ec82664e58419d671ef6527e374efd85008bb31f
Author: Jürg Billeter <j bitron ch>
Date:   Sun Oct 10 12:25:57 2010 +0200

    codegen: Use builder API for array length

 codegen/valaccodearraymodule.vala |   29 +++++++++++++----------------
 1 files changed, 13 insertions(+), 16 deletions(-)
---
diff --git a/codegen/valaccodearraymodule.vala b/codegen/valaccodearraymodule.vala
index dcd6a1c..43bfa7e 100644
--- a/codegen/valaccodearraymodule.vala
+++ b/codegen/valaccodearraymodule.vala
@@ -484,31 +484,28 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
 		var fun = new CCodeFunction ("_vala_array_length", "gint");
 		fun.modifiers = CCodeModifiers.STATIC;
 		fun.add_parameter (new CCodeFormalParameter ("array", "gpointer"));
-		cfile.add_function_declaration (fun);
-
-		var block = new CCodeBlock ();
-
-		var len_decl = new CCodeDeclaration ("int");
-		len_decl.add_declarator (new CCodeVariableDeclarator ("length", new CCodeConstant ("0")));
-		block.add_statement (len_decl);
 
-		var non_null_block = new CCodeBlock ();
-
-		var while_body = new CCodeBlock ();
-		while_body.add_statement (new CCodeExpressionStatement (new CCodeUnaryExpression (CCodeUnaryOperator.POSTFIX_INCREMENT, new CCodeIdentifier ("length"))));
+		push_function (fun);
 
-		var array_element_check = new CCodeElementAccess (new CCodeCastExpression (new CCodeIdentifier ("array"), "gpointer*"), new CCodeConstant ("length"));
-		non_null_block.add_statement (new CCodeWhileStatement (array_element_check, while_body));
+		ccode.add_declaration ("int", new CCodeVariableDeclarator ("length", new CCodeConstant ("0")));
 
 		// return 0 if the array is NULL
 		// avoids an extra NULL check on the caller side
 		var array_check = new CCodeIdentifier ("array");
-		block.add_statement (new CCodeIfStatement (array_check, non_null_block));
+		ccode.open_if (array_check);
 
-		block.add_statement (new CCodeReturnStatement (new CCodeIdentifier ("length")));
+		var array_element_check = new CCodeElementAccess (new CCodeCastExpression (new CCodeIdentifier ("array"), "gpointer*"), new CCodeConstant ("length"));
+		ccode.open_while (array_element_check);
+		ccode.add_expression (new CCodeUnaryExpression (CCodeUnaryOperator.POSTFIX_INCREMENT, new CCodeIdentifier ("length")));
+		ccode.close ();
+
+		ccode.close ();
 
-		fun.block = block;
+		ccode.add_return (new CCodeIdentifier ("length"));
 
+		pop_function ();
+
+		cfile.add_function_declaration (fun);
 		cfile.add_function (fun);
 	}
 



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