]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/debug.cpp
Store functions definitions in Stage
[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, StructDeclaration *>::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::begin_sub()
51 {
52         tree.back() = (tree.back()==BRANCH_LAST ? EMPTY : STRAIGHT);
53         tree.push_back(BRANCH);
54 }
55
56 void DumpTree::last_branch()
57 {
58         tree.back() = BRANCH_LAST;
59 }
60
61 void DumpTree::end_sub()
62 {
63         tree.pop_back();
64         if(tree.back()==STRAIGHT)
65                 tree.back() = BRANCH;
66 }
67
68 void DumpTree::annotated_branch(const string &annotation, Node &node)
69 {
70         append(annotation);
71         begin_sub();
72         last_branch();
73         node.visit(*this);
74         end_sub();
75 }
76
77 unsigned DumpTree::get_label(const Node &node)
78 {
79         unsigned &label = node_labels[&node];
80         if(!label)
81                 label = node_labels.size();
82         return label;
83 }
84
85 template<typename T>
86 typename T::const_iterator DumpTree::increment(typename T::const_iterator &iter, const T &container)
87 {
88         typename T::const_iterator ret = iter++;
89         if(iter==container.end())
90                 last_branch();
91         return ret;
92 }
93
94 void DumpTree::visit(Block &block)
95 {
96         append(format("Block %s", (block.use_braces ? "{}" : "(inline)")));
97         begin_sub();
98
99         for(std::map<string, VariableDeclaration *>::const_iterator i=block.variables.begin(); i!=block.variables.end(); ++i)
100                 append(format("Variable: %%%d %s %s", get_label(*i->second), i->second->type, i->first));
101
102         bool labeled_body = !block.variables.empty();
103         if(labeled_body)
104         {
105                 last_branch();
106                 append("Body");
107                 begin_sub();
108         }
109         for(NodeList<Statement>::const_iterator i=block.body.begin(); i!=block.body.end(); )
110         {
111                 NodeList<Statement>::const_iterator j = increment(i, block.body);
112                 (*j)->visit(*this);
113         }
114         if(labeled_body)
115                 end_sub();
116
117         end_sub();
118 }
119
120 void DumpTree::visit(Literal &literal)
121 {
122         append(format("Literal: %s", literal.token));
123 }
124
125 void DumpTree::visit(ParenthesizedExpression &parexpr)
126 {
127         annotated_branch("(expr)", *parexpr.expression);
128 }
129
130 void DumpTree::visit(VariableReference &var)
131 {
132         string text;
133         if(var.declaration)
134                 text += format("%%%d ", get_label(*var.declaration));
135         text += format("%s (var)", var.name);
136         append(text);
137 }
138
139 void DumpTree::visit(InterfaceBlockReference &iface)
140 {
141         string text;
142         if(iface.declaration)
143                 text += format("%%%d ", get_label(*iface.declaration));
144         text += format("%s (iface)", iface.name);
145         append(text);
146 }
147
148 void DumpTree::visit(MemberAccess &memacc)
149 {
150         string text = "Member access:";
151         if(memacc.declaration)
152                 text += format(" %%%d", get_label(*memacc.declaration));
153         text += format(" .%s", memacc.member);
154         annotated_branch(text, *memacc.left);
155 }
156
157 void DumpTree::visit(UnaryExpression &unary)
158 {
159         annotated_branch(format("Unary: %s, %sfix", unary.oper, (unary.prefix ? "pre" : "suff")), *unary.expression);
160 }
161
162 void DumpTree::visit(BinaryExpression &binary)
163 {
164         append(format("Binary: %s%s", binary.oper, binary.after));
165         begin_sub();
166         binary.left->visit(*this);
167         last_branch();
168         binary.right->visit(*this);
169         end_sub();
170 }
171
172 void DumpTree::visit(Assignment &assign)
173 {
174         append(format("Assignment: %s%s", assign.oper, (assign.self_referencing ? " (self-referencing)" : "")));
175         begin_sub();
176         if(assign.target_declaration)
177         {
178                 append(format("Target: %%%d %s %s", get_label(*assign.target_declaration), assign.target_declaration->type, assign.target_declaration->name));
179         }
180         assign.left->visit(*this);
181         last_branch();
182         assign.right->visit(*this);
183         end_sub();
184 }
185
186 void DumpTree::visit(FunctionCall &call)
187 {
188         string head = "Function call: ";
189         if(call.declaration)
190                 head += format("%%%d ", get_label(*call.declaration));
191         head += call.name;
192         if(call.constructor)
193                 head += " (constructor)";
194         append(head);
195
196         begin_sub();
197         for(NodeArray<Expression>::const_iterator i=call.arguments.begin(); i!=call.arguments.end(); )
198         {
199                 NodeArray<Expression>::const_iterator j = increment(i, call.arguments);
200                 (*j)->visit(*this);
201         }
202         end_sub();
203 }
204
205 void DumpTree::visit(ExpressionStatement &expr)
206 {
207         annotated_branch("expr;", *expr.expression);
208 }
209
210 void DumpTree::visit(Import &import)
211 {
212         append(format("import %s", import.module));
213 }
214
215 void DumpTree::visit(Precision &prec)
216 {
217         append(format("precision %s %s", prec.precision, prec.type));
218 }
219
220 void DumpTree::visit(Layout &layout)
221 {
222         append("Layout");
223         begin_sub();
224         for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); )
225         {
226                 vector<Layout::Qualifier>::const_iterator j = increment(i, layout.qualifiers);
227                 string qualifier = j->name;
228                 if(j->has_value)
229                         qualifier += format("=%d", j->value);
230                 append(qualifier);
231         }
232         end_sub();
233 }
234
235 void DumpTree::visit(InterfaceLayout &layout)
236 {
237         annotated_branch(format("Layout: %s", layout.interface), layout.layout);
238 }
239
240 void DumpTree::visit(StructDeclaration &strct)
241 {
242         annotated_branch(format("%%%d struct %s", get_label(strct), strct.name), strct.members);
243 }
244
245 void DumpTree::visit(VariableDeclaration &var)
246 {
247         string decl = format("%%%d ", get_label(var));
248         if(var.constant)
249                 decl += "const ";
250         if(!var.interpolation.empty())
251                 decl += format("%s ", var.interpolation);
252         if(!var.sampling.empty())
253                 decl += format("%s ", var.sampling);
254         if(!var.interface.empty())
255                 decl += format("%s ", var.interface);
256         if(!var.precision.empty())
257                 decl += format("%s ", var.precision);
258         decl += format("%s %s", var.type, var.name);
259         if(var.linked_declaration)
260                 decl += " (linked)";
261         append(decl);
262
263         begin_sub();
264         if(!var.array && !var.init_expression)
265                 last_branch();
266         if(var.layout)
267                 var.layout->visit(*this);
268
269         if(!var.init_expression)
270                 last_branch();
271         if(var.array)
272         {
273                 if(var.array_size)
274                         annotated_branch("Array []", *var.array_size);
275                 else
276                         append("Array []");
277         }
278
279         last_branch();
280         if(var.init_expression)
281                 var.init_expression->visit(*this);
282         end_sub();
283 }
284
285 void DumpTree::visit(InterfaceBlock &block)
286 {
287         string head;
288         if(!block.instance_name.empty())
289                 head += format("%%%d ", get_label(block));
290         head += format("%s %s", block.interface, block.name);
291         if(!block.instance_name.empty())
292                 head += format(" %s", block.instance_name);
293         if(block.array)
294                 head += "[]";
295         if(block.linked_block)
296                 head += " (linked)";
297         annotated_branch(head, block.members);
298 }
299
300 void DumpTree::visit(FunctionDeclaration &func)
301 {
302         append(format("%%%d %s %s()", get_label(func), func.return_type, func.name));
303         begin_sub();
304         for(NodeArray<VariableDeclaration>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
305                 (*i)->visit(*this);
306         if(func.definition)
307                 append(format("Definition: %%%d", get_label(*func.definition)));
308         last_branch();
309         func.body.visit(*this);
310         end_sub();
311 }
312
313 void DumpTree::visit(Conditional &cond)
314 {
315         append("if()");
316         begin_sub();
317         cond.condition->visit(*this);
318         if(cond.else_body.body.empty())
319                 last_branch();
320         cond.body.visit(*this);
321         if(!cond.else_body.body.empty())
322         {
323                 last_branch();
324                 cond.else_body.visit(*this);
325         }
326         end_sub();
327 }
328
329 void DumpTree::visit(Iteration &iter)
330 {
331         append("for()");
332         begin_sub();
333
334         if(iter.init_statement)
335                 annotated_branch("Initialization", *iter.init_statement);
336         if(iter.condition)
337                 annotated_branch("Condition", *iter.condition);
338         if(iter.loop_expression)
339                 annotated_branch("Loop", *iter.loop_expression);
340         last_branch();
341         annotated_branch("Body", iter.body);
342
343         end_sub();
344 }
345
346 void DumpTree::visit(Passthrough &pass)
347 {
348         append("passthrough");
349         if(pass.subscript)
350         {
351                 begin_sub();
352                 last_branch();
353                 pass.subscript->visit(*this);
354                 end_sub();
355         }
356 }
357
358 void DumpTree::visit(Return &ret)
359 {
360         if(ret.expression)
361                 annotated_branch("return", *ret.expression);
362         else
363                 append("return;");
364 }
365
366 void DumpTree::visit(Jump &jump)
367 {
368         append(format("%s;", jump.keyword));
369 }
370
371 } // namespace SL
372 } // namespace GL
373 } // namespace Msp