From: Mikko Rasa Date: Wed, 3 Mar 2021 17:55:55 +0000 (+0200) Subject: Minor, largely cosmetic tweaks X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=2989fc60f0fd98282962a0105bcba055193e2f8c;p=libs%2Fgl.git Minor, largely cosmetic tweaks --- diff --git a/source/glsl/debug.cpp b/source/glsl/debug.cpp index 04d7aac8..06bf1925 100644 --- a/source/glsl/debug.cpp +++ b/source/glsl/debug.cpp @@ -312,10 +312,10 @@ void DumpTree::visit(FunctionDeclaration &func) for(NodeArray::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i) (*i)->visit(*this); last_branch(); - if(func.definition!=&func) - append(format("Definition: %%%d", get_label(*func.definition))); - else + if(func.definition==&func) func.body.visit(*this); + else if(func.definition) + append(format("Definition: %%%d", get_label(*func.definition))); end_sub(); } diff --git a/source/glsl/generate.cpp b/source/glsl/generate.cpp index 4230a402..90128a18 100644 --- a/source/glsl/generate.cpp +++ b/source/glsl/generate.cpp @@ -118,8 +118,8 @@ VariableResolver::VariableResolver(): stage(0), r_members(0), record_target(false), - r_assignment_target(0), - r_self_referencing(false) + r_self_referencing(false), + r_assignment_target(0) { } void VariableResolver::apply(Stage &s) @@ -240,7 +240,7 @@ void VariableResolver::visit(BinaryExpression &binary) if(binary.oper->token[0]=='[') { { - SetForScope set(record_target, false); + SetFlag set(record_target, false); binary.right->visit(*this); } r_members = 0; @@ -264,13 +264,13 @@ void VariableResolver::visit(Assignment &assign) SetFlag set(record_target); r_assignment_target = 0; assign.left->visit(*this); + assign.target_declaration = r_assignment_target; } r_self_referencing = false; assign.right->visit(*this); - assign.self_referencing = (r_self_referencing || assign.oper->token[0]!='='); - assign.target_declaration = r_assignment_target; + r_members = 0; r_iface_ref = 0; } diff --git a/source/glsl/generate.h b/source/glsl/generate.h index f76c2c2d..2cf5c54a 100644 --- a/source/glsl/generate.h +++ b/source/glsl/generate.h @@ -65,8 +65,8 @@ private: RefPtr r_iface_ref; std::string block_interface; bool record_target; - VariableDeclaration *r_assignment_target; bool r_self_referencing; + VariableDeclaration *r_assignment_target; public: VariableResolver(); diff --git a/source/glsl/optimize.cpp b/source/glsl/optimize.cpp index 56ff34d2..3356a3c9 100644 --- a/source/glsl/optimize.cpp +++ b/source/glsl/optimize.cpp @@ -68,9 +68,9 @@ InlineContentInjector::InlineContentInjector(): deps_only(false) { } -const string &InlineContentInjector::apply(Stage &stage, FunctionDeclaration &target_func, Block &tgtb, const NodeList::iterator &ins_pt, FunctionDeclaration &src) +const string &InlineContentInjector::apply(Stage &stage, FunctionDeclaration &target_func, Block &tgt_blk, const NodeList::iterator &ins_pt, FunctionDeclaration &src) { - target_block = &tgtb; + target_block = &tgt_blk; source_func = &src; for(NodeList::iterator i=src.body.body.begin(); i!=src.body.body.end(); ++i) { @@ -81,7 +81,7 @@ const string &InlineContentInjector::apply(Stage &stage, FunctionDeclaration &ta SetFlag set_remap(remap_names); r_inlined_statement->visit(*this); - tgtb.body.insert(ins_pt, r_inlined_statement); + tgt_blk.body.insert(ins_pt, r_inlined_statement); } NodeReorderer().apply(stage, target_func, dependencies); @@ -474,7 +474,7 @@ void UnusedVariableRemover::visit(BinaryExpression &binary) if(assignment_target) r_assign_to_subfield = true; binary.left->visit(*this); - SetForScope set(assignment_target, false); + SetFlag set(assignment_target, false); binary.right->visit(*this); } else @@ -484,7 +484,7 @@ void UnusedVariableRemover::visit(BinaryExpression &binary) void UnusedVariableRemover::visit(Assignment &assign) { { - SetForScope set(assignment_target, !assign.self_referencing); + SetFlag set(assignment_target, !assign.self_referencing); assign.left->visit(*this); } assign.right->visit(*this); @@ -567,7 +567,8 @@ void UnusedVariableRemover::visit(FunctionDeclaration &func) BlockVariableMap &block_variables = variables.back(); for(BlockVariableMap::iterator i=block_variables.begin(); i!=block_variables.end(); ++i) - i->second.conditionally_assigned = true; + if(!i->second.local) + i->second.conditionally_assigned = true; for(NodeArray::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i) block_variables[i->get()].referenced = true; merge_down_variables(); diff --git a/source/glsl/output.cpp b/source/glsl/output.cpp index 7d502739..72af8379 100644 --- a/source/glsl/output.cpp +++ b/source/glsl/output.cpp @@ -82,6 +82,32 @@ void Formatter::set_source(unsigned index, unsigned line) source_line = line; } +void Formatter::visit(Block &block) +{ + unsigned brace_indent = indent; + bool use_braces = (block.use_braces || (indent && block.body.size()!=1)); + if(use_braces) + append(format("%s{\n", string(brace_indent*2, ' '))); + + SetForScope set(indent, indent+(indent>0 || use_braces)); + string spaces(indent*2, ' '); + bool first = true; + for(NodeList::iterator i=block.body.begin(); i!=block.body.end(); ++i) + { + if((*i)->source==BUILTIN_SOURCE) + continue; + if(!first) + append('\n'); + first = false; + set_source((*i)->source, (*i)->line); + append(spaces); + (*i)->visit(*this); + } + + if(use_braces) + append(format("\n%s}", string(brace_indent*2, ' '))); +} + void Formatter::visit(Literal &literal) { append(literal.token); @@ -153,32 +179,6 @@ void Formatter::visit(ExpressionStatement &expr) append(';'); } -void Formatter::visit(Block &block) -{ - unsigned brace_indent = indent; - bool use_braces = (block.use_braces || (indent && block.body.size()!=1)); - if(use_braces) - append(format("%s{\n", string(brace_indent*2, ' '))); - - SetForScope set(indent, indent+(indent>0 || use_braces)); - string spaces(indent*2, ' '); - bool first = true; - for(NodeList::iterator i=block.body.begin(); i!=block.body.end(); ++i) - { - if((*i)->source==BUILTIN_SOURCE) - continue; - if(!first) - append('\n'); - first = false; - set_source((*i)->source, (*i)->line); - append(spaces); - (*i)->visit(*this); - } - - if(use_braces) - append(format("\n%s}", string(brace_indent*2, ' '))); -} - void Formatter::visit(Import &import) { append(format("import %s;", import.module)); diff --git a/source/glsl/visitor.cpp b/source/glsl/visitor.cpp index f4b2a8d0..3d832c73 100644 --- a/source/glsl/visitor.cpp +++ b/source/glsl/visitor.cpp @@ -7,12 +7,6 @@ namespace Msp { namespace GL { namespace SL { -void NodeVisitor::visit(Assignment &assign) -{ - visit(static_cast(assign)); -} - - void TraversingVisitor::visit(Block &block) { if(&block!=current_block) @@ -43,6 +37,11 @@ void TraversingVisitor::visit(BinaryExpression &binary) binary.right->visit(*this); } +void TraversingVisitor::visit(Assignment &assign) +{ + visit(static_cast(assign)); +} + void TraversingVisitor::visit(FunctionCall &call) { for(NodeArray::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i) diff --git a/source/glsl/visitor.h b/source/glsl/visitor.h index e0d74f62..cd9dafb0 100644 --- a/source/glsl/visitor.h +++ b/source/glsl/visitor.h @@ -25,7 +25,7 @@ public: virtual void visit(MemberAccess &) { } virtual void visit(UnaryExpression &) { } virtual void visit(BinaryExpression &) { } - virtual void visit(Assignment &); + virtual void visit(Assignment &) { } virtual void visit(FunctionCall &) { } virtual void visit(ExpressionStatement &) { } virtual void visit(Import &) { } @@ -58,6 +58,7 @@ public: virtual void visit(MemberAccess &); virtual void visit(UnaryExpression &); virtual void visit(BinaryExpression &); + virtual void visit(Assignment &); virtual void visit(FunctionCall &); virtual void visit(ExpressionStatement &); virtual void visit(InterfaceLayout &);