Skip to content

Commit 97ce2cc

Browse files
tlivelyradekdoulik
authored andcommitted
[Parser] Parse remaining heap and reference types (WebAssembly#6218)
Parse types like `exnref` and `nofunc` that we did not previously support.
1 parent 3ce9971 commit 97ce2cc

File tree

3 files changed

+81
-22
lines changed

3 files changed

+81
-22
lines changed

src/parser/contexts.h

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,22 @@ struct NullTypeParserCtx {
9797
using ElemListT = Ok;
9898
using DataStringT = Ok;
9999

100-
HeapTypeT makeFunc() { return Ok{}; }
101-
HeapTypeT makeAny() { return Ok{}; }
102-
HeapTypeT makeExtern() { return Ok{}; }
103-
HeapTypeT makeEq() { return Ok{}; }
104-
HeapTypeT makeI31() { return Ok{}; }
100+
HeapTypeT makeFuncType() { return Ok{}; }
101+
HeapTypeT makeAnyType() { return Ok{}; }
102+
HeapTypeT makeExternType() { return Ok{}; }
103+
HeapTypeT makeEqType() { return Ok{}; }
104+
HeapTypeT makeI31Type() { return Ok{}; }
105105
HeapTypeT makeStructType() { return Ok{}; }
106106
HeapTypeT makeArrayType() { return Ok{}; }
107+
HeapTypeT makeExnType() { return Ok{}; }
107108
HeapTypeT makeStringType() { return Ok{}; }
108109
HeapTypeT makeStringViewWTF8Type() { return Ok{}; }
109110
HeapTypeT makeStringViewWTF16Type() { return Ok{}; }
110111
HeapTypeT makeStringViewIterType() { return Ok{}; }
112+
HeapTypeT makeNoneType() { return Ok{}; }
113+
HeapTypeT makeNoextType() { return Ok{}; }
114+
HeapTypeT makeNofuncType() { return Ok{}; }
115+
HeapTypeT makeNoexnType() { return Ok{}; }
111116

112117
TypeT makeI32() { return Ok{}; }
113118
TypeT makeI64() { return Ok{}; }
@@ -187,17 +192,22 @@ template<typename Ctx> struct TypeParserCtx {
187192

188193
Ctx& self() { return *static_cast<Ctx*>(this); }
189194

190-
HeapTypeT makeFunc() { return HeapType::func; }
191-
HeapTypeT makeAny() { return HeapType::any; }
192-
HeapTypeT makeExtern() { return HeapType::ext; }
193-
HeapTypeT makeEq() { return HeapType::eq; }
194-
HeapTypeT makeI31() { return HeapType::i31; }
195+
HeapTypeT makeFuncType() { return HeapType::func; }
196+
HeapTypeT makeAnyType() { return HeapType::any; }
197+
HeapTypeT makeExternType() { return HeapType::ext; }
198+
HeapTypeT makeEqType() { return HeapType::eq; }
199+
HeapTypeT makeI31Type() { return HeapType::i31; }
195200
HeapTypeT makeStructType() { return HeapType::struct_; }
196201
HeapTypeT makeArrayType() { return HeapType::array; }
202+
HeapTypeT makeExnType() { return HeapType::exn; }
197203
HeapTypeT makeStringType() { return HeapType::string; }
198204
HeapTypeT makeStringViewWTF8Type() { return HeapType::stringview_wtf8; }
199205
HeapTypeT makeStringViewWTF16Type() { return HeapType::stringview_wtf16; }
200206
HeapTypeT makeStringViewIterType() { return HeapType::stringview_iter; }
207+
HeapTypeT makeNoneType() { return HeapType::none; }
208+
HeapTypeT makeNoextType() { return HeapType::noext; }
209+
HeapTypeT makeNofuncType() { return HeapType::nofunc; }
210+
HeapTypeT makeNoexnType() { return HeapType::noexn; }
201211

202212
TypeT makeI32() { return Type::i32; }
203213
TypeT makeI64() { return Type::i64; }

src/parser/parsers.h

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -236,26 +236,29 @@ template<typename Ctx> WithPosition(Ctx& ctx, Index) -> WithPosition<Ctx>;
236236
// | 'extern' => extern
237237
template<typename Ctx> Result<typename Ctx::HeapTypeT> heaptype(Ctx& ctx) {
238238
if (ctx.in.takeKeyword("func"sv)) {
239-
return ctx.makeFunc();
239+
return ctx.makeFuncType();
240240
}
241241
if (ctx.in.takeKeyword("any"sv)) {
242-
return ctx.makeAny();
242+
return ctx.makeAnyType();
243243
}
244244
if (ctx.in.takeKeyword("extern"sv)) {
245-
return ctx.makeExtern();
245+
return ctx.makeExternType();
246246
}
247247
if (ctx.in.takeKeyword("eq"sv)) {
248-
return ctx.makeEq();
248+
return ctx.makeEqType();
249249
}
250250
if (ctx.in.takeKeyword("i31"sv)) {
251-
return ctx.makeI31();
251+
return ctx.makeI31Type();
252252
}
253253
if (ctx.in.takeKeyword("struct"sv)) {
254254
return ctx.makeStructType();
255255
}
256256
if (ctx.in.takeKeyword("array"sv)) {
257257
return ctx.makeArrayType();
258258
}
259+
if (ctx.in.takeKeyword("exn"sv)) {
260+
return ctx.makeExnType();
261+
}
259262
if (ctx.in.takeKeyword("string"sv)) {
260263
return ctx.makeStringType();
261264
}
@@ -268,6 +271,18 @@ template<typename Ctx> Result<typename Ctx::HeapTypeT> heaptype(Ctx& ctx) {
268271
if (ctx.in.takeKeyword("stringview_iter"sv)) {
269272
return ctx.makeStringViewIterType();
270273
}
274+
if (ctx.in.takeKeyword("none"sv)) {
275+
return ctx.makeNoneType();
276+
}
277+
if (ctx.in.takeKeyword("noextern"sv)) {
278+
return ctx.makeNoextType();
279+
}
280+
if (ctx.in.takeKeyword("nofunc"sv)) {
281+
return ctx.makeNofuncType();
282+
}
283+
if (ctx.in.takeKeyword("noexn"sv)) {
284+
return ctx.makeNoexnType();
285+
}
271286
auto type = typeidx(ctx);
272287
CHECK_ERR(type);
273288
return *type;
@@ -283,26 +298,29 @@ template<typename Ctx> Result<typename Ctx::HeapTypeT> heaptype(Ctx& ctx) {
283298
// | '(' ref null? t:heaptype ')' => ref null? t
284299
template<typename Ctx> MaybeResult<typename Ctx::TypeT> reftype(Ctx& ctx) {
285300
if (ctx.in.takeKeyword("funcref"sv)) {
286-
return ctx.makeRefType(ctx.makeFunc(), Nullable);
301+
return ctx.makeRefType(ctx.makeFuncType(), Nullable);
287302
}
288303
if (ctx.in.takeKeyword("externref"sv)) {
289-
return ctx.makeRefType(ctx.makeExtern(), Nullable);
304+
return ctx.makeRefType(ctx.makeExternType(), Nullable);
290305
}
291306
if (ctx.in.takeKeyword("anyref"sv)) {
292-
return ctx.makeRefType(ctx.makeAny(), Nullable);
307+
return ctx.makeRefType(ctx.makeAnyType(), Nullable);
293308
}
294309
if (ctx.in.takeKeyword("eqref"sv)) {
295-
return ctx.makeRefType(ctx.makeEq(), Nullable);
310+
return ctx.makeRefType(ctx.makeEqType(), Nullable);
296311
}
297312
if (ctx.in.takeKeyword("i31ref"sv)) {
298-
return ctx.makeRefType(ctx.makeI31(), Nullable);
313+
return ctx.makeRefType(ctx.makeI31Type(), Nullable);
299314
}
300315
if (ctx.in.takeKeyword("structref"sv)) {
301316
return ctx.makeRefType(ctx.makeStructType(), Nullable);
302317
}
303318
if (ctx.in.takeKeyword("arrayref"sv)) {
304319
return ctx.makeRefType(ctx.makeArrayType(), Nullable);
305320
}
321+
if (ctx.in.takeKeyword("exnref"sv)) {
322+
return ctx.makeRefType(ctx.makeExnType(), Nullable);
323+
}
306324
if (ctx.in.takeKeyword("stringref"sv)) {
307325
return ctx.makeRefType(ctx.makeStringType(), Nullable);
308326
}
@@ -315,6 +333,18 @@ template<typename Ctx> MaybeResult<typename Ctx::TypeT> reftype(Ctx& ctx) {
315333
if (ctx.in.takeKeyword("stringview_iter"sv)) {
316334
return ctx.makeRefType(ctx.makeStringViewIterType(), Nullable);
317335
}
336+
if (ctx.in.takeKeyword("nullref"sv)) {
337+
return ctx.makeRefType(ctx.makeNoneType(), Nullable);
338+
}
339+
if (ctx.in.takeKeyword("nullexternref"sv)) {
340+
return ctx.makeRefType(ctx.makeNoextType(), Nullable);
341+
}
342+
if (ctx.in.takeKeyword("nullfuncref"sv)) {
343+
return ctx.makeRefType(ctx.makeNofuncType(), Nullable);
344+
}
345+
if (ctx.in.takeKeyword("nullexnref"sv)) {
346+
return ctx.makeRefType(ctx.makeNoexnType(), Nullable);
347+
}
318348

319349
if (!ctx.in.takeSExprStart("ref"sv)) {
320350
return {};

test/lit/wat-kitchen-sink.wast

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,27 @@
214214
;; CHECK: (type $submany (sub final $many (func (param i32 i64 f32 f64) (result anyref (ref func)))))
215215
(type $submany (sub final $many (func (param i32 i64 f32 f64) (result anyref (ref func)))))
216216

217+
;; CHECK: (type $all-types (struct (field externref) (field (ref extern)) (field funcref) (field (ref func)) (field anyref) (field (ref any)) (field eqref) (field (ref eq)) (field i31ref) (field (ref i31)) (field structref) (field (ref struct)) (field arrayref) (field (ref array)) (field exnref) (field (ref exn)) (field stringref) (field (ref string)) (field stringview_wtf8) (field (ref stringview_wtf8)) (field stringview_wtf16) (field (ref stringview_wtf16)) (field stringview_iter) (field (ref stringview_iter)) (field nullref) (field (ref none)) (field nullexternref) (field (ref noextern)) (field nullfuncref) (field (ref nofunc)) (field nullexnref) (field (ref noexn))))
218+
(type $all-types (struct externref (ref extern)
219+
funcref (ref func)
220+
anyref (ref any)
221+
eqref (ref eq)
222+
i31ref (ref i31)
223+
structref (ref struct)
224+
arrayref (ref array)
225+
exnref (ref exn)
226+
stringref (ref string)
227+
stringview_wtf8 (ref stringview_wtf8)
228+
stringview_wtf16 (ref stringview_wtf16)
229+
stringview_iter (ref stringview_iter)
230+
nullref (ref none)
231+
nullexternref (ref noextern)
232+
nullfuncref (ref nofunc)
233+
nullexnref (ref noexn)))
234+
217235
;; imported memories
218236
(memory (export "mem") (export "mem2") (import "" "mem") 0)
219-
;; CHECK: (type $90 (func (param (ref $s0) (ref $s1) (ref $s2) (ref $s3) (ref $s4) (ref $s5) (ref $s6) (ref $s7) (ref $s8) (ref $a0) (ref $a1) (ref $a2) (ref $a3) (ref $subvoid) (ref $submany))))
237+
;; CHECK: (type $91 (func (param (ref $s0) (ref $s1) (ref $s2) (ref $s3) (ref $s4) (ref $s5) (ref $s6) (ref $s7) (ref $s8) (ref $a0) (ref $a1) (ref $a2) (ref $a3) (ref $subvoid) (ref $submany) (ref $all-types))))
220238

221239
;; CHECK: (import "" "mem" (memory $mimport$0 0))
222240

@@ -4724,7 +4742,7 @@
47244742
)
47254743
)
47264744

4727-
;; CHECK: (func $use-types (type $90) (param $0 (ref $s0)) (param $1 (ref $s1)) (param $2 (ref $s2)) (param $3 (ref $s3)) (param $4 (ref $s4)) (param $5 (ref $s5)) (param $6 (ref $s6)) (param $7 (ref $s7)) (param $8 (ref $s8)) (param $9 (ref $a0)) (param $10 (ref $a1)) (param $11 (ref $a2)) (param $12 (ref $a3)) (param $13 (ref $subvoid)) (param $14 (ref $submany))
4745+
;; CHECK: (func $use-types (type $91) (param $0 (ref $s0)) (param $1 (ref $s1)) (param $2 (ref $s2)) (param $3 (ref $s3)) (param $4 (ref $s4)) (param $5 (ref $s5)) (param $6 (ref $s6)) (param $7 (ref $s7)) (param $8 (ref $s8)) (param $9 (ref $a0)) (param $10 (ref $a1)) (param $11 (ref $a2)) (param $12 (ref $a3)) (param $13 (ref $subvoid)) (param $14 (ref $submany)) (param $15 (ref $all-types))
47284746
;; CHECK-NEXT: (nop)
47294747
;; CHECK-NEXT: )
47304748
(func $use-types
@@ -4743,5 +4761,6 @@
47434761
(param (ref $a3))
47444762
(param (ref $subvoid))
47454763
(param (ref $submany))
4764+
(param (ref $all-types))
47464765
)
47474766
)

0 commit comments

Comments
 (0)