summaryrefslogtreecommitdiff
path: root/vala/valasignal.vala
blob: 9eb7c894c61eebeac3db2c798d7ccd867c2bd109 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/* valasignal.vala
 *
 * Copyright (C) 2006-2012  Jürg Billeter
 *
 * 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:
 * 	Jürg Billeter <j@bitron.ch>
 */

using GLib;

/**
 * Represents an object signal. Signals enable objects to provide notifications.
 */
public class Vala.Signal : Symbol, Callable {
	/**
	 * The return type of handlers of this signal.
	 */
	public DataType return_type {
		get { return _return_type; }
		set {
			_return_type = value;
			_return_type.parent_node = this;
		}
	}

	public Block body {
		get { return _body; }
		set {
			_body = value;
			if (_body != null) {
				_body.owner = scope;
			}
		}
	}

	/**
	 * Specifies whether this signal has virtual method handler.
	 */
	public bool is_virtual { get; set; }

	private List<Parameter> parameters = new ArrayList<Parameter> ();
	/**
	 * Refers to the default signal handler, which is an anonymous
	 * function in the scope.
	 * */
	public Method default_handler { get; private set; }

	/**
	 * Refers to the public signal emitter method, which is an anonymous
	 * function in the scope.
	 * */
	public Method emitter { get; private set; }

	private DataType _return_type;

	private Block _body;

	/**
	 * Creates a new signal.
	 *
	 * @param name              signal name
	 * @param return_type       signal return type
	 * @param source_reference  reference to source code
	 * @return                  newly created signal
	 */
	public Signal (string name, DataType return_type, SourceReference? source_reference = null, Comment? comment = null) {
		base (name, source_reference, comment);
		this.return_type = return_type;
	}

	/**
	 * Appends parameter to signal handler.
	 *
	 * @param param a formal parameter
	 */
	public void add_parameter (Parameter param) {
		parameters.add (param);
		scope.add (param.name, param);
	}

	public unowned List<Parameter> get_parameters () {
		return parameters;
	}

	/**
	 * Returns generated delegate to be used for signal handlers.
	 *
	 * @return delegate
	 */
	public Delegate get_delegate (DataType sender_type, CodeNode node_reference) {
		var actual_return_type = return_type.get_actual_type (sender_type, null, node_reference);

		var generated_delegate = new Delegate (null, actual_return_type, source_reference);
		generated_delegate.access = SymbolAccessibility.PUBLIC;
		generated_delegate.owner = scope;

		// sender parameter is never null and doesn't own its value
		var sender_param_type = sender_type.copy ();
		sender_param_type.value_owned = false;
		sender_param_type.nullable = false;

		generated_delegate.sender_type = sender_param_type;

		bool is_generic = actual_return_type.is_generic ();

		foreach (Parameter param in parameters) {
			var actual_param = param.copy ();
			actual_param.variable_type = actual_param.variable_type.get_actual_type (sender_type, null, node_reference);
			generated_delegate.add_parameter (actual_param);

			if (actual_param.variable_type.is_generic ()) {
				is_generic = true;
			}
		}

		if (is_generic) {
			unowned ObjectTypeSymbol cl = (ObjectTypeSymbol) parent_symbol;
			foreach (var type_param in cl.get_type_parameters ()) {
				generated_delegate.add_type_parameter (new TypeParameter (type_param.name, type_param.source_reference));
			}

			// return type and parameter types must refer to the delegate type parameters
			// instead of to the class type parameters
			foreach (var type_param in generated_delegate.get_type_parameters ()) {
				actual_return_type.replace_type_parameter (cl.get_type_parameters ().get (cl.get_type_parameter_index (type_param.name)), type_param);
			}
			foreach (var param in generated_delegate.get_parameters ()) {
				foreach (var type_param in generated_delegate.get_type_parameters ()) {
					param.variable_type.replace_type_parameter (cl.get_type_parameters ().get (cl.get_type_parameter_index (type_param.name)), type_param);
				}
			}
		}

		scope.add (null, generated_delegate);

		return generated_delegate;
	}

	public override void accept (CodeVisitor visitor) {
		visitor.visit_signal (this);
	}

	public override void accept_children (CodeVisitor visitor) {
		return_type.accept (visitor);

		foreach (Parameter param in parameters) {
			param.accept (visitor);
		}
		if (default_handler == null && body != null) {
			body.accept (visitor);
		} else if (default_handler != null) {
			default_handler.accept (visitor);
		}
		if (emitter != null) {
			emitter.accept (visitor);
		}
	}

	public override void replace_type (DataType old_type, DataType new_type) {
		if (return_type == old_type) {
			return_type = new_type;
		}
	}

	public override bool check (CodeContext context) {
		if (checked) {
			return !error;
		}

		checked = true;

		// parent_symbol may be null for dynamic signals
		unowned Class? parent_cl = parent_symbol as Class;
		if (parent_cl != null && parent_cl.is_compact) {
			error = true;
			Report.error (source_reference, "Signals are not supported in compact classes");
			return false;
		}

		if (parent_cl != null) {
			foreach (DataType base_type in parent_cl.get_base_types ()) {
				if (SemanticAnalyzer.symbol_lookup_inherited (base_type.type_symbol, name) is Signal) {
					error = true;
					Report.error (source_reference, "Signals with the same name as a signal in a base type are not supported");
					return false;
				}
			}
		}

		if (this is DynamicSignal) {
			return !error;
		}

		return_type.check (context);
		if (!external_package) {
			context.analyzer.check_type (return_type);
			return_type.check_type_arguments (context, !(return_type is DelegateType));
		}

		if (return_type.type_symbol == context.analyzer.va_list_type.type_symbol) {
			error = true;
			Report.error (source_reference, "`%s' not supported as return type", return_type.type_symbol.get_full_name ());
			return false;
		}

		foreach (Parameter param in parameters) {
			if (param.ellipsis) {
				Report.error  (param.source_reference, "Signals with variable argument lists are not supported");
				return false;
			}

			if (!param.check (context)) {
				error = true;
			}
		}

		if (body != null || (is_virtual && external_package)) {
			default_handler = new Method (name, return_type, source_reference);

			default_handler.owner = owner;
			default_handler.access = (is_virtual ? access : SymbolAccessibility.PRIVATE);
			default_handler.external = external;
			default_handler.hides = hides;
			default_handler.is_virtual = is_virtual;
			default_handler.signal_reference = this;
			default_handler.body = body;

			foreach (Parameter param in parameters) {
				default_handler.add_parameter (param);
			}

			unowned ObjectTypeSymbol? cl = parent_symbol as ObjectTypeSymbol;

			cl.add_hidden_method (default_handler);
			default_handler.check (context);
		}

		if (get_attribute ("HasEmitter") != null) {
			emitter = new Method (name, return_type, source_reference);

			emitter.owner = owner;
			emitter.access = access;

			var body = new Block (source_reference);
			var call = new MethodCall (new MemberAccess.simple (name, source_reference), source_reference);

			foreach (Parameter param in parameters) {
				emitter.add_parameter (param);
				call.add_argument (new MemberAccess.simple (param.name, source_reference));
			}

			if (return_type is VoidType) {
				body.add_statement (new ExpressionStatement (call, source_reference));
			} else {
				body.add_statement (new ReturnStatement (call, source_reference));
			}
			emitter.body = body;

			unowned ObjectTypeSymbol? cl = parent_symbol as ObjectTypeSymbol;

			cl.add_hidden_method (emitter);
			if (!external_package) {
				emitter.check (context);
			}
		}


		if (!external_package && !hides && get_hidden_member () != null) {
			Report.warning (source_reference, "%s hides inherited signal `%s'. Use the `new' keyword if hiding was intentional", get_full_name (), get_hidden_member ().get_full_name ());
		}

		return !error;
	}
}