blob: 1b9bf58ad5f5ab0f4b6cb57f5f6f275048e3f628 [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):
José Fonseca903c2ca2011-09-23 09:43:05 +010086 # 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:
José Fonseca6fac5ae2010-11-29 16:09:13 +000094 expr = type.expr + " const"
95 else:
José Fonseca903c2ca2011-09-23 09:43:05 +010096 # The most legible
José Fonseca6fac5ae2010-11-29 16:09:13 +000097 expr = "const " + type.expr
98
99 Type.__init__(self, expr, 'C' + type.id)
100
101 self.type = type
102
103 def visit(self, visitor, *args, **kwargs):
104 return visitor.visit_const(self, *args, **kwargs)
105
106
107class Pointer(Type):
108
109 def __init__(self, type):
110 Type.__init__(self, type.expr + " *", 'P' + type.id)
111 self.type = type
112
113 def visit(self, visitor, *args, **kwargs):
114 return visitor.visit_pointer(self, *args, **kwargs)
115
116
117class Handle(Type):
118
José Fonseca8a844ae2010-12-06 18:50:52 +0000119 def __init__(self, name, type, range=None, key=None):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000120 Type.__init__(self, type.expr, 'P' + type.id)
121 self.name = name
122 self.type = type
123 self.range = range
José Fonseca8a844ae2010-12-06 18:50:52 +0000124 self.key = key
José Fonseca6fac5ae2010-11-29 16:09:13 +0000125
126 def visit(self, visitor, *args, **kwargs):
127 return visitor.visit_handle(self, *args, **kwargs)
128
129
130def ConstPointer(type):
131 return Pointer(Const(type))
132
133
134class Enum(Type):
135
José Fonseca6fac5ae2010-11-29 16:09:13 +0000136 def __init__(self, name, values):
137 Type.__init__(self, name)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000138 self.values = list(values)
139
140 def visit(self, visitor, *args, **kwargs):
141 return visitor.visit_enum(self, *args, **kwargs)
142
143
144def FakeEnum(type, values):
145 return Enum(type.expr, values)
146
147
148class Bitmask(Type):
149
150 def __init__(self, type, values):
151 Type.__init__(self, type.expr)
152 self.type = type
153 self.values = values
154
155 def visit(self, visitor, *args, **kwargs):
156 return visitor.visit_bitmask(self, *args, **kwargs)
157
158Flags = Bitmask
159
160
161class Array(Type):
162
163 def __init__(self, type, length):
164 Type.__init__(self, type.expr + " *")
165 self.type = type
166 self.length = length
167
168 def visit(self, visitor, *args, **kwargs):
169 return visitor.visit_array(self, *args, **kwargs)
170
171
172class Blob(Type):
173
174 def __init__(self, type, size):
175 Type.__init__(self, type.expr + ' *')
176 self.type = type
177 self.size = size
178
179 def visit(self, visitor, *args, **kwargs):
180 return visitor.visit_blob(self, *args, **kwargs)
181
182
183class Struct(Type):
184
185 def __init__(self, name, members):
186 Type.__init__(self, name)
187 self.name = name
188 self.members = members
189
190 def visit(self, visitor, *args, **kwargs):
191 return visitor.visit_struct(self, *args, **kwargs)
192
193
194class Alias(Type):
195
196 def __init__(self, expr, type):
197 Type.__init__(self, expr)
198 self.type = type
199
200 def visit(self, visitor, *args, **kwargs):
201 return visitor.visit_alias(self, *args, **kwargs)
202
203
204def Out(type, name):
205 arg = Arg(type, name, output=True)
206 return arg
207
208
209class Arg:
210
211 def __init__(self, type, name, output=False):
212 self.type = type
213 self.name = name
214 self.output = output
215 self.index = None
216
217 def __str__(self):
218 return '%s %s' % (self.type, self.name)
219
220
221class Function:
222
223 __id = 0
224
José Fonsecabca0f412011-04-24 20:53:38 +0100225 def __init__(self, type, name, args, call = '', fail = None, sideeffects=True):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000226 self.id = Function.__id
227 Function.__id += 1
228
229 self.type = type
230 self.name = name
231
232 self.args = []
233 index = 0
234 for arg in args:
José Fonseca8384ccb2011-05-25 10:12:02 +0100235 if not isinstance(arg, Arg):
236 if isinstance(arg, tuple):
237 arg_type, arg_name = arg
238 else:
239 arg_type = arg
240 arg_name = "arg%u" % index
José Fonseca6fac5ae2010-11-29 16:09:13 +0000241 arg = Arg(arg_type, arg_name)
242 arg.index = index
243 index += 1
244 self.args.append(arg)
245
246 self.call = call
247 self.fail = fail
248 self.sideeffects = sideeffects
José Fonseca6fac5ae2010-11-29 16:09:13 +0000249
250 def prototype(self, name=None):
251 if name is not None:
252 name = name.strip()
253 else:
254 name = self.name
255 s = name
256 if self.call:
257 s = self.call + ' ' + s
258 if name.startswith('*'):
259 s = '(' + s + ')'
260 s = self.type.expr + ' ' + s
261 s += "("
262 if self.args:
263 s += ", ".join(["%s %s" % (arg.type, arg.name) for arg in self.args])
264 else:
265 s += "void"
266 s += ")"
267 return s
268
269
270def StdFunction(*args, **kwargs):
271 kwargs.setdefault('call', '__stdcall')
272 return Function(*args, **kwargs)
273
274
275def FunctionPointer(type, name, args, **kwargs):
276 # XXX: We should probably treat function pointers (callbacks or not) in a generic fashion
277 return Opaque(name)
278
279
280class Interface(Type):
281
282 def __init__(self, name, base=None):
283 Type.__init__(self, name)
284 self.name = name
285 self.base = base
286 self.methods = []
287
288 def visit(self, visitor, *args, **kwargs):
289 return visitor.visit_interface(self, *args, **kwargs)
290
291 def itermethods(self):
292 if self.base is not None:
293 for method in self.base.itermethods():
294 yield method
295 for method in self.methods:
296 yield method
297 raise StopIteration
298
299
300class Method(Function):
301
302 def __init__(self, type, name, args):
303 Function.__init__(self, type, name, args, call = '__stdcall')
304 for index in range(len(self.args)):
305 self.args[index].index = index + 1
306
307
José Fonseca6fac5ae2010-11-29 16:09:13 +0000308class String(Type):
309
310 def __init__(self, expr = "char *", length = None):
311 Type.__init__(self, expr)
312 self.length = length
313
314 def visit(self, visitor, *args, **kwargs):
315 return visitor.visit_string(self, *args, **kwargs)
316
317# C string (i.e., zero terminated)
318CString = String()
319
320
321class Opaque(Type):
322 '''Opaque pointer.'''
323
324 def __init__(self, expr):
325 Type.__init__(self, expr)
326
327 def visit(self, visitor, *args, **kwargs):
328 return visitor.visit_opaque(self, *args, **kwargs)
329
330
331def OpaquePointer(type, *args):
332 return Opaque(type.expr + ' *')
333
334def OpaqueArray(type, size):
335 return Opaque(type.expr + ' *')
336
337def OpaqueBlob(type, size):
338 return Opaque(type.expr + ' *')
José Fonseca8a56d142008-07-09 12:18:08 +0900339
José Fonseca501f2862010-11-19 20:41:18 +0000340
José Fonseca16d46dd2011-10-13 09:52:52 +0100341class Polymorphic(Type):
342
343 def __init__(self, default_type, switch_expr, switch_types):
344 Type.__init__(self, default_type.expr)
345 self.default_type = default_type
346 self.switch_expr = switch_expr
347 self.switch_types = switch_types
348
349 def visit(self, visitor, *args, **kwargs):
350 return visitor.visit_polymorphic(self, *args, **kwargs)
351
352
José Fonseca501f2862010-11-19 20:41:18 +0000353class Visitor:
354
355 def visit(self, type, *args, **kwargs):
356 return type.visit(self, *args, **kwargs)
357
José Fonsecac356d6a2010-11-23 14:27:25 +0000358 def visit_void(self, void, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000359 raise NotImplementedError
360
José Fonsecac356d6a2010-11-23 14:27:25 +0000361 def visit_literal(self, literal, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000362 raise NotImplementedError
363
José Fonsecac356d6a2010-11-23 14:27:25 +0000364 def visit_string(self, string, *args, **kwargs):
José Fonseca2defc982010-11-22 16:59:10 +0000365 raise NotImplementedError
366
José Fonsecac356d6a2010-11-23 14:27:25 +0000367 def visit_const(self, const, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000368 raise NotImplementedError
369
José Fonsecac356d6a2010-11-23 14:27:25 +0000370 def visit_struct(self, struct, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000371 raise NotImplementedError
372
José Fonsecac356d6a2010-11-23 14:27:25 +0000373 def visit_array(self, array, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000374 raise NotImplementedError
375
José Fonsecac356d6a2010-11-23 14:27:25 +0000376 def visit_blob(self, blob, *args, **kwargs):
José Fonseca885f2652010-11-20 11:22:25 +0000377 raise NotImplementedError
378
José Fonsecac356d6a2010-11-23 14:27:25 +0000379 def visit_enum(self, enum, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000380 raise NotImplementedError
381
José Fonsecac356d6a2010-11-23 14:27:25 +0000382 def visit_bitmask(self, bitmask, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000383 raise NotImplementedError
384
José Fonsecac356d6a2010-11-23 14:27:25 +0000385 def visit_pointer(self, pointer, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000386 raise NotImplementedError
387
José Fonseca50d78d82010-11-23 22:13:14 +0000388 def visit_handle(self, handle, *args, **kwargs):
389 raise NotImplementedError
390
José Fonsecac356d6a2010-11-23 14:27:25 +0000391 def visit_alias(self, alias, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000392 raise NotImplementedError
393
José Fonsecac356d6a2010-11-23 14:27:25 +0000394 def visit_opaque(self, opaque, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000395 raise NotImplementedError
396
José Fonsecac356d6a2010-11-23 14:27:25 +0000397 def visit_interface(self, interface, *args, **kwargs):
398 raise NotImplementedError
399
José Fonseca16d46dd2011-10-13 09:52:52 +0100400 def visit_polymorphic(self, polymorphic, *args, **kwargs):
401 raise NotImplementedError
402 #return self.visit(polymorphic.default_type, *args, **kwargs)
403
José Fonsecac356d6a2010-11-23 14:27:25 +0000404
405class OnceVisitor(Visitor):
406
407 def __init__(self):
408 self.__visited = set()
409
410 def visit(self, type, *args, **kwargs):
411 if type not in self.__visited:
412 self.__visited.add(type)
413 return type.visit(self, *args, **kwargs)
414 return None
415
José Fonseca501f2862010-11-19 20:41:18 +0000416
José Fonsecac9edb832010-11-20 09:03:10 +0000417class Rebuilder(Visitor):
418
419 def visit_void(self, void):
420 return void
421
422 def visit_literal(self, literal):
423 return literal
424
José Fonseca2defc982010-11-22 16:59:10 +0000425 def visit_string(self, string):
426 return string
427
José Fonsecac9edb832010-11-20 09:03:10 +0000428 def visit_const(self, const):
429 return Const(const.type)
430
431 def visit_struct(self, struct):
José Fonseca06aa2842011-05-05 07:55:54 +0100432 members = [(self.visit(type), name) for type, name in struct.members]
José Fonsecac9edb832010-11-20 09:03:10 +0000433 return Struct(struct.name, members)
434
435 def visit_array(self, array):
436 type = self.visit(array.type)
437 return Array(type, array.length)
438
José Fonseca885f2652010-11-20 11:22:25 +0000439 def visit_blob(self, blob):
440 type = self.visit(blob.type)
441 return Blob(type, blob.size)
442
José Fonsecac9edb832010-11-20 09:03:10 +0000443 def visit_enum(self, enum):
444 return enum
445
446 def visit_bitmask(self, bitmask):
447 type = self.visit(bitmask.type)
448 return Bitmask(type, bitmask.values)
449
450 def visit_pointer(self, pointer):
451 type = self.visit(pointer.type)
452 return Pointer(type)
453
José Fonseca50d78d82010-11-23 22:13:14 +0000454 def visit_handle(self, handle):
455 type = self.visit(handle.type)
José Fonseca8a844ae2010-12-06 18:50:52 +0000456 return Handle(handle.name, type, range=handle.range, key=handle.key)
José Fonseca50d78d82010-11-23 22:13:14 +0000457
José Fonsecac9edb832010-11-20 09:03:10 +0000458 def visit_alias(self, alias):
459 type = self.visit(alias.type)
460 return Alias(alias.expr, type)
461
462 def visit_opaque(self, opaque):
463 return opaque
464
José Fonseca16d46dd2011-10-13 09:52:52 +0100465 def visit_polymorphic(self, polymorphic):
466 default_type = self.visit(polymorphic.default_type)
467 switch_expr = polymorphic.switch_expr
468 switch_types = [(expr, self.visit(type)) for expr, type in polymorphic.switch_types]
469 return Polymorphic(default_type, switch_expr, switch_types)
470
José Fonsecac9edb832010-11-20 09:03:10 +0000471
José Fonsecae6a50bd2010-11-24 10:12:22 +0000472class Collector(Visitor):
473 '''Collect.'''
474
475 def __init__(self):
476 self.__visited = set()
477 self.types = []
478
479 def visit(self, type):
480 if type in self.__visited:
481 return
482 self.__visited.add(type)
483 Visitor.visit(self, type)
484 self.types.append(type)
485
486 def visit_void(self, literal):
487 pass
488
489 def visit_literal(self, literal):
490 pass
491
492 def visit_string(self, string):
493 pass
494
495 def visit_const(self, const):
496 self.visit(const.type)
497
498 def visit_struct(self, struct):
499 for type, name in struct.members:
500 self.visit(type)
501
502 def visit_array(self, array):
503 self.visit(array.type)
504
505 def visit_blob(self, array):
506 pass
507
508 def visit_enum(self, enum):
509 pass
510
511 def visit_bitmask(self, bitmask):
512 self.visit(bitmask.type)
513
514 def visit_pointer(self, pointer):
515 self.visit(pointer.type)
516
517 def visit_handle(self, handle):
518 self.visit(handle.type)
519
520 def visit_alias(self, alias):
521 self.visit(alias.type)
522
523 def visit_opaque(self, opaque):
524 pass
525
526 def visit_interface(self, interface):
José Fonseca87d1cc62010-11-29 15:57:25 +0000527 if interface.base is not None:
528 self.visit(interface.base)
529 for method in interface.itermethods():
530 for arg in method.args:
531 self.visit(arg.type)
532 self.visit(method.type)
José Fonsecae6a50bd2010-11-24 10:12:22 +0000533
José Fonseca16d46dd2011-10-13 09:52:52 +0100534 def visit_polymorphic(self, polymorphic):
535 self.visit(polymorphic.default_type)
536 for expr, type in polymorphic.switch_types:
537 self.visit(type)
538
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000539
540class API:
541
José Fonseca68ec4122011-02-20 11:25:25 +0000542 def __init__(self, name = None):
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000543 self.name = name
544 self.headers = []
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000545 self.functions = []
546 self.interfaces = []
547
José Fonsecae6a50bd2010-11-24 10:12:22 +0000548 def all_types(self):
549 collector = Collector()
550 for function in self.functions:
551 for arg in function.args:
552 collector.visit(arg.type)
553 collector.visit(function.type)
554 for interface in self.interfaces:
555 collector.visit(interface)
José Fonseca87d1cc62010-11-29 15:57:25 +0000556 for method in interface.itermethods():
José Fonsecae6a50bd2010-11-24 10:12:22 +0000557 for arg in method.args:
558 collector.visit(arg.type)
559 collector.visit(method.type)
560 return collector.types
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000561
562 def add_function(self, function):
563 self.functions.append(function)
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000564
565 def add_functions(self, functions):
566 for function in functions:
567 self.add_function(function)
568
569 def add_interface(self, interface):
570 self.interfaces.append(interface)
571
572 def add_interfaces(self, interfaces):
573 self.interfaces.extend(interfaces)
574
José Fonseca68ec4122011-02-20 11:25:25 +0000575 def add_api(self, api):
576 self.headers.extend(api.headers)
577 self.add_functions(api.functions)
578 self.add_interfaces(api.interfaces)
579
José Fonsecaeccec3e2011-02-20 09:01:25 +0000580 def get_function_by_name(self, name):
581 for function in self.functions:
582 if function.name == name:
583 return function
584 return None
585
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000586
José Fonseca51c1ef82010-11-15 16:09:14 +0000587Bool = Literal("bool", "Bool")
588SChar = Literal("signed char", "SInt")
589UChar = Literal("unsigned char", "UInt")
590Short = Literal("short", "SInt")
591Int = Literal("int", "SInt")
592Long = Literal("long", "SInt")
593LongLong = Literal("long long", "SInt")
594UShort = Literal("unsigned short", "UInt")
595UInt = Literal("unsigned int", "UInt")
596ULong = Literal("unsigned long", "UInt")
José Fonseca28f034f2010-11-22 20:31:25 +0000597ULongLong = Literal("unsigned long long", "UInt")
José Fonseca51c1ef82010-11-15 16:09:14 +0000598Float = Literal("float", "Float")
José Fonseca9ff74442011-05-07 01:17:49 +0100599Double = Literal("double", "Double")
José Fonseca51c1ef82010-11-15 16:09:14 +0000600SizeT = Literal("size_t", "UInt")
601WString = Literal("wchar_t *", "WString")
José Fonsecad626cf42008-07-07 07:43:16 +0900602
José Fonseca2250a0e2010-11-26 15:01:29 +0000603Int8 = Literal("int8_t", "SInt")
604UInt8 = Literal("uint8_t", "UInt")
605Int16 = Literal("int16_t", "SInt")
606UInt16 = Literal("uint16_t", "UInt")
607Int32 = Literal("int32_t", "SInt")
608UInt32 = Literal("uint32_t", "UInt")
609Int64 = Literal("int64_t", "SInt")
610UInt64 = Literal("uint64_t", "UInt")