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