diff options
author | Thomas Lively <tlively@google.com> | 2023-11-21 08:50:53 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-20 23:50:53 -0800 |
commit | cccc7a6a66b00ab79626afe02f259aa5290d479c (patch) | |
tree | 1f03896877a1d59ee9c7ae6c25194382f767e4c6 /src/parser/context-decls.cpp | |
parent | beb816be810caa0b32ab37986e7cae6f6cf11b1b (diff) | |
download | binaryen-cccc7a6a66b00ab79626afe02f259aa5290d479c.tar.gz binaryen-cccc7a6a66b00ab79626afe02f259aa5290d479c.tar.bz2 binaryen-cccc7a6a66b00ab79626afe02f259aa5290d479c.zip |
[Parser] Parse tags and throw (#6126)
Also fix the parser to correctly error if an imported item appears after a
non-imported item and make the corresponding fix to the test.
Diffstat (limited to 'src/parser/context-decls.cpp')
-rw-r--r-- | src/parser/context-decls.cpp | 44 |
1 files changed, 35 insertions, 9 deletions
diff --git a/src/parser/context-decls.cpp b/src/parser/context-decls.cpp index 17b389488..721127614 100644 --- a/src/parser/context-decls.cpp +++ b/src/parser/context-decls.cpp @@ -69,9 +69,7 @@ Result<> ParseDeclsCtx::addFunc(Name name, TypeUseT type, std::optional<LocalsT>, Index pos) { - if (import && hasNonImport) { - return in.err(pos, "import after non-import"); - } + CHECK_ERR(checkImport(pos, import)); auto f = addFuncDecl(pos, name, import); CHECK_ERR(f); CHECK_ERR(addExports(in, wasm, *f, exports, ExternalKind::Function)); @@ -109,9 +107,7 @@ Result<> ParseDeclsCtx::addMemory(Name name, ImportNames* import, MemType type, Index pos) { - if (import && hasNonImport) { - return in.err(pos, "import after non-import"); - } + CHECK_ERR(checkImport(pos, import)); auto m = addMemoryDecl(pos, name, import, type); CHECK_ERR(m); CHECK_ERR(addExports(in, wasm, *m, exports, ExternalKind::Memory)); @@ -156,9 +152,7 @@ Result<> ParseDeclsCtx::addGlobal(Name name, GlobalTypeT, std::optional<ExprT>, Index pos) { - if (import && hasNonImport) { - return in.err(pos, "import after non-import"); - } + CHECK_ERR(checkImport(pos, import)); auto g = addGlobalDecl(pos, name, import); CHECK_ERR(g); CHECK_ERR(addExports(in, wasm, *g, exports, ExternalKind::Global)); @@ -190,4 +184,36 @@ Result<> ParseDeclsCtx::addData(Name name, return Ok{}; } +Result<Tag*> +ParseDeclsCtx::addTagDecl(Index pos, Name name, ImportNames* importNames) { + auto t = std::make_unique<Tag>(); + if (name) { + if (wasm.getTagOrNull(name)) { + // TODO: if the existing tag is not explicitly named, fix its name and + // continue. + return in.err(pos, "repeated tag name"); + } + t->setExplicitName(name); + } else { + name = (importNames ? "timport$" : "") + std::to_string(tagCounter++); + name = Names::getValidTagName(wasm, name); + t->name = name; + } + applyImportNames(*t, importNames); + return wasm.addTag(std::move(t)); +} + +Result<> ParseDeclsCtx::addTag(Name name, + const std::vector<Name>& exports, + ImportNames* import, + TypeUseT type, + Index pos) { + CHECK_ERR(checkImport(pos, import)); + auto t = addTagDecl(pos, name, import); + CHECK_ERR(t); + CHECK_ERR(addExports(in, wasm, *t, exports, ExternalKind::Tag)); + tagDefs.push_back({name, pos, Index(tagDefs.size())}); + return Ok{}; +} + } // namespace wasm::WATParser |