]> git.cworth.org Git - apitrace/blob - base.py
Try to cope with missing functions.
[apitrace] / base.py
1 #############################################################################
2 #
3 # Copyright 2008 Tungsten Graphics, Inc.
4 #
5 # This program is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License as published
7 # by the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18 #############################################################################
19
20 """C basic types"""
21
22
23 import debug
24
25
26 all_types = {}
27
28 class Type:
29
30     def __init__(self, name):
31         self.name = name
32
33     def __str__(self):
34         return self.name
35
36     def identifier(self):
37         return self.name.replace(' ', '_')
38
39     def isoutput(self):
40         return False
41
42     def decl(self):
43         pass
44
45     def impl(self):
46         pass
47
48     def dump(self, instance):
49         raise NotImplementedError
50     
51     def wrap_instance(self, instance):
52         pass 
53
54     def unwrap_instance(self, instance):
55         pass
56
57
58 class _Void(Type):
59
60     def __init__(self):
61         Type.__init__(self, "void")
62
63 Void = _Void()
64
65
66 class Concrete(Type):
67
68     def __init__(self, name):
69         for char in name:
70             assert char.isalnum() or char in '_ '
71
72         Type.__init__(self, name)
73         
74         assert self.name not in all_types
75         if self.name not in all_types:
76             all_types[self.name] = self
77
78     def decl(self):
79         print 'void Dump%s(const %s &value);' % (self.identifier(), str(self))
80     
81     def impl(self):
82         print 'void Dump%s(const %s &value) {' % (self.identifier(), str(self))
83         self._dump("value");
84         print '}'
85         print
86     
87     def _dump(self, instance):
88         raise NotImplementedError
89     
90     def dump(self, instance):
91         print '    Dump%s(%s);' % (self.identifier(), instance)
92     
93
94 class Intrinsic(Concrete):
95
96     def __init__(self, expr, format, name = None):
97         if name is None:
98             name = expr
99         Concrete.__init__(self, name)
100         self.expr = expr
101         self.format = format
102
103     def _dump(self, instance):
104         print '    Log::TextF("%s", %s);' % (self.format, instance)
105         
106     def __str__(self):
107         return self.expr
108
109
110 class Const(Type):
111
112     def __init__(self, type):
113         Type.__init__(self, 'C' + type.name)
114         self.type = type
115
116     def dump(self, instance):
117         self.type.dump(instance)
118
119     def __str__(self):
120         return "const " + str(self.type)
121
122
123 class Pointer(Type):
124
125     def __init__(self, type):
126         Type.__init__(self, 'P' + type.name)
127         self.type = type
128
129     def __str__(self):
130         return str(self.type) + " *"
131     
132     def dump(self, instance):
133         print '    if(%s) {' % instance
134         print '        Log::BeginReference("%s", %s);' % (self.type, instance)
135         try:
136             self.type.dump("*" + instance)
137         except NotImplementedError:
138             pass
139         print '        Log::EndReference();'
140         print '    }'
141         print '    else'
142         print '        Log::Text("NULL");'
143
144     def wrap_instance(self, instance):
145         self.type.wrap_instance("*" + instance)
146
147     def unwrap_instance(self, instance):
148         self.type.wrap_instance("*" + instance)
149
150
151 def ConstPointer(type):
152     return Pointer(Const(type))
153
154
155 class OutPointer(Pointer):
156
157     def isoutput(self):
158         return True
159
160
161 class Enum(Concrete):
162
163     def __init__(self, name, values):
164         Concrete.__init__(self, name)
165         self.values = values
166     
167     def _dump(self, instance):
168         print '    switch(%s) {' % instance
169         for value in self.values:
170             print '    case %s:' % value
171             print '        Log::Text("%s");' % value
172             print '        break;'
173         print '    default:'
174         print '        Log::TextF("%%i", %s);' % instance
175         print '        break;'
176         print '    }'
177
178
179 class Flags(Concrete):
180
181     __seq = 0
182     
183     def __init__(self, type, values):
184         Flags.__seq += 1
185         Concrete.__init__(self, type.name + str(Flags.__seq))
186         self.type = type
187         self.values = values
188
189     def __str__(self):
190         return str(self.type)
191     
192     def _dump(self, instance):
193         print '    %s l_Value = %s;' % (self.type, instance)
194         for value in self.values:
195             print '    if((l_Value & %s) == %s) {' % (value, value)
196             print '        Log::Text("%s | ");' % value
197             print '        l_Value &= ~%s;' % value
198             print '    }'
199         self.type.dump("l_Value");
200
201
202 class Struct(Concrete):
203
204     def __init__(self, name, members):
205         Concrete.__init__(self, name)
206         self.members = members
207
208     def _dump(self, instance):
209         for type, name in self.members:
210             print '    Log::BeginElement("%s", "%s");' % (type, name)
211             type.dump('(%s).%s' % (instance, name))
212             print '    Log::EndElement();'
213
214
215 class Alias(Type):
216
217     def __init__(self, name, type):
218         Type.__init__(self, name)
219         self.type = type
220
221     def dump(self, instance):
222         self.type.dump(instance)
223
224
225 class Function:
226
227     def __init__(self, type, name, args, call = '__stdcall', fail = None):
228         self.type = type
229         self.name = name
230         self.args = args
231         self.call = call
232         self.fail = fail
233
234     def prototype(self, name=None):
235         if name is not None:
236             name = name.strip()
237         else:
238             name = self.name
239         s = name
240         if self.call:
241             s = self.call + ' ' + s
242         if name.startswith('*'):
243             s = '(' + s + ')'
244         s = str(self.type) + ' ' + s
245         s += "("
246         if self.args:
247             s += ", ".join(["%s %s" % (type, name) for type, name in self.args])
248         else:
249             s += "void"
250         s += ")"
251         return s
252
253
254 class Interface(Type):
255
256     def __init__(self, name, base=None):
257         Type.__init__(self, name)
258         self.base = base
259         self.methods = []
260
261     def itermethods(self):
262         if self.base is not None:
263             for method in self.base.itermethods():
264                 yield method
265         for method in self.methods:
266             yield method
267         raise StopIteration
268
269     def wrap_name(self):
270         return "Wrap" + self.name
271
272     def wrap_pre_decl(self):
273         print "class %s;" % self.wrap_name()
274
275     def wrap_decl(self):
276         print "class %s : public %s " % (self.wrap_name(), self.name)
277         print "{"
278         print "public:"
279         print "    %s(%s * pInstance);" % (self.wrap_name(), self.name)
280         print "    virtual ~%s();" % self.wrap_name()
281         print
282         for method in self.itermethods():
283             print "    " + method.prototype() + ";"
284         print
285         #print "private:"
286         print "    %s * m_pInstance;" % (self.name,)
287         print "};"
288         print
289
290     def wrap_impl(self):
291         print '%s::%s(%s * pInstance) {' % (self.wrap_name(), self.wrap_name(), self.name)
292         print '    m_pInstance = pInstance;'
293         print '}'
294         print
295         print '%s::~%s() {' % (self.wrap_name(), self.wrap_name())
296         print '}'
297         print
298         for method in self.itermethods():
299             print method.prototype(self.wrap_name() + '::' + method.name) + ' {'
300             if method.type is Void:
301                 result = ''
302             else:
303                 print '    %s result;' % method.type
304                 result = 'result = '
305             print '    Log::BeginCall("%s");' % (self.name + '::' + method.name)
306             print '    Log::BeginArg("%s *", "this");' % self.name
307             print '    Log::BeginReference("%s", m_pInstance);' % self.name
308             print '    Log::EndReference();'
309             print '    Log::EndArg();'
310             for type, name in method.args:
311                 if not type.isoutput():
312                     type.unwrap_instance(name)
313                     print '    Log::BeginArg("%s", "%s");' % (type, name)
314                     type.dump(name)
315                     print '    Log::EndArg();'
316             print '    %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(name) for type, name in method.args]))
317             for type, name in method.args:
318                 if type.isoutput():
319                     print '    Log::BeginArg("%s", "%s");' % (type, name)
320                     type.dump(name)
321                     print '    Log::EndArg();'
322                     type.wrap_instance(name)
323             if method.type is not Void:
324                 print '    Log::BeginReturn("%s");' % method.type
325                 method.type.dump("result")
326                 print '    Log::EndReturn();'
327                 method.type.wrap_instance('result')
328             print '    Log::EndCall();'
329             if method.name == 'QueryInterface':
330                 print '    if(*ppvObj == m_pInstance)'
331                 print '        *ppvObj = this;'
332             if method.name == 'Release':
333                 assert method.type is not Void
334                 print '    if(!result)'
335                 print '        delete this;'
336             if method.type is not Void:
337                 print '    return result;'
338             print '}'
339             print
340         print
341
342
343 class Method(Function):
344
345     def __init__(self, type, name, args):
346         Function.__init__(self, type, name, args)
347
348
349 towrap = []
350
351 class WrapPointer(Pointer):
352
353     def __init__(self, type):
354         Pointer.__init__(self, type)
355         if type not in towrap:
356             towrap.append(type)
357
358     def wrap_instance(self, instance):
359         print "    if(%s)" % instance
360         print "        %s = new %s(%s);" % (instance, self.type.wrap_name(), instance)
361
362     def unwrap_instance(self, instance):
363         print "    if(%s)" % instance
364         print "        %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, self.type.wrap_name(), instance)
365
366
367 class _String(Type):
368
369     def __init__(self):
370         Type.__init__(self, "String")
371
372     def __str__(self):
373         return "char *"
374
375     def dump(self, instance):
376         print '    Log::DumpString((const char *)%s);' % instance
377
378 String = _String()
379
380
381 SChar = Intrinsic("signed char", "%i")
382 UChar = Intrinsic("unsigned char", "%u")
383 Short = Intrinsic("short", "%i")
384 Int = Intrinsic("int", "%i")
385 Long = Intrinsic("long", "%li")
386 UShort = Intrinsic("unsigned short", "%u")
387 UInt = Intrinsic("unsigned int", "%u")
388 ULong = Intrinsic("unsigned long", "%lu")
389 Float = Intrinsic("float", "%f")
390 Double = Intrinsic("double", "%f")
391
392
393 def wrap():
394     for type in all_types.itervalues():
395         type.decl()
396     print
397     for type in all_types.itervalues():
398         type.impl()
399     print
400     for type in towrap:
401         type.wrap_pre_decl()
402     print
403     for type in towrap:
404         type.wrap_decl()
405     print
406     for type in towrap:
407         type.wrap_impl()
408     print