]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/debug.cpp
Inject builtins into the module
[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                 append(format("Target: %%%d %s %s", get_label(*assign.target_declaration), assign.target_declaration->type, assign.target_declaration->name));
178         assign.left->visit(*this);
179         last_branch();
180         assign.right->visit(*this);
181         end_sub();
182 }
183
184 void DumpTree::visit(FunctionCall &call)
185 {
186         string head = "Function call: ";
187         if(call.declaration)
188                 head += format("%%%d ", get_label(*call.declaration));
189         head += call.name;
190         if(call.constructor)
191                 head += " (constructor)";
192         append(head);
193
194         begin_sub();
195         for(NodeArray<Expression>::const_iterator i=call.arguments.begin(); i!=call.arguments.end(); )
196         {
197                 NodeArray<Expression>::const_iterator j = increment(i, call.arguments);
198                 (*j)->visit(*this);
199         }
200         end_sub();
201 }
202
203 void DumpTree::visit(ExpressionStatement &expr)
204 {
205         annotated_branch("expr;", *expr.expression);
206 }
207
208 void DumpTree::visit(Import &import)
209 {
210         append(format("import %s", import.module));
211 }
212
213 void DumpTree::visit(Precision &prec)
214 {
215         append(format("precision %s %s", prec.precision, prec.type));
216 }
217
218 void DumpTree::visit(Layout &layout)
219 {
220         append("Layout");
221         begin_sub();
222         for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); )
223         {
224                 vector<Layout::Qualifier>::const_iterator j = increment(i, layout.qualifiers);
225                 string qualifier = j->name;
226                 if(j->has_value)
227                         qualifier += format("=%d", j->value);
228                 append(qualifier);
229         }
230         end_sub();
231 }
232
233 void DumpTree::visit(InterfaceLayout &layout)
234 {
235         annotated_branch(format("Layout: %s", layout.interface), layout.layout);
236 }
237
238 void DumpTree::visit(StructDeclaration &strct)
239 {
240         annotated_branch(format("%%%d struct %s", get_label(strct), strct.name), strct.members);
241 }
242
243 void DumpTree::visit(VariableDeclaration &var)
244 {
245         string decl = format("%%%d ", get_label(var));
246         if(var.constant)
247                 decl += "const ";
248         if(!var.interpolation.empty())
249                 decl += format("%s ", var.interpolation);
250         if(!var.sampling.empty())
251                 decl += format("%s ", var.sampling);
252         if(!var.interface.empty())
253                 decl += format("%s ", var.interface);
254         if(!var.precision.empty())
255                 decl += format("%s ", var.precision);
256         decl += format("%s %s", var.type, var.name);
257         if(var.source==BUILTIN_SOURCE)
258                 decl += " (builtin)";
259         else 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.source==BUILTIN_SOURCE)
296                 head += " (builtin)";
297         else if(block.linked_block)
298                 head += " (linked)";
299         annotated_branch(head, block.members);
300 }
301
302 void DumpTree::visit(FunctionDeclaration &func)
303 {
304         string text = format("%%%d %s %s", get_label(func), func.return_type, func.name);
305         if(func.source==BUILTIN_SOURCE)
306                 text += " (builtin)";
307         else if(!func.definition)
308                 text += " (undefined)";
309         append(text);
310         begin_sub();
311         for(NodeArray<VariableDeclaration>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
312                 (*i)->visit(*this);
313         last_branch();
314         if(func.definition!=&func)
315                 append(format("Definition: %%%d", get_label(*func.definition)));
316         else
317                 func.body.visit(*this);
318         end_sub();
319 }
320
321 void DumpTree::visit(Conditional &cond)
322 {
323         append("if()");
324         begin_sub();
325         cond.condition->visit(*this);
326         if(cond.else_body.body.empty())
327                 last_branch();
328         cond.body.visit(*this);
329         if(!cond.else_body.body.empty())
330         {
331                 last_branch();
332                 cond.else_body.visit(*this);
333         }
334         end_sub();
335 }
336
337 void DumpTree::visit(Iteration &iter)
338 {
339         append("for()");
340         begin_sub();
341
342         if(iter.init_statement)
343                 annotated_branch("Initialization", *iter.init_statement);
344         if(iter.condition)
345                 annotated_branch("Condition", *iter.condition);
346         if(iter.loop_expression)
347                 annotated_branch("Loop", *iter.loop_expression);
348         last_branch();
349         annotated_branch("Body", iter.body);
350
351         end_sub();
352 }
353
354 void DumpTree::visit(Passthrough &pass)
355 {
356         append("passthrough");
357         if(pass.subscript)
358         {
359                 begin_sub();
360                 last_branch();
361                 pass.subscript->visit(*this);
362                 end_sub();
363         }
364 }
365
366 void DumpTree::visit(Return &ret)
367 {
368         if(ret.expression)
369                 annotated_branch("return", *ret.expression);
370         else
371                 append("return;");
372 }
373
374 void DumpTree::visit(Jump &jump)
375 {
376         append(format("%s;", jump.keyword));
377 }
378
379 } // namespace SL
380 } // namespace GL
381 } // namespace Msp