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