Release 0.7.8
[vala-lang.git] / vala / valaobjectcreationexpression.vala
blob4a038ce3da3e8248c060f3439897caa2897f0cd3
1 /* valaobjectcreationexpression.vala
3 * Copyright (C) 2006-2009 Jürg Billeter
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 * Author:
20 * Jürg Billeter <j@bitron.ch>
23 using GLib;
25 /**
26 * Represents an object creation expression in the source code.
28 public class Vala.ObjectCreationExpression : Expression {
29 /**
30 * The object type to create.
32 public DataType type_reference {
33 get { return _data_type; }
34 set {
35 _data_type = value;
36 _data_type.parent_node = this;
40 /**
41 * The construction method to use. May be null to indicate that
42 * the default construction method should be used.
44 public Method constructor { get; set; }
46 /**
47 * The construction method to use or the data type to be created
48 * with the default construction method.
50 public MemberAccess member_name { get; set; }
52 public bool struct_creation { get; set; }
54 private List<Expression> argument_list = new ArrayList<Expression> ();
56 private List<MemberInitializer> object_initializer = new ArrayList<MemberInitializer> ();
58 private DataType _data_type;
60 /**
61 * Creates a new object creation expression.
63 * @param member_name object type to create
64 * @param source_reference reference to source code
65 * @return newly created object creation expression
67 public ObjectCreationExpression (MemberAccess member_name, SourceReference source_reference) {
68 this.source_reference = source_reference;
69 this.member_name = member_name;
72 /**
73 * Appends the specified expression to the list of arguments.
75 * @param arg an argument
77 public void add_argument (Expression arg) {
78 argument_list.add (arg);
79 arg.parent_node = this;
82 /**
83 * Returns a copy of the argument list.
85 * @return argument list
87 public List<Expression> get_argument_list () {
88 return new ReadOnlyList<Expression> (argument_list);
91 /**
92 * Appends the specified member initializer to the object initializer.
94 * @param init a member initializer
96 public void add_member_initializer (MemberInitializer init) {
97 object_initializer.add (init);
98 init.parent_node = this;
102 * Returns the object initializer.
104 * @return member initializer list
106 public List<MemberInitializer> get_object_initializer () {
107 return new ReadOnlyList<MemberInitializer> (object_initializer);
110 public override void accept (CodeVisitor visitor) {
111 visitor.visit_object_creation_expression (this);
113 visitor.visit_expression (this);
116 public override void accept_children (CodeVisitor visitor) {
117 if (type_reference != null) {
118 type_reference.accept (visitor);
121 if (member_name != null) {
122 member_name.accept (visitor);
125 foreach (Expression arg in argument_list) {
126 arg.accept (visitor);
129 foreach (MemberInitializer init in object_initializer) {
130 init.accept (visitor);
134 public override void replace_expression (Expression old_node, Expression new_node) {
135 int index = argument_list.index_of (old_node);
136 if (index >= 0 && new_node.parent_node == null) {
137 argument_list[index] = new_node;
138 new_node.parent_node = this;
142 public override bool is_pure () {
143 return false;
146 public override void replace_type (DataType old_type, DataType new_type) {
147 if (type_reference == old_type) {
148 type_reference = new_type;
152 public override bool check (SemanticAnalyzer analyzer) {
153 if (checked) {
154 return !error;
157 checked = true;
159 if (member_name != null) {
160 member_name.check (analyzer);
163 TypeSymbol type = null;
165 if (type_reference == null) {
166 if (member_name == null) {
167 error = true;
168 Report.error (source_reference, "Incomplete object creation expression");
169 return false;
172 if (member_name.symbol_reference == null) {
173 error = true;
174 return false;
177 var constructor_sym = member_name.symbol_reference;
178 var type_sym = member_name.symbol_reference;
180 var type_args = member_name.get_type_arguments ();
182 if (constructor_sym is Method) {
183 type_sym = constructor_sym.parent_symbol;
185 var constructor = (Method) constructor_sym;
186 if (!(constructor_sym is CreationMethod)) {
187 error = true;
188 Report.error (source_reference, "`%s' is not a creation method".printf (constructor.get_full_name ()));
189 return false;
192 symbol_reference = constructor;
194 // inner expression can also be base access when chaining constructors
195 var ma = member_name.inner as MemberAccess;
196 if (ma != null) {
197 type_args = ma.get_type_arguments ();
201 if (type_sym is Class) {
202 type = (TypeSymbol) type_sym;
203 type_reference = new ObjectType ((Class) type);
204 } else if (type_sym is Struct) {
205 type = (TypeSymbol) type_sym;
206 type_reference = new StructValueType ((Struct) type);
207 } else if (type_sym is ErrorCode) {
208 type_reference = new ErrorType ((ErrorDomain) type_sym.parent_symbol, (ErrorCode) type_sym, source_reference);
209 symbol_reference = type_sym;
210 } else {
211 error = true;
212 Report.error (source_reference, "`%s' is not a class, struct, or error code".printf (type_sym.get_full_name ()));
213 return false;
216 foreach (DataType type_arg in type_args) {
217 type_reference.add_type_argument (type_arg);
219 } else {
220 type = type_reference.data_type;
223 value_type = type_reference.copy ();
224 value_type.value_owned = true;
226 bool may_throw = false;
228 int given_num_type_args = type_reference.get_type_arguments ().size;
229 int expected_num_type_args = 0;
231 if (type is Class) {
232 var cl = (Class) type;
234 expected_num_type_args = cl.get_type_parameters ().size;
236 if (struct_creation) {
237 error = true;
238 Report.error (source_reference, "syntax error, use `new' to create new objects");
239 return false;
242 if (cl.is_abstract) {
243 value_type = null;
244 error = true;
245 Report.error (source_reference, "Can't create instance of abstract class `%s'".printf (cl.get_full_name ()));
246 return false;
249 if (symbol_reference == null) {
250 symbol_reference = cl.default_construction_method;
252 if (symbol_reference == null) {
253 error = true;
254 Report.error (source_reference, "`%s' does not have a default constructor".printf (cl.get_full_name ()));
255 return false;
258 // track usage for flow analyzer
259 symbol_reference.used = true;
262 if (symbol_reference != null && symbol_reference.access == SymbolAccessibility.PRIVATE) {
263 bool in_target_type = false;
264 for (Symbol this_symbol = analyzer.current_symbol; this_symbol != null; this_symbol = this_symbol.parent_symbol) {
265 if (this_symbol == cl) {
266 in_target_type = true;
267 break;
271 if (!in_target_type) {
272 error = true;
273 Report.error (source_reference, "Access to private member `%s' denied".printf (symbol_reference.get_full_name ()));
274 return false;
278 while (cl != null) {
279 if (cl.get_ref_sink_function () != null) {
280 value_type.floating_reference = true;
281 break;
284 cl = cl.base_class;
286 } else if (type is Struct) {
287 var st = (Struct) type;
289 expected_num_type_args = st.get_type_parameters ().size;
291 if (!struct_creation && !analyzer.context.deprecated) {
292 Report.warning (source_reference, "deprecated syntax, don't use `new' to initialize structs");
295 if (symbol_reference == null) {
296 symbol_reference = st.default_construction_method;
300 if (expected_num_type_args > given_num_type_args) {
301 error = true;
302 Report.error (source_reference, "too few type arguments");
303 return false;
304 } else if (expected_num_type_args < given_num_type_args) {
305 error = true;
306 Report.error (source_reference, "too many type arguments");
307 return false;
310 if (symbol_reference == null && get_argument_list ().size != 0) {
311 value_type = null;
312 error = true;
313 Report.error (source_reference, "No arguments allowed when constructing type `%s'".printf (type.get_full_name ()));
314 return false;
317 if (symbol_reference is Method) {
318 var m = (Method) symbol_reference;
320 var args = get_argument_list ();
321 Iterator<Expression> arg_it = args.iterator ();
322 foreach (FormalParameter param in m.get_parameters ()) {
323 if (param.ellipsis) {
324 break;
327 if (arg_it.next ()) {
328 Expression arg = arg_it.get ();
330 /* store expected type for callback parameters */
331 arg.formal_target_type = param.parameter_type;
332 arg.target_type = arg.formal_target_type.get_actual_type (value_type, null, this);
336 foreach (Expression arg in args) {
337 arg.check (analyzer);
340 analyzer.check_arguments (this, new MethodType (m), m.get_parameters (), args);
342 foreach (DataType error_type in m.get_error_types ()) {
343 may_throw = true;
345 // ensure we can trace back which expression may throw errors of this type
346 var call_error_type = error_type.copy ();
347 call_error_type.source_reference = source_reference;
349 add_error_type (call_error_type);
351 } else if (type_reference is ErrorType) {
352 if (type_reference != null) {
353 type_reference.check (analyzer);
356 if (member_name != null) {
357 member_name.check (analyzer);
360 foreach (Expression arg in argument_list) {
361 arg.check (analyzer);
364 foreach (MemberInitializer init in object_initializer) {
365 init.check (analyzer);
368 if (get_argument_list ().size == 0) {
369 error = true;
370 Report.error (source_reference, "Too few arguments, errors need at least 1 argument");
371 } else {
372 Iterator<Expression> arg_it = get_argument_list ().iterator ();
373 arg_it.next ();
374 var ex = arg_it.get ();
375 if (ex.value_type == null || !ex.value_type.compatible (analyzer.string_type)) {
376 error = true;
377 Report.error (source_reference, "Invalid type for argument 1");
382 foreach (MemberInitializer init in get_object_initializer ()) {
383 analyzer.visit_member_initializer (init, type_reference);
386 if (may_throw) {
387 if (parent_node is LocalVariable || parent_node is ExpressionStatement) {
388 // simple statements, no side effects after method call
389 } else {
390 // store parent_node as we need to replace the expression in the old parent node later on
391 var old_parent_node = parent_node;
393 var local = new LocalVariable (value_type, get_temp_name (), null, source_reference);
394 // use floating variable to avoid unnecessary (and sometimes impossible) copies
395 local.floating = true;
396 var decl = new DeclarationStatement (local, source_reference);
398 insert_statement (analyzer.insert_block, decl);
400 Expression temp_access = new MemberAccess.simple (local.name, source_reference);
401 temp_access.target_type = target_type;
403 // don't set initializer earlier as this changes parent_node and parent_statement
404 local.initializer = this;
405 decl.check (analyzer);
406 temp_access.check (analyzer);
408 // move temp variable to insert block to ensure the
409 // variable is in the same block as the declaration
410 // otherwise there will be scoping issues in the generated code
411 var block = (Block) analyzer.current_symbol;
412 block.remove_local_variable (local);
413 analyzer.insert_block.add_local_variable (local);
415 old_parent_node.replace_expression (this, temp_access);
419 return !error;
422 public override void get_defined_variables (Collection<LocalVariable> collection) {
423 foreach (Expression arg in argument_list) {
424 arg.get_defined_variables (collection);
428 public override void get_used_variables (Collection<LocalVariable> collection) {
429 foreach (Expression arg in argument_list) {
430 arg.get_used_variables (collection);