blob: 6b49009c4b505f4bc70ec389828fd3c3f70b4764 [file] [log] [blame]
José Fonseca7ad40262009-09-30 17:17:12 +01001##########################################################################
José Fonseca95442442008-07-08 10:32:53 +09002#
José Fonseca7ad40262009-09-30 17:17:12 +01003# Copyright 2008-2009 VMware, Inc.
4# All Rights Reserved.
José Fonseca95442442008-07-08 10:32:53 +09005#
José Fonseca7ad40262009-09-30 17:17:12 +01006# 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:
José Fonseca95442442008-07-08 10:32:53 +090012#
José Fonseca7ad40262009-09-30 17:17:12 +010013# The above copyright notice and this permission notice shall be included in
14# all copies or substantial portions of the Software.
José Fonseca95442442008-07-08 10:32:53 +090015#
José Fonseca7ad40262009-09-30 17:17:12 +010016# 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
22# THE SOFTWARE.
José Fonseca95442442008-07-08 10:32:53 +090023#
José Fonseca7ad40262009-09-30 17:17:12 +010024##########################################################################/
José Fonseca95442442008-07-08 10:32:53 +090025
José Fonsecad626cf42008-07-07 07:43:16 +090026"""C basic types"""
27
José Fonseca8a56d142008-07-09 12:18:08 +090028
29import debug
30
31
32all_types = {}
33
José Fonseca501f2862010-11-19 20:41:18 +000034
35class Visitor:
36
37 def visit(self, type, *args, **kwargs):
38 return type.visit(self, *args, **kwargs)
39
José Fonsecac356d6a2010-11-23 14:27:25 +000040 def visit_void(self, void, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +000041 raise NotImplementedError
42
José Fonsecac356d6a2010-11-23 14:27:25 +000043 def visit_literal(self, literal, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +000044 raise NotImplementedError
45
José Fonsecac356d6a2010-11-23 14:27:25 +000046 def visit_string(self, string, *args, **kwargs):
José Fonseca2defc982010-11-22 16:59:10 +000047 raise NotImplementedError
48
José Fonsecac356d6a2010-11-23 14:27:25 +000049 def visit_const(self, const, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +000050 raise NotImplementedError
51
José Fonsecac356d6a2010-11-23 14:27:25 +000052 def visit_struct(self, struct, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +000053 raise NotImplementedError
54
José Fonsecac356d6a2010-11-23 14:27:25 +000055 def visit_array(self, array, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +000056 raise NotImplementedError
57
José Fonsecac356d6a2010-11-23 14:27:25 +000058 def visit_blob(self, blob, *args, **kwargs):
José Fonseca885f2652010-11-20 11:22:25 +000059 raise NotImplementedError
60
José Fonsecac356d6a2010-11-23 14:27:25 +000061 def visit_enum(self, enum, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +000062 raise NotImplementedError
63
José Fonsecac356d6a2010-11-23 14:27:25 +000064 def visit_bitmask(self, bitmask, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +000065 raise NotImplementedError
66
José Fonsecac356d6a2010-11-23 14:27:25 +000067 def visit_pointer(self, pointer, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +000068 raise NotImplementedError
69
José Fonseca50d78d82010-11-23 22:13:14 +000070 def visit_handle(self, handle, *args, **kwargs):
71 raise NotImplementedError
72
José Fonsecac356d6a2010-11-23 14:27:25 +000073 def visit_alias(self, alias, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +000074 raise NotImplementedError
75
José Fonsecac356d6a2010-11-23 14:27:25 +000076 def visit_opaque(self, opaque, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +000077 raise NotImplementedError
78
José Fonsecac356d6a2010-11-23 14:27:25 +000079 def visit_interface(self, interface, *args, **kwargs):
80 raise NotImplementedError
81
82
83class OnceVisitor(Visitor):
84
85 def __init__(self):
86 self.__visited = set()
87
88 def visit(self, type, *args, **kwargs):
89 if type not in self.__visited:
90 self.__visited.add(type)
91 return type.visit(self, *args, **kwargs)
92 return None
93
José Fonseca501f2862010-11-19 20:41:18 +000094
José Fonsecac9edb832010-11-20 09:03:10 +000095class Rebuilder(Visitor):
96
97 def visit_void(self, void):
98 return void
99
100 def visit_literal(self, literal):
101 return literal
102
José Fonseca2defc982010-11-22 16:59:10 +0000103 def visit_string(self, string):
104 return string
105
José Fonsecac9edb832010-11-20 09:03:10 +0000106 def visit_const(self, const):
107 return Const(const.type)
108
109 def visit_struct(self, struct):
110 members = [self.visit(member) for member in struct.members]
111 return Struct(struct.name, members)
112
113 def visit_array(self, array):
114 type = self.visit(array.type)
115 return Array(type, array.length)
116
José Fonseca885f2652010-11-20 11:22:25 +0000117 def visit_blob(self, blob):
118 type = self.visit(blob.type)
119 return Blob(type, blob.size)
120
José Fonsecac9edb832010-11-20 09:03:10 +0000121 def visit_enum(self, enum):
122 return enum
123
124 def visit_bitmask(self, bitmask):
125 type = self.visit(bitmask.type)
126 return Bitmask(type, bitmask.values)
127
128 def visit_pointer(self, pointer):
129 type = self.visit(pointer.type)
130 return Pointer(type)
131
José Fonseca50d78d82010-11-23 22:13:14 +0000132 def visit_handle(self, handle):
133 type = self.visit(handle.type)
134 return Handle(handle.name, type)
135
José Fonsecac9edb832010-11-20 09:03:10 +0000136 def visit_alias(self, alias):
137 type = self.visit(alias.type)
138 return Alias(alias.expr, type)
139
140 def visit_opaque(self, opaque):
141 return opaque
142
143
José Fonsecad626cf42008-07-07 07:43:16 +0900144class Type:
145
José Fonsecae753ce82009-07-22 18:13:52 +0100146 __seq = 0
147
148 def __init__(self, expr, id = ''):
149 self.expr = expr
150
151 for char in id:
152 assert char.isalnum() or char in '_ '
153
154 id = id.replace(' ', '_')
155
156 if id in all_types:
157 Type.__seq += 1
158 id += str(Type.__seq)
159
160 assert id not in all_types
161 all_types[id] = self
162
163 self.id = id
José Fonsecad626cf42008-07-07 07:43:16 +0900164
165 def __str__(self):
José Fonsecae753ce82009-07-22 18:13:52 +0100166 return self.expr
José Fonsecae9e1d062009-04-13 13:26:29 +0100167
José Fonseca501f2862010-11-19 20:41:18 +0000168 def visit(self, visitor, *args, **kwargs):
169 raise NotImplementedError
170
José Fonseca27cd25d2008-07-07 13:44:00 +0900171
José Fonsecad626cf42008-07-07 07:43:16 +0900172
José Fonseca8a56d142008-07-09 12:18:08 +0900173class _Void(Type):
José Fonsecad626cf42008-07-07 07:43:16 +0900174
175 def __init__(self):
176 Type.__init__(self, "void")
177
José Fonseca501f2862010-11-19 20:41:18 +0000178 def visit(self, visitor, *args, **kwargs):
179 return visitor.visit_void(self, *args, **kwargs)
180
José Fonseca8a56d142008-07-09 12:18:08 +0900181Void = _Void()
José Fonsecad626cf42008-07-07 07:43:16 +0900182
183
José Fonseca8a56d142008-07-09 12:18:08 +0900184class Concrete(Type):
José Fonsecad626cf42008-07-07 07:43:16 +0900185
José Fonseca8a56d142008-07-09 12:18:08 +0900186 def decl(self):
José Fonseca622af962009-09-27 19:13:58 +0100187 print 'static void Dump%s(const %s &value);' % (self.id, self.expr)
José Fonseca8a56d142008-07-09 12:18:08 +0900188
189 def impl(self):
José Fonseca622af962009-09-27 19:13:58 +0100190 print 'static void Dump%s(const %s &value) {' % (self.id, self.expr)
José Fonseca8a56d142008-07-09 12:18:08 +0900191 self._dump("value");
192 print '}'
193 print
194
195 def _dump(self, instance):
196 raise NotImplementedError
197
198 def dump(self, instance):
José Fonsecae753ce82009-07-22 18:13:52 +0100199 print ' Dump%s(%s);' % (self.id, instance)
José Fonseca8a56d142008-07-09 12:18:08 +0900200
201
José Fonsecadd97c342010-11-23 12:29:46 +0000202class Literal(Type):
José Fonseca8a56d142008-07-09 12:18:08 +0900203
José Fonseca51c1ef82010-11-15 16:09:14 +0000204 def __init__(self, expr, format, base=10):
José Fonsecadd97c342010-11-23 12:29:46 +0000205 Type.__init__(self, expr)
José Fonsecad626cf42008-07-07 07:43:16 +0900206 self.format = format
207
José Fonseca501f2862010-11-19 20:41:18 +0000208 def visit(self, visitor, *args, **kwargs):
209 return visitor.visit_literal(self, *args, **kwargs)
210
José Fonsecad626cf42008-07-07 07:43:16 +0900211
212class Const(Type):
213
214 def __init__(self, type):
José Fonsecae753ce82009-07-22 18:13:52 +0100215
José Fonsecaf6592d72010-11-21 12:44:41 +0000216 if type.expr.startswith("const "):
José Fonsecae753ce82009-07-22 18:13:52 +0100217 expr = type.expr + " const"
218 else:
219 expr = "const " + type.expr
220
221 Type.__init__(self, expr, 'C' + type.id)
222
José Fonsecad626cf42008-07-07 07:43:16 +0900223 self.type = type
224
José Fonseca501f2862010-11-19 20:41:18 +0000225 def visit(self, visitor, *args, **kwargs):
226 return visitor.visit_const(self, *args, **kwargs)
227
José Fonsecad626cf42008-07-07 07:43:16 +0900228
229class Pointer(Type):
230
231 def __init__(self, type):
José Fonsecae753ce82009-07-22 18:13:52 +0100232 Type.__init__(self, type.expr + " *", 'P' + type.id)
José Fonsecad626cf42008-07-07 07:43:16 +0900233 self.type = type
234
José Fonseca501f2862010-11-19 20:41:18 +0000235 def visit(self, visitor, *args, **kwargs):
236 return visitor.visit_pointer(self, *args, **kwargs)
237
José Fonsecad626cf42008-07-07 07:43:16 +0900238
José Fonseca50d78d82010-11-23 22:13:14 +0000239class Handle(Type):
240
241 def __init__(self, name, type):
242 Type.__init__(self, type.expr, 'P' + type.id)
243 self.name = name
244 self.type = type
245
246 def visit(self, visitor, *args, **kwargs):
247 return visitor.visit_handle(self, *args, **kwargs)
248
249
José Fonsecab974caa2008-07-09 08:12:34 +0900250def ConstPointer(type):
251 return Pointer(Const(type))
252
253
José Fonseca8a56d142008-07-09 12:18:08 +0900254class Enum(Concrete):
José Fonsecad626cf42008-07-07 07:43:16 +0900255
256 def __init__(self, name, values):
José Fonseca8a56d142008-07-09 12:18:08 +0900257 Concrete.__init__(self, name)
José Fonsecad626cf42008-07-07 07:43:16 +0900258 self.values = values
José Fonsecaa83fb242008-07-07 16:55:52 +0900259
José Fonseca501f2862010-11-19 20:41:18 +0000260 def visit(self, visitor, *args, **kwargs):
261 return visitor.visit_enum(self, *args, **kwargs)
262
José Fonsecad626cf42008-07-07 07:43:16 +0900263
José Fonseca501f2862010-11-19 20:41:18 +0000264def FakeEnum(type, values):
265 return Enum(type.expr, values)
José Fonseca6edf23c2009-05-04 10:20:52 +0100266
267
José Fonseca501f2862010-11-19 20:41:18 +0000268class Bitmask(Concrete):
José Fonsecad626cf42008-07-07 07:43:16 +0900269
270 def __init__(self, type, values):
José Fonsecae753ce82009-07-22 18:13:52 +0100271 Concrete.__init__(self, type.expr)
José Fonsecaec61f312008-07-09 02:16:43 +0900272 self.type = type
José Fonsecad626cf42008-07-07 07:43:16 +0900273 self.values = values
274
José Fonseca501f2862010-11-19 20:41:18 +0000275 def visit(self, visitor, *args, **kwargs):
276 return visitor.visit_bitmask(self, *args, **kwargs)
277
José Fonseca501f2862010-11-19 20:41:18 +0000278Flags = Bitmask
279
José Fonsecad626cf42008-07-07 07:43:16 +0900280
José Fonsecaccae31c2009-07-22 18:14:12 +0100281class Array(Type):
282
283 def __init__(self, type, length):
José Fonseca885f2652010-11-20 11:22:25 +0000284 Type.__init__(self, type.expr + " *")
José Fonsecaccae31c2009-07-22 18:14:12 +0100285 self.type = type
286 self.length = length
287
José Fonseca501f2862010-11-19 20:41:18 +0000288 def visit(self, visitor, *args, **kwargs):
289 return visitor.visit_array(self, *args, **kwargs)
290
José Fonsecaccae31c2009-07-22 18:14:12 +0100291
José Fonseca885f2652010-11-20 11:22:25 +0000292class Blob(Type):
293
294 def __init__(self, type, size):
295 Type.__init__(self, type.expr + ' *')
296 self.type = type
297 self.size = size
298
299 def visit(self, visitor, *args, **kwargs):
300 return visitor.visit_blob(self, *args, **kwargs)
301
José Fonseca885f2652010-11-20 11:22:25 +0000302
José Fonseca8a56d142008-07-09 12:18:08 +0900303class Struct(Concrete):
José Fonsecad626cf42008-07-07 07:43:16 +0900304
305 def __init__(self, name, members):
José Fonseca8a56d142008-07-09 12:18:08 +0900306 Concrete.__init__(self, name)
José Fonseca51c1ef82010-11-15 16:09:14 +0000307 self.name = name
José Fonsecad626cf42008-07-07 07:43:16 +0900308 self.members = members
309
José Fonseca501f2862010-11-19 20:41:18 +0000310 def visit(self, visitor, *args, **kwargs):
311 return visitor.visit_struct(self, *args, **kwargs)
312
José Fonsecad626cf42008-07-07 07:43:16 +0900313
314class Alias(Type):
315
José Fonsecac9edb832010-11-20 09:03:10 +0000316 def __init__(self, expr, type):
317 Type.__init__(self, expr)
José Fonsecad626cf42008-07-07 07:43:16 +0900318 self.type = type
319
José Fonseca501f2862010-11-19 20:41:18 +0000320 def visit(self, visitor, *args, **kwargs):
321 return visitor.visit_alias(self, *args, **kwargs)
322
José Fonsecad626cf42008-07-07 07:43:16 +0900323
José Fonsecac9096f02010-11-22 13:02:26 +0000324def Out(type, name):
325 arg = Arg(type, name, output=True)
326 return arg
327
328
329class Arg:
330
331 def __init__(self, type, name, output=False):
332 self.type = type
333 self.name = name
334 self.output = output
335
336 def __str__(self):
337 return '%s %s' % (self.type, self.name)
José Fonseca83c9ac82010-01-28 14:45:36 +0000338
339
José Fonsecad626cf42008-07-07 07:43:16 +0900340class Function:
341
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000342 def __init__(self, type, name, args, call = '', fail = None, sideeffects=True, hidden=False):
José Fonsecad626cf42008-07-07 07:43:16 +0900343 self.type = type
344 self.name = name
José Fonsecac9096f02010-11-22 13:02:26 +0000345
346 self.args = []
347 for arg in args:
348 if isinstance(arg, tuple):
349 arg_type, arg_name = arg
350 arg = Arg(arg_type, arg_name)
351 self.args.append(arg)
352
José Fonsecad626cf42008-07-07 07:43:16 +0900353 self.call = call
José Fonseca290c28c2009-04-23 15:20:29 +0100354 self.fail = fail
José Fonsecaee855d92010-11-22 17:14:47 +0000355 self.sideeffects = sideeffects
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000356 self.hidden = False
José Fonsecad626cf42008-07-07 07:43:16 +0900357
358 def prototype(self, name=None):
359 if name is not None:
360 name = name.strip()
361 else:
362 name = self.name
363 s = name
364 if self.call:
365 s = self.call + ' ' + s
366 if name.startswith('*'):
367 s = '(' + s + ')'
José Fonsecae753ce82009-07-22 18:13:52 +0100368 s = self.type.expr + ' ' + s
José Fonsecad626cf42008-07-07 07:43:16 +0900369 s += "("
370 if self.args:
José Fonsecac9096f02010-11-22 13:02:26 +0000371 s += ", ".join(["%s %s" % (arg.type, arg.name) for arg in self.args])
José Fonsecad626cf42008-07-07 07:43:16 +0900372 else:
373 s += "void"
374 s += ")"
375 return s
376
José Fonseca3c2c9292009-05-04 12:16:30 +0100377
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000378def StdFunction(*args, **kwargs):
379 kwargs.setdefault('call', 'GLAPIENTRY')
380 return Function(*args, **kwargs)
José Fonsecac77023d2009-05-04 12:53:50 +0100381
José Fonsecad626cf42008-07-07 07:43:16 +0900382
José Fonseca31524192010-11-22 21:05:26 +0000383def FunctionPointer(type, name, args, **kwargs):
384 # XXX
385 return Opaque(name)
386
387
José Fonsecad626cf42008-07-07 07:43:16 +0900388class Interface(Type):
389
390 def __init__(self, name, base=None):
391 Type.__init__(self, name)
José Fonsecae753ce82009-07-22 18:13:52 +0100392 self.name = name
José Fonsecad626cf42008-07-07 07:43:16 +0900393 self.base = base
394 self.methods = []
395
396 def itermethods(self):
397 if self.base is not None:
398 for method in self.base.itermethods():
399 yield method
400 for method in self.methods:
401 yield method
402 raise StopIteration
403
José Fonsecad626cf42008-07-07 07:43:16 +0900404
405class Method(Function):
406
407 def __init__(self, type, name, args):
José Fonseca51c1ef82010-11-15 16:09:14 +0000408 Function.__init__(self, type, name, args, call = '__stdcall')
José Fonsecad626cf42008-07-07 07:43:16 +0900409
410
411towrap = []
412
José Fonsecad626cf42008-07-07 07:43:16 +0900413
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000414def WrapPointer(type):
415 return Pointer(type)
José Fonseca27cd25d2008-07-07 13:44:00 +0900416
José Fonseca8a56d142008-07-09 12:18:08 +0900417
418class _String(Type):
419
420 def __init__(self):
José Fonsecae753ce82009-07-22 18:13:52 +0100421 Type.__init__(self, "char *")
José Fonseca8a56d142008-07-09 12:18:08 +0900422
José Fonseca501f2862010-11-19 20:41:18 +0000423 def visit(self, visitor, *args, **kwargs):
José Fonseca2defc982010-11-22 16:59:10 +0000424 return visitor.visit_string(self, *args, **kwargs)
José Fonseca501f2862010-11-19 20:41:18 +0000425
José Fonseca8a56d142008-07-09 12:18:08 +0900426String = _String()
427
José Fonseca51c1ef82010-11-15 16:09:14 +0000428
José Fonsecaf6592d72010-11-21 12:44:41 +0000429class Opaque(Type):
430 '''Opaque pointer.'''
José Fonsecae54e4112009-06-25 13:56:18 +0100431
José Fonsecaf6592d72010-11-21 12:44:41 +0000432 def __init__(self, expr):
433 Type.__init__(self, expr)
José Fonsecae54e4112009-06-25 13:56:18 +0100434
José Fonseca501f2862010-11-19 20:41:18 +0000435 def visit(self, visitor, *args, **kwargs):
436 return visitor.visit_opaque(self, *args, **kwargs)
437
José Fonsecaf6592d72010-11-21 12:44:41 +0000438
439def OpaquePointer(type):
440 return Opaque(type.expr + ' *')
José Fonsecae54e4112009-06-25 13:56:18 +0100441
José Fonseca8a56d142008-07-09 12:18:08 +0900442
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000443
444class API:
445
446 def __init__(self, name):
447 self.name = name
448 self.headers = []
449 self.types = set()
450 self.functions = []
451 self.interfaces = []
452
453 def add_type(self, type):
454 if type not in self.types:
455 self.types.add(type)
456
457 def add_function(self, function):
458 self.functions.append(function)
459 for arg in function.args:
460 self.add_type(arg.type)
461 self.add_type(function.type)
462
463 def add_functions(self, functions):
464 for function in functions:
465 self.add_function(function)
466
467 def add_interface(self, interface):
468 self.interfaces.append(interface)
469
470 def add_interfaces(self, interfaces):
471 self.interfaces.extend(interfaces)
472
473
José Fonseca51c1ef82010-11-15 16:09:14 +0000474Bool = Literal("bool", "Bool")
475SChar = Literal("signed char", "SInt")
476UChar = Literal("unsigned char", "UInt")
477Short = Literal("short", "SInt")
478Int = Literal("int", "SInt")
479Long = Literal("long", "SInt")
480LongLong = Literal("long long", "SInt")
481UShort = Literal("unsigned short", "UInt")
482UInt = Literal("unsigned int", "UInt")
483ULong = Literal("unsigned long", "UInt")
José Fonseca28f034f2010-11-22 20:31:25 +0000484ULongLong = Literal("unsigned long long", "UInt")
José Fonseca51c1ef82010-11-15 16:09:14 +0000485Float = Literal("float", "Float")
486Double = Literal("double", "Float")
487SizeT = Literal("size_t", "UInt")
488WString = Literal("wchar_t *", "WString")
José Fonsecad626cf42008-07-07 07:43:16 +0900489