diff options
author | Ben Smith <binjimin@gmail.com> | 2018-12-03 23:10:12 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-12-03 23:10:12 -0800 |
commit | 96e12c00b3bec700f851e94b63708ead1d7f84ad (patch) | |
tree | d51725fbdf3c687434a7b0a6c9ae4199fab5b7ab /src | |
parent | 48e4b094c54103fbf406d7566c950190d928c34c (diff) | |
download | wabt-96e12c00b3bec700f851e94b63708ead1d7f84ad.tar.gz wabt-96e12c00b3bec700f851e94b63708ead1d7f84ad.tar.bz2 wabt-96e12c00b3bec700f851e94b63708ead1d7f84ad.zip |
Rename duplicate locals (#968)
Similar to PR #512, duplicate locals are allowed in the `names` section,
but will produce an invalid wat file. We could support these properly
with custom annotations, but that isn't available yet. In the meantime,
we follow the same before as with functions and append `.1`, `.2`, etc
to the names to make them unique.
Diffstat (limited to 'src')
-rw-r--r-- | src/binary-reader-ir.cc | 36 |
1 files changed, 25 insertions, 11 deletions
diff --git a/src/binary-reader-ir.cc b/src/binary-reader-ir.cc index 86fba145..7c4b9699 100644 --- a/src/binary-reader-ir.cc +++ b/src/binary-reader-ir.cc @@ -240,6 +240,9 @@ class BinaryReaderIR : public BinaryReaderNop { Result AppendExpr(std::unique_ptr<Expr> expr); void SetBlockDeclaration(BlockDeclaration* decl, Type sig_type); + std::string GetUniqueName(BindingHash* bindings, + const std::string& original_name); + Errors* errors_ = nullptr; Module* module_ = nullptr; @@ -328,6 +331,16 @@ void BinaryReaderIR::SetBlockDeclaration(BlockDeclaration* decl, } } +std::string BinaryReaderIR::GetUniqueName(BindingHash* bindings, + const std::string& orig_name) { + int counter = 1; + std::string unique_name = orig_name; + while (bindings->count(unique_name) != 0) { + unique_name = orig_name + "." + std::to_string(counter++); + } + return unique_name; +} + bool BinaryReaderIR::OnError(const Error& error) { errors_->push_back(error); return true; @@ -946,7 +959,9 @@ Result BinaryReaderIR::OnElemSegmentCount(Index count) { return Result::Ok; } -Result BinaryReaderIR::BeginElemSegment(Index index, Index table_index, bool passive) { +Result BinaryReaderIR::BeginElemSegment(Index index, + Index table_index, + bool passive) { auto field = MakeUnique<ElemSegmentModuleField>(GetLocation()); ElemSegment& elem_segment = field->elem_segment; elem_segment.table_var = Var(table_index, GetLocation()); @@ -994,7 +1009,9 @@ Result BinaryReaderIR::OnDataSegmentCount(Index count) { return Result::Ok; } -Result BinaryReaderIR::BeginDataSegment(Index index, Index memory_index, bool passive) { +Result BinaryReaderIR::BeginDataSegment(Index index, + Index memory_index, + bool passive) { auto field = MakeUnique<DataSegmentModuleField>(GetLocation()); DataSegment& data_segment = field->data_segment; data_segment.memory_var = Var(memory_index, GetLocation()); @@ -1056,12 +1073,8 @@ Result BinaryReaderIR::OnFunctionName(Index index, string_view name) { } Func* func = module_->funcs[index]; - std::string dollar_name = MakeDollarName(name); - int counter = 1; - std::string orig_name = dollar_name; - while (module_->func_bindings.count(dollar_name) != 0) { - dollar_name = orig_name + "." + std::to_string(counter++); - } + std::string dollar_name = + GetUniqueName(&module_->func_bindings, MakeDollarName(name)); func->name = dollar_name; module_->func_bindings.emplace(dollar_name, Binding(index)); return Result::Ok; @@ -1135,15 +1148,16 @@ Result BinaryReaderIR::OnLocalName(Index func_index, BindingHash* bindings; Index index; if (local_index < num_params) { - /* param name */ + // param name bindings = &func->param_bindings; index = local_index; } else { - /* local name */ + // local name bindings = &func->local_bindings; index = local_index - num_params; } - bindings->emplace(MakeDollarName(name), Binding(index)); + bindings->emplace(GetUniqueName(bindings, MakeDollarName(name)), + Binding(index)); return Result::Ok; } |