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", (assign.self_referencing ? " (self-referencing)" : "")));
152 assign.left->visit(*this);
154 assign.right->visit(*this);
158 void DumpTree::visit(FunctionCall &call)
160 string head = format("Function call: %s", call.name);
162 head += format(", declaration %%%d", get_label(*call.declaration));
164 head += ", constructor";
168 for(NodeArray<Expression>::const_iterator i=call.arguments.begin(); i!=call.arguments.end(); )
170 NodeArray<Expression>::const_iterator j = increment(i, call.arguments);
176 void DumpTree::visit(ExpressionStatement &expr)
178 annotated_branch("expr;", *expr.expression);
181 void DumpTree::visit(Import &import)
183 append(format("import %s", import.module));
186 void DumpTree::visit(Precision &prec)
188 append(format("precision %s %s", prec.precision, prec.type));
191 void DumpTree::visit(Layout &layout)
195 for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); )
197 vector<Layout::Qualifier>::const_iterator j = increment(i, layout.qualifiers);
198 string qualifier = j->identifier;
199 if(!j->value.empty())
200 qualifier += format("=%s", j->value);
206 void DumpTree::visit(InterfaceLayout &layout)
208 annotated_branch(format("Layout: %s", layout.interface), layout.layout);
211 void DumpTree::visit(StructDeclaration &strct)
213 annotated_branch(format("%%%d struct %s", get_label(strct), strct.name), strct.members);
216 void DumpTree::visit(VariableDeclaration &var)
218 string decl = format("%%%d ", get_label(var));
221 if(!var.interpolation.empty())
222 decl += format("%s ", var.interpolation);
223 if(!var.sampling.empty())
224 decl += format("%s ", var.sampling);
225 if(!var.interface.empty())
226 decl += format("%s ", var.interface);
227 if(!var.precision.empty())
228 decl += format("%s ", var.precision);
229 decl += format("%s %s", var.type, var.name);
233 if(!var.array && !var.init_expression)
236 var.layout->visit(*this);
238 if(!var.init_expression)
241 annotated_branch("Array []", *var.array_size);
244 if(var.init_expression)
245 var.init_expression->visit(*this);
249 void DumpTree::visit(InterfaceBlock &block)
251 annotated_branch(format("%s %s", block.interface, block.name), block.members);
254 void DumpTree::visit(FunctionDeclaration &func)
256 append(format("%%%d %s %s()", get_label(func), func.return_type, func.name));
258 for(NodeArray<VariableDeclaration>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
261 append(format("Definition %%%d", get_label(*func.definition)));
263 func.body.visit(*this);
267 void DumpTree::visit(Conditional &cond)
271 cond.condition->visit(*this);
272 if(cond.else_body.body.empty())
274 cond.body.visit(*this);
275 if(!cond.else_body.body.empty())
278 cond.else_body.visit(*this);
283 void DumpTree::visit(Iteration &iter)
288 if(iter.init_statement)
289 annotated_branch("Initialization", *iter.init_statement);
291 annotated_branch("Condition", *iter.condition);
292 if(iter.loop_expression)
293 annotated_branch("Loop", *iter.loop_expression);
295 annotated_branch("Body", iter.body);
300 void DumpTree::visit(Return &ret)
303 annotated_branch("return", *ret.expression);
308 void DumpTree::visit(Jump &jump)
310 append(format("%s;", jump.keyword));