dan sinclair | b7edc4c | 2020-04-07 12:46:30 +0000 | [diff] [blame] | 1 | // Copyright 2020 The Tint Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include "src/type_determiner.h" |
| 16 | |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 17 | #include <algorithm> |
dan sinclair | b7edc4c | 2020-04-07 12:46:30 +0000 | [diff] [blame] | 18 | #include <memory> |
| 19 | #include <utility> |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 20 | #include <vector> |
dan sinclair | b7edc4c | 2020-04-07 12:46:30 +0000 | [diff] [blame] | 21 | |
| 22 | #include "gtest/gtest.h" |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 23 | #include "src/ast/array_accessor_expression.h" |
dan sinclair | 6c498fc | 2020-04-07 12:47:23 +0000 | [diff] [blame] | 24 | #include "src/ast/assignment_statement.h" |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 25 | #include "src/ast/binary_expression.h" |
dan sinclair | a7d498e | 2020-09-22 22:07:13 +0000 | [diff] [blame] | 26 | #include "src/ast/bitcast_expression.h" |
dan sinclair | 0975dd5 | 2020-07-27 15:25:00 +0000 | [diff] [blame] | 27 | #include "src/ast/block_statement.h" |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 28 | #include "src/ast/bool_literal.h" |
dan sinclair | b7ea6e2 | 2020-04-07 12:54:10 +0000 | [diff] [blame] | 29 | #include "src/ast/break_statement.h" |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 30 | #include "src/ast/builder.h" |
dan sinclair | 3ca8746 | 2020-04-07 16:41:10 +0000 | [diff] [blame] | 31 | #include "src/ast/call_expression.h" |
dan sinclair | 50080b7 | 2020-07-21 13:42:13 +0000 | [diff] [blame] | 32 | #include "src/ast/call_statement.h" |
dan sinclair | 6010b29 | 2020-04-07 12:54:20 +0000 | [diff] [blame] | 33 | #include "src/ast/case_statement.h" |
dan sinclair | aec965e | 2020-04-07 12:54:29 +0000 | [diff] [blame] | 34 | #include "src/ast/continue_statement.h" |
dan sinclair | 0cf685f | 2020-04-07 12:54:37 +0000 | [diff] [blame] | 35 | #include "src/ast/else_statement.h" |
dan sinclair | b7edc4c | 2020-04-07 12:46:30 +0000 | [diff] [blame] | 36 | #include "src/ast/float_literal.h" |
dan sinclair | cab0e73 | 2020-04-07 12:57:27 +0000 | [diff] [blame] | 37 | #include "src/ast/identifier_expression.h" |
dan sinclair | 91c44a5 | 2020-04-07 12:55:25 +0000 | [diff] [blame] | 38 | #include "src/ast/if_statement.h" |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 39 | #include "src/ast/intrinsic_texture_helper_test.h" |
dan sinclair | bc71eda | 2020-04-07 12:55:51 +0000 | [diff] [blame] | 40 | #include "src/ast/loop_statement.h" |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 41 | #include "src/ast/member_accessor_expression.h" |
dan sinclair | 0592643 | 2020-09-21 17:51:31 +0000 | [diff] [blame] | 42 | #include "src/ast/pipeline_stage.h" |
dan sinclair | bf0fff8 | 2020-04-07 12:56:24 +0000 | [diff] [blame] | 43 | #include "src/ast/return_statement.h" |
dan sinclair | b7edc4c | 2020-04-07 12:46:30 +0000 | [diff] [blame] | 44 | #include "src/ast/scalar_constructor_expression.h" |
dan sinclair | c6f2947 | 2020-06-02 20:11:44 +0000 | [diff] [blame] | 45 | #include "src/ast/sint_literal.h" |
dan sinclair | 0592643 | 2020-09-21 17:51:31 +0000 | [diff] [blame] | 46 | #include "src/ast/stage_decoration.h" |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 47 | #include "src/ast/struct.h" |
| 48 | #include "src/ast/struct_member.h" |
dan sinclair | 18b3285 | 2020-04-07 12:56:45 +0000 | [diff] [blame] | 49 | #include "src/ast/switch_statement.h" |
dan sinclair | b445a9b | 2020-04-24 00:40:45 +0000 | [diff] [blame] | 50 | #include "src/ast/type/alias_type.h" |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 51 | #include "src/ast/type/array_type.h" |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 52 | #include "src/ast/type/bool_type.h" |
Tomek Ponitka | 1a61fc4 | 2020-08-31 15:24:54 +0000 | [diff] [blame] | 53 | #include "src/ast/type/depth_texture_type.h" |
dan sinclair | b7edc4c | 2020-04-07 12:46:30 +0000 | [diff] [blame] | 54 | #include "src/ast/type/f32_type.h" |
dan sinclair | 6c498fc | 2020-04-07 12:47:23 +0000 | [diff] [blame] | 55 | #include "src/ast/type/i32_type.h" |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 56 | #include "src/ast/type/matrix_type.h" |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 57 | #include "src/ast/type/multisampled_texture_type.h" |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 58 | #include "src/ast/type/pointer_type.h" |
Tomek Ponitka | 1a61fc4 | 2020-08-31 15:24:54 +0000 | [diff] [blame] | 59 | #include "src/ast/type/sampled_texture_type.h" |
| 60 | #include "src/ast/type/sampler_type.h" |
| 61 | #include "src/ast/type/storage_texture_type.h" |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 62 | #include "src/ast/type/struct_type.h" |
Tomek Ponitka | 1a61fc4 | 2020-08-31 15:24:54 +0000 | [diff] [blame] | 63 | #include "src/ast/type/texture_type.h" |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 64 | #include "src/ast/type/u32_type.h" |
dan sinclair | b7edc4c | 2020-04-07 12:46:30 +0000 | [diff] [blame] | 65 | #include "src/ast/type/vector_type.h" |
| 66 | #include "src/ast/type_constructor_expression.h" |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 67 | #include "src/ast/uint_literal.h" |
dan sinclair | 327ed1b | 2020-04-07 19:27:21 +0000 | [diff] [blame] | 68 | #include "src/ast/unary_op_expression.h" |
dan sinclair | ca893e3 | 2020-04-07 12:57:12 +0000 | [diff] [blame] | 69 | #include "src/ast/variable_decl_statement.h" |
dan sinclair | b7edc4c | 2020-04-07 12:46:30 +0000 | [diff] [blame] | 70 | |
| 71 | namespace tint { |
| 72 | namespace { |
| 73 | |
dan sinclair | 7456f42 | 2020-04-08 19:58:35 +0000 | [diff] [blame] | 74 | class FakeStmt : public ast::Statement { |
| 75 | public: |
Ben Clayton | ed2b978 | 2020-12-01 18:04:17 +0000 | [diff] [blame] | 76 | FakeStmt* Clone(ast::CloneContext*) const override { return nullptr; } |
dan sinclair | 7456f42 | 2020-04-08 19:58:35 +0000 | [diff] [blame] | 77 | bool IsValid() const override { return true; } |
dan sinclair | 0975dd5 | 2020-07-27 15:25:00 +0000 | [diff] [blame] | 78 | void to_str(std::ostream& out, size_t) const override { out << "Fake"; } |
dan sinclair | 7456f42 | 2020-04-08 19:58:35 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | class FakeExpr : public ast::Expression { |
| 82 | public: |
Ben Clayton | ed2b978 | 2020-12-01 18:04:17 +0000 | [diff] [blame] | 83 | FakeExpr* Clone(ast::CloneContext*) const override { return nullptr; } |
dan sinclair | 7456f42 | 2020-04-08 19:58:35 +0000 | [diff] [blame] | 84 | bool IsValid() const override { return true; } |
| 85 | void to_str(std::ostream&, size_t) const override {} |
| 86 | }; |
| 87 | |
dan sinclair | 685cb02 | 2020-12-02 21:17:58 +0000 | [diff] [blame] | 88 | class TypeDeterminerHelper : public ast::BuilderWithModule { |
dan sinclair | b7edc4c | 2020-04-07 12:46:30 +0000 | [diff] [blame] | 89 | public: |
dan sinclair | 685cb02 | 2020-12-02 21:17:58 +0000 | [diff] [blame] | 90 | TypeDeterminerHelper() : td_(std::make_unique<TypeDeterminer>(mod)) {} |
dan sinclair | b7edc4c | 2020-04-07 12:46:30 +0000 | [diff] [blame] | 91 | |
| 92 | TypeDeterminer* td() const { return td_.get(); } |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 93 | |
dan sinclair | b7edc4c | 2020-04-07 12:46:30 +0000 | [diff] [blame] | 94 | private: |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 95 | void OnVariableBuilt(ast::Variable* var) override { |
| 96 | td_->RegisterVariableForTesting(var); |
| 97 | } |
| 98 | |
dan sinclair | b7edc4c | 2020-04-07 12:46:30 +0000 | [diff] [blame] | 99 | std::unique_ptr<TypeDeterminer> td_; |
| 100 | }; |
| 101 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 102 | class TypeDeterminerTest : public TypeDeterminerHelper, public testing::Test {}; |
| 103 | |
| 104 | template <typename T> |
| 105 | class TypeDeterminerTestWithParam : public TypeDeterminerHelper, |
| 106 | public testing::TestWithParam<T> {}; |
| 107 | |
dan sinclair | 7456f42 | 2020-04-08 19:58:35 +0000 | [diff] [blame] | 108 | TEST_F(TypeDeterminerTest, Error_WithEmptySource) { |
| 109 | FakeStmt s; |
Ben Clayton | fc5a9cf | 2020-11-02 15:07:47 +0000 | [diff] [blame] | 110 | s.set_source(Source{}); |
dan sinclair | 7456f42 | 2020-04-08 19:58:35 +0000 | [diff] [blame] | 111 | |
| 112 | EXPECT_FALSE(td()->DetermineResultType(&s)); |
dan sinclair | 0975dd5 | 2020-07-27 15:25:00 +0000 | [diff] [blame] | 113 | EXPECT_EQ(td()->error(), |
| 114 | "unknown statement type for type determination: Fake"); |
dan sinclair | 7456f42 | 2020-04-08 19:58:35 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | TEST_F(TypeDeterminerTest, Stmt_Error_Unknown) { |
| 118 | FakeStmt s; |
Ben Clayton | fc5a9cf | 2020-11-02 15:07:47 +0000 | [diff] [blame] | 119 | s.set_source(Source{Source::Location{2, 30}}); |
dan sinclair | 7456f42 | 2020-04-08 19:58:35 +0000 | [diff] [blame] | 120 | |
| 121 | EXPECT_FALSE(td()->DetermineResultType(&s)); |
| 122 | EXPECT_EQ(td()->error(), |
dan sinclair | 0975dd5 | 2020-07-27 15:25:00 +0000 | [diff] [blame] | 123 | "2:30: unknown statement type for type determination: Fake"); |
dan sinclair | 7456f42 | 2020-04-08 19:58:35 +0000 | [diff] [blame] | 124 | } |
| 125 | |
dan sinclair | 6c498fc | 2020-04-07 12:47:23 +0000 | [diff] [blame] | 126 | TEST_F(TypeDeterminerTest, Stmt_Assign) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 127 | ast::type::F32 f32; |
| 128 | ast::type::I32 i32; |
dan sinclair | 6c498fc | 2020-04-07 12:47:23 +0000 | [diff] [blame] | 129 | |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 130 | auto* lhs = create<ast::ScalarConstructorExpression>( |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 131 | create<ast::SintLiteral>(&i32, 2)); |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 132 | auto* rhs = create<ast::ScalarConstructorExpression>( |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 133 | create<ast::FloatLiteral>(&f32, 2.3f)); |
dan sinclair | 6c498fc | 2020-04-07 12:47:23 +0000 | [diff] [blame] | 134 | |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 135 | ast::AssignmentStatement assign(lhs, rhs); |
dan sinclair | 6c498fc | 2020-04-07 12:47:23 +0000 | [diff] [blame] | 136 | |
| 137 | EXPECT_TRUE(td()->DetermineResultType(&assign)); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 138 | ASSERT_NE(lhs->result_type(), nullptr); |
| 139 | ASSERT_NE(rhs->result_type(), nullptr); |
dan sinclair | 6c498fc | 2020-04-07 12:47:23 +0000 | [diff] [blame] | 140 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 141 | EXPECT_TRUE(lhs->result_type()->Is<ast::type::I32>()); |
| 142 | EXPECT_TRUE(rhs->result_type()->Is<ast::type::F32>()); |
dan sinclair | 6c498fc | 2020-04-07 12:47:23 +0000 | [diff] [blame] | 143 | } |
| 144 | |
dan sinclair | 6010b29 | 2020-04-07 12:54:20 +0000 | [diff] [blame] | 145 | TEST_F(TypeDeterminerTest, Stmt_Case) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 146 | ast::type::I32 i32; |
| 147 | ast::type::F32 f32; |
dan sinclair | 6010b29 | 2020-04-07 12:54:20 +0000 | [diff] [blame] | 148 | |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 149 | auto* lhs = create<ast::ScalarConstructorExpression>( |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 150 | create<ast::SintLiteral>(&i32, 2)); |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 151 | auto* rhs = create<ast::ScalarConstructorExpression>( |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 152 | create<ast::FloatLiteral>(&f32, 2.3f)); |
dan sinclair | 6010b29 | 2020-04-07 12:54:20 +0000 | [diff] [blame] | 153 | |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 154 | auto* body = create<ast::BlockStatement>(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 155 | body->append(create<ast::AssignmentStatement>(lhs, rhs)); |
dan sinclair | 6010b29 | 2020-04-07 12:54:20 +0000 | [diff] [blame] | 156 | |
dan sinclair | 1aadbd4 | 2020-06-01 16:56:46 +0000 | [diff] [blame] | 157 | ast::CaseSelectorList lit; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 158 | lit.push_back(create<ast::SintLiteral>(&i32, 3)); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 159 | ast::CaseStatement cse(lit, body); |
dan sinclair | 6010b29 | 2020-04-07 12:54:20 +0000 | [diff] [blame] | 160 | |
| 161 | EXPECT_TRUE(td()->DetermineResultType(&cse)); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 162 | ASSERT_NE(lhs->result_type(), nullptr); |
| 163 | ASSERT_NE(rhs->result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 164 | EXPECT_TRUE(lhs->result_type()->Is<ast::type::I32>()); |
| 165 | EXPECT_TRUE(rhs->result_type()->Is<ast::type::F32>()); |
dan sinclair | 6010b29 | 2020-04-07 12:54:20 +0000 | [diff] [blame] | 166 | } |
| 167 | |
dan sinclair | 0975dd5 | 2020-07-27 15:25:00 +0000 | [diff] [blame] | 168 | TEST_F(TypeDeterminerTest, Stmt_Block) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 169 | ast::type::I32 i32; |
| 170 | ast::type::F32 f32; |
dan sinclair | 0975dd5 | 2020-07-27 15:25:00 +0000 | [diff] [blame] | 171 | |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 172 | auto* lhs = create<ast::ScalarConstructorExpression>( |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 173 | create<ast::SintLiteral>(&i32, 2)); |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 174 | auto* rhs = create<ast::ScalarConstructorExpression>( |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 175 | create<ast::FloatLiteral>(&f32, 2.3f)); |
dan sinclair | 0975dd5 | 2020-07-27 15:25:00 +0000 | [diff] [blame] | 176 | |
| 177 | ast::BlockStatement block; |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 178 | block.append(create<ast::AssignmentStatement>(lhs, rhs)); |
dan sinclair | 0975dd5 | 2020-07-27 15:25:00 +0000 | [diff] [blame] | 179 | |
| 180 | EXPECT_TRUE(td()->DetermineResultType(&block)); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 181 | ASSERT_NE(lhs->result_type(), nullptr); |
| 182 | ASSERT_NE(rhs->result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 183 | EXPECT_TRUE(lhs->result_type()->Is<ast::type::I32>()); |
| 184 | EXPECT_TRUE(rhs->result_type()->Is<ast::type::F32>()); |
dan sinclair | 0975dd5 | 2020-07-27 15:25:00 +0000 | [diff] [blame] | 185 | } |
| 186 | |
dan sinclair | 0cf685f | 2020-04-07 12:54:37 +0000 | [diff] [blame] | 187 | TEST_F(TypeDeterminerTest, Stmt_Else) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 188 | ast::type::I32 i32; |
| 189 | ast::type::F32 f32; |
dan sinclair | 0cf685f | 2020-04-07 12:54:37 +0000 | [diff] [blame] | 190 | |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 191 | auto* lhs = create<ast::ScalarConstructorExpression>( |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 192 | create<ast::SintLiteral>(&i32, 2)); |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 193 | auto* rhs = create<ast::ScalarConstructorExpression>( |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 194 | create<ast::FloatLiteral>(&f32, 2.3f)); |
dan sinclair | 0cf685f | 2020-04-07 12:54:37 +0000 | [diff] [blame] | 195 | |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 196 | auto* body = create<ast::BlockStatement>(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 197 | body->append(create<ast::AssignmentStatement>(lhs, rhs)); |
dan sinclair | 0cf685f | 2020-04-07 12:54:37 +0000 | [diff] [blame] | 198 | |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 199 | ast::ElseStatement stmt(create<ast::ScalarConstructorExpression>( |
| 200 | create<ast::SintLiteral>(&i32, 3)), |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 201 | body); |
dan sinclair | 0cf685f | 2020-04-07 12:54:37 +0000 | [diff] [blame] | 202 | |
| 203 | EXPECT_TRUE(td()->DetermineResultType(&stmt)); |
| 204 | ASSERT_NE(stmt.condition()->result_type(), nullptr); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 205 | ASSERT_NE(lhs->result_type(), nullptr); |
| 206 | ASSERT_NE(rhs->result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 207 | EXPECT_TRUE(stmt.condition()->result_type()->Is<ast::type::I32>()); |
| 208 | EXPECT_TRUE(lhs->result_type()->Is<ast::type::I32>()); |
| 209 | EXPECT_TRUE(rhs->result_type()->Is<ast::type::F32>()); |
dan sinclair | 0cf685f | 2020-04-07 12:54:37 +0000 | [diff] [blame] | 210 | } |
| 211 | |
dan sinclair | 91c44a5 | 2020-04-07 12:55:25 +0000 | [diff] [blame] | 212 | TEST_F(TypeDeterminerTest, Stmt_If) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 213 | ast::type::I32 i32; |
| 214 | ast::type::F32 f32; |
dan sinclair | 91c44a5 | 2020-04-07 12:55:25 +0000 | [diff] [blame] | 215 | |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 216 | auto* else_lhs = create<ast::ScalarConstructorExpression>( |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 217 | create<ast::SintLiteral>(&i32, 2)); |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 218 | auto* else_rhs = create<ast::ScalarConstructorExpression>( |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 219 | create<ast::FloatLiteral>(&f32, 2.3f)); |
dan sinclair | 91c44a5 | 2020-04-07 12:55:25 +0000 | [diff] [blame] | 220 | |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 221 | auto* else_body = create<ast::BlockStatement>(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 222 | else_body->append(create<ast::AssignmentStatement>(else_lhs, else_rhs)); |
dan sinclair | 91c44a5 | 2020-04-07 12:55:25 +0000 | [diff] [blame] | 223 | |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 224 | auto* else_stmt = |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 225 | create<ast::ElseStatement>(create<ast::ScalarConstructorExpression>( |
| 226 | create<ast::SintLiteral>(&i32, 3)), |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 227 | else_body); |
dan sinclair | 91c44a5 | 2020-04-07 12:55:25 +0000 | [diff] [blame] | 228 | |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 229 | auto* lhs = create<ast::ScalarConstructorExpression>( |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 230 | create<ast::SintLiteral>(&i32, 2)); |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 231 | auto* rhs = create<ast::ScalarConstructorExpression>( |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 232 | create<ast::FloatLiteral>(&f32, 2.3f)); |
dan sinclair | 91c44a5 | 2020-04-07 12:55:25 +0000 | [diff] [blame] | 233 | |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 234 | auto* body = create<ast::BlockStatement>(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 235 | body->append(create<ast::AssignmentStatement>(lhs, rhs)); |
dan sinclair | 91c44a5 | 2020-04-07 12:55:25 +0000 | [diff] [blame] | 236 | |
Ben Clayton | 9838768 | 2020-12-07 20:11:24 +0000 | [diff] [blame] | 237 | ast::IfStatement stmt(Source{}, |
| 238 | create<ast::ScalarConstructorExpression>( |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 239 | create<ast::SintLiteral>(&i32, 3)), |
Ben Clayton | 9838768 | 2020-12-07 20:11:24 +0000 | [diff] [blame] | 240 | body, ast::ElseStatementList{else_stmt}); |
dan sinclair | 91c44a5 | 2020-04-07 12:55:25 +0000 | [diff] [blame] | 241 | |
| 242 | EXPECT_TRUE(td()->DetermineResultType(&stmt)); |
| 243 | ASSERT_NE(stmt.condition()->result_type(), nullptr); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 244 | ASSERT_NE(else_lhs->result_type(), nullptr); |
| 245 | ASSERT_NE(else_rhs->result_type(), nullptr); |
| 246 | ASSERT_NE(lhs->result_type(), nullptr); |
| 247 | ASSERT_NE(rhs->result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 248 | EXPECT_TRUE(stmt.condition()->result_type()->Is<ast::type::I32>()); |
| 249 | EXPECT_TRUE(else_lhs->result_type()->Is<ast::type::I32>()); |
| 250 | EXPECT_TRUE(else_rhs->result_type()->Is<ast::type::F32>()); |
| 251 | EXPECT_TRUE(lhs->result_type()->Is<ast::type::I32>()); |
| 252 | EXPECT_TRUE(rhs->result_type()->Is<ast::type::F32>()); |
dan sinclair | 91c44a5 | 2020-04-07 12:55:25 +0000 | [diff] [blame] | 253 | } |
| 254 | |
dan sinclair | bc71eda | 2020-04-07 12:55:51 +0000 | [diff] [blame] | 255 | TEST_F(TypeDeterminerTest, Stmt_Loop) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 256 | ast::type::I32 i32; |
| 257 | ast::type::F32 f32; |
dan sinclair | bc71eda | 2020-04-07 12:55:51 +0000 | [diff] [blame] | 258 | |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 259 | auto* body_lhs = create<ast::ScalarConstructorExpression>( |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 260 | create<ast::SintLiteral>(&i32, 2)); |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 261 | auto* body_rhs = create<ast::ScalarConstructorExpression>( |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 262 | create<ast::FloatLiteral>(&f32, 2.3f)); |
dan sinclair | bc71eda | 2020-04-07 12:55:51 +0000 | [diff] [blame] | 263 | |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 264 | auto* body = create<ast::BlockStatement>(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 265 | body->append(create<ast::AssignmentStatement>(body_lhs, body_rhs)); |
dan sinclair | bc71eda | 2020-04-07 12:55:51 +0000 | [diff] [blame] | 266 | |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 267 | auto* continuing_lhs = create<ast::ScalarConstructorExpression>( |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 268 | create<ast::SintLiteral>(&i32, 2)); |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 269 | auto* continuing_rhs = create<ast::ScalarConstructorExpression>( |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 270 | create<ast::FloatLiteral>(&f32, 2.3f)); |
dan sinclair | bc71eda | 2020-04-07 12:55:51 +0000 | [diff] [blame] | 271 | |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 272 | auto* continuing = create<ast::BlockStatement>(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 273 | continuing->append( |
| 274 | create<ast::AssignmentStatement>(continuing_lhs, continuing_rhs)); |
dan sinclair | bc71eda | 2020-04-07 12:55:51 +0000 | [diff] [blame] | 275 | |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 276 | ast::LoopStatement stmt(body, continuing); |
dan sinclair | bc71eda | 2020-04-07 12:55:51 +0000 | [diff] [blame] | 277 | |
| 278 | EXPECT_TRUE(td()->DetermineResultType(&stmt)); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 279 | ASSERT_NE(body_lhs->result_type(), nullptr); |
| 280 | ASSERT_NE(body_rhs->result_type(), nullptr); |
| 281 | ASSERT_NE(continuing_lhs->result_type(), nullptr); |
| 282 | ASSERT_NE(continuing_rhs->result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 283 | EXPECT_TRUE(body_lhs->result_type()->Is<ast::type::I32>()); |
| 284 | EXPECT_TRUE(body_rhs->result_type()->Is<ast::type::F32>()); |
| 285 | EXPECT_TRUE(continuing_lhs->result_type()->Is<ast::type::I32>()); |
| 286 | EXPECT_TRUE(continuing_rhs->result_type()->Is<ast::type::F32>()); |
dan sinclair | bc71eda | 2020-04-07 12:55:51 +0000 | [diff] [blame] | 287 | } |
| 288 | |
dan sinclair | bf0fff8 | 2020-04-07 12:56:24 +0000 | [diff] [blame] | 289 | TEST_F(TypeDeterminerTest, Stmt_Return) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 290 | ast::type::I32 i32; |
dan sinclair | bf0fff8 | 2020-04-07 12:56:24 +0000 | [diff] [blame] | 291 | |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 292 | auto* cond = create<ast::ScalarConstructorExpression>( |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 293 | create<ast::SintLiteral>(&i32, 2)); |
dan sinclair | bf0fff8 | 2020-04-07 12:56:24 +0000 | [diff] [blame] | 294 | |
Ben Clayton | d0dda25 | 2020-12-07 21:09:57 +0000 | [diff] [blame] | 295 | ast::ReturnStatement ret(Source{}, cond); |
dan sinclair | bf0fff8 | 2020-04-07 12:56:24 +0000 | [diff] [blame] | 296 | |
| 297 | EXPECT_TRUE(td()->DetermineResultType(&ret)); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 298 | ASSERT_NE(cond->result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 299 | EXPECT_TRUE(cond->result_type()->Is<ast::type::I32>()); |
dan sinclair | bf0fff8 | 2020-04-07 12:56:24 +0000 | [diff] [blame] | 300 | } |
| 301 | |
dan sinclair | 327ed1b | 2020-04-07 19:27:21 +0000 | [diff] [blame] | 302 | TEST_F(TypeDeterminerTest, Stmt_Return_WithoutValue) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 303 | ast::type::I32 i32; |
Ben Clayton | d0dda25 | 2020-12-07 21:09:57 +0000 | [diff] [blame] | 304 | ast::ReturnStatement ret(Source{}); |
dan sinclair | 327ed1b | 2020-04-07 19:27:21 +0000 | [diff] [blame] | 305 | EXPECT_TRUE(td()->DetermineResultType(&ret)); |
| 306 | } |
| 307 | |
dan sinclair | 18b3285 | 2020-04-07 12:56:45 +0000 | [diff] [blame] | 308 | TEST_F(TypeDeterminerTest, Stmt_Switch) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 309 | ast::type::I32 i32; |
| 310 | ast::type::F32 f32; |
dan sinclair | 18b3285 | 2020-04-07 12:56:45 +0000 | [diff] [blame] | 311 | |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 312 | auto* lhs = create<ast::ScalarConstructorExpression>( |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 313 | create<ast::SintLiteral>(&i32, 2)); |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 314 | auto* rhs = create<ast::ScalarConstructorExpression>( |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 315 | create<ast::FloatLiteral>(&f32, 2.3f)); |
dan sinclair | 18b3285 | 2020-04-07 12:56:45 +0000 | [diff] [blame] | 316 | |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 317 | auto* body = create<ast::BlockStatement>(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 318 | body->append(create<ast::AssignmentStatement>(lhs, rhs)); |
dan sinclair | 18b3285 | 2020-04-07 12:56:45 +0000 | [diff] [blame] | 319 | |
dan sinclair | 1aadbd4 | 2020-06-01 16:56:46 +0000 | [diff] [blame] | 320 | ast::CaseSelectorList lit; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 321 | lit.push_back(create<ast::SintLiteral>(&i32, 3)); |
dan sinclair | 1aadbd4 | 2020-06-01 16:56:46 +0000 | [diff] [blame] | 322 | |
dan sinclair | 18b3285 | 2020-04-07 12:56:45 +0000 | [diff] [blame] | 323 | ast::CaseStatementList cases; |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 324 | cases.push_back(create<ast::CaseStatement>(lit, body)); |
dan sinclair | 18b3285 | 2020-04-07 12:56:45 +0000 | [diff] [blame] | 325 | |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 326 | ast::SwitchStatement stmt(create<ast::ScalarConstructorExpression>( |
| 327 | create<ast::SintLiteral>(&i32, 2)), |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 328 | cases); |
dan sinclair | 18b3285 | 2020-04-07 12:56:45 +0000 | [diff] [blame] | 329 | |
| 330 | EXPECT_TRUE(td()->DetermineResultType(&stmt)) << td()->error(); |
| 331 | ASSERT_NE(stmt.condition()->result_type(), nullptr); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 332 | ASSERT_NE(lhs->result_type(), nullptr); |
| 333 | ASSERT_NE(rhs->result_type(), nullptr); |
dan sinclair | 18b3285 | 2020-04-07 12:56:45 +0000 | [diff] [blame] | 334 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 335 | EXPECT_TRUE(stmt.condition()->result_type()->Is<ast::type::I32>()); |
| 336 | EXPECT_TRUE(lhs->result_type()->Is<ast::type::I32>()); |
| 337 | EXPECT_TRUE(rhs->result_type()->Is<ast::type::F32>()); |
dan sinclair | 18b3285 | 2020-04-07 12:56:45 +0000 | [diff] [blame] | 338 | } |
| 339 | |
dan sinclair | 50080b7 | 2020-07-21 13:42:13 +0000 | [diff] [blame] | 340 | TEST_F(TypeDeterminerTest, Stmt_Call) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 341 | ast::type::F32 f32; |
dan sinclair | 50080b7 | 2020-07-21 13:42:13 +0000 | [diff] [blame] | 342 | |
| 343 | ast::VariableList params; |
dan sinclair | a41132f | 2020-12-11 18:24:53 +0000 | [diff] [blame] | 344 | auto* func = create<ast::Function>( |
| 345 | Source{}, mod->RegisterSymbol("my_func"), "my_func", params, &f32, |
| 346 | create<ast::BlockStatement>(), ast::FunctionDecorationList{}); |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 347 | mod->AddFunction(func); |
dan sinclair | 50080b7 | 2020-07-21 13:42:13 +0000 | [diff] [blame] | 348 | |
| 349 | // Register the function |
| 350 | EXPECT_TRUE(td()->Determine()); |
| 351 | |
| 352 | ast::ExpressionList call_params; |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 353 | auto* expr = create<ast::CallExpression>( |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 354 | create<ast::IdentifierExpression>(mod->RegisterSymbol("my_func"), |
| 355 | "my_func"), |
| 356 | call_params); |
dan sinclair | 50080b7 | 2020-07-21 13:42:13 +0000 | [diff] [blame] | 357 | |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 358 | ast::CallStatement call(expr); |
dan sinclair | 50080b7 | 2020-07-21 13:42:13 +0000 | [diff] [blame] | 359 | EXPECT_TRUE(td()->DetermineResultType(&call)); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 360 | ASSERT_NE(expr->result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 361 | EXPECT_TRUE(expr->result_type()->Is<ast::type::F32>()); |
dan sinclair | 50080b7 | 2020-07-21 13:42:13 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Sarah Mashayekhi | 844f632 | 2020-08-18 02:10:03 +0000 | [diff] [blame] | 364 | TEST_F(TypeDeterminerTest, Stmt_Call_undeclared) { |
| 365 | // fn main() -> void {func(); return; } |
| 366 | // fn func() -> void { return; } |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 367 | ast::type::F32 f32; |
Sarah Mashayekhi | 844f632 | 2020-08-18 02:10:03 +0000 | [diff] [blame] | 368 | ast::ExpressionList call_params; |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 369 | auto* call_expr = create<ast::CallExpression>( |
| 370 | create<ast::IdentifierExpression>(Source{Source::Location{12, 34}}, |
| 371 | mod->RegisterSymbol("func"), "func"), |
| 372 | call_params); |
Sarah Mashayekhi | 844f632 | 2020-08-18 02:10:03 +0000 | [diff] [blame] | 373 | ast::VariableList params0; |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 374 | auto* main_body = create<ast::BlockStatement>(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 375 | main_body->append(create<ast::CallStatement>(call_expr)); |
Ben Clayton | d0dda25 | 2020-12-07 21:09:57 +0000 | [diff] [blame] | 376 | main_body->append(create<ast::ReturnStatement>(Source{})); |
dan sinclair | a41132f | 2020-12-11 18:24:53 +0000 | [diff] [blame] | 377 | auto* func_main = create<ast::Function>(Source{}, mod->RegisterSymbol("main"), |
| 378 | "main", params0, &f32, main_body, |
| 379 | ast::FunctionDecorationList{}); |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 380 | mod->AddFunction(func_main); |
Sarah Mashayekhi | 844f632 | 2020-08-18 02:10:03 +0000 | [diff] [blame] | 381 | |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 382 | auto* body = create<ast::BlockStatement>(); |
Ben Clayton | d0dda25 | 2020-12-07 21:09:57 +0000 | [diff] [blame] | 383 | body->append(create<ast::ReturnStatement>(Source{})); |
dan sinclair | a41132f | 2020-12-11 18:24:53 +0000 | [diff] [blame] | 384 | auto* func = |
| 385 | create<ast::Function>(Source{}, mod->RegisterSymbol("func"), "func", |
| 386 | params0, &f32, body, ast::FunctionDecorationList{}); |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 387 | mod->AddFunction(func); |
Sarah Mashayekhi | 844f632 | 2020-08-18 02:10:03 +0000 | [diff] [blame] | 388 | |
| 389 | EXPECT_FALSE(td()->Determine()) << td()->error(); |
| 390 | EXPECT_EQ(td()->error(), |
dan sinclair | ff267ca | 2020-10-14 18:26:31 +0000 | [diff] [blame] | 391 | "12:34: v-0006: identifier must be declared before use: func"); |
Sarah Mashayekhi | 844f632 | 2020-08-18 02:10:03 +0000 | [diff] [blame] | 392 | } |
| 393 | |
dan sinclair | ca893e3 | 2020-04-07 12:57:12 +0000 | [diff] [blame] | 394 | TEST_F(TypeDeterminerTest, Stmt_VariableDecl) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 395 | ast::type::I32 i32; |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 396 | auto* var = create<ast::Variable>( |
| 397 | Source{}, // source |
| 398 | "my_var", // name |
| 399 | ast::StorageClass::kNone, // storage_class |
| 400 | &i32, // type |
| 401 | false, // is_const |
| 402 | create<ast::ScalarConstructorExpression>( |
| 403 | create<ast::SintLiteral>(&i32, 2)), // constructor |
| 404 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 405 | auto* init = var->constructor(); |
dan sinclair | ca893e3 | 2020-04-07 12:57:12 +0000 | [diff] [blame] | 406 | |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 407 | ast::VariableDeclStatement decl(var); |
dan sinclair | ca893e3 | 2020-04-07 12:57:12 +0000 | [diff] [blame] | 408 | |
| 409 | EXPECT_TRUE(td()->DetermineResultType(&decl)); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 410 | ASSERT_NE(init->result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 411 | EXPECT_TRUE(init->result_type()->Is<ast::type::I32>()); |
dan sinclair | ca893e3 | 2020-04-07 12:57:12 +0000 | [diff] [blame] | 412 | } |
| 413 | |
dan sinclair | 7be237a | 2020-06-15 20:55:09 +0000 | [diff] [blame] | 414 | TEST_F(TypeDeterminerTest, Stmt_VariableDecl_ModuleScope) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 415 | ast::type::I32 i32; |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 416 | auto* var = create<ast::Variable>( |
| 417 | Source{}, // source |
| 418 | "my_var", // name |
| 419 | ast::StorageClass::kNone, // storage_class |
| 420 | &i32, // type |
| 421 | false, // is_const |
| 422 | create<ast::ScalarConstructorExpression>( |
| 423 | create<ast::SintLiteral>(&i32, 2)), // constructor |
| 424 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 425 | auto* init = var->constructor(); |
dan sinclair | 7be237a | 2020-06-15 20:55:09 +0000 | [diff] [blame] | 426 | |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 427 | mod->AddGlobalVariable(var); |
dan sinclair | 7be237a | 2020-06-15 20:55:09 +0000 | [diff] [blame] | 428 | |
| 429 | EXPECT_TRUE(td()->Determine()); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 430 | ASSERT_NE(init->result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 431 | EXPECT_TRUE(init->result_type()->Is<ast::type::I32>()); |
dan sinclair | 7be237a | 2020-06-15 20:55:09 +0000 | [diff] [blame] | 432 | } |
| 433 | |
dan sinclair | 7456f42 | 2020-04-08 19:58:35 +0000 | [diff] [blame] | 434 | TEST_F(TypeDeterminerTest, Expr_Error_Unknown) { |
| 435 | FakeExpr e; |
Ben Clayton | fc5a9cf | 2020-11-02 15:07:47 +0000 | [diff] [blame] | 436 | e.set_source(Source{Source::Location{2, 30}}); |
dan sinclair | 7456f42 | 2020-04-08 19:58:35 +0000 | [diff] [blame] | 437 | |
| 438 | EXPECT_FALSE(td()->DetermineResultType(&e)); |
| 439 | EXPECT_EQ(td()->error(), "2:30: unknown expression for type determination"); |
| 440 | } |
| 441 | |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 442 | TEST_F(TypeDeterminerTest, Expr_ArrayAccessor_Array) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 443 | ast::type::I32 i32; |
| 444 | ast::type::F32 f32; |
Ben Clayton | 8b0ffe9 | 2020-12-07 22:19:27 +0000 | [diff] [blame] | 445 | ast::type::Array ary(&f32, 3, ast::ArrayDecorationList{}); |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 446 | |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 447 | auto* idx = create<ast::ScalarConstructorExpression>( |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 448 | create<ast::SintLiteral>(&i32, 2)); |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 449 | auto* var = |
| 450 | create<ast::Variable>(Source{}, // source |
| 451 | "my_var", // name |
| 452 | ast::StorageClass::kFunction, // storage_class |
| 453 | &ary, // type |
| 454 | false, // is_const |
| 455 | nullptr, // constructor |
| 456 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 457 | mod->AddGlobalVariable(var); |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 458 | |
| 459 | // Register the global |
| 460 | EXPECT_TRUE(td()->Determine()); |
| 461 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 462 | ast::ArrayAccessorExpression acc(create<ast::IdentifierExpression>( |
| 463 | mod->RegisterSymbol("my_var"), "my_var"), |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 464 | idx); |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 465 | EXPECT_TRUE(td()->DetermineResultType(&acc)); |
| 466 | ASSERT_NE(acc.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 467 | ASSERT_TRUE(acc.result_type()->Is<ast::type::Pointer>()); |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 468 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 469 | auto* ptr = acc.result_type()->As<ast::type::Pointer>(); |
| 470 | EXPECT_TRUE(ptr->type()->Is<ast::type::F32>()); |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 471 | } |
| 472 | |
David Neto | 9c88ea5 | 2020-06-22 20:33:12 +0000 | [diff] [blame] | 473 | TEST_F(TypeDeterminerTest, Expr_ArrayAccessor_Alias_Array) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 474 | ast::type::I32 i32; |
| 475 | ast::type::F32 f32; |
Ben Clayton | 8b0ffe9 | 2020-12-07 22:19:27 +0000 | [diff] [blame] | 476 | ast::type::Array ary(&f32, 3, ast::ArrayDecorationList{}); |
dan sinclair | 4226b6a | 2020-12-11 19:35:03 +0000 | [diff] [blame^] | 477 | ast::type::Alias aary(mod->RegisterSymbol("myarrty"), "myarrty", &ary); |
David Neto | 9c88ea5 | 2020-06-22 20:33:12 +0000 | [diff] [blame] | 478 | |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 479 | auto* idx = create<ast::ScalarConstructorExpression>( |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 480 | create<ast::SintLiteral>(&i32, 2)); |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 481 | auto* var = |
| 482 | create<ast::Variable>(Source{}, // source |
| 483 | "my_var", // name |
| 484 | ast::StorageClass::kFunction, // storage_class |
| 485 | &aary, // type |
| 486 | false, // is_const |
| 487 | nullptr, // constructor |
| 488 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 489 | mod->AddGlobalVariable(var); |
David Neto | 9c88ea5 | 2020-06-22 20:33:12 +0000 | [diff] [blame] | 490 | |
| 491 | // Register the global |
| 492 | EXPECT_TRUE(td()->Determine()); |
| 493 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 494 | ast::ArrayAccessorExpression acc(create<ast::IdentifierExpression>( |
| 495 | mod->RegisterSymbol("my_var"), "my_var"), |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 496 | idx); |
David Neto | 9c88ea5 | 2020-06-22 20:33:12 +0000 | [diff] [blame] | 497 | EXPECT_TRUE(td()->DetermineResultType(&acc)); |
| 498 | ASSERT_NE(acc.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 499 | ASSERT_TRUE(acc.result_type()->Is<ast::type::Pointer>()); |
David Neto | 9c88ea5 | 2020-06-22 20:33:12 +0000 | [diff] [blame] | 500 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 501 | auto* ptr = acc.result_type()->As<ast::type::Pointer>(); |
| 502 | EXPECT_TRUE(ptr->type()->Is<ast::type::F32>()); |
David Neto | 9c88ea5 | 2020-06-22 20:33:12 +0000 | [diff] [blame] | 503 | } |
| 504 | |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 505 | TEST_F(TypeDeterminerTest, Expr_ArrayAccessor_Array_Constant) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 506 | ast::type::I32 i32; |
| 507 | ast::type::F32 f32; |
Ben Clayton | 8b0ffe9 | 2020-12-07 22:19:27 +0000 | [diff] [blame] | 508 | ast::type::Array ary(&f32, 3, ast::ArrayDecorationList{}); |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 509 | |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 510 | auto* idx = create<ast::ScalarConstructorExpression>( |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 511 | create<ast::SintLiteral>(&i32, 2)); |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 512 | auto* var = |
| 513 | create<ast::Variable>(Source{}, // source |
| 514 | "my_var", // name |
| 515 | ast::StorageClass::kFunction, // storage_class |
| 516 | &ary, // type |
| 517 | true, // is_const |
| 518 | nullptr, // constructor |
| 519 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 520 | mod->AddGlobalVariable(var); |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 521 | |
| 522 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 523 | EXPECT_TRUE(td()->Determine()); |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 524 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 525 | ast::ArrayAccessorExpression acc(create<ast::IdentifierExpression>( |
| 526 | mod->RegisterSymbol("my_var"), "my_var"), |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 527 | idx); |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 528 | EXPECT_TRUE(td()->DetermineResultType(&acc)); |
| 529 | ASSERT_NE(acc.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 530 | EXPECT_TRUE(acc.result_type()->Is<ast::type::F32>()) |
Ben Clayton | 75f39be | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 531 | << acc.result_type()->type_name(); |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | TEST_F(TypeDeterminerTest, Expr_ArrayAccessor_Matrix) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 535 | ast::type::I32 i32; |
| 536 | ast::type::F32 f32; |
| 537 | ast::type::Matrix mat(&f32, 3, 2); |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 538 | |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 539 | auto* idx = create<ast::ScalarConstructorExpression>( |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 540 | create<ast::SintLiteral>(&i32, 2)); |
Ben Clayton | 321e5a9 | 2020-12-07 21:08:07 +0000 | [diff] [blame] | 541 | auto* var = |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 542 | create<ast::Variable>(Source{}, // source |
| 543 | "my_var", // name |
| 544 | ast::StorageClass::kNone, // storage_class |
| 545 | &mat, // type |
| 546 | false, // is_const |
| 547 | nullptr, // constructor |
| 548 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 549 | mod->AddGlobalVariable(var); |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 550 | |
| 551 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 552 | EXPECT_TRUE(td()->Determine()); |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 553 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 554 | ast::ArrayAccessorExpression acc(create<ast::IdentifierExpression>( |
| 555 | mod->RegisterSymbol("my_var"), "my_var"), |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 556 | idx); |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 557 | EXPECT_TRUE(td()->DetermineResultType(&acc)); |
| 558 | ASSERT_NE(acc.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 559 | ASSERT_TRUE(acc.result_type()->Is<ast::type::Pointer>()); |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 560 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 561 | auto* ptr = acc.result_type()->As<ast::type::Pointer>(); |
| 562 | ASSERT_TRUE(ptr->type()->Is<ast::type::Vector>()); |
| 563 | EXPECT_EQ(ptr->type()->As<ast::type::Vector>()->size(), 3u); |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | TEST_F(TypeDeterminerTest, Expr_ArrayAccessor_Matrix_BothDimensions) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 567 | ast::type::I32 i32; |
| 568 | ast::type::F32 f32; |
| 569 | ast::type::Matrix mat(&f32, 3, 2); |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 570 | |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 571 | auto* idx1 = create<ast::ScalarConstructorExpression>( |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 572 | create<ast::SintLiteral>(&i32, 2)); |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 573 | auto* idx2 = create<ast::ScalarConstructorExpression>( |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 574 | create<ast::SintLiteral>(&i32, 1)); |
Ben Clayton | 321e5a9 | 2020-12-07 21:08:07 +0000 | [diff] [blame] | 575 | auto* var = |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 576 | create<ast::Variable>(Source{}, // source |
| 577 | "my_var", // name |
| 578 | ast::StorageClass::kNone, // storage_class |
| 579 | &mat, // type |
| 580 | false, // is_const |
| 581 | nullptr, // constructor |
| 582 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 583 | mod->AddGlobalVariable(var); |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 584 | |
| 585 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 586 | EXPECT_TRUE(td()->Determine()); |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 587 | |
| 588 | ast::ArrayAccessorExpression acc( |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 589 | create<ast::ArrayAccessorExpression>( |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 590 | create<ast::IdentifierExpression>(mod->RegisterSymbol("my_var"), |
| 591 | "my_var"), |
| 592 | idx1), |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 593 | idx2); |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 594 | |
| 595 | EXPECT_TRUE(td()->DetermineResultType(&acc)); |
| 596 | ASSERT_NE(acc.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 597 | ASSERT_TRUE(acc.result_type()->Is<ast::type::Pointer>()); |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 598 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 599 | auto* ptr = acc.result_type()->As<ast::type::Pointer>(); |
| 600 | EXPECT_TRUE(ptr->type()->Is<ast::type::F32>()); |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | TEST_F(TypeDeterminerTest, Expr_ArrayAccessor_Vector) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 604 | ast::type::I32 i32; |
| 605 | ast::type::F32 f32; |
| 606 | ast::type::Vector vec(&f32, 3); |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 607 | |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 608 | auto* idx = create<ast::ScalarConstructorExpression>( |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 609 | create<ast::SintLiteral>(&i32, 2)); |
Ben Clayton | 321e5a9 | 2020-12-07 21:08:07 +0000 | [diff] [blame] | 610 | auto* var = |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 611 | create<ast::Variable>(Source{}, // source |
| 612 | "my_var", // name |
| 613 | ast::StorageClass::kNone, // storage_class |
| 614 | &vec, // type |
| 615 | false, // is_const |
| 616 | nullptr, // constructor |
| 617 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 618 | mod->AddGlobalVariable(var); |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 619 | |
| 620 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 621 | EXPECT_TRUE(td()->Determine()); |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 622 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 623 | ast::ArrayAccessorExpression acc(create<ast::IdentifierExpression>( |
| 624 | mod->RegisterSymbol("my_var"), "my_var"), |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 625 | idx); |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 626 | EXPECT_TRUE(td()->DetermineResultType(&acc)); |
| 627 | ASSERT_NE(acc.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 628 | ASSERT_TRUE(acc.result_type()->Is<ast::type::Pointer>()); |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 629 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 630 | auto* ptr = acc.result_type()->As<ast::type::Pointer>(); |
| 631 | EXPECT_TRUE(ptr->type()->Is<ast::type::F32>()); |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 632 | } |
| 633 | |
dan sinclair | a7d498e | 2020-09-22 22:07:13 +0000 | [diff] [blame] | 634 | TEST_F(TypeDeterminerTest, Expr_Bitcast) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 635 | ast::type::F32 f32; |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 636 | ast::BitcastExpression bitcast( |
| 637 | &f32, |
| 638 | create<ast::IdentifierExpression>(mod->RegisterSymbol("name"), "name")); |
dan sinclair | a01777c | 2020-04-07 12:57:52 +0000 | [diff] [blame] | 639 | |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 640 | ast::Variable v(Source{}, "name", ast::StorageClass::kPrivate, &f32, false, |
| 641 | nullptr, ast::VariableDecorationList{}); |
dan sinclair | ff267ca | 2020-10-14 18:26:31 +0000 | [diff] [blame] | 642 | td()->RegisterVariableForTesting(&v); |
| 643 | |
dan sinclair | a7d498e | 2020-09-22 22:07:13 +0000 | [diff] [blame] | 644 | EXPECT_TRUE(td()->DetermineResultType(&bitcast)); |
| 645 | ASSERT_NE(bitcast.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 646 | EXPECT_TRUE(bitcast.result_type()->Is<ast::type::F32>()); |
dan sinclair | a01777c | 2020-04-07 12:57:52 +0000 | [diff] [blame] | 647 | } |
| 648 | |
dan sinclair | 3ca8746 | 2020-04-07 16:41:10 +0000 | [diff] [blame] | 649 | TEST_F(TypeDeterminerTest, Expr_Call) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 650 | ast::type::F32 f32; |
dan sinclair | 3ca8746 | 2020-04-07 16:41:10 +0000 | [diff] [blame] | 651 | |
| 652 | ast::VariableList params; |
dan sinclair | a41132f | 2020-12-11 18:24:53 +0000 | [diff] [blame] | 653 | auto* func = create<ast::Function>( |
| 654 | Source{}, mod->RegisterSymbol("my_func"), "my_func", params, &f32, |
| 655 | create<ast::BlockStatement>(), ast::FunctionDecorationList{}); |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 656 | mod->AddFunction(func); |
dan sinclair | 3ca8746 | 2020-04-07 16:41:10 +0000 | [diff] [blame] | 657 | |
| 658 | // Register the function |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 659 | EXPECT_TRUE(td()->Determine()); |
dan sinclair | 3ca8746 | 2020-04-07 16:41:10 +0000 | [diff] [blame] | 660 | |
| 661 | ast::ExpressionList call_params; |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 662 | ast::CallExpression call(create<ast::IdentifierExpression>( |
| 663 | mod->RegisterSymbol("my_func"), "my_func"), |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 664 | call_params); |
dan sinclair | 3ca8746 | 2020-04-07 16:41:10 +0000 | [diff] [blame] | 665 | EXPECT_TRUE(td()->DetermineResultType(&call)); |
| 666 | ASSERT_NE(call.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 667 | EXPECT_TRUE(call.result_type()->Is<ast::type::F32>()); |
dan sinclair | 3ca8746 | 2020-04-07 16:41:10 +0000 | [diff] [blame] | 668 | } |
| 669 | |
dan sinclair | ccb52dc | 2020-04-20 14:18:54 +0000 | [diff] [blame] | 670 | TEST_F(TypeDeterminerTest, Expr_Call_WithParams) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 671 | ast::type::F32 f32; |
dan sinclair | ccb52dc | 2020-04-20 14:18:54 +0000 | [diff] [blame] | 672 | |
| 673 | ast::VariableList params; |
dan sinclair | a41132f | 2020-12-11 18:24:53 +0000 | [diff] [blame] | 674 | auto* func = create<ast::Function>( |
| 675 | Source{}, mod->RegisterSymbol("my_func"), "my_func", params, &f32, |
| 676 | create<ast::BlockStatement>(), ast::FunctionDecorationList{}); |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 677 | mod->AddFunction(func); |
dan sinclair | ccb52dc | 2020-04-20 14:18:54 +0000 | [diff] [blame] | 678 | |
| 679 | // Register the function |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 680 | EXPECT_TRUE(td()->Determine()); |
dan sinclair | ccb52dc | 2020-04-20 14:18:54 +0000 | [diff] [blame] | 681 | |
| 682 | ast::ExpressionList call_params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 683 | call_params.push_back(create<ast::ScalarConstructorExpression>( |
| 684 | create<ast::FloatLiteral>(&f32, 2.4))); |
dan sinclair | ccb52dc | 2020-04-20 14:18:54 +0000 | [diff] [blame] | 685 | |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 686 | auto* param = call_params.back(); |
dan sinclair | ccb52dc | 2020-04-20 14:18:54 +0000 | [diff] [blame] | 687 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 688 | ast::CallExpression call(create<ast::IdentifierExpression>( |
| 689 | mod->RegisterSymbol("my_func"), "my_func"), |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 690 | call_params); |
dan sinclair | ccb52dc | 2020-04-20 14:18:54 +0000 | [diff] [blame] | 691 | EXPECT_TRUE(td()->DetermineResultType(&call)); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 692 | ASSERT_NE(param->result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 693 | EXPECT_TRUE(param->result_type()->Is<ast::type::F32>()); |
dan sinclair | ccb52dc | 2020-04-20 14:18:54 +0000 | [diff] [blame] | 694 | } |
| 695 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 696 | TEST_F(TypeDeterminerTest, Expr_Call_Intrinsic) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 697 | ast::type::F32 f32; |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 698 | |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 699 | // Register the function |
| 700 | EXPECT_TRUE(td()->Determine()); |
| 701 | |
| 702 | ast::ExpressionList call_params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 703 | call_params.push_back(create<ast::ScalarConstructorExpression>( |
| 704 | create<ast::FloatLiteral>(&f32, 2.4))); |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 705 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 706 | ast::CallExpression call( |
| 707 | create<ast::IdentifierExpression>(mod->RegisterSymbol("round"), "round"), |
| 708 | call_params); |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 709 | |
| 710 | EXPECT_TRUE(td()->DetermineResultType(&call)); |
| 711 | ASSERT_NE(call.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 712 | EXPECT_TRUE(call.result_type()->Is<ast::type::F32>()); |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 713 | } |
| 714 | |
dan sinclair | 4e80795 | 2020-04-07 16:41:23 +0000 | [diff] [blame] | 715 | TEST_F(TypeDeterminerTest, Expr_Cast) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 716 | ast::type::F32 f32; |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 717 | |
| 718 | ast::ExpressionList params; |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 719 | params.push_back( |
| 720 | create<ast::IdentifierExpression>(mod->RegisterSymbol("name"), "name")); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 721 | ast::TypeConstructorExpression cast(&f32, params); |
dan sinclair | 4e80795 | 2020-04-07 16:41:23 +0000 | [diff] [blame] | 722 | |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 723 | ast::Variable v(Source{}, "name", ast::StorageClass::kPrivate, &f32, false, |
| 724 | nullptr, ast::VariableDecorationList{}); |
dan sinclair | ff267ca | 2020-10-14 18:26:31 +0000 | [diff] [blame] | 725 | td()->RegisterVariableForTesting(&v); |
| 726 | |
dan sinclair | 4e80795 | 2020-04-07 16:41:23 +0000 | [diff] [blame] | 727 | EXPECT_TRUE(td()->DetermineResultType(&cast)); |
| 728 | ASSERT_NE(cast.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 729 | EXPECT_TRUE(cast.result_type()->Is<ast::type::F32>()); |
dan sinclair | 4e80795 | 2020-04-07 16:41:23 +0000 | [diff] [blame] | 730 | } |
| 731 | |
dan sinclair | b7edc4c | 2020-04-07 12:46:30 +0000 | [diff] [blame] | 732 | TEST_F(TypeDeterminerTest, Expr_Constructor_Scalar) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 733 | ast::type::F32 f32; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 734 | ast::ScalarConstructorExpression s(create<ast::FloatLiteral>(&f32, 1.0f)); |
dan sinclair | b7edc4c | 2020-04-07 12:46:30 +0000 | [diff] [blame] | 735 | |
| 736 | EXPECT_TRUE(td()->DetermineResultType(&s)); |
| 737 | ASSERT_NE(s.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 738 | EXPECT_TRUE(s.result_type()->Is<ast::type::F32>()); |
dan sinclair | b7edc4c | 2020-04-07 12:46:30 +0000 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | TEST_F(TypeDeterminerTest, Expr_Constructor_Type) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 742 | ast::type::F32 f32; |
| 743 | ast::type::Vector vec(&f32, 3); |
dan sinclair | b7edc4c | 2020-04-07 12:46:30 +0000 | [diff] [blame] | 744 | |
| 745 | ast::ExpressionList vals; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 746 | vals.push_back(create<ast::ScalarConstructorExpression>( |
| 747 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 748 | vals.push_back(create<ast::ScalarConstructorExpression>( |
| 749 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 750 | vals.push_back(create<ast::ScalarConstructorExpression>( |
| 751 | create<ast::FloatLiteral>(&f32, 3.0f))); |
dan sinclair | b7edc4c | 2020-04-07 12:46:30 +0000 | [diff] [blame] | 752 | |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 753 | ast::TypeConstructorExpression tc(&vec, vals); |
dan sinclair | b7edc4c | 2020-04-07 12:46:30 +0000 | [diff] [blame] | 754 | |
| 755 | EXPECT_TRUE(td()->DetermineResultType(&tc)); |
| 756 | ASSERT_NE(tc.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 757 | ASSERT_TRUE(tc.result_type()->Is<ast::type::Vector>()); |
| 758 | EXPECT_TRUE( |
| 759 | tc.result_type()->As<ast::type::Vector>()->type()->Is<ast::type::F32>()); |
| 760 | EXPECT_EQ(tc.result_type()->As<ast::type::Vector>()->size(), 3u); |
dan sinclair | b7edc4c | 2020-04-07 12:46:30 +0000 | [diff] [blame] | 761 | } |
| 762 | |
dan sinclair | cab0e73 | 2020-04-07 12:57:27 +0000 | [diff] [blame] | 763 | TEST_F(TypeDeterminerTest, Expr_Identifier_GlobalVariable) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 764 | ast::type::F32 f32; |
Ben Clayton | 321e5a9 | 2020-12-07 21:08:07 +0000 | [diff] [blame] | 765 | auto* var = |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 766 | create<ast::Variable>(Source{}, // source |
| 767 | "my_var", // name |
| 768 | ast::StorageClass::kNone, // storage_class |
| 769 | &f32, // type |
| 770 | false, // is_const |
| 771 | nullptr, // constructor |
| 772 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 773 | mod->AddGlobalVariable(var); |
dan sinclair | cab0e73 | 2020-04-07 12:57:27 +0000 | [diff] [blame] | 774 | |
| 775 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 776 | EXPECT_TRUE(td()->Determine()); |
dan sinclair | cab0e73 | 2020-04-07 12:57:27 +0000 | [diff] [blame] | 777 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 778 | ast::IdentifierExpression ident(mod->RegisterSymbol("my_var"), "my_var"); |
dan sinclair | cab0e73 | 2020-04-07 12:57:27 +0000 | [diff] [blame] | 779 | EXPECT_TRUE(td()->DetermineResultType(&ident)); |
| 780 | ASSERT_NE(ident.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 781 | EXPECT_TRUE(ident.result_type()->Is<ast::type::Pointer>()); |
Ben Clayton | c52f421 | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 782 | EXPECT_TRUE(ident.result_type() |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 783 | ->As<ast::type::Pointer>() |
Ben Clayton | c52f421 | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 784 | ->type() |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 785 | ->Is<ast::type::F32>()); |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | TEST_F(TypeDeterminerTest, Expr_Identifier_GlobalConstant) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 789 | ast::type::F32 f32; |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 790 | mod->AddGlobalVariable( |
| 791 | create<ast::Variable>(Source{}, // source |
| 792 | "my_var", // name |
| 793 | ast::StorageClass::kNone, // storage_class |
| 794 | &f32, // type |
| 795 | true, // is_const |
| 796 | nullptr, // constructor |
| 797 | ast::VariableDecorationList{})); // decorations |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 798 | |
| 799 | // Register the global |
| 800 | EXPECT_TRUE(td()->Determine()); |
| 801 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 802 | ast::IdentifierExpression ident(mod->RegisterSymbol("my_var"), "my_var"); |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 803 | EXPECT_TRUE(td()->DetermineResultType(&ident)); |
| 804 | ASSERT_NE(ident.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 805 | EXPECT_TRUE(ident.result_type()->Is<ast::type::F32>()); |
dan sinclair | cab0e73 | 2020-04-07 12:57:27 +0000 | [diff] [blame] | 806 | } |
| 807 | |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 808 | TEST_F(TypeDeterminerTest, Expr_Identifier_FunctionVariable_Const) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 809 | ast::type::F32 f32; |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 810 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 811 | auto* my_var = create<ast::IdentifierExpression>( |
| 812 | mod->RegisterSymbol("my_var"), "my_var"); |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 813 | |
Ben Clayton | 321e5a9 | 2020-12-07 21:08:07 +0000 | [diff] [blame] | 814 | auto* var = |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 815 | create<ast::Variable>(Source{}, // source |
| 816 | "my_var", // name |
| 817 | ast::StorageClass::kNone, // storage_class |
| 818 | &f32, // type |
| 819 | true, // is_const |
| 820 | nullptr, // constructor |
| 821 | ast::VariableDecorationList{}); // decorations |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 822 | |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 823 | auto* body = create<ast::BlockStatement>(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 824 | body->append(create<ast::VariableDeclStatement>(var)); |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 825 | body->append(create<ast::AssignmentStatement>( |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 826 | my_var, create<ast::IdentifierExpression>(mod->RegisterSymbol("my_var"), |
| 827 | "my_var"))); |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 828 | |
dan sinclair | a41132f | 2020-12-11 18:24:53 +0000 | [diff] [blame] | 829 | ast::Function f(Source{}, mod->RegisterSymbol("my_func"), "my_func", {}, &f32, |
| 830 | body, ast::FunctionDecorationList{}); |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 831 | |
| 832 | EXPECT_TRUE(td()->DetermineFunction(&f)); |
| 833 | |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 834 | ASSERT_NE(my_var->result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 835 | EXPECT_TRUE(my_var->result_type()->Is<ast::type::F32>()); |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 836 | } |
| 837 | |
dan sinclair | cab0e73 | 2020-04-07 12:57:27 +0000 | [diff] [blame] | 838 | TEST_F(TypeDeterminerTest, Expr_Identifier_FunctionVariable) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 839 | ast::type::F32 f32; |
dan sinclair | cab0e73 | 2020-04-07 12:57:27 +0000 | [diff] [blame] | 840 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 841 | auto* my_var = create<ast::IdentifierExpression>( |
| 842 | mod->RegisterSymbol("my_var"), "my_var"); |
dan sinclair | cab0e73 | 2020-04-07 12:57:27 +0000 | [diff] [blame] | 843 | |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 844 | auto* body = create<ast::BlockStatement>(); |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 845 | body->append(create<ast::VariableDeclStatement>( |
| 846 | create<ast::Variable>(Source{}, // source |
| 847 | "my_var", // name |
| 848 | ast::StorageClass::kNone, // storage_class |
| 849 | &f32, // type |
| 850 | false, // is_const |
| 851 | nullptr, // constructor |
| 852 | ast::VariableDecorationList{}))); // decorations |
dan sinclair | cab0e73 | 2020-04-07 12:57:27 +0000 | [diff] [blame] | 853 | |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 854 | body->append(create<ast::AssignmentStatement>( |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 855 | my_var, create<ast::IdentifierExpression>(mod->RegisterSymbol("my_var"), |
| 856 | "my_var"))); |
dan sinclair | cab0e73 | 2020-04-07 12:57:27 +0000 | [diff] [blame] | 857 | |
dan sinclair | a41132f | 2020-12-11 18:24:53 +0000 | [diff] [blame] | 858 | ast::Function f(Source{}, mod->RegisterSymbol("myfunc"), "my_func", {}, &f32, |
| 859 | body, ast::FunctionDecorationList{}); |
dan sinclair | cab0e73 | 2020-04-07 12:57:27 +0000 | [diff] [blame] | 860 | |
| 861 | EXPECT_TRUE(td()->DetermineFunction(&f)); |
| 862 | |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 863 | ASSERT_NE(my_var->result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 864 | EXPECT_TRUE(my_var->result_type()->Is<ast::type::Pointer>()); |
Ben Clayton | c52f421 | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 865 | EXPECT_TRUE(my_var->result_type() |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 866 | ->As<ast::type::Pointer>() |
Ben Clayton | c52f421 | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 867 | ->type() |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 868 | ->Is<ast::type::F32>()); |
dan sinclair | cab0e73 | 2020-04-07 12:57:27 +0000 | [diff] [blame] | 869 | } |
| 870 | |
dan sinclair | 5e98930 | 2020-09-16 21:20:36 +0000 | [diff] [blame] | 871 | TEST_F(TypeDeterminerTest, Expr_Identifier_Function_Ptr) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 872 | ast::type::F32 f32; |
| 873 | ast::type::Pointer ptr(&f32, ast::StorageClass::kFunction); |
dan sinclair | 5e98930 | 2020-09-16 21:20:36 +0000 | [diff] [blame] | 874 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 875 | auto* my_var = create<ast::IdentifierExpression>( |
| 876 | mod->RegisterSymbol("my_var"), "my_var"); |
dan sinclair | 5e98930 | 2020-09-16 21:20:36 +0000 | [diff] [blame] | 877 | |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 878 | auto* body = create<ast::BlockStatement>(); |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 879 | body->append(create<ast::VariableDeclStatement>( |
| 880 | create<ast::Variable>(Source{}, // source |
| 881 | "my_var", // name |
| 882 | ast::StorageClass::kNone, // storage_class |
| 883 | &ptr, // type |
| 884 | false, // is_const |
| 885 | nullptr, // constructor |
| 886 | ast::VariableDecorationList{}))); // decorations |
dan sinclair | 5e98930 | 2020-09-16 21:20:36 +0000 | [diff] [blame] | 887 | |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 888 | body->append(create<ast::AssignmentStatement>( |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 889 | my_var, create<ast::IdentifierExpression>(mod->RegisterSymbol("my_var"), |
| 890 | "my_var"))); |
dan sinclair | 5e98930 | 2020-09-16 21:20:36 +0000 | [diff] [blame] | 891 | |
dan sinclair | a41132f | 2020-12-11 18:24:53 +0000 | [diff] [blame] | 892 | ast::Function f(Source{}, mod->RegisterSymbol("my_func"), "my_func", {}, &f32, |
| 893 | body, ast::FunctionDecorationList{}); |
dan sinclair | 5e98930 | 2020-09-16 21:20:36 +0000 | [diff] [blame] | 894 | |
| 895 | EXPECT_TRUE(td()->DetermineFunction(&f)); |
| 896 | |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 897 | ASSERT_NE(my_var->result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 898 | EXPECT_TRUE(my_var->result_type()->Is<ast::type::Pointer>()); |
Ben Clayton | c52f421 | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 899 | EXPECT_TRUE(my_var->result_type() |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 900 | ->As<ast::type::Pointer>() |
Ben Clayton | c52f421 | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 901 | ->type() |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 902 | ->Is<ast::type::F32>()); |
dan sinclair | 5e98930 | 2020-09-16 21:20:36 +0000 | [diff] [blame] | 903 | } |
| 904 | |
dan sinclair | cab0e73 | 2020-04-07 12:57:27 +0000 | [diff] [blame] | 905 | TEST_F(TypeDeterminerTest, Expr_Identifier_Function) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 906 | ast::type::F32 f32; |
dan sinclair | cab0e73 | 2020-04-07 12:57:27 +0000 | [diff] [blame] | 907 | |
| 908 | ast::VariableList params; |
dan sinclair | a41132f | 2020-12-11 18:24:53 +0000 | [diff] [blame] | 909 | auto* func = create<ast::Function>( |
| 910 | Source{}, mod->RegisterSymbol("my_func"), "my_func", params, &f32, |
| 911 | create<ast::BlockStatement>(), ast::FunctionDecorationList{}); |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 912 | mod->AddFunction(func); |
dan sinclair | cab0e73 | 2020-04-07 12:57:27 +0000 | [diff] [blame] | 913 | |
| 914 | // Register the function |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 915 | EXPECT_TRUE(td()->Determine()); |
dan sinclair | cab0e73 | 2020-04-07 12:57:27 +0000 | [diff] [blame] | 916 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 917 | ast::IdentifierExpression ident(mod->RegisterSymbol("my_func"), "my_func"); |
dan sinclair | cab0e73 | 2020-04-07 12:57:27 +0000 | [diff] [blame] | 918 | EXPECT_TRUE(td()->DetermineResultType(&ident)); |
| 919 | ASSERT_NE(ident.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 920 | EXPECT_TRUE(ident.result_type()->Is<ast::type::F32>()); |
dan sinclair | cab0e73 | 2020-04-07 12:57:27 +0000 | [diff] [blame] | 921 | } |
| 922 | |
dan sinclair | ff267ca | 2020-10-14 18:26:31 +0000 | [diff] [blame] | 923 | TEST_F(TypeDeterminerTest, Expr_Identifier_Unknown) { |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 924 | ast::IdentifierExpression a(mod->RegisterSymbol("a"), "a"); |
dan sinclair | ff267ca | 2020-10-14 18:26:31 +0000 | [diff] [blame] | 925 | EXPECT_FALSE(td()->DetermineResultType(&a)); |
| 926 | } |
| 927 | |
dan sinclair | 13d2a3b | 2020-06-22 20:52:24 +0000 | [diff] [blame] | 928 | TEST_F(TypeDeterminerTest, Function_RegisterInputOutputVariables) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 929 | ast::type::F32 f32; |
dan sinclair | 13d2a3b | 2020-06-22 20:52:24 +0000 | [diff] [blame] | 930 | |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 931 | auto* in_var = |
| 932 | create<ast::Variable>(Source{}, // source |
| 933 | "in_var", // name |
| 934 | ast::StorageClass::kInput, // storage_class |
| 935 | &f32, // type |
| 936 | false, // is_const |
| 937 | nullptr, // constructor |
| 938 | ast::VariableDecorationList{}); // decorations |
| 939 | auto* out_var = |
| 940 | create<ast::Variable>(Source{}, // source |
| 941 | "out_var", // name |
| 942 | ast::StorageClass::kOutput, // storage_class |
| 943 | &f32, // type |
| 944 | false, // is_const |
| 945 | nullptr, // constructor |
| 946 | ast::VariableDecorationList{}); // decorations |
| 947 | auto* sb_var = |
| 948 | create<ast::Variable>(Source{}, // source |
| 949 | "sb_var", // name |
| 950 | ast::StorageClass::kStorageBuffer, // storage_class |
| 951 | &f32, // type |
| 952 | false, // is_const |
| 953 | nullptr, // constructor |
| 954 | ast::VariableDecorationList{}); // decorations |
| 955 | auto* wg_var = |
| 956 | create<ast::Variable>(Source{}, // source |
| 957 | "wg_var", // name |
| 958 | ast::StorageClass::kWorkgroup, // storage_class |
| 959 | &f32, // type |
| 960 | false, // is_const |
| 961 | nullptr, // constructor |
| 962 | ast::VariableDecorationList{}); // decorations |
| 963 | auto* priv_var = |
| 964 | create<ast::Variable>(Source{}, // source |
| 965 | "priv_var", // name |
| 966 | ast::StorageClass::kPrivate, // storage_class |
| 967 | &f32, // type |
| 968 | false, // is_const |
| 969 | nullptr, // constructor |
| 970 | ast::VariableDecorationList{}); // decorations |
dan sinclair | 13d2a3b | 2020-06-22 20:52:24 +0000 | [diff] [blame] | 971 | |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 972 | mod->AddGlobalVariable(in_var); |
| 973 | mod->AddGlobalVariable(out_var); |
| 974 | mod->AddGlobalVariable(sb_var); |
| 975 | mod->AddGlobalVariable(wg_var); |
| 976 | mod->AddGlobalVariable(priv_var); |
dan sinclair | 13d2a3b | 2020-06-22 20:52:24 +0000 | [diff] [blame] | 977 | |
| 978 | ast::VariableList params; |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 979 | auto* body = create<ast::BlockStatement>(); |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 980 | body->append(create<ast::AssignmentStatement>( |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 981 | create<ast::IdentifierExpression>(mod->RegisterSymbol("out_var"), |
| 982 | "out_var"), |
| 983 | create<ast::IdentifierExpression>(mod->RegisterSymbol("in_var"), |
| 984 | "in_var"))); |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 985 | body->append(create<ast::AssignmentStatement>( |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 986 | create<ast::IdentifierExpression>(mod->RegisterSymbol("wg_var"), |
| 987 | "wg_var"), |
| 988 | create<ast::IdentifierExpression>(mod->RegisterSymbol("wg_var"), |
| 989 | "wg_var"))); |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 990 | body->append(create<ast::AssignmentStatement>( |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 991 | create<ast::IdentifierExpression>(mod->RegisterSymbol("sb_var"), |
| 992 | "sb_var"), |
| 993 | create<ast::IdentifierExpression>(mod->RegisterSymbol("sb_var"), |
| 994 | "sb_var"))); |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 995 | body->append(create<ast::AssignmentStatement>( |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 996 | create<ast::IdentifierExpression>(mod->RegisterSymbol("priv_var"), |
| 997 | "priv_var"), |
| 998 | create<ast::IdentifierExpression>(mod->RegisterSymbol("priv_var"), |
| 999 | "priv_var"))); |
dan sinclair | a41132f | 2020-12-11 18:24:53 +0000 | [diff] [blame] | 1000 | auto* func = |
| 1001 | create<ast::Function>(Source{}, mod->RegisterSymbol("my_func"), "my_func", |
| 1002 | params, &f32, body, ast::FunctionDecorationList{}); |
dan sinclair | 13d2a3b | 2020-06-22 20:52:24 +0000 | [diff] [blame] | 1003 | |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 1004 | mod->AddFunction(func); |
dan sinclair | 13d2a3b | 2020-06-22 20:52:24 +0000 | [diff] [blame] | 1005 | |
| 1006 | // Register the function |
| 1007 | EXPECT_TRUE(td()->Determine()); |
| 1008 | |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 1009 | const auto& vars = func->referenced_module_variables(); |
Ryan Harrison | 9a452c1 | 2020-06-23 16:38:47 +0000 | [diff] [blame] | 1010 | ASSERT_EQ(vars.size(), 5u); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 1011 | EXPECT_EQ(vars[0], out_var); |
| 1012 | EXPECT_EQ(vars[1], in_var); |
| 1013 | EXPECT_EQ(vars[2], wg_var); |
| 1014 | EXPECT_EQ(vars[3], sb_var); |
| 1015 | EXPECT_EQ(vars[4], priv_var); |
dan sinclair | 13d2a3b | 2020-06-22 20:52:24 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
dan sinclair | de2dd68 | 2020-07-14 20:37:28 +0000 | [diff] [blame] | 1018 | TEST_F(TypeDeterminerTest, Function_RegisterInputOutputVariables_SubFunction) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1019 | ast::type::F32 f32; |
dan sinclair | de2dd68 | 2020-07-14 20:37:28 +0000 | [diff] [blame] | 1020 | |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 1021 | auto* in_var = |
| 1022 | create<ast::Variable>(Source{}, // source |
| 1023 | "in_var", // name |
| 1024 | ast::StorageClass::kInput, // storage_class |
| 1025 | &f32, // type |
| 1026 | false, // is_const |
| 1027 | nullptr, // constructor |
| 1028 | ast::VariableDecorationList{}); // decorations |
| 1029 | auto* out_var = |
| 1030 | create<ast::Variable>(Source{}, // source |
| 1031 | "out_var", // name |
| 1032 | ast::StorageClass::kOutput, // storage_class |
| 1033 | &f32, // type |
| 1034 | false, // is_const |
| 1035 | nullptr, // constructor |
| 1036 | ast::VariableDecorationList{}); // decorations |
| 1037 | auto* sb_var = |
| 1038 | create<ast::Variable>(Source{}, // source |
| 1039 | "sb_var", // name |
| 1040 | ast::StorageClass::kStorageBuffer, // storage_class |
| 1041 | &f32, // type |
| 1042 | false, // is_const |
| 1043 | nullptr, // constructor |
| 1044 | ast::VariableDecorationList{}); // decorations |
| 1045 | auto* wg_var = |
| 1046 | create<ast::Variable>(Source{}, // source |
| 1047 | "wg_var", // name |
| 1048 | ast::StorageClass::kWorkgroup, // storage_class |
| 1049 | &f32, // type |
| 1050 | false, // is_const |
| 1051 | nullptr, // constructor |
| 1052 | ast::VariableDecorationList{}); // decorations |
| 1053 | auto* priv_var = |
| 1054 | create<ast::Variable>(Source{}, // source |
| 1055 | "priv_var", // name |
| 1056 | ast::StorageClass::kPrivate, // storage_class |
| 1057 | &f32, // type |
| 1058 | false, // is_const |
| 1059 | nullptr, // constructor |
| 1060 | ast::VariableDecorationList{}); // decorations |
dan sinclair | de2dd68 | 2020-07-14 20:37:28 +0000 | [diff] [blame] | 1061 | |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 1062 | mod->AddGlobalVariable(in_var); |
| 1063 | mod->AddGlobalVariable(out_var); |
| 1064 | mod->AddGlobalVariable(sb_var); |
| 1065 | mod->AddGlobalVariable(wg_var); |
| 1066 | mod->AddGlobalVariable(priv_var); |
dan sinclair | de2dd68 | 2020-07-14 20:37:28 +0000 | [diff] [blame] | 1067 | |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 1068 | auto* body = create<ast::BlockStatement>(); |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 1069 | body->append(create<ast::AssignmentStatement>( |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 1070 | create<ast::IdentifierExpression>(mod->RegisterSymbol("out_var"), |
| 1071 | "out_var"), |
| 1072 | create<ast::IdentifierExpression>(mod->RegisterSymbol("in_var"), |
| 1073 | "in_var"))); |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 1074 | body->append(create<ast::AssignmentStatement>( |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 1075 | create<ast::IdentifierExpression>(mod->RegisterSymbol("wg_var"), |
| 1076 | "wg_var"), |
| 1077 | create<ast::IdentifierExpression>(mod->RegisterSymbol("wg_var"), |
| 1078 | "wg_var"))); |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 1079 | body->append(create<ast::AssignmentStatement>( |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 1080 | create<ast::IdentifierExpression>(mod->RegisterSymbol("sb_var"), |
| 1081 | "sb_var"), |
| 1082 | create<ast::IdentifierExpression>(mod->RegisterSymbol("sb_var"), |
| 1083 | "sb_var"))); |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 1084 | body->append(create<ast::AssignmentStatement>( |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 1085 | create<ast::IdentifierExpression>(mod->RegisterSymbol("priv_var"), |
| 1086 | "priv_var"), |
| 1087 | create<ast::IdentifierExpression>(mod->RegisterSymbol("priv_var"), |
| 1088 | "priv_var"))); |
Ben Clayton | b29b09f | 2020-11-14 01:13:04 +0000 | [diff] [blame] | 1089 | ast::VariableList params; |
dan sinclair | a41132f | 2020-12-11 18:24:53 +0000 | [diff] [blame] | 1090 | auto* func = |
| 1091 | create<ast::Function>(Source{}, mod->RegisterSymbol("my_func"), "my_func", |
| 1092 | params, &f32, body, ast::FunctionDecorationList{}); |
dan sinclair | de2dd68 | 2020-07-14 20:37:28 +0000 | [diff] [blame] | 1093 | |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 1094 | mod->AddFunction(func); |
dan sinclair | de2dd68 | 2020-07-14 20:37:28 +0000 | [diff] [blame] | 1095 | |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 1096 | body = create<ast::BlockStatement>(); |
| 1097 | body->append(create<ast::AssignmentStatement>( |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 1098 | create<ast::IdentifierExpression>(mod->RegisterSymbol("out_var"), |
| 1099 | "out_var"), |
| 1100 | create<ast::CallExpression>( |
| 1101 | create<ast::IdentifierExpression>(mod->RegisterSymbol("my_func"), |
| 1102 | "my_func"), |
| 1103 | ast::ExpressionList{}))); |
dan sinclair | a41132f | 2020-12-11 18:24:53 +0000 | [diff] [blame] | 1104 | auto* func2 = |
| 1105 | create<ast::Function>(Source{}, mod->RegisterSymbol("func"), "func", |
| 1106 | params, &f32, body, ast::FunctionDecorationList{}); |
dan sinclair | de2dd68 | 2020-07-14 20:37:28 +0000 | [diff] [blame] | 1107 | |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 1108 | mod->AddFunction(func2); |
dan sinclair | de2dd68 | 2020-07-14 20:37:28 +0000 | [diff] [blame] | 1109 | |
| 1110 | // Register the function |
| 1111 | EXPECT_TRUE(td()->Determine()); |
| 1112 | |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 1113 | const auto& vars = func2->referenced_module_variables(); |
dan sinclair | de2dd68 | 2020-07-14 20:37:28 +0000 | [diff] [blame] | 1114 | ASSERT_EQ(vars.size(), 5u); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 1115 | EXPECT_EQ(vars[0], out_var); |
| 1116 | EXPECT_EQ(vars[1], in_var); |
| 1117 | EXPECT_EQ(vars[2], wg_var); |
| 1118 | EXPECT_EQ(vars[3], sb_var); |
| 1119 | EXPECT_EQ(vars[4], priv_var); |
dan sinclair | de2dd68 | 2020-07-14 20:37:28 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
dan sinclair | 13d2a3b | 2020-06-22 20:52:24 +0000 | [diff] [blame] | 1122 | TEST_F(TypeDeterminerTest, Function_NotRegisterFunctionVariable) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1123 | ast::type::F32 f32; |
dan sinclair | 13d2a3b | 2020-06-22 20:52:24 +0000 | [diff] [blame] | 1124 | |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 1125 | auto* var = |
| 1126 | create<ast::Variable>(Source{}, // source |
| 1127 | "in_var", // name |
| 1128 | ast::StorageClass::kFunction, // storage_class |
| 1129 | &f32, // type |
| 1130 | false, // is_const |
| 1131 | nullptr, // constructor |
| 1132 | ast::VariableDecorationList{}); // decorations |
dan sinclair | 13d2a3b | 2020-06-22 20:52:24 +0000 | [diff] [blame] | 1133 | |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 1134 | auto* body = create<ast::BlockStatement>(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 1135 | body->append(create<ast::VariableDeclStatement>(var)); |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 1136 | body->append(create<ast::AssignmentStatement>( |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 1137 | create<ast::IdentifierExpression>(mod->RegisterSymbol("var"), "var"), |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 1138 | create<ast::ScalarConstructorExpression>( |
| 1139 | create<ast::FloatLiteral>(&f32, 1.f)))); |
Ben Clayton | b29b09f | 2020-11-14 01:13:04 +0000 | [diff] [blame] | 1140 | |
| 1141 | ast::VariableList params; |
dan sinclair | a41132f | 2020-12-11 18:24:53 +0000 | [diff] [blame] | 1142 | auto* func = |
| 1143 | create<ast::Function>(Source{}, mod->RegisterSymbol("my_func"), "my_func", |
| 1144 | params, &f32, body, ast::FunctionDecorationList{}); |
dan sinclair | 13d2a3b | 2020-06-22 20:52:24 +0000 | [diff] [blame] | 1145 | |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 1146 | mod->AddFunction(func); |
dan sinclair | 13d2a3b | 2020-06-22 20:52:24 +0000 | [diff] [blame] | 1147 | |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 1148 | ast::Variable v(Source{}, "var", ast::StorageClass::kFunction, &f32, false, |
| 1149 | nullptr, ast::VariableDecorationList{}); |
dan sinclair | ff267ca | 2020-10-14 18:26:31 +0000 | [diff] [blame] | 1150 | td()->RegisterVariableForTesting(&v); |
| 1151 | |
dan sinclair | 13d2a3b | 2020-06-22 20:52:24 +0000 | [diff] [blame] | 1152 | // Register the function |
dan sinclair | ff267ca | 2020-10-14 18:26:31 +0000 | [diff] [blame] | 1153 | EXPECT_TRUE(td()->Determine()) << td()->error(); |
dan sinclair | 13d2a3b | 2020-06-22 20:52:24 +0000 | [diff] [blame] | 1154 | |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 1155 | EXPECT_EQ(func->referenced_module_variables().size(), 0u); |
dan sinclair | 13d2a3b | 2020-06-22 20:52:24 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 1158 | TEST_F(TypeDeterminerTest, Expr_MemberAccessor_Struct) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1159 | ast::type::I32 i32; |
| 1160 | ast::type::F32 f32; |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 1161 | |
| 1162 | ast::StructMemberDecorationList decos; |
| 1163 | ast::StructMemberList members; |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 1164 | members.push_back(create<ast::StructMember>("first_member", &i32, decos)); |
| 1165 | members.push_back(create<ast::StructMember>("second_member", &f32, decos)); |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 1166 | |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 1167 | auto* strct = create<ast::Struct>(members); |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 1168 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1169 | ast::type::Struct st("S", strct); |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 1170 | |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 1171 | auto* var = |
| 1172 | create<ast::Variable>(Source{}, // source |
| 1173 | "my_struct", // name |
| 1174 | ast::StorageClass::kNone, // storage_class |
| 1175 | &st, // type |
| 1176 | false, // is_const |
| 1177 | nullptr, // constructor |
| 1178 | ast::VariableDecorationList{}); // decorations |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 1179 | |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 1180 | mod->AddGlobalVariable(var); |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 1181 | |
| 1182 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 1183 | EXPECT_TRUE(td()->Determine()); |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 1184 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 1185 | auto* ident = create<ast::IdentifierExpression>( |
| 1186 | mod->RegisterSymbol("my_struct"), "my_struct"); |
| 1187 | auto* mem_ident = create<ast::IdentifierExpression>( |
| 1188 | mod->RegisterSymbol("second_member"), "second_member"); |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 1189 | |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 1190 | ast::MemberAccessorExpression mem(ident, mem_ident); |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 1191 | EXPECT_TRUE(td()->DetermineResultType(&mem)); |
| 1192 | ASSERT_NE(mem.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1193 | ASSERT_TRUE(mem.result_type()->Is<ast::type::Pointer>()); |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 1194 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1195 | auto* ptr = mem.result_type()->As<ast::type::Pointer>(); |
| 1196 | EXPECT_TRUE(ptr->type()->Is<ast::type::F32>()); |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
dan sinclair | b445a9b | 2020-04-24 00:40:45 +0000 | [diff] [blame] | 1199 | TEST_F(TypeDeterminerTest, Expr_MemberAccessor_Struct_Alias) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1200 | ast::type::I32 i32; |
| 1201 | ast::type::F32 f32; |
dan sinclair | b445a9b | 2020-04-24 00:40:45 +0000 | [diff] [blame] | 1202 | |
| 1203 | ast::StructMemberDecorationList decos; |
| 1204 | ast::StructMemberList members; |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 1205 | members.push_back(create<ast::StructMember>("first_member", &i32, decos)); |
| 1206 | members.push_back(create<ast::StructMember>("second_member", &f32, decos)); |
dan sinclair | b445a9b | 2020-04-24 00:40:45 +0000 | [diff] [blame] | 1207 | |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 1208 | auto* strct = create<ast::Struct>(members); |
dan sinclair | b445a9b | 2020-04-24 00:40:45 +0000 | [diff] [blame] | 1209 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1210 | auto st = std::make_unique<ast::type::Struct>("alias", strct); |
dan sinclair | 4226b6a | 2020-12-11 19:35:03 +0000 | [diff] [blame^] | 1211 | ast::type::Alias alias(mod->RegisterSymbol("alias"), "alias", st.get()); |
dan sinclair | b445a9b | 2020-04-24 00:40:45 +0000 | [diff] [blame] | 1212 | |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 1213 | auto* var = |
| 1214 | create<ast::Variable>(Source{}, // source |
| 1215 | "my_struct", // name |
| 1216 | ast::StorageClass::kNone, // storage_class |
| 1217 | &alias, // type |
| 1218 | false, // is_const |
| 1219 | nullptr, // constructor |
| 1220 | ast::VariableDecorationList{}); // decorations |
dan sinclair | b445a9b | 2020-04-24 00:40:45 +0000 | [diff] [blame] | 1221 | |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 1222 | mod->AddGlobalVariable(var); |
dan sinclair | b445a9b | 2020-04-24 00:40:45 +0000 | [diff] [blame] | 1223 | |
| 1224 | // Register the global |
| 1225 | EXPECT_TRUE(td()->Determine()); |
| 1226 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 1227 | auto* ident = create<ast::IdentifierExpression>( |
| 1228 | mod->RegisterSymbol("my_struct"), "my_struct"); |
| 1229 | auto* mem_ident = create<ast::IdentifierExpression>( |
| 1230 | mod->RegisterSymbol("second_member"), "second_member"); |
dan sinclair | b445a9b | 2020-04-24 00:40:45 +0000 | [diff] [blame] | 1231 | |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 1232 | ast::MemberAccessorExpression mem(ident, mem_ident); |
dan sinclair | b445a9b | 2020-04-24 00:40:45 +0000 | [diff] [blame] | 1233 | EXPECT_TRUE(td()->DetermineResultType(&mem)); |
| 1234 | ASSERT_NE(mem.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1235 | ASSERT_TRUE(mem.result_type()->Is<ast::type::Pointer>()); |
dan sinclair | b445a9b | 2020-04-24 00:40:45 +0000 | [diff] [blame] | 1236 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1237 | auto* ptr = mem.result_type()->As<ast::type::Pointer>(); |
| 1238 | EXPECT_TRUE(ptr->type()->Is<ast::type::F32>()); |
dan sinclair | b445a9b | 2020-04-24 00:40:45 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 1241 | TEST_F(TypeDeterminerTest, Expr_MemberAccessor_VectorSwizzle) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1242 | ast::type::F32 f32; |
| 1243 | ast::type::Vector vec3(&f32, 3); |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 1244 | |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 1245 | auto* var = |
| 1246 | create<ast::Variable>(Source{}, // source |
| 1247 | "my_vec", // name |
| 1248 | ast::StorageClass::kNone, // storage_class |
| 1249 | &vec3, // type |
| 1250 | false, // is_const |
| 1251 | nullptr, // constructor |
| 1252 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 1253 | mod->AddGlobalVariable(var); |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 1254 | |
| 1255 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 1256 | EXPECT_TRUE(td()->Determine()); |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 1257 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 1258 | auto* ident = create<ast::IdentifierExpression>(mod->RegisterSymbol("my_vec"), |
| 1259 | "my_vec"); |
| 1260 | auto* swizzle = |
| 1261 | create<ast::IdentifierExpression>(mod->RegisterSymbol("xy"), "xy"); |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 1262 | |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 1263 | ast::MemberAccessorExpression mem(ident, swizzle); |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 1264 | EXPECT_TRUE(td()->DetermineResultType(&mem)) << td()->error(); |
| 1265 | ASSERT_NE(mem.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1266 | ASSERT_TRUE(mem.result_type()->Is<ast::type::Vector>()); |
| 1267 | EXPECT_TRUE( |
| 1268 | mem.result_type()->As<ast::type::Vector>()->type()->Is<ast::type::F32>()); |
| 1269 | EXPECT_EQ(mem.result_type()->As<ast::type::Vector>()->size(), 2u); |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
dan sinclair | aac5865 | 2020-04-21 13:05:34 +0000 | [diff] [blame] | 1272 | TEST_F(TypeDeterminerTest, Expr_MemberAccessor_VectorSwizzle_SingleElement) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1273 | ast::type::F32 f32; |
| 1274 | ast::type::Vector vec3(&f32, 3); |
dan sinclair | aac5865 | 2020-04-21 13:05:34 +0000 | [diff] [blame] | 1275 | |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 1276 | auto* var = |
| 1277 | create<ast::Variable>(Source{}, // source |
| 1278 | "my_vec", // name |
| 1279 | ast::StorageClass::kNone, // storage_class |
| 1280 | &vec3, // type |
| 1281 | false, // is_const |
| 1282 | nullptr, // constructor |
| 1283 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 1284 | mod->AddGlobalVariable(var); |
dan sinclair | aac5865 | 2020-04-21 13:05:34 +0000 | [diff] [blame] | 1285 | |
| 1286 | // Register the global |
| 1287 | EXPECT_TRUE(td()->Determine()); |
| 1288 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 1289 | auto* ident = create<ast::IdentifierExpression>(mod->RegisterSymbol("my_vec"), |
| 1290 | "my_vec"); |
| 1291 | auto* swizzle = |
| 1292 | create<ast::IdentifierExpression>(mod->RegisterSymbol("x"), "x"); |
dan sinclair | aac5865 | 2020-04-21 13:05:34 +0000 | [diff] [blame] | 1293 | |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 1294 | ast::MemberAccessorExpression mem(ident, swizzle); |
dan sinclair | aac5865 | 2020-04-21 13:05:34 +0000 | [diff] [blame] | 1295 | EXPECT_TRUE(td()->DetermineResultType(&mem)) << td()->error(); |
| 1296 | ASSERT_NE(mem.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1297 | ASSERT_TRUE(mem.result_type()->Is<ast::type::Pointer>()); |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 1298 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1299 | auto* ptr = mem.result_type()->As<ast::type::Pointer>(); |
| 1300 | ASSERT_TRUE(ptr->type()->Is<ast::type::F32>()); |
dan sinclair | aac5865 | 2020-04-21 13:05:34 +0000 | [diff] [blame] | 1301 | } |
| 1302 | |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 1303 | TEST_F(TypeDeterminerTest, Expr_Accessor_MultiLevel) { |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 1304 | // struct b { |
| 1305 | // vec4<f32> foo |
| 1306 | // } |
| 1307 | // struct A { |
| 1308 | // vec3<struct b> mem |
| 1309 | // } |
| 1310 | // var c : A |
| 1311 | // c.mem[0].foo.yx |
| 1312 | // -> vec2<f32> |
| 1313 | // |
| 1314 | // MemberAccessor{ |
| 1315 | // MemberAccessor{ |
| 1316 | // ArrayAccessor{ |
| 1317 | // MemberAccessor{ |
| 1318 | // Identifier{c} |
| 1319 | // Identifier{mem} |
| 1320 | // } |
| 1321 | // ScalarConstructor{0} |
| 1322 | // } |
| 1323 | // Identifier{foo} |
| 1324 | // } |
| 1325 | // Identifier{yx} |
| 1326 | // } |
| 1327 | // |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1328 | ast::type::I32 i32; |
| 1329 | ast::type::F32 f32; |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 1330 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1331 | ast::type::Vector vec4(&f32, 4); |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 1332 | |
| 1333 | ast::StructMemberDecorationList decos; |
| 1334 | ast::StructMemberList b_members; |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 1335 | b_members.push_back(create<ast::StructMember>("foo", &vec4, decos)); |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 1336 | |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 1337 | auto* strctB = create<ast::Struct>(b_members); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1338 | ast::type::Struct stB("B", strctB); |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 1339 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1340 | ast::type::Vector vecB(&stB, 3); |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 1341 | |
| 1342 | ast::StructMemberList a_members; |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 1343 | a_members.push_back(create<ast::StructMember>("mem", &vecB, decos)); |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 1344 | |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 1345 | auto* strctA = create<ast::Struct>(a_members); |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 1346 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1347 | ast::type::Struct stA("A", strctA); |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 1348 | |
Ben Clayton | 321e5a9 | 2020-12-07 21:08:07 +0000 | [diff] [blame] | 1349 | auto* var = |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 1350 | create<ast::Variable>(Source{}, // source |
| 1351 | "c", // name |
| 1352 | ast::StorageClass::kNone, // storage_class |
| 1353 | &stA, // type |
| 1354 | false, // is_const |
| 1355 | nullptr, // constructor |
| 1356 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 1357 | mod->AddGlobalVariable(var); |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 1358 | |
| 1359 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 1360 | EXPECT_TRUE(td()->Determine()); |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 1361 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 1362 | auto* ident = |
| 1363 | create<ast::IdentifierExpression>(mod->RegisterSymbol("c"), "c"); |
| 1364 | auto* mem_ident = |
| 1365 | create<ast::IdentifierExpression>(mod->RegisterSymbol("mem"), "mem"); |
| 1366 | auto* foo_ident = |
| 1367 | create<ast::IdentifierExpression>(mod->RegisterSymbol("foo"), "foo"); |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 1368 | auto* idx = create<ast::ScalarConstructorExpression>( |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 1369 | create<ast::SintLiteral>(&i32, 0)); |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 1370 | auto* swizzle = |
| 1371 | create<ast::IdentifierExpression>(mod->RegisterSymbol("yx"), "yx"); |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 1372 | |
| 1373 | ast::MemberAccessorExpression mem( |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 1374 | create<ast::MemberAccessorExpression>( |
| 1375 | create<ast::ArrayAccessorExpression>( |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 1376 | create<ast::MemberAccessorExpression>(ident, mem_ident), idx), |
| 1377 | foo_ident), |
| 1378 | swizzle); |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 1379 | EXPECT_TRUE(td()->DetermineResultType(&mem)) << td()->error(); |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 1380 | |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 1381 | ASSERT_NE(mem.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1382 | ASSERT_TRUE(mem.result_type()->Is<ast::type::Vector>()); |
| 1383 | EXPECT_TRUE( |
| 1384 | mem.result_type()->As<ast::type::Vector>()->type()->Is<ast::type::F32>()); |
| 1385 | EXPECT_EQ(mem.result_type()->As<ast::type::Vector>()->size(), 2u); |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1388 | using Expr_Binary_BitwiseTest = TypeDeterminerTestWithParam<ast::BinaryOp>; |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 1389 | TEST_P(Expr_Binary_BitwiseTest, Scalar) { |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1390 | auto op = GetParam(); |
| 1391 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1392 | ast::type::I32 i32; |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1393 | |
Ben Clayton | 321e5a9 | 2020-12-07 21:08:07 +0000 | [diff] [blame] | 1394 | auto* var = |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 1395 | create<ast::Variable>(Source{}, // source |
| 1396 | "val", // name |
| 1397 | ast::StorageClass::kNone, // storage_class |
| 1398 | &i32, // type |
| 1399 | false, // is_const |
| 1400 | nullptr, // constructor |
| 1401 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 1402 | mod->AddGlobalVariable(var); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1403 | |
| 1404 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 1405 | ASSERT_TRUE(td()->Determine()) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1406 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 1407 | ast::BinaryExpression expr( |
| 1408 | op, create<ast::IdentifierExpression>(mod->RegisterSymbol("val"), "val"), |
| 1409 | create<ast::IdentifierExpression>(mod->RegisterSymbol("val"), "val")); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1410 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1411 | ASSERT_TRUE(td()->DetermineResultType(&expr)) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1412 | ASSERT_NE(expr.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1413 | EXPECT_TRUE(expr.result_type()->Is<ast::type::I32>()); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 1416 | TEST_P(Expr_Binary_BitwiseTest, Vector) { |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1417 | auto op = GetParam(); |
| 1418 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1419 | ast::type::I32 i32; |
| 1420 | ast::type::Vector vec3(&i32, 3); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1421 | |
Ben Clayton | 321e5a9 | 2020-12-07 21:08:07 +0000 | [diff] [blame] | 1422 | auto* var = |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 1423 | create<ast::Variable>(Source{}, // source |
| 1424 | "val", // name |
| 1425 | ast::StorageClass::kNone, // storage_class |
| 1426 | &vec3, // type |
| 1427 | false, // is_const |
| 1428 | nullptr, // constructor |
| 1429 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 1430 | mod->AddGlobalVariable(var); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1431 | |
| 1432 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 1433 | ASSERT_TRUE(td()->Determine()) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1434 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 1435 | ast::BinaryExpression expr( |
| 1436 | op, create<ast::IdentifierExpression>(mod->RegisterSymbol("val"), "val"), |
| 1437 | create<ast::IdentifierExpression>(mod->RegisterSymbol("val"), "val")); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1438 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1439 | ASSERT_TRUE(td()->DetermineResultType(&expr)) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1440 | ASSERT_NE(expr.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1441 | ASSERT_TRUE(expr.result_type()->Is<ast::type::Vector>()); |
Ben Clayton | 8a083ce | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1442 | EXPECT_TRUE(expr.result_type() |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1443 | ->As<ast::type::Vector>() |
Ben Clayton | 8a083ce | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1444 | ->type() |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1445 | ->Is<ast::type::I32>()); |
| 1446 | EXPECT_EQ(expr.result_type()->As<ast::type::Vector>()->size(), 3u); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1447 | } |
| 1448 | INSTANTIATE_TEST_SUITE_P(TypeDeterminerTest, |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 1449 | Expr_Binary_BitwiseTest, |
| 1450 | testing::Values(ast::BinaryOp::kAnd, |
| 1451 | ast::BinaryOp::kOr, |
| 1452 | ast::BinaryOp::kXor, |
| 1453 | ast::BinaryOp::kShiftLeft, |
| 1454 | ast::BinaryOp::kShiftRight, |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 1455 | ast::BinaryOp::kAdd, |
| 1456 | ast::BinaryOp::kSubtract, |
| 1457 | ast::BinaryOp::kDivide, |
| 1458 | ast::BinaryOp::kModulo)); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1459 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1460 | using Expr_Binary_LogicalTest = TypeDeterminerTestWithParam<ast::BinaryOp>; |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 1461 | TEST_P(Expr_Binary_LogicalTest, Scalar) { |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1462 | auto op = GetParam(); |
| 1463 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1464 | ast::type::Bool bool_type; |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1465 | |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 1466 | auto* var = |
| 1467 | create<ast::Variable>(Source{}, // source |
| 1468 | "val", // name |
| 1469 | ast::StorageClass::kNone, // storage_class |
| 1470 | &bool_type, // type |
| 1471 | false, // is_const |
| 1472 | nullptr, // constructor |
| 1473 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 1474 | mod->AddGlobalVariable(var); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1475 | |
| 1476 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 1477 | ASSERT_TRUE(td()->Determine()) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1478 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 1479 | ast::BinaryExpression expr( |
| 1480 | op, create<ast::IdentifierExpression>(mod->RegisterSymbol("val"), "val"), |
| 1481 | create<ast::IdentifierExpression>(mod->RegisterSymbol("val"), "val")); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1482 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1483 | ASSERT_TRUE(td()->DetermineResultType(&expr)) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1484 | ASSERT_NE(expr.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1485 | EXPECT_TRUE(expr.result_type()->Is<ast::type::Bool>()); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1486 | } |
| 1487 | |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 1488 | TEST_P(Expr_Binary_LogicalTest, Vector) { |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1489 | auto op = GetParam(); |
| 1490 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1491 | ast::type::Bool bool_type; |
| 1492 | ast::type::Vector vec3(&bool_type, 3); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1493 | |
Ben Clayton | 321e5a9 | 2020-12-07 21:08:07 +0000 | [diff] [blame] | 1494 | auto* var = |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 1495 | create<ast::Variable>(Source{}, // source |
| 1496 | "val", // name |
| 1497 | ast::StorageClass::kNone, // storage_class |
| 1498 | &vec3, // type |
| 1499 | false, // is_const |
| 1500 | nullptr, // constructor |
| 1501 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 1502 | mod->AddGlobalVariable(var); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1503 | |
| 1504 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 1505 | ASSERT_TRUE(td()->Determine()) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1506 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 1507 | ast::BinaryExpression expr( |
| 1508 | op, create<ast::IdentifierExpression>(mod->RegisterSymbol("val"), "val"), |
| 1509 | create<ast::IdentifierExpression>(mod->RegisterSymbol("val"), "val")); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1510 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1511 | ASSERT_TRUE(td()->DetermineResultType(&expr)) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1512 | ASSERT_NE(expr.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1513 | ASSERT_TRUE(expr.result_type()->Is<ast::type::Vector>()); |
Ben Clayton | 8a083ce | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1514 | EXPECT_TRUE(expr.result_type() |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1515 | ->As<ast::type::Vector>() |
Ben Clayton | 8a083ce | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1516 | ->type() |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1517 | ->Is<ast::type::Bool>()); |
| 1518 | EXPECT_EQ(expr.result_type()->As<ast::type::Vector>()->size(), 3u); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1519 | } |
| 1520 | INSTANTIATE_TEST_SUITE_P(TypeDeterminerTest, |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 1521 | Expr_Binary_LogicalTest, |
| 1522 | testing::Values(ast::BinaryOp::kLogicalAnd, |
| 1523 | ast::BinaryOp::kLogicalOr)); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1524 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1525 | using Expr_Binary_CompareTest = TypeDeterminerTestWithParam<ast::BinaryOp>; |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 1526 | TEST_P(Expr_Binary_CompareTest, Scalar) { |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1527 | auto op = GetParam(); |
| 1528 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1529 | ast::type::I32 i32; |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1530 | |
Ben Clayton | 321e5a9 | 2020-12-07 21:08:07 +0000 | [diff] [blame] | 1531 | auto* var = |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 1532 | create<ast::Variable>(Source{}, // source |
| 1533 | "val", // name |
| 1534 | ast::StorageClass::kNone, // storage_class |
| 1535 | &i32, // type |
| 1536 | false, // is_const |
| 1537 | nullptr, // constructor |
| 1538 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 1539 | mod->AddGlobalVariable(var); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1540 | |
| 1541 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 1542 | ASSERT_TRUE(td()->Determine()) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1543 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 1544 | ast::BinaryExpression expr( |
| 1545 | op, create<ast::IdentifierExpression>(mod->RegisterSymbol("val"), "val"), |
| 1546 | create<ast::IdentifierExpression>(mod->RegisterSymbol("val"), "val")); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1547 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1548 | ASSERT_TRUE(td()->DetermineResultType(&expr)) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1549 | ASSERT_NE(expr.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1550 | EXPECT_TRUE(expr.result_type()->Is<ast::type::Bool>()); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 1553 | TEST_P(Expr_Binary_CompareTest, Vector) { |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1554 | auto op = GetParam(); |
| 1555 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1556 | ast::type::I32 i32; |
| 1557 | ast::type::Vector vec3(&i32, 3); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1558 | |
Ben Clayton | 321e5a9 | 2020-12-07 21:08:07 +0000 | [diff] [blame] | 1559 | auto* var = |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 1560 | create<ast::Variable>(Source{}, // source |
| 1561 | "val", // name |
| 1562 | ast::StorageClass::kNone, // storage_class |
| 1563 | &vec3, // type |
| 1564 | false, // is_const |
| 1565 | nullptr, // constructor |
| 1566 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 1567 | mod->AddGlobalVariable(var); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1568 | |
| 1569 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 1570 | ASSERT_TRUE(td()->Determine()) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1571 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 1572 | ast::BinaryExpression expr( |
| 1573 | op, create<ast::IdentifierExpression>(mod->RegisterSymbol("val"), "val"), |
| 1574 | create<ast::IdentifierExpression>(mod->RegisterSymbol("val"), "val")); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1575 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1576 | ASSERT_TRUE(td()->DetermineResultType(&expr)) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1577 | ASSERT_NE(expr.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1578 | ASSERT_TRUE(expr.result_type()->Is<ast::type::Vector>()); |
Ben Clayton | 8a083ce | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1579 | EXPECT_TRUE(expr.result_type() |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1580 | ->As<ast::type::Vector>() |
Ben Clayton | 8a083ce | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1581 | ->type() |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1582 | ->Is<ast::type::Bool>()); |
| 1583 | EXPECT_EQ(expr.result_type()->As<ast::type::Vector>()->size(), 3u); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1584 | } |
| 1585 | INSTANTIATE_TEST_SUITE_P(TypeDeterminerTest, |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 1586 | Expr_Binary_CompareTest, |
| 1587 | testing::Values(ast::BinaryOp::kEqual, |
| 1588 | ast::BinaryOp::kNotEqual, |
| 1589 | ast::BinaryOp::kLessThan, |
| 1590 | ast::BinaryOp::kGreaterThan, |
| 1591 | ast::BinaryOp::kLessThanEqual, |
| 1592 | ast::BinaryOp::kGreaterThanEqual)); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1593 | |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 1594 | TEST_F(TypeDeterminerTest, Expr_Binary_Multiply_Scalar_Scalar) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1595 | ast::type::I32 i32; |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1596 | |
Ben Clayton | 321e5a9 | 2020-12-07 21:08:07 +0000 | [diff] [blame] | 1597 | auto* var = |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 1598 | create<ast::Variable>(Source{}, // source |
| 1599 | "val", // name |
| 1600 | ast::StorageClass::kNone, // storage_class |
| 1601 | &i32, // type |
| 1602 | false, // is_const |
| 1603 | nullptr, // constructor |
| 1604 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 1605 | mod->AddGlobalVariable(var); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1606 | |
| 1607 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 1608 | ASSERT_TRUE(td()->Determine()) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1609 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 1610 | ast::BinaryExpression expr( |
| 1611 | ast::BinaryOp::kMultiply, |
| 1612 | create<ast::IdentifierExpression>(mod->RegisterSymbol("val"), "val"), |
| 1613 | create<ast::IdentifierExpression>(mod->RegisterSymbol("val"), "val")); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1614 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1615 | ASSERT_TRUE(td()->DetermineResultType(&expr)) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1616 | ASSERT_NE(expr.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1617 | EXPECT_TRUE(expr.result_type()->Is<ast::type::I32>()); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1618 | } |
| 1619 | |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 1620 | TEST_F(TypeDeterminerTest, Expr_Binary_Multiply_Vector_Scalar) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1621 | ast::type::F32 f32; |
| 1622 | ast::type::Vector vec3(&f32, 3); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1623 | |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 1624 | auto* scalar = |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 1625 | create<ast::Variable>(Source{}, // source |
| 1626 | "scalar", // name |
| 1627 | ast::StorageClass::kNone, // storage_class |
| 1628 | &f32, // type |
| 1629 | false, // is_const |
| 1630 | nullptr, // constructor |
| 1631 | ast::VariableDecorationList{}); // decorations |
| 1632 | auto* vector = |
| 1633 | create<ast::Variable>(Source{}, // source |
| 1634 | "vector", // name |
| 1635 | ast::StorageClass::kNone, // storage_class |
| 1636 | &vec3, // type |
| 1637 | false, // is_const |
| 1638 | nullptr, // constructor |
| 1639 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 1640 | mod->AddGlobalVariable(scalar); |
| 1641 | mod->AddGlobalVariable(vector); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1642 | |
| 1643 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 1644 | ASSERT_TRUE(td()->Determine()) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1645 | |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 1646 | ast::BinaryExpression expr(ast::BinaryOp::kMultiply, |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 1647 | create<ast::IdentifierExpression>( |
| 1648 | mod->RegisterSymbol("vector"), "vector"), |
| 1649 | create<ast::IdentifierExpression>( |
| 1650 | mod->RegisterSymbol("scalar"), "scalar")); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1651 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1652 | ASSERT_TRUE(td()->DetermineResultType(&expr)) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1653 | ASSERT_NE(expr.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1654 | ASSERT_TRUE(expr.result_type()->Is<ast::type::Vector>()); |
Ben Clayton | 8a083ce | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1655 | EXPECT_TRUE(expr.result_type() |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1656 | ->As<ast::type::Vector>() |
Ben Clayton | 8a083ce | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1657 | ->type() |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1658 | ->Is<ast::type::F32>()); |
| 1659 | EXPECT_EQ(expr.result_type()->As<ast::type::Vector>()->size(), 3u); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1660 | } |
| 1661 | |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 1662 | TEST_F(TypeDeterminerTest, Expr_Binary_Multiply_Scalar_Vector) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1663 | ast::type::F32 f32; |
| 1664 | ast::type::Vector vec3(&f32, 3); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1665 | |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 1666 | auto* scalar = |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 1667 | create<ast::Variable>(Source{}, // source |
| 1668 | "scalar", // name |
| 1669 | ast::StorageClass::kNone, // storage_class |
| 1670 | &f32, // type |
| 1671 | false, // is_const |
| 1672 | nullptr, // constructor |
| 1673 | ast::VariableDecorationList{}); // decorations |
| 1674 | auto* vector = |
| 1675 | create<ast::Variable>(Source{}, // source |
| 1676 | "vector", // name |
| 1677 | ast::StorageClass::kNone, // storage_class |
| 1678 | &vec3, // type |
| 1679 | false, // is_const |
| 1680 | nullptr, // constructor |
| 1681 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 1682 | mod->AddGlobalVariable(scalar); |
| 1683 | mod->AddGlobalVariable(vector); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1684 | |
| 1685 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 1686 | ASSERT_TRUE(td()->Determine()) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1687 | |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 1688 | ast::BinaryExpression expr(ast::BinaryOp::kMultiply, |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 1689 | create<ast::IdentifierExpression>( |
| 1690 | mod->RegisterSymbol("scalar"), "scalar"), |
| 1691 | create<ast::IdentifierExpression>( |
| 1692 | mod->RegisterSymbol("vector"), "vector")); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1693 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1694 | ASSERT_TRUE(td()->DetermineResultType(&expr)) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1695 | ASSERT_NE(expr.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1696 | ASSERT_TRUE(expr.result_type()->Is<ast::type::Vector>()); |
Ben Clayton | 8a083ce | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1697 | EXPECT_TRUE(expr.result_type() |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1698 | ->As<ast::type::Vector>() |
Ben Clayton | 8a083ce | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1699 | ->type() |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1700 | ->Is<ast::type::F32>()); |
| 1701 | EXPECT_EQ(expr.result_type()->As<ast::type::Vector>()->size(), 3u); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1702 | } |
| 1703 | |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 1704 | TEST_F(TypeDeterminerTest, Expr_Binary_Multiply_Vector_Vector) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1705 | ast::type::F32 f32; |
| 1706 | ast::type::Vector vec3(&f32, 3); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1707 | |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 1708 | auto* vector = |
| 1709 | create<ast::Variable>(Source{}, // source |
| 1710 | "vector", // name |
| 1711 | ast::StorageClass::kNone, // storage_class |
| 1712 | &vec3, // type |
| 1713 | false, // is_const |
| 1714 | nullptr, // constructor |
| 1715 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 1716 | mod->AddGlobalVariable(vector); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1717 | |
| 1718 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 1719 | ASSERT_TRUE(td()->Determine()) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1720 | |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 1721 | ast::BinaryExpression expr(ast::BinaryOp::kMultiply, |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 1722 | create<ast::IdentifierExpression>( |
| 1723 | mod->RegisterSymbol("vector"), "vector"), |
| 1724 | create<ast::IdentifierExpression>( |
| 1725 | mod->RegisterSymbol("vector"), "vector")); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1726 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1727 | ASSERT_TRUE(td()->DetermineResultType(&expr)) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1728 | ASSERT_NE(expr.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1729 | ASSERT_TRUE(expr.result_type()->Is<ast::type::Vector>()); |
Ben Clayton | 8a083ce | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1730 | EXPECT_TRUE(expr.result_type() |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1731 | ->As<ast::type::Vector>() |
Ben Clayton | 8a083ce | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1732 | ->type() |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1733 | ->Is<ast::type::F32>()); |
| 1734 | EXPECT_EQ(expr.result_type()->As<ast::type::Vector>()->size(), 3u); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1735 | } |
| 1736 | |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 1737 | TEST_F(TypeDeterminerTest, Expr_Binary_Multiply_Matrix_Scalar) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1738 | ast::type::F32 f32; |
| 1739 | ast::type::Matrix mat3x2(&f32, 3, 2); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1740 | |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 1741 | auto* scalar = |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 1742 | create<ast::Variable>(Source{}, // source |
| 1743 | "scalar", // name |
| 1744 | ast::StorageClass::kNone, // storage_class |
| 1745 | &f32, // type |
| 1746 | false, // is_const |
| 1747 | nullptr, // constructor |
| 1748 | ast::VariableDecorationList{}); // decorations |
| 1749 | auto* matrix = |
| 1750 | create<ast::Variable>(Source{}, // source |
| 1751 | "matrix", // name |
| 1752 | ast::StorageClass::kNone, // storage_class |
| 1753 | &mat3x2, // type |
| 1754 | false, // is_const |
| 1755 | nullptr, // constructor |
| 1756 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 1757 | mod->AddGlobalVariable(scalar); |
| 1758 | mod->AddGlobalVariable(matrix); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1759 | |
| 1760 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 1761 | ASSERT_TRUE(td()->Determine()) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1762 | |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 1763 | ast::BinaryExpression expr(ast::BinaryOp::kMultiply, |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 1764 | create<ast::IdentifierExpression>( |
| 1765 | mod->RegisterSymbol("matrix"), "matrix"), |
| 1766 | create<ast::IdentifierExpression>( |
| 1767 | mod->RegisterSymbol("scalar"), "scalar")); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1768 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1769 | ASSERT_TRUE(td()->DetermineResultType(&expr)) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1770 | ASSERT_NE(expr.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1771 | ASSERT_TRUE(expr.result_type()->Is<ast::type::Matrix>()); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1772 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1773 | auto* mat = expr.result_type()->As<ast::type::Matrix>(); |
| 1774 | EXPECT_TRUE(mat->type()->Is<ast::type::F32>()); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 1775 | EXPECT_EQ(mat->rows(), 3u); |
| 1776 | EXPECT_EQ(mat->columns(), 2u); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1777 | } |
| 1778 | |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 1779 | TEST_F(TypeDeterminerTest, Expr_Binary_Multiply_Scalar_Matrix) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1780 | ast::type::F32 f32; |
| 1781 | ast::type::Matrix mat3x2(&f32, 3, 2); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1782 | |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 1783 | auto* scalar = |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 1784 | create<ast::Variable>(Source{}, // source |
| 1785 | "scalar", // name |
| 1786 | ast::StorageClass::kNone, // storage_class |
| 1787 | &f32, // type |
| 1788 | false, // is_const |
| 1789 | nullptr, // constructor |
| 1790 | ast::VariableDecorationList{}); // decorations |
| 1791 | auto* matrix = |
| 1792 | create<ast::Variable>(Source{}, // source |
| 1793 | "matrix", // name |
| 1794 | ast::StorageClass::kNone, // storage_class |
| 1795 | &mat3x2, // type |
| 1796 | false, // is_const |
| 1797 | nullptr, // constructor |
| 1798 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 1799 | mod->AddGlobalVariable(scalar); |
| 1800 | mod->AddGlobalVariable(matrix); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1801 | |
| 1802 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 1803 | ASSERT_TRUE(td()->Determine()) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1804 | |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 1805 | ast::BinaryExpression expr(ast::BinaryOp::kMultiply, |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 1806 | create<ast::IdentifierExpression>( |
| 1807 | mod->RegisterSymbol("scalar"), "scalar"), |
| 1808 | create<ast::IdentifierExpression>( |
| 1809 | mod->RegisterSymbol("matrix"), "matrix")); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1810 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1811 | ASSERT_TRUE(td()->DetermineResultType(&expr)) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1812 | ASSERT_NE(expr.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1813 | ASSERT_TRUE(expr.result_type()->Is<ast::type::Matrix>()); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1814 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1815 | auto* mat = expr.result_type()->As<ast::type::Matrix>(); |
| 1816 | EXPECT_TRUE(mat->type()->Is<ast::type::F32>()); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 1817 | EXPECT_EQ(mat->rows(), 3u); |
| 1818 | EXPECT_EQ(mat->columns(), 2u); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1819 | } |
| 1820 | |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 1821 | TEST_F(TypeDeterminerTest, Expr_Binary_Multiply_Matrix_Vector) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1822 | ast::type::F32 f32; |
| 1823 | ast::type::Vector vec3(&f32, 2); |
| 1824 | ast::type::Matrix mat3x2(&f32, 3, 2); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1825 | |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 1826 | auto* vector = |
| 1827 | create<ast::Variable>(Source{}, // source |
| 1828 | "vector", // name |
| 1829 | ast::StorageClass::kNone, // storage_class |
| 1830 | &vec3, // type |
| 1831 | false, // is_const |
| 1832 | nullptr, // constructor |
| 1833 | ast::VariableDecorationList{}); // decorations |
| 1834 | auto* matrix = |
| 1835 | create<ast::Variable>(Source{}, // source |
| 1836 | "matrix", // name |
| 1837 | ast::StorageClass::kNone, // storage_class |
| 1838 | &mat3x2, // type |
| 1839 | false, // is_const |
| 1840 | nullptr, // constructor |
| 1841 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 1842 | mod->AddGlobalVariable(vector); |
| 1843 | mod->AddGlobalVariable(matrix); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1844 | |
| 1845 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 1846 | ASSERT_TRUE(td()->Determine()) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1847 | |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 1848 | ast::BinaryExpression expr(ast::BinaryOp::kMultiply, |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 1849 | create<ast::IdentifierExpression>( |
| 1850 | mod->RegisterSymbol("matrix"), "matrix"), |
| 1851 | create<ast::IdentifierExpression>( |
| 1852 | mod->RegisterSymbol("vector"), "vector")); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1853 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1854 | ASSERT_TRUE(td()->DetermineResultType(&expr)) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1855 | ASSERT_NE(expr.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1856 | ASSERT_TRUE(expr.result_type()->Is<ast::type::Vector>()); |
Ben Clayton | 8a083ce | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1857 | EXPECT_TRUE(expr.result_type() |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1858 | ->As<ast::type::Vector>() |
Ben Clayton | 8a083ce | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1859 | ->type() |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1860 | ->Is<ast::type::F32>()); |
| 1861 | EXPECT_EQ(expr.result_type()->As<ast::type::Vector>()->size(), 3u); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1862 | } |
| 1863 | |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 1864 | TEST_F(TypeDeterminerTest, Expr_Binary_Multiply_Vector_Matrix) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1865 | ast::type::F32 f32; |
| 1866 | ast::type::Vector vec3(&f32, 3); |
| 1867 | ast::type::Matrix mat3x2(&f32, 3, 2); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1868 | |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 1869 | auto* vector = |
| 1870 | create<ast::Variable>(Source{}, // source |
| 1871 | "vector", // name |
| 1872 | ast::StorageClass::kNone, // storage_class |
| 1873 | &vec3, // type |
| 1874 | false, // is_const |
| 1875 | nullptr, // constructor |
| 1876 | ast::VariableDecorationList{}); // decorations |
| 1877 | auto* matrix = |
| 1878 | create<ast::Variable>(Source{}, // source |
| 1879 | "matrix", // name |
| 1880 | ast::StorageClass::kNone, // storage_class |
| 1881 | &mat3x2, // type |
| 1882 | false, // is_const |
| 1883 | nullptr, // constructor |
| 1884 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 1885 | mod->AddGlobalVariable(vector); |
| 1886 | mod->AddGlobalVariable(matrix); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1887 | |
| 1888 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 1889 | ASSERT_TRUE(td()->Determine()) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1890 | |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 1891 | ast::BinaryExpression expr(ast::BinaryOp::kMultiply, |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 1892 | create<ast::IdentifierExpression>( |
| 1893 | mod->RegisterSymbol("vector"), "vector"), |
| 1894 | create<ast::IdentifierExpression>( |
| 1895 | mod->RegisterSymbol("matrix"), "matrix")); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1896 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1897 | ASSERT_TRUE(td()->DetermineResultType(&expr)) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1898 | ASSERT_NE(expr.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1899 | ASSERT_TRUE(expr.result_type()->Is<ast::type::Vector>()); |
Ben Clayton | 8a083ce | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1900 | EXPECT_TRUE(expr.result_type() |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1901 | ->As<ast::type::Vector>() |
Ben Clayton | 8a083ce | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1902 | ->type() |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1903 | ->Is<ast::type::F32>()); |
| 1904 | EXPECT_EQ(expr.result_type()->As<ast::type::Vector>()->size(), 2u); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1905 | } |
| 1906 | |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 1907 | TEST_F(TypeDeterminerTest, Expr_Binary_Multiply_Matrix_Matrix) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1908 | ast::type::F32 f32; |
| 1909 | ast::type::Matrix mat4x3(&f32, 4, 3); |
| 1910 | ast::type::Matrix mat3x4(&f32, 3, 4); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1911 | |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 1912 | auto* matrix1 = |
| 1913 | create<ast::Variable>(Source{}, // source |
| 1914 | "mat4x3", // name |
| 1915 | ast::StorageClass::kNone, // storage_class |
| 1916 | &mat4x3, // type |
| 1917 | false, // is_const |
| 1918 | nullptr, // constructor |
| 1919 | ast::VariableDecorationList{}); // decorations |
| 1920 | auto* matrix2 = |
| 1921 | create<ast::Variable>(Source{}, // source |
| 1922 | "mat3x4", // name |
| 1923 | ast::StorageClass::kNone, // storage_class |
| 1924 | &mat3x4, // type |
| 1925 | false, // is_const |
| 1926 | nullptr, // constructor |
| 1927 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 1928 | mod->AddGlobalVariable(matrix1); |
| 1929 | mod->AddGlobalVariable(matrix2); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1930 | |
| 1931 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 1932 | ASSERT_TRUE(td()->Determine()) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1933 | |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 1934 | ast::BinaryExpression expr(ast::BinaryOp::kMultiply, |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 1935 | create<ast::IdentifierExpression>( |
| 1936 | mod->RegisterSymbol("mat4x3"), "mat4x3"), |
| 1937 | create<ast::IdentifierExpression>( |
| 1938 | mod->RegisterSymbol("mat3x4"), "mat3x4")); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1939 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1940 | ASSERT_TRUE(td()->DetermineResultType(&expr)) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1941 | ASSERT_NE(expr.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1942 | ASSERT_TRUE(expr.result_type()->Is<ast::type::Matrix>()); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1943 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1944 | auto* mat = expr.result_type()->As<ast::type::Matrix>(); |
| 1945 | EXPECT_TRUE(mat->type()->Is<ast::type::F32>()); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 1946 | EXPECT_EQ(mat->rows(), 4u); |
| 1947 | EXPECT_EQ(mat->columns(), 4u); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1948 | } |
| 1949 | |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 1950 | using IntrinsicDerivativeTest = TypeDeterminerTestWithParam<std::string>; |
| 1951 | TEST_P(IntrinsicDerivativeTest, Scalar) { |
| 1952 | auto name = GetParam(); |
dan sinclair | b173056 | 2020-04-07 19:26:49 +0000 | [diff] [blame] | 1953 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1954 | ast::type::F32 f32; |
dan sinclair | b173056 | 2020-04-07 19:26:49 +0000 | [diff] [blame] | 1955 | |
Ben Clayton | 321e5a9 | 2020-12-07 21:08:07 +0000 | [diff] [blame] | 1956 | auto* var = |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 1957 | create<ast::Variable>(Source{}, // source |
| 1958 | "ident", // name |
| 1959 | ast::StorageClass::kNone, // storage_class |
| 1960 | &f32, // type |
| 1961 | false, // is_const |
| 1962 | nullptr, // constructor |
| 1963 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 1964 | mod->AddGlobalVariable(var); |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 1965 | |
| 1966 | // Register the global |
| 1967 | EXPECT_TRUE(td()->Determine()); |
| 1968 | |
| 1969 | ast::ExpressionList call_params; |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 1970 | call_params.push_back( |
| 1971 | create<ast::IdentifierExpression>(mod->RegisterSymbol("ident"), "ident")); |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 1972 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 1973 | ast::CallExpression expr( |
| 1974 | create<ast::IdentifierExpression>(mod->RegisterSymbol(name), name), |
| 1975 | call_params); |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 1976 | EXPECT_TRUE(td()->DetermineResultType(&expr)); |
| 1977 | |
| 1978 | ASSERT_NE(expr.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1979 | ASSERT_TRUE(expr.result_type()->Is<ast::type::F32>()); |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 1980 | } |
| 1981 | |
| 1982 | TEST_P(IntrinsicDerivativeTest, Vector) { |
| 1983 | auto name = GetParam(); |
| 1984 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 1985 | ast::type::F32 f32; |
| 1986 | ast::type::Vector vec4(&f32, 4); |
dan sinclair | b173056 | 2020-04-07 19:26:49 +0000 | [diff] [blame] | 1987 | |
Ben Clayton | 321e5a9 | 2020-12-07 21:08:07 +0000 | [diff] [blame] | 1988 | auto* var = |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 1989 | create<ast::Variable>(Source{}, // source |
| 1990 | "ident", // name |
| 1991 | ast::StorageClass::kNone, // storage_class |
| 1992 | &vec4, // type |
| 1993 | false, // is_const |
| 1994 | nullptr, // constructor |
| 1995 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 1996 | mod->AddGlobalVariable(var); |
dan sinclair | b173056 | 2020-04-07 19:26:49 +0000 | [diff] [blame] | 1997 | |
| 1998 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 1999 | EXPECT_TRUE(td()->Determine()); |
dan sinclair | b173056 | 2020-04-07 19:26:49 +0000 | [diff] [blame] | 2000 | |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2001 | ast::ExpressionList call_params; |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 2002 | call_params.push_back( |
| 2003 | create<ast::IdentifierExpression>(mod->RegisterSymbol("ident"), "ident")); |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2004 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 2005 | ast::CallExpression expr( |
| 2006 | create<ast::IdentifierExpression>(mod->RegisterSymbol(name), name), |
| 2007 | call_params); |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2008 | EXPECT_TRUE(td()->DetermineResultType(&expr)); |
| 2009 | |
| 2010 | ASSERT_NE(expr.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2011 | ASSERT_TRUE(expr.result_type()->Is<ast::type::Vector>()); |
Ben Clayton | 8a083ce | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2012 | EXPECT_TRUE(expr.result_type() |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2013 | ->As<ast::type::Vector>() |
Ben Clayton | 8a083ce | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2014 | ->type() |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2015 | ->Is<ast::type::F32>()); |
| 2016 | EXPECT_EQ(expr.result_type()->As<ast::type::Vector>()->size(), 4u); |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2017 | } |
| 2018 | |
| 2019 | TEST_P(IntrinsicDerivativeTest, MissingParam) { |
| 2020 | auto name = GetParam(); |
| 2021 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2022 | ast::type::F32 f32; |
| 2023 | ast::type::Vector vec4(&f32, 4); |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2024 | |
| 2025 | // Register the global |
| 2026 | EXPECT_TRUE(td()->Determine()); |
| 2027 | |
| 2028 | ast::ExpressionList call_params; |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 2029 | ast::CallExpression expr( |
| 2030 | create<ast::IdentifierExpression>(mod->RegisterSymbol(name), name), |
| 2031 | call_params); |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2032 | EXPECT_FALSE(td()->DetermineResultType(&expr)); |
| 2033 | EXPECT_EQ(td()->error(), "incorrect number of parameters for " + name); |
| 2034 | } |
| 2035 | |
| 2036 | TEST_P(IntrinsicDerivativeTest, ToomManyParams) { |
| 2037 | auto name = GetParam(); |
| 2038 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2039 | ast::type::F32 f32; |
| 2040 | ast::type::Vector vec4(&f32, 4); |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2041 | |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 2042 | auto* var1 = |
| 2043 | create<ast::Variable>(Source{}, // source |
| 2044 | "ident1", // name |
| 2045 | ast::StorageClass::kNone, // storage_class |
| 2046 | &vec4, // type |
| 2047 | false, // is_const |
| 2048 | nullptr, // constructor |
| 2049 | ast::VariableDecorationList{}); // decorations |
| 2050 | auto* var2 = |
| 2051 | create<ast::Variable>(Source{}, // source |
| 2052 | "ident2", // name |
| 2053 | ast::StorageClass::kNone, // storage_class |
| 2054 | &vec4, // type |
| 2055 | false, // is_const |
| 2056 | nullptr, // constructor |
| 2057 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 2058 | mod->AddGlobalVariable(var1); |
| 2059 | mod->AddGlobalVariable(var2); |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2060 | |
| 2061 | // Register the global |
| 2062 | EXPECT_TRUE(td()->Determine()); |
| 2063 | |
| 2064 | ast::ExpressionList call_params; |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 2065 | call_params.push_back(create<ast::IdentifierExpression>( |
| 2066 | mod->RegisterSymbol("ident1"), "ident1")); |
| 2067 | call_params.push_back(create<ast::IdentifierExpression>( |
| 2068 | mod->RegisterSymbol("ident2"), "ident2")); |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2069 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 2070 | ast::CallExpression expr( |
| 2071 | create<ast::IdentifierExpression>(mod->RegisterSymbol(name), name), |
| 2072 | call_params); |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2073 | EXPECT_FALSE(td()->DetermineResultType(&expr)); |
| 2074 | EXPECT_EQ(td()->error(), "incorrect number of parameters for " + name); |
dan sinclair | b173056 | 2020-04-07 19:26:49 +0000 | [diff] [blame] | 2075 | } |
| 2076 | INSTANTIATE_TEST_SUITE_P(TypeDeterminerTest, |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2077 | IntrinsicDerivativeTest, |
| 2078 | testing::Values("dpdx", |
Tomek Ponitka | 4e2d248 | 2020-09-08 16:30:29 +0000 | [diff] [blame] | 2079 | "dpdxCoarse", |
| 2080 | "dpdxFine", |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2081 | "dpdy", |
Tomek Ponitka | 4e2d248 | 2020-09-08 16:30:29 +0000 | [diff] [blame] | 2082 | "dpdyCoarse", |
| 2083 | "dpdyFine", |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2084 | "fwidth", |
Tomek Ponitka | 4e2d248 | 2020-09-08 16:30:29 +0000 | [diff] [blame] | 2085 | "fwidthCoarse", |
| 2086 | "fwidthFine")); |
dan sinclair | b173056 | 2020-04-07 19:26:49 +0000 | [diff] [blame] | 2087 | |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2088 | using Intrinsic = TypeDeterminerTestWithParam<std::string>; |
| 2089 | TEST_P(Intrinsic, Test) { |
| 2090 | auto name = GetParam(); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 2091 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2092 | ast::type::Bool bool_type; |
| 2093 | ast::type::Vector vec3(&bool_type, 3); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 2094 | |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 2095 | auto* var = |
| 2096 | create<ast::Variable>(Source{}, // source |
| 2097 | "my_var", // name |
| 2098 | ast::StorageClass::kNone, // storage_class |
| 2099 | &vec3, // type |
| 2100 | false, // is_const |
| 2101 | nullptr, // constructor |
| 2102 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 2103 | mod->AddGlobalVariable(var); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 2104 | |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2105 | ast::ExpressionList call_params; |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 2106 | call_params.push_back(create<ast::IdentifierExpression>( |
| 2107 | mod->RegisterSymbol("my_var"), "my_var")); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 2108 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 2109 | ast::CallExpression expr( |
| 2110 | create<ast::IdentifierExpression>(mod->RegisterSymbol(name), name), |
| 2111 | call_params); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 2112 | |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 2113 | // Register the variable |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 2114 | EXPECT_TRUE(td()->Determine()); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 2115 | |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2116 | EXPECT_TRUE(td()->DetermineResultType(&expr)); |
| 2117 | ASSERT_NE(expr.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2118 | EXPECT_TRUE(expr.result_type()->Is<ast::type::Bool>()); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 2119 | } |
| 2120 | INSTANTIATE_TEST_SUITE_P(TypeDeterminerTest, |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2121 | Intrinsic, |
| 2122 | testing::Values("any", "all")); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 2123 | |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2124 | using Intrinsic_FloatMethod = TypeDeterminerTestWithParam<std::string>; |
| 2125 | TEST_P(Intrinsic_FloatMethod, Vector) { |
| 2126 | auto name = GetParam(); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 2127 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2128 | ast::type::F32 f32; |
| 2129 | ast::type::Vector vec3(&f32, 3); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 2130 | |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 2131 | auto* var = |
| 2132 | create<ast::Variable>(Source{}, // source |
| 2133 | "my_var", // name |
| 2134 | ast::StorageClass::kNone, // storage_class |
| 2135 | &vec3, // type |
| 2136 | false, // is_const |
| 2137 | nullptr, // constructor |
| 2138 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 2139 | mod->AddGlobalVariable(var); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 2140 | |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2141 | ast::ExpressionList call_params; |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 2142 | call_params.push_back(create<ast::IdentifierExpression>( |
| 2143 | mod->RegisterSymbol("my_var"), "my_var")); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 2144 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 2145 | ast::CallExpression expr( |
| 2146 | create<ast::IdentifierExpression>(mod->RegisterSymbol(name), name), |
| 2147 | call_params); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 2148 | |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 2149 | // Register the variable |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 2150 | EXPECT_TRUE(td()->Determine()); |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2151 | EXPECT_TRUE(td()->DetermineResultType(&expr)); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 2152 | |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2153 | ASSERT_NE(expr.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2154 | ASSERT_TRUE(expr.result_type()->Is<ast::type::Vector>()); |
Ben Clayton | 8a083ce | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2155 | EXPECT_TRUE(expr.result_type() |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2156 | ->As<ast::type::Vector>() |
Ben Clayton | 8a083ce | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2157 | ->type() |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2158 | ->Is<ast::type::Bool>()); |
| 2159 | EXPECT_EQ(expr.result_type()->As<ast::type::Vector>()->size(), 3u); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 2160 | } |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2161 | |
| 2162 | TEST_P(Intrinsic_FloatMethod, Scalar) { |
| 2163 | auto name = GetParam(); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 2164 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2165 | ast::type::F32 f32; |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 2166 | |
Ben Clayton | 321e5a9 | 2020-12-07 21:08:07 +0000 | [diff] [blame] | 2167 | auto* var = |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 2168 | create<ast::Variable>(Source{}, // source |
| 2169 | "my_var", // name |
| 2170 | ast::StorageClass::kNone, // storage_class |
| 2171 | &f32, // type |
| 2172 | false, // is_const |
| 2173 | nullptr, // constructor |
| 2174 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 2175 | mod->AddGlobalVariable(var); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 2176 | |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2177 | ast::ExpressionList call_params; |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 2178 | call_params.push_back(create<ast::IdentifierExpression>( |
| 2179 | mod->RegisterSymbol("my_var"), "my_var")); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 2180 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 2181 | ast::CallExpression expr( |
| 2182 | create<ast::IdentifierExpression>(mod->RegisterSymbol(name), name), |
| 2183 | call_params); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 2184 | |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 2185 | // Register the variable |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 2186 | EXPECT_TRUE(td()->Determine()); |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2187 | EXPECT_TRUE(td()->DetermineResultType(&expr)); |
| 2188 | ASSERT_NE(expr.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2189 | EXPECT_TRUE(expr.result_type()->Is<ast::type::Bool>()); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 2190 | } |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 2191 | |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2192 | TEST_P(Intrinsic_FloatMethod, MissingParam) { |
| 2193 | auto name = GetParam(); |
| 2194 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2195 | ast::type::F32 f32; |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2196 | |
Ben Clayton | 321e5a9 | 2020-12-07 21:08:07 +0000 | [diff] [blame] | 2197 | auto* var = |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 2198 | create<ast::Variable>(Source{}, // source |
| 2199 | "my_var", // name |
| 2200 | ast::StorageClass::kNone, // storage_class |
| 2201 | &f32, // type |
| 2202 | false, // is_const |
| 2203 | nullptr, // constructor |
| 2204 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 2205 | mod->AddGlobalVariable(var); |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2206 | |
| 2207 | ast::ExpressionList call_params; |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 2208 | ast::CallExpression expr( |
| 2209 | create<ast::IdentifierExpression>(mod->RegisterSymbol(name), name), |
| 2210 | call_params); |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2211 | |
| 2212 | // Register the variable |
| 2213 | EXPECT_TRUE(td()->Determine()); |
| 2214 | EXPECT_FALSE(td()->DetermineResultType(&expr)); |
| 2215 | EXPECT_EQ(td()->error(), "incorrect number of parameters for " + name); |
| 2216 | } |
| 2217 | |
| 2218 | TEST_P(Intrinsic_FloatMethod, TooManyParams) { |
| 2219 | auto name = GetParam(); |
| 2220 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2221 | ast::type::F32 f32; |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2222 | |
Ben Clayton | 321e5a9 | 2020-12-07 21:08:07 +0000 | [diff] [blame] | 2223 | auto* var = |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 2224 | create<ast::Variable>(Source{}, // source |
| 2225 | "my_var", // name |
| 2226 | ast::StorageClass::kNone, // storage_class |
| 2227 | &f32, // type |
| 2228 | false, // is_const |
| 2229 | nullptr, // constructor |
| 2230 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 2231 | mod->AddGlobalVariable(var); |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2232 | |
| 2233 | ast::ExpressionList call_params; |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 2234 | call_params.push_back(create<ast::IdentifierExpression>( |
| 2235 | mod->RegisterSymbol("my_var"), "my_var")); |
| 2236 | call_params.push_back(create<ast::IdentifierExpression>( |
| 2237 | mod->RegisterSymbol("my_var"), "my_var")); |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2238 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 2239 | ast::CallExpression expr( |
| 2240 | create<ast::IdentifierExpression>(mod->RegisterSymbol(name), name), |
| 2241 | call_params); |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2242 | |
| 2243 | // Register the variable |
| 2244 | EXPECT_TRUE(td()->Determine()); |
| 2245 | EXPECT_FALSE(td()->DetermineResultType(&expr)); |
| 2246 | EXPECT_EQ(td()->error(), "incorrect number of parameters for " + name); |
| 2247 | } |
| 2248 | INSTANTIATE_TEST_SUITE_P( |
| 2249 | TypeDeterminerTest, |
| 2250 | Intrinsic_FloatMethod, |
Tomek Ponitka | 4e2d248 | 2020-09-08 16:30:29 +0000 | [diff] [blame] | 2251 | testing::Values("isInf", "isNan", "isFinite", "isNormal")); |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2252 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2253 | enum class Texture { kF32, kI32, kU32 }; |
| 2254 | inline std::ostream& operator<<(std::ostream& out, Texture data) { |
| 2255 | if (data == Texture::kF32) { |
Tomek Ponitka | 1a61fc4 | 2020-08-31 15:24:54 +0000 | [diff] [blame] | 2256 | out << "f32"; |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2257 | } else if (data == Texture::kI32) { |
Tomek Ponitka | 1a61fc4 | 2020-08-31 15:24:54 +0000 | [diff] [blame] | 2258 | out << "i32"; |
| 2259 | } else { |
| 2260 | out << "u32"; |
| 2261 | } |
| 2262 | return out; |
| 2263 | } |
| 2264 | |
| 2265 | struct TextureTestParams { |
| 2266 | ast::type::TextureDimension dim; |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2267 | Texture type = Texture::kF32; |
Tomek Ponitka | 1a61fc4 | 2020-08-31 15:24:54 +0000 | [diff] [blame] | 2268 | ast::type::ImageFormat format = ast::type::ImageFormat::kR16Float; |
| 2269 | }; |
| 2270 | inline std::ostream& operator<<(std::ostream& out, TextureTestParams data) { |
| 2271 | out << data.dim << "_" << data.type; |
| 2272 | return out; |
| 2273 | } |
| 2274 | |
| 2275 | class Intrinsic_TextureOperation |
| 2276 | : public TypeDeterminerTestWithParam<TextureTestParams> { |
| 2277 | public: |
| 2278 | std::unique_ptr<ast::type::Type> get_coords_type( |
| 2279 | ast::type::TextureDimension dim, |
| 2280 | ast::type::Type* type) { |
| 2281 | if (dim == ast::type::TextureDimension::k1d) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2282 | if (type->Is<ast::type::I32>()) { |
| 2283 | return std::make_unique<ast::type::I32>(); |
| 2284 | } else if (type->Is<ast::type::U32>()) { |
| 2285 | return std::make_unique<ast::type::U32>(); |
Tomek Ponitka | 1a61fc4 | 2020-08-31 15:24:54 +0000 | [diff] [blame] | 2286 | } else { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2287 | return std::make_unique<ast::type::F32>(); |
Tomek Ponitka | 1a61fc4 | 2020-08-31 15:24:54 +0000 | [diff] [blame] | 2288 | } |
| 2289 | } else if (dim == ast::type::TextureDimension::k1dArray || |
dan sinclair | d3f75ca | 2020-09-17 03:53:04 +0000 | [diff] [blame] | 2290 | dim == ast::type::TextureDimension::k2d) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2291 | return std::make_unique<ast::type::Vector>(type, 2); |
Tomek Ponitka | 1a61fc4 | 2020-08-31 15:24:54 +0000 | [diff] [blame] | 2292 | } else if (dim == ast::type::TextureDimension::kCubeArray) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2293 | return std::make_unique<ast::type::Vector>(type, 4); |
Tomek Ponitka | 1a61fc4 | 2020-08-31 15:24:54 +0000 | [diff] [blame] | 2294 | } else { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2295 | return std::make_unique<ast::type::Vector>(type, 3); |
Tomek Ponitka | 1a61fc4 | 2020-08-31 15:24:54 +0000 | [diff] [blame] | 2296 | } |
| 2297 | } |
| 2298 | |
| 2299 | void add_call_param(std::string name, |
| 2300 | ast::type::Type* type, |
| 2301 | ast::ExpressionList* call_params) { |
Ben Clayton | 321e5a9 | 2020-12-07 21:08:07 +0000 | [diff] [blame] | 2302 | auto* var = |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 2303 | create<ast::Variable>(Source{}, // source |
| 2304 | name, // name |
| 2305 | ast::StorageClass::kNone, // storage_class |
| 2306 | type, // type |
| 2307 | false, // is_const |
| 2308 | nullptr, // constructor |
| 2309 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 2310 | mod->AddGlobalVariable(var); |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 2311 | call_params->push_back( |
| 2312 | create<ast::IdentifierExpression>(mod->RegisterSymbol(name), name)); |
Tomek Ponitka | 1a61fc4 | 2020-08-31 15:24:54 +0000 | [diff] [blame] | 2313 | } |
| 2314 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2315 | std::unique_ptr<ast::type::Type> subtype(Texture type) { |
| 2316 | if (type == Texture::kF32) { |
| 2317 | return std::make_unique<ast::type::F32>(); |
Tomek Ponitka | 1a61fc4 | 2020-08-31 15:24:54 +0000 | [diff] [blame] | 2318 | } |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2319 | if (type == Texture::kI32) { |
| 2320 | return std::make_unique<ast::type::I32>(); |
Tomek Ponitka | 1a61fc4 | 2020-08-31 15:24:54 +0000 | [diff] [blame] | 2321 | } |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2322 | return std::make_unique<ast::type::U32>(); |
Tomek Ponitka | 1a61fc4 | 2020-08-31 15:24:54 +0000 | [diff] [blame] | 2323 | } |
| 2324 | }; |
| 2325 | |
| 2326 | using Intrinsic_StorageTextureOperation = Intrinsic_TextureOperation; |
| 2327 | TEST_P(Intrinsic_StorageTextureOperation, TextureLoadRo) { |
| 2328 | auto dim = GetParam().dim; |
| 2329 | auto type = GetParam().type; |
| 2330 | auto format = GetParam().format; |
| 2331 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2332 | ast::type::I32 i32; |
Tomek Ponitka | 1a61fc4 | 2020-08-31 15:24:54 +0000 | [diff] [blame] | 2333 | auto coords_type = get_coords_type(dim, &i32); |
| 2334 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2335 | ast::type::Type* texture_type = mod->create<ast::type::StorageTexture>( |
Ben Clayton | 7e4ffa0 | 2020-11-23 19:58:55 +0000 | [diff] [blame] | 2336 | dim, ast::AccessControl::kReadOnly, format); |
Tomek Ponitka | 1a61fc4 | 2020-08-31 15:24:54 +0000 | [diff] [blame] | 2337 | |
| 2338 | ast::ExpressionList call_params; |
| 2339 | |
| 2340 | add_call_param("texture", texture_type, &call_params); |
| 2341 | add_call_param("coords", coords_type.get(), &call_params); |
| 2342 | add_call_param("lod", &i32, &call_params); |
| 2343 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 2344 | ast::CallExpression expr( |
| 2345 | create<ast::IdentifierExpression>(mod->RegisterSymbol("textureLoad"), |
| 2346 | "textureLoad"), |
| 2347 | call_params); |
Tomek Ponitka | 1a61fc4 | 2020-08-31 15:24:54 +0000 | [diff] [blame] | 2348 | |
| 2349 | EXPECT_TRUE(td()->Determine()); |
| 2350 | EXPECT_TRUE(td()->DetermineResultType(&expr)); |
| 2351 | |
| 2352 | ASSERT_NE(expr.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2353 | ASSERT_TRUE(expr.result_type()->Is<ast::type::Vector>()); |
| 2354 | if (type == Texture::kF32) { |
Ben Clayton | 8a083ce | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2355 | EXPECT_TRUE(expr.result_type() |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2356 | ->As<ast::type::Vector>() |
Ben Clayton | 8a083ce | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2357 | ->type() |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2358 | ->Is<ast::type::F32>()); |
| 2359 | } else if (type == Texture::kI32) { |
Ben Clayton | 8a083ce | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2360 | EXPECT_TRUE(expr.result_type() |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2361 | ->As<ast::type::Vector>() |
Ben Clayton | 8a083ce | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2362 | ->type() |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2363 | ->Is<ast::type::I32>()); |
Tomek Ponitka | 1a61fc4 | 2020-08-31 15:24:54 +0000 | [diff] [blame] | 2364 | } else { |
Ben Clayton | 8a083ce | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2365 | EXPECT_TRUE(expr.result_type() |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2366 | ->As<ast::type::Vector>() |
Ben Clayton | 8a083ce | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2367 | ->type() |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2368 | ->Is<ast::type::U32>()); |
Tomek Ponitka | 1a61fc4 | 2020-08-31 15:24:54 +0000 | [diff] [blame] | 2369 | } |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2370 | EXPECT_EQ(expr.result_type()->As<ast::type::Vector>()->size(), 4u); |
Tomek Ponitka | 1a61fc4 | 2020-08-31 15:24:54 +0000 | [diff] [blame] | 2371 | } |
| 2372 | |
| 2373 | INSTANTIATE_TEST_SUITE_P( |
| 2374 | TypeDeterminerTest, |
| 2375 | Intrinsic_StorageTextureOperation, |
| 2376 | testing::Values( |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2377 | TextureTestParams{ast::type::TextureDimension::k1d, Texture::kF32, |
Tomek Ponitka | 1a61fc4 | 2020-08-31 15:24:54 +0000 | [diff] [blame] | 2378 | ast::type::ImageFormat::kR16Float}, |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2379 | TextureTestParams{ast::type::TextureDimension::k1d, Texture::kI32, |
Tomek Ponitka | 1a61fc4 | 2020-08-31 15:24:54 +0000 | [diff] [blame] | 2380 | ast::type::ImageFormat::kR16Sint}, |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2381 | TextureTestParams{ast::type::TextureDimension::k1d, Texture::kF32, |
Tomek Ponitka | 1a61fc4 | 2020-08-31 15:24:54 +0000 | [diff] [blame] | 2382 | ast::type::ImageFormat::kR8Unorm}, |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2383 | TextureTestParams{ast::type::TextureDimension::k1dArray, Texture::kF32, |
Tomek Ponitka | 1a61fc4 | 2020-08-31 15:24:54 +0000 | [diff] [blame] | 2384 | ast::type::ImageFormat::kR16Float}, |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2385 | TextureTestParams{ast::type::TextureDimension::k1dArray, Texture::kI32, |
Tomek Ponitka | 1a61fc4 | 2020-08-31 15:24:54 +0000 | [diff] [blame] | 2386 | ast::type::ImageFormat::kR16Sint}, |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2387 | TextureTestParams{ast::type::TextureDimension::k1dArray, Texture::kF32, |
Tomek Ponitka | 1a61fc4 | 2020-08-31 15:24:54 +0000 | [diff] [blame] | 2388 | ast::type::ImageFormat::kR8Unorm}, |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2389 | TextureTestParams{ast::type::TextureDimension::k2d, Texture::kF32, |
Tomek Ponitka | 1a61fc4 | 2020-08-31 15:24:54 +0000 | [diff] [blame] | 2390 | ast::type::ImageFormat::kR16Float}, |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2391 | TextureTestParams{ast::type::TextureDimension::k2d, Texture::kI32, |
Tomek Ponitka | 1a61fc4 | 2020-08-31 15:24:54 +0000 | [diff] [blame] | 2392 | ast::type::ImageFormat::kR16Sint}, |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2393 | TextureTestParams{ast::type::TextureDimension::k2d, Texture::kF32, |
| 2394 | ast::type::ImageFormat::kR8Unorm}, |
| 2395 | TextureTestParams{ast::type::TextureDimension::k2dArray, Texture::kF32, |
| 2396 | ast::type::ImageFormat::kR16Float}, |
| 2397 | TextureTestParams{ast::type::TextureDimension::k2dArray, Texture::kI32, |
| 2398 | ast::type::ImageFormat::kR16Sint}, |
| 2399 | TextureTestParams{ast::type::TextureDimension::k2dArray, Texture::kF32, |
| 2400 | ast::type::ImageFormat::kR8Unorm}, |
| 2401 | TextureTestParams{ast::type::TextureDimension::k3d, Texture::kF32, |
| 2402 | ast::type::ImageFormat::kR16Float}, |
| 2403 | TextureTestParams{ast::type::TextureDimension::k3d, Texture::kI32, |
| 2404 | ast::type::ImageFormat::kR16Sint}, |
| 2405 | TextureTestParams{ast::type::TextureDimension::k3d, Texture::kF32, |
Tomek Ponitka | 1a61fc4 | 2020-08-31 15:24:54 +0000 | [diff] [blame] | 2406 | ast::type::ImageFormat::kR8Unorm})); |
| 2407 | |
| 2408 | using Intrinsic_SampledTextureOperation = Intrinsic_TextureOperation; |
| 2409 | TEST_P(Intrinsic_SampledTextureOperation, TextureLoadSampled) { |
| 2410 | auto dim = GetParam().dim; |
| 2411 | auto type = GetParam().type; |
| 2412 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2413 | ast::type::I32 i32; |
Tomek Ponitka | 1a61fc4 | 2020-08-31 15:24:54 +0000 | [diff] [blame] | 2414 | std::unique_ptr<ast::type::Type> s = subtype(type); |
| 2415 | auto coords_type = get_coords_type(dim, &i32); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2416 | auto texture_type = std::make_unique<ast::type::SampledTexture>(dim, s.get()); |
Tomek Ponitka | 1a61fc4 | 2020-08-31 15:24:54 +0000 | [diff] [blame] | 2417 | |
| 2418 | ast::ExpressionList call_params; |
| 2419 | |
| 2420 | add_call_param("texture", texture_type.get(), &call_params); |
| 2421 | add_call_param("coords", coords_type.get(), &call_params); |
| 2422 | add_call_param("lod", &i32, &call_params); |
| 2423 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 2424 | ast::CallExpression expr( |
| 2425 | create<ast::IdentifierExpression>(mod->RegisterSymbol("textureLoad"), |
| 2426 | "textureLoad"), |
| 2427 | call_params); |
Tomek Ponitka | 1a61fc4 | 2020-08-31 15:24:54 +0000 | [diff] [blame] | 2428 | |
| 2429 | EXPECT_TRUE(td()->Determine()); |
| 2430 | EXPECT_TRUE(td()->DetermineResultType(&expr)); |
| 2431 | |
| 2432 | ASSERT_NE(expr.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2433 | ASSERT_TRUE(expr.result_type()->Is<ast::type::Vector>()); |
| 2434 | if (type == Texture::kF32) { |
Ben Clayton | 8a083ce | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2435 | EXPECT_TRUE(expr.result_type() |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2436 | ->As<ast::type::Vector>() |
Ben Clayton | 8a083ce | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2437 | ->type() |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2438 | ->Is<ast::type::F32>()); |
| 2439 | } else if (type == Texture::kI32) { |
Ben Clayton | 8a083ce | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2440 | EXPECT_TRUE(expr.result_type() |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2441 | ->As<ast::type::Vector>() |
Ben Clayton | 8a083ce | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2442 | ->type() |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2443 | ->Is<ast::type::I32>()); |
Tomek Ponitka | 1a61fc4 | 2020-08-31 15:24:54 +0000 | [diff] [blame] | 2444 | } else { |
Ben Clayton | 8a083ce | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2445 | EXPECT_TRUE(expr.result_type() |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2446 | ->As<ast::type::Vector>() |
Ben Clayton | 8a083ce | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2447 | ->type() |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2448 | ->Is<ast::type::U32>()); |
Tomek Ponitka | 1a61fc4 | 2020-08-31 15:24:54 +0000 | [diff] [blame] | 2449 | } |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2450 | EXPECT_EQ(expr.result_type()->As<ast::type::Vector>()->size(), 4u); |
Tomek Ponitka | 1a61fc4 | 2020-08-31 15:24:54 +0000 | [diff] [blame] | 2451 | } |
| 2452 | |
Tomek Ponitka | 1a61fc4 | 2020-08-31 15:24:54 +0000 | [diff] [blame] | 2453 | INSTANTIATE_TEST_SUITE_P( |
| 2454 | TypeDeterminerTest, |
| 2455 | Intrinsic_SampledTextureOperation, |
Tomek Ponitka | 1a61fc4 | 2020-08-31 15:24:54 +0000 | [diff] [blame] | 2456 | testing::Values(TextureTestParams{ast::type::TextureDimension::k2d}, |
| 2457 | TextureTestParams{ast::type::TextureDimension::k2dArray}, |
| 2458 | TextureTestParams{ast::type::TextureDimension::kCube}, |
| 2459 | TextureTestParams{ |
| 2460 | ast::type::TextureDimension::kCubeArray})); |
| 2461 | |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2462 | TEST_F(TypeDeterminerTest, Intrinsic_Dot) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2463 | ast::type::F32 f32; |
| 2464 | ast::type::Vector vec3(&f32, 3); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 2465 | |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 2466 | auto* var = |
| 2467 | create<ast::Variable>(Source{}, // source |
| 2468 | "my_var", // name |
| 2469 | ast::StorageClass::kNone, // storage_class |
| 2470 | &vec3, // type |
| 2471 | false, // is_const |
| 2472 | nullptr, // constructor |
| 2473 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 2474 | mod->AddGlobalVariable(var); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 2475 | |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2476 | ast::ExpressionList call_params; |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 2477 | call_params.push_back(create<ast::IdentifierExpression>( |
| 2478 | mod->RegisterSymbol("my_var"), "my_var")); |
| 2479 | call_params.push_back(create<ast::IdentifierExpression>( |
| 2480 | mod->RegisterSymbol("my_var"), "my_var")); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 2481 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 2482 | ast::CallExpression expr( |
| 2483 | create<ast::IdentifierExpression>(mod->RegisterSymbol("dot"), "dot"), |
| 2484 | call_params); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 2485 | |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 2486 | // Register the variable |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 2487 | EXPECT_TRUE(td()->Determine()); |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2488 | EXPECT_TRUE(td()->DetermineResultType(&expr)); |
| 2489 | ASSERT_NE(expr.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2490 | EXPECT_TRUE(expr.result_type()->Is<ast::type::F32>()); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 2491 | } |
| 2492 | |
dan sinclair | 16a2ea1 | 2020-07-21 17:44:44 +0000 | [diff] [blame] | 2493 | TEST_F(TypeDeterminerTest, Intrinsic_Select) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2494 | ast::type::F32 f32; |
| 2495 | ast::type::Bool bool_type; |
| 2496 | ast::type::Vector vec3(&f32, 3); |
| 2497 | ast::type::Vector bool_vec3(&bool_type, 3); |
dan sinclair | 16a2ea1 | 2020-07-21 17:44:44 +0000 | [diff] [blame] | 2498 | |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 2499 | auto* var = |
| 2500 | create<ast::Variable>(Source{}, // source |
| 2501 | "my_var", // name |
| 2502 | ast::StorageClass::kNone, // storage_class |
| 2503 | &vec3, // type |
| 2504 | false, // is_const |
| 2505 | nullptr, // constructor |
| 2506 | ast::VariableDecorationList{}); // decorations |
| 2507 | auto* bool_var = |
| 2508 | create<ast::Variable>(Source{}, // source |
| 2509 | "bool_var", // name |
| 2510 | ast::StorageClass::kNone, // storage_class |
| 2511 | &bool_vec3, // type |
| 2512 | false, // is_const |
| 2513 | nullptr, // constructor |
| 2514 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 2515 | mod->AddGlobalVariable(var); |
| 2516 | mod->AddGlobalVariable(bool_var); |
dan sinclair | 16a2ea1 | 2020-07-21 17:44:44 +0000 | [diff] [blame] | 2517 | |
| 2518 | ast::ExpressionList call_params; |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 2519 | call_params.push_back(create<ast::IdentifierExpression>( |
| 2520 | mod->RegisterSymbol("my_var"), "my_var")); |
| 2521 | call_params.push_back(create<ast::IdentifierExpression>( |
| 2522 | mod->RegisterSymbol("my_var"), "my_var")); |
| 2523 | call_params.push_back(create<ast::IdentifierExpression>( |
| 2524 | mod->RegisterSymbol("bool_var"), "bool_var")); |
dan sinclair | 16a2ea1 | 2020-07-21 17:44:44 +0000 | [diff] [blame] | 2525 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 2526 | ast::CallExpression expr(create<ast::IdentifierExpression>( |
| 2527 | mod->RegisterSymbol("select"), "select"), |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 2528 | call_params); |
dan sinclair | 16a2ea1 | 2020-07-21 17:44:44 +0000 | [diff] [blame] | 2529 | |
| 2530 | // Register the variable |
| 2531 | EXPECT_TRUE(td()->Determine()); |
| 2532 | EXPECT_TRUE(td()->DetermineResultType(&expr)) << td()->error(); |
| 2533 | ASSERT_NE(expr.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2534 | EXPECT_TRUE(expr.result_type()->Is<ast::type::Vector>()); |
| 2535 | EXPECT_EQ(expr.result_type()->As<ast::type::Vector>()->size(), 3u); |
Ben Clayton | 8a083ce | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2536 | EXPECT_TRUE(expr.result_type() |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2537 | ->As<ast::type::Vector>() |
Ben Clayton | 8a083ce | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2538 | ->type() |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2539 | ->Is<ast::type::F32>()); |
dan sinclair | 16a2ea1 | 2020-07-21 17:44:44 +0000 | [diff] [blame] | 2540 | } |
| 2541 | |
| 2542 | TEST_F(TypeDeterminerTest, Intrinsic_Select_TooFewParams) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2543 | ast::type::F32 f32; |
| 2544 | ast::type::Vector vec3(&f32, 3); |
dan sinclair | 16a2ea1 | 2020-07-21 17:44:44 +0000 | [diff] [blame] | 2545 | |
Ben Clayton | 321e5a9 | 2020-12-07 21:08:07 +0000 | [diff] [blame] | 2546 | auto* var = |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 2547 | create<ast::Variable>(Source{}, // source |
| 2548 | "v", // name |
| 2549 | ast::StorageClass::kNone, // storage_class |
| 2550 | &vec3, // type |
| 2551 | false, // is_const |
| 2552 | nullptr, // constructor |
| 2553 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 2554 | mod->AddGlobalVariable(var); |
dan sinclair | 16a2ea1 | 2020-07-21 17:44:44 +0000 | [diff] [blame] | 2555 | |
| 2556 | ast::ExpressionList call_params; |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 2557 | call_params.push_back( |
| 2558 | create<ast::IdentifierExpression>(mod->RegisterSymbol("v"), "v")); |
dan sinclair | 16a2ea1 | 2020-07-21 17:44:44 +0000 | [diff] [blame] | 2559 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 2560 | ast::CallExpression expr(create<ast::IdentifierExpression>( |
| 2561 | mod->RegisterSymbol("select"), "select"), |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 2562 | call_params); |
dan sinclair | 16a2ea1 | 2020-07-21 17:44:44 +0000 | [diff] [blame] | 2563 | |
| 2564 | // Register the variable |
| 2565 | EXPECT_TRUE(td()->Determine()); |
| 2566 | EXPECT_FALSE(td()->DetermineResultType(&expr)); |
| 2567 | EXPECT_EQ(td()->error(), |
| 2568 | "incorrect number of parameters for select expected 3 got 1"); |
| 2569 | } |
| 2570 | |
| 2571 | TEST_F(TypeDeterminerTest, Intrinsic_Select_TooManyParams) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2572 | ast::type::F32 f32; |
| 2573 | ast::type::Vector vec3(&f32, 3); |
dan sinclair | 16a2ea1 | 2020-07-21 17:44:44 +0000 | [diff] [blame] | 2574 | |
Ben Clayton | 321e5a9 | 2020-12-07 21:08:07 +0000 | [diff] [blame] | 2575 | auto* var = |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 2576 | create<ast::Variable>(Source{}, // source |
| 2577 | "v", // name |
| 2578 | ast::StorageClass::kNone, // storage_class |
| 2579 | &vec3, // type |
| 2580 | false, // is_const |
| 2581 | nullptr, // constructor |
| 2582 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 2583 | mod->AddGlobalVariable(var); |
dan sinclair | 16a2ea1 | 2020-07-21 17:44:44 +0000 | [diff] [blame] | 2584 | |
| 2585 | ast::ExpressionList call_params; |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 2586 | call_params.push_back( |
| 2587 | create<ast::IdentifierExpression>(mod->RegisterSymbol("v"), "v")); |
| 2588 | call_params.push_back( |
| 2589 | create<ast::IdentifierExpression>(mod->RegisterSymbol("v"), "v")); |
| 2590 | call_params.push_back( |
| 2591 | create<ast::IdentifierExpression>(mod->RegisterSymbol("v"), "v")); |
| 2592 | call_params.push_back( |
| 2593 | create<ast::IdentifierExpression>(mod->RegisterSymbol("v"), "v")); |
dan sinclair | 16a2ea1 | 2020-07-21 17:44:44 +0000 | [diff] [blame] | 2594 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 2595 | ast::CallExpression expr(create<ast::IdentifierExpression>( |
| 2596 | mod->RegisterSymbol("select"), "select"), |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 2597 | call_params); |
dan sinclair | 16a2ea1 | 2020-07-21 17:44:44 +0000 | [diff] [blame] | 2598 | |
| 2599 | // Register the variable |
| 2600 | EXPECT_TRUE(td()->Determine()); |
| 2601 | EXPECT_FALSE(td()->DetermineResultType(&expr)); |
| 2602 | EXPECT_EQ(td()->error(), |
| 2603 | "incorrect number of parameters for select expected 3 got 4"); |
| 2604 | } |
| 2605 | |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2606 | TEST_F(TypeDeterminerTest, Intrinsic_OuterProduct) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2607 | ast::type::F32 f32; |
| 2608 | ast::type::Vector vec3(&f32, 3); |
| 2609 | ast::type::Vector vec2(&f32, 2); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 2610 | |
Ben Clayton | 321e5a9 | 2020-12-07 21:08:07 +0000 | [diff] [blame] | 2611 | auto* var1 = |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 2612 | create<ast::Variable>(Source{}, // source |
| 2613 | "v3", // name |
| 2614 | ast::StorageClass::kNone, // storage_class |
| 2615 | &vec3, // type |
| 2616 | false, // is_const |
| 2617 | nullptr, // constructor |
| 2618 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 321e5a9 | 2020-12-07 21:08:07 +0000 | [diff] [blame] | 2619 | auto* var2 = |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 2620 | create<ast::Variable>(Source{}, // source |
| 2621 | "v2", // name |
| 2622 | ast::StorageClass::kNone, // storage_class |
| 2623 | &vec2, // type |
| 2624 | false, // is_const |
| 2625 | nullptr, // constructor |
| 2626 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 2627 | mod->AddGlobalVariable(var1); |
| 2628 | mod->AddGlobalVariable(var2); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 2629 | |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2630 | ast::ExpressionList call_params; |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 2631 | call_params.push_back( |
| 2632 | create<ast::IdentifierExpression>(mod->RegisterSymbol("v3"), "v3")); |
| 2633 | call_params.push_back( |
| 2634 | create<ast::IdentifierExpression>(mod->RegisterSymbol("v2"), "v2")); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 2635 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 2636 | ast::CallExpression expr( |
| 2637 | create<ast::IdentifierExpression>(mod->RegisterSymbol("outerProduct"), |
| 2638 | "outerProduct"), |
| 2639 | call_params); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 2640 | |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 2641 | // Register the variable |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 2642 | EXPECT_TRUE(td()->Determine()); |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2643 | EXPECT_TRUE(td()->DetermineResultType(&expr)); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 2644 | |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2645 | ASSERT_NE(expr.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2646 | ASSERT_TRUE(expr.result_type()->Is<ast::type::Matrix>()); |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2647 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2648 | auto* mat = expr.result_type()->As<ast::type::Matrix>(); |
| 2649 | EXPECT_TRUE(mat->type()->Is<ast::type::F32>()); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 2650 | EXPECT_EQ(mat->rows(), 3u); |
| 2651 | EXPECT_EQ(mat->columns(), 2u); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 2652 | } |
| 2653 | |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2654 | TEST_F(TypeDeterminerTest, Intrinsic_OuterProduct_TooFewParams) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2655 | ast::type::F32 f32; |
| 2656 | ast::type::Vector vec3(&f32, 3); |
| 2657 | ast::type::Vector vec2(&f32, 2); |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2658 | |
Ben Clayton | 321e5a9 | 2020-12-07 21:08:07 +0000 | [diff] [blame] | 2659 | auto* var2 = |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 2660 | create<ast::Variable>(Source{}, // source |
| 2661 | "v2", // name |
| 2662 | ast::StorageClass::kNone, // storage_class |
| 2663 | &vec2, // type |
| 2664 | false, // is_const |
| 2665 | nullptr, // constructor |
| 2666 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 2667 | mod->AddGlobalVariable(var2); |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2668 | |
| 2669 | ast::ExpressionList call_params; |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 2670 | call_params.push_back( |
| 2671 | create<ast::IdentifierExpression>(mod->RegisterSymbol("v2"), "v2")); |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2672 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 2673 | ast::CallExpression expr( |
| 2674 | create<ast::IdentifierExpression>(mod->RegisterSymbol("outerProduct"), |
| 2675 | "outerProduct"), |
| 2676 | call_params); |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2677 | |
| 2678 | // Register the variable |
| 2679 | EXPECT_TRUE(td()->Determine()); |
| 2680 | EXPECT_FALSE(td()->DetermineResultType(&expr)); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 2681 | EXPECT_EQ(td()->error(), "incorrect number of parameters for outerProduct"); |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2682 | } |
| 2683 | |
| 2684 | TEST_F(TypeDeterminerTest, Intrinsic_OuterProduct_TooManyParams) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2685 | ast::type::F32 f32; |
| 2686 | ast::type::Vector vec3(&f32, 3); |
| 2687 | ast::type::Vector vec2(&f32, 2); |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2688 | |
Ben Clayton | 321e5a9 | 2020-12-07 21:08:07 +0000 | [diff] [blame] | 2689 | auto* var2 = |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 2690 | create<ast::Variable>(Source{}, // source |
| 2691 | "v2", // name |
| 2692 | ast::StorageClass::kNone, // storage_class |
| 2693 | &vec2, // type |
| 2694 | false, // is_const |
| 2695 | nullptr, // constructor |
| 2696 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 2697 | mod->AddGlobalVariable(var2); |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2698 | |
| 2699 | ast::ExpressionList call_params; |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 2700 | call_params.push_back( |
| 2701 | create<ast::IdentifierExpression>(mod->RegisterSymbol("v2"), "v2")); |
| 2702 | call_params.push_back( |
| 2703 | create<ast::IdentifierExpression>(mod->RegisterSymbol("v2"), "v2")); |
| 2704 | call_params.push_back( |
| 2705 | create<ast::IdentifierExpression>(mod->RegisterSymbol("v2"), "v2")); |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2706 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 2707 | ast::CallExpression expr( |
| 2708 | create<ast::IdentifierExpression>(mod->RegisterSymbol("outerProduct"), |
| 2709 | "outerProduct"), |
| 2710 | call_params); |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2711 | |
| 2712 | // Register the variable |
| 2713 | EXPECT_TRUE(td()->Determine()); |
| 2714 | EXPECT_FALSE(td()->DetermineResultType(&expr)); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 2715 | EXPECT_EQ(td()->error(), "incorrect number of parameters for outerProduct"); |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2716 | } |
| 2717 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 2718 | using UnaryOpExpressionTest = TypeDeterminerTestWithParam<ast::UnaryOp>; |
dan sinclair | 0e25762 | 2020-04-07 19:27:11 +0000 | [diff] [blame] | 2719 | TEST_P(UnaryOpExpressionTest, Expr_UnaryOp) { |
| 2720 | auto op = GetParam(); |
| 2721 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2722 | ast::type::F32 f32; |
dan sinclair | 0e25762 | 2020-04-07 19:27:11 +0000 | [diff] [blame] | 2723 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2724 | ast::type::Vector vec4(&f32, 4); |
dan sinclair | 0e25762 | 2020-04-07 19:27:11 +0000 | [diff] [blame] | 2725 | |
Ben Clayton | 321e5a9 | 2020-12-07 21:08:07 +0000 | [diff] [blame] | 2726 | auto* var = |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 2727 | create<ast::Variable>(Source{}, // source |
| 2728 | "ident", // name |
| 2729 | ast::StorageClass::kNone, // storage_class |
| 2730 | &vec4, // type |
| 2731 | false, // is_const |
| 2732 | nullptr, // constructor |
| 2733 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 2734 | mod->AddGlobalVariable(var); |
dan sinclair | 0e25762 | 2020-04-07 19:27:11 +0000 | [diff] [blame] | 2735 | |
| 2736 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 2737 | EXPECT_TRUE(td()->Determine()); |
dan sinclair | 0e25762 | 2020-04-07 19:27:11 +0000 | [diff] [blame] | 2738 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 2739 | ast::UnaryOpExpression der(op, create<ast::IdentifierExpression>( |
| 2740 | mod->RegisterSymbol("ident"), "ident")); |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 2741 | EXPECT_TRUE(td()->DetermineResultType(&der)); |
dan sinclair | 0e25762 | 2020-04-07 19:27:11 +0000 | [diff] [blame] | 2742 | ASSERT_NE(der.result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2743 | ASSERT_TRUE(der.result_type()->Is<ast::type::Vector>()); |
| 2744 | EXPECT_TRUE( |
| 2745 | der.result_type()->As<ast::type::Vector>()->type()->Is<ast::type::F32>()); |
| 2746 | EXPECT_EQ(der.result_type()->As<ast::type::Vector>()->size(), 4u); |
dan sinclair | 0e25762 | 2020-04-07 19:27:11 +0000 | [diff] [blame] | 2747 | } |
| 2748 | INSTANTIATE_TEST_SUITE_P(TypeDeterminerTest, |
| 2749 | UnaryOpExpressionTest, |
| 2750 | testing::Values(ast::UnaryOp::kNegation, |
| 2751 | ast::UnaryOp::kNot)); |
| 2752 | |
dan sinclair | ee8ae04 | 2020-04-08 19:58:20 +0000 | [diff] [blame] | 2753 | TEST_F(TypeDeterminerTest, StorageClass_SetsIfMissing) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2754 | ast::type::I32 i32; |
dan sinclair | ee8ae04 | 2020-04-08 19:58:20 +0000 | [diff] [blame] | 2755 | |
Ben Clayton | 321e5a9 | 2020-12-07 21:08:07 +0000 | [diff] [blame] | 2756 | auto* var = |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 2757 | create<ast::Variable>(Source{}, // source |
| 2758 | "var", // name |
| 2759 | ast::StorageClass::kNone, // storage_class |
| 2760 | &i32, // type |
| 2761 | false, // is_const |
| 2762 | nullptr, // constructor |
| 2763 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 2764 | auto* stmt = create<ast::VariableDeclStatement>(var); |
dan sinclair | ee8ae04 | 2020-04-08 19:58:20 +0000 | [diff] [blame] | 2765 | |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 2766 | auto* body = create<ast::BlockStatement>(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 2767 | body->append(stmt); |
dan sinclair | a41132f | 2020-12-11 18:24:53 +0000 | [diff] [blame] | 2768 | auto* func = create<ast::Function>(Source{}, mod->RegisterSymbol("func"), |
| 2769 | "func", ast::VariableList{}, &i32, body, |
| 2770 | ast::FunctionDecorationList{}); |
dan sinclair | ee8ae04 | 2020-04-08 19:58:20 +0000 | [diff] [blame] | 2771 | |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 2772 | mod->AddFunction(func); |
dan sinclair | ee8ae04 | 2020-04-08 19:58:20 +0000 | [diff] [blame] | 2773 | |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 2774 | EXPECT_TRUE(td()->Determine()) << td()->error(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 2775 | EXPECT_EQ(var->storage_class(), ast::StorageClass::kFunction); |
dan sinclair | ee8ae04 | 2020-04-08 19:58:20 +0000 | [diff] [blame] | 2776 | } |
| 2777 | |
| 2778 | TEST_F(TypeDeterminerTest, StorageClass_DoesNotSetOnConst) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2779 | ast::type::I32 i32; |
dan sinclair | ee8ae04 | 2020-04-08 19:58:20 +0000 | [diff] [blame] | 2780 | |
Ben Clayton | 321e5a9 | 2020-12-07 21:08:07 +0000 | [diff] [blame] | 2781 | auto* var = |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 2782 | create<ast::Variable>(Source{}, // source |
| 2783 | "var", // name |
| 2784 | ast::StorageClass::kNone, // storage_class |
| 2785 | &i32, // type |
| 2786 | true, // is_const |
| 2787 | nullptr, // constructor |
| 2788 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 2789 | auto* stmt = create<ast::VariableDeclStatement>(var); |
dan sinclair | ee8ae04 | 2020-04-08 19:58:20 +0000 | [diff] [blame] | 2790 | |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 2791 | auto* body = create<ast::BlockStatement>(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 2792 | body->append(stmt); |
dan sinclair | a41132f | 2020-12-11 18:24:53 +0000 | [diff] [blame] | 2793 | auto* func = create<ast::Function>(Source{}, mod->RegisterSymbol("func"), |
| 2794 | "func", ast::VariableList{}, &i32, body, |
| 2795 | ast::FunctionDecorationList{}); |
dan sinclair | ee8ae04 | 2020-04-08 19:58:20 +0000 | [diff] [blame] | 2796 | |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 2797 | mod->AddFunction(func); |
dan sinclair | ee8ae04 | 2020-04-08 19:58:20 +0000 | [diff] [blame] | 2798 | |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 2799 | EXPECT_TRUE(td()->Determine()) << td()->error(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 2800 | EXPECT_EQ(var->storage_class(), ast::StorageClass::kNone); |
dan sinclair | ee8ae04 | 2020-04-08 19:58:20 +0000 | [diff] [blame] | 2801 | } |
| 2802 | |
| 2803 | TEST_F(TypeDeterminerTest, StorageClass_NonFunctionClassError) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2804 | ast::type::I32 i32; |
dan sinclair | ee8ae04 | 2020-04-08 19:58:20 +0000 | [diff] [blame] | 2805 | |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 2806 | auto* var = |
| 2807 | create<ast::Variable>(Source{}, // source |
| 2808 | "var", // name |
| 2809 | ast::StorageClass::kWorkgroup, // storage_class |
| 2810 | &i32, // type |
| 2811 | false, // is_const |
| 2812 | nullptr, // constructor |
| 2813 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 2814 | auto* stmt = create<ast::VariableDeclStatement>(var); |
dan sinclair | ee8ae04 | 2020-04-08 19:58:20 +0000 | [diff] [blame] | 2815 | |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 2816 | auto* body = create<ast::BlockStatement>(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 2817 | body->append(stmt); |
dan sinclair | a41132f | 2020-12-11 18:24:53 +0000 | [diff] [blame] | 2818 | auto* func = create<ast::Function>(Source{}, mod->RegisterSymbol("func"), |
| 2819 | "func", ast::VariableList{}, &i32, body, |
| 2820 | ast::FunctionDecorationList{}); |
dan sinclair | ee8ae04 | 2020-04-08 19:58:20 +0000 | [diff] [blame] | 2821 | |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 2822 | mod->AddFunction(func); |
dan sinclair | ee8ae04 | 2020-04-08 19:58:20 +0000 | [diff] [blame] | 2823 | |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 2824 | EXPECT_FALSE(td()->Determine()); |
dan sinclair | ee8ae04 | 2020-04-08 19:58:20 +0000 | [diff] [blame] | 2825 | EXPECT_EQ(td()->error(), |
| 2826 | "function variable has a non-function storage class"); |
| 2827 | } |
| 2828 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 2829 | struct IntrinsicData { |
dan sinclair | ca1723e | 2020-04-20 15:47:55 +0000 | [diff] [blame] | 2830 | const char* name; |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 2831 | ast::Intrinsic intrinsic; |
dan sinclair | ca1723e | 2020-04-20 15:47:55 +0000 | [diff] [blame] | 2832 | }; |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 2833 | inline std::ostream& operator<<(std::ostream& out, IntrinsicData data) { |
dan sinclair | ca1723e | 2020-04-20 15:47:55 +0000 | [diff] [blame] | 2834 | out << data.name; |
| 2835 | return out; |
| 2836 | } |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 2837 | using IntrinsicDataTest = TypeDeterminerTestWithParam<IntrinsicData>; |
| 2838 | TEST_P(IntrinsicDataTest, Lookup) { |
| 2839 | auto param = GetParam(); |
dan sinclair | ca1723e | 2020-04-20 15:47:55 +0000 | [diff] [blame] | 2840 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 2841 | ast::IdentifierExpression ident(mod->RegisterSymbol(param.name), param.name); |
dan sinclair | ff267ca | 2020-10-14 18:26:31 +0000 | [diff] [blame] | 2842 | EXPECT_TRUE(td()->SetIntrinsicIfNeeded(&ident)); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 2843 | EXPECT_EQ(ident.intrinsic(), param.intrinsic); |
| 2844 | EXPECT_TRUE(ident.IsIntrinsic()); |
| 2845 | } |
| 2846 | INSTANTIATE_TEST_SUITE_P( |
| 2847 | TypeDeterminerTest, |
| 2848 | IntrinsicDataTest, |
| 2849 | testing::Values( |
| 2850 | IntrinsicData{"abs", ast::Intrinsic::kAbs}, |
| 2851 | IntrinsicData{"acos", ast::Intrinsic::kAcos}, |
| 2852 | IntrinsicData{"all", ast::Intrinsic::kAll}, |
| 2853 | IntrinsicData{"any", ast::Intrinsic::kAny}, |
dan sinclair | 007dc42 | 2020-10-08 17:01:55 +0000 | [diff] [blame] | 2854 | IntrinsicData{"arrayLength", ast::Intrinsic::kArrayLength}, |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 2855 | IntrinsicData{"asin", ast::Intrinsic::kAsin}, |
| 2856 | IntrinsicData{"atan", ast::Intrinsic::kAtan}, |
| 2857 | IntrinsicData{"atan2", ast::Intrinsic::kAtan2}, |
| 2858 | IntrinsicData{"ceil", ast::Intrinsic::kCeil}, |
| 2859 | IntrinsicData{"clamp", ast::Intrinsic::kClamp}, |
| 2860 | IntrinsicData{"cos", ast::Intrinsic::kCos}, |
| 2861 | IntrinsicData{"cosh", ast::Intrinsic::kCosh}, |
| 2862 | IntrinsicData{"countOneBits", ast::Intrinsic::kCountOneBits}, |
| 2863 | IntrinsicData{"cross", ast::Intrinsic::kCross}, |
| 2864 | IntrinsicData{"determinant", ast::Intrinsic::kDeterminant}, |
| 2865 | IntrinsicData{"distance", ast::Intrinsic::kDistance}, |
| 2866 | IntrinsicData{"dot", ast::Intrinsic::kDot}, |
| 2867 | IntrinsicData{"dpdx", ast::Intrinsic::kDpdx}, |
| 2868 | IntrinsicData{"dpdxCoarse", ast::Intrinsic::kDpdxCoarse}, |
| 2869 | IntrinsicData{"dpdxFine", ast::Intrinsic::kDpdxFine}, |
| 2870 | IntrinsicData{"dpdy", ast::Intrinsic::kDpdy}, |
| 2871 | IntrinsicData{"dpdyCoarse", ast::Intrinsic::kDpdyCoarse}, |
| 2872 | IntrinsicData{"dpdyFine", ast::Intrinsic::kDpdyFine}, |
| 2873 | IntrinsicData{"exp", ast::Intrinsic::kExp}, |
| 2874 | IntrinsicData{"exp2", ast::Intrinsic::kExp2}, |
| 2875 | IntrinsicData{"faceForward", ast::Intrinsic::kFaceForward}, |
| 2876 | IntrinsicData{"floor", ast::Intrinsic::kFloor}, |
| 2877 | IntrinsicData{"fma", ast::Intrinsic::kFma}, |
| 2878 | IntrinsicData{"fract", ast::Intrinsic::kFract}, |
| 2879 | IntrinsicData{"frexp", ast::Intrinsic::kFrexp}, |
| 2880 | IntrinsicData{"fwidth", ast::Intrinsic::kFwidth}, |
| 2881 | IntrinsicData{"fwidthCoarse", ast::Intrinsic::kFwidthCoarse}, |
| 2882 | IntrinsicData{"fwidthFine", ast::Intrinsic::kFwidthFine}, |
| 2883 | IntrinsicData{"inverseSqrt", ast::Intrinsic::kInverseSqrt}, |
| 2884 | IntrinsicData{"isFinite", ast::Intrinsic::kIsFinite}, |
| 2885 | IntrinsicData{"isInf", ast::Intrinsic::kIsInf}, |
| 2886 | IntrinsicData{"isNan", ast::Intrinsic::kIsNan}, |
| 2887 | IntrinsicData{"isNormal", ast::Intrinsic::kIsNormal}, |
| 2888 | IntrinsicData{"ldexp", ast::Intrinsic::kLdexp}, |
| 2889 | IntrinsicData{"length", ast::Intrinsic::kLength}, |
| 2890 | IntrinsicData{"log", ast::Intrinsic::kLog}, |
| 2891 | IntrinsicData{"log2", ast::Intrinsic::kLog2}, |
| 2892 | IntrinsicData{"max", ast::Intrinsic::kMax}, |
| 2893 | IntrinsicData{"min", ast::Intrinsic::kMin}, |
| 2894 | IntrinsicData{"mix", ast::Intrinsic::kMix}, |
| 2895 | IntrinsicData{"modf", ast::Intrinsic::kModf}, |
| 2896 | IntrinsicData{"normalize", ast::Intrinsic::kNormalize}, |
| 2897 | IntrinsicData{"outerProduct", ast::Intrinsic::kOuterProduct}, |
| 2898 | IntrinsicData{"pow", ast::Intrinsic::kPow}, |
| 2899 | IntrinsicData{"reflect", ast::Intrinsic::kReflect}, |
| 2900 | IntrinsicData{"reverseBits", ast::Intrinsic::kReverseBits}, |
| 2901 | IntrinsicData{"round", ast::Intrinsic::kRound}, |
| 2902 | IntrinsicData{"select", ast::Intrinsic::kSelect}, |
| 2903 | IntrinsicData{"sign", ast::Intrinsic::kSign}, |
| 2904 | IntrinsicData{"sin", ast::Intrinsic::kSin}, |
| 2905 | IntrinsicData{"sinh", ast::Intrinsic::kSinh}, |
| 2906 | IntrinsicData{"smoothStep", ast::Intrinsic::kSmoothStep}, |
| 2907 | IntrinsicData{"sqrt", ast::Intrinsic::kSqrt}, |
| 2908 | IntrinsicData{"step", ast::Intrinsic::kStep}, |
| 2909 | IntrinsicData{"tan", ast::Intrinsic::kTan}, |
| 2910 | IntrinsicData{"tanh", ast::Intrinsic::kTanh}, |
| 2911 | IntrinsicData{"textureLoad", ast::Intrinsic::kTextureLoad}, |
| 2912 | IntrinsicData{"textureSample", ast::Intrinsic::kTextureSample}, |
| 2913 | IntrinsicData{"textureSampleBias", ast::Intrinsic::kTextureSampleBias}, |
| 2914 | IntrinsicData{"textureSampleCompare", |
| 2915 | ast::Intrinsic::kTextureSampleCompare}, |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 2916 | IntrinsicData{"textureSampleGrad", ast::Intrinsic::kTextureSampleGrad}, |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 2917 | IntrinsicData{"textureSampleLevel", |
| 2918 | ast::Intrinsic::kTextureSampleLevel}, |
| 2919 | IntrinsicData{"trunc", ast::Intrinsic::kTrunc})); |
| 2920 | |
| 2921 | TEST_F(TypeDeterminerTest, IntrinsicNotSetIfNotMatched) { |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 2922 | ast::IdentifierExpression ident(mod->RegisterSymbol("not_intrinsic"), |
| 2923 | "not_intrinsic"); |
dan sinclair | ff267ca | 2020-10-14 18:26:31 +0000 | [diff] [blame] | 2924 | EXPECT_FALSE(td()->SetIntrinsicIfNeeded(&ident)); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 2925 | EXPECT_EQ(ident.intrinsic(), ast::Intrinsic::kNone); |
| 2926 | EXPECT_FALSE(ident.IsIntrinsic()); |
| 2927 | } |
| 2928 | |
| 2929 | using ImportData_SingleParamTest = TypeDeterminerTestWithParam<IntrinsicData>; |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 2930 | TEST_P(ImportData_SingleParamTest, Scalar) { |
dan sinclair | ca1723e | 2020-04-20 15:47:55 +0000 | [diff] [blame] | 2931 | auto param = GetParam(); |
| 2932 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2933 | ast::type::F32 f32; |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 2934 | |
| 2935 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 2936 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 2937 | create<ast::FloatLiteral>(&f32, 1.f))); |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 2938 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 2939 | auto* ident = create<ast::IdentifierExpression>( |
| 2940 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 2941 | |
| 2942 | ast::CallExpression call(ident, params); |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 2943 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 2944 | EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 2945 | ASSERT_NE(ident->result_type(), nullptr); |
| 2946 | EXPECT_TRUE(ident->result_type()->is_float_scalar()); |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 2947 | } |
| 2948 | |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 2949 | TEST_P(ImportData_SingleParamTest, Vector) { |
dan sinclair | ca1723e | 2020-04-20 15:47:55 +0000 | [diff] [blame] | 2950 | auto param = GetParam(); |
| 2951 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2952 | ast::type::F32 f32; |
| 2953 | ast::type::Vector vec(&f32, 3); |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 2954 | |
| 2955 | ast::ExpressionList vals; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 2956 | vals.push_back(create<ast::ScalarConstructorExpression>( |
| 2957 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 2958 | vals.push_back(create<ast::ScalarConstructorExpression>( |
| 2959 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 2960 | vals.push_back(create<ast::ScalarConstructorExpression>( |
| 2961 | create<ast::FloatLiteral>(&f32, 3.0f))); |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 2962 | |
| 2963 | ast::ExpressionList params; |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 2964 | params.push_back(create<ast::TypeConstructorExpression>(&vec, vals)); |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 2965 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 2966 | auto* ident = create<ast::IdentifierExpression>( |
| 2967 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 2968 | |
| 2969 | ast::CallExpression call(ident, params); |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 2970 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 2971 | EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 2972 | ASSERT_NE(ident->result_type(), nullptr); |
| 2973 | EXPECT_TRUE(ident->result_type()->is_float_vector()); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2974 | EXPECT_EQ(ident->result_type()->As<ast::type::Vector>()->size(), 3u); |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 2975 | } |
| 2976 | |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 2977 | TEST_P(ImportData_SingleParamTest, Error_Integer) { |
dan sinclair | ca1723e | 2020-04-20 15:47:55 +0000 | [diff] [blame] | 2978 | auto param = GetParam(); |
| 2979 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2980 | ast::type::I32 i32; |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 2981 | |
| 2982 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 2983 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 2984 | create<ast::SintLiteral>(&i32, 1))); |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 2985 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 2986 | auto* ident = create<ast::IdentifierExpression>( |
| 2987 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 2988 | ast::CallExpression call(ident, params); |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 2989 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 2990 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
dan sinclair | 5335204 | 2020-06-08 18:49:31 +0000 | [diff] [blame] | 2991 | EXPECT_EQ(td()->error(), |
| 2992 | std::string("incorrect type for ") + param.name + |
| 2993 | ". Requires float scalar or float vector values"); |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 2994 | } |
| 2995 | |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 2996 | TEST_P(ImportData_SingleParamTest, Error_NoParams) { |
dan sinclair | ca1723e | 2020-04-20 15:47:55 +0000 | [diff] [blame] | 2997 | auto param = GetParam(); |
| 2998 | |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 2999 | ast::ExpressionList params; |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3000 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 3001 | auto* ident = create<ast::IdentifierExpression>( |
| 3002 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3003 | ast::CallExpression call(ident, params); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3004 | |
| 3005 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
dan sinclair | ca1723e | 2020-04-20 15:47:55 +0000 | [diff] [blame] | 3006 | EXPECT_EQ(td()->error(), std::string("incorrect number of parameters for ") + |
| 3007 | param.name + ". Expected 1 got 0"); |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 3008 | } |
| 3009 | |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 3010 | TEST_P(ImportData_SingleParamTest, Error_MultipleParams) { |
dan sinclair | ca1723e | 2020-04-20 15:47:55 +0000 | [diff] [blame] | 3011 | auto param = GetParam(); |
| 3012 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3013 | ast::type::F32 f32; |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 3014 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3015 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 3016 | create<ast::FloatLiteral>(&f32, 1.f))); |
| 3017 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 3018 | create<ast::FloatLiteral>(&f32, 1.f))); |
| 3019 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 3020 | create<ast::FloatLiteral>(&f32, 1.f))); |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 3021 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 3022 | auto* ident = create<ast::IdentifierExpression>( |
| 3023 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3024 | ast::CallExpression call(ident, params); |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 3025 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3026 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
dan sinclair | ca1723e | 2020-04-20 15:47:55 +0000 | [diff] [blame] | 3027 | EXPECT_EQ(td()->error(), std::string("incorrect number of parameters for ") + |
| 3028 | param.name + ". Expected 1 got 3"); |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 3029 | } |
| 3030 | |
dan sinclair | a49328f | 2020-04-20 15:49:50 +0000 | [diff] [blame] | 3031 | INSTANTIATE_TEST_SUITE_P( |
| 3032 | TypeDeterminerTest, |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 3033 | ImportData_SingleParamTest, |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3034 | testing::Values(IntrinsicData{"acos", ast::Intrinsic::kAcos}, |
| 3035 | IntrinsicData{"asin", ast::Intrinsic::kAsin}, |
| 3036 | IntrinsicData{"atan", ast::Intrinsic::kAtan}, |
| 3037 | IntrinsicData{"ceil", ast::Intrinsic::kCeil}, |
| 3038 | IntrinsicData{"cos", ast::Intrinsic::kCos}, |
| 3039 | IntrinsicData{"cosh", ast::Intrinsic::kCosh}, |
| 3040 | IntrinsicData{"exp", ast::Intrinsic::kExp}, |
| 3041 | IntrinsicData{"exp2", ast::Intrinsic::kExp2}, |
| 3042 | IntrinsicData{"floor", ast::Intrinsic::kFloor}, |
| 3043 | IntrinsicData{"fract", ast::Intrinsic::kFract}, |
| 3044 | IntrinsicData{"inverseSqrt", ast::Intrinsic::kInverseSqrt}, |
| 3045 | IntrinsicData{"log", ast::Intrinsic::kLog}, |
| 3046 | IntrinsicData{"log2", ast::Intrinsic::kLog2}, |
| 3047 | IntrinsicData{"normalize", ast::Intrinsic::kNormalize}, |
| 3048 | IntrinsicData{"round", ast::Intrinsic::kRound}, |
| 3049 | IntrinsicData{"sign", ast::Intrinsic::kSign}, |
| 3050 | IntrinsicData{"sin", ast::Intrinsic::kSin}, |
| 3051 | IntrinsicData{"sinh", ast::Intrinsic::kSinh}, |
| 3052 | IntrinsicData{"sqrt", ast::Intrinsic::kSqrt}, |
| 3053 | IntrinsicData{"tan", ast::Intrinsic::kTan}, |
| 3054 | IntrinsicData{"tanh", ast::Intrinsic::kTanh}, |
| 3055 | IntrinsicData{"trunc", ast::Intrinsic::kTrunc})); |
| 3056 | |
| 3057 | using ImportData_SingleParam_FloatOrInt_Test = |
| 3058 | TypeDeterminerTestWithParam<IntrinsicData>; |
| 3059 | TEST_P(ImportData_SingleParam_FloatOrInt_Test, Float_Scalar) { |
| 3060 | auto param = GetParam(); |
| 3061 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3062 | ast::type::F32 f32; |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3063 | |
| 3064 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3065 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 3066 | create<ast::FloatLiteral>(&f32, 1.f))); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3067 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 3068 | auto* ident = create<ast::IdentifierExpression>( |
| 3069 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3070 | |
| 3071 | ast::CallExpression call(ident, params); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3072 | |
| 3073 | EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3074 | ASSERT_NE(ident->result_type(), nullptr); |
| 3075 | EXPECT_TRUE(ident->result_type()->is_float_scalar()); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3076 | } |
| 3077 | |
| 3078 | TEST_P(ImportData_SingleParam_FloatOrInt_Test, Float_Vector) { |
| 3079 | auto param = GetParam(); |
| 3080 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3081 | ast::type::F32 f32; |
| 3082 | ast::type::Vector vec(&f32, 3); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3083 | |
| 3084 | ast::ExpressionList vals; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3085 | vals.push_back(create<ast::ScalarConstructorExpression>( |
| 3086 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 3087 | vals.push_back(create<ast::ScalarConstructorExpression>( |
| 3088 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 3089 | vals.push_back(create<ast::ScalarConstructorExpression>( |
| 3090 | create<ast::FloatLiteral>(&f32, 3.0f))); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3091 | |
| 3092 | ast::ExpressionList params; |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3093 | params.push_back(create<ast::TypeConstructorExpression>(&vec, vals)); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3094 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 3095 | auto* ident = create<ast::IdentifierExpression>( |
| 3096 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3097 | |
| 3098 | ast::CallExpression call(ident, params); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3099 | |
| 3100 | EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3101 | ASSERT_NE(ident->result_type(), nullptr); |
| 3102 | EXPECT_TRUE(ident->result_type()->is_float_vector()); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3103 | EXPECT_EQ(ident->result_type()->As<ast::type::Vector>()->size(), 3u); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3104 | } |
| 3105 | |
| 3106 | TEST_P(ImportData_SingleParam_FloatOrInt_Test, Sint_Scalar) { |
| 3107 | auto param = GetParam(); |
| 3108 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3109 | ast::type::I32 i32; |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3110 | |
| 3111 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3112 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 3113 | create<ast::SintLiteral>(&i32, -11))); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3114 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 3115 | auto* ident = create<ast::IdentifierExpression>( |
| 3116 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3117 | |
| 3118 | ast::CallExpression call(ident, params); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3119 | |
| 3120 | EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3121 | ASSERT_NE(ident->result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3122 | EXPECT_TRUE(ident->result_type()->Is<ast::type::I32>()); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3123 | } |
| 3124 | |
| 3125 | TEST_P(ImportData_SingleParam_FloatOrInt_Test, Sint_Vector) { |
| 3126 | auto param = GetParam(); |
| 3127 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3128 | ast::type::I32 i32; |
| 3129 | ast::type::Vector vec(&i32, 3); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3130 | |
| 3131 | ast::ExpressionList vals; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3132 | vals.push_back(create<ast::ScalarConstructorExpression>( |
| 3133 | create<ast::SintLiteral>(&i32, 1))); |
| 3134 | vals.push_back(create<ast::ScalarConstructorExpression>( |
| 3135 | create<ast::SintLiteral>(&i32, 1))); |
| 3136 | vals.push_back(create<ast::ScalarConstructorExpression>( |
| 3137 | create<ast::SintLiteral>(&i32, 3))); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3138 | |
| 3139 | ast::ExpressionList params; |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3140 | params.push_back(create<ast::TypeConstructorExpression>(&vec, vals)); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3141 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 3142 | auto* ident = create<ast::IdentifierExpression>( |
| 3143 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3144 | |
| 3145 | ast::CallExpression call(ident, params); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3146 | |
| 3147 | EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3148 | ASSERT_NE(ident->result_type(), nullptr); |
| 3149 | EXPECT_TRUE(ident->result_type()->is_signed_integer_vector()); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3150 | EXPECT_EQ(ident->result_type()->As<ast::type::Vector>()->size(), 3u); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3151 | } |
| 3152 | |
| 3153 | TEST_P(ImportData_SingleParam_FloatOrInt_Test, Uint_Scalar) { |
| 3154 | auto param = GetParam(); |
| 3155 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3156 | ast::type::U32 u32; |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3157 | |
| 3158 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3159 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 3160 | create<ast::UintLiteral>(&u32, 1))); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3161 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 3162 | auto* ident = create<ast::IdentifierExpression>( |
| 3163 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3164 | |
| 3165 | ast::CallExpression call(ident, params); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3166 | |
| 3167 | EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3168 | ASSERT_NE(ident->result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3169 | EXPECT_TRUE(ident->result_type()->Is<ast::type::U32>()); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3170 | } |
| 3171 | |
| 3172 | TEST_P(ImportData_SingleParam_FloatOrInt_Test, Uint_Vector) { |
| 3173 | auto param = GetParam(); |
| 3174 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3175 | ast::type::U32 u32; |
| 3176 | ast::type::Vector vec(&u32, 3); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3177 | |
| 3178 | ast::ExpressionList vals; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3179 | vals.push_back(create<ast::ScalarConstructorExpression>( |
| 3180 | create<ast::UintLiteral>(&u32, 1.0f))); |
| 3181 | vals.push_back(create<ast::ScalarConstructorExpression>( |
| 3182 | create<ast::UintLiteral>(&u32, 1.0f))); |
| 3183 | vals.push_back(create<ast::ScalarConstructorExpression>( |
| 3184 | create<ast::UintLiteral>(&u32, 3.0f))); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3185 | |
| 3186 | ast::ExpressionList params; |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3187 | params.push_back(create<ast::TypeConstructorExpression>(&vec, vals)); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3188 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 3189 | auto* ident = create<ast::IdentifierExpression>( |
| 3190 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3191 | |
| 3192 | ast::CallExpression call(ident, params); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3193 | |
| 3194 | EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3195 | ASSERT_NE(ident->result_type(), nullptr); |
| 3196 | EXPECT_TRUE(ident->result_type()->is_unsigned_integer_vector()); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3197 | EXPECT_EQ(ident->result_type()->As<ast::type::Vector>()->size(), 3u); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3198 | } |
| 3199 | |
| 3200 | TEST_P(ImportData_SingleParam_FloatOrInt_Test, Error_Bool) { |
| 3201 | auto param = GetParam(); |
| 3202 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3203 | ast::type::Bool bool_type; |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3204 | |
| 3205 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3206 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 3207 | create<ast::BoolLiteral>(&bool_type, false))); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3208 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 3209 | auto* ident = create<ast::IdentifierExpression>( |
| 3210 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3211 | ast::CallExpression call(ident, params); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3212 | |
| 3213 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
| 3214 | EXPECT_EQ(td()->error(), |
| 3215 | std::string("incorrect type for ") + param.name + |
| 3216 | ". Requires float or int, scalar or vector values"); |
| 3217 | } |
| 3218 | |
| 3219 | TEST_P(ImportData_SingleParam_FloatOrInt_Test, Error_NoParams) { |
| 3220 | auto param = GetParam(); |
| 3221 | |
| 3222 | ast::ExpressionList params; |
| 3223 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 3224 | auto* ident = create<ast::IdentifierExpression>( |
| 3225 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3226 | ast::CallExpression call(ident, params); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3227 | |
| 3228 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
| 3229 | EXPECT_EQ(td()->error(), std::string("incorrect number of parameters for ") + |
| 3230 | param.name + ". Expected 1 got 0"); |
| 3231 | } |
| 3232 | |
| 3233 | TEST_P(ImportData_SingleParam_FloatOrInt_Test, Error_MultipleParams) { |
| 3234 | auto param = GetParam(); |
| 3235 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3236 | ast::type::F32 f32; |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3237 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3238 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 3239 | create<ast::FloatLiteral>(&f32, 1.f))); |
| 3240 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 3241 | create<ast::FloatLiteral>(&f32, 1.f))); |
| 3242 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 3243 | create<ast::FloatLiteral>(&f32, 1.f))); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3244 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 3245 | auto* ident = create<ast::IdentifierExpression>( |
| 3246 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3247 | ast::CallExpression call(ident, params); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3248 | |
| 3249 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
| 3250 | EXPECT_EQ(td()->error(), std::string("incorrect number of parameters for ") + |
| 3251 | param.name + ". Expected 1 got 3"); |
| 3252 | } |
| 3253 | |
| 3254 | INSTANTIATE_TEST_SUITE_P(TypeDeterminerTest, |
| 3255 | ImportData_SingleParam_FloatOrInt_Test, |
| 3256 | testing::Values(IntrinsicData{"abs", |
| 3257 | ast::Intrinsic::kAbs})); |
dan sinclair | ca1723e | 2020-04-20 15:47:55 +0000 | [diff] [blame] | 3258 | |
dan sinclair | 652a4b9 | 2020-04-20 21:09:14 +0000 | [diff] [blame] | 3259 | TEST_F(TypeDeterminerTest, ImportData_Length_Scalar) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3260 | ast::type::F32 f32; |
dan sinclair | 652a4b9 | 2020-04-20 21:09:14 +0000 | [diff] [blame] | 3261 | |
| 3262 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3263 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 3264 | create<ast::FloatLiteral>(&f32, 1.f))); |
dan sinclair | 652a4b9 | 2020-04-20 21:09:14 +0000 | [diff] [blame] | 3265 | |
| 3266 | ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error(); |
| 3267 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 3268 | auto* ident = create<ast::IdentifierExpression>(mod->RegisterSymbol("length"), |
| 3269 | "length"); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3270 | |
| 3271 | ast::CallExpression call(ident, params); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3272 | |
| 3273 | EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3274 | ASSERT_NE(ident->result_type(), nullptr); |
| 3275 | EXPECT_TRUE(ident->result_type()->is_float_scalar()); |
dan sinclair | 652a4b9 | 2020-04-20 21:09:14 +0000 | [diff] [blame] | 3276 | } |
| 3277 | |
| 3278 | TEST_F(TypeDeterminerTest, ImportData_Length_FloatVector) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3279 | ast::type::F32 f32; |
| 3280 | ast::type::Vector vec(&f32, 3); |
dan sinclair | 652a4b9 | 2020-04-20 21:09:14 +0000 | [diff] [blame] | 3281 | |
| 3282 | ast::ExpressionList vals; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3283 | vals.push_back(create<ast::ScalarConstructorExpression>( |
| 3284 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 3285 | vals.push_back(create<ast::ScalarConstructorExpression>( |
| 3286 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 3287 | vals.push_back(create<ast::ScalarConstructorExpression>( |
| 3288 | create<ast::FloatLiteral>(&f32, 3.0f))); |
dan sinclair | 652a4b9 | 2020-04-20 21:09:14 +0000 | [diff] [blame] | 3289 | |
| 3290 | ast::ExpressionList params; |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3291 | params.push_back(create<ast::TypeConstructorExpression>(&vec, vals)); |
dan sinclair | 652a4b9 | 2020-04-20 21:09:14 +0000 | [diff] [blame] | 3292 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 3293 | auto* ident = create<ast::IdentifierExpression>(mod->RegisterSymbol("length"), |
| 3294 | "length"); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3295 | |
| 3296 | ast::CallExpression call(ident, params); |
dan sinclair | 652a4b9 | 2020-04-20 21:09:14 +0000 | [diff] [blame] | 3297 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3298 | EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3299 | ASSERT_NE(ident->result_type(), nullptr); |
| 3300 | EXPECT_TRUE(ident->result_type()->is_float_scalar()); |
dan sinclair | 652a4b9 | 2020-04-20 21:09:14 +0000 | [diff] [blame] | 3301 | } |
| 3302 | |
| 3303 | TEST_F(TypeDeterminerTest, ImportData_Length_Error_Integer) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3304 | ast::type::I32 i32; |
dan sinclair | 652a4b9 | 2020-04-20 21:09:14 +0000 | [diff] [blame] | 3305 | |
| 3306 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3307 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 3308 | create<ast::SintLiteral>(&i32, 1))); |
dan sinclair | 652a4b9 | 2020-04-20 21:09:14 +0000 | [diff] [blame] | 3309 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 3310 | auto* ident = create<ast::IdentifierExpression>(mod->RegisterSymbol("length"), |
| 3311 | "length"); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3312 | ast::CallExpression call(ident, params); |
dan sinclair | 652a4b9 | 2020-04-20 21:09:14 +0000 | [diff] [blame] | 3313 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3314 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
dan sinclair | 5335204 | 2020-06-08 18:49:31 +0000 | [diff] [blame] | 3315 | EXPECT_EQ(td()->error(), |
| 3316 | "incorrect type for length. Requires float scalar or float vector " |
| 3317 | "values"); |
dan sinclair | 652a4b9 | 2020-04-20 21:09:14 +0000 | [diff] [blame] | 3318 | } |
| 3319 | |
| 3320 | TEST_F(TypeDeterminerTest, ImportData_Length_Error_NoParams) { |
| 3321 | ast::ExpressionList params; |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3322 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 3323 | auto* ident = create<ast::IdentifierExpression>(mod->RegisterSymbol("length"), |
| 3324 | "length"); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3325 | ast::CallExpression call(ident, params); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3326 | |
| 3327 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
dan sinclair | 652a4b9 | 2020-04-20 21:09:14 +0000 | [diff] [blame] | 3328 | EXPECT_EQ(td()->error(), |
| 3329 | "incorrect number of parameters for length. Expected 1 got 0"); |
| 3330 | } |
| 3331 | |
| 3332 | TEST_F(TypeDeterminerTest, ImportData_Length_Error_MultipleParams) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3333 | ast::type::F32 f32; |
dan sinclair | 652a4b9 | 2020-04-20 21:09:14 +0000 | [diff] [blame] | 3334 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3335 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 3336 | create<ast::FloatLiteral>(&f32, 1.f))); |
| 3337 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 3338 | create<ast::FloatLiteral>(&f32, 1.f))); |
| 3339 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 3340 | create<ast::FloatLiteral>(&f32, 1.f))); |
dan sinclair | 652a4b9 | 2020-04-20 21:09:14 +0000 | [diff] [blame] | 3341 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 3342 | auto* ident = create<ast::IdentifierExpression>(mod->RegisterSymbol("length"), |
| 3343 | "length"); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3344 | ast::CallExpression call(ident, params); |
dan sinclair | 652a4b9 | 2020-04-20 21:09:14 +0000 | [diff] [blame] | 3345 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3346 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
dan sinclair | 652a4b9 | 2020-04-20 21:09:14 +0000 | [diff] [blame] | 3347 | EXPECT_EQ(td()->error(), |
| 3348 | "incorrect number of parameters for length. Expected 1 got 3"); |
| 3349 | } |
| 3350 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3351 | using ImportData_TwoParamTest = TypeDeterminerTestWithParam<IntrinsicData>; |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 3352 | TEST_P(ImportData_TwoParamTest, Scalar) { |
| 3353 | auto param = GetParam(); |
| 3354 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3355 | ast::type::F32 f32; |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 3356 | |
| 3357 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3358 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 3359 | create<ast::FloatLiteral>(&f32, 1.f))); |
| 3360 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 3361 | create<ast::FloatLiteral>(&f32, 1.f))); |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 3362 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 3363 | auto* ident = create<ast::IdentifierExpression>( |
| 3364 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3365 | |
| 3366 | ast::CallExpression call(ident, params); |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 3367 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3368 | EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3369 | ASSERT_NE(ident->result_type(), nullptr); |
| 3370 | EXPECT_TRUE(ident->result_type()->is_float_scalar()); |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 3371 | } |
| 3372 | |
| 3373 | TEST_P(ImportData_TwoParamTest, Vector) { |
| 3374 | auto param = GetParam(); |
| 3375 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3376 | ast::type::F32 f32; |
| 3377 | ast::type::Vector vec(&f32, 3); |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 3378 | |
| 3379 | ast::ExpressionList vals_1; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3380 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 3381 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 3382 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 3383 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 3384 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 3385 | create<ast::FloatLiteral>(&f32, 3.0f))); |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 3386 | |
| 3387 | ast::ExpressionList vals_2; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3388 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 3389 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 3390 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 3391 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 3392 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 3393 | create<ast::FloatLiteral>(&f32, 3.0f))); |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 3394 | |
| 3395 | ast::ExpressionList params; |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3396 | params.push_back(create<ast::TypeConstructorExpression>(&vec, vals_1)); |
| 3397 | params.push_back(create<ast::TypeConstructorExpression>(&vec, vals_2)); |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 3398 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 3399 | auto* ident = create<ast::IdentifierExpression>( |
| 3400 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3401 | |
| 3402 | ast::CallExpression call(ident, params); |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 3403 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3404 | EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3405 | ASSERT_NE(ident->result_type(), nullptr); |
| 3406 | EXPECT_TRUE(ident->result_type()->is_float_vector()); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3407 | EXPECT_EQ(ident->result_type()->As<ast::type::Vector>()->size(), 3u); |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 3408 | } |
| 3409 | |
| 3410 | TEST_P(ImportData_TwoParamTest, Error_Integer) { |
| 3411 | auto param = GetParam(); |
| 3412 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3413 | ast::type::I32 i32; |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 3414 | |
| 3415 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3416 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 3417 | create<ast::SintLiteral>(&i32, 1))); |
| 3418 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 3419 | create<ast::SintLiteral>(&i32, 2))); |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 3420 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 3421 | auto* ident = create<ast::IdentifierExpression>( |
| 3422 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3423 | ast::CallExpression call(ident, params); |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 3424 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3425 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 3426 | EXPECT_EQ(td()->error(), |
| 3427 | std::string("incorrect type for ") + param.name + |
dan sinclair | 5335204 | 2020-06-08 18:49:31 +0000 | [diff] [blame] | 3428 | ". Requires float scalar or float vector values"); |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 3429 | } |
| 3430 | |
| 3431 | TEST_P(ImportData_TwoParamTest, Error_NoParams) { |
| 3432 | auto param = GetParam(); |
| 3433 | |
| 3434 | ast::ExpressionList params; |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3435 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 3436 | auto* ident = create<ast::IdentifierExpression>( |
| 3437 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3438 | ast::CallExpression call(ident, params); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3439 | |
| 3440 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 3441 | EXPECT_EQ(td()->error(), std::string("incorrect number of parameters for ") + |
| 3442 | param.name + ". Expected 2 got 0"); |
| 3443 | } |
| 3444 | |
| 3445 | TEST_P(ImportData_TwoParamTest, Error_OneParam) { |
| 3446 | auto param = GetParam(); |
| 3447 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3448 | ast::type::F32 f32; |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 3449 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3450 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 3451 | create<ast::FloatLiteral>(&f32, 1.f))); |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 3452 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 3453 | auto* ident = create<ast::IdentifierExpression>( |
| 3454 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3455 | ast::CallExpression call(ident, params); |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 3456 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3457 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 3458 | EXPECT_EQ(td()->error(), std::string("incorrect number of parameters for ") + |
| 3459 | param.name + ". Expected 2 got 1"); |
| 3460 | } |
| 3461 | |
| 3462 | TEST_P(ImportData_TwoParamTest, Error_MismatchedParamCount) { |
| 3463 | auto param = GetParam(); |
| 3464 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3465 | ast::type::F32 f32; |
| 3466 | ast::type::Vector vec2(&f32, 2); |
| 3467 | ast::type::Vector vec3(&f32, 3); |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 3468 | |
| 3469 | ast::ExpressionList vals_1; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3470 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 3471 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 3472 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 3473 | create<ast::FloatLiteral>(&f32, 1.0f))); |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 3474 | |
| 3475 | ast::ExpressionList vals_2; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3476 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 3477 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 3478 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 3479 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 3480 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 3481 | create<ast::FloatLiteral>(&f32, 3.0f))); |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 3482 | |
| 3483 | ast::ExpressionList params; |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3484 | params.push_back(create<ast::TypeConstructorExpression>(&vec2, vals_1)); |
| 3485 | params.push_back(create<ast::TypeConstructorExpression>(&vec3, vals_2)); |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 3486 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 3487 | auto* ident = create<ast::IdentifierExpression>( |
| 3488 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3489 | ast::CallExpression call(ident, params); |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 3490 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3491 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 3492 | EXPECT_EQ(td()->error(), |
| 3493 | std::string("mismatched parameter types for ") + param.name); |
| 3494 | } |
| 3495 | |
| 3496 | TEST_P(ImportData_TwoParamTest, Error_MismatchedParamType) { |
| 3497 | auto param = GetParam(); |
| 3498 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3499 | ast::type::F32 f32; |
| 3500 | ast::type::Vector vec(&f32, 3); |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 3501 | |
| 3502 | ast::ExpressionList vals; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3503 | vals.push_back(create<ast::ScalarConstructorExpression>( |
| 3504 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 3505 | vals.push_back(create<ast::ScalarConstructorExpression>( |
| 3506 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 3507 | vals.push_back(create<ast::ScalarConstructorExpression>( |
| 3508 | create<ast::FloatLiteral>(&f32, 3.0f))); |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 3509 | |
| 3510 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3511 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 3512 | create<ast::FloatLiteral>(&f32, 1.0f))); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3513 | params.push_back(create<ast::TypeConstructorExpression>(&vec, vals)); |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 3514 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 3515 | auto* ident = create<ast::IdentifierExpression>( |
| 3516 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3517 | ast::CallExpression call(ident, params); |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 3518 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3519 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 3520 | EXPECT_EQ(td()->error(), |
| 3521 | std::string("mismatched parameter types for ") + param.name); |
| 3522 | } |
| 3523 | |
| 3524 | TEST_P(ImportData_TwoParamTest, Error_TooManyParams) { |
| 3525 | auto param = GetParam(); |
| 3526 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3527 | ast::type::F32 f32; |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 3528 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3529 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 3530 | create<ast::FloatLiteral>(&f32, 1.f))); |
| 3531 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 3532 | create<ast::FloatLiteral>(&f32, 1.f))); |
| 3533 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 3534 | create<ast::FloatLiteral>(&f32, 1.f))); |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 3535 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 3536 | auto* ident = create<ast::IdentifierExpression>( |
| 3537 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3538 | ast::CallExpression call(ident, params); |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 3539 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3540 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 3541 | EXPECT_EQ(td()->error(), std::string("incorrect number of parameters for ") + |
| 3542 | param.name + ". Expected 2 got 3"); |
| 3543 | } |
| 3544 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3545 | INSTANTIATE_TEST_SUITE_P( |
| 3546 | TypeDeterminerTest, |
| 3547 | ImportData_TwoParamTest, |
| 3548 | testing::Values(IntrinsicData{"atan2", ast::Intrinsic::kAtan2}, |
| 3549 | IntrinsicData{"pow", ast::Intrinsic::kPow}, |
| 3550 | IntrinsicData{"step", ast::Intrinsic::kStep}, |
| 3551 | IntrinsicData{"reflect", ast::Intrinsic::kReflect})); |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 3552 | |
dan sinclair | 5444438 | 2020-04-21 13:04:15 +0000 | [diff] [blame] | 3553 | TEST_F(TypeDeterminerTest, ImportData_Distance_Scalar) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3554 | ast::type::F32 f32; |
dan sinclair | 5444438 | 2020-04-21 13:04:15 +0000 | [diff] [blame] | 3555 | |
| 3556 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3557 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 3558 | create<ast::FloatLiteral>(&f32, 1.f))); |
| 3559 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 3560 | create<ast::FloatLiteral>(&f32, 1.f))); |
dan sinclair | 5444438 | 2020-04-21 13:04:15 +0000 | [diff] [blame] | 3561 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 3562 | auto* ident = create<ast::IdentifierExpression>( |
| 3563 | mod->RegisterSymbol("distance"), "distance"); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3564 | |
| 3565 | ast::CallExpression call(ident, params); |
dan sinclair | 5444438 | 2020-04-21 13:04:15 +0000 | [diff] [blame] | 3566 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3567 | EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3568 | ASSERT_NE(ident->result_type(), nullptr); |
| 3569 | EXPECT_TRUE(ident->result_type()->is_float_scalar()); |
dan sinclair | 5444438 | 2020-04-21 13:04:15 +0000 | [diff] [blame] | 3570 | } |
| 3571 | |
| 3572 | TEST_F(TypeDeterminerTest, ImportData_Distance_Vector) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3573 | ast::type::F32 f32; |
| 3574 | ast::type::Vector vec(&f32, 3); |
dan sinclair | 5444438 | 2020-04-21 13:04:15 +0000 | [diff] [blame] | 3575 | |
| 3576 | ast::ExpressionList vals_1; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3577 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 3578 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 3579 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 3580 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 3581 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 3582 | create<ast::FloatLiteral>(&f32, 3.0f))); |
dan sinclair | 5444438 | 2020-04-21 13:04:15 +0000 | [diff] [blame] | 3583 | |
| 3584 | ast::ExpressionList vals_2; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3585 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 3586 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 3587 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 3588 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 3589 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 3590 | create<ast::FloatLiteral>(&f32, 3.0f))); |
dan sinclair | 5444438 | 2020-04-21 13:04:15 +0000 | [diff] [blame] | 3591 | |
| 3592 | ast::ExpressionList params; |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3593 | params.push_back(create<ast::TypeConstructorExpression>(&vec, vals_1)); |
| 3594 | params.push_back(create<ast::TypeConstructorExpression>(&vec, vals_2)); |
dan sinclair | 5444438 | 2020-04-21 13:04:15 +0000 | [diff] [blame] | 3595 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 3596 | auto* ident = create<ast::IdentifierExpression>( |
| 3597 | mod->RegisterSymbol("distance"), "distance"); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3598 | |
| 3599 | ast::CallExpression call(ident, params); |
dan sinclair | 5444438 | 2020-04-21 13:04:15 +0000 | [diff] [blame] | 3600 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3601 | EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3602 | ASSERT_NE(ident->result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3603 | EXPECT_TRUE(ident->result_type()->Is<ast::type::F32>()); |
dan sinclair | 5444438 | 2020-04-21 13:04:15 +0000 | [diff] [blame] | 3604 | } |
| 3605 | |
| 3606 | TEST_F(TypeDeterminerTest, ImportData_Distance_Error_Integer) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3607 | ast::type::I32 i32; |
dan sinclair | 5444438 | 2020-04-21 13:04:15 +0000 | [diff] [blame] | 3608 | |
| 3609 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3610 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 3611 | create<ast::SintLiteral>(&i32, 1))); |
| 3612 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 3613 | create<ast::SintLiteral>(&i32, 2))); |
dan sinclair | 5444438 | 2020-04-21 13:04:15 +0000 | [diff] [blame] | 3614 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 3615 | auto* ident = create<ast::IdentifierExpression>( |
| 3616 | mod->RegisterSymbol("distance"), "distance"); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3617 | ast::CallExpression call(ident, params); |
dan sinclair | 5444438 | 2020-04-21 13:04:15 +0000 | [diff] [blame] | 3618 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3619 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
dan sinclair | 5444438 | 2020-04-21 13:04:15 +0000 | [diff] [blame] | 3620 | EXPECT_EQ(td()->error(), |
dan sinclair | 5335204 | 2020-06-08 18:49:31 +0000 | [diff] [blame] | 3621 | "incorrect type for distance. Requires float scalar or float " |
dan sinclair | 5444438 | 2020-04-21 13:04:15 +0000 | [diff] [blame] | 3622 | "vector values"); |
| 3623 | } |
| 3624 | |
| 3625 | TEST_F(TypeDeterminerTest, ImportData_Distance_Error_NoParams) { |
| 3626 | ast::ExpressionList params; |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3627 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 3628 | auto* ident = create<ast::IdentifierExpression>( |
| 3629 | mod->RegisterSymbol("distance"), "distance"); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3630 | ast::CallExpression call(ident, params); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3631 | |
| 3632 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
dan sinclair | 5444438 | 2020-04-21 13:04:15 +0000 | [diff] [blame] | 3633 | EXPECT_EQ(td()->error(), |
| 3634 | "incorrect number of parameters for distance. Expected 2 got 0"); |
| 3635 | } |
| 3636 | |
| 3637 | TEST_F(TypeDeterminerTest, ImportData_Distance_Error_OneParam) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3638 | ast::type::F32 f32; |
dan sinclair | 5444438 | 2020-04-21 13:04:15 +0000 | [diff] [blame] | 3639 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3640 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 3641 | create<ast::FloatLiteral>(&f32, 1.f))); |
dan sinclair | 5444438 | 2020-04-21 13:04:15 +0000 | [diff] [blame] | 3642 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 3643 | auto* ident = create<ast::IdentifierExpression>( |
| 3644 | mod->RegisterSymbol("distance"), "distance"); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3645 | ast::CallExpression call(ident, params); |
dan sinclair | 5444438 | 2020-04-21 13:04:15 +0000 | [diff] [blame] | 3646 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3647 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
dan sinclair | 5444438 | 2020-04-21 13:04:15 +0000 | [diff] [blame] | 3648 | EXPECT_EQ(td()->error(), |
| 3649 | "incorrect number of parameters for distance. Expected 2 got 1"); |
| 3650 | } |
| 3651 | |
| 3652 | TEST_F(TypeDeterminerTest, ImportData_Distance_Error_MismatchedParamCount) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3653 | ast::type::F32 f32; |
| 3654 | ast::type::Vector vec2(&f32, 2); |
| 3655 | ast::type::Vector vec3(&f32, 3); |
dan sinclair | 5444438 | 2020-04-21 13:04:15 +0000 | [diff] [blame] | 3656 | |
| 3657 | ast::ExpressionList vals_1; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3658 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 3659 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 3660 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 3661 | create<ast::FloatLiteral>(&f32, 1.0f))); |
dan sinclair | 5444438 | 2020-04-21 13:04:15 +0000 | [diff] [blame] | 3662 | |
| 3663 | ast::ExpressionList vals_2; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3664 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 3665 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 3666 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 3667 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 3668 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 3669 | create<ast::FloatLiteral>(&f32, 3.0f))); |
dan sinclair | 5444438 | 2020-04-21 13:04:15 +0000 | [diff] [blame] | 3670 | |
| 3671 | ast::ExpressionList params; |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3672 | params.push_back(create<ast::TypeConstructorExpression>(&vec2, vals_1)); |
| 3673 | params.push_back(create<ast::TypeConstructorExpression>(&vec3, vals_2)); |
dan sinclair | 5444438 | 2020-04-21 13:04:15 +0000 | [diff] [blame] | 3674 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 3675 | auto* ident = create<ast::IdentifierExpression>( |
| 3676 | mod->RegisterSymbol("distance"), "distance"); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3677 | ast::CallExpression call(ident, params); |
dan sinclair | 5444438 | 2020-04-21 13:04:15 +0000 | [diff] [blame] | 3678 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3679 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
dan sinclair | 5444438 | 2020-04-21 13:04:15 +0000 | [diff] [blame] | 3680 | EXPECT_EQ(td()->error(), "mismatched parameter types for distance"); |
| 3681 | } |
| 3682 | |
| 3683 | TEST_F(TypeDeterminerTest, ImportData_Distance_Error_MismatchedParamType) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3684 | ast::type::F32 f32; |
| 3685 | ast::type::Vector vec(&f32, 3); |
dan sinclair | 5444438 | 2020-04-21 13:04:15 +0000 | [diff] [blame] | 3686 | |
| 3687 | ast::ExpressionList vals; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3688 | vals.push_back(create<ast::ScalarConstructorExpression>( |
| 3689 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 3690 | vals.push_back(create<ast::ScalarConstructorExpression>( |
| 3691 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 3692 | vals.push_back(create<ast::ScalarConstructorExpression>( |
| 3693 | create<ast::FloatLiteral>(&f32, 3.0f))); |
dan sinclair | 5444438 | 2020-04-21 13:04:15 +0000 | [diff] [blame] | 3694 | |
| 3695 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3696 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 3697 | create<ast::FloatLiteral>(&f32, 1.0f))); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3698 | params.push_back(create<ast::TypeConstructorExpression>(&vec, vals)); |
dan sinclair | 5444438 | 2020-04-21 13:04:15 +0000 | [diff] [blame] | 3699 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 3700 | auto* ident = create<ast::IdentifierExpression>( |
| 3701 | mod->RegisterSymbol("distance"), "distance"); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3702 | ast::CallExpression call(ident, params); |
dan sinclair | 5444438 | 2020-04-21 13:04:15 +0000 | [diff] [blame] | 3703 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3704 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
dan sinclair | 5444438 | 2020-04-21 13:04:15 +0000 | [diff] [blame] | 3705 | EXPECT_EQ(td()->error(), "mismatched parameter types for distance"); |
| 3706 | } |
| 3707 | |
| 3708 | TEST_F(TypeDeterminerTest, ImportData_Distance_Error_TooManyParams) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3709 | ast::type::F32 f32; |
dan sinclair | 5444438 | 2020-04-21 13:04:15 +0000 | [diff] [blame] | 3710 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3711 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 3712 | create<ast::FloatLiteral>(&f32, 1.f))); |
| 3713 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 3714 | create<ast::FloatLiteral>(&f32, 1.f))); |
| 3715 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 3716 | create<ast::FloatLiteral>(&f32, 1.f))); |
dan sinclair | 5444438 | 2020-04-21 13:04:15 +0000 | [diff] [blame] | 3717 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 3718 | auto* ident = create<ast::IdentifierExpression>( |
| 3719 | mod->RegisterSymbol("distance"), "distance"); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3720 | ast::CallExpression call(ident, params); |
dan sinclair | 5444438 | 2020-04-21 13:04:15 +0000 | [diff] [blame] | 3721 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3722 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
dan sinclair | 5444438 | 2020-04-21 13:04:15 +0000 | [diff] [blame] | 3723 | EXPECT_EQ(td()->error(), |
| 3724 | "incorrect number of parameters for distance. Expected 2 got 3"); |
| 3725 | } |
| 3726 | |
dan sinclair | ee39225 | 2020-06-08 23:48:15 +0000 | [diff] [blame] | 3727 | TEST_F(TypeDeterminerTest, ImportData_Cross) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3728 | ast::type::F32 f32; |
| 3729 | ast::type::Vector vec(&f32, 3); |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 3730 | |
dan sinclair | ee39225 | 2020-06-08 23:48:15 +0000 | [diff] [blame] | 3731 | ast::ExpressionList vals_1; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3732 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 3733 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 3734 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 3735 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 3736 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 3737 | create<ast::FloatLiteral>(&f32, 3.0f))); |
dan sinclair | ee39225 | 2020-06-08 23:48:15 +0000 | [diff] [blame] | 3738 | |
| 3739 | ast::ExpressionList vals_2; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3740 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 3741 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 3742 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 3743 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 3744 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 3745 | create<ast::FloatLiteral>(&f32, 3.0f))); |
dan sinclair | ee39225 | 2020-06-08 23:48:15 +0000 | [diff] [blame] | 3746 | |
| 3747 | ast::ExpressionList params; |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3748 | params.push_back(create<ast::TypeConstructorExpression>(&vec, vals_1)); |
| 3749 | params.push_back(create<ast::TypeConstructorExpression>(&vec, vals_2)); |
dan sinclair | ee39225 | 2020-06-08 23:48:15 +0000 | [diff] [blame] | 3750 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 3751 | auto* ident = |
| 3752 | create<ast::IdentifierExpression>(mod->RegisterSymbol("cross"), "cross"); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3753 | |
| 3754 | ast::CallExpression call(ident, params); |
dan sinclair | ee39225 | 2020-06-08 23:48:15 +0000 | [diff] [blame] | 3755 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3756 | EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3757 | ASSERT_NE(ident->result_type(), nullptr); |
| 3758 | EXPECT_TRUE(ident->result_type()->is_float_vector()); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3759 | EXPECT_EQ(ident->result_type()->As<ast::type::Vector>()->size(), 3u); |
dan sinclair | ee39225 | 2020-06-08 23:48:15 +0000 | [diff] [blame] | 3760 | } |
| 3761 | |
| 3762 | TEST_F(TypeDeterminerTest, ImportData_Cross_Error_Scalar) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3763 | ast::type::F32 f32; |
dan sinclair | ee39225 | 2020-06-08 23:48:15 +0000 | [diff] [blame] | 3764 | |
| 3765 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3766 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 3767 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 3768 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 3769 | create<ast::FloatLiteral>(&f32, 1.0f))); |
dan sinclair | ee39225 | 2020-06-08 23:48:15 +0000 | [diff] [blame] | 3770 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 3771 | auto* ident = |
| 3772 | create<ast::IdentifierExpression>(mod->RegisterSymbol("cross"), "cross"); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3773 | ast::CallExpression call(ident, params); |
dan sinclair | ee39225 | 2020-06-08 23:48:15 +0000 | [diff] [blame] | 3774 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3775 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
dan sinclair | ee39225 | 2020-06-08 23:48:15 +0000 | [diff] [blame] | 3776 | EXPECT_EQ(td()->error(), |
| 3777 | "incorrect type for cross. Requires float vector values"); |
| 3778 | } |
| 3779 | |
| 3780 | TEST_F(TypeDeterminerTest, ImportData_Cross_Error_IntType) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3781 | ast::type::I32 i32; |
| 3782 | ast::type::Vector vec(&i32, 3); |
dan sinclair | ee39225 | 2020-06-08 23:48:15 +0000 | [diff] [blame] | 3783 | |
| 3784 | ast::ExpressionList vals_1; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3785 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 3786 | create<ast::SintLiteral>(&i32, 1))); |
| 3787 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 3788 | create<ast::SintLiteral>(&i32, 1))); |
| 3789 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 3790 | create<ast::SintLiteral>(&i32, 3))); |
dan sinclair | ee39225 | 2020-06-08 23:48:15 +0000 | [diff] [blame] | 3791 | |
| 3792 | ast::ExpressionList vals_2; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3793 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 3794 | create<ast::SintLiteral>(&i32, 1))); |
| 3795 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 3796 | create<ast::SintLiteral>(&i32, 1))); |
| 3797 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 3798 | create<ast::SintLiteral>(&i32, 3))); |
dan sinclair | ee39225 | 2020-06-08 23:48:15 +0000 | [diff] [blame] | 3799 | |
| 3800 | ast::ExpressionList params; |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3801 | params.push_back(create<ast::TypeConstructorExpression>(&vec, vals_1)); |
| 3802 | params.push_back(create<ast::TypeConstructorExpression>(&vec, vals_2)); |
dan sinclair | ee39225 | 2020-06-08 23:48:15 +0000 | [diff] [blame] | 3803 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 3804 | auto* ident = |
| 3805 | create<ast::IdentifierExpression>(mod->RegisterSymbol("cross"), "cross"); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3806 | ast::CallExpression call(ident, params); |
dan sinclair | ee39225 | 2020-06-08 23:48:15 +0000 | [diff] [blame] | 3807 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3808 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
dan sinclair | ee39225 | 2020-06-08 23:48:15 +0000 | [diff] [blame] | 3809 | EXPECT_EQ(td()->error(), |
| 3810 | "incorrect type for cross. Requires float vector values"); |
| 3811 | } |
| 3812 | |
| 3813 | TEST_F(TypeDeterminerTest, ImportData_Cross_Error_MissingParams) { |
dan sinclair | ee39225 | 2020-06-08 23:48:15 +0000 | [diff] [blame] | 3814 | ast::ExpressionList params; |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 3815 | auto* ident = |
| 3816 | create<ast::IdentifierExpression>(mod->RegisterSymbol("cross"), "cross"); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3817 | ast::CallExpression call(ident, params); |
dan sinclair | ee39225 | 2020-06-08 23:48:15 +0000 | [diff] [blame] | 3818 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3819 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
dan sinclair | ee39225 | 2020-06-08 23:48:15 +0000 | [diff] [blame] | 3820 | EXPECT_EQ(td()->error(), |
| 3821 | "incorrect number of parameters for cross. Expected 2 got 0"); |
| 3822 | } |
| 3823 | |
| 3824 | TEST_F(TypeDeterminerTest, ImportData_Cross_Error_TooFewParams) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3825 | ast::type::F32 f32; |
| 3826 | ast::type::Vector vec(&f32, 3); |
dan sinclair | ee39225 | 2020-06-08 23:48:15 +0000 | [diff] [blame] | 3827 | |
| 3828 | ast::ExpressionList vals_1; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3829 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 3830 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 3831 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 3832 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 3833 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 3834 | create<ast::FloatLiteral>(&f32, 3.0f))); |
dan sinclair | ee39225 | 2020-06-08 23:48:15 +0000 | [diff] [blame] | 3835 | |
| 3836 | ast::ExpressionList params; |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3837 | params.push_back(create<ast::TypeConstructorExpression>(&vec, vals_1)); |
dan sinclair | ee39225 | 2020-06-08 23:48:15 +0000 | [diff] [blame] | 3838 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 3839 | auto* ident = |
| 3840 | create<ast::IdentifierExpression>(mod->RegisterSymbol("cross"), "cross"); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3841 | ast::CallExpression call(ident, params); |
dan sinclair | ee39225 | 2020-06-08 23:48:15 +0000 | [diff] [blame] | 3842 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3843 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
dan sinclair | ee39225 | 2020-06-08 23:48:15 +0000 | [diff] [blame] | 3844 | EXPECT_EQ(td()->error(), |
| 3845 | "incorrect number of parameters for cross. Expected 2 got 1"); |
| 3846 | } |
| 3847 | |
| 3848 | TEST_F(TypeDeterminerTest, ImportData_Cross_Error_TooManyParams) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3849 | ast::type::F32 f32; |
| 3850 | ast::type::Vector vec(&f32, 3); |
dan sinclair | ee39225 | 2020-06-08 23:48:15 +0000 | [diff] [blame] | 3851 | |
| 3852 | ast::ExpressionList vals_1; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3853 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 3854 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 3855 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 3856 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 3857 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 3858 | create<ast::FloatLiteral>(&f32, 3.0f))); |
dan sinclair | ee39225 | 2020-06-08 23:48:15 +0000 | [diff] [blame] | 3859 | |
| 3860 | ast::ExpressionList vals_2; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3861 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 3862 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 3863 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 3864 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 3865 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 3866 | create<ast::FloatLiteral>(&f32, 3.0f))); |
dan sinclair | ee39225 | 2020-06-08 23:48:15 +0000 | [diff] [blame] | 3867 | |
| 3868 | ast::ExpressionList vals_3; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3869 | vals_3.push_back(create<ast::ScalarConstructorExpression>( |
| 3870 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 3871 | vals_3.push_back(create<ast::ScalarConstructorExpression>( |
| 3872 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 3873 | vals_3.push_back(create<ast::ScalarConstructorExpression>( |
| 3874 | create<ast::FloatLiteral>(&f32, 3.0f))); |
dan sinclair | ee39225 | 2020-06-08 23:48:15 +0000 | [diff] [blame] | 3875 | |
| 3876 | ast::ExpressionList params; |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3877 | params.push_back(create<ast::TypeConstructorExpression>(&vec, vals_1)); |
| 3878 | params.push_back(create<ast::TypeConstructorExpression>(&vec, vals_2)); |
| 3879 | params.push_back(create<ast::TypeConstructorExpression>(&vec, vals_3)); |
dan sinclair | ee39225 | 2020-06-08 23:48:15 +0000 | [diff] [blame] | 3880 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 3881 | auto* ident = |
| 3882 | create<ast::IdentifierExpression>(mod->RegisterSymbol("cross"), "cross"); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3883 | ast::CallExpression call(ident, params); |
dan sinclair | ee39225 | 2020-06-08 23:48:15 +0000 | [diff] [blame] | 3884 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3885 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
dan sinclair | ee39225 | 2020-06-08 23:48:15 +0000 | [diff] [blame] | 3886 | EXPECT_EQ(td()->error(), |
| 3887 | "incorrect number of parameters for cross. Expected 2 got 3"); |
| 3888 | } |
| 3889 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3890 | using ImportData_ThreeParamTest = TypeDeterminerTestWithParam<IntrinsicData>; |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 3891 | TEST_P(ImportData_ThreeParamTest, Scalar) { |
| 3892 | auto param = GetParam(); |
| 3893 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3894 | ast::type::F32 f32; |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 3895 | |
| 3896 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3897 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 3898 | create<ast::FloatLiteral>(&f32, 1.f))); |
| 3899 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 3900 | create<ast::FloatLiteral>(&f32, 1.f))); |
| 3901 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 3902 | create<ast::FloatLiteral>(&f32, 1.f))); |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 3903 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 3904 | auto* ident = create<ast::IdentifierExpression>( |
| 3905 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3906 | |
| 3907 | ast::CallExpression call(ident, params); |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 3908 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3909 | EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3910 | ASSERT_NE(ident->result_type(), nullptr); |
| 3911 | EXPECT_TRUE(ident->result_type()->is_float_scalar()); |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 3912 | } |
| 3913 | |
| 3914 | TEST_P(ImportData_ThreeParamTest, Vector) { |
| 3915 | auto param = GetParam(); |
| 3916 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3917 | ast::type::F32 f32; |
| 3918 | ast::type::Vector vec(&f32, 3); |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 3919 | |
| 3920 | ast::ExpressionList vals_1; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3921 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 3922 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 3923 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 3924 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 3925 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 3926 | create<ast::FloatLiteral>(&f32, 3.0f))); |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 3927 | |
| 3928 | ast::ExpressionList vals_2; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3929 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 3930 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 3931 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 3932 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 3933 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 3934 | create<ast::FloatLiteral>(&f32, 3.0f))); |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 3935 | |
| 3936 | ast::ExpressionList vals_3; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3937 | vals_3.push_back(create<ast::ScalarConstructorExpression>( |
| 3938 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 3939 | vals_3.push_back(create<ast::ScalarConstructorExpression>( |
| 3940 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 3941 | vals_3.push_back(create<ast::ScalarConstructorExpression>( |
| 3942 | create<ast::FloatLiteral>(&f32, 3.0f))); |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 3943 | |
| 3944 | ast::ExpressionList params; |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3945 | params.push_back(create<ast::TypeConstructorExpression>(&vec, vals_1)); |
| 3946 | params.push_back(create<ast::TypeConstructorExpression>(&vec, vals_2)); |
| 3947 | params.push_back(create<ast::TypeConstructorExpression>(&vec, vals_3)); |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 3948 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 3949 | auto* ident = create<ast::IdentifierExpression>( |
| 3950 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3951 | |
| 3952 | ast::CallExpression call(ident, params); |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 3953 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3954 | EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3955 | ASSERT_NE(ident->result_type(), nullptr); |
| 3956 | EXPECT_TRUE(ident->result_type()->is_float_vector()); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3957 | EXPECT_EQ(ident->result_type()->As<ast::type::Vector>()->size(), 3u); |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 3958 | } |
| 3959 | |
| 3960 | TEST_P(ImportData_ThreeParamTest, Error_Integer) { |
| 3961 | auto param = GetParam(); |
| 3962 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 3963 | ast::type::I32 i32; |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 3964 | |
| 3965 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 3966 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 3967 | create<ast::SintLiteral>(&i32, 1))); |
| 3968 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 3969 | create<ast::SintLiteral>(&i32, 2))); |
| 3970 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 3971 | create<ast::SintLiteral>(&i32, 3))); |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 3972 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 3973 | auto* ident = create<ast::IdentifierExpression>( |
| 3974 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3975 | ast::CallExpression call(ident, params); |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 3976 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3977 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 3978 | EXPECT_EQ(td()->error(), |
| 3979 | std::string("incorrect type for ") + param.name + |
dan sinclair | 5335204 | 2020-06-08 18:49:31 +0000 | [diff] [blame] | 3980 | ". Requires float scalar or float vector values"); |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 3981 | } |
| 3982 | |
| 3983 | TEST_P(ImportData_ThreeParamTest, Error_NoParams) { |
| 3984 | auto param = GetParam(); |
| 3985 | |
| 3986 | ast::ExpressionList params; |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3987 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 3988 | auto* ident = create<ast::IdentifierExpression>( |
| 3989 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 3990 | ast::CallExpression call(ident, params); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 3991 | |
| 3992 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 3993 | EXPECT_EQ(td()->error(), std::string("incorrect number of parameters for ") + |
| 3994 | param.name + ". Expected 3 got 0"); |
| 3995 | } |
| 3996 | |
| 3997 | TEST_P(ImportData_ThreeParamTest, Error_OneParam) { |
| 3998 | auto param = GetParam(); |
| 3999 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4000 | ast::type::F32 f32; |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 4001 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4002 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4003 | create<ast::FloatLiteral>(&f32, 1.f))); |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 4004 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 4005 | auto* ident = create<ast::IdentifierExpression>( |
| 4006 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4007 | ast::CallExpression call(ident, params); |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 4008 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4009 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 4010 | EXPECT_EQ(td()->error(), std::string("incorrect number of parameters for ") + |
| 4011 | param.name + ". Expected 3 got 1"); |
| 4012 | } |
| 4013 | |
| 4014 | TEST_P(ImportData_ThreeParamTest, Error_TwoParams) { |
| 4015 | auto param = GetParam(); |
| 4016 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4017 | ast::type::F32 f32; |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 4018 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4019 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4020 | create<ast::FloatLiteral>(&f32, 1.f))); |
| 4021 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4022 | create<ast::FloatLiteral>(&f32, 1.f))); |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 4023 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 4024 | auto* ident = create<ast::IdentifierExpression>( |
| 4025 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4026 | ast::CallExpression call(ident, params); |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 4027 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4028 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 4029 | EXPECT_EQ(td()->error(), std::string("incorrect number of parameters for ") + |
| 4030 | param.name + ". Expected 3 got 2"); |
| 4031 | } |
| 4032 | |
| 4033 | TEST_P(ImportData_ThreeParamTest, Error_MismatchedParamCount) { |
| 4034 | auto param = GetParam(); |
| 4035 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4036 | ast::type::F32 f32; |
| 4037 | ast::type::Vector vec2(&f32, 2); |
| 4038 | ast::type::Vector vec3(&f32, 3); |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 4039 | |
| 4040 | ast::ExpressionList vals_1; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4041 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 4042 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 4043 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 4044 | create<ast::FloatLiteral>(&f32, 1.0f))); |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 4045 | |
| 4046 | ast::ExpressionList vals_2; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4047 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 4048 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 4049 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 4050 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 4051 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 4052 | create<ast::FloatLiteral>(&f32, 3.0f))); |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 4053 | |
| 4054 | ast::ExpressionList vals_3; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4055 | vals_3.push_back(create<ast::ScalarConstructorExpression>( |
| 4056 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 4057 | vals_3.push_back(create<ast::ScalarConstructorExpression>( |
| 4058 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 4059 | vals_3.push_back(create<ast::ScalarConstructorExpression>( |
| 4060 | create<ast::FloatLiteral>(&f32, 3.0f))); |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 4061 | |
| 4062 | ast::ExpressionList params; |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4063 | params.push_back(create<ast::TypeConstructorExpression>(&vec2, vals_1)); |
| 4064 | params.push_back(create<ast::TypeConstructorExpression>(&vec3, vals_2)); |
| 4065 | params.push_back(create<ast::TypeConstructorExpression>(&vec3, vals_3)); |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 4066 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 4067 | auto* ident = create<ast::IdentifierExpression>( |
| 4068 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4069 | ast::CallExpression call(ident, params); |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 4070 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4071 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 4072 | EXPECT_EQ(td()->error(), |
| 4073 | std::string("mismatched parameter types for ") + param.name); |
| 4074 | } |
| 4075 | |
| 4076 | TEST_P(ImportData_ThreeParamTest, Error_MismatchedParamType) { |
| 4077 | auto param = GetParam(); |
| 4078 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4079 | ast::type::F32 f32; |
| 4080 | ast::type::Vector vec(&f32, 3); |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 4081 | |
| 4082 | ast::ExpressionList vals; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4083 | vals.push_back(create<ast::ScalarConstructorExpression>( |
| 4084 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 4085 | vals.push_back(create<ast::ScalarConstructorExpression>( |
| 4086 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 4087 | vals.push_back(create<ast::ScalarConstructorExpression>( |
| 4088 | create<ast::FloatLiteral>(&f32, 3.0f))); |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 4089 | |
| 4090 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4091 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4092 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 4093 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4094 | create<ast::FloatLiteral>(&f32, 1.0f))); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4095 | params.push_back(create<ast::TypeConstructorExpression>(&vec, vals)); |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 4096 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 4097 | auto* ident = create<ast::IdentifierExpression>( |
| 4098 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4099 | ast::CallExpression call(ident, params); |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 4100 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4101 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 4102 | EXPECT_EQ(td()->error(), |
| 4103 | std::string("mismatched parameter types for ") + param.name); |
| 4104 | } |
| 4105 | |
| 4106 | TEST_P(ImportData_ThreeParamTest, Error_TooManyParams) { |
| 4107 | auto param = GetParam(); |
| 4108 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4109 | ast::type::F32 f32; |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 4110 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4111 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4112 | create<ast::FloatLiteral>(&f32, 1.f))); |
| 4113 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4114 | create<ast::FloatLiteral>(&f32, 1.f))); |
| 4115 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4116 | create<ast::FloatLiteral>(&f32, 1.f))); |
| 4117 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4118 | create<ast::FloatLiteral>(&f32, 1.f))); |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 4119 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 4120 | auto* ident = create<ast::IdentifierExpression>( |
| 4121 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4122 | ast::CallExpression call(ident, params); |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 4123 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4124 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
dan sinclair | 2287d01 | 2020-04-22 00:23:57 +0000 | [diff] [blame] | 4125 | EXPECT_EQ(td()->error(), std::string("incorrect number of parameters for ") + |
| 4126 | param.name + ". Expected 3 got 4"); |
| 4127 | } |
| 4128 | |
| 4129 | INSTANTIATE_TEST_SUITE_P( |
| 4130 | TypeDeterminerTest, |
| 4131 | ImportData_ThreeParamTest, |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4132 | testing::Values(IntrinsicData{"mix", ast::Intrinsic::kMix}, |
| 4133 | IntrinsicData{"smoothStep", ast::Intrinsic::kSmoothStep}, |
| 4134 | IntrinsicData{"fma", ast::Intrinsic::kFma}, |
| 4135 | IntrinsicData{"faceForward", |
| 4136 | ast::Intrinsic::kFaceForward})); |
dan sinclair | 5e5fb9c | 2020-06-08 19:01:07 +0000 | [diff] [blame] | 4137 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4138 | using ImportData_ThreeParam_FloatOrInt_Test = |
| 4139 | TypeDeterminerTestWithParam<IntrinsicData>; |
| 4140 | TEST_P(ImportData_ThreeParam_FloatOrInt_Test, Float_Scalar) { |
| 4141 | auto param = GetParam(); |
| 4142 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4143 | ast::type::F32 f32; |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4144 | |
| 4145 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4146 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4147 | create<ast::FloatLiteral>(&f32, 1.f))); |
| 4148 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4149 | create<ast::FloatLiteral>(&f32, 1.f))); |
| 4150 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4151 | create<ast::FloatLiteral>(&f32, 1.f))); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4152 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 4153 | auto* ident = create<ast::IdentifierExpression>( |
| 4154 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4155 | |
| 4156 | ast::CallExpression call(ident, params); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4157 | |
| 4158 | EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4159 | ASSERT_NE(ident->result_type(), nullptr); |
| 4160 | EXPECT_TRUE(ident->result_type()->is_float_scalar()); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4161 | } |
| 4162 | |
| 4163 | TEST_P(ImportData_ThreeParam_FloatOrInt_Test, Float_Vector) { |
| 4164 | auto param = GetParam(); |
| 4165 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4166 | ast::type::F32 f32; |
| 4167 | ast::type::Vector vec(&f32, 3); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4168 | |
| 4169 | ast::ExpressionList vals_1; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4170 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 4171 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 4172 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 4173 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 4174 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 4175 | create<ast::FloatLiteral>(&f32, 3.0f))); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4176 | |
| 4177 | ast::ExpressionList vals_2; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4178 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 4179 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 4180 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 4181 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 4182 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 4183 | create<ast::FloatLiteral>(&f32, 3.0f))); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4184 | |
| 4185 | ast::ExpressionList vals_3; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4186 | vals_3.push_back(create<ast::ScalarConstructorExpression>( |
| 4187 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 4188 | vals_3.push_back(create<ast::ScalarConstructorExpression>( |
| 4189 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 4190 | vals_3.push_back(create<ast::ScalarConstructorExpression>( |
| 4191 | create<ast::FloatLiteral>(&f32, 3.0f))); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4192 | |
| 4193 | ast::ExpressionList params; |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4194 | params.push_back(create<ast::TypeConstructorExpression>(&vec, vals_1)); |
| 4195 | params.push_back(create<ast::TypeConstructorExpression>(&vec, vals_2)); |
| 4196 | params.push_back(create<ast::TypeConstructorExpression>(&vec, vals_3)); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4197 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 4198 | auto* ident = create<ast::IdentifierExpression>( |
| 4199 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4200 | |
| 4201 | ast::CallExpression call(ident, params); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4202 | |
| 4203 | EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4204 | ASSERT_NE(ident->result_type(), nullptr); |
| 4205 | EXPECT_TRUE(ident->result_type()->is_float_vector()); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4206 | EXPECT_EQ(ident->result_type()->As<ast::type::Vector>()->size(), 3u); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4207 | } |
| 4208 | |
| 4209 | TEST_P(ImportData_ThreeParam_FloatOrInt_Test, Sint_Scalar) { |
| 4210 | auto param = GetParam(); |
| 4211 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4212 | ast::type::I32 i32; |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4213 | |
| 4214 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4215 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4216 | create<ast::SintLiteral>(&i32, 1))); |
| 4217 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4218 | create<ast::SintLiteral>(&i32, 1))); |
| 4219 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4220 | create<ast::SintLiteral>(&i32, 1))); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4221 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 4222 | auto* ident = create<ast::IdentifierExpression>( |
| 4223 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4224 | |
| 4225 | ast::CallExpression call(ident, params); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4226 | |
| 4227 | EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4228 | ASSERT_NE(ident->result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4229 | EXPECT_TRUE(ident->result_type()->Is<ast::type::I32>()); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4230 | } |
| 4231 | |
| 4232 | TEST_P(ImportData_ThreeParam_FloatOrInt_Test, Sint_Vector) { |
| 4233 | auto param = GetParam(); |
| 4234 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4235 | ast::type::I32 i32; |
| 4236 | ast::type::Vector vec(&i32, 3); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4237 | |
| 4238 | ast::ExpressionList vals_1; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4239 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 4240 | create<ast::SintLiteral>(&i32, 1))); |
| 4241 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 4242 | create<ast::SintLiteral>(&i32, 1))); |
| 4243 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 4244 | create<ast::SintLiteral>(&i32, 3))); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4245 | |
| 4246 | ast::ExpressionList vals_2; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4247 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 4248 | create<ast::SintLiteral>(&i32, 1))); |
| 4249 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 4250 | create<ast::SintLiteral>(&i32, 1))); |
| 4251 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 4252 | create<ast::SintLiteral>(&i32, 3))); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4253 | |
| 4254 | ast::ExpressionList vals_3; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4255 | vals_3.push_back(create<ast::ScalarConstructorExpression>( |
| 4256 | create<ast::SintLiteral>(&i32, 1))); |
| 4257 | vals_3.push_back(create<ast::ScalarConstructorExpression>( |
| 4258 | create<ast::SintLiteral>(&i32, 1))); |
| 4259 | vals_3.push_back(create<ast::ScalarConstructorExpression>( |
| 4260 | create<ast::SintLiteral>(&i32, 3))); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4261 | |
| 4262 | ast::ExpressionList params; |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4263 | params.push_back(create<ast::TypeConstructorExpression>(&vec, vals_1)); |
| 4264 | params.push_back(create<ast::TypeConstructorExpression>(&vec, vals_2)); |
| 4265 | params.push_back(create<ast::TypeConstructorExpression>(&vec, vals_3)); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4266 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 4267 | auto* ident = create<ast::IdentifierExpression>( |
| 4268 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4269 | |
| 4270 | ast::CallExpression call(ident, params); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4271 | |
| 4272 | EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4273 | ASSERT_NE(ident->result_type(), nullptr); |
| 4274 | EXPECT_TRUE(ident->result_type()->is_signed_integer_vector()); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4275 | EXPECT_EQ(ident->result_type()->As<ast::type::Vector>()->size(), 3u); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4276 | } |
| 4277 | |
| 4278 | TEST_P(ImportData_ThreeParam_FloatOrInt_Test, Uint_Scalar) { |
| 4279 | auto param = GetParam(); |
| 4280 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4281 | ast::type::U32 u32; |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4282 | |
| 4283 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4284 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4285 | create<ast::UintLiteral>(&u32, 1))); |
| 4286 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4287 | create<ast::UintLiteral>(&u32, 1))); |
| 4288 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4289 | create<ast::UintLiteral>(&u32, 1))); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4290 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 4291 | auto* ident = create<ast::IdentifierExpression>( |
| 4292 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4293 | |
| 4294 | ast::CallExpression call(ident, params); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4295 | |
| 4296 | EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4297 | ASSERT_NE(ident->result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4298 | EXPECT_TRUE(ident->result_type()->Is<ast::type::U32>()); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4299 | } |
| 4300 | |
| 4301 | TEST_P(ImportData_ThreeParam_FloatOrInt_Test, Uint_Vector) { |
| 4302 | auto param = GetParam(); |
| 4303 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4304 | ast::type::U32 u32; |
| 4305 | ast::type::Vector vec(&u32, 3); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4306 | |
| 4307 | ast::ExpressionList vals_1; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4308 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 4309 | create<ast::UintLiteral>(&u32, 1))); |
| 4310 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 4311 | create<ast::UintLiteral>(&u32, 1))); |
| 4312 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 4313 | create<ast::UintLiteral>(&u32, 3))); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4314 | |
| 4315 | ast::ExpressionList vals_2; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4316 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 4317 | create<ast::UintLiteral>(&u32, 1))); |
| 4318 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 4319 | create<ast::UintLiteral>(&u32, 1))); |
| 4320 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 4321 | create<ast::UintLiteral>(&u32, 3))); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4322 | |
| 4323 | ast::ExpressionList vals_3; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4324 | vals_3.push_back(create<ast::ScalarConstructorExpression>( |
| 4325 | create<ast::UintLiteral>(&u32, 1))); |
| 4326 | vals_3.push_back(create<ast::ScalarConstructorExpression>( |
| 4327 | create<ast::UintLiteral>(&u32, 1))); |
| 4328 | vals_3.push_back(create<ast::ScalarConstructorExpression>( |
| 4329 | create<ast::UintLiteral>(&u32, 3))); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4330 | |
| 4331 | ast::ExpressionList params; |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4332 | params.push_back(create<ast::TypeConstructorExpression>(&vec, vals_1)); |
| 4333 | params.push_back(create<ast::TypeConstructorExpression>(&vec, vals_2)); |
| 4334 | params.push_back(create<ast::TypeConstructorExpression>(&vec, vals_3)); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4335 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 4336 | auto* ident = create<ast::IdentifierExpression>( |
| 4337 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4338 | |
| 4339 | ast::CallExpression call(ident, params); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4340 | |
| 4341 | EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4342 | ASSERT_NE(ident->result_type(), nullptr); |
| 4343 | EXPECT_TRUE(ident->result_type()->is_unsigned_integer_vector()); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4344 | EXPECT_EQ(ident->result_type()->As<ast::type::Vector>()->size(), 3u); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4345 | } |
| 4346 | |
| 4347 | TEST_P(ImportData_ThreeParam_FloatOrInt_Test, Error_Bool) { |
| 4348 | auto param = GetParam(); |
| 4349 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4350 | ast::type::Bool bool_type; |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4351 | |
| 4352 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4353 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4354 | create<ast::BoolLiteral>(&bool_type, true))); |
| 4355 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4356 | create<ast::BoolLiteral>(&bool_type, false))); |
| 4357 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4358 | create<ast::BoolLiteral>(&bool_type, true))); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4359 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 4360 | auto* ident = create<ast::IdentifierExpression>( |
| 4361 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4362 | ast::CallExpression call(ident, params); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4363 | |
| 4364 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
| 4365 | EXPECT_EQ(td()->error(), |
| 4366 | std::string("incorrect type for ") + param.name + |
| 4367 | ". Requires float or int, scalar or vector values"); |
| 4368 | } |
| 4369 | |
| 4370 | TEST_P(ImportData_ThreeParam_FloatOrInt_Test, Error_NoParams) { |
| 4371 | auto param = GetParam(); |
| 4372 | |
| 4373 | ast::ExpressionList params; |
| 4374 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 4375 | auto* ident = create<ast::IdentifierExpression>( |
| 4376 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4377 | ast::CallExpression call(ident, params); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4378 | |
| 4379 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
| 4380 | EXPECT_EQ(td()->error(), std::string("incorrect number of parameters for ") + |
| 4381 | param.name + ". Expected 3 got 0"); |
| 4382 | } |
| 4383 | |
| 4384 | TEST_P(ImportData_ThreeParam_FloatOrInt_Test, Error_OneParam) { |
| 4385 | auto param = GetParam(); |
| 4386 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4387 | ast::type::F32 f32; |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4388 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4389 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4390 | create<ast::FloatLiteral>(&f32, 1.f))); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4391 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 4392 | auto* ident = create<ast::IdentifierExpression>( |
| 4393 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4394 | ast::CallExpression call(ident, params); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4395 | |
| 4396 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
| 4397 | EXPECT_EQ(td()->error(), std::string("incorrect number of parameters for ") + |
| 4398 | param.name + ". Expected 3 got 1"); |
| 4399 | } |
| 4400 | |
| 4401 | TEST_P(ImportData_ThreeParam_FloatOrInt_Test, Error_TwoParams) { |
| 4402 | auto param = GetParam(); |
| 4403 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4404 | ast::type::F32 f32; |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4405 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4406 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4407 | create<ast::FloatLiteral>(&f32, 1.f))); |
| 4408 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4409 | create<ast::FloatLiteral>(&f32, 1.f))); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4410 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 4411 | auto* ident = create<ast::IdentifierExpression>( |
| 4412 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4413 | ast::CallExpression call(ident, params); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4414 | |
| 4415 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
| 4416 | EXPECT_EQ(td()->error(), std::string("incorrect number of parameters for ") + |
| 4417 | param.name + ". Expected 3 got 2"); |
| 4418 | } |
| 4419 | |
| 4420 | TEST_P(ImportData_ThreeParam_FloatOrInt_Test, Error_MismatchedParamCount) { |
| 4421 | auto param = GetParam(); |
| 4422 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4423 | ast::type::F32 f32; |
| 4424 | ast::type::Vector vec2(&f32, 2); |
| 4425 | ast::type::Vector vec3(&f32, 3); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4426 | |
| 4427 | ast::ExpressionList vals_1; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4428 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 4429 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 4430 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 4431 | create<ast::FloatLiteral>(&f32, 1.0f))); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4432 | |
| 4433 | ast::ExpressionList vals_2; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4434 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 4435 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 4436 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 4437 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 4438 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 4439 | create<ast::FloatLiteral>(&f32, 3.0f))); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4440 | |
| 4441 | ast::ExpressionList vals_3; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4442 | vals_3.push_back(create<ast::ScalarConstructorExpression>( |
| 4443 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 4444 | vals_3.push_back(create<ast::ScalarConstructorExpression>( |
| 4445 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 4446 | vals_3.push_back(create<ast::ScalarConstructorExpression>( |
| 4447 | create<ast::FloatLiteral>(&f32, 3.0f))); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4448 | |
| 4449 | ast::ExpressionList params; |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4450 | params.push_back(create<ast::TypeConstructorExpression>(&vec2, vals_1)); |
| 4451 | params.push_back(create<ast::TypeConstructorExpression>(&vec3, vals_2)); |
| 4452 | params.push_back(create<ast::TypeConstructorExpression>(&vec3, vals_3)); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4453 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 4454 | auto* ident = create<ast::IdentifierExpression>( |
| 4455 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4456 | ast::CallExpression call(ident, params); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4457 | |
| 4458 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
| 4459 | EXPECT_EQ(td()->error(), |
| 4460 | std::string("mismatched parameter types for ") + param.name); |
| 4461 | } |
| 4462 | |
| 4463 | TEST_P(ImportData_ThreeParam_FloatOrInt_Test, Error_MismatchedParamType) { |
| 4464 | auto param = GetParam(); |
| 4465 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4466 | ast::type::F32 f32; |
| 4467 | ast::type::Vector vec(&f32, 3); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4468 | |
| 4469 | ast::ExpressionList vals; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4470 | vals.push_back(create<ast::ScalarConstructorExpression>( |
| 4471 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 4472 | vals.push_back(create<ast::ScalarConstructorExpression>( |
| 4473 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 4474 | vals.push_back(create<ast::ScalarConstructorExpression>( |
| 4475 | create<ast::FloatLiteral>(&f32, 3.0f))); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4476 | |
| 4477 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4478 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4479 | create<ast::FloatLiteral>(&f32, 1.0f))); |
| 4480 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4481 | create<ast::FloatLiteral>(&f32, 1.0f))); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4482 | params.push_back(create<ast::TypeConstructorExpression>(&vec, vals)); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4483 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 4484 | auto* ident = create<ast::IdentifierExpression>( |
| 4485 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4486 | ast::CallExpression call(ident, params); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4487 | |
| 4488 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
| 4489 | EXPECT_EQ(td()->error(), |
| 4490 | std::string("mismatched parameter types for ") + param.name); |
| 4491 | } |
| 4492 | |
| 4493 | TEST_P(ImportData_ThreeParam_FloatOrInt_Test, Error_TooManyParams) { |
| 4494 | auto param = GetParam(); |
| 4495 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4496 | ast::type::F32 f32; |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4497 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4498 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4499 | create<ast::FloatLiteral>(&f32, 1.f))); |
| 4500 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4501 | create<ast::FloatLiteral>(&f32, 1.f))); |
| 4502 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4503 | create<ast::FloatLiteral>(&f32, 1.f))); |
| 4504 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4505 | create<ast::FloatLiteral>(&f32, 1.f))); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4506 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 4507 | auto* ident = create<ast::IdentifierExpression>( |
| 4508 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4509 | ast::CallExpression call(ident, params); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4510 | |
| 4511 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
| 4512 | EXPECT_EQ(td()->error(), std::string("incorrect number of parameters for ") + |
| 4513 | param.name + ". Expected 3 got 4"); |
| 4514 | } |
| 4515 | |
| 4516 | INSTANTIATE_TEST_SUITE_P(TypeDeterminerTest, |
| 4517 | ImportData_ThreeParam_FloatOrInt_Test, |
| 4518 | testing::Values(IntrinsicData{ |
| 4519 | "clamp", ast::Intrinsic::kClamp})); |
| 4520 | |
| 4521 | using ImportData_Int_SingleParamTest = |
| 4522 | TypeDeterminerTestWithParam<IntrinsicData>; |
dan sinclair | 5e5fb9c | 2020-06-08 19:01:07 +0000 | [diff] [blame] | 4523 | TEST_P(ImportData_Int_SingleParamTest, Scalar) { |
| 4524 | auto param = GetParam(); |
| 4525 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4526 | ast::type::I32 i32; |
dan sinclair | 5e5fb9c | 2020-06-08 19:01:07 +0000 | [diff] [blame] | 4527 | |
| 4528 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4529 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4530 | create<ast::SintLiteral>(&i32, 1))); |
dan sinclair | 5e5fb9c | 2020-06-08 19:01:07 +0000 | [diff] [blame] | 4531 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 4532 | auto* ident = create<ast::IdentifierExpression>( |
| 4533 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4534 | |
| 4535 | ast::CallExpression call(ident, params); |
dan sinclair | 5e5fb9c | 2020-06-08 19:01:07 +0000 | [diff] [blame] | 4536 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4537 | EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4538 | ASSERT_NE(ident->result_type(), nullptr); |
| 4539 | EXPECT_TRUE(ident->result_type()->is_integer_scalar()); |
dan sinclair | 5e5fb9c | 2020-06-08 19:01:07 +0000 | [diff] [blame] | 4540 | } |
| 4541 | |
| 4542 | TEST_P(ImportData_Int_SingleParamTest, Vector) { |
| 4543 | auto param = GetParam(); |
| 4544 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4545 | ast::type::I32 i32; |
| 4546 | ast::type::Vector vec(&i32, 3); |
dan sinclair | 5e5fb9c | 2020-06-08 19:01:07 +0000 | [diff] [blame] | 4547 | |
| 4548 | ast::ExpressionList vals; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4549 | vals.push_back(create<ast::ScalarConstructorExpression>( |
| 4550 | create<ast::SintLiteral>(&i32, 1))); |
| 4551 | vals.push_back(create<ast::ScalarConstructorExpression>( |
| 4552 | create<ast::SintLiteral>(&i32, 1))); |
| 4553 | vals.push_back(create<ast::ScalarConstructorExpression>( |
| 4554 | create<ast::SintLiteral>(&i32, 3))); |
dan sinclair | 5e5fb9c | 2020-06-08 19:01:07 +0000 | [diff] [blame] | 4555 | |
| 4556 | ast::ExpressionList params; |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4557 | params.push_back(create<ast::TypeConstructorExpression>(&vec, vals)); |
dan sinclair | 5e5fb9c | 2020-06-08 19:01:07 +0000 | [diff] [blame] | 4558 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 4559 | auto* ident = create<ast::IdentifierExpression>( |
| 4560 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4561 | |
| 4562 | ast::CallExpression call(ident, params); |
dan sinclair | 5e5fb9c | 2020-06-08 19:01:07 +0000 | [diff] [blame] | 4563 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4564 | EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4565 | ASSERT_NE(ident->result_type(), nullptr); |
| 4566 | EXPECT_TRUE(ident->result_type()->is_signed_integer_vector()); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4567 | EXPECT_EQ(ident->result_type()->As<ast::type::Vector>()->size(), 3u); |
dan sinclair | 5e5fb9c | 2020-06-08 19:01:07 +0000 | [diff] [blame] | 4568 | } |
| 4569 | |
| 4570 | TEST_P(ImportData_Int_SingleParamTest, Error_Float) { |
| 4571 | auto param = GetParam(); |
| 4572 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4573 | ast::type::F32 f32; |
dan sinclair | 5e5fb9c | 2020-06-08 19:01:07 +0000 | [diff] [blame] | 4574 | |
| 4575 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4576 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4577 | create<ast::FloatLiteral>(&f32, 1.f))); |
dan sinclair | 5e5fb9c | 2020-06-08 19:01:07 +0000 | [diff] [blame] | 4578 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 4579 | auto* ident = create<ast::IdentifierExpression>( |
| 4580 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4581 | ast::CallExpression call(ident, params); |
dan sinclair | 5e5fb9c | 2020-06-08 19:01:07 +0000 | [diff] [blame] | 4582 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4583 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
dan sinclair | 5e5fb9c | 2020-06-08 19:01:07 +0000 | [diff] [blame] | 4584 | EXPECT_EQ(td()->error(), |
| 4585 | std::string("incorrect type for ") + param.name + |
| 4586 | ". Requires integer scalar or integer vector values"); |
| 4587 | } |
| 4588 | |
| 4589 | TEST_P(ImportData_Int_SingleParamTest, Error_NoParams) { |
| 4590 | auto param = GetParam(); |
| 4591 | |
| 4592 | ast::ExpressionList params; |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4593 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 4594 | auto* ident = create<ast::IdentifierExpression>( |
| 4595 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4596 | ast::CallExpression call(ident, params); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4597 | |
| 4598 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
dan sinclair | 5e5fb9c | 2020-06-08 19:01:07 +0000 | [diff] [blame] | 4599 | EXPECT_EQ(td()->error(), std::string("incorrect number of parameters for ") + |
| 4600 | param.name + ". Expected 1 got 0"); |
| 4601 | } |
| 4602 | |
| 4603 | TEST_P(ImportData_Int_SingleParamTest, Error_MultipleParams) { |
| 4604 | auto param = GetParam(); |
| 4605 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4606 | ast::type::I32 i32; |
dan sinclair | 5e5fb9c | 2020-06-08 19:01:07 +0000 | [diff] [blame] | 4607 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4608 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4609 | create<ast::SintLiteral>(&i32, 1))); |
| 4610 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4611 | create<ast::SintLiteral>(&i32, 1))); |
| 4612 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4613 | create<ast::SintLiteral>(&i32, 1))); |
dan sinclair | 5e5fb9c | 2020-06-08 19:01:07 +0000 | [diff] [blame] | 4614 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 4615 | auto* ident = create<ast::IdentifierExpression>( |
| 4616 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4617 | ast::CallExpression call(ident, params); |
dan sinclair | 5e5fb9c | 2020-06-08 19:01:07 +0000 | [diff] [blame] | 4618 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4619 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
dan sinclair | 5e5fb9c | 2020-06-08 19:01:07 +0000 | [diff] [blame] | 4620 | EXPECT_EQ(td()->error(), std::string("incorrect number of parameters for ") + |
| 4621 | param.name + ". Expected 1 got 3"); |
| 4622 | } |
| 4623 | |
dan sinclair | af5df70 | 2020-06-08 23:48:26 +0000 | [diff] [blame] | 4624 | INSTANTIATE_TEST_SUITE_P( |
| 4625 | TypeDeterminerTest, |
| 4626 | ImportData_Int_SingleParamTest, |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4627 | testing::Values( |
| 4628 | IntrinsicData{"countOneBits", ast::Intrinsic::kCountOneBits}, |
| 4629 | IntrinsicData{"reverseBits", ast::Intrinsic::kReverseBits})); |
dan sinclair | 5e5fb9c | 2020-06-08 19:01:07 +0000 | [diff] [blame] | 4630 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4631 | using ImportData_FloatOrInt_TwoParamTest = |
| 4632 | TypeDeterminerTestWithParam<IntrinsicData>; |
| 4633 | TEST_P(ImportData_FloatOrInt_TwoParamTest, Scalar_Signed) { |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4634 | auto param = GetParam(); |
| 4635 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4636 | ast::type::I32 i32; |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4637 | |
| 4638 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4639 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4640 | create<ast::SintLiteral>(&i32, 1))); |
| 4641 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4642 | create<ast::SintLiteral>(&i32, 1))); |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4643 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 4644 | auto* ident = create<ast::IdentifierExpression>( |
| 4645 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4646 | |
| 4647 | ast::CallExpression call(ident, params); |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4648 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4649 | EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4650 | ASSERT_NE(ident->result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4651 | EXPECT_TRUE(ident->result_type()->Is<ast::type::I32>()); |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4652 | } |
| 4653 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4654 | TEST_P(ImportData_FloatOrInt_TwoParamTest, Scalar_Unsigned) { |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4655 | auto param = GetParam(); |
| 4656 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4657 | ast::type::U32 u32; |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4658 | |
| 4659 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4660 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4661 | create<ast::UintLiteral>(&u32, 1))); |
| 4662 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4663 | create<ast::UintLiteral>(&u32, 1))); |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4664 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 4665 | auto* ident = create<ast::IdentifierExpression>( |
| 4666 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4667 | |
| 4668 | ast::CallExpression call(ident, params); |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4669 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4670 | EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4671 | ASSERT_NE(ident->result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4672 | EXPECT_TRUE(ident->result_type()->Is<ast::type::U32>()); |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4673 | } |
| 4674 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4675 | TEST_P(ImportData_FloatOrInt_TwoParamTest, Scalar_Float) { |
| 4676 | auto param = GetParam(); |
| 4677 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4678 | ast::type::F32 f32; |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4679 | |
| 4680 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4681 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4682 | create<ast::FloatLiteral>(&f32, 1))); |
| 4683 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4684 | create<ast::FloatLiteral>(&f32, 1))); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4685 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 4686 | auto* ident = create<ast::IdentifierExpression>( |
| 4687 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4688 | |
| 4689 | ast::CallExpression call(ident, params); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4690 | |
| 4691 | EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4692 | ASSERT_NE(ident->result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4693 | EXPECT_TRUE(ident->result_type()->Is<ast::type::F32>()); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4694 | } |
| 4695 | |
| 4696 | TEST_P(ImportData_FloatOrInt_TwoParamTest, Vector_Signed) { |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4697 | auto param = GetParam(); |
| 4698 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4699 | ast::type::I32 i32; |
| 4700 | ast::type::Vector vec(&i32, 3); |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4701 | |
| 4702 | ast::ExpressionList vals_1; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4703 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 4704 | create<ast::SintLiteral>(&i32, 1))); |
| 4705 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 4706 | create<ast::SintLiteral>(&i32, 1))); |
| 4707 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 4708 | create<ast::SintLiteral>(&i32, 3))); |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4709 | |
| 4710 | ast::ExpressionList vals_2; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4711 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 4712 | create<ast::SintLiteral>(&i32, 1))); |
| 4713 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 4714 | create<ast::SintLiteral>(&i32, 1))); |
| 4715 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 4716 | create<ast::SintLiteral>(&i32, 3))); |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4717 | |
| 4718 | ast::ExpressionList params; |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4719 | params.push_back(create<ast::TypeConstructorExpression>(&vec, vals_1)); |
| 4720 | params.push_back(create<ast::TypeConstructorExpression>(&vec, vals_2)); |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4721 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 4722 | auto* ident = create<ast::IdentifierExpression>( |
| 4723 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4724 | |
| 4725 | ast::CallExpression call(ident, params); |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4726 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4727 | EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4728 | ASSERT_NE(ident->result_type(), nullptr); |
| 4729 | EXPECT_TRUE(ident->result_type()->is_signed_integer_vector()); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4730 | EXPECT_EQ(ident->result_type()->As<ast::type::Vector>()->size(), 3u); |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4731 | } |
| 4732 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4733 | TEST_P(ImportData_FloatOrInt_TwoParamTest, Vector_Unsigned) { |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4734 | auto param = GetParam(); |
| 4735 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4736 | ast::type::U32 u32; |
| 4737 | ast::type::Vector vec(&u32, 3); |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4738 | |
| 4739 | ast::ExpressionList vals_1; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4740 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 4741 | create<ast::UintLiteral>(&u32, 1))); |
| 4742 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 4743 | create<ast::UintLiteral>(&u32, 1))); |
| 4744 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 4745 | create<ast::UintLiteral>(&u32, 3))); |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4746 | |
| 4747 | ast::ExpressionList vals_2; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4748 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 4749 | create<ast::UintLiteral>(&u32, 1))); |
| 4750 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 4751 | create<ast::UintLiteral>(&u32, 1))); |
| 4752 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 4753 | create<ast::UintLiteral>(&u32, 3))); |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4754 | |
| 4755 | ast::ExpressionList params; |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4756 | params.push_back(create<ast::TypeConstructorExpression>(&vec, vals_1)); |
| 4757 | params.push_back(create<ast::TypeConstructorExpression>(&vec, vals_2)); |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4758 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 4759 | auto* ident = create<ast::IdentifierExpression>( |
| 4760 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4761 | |
| 4762 | ast::CallExpression call(ident, params); |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4763 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4764 | EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4765 | ASSERT_NE(ident->result_type(), nullptr); |
| 4766 | EXPECT_TRUE(ident->result_type()->is_unsigned_integer_vector()); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4767 | EXPECT_EQ(ident->result_type()->As<ast::type::Vector>()->size(), 3u); |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4768 | } |
| 4769 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4770 | TEST_P(ImportData_FloatOrInt_TwoParamTest, Vector_Float) { |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4771 | auto param = GetParam(); |
| 4772 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4773 | ast::type::F32 f32; |
| 4774 | ast::type::Vector vec(&f32, 3); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4775 | |
| 4776 | ast::ExpressionList vals_1; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4777 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 4778 | create<ast::FloatLiteral>(&f32, 1))); |
| 4779 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 4780 | create<ast::FloatLiteral>(&f32, 1))); |
| 4781 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 4782 | create<ast::FloatLiteral>(&f32, 3))); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4783 | |
| 4784 | ast::ExpressionList vals_2; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4785 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 4786 | create<ast::FloatLiteral>(&f32, 1))); |
| 4787 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 4788 | create<ast::FloatLiteral>(&f32, 1))); |
| 4789 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 4790 | create<ast::FloatLiteral>(&f32, 3))); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4791 | |
| 4792 | ast::ExpressionList params; |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4793 | params.push_back(create<ast::TypeConstructorExpression>(&vec, vals_1)); |
| 4794 | params.push_back(create<ast::TypeConstructorExpression>(&vec, vals_2)); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4795 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 4796 | auto* ident = create<ast::IdentifierExpression>( |
| 4797 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4798 | |
| 4799 | ast::CallExpression call(ident, params); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4800 | |
| 4801 | EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4802 | ASSERT_NE(ident->result_type(), nullptr); |
| 4803 | EXPECT_TRUE(ident->result_type()->is_float_vector()); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4804 | EXPECT_EQ(ident->result_type()->As<ast::type::Vector>()->size(), 3u); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4805 | } |
| 4806 | |
| 4807 | TEST_P(ImportData_FloatOrInt_TwoParamTest, Error_Bool) { |
| 4808 | auto param = GetParam(); |
| 4809 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4810 | ast::type::Bool bool_type; |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4811 | |
| 4812 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4813 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4814 | create<ast::BoolLiteral>(&bool_type, true))); |
| 4815 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4816 | create<ast::BoolLiteral>(&bool_type, false))); |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4817 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 4818 | auto* ident = create<ast::IdentifierExpression>( |
| 4819 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4820 | ast::CallExpression call(ident, params); |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4821 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4822 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4823 | EXPECT_EQ(td()->error(), |
| 4824 | std::string("incorrect type for ") + param.name + |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4825 | ". Requires float or int, scalar or vector values"); |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4826 | } |
| 4827 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4828 | TEST_P(ImportData_FloatOrInt_TwoParamTest, Error_NoParams) { |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4829 | auto param = GetParam(); |
| 4830 | |
| 4831 | ast::ExpressionList params; |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4832 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 4833 | auto* ident = create<ast::IdentifierExpression>( |
| 4834 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4835 | ast::CallExpression call(ident, params); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4836 | |
| 4837 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4838 | EXPECT_EQ(td()->error(), std::string("incorrect number of parameters for ") + |
| 4839 | param.name + ". Expected 2 got 0"); |
| 4840 | } |
| 4841 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4842 | TEST_P(ImportData_FloatOrInt_TwoParamTest, Error_OneParam) { |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4843 | auto param = GetParam(); |
| 4844 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4845 | ast::type::I32 i32; |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4846 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4847 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4848 | create<ast::SintLiteral>(&i32, 1))); |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4849 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 4850 | auto* ident = create<ast::IdentifierExpression>( |
| 4851 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4852 | ast::CallExpression call(ident, params); |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4853 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4854 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4855 | EXPECT_EQ(td()->error(), std::string("incorrect number of parameters for ") + |
| 4856 | param.name + ". Expected 2 got 1"); |
| 4857 | } |
| 4858 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4859 | TEST_P(ImportData_FloatOrInt_TwoParamTest, Error_MismatchedParamCount) { |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4860 | auto param = GetParam(); |
| 4861 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4862 | ast::type::I32 i32; |
| 4863 | ast::type::Vector vec2(&i32, 2); |
| 4864 | ast::type::Vector vec3(&i32, 3); |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4865 | |
| 4866 | ast::ExpressionList vals_1; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4867 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 4868 | create<ast::SintLiteral>(&i32, 1))); |
| 4869 | vals_1.push_back(create<ast::ScalarConstructorExpression>( |
| 4870 | create<ast::SintLiteral>(&i32, 1))); |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4871 | |
| 4872 | ast::ExpressionList vals_2; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4873 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 4874 | create<ast::SintLiteral>(&i32, 1))); |
| 4875 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 4876 | create<ast::SintLiteral>(&i32, 1))); |
| 4877 | vals_2.push_back(create<ast::ScalarConstructorExpression>( |
| 4878 | create<ast::SintLiteral>(&i32, 3))); |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4879 | |
| 4880 | ast::ExpressionList params; |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4881 | params.push_back(create<ast::TypeConstructorExpression>(&vec2, vals_1)); |
| 4882 | params.push_back(create<ast::TypeConstructorExpression>(&vec3, vals_2)); |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4883 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 4884 | auto* ident = create<ast::IdentifierExpression>( |
| 4885 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4886 | ast::CallExpression call(ident, params); |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4887 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4888 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4889 | EXPECT_EQ(td()->error(), |
| 4890 | std::string("mismatched parameter types for ") + param.name); |
| 4891 | } |
| 4892 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4893 | TEST_P(ImportData_FloatOrInt_TwoParamTest, Error_MismatchedParamType) { |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4894 | auto param = GetParam(); |
| 4895 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4896 | ast::type::I32 i32; |
| 4897 | ast::type::Vector vec(&i32, 3); |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4898 | |
| 4899 | ast::ExpressionList vals; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4900 | vals.push_back(create<ast::ScalarConstructorExpression>( |
| 4901 | create<ast::SintLiteral>(&i32, 1))); |
| 4902 | vals.push_back(create<ast::ScalarConstructorExpression>( |
| 4903 | create<ast::SintLiteral>(&i32, 1))); |
| 4904 | vals.push_back(create<ast::ScalarConstructorExpression>( |
| 4905 | create<ast::SintLiteral>(&i32, 3))); |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4906 | |
| 4907 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4908 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4909 | create<ast::SintLiteral>(&i32, 1))); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4910 | params.push_back(create<ast::TypeConstructorExpression>(&vec, vals)); |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4911 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 4912 | auto* ident = create<ast::IdentifierExpression>( |
| 4913 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4914 | ast::CallExpression call(ident, params); |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4915 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4916 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4917 | EXPECT_EQ(td()->error(), |
| 4918 | std::string("mismatched parameter types for ") + param.name); |
| 4919 | } |
| 4920 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4921 | TEST_P(ImportData_FloatOrInt_TwoParamTest, Error_TooManyParams) { |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4922 | auto param = GetParam(); |
| 4923 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4924 | ast::type::I32 i32; |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4925 | ast::ExpressionList params; |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 4926 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4927 | create<ast::SintLiteral>(&i32, 1))); |
| 4928 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4929 | create<ast::SintLiteral>(&i32, 1))); |
| 4930 | params.push_back(create<ast::ScalarConstructorExpression>( |
| 4931 | create<ast::SintLiteral>(&i32, 1))); |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4932 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 4933 | auto* ident = create<ast::IdentifierExpression>( |
| 4934 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4935 | ast::CallExpression call(ident, params); |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4936 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4937 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4938 | EXPECT_EQ(td()->error(), std::string("incorrect number of parameters for ") + |
| 4939 | param.name + ". Expected 2 got 3"); |
| 4940 | } |
| 4941 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4942 | INSTANTIATE_TEST_SUITE_P( |
| 4943 | TypeDeterminerTest, |
| 4944 | ImportData_FloatOrInt_TwoParamTest, |
| 4945 | testing::Values(IntrinsicData{"min", ast::Intrinsic::kMin}, |
| 4946 | IntrinsicData{"max", ast::Intrinsic::kMax})); |
dan sinclair | 92bb557 | 2020-06-08 23:48:07 +0000 | [diff] [blame] | 4947 | |
dan sinclair | 3238eaa | 2020-06-17 20:22:08 +0000 | [diff] [blame] | 4948 | TEST_F(TypeDeterminerTest, ImportData_GLSL_Determinant) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4949 | ast::type::F32 f32; |
| 4950 | ast::type::Matrix mat(&f32, 3, 3); |
dan sinclair | 3819c26 | 2020-06-17 18:39:17 +0000 | [diff] [blame] | 4951 | |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 4952 | auto* var = |
| 4953 | create<ast::Variable>(Source{}, // source |
| 4954 | "var", // name |
| 4955 | ast::StorageClass::kFunction, // storage_class |
| 4956 | &mat, // type |
| 4957 | false, // is_const |
| 4958 | nullptr, // constructor |
| 4959 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 4960 | mod->AddGlobalVariable(var); |
dan sinclair | 3819c26 | 2020-06-17 18:39:17 +0000 | [diff] [blame] | 4961 | |
| 4962 | // Register the global |
| 4963 | ASSERT_TRUE(td()->Determine()) << td()->error(); |
| 4964 | |
| 4965 | ast::ExpressionList params; |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 4966 | params.push_back( |
| 4967 | create<ast::IdentifierExpression>(mod->RegisterSymbol("var"), "var")); |
dan sinclair | 3819c26 | 2020-06-17 18:39:17 +0000 | [diff] [blame] | 4968 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 4969 | auto* ident = create<ast::IdentifierExpression>( |
| 4970 | mod->RegisterSymbol("determinant"), "determinant"); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4971 | |
| 4972 | ast::CallExpression call(ident, params); |
dan sinclair | 3819c26 | 2020-06-17 18:39:17 +0000 | [diff] [blame] | 4973 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4974 | EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 4975 | ASSERT_NE(ident->result_type(), nullptr); |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4976 | EXPECT_TRUE(ident->result_type()->Is<ast::type::F32>()); |
dan sinclair | 3819c26 | 2020-06-17 18:39:17 +0000 | [diff] [blame] | 4977 | } |
| 4978 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 4979 | using ImportData_Matrix_OneParam_Test = |
| 4980 | TypeDeterminerTestWithParam<IntrinsicData>; |
dan sinclair | 3238eaa | 2020-06-17 20:22:08 +0000 | [diff] [blame] | 4981 | TEST_P(ImportData_Matrix_OneParam_Test, Error_Float) { |
| 4982 | auto param = GetParam(); |
| 4983 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 4984 | ast::type::F32 f32; |
dan sinclair | 3819c26 | 2020-06-17 18:39:17 +0000 | [diff] [blame] | 4985 | |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 4986 | auto* var = |
| 4987 | create<ast::Variable>(Source{}, // source |
| 4988 | "var", // name |
| 4989 | ast::StorageClass::kFunction, // storage_class |
| 4990 | &f32, // type |
| 4991 | false, // is_const |
| 4992 | nullptr, // constructor |
| 4993 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 4994 | mod->AddGlobalVariable(var); |
dan sinclair | 3819c26 | 2020-06-17 18:39:17 +0000 | [diff] [blame] | 4995 | |
| 4996 | // Register the global |
| 4997 | ASSERT_TRUE(td()->Determine()) << td()->error(); |
| 4998 | |
| 4999 | ast::ExpressionList params; |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 5000 | params.push_back( |
| 5001 | create<ast::IdentifierExpression>(mod->RegisterSymbol("var"), "var")); |
dan sinclair | 3819c26 | 2020-06-17 18:39:17 +0000 | [diff] [blame] | 5002 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 5003 | auto* ident = create<ast::IdentifierExpression>( |
| 5004 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 5005 | ast::CallExpression call(ident, params); |
dan sinclair | 3819c26 | 2020-06-17 18:39:17 +0000 | [diff] [blame] | 5006 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 5007 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
dan sinclair | 3238eaa | 2020-06-17 20:22:08 +0000 | [diff] [blame] | 5008 | EXPECT_EQ(td()->error(), std::string("incorrect type for ") + param.name + |
| 5009 | ". Requires matrix value"); |
dan sinclair | 3819c26 | 2020-06-17 18:39:17 +0000 | [diff] [blame] | 5010 | } |
| 5011 | |
dan sinclair | 3238eaa | 2020-06-17 20:22:08 +0000 | [diff] [blame] | 5012 | TEST_P(ImportData_Matrix_OneParam_Test, NoParams) { |
| 5013 | auto param = GetParam(); |
| 5014 | |
dan sinclair | 3819c26 | 2020-06-17 18:39:17 +0000 | [diff] [blame] | 5015 | ast::ExpressionList params; |
| 5016 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 5017 | auto* ident = create<ast::IdentifierExpression>( |
| 5018 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 5019 | ast::CallExpression call(ident, params); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 5020 | |
| 5021 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
dan sinclair | 3238eaa | 2020-06-17 20:22:08 +0000 | [diff] [blame] | 5022 | EXPECT_EQ(td()->error(), std::string("incorrect number of parameters for ") + |
| 5023 | param.name + ". Expected 1 got 0"); |
dan sinclair | 3819c26 | 2020-06-17 18:39:17 +0000 | [diff] [blame] | 5024 | } |
| 5025 | |
dan sinclair | 3238eaa | 2020-06-17 20:22:08 +0000 | [diff] [blame] | 5026 | TEST_P(ImportData_Matrix_OneParam_Test, TooManyParams) { |
| 5027 | auto param = GetParam(); |
| 5028 | |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 5029 | ast::type::F32 f32; |
| 5030 | ast::type::Matrix mat(&f32, 3, 3); |
dan sinclair | 3819c26 | 2020-06-17 18:39:17 +0000 | [diff] [blame] | 5031 | |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 5032 | auto* var = |
| 5033 | create<ast::Variable>(Source{}, // source |
| 5034 | "var", // name |
| 5035 | ast::StorageClass::kFunction, // storage_class |
| 5036 | &mat, // type |
| 5037 | false, // is_const |
| 5038 | nullptr, // constructor |
| 5039 | ast::VariableDecorationList{}); // decorations |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5040 | mod->AddGlobalVariable(var); |
dan sinclair | 3819c26 | 2020-06-17 18:39:17 +0000 | [diff] [blame] | 5041 | |
| 5042 | // Register the global |
| 5043 | ASSERT_TRUE(td()->Determine()) << td()->error(); |
| 5044 | |
| 5045 | ast::ExpressionList params; |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 5046 | params.push_back( |
| 5047 | create<ast::IdentifierExpression>(mod->RegisterSymbol("var"), "var")); |
| 5048 | params.push_back( |
| 5049 | create<ast::IdentifierExpression>(mod->RegisterSymbol("var"), "var")); |
dan sinclair | 3819c26 | 2020-06-17 18:39:17 +0000 | [diff] [blame] | 5050 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 5051 | auto* ident = create<ast::IdentifierExpression>( |
| 5052 | mod->RegisterSymbol(param.name), param.name); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 5053 | ast::CallExpression call(ident, params); |
dan sinclair | 3819c26 | 2020-06-17 18:39:17 +0000 | [diff] [blame] | 5054 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 5055 | EXPECT_FALSE(td()->DetermineResultType(&call)); |
dan sinclair | 3238eaa | 2020-06-17 20:22:08 +0000 | [diff] [blame] | 5056 | EXPECT_EQ(td()->error(), std::string("incorrect number of parameters for ") + |
| 5057 | param.name + ". Expected 1 got 2"); |
dan sinclair | 3819c26 | 2020-06-17 18:39:17 +0000 | [diff] [blame] | 5058 | } |
dan sinclair | e9598d6 | 2020-06-18 18:03:00 +0000 | [diff] [blame] | 5059 | INSTANTIATE_TEST_SUITE_P(TypeDeterminerTest, |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 5060 | ImportData_Matrix_OneParam_Test, |
| 5061 | testing::Values(IntrinsicData{ |
| 5062 | "determinant", ast::Intrinsic::kDeterminant})); |
dan sinclair | e9598d6 | 2020-06-18 18:03:00 +0000 | [diff] [blame] | 5063 | |
dan sinclair | 0592643 | 2020-09-21 17:51:31 +0000 | [diff] [blame] | 5064 | TEST_F(TypeDeterminerTest, Function_EntryPoints_StageDecoration) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 5065 | ast::type::F32 f32; |
dan sinclair | 0592643 | 2020-09-21 17:51:31 +0000 | [diff] [blame] | 5066 | |
| 5067 | // fn b() {} |
| 5068 | // fn c() { b(); } |
| 5069 | // fn a() { c(); } |
| 5070 | // fn ep_1() { a(); b(); } |
| 5071 | // fn ep_2() { c();} |
| 5072 | // |
| 5073 | // c -> {ep_1, ep_2} |
| 5074 | // a -> {ep_1} |
| 5075 | // b -> {ep_1, ep_2} |
| 5076 | // ep_1 -> {} |
| 5077 | // ep_2 -> {} |
| 5078 | |
| 5079 | ast::VariableList params; |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 5080 | auto* body = create<ast::BlockStatement>(); |
dan sinclair | a41132f | 2020-12-11 18:24:53 +0000 | [diff] [blame] | 5081 | auto* func_b = |
| 5082 | create<ast::Function>(Source{}, mod->RegisterSymbol("b"), "b", params, |
| 5083 | &f32, body, ast::FunctionDecorationList{}); |
dan sinclair | 0592643 | 2020-09-21 17:51:31 +0000 | [diff] [blame] | 5084 | |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 5085 | body = create<ast::BlockStatement>(); |
| 5086 | body->append(create<ast::AssignmentStatement>( |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 5087 | create<ast::IdentifierExpression>(mod->RegisterSymbol("second"), |
| 5088 | "second"), |
| 5089 | create<ast::CallExpression>( |
| 5090 | create<ast::IdentifierExpression>(mod->RegisterSymbol("b"), "b"), |
| 5091 | ast::ExpressionList{}))); |
dan sinclair | a41132f | 2020-12-11 18:24:53 +0000 | [diff] [blame] | 5092 | auto* func_c = |
| 5093 | create<ast::Function>(Source{}, mod->RegisterSymbol("c"), "c", params, |
| 5094 | &f32, body, ast::FunctionDecorationList{}); |
dan sinclair | 0592643 | 2020-09-21 17:51:31 +0000 | [diff] [blame] | 5095 | |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 5096 | body = create<ast::BlockStatement>(); |
| 5097 | body->append(create<ast::AssignmentStatement>( |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 5098 | create<ast::IdentifierExpression>(mod->RegisterSymbol("first"), "first"), |
| 5099 | create<ast::CallExpression>( |
| 5100 | create<ast::IdentifierExpression>(mod->RegisterSymbol("c"), "c"), |
| 5101 | ast::ExpressionList{}))); |
dan sinclair | a41132f | 2020-12-11 18:24:53 +0000 | [diff] [blame] | 5102 | auto* func_a = |
| 5103 | create<ast::Function>(Source{}, mod->RegisterSymbol("a"), "a", params, |
| 5104 | &f32, body, ast::FunctionDecorationList{}); |
dan sinclair | 0592643 | 2020-09-21 17:51:31 +0000 | [diff] [blame] | 5105 | |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 5106 | body = create<ast::BlockStatement>(); |
| 5107 | body->append(create<ast::AssignmentStatement>( |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 5108 | create<ast::IdentifierExpression>(mod->RegisterSymbol("call_a"), |
| 5109 | "call_a"), |
| 5110 | create<ast::CallExpression>( |
| 5111 | create<ast::IdentifierExpression>(mod->RegisterSymbol("a"), "a"), |
| 5112 | ast::ExpressionList{}))); |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 5113 | body->append(create<ast::AssignmentStatement>( |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 5114 | create<ast::IdentifierExpression>(mod->RegisterSymbol("call_b"), |
| 5115 | "call_b"), |
| 5116 | create<ast::CallExpression>( |
| 5117 | create<ast::IdentifierExpression>(mod->RegisterSymbol("b"), "b"), |
| 5118 | ast::ExpressionList{}))); |
Ben Clayton | 234b7de | 2020-12-07 20:45:14 +0000 | [diff] [blame] | 5119 | auto* ep_1 = create<ast::Function>( |
dan sinclair | a41132f | 2020-12-11 18:24:53 +0000 | [diff] [blame] | 5120 | Source{}, mod->RegisterSymbol("ep_1"), "ep_1", params, &f32, body, |
Ben Clayton | 234b7de | 2020-12-07 20:45:14 +0000 | [diff] [blame] | 5121 | ast::FunctionDecorationList{ |
| 5122 | create<ast::StageDecoration>(ast::PipelineStage::kVertex, Source{}), |
| 5123 | }); |
dan sinclair | 0592643 | 2020-09-21 17:51:31 +0000 | [diff] [blame] | 5124 | |
Ben Clayton | 6262592 | 2020-11-13 22:09:38 +0000 | [diff] [blame] | 5125 | body = create<ast::BlockStatement>(); |
| 5126 | body->append(create<ast::AssignmentStatement>( |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 5127 | create<ast::IdentifierExpression>(mod->RegisterSymbol("call_c"), |
| 5128 | "call_c"), |
| 5129 | create<ast::CallExpression>( |
| 5130 | create<ast::IdentifierExpression>(mod->RegisterSymbol("c"), "c"), |
| 5131 | ast::ExpressionList{}))); |
Ben Clayton | 234b7de | 2020-12-07 20:45:14 +0000 | [diff] [blame] | 5132 | auto* ep_2 = create<ast::Function>( |
dan sinclair | a41132f | 2020-12-11 18:24:53 +0000 | [diff] [blame] | 5133 | Source{}, mod->RegisterSymbol("ep_2"), "ep_2", params, &f32, body, |
Ben Clayton | 234b7de | 2020-12-07 20:45:14 +0000 | [diff] [blame] | 5134 | ast::FunctionDecorationList{ |
| 5135 | create<ast::StageDecoration>(ast::PipelineStage::kVertex, Source{}), |
| 5136 | }); |
dan sinclair | 0592643 | 2020-09-21 17:51:31 +0000 | [diff] [blame] | 5137 | |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5138 | mod->AddFunction(func_b); |
| 5139 | mod->AddFunction(func_c); |
| 5140 | mod->AddFunction(func_a); |
| 5141 | mod->AddFunction(ep_1); |
| 5142 | mod->AddFunction(ep_2); |
dan sinclair | 0592643 | 2020-09-21 17:51:31 +0000 | [diff] [blame] | 5143 | |
Ben Clayton | a80511e | 2020-12-11 13:07:02 +0000 | [diff] [blame] | 5144 | mod->AddGlobalVariable( |
| 5145 | create<ast::Variable>(Source{}, // source |
| 5146 | "first", // name |
| 5147 | ast::StorageClass::kPrivate, // storage_class |
| 5148 | &f32, // type |
| 5149 | false, // is_const |
| 5150 | nullptr, // constructor |
| 5151 | ast::VariableDecorationList{})); // decorations |
| 5152 | mod->AddGlobalVariable( |
| 5153 | create<ast::Variable>(Source{}, // source |
| 5154 | "second", // name |
| 5155 | ast::StorageClass::kPrivate, // storage_class |
| 5156 | &f32, // type |
| 5157 | false, // is_const |
| 5158 | nullptr, // constructor |
| 5159 | ast::VariableDecorationList{})); // decorations |
| 5160 | mod->AddGlobalVariable( |
| 5161 | create<ast::Variable>(Source{}, // source |
| 5162 | "call_a", // name |
| 5163 | ast::StorageClass::kPrivate, // storage_class |
| 5164 | &f32, // type |
| 5165 | false, // is_const |
| 5166 | nullptr, // constructor |
| 5167 | ast::VariableDecorationList{})); // decorations |
| 5168 | mod->AddGlobalVariable( |
| 5169 | create<ast::Variable>(Source{}, // source |
| 5170 | "call_b", // name |
| 5171 | ast::StorageClass::kPrivate, // storage_class |
| 5172 | &f32, // type |
| 5173 | false, // is_const |
| 5174 | nullptr, // constructor |
| 5175 | ast::VariableDecorationList{})); // decorations |
| 5176 | mod->AddGlobalVariable( |
| 5177 | create<ast::Variable>(Source{}, // source |
| 5178 | "call_c", // name |
| 5179 | ast::StorageClass::kPrivate, // storage_class |
| 5180 | &f32, // type |
| 5181 | false, // is_const |
| 5182 | nullptr, // constructor |
| 5183 | ast::VariableDecorationList{})); // decorations |
dan sinclair | ff267ca | 2020-10-14 18:26:31 +0000 | [diff] [blame] | 5184 | |
dan sinclair | 0592643 | 2020-09-21 17:51:31 +0000 | [diff] [blame] | 5185 | // Register the functions and calculate the callers |
| 5186 | ASSERT_TRUE(td()->Determine()) << td()->error(); |
| 5187 | |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 5188 | const auto& b_eps = func_b->ancestor_entry_points(); |
dan sinclair | 0592643 | 2020-09-21 17:51:31 +0000 | [diff] [blame] | 5189 | ASSERT_EQ(2u, b_eps.size()); |
dan sinclair | a41132f | 2020-12-11 18:24:53 +0000 | [diff] [blame] | 5190 | EXPECT_EQ(mod->RegisterSymbol("ep_1"), b_eps[0]); |
| 5191 | EXPECT_EQ(mod->RegisterSymbol("ep_2"), b_eps[1]); |
dan sinclair | 0592643 | 2020-09-21 17:51:31 +0000 | [diff] [blame] | 5192 | |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 5193 | const auto& a_eps = func_a->ancestor_entry_points(); |
dan sinclair | 0592643 | 2020-09-21 17:51:31 +0000 | [diff] [blame] | 5194 | ASSERT_EQ(1u, a_eps.size()); |
dan sinclair | a41132f | 2020-12-11 18:24:53 +0000 | [diff] [blame] | 5195 | EXPECT_EQ(mod->RegisterSymbol("ep_1"), a_eps[0]); |
dan sinclair | 0592643 | 2020-09-21 17:51:31 +0000 | [diff] [blame] | 5196 | |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 5197 | const auto& c_eps = func_c->ancestor_entry_points(); |
dan sinclair | 0592643 | 2020-09-21 17:51:31 +0000 | [diff] [blame] | 5198 | ASSERT_EQ(2u, c_eps.size()); |
dan sinclair | a41132f | 2020-12-11 18:24:53 +0000 | [diff] [blame] | 5199 | EXPECT_EQ(mod->RegisterSymbol("ep_1"), c_eps[0]); |
| 5200 | EXPECT_EQ(mod->RegisterSymbol("ep_2"), c_eps[1]); |
dan sinclair | 0592643 | 2020-09-21 17:51:31 +0000 | [diff] [blame] | 5201 | |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 5202 | EXPECT_TRUE(ep_1->ancestor_entry_points().empty()); |
| 5203 | EXPECT_TRUE(ep_2->ancestor_entry_points().empty()); |
dan sinclair | 0592643 | 2020-09-21 17:51:31 +0000 | [diff] [blame] | 5204 | } |
| 5205 | |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5206 | using TypeDeterminerTextureIntrinsicTest = |
| 5207 | TypeDeterminerTestWithParam<ast::intrinsic::test::TextureOverloadCase>; |
| 5208 | |
| 5209 | INSTANTIATE_TEST_SUITE_P( |
| 5210 | TypeDeterminerTest, |
| 5211 | TypeDeterminerTextureIntrinsicTest, |
| 5212 | testing::ValuesIn(ast::intrinsic::test::TextureOverloadCase::ValidCases())); |
| 5213 | |
| 5214 | std::string to_str(const std::string& function, |
| 5215 | const ast::intrinsic::TextureSignature* sig) { |
| 5216 | struct Parameter { |
| 5217 | size_t idx; |
| 5218 | std::string name; |
| 5219 | }; |
| 5220 | std::vector<Parameter> params; |
| 5221 | auto maybe_add_param = [¶ms](size_t idx, const char* name) { |
| 5222 | if (idx != ast::intrinsic::TextureSignature::Parameters::kNotUsed) { |
| 5223 | params.emplace_back(Parameter{idx, name}); |
| 5224 | } |
| 5225 | }; |
| 5226 | maybe_add_param(sig->params.idx.array_index, "array_index"); |
| 5227 | maybe_add_param(sig->params.idx.bias, "bias"); |
| 5228 | maybe_add_param(sig->params.idx.coords, "coords"); |
| 5229 | maybe_add_param(sig->params.idx.depth_ref, "depth_ref"); |
| 5230 | maybe_add_param(sig->params.idx.ddx, "ddx"); |
| 5231 | maybe_add_param(sig->params.idx.ddy, "ddy"); |
| 5232 | maybe_add_param(sig->params.idx.level, "level"); |
| 5233 | maybe_add_param(sig->params.idx.offset, "offset"); |
| 5234 | maybe_add_param(sig->params.idx.sampler, "sampler"); |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5235 | maybe_add_param(sig->params.idx.sample_index, "sample_index"); |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5236 | maybe_add_param(sig->params.idx.texture, "texture"); |
Ben Clayton | 591268d | 2020-12-10 18:39:41 +0000 | [diff] [blame] | 5237 | maybe_add_param(sig->params.idx.value, "value"); |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5238 | std::sort( |
| 5239 | params.begin(), params.end(), |
| 5240 | [](const Parameter& a, const Parameter& b) { return a.idx < b.idx; }); |
| 5241 | |
| 5242 | std::stringstream out; |
| 5243 | out << function << "("; |
| 5244 | bool first = true; |
| 5245 | for (auto& param : params) { |
| 5246 | if (!first) { |
| 5247 | out << ", "; |
| 5248 | } |
| 5249 | out << param.name; |
| 5250 | first = false; |
| 5251 | } |
| 5252 | out << ")"; |
| 5253 | return out.str(); |
| 5254 | } |
| 5255 | |
| 5256 | const char* expected_texture_overload( |
| 5257 | ast::intrinsic::test::ValidTextureOverload overload) { |
| 5258 | using ValidTextureOverload = ast::intrinsic::test::ValidTextureOverload; |
| 5259 | switch (overload) { |
| 5260 | case ValidTextureOverload::kSample1dF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5261 | return R"(textureSample(texture, sampler, coords))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5262 | case ValidTextureOverload::kSample1dArrayF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5263 | return R"(textureSample(texture, sampler, coords, array_index))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5264 | case ValidTextureOverload::kSample2dF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5265 | return R"(textureSample(texture, sampler, coords))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5266 | case ValidTextureOverload::kSample2dOffsetF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5267 | return R"(textureSample(texture, sampler, coords, offset))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5268 | case ValidTextureOverload::kSample2dArrayF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5269 | return R"(textureSample(texture, sampler, coords, array_index))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5270 | case ValidTextureOverload::kSample2dArrayOffsetF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5271 | return R"(textureSample(texture, sampler, coords, array_index, offset))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5272 | case ValidTextureOverload::kSample3dF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5273 | return R"(textureSample(texture, sampler, coords))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5274 | case ValidTextureOverload::kSample3dOffsetF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5275 | return R"(textureSample(texture, sampler, coords, offset))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5276 | case ValidTextureOverload::kSampleCubeF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5277 | return R"(textureSample(texture, sampler, coords))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5278 | case ValidTextureOverload::kSampleCubeArrayF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5279 | return R"(textureSample(texture, sampler, coords, array_index))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5280 | case ValidTextureOverload::kSampleDepth2dF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5281 | return R"(textureSample(texture, sampler, coords))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5282 | case ValidTextureOverload::kSampleDepth2dOffsetF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5283 | return R"(textureSample(texture, sampler, coords, offset))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5284 | case ValidTextureOverload::kSampleDepth2dArrayF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5285 | return R"(textureSample(texture, sampler, coords, array_index))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5286 | case ValidTextureOverload::kSampleDepth2dArrayOffsetF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5287 | return R"(textureSample(texture, sampler, coords, array_index, offset))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5288 | case ValidTextureOverload::kSampleDepthCubeF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5289 | return R"(textureSample(texture, sampler, coords))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5290 | case ValidTextureOverload::kSampleDepthCubeArrayF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5291 | return R"(textureSample(texture, sampler, coords, array_index))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5292 | case ValidTextureOverload::kSampleBias2dF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5293 | return R"(textureSampleBias(texture, sampler, coords, bias))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5294 | case ValidTextureOverload::kSampleBias2dOffsetF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5295 | return R"(textureSampleBias(texture, sampler, coords, bias, offset))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5296 | case ValidTextureOverload::kSampleBias2dArrayF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5297 | return R"(textureSampleBias(texture, sampler, coords, array_index, bias))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5298 | case ValidTextureOverload::kSampleBias2dArrayOffsetF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5299 | return R"(textureSampleBias(texture, sampler, coords, array_index, bias, offset))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5300 | case ValidTextureOverload::kSampleBias3dF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5301 | return R"(textureSampleBias(texture, sampler, coords, bias))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5302 | case ValidTextureOverload::kSampleBias3dOffsetF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5303 | return R"(textureSampleBias(texture, sampler, coords, bias, offset))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5304 | case ValidTextureOverload::kSampleBiasCubeF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5305 | return R"(textureSampleBias(texture, sampler, coords, bias))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5306 | case ValidTextureOverload::kSampleBiasCubeArrayF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5307 | return R"(textureSampleBias(texture, sampler, coords, array_index, bias))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5308 | case ValidTextureOverload::kSampleLevel2dF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5309 | return R"(textureSampleLevel(texture, sampler, coords, level))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5310 | case ValidTextureOverload::kSampleLevel2dOffsetF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5311 | return R"(textureSampleLevel(texture, sampler, coords, level, offset))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5312 | case ValidTextureOverload::kSampleLevel2dArrayF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5313 | return R"(textureSampleLevel(texture, sampler, coords, array_index, level))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5314 | case ValidTextureOverload::kSampleLevel2dArrayOffsetF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5315 | return R"(textureSampleLevel(texture, sampler, coords, array_index, level, offset))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5316 | case ValidTextureOverload::kSampleLevel3dF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5317 | return R"(textureSampleLevel(texture, sampler, coords, level))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5318 | case ValidTextureOverload::kSampleLevel3dOffsetF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5319 | return R"(textureSampleLevel(texture, sampler, coords, level, offset))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5320 | case ValidTextureOverload::kSampleLevelCubeF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5321 | return R"(textureSampleLevel(texture, sampler, coords, level))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5322 | case ValidTextureOverload::kSampleLevelCubeArrayF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5323 | return R"(textureSampleLevel(texture, sampler, coords, array_index, level))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5324 | case ValidTextureOverload::kSampleLevelDepth2dF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5325 | return R"(textureSampleLevel(texture, sampler, coords, level))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5326 | case ValidTextureOverload::kSampleLevelDepth2dOffsetF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5327 | return R"(textureSampleLevel(texture, sampler, coords, level, offset))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5328 | case ValidTextureOverload::kSampleLevelDepth2dArrayF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5329 | return R"(textureSampleLevel(texture, sampler, coords, array_index, level))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5330 | case ValidTextureOverload::kSampleLevelDepth2dArrayOffsetF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5331 | return R"(textureSampleLevel(texture, sampler, coords, array_index, level, offset))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5332 | case ValidTextureOverload::kSampleLevelDepthCubeF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5333 | return R"(textureSampleLevel(texture, sampler, coords, level))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5334 | case ValidTextureOverload::kSampleLevelDepthCubeArrayF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5335 | return R"(textureSampleLevel(texture, sampler, coords, array_index, level))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5336 | case ValidTextureOverload::kSampleGrad2dF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5337 | return R"(textureSampleGrad(texture, sampler, coords, ddx, ddy))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5338 | case ValidTextureOverload::kSampleGrad2dOffsetF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5339 | return R"(textureSampleGrad(texture, sampler, coords, ddx, ddy, offset))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5340 | case ValidTextureOverload::kSampleGrad2dArrayF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5341 | return R"(textureSampleGrad(texture, sampler, coords, array_index, ddx, ddy))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5342 | case ValidTextureOverload::kSampleGrad2dArrayOffsetF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5343 | return R"(textureSampleGrad(texture, sampler, coords, array_index, ddx, ddy, offset))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5344 | case ValidTextureOverload::kSampleGrad3dF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5345 | return R"(textureSampleGrad(texture, sampler, coords, ddx, ddy))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5346 | case ValidTextureOverload::kSampleGrad3dOffsetF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5347 | return R"(textureSampleGrad(texture, sampler, coords, ddx, ddy, offset))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5348 | case ValidTextureOverload::kSampleGradCubeF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5349 | return R"(textureSampleGrad(texture, sampler, coords, ddx, ddy))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5350 | case ValidTextureOverload::kSampleGradCubeArrayF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5351 | return R"(textureSampleGrad(texture, sampler, coords, array_index, ddx, ddy))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5352 | case ValidTextureOverload::kSampleGradDepth2dF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5353 | return R"(textureSampleCompare(texture, sampler, coords, depth_ref))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5354 | case ValidTextureOverload::kSampleGradDepth2dOffsetF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5355 | return R"(textureSampleCompare(texture, sampler, coords, depth_ref, offset))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5356 | case ValidTextureOverload::kSampleGradDepth2dArrayF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5357 | return R"(textureSampleCompare(texture, sampler, coords, array_index, depth_ref))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5358 | case ValidTextureOverload::kSampleGradDepth2dArrayOffsetF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5359 | return R"(textureSampleCompare(texture, sampler, coords, array_index, depth_ref, offset))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5360 | case ValidTextureOverload::kSampleGradDepthCubeF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5361 | return R"(textureSampleCompare(texture, sampler, coords, depth_ref))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5362 | case ValidTextureOverload::kSampleGradDepthCubeArrayF32: |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5363 | return R"(textureSampleCompare(texture, sampler, coords, array_index, depth_ref))"; |
| 5364 | case ValidTextureOverload::kLoad1dF32: |
| 5365 | return R"(textureLoad(texture, coords))"; |
| 5366 | case ValidTextureOverload::kLoad1dU32: |
| 5367 | return R"(textureLoad(texture, coords))"; |
| 5368 | case ValidTextureOverload::kLoad1dI32: |
| 5369 | return R"(textureLoad(texture, coords))"; |
| 5370 | case ValidTextureOverload::kLoad1dArrayF32: |
| 5371 | return R"(textureLoad(texture, coords, array_index))"; |
| 5372 | case ValidTextureOverload::kLoad1dArrayU32: |
| 5373 | return R"(textureLoad(texture, coords, array_index))"; |
| 5374 | case ValidTextureOverload::kLoad1dArrayI32: |
| 5375 | return R"(textureLoad(texture, coords, array_index))"; |
| 5376 | case ValidTextureOverload::kLoad2dF32: |
| 5377 | return R"(textureLoad(texture, coords))"; |
| 5378 | case ValidTextureOverload::kLoad2dU32: |
| 5379 | return R"(textureLoad(texture, coords))"; |
| 5380 | case ValidTextureOverload::kLoad2dI32: |
| 5381 | return R"(textureLoad(texture, coords))"; |
| 5382 | case ValidTextureOverload::kLoad2dLevelF32: |
| 5383 | return R"(textureLoad(texture, coords, level))"; |
| 5384 | case ValidTextureOverload::kLoad2dLevelU32: |
| 5385 | return R"(textureLoad(texture, coords, level))"; |
| 5386 | case ValidTextureOverload::kLoad2dLevelI32: |
| 5387 | return R"(textureLoad(texture, coords, level))"; |
| 5388 | case ValidTextureOverload::kLoad2dArrayF32: |
| 5389 | return R"(textureLoad(texture, coords, array_index))"; |
| 5390 | case ValidTextureOverload::kLoad2dArrayU32: |
| 5391 | return R"(textureLoad(texture, coords, array_index))"; |
| 5392 | case ValidTextureOverload::kLoad2dArrayI32: |
| 5393 | return R"(textureLoad(texture, coords, array_index))"; |
| 5394 | case ValidTextureOverload::kLoad2dArrayLevelF32: |
| 5395 | return R"(textureLoad(texture, coords, array_index, level))"; |
| 5396 | case ValidTextureOverload::kLoad2dArrayLevelU32: |
| 5397 | return R"(textureLoad(texture, coords, array_index, level))"; |
| 5398 | case ValidTextureOverload::kLoad2dArrayLevelI32: |
| 5399 | return R"(textureLoad(texture, coords, array_index, level))"; |
| 5400 | case ValidTextureOverload::kLoad3dF32: |
| 5401 | return R"(textureLoad(texture, coords))"; |
| 5402 | case ValidTextureOverload::kLoad3dU32: |
| 5403 | return R"(textureLoad(texture, coords))"; |
| 5404 | case ValidTextureOverload::kLoad3dI32: |
| 5405 | return R"(textureLoad(texture, coords))"; |
| 5406 | case ValidTextureOverload::kLoad3dLevelF32: |
| 5407 | return R"(textureLoad(texture, coords, level))"; |
| 5408 | case ValidTextureOverload::kLoad3dLevelU32: |
| 5409 | return R"(textureLoad(texture, coords, level))"; |
| 5410 | case ValidTextureOverload::kLoad3dLevelI32: |
| 5411 | return R"(textureLoad(texture, coords, level))"; |
| 5412 | case ValidTextureOverload::kLoadMultisampled2dF32: |
| 5413 | return R"(textureLoad(texture, coords, sample_index))"; |
| 5414 | case ValidTextureOverload::kLoadMultisampled2dU32: |
| 5415 | return R"(textureLoad(texture, coords, sample_index))"; |
| 5416 | case ValidTextureOverload::kLoadMultisampled2dI32: |
| 5417 | return R"(textureLoad(texture, coords, sample_index))"; |
| 5418 | case ValidTextureOverload::kLoadMultisampled2dArrayF32: |
| 5419 | return R"(textureLoad(texture, coords, array_index, sample_index))"; |
| 5420 | case ValidTextureOverload::kLoadMultisampled2dArrayU32: |
| 5421 | return R"(textureLoad(texture, coords, array_index, sample_index))"; |
| 5422 | case ValidTextureOverload::kLoadMultisampled2dArrayI32: |
| 5423 | return R"(textureLoad(texture, coords, array_index, sample_index))"; |
| 5424 | case ValidTextureOverload::kLoadDepth2dF32: |
| 5425 | return R"(textureLoad(texture, coords))"; |
| 5426 | case ValidTextureOverload::kLoadDepth2dLevelF32: |
| 5427 | return R"(textureLoad(texture, coords, level))"; |
| 5428 | case ValidTextureOverload::kLoadDepth2dArrayF32: |
| 5429 | return R"(textureLoad(texture, coords, array_index))"; |
| 5430 | case ValidTextureOverload::kLoadDepth2dArrayLevelF32: |
| 5431 | return R"(textureLoad(texture, coords, array_index, level))"; |
| 5432 | case ValidTextureOverload::kLoadStorageRO1dRgba32float: |
| 5433 | return R"(textureLoad(texture, coords))"; |
| 5434 | case ValidTextureOverload::kLoadStorageRO1dArrayRgba32float: |
| 5435 | return R"(textureLoad(texture, coords, array_index))"; |
| 5436 | case ValidTextureOverload::kLoadStorageRO2dRgba8unorm: |
| 5437 | case ValidTextureOverload::kLoadStorageRO2dRgba8snorm: |
| 5438 | case ValidTextureOverload::kLoadStorageRO2dRgba8uint: |
| 5439 | case ValidTextureOverload::kLoadStorageRO2dRgba8sint: |
| 5440 | case ValidTextureOverload::kLoadStorageRO2dRgba16uint: |
| 5441 | case ValidTextureOverload::kLoadStorageRO2dRgba16sint: |
| 5442 | case ValidTextureOverload::kLoadStorageRO2dRgba16float: |
| 5443 | case ValidTextureOverload::kLoadStorageRO2dR32uint: |
| 5444 | case ValidTextureOverload::kLoadStorageRO2dR32sint: |
| 5445 | case ValidTextureOverload::kLoadStorageRO2dR32float: |
| 5446 | case ValidTextureOverload::kLoadStorageRO2dRg32uint: |
| 5447 | case ValidTextureOverload::kLoadStorageRO2dRg32sint: |
| 5448 | case ValidTextureOverload::kLoadStorageRO2dRg32float: |
| 5449 | case ValidTextureOverload::kLoadStorageRO2dRgba32uint: |
| 5450 | case ValidTextureOverload::kLoadStorageRO2dRgba32sint: |
| 5451 | case ValidTextureOverload::kLoadStorageRO2dRgba32float: |
| 5452 | return R"(textureLoad(texture, coords))"; |
| 5453 | case ValidTextureOverload::kLoadStorageRO2dArrayRgba32float: |
| 5454 | return R"(textureLoad(texture, coords, array_index))"; |
| 5455 | case ValidTextureOverload::kLoadStorageRO3dRgba32float: |
| 5456 | return R"(textureLoad(texture, coords))"; |
Ben Clayton | 591268d | 2020-12-10 18:39:41 +0000 | [diff] [blame] | 5457 | case ValidTextureOverload::kStoreWO1dRgba32float: |
| 5458 | return R"(textureStore(texture, coords, value))"; |
| 5459 | case ValidTextureOverload::kStoreWO1dArrayRgba32float: |
| 5460 | return R"(textureStore(texture, coords, array_index, value))"; |
| 5461 | case ValidTextureOverload::kStoreWO2dRgba32float: |
| 5462 | return R"(textureStore(texture, coords, value))"; |
| 5463 | case ValidTextureOverload::kStoreWO2dArrayRgba32float: |
| 5464 | return R"(textureStore(texture, coords, array_index, value))"; |
| 5465 | case ValidTextureOverload::kStoreWO3dRgba32float: |
| 5466 | return R"(textureStore(texture, coords, value))"; |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5467 | } |
| 5468 | return "<unmatched texture overload>"; |
| 5469 | } |
| 5470 | |
| 5471 | TEST_P(TypeDeterminerTextureIntrinsicTest, Call) { |
| 5472 | auto param = GetParam(); |
| 5473 | |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5474 | param.buildTextureVariable(this); |
| 5475 | param.buildSamplerVariable(this); |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5476 | |
| 5477 | auto* ident = Expr(param.function); |
| 5478 | ast::CallExpression call{ident, param.args(this)}; |
| 5479 | |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5480 | ASSERT_TRUE(td()->Determine()) << td()->error(); |
| 5481 | ASSERT_TRUE(td()->DetermineResultType(&call)) << td()->error(); |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5482 | |
Ben Clayton | 591268d | 2020-12-10 18:39:41 +0000 | [diff] [blame] | 5483 | if (std::string(param.function) == "textureStore") { |
| 5484 | EXPECT_EQ(call.result_type(), ty.void_); |
| 5485 | } else { |
| 5486 | switch (param.texture_kind) { |
| 5487 | case ast::intrinsic::test::TextureKind::kRegular: |
| 5488 | case ast::intrinsic::test::TextureKind::kMultisampled: |
| 5489 | case ast::intrinsic::test::TextureKind::kStorage: { |
| 5490 | auto* datatype = param.resultVectorComponentType(this); |
| 5491 | ASSERT_TRUE(call.result_type()->Is<ast::type::Vector>()); |
| 5492 | EXPECT_EQ(call.result_type()->As<ast::type::Vector>()->type(), |
| 5493 | datatype); |
| 5494 | break; |
| 5495 | } |
| 5496 | case ast::intrinsic::test::TextureKind::kDepth: { |
| 5497 | EXPECT_EQ(call.result_type(), ty.f32); |
| 5498 | break; |
| 5499 | } |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5500 | } |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5501 | } |
| 5502 | |
| 5503 | auto* sig = static_cast<const ast::intrinsic::TextureSignature*>( |
| 5504 | ident->intrinsic_signature()); |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5505 | ASSERT_NE(sig, nullptr); |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5506 | |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5507 | auto got = to_str(param.function, sig); |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5508 | auto* expected = expected_texture_overload(param.overload); |
Ben Clayton | 7f04e5c | 2020-12-09 15:57:00 +0000 | [diff] [blame] | 5509 | EXPECT_EQ(got, expected); |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 5510 | } |
| 5511 | |
dan sinclair | b7edc4c | 2020-04-07 12:46:30 +0000 | [diff] [blame] | 5512 | } // namespace |
| 5513 | } // namespace tint |