valafield.vala 3.98 KB
Newer Older
1
2
/* valafield.vala
 *
3
 * Copyright (C) 2006-2007  Jürg Billeter
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 *
 * 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 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;
24
using Gee;
25

26
27
28
/**
 * Represents a type or namespace field.
 */
29
public class Vala.Field : Member, Invokable, Lockable {
30
31
32
	/**
	 * The data type of this field.
	 */
33
	public TypeReference! type_reference { get; set; }
Jürg Billeter's avatar
Jürg Billeter committed
34

35
36
37
38
39
40
41
42
43
44
	/**
	 * Specifies the expression to be used to initialize this field.
	 */
	public Expression initializer { get; set; }
	
	/**
	 * Specifies whether this field may only be accessed with an instance of
	 * the contained type.
	 */
	public bool instance {
45
46
		get { return _instance; }
		set { _instance = value; }
47
	}
48
49
50
51
52
53
	
	/**
	 * Specifies whether an array length field should implicitly be created
	 * if the field type is an array.
	 */
	public bool no_array_length { get; set; }
54
55
56
57

	private string cname;
	private bool _instance = true;
	
58
59
	private bool lock_used = false;
	
60
61
62
63
64
65
66
67
68
	/**
	 * Creates a new field.
	 *
	 * @param name   field name
	 * @param type   field type
	 * @param init   initializer expression
	 * @param source reference to source code
	 * @return       newly created field
	 */
69
	public Field (construct string! name, construct TypeReference! type_reference, construct Expression initializer, construct SourceReference source_reference = null) {
70
	}
71

72
	public override void accept (CodeVisitor! visitor) {
73
74
		visitor.visit_member (this);
		
75
76
77
78
		visitor.visit_field (this);
	}

	public override void accept_children (CodeVisitor! visitor) {
79
		type_reference.accept (visitor);
Jürg Billeter's avatar
Jürg Billeter committed
80
		
81
82
83
84
85
86
87
88
89
90
91
92
		if (initializer != null) {
			initializer.accept (visitor);
		}
	}

	/**
	 * Returns the name of this field as it is used in C code.
	 *
	 * @return the name to be used in C code
	 */
	public string! get_cname () {
		if (cname == null) {
93
			cname = get_default_cname ();
94
95
96
97
98
99
100
		}
		return cname;
	}
	
	private void set_cname (string! cname) {
		this.cname = cname;
	}
101
102
103
104
105
106
107
108
109
110
111
112
113
114

	/**
	 * Returns the default name of this field as it is used in C code.
	 *
	 * @return the name to be used in C code by default
	 */
	public string! get_default_cname () {
		if (!instance) {
			return parent_symbol.get_lower_case_cprefix () + name;
		} else {
			return name;
		}
	}

115
	private void process_ccode_attribute (Attribute! a) {
116
117
		if (a.has_argument ("cname")) {
			set_cname (a.get_string ("cname"));
Jürg Billeter's avatar
Jürg Billeter committed
118
		}
119
120
121
122
123
124
		if (a.has_argument ("cheader_filename")) {
			var val = a.get_string ("cheader_filename");
			foreach (string filename in val.split (",")) {
				add_cheader_filename (filename);
			}
		}
125
126
127
128
129
130
131
132
133
	}
	
	/**
	 * Process all associated attributes.
	 */
	public void process_attributes () {
		foreach (Attribute a in attributes) {
			if (a.name == "CCode") {
				process_ccode_attribute (a);
134
135
			} else if (a.name == "NoArrayLength") {
				no_array_length = true;
Jürg Billeter's avatar
Jürg Billeter committed
136
137
			}
		}
138
	}
139

140
	public Collection<FormalParameter> get_parameters () {
141
142
143
144
145
146
147
148
		if (!is_invokable ()) {
			return null;
		}
		
		var cb = (Callback) type_reference.data_type;
		return cb.get_parameters ();
	}
	
149
	public TypeReference get_return_type () {
150
151
152
153
154
155
156
157
		if (!is_invokable ()) {
			return null;
		}
		
		var cb = (Callback) type_reference.data_type;
		return cb.return_type;
	}

158
	public bool is_invokable () {
159
160
		return (type_reference.data_type is Callback);
	}
161
	
162
	public bool get_lock_used () {
163
164
165
		return lock_used;
	}
	
166
	public void set_lock_used (bool used) {
167
168
		lock_used = used;
	}
169
}