From b89239e1d616f0c1d8e432b7df1eca149585cf5e Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 14 Mar 2021 15:51:32 +0200 Subject: [PATCH] Move unique name generation to syntax.cpp It's going to be needed elsewhere too. --- source/glsl/optimize.cpp | 18 ++---------------- source/glsl/optimize.h | 2 -- source/glsl/syntax.cpp | 31 +++++++++++++++++++++++++++++++ source/glsl/syntax.h | 2 ++ 4 files changed, 35 insertions(+), 18 deletions(-) diff --git a/source/glsl/optimize.cpp b/source/glsl/optimize.cpp index 22001797..c8c9041f 100644 --- a/source/glsl/optimize.cpp +++ b/source/glsl/optimize.cpp @@ -91,20 +91,6 @@ const string &InlineContentInjector::apply(Stage &stage, FunctionDeclaration &ta return r_result_name; } -string InlineContentInjector::create_unused_name(const string &base, bool always_prefix) -{ - string result = base; - if(always_prefix || target_block->variables.count(result)) - result = format("_%s_%s", source_func->name, base); - unsigned initial_size = result.size(); - for(unsigned i=1; target_block->variables.count(result); ++i) - { - result.erase(initial_size); - result += format("_%d", i); - } - return result; -} - void InlineContentInjector::visit(VariableReference &var) { if(remap_names) @@ -152,7 +138,7 @@ void InlineContentInjector::visit(VariableDeclaration &var) if(!remap_names && !deps_only) { RefPtr inlined_var = var.clone(); - inlined_var->name = create_unused_name(var.name, false); + inlined_var->name = get_unused_variable_name(*target_block, var.name, source_func->name); r_inlined_statement = inlined_var; variable_map[var.name] = inlined_var.get(); @@ -167,7 +153,7 @@ void InlineContentInjector::visit(Return &ret) { /* Create a new variable to hold the return value of the inlined function. */ - r_result_name = create_unused_name("return", true); + r_result_name = get_unused_variable_name(*target_block, "_return", source_func->name); RefPtr var = new VariableDeclaration; var->source = ret.source; var->line = ret.line; diff --git a/source/glsl/optimize.h b/source/glsl/optimize.h index fa75e6d7..ab954b58 100644 --- a/source/glsl/optimize.h +++ b/source/glsl/optimize.h @@ -55,8 +55,6 @@ public: const std::string &apply(Stage &, FunctionDeclaration &, Block &, const NodeList::iterator &, FunctionDeclaration &); private: - std::string create_unused_name(const std::string &, bool); - virtual void visit(VariableReference &); virtual void visit(InterfaceBlockReference &); virtual void visit(FunctionCall &); diff --git a/source/glsl/syntax.cpp b/source/glsl/syntax.cpp index 469ce5a6..cb50fee1 100644 --- a/source/glsl/syntax.cpp +++ b/source/glsl/syntax.cpp @@ -464,6 +464,37 @@ Module::Module(): shared(Stage::SHARED) { } + +string get_unused_variable_name(const Block &block, const string &base, const string &prefix_hint) +{ + string name = base; + + bool prefixed = false; + unsigned number = 1; + unsigned size_without_number = name.size(); + while(block.variables.count(name)) + { + if(!prefixed && !prefix_hint.empty()) + { + if(name.front()!='_') + name = "_"+name; + name = prefix_hint+name; + if(name.front()!='_') + name = "_"+name; + prefixed = true; + size_without_number = name.size(); + } + else + { + name.erase(size_without_number); + name += format("_%d", number); + ++number; + } + } + + return name; +} + } // namespace SL } // namespace GL } // namespace Msp diff --git a/source/glsl/syntax.h b/source/glsl/syntax.h index ac876db4..55823d34 100644 --- a/source/glsl/syntax.h +++ b/source/glsl/syntax.h @@ -545,6 +545,8 @@ struct Module Module(); }; +std::string get_unused_variable_name(const Block &, const std::string &, const std::string &); + } // namespace SL } // namespace GL } // namespace Msp -- 2.43.0