blob: 97f586869d44f809c8a92851089188db86231449 [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é Fonseca50c2a142015-01-15 13:10:46 +0000417 def getArgByType(self, type):
418 for arg in self.args:
419 if arg.type is type:
420 return arg
421 return None
422
José Fonseca6fac5ae2010-11-29 16:09:13 +0000423
424def StdFunction(*args, **kwargs):
425 kwargs.setdefault('call', '__stdcall')
426 return Function(*args, **kwargs)
427
428
429def FunctionPointer(type, name, args, **kwargs):
430 # XXX: We should probably treat function pointers (callbacks or not) in a generic fashion
431 return Opaque(name)
432
433
434class Interface(Type):
435
436 def __init__(self, name, base=None):
437 Type.__init__(self, name)
438 self.name = name
439 self.base = base
440 self.methods = []
441
442 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000443 return visitor.visitInterface(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000444
José Fonsecabd086342012-04-18 19:58:32 +0100445 def getMethodByName(self, name):
José Fonsecad275d0a2012-04-30 23:18:05 +0100446 for method in self.iterMethods():
447 if method.name == name:
448 return method
José Fonsecabd086342012-04-18 19:58:32 +0100449 return None
450
José Fonseca54f304a2012-01-14 19:33:08 +0000451 def iterMethods(self):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000452 if self.base is not None:
José Fonseca54f304a2012-01-14 19:33:08 +0000453 for method in self.base.iterMethods():
José Fonseca6fac5ae2010-11-29 16:09:13 +0000454 yield method
455 for method in self.methods:
456 yield method
457 raise StopIteration
458
José Fonseca143e9252012-04-15 09:31:18 +0100459 def iterBases(self):
460 iface = self
461 while iface is not None:
462 yield iface
463 iface = iface.base
464 raise StopIteration
465
José Fonseca5abf5602013-05-30 14:00:44 +0100466 def hasBase(self, *bases):
467 for iface in self.iterBases():
468 if iface in bases:
469 return True
470 return False
471
José Fonseca4220b1b2012-02-03 19:05:29 +0000472 def iterBaseMethods(self):
473 if self.base is not None:
474 for iface, method in self.base.iterBaseMethods():
475 yield iface, method
476 for method in self.methods:
477 yield self, method
478 raise StopIteration
479
José Fonseca6fac5ae2010-11-29 16:09:13 +0000480
481class Method(Function):
482
José Fonseca43aa19f2012-11-10 09:29:38 +0000483 def __init__(self, type, name, args, call = '', const=False, sideeffects=True):
484 assert call == '__stdcall'
José Fonseca5b6fb752012-04-14 14:56:45 +0100485 Function.__init__(self, type, name, args, call = call, sideeffects=sideeffects)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000486 for index in range(len(self.args)):
487 self.args[index].index = index + 1
José Fonseca9dbeda62012-02-03 19:05:54 +0000488 self.const = const
489
490 def prototype(self, name=None):
491 s = Function.prototype(self, name)
492 if self.const:
493 s += ' const'
494 return s
José Fonseca6fac5ae2010-11-29 16:09:13 +0000495
José Fonsecabcb26b22012-04-15 08:42:25 +0100496
José Fonseca5b6fb752012-04-14 14:56:45 +0100497def StdMethod(*args, **kwargs):
498 kwargs.setdefault('call', '__stdcall')
499 return Method(*args, **kwargs)
500
José Fonseca6fac5ae2010-11-29 16:09:13 +0000501
José Fonseca6fac5ae2010-11-29 16:09:13 +0000502class String(Type):
José Fonsecabcfc81b2012-08-07 21:07:22 +0100503 '''Human-legible character string.'''
José Fonseca6fac5ae2010-11-29 16:09:13 +0000504
José Fonsecabcfc81b2012-08-07 21:07:22 +0100505 def __init__(self, type = Char, length = None, wide = False):
506 assert isinstance(type, Type)
507 Type.__init__(self, type.expr + ' *')
508 self.type = type
José Fonseca6fac5ae2010-11-29 16:09:13 +0000509 self.length = length
José Fonsecabcfc81b2012-08-07 21:07:22 +0100510 self.wide = wide
José Fonseca6fac5ae2010-11-29 16:09:13 +0000511
512 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000513 return visitor.visitString(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000514
José Fonseca6fac5ae2010-11-29 16:09:13 +0000515
516class Opaque(Type):
517 '''Opaque pointer.'''
518
519 def __init__(self, expr):
520 Type.__init__(self, expr)
521
522 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000523 return visitor.visitOpaque(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000524
525
526def OpaquePointer(type, *args):
527 return Opaque(type.expr + ' *')
528
529def OpaqueArray(type, size):
530 return Opaque(type.expr + ' *')
531
532def OpaqueBlob(type, size):
533 return Opaque(type.expr + ' *')
José Fonseca8a56d142008-07-09 12:18:08 +0900534
José Fonseca501f2862010-11-19 20:41:18 +0000535
José Fonseca16d46dd2011-10-13 09:52:52 +0100536class Polymorphic(Type):
537
José Fonsecaeb216e62012-11-20 11:08:08 +0000538 def __init__(self, switchExpr, switchTypes, defaultType=None, contextLess=True):
539 if defaultType is None:
540 Type.__init__(self, None)
541 contextLess = False
542 else:
543 Type.__init__(self, defaultType.expr)
José Fonseca54f304a2012-01-14 19:33:08 +0000544 self.switchExpr = switchExpr
545 self.switchTypes = switchTypes
José Fonsecab95e3722012-04-16 14:01:15 +0100546 self.defaultType = defaultType
547 self.contextLess = contextLess
José Fonseca16d46dd2011-10-13 09:52:52 +0100548
549 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000550 return visitor.visitPolymorphic(self, *args, **kwargs)
José Fonseca16d46dd2011-10-13 09:52:52 +0100551
José Fonseca54f304a2012-01-14 19:33:08 +0000552 def iterSwitch(self):
José Fonsecaeb216e62012-11-20 11:08:08 +0000553 cases = []
554 types = []
555
556 if self.defaultType is not None:
557 cases.append(['default'])
558 types.append(self.defaultType)
José Fonseca46161112011-10-14 10:04:55 +0100559
José Fonseca54f304a2012-01-14 19:33:08 +0000560 for expr, type in self.switchTypes:
José Fonseca46161112011-10-14 10:04:55 +0100561 case = 'case %s' % expr
562 try:
563 i = types.index(type)
564 except ValueError:
565 cases.append([case])
566 types.append(type)
567 else:
568 cases[i].append(case)
569
570 return zip(cases, types)
571
José Fonseca16d46dd2011-10-13 09:52:52 +0100572
José Fonsecab95e3722012-04-16 14:01:15 +0100573def EnumPolymorphic(enumName, switchExpr, switchTypes, defaultType, contextLess=True):
574 enumValues = [expr for expr, type in switchTypes]
575 enum = Enum(enumName, enumValues)
576 polymorphic = Polymorphic(switchExpr, switchTypes, defaultType, contextLess)
577 return enum, polymorphic
578
579
José Fonseca501f2862010-11-19 20:41:18 +0000580class Visitor:
José Fonseca9c4a2572012-01-13 23:21:10 +0000581 '''Abstract visitor for the type hierarchy.'''
José Fonseca501f2862010-11-19 20:41:18 +0000582
583 def visit(self, type, *args, **kwargs):
584 return type.visit(self, *args, **kwargs)
585
José Fonseca54f304a2012-01-14 19:33:08 +0000586 def visitVoid(self, void, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000587 raise NotImplementedError
588
José Fonseca54f304a2012-01-14 19:33:08 +0000589 def visitLiteral(self, literal, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000590 raise NotImplementedError
591
José Fonseca54f304a2012-01-14 19:33:08 +0000592 def visitString(self, string, *args, **kwargs):
José Fonseca2defc982010-11-22 16:59:10 +0000593 raise NotImplementedError
594
José Fonseca54f304a2012-01-14 19:33:08 +0000595 def visitConst(self, const, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000596 raise NotImplementedError
597
José Fonseca54f304a2012-01-14 19:33:08 +0000598 def visitStruct(self, struct, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000599 raise NotImplementedError
600
José Fonseca54f304a2012-01-14 19:33:08 +0000601 def visitArray(self, array, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000602 raise NotImplementedError
603
Andreas Hartmetzba7bb0d2013-07-07 22:51:12 +0200604 def visitAttribArray(self, array, *args, **kwargs):
605 raise NotImplementedError
606
José Fonseca54f304a2012-01-14 19:33:08 +0000607 def visitBlob(self, blob, *args, **kwargs):
José Fonseca885f2652010-11-20 11:22:25 +0000608 raise NotImplementedError
609
José Fonseca54f304a2012-01-14 19:33:08 +0000610 def visitEnum(self, enum, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000611 raise NotImplementedError
612
José Fonseca54f304a2012-01-14 19:33:08 +0000613 def visitBitmask(self, bitmask, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000614 raise NotImplementedError
615
José Fonseca54f304a2012-01-14 19:33:08 +0000616 def visitPointer(self, pointer, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000617 raise NotImplementedError
618
José Fonseca59ee88e2012-01-15 14:24:10 +0000619 def visitIntPointer(self, pointer, *args, **kwargs):
620 raise NotImplementedError
621
José Fonsecafbcf6832012-04-05 07:10:30 +0100622 def visitObjPointer(self, pointer, *args, **kwargs):
623 raise NotImplementedError
624
José Fonseca59ee88e2012-01-15 14:24:10 +0000625 def visitLinearPointer(self, pointer, *args, **kwargs):
626 raise NotImplementedError
627
José Fonsecab89c5932012-04-01 22:47:11 +0200628 def visitReference(self, reference, *args, **kwargs):
629 raise NotImplementedError
630
José Fonseca54f304a2012-01-14 19:33:08 +0000631 def visitHandle(self, handle, *args, **kwargs):
José Fonseca50d78d82010-11-23 22:13:14 +0000632 raise NotImplementedError
633
José Fonseca54f304a2012-01-14 19:33:08 +0000634 def visitAlias(self, alias, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000635 raise NotImplementedError
636
José Fonseca54f304a2012-01-14 19:33:08 +0000637 def visitOpaque(self, opaque, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000638 raise NotImplementedError
639
José Fonseca54f304a2012-01-14 19:33:08 +0000640 def visitInterface(self, interface, *args, **kwargs):
José Fonsecac356d6a2010-11-23 14:27:25 +0000641 raise NotImplementedError
642
José Fonseca54f304a2012-01-14 19:33:08 +0000643 def visitPolymorphic(self, polymorphic, *args, **kwargs):
José Fonseca16d46dd2011-10-13 09:52:52 +0100644 raise NotImplementedError
José Fonseca54f304a2012-01-14 19:33:08 +0000645 #return self.visit(polymorphic.defaultType, *args, **kwargs)
José Fonseca16d46dd2011-10-13 09:52:52 +0100646
José Fonsecac356d6a2010-11-23 14:27:25 +0000647
648class OnceVisitor(Visitor):
José Fonseca9c4a2572012-01-13 23:21:10 +0000649 '''Visitor that guarantees that each type is visited only once.'''
José Fonsecac356d6a2010-11-23 14:27:25 +0000650
651 def __init__(self):
652 self.__visited = set()
653
654 def visit(self, type, *args, **kwargs):
655 if type not in self.__visited:
656 self.__visited.add(type)
657 return type.visit(self, *args, **kwargs)
658 return None
659
José Fonseca501f2862010-11-19 20:41:18 +0000660
José Fonsecac9edb832010-11-20 09:03:10 +0000661class Rebuilder(Visitor):
José Fonseca9c4a2572012-01-13 23:21:10 +0000662 '''Visitor which rebuild types as it visits them.
663
664 By itself it is a no-op -- it is intended to be overwritten.
665 '''
José Fonsecac9edb832010-11-20 09:03:10 +0000666
José Fonseca54f304a2012-01-14 19:33:08 +0000667 def visitVoid(self, void):
José Fonsecac9edb832010-11-20 09:03:10 +0000668 return void
669
José Fonseca54f304a2012-01-14 19:33:08 +0000670 def visitLiteral(self, literal):
José Fonsecac9edb832010-11-20 09:03:10 +0000671 return literal
672
José Fonseca54f304a2012-01-14 19:33:08 +0000673 def visitString(self, string):
José Fonsecabcfc81b2012-08-07 21:07:22 +0100674 string_type = self.visit(string.type)
675 if string_type is string.type:
676 return string
677 else:
678 return String(string_type, string.length, string.wide)
José Fonseca2defc982010-11-22 16:59:10 +0000679
José Fonseca54f304a2012-01-14 19:33:08 +0000680 def visitConst(self, const):
José Fonsecaf182eda2012-04-05 19:59:56 +0100681 const_type = self.visit(const.type)
682 if const_type is const.type:
683 return const
684 else:
685 return Const(const_type)
José Fonsecac9edb832010-11-20 09:03:10 +0000686
José Fonseca54f304a2012-01-14 19:33:08 +0000687 def visitStruct(self, struct):
José Fonseca06aa2842011-05-05 07:55:54 +0100688 members = [(self.visit(type), name) for type, name in struct.members]
José Fonsecac9edb832010-11-20 09:03:10 +0000689 return Struct(struct.name, members)
690
José Fonseca54f304a2012-01-14 19:33:08 +0000691 def visitArray(self, array):
José Fonsecac9edb832010-11-20 09:03:10 +0000692 type = self.visit(array.type)
693 return Array(type, array.length)
694
José Fonseca54f304a2012-01-14 19:33:08 +0000695 def visitBlob(self, blob):
José Fonseca885f2652010-11-20 11:22:25 +0000696 type = self.visit(blob.type)
697 return Blob(type, blob.size)
698
José Fonseca54f304a2012-01-14 19:33:08 +0000699 def visitEnum(self, enum):
José Fonsecac9edb832010-11-20 09:03:10 +0000700 return enum
701
José Fonseca54f304a2012-01-14 19:33:08 +0000702 def visitBitmask(self, bitmask):
José Fonsecac9edb832010-11-20 09:03:10 +0000703 type = self.visit(bitmask.type)
704 return Bitmask(type, bitmask.values)
705
José Fonseca54f304a2012-01-14 19:33:08 +0000706 def visitPointer(self, pointer):
José Fonsecaf182eda2012-04-05 19:59:56 +0100707 pointer_type = self.visit(pointer.type)
708 if pointer_type is pointer.type:
709 return pointer
710 else:
711 return Pointer(pointer_type)
José Fonsecac9edb832010-11-20 09:03:10 +0000712
José Fonseca59ee88e2012-01-15 14:24:10 +0000713 def visitIntPointer(self, pointer):
714 return pointer
715
José Fonsecafbcf6832012-04-05 07:10:30 +0100716 def visitObjPointer(self, pointer):
José Fonsecaf182eda2012-04-05 19:59:56 +0100717 pointer_type = self.visit(pointer.type)
718 if pointer_type is pointer.type:
719 return pointer
720 else:
721 return ObjPointer(pointer_type)
José Fonsecafbcf6832012-04-05 07:10:30 +0100722
José Fonseca59ee88e2012-01-15 14:24:10 +0000723 def visitLinearPointer(self, pointer):
José Fonsecaf182eda2012-04-05 19:59:56 +0100724 pointer_type = self.visit(pointer.type)
725 if pointer_type is pointer.type:
726 return pointer
727 else:
728 return LinearPointer(pointer_type)
José Fonseca59ee88e2012-01-15 14:24:10 +0000729
José Fonsecab89c5932012-04-01 22:47:11 +0200730 def visitReference(self, reference):
José Fonsecaf182eda2012-04-05 19:59:56 +0100731 reference_type = self.visit(reference.type)
732 if reference_type is reference.type:
733 return reference
734 else:
735 return Reference(reference_type)
José Fonsecab89c5932012-04-01 22:47:11 +0200736
José Fonseca54f304a2012-01-14 19:33:08 +0000737 def visitHandle(self, handle):
José Fonsecaf182eda2012-04-05 19:59:56 +0100738 handle_type = self.visit(handle.type)
739 if handle_type is handle.type:
740 return handle
741 else:
742 return Handle(handle.name, handle_type, range=handle.range, key=handle.key)
José Fonseca50d78d82010-11-23 22:13:14 +0000743
José Fonseca54f304a2012-01-14 19:33:08 +0000744 def visitAlias(self, alias):
José Fonsecaf182eda2012-04-05 19:59:56 +0100745 alias_type = self.visit(alias.type)
746 if alias_type is alias.type:
747 return alias
748 else:
749 return Alias(alias.expr, alias_type)
José Fonsecac9edb832010-11-20 09:03:10 +0000750
José Fonseca54f304a2012-01-14 19:33:08 +0000751 def visitOpaque(self, opaque):
José Fonsecac9edb832010-11-20 09:03:10 +0000752 return opaque
753
José Fonseca7814edf2012-01-31 10:55:49 +0000754 def visitInterface(self, interface, *args, **kwargs):
755 return interface
756
José Fonseca54f304a2012-01-14 19:33:08 +0000757 def visitPolymorphic(self, polymorphic):
José Fonseca54f304a2012-01-14 19:33:08 +0000758 switchExpr = polymorphic.switchExpr
759 switchTypes = [(expr, self.visit(type)) for expr, type in polymorphic.switchTypes]
José Fonsecaeb216e62012-11-20 11:08:08 +0000760 if polymorphic.defaultType is None:
761 defaultType = None
762 else:
763 defaultType = self.visit(polymorphic.defaultType)
José Fonsecab95e3722012-04-16 14:01:15 +0100764 return Polymorphic(switchExpr, switchTypes, defaultType, polymorphic.contextLess)
José Fonseca16d46dd2011-10-13 09:52:52 +0100765
José Fonsecac9edb832010-11-20 09:03:10 +0000766
José Fonseca0075f152012-04-14 20:25:52 +0100767class MutableRebuilder(Rebuilder):
768 '''Type visitor which derives a mutable type.'''
769
José Fonsecabcfc81b2012-08-07 21:07:22 +0100770 def visitString(self, string):
771 return string
772
José Fonseca0075f152012-04-14 20:25:52 +0100773 def visitConst(self, const):
774 # Strip out const qualifier
775 return const.type
776
777 def visitAlias(self, alias):
778 # Tear the alias on type changes
779 type = self.visit(alias.type)
780 if type is alias.type:
781 return alias
782 return type
783
784 def visitReference(self, reference):
785 # Strip out references
786 return reference.type
787
788
789class Traverser(Visitor):
790 '''Visitor which all types.'''
791
792 def visitVoid(self, void, *args, **kwargs):
793 pass
794
795 def visitLiteral(self, literal, *args, **kwargs):
796 pass
797
798 def visitString(self, string, *args, **kwargs):
799 pass
800
801 def visitConst(self, const, *args, **kwargs):
802 self.visit(const.type, *args, **kwargs)
803
804 def visitStruct(self, struct, *args, **kwargs):
805 for type, name in struct.members:
806 self.visit(type, *args, **kwargs)
807
808 def visitArray(self, array, *args, **kwargs):
809 self.visit(array.type, *args, **kwargs)
810
Andreas Hartmetzba7bb0d2013-07-07 22:51:12 +0200811 def visitAttribArray(self, attribs, *args, **kwargs):
812 for key, valueType in attribs.valueTypes:
Andreas Hartmetz7a0de292013-07-09 22:38:29 +0200813 if valueType is not None:
814 self.visit(valueType, *args, **kwargs)
Andreas Hartmetzba7bb0d2013-07-07 22:51:12 +0200815
José Fonseca0075f152012-04-14 20:25:52 +0100816 def visitBlob(self, array, *args, **kwargs):
817 pass
818
819 def visitEnum(self, enum, *args, **kwargs):
820 pass
821
822 def visitBitmask(self, bitmask, *args, **kwargs):
823 self.visit(bitmask.type, *args, **kwargs)
824
825 def visitPointer(self, pointer, *args, **kwargs):
826 self.visit(pointer.type, *args, **kwargs)
827
828 def visitIntPointer(self, pointer, *args, **kwargs):
829 pass
830
831 def visitObjPointer(self, pointer, *args, **kwargs):
832 self.visit(pointer.type, *args, **kwargs)
833
834 def visitLinearPointer(self, pointer, *args, **kwargs):
835 self.visit(pointer.type, *args, **kwargs)
836
837 def visitReference(self, reference, *args, **kwargs):
838 self.visit(reference.type, *args, **kwargs)
839
840 def visitHandle(self, handle, *args, **kwargs):
841 self.visit(handle.type, *args, **kwargs)
842
843 def visitAlias(self, alias, *args, **kwargs):
844 self.visit(alias.type, *args, **kwargs)
845
846 def visitOpaque(self, opaque, *args, **kwargs):
847 pass
848
849 def visitInterface(self, interface, *args, **kwargs):
850 if interface.base is not None:
851 self.visit(interface.base, *args, **kwargs)
852 for method in interface.iterMethods():
853 for arg in method.args:
854 self.visit(arg.type, *args, **kwargs)
855 self.visit(method.type, *args, **kwargs)
856
857 def visitPolymorphic(self, polymorphic, *args, **kwargs):
José Fonseca0075f152012-04-14 20:25:52 +0100858 for expr, type in polymorphic.switchTypes:
859 self.visit(type, *args, **kwargs)
José Fonsecaeb216e62012-11-20 11:08:08 +0000860 if polymorphic.defaultType is not None:
861 self.visit(polymorphic.defaultType, *args, **kwargs)
José Fonseca0075f152012-04-14 20:25:52 +0100862
863
864class Collector(Traverser):
José Fonseca9c4a2572012-01-13 23:21:10 +0000865 '''Visitor which collects all unique types as it traverses them.'''
José Fonsecae6a50bd2010-11-24 10:12:22 +0000866
867 def __init__(self):
868 self.__visited = set()
869 self.types = []
870
871 def visit(self, type):
872 if type in self.__visited:
873 return
874 self.__visited.add(type)
875 Visitor.visit(self, type)
876 self.types.append(type)
877
José Fonseca16d46dd2011-10-13 09:52:52 +0100878
José Fonsecadbf714b2012-11-20 17:03:43 +0000879class ExpanderMixin:
880 '''Mixin class that provides a bunch of methods to expand C expressions
881 from the specifications.'''
882
883 __structs = None
884 __indices = None
885
886 def expand(self, expr):
887 # Expand a C expression, replacing certain variables
888 if not isinstance(expr, basestring):
889 return expr
890 variables = {}
891
892 if self.__structs is not None:
893 variables['self'] = '(%s)' % self.__structs[0]
894 if self.__indices is not None:
895 variables['i'] = self.__indices[0]
896
897 expandedExpr = expr.format(**variables)
898 if expandedExpr != expr and 0:
899 sys.stderr.write(" %r -> %r\n" % (expr, expandedExpr))
900 return expandedExpr
901
902 def visitMember(self, member, structInstance, *args, **kwargs):
903 memberType, memberName = member
904 if memberName is None:
905 # Anonymous structure/union member
906 memberInstance = structInstance
907 else:
908 memberInstance = '(%s).%s' % (structInstance, memberName)
909 self.__structs = (structInstance, self.__structs)
910 try:
911 return self.visit(memberType, memberInstance, *args, **kwargs)
912 finally:
913 _, self.__structs = self.__structs
914
915 def visitElement(self, elementIndex, elementType, *args, **kwargs):
916 self.__indices = (elementIndex, self.__indices)
917 try:
918 return self.visit(elementType, *args, **kwargs)
919 finally:
920 _, self.__indices = self.__indices
921
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000922
José Fonseca81301932012-11-11 00:10:20 +0000923class Module:
924 '''A collection of functions.'''
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000925
José Fonseca68ec4122011-02-20 11:25:25 +0000926 def __init__(self, name = None):
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000927 self.name = name
928 self.headers = []
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000929 self.functions = []
930 self.interfaces = []
931
José Fonseca54f304a2012-01-14 19:33:08 +0000932 def addFunctions(self, functions):
José Fonseca81301932012-11-11 00:10:20 +0000933 self.functions.extend(functions)
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000934
José Fonseca54f304a2012-01-14 19:33:08 +0000935 def addInterfaces(self, interfaces):
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000936 self.interfaces.extend(interfaces)
937
José Fonseca81301932012-11-11 00:10:20 +0000938 def mergeModule(self, module):
939 self.headers.extend(module.headers)
940 self.functions.extend(module.functions)
941 self.interfaces.extend(module.interfaces)
José Fonseca68ec4122011-02-20 11:25:25 +0000942
José Fonseca1b6c8752012-04-15 14:33:00 +0100943 def getFunctionByName(self, name):
José Fonsecaeccec3e2011-02-20 09:01:25 +0000944 for function in self.functions:
945 if function.name == name:
946 return function
947 return None
948
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000949
José Fonseca81301932012-11-11 00:10:20 +0000950class API:
951 '''API abstraction.
952
953 Essentially, a collection of types, functions, and interfaces.
954 '''
955
956 def __init__(self, modules = None):
957 self.modules = []
958 if modules is not None:
959 self.modules.extend(modules)
960
961 def getAllTypes(self):
962 collector = Collector()
963 for module in self.modules:
964 for function in module.functions:
965 for arg in function.args:
966 collector.visit(arg.type)
967 collector.visit(function.type)
968 for interface in module.interfaces:
969 collector.visit(interface)
970 for method in interface.iterMethods():
971 for arg in method.args:
972 collector.visit(arg.type)
973 collector.visit(method.type)
974 return collector.types
975
976 def getAllFunctions(self):
977 functions = []
978 for module in self.modules:
979 functions.extend(module.functions)
980 return functions
981
982 def getAllInterfaces(self):
983 types = self.getAllTypes()
984 interfaces = [type for type in types if isinstance(type, Interface)]
985 for module in self.modules:
986 for interface in module.interfaces:
987 if interface not in interfaces:
988 interfaces.append(interface)
989 return interfaces
990
991 def addModule(self, module):
992 self.modules.append(module)
993
994 def getFunctionByName(self, name):
995 for module in self.modules:
996 for function in module.functions:
997 if function.name == name:
998 return function
999 return None
1000
1001
José Fonseca280a1762012-01-31 15:10:13 +00001002# C string (i.e., zero terminated)
José Fonsecabcfc81b2012-08-07 21:07:22 +01001003CString = String(Char)
1004WString = String(WChar, wide=True)
1005ConstCString = String(Const(Char))
1006ConstWString = String(Const(WChar), wide=True)