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);
68 j->node->visit(*this);
73 j->node->visit(*this);
78 void DumpTree::append_subtree(Node &node)
86 void DumpTree::begin_sub()
88 tree.back() = (tree.back()==BRANCH_LAST ? EMPTY : STRAIGHT);
89 tree.push_back(BRANCH);
92 void DumpTree::last_branch()
94 tree.back() = BRANCH_LAST;
97 void DumpTree::end_sub()
100 if(tree.back()==STRAIGHT)
101 tree.back() = BRANCH;
104 unsigned DumpTree::get_label(const Node &node)
106 unsigned &label = node_labels[&node];
108 label = node_labels.size();
112 string DumpTree::format_type(TypeDeclaration *type)
114 return (type ? type->name : "?");
118 typename T::const_iterator DumpTree::increment(typename T::const_iterator &iter, const T &container)
120 typename T::const_iterator ret = iter++;
121 if(iter==container.end())
126 void DumpTree::visit(Block &block)
128 append(block, format("Block %s", (block.use_braces ? "{}" : "(inline)")));
131 for(std::map<string, VariableDeclaration *>::const_iterator i=block.variables.begin(); i!=block.variables.end(); ++i)
132 append(format("Variable: %%%d %s %s", get_label(*i->second), i->second->type, i->first));
134 for(NodeList<Statement>::const_iterator i=block.body.begin(); i!=block.body.end(); )
136 NodeList<Statement>::const_iterator j = increment(i, block.body);
143 void DumpTree::visit(Literal &literal)
145 append(literal, format("Literal: %s -> %s", literal.token, format_type(literal.type)));
148 void DumpTree::visit(VariableReference &var)
152 text += format("%%%d ", get_label(*var.declaration));
153 text += format("%s (var) -> %s", var.name, format_type(var.type));
157 void DumpTree::visit(InterfaceBlockReference &iface)
160 if(iface.declaration)
161 text += format("%%%d ", get_label(*iface.declaration));
162 text += format("%s (iface) -> %s", iface.name, format_type(iface.type));
166 void DumpTree::visit(MemberAccess &memacc)
168 string text = "Member access:";
169 if(memacc.declaration)
170 text += format(" %%%d", get_label(*memacc.declaration));
171 text += format(" .%s -> %s", memacc.member, format_type(memacc.type));
172 append(memacc, text);
173 append_subtree(*memacc.left);
176 void DumpTree::visit(Swizzle &swizzle)
178 static const char components[4] = { 'x', 'y', 'z', 'w' };
179 string text = "Swizzle: .";
180 for(unsigned i=0; i<swizzle.count; ++i)
181 text += components[swizzle.components[i]];
182 text += format(" -> %s", format_type(swizzle.type));
183 append(swizzle, text);
184 append_subtree(*swizzle.left);
187 void DumpTree::visit(UnaryExpression &unary)
189 string text = format("Unary: %s, %sfix -> %s", unary.oper->token, (unary.oper->type==Operator::PREFIX ? "pre" : "post"), format_type(unary.type));
191 append_subtree(*unary.expression);
194 void DumpTree::visit(BinaryExpression &binary)
196 append(binary, format("Binary: %s%s -> %s", binary.oper->token, binary.oper->token2, format_type(binary.type)));
198 binary.left->visit(*this);
200 binary.right->visit(*this);
204 void DumpTree::visit(Assignment &assign)
206 append(assign, format("Assignment: %s%s -> %s", assign.oper->token, (assign.self_referencing ? " (self-referencing)" : ""), format_type(assign.type)));
208 if(assign.target.declaration)
210 string text = format("Target: %%%d", get_label(*assign.target.declaration));
212 static const char swizzle[4] = { 'x', 'y', 'z', 'w' };
213 for(unsigned i=0; i<assign.target.chain_len; ++i)
215 unsigned component = assign.target.chain[i];
216 switch(static_cast<Assignment::Target::ChainType>(component&0xC0))
218 case Assignment::Target::MEMBER:
219 text += format(" .%d", component&0x3F);
221 case Assignment::Target::SWIZZLE:
223 for(unsigned j=0; j<4; ++j)
227 case Assignment::Target::ARRAY:
228 text += format(" [%d]", component&0x3F);
234 assign.left->visit(*this);
236 assign.right->visit(*this);
240 void DumpTree::visit(TernaryExpression &ternary)
242 append(ternary, format("Ternary: %s%s -> %s", ternary.oper->token, ternary.oper->token2, format_type(ternary.type)));
244 ternary.condition->visit(*this);
245 ternary.true_expr->visit(*this);
247 ternary.false_expr->visit(*this);
251 void DumpTree::visit(FunctionCall &call)
253 string head = "Function call: ";
255 head += format("%%%d ", get_label(*call.declaration));
258 head += " (constructor)";
259 head += format(" -> %s", format_type(call.type));
263 for(NodeArray<Expression>::const_iterator i=call.arguments.begin(); i!=call.arguments.end(); )
265 NodeArray<Expression>::const_iterator j = increment(i, call.arguments);
271 void DumpTree::visit(ExpressionStatement &expr)
273 append(expr, "expr;");
274 append_subtree(*expr.expression);
277 void DumpTree::visit(Import &import)
279 append(import, format("import %s", import.module));
282 void DumpTree::visit(Precision &prec)
284 append(prec, format("precision %s %s", prec.precision, prec.type));
287 void DumpTree::visit(Layout &layout)
289 append(layout, "Layout");
291 for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); )
293 vector<Layout::Qualifier>::const_iterator j = increment(i, layout.qualifiers);
294 string qualifier = j->name;
296 qualifier += format("=%d", j->value);
302 void DumpTree::visit(InterfaceLayout &layout)
304 append(layout, format("Layout: %s", layout.interface));
305 append_subtree(layout.layout);
308 void DumpTree::visit(BasicTypeDeclaration &type)
310 append(type, format("%%%d typedef %s", get_label(type), type.name));
312 vector<Branch> branches;
314 branches.push_back(format("%s: %%%d %s", (type.kind==BasicTypeDeclaration::ALIAS ? "Alias of" : "Base"), get_label(*type.base_type), type.base_type->name));
315 if(type.kind==BasicTypeDeclaration::VECTOR)
316 branches.push_back(format("Vector: %d", type.size));
317 else if(type.kind==BasicTypeDeclaration::MATRIX)
318 branches.push_back(format("Matrix: %dx%d", type.size&0xFFFF, type.size>>16));
319 append_subtree(branches);
322 void DumpTree::visit(ImageTypeDeclaration &type)
324 static const char *dims[] = { "1D", "2D", "3D", "Cube" };
326 append(type, format("%%%d typedef %s", get_label(type), type.name));
328 vector<Branch> branches;
329 branches.push_back(format("Dimensions: %s%s", dims[type.dimensions-1], (type.array ? " array" : "")));
331 branches.push_back(format("Element type: %%%d %s", get_label(*type.base_type), type.base_type->name));
333 branches.push_back("Shadow");
334 append_subtree(branches);
337 void DumpTree::visit(StructDeclaration &strct)
339 append(strct, format("%%%d struct %s", get_label(strct), strct.name));
340 append_subtree(strct.members);
343 void DumpTree::visit(VariableDeclaration &var)
345 string decl = format("%%%d ", get_label(var));
348 if(!var.interpolation.empty())
349 decl += format("%s ", var.interpolation);
350 if(!var.sampling.empty())
351 decl += format("%s ", var.sampling);
352 if(!var.interface.empty())
353 decl += format("%s ", var.interface);
354 if(!var.precision.empty())
355 decl += format("%s ", var.precision);
356 decl += format("%s %s", var.type, var.name);
357 if(var.source==BUILTIN_SOURCE)
358 decl += " (builtin)";
359 else if(var.linked_declaration)
363 vector<Branch> branches;
364 if(var.type_declaration)
365 branches.push_back(format("Type: %%%d %s", get_label(*var.type_declaration), var.type_declaration->name));
367 branches.push_back(var.layout.get());
371 branches.push_back(Branch("Array []", var.array_size.get()));
373 branches.push_back("Array []");
375 if(var.init_expression)
376 branches.push_back(var.init_expression.get());
377 append_subtree(branches);
380 void DumpTree::visit(InterfaceBlock &iface)
383 if(!iface.instance_name.empty())
384 head += format("%%%d ", get_label(iface));
385 head += format("%s %s", iface.interface, iface.block_name);
386 if(!iface.instance_name.empty())
387 head += format(" %s", iface.instance_name);
390 if(iface.source==BUILTIN_SOURCE)
391 head += " (builtin)";
392 else if(iface.linked_block)
396 vector<Branch> branches;
397 if(iface.type_declaration)
398 append(format("Type: %%%d %s", get_label(*iface.type_declaration), iface.type_declaration->name));
400 branches.push_back(iface.layout.get());
402 branches.push_back(iface.members.get());
403 append_subtree(branches);
406 void DumpTree::visit(FunctionDeclaration &func)
408 string text = format("%%%d %s %s%s", get_label(func), func.return_type, func.name, (func.signature.empty() ? "(?)" : func.signature));
409 if(func.source==BUILTIN_SOURCE)
410 text += " (builtin)";
411 else if(!func.definition)
412 text += " (undefined)";
416 if(func.return_type_declaration)
417 append(format("Return type: %%%d %s", get_label(*func.return_type_declaration), func.return_type_declaration->name));
418 for(NodeArray<VariableDeclaration>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
421 if(func.definition==&func)
422 func.body.visit(*this);
423 else if(func.definition)
424 append(format("Definition: %%%d", get_label(*func.definition)));
428 void DumpTree::visit(Conditional &cond)
430 append(cond, "if()");
432 vector<Branch> branches;
433 branches.push_back(cond.condition.get());
434 branches.push_back(Branch("then", &cond.body));
435 if(!cond.else_body.body.empty())
436 branches.push_back(Branch("else", &cond.else_body));
437 append_subtree(branches);
440 void DumpTree::visit(Iteration &iter)
442 append(iter, "for()");
444 vector<Branch> branches;
445 if(iter.init_statement)
446 branches.push_back(Branch("Initialization", iter.init_statement.get()));
448 branches.push_back(Branch("Condition", iter.condition.get()));
449 if(iter.loop_expression)
450 branches.push_back(Branch("Loop", iter.loop_expression.get()));
451 branches.push_back(&iter.body);
452 append_subtree(branches);
455 void DumpTree::visit(Passthrough &pass)
457 append(pass, (pass.subscript ? "passthrough[]" : "passthrough"));
459 append_subtree(*pass.subscript);
462 void DumpTree::visit(Return &ret)
464 append(ret, (ret.expression ? "return" : "return;"));
466 append_subtree(*ret.expression);
469 void DumpTree::visit(Jump &jump)
471 append(jump, format("%s;", jump.keyword));