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));
20 stage.content.visit(*this);
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 string text = "Member access:";
131 if(memacc.declaration)
132 text += format(" %%%d", get_label(*memacc.declaration));
133 text += format(" .%s", memacc.member);
134 annotated_branch(text, *memacc.left);
137 void DumpTree::visit(UnaryExpression &unary)
139 annotated_branch(format("Unary: %s, %sfix", unary.oper, (unary.prefix ? "pre" : "suff")), *unary.expression);
142 void DumpTree::visit(BinaryExpression &binary)
144 append(format("Binary: %s%s", binary.oper, binary.after));
146 binary.left->visit(*this);
148 binary.right->visit(*this);
152 void DumpTree::visit(Assignment &assign)
154 append(format("Assignment: %s%s", assign.oper, (assign.self_referencing ? " (self-referencing)" : "")));
156 assign.left->visit(*this);
158 assign.right->visit(*this);
162 void DumpTree::visit(FunctionCall &call)
164 string head = "Function call: ";
166 head += format("%%%d ", get_label(*call.declaration));
169 head += " (constructor)";
173 for(NodeArray<Expression>::const_iterator i=call.arguments.begin(); i!=call.arguments.end(); )
175 NodeArray<Expression>::const_iterator j = increment(i, call.arguments);
181 void DumpTree::visit(ExpressionStatement &expr)
183 annotated_branch("expr;", *expr.expression);
186 void DumpTree::visit(Import &import)
188 append(format("import %s", import.module));
191 void DumpTree::visit(Precision &prec)
193 append(format("precision %s %s", prec.precision, prec.type));
196 void DumpTree::visit(Layout &layout)
200 for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); )
202 vector<Layout::Qualifier>::const_iterator j = increment(i, layout.qualifiers);
203 string qualifier = j->name;
205 qualifier += format("=%d", j->value);
211 void DumpTree::visit(InterfaceLayout &layout)
213 annotated_branch(format("Layout: %s", layout.interface), layout.layout);
216 void DumpTree::visit(StructDeclaration &strct)
218 annotated_branch(format("%%%d struct %s", get_label(strct), strct.name), strct.members);
221 void DumpTree::visit(VariableDeclaration &var)
223 string decl = format("%%%d ", get_label(var));
226 if(!var.interpolation.empty())
227 decl += format("%s ", var.interpolation);
228 if(!var.sampling.empty())
229 decl += format("%s ", var.sampling);
230 if(!var.interface.empty())
231 decl += format("%s ", var.interface);
232 if(!var.precision.empty())
233 decl += format("%s ", var.precision);
234 decl += format("%s %s", var.type, var.name);
235 if(var.linked_declaration)
240 if(!var.array && !var.init_expression)
243 var.layout->visit(*this);
245 if(!var.init_expression)
250 annotated_branch("Array []", *var.array_size);
256 if(var.init_expression)
257 var.init_expression->visit(*this);
261 void DumpTree::visit(InterfaceBlock &block)
263 annotated_branch(format("%s %s", block.interface, block.name), block.members);
266 void DumpTree::visit(FunctionDeclaration &func)
268 append(format("%%%d %s %s()", get_label(func), func.return_type, func.name));
270 for(NodeArray<VariableDeclaration>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
273 append(format("Definition %%%d", get_label(*func.definition)));
275 func.body.visit(*this);
279 void DumpTree::visit(Conditional &cond)
283 cond.condition->visit(*this);
284 if(cond.else_body.body.empty())
286 cond.body.visit(*this);
287 if(!cond.else_body.body.empty())
290 cond.else_body.visit(*this);
295 void DumpTree::visit(Iteration &iter)
300 if(iter.init_statement)
301 annotated_branch("Initialization", *iter.init_statement);
303 annotated_branch("Condition", *iter.condition);
304 if(iter.loop_expression)
305 annotated_branch("Loop", *iter.loop_expression);
307 annotated_branch("Body", iter.body);
312 void DumpTree::visit(Passthrough &pass)
314 append("passthrough");
319 pass.subscript->visit(*this);
324 void DumpTree::visit(Return &ret)
327 annotated_branch("return", *ret.expression);
332 void DumpTree::visit(Jump &jump)
334 append(format("%s;", jump.keyword));