]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/debug.cpp
Transform interface block contents into structs
[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(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         annotated_branch(text, *unary.expression);
185 }
186
187 void DumpTree::visit(BinaryExpression &binary)
188 {
189         append(format("Binary: %s -> %s", (binary.oper->token[0]=='[' ? "[]" : binary.oper->token), 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(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                 append(format("Target: %%%d %s %s", get_label(*assign.target_declaration), assign.target_declaration->type, assign.target_declaration->name));
203         assign.left->visit(*this);
204         last_branch();
205         assign.right->visit(*this);
206         end_sub();
207 }
208
209 void DumpTree::visit(FunctionCall &call)
210 {
211         string head = "Function call: ";
212         if(call.declaration)
213                 head += format("%%%d ", get_label(*call.declaration));
214         head += call.name;
215         if(call.constructor)
216                 head += " (constructor)";
217         head += format(" -> %s", format_type(call.type));
218         append(head);
219
220         begin_sub();
221         for(NodeArray<Expression>::const_iterator i=call.arguments.begin(); i!=call.arguments.end(); )
222         {
223                 NodeArray<Expression>::const_iterator j = increment(i, call.arguments);
224                 (*j)->visit(*this);
225         }
226         end_sub();
227 }
228
229 void DumpTree::visit(ExpressionStatement &expr)
230 {
231         annotated_branch("expr;", *expr.expression);
232 }
233
234 void DumpTree::visit(Import &import)
235 {
236         append(format("import %s", import.module));
237 }
238
239 void DumpTree::visit(Precision &prec)
240 {
241         append(format("precision %s %s", prec.precision, prec.type));
242 }
243
244 void DumpTree::visit(Layout &layout)
245 {
246         append("Layout");
247         begin_sub();
248         for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); )
249         {
250                 vector<Layout::Qualifier>::const_iterator j = increment(i, layout.qualifiers);
251                 string qualifier = j->name;
252                 if(j->has_value)
253                         qualifier += format("=%d", j->value);
254                 append(qualifier);
255         }
256         end_sub();
257 }
258
259 void DumpTree::visit(InterfaceLayout &layout)
260 {
261         annotated_branch(format("Layout: %s", layout.interface), layout.layout);
262 }
263
264 void DumpTree::visit(BasicTypeDeclaration &type)
265 {
266         append(format("%%%d typedef %s", get_label(type), type.name));
267
268         vector<Branch> branches;
269         if(type.base_type)
270                 branches.push_back(format("%s: %%%d %s", (type.kind==BasicTypeDeclaration::ALIAS ? "Alias of" : "Base"), get_label(*type.base_type), type.base_type->name));
271         if(type.kind==BasicTypeDeclaration::VECTOR)
272                 branches.push_back(format("Vector: %d", type.size));
273         else if(type.kind==BasicTypeDeclaration::MATRIX)
274                 branches.push_back(format("Matrix: %dx%d", type.size&0xFFFF, type.size>>16));
275         append_subtree(branches);
276 }
277
278 void DumpTree::visit(ImageTypeDeclaration &type)
279 {
280         static const char *dims[] = { "1D", "2D", "3D", "Cube" };
281
282         append(format("%%%d typedef %s", get_label(type), type.name));
283
284         vector<Branch> branches;
285         branches.push_back(format("Dimensions: %s%s", dims[type.dimensions-1], (type.array ? " array" : "")));
286         if(type.base_type)
287                 branches.push_back(format("Element type: %%%d %s", get_label(*type.base_type), type.base_type->name));
288         if(type.shadow)
289                 branches.push_back("Shadow");
290         append_subtree(branches);
291 }
292
293 void DumpTree::visit(StructDeclaration &strct)
294 {
295         annotated_branch(format("%%%d struct %s", get_label(strct), strct.name), strct.members);
296 }
297
298 void DumpTree::visit(VariableDeclaration &var)
299 {
300         string decl = format("%%%d ", get_label(var));
301         if(var.constant)
302                 decl += "const ";
303         if(!var.interpolation.empty())
304                 decl += format("%s ", var.interpolation);
305         if(!var.sampling.empty())
306                 decl += format("%s ", var.sampling);
307         if(!var.interface.empty())
308                 decl += format("%s ", var.interface);
309         if(!var.precision.empty())
310                 decl += format("%s ", var.precision);
311         decl += format("%s %s", var.type, var.name);
312         if(var.source==BUILTIN_SOURCE)
313                 decl += " (builtin)";
314         else if(var.linked_declaration)
315                 decl += " (linked)";
316         append(decl);
317
318         vector<Branch> branches;
319         if(var.type_declaration)
320                 branches.push_back(format("Type: %%%d %s", get_label(*var.type_declaration), var.type_declaration->name));
321         if(var.layout)
322                 branches.push_back(var.layout.get());
323         if(var.array)
324         {
325                 if(var.array_size)
326                         branches.push_back(Branch("Array []", var.array_size.get()));
327                 else
328                         branches.push_back("Array []");
329         }
330         if(var.init_expression)
331                 branches.push_back(var.init_expression.get());
332         append_subtree(branches);
333 }
334
335 void DumpTree::visit(InterfaceBlock &block)
336 {
337         string head;
338         if(!block.instance_name.empty())
339                 head += format("%%%d ", get_label(block));
340         head += format("%s %s", block.interface, block.name);
341         if(!block.instance_name.empty())
342                 head += format(" %s", block.instance_name);
343         if(block.array)
344                 head += "[]";
345         if(block.source==BUILTIN_SOURCE)
346                 head += " (builtin)";
347         else if(block.linked_block)
348                 head += " (linked)";
349         append(head);
350
351         begin_sub();
352         last_branch();
353         if(block.type_declaration)
354                 append(format("Type: %%%d %s", get_label(*block.type_declaration), block.type_declaration->name));
355         else if(block.members)
356                 block.members->visit(*this);
357         end_sub();
358 }
359
360 void DumpTree::visit(FunctionDeclaration &func)
361 {
362         string text = format("%%%d %s %s", get_label(func), func.return_type, func.name);
363         if(func.source==BUILTIN_SOURCE)
364                 text += " (builtin)";
365         else if(!func.definition)
366                 text += " (undefined)";
367         append(text);
368
369         begin_sub();
370         for(NodeArray<VariableDeclaration>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
371                 (*i)->visit(*this);
372         last_branch();
373         if(func.definition==&func)
374                 func.body.visit(*this);
375         else if(func.definition)
376                 append(format("Definition: %%%d", get_label(*func.definition)));
377         end_sub();
378 }
379
380 void DumpTree::visit(Conditional &cond)
381 {
382         append("if()");
383
384         vector<Branch> branches;
385         branches.push_back(cond.condition.get());
386         branches.push_back(&cond.body);
387         if(!cond.else_body.body.empty())
388                 branches.push_back(&cond.else_body);
389         append_subtree(branches);
390 }
391
392 void DumpTree::visit(Iteration &iter)
393 {
394         append("for()");
395
396         begin_sub();
397         if(iter.init_statement)
398                 annotated_branch("Initialization", *iter.init_statement);
399         if(iter.condition)
400                 annotated_branch("Condition", *iter.condition);
401         if(iter.loop_expression)
402                 annotated_branch("Loop", *iter.loop_expression);
403         last_branch();
404         annotated_branch("Body", iter.body);
405         end_sub();
406 }
407
408 void DumpTree::visit(Passthrough &pass)
409 {
410         if(pass.subscript)
411                 annotated_branch("passthrough[]", *pass.subscript);
412         else
413                 append("passthrough;");
414 }
415
416 void DumpTree::visit(Return &ret)
417 {
418         if(ret.expression)
419                 annotated_branch("return", *ret.expression);
420         else
421                 append("return;");
422 }
423
424 void DumpTree::visit(Jump &jump)
425 {
426         append(format("%s;", jump.keyword));
427 }
428
429 } // namespace SL
430 } // namespace GL
431 } // namespace Msp