1 #include <msp/stringcodec/utf8.h>
2 #include <msp/strings/format.h>
11 const std::string &DumpTree::apply(Stage &stage)
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));
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));
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)
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);
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));
34 stage.content.visit(*this);
38 void DumpTree::append(const string &line)
40 StringCodec::Utf8::Encoder enc;
41 for(vector<TreeChars>::const_iterator i=tree.begin(); i!=tree.end(); )
43 enc.encode_char(*i++, formatted);
44 enc.encode_char((i==tree.end() ? REACH : EMPTY), formatted);
50 void DumpTree::begin_sub()
52 tree.back() = (tree.back()==BRANCH_LAST ? EMPTY : STRAIGHT);
53 tree.push_back(BRANCH);
56 void DumpTree::last_branch()
58 tree.back() = BRANCH_LAST;
61 void DumpTree::end_sub()
64 if(tree.back()==STRAIGHT)
68 void DumpTree::annotated_branch(const string &annotation, Node &node)
77 unsigned DumpTree::get_label(const Node &node)
79 unsigned &label = node_labels[&node];
81 label = node_labels.size();
86 typename T::const_iterator DumpTree::increment(typename T::const_iterator &iter, const T &container)
88 typename T::const_iterator ret = iter++;
89 if(iter==container.end())
94 void DumpTree::visit(Block &block)
96 append(format("Block %s", (block.use_braces ? "{}" : "(inline)")));
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));
102 bool labeled_body = !block.variables.empty();
109 for(NodeList<Statement>::const_iterator i=block.body.begin(); i!=block.body.end(); )
111 NodeList<Statement>::const_iterator j = increment(i, block.body);
120 void DumpTree::visit(Literal &literal)
122 append(format("Literal: %s", literal.token));
125 void DumpTree::visit(ParenthesizedExpression &parexpr)
127 annotated_branch("(expr)", *parexpr.expression);
130 void DumpTree::visit(VariableReference &var)
134 text += format("%%%d ", get_label(*var.declaration));
135 text += format("%s (var)", var.name);
139 void DumpTree::visit(InterfaceBlockReference &iface)
142 if(iface.declaration)
143 text += format("%%%d ", get_label(*iface.declaration));
144 text += format("%s (iface)", iface.name);
148 void DumpTree::visit(MemberAccess &memacc)
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);
157 void DumpTree::visit(UnaryExpression &unary)
159 annotated_branch(format("Unary: %s, %sfix", unary.oper, (unary.prefix ? "pre" : "suff")), *unary.expression);
162 void DumpTree::visit(BinaryExpression &binary)
164 append(format("Binary: %s%s", binary.oper, binary.after));
166 binary.left->visit(*this);
168 binary.right->visit(*this);
172 void DumpTree::visit(Assignment &assign)
174 append(format("Assignment: %s%s", assign.oper, (assign.self_referencing ? " (self-referencing)" : "")));
176 if(assign.target_declaration)
177 append(format("Target: %%%d %s %s", get_label(*assign.target_declaration), assign.target_declaration->type, assign.target_declaration->name));
178 assign.left->visit(*this);
180 assign.right->visit(*this);
184 void DumpTree::visit(FunctionCall &call)
186 string head = "Function call: ";
188 head += format("%%%d ", get_label(*call.declaration));
191 head += " (constructor)";
195 for(NodeArray<Expression>::const_iterator i=call.arguments.begin(); i!=call.arguments.end(); )
197 NodeArray<Expression>::const_iterator j = increment(i, call.arguments);
203 void DumpTree::visit(ExpressionStatement &expr)
205 annotated_branch("expr;", *expr.expression);
208 void DumpTree::visit(Import &import)
210 append(format("import %s", import.module));
213 void DumpTree::visit(Precision &prec)
215 append(format("precision %s %s", prec.precision, prec.type));
218 void DumpTree::visit(Layout &layout)
222 for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); )
224 vector<Layout::Qualifier>::const_iterator j = increment(i, layout.qualifiers);
225 string qualifier = j->name;
227 qualifier += format("=%d", j->value);
233 void DumpTree::visit(InterfaceLayout &layout)
235 annotated_branch(format("Layout: %s", layout.interface), layout.layout);
238 void DumpTree::visit(StructDeclaration &strct)
240 annotated_branch(format("%%%d struct %s", get_label(strct), strct.name), strct.members);
243 void DumpTree::visit(VariableDeclaration &var)
245 string decl = format("%%%d ", get_label(var));
248 if(!var.interpolation.empty())
249 decl += format("%s ", var.interpolation);
250 if(!var.sampling.empty())
251 decl += format("%s ", var.sampling);
252 if(!var.interface.empty())
253 decl += format("%s ", var.interface);
254 if(!var.precision.empty())
255 decl += format("%s ", var.precision);
256 decl += format("%s %s", var.type, var.name);
257 if(var.source==BUILTIN_SOURCE)
258 decl += " (builtin)";
259 else if(var.linked_declaration)
264 if(!var.array && !var.init_expression)
267 var.layout->visit(*this);
269 if(!var.init_expression)
274 annotated_branch("Array []", *var.array_size);
280 if(var.init_expression)
281 var.init_expression->visit(*this);
285 void DumpTree::visit(InterfaceBlock &block)
288 if(!block.instance_name.empty())
289 head += format("%%%d ", get_label(block));
290 head += format("%s %s", block.interface, block.name);
291 if(!block.instance_name.empty())
292 head += format(" %s", block.instance_name);
295 if(block.source==BUILTIN_SOURCE)
296 head += " (builtin)";
297 else if(block.linked_block)
299 annotated_branch(head, block.members);
302 void DumpTree::visit(FunctionDeclaration &func)
304 string text = format("%%%d %s %s", get_label(func), func.return_type, func.name);
305 if(func.source==BUILTIN_SOURCE)
306 text += " (builtin)";
307 else if(!func.definition)
308 text += " (undefined)";
311 for(NodeArray<VariableDeclaration>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
314 if(func.definition!=&func)
315 append(format("Definition: %%%d", get_label(*func.definition)));
317 func.body.visit(*this);
321 void DumpTree::visit(Conditional &cond)
325 cond.condition->visit(*this);
326 if(cond.else_body.body.empty())
328 cond.body.visit(*this);
329 if(!cond.else_body.body.empty())
332 cond.else_body.visit(*this);
337 void DumpTree::visit(Iteration &iter)
342 if(iter.init_statement)
343 annotated_branch("Initialization", *iter.init_statement);
345 annotated_branch("Condition", *iter.condition);
346 if(iter.loop_expression)
347 annotated_branch("Loop", *iter.loop_expression);
349 annotated_branch("Body", iter.body);
354 void DumpTree::visit(Passthrough &pass)
356 append("passthrough");
361 pass.subscript->visit(*this);
366 void DumpTree::visit(Return &ret)
369 annotated_branch("return", *ret.expression);
374 void DumpTree::visit(Jump &jump)
376 append(format("%s;", jump.keyword));