Commit d8860522 authored by Jürg Billeter's avatar Jürg Billeter Committed by Jürg Billeter

Drop obsolete foreach support for null-terminated arrays

2009-01-04  Jürg Billeter  <j@bitron.ch>

	* gobject/valaccodecontrolflowmodule.vala:

	Drop obsolete foreach support for null-terminated arrays

	* gobject/valaccodearraymodule.vala:

	Fix array length C expression for null-terminated array fields,
	drop obsolete array_free support for null-terminated arrays

	* gobject/valaccodeassignmentmodule.vala:

	Correctly obey no_array_length when assigning to fields

	* vala/valacodecontext.vala:
	* gobject/valaccodecompiler.vala:
	* compiler/valacompiler.vala:
	* vapi/glib-2.0.vapi:
	* vapigen/valavapigen.vala:

	Use array_null_termianted attribute where appropriate

svn path=/trunk/; revision=2269
parent 20570676
2009-01-04 Jürg Billeter <j@bitron.ch>
* gobject/valaccodecontrolflowmodule.vala:
Drop obsolete foreach support for null-terminated arrays
* gobject/valaccodearraymodule.vala:
Fix array length C expression for null-terminated array fields,
drop obsolete array_free support for null-terminated arrays
* gobject/valaccodeassignmentmodule.vala:
Correctly obey no_array_length when assigning to fields
* vala/valacodecontext.vala:
* gobject/valaccodecompiler.vala:
* compiler/valacompiler.vala:
* vapi/glib-2.0.vapi:
* vapigen/valavapigen.vala:
Use array_null_termianted attribute where appropriate
2009-01-04 Thijs Vermeir <thijsvermeir@gmail.com>
* vapi/packages/vte/vte.metadata:
......
......@@ -27,14 +27,14 @@ class Vala.Compiler {
static string basedir;
static string directory;
static bool version;
[CCode (array_length = false)]
[CCode (array_length = false, array_null_terminated = true)]
[NoArrayLength]
static string[] sources;
[CCode (array_length = false)]
[CCode (array_length = false, array_null_terminated = true)]
[NoArrayLength]
static string[] vapi_directories;
static string library;
[CCode (array_length = false)]
[CCode (array_length = false, array_null_terminated = true)]
[NoArrayLength]
static string[] packages;
static string target_glib;
......@@ -50,7 +50,7 @@ class Vala.Compiler {
static bool non_null_experimental;
static bool disable_dbus_transformation;
static string cc_command;
[CCode (array_length = false)]
[CCode (array_length = false, array_null_terminated = true)]
[NoArrayLength]
static string[] cc_options;
static string dump_tree;
......
......@@ -170,7 +170,32 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
} else if (array_expr.symbol_reference is Field) {
var field = (Field) array_expr.symbol_reference;
if (field.array_null_terminated) {
var carray_expr = (CCodeExpression) array_expr.ccodenode;
var ma = (MemberAccess) array_expr;
CCodeExpression carray_expr = null;
if (field.binding == MemberBinding.INSTANCE) {
var cl = field.parent_symbol as Class;
bool is_gtypeinstance = (cl != null && !cl.is_compact);
string array_cname = field.get_cname ();
CCodeExpression typed_inst = (CCodeExpression) get_ccodenode (ma.inner);
CCodeExpression inst;
if (is_gtypeinstance && field.access == SymbolAccessibility.PRIVATE) {
inst = new CCodeMemberAccess.pointer (typed_inst, "priv");
} else {
inst = typed_inst;
}
if (((TypeSymbol) field.parent_symbol).is_reference_type ()) {
carray_expr = new CCodeMemberAccess.pointer (inst, array_cname);
} else {
carray_expr = new CCodeMemberAccess (inst, array_cname);
}
} else {
carray_expr = new CCodeIdentifier (field.get_cname ());
}
var len_call = new CCodeFunctionCall (new CCodeIdentifier ("g_strv_length"));
len_call.add_argument (carray_expr);
return len_call;
......@@ -343,7 +368,7 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
}
}
private CCodeForStatement get_vala_array_free_loop (bool have_length) {
private CCodeForStatement get_vala_array_free_loop () {
var cbody = new CCodeBlock ();
var cptrarray = new CCodeCastExpression (new CCodeIdentifier ("array"), "gpointer*");
var cea = new CCodeElementAccess (cptrarray, new CCodeIdentifier ("i"));
......@@ -351,16 +376,11 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
var cfreecall = new CCodeFunctionCall (new CCodeIdentifier ("destroy_func"));
cfreecall.add_argument (cea);
CCodeExpression cforcond;
if (have_length) {
var cfreecond = new CCodeBinaryExpression (CCodeBinaryOperator.INEQUALITY, cea, new CCodeConstant ("NULL"));
cforcond = new CCodeBinaryExpression (CCodeBinaryOperator.LESS_THAN, new CCodeIdentifier ("i"), new CCodeIdentifier ("array_length"));
cbody.add_statement (new CCodeIfStatement (cfreecond, new CCodeExpressionStatement (cfreecall)));
} else {
cforcond = new CCodeBinaryExpression (CCodeBinaryOperator.INEQUALITY, cea, new CCodeConstant ("NULL"));
cbody.add_statement (new CCodeExpressionStatement (cfreecall));
}
var cfreecond = new CCodeBinaryExpression (CCodeBinaryOperator.INEQUALITY, cea, new CCodeConstant ("NULL"));
var cforcond = new CCodeBinaryExpression (CCodeBinaryOperator.LESS_THAN, new CCodeIdentifier ("i"), new CCodeIdentifier ("array_length"));
var cfreeblock = new CCodeBlock ();
cfreeblock.add_statement (new CCodeExpressionStatement (cfreecall));
cbody.add_statement (new CCodeIfStatement (cfreecond, cfreeblock));
var cfor = new CCodeForStatement (cforcond, cbody);
cfor.add_initializer (new CCodeAssignment (new CCodeIdentifier ("i"), new CCodeConstant ("0")));
......@@ -383,9 +403,7 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
citdecl.add_declarator (new CCodeVariableDeclarator ("i"));
cdofree.add_statement (citdecl);
var clencheck = new CCodeBinaryExpression (CCodeBinaryOperator.GREATER_THAN_OR_EQUAL, new CCodeIdentifier ("array_length"), new CCodeConstant ("0"));
var ciflen = new CCodeIfStatement (clencheck, get_vala_array_free_loop (true), get_vala_array_free_loop (false));
cdofree.add_statement (ciflen);
cdofree.add_statement (get_vala_array_free_loop ());
var ccondarr = new CCodeBinaryExpression (CCodeBinaryOperator.INEQUALITY, new CCodeIdentifier ("array"), new CCodeConstant ("NULL"));
var ccondfunc = new CCodeBinaryExpression (CCodeBinaryOperator.INEQUALITY, new CCodeIdentifier ("destroy_func"), new CCodeConstant ("NULL"));
......
......@@ -145,7 +145,8 @@ public class Vala.CCodeAssignmentModule : CCodeMemberAccessModule {
bool array = false;
bool instance_delegate = false;
if (assignment.left.value_type is ArrayType) {
array = !(head.get_array_length_cexpression (assignment.left, 1) is CCodeConstant);
var array_field = assignment.left.symbol_reference as Field;
array = (array_field == null || !array_field.no_array_length);
} else if (assignment.left.value_type is DelegateType) {
var delegate_type = (DelegateType) assignment.left.value_type;
instance_delegate = delegate_type.delegate_symbol.has_target;
......@@ -172,8 +173,7 @@ public class Vala.CCodeAssignmentModule : CCodeMemberAccessModule {
if (array_type.rank == 1) {
var array_var = assignment.left.symbol_reference;
if (array_var != null && array_var.is_internal_symbol ()
&& (assignment.left.symbol_reference is LocalVariable
|| assignment.left.symbol_reference is Field)) {
&& (array_var is LocalVariable || array_var is Field)) {
var lhs_array_size = head.get_array_size_cexpression (assignment.left);
var rhs_array_len = head.get_array_length_cexpression (assignment.left, 1);
ccomma.append_expression (new CCodeAssignment (lhs_array_size, rhs_array_len));
......
......@@ -49,7 +49,7 @@ public class Vala.CCodeCompiler {
* @param context a code context
*/
[NoArrayLength]
public void compile (CodeContext context, string? cc_command, [CCode (array_length = false)] string[] cc_options) {
public void compile (CodeContext context, string? cc_command, string[] cc_options) {
string pc = "pkg-config --cflags";
if (!context.compile_only) {
pc += " --libs";
......
......@@ -315,144 +315,61 @@ public class Vala.CCodeControlFlowModule : CCodeMethodModule {
cblock.add_statement (clendecl);
}
if (array_len is CCodeConstant) {
// the array has no length parameter i.e. it is NULL-terminated array
var it_name = "%s_it".printf (stmt.variable_name);
if (current_method != null && current_method.coroutine) {
closure_struct.add_field (collection_type.get_cname (), it_name);
} else {
var citdecl = new CCodeDeclaration (collection_type.get_cname ());
citdecl.add_declarator (new CCodeVariableDeclarator (it_name));
cblock.add_statement (citdecl);
}
var cbody = new CCodeBlock ();
CCodeExpression element_expr = get_variable_cexpression ("*%s".printf (it_name));
var element_type = array_type.element_type.copy ();
element_type.value_owned = false;
element_expr = transform_expression (element_expr, element_type, stmt.type_reference);
cfrag = new CCodeFragment ();
append_temp_decl (cfrag, temp_vars);
cbody.add_statement (cfrag);
temp_vars.clear ();
if (current_method != null && current_method.coroutine) {
closure_struct.add_field (stmt.type_reference.get_cname (), stmt.variable_name);
cbody.add_statement (new CCodeExpressionStatement (new CCodeAssignment (get_variable_cexpression (stmt.variable_name), element_expr)));
} else {
var cdecl = new CCodeDeclaration (stmt.type_reference.get_cname ());
cdecl.add_declarator (new CCodeVariableDeclarator.with_initializer (stmt.variable_name, element_expr));
cbody.add_statement (cdecl);
}
// add array length variable for stacked arrays
if (stmt.type_reference is ArrayType) {
var inner_array_type = (ArrayType) stmt.type_reference;
for (int dim = 1; dim <= inner_array_type.rank; dim++) {
if (current_method != null && current_method.coroutine) {
closure_struct.add_field ("int", head.get_array_length_cname (stmt.variable_name, dim));
cbody.add_statement (new CCodeExpressionStatement (new CCodeAssignment (get_variable_cexpression (head.get_array_length_cname (stmt.variable_name, dim)), new CCodeConstant ("-1"))));
} else {
var cdecl = new CCodeDeclaration ("int");
cdecl.add_declarator (new CCodeVariableDeclarator.with_initializer (head.get_array_length_cname (stmt.variable_name, dim), new CCodeConstant ("-1")));
cbody.add_statement (cdecl);
}
}
}
cbody.add_statement (stmt.body.ccodenode);
var ccond = new CCodeBinaryExpression (CCodeBinaryOperator.INEQUALITY, new CCodeIdentifier ("*%s".printf (it_name)), new CCodeConstant ("NULL"));
var cfor = new CCodeForStatement (ccond, cbody);
cfor.add_initializer (new CCodeAssignment (new CCodeIdentifier (it_name), new CCodeIdentifier (collection_backup.name)));
var it_name = (stmt.variable_name + "_it");
cfor.add_iterator (new CCodeAssignment (new CCodeIdentifier (it_name), new CCodeBinaryExpression (CCodeBinaryOperator.PLUS, new CCodeIdentifier (it_name), new CCodeConstant ("1"))));
cblock.add_statement (cfor);
if (current_method != null && current_method.coroutine) {
closure_struct.add_field ("int", it_name);
} else {
// the array has a length parameter
var it_name = (stmt.variable_name + "_it");
var citdecl = new CCodeDeclaration ("int");
citdecl.add_declarator (new CCodeVariableDeclarator (it_name));
cblock.add_statement (citdecl);
}
if (current_method != null && current_method.coroutine) {
closure_struct.add_field ("int", it_name);
} else {
var citdecl = new CCodeDeclaration ("int");
citdecl.add_declarator (new CCodeVariableDeclarator (it_name));
cblock.add_statement (citdecl);
}
var cbody = new CCodeBlock ();
var cbody = new CCodeBlock ();
CCodeExpression element_expr = new CCodeElementAccess (get_variable_cexpression (collection_backup.name), get_variable_cexpression (it_name));
CCodeExpression element_expr = new CCodeElementAccess (get_variable_cexpression (collection_backup.name), get_variable_cexpression (it_name));
var element_type = array_type.element_type.copy ();
element_type.value_owned = false;
element_expr = transform_expression (element_expr, element_type, stmt.type_reference);
var element_type = array_type.element_type.copy ();
element_type.value_owned = false;
element_expr = transform_expression (element_expr, element_type, stmt.type_reference);
cfrag = new CCodeFragment ();
append_temp_decl (cfrag, temp_vars);
cbody.add_statement (cfrag);
temp_vars.clear ();
cfrag = new CCodeFragment ();
append_temp_decl (cfrag, temp_vars);
cbody.add_statement (cfrag);
temp_vars.clear ();
if (current_method != null && current_method.coroutine) {
closure_struct.add_field (stmt.type_reference.get_cname (), stmt.variable_name);
cbody.add_statement (new CCodeExpressionStatement (new CCodeAssignment (get_variable_cexpression (stmt.variable_name), element_expr)));
} else {
var cdecl = new CCodeDeclaration (stmt.type_reference.get_cname ());
cdecl.add_declarator (new CCodeVariableDeclarator.with_initializer (stmt.variable_name, element_expr));
cbody.add_statement (cdecl);
}
if (current_method != null && current_method.coroutine) {
closure_struct.add_field (stmt.type_reference.get_cname (), stmt.variable_name);
cbody.add_statement (new CCodeExpressionStatement (new CCodeAssignment (get_variable_cexpression (stmt.variable_name), element_expr)));
} else {
var cdecl = new CCodeDeclaration (stmt.type_reference.get_cname ());
cdecl.add_declarator (new CCodeVariableDeclarator.with_initializer (stmt.variable_name, element_expr));
cbody.add_statement (cdecl);
}
// add array length variable for stacked arrays
if (stmt.type_reference is ArrayType) {
var inner_array_type = (ArrayType) stmt.type_reference;
for (int dim = 1; dim <= inner_array_type.rank; dim++) {
if (current_method != null && current_method.coroutine) {
closure_struct.add_field ("int", head.get_array_length_cname (stmt.variable_name, dim));
cbody.add_statement (new CCodeExpressionStatement (new CCodeAssignment (get_variable_cexpression (head.get_array_length_cname (stmt.variable_name, dim)), new CCodeConstant ("-1"))));
} else {
var cdecl = new CCodeDeclaration ("int");
cdecl.add_declarator (new CCodeVariableDeclarator.with_initializer (head.get_array_length_cname (stmt.variable_name, dim), new CCodeConstant ("-1")));
cbody.add_statement (cdecl);
}
// add array length variable for stacked arrays
if (stmt.type_reference is ArrayType) {
var inner_array_type = (ArrayType) stmt.type_reference;
for (int dim = 1; dim <= inner_array_type.rank; dim++) {
if (current_method != null && current_method.coroutine) {
closure_struct.add_field ("int", head.get_array_length_cname (stmt.variable_name, dim));
cbody.add_statement (new CCodeExpressionStatement (new CCodeAssignment (get_variable_cexpression (head.get_array_length_cname (stmt.variable_name, dim)), new CCodeConstant ("-1"))));
} else {
var cdecl = new CCodeDeclaration ("int");
cdecl.add_declarator (new CCodeVariableDeclarator.with_initializer (head.get_array_length_cname (stmt.variable_name, dim), new CCodeConstant ("-1")));
cbody.add_statement (cdecl);
}
}
}
cbody.add_statement (stmt.body.ccodenode);
var ccond_ind1 = new CCodeBinaryExpression (CCodeBinaryOperator.INEQUALITY, array_len, new CCodeConstant ("-1"));
var ccond_ind2 = new CCodeBinaryExpression (CCodeBinaryOperator.LESS_THAN, get_variable_cexpression (it_name), array_len);
var ccond_ind = new CCodeBinaryExpression (CCodeBinaryOperator.AND, ccond_ind1, ccond_ind2);
/* only check for null if the containers elements are of reference-type */
CCodeBinaryExpression ccond;
if (array_type.element_type.is_reference_type_or_type_parameter ()) {
var ccond_term1 = new CCodeBinaryExpression (CCodeBinaryOperator.EQUALITY, array_len, new CCodeConstant ("-1"));
var ccond_term2 = new CCodeBinaryExpression (CCodeBinaryOperator.INEQUALITY, new CCodeElementAccess (new CCodeIdentifier (collection_backup.name), get_variable_cexpression (it_name)), new CCodeConstant ("NULL"));
var ccond_term = new CCodeBinaryExpression (CCodeBinaryOperator.AND, ccond_term1, ccond_term2);
ccond = new CCodeBinaryExpression (CCodeBinaryOperator.OR, ccond_ind, ccond_term);
} else {
/* assert when trying to iterate over value-type arrays of unknown length */
var cassert = new CCodeFunctionCall (new CCodeIdentifier ("g_assert"));
cassert.add_argument (ccond_ind1);
cblock.add_statement (new CCodeExpressionStatement (cassert));
cbody.add_statement (stmt.body.ccodenode);
var ccond = new CCodeBinaryExpression (CCodeBinaryOperator.LESS_THAN, get_variable_cexpression (it_name), array_len);
ccond = ccond_ind2;
}
var cfor = new CCodeForStatement (ccond, cbody);
cfor.add_initializer (new CCodeAssignment (get_variable_cexpression (it_name), new CCodeConstant ("0")));
cfor.add_iterator (new CCodeAssignment (get_variable_cexpression (it_name), new CCodeBinaryExpression (CCodeBinaryOperator.PLUS, get_variable_cexpression (it_name), new CCodeConstant ("1"))));
cblock.add_statement (cfor);
}
var cfor = new CCodeForStatement (ccond, cbody);
cfor.add_initializer (new CCodeAssignment (get_variable_cexpression (it_name), new CCodeConstant ("0")));
cfor.add_iterator (new CCodeAssignment (get_variable_cexpression (it_name), new CCodeBinaryExpression (CCodeBinaryOperator.PLUS, get_variable_cexpression (it_name), new CCodeConstant ("1"))));
cblock.add_statement (cfor);
} else if (stmt.collection.value_type.compatible (new ObjectType (glist_type)) || stmt.collection.value_type.compatible (new ObjectType (gslist_type))) {
// iterating over a GList or GSList
......
/* valacodecontext.vala
*
* Copyright (C) 2006-2008 Jürg Billeter
* Copyright (C) 2006-2009 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
......@@ -380,7 +380,7 @@ public class Vala.CodeContext {
file.mark = 2;
}
public string? get_package_path (string pkg, [CCode (array_length_pos = 1.9)] string[] vapi_directories) {
public string? get_package_path (string pkg, string[] vapi_directories) {
string basename = "%s.vapi".printf (pkg);
string filename = null;
......
......@@ -1787,7 +1787,7 @@ namespace GLib {
public static bool set_variable (string variable, string value, bool overwrite);
[CCode (cname = "g_unsetenv")]
public static void unset_variable (string variable);
[CCode (cname = "g_listenv", array_length = false)]
[CCode (cname = "g_listenv", array_length = false, array_null_terminated = true)]
public static string[] list_variables ();
[CCode (cname = "g_get_user_name")]
public static weak string get_user_name ();
......@@ -1801,7 +1801,7 @@ namespace GLib {
public static weak string get_user_config_dir ();
[CCode (cname = "g_get_user_special_dir")]
public static weak string get_user_special_dir (UserDirectory directory);
[CCode (cname = "g_get_system_data_dirs", array_length = false)]
[CCode (cname = "g_get_system_data_dirs", array_length = false, array_null_terminated = true)]
[NoArrayLength]
public static weak string[] get_system_data_dirs ();
[CCode (cname = "g_get_system_config_dirs")]
......
......@@ -26,14 +26,14 @@ class Vala.VAPIGen : Object {
static string directory;
static bool version;
static bool quiet_mode;
[CCode (array_length = false)]
[CCode (array_length = false, array_null_terminated = true)]
[NoArrayLength]
static string[] sources;
[CCode (array_length = false)]
[CCode (array_length = false, array_null_terminated = true)]
[NoArrayLength]
static string[] vapi_directories;
static string library;
[CCode (array_length = false)]
[CCode (array_length = false, array_null_terminated = true)]
[NoArrayLength]
static string[] packages;
static string metadata_filename;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment