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 string text = format("Unary: %s, %sfix", unary.oper->token, (unary.oper->type==Operator::PREFIX ? "pre" : "post"));
160 annotated_branch(text, *unary.expression);
163 void DumpTree::visit(BinaryExpression &binary)
165 append(format("Binary: %s", (binary.oper->token[0]=='[' ? "[]" : binary.oper->token)));
167 binary.left->visit(*this);
169 binary.right->visit(*this);
173 void DumpTree::visit(Assignment &assign)
175 append(format("Assignment: %s%s", assign.oper->token, (assign.self_referencing ? " (self-referencing)" : "")));
177 if(assign.target_declaration)
178 append(format("Target: %%%d %s %s", get_label(*assign.target_declaration), assign.target_declaration->type, assign.target_declaration->name));
179 assign.left->visit(*this);
181 assign.right->visit(*this);
185 void DumpTree::visit(FunctionCall &call)
187 string head = "Function call: ";
189 head += format("%%%d ", get_label(*call.declaration));
192 head += " (constructor)";
196 for(NodeArray<Expression>::const_iterator i=call.arguments.begin(); i!=call.arguments.end(); )
198 NodeArray<Expression>::const_iterator j = increment(i, call.arguments);
204 void DumpTree::visit(ExpressionStatement &expr)
206 annotated_branch("expr;", *expr.expression);
209 void DumpTree::visit(Import &import)
211 append(format("import %s", import.module));
214 void DumpTree::visit(Precision &prec)
216 append(format("precision %s %s", prec.precision, prec.type));
219 void DumpTree::visit(Layout &layout)
223 for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); )
225 vector<Layout::Qualifier>::const_iterator j = increment(i, layout.qualifiers);
226 string qualifier = j->name;
228 qualifier += format("=%d", j->value);
234 void DumpTree::visit(InterfaceLayout &layout)
236 annotated_branch(format("Layout: %s", layout.interface), layout.layout);
239 void DumpTree::visit(StructDeclaration &strct)
241 annotated_branch(format("%%%d struct %s", get_label(strct), strct.name), strct.members);
244 void DumpTree::visit(VariableDeclaration &var)
246 string decl = format("%%%d ", get_label(var));
249 if(!var.interpolation.empty())
250 decl += format("%s ", var.interpolation);
251 if(!var.sampling.empty())
252 decl += format("%s ", var.sampling);
253 if(!var.interface.empty())
254 decl += format("%s ", var.interface);
255 if(!var.precision.empty())
256 decl += format("%s ", var.precision);
257 decl += format("%s %s", var.type, var.name);
258 if(var.source==BUILTIN_SOURCE)
259 decl += " (builtin)";
260 else if(var.linked_declaration)
265 if(!var.array && !var.init_expression)
268 var.layout->visit(*this);
270 if(!var.init_expression)
275 annotated_branch("Array []", *var.array_size);
281 if(var.init_expression)
282 var.init_expression->visit(*this);
286 void DumpTree::visit(InterfaceBlock &block)
289 if(!block.instance_name.empty())
290 head += format("%%%d ", get_label(block));
291 head += format("%s %s", block.interface, block.name);
292 if(!block.instance_name.empty())
293 head += format(" %s", block.instance_name);
296 if(block.source==BUILTIN_SOURCE)
297 head += " (builtin)";
298 else if(block.linked_block)
300 annotated_branch(head, block.members);
303 void DumpTree::visit(FunctionDeclaration &func)
305 string text = format("%%%d %s %s", get_label(func), func.return_type, func.name);
306 if(func.source==BUILTIN_SOURCE)
307 text += " (builtin)";
308 else if(!func.definition)
309 text += " (undefined)";
312 for(NodeArray<VariableDeclaration>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
315 if(func.definition==&func)
316 func.body.visit(*this);
317 else if(func.definition)
318 append(format("Definition: %%%d", get_label(*func.definition)));
322 void DumpTree::visit(Conditional &cond)
326 cond.condition->visit(*this);
327 if(cond.else_body.body.empty())
329 cond.body.visit(*this);
330 if(!cond.else_body.body.empty())
333 cond.else_body.visit(*this);
338 void DumpTree::visit(Iteration &iter)
343 if(iter.init_statement)
344 annotated_branch("Initialization", *iter.init_statement);
346 annotated_branch("Condition", *iter.condition);
347 if(iter.loop_expression)
348 annotated_branch("Loop", *iter.loop_expression);
350 annotated_branch("Body", iter.body);
355 void DumpTree::visit(Passthrough &pass)
357 append("passthrough");
362 pass.subscript->visit(*this);
367 void DumpTree::visit(Return &ret)
370 annotated_branch("return", *ret.expression);
375 void DumpTree::visit(Jump &jump)
377 append(format("%s;", jump.keyword));