blob: 8852345884a69ce26b081a755201d9aa12e050bc [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
José Fonseca6fac5ae2010-11-29 16:09:13 +0000129 def __init__(self, name, values):
130 Type.__init__(self, name)
José Fonseca6fac5ae2010-11-29 16:09:13 +0000131 self.values = list(values)
132
133 def visit(self, visitor, *args, **kwargs):
134 return visitor.visit_enum(self, *args, **kwargs)
135
136
137def FakeEnum(type, values):
138 return Enum(type.expr, values)
139
140
141class Bitmask(Type):
142
143 def __init__(self, type, values):
144 Type.__init__(self, type.expr)
145 self.type = type
146 self.values = values
147
148 def visit(self, visitor, *args, **kwargs):
149 return visitor.visit_bitmask(self, *args, **kwargs)
150
151Flags = Bitmask
152
153
154class Array(Type):
155
156 def __init__(self, type, length):
157 Type.__init__(self, type.expr + " *")
158 self.type = type
159 self.length = length
160
161 def visit(self, visitor, *args, **kwargs):
162 return visitor.visit_array(self, *args, **kwargs)
163
164
165class Blob(Type):
166
167 def __init__(self, type, size):
168 Type.__init__(self, type.expr + ' *')
169 self.type = type
170 self.size = size
171
172 def visit(self, visitor, *args, **kwargs):
173 return visitor.visit_blob(self, *args, **kwargs)
174
175
176class Struct(Type):
177
178 def __init__(self, name, members):
179 Type.__init__(self, name)
180 self.name = name
181 self.members = members
182
183 def visit(self, visitor, *args, **kwargs):
184 return visitor.visit_struct(self, *args, **kwargs)
185
186
187class Alias(Type):
188
189 def __init__(self, expr, type):
190 Type.__init__(self, expr)
191 self.type = type
192
193 def visit(self, visitor, *args, **kwargs):
194 return visitor.visit_alias(self, *args, **kwargs)
195
196
197def Out(type, name):
198 arg = Arg(type, name, output=True)
199 return arg
200
201
202class Arg:
203
204 def __init__(self, type, name, output=False):
205 self.type = type
206 self.name = name
207 self.output = output
208 self.index = None
209
210 def __str__(self):
211 return '%s %s' % (self.type, self.name)
212
213
214class Function:
215
216 __id = 0
217
José Fonsecabca0f412011-04-24 20:53:38 +0100218 def __init__(self, type, name, args, call = '', fail = None, sideeffects=True):
José Fonseca6fac5ae2010-11-29 16:09:13 +0000219 self.id = Function.__id
220 Function.__id += 1
221
222 self.type = type
223 self.name = name
224
225 self.args = []
226 index = 0
227 for arg in args:
José Fonseca8384ccb2011-05-25 10:12:02 +0100228 if not isinstance(arg, Arg):
229 if isinstance(arg, tuple):
230 arg_type, arg_name = arg
231 else:
232 arg_type = arg
233 arg_name = "arg%u" % index
José Fonseca6fac5ae2010-11-29 16:09:13 +0000234 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
José Fonseca6fac5ae2010-11-29 16:09:13 +0000242
243 def prototype(self, name=None):
244 if name is not None:
245 name = name.strip()
246 else:
247 name = self.name
248 s = name
249 if self.call:
250 s = self.call + ' ' + s
251 if name.startswith('*'):
252 s = '(' + s + ')'
253 s = self.type.expr + ' ' + s
254 s += "("
255 if self.args:
256 s += ", ".join(["%s %s" % (arg.type, arg.name) for arg in self.args])
257 else:
258 s += "void"
259 s += ")"
260 return s
261
262
263def StdFunction(*args, **kwargs):
264 kwargs.setdefault('call', '__stdcall')
265 return Function(*args, **kwargs)
266
267
268def FunctionPointer(type, name, args, **kwargs):
269 # XXX: We should probably treat function pointers (callbacks or not) in a generic fashion
270 return Opaque(name)
271
272
273class Interface(Type):
274
275 def __init__(self, name, base=None):
276 Type.__init__(self, name)
277 self.name = name
278 self.base = base
279 self.methods = []
280
281 def visit(self, visitor, *args, **kwargs):
282 return visitor.visit_interface(self, *args, **kwargs)
283
284 def itermethods(self):
285 if self.base is not None:
286 for method in self.base.itermethods():
287 yield method
288 for method in self.methods:
289 yield method
290 raise StopIteration
291
292
293class Method(Function):
294
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
299
300
301def WrapPointer(type):
302 return Pointer(type)
303
304
305class String(Type):
306
307 def __init__(self, expr = "char *", length = None):
308 Type.__init__(self, expr)
309 self.length = length
310
311 def visit(self, visitor, *args, **kwargs):
312 return visitor.visit_string(self, *args, **kwargs)
313
314# C string (i.e., zero terminated)
315CString = String()
316
317
318class Opaque(Type):
319 '''Opaque pointer.'''
320
321 def __init__(self, expr):
322 Type.__init__(self, expr)
323
324 def visit(self, visitor, *args, **kwargs):
325 return visitor.visit_opaque(self, *args, **kwargs)
326
327
328def OpaquePointer(type, *args):
329 return Opaque(type.expr + ' *')
330
331def OpaqueArray(type, size):
332 return Opaque(type.expr + ' *')
333
334def OpaqueBlob(type, size):
335 return Opaque(type.expr + ' *')
José Fonseca8a56d142008-07-09 12:18:08 +0900336
José Fonseca501f2862010-11-19 20:41:18 +0000337
338class Visitor:
339
340 def visit(self, type, *args, **kwargs):
341 return type.visit(self, *args, **kwargs)
342
José Fonsecac356d6a2010-11-23 14:27:25 +0000343 def visit_void(self, void, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000344 raise NotImplementedError
345
José Fonsecac356d6a2010-11-23 14:27:25 +0000346 def visit_literal(self, literal, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000347 raise NotImplementedError
348
José Fonsecac356d6a2010-11-23 14:27:25 +0000349 def visit_string(self, string, *args, **kwargs):
José Fonseca2defc982010-11-22 16:59:10 +0000350 raise NotImplementedError
351
José Fonsecac356d6a2010-11-23 14:27:25 +0000352 def visit_const(self, const, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000353 raise NotImplementedError
354
José Fonsecac356d6a2010-11-23 14:27:25 +0000355 def visit_struct(self, struct, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000356 raise NotImplementedError
357
José Fonsecac356d6a2010-11-23 14:27:25 +0000358 def visit_array(self, array, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000359 raise NotImplementedError
360
José Fonsecac356d6a2010-11-23 14:27:25 +0000361 def visit_blob(self, blob, *args, **kwargs):
José Fonseca885f2652010-11-20 11:22:25 +0000362 raise NotImplementedError
363
José Fonsecac356d6a2010-11-23 14:27:25 +0000364 def visit_enum(self, enum, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000365 raise NotImplementedError
366
José Fonsecac356d6a2010-11-23 14:27:25 +0000367 def visit_bitmask(self, bitmask, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000368 raise NotImplementedError
369
José Fonsecac356d6a2010-11-23 14:27:25 +0000370 def visit_pointer(self, pointer, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000371 raise NotImplementedError
372
José Fonseca50d78d82010-11-23 22:13:14 +0000373 def visit_handle(self, handle, *args, **kwargs):
374 raise NotImplementedError
375
José Fonsecac356d6a2010-11-23 14:27:25 +0000376 def visit_alias(self, alias, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000377 raise NotImplementedError
378
José Fonsecac356d6a2010-11-23 14:27:25 +0000379 def visit_opaque(self, opaque, *args, **kwargs):
José Fonseca501f2862010-11-19 20:41:18 +0000380 raise NotImplementedError
381
José Fonsecac356d6a2010-11-23 14:27:25 +0000382 def visit_interface(self, interface, *args, **kwargs):
383 raise NotImplementedError
384
385
386class OnceVisitor(Visitor):
387
388 def __init__(self):
389 self.__visited = set()
390
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)
395 return None
396
José Fonseca501f2862010-11-19 20:41:18 +0000397
José Fonsecac9edb832010-11-20 09:03:10 +0000398class Rebuilder(Visitor):
399
400 def visit_void(self, void):
401 return void
402
403 def visit_literal(self, literal):
404 return literal
405
José Fonseca2defc982010-11-22 16:59:10 +0000406 def visit_string(self, string):
407 return string
408
José Fonsecac9edb832010-11-20 09:03:10 +0000409 def visit_const(self, const):
410 return Const(const.type)
411
412 def visit_struct(self, struct):
José Fonseca06aa2842011-05-05 07:55:54 +0100413 members = [(self.visit(type), name) for type, name in struct.members]
José Fonsecac9edb832010-11-20 09:03:10 +0000414 return Struct(struct.name, members)
415
416 def visit_array(self, array):
417 type = self.visit(array.type)
418 return Array(type, array.length)
419
José Fonseca885f2652010-11-20 11:22:25 +0000420 def visit_blob(self, blob):
421 type = self.visit(blob.type)
422 return Blob(type, blob.size)
423
José Fonsecac9edb832010-11-20 09:03:10 +0000424 def visit_enum(self, enum):
425 return enum
426
427 def visit_bitmask(self, bitmask):
428 type = self.visit(bitmask.type)
429 return Bitmask(type, bitmask.values)
430
431 def visit_pointer(self, pointer):
432 type = self.visit(pointer.type)
433 return Pointer(type)
434
José Fonseca50d78d82010-11-23 22:13:14 +0000435 def visit_handle(self, handle):
436 type = self.visit(handle.type)
José Fonseca8a844ae2010-12-06 18:50:52 +0000437 return Handle(handle.name, type, range=handle.range, key=handle.key)
José Fonseca50d78d82010-11-23 22:13:14 +0000438
José Fonsecac9edb832010-11-20 09:03:10 +0000439 def visit_alias(self, alias):
440 type = self.visit(alias.type)
441 return Alias(alias.expr, type)
442
443 def visit_opaque(self, opaque):
444 return opaque
445
446
José Fonsecae6a50bd2010-11-24 10:12:22 +0000447class Collector(Visitor):
448 '''Collect.'''
449
450 def __init__(self):
451 self.__visited = set()
452 self.types = []
453
454 def visit(self, type):
455 if type in self.__visited:
456 return
457 self.__visited.add(type)
458 Visitor.visit(self, type)
459 self.types.append(type)
460
461 def visit_void(self, literal):
462 pass
463
464 def visit_literal(self, literal):
465 pass
466
467 def visit_string(self, string):
468 pass
469
470 def visit_const(self, const):
471 self.visit(const.type)
472
473 def visit_struct(self, struct):
474 for type, name in struct.members:
475 self.visit(type)
476
477 def visit_array(self, array):
478 self.visit(array.type)
479
480 def visit_blob(self, array):
481 pass
482
483 def visit_enum(self, enum):
484 pass
485
486 def visit_bitmask(self, bitmask):
487 self.visit(bitmask.type)
488
489 def visit_pointer(self, pointer):
490 self.visit(pointer.type)
491
492 def visit_handle(self, handle):
493 self.visit(handle.type)
494
495 def visit_alias(self, alias):
496 self.visit(alias.type)
497
498 def visit_opaque(self, opaque):
499 pass
500
501 def visit_interface(self, interface):
José Fonseca87d1cc62010-11-29 15:57:25 +0000502 if interface.base is not None:
503 self.visit(interface.base)
504 for method in interface.itermethods():
505 for arg in method.args:
506 self.visit(arg.type)
507 self.visit(method.type)
José Fonsecae6a50bd2010-11-24 10:12:22 +0000508
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000509
510class API:
511
José Fonseca68ec4122011-02-20 11:25:25 +0000512 def __init__(self, name = None):
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000513 self.name = name
514 self.headers = []
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000515 self.functions = []
516 self.interfaces = []
517
José Fonsecae6a50bd2010-11-24 10:12:22 +0000518 def all_types(self):
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)
José Fonseca87d1cc62010-11-29 15:57:25 +0000526 for method in interface.itermethods():
José Fonsecae6a50bd2010-11-24 10:12:22 +0000527 for arg in method.args:
528 collector.visit(arg.type)
529 collector.visit(method.type)
530 return collector.types
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000531
532 def add_function(self, function):
533 self.functions.append(function)
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000534
535 def add_functions(self, functions):
536 for function in functions:
537 self.add_function(function)
538
539 def add_interface(self, interface):
540 self.interfaces.append(interface)
541
542 def add_interfaces(self, interfaces):
543 self.interfaces.extend(interfaces)
544
José Fonseca68ec4122011-02-20 11:25:25 +0000545 def add_api(self, api):
546 self.headers.extend(api.headers)
547 self.add_functions(api.functions)
548 self.add_interfaces(api.interfaces)
549
José Fonsecaeccec3e2011-02-20 09:01:25 +0000550 def get_function_by_name(self, name):
551 for function in self.functions:
552 if function.name == name:
553 return function
554 return None
555
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000556
José Fonseca51c1ef82010-11-15 16:09:14 +0000557Bool = Literal("bool", "Bool")
558SChar = Literal("signed char", "SInt")
559UChar = Literal("unsigned char", "UInt")
560Short = Literal("short", "SInt")
561Int = Literal("int", "SInt")
562Long = Literal("long", "SInt")
563LongLong = Literal("long long", "SInt")
564UShort = Literal("unsigned short", "UInt")
565UInt = Literal("unsigned int", "UInt")
566ULong = Literal("unsigned long", "UInt")
José Fonseca28f034f2010-11-22 20:31:25 +0000567ULongLong = Literal("unsigned long long", "UInt")
José Fonseca51c1ef82010-11-15 16:09:14 +0000568Float = Literal("float", "Float")
José Fonseca9ff74442011-05-07 01:17:49 +0100569Double = Literal("double", "Double")
José Fonseca51c1ef82010-11-15 16:09:14 +0000570SizeT = Literal("size_t", "UInt")
571WString = Literal("wchar_t *", "WString")
José Fonsecad626cf42008-07-07 07:43:16 +0900572
José Fonseca2250a0e2010-11-26 15:01:29 +0000573Int8 = Literal("int8_t", "SInt")
574UInt8 = Literal("uint8_t", "UInt")
575Int16 = Literal("int16_t", "SInt")
576UInt16 = Literal("uint16_t", "UInt")
577Int32 = Literal("int32_t", "SInt")
578UInt32 = Literal("uint32_t", "UInt")
579Int64 = Literal("int64_t", "SInt")
580UInt64 = Literal("uint64_t", "UInt")