Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Parser] Support references to struct fields by name #6293

Merged
merged 1 commit into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions src/parser/contexts.h
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,8 @@ struct ParseDefsCtx : TypeParserCtx<ParseDefsCtx> {

const std::vector<HeapType>& types;
const std::unordered_map<Index, HeapType>& implicitTypes;
const std::unordered_map<HeapType, std::unordered_map<Name, Index>>&
typeNames;
const std::unordered_map<Index, Index>& implicitElemIndices;

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

ParseDefsCtx(std::string_view in,
Module& wasm,
const std::vector<HeapType>& types,
const std::unordered_map<Index, HeapType>& implicitTypes,
const std::unordered_map<Index, Index>& implicitElemIndices,
const IndexMap& typeIndices)
ParseDefsCtx(
std::string_view in,
Module& wasm,
const std::vector<HeapType>& types,
const std::unordered_map<Index, HeapType>& implicitTypes,
const std::unordered_map<HeapType, std::unordered_map<Name, Index>>&
typeNames,
const std::unordered_map<Index, Index>& implicitElemIndices,
const IndexMap& typeIndices)
: TypeParserCtx(typeIndices), in(in), wasm(wasm), builder(wasm),
types(types), implicitTypes(implicitTypes),
types(types), implicitTypes(implicitTypes), typeNames(typeNames),
implicitElemIndices(implicitElemIndices), irBuilder(wasm) {}

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

Result<Index> getFieldFromName(HeapType type, Name name) {
// TODO: Field names
return in.err("symbolic field names note yet supported");
if (auto typeIt = typeNames.find(type); typeIt != typeNames.end()) {
const auto& fieldIdxs = typeIt->second;
if (auto fieldIt = fieldIdxs.find(name); fieldIt != fieldIdxs.end()) {
return fieldIt->second;
}
}
return in.err("unrecognized field name");
}

Result<Index> getLocalFromIdx(uint32_t idx) {
Expand Down
11 changes: 9 additions & 2 deletions src/parser/wat-parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ Result<> parseModule(Module& wasm, std::string_view input) {

// Parse type definitions.
std::vector<HeapType> types;
std::unordered_map<HeapType, std::unordered_map<Name, Index>> typeNames;
{
TypeBuilder builder(decls.subtypeDefs.size());
ParseTypeDefsCtx ctx(input, builder, *typeIndices);
Expand All @@ -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});
}
}
}
}
Expand Down Expand Up @@ -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));
Expand Down
Loading
Loading