3 * Copyright (C) 2007-2009 Jürg Billeter
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 * Jürg Billeter <j@bitron.ch>
28 public class Vala
.ArrayType
: ReferenceType
{
32 public DataType element_type
{
33 get { return _element_type
; }
35 _element_type
= value
;
36 _element_type
.parent_node
= this
;
41 * The rank of this array.
43 public int rank
{ get; set; }
45 private DataType _element_type
;
47 private ArrayLengthField length_field
;
48 private ArrayResizeMethod resize_method
;
49 private ArrayMoveMethod move_method
;
51 public ArrayType (DataType element_type
, int rank
, SourceReference? source_reference
) {
52 this
.element_type
= element_type
;
54 this
.source_reference
= source_reference
;
57 public override Symbol?
get_member (string member_name
) {
58 if (member_name
== "length") {
59 return get_length_field ();
60 } else if (member_name
== "move") {
61 return get_move_method ();
62 } else if (member_name
== "resize") {
63 return get_resize_method ();
68 private ArrayLengthField
get_length_field () {
69 if (length_field
== null) {
70 length_field
= new
ArrayLengthField (source_reference
);
72 length_field
.access
= SymbolAccessibility
.PUBLIC
;
74 var root_symbol
= source_reference
.file
.context
.root
;
76 // length is an int[] containing the dimensions of the array, starting at 0
77 ValueType integer
= new
IntegerType ((Struct
) root_symbol
.scope
.lookup ("int"));
78 length_field
.field_type
= new
ArrayType (integer
, 1, source_reference
);
80 length_field
.field_type
= new
IntegerType ((Struct
) root_symbol
.scope
.lookup ("int"));
87 private ArrayResizeMethod
get_resize_method () {
88 if (resize_method
== null) {
89 resize_method
= new
ArrayResizeMethod (source_reference
);
91 resize_method
.return_type
= new
VoidType ();
92 resize_method
.access
= SymbolAccessibility
.PUBLIC
;
94 resize_method
.set_cname ("g_renew");
96 var root_symbol
= source_reference
.file
.context
.root
;
97 var int_type
= new
IntegerType ((Struct
) root_symbol
.scope
.lookup ("int"));
99 resize_method
.add_parameter (new
FormalParameter ("length", int_type
));
101 resize_method
.returns_modified_pointer
= true;
103 return resize_method
;
106 private ArrayMoveMethod
get_move_method () {
107 if (move_method
== null) {
108 move_method
= new
ArrayMoveMethod (source_reference
);
110 move_method
.return_type
= new
VoidType ();
111 move_method
.access
= SymbolAccessibility
.PUBLIC
;
113 move_method
.set_cname ("_vala_array_move");
115 var root_symbol
= source_reference
.file
.context
.root
;
116 var int_type
= new
IntegerType ((Struct
) root_symbol
.scope
.lookup ("int"));
118 move_method
.add_parameter (new
FormalParameter ("src", int_type
));
119 move_method
.add_parameter (new
FormalParameter ("dest", int_type
));
120 move_method
.add_parameter (new
FormalParameter ("length", int_type
));
125 public override DataType
copy () {
126 var result
= new
ArrayType (element_type
.copy (), rank
, source_reference
);
127 result
.value_owned
= value_owned
;
128 result
.nullable
= nullable
;
129 result
.floating_reference
= floating_reference
;
134 public override string?
get_cname () {
135 // FIXME add support for [Immutable] or [Const] attribute to support arrays to const data
136 return element_type
.get_cname () + "*";
139 public override bool is_array () {
143 public override string to_qualified_string (Scope? scope
) {
144 return "%s[%s]%s".printf (element_type
.to_qualified_string (scope
), string.nfill (rank
- 1, ','), nullable ?
"?" : "");
147 public override bool compatible (DataType target_type
) {
148 if (target_type is PointerType
|| (target_type
.data_type
!= null && target_type
.data_type
.get_attribute ("PointerType") != null)) {
149 /* any array type can be cast to a generic pointer */
153 /* temporarily ignore type parameters */
154 if (target_type
.type_parameter
!= null) {
158 var target_array_type
= target_type as ArrayType
;
159 if (target_array_type
== null) {
163 if (element_type
.compatible (target_array_type
.element_type
)
164 && target_array_type
.element_type
.compatible (element_type
)) {
171 public override bool is_reference_type_or_type_parameter () {
175 public override string?
get_type_signature () {
176 string element_type_signature
= element_type
.get_type_signature ();
178 if (element_type_signature
== null) {
182 return string.nfill (rank
, 'a') + element_type_signature
;
185 public override void accept_children (CodeVisitor visitor
) {
186 element_type
.accept (visitor
);
189 public override void replace_type (DataType old_type
, DataType new_type
) {
190 if (element_type
== old_type
) {
191 element_type
= new_type
;
195 public override Gee
.List
<Symbol
> get_symbols () {
196 return element_type
.get_symbols ();