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['signed char'] = TYPE_INT8
88 type_names['unsigned char'] = TYPE_UINT8
89 type_names['short'] = TYPE_INT16
90 type_names['signed short'] = TYPE_INT16
91 type_names['unsigned short'] = TYPE_UINT16
92 type_names['int'] = TYPE_INT
93 type_names['signed int'] = TYPE_INT
94 type_names['signed'] = TYPE_INT
95 type_names['unsigned int'] = TYPE_UINT
96 type_names['unsigned'] = TYPE_UINT
97 type_names['long'] = TYPE_LONG
98 type_names['signed long'] = TYPE_LONG
99 type_names['unsigned long'] = TYPE_ULONG
100 type_names['float'] = TYPE_FLOAT
101 type_names['double'] = TYPE_DOUBLE
102 type_names['char*'] = TYPE_STRING
103 type_names['void*'] = TYPE_ANY
104 type_names['void'] = TYPE_NONE
105 type_names['size_t'] = TYPE_SIZET
106 type_names['ssize_t'] = TYPE_SSIZET
107 type_names['off_t'] = TYPE_SIZET
108 # FIXME - can we make libraries use GPid?
109 type_names['pid_t'] = TYPE_INT
111 # Suppress some GLib names
112 type_names['uchar'] = TYPE_UINT8
113 type_names['ushort'] = TYPE_UINT16
114 type_names['size'] = TYPE_SIZET
115 type_names['ssize'] = TYPE_SSIZET
116 type_names['pointer'] = TYPE_ANY
117 type_names['constpointer'] = TYPE_ANY
120 # These types, when seen by reference, are converted into an Array()
122 default_array_types = {}
123 default_array_types['uint8*'] = TYPE_UINT8
124 default_array_types['char**'] = TYPE_STRING
126 # These types, when seen by reference, are interpreted as out parameters
127 default_out_types = (TYPE_INT, TYPE_UINT, TYPE_LONG, TYPE_ULONG,
128 TYPE_FLOAT, TYPE_DOUBLE)
131 def type_name_from_ctype(ctype):
132 return type_names.get(ctype, ctype)
137 def __init__(self, name=None):
139 self.deprecated = None
140 self.deprecated_version = None
143 return '%s(%r)' % (self.__class__.__name__, self.name)
146 class Namespace(Node):
148 def __init__(self, name, version):
149 Node.__init__(self, name)
150 self.version = version
154 return '%s(%r, %r, %r)' % (self.__class__.__name__, self.name,
155 self.version, self.nodes)
160 def __init__(self, name, version):
161 Node.__init__(self, 'include')
163 self.version = version
166 def from_string(self, string):
167 return Include(*string.split('-', 1))
169 def __cmp__(self, other):
170 if not isinstance(other, Include):
171 return cmp(self, other)
172 namecmp = cmp(self.name, other.name)
175 return cmp(self.version, other.version)
178 return hash((self.name, self.version))
181 return '%s-%s' % (self.name, self.version)
184 class Function(Node):
186 def __init__(self, name, retval, parameters, symbol):
187 Node.__init__(self, name)
189 self.parameters = parameters
193 return '%s(%r, %r, %r)' % (self.__class__.__name__,
194 self.name, self.retval,
198 class VFunction(Function):
204 def __init__(self, name, ctype=None):
205 Node.__init__(self, name)
207 self.resolved = False
213 Type.__init__(self, '<varargs>')
218 def __init__(self, ctype, element_type):
219 Type.__init__(self, '<carray>', ctype)
220 self.element_type = element_type
221 self.zeroterminated = True
222 self.length_param_index = -1
223 self.length_param_name = None
226 return 'Array(%r of %r)' % (self.name, self.element_type, )
231 def __init__(self, name, ctype, element_type):
232 Type.__init__(self, name, ctype)
233 self.element_type = element_type
236 return 'List(%r of %r)' % (self.name, self.element_type, )
241 def __init__(self, name, ctype, key_type, value_type):
242 Type.__init__(self, name, ctype)
243 self.key_type = key_type
244 self.value_type = value_type
247 return 'Map(%r <%r,%r.)' % (self.name, self.key_type, self.value_type)
252 def __init__(self, name, target, ctype=None):
253 Node.__init__(self, name)
258 return 'Alias(%r, %r)' % (self.name, self.target)
261 class Parameter(Node):
263 def __init__(self, name, typenode):
264 Node.__init__(self, name)
266 self.direction = PARAM_DIRECTION_IN
268 self.allow_none = False
271 return 'Parameter(%r, %r)' % (self.name, self.type)
276 def __init__(self, name, symbol, members):
277 Node.__init__(self, name)
279 self.members = members
282 return 'Enum(%r, %r)' % (self.name, self.members)
287 def __init__(self, name, value, symbol):
288 Node.__init__(self, name)
293 return 'Member(%r, %r)' % (self.name, self.value)
298 def __init__(self, name, symbol):
299 Node.__init__(self, name)
301 self.constructors = []
307 def __init__(self, name, typenode, symbol, bits=None):
308 Node.__init__(self, name)
315 return 'Field(%r, %r, %r)' % (self.name, self.type, self.bits)
317 return 'Field(%r, %r)' % (self.name, self.type)
322 def __init__(self, rtype, transfer=None):
325 if transfer is None and rtype.name in ['utf8', 'filename']:
326 self.transfer = 'full'
328 self.transfer = transfer
331 return 'Return(%r)' % (self.type, )
336 def __init__(self, name, parent):
337 Node.__init__(self, name)
342 self.constructors = []
347 return '%s(%r, %r, %r)' % (
348 self.__class__.__name__,
349 self.name, self.parent, self.methods)
352 class Interface(Node):
354 def __init__(self, name, parent):
355 Node.__init__(self, name)
362 return '%s(%r, %r)' % (
363 self.__class__.__name__,
364 self.name, self.methods)
367 class Constant(Node):
369 def __init__(self, name, type_name, value):
370 Node.__init__(self, name)
371 self.type = Type(type_name)
375 return 'Constant(%r, %r, %r)' % (
376 self.name, self.type, self.value)
379 class Property(Node):
381 def __init__(self, name, type_name, readable, writable,
382 construct, construct_only, ctype=None):
383 Node.__init__(self, name)
384 self.type = Type(type_name, ctype)
385 self.readable = readable
386 self.writable = writable
387 self.construct = construct
388 self.construct_only = construct_only
391 return '%s(%r, %r, %r)' % (
392 self.__class__.__name__,
393 self.name, self.type, self.value)
396 # FIXME: Inherit from Function
399 class Callback(Node):
401 def __init__(self, name, retval, parameters, ctype=None):
402 Node.__init__(self, name)
404 self.parameters = parameters
408 return 'Callback(%r, %r, %r)' % (
409 self.name, self.retval, self.parameters)
414 def __init__(self, name, symbol):
415 Node.__init__(self, name)
417 self.constructors = []
421 return 'Union(%r, %r)' % (self.name, self.fields, )