]> git.cworth.org Git - apitrace/blob - base.py
Use separate functions for dumping.
[apitrace] / base.py
1 #############################################################################
2 #
3 # Copyright 2008 Jose Fonseca
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 isoutput(self):
37         return False
38
39     def decl(self):
40         pass
41
42     def impl(self):
43         pass
44
45     def dump(self, instance):
46         raise NotImplementedError
47     
48     def wrap_instance(self, instance):
49         pass 
50
51     def unwrap_instance(self, instance):
52         pass
53
54
55 class _Void(Type):
56
57     def __init__(self):
58         Type.__init__(self, "void")
59
60 Void = _Void()
61
62
63 class Concrete(Type):
64
65     def __init__(self, name):
66         for char in name:
67             assert char.isalnum() or char == '_'
68
69         Type.__init__(self, name)
70         
71         assert self.name not in all_types
72         if self.name not in all_types:
73             all_types[self.name] = self
74
75     def decl(self):
76         print 'void Dump%s(const %s &value);' % (self.name, str(self))
77     
78     def impl(self):
79         print 'void Dump%s(const %s &value) {' % (self.name, str(self))
80         self._dump("value");
81         print '}'
82         print
83     
84     def _dump(self, instance):
85         raise NotImplementedError
86     
87     def dump(self, instance):
88         print '    Dump%s(%s);' % (self.name, instance)
89     
90
91 class Intrinsic(Concrete):
92
93     def __init__(self, expr, format, name = None):
94         if name is None:
95             name = expr
96         Concrete.__init__(self, name)
97         self.expr = expr
98         self.format = format
99
100     def _dump(self, instance):
101         print '    Log::TextF("%s", %s);' % (self.format, instance)
102         
103     def __str__(self):
104         return self.expr
105
106
107 class Const(Type):
108
109     def __init__(self, type):
110         Type.__init__(self, 'C' + type.name)
111         self.type = type
112
113     def dump(self, instance):
114         self.type.dump(instance)
115
116     def __str__(self):
117         return "const " + str(self.type)
118
119
120 class Pointer(Type):
121
122     def __init__(self, type):
123         Type.__init__(self, 'P' + type.name)
124         self.type = type
125
126     def __str__(self):
127         return str(self.type) + " *"
128     
129     def dump(self, instance):
130         print '    if(%s) {' % instance
131         try:
132             self.type.dump("*" + instance)
133         except NotImplementedError:
134             print '        Log::TextF("%%p", %s);' % instance
135         print '    }'
136         print '    else'
137         print '        Log::Text("NULL");'
138
139     def wrap_instance(self, instance):
140         self.type.wrap_instance("*" + instance)
141
142     def unwrap_instance(self, instance):
143         self.type.wrap_instance("*" + instance)
144
145
146 def ConstPointer(type):
147     return Pointer(Const(type))
148
149
150 class OutPointer(Pointer):
151
152     def isoutput(self):
153         return True
154
155
156 class Enum(Concrete):
157
158     def __init__(self, name, values):
159         Concrete.__init__(self, name)
160         self.values = values
161     
162     def _dump(self, instance):
163         print '    switch(%s) {' % instance
164         for value in self.values:
165             print '    case %s:' % value
166             print '        Log::Text("%s");' % value
167             print '        break;'
168         print '    default:'
169         print '        Log::TextF("%%i", %s);' % instance
170         print '        break;'
171         print '    }'
172
173
174 class Flags(Concrete):
175
176     __seq = 0
177     
178     def __init__(self, type, values):
179         Flags.__seq += 1
180         Concrete.__init__(self, type.name + str(Flags.__seq))
181         self.type = type
182         self.values = values
183
184     def __str__(self):
185         return str(self.type)
186     
187     def _dump(self, instance):
188         print '    %s l_Value = %s;' % (self.type, instance)
189         for value in self.values:
190             print '    if((l_Value & %s) == %s) {' % (value, value)
191             print '        Log::Text("%s | ");' % value
192             print '        l_Value &= ~%s;' % value
193             print '    }'
194         self.type.dump("l_Value");
195
196
197 class Struct(Concrete):
198
199     def __init__(self, name, members):
200         Concrete.__init__(self, name)
201         self.members = members
202
203     def _dump(self, instance):
204         print '    Log::Text("{");'
205         first = True
206         for type, name in self.members:
207             if first:
208                 first = False
209             else:
210                 print '    Log::Text(", ");'
211             type.dump('(%s).%s' % (instance, name))
212         print '    Log::Text("}");'
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::BeginParam("this", "%s *");' % self.name
306             print '    Log::TextF("%p", m_pInstance);'
307             print '    Log::EndParam();'
308             for type, name in method.args:
309                 if not type.isoutput():
310                     type.unwrap_instance(name)
311                     print '    Log::BeginParam("%s", "%s");' % (name, type)
312                     type.dump(name)
313                     print '    Log::EndParam();'
314             print '    %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(name) for type, name in method.args]))
315             for type, name in method.args:
316                 if type.isoutput():
317                     print '    Log::BeginParam("%s", "%s");' % (name, type)
318                     type.dump(name)
319                     print '    Log::EndParam();'
320                     type.wrap_instance(name)
321             if method.type is not Void:
322                 print '    Log::BeginReturn("%s");' % method.type
323                 method.type.dump("result")
324                 print '    Log::EndReturn();'
325                 method.type.wrap_instance('result')
326             print '    Log::EndCall();'
327             if method.name == 'QueryInterface':
328                 print '    if(*ppvObj == m_pInstance)'
329                 print '        *ppvObj = this;'
330             if method.name == 'Release':
331                 assert method.type is not Void
332                 print '    if(!result)'
333                 print '        delete this;'
334             if method.type is not Void:
335                 print '    return result;'
336             print '}'
337             print
338         print
339
340
341 class Method(Function):
342
343     def __init__(self, type, name, args):
344         Function.__init__(self, type, name, args)
345
346
347 towrap = []
348
349 class WrapPointer(Pointer):
350
351     def __init__(self, type):
352         Pointer.__init__(self, type)
353         if type not in towrap:
354             towrap.append(type)
355
356     def wrap_instance(self, instance):
357         print "    if(%s)" % instance
358         print "        %s = new %s(%s);" % (instance, self.type.wrap_name(), instance)
359
360     def unwrap_instance(self, instance):
361         print "    if(%s)" % instance
362         print "        %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, self.type.wrap_name(), instance)
363
364
365 class _String(Type):
366
367     def __init__(self):
368         Type.__init__(self, "String")
369
370     def __str__(self):
371         return "char *"
372
373     def dump(self, instance):
374         print '    Log::DumpString(%s);' % instance
375
376 String = _String()
377
378
379 Short = Intrinsic("short", "%i")
380 Int = Intrinsic("int", "%i")
381 Long = Intrinsic("long", "%li")
382 Float = Intrinsic("float", "%f")
383
384
385 def wrap():
386     for type in all_types.itervalues():
387         type.decl()
388     print
389     for type in all_types.itervalues():
390         type.impl()
391     print
392     for type in towrap:
393         type.wrap_pre_decl()
394     print
395     for type in towrap:
396         type.wrap_decl()
397     print
398     for type in towrap:
399         type.wrap_impl()
400     print