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 stage.content.visit(*this);
21 void DumpTree::append(const string &line)
23 StringCodec::Utf8::Encoder enc;
24 for(vector<TreeChars>::const_iterator i=tree.begin(); i!=tree.end(); )
26 enc.encode_char(*i++, formatted);
27 enc.encode_char((i==tree.end() ? REACH : EMPTY), formatted);
33 void DumpTree::begin_sub()
35 tree.back() = (tree.back()==BRANCH_LAST ? EMPTY : STRAIGHT);
36 tree.push_back(BRANCH);
39 void DumpTree::last_branch()
41 tree.back() = BRANCH_LAST;
44 void DumpTree::end_sub()
47 if(tree.back()==STRAIGHT)
51 void DumpTree::annotated_branch(const string &annotation, Node &node)
60 unsigned DumpTree::get_label(const Node &node)
62 unsigned &label = node_labels[&node];
64 label = node_labels.size();
69 typename T::const_iterator DumpTree::increment(typename T::const_iterator &iter, const T &container)
71 typename T::const_iterator ret = iter++;
72 if(iter==container.end())
77 void DumpTree::visit(Block &block)
79 append(format("Block %s", (block.use_braces ? "{}" : "(inline)")));
82 for(std::map<string, StructDeclaration *>::const_iterator i=block.types.begin(); i!=block.types.end(); ++i)
83 append(format("Type: %%%d %s", get_label(*i->second), i->first));
85 for(std::map<string, VariableDeclaration *>::const_iterator i=block.variables.begin(); i!=block.variables.end(); ++i)
86 append(format("Variable: %%%d %s %s", get_label(*i->second), i->second->type, i->first));
88 for(std::set<InterfaceBlock *>::const_iterator i=block.interfaces.begin(); i!=block.interfaces.end(); ++i)
89 append(format("Interface: %%%d %s %s", get_label(*i->second), i->second->interface, i->second->name));
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 if(assign.target_declaration)
158 append(format("Target: %%%d %s %s", get_label(*assign.target_declaration), assign.target_declaration->type, assign.target_declaration->name));
160 assign.left->visit(*this);
162 assign.right->visit(*this);
166 void DumpTree::visit(FunctionCall &call)
168 string head = "Function call: ";
170 head += format("%%%d ", get_label(*call.declaration));
173 head += " (constructor)";
177 for(NodeArray<Expression>::const_iterator i=call.arguments.begin(); i!=call.arguments.end(); )
179 NodeArray<Expression>::const_iterator j = increment(i, call.arguments);
185 void DumpTree::visit(ExpressionStatement &expr)
187 annotated_branch("expr;", *expr.expression);
190 void DumpTree::visit(Import &import)
192 append(format("import %s", import.module));
195 void DumpTree::visit(Precision &prec)
197 append(format("precision %s %s", prec.precision, prec.type));
200 void DumpTree::visit(Layout &layout)
204 for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); )
206 vector<Layout::Qualifier>::const_iterator j = increment(i, layout.qualifiers);
207 string qualifier = j->name;
209 qualifier += format("=%d", j->value);
215 void DumpTree::visit(InterfaceLayout &layout)
217 annotated_branch(format("Layout: %s", layout.interface), layout.layout);
220 void DumpTree::visit(StructDeclaration &strct)
222 annotated_branch(format("%%%d struct %s", get_label(strct), strct.name), strct.members);
225 void DumpTree::visit(VariableDeclaration &var)
227 string decl = format("%%%d ", get_label(var));
230 if(!var.interpolation.empty())
231 decl += format("%s ", var.interpolation);
232 if(!var.sampling.empty())
233 decl += format("%s ", var.sampling);
234 if(!var.interface.empty())
235 decl += format("%s ", var.interface);
236 if(!var.precision.empty())
237 decl += format("%s ", var.precision);
238 decl += format("%s %s", var.type, var.name);
239 if(var.linked_declaration)
244 if(!var.array && !var.init_expression)
247 var.layout->visit(*this);
249 if(!var.init_expression)
254 annotated_branch("Array []", *var.array_size);
260 if(var.init_expression)
261 var.init_expression->visit(*this);
265 void DumpTree::visit(InterfaceBlock &block)
267 annotated_branch(format("%s %s", block.interface, block.name), block.members);
270 void DumpTree::visit(FunctionDeclaration &func)
272 append(format("%%%d %s %s()", get_label(func), func.return_type, func.name));
274 for(NodeArray<VariableDeclaration>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
277 append(format("Definition: %%%d", get_label(*func.definition)));
279 func.body.visit(*this);
283 void DumpTree::visit(Conditional &cond)
287 cond.condition->visit(*this);
288 if(cond.else_body.body.empty())
290 cond.body.visit(*this);
291 if(!cond.else_body.body.empty())
294 cond.else_body.visit(*this);
299 void DumpTree::visit(Iteration &iter)
304 if(iter.init_statement)
305 annotated_branch("Initialization", *iter.init_statement);
307 annotated_branch("Condition", *iter.condition);
308 if(iter.loop_expression)
309 annotated_branch("Loop", *iter.loop_expression);
311 annotated_branch("Body", iter.body);
316 void DumpTree::visit(Passthrough &pass)
318 append("passthrough");
323 pass.subscript->visit(*this);
328 void DumpTree::visit(Return &ret)
331 annotated_branch("return", *ret.expression);
336 void DumpTree::visit(Jump &jump)
338 append(format("%s;", jump.keyword));