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, TypeDeclaration *>::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->block_name);
25 if(!i->second->instance_name.empty())
26 text += format(" %s", i->second->instance_name);
30 for(std::map<string, FunctionDeclaration *>::const_iterator i=stage.functions.begin(); i!=stage.functions.end(); ++i)
31 append(format("Function: %%%d %s", get_label(*i->second), i->first));
34 stage.content.visit(*this);
38 void DumpTree::append(const string &line)
40 StringCodec::Utf8::Encoder enc;
41 for(vector<TreeChars>::const_iterator i=tree.begin(); i!=tree.end(); )
43 enc.encode_char(*i++, formatted);
44 enc.encode_char((i==tree.end() ? REACH : EMPTY), formatted);
50 void DumpTree::append(const Node &node, const string &line)
52 append(format("<%d:%d> %s", node.source, node.line, line));
55 void DumpTree::append_subtree(const vector<Branch> &branches)
58 for(vector<Branch>::const_iterator i=branches.begin(); i!=branches.end(); )
60 vector<Branch>::const_iterator j = increment(i, branches);
64 annotated_branch(j->text, *j->node);
69 j->node->visit(*this);
74 void DumpTree::append_subtree(Node &node)
82 void DumpTree::begin_sub()
84 tree.back() = (tree.back()==BRANCH_LAST ? EMPTY : STRAIGHT);
85 tree.push_back(BRANCH);
88 void DumpTree::last_branch()
90 tree.back() = BRANCH_LAST;
93 void DumpTree::end_sub()
96 if(tree.back()==STRAIGHT)
100 void DumpTree::annotated_branch(const string &annotation, Node &node)
109 unsigned DumpTree::get_label(const Node &node)
111 unsigned &label = node_labels[&node];
113 label = node_labels.size();
117 string DumpTree::format_type(TypeDeclaration *type)
119 return (type ? type->name : "?");
123 typename T::const_iterator DumpTree::increment(typename T::const_iterator &iter, const T &container)
125 typename T::const_iterator ret = iter++;
126 if(iter==container.end())
131 void DumpTree::visit(Block &block)
133 append(block, format("Block %s", (block.use_braces ? "{}" : "(inline)")));
136 for(std::map<string, VariableDeclaration *>::const_iterator i=block.variables.begin(); i!=block.variables.end(); ++i)
137 append(format("Variable: %%%d %s %s", get_label(*i->second), i->second->type, i->first));
139 bool labeled_body = !block.variables.empty();
146 for(NodeList<Statement>::const_iterator i=block.body.begin(); i!=block.body.end(); )
148 NodeList<Statement>::const_iterator j = increment(i, block.body);
157 void DumpTree::visit(Literal &literal)
159 append(literal, format("Literal: %s -> %s", literal.token, format_type(literal.type)));
162 void DumpTree::visit(VariableReference &var)
166 text += format("%%%d ", get_label(*var.declaration));
167 text += format("%s (var) -> %s", var.name, format_type(var.type));
171 void DumpTree::visit(InterfaceBlockReference &iface)
174 if(iface.declaration)
175 text += format("%%%d ", get_label(*iface.declaration));
176 text += format("%s (iface) -> %s", iface.name, format_type(iface.type));
180 void DumpTree::visit(MemberAccess &memacc)
182 string text = "Member access:";
183 if(memacc.declaration)
184 text += format(" %%%d", get_label(*memacc.declaration));
185 text += format(" .%s -> %s", memacc.member, format_type(memacc.type));
186 append(memacc, text);
187 append_subtree(*memacc.left);
190 void DumpTree::visit(Swizzle &swizzle)
192 static const char components[4] = { 'x', 'y', 'z', 'w' };
193 string text = "Swizzle: .";
194 for(unsigned i=0; i<swizzle.count; ++i)
195 text += components[swizzle.components[i]];
196 text += format(" -> %s", format_type(swizzle.type));
197 append(swizzle, text);
198 append_subtree(*swizzle.left);
201 void DumpTree::visit(UnaryExpression &unary)
203 string text = format("Unary: %s, %sfix -> %s", unary.oper->token, (unary.oper->type==Operator::PREFIX ? "pre" : "post"), format_type(unary.type));
205 append_subtree(*unary.expression);
208 void DumpTree::visit(BinaryExpression &binary)
210 append(binary, format("Binary: %s%s -> %s", binary.oper->token, binary.oper->token2, format_type(binary.type)));
212 binary.left->visit(*this);
214 binary.right->visit(*this);
218 void DumpTree::visit(Assignment &assign)
220 append(assign, format("Assignment: %s%s -> %s", assign.oper->token, (assign.self_referencing ? " (self-referencing)" : ""), format_type(assign.type)));
222 if(assign.target.declaration)
224 string text = format("Target: %%%d", get_label(*assign.target.declaration));
226 static const char swizzle[4] = { 'x', 'y', 'z', 'w' };
227 for(unsigned i=0; i<assign.target.chain_len; ++i)
229 unsigned component = assign.target.chain[i];
230 switch(static_cast<Assignment::Target::ChainType>(component&0xC0))
232 case Assignment::Target::MEMBER:
233 text += format(" .%d", component&0x3F);
235 case Assignment::Target::SWIZZLE:
237 for(unsigned j=0; j<4; ++j)
241 case Assignment::Target::ARRAY:
242 text += format(" [%d]", component&0x3F);
248 assign.left->visit(*this);
250 assign.right->visit(*this);
254 void DumpTree::visit(TernaryExpression &ternary)
256 append(ternary, format("Ternary: %s%s -> %s", ternary.oper->token, ternary.oper->token2, format_type(ternary.type)));
258 ternary.condition->visit(*this);
259 ternary.true_expr->visit(*this);
261 ternary.false_expr->visit(*this);
265 void DumpTree::visit(FunctionCall &call)
267 string head = "Function call: ";
269 head += format("%%%d ", get_label(*call.declaration));
272 head += " (constructor)";
273 head += format(" -> %s", format_type(call.type));
277 for(NodeArray<Expression>::const_iterator i=call.arguments.begin(); i!=call.arguments.end(); )
279 NodeArray<Expression>::const_iterator j = increment(i, call.arguments);
285 void DumpTree::visit(ExpressionStatement &expr)
287 append(expr, "expr;");
288 append_subtree(*expr.expression);
291 void DumpTree::visit(Import &import)
293 append(import, format("import %s", import.module));
296 void DumpTree::visit(Precision &prec)
298 append(prec, format("precision %s %s", prec.precision, prec.type));
301 void DumpTree::visit(Layout &layout)
303 append(layout, "Layout");
305 for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); )
307 vector<Layout::Qualifier>::const_iterator j = increment(i, layout.qualifiers);
308 string qualifier = j->name;
310 qualifier += format("=%d", j->value);
316 void DumpTree::visit(InterfaceLayout &layout)
318 append(layout, format("Layout: %s", layout.interface));
319 append_subtree(layout.layout);
322 void DumpTree::visit(BasicTypeDeclaration &type)
324 append(type, format("%%%d typedef %s", get_label(type), type.name));
326 vector<Branch> branches;
328 branches.push_back(format("%s: %%%d %s", (type.kind==BasicTypeDeclaration::ALIAS ? "Alias of" : "Base"), get_label(*type.base_type), type.base_type->name));
329 if(type.kind==BasicTypeDeclaration::VECTOR)
330 branches.push_back(format("Vector: %d", type.size));
331 else if(type.kind==BasicTypeDeclaration::MATRIX)
332 branches.push_back(format("Matrix: %dx%d", type.size&0xFFFF, type.size>>16));
333 append_subtree(branches);
336 void DumpTree::visit(ImageTypeDeclaration &type)
338 static const char *dims[] = { "1D", "2D", "3D", "Cube" };
340 append(type, format("%%%d typedef %s", get_label(type), type.name));
342 vector<Branch> branches;
343 branches.push_back(format("Dimensions: %s%s", dims[type.dimensions-1], (type.array ? " array" : "")));
345 branches.push_back(format("Element type: %%%d %s", get_label(*type.base_type), type.base_type->name));
347 branches.push_back("Shadow");
348 append_subtree(branches);
351 void DumpTree::visit(StructDeclaration &strct)
353 append(strct, format("%%%d struct %s", get_label(strct), strct.name));
354 append_subtree(strct.members);
357 void DumpTree::visit(VariableDeclaration &var)
359 string decl = format("%%%d ", get_label(var));
362 if(!var.interpolation.empty())
363 decl += format("%s ", var.interpolation);
364 if(!var.sampling.empty())
365 decl += format("%s ", var.sampling);
366 if(!var.interface.empty())
367 decl += format("%s ", var.interface);
368 if(!var.precision.empty())
369 decl += format("%s ", var.precision);
370 decl += format("%s %s", var.type, var.name);
371 if(var.source==BUILTIN_SOURCE)
372 decl += " (builtin)";
373 else if(var.linked_declaration)
377 vector<Branch> branches;
378 if(var.type_declaration)
379 branches.push_back(format("Type: %%%d %s", get_label(*var.type_declaration), var.type_declaration->name));
381 branches.push_back(var.layout.get());
385 branches.push_back(Branch("Array []", var.array_size.get()));
387 branches.push_back("Array []");
389 if(var.init_expression)
390 branches.push_back(var.init_expression.get());
391 append_subtree(branches);
394 void DumpTree::visit(InterfaceBlock &iface)
397 if(!iface.instance_name.empty())
398 head += format("%%%d ", get_label(iface));
399 head += format("%s %s", iface.interface, iface.block_name);
400 if(!iface.instance_name.empty())
401 head += format(" %s", iface.instance_name);
404 if(iface.source==BUILTIN_SOURCE)
405 head += " (builtin)";
406 else if(iface.linked_block)
410 vector<Branch> branches;
411 if(iface.type_declaration)
412 append(format("Type: %%%d %s", get_label(*iface.type_declaration), iface.type_declaration->name));
414 branches.push_back(Branch("Layout", iface.layout.get()));
416 branches.push_back(Branch("Members", iface.members.get()));
417 append_subtree(branches);
420 void DumpTree::visit(FunctionDeclaration &func)
422 string text = format("%%%d %s %s%s", get_label(func), func.return_type, func.name, (func.signature.empty() ? "(?)" : func.signature));
423 if(func.source==BUILTIN_SOURCE)
424 text += " (builtin)";
425 else if(!func.definition)
426 text += " (undefined)";
430 if(func.return_type_declaration)
431 append(format("Return type: %%%d %s", get_label(*func.return_type_declaration), func.return_type_declaration->name));
432 for(NodeArray<VariableDeclaration>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
435 if(func.definition==&func)
436 func.body.visit(*this);
437 else if(func.definition)
438 append(format("Definition: %%%d", get_label(*func.definition)));
442 void DumpTree::visit(Conditional &cond)
444 append(cond, "if()");
446 vector<Branch> branches;
447 branches.push_back(cond.condition.get());
448 branches.push_back(&cond.body);
449 if(!cond.else_body.body.empty())
450 branches.push_back(&cond.else_body);
451 append_subtree(branches);
454 void DumpTree::visit(Iteration &iter)
456 append(iter, "for()");
459 if(iter.init_statement)
460 annotated_branch("Initialization", *iter.init_statement);
462 annotated_branch("Condition", *iter.condition);
463 if(iter.loop_expression)
464 annotated_branch("Loop", *iter.loop_expression);
466 annotated_branch("Body", iter.body);
470 void DumpTree::visit(Passthrough &pass)
472 append(pass, (pass.subscript ? "passthrough[]" : "passthrough"));
474 append_subtree(*pass.subscript);
477 void DumpTree::visit(Return &ret)
479 append(ret, (ret.expression ? "return" : "return;"));
481 append_subtree(*ret.expression);
484 void DumpTree::visit(Jump &jump)
486 append(jump, format("%s;", jump.keyword));