]> git.cworth.org Git - apitrace/blob - base.py
Basic support for tracing d3d7.
[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'):
228         self.type = type
229         self.name = name
230         self.args = args
231         self.call = call
232
233     def prototype(self, name=None):
234         if name is not None:
235             name = name.strip()
236         else:
237             name = self.name
238         s = name
239         if self.call:
240             s = self.call + ' ' + s
241         if name.startswith('*'):
242             s = '(' + s + ')'
243         s = str(self.type) + ' ' + s
244         s += "("
245         if self.args:
246             s += ", ".join(["%s %s" % (type, name) for type, name in self.args])
247         else:
248             s += "void"
249         s += ")"
250         return s
251
252
253 class Interface(Type):
254
255     def __init__(self, name, base=None):
256         Type.__init__(self, name)
257         self.base = base
258         self.methods = []
259
260     def itermethods(self):
261         if self.base is not None:
262             for method in self.base.itermethods():
263                 yield method
264         for method in self.methods:
265             yield method
266         raise StopIteration
267
268     def wrap_name(self):
269         return "Wrap" + self.name
270
271     def wrap_pre_decl(self):
272         print "class %s;" % self.wrap_name()
273
274     def wrap_decl(self):
275         print "class %s : public %s " % (self.wrap_name(), self.name)
276         print "{"
277         print "public:"
278         print "    %s(%s * pInstance);" % (self.wrap_name(), self.name)
279         print "    virtual ~%s();" % self.wrap_name()
280         print
281         for method in self.itermethods():
282             print "    " + method.prototype() + ";"
283         print
284         #print "private:"
285         print "    %s * m_pInstance;" % (self.name,)
286         print "};"
287         print
288
289     def wrap_impl(self):
290         print '%s::%s(%s * pInstance) {' % (self.wrap_name(), self.wrap_name(), self.name)
291         print '    m_pInstance = pInstance;'
292         print '}'
293         print
294         print '%s::~%s() {' % (self.wrap_name(), self.wrap_name())
295         print '}'
296         print
297         for method in self.itermethods():
298             print method.prototype(self.wrap_name() + '::' + method.name) + ' {'
299             if method.type is Void:
300                 result = ''
301             else:
302                 print '    %s result;' % method.type
303                 result = 'result = '
304             print '    Log::BeginCall("%s");' % (self.name + '::' + method.name)
305             print '    Log::BeginArg("%s *", "this");' % self.name
306             print '    Log::BeginReference("%s", m_pInstance);' % self.name
307             print '    Log::EndReference();'
308             print '    Log::EndArg();'
309             for type, name in method.args:
310                 if not type.isoutput():
311                     type.unwrap_instance(name)
312                     print '    Log::BeginArg("%s", "%s");' % (type, name)
313                     type.dump(name)
314                     print '    Log::EndArg();'
315             print '    %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(name) for type, name in method.args]))
316             for type, name in method.args:
317                 if type.isoutput():
318                     print '    Log::BeginArg("%s", "%s");' % (type, name)
319                     type.dump(name)
320                     print '    Log::EndArg();'
321                     type.wrap_instance(name)
322             if method.type is not Void:
323                 print '    Log::BeginReturn("%s");' % method.type
324                 method.type.dump("result")
325                 print '    Log::EndReturn();'
326                 method.type.wrap_instance('result')
327             print '    Log::EndCall();'
328             if method.name == 'QueryInterface':
329                 print '    if(*ppvObj == m_pInstance)'
330                 print '        *ppvObj = this;'
331             if method.name == 'Release':
332                 assert method.type is not Void
333                 print '    if(!result)'
334                 print '        delete this;'
335             if method.type is not Void:
336                 print '    return result;'
337             print '}'
338             print
339         print
340
341
342 class Method(Function):
343
344     def __init__(self, type, name, args):
345         Function.__init__(self, type, name, args)
346
347
348 towrap = []
349
350 class WrapPointer(Pointer):
351
352     def __init__(self, type):
353         Pointer.__init__(self, type)
354         if type not in towrap:
355             towrap.append(type)
356
357     def wrap_instance(self, instance):
358         print "    if(%s)" % instance
359         print "        %s = new %s(%s);" % (instance, self.type.wrap_name(), instance)
360
361     def unwrap_instance(self, instance):
362         print "    if(%s)" % instance
363         print "        %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, self.type.wrap_name(), instance)
364
365
366 class _String(Type):
367
368     def __init__(self):
369         Type.__init__(self, "String")
370
371     def __str__(self):
372         return "char *"
373
374     def dump(self, instance):
375         print '    Log::DumpString((const char *)%s);' % instance
376
377 String = _String()
378
379
380 SChar = Intrinsic("signed char", "%i")
381 UChar = Intrinsic("unsigned char", "%u")
382 Short = Intrinsic("short", "%i")
383 Int = Intrinsic("int", "%i")
384 Long = Intrinsic("long", "%li")
385 UShort = Intrinsic("unsigned short", "%u")
386 UInt = Intrinsic("unsigned int", "%u")
387 ULong = Intrinsic("unsigned long", "%lu")
388 Float = Intrinsic("float", "%f")
389 Double = Intrinsic("double", "%f")
390
391
392 def wrap():
393     for type in all_types.itervalues():
394         type.decl()
395     print
396     for type in all_types.itervalues():
397         type.impl()
398     print
399     for type in towrap:
400         type.wrap_pre_decl()
401     print
402     for type in towrap:
403         type.wrap_decl()
404     print
405     for type in towrap:
406         type.wrap_impl()
407     print