1 /* valaelementaccess.vala
3 * Copyright (C) 2006-2009 Jürg Billeter
4 * Copyright (C) 2006-2008 Raffaele Sandrini
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 * Raffaele Sandrini <raffaele@sandrini.ch>
22 * Jürg Billeter <j@bitron.ch>
28 * Represents an array access expression e.g. "a[1,2]".
30 public class Vala
.ElementAccess
: Expression
{
32 * Expression representing the container on wich we want to access.
34 public Expression container
{
40 _container
.parent_node
= this
;
45 * Expressions representing the indices we want to access inside the container.
47 private List
<Expression
> indices
= new ArrayList
<Expression
> ();
49 Expression _container
;
51 public void append_index (Expression index
) {
53 index
.parent_node
= this
;
56 public List
<Expression
> get_indices () {
60 public ElementAccess (Expression container
, SourceReference source_reference
) {
61 this
.source_reference
= source_reference
;
62 this
.container
= container
;
65 public override void accept (CodeVisitor visitor
) {
66 visitor
.visit_element_access (this
);
68 visitor
.visit_expression (this
);
71 public override void accept_children (CodeVisitor visitor
) {
72 container
.accept (visitor
);
73 foreach (Expression e
in indices
) {
78 public override void replace_expression (Expression old_node
, Expression new_node
) {
79 if (container
== old_node
) {
83 int index
= indices
.index_of (old_node
);
84 if (index
>= 0 && new_node
.parent_node
== null) {
85 indices
[index
] = new_node
;
86 new_node
.parent_node
= this
;
90 public override bool is_pure () {
91 foreach (Expression index
in indices
) {
92 if (!index
.is_pure ()) {
96 return container
.is_pure ();
99 public override bool check (SemanticAnalyzer analyzer
) {
106 if (!container
.check (analyzer
)) {
107 /* don't proceed if a child expression failed */
112 if (container
.value_type
== null) {
114 Report
.error (container
.source_reference
, "Invalid container expression");
118 var container_type
= container
.value_type
.data_type
;
120 if (container is MemberAccess
&& container
.symbol_reference is Signal
) {
121 // signal detail access
122 if (get_indices ().size
!= 1) {
124 Report
.error (source_reference
, "Element access with more than one dimension is not supported for signals");
127 get_indices ().get (0).target_type
= analyzer
.string_type
.copy ();
130 foreach (Expression index
in get_indices ()) {
131 index
.check (analyzer
);
134 bool index_int_type_check
= true;
136 var pointer_type
= container
.value_type as PointerType
;
138 /* assign a value_type when possible */
139 if (container
.value_type is ArrayType
) {
140 var array_type
= (ArrayType
) container
.value_type
;
141 value_type
= array_type
.element_type
.copy ();
143 value_type
.value_owned
= false;
145 } else if (pointer_type
!= null && !pointer_type
.base_type
.is_reference_type_or_type_parameter ()) {
146 value_type
= pointer_type
.base_type
.copy ();
147 } else if (analyzer
.context
.profile
== Profile
.GOBJECT
&& container_type
== analyzer
.string_type
.data_type
) {
148 if (get_indices ().size
!= 1) {
150 Report
.error (source_reference
, "Element access with more than one dimension is not supported for strings");
154 value_type
= analyzer
.unichar_type
;
155 } else if (analyzer
.context
.profile
== Profile
.DOVA
&& container_type
== analyzer
.tuple_type
.data_type
) {
156 if (get_indices ().size
!= 1) {
158 Report
.error (source_reference
, "Element access with more than one dimension is not supported for tuples");
161 var index
= get_indices ().get (0) as IntegerLiteral
;
164 Report
.error (source_reference
, "Element access with non-literal index is not supported for tuples");
167 int i
= index
.value
.to_int ();
168 if (container
.value_type
.get_type_arguments ().size
== 0) {
170 Report
.error (source_reference
, "Element access is not supported for untyped tuples");
173 if (i
< 0 || i
>= container
.value_type
.get_type_arguments ().size
) {
175 Report
.error (source_reference
, "Index out of range");
179 value_type
= container
.value_type
.get_type_arguments ().get (i
);
181 // replace element access by call to generic get method
182 var ma
= new
MemberAccess (container
, "get");
183 ma
.add_type_argument (value_type
);
184 var get_call
= new
MethodCall (ma
);
185 get_call
.add_argument (index
);
186 get_call
.target_type
= this
.target_type
;
187 parent_node
.replace_expression (this
, get_call
);
188 return get_call
.check (analyzer
);
189 } else if (container is MemberAccess
&& container
.symbol_reference is Signal
) {
190 index_int_type_check
= false;
192 symbol_reference
= container
.symbol_reference
;
193 value_type
= container
.value_type
;
196 var set_method
= container
.value_type
.get_member ("set") as Method
;
197 var assignment
= parent_node as Assignment
;
198 if (set_method
!= null && assignment
!= null) {
202 var get_method
= container
.value_type
.get_member ("get") as Method
;
203 if (get_method
!= null) {
204 var get_call
= new
MethodCall (new
MemberAccess (container
, "get"));
205 foreach (Expression e
in get_indices ()) {
206 get_call
.add_argument (e
);
208 get_call
.target_type
= this
.target_type
;
209 parent_node
.replace_expression (this
, get_call
);
210 return get_call
.check (analyzer
);
215 Report
.error (source_reference
, "The expression `%s' does not denote an array".printf (container
.value_type
.to_string ()));
218 if (index_int_type_check
) {
219 /* check if the index is of type integer */
220 foreach (Expression e
in get_indices ()) {
221 /* don't proceed if a child expression failed */
222 if (e
.value_type
== null) {
226 /* check if the index is of type integer */
227 if (!(e
.value_type is IntegerType
|| e
.value_type is EnumValueType
)) {
229 Report
.error (e
.source_reference
, "Expression of integer type expected");
237 public override void get_defined_variables (Collection
<LocalVariable
> collection
) {
238 container
.get_defined_variables (collection
);
239 foreach (Expression index
in indices
) {
240 index
.get_defined_variables (collection
);
244 public override void get_used_variables (Collection
<LocalVariable
> collection
) {
245 container
.get_used_variables (collection
);
246 foreach (Expression index
in indices
) {
247 index
.get_used_variables (collection
);