Line data Source code
1 : /* valacallabletype.vala
2 : *
3 : * Copyright (C) 2017 Rico Tzschichholz
4 : *
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.
9 :
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.
14 :
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
18 : *
19 : * Author:
20 : * Rico Tzschichholz <ricotz@ubuntu.com>
21 : */
22 :
23 : using GLib;
24 :
25 : /**
26 : * A callable type, i.e. a delegate, method, or signal type.
27 : */
28 1429 : public abstract class Vala.CallableType : DataType {
29 : public weak Callable callable_symbol {
30 453899 : get {
31 453899 : return (Callable) symbol;
32 : }
33 : }
34 :
35 1915906 : protected CallableType (Symbol symbol, SourceReference? source_reference = null) {
36 957953 : base.with_symbol (symbol, source_reference);
37 : }
38 :
39 190759 : public override bool is_invokable () {
40 190759 : return true;
41 : }
42 :
43 233602 : public override unowned DataType? get_return_type () {
44 233602 : return callable_symbol.return_type;
45 : }
46 :
47 220279 : public override unowned List<Parameter>? get_parameters () {
48 220279 : return callable_symbol.get_parameters ();
49 : }
50 :
51 18 : public override string to_prototype_string (string? override_name = null) {
52 18 : StringBuilder builder = new StringBuilder ();
53 :
54 18 : unowned DelegateType? delegate_type = this as DelegateType;
55 18 : unowned MethodType? method_type = this as MethodType;
56 18 : unowned SignalType? signal_type = this as SignalType;
57 :
58 18 : if (method_type != null && method_type.method_symbol.coroutine) {
59 : // Only methods can be asynchronous
60 0 : builder.append ("async ");
61 18 : } else if (delegate_type != null) {
62 6 : builder.append ("delegate ");
63 12 : } else if (signal_type != null) {
64 0 : builder.append ("signal ");
65 : }
66 :
67 : // Append return-type, but omit return-type for creation methods
68 18 : if (method_type == null || !(method_type.method_symbol is CreationMethod)) {
69 36 : builder.append (get_return_type ().to_prototype_string ());
70 : }
71 :
72 : // Append name
73 18 : builder.append_c (' ');
74 36 : builder.append (override_name ?? this.to_string ());
75 18 : builder.append_c (' ');
76 :
77 : // Append parameter-list
78 18 : builder.append_c ('(');
79 18 : int i = 1;
80 : // add sender parameter for internal signal-delegates
81 18 : if (delegate_type != null) {
82 6 : unowned Delegate delegate_symbol = delegate_type.delegate_symbol;
83 6 : if (delegate_symbol.parent_symbol is Signal && delegate_symbol.sender_type != null) {
84 1 : builder.append (delegate_symbol.sender_type.to_qualified_string ());
85 1 : i++;
86 : }
87 : }
88 32 : foreach (Parameter param in get_parameters ()) {
89 7 : if (i > 1) {
90 0 : builder.append (", ");
91 : }
92 :
93 7 : if (param.ellipsis) {
94 0 : builder.append ("...");
95 0 : continue;
96 : }
97 :
98 7 : if (param.params_array) {
99 0 : builder.append ("params ");
100 : }
101 :
102 7 : if (param.direction == ParameterDirection.IN) {
103 7 : if (param.variable_type.value_owned) {
104 1 : builder.append ("owned ");
105 : }
106 : } else {
107 0 : if (param.direction == ParameterDirection.REF) {
108 0 : builder.append ("ref ");
109 0 : } else if (param.direction == ParameterDirection.OUT) {
110 0 : builder.append ("out ");
111 : }
112 0 : if (!param.variable_type.value_owned && param.variable_type is ReferenceType) {
113 0 : builder.append ("weak ");
114 : }
115 : }
116 :
117 14 : builder.append (param.variable_type.to_qualified_string ());
118 :
119 7 : if (param.initializer != null) {
120 0 : builder.append (" = ");
121 0 : builder.append (param.initializer.to_string ());
122 : }
123 :
124 7 : i++;
125 : }
126 18 : builder.append_c (')');
127 :
128 : // Append error-types
129 18 : var error_types = new ArrayList<DataType> ();
130 18 : callable_symbol.get_error_types (error_types);
131 18 : if (error_types.size > 0) {
132 5 : builder.append (" throws ");
133 :
134 5 : bool first = true;
135 15 : foreach (DataType type in error_types) {
136 5 : if (!first) {
137 0 : builder.append (", ");
138 : } else {
139 : first = false;
140 : }
141 :
142 10 : builder.append (type.to_string ());
143 : }
144 : }
145 :
146 36 : return builder.str;
147 : }
148 : }
|