dawn-cmake/src/reader/wgsl/parser_impl_function_decl_test.cc
Ben Clayton 0573714bfd wsgl parser: Have Expect & Maybe operator-> deref
For values of type T* and std::unique_ptr<T>.

This allows us to replace all occurances of `res.value->member` with: `res->member`, which also asserts that `res` is not in an error state.

Brings the verbosity back down to pre-expect and pre-maybe levels.

Bug: tint:282
Change-Id: Ib00018affca53ac5e71ee2140e7e0cd607b83715
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/32141
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
2020-11-09 20:44:34 +00:00

226 lines
6.8 KiB
C++

// Copyright 2020 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "gtest/gtest.h"
#include "src/ast/function.h"
#include "src/ast/type/type.h"
#include "src/ast/workgroup_decoration.h"
#include "src/reader/wgsl/parser_impl.h"
#include "src/reader/wgsl/parser_impl_test_helper.h"
namespace tint {
namespace reader {
namespace wgsl {
namespace {
TEST_F(ParserImplTest, FunctionDecl) {
auto* p = parser("fn main(a : i32, b : f32) -> void { return; }");
auto decos = p->decoration_list();
EXPECT_FALSE(p->has_error()) << p->error();
ASSERT_FALSE(decos.errored);
EXPECT_FALSE(decos.matched);
auto f = p->function_decl(decos.value);
EXPECT_FALSE(p->has_error()) << p->error();
EXPECT_FALSE(f.errored);
EXPECT_TRUE(f.matched);
ASSERT_NE(f.value, nullptr);
EXPECT_EQ(f->name(), "main");
ASSERT_NE(f->return_type(), nullptr);
EXPECT_TRUE(f->return_type()->IsVoid());
ASSERT_EQ(f->params().size(), 2u);
EXPECT_EQ(f->params()[0]->name(), "a");
EXPECT_EQ(f->params()[1]->name(), "b");
ASSERT_NE(f->return_type(), nullptr);
EXPECT_TRUE(f->return_type()->IsVoid());
auto* body = f->body();
ASSERT_EQ(body->size(), 1u);
EXPECT_TRUE(body->get(0)->IsReturn());
}
TEST_F(ParserImplTest, FunctionDecl_DecorationList) {
auto* p = parser("[[workgroup_size(2, 3, 4)]] fn main() -> void { return; }");
auto decos = p->decoration_list();
EXPECT_FALSE(p->has_error()) << p->error();
ASSERT_FALSE(decos.errored);
ASSERT_TRUE(decos.matched);
auto f = p->function_decl(decos.value);
EXPECT_FALSE(p->has_error()) << p->error();
EXPECT_FALSE(f.errored);
EXPECT_TRUE(f.matched);
ASSERT_NE(f.value, nullptr);
EXPECT_EQ(f->name(), "main");
ASSERT_NE(f->return_type(), nullptr);
EXPECT_TRUE(f->return_type()->IsVoid());
ASSERT_EQ(f->params().size(), 0u);
ASSERT_NE(f->return_type(), nullptr);
EXPECT_TRUE(f->return_type()->IsVoid());
auto& decorations = f->decorations();
ASSERT_EQ(decorations.size(), 1u);
ASSERT_TRUE(decorations[0]->IsWorkgroup());
uint32_t x = 0;
uint32_t y = 0;
uint32_t z = 0;
std::tie(x, y, z) = decorations[0]->AsWorkgroup()->values();
EXPECT_EQ(x, 2u);
EXPECT_EQ(y, 3u);
EXPECT_EQ(z, 4u);
auto* body = f->body();
ASSERT_EQ(body->size(), 1u);
EXPECT_TRUE(body->get(0)->IsReturn());
}
TEST_F(ParserImplTest, FunctionDecl_DecorationList_MultipleEntries) {
auto* p = parser(R"(
[[workgroup_size(2, 3, 4), workgroup_size(5, 6, 7)]]
fn main() -> void { return; })");
auto decos = p->decoration_list();
EXPECT_FALSE(p->has_error()) << p->error();
ASSERT_FALSE(decos.errored);
ASSERT_TRUE(decos.matched);
auto f = p->function_decl(decos.value);
EXPECT_FALSE(p->has_error()) << p->error();
EXPECT_FALSE(f.errored);
EXPECT_TRUE(f.matched);
ASSERT_NE(f.value, nullptr);
EXPECT_EQ(f->name(), "main");
ASSERT_NE(f->return_type(), nullptr);
EXPECT_TRUE(f->return_type()->IsVoid());
ASSERT_EQ(f->params().size(), 0u);
ASSERT_NE(f->return_type(), nullptr);
EXPECT_TRUE(f->return_type()->IsVoid());
auto& decorations = f->decorations();
ASSERT_EQ(decorations.size(), 2u);
uint32_t x = 0;
uint32_t y = 0;
uint32_t z = 0;
ASSERT_TRUE(decorations[0]->IsWorkgroup());
std::tie(x, y, z) = decorations[0]->AsWorkgroup()->values();
EXPECT_EQ(x, 2u);
EXPECT_EQ(y, 3u);
EXPECT_EQ(z, 4u);
ASSERT_TRUE(decorations[1]->IsWorkgroup());
std::tie(x, y, z) = decorations[1]->AsWorkgroup()->values();
EXPECT_EQ(x, 5u);
EXPECT_EQ(y, 6u);
EXPECT_EQ(z, 7u);
auto* body = f->body();
ASSERT_EQ(body->size(), 1u);
EXPECT_TRUE(body->get(0)->IsReturn());
}
TEST_F(ParserImplTest, FunctionDecl_DecorationList_MultipleLists) {
auto* p = parser(R"(
[[workgroup_size(2, 3, 4)]]
[[workgroup_size(5, 6, 7)]]
fn main() -> void { return; })");
auto decorations = p->decoration_list();
EXPECT_FALSE(p->has_error()) << p->error();
ASSERT_FALSE(decorations.errored);
ASSERT_TRUE(decorations.matched);
auto f = p->function_decl(decorations.value);
EXPECT_FALSE(p->has_error()) << p->error();
EXPECT_FALSE(f.errored);
EXPECT_TRUE(f.matched);
ASSERT_NE(f.value, nullptr);
EXPECT_EQ(f->name(), "main");
ASSERT_NE(f->return_type(), nullptr);
EXPECT_TRUE(f->return_type()->IsVoid());
ASSERT_EQ(f->params().size(), 0u);
ASSERT_NE(f->return_type(), nullptr);
EXPECT_TRUE(f->return_type()->IsVoid());
auto& decos = f->decorations();
ASSERT_EQ(decos.size(), 2u);
uint32_t x = 0;
uint32_t y = 0;
uint32_t z = 0;
ASSERT_TRUE(decos[0]->IsWorkgroup());
std::tie(x, y, z) = decos[0]->AsWorkgroup()->values();
EXPECT_EQ(x, 2u);
EXPECT_EQ(y, 3u);
EXPECT_EQ(z, 4u);
ASSERT_TRUE(decos[1]->IsWorkgroup());
std::tie(x, y, z) = decos[1]->AsWorkgroup()->values();
EXPECT_EQ(x, 5u);
EXPECT_EQ(y, 6u);
EXPECT_EQ(z, 7u);
auto* body = f->body();
ASSERT_EQ(body->size(), 1u);
EXPECT_TRUE(body->get(0)->IsReturn());
}
TEST_F(ParserImplTest, FunctionDecl_InvalidHeader) {
auto* p = parser("fn main() -> { }");
auto decos = p->decoration_list();
EXPECT_FALSE(p->has_error()) << p->error();
ASSERT_FALSE(decos.errored);
EXPECT_FALSE(decos.matched);
auto f = p->function_decl(decos.value);
EXPECT_TRUE(f.errored);
EXPECT_FALSE(f.matched);
EXPECT_TRUE(p->has_error());
EXPECT_EQ(f.value, nullptr);
EXPECT_EQ(p->error(), "1:14: unable to determine function return type");
}
TEST_F(ParserImplTest, FunctionDecl_InvalidBody) {
auto* p = parser("fn main() -> void { return }");
auto decos = p->decoration_list();
EXPECT_FALSE(p->has_error()) << p->error();
ASSERT_FALSE(decos.errored);
EXPECT_FALSE(decos.matched);
auto f = p->function_decl(decos.value);
EXPECT_TRUE(f.errored);
EXPECT_FALSE(f.matched);
EXPECT_TRUE(p->has_error());
EXPECT_EQ(f.value, nullptr);
EXPECT_EQ(p->error(), "1:28: expected ';' for return statement");
}
TEST_F(ParserImplTest, FunctionDecl_MissingLeftBrace) {
auto* p = parser("fn main() -> void return; }");
auto decos = p->decoration_list();
EXPECT_FALSE(p->has_error()) << p->error();
ASSERT_FALSE(decos.errored);
EXPECT_FALSE(decos.matched);
auto f = p->function_decl(decos.value);
EXPECT_TRUE(f.errored);
EXPECT_FALSE(f.matched);
EXPECT_TRUE(p->has_error());
EXPECT_EQ(f.value, nullptr);
EXPECT_EQ(p->error(), "1:19: expected '{'");
}
} // namespace
} // namespace wgsl
} // namespace reader
} // namespace tint