]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/debug.cpp
Use emplace_back when a new object is being constructed
[libs/gl.git] / source / glsl / debug.cpp
index 8a76744a1a90bbeeeac5366489ab978c017c93ee..60ac76f252594f79b823f30cc8c1f015766e4d92 100644 (file)
@@ -8,27 +8,20 @@ namespace Msp {
 namespace GL {
 namespace SL {
 
-const std::string &DumpTree::apply(Stage &stage)
+string DumpTree::apply(Stage &stage)
 {
        formatted = format("Stage: %s\n", Stage::get_stage_name(stage.type));
-       tree.push_back(BRANCH);
+       begin_sub();
        append(format("Version: %d.%02d", stage.required_features.glsl_version.major, stage.required_features.glsl_version.minor));
 
-       for(std::map<string, TypeDeclaration *>::const_iterator i=stage.types.begin(); i!=stage.types.end(); ++i)
-               append(format("Type: %%%d %s", get_label(*i->second), i->first));
+       for(const auto &kvp: stage.types)
+               append(format("Type: %%%d %s", get_label(*kvp.second), kvp.first));
 
-       set<InterfaceBlock *> seen_interfaces;
-       for(std::map<string, InterfaceBlock *>::const_iterator i=stage.interface_blocks.begin(); i!=stage.interface_blocks.end(); ++i)
-               if(seen_interfaces.insert(i->second).second)
-               {
-                       string text = format("Interface block: %%%d %s %s", get_label(*i->second), i->second->interface, i->second->name);
-                       if(!i->second->instance_name.empty())
-                               text += format(" %s", i->second->instance_name);
-                       append(text);
-               }
+       for(const auto &kvp: stage.interface_blocks)
+               append(format("Interface block: %%%d %s", get_label(*kvp.second), kvp.first));
 
-       for(std::map<string, FunctionDeclaration *>::const_iterator i=stage.functions.begin(); i!=stage.functions.end(); ++i)
-               append(format("Function: %%%d %s", get_label(*i->second), i->first));
+       for(const auto &kvp: stage.functions)
+               append(format("Function: %%%d %s", get_label(*kvp.second), kvp.first));
 
        last_branch();
        stage.content.visit(*this);
@@ -38,7 +31,7 @@ const std::string &DumpTree::apply(Stage &stage)
 void DumpTree::append(const string &line)
 {
        StringCodec::Utf8::Encoder enc;
-       for(vector<TreeChars>::const_iterator i=tree.begin(); i!=tree.end(); )
+       for(auto i=tree.begin(); i!=tree.end(); )
        {
                enc.encode_char(*i++, formatted);
                enc.encode_char((i==tree.end() ? REACH : EMPTY), formatted);
@@ -47,9 +40,46 @@ void DumpTree::append(const string &line)
        formatted += '\n';
 }
 
+void DumpTree::append(const Node &node, const string &line)
+{
+       append(format("<%d:%d> %s", node.source, node.line, line));
+}
+
+void DumpTree::append_subtree(const vector<Branch> &branches)
+{
+       begin_sub();
+       for(auto i=branches.begin(); i!=branches.end(); )
+       {
+               auto j = increment(i, branches);
+               if(!j->text.empty())
+               {
+                       append(j->text);
+                       if(j->node)
+                       {
+                               begin_sub();
+                               last_branch();
+                               j->node->visit(*this);
+                               end_sub();
+                       }
+               }
+               else
+                       j->node->visit(*this);
+       }
+       end_sub();
+}
+
+void DumpTree::append_subtree(Node &node)
+{
+       begin_sub();
+       last_branch();
+       node.visit(*this);
+       end_sub();
+}
+
 void DumpTree::begin_sub()
 {
-       tree.back() = (tree.back()==BRANCH_LAST ? EMPTY : STRAIGHT);
+       if(!tree.empty())
+               tree.back() = (tree.back()==BRANCH_LAST ? EMPTY : STRAIGHT);
        tree.push_back(BRANCH);
 }
 
@@ -61,19 +91,10 @@ void DumpTree::last_branch()
 void DumpTree::end_sub()
 {
        tree.pop_back();
-       if(tree.back()==STRAIGHT)
+       if(!tree.empty() && tree.back()==STRAIGHT)
                tree.back() = BRANCH;
 }
 
-void DumpTree::annotated_branch(const string &annotation, Node &node)
-{
-       append(annotation);
-       begin_sub();
-       last_branch();
-       node.visit(*this);
-       end_sub();
-}
-
 unsigned DumpTree::get_label(const Node &node)
 {
        unsigned &label = node_labels[&node];
@@ -90,7 +111,7 @@ string DumpTree::format_type(TypeDeclaration *type)
 template<typename T>
 typename T::const_iterator DumpTree::increment(typename T::const_iterator &iter, const T &container)
 {
-       typename T::const_iterator ret = iter++;
+       auto ret = iter++;
        if(iter==container.end())
                last_branch();
        return ret;
@@ -98,38 +119,24 @@ typename T::const_iterator DumpTree::increment(typename T::const_iterator &iter,
 
 void DumpTree::visit(Block &block)
 {
-       append(format("Block %s", (block.use_braces ? "{}" : "(inline)")));
+       append(block, format("Block %s", (block.use_braces ? "{}" : "(inline)")));
        begin_sub();
 
-       for(std::map<string, VariableDeclaration *>::const_iterator i=block.variables.begin(); i!=block.variables.end(); ++i)
-               append(format("Variable: %%%d %s %s", get_label(*i->second), i->second->type, i->first));
+       for(const auto &kvp: block.variables)
+               append(format("Variable: %%%d %s %s", get_label(*kvp.second), kvp.second->type, kvp.first));
 
-       bool labeled_body = !block.variables.empty();
-       if(labeled_body)
-       {
-               last_branch();
-               append("Body");
-               begin_sub();
-       }
-       for(NodeList<Statement>::const_iterator i=block.body.begin(); i!=block.body.end(); )
+       for(auto i=block.body.cbegin(); i!=block.body.cend(); )
        {
-               NodeList<Statement>::const_iterator j = increment(i, block.body);
+               auto j = increment(i, block.body);
                (*j)->visit(*this);
        }
-       if(labeled_body)
-               end_sub();
 
        end_sub();
 }
 
 void DumpTree::visit(Literal &literal)
 {
-       append(format("Literal: %s -> %s", literal.token, format_type(literal.type)));
-}
-
-void DumpTree::visit(ParenthesizedExpression &parexpr)
-{
-       annotated_branch(format("(expr) -> %s", format_type(parexpr.type)), *parexpr.expression);
+       append(literal, format("Literal: %s -> %s", literal.token, format_type(literal.type)));
 }
 
 void DumpTree::visit(VariableReference &var)
@@ -138,7 +145,7 @@ void DumpTree::visit(VariableReference &var)
        if(var.declaration)
                text += format("%%%d ", get_label(*var.declaration));
        text += format("%s (var) -> %s", var.name, format_type(var.type));
-       append(text);
+       append(var, text);
 }
 
 void DumpTree::visit(InterfaceBlockReference &iface)
@@ -146,8 +153,8 @@ void DumpTree::visit(InterfaceBlockReference &iface)
        string text;
        if(iface.declaration)
                text += format("%%%d ", get_label(*iface.declaration));
-       text += format("%s (iface)", iface.name);
-       append(text);
+       text += format("%s (iface) -> %s", iface.name, format_type(iface.type));
+       append(iface, text);
 }
 
 void DumpTree::visit(MemberAccess &memacc)
@@ -155,19 +162,32 @@ void DumpTree::visit(MemberAccess &memacc)
        string text = "Member access:";
        if(memacc.declaration)
                text += format(" %%%d", get_label(*memacc.declaration));
-       text += format(" .%s -> %s", memacc.member, format_type(memacc.type));
-       annotated_branch(text, *memacc.left);
+       text += format(" .%s (%d) -> %s", memacc.member, memacc.index, format_type(memacc.type));
+       append(memacc, text);
+       append_subtree(*memacc.left);
+}
+
+void DumpTree::visit(Swizzle &swizzle)
+{
+       static const char components[4] = { 'x', 'y', 'z', 'w' };
+       string text = "Swizzle: .";
+       for(unsigned i=0; i<swizzle.count; ++i)
+               text += components[swizzle.components[i]];
+       text += format(" -> %s", format_type(swizzle.type));
+       append(swizzle, text);
+       append_subtree(*swizzle.left);
 }
 
 void DumpTree::visit(UnaryExpression &unary)
 {
        string text = format("Unary: %s, %sfix -> %s", unary.oper->token, (unary.oper->type==Operator::PREFIX ? "pre" : "post"), format_type(unary.type));
-       annotated_branch(text, *unary.expression);
+       append(unary, text);
+       append_subtree(*unary.expression);
 }
 
 void DumpTree::visit(BinaryExpression &binary)
 {
-       append(format("Binary: %s -> %s", (binary.oper->token[0]=='[' ? "[]" : binary.oper->token), format_type(binary.type)));
+       append(binary, format("Binary: %s%s -> %s", binary.oper->token, binary.oper->token2, format_type(binary.type)));
        begin_sub();
        binary.left->visit(*this);
        last_branch();
@@ -177,16 +197,51 @@ void DumpTree::visit(BinaryExpression &binary)
 
 void DumpTree::visit(Assignment &assign)
 {
-       append(format("Assignment: %s%s", assign.oper->token, (assign.self_referencing ? " (self-referencing)" : "")));
+       append(assign, format("Assignment: %s%s -> %s", assign.oper->token, (assign.self_referencing ? " (self-referencing)" : ""), format_type(assign.type)));
        begin_sub();
-       if(assign.target_declaration)
-               append(format("Target: %%%d %s %s", get_label(*assign.target_declaration), assign.target_declaration->type, assign.target_declaration->name));
+       if(assign.target.declaration)
+       {
+               string text = format("Target: %%%d", get_label(*assign.target.declaration));
+
+               static const char swizzle[4] = { 'x', 'y', 'z', 'w' };
+               for(unsigned i=0; i<assign.target.chain_len; ++i)
+               {
+                       unsigned component = assign.target.chain[i];
+                       switch(static_cast<Assignment::Target::ChainType>(component&0xC0))
+                       {
+                       case Assignment::Target::MEMBER:
+                               text += format(" .%d", component&0x3F);
+                               break;
+                       case Assignment::Target::SWIZZLE:
+                               text += " .";
+                               for(unsigned j=0; j<4; ++j)
+                                       if(component&(1<<j))
+                                               text += swizzle[j];
+                               break;
+                       case Assignment::Target::ARRAY:
+                               text += format(" [%d]", component&0x3F);
+                               break;
+                       }
+               }
+               append(text);
+       }
        assign.left->visit(*this);
        last_branch();
        assign.right->visit(*this);
        end_sub();
 }
 
+void DumpTree::visit(TernaryExpression &ternary)
+{
+       append(ternary, format("Ternary: %s%s -> %s", ternary.oper->token, ternary.oper->token2, format_type(ternary.type)));
+       begin_sub();
+       ternary.condition->visit(*this);
+       ternary.true_expr->visit(*this);
+       last_branch();
+       ternary.false_expr->visit(*this);
+       end_sub();
+}
+
 void DumpTree::visit(FunctionCall &call)
 {
        string head = "Function call: ";
@@ -196,12 +251,12 @@ void DumpTree::visit(FunctionCall &call)
        if(call.constructor)
                head += " (constructor)";
        head += format(" -> %s", format_type(call.type));
-       append(head);
+       append(call, head);
 
        begin_sub();
-       for(NodeArray<Expression>::const_iterator i=call.arguments.begin(); i!=call.arguments.end(); )
+       for(auto i=call.arguments.cbegin(); i!=call.arguments.cend(); )
        {
-               NodeArray<Expression>::const_iterator j = increment(i, call.arguments);
+               auto j = increment(i, call.arguments);
                (*j)->visit(*this);
        }
        end_sub();
@@ -209,26 +264,27 @@ void DumpTree::visit(FunctionCall &call)
 
 void DumpTree::visit(ExpressionStatement &expr)
 {
-       annotated_branch("expr;", *expr.expression);
+       append(expr, "expr;");
+       append_subtree(*expr.expression);
 }
 
 void DumpTree::visit(Import &import)
 {
-       append(format("import %s", import.module));
+       append(import, format("import %s", import.module));
 }
 
 void DumpTree::visit(Precision &prec)
 {
-       append(format("precision %s %s", prec.precision, prec.type));
+       append(prec, format("precision %s %s", prec.precision, prec.type));
 }
 
 void DumpTree::visit(Layout &layout)
 {
-       append("Layout");
+       append(layout, "Layout");
        begin_sub();
-       for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); )
+       for(auto i=layout.qualifiers.cbegin(); i!=layout.qualifiers.cend(); )
        {
-               vector<Layout::Qualifier>::const_iterator j = increment(i, layout.qualifiers);
+               auto j = increment(i, layout.qualifiers);
                string qualifier = j->name;
                if(j->has_value)
                        qualifier += format("=%d", j->value);
@@ -239,51 +295,43 @@ void DumpTree::visit(Layout &layout)
 
 void DumpTree::visit(InterfaceLayout &layout)
 {
-       annotated_branch(format("Layout: %s", layout.interface), layout.layout);
+       append(layout, format("Layout: %s", layout.interface));
+       append_subtree(layout.layout);
 }
 
 void DumpTree::visit(BasicTypeDeclaration &type)
 {
-       append(format("%%%d typedef %s", get_label(type), type.name));
-       begin_sub();
-       if(type.kind!=BasicTypeDeclaration::VECTOR && type.kind!=BasicTypeDeclaration::MATRIX)
-               last_branch();
-       if(type.base_type)
-               append(format("%s: %%%d %s", (type.kind==BasicTypeDeclaration::ALIAS ? "Alias of" : "Base"), get_label(*type.base_type), type.base_type->name));
+       append(type, format("%%%d typedef %s", get_label(type), type.name));
 
-       last_branch();
+       vector<Branch> branches;
+       if(type.base_type)
+               branches.emplace_back(format("%s: %%%d %s", (type.kind==BasicTypeDeclaration::ALIAS ? "Alias of" : "Base"), get_label(*type.base_type), type.base_type->name));
        if(type.kind==BasicTypeDeclaration::VECTOR)
-               append(format("Vector: %d", type.size));
+               branches.emplace_back(format("Vector: %d", type.size));
        else if(type.kind==BasicTypeDeclaration::MATRIX)
-               append(format("Matrix: %dx%d", type.size&0xFFFF, type.size>>16));
-       end_sub();
+               branches.emplace_back(format("Matrix: %dx%d", type.size&0xFFFF, type.size>>16));
+       append_subtree(branches);
 }
 
 void DumpTree::visit(ImageTypeDeclaration &type)
 {
-       append(format("%%%d typedef %s", get_label(type), type.name));
-       begin_sub();
+       static const char *const dims[] = { "1D", "2D", "3D", "Cube" };
 
-       if(!type.shadow && !type.base_type)
-               last_branch();
-       static const char *dims[] = { "1D", "2D", "3D", "Cube" };
-       append(format("Dimensions: %s%s", dims[type.dimensions-1], (type.array ? " array" : "")));
+       append(type, format("%%%d typedef %s", get_label(type), type.name));
 
-       if(!type.shadow)
-               last_branch();
+       vector<Branch> branches;
+       branches.emplace_back(format("Dimensions: %s%s", dims[type.dimensions-1], (type.array ? " array" : "")));
        if(type.base_type)
-               append(format("Element type: %%%d %s", get_label(*type.base_type), type.base_type->name));
-
-       last_branch();
+               branches.emplace_back(format("Element type: %%%d %s", get_label(*type.base_type), type.base_type->name));
        if(type.shadow)
-               append("Shadow");
-
-       end_sub();
+               branches.emplace_back("Shadow");
+       append_subtree(branches);
 }
 
 void DumpTree::visit(StructDeclaration &strct)
 {
-       annotated_branch(format("%%%d struct %s", get_label(strct), strct.name), strct.members);
+       append(strct, format("%%%d struct %s", get_label(strct), strct.name));
+       append_subtree(strct.members);
 }
 
 void DumpTree::visit(VariableDeclaration &var)
@@ -304,63 +352,62 @@ void DumpTree::visit(VariableDeclaration &var)
                decl += " (builtin)";
        else if(var.linked_declaration)
                decl += " (linked)";
-       append(decl);
+       append(var, decl);
 
-       begin_sub();
-       if(!var.layout && !var.array && !var.init_expression)
-               last_branch();
+       vector<Branch> branches;
        if(var.type_declaration)
-               append(format("Type: %%%d %s", get_label(*var.type_declaration), var.type_declaration->name));
-
-       if(!var.array && !var.init_expression)
-               last_branch();
+               branches.push_back(format("Type: %%%d %s", get_label(*var.type_declaration), var.type_declaration->name));
        if(var.layout)
-               var.layout->visit(*this);
-
-       if(!var.init_expression)
-               last_branch();
+               branches.push_back(var.layout.get());
        if(var.array)
        {
                if(var.array_size)
-                       annotated_branch("Array []", *var.array_size);
+                       branches.emplace_back("Array []", var.array_size.get());
                else
-                       append("Array []");
+                       branches.push_back("Array []");
        }
-
-       last_branch();
        if(var.init_expression)
-               var.init_expression->visit(*this);
-       end_sub();
+               branches.push_back(var.init_expression.get());
+       append_subtree(branches);
 }
 
-void DumpTree::visit(InterfaceBlock &block)
+void DumpTree::visit(InterfaceBlock &iface)
 {
-       string head;
-       if(!block.instance_name.empty())
-               head += format("%%%d ", get_label(block));
-       head += format("%s %s", block.interface, block.name);
-       if(!block.instance_name.empty())
-               head += format(" %s", block.instance_name);
-       if(block.array)
+       string head = format("%%%d %s %s", get_label(iface), iface.interface, iface.block_name);
+       if(!iface.instance_name.empty())
+               head += format(" %s", iface.instance_name);
+       if(iface.array)
                head += "[]";
-       if(block.source==BUILTIN_SOURCE)
+       if(iface.source==BUILTIN_SOURCE)
                head += " (builtin)";
-       else if(block.linked_block)
+       else if(iface.linked_block)
                head += " (linked)";
-       annotated_branch(head, block.members);
+       append(iface, head);
+
+       vector<Branch> branches;
+       if(iface.type_declaration)
+               branches.push_back(format("Type: %%%d %s", get_label(*iface.type_declaration), iface.type_declaration->name));
+       if(iface.layout)
+               branches.push_back(iface.layout.get());
+       if(iface.members)
+               branches.push_back(iface.members.get());
+       append_subtree(branches);
 }
 
 void DumpTree::visit(FunctionDeclaration &func)
 {
-       string text = format("%%%d %s %s", get_label(func), func.return_type, func.name);
+       string text = format("%%%d %s %s%s", get_label(func), func.return_type, func.name, (func.signature.empty() ? "(?)" : func.signature));
        if(func.source==BUILTIN_SOURCE)
                text += " (builtin)";
        else if(!func.definition)
                text += " (undefined)";
-       append(text);
+       append(func, text);
+
        begin_sub();
-       for(NodeArray<VariableDeclaration>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
-               (*i)->visit(*this);
+       if(func.return_type_declaration)
+               append(format("Return type: %%%d %s", get_label(*func.return_type_declaration), func.return_type_declaration->name));
+       for(const RefPtr<VariableDeclaration> &p: func.parameters)
+               p->visit(*this);
        last_branch();
        if(func.definition==&func)
                func.body.visit(*this);
@@ -371,60 +418,48 @@ void DumpTree::visit(FunctionDeclaration &func)
 
 void DumpTree::visit(Conditional &cond)
 {
-       append("if()");
-       begin_sub();
-       cond.condition->visit(*this);
-       if(cond.else_body.body.empty())
-               last_branch();
-       cond.body.visit(*this);
+       append(cond, "if()");
+
+       vector<Branch> branches;
+       branches.emplace_back(cond.condition.get());
+       branches.emplace_back("then", &cond.body);
        if(!cond.else_body.body.empty())
-       {
-               last_branch();
-               cond.else_body.visit(*this);
-       }
-       end_sub();
+               branches.emplace_back("else", &cond.else_body);
+       append_subtree(branches);
 }
 
 void DumpTree::visit(Iteration &iter)
 {
-       append("for()");
-       begin_sub();
+       append(iter, "for()");
 
+       vector<Branch> branches;
        if(iter.init_statement)
-               annotated_branch("Initialization", *iter.init_statement);
+               branches.emplace_back("Initialization", iter.init_statement.get());
        if(iter.condition)
-               annotated_branch("Condition", *iter.condition);
+               branches.emplace_back("Condition", iter.condition.get());
        if(iter.loop_expression)
-               annotated_branch("Loop", *iter.loop_expression);
-       last_branch();
-       annotated_branch("Body", iter.body);
-
-       end_sub();
+               branches.emplace_back("Loop", iter.loop_expression.get());
+       branches.emplace_back(&iter.body);
+       append_subtree(branches);
 }
 
 void DumpTree::visit(Passthrough &pass)
 {
-       append("passthrough");
+       append(pass, (pass.subscript ? "passthrough[]" : "passthrough"));
        if(pass.subscript)
-       {
-               begin_sub();
-               last_branch();
-               pass.subscript->visit(*this);
-               end_sub();
-       }
+               append_subtree(*pass.subscript);
 }
 
 void DumpTree::visit(Return &ret)
 {
+       append(ret, (ret.expression ? "return" : "return;"));
        if(ret.expression)
-               annotated_branch("return", *ret.expression);
-       else
-               append("return;");
+               append_subtree(*ret.expression);
 }
 
 void DumpTree::visit(Jump &jump)
 {
-       append(format("%s;", jump.keyword));
+       append(jump, format("%s;", jump.keyword));
 }
 
 } // namespace SL