blob: 990c6510ca6a914cce8eba23998ba4599edf826e [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é Fonseca999284f2013-02-19 13:29:26 +0000388 def getArgByName(self, name):
389 for arg in self.args:
390 if arg.name == name:
391 return arg
392 return None
393
José Fonseca6fac5ae2010-11-29 16:09:13 +0000394
395def StdFunction(*args, **kwargs):
396 kwargs.setdefault('call', '__stdcall')
397 return Function(*args, **kwargs)
398
399
400def FunctionPointer(type, name, args, **kwargs):
401 # XXX: We should probably treat function pointers (callbacks or not) in a generic fashion
402 return Opaque(name)
403
404
405class Interface(Type):
406
407 def __init__(self, name, base=None):
408 Type.__init__(self, name)
409 self.name = name
410 self.base = base
411 self.methods = []
412
413 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000414 return visitor.visitInterface(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000415
José Fonsecabd086342012-04-18 19:58:32 +0100416 def getMethodByName(self, name):
José Fonsecad275d0a2012-04-30 23:18:05 +0100417 for method in self.iterMethods():
418 if method.name == name:
419 return method
José Fonsecabd086342012-04-18 19:58:32 +0100420 return None
421
José Fonseca54f304a2012-01-14 19:33:08 +0000422 def iterMethods(self):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000423 if self.base is not None:
José Fonseca54f304a2012-01-14 19:33:08 +0000424 for method in self.base.iterMethods():
José Fonseca6fac5ae2010-11-29 16:09:13 +0000425 yield method
426 for method in self.methods:
427 yield method
428 raise StopIteration
429
José Fonseca143e9252012-04-15 09:31:18 +0100430 def iterBases(self):
431 iface = self
432 while iface is not None:
433 yield iface
434 iface = iface.base
435 raise StopIteration
436
José Fonseca5abf5602013-05-30 14:00:44 +0100437 def hasBase(self, *bases):
438 for iface in self.iterBases():
439 if iface in bases:
440 return True
441 return False
442
José Fonseca4220b1b2012-02-03 19:05:29 +0000443 def iterBaseMethods(self):
444 if self.base is not None:
445 for iface, method in self.base.iterBaseMethods():
446 yield iface, method
447 for method in self.methods:
448 yield self, method
449 raise StopIteration
450
José Fonseca6fac5ae2010-11-29 16:09:13 +0000451
452class Method(Function):
453
José Fonseca43aa19f2012-11-10 09:29:38 +0000454 def __init__(self, type, name, args, call = '', const=False, sideeffects=True):
455 assert call == '__stdcall'
José Fonseca5b6fb752012-04-14 14:56:45 +0100456 Function.__init__(self, type, name, args, call = call, sideeffects=sideeffects)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000457 for index in range(len(self.args)):
458 self.args[index].index = index + 1
José Fonseca9dbeda62012-02-03 19:05:54 +0000459 self.const = const
460
461 def prototype(self, name=None):
462 s = Function.prototype(self, name)
463 if self.const:
464 s += ' const'
465 return s
José Fonseca6fac5ae2010-11-29 16:09:13 +0000466
José Fonsecabcb26b22012-04-15 08:42:25 +0100467
José Fonseca5b6fb752012-04-14 14:56:45 +0100468def StdMethod(*args, **kwargs):
469 kwargs.setdefault('call', '__stdcall')
470 return Method(*args, **kwargs)
471
José Fonseca6fac5ae2010-11-29 16:09:13 +0000472
José Fonseca6fac5ae2010-11-29 16:09:13 +0000473class String(Type):
José Fonsecabcfc81b2012-08-07 21:07:22 +0100474 '''Human-legible character string.'''
José Fonseca6fac5ae2010-11-29 16:09:13 +0000475
José Fonsecabcfc81b2012-08-07 21:07:22 +0100476 def __init__(self, type = Char, length = None, wide = False):
477 assert isinstance(type, Type)
478 Type.__init__(self, type.expr + ' *')
479 self.type = type
José Fonseca6fac5ae2010-11-29 16:09:13 +0000480 self.length = length
José Fonsecabcfc81b2012-08-07 21:07:22 +0100481 self.wide = wide
José Fonseca6fac5ae2010-11-29 16:09:13 +0000482
483 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000484 return visitor.visitString(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000485
José Fonseca6fac5ae2010-11-29 16:09:13 +0000486
487class Opaque(Type):
488 '''Opaque pointer.'''
489
490 def __init__(self, expr):
491 Type.__init__(self, expr)
492
493 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000494 return visitor.visitOpaque(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000495
496
497def OpaquePointer(type, *args):
498 return Opaque(type.expr + ' *')
499
500def OpaqueArray(type, size):
501 return Opaque(type.expr + ' *')
502
503def OpaqueBlob(type, size):
504 return Opaque(type.expr + ' *')
José Fonseca8a56d142008-07-09 12:18:08 +0900505
José Fonseca501f2862010-11-19 20:41:18 +0000506
José Fonseca16d46dd2011-10-13 09:52:52 +0100507class Polymorphic(Type):
508
José Fonsecaeb216e62012-11-20 11:08:08 +0000509 def __init__(self, switchExpr, switchTypes, defaultType=None, contextLess=True):
510 if defaultType is None:
511 Type.__init__(self, None)
512 contextLess = False
513 else:
514 Type.__init__(self, defaultType.expr)
José Fonseca54f304a2012-01-14 19:33:08 +0000515 self.switchExpr = switchExpr
516 self.switchTypes = switchTypes
José Fonsecab95e3722012-04-16 14:01:15 +0100517 self.defaultType = defaultType
518 self.contextLess = contextLess
José Fonseca16d46dd2011-10-13 09:52:52 +0100519
520 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000521 return visitor.visitPolymorphic(self, *args, **kwargs)
José Fonseca16d46dd2011-10-13 09:52:52 +0100522
José Fonseca54f304a2012-01-14 19:33:08 +0000523 def iterSwitch(self):
José Fonsecaeb216e62012-11-20 11:08:08 +0000524 cases = []
525 types = []
526
527 if self.defaultType is not None:
528 cases.append(['default'])
529 types.append(self.defaultType)
José Fonseca46161112011-10-14 10:04:55 +0100530
José Fonseca54f304a2012-01-14 19:33:08 +0000531 for expr, type in self.switchTypes:
José Fonseca46161112011-10-14 10:04:55 +0100532 case = 'case %s' % expr
533 try:
534 i = types.index(type)
535 except ValueError:
536 cases.append([case])
537 types.append(type)
538 else:
539 cases[i].append(case)
540
541 return zip(cases, types)
542
José Fonseca16d46dd2011-10-13 09:52:52 +0100543
José Fonsecab95e3722012-04-16 14:01:15 +0100544def EnumPolymorphic(enumName, switchExpr, switchTypes, defaultType, contextLess=True):
545 enumValues = [expr for expr, type in switchTypes]
546 enum = Enum(enumName, enumValues)
547 polymorphic = Polymorphic(switchExpr, switchTypes, defaultType, contextLess)
548 return enum, polymorphic
549
550
José Fonseca501f2862010-11-19 20:41:18 +0000551class Visitor:
José Fonseca9c4a2572012-01-13 23:21:10 +0000552 '''Abstract visitor for the type hierarchy.'''
José Fonseca501f2862010-11-19 20:41:18 +0000553
554 def visit(self, type, *args, **kwargs):
555 return type.visit(self, *args, **kwargs)
556
José Fonseca54f304a2012-01-14 19:33:08 +0000557 def visitVoid(self, void, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000558 raise NotImplementedError
559
José Fonseca54f304a2012-01-14 19:33:08 +0000560 def visitLiteral(self, literal, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000561 raise NotImplementedError
562
José Fonseca54f304a2012-01-14 19:33:08 +0000563 def visitString(self, string, *args, **kwargs):
José Fonseca2defc982010-11-22 16:59:10 +0000564 raise NotImplementedError
565
José Fonseca54f304a2012-01-14 19:33:08 +0000566 def visitConst(self, const, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000567 raise NotImplementedError
568
José Fonseca54f304a2012-01-14 19:33:08 +0000569 def visitStruct(self, struct, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000570 raise NotImplementedError
571
José Fonseca54f304a2012-01-14 19:33:08 +0000572 def visitArray(self, array, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000573 raise NotImplementedError
574
José Fonseca54f304a2012-01-14 19:33:08 +0000575 def visitBlob(self, blob, *args, **kwargs):
José Fonseca885f2652010-11-20 11:22:25 +0000576 raise NotImplementedError
577
José Fonseca54f304a2012-01-14 19:33:08 +0000578 def visitEnum(self, enum, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000579 raise NotImplementedError
580
José Fonseca54f304a2012-01-14 19:33:08 +0000581 def visitBitmask(self, bitmask, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000582 raise NotImplementedError
583
José Fonseca54f304a2012-01-14 19:33:08 +0000584 def visitPointer(self, pointer, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000585 raise NotImplementedError
586
José Fonseca59ee88e2012-01-15 14:24:10 +0000587 def visitIntPointer(self, pointer, *args, **kwargs):
588 raise NotImplementedError
589
José Fonsecafbcf6832012-04-05 07:10:30 +0100590 def visitObjPointer(self, pointer, *args, **kwargs):
591 raise NotImplementedError
592
José Fonseca59ee88e2012-01-15 14:24:10 +0000593 def visitLinearPointer(self, pointer, *args, **kwargs):
594 raise NotImplementedError
595
José Fonsecab89c5932012-04-01 22:47:11 +0200596 def visitReference(self, reference, *args, **kwargs):
597 raise NotImplementedError
598
José Fonseca54f304a2012-01-14 19:33:08 +0000599 def visitHandle(self, handle, *args, **kwargs):
José Fonseca50d78d82010-11-23 22:13:14 +0000600 raise NotImplementedError
601
José Fonseca54f304a2012-01-14 19:33:08 +0000602 def visitAlias(self, alias, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000603 raise NotImplementedError
604
José Fonseca54f304a2012-01-14 19:33:08 +0000605 def visitOpaque(self, opaque, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000606 raise NotImplementedError
607
José Fonseca54f304a2012-01-14 19:33:08 +0000608 def visitInterface(self, interface, *args, **kwargs):
José Fonsecac356d6a2010-11-23 14:27:25 +0000609 raise NotImplementedError
610
José Fonseca54f304a2012-01-14 19:33:08 +0000611 def visitPolymorphic(self, polymorphic, *args, **kwargs):
José Fonseca16d46dd2011-10-13 09:52:52 +0100612 raise NotImplementedError
José Fonseca54f304a2012-01-14 19:33:08 +0000613 #return self.visit(polymorphic.defaultType, *args, **kwargs)
José Fonseca16d46dd2011-10-13 09:52:52 +0100614
José Fonsecac356d6a2010-11-23 14:27:25 +0000615
616class OnceVisitor(Visitor):
José Fonseca9c4a2572012-01-13 23:21:10 +0000617 '''Visitor that guarantees that each type is visited only once.'''
José Fonsecac356d6a2010-11-23 14:27:25 +0000618
619 def __init__(self):
620 self.__visited = set()
621
622 def visit(self, type, *args, **kwargs):
623 if type not in self.__visited:
624 self.__visited.add(type)
625 return type.visit(self, *args, **kwargs)
626 return None
627
José Fonseca501f2862010-11-19 20:41:18 +0000628
José Fonsecac9edb832010-11-20 09:03:10 +0000629class Rebuilder(Visitor):
José Fonseca9c4a2572012-01-13 23:21:10 +0000630 '''Visitor which rebuild types as it visits them.
631
632 By itself it is a no-op -- it is intended to be overwritten.
633 '''
José Fonsecac9edb832010-11-20 09:03:10 +0000634
José Fonseca54f304a2012-01-14 19:33:08 +0000635 def visitVoid(self, void):
José Fonsecac9edb832010-11-20 09:03:10 +0000636 return void
637
José Fonseca54f304a2012-01-14 19:33:08 +0000638 def visitLiteral(self, literal):
José Fonsecac9edb832010-11-20 09:03:10 +0000639 return literal
640
José Fonseca54f304a2012-01-14 19:33:08 +0000641 def visitString(self, string):
José Fonsecabcfc81b2012-08-07 21:07:22 +0100642 string_type = self.visit(string.type)
643 if string_type is string.type:
644 return string
645 else:
646 return String(string_type, string.length, string.wide)
José Fonseca2defc982010-11-22 16:59:10 +0000647
José Fonseca54f304a2012-01-14 19:33:08 +0000648 def visitConst(self, const):
José Fonsecaf182eda2012-04-05 19:59:56 +0100649 const_type = self.visit(const.type)
650 if const_type is const.type:
651 return const
652 else:
653 return Const(const_type)
José Fonsecac9edb832010-11-20 09:03:10 +0000654
José Fonseca54f304a2012-01-14 19:33:08 +0000655 def visitStruct(self, struct):
José Fonseca06aa2842011-05-05 07:55:54 +0100656 members = [(self.visit(type), name) for type, name in struct.members]
José Fonsecac9edb832010-11-20 09:03:10 +0000657 return Struct(struct.name, members)
658
José Fonseca54f304a2012-01-14 19:33:08 +0000659 def visitArray(self, array):
José Fonsecac9edb832010-11-20 09:03:10 +0000660 type = self.visit(array.type)
661 return Array(type, array.length)
662
José Fonseca54f304a2012-01-14 19:33:08 +0000663 def visitBlob(self, blob):
José Fonseca885f2652010-11-20 11:22:25 +0000664 type = self.visit(blob.type)
665 return Blob(type, blob.size)
666
José Fonseca54f304a2012-01-14 19:33:08 +0000667 def visitEnum(self, enum):
José Fonsecac9edb832010-11-20 09:03:10 +0000668 return enum
669
José Fonseca54f304a2012-01-14 19:33:08 +0000670 def visitBitmask(self, bitmask):
José Fonsecac9edb832010-11-20 09:03:10 +0000671 type = self.visit(bitmask.type)
672 return Bitmask(type, bitmask.values)
673
José Fonseca54f304a2012-01-14 19:33:08 +0000674 def visitPointer(self, pointer):
José Fonsecaf182eda2012-04-05 19:59:56 +0100675 pointer_type = self.visit(pointer.type)
676 if pointer_type is pointer.type:
677 return pointer
678 else:
679 return Pointer(pointer_type)
José Fonsecac9edb832010-11-20 09:03:10 +0000680
José Fonseca59ee88e2012-01-15 14:24:10 +0000681 def visitIntPointer(self, pointer):
682 return pointer
683
José Fonsecafbcf6832012-04-05 07:10:30 +0100684 def visitObjPointer(self, pointer):
José Fonsecaf182eda2012-04-05 19:59:56 +0100685 pointer_type = self.visit(pointer.type)
686 if pointer_type is pointer.type:
687 return pointer
688 else:
689 return ObjPointer(pointer_type)
José Fonsecafbcf6832012-04-05 07:10:30 +0100690
José Fonseca59ee88e2012-01-15 14:24:10 +0000691 def visitLinearPointer(self, pointer):
José Fonsecaf182eda2012-04-05 19:59:56 +0100692 pointer_type = self.visit(pointer.type)
693 if pointer_type is pointer.type:
694 return pointer
695 else:
696 return LinearPointer(pointer_type)
José Fonseca59ee88e2012-01-15 14:24:10 +0000697
José Fonsecab89c5932012-04-01 22:47:11 +0200698 def visitReference(self, reference):
José Fonsecaf182eda2012-04-05 19:59:56 +0100699 reference_type = self.visit(reference.type)
700 if reference_type is reference.type:
701 return reference
702 else:
703 return Reference(reference_type)
José Fonsecab89c5932012-04-01 22:47:11 +0200704
José Fonseca54f304a2012-01-14 19:33:08 +0000705 def visitHandle(self, handle):
José Fonsecaf182eda2012-04-05 19:59:56 +0100706 handle_type = self.visit(handle.type)
707 if handle_type is handle.type:
708 return handle
709 else:
710 return Handle(handle.name, handle_type, range=handle.range, key=handle.key)
José Fonseca50d78d82010-11-23 22:13:14 +0000711
José Fonseca54f304a2012-01-14 19:33:08 +0000712 def visitAlias(self, alias):
José Fonsecaf182eda2012-04-05 19:59:56 +0100713 alias_type = self.visit(alias.type)
714 if alias_type is alias.type:
715 return alias
716 else:
717 return Alias(alias.expr, alias_type)
José Fonsecac9edb832010-11-20 09:03:10 +0000718
José Fonseca54f304a2012-01-14 19:33:08 +0000719 def visitOpaque(self, opaque):
José Fonsecac9edb832010-11-20 09:03:10 +0000720 return opaque
721
José Fonseca7814edf2012-01-31 10:55:49 +0000722 def visitInterface(self, interface, *args, **kwargs):
723 return interface
724
José Fonseca54f304a2012-01-14 19:33:08 +0000725 def visitPolymorphic(self, polymorphic):
José Fonseca54f304a2012-01-14 19:33:08 +0000726 switchExpr = polymorphic.switchExpr
727 switchTypes = [(expr, self.visit(type)) for expr, type in polymorphic.switchTypes]
José Fonsecaeb216e62012-11-20 11:08:08 +0000728 if polymorphic.defaultType is None:
729 defaultType = None
730 else:
731 defaultType = self.visit(polymorphic.defaultType)
José Fonsecab95e3722012-04-16 14:01:15 +0100732 return Polymorphic(switchExpr, switchTypes, defaultType, polymorphic.contextLess)
José Fonseca16d46dd2011-10-13 09:52:52 +0100733
José Fonsecac9edb832010-11-20 09:03:10 +0000734
José Fonseca0075f152012-04-14 20:25:52 +0100735class MutableRebuilder(Rebuilder):
736 '''Type visitor which derives a mutable type.'''
737
José Fonsecabcfc81b2012-08-07 21:07:22 +0100738 def visitString(self, string):
739 return string
740
José Fonseca0075f152012-04-14 20:25:52 +0100741 def visitConst(self, const):
742 # Strip out const qualifier
743 return const.type
744
745 def visitAlias(self, alias):
746 # Tear the alias on type changes
747 type = self.visit(alias.type)
748 if type is alias.type:
749 return alias
750 return type
751
752 def visitReference(self, reference):
753 # Strip out references
754 return reference.type
755
756
757class Traverser(Visitor):
758 '''Visitor which all types.'''
759
760 def visitVoid(self, void, *args, **kwargs):
761 pass
762
763 def visitLiteral(self, literal, *args, **kwargs):
764 pass
765
766 def visitString(self, string, *args, **kwargs):
767 pass
768
769 def visitConst(self, const, *args, **kwargs):
770 self.visit(const.type, *args, **kwargs)
771
772 def visitStruct(self, struct, *args, **kwargs):
773 for type, name in struct.members:
774 self.visit(type, *args, **kwargs)
775
776 def visitArray(self, array, *args, **kwargs):
777 self.visit(array.type, *args, **kwargs)
778
779 def visitBlob(self, array, *args, **kwargs):
780 pass
781
782 def visitEnum(self, enum, *args, **kwargs):
783 pass
784
785 def visitBitmask(self, bitmask, *args, **kwargs):
786 self.visit(bitmask.type, *args, **kwargs)
787
788 def visitPointer(self, pointer, *args, **kwargs):
789 self.visit(pointer.type, *args, **kwargs)
790
791 def visitIntPointer(self, pointer, *args, **kwargs):
792 pass
793
794 def visitObjPointer(self, pointer, *args, **kwargs):
795 self.visit(pointer.type, *args, **kwargs)
796
797 def visitLinearPointer(self, pointer, *args, **kwargs):
798 self.visit(pointer.type, *args, **kwargs)
799
800 def visitReference(self, reference, *args, **kwargs):
801 self.visit(reference.type, *args, **kwargs)
802
803 def visitHandle(self, handle, *args, **kwargs):
804 self.visit(handle.type, *args, **kwargs)
805
806 def visitAlias(self, alias, *args, **kwargs):
807 self.visit(alias.type, *args, **kwargs)
808
809 def visitOpaque(self, opaque, *args, **kwargs):
810 pass
811
812 def visitInterface(self, interface, *args, **kwargs):
813 if interface.base is not None:
814 self.visit(interface.base, *args, **kwargs)
815 for method in interface.iterMethods():
816 for arg in method.args:
817 self.visit(arg.type, *args, **kwargs)
818 self.visit(method.type, *args, **kwargs)
819
820 def visitPolymorphic(self, polymorphic, *args, **kwargs):
José Fonseca0075f152012-04-14 20:25:52 +0100821 for expr, type in polymorphic.switchTypes:
822 self.visit(type, *args, **kwargs)
José Fonsecaeb216e62012-11-20 11:08:08 +0000823 if polymorphic.defaultType is not None:
824 self.visit(polymorphic.defaultType, *args, **kwargs)
José Fonseca0075f152012-04-14 20:25:52 +0100825
826
827class Collector(Traverser):
José Fonseca9c4a2572012-01-13 23:21:10 +0000828 '''Visitor which collects all unique types as it traverses them.'''
José Fonsecae6a50bd2010-11-24 10:12:22 +0000829
830 def __init__(self):
831 self.__visited = set()
832 self.types = []
833
834 def visit(self, type):
835 if type in self.__visited:
836 return
837 self.__visited.add(type)
838 Visitor.visit(self, type)
839 self.types.append(type)
840
José Fonseca16d46dd2011-10-13 09:52:52 +0100841
José Fonsecadbf714b2012-11-20 17:03:43 +0000842class ExpanderMixin:
843 '''Mixin class that provides a bunch of methods to expand C expressions
844 from the specifications.'''
845
846 __structs = None
847 __indices = None
848
849 def expand(self, expr):
850 # Expand a C expression, replacing certain variables
851 if not isinstance(expr, basestring):
852 return expr
853 variables = {}
854
855 if self.__structs is not None:
856 variables['self'] = '(%s)' % self.__structs[0]
857 if self.__indices is not None:
858 variables['i'] = self.__indices[0]
859
860 expandedExpr = expr.format(**variables)
861 if expandedExpr != expr and 0:
862 sys.stderr.write(" %r -> %r\n" % (expr, expandedExpr))
863 return expandedExpr
864
865 def visitMember(self, member, structInstance, *args, **kwargs):
866 memberType, memberName = member
867 if memberName is None:
868 # Anonymous structure/union member
869 memberInstance = structInstance
870 else:
871 memberInstance = '(%s).%s' % (structInstance, memberName)
872 self.__structs = (structInstance, self.__structs)
873 try:
874 return self.visit(memberType, memberInstance, *args, **kwargs)
875 finally:
876 _, self.__structs = self.__structs
877
878 def visitElement(self, elementIndex, elementType, *args, **kwargs):
879 self.__indices = (elementIndex, self.__indices)
880 try:
881 return self.visit(elementType, *args, **kwargs)
882 finally:
883 _, self.__indices = self.__indices
884
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000885
José Fonseca81301932012-11-11 00:10:20 +0000886class Module:
887 '''A collection of functions.'''
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000888
José Fonseca68ec4122011-02-20 11:25:25 +0000889 def __init__(self, name = None):
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000890 self.name = name
891 self.headers = []
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000892 self.functions = []
893 self.interfaces = []
894
José Fonseca54f304a2012-01-14 19:33:08 +0000895 def addFunctions(self, functions):
José Fonseca81301932012-11-11 00:10:20 +0000896 self.functions.extend(functions)
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000897
José Fonseca54f304a2012-01-14 19:33:08 +0000898 def addInterfaces(self, interfaces):
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000899 self.interfaces.extend(interfaces)
900
José Fonseca81301932012-11-11 00:10:20 +0000901 def mergeModule(self, module):
902 self.headers.extend(module.headers)
903 self.functions.extend(module.functions)
904 self.interfaces.extend(module.interfaces)
José Fonseca68ec4122011-02-20 11:25:25 +0000905
José Fonseca1b6c8752012-04-15 14:33:00 +0100906 def getFunctionByName(self, name):
José Fonsecaeccec3e2011-02-20 09:01:25 +0000907 for function in self.functions:
908 if function.name == name:
909 return function
910 return None
911
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000912
José Fonseca81301932012-11-11 00:10:20 +0000913class API:
914 '''API abstraction.
915
916 Essentially, a collection of types, functions, and interfaces.
917 '''
918
919 def __init__(self, modules = None):
920 self.modules = []
921 if modules is not None:
922 self.modules.extend(modules)
923
924 def getAllTypes(self):
925 collector = Collector()
926 for module in self.modules:
927 for function in module.functions:
928 for arg in function.args:
929 collector.visit(arg.type)
930 collector.visit(function.type)
931 for interface in module.interfaces:
932 collector.visit(interface)
933 for method in interface.iterMethods():
934 for arg in method.args:
935 collector.visit(arg.type)
936 collector.visit(method.type)
937 return collector.types
938
939 def getAllFunctions(self):
940 functions = []
941 for module in self.modules:
942 functions.extend(module.functions)
943 return functions
944
945 def getAllInterfaces(self):
946 types = self.getAllTypes()
947 interfaces = [type for type in types if isinstance(type, Interface)]
948 for module in self.modules:
949 for interface in module.interfaces:
950 if interface not in interfaces:
951 interfaces.append(interface)
952 return interfaces
953
954 def addModule(self, module):
955 self.modules.append(module)
956
957 def getFunctionByName(self, name):
958 for module in self.modules:
959 for function in module.functions:
960 if function.name == name:
961 return function
962 return None
963
964
José Fonseca280a1762012-01-31 15:10:13 +0000965# C string (i.e., zero terminated)
José Fonsecabcfc81b2012-08-07 21:07:22 +0100966CString = String(Char)
967WString = String(WChar, wide=True)
968ConstCString = String(Const(Char))
969ConstWString = String(Const(WChar), wide=True)