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_TRANSFER_NONE = 'none'
86 PARAM_TRANSFER_CONTAINER = 'container'
87 PARAM_TRANSFER_FULL = 'full'
90 for name in GIR_TYPES:
91 type_names[name] = name
94 type_names['char'] = TYPE_INT8
95 type_names['signed char'] = TYPE_INT8
96 type_names['unsigned char'] = TYPE_UINT8
97 type_names['short'] = TYPE_SHORT
98 type_names['signed short'] = TYPE_SHORT
99 type_names['unsigned short'] = TYPE_USHORT
100 type_names['int'] = TYPE_INT
101 type_names['signed int'] = TYPE_INT
102 type_names['signed'] = TYPE_INT
103 type_names['unsigned int'] = TYPE_UINT
104 type_names['unsigned'] = TYPE_UINT
105 type_names['long'] = TYPE_LONG
106 type_names['signed long'] = TYPE_LONG
107 type_names['unsigned long'] = TYPE_ULONG
108 type_names['float'] = TYPE_FLOAT
109 type_names['double'] = TYPE_DOUBLE
110 type_names['char*'] = TYPE_STRING
111 type_names['void*'] = TYPE_ANY
112 type_names['void'] = TYPE_NONE
115 type_names['off_t'] = TYPE_SIZET
116 type_names['pid_t'] = TYPE_INT
117 type_names['size_t'] = TYPE_SIZET
118 type_names['ssize_t'] = TYPE_SSIZET
119 type_names['socklen_t'] = TYPE_INT32
122 type_names['id'] = TYPE_ANY
124 # Suppress some GLib names
125 type_names['uchar'] = TYPE_UINT8
126 type_names['ushort'] = TYPE_USHORT
127 type_names['size'] = TYPE_SIZET
128 type_names['ssize'] = TYPE_SSIZET
129 type_names['pointer'] = TYPE_ANY
130 type_names['constpointer'] = TYPE_ANY
133 # These types, when seen by reference, are converted into an Array()
135 # If you add/change these, be sure to update glibast.py too
136 default_array_types = {}
137 default_array_types['uint8*'] = TYPE_UINT8
138 default_array_types['utf8*'] = TYPE_STRING
140 # These types, when seen by reference, are interpreted as out parameters
141 default_out_types = (TYPE_SHORT, TYPE_USHORT, TYPE_INT, TYPE_UINT,
142 TYPE_LONG, TYPE_ULONG, TYPE_FLOAT, TYPE_DOUBLE,
143 TYPE_SIZET, TYPE_SSIZET)
146 def type_name_from_ctype(ctype):
147 return type_names.get(ctype, ctype)
152 def __init__(self, name=None):
154 self.attributes = [] # (key, value)*
156 self.deprecated = None
157 self.deprecated_version = None
160 def __cmp__(self, other):
161 return cmp(self.name, other.name)
164 return '%s(%r)' % (self.__class__.__name__, self.name)
166 def remove_matching_children(self, pred):
169 class Namespace(Node):
171 def __init__(self, name, version):
172 Node.__init__(self, name)
173 self.version = version
177 return '%s(%r, %r, %r)' % (self.__class__.__name__, self.name,
178 self.version, self.nodes)
180 def remove_matching(self, pred):
182 def recursive_pred(node):
183 node.remove_matching_children(pred)
186 self.nodes = filter(recursive_pred, self.nodes)
190 def __init__(self, name, version):
191 Node.__init__(self, 'include')
193 self.version = version
196 def from_string(self, string):
197 return Include(*string.split('-', 1))
199 def __cmp__(self, other):
200 if not isinstance(other, Include):
201 return cmp(self, other)
202 namecmp = cmp(self.name, other.name)
205 return cmp(self.version, other.version)
208 return hash((self.name, self.version))
211 return '%s-%s' % (self.name, self.version)
213 class Callable(Node):
215 def __init__(self, name, retval, parameters, throws):
216 Node.__init__(self, name)
218 self.parameters = parameters
219 self.throws = not not throws
223 return '%s(%r, %r, %r)' % (self.__class__.__name__,
224 self.name, self.retval,
227 class Function(Callable):
229 def __init__(self, name, retval, parameters, symbol, throws=None):
230 Callable.__init__(self, name, retval, parameters, throws)
232 self.is_method = False
235 def get_parameter_index(self, name):
236 for i, parameter in enumerate(self.parameters):
237 if parameter.name == name:
238 return i + int(self.is_method)
240 def get_parameter(self, name):
241 for parameter in self.parameters:
242 if parameter.name == name:
246 class VFunction(Callable):
248 def __init__(self, name, retval, parameters, throws):
249 Callable.__init__(self, name, retval, parameters, throws)
253 def from_callback(cls, cb):
254 obj = cls(cb.name, cb.retval, cb.parameters[1:],
261 def __init__(self, name, ctype=None):
262 Node.__init__(self, name)
264 self.resolved = False
265 self.is_const = False
266 self.canonical = None
267 self.derefed_canonical = None
273 Type.__init__(self, '<varargs>')
278 def __init__(self, name, ctype, element_type):
281 Type.__init__(self, name, ctype)
282 self.element_type = element_type
283 self.zeroterminated = True
284 self.length_param_index = -1
285 self.length_param_name = None
289 return 'Array(%r, %r)' % (self.name, self.element_type, )
294 def __init__(self, name, ctype, element_type):
295 Type.__init__(self, name, ctype)
296 self.element_type = element_type
299 return 'List(%r of %r)' % (self.name, self.element_type, )
304 def __init__(self, name, ctype, key_type, value_type):
305 Type.__init__(self, name, ctype)
306 self.key_type = key_type
307 self.value_type = value_type
310 return 'Map(%r <%r,%r>)' % (self.name, self.key_type, self.value_type)
315 def __init__(self, name, target, ctype=None):
316 Node.__init__(self, name)
321 return 'Alias(%r, %r)' % (self.name, self.target)
324 class TypeContainer(Node):
326 def __init__(self, name, typenode, transfer):
327 Node.__init__(self, name)
329 if transfer in [PARAM_TRANSFER_NONE, PARAM_TRANSFER_CONTAINER,
330 PARAM_TRANSFER_FULL]:
331 self.transfer = transfer
336 class Parameter(TypeContainer):
338 def __init__(self, name, typenode, direction=None,
339 transfer=None, allow_none=False, scope=None):
340 TypeContainer.__init__(self, name, typenode, transfer)
341 if direction in [PARAM_DIRECTION_IN, PARAM_DIRECTION_OUT,
342 PARAM_DIRECTION_INOUT, None]:
343 self.direction = direction
345 self.direction = PARAM_DIRECTION_IN
347 self.allow_none = allow_none
349 self.closure_index = -1
350 self.destroy_index = -1
354 return 'Parameter(%r, %r)' % (self.name, self.type)
359 def __init__(self, name, symbol, members):
360 Node.__init__(self, name)
362 self.members = members
366 return 'Enum(%r, %r)' % (self.name, self.members)
369 class Bitfield(Node):
371 def __init__(self, name, symbol, members):
372 Node.__init__(self, name)
374 self.members = members
378 return 'Bitfield(%r, %r)' % (self.name, self.members)
383 def __init__(self, name, value, symbol):
384 Node.__init__(self, name)
389 return 'Member(%r, %r)' % (self.name, self.value)
394 def __init__(self, name, symbol, disguised=False):
395 Node.__init__(self, name)
397 self.constructors = []
399 self.disguised = disguised
403 def remove_matching_children(self, pred):
404 self.fields = filter(pred, self.fields)
405 self.constructors = filter(pred, self.constructors)
406 self.methods = filter(pred, self.methods)
414 def __init__(self, name, typenode, symbol, readable, writable, bits=None):
415 Node.__init__(self, name)
418 self.readable = readable
419 self.writable = writable
424 return 'Field(%r, %r, %r)' % (self.name, self.type, self.bits)
426 return 'Field(%r, %r)' % (self.name, self.type)
429 class Return(TypeContainer):
431 def __init__(self, rtype, transfer=None):
432 TypeContainer.__init__(self, None, rtype, transfer)
433 self.direction = PARAM_DIRECTION_OUT
437 return 'Return(%r)' % (self.type, )
442 def __init__(self, name, parent, is_abstract):
443 Node.__init__(self, name)
446 self.glib_type_struct = None
447 self.is_abstract = is_abstract
449 self.virtual_methods = []
450 self.static_methods = []
452 self.constructors = []
457 def remove_matching_children(self, pred):
458 self.methods = filter(pred, self.methods)
459 self.constructors = filter(pred, self.constructors)
460 self.properties = filter(pred, self.properties)
461 self.fields = filter(pred, self.fields)
464 return '%s(%r, %r, %r)' % (
465 self.__class__.__name__,
466 self.name, self.parent, self.methods)
469 class Interface(Node):
471 def __init__(self, name, parent):
472 Node.__init__(self, name)
475 self.virtual_methods = []
476 self.glib_type_struct = None
479 self.prerequisites = []
483 return '%s(%r, %r)' % (
484 self.__class__.__name__,
485 self.name, self.methods)
488 class Constant(Node):
490 def __init__(self, name, type_name, value):
491 Node.__init__(self, name)
492 self.type = Type(type_name)
496 return 'Constant(%r, %r, %r)' % (
497 self.name, self.type, self.value)
500 class Property(Node):
502 def __init__(self, name, type_name, readable, writable,
503 construct, construct_only, ctype=None):
504 Node.__init__(self, name)
505 self.type = Type(type_name, ctype)
506 self.readable = readable
507 self.writable = writable
508 self.construct = construct
509 self.construct_only = construct_only
513 return '%s(%r, %r)' % (
514 self.__class__.__name__,
515 self.name, self.type)
518 # FIXME: Inherit from Function
521 class Callback(Node):
523 def __init__(self, name, retval, parameters, ctype=None):
524 Node.__init__(self, name)
526 self.parameters = parameters
531 def get_parameter_index(self, name):
532 for i, parameter in enumerate(self.parameters):
533 if parameter.name == name:
536 def get_parameter(self, name):
537 for parameter in self.parameters:
538 if parameter.name == name:
542 return 'Callback(%r, %r, %r)' % (
543 self.name, self.retval, self.parameters)
548 def __init__(self, name, symbol):
549 Node.__init__(self, name)
551 self.constructors = []
557 return 'Union(%r, %r)' % (self.name, self.fields, )