1 ##########################################################################
3 # Copyright 2008-2010 VMware, Inc.
6 # 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:
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
16 # 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
24 ##########################################################################/
37 def __init__(self, expr, id = ''):
41 assert char.isalnum() or char in '_ '
43 id = id.replace(' ', '_')
49 assert id not in Type.__all
57 def visit(self, visitor, *args, **kwargs):
58 raise NotImplementedError
65 Type.__init__(self, "void")
67 def visit(self, visitor, *args, **kwargs):
68 return visitor.visit_void(self, *args, **kwargs)
75 def __init__(self, expr, format, base=10):
76 Type.__init__(self, expr)
79 def visit(self, visitor, *args, **kwargs):
80 return visitor.visit_literal(self, *args, **kwargs)
85 def __init__(self, type):
87 if type.expr.startswith("const "):
88 expr = type.expr + " const"
90 expr = "const " + type.expr
92 Type.__init__(self, expr, 'C' + type.id)
96 def visit(self, visitor, *args, **kwargs):
97 return visitor.visit_const(self, *args, **kwargs)
102 def __init__(self, type):
103 Type.__init__(self, type.expr + " *", 'P' + type.id)
106 def visit(self, visitor, *args, **kwargs):
107 return visitor.visit_pointer(self, *args, **kwargs)
112 def __init__(self, name, type, range=None, key=None):
113 Type.__init__(self, type.expr, 'P' + type.id)
119 def visit(self, visitor, *args, **kwargs):
120 return visitor.visit_handle(self, *args, **kwargs)
123 def ConstPointer(type):
124 return Pointer(Const(type))
129 def __init__(self, name, values):
130 Type.__init__(self, name)
131 self.values = list(values)
133 def visit(self, visitor, *args, **kwargs):
134 return visitor.visit_enum(self, *args, **kwargs)
137 def FakeEnum(type, values):
138 return Enum(type.expr, values)
143 def __init__(self, type, values):
144 Type.__init__(self, type.expr)
148 def visit(self, visitor, *args, **kwargs):
149 return visitor.visit_bitmask(self, *args, **kwargs)
156 def __init__(self, type, length):
157 Type.__init__(self, type.expr + " *")
161 def visit(self, visitor, *args, **kwargs):
162 return visitor.visit_array(self, *args, **kwargs)
167 def __init__(self, type, size):
168 Type.__init__(self, type.expr + ' *')
172 def visit(self, visitor, *args, **kwargs):
173 return visitor.visit_blob(self, *args, **kwargs)
178 def __init__(self, name, members):
179 Type.__init__(self, name)
181 self.members = members
183 def visit(self, visitor, *args, **kwargs):
184 return visitor.visit_struct(self, *args, **kwargs)
189 def __init__(self, expr, type):
190 Type.__init__(self, expr)
193 def visit(self, visitor, *args, **kwargs):
194 return visitor.visit_alias(self, *args, **kwargs)
198 arg = Arg(type, name, output=True)
204 def __init__(self, type, name, output=False):
211 return '%s %s' % (self.type, self.name)
218 def __init__(self, type, name, args, call = '', fail = None, sideeffects=True):
219 self.id = Function.__id
228 if isinstance(arg, tuple):
229 arg_type, arg_name = arg
230 arg = Arg(arg_type, arg_name)
233 self.args.append(arg)
237 self.sideeffects = sideeffects
239 def prototype(self, name=None):
246 s = self.call + ' ' + s
247 if name.startswith('*'):
249 s = self.type.expr + ' ' + s
252 s += ", ".join(["%s %s" % (arg.type, arg.name) for arg in self.args])
259 def StdFunction(*args, **kwargs):
260 kwargs.setdefault('call', '__stdcall')
261 return Function(*args, **kwargs)
264 def FunctionPointer(type, name, args, **kwargs):
265 # XXX: We should probably treat function pointers (callbacks or not) in a generic fashion
269 class Interface(Type):
271 def __init__(self, name, base=None):
272 Type.__init__(self, name)
277 def visit(self, visitor, *args, **kwargs):
278 return visitor.visit_interface(self, *args, **kwargs)
280 def itermethods(self):
281 if self.base is not None:
282 for method in self.base.itermethods():
284 for method in self.methods:
289 class Method(Function):
291 def __init__(self, type, name, args):
292 Function.__init__(self, type, name, args, call = '__stdcall')
293 for index in range(len(self.args)):
294 self.args[index].index = index + 1
297 def WrapPointer(type):
303 def __init__(self, expr = "char *", length = None):
304 Type.__init__(self, expr)
307 def visit(self, visitor, *args, **kwargs):
308 return visitor.visit_string(self, *args, **kwargs)
310 # C string (i.e., zero terminated)
315 '''Opaque pointer.'''
317 def __init__(self, expr):
318 Type.__init__(self, expr)
320 def visit(self, visitor, *args, **kwargs):
321 return visitor.visit_opaque(self, *args, **kwargs)
324 def OpaquePointer(type, *args):
325 return Opaque(type.expr + ' *')
327 def OpaqueArray(type, size):
328 return Opaque(type.expr + ' *')
330 def OpaqueBlob(type, size):
331 return Opaque(type.expr + ' *')
336 def visit(self, type, *args, **kwargs):
337 return type.visit(self, *args, **kwargs)
339 def visit_void(self, void, *args, **kwargs):
340 raise NotImplementedError
342 def visit_literal(self, literal, *args, **kwargs):
343 raise NotImplementedError
345 def visit_string(self, string, *args, **kwargs):
346 raise NotImplementedError
348 def visit_const(self, const, *args, **kwargs):
349 raise NotImplementedError
351 def visit_struct(self, struct, *args, **kwargs):
352 raise NotImplementedError
354 def visit_array(self, array, *args, **kwargs):
355 raise NotImplementedError
357 def visit_blob(self, blob, *args, **kwargs):
358 raise NotImplementedError
360 def visit_enum(self, enum, *args, **kwargs):
361 raise NotImplementedError
363 def visit_bitmask(self, bitmask, *args, **kwargs):
364 raise NotImplementedError
366 def visit_pointer(self, pointer, *args, **kwargs):
367 raise NotImplementedError
369 def visit_handle(self, handle, *args, **kwargs):
370 raise NotImplementedError
372 def visit_alias(self, alias, *args, **kwargs):
373 raise NotImplementedError
375 def visit_opaque(self, opaque, *args, **kwargs):
376 raise NotImplementedError
378 def visit_interface(self, interface, *args, **kwargs):
379 raise NotImplementedError
382 class OnceVisitor(Visitor):
385 self.__visited = set()
387 def visit(self, type, *args, **kwargs):
388 if type not in self.__visited:
389 self.__visited.add(type)
390 return type.visit(self, *args, **kwargs)
394 class Rebuilder(Visitor):
396 def visit_void(self, void):
399 def visit_literal(self, literal):
402 def visit_string(self, string):
405 def visit_const(self, const):
406 return Const(const.type)
408 def visit_struct(self, struct):
409 members = [(self.visit(type), name) for type, name in struct.members]
410 return Struct(struct.name, members)
412 def visit_array(self, array):
413 type = self.visit(array.type)
414 return Array(type, array.length)
416 def visit_blob(self, blob):
417 type = self.visit(blob.type)
418 return Blob(type, blob.size)
420 def visit_enum(self, enum):
423 def visit_bitmask(self, bitmask):
424 type = self.visit(bitmask.type)
425 return Bitmask(type, bitmask.values)
427 def visit_pointer(self, pointer):
428 type = self.visit(pointer.type)
431 def visit_handle(self, handle):
432 type = self.visit(handle.type)
433 return Handle(handle.name, type, range=handle.range, key=handle.key)
435 def visit_alias(self, alias):
436 type = self.visit(alias.type)
437 return Alias(alias.expr, type)
439 def visit_opaque(self, opaque):
443 class Collector(Visitor):
447 self.__visited = set()
450 def visit(self, type):
451 if type in self.__visited:
453 self.__visited.add(type)
454 Visitor.visit(self, type)
455 self.types.append(type)
457 def visit_void(self, literal):
460 def visit_literal(self, literal):
463 def visit_string(self, string):
466 def visit_const(self, const):
467 self.visit(const.type)
469 def visit_struct(self, struct):
470 for type, name in struct.members:
473 def visit_array(self, array):
474 self.visit(array.type)
476 def visit_blob(self, array):
479 def visit_enum(self, enum):
482 def visit_bitmask(self, bitmask):
483 self.visit(bitmask.type)
485 def visit_pointer(self, pointer):
486 self.visit(pointer.type)
488 def visit_handle(self, handle):
489 self.visit(handle.type)
491 def visit_alias(self, alias):
492 self.visit(alias.type)
494 def visit_opaque(self, opaque):
497 def visit_interface(self, interface):
498 if interface.base is not None:
499 self.visit(interface.base)
500 for method in interface.itermethods():
501 for arg in method.args:
503 self.visit(method.type)
508 def __init__(self, name = None):
515 collector = Collector()
516 for function in self.functions:
517 for arg in function.args:
518 collector.visit(arg.type)
519 collector.visit(function.type)
520 for interface in self.interfaces:
521 collector.visit(interface)
522 for method in interface.itermethods():
523 for arg in method.args:
524 collector.visit(arg.type)
525 collector.visit(method.type)
526 return collector.types
528 def add_function(self, function):
529 self.functions.append(function)
531 def add_functions(self, functions):
532 for function in functions:
533 self.add_function(function)
535 def add_interface(self, interface):
536 self.interfaces.append(interface)
538 def add_interfaces(self, interfaces):
539 self.interfaces.extend(interfaces)
541 def add_api(self, api):
542 self.headers.extend(api.headers)
543 self.add_functions(api.functions)
544 self.add_interfaces(api.interfaces)
546 def get_function_by_name(self, name):
547 for function in self.functions:
548 if function.name == name:
553 Bool = Literal("bool", "Bool")
554 SChar = Literal("signed char", "SInt")
555 UChar = Literal("unsigned char", "UInt")
556 Short = Literal("short", "SInt")
557 Int = Literal("int", "SInt")
558 Long = Literal("long", "SInt")
559 LongLong = Literal("long long", "SInt")
560 UShort = Literal("unsigned short", "UInt")
561 UInt = Literal("unsigned int", "UInt")
562 ULong = Literal("unsigned long", "UInt")
563 ULongLong = Literal("unsigned long long", "UInt")
564 Float = Literal("float", "Float")
565 Double = Literal("double", "Double")
566 SizeT = Literal("size_t", "UInt")
567 WString = Literal("wchar_t *", "WString")
569 Int8 = Literal("int8_t", "SInt")
570 UInt8 = Literal("uint8_t", "UInt")
571 Int16 = Literal("int16_t", "SInt")
572 UInt16 = Literal("uint16_t", "UInt")
573 Int32 = Literal("int32_t", "SInt")
574 UInt32 = Literal("uint32_t", "UInt")
575 Int64 = Literal("int64_t", "SInt")
576 UInt64 = Literal("uint64_t", "UInt")