remove obsolete ref modifier and callback keyword
[vala-lang.git] / vala / valadatatype.vala
blob53448ae8eab9938bb6401100b32131588d5f155d
1 /* valatype.vala
3 * Copyright (C) 2006-2007 Jürg Billeter, Raffaele Sandrini
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 of the License, or (at your option) any later version.
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.
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
19 * Author:
20 * Jürg Billeter <j@bitron.ch>
21 * Raffaele Sandrini <rasa@gmx.ch>
24 using GLib;
26 /**
27 * Represents a runtime data type. This data type may be defined in Vala source
28 * code or imported from an external library with a Vala API file.
30 public abstract class Vala.DataType : CodeNode {
31 /**
32 * The symbol name of this data type.
34 public string name { get; set; }
36 /**
37 * Specifies the accessibility of the class. Public accessibility
38 * doesn't limit access. Default accessibility limits access to this
39 * program or library. Protected and private accessibility is not
40 * supported for types.
42 public MemberAccessibility access;
44 /**
45 * The namespace containing this data type.
47 public weak Namespace @namespace;
49 private List<string> cheader_filenames;
51 private Pointer pointer_type;
53 /* holds the array types of this type; each rank is a separate one */
54 /* FIXME: uses string because int does not work as key yet */
55 private HashTable<string,Array> array_types = new HashTable.full (str_hash, str_equal, g_free, g_object_unref);
57 /**
58 * Returns the name of this data type as it is used in C code.
60 * @return the name to be used in C code
62 public abstract string get_cname (bool const_type = false);
64 /**
65 * Checks whether this data type has value or reference type semantics.
67 * @return true if this data type has reference type semantics
69 public virtual bool is_reference_type () {
70 return false;
73 /**
74 * Returns the C function name that duplicates instances of this data
75 * type. The specified C function must accept one argument referencing
76 * the instance of this data type and return a reference to the
77 * duplicate.
79 * @return the name of the C function if supported or null otherwise
81 public virtual string get_dup_function () {
82 return null;
85 /**
86 * Returns the C function name that frees instances of this data type.
87 * This is only valid for data types with reference type semantics that
88 * do not support reference counting. The specified C function must
89 * accept one argument pointing to the instance to be freed.
91 * @return the name of the C function or null if this data type is not a
92 * reference type or if it supports reference counting
94 public virtual string get_free_function () {
95 return null;
98 /**
99 * Checks whether this data type supports reference counting. This is
100 * only valid for reference types.
102 * @return true if this data type supports reference counting
104 public virtual bool is_reference_counting () {
105 return false;
109 * Returns the C function name that increments the reference count of
110 * instances of this data type. This is only valid for data types
111 * supporting reference counting. The specified C function must accept
112 * one argument referencing the instance of this data type and return
113 * the reference.
115 * @return the name of the C function or null if this data type does not
116 * support reference counting
118 public virtual string get_ref_function () {
119 return null;
123 * Returns the C function name that decrements the reference count of
124 * instances of this data type. This is only valid for data types
125 * supporting reference counting. The specified C function must accept
126 * one argument referencing the instance of this data type.
128 * @return the name of the C function or null if this data type does not
129 * support reference counting
131 public virtual string get_unref_function () {
132 return null;
136 * Returns the C symbol representing the runtime type id for this data
137 * type. The specified symbol must express a registered GType.
139 * @return the name of the GType name in C code or null if this data
140 * type is not registered with GType
142 public virtual string get_type_id () {
143 return null;
147 * Returns the name of this data type as used in C code marshallers
149 * @return type name for marshallers
151 public virtual string get_marshaller_type_name () {
152 return null;
156 * Returns the cname of the GValue getter function,
158 public virtual string get_get_value_function () {
159 return null;
163 * Returns the cname of the GValue setter function,
165 public virtual string get_set_value_function () {
166 return null;
170 * Returns the C name of this data type in upper case. Words are
171 * separated by underscores. The upper case C name of the namespace is
172 * prefix of the result.
174 * @param infix a string to be placed between namespace and data type
175 * name or null
176 * @return the upper case name to be used in C code
178 public virtual string get_upper_case_cname (string infix = null) {
179 return null;
183 * Returns the C name of this data type in lower case. Words are
184 * separated by underscores. The lower case C name of the namespace is
185 * prefix of the result.
187 * @param infix a string to be placed between namespace and data type
188 * name or null
189 * @return the lower case name to be used in C code
191 public virtual string get_lower_case_cname (string infix = null) {
192 return null;
196 * Returns the string to be prefixed to members of this data type in
197 * lower case when used in C code.
199 * @return the lower case prefix to be used in C code
201 public virtual string get_lower_case_cprefix () {
202 return null;
206 * Returns the default value for the given type. Returning null means
207 * there is no default value (i.e. not that the default name is NULL).
209 * @return the name of the default value
211 public virtual string get_default_value () {
212 return null;
216 * Returns a list of C header filenames users of this data type must
217 * include.
219 * @return list of C header filenames for this data type
221 public virtual List<string> get_cheader_filenames () {
222 if (cheader_filenames == null) {
223 /* default to header filenames of the namespace */
224 foreach (string filename in @namespace.get_cheader_filenames ()) {
225 add_cheader_filename (filename);
228 return cheader_filenames.copy ();
232 * Adds a filename to the list of C header filenames users of this data
233 * type must include.
235 * @param filename a C header filename
237 public void add_cheader_filename (string! filename) {
238 cheader_filenames.append (filename);
242 * Returns the pointer type of this data type.
244 * @return pointer-type for this data type
246 public Pointer! get_pointer () {
247 if (pointer_type == null) {
248 pointer_type = new Pointer (this, source_reference);
249 /* create a new Symbol */
250 pointer_type.symbol = new Symbol (pointer_type);
251 this.symbol.parent_symbol.add (pointer_type.name, pointer_type.symbol);
253 /* link the namespace */
254 pointer_type.@namespace = this.@namespace;
257 return pointer_type;
261 * Returns the array type for elements of this data type.
263 * @param rank the rank the array should be of
264 * @return array type for this data type
266 public Array! get_array (int rank) {
267 Array array_type = (Array) array_types.lookup (rank.to_string ());
269 if (array_type == null) {
270 var new_array_type = new Array (this, rank, source_reference);
271 /* create a new Symbol */
272 new_array_type.symbol = new Symbol (new_array_type);
273 this.symbol.parent_symbol.add (new_array_type.name, new_array_type.symbol);
275 /* add internal length field */
276 new_array_type.symbol.add (new_array_type.get_length_field ().name, new_array_type.get_length_field ().symbol);
277 /* add internal resize method */
278 new_array_type.symbol.add (new_array_type.get_resize_method ().name, new_array_type.get_resize_method ().symbol);
280 /* link the array type to the same source as the container type */
281 new_array_type.source_reference = this.source_reference;
282 /* link the namespace */
283 new_array_type.@namespace = this.@namespace;
285 array_types.insert (rank.to_string (), new_array_type);
287 array_type = new_array_type;
290 return array_type;
294 * Checks whether this data type is a subtype of the specified data
295 * type.
297 * @param t a data type
298 * @return true if t is a supertype of this data type, false otherwise
300 public virtual bool is_subtype_of (DataType! t) {
301 return false;
305 * Return the index of the specified type parameter name.
307 public virtual int get_type_parameter_index (string! name) {
308 return -1;