Bug 554632: Create type tag for GType
[gnome.gobject-introspection] / giscanner / glibtransformer.py
1 # -*- Mode: Python -*-
2 # GObject-Introspection - a framework for introspecting GObject libraries
3 # Copyright (C) 2008  Johan Dahlin
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program 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
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 # 02110-1301, USA.
19 #
20
21 import os
22 import re
23 import ctypes
24 from ctypes.util import find_library
25
26 from . import cgobject
27 from .ast import (Callback, Enum, Function, Member, Namespace, Parameter,
28                   Property, Return, Struct, Type, Alias, Array,
29                   Union, type_name_from_ctype,
30                   default_array_types, TYPE_UINT8)
31 from .transformer import Names
32 from .glibast import (GLibBoxed, GLibEnum, GLibEnumMember, GLibFlags,
33                       GLibInterface, GLibObject, GLibSignal, GLibBoxedStruct,
34                       GLibBoxedUnion, GLibBoxedOther, type_names)
35 from .utils import extract_libtool, to_underscores, to_underscores_noprefix
36
37 default_array_types['guchar*'] = TYPE_UINT8
38
39 SYMBOL_BLACKLIST = [
40     # These ones break GError conventions
41     'g_simple_async_result_new_from_error',
42     'g_simple_async_result_set_from_error',
43     'g_simple_async_result_propagate_error',
44     'g_simple_async_result_report_error_in_idle',
45     'gtk_print_operation_get_error',
46 ]
47
48 SYMBOL_BLACKLIST_RE = [re.compile(x) for x in \
49                            [r'\w+_marshal_[A-Z]+__', ]]
50
51
52 class Unresolved(object):
53
54     def __init__(self, target):
55         self.target = target
56
57
58 class UnknownTypeError(Exception):
59     pass
60
61
62 class GLibTransformer(object):
63
64     def __init__(self, transformer, noclosure=False):
65         self._transformer = transformer
66         self._transformer.set_container_types(['GList*', 'GSList*'],
67                                               ['GHashtable*'])
68         self._namespace_name = None
69         self._names = Names()
70         self._uscore_type_names = {}
71         self._libraries = []
72         self._failed_types = {}
73         self._boxed_types = {}
74         self._private_internal_types = {}
75         self._noclosure = noclosure
76         self._validating = False
77
78     # Public API
79
80     def add_library(self, libname):
81         # For testing mainly.
82         libtool_libname = 'lib' + libname + '.la'
83         if os.path.exists(libtool_libname):
84             found_libname = extract_libtool(libtool_libname)
85         elif libname.endswith('.la'):
86             found_libname = extract_libtool(libname)
87         else:
88             found_libname = find_library(libname)
89         if not found_libname:
90             raise ValueError("Failed to find library: %r" % (libname, ))
91         self._libraries.append(ctypes.cdll.LoadLibrary(found_libname))
92
93     def _print_statistics(self):
94         nodes = list(self._names.names.itervalues())
95
96         def count_type(otype):
97             return len([x for x in nodes
98                         if isinstance(x[1], otype)])
99         objectcount = count_type(GLibObject)
100         ifacecount = count_type(GLibInterface)
101         enumcount = count_type(GLibEnum)
102         print " %d nodes; %d objects, %d interfaces, %d enumsr" \
103             % (len(nodes), objectcount, ifacecount, enumcount)
104
105     def parse(self):
106         namespace = self._transformer.parse()
107         self._namespace_name = namespace.name
108
109         # First pass: parsing
110         for node in namespace.nodes:
111             self._parse_node(node)
112
113         # We don't want an alias for this - it's handled specially in
114         # the typelib compiler.
115         if namespace.name == 'GObject':
116             del self._names.aliases['Type']
117
118         # Introspection is done from within parsing
119
120         # Second pass: pair boxed structures
121         for boxed in self._boxed_types.itervalues():
122             self._pair_boxed_type(boxed)
123         # Third pass: delete class structures, resolve
124         # all types we now know about
125         nodes = list(self._names.names.itervalues())
126         for (ns, node) in nodes:
127             try:
128                 self._resolve_node(node)
129             except KeyError, e:
130                 print "WARNING: DELETING node %s: %s" % (node.name, e)
131                 self._remove_attribute(node.name)
132             # associate GtkButtonClass with GtkButton
133             if isinstance(node, Struct):
134                 self._pair_class_struct(node)
135         for (ns, alias) in self._names.aliases.itervalues():
136             self._resolve_alias(alias)
137
138         self._print_statistics()
139         # Fourth pass: ensure all types are known
140         if not self._noclosure:
141             self._validate(nodes)
142
143         # Create a new namespace with what we found
144         namespace = Namespace(namespace.name)
145         namespace.nodes = map(lambda x: x[1], self._names.aliases.itervalues())
146         for (ns, x) in self._names.names.itervalues():
147             namespace.nodes.append(x)
148         return namespace
149
150     # Private
151
152     def _add_attribute(self, node, replace=False):
153         node_name = node.name
154         if (not replace) and node_name in self._names.names:
155             return
156         self._names.names[node_name] = (None, node)
157
158     def _remove_attribute(self, name):
159         del self._names.names[name]
160
161     def _get_attribute(self, name):
162         node = self._names.names.get(name)
163         if node:
164             return node[1]
165         return None
166
167     def _register_internal_type(self, type_name, node):
168         self._names.type_names[type_name] = (None, node)
169         uscored = to_underscores(type_name).lower()
170         self._uscore_type_names[uscored] = node
171         # Besides the straight underscore conversion, we also try
172         # removing the underscores from the namespace as a possible C
173         # mapping; e.g. it's webkit_web_view, not web_kit_web_view
174         suffix = self._transformer.strip_namespace_object(type_name)
175         prefix = type_name[:-len(suffix)]
176         no_uscore_prefixed = (prefix + '_' + to_underscores(suffix)).lower()
177         self._uscore_type_names[no_uscore_prefixed] = node
178
179     # Helper functions
180
181     def _type_from_gtype(self, type_id):
182         ctype = cgobject.type_name(type_id)
183         type_name = type_name_from_ctype(ctype)
184         type_name = type_name.replace('*', '')
185         type_name = self._resolve_type_name(type_name)
186         return Type(type_name, ctype)
187
188     def _resolve_gtypename(self, gtype_name):
189         try:
190             return self._transformer.gtypename_to_giname(gtype_name,
191                                                          self._names)
192         except KeyError, e:
193             return Unresolved(gtype_name)
194
195     def _create_gobject(self, node):
196         type_name = 'G' + node.name
197         if type_name == 'GObject':
198             parent_gitype = None
199             symbol = 'intern'
200         else:
201             type_id = cgobject.type_from_name(type_name)
202             parent_type_name = cgobject.type_name(
203                 cgobject.type_parent(type_id))
204             parent_gitype = self._resolve_gtypename(parent_type_name)
205             symbol = to_underscores(type_name).lower() + '_get_type'
206         node = GLibObject(node.name, parent_gitype, type_name, symbol)
207         type_id = cgobject.TYPE_OBJECT
208         self._introspect_properties(node, type_id)
209         self._introspect_signals(node, type_id)
210         self._add_attribute(node)
211         self._register_internal_type(type_name, node)
212
213     # Parser
214
215     def _parse_node(self, node):
216         if isinstance(node, Enum):
217             self._parse_enum(node)
218         elif isinstance(node, Function):
219             self._parse_function(node)
220         elif isinstance(node, Struct):
221             self._parse_struct(node)
222         elif isinstance(node, Callback):
223             self._parse_callback(node)
224         elif isinstance(node, Alias):
225             self._parse_alias(node)
226         elif isinstance(node, Member):
227             # FIXME: atk_misc_instance singletons
228             pass
229         elif isinstance(node, Union):
230             self._parse_union(node)
231         else:
232             print 'GLIB Transformer: Unhandled node:', node
233
234     def _parse_alias(self, alias):
235         self._names.aliases[alias.name] = (None, alias)
236
237     def _parse_enum(self, enum):
238         self._add_attribute(enum)
239
240     def _parse_function(self, func):
241         if func.symbol in SYMBOL_BLACKLIST:
242             return
243         if func.symbol.startswith('_'):
244             return
245         for regexp in SYMBOL_BLACKLIST_RE:
246             if regexp.match(func.symbol):
247                 return
248         if self._parse_get_type_function(func):
249             return
250
251         self._add_attribute(func)
252
253     def _parse_get_type_function(self, func):
254         symbol = func.symbol
255         if not symbol.endswith('_get_type'):
256             return False
257         if self._namespace_name == 'GLib':
258             # No GObjects in GLib
259             return False
260         # GType *_get_type(void)
261         if func.retval.type.name not in ['Type',
262                                          'GType',
263                                          'GObject.Type',
264                                          'Gtk.Type']:
265             print ("Warning: *_get_type function returns '%r'"
266                    ", not GObject.Type") % (func.retval.type.name, )
267             return False
268         if func.parameters:
269             return False
270
271         if not self._libraries:
272             print "Warning: No libraries loaded, cannot call %s" % (symbol, )
273             return False
274
275         for library in self._libraries:
276             try:
277                 func = getattr(library, symbol)
278                 break
279             except AttributeError:
280                 continue
281         else:
282             print 'Warning: could not find symbol: %s' % symbol
283             name = symbol.replace('_get_type', '')
284             self._failed_types[name] = True
285             return False
286
287         func.restype = cgobject.GType
288         func.argtypes = []
289         type_id = func()
290         self._introspect_type(type_id, symbol)
291         return True
292
293     def _name_is_internal_gtype(self, giname):
294         try:
295             node = self._get_attribute(giname)
296             return isinstance(node, (GLibObject, GLibInterface,
297                                      GLibEnum, GLibFlags))
298         except KeyError, e:
299             return False
300
301     def _parse_method(self, func):
302         if not func.parameters:
303             return False
304         return self._parse_method_common(func, True)
305
306     def _parse_constructor(self, func):
307         return self._parse_method_common(func, False)
308
309     def _parse_method_common(self, func, is_method):
310         # Skip _get_type functions, we processed them
311         # already
312         if func.symbol.endswith('_get_type'):
313             return None
314         if self._namespace_name == 'GLib':
315             # No GObjects in GLib
316             return None
317
318         if not is_method:
319             target_arg = func.retval
320         else:
321             target_arg = func.parameters[0]
322         target_arg.type = self._resolve_param_type(target_arg.type)
323
324         if is_method:
325             # Methods require their first arg to be a known class
326             # Look at the original C type (before namespace stripping), without
327             # pointers: GtkButton -> gtk_button_, so we can figure out the
328             # method name
329             argtype = target_arg.type.ctype.replace('*', '')
330             name = self._transformer.strip_namespace_object(argtype)
331             name_uscore = to_underscores_noprefix(name).lower()
332             name_offset = func.symbol.find(name_uscore)
333             if name_offset < 0:
334                 return None
335             prefix = func.symbol[:name_offset+len(name_uscore)]
336         else:
337             # Constructors must have _new
338             # Take everything before that as class name
339             new_idx = func.symbol.find('_new')
340             if new_idx < 0:
341                 return None
342             # Constructors don't return basic types
343             derefed = self._transformer.follow_aliases(target_arg.type.name,
344                                                        self._names)
345             if derefed in type_names:
346                 #print "NOTE: Rejecting constructor returning basic: %r" \
347                 #    % (func.symbol, )
348                 return None
349             prefix = func.symbol[:new_idx]
350
351         klass = None
352
353         def valid_matching_klass(tclass):
354             if tclass is None:
355                 return False
356             elif isinstance(klass, (GLibEnum, GLibFlags)):
357                 return False
358             elif not isinstance(tclass, (GLibObject, GLibBoxed,
359                                           GLibInterface)):
360                 return False
361             else:
362                 return True
363
364         klass = self._uscore_type_names.get(prefix)
365         if klass is None:
366             #print "NOTE: No valid matching class for likely "+\
367             #    "method or constructor: %r" % (func.symbol, )
368             return None
369         # Enums can't have ctors or methods
370         if isinstance(klass, (GLibEnum, GLibFlags)):
371             return None
372
373         # The _uscore_type_names member holds the plain GLibBoxed
374         # object; we want to actually use the struct/record associated
375         if isinstance(klass, GLibBoxed):
376             name = self._transformer.strip_namespace_object(klass.type_name)
377             klass = self._get_attribute(name)
378
379         if not is_method:
380             # Interfaces can't have constructors, punt to global scope
381             if isinstance(klass, (GLibInterface, GLibBoxed)):
382                 #print "NOTE: Rejecting constructor for"+\
383                 #    " interface type: %r" % (func.symbol, )
384                 return None
385             # TODO - check that the return type is a subclass of the
386             # class from the prefix
387             # But for now, ensure that constructor returns are always
388             # the most concrete class
389             func.retval.type = Type(klass.name, klass.ctype+'*')
390
391         self._remove_attribute(func.name)
392         # Strip namespace and object prefix: gtk_window_new -> new
393         func.name = func.symbol[len(prefix)+1:]
394         if is_method:
395             klass.methods.append(func)
396         else:
397             klass.constructors.append(func)
398         return func
399
400     def _parse_struct(self, struct):
401         # This is a hack, but GObject is a rather fundamental piece so.
402         internal_names = ["Object", 'InitiallyUnowned']
403         g_internal_names = ["G" + x for x in internal_names]
404         if (self._namespace_name == 'GObject' and
405             struct.name in internal_names):
406             self._create_gobject(struct)
407             return
408         elif struct.name in g_internal_names:
409             # Avoid duplicates
410             return
411         node = self._names.names.get(struct.name)
412         if node is None:
413             self._add_attribute(struct, replace=True)
414             return
415         (ns, node) = node
416         node.fields = struct.fields[:]
417
418     def _parse_union(self, union):
419         node = self._names.names.get(union.name)
420         if node is None:
421             self._add_attribute(union, replace=True)
422             return
423         (ns, node) = node
424         node.fields = union.fields[:]
425
426     def _parse_callback(self, callback):
427         self._add_attribute(callback)
428
429     def _strip_class_suffix(self, name):
430         if (name.endswith('Class') or
431             name.endswith('Iface')):
432             return name[:-5]
433         elif name.endswith('Interface'):
434             return name[:-9]
435         else:
436             return name
437
438     def _arg_is_failed(self, param):
439         ctype = self._transformer.ctype_of(param).replace('*', '')
440         uscored = to_underscores(self._strip_class_suffix(ctype)).lower()
441         if uscored in self._failed_types:
442             print "Warning: failed type: %r" % (param, )
443             return True
444         return False
445
446     def _pair_class_struct(self, maybe_class):
447         name = self._strip_class_suffix(maybe_class.name)
448         if name == maybe_class.name:
449             return
450
451         if self._arg_is_failed(maybe_class):
452             print "WARNING: deleting no-type %r" % (maybe_class.name, )
453             del self._names.names[maybe_class.name]
454             return
455
456         name = self._resolve_type_name(name)
457         resolved = self._transformer.strip_namespace_object(name)
458         pair_class = self._get_attribute(resolved)
459         if pair_class and isinstance(pair_class,
460                                      (GLibObject, GLibInterface)):
461             for field in maybe_class.fields[1:]:
462                 pair_class.fields.append(field)
463             return
464         name = self._transformer.strip_namespace_object(maybe_class.name)
465         pair_class = self._get_attribute(name)
466         if pair_class and isinstance(pair_class,
467                                      (GLibObject, GLibInterface)):
468
469             del self._names.names[maybe_class.name]
470
471     # Introspection
472
473     def _introspect_type(self, type_id, symbol):
474         fundamental_type_id = cgobject.type_fundamental(type_id)
475         if (fundamental_type_id == cgobject.TYPE_ENUM or
476             fundamental_type_id == cgobject.TYPE_FLAGS):
477             self._introspect_enum(fundamental_type_id, type_id, symbol)
478         elif fundamental_type_id == cgobject.TYPE_OBJECT:
479             self._introspect_object(type_id, symbol)
480         elif fundamental_type_id == cgobject.TYPE_INTERFACE:
481             self._introspect_interface(type_id, symbol)
482         elif fundamental_type_id == cgobject.TYPE_BOXED:
483             self._introspect_boxed(type_id, symbol)
484         elif fundamental_type_id == cgobject.TYPE_BOXED:
485             self._introspect_boxed(type_id, symbol)
486         elif fundamental_type_id == cgobject.TYPE_POINTER:
487             # FIXME: Should we do something about these?
488             #        GHashTable, GValue and a few other fundamentals are
489             #        covered here
490             return
491         else:
492             print 'unhandled GType: %s(%d)' % (cgobject.type_name(type_id),
493                                                type_id)
494
495     def _introspect_enum(self, ftype_id, type_id, symbol):
496         type_class = cgobject.type_class_ref(type_id)
497         if type_class is None:
498             return
499
500         members = []
501         for enum_value in type_class.get_values():
502             members.append(GLibEnumMember(enum_value.value_nick,
503                                           enum_value.value,
504                                           enum_value.value_name,
505                                           enum_value.value_nick))
506
507         klass = (GLibFlags if ftype_id == cgobject.TYPE_FLAGS else GLibEnum)
508         type_name = cgobject.type_name(type_id)
509         enum_name = self._transformer.strip_namespace_object(type_name)
510         node = klass(enum_name, type_name, members, symbol)
511         self._add_attribute(node, replace=True)
512         self._register_internal_type(type_name, node)
513
514     def _introspect_object(self, type_id, symbol):
515         type_name = cgobject.type_name(type_id)
516         # We handle this specially above; in 2.16 and below there
517         # was no g_object_get_type, for later versions we need
518         # to skip it
519         if type_name == 'GObject':
520             return
521         parent_type_name = cgobject.type_name(cgobject.type_parent(type_id))
522         parent_gitype = self._resolve_gtypename(parent_type_name)
523         node = GLibObject(
524             self._transformer.strip_namespace_object(type_name),
525             parent_gitype,
526             type_name, symbol)
527         self._introspect_properties(node, type_id)
528         self._introspect_signals(node, type_id)
529         self._introspect_implemented_interfaces(node, type_id)
530         self._add_attribute(node, replace=True)
531         self._register_internal_type(type_name, node)
532
533     def _introspect_interface(self, type_id, symbol):
534         type_name = cgobject.type_name(type_id)
535         parent_type_name = cgobject.type_name(cgobject.type_parent(type_id))
536         if parent_type_name == 'GInterface':
537             parent_gitype = None
538         else:
539             parent_gitype = self._resolve_gtypename(parent_type_name)
540         node = GLibInterface(
541             self._transformer.strip_namespace_object(type_name),
542             parent_gitype,
543             type_name, symbol)
544         self._introspect_properties(node, type_id)
545         self._introspect_signals(node, type_id)
546         # GtkFileChooserEmbed is an example of a private interface, we
547         # just filter them out
548         if symbol.startswith('_'):
549             print "NOTICE: Marking %s as internal type" % (type_name, )
550             self._private_internal_types[type_name] = node
551         else:
552             self._add_attribute(node, replace=True)
553             self._register_internal_type(type_name, node)
554
555     def _introspect_boxed(self, type_id, symbol):
556         type_name = cgobject.type_name(type_id)
557         # This one doesn't go in the main namespace; we associate it with
558         # the struct or union
559         node = GLibBoxed(type_name, symbol)
560         self._boxed_types[node.type_name] = node
561         self._register_internal_type(type_name, node)
562
563     def _introspect_implemented_interfaces(self, node, type_id):
564         fundamental_type_id = cgobject.type_fundamental(type_id)
565         if fundamental_type_id != cgobject.TYPE_OBJECT:
566             raise AssertionError
567         interfaces = cgobject.type_interfaces(type_id)
568         gt_interfaces = []
569         for interface_typeid in interfaces:
570             iname = cgobject.type_name(interface_typeid)
571             gitype = self._resolve_gtypename(iname)
572             gt_interfaces.append(gitype)
573         node.interfaces = gt_interfaces
574
575     def _introspect_properties(self, node, type_id):
576         fundamental_type_id = cgobject.type_fundamental(type_id)
577         if fundamental_type_id == cgobject.TYPE_OBJECT:
578             pspecs = cgobject.object_class_list_properties(type_id)
579         elif fundamental_type_id == cgobject.TYPE_INTERFACE:
580             pspecs = cgobject.object_interface_list_properties(type_id)
581         else:
582             raise AssertionError
583
584         for pspec in pspecs:
585             if pspec.owner_type != type_id:
586                 continue
587             ctype = cgobject.type_name(pspec.value_type)
588             readable = (pspec.flags & 1) != 0
589             writable = (pspec.flags & 2) != 0
590             construct = (pspec.flags & 4) != 0
591             construct_only = (pspec.flags & 8) != 0
592             node.properties.append(Property(
593                 pspec.name,
594                 type_name_from_ctype(ctype),
595                 readable, writable, construct, construct_only,
596                 ctype,
597                 ))
598
599     def _introspect_signals(self, node, type_id):
600         for signal_info in cgobject.signal_list(type_id):
601             rtype = self._type_from_gtype(signal_info.return_type)
602             return_ = Return(rtype)
603             signal = GLibSignal(signal_info.signal_name, return_)
604             for i, parameter in enumerate(signal_info.get_params()):
605                 if i == 0:
606                     name = 'object'
607                 else:
608                     name = 'p%s' % (i-1, )
609                 ptype = self._type_from_gtype(parameter)
610                 param = Parameter(name, ptype)
611                 signal.parameters.append(param)
612             node.signals.append(signal)
613
614     # Resolver
615
616     def _resolve_type_name(self, type_name, ctype=None):
617         # Workaround glib bug #548689, to be included in 2.18.0
618         if type_name == "GParam":
619             type_name = "GObject.ParamSpec"
620
621         res = self._transformer.resolve_type_name_full
622         try:
623             return res(type_name, ctype, self._names)
624         except KeyError, e:
625             return self._transformer.resolve_type_name(type_name, ctype)
626
627     def _validate_type_name(self, name):
628         if name in type_names:
629             return True
630         if name.find('.') >= 0:
631             return True
632         if name in self._names.aliases:
633             return True
634         if name in self._names.names:
635             return True
636         return False
637
638     def _validate_type(self, ptype):
639         if isinstance(ptype, Array):
640             etype = ptype.element_type
641             if isinstance(etype, Array):
642                 return self._validate_type(etype)
643             return self._validate_type_name(etype)
644         return self._validate_type_name(ptype.name)
645
646     def _resolve_param_type_validate(self, ptype):
647         ptype = self._resolve_param_type(ptype)
648         if self._validating and not self._validate_type(ptype):
649             raise UnknownTypeError("Unknown type %r" % (ptype, ))
650         return ptype
651
652     def _resolve_param_type(self, ptype):
653         try:
654             return self._transformer.resolve_param_type_full(ptype,
655                                                              self._names)
656         except KeyError, e:
657             return self._transformer.resolve_param_type(ptype)
658         return ptype
659
660     def _resolve_node(self, node):
661         if isinstance(node, Function):
662             self._resolve_function_toplevel(node)
663
664         elif isinstance(node, Callback):
665             self._resolve_function(node)
666         elif isinstance(node, GLibObject):
667             self._resolve_glib_object(node)
668         elif isinstance(node, GLibInterface):
669             self._resolve_glib_interface(node)
670         elif isinstance(node, Struct):
671             self._resolve_struct(node)
672         elif isinstance(node, Union):
673             self._resolve_union(node)
674         elif isinstance(node, Alias):
675             self._resolve_alias(node)
676
677     def _resolve_function_toplevel(self, func):
678         newfunc = self._parse_constructor(func)
679         if not newfunc:
680             newfunc = self._parse_method(func)
681             if not newfunc:
682                 self._resolve_function(func)
683                 return
684         self._resolve_function(newfunc)
685
686     def _pair_boxed_type(self, boxed):
687         name = self._transformer.strip_namespace_object(boxed.type_name)
688         pair_node = self._get_attribute(name)
689         if not pair_node:
690             boxed_item = GLibBoxedOther(name, boxed.type_name,
691                                         boxed.get_type)
692         elif isinstance(pair_node, Struct):
693             boxed_item = GLibBoxedStruct(pair_node.name, boxed.type_name,
694                                          boxed.get_type)
695             boxed_item.fields = pair_node.fields
696         elif isinstance(pair_node, Union):
697             boxed_item = GLibBoxedUnion(pair_node.name, boxed.type_name,
698                                          boxed.get_type)
699             boxed_item.fields = pair_node.fields
700         else:
701             return False
702         self._add_attribute(boxed_item, replace=True)
703
704     def _resolve_struct(self, node):
705         for field in node.fields:
706             self._resolve_field(field)
707
708     def _resolve_union(self, node):
709         for field in node.fields:
710             self._resolve_field(field)
711
712     def _force_resolve(self, item, allow_unknown=False):
713         if isinstance(item, Unresolved):
714             if item.target in self._private_internal_types:
715                 return None
716             try:
717                 return self._transformer.gtypename_to_giname(item.target,
718                                                              self._names)
719             except KeyError, e:
720                 if allow_unknown:
721                     print "WARNING: Skipping unknown interface %s" % \
722                         (item.target, )
723                     return None
724                 else:
725                     raise
726         if item in self._private_internal_types:
727             return None
728         return item
729
730     def _resolve_glib_interface(self, node):
731         node.parent = self._force_resolve(node.parent)
732         self._resolve_methods(node.methods)
733         self._resolve_properties(node.properties)
734         self._resolve_signals(node.signals)
735
736     def _resolve_glib_object(self, node):
737         node.parent = self._force_resolve(node.parent)
738         node.interfaces = filter(None,
739             [self._force_resolve(x, allow_unknown=True)
740                                     for x in node.interfaces])
741         self._resolve_constructors(node.constructors)
742         self._resolve_methods(node.methods)
743         self._resolve_properties(node.properties)
744         self._resolve_signals(node.signals)
745
746     def _resolve_glib_boxed(self, node):
747         self._resolve_constructors(node.constructors)
748         self._resolve_methods(node.methods)
749
750     def _resolve_constructors(self, constructors):
751         for ctor in constructors:
752             self._resolve_function(ctor)
753
754     def _resolve_methods(self, methods):
755         for method in methods:
756             self._resolve_function(method)
757
758     def _resolve_signals(self, signals):
759         for signal in signals:
760             self._resolve_function(signal)
761
762     def _resolve_properties(self, properties):
763         for prop in properties:
764             self._resolve_property(prop)
765
766     def _resolve_property(self, prop):
767         prop.type = self._resolve_param_type(prop.type)
768
769     def _resolve_function(self, func):
770         self._resolve_parameters(func.parameters)
771         func.retval.type = self._resolve_param_type(func.retval.type)
772
773     def _resolve_parameters(self, parameters):
774         for parameter in parameters:
775             parameter.type = self._resolve_param_type(parameter.type)
776
777     def _resolve_field(self, field):
778         if isinstance(field, Callback):
779             self._resolve_function(field)
780             return
781         field.type = self._resolve_param_type(field.type)
782
783     def _resolve_alias(self, alias):
784         alias.target = self._resolve_type_name(alias.target, alias.target)
785
786     # Validation
787
788     def _validate(self, nodes):
789         nodes = list(self._names.names.itervalues())
790         i = 0
791         self._validating = True
792         while True:
793             initlen = len(nodes)
794
795             print "Type resolution; pass=%d" % (i, )
796             nodes = list(self._names.names.itervalues())
797             for node in nodes:
798                 try:
799                     self._resolve_node(node)
800                 except UnknownTypeError, e:
801                     print "WARNING: %s: Deleting %r" % (e, node)
802                     self._remove_attribute(node.name)
803             if len(nodes) == initlen:
804                 break
805             i += 1
806             self._print_statistics()
807         self._validating = False