]> git.cworth.org Git - apitrace/blob - base.py
Improve the XML semantic info. CSS magic.
[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         print '        Log::BeginReference("%s", %s);' % (self.type, instance)
132         try:
133             self.type.dump("*" + instance)
134         except NotImplementedError:
135             pass
136         print '        Log::EndReference();'
137         print '    }'
138         print '    else'
139         print '        Log::Text("NULL");'
140
141     def wrap_instance(self, instance):
142         self.type.wrap_instance("*" + instance)
143
144     def unwrap_instance(self, instance):
145         self.type.wrap_instance("*" + instance)
146
147
148 def ConstPointer(type):
149     return Pointer(Const(type))
150
151
152 class OutPointer(Pointer):
153
154     def isoutput(self):
155         return True
156
157
158 class Enum(Concrete):
159
160     def __init__(self, name, values):
161         Concrete.__init__(self, name)
162         self.values = values
163     
164     def _dump(self, instance):
165         print '    switch(%s) {' % instance
166         for value in self.values:
167             print '    case %s:' % value
168             print '        Log::Text("%s");' % value
169             print '        break;'
170         print '    default:'
171         print '        Log::TextF("%%i", %s);' % instance
172         print '        break;'
173         print '    }'
174
175
176 class Flags(Concrete):
177
178     __seq = 0
179     
180     def __init__(self, type, values):
181         Flags.__seq += 1
182         Concrete.__init__(self, type.name + str(Flags.__seq))
183         self.type = type
184         self.values = values
185
186     def __str__(self):
187         return str(self.type)
188     
189     def _dump(self, instance):
190         print '    %s l_Value = %s;' % (self.type, instance)
191         for value in self.values:
192             print '    if((l_Value & %s) == %s) {' % (value, value)
193             print '        Log::Text("%s | ");' % value
194             print '        l_Value &= ~%s;' % value
195             print '    }'
196         self.type.dump("l_Value");
197
198
199 class Struct(Concrete):
200
201     def __init__(self, name, members):
202         Concrete.__init__(self, name)
203         self.members = members
204
205     def _dump(self, instance):
206         for type, name in self.members:
207             print '    Log::BeginElement("%s", "%s");' % (type, name)
208             type.dump('(%s).%s' % (instance, name))
209             print '    Log::EndElement();'
210
211
212 class Alias(Type):
213
214     def __init__(self, name, type):
215         Type.__init__(self, name)
216         self.type = type
217
218     def dump(self, instance):
219         self.type.dump(instance)
220
221
222 class Function:
223
224     def __init__(self, type, name, args, call = '__stdcall'):
225         self.type = type
226         self.name = name
227         self.args = args
228         self.call = call
229
230     def prototype(self, name=None):
231         if name is not None:
232             name = name.strip()
233         else:
234             name = self.name
235         s = name
236         if self.call:
237             s = self.call + ' ' + s
238         if name.startswith('*'):
239             s = '(' + s + ')'
240         s = str(self.type) + ' ' + s
241         s += "("
242         if self.args:
243             s += ", ".join(["%s %s" % (type, name) for type, name in self.args])
244         else:
245             s += "void"
246         s += ")"
247         return s
248
249
250 class Interface(Type):
251
252     def __init__(self, name, base=None):
253         Type.__init__(self, name)
254         self.base = base
255         self.methods = []
256
257     def itermethods(self):
258         if self.base is not None:
259             for method in self.base.itermethods():
260                 yield method
261         for method in self.methods:
262             yield method
263         raise StopIteration
264
265     def wrap_name(self):
266         return "Wrap" + self.name
267
268     def wrap_pre_decl(self):
269         print "class %s;" % self.wrap_name()
270
271     def wrap_decl(self):
272         print "class %s : public %s " % (self.wrap_name(), self.name)
273         print "{"
274         print "public:"
275         print "    %s(%s * pInstance);" % (self.wrap_name(), self.name)
276         print "    virtual ~%s();" % self.wrap_name()
277         print
278         for method in self.itermethods():
279             print "    " + method.prototype() + ";"
280         print
281         #print "private:"
282         print "    %s * m_pInstance;" % (self.name,)
283         print "};"
284         print
285
286     def wrap_impl(self):
287         print '%s::%s(%s * pInstance) {' % (self.wrap_name(), self.wrap_name(), self.name)
288         print '    m_pInstance = pInstance;'
289         print '}'
290         print
291         print '%s::~%s() {' % (self.wrap_name(), self.wrap_name())
292         print '}'
293         print
294         for method in self.itermethods():
295             print method.prototype(self.wrap_name() + '::' + method.name) + ' {'
296             if method.type is Void:
297                 result = ''
298             else:
299                 print '    %s result;' % method.type
300                 result = 'result = '
301             print '    Log::BeginCall("%s");' % (self.name + '::' + method.name)
302             print '    Log::BeginArg("%s *", "this");' % self.name
303             print '    Log::TextF("%p", m_pInstance);'
304             print '    Log::EndArg();'
305             for type, name in method.args:
306                 if not type.isoutput():
307                     type.unwrap_instance(name)
308                     print '    Log::BeginArg("%s", "%s");' % (type, name)
309                     type.dump(name)
310                     print '    Log::EndArg();'
311             print '    %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(name) for type, name in method.args]))
312             for type, name in method.args:
313                 if type.isoutput():
314                     print '    Log::BeginArg("%s", "%s");' % (type, name)
315                     type.dump(name)
316                     print '    Log::EndArg();'
317                     type.wrap_instance(name)
318             if method.type is not Void:
319                 print '    Log::BeginReturn("%s");' % method.type
320                 method.type.dump("result")
321                 print '    Log::EndReturn();'
322                 method.type.wrap_instance('result')
323             print '    Log::EndCall();'
324             if method.name == 'QueryInterface':
325                 print '    if(*ppvObj == m_pInstance)'
326                 print '        *ppvObj = this;'
327             if method.name == 'Release':
328                 assert method.type is not Void
329                 print '    if(!result)'
330                 print '        delete this;'
331             if method.type is not Void:
332                 print '    return result;'
333             print '}'
334             print
335         print
336
337
338 class Method(Function):
339
340     def __init__(self, type, name, args):
341         Function.__init__(self, type, name, args)
342
343
344 towrap = []
345
346 class WrapPointer(Pointer):
347
348     def __init__(self, type):
349         Pointer.__init__(self, type)
350         if type not in towrap:
351             towrap.append(type)
352
353     def wrap_instance(self, instance):
354         print "    if(%s)" % instance
355         print "        %s = new %s(%s);" % (instance, self.type.wrap_name(), instance)
356
357     def unwrap_instance(self, instance):
358         print "    if(%s)" % instance
359         print "        %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, self.type.wrap_name(), instance)
360
361
362 class _String(Type):
363
364     def __init__(self):
365         Type.__init__(self, "String")
366
367     def __str__(self):
368         return "char *"
369
370     def dump(self, instance):
371         print '    Log::DumpString(%s);' % instance
372
373 String = _String()
374
375
376 Short = Intrinsic("short", "%i")
377 Int = Intrinsic("int", "%i")
378 Long = Intrinsic("long", "%li")
379 Float = Intrinsic("float", "%f")
380
381
382 def wrap():
383     for type in all_types.itervalues():
384         type.decl()
385     print
386     for type in all_types.itervalues():
387         type.impl()
388     print
389     for type in towrap:
390         type.wrap_pre_decl()
391     print
392     for type in towrap:
393         type.wrap_decl()
394     print
395     for type in towrap:
396         type.wrap_impl()
397     print