summaryrefslogtreecommitdiff
path: root/vala/valadatatype.vala
blob: ac2eecd1743a285d0ff88eb1be0adc00f673c238 (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
/* valatype.vala
 *
 * Copyright (C) 2006-2007  Jürg Billeter, Raffaele Sandrini
 *
 * 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>
 *	Raffaele Sandrini <rasa@gmx.ch>
 */

using GLib;

/**
 * Represents a runtime data type. This data type may be defined in Vala source
 * code or imported from an external library with a Vala API file.
 */
public abstract class Vala.DataType : Symbol {
	/**
	 * Specifies the accessibility of the class. Public accessibility
	 * doesn't limit access. Default accessibility limits access to this
	 * program or library. Protected and private accessibility is not
	 * supported for types.
	 */
	public MemberAccessibility access;
	
	private List<string> cheader_filenames;

	private Pointer pointer_type;

	/* holds the array types of this type; each rank is a separate one */
	private HashTable<int,Array> array_types;

	/**
	 * Returns the name of this data type as it is used in C code.
	 *
	 * @return the name to be used in C code
	 */
	public abstract string get_cname (bool const_type = false);
	
	/**
	 * Checks whether this data type has value or reference type semantics.
	 *
	 * @return true if this data type has reference type semantics
	 */
	public virtual bool is_reference_type () {
		return false;
	}
	
	/**
	 * Returns the C function name that duplicates instances of this data
	 * type. The specified C function must accept one argument referencing
	 * the instance of this data type and return a reference to the
	 * duplicate.
	 *
	 * @return the name of the C function if supported or null otherwise
	 */
	public virtual string get_dup_function () {
		return null;
	}
	
	/**
	 * Returns the C function name that frees instances of this data type.
	 * This is only valid for data types with reference type semantics that
	 * do not support reference counting. The specified C function must
	 * accept one argument pointing to the instance to be freed.
	 *
	 * @return the name of the C function or null if this data type is not a
	 *         reference type or if it supports reference counting
	 */
	public virtual string get_free_function () {
		return null;
	}
	
	/**
	 * Checks whether this data type supports reference counting. This is
	 * only valid for reference types.
	 *
	 * @return true if this data type supports reference counting
	 */
	public virtual bool is_reference_counting () {
		return false;
	}
	
	/**
	 * Returns the C function name that increments the reference count of
	 * instances of this data type. This is only valid for data types
	 * supporting reference counting. The specified C function must accept
	 * one argument referencing the instance of this data type and return
	 * the reference.
	 *
	 * @return the name of the C function or null if this data type does not
	 *         support reference counting
	 */
	public virtual string get_ref_function () {
		return null;
	}
	
	/**
	 * Returns the C function name that decrements the reference count of
	 * instances of this data type. This is only valid for data types
	 * supporting reference counting. The specified C function must accept
	 * one argument referencing the instance of this data type.
	 *
	 * @return the name of the C function or null if this data type does not
	 *         support reference counting
	 */
	public virtual string get_unref_function () {
		return null;
	}
	
	/**
	 * Returns the C symbol representing the runtime type id for this data
	 * type. The specified symbol must express a registered GType.
	 *
	 * @return the name of the GType name in C code or null if this data
	 *         type is not registered with GType
	 */
	public virtual string get_type_id () {
		return null;
	}
	
	/**
	 * Returns the name of this data type as used in C code marshallers
	 *
	 * @return type name for marshallers
	 */
	public virtual string get_marshaller_type_name () {
		return null;
	}
	
	/**
	 * Returns the cname of the GValue getter function,
	 */
	public virtual string get_get_value_function () {
		return null;
	}
	
	/**
	 * Returns the cname of the GValue setter function,
	 */
	public virtual string get_set_value_function () {
		return null;
	}
	
	/**
	 * Returns the C name of this data type in upper case. Words are
	 * separated by underscores. The upper case C name of the namespace is
	 * prefix of the result.
	 *
	 * @param infix a string to be placed between namespace and data type
	 *              name or null
	 * @return      the upper case name to be used in C code
	 */
	public virtual string get_upper_case_cname (string infix = null) {
		return null;
	}

	/**
	 * Returns the default value for the given type. Returning null means
	 * there is no default value (i.e. not that the default name is NULL).
	 *
	 * @return	the name of the default value
	 */
	public virtual string get_default_value () {
		return null;
	}

	public override List<weak string> get_cheader_filenames () {
		if (cheader_filenames == null) {
			/* default to header filenames of the namespace */
			foreach (string filename in parent_symbol.get_cheader_filenames ()) {
				add_cheader_filename (filename);
			}

			if (cheader_filenames == null && source_reference != null && !source_reference.file.pkg) {
				// don't add default include directives for VAPI files
				cheader_filenames.append (source_reference.file.get_cinclude_filename ());
			}
		}
		return cheader_filenames.copy ();
	}

	/**
	 * Adds a filename to the list of C header filenames users of this data
	 * type must include.
	 *
	 * @param filename a C header filename
	 */
	public void add_cheader_filename (string! filename) {
		cheader_filenames.append (filename);
	}
	
	/**
	 * Returns the pointer type of this data type.
	 *
	 * @return pointer-type for this data type
	 */
	public Pointer! get_pointer () {
		if (pointer_type == null) {
			pointer_type = new Pointer (this, source_reference);
			/* create a new Symbol */
			parent_symbol.scope.add (pointer_type.name, pointer_type);

			/* link the namespace */
			pointer_type.owner = parent_symbol.scope;
		}

		return pointer_type;
	}
	
	/**
	 * Returns the array type for elements of this data type.
	 *
	 * @param rank the rank the array should be of
	 * @return array type for this data type
	 */
	public Array! get_array (int rank) {
		Array array_type = null;

		if (array_types != null) {
			array_type = array_types.lookup (rank);
		}

		if (array_type == null) {
			if (array_types == null) {
				array_types = new HashTable.full (direct_hash, direct_equal, null, g_object_unref);
			}

			var new_array_type = new Array (this, rank, source_reference);
			parent_symbol.scope.add (new_array_type.name, new_array_type);

			/* add internal length field */
			new_array_type.scope.add (new_array_type.get_length_field ().name, new_array_type.get_length_field ());
			/* add internal resize method */
			new_array_type.scope.add (new_array_type.get_resize_method ().name, new_array_type.get_resize_method ());
			/* add internal move method */
			new_array_type.scope.add (new_array_type.get_move_method ().name, new_array_type.get_move_method ());

			/* link the array type to the same source as the container type */
			new_array_type.source_reference = this.source_reference;
			
			array_types.insert (rank, new_array_type);
			
			array_type = new_array_type;
		}
		
		return array_type;
	}

	/**
	 * Checks whether this data type is a subtype of the specified data
	 * type.
	 *
	 * @param t a data type
	 * @return  true if t is a supertype of this data type, false otherwise
	 */
	public virtual bool is_subtype_of (DataType! t) {
		return false;
	}
	
	/**
	 * Return the index of the specified type parameter name.
	 */
	public virtual int get_type_parameter_index (string! name) {
		return -1;
	}
}