]> git.cworth.org Git - apitrace/blob - specs/stdapi.py
Draft D2D trace support.
[apitrace] / specs / stdapi.py
1 ##########################################################################
2 #
3 # Copyright 2008-2010 VMware, Inc.
4 # All Rights Reserved.
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining a copy
7 # of this software and associated documentation files (the "Software"), to deal
8 # in the Software without restriction, including without limitation the rights
9 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 # copies of the Software, and to permit persons to whom the Software is
11 # furnished to do so, subject to the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 # THE SOFTWARE.
23 #
24 ##########################################################################/
25
26 """C basic types"""
27
28
29 import debug
30
31
32 class Type:
33
34     __all = {}
35     __seq = 0
36
37     def __init__(self, expr, id = ''):
38         self.expr = expr
39         
40         for char in id:
41             assert char.isalnum() or char in '_ '
42
43         id = id.replace(' ', '_')
44         
45         if id in Type.__all:
46             Type.__seq += 1
47             id += str(Type.__seq)
48         
49         assert id not in Type.__all
50         Type.__all[id] = self
51
52         self.id = id
53
54     def __str__(self):
55         return self.expr
56
57     def visit(self, visitor, *args, **kwargs):
58         raise NotImplementedError
59
60
61
62 class _Void(Type):
63
64     def __init__(self):
65         Type.__init__(self, "void")
66
67     def visit(self, visitor, *args, **kwargs):
68         return visitor.visit_void(self, *args, **kwargs)
69
70 Void = _Void()
71
72
73 class Literal(Type):
74
75     def __init__(self, expr, format, base=10):
76         Type.__init__(self, expr)
77         self.format = format
78
79     def visit(self, visitor, *args, **kwargs):
80         return visitor.visit_literal(self, *args, **kwargs)
81
82
83 class Const(Type):
84
85     def __init__(self, type):
86         # While "const foo" and "foo const" are synonymous, "const foo *" and
87         # "foo * const" are not quite the same, and some compilers do enforce
88         # strict const correctness.
89         if isinstance(type, String) or type is WString:
90             # For strings we never intend to say a const pointer to chars, but
91             # rather a point to const chars.
92             expr = "const " + type.expr
93         elif type.expr.startswith("const ") or '*' in type.expr:
94             expr = type.expr + " const"
95         else:
96             # The most legible
97             expr = "const " + type.expr
98
99         Type.__init__(self, expr, 'C' + type.id)
100
101         self.type = type
102
103     def visit(self, visitor, *args, **kwargs):
104         return visitor.visit_const(self, *args, **kwargs)
105
106
107 class Pointer(Type):
108
109     def __init__(self, type):
110         Type.__init__(self, type.expr + " *", 'P' + type.id)
111         self.type = type
112
113     def visit(self, visitor, *args, **kwargs):
114         return visitor.visit_pointer(self, *args, **kwargs)
115
116
117 class Handle(Type):
118
119     def __init__(self, name, type, range=None, key=None):
120         Type.__init__(self, type.expr, 'P' + type.id)
121         self.name = name
122         self.type = type
123         self.range = range
124         self.key = key
125
126     def visit(self, visitor, *args, **kwargs):
127         return visitor.visit_handle(self, *args, **kwargs)
128
129
130 def ConstPointer(type):
131     return Pointer(Const(type))
132
133
134 class Enum(Type):
135
136     def __init__(self, name, values):
137         Type.__init__(self, name)
138         self.values = list(values)
139     
140     def visit(self, visitor, *args, **kwargs):
141         return visitor.visit_enum(self, *args, **kwargs)
142
143
144 def FakeEnum(type, values):
145     return Enum(type.expr, values)
146
147
148 class Bitmask(Type):
149
150     def __init__(self, type, values):
151         Type.__init__(self, type.expr)
152         self.type = type
153         self.values = values
154
155     def visit(self, visitor, *args, **kwargs):
156         return visitor.visit_bitmask(self, *args, **kwargs)
157
158 Flags = Bitmask
159
160
161 class Array(Type):
162
163     def __init__(self, type, length):
164         Type.__init__(self, type.expr + " *")
165         self.type = type
166         self.length = length
167
168     def visit(self, visitor, *args, **kwargs):
169         return visitor.visit_array(self, *args, **kwargs)
170
171
172 class Blob(Type):
173
174     def __init__(self, type, size):
175         Type.__init__(self, type.expr + ' *')
176         self.type = type
177         self.size = size
178
179     def visit(self, visitor, *args, **kwargs):
180         return visitor.visit_blob(self, *args, **kwargs)
181
182
183 class Struct(Type):
184
185     def __init__(self, name, members):
186         Type.__init__(self, name)
187         self.name = name
188         self.members = members
189
190     def visit(self, visitor, *args, **kwargs):
191         return visitor.visit_struct(self, *args, **kwargs)
192
193
194 class Alias(Type):
195
196     def __init__(self, expr, type):
197         Type.__init__(self, expr)
198         self.type = type
199
200     def visit(self, visitor, *args, **kwargs):
201         return visitor.visit_alias(self, *args, **kwargs)
202
203
204 def Out(type, name):
205     arg = Arg(type, name, output=True)
206     return arg
207
208
209 class Arg:
210
211     def __init__(self, type, name, output=False):
212         self.type = type
213         self.name = name
214         self.output = output
215         self.index = None
216
217     def __str__(self):
218         return '%s %s' % (self.type, self.name)
219
220
221 class Function:
222
223     __id = 0
224
225     def __init__(self, type, name, args, call = '', fail = None, sideeffects=True):
226         self.id = Function.__id
227         Function.__id += 1
228
229         self.type = type
230         self.name = name
231
232         self.args = []
233         index = 0
234         for arg in args:
235             if not isinstance(arg, Arg):
236                 if isinstance(arg, tuple):
237                     arg_type, arg_name = arg
238                 else:
239                     arg_type = arg
240                     arg_name = "arg%u" % index
241                 arg = Arg(arg_type, arg_name)
242             arg.index = index
243             index += 1
244             self.args.append(arg)
245
246         self.call = call
247         self.fail = fail
248         self.sideeffects = sideeffects
249
250     def prototype(self, name=None):
251         if name is not None:
252             name = name.strip()
253         else:
254             name = self.name
255         s = name
256         if self.call:
257             s = self.call + ' ' + s
258         if name.startswith('*'):
259             s = '(' + s + ')'
260         s = self.type.expr + ' ' + s
261         s += "("
262         if self.args:
263             s += ", ".join(["%s %s" % (arg.type, arg.name) for arg in self.args])
264         else:
265             s += "void"
266         s += ")"
267         return s
268
269
270 def StdFunction(*args, **kwargs):
271     kwargs.setdefault('call', '__stdcall')
272     return Function(*args, **kwargs)
273
274
275 def FunctionPointer(type, name, args, **kwargs):
276     # XXX: We should probably treat function pointers (callbacks or not) in a generic fashion
277     return Opaque(name)
278
279
280 class Interface(Type):
281
282     def __init__(self, name, base=None):
283         Type.__init__(self, name)
284         self.name = name
285         self.base = base
286         self.methods = []
287
288     def visit(self, visitor, *args, **kwargs):
289         return visitor.visit_interface(self, *args, **kwargs)
290
291     def itermethods(self):
292         if self.base is not None:
293             for method in self.base.itermethods():
294                 yield method
295         for method in self.methods:
296             yield method
297         raise StopIteration
298
299
300 class Method(Function):
301
302     def __init__(self, type, name, args, const=False):
303         Function.__init__(self, type, name, args, call = '__stdcall')
304         for index in range(len(self.args)):
305             self.args[index].index = index + 1
306         self.const = const
307
308     def prototype(self, name=None):
309         s = Function.prototype(self, name)
310         if self.const:
311             s += ' const'
312         return s
313
314
315 class String(Type):
316
317     def __init__(self, expr = "char *", length = None):
318         Type.__init__(self, expr)
319         self.length = length
320
321     def visit(self, visitor, *args, **kwargs):
322         return visitor.visit_string(self, *args, **kwargs)
323
324 # C string (i.e., zero terminated)
325 CString = String()
326
327
328 class Opaque(Type):
329     '''Opaque pointer.'''
330
331     def __init__(self, expr):
332         Type.__init__(self, expr)
333
334     def visit(self, visitor, *args, **kwargs):
335         return visitor.visit_opaque(self, *args, **kwargs)
336
337
338 def OpaquePointer(type, *args):
339     return Opaque(type.expr + ' *')
340
341 def OpaqueArray(type, size):
342     return Opaque(type.expr + ' *')
343
344 def OpaqueBlob(type, size):
345     return Opaque(type.expr + ' *')
346
347
348 class Visitor:
349
350     def visit(self, type, *args, **kwargs):
351         return type.visit(self, *args, **kwargs)
352
353     def visit_void(self, void, *args, **kwargs):
354         raise NotImplementedError
355
356     def visit_literal(self, literal, *args, **kwargs):
357         raise NotImplementedError
358
359     def visit_string(self, string, *args, **kwargs):
360         raise NotImplementedError
361
362     def visit_const(self, const, *args, **kwargs):
363         raise NotImplementedError
364
365     def visit_struct(self, struct, *args, **kwargs):
366         raise NotImplementedError
367
368     def visit_array(self, array, *args, **kwargs):
369         raise NotImplementedError
370
371     def visit_blob(self, blob, *args, **kwargs):
372         raise NotImplementedError
373
374     def visit_enum(self, enum, *args, **kwargs):
375         raise NotImplementedError
376
377     def visit_bitmask(self, bitmask, *args, **kwargs):
378         raise NotImplementedError
379
380     def visit_pointer(self, pointer, *args, **kwargs):
381         raise NotImplementedError
382
383     def visit_handle(self, handle, *args, **kwargs):
384         raise NotImplementedError
385
386     def visit_alias(self, alias, *args, **kwargs):
387         raise NotImplementedError
388
389     def visit_opaque(self, opaque, *args, **kwargs):
390         raise NotImplementedError
391
392     def visit_interface(self, interface, *args, **kwargs):
393         raise NotImplementedError
394
395
396 class OnceVisitor(Visitor):
397
398     def __init__(self):
399         self.__visited = set()
400
401     def visit(self, type, *args, **kwargs):
402         if type not in self.__visited:
403             self.__visited.add(type)
404             return type.visit(self, *args, **kwargs)
405         return None
406
407
408 class Rebuilder(Visitor):
409
410     def visit_void(self, void):
411         return void
412
413     def visit_literal(self, literal):
414         return literal
415
416     def visit_string(self, string):
417         return string
418
419     def visit_const(self, const):
420         return Const(const.type)
421
422     def visit_struct(self, struct):
423         members = [(self.visit(type), name) for type, name in struct.members]
424         return Struct(struct.name, members)
425
426     def visit_array(self, array):
427         type = self.visit(array.type)
428         return Array(type, array.length)
429
430     def visit_blob(self, blob):
431         type = self.visit(blob.type)
432         return Blob(type, blob.size)
433
434     def visit_enum(self, enum):
435         return enum
436
437     def visit_bitmask(self, bitmask):
438         type = self.visit(bitmask.type)
439         return Bitmask(type, bitmask.values)
440
441     def visit_pointer(self, pointer):
442         type = self.visit(pointer.type)
443         return Pointer(type)
444
445     def visit_handle(self, handle):
446         type = self.visit(handle.type)
447         return Handle(handle.name, type, range=handle.range, key=handle.key)
448
449     def visit_alias(self, alias):
450         type = self.visit(alias.type)
451         return Alias(alias.expr, type)
452
453     def visit_opaque(self, opaque):
454         return opaque
455
456
457 class Collector(Visitor):
458     '''Collect.'''
459
460     def __init__(self):
461         self.__visited = set()
462         self.types = []
463
464     def visit(self, type):
465         if type in self.__visited:
466             return
467         self.__visited.add(type)
468         Visitor.visit(self, type)
469         self.types.append(type)
470
471     def visit_void(self, literal):
472         pass
473
474     def visit_literal(self, literal):
475         pass
476
477     def visit_string(self, string):
478         pass
479
480     def visit_const(self, const):
481         self.visit(const.type)
482
483     def visit_struct(self, struct):
484         for type, name in struct.members:
485             self.visit(type)
486
487     def visit_array(self, array):
488         self.visit(array.type)
489
490     def visit_blob(self, array):
491         pass
492
493     def visit_enum(self, enum):
494         pass
495
496     def visit_bitmask(self, bitmask):
497         self.visit(bitmask.type)
498
499     def visit_pointer(self, pointer):
500         self.visit(pointer.type)
501
502     def visit_handle(self, handle):
503         self.visit(handle.type)
504
505     def visit_alias(self, alias):
506         self.visit(alias.type)
507
508     def visit_opaque(self, opaque):
509         pass
510
511     def visit_interface(self, interface):
512         if interface.base is not None:
513             self.visit(interface.base)
514         for method in interface.itermethods():
515             for arg in method.args:
516                 self.visit(arg.type)
517             self.visit(method.type)
518
519
520 class API:
521
522     def __init__(self, name = None):
523         self.name = name
524         self.headers = []
525         self.functions = []
526         self.interfaces = []
527
528     def all_types(self):
529         collector = Collector()
530         for function in self.functions:
531             for arg in function.args:
532                 collector.visit(arg.type)
533             collector.visit(function.type)
534         for interface in self.interfaces:
535             collector.visit(interface)
536             for method in interface.itermethods():
537                 for arg in method.args:
538                     collector.visit(arg.type)
539                 collector.visit(method.type)
540         return collector.types
541
542     def add_function(self, function):
543         self.functions.append(function)
544
545     def add_functions(self, functions):
546         for function in functions:
547             self.add_function(function)
548
549     def add_interface(self, interface):
550         self.interfaces.append(interface)
551
552     def add_interfaces(self, interfaces):
553         self.interfaces.extend(interfaces)
554
555     def add_api(self, api):
556         self.headers.extend(api.headers)
557         self.add_functions(api.functions)
558         self.add_interfaces(api.interfaces)
559
560     def get_function_by_name(self, name):
561         for function in self.functions:
562             if function.name == name:
563                 return function
564         return None
565
566
567 Bool = Literal("bool", "Bool")
568 SChar = Literal("signed char", "SInt")
569 UChar = Literal("unsigned char", "UInt")
570 Short = Literal("short", "SInt")
571 Int = Literal("int", "SInt")
572 Long = Literal("long", "SInt")
573 LongLong = Literal("long long", "SInt")
574 UShort = Literal("unsigned short", "UInt")
575 UInt = Literal("unsigned int", "UInt")
576 ULong = Literal("unsigned long", "UInt")
577 ULongLong = Literal("unsigned long long", "UInt")
578 Float = Literal("float", "Float")
579 Double = Literal("double", "Double")
580 SizeT = Literal("size_t", "UInt")
581 WString = Literal("wchar_t *", "WString")
582
583 Int8 = Literal("int8_t", "SInt")
584 UInt8 = Literal("uint8_t", "UInt")
585 Int16 = Literal("int16_t", "SInt")
586 UInt16 = Literal("uint16_t", "UInt")
587 Int32 = Literal("int32_t", "SInt")
588 UInt32 = Literal("uint32_t", "UInt")
589 Int64 = Literal("int64_t", "SInt")
590 UInt64 = Literal("uint64_t", "UInt")