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 () {
57 return new ReadOnlyList
<Expression
> (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 (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 (container is MemberAccess
&& container
.symbol_reference is Signal
) {
156 index_int_type_check
= false;
158 symbol_reference
= container
.symbol_reference
;
159 value_type
= container
.value_type
;
162 var set_method
= container
.value_type
.get_member ("set") as Method
;
163 var assignment
= parent_node as Assignment
;
164 if (set_method
!= null && assignment
!= null) {
168 var get_method
= container
.value_type
.get_member ("get") as Method
;
169 if (get_method
!= null) {
170 var get_call
= new
MethodCall (new
MemberAccess (container
, "get"));
171 foreach (Expression e
in get_indices ()) {
172 get_call
.add_argument (e
);
174 get_call
.target_type
= this
.target_type
;
175 parent_node
.replace_expression (this
, get_call
);
176 return get_call
.check (analyzer
);
181 Report
.error (source_reference
, "The expression `%s' does not denote an array".printf (container
.value_type
.to_string ()));
184 if (index_int_type_check
) {
185 /* check if the index is of type integer */
186 foreach (Expression e
in get_indices ()) {
187 /* don't proceed if a child expression failed */
188 if (e
.value_type
== null) {
192 /* check if the index is of type integer */
193 if (!(e
.value_type is IntegerType
|| e
.value_type is EnumValueType
)) {
195 Report
.error (e
.source_reference
, "Expression of integer type expected");
203 public override void get_defined_variables (Collection
<LocalVariable
> collection
) {
204 container
.get_defined_variables (collection
);
205 foreach (Expression index
in indices
) {
206 index
.get_defined_variables (collection
);
210 public override void get_used_variables (Collection
<LocalVariable
> collection
) {
211 container
.get_used_variables (collection
);
212 foreach (Expression index
in indices
) {
213 index
.get_used_variables (collection
);