1/* valasignaltype.vala
2 *
3 * Copyright (C) 2007-2011  Jürg Billeter
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 * 	Jürg Billeter <j@bitron.ch>
21 */
22
23using GLib;
24
25/**
26 * The type of a signal referencea.
27 */
28public class Vala.SignalType : CallableType {
29	public weak Signal signal_symbol {
30		get {
31			return (Signal) symbol;
32		}
33	}
34
35	Method? connect_method;
36	Method? connect_after_method;
37	Method? disconnect_method;
38
39	public SignalType (Signal signal_symbol) {
40		base (signal_symbol);
41	}
42
43	public override DataType copy () {
44		return new SignalType (signal_symbol);
45	}
46
47	public override bool compatible (DataType target_type) {
48		return false;
49	}
50
51	public override string to_qualified_string (Scope? scope) {
52		return signal_symbol.get_full_name ();
53	}
54
55	public DelegateType get_handler_type () {
56		var type_sym = (ObjectTypeSymbol) signal_symbol.parent_symbol;
57		var sender_type = SemanticAnalyzer.get_data_type_for_symbol (type_sym);
58		var result = new DelegateType (signal_symbol.get_delegate (sender_type, this));
59		result.value_owned = true;
60
61		if (result.delegate_symbol.has_type_parameters ()) {
62			foreach (var type_param in type_sym.get_type_parameters ()) {
63				var type_arg = new GenericType (type_param);
64				type_arg.value_owned = true;
65				result.add_type_argument (type_arg);
66			}
67		}
68
69		return result;
70	}
71
72	unowned Method get_connect_method () {
73		if (connect_method == null) {
74			var ulong_type = new IntegerType ((Struct) CodeContext.get ().root.scope.lookup ("ulong"));
75			connect_method = new Method ("connect", ulong_type);
76			connect_method.access = SymbolAccessibility.PUBLIC;
77			connect_method.external = true;
78			connect_method.owner = signal_symbol.scope;
79			connect_method.add_parameter (new Parameter ("handler", get_handler_type ()));
80		}
81		return connect_method;
82	}
83
84	unowned Method get_connect_after_method () {
85		if (connect_after_method == null) {
86			var ulong_type = new IntegerType ((Struct) CodeContext.get ().root.scope.lookup ("ulong"));
87			connect_after_method = new Method ("connect_after", ulong_type);
88			connect_after_method.access = SymbolAccessibility.PUBLIC;
89			connect_after_method.external = true;
90			connect_after_method.owner = signal_symbol.scope;
91			connect_after_method.add_parameter (new Parameter ("handler", get_handler_type ()));
92		}
93		return connect_after_method;
94	}
95
96	unowned Method get_disconnect_method () {
97		if (disconnect_method == null) {
98			disconnect_method = new Method ("disconnect", new VoidType ());
99			disconnect_method.access = SymbolAccessibility.PUBLIC;
100			disconnect_method.external = true;
101			disconnect_method.owner = signal_symbol.scope;
102			disconnect_method.add_parameter (new Parameter ("handler", get_handler_type ()));
103		}
104		return disconnect_method;
105	}
106
107	public override Symbol? get_member (string member_name) {
108		if (member_name == "connect") {
109			return get_connect_method ();
110		} else if (member_name == "connect_after") {
111			return get_connect_after_method ();
112		} else if (member_name == "disconnect") {
113			return get_disconnect_method ();
114		}
115		return null;
116	}
117
118	public override bool is_accessible (Symbol sym) {
119		return signal_symbol.is_accessible (sym);
120	}
121}
122