]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/debug.cpp
Add a visitor to dump the AST for debugging purposes
[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         visit(stage.content);
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         annotated_branch(format("Member access: .%s", memacc.member), *memacc.left);
131 }
132
133 void DumpTree::visit(UnaryExpression &unary)
134 {
135         annotated_branch(format("Unary: %s, %sfix", unary.oper, (unary.prefix ? "pre" : "suff")), *unary.expression);
136 }
137
138 void DumpTree::visit(BinaryExpression &binary)
139 {
140         append(format("Binary: %s%s", binary.oper, binary.after));
141         begin_sub();
142         binary.left->visit(*this);
143         last_branch();
144         binary.right->visit(*this);
145         end_sub();
146 }
147
148 void DumpTree::visit(Assignment &assign)
149 {
150         append(format("Assignment%s", (assign.self_referencing ? " (self-referencing)" : "")));
151         begin_sub();
152         assign.left->visit(*this);
153         last_branch();
154         assign.right->visit(*this);
155         end_sub();
156 }
157
158 void DumpTree::visit(FunctionCall &call)
159 {
160         string head = format("Function call: %s", call.name);
161         if(call.declaration)
162                 head += format(", declaration %%%d", get_label(*call.declaration));
163         if(call.constructor)
164                 head += ", constructor";
165         append(head);
166
167         begin_sub();
168         for(NodeArray<Expression>::const_iterator i=call.arguments.begin(); i!=call.arguments.end(); )
169         {
170                 NodeArray<Expression>::const_iterator j = increment(i, call.arguments);
171                 (*j)->visit(*this);
172         }
173         end_sub();
174 }
175
176 void DumpTree::visit(ExpressionStatement &expr)
177 {
178         annotated_branch("expr;", *expr.expression);
179 }
180
181 void DumpTree::visit(Import &import)
182 {
183         append(format("import %s", import.module));
184 }
185
186 void DumpTree::visit(Precision &prec)
187 {
188         append(format("precision %s %s", prec.precision, prec.type));
189 }
190
191 void DumpTree::visit(Layout &layout)
192 {
193         append("Layout");
194         begin_sub();
195         for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); )
196         {
197                 vector<Layout::Qualifier>::const_iterator j = increment(i, layout.qualifiers);
198                 string qualifier = j->identifier;
199                 if(!j->value.empty())
200                         qualifier += format("=%s", j->value);
201                 append(qualifier);
202         }
203         end_sub();
204 }
205
206 void DumpTree::visit(InterfaceLayout &layout)
207 {
208         annotated_branch(format("Layout: %s", layout.interface), layout.layout);
209 }
210
211 void DumpTree::visit(StructDeclaration &strct)
212 {
213         annotated_branch(format("%%%d struct %s", get_label(strct), strct.name), strct.members);
214 }
215
216 void DumpTree::visit(VariableDeclaration &var)
217 {
218         string decl = format("%%%d ", get_label(var));
219         if(var.constant)
220                 decl += "const ";
221         if(!var.interpolation.empty())
222                 decl += format("%s ", var.interpolation);
223         if(!var.sampling.empty())
224                 decl += format("%s ", var.sampling);
225         if(!var.interface.empty())
226                 decl += format("%s ", var.interface);
227         if(!var.precision.empty())
228                 decl += format("%s ", var.precision);
229         decl += format("%s %s", var.type, var.name);
230         append(decl);
231
232         begin_sub();
233         if(!var.array && !var.init_expression)
234                 last_branch();
235         if(var.layout)
236                 var.layout->visit(*this);
237
238         if(!var.init_expression)
239                 last_branch();
240         if(var.array)
241                 annotated_branch("Array []", *var.array_size);
242
243         last_branch();
244         if(var.init_expression)
245                 var.init_expression->visit(*this);
246         end_sub();
247 }
248
249 void DumpTree::visit(InterfaceBlock &block)
250 {
251         annotated_branch(format("%s %s", block.interface, block.name), block.members);
252 }
253
254 void DumpTree::visit(FunctionDeclaration &func)
255 {
256         append(format("%%%d %s %s()", get_label(func), func.return_type, func.name));
257         begin_sub();
258         for(NodeArray<VariableDeclaration>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
259                 (*i)->visit(*this);
260         if(func.definition)
261                 append(format("Definition %%%d", get_label(*func.definition)));
262         last_branch();
263         func.body.visit(*this);
264         end_sub();
265 }
266
267 void DumpTree::visit(Conditional &cond)
268 {
269         append("if()");
270         begin_sub();
271         cond.condition->visit(*this);
272         if(cond.else_body.body.empty())
273                 last_branch();
274         cond.body.visit(*this);
275         if(!cond.else_body.body.empty())
276         {
277                 last_branch();
278                 cond.else_body.visit(*this);
279         }
280         end_sub();
281 }
282
283 void DumpTree::visit(Iteration &iter)
284 {
285         append("for()");
286         begin_sub();
287
288         if(iter.init_statement)
289                 annotated_branch("Initialization", *iter.init_statement);
290         if(iter.condition)
291                 annotated_branch("Condition", *iter.condition);
292         if(iter.loop_expression)
293                 annotated_branch("Loop", *iter.loop_expression);
294         last_branch();
295         annotated_branch("Body", iter.body);
296
297         end_sub();
298 }
299
300 void DumpTree::visit(Return &ret)
301 {
302         if(ret.expression)
303                 annotated_branch("return", *ret.expression);
304         else
305                 append("return;");
306 }
307
308 void DumpTree::visit(Jump &jump)
309 {
310         append(format("%s;", jump.keyword));
311 }
312
313 } // namespace SL
314 } // namespace GL
315 } // namespace Msp