blob: c72c264bf6096ccd8600564dcad7df103f77e64a [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
Jose Fonsecaa9b61382015-06-10 22:04:45 +0100271 def __init__(self, type_, length):
272 Type.__init__(self, type_.expr + " *")
273 self.type = type_
José Fonseca6fac5ae2010-11-29 16:09:13 +0000274 self.length = length
Jose Fonsecaa9b61382015-06-10 22:04:45 +0100275 if not isinstance(length, int):
276 assert isinstance(length, basestring)
277 # Check if length is actually a valid constant expression
278 try:
279 eval(length, {}, {})
280 except:
281 pass
282 else:
283 raise ValueError("length %r should be an integer" % length)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000284
285 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000286 return visitor.visitArray(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000287
288
Andreas Hartmetzba7bb0d2013-07-07 22:51:12 +0200289class AttribArray(Type):
290
José Fonseca48a92b92013-07-20 15:34:24 +0100291 def __init__(self, baseType, valueTypes, terminator = '0'):
José Fonseca77c10d82013-07-20 15:27:29 +0100292 self.baseType = baseType
José Fonseca48a92b92013-07-20 15:34:24 +0100293 Type.__init__(self, (Pointer(self.baseType)).expr)
Andreas Hartmetzba7bb0d2013-07-07 22:51:12 +0200294 self.valueTypes = valueTypes
Andreas Hartmetz7a0de292013-07-09 22:38:29 +0200295 self.terminator = terminator
296 self.hasKeysWithoutValues = False
297 for key, value in valueTypes:
298 if value is None:
299 self.hasKeysWithoutValues = True
Andreas Hartmetzba7bb0d2013-07-07 22:51:12 +0200300
301 def visit(self, visitor, *args, **kwargs):
302 return visitor.visitAttribArray(self, *args, **kwargs)
303
304
José Fonseca6fac5ae2010-11-29 16:09:13 +0000305class Blob(Type):
306
307 def __init__(self, type, size):
308 Type.__init__(self, type.expr + ' *')
309 self.type = type
310 self.size = size
311
312 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000313 return visitor.visitBlob(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000314
315
316class Struct(Type):
317
José Fonseca02c25002011-10-15 13:17:26 +0100318 __id = 0
319
José Fonseca6fac5ae2010-11-29 16:09:13 +0000320 def __init__(self, name, members):
321 Type.__init__(self, name)
José Fonseca02c25002011-10-15 13:17:26 +0100322
323 self.id = Struct.__id
324 Struct.__id += 1
325
José Fonseca6fac5ae2010-11-29 16:09:13 +0000326 self.name = name
José Fonsecadbf714b2012-11-20 17:03:43 +0000327 self.members = members
José Fonseca6fac5ae2010-11-29 16:09:13 +0000328
329 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000330 return visitor.visitStruct(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000331
332
José Fonsecadbf714b2012-11-20 17:03:43 +0000333def Union(kindExpr, kindTypes, contextLess=True):
José Fonsecaeb216e62012-11-20 11:08:08 +0000334 switchTypes = []
335 for kindCase, kindType, kindMemberName in kindTypes:
336 switchType = Struct(None, [(kindType, kindMemberName)])
337 switchTypes.append((kindCase, switchType))
338 return Polymorphic(kindExpr, switchTypes, contextLess=contextLess)
339
José Fonseca5b6fb752012-04-14 14:56:45 +0100340
José Fonseca6fac5ae2010-11-29 16:09:13 +0000341class Alias(Type):
342
343 def __init__(self, expr, type):
344 Type.__init__(self, expr)
345 self.type = type
346
347 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000348 return visitor.visitAlias(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000349
José Fonseca6fac5ae2010-11-29 16:09:13 +0000350class Arg:
351
José Fonseca9dd8f702012-04-07 10:42:50 +0100352 def __init__(self, type, name, input=True, output=False):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000353 self.type = type
354 self.name = name
José Fonseca9dd8f702012-04-07 10:42:50 +0100355 self.input = input
José Fonseca6fac5ae2010-11-29 16:09:13 +0000356 self.output = output
357 self.index = None
358
359 def __str__(self):
360 return '%s %s' % (self.type, self.name)
361
362
José Fonseca9dd8f702012-04-07 10:42:50 +0100363def In(type, name):
364 return Arg(type, name, input=True, output=False)
365
366def Out(type, name):
367 return Arg(type, name, input=False, output=True)
368
369def InOut(type, name):
370 return Arg(type, name, input=True, output=True)
371
372
José Fonseca6fac5ae2010-11-29 16:09:13 +0000373class Function:
374
José Fonseca84cea3b2012-05-09 21:12:30 +0100375 def __init__(self, type, name, args, call = '', fail = None, sideeffects=True, internal=False):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000376 self.type = type
377 self.name = name
378
379 self.args = []
380 index = 0
381 for arg in args:
José Fonseca8384ccb2011-05-25 10:12:02 +0100382 if not isinstance(arg, Arg):
383 if isinstance(arg, tuple):
384 arg_type, arg_name = arg
385 else:
386 arg_type = arg
387 arg_name = "arg%u" % index
José Fonseca6fac5ae2010-11-29 16:09:13 +0000388 arg = Arg(arg_type, arg_name)
389 arg.index = index
390 index += 1
391 self.args.append(arg)
392
393 self.call = call
394 self.fail = fail
395 self.sideeffects = sideeffects
José Fonseca84cea3b2012-05-09 21:12:30 +0100396 self.internal = internal
José Fonseca6fac5ae2010-11-29 16:09:13 +0000397
398 def prototype(self, name=None):
399 if name is not None:
400 name = name.strip()
401 else:
402 name = self.name
403 s = name
404 if self.call:
405 s = self.call + ' ' + s
406 if name.startswith('*'):
407 s = '(' + s + ')'
408 s = self.type.expr + ' ' + s
409 s += "("
410 if self.args:
411 s += ", ".join(["%s %s" % (arg.type, arg.name) for arg in self.args])
412 else:
413 s += "void"
414 s += ")"
415 return s
416
José Fonseca568ecc22012-01-15 13:57:03 +0000417 def argNames(self):
418 return [arg.name for arg in self.args]
419
José Fonseca999284f2013-02-19 13:29:26 +0000420 def getArgByName(self, name):
421 for arg in self.args:
422 if arg.name == name:
423 return arg
424 return None
425
José Fonseca50c2a142015-01-15 13:10:46 +0000426 def getArgByType(self, type):
427 for arg in self.args:
428 if arg.type is type:
429 return arg
430 return None
431
José Fonseca6fac5ae2010-11-29 16:09:13 +0000432
433def StdFunction(*args, **kwargs):
434 kwargs.setdefault('call', '__stdcall')
435 return Function(*args, **kwargs)
436
437
438def FunctionPointer(type, name, args, **kwargs):
439 # XXX: We should probably treat function pointers (callbacks or not) in a generic fashion
440 return Opaque(name)
441
442
443class Interface(Type):
444
445 def __init__(self, name, base=None):
446 Type.__init__(self, name)
447 self.name = name
448 self.base = base
449 self.methods = []
450
451 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000452 return visitor.visitInterface(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000453
José Fonsecabd086342012-04-18 19:58:32 +0100454 def getMethodByName(self, name):
José Fonsecad275d0a2012-04-30 23:18:05 +0100455 for method in self.iterMethods():
456 if method.name == name:
457 return method
José Fonsecabd086342012-04-18 19:58:32 +0100458 return None
459
José Fonseca54f304a2012-01-14 19:33:08 +0000460 def iterMethods(self):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000461 if self.base is not None:
José Fonseca54f304a2012-01-14 19:33:08 +0000462 for method in self.base.iterMethods():
José Fonseca6fac5ae2010-11-29 16:09:13 +0000463 yield method
464 for method in self.methods:
465 yield method
466 raise StopIteration
467
José Fonseca143e9252012-04-15 09:31:18 +0100468 def iterBases(self):
469 iface = self
470 while iface is not None:
471 yield iface
472 iface = iface.base
473 raise StopIteration
474
José Fonseca5abf5602013-05-30 14:00:44 +0100475 def hasBase(self, *bases):
476 for iface in self.iterBases():
477 if iface in bases:
478 return True
479 return False
480
José Fonseca4220b1b2012-02-03 19:05:29 +0000481 def iterBaseMethods(self):
482 if self.base is not None:
483 for iface, method in self.base.iterBaseMethods():
484 yield iface, method
485 for method in self.methods:
486 yield self, method
487 raise StopIteration
488
José Fonseca6fac5ae2010-11-29 16:09:13 +0000489
490class Method(Function):
491
José Fonseca43aa19f2012-11-10 09:29:38 +0000492 def __init__(self, type, name, args, call = '', const=False, sideeffects=True):
493 assert call == '__stdcall'
José Fonseca5b6fb752012-04-14 14:56:45 +0100494 Function.__init__(self, type, name, args, call = call, sideeffects=sideeffects)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000495 for index in range(len(self.args)):
496 self.args[index].index = index + 1
José Fonseca9dbeda62012-02-03 19:05:54 +0000497 self.const = const
498
499 def prototype(self, name=None):
500 s = Function.prototype(self, name)
501 if self.const:
502 s += ' const'
503 return s
José Fonseca6fac5ae2010-11-29 16:09:13 +0000504
José Fonsecabcb26b22012-04-15 08:42:25 +0100505
José Fonseca5b6fb752012-04-14 14:56:45 +0100506def StdMethod(*args, **kwargs):
507 kwargs.setdefault('call', '__stdcall')
508 return Method(*args, **kwargs)
509
José Fonseca6fac5ae2010-11-29 16:09:13 +0000510
José Fonseca6fac5ae2010-11-29 16:09:13 +0000511class String(Type):
José Fonsecabcfc81b2012-08-07 21:07:22 +0100512 '''Human-legible character string.'''
José Fonseca6fac5ae2010-11-29 16:09:13 +0000513
José Fonsecabcfc81b2012-08-07 21:07:22 +0100514 def __init__(self, type = Char, length = None, wide = False):
515 assert isinstance(type, Type)
516 Type.__init__(self, type.expr + ' *')
517 self.type = type
José Fonseca6fac5ae2010-11-29 16:09:13 +0000518 self.length = length
José Fonsecabcfc81b2012-08-07 21:07:22 +0100519 self.wide = wide
José Fonseca6fac5ae2010-11-29 16:09:13 +0000520
521 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000522 return visitor.visitString(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000523
José Fonseca6fac5ae2010-11-29 16:09:13 +0000524
525class Opaque(Type):
526 '''Opaque pointer.'''
527
528 def __init__(self, expr):
529 Type.__init__(self, expr)
530
531 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000532 return visitor.visitOpaque(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000533
534
535def OpaquePointer(type, *args):
536 return Opaque(type.expr + ' *')
537
538def OpaqueArray(type, size):
539 return Opaque(type.expr + ' *')
540
541def OpaqueBlob(type, size):
542 return Opaque(type.expr + ' *')
José Fonseca8a56d142008-07-09 12:18:08 +0900543
José Fonseca501f2862010-11-19 20:41:18 +0000544
José Fonseca16d46dd2011-10-13 09:52:52 +0100545class Polymorphic(Type):
546
José Fonsecaeb216e62012-11-20 11:08:08 +0000547 def __init__(self, switchExpr, switchTypes, defaultType=None, contextLess=True):
548 if defaultType is None:
549 Type.__init__(self, None)
550 contextLess = False
551 else:
552 Type.__init__(self, defaultType.expr)
José Fonseca54f304a2012-01-14 19:33:08 +0000553 self.switchExpr = switchExpr
554 self.switchTypes = switchTypes
José Fonsecab95e3722012-04-16 14:01:15 +0100555 self.defaultType = defaultType
556 self.contextLess = contextLess
José Fonseca16d46dd2011-10-13 09:52:52 +0100557
558 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000559 return visitor.visitPolymorphic(self, *args, **kwargs)
José Fonseca16d46dd2011-10-13 09:52:52 +0100560
José Fonseca54f304a2012-01-14 19:33:08 +0000561 def iterSwitch(self):
José Fonsecaeb216e62012-11-20 11:08:08 +0000562 cases = []
563 types = []
564
565 if self.defaultType is not None:
566 cases.append(['default'])
567 types.append(self.defaultType)
José Fonseca46161112011-10-14 10:04:55 +0100568
José Fonseca54f304a2012-01-14 19:33:08 +0000569 for expr, type in self.switchTypes:
José Fonseca46161112011-10-14 10:04:55 +0100570 case = 'case %s' % expr
571 try:
572 i = types.index(type)
573 except ValueError:
574 cases.append([case])
575 types.append(type)
576 else:
577 cases[i].append(case)
578
579 return zip(cases, types)
580
José Fonseca16d46dd2011-10-13 09:52:52 +0100581
José Fonsecab95e3722012-04-16 14:01:15 +0100582def EnumPolymorphic(enumName, switchExpr, switchTypes, defaultType, contextLess=True):
583 enumValues = [expr for expr, type in switchTypes]
584 enum = Enum(enumName, enumValues)
585 polymorphic = Polymorphic(switchExpr, switchTypes, defaultType, contextLess)
586 return enum, polymorphic
587
588
José Fonseca501f2862010-11-19 20:41:18 +0000589class Visitor:
José Fonseca9c4a2572012-01-13 23:21:10 +0000590 '''Abstract visitor for the type hierarchy.'''
José Fonseca501f2862010-11-19 20:41:18 +0000591
592 def visit(self, type, *args, **kwargs):
593 return type.visit(self, *args, **kwargs)
594
José Fonseca54f304a2012-01-14 19:33:08 +0000595 def visitVoid(self, void, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000596 raise NotImplementedError
597
José Fonseca54f304a2012-01-14 19:33:08 +0000598 def visitLiteral(self, literal, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000599 raise NotImplementedError
600
José Fonseca54f304a2012-01-14 19:33:08 +0000601 def visitString(self, string, *args, **kwargs):
José Fonseca2defc982010-11-22 16:59:10 +0000602 raise NotImplementedError
603
José Fonseca54f304a2012-01-14 19:33:08 +0000604 def visitConst(self, const, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000605 raise NotImplementedError
606
José Fonseca54f304a2012-01-14 19:33:08 +0000607 def visitStruct(self, struct, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000608 raise NotImplementedError
609
José Fonseca54f304a2012-01-14 19:33:08 +0000610 def visitArray(self, array, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000611 raise NotImplementedError
612
Andreas Hartmetzba7bb0d2013-07-07 22:51:12 +0200613 def visitAttribArray(self, array, *args, **kwargs):
614 raise NotImplementedError
615
José Fonseca54f304a2012-01-14 19:33:08 +0000616 def visitBlob(self, blob, *args, **kwargs):
José Fonseca885f2652010-11-20 11:22:25 +0000617 raise NotImplementedError
618
José Fonseca54f304a2012-01-14 19:33:08 +0000619 def visitEnum(self, enum, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000620 raise NotImplementedError
621
José Fonseca54f304a2012-01-14 19:33:08 +0000622 def visitBitmask(self, bitmask, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000623 raise NotImplementedError
624
José Fonseca54f304a2012-01-14 19:33:08 +0000625 def visitPointer(self, pointer, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000626 raise NotImplementedError
627
José Fonseca59ee88e2012-01-15 14:24:10 +0000628 def visitIntPointer(self, pointer, *args, **kwargs):
629 raise NotImplementedError
630
José Fonsecafbcf6832012-04-05 07:10:30 +0100631 def visitObjPointer(self, pointer, *args, **kwargs):
632 raise NotImplementedError
633
José Fonseca59ee88e2012-01-15 14:24:10 +0000634 def visitLinearPointer(self, pointer, *args, **kwargs):
635 raise NotImplementedError
636
José Fonsecab89c5932012-04-01 22:47:11 +0200637 def visitReference(self, reference, *args, **kwargs):
638 raise NotImplementedError
639
José Fonseca54f304a2012-01-14 19:33:08 +0000640 def visitHandle(self, handle, *args, **kwargs):
José Fonseca50d78d82010-11-23 22:13:14 +0000641 raise NotImplementedError
642
José Fonseca54f304a2012-01-14 19:33:08 +0000643 def visitAlias(self, alias, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000644 raise NotImplementedError
645
José Fonseca54f304a2012-01-14 19:33:08 +0000646 def visitOpaque(self, opaque, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000647 raise NotImplementedError
648
José Fonseca54f304a2012-01-14 19:33:08 +0000649 def visitInterface(self, interface, *args, **kwargs):
José Fonsecac356d6a2010-11-23 14:27:25 +0000650 raise NotImplementedError
651
José Fonseca54f304a2012-01-14 19:33:08 +0000652 def visitPolymorphic(self, polymorphic, *args, **kwargs):
José Fonseca16d46dd2011-10-13 09:52:52 +0100653 raise NotImplementedError
José Fonseca54f304a2012-01-14 19:33:08 +0000654 #return self.visit(polymorphic.defaultType, *args, **kwargs)
José Fonseca16d46dd2011-10-13 09:52:52 +0100655
José Fonsecac356d6a2010-11-23 14:27:25 +0000656
657class OnceVisitor(Visitor):
José Fonseca9c4a2572012-01-13 23:21:10 +0000658 '''Visitor that guarantees that each type is visited only once.'''
José Fonsecac356d6a2010-11-23 14:27:25 +0000659
660 def __init__(self):
661 self.__visited = set()
662
663 def visit(self, type, *args, **kwargs):
664 if type not in self.__visited:
665 self.__visited.add(type)
666 return type.visit(self, *args, **kwargs)
667 return None
668
José Fonseca501f2862010-11-19 20:41:18 +0000669
José Fonsecac9edb832010-11-20 09:03:10 +0000670class Rebuilder(Visitor):
José Fonseca9c4a2572012-01-13 23:21:10 +0000671 '''Visitor which rebuild types as it visits them.
672
673 By itself it is a no-op -- it is intended to be overwritten.
674 '''
José Fonsecac9edb832010-11-20 09:03:10 +0000675
José Fonseca54f304a2012-01-14 19:33:08 +0000676 def visitVoid(self, void):
José Fonsecac9edb832010-11-20 09:03:10 +0000677 return void
678
José Fonseca54f304a2012-01-14 19:33:08 +0000679 def visitLiteral(self, literal):
José Fonsecac9edb832010-11-20 09:03:10 +0000680 return literal
681
José Fonseca54f304a2012-01-14 19:33:08 +0000682 def visitString(self, string):
José Fonsecabcfc81b2012-08-07 21:07:22 +0100683 string_type = self.visit(string.type)
684 if string_type is string.type:
685 return string
686 else:
687 return String(string_type, string.length, string.wide)
José Fonseca2defc982010-11-22 16:59:10 +0000688
José Fonseca54f304a2012-01-14 19:33:08 +0000689 def visitConst(self, const):
José Fonsecaf182eda2012-04-05 19:59:56 +0100690 const_type = self.visit(const.type)
691 if const_type is const.type:
692 return const
693 else:
694 return Const(const_type)
José Fonsecac9edb832010-11-20 09:03:10 +0000695
José Fonseca54f304a2012-01-14 19:33:08 +0000696 def visitStruct(self, struct):
José Fonseca06aa2842011-05-05 07:55:54 +0100697 members = [(self.visit(type), name) for type, name in struct.members]
José Fonsecac9edb832010-11-20 09:03:10 +0000698 return Struct(struct.name, members)
699
José Fonseca54f304a2012-01-14 19:33:08 +0000700 def visitArray(self, array):
José Fonsecac9edb832010-11-20 09:03:10 +0000701 type = self.visit(array.type)
702 return Array(type, array.length)
703
José Fonseca54f304a2012-01-14 19:33:08 +0000704 def visitBlob(self, blob):
José Fonseca885f2652010-11-20 11:22:25 +0000705 type = self.visit(blob.type)
706 return Blob(type, blob.size)
707
José Fonseca54f304a2012-01-14 19:33:08 +0000708 def visitEnum(self, enum):
José Fonsecac9edb832010-11-20 09:03:10 +0000709 return enum
710
José Fonseca54f304a2012-01-14 19:33:08 +0000711 def visitBitmask(self, bitmask):
José Fonsecac9edb832010-11-20 09:03:10 +0000712 type = self.visit(bitmask.type)
713 return Bitmask(type, bitmask.values)
714
José Fonseca54f304a2012-01-14 19:33:08 +0000715 def visitPointer(self, pointer):
José Fonsecaf182eda2012-04-05 19:59:56 +0100716 pointer_type = self.visit(pointer.type)
717 if pointer_type is pointer.type:
718 return pointer
719 else:
720 return Pointer(pointer_type)
José Fonsecac9edb832010-11-20 09:03:10 +0000721
José Fonseca59ee88e2012-01-15 14:24:10 +0000722 def visitIntPointer(self, pointer):
723 return pointer
724
José Fonsecafbcf6832012-04-05 07:10:30 +0100725 def visitObjPointer(self, pointer):
José Fonsecaf182eda2012-04-05 19:59:56 +0100726 pointer_type = self.visit(pointer.type)
727 if pointer_type is pointer.type:
728 return pointer
729 else:
730 return ObjPointer(pointer_type)
José Fonsecafbcf6832012-04-05 07:10:30 +0100731
José Fonseca59ee88e2012-01-15 14:24:10 +0000732 def visitLinearPointer(self, pointer):
José Fonsecaf182eda2012-04-05 19:59:56 +0100733 pointer_type = self.visit(pointer.type)
734 if pointer_type is pointer.type:
735 return pointer
736 else:
737 return LinearPointer(pointer_type)
José Fonseca59ee88e2012-01-15 14:24:10 +0000738
José Fonsecab89c5932012-04-01 22:47:11 +0200739 def visitReference(self, reference):
José Fonsecaf182eda2012-04-05 19:59:56 +0100740 reference_type = self.visit(reference.type)
741 if reference_type is reference.type:
742 return reference
743 else:
744 return Reference(reference_type)
José Fonsecab89c5932012-04-01 22:47:11 +0200745
José Fonseca54f304a2012-01-14 19:33:08 +0000746 def visitHandle(self, handle):
José Fonsecaf182eda2012-04-05 19:59:56 +0100747 handle_type = self.visit(handle.type)
748 if handle_type is handle.type:
749 return handle
750 else:
751 return Handle(handle.name, handle_type, range=handle.range, key=handle.key)
José Fonseca50d78d82010-11-23 22:13:14 +0000752
José Fonseca54f304a2012-01-14 19:33:08 +0000753 def visitAlias(self, alias):
José Fonsecaf182eda2012-04-05 19:59:56 +0100754 alias_type = self.visit(alias.type)
755 if alias_type is alias.type:
756 return alias
757 else:
758 return Alias(alias.expr, alias_type)
José Fonsecac9edb832010-11-20 09:03:10 +0000759
José Fonseca54f304a2012-01-14 19:33:08 +0000760 def visitOpaque(self, opaque):
José Fonsecac9edb832010-11-20 09:03:10 +0000761 return opaque
762
José Fonseca7814edf2012-01-31 10:55:49 +0000763 def visitInterface(self, interface, *args, **kwargs):
764 return interface
765
José Fonseca54f304a2012-01-14 19:33:08 +0000766 def visitPolymorphic(self, polymorphic):
José Fonseca54f304a2012-01-14 19:33:08 +0000767 switchExpr = polymorphic.switchExpr
768 switchTypes = [(expr, self.visit(type)) for expr, type in polymorphic.switchTypes]
José Fonsecaeb216e62012-11-20 11:08:08 +0000769 if polymorphic.defaultType is None:
770 defaultType = None
771 else:
772 defaultType = self.visit(polymorphic.defaultType)
José Fonsecab95e3722012-04-16 14:01:15 +0100773 return Polymorphic(switchExpr, switchTypes, defaultType, polymorphic.contextLess)
José Fonseca16d46dd2011-10-13 09:52:52 +0100774
José Fonsecac9edb832010-11-20 09:03:10 +0000775
José Fonseca0075f152012-04-14 20:25:52 +0100776class MutableRebuilder(Rebuilder):
777 '''Type visitor which derives a mutable type.'''
778
José Fonsecabcfc81b2012-08-07 21:07:22 +0100779 def visitString(self, string):
780 return string
781
José Fonseca0075f152012-04-14 20:25:52 +0100782 def visitConst(self, const):
783 # Strip out const qualifier
784 return const.type
785
786 def visitAlias(self, alias):
787 # Tear the alias on type changes
788 type = self.visit(alias.type)
789 if type is alias.type:
790 return alias
791 return type
792
793 def visitReference(self, reference):
794 # Strip out references
795 return reference.type
796
797
798class Traverser(Visitor):
799 '''Visitor which all types.'''
800
801 def visitVoid(self, void, *args, **kwargs):
802 pass
803
804 def visitLiteral(self, literal, *args, **kwargs):
805 pass
806
807 def visitString(self, string, *args, **kwargs):
808 pass
809
810 def visitConst(self, const, *args, **kwargs):
811 self.visit(const.type, *args, **kwargs)
812
813 def visitStruct(self, struct, *args, **kwargs):
814 for type, name in struct.members:
815 self.visit(type, *args, **kwargs)
816
817 def visitArray(self, array, *args, **kwargs):
818 self.visit(array.type, *args, **kwargs)
819
Andreas Hartmetzba7bb0d2013-07-07 22:51:12 +0200820 def visitAttribArray(self, attribs, *args, **kwargs):
821 for key, valueType in attribs.valueTypes:
Andreas Hartmetz7a0de292013-07-09 22:38:29 +0200822 if valueType is not None:
823 self.visit(valueType, *args, **kwargs)
Andreas Hartmetzba7bb0d2013-07-07 22:51:12 +0200824
José Fonseca0075f152012-04-14 20:25:52 +0100825 def visitBlob(self, array, *args, **kwargs):
826 pass
827
828 def visitEnum(self, enum, *args, **kwargs):
829 pass
830
831 def visitBitmask(self, bitmask, *args, **kwargs):
832 self.visit(bitmask.type, *args, **kwargs)
833
834 def visitPointer(self, pointer, *args, **kwargs):
835 self.visit(pointer.type, *args, **kwargs)
836
837 def visitIntPointer(self, pointer, *args, **kwargs):
838 pass
839
840 def visitObjPointer(self, pointer, *args, **kwargs):
841 self.visit(pointer.type, *args, **kwargs)
842
843 def visitLinearPointer(self, pointer, *args, **kwargs):
844 self.visit(pointer.type, *args, **kwargs)
845
846 def visitReference(self, reference, *args, **kwargs):
847 self.visit(reference.type, *args, **kwargs)
848
849 def visitHandle(self, handle, *args, **kwargs):
850 self.visit(handle.type, *args, **kwargs)
851
852 def visitAlias(self, alias, *args, **kwargs):
853 self.visit(alias.type, *args, **kwargs)
854
855 def visitOpaque(self, opaque, *args, **kwargs):
856 pass
857
858 def visitInterface(self, interface, *args, **kwargs):
859 if interface.base is not None:
860 self.visit(interface.base, *args, **kwargs)
861 for method in interface.iterMethods():
862 for arg in method.args:
863 self.visit(arg.type, *args, **kwargs)
864 self.visit(method.type, *args, **kwargs)
865
866 def visitPolymorphic(self, polymorphic, *args, **kwargs):
José Fonseca0075f152012-04-14 20:25:52 +0100867 for expr, type in polymorphic.switchTypes:
868 self.visit(type, *args, **kwargs)
José Fonsecaeb216e62012-11-20 11:08:08 +0000869 if polymorphic.defaultType is not None:
870 self.visit(polymorphic.defaultType, *args, **kwargs)
José Fonseca0075f152012-04-14 20:25:52 +0100871
872
873class Collector(Traverser):
José Fonseca9c4a2572012-01-13 23:21:10 +0000874 '''Visitor which collects all unique types as it traverses them.'''
José Fonsecae6a50bd2010-11-24 10:12:22 +0000875
876 def __init__(self):
877 self.__visited = set()
878 self.types = []
879
880 def visit(self, type):
881 if type in self.__visited:
882 return
883 self.__visited.add(type)
884 Visitor.visit(self, type)
885 self.types.append(type)
886
José Fonseca16d46dd2011-10-13 09:52:52 +0100887
José Fonsecadbf714b2012-11-20 17:03:43 +0000888class ExpanderMixin:
889 '''Mixin class that provides a bunch of methods to expand C expressions
890 from the specifications.'''
891
892 __structs = None
893 __indices = None
894
895 def expand(self, expr):
896 # Expand a C expression, replacing certain variables
897 if not isinstance(expr, basestring):
898 return expr
899 variables = {}
900
901 if self.__structs is not None:
902 variables['self'] = '(%s)' % self.__structs[0]
903 if self.__indices is not None:
904 variables['i'] = self.__indices[0]
905
906 expandedExpr = expr.format(**variables)
907 if expandedExpr != expr and 0:
908 sys.stderr.write(" %r -> %r\n" % (expr, expandedExpr))
909 return expandedExpr
910
911 def visitMember(self, member, structInstance, *args, **kwargs):
912 memberType, memberName = member
913 if memberName is None:
914 # Anonymous structure/union member
915 memberInstance = structInstance
916 else:
917 memberInstance = '(%s).%s' % (structInstance, memberName)
918 self.__structs = (structInstance, self.__structs)
919 try:
920 return self.visit(memberType, memberInstance, *args, **kwargs)
921 finally:
922 _, self.__structs = self.__structs
923
924 def visitElement(self, elementIndex, elementType, *args, **kwargs):
925 self.__indices = (elementIndex, self.__indices)
926 try:
927 return self.visit(elementType, *args, **kwargs)
928 finally:
929 _, self.__indices = self.__indices
930
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000931
José Fonseca81301932012-11-11 00:10:20 +0000932class Module:
933 '''A collection of functions.'''
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000934
José Fonseca68ec4122011-02-20 11:25:25 +0000935 def __init__(self, name = None):
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000936 self.name = name
937 self.headers = []
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000938 self.functions = []
939 self.interfaces = []
940
José Fonseca54f304a2012-01-14 19:33:08 +0000941 def addFunctions(self, functions):
José Fonseca81301932012-11-11 00:10:20 +0000942 self.functions.extend(functions)
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000943
José Fonseca54f304a2012-01-14 19:33:08 +0000944 def addInterfaces(self, interfaces):
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000945 self.interfaces.extend(interfaces)
946
José Fonseca81301932012-11-11 00:10:20 +0000947 def mergeModule(self, module):
948 self.headers.extend(module.headers)
949 self.functions.extend(module.functions)
950 self.interfaces.extend(module.interfaces)
José Fonseca68ec4122011-02-20 11:25:25 +0000951
José Fonseca1b6c8752012-04-15 14:33:00 +0100952 def getFunctionByName(self, name):
José Fonsecaeccec3e2011-02-20 09:01:25 +0000953 for function in self.functions:
954 if function.name == name:
955 return function
956 return None
957
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000958
José Fonseca81301932012-11-11 00:10:20 +0000959class API:
960 '''API abstraction.
961
962 Essentially, a collection of types, functions, and interfaces.
963 '''
964
965 def __init__(self, modules = None):
966 self.modules = []
967 if modules is not None:
968 self.modules.extend(modules)
969
970 def getAllTypes(self):
971 collector = Collector()
972 for module in self.modules:
973 for function in module.functions:
974 for arg in function.args:
975 collector.visit(arg.type)
976 collector.visit(function.type)
977 for interface in module.interfaces:
978 collector.visit(interface)
979 for method in interface.iterMethods():
980 for arg in method.args:
981 collector.visit(arg.type)
982 collector.visit(method.type)
983 return collector.types
984
985 def getAllFunctions(self):
986 functions = []
987 for module in self.modules:
988 functions.extend(module.functions)
989 return functions
990
991 def getAllInterfaces(self):
992 types = self.getAllTypes()
993 interfaces = [type for type in types if isinstance(type, Interface)]
994 for module in self.modules:
995 for interface in module.interfaces:
996 if interface not in interfaces:
997 interfaces.append(interface)
998 return interfaces
999
1000 def addModule(self, module):
1001 self.modules.append(module)
1002
1003 def getFunctionByName(self, name):
1004 for module in self.modules:
1005 for function in module.functions:
1006 if function.name == name:
1007 return function
1008 return None
1009
1010
José Fonseca280a1762012-01-31 15:10:13 +00001011# C string (i.e., zero terminated)
José Fonsecabcfc81b2012-08-07 21:07:22 +01001012CString = String(Char)
1013WString = String(WChar, wide=True)
1014ConstCString = String(Const(Char))
1015ConstWString = String(Const(WChar), wide=True)