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)
225 def __init__(self, type, name, args, call = '', fail = None, sideeffects=True):
226 self.id = Function.__id
235 if not isinstance(arg, Arg):
236 if isinstance(arg, tuple):
237 arg_type, arg_name = arg
240 arg_name = "arg%u" % index
241 arg = Arg(arg_type, arg_name)
244 self.args.append(arg)
248 self.sideeffects = sideeffects
250 def prototype(self, name=None):
257 s = self.call + ' ' + s
258 if name.startswith('*'):
260 s = self.type.expr + ' ' + s
263 s += ", ".join(["%s %s" % (arg.type, arg.name) for arg in self.args])
270 def StdFunction(*args, **kwargs):
271 kwargs.setdefault('call', '__stdcall')
272 return Function(*args, **kwargs)
275 def FunctionPointer(type, name, args, **kwargs):
276 # XXX: We should probably treat function pointers (callbacks or not) in a generic fashion
280 class Interface(Type):
282 def __init__(self, name, base=None):
283 Type.__init__(self, name)
288 def visit(self, visitor, *args, **kwargs):
289 return visitor.visit_interface(self, *args, **kwargs)
291 def itermethods(self):
292 if self.base is not None:
293 for method in self.base.itermethods():
295 for method in self.methods:
300 class Method(Function):
302 def __init__(self, type, name, args):
303 Function.__init__(self, type, name, args, call = '__stdcall')
304 for index in range(len(self.args)):
305 self.args[index].index = index + 1
310 def __init__(self, expr = "char *", length = None):
311 Type.__init__(self, expr)
314 def visit(self, visitor, *args, **kwargs):
315 return visitor.visit_string(self, *args, **kwargs)
317 # C string (i.e., zero terminated)
322 '''Opaque pointer.'''
324 def __init__(self, expr):
325 Type.__init__(self, expr)
327 def visit(self, visitor, *args, **kwargs):
328 return visitor.visit_opaque(self, *args, **kwargs)
331 def OpaquePointer(type, *args):
332 return Opaque(type.expr + ' *')
334 def OpaqueArray(type, size):
335 return Opaque(type.expr + ' *')
337 def OpaqueBlob(type, size):
338 return Opaque(type.expr + ' *')
341 class Polymorphic(Type):
343 def __init__(self, default_type, switch_expr, switch_types):
344 Type.__init__(self, default_type.expr)
345 self.default_type = default_type
346 self.switch_expr = switch_expr
347 self.switch_types = switch_types
349 def visit(self, visitor, *args, **kwargs):
350 return visitor.visit_polymorphic(self, *args, **kwargs)
352 def iterswitch(self):
353 cases = [['default']]
354 types = [self.default_type]
356 for expr, type in self.switch_types:
357 case = 'case %s' % expr
359 i = types.index(type)
364 cases[i].append(case)
366 return zip(cases, types)
371 def visit(self, type, *args, **kwargs):
372 return type.visit(self, *args, **kwargs)
374 def visit_void(self, void, *args, **kwargs):
375 raise NotImplementedError
377 def visit_literal(self, literal, *args, **kwargs):
378 raise NotImplementedError
380 def visit_string(self, string, *args, **kwargs):
381 raise NotImplementedError
383 def visit_const(self, const, *args, **kwargs):
384 raise NotImplementedError
386 def visit_struct(self, struct, *args, **kwargs):
387 raise NotImplementedError
389 def visit_array(self, array, *args, **kwargs):
390 raise NotImplementedError
392 def visit_blob(self, blob, *args, **kwargs):
393 raise NotImplementedError
395 def visit_enum(self, enum, *args, **kwargs):
396 raise NotImplementedError
398 def visit_bitmask(self, bitmask, *args, **kwargs):
399 raise NotImplementedError
401 def visit_pointer(self, pointer, *args, **kwargs):
402 raise NotImplementedError
404 def visit_handle(self, handle, *args, **kwargs):
405 raise NotImplementedError
407 def visit_alias(self, alias, *args, **kwargs):
408 raise NotImplementedError
410 def visit_opaque(self, opaque, *args, **kwargs):
411 raise NotImplementedError
413 def visit_interface(self, interface, *args, **kwargs):
414 raise NotImplementedError
416 def visit_polymorphic(self, polymorphic, *args, **kwargs):
417 raise NotImplementedError
418 #return self.visit(polymorphic.default_type, *args, **kwargs)
421 class OnceVisitor(Visitor):
424 self.__visited = set()
426 def visit(self, type, *args, **kwargs):
427 if type not in self.__visited:
428 self.__visited.add(type)
429 return type.visit(self, *args, **kwargs)
433 class Rebuilder(Visitor):
435 def visit_void(self, void):
438 def visit_literal(self, literal):
441 def visit_string(self, string):
444 def visit_const(self, const):
445 return Const(const.type)
447 def visit_struct(self, struct):
448 members = [(self.visit(type), name) for type, name in struct.members]
449 return Struct(struct.name, members)
451 def visit_array(self, array):
452 type = self.visit(array.type)
453 return Array(type, array.length)
455 def visit_blob(self, blob):
456 type = self.visit(blob.type)
457 return Blob(type, blob.size)
459 def visit_enum(self, enum):
462 def visit_bitmask(self, bitmask):
463 type = self.visit(bitmask.type)
464 return Bitmask(type, bitmask.values)
466 def visit_pointer(self, pointer):
467 type = self.visit(pointer.type)
470 def visit_handle(self, handle):
471 type = self.visit(handle.type)
472 return Handle(handle.name, type, range=handle.range, key=handle.key)
474 def visit_alias(self, alias):
475 type = self.visit(alias.type)
476 return Alias(alias.expr, type)
478 def visit_opaque(self, opaque):
481 def visit_polymorphic(self, polymorphic):
482 default_type = self.visit(polymorphic.default_type)
483 switch_expr = polymorphic.switch_expr
484 switch_types = [(expr, self.visit(type)) for expr, type in polymorphic.switch_types]
485 return Polymorphic(default_type, switch_expr, switch_types)
488 class Collector(Visitor):
492 self.__visited = set()
495 def visit(self, type):
496 if type in self.__visited:
498 self.__visited.add(type)
499 Visitor.visit(self, type)
500 self.types.append(type)
502 def visit_void(self, literal):
505 def visit_literal(self, literal):
508 def visit_string(self, string):
511 def visit_const(self, const):
512 self.visit(const.type)
514 def visit_struct(self, struct):
515 for type, name in struct.members:
518 def visit_array(self, array):
519 self.visit(array.type)
521 def visit_blob(self, array):
524 def visit_enum(self, enum):
527 def visit_bitmask(self, bitmask):
528 self.visit(bitmask.type)
530 def visit_pointer(self, pointer):
531 self.visit(pointer.type)
533 def visit_handle(self, handle):
534 self.visit(handle.type)
536 def visit_alias(self, alias):
537 self.visit(alias.type)
539 def visit_opaque(self, opaque):
542 def visit_interface(self, interface):
543 if interface.base is not None:
544 self.visit(interface.base)
545 for method in interface.itermethods():
546 for arg in method.args:
548 self.visit(method.type)
550 def visit_polymorphic(self, polymorphic):
551 self.visit(polymorphic.default_type)
552 for expr, type in polymorphic.switch_types:
558 def __init__(self, name = None):
565 collector = Collector()
566 for function in self.functions:
567 for arg in function.args:
568 collector.visit(arg.type)
569 collector.visit(function.type)
570 for interface in self.interfaces:
571 collector.visit(interface)
572 for method in interface.itermethods():
573 for arg in method.args:
574 collector.visit(arg.type)
575 collector.visit(method.type)
576 return collector.types
578 def add_function(self, function):
579 self.functions.append(function)
581 def add_functions(self, functions):
582 for function in functions:
583 self.add_function(function)
585 def add_interface(self, interface):
586 self.interfaces.append(interface)
588 def add_interfaces(self, interfaces):
589 self.interfaces.extend(interfaces)
591 def add_api(self, api):
592 self.headers.extend(api.headers)
593 self.add_functions(api.functions)
594 self.add_interfaces(api.interfaces)
596 def get_function_by_name(self, name):
597 for function in self.functions:
598 if function.name == name:
603 Bool = Literal("bool", "Bool")
604 SChar = Literal("signed char", "SInt")
605 UChar = Literal("unsigned char", "UInt")
606 Short = Literal("short", "SInt")
607 Int = Literal("int", "SInt")
608 Long = Literal("long", "SInt")
609 LongLong = Literal("long long", "SInt")
610 UShort = Literal("unsigned short", "UInt")
611 UInt = Literal("unsigned int", "UInt")
612 ULong = Literal("unsigned long", "UInt")
613 ULongLong = Literal("unsigned long long", "UInt")
614 Float = Literal("float", "Float")
615 Double = Literal("double", "Double")
616 SizeT = Literal("size_t", "UInt")
617 WString = Literal("wchar_t *", "WString")
619 Int8 = Literal("int8_t", "SInt")
620 UInt8 = Literal("uint8_t", "UInt")
621 Int16 = Literal("int16_t", "SInt")
622 UInt16 = Literal("uint16_t", "UInt")
623 Int32 = Literal("int32_t", "SInt")
624 UInt32 = Literal("uint32_t", "UInt")
625 Int64 = Literal("int64_t", "SInt")
626 UInt64 = Literal("uint64_t", "UInt")