Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 1 | // Copyright 2020 The Dawn & Tint Authors |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 3 | // Redistribution and use in source and binary forms, with or without |
| 4 | // modification, are permitted provided that the following conditions are met: |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 5 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 6 | // 1. Redistributions of source code must retain the above copyright notice, this |
| 7 | // list of conditions and the following disclaimer. |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 8 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 9 | // 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 27 | |
Ben Clayton | d368f2c | 2023-08-01 00:37:35 +0000 | [diff] [blame] | 28 | #include "src/tint/lang/wgsl/ast/helper_test.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 29 | |
dan sinclair | 34323ac | 2022-04-07 18:39:35 +0000 | [diff] [blame] | 30 | namespace tint::ast { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 31 | namespace { |
| 32 | |
| 33 | using CallExpressionTest = TestHelper; |
James Price | 43c6768 | 2024-06-18 18:45:07 +0000 | [diff] [blame] | 34 | using CallExpressionDeathTest = CallExpressionTest; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 35 | |
| 36 | TEST_F(CallExpressionTest, CreationIdentifier) { |
Ben Clayton | 971318f | 2023-02-14 13:52:43 +0000 | [diff] [blame] | 37 | auto* func = Expr("func"); |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 38 | tint::Vector params{ |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 39 | Expr("param1"), |
| 40 | Expr("param2"), |
| 41 | }; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 42 | |
Ben Clayton | 999db74 | 2023-02-02 15:16:28 +0000 | [diff] [blame] | 43 | auto* stmt = Call(func, params); |
Ben Clayton | 971318f | 2023-02-14 13:52:43 +0000 | [diff] [blame] | 44 | EXPECT_EQ(stmt->target, func); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 45 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 46 | const auto& vec = stmt->args; |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 47 | ASSERT_EQ(vec.Length(), 2u); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 48 | EXPECT_EQ(vec[0], params[0]); |
| 49 | EXPECT_EQ(vec[1], params[1]); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | TEST_F(CallExpressionTest, CreationIdentifier_WithSource) { |
Ben Clayton | 971318f | 2023-02-14 13:52:43 +0000 | [diff] [blame] | 53 | auto* func = Expr("func"); |
Ben Clayton | 999db74 | 2023-02-02 15:16:28 +0000 | [diff] [blame] | 54 | auto* stmt = Call(Source{{20, 2}}, func); |
Ben Clayton | 971318f | 2023-02-14 13:52:43 +0000 | [diff] [blame] | 55 | EXPECT_EQ(stmt->target, func); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 56 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 57 | auto src = stmt->source; |
| 58 | EXPECT_EQ(src.range.begin.line, 20u); |
| 59 | EXPECT_EQ(src.range.begin.column, 2u); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | TEST_F(CallExpressionTest, CreationType) { |
Ben Clayton | 971318f | 2023-02-14 13:52:43 +0000 | [diff] [blame] | 63 | auto* type = Expr(ty.f32()); |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 64 | tint::Vector params{ |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 65 | Expr("param1"), |
| 66 | Expr("param2"), |
| 67 | }; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 68 | |
Ben Clayton | 01ac21c | 2023-02-07 16:14:25 +0000 | [diff] [blame] | 69 | auto* stmt = Call(type, params); |
Ben Clayton | 971318f | 2023-02-14 13:52:43 +0000 | [diff] [blame] | 70 | EXPECT_EQ(stmt->target, type); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 71 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 72 | const auto& vec = stmt->args; |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 73 | ASSERT_EQ(vec.Length(), 2u); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 74 | EXPECT_EQ(vec[0], params[0]); |
| 75 | EXPECT_EQ(vec[1], params[1]); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | TEST_F(CallExpressionTest, CreationType_WithSource) { |
Ben Clayton | 971318f | 2023-02-14 13:52:43 +0000 | [diff] [blame] | 79 | auto* type = Expr(ty.f32()); |
Ben Clayton | 01ac21c | 2023-02-07 16:14:25 +0000 | [diff] [blame] | 80 | auto* stmt = Call(Source{{20, 2}}, type); |
Ben Clayton | 971318f | 2023-02-14 13:52:43 +0000 | [diff] [blame] | 81 | EXPECT_EQ(stmt->target, type); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 82 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 83 | auto src = stmt->source; |
| 84 | EXPECT_EQ(src.range.begin.line, 20u); |
| 85 | EXPECT_EQ(src.range.begin.column, 2u); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | TEST_F(CallExpressionTest, IsCall) { |
Ben Clayton | 971318f | 2023-02-14 13:52:43 +0000 | [diff] [blame] | 89 | auto* func = Expr("func"); |
Ben Clayton | 999db74 | 2023-02-02 15:16:28 +0000 | [diff] [blame] | 90 | auto* stmt = Call(func); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 91 | EXPECT_TRUE(stmt->Is<CallExpression>()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 92 | } |
| 93 | |
James Price | 43c6768 | 2024-06-18 18:45:07 +0000 | [diff] [blame] | 94 | TEST_F(CallExpressionDeathTest, Assert_Null_Identifier) { |
Ben Clayton | a655053 | 2024-05-09 16:07:00 +0000 | [diff] [blame] | 95 | EXPECT_DEATH_IF_SUPPORTED( |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 96 | { |
| 97 | ProgramBuilder b; |
Ben Clayton | 999db74 | 2023-02-02 15:16:28 +0000 | [diff] [blame] | 98 | b.Call(static_cast<Identifier*>(nullptr)); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 99 | }, |
| 100 | "internal compiler error"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 101 | } |
| 102 | |
James Price | 43c6768 | 2024-06-18 18:45:07 +0000 | [diff] [blame] | 103 | TEST_F(CallExpressionDeathTest, Assert_Null_Param) { |
Ben Clayton | a655053 | 2024-05-09 16:07:00 +0000 | [diff] [blame] | 104 | EXPECT_DEATH_IF_SUPPORTED( |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 105 | { |
| 106 | ProgramBuilder b; |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 107 | b.Call(b.Ident("func"), tint::Vector{ |
Ben Clayton | 999db74 | 2023-02-02 15:16:28 +0000 | [diff] [blame] | 108 | b.Expr("param1"), |
| 109 | nullptr, |
| 110 | b.Expr("param2"), |
| 111 | }); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 112 | }, |
| 113 | "internal compiler error"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 114 | } |
| 115 | |
James Price | 43c6768 | 2024-06-18 18:45:07 +0000 | [diff] [blame] | 116 | TEST_F(CallExpressionDeathTest, Assert_DifferentGenerationID_Identifier) { |
Ben Clayton | a655053 | 2024-05-09 16:07:00 +0000 | [diff] [blame] | 117 | EXPECT_DEATH_IF_SUPPORTED( |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 118 | { |
| 119 | ProgramBuilder b1; |
| 120 | ProgramBuilder b2; |
Ben Clayton | 999db74 | 2023-02-02 15:16:28 +0000 | [diff] [blame] | 121 | b1.Call(b2.Ident("func")); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 122 | }, |
| 123 | "internal compiler error"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 124 | } |
| 125 | |
James Price | 43c6768 | 2024-06-18 18:45:07 +0000 | [diff] [blame] | 126 | TEST_F(CallExpressionDeathTest, Assert_DifferentGenerationID_Type) { |
Ben Clayton | a655053 | 2024-05-09 16:07:00 +0000 | [diff] [blame] | 127 | EXPECT_DEATH_IF_SUPPORTED( |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 128 | { |
| 129 | ProgramBuilder b1; |
| 130 | ProgramBuilder b2; |
Ben Clayton | 01ac21c | 2023-02-07 16:14:25 +0000 | [diff] [blame] | 131 | b1.Call(b2.ty.f32()); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 132 | }, |
| 133 | "internal compiler error"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 134 | } |
| 135 | |
James Price | 43c6768 | 2024-06-18 18:45:07 +0000 | [diff] [blame] | 136 | TEST_F(CallExpressionDeathTest, Assert_DifferentGenerationID_Param) { |
Ben Clayton | a655053 | 2024-05-09 16:07:00 +0000 | [diff] [blame] | 137 | EXPECT_DEATH_IF_SUPPORTED( |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 138 | { |
| 139 | ProgramBuilder b1; |
| 140 | ProgramBuilder b2; |
Ben Clayton | 999db74 | 2023-02-02 15:16:28 +0000 | [diff] [blame] | 141 | b1.Call(b1.Ident("func"), b2.Expr("param1")); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 142 | }, |
| 143 | "internal compiler error"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | } // namespace |
dan sinclair | 34323ac | 2022-04-07 18:39:35 +0000 | [diff] [blame] | 147 | } // namespace tint::ast |