1 ##########################################################################
3 # Copyright 2008-2009 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 visit(self, type, *args, **kwargs):
38 return type.visit(self, *args, **kwargs)
40 def visit_void(self, void, *args, **kwargs):
41 raise NotImplementedError
43 def visit_literal(self, literal, *args, **kwargs):
44 raise NotImplementedError
46 def visit_string(self, string, *args, **kwargs):
47 raise NotImplementedError
49 def visit_const(self, const, *args, **kwargs):
50 raise NotImplementedError
52 def visit_struct(self, struct, *args, **kwargs):
53 raise NotImplementedError
55 def visit_array(self, array, *args, **kwargs):
56 raise NotImplementedError
58 def visit_blob(self, blob, *args, **kwargs):
59 raise NotImplementedError
61 def visit_enum(self, enum, *args, **kwargs):
62 raise NotImplementedError
64 def visit_bitmask(self, bitmask, *args, **kwargs):
65 raise NotImplementedError
67 def visit_pointer(self, pointer, *args, **kwargs):
68 raise NotImplementedError
70 def visit_handle(self, handle, *args, **kwargs):
71 raise NotImplementedError
73 def visit_alias(self, alias, *args, **kwargs):
74 raise NotImplementedError
76 def visit_opaque(self, opaque, *args, **kwargs):
77 raise NotImplementedError
79 def visit_interface(self, interface, *args, **kwargs):
80 raise NotImplementedError
83 class OnceVisitor(Visitor):
86 self.__visited = set()
88 def visit(self, type, *args, **kwargs):
89 if type not in self.__visited:
90 self.__visited.add(type)
91 return type.visit(self, *args, **kwargs)
95 class Rebuilder(Visitor):
97 def visit_void(self, void):
100 def visit_literal(self, literal):
103 def visit_string(self, string):
106 def visit_const(self, const):
107 return Const(const.type)
109 def visit_struct(self, struct):
110 members = [self.visit(member) for member in struct.members]
111 return Struct(struct.name, members)
113 def visit_array(self, array):
114 type = self.visit(array.type)
115 return Array(type, array.length)
117 def visit_blob(self, blob):
118 type = self.visit(blob.type)
119 return Blob(type, blob.size)
121 def visit_enum(self, enum):
124 def visit_bitmask(self, bitmask):
125 type = self.visit(bitmask.type)
126 return Bitmask(type, bitmask.values)
128 def visit_pointer(self, pointer):
129 type = self.visit(pointer.type)
132 def visit_handle(self, handle):
133 type = self.visit(handle.type)
134 return Handle(handle.name, type, handle.range)
136 def visit_alias(self, alias):
137 type = self.visit(alias.type)
138 return Alias(alias.expr, type)
140 def visit_opaque(self, opaque):
148 def __init__(self, expr, id = ''):
152 assert char.isalnum() or char in '_ '
154 id = id.replace(' ', '_')
158 id += str(Type.__seq)
160 assert id not in all_types
168 def visit(self, visitor, *args, **kwargs):
169 raise NotImplementedError
176 Type.__init__(self, "void")
178 def visit(self, visitor, *args, **kwargs):
179 return visitor.visit_void(self, *args, **kwargs)
184 class Concrete(Type):
187 print 'static void Dump%s(const %s &value);' % (self.id, self.expr)
190 print 'static void Dump%s(const %s &value) {' % (self.id, self.expr)
195 def _dump(self, instance):
196 raise NotImplementedError
198 def dump(self, instance):
199 print ' Dump%s(%s);' % (self.id, instance)
204 def __init__(self, expr, format, base=10):
205 Type.__init__(self, expr)
208 def visit(self, visitor, *args, **kwargs):
209 return visitor.visit_literal(self, *args, **kwargs)
214 def __init__(self, type):
216 if type.expr.startswith("const "):
217 expr = type.expr + " const"
219 expr = "const " + type.expr
221 Type.__init__(self, expr, 'C' + type.id)
225 def visit(self, visitor, *args, **kwargs):
226 return visitor.visit_const(self, *args, **kwargs)
231 def __init__(self, type):
232 Type.__init__(self, type.expr + " *", 'P' + type.id)
235 def visit(self, visitor, *args, **kwargs):
236 return visitor.visit_pointer(self, *args, **kwargs)
241 def __init__(self, name, type, range=None):
242 Type.__init__(self, type.expr, 'P' + type.id)
247 def visit(self, visitor, *args, **kwargs):
248 return visitor.visit_handle(self, *args, **kwargs)
251 def ConstPointer(type):
252 return Pointer(Const(type))
255 class Enum(Concrete):
257 def __init__(self, name, values):
258 Concrete.__init__(self, name)
261 def visit(self, visitor, *args, **kwargs):
262 return visitor.visit_enum(self, *args, **kwargs)
265 def FakeEnum(type, values):
266 return Enum(type.expr, values)
269 class Bitmask(Concrete):
271 def __init__(self, type, values):
272 Concrete.__init__(self, type.expr)
276 def visit(self, visitor, *args, **kwargs):
277 return visitor.visit_bitmask(self, *args, **kwargs)
284 def __init__(self, type, length):
285 Type.__init__(self, type.expr + " *")
289 def visit(self, visitor, *args, **kwargs):
290 return visitor.visit_array(self, *args, **kwargs)
295 def __init__(self, type, size):
296 Type.__init__(self, type.expr + ' *')
300 def visit(self, visitor, *args, **kwargs):
301 return visitor.visit_blob(self, *args, **kwargs)
304 class Struct(Concrete):
306 def __init__(self, name, members):
307 Concrete.__init__(self, name)
309 self.members = members
311 def visit(self, visitor, *args, **kwargs):
312 return visitor.visit_struct(self, *args, **kwargs)
317 def __init__(self, expr, type):
318 Type.__init__(self, expr)
321 def visit(self, visitor, *args, **kwargs):
322 return visitor.visit_alias(self, *args, **kwargs)
326 arg = Arg(type, name, output=True)
332 def __init__(self, type, name, output=False):
339 return '%s %s' % (self.type, self.name)
344 def __init__(self, type, name, args, call = '', fail = None, sideeffects=True, hidden=False):
351 if isinstance(arg, tuple):
352 arg_type, arg_name = arg
353 arg = Arg(arg_type, arg_name)
356 self.args.append(arg)
360 self.sideeffects = sideeffects
363 def prototype(self, name=None):
370 s = self.call + ' ' + s
371 if name.startswith('*'):
373 s = self.type.expr + ' ' + s
376 s += ", ".join(["%s %s" % (arg.type, arg.name) for arg in self.args])
383 def StdFunction(*args, **kwargs):
384 kwargs.setdefault('call', '__stdcall')
385 return Function(*args, **kwargs)
388 def FunctionPointer(type, name, args, **kwargs):
393 class Interface(Type):
395 def __init__(self, name, base=None):
396 Type.__init__(self, name)
401 def itermethods(self):
402 if self.base is not None:
403 for method in self.stdapi.itermethods():
405 for method in self.methods:
410 class Method(Function):
412 def __init__(self, type, name, args):
413 Function.__init__(self, type, name, args, call = '__stdcall')
419 def WrapPointer(type):
425 def __init__(self, expr = "char *", length = None):
426 Type.__init__(self, expr)
429 def visit(self, visitor, *args, **kwargs):
430 return visitor.visit_string(self, *args, **kwargs)
436 '''Opaque pointer.'''
438 def __init__(self, expr):
439 Type.__init__(self, expr)
441 def visit(self, visitor, *args, **kwargs):
442 return visitor.visit_opaque(self, *args, **kwargs)
445 def OpaquePointer(type, *args):
446 return Opaque(type.expr + ' *')
448 def OpaqueArray(type, size):
449 return Opaque(type.expr + ' *')
451 def OpaqueBlob(type, size):
452 return Opaque(type.expr + ' *')
455 class Collector(Visitor):
459 self.__visited = set()
462 def visit(self, type):
463 if type in self.__visited:
465 self.__visited.add(type)
466 Visitor.visit(self, type)
467 self.types.append(type)
469 def visit_void(self, literal):
472 def visit_literal(self, literal):
475 def visit_string(self, string):
478 def visit_const(self, const):
479 self.visit(const.type)
481 def visit_struct(self, struct):
482 for type, name in struct.members:
485 def visit_array(self, array):
486 self.visit(array.type)
488 def visit_blob(self, array):
491 def visit_enum(self, enum):
494 def visit_bitmask(self, bitmask):
495 self.visit(bitmask.type)
497 def visit_pointer(self, pointer):
498 self.visit(pointer.type)
500 def visit_handle(self, handle):
501 self.visit(handle.type)
503 def visit_alias(self, alias):
504 self.visit(alias.type)
506 def visit_opaque(self, opaque):
509 def visit_interface(self, interface):
515 def __init__(self, name):
522 collector = Collector()
523 for function in self.functions:
524 for arg in function.args:
525 collector.visit(arg.type)
526 collector.visit(function.type)
527 for interface in self.interfaces:
528 collector.visit(interface)
529 for method in interface.methods:
530 for arg in method.args:
531 collector.visit(arg.type)
532 collector.visit(method.type)
533 return collector.types
535 def add_function(self, function):
536 self.functions.append(function)
538 def add_functions(self, functions):
539 for function in functions:
540 self.add_function(function)
542 def add_interface(self, interface):
543 self.interfaces.append(interface)
545 def add_interfaces(self, interfaces):
546 self.interfaces.extend(interfaces)
549 Bool = Literal("bool", "Bool")
550 SChar = Literal("signed char", "SInt")
551 UChar = Literal("unsigned char", "UInt")
552 Short = Literal("short", "SInt")
553 Int = Literal("int", "SInt")
554 Long = Literal("long", "SInt")
555 LongLong = Literal("long long", "SInt")
556 UShort = Literal("unsigned short", "UInt")
557 UInt = Literal("unsigned int", "UInt")
558 ULong = Literal("unsigned long", "UInt")
559 ULongLong = Literal("unsigned long long", "UInt")
560 Float = Literal("float", "Float")
561 Double = Literal("double", "Float")
562 SizeT = Literal("size_t", "UInt")
563 WString = Literal("wchar_t *", "WString")