Revert "Do not warn about non-default parameters for coroutines."
[vala-gnome.git] / vala / valamethod.vala
blob85f9d463e2b193e9b98fcca50ea6aaad9d9eacfd
1 /* valamethod.vala
3 * Copyright (C) 2006-2010 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
20 * Author:
21 * Jürg Billeter <j@bitron.ch>
22 * Raffaele Sandrini <raffaele@sandrini.ch>
25 using GLib;
27 /**
28 * Represents a type or namespace method.
30 public class Vala.Method : Subroutine, Callable {
31 List<TypeParameter> type_parameters;
33 /**
34 * The return type of this method.
36 public DataType return_type {
37 get { return _return_type; }
38 set {
39 _return_type = value;
40 _return_type.parent_node = this;
44 public override bool has_result {
45 get { return !(return_type is VoidType); }
48 /**
49 * Specifies whether this method may only be called with an instance of
50 * the contained type.
52 public MemberBinding binding { get; set; default = MemberBinding.INSTANCE; }
54 /**
55 * Specifies whether this method is abstract. Abstract methods have no
56 * body, may only be specified within abstract classes, and must be
57 * overriden by derived non-abstract classes.
59 public bool is_abstract { get; set; }
61 /**
62 * Specifies whether this method is virtual. Virtual methods may be
63 * overridden by derived classes.
65 public bool is_virtual { get; set; }
67 /**
68 * Specifies whether this method overrides a virtual or abstract method
69 * of a base type.
71 public bool overrides { get; set; }
73 /**
74 * Specifies whether this method should be inlined.
76 public bool is_inline { get; set; }
78 public bool returns_floating_reference {
79 get {
80 return get_attribute_bool ("CCode", "returns_floating_reference");
82 set {
83 set_attribute_bool ("CCode", "returns_floating_reference", value);
88 * Specifies whether the C method returns a new instance pointer which
89 * may be different from the previous instance pointer. Only valid for
90 * imported methods.
92 public bool returns_modified_pointer {
93 get {
94 return get_attribute ("ReturnsModifiedPointer") != null;
96 set {
97 set_attribute ("ReturnsModifiedPointer", value);
102 * Specifies the virtual or abstract method this method overrides.
103 * Reference must be weak as virtual and abstract methods set
104 * base_method to themselves.
106 public Method base_method {
107 get {
108 find_base_methods ();
109 return _base_method;
114 * Specifies the abstract interface method this method implements.
116 public Method base_interface_method {
117 get {
118 find_base_methods ();
119 return _base_interface_method;
124 * Specifies the explicit interface containing the method this method implements.
126 public DataType base_interface_type {
127 get { return _base_interface_type; }
128 set {
129 _base_interface_type = value;
130 _base_interface_type.parent_node = this;
134 public bool entry_point { get; private set; }
137 * Specifies the generated `this` parameter for instance methods.
139 public Parameter this_parameter { get; set; }
142 * Specifies whether this method expects printf-style format arguments.
144 public bool printf_format {
145 get {
146 return get_attribute ("PrintfFormat") != null;
148 set {
149 set_attribute ("PrintfFormat", value);
154 * Specifies whether this method expects scanf-style format arguments.
156 public bool scanf_format {
157 get {
158 return get_attribute ("ScanfFormat") != null;
160 set {
161 set_attribute ("ScanfFormat", value);
166 * Specifies whether a construct function with a GType parameter is
167 * available. This is only applicable to creation methods.
169 public bool has_construct_function {
170 get {
171 return get_attribute_bool ("CCode", "has_construct_function", true);
173 set {
174 set_attribute_bool ("CCode", "has_construct_function", value);
178 public weak Signal signal_reference { get; set; }
180 public bool closure { get; set; }
182 public bool coroutine { get; set; }
184 public bool is_async_callback { get; set; }
186 [Version (deprecated = true)]
187 public int yield_count { get; set; }
189 private List<Parameter> parameters = new ArrayList<Parameter> ();
190 private List<Expression> preconditions;
191 private List<Expression> postconditions;
192 private DataType _return_type;
194 private weak Method _base_method;
195 private weak Method _base_interface_method;
196 private DataType _base_interface_type;
197 private bool base_methods_valid;
199 Method? callback_method;
200 Method? end_method;
202 // only valid for closures
203 List<LocalVariable> captured_variables;
205 static List<Expression> _empty_expression_list;
206 static List<TypeParameter> _empty_type_parameter_list;
209 * Creates a new method.
211 * @param name method name
212 * @param return_type method return type
213 * @param source_reference reference to source code
214 * @return newly created method
216 public Method (string? name, DataType return_type, SourceReference? source_reference = null, Comment? comment = null) {
217 base (name, source_reference, comment);
218 this.return_type = return_type;
222 * Appends parameter to this method.
224 * @param param a formal parameter
226 public void add_parameter (Parameter param) {
227 // default C parameter position
228 parameters.add (param);
229 scope.add (param.name, param);
232 public List<Parameter> get_parameters () {
233 return parameters;
237 * Remove all parameters from this method.
239 public void clear_parameters () {
240 foreach (Parameter param in parameters) {
241 if (!param.ellipsis) {
242 scope.remove (param.name);
245 parameters.clear ();
248 public bool is_variadic () {
249 foreach (Parameter param in parameters) {
250 if (param.ellipsis) {
251 return true;
254 return false;
257 public override void accept (CodeVisitor visitor) {
258 visitor.visit_method (this);
261 public override void accept_children (CodeVisitor visitor) {
262 foreach (TypeParameter p in get_type_parameters ()) {
263 p.accept (visitor);
266 if (base_interface_type != null) {
267 base_interface_type.accept (visitor);
270 if (return_type != null) {
271 return_type.accept (visitor);
274 foreach (Parameter param in parameters) {
275 param.accept (visitor);
278 foreach (DataType error_type in get_error_types ()) {
279 error_type.accept (visitor);
282 if (result_var != null) {
283 result_var.accept (visitor);
286 if (preconditions != null) {
287 foreach (Expression precondition in preconditions) {
288 precondition.accept (visitor);
292 if (postconditions != null) {
293 foreach (Expression postcondition in postconditions) {
294 postcondition.accept (visitor);
298 if (body != null) {
299 body.accept (visitor);
304 * Checks whether the parameters and return type of this method are
305 * compatible with the specified method
307 * @param base_method a method
308 * @param invalid_match error string about which check failed
309 * @return true if the specified method is compatible to this method
311 public bool compatible (Method base_method, out string? invalid_match) {
312 // method is always compatible to itself
313 if (this == base_method) {
314 invalid_match = null;
315 return true;
318 if (binding != base_method.binding) {
319 invalid_match = "incompatible binding";
320 return false;
323 ObjectType object_type = null;
324 if (parent_symbol is ObjectTypeSymbol) {
325 object_type = new ObjectType ((ObjectTypeSymbol) parent_symbol);
326 foreach (TypeParameter type_parameter in object_type.type_symbol.get_type_parameters ()) {
327 var type_arg = new GenericType (type_parameter);
328 type_arg.value_owned = true;
329 object_type.add_type_argument (type_arg);
333 if (this.get_type_parameters ().size < base_method.get_type_parameters ().size) {
334 invalid_match = "too few type parameters";
335 return false;
336 } else if (this.get_type_parameters ().size > base_method.get_type_parameters ().size) {
337 invalid_match = "too many type parameters";
338 return false;
341 List<DataType> method_type_args = null;
342 if (this.get_type_parameters ().size > 0) {
343 method_type_args = new ArrayList<DataType> ();
344 foreach (TypeParameter type_parameter in this.get_type_parameters ()) {
345 var type_arg = new GenericType (type_parameter);
346 type_arg.value_owned = true;
347 method_type_args.add (type_arg);
351 var actual_base_type = base_method.return_type.get_actual_type (object_type, method_type_args, this);
352 if (!return_type.equals (actual_base_type)) {
353 invalid_match = "Base method expected return type `%s', but `%s' was provided".printf (type_to_prototype_string (actual_base_type), type_to_prototype_string (return_type));
354 return false;
357 Iterator<Parameter> method_params_it = parameters.iterator ();
358 int param_index = 1;
359 foreach (Parameter base_param in base_method.parameters) {
360 /* this method may not expect less arguments */
361 if (!method_params_it.next ()) {
362 invalid_match = "too few parameters";
363 return false;
366 var param = method_params_it.get ();
367 if (base_param.ellipsis != param.ellipsis) {
368 invalid_match = "ellipsis parameter mismatch";
369 return false;
371 if (!base_param.ellipsis) {
372 if (base_param.direction != param.direction) {
373 invalid_match = "incompatible direction of parameter %d".printf (param_index);
374 return false;
377 actual_base_type = base_param.variable_type.get_actual_type (object_type, method_type_args, this);
378 if (!actual_base_type.equals (param.variable_type)) {
379 invalid_match = "incompatible type of parameter %d".printf (param_index);
380 return false;
383 param_index++;
386 /* this method may not expect more arguments */
387 if (method_params_it.next ()) {
388 invalid_match = "too many parameters";
389 return false;
392 /* this method may throw less but not more errors than the base method */
393 foreach (DataType method_error_type in get_error_types ()) {
394 bool match = false;
395 foreach (DataType base_method_error_type in base_method.get_error_types ()) {
396 if (method_error_type.compatible (base_method_error_type)) {
397 match = true;
398 break;
402 if (!match) {
403 invalid_match = "incompatible error type `%s'".printf (method_error_type.to_string ());
404 return false;
407 if (base_method.coroutine != this.coroutine) {
408 invalid_match = "async mismatch";
409 return false;
412 invalid_match = null;
413 return true;
417 * Appends the specified parameter to the list of type parameters.
419 * @param p a type parameter
421 public void add_type_parameter (TypeParameter p) {
422 if (type_parameters == null) {
423 type_parameters = new ArrayList<TypeParameter> ();
425 type_parameters.add (p);
426 scope.add (p.name, p);
430 * Returns a copy of the type parameter list.
432 * @return list of type parameters
434 public List<TypeParameter> get_type_parameters () {
435 if (type_parameters != null) {
436 return type_parameters;
438 if (_empty_type_parameter_list == null) {
439 _empty_type_parameter_list = new ArrayList<TypeParameter> ();
441 return _empty_type_parameter_list;
444 public int get_type_parameter_index (string name) {
445 if (type_parameters == null) {
446 return -1;
449 int i = 0;
450 foreach (TypeParameter parameter in type_parameters) {
451 if (parameter.name == name) {
452 return i;
454 i++;
456 return -1;
460 * Adds a precondition to this method.
462 * @param precondition a boolean precondition expression
464 public void add_precondition (Expression precondition) {
465 if (preconditions == null) {
466 preconditions = new ArrayList<Expression> ();
468 preconditions.add (precondition);
469 precondition.parent_node = this;
473 * Returns a copy of the list of preconditions of this method.
475 * @return list of preconditions
477 public List<Expression> get_preconditions () {
478 if (preconditions != null) {
479 return preconditions;
481 if (_empty_expression_list == null) {
482 _empty_expression_list = new ArrayList<Expression> ();
484 return _empty_expression_list;
488 * Adds a postcondition to this method.
490 * @param postcondition a boolean postcondition expression
492 public void add_postcondition (Expression postcondition) {
493 if (postconditions == null) {
494 postconditions = new ArrayList<Expression> ();
496 postconditions.add (postcondition);
497 postcondition.parent_node = this;
501 * Returns a copy of the list of postconditions of this method.
503 * @return list of postconditions
505 public List<Expression> get_postconditions () {
506 if (postconditions != null) {
507 return postconditions;
509 if (_empty_expression_list == null) {
510 _empty_expression_list = new ArrayList<Expression> ();
512 return _empty_expression_list;
515 public override void replace_type (DataType old_type, DataType new_type) {
516 if (base_interface_type == old_type) {
517 base_interface_type = new_type;
518 return;
520 if (return_type == old_type) {
521 return_type = new_type;
522 return;
524 var error_types = get_error_types ();
525 for (int i = 0; i < error_types.size; i++) {
526 if (error_types[i] == old_type) {
527 error_types[i] = new_type;
528 return;
533 private void find_base_methods () {
534 if (base_methods_valid) {
535 return;
538 if (parent_symbol is Class) {
539 if (!(this is CreationMethod)) {
540 find_base_interface_method ((Class) parent_symbol);
541 if (is_virtual || is_abstract || overrides) {
542 find_base_class_method ((Class) parent_symbol);
545 } else if (parent_symbol is Interface) {
546 if (is_virtual || is_abstract) {
547 _base_interface_method = this;
551 base_methods_valid = true;
554 private void find_base_class_method (Class cl) {
555 var sym = cl.scope.lookup (name);
556 if (sym is Signal) {
557 var sig = (Signal) sym;
558 sym = sig.default_handler;
560 if (sym is Method) {
561 var base_method = (Method) sym;
562 if (base_method.is_abstract || base_method.is_virtual) {
563 string invalid_match;
564 if (!compatible (base_method, out invalid_match)) {
565 error = true;
566 var base_method_type = new MethodType (base_method);
567 Report.error (source_reference, "overriding method `%s' is incompatible with base method `%s': %s.".printf (get_full_name (), type_to_prototype_string (base_method_type), invalid_match));
568 return;
571 _base_method = base_method;
572 return;
576 if (cl.base_class != null) {
577 find_base_class_method (cl.base_class);
581 private void find_base_interface_method (Class cl) {
582 foreach (DataType type in cl.get_base_types ()) {
583 if (type.data_type is Interface) {
584 if (base_interface_type != null && base_interface_type.data_type != type.data_type) {
585 continue;
588 var sym = type.data_type.scope.lookup (name);
589 if (sym is Signal) {
590 var sig = (Signal) sym;
591 sym = sig.default_handler;
593 if (sym is Method) {
594 var base_method = (Method) sym;
595 if (base_method.is_abstract || base_method.is_virtual) {
596 if (base_interface_type == null) {
597 // check for existing explicit implementation
598 var has_explicit_implementation = false;
599 foreach (var m in cl.get_methods ()) {
600 if (m.base_interface_type != null && base_method == m.base_interface_method) {
601 has_explicit_implementation = true;
602 break;
605 if (has_explicit_implementation) {
606 continue;
610 string invalid_match = null;
611 if (!compatible (base_method, out invalid_match)) {
612 error = true;
613 var base_method_type = new MethodType (base_method);
614 Report.error (source_reference, "overriding method `%s' is incompatible with base method `%s': %s.".printf (get_full_name (), type_to_prototype_string (base_method_type), invalid_match));
615 return;
618 _base_interface_method = base_method;
619 return;
625 if (base_interface_type != null) {
626 Report.error (source_reference, "`%s': no suitable interface method found to implement".printf (get_full_name ()));
630 public override bool check (CodeContext context) {
631 if (checked) {
632 return !error;
635 checked = true;
637 if (get_attribute ("DestroysInstance") != null) {
638 this_parameter.variable_type.value_owned = true;
640 if (get_attribute ("NoThrow") != null) {
641 get_error_types ().clear ();
644 if (is_abstract) {
645 if (parent_symbol is Class) {
646 var cl = (Class) parent_symbol;
647 if (!cl.is_abstract) {
648 error = true;
649 Report.error (source_reference, "Abstract methods may not be declared in non-abstract classes");
650 return false;
652 } else if (!(parent_symbol is Interface)) {
653 error = true;
654 Report.error (source_reference, "Abstract methods may not be declared outside of classes and interfaces");
655 return false;
657 } else if (is_virtual) {
658 if (!(parent_symbol is Class) && !(parent_symbol is Interface)) {
659 error = true;
660 Report.error (source_reference, "Virtual methods may not be declared outside of classes and interfaces");
661 return false;
664 if (parent_symbol is Class) {
665 var cl = (Class) parent_symbol;
666 if (cl.is_compact && cl != context.analyzer.gsource_type) {
667 Report.error (source_reference, "Virtual methods may not be declared in compact classes");
668 return false;
671 } else if (overrides) {
672 if (!(parent_symbol is Class)) {
673 error = true;
674 Report.error (source_reference, "Methods may not be overridden outside of classes");
675 return false;
677 } else if (access == SymbolAccessibility.PROTECTED) {
678 if (!(parent_symbol is Class) && !(parent_symbol is Interface)) {
679 error = true;
680 Report.error (source_reference, "Protected methods may not be declared outside of classes and interfaces");
681 return false;
685 if (is_abstract && body != null) {
686 Report.error (source_reference, "Abstract methods cannot have bodies");
687 } else if ((is_abstract || is_virtual) && external && !external_package && !parent_symbol.external) {
688 Report.error (source_reference, "Extern methods cannot be abstract or virtual");
689 } else if (external && body != null) {
690 Report.error (source_reference, "Extern methods cannot have bodies");
691 } else if (!is_abstract && !external && source_type == SourceFileType.SOURCE && body == null) {
692 Report.error (source_reference, "Non-abstract, non-extern methods must have bodies");
695 if (coroutine && !external_package && !context.has_package ("gio-2.0")) {
696 error = true;
697 Report.error (source_reference, "gio-2.0 package required for async methods");
698 return false;
701 var old_source_file = context.analyzer.current_source_file;
702 var old_symbol = context.analyzer.current_symbol;
704 if (source_reference != null) {
705 context.analyzer.current_source_file = source_reference.file;
707 context.analyzer.current_symbol = this;
709 return_type.check (context);
711 var init_attr = get_attribute ("ModuleInit");
712 if (init_attr != null) {
713 source_reference.file.context.module_init_method = this;
716 if (return_type != null) {
717 return_type.check (context);
720 if (parameters.size == 1 && parameters[0].ellipsis && body != null && binding != MemberBinding.INSTANCE) {
721 // accept just `...' for external methods and instance methods
722 error = true;
723 Report.error (parameters[0].source_reference, "Named parameter required before `...'");
726 var optional_param = false;
727 foreach (Parameter param in parameters) {
728 param.check (context);
729 if (coroutine && param.direction == ParameterDirection.REF) {
730 error = true;
731 Report.error (param.source_reference, "Reference parameters are not supported for async methods");
733 if (optional_param && param.initializer == null && !param.ellipsis) {
734 Report.warning (param.source_reference, "parameter without default follows parameter with default");
735 } else if (param.initializer != null) {
736 optional_param = true;
740 foreach (DataType error_type in get_error_types ()) {
741 error_type.check (context);
743 // check whether error type is at least as accessible as the method
744 if (!context.analyzer.is_type_accessible (this, error_type)) {
745 error = true;
746 Report.error (source_reference, "error type `%s` is less accessible than method `%s`".printf (error_type.to_string (), get_full_name ()));
747 return false;
751 if (result_var != null) {
752 result_var.check (context);
755 if (preconditions != null) {
756 foreach (Expression precondition in preconditions) {
757 precondition.check (context);
761 if (postconditions != null) {
762 foreach (Expression postcondition in postconditions) {
763 postcondition.check (context);
767 if (body != null) {
768 body.check (context);
771 if (context.analyzer.current_struct != null) {
772 if (is_abstract || is_virtual || overrides) {
773 error = true;
774 Report.error (source_reference, "A struct member `%s' cannot be marked as override, virtual, or abstract".printf (get_full_name ()));
775 return false;
777 } else if (overrides && base_method == null) {
778 Report.error (source_reference, "`%s': no suitable method found to override".printf (get_full_name ()));
779 } else if ((is_abstract || is_virtual || overrides) && access == SymbolAccessibility.PRIVATE) {
780 error = true;
781 Report.error (source_reference, "Private member `%s' cannot be marked as override, virtual, or abstract".printf (get_full_name ()));
782 return false;
785 if (base_interface_type != null && base_interface_method != null && parent_symbol is Class) {
786 var cl = (Class) parent_symbol;
787 foreach (var m in cl.get_methods ()) {
788 if (m != this && m.base_interface_method == base_interface_method) {
789 m.checked = true;
790 m.error = true;
791 error = true;
792 Report.error (source_reference, "`%s' already contains an implementation for `%s'".printf (cl.get_full_name (), base_interface_method.get_full_name ()));
793 Report.notice (m.source_reference, "previous implementation of `%s' was here".printf (base_interface_method.get_full_name ()));
794 return false;
799 context.analyzer.current_source_file = old_source_file;
800 context.analyzer.current_symbol = old_symbol;
802 if (!external_package && !overrides && !hides && get_hidden_member () != null) {
803 Report.warning (source_reference, "%s hides inherited method `%s'. Use the `new' keyword if hiding was intentional".printf (get_full_name (), get_hidden_member ().get_full_name ()));
806 // check whether return type is at least as accessible as the method
807 if (!context.analyzer.is_type_accessible (this, return_type)) {
808 error = true;
809 Report.error (source_reference, "return type `%s` is less accessible than method `%s`".printf (return_type.to_string (), get_full_name ()));
810 return false;
813 foreach (Expression precondition in get_preconditions ()) {
814 if (precondition.error) {
815 // if there was an error in the precondition, skip this check
816 error = true;
817 return false;
820 if (!precondition.value_type.compatible (context.analyzer.bool_type)) {
821 error = true;
822 Report.error (precondition.source_reference, "Precondition must be boolean");
823 return false;
827 foreach (Expression postcondition in get_postconditions ()) {
828 if (postcondition.error) {
829 // if there was an error in the postcondition, skip this check
830 error = true;
831 return false;
834 if (!postcondition.value_type.compatible (context.analyzer.bool_type)) {
835 error = true;
836 Report.error (postcondition.source_reference, "Postcondition must be boolean");
837 return false;
841 // check that all errors that can be thrown in the method body are declared
842 if (body != null) {
843 foreach (DataType body_error_type in body.get_error_types ()) {
844 bool can_propagate_error = false;
845 foreach (DataType method_error_type in get_error_types ()) {
846 if (body_error_type.compatible (method_error_type)) {
847 can_propagate_error = true;
850 bool is_dynamic_error = body_error_type is ErrorType && ((ErrorType) body_error_type).dynamic_error;
851 if (!can_propagate_error && !is_dynamic_error) {
852 Report.warning (body_error_type.source_reference, "unhandled error `%s'".printf (body_error_type.to_string()));
857 if (is_possible_entry_point (context)) {
858 if (context.entry_point != null) {
859 error = true;
860 Report.error (source_reference, "program already has an entry point `%s'".printf (context.entry_point.get_full_name ()));
861 return false;
863 entry_point = true;
864 context.entry_point = this;
866 if (tree_can_fail) {
867 Report.error (source_reference, "\"main\" method cannot throw errors");
870 if (is_inline) {
871 Report.error (source_reference, "\"main\" method cannot be inline");
874 if (coroutine) {
875 Report.error (source_reference, "\"main\" method cannot be async");
879 if (get_attribute ("GtkCallback") != null) {
880 used = true;
883 return !error;
886 bool is_possible_entry_point (CodeContext context) {
887 if (external_package) {
888 return false;
891 if (context.entry_point_name == null) {
892 if (name == null || name != "main") {
893 // method must be called "main"
894 return false;
896 } else {
897 // custom entry point name
898 if (get_full_name () != context.entry_point_name) {
899 return false;
903 if (binding == MemberBinding.INSTANCE) {
904 // method must be static
905 return false;
908 if (return_type is VoidType) {
909 } else if (return_type.data_type == context.analyzer.int_type.data_type) {
910 } else {
911 // return type must be void or int
912 return false;
915 var params = get_parameters ();
916 if (params.size == 0) {
917 // method may have no parameters
918 return true;
921 if (params.size > 1) {
922 // method must not have more than one parameter
923 return false;
926 Iterator<Parameter> params_it = params.iterator ();
927 params_it.next ();
928 var param = params_it.get ();
930 if (param.direction == ParameterDirection.OUT) {
931 // parameter must not be an out parameter
932 return false;
935 if (!(param.variable_type is ArrayType)) {
936 // parameter must be an array
937 return false;
940 var array_type = (ArrayType) param.variable_type;
941 if (array_type.element_type.data_type != context.analyzer.string_type.data_type) {
942 // parameter must be an array of strings
943 return false;
946 return true;
949 public int get_required_arguments () {
950 int n = 0;
951 foreach (var param in parameters) {
952 if (param.initializer != null || param.ellipsis) {
953 // optional argument
954 break;
956 n++;
958 return n;
961 public Method get_end_method () {
962 assert (this.coroutine);
964 if (end_method == null) {
965 end_method = new Method ("end", return_type, source_reference);
966 end_method.access = SymbolAccessibility.PUBLIC;
967 end_method.external = true;
968 end_method.owner = scope;
969 foreach (var param in get_async_end_parameters ()) {
970 end_method.add_parameter (param.copy ());
972 foreach (var param in get_type_parameters ()) {
973 end_method.add_type_parameter (param);
976 return end_method;
979 public Method get_callback_method () {
980 assert (this.coroutine);
982 if (callback_method == null) {
983 var bool_type = new BooleanType ((Struct) CodeContext.get ().root.scope.lookup ("bool"));
984 bool_type.value_owned = true;
985 callback_method = new Method ("callback", bool_type, source_reference);
986 callback_method.access = SymbolAccessibility.PUBLIC;
987 callback_method.external = true;
988 callback_method.binding = MemberBinding.INSTANCE;
989 callback_method.owner = scope;
990 callback_method.is_async_callback = true;
992 return callback_method;
995 public List<Parameter> get_async_begin_parameters () {
996 assert (this.coroutine);
998 var glib_ns = CodeContext.get ().root.scope.lookup ("GLib");
1000 var params = new ArrayList<Parameter> ();
1001 Parameter ellipsis = null;
1002 foreach (var param in parameters) {
1003 if (param.ellipsis) {
1004 ellipsis = param;
1005 } else if (param.direction == ParameterDirection.IN) {
1006 params.add (param);
1010 var callback_type = new DelegateType ((Delegate) glib_ns.scope.lookup ("AsyncReadyCallback"));
1011 callback_type.nullable = true;
1012 callback_type.value_owned = true;
1013 callback_type.is_called_once = true;
1015 var callback_param = new Parameter ("_callback_", callback_type);
1016 callback_param.initializer = new NullLiteral (source_reference);
1017 callback_param.initializer.target_type = callback_type.copy ();
1018 callback_param.set_attribute_double ("CCode", "pos", -1);
1019 callback_param.set_attribute_double ("CCode", "delegate_target_pos", -0.9);
1021 params.add (callback_param);
1023 if (ellipsis != null) {
1024 params.add (ellipsis);
1027 return params;
1030 public List<Parameter> get_async_end_parameters () {
1031 assert (this.coroutine);
1033 var params = new ArrayList<Parameter> ();
1035 var glib_ns = CodeContext.get ().root.scope.lookup ("GLib");
1036 var result_type = new ObjectType ((ObjectTypeSymbol) glib_ns.scope.lookup ("AsyncResult"));
1038 var result_param = new Parameter ("_res_", result_type);
1039 result_param.set_attribute_double ("CCode", "pos", 0.1);
1040 params.add (result_param);
1042 foreach (var param in parameters) {
1043 if (param.direction == ParameterDirection.OUT) {
1044 params.add (param);
1048 return params;
1051 public void add_captured_variable (LocalVariable local) {
1052 assert (this.closure);
1054 if (captured_variables == null) {
1055 captured_variables = new ArrayList<LocalVariable> ();
1057 captured_variables.add (local);
1060 public void get_captured_variables (Collection<LocalVariable> variables) {
1061 if (captured_variables != null) {
1062 foreach (var local in captured_variables) {
1063 variables.add (local);
1068 public override void get_defined_variables (Collection<Variable> collection) {
1069 // capturing variables is only supported if they are initialized
1070 // therefore assume that captured variables are initialized
1071 if (closure) {
1072 get_captured_variables ((Collection<LocalVariable>) collection);
1076 public int get_format_arg_index () {
1077 for (int i = 0; i < parameters.size; i++) {
1078 if (parameters[i].format_arg) {
1079 return i;
1082 return -1;
1085 private static string type_to_prototype_string (DataType type) {
1086 return "%s%s".printf (type.is_weak () ? "unowned " : "", type.to_qualified_string ());
1090 // vim:sw=8 noet