1 #include <msp/stringcodec/utf8.h>
2 #include <msp/strings/format.h>
11 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(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(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(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(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 (%d) -> %s", memacc.member, memacc.index, 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)
376 string head = format("%%%d %s %s", get_label(iface), iface.interface, iface.block_name);
377 if(!iface.instance_name.empty())
378 head += format(" %s", iface.instance_name);
381 if(iface.source==BUILTIN_SOURCE)
382 head += " (builtin)";
383 else if(iface.linked_block)
387 vector<Branch> branches;
388 if(iface.type_declaration)
389 branches.push_back(format("Type: %%%d %s", get_label(*iface.type_declaration), iface.type_declaration->name));
391 branches.push_back(iface.layout.get());
393 branches.push_back(iface.members.get());
394 append_subtree(branches);
397 void DumpTree::visit(FunctionDeclaration &func)
399 string text = format("%%%d %s %s%s", get_label(func), func.return_type, func.name, (func.signature.empty() ? "(?)" : func.signature));
400 if(func.source==BUILTIN_SOURCE)
401 text += " (builtin)";
402 else if(!func.definition)
403 text += " (undefined)";
407 if(func.return_type_declaration)
408 append(format("Return type: %%%d %s", get_label(*func.return_type_declaration), func.return_type_declaration->name));
409 for(NodeArray<VariableDeclaration>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
412 if(func.definition==&func)
413 func.body.visit(*this);
414 else if(func.definition)
415 append(format("Definition: %%%d", get_label(*func.definition)));
419 void DumpTree::visit(Conditional &cond)
421 append(cond, "if()");
423 vector<Branch> branches;
424 branches.push_back(cond.condition.get());
425 branches.push_back(Branch("then", &cond.body));
426 if(!cond.else_body.body.empty())
427 branches.push_back(Branch("else", &cond.else_body));
428 append_subtree(branches);
431 void DumpTree::visit(Iteration &iter)
433 append(iter, "for()");
435 vector<Branch> branches;
436 if(iter.init_statement)
437 branches.push_back(Branch("Initialization", iter.init_statement.get()));
439 branches.push_back(Branch("Condition", iter.condition.get()));
440 if(iter.loop_expression)
441 branches.push_back(Branch("Loop", iter.loop_expression.get()));
442 branches.push_back(&iter.body);
443 append_subtree(branches);
446 void DumpTree::visit(Passthrough &pass)
448 append(pass, (pass.subscript ? "passthrough[]" : "passthrough"));
450 append_subtree(*pass.subscript);
453 void DumpTree::visit(Return &ret)
455 append(ret, (ret.expression ? "return" : "return;"));
457 append_subtree(*ret.expression);
460 void DumpTree::visit(Jump &jump)
462 append(jump, format("%s;", jump.keyword));