]> git.cworth.org Git - apitrace/blob - base.py
Major move to visitor paradigm.
[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_alias(self, alias, *args, **kwargs):
71         raise NotImplementedError
72
73     def visit_opaque(self, opaque, *args, **kwargs):
74         raise NotImplementedError
75
76     def visit_interface(self, interface, *args, **kwargs):
77         raise NotImplementedError
78
79
80 class OnceVisitor(Visitor):
81
82     def __init__(self):
83         self.__visited = set()
84
85     def visit(self, type, *args, **kwargs):
86         if type not in self.__visited:
87             self.__visited.add(type)
88             return type.visit(self, *args, **kwargs)
89         return None
90
91
92 class Rebuilder(Visitor):
93
94     def visit_void(self, void):
95         return void
96
97     def visit_literal(self, literal):
98         return literal
99
100     def visit_string(self, string):
101         return string
102
103     def visit_const(self, const):
104         return Const(const.type)
105
106     def visit_struct(self, struct):
107         members = [self.visit(member) for member in struct.members]
108         return Struct(struct.name, members)
109
110     def visit_array(self, array):
111         type = self.visit(array.type)
112         return Array(type, array.length)
113
114     def visit_blob(self, blob):
115         type = self.visit(blob.type)
116         return Blob(type, blob.size)
117
118     def visit_enum(self, enum):
119         return enum
120
121     def visit_bitmask(self, bitmask):
122         type = self.visit(bitmask.type)
123         return Bitmask(type, bitmask.values)
124
125     def visit_pointer(self, pointer):
126         type = self.visit(pointer.type)
127         return Pointer(type)
128
129     def visit_alias(self, alias):
130         type = self.visit(alias.type)
131         return Alias(alias.expr, type)
132
133     def visit_opaque(self, opaque):
134         return opaque
135
136
137 class Type:
138
139     __seq = 0
140
141     def __init__(self, expr, id = ''):
142         self.expr = expr
143         
144         for char in id:
145             assert char.isalnum() or char in '_ '
146
147         id = id.replace(' ', '_')
148         
149         if id in all_types:
150             Type.__seq += 1
151             id += str(Type.__seq)
152         
153         assert id not in all_types
154         all_types[id] = self
155
156         self.id = id
157
158     def __str__(self):
159         return self.expr
160
161     def visit(self, visitor, *args, **kwargs):
162         raise NotImplementedError
163
164
165
166 class _Void(Type):
167
168     def __init__(self):
169         Type.__init__(self, "void")
170
171     def visit(self, visitor, *args, **kwargs):
172         return visitor.visit_void(self, *args, **kwargs)
173
174 Void = _Void()
175
176
177 class Concrete(Type):
178
179     def decl(self):
180         print 'static void Dump%s(const %s &value);' % (self.id, self.expr)
181     
182     def impl(self):
183         print 'static void Dump%s(const %s &value) {' % (self.id, self.expr)
184         self._dump("value");
185         print '}'
186         print
187     
188     def _dump(self, instance):
189         raise NotImplementedError
190     
191     def dump(self, instance):
192         print '    Dump%s(%s);' % (self.id, instance)
193     
194
195 class Literal(Type):
196
197     def __init__(self, expr, format, base=10):
198         Type.__init__(self, expr)
199         self.format = format
200
201     def visit(self, visitor, *args, **kwargs):
202         return visitor.visit_literal(self, *args, **kwargs)
203
204
205 class Const(Type):
206
207     def __init__(self, type):
208
209         if type.expr.startswith("const "):
210             expr = type.expr + " const"
211         else:
212             expr = "const " + type.expr
213
214         Type.__init__(self, expr, 'C' + type.id)
215
216         self.type = type
217
218     def visit(self, visitor, *args, **kwargs):
219         return visitor.visit_const(self, *args, **kwargs)
220
221
222 class Pointer(Type):
223
224     def __init__(self, type):
225         Type.__init__(self, type.expr + " *", 'P' + type.id)
226         self.type = type
227
228     def visit(self, visitor, *args, **kwargs):
229         return visitor.visit_pointer(self, *args, **kwargs)
230
231
232 def ConstPointer(type):
233     return Pointer(Const(type))
234
235
236 class Enum(Concrete):
237
238     def __init__(self, name, values):
239         Concrete.__init__(self, name)
240         self.values = values
241     
242     def visit(self, visitor, *args, **kwargs):
243         return visitor.visit_enum(self, *args, **kwargs)
244
245
246 def FakeEnum(type, values):
247     return Enum(type.expr, values)
248
249
250 class Bitmask(Concrete):
251
252     def __init__(self, type, values):
253         Concrete.__init__(self, type.expr)
254         self.type = type
255         self.values = values
256
257     def visit(self, visitor, *args, **kwargs):
258         return visitor.visit_bitmask(self, *args, **kwargs)
259
260 Flags = Bitmask
261
262
263 class Array(Type):
264
265     def __init__(self, type, length):
266         Type.__init__(self, type.expr + " *")
267         self.type = type
268         self.length = length
269
270     def visit(self, visitor, *args, **kwargs):
271         return visitor.visit_array(self, *args, **kwargs)
272
273
274 class Blob(Type):
275
276     def __init__(self, type, size):
277         Type.__init__(self, type.expr + ' *')
278         self.type = type
279         self.size = size
280
281     def visit(self, visitor, *args, **kwargs):
282         return visitor.visit_blob(self, *args, **kwargs)
283
284
285 class Struct(Concrete):
286
287     def __init__(self, name, members):
288         Concrete.__init__(self, name)
289         self.name = name
290         self.members = members
291
292     def visit(self, visitor, *args, **kwargs):
293         return visitor.visit_struct(self, *args, **kwargs)
294
295
296 class Alias(Type):
297
298     def __init__(self, expr, type):
299         Type.__init__(self, expr)
300         self.type = type
301
302     def visit(self, visitor, *args, **kwargs):
303         return visitor.visit_alias(self, *args, **kwargs)
304
305
306 def Out(type, name):
307     arg = Arg(type, name, output=True)
308     return arg
309
310
311 class Arg:
312
313     def __init__(self, type, name, output=False):
314         self.type = type
315         self.name = name
316         self.output = output
317
318     def __str__(self):
319         return '%s %s' % (self.type, self.name)
320
321
322 class Function:
323
324     def __init__(self, type, name, args, call = '', fail = None, sideeffects=True, hidden=False):
325         self.type = type
326         self.name = name
327
328         self.args = []
329         for arg in args:
330             if isinstance(arg, tuple):
331                 arg_type, arg_name = arg
332                 arg = Arg(arg_type, arg_name)
333             self.args.append(arg)
334
335         self.call = call
336         self.fail = fail
337         self.sideeffects = sideeffects
338         self.hidden = False
339
340     def prototype(self, name=None):
341         if name is not None:
342             name = name.strip()
343         else:
344             name = self.name
345         s = name
346         if self.call:
347             s = self.call + ' ' + s
348         if name.startswith('*'):
349             s = '(' + s + ')'
350         s = self.type.expr + ' ' + s
351         s += "("
352         if self.args:
353             s += ", ".join(["%s %s" % (arg.type, arg.name) for arg in self.args])
354         else:
355             s += "void"
356         s += ")"
357         return s
358
359
360 def StdFunction(*args, **kwargs):
361     kwargs.setdefault('call', 'GLAPIENTRY')
362     return Function(*args, **kwargs)
363
364
365 def FunctionPointer(type, name, args, **kwargs):
366     # XXX
367     return Opaque(name)
368
369
370 class Interface(Type):
371
372     def __init__(self, name, base=None):
373         Type.__init__(self, name)
374         self.name = name
375         self.base = base
376         self.methods = []
377
378     def itermethods(self):
379         if self.base is not None:
380             for method in self.base.itermethods():
381                 yield method
382         for method in self.methods:
383             yield method
384         raise StopIteration
385
386
387 class Method(Function):
388
389     def __init__(self, type, name, args):
390         Function.__init__(self, type, name, args, call = '__stdcall')
391
392
393 towrap = []
394
395
396 def WrapPointer(type):
397     return Pointer(type)
398
399
400 class _String(Type):
401
402     def __init__(self):
403         Type.__init__(self, "char *")
404
405     def visit(self, visitor, *args, **kwargs):
406         return visitor.visit_string(self, *args, **kwargs)
407
408 String = _String()
409
410
411 class Opaque(Type):
412     '''Opaque pointer.'''
413
414     def __init__(self, expr):
415         Type.__init__(self, expr)
416
417     def visit(self, visitor, *args, **kwargs):
418         return visitor.visit_opaque(self, *args, **kwargs)
419
420
421 def OpaquePointer(type):
422     return Opaque(type.expr + ' *')
423
424
425
426 class API:
427
428     def __init__(self, name):
429         self.name = name
430         self.headers = []
431         self.types = set()
432         self.functions = []
433         self.interfaces = []
434
435     def add_type(self, type):
436         if type not in self.types:
437             self.types.add(type)
438
439     def add_function(self, function):
440         self.functions.append(function)
441         for arg in function.args:
442             self.add_type(arg.type)
443         self.add_type(function.type)
444
445     def add_functions(self, functions):
446         for function in functions:
447             self.add_function(function)
448
449     def add_interface(self, interface):
450         self.interfaces.append(interface)
451
452     def add_interfaces(self, interfaces):
453         self.interfaces.extend(interfaces)
454
455
456 Bool = Literal("bool", "Bool")
457 SChar = Literal("signed char", "SInt")
458 UChar = Literal("unsigned char", "UInt")
459 Short = Literal("short", "SInt")
460 Int = Literal("int", "SInt")
461 Long = Literal("long", "SInt")
462 LongLong = Literal("long long", "SInt")
463 UShort = Literal("unsigned short", "UInt")
464 UInt = Literal("unsigned int", "UInt")
465 ULong = Literal("unsigned long", "UInt")
466 ULongLong = Literal("unsigned long long", "UInt")
467 Float = Literal("float", "Float")
468 Double = Literal("double", "Float")
469 SizeT = Literal("size_t", "UInt")
470 WString = Literal("wchar_t *", "WString")
471