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