]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/debug.cpp
Slightly refactor layout qualifiers in the GLSL parser
[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%s", assign.oper, (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 = "Function call: ";
161         if(call.declaration)
162                 head += format("%%%d ", get_label(*call.declaration));
163         head += call.name;
164         if(call.constructor)
165                 head += " (constructor)";
166         append(head);
167
168         begin_sub();
169         for(NodeArray<Expression>::const_iterator i=call.arguments.begin(); i!=call.arguments.end(); )
170         {
171                 NodeArray<Expression>::const_iterator j = increment(i, call.arguments);
172                 (*j)->visit(*this);
173         }
174         end_sub();
175 }
176
177 void DumpTree::visit(ExpressionStatement &expr)
178 {
179         annotated_branch("expr;", *expr.expression);
180 }
181
182 void DumpTree::visit(Import &import)
183 {
184         append(format("import %s", import.module));
185 }
186
187 void DumpTree::visit(Precision &prec)
188 {
189         append(format("precision %s %s", prec.precision, prec.type));
190 }
191
192 void DumpTree::visit(Layout &layout)
193 {
194         append("Layout");
195         begin_sub();
196         for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); )
197         {
198                 vector<Layout::Qualifier>::const_iterator j = increment(i, layout.qualifiers);
199                 string qualifier = j->name;
200                 if(j->has_value)
201                         qualifier += format("=%d", j->value);
202                 append(qualifier);
203         }
204         end_sub();
205 }
206
207 void DumpTree::visit(InterfaceLayout &layout)
208 {
209         annotated_branch(format("Layout: %s", layout.interface), layout.layout);
210 }
211
212 void DumpTree::visit(StructDeclaration &strct)
213 {
214         annotated_branch(format("%%%d struct %s", get_label(strct), strct.name), strct.members);
215 }
216
217 void DumpTree::visit(VariableDeclaration &var)
218 {
219         string decl = format("%%%d ", get_label(var));
220         if(var.constant)
221                 decl += "const ";
222         if(!var.interpolation.empty())
223                 decl += format("%s ", var.interpolation);
224         if(!var.sampling.empty())
225                 decl += format("%s ", var.sampling);
226         if(!var.interface.empty())
227                 decl += format("%s ", var.interface);
228         if(!var.precision.empty())
229                 decl += format("%s ", var.precision);
230         decl += format("%s %s", var.type, var.name);
231         if(var.linked_declaration)
232                 decl += " (linked)";
233         append(decl);
234
235         begin_sub();
236         if(!var.array && !var.init_expression)
237                 last_branch();
238         if(var.layout)
239                 var.layout->visit(*this);
240
241         if(!var.init_expression)
242                 last_branch();
243         if(var.array)
244                 annotated_branch("Array []", *var.array_size);
245
246         last_branch();
247         if(var.init_expression)
248                 var.init_expression->visit(*this);
249         end_sub();
250 }
251
252 void DumpTree::visit(InterfaceBlock &block)
253 {
254         annotated_branch(format("%s %s", block.interface, block.name), block.members);
255 }
256
257 void DumpTree::visit(FunctionDeclaration &func)
258 {
259         append(format("%%%d %s %s()", get_label(func), func.return_type, func.name));
260         begin_sub();
261         for(NodeArray<VariableDeclaration>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
262                 (*i)->visit(*this);
263         if(func.definition)
264                 append(format("Definition %%%d", get_label(*func.definition)));
265         last_branch();
266         func.body.visit(*this);
267         end_sub();
268 }
269
270 void DumpTree::visit(Conditional &cond)
271 {
272         append("if()");
273         begin_sub();
274         cond.condition->visit(*this);
275         if(cond.else_body.body.empty())
276                 last_branch();
277         cond.body.visit(*this);
278         if(!cond.else_body.body.empty())
279         {
280                 last_branch();
281                 cond.else_body.visit(*this);
282         }
283         end_sub();
284 }
285
286 void DumpTree::visit(Iteration &iter)
287 {
288         append("for()");
289         begin_sub();
290
291         if(iter.init_statement)
292                 annotated_branch("Initialization", *iter.init_statement);
293         if(iter.condition)
294                 annotated_branch("Condition", *iter.condition);
295         if(iter.loop_expression)
296                 annotated_branch("Loop", *iter.loop_expression);
297         last_branch();
298         annotated_branch("Body", iter.body);
299
300         end_sub();
301 }
302
303 void DumpTree::visit(Return &ret)
304 {
305         if(ret.expression)
306                 annotated_branch("return", *ret.expression);
307         else
308                 append("return;");
309 }
310
311 void DumpTree::visit(Jump &jump)
312 {
313         append(format("%s;", jump.keyword));
314 }
315
316 } // namespace SL
317 } // namespace GL
318 } // namespace Msp