1 #include <msp/stringcodec/utf8.h>
2 #include <msp/strings/format.h>
11 std::string DumpTree::apply(Stage &stage)
13 formatted = format("Stage: %s\n", Stage::get_stage_name(stage.type));
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 for(std::map<string, InterfaceBlock *>::const_iterator i=stage.interface_blocks.begin(); i!=stage.interface_blocks.end(); ++i)
21 append(format("Interface block: %%%d %s", get_label(*i->second), i->first));
23 for(std::map<string, FunctionDeclaration *>::const_iterator i=stage.functions.begin(); i!=stage.functions.end(); ++i)
24 append(format("Function: %%%d %s", get_label(*i->second), i->first));
27 stage.content.visit(*this);
31 void DumpTree::append(const string &line)
33 StringCodec::Utf8::Encoder enc;
34 for(vector<TreeChars>::const_iterator i=tree.begin(); i!=tree.end(); )
36 enc.encode_char(*i++, formatted);
37 enc.encode_char((i==tree.end() ? REACH : EMPTY), formatted);
43 void DumpTree::append(const Node &node, const string &line)
45 append(format("<%d:%d> %s", node.source, node.line, line));
48 void DumpTree::append_subtree(const vector<Branch> &branches)
51 for(vector<Branch>::const_iterator i=branches.begin(); i!=branches.end(); )
53 vector<Branch>::const_iterator j = increment(i, branches);
61 j->node->visit(*this);
66 j->node->visit(*this);
71 void DumpTree::append_subtree(Node &node)
79 void DumpTree::begin_sub()
82 tree.back() = (tree.back()==BRANCH_LAST ? EMPTY : STRAIGHT);
83 tree.push_back(BRANCH);
86 void DumpTree::last_branch()
88 tree.back() = BRANCH_LAST;
91 void DumpTree::end_sub()
94 if(!tree.empty() && tree.back()==STRAIGHT)
98 unsigned DumpTree::get_label(const Node &node)
100 unsigned &label = node_labels[&node];
102 label = node_labels.size();
106 string DumpTree::format_type(TypeDeclaration *type)
108 return (type ? type->name : "?");
112 typename T::const_iterator DumpTree::increment(typename T::const_iterator &iter, const T &container)
114 typename T::const_iterator ret = iter++;
115 if(iter==container.end())
120 void DumpTree::visit(Block &block)
122 append(block, format("Block %s", (block.use_braces ? "{}" : "(inline)")));
125 for(std::map<string, VariableDeclaration *>::const_iterator i=block.variables.begin(); i!=block.variables.end(); ++i)
126 append(format("Variable: %%%d %s %s", get_label(*i->second), i->second->type, i->first));
128 for(NodeList<Statement>::const_iterator i=block.body.begin(); i!=block.body.end(); )
130 NodeList<Statement>::const_iterator j = increment(i, block.body);
137 void DumpTree::visit(Literal &literal)
139 append(literal, format("Literal: %s -> %s", literal.token, format_type(literal.type)));
142 void DumpTree::visit(VariableReference &var)
146 text += format("%%%d ", get_label(*var.declaration));
147 text += format("%s (var) -> %s", var.name, format_type(var.type));
151 void DumpTree::visit(InterfaceBlockReference &iface)
154 if(iface.declaration)
155 text += format("%%%d ", get_label(*iface.declaration));
156 text += format("%s (iface) -> %s", iface.name, format_type(iface.type));
160 void DumpTree::visit(MemberAccess &memacc)
162 string text = "Member access:";
163 if(memacc.declaration)
164 text += format(" %%%d", get_label(*memacc.declaration));
165 text += format(" .%s -> %s", memacc.member, format_type(memacc.type));
166 append(memacc, text);
167 append_subtree(*memacc.left);
170 void DumpTree::visit(Swizzle &swizzle)
172 static const char components[4] = { 'x', 'y', 'z', 'w' };
173 string text = "Swizzle: .";
174 for(unsigned i=0; i<swizzle.count; ++i)
175 text += components[swizzle.components[i]];
176 text += format(" -> %s", format_type(swizzle.type));
177 append(swizzle, text);
178 append_subtree(*swizzle.left);
181 void DumpTree::visit(UnaryExpression &unary)
183 string text = format("Unary: %s, %sfix -> %s", unary.oper->token, (unary.oper->type==Operator::PREFIX ? "pre" : "post"), format_type(unary.type));
185 append_subtree(*unary.expression);
188 void DumpTree::visit(BinaryExpression &binary)
190 append(binary, format("Binary: %s%s -> %s", binary.oper->token, binary.oper->token2, format_type(binary.type)));
192 binary.left->visit(*this);
194 binary.right->visit(*this);
198 void DumpTree::visit(Assignment &assign)
200 append(assign, format("Assignment: %s%s -> %s", assign.oper->token, (assign.self_referencing ? " (self-referencing)" : ""), format_type(assign.type)));
202 if(assign.target.declaration)
204 string text = format("Target: %%%d", get_label(*assign.target.declaration));
206 static const char swizzle[4] = { 'x', 'y', 'z', 'w' };
207 for(unsigned i=0; i<assign.target.chain_len; ++i)
209 unsigned component = assign.target.chain[i];
210 switch(static_cast<Assignment::Target::ChainType>(component&0xC0))
212 case Assignment::Target::MEMBER:
213 text += format(" .%d", component&0x3F);
215 case Assignment::Target::SWIZZLE:
217 for(unsigned j=0; j<4; ++j)
221 case Assignment::Target::ARRAY:
222 text += format(" [%d]", component&0x3F);
228 assign.left->visit(*this);
230 assign.right->visit(*this);
234 void DumpTree::visit(TernaryExpression &ternary)
236 append(ternary, format("Ternary: %s%s -> %s", ternary.oper->token, ternary.oper->token2, format_type(ternary.type)));
238 ternary.condition->visit(*this);
239 ternary.true_expr->visit(*this);
241 ternary.false_expr->visit(*this);
245 void DumpTree::visit(FunctionCall &call)
247 string head = "Function call: ";
249 head += format("%%%d ", get_label(*call.declaration));
252 head += " (constructor)";
253 head += format(" -> %s", format_type(call.type));
257 for(NodeArray<Expression>::const_iterator i=call.arguments.begin(); i!=call.arguments.end(); )
259 NodeArray<Expression>::const_iterator j = increment(i, call.arguments);
265 void DumpTree::visit(ExpressionStatement &expr)
267 append(expr, "expr;");
268 append_subtree(*expr.expression);
271 void DumpTree::visit(Import &import)
273 append(import, format("import %s", import.module));
276 void DumpTree::visit(Precision &prec)
278 append(prec, format("precision %s %s", prec.precision, prec.type));
281 void DumpTree::visit(Layout &layout)
283 append(layout, "Layout");
285 for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); )
287 vector<Layout::Qualifier>::const_iterator j = increment(i, layout.qualifiers);
288 string qualifier = j->name;
290 qualifier += format("=%d", j->value);
296 void DumpTree::visit(InterfaceLayout &layout)
298 append(layout, format("Layout: %s", layout.interface));
299 append_subtree(layout.layout);
302 void DumpTree::visit(BasicTypeDeclaration &type)
304 append(type, format("%%%d typedef %s", get_label(type), type.name));
306 vector<Branch> branches;
308 branches.push_back(format("%s: %%%d %s", (type.kind==BasicTypeDeclaration::ALIAS ? "Alias of" : "Base"), get_label(*type.base_type), type.base_type->name));
309 if(type.kind==BasicTypeDeclaration::VECTOR)
310 branches.push_back(format("Vector: %d", type.size));
311 else if(type.kind==BasicTypeDeclaration::MATRIX)
312 branches.push_back(format("Matrix: %dx%d", type.size&0xFFFF, type.size>>16));
313 append_subtree(branches);
316 void DumpTree::visit(ImageTypeDeclaration &type)
318 static const char *dims[] = { "1D", "2D", "3D", "Cube" };
320 append(type, format("%%%d typedef %s", get_label(type), type.name));
322 vector<Branch> branches;
323 branches.push_back(format("Dimensions: %s%s", dims[type.dimensions-1], (type.array ? " array" : "")));
325 branches.push_back(format("Element type: %%%d %s", get_label(*type.base_type), type.base_type->name));
327 branches.push_back("Shadow");
328 append_subtree(branches);
331 void DumpTree::visit(StructDeclaration &strct)
333 append(strct, format("%%%d struct %s", get_label(strct), strct.name));
334 append_subtree(strct.members);
337 void DumpTree::visit(VariableDeclaration &var)
339 string decl = format("%%%d ", get_label(var));
342 if(!var.interpolation.empty())
343 decl += format("%s ", var.interpolation);
344 if(!var.sampling.empty())
345 decl += format("%s ", var.sampling);
346 if(!var.interface.empty())
347 decl += format("%s ", var.interface);
348 if(!var.precision.empty())
349 decl += format("%s ", var.precision);
350 decl += format("%s %s", var.type, var.name);
351 if(var.source==BUILTIN_SOURCE)
352 decl += " (builtin)";
353 else if(var.linked_declaration)
357 vector<Branch> branches;
358 if(var.type_declaration)
359 branches.push_back(format("Type: %%%d %s", get_label(*var.type_declaration), var.type_declaration->name));
361 branches.push_back(var.layout.get());
365 branches.push_back(Branch("Array []", var.array_size.get()));
367 branches.push_back("Array []");
369 if(var.init_expression)
370 branches.push_back(var.init_expression.get());
371 append_subtree(branches);
374 void DumpTree::visit(InterfaceBlock &iface)
377 if(!iface.instance_name.empty())
378 head += format("%%%d ", get_label(iface));
379 head += format("%s %s", iface.interface, iface.block_name);
380 if(!iface.instance_name.empty())
381 head += format(" %s", iface.instance_name);
384 if(iface.source==BUILTIN_SOURCE)
385 head += " (builtin)";
386 else if(iface.linked_block)
390 vector<Branch> branches;
391 if(iface.type_declaration)
392 branches.push_back(format("Type: %%%d %s", get_label(*iface.type_declaration), iface.type_declaration->name));
394 branches.push_back(iface.layout.get());
396 branches.push_back(iface.members.get());
397 append_subtree(branches);
400 void DumpTree::visit(FunctionDeclaration &func)
402 string text = format("%%%d %s %s%s", get_label(func), func.return_type, func.name, (func.signature.empty() ? "(?)" : func.signature));
403 if(func.source==BUILTIN_SOURCE)
404 text += " (builtin)";
405 else if(!func.definition)
406 text += " (undefined)";
410 if(func.return_type_declaration)
411 append(format("Return type: %%%d %s", get_label(*func.return_type_declaration), func.return_type_declaration->name));
412 for(NodeArray<VariableDeclaration>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
415 if(func.definition==&func)
416 func.body.visit(*this);
417 else if(func.definition)
418 append(format("Definition: %%%d", get_label(*func.definition)));
422 void DumpTree::visit(Conditional &cond)
424 append(cond, "if()");
426 vector<Branch> branches;
427 branches.push_back(cond.condition.get());
428 branches.push_back(Branch("then", &cond.body));
429 if(!cond.else_body.body.empty())
430 branches.push_back(Branch("else", &cond.else_body));
431 append_subtree(branches);
434 void DumpTree::visit(Iteration &iter)
436 append(iter, "for()");
438 vector<Branch> branches;
439 if(iter.init_statement)
440 branches.push_back(Branch("Initialization", iter.init_statement.get()));
442 branches.push_back(Branch("Condition", iter.condition.get()));
443 if(iter.loop_expression)
444 branches.push_back(Branch("Loop", iter.loop_expression.get()));
445 branches.push_back(&iter.body);
446 append_subtree(branches);
449 void DumpTree::visit(Passthrough &pass)
451 append(pass, (pass.subscript ? "passthrough[]" : "passthrough"));
453 append_subtree(*pass.subscript);
456 void DumpTree::visit(Return &ret)
458 append(ret, (ret.expression ? "return" : "return;"));
460 append_subtree(*ret.expression);
463 void DumpTree::visit(Jump &jump)
465 append(jump, format("%s;", jump.keyword));