From: Jürg Billeter Date: Sun, 10 Oct 2010 10:25:57 +0000 (+0200) Subject: codegen: Use builder API for array length X-Git-Tag: 0.11.1~137 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ec82664e58419d671ef6527e374efd85008bb31f;p=thirdparty%2Fvala.git codegen: Use builder API for array length --- diff --git a/codegen/valaccodearraymodule.vala b/codegen/valaccodearraymodule.vala index dcd6a1c91..43bfa7efc 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); }