|
| 1 | +/* |
| 2 | + * Copyright 2024 WebAssembly Community Group participants |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "wat-parser-internal.h" |
| 18 | + |
| 19 | +namespace wasm::WATParser { |
| 20 | + |
| 21 | +Result<> parseDefinitions( |
| 22 | + ParseDeclsCtx& decls, |
| 23 | + Lexer& input, |
| 24 | + IndexMap& typeIndices, |
| 25 | + std::vector<HeapType>& types, |
| 26 | + std::unordered_map<Index, HeapType>& implicitTypes, |
| 27 | + std::unordered_map<HeapType, std::unordered_map<Name, Index>>& typeNames) { |
| 28 | + // Parse definitions. |
| 29 | + // TODO: Parallelize this. |
| 30 | + ParseDefsCtx ctx(input, |
| 31 | + decls.wasm, |
| 32 | + types, |
| 33 | + implicitTypes, |
| 34 | + typeNames, |
| 35 | + decls.implicitElemIndices, |
| 36 | + typeIndices); |
| 37 | + CHECK_ERR(parseDefs(ctx, decls.tableDefs, table)); |
| 38 | + CHECK_ERR(parseDefs(ctx, decls.globalDefs, global)); |
| 39 | + CHECK_ERR(parseDefs(ctx, decls.startDefs, start)); |
| 40 | + CHECK_ERR(parseDefs(ctx, decls.elemDefs, elem)); |
| 41 | + CHECK_ERR(parseDefs(ctx, decls.dataDefs, data)); |
| 42 | + |
| 43 | + for (Index i = 0; i < decls.funcDefs.size(); ++i) { |
| 44 | + ctx.index = i; |
| 45 | + auto* f = decls.wasm.functions[i].get(); |
| 46 | + WithPosition with(ctx, decls.funcDefs[i].pos); |
| 47 | + ctx.setSrcLoc(decls.funcDefs[i].annotations); |
| 48 | + if (!f->imported()) { |
| 49 | + CHECK_ERR(ctx.visitFunctionStart(f)); |
| 50 | + } |
| 51 | + if (auto parsed = func(ctx)) { |
| 52 | + CHECK_ERR(parsed); |
| 53 | + } else { |
| 54 | + auto im = import_(ctx); |
| 55 | + assert(im); |
| 56 | + CHECK_ERR(im); |
| 57 | + } |
| 58 | + if (!f->imported()) { |
| 59 | + auto end = ctx.irBuilder.visitEnd(); |
| 60 | + if (auto* err = end.getErr()) { |
| 61 | + return ctx.in.err(decls.funcDefs[i].pos, err->msg); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // Parse exports. |
| 67 | + // TODO: It would be more technically correct to interleave these properly |
| 68 | + // with the implicit inline exports in other module field definitions. |
| 69 | + for (auto pos : decls.exportDefs) { |
| 70 | + WithPosition with(ctx, pos); |
| 71 | + auto parsed = export_(ctx); |
| 72 | + CHECK_ERR(parsed); |
| 73 | + assert(parsed); |
| 74 | + } |
| 75 | + return Ok{}; |
| 76 | +} |
| 77 | + |
| 78 | +Result<Literal> parseConst(Lexer& lexer) { |
| 79 | + Module wasm; |
| 80 | + ParseDefsCtx ctx(lexer, wasm, {}, {}, {}, {}, {}); |
| 81 | + auto inst = foldedinstr(ctx); |
| 82 | + CHECK_ERR(inst); |
| 83 | + auto expr = ctx.irBuilder.build(); |
| 84 | + if (auto* err = expr.getErr()) { |
| 85 | + return lexer.err(err->msg); |
| 86 | + } |
| 87 | + auto* e = *expr; |
| 88 | + if (!e->is<Const>() && !e->is<RefNull>() && !e->is<RefI31>()) { |
| 89 | + return lexer.err("expected constant"); |
| 90 | + } |
| 91 | + lexer = ctx.in; |
| 92 | + return getLiteralFromConstExpression(e); |
| 93 | +} |
| 94 | + |
| 95 | +} // namespace wasm::WATParser |
0 commit comments