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::begin_sub()
52 tree.back() = (tree.back()==BRANCH_LAST ? EMPTY : STRAIGHT);
53 tree.push_back(BRANCH);
56 void DumpTree::last_branch()
58 tree.back() = BRANCH_LAST;
61 void DumpTree::end_sub()
64 if(tree.back()==STRAIGHT)
68 void DumpTree::annotated_branch(const string &annotation, Node &node)
77 unsigned DumpTree::get_label(const Node &node)
79 unsigned &label = node_labels[&node];
81 label = node_labels.size();
85 string DumpTree::format_type(TypeDeclaration *type)
87 return (type ? type->name : "?");
91 typename T::const_iterator DumpTree::increment(typename T::const_iterator &iter, const T &container)
93 typename T::const_iterator ret = iter++;
94 if(iter==container.end())
99 void DumpTree::visit(Block &block)
101 append(format("Block %s", (block.use_braces ? "{}" : "(inline)")));
104 for(std::map<string, VariableDeclaration *>::const_iterator i=block.variables.begin(); i!=block.variables.end(); ++i)
105 append(format("Variable: %%%d %s %s", get_label(*i->second), i->second->type, i->first));
107 bool labeled_body = !block.variables.empty();
114 for(NodeList<Statement>::const_iterator i=block.body.begin(); i!=block.body.end(); )
116 NodeList<Statement>::const_iterator j = increment(i, block.body);
125 void DumpTree::visit(Literal &literal)
127 append(format("Literal: %s -> %s", literal.token, format_type(literal.type)));
130 void DumpTree::visit(ParenthesizedExpression &parexpr)
132 annotated_branch(format("(expr) -> %s", format_type(parexpr.type)), *parexpr.expression);
135 void DumpTree::visit(VariableReference &var)
139 text += format("%%%d ", get_label(*var.declaration));
140 text += format("%s (var) -> %s", var.name, format_type(var.type));
144 void DumpTree::visit(InterfaceBlockReference &iface)
147 if(iface.declaration)
148 text += format("%%%d ", get_label(*iface.declaration));
149 text += format("%s (iface)", iface.name);
153 void DumpTree::visit(MemberAccess &memacc)
155 string text = "Member access:";
156 if(memacc.declaration)
157 text += format(" %%%d", get_label(*memacc.declaration));
158 text += format(" .%s -> %s", memacc.member, format_type(memacc.type));
159 annotated_branch(text, *memacc.left);
162 void DumpTree::visit(UnaryExpression &unary)
164 string text = format("Unary: %s, %sfix -> %s", unary.oper->token, (unary.oper->type==Operator::PREFIX ? "pre" : "post"), format_type(unary.type));
165 annotated_branch(text, *unary.expression);
168 void DumpTree::visit(BinaryExpression &binary)
170 append(format("Binary: %s -> %s", (binary.oper->token[0]=='[' ? "[]" : binary.oper->token), format_type(binary.type)));
172 binary.left->visit(*this);
174 binary.right->visit(*this);
178 void DumpTree::visit(Assignment &assign)
180 append(format("Assignment: %s%s -> %s", assign.oper->token, (assign.self_referencing ? " (self-referencing)" : ""), format_type(assign.type)));
182 if(assign.target_declaration)
183 append(format("Target: %%%d %s %s", get_label(*assign.target_declaration), assign.target_declaration->type, assign.target_declaration->name));
184 assign.left->visit(*this);
186 assign.right->visit(*this);
190 void DumpTree::visit(FunctionCall &call)
192 string head = "Function call: ";
194 head += format("%%%d ", get_label(*call.declaration));
197 head += " (constructor)";
198 head += format(" -> %s", format_type(call.type));
202 for(NodeArray<Expression>::const_iterator i=call.arguments.begin(); i!=call.arguments.end(); )
204 NodeArray<Expression>::const_iterator j = increment(i, call.arguments);
210 void DumpTree::visit(ExpressionStatement &expr)
212 annotated_branch("expr;", *expr.expression);
215 void DumpTree::visit(Import &import)
217 append(format("import %s", import.module));
220 void DumpTree::visit(Precision &prec)
222 append(format("precision %s %s", prec.precision, prec.type));
225 void DumpTree::visit(Layout &layout)
229 for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); )
231 vector<Layout::Qualifier>::const_iterator j = increment(i, layout.qualifiers);
232 string qualifier = j->name;
234 qualifier += format("=%d", j->value);
240 void DumpTree::visit(InterfaceLayout &layout)
242 annotated_branch(format("Layout: %s", layout.interface), layout.layout);
245 void DumpTree::visit(BasicTypeDeclaration &type)
247 append(format("%%%d typedef %s", get_label(type), type.name));
249 if(type.kind!=BasicTypeDeclaration::VECTOR && type.kind!=BasicTypeDeclaration::MATRIX)
252 append(format("%s: %%%d %s", (type.kind==BasicTypeDeclaration::ALIAS ? "Alias of" : "Base"), get_label(*type.base_type), type.base_type->name));
255 if(type.kind==BasicTypeDeclaration::VECTOR)
256 append(format("Vector: %d", type.size));
257 else if(type.kind==BasicTypeDeclaration::MATRIX)
258 append(format("Matrix: %dx%d", type.size&0xFFFF, type.size>>16));
262 void DumpTree::visit(ImageTypeDeclaration &type)
264 append(format("%%%d typedef %s", get_label(type), type.name));
267 if(!type.shadow && !type.base_type)
269 static const char *dims[] = { "1D", "2D", "3D", "Cube" };
270 append(format("Dimensions: %s%s", dims[type.dimensions-1], (type.array ? " array" : "")));
275 append(format("Element type: %%%d %s", get_label(*type.base_type), type.base_type->name));
284 void DumpTree::visit(StructDeclaration &strct)
286 annotated_branch(format("%%%d struct %s", get_label(strct), strct.name), strct.members);
289 void DumpTree::visit(VariableDeclaration &var)
291 string decl = format("%%%d ", get_label(var));
294 if(!var.interpolation.empty())
295 decl += format("%s ", var.interpolation);
296 if(!var.sampling.empty())
297 decl += format("%s ", var.sampling);
298 if(!var.interface.empty())
299 decl += format("%s ", var.interface);
300 if(!var.precision.empty())
301 decl += format("%s ", var.precision);
302 decl += format("%s %s", var.type, var.name);
303 if(var.source==BUILTIN_SOURCE)
304 decl += " (builtin)";
305 else if(var.linked_declaration)
310 if(!var.layout && !var.array && !var.init_expression)
312 if(var.type_declaration)
313 append(format("Type: %%%d %s", get_label(*var.type_declaration), var.type_declaration->name));
315 if(!var.array && !var.init_expression)
318 var.layout->visit(*this);
320 if(!var.init_expression)
325 annotated_branch("Array []", *var.array_size);
331 if(var.init_expression)
332 var.init_expression->visit(*this);
336 void DumpTree::visit(InterfaceBlock &block)
339 if(!block.instance_name.empty())
340 head += format("%%%d ", get_label(block));
341 head += format("%s %s", block.interface, block.name);
342 if(!block.instance_name.empty())
343 head += format(" %s", block.instance_name);
346 if(block.source==BUILTIN_SOURCE)
347 head += " (builtin)";
348 else if(block.linked_block)
350 annotated_branch(head, block.members);
353 void DumpTree::visit(FunctionDeclaration &func)
355 string text = format("%%%d %s %s", get_label(func), func.return_type, func.name);
356 if(func.source==BUILTIN_SOURCE)
357 text += " (builtin)";
358 else if(!func.definition)
359 text += " (undefined)";
362 for(NodeArray<VariableDeclaration>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
365 if(func.definition==&func)
366 func.body.visit(*this);
367 else if(func.definition)
368 append(format("Definition: %%%d", get_label(*func.definition)));
372 void DumpTree::visit(Conditional &cond)
376 cond.condition->visit(*this);
377 if(cond.else_body.body.empty())
379 cond.body.visit(*this);
380 if(!cond.else_body.body.empty())
383 cond.else_body.visit(*this);
388 void DumpTree::visit(Iteration &iter)
393 if(iter.init_statement)
394 annotated_branch("Initialization", *iter.init_statement);
396 annotated_branch("Condition", *iter.condition);
397 if(iter.loop_expression)
398 annotated_branch("Loop", *iter.loop_expression);
400 annotated_branch("Body", iter.body);
405 void DumpTree::visit(Passthrough &pass)
407 append("passthrough");
412 pass.subscript->visit(*this);
417 void DumpTree::visit(Return &ret)
420 annotated_branch("return", *ret.expression);
425 void DumpTree::visit(Jump &jump)
427 append(format("%s;", jump.keyword));