]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/debug.cpp
Don't return references from apply functions
[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 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, TypeDeclaration *>::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         for(std::map<string, InterfaceBlock *>::const_iterator i=stage.interface_blocks.begin(); i!=stage.interface_blocks.end(); ++i)
21                 append(format("Interface block: %%%d %s", get_label(*i->second), i->first));
22
23         for(std::map<string, FunctionDeclaration *>::const_iterator i=stage.functions.begin(); i!=stage.functions.end(); ++i)
24                 append(format("Function: %%%d %s", get_label(*i->second), i->first));
25
26         last_branch();
27         stage.content.visit(*this);
28         return formatted;
29 }
30
31 void DumpTree::append(const string &line)
32 {
33         StringCodec::Utf8::Encoder enc;
34         for(vector<TreeChars>::const_iterator i=tree.begin(); i!=tree.end(); )
35         {
36                 enc.encode_char(*i++, formatted);
37                 enc.encode_char((i==tree.end() ? REACH : EMPTY), formatted);
38         }
39         formatted += line;
40         formatted += '\n';
41 }
42
43 void DumpTree::append(const Node &node, const string &line)
44 {
45         append(format("<%d:%d> %s", node.source, node.line, line));
46 }
47
48 void DumpTree::append_subtree(const vector<Branch> &branches)
49 {
50         begin_sub();
51         for(vector<Branch>::const_iterator i=branches.begin(); i!=branches.end(); )
52         {
53                 vector<Branch>::const_iterator j = increment(i, branches);
54                 if(!j->text.empty())
55                 {
56                         append(j->text);
57                         if(j->node)
58                         {
59                                 begin_sub();
60                                 last_branch();
61                                 j->node->visit(*this);
62                                 end_sub();
63                         }
64                 }
65                 else
66                         j->node->visit(*this);
67         }
68         end_sub();
69 }
70
71 void DumpTree::append_subtree(Node &node)
72 {
73         begin_sub();
74         last_branch();
75         node.visit(*this);
76         end_sub();
77 }
78
79 void DumpTree::begin_sub()
80 {
81         tree.back() = (tree.back()==BRANCH_LAST ? EMPTY : STRAIGHT);
82         tree.push_back(BRANCH);
83 }
84
85 void DumpTree::last_branch()
86 {
87         tree.back() = BRANCH_LAST;
88 }
89
90 void DumpTree::end_sub()
91 {
92         tree.pop_back();
93         if(tree.back()==STRAIGHT)
94                 tree.back() = BRANCH;
95 }
96
97 unsigned DumpTree::get_label(const Node &node)
98 {
99         unsigned &label = node_labels[&node];
100         if(!label)
101                 label = node_labels.size();
102         return label;
103 }
104
105 string DumpTree::format_type(TypeDeclaration *type)
106 {
107         return (type ? type->name : "?");
108 }
109
110 template<typename T>
111 typename T::const_iterator DumpTree::increment(typename T::const_iterator &iter, const T &container)
112 {
113         typename T::const_iterator ret = iter++;
114         if(iter==container.end())
115                 last_branch();
116         return ret;
117 }
118
119 void DumpTree::visit(Block &block)
120 {
121         append(block, format("Block %s", (block.use_braces ? "{}" : "(inline)")));
122         begin_sub();
123
124         for(std::map<string, VariableDeclaration *>::const_iterator i=block.variables.begin(); i!=block.variables.end(); ++i)
125                 append(format("Variable: %%%d %s %s", get_label(*i->second), i->second->type, i->first));
126
127         for(NodeList<Statement>::const_iterator i=block.body.begin(); i!=block.body.end(); )
128         {
129                 NodeList<Statement>::const_iterator j = increment(i, block.body);
130                 (*j)->visit(*this);
131         }
132
133         end_sub();
134 }
135
136 void DumpTree::visit(Literal &literal)
137 {
138         append(literal, format("Literal: %s -> %s", literal.token, format_type(literal.type)));
139 }
140
141 void DumpTree::visit(VariableReference &var)
142 {
143         string text;
144         if(var.declaration)
145                 text += format("%%%d ", get_label(*var.declaration));
146         text += format("%s (var) -> %s", var.name, format_type(var.type));
147         append(var, text);
148 }
149
150 void DumpTree::visit(InterfaceBlockReference &iface)
151 {
152         string text;
153         if(iface.declaration)
154                 text += format("%%%d ", get_label(*iface.declaration));
155         text += format("%s (iface) -> %s", iface.name, format_type(iface.type));
156         append(iface, text);
157 }
158
159 void DumpTree::visit(MemberAccess &memacc)
160 {
161         string text = "Member access:";
162         if(memacc.declaration)
163                 text += format(" %%%d", get_label(*memacc.declaration));
164         text += format(" .%s -> %s", memacc.member, format_type(memacc.type));
165         append(memacc, text);
166         append_subtree(*memacc.left);
167 }
168
169 void DumpTree::visit(Swizzle &swizzle)
170 {
171         static const char components[4] = { 'x', 'y', 'z', 'w' };
172         string text = "Swizzle: .";
173         for(unsigned i=0; i<swizzle.count; ++i)
174                 text += components[swizzle.components[i]];
175         text += format(" -> %s", format_type(swizzle.type));
176         append(swizzle, text);
177         append_subtree(*swizzle.left);
178 }
179
180 void DumpTree::visit(UnaryExpression &unary)
181 {
182         string text = format("Unary: %s, %sfix -> %s", unary.oper->token, (unary.oper->type==Operator::PREFIX ? "pre" : "post"), format_type(unary.type));
183         append(unary, text);
184         append_subtree(*unary.expression);
185 }
186
187 void DumpTree::visit(BinaryExpression &binary)
188 {
189         append(binary, format("Binary: %s%s -> %s", binary.oper->token, binary.oper->token2, format_type(binary.type)));
190         begin_sub();
191         binary.left->visit(*this);
192         last_branch();
193         binary.right->visit(*this);
194         end_sub();
195 }
196
197 void DumpTree::visit(Assignment &assign)
198 {
199         append(assign, format("Assignment: %s%s -> %s", assign.oper->token, (assign.self_referencing ? " (self-referencing)" : ""), format_type(assign.type)));
200         begin_sub();
201         if(assign.target.declaration)
202         {
203                 string text = format("Target: %%%d", get_label(*assign.target.declaration));
204
205                 static const char swizzle[4] = { 'x', 'y', 'z', 'w' };
206                 for(unsigned i=0; i<assign.target.chain_len; ++i)
207                 {
208                         unsigned component = assign.target.chain[i];
209                         switch(static_cast<Assignment::Target::ChainType>(component&0xC0))
210                         {
211                         case Assignment::Target::MEMBER:
212                                 text += format(" .%d", component&0x3F);
213                                 break;
214                         case Assignment::Target::SWIZZLE:
215                                 text += " .";
216                                 for(unsigned j=0; j<4; ++j)
217                                         if(component&(1<<j))
218                                                 text += swizzle[j];
219                                 break;
220                         case Assignment::Target::ARRAY:
221                                 text += format(" [%d]", component&0x3F);
222                                 break;
223                         }
224                 }
225                 append(text);
226         }
227         assign.left->visit(*this);
228         last_branch();
229         assign.right->visit(*this);
230         end_sub();
231 }
232
233 void DumpTree::visit(TernaryExpression &ternary)
234 {
235         append(ternary, format("Ternary: %s%s -> %s", ternary.oper->token, ternary.oper->token2, format_type(ternary.type)));
236         begin_sub();
237         ternary.condition->visit(*this);
238         ternary.true_expr->visit(*this);
239         last_branch();
240         ternary.false_expr->visit(*this);
241         end_sub();
242 }
243
244 void DumpTree::visit(FunctionCall &call)
245 {
246         string head = "Function call: ";
247         if(call.declaration)
248                 head += format("%%%d ", get_label(*call.declaration));
249         head += call.name;
250         if(call.constructor)
251                 head += " (constructor)";
252         head += format(" -> %s", format_type(call.type));
253         append(call, head);
254
255         begin_sub();
256         for(NodeArray<Expression>::const_iterator i=call.arguments.begin(); i!=call.arguments.end(); )
257         {
258                 NodeArray<Expression>::const_iterator j = increment(i, call.arguments);
259                 (*j)->visit(*this);
260         }
261         end_sub();
262 }
263
264 void DumpTree::visit(ExpressionStatement &expr)
265 {
266         append(expr, "expr;");
267         append_subtree(*expr.expression);
268 }
269
270 void DumpTree::visit(Import &import)
271 {
272         append(import, format("import %s", import.module));
273 }
274
275 void DumpTree::visit(Precision &prec)
276 {
277         append(prec, format("precision %s %s", prec.precision, prec.type));
278 }
279
280 void DumpTree::visit(Layout &layout)
281 {
282         append(layout, "Layout");
283         begin_sub();
284         for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); )
285         {
286                 vector<Layout::Qualifier>::const_iterator j = increment(i, layout.qualifiers);
287                 string qualifier = j->name;
288                 if(j->has_value)
289                         qualifier += format("=%d", j->value);
290                 append(qualifier);
291         }
292         end_sub();
293 }
294
295 void DumpTree::visit(InterfaceLayout &layout)
296 {
297         append(layout, format("Layout: %s", layout.interface));
298         append_subtree(layout.layout);
299 }
300
301 void DumpTree::visit(BasicTypeDeclaration &type)
302 {
303         append(type, format("%%%d typedef %s", get_label(type), type.name));
304
305         vector<Branch> branches;
306         if(type.base_type)
307                 branches.push_back(format("%s: %%%d %s", (type.kind==BasicTypeDeclaration::ALIAS ? "Alias of" : "Base"), get_label(*type.base_type), type.base_type->name));
308         if(type.kind==BasicTypeDeclaration::VECTOR)
309                 branches.push_back(format("Vector: %d", type.size));
310         else if(type.kind==BasicTypeDeclaration::MATRIX)
311                 branches.push_back(format("Matrix: %dx%d", type.size&0xFFFF, type.size>>16));
312         append_subtree(branches);
313 }
314
315 void DumpTree::visit(ImageTypeDeclaration &type)
316 {
317         static const char *dims[] = { "1D", "2D", "3D", "Cube" };
318
319         append(type, format("%%%d typedef %s", get_label(type), type.name));
320
321         vector<Branch> branches;
322         branches.push_back(format("Dimensions: %s%s", dims[type.dimensions-1], (type.array ? " array" : "")));
323         if(type.base_type)
324                 branches.push_back(format("Element type: %%%d %s", get_label(*type.base_type), type.base_type->name));
325         if(type.shadow)
326                 branches.push_back("Shadow");
327         append_subtree(branches);
328 }
329
330 void DumpTree::visit(StructDeclaration &strct)
331 {
332         append(strct, format("%%%d struct %s", get_label(strct), strct.name));
333         append_subtree(strct.members);
334 }
335
336 void DumpTree::visit(VariableDeclaration &var)
337 {
338         string decl = format("%%%d ", get_label(var));
339         if(var.constant)
340                 decl += "const ";
341         if(!var.interpolation.empty())
342                 decl += format("%s ", var.interpolation);
343         if(!var.sampling.empty())
344                 decl += format("%s ", var.sampling);
345         if(!var.interface.empty())
346                 decl += format("%s ", var.interface);
347         if(!var.precision.empty())
348                 decl += format("%s ", var.precision);
349         decl += format("%s %s", var.type, var.name);
350         if(var.source==BUILTIN_SOURCE)
351                 decl += " (builtin)";
352         else if(var.linked_declaration)
353                 decl += " (linked)";
354         append(var, decl);
355
356         vector<Branch> branches;
357         if(var.type_declaration)
358                 branches.push_back(format("Type: %%%d %s", get_label(*var.type_declaration), var.type_declaration->name));
359         if(var.layout)
360                 branches.push_back(var.layout.get());
361         if(var.array)
362         {
363                 if(var.array_size)
364                         branches.push_back(Branch("Array []", var.array_size.get()));
365                 else
366                         branches.push_back("Array []");
367         }
368         if(var.init_expression)
369                 branches.push_back(var.init_expression.get());
370         append_subtree(branches);
371 }
372
373 void DumpTree::visit(InterfaceBlock &iface)
374 {
375         string head;
376         if(!iface.instance_name.empty())
377                 head += format("%%%d ", get_label(iface));
378         head += format("%s %s", iface.interface, iface.block_name);
379         if(!iface.instance_name.empty())
380                 head += format(" %s", iface.instance_name);
381         if(iface.array)
382                 head += "[]";
383         if(iface.source==BUILTIN_SOURCE)
384                 head += " (builtin)";
385         else if(iface.linked_block)
386                 head += " (linked)";
387         append(iface, head);
388
389         vector<Branch> branches;
390         if(iface.type_declaration)
391                 branches.push_back(format("Type: %%%d %s", get_label(*iface.type_declaration), iface.type_declaration->name));
392         if(iface.layout)
393                 branches.push_back(iface.layout.get());
394         if(iface.members)
395                 branches.push_back(iface.members.get());
396         append_subtree(branches);
397 }
398
399 void DumpTree::visit(FunctionDeclaration &func)
400 {
401         string text = format("%%%d %s %s%s", get_label(func), func.return_type, func.name, (func.signature.empty() ? "(?)" : func.signature));
402         if(func.source==BUILTIN_SOURCE)
403                 text += " (builtin)";
404         else if(!func.definition)
405                 text += " (undefined)";
406         append(func, text);
407
408         begin_sub();
409         if(func.return_type_declaration)
410                 append(format("Return type: %%%d %s", get_label(*func.return_type_declaration), func.return_type_declaration->name));
411         for(NodeArray<VariableDeclaration>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
412                 (*i)->visit(*this);
413         last_branch();
414         if(func.definition==&func)
415                 func.body.visit(*this);
416         else if(func.definition)
417                 append(format("Definition: %%%d", get_label(*func.definition)));
418         end_sub();
419 }
420
421 void DumpTree::visit(Conditional &cond)
422 {
423         append(cond, "if()");
424
425         vector<Branch> branches;
426         branches.push_back(cond.condition.get());
427         branches.push_back(Branch("then", &cond.body));
428         if(!cond.else_body.body.empty())
429                 branches.push_back(Branch("else", &cond.else_body));
430         append_subtree(branches);
431 }
432
433 void DumpTree::visit(Iteration &iter)
434 {
435         append(iter, "for()");
436
437         vector<Branch> branches;
438         if(iter.init_statement)
439                 branches.push_back(Branch("Initialization", iter.init_statement.get()));
440         if(iter.condition)
441                 branches.push_back(Branch("Condition", iter.condition.get()));
442         if(iter.loop_expression)
443                 branches.push_back(Branch("Loop", iter.loop_expression.get()));
444         branches.push_back(&iter.body);
445         append_subtree(branches);
446 }
447
448 void DumpTree::visit(Passthrough &pass)
449 {
450         append(pass, (pass.subscript ? "passthrough[]" : "passthrough"));
451         if(pass.subscript)
452                 append_subtree(*pass.subscript);
453 }
454
455 void DumpTree::visit(Return &ret)
456 {
457         append(ret, (ret.expression ? "return" : "return;"));
458         if(ret.expression)
459                 append_subtree(*ret.expression);
460 }
461
462 void DumpTree::visit(Jump &jump)
463 {
464         append(jump, format("%s;", jump.keyword));
465 }
466
467 } // namespace SL
468 } // namespace GL
469 } // namespace Msp