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_UINT16 = 'uint16'
44 TYPE_UINT32 = 'uint32'
46 TYPE_UINT64 = 'uint64'
49 TYPE_SSIZET = 'ssize_t'
54 TYPE_DOUBLE = 'double'
55 TYPE_STRING = 'utf8' # requires zero-terminated
56 TYPE_FILENAME = 'filename'
58 BASIC_GIR_TYPES = [TYPE_BOOLEAN, TYPE_INT8, TYPE_UINT8, TYPE_INT16,
59 TYPE_UINT16, TYPE_INT32, TYPE_UINT32, TYPE_INT64,
60 TYPE_UINT64, TYPE_INT, TYPE_UINT, TYPE_LONG,
61 TYPE_ULONG, TYPE_SSIZET, TYPE_SIZET, TYPE_FLOAT,
62 TYPE_DOUBLE, TYPE_TIMET, TYPE_GTYPE]
63 GIR_TYPES = [TYPE_NONE, TYPE_ANY]
64 GIR_TYPES.extend(BASIC_GIR_TYPES)
65 GIR_TYPES.extend([TYPE_STRING, TYPE_FILENAME])
67 # Higher-level data types
68 TYPE_SEQUENCE = 'sequence' # Sequence of something
72 TYPE_USTRING = 'ustring'
78 PARAM_DIRECTION_IN = 'in'
79 PARAM_DIRECTION_OUT = 'out'
80 PARAM_DIRECTION_INOUT = 'inout'
82 PARAM_TRANSFER_NONE = 'none'
83 PARAM_TRANSFER_CONTAINER = 'container'
84 PARAM_TRANSFER_FULL = 'full'
87 for name in GIR_TYPES:
88 type_names[name] = name
91 type_names['char'] = TYPE_INT8
92 type_names['signed char'] = TYPE_INT8
93 type_names['unsigned char'] = TYPE_UINT8
94 type_names['short'] = TYPE_INT16
95 type_names['signed short'] = TYPE_INT16
96 type_names['unsigned short'] = TYPE_UINT16
97 type_names['int'] = TYPE_INT
98 type_names['signed int'] = TYPE_INT
99 type_names['signed'] = TYPE_INT
100 type_names['unsigned int'] = TYPE_UINT
101 type_names['unsigned'] = TYPE_UINT
102 type_names['long'] = TYPE_LONG
103 type_names['signed long'] = TYPE_LONG
104 type_names['unsigned long'] = TYPE_ULONG
105 type_names['float'] = TYPE_FLOAT
106 type_names['double'] = TYPE_DOUBLE
107 type_names['char*'] = TYPE_STRING
108 type_names['void*'] = TYPE_ANY
109 type_names['void'] = TYPE_NONE
112 type_names['off_t'] = TYPE_SIZET
113 type_names['pid_t'] = TYPE_INT
114 type_names['size_t'] = TYPE_SIZET
115 type_names['ssize_t'] = TYPE_SSIZET
116 type_names['socklen_t'] = TYPE_INT32
119 type_names['id'] = TYPE_ANY
121 # Suppress some GLib names
122 type_names['uchar'] = TYPE_UINT8
123 type_names['ushort'] = TYPE_UINT16
124 type_names['size'] = TYPE_SIZET
125 type_names['ssize'] = TYPE_SSIZET
126 type_names['pointer'] = TYPE_ANY
127 type_names['constpointer'] = TYPE_ANY
130 # These types, when seen by reference, are converted into an Array()
132 # If you add/change these, be sure to update glibast.py too
133 default_array_types = {}
134 default_array_types['uint8*'] = TYPE_UINT8
135 default_array_types['utf8*'] = TYPE_STRING
137 # These types, when seen by reference, are interpreted as out parameters
138 default_out_types = (TYPE_INT, TYPE_UINT, TYPE_LONG, TYPE_ULONG,
139 TYPE_FLOAT, TYPE_DOUBLE, TYPE_SIZET, TYPE_SSIZET)
142 def type_name_from_ctype(ctype):
143 return type_names.get(ctype, ctype)
148 def __init__(self, name=None):
150 self.attributes = [] # (key, value)*
151 self.deprecated = None
152 self.deprecated_version = None
155 def __cmp__(self, other):
156 return cmp(self.name, other.name)
159 return '%s(%r)' % (self.__class__.__name__, self.name)
162 class Namespace(Node):
164 def __init__(self, name, version):
165 Node.__init__(self, name)
166 self.version = version
170 return '%s(%r, %r, %r)' % (self.__class__.__name__, self.name,
171 self.version, self.nodes)
176 def __init__(self, name, version):
177 Node.__init__(self, 'include')
179 self.version = version
182 def from_string(self, string):
183 return Include(*string.split('-', 1))
185 def __cmp__(self, other):
186 if not isinstance(other, Include):
187 return cmp(self, other)
188 namecmp = cmp(self.name, other.name)
191 return cmp(self.version, other.version)
194 return hash((self.name, self.version))
197 return '%s-%s' % (self.name, self.version)
200 class Function(Node):
202 def __init__(self, name, retval, parameters, symbol, throws=None):
203 Node.__init__(self, name)
205 self.parameters = parameters
207 self.throws = not not throws
208 self.is_method = False
211 def get_parameter_index(self, name):
212 for i, parameter in enumerate(self.parameters):
213 if parameter.name == name:
214 return i + int(self.is_method)
216 def get_parameter(self, name):
217 for parameter in self.parameters:
218 if parameter.name == name:
222 return '%s(%r, %r, %r)' % (self.__class__.__name__,
223 self.name, self.retval,
227 class VFunction(Function):
233 def __init__(self, name, ctype=None):
234 Node.__init__(self, name)
236 self.resolved = False
237 self.is_const = False
238 self.canonical = None
239 self.derefed_canonical = None
245 Type.__init__(self, '<varargs>')
250 def __init__(self, ctype, element_type):
251 Type.__init__(self, '<carray>', ctype)
252 self.element_type = element_type
253 self.zeroterminated = True
254 self.length_param_index = -1
255 self.length_param_name = None
259 return 'Array(%r, %r)' % (self.name, self.element_type, )
264 def __init__(self, name, ctype, element_type):
265 Type.__init__(self, name, ctype)
266 self.element_type = element_type
269 return 'List(%r of %r)' % (self.name, self.element_type, )
274 def __init__(self, name, ctype, key_type, value_type):
275 Type.__init__(self, name, ctype)
276 self.key_type = key_type
277 self.value_type = value_type
280 return 'Map(%r <%r,%r.)' % (self.name, self.key_type, self.value_type)
285 def __init__(self, name, target, ctype=None):
286 Node.__init__(self, name)
291 return 'Alias(%r, %r)' % (self.name, self.target)
294 class TypeContainer(Node):
296 def __init__(self, name, typenode, transfer):
297 Node.__init__(self, name)
299 if transfer in [PARAM_TRANSFER_NONE, PARAM_TRANSFER_CONTAINER,
300 PARAM_TRANSFER_FULL]:
301 self.transfer = transfer
306 class Parameter(TypeContainer):
308 def __init__(self, name, typenode, direction=None,
309 transfer=None, allow_none=False, scope=None):
310 TypeContainer.__init__(self, name, typenode, transfer)
311 if direction in [PARAM_DIRECTION_IN, PARAM_DIRECTION_OUT,
312 PARAM_DIRECTION_INOUT, None]:
313 self.direction = direction
315 self.direction = PARAM_DIRECTION_IN
317 self.allow_none = allow_none
319 self.closure_index = -1
320 self.destroy_index = -1
324 return 'Parameter(%r, %r)' % (self.name, self.type)
329 def __init__(self, name, symbol, members):
330 Node.__init__(self, name)
332 self.members = members
336 return 'Enum(%r, %r)' % (self.name, self.members)
339 class Bitfield(Node):
341 def __init__(self, name, symbol, members):
342 Node.__init__(self, name)
344 self.members = members
348 return 'Bitfield(%r, %r)' % (self.name, self.members)
353 def __init__(self, name, value, symbol):
354 Node.__init__(self, name)
359 return 'Member(%r, %r)' % (self.name, self.value)
364 def __init__(self, name, symbol, disguised=False):
365 Node.__init__(self, name)
367 self.constructors = []
369 self.disguised = disguised
379 def __init__(self, name, typenode, symbol, readable, writable, bits=None):
380 Node.__init__(self, name)
383 self.readable = readable
384 self.writable = writable
389 return 'Field(%r, %r, %r)' % (self.name, self.type, self.bits)
391 return 'Field(%r, %r)' % (self.name, self.type)
394 class Return(TypeContainer):
396 def __init__(self, rtype, transfer=None):
397 TypeContainer.__init__(self, None, rtype, transfer)
398 self.direction = PARAM_DIRECTION_OUT
402 return 'Return(%r)' % (self.type, )
407 def __init__(self, name, parent, is_abstract):
408 Node.__init__(self, name)
411 self.glib_type_struct = None
412 self.is_abstract = is_abstract
414 self.static_methods = []
416 self.constructors = []
422 return '%s(%r, %r, %r)' % (
423 self.__class__.__name__,
424 self.name, self.parent, self.methods)
427 class Interface(Node):
429 def __init__(self, name, parent):
430 Node.__init__(self, name)
433 self.glib_type_struct = None
436 self.prerequisites = []
440 return '%s(%r, %r)' % (
441 self.__class__.__name__,
442 self.name, self.methods)
445 class Constant(Node):
447 def __init__(self, name, type_name, value):
448 Node.__init__(self, name)
449 self.type = Type(type_name)
453 return 'Constant(%r, %r, %r)' % (
454 self.name, self.type, self.value)
457 class Property(Node):
459 def __init__(self, name, type_name, readable, writable,
460 construct, construct_only, ctype=None):
461 Node.__init__(self, name)
462 self.type = Type(type_name, ctype)
463 self.readable = readable
464 self.writable = writable
465 self.construct = construct
466 self.construct_only = construct_only
470 return '%s(%r, %r)' % (
471 self.__class__.__name__,
472 self.name, self.type)
475 # FIXME: Inherit from Function
478 class Callback(Node):
480 def __init__(self, name, retval, parameters, ctype=None):
481 Node.__init__(self, name)
483 self.parameters = parameters
489 return 'Callback(%r, %r, %r)' % (
490 self.name, self.retval, self.parameters)
495 def __init__(self, name, symbol):
496 Node.__init__(self, name)
498 self.constructors = []
504 return 'Union(%r, %r)' % (self.name, self.fields, )