summaryrefslogtreecommitdiff
path: root/src/generate-names.cc
diff options
context:
space:
mode:
authorBen Smith <binjimin@gmail.com>2017-03-16 14:49:06 -0700
committerGitHub <noreply@github.com>2017-03-16 14:49:06 -0700
commit5b642c3ce487b77102dec4fc4b55538cfbccc5ff (patch)
tree960aebda63374bd6d5660355f836e935b402bdc0 /src/generate-names.cc
parent81a64d68c7ce50617bac025061d8f6db91b00ee9 (diff)
downloadwabt-5b642c3ce487b77102dec4fc4b55538cfbccc5ff.tar.gz
wabt-5b642c3ce487b77102dec4fc4b55538cfbccc5ff.tar.bz2
wabt-5b642c3ce487b77102dec4fc4b55538cfbccc5ff.zip
Use std::unordered_multimap for BindingHash (#357)
This change can't really be done in isolation, since once we add members with constructors to a struct, it is no longer has a trivial constructor. This propagates through all types that use it, etc. There are a number of changes that are ugly, but hopefully reduced the amount of code that needed to change. In particular, I changed some union members to pointers so they would stay trivially constructible. Another tricky change is the handling of duplicate bindings: previously we just relied on the fact that our hash implementation would be consistent. A nicer solution is to display the duplicated bindings in source order. There's probably a nicer way to do this; bikeshedding welcome. :-)
Diffstat (limited to 'src/generate-names.cc')
-rw-r--r--src/generate-names.cc38
1 files changed, 18 insertions, 20 deletions
diff --git a/src/generate-names.cc b/src/generate-names.cc
index 7da47662..130fd855 100644
--- a/src/generate-names.cc
+++ b/src/generate-names.cc
@@ -32,9 +32,11 @@ namespace wabt {
namespace {
struct Context {
+ Context() : module(nullptr), label_count(0) { WABT_ZERO_MEMORY(visitor); }
+
Module* module;
ExprVisitor visitor;
- StringSliceVector index_to_name;
+ std::vector<std::string> index_to_name;
uint32_t label_count;
};
@@ -70,9 +72,7 @@ static void generate_and_bind_name(BindingHash* bindings,
uint32_t index,
StringSlice* str) {
generate_name(prefix, index, str);
- Binding* binding;
- binding = insert_binding(bindings, str);
- binding->index = index;
+ bindings->emplace(string_slice_to_string(*str), Binding(index));
}
static void maybe_generate_and_bind_name(BindingHash* bindings,
@@ -83,17 +83,19 @@ static void maybe_generate_and_bind_name(BindingHash* bindings,
generate_and_bind_name(bindings, prefix, index, str);
}
-static void generate_and_bind_local_names(StringSliceVector* index_to_name,
- BindingHash* bindings,
- const char* prefix) {
- for (size_t i = 0; i < index_to_name->size; ++i) {
- StringSlice* old_name = &index_to_name->data[i];
- if (has_name(old_name))
+static void generate_and_bind_local_names(
+ Context* ctx,
+ BindingHash* bindings,
+ const char* prefix) {
+ for (size_t i = 0; i < ctx->index_to_name.size(); ++i) {
+ const std::string& old_name = ctx->index_to_name[i];
+ if (!old_name.empty())
continue;
StringSlice new_name;
generate_and_bind_name(bindings, prefix, i, &new_name);
- index_to_name->data[i] = new_name;
+ ctx->index_to_name[i] = string_slice_to_string(new_name);
+ destroy_string_slice(&new_name);
}
}
@@ -119,15 +121,13 @@ static Result visit_func(Context* ctx, uint32_t func_index, Func* func) {
maybe_generate_and_bind_name(&ctx->module->func_bindings, "$f", func_index,
&func->name);
- make_type_binding_reverse_mapping(&func->decl.sig.param_types,
- &func->param_bindings, &ctx->index_to_name);
- generate_and_bind_local_names(&ctx->index_to_name, &func->param_bindings,
- "$p");
+ make_type_binding_reverse_mapping(func->decl.sig.param_types,
+ func->param_bindings, &ctx->index_to_name);
+ generate_and_bind_local_names(ctx, &func->param_bindings, "$p");
- make_type_binding_reverse_mapping(&func->local_types, &func->local_bindings,
+ make_type_binding_reverse_mapping(func->local_types, func->local_bindings,
&ctx->index_to_name);
- generate_and_bind_local_names(&ctx->index_to_name, &func->local_bindings,
- "$l");
+ generate_and_bind_local_names(ctx, &func->local_bindings, "$l");
ctx->label_count = 0;
CHECK_RESULT(visit_func(func, &ctx->visitor));
@@ -180,14 +180,12 @@ static Result visit_module(Context* ctx, Module* module) {
Result generate_names(Module* module) {
Context ctx;
- WABT_ZERO_MEMORY(ctx);
ctx.visitor.user_data = &ctx;
ctx.visitor.begin_block_expr = begin_block_expr;
ctx.visitor.begin_loop_expr = begin_loop_expr;
ctx.visitor.begin_if_expr = begin_if_expr;
ctx.module = module;
Result result = visit_module(&ctx, module);
- destroy_string_slice_vector(&ctx.index_to_name);
return result;
}