]> git.cworth.org Git - apitrace/blob - model.py
Build D3D wrappers with CMake.
[apitrace] / model.py
1 #!/usr/bin/env python
2 ##########################################################################
3 #
4 # Copyright 2008-2009 VMware, Inc.
5 # All Rights Reserved.
6 #
7 # Permission is hereby granted, free of charge, to any person obtaining a copy
8 # of this software and associated documentation files (the "Software"), to deal
9 # in the Software without restriction, including without limitation the rights
10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 # copies of the Software, and to permit persons to whom the Software is
12 # furnished to do so, subject to the following conditions:
13 #
14 # The above copyright notice and this permission notice shall be included in
15 # all copies or substantial portions of the Software.
16 #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 # THE SOFTWARE.
24 #
25 ##########################################################################/
26
27
28 '''Trace data model.'''
29
30
31 import sys
32 import string
33 import format
34
35 try:
36     from cStringIO import StringIO
37 except ImportError:
38     from StringIO import StringIO
39
40
41 class Node:
42     
43     def visit(self, visitor):
44         raise NotImplementedError
45
46     def __str__(self):
47         stream = StringIO()
48         formatter = format.DefaultFormatter(stream)
49         pretty_printer = PrettyPrinter(formatter)
50         self.visit(pretty_printer)
51         return stream.getvalue()
52
53
54 class Literal(Node):
55     
56     def __init__(self, value):
57         self.value = value
58
59     def visit(self, visitor):
60         visitor.visit_literal(self)
61
62
63 class NamedConstant(Node):
64     
65     def __init__(self, name):
66         self.name = name
67
68     def visit(self, visitor):
69         visitor.visit_named_constant(self)
70     
71
72 class Bitmask(Node):
73     
74     def __init__(self, elements):
75         self.elements = elements
76
77     def visit(self, visitor):
78         visitor.visit_bitmask(self)
79
80
81 class Array(Node):
82     
83     def __init__(self, elements):
84         self.elements = elements
85
86     def visit(self, visitor):
87         visitor.visit_array(self)
88
89
90 class Struct(Node):
91     
92     def __init__(self, name, members):
93         self.name = name
94         self.members = members        
95
96     def visit(self, visitor):
97         visitor.visit_struct(self)
98
99         
100 class Pointer(Node):
101     
102     def __init__(self, address, value):
103         self.address = address
104         self.address = value
105
106     def visit(self, visitor):
107         visitor.visit_pointer(self)
108
109
110 class Call(Node):
111     
112     def __init__(self, no, name, args, ret, attrs = None):
113         self.no = no
114         self.name = name
115         self.args = args
116         self.ret = ret
117         if attrs is None:
118             self.attrs = {}
119         else:
120             self.attrs = attrs
121         
122     def visit(self, visitor):
123         visitor.visit_call(self)
124
125
126 class Trace:
127     
128     def __init__(self, calls):
129         self.calls = calls
130         
131     def visit(self, visitor):
132         visitor.visit_trace(self)
133     
134     
135 class Visitor:
136     
137     def visit_literal(self, node):
138         raise NotImplementedError
139     
140     def visit_named_constant(self, node):
141         raise NotImplementedError
142     
143     def visit_bitmask(self, node):
144         raise NotImplementedError
145     
146     def visit_array(self, node):
147         raise NotImplementedError
148     
149     def visit_struct(self, node):
150         raise NotImplementedError
151     
152     def visit_pointer(self, node):
153         raise NotImplementedError
154     
155     def visit_call(self, node):
156         raise NotImplementedError
157     
158     def visit_trace(self, node):
159         raise NotImplementedError
160
161
162 class PrettyPrinter:
163
164     def __init__(self, formatter):
165         self.formatter = formatter
166     
167     def visit_literal(self, node):
168         if isinstance(node.value, basestring):
169             if len(node.value) >= 4096 or node.value.strip(string.printable):
170                 self.formatter.text('...')
171                 return
172
173             self.formatter.literal('"' + node.value + '"')
174             return
175
176         self.formatter.literal(str(node.value))
177     
178     def visit_named_constant(self, node):
179         self.formatter.literal(node.name)
180     
181     def visit_bitmask(self, node):
182         if len(node.elements) == 0:
183             self.formatter.literal('0')
184             return
185         if len(node.elements) > 1:
186             self.formatter.text('(')
187         sep = ''
188         for value in node.elements:
189             self.formatter.text(sep)
190             value.visit(self) 
191             sep = ' | '
192         if len(node.elements) > 1:
193             self.formatter.text(')')
194     
195     def visit_array(self, node):
196         self.formatter.text('{')
197         sep = ''
198         for value in node.elements:
199             self.formatter.text(sep)
200             value.visit(self) 
201             sep = ', '
202         self.formatter.text('}')
203     
204     def visit_struct(self, node):
205         self.formatter.text('{')
206         sep = ''
207         for name, value in node.members:
208             self.formatter.text(sep)
209             self.formatter.variable(name)
210             self.formatter.text(' = ')
211             value.visit(self) 
212             sep = ', '
213         self.formatter.text('}')
214     
215     def visit_pointer(self, node):
216         self.formatter.address(node.address)
217     
218     def visit_call(self, node):
219         self.formatter.text('%s ' % node.no)
220         self.formatter.function(node.name)
221         self.formatter.text('(')
222         sep = ''
223         for name, value in node.args:
224             self.formatter.text(sep)
225             self.formatter.variable(name)
226             self.formatter.text(' = ')
227             value.visit(self) 
228             sep = ', '
229         self.formatter.text(')')
230         if node.ret is not None:
231             self.formatter.text(' = ')
232             node.ret.visit(self)
233     
234     def visit_trace(self, node):
235         for call in node.calls:
236             call.visit(self)
237             self.formatter.newline()
238