blob: 1f8b81e72bd8d632711fbeeb5dba5eee210c1610 [file] [log] [blame]
José Fonseca7ad40262009-09-30 17:17:12 +01001##########################################################################
José Fonseca95442442008-07-08 10:32:53 +09002#
José Fonseca6fac5ae2010-11-29 16:09:13 +00003# Copyright 2008-2010 VMware, Inc.
José Fonseca7ad40262009-09-30 17:17:12 +01004# 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
José Fonseca6fac5ae2010-11-29 16:09:13 +000032class Type:
33
34 __all = {}
35 __seq = 0
36
37 def __init__(self, expr, id = ''):
38 self.expr = expr
39
40 for char in id:
41 assert char.isalnum() or char in '_ '
42
43 id = id.replace(' ', '_')
44
45 if id in Type.__all:
46 Type.__seq += 1
47 id += str(Type.__seq)
48
49 assert id not in Type.__all
50 Type.__all[id] = self
51
52 self.id = id
53
54 def __str__(self):
55 return self.expr
56
57 def visit(self, visitor, *args, **kwargs):
58 raise NotImplementedError
59
60
61
62class _Void(Type):
63
64 def __init__(self):
65 Type.__init__(self, "void")
66
67 def visit(self, visitor, *args, **kwargs):
68 return visitor.visit_void(self, *args, **kwargs)
69
70Void = _Void()
71
72
73class Literal(Type):
74
75 def __init__(self, expr, format, base=10):
76 Type.__init__(self, expr)
77 self.format = format
78
79 def visit(self, visitor, *args, **kwargs):
80 return visitor.visit_literal(self, *args, **kwargs)
81
82
83class Const(Type):
84
85 def __init__(self, type):
86
87 if type.expr.startswith("const "):
88 expr = type.expr + " const"
89 else:
90 expr = "const " + type.expr
91
92 Type.__init__(self, expr, 'C' + type.id)
93
94 self.type = type
95
96 def visit(self, visitor, *args, **kwargs):
97 return visitor.visit_const(self, *args, **kwargs)
98
99
100class Pointer(Type):
101
102 def __init__(self, type):
103 Type.__init__(self, type.expr + " *", 'P' + type.id)
104 self.type = type
105
106 def visit(self, visitor, *args, **kwargs):
107 return visitor.visit_pointer(self, *args, **kwargs)
108
109
110class Handle(Type):
111
José Fonseca8a844ae2010-12-06 18:50:52 +0000112 def __init__(self, name, type, range=None, key=None):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000113 Type.__init__(self, type.expr, 'P' + type.id)
114 self.name = name
115 self.type = type
116 self.range = range
José Fonseca8a844ae2010-12-06 18:50:52 +0000117 self.key = key
José Fonseca6fac5ae2010-11-29 16:09:13 +0000118
119 def visit(self, visitor, *args, **kwargs):
120 return visitor.visit_handle(self, *args, **kwargs)
121
122
123def ConstPointer(type):
124 return Pointer(Const(type))
125
126
127class Enum(Type):
128
129 __vid = 0
130
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)
136
137 def visit(self, visitor, *args, **kwargs):
138 return visitor.visit_enum(self, *args, **kwargs)
139
140
141def FakeEnum(type, values):
142 return Enum(type.expr, values)
143
144
145class Bitmask(Type):
146
147 def __init__(self, type, values):
148 Type.__init__(self, type.expr)
149 self.type = type
150 self.values = values
151
152 def visit(self, visitor, *args, **kwargs):
153 return visitor.visit_bitmask(self, *args, **kwargs)
154
155Flags = Bitmask
156
157
158class Array(Type):
159
160 def __init__(self, type, length):
161 Type.__init__(self, type.expr + " *")
162 self.type = type
163 self.length = length
164
165 def visit(self, visitor, *args, **kwargs):
166 return visitor.visit_array(self, *args, **kwargs)
167
168
169class Blob(Type):
170
171 def __init__(self, type, size):
172 Type.__init__(self, type.expr + ' *')
173 self.type = type
174 self.size = size
175
176 def visit(self, visitor, *args, **kwargs):
177 return visitor.visit_blob(self, *args, **kwargs)
178
179
180class Struct(Type):
181
182 def __init__(self, name, members):
183 Type.__init__(self, name)
184 self.name = name
185 self.members = members
186
187 def visit(self, visitor, *args, **kwargs):
188 return visitor.visit_struct(self, *args, **kwargs)
189
190
191class Alias(Type):
192
193 def __init__(self, expr, type):
194 Type.__init__(self, expr)
195 self.type = type
196
197 def visit(self, visitor, *args, **kwargs):
198 return visitor.visit_alias(self, *args, **kwargs)
199
200
201def Out(type, name):
202 arg = Arg(type, name, output=True)
203 return arg
204
205
206class Arg:
207
208 def __init__(self, type, name, output=False):
209 self.type = type
210 self.name = name
211 self.output = output
212 self.index = None
213
214 def __str__(self):
215 return '%s %s' % (self.type, self.name)
216
217
218class Function:
219
220 __id = 0
221
222 def __init__(self, type, name, args, call = '', fail = None, sideeffects=True, hidden=False):
223 self.id = Function.__id
224 Function.__id += 1
225
226 self.type = type
227 self.name = name
228
229 self.args = []
230 index = 0
231 for arg in args:
232 if isinstance(arg, tuple):
233 arg_type, arg_name = arg
234 arg = Arg(arg_type, arg_name)
235 arg.index = index
236 index += 1
237 self.args.append(arg)
238
239 self.call = call
240 self.fail = fail
241 self.sideeffects = sideeffects
242 self.hidden = False
243
244 def prototype(self, name=None):
245 if name is not None:
246 name = name.strip()
247 else:
248 name = self.name
249 s = name
250 if self.call:
251 s = self.call + ' ' + s
252 if name.startswith('*'):
253 s = '(' + s + ')'
254 s = self.type.expr + ' ' + s
255 s += "("
256 if self.args:
257 s += ", ".join(["%s %s" % (arg.type, arg.name) for arg in self.args])
258 else:
259 s += "void"
260 s += ")"
261 return s
262
263
264def StdFunction(*args, **kwargs):
265 kwargs.setdefault('call', '__stdcall')
266 return Function(*args, **kwargs)
267
268
269def FunctionPointer(type, name, args, **kwargs):
270 # XXX: We should probably treat function pointers (callbacks or not) in a generic fashion
271 return Opaque(name)
272
273
274class Interface(Type):
275
276 def __init__(self, name, base=None):
277 Type.__init__(self, name)
278 self.name = name
279 self.base = base
280 self.methods = []
281
282 def visit(self, visitor, *args, **kwargs):
283 return visitor.visit_interface(self, *args, **kwargs)
284
285 def itermethods(self):
286 if self.base is not None:
287 for method in self.base.itermethods():
288 yield method
289 for method in self.methods:
290 yield method
291 raise StopIteration
292
293
294class Method(Function):
295
296 def __init__(self, type, name, args):
297 Function.__init__(self, type, name, args, call = '__stdcall')
298 for index in range(len(self.args)):
299 self.args[index].index = index + 1
300
301
302def WrapPointer(type):
303 return Pointer(type)
304
305
306class String(Type):
307
308 def __init__(self, expr = "char *", length = None):
309 Type.__init__(self, expr)
310 self.length = length
311
312 def visit(self, visitor, *args, **kwargs):
313 return visitor.visit_string(self, *args, **kwargs)
314
315# C string (i.e., zero terminated)
316CString = String()
317
318
319class Opaque(Type):
320 '''Opaque pointer.'''
321
322 def __init__(self, expr):
323 Type.__init__(self, expr)
324
325 def visit(self, visitor, *args, **kwargs):
326 return visitor.visit_opaque(self, *args, **kwargs)
327
328
329def OpaquePointer(type, *args):
330 return Opaque(type.expr + ' *')
331
332def OpaqueArray(type, size):
333 return Opaque(type.expr + ' *')
334
335def OpaqueBlob(type, size):
336 return Opaque(type.expr + ' *')
José Fonseca8a56d142008-07-09 12:18:08 +0900337
José Fonseca501f2862010-11-19 20:41:18 +0000338
339class Visitor:
340
341 def visit(self, type, *args, **kwargs):
342 return type.visit(self, *args, **kwargs)
343
José Fonsecac356d6a2010-11-23 14:27:25 +0000344 def visit_void(self, void, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000345 raise NotImplementedError
346
José Fonsecac356d6a2010-11-23 14:27:25 +0000347 def visit_literal(self, literal, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000348 raise NotImplementedError
349
José Fonsecac356d6a2010-11-23 14:27:25 +0000350 def visit_string(self, string, *args, **kwargs):
José Fonseca2defc982010-11-22 16:59:10 +0000351 raise NotImplementedError
352
José Fonsecac356d6a2010-11-23 14:27:25 +0000353 def visit_const(self, const, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000354 raise NotImplementedError
355
José Fonsecac356d6a2010-11-23 14:27:25 +0000356 def visit_struct(self, struct, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000357 raise NotImplementedError
358
José Fonsecac356d6a2010-11-23 14:27:25 +0000359 def visit_array(self, array, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000360 raise NotImplementedError
361
José Fonsecac356d6a2010-11-23 14:27:25 +0000362 def visit_blob(self, blob, *args, **kwargs):
José Fonseca885f2652010-11-20 11:22:25 +0000363 raise NotImplementedError
364
José Fonsecac356d6a2010-11-23 14:27:25 +0000365 def visit_enum(self, enum, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000366 raise NotImplementedError
367
José Fonsecac356d6a2010-11-23 14:27:25 +0000368 def visit_bitmask(self, bitmask, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000369 raise NotImplementedError
370
José Fonsecac356d6a2010-11-23 14:27:25 +0000371 def visit_pointer(self, pointer, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000372 raise NotImplementedError
373
José Fonseca50d78d82010-11-23 22:13:14 +0000374 def visit_handle(self, handle, *args, **kwargs):
375 raise NotImplementedError
376
José Fonsecac356d6a2010-11-23 14:27:25 +0000377 def visit_alias(self, alias, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000378 raise NotImplementedError
379
José Fonsecac356d6a2010-11-23 14:27:25 +0000380 def visit_opaque(self, opaque, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000381 raise NotImplementedError
382
José Fonsecac356d6a2010-11-23 14:27:25 +0000383 def visit_interface(self, interface, *args, **kwargs):
384 raise NotImplementedError
385
386
387class OnceVisitor(Visitor):
388
389 def __init__(self):
390 self.__visited = set()
391
392 def visit(self, type, *args, **kwargs):
393 if type not in self.__visited:
394 self.__visited.add(type)
395 return type.visit(self, *args, **kwargs)
396 return None
397
José Fonseca501f2862010-11-19 20:41:18 +0000398
José Fonsecac9edb832010-11-20 09:03:10 +0000399class Rebuilder(Visitor):
400
401 def visit_void(self, void):
402 return void
403
404 def visit_literal(self, literal):
405 return literal
406
José Fonseca2defc982010-11-22 16:59:10 +0000407 def visit_string(self, string):
408 return string
409
José Fonsecac9edb832010-11-20 09:03:10 +0000410 def visit_const(self, const):
411 return Const(const.type)
412
413 def visit_struct(self, struct):
414 members = [self.visit(member) for member in struct.members]
415 return Struct(struct.name, members)
416
417 def visit_array(self, array):
418 type = self.visit(array.type)
419 return Array(type, array.length)
420
José Fonseca885f2652010-11-20 11:22:25 +0000421 def visit_blob(self, blob):
422 type = self.visit(blob.type)
423 return Blob(type, blob.size)
424
José Fonsecac9edb832010-11-20 09:03:10 +0000425 def visit_enum(self, enum):
426 return enum
427
428 def visit_bitmask(self, bitmask):
429 type = self.visit(bitmask.type)
430 return Bitmask(type, bitmask.values)
431
432 def visit_pointer(self, pointer):
433 type = self.visit(pointer.type)
434 return Pointer(type)
435
José Fonseca50d78d82010-11-23 22:13:14 +0000436 def visit_handle(self, handle):
437 type = self.visit(handle.type)
José Fonseca8a844ae2010-12-06 18:50:52 +0000438 return Handle(handle.name, type, range=handle.range, key=handle.key)
José Fonseca50d78d82010-11-23 22:13:14 +0000439
José Fonsecac9edb832010-11-20 09:03:10 +0000440 def visit_alias(self, alias):
441 type = self.visit(alias.type)
442 return Alias(alias.expr, type)
443
444 def visit_opaque(self, opaque):
445 return opaque
446
447
José Fonsecae6a50bd2010-11-24 10:12:22 +0000448class Collector(Visitor):
449 '''Collect.'''
450
451 def __init__(self):
452 self.__visited = set()
453 self.types = []
454
455 def visit(self, type):
456 if type in self.__visited:
457 return
458 self.__visited.add(type)
459 Visitor.visit(self, type)
460 self.types.append(type)
461
462 def visit_void(self, literal):
463 pass
464
465 def visit_literal(self, literal):
466 pass
467
468 def visit_string(self, string):
469 pass
470
471 def visit_const(self, const):
472 self.visit(const.type)
473
474 def visit_struct(self, struct):
475 for type, name in struct.members:
476 self.visit(type)
477
478 def visit_array(self, array):
479 self.visit(array.type)
480
481 def visit_blob(self, array):
482 pass
483
484 def visit_enum(self, enum):
485 pass
486
487 def visit_bitmask(self, bitmask):
488 self.visit(bitmask.type)
489
490 def visit_pointer(self, pointer):
491 self.visit(pointer.type)
492
493 def visit_handle(self, handle):
494 self.visit(handle.type)
495
496 def visit_alias(self, alias):
497 self.visit(alias.type)
498
499 def visit_opaque(self, opaque):
500 pass
501
502 def visit_interface(self, interface):
José Fonseca87d1cc62010-11-29 15:57:25 +0000503 if interface.base is not None:
504 self.visit(interface.base)
505 for method in interface.itermethods():
506 for arg in method.args:
507 self.visit(arg.type)
508 self.visit(method.type)
José Fonsecae6a50bd2010-11-24 10:12:22 +0000509
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000510
511class API:
512
José Fonseca68ec4122011-02-20 11:25:25 +0000513 def __init__(self, name = None):
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000514 self.name = name
515 self.headers = []
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000516 self.functions = []
517 self.interfaces = []
518
José Fonsecae6a50bd2010-11-24 10:12:22 +0000519 def all_types(self):
520 collector = Collector()
521 for function in self.functions:
522 for arg in function.args:
523 collector.visit(arg.type)
524 collector.visit(function.type)
525 for interface in self.interfaces:
526 collector.visit(interface)
José Fonseca87d1cc62010-11-29 15:57:25 +0000527 for method in interface.itermethods():
José Fonsecae6a50bd2010-11-24 10:12:22 +0000528 for arg in method.args:
529 collector.visit(arg.type)
530 collector.visit(method.type)
531 return collector.types
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000532
533 def add_function(self, function):
534 self.functions.append(function)
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000535
536 def add_functions(self, functions):
537 for function in functions:
538 self.add_function(function)
539
540 def add_interface(self, interface):
541 self.interfaces.append(interface)
542
543 def add_interfaces(self, interfaces):
544 self.interfaces.extend(interfaces)
545
José Fonseca68ec4122011-02-20 11:25:25 +0000546 def add_api(self, api):
547 self.headers.extend(api.headers)
548 self.add_functions(api.functions)
549 self.add_interfaces(api.interfaces)
550
José Fonsecaeccec3e2011-02-20 09:01:25 +0000551 def get_function_by_name(self, name):
552 for function in self.functions:
553 if function.name == name:
554 return function
555 return None
556
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000557
José Fonseca51c1ef82010-11-15 16:09:14 +0000558Bool = Literal("bool", "Bool")
559SChar = Literal("signed char", "SInt")
560UChar = Literal("unsigned char", "UInt")
561Short = Literal("short", "SInt")
562Int = Literal("int", "SInt")
563Long = Literal("long", "SInt")
564LongLong = Literal("long long", "SInt")
565UShort = Literal("unsigned short", "UInt")
566UInt = Literal("unsigned int", "UInt")
567ULong = Literal("unsigned long", "UInt")
José Fonseca28f034f2010-11-22 20:31:25 +0000568ULongLong = Literal("unsigned long long", "UInt")
José Fonseca51c1ef82010-11-15 16:09:14 +0000569Float = Literal("float", "Float")
570Double = Literal("double", "Float")
571SizeT = Literal("size_t", "UInt")
572WString = Literal("wchar_t *", "WString")
José Fonsecad626cf42008-07-07 07:43:16 +0900573
José Fonseca2250a0e2010-11-26 15:01:29 +0000574Int8 = Literal("int8_t", "SInt")
575UInt8 = Literal("uint8_t", "UInt")
576Int16 = Literal("int16_t", "SInt")
577UInt16 = Literal("uint16_t", "UInt")
578Int32 = Literal("int32_t", "SInt")
579UInt32 = Literal("uint32_t", "UInt")
580Int64 = Literal("int64_t", "SInt")
581UInt64 = Literal("uint64_t", "UInt")