blob: b86668bd13be9d2a44b34227fbf5d06eee7733f5 [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
273class Blob(Type):
274
275 def __init__(self, type, size):
276 Type.__init__(self, type.expr + ' *')
277 self.type = type
278 self.size = size
279
280 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000281 return visitor.visitBlob(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000282
283
284class Struct(Type):
285
José Fonseca02c25002011-10-15 13:17:26 +0100286 __id = 0
287
José Fonseca6fac5ae2010-11-29 16:09:13 +0000288 def __init__(self, name, members):
289 Type.__init__(self, name)
José Fonseca02c25002011-10-15 13:17:26 +0100290
291 self.id = Struct.__id
292 Struct.__id += 1
293
José Fonseca6fac5ae2010-11-29 16:09:13 +0000294 self.name = name
José Fonsecadbf714b2012-11-20 17:03:43 +0000295 self.members = members
José Fonseca6fac5ae2010-11-29 16:09:13 +0000296
297 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000298 return visitor.visitStruct(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000299
300
José Fonsecadbf714b2012-11-20 17:03:43 +0000301def Union(kindExpr, kindTypes, contextLess=True):
José Fonsecaeb216e62012-11-20 11:08:08 +0000302 switchTypes = []
303 for kindCase, kindType, kindMemberName in kindTypes:
304 switchType = Struct(None, [(kindType, kindMemberName)])
305 switchTypes.append((kindCase, switchType))
306 return Polymorphic(kindExpr, switchTypes, contextLess=contextLess)
307
José Fonseca5b6fb752012-04-14 14:56:45 +0100308
José Fonseca6fac5ae2010-11-29 16:09:13 +0000309class Alias(Type):
310
311 def __init__(self, expr, type):
312 Type.__init__(self, expr)
313 self.type = type
314
315 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000316 return visitor.visitAlias(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000317
José Fonseca6fac5ae2010-11-29 16:09:13 +0000318class Arg:
319
José Fonseca9dd8f702012-04-07 10:42:50 +0100320 def __init__(self, type, name, input=True, output=False):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000321 self.type = type
322 self.name = name
José Fonseca9dd8f702012-04-07 10:42:50 +0100323 self.input = input
José Fonseca6fac5ae2010-11-29 16:09:13 +0000324 self.output = output
325 self.index = None
326
327 def __str__(self):
328 return '%s %s' % (self.type, self.name)
329
330
José Fonseca9dd8f702012-04-07 10:42:50 +0100331def In(type, name):
332 return Arg(type, name, input=True, output=False)
333
334def Out(type, name):
335 return Arg(type, name, input=False, output=True)
336
337def InOut(type, name):
338 return Arg(type, name, input=True, output=True)
339
340
José Fonseca6fac5ae2010-11-29 16:09:13 +0000341class Function:
342
José Fonseca84cea3b2012-05-09 21:12:30 +0100343 def __init__(self, type, name, args, call = '', fail = None, sideeffects=True, internal=False):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000344 self.type = type
345 self.name = name
346
347 self.args = []
348 index = 0
349 for arg in args:
José Fonseca8384ccb2011-05-25 10:12:02 +0100350 if not isinstance(arg, Arg):
351 if isinstance(arg, tuple):
352 arg_type, arg_name = arg
353 else:
354 arg_type = arg
355 arg_name = "arg%u" % index
José Fonseca6fac5ae2010-11-29 16:09:13 +0000356 arg = Arg(arg_type, arg_name)
357 arg.index = index
358 index += 1
359 self.args.append(arg)
360
361 self.call = call
362 self.fail = fail
363 self.sideeffects = sideeffects
José Fonseca84cea3b2012-05-09 21:12:30 +0100364 self.internal = internal
José Fonseca6fac5ae2010-11-29 16:09:13 +0000365
366 def prototype(self, name=None):
367 if name is not None:
368 name = name.strip()
369 else:
370 name = self.name
371 s = name
372 if self.call:
373 s = self.call + ' ' + s
374 if name.startswith('*'):
375 s = '(' + s + ')'
376 s = self.type.expr + ' ' + s
377 s += "("
378 if self.args:
379 s += ", ".join(["%s %s" % (arg.type, arg.name) for arg in self.args])
380 else:
381 s += "void"
382 s += ")"
383 return s
384
José Fonseca568ecc22012-01-15 13:57:03 +0000385 def argNames(self):
386 return [arg.name for arg in self.args]
387
José Fonseca6fac5ae2010-11-29 16:09:13 +0000388
389def StdFunction(*args, **kwargs):
390 kwargs.setdefault('call', '__stdcall')
391 return Function(*args, **kwargs)
392
393
394def FunctionPointer(type, name, args, **kwargs):
395 # XXX: We should probably treat function pointers (callbacks or not) in a generic fashion
396 return Opaque(name)
397
398
399class Interface(Type):
400
401 def __init__(self, name, base=None):
402 Type.__init__(self, name)
403 self.name = name
404 self.base = base
405 self.methods = []
406
407 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000408 return visitor.visitInterface(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000409
José Fonsecabd086342012-04-18 19:58:32 +0100410 def getMethodByName(self, name):
José Fonsecad275d0a2012-04-30 23:18:05 +0100411 for method in self.iterMethods():
412 if method.name == name:
413 return method
José Fonsecabd086342012-04-18 19:58:32 +0100414 return None
415
José Fonseca54f304a2012-01-14 19:33:08 +0000416 def iterMethods(self):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000417 if self.base is not None:
José Fonseca54f304a2012-01-14 19:33:08 +0000418 for method in self.base.iterMethods():
José Fonseca6fac5ae2010-11-29 16:09:13 +0000419 yield method
420 for method in self.methods:
421 yield method
422 raise StopIteration
423
José Fonseca143e9252012-04-15 09:31:18 +0100424 def iterBases(self):
425 iface = self
426 while iface is not None:
427 yield iface
428 iface = iface.base
429 raise StopIteration
430
José Fonseca4220b1b2012-02-03 19:05:29 +0000431 def iterBaseMethods(self):
432 if self.base is not None:
433 for iface, method in self.base.iterBaseMethods():
434 yield iface, method
435 for method in self.methods:
436 yield self, method
437 raise StopIteration
438
José Fonseca6fac5ae2010-11-29 16:09:13 +0000439
440class Method(Function):
441
José Fonseca43aa19f2012-11-10 09:29:38 +0000442 def __init__(self, type, name, args, call = '', const=False, sideeffects=True):
443 assert call == '__stdcall'
José Fonseca5b6fb752012-04-14 14:56:45 +0100444 Function.__init__(self, type, name, args, call = call, sideeffects=sideeffects)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000445 for index in range(len(self.args)):
446 self.args[index].index = index + 1
José Fonseca9dbeda62012-02-03 19:05:54 +0000447 self.const = const
448
449 def prototype(self, name=None):
450 s = Function.prototype(self, name)
451 if self.const:
452 s += ' const'
453 return s
José Fonseca6fac5ae2010-11-29 16:09:13 +0000454
José Fonsecabcb26b22012-04-15 08:42:25 +0100455
José Fonseca5b6fb752012-04-14 14:56:45 +0100456def StdMethod(*args, **kwargs):
457 kwargs.setdefault('call', '__stdcall')
458 return Method(*args, **kwargs)
459
José Fonseca6fac5ae2010-11-29 16:09:13 +0000460
José Fonseca6fac5ae2010-11-29 16:09:13 +0000461class String(Type):
José Fonsecabcfc81b2012-08-07 21:07:22 +0100462 '''Human-legible character string.'''
José Fonseca6fac5ae2010-11-29 16:09:13 +0000463
José Fonsecabcfc81b2012-08-07 21:07:22 +0100464 def __init__(self, type = Char, length = None, wide = False):
465 assert isinstance(type, Type)
466 Type.__init__(self, type.expr + ' *')
467 self.type = type
José Fonseca6fac5ae2010-11-29 16:09:13 +0000468 self.length = length
José Fonsecabcfc81b2012-08-07 21:07:22 +0100469 self.wide = wide
José Fonseca6fac5ae2010-11-29 16:09:13 +0000470
471 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000472 return visitor.visitString(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000473
José Fonseca6fac5ae2010-11-29 16:09:13 +0000474
475class Opaque(Type):
476 '''Opaque pointer.'''
477
478 def __init__(self, expr):
479 Type.__init__(self, expr)
480
481 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000482 return visitor.visitOpaque(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000483
484
485def OpaquePointer(type, *args):
486 return Opaque(type.expr + ' *')
487
488def OpaqueArray(type, size):
489 return Opaque(type.expr + ' *')
490
491def OpaqueBlob(type, size):
492 return Opaque(type.expr + ' *')
José Fonseca8a56d142008-07-09 12:18:08 +0900493
José Fonseca501f2862010-11-19 20:41:18 +0000494
José Fonseca16d46dd2011-10-13 09:52:52 +0100495class Polymorphic(Type):
496
José Fonsecaeb216e62012-11-20 11:08:08 +0000497 def __init__(self, switchExpr, switchTypes, defaultType=None, contextLess=True):
498 if defaultType is None:
499 Type.__init__(self, None)
500 contextLess = False
501 else:
502 Type.__init__(self, defaultType.expr)
José Fonseca54f304a2012-01-14 19:33:08 +0000503 self.switchExpr = switchExpr
504 self.switchTypes = switchTypes
José Fonsecab95e3722012-04-16 14:01:15 +0100505 self.defaultType = defaultType
506 self.contextLess = contextLess
José Fonseca16d46dd2011-10-13 09:52:52 +0100507
508 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000509 return visitor.visitPolymorphic(self, *args, **kwargs)
José Fonseca16d46dd2011-10-13 09:52:52 +0100510
José Fonseca54f304a2012-01-14 19:33:08 +0000511 def iterSwitch(self):
José Fonsecaeb216e62012-11-20 11:08:08 +0000512 cases = []
513 types = []
514
515 if self.defaultType is not None:
516 cases.append(['default'])
517 types.append(self.defaultType)
José Fonseca46161112011-10-14 10:04:55 +0100518
José Fonseca54f304a2012-01-14 19:33:08 +0000519 for expr, type in self.switchTypes:
José Fonseca46161112011-10-14 10:04:55 +0100520 case = 'case %s' % expr
521 try:
522 i = types.index(type)
523 except ValueError:
524 cases.append([case])
525 types.append(type)
526 else:
527 cases[i].append(case)
528
529 return zip(cases, types)
530
José Fonseca16d46dd2011-10-13 09:52:52 +0100531
José Fonsecab95e3722012-04-16 14:01:15 +0100532def EnumPolymorphic(enumName, switchExpr, switchTypes, defaultType, contextLess=True):
533 enumValues = [expr for expr, type in switchTypes]
534 enum = Enum(enumName, enumValues)
535 polymorphic = Polymorphic(switchExpr, switchTypes, defaultType, contextLess)
536 return enum, polymorphic
537
538
José Fonseca501f2862010-11-19 20:41:18 +0000539class Visitor:
José Fonseca9c4a2572012-01-13 23:21:10 +0000540 '''Abstract visitor for the type hierarchy.'''
José Fonseca501f2862010-11-19 20:41:18 +0000541
542 def visit(self, type, *args, **kwargs):
543 return type.visit(self, *args, **kwargs)
544
José Fonseca54f304a2012-01-14 19:33:08 +0000545 def visitVoid(self, void, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000546 raise NotImplementedError
547
José Fonseca54f304a2012-01-14 19:33:08 +0000548 def visitLiteral(self, literal, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000549 raise NotImplementedError
550
José Fonseca54f304a2012-01-14 19:33:08 +0000551 def visitString(self, string, *args, **kwargs):
José Fonseca2defc982010-11-22 16:59:10 +0000552 raise NotImplementedError
553
José Fonseca54f304a2012-01-14 19:33:08 +0000554 def visitConst(self, const, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000555 raise NotImplementedError
556
José Fonseca54f304a2012-01-14 19:33:08 +0000557 def visitStruct(self, struct, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000558 raise NotImplementedError
559
José Fonseca54f304a2012-01-14 19:33:08 +0000560 def visitArray(self, array, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000561 raise NotImplementedError
562
José Fonseca54f304a2012-01-14 19:33:08 +0000563 def visitBlob(self, blob, *args, **kwargs):
José Fonseca885f2652010-11-20 11:22:25 +0000564 raise NotImplementedError
565
José Fonseca54f304a2012-01-14 19:33:08 +0000566 def visitEnum(self, enum, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000567 raise NotImplementedError
568
José Fonseca54f304a2012-01-14 19:33:08 +0000569 def visitBitmask(self, bitmask, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000570 raise NotImplementedError
571
José Fonseca54f304a2012-01-14 19:33:08 +0000572 def visitPointer(self, pointer, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000573 raise NotImplementedError
574
José Fonseca59ee88e2012-01-15 14:24:10 +0000575 def visitIntPointer(self, pointer, *args, **kwargs):
576 raise NotImplementedError
577
José Fonsecafbcf6832012-04-05 07:10:30 +0100578 def visitObjPointer(self, pointer, *args, **kwargs):
579 raise NotImplementedError
580
José Fonseca59ee88e2012-01-15 14:24:10 +0000581 def visitLinearPointer(self, pointer, *args, **kwargs):
582 raise NotImplementedError
583
José Fonsecab89c5932012-04-01 22:47:11 +0200584 def visitReference(self, reference, *args, **kwargs):
585 raise NotImplementedError
586
José Fonseca54f304a2012-01-14 19:33:08 +0000587 def visitHandle(self, handle, *args, **kwargs):
José Fonseca50d78d82010-11-23 22:13:14 +0000588 raise NotImplementedError
589
José Fonseca54f304a2012-01-14 19:33:08 +0000590 def visitAlias(self, alias, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000591 raise NotImplementedError
592
José Fonseca54f304a2012-01-14 19:33:08 +0000593 def visitOpaque(self, opaque, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000594 raise NotImplementedError
595
José Fonseca54f304a2012-01-14 19:33:08 +0000596 def visitInterface(self, interface, *args, **kwargs):
José Fonsecac356d6a2010-11-23 14:27:25 +0000597 raise NotImplementedError
598
José Fonseca54f304a2012-01-14 19:33:08 +0000599 def visitPolymorphic(self, polymorphic, *args, **kwargs):
José Fonseca16d46dd2011-10-13 09:52:52 +0100600 raise NotImplementedError
José Fonseca54f304a2012-01-14 19:33:08 +0000601 #return self.visit(polymorphic.defaultType, *args, **kwargs)
José Fonseca16d46dd2011-10-13 09:52:52 +0100602
José Fonsecac356d6a2010-11-23 14:27:25 +0000603
604class OnceVisitor(Visitor):
José Fonseca9c4a2572012-01-13 23:21:10 +0000605 '''Visitor that guarantees that each type is visited only once.'''
José Fonsecac356d6a2010-11-23 14:27:25 +0000606
607 def __init__(self):
608 self.__visited = set()
609
610 def visit(self, type, *args, **kwargs):
611 if type not in self.__visited:
612 self.__visited.add(type)
613 return type.visit(self, *args, **kwargs)
614 return None
615
José Fonseca501f2862010-11-19 20:41:18 +0000616
José Fonsecac9edb832010-11-20 09:03:10 +0000617class Rebuilder(Visitor):
José Fonseca9c4a2572012-01-13 23:21:10 +0000618 '''Visitor which rebuild types as it visits them.
619
620 By itself it is a no-op -- it is intended to be overwritten.
621 '''
José Fonsecac9edb832010-11-20 09:03:10 +0000622
José Fonseca54f304a2012-01-14 19:33:08 +0000623 def visitVoid(self, void):
José Fonsecac9edb832010-11-20 09:03:10 +0000624 return void
625
José Fonseca54f304a2012-01-14 19:33:08 +0000626 def visitLiteral(self, literal):
José Fonsecac9edb832010-11-20 09:03:10 +0000627 return literal
628
José Fonseca54f304a2012-01-14 19:33:08 +0000629 def visitString(self, string):
José Fonsecabcfc81b2012-08-07 21:07:22 +0100630 string_type = self.visit(string.type)
631 if string_type is string.type:
632 return string
633 else:
634 return String(string_type, string.length, string.wide)
José Fonseca2defc982010-11-22 16:59:10 +0000635
José Fonseca54f304a2012-01-14 19:33:08 +0000636 def visitConst(self, const):
José Fonsecaf182eda2012-04-05 19:59:56 +0100637 const_type = self.visit(const.type)
638 if const_type is const.type:
639 return const
640 else:
641 return Const(const_type)
José Fonsecac9edb832010-11-20 09:03:10 +0000642
José Fonseca54f304a2012-01-14 19:33:08 +0000643 def visitStruct(self, struct):
José Fonseca06aa2842011-05-05 07:55:54 +0100644 members = [(self.visit(type), name) for type, name in struct.members]
José Fonsecac9edb832010-11-20 09:03:10 +0000645 return Struct(struct.name, members)
646
José Fonseca54f304a2012-01-14 19:33:08 +0000647 def visitArray(self, array):
José Fonsecac9edb832010-11-20 09:03:10 +0000648 type = self.visit(array.type)
649 return Array(type, array.length)
650
José Fonseca54f304a2012-01-14 19:33:08 +0000651 def visitBlob(self, blob):
José Fonseca885f2652010-11-20 11:22:25 +0000652 type = self.visit(blob.type)
653 return Blob(type, blob.size)
654
José Fonseca54f304a2012-01-14 19:33:08 +0000655 def visitEnum(self, enum):
José Fonsecac9edb832010-11-20 09:03:10 +0000656 return enum
657
José Fonseca54f304a2012-01-14 19:33:08 +0000658 def visitBitmask(self, bitmask):
José Fonsecac9edb832010-11-20 09:03:10 +0000659 type = self.visit(bitmask.type)
660 return Bitmask(type, bitmask.values)
661
José Fonseca54f304a2012-01-14 19:33:08 +0000662 def visitPointer(self, pointer):
José Fonsecaf182eda2012-04-05 19:59:56 +0100663 pointer_type = self.visit(pointer.type)
664 if pointer_type is pointer.type:
665 return pointer
666 else:
667 return Pointer(pointer_type)
José Fonsecac9edb832010-11-20 09:03:10 +0000668
José Fonseca59ee88e2012-01-15 14:24:10 +0000669 def visitIntPointer(self, pointer):
670 return pointer
671
José Fonsecafbcf6832012-04-05 07:10:30 +0100672 def visitObjPointer(self, pointer):
José Fonsecaf182eda2012-04-05 19:59:56 +0100673 pointer_type = self.visit(pointer.type)
674 if pointer_type is pointer.type:
675 return pointer
676 else:
677 return ObjPointer(pointer_type)
José Fonsecafbcf6832012-04-05 07:10:30 +0100678
José Fonseca59ee88e2012-01-15 14:24:10 +0000679 def visitLinearPointer(self, pointer):
José Fonsecaf182eda2012-04-05 19:59:56 +0100680 pointer_type = self.visit(pointer.type)
681 if pointer_type is pointer.type:
682 return pointer
683 else:
684 return LinearPointer(pointer_type)
José Fonseca59ee88e2012-01-15 14:24:10 +0000685
José Fonsecab89c5932012-04-01 22:47:11 +0200686 def visitReference(self, reference):
José Fonsecaf182eda2012-04-05 19:59:56 +0100687 reference_type = self.visit(reference.type)
688 if reference_type is reference.type:
689 return reference
690 else:
691 return Reference(reference_type)
José Fonsecab89c5932012-04-01 22:47:11 +0200692
José Fonseca54f304a2012-01-14 19:33:08 +0000693 def visitHandle(self, handle):
José Fonsecaf182eda2012-04-05 19:59:56 +0100694 handle_type = self.visit(handle.type)
695 if handle_type is handle.type:
696 return handle
697 else:
698 return Handle(handle.name, handle_type, range=handle.range, key=handle.key)
José Fonseca50d78d82010-11-23 22:13:14 +0000699
José Fonseca54f304a2012-01-14 19:33:08 +0000700 def visitAlias(self, alias):
José Fonsecaf182eda2012-04-05 19:59:56 +0100701 alias_type = self.visit(alias.type)
702 if alias_type is alias.type:
703 return alias
704 else:
705 return Alias(alias.expr, alias_type)
José Fonsecac9edb832010-11-20 09:03:10 +0000706
José Fonseca54f304a2012-01-14 19:33:08 +0000707 def visitOpaque(self, opaque):
José Fonsecac9edb832010-11-20 09:03:10 +0000708 return opaque
709
José Fonseca7814edf2012-01-31 10:55:49 +0000710 def visitInterface(self, interface, *args, **kwargs):
711 return interface
712
José Fonseca54f304a2012-01-14 19:33:08 +0000713 def visitPolymorphic(self, polymorphic):
José Fonseca54f304a2012-01-14 19:33:08 +0000714 switchExpr = polymorphic.switchExpr
715 switchTypes = [(expr, self.visit(type)) for expr, type in polymorphic.switchTypes]
José Fonsecaeb216e62012-11-20 11:08:08 +0000716 if polymorphic.defaultType is None:
717 defaultType = None
718 else:
719 defaultType = self.visit(polymorphic.defaultType)
José Fonsecab95e3722012-04-16 14:01:15 +0100720 return Polymorphic(switchExpr, switchTypes, defaultType, polymorphic.contextLess)
José Fonseca16d46dd2011-10-13 09:52:52 +0100721
José Fonsecac9edb832010-11-20 09:03:10 +0000722
José Fonseca0075f152012-04-14 20:25:52 +0100723class MutableRebuilder(Rebuilder):
724 '''Type visitor which derives a mutable type.'''
725
José Fonsecabcfc81b2012-08-07 21:07:22 +0100726 def visitString(self, string):
727 return string
728
José Fonseca0075f152012-04-14 20:25:52 +0100729 def visitConst(self, const):
730 # Strip out const qualifier
731 return const.type
732
733 def visitAlias(self, alias):
734 # Tear the alias on type changes
735 type = self.visit(alias.type)
736 if type is alias.type:
737 return alias
738 return type
739
740 def visitReference(self, reference):
741 # Strip out references
742 return reference.type
743
744
745class Traverser(Visitor):
746 '''Visitor which all types.'''
747
748 def visitVoid(self, void, *args, **kwargs):
749 pass
750
751 def visitLiteral(self, literal, *args, **kwargs):
752 pass
753
754 def visitString(self, string, *args, **kwargs):
755 pass
756
757 def visitConst(self, const, *args, **kwargs):
758 self.visit(const.type, *args, **kwargs)
759
760 def visitStruct(self, struct, *args, **kwargs):
761 for type, name in struct.members:
762 self.visit(type, *args, **kwargs)
763
764 def visitArray(self, array, *args, **kwargs):
765 self.visit(array.type, *args, **kwargs)
766
767 def visitBlob(self, array, *args, **kwargs):
768 pass
769
770 def visitEnum(self, enum, *args, **kwargs):
771 pass
772
773 def visitBitmask(self, bitmask, *args, **kwargs):
774 self.visit(bitmask.type, *args, **kwargs)
775
776 def visitPointer(self, pointer, *args, **kwargs):
777 self.visit(pointer.type, *args, **kwargs)
778
779 def visitIntPointer(self, pointer, *args, **kwargs):
780 pass
781
782 def visitObjPointer(self, pointer, *args, **kwargs):
783 self.visit(pointer.type, *args, **kwargs)
784
785 def visitLinearPointer(self, pointer, *args, **kwargs):
786 self.visit(pointer.type, *args, **kwargs)
787
788 def visitReference(self, reference, *args, **kwargs):
789 self.visit(reference.type, *args, **kwargs)
790
791 def visitHandle(self, handle, *args, **kwargs):
792 self.visit(handle.type, *args, **kwargs)
793
794 def visitAlias(self, alias, *args, **kwargs):
795 self.visit(alias.type, *args, **kwargs)
796
797 def visitOpaque(self, opaque, *args, **kwargs):
798 pass
799
800 def visitInterface(self, interface, *args, **kwargs):
801 if interface.base is not None:
802 self.visit(interface.base, *args, **kwargs)
803 for method in interface.iterMethods():
804 for arg in method.args:
805 self.visit(arg.type, *args, **kwargs)
806 self.visit(method.type, *args, **kwargs)
807
808 def visitPolymorphic(self, polymorphic, *args, **kwargs):
José Fonseca0075f152012-04-14 20:25:52 +0100809 for expr, type in polymorphic.switchTypes:
810 self.visit(type, *args, **kwargs)
José Fonsecaeb216e62012-11-20 11:08:08 +0000811 if polymorphic.defaultType is not None:
812 self.visit(polymorphic.defaultType, *args, **kwargs)
José Fonseca0075f152012-04-14 20:25:52 +0100813
814
815class Collector(Traverser):
José Fonseca9c4a2572012-01-13 23:21:10 +0000816 '''Visitor which collects all unique types as it traverses them.'''
José Fonsecae6a50bd2010-11-24 10:12:22 +0000817
818 def __init__(self):
819 self.__visited = set()
820 self.types = []
821
822 def visit(self, type):
823 if type in self.__visited:
824 return
825 self.__visited.add(type)
826 Visitor.visit(self, type)
827 self.types.append(type)
828
José Fonseca16d46dd2011-10-13 09:52:52 +0100829
José Fonsecadbf714b2012-11-20 17:03:43 +0000830class ExpanderMixin:
831 '''Mixin class that provides a bunch of methods to expand C expressions
832 from the specifications.'''
833
834 __structs = None
835 __indices = None
836
837 def expand(self, expr):
838 # Expand a C expression, replacing certain variables
839 if not isinstance(expr, basestring):
840 return expr
841 variables = {}
842
843 if self.__structs is not None:
844 variables['self'] = '(%s)' % self.__structs[0]
845 if self.__indices is not None:
846 variables['i'] = self.__indices[0]
847
848 expandedExpr = expr.format(**variables)
849 if expandedExpr != expr and 0:
850 sys.stderr.write(" %r -> %r\n" % (expr, expandedExpr))
851 return expandedExpr
852
853 def visitMember(self, member, structInstance, *args, **kwargs):
854 memberType, memberName = member
855 if memberName is None:
856 # Anonymous structure/union member
857 memberInstance = structInstance
858 else:
859 memberInstance = '(%s).%s' % (structInstance, memberName)
860 self.__structs = (structInstance, self.__structs)
861 try:
862 return self.visit(memberType, memberInstance, *args, **kwargs)
863 finally:
864 _, self.__structs = self.__structs
865
866 def visitElement(self, elementIndex, elementType, *args, **kwargs):
867 self.__indices = (elementIndex, self.__indices)
868 try:
869 return self.visit(elementType, *args, **kwargs)
870 finally:
871 _, self.__indices = self.__indices
872
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000873
José Fonseca81301932012-11-11 00:10:20 +0000874class Module:
875 '''A collection of functions.'''
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000876
José Fonseca68ec4122011-02-20 11:25:25 +0000877 def __init__(self, name = None):
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000878 self.name = name
879 self.headers = []
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000880 self.functions = []
881 self.interfaces = []
882
José Fonseca54f304a2012-01-14 19:33:08 +0000883 def addFunctions(self, functions):
José Fonseca81301932012-11-11 00:10:20 +0000884 self.functions.extend(functions)
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000885
José Fonseca54f304a2012-01-14 19:33:08 +0000886 def addInterfaces(self, interfaces):
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000887 self.interfaces.extend(interfaces)
888
José Fonseca81301932012-11-11 00:10:20 +0000889 def mergeModule(self, module):
890 self.headers.extend(module.headers)
891 self.functions.extend(module.functions)
892 self.interfaces.extend(module.interfaces)
José Fonseca68ec4122011-02-20 11:25:25 +0000893
José Fonseca1b6c8752012-04-15 14:33:00 +0100894 def getFunctionByName(self, name):
José Fonsecaeccec3e2011-02-20 09:01:25 +0000895 for function in self.functions:
896 if function.name == name:
897 return function
898 return None
899
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000900
José Fonseca81301932012-11-11 00:10:20 +0000901class API:
902 '''API abstraction.
903
904 Essentially, a collection of types, functions, and interfaces.
905 '''
906
907 def __init__(self, modules = None):
908 self.modules = []
909 if modules is not None:
910 self.modules.extend(modules)
911
912 def getAllTypes(self):
913 collector = Collector()
914 for module in self.modules:
915 for function in module.functions:
916 for arg in function.args:
917 collector.visit(arg.type)
918 collector.visit(function.type)
919 for interface in module.interfaces:
920 collector.visit(interface)
921 for method in interface.iterMethods():
922 for arg in method.args:
923 collector.visit(arg.type)
924 collector.visit(method.type)
925 return collector.types
926
927 def getAllFunctions(self):
928 functions = []
929 for module in self.modules:
930 functions.extend(module.functions)
931 return functions
932
933 def getAllInterfaces(self):
934 types = self.getAllTypes()
935 interfaces = [type for type in types if isinstance(type, Interface)]
936 for module in self.modules:
937 for interface in module.interfaces:
938 if interface not in interfaces:
939 interfaces.append(interface)
940 return interfaces
941
942 def addModule(self, module):
943 self.modules.append(module)
944
945 def getFunctionByName(self, name):
946 for module in self.modules:
947 for function in module.functions:
948 if function.name == name:
949 return function
950 return None
951
952
José Fonseca280a1762012-01-31 15:10:13 +0000953# C string (i.e., zero terminated)
José Fonsecabcfc81b2012-08-07 21:07:22 +0100954CString = String(Char)
955WString = String(WChar, wide=True)
956ConstCString = String(Const(Char))
957ConstWString = String(Const(WChar), wide=True)