blob: d719926643bfc98350e60944f0dc6c2fbd662139 [file] [log] [blame]
Austin Engcc2516a2023-10-17 20:57:54 +00001// Copyright 2020 The Dawn & Tint Authors
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002//
Austin Engcc2516a2023-10-17 20:57:54 +00003// Redistribution and use in source and binary forms, with or without
4// modification, are permitted provided that the following conditions are met:
Ryan Harrisondbc13af2022-02-21 15:19:07 +00005//
Austin Engcc2516a2023-10-17 20:57:54 +00006// 1. Redistributions of source code must retain the above copyright notice, this
7// list of conditions and the following disclaimer.
Ryan Harrisondbc13af2022-02-21 15:19:07 +00008//
Austin Engcc2516a2023-10-17 20:57:54 +00009// 2. Redistributions in binary form must reproduce the above copyright notice,
10// this list of conditions and the following disclaimer in the documentation
11// and/or other materials provided with the distribution.
12//
13// 3. Neither the name of the copyright holder nor the names of its
14// contributors may be used to endorse or promote products derived from
15// this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Ryan Harrisondbc13af2022-02-21 15:19:07 +000027
Ben Claytond368f2c2023-08-01 00:37:35 +000028#include "src/tint/lang/wgsl/ast/helper_test.h"
Ryan Harrisondbc13af2022-02-21 15:19:07 +000029
dan sinclair34323ac2022-04-07 18:39:35 +000030namespace tint::ast {
Ryan Harrisondbc13af2022-02-21 15:19:07 +000031namespace {
32
33using CallExpressionTest = TestHelper;
James Price43c67682024-06-18 18:45:07 +000034using CallExpressionDeathTest = CallExpressionTest;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000035
36TEST_F(CallExpressionTest, CreationIdentifier) {
Ben Clayton971318f2023-02-14 13:52:43 +000037 auto* func = Expr("func");
dan sinclairbae54e72023-07-28 15:01:54 +000038 tint::Vector params{
Ben Clayton783b1692022-08-02 17:03:35 +000039 Expr("param1"),
40 Expr("param2"),
41 };
Ryan Harrisondbc13af2022-02-21 15:19:07 +000042
Ben Clayton999db742023-02-02 15:16:28 +000043 auto* stmt = Call(func, params);
Ben Clayton971318f2023-02-14 13:52:43 +000044 EXPECT_EQ(stmt->target, func);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000045
dan sinclair41e4d9a2022-05-01 14:40:55 +000046 const auto& vec = stmt->args;
Ben Clayton783b1692022-08-02 17:03:35 +000047 ASSERT_EQ(vec.Length(), 2u);
dan sinclair41e4d9a2022-05-01 14:40:55 +000048 EXPECT_EQ(vec[0], params[0]);
49 EXPECT_EQ(vec[1], params[1]);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000050}
51
52TEST_F(CallExpressionTest, CreationIdentifier_WithSource) {
Ben Clayton971318f2023-02-14 13:52:43 +000053 auto* func = Expr("func");
Ben Clayton999db742023-02-02 15:16:28 +000054 auto* stmt = Call(Source{{20, 2}}, func);
Ben Clayton971318f2023-02-14 13:52:43 +000055 EXPECT_EQ(stmt->target, func);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000056
dan sinclair41e4d9a2022-05-01 14:40:55 +000057 auto src = stmt->source;
58 EXPECT_EQ(src.range.begin.line, 20u);
59 EXPECT_EQ(src.range.begin.column, 2u);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000060}
61
62TEST_F(CallExpressionTest, CreationType) {
Ben Clayton971318f2023-02-14 13:52:43 +000063 auto* type = Expr(ty.f32());
dan sinclairbae54e72023-07-28 15:01:54 +000064 tint::Vector params{
Ben Clayton783b1692022-08-02 17:03:35 +000065 Expr("param1"),
66 Expr("param2"),
67 };
Ryan Harrisondbc13af2022-02-21 15:19:07 +000068
Ben Clayton01ac21c2023-02-07 16:14:25 +000069 auto* stmt = Call(type, params);
Ben Clayton971318f2023-02-14 13:52:43 +000070 EXPECT_EQ(stmt->target, type);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000071
dan sinclair41e4d9a2022-05-01 14:40:55 +000072 const auto& vec = stmt->args;
Ben Clayton783b1692022-08-02 17:03:35 +000073 ASSERT_EQ(vec.Length(), 2u);
dan sinclair41e4d9a2022-05-01 14:40:55 +000074 EXPECT_EQ(vec[0], params[0]);
75 EXPECT_EQ(vec[1], params[1]);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000076}
77
78TEST_F(CallExpressionTest, CreationType_WithSource) {
Ben Clayton971318f2023-02-14 13:52:43 +000079 auto* type = Expr(ty.f32());
Ben Clayton01ac21c2023-02-07 16:14:25 +000080 auto* stmt = Call(Source{{20, 2}}, type);
Ben Clayton971318f2023-02-14 13:52:43 +000081 EXPECT_EQ(stmt->target, type);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000082
dan sinclair41e4d9a2022-05-01 14:40:55 +000083 auto src = stmt->source;
84 EXPECT_EQ(src.range.begin.line, 20u);
85 EXPECT_EQ(src.range.begin.column, 2u);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000086}
87
88TEST_F(CallExpressionTest, IsCall) {
Ben Clayton971318f2023-02-14 13:52:43 +000089 auto* func = Expr("func");
Ben Clayton999db742023-02-02 15:16:28 +000090 auto* stmt = Call(func);
dan sinclair41e4d9a2022-05-01 14:40:55 +000091 EXPECT_TRUE(stmt->Is<CallExpression>());
Ryan Harrisondbc13af2022-02-21 15:19:07 +000092}
93
James Price43c67682024-06-18 18:45:07 +000094TEST_F(CallExpressionDeathTest, Assert_Null_Identifier) {
Ben Claytona6550532024-05-09 16:07:00 +000095 EXPECT_DEATH_IF_SUPPORTED(
dan sinclair41e4d9a2022-05-01 14:40:55 +000096 {
97 ProgramBuilder b;
Ben Clayton999db742023-02-02 15:16:28 +000098 b.Call(static_cast<Identifier*>(nullptr));
dan sinclair41e4d9a2022-05-01 14:40:55 +000099 },
100 "internal compiler error");
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000101}
102
James Price43c67682024-06-18 18:45:07 +0000103TEST_F(CallExpressionDeathTest, Assert_Null_Param) {
Ben Claytona6550532024-05-09 16:07:00 +0000104 EXPECT_DEATH_IF_SUPPORTED(
dan sinclair41e4d9a2022-05-01 14:40:55 +0000105 {
106 ProgramBuilder b;
dan sinclairbae54e72023-07-28 15:01:54 +0000107 b.Call(b.Ident("func"), tint::Vector{
Ben Clayton999db742023-02-02 15:16:28 +0000108 b.Expr("param1"),
109 nullptr,
110 b.Expr("param2"),
111 });
dan sinclair41e4d9a2022-05-01 14:40:55 +0000112 },
113 "internal compiler error");
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000114}
115
James Price43c67682024-06-18 18:45:07 +0000116TEST_F(CallExpressionDeathTest, Assert_DifferentGenerationID_Identifier) {
Ben Claytona6550532024-05-09 16:07:00 +0000117 EXPECT_DEATH_IF_SUPPORTED(
dan sinclair41e4d9a2022-05-01 14:40:55 +0000118 {
119 ProgramBuilder b1;
120 ProgramBuilder b2;
Ben Clayton999db742023-02-02 15:16:28 +0000121 b1.Call(b2.Ident("func"));
dan sinclair41e4d9a2022-05-01 14:40:55 +0000122 },
123 "internal compiler error");
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000124}
125
James Price43c67682024-06-18 18:45:07 +0000126TEST_F(CallExpressionDeathTest, Assert_DifferentGenerationID_Type) {
Ben Claytona6550532024-05-09 16:07:00 +0000127 EXPECT_DEATH_IF_SUPPORTED(
dan sinclair41e4d9a2022-05-01 14:40:55 +0000128 {
129 ProgramBuilder b1;
130 ProgramBuilder b2;
Ben Clayton01ac21c2023-02-07 16:14:25 +0000131 b1.Call(b2.ty.f32());
dan sinclair41e4d9a2022-05-01 14:40:55 +0000132 },
133 "internal compiler error");
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000134}
135
James Price43c67682024-06-18 18:45:07 +0000136TEST_F(CallExpressionDeathTest, Assert_DifferentGenerationID_Param) {
Ben Claytona6550532024-05-09 16:07:00 +0000137 EXPECT_DEATH_IF_SUPPORTED(
dan sinclair41e4d9a2022-05-01 14:40:55 +0000138 {
139 ProgramBuilder b1;
140 ProgramBuilder b2;
Ben Clayton999db742023-02-02 15:16:28 +0000141 b1.Call(b1.Ident("func"), b2.Expr("param1"));
dan sinclair41e4d9a2022-05-01 14:40:55 +0000142 },
143 "internal compiler error");
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000144}
145
146} // namespace
dan sinclair34323ac2022-04-07 18:39:35 +0000147} // namespace tint::ast