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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
/* 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 the initializer expression list
*
* @return expression list
*/
public unowned 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 = null) {
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 !(target_type == null || target_type.is_disposable ());
}
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 ());
}
}
builder.append_c ('}');
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.error) {
error = true;
return false;
} else if (target_type is ArrayType) {
/* initializer is used as array initializer */
unowned ArrayType array_type = (ArrayType) target_type;
bool requires_constants_only = false;
bool is_global_constant_inline = false;
unowned CodeNode? node = parent_node;
while (node != null) {
if (node is Constant) {
requires_constants_only = true;
break;
} else if (node is Field && ((Field) node).parent_symbol is Namespace) {
is_global_constant_inline = array_type.inline_allocated && is_constant ();
}
node = node.parent_node;
}
if (!(parent_node is ArrayCreationExpression) && !requires_constants_only && !is_global_constant_inline
&& (!(parent_node is InitializerList) || ((InitializerList) parent_node).target_type.type_symbol 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.length_type = array_type.length_type.copy ();
array_creation.target_type = target_type;
array_creation.formal_target_type = formal_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.type_symbol is Struct) {
/* initializer is used as struct initializer */
unowned Struct st = (Struct) target_type.type_symbol;
while (st.base_struct != null) {
st = st.base_struct;
}
var in_array_creation_initializer = parent_node is InitializerList && parent_node.parent_node is ArrayCreationExpression;
ObjectCreationExpression? struct_creation = null;
if (in_array_creation_initializer) {
unowned Symbol? sym = st;
var ma = new MemberAccess.simple (sym.name, source_reference);
ma.symbol_reference = sym;
MemberAccess inner = ma;
while (sym.parent_symbol != null && sym.parent_symbol != context.root) {
sym = sym.parent_symbol;
var ma_inner = new MemberAccess.simple (sym.name, source_reference);
inner.inner = ma_inner;
inner = ma_inner;
}
struct_creation = new ObjectCreationExpression (ma, source_reference);
struct_creation.target_type = target_type.copy ();
struct_creation.struct_creation = true;
}
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'", target_type.to_string ());
return false;
}
field = field_it.get ();
if (field.binding != MemberBinding.INSTANCE) {
// we only initialize instance fields
field = null;
}
}
if (in_array_creation_initializer) {
var member_init = new MemberInitializer (field.name, e, e.source_reference);
struct_creation.add_member_initializer (member_init);
} else {
e.target_type = field.variable_type.copy ();
if (!target_type.value_owned) {
e.target_type.value_owned = false;
}
}
}
if (in_array_creation_initializer) {
parent_node.replace_expression (this, struct_creation);
checked = false;
return struct_creation.check (context);
}
} else {
error = true;
Report.error (source_reference, "initializer list used for `%s', which is neither array nor struct", target_type.to_string ());
return false;
}
foreach (Expression expr in initializers) {
if (!expr.check (context)) {
error = true;
}
}
if (error) {
return 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;
}
if (e is NullLiteral && e.target_type != null && e.target_type.is_real_non_null_struct_type ()) {
// Allow using null instead of {} to initialize struct
} 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'", 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;
}
if (value_type != null) {
value_type.check (context);
}
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);
}
}
}
|