vala: Allow read-only properties
[vala-gnome.git] / vala / valaelementaccess.vala
blob755eaf6ee78215843ab1be339462e3af6a9758a2
1 /* valaelementaccess.vala
3 * Copyright (C) 2006-2010 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
20 * Author:
21 * Raffaele Sandrini <raffaele@sandrini.ch>
22 * Jürg Billeter <j@bitron.ch>
25 using GLib;
27 /**
28 * Represents an array access expression e.g. "a[1,2]".
30 public class Vala.ElementAccess : Expression {
31 /**
32 * Expression representing the container on which we want to access.
34 public Expression container {
35 get {
36 return _container;
38 set {
39 _container = value;
40 _container.parent_node = this;
44 /**
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) {
52 indices.add (index);
53 index.parent_node = this;
56 public List<Expression> get_indices () {
57 return 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) {
74 e.accept (visitor);
78 public override void replace_expression (Expression old_node, Expression new_node) {
79 if (container == old_node) {
80 container = new_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 ()) {
93 return false;
96 return container.is_pure ();
99 public override bool is_accessible (Symbol sym) {
100 foreach (Expression index in indices) {
101 if (!index.is_accessible (sym)) {
102 return false;
106 return container.is_accessible (sym);
109 public override bool check (CodeContext context) {
110 if (checked) {
111 return !error;
114 checked = true;
116 if (!container.check (context)) {
117 /* don't proceed if a child expression failed */
118 error = true;
119 return false;
122 if (container.value_type == null) {
123 error = true;
124 Report.error (container.source_reference, "Invalid container expression");
125 return false;
128 if (container is MemberAccess && container.symbol_reference is Signal) {
129 // signal detail access
130 if (get_indices ().size != 1) {
131 error = true;
132 Report.error (source_reference, "Element access with more than one dimension is not supported for signals");
133 return false;
135 get_indices ().get (0).target_type = context.analyzer.string_type.copy ();
138 foreach (Expression index in get_indices ()) {
139 index.check (context);
142 bool index_int_type_check = true;
144 var pointer_type = container.value_type as PointerType;
146 /* assign a value_type when possible */
147 if (container.value_type is ArrayType) {
148 var array_type = (ArrayType) container.value_type;
149 value_type = array_type.element_type.copy ();
150 if (!lvalue) {
151 value_type.value_owned = false;
152 } else {
153 var ma = container as MemberAccess;
154 if (context.profile == Profile.GOBJECT && ma != null && ma.symbol_reference is ArrayLengthField) {
155 // propagate lvalue for gobject length access
156 ma.inner.lvalue = true;
157 ((MemberAccess) ma.inner).check_lvalue_access ();
158 } else if (ma != null && ma.symbol_reference is Field &&
159 ma.inner != null && ma.inner.symbol_reference is Variable &&
160 ma.inner.value_type is StructValueType && !ma.inner.value_type.nullable) {
161 // propagate lvalue if container is a field and container.inner is a struct variable
162 ma.lvalue = true;
163 ma.check_lvalue_access ();
167 if (array_type.rank < get_indices ().size) {
168 Report.error (source_reference, "%d extra indices for element access".printf (get_indices ().size - array_type.rank));
169 } else if (array_type.rank > get_indices ().size) {
170 Report.error (source_reference, "%d missing indices for element access".printf (array_type.rank - get_indices ().size));
172 } else if (pointer_type != null && !pointer_type.base_type.is_reference_type_or_type_parameter ()) {
173 value_type = pointer_type.base_type.copy ();
174 } else if (container is MemberAccess && container.symbol_reference is Signal) {
175 index_int_type_check = false;
177 symbol_reference = container.symbol_reference;
178 value_type = container.value_type;
179 } else {
180 if (lvalue) {
181 var set_method = container.value_type.get_member ("set") as Method;
182 var assignment = parent_node as Assignment;
183 if (set_method != null && set_method.return_type is VoidType && assignment != null) {
184 return !error;
186 } else {
187 var get_method = container.value_type.get_member ("get") as Method;
188 if (get_method != null) {
189 var get_call = new MethodCall (new MemberAccess (container, "get", source_reference), source_reference);
190 foreach (Expression e in get_indices ()) {
191 get_call.add_argument (e);
193 get_call.formal_target_type = this.formal_target_type;
194 get_call.target_type = this.target_type;
195 parent_node.replace_expression (this, get_call);
196 return get_call.check (context);
200 error = true;
201 Report.error (source_reference, "The expression `%s' does not denote an array".printf (container.value_type.to_string ()));
204 if (index_int_type_check) {
205 /* check if the index is of type integer */
206 foreach (Expression e in get_indices ()) {
207 /* don't proceed if a child expression failed */
208 if (e.value_type == null) {
209 return false;
212 /* check if the index is of type integer */
213 if (!(e.value_type is IntegerType || e.value_type is EnumValueType)) {
214 error = true;
215 Report.error (e.source_reference, "Expression of integer type expected");
220 return !error;
223 public override void emit (CodeGenerator codegen) {
224 container.emit (codegen);
225 foreach (Expression e in indices) {
226 e.emit (codegen);
229 codegen.visit_element_access (this);
231 codegen.visit_expression (this);
234 public override void get_defined_variables (Collection<Variable> collection) {
235 container.get_defined_variables (collection);
236 foreach (Expression index in indices) {
237 index.get_defined_variables (collection);
241 public override void get_used_variables (Collection<Variable> collection) {
242 container.get_used_variables (collection);
243 foreach (Expression index in indices) {
244 index.get_used_variables (collection);