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))
131 def __init__(self, name, values):
132 Type.__init__(self, name)
133 self.vid = Enum.__vid
134 Enum.__vid += len(values)
135 self.values = list(values)
137 def visit(self, visitor, *args, **kwargs):
138 return visitor.visit_enum(self, *args, **kwargs)
141 def FakeEnum(type, values):
142 return Enum(type.expr, values)
147 def __init__(self, type, values):
148 Type.__init__(self, type.expr)
152 def visit(self, visitor, *args, **kwargs):
153 return visitor.visit_bitmask(self, *args, **kwargs)
160 def __init__(self, type, length):
161 Type.__init__(self, type.expr + " *")
165 def visit(self, visitor, *args, **kwargs):
166 return visitor.visit_array(self, *args, **kwargs)
171 def __init__(self, type, size):
172 Type.__init__(self, type.expr + ' *')
176 def visit(self, visitor, *args, **kwargs):
177 return visitor.visit_blob(self, *args, **kwargs)
182 def __init__(self, name, members):
183 Type.__init__(self, name)
185 self.members = members
187 def visit(self, visitor, *args, **kwargs):
188 return visitor.visit_struct(self, *args, **kwargs)
193 def __init__(self, expr, type):
194 Type.__init__(self, expr)
197 def visit(self, visitor, *args, **kwargs):
198 return visitor.visit_alias(self, *args, **kwargs)
202 arg = Arg(type, name, output=True)
208 def __init__(self, type, name, output=False):
215 return '%s %s' % (self.type, self.name)
222 def __init__(self, type, name, args, call = '', fail = None, sideeffects=True, hidden=False):
223 self.id = Function.__id
232 if isinstance(arg, tuple):
233 arg_type, arg_name = arg
234 arg = Arg(arg_type, arg_name)
237 self.args.append(arg)
241 self.sideeffects = sideeffects
244 def prototype(self, name=None):
251 s = self.call + ' ' + s
252 if name.startswith('*'):
254 s = self.type.expr + ' ' + s
257 s += ", ".join(["%s %s" % (arg.type, arg.name) for arg in self.args])
264 def StdFunction(*args, **kwargs):
265 kwargs.setdefault('call', '__stdcall')
266 return Function(*args, **kwargs)
269 def FunctionPointer(type, name, args, **kwargs):
270 # XXX: We should probably treat function pointers (callbacks or not) in a generic fashion
274 class Interface(Type):
276 def __init__(self, name, base=None):
277 Type.__init__(self, name)
282 def visit(self, visitor, *args, **kwargs):
283 return visitor.visit_interface(self, *args, **kwargs)
285 def itermethods(self):
286 if self.base is not None:
287 for method in self.base.itermethods():
289 for method in self.methods:
294 class Method(Function):
296 def __init__(self, type, name, args):
297 Function.__init__(self, type, name, args, call = '__stdcall')
298 for index in range(len(self.args)):
299 self.args[index].index = index + 1
302 def WrapPointer(type):
308 def __init__(self, expr = "char *", length = None):
309 Type.__init__(self, expr)
312 def visit(self, visitor, *args, **kwargs):
313 return visitor.visit_string(self, *args, **kwargs)
315 # C string (i.e., zero terminated)
320 '''Opaque pointer.'''
322 def __init__(self, expr):
323 Type.__init__(self, expr)
325 def visit(self, visitor, *args, **kwargs):
326 return visitor.visit_opaque(self, *args, **kwargs)
329 def OpaquePointer(type, *args):
330 return Opaque(type.expr + ' *')
332 def OpaqueArray(type, size):
333 return Opaque(type.expr + ' *')
335 def OpaqueBlob(type, size):
336 return Opaque(type.expr + ' *')
341 def visit(self, type, *args, **kwargs):
342 return type.visit(self, *args, **kwargs)
344 def visit_void(self, void, *args, **kwargs):
345 raise NotImplementedError
347 def visit_literal(self, literal, *args, **kwargs):
348 raise NotImplementedError
350 def visit_string(self, string, *args, **kwargs):
351 raise NotImplementedError
353 def visit_const(self, const, *args, **kwargs):
354 raise NotImplementedError
356 def visit_struct(self, struct, *args, **kwargs):
357 raise NotImplementedError
359 def visit_array(self, array, *args, **kwargs):
360 raise NotImplementedError
362 def visit_blob(self, blob, *args, **kwargs):
363 raise NotImplementedError
365 def visit_enum(self, enum, *args, **kwargs):
366 raise NotImplementedError
368 def visit_bitmask(self, bitmask, *args, **kwargs):
369 raise NotImplementedError
371 def visit_pointer(self, pointer, *args, **kwargs):
372 raise NotImplementedError
374 def visit_handle(self, handle, *args, **kwargs):
375 raise NotImplementedError
377 def visit_alias(self, alias, *args, **kwargs):
378 raise NotImplementedError
380 def visit_opaque(self, opaque, *args, **kwargs):
381 raise NotImplementedError
383 def visit_interface(self, interface, *args, **kwargs):
384 raise NotImplementedError
387 class OnceVisitor(Visitor):
390 self.__visited = set()
392 def visit(self, type, *args, **kwargs):
393 if type not in self.__visited:
394 self.__visited.add(type)
395 return type.visit(self, *args, **kwargs)
399 class Rebuilder(Visitor):
401 def visit_void(self, void):
404 def visit_literal(self, literal):
407 def visit_string(self, string):
410 def visit_const(self, const):
411 return Const(const.type)
413 def visit_struct(self, struct):
414 members = [self.visit(member) for member in struct.members]
415 return Struct(struct.name, members)
417 def visit_array(self, array):
418 type = self.visit(array.type)
419 return Array(type, array.length)
421 def visit_blob(self, blob):
422 type = self.visit(blob.type)
423 return Blob(type, blob.size)
425 def visit_enum(self, enum):
428 def visit_bitmask(self, bitmask):
429 type = self.visit(bitmask.type)
430 return Bitmask(type, bitmask.values)
432 def visit_pointer(self, pointer):
433 type = self.visit(pointer.type)
436 def visit_handle(self, handle):
437 type = self.visit(handle.type)
438 return Handle(handle.name, type, range=handle.range, key=handle.key)
440 def visit_alias(self, alias):
441 type = self.visit(alias.type)
442 return Alias(alias.expr, type)
444 def visit_opaque(self, opaque):
448 class Collector(Visitor):
452 self.__visited = set()
455 def visit(self, type):
456 if type in self.__visited:
458 self.__visited.add(type)
459 Visitor.visit(self, type)
460 self.types.append(type)
462 def visit_void(self, literal):
465 def visit_literal(self, literal):
468 def visit_string(self, string):
471 def visit_const(self, const):
472 self.visit(const.type)
474 def visit_struct(self, struct):
475 for type, name in struct.members:
478 def visit_array(self, array):
479 self.visit(array.type)
481 def visit_blob(self, array):
484 def visit_enum(self, enum):
487 def visit_bitmask(self, bitmask):
488 self.visit(bitmask.type)
490 def visit_pointer(self, pointer):
491 self.visit(pointer.type)
493 def visit_handle(self, handle):
494 self.visit(handle.type)
496 def visit_alias(self, alias):
497 self.visit(alias.type)
499 def visit_opaque(self, opaque):
502 def visit_interface(self, interface):
503 if interface.base is not None:
504 self.visit(interface.base)
505 for method in interface.itermethods():
506 for arg in method.args:
508 self.visit(method.type)
513 def __init__(self, name = None):
520 collector = Collector()
521 for function in self.functions:
522 for arg in function.args:
523 collector.visit(arg.type)
524 collector.visit(function.type)
525 for interface in self.interfaces:
526 collector.visit(interface)
527 for method in interface.itermethods():
528 for arg in method.args:
529 collector.visit(arg.type)
530 collector.visit(method.type)
531 return collector.types
533 def add_function(self, function):
534 self.functions.append(function)
536 def add_functions(self, functions):
537 for function in functions:
538 self.add_function(function)
540 def add_interface(self, interface):
541 self.interfaces.append(interface)
543 def add_interfaces(self, interfaces):
544 self.interfaces.extend(interfaces)
546 def add_api(self, api):
547 self.headers.extend(api.headers)
548 self.add_functions(api.functions)
549 self.add_interfaces(api.interfaces)
551 def get_function_by_name(self, name):
552 for function in self.functions:
553 if function.name == name:
558 Bool = Literal("bool", "Bool")
559 SChar = Literal("signed char", "SInt")
560 UChar = Literal("unsigned char", "UInt")
561 Short = Literal("short", "SInt")
562 Int = Literal("int", "SInt")
563 Long = Literal("long", "SInt")
564 LongLong = Literal("long long", "SInt")
565 UShort = Literal("unsigned short", "UInt")
566 UInt = Literal("unsigned int", "UInt")
567 ULong = Literal("unsigned long", "UInt")
568 ULongLong = Literal("unsigned long long", "UInt")
569 Float = Literal("float", "Float")
570 Double = Literal("double", "Float")
571 SizeT = Literal("size_t", "UInt")
572 WString = Literal("wchar_t *", "WString")
574 Int8 = Literal("int8_t", "SInt")
575 UInt8 = Literal("uint8_t", "UInt")
576 Int16 = Literal("int16_t", "SInt")
577 UInt16 = Literal("uint16_t", "UInt")
578 Int32 = Literal("int32_t", "SInt")
579 UInt32 = Literal("uint32_t", "UInt")
580 Int64 = Literal("int64_t", "SInt")
581 UInt64 = Literal("uint64_t", "UInt")