summaryrefslogtreecommitdiff
path: root/vala/valacallabletype.vala
blob: 0642fa644f30c92b5813d8065c480dc1833eaf03 (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
/* 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 override string to_prototype_string (string? override_name = null) {
		StringBuilder builder = new StringBuilder ();

		// Append return-type
		var return_type = get_return_type ();
		if (return_type.is_weak ()) {
			builder.append ("unowned ");
		}
		builder.append (return_type.to_qualified_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
		var delegate_type = this as DelegateType;
		if (delegate_type != null) {
			var 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.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 = get_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;
	}
}