D-Bus: Remove extra semicolon to avoid C warning
[vala-lang.git] / vala / valaobjectcreationexpression.vala
blobdac7d74ef0c884ce9dece9a52ddf4d5c90ce6069
1 /* valaobjectcreationexpression.vala
3 * Copyright (C) 2006-2010 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 or the data type to be created
42 * with the default construction method.
44 public MemberAccess member_name { get; set; }
46 public bool struct_creation { get; set; }
48 private List<Expression> argument_list = new ArrayList<Expression> ();
50 private List<MemberInitializer> object_initializer = new ArrayList<MemberInitializer> ();
52 private DataType _data_type;
54 /**
55 * Creates a new object creation expression.
57 * @param member_name object type to create
58 * @param source_reference reference to source code
59 * @return newly created object creation expression
61 public ObjectCreationExpression (MemberAccess member_name, SourceReference source_reference) {
62 this.source_reference = source_reference;
63 this.member_name = member_name;
66 /**
67 * Appends the specified expression to the list of arguments.
69 * @param arg an argument
71 public void add_argument (Expression arg) {
72 argument_list.add (arg);
73 arg.parent_node = this;
76 /**
77 * Returns a copy of the argument list.
79 * @return argument list
81 public List<Expression> get_argument_list () {
82 return argument_list;
85 /**
86 * Appends the specified member initializer to the object initializer.
88 * @param init a member initializer
90 public void add_member_initializer (MemberInitializer init) {
91 object_initializer.add (init);
92 init.parent_node = this;
95 /**
96 * Returns the object initializer.
98 * @return member initializer list
100 public List<MemberInitializer> get_object_initializer () {
101 return object_initializer;
104 public override void accept (CodeVisitor visitor) {
105 visitor.visit_object_creation_expression (this);
107 visitor.visit_expression (this);
110 public override void accept_children (CodeVisitor visitor) {
111 if (type_reference != null) {
112 type_reference.accept (visitor);
115 if (member_name != null) {
116 member_name.accept (visitor);
119 foreach (Expression arg in argument_list) {
120 arg.accept (visitor);
123 foreach (MemberInitializer init in object_initializer) {
124 init.accept (visitor);
128 public override void replace_expression (Expression old_node, Expression new_node) {
129 int index = argument_list.index_of (old_node);
130 if (index >= 0 && new_node.parent_node == null) {
131 argument_list[index] = new_node;
132 new_node.parent_node = this;
136 public override bool is_pure () {
137 return false;
140 public override void replace_type (DataType old_type, DataType new_type) {
141 if (type_reference == old_type) {
142 type_reference = new_type;
146 public override bool check (CodeContext context) {
147 if (checked) {
148 return !error;
151 checked = true;
153 if (member_name != null) {
154 member_name.check (context);
157 TypeSymbol type = null;
159 if (type_reference == null) {
160 if (member_name == null) {
161 error = true;
162 Report.error (source_reference, "Incomplete object creation expression");
163 return false;
166 if (member_name.symbol_reference == null) {
167 error = true;
168 return false;
171 var constructor_sym = member_name.symbol_reference;
172 var type_sym = member_name.symbol_reference;
174 var type_args = member_name.get_type_arguments ();
176 if (constructor_sym is Method) {
177 type_sym = constructor_sym.parent_symbol;
179 var constructor = (Method) constructor_sym;
180 if (!(constructor_sym is CreationMethod)) {
181 error = true;
182 Report.error (source_reference, "`%s' is not a creation method".printf (constructor.get_full_name ()));
183 return false;
186 symbol_reference = constructor;
188 // inner expression can also be base access when chaining constructors
189 var ma = member_name.inner as MemberAccess;
190 if (ma != null) {
191 type_args = ma.get_type_arguments ();
195 if (type_sym is Class) {
196 type = (TypeSymbol) type_sym;
197 if (((Class) type).is_error_base) {
198 type_reference = new ErrorType (null, null, source_reference);
199 } else {
200 type_reference = new ObjectType ((Class) type);
202 } else if (type_sym is Struct) {
203 type = (TypeSymbol) type_sym;
204 type_reference = new StructValueType ((Struct) type);
205 } else if (type_sym is ErrorCode) {
206 type_reference = new ErrorType ((ErrorDomain) type_sym.parent_symbol, (ErrorCode) type_sym, source_reference);
207 symbol_reference = type_sym;
208 } else {
209 error = true;
210 Report.error (source_reference, "`%s' is not a class, struct, or error code".printf (type_sym.get_full_name ()));
211 return false;
214 foreach (DataType type_arg in type_args) {
215 type_reference.add_type_argument (type_arg);
217 } else {
218 type = type_reference.data_type;
221 value_type = type_reference.copy ();
222 value_type.value_owned = true;
224 bool may_throw = false;
226 int given_num_type_args = type_reference.get_type_arguments ().size;
227 int expected_num_type_args = 0;
229 if (type is Class) {
230 var cl = (Class) type;
232 expected_num_type_args = cl.get_type_parameters ().size;
234 if (struct_creation) {
235 error = true;
236 Report.error (source_reference, "syntax error, use `new' to create new objects");
237 return false;
240 if (cl.is_abstract) {
241 value_type = null;
242 error = true;
243 Report.error (source_reference, "Can't create instance of abstract class `%s'".printf (cl.get_full_name ()));
244 return false;
247 if (symbol_reference == null) {
248 symbol_reference = cl.default_construction_method;
250 if (symbol_reference == null) {
251 error = true;
252 Report.error (source_reference, "`%s' does not have a default constructor".printf (cl.get_full_name ()));
253 return false;
256 // track usage for flow analyzer
257 symbol_reference.used = true;
258 symbol_reference.check_deprecated (source_reference);
261 if (symbol_reference != null && symbol_reference.access == SymbolAccessibility.PRIVATE) {
262 bool in_target_type = false;
263 for (Symbol this_symbol = context.analyzer.current_symbol; this_symbol != null; this_symbol = this_symbol.parent_symbol) {
264 if (this_symbol == cl) {
265 in_target_type = true;
266 break;
270 if (!in_target_type) {
271 error = true;
272 Report.error (source_reference, "Access to private member `%s' denied".printf (symbol_reference.get_full_name ()));
273 return false;
277 while (cl != null) {
278 if (cl.get_ref_sink_function () != null) {
279 value_type.floating_reference = true;
280 break;
283 cl = cl.base_class;
285 } else if (type is Struct) {
286 var st = (Struct) type;
288 expected_num_type_args = st.get_type_parameters ().size;
290 if (!struct_creation && !context.deprecated) {
291 Report.warning (source_reference, "deprecated syntax, don't use `new' to initialize structs");
294 if (symbol_reference == null) {
295 symbol_reference = st.default_construction_method;
299 if (expected_num_type_args > given_num_type_args) {
300 error = true;
301 Report.error (source_reference, "too few type arguments");
302 return false;
303 } else if (expected_num_type_args < given_num_type_args) {
304 error = true;
305 Report.error (source_reference, "too many type arguments");
306 return false;
309 if (symbol_reference == null && get_argument_list ().size != 0) {
310 value_type = null;
311 error = true;
312 Report.error (source_reference, "No arguments allowed when constructing type `%s'".printf (type.get_full_name ()));
313 return false;
316 if (symbol_reference is Method) {
317 var m = (Method) symbol_reference;
319 var args = get_argument_list ();
320 Iterator<Expression> arg_it = args.iterator ();
321 foreach (Parameter param in m.get_parameters ()) {
322 if (param.ellipsis) {
323 break;
326 if (arg_it.next ()) {
327 Expression arg = arg_it.get ();
329 /* store expected type for callback parameters */
330 arg.formal_target_type = param.variable_type;
331 arg.target_type = arg.formal_target_type.get_actual_type (value_type, null, this);
335 foreach (Expression arg in args) {
336 arg.check (context);
339 context.analyzer.check_arguments (this, new MethodType (m), m.get_parameters (), args);
341 foreach (DataType error_type in m.get_error_types ()) {
342 may_throw = true;
344 // ensure we can trace back which expression may throw errors of this type
345 var call_error_type = error_type.copy ();
346 call_error_type.source_reference = source_reference;
348 add_error_type (call_error_type);
350 } else if (type_reference is ErrorType) {
351 if (type_reference != null) {
352 type_reference.check (context);
355 if (member_name != null) {
356 member_name.check (context);
359 foreach (Expression arg in argument_list) {
360 arg.check (context);
363 foreach (MemberInitializer init in object_initializer) {
364 init.check (context);
367 if (get_argument_list ().size == 0) {
368 error = true;
369 Report.error (source_reference, "Too few arguments, errors need at least 1 argument");
370 } else {
371 Iterator<Expression> arg_it = get_argument_list ().iterator ();
372 arg_it.next ();
373 var ex = arg_it.get ();
374 if (ex.value_type == null || !ex.value_type.compatible (context.analyzer.string_type)) {
375 error = true;
376 Report.error (source_reference, "Invalid type for argument 1");
381 foreach (MemberInitializer init in get_object_initializer ()) {
382 context.analyzer.visit_member_initializer (init, type_reference);
385 if (may_throw) {
386 if (parent_node is LocalVariable || parent_node is ExpressionStatement) {
387 // simple statements, no side effects after method call
388 } else if (!(context.analyzer.current_symbol is Block)) {
389 if (context.profile != Profile.DOVA) {
390 // can't handle errors in field initializers
391 Report.error (source_reference, "Field initializers must not throw errors");
393 } else {
394 // store parent_node as we need to replace the expression in the old parent node later on
395 var old_parent_node = parent_node;
397 var local = new LocalVariable (value_type, get_temp_name (), null, source_reference);
398 // use floating variable to avoid unnecessary (and sometimes impossible) copies
399 local.floating = true;
400 var decl = new DeclarationStatement (local, source_reference);
402 insert_statement (context.analyzer.insert_block, decl);
404 Expression temp_access = new MemberAccess.simple (local.name, source_reference);
405 temp_access.target_type = target_type;
407 // don't set initializer earlier as this changes parent_node and parent_statement
408 local.initializer = this;
409 decl.check (context);
410 temp_access.check (context);
412 // move temp variable to insert block to ensure the
413 // variable is in the same block as the declaration
414 // otherwise there will be scoping issues in the generated code
415 var block = (Block) context.analyzer.current_symbol;
416 block.remove_local_variable (local);
417 context.analyzer.insert_block.add_local_variable (local);
419 old_parent_node.replace_expression (this, temp_access);
423 return !error;
426 public override void emit (CodeGenerator codegen) {
427 foreach (Expression arg in argument_list) {
428 arg.emit (codegen);
431 foreach (MemberInitializer init in object_initializer) {
432 init.emit (codegen);
435 codegen.visit_object_creation_expression (this);
437 codegen.visit_expression (this);
440 public override void get_defined_variables (Collection<LocalVariable> collection) {
441 foreach (Expression arg in argument_list) {
442 arg.get_defined_variables (collection);
446 public override void get_used_variables (Collection<LocalVariable> collection) {
447 foreach (Expression arg in argument_list) {
448 arg.get_used_variables (collection);