[Vala] [PATCH] Allow access to the dimension sizes of a multidimensional array.
- From: Rob Taylor <rob taylor codethink co uk>
- To: vala-list gnome org
- Subject: [Vala] [PATCH] Allow access to the dimension sizes of a multidimensional array.
- Date: Mon, 10 Mar 2008 10:01:43 +0100
Makes the length field of a multidim array an array of ints, with values
corresponding to the dimensions of the array. This currently only works
with literals for the index into the array.
---
gobject/valaccodeelementaccessbinding.vala | 11 ++++++++++-
gobject/valaccodegenerator.vala | 1 -
gobject/valaccodegeneratormemberaccess.vala | 1 +
tests/arrays.vala | 5 ++++-
vala/valaarray.vala | 9 ++++++++-
5 files changed, 23 insertions(+), 4 deletions(-)
diff --git a/gobject/valaccodeelementaccessbinding.vala
b/gobject/valaccodeelementaccessbinding.vala
index 61836ec..5780192 100644
--- a/gobject/valaccodeelementaccessbinding.vala
+++ b/gobject/valaccodeelementaccessbinding.vala
@@ -43,7 +43,16 @@ public class Vala.CCodeElementAccessBinding :
CCodeExpressionBinding {
var ccontainer = (CCodeExpression) expr.container.ccodenode;
var cindex = (CCodeExpression) indices[0].ccodenode;
- if (container_type == codegen.string_type.data_type) {
+ if (expr.container.symbol_reference is ArrayLengthField) {
+ /* Figure if cindex is a constant expression and calculate dim...*/
+ var lit = indices[0] as LiteralExpression;
+ var memberaccess = expr.container as MemberAccess;
+ if (lit != null &&
+ lit.literal is IntegerLiteral &&
+ memberaccess != null) {
+ codenode = codegen.get_array_length_cexpression
(memberaccess.inner, (lit.literal as IntegerLiteral).value.to_int() + 1);
+ }
+ } else if (container_type == codegen.string_type.data_type) {
// access to unichar in a string
var coffsetcall = new CCodeFunctionCall (new CCodeIdentifier
("g_utf8_offset_to_pointer"));
coffsetcall.add_argument (ccontainer);
diff --git a/gobject/valaccodegenerator.vala
b/gobject/valaccodegenerator.vala
index a8066b7..4d866c9 100644
--- a/gobject/valaccodegenerator.vala
+++ b/gobject/valaccodegenerator.vala
@@ -2353,7 +2353,6 @@ public class Vala.CCodeGenerator : CodeGenerator {
var instance_expression_type = get_data_type_for_symbol
(base_type);
var instance_target_type = get_data_type_for_symbol
((Typesymbol) field.parent_symbol);
CCodeExpression typed_inst = get_implicit_cast_expression
(pub_inst, instance_expression_type, instance_target_type);
-
CCodeExpression inst;
if (field.access == SymbolAccessibility.PRIVATE) {
inst = new CCodeMemberAccess.pointer (typed_inst,
"priv");
diff --git a/gobject/valaccodegeneratormemberaccess.vala
b/gobject/valaccodegeneratormemberaccess.vala
index 4c1a709..13de7aa 100644
--- a/gobject/valaccodegeneratormemberaccess.vala
+++ b/gobject/valaccodegeneratormemberaccess.vala
@@ -71,6 +71,7 @@ public class Vala.CCodeGenerator {
expr.ccodenode = new CCodeIdentifier (m.get_cname ());
}
} else if (expr.symbol_reference is ArrayLengthField) {
+ var array_type = expr.inner.static_type as ArrayType;
expr.ccodenode = get_array_length_cexpression (expr.inner, 1);
} else if (expr.symbol_reference is Field) {
var f = (Field) expr.symbol_reference;
diff --git a/tests/arrays.vala b/tests/arrays.vala
index 0800881..1117caf 100644
--- a/tests/arrays.vala
+++ b/tests/arrays.vala
@@ -241,7 +241,7 @@ class Maman.Foo : Object {
}
static void test_arrays_multi_dimensional () {
- int[,] array = new int[3,2];
+ var array = new int[3,2];
int i = 0;
for (int x = 0; x < 3; x++) {
@@ -256,6 +256,9 @@ class Maman.Foo : Object {
i++;
}
assert (i == 3 * 2);
+
+ assert (array.length[0] == 3);
+ assert (array.length[1] == 2);
}
static void main (string[] args) {
diff --git a/vala/valaarray.vala b/vala/valaarray.vala
index c894cc8..4dc7e43 100644
--- a/vala/valaarray.vala
+++ b/vala/valaarray.vala
@@ -158,7 +158,14 @@ public class Vala.Array : Typesymbol {
length_field.access = SymbolAccessibility.PUBLIC;
var root_symbol = source_reference.file.context.root;
- length_field.type_reference = new ValueType ((Typesymbol)
root_symbol.scope.lookup ("int"));
+ if (rank > 1) {
+ //length is an int[] containing the dimentions of the array,
starting at 0
+ ValueType integer = new ValueType((Typesymbol)
root_symbol.scope.lookup("int"));
+ length_field.type_reference = new ArrayType (integer, 1);
+ length_field.type_reference.add_type_argument (integer);
+ } else {
+ length_field.type_reference = new ValueType ((Typesymbol)
root_symbol.scope.lookup ("int"));
+ }
}
return length_field;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]