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);
16 stage.content.visit(*this);
20 void DumpTree::append(const string &line)
22 StringCodec::Utf8::Encoder enc;
23 for(vector<TreeChars>::const_iterator i=tree.begin(); i!=tree.end(); )
25 enc.encode_char(*i++, formatted);
26 enc.encode_char((i==tree.end() ? REACH : EMPTY), formatted);
32 void DumpTree::begin_sub()
34 tree.back() = (tree.back()==BRANCH_LAST ? EMPTY : STRAIGHT);
35 tree.push_back(BRANCH);
38 void DumpTree::last_branch()
40 tree.back() = BRANCH_LAST;
43 void DumpTree::end_sub()
46 if(tree.back()==STRAIGHT)
50 void DumpTree::annotated_branch(const string &annotation, Node &node)
59 unsigned DumpTree::get_label(const Node &node)
61 unsigned &label = node_labels[&node];
63 label = node_labels.size();
68 typename T::const_iterator DumpTree::increment(typename T::const_iterator &iter, const T &container)
70 typename T::const_iterator ret = iter++;
71 if(iter==container.end())
76 void DumpTree::visit(Block &block)
78 append(format("Block %s", (block.use_braces ? "{}" : "(inline)")));
81 for(std::map<string, StructDeclaration *>::const_iterator i=block.types.begin(); i!=block.types.end(); ++i)
82 append(format("Type %%%d %s", get_label(*i->second), i->first));
84 for(std::map<string, VariableDeclaration *>::const_iterator i=block.variables.begin(); i!=block.variables.end(); ++i)
85 append(format("Variable %%%d %s %s", get_label(*i->second), i->second->type, i->first));
87 for(std::set<InterfaceBlock *>::const_iterator i=block.interfaces.begin(); i!=block.interfaces.end(); ++i)
88 append(format("Interface %%%d %s %s", get_label(*i->second), i->second->interface, i->second->name));
90 bool labeled_body = (!block.types.empty() || !block.variables.empty());
97 for(NodeList<Statement>::const_iterator i=block.body.begin(); i!=block.body.end(); )
99 NodeList<Statement>::const_iterator j = increment(i, block.body);
108 void DumpTree::visit(Literal &literal)
110 append(format("Literal: %s", literal.token));
113 void DumpTree::visit(ParenthesizedExpression &parexpr)
115 annotated_branch("(expr)", *parexpr.expression);
118 void DumpTree::visit(VariableReference &var)
122 text += format("%%%d ", get_label(*var.declaration));
127 void DumpTree::visit(MemberAccess &memacc)
129 string text = "Member access:";
130 if(memacc.declaration)
131 text += format(" %%%d", get_label(*memacc.declaration));
132 text += format(" .%s", memacc.member);
133 annotated_branch(text, *memacc.left);
136 void DumpTree::visit(UnaryExpression &unary)
138 annotated_branch(format("Unary: %s, %sfix", unary.oper, (unary.prefix ? "pre" : "suff")), *unary.expression);
141 void DumpTree::visit(BinaryExpression &binary)
143 append(format("Binary: %s%s", binary.oper, binary.after));
145 binary.left->visit(*this);
147 binary.right->visit(*this);
151 void DumpTree::visit(Assignment &assign)
153 append(format("Assignment: %s%s", assign.oper, (assign.self_referencing ? " (self-referencing)" : "")));
155 assign.left->visit(*this);
157 assign.right->visit(*this);
161 void DumpTree::visit(FunctionCall &call)
163 string head = "Function call: ";
165 head += format("%%%d ", get_label(*call.declaration));
168 head += " (constructor)";
172 for(NodeArray<Expression>::const_iterator i=call.arguments.begin(); i!=call.arguments.end(); )
174 NodeArray<Expression>::const_iterator j = increment(i, call.arguments);
180 void DumpTree::visit(ExpressionStatement &expr)
182 annotated_branch("expr;", *expr.expression);
185 void DumpTree::visit(Import &import)
187 append(format("import %s", import.module));
190 void DumpTree::visit(Precision &prec)
192 append(format("precision %s %s", prec.precision, prec.type));
195 void DumpTree::visit(Layout &layout)
199 for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); )
201 vector<Layout::Qualifier>::const_iterator j = increment(i, layout.qualifiers);
202 string qualifier = j->name;
204 qualifier += format("=%d", j->value);
210 void DumpTree::visit(InterfaceLayout &layout)
212 annotated_branch(format("Layout: %s", layout.interface), layout.layout);
215 void DumpTree::visit(StructDeclaration &strct)
217 annotated_branch(format("%%%d struct %s", get_label(strct), strct.name), strct.members);
220 void DumpTree::visit(VariableDeclaration &var)
222 string decl = format("%%%d ", get_label(var));
225 if(!var.interpolation.empty())
226 decl += format("%s ", var.interpolation);
227 if(!var.sampling.empty())
228 decl += format("%s ", var.sampling);
229 if(!var.interface.empty())
230 decl += format("%s ", var.interface);
231 if(!var.precision.empty())
232 decl += format("%s ", var.precision);
233 decl += format("%s %s", var.type, var.name);
234 if(var.linked_declaration)
239 if(!var.array && !var.init_expression)
242 var.layout->visit(*this);
244 if(!var.init_expression)
249 annotated_branch("Array []", *var.array_size);
255 if(var.init_expression)
256 var.init_expression->visit(*this);
260 void DumpTree::visit(InterfaceBlock &block)
262 annotated_branch(format("%s %s", block.interface, block.name), block.members);
265 void DumpTree::visit(FunctionDeclaration &func)
267 append(format("%%%d %s %s()", get_label(func), func.return_type, func.name));
269 for(NodeArray<VariableDeclaration>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
272 append(format("Definition %%%d", get_label(*func.definition)));
274 func.body.visit(*this);
278 void DumpTree::visit(Conditional &cond)
282 cond.condition->visit(*this);
283 if(cond.else_body.body.empty())
285 cond.body.visit(*this);
286 if(!cond.else_body.body.empty())
289 cond.else_body.visit(*this);
294 void DumpTree::visit(Iteration &iter)
299 if(iter.init_statement)
300 annotated_branch("Initialization", *iter.init_statement);
302 annotated_branch("Condition", *iter.condition);
303 if(iter.loop_expression)
304 annotated_branch("Loop", *iter.loop_expression);
306 annotated_branch("Body", iter.body);
311 void DumpTree::visit(Passthrough &pass)
313 append("passthrough");
318 pass.subscript->visit(*this);
323 void DumpTree::visit(Return &ret)
326 annotated_branch("return", *ret.expression);
331 void DumpTree::visit(Jump &jump)
333 append(format("%s;", jump.keyword));