1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2  * GObject introspection: Type implementation
3  *
4  * Copyright (C) 2005 Matthias Clasen
5  * Copyright (C) 2008,2009 Red Hat, Inc.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22 
23 #include "config.h"
24 
25 #include <glib.h>
26 
27 #include <girepository.h>
28 #include "girepository-private.h"
29 #include "gitypelib-internal.h"
30 
31 /**
32  * SECTION:gitypeinfo
33  * @title: GITypeInfo
34  * @short_description: Struct representing a type
35  *
36  * GITypeInfo represents a type. You can retrieve a type info from
37  * an argument (see #GIArgInfo), a functions return value (see #GIFunctionInfo),
38  * a field (see #GIFieldInfo), a property (see #GIPropertyInfo), a constant
39  * (see #GIConstantInfo) or for a union discriminator (see #GIUnionInfo).
40  *
41  * A type can either be a of a basic type which is a standard C primitive
42  * type or an interface type. For interface types you need to call
43  * g_type_info_get_interface() to get a reference to the base info for that
44  * interface.
45  *
46  * <refsect1 id="gi-gitypeinfo.struct-hierarchy" role="struct_hierarchy">
47  * <title role="struct_hierarchy.title">Struct hierarchy</title>
48  * <synopsis>
49  *   <link linkend="GIBaseInfo">GIBaseInfo</link>
50  *    +----GITypeInfo
51  * </synopsis>
52  * </refsect1>
53  */
54 
55 /**
56  * g_type_info_is_pointer:
57  * @info: a #GITypeInfo
58  *
59  * Obtain if the type is passed as a reference.
60  *
61  * Note that the types of %GI_DIRECTION_OUT and %GI_DIRECTION_INOUT parameters
62  * will only be pointers if the underlying type being transferred is a pointer
63  * (i.e. only if the type of the C function’s formal parameter is a pointer to a
64  * pointer).
65  *
66  * Returns: %TRUE if it is a pointer
67  */
68 gboolean
g_type_info_is_pointer(GITypeInfo * info)69 g_type_info_is_pointer (GITypeInfo *info)
70 {
71   GIRealInfo *rinfo = (GIRealInfo *)info;
72   SimpleTypeBlob *type;
73 
74   g_return_val_if_fail (info != NULL, FALSE);
75   g_return_val_if_fail (GI_IS_TYPE_INFO (info), FALSE);
76 
77   type = (SimpleTypeBlob *)&rinfo->typelib->data[rinfo->offset];
78 
79   if (type->flags.reserved == 0 && type->flags.reserved2 == 0)
80     return type->flags.pointer;
81   else
82     {
83       InterfaceTypeBlob *iface = (InterfaceTypeBlob *)&rinfo->typelib->data[rinfo->offset];
84 
85       return iface->pointer;
86     }
87 }
88 
89 /**
90  * g_type_info_get_tag:
91  * @info: a #GITypeInfo
92  *
93  * Obtain the type tag for the type. See #GITypeTag for a list
94  * of type tags.
95  *
96  * Returns: the type tag
97  */
98 GITypeTag
g_type_info_get_tag(GITypeInfo * info)99 g_type_info_get_tag (GITypeInfo *info)
100 {
101   GIRealInfo *rinfo = (GIRealInfo *)info;
102   SimpleTypeBlob *type;
103 
104   g_return_val_if_fail (info != NULL, GI_TYPE_TAG_BOOLEAN);
105   g_return_val_if_fail (GI_IS_TYPE_INFO (info), GI_TYPE_TAG_BOOLEAN);
106 
107   type = (SimpleTypeBlob *)&rinfo->typelib->data[rinfo->offset];
108 
109   if (rinfo->type_is_embedded)
110     return GI_TYPE_TAG_INTERFACE;
111   else if (type->flags.reserved == 0 && type->flags.reserved2 == 0)
112     return type->flags.tag;
113   else
114     {
115       InterfaceTypeBlob *iface = (InterfaceTypeBlob *)&rinfo->typelib->data[rinfo->offset];
116 
117       return iface->tag;
118     }
119 }
120 
121 /**
122  * g_type_info_get_param_type:
123  * @info: a #GITypeInfo
124  * @n: index of the parameter
125  *
126  * Obtain the parameter type @n.
127  *
128  * Returns: (transfer full): the param type info
129  */
130 GITypeInfo *
g_type_info_get_param_type(GITypeInfo * info,gint n)131 g_type_info_get_param_type (GITypeInfo *info,
132                             gint        n)
133 {
134   GIRealInfo *rinfo = (GIRealInfo *)info;
135   SimpleTypeBlob *type;
136 
137   g_return_val_if_fail (info != NULL, NULL);
138   g_return_val_if_fail (GI_IS_TYPE_INFO (info), NULL);
139 
140   type = (SimpleTypeBlob *)&rinfo->typelib->data[rinfo->offset];
141 
142   if (!(type->flags.reserved == 0 && type->flags.reserved2 == 0))
143     {
144       ParamTypeBlob *param = (ParamTypeBlob *)&rinfo->typelib->data[rinfo->offset];
145 
146       switch (param->tag)
147         {
148           case GI_TYPE_TAG_ARRAY:
149           case GI_TYPE_TAG_GLIST:
150           case GI_TYPE_TAG_GSLIST:
151           case GI_TYPE_TAG_GHASH:
152             return _g_type_info_new ((GIBaseInfo*)info, rinfo->typelib,
153                                     rinfo->offset + sizeof (ParamTypeBlob)
154                                     + sizeof (SimpleTypeBlob) * n);
155             break;
156           default:
157             break;
158         }
159     }
160 
161   return NULL;
162 }
163 
164 /**
165  * g_type_info_get_interface:
166  * @info: a #GITypeInfo
167  *
168  * For types which have #GI_TYPE_TAG_INTERFACE such as GObjects and boxed values,
169  * this function returns full information about the referenced type.  You can then
170  * inspect the type of the returned #GIBaseInfo to further query whether it is
171  * a concrete GObject, a GInterface, a structure, etc. using g_base_info_get_type().
172  *
173  * Returns: (transfer full): the #GIBaseInfo, or %NULL. Free it with
174  * g_base_info_unref() when done.
175  */
176 GIBaseInfo *
g_type_info_get_interface(GITypeInfo * info)177 g_type_info_get_interface (GITypeInfo *info)
178 {
179   GIRealInfo *rinfo = (GIRealInfo *)info;
180 
181   g_return_val_if_fail (info != NULL, NULL);
182   g_return_val_if_fail (GI_IS_TYPE_INFO (info), NULL);
183 
184   /* For embedded types, the given offset is a pointer to the actual blob,
185    * after the end of the field.  In that case we know it's a "subclass" of
186    * CommonBlob, so use that to determine the info type.
187    */
188   if (rinfo->type_is_embedded)
189     {
190       CommonBlob *common = (CommonBlob *)&rinfo->typelib->data[rinfo->offset];
191       GIInfoType info_type;
192 
193       switch (common->blob_type)
194         {
195           case BLOB_TYPE_CALLBACK:
196             info_type = GI_INFO_TYPE_CALLBACK;
197             break;
198           default:
199             g_assert_not_reached ();
200             return NULL;
201         }
202       return (GIBaseInfo *) g_info_new (info_type, (GIBaseInfo*)info, rinfo->typelib,
203                                         rinfo->offset);
204     }
205   else
206     {
207       SimpleTypeBlob *type = (SimpleTypeBlob *)&rinfo->typelib->data[rinfo->offset];
208       if (!(type->flags.reserved == 0 && type->flags.reserved2 == 0))
209         {
210           InterfaceTypeBlob *blob = (InterfaceTypeBlob *)&rinfo->typelib->data[rinfo->offset];
211 
212           if (blob->tag == GI_TYPE_TAG_INTERFACE)
213             return _g_info_from_entry (rinfo->repository, rinfo->typelib, blob->interface);
214         }
215     }
216 
217   return NULL;
218 }
219 
220 /**
221  * g_type_info_get_array_length:
222  * @info: a #GITypeInfo
223  *
224  * Obtain the array length of the type. The type tag must be a
225  * #GI_TYPE_TAG_ARRAY or -1 will returned.
226  *
227  * Returns: the array length, or -1 if the type is not an array
228  */
229 gint
g_type_info_get_array_length(GITypeInfo * info)230 g_type_info_get_array_length (GITypeInfo *info)
231 {
232   GIRealInfo *rinfo = (GIRealInfo *)info;
233   SimpleTypeBlob *type;
234 
235   g_return_val_if_fail (info != NULL, -1);
236   g_return_val_if_fail (GI_IS_TYPE_INFO (info), -1);
237 
238   type = (SimpleTypeBlob *)&rinfo->typelib->data[rinfo->offset];
239 
240   if (!(type->flags.reserved == 0 && type->flags.reserved2 == 0))
241     {
242       ArrayTypeBlob *blob = (ArrayTypeBlob *)&rinfo->typelib->data[rinfo->offset];
243 
244       if (blob->tag == GI_TYPE_TAG_ARRAY)
245 	{
246 	  if (blob->has_length)
247 	    return blob->dimensions.length;
248 	}
249     }
250 
251   return -1;
252 }
253 
254 /**
255  * g_type_info_get_array_fixed_size:
256  * @info: a #GITypeInfo
257  *
258  * Obtain the fixed array size of the type. The type tag must be a
259  * #GI_TYPE_TAG_ARRAY or -1 will returned.
260  *
261  * Returns: the size or -1 if it's not an array
262  */
263 gint
g_type_info_get_array_fixed_size(GITypeInfo * info)264 g_type_info_get_array_fixed_size (GITypeInfo *info)
265 {
266   GIRealInfo *rinfo = (GIRealInfo *)info;
267   SimpleTypeBlob *type;
268 
269   g_return_val_if_fail (info != NULL, 0);
270   g_return_val_if_fail (GI_IS_TYPE_INFO (info), 0);
271 
272   type = (SimpleTypeBlob *)&rinfo->typelib->data[rinfo->offset];
273 
274   if (!(type->flags.reserved == 0 && type->flags.reserved2 == 0))
275     {
276       ArrayTypeBlob *blob = (ArrayTypeBlob *)&rinfo->typelib->data[rinfo->offset];
277 
278       if (blob->tag == GI_TYPE_TAG_ARRAY)
279 	{
280 	  if (blob->has_size)
281 	    return blob->dimensions.size;
282 	}
283     }
284 
285   return -1;
286 }
287 
288 /**
289  * g_type_info_is_zero_terminated:
290  * @info: a #GITypeInfo
291  *
292  * Obtain if the last element of the array is %NULL. The type tag must be a
293  * #GI_TYPE_TAG_ARRAY or %FALSE will returned.
294  *
295  * Returns: %TRUE if zero terminated
296  */
297 gboolean
g_type_info_is_zero_terminated(GITypeInfo * info)298 g_type_info_is_zero_terminated (GITypeInfo *info)
299 {
300   GIRealInfo *rinfo = (GIRealInfo *)info;
301   SimpleTypeBlob *type;
302 
303   g_return_val_if_fail (info != NULL, FALSE);
304   g_return_val_if_fail (GI_IS_TYPE_INFO (info), FALSE);
305 
306   type = (SimpleTypeBlob *)&rinfo->typelib->data[rinfo->offset];
307 
308   if (!(type->flags.reserved == 0 && type->flags.reserved2 == 0))
309     {
310       ArrayTypeBlob *blob = (ArrayTypeBlob *)&rinfo->typelib->data[rinfo->offset];
311 
312       if (blob->tag == GI_TYPE_TAG_ARRAY)
313 	return blob->zero_terminated;
314     }
315 
316   return FALSE;
317 }
318 
319 /**
320  * g_type_info_get_array_type:
321  * @info: a #GITypeInfo
322  *
323  * Obtain the array type for this type. See #GIArrayType for a list of
324  * possible values. If the type tag of this type is not array, -1 will be
325  * returned.
326  *
327  * Returns: the array type or -1
328  */
329 GIArrayType
g_type_info_get_array_type(GITypeInfo * info)330 g_type_info_get_array_type (GITypeInfo *info)
331 {
332   GIRealInfo *rinfo = (GIRealInfo *)info;
333   SimpleTypeBlob *type;
334 
335   g_return_val_if_fail (info != NULL, -1);
336   g_return_val_if_fail (GI_IS_TYPE_INFO (info), -1);
337 
338   type = (SimpleTypeBlob *)&rinfo->typelib->data[rinfo->offset];
339 
340   if (!(type->flags.reserved == 0 && type->flags.reserved2 == 0))
341     {
342       ArrayTypeBlob *blob = (ArrayTypeBlob *)&rinfo->typelib->data[rinfo->offset];
343       g_return_val_if_fail (blob->tag == GI_TYPE_TAG_ARRAY, -1);
344 
345       return blob->array_type;
346     }
347 
348   return -1;
349 }
350 
351 /**
352  * g_type_info_get_storage_type:
353  * @info: a #GITypeInfo
354  *
355  * Obtain the type tag corresponding to the underlying storage type in C for
356  * the type.
357  * See #GITypeTag for a list of type tags.
358  *
359  * Returns: the type tag
360  *
361  * Since: 1.66
362  */
363 GITypeTag
g_type_info_get_storage_type(GITypeInfo * info)364 g_type_info_get_storage_type (GITypeInfo *info)
365 {
366   GITypeTag type_tag = g_type_info_get_tag (info);
367 
368   if (type_tag == GI_TYPE_TAG_INTERFACE)
369     {
370       GIBaseInfo *interface = g_type_info_get_interface (info);
371       GIInfoType info_type = g_base_info_get_type (interface);
372       if (info_type == GI_INFO_TYPE_ENUM || info_type == GI_INFO_TYPE_FLAGS)
373         type_tag = g_enum_info_get_storage_type (interface);
374       g_base_info_unref (interface);
375     }
376 
377   return type_tag;
378 }
379 
380 /**
381  * g_type_info_argument_from_hash_pointer:
382  * @info: a #GITypeInfo
383  * @hash_pointer: A pointer, such as a #GHashTable data pointer
384  * @arg: A #GIArgument to fill in
385  *
386  * GLib data structures, such as #GList, #GSList, and #GHashTable, all store
387  * data pointers.
388  * In the case where the list or hash table is storing single types rather than
389  * structs, these data pointers may have values stuffed into them via macros
390  * such as %GPOINTER_TO_INT.
391  *
392  * Use this function to ensure that all values are correctly extracted from
393  * stuffed pointers, regardless of the machine's architecture or endianness.
394  *
395  * This function fills in the appropriate field of @arg with the value extracted
396  * from @hash_pointer, depending on the storage type of @info.
397  *
398  * Since: 1.66
399  */
400 void
g_type_info_argument_from_hash_pointer(GITypeInfo * info,gpointer hash_pointer,GIArgument * arg)401 g_type_info_argument_from_hash_pointer (GITypeInfo *info,
402                                         gpointer hash_pointer,
403                                         GIArgument *arg)
404 {
405   GITypeTag type_tag = g_type_info_get_storage_type (info);
406 
407   switch (type_tag)
408     {
409       case GI_TYPE_TAG_BOOLEAN:
410         arg->v_boolean = !!GPOINTER_TO_INT (hash_pointer);
411         break;
412       case GI_TYPE_TAG_INT8:
413         arg->v_int8 = (gint8)GPOINTER_TO_INT (hash_pointer);
414         break;
415       case GI_TYPE_TAG_UINT8:
416         arg->v_uint8 = (guint8)GPOINTER_TO_UINT (hash_pointer);
417         break;
418       case GI_TYPE_TAG_INT16:
419         arg->v_int16 = (gint16)GPOINTER_TO_INT (hash_pointer);
420         break;
421       case GI_TYPE_TAG_UINT16:
422         arg->v_uint16 = (guint16)GPOINTER_TO_UINT (hash_pointer);
423         break;
424       case GI_TYPE_TAG_INT32:
425         arg->v_int32 = (gint32)GPOINTER_TO_INT (hash_pointer);
426         break;
427       case GI_TYPE_TAG_UINT32:
428       case GI_TYPE_TAG_UNICHAR:
429         arg->v_uint32 = (guint32)GPOINTER_TO_UINT (hash_pointer);
430         break;
431       case GI_TYPE_TAG_GTYPE:
432         arg->v_size = GPOINTER_TO_SIZE (hash_pointer);
433         break;
434       case GI_TYPE_TAG_UTF8:
435       case GI_TYPE_TAG_FILENAME:
436       case GI_TYPE_TAG_INTERFACE:
437       case GI_TYPE_TAG_ARRAY:
438       case GI_TYPE_TAG_GLIST:
439       case GI_TYPE_TAG_GSLIST:
440       case GI_TYPE_TAG_GHASH:
441       case GI_TYPE_TAG_ERROR:
442         arg->v_pointer = hash_pointer;
443         break;
444       case GI_TYPE_TAG_INT64:
445       case GI_TYPE_TAG_UINT64:
446       case GI_TYPE_TAG_FLOAT:
447       case GI_TYPE_TAG_DOUBLE:
448       default:
449         g_critical ("Unsupported type for pointer-stuffing: %s",
450                     g_type_tag_to_string (type_tag));
451         arg->v_pointer = hash_pointer;
452     }
453 }
454 
455 /**
456  * g_type_info_hash_pointer_from_argument:
457  * @info: a #GITypeInfo
458  * @arg: A #GIArgument with the value to stuff into a pointer
459  *
460  * GLib data structures, such as #GList, #GSList, and #GHashTable, all store
461  * data pointers.
462  * In the case where the list or hash table is storing single types rather than
463  * structs, these data pointers may have values stuffed into them via macros
464  * such as %GPOINTER_TO_INT.
465  *
466  * Use this function to ensure that all values are correctly stuffed into
467  * pointers, regardless of the machine's architecture or endianness.
468  *
469  * This function returns a pointer stuffed with the appropriate field of @arg,
470  * depending on the storage type of @info.
471  *
472  * Returns: A stuffed pointer, that can be stored in a #GHashTable, for example
473  *
474  * Since: 1.66
475  */
476 gpointer
g_type_info_hash_pointer_from_argument(GITypeInfo * info,GIArgument * arg)477 g_type_info_hash_pointer_from_argument (GITypeInfo *info,
478                                         GIArgument *arg)
479 {
480   GITypeTag type_tag = g_type_info_get_storage_type (info);
481 
482   switch (type_tag)
483     {
484       case GI_TYPE_TAG_BOOLEAN:
485         return GINT_TO_POINTER (arg->v_boolean);
486       case GI_TYPE_TAG_INT8:
487         return GINT_TO_POINTER (arg->v_int8);
488       case GI_TYPE_TAG_UINT8:
489         return GUINT_TO_POINTER (arg->v_uint8);
490       case GI_TYPE_TAG_INT16:
491         return GINT_TO_POINTER (arg->v_int16);
492       case GI_TYPE_TAG_UINT16:
493         return GUINT_TO_POINTER (arg->v_uint16);
494       case GI_TYPE_TAG_INT32:
495         return GINT_TO_POINTER (arg->v_int32);
496       case GI_TYPE_TAG_UINT32:
497       case GI_TYPE_TAG_UNICHAR:
498         return GUINT_TO_POINTER (arg->v_uint32);
499       case GI_TYPE_TAG_GTYPE:
500         return GSIZE_TO_POINTER (arg->v_size);
501       case GI_TYPE_TAG_UTF8:
502       case GI_TYPE_TAG_FILENAME:
503       case GI_TYPE_TAG_INTERFACE:
504       case GI_TYPE_TAG_ARRAY:
505       case GI_TYPE_TAG_GLIST:
506       case GI_TYPE_TAG_GSLIST:
507       case GI_TYPE_TAG_GHASH:
508       case GI_TYPE_TAG_ERROR:
509         return arg->v_pointer;
510       case GI_TYPE_TAG_INT64:
511       case GI_TYPE_TAG_UINT64:
512       case GI_TYPE_TAG_FLOAT:
513       case GI_TYPE_TAG_DOUBLE:
514       default:
515         g_critical ("Unsupported type for pointer-stuffing: %s",
516                     g_type_tag_to_string (type_tag));
517         return arg->v_pointer;
518     }
519 }
520