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):
86 # While "const foo" and "foo const" are synonymous, "const foo *" and
87 # "foo * const" are not quite the same, and some compilers do enforce
88 # strict const correctness.
89 if isinstance(type, String) or type is WString:
90 # For strings we never intend to say a const pointer to chars, but
91 # rather a point to const chars.
92 expr = "const " + type.expr
93 elif type.expr.startswith("const ") or '*' in type.expr:
94 expr = type.expr + " const"
97 expr = "const " + type.expr
99 Type.__init__(self, expr, 'C' + type.id)
103 def visit(self, visitor, *args, **kwargs):
104 return visitor.visit_const(self, *args, **kwargs)
109 def __init__(self, type):
110 Type.__init__(self, type.expr + " *", 'P' + type.id)
113 def visit(self, visitor, *args, **kwargs):
114 return visitor.visit_pointer(self, *args, **kwargs)
119 def __init__(self, name, type, range=None, key=None):
120 Type.__init__(self, type.expr, 'P' + type.id)
126 def visit(self, visitor, *args, **kwargs):
127 return visitor.visit_handle(self, *args, **kwargs)
130 def ConstPointer(type):
131 return Pointer(Const(type))
136 def __init__(self, name, values):
137 Type.__init__(self, name)
138 self.values = list(values)
140 def visit(self, visitor, *args, **kwargs):
141 return visitor.visit_enum(self, *args, **kwargs)
144 def FakeEnum(type, values):
145 return Enum(type.expr, values)
150 def __init__(self, type, values):
151 Type.__init__(self, type.expr)
155 def visit(self, visitor, *args, **kwargs):
156 return visitor.visit_bitmask(self, *args, **kwargs)
163 def __init__(self, type, length):
164 Type.__init__(self, type.expr + " *")
168 def visit(self, visitor, *args, **kwargs):
169 return visitor.visit_array(self, *args, **kwargs)
174 def __init__(self, type, size):
175 Type.__init__(self, type.expr + ' *')
179 def visit(self, visitor, *args, **kwargs):
180 return visitor.visit_blob(self, *args, **kwargs)
185 def __init__(self, name, members):
186 Type.__init__(self, name)
188 self.members = members
190 def visit(self, visitor, *args, **kwargs):
191 return visitor.visit_struct(self, *args, **kwargs)
196 def __init__(self, expr, type):
197 Type.__init__(self, expr)
200 def visit(self, visitor, *args, **kwargs):
201 return visitor.visit_alias(self, *args, **kwargs)
205 arg = Arg(type, name, output=True)
211 def __init__(self, type, name, output=False):
218 return '%s %s' % (self.type, self.name)
223 # 0-3 are reserved to memcpy, malloc, free, and realloc
226 def __init__(self, type, name, args, call = '', fail = None, sideeffects=True):
227 self.id = Function.__id
236 if not isinstance(arg, Arg):
237 if isinstance(arg, tuple):
238 arg_type, arg_name = arg
241 arg_name = "arg%u" % index
242 arg = Arg(arg_type, arg_name)
245 self.args.append(arg)
249 self.sideeffects = sideeffects
251 def prototype(self, name=None):
258 s = self.call + ' ' + s
259 if name.startswith('*'):
261 s = self.type.expr + ' ' + s
264 s += ", ".join(["%s %s" % (arg.type, arg.name) for arg in self.args])
271 def StdFunction(*args, **kwargs):
272 kwargs.setdefault('call', '__stdcall')
273 return Function(*args, **kwargs)
276 def FunctionPointer(type, name, args, **kwargs):
277 # XXX: We should probably treat function pointers (callbacks or not) in a generic fashion
281 class Interface(Type):
283 def __init__(self, name, base=None):
284 Type.__init__(self, name)
289 def visit(self, visitor, *args, **kwargs):
290 return visitor.visit_interface(self, *args, **kwargs)
292 def itermethods(self):
293 if self.base is not None:
294 for method in self.base.itermethods():
296 for method in self.methods:
301 class Method(Function):
303 def __init__(self, type, name, args):
304 Function.__init__(self, type, name, args, call = '__stdcall')
305 for index in range(len(self.args)):
306 self.args[index].index = index + 1
311 def __init__(self, expr = "char *", length = None):
312 Type.__init__(self, expr)
315 def visit(self, visitor, *args, **kwargs):
316 return visitor.visit_string(self, *args, **kwargs)
318 # C string (i.e., zero terminated)
323 '''Opaque pointer.'''
325 def __init__(self, expr):
326 Type.__init__(self, expr)
328 def visit(self, visitor, *args, **kwargs):
329 return visitor.visit_opaque(self, *args, **kwargs)
332 def OpaquePointer(type, *args):
333 return Opaque(type.expr + ' *')
335 def OpaqueArray(type, size):
336 return Opaque(type.expr + ' *')
338 def OpaqueBlob(type, size):
339 return Opaque(type.expr + ' *')
342 class Polymorphic(Type):
344 def __init__(self, default_type, switch_expr, switch_types):
345 Type.__init__(self, default_type.expr)
346 self.default_type = default_type
347 self.switch_expr = switch_expr
348 self.switch_types = switch_types
350 def visit(self, visitor, *args, **kwargs):
351 return visitor.visit_polymorphic(self, *args, **kwargs)
353 def iterswitch(self):
354 cases = [['default']]
355 types = [self.default_type]
357 for expr, type in self.switch_types:
358 case = 'case %s' % expr
360 i = types.index(type)
365 cases[i].append(case)
367 return zip(cases, types)
372 def visit(self, type, *args, **kwargs):
373 return type.visit(self, *args, **kwargs)
375 def visit_void(self, void, *args, **kwargs):
376 raise NotImplementedError
378 def visit_literal(self, literal, *args, **kwargs):
379 raise NotImplementedError
381 def visit_string(self, string, *args, **kwargs):
382 raise NotImplementedError
384 def visit_const(self, const, *args, **kwargs):
385 raise NotImplementedError
387 def visit_struct(self, struct, *args, **kwargs):
388 raise NotImplementedError
390 def visit_array(self, array, *args, **kwargs):
391 raise NotImplementedError
393 def visit_blob(self, blob, *args, **kwargs):
394 raise NotImplementedError
396 def visit_enum(self, enum, *args, **kwargs):
397 raise NotImplementedError
399 def visit_bitmask(self, bitmask, *args, **kwargs):
400 raise NotImplementedError
402 def visit_pointer(self, pointer, *args, **kwargs):
403 raise NotImplementedError
405 def visit_handle(self, handle, *args, **kwargs):
406 raise NotImplementedError
408 def visit_alias(self, alias, *args, **kwargs):
409 raise NotImplementedError
411 def visit_opaque(self, opaque, *args, **kwargs):
412 raise NotImplementedError
414 def visit_interface(self, interface, *args, **kwargs):
415 raise NotImplementedError
417 def visit_polymorphic(self, polymorphic, *args, **kwargs):
418 raise NotImplementedError
419 #return self.visit(polymorphic.default_type, *args, **kwargs)
422 class OnceVisitor(Visitor):
425 self.__visited = set()
427 def visit(self, type, *args, **kwargs):
428 if type not in self.__visited:
429 self.__visited.add(type)
430 return type.visit(self, *args, **kwargs)
434 class Rebuilder(Visitor):
436 def visit_void(self, void):
439 def visit_literal(self, literal):
442 def visit_string(self, string):
445 def visit_const(self, const):
446 return Const(const.type)
448 def visit_struct(self, struct):
449 members = [(self.visit(type), name) for type, name in struct.members]
450 return Struct(struct.name, members)
452 def visit_array(self, array):
453 type = self.visit(array.type)
454 return Array(type, array.length)
456 def visit_blob(self, blob):
457 type = self.visit(blob.type)
458 return Blob(type, blob.size)
460 def visit_enum(self, enum):
463 def visit_bitmask(self, bitmask):
464 type = self.visit(bitmask.type)
465 return Bitmask(type, bitmask.values)
467 def visit_pointer(self, pointer):
468 type = self.visit(pointer.type)
471 def visit_handle(self, handle):
472 type = self.visit(handle.type)
473 return Handle(handle.name, type, range=handle.range, key=handle.key)
475 def visit_alias(self, alias):
476 type = self.visit(alias.type)
477 return Alias(alias.expr, type)
479 def visit_opaque(self, opaque):
482 def visit_polymorphic(self, polymorphic):
483 default_type = self.visit(polymorphic.default_type)
484 switch_expr = polymorphic.switch_expr
485 switch_types = [(expr, self.visit(type)) for expr, type in polymorphic.switch_types]
486 return Polymorphic(default_type, switch_expr, switch_types)
489 class Collector(Visitor):
493 self.__visited = set()
496 def visit(self, type):
497 if type in self.__visited:
499 self.__visited.add(type)
500 Visitor.visit(self, type)
501 self.types.append(type)
503 def visit_void(self, literal):
506 def visit_literal(self, literal):
509 def visit_string(self, string):
512 def visit_const(self, const):
513 self.visit(const.type)
515 def visit_struct(self, struct):
516 for type, name in struct.members:
519 def visit_array(self, array):
520 self.visit(array.type)
522 def visit_blob(self, array):
525 def visit_enum(self, enum):
528 def visit_bitmask(self, bitmask):
529 self.visit(bitmask.type)
531 def visit_pointer(self, pointer):
532 self.visit(pointer.type)
534 def visit_handle(self, handle):
535 self.visit(handle.type)
537 def visit_alias(self, alias):
538 self.visit(alias.type)
540 def visit_opaque(self, opaque):
543 def visit_interface(self, interface):
544 if interface.base is not None:
545 self.visit(interface.base)
546 for method in interface.itermethods():
547 for arg in method.args:
549 self.visit(method.type)
551 def visit_polymorphic(self, polymorphic):
552 self.visit(polymorphic.default_type)
553 for expr, type in polymorphic.switch_types:
559 def __init__(self, name = None):
566 collector = Collector()
567 for function in self.functions:
568 for arg in function.args:
569 collector.visit(arg.type)
570 collector.visit(function.type)
571 for interface in self.interfaces:
572 collector.visit(interface)
573 for method in interface.itermethods():
574 for arg in method.args:
575 collector.visit(arg.type)
576 collector.visit(method.type)
577 return collector.types
579 def add_function(self, function):
580 self.functions.append(function)
582 def add_functions(self, functions):
583 for function in functions:
584 self.add_function(function)
586 def add_interface(self, interface):
587 self.interfaces.append(interface)
589 def add_interfaces(self, interfaces):
590 self.interfaces.extend(interfaces)
592 def add_api(self, api):
593 self.headers.extend(api.headers)
594 self.add_functions(api.functions)
595 self.add_interfaces(api.interfaces)
597 def get_function_by_name(self, name):
598 for function in self.functions:
599 if function.name == name:
604 Bool = Literal("bool", "Bool")
605 SChar = Literal("signed char", "SInt")
606 UChar = Literal("unsigned char", "UInt")
607 Short = Literal("short", "SInt")
608 Int = Literal("int", "SInt")
609 Long = Literal("long", "SInt")
610 LongLong = Literal("long long", "SInt")
611 UShort = Literal("unsigned short", "UInt")
612 UInt = Literal("unsigned int", "UInt")
613 ULong = Literal("unsigned long", "UInt")
614 ULongLong = Literal("unsigned long long", "UInt")
615 Float = Literal("float", "Float")
616 Double = Literal("double", "Double")
617 SizeT = Literal("size_t", "UInt")
618 WString = Literal("wchar_t *", "WString")
620 Int8 = Literal("int8_t", "SInt")
621 UInt8 = Literal("uint8_t", "UInt")
622 Int16 = Literal("int16_t", "SInt")
623 UInt16 = Literal("uint16_t", "UInt")
624 Int32 = Literal("int32_t", "SInt")
625 UInt32 = Literal("uint32_t", "UInt")
626 Int64 = Literal("int64_t", "SInt")
627 UInt64 = Literal("uint64_t", "UInt")