2 # GObject-Introspection - a framework for introspecting GObject libraries
3 # Copyright (C) 2008 Johan Dahlin
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2 of the License, or (at your option) any later version.
10 # This library 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 GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the
17 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 # Boston, MA 02111-1307, USA.
22 This file descbribes abstract data type nodes independent on the
23 implementation language.
25 These can later on be extended (eg subclassed) with additional information
26 which is language/library/domain specific.
30 ## Basic types, modeled on GITypeTag but not equivalent
33 TYPE_NONE = 'none' # We differ from repository on these first two
35 TYPE_BOOLEAN = 'boolean'
39 TYPE_UINT16 = 'uint16'
43 TYPE_UINT32 = 'uint32'
45 TYPE_UINT64 = 'uint64'
48 TYPE_SSIZET = 'ssize_t'
53 TYPE_DOUBLE = 'double'
54 TYPE_STRING = 'utf8' # requires zero-terminated
55 TYPE_FILENAME = 'filename'
57 BASIC_GIR_TYPES = [TYPE_BOOLEAN, TYPE_INT8, TYPE_UINT8, TYPE_INT16,
58 TYPE_UINT16, TYPE_INT32, TYPE_UINT32, TYPE_INT64,
59 TYPE_UINT64, TYPE_INT, TYPE_UINT, TYPE_LONG,
60 TYPE_ULONG, TYPE_SSIZET, TYPE_SIZET, TYPE_FLOAT,
61 TYPE_DOUBLE, TYPE_TIMET, TYPE_GTYPE]
62 GIR_TYPES = [TYPE_NONE, TYPE_ANY]
63 GIR_TYPES.extend(BASIC_GIR_TYPES)
64 GIR_TYPES.extend([TYPE_STRING, TYPE_FILENAME])
66 # Higher-level data types
67 TYPE_SEQUENCE = 'sequence' # Sequence of something
71 TYPE_USTRING = 'ustring'
77 PARAM_DIRECTION_IN = 'in'
78 PARAM_DIRECTION_OUT = 'out'
79 PARAM_DIRECTION_INOUT = 'inout'
81 PARAM_TRANSFER_NONE = 'none'
82 PARAM_TRANSFER_CONTAINER = 'container'
83 PARAM_TRANSFER_FULL = 'full'
86 for name in GIR_TYPES:
87 type_names[name] = name
90 type_names['char'] = TYPE_INT8
91 type_names['signed char'] = TYPE_INT8
92 type_names['unsigned char'] = TYPE_UINT8
93 type_names['short'] = TYPE_INT16
94 type_names['signed short'] = TYPE_INT16
95 type_names['unsigned short'] = TYPE_UINT16
96 type_names['int'] = TYPE_INT
97 type_names['signed int'] = TYPE_INT
98 type_names['signed'] = TYPE_INT
99 type_names['unsigned int'] = TYPE_UINT
100 type_names['unsigned'] = TYPE_UINT
101 type_names['long'] = TYPE_LONG
102 type_names['signed long'] = TYPE_LONG
103 type_names['unsigned long'] = TYPE_ULONG
104 type_names['float'] = TYPE_FLOAT
105 type_names['double'] = TYPE_DOUBLE
106 type_names['char*'] = TYPE_STRING
107 type_names['void*'] = TYPE_ANY
108 type_names['void'] = TYPE_NONE
111 type_names['off_t'] = TYPE_SIZET
112 type_names['pid_t'] = TYPE_INT
113 type_names['size_t'] = TYPE_SIZET
114 type_names['ssize_t'] = TYPE_SSIZET
115 type_names['socklen_t'] = TYPE_INT32
118 type_names['id'] = TYPE_ANY
120 # Suppress some GLib names
121 type_names['uchar'] = TYPE_UINT8
122 type_names['ushort'] = TYPE_UINT16
123 type_names['size'] = TYPE_SIZET
124 type_names['ssize'] = TYPE_SSIZET
125 type_names['pointer'] = TYPE_ANY
126 type_names['constpointer'] = TYPE_ANY
129 # These types, when seen by reference, are converted into an Array()
131 # If you add/change these, be sure to update glibast.py too
132 default_array_types = {}
133 default_array_types['uint8*'] = TYPE_UINT8
134 default_array_types['utf8*'] = TYPE_STRING
136 # These types, when seen by reference, are interpreted as out parameters
137 default_out_types = (TYPE_INT, TYPE_UINT, TYPE_LONG, TYPE_ULONG,
138 TYPE_FLOAT, TYPE_DOUBLE, TYPE_SIZET, TYPE_SSIZET)
141 def type_name_from_ctype(ctype):
142 return type_names.get(ctype, ctype)
147 def __init__(self, name=None):
149 self.deprecated = None
150 self.deprecated_version = None
154 return '%s(%r)' % (self.__class__.__name__, self.name)
157 class Namespace(Node):
159 def __init__(self, name, version):
160 Node.__init__(self, name)
161 self.version = version
165 return '%s(%r, %r, %r)' % (self.__class__.__name__, self.name,
166 self.version, self.nodes)
171 def __init__(self, name, version):
172 Node.__init__(self, 'include')
174 self.version = version
177 def from_string(self, string):
178 return Include(*string.split('-', 1))
180 def __cmp__(self, other):
181 if not isinstance(other, Include):
182 return cmp(self, other)
183 namecmp = cmp(self.name, other.name)
186 return cmp(self.version, other.version)
189 return hash((self.name, self.version))
192 return '%s-%s' % (self.name, self.version)
195 class Function(Node):
197 def __init__(self, name, retval, parameters, symbol, throws=None):
198 Node.__init__(self, name)
200 self.parameters = parameters
202 self.throws = not not throws
203 self.is_method = False
206 def get_parameter_index(self, name):
207 for i, parameter in enumerate(self.parameters):
208 if parameter.name == name:
209 return i + int(self.is_method)
211 def get_parameter(self, name):
212 for parameter in self.parameters:
213 if parameter.name == name:
217 return '%s(%r, %r, %r)' % (self.__class__.__name__,
218 self.name, self.retval,
222 class VFunction(Function):
228 def __init__(self, name, ctype=None):
229 Node.__init__(self, name)
231 self.resolved = False
232 self.is_const = False
233 self.canonical = None
234 self.derefed_canonical = None
240 Type.__init__(self, '<varargs>')
245 def __init__(self, ctype, element_type):
246 Type.__init__(self, '<carray>', ctype)
247 self.element_type = element_type
248 self.zeroterminated = True
249 self.length_param_index = -1
250 self.length_param_name = None
254 return 'Array(%r, %r)' % (self.name, self.element_type, )
259 def __init__(self, name, ctype, element_type):
260 Type.__init__(self, name, ctype)
261 self.element_type = element_type
264 return 'List(%r of %r)' % (self.name, self.element_type, )
269 def __init__(self, name, ctype, key_type, value_type):
270 Type.__init__(self, name, ctype)
271 self.key_type = key_type
272 self.value_type = value_type
275 return 'Map(%r <%r,%r.)' % (self.name, self.key_type, self.value_type)
280 def __init__(self, name, target, ctype=None):
281 Node.__init__(self, name)
286 return 'Alias(%r, %r)' % (self.name, self.target)
289 class TypeContainer(Node):
291 def __init__(self, name, typenode, transfer):
292 Node.__init__(self, name)
294 if transfer in [PARAM_TRANSFER_NONE, PARAM_TRANSFER_CONTAINER,
295 PARAM_TRANSFER_FULL]:
296 self.transfer = transfer
301 class Parameter(TypeContainer):
303 def __init__(self, name, typenode, direction=None,
304 transfer=None, allow_none=False, scope=None):
305 TypeContainer.__init__(self, name, typenode, transfer)
306 if direction in [PARAM_DIRECTION_IN, PARAM_DIRECTION_OUT,
307 PARAM_DIRECTION_INOUT, None]:
308 self.direction = direction
310 self.direction = PARAM_DIRECTION_IN
312 self.allow_none = allow_none
314 self.closure_index = -1
315 self.destroy_index = -1
319 return 'Parameter(%r, %r)' % (self.name, self.type)
324 def __init__(self, name, symbol, members):
325 Node.__init__(self, name)
327 self.members = members
331 return 'Enum(%r, %r)' % (self.name, self.members)
334 class Bitfield(Node):
336 def __init__(self, name, symbol, members):
337 Node.__init__(self, name)
339 self.members = members
343 return 'Bitfield(%r, %r)' % (self.name, self.members)
348 def __init__(self, name, value, symbol):
349 Node.__init__(self, name)
354 return 'Member(%r, %r)' % (self.name, self.value)
359 def __init__(self, name, symbol, disguised=False):
360 Node.__init__(self, name)
362 self.constructors = []
364 self.disguised = disguised
366 self.constructors = []
375 def __init__(self, name, typenode, symbol, readable, writable, bits=None):
376 Node.__init__(self, name)
379 self.readable = readable
380 self.writable = writable
385 return 'Field(%r, %r, %r)' % (self.name, self.type, self.bits)
387 return 'Field(%r, %r)' % (self.name, self.type)
390 class Return(TypeContainer):
392 def __init__(self, rtype, transfer=None):
393 TypeContainer.__init__(self, None, rtype, transfer)
394 self.direction = PARAM_DIRECTION_OUT
398 return 'Return(%r)' % (self.type, )
403 def __init__(self, name, parent, is_abstract):
404 Node.__init__(self, name)
407 self.is_abstract = is_abstract
409 self.static_methods = []
411 self.constructors = []
417 return '%s(%r, %r, %r)' % (
418 self.__class__.__name__,
419 self.name, self.parent, self.methods)
422 class Interface(Node):
424 def __init__(self, name, parent):
425 Node.__init__(self, name)
430 self.prerequisites = []
434 return '%s(%r, %r)' % (
435 self.__class__.__name__,
436 self.name, self.methods)
439 class Constant(Node):
441 def __init__(self, name, type_name, value):
442 Node.__init__(self, name)
443 self.type = Type(type_name)
447 return 'Constant(%r, %r, %r)' % (
448 self.name, self.type, self.value)
451 class Property(Node):
453 def __init__(self, name, type_name, readable, writable,
454 construct, construct_only, ctype=None):
455 Node.__init__(self, name)
456 self.type = Type(type_name, ctype)
457 self.readable = readable
458 self.writable = writable
459 self.construct = construct
460 self.construct_only = construct_only
464 return '%s(%r, %r)' % (
465 self.__class__.__name__,
466 self.name, self.type)
469 # FIXME: Inherit from Function
472 class Callback(Node):
474 def __init__(self, name, retval, parameters, ctype=None):
475 Node.__init__(self, name)
477 self.parameters = parameters
483 return 'Callback(%r, %r, %r)' % (
484 self.name, self.retval, self.parameters)
489 def __init__(self, name, symbol):
490 Node.__init__(self, name)
492 self.constructors = []
498 return 'Union(%r, %r)' % (self.name, self.fields, )