]> git.cworth.org Git - apitrace/blob - base.py
1637128ddc77fa7a15cd923e16dc1db8ab525bab
[apitrace] / base.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)
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):
242         Type.__init__(self, type.expr, 'P' + type.id)
243         self.name = name
244         self.type = type
245
246     def visit(self, visitor, *args, **kwargs):
247         return visitor.visit_handle(self, *args, **kwargs)
248
249
250 def ConstPointer(type):
251     return Pointer(Const(type))
252
253
254 class Enum(Concrete):
255
256     def __init__(self, name, values):
257         Concrete.__init__(self, name)
258         self.values = values
259     
260     def visit(self, visitor, *args, **kwargs):
261         return visitor.visit_enum(self, *args, **kwargs)
262
263
264 def FakeEnum(type, values):
265     return Enum(type.expr, values)
266
267
268 class Bitmask(Concrete):
269
270     def __init__(self, type, values):
271         Concrete.__init__(self, type.expr)
272         self.type = type
273         self.values = values
274
275     def visit(self, visitor, *args, **kwargs):
276         return visitor.visit_bitmask(self, *args, **kwargs)
277
278 Flags = Bitmask
279
280
281 class Array(Type):
282
283     def __init__(self, type, length):
284         Type.__init__(self, type.expr + " *")
285         self.type = type
286         self.length = length
287
288     def visit(self, visitor, *args, **kwargs):
289         return visitor.visit_array(self, *args, **kwargs)
290
291
292 class Blob(Type):
293
294     def __init__(self, type, size):
295         Type.__init__(self, type.expr + ' *')
296         self.type = type
297         self.size = size
298
299     def visit(self, visitor, *args, **kwargs):
300         return visitor.visit_blob(self, *args, **kwargs)
301
302
303 class Struct(Concrete):
304
305     def __init__(self, name, members):
306         Concrete.__init__(self, name)
307         self.name = name
308         self.members = members
309
310     def visit(self, visitor, *args, **kwargs):
311         return visitor.visit_struct(self, *args, **kwargs)
312
313
314 class Alias(Type):
315
316     def __init__(self, expr, type):
317         Type.__init__(self, expr)
318         self.type = type
319
320     def visit(self, visitor, *args, **kwargs):
321         return visitor.visit_alias(self, *args, **kwargs)
322
323
324 def Out(type, name):
325     arg = Arg(type, name, output=True)
326     return arg
327
328
329 class Arg:
330
331     def __init__(self, type, name, output=False):
332         self.type = type
333         self.name = name
334         self.output = output
335
336     def __str__(self):
337         return '%s %s' % (self.type, self.name)
338
339
340 class Function:
341
342     def __init__(self, type, name, args, call = '', fail = None, sideeffects=True, hidden=False):
343         self.type = type
344         self.name = name
345
346         self.args = []
347         for arg in args:
348             if isinstance(arg, tuple):
349                 arg_type, arg_name = arg
350                 arg = Arg(arg_type, arg_name)
351             self.args.append(arg)
352
353         self.call = call
354         self.fail = fail
355         self.sideeffects = sideeffects
356         self.hidden = False
357
358     def prototype(self, name=None):
359         if name is not None:
360             name = name.strip()
361         else:
362             name = self.name
363         s = name
364         if self.call:
365             s = self.call + ' ' + s
366         if name.startswith('*'):
367             s = '(' + s + ')'
368         s = self.type.expr + ' ' + s
369         s += "("
370         if self.args:
371             s += ", ".join(["%s %s" % (arg.type, arg.name) for arg in self.args])
372         else:
373             s += "void"
374         s += ")"
375         return s
376
377
378 def StdFunction(*args, **kwargs):
379     kwargs.setdefault('call', 'GLAPIENTRY')
380     return Function(*args, **kwargs)
381
382
383 def FunctionPointer(type, name, args, **kwargs):
384     # XXX
385     return Opaque(name)
386
387
388 class Interface(Type):
389
390     def __init__(self, name, base=None):
391         Type.__init__(self, name)
392         self.name = name
393         self.base = base
394         self.methods = []
395
396     def itermethods(self):
397         if self.base is not None:
398             for method in self.base.itermethods():
399                 yield method
400         for method in self.methods:
401             yield method
402         raise StopIteration
403
404
405 class Method(Function):
406
407     def __init__(self, type, name, args):
408         Function.__init__(self, type, name, args, call = '__stdcall')
409
410
411 towrap = []
412
413
414 def WrapPointer(type):
415     return Pointer(type)
416
417
418 class String(Type):
419
420     def __init__(self, expr = "char *", length = None):
421         Type.__init__(self, expr)
422         self.length = length
423
424     def visit(self, visitor, *args, **kwargs):
425         return visitor.visit_string(self, *args, **kwargs)
426
427 CString = String()
428
429
430 class Opaque(Type):
431     '''Opaque pointer.'''
432
433     def __init__(self, expr):
434         Type.__init__(self, expr)
435
436     def visit(self, visitor, *args, **kwargs):
437         return visitor.visit_opaque(self, *args, **kwargs)
438
439
440 def OpaquePointer(type):
441     return Opaque(type.expr + ' *')
442
443
444 class Collector(Visitor):
445     '''Collect.'''
446
447     def __init__(self):
448         self.__visited = set()
449         self.types = []
450
451     def visit(self, type):
452         if type in self.__visited:
453             return
454         self.__visited.add(type)
455         Visitor.visit(self, type)
456         self.types.append(type)
457
458     def visit_void(self, literal):
459         pass
460
461     def visit_literal(self, literal):
462         pass
463
464     def visit_string(self, string):
465         pass
466
467     def visit_const(self, const):
468         self.visit(const.type)
469
470     def visit_struct(self, struct):
471         for type, name in struct.members:
472             self.visit(type)
473
474     def visit_array(self, array):
475         self.visit(array.type)
476
477     def visit_blob(self, array):
478         pass
479
480     def visit_enum(self, enum):
481         pass
482
483     def visit_bitmask(self, bitmask):
484         self.visit(bitmask.type)
485
486     def visit_pointer(self, pointer):
487         self.visit(pointer.type)
488
489     def visit_handle(self, handle):
490         self.visit(handle.type)
491
492     def visit_alias(self, alias):
493         self.visit(alias.type)
494
495     def visit_opaque(self, opaque):
496         pass
497
498     def visit_interface(self, interface):
499         pass
500
501
502 class API:
503
504     def __init__(self, name):
505         self.name = name
506         self.headers = []
507         self.functions = []
508         self.interfaces = []
509
510     def all_types(self):
511         collector = Collector()
512         for function in self.functions:
513             for arg in function.args:
514                 collector.visit(arg.type)
515             collector.visit(function.type)
516         for interface in self.interfaces:
517             collector.visit(interface)
518             for method in interface.methods:
519                 for arg in method.args:
520                     collector.visit(arg.type)
521                 collector.visit(method.type)
522         return collector.types
523
524     def add_function(self, function):
525         self.functions.append(function)
526
527     def add_functions(self, functions):
528         for function in functions:
529             self.add_function(function)
530
531     def add_interface(self, interface):
532         self.interfaces.append(interface)
533
534     def add_interfaces(self, interfaces):
535         self.interfaces.extend(interfaces)
536
537
538 Bool = Literal("bool", "Bool")
539 SChar = Literal("signed char", "SInt")
540 UChar = Literal("unsigned char", "UInt")
541 Short = Literal("short", "SInt")
542 Int = Literal("int", "SInt")
543 Long = Literal("long", "SInt")
544 LongLong = Literal("long long", "SInt")
545 UShort = Literal("unsigned short", "UInt")
546 UInt = Literal("unsigned int", "UInt")
547 ULong = Literal("unsigned long", "UInt")
548 ULongLong = Literal("unsigned long long", "UInt")
549 Float = Literal("float", "Float")
550 Double = Literal("double", "Float")
551 SizeT = Literal("size_t", "UInt")
552 WString = Literal("wchar_t *", "WString")
553