diff options
Diffstat (limited to 'src/parser/context-decls.cpp')
-rw-r--r-- | src/parser/context-decls.cpp | 75 |
1 files changed, 74 insertions, 1 deletions
diff --git a/src/parser/context-decls.cpp b/src/parser/context-decls.cpp index 721127614..7d2d8a0a9 100644 --- a/src/parser/context-decls.cpp +++ b/src/parser/context-decls.cpp @@ -77,6 +77,59 @@ Result<> ParseDeclsCtx::addFunc(Name name, return Ok{}; } +Result<Table*> ParseDeclsCtx::addTableDecl(Index pos, + Name name, + ImportNames* importNames, + Limits limits) { + auto t = std::make_unique<Table>(); + t->initial = limits.initial; + t->max = limits.max ? *limits.max : Table::kUnlimitedSize; + if (name.is()) { + if (wasm.getTableOrNull(name)) { + // TODO: if the existing table is not explicitly named, fix its name and + // continue. + return in.err(pos, "repeated table name"); + } + t->setExplicitName(name); + } else { + name = (importNames ? "timport$" : "") + std::to_string(tableCounter++); + name = Names::getValidTableName(wasm, name); + t->name = name; + } + applyImportNames(*t, importNames); + return wasm.addTable(std::move(t)); +} + +Result<> ParseDeclsCtx::addTable(Name name, + const std::vector<Name>& exports, + ImportNames* import, + Limits limits, + Index pos) { + CHECK_ERR(checkImport(pos, import)); + auto t = addTableDecl(pos, name, import, limits); + CHECK_ERR(t); + CHECK_ERR(addExports(in, wasm, *t, exports, ExternalKind::Table)); + tableDefs.push_back({name, pos, Index(tableDefs.size())}); + return Ok{}; +} + +Result<> ParseDeclsCtx::addImplicitElems(TypeT, ElemListT&& elems) { + auto& table = *wasm.tables.back(); + auto e = std::make_unique<ElementSegment>(); + e->table = table.name; + e->offset = Builder(wasm).makeConstPtr(0, Type::i32); + e->name = Names::getValidElementSegmentName(wasm, "implicit-elem"); + wasm.addElementSegment(std::move(e)); + + // Record the index mapping so we can find this segment again to set its type + // and elements in later phases. + Index tableIndex = wasm.tables.size() - 1; + Index elemIndex = wasm.elementSegments.size() - 1; + implicitElemIndices[tableIndex] = elemIndex; + + return Ok{}; +} + Result<Memory*> ParseDeclsCtx::addMemoryDecl(Index pos, Name name, ImportNames* importNames, @@ -84,7 +137,7 @@ Result<Memory*> ParseDeclsCtx::addMemoryDecl(Index pos, auto m = std::make_unique<Memory>(); m->indexType = type.type; m->initial = type.limits.initial; - m->max = type.limits.max; + m->max = type.limits.max ? *type.limits.max : Memory::kUnlimitedSize; m->shared = type.shared; if (name) { // TODO: if the existing memory is not explicitly named, fix its name @@ -160,6 +213,26 @@ Result<> ParseDeclsCtx::addGlobal(Name name, return Ok{}; } +Result<> ParseDeclsCtx::addElem( + Name name, TableIdxT*, std::optional<ExprT>, ElemListT&&, Index pos) { + auto e = std::make_unique<ElementSegment>(); + if (name) { + if (wasm.getElementSegmentOrNull(name)) { + // TDOO: if the existing segment is not explicitly named, fix its name and + // continue. + return in.err(pos, "repeated element segment name"); + } + e->setExplicitName(name); + } else { + name = std::to_string(elemCounter++); + name = Names::getValidElementSegmentName(wasm, name); + e->name = name; + } + elemDefs.push_back({name, pos, Index(wasm.elementSegments.size())}); + wasm.addElementSegment(std::move(e)); + return Ok{}; +} + Result<> ParseDeclsCtx::addData(Name name, MemoryIdxT*, std::optional<ExprT>, |