summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/binary-reader-interpreter.cc2
-rw-r--r--src/binding-hash.cc20
-rw-r--r--src/binding-hash.h19
-rw-r--r--src/interpreter.cc2
-rw-r--r--src/ir.cc8
-rw-r--r--src/resolve-names.cc35
-rw-r--r--src/tools/wasm-link.cc2
-rw-r--r--src/validator.cc25
8 files changed, 42 insertions, 71 deletions
diff --git a/src/binary-reader-interpreter.cc b/src/binary-reader-interpreter.cc
index 6ddc9f52..c912ba51 100644
--- a/src/binary-reader-interpreter.cc
+++ b/src/binary-reader-interpreter.cc
@@ -649,7 +649,7 @@ wabt::Result BinaryReaderInterpreter::AppendExport(Module* module,
ExternalKind kind,
Index item_index,
StringSlice name) {
- if (module->export_bindings.find_index(name) != -1) {
+ if (module->export_bindings.FindIndex(name) != -1) {
PrintError("duplicate export \"" PRIstringslice "\"",
WABT_PRINTF_STRING_SLICE_ARG(name));
return wabt::Result::Error;
diff --git a/src/binding-hash.cc b/src/binding-hash.cc
index 1e87ad5b..ec180603 100644
--- a/src/binding-hash.cc
+++ b/src/binding-hash.cc
@@ -21,17 +21,16 @@
namespace wabt {
-void BindingHash::find_duplicates(DuplicateCallback callback,
- void* user_data) const {
+void BindingHash::FindDuplicates(DuplicateCallback callback) const {
if (size() > 0) {
ValueTypeVector duplicates;
- create_duplicates_vector(&duplicates);
- sort_duplicates_vector_by_location(&duplicates);
- call_callbacks(duplicates, callback, user_data);
+ CreateDuplicatesVector(&duplicates);
+ SortDuplicatesVectorByLocation(&duplicates);
+ CallCallbacks(duplicates, callback);
}
}
-void BindingHash::create_duplicates_vector(
+void BindingHash::CreateDuplicatesVector(
ValueTypeVector* out_duplicates) const {
// This relies on the fact that in an unordered_multimap, all values with the
// same key are adjacent in iteration order.
@@ -50,7 +49,7 @@ void BindingHash::create_duplicates_vector(
}
}
-void BindingHash::sort_duplicates_vector_by_location(
+void BindingHash::SortDuplicatesVectorByLocation(
ValueTypeVector* duplicates) const {
std::sort(
duplicates->begin(), duplicates->end(),
@@ -61,9 +60,8 @@ void BindingHash::sort_duplicates_vector_by_location(
});
}
-void BindingHash::call_callbacks(const ValueTypeVector& duplicates,
- DuplicateCallback callback,
- void* user_data) const {
+void BindingHash::CallCallbacks(const ValueTypeVector& duplicates,
+ DuplicateCallback callback) const {
// Loop through all duplicates in order, and call callback with first
// occurrence.
for (auto iter = duplicates.begin(), end = duplicates.end(); iter != end;
@@ -75,7 +73,7 @@ void BindingHash::call_callbacks(const ValueTypeVector& duplicates,
if (first == iter)
continue;
assert(first != duplicates.end());
- callback(**first, **iter, user_data);
+ callback(**first, **iter);
}
}
diff --git a/src/binding-hash.h b/src/binding-hash.h
index 4aa8c168..6222a254 100644
--- a/src/binding-hash.h
+++ b/src/binding-hash.h
@@ -17,6 +17,7 @@
#ifndef WABT_BINDING_HASH_H_
#define WABT_BINDING_HASH_H_
+#include <functional>
#include <string>
#include <vector>
#include <unordered_map>
@@ -40,13 +41,12 @@ struct Binding {
// object through a pointer to std::unordered_multimap.
class BindingHash : public std::unordered_multimap<std::string, Binding> {
public:
- typedef void (*DuplicateCallback)(const value_type& a,
- const value_type& b,
- void* user_data);
+ typedef std::function<void(const value_type&, const value_type&)>
+ DuplicateCallback;
- void find_duplicates(DuplicateCallback callback, void* user_data) const;
+ void FindDuplicates(DuplicateCallback callback) const;
- int find_index(const StringSlice& name) const {
+ int FindIndex(const StringSlice& name) const {
auto iter = find(string_slice_to_string(name));
if (iter != end())
return iter->second.index;
@@ -56,11 +56,10 @@ class BindingHash : public std::unordered_multimap<std::string, Binding> {
private:
typedef std::vector<const value_type*> ValueTypeVector;
- void create_duplicates_vector(ValueTypeVector* out_duplicates) const;
- void sort_duplicates_vector_by_location(ValueTypeVector* duplicates) const;
- void call_callbacks(const ValueTypeVector& duplicates,
- DuplicateCallback callback,
- void* user_data) const;
+ void CreateDuplicatesVector(ValueTypeVector* out_duplicates) const;
+ void SortDuplicatesVectorByLocation(ValueTypeVector* duplicates) const;
+ void CallCallbacks(const ValueTypeVector& duplicates,
+ DuplicateCallback callback) const;
};
} // namespace wabt
diff --git a/src/interpreter.cc b/src/interpreter.cc
index 11921f7a..6b689627 100644
--- a/src/interpreter.cc
+++ b/src/interpreter.cc
@@ -164,7 +164,7 @@ Module::~Module() {
}
Export* Module::GetExport(StringSlice name) {
- int field_index = export_bindings.find_index(name);
+ int field_index = export_bindings.FindIndex(name);
if (field_index < 0)
return nullptr;
return &exports[field_index];
diff --git a/src/ir.cc b/src/ir.cc
index 07c1bc2c..ef4ee00f 100644
--- a/src/ir.cc
+++ b/src/ir.cc
@@ -23,12 +23,12 @@ namespace wabt {
Index get_index_from_var(const BindingHash* hash, const Var* var) {
if (var->type == VarType::Name)
- return hash->find_index(var->name);
+ return hash->FindIndex(var->name);
return var->index;
}
Export* get_export_by_name(const Module* module, const StringSlice* name) {
- Index index = module->export_bindings.find_index(*name);
+ Index index = module->export_bindings.FindIndex(*name);
if (index >= module->exports.size())
return nullptr;
return module->exports[index];
@@ -58,11 +58,11 @@ Index get_local_index_by_var(const Func* func, const Var* var) {
if (var->type == VarType::Index)
return var->index;
- Index result = func->param_bindings.find_index(var->name);
+ Index result = func->param_bindings.FindIndex(var->name);
if (result != kInvalidIndex)
return result;
- result = func->local_bindings.find_index(var->name);
+ result = func->local_bindings.FindIndex(var->name);
if (result == kInvalidIndex)
return result;
diff --git a/src/resolve-names.cc b/src/resolve-names.cc
index b5958f4a..ea3a68fa 100644
--- a/src/resolve-names.cc
+++ b/src/resolve-names.cc
@@ -60,9 +60,6 @@ class NameResolver : public ExprVisitor::DelegateNop {
void PrintError(const Location* loc, const char* fmt, ...);
void PushLabel(Label* label);
void PopLabel();
- static void OnDuplicateBinding(const BindingHash::value_type& a,
- const BindingHash::value_type& b,
- void* user_data);
void CheckDuplicateBindings(const BindingHash* bindings, const char* desc);
void ResolveLabelVar(Var* var);
void ResolveVar(const BindingHash* bindings, Var* var, const char* desc);
@@ -118,31 +115,17 @@ void NameResolver::PopLabel() {
labels_.pop_back();
}
-struct FindDuplicateBindingContext {
- NameResolver* resolver;
- const char* desc;
-};
-
-// static
-void NameResolver::OnDuplicateBinding(const BindingHash::value_type& a,
- const BindingHash::value_type& b,
- void* user_data) {
- FindDuplicateBindingContext* fdbc =
- static_cast<FindDuplicateBindingContext*>(user_data);
- /* choose the location that is later in the file */
- const Location& a_loc = a.second.loc;
- const Location& b_loc = b.second.loc;
- const Location& loc = a_loc.line > b_loc.line ? a_loc : b_loc;
- fdbc->resolver->PrintError(&loc, "redefinition of %s \"%s\"", fdbc->desc,
- a.first.c_str());
-}
-
void NameResolver::CheckDuplicateBindings(const BindingHash* bindings,
const char* desc) {
- FindDuplicateBindingContext fdbc;
- fdbc.resolver = this;
- fdbc.desc = desc;
- bindings->find_duplicates(OnDuplicateBinding, &fdbc);
+ bindings->FindDuplicates([this, desc](const BindingHash::value_type& a,
+ const BindingHash::value_type& b) {
+ // Choose the location that is later in the file.
+ const Location& a_loc = a.second.loc;
+ const Location& b_loc = b.second.loc;
+ const Location& loc = a_loc.line > b_loc.line ? a_loc : b_loc;
+ PrintError(&loc, "redefinition of %s \"%s\"", desc, a.first.c_str());
+
+ });
}
void NameResolver::ResolveLabelVar(Var* var) {
diff --git a/src/tools/wasm-link.cc b/src/tools/wasm-link.cc
index bca366dc..c2a953e4 100644
--- a/src/tools/wasm-link.cc
+++ b/src/tools/wasm-link.cc
@@ -693,7 +693,7 @@ static void resolve_symbols(Context* ctx) {
LinkerInputBinary* binary = ctx->inputs[i].get();
for (size_t j = 0; j < binary->function_imports.size(); j++) {
FunctionImport* import = &binary->function_imports[j];
- int export_index = export_map.find_index(import->name);
+ int export_index = export_map.FindIndex(import->name);
if (export_index == -1) {
if (!s_relocatable)
WABT_FATAL("undefined symbol: " PRIstringslice "\n",
diff --git a/src/validator.cc b/src/validator.cc
index 58de1f90..5e048bb5 100644
--- a/src/validator.cc
+++ b/src/validator.cc
@@ -125,8 +125,6 @@ class Validator {
void CheckImport(const Location* loc, const Import* import);
void CheckExport(const Export* export_);
- void OnDuplicateBinding(const BindingHash::value_type& a,
- const BindingHash::value_type& b);
void CheckDuplicateExportBindings(const Module* module);
void CheckModule(const Module* module);
const TypeVector* CheckInvoke(const Action* action);
@@ -779,22 +777,15 @@ void Validator::CheckExport(const Export* export_) {
}
}
-void Validator::OnDuplicateBinding(const BindingHash::value_type& a,
- const BindingHash::value_type& b) {
- // Choose the location that is later in the file.
- const Location& a_loc = a.second.loc;
- const Location& b_loc = b.second.loc;
- const Location& loc = a_loc.line > b_loc.line ? a_loc : b_loc;
- PrintError(&loc, "redefinition of export \"%s\"", a.first.c_str());
-}
-
void Validator::CheckDuplicateExportBindings(const Module* module) {
- module->export_bindings.find_duplicates(
- [](const BindingHash::value_type& a, const BindingHash::value_type& b,
- void* user_data) {
- static_cast<Validator*>(user_data)->OnDuplicateBinding(a, b);
- },
- this);
+ module->export_bindings.FindDuplicates([this](
+ const BindingHash::value_type& a, const BindingHash::value_type& b) {
+ // Choose the location that is later in the file.
+ const Location& a_loc = a.second.loc;
+ const Location& b_loc = b.second.loc;
+ const Location& loc = a_loc.line > b_loc.line ? a_loc : b_loc;
+ PrintError(&loc, "redefinition of export \"%s\"", a.first.c_str());
+ });
}
void Validator::CheckModule(const Module* module) {