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