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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
/* valainitializerlist.vala
*
* Copyright (C) 2006-2011 Jürg Billeter
* Copyright (C) 2006-2008 Raffaele Sandrini
*
* 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:
* Jürg Billeter <j@bitron.ch>
* Raffaele Sandrini <raffaele@sandrini.ch>
*/
using GLib;
/**
* Represents an array or struct initializer list in the source code.
*/
public class Vala.InitializerList : Expression {
private List<Expression> initializers = new ArrayList<Expression> ();
/**
* Appends the specified expression to this initializer
*
* @param expr an expression
*/
public void append (Expression expr) {
initializers.add (expr);
expr.parent_node = this;
}
/**
* Returns a copy of the expression
*
* @return expression list
*/
public List<Expression> get_initializers () {
return initializers;
}
/**
* Returns the initializer count in this initializer
*/
public int size {
get { return initializers.size; }
}
/**
* Creates a new initializer
*
* @param source_reference reference to source code
* @return newly created initializer list
*/
public InitializerList (SourceReference source_reference) {
this.source_reference = source_reference;
}
public override void accept_children (CodeVisitor visitor) {
foreach (Expression expr in initializers) {
expr.accept (visitor);
}
}
public override void accept (CodeVisitor visitor) {
visitor.visit_initializer_list (this);
visitor.visit_expression (this);
}
public override bool is_constant () {
foreach (Expression initializer in initializers) {
if (!initializer.is_constant ()) {
return false;
}
}
return true;
}
public override bool is_pure () {
foreach (Expression initializer in initializers) {
if (!initializer.is_pure ()) {
return false;
}
}
return true;
}
public override bool is_accessible (Symbol sym) {
foreach (Expression initializer in initializers) {
if (!initializer.is_accessible (sym)) {
return false;
}
}
return true;
}
public override string to_string () {
var builder = new StringBuilder ("{");
bool first = true;
foreach (var initializer in initializers) {
if (first) {
builder.append (initializer.to_string ());
first = false;
} else {
builder.append_printf (", %s", initializer.to_string ());
}
}
return builder.str;
}
public override void replace_expression (Expression old_node, Expression new_node) {
for (int i = 0; i < initializers.size; i++) {
if (initializers[i] == old_node) {
initializers[i] = new_node;
new_node.parent_node = this;
}
}
}
public override bool check (CodeContext context) {
if (checked) {
return !error;
}
checked = true;
if (target_type == null) {
error = true;
Report.error (source_reference, "initializer list used for unknown type");
return false;
} else if (target_type is ArrayType) {
/* initializer is used as array initializer */
var array_type = (ArrayType) target_type;
bool requires_constants_only = false;
unowned CodeNode? node = parent_node;
while (node != null) {
if (node is Constant) {
requires_constants_only = true;
break;
}
node = node.parent_node;
}
if (!(parent_node is ArrayCreationExpression) && !requires_constants_only
&& (!(parent_node is InitializerList) || ((InitializerList) parent_node).target_type.data_type is Struct)) {
// transform shorthand form
// int[] array = { 42 };
// into
// int[] array = new int[] { 42 };
var old_parent_node = parent_node;
var array_creation = new ArrayCreationExpression (array_type.element_type.copy (), array_type.rank, this, source_reference);
array_creation.target_type = target_type;
old_parent_node.replace_expression (this, array_creation);
checked = false;
return array_creation.check (context);
}
DataType inner_target_type;
if (array_type.rank > 1) {
// allow initialization of multi-dimensional arrays
var inner_array_type = (ArrayType) array_type.copy ();
inner_array_type.rank--;
inner_target_type = inner_array_type;
} else {
inner_target_type = array_type.element_type.copy ();
}
foreach (Expression e in get_initializers ()) {
e.target_type = inner_target_type;
}
} else if (target_type.data_type is Struct) {
/* initializer is used as struct initializer */
var st = (Struct) target_type.data_type;
while (st.base_struct != null) {
st = st.base_struct;
}
var field_it = st.get_fields ().iterator ();
foreach (Expression e in get_initializers ()) {
Field field = null;
while (field == null) {
if (!field_it.next ()) {
error = true;
Report.error (e.source_reference, "too many expressions in initializer list for `%s'".printf (target_type.to_string ()));
return false;
}
field = field_it.get ();
if (field.binding != MemberBinding.INSTANCE) {
// we only initialize instance fields
field = null;
}
}
e.target_type = field.variable_type.copy ();
if (!target_type.value_owned) {
e.target_type.value_owned = false;
}
}
} else {
error = true;
Report.error (source_reference, "initializer list used for `%s', which is neither array nor struct".printf (target_type.to_string ()));
return false;
}
foreach (Expression expr in initializers) {
expr.check (context);
}
bool error = false;
foreach (Expression e in get_initializers ()) {
if (e.value_type == null) {
error = true;
Report.error (e.source_reference, "expression type not allowed as initializer");
continue;
}
var unary = e as UnaryExpression;
if (unary != null && (unary.operator == UnaryOperator.REF || unary.operator == UnaryOperator.OUT)) {
// TODO check type for ref and out expressions
} else if (!e.value_type.compatible (e.target_type)) {
error = true;
e.error = true;
Report.error (e.source_reference, "Expected initializer of type `%s' but got `%s'".printf (e.target_type.to_string (), e.value_type.to_string ()));
}
}
if (!error) {
/* everything seems to be correct */
value_type = target_type.copy ();
value_type.nullable = false;
}
return !error;
}
public override void emit (CodeGenerator codegen) {
foreach (Expression expr in initializers) {
expr.emit (codegen);
}
codegen.visit_initializer_list (this);
codegen.visit_expression (this);
}
public override void get_used_variables (Collection<Variable> collection) {
foreach (Expression expr in initializers) {
expr.get_used_variables (collection);
}
}
}
|