]> git.tdb.fi Git - libs/gl.git/commitdiff
Move unique name generation to syntax.cpp
authorMikko Rasa <tdb@tdb.fi>
Sun, 14 Mar 2021 13:51:32 +0000 (15:51 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sun, 14 Mar 2021 16:56:35 +0000 (18:56 +0200)
It's going to be needed elsewhere too.

source/glsl/optimize.cpp
source/glsl/optimize.h
source/glsl/syntax.cpp
source/glsl/syntax.h

index 220017971a2f75001cb6e30faeb4a4fecc2b9fa9..c8c9041fd43c7c9814cbd53e4d3ab8a719252cb2 100644 (file)
@@ -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<VariableDeclaration> 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<VariableDeclaration> var = new VariableDeclaration;
                var->source = ret.source;
                var->line = ret.line;
index fa75e6d7e867cdbb9f18f4a8bd0896e89f33f663..ab954b58a471431feab1d6da0f75e9a2abe3bbaa 100644 (file)
@@ -55,8 +55,6 @@ public:
        const std::string &apply(Stage &, FunctionDeclaration &, Block &, const NodeList<Statement>::iterator &, FunctionDeclaration &);
 
 private:
-       std::string create_unused_name(const std::string &, bool);
-
        virtual void visit(VariableReference &);
        virtual void visit(InterfaceBlockReference &);
        virtual void visit(FunctionCall &);
index 469ce5a602c2f22a464a5896c003c5aa209d29ef..cb50fee18b79538c126482594a023ea8f3864001 100644 (file)
@@ -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
index ac876db44e2e6b0f95c9224f5df6a74333a6ce0a..55823d34bb8acad73c17a1018f2c56804f8e7b7d 100644 (file)
@@ -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