1 /* valainitializerlist.vala
3 * Copyright (C) 2006-2011 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 * Jürg Billeter <j@bitron.ch>
22 * Raffaele Sandrini <raffaele@sandrini.ch>
28 * Represents an array or struct initializer list in the source code.
30 public class Vala
.InitializerList
: Expression
{
31 private List
<Expression
> initializers
= new ArrayList
<Expression
> ();
34 * Appends the specified expression to this initializer
36 * @param expr an expression
38 public void append (Expression expr
) {
39 initializers
.add (expr
);
40 expr
.parent_node
= this
;
44 * Returns a copy of the expression
46 * @return expression list
48 public List
<Expression
> get_initializers () {
53 * Returns the initializer count in this initializer
56 get { return initializers
.size
; }
60 * Creates a new initializer
62 * @param source_reference reference to source code
63 * @return newly created initializer list
65 public InitializerList (SourceReference source_reference
) {
66 this
.source_reference
= source_reference
;
69 public override void accept_children (CodeVisitor visitor
) {
70 foreach (Expression expr
in initializers
) {
71 expr
.accept (visitor
);
75 public override void accept (CodeVisitor visitor
) {
76 visitor
.visit_initializer_list (this
);
79 public override bool is_constant () {
80 foreach (Expression initializer
in initializers
) {
81 if (!initializer
.is_constant ()) {
88 public override bool is_pure () {
89 foreach (Expression initializer
in initializers
) {
90 if (!initializer
.is_pure ()) {
97 public override void replace_expression (Expression old_node
, Expression new_node
) {
98 for (int i
= 0; i
< initializers
.size
; i
++) {
99 if (initializers
[i
] == old_node
) {
100 initializers
[i
] = new_node
;
105 public override bool check (CodeContext context
) {
112 if (target_type
== null) {
114 Report
.error (source_reference
, "initializer list used for unknown type");
116 } else if (target_type is ArrayType
) {
117 /* initializer is used as array initializer */
118 var array_type
= (ArrayType
) target_type
;
120 if (!(parent_node is ArrayCreationExpression
)
121 && !(parent_node is Constant
)
122 && !(parent_node is InitializerList
)) {
123 // transform shorthand form
124 // int[] array = { 42 };
126 // int[] array = new int[] { 42 };
128 var old_parent_node
= parent_node
;
130 var array_creation
= new
ArrayCreationExpression (array_type
.element_type
.copy (), array_type
.rank
, this
, source_reference
);
131 array_creation
.target_type
= target_type
;
132 old_parent_node
.replace_expression (this
, array_creation
);
135 return array_creation
.check (context
);
138 DataType inner_target_type
;
139 if (array_type
.rank
> 1) {
140 // allow initialization of multi-dimensional arrays
141 var inner_array_type
= (ArrayType
) array_type
.copy ();
142 inner_array_type
.rank
--;
143 inner_target_type
= inner_array_type
;
145 inner_target_type
= array_type
.element_type
.copy ();
148 foreach (Expression e
in get_initializers ()) {
149 e
.target_type
= inner_target_type
;
151 } else if (target_type
.data_type is Struct
) {
152 /* initializer is used as struct initializer */
153 var st
= (Struct
) target_type
.data_type
;
155 var field_it
= st
.get_fields ().iterator ();
156 foreach (Expression e
in get_initializers ()) {
158 while (field
== null) {
159 if (!field_it
.next ()) {
161 Report
.error (e
.source_reference
, "too many expressions in initializer list for `%s'".printf (target_type
.to_string ()));
164 field
= field_it
.get ();
165 if (field
.binding
!= MemberBinding
.INSTANCE
) {
166 // we only initialize instance fields
171 e
.target_type
= field
.variable_type
.copy ();
172 if (!target_type
.value_owned
) {
173 e
.target_type
.value_owned
= false;
178 Report
.error (source_reference
, "initializer list used for `%s', which is neither array nor struct".printf (target_type
.to_string ()));
182 foreach (Expression expr
in initializers
) {
183 expr
.check (context
);
187 foreach (Expression e
in get_initializers ()) {
188 if (e
.value_type
== null) {
190 Report
.error (e
.source_reference
, "expression type not allowed as initializer");
194 var unary
= e as UnaryExpression
;
195 if (unary
!= null && (unary
.operator
== UnaryOperator
.REF
|| unary
.operator
== UnaryOperator
.OUT
)) {
196 // TODO check type for ref and out expressions
197 } else if (!e
.value_type
.compatible (e
.target_type
)) {
200 Report
.error (e
.source_reference
, "Expected initializer of type `%s' but got `%s'".printf (e
.target_type
.to_string (), e
.value_type
.to_string ()));
205 /* everything seems to be correct */
206 value_type
= target_type
;
212 public override void emit (CodeGenerator codegen
) {
213 foreach (Expression expr
in initializers
) {
217 codegen
.visit_initializer_list (this
);