Skip to content

Commit 90087f0

Browse files
authored
[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.
1 parent f5d8d30 commit 90087f0

File tree

3 files changed

+142
-99
lines changed

3 files changed

+142
-99
lines changed

src/parser/contexts.h

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,8 @@ struct ParseDefsCtx : TypeParserCtx<ParseDefsCtx> {
10961096

10971097
const std::vector<HeapType>& types;
10981098
const std::unordered_map<Index, HeapType>& implicitTypes;
1099+
const std::unordered_map<HeapType, std::unordered_map<Name, Index>>&
1100+
typeNames;
10991101
const std::unordered_map<Index, Index>& implicitElemIndices;
11001102

11011103
// The index of the current module element.
@@ -1113,14 +1115,17 @@ struct ParseDefsCtx : TypeParserCtx<ParseDefsCtx> {
11131115
return Ok{};
11141116
}
11151117

1116-
ParseDefsCtx(std::string_view in,
1117-
Module& wasm,
1118-
const std::vector<HeapType>& types,
1119-
const std::unordered_map<Index, HeapType>& implicitTypes,
1120-
const std::unordered_map<Index, Index>& implicitElemIndices,
1121-
const IndexMap& typeIndices)
1118+
ParseDefsCtx(
1119+
std::string_view in,
1120+
Module& wasm,
1121+
const std::vector<HeapType>& types,
1122+
const std::unordered_map<Index, HeapType>& implicitTypes,
1123+
const std::unordered_map<HeapType, std::unordered_map<Name, Index>>&
1124+
typeNames,
1125+
const std::unordered_map<Index, Index>& implicitElemIndices,
1126+
const IndexMap& typeIndices)
11221127
: TypeParserCtx(typeIndices), in(in), wasm(wasm), builder(wasm),
1123-
types(types), implicitTypes(implicitTypes),
1128+
types(types), implicitTypes(implicitTypes), typeNames(typeNames),
11241129
implicitElemIndices(implicitElemIndices), irBuilder(wasm) {}
11251130

11261131
template<typename T> Result<T> withLoc(Index pos, Result<T> res) {
@@ -1192,8 +1197,13 @@ struct ParseDefsCtx : TypeParserCtx<ParseDefsCtx> {
11921197
}
11931198

11941199
Result<Index> getFieldFromName(HeapType type, Name name) {
1195-
// TODO: Field names
1196-
return in.err("symbolic field names note yet supported");
1200+
if (auto typeIt = typeNames.find(type); typeIt != typeNames.end()) {
1201+
const auto& fieldIdxs = typeIt->second;
1202+
if (auto fieldIt = fieldIdxs.find(name); fieldIt != fieldIdxs.end()) {
1203+
return fieldIt->second;
1204+
}
1205+
}
1206+
return in.err("unrecognized field name");
11971207
}
11981208

11991209
Result<Index> getLocalFromIdx(uint32_t idx) {

src/parser/wat-parser.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ Result<> parseModule(Module& wasm, std::string_view input) {
110110

111111
// Parse type definitions.
112112
std::vector<HeapType> types;
113+
std::unordered_map<HeapType, std::unordered_map<Name, Index>> typeNames;
113114
{
114115
TypeBuilder builder(decls.subtypeDefs.size());
115116
ParseTypeDefsCtx ctx(input, builder, *typeIndices);
@@ -124,11 +125,16 @@ Result<> parseModule(Module& wasm, std::string_view input) {
124125
return ctx.in.err(decls.typeDefs[err->index].pos, msg.str());
125126
}
126127
types = *built;
127-
// Record type names on the module.
128+
// Record type names on the module and in typeNames.
128129
for (size_t i = 0; i < types.size(); ++i) {
129130
auto& names = ctx.names[i];
130-
if (names.name.is() || names.fieldNames.size()) {
131+
auto& fieldNames = names.fieldNames;
132+
if (names.name.is() || fieldNames.size()) {
131133
wasm.typeNames.insert({types[i], names});
134+
auto& fieldIdxMap = typeNames[types[i]];
135+
for (auto [idx, name] : fieldNames) {
136+
fieldIdxMap.insert({name, idx});
137+
}
132138
}
133139
}
134140
}
@@ -167,6 +173,7 @@ Result<> parseModule(Module& wasm, std::string_view input) {
167173
wasm,
168174
types,
169175
implicitTypes,
176+
typeNames,
170177
decls.implicitElemIndices,
171178
*typeIndices);
172179
CHECK_ERR(parseDefs(ctx, decls.tableDefs, table));

0 commit comments

Comments
 (0)