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