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