]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Use builder API for array length
authorJürg Billeter <j@bitron.ch>
Sun, 10 Oct 2010 10:25:57 +0000 (12:25 +0200)
committerJürg Billeter <j@bitron.ch>
Sun, 10 Oct 2010 10:46:19 +0000 (12:46 +0200)
codegen/valaccodearraymodule.vala

index dcd6a1c91435e7fd3a66829f7387770b8396c469..43bfa7efc9b3a2e3068cffb341c745d47a4f09aa 100644 (file)
@@ -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);
        }