2 # GObject-Introspection - a framework for introspecting GObject libraries
3 # Copyright (C) 2008 Johan Dahlin
4 # Copyright (C) 2008, 2009 Red Hat, Inc.
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2 of the License, or (at your option) any later version.
11 # This library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # Lesser General Public License for more details.
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with this library; if not, write to the
18 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 # Boston, MA 02111-1307, USA.
22 from .odict import odict
23 from .utils import to_underscores
26 """A Type can be either:
27 * A reference to a node (target_giname)
28 * A reference to a "fundamental" type like 'utf8'
29 * A "foreign" type - this can be any string."
30 If none are specified, then it's in an "unresolved" state.
31 In this case, the ctype must be specified.
36 target_fundamental=None,
39 _target_unknown=False,
43 self.origin_symbol = origin_symbol
45 assert isinstance(self, TypeUnknown)
46 elif target_fundamental:
47 assert target_giname is None
48 assert target_foreign is None
50 assert '.' in target_giname
51 assert target_fundamental is None
52 assert target_foreign is None
54 assert ctype is not None
55 assert target_giname is None
56 assert target_fundamental is None
58 assert ctype is not None
59 self.target_fundamental = target_fundamental
60 self.target_giname = target_giname
61 self.target_foreign = target_foreign
62 self.is_const = is_const
66 return (self.target_fundamental or
71 assert self.target_giname is not None
72 return self.target_giname.split('.')[1]
74 def __cmp__(self, other):
75 if self.target_fundamental:
76 return cmp(self.target_fundamental, other.target_fundamental)
77 if self.target_giname:
78 return cmp(self.target_giname, other.target_giname)
79 if self.target_foreign:
80 return cmp(self.target_foreign, other.target_foreign)
81 return cmp(self.ctype, other.ctype)
83 def is_equiv(self, typeval):
84 """Return True if the specified types are compatible at
85 an introspection level, disregarding their C types.
86 A sequence may be given for typeval, in which case
87 this function returns True if the type is compatible with
89 if isinstance(typeval, (list, tuple)):
91 if self.is_equiv(val):
94 return self == typeval
97 return Type(target_fundamental=self.target_fundamental,
98 target_giname=self.target_giname,
99 target_foreign=self.target_foreign,
101 is_const=self.is_const)
104 if self.target_fundamental:
105 return self.target_fundamental
106 elif self.target_giname:
107 return self.target_giname
108 elif self.target_foreign:
109 return self.target_foreign
112 if self.target_fundamental:
113 data = 'target_fundamental=%s, ' % (self.target_fundamental, )
114 elif self.target_giname:
115 data = 'target_giname=%s, ' % (self.target_giname, )
116 elif self.target_foreign:
117 data = 'target_foreign=%s, ' % (self.target_foreign, )
120 return '%s(%sctype=%s)' % (self.__class__.__name__, data, self.ctype)
122 class TypeUnknown(Type):
124 Type.__init__(self, _target_unknown=True)
130 TYPE_NONE = Type(target_fundamental='none', ctype='void')
131 TYPE_ANY = Type(target_fundamental='gpointer', ctype='gpointer')
133 TYPE_BOOLEAN = Type(target_fundamental='gboolean', ctype='gboolean')
134 TYPE_INT8 = Type(target_fundamental='gint8', ctype='gint8')
135 TYPE_UINT8 = Type(target_fundamental='guint8', ctype='guint8')
136 TYPE_INT16 = Type(target_fundamental='gint16', ctype='gint16')
137 TYPE_UINT16 = Type(target_fundamental='guint16', ctype='guint16')
138 TYPE_INT32 = Type(target_fundamental='gint32', ctype='gint32')
139 TYPE_UINT32 = Type(target_fundamental='guint32', ctype='guint32')
140 TYPE_INT64 = Type(target_fundamental='gint64', ctype='gint64')
141 TYPE_UINT64 = Type(target_fundamental='guint64', ctype='guint64')
142 TYPE_CHAR = Type(target_fundamental='gchar', ctype='gchar')
143 TYPE_SHORT = Type(target_fundamental='gshort', ctype='gshort')
144 TYPE_USHORT = Type(target_fundamental='gushort', ctype='gushort')
145 TYPE_INT = Type(target_fundamental='gint', ctype='gint')
146 TYPE_UINT = Type(target_fundamental='guint', ctype='guint')
147 TYPE_LONG = Type(target_fundamental='glong', ctype='glong')
148 TYPE_ULONG = Type(target_fundamental='gulong', ctype='gulong')
150 TYPE_LONG_LONG = Type(target_fundamental='long long', ctype='long long')
151 TYPE_LONG_ULONG = Type(target_fundamental='unsigned long long',
152 ctype='unsigned long long')
153 TYPE_FLOAT = Type(target_fundamental='gfloat', ctype='gfloat')
154 TYPE_DOUBLE = Type(target_fundamental='gdouble', ctype='gdouble')
156 TYPE_LONG_DOUBLE = Type(target_fundamental='long double',
158 TYPE_UNICHAR = Type(target_fundamental='gunichar', ctype='gunichar')
160 # C types with semantics overlaid
161 TYPE_GTYPE = Type(target_fundamental='GType', ctype='GType')
162 TYPE_STRING = Type(target_fundamental='utf8', ctype='gchar*')
163 TYPE_FILENAME = Type(target_fundamental='filename', ctype='gchar*')
165 TYPE_VALIST = Type(target_fundamental='va_list', ctype='va_list')
167 BASIC_GIR_TYPES = [TYPE_BOOLEAN, TYPE_INT8, TYPE_UINT8, TYPE_INT16,
168 TYPE_UINT16, TYPE_INT32, TYPE_UINT32, TYPE_INT64,
169 TYPE_UINT64, TYPE_CHAR, TYPE_SHORT, TYPE_USHORT, TYPE_INT,
170 TYPE_UINT, TYPE_LONG, TYPE_ULONG, TYPE_LONG_LONG,
171 TYPE_LONG_ULONG, TYPE_FLOAT, TYPE_DOUBLE,
172 TYPE_LONG_DOUBLE, TYPE_UNICHAR, TYPE_GTYPE]
173 GIR_TYPES = [TYPE_NONE, TYPE_ANY]
174 GIR_TYPES.extend(BASIC_GIR_TYPES)
175 GIR_TYPES.extend([TYPE_STRING, TYPE_FILENAME, TYPE_VALIST])
177 INTROSPECTABLE_BASIC = list(GIR_TYPES)
178 for v in [TYPE_NONE, TYPE_ANY,
179 TYPE_LONG_LONG, TYPE_LONG_ULONG,
180 TYPE_LONG_DOUBLE, TYPE_VALIST]:
181 INTROSPECTABLE_BASIC.remove(v)
184 for typeval in GIR_TYPES:
185 type_names[typeval.target_fundamental] = typeval
186 basic_type_names = {}
187 for typeval in BASIC_GIR_TYPES:
188 basic_type_names[typeval.target_fundamental] = typeval
191 type_names['char'] = TYPE_CHAR
192 type_names['signed char'] = TYPE_INT8
193 type_names['unsigned char'] = TYPE_UINT8
194 type_names['short'] = TYPE_SHORT
195 type_names['signed short'] = TYPE_SHORT
196 type_names['unsigned short'] = TYPE_USHORT
197 type_names['int'] = TYPE_INT
198 type_names['signed int'] = TYPE_INT
199 type_names['unsigned short int'] = TYPE_USHORT
200 type_names['signed'] = TYPE_INT
201 type_names['unsigned int'] = TYPE_UINT
202 type_names['unsigned'] = TYPE_UINT
203 type_names['long'] = TYPE_LONG
204 type_names['signed long'] = TYPE_LONG
205 type_names['unsigned long'] = TYPE_ULONG
206 type_names['unsigned long int'] = TYPE_ULONG
207 type_names['float'] = TYPE_FLOAT
208 type_names['double'] = TYPE_DOUBLE
209 type_names['char*'] = TYPE_STRING
210 type_names['void*'] = TYPE_ANY
211 type_names['void'] = TYPE_NONE
212 # Also alias the signed one here
213 type_names['signed long long'] = TYPE_LONG_LONG
215 # A few additional GLib type aliases
216 type_names['guchar'] = TYPE_UINT8
217 type_names['gchararray'] = TYPE_STRING
218 type_names['gchar*'] = TYPE_STRING
219 type_names['goffset'] = TYPE_INT64
220 type_names['gunichar2'] = TYPE_UINT16
221 type_names['gsize'] = TYPE_ULONG
222 type_names['gssize'] = TYPE_LONG
223 type_names['gconstpointer'] = TYPE_ANY
225 # C stdio, used in GLib public headers; squash this for now here
226 # until we move scanning into GLib and can (skip)
227 type_names['FILE*'] = TYPE_ANY
229 # One off C unix type definitions; note some of these may be GNU Libc
230 # specific. If someone is actually bitten by this, feel free to do
231 # the required configure goop to determine their size and replace
234 # We don't want to encourage people to use these in their APIs because
235 # they compromise the platform-independence that GLib gives you.
236 # These are here mostly to avoid blowing when random platform-specific
237 # methods are added under #ifdefs inside GLib itself. We could just (skip)
238 # the relevant methods, but on the other hand, since these types are just
239 # integers it's easy enough to expand them.
240 type_names['size_t'] = type_names['gsize']
241 type_names['time_t'] = TYPE_LONG
242 type_names['off_t'] = type_names['gsize']
243 type_names['pid_t'] = TYPE_INT
244 type_names['uid_t'] = TYPE_UINT
245 type_names['gid_t'] = TYPE_UINT
246 type_names['dev_t'] = TYPE_INT
247 type_names['socklen_t'] = TYPE_INT32
248 type_names['size_t'] = TYPE_ULONG
249 type_names['ssize_t'] = TYPE_LONG
252 type_names['id'] = TYPE_ANY
258 PARAM_DIRECTION_IN = 'in'
259 PARAM_DIRECTION_OUT = 'out'
260 PARAM_DIRECTION_INOUT = 'inout'
262 PARAM_SCOPE_CALL = 'call'
263 PARAM_SCOPE_ASYNC = 'async'
264 PARAM_SCOPE_NOTIFIED = 'notified'
266 PARAM_TRANSFER_NONE = 'none'
267 PARAM_TRANSFER_CONTAINER = 'container'
268 PARAM_TRANSFER_FULL = 'full'
270 class Namespace(object):
271 def __init__(self, name, version,
272 identifier_prefixes=None,
273 symbol_prefixes=None):
275 self.version = version
276 if identifier_prefixes:
277 self.identifier_prefixes = identifier_prefixes
279 self.identifier_prefixes = [name]
281 self.symbol_prefixes = symbol_prefixes
283 ps = self.identifier_prefixes
284 self.symbol_prefixes = [to_underscores(p).lower() for p in ps]
285 self._names = odict() # Maps from GIName -> node
286 self._aliases = {} # Maps from GIName -> GIName
287 self._type_names = {} # Maps from GTName -> node
288 self._ctypes = {} # Maps from CType -> node
289 self._symbols = {} # Maps from function symbols -> Function
300 def type_names(self):
301 return self._type_names
307 def type_from_name(self, name, ctype=None):
308 """Backwards compatibility method for older .gir files, which
309 only use the 'name' attribute. If name refers to a fundamental type,
310 create a Type object referncing it. If name is already a
311 fully-qualified GIName like 'Foo.Bar', returns a Type targeting it .
312 Otherwise a Type targeting name qualififed with the namespace name is
314 if name in type_names:
315 return Type(target_fundamental=name, ctype=ctype)
319 target = '%s.%s' % (self.name, name)
320 return Type(target_giname=target, ctype=ctype)
322 def contains_ident(self, ident):
323 """Return True if this namespace should contain the given C
324 identifier string."""
325 return any(ident.startswith(prefix) for prefix in self.identifier_prefixes)
327 def append(self, node, replace=False):
328 previous = self._names.get(node.name)
329 if previous is not None:
331 raise ValueError("Namespace conflict")
332 self.remove(previous)
333 # A layering violation...but oh well.
334 from .glibast import GLibBoxed
335 if isinstance(node, Alias):
336 self._aliases[node.name] = node
337 elif isinstance(node, (GLibBoxed, Interface, Class)):
338 self._type_names[node.type_name] = node
339 elif isinstance(node, Function):
340 self._symbols[node.symbol] = node
341 assert isinstance(node, Node)
342 assert node.namespace is None
343 node.namespace = self
344 self._names[node.name] = node
345 if hasattr(node, 'ctype'):
346 self._ctypes[node.ctype] = node
347 if hasattr(node, 'symbol'):
348 self._ctypes[node.symbol] = node
350 def remove(self, node):
351 from .glibast import GLibBoxed
352 if isinstance(node, Alias):
353 del self._aliases[node.name]
354 elif isinstance(node, (GLibBoxed, Interface, Class)):
355 del self._type_names[node.type_name]
356 del self._names[node.name]
357 node.namespace = None
358 if hasattr(node, 'ctype'):
359 del self._ctypes[node.ctype]
360 if isinstance(node, Function):
361 del self._symbols[node.symbol]
363 def float(self, node):
364 """Like remove(), but doesn't unset the node's namespace
365 back-reference, and it's still possible to look up
366 functions via get_by_symbol()."""
367 if isinstance(node, Function):
370 self._symbols[symbol] = node
371 node.namespace = self
374 return iter(self._names)
377 return self._names.iteritems()
379 def itervalues(self):
380 return self._names.itervalues()
383 return self._names.get(name)
385 def get_by_ctype(self, ctype):
386 return self._ctypes.get(ctype)
388 def get_by_symbol(self, symbol):
389 return self._symbols.get(symbol)
391 def walk(self, callback):
392 for node in self.itervalues():
393 node.walk(callback, [])
395 class Include(object):
397 def __init__(self, name, version):
399 self.version = version
402 def from_string(cls, string):
403 return cls(*string.split('-', 1))
405 def __cmp__(self, other):
406 namecmp = cmp(self.name, other.name)
409 return cmp(self.version, other.version)
412 return hash(str(self))
415 return '%s-%s' % (self.name, self.version)
417 class Annotated(object):
418 """An object which has a few generic metadata
423 self.introspectable = True
424 self.attributes = [] # (key, value)*
425 self.deprecated = None
426 self.deprecated_version = None
429 class Node(Annotated):
430 """A node is a type of object which is uniquely identified by its
431 (namespace, name) pair. When combined with a ., this is called a
432 GIName. It's possible for nodes to contain or point to other nodes."""
434 c_name = property(lambda self: self.namespace.name + self.name)
435 gi_name = property(lambda self: '%s.%s' % (self.namespace.name, self.name))
437 def __init__(self, name=None):
438 Annotated.__init__(self)
439 self.namespace = None # Should be set later by Namespace.append()
442 self.file_positions = set()
444 def create_type(self):
445 """Create a Type object referencing this node."""
446 assert self.namespace is not None
447 return Type(target_giname=('%s.%s' % (self.namespace.name, self.name)))
449 def __cmp__(self, other):
450 nscmp = cmp(self.namespace, other.namespace)
453 return cmp(self.name, other.name)
456 return '%s(%r)' % (self.__class__.__name__, self.name)
458 def remove_matching_children(self, pred):
461 def inherit_file_positions(self, node):
462 self.file_positions.update(node.file_positions)
464 def add_file_position(self, filename, line, column):
465 self.file_positions.add((filename, line, column))
467 def add_symbol_reference(self, symbol):
468 if symbol.source_filename:
469 self.add_file_position(symbol.source_filename, symbol.line, -1)
471 def walk(self, callback, chain):
472 res = callback(self, chain)
473 assert res in (True, False), "Walk function must return boolean, not %r" % (res, )
477 self._walk(callback, chain)
480 def _walk(self, callback, chain):
483 class Callable(Node):
485 def __init__(self, name, retval, parameters, throws):
486 Node.__init__(self, name)
488 self.parameters = parameters
489 self.throws = not not throws
491 def get_parameter_index(self, name):
492 for i, parameter in enumerate(self.parameters):
493 if parameter.argname == name:
495 raise ValueError("Unknown argument %s" % (name, ))
497 def get_parameter(self, name):
498 for parameter in self.parameters:
499 if parameter.argname == name:
501 raise ValueError("Unknown argument %s" % (name, ))
504 class Function(Callable):
506 def __init__(self, name, retval, parameters, throws, symbol):
507 Callable.__init__(self, name, retval, parameters, throws)
509 self.is_method = False
510 self.is_constructor = False
511 self.shadowed_by = None # C symbol string
512 self.shadows = None # C symbol string
515 class VFunction(Callable):
517 def __init__(self, name, retval, parameters, throws):
518 Callable.__init__(self, name, retval, parameters, throws)
522 def from_callback(cls, cb):
523 obj = cls(cb.name, cb.retval, cb.parameters[1:],
532 Type.__init__(self, '<varargs>', target_fundamental='<varargs>')
537 GLIB_ARRAY = 'GLib.Array'
538 GLIB_BYTEARRAY = 'GLib.ByteArray'
539 GLIB_PTRARRAY = 'GLib.PtrArray'
541 def __init__(self, array_type, element_type, **kwargs):
542 Type.__init__(self, target_fundamental='<array>',
544 if (array_type is None or array_type == self.C):
545 self.array_type = self.C
547 assert array_type in (self.GLIB_ARRAY,
550 self.array_type = array_type
551 assert isinstance(element_type, Type)
552 self.element_type = element_type
553 self.zeroterminated = True
554 self.length_param_name = None
558 arr = Array(self.array_type, self.element_type)
559 arr.element_type = self.element_type
560 arr.zeroterminated = self.zeroterminated
561 arr.length_param_name = self.length_param_name
567 def __init__(self, name, element_type, **kwargs):
568 Type.__init__(self, target_fundamental='<list>',
571 assert isinstance(element_type, Type)
572 self.element_type = element_type
575 l = List(self.name, self.element_type)
576 l.element_type = self.element_type
577 l.zeroterminated = self.zeroterminated
578 l.length_param_name = self.length_param_name
584 def __init__(self, key_type, value_type, **kwargs):
585 Type.__init__(self, target_fundamental='<map>', **kwargs)
586 assert isinstance(key_type, Type)
587 self.key_type = key_type
588 assert isinstance(value_type, Type)
589 self.value_type = value_type
592 m = Map(self.key_type, self.value_type)
597 def __init__(self, name, target, ctype=None):
598 Node.__init__(self, name)
603 class TypeContainer(Annotated):
604 """A fundamental base class for Return and Parameter."""
606 def __init__(self, typenode, transfer):
607 Annotated.__init__(self)
609 if transfer is not None:
610 self.transfer = transfer
611 elif typenode.is_const:
612 self.transfer = PARAM_TRANSFER_NONE
617 class Parameter(TypeContainer):
618 """An argument to a function."""
620 def __init__(self, argname, typenode, direction=None,
621 transfer=None, allow_none=False, scope=None,
622 caller_allocates=False):
623 TypeContainer.__init__(self, typenode, transfer)
624 self.argname = argname
625 self.direction = direction
626 self.allow_none = allow_none
628 self.caller_allocates = caller_allocates
629 self.closure_name = None
630 self.destroy_name = None
633 class Return(TypeContainer):
634 """A return value from a function."""
636 def __init__(self, rtype, transfer=None):
637 TypeContainer.__init__(self, rtype, transfer)
638 self.direction = PARAM_DIRECTION_OUT
643 def __init__(self, name, symbol, members):
644 Node.__init__(self, name)
646 self.members = members
649 class Bitfield(Node):
651 def __init__(self, name, symbol, members):
652 Node.__init__(self, name)
654 self.members = members
657 class Member(Annotated):
659 def __init__(self, name, value, symbol):
660 Annotated.__init__(self)
665 def __cmp__(self, other):
666 return cmp(self.name, other.name)
671 def __init__(self, name, symbol, disguised=False):
672 Node.__init__(self, name)
674 self.constructors = []
676 self.disguised = disguised
678 self.static_methods = []
680 def _walk(self, callback, chain):
681 for ctor in self.constructors:
682 ctor.walk(callback, chain)
683 for func in self.methods:
684 func.walk(callback, chain)
685 for func in self.static_methods:
686 func.walk(callback, chain)
687 for field in self.fields:
688 if field.anonymous_node is not None:
689 field.anonymous_node.walk(callback, chain)
691 def remove_matching_children(self, pred):
692 self.fields = filter(pred, self.fields)
693 self.constructors = filter(pred, self.constructors)
694 self.methods = filter(pred, self.methods)
697 class Field(Annotated):
699 def __init__(self, name, typenode, readable, writable, bits=None,
700 anonymous_node=None):
701 Annotated.__init__(self)
702 assert (typenode or anonymous_node)
705 self.readable = readable
706 self.writable = writable
708 self.anonymous_node = anonymous_node
710 def __cmp__(self, other):
711 return cmp(self.name, other.name)
716 def __init__(self, name, parent, is_abstract):
717 Node.__init__(self, name)
719 self.c_symbol_prefix = None
721 # When we're in the scanner, we keep around a list
722 # of parents so that we can transparently fall back
723 # if there are 'hidden' parents
724 self.parent_chain = []
725 self.glib_type_struct = None
726 self.is_abstract = is_abstract
728 self.virtual_methods = []
729 self.static_methods = []
731 self.constructors = []
735 def remove_matching_children(self, pred):
736 self.methods = filter(pred, self.methods)
737 self.constructors = filter(pred, self.constructors)
738 self.properties = filter(pred, self.properties)
739 self.fields = filter(pred, self.fields)
741 def _walk(self, callback, chain):
742 for meth in self.methods:
743 meth.walk(callback, chain)
744 for meth in self.virtual_methods:
745 meth.walk(callback, chain)
746 for meth in self.static_methods:
747 meth.walk(callback, chain)
748 for ctor in self.constructors:
749 ctor.walk(callback, chain)
750 for field in self.fields:
751 if field.anonymous_node:
752 field.anonymous_node.walk(callback, chain)
754 class Interface(Node):
756 def __init__(self, name, parent):
757 Node.__init__(self, name)
758 self.c_symbol_prefix = None
760 self.parent_chain = []
762 self.static_methods = []
763 self.virtual_methods = []
764 self.glib_type_struct = None
767 self.prerequisites = []
769 def _walk(self, callback, chain):
770 for meth in self.methods:
771 meth.walk(callback, chain)
772 for meth in self.static_methods:
773 meth.walk(callback, chain)
774 for meth in self.virtual_methods:
775 meth.walk(callback, chain)
776 for field in self.fields:
777 if field.anonymous_node:
778 field.anonymous_node.walk(callback, chain)
780 class Constant(Node):
782 def __init__(self, name, value_type, value):
783 Node.__init__(self, name)
784 self.value_type = value_type
788 class Property(Node):
790 def __init__(self, name, typeobj, readable, writable,
791 construct, construct_only, transfer=None):
792 Node.__init__(self, name)
794 self.readable = readable
795 self.writable = writable
796 self.construct = construct
797 self.construct_only = construct_only
798 self.transfer = PARAM_TRANSFER_NONE
801 class Callback(Callable):
803 def __init__(self, name, retval, parameters, throws, ctype=None):
804 Callable.__init__(self, name, retval, parameters, throws)
810 def __init__(self, name, symbol):
811 Node.__init__(self, name)
813 self.constructors = []
815 self.static_methods = []
818 def _walk(self, callback, chain):
819 for ctor in self.constructors:
820 ctor.walk(callback, chain)
821 for meth in self.methods:
822 meth.walk(callback, chain)
823 for meth in self.static_methods:
824 meth.walk(callback, chain)
825 for field in self.fields:
826 if field.anonymous_node:
827 field.anonymous_node.walk(callback, chain)