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 ##########################################################################/
33 """Base class for all types."""
37 def __init__(self, expr, tag = None):
40 # Generate a default tag, used when naming functions that will operate
41 # on this type, so it should preferrably be something representative of
44 tag = ''.join([c for c in expr if c.isalnum() or c in '_'])
47 assert c.isalnum() or c in '_'
49 # Ensure it is unique.
50 if tag in Type.__tags:
52 while tag + str(suffix) in Type.__tags:
56 assert tag not in Type.__tags
62 """Return the C/C++ type expression for this type."""
65 def visit(self, visitor, *args, **kwargs):
66 raise NotImplementedError
71 """Singleton void type."""
74 Type.__init__(self, "void")
76 def visit(self, visitor, *args, **kwargs):
77 return visitor.visit_void(self, *args, **kwargs)
83 """Class to describe literal types.
85 Types which are not defined in terms of other types, such as integers and
88 def __init__(self, expr, kind):
89 Type.__init__(self, expr)
92 def visit(self, visitor, *args, **kwargs):
93 return visitor.visit_literal(self, *args, **kwargs)
98 def __init__(self, type):
99 # While "const foo" and "foo const" are synonymous, "const foo *" and
100 # "foo * const" are not quite the same, and some compilers do enforce
101 # strict const correctness.
102 if isinstance(type, String) or type is WString:
103 # For strings we never intend to say a const pointer to chars, but
104 # rather a point to const chars.
105 expr = "const " + type.expr
106 elif type.expr.startswith("const ") or '*' in type.expr:
107 expr = type.expr + " const"
110 expr = "const " + type.expr
112 Type.__init__(self, expr, 'C' + type.tag)
116 def visit(self, visitor, *args, **kwargs):
117 return visitor.visit_const(self, *args, **kwargs)
122 def __init__(self, type):
123 Type.__init__(self, type.expr + " *", 'P' + type.tag)
126 def visit(self, visitor, *args, **kwargs):
127 return visitor.visit_pointer(self, *args, **kwargs)
132 def __init__(self, name, type, range=None, key=None):
133 Type.__init__(self, type.expr, 'P' + type.tag)
139 def visit(self, visitor, *args, **kwargs):
140 return visitor.visit_handle(self, *args, **kwargs)
143 def ConstPointer(type):
144 return Pointer(Const(type))
151 def __init__(self, name, values):
152 Type.__init__(self, name)
157 self.values = list(values)
159 def visit(self, visitor, *args, **kwargs):
160 return visitor.visit_enum(self, *args, **kwargs)
163 def FakeEnum(type, values):
164 return Enum(type.expr, values)
171 def __init__(self, type, values):
172 Type.__init__(self, type.expr)
174 self.id = Bitmask.__id
180 def visit(self, visitor, *args, **kwargs):
181 return visitor.visit_bitmask(self, *args, **kwargs)
188 def __init__(self, type, length):
189 Type.__init__(self, type.expr + " *")
193 def visit(self, visitor, *args, **kwargs):
194 return visitor.visit_array(self, *args, **kwargs)
199 def __init__(self, type, size):
200 Type.__init__(self, type.expr + ' *')
204 def visit(self, visitor, *args, **kwargs):
205 return visitor.visit_blob(self, *args, **kwargs)
212 def __init__(self, name, members):
213 Type.__init__(self, name)
215 self.id = Struct.__id
219 self.members = members
221 def visit(self, visitor, *args, **kwargs):
222 return visitor.visit_struct(self, *args, **kwargs)
227 def __init__(self, expr, type):
228 Type.__init__(self, expr)
231 def visit(self, visitor, *args, **kwargs):
232 return visitor.visit_alias(self, *args, **kwargs)
236 arg = Arg(type, name, output=True)
242 def __init__(self, type, name, output=False):
249 return '%s %s' % (self.type, self.name)
254 # 0-3 are reserved to memcpy, malloc, free, and realloc
257 def __init__(self, type, name, args, call = '', fail = None, sideeffects=True):
258 self.id = Function.__id
267 if not isinstance(arg, Arg):
268 if isinstance(arg, tuple):
269 arg_type, arg_name = arg
272 arg_name = "arg%u" % index
273 arg = Arg(arg_type, arg_name)
276 self.args.append(arg)
280 self.sideeffects = sideeffects
282 def prototype(self, name=None):
289 s = self.call + ' ' + s
290 if name.startswith('*'):
292 s = self.type.expr + ' ' + s
295 s += ", ".join(["%s %s" % (arg.type, arg.name) for arg in self.args])
302 def StdFunction(*args, **kwargs):
303 kwargs.setdefault('call', '__stdcall')
304 return Function(*args, **kwargs)
307 def FunctionPointer(type, name, args, **kwargs):
308 # XXX: We should probably treat function pointers (callbacks or not) in a generic fashion
312 class Interface(Type):
314 def __init__(self, name, base=None):
315 Type.__init__(self, name)
320 def visit(self, visitor, *args, **kwargs):
321 return visitor.visit_interface(self, *args, **kwargs)
323 def itermethods(self):
324 if self.base is not None:
325 for method in self.base.itermethods():
327 for method in self.methods:
332 class Method(Function):
334 def __init__(self, type, name, args):
335 Function.__init__(self, type, name, args, call = '__stdcall')
336 for index in range(len(self.args)):
337 self.args[index].index = index + 1
342 def __init__(self, expr = "char *", length = None):
343 Type.__init__(self, expr)
346 def visit(self, visitor, *args, **kwargs):
347 return visitor.visit_string(self, *args, **kwargs)
349 # C string (i.e., zero terminated)
354 '''Opaque pointer.'''
356 def __init__(self, expr):
357 Type.__init__(self, expr)
359 def visit(self, visitor, *args, **kwargs):
360 return visitor.visit_opaque(self, *args, **kwargs)
363 def OpaquePointer(type, *args):
364 return Opaque(type.expr + ' *')
366 def OpaqueArray(type, size):
367 return Opaque(type.expr + ' *')
369 def OpaqueBlob(type, size):
370 return Opaque(type.expr + ' *')
373 class Polymorphic(Type):
375 def __init__(self, default_type, switch_expr, switch_types):
376 Type.__init__(self, default_type.expr)
377 self.default_type = default_type
378 self.switch_expr = switch_expr
379 self.switch_types = switch_types
381 def visit(self, visitor, *args, **kwargs):
382 return visitor.visit_polymorphic(self, *args, **kwargs)
384 def iterswitch(self):
385 cases = [['default']]
386 types = [self.default_type]
388 for expr, type in self.switch_types:
389 case = 'case %s' % expr
391 i = types.index(type)
396 cases[i].append(case)
398 return zip(cases, types)
403 def visit(self, type, *args, **kwargs):
404 return type.visit(self, *args, **kwargs)
406 def visit_void(self, void, *args, **kwargs):
407 raise NotImplementedError
409 def visit_literal(self, literal, *args, **kwargs):
410 raise NotImplementedError
412 def visit_string(self, string, *args, **kwargs):
413 raise NotImplementedError
415 def visit_const(self, const, *args, **kwargs):
416 raise NotImplementedError
418 def visit_struct(self, struct, *args, **kwargs):
419 raise NotImplementedError
421 def visit_array(self, array, *args, **kwargs):
422 raise NotImplementedError
424 def visit_blob(self, blob, *args, **kwargs):
425 raise NotImplementedError
427 def visit_enum(self, enum, *args, **kwargs):
428 raise NotImplementedError
430 def visit_bitmask(self, bitmask, *args, **kwargs):
431 raise NotImplementedError
433 def visit_pointer(self, pointer, *args, **kwargs):
434 raise NotImplementedError
436 def visit_handle(self, handle, *args, **kwargs):
437 raise NotImplementedError
439 def visit_alias(self, alias, *args, **kwargs):
440 raise NotImplementedError
442 def visit_opaque(self, opaque, *args, **kwargs):
443 raise NotImplementedError
445 def visit_interface(self, interface, *args, **kwargs):
446 raise NotImplementedError
448 def visit_polymorphic(self, polymorphic, *args, **kwargs):
449 raise NotImplementedError
450 #return self.visit(polymorphic.default_type, *args, **kwargs)
453 class OnceVisitor(Visitor):
456 self.__visited = set()
458 def visit(self, type, *args, **kwargs):
459 if type not in self.__visited:
460 self.__visited.add(type)
461 return type.visit(self, *args, **kwargs)
465 class Rebuilder(Visitor):
467 def visit_void(self, void):
470 def visit_literal(self, literal):
473 def visit_string(self, string):
476 def visit_const(self, const):
477 return Const(const.type)
479 def visit_struct(self, struct):
480 members = [(self.visit(type), name) for type, name in struct.members]
481 return Struct(struct.name, members)
483 def visit_array(self, array):
484 type = self.visit(array.type)
485 return Array(type, array.length)
487 def visit_blob(self, blob):
488 type = self.visit(blob.type)
489 return Blob(type, blob.size)
491 def visit_enum(self, enum):
494 def visit_bitmask(self, bitmask):
495 type = self.visit(bitmask.type)
496 return Bitmask(type, bitmask.values)
498 def visit_pointer(self, pointer):
499 type = self.visit(pointer.type)
502 def visit_handle(self, handle):
503 type = self.visit(handle.type)
504 return Handle(handle.name, type, range=handle.range, key=handle.key)
506 def visit_alias(self, alias):
507 type = self.visit(alias.type)
508 return Alias(alias.expr, type)
510 def visit_opaque(self, opaque):
513 def visit_polymorphic(self, polymorphic):
514 default_type = self.visit(polymorphic.default_type)
515 switch_expr = polymorphic.switch_expr
516 switch_types = [(expr, self.visit(type)) for expr, type in polymorphic.switch_types]
517 return Polymorphic(default_type, switch_expr, switch_types)
520 class Collector(Visitor):
524 self.__visited = set()
527 def visit(self, type):
528 if type in self.__visited:
530 self.__visited.add(type)
531 Visitor.visit(self, type)
532 self.types.append(type)
534 def visit_void(self, literal):
537 def visit_literal(self, literal):
540 def visit_string(self, string):
543 def visit_const(self, const):
544 self.visit(const.type)
546 def visit_struct(self, struct):
547 for type, name in struct.members:
550 def visit_array(self, array):
551 self.visit(array.type)
553 def visit_blob(self, array):
556 def visit_enum(self, enum):
559 def visit_bitmask(self, bitmask):
560 self.visit(bitmask.type)
562 def visit_pointer(self, pointer):
563 self.visit(pointer.type)
565 def visit_handle(self, handle):
566 self.visit(handle.type)
568 def visit_alias(self, alias):
569 self.visit(alias.type)
571 def visit_opaque(self, opaque):
574 def visit_interface(self, interface):
575 if interface.base is not None:
576 self.visit(interface.base)
577 for method in interface.itermethods():
578 for arg in method.args:
580 self.visit(method.type)
582 def visit_polymorphic(self, polymorphic):
583 self.visit(polymorphic.default_type)
584 for expr, type in polymorphic.switch_types:
590 def __init__(self, name = None):
597 collector = Collector()
598 for function in self.functions:
599 for arg in function.args:
600 collector.visit(arg.type)
601 collector.visit(function.type)
602 for interface in self.interfaces:
603 collector.visit(interface)
604 for method in interface.itermethods():
605 for arg in method.args:
606 collector.visit(arg.type)
607 collector.visit(method.type)
608 return collector.types
610 def add_function(self, function):
611 self.functions.append(function)
613 def add_functions(self, functions):
614 for function in functions:
615 self.add_function(function)
617 def add_interface(self, interface):
618 self.interfaces.append(interface)
620 def add_interfaces(self, interfaces):
621 self.interfaces.extend(interfaces)
623 def add_api(self, api):
624 self.headers.extend(api.headers)
625 self.add_functions(api.functions)
626 self.add_interfaces(api.interfaces)
628 def get_function_by_name(self, name):
629 for function in self.functions:
630 if function.name == name:
635 Bool = Literal("bool", "Bool")
636 SChar = Literal("signed char", "SInt")
637 UChar = Literal("unsigned char", "UInt")
638 Short = Literal("short", "SInt")
639 Int = Literal("int", "SInt")
640 Long = Literal("long", "SInt")
641 LongLong = Literal("long long", "SInt")
642 UShort = Literal("unsigned short", "UInt")
643 UInt = Literal("unsigned int", "UInt")
644 ULong = Literal("unsigned long", "UInt")
645 ULongLong = Literal("unsigned long long", "UInt")
646 Float = Literal("float", "Float")
647 Double = Literal("double", "Double")
648 SizeT = Literal("size_t", "UInt")
649 WString = Literal("wchar_t *", "WString")
651 Int8 = Literal("int8_t", "SInt")
652 UInt8 = Literal("uint8_t", "UInt")
653 Int16 = Literal("int16_t", "SInt")
654 UInt16 = Literal("uint16_t", "UInt")
655 Int32 = Literal("int32_t", "SInt")
656 UInt32 = Literal("uint32_t", "UInt")
657 Int64 = Literal("int64_t", "SInt")
658 UInt64 = Literal("uint64_t", "UInt")