]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/debug.cpp
Tweak debug output
[libs/gl.git] / source / glsl / debug.cpp
1 #include <msp/stringcodec/utf8.h>
2 #include <msp/strings/format.h>
3 #include "debug.h"
4
5 using namespace std;
6
7 namespace Msp {
8 namespace GL {
9 namespace SL {
10
11 const std::string &DumpTree::apply(Stage &stage)
12 {
13         formatted = format("Stage: %s\n", Stage::get_stage_name(stage.type));
14         tree.push_back(BRANCH);
15         append(format("Version: %d.%02d", stage.required_features.glsl_version.major, stage.required_features.glsl_version.minor));
16         last_branch();
17         stage.content.visit(*this);
18         return formatted;
19 }
20
21 void DumpTree::append(const string &line)
22 {
23         StringCodec::Utf8::Encoder enc;
24         for(vector<TreeChars>::const_iterator i=tree.begin(); i!=tree.end(); )
25         {
26                 enc.encode_char(*i++, formatted);
27                 enc.encode_char((i==tree.end() ? REACH : EMPTY), formatted);
28         }
29         formatted += line;
30         formatted += '\n';
31 }
32
33 void DumpTree::begin_sub()
34 {
35         tree.back() = (tree.back()==BRANCH_LAST ? EMPTY : STRAIGHT);
36         tree.push_back(BRANCH);
37 }
38
39 void DumpTree::last_branch()
40 {
41         tree.back() = BRANCH_LAST;
42 }
43
44 void DumpTree::end_sub()
45 {
46         tree.pop_back();
47         if(tree.back()==STRAIGHT)
48                 tree.back() = BRANCH;
49 }
50
51 void DumpTree::annotated_branch(const string &annotation, Node &node)
52 {
53         append(annotation);
54         begin_sub();
55         last_branch();
56         node.visit(*this);
57         end_sub();
58 }
59
60 unsigned DumpTree::get_label(const Node &node)
61 {
62         unsigned &label = node_labels[&node];
63         if(!label)
64                 label = node_labels.size();
65         return label;
66 }
67
68 template<typename T>
69 typename T::const_iterator DumpTree::increment(typename T::const_iterator &iter, const T &container)
70 {
71         typename T::const_iterator ret = iter++;
72         if(iter==container.end())
73                 last_branch();
74         return ret;
75 }
76
77 void DumpTree::visit(Block &block)
78 {
79         append(format("Block %s", (block.use_braces ? "{}" : "(inline)")));
80         begin_sub();
81
82         for(std::map<string, StructDeclaration *>::const_iterator i=block.types.begin(); i!=block.types.end(); ++i)
83                 append(format("Type: %%%d %s", get_label(*i->second), i->first));
84
85         for(std::map<string, VariableDeclaration *>::const_iterator i=block.variables.begin(); i!=block.variables.end(); ++i)
86                 append(format("Variable: %%%d %s %s", get_label(*i->second), i->second->type, i->first));
87
88         for(std::set<InterfaceBlock *>::const_iterator i=block.interfaces.begin(); i!=block.interfaces.end(); ++i)
89                 append(format("Interface: %%%d %s %s", get_label(*i->second), i->second->interface, i->second->name));
90
91         bool labeled_body = (!block.types.empty() || !block.variables.empty());
92         if(labeled_body)
93         {
94                 last_branch();
95                 append("Body");
96                 begin_sub();
97         }
98         for(NodeList<Statement>::const_iterator i=block.body.begin(); i!=block.body.end(); )
99         {
100                 NodeList<Statement>::const_iterator j = increment(i, block.body);
101                 (*j)->visit(*this);
102         }
103         if(labeled_body)
104                 end_sub();
105
106         end_sub();
107 }
108
109 void DumpTree::visit(Literal &literal)
110 {
111         append(format("Literal: %s", literal.token));
112 }
113
114 void DumpTree::visit(ParenthesizedExpression &parexpr)
115 {
116         annotated_branch("(expr)", *parexpr.expression);
117 }
118
119 void DumpTree::visit(VariableReference &var)
120 {
121         string text;
122         if(var.declaration)
123                 text += format("%%%d ", get_label(*var.declaration));
124         text += var.name;
125         append(text);
126 }
127
128 void DumpTree::visit(MemberAccess &memacc)
129 {
130         string text = "Member access:";
131         if(memacc.declaration)
132                 text += format(" %%%d", get_label(*memacc.declaration));
133         text += format(" .%s", memacc.member);
134         annotated_branch(text, *memacc.left);
135 }
136
137 void DumpTree::visit(UnaryExpression &unary)
138 {
139         annotated_branch(format("Unary: %s, %sfix", unary.oper, (unary.prefix ? "pre" : "suff")), *unary.expression);
140 }
141
142 void DumpTree::visit(BinaryExpression &binary)
143 {
144         append(format("Binary: %s%s", binary.oper, binary.after));
145         begin_sub();
146         binary.left->visit(*this);
147         last_branch();
148         binary.right->visit(*this);
149         end_sub();
150 }
151
152 void DumpTree::visit(Assignment &assign)
153 {
154         append(format("Assignment: %s%s", assign.oper, (assign.self_referencing ? " (self-referencing)" : "")));
155         begin_sub();
156         if(assign.target_declaration)
157         {
158                 append(format("Target: %%%d %s %s", get_label(*assign.target_declaration), assign.target_declaration->type, assign.target_declaration->name));
159         }
160         assign.left->visit(*this);
161         last_branch();
162         assign.right->visit(*this);
163         end_sub();
164 }
165
166 void DumpTree::visit(FunctionCall &call)
167 {
168         string head = "Function call: ";
169         if(call.declaration)
170                 head += format("%%%d ", get_label(*call.declaration));
171         head += call.name;
172         if(call.constructor)
173                 head += " (constructor)";
174         append(head);
175
176         begin_sub();
177         for(NodeArray<Expression>::const_iterator i=call.arguments.begin(); i!=call.arguments.end(); )
178         {
179                 NodeArray<Expression>::const_iterator j = increment(i, call.arguments);
180                 (*j)->visit(*this);
181         }
182         end_sub();
183 }
184
185 void DumpTree::visit(ExpressionStatement &expr)
186 {
187         annotated_branch("expr;", *expr.expression);
188 }
189
190 void DumpTree::visit(Import &import)
191 {
192         append(format("import %s", import.module));
193 }
194
195 void DumpTree::visit(Precision &prec)
196 {
197         append(format("precision %s %s", prec.precision, prec.type));
198 }
199
200 void DumpTree::visit(Layout &layout)
201 {
202         append("Layout");
203         begin_sub();
204         for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); )
205         {
206                 vector<Layout::Qualifier>::const_iterator j = increment(i, layout.qualifiers);
207                 string qualifier = j->name;
208                 if(j->has_value)
209                         qualifier += format("=%d", j->value);
210                 append(qualifier);
211         }
212         end_sub();
213 }
214
215 void DumpTree::visit(InterfaceLayout &layout)
216 {
217         annotated_branch(format("Layout: %s", layout.interface), layout.layout);
218 }
219
220 void DumpTree::visit(StructDeclaration &strct)
221 {
222         annotated_branch(format("%%%d struct %s", get_label(strct), strct.name), strct.members);
223 }
224
225 void DumpTree::visit(VariableDeclaration &var)
226 {
227         string decl = format("%%%d ", get_label(var));
228         if(var.constant)
229                 decl += "const ";
230         if(!var.interpolation.empty())
231                 decl += format("%s ", var.interpolation);
232         if(!var.sampling.empty())
233                 decl += format("%s ", var.sampling);
234         if(!var.interface.empty())
235                 decl += format("%s ", var.interface);
236         if(!var.precision.empty())
237                 decl += format("%s ", var.precision);
238         decl += format("%s %s", var.type, var.name);
239         if(var.linked_declaration)
240                 decl += " (linked)";
241         append(decl);
242
243         begin_sub();
244         if(!var.array && !var.init_expression)
245                 last_branch();
246         if(var.layout)
247                 var.layout->visit(*this);
248
249         if(!var.init_expression)
250                 last_branch();
251         if(var.array)
252         {
253                 if(var.array_size)
254                         annotated_branch("Array []", *var.array_size);
255                 else
256                         append("Array []");
257         }
258
259         last_branch();
260         if(var.init_expression)
261                 var.init_expression->visit(*this);
262         end_sub();
263 }
264
265 void DumpTree::visit(InterfaceBlock &block)
266 {
267         annotated_branch(format("%s %s", block.interface, block.name), block.members);
268 }
269
270 void DumpTree::visit(FunctionDeclaration &func)
271 {
272         append(format("%%%d %s %s()", get_label(func), func.return_type, func.name));
273         begin_sub();
274         for(NodeArray<VariableDeclaration>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
275                 (*i)->visit(*this);
276         if(func.definition)
277                 append(format("Definition: %%%d", get_label(*func.definition)));
278         last_branch();
279         func.body.visit(*this);
280         end_sub();
281 }
282
283 void DumpTree::visit(Conditional &cond)
284 {
285         append("if()");
286         begin_sub();
287         cond.condition->visit(*this);
288         if(cond.else_body.body.empty())
289                 last_branch();
290         cond.body.visit(*this);
291         if(!cond.else_body.body.empty())
292         {
293                 last_branch();
294                 cond.else_body.visit(*this);
295         }
296         end_sub();
297 }
298
299 void DumpTree::visit(Iteration &iter)
300 {
301         append("for()");
302         begin_sub();
303
304         if(iter.init_statement)
305                 annotated_branch("Initialization", *iter.init_statement);
306         if(iter.condition)
307                 annotated_branch("Condition", *iter.condition);
308         if(iter.loop_expression)
309                 annotated_branch("Loop", *iter.loop_expression);
310         last_branch();
311         annotated_branch("Body", iter.body);
312
313         end_sub();
314 }
315
316 void DumpTree::visit(Passthrough &pass)
317 {
318         append("passthrough");
319         if(pass.subscript)
320         {
321                 begin_sub();
322                 last_branch();
323                 pass.subscript->visit(*this);
324                 end_sub();
325         }
326 }
327
328 void DumpTree::visit(Return &ret)
329 {
330         if(ret.expression)
331                 annotated_branch("return", *ret.expression);
332         else
333                 append("return;");
334 }
335
336 void DumpTree::visit(Jump &jump)
337 {
338         append(format("%s;", jump.keyword));
339 }
340
341 } // namespace SL
342 } // namespace GL
343 } // namespace Msp