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)
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();
{
/* 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;
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 &);
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