diff --git a/lld/wasm/Symbols.h b/lld/wasm/Symbols.h --- a/lld/wasm/Symbols.h +++ b/lld/wasm/Symbols.h @@ -172,6 +172,9 @@ bool isStub : 1; uint32_t flags; + + llvm::Optional importName; + llvm::Optional importModule; }; class FunctionSymbol : public Symbol { @@ -222,15 +225,15 @@ const WasmSignature *type = nullptr, bool isCalledDirectly = true) : FunctionSymbol(name, UndefinedFunctionKind, flags, file, type), - importName(importName), importModule(importModule), - isCalledDirectly(isCalledDirectly) {} + isCalledDirectly(isCalledDirectly) { + this->importName = importName; + this->importModule = importModule; + } static bool classof(const Symbol *s) { return s->kind() == UndefinedFunctionKind; } - llvm::Optional importName; - llvm::Optional importModule; DefinedFunction *stubFunction = nullptr; bool isCalledDirectly; }; @@ -354,15 +357,14 @@ llvm::Optional importModule, uint32_t flags, InputFile *file = nullptr, const WasmGlobalType *type = nullptr) - : GlobalSymbol(name, UndefinedGlobalKind, flags, file, type), - importName(importName), importModule(importModule) {} + : GlobalSymbol(name, UndefinedGlobalKind, flags, file, type) { + this->importName = importName; + this->importModule = importModule; + } static bool classof(const Symbol *s) { return s->kind() == UndefinedGlobalKind; } - - llvm::Optional importName; - llvm::Optional importModule; }; class TableSymbol : public Symbol { @@ -403,15 +405,14 @@ UndefinedTable(StringRef name, llvm::Optional importName, llvm::Optional importModule, uint32_t flags, InputFile *file, const WasmTableType *type) - : TableSymbol(name, UndefinedTableKind, flags, file, type), - importName(importName), importModule(importModule) {} + : TableSymbol(name, UndefinedTableKind, flags, file, type) { + this->importName = importName; + this->importModule = importModule; + } static bool classof(const Symbol *s) { return s->kind() == UndefinedTableKind; } - - llvm::Optional importName; - llvm::Optional importModule; }; // A tag is a general format to distinguish typed entities. Each tag has an diff --git a/lld/wasm/SyntheticSections.cpp b/lld/wasm/SyntheticSections.cpp --- a/lld/wasm/SyntheticSections.cpp +++ b/lld/wasm/SyntheticSections.cpp @@ -107,24 +107,11 @@ gotSymbols.push_back(sym); } -template -static void getImportModuleAndName(Symbol *sym, StringRef *outModule, - StringRef *outName) { - if (auto *undef = dyn_cast(sym)) { - *outModule = undef->importModule ? *undef->importModule : defaultModule; - *outName = undef->importName ? *undef->importName : sym->getName(); - } else { - *outModule = defaultModule; - *outName = sym->getName(); - } -} - void ImportSection::addImport(Symbol *sym) { assert(!isSealed); - StringRef module; - StringRef name; + StringRef module = sym->importModule.getValueOr(defaultModule); + StringRef name = sym->importName.getValueOr(sym->getName()); if (auto *f = dyn_cast(sym)) { - getImportModuleAndName(sym, &module, &name); ImportKey key(*(f->getSignature()), module, name); auto entry = importedFunctions.try_emplace(key, numImportedFunctions); if (entry.second) { @@ -134,7 +121,6 @@ f->setFunctionIndex(entry.first->second); } } else if (auto *g = dyn_cast(sym)) { - getImportModuleAndName(sym, &module, &name); ImportKey key(*(g->getGlobalType()), module, name); auto entry = importedGlobals.try_emplace(key, numImportedGlobals); if (entry.second) { @@ -150,7 +136,6 @@ t->setTagIndex(numImportedTags++); } else { auto *table = cast(sym); - getImportModuleAndName(sym, &module, &name); ImportKey key(*(table->getTableType()), module, name); auto entry = importedTables.try_emplace(key, numImportedTables); if (entry.second) { @@ -189,19 +174,8 @@ for (const Symbol *sym : importedSymbols) { WasmImport import; - if (auto *f = dyn_cast(sym)) { - import.Field = f->importName ? *f->importName : sym->getName(); - import.Module = f->importModule ? *f->importModule : defaultModule; - } else if (auto *g = dyn_cast(sym)) { - import.Field = g->importName ? *g->importName : sym->getName(); - import.Module = g->importModule ? *g->importModule : defaultModule; - } else if (auto *t = dyn_cast(sym)) { - import.Field = t->importName ? *t->importName : sym->getName(); - import.Module = t->importModule ? *t->importModule : defaultModule; - } else { - import.Field = sym->getName(); - import.Module = defaultModule; - } + import.Field = sym->importName.getValueOr(sym->getName()); + import.Module = sym->importModule.getValueOr(defaultModule); if (auto *functionSym = dyn_cast(sym)) { import.Kind = WASM_EXTERNAL_FUNCTION; diff --git a/lld/wasm/Writer.cpp b/lld/wasm/Writer.cpp --- a/lld/wasm/Writer.cpp +++ b/lld/wasm/Writer.cpp @@ -562,12 +562,8 @@ return true; if (config->allowUndefinedSymbols.count(sym->getName()) != 0) return true; - if (auto *g = dyn_cast(sym)) - return g->importName.hasValue(); - if (auto *f = dyn_cast(sym)) - return f->importName.hasValue(); - if (auto *t = dyn_cast(sym)) - return t->importName.hasValue(); + if (sym->isUndefined()) + return sym->importName.hasValue(); return false; }