1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
/* valasliceexpression.vala
*
* Copyright (C) 2009 Robin Sonefors
* Copyright (C) 2009-2013 Jürg Billeter
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author:
* Robin Sonefors <ozamosi@flukkost.nu>
* Jürg Billeter <j@bitron.ch>
*/
using GLib;
/**
* Represents an array slice expression e.g "a[1:5]".
*/
public class Vala.SliceExpression : Expression {
public Expression container {
get {
return _container;
}
set {
_container = value;
_container.parent_node = this;
}
}
public Expression start {
get {
return _start;
}
private set {
_start = value;
_start.parent_node = this;
}
}
public Expression stop {
get {
return _stop;
}
private set {
_stop = value;
_stop.parent_node = this;
}
}
Expression _container;
Expression _start;
Expression _stop;
public SliceExpression (Expression container, Expression start, Expression stop, SourceReference? source_reference = null) {
this.container = container;
this.start = start;
this.stop = stop;
this.source_reference = source_reference;
}
public override void accept (CodeVisitor visitor) {
visitor.visit_slice_expression (this);
visitor.visit_expression (this);
}
public override void accept_children (CodeVisitor visitor) {
container.accept (visitor);
start.accept (visitor);
stop.accept (visitor);
}
public override void replace_expression (Expression old_node, Expression new_node) {
if (container == old_node) {
container = new_node;
}
if (start == old_node) {
start = new_node;
}
if (stop == old_node) {
stop = new_node;
}
}
public override bool is_pure () {
return false;
}
public override bool check (CodeContext context) {
if (checked) {
return !error;
}
checked = true;
if (!container.check (context)) {
error = true;
return false;
}
if (container.value_type is ArrayType) {
start.target_type = context.analyzer.int_type.copy ();
stop.target_type = context.analyzer.int_type.copy ();
}
if (!start.check (context)) {
error = true;
return false;
}
if (!stop.check (context)) {
error = true;
return false;
}
if (container.value_type == null) {
error = true;
Report.error (container.source_reference, "Invalid container expression");
return false;
}
if (lvalue) {
error = true;
Report.error (container.source_reference, "Slice expressions cannot be used as lvalue");
return false;
}
if (container.value_type is ArrayType) {
value_type = container.value_type.copy ();
value_type.value_owned = false;
/* check if the index is of type integer */
if (!(start.value_type is IntegerType || start.value_type is EnumValueType)) {
error = true;
Report.error (start.source_reference, "Expression of integer type expected");
}
if (!(stop.value_type is IntegerType || stop.value_type is EnumValueType)) {
error = true;
Report.error (stop.source_reference, "Expression of integer type expected");
}
} else {
var slice_method = container.value_type.get_member ("slice") as Method;
if (slice_method != null) {
var slice_call = new MethodCall (new MemberAccess (container, "slice"));
slice_call.add_argument (start);
slice_call.add_argument (stop);
slice_call.target_type = this.target_type;
parent_node.replace_expression (this, slice_call);
return slice_call.check (context);
}
error = true;
Report.error (source_reference, "The expression `%s' does not denote an array".printf (container.value_type.to_string ()));
}
return !error;
}
public override void emit (CodeGenerator codegen) {
container.emit (codegen);
start.emit (codegen);
stop.emit (codegen);
codegen.visit_slice_expression (this);
codegen.visit_expression (this);
}
public override void get_defined_variables (Collection<Variable> collection) {
container.get_defined_variables (collection);
start.get_defined_variables (collection);
stop.get_defined_variables (collection);
}
public override void get_used_variables (Collection<Variable> collection) {
container.get_used_variables (collection);
start.get_used_variables (collection);
stop.get_used_variables (collection);
}
}
|