1 ##########################################################################
3 # Copyright 2008-2009 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 visit(self, type, *args, **kwargs):
38 return type.visit(self, *args, **kwargs)
40 def visit_void(self, void, *args, **kwargs):
41 raise NotImplementedError
43 def visit_literal(self, literal, *args, **kwargs):
44 raise NotImplementedError
46 def visit_string(self, string, *args, **kwargs):
47 raise NotImplementedError
49 def visit_const(self, const, *args, **kwargs):
50 raise NotImplementedError
52 def visit_struct(self, struct, *args, **kwargs):
53 raise NotImplementedError
55 def visit_array(self, array, *args, **kwargs):
56 raise NotImplementedError
58 def visit_blob(self, blob, *args, **kwargs):
59 raise NotImplementedError
61 def visit_enum(self, enum, *args, **kwargs):
62 raise NotImplementedError
64 def visit_bitmask(self, bitmask, *args, **kwargs):
65 raise NotImplementedError
67 def visit_pointer(self, pointer, *args, **kwargs):
68 raise NotImplementedError
70 def visit_alias(self, alias, *args, **kwargs):
71 raise NotImplementedError
73 def visit_opaque(self, opaque, *args, **kwargs):
74 raise NotImplementedError
76 def visit_interface(self, interface, *args, **kwargs):
77 raise NotImplementedError
80 class OnceVisitor(Visitor):
83 self.__visited = set()
85 def visit(self, type, *args, **kwargs):
86 if type not in self.__visited:
87 self.__visited.add(type)
88 return type.visit(self, *args, **kwargs)
92 class Rebuilder(Visitor):
94 def visit_void(self, void):
97 def visit_literal(self, literal):
100 def visit_string(self, string):
103 def visit_const(self, const):
104 return Const(const.type)
106 def visit_struct(self, struct):
107 members = [self.visit(member) for member in struct.members]
108 return Struct(struct.name, members)
110 def visit_array(self, array):
111 type = self.visit(array.type)
112 return Array(type, array.length)
114 def visit_blob(self, blob):
115 type = self.visit(blob.type)
116 return Blob(type, blob.size)
118 def visit_enum(self, enum):
121 def visit_bitmask(self, bitmask):
122 type = self.visit(bitmask.type)
123 return Bitmask(type, bitmask.values)
125 def visit_pointer(self, pointer):
126 type = self.visit(pointer.type)
129 def visit_alias(self, alias):
130 type = self.visit(alias.type)
131 return Alias(alias.expr, type)
133 def visit_opaque(self, opaque):
141 def __init__(self, expr, id = ''):
145 assert char.isalnum() or char in '_ '
147 id = id.replace(' ', '_')
151 id += str(Type.__seq)
153 assert id not in all_types
161 def visit(self, visitor, *args, **kwargs):
162 raise NotImplementedError
169 Type.__init__(self, "void")
171 def visit(self, visitor, *args, **kwargs):
172 return visitor.visit_void(self, *args, **kwargs)
177 class Concrete(Type):
180 print 'static void Dump%s(const %s &value);' % (self.id, self.expr)
183 print 'static void Dump%s(const %s &value) {' % (self.id, self.expr)
188 def _dump(self, instance):
189 raise NotImplementedError
191 def dump(self, instance):
192 print ' Dump%s(%s);' % (self.id, instance)
197 def __init__(self, expr, format, base=10):
198 Type.__init__(self, expr)
201 def visit(self, visitor, *args, **kwargs):
202 return visitor.visit_literal(self, *args, **kwargs)
207 def __init__(self, type):
209 if type.expr.startswith("const "):
210 expr = type.expr + " const"
212 expr = "const " + type.expr
214 Type.__init__(self, expr, 'C' + type.id)
218 def visit(self, visitor, *args, **kwargs):
219 return visitor.visit_const(self, *args, **kwargs)
224 def __init__(self, type):
225 Type.__init__(self, type.expr + " *", 'P' + type.id)
228 def visit(self, visitor, *args, **kwargs):
229 return visitor.visit_pointer(self, *args, **kwargs)
232 def ConstPointer(type):
233 return Pointer(Const(type))
236 class Enum(Concrete):
238 def __init__(self, name, values):
239 Concrete.__init__(self, name)
242 def visit(self, visitor, *args, **kwargs):
243 return visitor.visit_enum(self, *args, **kwargs)
246 def FakeEnum(type, values):
247 return Enum(type.expr, values)
250 class Bitmask(Concrete):
252 def __init__(self, type, values):
253 Concrete.__init__(self, type.expr)
257 def visit(self, visitor, *args, **kwargs):
258 return visitor.visit_bitmask(self, *args, **kwargs)
265 def __init__(self, type, length):
266 Type.__init__(self, type.expr + " *")
270 def visit(self, visitor, *args, **kwargs):
271 return visitor.visit_array(self, *args, **kwargs)
276 def __init__(self, type, size):
277 Type.__init__(self, type.expr + ' *')
281 def visit(self, visitor, *args, **kwargs):
282 return visitor.visit_blob(self, *args, **kwargs)
285 class Struct(Concrete):
287 def __init__(self, name, members):
288 Concrete.__init__(self, name)
290 self.members = members
292 def visit(self, visitor, *args, **kwargs):
293 return visitor.visit_struct(self, *args, **kwargs)
298 def __init__(self, expr, type):
299 Type.__init__(self, expr)
302 def visit(self, visitor, *args, **kwargs):
303 return visitor.visit_alias(self, *args, **kwargs)
307 arg = Arg(type, name, output=True)
313 def __init__(self, type, name, output=False):
319 return '%s %s' % (self.type, self.name)
324 def __init__(self, type, name, args, call = '', fail = None, sideeffects=True, hidden=False):
330 if isinstance(arg, tuple):
331 arg_type, arg_name = arg
332 arg = Arg(arg_type, arg_name)
333 self.args.append(arg)
337 self.sideeffects = sideeffects
340 def prototype(self, name=None):
347 s = self.call + ' ' + s
348 if name.startswith('*'):
350 s = self.type.expr + ' ' + s
353 s += ", ".join(["%s %s" % (arg.type, arg.name) for arg in self.args])
360 def StdFunction(*args, **kwargs):
361 kwargs.setdefault('call', 'GLAPIENTRY')
362 return Function(*args, **kwargs)
365 def FunctionPointer(type, name, args, **kwargs):
370 class Interface(Type):
372 def __init__(self, name, base=None):
373 Type.__init__(self, name)
378 def itermethods(self):
379 if self.base is not None:
380 for method in self.base.itermethods():
382 for method in self.methods:
387 class Method(Function):
389 def __init__(self, type, name, args):
390 Function.__init__(self, type, name, args, call = '__stdcall')
396 def WrapPointer(type):
403 Type.__init__(self, "char *")
405 def visit(self, visitor, *args, **kwargs):
406 return visitor.visit_string(self, *args, **kwargs)
412 '''Opaque pointer.'''
414 def __init__(self, expr):
415 Type.__init__(self, expr)
417 def visit(self, visitor, *args, **kwargs):
418 return visitor.visit_opaque(self, *args, **kwargs)
421 def OpaquePointer(type):
422 return Opaque(type.expr + ' *')
428 def __init__(self, name):
435 def add_type(self, type):
436 if type not in self.types:
439 def add_function(self, function):
440 self.functions.append(function)
441 for arg in function.args:
442 self.add_type(arg.type)
443 self.add_type(function.type)
445 def add_functions(self, functions):
446 for function in functions:
447 self.add_function(function)
449 def add_interface(self, interface):
450 self.interfaces.append(interface)
452 def add_interfaces(self, interfaces):
453 self.interfaces.extend(interfaces)
456 Bool = Literal("bool", "Bool")
457 SChar = Literal("signed char", "SInt")
458 UChar = Literal("unsigned char", "UInt")
459 Short = Literal("short", "SInt")
460 Int = Literal("int", "SInt")
461 Long = Literal("long", "SInt")
462 LongLong = Literal("long long", "SInt")
463 UShort = Literal("unsigned short", "UInt")
464 UInt = Literal("unsigned int", "UInt")
465 ULong = Literal("unsigned long", "UInt")
466 ULongLong = Literal("unsigned long long", "UInt")
467 Float = Literal("float", "Float")
468 Double = Literal("double", "Float")
469 SizeT = Literal("size_t", "UInt")
470 WString = Literal("wchar_t *", "WString")