blob: 5d5d57004a148d66a0e96885ed16f0dd266a71b6 [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 Hartmetzb936e552013-07-08 12:36:11 +0200275 def __init__(self, keyType, valueTypes, isConst = True):
276 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
283
284 def visit(self, visitor, *args, **kwargs):
285 return visitor.visitAttribArray(self, *args, **kwargs)
286
287
José Fonseca6fac5ae2010-11-29 16:09:13 +0000288class Blob(Type):
289
290 def __init__(self, type, size):
291 Type.__init__(self, type.expr + ' *')
292 self.type = type
293 self.size = size
294
295 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000296 return visitor.visitBlob(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000297
298
299class Struct(Type):
300
José Fonseca02c25002011-10-15 13:17:26 +0100301 __id = 0
302
José Fonseca6fac5ae2010-11-29 16:09:13 +0000303 def __init__(self, name, members):
304 Type.__init__(self, name)
José Fonseca02c25002011-10-15 13:17:26 +0100305
306 self.id = Struct.__id
307 Struct.__id += 1
308
José Fonseca6fac5ae2010-11-29 16:09:13 +0000309 self.name = name
José Fonsecadbf714b2012-11-20 17:03:43 +0000310 self.members = members
José Fonseca6fac5ae2010-11-29 16:09:13 +0000311
312 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000313 return visitor.visitStruct(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000314
315
José Fonsecadbf714b2012-11-20 17:03:43 +0000316def Union(kindExpr, kindTypes, contextLess=True):
José Fonsecaeb216e62012-11-20 11:08:08 +0000317 switchTypes = []
318 for kindCase, kindType, kindMemberName in kindTypes:
319 switchType = Struct(None, [(kindType, kindMemberName)])
320 switchTypes.append((kindCase, switchType))
321 return Polymorphic(kindExpr, switchTypes, contextLess=contextLess)
322
José Fonseca5b6fb752012-04-14 14:56:45 +0100323
José Fonseca6fac5ae2010-11-29 16:09:13 +0000324class Alias(Type):
325
326 def __init__(self, expr, type):
327 Type.__init__(self, expr)
328 self.type = type
329
330 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000331 return visitor.visitAlias(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000332
José Fonseca6fac5ae2010-11-29 16:09:13 +0000333class Arg:
334
José Fonseca9dd8f702012-04-07 10:42:50 +0100335 def __init__(self, type, name, input=True, output=False):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000336 self.type = type
337 self.name = name
José Fonseca9dd8f702012-04-07 10:42:50 +0100338 self.input = input
José Fonseca6fac5ae2010-11-29 16:09:13 +0000339 self.output = output
340 self.index = None
341
342 def __str__(self):
343 return '%s %s' % (self.type, self.name)
344
345
José Fonseca9dd8f702012-04-07 10:42:50 +0100346def In(type, name):
347 return Arg(type, name, input=True, output=False)
348
349def Out(type, name):
350 return Arg(type, name, input=False, output=True)
351
352def InOut(type, name):
353 return Arg(type, name, input=True, output=True)
354
355
José Fonseca6fac5ae2010-11-29 16:09:13 +0000356class Function:
357
José Fonseca84cea3b2012-05-09 21:12:30 +0100358 def __init__(self, type, name, args, call = '', fail = None, sideeffects=True, internal=False):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000359 self.type = type
360 self.name = name
361
362 self.args = []
363 index = 0
364 for arg in args:
José Fonseca8384ccb2011-05-25 10:12:02 +0100365 if not isinstance(arg, Arg):
366 if isinstance(arg, tuple):
367 arg_type, arg_name = arg
368 else:
369 arg_type = arg
370 arg_name = "arg%u" % index
José Fonseca6fac5ae2010-11-29 16:09:13 +0000371 arg = Arg(arg_type, arg_name)
372 arg.index = index
373 index += 1
374 self.args.append(arg)
375
376 self.call = call
377 self.fail = fail
378 self.sideeffects = sideeffects
José Fonseca84cea3b2012-05-09 21:12:30 +0100379 self.internal = internal
José Fonseca6fac5ae2010-11-29 16:09:13 +0000380
381 def prototype(self, name=None):
382 if name is not None:
383 name = name.strip()
384 else:
385 name = self.name
386 s = name
387 if self.call:
388 s = self.call + ' ' + s
389 if name.startswith('*'):
390 s = '(' + s + ')'
391 s = self.type.expr + ' ' + s
392 s += "("
393 if self.args:
394 s += ", ".join(["%s %s" % (arg.type, arg.name) for arg in self.args])
395 else:
396 s += "void"
397 s += ")"
398 return s
399
José Fonseca568ecc22012-01-15 13:57:03 +0000400 def argNames(self):
401 return [arg.name for arg in self.args]
402
José Fonseca999284f2013-02-19 13:29:26 +0000403 def getArgByName(self, name):
404 for arg in self.args:
405 if arg.name == name:
406 return arg
407 return None
408
José Fonseca6fac5ae2010-11-29 16:09:13 +0000409
410def StdFunction(*args, **kwargs):
411 kwargs.setdefault('call', '__stdcall')
412 return Function(*args, **kwargs)
413
414
415def FunctionPointer(type, name, args, **kwargs):
416 # XXX: We should probably treat function pointers (callbacks or not) in a generic fashion
417 return Opaque(name)
418
419
420class Interface(Type):
421
422 def __init__(self, name, base=None):
423 Type.__init__(self, name)
424 self.name = name
425 self.base = base
426 self.methods = []
427
428 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000429 return visitor.visitInterface(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000430
José Fonsecabd086342012-04-18 19:58:32 +0100431 def getMethodByName(self, name):
José Fonsecad275d0a2012-04-30 23:18:05 +0100432 for method in self.iterMethods():
433 if method.name == name:
434 return method
José Fonsecabd086342012-04-18 19:58:32 +0100435 return None
436
José Fonseca54f304a2012-01-14 19:33:08 +0000437 def iterMethods(self):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000438 if self.base is not None:
José Fonseca54f304a2012-01-14 19:33:08 +0000439 for method in self.base.iterMethods():
José Fonseca6fac5ae2010-11-29 16:09:13 +0000440 yield method
441 for method in self.methods:
442 yield method
443 raise StopIteration
444
José Fonseca143e9252012-04-15 09:31:18 +0100445 def iterBases(self):
446 iface = self
447 while iface is not None:
448 yield iface
449 iface = iface.base
450 raise StopIteration
451
José Fonseca5abf5602013-05-30 14:00:44 +0100452 def hasBase(self, *bases):
453 for iface in self.iterBases():
454 if iface in bases:
455 return True
456 return False
457
José Fonseca4220b1b2012-02-03 19:05:29 +0000458 def iterBaseMethods(self):
459 if self.base is not None:
460 for iface, method in self.base.iterBaseMethods():
461 yield iface, method
462 for method in self.methods:
463 yield self, method
464 raise StopIteration
465
José Fonseca6fac5ae2010-11-29 16:09:13 +0000466
467class Method(Function):
468
José Fonseca43aa19f2012-11-10 09:29:38 +0000469 def __init__(self, type, name, args, call = '', const=False, sideeffects=True):
470 assert call == '__stdcall'
José Fonseca5b6fb752012-04-14 14:56:45 +0100471 Function.__init__(self, type, name, args, call = call, sideeffects=sideeffects)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000472 for index in range(len(self.args)):
473 self.args[index].index = index + 1
José Fonseca9dbeda62012-02-03 19:05:54 +0000474 self.const = const
475
476 def prototype(self, name=None):
477 s = Function.prototype(self, name)
478 if self.const:
479 s += ' const'
480 return s
José Fonseca6fac5ae2010-11-29 16:09:13 +0000481
José Fonsecabcb26b22012-04-15 08:42:25 +0100482
José Fonseca5b6fb752012-04-14 14:56:45 +0100483def StdMethod(*args, **kwargs):
484 kwargs.setdefault('call', '__stdcall')
485 return Method(*args, **kwargs)
486
José Fonseca6fac5ae2010-11-29 16:09:13 +0000487
José Fonseca6fac5ae2010-11-29 16:09:13 +0000488class String(Type):
José Fonsecabcfc81b2012-08-07 21:07:22 +0100489 '''Human-legible character string.'''
José Fonseca6fac5ae2010-11-29 16:09:13 +0000490
José Fonsecabcfc81b2012-08-07 21:07:22 +0100491 def __init__(self, type = Char, length = None, wide = False):
492 assert isinstance(type, Type)
493 Type.__init__(self, type.expr + ' *')
494 self.type = type
José Fonseca6fac5ae2010-11-29 16:09:13 +0000495 self.length = length
José Fonsecabcfc81b2012-08-07 21:07:22 +0100496 self.wide = wide
José Fonseca6fac5ae2010-11-29 16:09:13 +0000497
498 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000499 return visitor.visitString(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000500
José Fonseca6fac5ae2010-11-29 16:09:13 +0000501
502class Opaque(Type):
503 '''Opaque pointer.'''
504
505 def __init__(self, expr):
506 Type.__init__(self, expr)
507
508 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000509 return visitor.visitOpaque(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000510
511
512def OpaquePointer(type, *args):
513 return Opaque(type.expr + ' *')
514
515def OpaqueArray(type, size):
516 return Opaque(type.expr + ' *')
517
518def OpaqueBlob(type, size):
519 return Opaque(type.expr + ' *')
José Fonseca8a56d142008-07-09 12:18:08 +0900520
José Fonseca501f2862010-11-19 20:41:18 +0000521
José Fonseca16d46dd2011-10-13 09:52:52 +0100522class Polymorphic(Type):
523
José Fonsecaeb216e62012-11-20 11:08:08 +0000524 def __init__(self, switchExpr, switchTypes, defaultType=None, contextLess=True):
525 if defaultType is None:
526 Type.__init__(self, None)
527 contextLess = False
528 else:
529 Type.__init__(self, defaultType.expr)
José Fonseca54f304a2012-01-14 19:33:08 +0000530 self.switchExpr = switchExpr
531 self.switchTypes = switchTypes
José Fonsecab95e3722012-04-16 14:01:15 +0100532 self.defaultType = defaultType
533 self.contextLess = contextLess
José Fonseca16d46dd2011-10-13 09:52:52 +0100534
535 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000536 return visitor.visitPolymorphic(self, *args, **kwargs)
José Fonseca16d46dd2011-10-13 09:52:52 +0100537
José Fonseca54f304a2012-01-14 19:33:08 +0000538 def iterSwitch(self):
José Fonsecaeb216e62012-11-20 11:08:08 +0000539 cases = []
540 types = []
541
542 if self.defaultType is not None:
543 cases.append(['default'])
544 types.append(self.defaultType)
José Fonseca46161112011-10-14 10:04:55 +0100545
José Fonseca54f304a2012-01-14 19:33:08 +0000546 for expr, type in self.switchTypes:
José Fonseca46161112011-10-14 10:04:55 +0100547 case = 'case %s' % expr
548 try:
549 i = types.index(type)
550 except ValueError:
551 cases.append([case])
552 types.append(type)
553 else:
554 cases[i].append(case)
555
556 return zip(cases, types)
557
José Fonseca16d46dd2011-10-13 09:52:52 +0100558
José Fonsecab95e3722012-04-16 14:01:15 +0100559def EnumPolymorphic(enumName, switchExpr, switchTypes, defaultType, contextLess=True):
560 enumValues = [expr for expr, type in switchTypes]
561 enum = Enum(enumName, enumValues)
562 polymorphic = Polymorphic(switchExpr, switchTypes, defaultType, contextLess)
563 return enum, polymorphic
564
565
José Fonseca501f2862010-11-19 20:41:18 +0000566class Visitor:
José Fonseca9c4a2572012-01-13 23:21:10 +0000567 '''Abstract visitor for the type hierarchy.'''
José Fonseca501f2862010-11-19 20:41:18 +0000568
569 def visit(self, type, *args, **kwargs):
570 return type.visit(self, *args, **kwargs)
571
José Fonseca54f304a2012-01-14 19:33:08 +0000572 def visitVoid(self, void, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000573 raise NotImplementedError
574
José Fonseca54f304a2012-01-14 19:33:08 +0000575 def visitLiteral(self, literal, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000576 raise NotImplementedError
577
José Fonseca54f304a2012-01-14 19:33:08 +0000578 def visitString(self, string, *args, **kwargs):
José Fonseca2defc982010-11-22 16:59:10 +0000579 raise NotImplementedError
580
José Fonseca54f304a2012-01-14 19:33:08 +0000581 def visitConst(self, const, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000582 raise NotImplementedError
583
José Fonseca54f304a2012-01-14 19:33:08 +0000584 def visitStruct(self, struct, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000585 raise NotImplementedError
586
José Fonseca54f304a2012-01-14 19:33:08 +0000587 def visitArray(self, array, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000588 raise NotImplementedError
589
Andreas Hartmetzba7bb0d2013-07-07 22:51:12 +0200590 def visitAttribArray(self, array, *args, **kwargs):
591 raise NotImplementedError
592
José Fonseca54f304a2012-01-14 19:33:08 +0000593 def visitBlob(self, blob, *args, **kwargs):
José Fonseca885f2652010-11-20 11:22:25 +0000594 raise NotImplementedError
595
José Fonseca54f304a2012-01-14 19:33:08 +0000596 def visitEnum(self, enum, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000597 raise NotImplementedError
598
José Fonseca54f304a2012-01-14 19:33:08 +0000599 def visitBitmask(self, bitmask, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000600 raise NotImplementedError
601
José Fonseca54f304a2012-01-14 19:33:08 +0000602 def visitPointer(self, pointer, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000603 raise NotImplementedError
604
José Fonseca59ee88e2012-01-15 14:24:10 +0000605 def visitIntPointer(self, pointer, *args, **kwargs):
606 raise NotImplementedError
607
José Fonsecafbcf6832012-04-05 07:10:30 +0100608 def visitObjPointer(self, pointer, *args, **kwargs):
609 raise NotImplementedError
610
José Fonseca59ee88e2012-01-15 14:24:10 +0000611 def visitLinearPointer(self, pointer, *args, **kwargs):
612 raise NotImplementedError
613
José Fonsecab89c5932012-04-01 22:47:11 +0200614 def visitReference(self, reference, *args, **kwargs):
615 raise NotImplementedError
616
José Fonseca54f304a2012-01-14 19:33:08 +0000617 def visitHandle(self, handle, *args, **kwargs):
José Fonseca50d78d82010-11-23 22:13:14 +0000618 raise NotImplementedError
619
José Fonseca54f304a2012-01-14 19:33:08 +0000620 def visitAlias(self, alias, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000621 raise NotImplementedError
622
José Fonseca54f304a2012-01-14 19:33:08 +0000623 def visitOpaque(self, opaque, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000624 raise NotImplementedError
625
José Fonseca54f304a2012-01-14 19:33:08 +0000626 def visitInterface(self, interface, *args, **kwargs):
José Fonsecac356d6a2010-11-23 14:27:25 +0000627 raise NotImplementedError
628
José Fonseca54f304a2012-01-14 19:33:08 +0000629 def visitPolymorphic(self, polymorphic, *args, **kwargs):
José Fonseca16d46dd2011-10-13 09:52:52 +0100630 raise NotImplementedError
José Fonseca54f304a2012-01-14 19:33:08 +0000631 #return self.visit(polymorphic.defaultType, *args, **kwargs)
José Fonseca16d46dd2011-10-13 09:52:52 +0100632
José Fonsecac356d6a2010-11-23 14:27:25 +0000633
634class OnceVisitor(Visitor):
José Fonseca9c4a2572012-01-13 23:21:10 +0000635 '''Visitor that guarantees that each type is visited only once.'''
José Fonsecac356d6a2010-11-23 14:27:25 +0000636
637 def __init__(self):
638 self.__visited = set()
639
640 def visit(self, type, *args, **kwargs):
641 if type not in self.__visited:
642 self.__visited.add(type)
643 return type.visit(self, *args, **kwargs)
644 return None
645
José Fonseca501f2862010-11-19 20:41:18 +0000646
José Fonsecac9edb832010-11-20 09:03:10 +0000647class Rebuilder(Visitor):
José Fonseca9c4a2572012-01-13 23:21:10 +0000648 '''Visitor which rebuild types as it visits them.
649
650 By itself it is a no-op -- it is intended to be overwritten.
651 '''
José Fonsecac9edb832010-11-20 09:03:10 +0000652
José Fonseca54f304a2012-01-14 19:33:08 +0000653 def visitVoid(self, void):
José Fonsecac9edb832010-11-20 09:03:10 +0000654 return void
655
José Fonseca54f304a2012-01-14 19:33:08 +0000656 def visitLiteral(self, literal):
José Fonsecac9edb832010-11-20 09:03:10 +0000657 return literal
658
José Fonseca54f304a2012-01-14 19:33:08 +0000659 def visitString(self, string):
José Fonsecabcfc81b2012-08-07 21:07:22 +0100660 string_type = self.visit(string.type)
661 if string_type is string.type:
662 return string
663 else:
664 return String(string_type, string.length, string.wide)
José Fonseca2defc982010-11-22 16:59:10 +0000665
José Fonseca54f304a2012-01-14 19:33:08 +0000666 def visitConst(self, const):
José Fonsecaf182eda2012-04-05 19:59:56 +0100667 const_type = self.visit(const.type)
668 if const_type is const.type:
669 return const
670 else:
671 return Const(const_type)
José Fonsecac9edb832010-11-20 09:03:10 +0000672
José Fonseca54f304a2012-01-14 19:33:08 +0000673 def visitStruct(self, struct):
José Fonseca06aa2842011-05-05 07:55:54 +0100674 members = [(self.visit(type), name) for type, name in struct.members]
José Fonsecac9edb832010-11-20 09:03:10 +0000675 return Struct(struct.name, members)
676
José Fonseca54f304a2012-01-14 19:33:08 +0000677 def visitArray(self, array):
José Fonsecac9edb832010-11-20 09:03:10 +0000678 type = self.visit(array.type)
679 return Array(type, array.length)
680
José Fonseca54f304a2012-01-14 19:33:08 +0000681 def visitBlob(self, blob):
José Fonseca885f2652010-11-20 11:22:25 +0000682 type = self.visit(blob.type)
683 return Blob(type, blob.size)
684
José Fonseca54f304a2012-01-14 19:33:08 +0000685 def visitEnum(self, enum):
José Fonsecac9edb832010-11-20 09:03:10 +0000686 return enum
687
José Fonseca54f304a2012-01-14 19:33:08 +0000688 def visitBitmask(self, bitmask):
José Fonsecac9edb832010-11-20 09:03:10 +0000689 type = self.visit(bitmask.type)
690 return Bitmask(type, bitmask.values)
691
José Fonseca54f304a2012-01-14 19:33:08 +0000692 def visitPointer(self, pointer):
José Fonsecaf182eda2012-04-05 19:59:56 +0100693 pointer_type = self.visit(pointer.type)
694 if pointer_type is pointer.type:
695 return pointer
696 else:
697 return Pointer(pointer_type)
José Fonsecac9edb832010-11-20 09:03:10 +0000698
José Fonseca59ee88e2012-01-15 14:24:10 +0000699 def visitIntPointer(self, pointer):
700 return pointer
701
José Fonsecafbcf6832012-04-05 07:10:30 +0100702 def visitObjPointer(self, pointer):
José Fonsecaf182eda2012-04-05 19:59:56 +0100703 pointer_type = self.visit(pointer.type)
704 if pointer_type is pointer.type:
705 return pointer
706 else:
707 return ObjPointer(pointer_type)
José Fonsecafbcf6832012-04-05 07:10:30 +0100708
José Fonseca59ee88e2012-01-15 14:24:10 +0000709 def visitLinearPointer(self, pointer):
José Fonsecaf182eda2012-04-05 19:59:56 +0100710 pointer_type = self.visit(pointer.type)
711 if pointer_type is pointer.type:
712 return pointer
713 else:
714 return LinearPointer(pointer_type)
José Fonseca59ee88e2012-01-15 14:24:10 +0000715
José Fonsecab89c5932012-04-01 22:47:11 +0200716 def visitReference(self, reference):
José Fonsecaf182eda2012-04-05 19:59:56 +0100717 reference_type = self.visit(reference.type)
718 if reference_type is reference.type:
719 return reference
720 else:
721 return Reference(reference_type)
José Fonsecab89c5932012-04-01 22:47:11 +0200722
José Fonseca54f304a2012-01-14 19:33:08 +0000723 def visitHandle(self, handle):
José Fonsecaf182eda2012-04-05 19:59:56 +0100724 handle_type = self.visit(handle.type)
725 if handle_type is handle.type:
726 return handle
727 else:
728 return Handle(handle.name, handle_type, range=handle.range, key=handle.key)
José Fonseca50d78d82010-11-23 22:13:14 +0000729
José Fonseca54f304a2012-01-14 19:33:08 +0000730 def visitAlias(self, alias):
José Fonsecaf182eda2012-04-05 19:59:56 +0100731 alias_type = self.visit(alias.type)
732 if alias_type is alias.type:
733 return alias
734 else:
735 return Alias(alias.expr, alias_type)
José Fonsecac9edb832010-11-20 09:03:10 +0000736
José Fonseca54f304a2012-01-14 19:33:08 +0000737 def visitOpaque(self, opaque):
José Fonsecac9edb832010-11-20 09:03:10 +0000738 return opaque
739
José Fonseca7814edf2012-01-31 10:55:49 +0000740 def visitInterface(self, interface, *args, **kwargs):
741 return interface
742
José Fonseca54f304a2012-01-14 19:33:08 +0000743 def visitPolymorphic(self, polymorphic):
José Fonseca54f304a2012-01-14 19:33:08 +0000744 switchExpr = polymorphic.switchExpr
745 switchTypes = [(expr, self.visit(type)) for expr, type in polymorphic.switchTypes]
José Fonsecaeb216e62012-11-20 11:08:08 +0000746 if polymorphic.defaultType is None:
747 defaultType = None
748 else:
749 defaultType = self.visit(polymorphic.defaultType)
José Fonsecab95e3722012-04-16 14:01:15 +0100750 return Polymorphic(switchExpr, switchTypes, defaultType, polymorphic.contextLess)
José Fonseca16d46dd2011-10-13 09:52:52 +0100751
José Fonsecac9edb832010-11-20 09:03:10 +0000752
José Fonseca0075f152012-04-14 20:25:52 +0100753class MutableRebuilder(Rebuilder):
754 '''Type visitor which derives a mutable type.'''
755
José Fonsecabcfc81b2012-08-07 21:07:22 +0100756 def visitString(self, string):
757 return string
758
José Fonseca0075f152012-04-14 20:25:52 +0100759 def visitConst(self, const):
760 # Strip out const qualifier
761 return const.type
762
763 def visitAlias(self, alias):
764 # Tear the alias on type changes
765 type = self.visit(alias.type)
766 if type is alias.type:
767 return alias
768 return type
769
770 def visitReference(self, reference):
771 # Strip out references
772 return reference.type
773
774
775class Traverser(Visitor):
776 '''Visitor which all types.'''
777
778 def visitVoid(self, void, *args, **kwargs):
779 pass
780
781 def visitLiteral(self, literal, *args, **kwargs):
782 pass
783
784 def visitString(self, string, *args, **kwargs):
785 pass
786
787 def visitConst(self, const, *args, **kwargs):
788 self.visit(const.type, *args, **kwargs)
789
790 def visitStruct(self, struct, *args, **kwargs):
791 for type, name in struct.members:
792 self.visit(type, *args, **kwargs)
793
794 def visitArray(self, array, *args, **kwargs):
795 self.visit(array.type, *args, **kwargs)
796
Andreas Hartmetzba7bb0d2013-07-07 22:51:12 +0200797 def visitAttribArray(self, attribs, *args, **kwargs):
798 for key, valueType in attribs.valueTypes:
799 self.visit(valueType, *args, **kwargs)
800
José Fonseca0075f152012-04-14 20:25:52 +0100801 def visitBlob(self, array, *args, **kwargs):
802 pass
803
804 def visitEnum(self, enum, *args, **kwargs):
805 pass
806
807 def visitBitmask(self, bitmask, *args, **kwargs):
808 self.visit(bitmask.type, *args, **kwargs)
809
810 def visitPointer(self, pointer, *args, **kwargs):
811 self.visit(pointer.type, *args, **kwargs)
812
813 def visitIntPointer(self, pointer, *args, **kwargs):
814 pass
815
816 def visitObjPointer(self, pointer, *args, **kwargs):
817 self.visit(pointer.type, *args, **kwargs)
818
819 def visitLinearPointer(self, pointer, *args, **kwargs):
820 self.visit(pointer.type, *args, **kwargs)
821
822 def visitReference(self, reference, *args, **kwargs):
823 self.visit(reference.type, *args, **kwargs)
824
825 def visitHandle(self, handle, *args, **kwargs):
826 self.visit(handle.type, *args, **kwargs)
827
828 def visitAlias(self, alias, *args, **kwargs):
829 self.visit(alias.type, *args, **kwargs)
830
831 def visitOpaque(self, opaque, *args, **kwargs):
832 pass
833
834 def visitInterface(self, interface, *args, **kwargs):
835 if interface.base is not None:
836 self.visit(interface.base, *args, **kwargs)
837 for method in interface.iterMethods():
838 for arg in method.args:
839 self.visit(arg.type, *args, **kwargs)
840 self.visit(method.type, *args, **kwargs)
841
842 def visitPolymorphic(self, polymorphic, *args, **kwargs):
José Fonseca0075f152012-04-14 20:25:52 +0100843 for expr, type in polymorphic.switchTypes:
844 self.visit(type, *args, **kwargs)
José Fonsecaeb216e62012-11-20 11:08:08 +0000845 if polymorphic.defaultType is not None:
846 self.visit(polymorphic.defaultType, *args, **kwargs)
José Fonseca0075f152012-04-14 20:25:52 +0100847
848
849class Collector(Traverser):
José Fonseca9c4a2572012-01-13 23:21:10 +0000850 '''Visitor which collects all unique types as it traverses them.'''
José Fonsecae6a50bd2010-11-24 10:12:22 +0000851
852 def __init__(self):
853 self.__visited = set()
854 self.types = []
855
856 def visit(self, type):
857 if type in self.__visited:
858 return
859 self.__visited.add(type)
860 Visitor.visit(self, type)
861 self.types.append(type)
862
José Fonseca16d46dd2011-10-13 09:52:52 +0100863
José Fonsecadbf714b2012-11-20 17:03:43 +0000864class ExpanderMixin:
865 '''Mixin class that provides a bunch of methods to expand C expressions
866 from the specifications.'''
867
868 __structs = None
869 __indices = None
870
871 def expand(self, expr):
872 # Expand a C expression, replacing certain variables
873 if not isinstance(expr, basestring):
874 return expr
875 variables = {}
876
877 if self.__structs is not None:
878 variables['self'] = '(%s)' % self.__structs[0]
879 if self.__indices is not None:
880 variables['i'] = self.__indices[0]
881
882 expandedExpr = expr.format(**variables)
883 if expandedExpr != expr and 0:
884 sys.stderr.write(" %r -> %r\n" % (expr, expandedExpr))
885 return expandedExpr
886
887 def visitMember(self, member, structInstance, *args, **kwargs):
888 memberType, memberName = member
889 if memberName is None:
890 # Anonymous structure/union member
891 memberInstance = structInstance
892 else:
893 memberInstance = '(%s).%s' % (structInstance, memberName)
894 self.__structs = (structInstance, self.__structs)
895 try:
896 return self.visit(memberType, memberInstance, *args, **kwargs)
897 finally:
898 _, self.__structs = self.__structs
899
900 def visitElement(self, elementIndex, elementType, *args, **kwargs):
901 self.__indices = (elementIndex, self.__indices)
902 try:
903 return self.visit(elementType, *args, **kwargs)
904 finally:
905 _, self.__indices = self.__indices
906
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000907
José Fonseca81301932012-11-11 00:10:20 +0000908class Module:
909 '''A collection of functions.'''
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000910
José Fonseca68ec4122011-02-20 11:25:25 +0000911 def __init__(self, name = None):
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000912 self.name = name
913 self.headers = []
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000914 self.functions = []
915 self.interfaces = []
916
José Fonseca54f304a2012-01-14 19:33:08 +0000917 def addFunctions(self, functions):
José Fonseca81301932012-11-11 00:10:20 +0000918 self.functions.extend(functions)
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000919
José Fonseca54f304a2012-01-14 19:33:08 +0000920 def addInterfaces(self, interfaces):
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000921 self.interfaces.extend(interfaces)
922
José Fonseca81301932012-11-11 00:10:20 +0000923 def mergeModule(self, module):
924 self.headers.extend(module.headers)
925 self.functions.extend(module.functions)
926 self.interfaces.extend(module.interfaces)
José Fonseca68ec4122011-02-20 11:25:25 +0000927
José Fonseca1b6c8752012-04-15 14:33:00 +0100928 def getFunctionByName(self, name):
José Fonsecaeccec3e2011-02-20 09:01:25 +0000929 for function in self.functions:
930 if function.name == name:
931 return function
932 return None
933
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000934
José Fonseca81301932012-11-11 00:10:20 +0000935class API:
936 '''API abstraction.
937
938 Essentially, a collection of types, functions, and interfaces.
939 '''
940
941 def __init__(self, modules = None):
942 self.modules = []
943 if modules is not None:
944 self.modules.extend(modules)
945
946 def getAllTypes(self):
947 collector = Collector()
948 for module in self.modules:
949 for function in module.functions:
950 for arg in function.args:
951 collector.visit(arg.type)
952 collector.visit(function.type)
953 for interface in module.interfaces:
954 collector.visit(interface)
955 for method in interface.iterMethods():
956 for arg in method.args:
957 collector.visit(arg.type)
958 collector.visit(method.type)
959 return collector.types
960
961 def getAllFunctions(self):
962 functions = []
963 for module in self.modules:
964 functions.extend(module.functions)
965 return functions
966
967 def getAllInterfaces(self):
968 types = self.getAllTypes()
969 interfaces = [type for type in types if isinstance(type, Interface)]
970 for module in self.modules:
971 for interface in module.interfaces:
972 if interface not in interfaces:
973 interfaces.append(interface)
974 return interfaces
975
976 def addModule(self, module):
977 self.modules.append(module)
978
979 def getFunctionByName(self, name):
980 for module in self.modules:
981 for function in module.functions:
982 if function.name == name:
983 return function
984 return None
985
986
José Fonseca280a1762012-01-31 15:10:13 +0000987# C string (i.e., zero terminated)
José Fonsecabcfc81b2012-08-07 21:07:22 +0100988CString = String(Char)
989WString = String(WChar, wide=True)
990ConstCString = String(Const(Char))
991ConstWString = String(Const(WChar), wide=True)