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