blob: 1988c6588068389304a2dcf6d22488959782a049 [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
José Fonseca77c10d82013-07-20 15:27:29 +0100275 def __init__(self, baseType, valueTypes, isConst = True, terminator = '0'):
276 self.baseType = baseType
Andreas Hartmetzb936e552013-07-08 12:36:11 +0200277 if isConst:
Andreas Hartmetzedea8992013-07-12 11:37:35 +0200278 Type.__init__(self, (Pointer(Const(self.baseType))).expr)
Andreas Hartmetzb936e552013-07-08 12:36:11 +0200279 else:
Andreas Hartmetzedea8992013-07-12 11:37:35 +0200280 Type.__init__(self, (Pointer(self.baseType)).expr)
Andreas Hartmetzba7bb0d2013-07-07 22:51:12 +0200281 self.valueTypes = valueTypes
Andreas Hartmetz7a0de292013-07-09 22:38:29 +0200282 self.terminator = terminator
283 self.hasKeysWithoutValues = False
284 for key, value in valueTypes:
285 if value is None:
286 self.hasKeysWithoutValues = True
Andreas Hartmetzba7bb0d2013-07-07 22:51:12 +0200287
288 def visit(self, visitor, *args, **kwargs):
289 return visitor.visitAttribArray(self, *args, **kwargs)
290
291
José Fonseca6fac5ae2010-11-29 16:09:13 +0000292class Blob(Type):
293
294 def __init__(self, type, size):
295 Type.__init__(self, type.expr + ' *')
296 self.type = type
297 self.size = size
298
299 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000300 return visitor.visitBlob(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000301
302
303class Struct(Type):
304
José Fonseca02c25002011-10-15 13:17:26 +0100305 __id = 0
306
José Fonseca6fac5ae2010-11-29 16:09:13 +0000307 def __init__(self, name, members):
308 Type.__init__(self, name)
José Fonseca02c25002011-10-15 13:17:26 +0100309
310 self.id = Struct.__id
311 Struct.__id += 1
312
José Fonseca6fac5ae2010-11-29 16:09:13 +0000313 self.name = name
José Fonsecadbf714b2012-11-20 17:03:43 +0000314 self.members = members
José Fonseca6fac5ae2010-11-29 16:09:13 +0000315
316 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000317 return visitor.visitStruct(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000318
319
José Fonsecadbf714b2012-11-20 17:03:43 +0000320def Union(kindExpr, kindTypes, contextLess=True):
José Fonsecaeb216e62012-11-20 11:08:08 +0000321 switchTypes = []
322 for kindCase, kindType, kindMemberName in kindTypes:
323 switchType = Struct(None, [(kindType, kindMemberName)])
324 switchTypes.append((kindCase, switchType))
325 return Polymorphic(kindExpr, switchTypes, contextLess=contextLess)
326
José Fonseca5b6fb752012-04-14 14:56:45 +0100327
José Fonseca6fac5ae2010-11-29 16:09:13 +0000328class Alias(Type):
329
330 def __init__(self, expr, type):
331 Type.__init__(self, expr)
332 self.type = type
333
334 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000335 return visitor.visitAlias(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000336
José Fonseca6fac5ae2010-11-29 16:09:13 +0000337class Arg:
338
José Fonseca9dd8f702012-04-07 10:42:50 +0100339 def __init__(self, type, name, input=True, output=False):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000340 self.type = type
341 self.name = name
José Fonseca9dd8f702012-04-07 10:42:50 +0100342 self.input = input
José Fonseca6fac5ae2010-11-29 16:09:13 +0000343 self.output = output
344 self.index = None
345
346 def __str__(self):
347 return '%s %s' % (self.type, self.name)
348
349
José Fonseca9dd8f702012-04-07 10:42:50 +0100350def In(type, name):
351 return Arg(type, name, input=True, output=False)
352
353def Out(type, name):
354 return Arg(type, name, input=False, output=True)
355
356def InOut(type, name):
357 return Arg(type, name, input=True, output=True)
358
359
José Fonseca6fac5ae2010-11-29 16:09:13 +0000360class Function:
361
José Fonseca84cea3b2012-05-09 21:12:30 +0100362 def __init__(self, type, name, args, call = '', fail = None, sideeffects=True, internal=False):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000363 self.type = type
364 self.name = name
365
366 self.args = []
367 index = 0
368 for arg in args:
José Fonseca8384ccb2011-05-25 10:12:02 +0100369 if not isinstance(arg, Arg):
370 if isinstance(arg, tuple):
371 arg_type, arg_name = arg
372 else:
373 arg_type = arg
374 arg_name = "arg%u" % index
José Fonseca6fac5ae2010-11-29 16:09:13 +0000375 arg = Arg(arg_type, arg_name)
376 arg.index = index
377 index += 1
378 self.args.append(arg)
379
380 self.call = call
381 self.fail = fail
382 self.sideeffects = sideeffects
José Fonseca84cea3b2012-05-09 21:12:30 +0100383 self.internal = internal
José Fonseca6fac5ae2010-11-29 16:09:13 +0000384
385 def prototype(self, name=None):
386 if name is not None:
387 name = name.strip()
388 else:
389 name = self.name
390 s = name
391 if self.call:
392 s = self.call + ' ' + s
393 if name.startswith('*'):
394 s = '(' + s + ')'
395 s = self.type.expr + ' ' + s
396 s += "("
397 if self.args:
398 s += ", ".join(["%s %s" % (arg.type, arg.name) for arg in self.args])
399 else:
400 s += "void"
401 s += ")"
402 return s
403
José Fonseca568ecc22012-01-15 13:57:03 +0000404 def argNames(self):
405 return [arg.name for arg in self.args]
406
José Fonseca999284f2013-02-19 13:29:26 +0000407 def getArgByName(self, name):
408 for arg in self.args:
409 if arg.name == name:
410 return arg
411 return None
412
José Fonseca6fac5ae2010-11-29 16:09:13 +0000413
414def StdFunction(*args, **kwargs):
415 kwargs.setdefault('call', '__stdcall')
416 return Function(*args, **kwargs)
417
418
419def FunctionPointer(type, name, args, **kwargs):
420 # XXX: We should probably treat function pointers (callbacks or not) in a generic fashion
421 return Opaque(name)
422
423
424class Interface(Type):
425
426 def __init__(self, name, base=None):
427 Type.__init__(self, name)
428 self.name = name
429 self.base = base
430 self.methods = []
431
432 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000433 return visitor.visitInterface(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000434
José Fonsecabd086342012-04-18 19:58:32 +0100435 def getMethodByName(self, name):
José Fonsecad275d0a2012-04-30 23:18:05 +0100436 for method in self.iterMethods():
437 if method.name == name:
438 return method
José Fonsecabd086342012-04-18 19:58:32 +0100439 return None
440
José Fonseca54f304a2012-01-14 19:33:08 +0000441 def iterMethods(self):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000442 if self.base is not None:
José Fonseca54f304a2012-01-14 19:33:08 +0000443 for method in self.base.iterMethods():
José Fonseca6fac5ae2010-11-29 16:09:13 +0000444 yield method
445 for method in self.methods:
446 yield method
447 raise StopIteration
448
José Fonseca143e9252012-04-15 09:31:18 +0100449 def iterBases(self):
450 iface = self
451 while iface is not None:
452 yield iface
453 iface = iface.base
454 raise StopIteration
455
José Fonseca5abf5602013-05-30 14:00:44 +0100456 def hasBase(self, *bases):
457 for iface in self.iterBases():
458 if iface in bases:
459 return True
460 return False
461
José Fonseca4220b1b2012-02-03 19:05:29 +0000462 def iterBaseMethods(self):
463 if self.base is not None:
464 for iface, method in self.base.iterBaseMethods():
465 yield iface, method
466 for method in self.methods:
467 yield self, method
468 raise StopIteration
469
José Fonseca6fac5ae2010-11-29 16:09:13 +0000470
471class Method(Function):
472
José Fonseca43aa19f2012-11-10 09:29:38 +0000473 def __init__(self, type, name, args, call = '', const=False, sideeffects=True):
474 assert call == '__stdcall'
José Fonseca5b6fb752012-04-14 14:56:45 +0100475 Function.__init__(self, type, name, args, call = call, sideeffects=sideeffects)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000476 for index in range(len(self.args)):
477 self.args[index].index = index + 1
José Fonseca9dbeda62012-02-03 19:05:54 +0000478 self.const = const
479
480 def prototype(self, name=None):
481 s = Function.prototype(self, name)
482 if self.const:
483 s += ' const'
484 return s
José Fonseca6fac5ae2010-11-29 16:09:13 +0000485
José Fonsecabcb26b22012-04-15 08:42:25 +0100486
José Fonseca5b6fb752012-04-14 14:56:45 +0100487def StdMethod(*args, **kwargs):
488 kwargs.setdefault('call', '__stdcall')
489 return Method(*args, **kwargs)
490
José Fonseca6fac5ae2010-11-29 16:09:13 +0000491
José Fonseca6fac5ae2010-11-29 16:09:13 +0000492class String(Type):
José Fonsecabcfc81b2012-08-07 21:07:22 +0100493 '''Human-legible character string.'''
José Fonseca6fac5ae2010-11-29 16:09:13 +0000494
José Fonsecabcfc81b2012-08-07 21:07:22 +0100495 def __init__(self, type = Char, length = None, wide = False):
496 assert isinstance(type, Type)
497 Type.__init__(self, type.expr + ' *')
498 self.type = type
José Fonseca6fac5ae2010-11-29 16:09:13 +0000499 self.length = length
José Fonsecabcfc81b2012-08-07 21:07:22 +0100500 self.wide = wide
José Fonseca6fac5ae2010-11-29 16:09:13 +0000501
502 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000503 return visitor.visitString(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000504
José Fonseca6fac5ae2010-11-29 16:09:13 +0000505
506class Opaque(Type):
507 '''Opaque pointer.'''
508
509 def __init__(self, expr):
510 Type.__init__(self, expr)
511
512 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000513 return visitor.visitOpaque(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000514
515
516def OpaquePointer(type, *args):
517 return Opaque(type.expr + ' *')
518
519def OpaqueArray(type, size):
520 return Opaque(type.expr + ' *')
521
522def OpaqueBlob(type, size):
523 return Opaque(type.expr + ' *')
José Fonseca8a56d142008-07-09 12:18:08 +0900524
José Fonseca501f2862010-11-19 20:41:18 +0000525
José Fonseca16d46dd2011-10-13 09:52:52 +0100526class Polymorphic(Type):
527
José Fonsecaeb216e62012-11-20 11:08:08 +0000528 def __init__(self, switchExpr, switchTypes, defaultType=None, contextLess=True):
529 if defaultType is None:
530 Type.__init__(self, None)
531 contextLess = False
532 else:
533 Type.__init__(self, defaultType.expr)
José Fonseca54f304a2012-01-14 19:33:08 +0000534 self.switchExpr = switchExpr
535 self.switchTypes = switchTypes
José Fonsecab95e3722012-04-16 14:01:15 +0100536 self.defaultType = defaultType
537 self.contextLess = contextLess
José Fonseca16d46dd2011-10-13 09:52:52 +0100538
539 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000540 return visitor.visitPolymorphic(self, *args, **kwargs)
José Fonseca16d46dd2011-10-13 09:52:52 +0100541
José Fonseca54f304a2012-01-14 19:33:08 +0000542 def iterSwitch(self):
José Fonsecaeb216e62012-11-20 11:08:08 +0000543 cases = []
544 types = []
545
546 if self.defaultType is not None:
547 cases.append(['default'])
548 types.append(self.defaultType)
José Fonseca46161112011-10-14 10:04:55 +0100549
José Fonseca54f304a2012-01-14 19:33:08 +0000550 for expr, type in self.switchTypes:
José Fonseca46161112011-10-14 10:04:55 +0100551 case = 'case %s' % expr
552 try:
553 i = types.index(type)
554 except ValueError:
555 cases.append([case])
556 types.append(type)
557 else:
558 cases[i].append(case)
559
560 return zip(cases, types)
561
José Fonseca16d46dd2011-10-13 09:52:52 +0100562
José Fonsecab95e3722012-04-16 14:01:15 +0100563def EnumPolymorphic(enumName, switchExpr, switchTypes, defaultType, contextLess=True):
564 enumValues = [expr for expr, type in switchTypes]
565 enum = Enum(enumName, enumValues)
566 polymorphic = Polymorphic(switchExpr, switchTypes, defaultType, contextLess)
567 return enum, polymorphic
568
569
José Fonseca501f2862010-11-19 20:41:18 +0000570class Visitor:
José Fonseca9c4a2572012-01-13 23:21:10 +0000571 '''Abstract visitor for the type hierarchy.'''
José Fonseca501f2862010-11-19 20:41:18 +0000572
573 def visit(self, type, *args, **kwargs):
574 return type.visit(self, *args, **kwargs)
575
José Fonseca54f304a2012-01-14 19:33:08 +0000576 def visitVoid(self, void, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000577 raise NotImplementedError
578
José Fonseca54f304a2012-01-14 19:33:08 +0000579 def visitLiteral(self, literal, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000580 raise NotImplementedError
581
José Fonseca54f304a2012-01-14 19:33:08 +0000582 def visitString(self, string, *args, **kwargs):
José Fonseca2defc982010-11-22 16:59:10 +0000583 raise NotImplementedError
584
José Fonseca54f304a2012-01-14 19:33:08 +0000585 def visitConst(self, const, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000586 raise NotImplementedError
587
José Fonseca54f304a2012-01-14 19:33:08 +0000588 def visitStruct(self, struct, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000589 raise NotImplementedError
590
José Fonseca54f304a2012-01-14 19:33:08 +0000591 def visitArray(self, array, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000592 raise NotImplementedError
593
Andreas Hartmetzba7bb0d2013-07-07 22:51:12 +0200594 def visitAttribArray(self, array, *args, **kwargs):
595 raise NotImplementedError
596
José Fonseca54f304a2012-01-14 19:33:08 +0000597 def visitBlob(self, blob, *args, **kwargs):
José Fonseca885f2652010-11-20 11:22:25 +0000598 raise NotImplementedError
599
José Fonseca54f304a2012-01-14 19:33:08 +0000600 def visitEnum(self, enum, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000601 raise NotImplementedError
602
José Fonseca54f304a2012-01-14 19:33:08 +0000603 def visitBitmask(self, bitmask, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000604 raise NotImplementedError
605
José Fonseca54f304a2012-01-14 19:33:08 +0000606 def visitPointer(self, pointer, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000607 raise NotImplementedError
608
José Fonseca59ee88e2012-01-15 14:24:10 +0000609 def visitIntPointer(self, pointer, *args, **kwargs):
610 raise NotImplementedError
611
José Fonsecafbcf6832012-04-05 07:10:30 +0100612 def visitObjPointer(self, pointer, *args, **kwargs):
613 raise NotImplementedError
614
José Fonseca59ee88e2012-01-15 14:24:10 +0000615 def visitLinearPointer(self, pointer, *args, **kwargs):
616 raise NotImplementedError
617
José Fonsecab89c5932012-04-01 22:47:11 +0200618 def visitReference(self, reference, *args, **kwargs):
619 raise NotImplementedError
620
José Fonseca54f304a2012-01-14 19:33:08 +0000621 def visitHandle(self, handle, *args, **kwargs):
José Fonseca50d78d82010-11-23 22:13:14 +0000622 raise NotImplementedError
623
José Fonseca54f304a2012-01-14 19:33:08 +0000624 def visitAlias(self, alias, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000625 raise NotImplementedError
626
José Fonseca54f304a2012-01-14 19:33:08 +0000627 def visitOpaque(self, opaque, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000628 raise NotImplementedError
629
José Fonseca54f304a2012-01-14 19:33:08 +0000630 def visitInterface(self, interface, *args, **kwargs):
José Fonsecac356d6a2010-11-23 14:27:25 +0000631 raise NotImplementedError
632
José Fonseca54f304a2012-01-14 19:33:08 +0000633 def visitPolymorphic(self, polymorphic, *args, **kwargs):
José Fonseca16d46dd2011-10-13 09:52:52 +0100634 raise NotImplementedError
José Fonseca54f304a2012-01-14 19:33:08 +0000635 #return self.visit(polymorphic.defaultType, *args, **kwargs)
José Fonseca16d46dd2011-10-13 09:52:52 +0100636
José Fonsecac356d6a2010-11-23 14:27:25 +0000637
638class OnceVisitor(Visitor):
José Fonseca9c4a2572012-01-13 23:21:10 +0000639 '''Visitor that guarantees that each type is visited only once.'''
José Fonsecac356d6a2010-11-23 14:27:25 +0000640
641 def __init__(self):
642 self.__visited = set()
643
644 def visit(self, type, *args, **kwargs):
645 if type not in self.__visited:
646 self.__visited.add(type)
647 return type.visit(self, *args, **kwargs)
648 return None
649
José Fonseca501f2862010-11-19 20:41:18 +0000650
José Fonsecac9edb832010-11-20 09:03:10 +0000651class Rebuilder(Visitor):
José Fonseca9c4a2572012-01-13 23:21:10 +0000652 '''Visitor which rebuild types as it visits them.
653
654 By itself it is a no-op -- it is intended to be overwritten.
655 '''
José Fonsecac9edb832010-11-20 09:03:10 +0000656
José Fonseca54f304a2012-01-14 19:33:08 +0000657 def visitVoid(self, void):
José Fonsecac9edb832010-11-20 09:03:10 +0000658 return void
659
José Fonseca54f304a2012-01-14 19:33:08 +0000660 def visitLiteral(self, literal):
José Fonsecac9edb832010-11-20 09:03:10 +0000661 return literal
662
José Fonseca54f304a2012-01-14 19:33:08 +0000663 def visitString(self, string):
José Fonsecabcfc81b2012-08-07 21:07:22 +0100664 string_type = self.visit(string.type)
665 if string_type is string.type:
666 return string
667 else:
668 return String(string_type, string.length, string.wide)
José Fonseca2defc982010-11-22 16:59:10 +0000669
José Fonseca54f304a2012-01-14 19:33:08 +0000670 def visitConst(self, const):
José Fonsecaf182eda2012-04-05 19:59:56 +0100671 const_type = self.visit(const.type)
672 if const_type is const.type:
673 return const
674 else:
675 return Const(const_type)
José Fonsecac9edb832010-11-20 09:03:10 +0000676
José Fonseca54f304a2012-01-14 19:33:08 +0000677 def visitStruct(self, struct):
José Fonseca06aa2842011-05-05 07:55:54 +0100678 members = [(self.visit(type), name) for type, name in struct.members]
José Fonsecac9edb832010-11-20 09:03:10 +0000679 return Struct(struct.name, members)
680
José Fonseca54f304a2012-01-14 19:33:08 +0000681 def visitArray(self, array):
José Fonsecac9edb832010-11-20 09:03:10 +0000682 type = self.visit(array.type)
683 return Array(type, array.length)
684
José Fonseca54f304a2012-01-14 19:33:08 +0000685 def visitBlob(self, blob):
José Fonseca885f2652010-11-20 11:22:25 +0000686 type = self.visit(blob.type)
687 return Blob(type, blob.size)
688
José Fonseca54f304a2012-01-14 19:33:08 +0000689 def visitEnum(self, enum):
José Fonsecac9edb832010-11-20 09:03:10 +0000690 return enum
691
José Fonseca54f304a2012-01-14 19:33:08 +0000692 def visitBitmask(self, bitmask):
José Fonsecac9edb832010-11-20 09:03:10 +0000693 type = self.visit(bitmask.type)
694 return Bitmask(type, bitmask.values)
695
José Fonseca54f304a2012-01-14 19:33:08 +0000696 def visitPointer(self, pointer):
José Fonsecaf182eda2012-04-05 19:59:56 +0100697 pointer_type = self.visit(pointer.type)
698 if pointer_type is pointer.type:
699 return pointer
700 else:
701 return Pointer(pointer_type)
José Fonsecac9edb832010-11-20 09:03:10 +0000702
José Fonseca59ee88e2012-01-15 14:24:10 +0000703 def visitIntPointer(self, pointer):
704 return pointer
705
José Fonsecafbcf6832012-04-05 07:10:30 +0100706 def visitObjPointer(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 ObjPointer(pointer_type)
José Fonsecafbcf6832012-04-05 07:10:30 +0100712
José Fonseca59ee88e2012-01-15 14:24:10 +0000713 def visitLinearPointer(self, pointer):
José Fonsecaf182eda2012-04-05 19:59:56 +0100714 pointer_type = self.visit(pointer.type)
715 if pointer_type is pointer.type:
716 return pointer
717 else:
718 return LinearPointer(pointer_type)
José Fonseca59ee88e2012-01-15 14:24:10 +0000719
José Fonsecab89c5932012-04-01 22:47:11 +0200720 def visitReference(self, reference):
José Fonsecaf182eda2012-04-05 19:59:56 +0100721 reference_type = self.visit(reference.type)
722 if reference_type is reference.type:
723 return reference
724 else:
725 return Reference(reference_type)
José Fonsecab89c5932012-04-01 22:47:11 +0200726
José Fonseca54f304a2012-01-14 19:33:08 +0000727 def visitHandle(self, handle):
José Fonsecaf182eda2012-04-05 19:59:56 +0100728 handle_type = self.visit(handle.type)
729 if handle_type is handle.type:
730 return handle
731 else:
732 return Handle(handle.name, handle_type, range=handle.range, key=handle.key)
José Fonseca50d78d82010-11-23 22:13:14 +0000733
José Fonseca54f304a2012-01-14 19:33:08 +0000734 def visitAlias(self, alias):
José Fonsecaf182eda2012-04-05 19:59:56 +0100735 alias_type = self.visit(alias.type)
736 if alias_type is alias.type:
737 return alias
738 else:
739 return Alias(alias.expr, alias_type)
José Fonsecac9edb832010-11-20 09:03:10 +0000740
José Fonseca54f304a2012-01-14 19:33:08 +0000741 def visitOpaque(self, opaque):
José Fonsecac9edb832010-11-20 09:03:10 +0000742 return opaque
743
José Fonseca7814edf2012-01-31 10:55:49 +0000744 def visitInterface(self, interface, *args, **kwargs):
745 return interface
746
José Fonseca54f304a2012-01-14 19:33:08 +0000747 def visitPolymorphic(self, polymorphic):
José Fonseca54f304a2012-01-14 19:33:08 +0000748 switchExpr = polymorphic.switchExpr
749 switchTypes = [(expr, self.visit(type)) for expr, type in polymorphic.switchTypes]
José Fonsecaeb216e62012-11-20 11:08:08 +0000750 if polymorphic.defaultType is None:
751 defaultType = None
752 else:
753 defaultType = self.visit(polymorphic.defaultType)
José Fonsecab95e3722012-04-16 14:01:15 +0100754 return Polymorphic(switchExpr, switchTypes, defaultType, polymorphic.contextLess)
José Fonseca16d46dd2011-10-13 09:52:52 +0100755
José Fonsecac9edb832010-11-20 09:03:10 +0000756
José Fonseca0075f152012-04-14 20:25:52 +0100757class MutableRebuilder(Rebuilder):
758 '''Type visitor which derives a mutable type.'''
759
José Fonsecabcfc81b2012-08-07 21:07:22 +0100760 def visitString(self, string):
761 return string
762
José Fonseca0075f152012-04-14 20:25:52 +0100763 def visitConst(self, const):
764 # Strip out const qualifier
765 return const.type
766
767 def visitAlias(self, alias):
768 # Tear the alias on type changes
769 type = self.visit(alias.type)
770 if type is alias.type:
771 return alias
772 return type
773
774 def visitReference(self, reference):
775 # Strip out references
776 return reference.type
777
778
779class Traverser(Visitor):
780 '''Visitor which all types.'''
781
782 def visitVoid(self, void, *args, **kwargs):
783 pass
784
785 def visitLiteral(self, literal, *args, **kwargs):
786 pass
787
788 def visitString(self, string, *args, **kwargs):
789 pass
790
791 def visitConst(self, const, *args, **kwargs):
792 self.visit(const.type, *args, **kwargs)
793
794 def visitStruct(self, struct, *args, **kwargs):
795 for type, name in struct.members:
796 self.visit(type, *args, **kwargs)
797
798 def visitArray(self, array, *args, **kwargs):
799 self.visit(array.type, *args, **kwargs)
800
Andreas Hartmetzba7bb0d2013-07-07 22:51:12 +0200801 def visitAttribArray(self, attribs, *args, **kwargs):
802 for key, valueType in attribs.valueTypes:
Andreas Hartmetz7a0de292013-07-09 22:38:29 +0200803 if valueType is not None:
804 self.visit(valueType, *args, **kwargs)
Andreas Hartmetzba7bb0d2013-07-07 22:51:12 +0200805
José Fonseca0075f152012-04-14 20:25:52 +0100806 def visitBlob(self, array, *args, **kwargs):
807 pass
808
809 def visitEnum(self, enum, *args, **kwargs):
810 pass
811
812 def visitBitmask(self, bitmask, *args, **kwargs):
813 self.visit(bitmask.type, *args, **kwargs)
814
815 def visitPointer(self, pointer, *args, **kwargs):
816 self.visit(pointer.type, *args, **kwargs)
817
818 def visitIntPointer(self, pointer, *args, **kwargs):
819 pass
820
821 def visitObjPointer(self, pointer, *args, **kwargs):
822 self.visit(pointer.type, *args, **kwargs)
823
824 def visitLinearPointer(self, pointer, *args, **kwargs):
825 self.visit(pointer.type, *args, **kwargs)
826
827 def visitReference(self, reference, *args, **kwargs):
828 self.visit(reference.type, *args, **kwargs)
829
830 def visitHandle(self, handle, *args, **kwargs):
831 self.visit(handle.type, *args, **kwargs)
832
833 def visitAlias(self, alias, *args, **kwargs):
834 self.visit(alias.type, *args, **kwargs)
835
836 def visitOpaque(self, opaque, *args, **kwargs):
837 pass
838
839 def visitInterface(self, interface, *args, **kwargs):
840 if interface.base is not None:
841 self.visit(interface.base, *args, **kwargs)
842 for method in interface.iterMethods():
843 for arg in method.args:
844 self.visit(arg.type, *args, **kwargs)
845 self.visit(method.type, *args, **kwargs)
846
847 def visitPolymorphic(self, polymorphic, *args, **kwargs):
José Fonseca0075f152012-04-14 20:25:52 +0100848 for expr, type in polymorphic.switchTypes:
849 self.visit(type, *args, **kwargs)
José Fonsecaeb216e62012-11-20 11:08:08 +0000850 if polymorphic.defaultType is not None:
851 self.visit(polymorphic.defaultType, *args, **kwargs)
José Fonseca0075f152012-04-14 20:25:52 +0100852
853
854class Collector(Traverser):
José Fonseca9c4a2572012-01-13 23:21:10 +0000855 '''Visitor which collects all unique types as it traverses them.'''
José Fonsecae6a50bd2010-11-24 10:12:22 +0000856
857 def __init__(self):
858 self.__visited = set()
859 self.types = []
860
861 def visit(self, type):
862 if type in self.__visited:
863 return
864 self.__visited.add(type)
865 Visitor.visit(self, type)
866 self.types.append(type)
867
José Fonseca16d46dd2011-10-13 09:52:52 +0100868
José Fonsecadbf714b2012-11-20 17:03:43 +0000869class ExpanderMixin:
870 '''Mixin class that provides a bunch of methods to expand C expressions
871 from the specifications.'''
872
873 __structs = None
874 __indices = None
875
876 def expand(self, expr):
877 # Expand a C expression, replacing certain variables
878 if not isinstance(expr, basestring):
879 return expr
880 variables = {}
881
882 if self.__structs is not None:
883 variables['self'] = '(%s)' % self.__structs[0]
884 if self.__indices is not None:
885 variables['i'] = self.__indices[0]
886
887 expandedExpr = expr.format(**variables)
888 if expandedExpr != expr and 0:
889 sys.stderr.write(" %r -> %r\n" % (expr, expandedExpr))
890 return expandedExpr
891
892 def visitMember(self, member, structInstance, *args, **kwargs):
893 memberType, memberName = member
894 if memberName is None:
895 # Anonymous structure/union member
896 memberInstance = structInstance
897 else:
898 memberInstance = '(%s).%s' % (structInstance, memberName)
899 self.__structs = (structInstance, self.__structs)
900 try:
901 return self.visit(memberType, memberInstance, *args, **kwargs)
902 finally:
903 _, self.__structs = self.__structs
904
905 def visitElement(self, elementIndex, elementType, *args, **kwargs):
906 self.__indices = (elementIndex, self.__indices)
907 try:
908 return self.visit(elementType, *args, **kwargs)
909 finally:
910 _, self.__indices = self.__indices
911
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000912
José Fonseca81301932012-11-11 00:10:20 +0000913class Module:
914 '''A collection of functions.'''
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000915
José Fonseca68ec4122011-02-20 11:25:25 +0000916 def __init__(self, name = None):
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000917 self.name = name
918 self.headers = []
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000919 self.functions = []
920 self.interfaces = []
921
José Fonseca54f304a2012-01-14 19:33:08 +0000922 def addFunctions(self, functions):
José Fonseca81301932012-11-11 00:10:20 +0000923 self.functions.extend(functions)
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000924
José Fonseca54f304a2012-01-14 19:33:08 +0000925 def addInterfaces(self, interfaces):
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000926 self.interfaces.extend(interfaces)
927
José Fonseca81301932012-11-11 00:10:20 +0000928 def mergeModule(self, module):
929 self.headers.extend(module.headers)
930 self.functions.extend(module.functions)
931 self.interfaces.extend(module.interfaces)
José Fonseca68ec4122011-02-20 11:25:25 +0000932
José Fonseca1b6c8752012-04-15 14:33:00 +0100933 def getFunctionByName(self, name):
José Fonsecaeccec3e2011-02-20 09:01:25 +0000934 for function in self.functions:
935 if function.name == name:
936 return function
937 return None
938
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000939
José Fonseca81301932012-11-11 00:10:20 +0000940class API:
941 '''API abstraction.
942
943 Essentially, a collection of types, functions, and interfaces.
944 '''
945
946 def __init__(self, modules = None):
947 self.modules = []
948 if modules is not None:
949 self.modules.extend(modules)
950
951 def getAllTypes(self):
952 collector = Collector()
953 for module in self.modules:
954 for function in module.functions:
955 for arg in function.args:
956 collector.visit(arg.type)
957 collector.visit(function.type)
958 for interface in module.interfaces:
959 collector.visit(interface)
960 for method in interface.iterMethods():
961 for arg in method.args:
962 collector.visit(arg.type)
963 collector.visit(method.type)
964 return collector.types
965
966 def getAllFunctions(self):
967 functions = []
968 for module in self.modules:
969 functions.extend(module.functions)
970 return functions
971
972 def getAllInterfaces(self):
973 types = self.getAllTypes()
974 interfaces = [type for type in types if isinstance(type, Interface)]
975 for module in self.modules:
976 for interface in module.interfaces:
977 if interface not in interfaces:
978 interfaces.append(interface)
979 return interfaces
980
981 def addModule(self, module):
982 self.modules.append(module)
983
984 def getFunctionByName(self, name):
985 for module in self.modules:
986 for function in module.functions:
987 if function.name == name:
988 return function
989 return None
990
991
José Fonseca280a1762012-01-31 15:10:13 +0000992# C string (i.e., zero terminated)
José Fonsecabcfc81b2012-08-07 21:07:22 +0100993CString = String(Char)
994WString = String(WChar, wide=True)
995ConstCString = String(Const(Char))
996ConstWString = String(Const(WChar), wide=True)