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->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_subtree(const vector<Branch> &branches)
53 for(vector<Branch>::const_iterator i=branches.begin(); i!=branches.end(); )
55 vector<Branch>::const_iterator j = increment(i, branches);
59 annotated_branch(j->text, *j->node);
64 j->node->visit(*this);
69 void DumpTree::begin_sub()
71 tree.back() = (tree.back()==BRANCH_LAST ? EMPTY : STRAIGHT);
72 tree.push_back(BRANCH);
75 void DumpTree::last_branch()
77 tree.back() = BRANCH_LAST;
80 void DumpTree::end_sub()
83 if(tree.back()==STRAIGHT)
87 void DumpTree::annotated_branch(const string &annotation, Node &node)
96 unsigned DumpTree::get_label(const Node &node)
98 unsigned &label = node_labels[&node];
100 label = node_labels.size();
104 string DumpTree::format_type(TypeDeclaration *type)
106 return (type ? type->name : "?");
110 typename T::const_iterator DumpTree::increment(typename T::const_iterator &iter, const T &container)
112 typename T::const_iterator ret = iter++;
113 if(iter==container.end())
118 void DumpTree::visit(Block &block)
120 append(format("Block %s", (block.use_braces ? "{}" : "(inline)")));
123 for(std::map<string, VariableDeclaration *>::const_iterator i=block.variables.begin(); i!=block.variables.end(); ++i)
124 append(format("Variable: %%%d %s %s", get_label(*i->second), i->second->type, i->first));
126 bool labeled_body = !block.variables.empty();
133 for(NodeList<Statement>::const_iterator i=block.body.begin(); i!=block.body.end(); )
135 NodeList<Statement>::const_iterator j = increment(i, block.body);
144 void DumpTree::visit(Literal &literal)
146 append(format("Literal: %s -> %s", literal.token, format_type(literal.type)));
149 void DumpTree::visit(ParenthesizedExpression &parexpr)
151 annotated_branch(format("(expr) -> %s", format_type(parexpr.type)), *parexpr.expression);
154 void DumpTree::visit(VariableReference &var)
158 text += format("%%%d ", get_label(*var.declaration));
159 text += format("%s (var) -> %s", var.name, format_type(var.type));
163 void DumpTree::visit(InterfaceBlockReference &iface)
166 if(iface.declaration)
167 text += format("%%%d ", get_label(*iface.declaration));
168 text += format("%s (iface) -> %s", iface.name, format_type(iface.type));
172 void DumpTree::visit(MemberAccess &memacc)
174 string text = "Member access:";
175 if(memacc.declaration)
176 text += format(" %%%d", get_label(*memacc.declaration));
177 text += format(" .%s -> %s", memacc.member, format_type(memacc.type));
178 annotated_branch(text, *memacc.left);
181 void DumpTree::visit(Swizzle &swizzle)
183 static const char components[4] = { 'x', 'y', 'z', 'w' };
184 string text = "Swizzle: .";
185 for(unsigned i=0; i<swizzle.count; ++i)
186 text += components[swizzle.components[i]];
187 text += format(" -> %s", format_type(swizzle.type));
188 annotated_branch(text, *swizzle.left);
191 void DumpTree::visit(UnaryExpression &unary)
193 string text = format("Unary: %s, %sfix -> %s", unary.oper->token, (unary.oper->type==Operator::PREFIX ? "pre" : "post"), format_type(unary.type));
194 annotated_branch(text, *unary.expression);
197 void DumpTree::visit(BinaryExpression &binary)
199 append(format("Binary: %s -> %s", (binary.oper->token[0]=='[' ? "[]" : binary.oper->token), format_type(binary.type)));
201 binary.left->visit(*this);
203 binary.right->visit(*this);
207 void DumpTree::visit(Assignment &assign)
209 append(format("Assignment: %s%s -> %s", assign.oper->token, (assign.self_referencing ? " (self-referencing)" : ""), format_type(assign.type)));
211 if(assign.target.declaration)
213 string text = format("Target: %%%d", get_label(*assign.target.declaration));
215 static const char swizzle[4] = { 'x', 'y', 'z', 'w' };
216 for(unsigned i=0; i<assign.target.chain_len; ++i)
218 unsigned component = assign.target.chain[i];
219 switch(static_cast<Assignment::Target::ChainType>(component&0xC0))
221 case Assignment::Target::MEMBER:
222 text += format(" .%d", component&0x3F);
224 case Assignment::Target::SWIZZLE:
226 for(unsigned j=0; j<4; ++j)
230 case Assignment::Target::ARRAY:
231 text += format(" [%d]", component&0x3F);
237 assign.left->visit(*this);
239 assign.right->visit(*this);
243 void DumpTree::visit(FunctionCall &call)
245 string head = "Function call: ";
247 head += format("%%%d ", get_label(*call.declaration));
250 head += " (constructor)";
251 head += format(" -> %s", format_type(call.type));
255 for(NodeArray<Expression>::const_iterator i=call.arguments.begin(); i!=call.arguments.end(); )
257 NodeArray<Expression>::const_iterator j = increment(i, call.arguments);
263 void DumpTree::visit(ExpressionStatement &expr)
265 annotated_branch("expr;", *expr.expression);
268 void DumpTree::visit(Import &import)
270 append(format("import %s", import.module));
273 void DumpTree::visit(Precision &prec)
275 append(format("precision %s %s", prec.precision, prec.type));
278 void DumpTree::visit(Layout &layout)
282 for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); )
284 vector<Layout::Qualifier>::const_iterator j = increment(i, layout.qualifiers);
285 string qualifier = j->name;
287 qualifier += format("=%d", j->value);
293 void DumpTree::visit(InterfaceLayout &layout)
295 annotated_branch(format("Layout: %s", layout.interface), layout.layout);
298 void DumpTree::visit(BasicTypeDeclaration &type)
300 append(format("%%%d typedef %s", get_label(type), type.name));
302 vector<Branch> branches;
304 branches.push_back(format("%s: %%%d %s", (type.kind==BasicTypeDeclaration::ALIAS ? "Alias of" : "Base"), get_label(*type.base_type), type.base_type->name));
305 if(type.kind==BasicTypeDeclaration::VECTOR)
306 branches.push_back(format("Vector: %d", type.size));
307 else if(type.kind==BasicTypeDeclaration::MATRIX)
308 branches.push_back(format("Matrix: %dx%d", type.size&0xFFFF, type.size>>16));
309 append_subtree(branches);
312 void DumpTree::visit(ImageTypeDeclaration &type)
314 static const char *dims[] = { "1D", "2D", "3D", "Cube" };
316 append(format("%%%d typedef %s", get_label(type), type.name));
318 vector<Branch> branches;
319 branches.push_back(format("Dimensions: %s%s", dims[type.dimensions-1], (type.array ? " array" : "")));
321 branches.push_back(format("Element type: %%%d %s", get_label(*type.base_type), type.base_type->name));
323 branches.push_back("Shadow");
324 append_subtree(branches);
327 void DumpTree::visit(StructDeclaration &strct)
329 annotated_branch(format("%%%d struct %s", get_label(strct), strct.name), strct.members);
332 void DumpTree::visit(VariableDeclaration &var)
334 string decl = format("%%%d ", get_label(var));
337 if(!var.interpolation.empty())
338 decl += format("%s ", var.interpolation);
339 if(!var.sampling.empty())
340 decl += format("%s ", var.sampling);
341 if(!var.interface.empty())
342 decl += format("%s ", var.interface);
343 if(!var.precision.empty())
344 decl += format("%s ", var.precision);
345 decl += format("%s %s", var.type, var.name);
346 if(var.source==BUILTIN_SOURCE)
347 decl += " (builtin)";
348 else if(var.linked_declaration)
352 vector<Branch> branches;
353 if(var.type_declaration)
354 branches.push_back(format("Type: %%%d %s", get_label(*var.type_declaration), var.type_declaration->name));
356 branches.push_back(var.layout.get());
360 branches.push_back(Branch("Array []", var.array_size.get()));
362 branches.push_back("Array []");
364 if(var.init_expression)
365 branches.push_back(var.init_expression.get());
366 append_subtree(branches);
369 void DumpTree::visit(InterfaceBlock &iface)
372 if(!iface.instance_name.empty())
373 head += format("%%%d ", get_label(iface));
374 head += format("%s %s", iface.interface, iface.name);
375 if(!iface.instance_name.empty())
376 head += format(" %s", iface.instance_name);
379 if(iface.source==BUILTIN_SOURCE)
380 head += " (builtin)";
381 else if(iface.linked_block)
387 if(iface.type_declaration)
388 append(format("Type: %%%d %s", get_label(*iface.type_declaration), iface.type_declaration->name));
389 else if(iface.members)
390 iface.members->visit(*this);
394 void DumpTree::visit(FunctionDeclaration &func)
396 string text = format("%%%d %s %s%s", get_label(func), func.return_type, func.name, (func.signature.empty() ? "(?)" : func.signature));
397 if(func.source==BUILTIN_SOURCE)
398 text += " (builtin)";
399 else if(!func.definition)
400 text += " (undefined)";
404 if(func.return_type_declaration)
405 append(format("Return type: %%%d %s", get_label(*func.return_type_declaration), func.return_type_declaration->name));
406 for(NodeArray<VariableDeclaration>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
409 if(func.definition==&func)
410 func.body.visit(*this);
411 else if(func.definition)
412 append(format("Definition: %%%d", get_label(*func.definition)));
416 void DumpTree::visit(Conditional &cond)
420 vector<Branch> branches;
421 branches.push_back(cond.condition.get());
422 branches.push_back(&cond.body);
423 if(!cond.else_body.body.empty())
424 branches.push_back(&cond.else_body);
425 append_subtree(branches);
428 void DumpTree::visit(Iteration &iter)
433 if(iter.init_statement)
434 annotated_branch("Initialization", *iter.init_statement);
436 annotated_branch("Condition", *iter.condition);
437 if(iter.loop_expression)
438 annotated_branch("Loop", *iter.loop_expression);
440 annotated_branch("Body", iter.body);
444 void DumpTree::visit(Passthrough &pass)
447 annotated_branch("passthrough[]", *pass.subscript);
449 append("passthrough;");
452 void DumpTree::visit(Return &ret)
455 annotated_branch("return", *ret.expression);
460 void DumpTree::visit(Jump &jump)
462 append(format("%s;", jump.keyword));