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