1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2 * GObject introspection: Base struct 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 <stdlib.h>
26 #include <string.h>
27
28 #include <glib.h>
29 #include <glib-object.h>
30
31 #include "gitypelib-internal.h"
32 #include "girepository-private.h"
33
34 #define INVALID_REFCOUNT 0x7FFFFFFF
35
36 /* GBoxed registration of BaseInfo. */
37 GType
g_base_info_gtype_get_type(void)38 g_base_info_gtype_get_type (void)
39 {
40 static GType our_type = 0;
41
42 if (our_type == 0)
43 our_type =
44 g_boxed_type_register_static ("GIBaseInfo",
45 (GBoxedCopyFunc) g_base_info_ref,
46 (GBoxedFreeFunc) g_base_info_unref);
47
48 return our_type;
49 }
50
51 /* info creation */
52 GIBaseInfo *
_g_info_new_full(GIInfoType type,GIRepository * repository,GIBaseInfo * container,GITypelib * typelib,guint32 offset)53 _g_info_new_full (GIInfoType type,
54 GIRepository *repository,
55 GIBaseInfo *container,
56 GITypelib *typelib,
57 guint32 offset)
58 {
59 GIRealInfo *info;
60
61 g_return_val_if_fail (container != NULL || repository != NULL, NULL);
62
63 info = g_slice_new (GIRealInfo);
64
65 _g_info_init (info, type, repository, container, typelib, offset);
66 info->ref_count = 1;
67
68 if (container && ((GIRealInfo *) container)->ref_count != INVALID_REFCOUNT)
69 g_base_info_ref (info->container);
70
71 g_object_ref (info->repository);
72
73 return (GIBaseInfo*)info;
74 }
75
76 /**
77 * g_info_new:
78 * @type: TODO
79 * @container: TODO
80 * @typelib: TODO
81 * @offset: TODO
82 *
83 * TODO
84 *
85 * Returns: TODO
86 */
87 GIBaseInfo *
g_info_new(GIInfoType type,GIBaseInfo * container,GITypelib * typelib,guint32 offset)88 g_info_new (GIInfoType type,
89 GIBaseInfo *container,
90 GITypelib *typelib,
91 guint32 offset)
92 {
93 return _g_info_new_full (type, ((GIRealInfo*)container)->repository, container, typelib, offset);
94 }
95
96 void
_g_info_init(GIRealInfo * info,GIInfoType type,GIRepository * repository,GIBaseInfo * container,GITypelib * typelib,guint32 offset)97 _g_info_init (GIRealInfo *info,
98 GIInfoType type,
99 GIRepository *repository,
100 GIBaseInfo *container,
101 GITypelib *typelib,
102 guint32 offset)
103 {
104 memset (info, 0, sizeof (GIRealInfo));
105
106 /* Invalid refcount used to flag stack-allocated infos */
107 info->ref_count = INVALID_REFCOUNT;
108 info->type = type;
109
110 info->typelib = typelib;
111 info->offset = offset;
112
113 if (container)
114 info->container = container;
115
116 g_assert (G_IS_IREPOSITORY (repository));
117 info->repository = repository;
118 }
119
120 GIBaseInfo *
_g_info_from_entry(GIRepository * repository,GITypelib * typelib,guint16 index)121 _g_info_from_entry (GIRepository *repository,
122 GITypelib *typelib,
123 guint16 index)
124 {
125 GIBaseInfo *result;
126 DirEntry *entry = g_typelib_get_dir_entry (typelib, index);
127
128 if (entry->local)
129 result = _g_info_new_full (entry->blob_type, repository, NULL, typelib, entry->offset);
130 else
131 {
132 const gchar *namespace = g_typelib_get_string (typelib, entry->offset);
133 const gchar *name = g_typelib_get_string (typelib, entry->name);
134
135 result = g_irepository_find_by_name (repository, namespace, name);
136 if (result == NULL)
137 {
138 GIUnresolvedInfo *unresolved;
139
140 unresolved = g_slice_new0 (GIUnresolvedInfo);
141
142 unresolved->type = GI_INFO_TYPE_UNRESOLVED;
143 unresolved->ref_count = 1;
144 unresolved->repository = g_object_ref (repository);
145 unresolved->container = NULL;
146 unresolved->name = name;
147 unresolved->namespace = namespace;
148
149 return (GIBaseInfo *)unresolved;
150 }
151 return (GIBaseInfo *)result;
152 }
153
154 return (GIBaseInfo *)result;
155 }
156
157 GITypeInfo *
_g_type_info_new(GIBaseInfo * container,GITypelib * typelib,guint32 offset)158 _g_type_info_new (GIBaseInfo *container,
159 GITypelib *typelib,
160 guint32 offset)
161 {
162 SimpleTypeBlob *type = (SimpleTypeBlob *)&typelib->data[offset];
163
164 return (GITypeInfo *) g_info_new (GI_INFO_TYPE_TYPE, container, typelib,
165 (type->flags.reserved == 0 && type->flags.reserved2 == 0) ? offset : type->offset);
166 }
167
168 void
_g_type_info_init(GIBaseInfo * info,GIBaseInfo * container,GITypelib * typelib,guint32 offset)169 _g_type_info_init (GIBaseInfo *info,
170 GIBaseInfo *container,
171 GITypelib *typelib,
172 guint32 offset)
173 {
174 GIRealInfo *rinfo = (GIRealInfo*)container;
175 SimpleTypeBlob *type = (SimpleTypeBlob *)&typelib->data[offset];
176
177 _g_info_init ((GIRealInfo*)info, GI_INFO_TYPE_TYPE, rinfo->repository, container, typelib,
178 (type->flags.reserved == 0 && type->flags.reserved2 == 0) ? offset : type->offset);
179 }
180
181 /* GIBaseInfo functions */
182
183 /**
184 * SECTION:gibaseinfo
185 * @title: GIBaseInfo
186 * @short_description: Base struct for all GITypelib structs
187 *
188 * GIBaseInfo is the common base struct of all other *Info structs
189 * accessible through the #GIRepository API.
190 * All other structs can be casted to a #GIBaseInfo, for instance:
191 * <example>
192 * <title>Casting a #GIFunctionInfo to #GIBaseInfo</title>
193 * <programlisting>
194 * GIFunctionInfo *function_info = ...;
195 * GIBaseInfo *info = (GIBaseInfo*)function_info;
196 * </programlisting>
197 * </example>
198 * Most #GIRepository APIs returning a #GIBaseInfo is actually creating a new struct, in other
199 * words, g_base_info_unref() has to be called when done accessing the data.
200 * GIBaseInfos are normally accessed by calling either
201 * g_irepository_find_by_name(), g_irepository_find_by_gtype() or g_irepository_get_info().
202 *
203 * <example>
204 * <title>Getting the Button of the Gtk typelib</title>
205 * <programlisting>
206 * GIBaseInfo *button_info = g_irepository_find_by_name(NULL, "Gtk", "Button");
207 * ... use button_info ...
208 * g_base_info_unref(button_info);
209 * </programlisting>
210 * </example>
211 *
212 * <refsect1 id="gi-gibaseinfo.struct-hierarchy" role="struct_hierarchy">
213 * <title role="struct_hierarchy.title">Struct hierarchy</title>
214 * <synopsis>
215 * GIBaseInfo
216 * +----<link linkend="gi-GIArgInfo">GIArgInfo</link>
217 * +----<link linkend="gi-GICallableInfo">GICallableInfo</link>
218 * +----<link linkend="gi-GIConstantInfo">GIConstantInfo</link>
219 * +----<link linkend="gi-GIFieldInfo">GIFieldInfo</link>
220 * +----<link linkend="gi-GIPropertyInfo">GIPropertyInfo</link>
221 * +----<link linkend="gi-GIRegisteredTypeInfo">GIRegisteredTypeInfo</link>
222 * +----<link linkend="gi-GITypeInfo">GITypeInfo</link>
223 * </synopsis>
224 * </refsect1>
225 */
226
227 /**
228 * g_base_info_ref: (skip)
229 * @info: a #GIBaseInfo
230 *
231 * Increases the reference count of @info.
232 *
233 * Returns: the same @info.
234 */
235 GIBaseInfo *
g_base_info_ref(GIBaseInfo * info)236 g_base_info_ref (GIBaseInfo *info)
237 {
238 GIRealInfo *rinfo = (GIRealInfo*)info;
239
240 g_assert (rinfo->ref_count != INVALID_REFCOUNT);
241 g_atomic_int_inc (&rinfo->ref_count);
242
243 return info;
244 }
245
246 /**
247 * g_base_info_unref: (skip)
248 * @info: a #GIBaseInfo
249 *
250 * Decreases the reference count of @info. When its reference count
251 * drops to 0, the info is freed.
252 */
253 void
g_base_info_unref(GIBaseInfo * info)254 g_base_info_unref (GIBaseInfo *info)
255 {
256 GIRealInfo *rinfo = (GIRealInfo*)info;
257
258 g_assert (rinfo->ref_count > 0 && rinfo->ref_count != INVALID_REFCOUNT);
259
260 if (!g_atomic_int_dec_and_test (&rinfo->ref_count))
261 return;
262
263 if (rinfo->container && ((GIRealInfo *) rinfo->container)->ref_count != INVALID_REFCOUNT)
264 g_base_info_unref (rinfo->container);
265
266 if (rinfo->repository)
267 g_object_unref (rinfo->repository);
268
269 if (rinfo->type == GI_INFO_TYPE_UNRESOLVED)
270 g_slice_free (GIUnresolvedInfo, (GIUnresolvedInfo *) rinfo);
271 else
272 g_slice_free (GIRealInfo, rinfo);
273 }
274
275 /**
276 * g_base_info_get_type:
277 * @info: a #GIBaseInfo
278 *
279 * Obtain the info type of the GIBaseInfo.
280 *
281 * Returns: the info type of @info
282 */
283 GIInfoType
g_base_info_get_type(GIBaseInfo * info)284 g_base_info_get_type (GIBaseInfo *info)
285 {
286
287 return ((GIRealInfo*)info)->type;
288 }
289
290 /**
291 * g_base_info_get_name:
292 * @info: a #GIBaseInfo
293 *
294 * Obtain the name of the @info. What the name represents depends on
295 * the #GIInfoType of the @info. For instance for #GIFunctionInfo it is
296 * the name of the function.
297 *
298 * Returns: the name of @info or %NULL if it lacks a name.
299 */
300 const gchar *
g_base_info_get_name(GIBaseInfo * info)301 g_base_info_get_name (GIBaseInfo *info)
302 {
303 GIRealInfo *rinfo = (GIRealInfo*)info;
304 g_assert (rinfo->ref_count > 0);
305 switch (rinfo->type)
306 {
307 case GI_INFO_TYPE_FUNCTION:
308 case GI_INFO_TYPE_CALLBACK:
309 case GI_INFO_TYPE_STRUCT:
310 case GI_INFO_TYPE_BOXED:
311 case GI_INFO_TYPE_ENUM:
312 case GI_INFO_TYPE_FLAGS:
313 case GI_INFO_TYPE_OBJECT:
314 case GI_INFO_TYPE_INTERFACE:
315 case GI_INFO_TYPE_CONSTANT:
316 case GI_INFO_TYPE_INVALID_0:
317 case GI_INFO_TYPE_UNION:
318 {
319 CommonBlob *blob = (CommonBlob *)&rinfo->typelib->data[rinfo->offset];
320
321 return g_typelib_get_string (rinfo->typelib, blob->name);
322 }
323 break;
324
325 case GI_INFO_TYPE_VALUE:
326 {
327 ValueBlob *blob = (ValueBlob *)&rinfo->typelib->data[rinfo->offset];
328
329 return g_typelib_get_string (rinfo->typelib, blob->name);
330 }
331 break;
332
333 case GI_INFO_TYPE_SIGNAL:
334 {
335 SignalBlob *blob = (SignalBlob *)&rinfo->typelib->data[rinfo->offset];
336
337 return g_typelib_get_string (rinfo->typelib, blob->name);
338 }
339 break;
340
341 case GI_INFO_TYPE_PROPERTY:
342 {
343 PropertyBlob *blob = (PropertyBlob *)&rinfo->typelib->data[rinfo->offset];
344
345 return g_typelib_get_string (rinfo->typelib, blob->name);
346 }
347 break;
348
349 case GI_INFO_TYPE_VFUNC:
350 {
351 VFuncBlob *blob = (VFuncBlob *)&rinfo->typelib->data[rinfo->offset];
352
353 return g_typelib_get_string (rinfo->typelib, blob->name);
354 }
355 break;
356
357 case GI_INFO_TYPE_FIELD:
358 {
359 FieldBlob *blob = (FieldBlob *)&rinfo->typelib->data[rinfo->offset];
360
361 return g_typelib_get_string (rinfo->typelib, blob->name);
362 }
363 break;
364
365 case GI_INFO_TYPE_ARG:
366 {
367 ArgBlob *blob = (ArgBlob *)&rinfo->typelib->data[rinfo->offset];
368
369 return g_typelib_get_string (rinfo->typelib, blob->name);
370 }
371 break;
372 case GI_INFO_TYPE_UNRESOLVED:
373 {
374 GIUnresolvedInfo *unresolved = (GIUnresolvedInfo *)info;
375
376 return unresolved->name;
377 }
378 break;
379 case GI_INFO_TYPE_TYPE:
380 return NULL;
381 default: ;
382 g_assert_not_reached ();
383 /* unnamed */
384 }
385
386 return NULL;
387 }
388
389 /**
390 * g_base_info_get_namespace:
391 * @info: a #GIBaseInfo
392 *
393 * Obtain the namespace of @info.
394 *
395 * Returns: the namespace
396 */
397 const gchar *
g_base_info_get_namespace(GIBaseInfo * info)398 g_base_info_get_namespace (GIBaseInfo *info)
399 {
400 GIRealInfo *rinfo = (GIRealInfo*) info;
401 Header *header = (Header *)rinfo->typelib->data;
402
403 g_assert (rinfo->ref_count > 0);
404
405 if (rinfo->type == GI_INFO_TYPE_UNRESOLVED)
406 {
407 GIUnresolvedInfo *unresolved = (GIUnresolvedInfo *)info;
408
409 return unresolved->namespace;
410 }
411
412 return g_typelib_get_string (rinfo->typelib, header->namespace);
413 }
414
415 /**
416 * g_base_info_is_deprecated:
417 * @info: a #GIBaseInfo
418 *
419 * Obtain whether the @info is represents a metadata which is
420 * deprecated or not.
421 *
422 * Returns: %TRUE if deprecated
423 */
424 gboolean
g_base_info_is_deprecated(GIBaseInfo * info)425 g_base_info_is_deprecated (GIBaseInfo *info)
426 {
427 GIRealInfo *rinfo = (GIRealInfo*) info;
428 switch (rinfo->type)
429 {
430 case GI_INFO_TYPE_FUNCTION:
431 case GI_INFO_TYPE_CALLBACK:
432 case GI_INFO_TYPE_STRUCT:
433 case GI_INFO_TYPE_BOXED:
434 case GI_INFO_TYPE_ENUM:
435 case GI_INFO_TYPE_FLAGS:
436 case GI_INFO_TYPE_OBJECT:
437 case GI_INFO_TYPE_INTERFACE:
438 case GI_INFO_TYPE_CONSTANT:
439 case GI_INFO_TYPE_INVALID_0:
440 {
441 CommonBlob *blob = (CommonBlob *)&rinfo->typelib->data[rinfo->offset];
442
443 return blob->deprecated;
444 }
445 break;
446
447 case GI_INFO_TYPE_VALUE:
448 {
449 ValueBlob *blob = (ValueBlob *)&rinfo->typelib->data[rinfo->offset];
450
451 return blob->deprecated;
452 }
453 break;
454
455 case GI_INFO_TYPE_SIGNAL:
456 {
457 SignalBlob *blob = (SignalBlob *)&rinfo->typelib->data[rinfo->offset];
458
459 return blob->deprecated;
460 }
461 break;
462
463 case GI_INFO_TYPE_PROPERTY:
464 {
465 PropertyBlob *blob = (PropertyBlob *)&rinfo->typelib->data[rinfo->offset];
466
467 return blob->deprecated;
468 }
469 break;
470
471 case GI_INFO_TYPE_VFUNC:
472 case GI_INFO_TYPE_FIELD:
473 case GI_INFO_TYPE_ARG:
474 case GI_INFO_TYPE_TYPE:
475 default: ;
476 /* no deprecation flag for these */
477 }
478
479 return FALSE;
480 }
481
482 /**
483 * g_base_info_get_attribute:
484 * @info: a #GIBaseInfo
485 * @name: a freeform string naming an attribute
486 *
487 * Retrieve an arbitrary attribute associated with this node.
488 *
489 * Returns: The value of the attribute, or %NULL if no such attribute exists
490 */
491 const gchar *
g_base_info_get_attribute(GIBaseInfo * info,const gchar * name)492 g_base_info_get_attribute (GIBaseInfo *info,
493 const gchar *name)
494 {
495 GIAttributeIter iter = { 0, };
496 gchar *curname, *curvalue;
497 while (g_base_info_iterate_attributes (info, &iter, &curname, &curvalue))
498 {
499 if (strcmp (name, curname) == 0)
500 return (const gchar*) curvalue;
501 }
502
503 return NULL;
504 }
505
506 static int
cmp_attribute(const void * av,const void * bv)507 cmp_attribute (const void *av,
508 const void *bv)
509 {
510 const AttributeBlob *a = av;
511 const AttributeBlob *b = bv;
512
513 if (a->offset < b->offset)
514 return -1;
515 else if (a->offset == b->offset)
516 return 0;
517 else
518 return 1;
519 }
520
521 /*
522 * _attribute_blob_find_first:
523 * @GIBaseInfo: A #GIBaseInfo.
524 * @blob_offset: The offset for the blob to find the first attribute for.
525 *
526 * Searches for the first #AttributeBlob for @blob_offset and returns
527 * it if found.
528 *
529 * Returns: A pointer to #AttributeBlob or %NULL if not found.
530 */
531 AttributeBlob *
_attribute_blob_find_first(GIBaseInfo * info,guint32 blob_offset)532 _attribute_blob_find_first (GIBaseInfo *info,
533 guint32 blob_offset)
534 {
535 GIRealInfo *rinfo = (GIRealInfo *) info;
536 Header *header = (Header *)rinfo->typelib->data;
537 AttributeBlob blob, *first, *res, *previous;
538
539 blob.offset = blob_offset;
540
541 first = (AttributeBlob *) &rinfo->typelib->data[header->attributes];
542
543 res = bsearch (&blob, first, header->n_attributes,
544 header->attribute_blob_size, cmp_attribute);
545
546 if (res == NULL)
547 return NULL;
548
549 previous = res - 1;
550 while (previous >= first && previous->offset == blob_offset)
551 {
552 res = previous;
553 previous = res - 1;
554 }
555
556 return res;
557 }
558
559 /**
560 * g_base_info_iterate_attributes:
561 * @info: a #GIBaseInfo
562 * @iterator: (inout): a #GIAttributeIter structure, must be initialized; see below
563 * @name: (out) (transfer none): Returned name, must not be freed
564 * @value: (out) (transfer none): Returned name, must not be freed
565 *
566 * Iterate over all attributes associated with this node. The iterator
567 * structure is typically stack allocated, and must have its first
568 * member initialized to %NULL. Attributes are arbitrary namespaced key–value
569 * pairs which can be attached to almost any item. They are intended for use
570 * by software higher in the toolchain than bindings, and are distinct from
571 * normal GIR annotations.
572 *
573 * Both the @name and @value should be treated as constants
574 * and must not be freed.
575 *
576 * <example>
577 * <title>Iterating over attributes</title>
578 * <programlisting>
579 * void
580 * print_attributes (GIBaseInfo *info)
581 * {
582 * GIAttributeIter iter = { 0, };
583 * char *name;
584 * char *value;
585 * while (g_base_info_iterate_attributes (info, &iter, &name, &value))
586 * {
587 * g_print ("attribute name: %s value: %s", name, value);
588 * }
589 * }
590 * </programlisting>
591 * </example>
592 *
593 * Returns: %TRUE if there are more attributes
594 */
595 gboolean
g_base_info_iterate_attributes(GIBaseInfo * info,GIAttributeIter * iterator,gchar ** name,gchar ** value)596 g_base_info_iterate_attributes (GIBaseInfo *info,
597 GIAttributeIter *iterator,
598 gchar **name,
599 gchar **value)
600 {
601 GIRealInfo *rinfo = (GIRealInfo *)info;
602 Header *header = (Header *)rinfo->typelib->data;
603 AttributeBlob *next, *after;
604
605 after = (AttributeBlob *) &rinfo->typelib->data[header->attributes +
606 header->n_attributes * header->attribute_blob_size];
607
608 if (iterator->data != NULL)
609 next = (AttributeBlob *) iterator->data;
610 else
611 next = _attribute_blob_find_first (info, rinfo->offset);
612
613 if (next == NULL || next->offset != rinfo->offset || next >= after)
614 return FALSE;
615
616 *name = (gchar*) g_typelib_get_string (rinfo->typelib, next->name);
617 *value = (gchar*) g_typelib_get_string (rinfo->typelib, next->value);
618 iterator->data = next + 1;
619
620 return TRUE;
621 }
622
623 /**
624 * g_base_info_get_container:
625 * @info: a #GIBaseInfo
626 *
627 * Obtain the container of the @info. The container is the parent
628 * GIBaseInfo. For instance, the parent of a #GIFunctionInfo is an
629 * #GIObjectInfo or #GIInterfaceInfo.
630 *
631 * Returns: (transfer none): the container
632 */
633 GIBaseInfo *
g_base_info_get_container(GIBaseInfo * info)634 g_base_info_get_container (GIBaseInfo *info)
635 {
636 return ((GIRealInfo*)info)->container;
637 }
638
639 /**
640 * g_base_info_get_typelib:
641 * @info: a #GIBaseInfo
642 *
643 * Obtain the typelib this @info belongs to
644 *
645 * Returns: (transfer none): the typelib.
646 */
647 GITypelib *
g_base_info_get_typelib(GIBaseInfo * info)648 g_base_info_get_typelib (GIBaseInfo *info)
649 {
650 return ((GIRealInfo*)info)->typelib;
651 }
652
653 /**
654 * g_base_info_equal:
655 * @info1: a #GIBaseInfo
656 * @info2: a #GIBaseInfo
657 *
658 * Compare two #GIBaseInfo.
659 *
660 * Using pointer comparison is not practical since many functions return
661 * different instances of #GIBaseInfo that refers to the same part of the
662 * TypeLib; use this function instead to do #GIBaseInfo comparisons.
663 *
664 * Returns: %TRUE if and only if @info1 equals @info2.
665 */
666 gboolean
g_base_info_equal(GIBaseInfo * info1,GIBaseInfo * info2)667 g_base_info_equal (GIBaseInfo *info1, GIBaseInfo *info2)
668 {
669 /* Compare the TypeLib pointers, which are mmapped. */
670 GIRealInfo *rinfo1 = (GIRealInfo*)info1;
671 GIRealInfo *rinfo2 = (GIRealInfo*)info2;
672 return rinfo1->typelib->data + rinfo1->offset == rinfo2->typelib->data + rinfo2->offset;
673 }
674
675
676