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 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));
24 void DumpTree::append(const string &line)
26 StringCodec::Utf8::Encoder enc;
27 for(vector<TreeChars>::const_iterator i=tree.begin(); i!=tree.end(); )
29 enc.encode_char(*i++, formatted);
30 enc.encode_char((i==tree.end() ? REACH : EMPTY), formatted);
36 void DumpTree::begin_sub()
38 tree.back() = (tree.back()==BRANCH_LAST ? EMPTY : STRAIGHT);
39 tree.push_back(BRANCH);
42 void DumpTree::last_branch()
44 tree.back() = BRANCH_LAST;
47 void DumpTree::end_sub()
50 if(tree.back()==STRAIGHT)
54 void DumpTree::annotated_branch(const string &annotation, Node &node)
63 unsigned DumpTree::get_label(const Node &node)
65 unsigned &label = node_labels[&node];
67 label = node_labels.size();
72 typename T::const_iterator DumpTree::increment(typename T::const_iterator &iter, const T &container)
74 typename T::const_iterator ret = iter++;
75 if(iter==container.end())
80 void DumpTree::visit(Block &block)
82 append(format("Block %s", (block.use_braces ? "{}" : "(inline)")));
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));
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));
91 bool labeled_body = (!block.types.empty() || !block.variables.empty());
98 for(NodeList<Statement>::const_iterator i=block.body.begin(); i!=block.body.end(); )
100 NodeList<Statement>::const_iterator j = increment(i, block.body);
109 void DumpTree::visit(Literal &literal)
111 append(format("Literal: %s", literal.token));
114 void DumpTree::visit(ParenthesizedExpression &parexpr)
116 annotated_branch("(expr)", *parexpr.expression);
119 void DumpTree::visit(VariableReference &var)
123 text += format("%%%d ", get_label(*var.declaration));
128 void DumpTree::visit(MemberAccess &memacc)
130 annotated_branch(format("Member access: .%s", memacc.member), *memacc.left);
133 void DumpTree::visit(UnaryExpression &unary)
135 annotated_branch(format("Unary: %s, %sfix", unary.oper, (unary.prefix ? "pre" : "suff")), *unary.expression);
138 void DumpTree::visit(BinaryExpression &binary)
140 append(format("Binary: %s%s", binary.oper, binary.after));
142 binary.left->visit(*this);
144 binary.right->visit(*this);
148 void DumpTree::visit(Assignment &assign)
150 append(format("Assignment: %s%s", assign.oper, (assign.self_referencing ? " (self-referencing)" : "")));
152 assign.left->visit(*this);
154 assign.right->visit(*this);
158 void DumpTree::visit(FunctionCall &call)
160 string head = "Function call: ";
162 head += format("%%%d ", get_label(*call.declaration));
165 head += " (constructor)";
169 for(NodeArray<Expression>::const_iterator i=call.arguments.begin(); i!=call.arguments.end(); )
171 NodeArray<Expression>::const_iterator j = increment(i, call.arguments);
177 void DumpTree::visit(ExpressionStatement &expr)
179 annotated_branch("expr;", *expr.expression);
182 void DumpTree::visit(Import &import)
184 append(format("import %s", import.module));
187 void DumpTree::visit(Precision &prec)
189 append(format("precision %s %s", prec.precision, prec.type));
192 void DumpTree::visit(Layout &layout)
196 for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); )
198 vector<Layout::Qualifier>::const_iterator j = increment(i, layout.qualifiers);
199 string qualifier = j->identifier;
200 if(!j->value.empty())
201 qualifier += format("=%s", j->value);
207 void DumpTree::visit(InterfaceLayout &layout)
209 annotated_branch(format("Layout: %s", layout.interface), layout.layout);
212 void DumpTree::visit(StructDeclaration &strct)
214 annotated_branch(format("%%%d struct %s", get_label(strct), strct.name), strct.members);
217 void DumpTree::visit(VariableDeclaration &var)
219 string decl = format("%%%d ", get_label(var));
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)
236 if(!var.array && !var.init_expression)
239 var.layout->visit(*this);
241 if(!var.init_expression)
244 annotated_branch("Array []", *var.array_size);
247 if(var.init_expression)
248 var.init_expression->visit(*this);
252 void DumpTree::visit(InterfaceBlock &block)
254 annotated_branch(format("%s %s", block.interface, block.name), block.members);
257 void DumpTree::visit(FunctionDeclaration &func)
259 append(format("%%%d %s %s()", get_label(func), func.return_type, func.name));
261 for(NodeArray<VariableDeclaration>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
264 append(format("Definition %%%d", get_label(*func.definition)));
266 func.body.visit(*this);
270 void DumpTree::visit(Conditional &cond)
274 cond.condition->visit(*this);
275 if(cond.else_body.body.empty())
277 cond.body.visit(*this);
278 if(!cond.else_body.body.empty())
281 cond.else_body.visit(*this);
286 void DumpTree::visit(Iteration &iter)
291 if(iter.init_statement)
292 annotated_branch("Initialization", *iter.init_statement);
294 annotated_branch("Condition", *iter.condition);
295 if(iter.loop_expression)
296 annotated_branch("Loop", *iter.loop_expression);
298 annotated_branch("Body", iter.body);
303 void DumpTree::visit(Return &ret)
306 annotated_branch("return", *ret.expression);
311 void DumpTree::visit(Jump &jump)
313 append(format("%s;", jump.keyword));