Commit 7e38cfff authored by Jürg Billeter's avatar Jürg Billeter Committed by Jürg Billeter

Move array dup wrapper generation to CCodeArrayModule

2008-11-04  Jürg Billeter  <j@bitron.ch>

	* gobject/valaccodearraymodule.vala:
	* gobject/valaccodebasemodule.vala:

	Move array dup wrapper generation to CCodeArrayModule

svn path=/trunk/; revision=1977
parent c1c5dba7
2008-11-04 Jürg Billeter <j@bitron.ch>
* gobject/valaccodearraymodule.vala:
* gobject/valaccodebasemodule.vala:
Move array dup wrapper generation to CCodeArrayModule
2008-11-04 Jürg Billeter <j@bitron.ch>
* gobject/valaccodebasemodule.vala:
......
......@@ -28,6 +28,8 @@ using Gee;
* The link between an assignment and generated code.
*/
public class Vala.CCodeArrayModule : CCodeInvocationExpressionModule {
private int next_array_dup_id = 0;
public CCodeArrayModule (CCodeGenerator codegen, CCodeModule? next) {
base (codegen, next);
}
......@@ -396,4 +398,84 @@ public class Vala.CCodeArrayModule : CCodeInvocationExpressionModule {
source_type_member_definition.append (fun);
}
public override CCodeExpression? get_dup_func_expression (DataType type, SourceReference? source_reference) {
if (type is ArrayType) {
return new CCodeIdentifier (generate_array_dup_wrapper ((ArrayType) type));
} else {
return base.get_dup_func_expression (type, source_reference);
}
}
string generate_array_dup_wrapper (ArrayType array_type) {
string dup_func = "_vala_array_dup%d".printf (++next_array_dup_id);
if (!add_wrapper (dup_func)) {
// wrapper already defined
return dup_func;
}
// declaration
var function = new CCodeFunction (dup_func, array_type.get_cname ());
function.modifiers = CCodeModifiers.STATIC;
function.add_parameter (new CCodeFormalParameter ("self", array_type.get_cname ()));
// total length over all dimensions
function.add_parameter (new CCodeFormalParameter ("length", "int"));
// definition
var block = new CCodeBlock ();
if (requires_copy (array_type.element_type)) {
var old_temp_vars = temp_vars;
var cdecl = new CCodeDeclaration (array_type.get_cname ());
var cvardecl = new CCodeVariableDeclarator ("result");
cdecl.add_declarator (cvardecl);
var gnew = new CCodeFunctionCall (new CCodeIdentifier ("g_new0"));
gnew.add_argument (new CCodeIdentifier (array_type.element_type.get_cname ()));
gnew.add_argument (new CCodeIdentifier ("length"));
cvardecl.initializer = gnew;
block.add_statement (cdecl);
var idx_decl = new CCodeDeclaration ("int");
idx_decl.add_declarator (new CCodeVariableDeclarator ("i"));
block.add_statement (idx_decl);
var loop_body = new CCodeBlock ();
loop_body.add_statement (new CCodeExpressionStatement (new CCodeAssignment (new CCodeElementAccess (new CCodeIdentifier ("result"), new CCodeIdentifier ("i")), get_ref_cexpression (array_type.element_type, new CCodeElementAccess (new CCodeIdentifier ("self"), new CCodeIdentifier ("i")), null, array_type))));
var cfor = new CCodeForStatement (new CCodeBinaryExpression (CCodeBinaryOperator.LESS_THAN, new CCodeIdentifier ("i"), new CCodeIdentifier ("length")), loop_body);
cfor.add_initializer (new CCodeAssignment (new CCodeIdentifier ("i"), new CCodeConstant ("0")));
cfor.add_iterator (new CCodeUnaryExpression (CCodeUnaryOperator.POSTFIX_INCREMENT, new CCodeIdentifier ("i")));
block.add_statement (cfor);
block.add_statement (new CCodeReturnStatement (new CCodeIdentifier ("result")));
var cfrag = new CCodeFragment ();
append_temp_decl (cfrag, temp_vars);
block.add_statement (cfrag);
temp_vars = old_temp_vars;
} else {
var dup_call = new CCodeFunctionCall (new CCodeIdentifier ("g_memdup"));
dup_call.add_argument (new CCodeIdentifier ("self"));
var sizeof_call = new CCodeFunctionCall (new CCodeIdentifier ("sizeof"));
sizeof_call.add_argument (new CCodeIdentifier (array_type.element_type.get_cname ()));
dup_call.add_argument (new CCodeBinaryExpression (CCodeBinaryOperator.MUL, new CCodeIdentifier ("length"), sizeof_call));
block.add_statement (new CCodeReturnStatement (dup_call));
}
// append to file
source_type_member_declaration.append (function.copy ());
function.block = block;
source_type_member_definition.append (function);
return dup_func;
}
}
......@@ -78,7 +78,6 @@ public class Vala.CCodeBaseModule : CCodeModule {
public Gee.Set<string> c_keywords;
public int next_temp_var_id = 0;
private int next_array_dup_id = 0;
public bool in_creation_method = false;
public bool in_constructor = false;
public bool in_static_or_class_ctor = false;
......@@ -1570,12 +1569,12 @@ public class Vala.CCodeBaseModule : CCodeModule {
}
}
public CCodeExpression? get_dup_func_expression (DataType type, SourceReference? source_reference) {
var cl = type.data_type as Class;
public virtual CCodeExpression? get_dup_func_expression (DataType type, SourceReference? source_reference) {
if (type is ErrorType) {
return new CCodeIdentifier ("g_error_copy");
} else if (type.data_type != null) {
string dup_function;
var cl = type.data_type as Class;
if (type.data_type.is_reference_counting ()) {
dup_function = type.data_type.get_ref_function ();
if (type.data_type is Interface && dup_function == null) {
......@@ -1601,8 +1600,6 @@ public class Vala.CCodeBaseModule : CCodeModule {
} else if (type.type_parameter != null && current_type_symbol is Class) {
string func_name = "%s_dup_func".printf (type.type_parameter.name.down ());
return new CCodeMemberAccess.pointer (new CCodeMemberAccess.pointer (new CCodeIdentifier ("self"), "priv"), func_name);
} else if (type is ArrayType) {
return new CCodeIdentifier (generate_array_dup_wrapper ((ArrayType) type));
} else if (type is PointerType) {
var pointer_type = (PointerType) type;
return get_dup_func_expression (pointer_type.base_type, source_reference);
......@@ -1611,78 +1608,6 @@ public class Vala.CCodeBaseModule : CCodeModule {
}
}
string generate_array_dup_wrapper (ArrayType array_type) {
string dup_func = "_vala_array_dup%d".printf (++next_array_dup_id);
if (!add_wrapper (dup_func)) {
// wrapper already defined
return dup_func;
}
// declaration
var function = new CCodeFunction (dup_func, array_type.get_cname ());
function.modifiers = CCodeModifiers.STATIC;
function.add_parameter (new CCodeFormalParameter ("self", array_type.get_cname ()));
// total length over all dimensions
function.add_parameter (new CCodeFormalParameter ("length", "int"));
// definition
var block = new CCodeBlock ();
if (requires_copy (array_type.element_type)) {
var old_temp_vars = temp_vars;
var cdecl = new CCodeDeclaration (array_type.get_cname ());
var cvardecl = new CCodeVariableDeclarator ("result");
cdecl.add_declarator (cvardecl);
var gnew = new CCodeFunctionCall (new CCodeIdentifier ("g_new0"));
gnew.add_argument (new CCodeIdentifier (array_type.element_type.get_cname ()));
gnew.add_argument (new CCodeIdentifier ("length"));
cvardecl.initializer = gnew;
block.add_statement (cdecl);
var idx_decl = new CCodeDeclaration ("int");
idx_decl.add_declarator (new CCodeVariableDeclarator ("i"));
block.add_statement (idx_decl);
var loop_body = new CCodeBlock ();
loop_body.add_statement (new CCodeExpressionStatement (new CCodeAssignment (new CCodeElementAccess (new CCodeIdentifier ("result"), new CCodeIdentifier ("i")), get_ref_cexpression (array_type.element_type, new CCodeElementAccess (new CCodeIdentifier ("self"), new CCodeIdentifier ("i")), null, array_type))));
var cfor = new CCodeForStatement (new CCodeBinaryExpression (CCodeBinaryOperator.LESS_THAN, new CCodeIdentifier ("i"), new CCodeIdentifier ("length")), loop_body);
cfor.add_initializer (new CCodeAssignment (new CCodeIdentifier ("i"), new CCodeConstant ("0")));
cfor.add_iterator (new CCodeUnaryExpression (CCodeUnaryOperator.POSTFIX_INCREMENT, new CCodeIdentifier ("i")));
block.add_statement (cfor);
block.add_statement (new CCodeReturnStatement (new CCodeIdentifier ("result")));
var cfrag = new CCodeFragment ();
append_temp_decl (cfrag, temp_vars);
block.add_statement (cfrag);
temp_vars = old_temp_vars;
} else {
var dup_call = new CCodeFunctionCall (new CCodeIdentifier ("g_memdup"));
dup_call.add_argument (new CCodeIdentifier ("self"));
var sizeof_call = new CCodeFunctionCall (new CCodeIdentifier ("sizeof"));
sizeof_call.add_argument (new CCodeIdentifier (array_type.element_type.get_cname ()));
dup_call.add_argument (new CCodeBinaryExpression (CCodeBinaryOperator.MUL, new CCodeIdentifier ("length"), sizeof_call));
block.add_statement (new CCodeReturnStatement (dup_call));
}
// append to file
source_type_member_declaration.append (function.copy ());
function.block = block;
source_type_member_definition.append (function);
return dup_func;
}
private string generate_struct_dup_wrapper (ValueType value_type) {
string dup_func = "_%sdup".printf (value_type.type_symbol.get_lower_case_cprefix ());
......@@ -2962,7 +2887,7 @@ public class Vala.CCodeBaseModule : CCodeModule {
return true;
}
private CCodeExpression? get_ref_cexpression (DataType expression_type, CCodeExpression cexpr, Expression? expr, CodeNode node) {
public CCodeExpression? get_ref_cexpression (DataType expression_type, CCodeExpression cexpr, Expression? expr, CodeNode node) {
if (expression_type is ValueType && !expression_type.nullable) {
// normal value type, no null check
// (copy (&expr, &temp), temp)
......
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