diff options
author | Heejin Ahn <aheejin@gmail.com> | 2019-12-23 14:55:28 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-12-23 14:55:28 -0800 |
commit | f42d8f62151d4b34ece60a7a4c6b85ac117146cd (patch) | |
tree | 0c184a1954e324b4b351671d8b01b1dca4d91b36 /src | |
parent | 977aa84f357d051b0ddc60f204c6b743463c3c6f (diff) | |
download | binaryen-f42d8f62151d4b34ece60a7a4c6b85ac117146cd.tar.gz binaryen-f42d8f62151d4b34ece60a7a4c6b85ac117146cd.tar.bz2 binaryen-f42d8f62151d4b34ece60a7a4c6b85ac117146cd.zip |
Refactor module element related functions (NFC) (#2550)
This does something similar to #2489 for more functions, removing
boilerplate code for each module element using template functions.
Diffstat (limited to 'src')
-rw-r--r-- | src/wasm.h | 6 | ||||
-rw-r--r-- | src/wasm/wasm.cpp | 148 |
2 files changed, 72 insertions, 82 deletions
diff --git a/src/wasm.h b/src/wasm.h index 4c8c4d444..48adf103b 100644 --- a/src/wasm.h +++ b/src/wasm.h @@ -1389,10 +1389,14 @@ public: Export* addExport(Export* curr); Function* addFunction(Function* curr); - Function* addFunction(std::unique_ptr<Function> curr); Global* addGlobal(Global* curr); Event* addEvent(Event* curr); + Export* addExport(std::unique_ptr<Export> curr); + Function* addFunction(std::unique_ptr<Function> curr); + Global* addGlobal(std::unique_ptr<Global> curr); + Event* addEvent(std::unique_ptr<Event> curr); + void addStart(const Name& s); void removeExport(Name name); diff --git a/src/wasm/wasm.cpp b/src/wasm/wasm.cpp index 783d51e0f..ff1295bad 100644 --- a/src/wasm/wasm.cpp +++ b/src/wasm/wasm.cpp @@ -975,134 +975,120 @@ void Function::clearDebugInfo() { epilogLocation.clear(); } -Export* Module::getExport(Name name) { - auto iter = exportsMap.find(name); - if (iter == exportsMap.end()) { - Fatal() << "Module::getExport: " << name << " does not exist"; +template<typename Map> +typename Map::mapped_type& +getModuleElement(Map& m, Name name, const std::string& funcName) { + auto iter = m.find(name); + if (iter == m.end()) { + Fatal() << "Module::" << funcName << ": " << name << " does not exist"; } return iter->second; } +Export* Module::getExport(Name name) { + return getModuleElement(exportsMap, name, "getExport"); +} + Function* Module::getFunction(Name name) { - auto iter = functionsMap.find(name); - if (iter == functionsMap.end()) { - Fatal() << "Module::getFunction: " << name << " does not exist"; - } - return iter->second; + return getModuleElement(functionsMap, name, "getFunction"); } Global* Module::getGlobal(Name name) { - auto iter = globalsMap.find(name); - if (iter == globalsMap.end()) { - assert(false); - Fatal() << "Module::getGlobal: " << name << " does not exist"; - } - return iter->second; + return getModuleElement(globalsMap, name, "getGlobal"); } Event* Module::getEvent(Name name) { - auto iter = eventsMap.find(name); - if (iter == eventsMap.end()) { - Fatal() << "Module::getEvent: " << name << " does not exist"; - } - return iter->second; + return getModuleElement(eventsMap, name, "getEvent"); } -Export* Module::getExportOrNull(Name name) { - auto iter = exportsMap.find(name); - if (iter == exportsMap.end()) { +template<typename Map> +typename Map::mapped_type getModuleElementOrNull(Map& m, Name name) { + auto iter = m.find(name); + if (iter == m.end()) { return nullptr; } return iter->second; } +Export* Module::getExportOrNull(Name name) { + return getModuleElementOrNull(exportsMap, name); +} + Function* Module::getFunctionOrNull(Name name) { - auto iter = functionsMap.find(name); - if (iter == functionsMap.end()) { - return nullptr; - } - return iter->second; + return getModuleElementOrNull(functionsMap, name); } Global* Module::getGlobalOrNull(Name name) { - auto iter = globalsMap.find(name); - if (iter == globalsMap.end()) { - return nullptr; - } - return iter->second; + return getModuleElementOrNull(globalsMap, name); } Event* Module::getEventOrNull(Name name) { - auto iter = eventsMap.find(name); - if (iter == eventsMap.end()) { - return nullptr; - } - return iter->second; -} - -Export* Module::addExport(Export* curr) { - if (!curr->name.is()) { - Fatal() << "Module::addExport: empty name"; - } - if (getExportOrNull(curr->name)) { - Fatal() << "Module::addExport: " << curr->name << " already exists"; - } - exports.push_back(std::unique_ptr<Export>(curr)); - exportsMap[curr->name] = curr; - return curr; + return getModuleElementOrNull(eventsMap, name); } // TODO(@warchant): refactor all usages to use variant with unique_ptr -Function* Module::addFunction(Function* curr) { +template<typename Vector, typename Map, typename Elem> +Elem* addModuleElement(Vector& v, Map& m, Elem* curr, std::string funcName) { if (!curr->name.is()) { - Fatal() << "Module::addFunction: empty name"; + Fatal() << "Module::" << funcName << ": empty name"; } - if (getFunctionOrNull(curr->name)) { - Fatal() << "Module::addFunction: " << curr->name << " already exists"; + if (getModuleElementOrNull(m, curr->name)) { + Fatal() << "Module::" << funcName << ": " << curr->name + << " already exists"; } - functions.push_back(std::unique_ptr<Function>(curr)); - functionsMap[curr->name] = curr; + v.push_back(std::unique_ptr<Elem>(curr)); + m[curr->name] = curr; return curr; } -Function* Module::addFunction(std::unique_ptr<Function> curr) { +template<typename Vector, typename Map, typename Elem> +Elem* addModuleElement(Vector& v, + Map& m, + std::unique_ptr<Elem> curr, + std::string funcName) { if (!curr->name.is()) { - Fatal() << "Module::addFunction: empty name"; + Fatal() << "Module::" << funcName << ": empty name"; } - if (getFunctionOrNull(curr->name)) { - Fatal() << "Module::addFunction: " << curr->name << " already exists"; + if (getModuleElementOrNull(m, curr->name)) { + Fatal() << "Module::" << funcName << ": " << curr->name + << " already exists"; } - auto* ret = functionsMap[curr->name] = curr.get(); - functions.push_back(std::move(curr)); + auto* ret = m[curr->name] = curr.get(); + v.push_back(std::move(curr)); return ret; } -Global* Module::addGlobal(Global* curr) { - if (!curr->name.is()) { - Fatal() << "Module::addGlobal: empty name"; - } - if (getGlobalOrNull(curr->name)) { - Fatal() << "Module::addGlobal: " << curr->name << " already exists"; - } +Export* Module::addExport(Export* curr) { + return addModuleElement(exports, exportsMap, curr, "addExport"); +} - globals.emplace_back(curr); +Function* Module::addFunction(Function* curr) { + return addModuleElement(functions, functionsMap, curr, "addFunction"); +} - globalsMap[curr->name] = curr; - return curr; +Global* Module::addGlobal(Global* curr) { + return addModuleElement(globals, globalsMap, curr, "addGlobal"); } Event* Module::addEvent(Event* curr) { - if (!curr->name.is()) { - Fatal() << "Module::addEvent: empty name"; - } - if (getEventOrNull(curr->name)) { - Fatal() << "Module::addEvent: " << curr->name << " already exists"; - } + return addModuleElement(events, eventsMap, curr, "addEvent"); +} - events.emplace_back(curr); +Export* Module::addExport(std::unique_ptr<Export> curr) { + return addModuleElement(exports, exportsMap, std::move(curr), "addExport"); +} - eventsMap[curr->name] = curr; - return curr; +Function* Module::addFunction(std::unique_ptr<Function> curr) { + return addModuleElement( + functions, functionsMap, std::move(curr), "addFunction"); +} + +Global* Module::addGlobal(std::unique_ptr<Global> curr) { + return addModuleElement(globals, globalsMap, std::move(curr), "addGlobal"); +} + +Event* Module::addEvent(std::unique_ptr<Event> curr) { + return addModuleElement(events, eventsMap, std::move(curr), "addEvent"); } void Module::addStart(const Name& s) { start = s; } |