From 90087f0972aa34ee37860b70072cb8fc33c89ce3 Mon Sep 17 00:00:00 2001 From: Thomas Lively Date: Thu, 8 Feb 2024 16:19:40 -0800 Subject: [Parser] Support references to struct fields by name (#6293) Construct a mapping from heap type and field name to field index, then use it while parsing instructions. --- src/parser/wat-parser.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'src/parser/wat-parser.cpp') diff --git a/src/parser/wat-parser.cpp b/src/parser/wat-parser.cpp index 50a47d7b6..33066ea71 100644 --- a/src/parser/wat-parser.cpp +++ b/src/parser/wat-parser.cpp @@ -110,6 +110,7 @@ Result<> parseModule(Module& wasm, std::string_view input) { // Parse type definitions. std::vector types; + std::unordered_map> typeNames; { TypeBuilder builder(decls.subtypeDefs.size()); ParseTypeDefsCtx ctx(input, builder, *typeIndices); @@ -124,11 +125,16 @@ Result<> parseModule(Module& wasm, std::string_view input) { return ctx.in.err(decls.typeDefs[err->index].pos, msg.str()); } types = *built; - // Record type names on the module. + // Record type names on the module and in typeNames. for (size_t i = 0; i < types.size(); ++i) { auto& names = ctx.names[i]; - if (names.name.is() || names.fieldNames.size()) { + auto& fieldNames = names.fieldNames; + if (names.name.is() || fieldNames.size()) { wasm.typeNames.insert({types[i], names}); + auto& fieldIdxMap = typeNames[types[i]]; + for (auto [idx, name] : fieldNames) { + fieldIdxMap.insert({name, idx}); + } } } } @@ -167,6 +173,7 @@ Result<> parseModule(Module& wasm, std::string_view input) { wasm, types, implicitTypes, + typeNames, decls.implicitElemIndices, *typeIndices); CHECK_ERR(parseDefs(ctx, decls.tableDefs, table)); -- cgit v1.2.3