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):
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
243 def prototype(self, name=None):
250 s = self.call + ' ' + s
251 if name.startswith('*'):
253 s = self.type.expr + ' ' + s
256 s += ", ".join(["%s %s" % (arg.type, arg.name) for arg in self.args])
263 def StdFunction(*args, **kwargs):
264 kwargs.setdefault('call', '__stdcall')
265 return Function(*args, **kwargs)
268 def FunctionPointer(type, name, args, **kwargs):
269 # XXX: We should probably treat function pointers (callbacks or not) in a generic fashion
273 class Interface(Type):
275 def __init__(self, name, base=None):
276 Type.__init__(self, name)
281 def visit(self, visitor, *args, **kwargs):
282 return visitor.visit_interface(self, *args, **kwargs)
284 def itermethods(self):
285 if self.base is not None:
286 for method in self.base.itermethods():
288 for method in self.methods:
293 class Method(Function):
295 def __init__(self, type, name, args):
296 Function.__init__(self, type, name, args, call = '__stdcall')
297 for index in range(len(self.args)):
298 self.args[index].index = index + 1
301 def WrapPointer(type):
307 def __init__(self, expr = "char *", length = None):
308 Type.__init__(self, expr)
311 def visit(self, visitor, *args, **kwargs):
312 return visitor.visit_string(self, *args, **kwargs)
314 # C string (i.e., zero terminated)
319 '''Opaque pointer.'''
321 def __init__(self, expr):
322 Type.__init__(self, expr)
324 def visit(self, visitor, *args, **kwargs):
325 return visitor.visit_opaque(self, *args, **kwargs)
328 def OpaquePointer(type, *args):
329 return Opaque(type.expr + ' *')
331 def OpaqueArray(type, size):
332 return Opaque(type.expr + ' *')
334 def OpaqueBlob(type, size):
335 return Opaque(type.expr + ' *')
340 def visit(self, type, *args, **kwargs):
341 return type.visit(self, *args, **kwargs)
343 def visit_void(self, void, *args, **kwargs):
344 raise NotImplementedError
346 def visit_literal(self, literal, *args, **kwargs):
347 raise NotImplementedError
349 def visit_string(self, string, *args, **kwargs):
350 raise NotImplementedError
352 def visit_const(self, const, *args, **kwargs):
353 raise NotImplementedError
355 def visit_struct(self, struct, *args, **kwargs):
356 raise NotImplementedError
358 def visit_array(self, array, *args, **kwargs):
359 raise NotImplementedError
361 def visit_blob(self, blob, *args, **kwargs):
362 raise NotImplementedError
364 def visit_enum(self, enum, *args, **kwargs):
365 raise NotImplementedError
367 def visit_bitmask(self, bitmask, *args, **kwargs):
368 raise NotImplementedError
370 def visit_pointer(self, pointer, *args, **kwargs):
371 raise NotImplementedError
373 def visit_handle(self, handle, *args, **kwargs):
374 raise NotImplementedError
376 def visit_alias(self, alias, *args, **kwargs):
377 raise NotImplementedError
379 def visit_opaque(self, opaque, *args, **kwargs):
380 raise NotImplementedError
382 def visit_interface(self, interface, *args, **kwargs):
383 raise NotImplementedError
386 class OnceVisitor(Visitor):
389 self.__visited = set()
391 def visit(self, type, *args, **kwargs):
392 if type not in self.__visited:
393 self.__visited.add(type)
394 return type.visit(self, *args, **kwargs)
398 class Rebuilder(Visitor):
400 def visit_void(self, void):
403 def visit_literal(self, literal):
406 def visit_string(self, string):
409 def visit_const(self, const):
410 return Const(const.type)
412 def visit_struct(self, struct):
413 members = [(self.visit(type), name) for type, name in struct.members]
414 return Struct(struct.name, members)
416 def visit_array(self, array):
417 type = self.visit(array.type)
418 return Array(type, array.length)
420 def visit_blob(self, blob):
421 type = self.visit(blob.type)
422 return Blob(type, blob.size)
424 def visit_enum(self, enum):
427 def visit_bitmask(self, bitmask):
428 type = self.visit(bitmask.type)
429 return Bitmask(type, bitmask.values)
431 def visit_pointer(self, pointer):
432 type = self.visit(pointer.type)
435 def visit_handle(self, handle):
436 type = self.visit(handle.type)
437 return Handle(handle.name, type, range=handle.range, key=handle.key)
439 def visit_alias(self, alias):
440 type = self.visit(alias.type)
441 return Alias(alias.expr, type)
443 def visit_opaque(self, opaque):
447 class Collector(Visitor):
451 self.__visited = set()
454 def visit(self, type):
455 if type in self.__visited:
457 self.__visited.add(type)
458 Visitor.visit(self, type)
459 self.types.append(type)
461 def visit_void(self, literal):
464 def visit_literal(self, literal):
467 def visit_string(self, string):
470 def visit_const(self, const):
471 self.visit(const.type)
473 def visit_struct(self, struct):
474 for type, name in struct.members:
477 def visit_array(self, array):
478 self.visit(array.type)
480 def visit_blob(self, array):
483 def visit_enum(self, enum):
486 def visit_bitmask(self, bitmask):
487 self.visit(bitmask.type)
489 def visit_pointer(self, pointer):
490 self.visit(pointer.type)
492 def visit_handle(self, handle):
493 self.visit(handle.type)
495 def visit_alias(self, alias):
496 self.visit(alias.type)
498 def visit_opaque(self, opaque):
501 def visit_interface(self, interface):
502 if interface.base is not None:
503 self.visit(interface.base)
504 for method in interface.itermethods():
505 for arg in method.args:
507 self.visit(method.type)
512 def __init__(self, name = None):
519 collector = Collector()
520 for function in self.functions:
521 for arg in function.args:
522 collector.visit(arg.type)
523 collector.visit(function.type)
524 for interface in self.interfaces:
525 collector.visit(interface)
526 for method in interface.itermethods():
527 for arg in method.args:
528 collector.visit(arg.type)
529 collector.visit(method.type)
530 return collector.types
532 def add_function(self, function):
533 self.functions.append(function)
535 def add_functions(self, functions):
536 for function in functions:
537 self.add_function(function)
539 def add_interface(self, interface):
540 self.interfaces.append(interface)
542 def add_interfaces(self, interfaces):
543 self.interfaces.extend(interfaces)
545 def add_api(self, api):
546 self.headers.extend(api.headers)
547 self.add_functions(api.functions)
548 self.add_interfaces(api.interfaces)
550 def get_function_by_name(self, name):
551 for function in self.functions:
552 if function.name == name:
557 Bool = Literal("bool", "Bool")
558 SChar = Literal("signed char", "SInt")
559 UChar = Literal("unsigned char", "UInt")
560 Short = Literal("short", "SInt")
561 Int = Literal("int", "SInt")
562 Long = Literal("long", "SInt")
563 LongLong = Literal("long long", "SInt")
564 UShort = Literal("unsigned short", "UInt")
565 UInt = Literal("unsigned int", "UInt")
566 ULong = Literal("unsigned long", "UInt")
567 ULongLong = Literal("unsigned long long", "UInt")
568 Float = Literal("float", "Float")
569 Double = Literal("double", "Float")
570 SizeT = Literal("size_t", "UInt")
571 WString = Literal("wchar_t *", "WString")
573 Int8 = Literal("int8_t", "SInt")
574 UInt8 = Literal("uint8_t", "UInt")
575 Int16 = Literal("int16_t", "SInt")
576 UInt16 = Literal("uint16_t", "UInt")
577 Int32 = Literal("int32_t", "SInt")
578 UInt32 = Literal("uint32_t", "UInt")
579 Int64 = Literal("int64_t", "SInt")
580 UInt64 = Literal("uint64_t", "UInt")