]> git.cworth.org Git - apitrace/blob - stdapi.py
Accept zero valued bitmasks.
[apitrace] / stdapi.py
1 ##########################################################################
2 #
3 # Copyright 2008-2010 VMware, Inc.
4 # All Rights Reserved.
5 #
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:
12 #
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
15 #
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
22 # THE SOFTWARE.
23 #
24 ##########################################################################/
25
26 """C basic types"""
27
28
29 import debug
30
31
32 class 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
62 class _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
70 Void = _Void()
71
72
73 class 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
83 class 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
100 class 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
110 class Handle(Type):
111
112     def __init__(self, name, type, range=None, key=None):
113         Type.__init__(self, type.expr, 'P' + type.id)
114         self.name = name
115         self.type = type
116         self.range = range
117         self.key = key
118
119     def visit(self, visitor, *args, **kwargs):
120         return visitor.visit_handle(self, *args, **kwargs)
121
122
123 def ConstPointer(type):
124     return Pointer(Const(type))
125
126
127 class 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
141 def FakeEnum(type, values):
142     return Enum(type.expr, values)
143
144
145 class 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
155 Flags = Bitmask
156
157
158 class 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
169 class 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
180 class 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
191 class 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
201 def Out(type, name):
202     arg = Arg(type, name, output=True)
203     return arg
204
205
206 class 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
218 class 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
264 def StdFunction(*args, **kwargs):
265     kwargs.setdefault('call', '__stdcall')
266     return Function(*args, **kwargs)
267
268
269 def 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
274 class 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
294 class 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
302 def WrapPointer(type):
303     return Pointer(type)
304
305
306 class 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)
316 CString = String()
317
318
319 class 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
329 def OpaquePointer(type, *args):
330     return Opaque(type.expr + ' *')
331
332 def OpaqueArray(type, size):
333     return Opaque(type.expr + ' *')
334
335 def OpaqueBlob(type, size):
336     return Opaque(type.expr + ' *')
337
338
339 class Visitor:
340
341     def visit(self, type, *args, **kwargs):
342         return type.visit(self, *args, **kwargs)
343
344     def visit_void(self, void, *args, **kwargs):
345         raise NotImplementedError
346
347     def visit_literal(self, literal, *args, **kwargs):
348         raise NotImplementedError
349
350     def visit_string(self, string, *args, **kwargs):
351         raise NotImplementedError
352
353     def visit_const(self, const, *args, **kwargs):
354         raise NotImplementedError
355
356     def visit_struct(self, struct, *args, **kwargs):
357         raise NotImplementedError
358
359     def visit_array(self, array, *args, **kwargs):
360         raise NotImplementedError
361
362     def visit_blob(self, blob, *args, **kwargs):
363         raise NotImplementedError
364
365     def visit_enum(self, enum, *args, **kwargs):
366         raise NotImplementedError
367
368     def visit_bitmask(self, bitmask, *args, **kwargs):
369         raise NotImplementedError
370
371     def visit_pointer(self, pointer, *args, **kwargs):
372         raise NotImplementedError
373
374     def visit_handle(self, handle, *args, **kwargs):
375         raise NotImplementedError
376
377     def visit_alias(self, alias, *args, **kwargs):
378         raise NotImplementedError
379
380     def visit_opaque(self, opaque, *args, **kwargs):
381         raise NotImplementedError
382
383     def visit_interface(self, interface, *args, **kwargs):
384         raise NotImplementedError
385
386
387 class 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
398
399 class Rebuilder(Visitor):
400
401     def visit_void(self, void):
402         return void
403
404     def visit_literal(self, literal):
405         return literal
406
407     def visit_string(self, string):
408         return string
409
410     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
421     def visit_blob(self, blob):
422         type = self.visit(blob.type)
423         return Blob(type, blob.size)
424
425     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
436     def visit_handle(self, handle):
437         type = self.visit(handle.type)
438         return Handle(handle.name, type, range=handle.range, key=handle.key)
439
440     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
448 class 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):
503         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)
509
510
511 class API:
512
513     def __init__(self, name = None):
514         self.name = name
515         self.headers = []
516         self.functions = []
517         self.interfaces = []
518
519     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)
527             for method in interface.itermethods():
528                 for arg in method.args:
529                     collector.visit(arg.type)
530                 collector.visit(method.type)
531         return collector.types
532
533     def add_function(self, function):
534         self.functions.append(function)
535
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
546     def add_api(self, api):
547         self.headers.extend(api.headers)
548         self.add_functions(api.functions)
549         self.add_interfaces(api.interfaces)
550
551     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
557
558 Bool = Literal("bool", "Bool")
559 SChar = Literal("signed char", "SInt")
560 UChar = Literal("unsigned char", "UInt")
561 Short = Literal("short", "SInt")
562 Int = Literal("int", "SInt")
563 Long = Literal("long", "SInt")
564 LongLong = Literal("long long", "SInt")
565 UShort = Literal("unsigned short", "UInt")
566 UInt = Literal("unsigned int", "UInt")
567 ULong = Literal("unsigned long", "UInt")
568 ULongLong = Literal("unsigned long long", "UInt")
569 Float = Literal("float", "Float")
570 Double = Literal("double", "Float")
571 SizeT = Literal("size_t", "UInt")
572 WString = Literal("wchar_t *", "WString")
573
574 Int8 = Literal("int8_t", "SInt")
575 UInt8 = Literal("uint8_t", "UInt")
576 Int16 = Literal("int16_t", "SInt")
577 UInt16 = Literal("uint16_t", "UInt")
578 Int32 = Literal("int32_t", "SInt")
579 UInt32 = Literal("uint32_t", "UInt")
580 Int64 = Literal("int64_t", "SInt")
581 UInt64 = Literal("uint64_t", "UInt")