]> git.cworth.org Git - apitrace/blob - trace_model.cpp
Move Visitor::visit methods to .cpp; cleanup.
[apitrace] / trace_model.cpp
1 /**************************************************************************
2  *
3  * Copyright 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
27 #include "formatter.hpp"
28 #include "trace_model.hpp"
29
30
31 namespace Trace {
32
33
34 Call::~Call() {
35     for (unsigned i = 0; i < args.size(); ++i) {
36         delete args[i];
37     }
38
39     if (ret) {
40         delete ret;
41     }
42 }
43
44
45 Struct::~Struct() {
46     for (std::vector<Value *>::iterator it = members.begin(); it != members.end(); ++it) {
47         delete *it;
48     }
49 }
50
51
52 Array::~Array() {
53     for (std::vector<Value *>::iterator it = values.begin(); it != values.end(); ++it) {
54         delete *it;
55     }
56 }
57
58 Blob::~Blob() {
59     // TODO: Don't leak blobs.  Blobs are often bound and accessed during many
60     // calls, so we can't delete them here.
61     //delete [] buf;
62 }
63
64
65 // virtual Value::visit()
66 void Null   ::visit(Visitor &visitor) { visitor.visit(this); }
67 void Bool   ::visit(Visitor &visitor) { visitor.visit(this); }
68 void SInt   ::visit(Visitor &visitor) { visitor.visit(this); }
69 void UInt   ::visit(Visitor &visitor) { visitor.visit(this); }
70 void Float  ::visit(Visitor &visitor) { visitor.visit(this); }
71 void String ::visit(Visitor &visitor) { visitor.visit(this); }
72 void Enum   ::visit(Visitor &visitor) { visitor.visit(this); }
73 void Bitmask::visit(Visitor &visitor) { visitor.visit(this); }
74 void Struct ::visit(Visitor &visitor) { visitor.visit(this); }
75 void Array  ::visit(Visitor &visitor) { visitor.visit(this); }
76 void Blob   ::visit(Visitor &visitor) { visitor.visit(this); }
77 void Pointer::visit(Visitor &visitor) { visitor.visit(this); }
78
79
80 void Visitor::visit(Null *) { assert(0); }
81 void Visitor::visit(Bool *) { assert(0); }
82 void Visitor::visit(SInt *) { assert(0); }
83 void Visitor::visit(UInt *) { assert(0); }
84 void Visitor::visit(Float *) { assert(0); }
85 void Visitor::visit(String *) { assert(0); }
86 void Visitor::visit(Enum *) { assert(0); }
87 void Visitor::visit(Bitmask *bitmask) { visit(static_cast<UInt *>(bitmask)); }
88 void Visitor::visit(Struct *) { assert(0); }
89 void Visitor::visit(Array *) { assert(0); }
90 void Visitor::visit(Blob *) { assert(0); }
91 void Visitor::visit(Pointer *) { assert(0); }
92
93
94 class Dumper : public Visitor
95 {
96 protected:
97     std::ostream &os;
98     Formatter::Formatter *formatter;
99     Formatter::Attribute *normal;
100     Formatter::Attribute *bold;
101     Formatter::Attribute *italic;
102     Formatter::Attribute *red;
103     Formatter::Attribute *pointer;
104     Formatter::Attribute *literal;
105
106 public:
107     Dumper(std::ostream &_os) : os(_os) {
108         formatter = Formatter::defaultFormatter();
109         normal = formatter->normal();
110         bold = formatter->bold();
111         italic = formatter->italic();
112         red = formatter->color(Formatter::RED);
113         pointer = formatter->color(Formatter::GREEN);
114         literal = formatter->color(Formatter::BLUE);
115     }
116
117     ~Dumper() {
118         delete normal;
119         delete bold;
120         delete italic;
121         delete red;
122         delete pointer;
123         delete literal;
124         delete formatter;
125     }
126
127     void visit(Null *) {
128         os << "NULL";
129     }
130
131     void visit(Bool *node) {
132         os << literal << (node->value ? "true" : "false") << normal;
133     }
134
135     void visit(SInt *node) {
136         os << literal << node->value << normal;
137     }
138
139     void visit(UInt *node) {
140         os << literal << node->value << normal;
141     }
142
143     void visit(Float *node) {
144         os << literal << node->value << normal;
145     }
146
147     void visit(String *node) {
148         os << literal << '"' << node->value << '"' << normal;
149     }
150
151     void visit(Enum *node) {
152         os << literal << node->sig->first << normal;
153     }
154
155     void visit(Bitmask *bitmask) {
156         unsigned long long value = bitmask->value;
157         const Bitmask::Signature *sig = bitmask->sig;
158         bool first = true;
159         for (Bitmask::Signature::const_iterator it = sig->begin(); value != 0 && it != sig->end(); ++it) {
160             assert(it->second);
161             if ((value & it->second) == it->second) {
162                 if (!first) {
163                     os << " | ";
164                 }
165                 os << literal << it->first << normal;
166                 value &= ~it->second;
167                 first = false;
168             }
169         }
170         if (value || first) {
171             if (!first) {
172                 os << " | ";
173             }
174             os << literal << "0x" << std::hex << value << std::dec << normal;
175         }
176     }
177
178     void visit(Struct *s) {
179         const char *sep = "";
180         os << "{";
181         for (unsigned i = 0; i < s->members.size(); ++i) {
182             os << sep << italic << s->sig->member_names[i] << normal << " = ";
183             _visit(s->members[i]);
184             sep = ", ";
185         }
186         os << "}";
187     }
188
189     void visit(Array *array) {
190         if (array->values.size() == 1) {
191             os << "&";
192             _visit(array->values[0]);
193         }
194         else {
195             const char *sep = "";
196             os << "{";
197             for (std::vector<Value *>::iterator it = array->values.begin(); it != array->values.end(); ++it) {
198                 os << sep;
199                 _visit(*it);
200                 sep = ", ";
201             }
202             os << "}";
203         }
204     }
205
206     void visit(Blob *blob) {
207         os << pointer << "blob(" << blob->size << ")" << normal;
208     }
209
210     void visit(Pointer *p) {
211         os << pointer << "0x" << std::hex << p->value << std::dec << normal;
212     }
213
214     void visit(Call *call) {
215         const char *sep = "";
216         os << bold << call->sig->name << normal << "(";
217         for (unsigned i = 0; i < call->args.size(); ++i) {
218             os << sep << italic << call->sig->arg_names[i] << normal << " = ";
219             _visit(call->args[i]);
220             sep = ", ";
221         }
222         os << ")";
223         if (call->ret) {
224             os << " = ";
225             _visit(call->ret);
226         }
227         os << "\n";
228     }
229 };
230
231
232 std::ostream & operator <<(std::ostream &os, Value *value) {
233     Dumper d(os);
234     if (value) {
235         value->visit(d);
236     }
237     return os;
238 }
239
240
241 static inline const Value *unwrap(const Value *node) {
242     const Enum *c = dynamic_cast<const Enum *>(node);
243     if (c)
244         return c->sig->second;
245     return node;
246 }
247
248
249 Value::operator bool(void) const {
250     const Bool *b = dynamic_cast<const Bool *>(unwrap(this));
251     if (b)
252         return b->value;
253     assert(0);
254     return false;
255 }
256
257 Value::operator signed long long(void) const {
258     const SInt *sint = dynamic_cast<const SInt *>(unwrap(this));
259     if (sint)
260         return sint->value;
261     const UInt *uint = dynamic_cast<const UInt *>(unwrap(this));
262     if (uint)
263         return uint->value;
264     assert(0);
265     return 0;
266 }
267
268 Value::operator unsigned long long(void) const {
269     const UInt *uint = dynamic_cast<const UInt *>(unwrap(this));
270     if (uint)
271         return uint->value;
272     assert(0);
273     return 0;
274 }
275
276
277 Value::operator double(void) const {
278     const Float *fl = dynamic_cast<const Float *>(unwrap(this));
279     assert(fl);
280     return fl->value;
281 }
282
283 static Null null;
284
285 const Value & Value::operator[](size_t index) const {
286     const Array *array = dynamic_cast<const Array *>(unwrap(this));
287     if (array) {
288         if (index < array->values.size()) {
289             return *array->values[index];
290         }
291     }
292     return null;
293 }
294
295 void * Value::blob(void) const {
296     const Blob *blob = dynamic_cast<const Blob *>(unwrap(this));
297     if (blob)
298         return blob->buf;
299     const Null *null = dynamic_cast<const Null *>(unwrap(this));
300     if (null)
301         return NULL;
302     const Pointer *pointer = dynamic_cast<const Pointer *>(unwrap(this));
303     if (pointer) {
304         assert(pointer->value  < 0x100000ULL);
305         return (void *)pointer->value;
306     }
307     assert(0);
308     return NULL;
309 }
310
311 const char * Value::string(void) const {
312     const String *string = dynamic_cast<const String *>(unwrap(this));
313     if (string)
314         return string->value.c_str();
315     const Null *null = dynamic_cast<const Null *>(unwrap(this));
316     if (null)
317         return NULL;
318     assert(0);
319     return NULL;
320 }
321
322 std::ostream & operator <<(std::ostream &os, Call &call) {
323     Dumper d(os);
324     os << call.no << " ";
325     d.visit(&call);
326     return os;
327 }
328
329
330 } /* namespace Trace */