2 # GObject-Introspection - a framework for introspecting GObject libraries
3 # Copyright (C) 2008 Johan Dahlin
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program 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
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
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'
82 for name in GIR_TYPES:
83 type_names[name] = name
86 type_names['char'] = TYPE_INT8
87 type_names['unsigned char'] = TYPE_UINT8
88 type_names['short'] = TYPE_INT16
89 type_names['unsigned short'] = TYPE_UINT16
90 type_names['int'] = TYPE_INT
91 type_names['unsigned int'] = TYPE_UINT
92 type_names['unsigned'] = TYPE_UINT
93 type_names['long'] = TYPE_LONG
94 type_names['unsigned long'] = TYPE_ULONG
95 type_names['float'] = TYPE_FLOAT
96 type_names['double'] = TYPE_DOUBLE
97 type_names['char*'] = TYPE_STRING
98 type_names['void*'] = TYPE_ANY
99 type_names['void'] = TYPE_NONE
100 type_names['size_t'] = TYPE_SIZET
101 type_names['ssize_t'] = TYPE_SSIZET
102 # FIXME - can we make libraries use GPid?
103 type_names['pid_t'] = TYPE_INT
105 # Suppress some GLib names
106 type_names['uchar'] = TYPE_UINT8
107 type_names['ushort'] = TYPE_UINT16
108 type_names['size'] = TYPE_SIZET
109 type_names['ssize'] = TYPE_SSIZET
110 type_names['pointer'] = TYPE_ANY
111 type_names['constpointer'] = TYPE_ANY
114 # These types, when seen by reference, are converted into an Array()
116 default_array_types = {}
117 default_array_types['uint8*'] = TYPE_UINT8
118 default_array_types['char**'] = TYPE_STRING
121 def type_name_from_ctype(ctype):
122 return type_names.get(ctype, ctype)
127 def __init__(self, name=None):
129 self.deprecated = None
130 self.deprecated_version = None
133 return '%s(%r)' % (self.__class__.__name__, self.name)
136 class Namespace(Node):
138 def __init__(self, name, version):
139 Node.__init__(self, name)
140 self.version = version
144 return '%s(%r, %r, %r)' % (self.__class__.__name__, self.name,
145 self.version, self.nodes)
148 class Function(Node):
150 def __init__(self, name, retval, parameters, symbol):
151 Node.__init__(self, name)
153 self.parameters = parameters
157 return '%s(%r, %r, %r)' % (self.__class__.__name__,
158 self.name, self.retval,
162 class VFunction(Function):
168 def __init__(self, name, ctype=None):
169 Node.__init__(self, name)
171 self.resolved = False
177 Type.__init__(self, '<varargs>')
182 def __init__(self, ctype, element_type):
183 Type.__init__(self, '<carray>', ctype)
184 self.element_type = element_type
185 self.zeroterminated = True
186 self.length_param_index = -1
187 self.length_param_name = None
190 return 'Array(%r of %r)' % (self.name, self.element_type, )
195 def __init__(self, name, ctype, element_type):
196 Type.__init__(self, name, ctype)
197 self.element_type = element_type
200 return 'List(%r of %r)' % (self.name, self.element_type, )
205 def __init__(self, name, ctype, key_type, value_type):
206 Type.__init__(self, name, ctype)
207 self.key_type = key_type
208 self.value_type = value_type
211 return 'Map(%r <%r,%r.)' % (self.name, self.key_type, self.value_type)
216 def __init__(self, name, target, ctype=None):
217 Node.__init__(self, name)
222 return 'Alias(%r, %r)' % (self.name, self.target)
225 class Parameter(Node):
227 def __init__(self, name, typenode):
228 Node.__init__(self, name)
230 self.direction = PARAM_DIRECTION_IN
232 self.allow_none = False
235 return 'Parameter(%r, %r)' % (self.name, self.type)
240 def __init__(self, name, symbol, members):
241 Node.__init__(self, name)
243 self.members = members
246 return 'Enum(%r, %r)' % (self.name, self.members)
251 def __init__(self, name, value, symbol):
252 Node.__init__(self, name)
257 return 'Member(%r, %r)' % (self.name, self.value)
262 def __init__(self, name, symbol):
263 Node.__init__(self, name)
265 self.constructors = []
271 def __init__(self, name, typenode, symbol, bits=None):
272 Node.__init__(self, name)
279 return 'Field(%r, %r, %r)' % (self.name, self.type, self.bits)
281 return 'Field(%r, %r)' % (self.name, self.type)
286 def __init__(self, rtype, transfer=None):
289 if transfer is None and rtype.name in ['utf8', 'filename']:
290 self.transfer = 'full'
292 self.transfer = transfer
295 return 'Return(%r)' % (self.type, )
300 def __init__(self, name, parent):
301 Node.__init__(self, name)
306 self.constructors = []
311 return '%s(%r, %r, %r)' % (
312 self.__class__.__name__,
313 self.name, self.parent, self.methods)
316 class Interface(Node):
318 def __init__(self, name, parent):
319 Node.__init__(self, name)
326 return '%s(%r, %r)' % (
327 self.__class__.__name__,
328 self.name, self.methods)
331 class Constant(Node):
333 def __init__(self, name, type_name, value):
334 Node.__init__(self, name)
335 self.type = Type(type_name)
339 return 'Constant(%r, %r, %r)' % (
340 self.name, self.type, self.value)
343 class Property(Node):
345 def __init__(self, name, type_name, readable, writable,
346 construct, construct_only, ctype=None):
347 Node.__init__(self, name)
348 self.type = Type(type_name, ctype)
349 self.readable = readable
350 self.writable = writable
351 self.construct = construct
352 self.construct_only = construct_only
355 return '%s(%r, %r, %r)' % (
356 self.__class__.__name__,
357 self.name, self.type, self.value)
360 # FIXME: Inherit from Function
363 class Callback(Node):
365 def __init__(self, name, retval, parameters, ctype=None):
366 Node.__init__(self, name)
368 self.parameters = parameters
372 return 'Callback(%r, %r, %r)' % (
373 self.name, self.retval, self.parameters)
378 def __init__(self, name, symbol):
379 Node.__init__(self, name)
381 self.constructors = []
385 return 'Union(%r, %r)' % (self.name, self.fields, )