summaryrefslogtreecommitdiff
path: root/vala/valacallabletype.vala
blob: 6ea2349d0ed99f7d2b7ad05a34ff34adfdad8ee7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* valacallabletype.vala
 *
 * Copyright (C) 2017  Rico Tzschichholz
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 * Author:
 * 	Rico Tzschichholz <ricotz@ubuntu.com>
 */

using GLib;

/**
 * A callable type, i.e. a delegate, method, or signal type.
 */
public abstract class Vala.CallableType : DataType {
	public weak Callable callable_symbol {
		get {
			return (Callable) symbol;
		}
	}

	protected CallableType (Symbol symbol, SourceReference? source_reference = null) {
		base.with_symbol (symbol, source_reference);
	}

	public override bool is_invokable () {
		return true;
	}

	public override unowned DataType? get_return_type () {
		return callable_symbol.return_type;
	}

	public override unowned List<Parameter>? get_parameters () {
		return callable_symbol.get_parameters ();
	}

	public override string to_prototype_string (string? override_name = null) {
		StringBuilder builder = new StringBuilder ();

		unowned DelegateType? delegate_type = this as DelegateType;
		unowned MethodType? method_type = this as MethodType;
		unowned SignalType? signal_type = this as SignalType;

		if (method_type != null && method_type.method_symbol.coroutine) {
			// Only methods can be asynchronous
			builder.append ("async ");
		} else if (delegate_type != null) {
			builder.append ("delegate ");
		} else if (signal_type != null) {
			builder.append ("signal ");
		}

		// Append return-type, but omit return-type for creation methods
		if (method_type == null || !(method_type.method_symbol is CreationMethod)) {
			builder.append (get_return_type ().to_prototype_string ());
		}

		// Append name
		builder.append_c (' ');
		builder.append (override_name ?? this.to_string ());
		builder.append_c (' ');

		// Append parameter-list
		builder.append_c ('(');
		int i = 1;
		// add sender parameter for internal signal-delegates
		if (delegate_type != null) {
			unowned Delegate delegate_symbol = delegate_type.delegate_symbol;
			if (delegate_symbol.parent_symbol is Signal && delegate_symbol.sender_type != null) {
				builder.append (delegate_symbol.sender_type.to_qualified_string ());
				i++;
			}
		}
		foreach (Parameter param in get_parameters ()) {
			if (i > 1) {
				builder.append (", ");
			}

			if (param.ellipsis) {
				builder.append ("...");
				continue;
			}

			if (param.params_array) {
				builder.append ("params ");
			}

			if (param.direction == ParameterDirection.IN) {
				if (param.variable_type.value_owned) {
					builder.append ("owned ");
				}
			} else {
				if (param.direction == ParameterDirection.REF) {
					builder.append ("ref ");
				} else if (param.direction == ParameterDirection.OUT) {
					builder.append ("out ");
				}
				if (!param.variable_type.value_owned && param.variable_type is ReferenceType) {
					builder.append ("weak ");
				}
			}

			builder.append (param.variable_type.to_qualified_string ());

			if (param.initializer != null) {
				builder.append (" = ");
				builder.append (param.initializer.to_string ());
			}

			i++;
		}
		builder.append_c (')');

		// Append error-types
		var error_types = new ArrayList<DataType> ();
		callable_symbol.get_error_types (error_types);
		if (error_types.size > 0) {
			builder.append (" throws ");

			bool first = true;
			foreach (DataType type in error_types) {
				if (!first) {
					builder.append (", ");
				} else {
					first = false;
				}

				builder.append (type.to_string ());
			}
		}

		return builder.str;
	}
}