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 set<InterfaceBlock *> seen_interfaces;
89 for(std::map<string, InterfaceBlock *>::const_iterator i=block.interfaces.begin(); i!=block.interfaces.end(); ++i)
90 if(seen_interfaces.insert(i->second).second)
92 string text = format("Interface: %%%d %s %s", get_label(*i->second), i->second->interface, i->second->name);
93 if(!i->second->instance_name.empty())
94 text += format(" %s", i->second->instance_name);
98 bool labeled_body = (!block.types.empty() || !block.variables.empty());
105 for(NodeList<Statement>::const_iterator i=block.body.begin(); i!=block.body.end(); )
107 NodeList<Statement>::const_iterator j = increment(i, block.body);
116 void DumpTree::visit(Literal &literal)
118 append(format("Literal: %s", literal.token));
121 void DumpTree::visit(ParenthesizedExpression &parexpr)
123 annotated_branch("(expr)", *parexpr.expression);
126 void DumpTree::visit(VariableReference &var)
130 text += format("%%%d ", get_label(*var.declaration));
131 text += format("%s (var)", var.name);
135 void DumpTree::visit(InterfaceBlockReference &iface)
138 if(iface.declaration)
139 text += format("%%%d ", get_label(*iface.declaration));
140 text += format("%s (iface)", iface.name);
144 void DumpTree::visit(MemberAccess &memacc)
146 string text = "Member access:";
147 if(memacc.declaration)
148 text += format(" %%%d", get_label(*memacc.declaration));
149 text += format(" .%s", memacc.member);
150 annotated_branch(text, *memacc.left);
153 void DumpTree::visit(UnaryExpression &unary)
155 annotated_branch(format("Unary: %s, %sfix", unary.oper, (unary.prefix ? "pre" : "suff")), *unary.expression);
158 void DumpTree::visit(BinaryExpression &binary)
160 append(format("Binary: %s%s", binary.oper, binary.after));
162 binary.left->visit(*this);
164 binary.right->visit(*this);
168 void DumpTree::visit(Assignment &assign)
170 append(format("Assignment: %s%s", assign.oper, (assign.self_referencing ? " (self-referencing)" : "")));
172 if(assign.target_declaration)
174 append(format("Target: %%%d %s %s", get_label(*assign.target_declaration), assign.target_declaration->type, assign.target_declaration->name));
176 assign.left->visit(*this);
178 assign.right->visit(*this);
182 void DumpTree::visit(FunctionCall &call)
184 string head = "Function call: ";
186 head += format("%%%d ", get_label(*call.declaration));
189 head += " (constructor)";
193 for(NodeArray<Expression>::const_iterator i=call.arguments.begin(); i!=call.arguments.end(); )
195 NodeArray<Expression>::const_iterator j = increment(i, call.arguments);
201 void DumpTree::visit(ExpressionStatement &expr)
203 annotated_branch("expr;", *expr.expression);
206 void DumpTree::visit(Import &import)
208 append(format("import %s", import.module));
211 void DumpTree::visit(Precision &prec)
213 append(format("precision %s %s", prec.precision, prec.type));
216 void DumpTree::visit(Layout &layout)
220 for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); )
222 vector<Layout::Qualifier>::const_iterator j = increment(i, layout.qualifiers);
223 string qualifier = j->name;
225 qualifier += format("=%d", j->value);
231 void DumpTree::visit(InterfaceLayout &layout)
233 annotated_branch(format("Layout: %s", layout.interface), layout.layout);
236 void DumpTree::visit(StructDeclaration &strct)
238 annotated_branch(format("%%%d struct %s", get_label(strct), strct.name), strct.members);
241 void DumpTree::visit(VariableDeclaration &var)
243 string decl = format("%%%d ", get_label(var));
246 if(!var.interpolation.empty())
247 decl += format("%s ", var.interpolation);
248 if(!var.sampling.empty())
249 decl += format("%s ", var.sampling);
250 if(!var.interface.empty())
251 decl += format("%s ", var.interface);
252 if(!var.precision.empty())
253 decl += format("%s ", var.precision);
254 decl += format("%s %s", var.type, var.name);
255 if(var.linked_declaration)
260 if(!var.array && !var.init_expression)
263 var.layout->visit(*this);
265 if(!var.init_expression)
270 annotated_branch("Array []", *var.array_size);
276 if(var.init_expression)
277 var.init_expression->visit(*this);
281 void DumpTree::visit(InterfaceBlock &block)
284 if(!block.instance_name.empty())
285 head += format("%%%d ", get_label(block));
286 head += format("%s %s", block.interface, block.name);
287 if(!block.instance_name.empty())
288 head += format(" %s", block.instance_name);
291 if(block.linked_block)
293 annotated_branch(head, block.members);
296 void DumpTree::visit(FunctionDeclaration &func)
298 append(format("%%%d %s %s()", get_label(func), func.return_type, func.name));
300 for(NodeArray<VariableDeclaration>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
303 append(format("Definition: %%%d", get_label(*func.definition)));
305 func.body.visit(*this);
309 void DumpTree::visit(Conditional &cond)
313 cond.condition->visit(*this);
314 if(cond.else_body.body.empty())
316 cond.body.visit(*this);
317 if(!cond.else_body.body.empty())
320 cond.else_body.visit(*this);
325 void DumpTree::visit(Iteration &iter)
330 if(iter.init_statement)
331 annotated_branch("Initialization", *iter.init_statement);
333 annotated_branch("Condition", *iter.condition);
334 if(iter.loop_expression)
335 annotated_branch("Loop", *iter.loop_expression);
337 annotated_branch("Body", iter.body);
342 void DumpTree::visit(Passthrough &pass)
344 append("passthrough");
349 pass.subscript->visit(*this);
354 void DumpTree::visit(Return &ret)
357 annotated_branch("return", *ret.expression);
362 void DumpTree::visit(Jump &jump)
364 append(format("%s;", jump.keyword));