]> git.cworth.org Git - apitrace/blob - stdapi.py
More efficient call representation.
[apitrace] / stdapi.py
1 ##########################################################################
2 #
3 # Copyright 2008-2009 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 all_types = {}
33
34
35 class Visitor:
36
37     def visit(self, type, *args, **kwargs):
38         return type.visit(self, *args, **kwargs)
39
40     def visit_void(self, void, *args, **kwargs):
41         raise NotImplementedError
42
43     def visit_literal(self, literal, *args, **kwargs):
44         raise NotImplementedError
45
46     def visit_string(self, string, *args, **kwargs):
47         raise NotImplementedError
48
49     def visit_const(self, const, *args, **kwargs):
50         raise NotImplementedError
51
52     def visit_struct(self, struct, *args, **kwargs):
53         raise NotImplementedError
54
55     def visit_array(self, array, *args, **kwargs):
56         raise NotImplementedError
57
58     def visit_blob(self, blob, *args, **kwargs):
59         raise NotImplementedError
60
61     def visit_enum(self, enum, *args, **kwargs):
62         raise NotImplementedError
63
64     def visit_bitmask(self, bitmask, *args, **kwargs):
65         raise NotImplementedError
66
67     def visit_pointer(self, pointer, *args, **kwargs):
68         raise NotImplementedError
69
70     def visit_handle(self, handle, *args, **kwargs):
71         raise NotImplementedError
72
73     def visit_alias(self, alias, *args, **kwargs):
74         raise NotImplementedError
75
76     def visit_opaque(self, opaque, *args, **kwargs):
77         raise NotImplementedError
78
79     def visit_interface(self, interface, *args, **kwargs):
80         raise NotImplementedError
81
82
83 class OnceVisitor(Visitor):
84
85     def __init__(self):
86         self.__visited = set()
87
88     def visit(self, type, *args, **kwargs):
89         if type not in self.__visited:
90             self.__visited.add(type)
91             return type.visit(self, *args, **kwargs)
92         return None
93
94
95 class Rebuilder(Visitor):
96
97     def visit_void(self, void):
98         return void
99
100     def visit_literal(self, literal):
101         return literal
102
103     def visit_string(self, string):
104         return string
105
106     def visit_const(self, const):
107         return Const(const.type)
108
109     def visit_struct(self, struct):
110         members = [self.visit(member) for member in struct.members]
111         return Struct(struct.name, members)
112
113     def visit_array(self, array):
114         type = self.visit(array.type)
115         return Array(type, array.length)
116
117     def visit_blob(self, blob):
118         type = self.visit(blob.type)
119         return Blob(type, blob.size)
120
121     def visit_enum(self, enum):
122         return enum
123
124     def visit_bitmask(self, bitmask):
125         type = self.visit(bitmask.type)
126         return Bitmask(type, bitmask.values)
127
128     def visit_pointer(self, pointer):
129         type = self.visit(pointer.type)
130         return Pointer(type)
131
132     def visit_handle(self, handle):
133         type = self.visit(handle.type)
134         return Handle(handle.name, type, handle.range)
135
136     def visit_alias(self, alias):
137         type = self.visit(alias.type)
138         return Alias(alias.expr, type)
139
140     def visit_opaque(self, opaque):
141         return opaque
142
143
144 class Type:
145
146     __seq = 0
147
148     def __init__(self, expr, id = ''):
149         self.expr = expr
150         
151         for char in id:
152             assert char.isalnum() or char in '_ '
153
154         id = id.replace(' ', '_')
155         
156         if id in all_types:
157             Type.__seq += 1
158             id += str(Type.__seq)
159         
160         assert id not in all_types
161         all_types[id] = self
162
163         self.id = id
164
165     def __str__(self):
166         return self.expr
167
168     def visit(self, visitor, *args, **kwargs):
169         raise NotImplementedError
170
171
172
173 class _Void(Type):
174
175     def __init__(self):
176         Type.__init__(self, "void")
177
178     def visit(self, visitor, *args, **kwargs):
179         return visitor.visit_void(self, *args, **kwargs)
180
181 Void = _Void()
182
183
184 class Concrete(Type):
185
186     def decl(self):
187         print 'static void Dump%s(const %s &value);' % (self.id, self.expr)
188     
189     def impl(self):
190         print 'static void Dump%s(const %s &value) {' % (self.id, self.expr)
191         self._dump("value");
192         print '}'
193         print
194     
195     def _dump(self, instance):
196         raise NotImplementedError
197     
198     def dump(self, instance):
199         print '    Dump%s(%s);' % (self.id, instance)
200     
201
202 class Literal(Type):
203
204     def __init__(self, expr, format, base=10):
205         Type.__init__(self, expr)
206         self.format = format
207
208     def visit(self, visitor, *args, **kwargs):
209         return visitor.visit_literal(self, *args, **kwargs)
210
211
212 class Const(Type):
213
214     def __init__(self, type):
215
216         if type.expr.startswith("const "):
217             expr = type.expr + " const"
218         else:
219             expr = "const " + type.expr
220
221         Type.__init__(self, expr, 'C' + type.id)
222
223         self.type = type
224
225     def visit(self, visitor, *args, **kwargs):
226         return visitor.visit_const(self, *args, **kwargs)
227
228
229 class Pointer(Type):
230
231     def __init__(self, type):
232         Type.__init__(self, type.expr + " *", 'P' + type.id)
233         self.type = type
234
235     def visit(self, visitor, *args, **kwargs):
236         return visitor.visit_pointer(self, *args, **kwargs)
237
238
239 class Handle(Type):
240
241     def __init__(self, name, type, range=None):
242         Type.__init__(self, type.expr, 'P' + type.id)
243         self.name = name
244         self.type = type
245         self.range = range
246
247     def visit(self, visitor, *args, **kwargs):
248         return visitor.visit_handle(self, *args, **kwargs)
249
250
251 def ConstPointer(type):
252     return Pointer(Const(type))
253
254
255 class Enum(Concrete):
256
257     def __init__(self, name, values):
258         Concrete.__init__(self, name)
259         self.values = values
260     
261     def visit(self, visitor, *args, **kwargs):
262         return visitor.visit_enum(self, *args, **kwargs)
263
264
265 def FakeEnum(type, values):
266     return Enum(type.expr, values)
267
268
269 class Bitmask(Concrete):
270
271     def __init__(self, type, values):
272         Concrete.__init__(self, type.expr)
273         self.type = type
274         self.values = values
275
276     def visit(self, visitor, *args, **kwargs):
277         return visitor.visit_bitmask(self, *args, **kwargs)
278
279 Flags = Bitmask
280
281
282 class Array(Type):
283
284     def __init__(self, type, length):
285         Type.__init__(self, type.expr + " *")
286         self.type = type
287         self.length = length
288
289     def visit(self, visitor, *args, **kwargs):
290         return visitor.visit_array(self, *args, **kwargs)
291
292
293 class Blob(Type):
294
295     def __init__(self, type, size):
296         Type.__init__(self, type.expr + ' *')
297         self.type = type
298         self.size = size
299
300     def visit(self, visitor, *args, **kwargs):
301         return visitor.visit_blob(self, *args, **kwargs)
302
303
304 class Struct(Concrete):
305
306     def __init__(self, name, members):
307         Concrete.__init__(self, name)
308         self.name = name
309         self.members = members
310
311     def visit(self, visitor, *args, **kwargs):
312         return visitor.visit_struct(self, *args, **kwargs)
313
314
315 class Alias(Type):
316
317     def __init__(self, expr, type):
318         Type.__init__(self, expr)
319         self.type = type
320
321     def visit(self, visitor, *args, **kwargs):
322         return visitor.visit_alias(self, *args, **kwargs)
323
324
325 def Out(type, name):
326     arg = Arg(type, name, output=True)
327     return arg
328
329
330 class Arg:
331
332     def __init__(self, type, name, output=False):
333         self.type = type
334         self.name = name
335         self.output = output
336         self.index = None
337
338     def __str__(self):
339         return '%s %s' % (self.type, self.name)
340
341
342 class Function:
343
344     __id = 0
345
346     def __init__(self, type, name, args, call = '', fail = None, sideeffects=True, hidden=False):
347         self.id = Function.__id
348         Function.__id += 1
349
350         self.type = type
351         self.name = name
352
353         self.args = []
354         index = 0
355         for arg in args:
356             if isinstance(arg, tuple):
357                 arg_type, arg_name = arg
358                 arg = Arg(arg_type, arg_name)
359             arg.index = index
360             index += 1
361             self.args.append(arg)
362
363         self.call = call
364         self.fail = fail
365         self.sideeffects = sideeffects
366         self.hidden = False
367
368     def prototype(self, name=None):
369         if name is not None:
370             name = name.strip()
371         else:
372             name = self.name
373         s = name
374         if self.call:
375             s = self.call + ' ' + s
376         if name.startswith('*'):
377             s = '(' + s + ')'
378         s = self.type.expr + ' ' + s
379         s += "("
380         if self.args:
381             s += ", ".join(["%s %s" % (arg.type, arg.name) for arg in self.args])
382         else:
383             s += "void"
384         s += ")"
385         return s
386
387
388 def StdFunction(*args, **kwargs):
389     kwargs.setdefault('call', '__stdcall')
390     return Function(*args, **kwargs)
391
392
393 def FunctionPointer(type, name, args, **kwargs):
394     # XXX
395     return Opaque(name)
396
397
398 class Interface(Type):
399
400     def __init__(self, name, base=None):
401         Type.__init__(self, name)
402         self.name = name
403         self.base = base
404         self.methods = []
405
406     def itermethods(self):
407         if self.base is not None:
408             for method in self.stdapi.itermethods():
409                 yield method
410         for method in self.methods:
411             yield method
412         raise StopIteration
413
414
415 class Method(Function):
416
417     def __init__(self, type, name, args):
418         Function.__init__(self, type, name, args, call = '__stdcall')
419
420
421 towrap = []
422
423
424 def WrapPointer(type):
425     return Pointer(type)
426
427
428 class String(Type):
429
430     def __init__(self, expr = "char *", length = None):
431         Type.__init__(self, expr)
432         self.length = length
433
434     def visit(self, visitor, *args, **kwargs):
435         return visitor.visit_string(self, *args, **kwargs)
436
437 CString = String()
438
439
440 class Opaque(Type):
441     '''Opaque pointer.'''
442
443     def __init__(self, expr):
444         Type.__init__(self, expr)
445
446     def visit(self, visitor, *args, **kwargs):
447         return visitor.visit_opaque(self, *args, **kwargs)
448
449
450 def OpaquePointer(type, *args):
451     return Opaque(type.expr + ' *')
452
453 def OpaqueArray(type, size):
454     return Opaque(type.expr + ' *')
455
456 def OpaqueBlob(type, size):
457     return Opaque(type.expr + ' *')
458
459
460 class Collector(Visitor):
461     '''Collect.'''
462
463     def __init__(self):
464         self.__visited = set()
465         self.types = []
466
467     def visit(self, type):
468         if type in self.__visited:
469             return
470         self.__visited.add(type)
471         Visitor.visit(self, type)
472         self.types.append(type)
473
474     def visit_void(self, literal):
475         pass
476
477     def visit_literal(self, literal):
478         pass
479
480     def visit_string(self, string):
481         pass
482
483     def visit_const(self, const):
484         self.visit(const.type)
485
486     def visit_struct(self, struct):
487         for type, name in struct.members:
488             self.visit(type)
489
490     def visit_array(self, array):
491         self.visit(array.type)
492
493     def visit_blob(self, array):
494         pass
495
496     def visit_enum(self, enum):
497         pass
498
499     def visit_bitmask(self, bitmask):
500         self.visit(bitmask.type)
501
502     def visit_pointer(self, pointer):
503         self.visit(pointer.type)
504
505     def visit_handle(self, handle):
506         self.visit(handle.type)
507
508     def visit_alias(self, alias):
509         self.visit(alias.type)
510
511     def visit_opaque(self, opaque):
512         pass
513
514     def visit_interface(self, interface):
515         pass
516
517
518 class API:
519
520     def __init__(self, name):
521         self.name = name
522         self.headers = []
523         self.functions = []
524         self.interfaces = []
525
526     def all_types(self):
527         collector = Collector()
528         for function in self.functions:
529             for arg in function.args:
530                 collector.visit(arg.type)
531             collector.visit(function.type)
532         for interface in self.interfaces:
533             collector.visit(interface)
534             for method in interface.methods:
535                 for arg in method.args:
536                     collector.visit(arg.type)
537                 collector.visit(method.type)
538         return collector.types
539
540     def add_function(self, function):
541         self.functions.append(function)
542
543     def add_functions(self, functions):
544         for function in functions:
545             self.add_function(function)
546
547     def add_interface(self, interface):
548         self.interfaces.append(interface)
549
550     def add_interfaces(self, interfaces):
551         self.interfaces.extend(interfaces)
552
553
554 Bool = Literal("bool", "Bool")
555 SChar = Literal("signed char", "SInt")
556 UChar = Literal("unsigned char", "UInt")
557 Short = Literal("short", "SInt")
558 Int = Literal("int", "SInt")
559 Long = Literal("long", "SInt")
560 LongLong = Literal("long long", "SInt")
561 UShort = Literal("unsigned short", "UInt")
562 UInt = Literal("unsigned int", "UInt")
563 ULong = Literal("unsigned long", "UInt")
564 ULongLong = Literal("unsigned long long", "UInt")
565 Float = Literal("float", "Float")
566 Double = Literal("double", "Float")
567 SizeT = Literal("size_t", "UInt")
568 WString = Literal("wchar_t *", "WString")
569
570 Int8 = Literal("int8_t", "SInt")
571 UInt8 = Literal("uint8_t", "UInt")
572 Int16 = Literal("int16_t", "SInt")
573 UInt16 = Literal("uint16_t", "UInt")
574 Int32 = Literal("int32_t", "SInt")
575 UInt32 = Literal("uint32_t", "UInt")
576 Int64 = Literal("int64_t", "SInt")
577 UInt64 = Literal("uint64_t", "UInt")