llvm-project/clang/test/SemaCXX/wasm-refs-and-tables.cpp
Paulo Matos 55aeb23fe0 [clang][WebAssembly] Implement support for table types and builtins
This commit implements support for WebAssembly table types and
respective builtins. Table tables are WebAssembly objects to store
reference types. They have a large amount of semantic restrictions
including, but not limited to, only being allowed to be declared
at the top-level as static arrays of zero-length. Not being arguments
or result of functions, not being stored ot memory, etc.

This commit introduces the __attribute__((wasm_table)) to attach to
arrays of WebAssembly reference types. And the following builtins to
manage tables:

* ref   __builtin_wasm_table_get(table, idx)
* void  __builtin_wasm_table_set(table, idx, ref)
* uint  __builtin_wasm_table_size(table)
* uint  __builtin_wasm_table_grow(table, ref, uint)
* void  __builtin_wasm_table_fill(table, idx, ref, uint)
* void  __builtin_wasm_table_copy(table, table, uint, uint, uint)

This commit also enables reference-types feature at bleeding-edge.

This is joint work with Alex Bradbury (@asb).

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D139010
2023-06-10 15:53:13 +02:00

31 lines
1.2 KiB
C++

// RUN: %clang_cc1 -std=c++11 -fcxx-exceptions -fexceptions -fsyntax-only -verify -triple wasm32 -Wno-unused-value -target-feature +reference-types %s
// RUN: %clang_cc1 -std=c++20 -fcxx-exceptions -fexceptions -fsyntax-only -verify -triple wasm32 -Wno-unused-value -target-feature +reference-types %s
//
// Note: As WebAssembly references are sizeless types, we don't exhaustively
// test for cases covered by sizeless-1.c and similar tests.
// Using c++11 to test dynamic exception specifications (which are not
// allowed in c++17).
// Unlike standard sizeless types, reftype globals are supported.
__externref_t r1;
static __externref_t table[0];
#if (_cplusplus == 201103L)
__externref_t func(__externref_t ref) throw(__externref_t) { // expected-error {{WebAssembly reference type not allowed in exception specification}}
return ref;
}
#endif
void *ret_void_ptr() {
throw table; // expected-error {{cannot throw a WebAssembly reference type}}
throw r1; // expected-error {{cannot throw a WebAssembly reference type}}
try {}
catch (__externref_t T) { // expected-error {{cannot catch a WebAssembly reference type}}
(void)0;
}
return table; // expected-error {{cannot return a WebAssembly table}}
}