]> git.cworth.org Git - apitrace/blob - stdapi.py
PNG read support.
[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):
113         Type.__init__(self, type.expr, 'P' + type.id)
114         self.name = name
115         self.type = type
116         self.range = range
117
118     def visit(self, visitor, *args, **kwargs):
119         return visitor.visit_handle(self, *args, **kwargs)
120
121
122 def ConstPointer(type):
123     return Pointer(Const(type))
124
125
126 class Enum(Type):
127
128     __vid = 0
129
130     def __init__(self, name, values):
131         Type.__init__(self, name)
132         self.vid = Enum.__vid
133         Enum.__vid += len(values)
134         self.values = list(values)
135     
136     def visit(self, visitor, *args, **kwargs):
137         return visitor.visit_enum(self, *args, **kwargs)
138
139
140 def FakeEnum(type, values):
141     return Enum(type.expr, values)
142
143
144 class Bitmask(Type):
145
146     def __init__(self, type, values):
147         Type.__init__(self, type.expr)
148         self.type = type
149         self.values = values
150
151     def visit(self, visitor, *args, **kwargs):
152         return visitor.visit_bitmask(self, *args, **kwargs)
153
154 Flags = Bitmask
155
156
157 class Array(Type):
158
159     def __init__(self, type, length):
160         Type.__init__(self, type.expr + " *")
161         self.type = type
162         self.length = length
163
164     def visit(self, visitor, *args, **kwargs):
165         return visitor.visit_array(self, *args, **kwargs)
166
167
168 class Blob(Type):
169
170     def __init__(self, type, size):
171         Type.__init__(self, type.expr + ' *')
172         self.type = type
173         self.size = size
174
175     def visit(self, visitor, *args, **kwargs):
176         return visitor.visit_blob(self, *args, **kwargs)
177
178
179 class Struct(Type):
180
181     def __init__(self, name, members):
182         Type.__init__(self, name)
183         self.name = name
184         self.members = members
185
186     def visit(self, visitor, *args, **kwargs):
187         return visitor.visit_struct(self, *args, **kwargs)
188
189
190 class Alias(Type):
191
192     def __init__(self, expr, type):
193         Type.__init__(self, expr)
194         self.type = type
195
196     def visit(self, visitor, *args, **kwargs):
197         return visitor.visit_alias(self, *args, **kwargs)
198
199
200 def Out(type, name):
201     arg = Arg(type, name, output=True)
202     return arg
203
204
205 class Arg:
206
207     def __init__(self, type, name, output=False):
208         self.type = type
209         self.name = name
210         self.output = output
211         self.index = None
212
213     def __str__(self):
214         return '%s %s' % (self.type, self.name)
215
216
217 class Function:
218
219     __id = 0
220
221     def __init__(self, type, name, args, call = '', fail = None, sideeffects=True, hidden=False):
222         self.id = Function.__id
223         Function.__id += 1
224
225         self.type = type
226         self.name = name
227
228         self.args = []
229         index = 0
230         for arg in args:
231             if isinstance(arg, tuple):
232                 arg_type, arg_name = arg
233                 arg = Arg(arg_type, arg_name)
234             arg.index = index
235             index += 1
236             self.args.append(arg)
237
238         self.call = call
239         self.fail = fail
240         self.sideeffects = sideeffects
241         self.hidden = False
242
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
263 def StdFunction(*args, **kwargs):
264     kwargs.setdefault('call', '__stdcall')
265     return Function(*args, **kwargs)
266
267
268 def 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
273 class 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
293 class 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
301 def WrapPointer(type):
302     return Pointer(type)
303
304
305 class 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)
315 CString = String()
316
317
318 class 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
328 def OpaquePointer(type, *args):
329     return Opaque(type.expr + ' *')
330
331 def OpaqueArray(type, size):
332     return Opaque(type.expr + ' *')
333
334 def OpaqueBlob(type, size):
335     return Opaque(type.expr + ' *')
336
337
338 class Visitor:
339
340     def visit(self, type, *args, **kwargs):
341         return type.visit(self, *args, **kwargs)
342
343     def visit_void(self, void, *args, **kwargs):
344         raise NotImplementedError
345
346     def visit_literal(self, literal, *args, **kwargs):
347         raise NotImplementedError
348
349     def visit_string(self, string, *args, **kwargs):
350         raise NotImplementedError
351
352     def visit_const(self, const, *args, **kwargs):
353         raise NotImplementedError
354
355     def visit_struct(self, struct, *args, **kwargs):
356         raise NotImplementedError
357
358     def visit_array(self, array, *args, **kwargs):
359         raise NotImplementedError
360
361     def visit_blob(self, blob, *args, **kwargs):
362         raise NotImplementedError
363
364     def visit_enum(self, enum, *args, **kwargs):
365         raise NotImplementedError
366
367     def visit_bitmask(self, bitmask, *args, **kwargs):
368         raise NotImplementedError
369
370     def visit_pointer(self, pointer, *args, **kwargs):
371         raise NotImplementedError
372
373     def visit_handle(self, handle, *args, **kwargs):
374         raise NotImplementedError
375
376     def visit_alias(self, alias, *args, **kwargs):
377         raise NotImplementedError
378
379     def visit_opaque(self, opaque, *args, **kwargs):
380         raise NotImplementedError
381
382     def visit_interface(self, interface, *args, **kwargs):
383         raise NotImplementedError
384
385
386 class 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
397
398 class Rebuilder(Visitor):
399
400     def visit_void(self, void):
401         return void
402
403     def visit_literal(self, literal):
404         return literal
405
406     def visit_string(self, string):
407         return string
408
409     def visit_const(self, const):
410         return Const(const.type)
411
412     def visit_struct(self, struct):
413         members = [self.visit(member) for member in struct.members]
414         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
420     def visit_blob(self, blob):
421         type = self.visit(blob.type)
422         return Blob(type, blob.size)
423
424     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
435     def visit_handle(self, handle):
436         type = self.visit(handle.type)
437         return Handle(handle.name, type, handle.range)
438
439     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
447 class 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):
502         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)
508
509
510 class API:
511
512     def __init__(self, name):
513         self.name = name
514         self.headers = []
515         self.functions = []
516         self.interfaces = []
517
518     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)
526             for method in interface.itermethods():
527                 for arg in method.args:
528                     collector.visit(arg.type)
529                 collector.visit(method.type)
530         return collector.types
531
532     def add_function(self, function):
533         self.functions.append(function)
534
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
545
546 Bool = Literal("bool", "Bool")
547 SChar = Literal("signed char", "SInt")
548 UChar = Literal("unsigned char", "UInt")
549 Short = Literal("short", "SInt")
550 Int = Literal("int", "SInt")
551 Long = Literal("long", "SInt")
552 LongLong = Literal("long long", "SInt")
553 UShort = Literal("unsigned short", "UInt")
554 UInt = Literal("unsigned int", "UInt")
555 ULong = Literal("unsigned long", "UInt")
556 ULongLong = Literal("unsigned long long", "UInt")
557 Float = Literal("float", "Float")
558 Double = Literal("double", "Float")
559 SizeT = Literal("size_t", "UInt")
560 WString = Literal("wchar_t *", "WString")
561
562 Int8 = Literal("int8_t", "SInt")
563 UInt8 = Literal("uint8_t", "UInt")
564 Int16 = Literal("int16_t", "SInt")
565 UInt16 = Literal("uint16_t", "UInt")
566 Int32 = Literal("int32_t", "SInt")
567 UInt32 = Literal("uint32_t", "UInt")
568 Int64 = Literal("int64_t", "SInt")
569 UInt64 = Literal("uint64_t", "UInt")