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.
23 This file descbribes abstract data type nodes independent on the
24 implementation language.
26 These can later on be extended (eg subclassed) with additional information
27 which is language/library/domain specific.
31 ## Basic types, modeled on GITypeTag but not equivalent
34 TYPE_NONE = 'none' # We differ from repository on these first two
36 TYPE_BOOLEAN = 'boolean'
40 TYPE_USHORT = 'ushort'
42 TYPE_UINT16 = 'uint16'
46 TYPE_UINT32 = 'uint32'
48 TYPE_UINT64 = 'uint64'
51 TYPE_SSIZET = 'ssize_t'
56 TYPE_DOUBLE = 'double'
57 TYPE_STRING = 'utf8' # requires zero-terminated
58 TYPE_FILENAME = 'filename'
60 BASIC_GIR_TYPES = [TYPE_BOOLEAN, TYPE_INT8, TYPE_UINT8, TYPE_INT16,
61 TYPE_UINT16, TYPE_INT32, TYPE_UINT32, TYPE_INT64,
62 TYPE_UINT64, TYPE_SHORT, TYPE_USHORT, TYPE_INT,
63 TYPE_UINT, TYPE_LONG, TYPE_ULONG, TYPE_SSIZET,
64 TYPE_SIZET, TYPE_FLOAT, TYPE_DOUBLE, TYPE_TIMET,
66 GIR_TYPES = [TYPE_NONE, TYPE_ANY]
67 GIR_TYPES.extend(BASIC_GIR_TYPES)
68 GIR_TYPES.extend([TYPE_STRING, TYPE_FILENAME])
70 # Higher-level data types
71 TYPE_SEQUENCE = 'sequence' # Sequence of something
75 TYPE_USTRING = 'ustring'
81 PARAM_DIRECTION_IN = 'in'
82 PARAM_DIRECTION_OUT = 'out'
83 PARAM_DIRECTION_INOUT = 'inout'
85 PARAM_SCOPE_CALL = 'call'
86 PARAM_SCOPE_ASYNC = 'async'
87 PARAM_SCOPE_NOTIFIED = 'notified'
89 PARAM_TRANSFER_NONE = 'none'
90 PARAM_TRANSFER_CONTAINER = 'container'
91 PARAM_TRANSFER_FULL = 'full'
94 for name in GIR_TYPES:
95 type_names[name] = name
98 type_names['char'] = TYPE_INT8
99 type_names['signed char'] = TYPE_INT8
100 type_names['unsigned char'] = TYPE_UINT8
101 type_names['short'] = TYPE_SHORT
102 type_names['signed short'] = TYPE_SHORT
103 type_names['unsigned short'] = TYPE_USHORT
104 type_names['int'] = TYPE_INT
105 type_names['signed int'] = TYPE_INT
106 type_names['signed'] = TYPE_INT
107 type_names['unsigned int'] = TYPE_UINT
108 type_names['unsigned'] = TYPE_UINT
109 type_names['long'] = TYPE_LONG
110 type_names['signed long'] = TYPE_LONG
111 type_names['unsigned long'] = TYPE_ULONG
112 type_names['float'] = TYPE_FLOAT
113 type_names['double'] = TYPE_DOUBLE
114 type_names['char*'] = TYPE_STRING
115 type_names['void*'] = TYPE_ANY
116 type_names['void'] = TYPE_NONE
118 # Unix types that we special case here (and that have their own introspection
119 # type tag) because of wide use in GLib.
120 type_names['size_t'] = TYPE_SIZET
121 type_names['ssize_t'] = TYPE_SSIZET
123 # One off C unix type definitions; note some of these may be GNU Libc
124 # specific. If someone is actually bitten by this, feel free to do
125 # the required configure goop to determine their size and replace
128 # We don't want to encourage people to use these in their APIs because
129 # they compromise the platform-independence that GLib gives you.
130 # These are here mostly to avoid blowing when random platform-specific
131 # methods are added under #ifdefs inside GLib itself. We could just (skip)
132 # the relevant methods, but on the other hand, since these types are just
133 # integers it's easy enough to expand them.
134 type_names['off_t'] = TYPE_SIZET
135 type_names['pid_t'] = TYPE_INT
136 type_names['uid_t'] = TYPE_UINT
137 type_names['gid_t'] = TYPE_UINT
138 type_names['dev_t'] = TYPE_INT
139 type_names['socklen_t'] = TYPE_INT32
142 type_names['id'] = TYPE_ANY
144 # Suppress some GLib names
145 type_names['uchar'] = TYPE_UINT8
146 type_names['ushort'] = TYPE_USHORT
147 type_names['size'] = TYPE_SIZET
148 type_names['ssize'] = TYPE_SSIZET
149 type_names['pointer'] = TYPE_ANY
150 type_names['constpointer'] = TYPE_ANY
153 # These types, when seen by reference, are converted into an Array()
155 # If you add/change these, be sure to update glibast.py too
156 default_array_types = {}
157 default_array_types['uint8*'] = TYPE_UINT8
158 default_array_types['utf8*'] = TYPE_STRING
160 # These types, when seen by reference, are interpreted as out parameters
161 default_out_types = (TYPE_SHORT, TYPE_USHORT, TYPE_INT, TYPE_UINT,
162 TYPE_LONG, TYPE_ULONG, TYPE_FLOAT, TYPE_DOUBLE,
163 TYPE_SIZET, TYPE_SSIZET)
166 def type_name_from_ctype(ctype):
167 return type_names.get(ctype, ctype)
172 def __init__(self, name=None):
174 self.attributes = [] # (key, value)*
176 self.deprecated = None
177 self.deprecated_version = None
181 def __cmp__(self, other):
182 return cmp(self.name, other.name)
185 return '%s(%r)' % (self.__class__.__name__, self.name)
187 def remove_matching_children(self, pred):
190 class Namespace(Node):
192 def __init__(self, name, version):
193 Node.__init__(self, name)
194 self.version = version
198 return '%s(%r, %r, %r)' % (self.__class__.__name__, self.name,
199 self.version, self.nodes)
201 def remove_matching(self, pred):
203 def recursive_pred(node):
204 node.remove_matching_children(pred)
207 self.nodes = filter(recursive_pred, self.nodes)
211 def __init__(self, name, version):
212 Node.__init__(self, 'include')
214 self.version = version
217 def from_string(self, string):
218 return Include(*string.split('-', 1))
220 def __cmp__(self, other):
221 if not isinstance(other, Include):
222 return cmp(self, other)
223 namecmp = cmp(self.name, other.name)
226 return cmp(self.version, other.version)
229 return hash((self.name, self.version))
232 return '%s-%s' % (self.name, self.version)
234 class Callable(Node):
236 def __init__(self, name, retval, parameters, throws):
237 Node.__init__(self, name)
239 self.parameters = parameters
240 self.throws = not not throws
244 return '%s(%r, %r, %r)' % (self.__class__.__name__,
245 self.name, self.retval,
248 class Function(Callable):
250 def __init__(self, name, retval, parameters, symbol, throws=None):
251 Callable.__init__(self, name, retval, parameters, throws)
253 self.is_method = False
256 def get_parameter_index(self, name):
257 for i, parameter in enumerate(self.parameters):
258 if parameter.name == name:
259 return i + int(self.is_method)
261 def get_parameter(self, name):
262 for parameter in self.parameters:
263 if parameter.name == name:
267 class VFunction(Callable):
269 def __init__(self, name, retval, parameters, throws):
270 Callable.__init__(self, name, retval, parameters, throws)
274 def from_callback(cls, cb):
275 obj = cls(cb.name, cb.retval, cb.parameters[1:],
282 def __init__(self, name, ctype=None):
283 Node.__init__(self, name)
285 self.resolved = False
286 self.is_const = False
287 self.canonical = None
288 self.derefed_canonical = None
294 Type.__init__(self, '<varargs>')
299 def __init__(self, name, ctype, element_type):
302 Type.__init__(self, name, ctype)
303 self.element_type = element_type
304 self.zeroterminated = True
305 self.length_param_index = -1
306 self.length_param_name = None
310 return 'Array(%r, %r)' % (self.name, self.element_type, )
315 def __init__(self, name, ctype, element_type):
316 Type.__init__(self, name, ctype)
317 self.element_type = element_type
320 return 'List(%r of %r)' % (self.name, self.element_type, )
325 def __init__(self, name, ctype, key_type, value_type):
326 Type.__init__(self, name, ctype)
327 self.key_type = key_type
328 self.value_type = value_type
331 return 'Map(%r <%r,%r>)' % (self.name, self.key_type, self.value_type)
336 def __init__(self, name, target, ctype=None):
337 Node.__init__(self, name)
342 return 'Alias(%r, %r)' % (self.name, self.target)
345 class TypeContainer(Node):
347 def __init__(self, name, typenode, transfer):
348 Node.__init__(self, name)
350 if transfer in [PARAM_TRANSFER_NONE, PARAM_TRANSFER_CONTAINER,
351 PARAM_TRANSFER_FULL]:
352 self.transfer = transfer
357 class Parameter(TypeContainer):
359 def __init__(self, name, typenode, direction=None,
360 transfer=None, allow_none=False, scope=None):
361 TypeContainer.__init__(self, name, typenode, transfer)
362 if direction in [PARAM_DIRECTION_IN, PARAM_DIRECTION_OUT,
363 PARAM_DIRECTION_INOUT, None]:
364 self.direction = direction
366 self.direction = PARAM_DIRECTION_IN
368 self.allow_none = allow_none
370 self.closure_index = -1
371 self.destroy_index = -1
375 return 'Parameter(%r, %r)' % (self.name, self.type)
380 def __init__(self, name, symbol, members):
381 Node.__init__(self, name)
383 self.members = members
387 return 'Enum(%r, %r)' % (self.name, self.members)
390 class Bitfield(Node):
392 def __init__(self, name, symbol, members):
393 Node.__init__(self, name)
395 self.members = members
399 return 'Bitfield(%r, %r)' % (self.name, self.members)
404 def __init__(self, name, value, symbol):
405 Node.__init__(self, name)
410 return 'Member(%r, %r)' % (self.name, self.value)
415 def __init__(self, name, symbol, disguised=False):
416 Node.__init__(self, name)
418 self.constructors = []
420 self.disguised = disguised
424 def remove_matching_children(self, pred):
425 self.fields = filter(pred, self.fields)
426 self.constructors = filter(pred, self.constructors)
427 self.methods = filter(pred, self.methods)
435 def __init__(self, name, typenode, symbol, readable, writable, bits=None):
436 Node.__init__(self, name)
439 self.readable = readable
440 self.writable = writable
445 return 'Field(%r, %r, %r)' % (self.name, self.type, self.bits)
447 return 'Field(%r, %r)' % (self.name, self.type)
450 class Return(TypeContainer):
452 def __init__(self, rtype, transfer=None):
453 TypeContainer.__init__(self, None, rtype, transfer)
454 self.direction = PARAM_DIRECTION_OUT
458 return 'Return(%r)' % (self.type, )
463 def __init__(self, name, parent, is_abstract):
464 Node.__init__(self, name)
467 self.glib_type_struct = None
468 self.is_abstract = is_abstract
470 self.virtual_methods = []
471 self.static_methods = []
473 self.constructors = []
478 def remove_matching_children(self, pred):
479 self.methods = filter(pred, self.methods)
480 self.constructors = filter(pred, self.constructors)
481 self.properties = filter(pred, self.properties)
482 self.fields = filter(pred, self.fields)
485 return '%s(%r, %r, %r)' % (
486 self.__class__.__name__,
487 self.name, self.parent, self.methods)
490 class Interface(Node):
492 def __init__(self, name, parent):
493 Node.__init__(self, name)
496 self.virtual_methods = []
497 self.glib_type_struct = None
500 self.prerequisites = []
504 return '%s(%r, %r)' % (
505 self.__class__.__name__,
506 self.name, self.methods)
509 class Constant(Node):
511 def __init__(self, name, type_name, value):
512 Node.__init__(self, name)
513 self.type = Type(type_name)
517 return 'Constant(%r, %r, %r)' % (
518 self.name, self.type, self.value)
521 class Property(Node):
523 def __init__(self, name, type_name, readable, writable,
524 construct, construct_only, ctype=None):
525 Node.__init__(self, name)
526 self.type = Type(type_name, ctype)
527 self.readable = readable
528 self.writable = writable
529 self.construct = construct
530 self.construct_only = construct_only
534 return '%s(%r, %r)' % (
535 self.__class__.__name__,
536 self.name, self.type)
539 # FIXME: Inherit from Function
542 class Callback(Node):
544 def __init__(self, name, retval, parameters, ctype=None):
545 Node.__init__(self, name)
547 self.parameters = parameters
552 def get_parameter_index(self, name):
553 for i, parameter in enumerate(self.parameters):
554 if parameter.name == name:
557 def get_parameter(self, name):
558 for parameter in self.parameters:
559 if parameter.name == name:
563 return 'Callback(%r, %r, %r)' % (
564 self.name, self.retval, self.parameters)
569 def __init__(self, name, symbol):
570 Node.__init__(self, name)
572 self.constructors = []
578 return 'Union(%r, %r)' % (self.name, self.fields, )