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:
299 def itermethods2(self):
300 if self.base is not None:
301 for iface, method in self.base.itermethods2():
303 for method in self.methods:
308 class Method(Function):
310 def __init__(self, type, name, args, const=False):
311 Function.__init__(self, type, name, args, call = '__stdcall')
312 for index in range(len(self.args)):
313 self.args[index].index = index + 1
316 def prototype(self, name=None):
317 s = Function.prototype(self, name)
325 def __init__(self, expr = "char *", length = None):
326 Type.__init__(self, expr)
329 def visit(self, visitor, *args, **kwargs):
330 return visitor.visit_string(self, *args, **kwargs)
332 # C string (i.e., zero terminated)
337 '''Opaque pointer.'''
339 def __init__(self, expr):
340 Type.__init__(self, expr)
342 def visit(self, visitor, *args, **kwargs):
343 return visitor.visit_opaque(self, *args, **kwargs)
346 def OpaquePointer(type, *args):
347 return Opaque(type.expr + ' *')
349 def OpaqueArray(type, size):
350 return Opaque(type.expr + ' *')
352 def OpaqueBlob(type, size):
353 return Opaque(type.expr + ' *')
358 def visit(self, type, *args, **kwargs):
359 return type.visit(self, *args, **kwargs)
361 def visit_void(self, void, *args, **kwargs):
362 raise NotImplementedError
364 def visit_literal(self, literal, *args, **kwargs):
365 raise NotImplementedError
367 def visit_string(self, string, *args, **kwargs):
368 raise NotImplementedError
370 def visit_const(self, const, *args, **kwargs):
371 raise NotImplementedError
373 def visit_struct(self, struct, *args, **kwargs):
374 raise NotImplementedError
376 def visit_array(self, array, *args, **kwargs):
377 raise NotImplementedError
379 def visit_blob(self, blob, *args, **kwargs):
380 raise NotImplementedError
382 def visit_enum(self, enum, *args, **kwargs):
383 raise NotImplementedError
385 def visit_bitmask(self, bitmask, *args, **kwargs):
386 raise NotImplementedError
388 def visit_pointer(self, pointer, *args, **kwargs):
389 raise NotImplementedError
391 def visit_handle(self, handle, *args, **kwargs):
392 raise NotImplementedError
394 def visit_alias(self, alias, *args, **kwargs):
395 raise NotImplementedError
397 def visit_opaque(self, opaque, *args, **kwargs):
398 raise NotImplementedError
400 def visit_interface(self, interface, *args, **kwargs):
401 raise NotImplementedError
404 class OnceVisitor(Visitor):
407 self.__visited = set()
409 def visit(self, type, *args, **kwargs):
410 if type not in self.__visited:
411 self.__visited.add(type)
412 return type.visit(self, *args, **kwargs)
416 class Rebuilder(Visitor):
418 def visit_void(self, void):
421 def visit_literal(self, literal):
424 def visit_string(self, string):
427 def visit_const(self, const):
428 return Const(const.type)
430 def visit_struct(self, struct):
431 members = [(self.visit(type), name) for type, name in struct.members]
432 return Struct(struct.name, members)
434 def visit_array(self, array):
435 type = self.visit(array.type)
436 return Array(type, array.length)
438 def visit_blob(self, blob):
439 type = self.visit(blob.type)
440 return Blob(type, blob.size)
442 def visit_enum(self, enum):
445 def visit_bitmask(self, bitmask):
446 type = self.visit(bitmask.type)
447 return Bitmask(type, bitmask.values)
449 def visit_pointer(self, pointer):
450 type = self.visit(pointer.type)
453 def visit_handle(self, handle):
454 type = self.visit(handle.type)
455 return Handle(handle.name, type, range=handle.range, key=handle.key)
457 def visit_alias(self, alias):
458 type = self.visit(alias.type)
459 return Alias(alias.expr, type)
461 def visit_opaque(self, opaque):
465 class Collector(Visitor):
469 self.__visited = set()
472 def visit(self, type):
473 if type in self.__visited:
475 self.__visited.add(type)
476 Visitor.visit(self, type)
477 self.types.append(type)
479 def visit_void(self, literal):
482 def visit_literal(self, literal):
485 def visit_string(self, string):
488 def visit_const(self, const):
489 self.visit(const.type)
491 def visit_struct(self, struct):
492 for type, name in struct.members:
495 def visit_array(self, array):
496 self.visit(array.type)
498 def visit_blob(self, array):
501 def visit_enum(self, enum):
504 def visit_bitmask(self, bitmask):
505 self.visit(bitmask.type)
507 def visit_pointer(self, pointer):
508 self.visit(pointer.type)
510 def visit_handle(self, handle):
511 self.visit(handle.type)
513 def visit_alias(self, alias):
514 self.visit(alias.type)
516 def visit_opaque(self, opaque):
519 def visit_interface(self, interface):
520 if interface.base is not None:
521 self.visit(interface.base)
522 for method in interface.itermethods():
523 for arg in method.args:
525 self.visit(method.type)
530 def __init__(self, name = None):
537 collector = Collector()
538 for function in self.functions:
539 for arg in function.args:
540 collector.visit(arg.type)
541 collector.visit(function.type)
542 for interface in self.interfaces:
543 collector.visit(interface)
544 for method in interface.itermethods():
545 for arg in method.args:
546 collector.visit(arg.type)
547 collector.visit(method.type)
548 return collector.types
550 def add_function(self, function):
551 self.functions.append(function)
553 def add_functions(self, functions):
554 for function in functions:
555 self.add_function(function)
557 def add_interface(self, interface):
558 self.interfaces.append(interface)
560 def add_interfaces(self, interfaces):
561 self.interfaces.extend(interfaces)
563 def add_api(self, api):
564 self.headers.extend(api.headers)
565 self.add_functions(api.functions)
566 self.add_interfaces(api.interfaces)
568 def get_function_by_name(self, name):
569 for function in self.functions:
570 if function.name == name:
575 Bool = Literal("bool", "Bool")
576 SChar = Literal("signed char", "SInt")
577 UChar = Literal("unsigned char", "UInt")
578 Short = Literal("short", "SInt")
579 Int = Literal("int", "SInt")
580 Long = Literal("long", "SInt")
581 LongLong = Literal("long long", "SInt")
582 UShort = Literal("unsigned short", "UInt")
583 UInt = Literal("unsigned int", "UInt")
584 ULong = Literal("unsigned long", "UInt")
585 ULongLong = Literal("unsigned long long", "UInt")
586 Float = Literal("float", "Float")
587 Double = Literal("double", "Double")
588 SizeT = Literal("size_t", "UInt")
589 WString = Literal("wchar_t *", "WString")
591 Int8 = Literal("int8_t", "SInt")
592 UInt8 = Literal("uint8_t", "UInt")
593 Int16 = Literal("int16_t", "SInt")
594 UInt16 = Literal("uint16_t", "UInt")
595 Int32 = Literal("int32_t", "SInt")
596 UInt32 = Literal("uint32_t", "UInt")
597 Int64 = Literal("int64_t", "SInt")
598 UInt64 = Literal("uint64_t", "UInt")