blob: baab081bf29e581c861b9eb2020d170b58d05be7 [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
José Fonseca13b93432014-11-18 20:21:23 +000078 def depends(self, other):
79 '''Whether this type depends on another.'''
80
81 collector = Collector()
82 collector.visit(self)
83 return other in collector.types
84
José Fonseca6fac5ae2010-11-29 16:09:13 +000085
86class _Void(Type):
José Fonseca02c25002011-10-15 13:17:26 +010087 """Singleton void type."""
José Fonseca6fac5ae2010-11-29 16:09:13 +000088
89 def __init__(self):
90 Type.__init__(self, "void")
91
92 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +000093 return visitor.visitVoid(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +000094
95Void = _Void()
96
97
98class Literal(Type):
José Fonseca2f2ea482011-10-15 15:10:06 +010099 """Class to describe literal types.
José Fonseca6fac5ae2010-11-29 16:09:13 +0000100
José Fonseca2f2ea482011-10-15 15:10:06 +0100101 Types which are not defined in terms of other types, such as integers and
102 floats."""
103
104 def __init__(self, expr, kind):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000105 Type.__init__(self, expr)
José Fonseca2f2ea482011-10-15 15:10:06 +0100106 self.kind = kind
José Fonseca6fac5ae2010-11-29 16:09:13 +0000107
108 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000109 return visitor.visitLiteral(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000110
111
José Fonsecabcfc81b2012-08-07 21:07:22 +0100112Bool = Literal("bool", "Bool")
113SChar = Literal("signed char", "SInt")
114UChar = Literal("unsigned char", "UInt")
115Short = Literal("short", "SInt")
116Int = Literal("int", "SInt")
117Long = Literal("long", "SInt")
118LongLong = Literal("long long", "SInt")
119UShort = Literal("unsigned short", "UInt")
120UInt = Literal("unsigned int", "UInt")
121ULong = Literal("unsigned long", "UInt")
122ULongLong = Literal("unsigned long long", "UInt")
123Float = Literal("float", "Float")
124Double = Literal("double", "Double")
125SizeT = Literal("size_t", "UInt")
126
127Char = Literal("char", "SInt")
128WChar = Literal("wchar_t", "SInt")
129
130Int8 = Literal("int8_t", "SInt")
131UInt8 = Literal("uint8_t", "UInt")
132Int16 = Literal("int16_t", "SInt")
133UInt16 = Literal("uint16_t", "UInt")
134Int32 = Literal("int32_t", "SInt")
135UInt32 = Literal("uint32_t", "UInt")
136Int64 = Literal("int64_t", "SInt")
137UInt64 = Literal("uint64_t", "UInt")
138
José Fonsecacb9d2e02012-10-19 14:51:48 +0100139IntPtr = Literal("intptr_t", "SInt")
140UIntPtr = Literal("uintptr_t", "UInt")
José Fonsecabcfc81b2012-08-07 21:07:22 +0100141
José Fonseca6fac5ae2010-11-29 16:09:13 +0000142class Const(Type):
143
144 def __init__(self, type):
José Fonseca903c2ca2011-09-23 09:43:05 +0100145 # While "const foo" and "foo const" are synonymous, "const foo *" and
146 # "foo * const" are not quite the same, and some compilers do enforce
147 # strict const correctness.
José Fonsecabcfc81b2012-08-07 21:07:22 +0100148 if type.expr.startswith("const ") or '*' in type.expr:
José Fonseca6fac5ae2010-11-29 16:09:13 +0000149 expr = type.expr + " const"
150 else:
José Fonseca903c2ca2011-09-23 09:43:05 +0100151 # The most legible
José Fonseca6fac5ae2010-11-29 16:09:13 +0000152 expr = "const " + type.expr
153
José Fonseca02c25002011-10-15 13:17:26 +0100154 Type.__init__(self, expr, 'C' + type.tag)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000155
156 self.type = type
157
158 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000159 return visitor.visitConst(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000160
161
162class Pointer(Type):
163
164 def __init__(self, type):
José Fonseca02c25002011-10-15 13:17:26 +0100165 Type.__init__(self, type.expr + " *", 'P' + type.tag)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000166 self.type = type
167
168 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000169 return visitor.visitPointer(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000170
171
José Fonseca59ee88e2012-01-15 14:24:10 +0000172class IntPointer(Type):
173 '''Integer encoded as a pointer.'''
174
175 def visit(self, visitor, *args, **kwargs):
176 return visitor.visitIntPointer(self, *args, **kwargs)
177
178
José Fonsecafbcf6832012-04-05 07:10:30 +0100179class ObjPointer(Type):
180 '''Pointer to an object.'''
181
182 def __init__(self, type):
183 Type.__init__(self, type.expr + " *", 'P' + type.tag)
184 self.type = type
185
186 def visit(self, visitor, *args, **kwargs):
187 return visitor.visitObjPointer(self, *args, **kwargs)
188
189
José Fonseca59ee88e2012-01-15 14:24:10 +0000190class LinearPointer(Type):
José Fonsecafbcf6832012-04-05 07:10:30 +0100191 '''Pointer to a linear range of memory.'''
José Fonseca59ee88e2012-01-15 14:24:10 +0000192
193 def __init__(self, type, size = None):
194 Type.__init__(self, type.expr + " *", 'P' + type.tag)
195 self.type = type
196 self.size = size
197
198 def visit(self, visitor, *args, **kwargs):
199 return visitor.visitLinearPointer(self, *args, **kwargs)
200
201
José Fonsecab89c5932012-04-01 22:47:11 +0200202class Reference(Type):
203 '''C++ references.'''
204
205 def __init__(self, type):
206 Type.__init__(self, type.expr + " &", 'R' + type.tag)
207 self.type = type
208
209 def visit(self, visitor, *args, **kwargs):
210 return visitor.visitReference(self, *args, **kwargs)
211
212
José Fonseca6fac5ae2010-11-29 16:09:13 +0000213class Handle(Type):
214
José Fonseca8a844ae2010-12-06 18:50:52 +0000215 def __init__(self, name, type, range=None, key=None):
José Fonseca02c25002011-10-15 13:17:26 +0100216 Type.__init__(self, type.expr, 'P' + type.tag)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000217 self.name = name
218 self.type = type
219 self.range = range
José Fonseca8a844ae2010-12-06 18:50:52 +0000220 self.key = key
José Fonseca6fac5ae2010-11-29 16:09:13 +0000221
222 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000223 return visitor.visitHandle(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000224
225
226def ConstPointer(type):
227 return Pointer(Const(type))
228
229
230class Enum(Type):
231
José Fonseca02c25002011-10-15 13:17:26 +0100232 __id = 0
233
José Fonseca6fac5ae2010-11-29 16:09:13 +0000234 def __init__(self, name, values):
235 Type.__init__(self, name)
José Fonseca02c25002011-10-15 13:17:26 +0100236
237 self.id = Enum.__id
238 Enum.__id += 1
239
José Fonseca6fac5ae2010-11-29 16:09:13 +0000240 self.values = list(values)
José Fonseca02c25002011-10-15 13:17:26 +0100241
José Fonseca6fac5ae2010-11-29 16:09:13 +0000242 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000243 return visitor.visitEnum(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000244
245
246def FakeEnum(type, values):
247 return Enum(type.expr, values)
248
249
250class Bitmask(Type):
251
José Fonseca02c25002011-10-15 13:17:26 +0100252 __id = 0
253
José Fonseca6fac5ae2010-11-29 16:09:13 +0000254 def __init__(self, type, values):
255 Type.__init__(self, type.expr)
José Fonseca02c25002011-10-15 13:17:26 +0100256
257 self.id = Bitmask.__id
258 Bitmask.__id += 1
259
José Fonseca6fac5ae2010-11-29 16:09:13 +0000260 self.type = type
261 self.values = values
262
263 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000264 return visitor.visitBitmask(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000265
266Flags = Bitmask
267
268
269class Array(Type):
270
271 def __init__(self, type, length):
272 Type.__init__(self, type.expr + " *")
273 self.type = type
274 self.length = length
275
276 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000277 return visitor.visitArray(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000278
279
Andreas Hartmetzba7bb0d2013-07-07 22:51:12 +0200280class AttribArray(Type):
281
José Fonseca48a92b92013-07-20 15:34:24 +0100282 def __init__(self, baseType, valueTypes, terminator = '0'):
José Fonseca77c10d82013-07-20 15:27:29 +0100283 self.baseType = baseType
José Fonseca48a92b92013-07-20 15:34:24 +0100284 Type.__init__(self, (Pointer(self.baseType)).expr)
Andreas Hartmetzba7bb0d2013-07-07 22:51:12 +0200285 self.valueTypes = valueTypes
Andreas Hartmetz7a0de292013-07-09 22:38:29 +0200286 self.terminator = terminator
287 self.hasKeysWithoutValues = False
288 for key, value in valueTypes:
289 if value is None:
290 self.hasKeysWithoutValues = True
Andreas Hartmetzba7bb0d2013-07-07 22:51:12 +0200291
292 def visit(self, visitor, *args, **kwargs):
293 return visitor.visitAttribArray(self, *args, **kwargs)
294
295
José Fonseca6fac5ae2010-11-29 16:09:13 +0000296class Blob(Type):
297
298 def __init__(self, type, size):
299 Type.__init__(self, type.expr + ' *')
300 self.type = type
301 self.size = size
302
303 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000304 return visitor.visitBlob(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000305
306
307class Struct(Type):
308
José Fonseca02c25002011-10-15 13:17:26 +0100309 __id = 0
310
José Fonseca6fac5ae2010-11-29 16:09:13 +0000311 def __init__(self, name, members):
312 Type.__init__(self, name)
José Fonseca02c25002011-10-15 13:17:26 +0100313
314 self.id = Struct.__id
315 Struct.__id += 1
316
José Fonseca6fac5ae2010-11-29 16:09:13 +0000317 self.name = name
José Fonsecadbf714b2012-11-20 17:03:43 +0000318 self.members = members
José Fonseca6fac5ae2010-11-29 16:09:13 +0000319
320 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000321 return visitor.visitStruct(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000322
323
José Fonsecadbf714b2012-11-20 17:03:43 +0000324def Union(kindExpr, kindTypes, contextLess=True):
José Fonsecaeb216e62012-11-20 11:08:08 +0000325 switchTypes = []
326 for kindCase, kindType, kindMemberName in kindTypes:
327 switchType = Struct(None, [(kindType, kindMemberName)])
328 switchTypes.append((kindCase, switchType))
329 return Polymorphic(kindExpr, switchTypes, contextLess=contextLess)
330
José Fonseca5b6fb752012-04-14 14:56:45 +0100331
José Fonseca6fac5ae2010-11-29 16:09:13 +0000332class Alias(Type):
333
334 def __init__(self, expr, type):
335 Type.__init__(self, expr)
336 self.type = type
337
338 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000339 return visitor.visitAlias(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000340
José Fonseca6fac5ae2010-11-29 16:09:13 +0000341class Arg:
342
José Fonseca9dd8f702012-04-07 10:42:50 +0100343 def __init__(self, type, name, input=True, output=False):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000344 self.type = type
345 self.name = name
José Fonseca9dd8f702012-04-07 10:42:50 +0100346 self.input = input
José Fonseca6fac5ae2010-11-29 16:09:13 +0000347 self.output = output
348 self.index = None
349
350 def __str__(self):
351 return '%s %s' % (self.type, self.name)
352
353
José Fonseca9dd8f702012-04-07 10:42:50 +0100354def In(type, name):
355 return Arg(type, name, input=True, output=False)
356
357def Out(type, name):
358 return Arg(type, name, input=False, output=True)
359
360def InOut(type, name):
361 return Arg(type, name, input=True, output=True)
362
363
José Fonseca6fac5ae2010-11-29 16:09:13 +0000364class Function:
365
José Fonseca84cea3b2012-05-09 21:12:30 +0100366 def __init__(self, type, name, args, call = '', fail = None, sideeffects=True, internal=False):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000367 self.type = type
368 self.name = name
369
370 self.args = []
371 index = 0
372 for arg in args:
José Fonseca8384ccb2011-05-25 10:12:02 +0100373 if not isinstance(arg, Arg):
374 if isinstance(arg, tuple):
375 arg_type, arg_name = arg
376 else:
377 arg_type = arg
378 arg_name = "arg%u" % index
José Fonseca6fac5ae2010-11-29 16:09:13 +0000379 arg = Arg(arg_type, arg_name)
380 arg.index = index
381 index += 1
382 self.args.append(arg)
383
384 self.call = call
385 self.fail = fail
386 self.sideeffects = sideeffects
José Fonseca84cea3b2012-05-09 21:12:30 +0100387 self.internal = internal
José Fonseca6fac5ae2010-11-29 16:09:13 +0000388
389 def prototype(self, name=None):
390 if name is not None:
391 name = name.strip()
392 else:
393 name = self.name
394 s = name
395 if self.call:
396 s = self.call + ' ' + s
397 if name.startswith('*'):
398 s = '(' + s + ')'
399 s = self.type.expr + ' ' + s
400 s += "("
401 if self.args:
402 s += ", ".join(["%s %s" % (arg.type, arg.name) for arg in self.args])
403 else:
404 s += "void"
405 s += ")"
406 return s
407
José Fonseca568ecc22012-01-15 13:57:03 +0000408 def argNames(self):
409 return [arg.name for arg in self.args]
410
José Fonseca999284f2013-02-19 13:29:26 +0000411 def getArgByName(self, name):
412 for arg in self.args:
413 if arg.name == name:
414 return arg
415 return None
416
José Fonseca6fac5ae2010-11-29 16:09:13 +0000417
418def StdFunction(*args, **kwargs):
419 kwargs.setdefault('call', '__stdcall')
420 return Function(*args, **kwargs)
421
422
423def FunctionPointer(type, name, args, **kwargs):
424 # XXX: We should probably treat function pointers (callbacks or not) in a generic fashion
425 return Opaque(name)
426
427
428class Interface(Type):
429
430 def __init__(self, name, base=None):
431 Type.__init__(self, name)
432 self.name = name
433 self.base = base
434 self.methods = []
435
436 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000437 return visitor.visitInterface(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000438
José Fonsecabd086342012-04-18 19:58:32 +0100439 def getMethodByName(self, name):
José Fonsecad275d0a2012-04-30 23:18:05 +0100440 for method in self.iterMethods():
441 if method.name == name:
442 return method
José Fonsecabd086342012-04-18 19:58:32 +0100443 return None
444
José Fonseca54f304a2012-01-14 19:33:08 +0000445 def iterMethods(self):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000446 if self.base is not None:
José Fonseca54f304a2012-01-14 19:33:08 +0000447 for method in self.base.iterMethods():
José Fonseca6fac5ae2010-11-29 16:09:13 +0000448 yield method
449 for method in self.methods:
450 yield method
451 raise StopIteration
452
José Fonseca143e9252012-04-15 09:31:18 +0100453 def iterBases(self):
454 iface = self
455 while iface is not None:
456 yield iface
457 iface = iface.base
458 raise StopIteration
459
José Fonseca5abf5602013-05-30 14:00:44 +0100460 def hasBase(self, *bases):
461 for iface in self.iterBases():
462 if iface in bases:
463 return True
464 return False
465
José Fonseca4220b1b2012-02-03 19:05:29 +0000466 def iterBaseMethods(self):
467 if self.base is not None:
468 for iface, method in self.base.iterBaseMethods():
469 yield iface, method
470 for method in self.methods:
471 yield self, method
472 raise StopIteration
473
José Fonseca6fac5ae2010-11-29 16:09:13 +0000474
475class Method(Function):
476
José Fonseca43aa19f2012-11-10 09:29:38 +0000477 def __init__(self, type, name, args, call = '', const=False, sideeffects=True):
478 assert call == '__stdcall'
José Fonseca5b6fb752012-04-14 14:56:45 +0100479 Function.__init__(self, type, name, args, call = call, sideeffects=sideeffects)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000480 for index in range(len(self.args)):
481 self.args[index].index = index + 1
José Fonseca9dbeda62012-02-03 19:05:54 +0000482 self.const = const
483
484 def prototype(self, name=None):
485 s = Function.prototype(self, name)
486 if self.const:
487 s += ' const'
488 return s
José Fonseca6fac5ae2010-11-29 16:09:13 +0000489
José Fonsecabcb26b22012-04-15 08:42:25 +0100490
José Fonseca5b6fb752012-04-14 14:56:45 +0100491def StdMethod(*args, **kwargs):
492 kwargs.setdefault('call', '__stdcall')
493 return Method(*args, **kwargs)
494
José Fonseca6fac5ae2010-11-29 16:09:13 +0000495
José Fonseca6fac5ae2010-11-29 16:09:13 +0000496class String(Type):
José Fonsecabcfc81b2012-08-07 21:07:22 +0100497 '''Human-legible character string.'''
José Fonseca6fac5ae2010-11-29 16:09:13 +0000498
José Fonsecabcfc81b2012-08-07 21:07:22 +0100499 def __init__(self, type = Char, length = None, wide = False):
500 assert isinstance(type, Type)
501 Type.__init__(self, type.expr + ' *')
502 self.type = type
José Fonseca6fac5ae2010-11-29 16:09:13 +0000503 self.length = length
José Fonsecabcfc81b2012-08-07 21:07:22 +0100504 self.wide = wide
José Fonseca6fac5ae2010-11-29 16:09:13 +0000505
506 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000507 return visitor.visitString(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000508
José Fonseca6fac5ae2010-11-29 16:09:13 +0000509
510class Opaque(Type):
511 '''Opaque pointer.'''
512
513 def __init__(self, expr):
514 Type.__init__(self, expr)
515
516 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000517 return visitor.visitOpaque(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000518
519
520def OpaquePointer(type, *args):
521 return Opaque(type.expr + ' *')
522
523def OpaqueArray(type, size):
524 return Opaque(type.expr + ' *')
525
526def OpaqueBlob(type, size):
527 return Opaque(type.expr + ' *')
José Fonseca8a56d142008-07-09 12:18:08 +0900528
José Fonseca501f2862010-11-19 20:41:18 +0000529
José Fonseca16d46dd2011-10-13 09:52:52 +0100530class Polymorphic(Type):
531
José Fonsecaeb216e62012-11-20 11:08:08 +0000532 def __init__(self, switchExpr, switchTypes, defaultType=None, contextLess=True):
533 if defaultType is None:
534 Type.__init__(self, None)
535 contextLess = False
536 else:
537 Type.__init__(self, defaultType.expr)
José Fonseca54f304a2012-01-14 19:33:08 +0000538 self.switchExpr = switchExpr
539 self.switchTypes = switchTypes
José Fonsecab95e3722012-04-16 14:01:15 +0100540 self.defaultType = defaultType
541 self.contextLess = contextLess
José Fonseca16d46dd2011-10-13 09:52:52 +0100542
543 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000544 return visitor.visitPolymorphic(self, *args, **kwargs)
José Fonseca16d46dd2011-10-13 09:52:52 +0100545
José Fonseca54f304a2012-01-14 19:33:08 +0000546 def iterSwitch(self):
José Fonsecaeb216e62012-11-20 11:08:08 +0000547 cases = []
548 types = []
549
550 if self.defaultType is not None:
551 cases.append(['default'])
552 types.append(self.defaultType)
José Fonseca46161112011-10-14 10:04:55 +0100553
José Fonseca54f304a2012-01-14 19:33:08 +0000554 for expr, type in self.switchTypes:
José Fonseca46161112011-10-14 10:04:55 +0100555 case = 'case %s' % expr
556 try:
557 i = types.index(type)
558 except ValueError:
559 cases.append([case])
560 types.append(type)
561 else:
562 cases[i].append(case)
563
564 return zip(cases, types)
565
José Fonseca16d46dd2011-10-13 09:52:52 +0100566
José Fonsecab95e3722012-04-16 14:01:15 +0100567def EnumPolymorphic(enumName, switchExpr, switchTypes, defaultType, contextLess=True):
568 enumValues = [expr for expr, type in switchTypes]
569 enum = Enum(enumName, enumValues)
570 polymorphic = Polymorphic(switchExpr, switchTypes, defaultType, contextLess)
571 return enum, polymorphic
572
573
José Fonseca501f2862010-11-19 20:41:18 +0000574class Visitor:
José Fonseca9c4a2572012-01-13 23:21:10 +0000575 '''Abstract visitor for the type hierarchy.'''
José Fonseca501f2862010-11-19 20:41:18 +0000576
577 def visit(self, type, *args, **kwargs):
578 return type.visit(self, *args, **kwargs)
579
José Fonseca54f304a2012-01-14 19:33:08 +0000580 def visitVoid(self, void, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000581 raise NotImplementedError
582
José Fonseca54f304a2012-01-14 19:33:08 +0000583 def visitLiteral(self, literal, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000584 raise NotImplementedError
585
José Fonseca54f304a2012-01-14 19:33:08 +0000586 def visitString(self, string, *args, **kwargs):
José Fonseca2defc982010-11-22 16:59:10 +0000587 raise NotImplementedError
588
José Fonseca54f304a2012-01-14 19:33:08 +0000589 def visitConst(self, const, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000590 raise NotImplementedError
591
José Fonseca54f304a2012-01-14 19:33:08 +0000592 def visitStruct(self, struct, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000593 raise NotImplementedError
594
José Fonseca54f304a2012-01-14 19:33:08 +0000595 def visitArray(self, array, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000596 raise NotImplementedError
597
Andreas Hartmetzba7bb0d2013-07-07 22:51:12 +0200598 def visitAttribArray(self, array, *args, **kwargs):
599 raise NotImplementedError
600
José Fonseca54f304a2012-01-14 19:33:08 +0000601 def visitBlob(self, blob, *args, **kwargs):
José Fonseca885f2652010-11-20 11:22:25 +0000602 raise NotImplementedError
603
José Fonseca54f304a2012-01-14 19:33:08 +0000604 def visitEnum(self, enum, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000605 raise NotImplementedError
606
José Fonseca54f304a2012-01-14 19:33:08 +0000607 def visitBitmask(self, bitmask, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000608 raise NotImplementedError
609
José Fonseca54f304a2012-01-14 19:33:08 +0000610 def visitPointer(self, pointer, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000611 raise NotImplementedError
612
José Fonseca59ee88e2012-01-15 14:24:10 +0000613 def visitIntPointer(self, pointer, *args, **kwargs):
614 raise NotImplementedError
615
José Fonsecafbcf6832012-04-05 07:10:30 +0100616 def visitObjPointer(self, pointer, *args, **kwargs):
617 raise NotImplementedError
618
José Fonseca59ee88e2012-01-15 14:24:10 +0000619 def visitLinearPointer(self, pointer, *args, **kwargs):
620 raise NotImplementedError
621
José Fonsecab89c5932012-04-01 22:47:11 +0200622 def visitReference(self, reference, *args, **kwargs):
623 raise NotImplementedError
624
José Fonseca54f304a2012-01-14 19:33:08 +0000625 def visitHandle(self, handle, *args, **kwargs):
José Fonseca50d78d82010-11-23 22:13:14 +0000626 raise NotImplementedError
627
José Fonseca54f304a2012-01-14 19:33:08 +0000628 def visitAlias(self, alias, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000629 raise NotImplementedError
630
José Fonseca54f304a2012-01-14 19:33:08 +0000631 def visitOpaque(self, opaque, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000632 raise NotImplementedError
633
José Fonseca54f304a2012-01-14 19:33:08 +0000634 def visitInterface(self, interface, *args, **kwargs):
José Fonsecac356d6a2010-11-23 14:27:25 +0000635 raise NotImplementedError
636
José Fonseca54f304a2012-01-14 19:33:08 +0000637 def visitPolymorphic(self, polymorphic, *args, **kwargs):
José Fonseca16d46dd2011-10-13 09:52:52 +0100638 raise NotImplementedError
José Fonseca54f304a2012-01-14 19:33:08 +0000639 #return self.visit(polymorphic.defaultType, *args, **kwargs)
José Fonseca16d46dd2011-10-13 09:52:52 +0100640
José Fonsecac356d6a2010-11-23 14:27:25 +0000641
642class OnceVisitor(Visitor):
José Fonseca9c4a2572012-01-13 23:21:10 +0000643 '''Visitor that guarantees that each type is visited only once.'''
José Fonsecac356d6a2010-11-23 14:27:25 +0000644
645 def __init__(self):
646 self.__visited = set()
647
648 def visit(self, type, *args, **kwargs):
649 if type not in self.__visited:
650 self.__visited.add(type)
651 return type.visit(self, *args, **kwargs)
652 return None
653
José Fonseca501f2862010-11-19 20:41:18 +0000654
José Fonsecac9edb832010-11-20 09:03:10 +0000655class Rebuilder(Visitor):
José Fonseca9c4a2572012-01-13 23:21:10 +0000656 '''Visitor which rebuild types as it visits them.
657
658 By itself it is a no-op -- it is intended to be overwritten.
659 '''
José Fonsecac9edb832010-11-20 09:03:10 +0000660
José Fonseca54f304a2012-01-14 19:33:08 +0000661 def visitVoid(self, void):
José Fonsecac9edb832010-11-20 09:03:10 +0000662 return void
663
José Fonseca54f304a2012-01-14 19:33:08 +0000664 def visitLiteral(self, literal):
José Fonsecac9edb832010-11-20 09:03:10 +0000665 return literal
666
José Fonseca54f304a2012-01-14 19:33:08 +0000667 def visitString(self, string):
José Fonsecabcfc81b2012-08-07 21:07:22 +0100668 string_type = self.visit(string.type)
669 if string_type is string.type:
670 return string
671 else:
672 return String(string_type, string.length, string.wide)
José Fonseca2defc982010-11-22 16:59:10 +0000673
José Fonseca54f304a2012-01-14 19:33:08 +0000674 def visitConst(self, const):
José Fonsecaf182eda2012-04-05 19:59:56 +0100675 const_type = self.visit(const.type)
676 if const_type is const.type:
677 return const
678 else:
679 return Const(const_type)
José Fonsecac9edb832010-11-20 09:03:10 +0000680
José Fonseca54f304a2012-01-14 19:33:08 +0000681 def visitStruct(self, struct):
José Fonseca06aa2842011-05-05 07:55:54 +0100682 members = [(self.visit(type), name) for type, name in struct.members]
José Fonsecac9edb832010-11-20 09:03:10 +0000683 return Struct(struct.name, members)
684
José Fonseca54f304a2012-01-14 19:33:08 +0000685 def visitArray(self, array):
José Fonsecac9edb832010-11-20 09:03:10 +0000686 type = self.visit(array.type)
687 return Array(type, array.length)
688
José Fonseca54f304a2012-01-14 19:33:08 +0000689 def visitBlob(self, blob):
José Fonseca885f2652010-11-20 11:22:25 +0000690 type = self.visit(blob.type)
691 return Blob(type, blob.size)
692
José Fonseca54f304a2012-01-14 19:33:08 +0000693 def visitEnum(self, enum):
José Fonsecac9edb832010-11-20 09:03:10 +0000694 return enum
695
José Fonseca54f304a2012-01-14 19:33:08 +0000696 def visitBitmask(self, bitmask):
José Fonsecac9edb832010-11-20 09:03:10 +0000697 type = self.visit(bitmask.type)
698 return Bitmask(type, bitmask.values)
699
José Fonseca54f304a2012-01-14 19:33:08 +0000700 def visitPointer(self, pointer):
José Fonsecaf182eda2012-04-05 19:59:56 +0100701 pointer_type = self.visit(pointer.type)
702 if pointer_type is pointer.type:
703 return pointer
704 else:
705 return Pointer(pointer_type)
José Fonsecac9edb832010-11-20 09:03:10 +0000706
José Fonseca59ee88e2012-01-15 14:24:10 +0000707 def visitIntPointer(self, pointer):
708 return pointer
709
José Fonsecafbcf6832012-04-05 07:10:30 +0100710 def visitObjPointer(self, pointer):
José Fonsecaf182eda2012-04-05 19:59:56 +0100711 pointer_type = self.visit(pointer.type)
712 if pointer_type is pointer.type:
713 return pointer
714 else:
715 return ObjPointer(pointer_type)
José Fonsecafbcf6832012-04-05 07:10:30 +0100716
José Fonseca59ee88e2012-01-15 14:24:10 +0000717 def visitLinearPointer(self, pointer):
José Fonsecaf182eda2012-04-05 19:59:56 +0100718 pointer_type = self.visit(pointer.type)
719 if pointer_type is pointer.type:
720 return pointer
721 else:
722 return LinearPointer(pointer_type)
José Fonseca59ee88e2012-01-15 14:24:10 +0000723
José Fonsecab89c5932012-04-01 22:47:11 +0200724 def visitReference(self, reference):
José Fonsecaf182eda2012-04-05 19:59:56 +0100725 reference_type = self.visit(reference.type)
726 if reference_type is reference.type:
727 return reference
728 else:
729 return Reference(reference_type)
José Fonsecab89c5932012-04-01 22:47:11 +0200730
José Fonseca54f304a2012-01-14 19:33:08 +0000731 def visitHandle(self, handle):
José Fonsecaf182eda2012-04-05 19:59:56 +0100732 handle_type = self.visit(handle.type)
733 if handle_type is handle.type:
734 return handle
735 else:
736 return Handle(handle.name, handle_type, range=handle.range, key=handle.key)
José Fonseca50d78d82010-11-23 22:13:14 +0000737
José Fonseca54f304a2012-01-14 19:33:08 +0000738 def visitAlias(self, alias):
José Fonsecaf182eda2012-04-05 19:59:56 +0100739 alias_type = self.visit(alias.type)
740 if alias_type is alias.type:
741 return alias
742 else:
743 return Alias(alias.expr, alias_type)
José Fonsecac9edb832010-11-20 09:03:10 +0000744
José Fonseca54f304a2012-01-14 19:33:08 +0000745 def visitOpaque(self, opaque):
José Fonsecac9edb832010-11-20 09:03:10 +0000746 return opaque
747
José Fonseca7814edf2012-01-31 10:55:49 +0000748 def visitInterface(self, interface, *args, **kwargs):
749 return interface
750
José Fonseca54f304a2012-01-14 19:33:08 +0000751 def visitPolymorphic(self, polymorphic):
José Fonseca54f304a2012-01-14 19:33:08 +0000752 switchExpr = polymorphic.switchExpr
753 switchTypes = [(expr, self.visit(type)) for expr, type in polymorphic.switchTypes]
José Fonsecaeb216e62012-11-20 11:08:08 +0000754 if polymorphic.defaultType is None:
755 defaultType = None
756 else:
757 defaultType = self.visit(polymorphic.defaultType)
José Fonsecab95e3722012-04-16 14:01:15 +0100758 return Polymorphic(switchExpr, switchTypes, defaultType, polymorphic.contextLess)
José Fonseca16d46dd2011-10-13 09:52:52 +0100759
José Fonsecac9edb832010-11-20 09:03:10 +0000760
José Fonseca0075f152012-04-14 20:25:52 +0100761class MutableRebuilder(Rebuilder):
762 '''Type visitor which derives a mutable type.'''
763
José Fonsecabcfc81b2012-08-07 21:07:22 +0100764 def visitString(self, string):
765 return string
766
José Fonseca0075f152012-04-14 20:25:52 +0100767 def visitConst(self, const):
768 # Strip out const qualifier
769 return const.type
770
771 def visitAlias(self, alias):
772 # Tear the alias on type changes
773 type = self.visit(alias.type)
774 if type is alias.type:
775 return alias
776 return type
777
778 def visitReference(self, reference):
779 # Strip out references
780 return reference.type
781
782
783class Traverser(Visitor):
784 '''Visitor which all types.'''
785
786 def visitVoid(self, void, *args, **kwargs):
787 pass
788
789 def visitLiteral(self, literal, *args, **kwargs):
790 pass
791
792 def visitString(self, string, *args, **kwargs):
793 pass
794
795 def visitConst(self, const, *args, **kwargs):
796 self.visit(const.type, *args, **kwargs)
797
798 def visitStruct(self, struct, *args, **kwargs):
799 for type, name in struct.members:
800 self.visit(type, *args, **kwargs)
801
802 def visitArray(self, array, *args, **kwargs):
803 self.visit(array.type, *args, **kwargs)
804
Andreas Hartmetzba7bb0d2013-07-07 22:51:12 +0200805 def visitAttribArray(self, attribs, *args, **kwargs):
806 for key, valueType in attribs.valueTypes:
Andreas Hartmetz7a0de292013-07-09 22:38:29 +0200807 if valueType is not None:
808 self.visit(valueType, *args, **kwargs)
Andreas Hartmetzba7bb0d2013-07-07 22:51:12 +0200809
José Fonseca0075f152012-04-14 20:25:52 +0100810 def visitBlob(self, array, *args, **kwargs):
811 pass
812
813 def visitEnum(self, enum, *args, **kwargs):
814 pass
815
816 def visitBitmask(self, bitmask, *args, **kwargs):
817 self.visit(bitmask.type, *args, **kwargs)
818
819 def visitPointer(self, pointer, *args, **kwargs):
820 self.visit(pointer.type, *args, **kwargs)
821
822 def visitIntPointer(self, pointer, *args, **kwargs):
823 pass
824
825 def visitObjPointer(self, pointer, *args, **kwargs):
826 self.visit(pointer.type, *args, **kwargs)
827
828 def visitLinearPointer(self, pointer, *args, **kwargs):
829 self.visit(pointer.type, *args, **kwargs)
830
831 def visitReference(self, reference, *args, **kwargs):
832 self.visit(reference.type, *args, **kwargs)
833
834 def visitHandle(self, handle, *args, **kwargs):
835 self.visit(handle.type, *args, **kwargs)
836
837 def visitAlias(self, alias, *args, **kwargs):
838 self.visit(alias.type, *args, **kwargs)
839
840 def visitOpaque(self, opaque, *args, **kwargs):
841 pass
842
843 def visitInterface(self, interface, *args, **kwargs):
844 if interface.base is not None:
845 self.visit(interface.base, *args, **kwargs)
846 for method in interface.iterMethods():
847 for arg in method.args:
848 self.visit(arg.type, *args, **kwargs)
849 self.visit(method.type, *args, **kwargs)
850
851 def visitPolymorphic(self, polymorphic, *args, **kwargs):
José Fonseca0075f152012-04-14 20:25:52 +0100852 for expr, type in polymorphic.switchTypes:
853 self.visit(type, *args, **kwargs)
José Fonsecaeb216e62012-11-20 11:08:08 +0000854 if polymorphic.defaultType is not None:
855 self.visit(polymorphic.defaultType, *args, **kwargs)
José Fonseca0075f152012-04-14 20:25:52 +0100856
857
858class Collector(Traverser):
José Fonseca9c4a2572012-01-13 23:21:10 +0000859 '''Visitor which collects all unique types as it traverses them.'''
José Fonsecae6a50bd2010-11-24 10:12:22 +0000860
861 def __init__(self):
862 self.__visited = set()
863 self.types = []
864
865 def visit(self, type):
866 if type in self.__visited:
867 return
868 self.__visited.add(type)
869 Visitor.visit(self, type)
870 self.types.append(type)
871
José Fonseca16d46dd2011-10-13 09:52:52 +0100872
José Fonsecadbf714b2012-11-20 17:03:43 +0000873class ExpanderMixin:
874 '''Mixin class that provides a bunch of methods to expand C expressions
875 from the specifications.'''
876
877 __structs = None
878 __indices = None
879
880 def expand(self, expr):
881 # Expand a C expression, replacing certain variables
882 if not isinstance(expr, basestring):
883 return expr
884 variables = {}
885
886 if self.__structs is not None:
887 variables['self'] = '(%s)' % self.__structs[0]
888 if self.__indices is not None:
889 variables['i'] = self.__indices[0]
890
891 expandedExpr = expr.format(**variables)
892 if expandedExpr != expr and 0:
893 sys.stderr.write(" %r -> %r\n" % (expr, expandedExpr))
894 return expandedExpr
895
896 def visitMember(self, member, structInstance, *args, **kwargs):
897 memberType, memberName = member
898 if memberName is None:
899 # Anonymous structure/union member
900 memberInstance = structInstance
901 else:
902 memberInstance = '(%s).%s' % (structInstance, memberName)
903 self.__structs = (structInstance, self.__structs)
904 try:
905 return self.visit(memberType, memberInstance, *args, **kwargs)
906 finally:
907 _, self.__structs = self.__structs
908
909 def visitElement(self, elementIndex, elementType, *args, **kwargs):
910 self.__indices = (elementIndex, self.__indices)
911 try:
912 return self.visit(elementType, *args, **kwargs)
913 finally:
914 _, self.__indices = self.__indices
915
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000916
José Fonseca81301932012-11-11 00:10:20 +0000917class Module:
918 '''A collection of functions.'''
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000919
José Fonseca68ec4122011-02-20 11:25:25 +0000920 def __init__(self, name = None):
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000921 self.name = name
922 self.headers = []
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000923 self.functions = []
924 self.interfaces = []
925
José Fonseca54f304a2012-01-14 19:33:08 +0000926 def addFunctions(self, functions):
José Fonseca81301932012-11-11 00:10:20 +0000927 self.functions.extend(functions)
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000928
José Fonseca54f304a2012-01-14 19:33:08 +0000929 def addInterfaces(self, interfaces):
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000930 self.interfaces.extend(interfaces)
931
José Fonseca81301932012-11-11 00:10:20 +0000932 def mergeModule(self, module):
933 self.headers.extend(module.headers)
934 self.functions.extend(module.functions)
935 self.interfaces.extend(module.interfaces)
José Fonseca68ec4122011-02-20 11:25:25 +0000936
José Fonseca1b6c8752012-04-15 14:33:00 +0100937 def getFunctionByName(self, name):
José Fonsecaeccec3e2011-02-20 09:01:25 +0000938 for function in self.functions:
939 if function.name == name:
940 return function
941 return None
942
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000943
José Fonseca81301932012-11-11 00:10:20 +0000944class API:
945 '''API abstraction.
946
947 Essentially, a collection of types, functions, and interfaces.
948 '''
949
950 def __init__(self, modules = None):
951 self.modules = []
952 if modules is not None:
953 self.modules.extend(modules)
954
955 def getAllTypes(self):
956 collector = Collector()
957 for module in self.modules:
958 for function in module.functions:
959 for arg in function.args:
960 collector.visit(arg.type)
961 collector.visit(function.type)
962 for interface in module.interfaces:
963 collector.visit(interface)
964 for method in interface.iterMethods():
965 for arg in method.args:
966 collector.visit(arg.type)
967 collector.visit(method.type)
968 return collector.types
969
970 def getAllFunctions(self):
971 functions = []
972 for module in self.modules:
973 functions.extend(module.functions)
974 return functions
975
976 def getAllInterfaces(self):
977 types = self.getAllTypes()
978 interfaces = [type for type in types if isinstance(type, Interface)]
979 for module in self.modules:
980 for interface in module.interfaces:
981 if interface not in interfaces:
982 interfaces.append(interface)
983 return interfaces
984
985 def addModule(self, module):
986 self.modules.append(module)
987
988 def getFunctionByName(self, name):
989 for module in self.modules:
990 for function in module.functions:
991 if function.name == name:
992 return function
993 return None
994
995
José Fonseca280a1762012-01-31 15:10:13 +0000996# C string (i.e., zero terminated)
José Fonsecabcfc81b2012-08-07 21:07:22 +0100997CString = String(Char)
998WString = String(WChar, wide=True)
999ConstCString = String(Const(Char))
1000ConstWString = String(Const(WChar), wide=True)