summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Lively <tlively@google.com>2024-06-14 14:47:27 -0700
committerGitHub <noreply@github.com>2024-06-14 14:47:27 -0700
commiteacea8060a29acefdd9cb45fdf95d73afa76c656 (patch)
treeacd001c7102a26536f9c1f775add7c899ccf9d88
parent027128dccf76692ca4e000c6dbb489b348b12c73 (diff)
downloadbinaryen-eacea8060a29acefdd9cb45fdf95d73afa76c656.tar.gz
binaryen-eacea8060a29acefdd9cb45fdf95d73afa76c656.tar.bz2
binaryen-eacea8060a29acefdd9cb45fdf95d73afa76c656.zip
[Parser][NFC] Make typeidx and maybeTypeidx return consistent types (#6663)
Since the BasicHeapTypes are in an enum, calling HeapType methods on them requires something like `HeapType(HeapType::func).someMethod()`. This is unnecessarily verbose, so add a new `HeapTypes` namespace that contains constexpr HeapType globals that can be used instead, shorting this to `HeapTypes::func.someMethod()`.
-rw-r--r--src/parser/contexts.h9
-rw-r--r--src/parser/parsers.h16
2 files changed, 12 insertions, 13 deletions
diff --git a/src/parser/contexts.h b/src/parser/contexts.h
index 7c1a07b53..351f6a208 100644
--- a/src/parser/contexts.h
+++ b/src/parser/contexts.h
@@ -908,7 +908,7 @@ struct ParseDeclsCtx : NullTypeParserCtx, NullInstrParserCtx {
void addArrayType(ArrayT) {}
void setOpen() {}
void setShared() {}
- Result<> addSubtype(Index) { return Ok{}; }
+ Result<> addSubtype(HeapTypeT) { return Ok{}; }
void finishSubtype(Name name, Index pos) {
// TODO: type annotations
subtypeDefs.push_back({name, pos, Index(subtypeDefs.size()), {}});
@@ -1080,11 +1080,8 @@ struct ParseTypeDefsCtx : TypeParserCtx<ParseTypeDefsCtx> {
void setShared() { builder[index].setShared(); }
- Result<> addSubtype(Index super) {
- if (super >= builder.size()) {
- return in.err("supertype index out of bounds");
- }
- builder[index].subTypeOf(builder[super]);
+ Result<> addSubtype(HeapTypeT super) {
+ builder[index].subTypeOf(super);
return Ok{};
}
diff --git a/src/parser/parsers.h b/src/parser/parsers.h
index a3fe5e5eb..e56efb529 100644
--- a/src/parser/parsers.h
+++ b/src/parser/parsers.h
@@ -305,7 +305,8 @@ Result<> ignore(Ctx&, Index, const std::vector<Annotation>&) {
}
// Modules
-template<typename Ctx> MaybeResult<Index> maybeTypeidx(Ctx& ctx);
+template<typename Ctx>
+MaybeResult<typename Ctx::HeapTypeT> maybeTypeidx(Ctx& ctx);
template<typename Ctx> Result<typename Ctx::HeapTypeT> typeidx(Ctx&);
template<typename Ctx>
Result<typename Ctx::FieldIdxT> fieldidx(Ctx&, typename Ctx::HeapTypeT);
@@ -2436,23 +2437,24 @@ makeSuspend(Ctx& ctx, Index pos, const std::vector<Annotation>& annotations) {
// typeidx ::= x:u32 => x
// | v:id => x (if types[x] = v)
-template<typename Ctx> MaybeResult<Index> maybeTypeidx(Ctx& ctx) {
+template<typename Ctx>
+MaybeResult<typename Ctx::HeapTypeT> maybeTypeidx(Ctx& ctx) {
if (auto x = ctx.in.takeU32()) {
- return *x;
+ return ctx.getHeapTypeFromIdx(*x);
}
if (auto id = ctx.in.takeID()) {
// TODO: Fix position to point to start of id, not next element.
auto idx = ctx.getTypeIndex(*id);
CHECK_ERR(idx);
- return *idx;
+ return ctx.getHeapTypeFromIdx(*idx);
}
return {};
}
template<typename Ctx> Result<typename Ctx::HeapTypeT> typeidx(Ctx& ctx) {
- if (auto idx = maybeTypeidx(ctx)) {
- CHECK_ERR(idx);
- return ctx.getHeapTypeFromIdx(*idx);
+ if (auto t = maybeTypeidx(ctx)) {
+ CHECK_ERR(t);
+ return *t;
}
return ctx.in.err("expected type index or identifier");
}