/* valadovaarraymodule.vala
*
- * Copyright (C) 2006-2010 Jürg Billeter
+ * Copyright (C) 2006-2011 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
return;
}
- generate_method_declaration (array_class.default_construction_method, cfile);
+ generate_method_declaration ((Method) array_struct.scope.lookup ("create"), cfile);
- var array_new = new CCodeFunctionCall (new CCodeIdentifier ("dova_array_new"));
+ var array_new = new CCodeFunctionCall (new CCodeIdentifier ("dova_array_create"));
array_new.add_argument (get_type_id_expression (expr.element_type));
// length of new array
// access to element in an array
set_cvalue (expr, new CCodeElementAccess (ccontainer, cindex));
}
+
+ public override void visit_slice_expression (SliceExpression expr) {
+ var ccontainer = get_cvalue (expr.container);
+ var cstart = get_cvalue (expr.start);
+ var cstop = get_cvalue (expr.stop);
+
+ var array_type = (ArrayType) expr.container.value_type;
+
+ var array = new CCodeFunctionCall (new CCodeIdentifier ("dova_array"));
+ array.add_argument (new CCodeBinaryExpression (CCodeBinaryOperator.PLUS, new CCodeCastExpression (new CCodeMemberAccess (ccontainer, "data"), array_type.element_type.get_cname () + "*"), cstart));
+ array.add_argument (new CCodeBinaryExpression (CCodeBinaryOperator.MINUS, cstop, cstart));
+
+ set_cvalue (expr, array);
+ }
}
/* valadovabasemodule.vala
*
- * Copyright (C) 2006-2010 Jürg Billeter
+ * Copyright (C) 2006-2011 Jürg Billeter
* Copyright (C) 2006-2008 Raffaele Sandrini
*
* This library is free software; you can redistribute it and/or
public Class type_class;
public Class value_class;
public Class string_class;
- public Class array_class;
+ public Struct array_struct;
public Class delegate_class;
public Class error_class;
type_class = (Class) dova_ns.scope.lookup ("Type");
value_class = (Class) dova_ns.scope.lookup ("Value");
string_class = (Class) root_symbol.scope.lookup ("string");
- array_class = (Class) dova_ns.scope.lookup ("Array");
+ array_struct = (Struct) dova_ns.scope.lookup ("Array");
delegate_class = (Class) dova_ns.scope.lookup ("Delegate");
error_class = (Class) dova_ns.scope.lookup ("Error");
vardecl.init0 = true;
} else if (!local.variable_type.nullable &&
(st != null && st.get_fields ().size > 0) ||
- (array_type != null && array_type.fixed_length)) {
+ array_type != null) {
// 0-initialize struct with struct initializer { 0 }
// necessary as they will be passed by reference
var clist = new CCodeInitializerList ();
}
public override void visit_delete_statement (DeleteStatement stmt) {
- var pointer_type = (PointerType) stmt.expression.value_type;
- DataType type = pointer_type;
- if (pointer_type.base_type.data_type != null && pointer_type.base_type.data_type.is_reference_type ()) {
- type = pointer_type.base_type;
+ var pointer_type = stmt.expression.value_type as PointerType;
+ var array_type = stmt.expression.value_type as ArrayType;
+
+ if (pointer_type != null) {
+ DataType type = pointer_type;
+ if (pointer_type.base_type.data_type != null && pointer_type.base_type.data_type.is_reference_type ()) {
+ type = pointer_type.base_type;
+ }
+
+ var ccall = new CCodeFunctionCall (get_destroy_func_expression (type));
+ ccall.add_argument (get_cvalue (stmt.expression));
+ ccode.add_expression (ccall);
+ } else if (array_type != null) {
+ // TODO free elements
+ var free_call = new CCodeFunctionCall (new CCodeIdentifier ("free"));
+ free_call.add_argument (new CCodeMemberAccess (get_cvalue (stmt.expression), "data"));
+ ccode.add_expression (free_call);
+
+ ccode.add_assignment (new CCodeMemberAccess (get_cvalue (stmt.expression), "data"), new CCodeConstant ("NULL"));
+ ccode.add_assignment (new CCodeMemberAccess (get_cvalue (stmt.expression), "length"), new CCodeConstant ("0"));
+ } else {
+ assert_not_reached ();
}
-
- var ccall = new CCodeFunctionCall (get_destroy_func_expression (type));
- ccall.add_argument (get_cvalue (stmt.expression));
- ccode.add_expression (ccall);
}
public override void visit_expression (Expression expr) {
return;
}
+ if (expr.inner.value_type is ArrayType && expr.type_reference is PointerType) {
+ var array_type = (ArrayType) expr.inner.value_type;
+ if (!array_type.fixed_length) {
+ set_cvalue (expr, new CCodeMemberAccess (get_cvalue (expr.inner), "data"));
+ return;
+ }
+ }
+
generate_type_declaration (expr.type_reference, cfile);
if (expr.inner.value_type is GenericType && !(expr.type_reference is GenericType)) {
}
if (expression_type is NullType) {
+ if (target_type is ArrayType) {
+ var array = new CCodeFunctionCall (new CCodeIdentifier ("dova_array"));
+ array.add_argument (new CCodeConstant ("NULL"));
+ array.add_argument (new CCodeConstant ("0"));
+ return array;
+ }
+
// null literal, no cast required when not converting to generic type pointer
return cexpr;
}
+ if (expression_type is ArrayType && target_type is PointerType) {
+ var array_type = (ArrayType) expression_type;
+ if (!array_type.inline_allocated) {
+ return new CCodeMemberAccess (cexpr, "data");
+ }
+ }
+
+ if (expression_type is ArrayType && target_type is ArrayType) {
+ var source_array_type = (ArrayType) expression_type;
+ var target_array_type = (ArrayType) target_type;
+ if (source_array_type.inline_allocated && !target_array_type.inline_allocated) {
+ var array = new CCodeFunctionCall (new CCodeIdentifier ("dova_array"));
+ array.add_argument (cexpr);
+
+ var csizeof = new CCodeFunctionCall (new CCodeIdentifier ("sizeof"));
+ csizeof.add_argument (cexpr);
+ var csizeofelement = new CCodeFunctionCall (new CCodeIdentifier ("sizeof"));
+ csizeofelement.add_argument (new CCodeElementAccess (cexpr, new CCodeConstant ("0")));
+ array.add_argument (new CCodeBinaryExpression (CCodeBinaryOperator.DIV, csizeof, csizeofelement));
+
+ return array;
+ }
+ }
+
generate_type_declaration (target_type, cfile);
if (target_type is DelegateType && expression_type is MethodType) {
return memset_call;
} else if (initializer_expression && !type.nullable &&
((st != null && st.get_fields ().size > 0) ||
- (array_type != null && array_type.fixed_length))) {
+ array_type != null)) {
// 0-initialize struct with struct initializer { 0 }
// only allowed as initializer expression in C
var clist = new CCodeInitializerList ();
return clist;
} else if ((type.data_type != null && type.data_type.is_reference_type ())
|| type.nullable
- || type is PointerType || type is DelegateType
- || (array_type != null && !array_type.fixed_length)) {
+ || type is PointerType || type is DelegateType) {
return new CCodeConstant ("NULL");
} else if (type.data_type != null && type.data_type.get_default_value () != null) {
return new CCodeConstant (type.data_type.get_default_value ());
/* valadovamemberaccessmodule.vala
*
- * Copyright (C) 2006-2010 Jürg Billeter
+ * Copyright (C) 2006-2011 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
set_cvalue (expr, new CCodeIdentifier (m.get_cname ()));
}
} else if (expr.symbol_reference is ArrayLengthField) {
- generate_property_accessor_declaration (((Property) array_class.scope.lookup ("length")).get_accessor, cfile);
-
- var ccall = new CCodeFunctionCall (new CCodeIdentifier ("dova_array_get_length"));
- ccall.add_argument (pub_inst);
- set_cvalue (expr, ccall);
+ var array_type = (ArrayType) expr.inner.value_type;
+ if (array_type.fixed_length) {
+ var csizeof = new CCodeFunctionCall (new CCodeIdentifier ("sizeof"));
+ csizeof.add_argument (pub_inst);
+ var csizeofelement = new CCodeFunctionCall (new CCodeIdentifier ("sizeof"));
+ csizeofelement.add_argument (new CCodeElementAccess (pub_inst, new CCodeConstant ("0")));
+ set_cvalue (expr, new CCodeBinaryExpression (CCodeBinaryOperator.DIV, csizeof, csizeofelement));
+ } else {
+ set_cvalue (expr, new CCodeMemberAccess (pub_inst, "length"));
+ }
} else if (expr.symbol_reference is Field) {
var f = (Field) expr.symbol_reference;
expr.target_value = load_field (f, expr.inner != null ? expr.inner.target_value : null);
/* valadovaobjectmodule.vala
*
- * Copyright (C) 2009-2010 Jürg Billeter
+ * Copyright (C) 2009-2011 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
push_function (function);
- if (acc.result_var != null) {
- acc.result_var.accept (this);
- }
-
acc.body.emit (this);
if (acc.readable) {
}
}
- if (m.result_var != null) {
- m.result_var.accept (this);
- }
-
m.body.emit (this);
if (!(m.return_type is VoidType) && !(m.return_type is GenericType)) {
pop_context ();
if (m.entry_point) {
- generate_type_declaration (new ObjectType (array_class), cfile);
+ generate_type_declaration (new StructValueType (array_struct), cfile);
// m is possible entry point, add appropriate startup code
var cmain = new CCodeFunction ("main", "int");
set_cvalue (expr, new CCodeElementAccess (get_cvalue (expr.container), cindex));
}
} else {
- generate_property_accessor_declaration (((Property) array_class.scope.lookup ("data")).get_accessor, cfile);
-
- var ccontainer = new CCodeFunctionCall (new CCodeIdentifier ("dova_array_get_data"));
- ccontainer.add_argument (get_cvalue (expr.container));
+ var ccontainer = new CCodeMemberAccess (get_cvalue (expr.container), "data");
if (array_type.element_type is GenericType) {
// generic array
var array_type = dest.value_type as ArrayType;
if (array_type != null && !array_type.inline_allocated) {
- generate_property_accessor_declaration (((Property) array_class.scope.lookup ("data")).get_accessor, cfile);
-
- var data_call = new CCodeFunctionCall (new CCodeIdentifier ("dova_array_get_data"));
- data_call.add_argument ((CCodeExpression) get_ccodenode (dest));
- cdest = data_call;
+ cdest = new CCodeMemberAccess ((CCodeExpression) get_ccodenode (dest), "data");
} else {
cdest = (CCodeExpression) get_ccodenode (dest);
}
var array_type = src.value_type as ArrayType;
if (array_type != null && !array_type.inline_allocated) {
- generate_property_accessor_declaration (((Property) array_class.scope.lookup ("data")).get_accessor, cfile);
-
- var data_call = new CCodeFunctionCall (new CCodeIdentifier ("dova_array_get_data"));
- data_call.add_argument ((CCodeExpression) get_ccodenode (src));
- csrc = data_call;
+ csrc = new CCodeMemberAccess ((CCodeExpression) get_ccodenode (src), "data");
} else {
csrc = (CCodeExpression) get_ccodenode (src);
}
var right_ea = expr.right as ElementAccess;
if (left_ea != null) {
- generate_property_accessor_declaration (((Property) array_class.scope.lookup ("data")).get_accessor, cfile);
-
- var data_call = new CCodeFunctionCall (new CCodeIdentifier ("dova_array_get_data"));
- data_call.add_argument ((CCodeExpression) get_ccodenode (left_ea.container));
- cleft = data_call;
+ cleft = new CCodeMemberAccess ((CCodeExpression) get_ccodenode (left_ea.container), "data");
left_index = (CCodeExpression) get_ccodenode (left_ea.get_indices ().get (0));
} else {
cleft = (CCodeExpression) get_ccodenode (expr.left);
}
if (right_ea != null) {
- generate_property_accessor_declaration (((Property) array_class.scope.lookup ("data")).get_accessor, cfile);
-
- var data_call = new CCodeFunctionCall (new CCodeIdentifier ("dova_array_get_data"));
- data_call.add_argument ((CCodeExpression) get_ccodenode (right_ea.container));
- cright = data_call;
+ cright = new CCodeMemberAccess ((CCodeExpression) get_ccodenode (right_ea.container), "data");
right_index = (CCodeExpression) get_ccodenode (right_ea.get_indices ().get (0));
} else {
cright = (CCodeExpression) get_ccodenode (expr.right);
if (val_ea != null) {
val = val_ea.container;
- generate_property_accessor_declaration (((Property) array_class.scope.lookup ("data")).get_accessor, cfile);
-
- var data_call = new CCodeFunctionCall (new CCodeIdentifier ("dova_array_get_data"));
- data_call.add_argument ((CCodeExpression) get_ccodenode (val));
- cval = data_call;
+ cval = new CCodeMemberAccess ((CCodeExpression) get_ccodenode (val), "data");
val_index = (CCodeExpression) get_ccodenode (val_ea.get_indices ().get (0));
} else {
cval = (CCodeExpression) get_ccodenode (val);
}
public override void visit_list_literal (ListLiteral expr) {
- var ce = new CCodeCommaExpression ();
+ CCodeExpression ptr;
int length = expr.get_expressions ().size;
if (length == 0) {
- ce.append_expression (new CCodeConstant ("NULL"));
+ ptr = new CCodeConstant ("NULL");
} else {
var array_type = new ArrayType (expr.element_type, 1, expr.source_reference);
array_type.inline_allocated = true;
int i = 0;
foreach (Expression e in expr.get_expressions ()) {
- ce.append_expression (new CCodeAssignment (new CCodeElementAccess (name_cnode, new CCodeConstant (i.to_string ())), get_cvalue (e)));
+ ccode.add_assignment (new CCodeElementAccess (name_cnode, new CCodeConstant (i.to_string ())), get_cvalue (e));
i++;
}
- ce.append_expression (name_cnode);
+ ptr = name_cnode;
}
+ var array_type = new ArrayType (expr.element_type, 1, expr.source_reference);
+
+ var temp_var = get_temp_variable (array_type, true, expr);
+ var name_cnode = get_variable_cexpression (temp_var.name);
+
+ emit_temp_var (temp_var);
+
+ var array_init = new CCodeFunctionCall (new CCodeIdentifier ("dova_array_init"));
+ array_init.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, name_cnode));
+ array_init.add_argument (ptr);
+ array_init.add_argument (new CCodeConstant (length.to_string ()));
+ ccode.add_expression (array_init);
+
var list_creation = new CCodeFunctionCall (new CCodeIdentifier ("dova_list_new"));
list_creation.add_argument (get_type_id_expression (expr.element_type));
- list_creation.add_argument (new CCodeConstant (length.to_string ()));
- list_creation.add_argument (ce);
+ list_creation.add_argument (name_cnode);
set_cvalue (expr, list_creation);
}
/* valaarraytype.vala
*
- * Copyright (C) 2007-2010 Jürg Billeter
+ * Copyright (C) 2007-2011 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
}
public override Symbol? get_member (string member_name) {
- if (CodeContext.get ().profile == Profile.DOVA) {
- return SemanticAnalyzer.symbol_lookup_inherited (CodeContext.get ().root.scope.lookup ("Dova").scope.lookup ("Array"), member_name);
- } else if (member_name == "length") {
+ if (member_name == "length") {
return get_length_field ();
} else if (member_name == "move") {
return get_move_method ();
return element_type.get_cname ();
} else {
if (CodeContext.get ().profile == Profile.DOVA) {
- return "DovaArray*";
+ return "DovaArray";
} else {
return element_type.get_cname () + "*";
}
public override bool is_disposable () {
if (fixed_length) {
return element_type.is_disposable ();
+ } else if (CodeContext.get ().profile == Profile.DOVA) {
+ return false;
} else {
return base.is_disposable ();
}
/* valadeletestatement.vala
*
- * Copyright (C) 2008-2010 Jürg Billeter
+ * Copyright (C) 2008-2011 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
return false;
}
- if (!(expression.value_type is PointerType)) {
+ if (!(expression.value_type is PointerType) && !(expression.value_type is ArrayType)) {
error = true;
Report.error (source_reference, "delete operator not supported for `%s'".printf (expression.value_type.to_string ()));
}