blob: 88711e1af10f713ad0f02c8f8e7d542e70701301 [file] [log] [blame]
José Fonseca7ad40262009-09-30 17:17:12 +01001##########################################################################
José Fonseca95442442008-07-08 10:32:53 +09002#
José Fonseca6fac5ae2010-11-29 16:09:13 +00003# Copyright 2008-2010 VMware, Inc.
José Fonseca7ad40262009-09-30 17:17:12 +01004# All Rights Reserved.
José Fonseca95442442008-07-08 10:32:53 +09005#
José Fonseca7ad40262009-09-30 17:17:12 +01006# Permission is hereby granted, free of charge, to any person obtaining a copy
7# of this software and associated documentation files (the "Software"), to deal
8# in the Software without restriction, including without limitation the rights
9# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10# copies of the Software, and to permit persons to whom the Software is
11# furnished to do so, subject to the following conditions:
José Fonseca95442442008-07-08 10:32:53 +090012#
José Fonseca7ad40262009-09-30 17:17:12 +010013# The above copyright notice and this permission notice shall be included in
14# all copies or substantial portions of the Software.
José Fonseca95442442008-07-08 10:32:53 +090015#
José Fonseca7ad40262009-09-30 17:17:12 +010016# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22# THE SOFTWARE.
José Fonseca95442442008-07-08 10:32:53 +090023#
José Fonseca7ad40262009-09-30 17:17:12 +010024##########################################################################/
José Fonseca95442442008-07-08 10:32:53 +090025
José Fonsecad626cf42008-07-07 07:43:16 +090026"""C basic types"""
27
José Fonseca8a56d142008-07-09 12:18:08 +090028
29import debug
30
31
José Fonseca6fac5ae2010-11-29 16:09:13 +000032class Type:
José Fonseca02c25002011-10-15 13:17:26 +010033 """Base class for all types."""
José Fonseca6fac5ae2010-11-29 16:09:13 +000034
José Fonseca02c25002011-10-15 13:17:26 +010035 __tags = set()
José Fonseca6fac5ae2010-11-29 16:09:13 +000036
José Fonseca02c25002011-10-15 13:17:26 +010037 def __init__(self, expr, tag = None):
José Fonseca6fac5ae2010-11-29 16:09:13 +000038 self.expr = expr
José Fonseca6fac5ae2010-11-29 16:09:13 +000039
José Fonseca02c25002011-10-15 13:17:26 +010040 # Generate a default tag, used when naming functions that will operate
41 # on this type, so it should preferrably be something representative of
42 # the type.
43 if tag is None:
José Fonseca5b6fb752012-04-14 14:56:45 +010044 if expr is not None:
45 tag = ''.join([c for c in expr if c.isalnum() or c in '_'])
46 else:
47 tag = 'anonynoums'
José Fonseca02c25002011-10-15 13:17:26 +010048 else:
49 for c in tag:
50 assert c.isalnum() or c in '_'
José Fonseca6fac5ae2010-11-29 16:09:13 +000051
José Fonseca02c25002011-10-15 13:17:26 +010052 # Ensure it is unique.
53 if tag in Type.__tags:
54 suffix = 1
55 while tag + str(suffix) in Type.__tags:
56 suffix += 1
57 tag += str(suffix)
58
59 assert tag not in Type.__tags
60 Type.__tags.add(tag)
61
62 self.tag = tag
José Fonseca6fac5ae2010-11-29 16:09:13 +000063
64 def __str__(self):
José Fonseca02c25002011-10-15 13:17:26 +010065 """Return the C/C++ type expression for this type."""
José Fonseca6fac5ae2010-11-29 16:09:13 +000066 return self.expr
67
68 def visit(self, visitor, *args, **kwargs):
69 raise NotImplementedError
70
José Fonseca0075f152012-04-14 20:25:52 +010071 def mutable(self):
72 '''Return a mutable version of this type.
73
74 Convenience wrapper around MutableRebuilder.'''
75 visitor = MutableRebuilder()
76 return visitor.visit(self)
José Fonseca6fac5ae2010-11-29 16:09:13 +000077
78
79class _Void(Type):
José Fonseca02c25002011-10-15 13:17:26 +010080 """Singleton void type."""
José Fonseca6fac5ae2010-11-29 16:09:13 +000081
82 def __init__(self):
83 Type.__init__(self, "void")
84
85 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +000086 return visitor.visitVoid(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +000087
88Void = _Void()
89
90
91class Literal(Type):
José Fonseca2f2ea482011-10-15 15:10:06 +010092 """Class to describe literal types.
José Fonseca6fac5ae2010-11-29 16:09:13 +000093
José Fonseca2f2ea482011-10-15 15:10:06 +010094 Types which are not defined in terms of other types, such as integers and
95 floats."""
96
97 def __init__(self, expr, kind):
José Fonseca6fac5ae2010-11-29 16:09:13 +000098 Type.__init__(self, expr)
José Fonseca2f2ea482011-10-15 15:10:06 +010099 self.kind = kind
José Fonseca6fac5ae2010-11-29 16:09:13 +0000100
101 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000102 return visitor.visitLiteral(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000103
104
José Fonsecabcfc81b2012-08-07 21:07:22 +0100105Bool = Literal("bool", "Bool")
106SChar = Literal("signed char", "SInt")
107UChar = Literal("unsigned char", "UInt")
108Short = Literal("short", "SInt")
109Int = Literal("int", "SInt")
110Long = Literal("long", "SInt")
111LongLong = Literal("long long", "SInt")
112UShort = Literal("unsigned short", "UInt")
113UInt = Literal("unsigned int", "UInt")
114ULong = Literal("unsigned long", "UInt")
115ULongLong = Literal("unsigned long long", "UInt")
116Float = Literal("float", "Float")
117Double = Literal("double", "Double")
118SizeT = Literal("size_t", "UInt")
119
120Char = Literal("char", "SInt")
121WChar = Literal("wchar_t", "SInt")
122
123Int8 = Literal("int8_t", "SInt")
124UInt8 = Literal("uint8_t", "UInt")
125Int16 = Literal("int16_t", "SInt")
126UInt16 = Literal("uint16_t", "UInt")
127Int32 = Literal("int32_t", "SInt")
128UInt32 = Literal("uint32_t", "UInt")
129Int64 = Literal("int64_t", "SInt")
130UInt64 = Literal("uint64_t", "UInt")
131
José Fonsecacb9d2e02012-10-19 14:51:48 +0100132IntPtr = Literal("intptr_t", "SInt")
133UIntPtr = Literal("uintptr_t", "UInt")
José Fonsecabcfc81b2012-08-07 21:07:22 +0100134
José Fonseca6fac5ae2010-11-29 16:09:13 +0000135class Const(Type):
136
137 def __init__(self, type):
José Fonseca903c2ca2011-09-23 09:43:05 +0100138 # While "const foo" and "foo const" are synonymous, "const foo *" and
139 # "foo * const" are not quite the same, and some compilers do enforce
140 # strict const correctness.
José Fonsecabcfc81b2012-08-07 21:07:22 +0100141 if type.expr.startswith("const ") or '*' in type.expr:
José Fonseca6fac5ae2010-11-29 16:09:13 +0000142 expr = type.expr + " const"
143 else:
José Fonseca903c2ca2011-09-23 09:43:05 +0100144 # The most legible
José Fonseca6fac5ae2010-11-29 16:09:13 +0000145 expr = "const " + type.expr
146
José Fonseca02c25002011-10-15 13:17:26 +0100147 Type.__init__(self, expr, 'C' + type.tag)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000148
149 self.type = type
150
151 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000152 return visitor.visitConst(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000153
154
155class Pointer(Type):
156
157 def __init__(self, type):
José Fonseca02c25002011-10-15 13:17:26 +0100158 Type.__init__(self, type.expr + " *", 'P' + type.tag)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000159 self.type = type
160
161 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000162 return visitor.visitPointer(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000163
164
José Fonseca59ee88e2012-01-15 14:24:10 +0000165class IntPointer(Type):
166 '''Integer encoded as a pointer.'''
167
168 def visit(self, visitor, *args, **kwargs):
169 return visitor.visitIntPointer(self, *args, **kwargs)
170
171
José Fonsecafbcf6832012-04-05 07:10:30 +0100172class ObjPointer(Type):
173 '''Pointer to an object.'''
174
175 def __init__(self, type):
176 Type.__init__(self, type.expr + " *", 'P' + type.tag)
177 self.type = type
178
179 def visit(self, visitor, *args, **kwargs):
180 return visitor.visitObjPointer(self, *args, **kwargs)
181
182
José Fonseca59ee88e2012-01-15 14:24:10 +0000183class LinearPointer(Type):
José Fonsecafbcf6832012-04-05 07:10:30 +0100184 '''Pointer to a linear range of memory.'''
José Fonseca59ee88e2012-01-15 14:24:10 +0000185
186 def __init__(self, type, size = None):
187 Type.__init__(self, type.expr + " *", 'P' + type.tag)
188 self.type = type
189 self.size = size
190
191 def visit(self, visitor, *args, **kwargs):
192 return visitor.visitLinearPointer(self, *args, **kwargs)
193
194
José Fonsecab89c5932012-04-01 22:47:11 +0200195class Reference(Type):
196 '''C++ references.'''
197
198 def __init__(self, type):
199 Type.__init__(self, type.expr + " &", 'R' + type.tag)
200 self.type = type
201
202 def visit(self, visitor, *args, **kwargs):
203 return visitor.visitReference(self, *args, **kwargs)
204
205
José Fonseca6fac5ae2010-11-29 16:09:13 +0000206class Handle(Type):
207
José Fonseca8a844ae2010-12-06 18:50:52 +0000208 def __init__(self, name, type, range=None, key=None):
José Fonseca02c25002011-10-15 13:17:26 +0100209 Type.__init__(self, type.expr, 'P' + type.tag)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000210 self.name = name
211 self.type = type
212 self.range = range
José Fonseca8a844ae2010-12-06 18:50:52 +0000213 self.key = key
José Fonseca6fac5ae2010-11-29 16:09:13 +0000214
215 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000216 return visitor.visitHandle(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000217
218
219def ConstPointer(type):
220 return Pointer(Const(type))
221
222
223class Enum(Type):
224
José Fonseca02c25002011-10-15 13:17:26 +0100225 __id = 0
226
José Fonseca6fac5ae2010-11-29 16:09:13 +0000227 def __init__(self, name, values):
228 Type.__init__(self, name)
José Fonseca02c25002011-10-15 13:17:26 +0100229
230 self.id = Enum.__id
231 Enum.__id += 1
232
José Fonseca6fac5ae2010-11-29 16:09:13 +0000233 self.values = list(values)
José Fonseca02c25002011-10-15 13:17:26 +0100234
José Fonseca6fac5ae2010-11-29 16:09:13 +0000235 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000236 return visitor.visitEnum(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000237
238
239def FakeEnum(type, values):
240 return Enum(type.expr, values)
241
242
243class Bitmask(Type):
244
José Fonseca02c25002011-10-15 13:17:26 +0100245 __id = 0
246
José Fonseca6fac5ae2010-11-29 16:09:13 +0000247 def __init__(self, type, values):
248 Type.__init__(self, type.expr)
José Fonseca02c25002011-10-15 13:17:26 +0100249
250 self.id = Bitmask.__id
251 Bitmask.__id += 1
252
José Fonseca6fac5ae2010-11-29 16:09:13 +0000253 self.type = type
254 self.values = values
255
256 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000257 return visitor.visitBitmask(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000258
259Flags = Bitmask
260
261
262class Array(Type):
263
264 def __init__(self, type, length):
265 Type.__init__(self, type.expr + " *")
266 self.type = type
267 self.length = length
268
269 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000270 return visitor.visitArray(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000271
272
Andreas Hartmetzba7bb0d2013-07-07 22:51:12 +0200273class AttribArray(Type):
274
Andreas Hartmetz7a0de292013-07-09 22:38:29 +0200275 def __init__(self, keyType, valueTypes, isConst = True, terminator = '0'):
Andreas Hartmetzb936e552013-07-08 12:36:11 +0200276 if isConst:
277 Type.__init__(self, (Pointer(Const(Int))).expr)
278 else:
279 Type.__init__(self, (Pointer(Int)).expr)
Andreas Hartmetzba7bb0d2013-07-07 22:51:12 +0200280 self.type = (Pointer(Const(Int))) # for function prototypes and such
281 self.keyType = keyType
282 self.valueTypes = valueTypes
Andreas Hartmetz7a0de292013-07-09 22:38:29 +0200283 self.terminator = terminator
284 self.hasKeysWithoutValues = False
285 for key, value in valueTypes:
286 if value is None:
287 self.hasKeysWithoutValues = True
Andreas Hartmetzba7bb0d2013-07-07 22:51:12 +0200288
289 def visit(self, visitor, *args, **kwargs):
290 return visitor.visitAttribArray(self, *args, **kwargs)
291
292
José Fonseca6fac5ae2010-11-29 16:09:13 +0000293class Blob(Type):
294
295 def __init__(self, type, size):
296 Type.__init__(self, type.expr + ' *')
297 self.type = type
298 self.size = size
299
300 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000301 return visitor.visitBlob(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000302
303
304class Struct(Type):
305
José Fonseca02c25002011-10-15 13:17:26 +0100306 __id = 0
307
José Fonseca6fac5ae2010-11-29 16:09:13 +0000308 def __init__(self, name, members):
309 Type.__init__(self, name)
José Fonseca02c25002011-10-15 13:17:26 +0100310
311 self.id = Struct.__id
312 Struct.__id += 1
313
José Fonseca6fac5ae2010-11-29 16:09:13 +0000314 self.name = name
José Fonsecadbf714b2012-11-20 17:03:43 +0000315 self.members = members
José Fonseca6fac5ae2010-11-29 16:09:13 +0000316
317 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000318 return visitor.visitStruct(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000319
320
José Fonsecadbf714b2012-11-20 17:03:43 +0000321def Union(kindExpr, kindTypes, contextLess=True):
José Fonsecaeb216e62012-11-20 11:08:08 +0000322 switchTypes = []
323 for kindCase, kindType, kindMemberName in kindTypes:
324 switchType = Struct(None, [(kindType, kindMemberName)])
325 switchTypes.append((kindCase, switchType))
326 return Polymorphic(kindExpr, switchTypes, contextLess=contextLess)
327
José Fonseca5b6fb752012-04-14 14:56:45 +0100328
José Fonseca6fac5ae2010-11-29 16:09:13 +0000329class Alias(Type):
330
331 def __init__(self, expr, type):
332 Type.__init__(self, expr)
333 self.type = type
334
335 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000336 return visitor.visitAlias(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000337
José Fonseca6fac5ae2010-11-29 16:09:13 +0000338class Arg:
339
José Fonseca9dd8f702012-04-07 10:42:50 +0100340 def __init__(self, type, name, input=True, output=False):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000341 self.type = type
342 self.name = name
José Fonseca9dd8f702012-04-07 10:42:50 +0100343 self.input = input
José Fonseca6fac5ae2010-11-29 16:09:13 +0000344 self.output = output
345 self.index = None
346
347 def __str__(self):
348 return '%s %s' % (self.type, self.name)
349
350
José Fonseca9dd8f702012-04-07 10:42:50 +0100351def In(type, name):
352 return Arg(type, name, input=True, output=False)
353
354def Out(type, name):
355 return Arg(type, name, input=False, output=True)
356
357def InOut(type, name):
358 return Arg(type, name, input=True, output=True)
359
360
José Fonseca6fac5ae2010-11-29 16:09:13 +0000361class Function:
362
José Fonseca84cea3b2012-05-09 21:12:30 +0100363 def __init__(self, type, name, args, call = '', fail = None, sideeffects=True, internal=False):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000364 self.type = type
365 self.name = name
366
367 self.args = []
368 index = 0
369 for arg in args:
José Fonseca8384ccb2011-05-25 10:12:02 +0100370 if not isinstance(arg, Arg):
371 if isinstance(arg, tuple):
372 arg_type, arg_name = arg
373 else:
374 arg_type = arg
375 arg_name = "arg%u" % index
José Fonseca6fac5ae2010-11-29 16:09:13 +0000376 arg = Arg(arg_type, arg_name)
377 arg.index = index
378 index += 1
379 self.args.append(arg)
380
381 self.call = call
382 self.fail = fail
383 self.sideeffects = sideeffects
José Fonseca84cea3b2012-05-09 21:12:30 +0100384 self.internal = internal
José Fonseca6fac5ae2010-11-29 16:09:13 +0000385
386 def prototype(self, name=None):
387 if name is not None:
388 name = name.strip()
389 else:
390 name = self.name
391 s = name
392 if self.call:
393 s = self.call + ' ' + s
394 if name.startswith('*'):
395 s = '(' + s + ')'
396 s = self.type.expr + ' ' + s
397 s += "("
398 if self.args:
399 s += ", ".join(["%s %s" % (arg.type, arg.name) for arg in self.args])
400 else:
401 s += "void"
402 s += ")"
403 return s
404
José Fonseca568ecc22012-01-15 13:57:03 +0000405 def argNames(self):
406 return [arg.name for arg in self.args]
407
José Fonseca999284f2013-02-19 13:29:26 +0000408 def getArgByName(self, name):
409 for arg in self.args:
410 if arg.name == name:
411 return arg
412 return None
413
José Fonseca6fac5ae2010-11-29 16:09:13 +0000414
415def StdFunction(*args, **kwargs):
416 kwargs.setdefault('call', '__stdcall')
417 return Function(*args, **kwargs)
418
419
420def FunctionPointer(type, name, args, **kwargs):
421 # XXX: We should probably treat function pointers (callbacks or not) in a generic fashion
422 return Opaque(name)
423
424
425class Interface(Type):
426
427 def __init__(self, name, base=None):
428 Type.__init__(self, name)
429 self.name = name
430 self.base = base
431 self.methods = []
432
433 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000434 return visitor.visitInterface(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000435
José Fonsecabd086342012-04-18 19:58:32 +0100436 def getMethodByName(self, name):
José Fonsecad275d0a2012-04-30 23:18:05 +0100437 for method in self.iterMethods():
438 if method.name == name:
439 return method
José Fonsecabd086342012-04-18 19:58:32 +0100440 return None
441
José Fonseca54f304a2012-01-14 19:33:08 +0000442 def iterMethods(self):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000443 if self.base is not None:
José Fonseca54f304a2012-01-14 19:33:08 +0000444 for method in self.base.iterMethods():
José Fonseca6fac5ae2010-11-29 16:09:13 +0000445 yield method
446 for method in self.methods:
447 yield method
448 raise StopIteration
449
José Fonseca143e9252012-04-15 09:31:18 +0100450 def iterBases(self):
451 iface = self
452 while iface is not None:
453 yield iface
454 iface = iface.base
455 raise StopIteration
456
José Fonseca5abf5602013-05-30 14:00:44 +0100457 def hasBase(self, *bases):
458 for iface in self.iterBases():
459 if iface in bases:
460 return True
461 return False
462
José Fonseca4220b1b2012-02-03 19:05:29 +0000463 def iterBaseMethods(self):
464 if self.base is not None:
465 for iface, method in self.base.iterBaseMethods():
466 yield iface, method
467 for method in self.methods:
468 yield self, method
469 raise StopIteration
470
José Fonseca6fac5ae2010-11-29 16:09:13 +0000471
472class Method(Function):
473
José Fonseca43aa19f2012-11-10 09:29:38 +0000474 def __init__(self, type, name, args, call = '', const=False, sideeffects=True):
475 assert call == '__stdcall'
José Fonseca5b6fb752012-04-14 14:56:45 +0100476 Function.__init__(self, type, name, args, call = call, sideeffects=sideeffects)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000477 for index in range(len(self.args)):
478 self.args[index].index = index + 1
José Fonseca9dbeda62012-02-03 19:05:54 +0000479 self.const = const
480
481 def prototype(self, name=None):
482 s = Function.prototype(self, name)
483 if self.const:
484 s += ' const'
485 return s
José Fonseca6fac5ae2010-11-29 16:09:13 +0000486
José Fonsecabcb26b22012-04-15 08:42:25 +0100487
José Fonseca5b6fb752012-04-14 14:56:45 +0100488def StdMethod(*args, **kwargs):
489 kwargs.setdefault('call', '__stdcall')
490 return Method(*args, **kwargs)
491
José Fonseca6fac5ae2010-11-29 16:09:13 +0000492
José Fonseca6fac5ae2010-11-29 16:09:13 +0000493class String(Type):
José Fonsecabcfc81b2012-08-07 21:07:22 +0100494 '''Human-legible character string.'''
José Fonseca6fac5ae2010-11-29 16:09:13 +0000495
José Fonsecabcfc81b2012-08-07 21:07:22 +0100496 def __init__(self, type = Char, length = None, wide = False):
497 assert isinstance(type, Type)
498 Type.__init__(self, type.expr + ' *')
499 self.type = type
José Fonseca6fac5ae2010-11-29 16:09:13 +0000500 self.length = length
José Fonsecabcfc81b2012-08-07 21:07:22 +0100501 self.wide = wide
José Fonseca6fac5ae2010-11-29 16:09:13 +0000502
503 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000504 return visitor.visitString(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000505
José Fonseca6fac5ae2010-11-29 16:09:13 +0000506
507class Opaque(Type):
508 '''Opaque pointer.'''
509
510 def __init__(self, expr):
511 Type.__init__(self, expr)
512
513 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000514 return visitor.visitOpaque(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000515
516
517def OpaquePointer(type, *args):
518 return Opaque(type.expr + ' *')
519
520def OpaqueArray(type, size):
521 return Opaque(type.expr + ' *')
522
523def OpaqueBlob(type, size):
524 return Opaque(type.expr + ' *')
José Fonseca8a56d142008-07-09 12:18:08 +0900525
José Fonseca501f2862010-11-19 20:41:18 +0000526
José Fonseca16d46dd2011-10-13 09:52:52 +0100527class Polymorphic(Type):
528
José Fonsecaeb216e62012-11-20 11:08:08 +0000529 def __init__(self, switchExpr, switchTypes, defaultType=None, contextLess=True):
530 if defaultType is None:
531 Type.__init__(self, None)
532 contextLess = False
533 else:
534 Type.__init__(self, defaultType.expr)
José Fonseca54f304a2012-01-14 19:33:08 +0000535 self.switchExpr = switchExpr
536 self.switchTypes = switchTypes
José Fonsecab95e3722012-04-16 14:01:15 +0100537 self.defaultType = defaultType
538 self.contextLess = contextLess
José Fonseca16d46dd2011-10-13 09:52:52 +0100539
540 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000541 return visitor.visitPolymorphic(self, *args, **kwargs)
José Fonseca16d46dd2011-10-13 09:52:52 +0100542
José Fonseca54f304a2012-01-14 19:33:08 +0000543 def iterSwitch(self):
José Fonsecaeb216e62012-11-20 11:08:08 +0000544 cases = []
545 types = []
546
547 if self.defaultType is not None:
548 cases.append(['default'])
549 types.append(self.defaultType)
José Fonseca46161112011-10-14 10:04:55 +0100550
José Fonseca54f304a2012-01-14 19:33:08 +0000551 for expr, type in self.switchTypes:
José Fonseca46161112011-10-14 10:04:55 +0100552 case = 'case %s' % expr
553 try:
554 i = types.index(type)
555 except ValueError:
556 cases.append([case])
557 types.append(type)
558 else:
559 cases[i].append(case)
560
561 return zip(cases, types)
562
José Fonseca16d46dd2011-10-13 09:52:52 +0100563
José Fonsecab95e3722012-04-16 14:01:15 +0100564def EnumPolymorphic(enumName, switchExpr, switchTypes, defaultType, contextLess=True):
565 enumValues = [expr for expr, type in switchTypes]
566 enum = Enum(enumName, enumValues)
567 polymorphic = Polymorphic(switchExpr, switchTypes, defaultType, contextLess)
568 return enum, polymorphic
569
570
José Fonseca501f2862010-11-19 20:41:18 +0000571class Visitor:
José Fonseca9c4a2572012-01-13 23:21:10 +0000572 '''Abstract visitor for the type hierarchy.'''
José Fonseca501f2862010-11-19 20:41:18 +0000573
574 def visit(self, type, *args, **kwargs):
575 return type.visit(self, *args, **kwargs)
576
José Fonseca54f304a2012-01-14 19:33:08 +0000577 def visitVoid(self, void, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000578 raise NotImplementedError
579
José Fonseca54f304a2012-01-14 19:33:08 +0000580 def visitLiteral(self, literal, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000581 raise NotImplementedError
582
José Fonseca54f304a2012-01-14 19:33:08 +0000583 def visitString(self, string, *args, **kwargs):
José Fonseca2defc982010-11-22 16:59:10 +0000584 raise NotImplementedError
585
José Fonseca54f304a2012-01-14 19:33:08 +0000586 def visitConst(self, const, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000587 raise NotImplementedError
588
José Fonseca54f304a2012-01-14 19:33:08 +0000589 def visitStruct(self, struct, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000590 raise NotImplementedError
591
José Fonseca54f304a2012-01-14 19:33:08 +0000592 def visitArray(self, array, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000593 raise NotImplementedError
594
Andreas Hartmetzba7bb0d2013-07-07 22:51:12 +0200595 def visitAttribArray(self, array, *args, **kwargs):
596 raise NotImplementedError
597
José Fonseca54f304a2012-01-14 19:33:08 +0000598 def visitBlob(self, blob, *args, **kwargs):
José Fonseca885f2652010-11-20 11:22:25 +0000599 raise NotImplementedError
600
José Fonseca54f304a2012-01-14 19:33:08 +0000601 def visitEnum(self, enum, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000602 raise NotImplementedError
603
José Fonseca54f304a2012-01-14 19:33:08 +0000604 def visitBitmask(self, bitmask, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000605 raise NotImplementedError
606
José Fonseca54f304a2012-01-14 19:33:08 +0000607 def visitPointer(self, pointer, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000608 raise NotImplementedError
609
José Fonseca59ee88e2012-01-15 14:24:10 +0000610 def visitIntPointer(self, pointer, *args, **kwargs):
611 raise NotImplementedError
612
José Fonsecafbcf6832012-04-05 07:10:30 +0100613 def visitObjPointer(self, pointer, *args, **kwargs):
614 raise NotImplementedError
615
José Fonseca59ee88e2012-01-15 14:24:10 +0000616 def visitLinearPointer(self, pointer, *args, **kwargs):
617 raise NotImplementedError
618
José Fonsecab89c5932012-04-01 22:47:11 +0200619 def visitReference(self, reference, *args, **kwargs):
620 raise NotImplementedError
621
José Fonseca54f304a2012-01-14 19:33:08 +0000622 def visitHandle(self, handle, *args, **kwargs):
José Fonseca50d78d82010-11-23 22:13:14 +0000623 raise NotImplementedError
624
José Fonseca54f304a2012-01-14 19:33:08 +0000625 def visitAlias(self, alias, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000626 raise NotImplementedError
627
José Fonseca54f304a2012-01-14 19:33:08 +0000628 def visitOpaque(self, opaque, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000629 raise NotImplementedError
630
José Fonseca54f304a2012-01-14 19:33:08 +0000631 def visitInterface(self, interface, *args, **kwargs):
José Fonsecac356d6a2010-11-23 14:27:25 +0000632 raise NotImplementedError
633
José Fonseca54f304a2012-01-14 19:33:08 +0000634 def visitPolymorphic(self, polymorphic, *args, **kwargs):
José Fonseca16d46dd2011-10-13 09:52:52 +0100635 raise NotImplementedError
José Fonseca54f304a2012-01-14 19:33:08 +0000636 #return self.visit(polymorphic.defaultType, *args, **kwargs)
José Fonseca16d46dd2011-10-13 09:52:52 +0100637
José Fonsecac356d6a2010-11-23 14:27:25 +0000638
639class OnceVisitor(Visitor):
José Fonseca9c4a2572012-01-13 23:21:10 +0000640 '''Visitor that guarantees that each type is visited only once.'''
José Fonsecac356d6a2010-11-23 14:27:25 +0000641
642 def __init__(self):
643 self.__visited = set()
644
645 def visit(self, type, *args, **kwargs):
646 if type not in self.__visited:
647 self.__visited.add(type)
648 return type.visit(self, *args, **kwargs)
649 return None
650
José Fonseca501f2862010-11-19 20:41:18 +0000651
José Fonsecac9edb832010-11-20 09:03:10 +0000652class Rebuilder(Visitor):
José Fonseca9c4a2572012-01-13 23:21:10 +0000653 '''Visitor which rebuild types as it visits them.
654
655 By itself it is a no-op -- it is intended to be overwritten.
656 '''
José Fonsecac9edb832010-11-20 09:03:10 +0000657
José Fonseca54f304a2012-01-14 19:33:08 +0000658 def visitVoid(self, void):
José Fonsecac9edb832010-11-20 09:03:10 +0000659 return void
660
José Fonseca54f304a2012-01-14 19:33:08 +0000661 def visitLiteral(self, literal):
José Fonsecac9edb832010-11-20 09:03:10 +0000662 return literal
663
José Fonseca54f304a2012-01-14 19:33:08 +0000664 def visitString(self, string):
José Fonsecabcfc81b2012-08-07 21:07:22 +0100665 string_type = self.visit(string.type)
666 if string_type is string.type:
667 return string
668 else:
669 return String(string_type, string.length, string.wide)
José Fonseca2defc982010-11-22 16:59:10 +0000670
José Fonseca54f304a2012-01-14 19:33:08 +0000671 def visitConst(self, const):
José Fonsecaf182eda2012-04-05 19:59:56 +0100672 const_type = self.visit(const.type)
673 if const_type is const.type:
674 return const
675 else:
676 return Const(const_type)
José Fonsecac9edb832010-11-20 09:03:10 +0000677
José Fonseca54f304a2012-01-14 19:33:08 +0000678 def visitStruct(self, struct):
José Fonseca06aa2842011-05-05 07:55:54 +0100679 members = [(self.visit(type), name) for type, name in struct.members]
José Fonsecac9edb832010-11-20 09:03:10 +0000680 return Struct(struct.name, members)
681
José Fonseca54f304a2012-01-14 19:33:08 +0000682 def visitArray(self, array):
José Fonsecac9edb832010-11-20 09:03:10 +0000683 type = self.visit(array.type)
684 return Array(type, array.length)
685
José Fonseca54f304a2012-01-14 19:33:08 +0000686 def visitBlob(self, blob):
José Fonseca885f2652010-11-20 11:22:25 +0000687 type = self.visit(blob.type)
688 return Blob(type, blob.size)
689
José Fonseca54f304a2012-01-14 19:33:08 +0000690 def visitEnum(self, enum):
José Fonsecac9edb832010-11-20 09:03:10 +0000691 return enum
692
José Fonseca54f304a2012-01-14 19:33:08 +0000693 def visitBitmask(self, bitmask):
José Fonsecac9edb832010-11-20 09:03:10 +0000694 type = self.visit(bitmask.type)
695 return Bitmask(type, bitmask.values)
696
José Fonseca54f304a2012-01-14 19:33:08 +0000697 def visitPointer(self, pointer):
José Fonsecaf182eda2012-04-05 19:59:56 +0100698 pointer_type = self.visit(pointer.type)
699 if pointer_type is pointer.type:
700 return pointer
701 else:
702 return Pointer(pointer_type)
José Fonsecac9edb832010-11-20 09:03:10 +0000703
José Fonseca59ee88e2012-01-15 14:24:10 +0000704 def visitIntPointer(self, pointer):
705 return pointer
706
José Fonsecafbcf6832012-04-05 07:10:30 +0100707 def visitObjPointer(self, pointer):
José Fonsecaf182eda2012-04-05 19:59:56 +0100708 pointer_type = self.visit(pointer.type)
709 if pointer_type is pointer.type:
710 return pointer
711 else:
712 return ObjPointer(pointer_type)
José Fonsecafbcf6832012-04-05 07:10:30 +0100713
José Fonseca59ee88e2012-01-15 14:24:10 +0000714 def visitLinearPointer(self, pointer):
José Fonsecaf182eda2012-04-05 19:59:56 +0100715 pointer_type = self.visit(pointer.type)
716 if pointer_type is pointer.type:
717 return pointer
718 else:
719 return LinearPointer(pointer_type)
José Fonseca59ee88e2012-01-15 14:24:10 +0000720
José Fonsecab89c5932012-04-01 22:47:11 +0200721 def visitReference(self, reference):
José Fonsecaf182eda2012-04-05 19:59:56 +0100722 reference_type = self.visit(reference.type)
723 if reference_type is reference.type:
724 return reference
725 else:
726 return Reference(reference_type)
José Fonsecab89c5932012-04-01 22:47:11 +0200727
José Fonseca54f304a2012-01-14 19:33:08 +0000728 def visitHandle(self, handle):
José Fonsecaf182eda2012-04-05 19:59:56 +0100729 handle_type = self.visit(handle.type)
730 if handle_type is handle.type:
731 return handle
732 else:
733 return Handle(handle.name, handle_type, range=handle.range, key=handle.key)
José Fonseca50d78d82010-11-23 22:13:14 +0000734
José Fonseca54f304a2012-01-14 19:33:08 +0000735 def visitAlias(self, alias):
José Fonsecaf182eda2012-04-05 19:59:56 +0100736 alias_type = self.visit(alias.type)
737 if alias_type is alias.type:
738 return alias
739 else:
740 return Alias(alias.expr, alias_type)
José Fonsecac9edb832010-11-20 09:03:10 +0000741
José Fonseca54f304a2012-01-14 19:33:08 +0000742 def visitOpaque(self, opaque):
José Fonsecac9edb832010-11-20 09:03:10 +0000743 return opaque
744
José Fonseca7814edf2012-01-31 10:55:49 +0000745 def visitInterface(self, interface, *args, **kwargs):
746 return interface
747
José Fonseca54f304a2012-01-14 19:33:08 +0000748 def visitPolymorphic(self, polymorphic):
José Fonseca54f304a2012-01-14 19:33:08 +0000749 switchExpr = polymorphic.switchExpr
750 switchTypes = [(expr, self.visit(type)) for expr, type in polymorphic.switchTypes]
José Fonsecaeb216e62012-11-20 11:08:08 +0000751 if polymorphic.defaultType is None:
752 defaultType = None
753 else:
754 defaultType = self.visit(polymorphic.defaultType)
José Fonsecab95e3722012-04-16 14:01:15 +0100755 return Polymorphic(switchExpr, switchTypes, defaultType, polymorphic.contextLess)
José Fonseca16d46dd2011-10-13 09:52:52 +0100756
José Fonsecac9edb832010-11-20 09:03:10 +0000757
José Fonseca0075f152012-04-14 20:25:52 +0100758class MutableRebuilder(Rebuilder):
759 '''Type visitor which derives a mutable type.'''
760
José Fonsecabcfc81b2012-08-07 21:07:22 +0100761 def visitString(self, string):
762 return string
763
José Fonseca0075f152012-04-14 20:25:52 +0100764 def visitConst(self, const):
765 # Strip out const qualifier
766 return const.type
767
768 def visitAlias(self, alias):
769 # Tear the alias on type changes
770 type = self.visit(alias.type)
771 if type is alias.type:
772 return alias
773 return type
774
775 def visitReference(self, reference):
776 # Strip out references
777 return reference.type
778
779
780class Traverser(Visitor):
781 '''Visitor which all types.'''
782
783 def visitVoid(self, void, *args, **kwargs):
784 pass
785
786 def visitLiteral(self, literal, *args, **kwargs):
787 pass
788
789 def visitString(self, string, *args, **kwargs):
790 pass
791
792 def visitConst(self, const, *args, **kwargs):
793 self.visit(const.type, *args, **kwargs)
794
795 def visitStruct(self, struct, *args, **kwargs):
796 for type, name in struct.members:
797 self.visit(type, *args, **kwargs)
798
799 def visitArray(self, array, *args, **kwargs):
800 self.visit(array.type, *args, **kwargs)
801
Andreas Hartmetzba7bb0d2013-07-07 22:51:12 +0200802 def visitAttribArray(self, attribs, *args, **kwargs):
803 for key, valueType in attribs.valueTypes:
Andreas Hartmetz7a0de292013-07-09 22:38:29 +0200804 if valueType is not None:
805 self.visit(valueType, *args, **kwargs)
Andreas Hartmetzba7bb0d2013-07-07 22:51:12 +0200806
José Fonseca0075f152012-04-14 20:25:52 +0100807 def visitBlob(self, array, *args, **kwargs):
808 pass
809
810 def visitEnum(self, enum, *args, **kwargs):
811 pass
812
813 def visitBitmask(self, bitmask, *args, **kwargs):
814 self.visit(bitmask.type, *args, **kwargs)
815
816 def visitPointer(self, pointer, *args, **kwargs):
817 self.visit(pointer.type, *args, **kwargs)
818
819 def visitIntPointer(self, pointer, *args, **kwargs):
820 pass
821
822 def visitObjPointer(self, pointer, *args, **kwargs):
823 self.visit(pointer.type, *args, **kwargs)
824
825 def visitLinearPointer(self, pointer, *args, **kwargs):
826 self.visit(pointer.type, *args, **kwargs)
827
828 def visitReference(self, reference, *args, **kwargs):
829 self.visit(reference.type, *args, **kwargs)
830
831 def visitHandle(self, handle, *args, **kwargs):
832 self.visit(handle.type, *args, **kwargs)
833
834 def visitAlias(self, alias, *args, **kwargs):
835 self.visit(alias.type, *args, **kwargs)
836
837 def visitOpaque(self, opaque, *args, **kwargs):
838 pass
839
840 def visitInterface(self, interface, *args, **kwargs):
841 if interface.base is not None:
842 self.visit(interface.base, *args, **kwargs)
843 for method in interface.iterMethods():
844 for arg in method.args:
845 self.visit(arg.type, *args, **kwargs)
846 self.visit(method.type, *args, **kwargs)
847
848 def visitPolymorphic(self, polymorphic, *args, **kwargs):
José Fonseca0075f152012-04-14 20:25:52 +0100849 for expr, type in polymorphic.switchTypes:
850 self.visit(type, *args, **kwargs)
José Fonsecaeb216e62012-11-20 11:08:08 +0000851 if polymorphic.defaultType is not None:
852 self.visit(polymorphic.defaultType, *args, **kwargs)
José Fonseca0075f152012-04-14 20:25:52 +0100853
854
855class Collector(Traverser):
José Fonseca9c4a2572012-01-13 23:21:10 +0000856 '''Visitor which collects all unique types as it traverses them.'''
José Fonsecae6a50bd2010-11-24 10:12:22 +0000857
858 def __init__(self):
859 self.__visited = set()
860 self.types = []
861
862 def visit(self, type):
863 if type in self.__visited:
864 return
865 self.__visited.add(type)
866 Visitor.visit(self, type)
867 self.types.append(type)
868
José Fonseca16d46dd2011-10-13 09:52:52 +0100869
José Fonsecadbf714b2012-11-20 17:03:43 +0000870class ExpanderMixin:
871 '''Mixin class that provides a bunch of methods to expand C expressions
872 from the specifications.'''
873
874 __structs = None
875 __indices = None
876
877 def expand(self, expr):
878 # Expand a C expression, replacing certain variables
879 if not isinstance(expr, basestring):
880 return expr
881 variables = {}
882
883 if self.__structs is not None:
884 variables['self'] = '(%s)' % self.__structs[0]
885 if self.__indices is not None:
886 variables['i'] = self.__indices[0]
887
888 expandedExpr = expr.format(**variables)
889 if expandedExpr != expr and 0:
890 sys.stderr.write(" %r -> %r\n" % (expr, expandedExpr))
891 return expandedExpr
892
893 def visitMember(self, member, structInstance, *args, **kwargs):
894 memberType, memberName = member
895 if memberName is None:
896 # Anonymous structure/union member
897 memberInstance = structInstance
898 else:
899 memberInstance = '(%s).%s' % (structInstance, memberName)
900 self.__structs = (structInstance, self.__structs)
901 try:
902 return self.visit(memberType, memberInstance, *args, **kwargs)
903 finally:
904 _, self.__structs = self.__structs
905
906 def visitElement(self, elementIndex, elementType, *args, **kwargs):
907 self.__indices = (elementIndex, self.__indices)
908 try:
909 return self.visit(elementType, *args, **kwargs)
910 finally:
911 _, self.__indices = self.__indices
912
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000913
José Fonseca81301932012-11-11 00:10:20 +0000914class Module:
915 '''A collection of functions.'''
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000916
José Fonseca68ec4122011-02-20 11:25:25 +0000917 def __init__(self, name = None):
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000918 self.name = name
919 self.headers = []
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000920 self.functions = []
921 self.interfaces = []
922
José Fonseca54f304a2012-01-14 19:33:08 +0000923 def addFunctions(self, functions):
José Fonseca81301932012-11-11 00:10:20 +0000924 self.functions.extend(functions)
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000925
José Fonseca54f304a2012-01-14 19:33:08 +0000926 def addInterfaces(self, interfaces):
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000927 self.interfaces.extend(interfaces)
928
José Fonseca81301932012-11-11 00:10:20 +0000929 def mergeModule(self, module):
930 self.headers.extend(module.headers)
931 self.functions.extend(module.functions)
932 self.interfaces.extend(module.interfaces)
José Fonseca68ec4122011-02-20 11:25:25 +0000933
José Fonseca1b6c8752012-04-15 14:33:00 +0100934 def getFunctionByName(self, name):
José Fonsecaeccec3e2011-02-20 09:01:25 +0000935 for function in self.functions:
936 if function.name == name:
937 return function
938 return None
939
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000940
José Fonseca81301932012-11-11 00:10:20 +0000941class API:
942 '''API abstraction.
943
944 Essentially, a collection of types, functions, and interfaces.
945 '''
946
947 def __init__(self, modules = None):
948 self.modules = []
949 if modules is not None:
950 self.modules.extend(modules)
951
952 def getAllTypes(self):
953 collector = Collector()
954 for module in self.modules:
955 for function in module.functions:
956 for arg in function.args:
957 collector.visit(arg.type)
958 collector.visit(function.type)
959 for interface in module.interfaces:
960 collector.visit(interface)
961 for method in interface.iterMethods():
962 for arg in method.args:
963 collector.visit(arg.type)
964 collector.visit(method.type)
965 return collector.types
966
967 def getAllFunctions(self):
968 functions = []
969 for module in self.modules:
970 functions.extend(module.functions)
971 return functions
972
973 def getAllInterfaces(self):
974 types = self.getAllTypes()
975 interfaces = [type for type in types if isinstance(type, Interface)]
976 for module in self.modules:
977 for interface in module.interfaces:
978 if interface not in interfaces:
979 interfaces.append(interface)
980 return interfaces
981
982 def addModule(self, module):
983 self.modules.append(module)
984
985 def getFunctionByName(self, name):
986 for module in self.modules:
987 for function in module.functions:
988 if function.name == name:
989 return function
990 return None
991
992
José Fonseca280a1762012-01-31 15:10:13 +0000993# C string (i.e., zero terminated)
José Fonsecabcfc81b2012-08-07 21:07:22 +0100994CString = String(Char)
995WString = String(WChar, wide=True)
996ConstCString = String(Const(Char))
997ConstWString = String(Const(WChar), wide=True)