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 for(std::map<string, StructDeclaration *>::const_iterator i=stage.types.begin(); i!=stage.types.end(); ++i)
18 append(format("Type: %%%d %s", get_label(*i->second), i->first));
20 set<InterfaceBlock *> seen_interfaces;
21 for(std::map<string, InterfaceBlock *>::const_iterator i=stage.interface_blocks.begin(); i!=stage.interface_blocks.end(); ++i)
22 if(seen_interfaces.insert(i->second).second)
24 string text = format("Interface block: %%%d %s %s", get_label(*i->second), i->second->interface, i->second->name);
25 if(!i->second->instance_name.empty())
26 text += format(" %s", i->second->instance_name);
31 stage.content.visit(*this);
35 void DumpTree::append(const string &line)
37 StringCodec::Utf8::Encoder enc;
38 for(vector<TreeChars>::const_iterator i=tree.begin(); i!=tree.end(); )
40 enc.encode_char(*i++, formatted);
41 enc.encode_char((i==tree.end() ? REACH : EMPTY), formatted);
47 void DumpTree::begin_sub()
49 tree.back() = (tree.back()==BRANCH_LAST ? EMPTY : STRAIGHT);
50 tree.push_back(BRANCH);
53 void DumpTree::last_branch()
55 tree.back() = BRANCH_LAST;
58 void DumpTree::end_sub()
61 if(tree.back()==STRAIGHT)
65 void DumpTree::annotated_branch(const string &annotation, Node &node)
74 unsigned DumpTree::get_label(const Node &node)
76 unsigned &label = node_labels[&node];
78 label = node_labels.size();
83 typename T::const_iterator DumpTree::increment(typename T::const_iterator &iter, const T &container)
85 typename T::const_iterator ret = iter++;
86 if(iter==container.end())
91 void DumpTree::visit(Block &block)
93 append(format("Block %s", (block.use_braces ? "{}" : "(inline)")));
96 for(std::map<string, VariableDeclaration *>::const_iterator i=block.variables.begin(); i!=block.variables.end(); ++i)
97 append(format("Variable: %%%d %s %s", get_label(*i->second), i->second->type, i->first));
99 bool labeled_body = !block.variables.empty();
106 for(NodeList<Statement>::const_iterator i=block.body.begin(); i!=block.body.end(); )
108 NodeList<Statement>::const_iterator j = increment(i, block.body);
117 void DumpTree::visit(Literal &literal)
119 append(format("Literal: %s", literal.token));
122 void DumpTree::visit(ParenthesizedExpression &parexpr)
124 annotated_branch("(expr)", *parexpr.expression);
127 void DumpTree::visit(VariableReference &var)
131 text += format("%%%d ", get_label(*var.declaration));
132 text += format("%s (var)", var.name);
136 void DumpTree::visit(InterfaceBlockReference &iface)
139 if(iface.declaration)
140 text += format("%%%d ", get_label(*iface.declaration));
141 text += format("%s (iface)", iface.name);
145 void DumpTree::visit(MemberAccess &memacc)
147 string text = "Member access:";
148 if(memacc.declaration)
149 text += format(" %%%d", get_label(*memacc.declaration));
150 text += format(" .%s", memacc.member);
151 annotated_branch(text, *memacc.left);
154 void DumpTree::visit(UnaryExpression &unary)
156 annotated_branch(format("Unary: %s, %sfix", unary.oper, (unary.prefix ? "pre" : "suff")), *unary.expression);
159 void DumpTree::visit(BinaryExpression &binary)
161 append(format("Binary: %s%s", binary.oper, binary.after));
163 binary.left->visit(*this);
165 binary.right->visit(*this);
169 void DumpTree::visit(Assignment &assign)
171 append(format("Assignment: %s%s", assign.oper, (assign.self_referencing ? " (self-referencing)" : "")));
173 if(assign.target_declaration)
175 append(format("Target: %%%d %s %s", get_label(*assign.target_declaration), assign.target_declaration->type, assign.target_declaration->name));
177 assign.left->visit(*this);
179 assign.right->visit(*this);
183 void DumpTree::visit(FunctionCall &call)
185 string head = "Function call: ";
187 head += format("%%%d ", get_label(*call.declaration));
190 head += " (constructor)";
194 for(NodeArray<Expression>::const_iterator i=call.arguments.begin(); i!=call.arguments.end(); )
196 NodeArray<Expression>::const_iterator j = increment(i, call.arguments);
202 void DumpTree::visit(ExpressionStatement &expr)
204 annotated_branch("expr;", *expr.expression);
207 void DumpTree::visit(Import &import)
209 append(format("import %s", import.module));
212 void DumpTree::visit(Precision &prec)
214 append(format("precision %s %s", prec.precision, prec.type));
217 void DumpTree::visit(Layout &layout)
221 for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); )
223 vector<Layout::Qualifier>::const_iterator j = increment(i, layout.qualifiers);
224 string qualifier = j->name;
226 qualifier += format("=%d", j->value);
232 void DumpTree::visit(InterfaceLayout &layout)
234 annotated_branch(format("Layout: %s", layout.interface), layout.layout);
237 void DumpTree::visit(StructDeclaration &strct)
239 annotated_branch(format("%%%d struct %s", get_label(strct), strct.name), strct.members);
242 void DumpTree::visit(VariableDeclaration &var)
244 string decl = format("%%%d ", get_label(var));
247 if(!var.interpolation.empty())
248 decl += format("%s ", var.interpolation);
249 if(!var.sampling.empty())
250 decl += format("%s ", var.sampling);
251 if(!var.interface.empty())
252 decl += format("%s ", var.interface);
253 if(!var.precision.empty())
254 decl += format("%s ", var.precision);
255 decl += format("%s %s", var.type, var.name);
256 if(var.linked_declaration)
261 if(!var.array && !var.init_expression)
264 var.layout->visit(*this);
266 if(!var.init_expression)
271 annotated_branch("Array []", *var.array_size);
277 if(var.init_expression)
278 var.init_expression->visit(*this);
282 void DumpTree::visit(InterfaceBlock &block)
285 if(!block.instance_name.empty())
286 head += format("%%%d ", get_label(block));
287 head += format("%s %s", block.interface, block.name);
288 if(!block.instance_name.empty())
289 head += format(" %s", block.instance_name);
292 if(block.linked_block)
294 annotated_branch(head, block.members);
297 void DumpTree::visit(FunctionDeclaration &func)
299 append(format("%%%d %s %s()", get_label(func), func.return_type, func.name));
301 for(NodeArray<VariableDeclaration>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
304 append(format("Definition: %%%d", get_label(*func.definition)));
306 func.body.visit(*this);
310 void DumpTree::visit(Conditional &cond)
314 cond.condition->visit(*this);
315 if(cond.else_body.body.empty())
317 cond.body.visit(*this);
318 if(!cond.else_body.body.empty())
321 cond.else_body.visit(*this);
326 void DumpTree::visit(Iteration &iter)
331 if(iter.init_statement)
332 annotated_branch("Initialization", *iter.init_statement);
334 annotated_branch("Condition", *iter.condition);
335 if(iter.loop_expression)
336 annotated_branch("Loop", *iter.loop_expression);
338 annotated_branch("Body", iter.body);
343 void DumpTree::visit(Passthrough &pass)
345 append("passthrough");
350 pass.subscript->visit(*this);
355 void DumpTree::visit(Return &ret)
358 annotated_branch("return", *ret.expression);
363 void DumpTree::visit(Jump &jump)
365 append(format("%s;", jump.keyword));