blob: 4b4808e458641173cf0b350a06a91cc715f85881 [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
273class Blob(Type):
274
275 def __init__(self, type, size):
276 Type.__init__(self, type.expr + ' *')
277 self.type = type
278 self.size = size
279
280 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000281 return visitor.visitBlob(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000282
283
284class Struct(Type):
285
José Fonseca02c25002011-10-15 13:17:26 +0100286 __id = 0
287
José Fonseca6fac5ae2010-11-29 16:09:13 +0000288 def __init__(self, name, members):
289 Type.__init__(self, name)
José Fonseca02c25002011-10-15 13:17:26 +0100290
291 self.id = Struct.__id
292 Struct.__id += 1
293
José Fonseca6fac5ae2010-11-29 16:09:13 +0000294 self.name = name
José Fonseca5b6fb752012-04-14 14:56:45 +0100295 self.members = []
296
297 # Eliminate anonymous unions
298 for type, name in members:
José Fonsecaeb216e62012-11-20 11:08:08 +0000299 if name is not None or isinstance(type, Polymorphic):
José Fonseca5b6fb752012-04-14 14:56:45 +0100300 self.members.append((type, name))
301 else:
302 assert isinstance(type, Union)
303 assert type.name is None
304 self.members.extend(type.members)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000305
306 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000307 return visitor.visitStruct(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000308
309
José Fonseca5b6fb752012-04-14 14:56:45 +0100310class Union(Type):
311
312 __id = 0
313
314 def __init__(self, name, members):
315 Type.__init__(self, name)
316
317 self.id = Union.__id
318 Union.__id += 1
319
320 self.name = name
321 self.members = members
322
José Fonsecaeb216e62012-11-20 11:08:08 +0000323def Union_(kindExpr, kindTypes, contextLess=True):
324 switchTypes = []
325 for kindCase, kindType, kindMemberName in kindTypes:
326 switchType = Struct(None, [(kindType, kindMemberName)])
327 switchTypes.append((kindCase, switchType))
328 return Polymorphic(kindExpr, switchTypes, contextLess=contextLess)
329
José Fonseca5b6fb752012-04-14 14:56:45 +0100330
José Fonseca6fac5ae2010-11-29 16:09:13 +0000331class Alias(Type):
332
333 def __init__(self, expr, type):
334 Type.__init__(self, expr)
335 self.type = type
336
337 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000338 return visitor.visitAlias(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000339
José Fonseca6fac5ae2010-11-29 16:09:13 +0000340class Arg:
341
José Fonseca9dd8f702012-04-07 10:42:50 +0100342 def __init__(self, type, name, input=True, output=False):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000343 self.type = type
344 self.name = name
José Fonseca9dd8f702012-04-07 10:42:50 +0100345 self.input = input
José Fonseca6fac5ae2010-11-29 16:09:13 +0000346 self.output = output
347 self.index = None
348
349 def __str__(self):
350 return '%s %s' % (self.type, self.name)
351
352
José Fonseca9dd8f702012-04-07 10:42:50 +0100353def In(type, name):
354 return Arg(type, name, input=True, output=False)
355
356def Out(type, name):
357 return Arg(type, name, input=False, output=True)
358
359def InOut(type, name):
360 return Arg(type, name, input=True, output=True)
361
362
José Fonseca6fac5ae2010-11-29 16:09:13 +0000363class Function:
364
José Fonseca46a48392011-10-14 11:34:27 +0100365 # 0-3 are reserved to memcpy, malloc, free, and realloc
366 __id = 4
José Fonseca6fac5ae2010-11-29 16:09:13 +0000367
José Fonseca84cea3b2012-05-09 21:12:30 +0100368 def __init__(self, type, name, args, call = '', fail = None, sideeffects=True, internal=False):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000369 self.id = Function.__id
370 Function.__id += 1
371
372 self.type = type
373 self.name = name
374
375 self.args = []
376 index = 0
377 for arg in args:
José Fonseca8384ccb2011-05-25 10:12:02 +0100378 if not isinstance(arg, Arg):
379 if isinstance(arg, tuple):
380 arg_type, arg_name = arg
381 else:
382 arg_type = arg
383 arg_name = "arg%u" % index
José Fonseca6fac5ae2010-11-29 16:09:13 +0000384 arg = Arg(arg_type, arg_name)
385 arg.index = index
386 index += 1
387 self.args.append(arg)
388
389 self.call = call
390 self.fail = fail
391 self.sideeffects = sideeffects
José Fonseca84cea3b2012-05-09 21:12:30 +0100392 self.internal = internal
José Fonseca6fac5ae2010-11-29 16:09:13 +0000393
394 def prototype(self, name=None):
395 if name is not None:
396 name = name.strip()
397 else:
398 name = self.name
399 s = name
400 if self.call:
401 s = self.call + ' ' + s
402 if name.startswith('*'):
403 s = '(' + s + ')'
404 s = self.type.expr + ' ' + s
405 s += "("
406 if self.args:
407 s += ", ".join(["%s %s" % (arg.type, arg.name) for arg in self.args])
408 else:
409 s += "void"
410 s += ")"
411 return s
412
José Fonseca568ecc22012-01-15 13:57:03 +0000413 def argNames(self):
414 return [arg.name for arg in self.args]
415
José Fonseca6fac5ae2010-11-29 16:09:13 +0000416
417def StdFunction(*args, **kwargs):
418 kwargs.setdefault('call', '__stdcall')
419 return Function(*args, **kwargs)
420
421
422def FunctionPointer(type, name, args, **kwargs):
423 # XXX: We should probably treat function pointers (callbacks or not) in a generic fashion
424 return Opaque(name)
425
426
427class Interface(Type):
428
429 def __init__(self, name, base=None):
430 Type.__init__(self, name)
431 self.name = name
432 self.base = base
433 self.methods = []
434
435 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000436 return visitor.visitInterface(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000437
José Fonsecabd086342012-04-18 19:58:32 +0100438 def getMethodByName(self, name):
José Fonsecad275d0a2012-04-30 23:18:05 +0100439 for method in self.iterMethods():
440 if method.name == name:
441 return method
José Fonsecabd086342012-04-18 19:58:32 +0100442 return None
443
José Fonseca54f304a2012-01-14 19:33:08 +0000444 def iterMethods(self):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000445 if self.base is not None:
José Fonseca54f304a2012-01-14 19:33:08 +0000446 for method in self.base.iterMethods():
José Fonseca6fac5ae2010-11-29 16:09:13 +0000447 yield method
448 for method in self.methods:
449 yield method
450 raise StopIteration
451
José Fonseca143e9252012-04-15 09:31:18 +0100452 def iterBases(self):
453 iface = self
454 while iface is not None:
455 yield iface
456 iface = iface.base
457 raise StopIteration
458
José Fonseca4220b1b2012-02-03 19:05:29 +0000459 def iterBaseMethods(self):
460 if self.base is not None:
461 for iface, method in self.base.iterBaseMethods():
462 yield iface, method
463 for method in self.methods:
464 yield self, method
465 raise StopIteration
466
José Fonseca6fac5ae2010-11-29 16:09:13 +0000467
468class Method(Function):
469
José Fonseca43aa19f2012-11-10 09:29:38 +0000470 def __init__(self, type, name, args, call = '', const=False, sideeffects=True):
471 assert call == '__stdcall'
José Fonseca5b6fb752012-04-14 14:56:45 +0100472 Function.__init__(self, type, name, args, call = call, sideeffects=sideeffects)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000473 for index in range(len(self.args)):
474 self.args[index].index = index + 1
José Fonseca9dbeda62012-02-03 19:05:54 +0000475 self.const = const
476
477 def prototype(self, name=None):
478 s = Function.prototype(self, name)
479 if self.const:
480 s += ' const'
481 return s
José Fonseca6fac5ae2010-11-29 16:09:13 +0000482
José Fonsecabcb26b22012-04-15 08:42:25 +0100483
José Fonseca5b6fb752012-04-14 14:56:45 +0100484def StdMethod(*args, **kwargs):
485 kwargs.setdefault('call', '__stdcall')
486 return Method(*args, **kwargs)
487
José Fonseca6fac5ae2010-11-29 16:09:13 +0000488
José Fonseca6fac5ae2010-11-29 16:09:13 +0000489class String(Type):
José Fonsecabcfc81b2012-08-07 21:07:22 +0100490 '''Human-legible character string.'''
José Fonseca6fac5ae2010-11-29 16:09:13 +0000491
José Fonsecabcfc81b2012-08-07 21:07:22 +0100492 def __init__(self, type = Char, length = None, wide = False):
493 assert isinstance(type, Type)
494 Type.__init__(self, type.expr + ' *')
495 self.type = type
José Fonseca6fac5ae2010-11-29 16:09:13 +0000496 self.length = length
José Fonsecabcfc81b2012-08-07 21:07:22 +0100497 self.wide = wide
José Fonseca6fac5ae2010-11-29 16:09:13 +0000498
499 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000500 return visitor.visitString(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000501
José Fonseca6fac5ae2010-11-29 16:09:13 +0000502
503class Opaque(Type):
504 '''Opaque pointer.'''
505
506 def __init__(self, expr):
507 Type.__init__(self, expr)
508
509 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000510 return visitor.visitOpaque(self, *args, **kwargs)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000511
512
513def OpaquePointer(type, *args):
514 return Opaque(type.expr + ' *')
515
516def OpaqueArray(type, size):
517 return Opaque(type.expr + ' *')
518
519def OpaqueBlob(type, size):
520 return Opaque(type.expr + ' *')
José Fonseca8a56d142008-07-09 12:18:08 +0900521
José Fonseca501f2862010-11-19 20:41:18 +0000522
José Fonseca16d46dd2011-10-13 09:52:52 +0100523class Polymorphic(Type):
524
José Fonsecaeb216e62012-11-20 11:08:08 +0000525 def __init__(self, switchExpr, switchTypes, defaultType=None, contextLess=True):
526 if defaultType is None:
527 Type.__init__(self, None)
528 contextLess = False
529 else:
530 Type.__init__(self, defaultType.expr)
José Fonseca54f304a2012-01-14 19:33:08 +0000531 self.switchExpr = switchExpr
532 self.switchTypes = switchTypes
José Fonsecab95e3722012-04-16 14:01:15 +0100533 self.defaultType = defaultType
534 self.contextLess = contextLess
José Fonseca16d46dd2011-10-13 09:52:52 +0100535
536 def visit(self, visitor, *args, **kwargs):
José Fonseca54f304a2012-01-14 19:33:08 +0000537 return visitor.visitPolymorphic(self, *args, **kwargs)
José Fonseca16d46dd2011-10-13 09:52:52 +0100538
José Fonseca54f304a2012-01-14 19:33:08 +0000539 def iterSwitch(self):
José Fonsecaeb216e62012-11-20 11:08:08 +0000540 cases = []
541 types = []
542
543 if self.defaultType is not None:
544 cases.append(['default'])
545 types.append(self.defaultType)
José Fonseca46161112011-10-14 10:04:55 +0100546
José Fonseca54f304a2012-01-14 19:33:08 +0000547 for expr, type in self.switchTypes:
José Fonseca46161112011-10-14 10:04:55 +0100548 case = 'case %s' % expr
549 try:
550 i = types.index(type)
551 except ValueError:
552 cases.append([case])
553 types.append(type)
554 else:
555 cases[i].append(case)
556
557 return zip(cases, types)
558
José Fonseca16d46dd2011-10-13 09:52:52 +0100559
José Fonsecab95e3722012-04-16 14:01:15 +0100560def EnumPolymorphic(enumName, switchExpr, switchTypes, defaultType, contextLess=True):
561 enumValues = [expr for expr, type in switchTypes]
562 enum = Enum(enumName, enumValues)
563 polymorphic = Polymorphic(switchExpr, switchTypes, defaultType, contextLess)
564 return enum, polymorphic
565
566
José Fonseca501f2862010-11-19 20:41:18 +0000567class Visitor:
José Fonseca9c4a2572012-01-13 23:21:10 +0000568 '''Abstract visitor for the type hierarchy.'''
José Fonseca501f2862010-11-19 20:41:18 +0000569
570 def visit(self, type, *args, **kwargs):
571 return type.visit(self, *args, **kwargs)
572
José Fonseca54f304a2012-01-14 19:33:08 +0000573 def visitVoid(self, void, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000574 raise NotImplementedError
575
José Fonseca54f304a2012-01-14 19:33:08 +0000576 def visitLiteral(self, literal, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000577 raise NotImplementedError
578
José Fonseca54f304a2012-01-14 19:33:08 +0000579 def visitString(self, string, *args, **kwargs):
José Fonseca2defc982010-11-22 16:59:10 +0000580 raise NotImplementedError
581
José Fonseca54f304a2012-01-14 19:33:08 +0000582 def visitConst(self, const, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000583 raise NotImplementedError
584
José Fonseca54f304a2012-01-14 19:33:08 +0000585 def visitStruct(self, struct, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000586 raise NotImplementedError
587
José Fonseca54f304a2012-01-14 19:33:08 +0000588 def visitArray(self, array, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000589 raise NotImplementedError
590
José Fonseca54f304a2012-01-14 19:33:08 +0000591 def visitBlob(self, blob, *args, **kwargs):
José Fonseca885f2652010-11-20 11:22:25 +0000592 raise NotImplementedError
593
José Fonseca54f304a2012-01-14 19:33:08 +0000594 def visitEnum(self, enum, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000595 raise NotImplementedError
596
José Fonseca54f304a2012-01-14 19:33:08 +0000597 def visitBitmask(self, bitmask, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000598 raise NotImplementedError
599
José Fonseca54f304a2012-01-14 19:33:08 +0000600 def visitPointer(self, pointer, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000601 raise NotImplementedError
602
José Fonseca59ee88e2012-01-15 14:24:10 +0000603 def visitIntPointer(self, pointer, *args, **kwargs):
604 raise NotImplementedError
605
José Fonsecafbcf6832012-04-05 07:10:30 +0100606 def visitObjPointer(self, pointer, *args, **kwargs):
607 raise NotImplementedError
608
José Fonseca59ee88e2012-01-15 14:24:10 +0000609 def visitLinearPointer(self, pointer, *args, **kwargs):
610 raise NotImplementedError
611
José Fonsecab89c5932012-04-01 22:47:11 +0200612 def visitReference(self, reference, *args, **kwargs):
613 raise NotImplementedError
614
José Fonseca54f304a2012-01-14 19:33:08 +0000615 def visitHandle(self, handle, *args, **kwargs):
José Fonseca50d78d82010-11-23 22:13:14 +0000616 raise NotImplementedError
617
José Fonseca54f304a2012-01-14 19:33:08 +0000618 def visitAlias(self, alias, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000619 raise NotImplementedError
620
José Fonseca54f304a2012-01-14 19:33:08 +0000621 def visitOpaque(self, opaque, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000622 raise NotImplementedError
623
José Fonseca54f304a2012-01-14 19:33:08 +0000624 def visitInterface(self, interface, *args, **kwargs):
José Fonsecac356d6a2010-11-23 14:27:25 +0000625 raise NotImplementedError
626
José Fonseca54f304a2012-01-14 19:33:08 +0000627 def visitPolymorphic(self, polymorphic, *args, **kwargs):
José Fonseca16d46dd2011-10-13 09:52:52 +0100628 raise NotImplementedError
José Fonseca54f304a2012-01-14 19:33:08 +0000629 #return self.visit(polymorphic.defaultType, *args, **kwargs)
José Fonseca16d46dd2011-10-13 09:52:52 +0100630
José Fonsecac356d6a2010-11-23 14:27:25 +0000631
632class OnceVisitor(Visitor):
José Fonseca9c4a2572012-01-13 23:21:10 +0000633 '''Visitor that guarantees that each type is visited only once.'''
José Fonsecac356d6a2010-11-23 14:27:25 +0000634
635 def __init__(self):
636 self.__visited = set()
637
638 def visit(self, type, *args, **kwargs):
639 if type not in self.__visited:
640 self.__visited.add(type)
641 return type.visit(self, *args, **kwargs)
642 return None
643
José Fonseca501f2862010-11-19 20:41:18 +0000644
José Fonsecac9edb832010-11-20 09:03:10 +0000645class Rebuilder(Visitor):
José Fonseca9c4a2572012-01-13 23:21:10 +0000646 '''Visitor which rebuild types as it visits them.
647
648 By itself it is a no-op -- it is intended to be overwritten.
649 '''
José Fonsecac9edb832010-11-20 09:03:10 +0000650
José Fonseca54f304a2012-01-14 19:33:08 +0000651 def visitVoid(self, void):
José Fonsecac9edb832010-11-20 09:03:10 +0000652 return void
653
José Fonseca54f304a2012-01-14 19:33:08 +0000654 def visitLiteral(self, literal):
José Fonsecac9edb832010-11-20 09:03:10 +0000655 return literal
656
José Fonseca54f304a2012-01-14 19:33:08 +0000657 def visitString(self, string):
José Fonsecabcfc81b2012-08-07 21:07:22 +0100658 string_type = self.visit(string.type)
659 if string_type is string.type:
660 return string
661 else:
662 return String(string_type, string.length, string.wide)
José Fonseca2defc982010-11-22 16:59:10 +0000663
José Fonseca54f304a2012-01-14 19:33:08 +0000664 def visitConst(self, const):
José Fonsecaf182eda2012-04-05 19:59:56 +0100665 const_type = self.visit(const.type)
666 if const_type is const.type:
667 return const
668 else:
669 return Const(const_type)
José Fonsecac9edb832010-11-20 09:03:10 +0000670
José Fonseca54f304a2012-01-14 19:33:08 +0000671 def visitStruct(self, struct):
José Fonseca06aa2842011-05-05 07:55:54 +0100672 members = [(self.visit(type), name) for type, name in struct.members]
José Fonsecac9edb832010-11-20 09:03:10 +0000673 return Struct(struct.name, members)
674
José Fonseca54f304a2012-01-14 19:33:08 +0000675 def visitArray(self, array):
José Fonsecac9edb832010-11-20 09:03:10 +0000676 type = self.visit(array.type)
677 return Array(type, array.length)
678
José Fonseca54f304a2012-01-14 19:33:08 +0000679 def visitBlob(self, blob):
José Fonseca885f2652010-11-20 11:22:25 +0000680 type = self.visit(blob.type)
681 return Blob(type, blob.size)
682
José Fonseca54f304a2012-01-14 19:33:08 +0000683 def visitEnum(self, enum):
José Fonsecac9edb832010-11-20 09:03:10 +0000684 return enum
685
José Fonseca54f304a2012-01-14 19:33:08 +0000686 def visitBitmask(self, bitmask):
José Fonsecac9edb832010-11-20 09:03:10 +0000687 type = self.visit(bitmask.type)
688 return Bitmask(type, bitmask.values)
689
José Fonseca54f304a2012-01-14 19:33:08 +0000690 def visitPointer(self, pointer):
José Fonsecaf182eda2012-04-05 19:59:56 +0100691 pointer_type = self.visit(pointer.type)
692 if pointer_type is pointer.type:
693 return pointer
694 else:
695 return Pointer(pointer_type)
José Fonsecac9edb832010-11-20 09:03:10 +0000696
José Fonseca59ee88e2012-01-15 14:24:10 +0000697 def visitIntPointer(self, pointer):
698 return pointer
699
José Fonsecafbcf6832012-04-05 07:10:30 +0100700 def visitObjPointer(self, pointer):
José Fonsecaf182eda2012-04-05 19:59:56 +0100701 pointer_type = self.visit(pointer.type)
702 if pointer_type is pointer.type:
703 return pointer
704 else:
705 return ObjPointer(pointer_type)
José Fonsecafbcf6832012-04-05 07:10:30 +0100706
José Fonseca59ee88e2012-01-15 14:24:10 +0000707 def visitLinearPointer(self, pointer):
José Fonsecaf182eda2012-04-05 19:59:56 +0100708 pointer_type = self.visit(pointer.type)
709 if pointer_type is pointer.type:
710 return pointer
711 else:
712 return LinearPointer(pointer_type)
José Fonseca59ee88e2012-01-15 14:24:10 +0000713
José Fonsecab89c5932012-04-01 22:47:11 +0200714 def visitReference(self, reference):
José Fonsecaf182eda2012-04-05 19:59:56 +0100715 reference_type = self.visit(reference.type)
716 if reference_type is reference.type:
717 return reference
718 else:
719 return Reference(reference_type)
José Fonsecab89c5932012-04-01 22:47:11 +0200720
José Fonseca54f304a2012-01-14 19:33:08 +0000721 def visitHandle(self, handle):
José Fonsecaf182eda2012-04-05 19:59:56 +0100722 handle_type = self.visit(handle.type)
723 if handle_type is handle.type:
724 return handle
725 else:
726 return Handle(handle.name, handle_type, range=handle.range, key=handle.key)
José Fonseca50d78d82010-11-23 22:13:14 +0000727
José Fonseca54f304a2012-01-14 19:33:08 +0000728 def visitAlias(self, alias):
José Fonsecaf182eda2012-04-05 19:59:56 +0100729 alias_type = self.visit(alias.type)
730 if alias_type is alias.type:
731 return alias
732 else:
733 return Alias(alias.expr, alias_type)
José Fonsecac9edb832010-11-20 09:03:10 +0000734
José Fonseca54f304a2012-01-14 19:33:08 +0000735 def visitOpaque(self, opaque):
José Fonsecac9edb832010-11-20 09:03:10 +0000736 return opaque
737
José Fonseca7814edf2012-01-31 10:55:49 +0000738 def visitInterface(self, interface, *args, **kwargs):
739 return interface
740
José Fonseca54f304a2012-01-14 19:33:08 +0000741 def visitPolymorphic(self, polymorphic):
José Fonseca54f304a2012-01-14 19:33:08 +0000742 switchExpr = polymorphic.switchExpr
743 switchTypes = [(expr, self.visit(type)) for expr, type in polymorphic.switchTypes]
José Fonsecaeb216e62012-11-20 11:08:08 +0000744 if polymorphic.defaultType is None:
745 defaultType = None
746 else:
747 defaultType = self.visit(polymorphic.defaultType)
José Fonsecab95e3722012-04-16 14:01:15 +0100748 return Polymorphic(switchExpr, switchTypes, defaultType, polymorphic.contextLess)
José Fonseca16d46dd2011-10-13 09:52:52 +0100749
José Fonsecac9edb832010-11-20 09:03:10 +0000750
José Fonseca0075f152012-04-14 20:25:52 +0100751class MutableRebuilder(Rebuilder):
752 '''Type visitor which derives a mutable type.'''
753
José Fonsecabcfc81b2012-08-07 21:07:22 +0100754 def visitString(self, string):
755 return string
756
José Fonseca0075f152012-04-14 20:25:52 +0100757 def visitConst(self, const):
758 # Strip out const qualifier
759 return const.type
760
761 def visitAlias(self, alias):
762 # Tear the alias on type changes
763 type = self.visit(alias.type)
764 if type is alias.type:
765 return alias
766 return type
767
768 def visitReference(self, reference):
769 # Strip out references
770 return reference.type
771
772
773class Traverser(Visitor):
774 '''Visitor which all types.'''
775
776 def visitVoid(self, void, *args, **kwargs):
777 pass
778
779 def visitLiteral(self, literal, *args, **kwargs):
780 pass
781
782 def visitString(self, string, *args, **kwargs):
783 pass
784
785 def visitConst(self, const, *args, **kwargs):
786 self.visit(const.type, *args, **kwargs)
787
788 def visitStruct(self, struct, *args, **kwargs):
789 for type, name in struct.members:
790 self.visit(type, *args, **kwargs)
791
792 def visitArray(self, array, *args, **kwargs):
793 self.visit(array.type, *args, **kwargs)
794
795 def visitBlob(self, array, *args, **kwargs):
796 pass
797
798 def visitEnum(self, enum, *args, **kwargs):
799 pass
800
801 def visitBitmask(self, bitmask, *args, **kwargs):
802 self.visit(bitmask.type, *args, **kwargs)
803
804 def visitPointer(self, pointer, *args, **kwargs):
805 self.visit(pointer.type, *args, **kwargs)
806
807 def visitIntPointer(self, pointer, *args, **kwargs):
808 pass
809
810 def visitObjPointer(self, pointer, *args, **kwargs):
811 self.visit(pointer.type, *args, **kwargs)
812
813 def visitLinearPointer(self, pointer, *args, **kwargs):
814 self.visit(pointer.type, *args, **kwargs)
815
816 def visitReference(self, reference, *args, **kwargs):
817 self.visit(reference.type, *args, **kwargs)
818
819 def visitHandle(self, handle, *args, **kwargs):
820 self.visit(handle.type, *args, **kwargs)
821
822 def visitAlias(self, alias, *args, **kwargs):
823 self.visit(alias.type, *args, **kwargs)
824
825 def visitOpaque(self, opaque, *args, **kwargs):
826 pass
827
828 def visitInterface(self, interface, *args, **kwargs):
829 if interface.base is not None:
830 self.visit(interface.base, *args, **kwargs)
831 for method in interface.iterMethods():
832 for arg in method.args:
833 self.visit(arg.type, *args, **kwargs)
834 self.visit(method.type, *args, **kwargs)
835
836 def visitPolymorphic(self, polymorphic, *args, **kwargs):
José Fonseca0075f152012-04-14 20:25:52 +0100837 for expr, type in polymorphic.switchTypes:
838 self.visit(type, *args, **kwargs)
José Fonsecaeb216e62012-11-20 11:08:08 +0000839 if polymorphic.defaultType is not None:
840 self.visit(polymorphic.defaultType, *args, **kwargs)
José Fonseca0075f152012-04-14 20:25:52 +0100841
842
843class Collector(Traverser):
José Fonseca9c4a2572012-01-13 23:21:10 +0000844 '''Visitor which collects all unique types as it traverses them.'''
José Fonsecae6a50bd2010-11-24 10:12:22 +0000845
846 def __init__(self):
847 self.__visited = set()
848 self.types = []
849
850 def visit(self, type):
851 if type in self.__visited:
852 return
853 self.__visited.add(type)
854 Visitor.visit(self, type)
855 self.types.append(type)
856
José Fonseca16d46dd2011-10-13 09:52:52 +0100857
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000858
José Fonseca81301932012-11-11 00:10:20 +0000859class Module:
860 '''A collection of functions.'''
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000861
José Fonseca68ec4122011-02-20 11:25:25 +0000862 def __init__(self, name = None):
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000863 self.name = name
864 self.headers = []
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000865 self.functions = []
866 self.interfaces = []
867
José Fonseca54f304a2012-01-14 19:33:08 +0000868 def addFunctions(self, functions):
José Fonseca81301932012-11-11 00:10:20 +0000869 self.functions.extend(functions)
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000870
José Fonseca54f304a2012-01-14 19:33:08 +0000871 def addInterfaces(self, interfaces):
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000872 self.interfaces.extend(interfaces)
873
José Fonseca81301932012-11-11 00:10:20 +0000874 def mergeModule(self, module):
875 self.headers.extend(module.headers)
876 self.functions.extend(module.functions)
877 self.interfaces.extend(module.interfaces)
José Fonseca68ec4122011-02-20 11:25:25 +0000878
José Fonseca1b6c8752012-04-15 14:33:00 +0100879 def getFunctionByName(self, name):
José Fonsecaeccec3e2011-02-20 09:01:25 +0000880 for function in self.functions:
881 if function.name == name:
882 return function
883 return None
884
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000885
José Fonseca81301932012-11-11 00:10:20 +0000886class API:
887 '''API abstraction.
888
889 Essentially, a collection of types, functions, and interfaces.
890 '''
891
892 def __init__(self, modules = None):
893 self.modules = []
894 if modules is not None:
895 self.modules.extend(modules)
896
897 def getAllTypes(self):
898 collector = Collector()
899 for module in self.modules:
900 for function in module.functions:
901 for arg in function.args:
902 collector.visit(arg.type)
903 collector.visit(function.type)
904 for interface in module.interfaces:
905 collector.visit(interface)
906 for method in interface.iterMethods():
907 for arg in method.args:
908 collector.visit(arg.type)
909 collector.visit(method.type)
910 return collector.types
911
912 def getAllFunctions(self):
913 functions = []
914 for module in self.modules:
915 functions.extend(module.functions)
916 return functions
917
918 def getAllInterfaces(self):
919 types = self.getAllTypes()
920 interfaces = [type for type in types if isinstance(type, Interface)]
921 for module in self.modules:
922 for interface in module.interfaces:
923 if interface not in interfaces:
924 interfaces.append(interface)
925 return interfaces
926
927 def addModule(self, module):
928 self.modules.append(module)
929
930 def getFunctionByName(self, name):
931 for module in self.modules:
932 for function in module.functions:
933 if function.name == name:
934 return function
935 return None
936
937
José Fonseca280a1762012-01-31 15:10:13 +0000938# C string (i.e., zero terminated)
José Fonsecabcfc81b2012-08-07 21:07:22 +0100939CString = String(Char)
940WString = String(WChar, wide=True)
941ConstCString = String(Const(Char))
942ConstWString = String(Const(WChar), wide=True)