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));
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 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()
81 tree.back() = (tree.back()==BRANCH_LAST ? EMPTY : STRAIGHT);
82 tree.push_back(BRANCH);
85 void DumpTree::last_branch()
87 tree.back() = BRANCH_LAST;
90 void DumpTree::end_sub()
93 if(tree.back()==STRAIGHT)
97 unsigned DumpTree::get_label(const Node &node)
99 unsigned &label = node_labels[&node];
101 label = node_labels.size();
105 string DumpTree::format_type(TypeDeclaration *type)
107 return (type ? type->name : "?");
111 typename T::const_iterator DumpTree::increment(typename T::const_iterator &iter, const T &container)
113 typename T::const_iterator ret = iter++;
114 if(iter==container.end())
119 void DumpTree::visit(Block &block)
121 append(block, format("Block %s", (block.use_braces ? "{}" : "(inline)")));
124 for(std::map<string, VariableDeclaration *>::const_iterator i=block.variables.begin(); i!=block.variables.end(); ++i)
125 append(format("Variable: %%%d %s %s", get_label(*i->second), i->second->type, i->first));
127 for(NodeList<Statement>::const_iterator i=block.body.begin(); i!=block.body.end(); )
129 NodeList<Statement>::const_iterator j = increment(i, block.body);
136 void DumpTree::visit(Literal &literal)
138 append(literal, format("Literal: %s -> %s", literal.token, format_type(literal.type)));
141 void DumpTree::visit(VariableReference &var)
145 text += format("%%%d ", get_label(*var.declaration));
146 text += format("%s (var) -> %s", var.name, format_type(var.type));
150 void DumpTree::visit(InterfaceBlockReference &iface)
153 if(iface.declaration)
154 text += format("%%%d ", get_label(*iface.declaration));
155 text += format("%s (iface) -> %s", iface.name, format_type(iface.type));
159 void DumpTree::visit(MemberAccess &memacc)
161 string text = "Member access:";
162 if(memacc.declaration)
163 text += format(" %%%d", get_label(*memacc.declaration));
164 text += format(" .%s -> %s", memacc.member, format_type(memacc.type));
165 append(memacc, text);
166 append_subtree(*memacc.left);
169 void DumpTree::visit(Swizzle &swizzle)
171 static const char components[4] = { 'x', 'y', 'z', 'w' };
172 string text = "Swizzle: .";
173 for(unsigned i=0; i<swizzle.count; ++i)
174 text += components[swizzle.components[i]];
175 text += format(" -> %s", format_type(swizzle.type));
176 append(swizzle, text);
177 append_subtree(*swizzle.left);
180 void DumpTree::visit(UnaryExpression &unary)
182 string text = format("Unary: %s, %sfix -> %s", unary.oper->token, (unary.oper->type==Operator::PREFIX ? "pre" : "post"), format_type(unary.type));
184 append_subtree(*unary.expression);
187 void DumpTree::visit(BinaryExpression &binary)
189 append(binary, format("Binary: %s%s -> %s", binary.oper->token, binary.oper->token2, format_type(binary.type)));
191 binary.left->visit(*this);
193 binary.right->visit(*this);
197 void DumpTree::visit(Assignment &assign)
199 append(assign, format("Assignment: %s%s -> %s", assign.oper->token, (assign.self_referencing ? " (self-referencing)" : ""), format_type(assign.type)));
201 if(assign.target.declaration)
203 string text = format("Target: %%%d", get_label(*assign.target.declaration));
205 static const char swizzle[4] = { 'x', 'y', 'z', 'w' };
206 for(unsigned i=0; i<assign.target.chain_len; ++i)
208 unsigned component = assign.target.chain[i];
209 switch(static_cast<Assignment::Target::ChainType>(component&0xC0))
211 case Assignment::Target::MEMBER:
212 text += format(" .%d", component&0x3F);
214 case Assignment::Target::SWIZZLE:
216 for(unsigned j=0; j<4; ++j)
220 case Assignment::Target::ARRAY:
221 text += format(" [%d]", component&0x3F);
227 assign.left->visit(*this);
229 assign.right->visit(*this);
233 void DumpTree::visit(TernaryExpression &ternary)
235 append(ternary, format("Ternary: %s%s -> %s", ternary.oper->token, ternary.oper->token2, format_type(ternary.type)));
237 ternary.condition->visit(*this);
238 ternary.true_expr->visit(*this);
240 ternary.false_expr->visit(*this);
244 void DumpTree::visit(FunctionCall &call)
246 string head = "Function call: ";
248 head += format("%%%d ", get_label(*call.declaration));
251 head += " (constructor)";
252 head += format(" -> %s", format_type(call.type));
256 for(NodeArray<Expression>::const_iterator i=call.arguments.begin(); i!=call.arguments.end(); )
258 NodeArray<Expression>::const_iterator j = increment(i, call.arguments);
264 void DumpTree::visit(ExpressionStatement &expr)
266 append(expr, "expr;");
267 append_subtree(*expr.expression);
270 void DumpTree::visit(Import &import)
272 append(import, format("import %s", import.module));
275 void DumpTree::visit(Precision &prec)
277 append(prec, format("precision %s %s", prec.precision, prec.type));
280 void DumpTree::visit(Layout &layout)
282 append(layout, "Layout");
284 for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); )
286 vector<Layout::Qualifier>::const_iterator j = increment(i, layout.qualifiers);
287 string qualifier = j->name;
289 qualifier += format("=%d", j->value);
295 void DumpTree::visit(InterfaceLayout &layout)
297 append(layout, format("Layout: %s", layout.interface));
298 append_subtree(layout.layout);
301 void DumpTree::visit(BasicTypeDeclaration &type)
303 append(type, format("%%%d typedef %s", get_label(type), type.name));
305 vector<Branch> branches;
307 branches.push_back(format("%s: %%%d %s", (type.kind==BasicTypeDeclaration::ALIAS ? "Alias of" : "Base"), get_label(*type.base_type), type.base_type->name));
308 if(type.kind==BasicTypeDeclaration::VECTOR)
309 branches.push_back(format("Vector: %d", type.size));
310 else if(type.kind==BasicTypeDeclaration::MATRIX)
311 branches.push_back(format("Matrix: %dx%d", type.size&0xFFFF, type.size>>16));
312 append_subtree(branches);
315 void DumpTree::visit(ImageTypeDeclaration &type)
317 static const char *dims[] = { "1D", "2D", "3D", "Cube" };
319 append(type, format("%%%d typedef %s", get_label(type), type.name));
321 vector<Branch> branches;
322 branches.push_back(format("Dimensions: %s%s", dims[type.dimensions-1], (type.array ? " array" : "")));
324 branches.push_back(format("Element type: %%%d %s", get_label(*type.base_type), type.base_type->name));
326 branches.push_back("Shadow");
327 append_subtree(branches);
330 void DumpTree::visit(StructDeclaration &strct)
332 append(strct, format("%%%d struct %s", get_label(strct), strct.name));
333 append_subtree(strct.members);
336 void DumpTree::visit(VariableDeclaration &var)
338 string decl = format("%%%d ", get_label(var));
341 if(!var.interpolation.empty())
342 decl += format("%s ", var.interpolation);
343 if(!var.sampling.empty())
344 decl += format("%s ", var.sampling);
345 if(!var.interface.empty())
346 decl += format("%s ", var.interface);
347 if(!var.precision.empty())
348 decl += format("%s ", var.precision);
349 decl += format("%s %s", var.type, var.name);
350 if(var.source==BUILTIN_SOURCE)
351 decl += " (builtin)";
352 else if(var.linked_declaration)
356 vector<Branch> branches;
357 if(var.type_declaration)
358 branches.push_back(format("Type: %%%d %s", get_label(*var.type_declaration), var.type_declaration->name));
360 branches.push_back(var.layout.get());
364 branches.push_back(Branch("Array []", var.array_size.get()));
366 branches.push_back("Array []");
368 if(var.init_expression)
369 branches.push_back(var.init_expression.get());
370 append_subtree(branches);
373 void DumpTree::visit(InterfaceBlock &iface)
376 if(!iface.instance_name.empty())
377 head += format("%%%d ", get_label(iface));
378 head += format("%s %s", iface.interface, iface.block_name);
379 if(!iface.instance_name.empty())
380 head += format(" %s", iface.instance_name);
383 if(iface.source==BUILTIN_SOURCE)
384 head += " (builtin)";
385 else if(iface.linked_block)
389 vector<Branch> branches;
390 if(iface.type_declaration)
391 branches.push_back(format("Type: %%%d %s", get_label(*iface.type_declaration), iface.type_declaration->name));
393 branches.push_back(iface.layout.get());
395 branches.push_back(iface.members.get());
396 append_subtree(branches);
399 void DumpTree::visit(FunctionDeclaration &func)
401 string text = format("%%%d %s %s%s", get_label(func), func.return_type, func.name, (func.signature.empty() ? "(?)" : func.signature));
402 if(func.source==BUILTIN_SOURCE)
403 text += " (builtin)";
404 else if(!func.definition)
405 text += " (undefined)";
409 if(func.return_type_declaration)
410 append(format("Return type: %%%d %s", get_label(*func.return_type_declaration), func.return_type_declaration->name));
411 for(NodeArray<VariableDeclaration>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
414 if(func.definition==&func)
415 func.body.visit(*this);
416 else if(func.definition)
417 append(format("Definition: %%%d", get_label(*func.definition)));
421 void DumpTree::visit(Conditional &cond)
423 append(cond, "if()");
425 vector<Branch> branches;
426 branches.push_back(cond.condition.get());
427 branches.push_back(Branch("then", &cond.body));
428 if(!cond.else_body.body.empty())
429 branches.push_back(Branch("else", &cond.else_body));
430 append_subtree(branches);
433 void DumpTree::visit(Iteration &iter)
435 append(iter, "for()");
437 vector<Branch> branches;
438 if(iter.init_statement)
439 branches.push_back(Branch("Initialization", iter.init_statement.get()));
441 branches.push_back(Branch("Condition", iter.condition.get()));
442 if(iter.loop_expression)
443 branches.push_back(Branch("Loop", iter.loop_expression.get()));
444 branches.push_back(&iter.body);
445 append_subtree(branches);
448 void DumpTree::visit(Passthrough &pass)
450 append(pass, (pass.subscript ? "passthrough[]" : "passthrough"));
452 append_subtree(*pass.subscript);
455 void DumpTree::visit(Return &ret)
457 append(ret, (ret.expression ? "return" : "return;"));
459 append_subtree(*ret.expression);
462 void DumpTree::visit(Jump &jump)
464 append(jump, format("%s;", jump.keyword));