]> git.cworth.org Git - apitrace/blob - base.py
5cf8c77db29548c6b3e61463f3c2fd59b784d488
[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 FakeEnum(Enum):
180
181     __seq = 0
182     
183     def __init__(self, type, values):
184         FakeEnum.__seq += 1
185         Enum.__init__(self, type.name + str(FakeEnum.__seq), values)
186         self.type = type
187     
188     def __str__(self):
189         return str(self.type)
190
191
192 class Flags(Concrete):
193
194     __seq = 0
195     
196     def __init__(self, type, values):
197         Flags.__seq += 1
198         Concrete.__init__(self, type.name + str(Flags.__seq))
199         self.type = type
200         self.values = values
201
202     def __str__(self):
203         return str(self.type)
204     
205     def _dump(self, instance):
206         print '    %s l_Value = %s;' % (self.type, instance)
207         for value in self.values:
208             print '    if((l_Value & %s) == %s) {' % (value, value)
209             print '        Log::Text("%s | ");' % value
210             print '        l_Value &= ~%s;' % value
211             print '    }'
212         self.type.dump("l_Value");
213
214
215 class Struct(Concrete):
216
217     def __init__(self, name, members):
218         Concrete.__init__(self, name)
219         self.members = members
220
221     def _dump(self, instance):
222         for type, name in self.members:
223             print '    Log::BeginElement("%s", "%s");' % (type, name)
224             type.dump('(%s).%s' % (instance, name))
225             print '    Log::EndElement();'
226
227
228 class Alias(Type):
229
230     def __init__(self, name, type):
231         Type.__init__(self, name)
232         self.type = type
233
234     def dump(self, instance):
235         self.type.dump(instance)
236
237
238 class Function:
239
240     def __init__(self, type, name, args, call = '__stdcall', fail = None):
241         self.type = type
242         self.name = name
243         self.args = args
244         self.call = call
245         self.fail = fail
246
247     def prototype(self, name=None):
248         if name is not None:
249             name = name.strip()
250         else:
251             name = self.name
252         s = name
253         if self.call:
254             s = self.call + ' ' + s
255         if name.startswith('*'):
256             s = '(' + s + ')'
257         s = str(self.type) + ' ' + s
258         s += "("
259         if self.args:
260             s += ", ".join(["%s %s" % (type, name) for type, name in self.args])
261         else:
262             s += "void"
263         s += ")"
264         return s
265
266
267 class Interface(Type):
268
269     def __init__(self, name, base=None):
270         Type.__init__(self, name)
271         self.base = base
272         self.methods = []
273
274     def itermethods(self):
275         if self.base is not None:
276             for method in self.base.itermethods():
277                 yield method
278         for method in self.methods:
279             yield method
280         raise StopIteration
281
282     def wrap_name(self):
283         return "Wrap" + self.name
284
285     def wrap_pre_decl(self):
286         print "class %s;" % self.wrap_name()
287
288     def wrap_decl(self):
289         print "class %s : public %s " % (self.wrap_name(), self.name)
290         print "{"
291         print "public:"
292         print "    %s(%s * pInstance);" % (self.wrap_name(), self.name)
293         print "    virtual ~%s();" % self.wrap_name()
294         print
295         for method in self.itermethods():
296             print "    " + method.prototype() + ";"
297         print
298         #print "private:"
299         print "    %s * m_pInstance;" % (self.name,)
300         print "};"
301         print
302
303     def wrap_impl(self):
304         print '%s::%s(%s * pInstance) {' % (self.wrap_name(), self.wrap_name(), self.name)
305         print '    m_pInstance = pInstance;'
306         print '}'
307         print
308         print '%s::~%s() {' % (self.wrap_name(), self.wrap_name())
309         print '}'
310         print
311         for method in self.itermethods():
312             print method.prototype(self.wrap_name() + '::' + method.name) + ' {'
313             if method.type is Void:
314                 result = ''
315             else:
316                 print '    %s result;' % method.type
317                 result = 'result = '
318             print '    Log::BeginCall("%s");' % (self.name + '::' + method.name)
319             print '    Log::BeginArg("%s *", "this");' % self.name
320             print '    Log::BeginReference("%s", m_pInstance);' % self.name
321             print '    Log::EndReference();'
322             print '    Log::EndArg();'
323             for type, name in method.args:
324                 if not type.isoutput():
325                     type.unwrap_instance(name)
326                     print '    Log::BeginArg("%s", "%s");' % (type, name)
327                     type.dump(name)
328                     print '    Log::EndArg();'
329             print '    %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(name) for type, name in method.args]))
330             for type, name in method.args:
331                 if type.isoutput():
332                     print '    Log::BeginArg("%s", "%s");' % (type, name)
333                     type.dump(name)
334                     print '    Log::EndArg();'
335                     type.wrap_instance(name)
336             if method.type is not Void:
337                 print '    Log::BeginReturn("%s");' % method.type
338                 method.type.dump("result")
339                 print '    Log::EndReturn();'
340                 method.type.wrap_instance('result')
341             print '    Log::EndCall();'
342             if method.name == 'QueryInterface':
343                 print '    if(*ppvObj == m_pInstance)'
344                 print '        *ppvObj = this;'
345             if method.name == 'Release':
346                 assert method.type is not Void
347                 print '    if(!result)'
348                 print '        delete this;'
349             if method.type is not Void:
350                 print '    return result;'
351             print '}'
352             print
353         print
354
355
356 class Method(Function):
357
358     def __init__(self, type, name, args):
359         Function.__init__(self, type, name, args)
360
361
362 towrap = []
363
364 class WrapPointer(Pointer):
365
366     def __init__(self, type):
367         Pointer.__init__(self, type)
368         if type not in towrap:
369             towrap.append(type)
370
371     def wrap_instance(self, instance):
372         print "    if(%s)" % instance
373         print "        %s = new %s(%s);" % (instance, self.type.wrap_name(), instance)
374
375     def unwrap_instance(self, instance):
376         print "    if(%s)" % instance
377         print "        %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, self.type.wrap_name(), instance)
378
379
380 class _String(Type):
381
382     def __init__(self):
383         Type.__init__(self, "String")
384
385     def __str__(self):
386         return "char *"
387
388     def dump(self, instance):
389         print '    Log::DumpString((const char *)%s);' % instance
390
391 String = _String()
392
393
394 SChar = Intrinsic("signed char", "%i")
395 UChar = Intrinsic("unsigned char", "%u")
396 Short = Intrinsic("short", "%i")
397 Int = Intrinsic("int", "%i")
398 Long = Intrinsic("long", "%li")
399 UShort = Intrinsic("unsigned short", "%u")
400 UInt = Intrinsic("unsigned int", "%u")
401 ULong = Intrinsic("unsigned long", "%lu")
402 Float = Intrinsic("float", "%f")
403 Double = Intrinsic("double", "%f")
404
405
406 def wrap():
407     for type in all_types.itervalues():
408         type.decl()
409     print
410     for type in all_types.itervalues():
411         type.impl()
412     print
413     for type in towrap:
414         type.wrap_pre_decl()
415     print
416     for type in towrap:
417         type.wrap_decl()
418     print
419     for type in towrap:
420         type.wrap_impl()
421     print