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 | |
| 17 | #include <memory> |
| 18 | #include <utility> |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 19 | #include <vector> |
dan sinclair | b7edc4c | 2020-04-07 12:46:30 +0000 | [diff] [blame] | 20 | |
| 21 | #include "gtest/gtest.h" |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 22 | #include "spirv/unified1/GLSL.std.450.h" |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 23 | #include "src/ast/array_accessor_expression.h" |
dan sinclair | a01777c | 2020-04-07 12:57:52 +0000 | [diff] [blame] | 24 | #include "src/ast/as_expression.h" |
dan sinclair | 6c498fc | 2020-04-07 12:47:23 +0000 | [diff] [blame] | 25 | #include "src/ast/assignment_statement.h" |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 26 | #include "src/ast/binary_expression.h" |
dan sinclair | b7ea6e2 | 2020-04-07 12:54:10 +0000 | [diff] [blame] | 27 | #include "src/ast/break_statement.h" |
dan sinclair | 3ca8746 | 2020-04-07 16:41:10 +0000 | [diff] [blame] | 28 | #include "src/ast/call_expression.h" |
dan sinclair | 6010b29 | 2020-04-07 12:54:20 +0000 | [diff] [blame] | 29 | #include "src/ast/case_statement.h" |
dan sinclair | 4e80795 | 2020-04-07 16:41:23 +0000 | [diff] [blame] | 30 | #include "src/ast/cast_expression.h" |
dan sinclair | aec965e | 2020-04-07 12:54:29 +0000 | [diff] [blame] | 31 | #include "src/ast/continue_statement.h" |
dan sinclair | 0cf685f | 2020-04-07 12:54:37 +0000 | [diff] [blame] | 32 | #include "src/ast/else_statement.h" |
dan sinclair | b7edc4c | 2020-04-07 12:46:30 +0000 | [diff] [blame] | 33 | #include "src/ast/float_literal.h" |
dan sinclair | cab0e73 | 2020-04-07 12:57:27 +0000 | [diff] [blame] | 34 | #include "src/ast/identifier_expression.h" |
dan sinclair | 91c44a5 | 2020-04-07 12:55:25 +0000 | [diff] [blame] | 35 | #include "src/ast/if_statement.h" |
dan sinclair | b7edc4c | 2020-04-07 12:46:30 +0000 | [diff] [blame] | 36 | #include "src/ast/int_literal.h" |
dan sinclair | bc71eda | 2020-04-07 12:55:51 +0000 | [diff] [blame] | 37 | #include "src/ast/loop_statement.h" |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 38 | #include "src/ast/member_accessor_expression.h" |
dan sinclair | bf0fff8 | 2020-04-07 12:56:24 +0000 | [diff] [blame] | 39 | #include "src/ast/return_statement.h" |
dan sinclair | b7edc4c | 2020-04-07 12:46:30 +0000 | [diff] [blame] | 40 | #include "src/ast/scalar_constructor_expression.h" |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 41 | #include "src/ast/struct.h" |
| 42 | #include "src/ast/struct_member.h" |
dan sinclair | 18b3285 | 2020-04-07 12:56:45 +0000 | [diff] [blame] | 43 | #include "src/ast/switch_statement.h" |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 44 | #include "src/ast/type/array_type.h" |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 45 | #include "src/ast/type/bool_type.h" |
dan sinclair | b7edc4c | 2020-04-07 12:46:30 +0000 | [diff] [blame] | 46 | #include "src/ast/type/f32_type.h" |
dan sinclair | 6c498fc | 2020-04-07 12:47:23 +0000 | [diff] [blame] | 47 | #include "src/ast/type/i32_type.h" |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 48 | #include "src/ast/type/matrix_type.h" |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 49 | #include "src/ast/type/struct_type.h" |
dan sinclair | b7edc4c | 2020-04-07 12:46:30 +0000 | [diff] [blame] | 50 | #include "src/ast/type/vector_type.h" |
| 51 | #include "src/ast/type_constructor_expression.h" |
dan sinclair | b173056 | 2020-04-07 19:26:49 +0000 | [diff] [blame] | 52 | #include "src/ast/unary_derivative_expression.h" |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 53 | #include "src/ast/unary_method_expression.h" |
dan sinclair | 327ed1b | 2020-04-07 19:27:21 +0000 | [diff] [blame] | 54 | #include "src/ast/unary_op_expression.h" |
dan sinclair | 9a84e5e | 2020-04-07 12:56:57 +0000 | [diff] [blame] | 55 | #include "src/ast/unless_statement.h" |
dan sinclair | ca893e3 | 2020-04-07 12:57:12 +0000 | [diff] [blame] | 56 | #include "src/ast/variable_decl_statement.h" |
dan sinclair | b7edc4c | 2020-04-07 12:46:30 +0000 | [diff] [blame] | 57 | |
| 58 | namespace tint { |
| 59 | namespace { |
| 60 | |
dan sinclair | 7456f42 | 2020-04-08 19:58:35 +0000 | [diff] [blame] | 61 | class FakeStmt : public ast::Statement { |
| 62 | public: |
| 63 | bool IsValid() const override { return true; } |
| 64 | void to_str(std::ostream&, size_t) const override {} |
| 65 | }; |
| 66 | |
| 67 | class FakeExpr : public ast::Expression { |
| 68 | public: |
| 69 | bool IsValid() const override { return true; } |
| 70 | void to_str(std::ostream&, size_t) const override {} |
| 71 | }; |
| 72 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 73 | class TypeDeterminerHelper { |
dan sinclair | b7edc4c | 2020-04-07 12:46:30 +0000 | [diff] [blame] | 74 | public: |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 75 | TypeDeterminerHelper() |
| 76 | : td_(std::make_unique<TypeDeterminer>(&ctx_, &mod_)) {} |
dan sinclair | b7edc4c | 2020-04-07 12:46:30 +0000 | [diff] [blame] | 77 | |
| 78 | TypeDeterminer* td() const { return td_.get(); } |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 79 | ast::Module* mod() { return &mod_; } |
dan sinclair | b7edc4c | 2020-04-07 12:46:30 +0000 | [diff] [blame] | 80 | |
| 81 | private: |
| 82 | Context ctx_; |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 83 | ast::Module mod_; |
dan sinclair | b7edc4c | 2020-04-07 12:46:30 +0000 | [diff] [blame] | 84 | std::unique_ptr<TypeDeterminer> td_; |
| 85 | }; |
| 86 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 87 | class TypeDeterminerTest : public TypeDeterminerHelper, public testing::Test {}; |
| 88 | |
| 89 | template <typename T> |
| 90 | class TypeDeterminerTestWithParam : public TypeDeterminerHelper, |
| 91 | public testing::TestWithParam<T> {}; |
| 92 | |
dan sinclair | 7456f42 | 2020-04-08 19:58:35 +0000 | [diff] [blame] | 93 | TEST_F(TypeDeterminerTest, Error_WithEmptySource) { |
| 94 | FakeStmt s; |
| 95 | s.set_source(Source{0, 0}); |
| 96 | |
| 97 | EXPECT_FALSE(td()->DetermineResultType(&s)); |
| 98 | EXPECT_EQ(td()->error(), "unknown statement type for type determination"); |
| 99 | } |
| 100 | |
| 101 | TEST_F(TypeDeterminerTest, Stmt_Error_Unknown) { |
| 102 | FakeStmt s; |
| 103 | s.set_source(Source{2, 30}); |
| 104 | |
| 105 | EXPECT_FALSE(td()->DetermineResultType(&s)); |
| 106 | EXPECT_EQ(td()->error(), |
| 107 | "2:30: unknown statement type for type determination"); |
| 108 | } |
| 109 | |
dan sinclair | 6c498fc | 2020-04-07 12:47:23 +0000 | [diff] [blame] | 110 | TEST_F(TypeDeterminerTest, Stmt_Assign) { |
| 111 | ast::type::F32Type f32; |
| 112 | ast::type::I32Type i32; |
| 113 | |
| 114 | auto lhs = std::make_unique<ast::ScalarConstructorExpression>( |
| 115 | std::make_unique<ast::IntLiteral>(&i32, 2)); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 116 | auto* lhs_ptr = lhs.get(); |
dan sinclair | 6c498fc | 2020-04-07 12:47:23 +0000 | [diff] [blame] | 117 | |
| 118 | auto rhs = std::make_unique<ast::ScalarConstructorExpression>( |
| 119 | std::make_unique<ast::FloatLiteral>(&f32, 2.3f)); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 120 | auto* rhs_ptr = rhs.get(); |
dan sinclair | 6c498fc | 2020-04-07 12:47:23 +0000 | [diff] [blame] | 121 | |
| 122 | ast::AssignmentStatement assign(std::move(lhs), std::move(rhs)); |
| 123 | |
| 124 | EXPECT_TRUE(td()->DetermineResultType(&assign)); |
| 125 | ASSERT_NE(lhs_ptr->result_type(), nullptr); |
| 126 | ASSERT_NE(rhs_ptr->result_type(), nullptr); |
| 127 | |
| 128 | EXPECT_TRUE(lhs_ptr->result_type()->IsI32()); |
| 129 | EXPECT_TRUE(rhs_ptr->result_type()->IsF32()); |
| 130 | } |
| 131 | |
dan sinclair | b7ea6e2 | 2020-04-07 12:54:10 +0000 | [diff] [blame] | 132 | TEST_F(TypeDeterminerTest, Stmt_Break) { |
| 133 | ast::type::I32Type i32; |
| 134 | |
| 135 | auto cond = std::make_unique<ast::ScalarConstructorExpression>( |
| 136 | std::make_unique<ast::IntLiteral>(&i32, 2)); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 137 | auto* cond_ptr = cond.get(); |
dan sinclair | b7ea6e2 | 2020-04-07 12:54:10 +0000 | [diff] [blame] | 138 | |
| 139 | ast::BreakStatement brk(ast::StatementCondition::kIf, std::move(cond)); |
| 140 | |
| 141 | EXPECT_TRUE(td()->DetermineResultType(&brk)); |
| 142 | ASSERT_NE(cond_ptr->result_type(), nullptr); |
| 143 | EXPECT_TRUE(cond_ptr->result_type()->IsI32()); |
| 144 | } |
| 145 | |
dan sinclair | 327ed1b | 2020-04-07 19:27:21 +0000 | [diff] [blame] | 146 | TEST_F(TypeDeterminerTest, Stmt_Break_WithoutCondition) { |
| 147 | ast::type::I32Type i32; |
| 148 | ast::BreakStatement brk; |
| 149 | EXPECT_TRUE(td()->DetermineResultType(&brk)); |
| 150 | } |
| 151 | |
dan sinclair | 6010b29 | 2020-04-07 12:54:20 +0000 | [diff] [blame] | 152 | TEST_F(TypeDeterminerTest, Stmt_Case) { |
| 153 | ast::type::I32Type i32; |
| 154 | ast::type::F32Type f32; |
| 155 | |
| 156 | auto lhs = std::make_unique<ast::ScalarConstructorExpression>( |
| 157 | std::make_unique<ast::IntLiteral>(&i32, 2)); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 158 | auto* lhs_ptr = lhs.get(); |
dan sinclair | 6010b29 | 2020-04-07 12:54:20 +0000 | [diff] [blame] | 159 | |
| 160 | auto rhs = std::make_unique<ast::ScalarConstructorExpression>( |
| 161 | std::make_unique<ast::FloatLiteral>(&f32, 2.3f)); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 162 | auto* rhs_ptr = rhs.get(); |
dan sinclair | 6010b29 | 2020-04-07 12:54:20 +0000 | [diff] [blame] | 163 | |
| 164 | ast::StatementList body; |
| 165 | body.push_back(std::make_unique<ast::AssignmentStatement>(std::move(lhs), |
| 166 | std::move(rhs))); |
| 167 | |
| 168 | ast::CaseStatement cse(std::make_unique<ast::IntLiteral>(&i32, 3), |
| 169 | std::move(body)); |
| 170 | |
| 171 | EXPECT_TRUE(td()->DetermineResultType(&cse)); |
| 172 | ASSERT_NE(lhs_ptr->result_type(), nullptr); |
| 173 | ASSERT_NE(rhs_ptr->result_type(), nullptr); |
| 174 | EXPECT_TRUE(lhs_ptr->result_type()->IsI32()); |
| 175 | EXPECT_TRUE(rhs_ptr->result_type()->IsF32()); |
| 176 | } |
| 177 | |
dan sinclair | aec965e | 2020-04-07 12:54:29 +0000 | [diff] [blame] | 178 | TEST_F(TypeDeterminerTest, Stmt_Continue) { |
| 179 | ast::type::I32Type i32; |
| 180 | |
| 181 | auto cond = std::make_unique<ast::ScalarConstructorExpression>( |
| 182 | std::make_unique<ast::IntLiteral>(&i32, 2)); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 183 | auto* cond_ptr = cond.get(); |
dan sinclair | aec965e | 2020-04-07 12:54:29 +0000 | [diff] [blame] | 184 | |
| 185 | ast::ContinueStatement stmt(ast::StatementCondition::kIf, std::move(cond)); |
| 186 | |
| 187 | EXPECT_TRUE(td()->DetermineResultType(&stmt)); |
| 188 | ASSERT_NE(cond_ptr->result_type(), nullptr); |
| 189 | EXPECT_TRUE(cond_ptr->result_type()->IsI32()); |
| 190 | } |
| 191 | |
dan sinclair | 327ed1b | 2020-04-07 19:27:21 +0000 | [diff] [blame] | 192 | TEST_F(TypeDeterminerTest, Stmt_Continue_WithoutStatement) { |
| 193 | ast::type::I32Type i32; |
| 194 | ast::ContinueStatement stmt; |
| 195 | EXPECT_TRUE(td()->DetermineResultType(&stmt)); |
| 196 | } |
| 197 | |
dan sinclair | 0cf685f | 2020-04-07 12:54:37 +0000 | [diff] [blame] | 198 | TEST_F(TypeDeterminerTest, Stmt_Else) { |
| 199 | ast::type::I32Type i32; |
| 200 | ast::type::F32Type f32; |
| 201 | |
| 202 | auto lhs = std::make_unique<ast::ScalarConstructorExpression>( |
| 203 | std::make_unique<ast::IntLiteral>(&i32, 2)); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 204 | auto* lhs_ptr = lhs.get(); |
dan sinclair | 0cf685f | 2020-04-07 12:54:37 +0000 | [diff] [blame] | 205 | |
| 206 | auto rhs = std::make_unique<ast::ScalarConstructorExpression>( |
| 207 | std::make_unique<ast::FloatLiteral>(&f32, 2.3f)); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 208 | auto* rhs_ptr = rhs.get(); |
dan sinclair | 0cf685f | 2020-04-07 12:54:37 +0000 | [diff] [blame] | 209 | |
| 210 | ast::StatementList body; |
| 211 | body.push_back(std::make_unique<ast::AssignmentStatement>(std::move(lhs), |
| 212 | std::move(rhs))); |
| 213 | |
| 214 | ast::ElseStatement stmt(std::make_unique<ast::ScalarConstructorExpression>( |
| 215 | std::make_unique<ast::IntLiteral>(&i32, 3)), |
| 216 | std::move(body)); |
| 217 | |
| 218 | EXPECT_TRUE(td()->DetermineResultType(&stmt)); |
| 219 | ASSERT_NE(stmt.condition()->result_type(), nullptr); |
| 220 | ASSERT_NE(lhs_ptr->result_type(), nullptr); |
| 221 | ASSERT_NE(rhs_ptr->result_type(), nullptr); |
| 222 | EXPECT_TRUE(stmt.condition()->result_type()->IsI32()); |
| 223 | EXPECT_TRUE(lhs_ptr->result_type()->IsI32()); |
| 224 | EXPECT_TRUE(rhs_ptr->result_type()->IsF32()); |
| 225 | } |
| 226 | |
dan sinclair | 91c44a5 | 2020-04-07 12:55:25 +0000 | [diff] [blame] | 227 | TEST_F(TypeDeterminerTest, Stmt_If) { |
| 228 | ast::type::I32Type i32; |
| 229 | ast::type::F32Type f32; |
| 230 | |
| 231 | auto else_lhs = std::make_unique<ast::ScalarConstructorExpression>( |
| 232 | std::make_unique<ast::IntLiteral>(&i32, 2)); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 233 | auto* else_lhs_ptr = else_lhs.get(); |
dan sinclair | 91c44a5 | 2020-04-07 12:55:25 +0000 | [diff] [blame] | 234 | |
| 235 | auto else_rhs = std::make_unique<ast::ScalarConstructorExpression>( |
| 236 | std::make_unique<ast::FloatLiteral>(&f32, 2.3f)); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 237 | auto* else_rhs_ptr = else_rhs.get(); |
dan sinclair | 91c44a5 | 2020-04-07 12:55:25 +0000 | [diff] [blame] | 238 | |
| 239 | ast::StatementList else_body; |
| 240 | else_body.push_back(std::make_unique<ast::AssignmentStatement>( |
| 241 | std::move(else_lhs), std::move(else_rhs))); |
| 242 | |
| 243 | auto else_stmt = std::make_unique<ast::ElseStatement>( |
| 244 | std::make_unique<ast::ScalarConstructorExpression>( |
| 245 | std::make_unique<ast::IntLiteral>(&i32, 3)), |
| 246 | std::move(else_body)); |
| 247 | |
| 248 | ast::ElseStatementList else_stmts; |
| 249 | else_stmts.push_back(std::move(else_stmt)); |
| 250 | |
| 251 | auto lhs = std::make_unique<ast::ScalarConstructorExpression>( |
| 252 | std::make_unique<ast::IntLiteral>(&i32, 2)); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 253 | auto* lhs_ptr = lhs.get(); |
dan sinclair | 91c44a5 | 2020-04-07 12:55:25 +0000 | [diff] [blame] | 254 | |
| 255 | auto rhs = std::make_unique<ast::ScalarConstructorExpression>( |
| 256 | std::make_unique<ast::FloatLiteral>(&f32, 2.3f)); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 257 | auto* rhs_ptr = rhs.get(); |
dan sinclair | 91c44a5 | 2020-04-07 12:55:25 +0000 | [diff] [blame] | 258 | |
| 259 | ast::StatementList body; |
| 260 | body.push_back(std::make_unique<ast::AssignmentStatement>(std::move(lhs), |
| 261 | std::move(rhs))); |
| 262 | |
| 263 | ast::IfStatement stmt(std::make_unique<ast::ScalarConstructorExpression>( |
| 264 | std::make_unique<ast::IntLiteral>(&i32, 3)), |
| 265 | std::move(body)); |
| 266 | stmt.set_else_statements(std::move(else_stmts)); |
| 267 | |
| 268 | EXPECT_TRUE(td()->DetermineResultType(&stmt)); |
| 269 | ASSERT_NE(stmt.condition()->result_type(), nullptr); |
| 270 | ASSERT_NE(else_lhs_ptr->result_type(), nullptr); |
| 271 | ASSERT_NE(else_rhs_ptr->result_type(), nullptr); |
| 272 | ASSERT_NE(lhs_ptr->result_type(), nullptr); |
| 273 | ASSERT_NE(rhs_ptr->result_type(), nullptr); |
| 274 | EXPECT_TRUE(stmt.condition()->result_type()->IsI32()); |
| 275 | EXPECT_TRUE(else_lhs_ptr->result_type()->IsI32()); |
| 276 | EXPECT_TRUE(else_rhs_ptr->result_type()->IsF32()); |
| 277 | EXPECT_TRUE(lhs_ptr->result_type()->IsI32()); |
| 278 | EXPECT_TRUE(rhs_ptr->result_type()->IsF32()); |
| 279 | } |
| 280 | |
dan sinclair | bc71eda | 2020-04-07 12:55:51 +0000 | [diff] [blame] | 281 | TEST_F(TypeDeterminerTest, Stmt_Loop) { |
| 282 | ast::type::I32Type i32; |
| 283 | ast::type::F32Type f32; |
| 284 | |
| 285 | auto body_lhs = std::make_unique<ast::ScalarConstructorExpression>( |
| 286 | std::make_unique<ast::IntLiteral>(&i32, 2)); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 287 | auto* body_lhs_ptr = body_lhs.get(); |
dan sinclair | bc71eda | 2020-04-07 12:55:51 +0000 | [diff] [blame] | 288 | |
| 289 | auto body_rhs = std::make_unique<ast::ScalarConstructorExpression>( |
| 290 | std::make_unique<ast::FloatLiteral>(&f32, 2.3f)); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 291 | auto* body_rhs_ptr = body_rhs.get(); |
dan sinclair | bc71eda | 2020-04-07 12:55:51 +0000 | [diff] [blame] | 292 | |
| 293 | ast::StatementList body; |
| 294 | body.push_back(std::make_unique<ast::AssignmentStatement>( |
| 295 | std::move(body_lhs), std::move(body_rhs))); |
| 296 | |
| 297 | auto continuing_lhs = std::make_unique<ast::ScalarConstructorExpression>( |
| 298 | std::make_unique<ast::IntLiteral>(&i32, 2)); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 299 | auto* continuing_lhs_ptr = continuing_lhs.get(); |
dan sinclair | bc71eda | 2020-04-07 12:55:51 +0000 | [diff] [blame] | 300 | |
| 301 | auto continuing_rhs = std::make_unique<ast::ScalarConstructorExpression>( |
| 302 | std::make_unique<ast::FloatLiteral>(&f32, 2.3f)); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 303 | auto* continuing_rhs_ptr = continuing_rhs.get(); |
dan sinclair | bc71eda | 2020-04-07 12:55:51 +0000 | [diff] [blame] | 304 | |
| 305 | ast::StatementList continuing; |
| 306 | continuing.push_back(std::make_unique<ast::AssignmentStatement>( |
| 307 | std::move(continuing_lhs), std::move(continuing_rhs))); |
| 308 | |
| 309 | ast::LoopStatement stmt(std::move(body), std::move(continuing)); |
| 310 | |
| 311 | EXPECT_TRUE(td()->DetermineResultType(&stmt)); |
| 312 | ASSERT_NE(body_lhs_ptr->result_type(), nullptr); |
| 313 | ASSERT_NE(body_rhs_ptr->result_type(), nullptr); |
| 314 | ASSERT_NE(continuing_lhs_ptr->result_type(), nullptr); |
| 315 | ASSERT_NE(continuing_rhs_ptr->result_type(), nullptr); |
| 316 | EXPECT_TRUE(body_lhs_ptr->result_type()->IsI32()); |
| 317 | EXPECT_TRUE(body_rhs_ptr->result_type()->IsF32()); |
| 318 | EXPECT_TRUE(continuing_lhs_ptr->result_type()->IsI32()); |
| 319 | EXPECT_TRUE(continuing_rhs_ptr->result_type()->IsF32()); |
| 320 | } |
| 321 | |
dan sinclair | bf0fff8 | 2020-04-07 12:56:24 +0000 | [diff] [blame] | 322 | TEST_F(TypeDeterminerTest, Stmt_Return) { |
| 323 | ast::type::I32Type i32; |
| 324 | |
| 325 | auto cond = std::make_unique<ast::ScalarConstructorExpression>( |
| 326 | std::make_unique<ast::IntLiteral>(&i32, 2)); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 327 | auto* cond_ptr = cond.get(); |
dan sinclair | bf0fff8 | 2020-04-07 12:56:24 +0000 | [diff] [blame] | 328 | |
| 329 | ast::ReturnStatement ret(std::move(cond)); |
| 330 | |
| 331 | EXPECT_TRUE(td()->DetermineResultType(&ret)); |
| 332 | ASSERT_NE(cond_ptr->result_type(), nullptr); |
| 333 | EXPECT_TRUE(cond_ptr->result_type()->IsI32()); |
| 334 | } |
| 335 | |
dan sinclair | 327ed1b | 2020-04-07 19:27:21 +0000 | [diff] [blame] | 336 | TEST_F(TypeDeterminerTest, Stmt_Return_WithoutValue) { |
| 337 | ast::type::I32Type i32; |
| 338 | ast::ReturnStatement ret; |
| 339 | EXPECT_TRUE(td()->DetermineResultType(&ret)); |
| 340 | } |
| 341 | |
dan sinclair | 18b3285 | 2020-04-07 12:56:45 +0000 | [diff] [blame] | 342 | TEST_F(TypeDeterminerTest, Stmt_Switch) { |
| 343 | ast::type::I32Type i32; |
| 344 | ast::type::F32Type f32; |
| 345 | |
| 346 | auto lhs = std::make_unique<ast::ScalarConstructorExpression>( |
| 347 | std::make_unique<ast::IntLiteral>(&i32, 2)); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 348 | auto* lhs_ptr = lhs.get(); |
dan sinclair | 18b3285 | 2020-04-07 12:56:45 +0000 | [diff] [blame] | 349 | |
| 350 | auto rhs = std::make_unique<ast::ScalarConstructorExpression>( |
| 351 | std::make_unique<ast::FloatLiteral>(&f32, 2.3f)); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 352 | auto* rhs_ptr = rhs.get(); |
dan sinclair | 18b3285 | 2020-04-07 12:56:45 +0000 | [diff] [blame] | 353 | |
| 354 | ast::StatementList body; |
| 355 | body.push_back(std::make_unique<ast::AssignmentStatement>(std::move(lhs), |
| 356 | std::move(rhs))); |
| 357 | |
| 358 | ast::CaseStatementList cases; |
| 359 | cases.push_back(std::make_unique<ast::CaseStatement>( |
| 360 | std::make_unique<ast::IntLiteral>(&i32, 3), std::move(body))); |
| 361 | |
| 362 | ast::SwitchStatement stmt(std::make_unique<ast::ScalarConstructorExpression>( |
| 363 | std::make_unique<ast::IntLiteral>(&i32, 2)), |
| 364 | std::move(cases)); |
| 365 | |
| 366 | EXPECT_TRUE(td()->DetermineResultType(&stmt)) << td()->error(); |
| 367 | ASSERT_NE(stmt.condition()->result_type(), nullptr); |
| 368 | ASSERT_NE(lhs_ptr->result_type(), nullptr); |
| 369 | ASSERT_NE(rhs_ptr->result_type(), nullptr); |
| 370 | |
| 371 | EXPECT_TRUE(stmt.condition()->result_type()->IsI32()); |
| 372 | EXPECT_TRUE(lhs_ptr->result_type()->IsI32()); |
| 373 | EXPECT_TRUE(rhs_ptr->result_type()->IsF32()); |
| 374 | } |
| 375 | |
dan sinclair | 9a84e5e | 2020-04-07 12:56:57 +0000 | [diff] [blame] | 376 | TEST_F(TypeDeterminerTest, Stmt_Unless) { |
| 377 | ast::type::I32Type i32; |
| 378 | ast::type::F32Type f32; |
| 379 | |
| 380 | auto lhs = std::make_unique<ast::ScalarConstructorExpression>( |
| 381 | std::make_unique<ast::IntLiteral>(&i32, 2)); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 382 | auto* lhs_ptr = lhs.get(); |
dan sinclair | 9a84e5e | 2020-04-07 12:56:57 +0000 | [diff] [blame] | 383 | |
| 384 | auto rhs = std::make_unique<ast::ScalarConstructorExpression>( |
| 385 | std::make_unique<ast::FloatLiteral>(&f32, 2.3f)); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 386 | auto* rhs_ptr = rhs.get(); |
dan sinclair | 9a84e5e | 2020-04-07 12:56:57 +0000 | [diff] [blame] | 387 | |
| 388 | ast::StatementList body; |
| 389 | body.push_back(std::make_unique<ast::AssignmentStatement>(std::move(lhs), |
| 390 | std::move(rhs))); |
| 391 | |
| 392 | ast::UnlessStatement unless( |
| 393 | std::make_unique<ast::ScalarConstructorExpression>( |
| 394 | std::make_unique<ast::IntLiteral>(&i32, 3)), |
| 395 | std::move(body)); |
| 396 | |
| 397 | EXPECT_TRUE(td()->DetermineResultType(&unless)); |
| 398 | ASSERT_NE(unless.condition()->result_type(), nullptr); |
| 399 | ASSERT_NE(lhs_ptr->result_type(), nullptr); |
| 400 | ASSERT_NE(rhs_ptr->result_type(), nullptr); |
| 401 | EXPECT_TRUE(unless.condition()->result_type()->IsI32()); |
| 402 | EXPECT_TRUE(lhs_ptr->result_type()->IsI32()); |
| 403 | EXPECT_TRUE(rhs_ptr->result_type()->IsF32()); |
| 404 | } |
| 405 | |
dan sinclair | ca893e3 | 2020-04-07 12:57:12 +0000 | [diff] [blame] | 406 | TEST_F(TypeDeterminerTest, Stmt_VariableDecl) { |
| 407 | ast::type::I32Type i32; |
| 408 | auto var = |
| 409 | std::make_unique<ast::Variable>("my_var", ast::StorageClass::kNone, &i32); |
| 410 | var->set_constructor(std::make_unique<ast::ScalarConstructorExpression>( |
| 411 | std::make_unique<ast::IntLiteral>(&i32, 2))); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 412 | auto* init_ptr = var->constructor(); |
dan sinclair | ca893e3 | 2020-04-07 12:57:12 +0000 | [diff] [blame] | 413 | |
| 414 | ast::VariableDeclStatement decl(std::move(var)); |
| 415 | |
| 416 | EXPECT_TRUE(td()->DetermineResultType(&decl)); |
| 417 | ASSERT_NE(init_ptr->result_type(), nullptr); |
| 418 | EXPECT_TRUE(init_ptr->result_type()->IsI32()); |
| 419 | } |
| 420 | |
dan sinclair | 7456f42 | 2020-04-08 19:58:35 +0000 | [diff] [blame] | 421 | TEST_F(TypeDeterminerTest, Expr_Error_Unknown) { |
| 422 | FakeExpr e; |
| 423 | e.set_source(Source{2, 30}); |
| 424 | |
| 425 | EXPECT_FALSE(td()->DetermineResultType(&e)); |
| 426 | EXPECT_EQ(td()->error(), "2:30: unknown expression for type determination"); |
| 427 | } |
| 428 | |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 429 | TEST_F(TypeDeterminerTest, Expr_ArrayAccessor_Array) { |
| 430 | ast::type::I32Type i32; |
| 431 | ast::type::F32Type f32; |
| 432 | ast::type::ArrayType ary(&f32, 3); |
| 433 | |
| 434 | auto idx = std::make_unique<ast::ScalarConstructorExpression>( |
| 435 | std::make_unique<ast::IntLiteral>(&i32, 2)); |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 436 | auto var = |
| 437 | std::make_unique<ast::Variable>("my_var", ast::StorageClass::kNone, &ary); |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 438 | mod()->AddGlobalVariable(std::move(var)); |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 439 | |
| 440 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 441 | EXPECT_TRUE(td()->Determine()); |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 442 | |
| 443 | ast::ArrayAccessorExpression acc( |
| 444 | std::make_unique<ast::IdentifierExpression>("my_var"), std::move(idx)); |
| 445 | EXPECT_TRUE(td()->DetermineResultType(&acc)); |
| 446 | ASSERT_NE(acc.result_type(), nullptr); |
| 447 | EXPECT_TRUE(acc.result_type()->IsF32()); |
| 448 | } |
| 449 | |
| 450 | TEST_F(TypeDeterminerTest, Expr_ArrayAccessor_Matrix) { |
| 451 | ast::type::I32Type i32; |
| 452 | ast::type::F32Type f32; |
| 453 | ast::type::MatrixType mat(&f32, 3, 2); |
| 454 | |
| 455 | auto idx = std::make_unique<ast::ScalarConstructorExpression>( |
| 456 | std::make_unique<ast::IntLiteral>(&i32, 2)); |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 457 | auto var = |
| 458 | std::make_unique<ast::Variable>("my_var", ast::StorageClass::kNone, &mat); |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 459 | mod()->AddGlobalVariable(std::move(var)); |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 460 | |
| 461 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 462 | EXPECT_TRUE(td()->Determine()); |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 463 | |
| 464 | ast::ArrayAccessorExpression acc( |
| 465 | std::make_unique<ast::IdentifierExpression>("my_var"), std::move(idx)); |
| 466 | EXPECT_TRUE(td()->DetermineResultType(&acc)); |
| 467 | ASSERT_NE(acc.result_type(), nullptr); |
| 468 | ASSERT_TRUE(acc.result_type()->IsVector()); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 469 | EXPECT_EQ(acc.result_type()->AsVector()->size(), 3u); |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | TEST_F(TypeDeterminerTest, Expr_ArrayAccessor_Matrix_BothDimensions) { |
| 473 | ast::type::I32Type i32; |
| 474 | ast::type::F32Type f32; |
| 475 | ast::type::MatrixType mat(&f32, 3, 2); |
| 476 | |
| 477 | auto idx1 = std::make_unique<ast::ScalarConstructorExpression>( |
| 478 | std::make_unique<ast::IntLiteral>(&i32, 2)); |
| 479 | auto idx2 = std::make_unique<ast::ScalarConstructorExpression>( |
| 480 | std::make_unique<ast::IntLiteral>(&i32, 1)); |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 481 | auto var = |
| 482 | std::make_unique<ast::Variable>("my_var", ast::StorageClass::kNone, &mat); |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 483 | mod()->AddGlobalVariable(std::move(var)); |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 484 | |
| 485 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 486 | EXPECT_TRUE(td()->Determine()); |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 487 | |
| 488 | ast::ArrayAccessorExpression acc( |
| 489 | std::make_unique<ast::ArrayAccessorExpression>( |
| 490 | std::make_unique<ast::IdentifierExpression>("my_var"), |
| 491 | std::move(idx1)), |
| 492 | std::move(idx2)); |
| 493 | |
| 494 | EXPECT_TRUE(td()->DetermineResultType(&acc)); |
| 495 | ASSERT_NE(acc.result_type(), nullptr); |
| 496 | EXPECT_TRUE(acc.result_type()->IsF32()); |
| 497 | } |
| 498 | |
| 499 | TEST_F(TypeDeterminerTest, Expr_ArrayAccessor_Vector) { |
| 500 | ast::type::I32Type i32; |
| 501 | ast::type::F32Type f32; |
| 502 | ast::type::VectorType vec(&f32, 3); |
| 503 | |
| 504 | auto idx = std::make_unique<ast::ScalarConstructorExpression>( |
| 505 | std::make_unique<ast::IntLiteral>(&i32, 2)); |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 506 | auto var = |
| 507 | std::make_unique<ast::Variable>("my_var", ast::StorageClass::kNone, &vec); |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 508 | mod()->AddGlobalVariable(std::move(var)); |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 509 | |
| 510 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 511 | EXPECT_TRUE(td()->Determine()); |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 512 | |
| 513 | ast::ArrayAccessorExpression acc( |
| 514 | std::make_unique<ast::IdentifierExpression>("my_var"), std::move(idx)); |
| 515 | EXPECT_TRUE(td()->DetermineResultType(&acc)); |
| 516 | ASSERT_NE(acc.result_type(), nullptr); |
| 517 | EXPECT_TRUE(acc.result_type()->IsF32()); |
| 518 | } |
| 519 | |
dan sinclair | a01777c | 2020-04-07 12:57:52 +0000 | [diff] [blame] | 520 | TEST_F(TypeDeterminerTest, Expr_As) { |
| 521 | ast::type::F32Type f32; |
| 522 | ast::AsExpression as(&f32, |
| 523 | std::make_unique<ast::IdentifierExpression>("name")); |
| 524 | |
| 525 | EXPECT_TRUE(td()->DetermineResultType(&as)); |
| 526 | ASSERT_NE(as.result_type(), nullptr); |
| 527 | EXPECT_TRUE(as.result_type()->IsF32()); |
| 528 | } |
| 529 | |
dan sinclair | 3ca8746 | 2020-04-07 16:41:10 +0000 | [diff] [blame] | 530 | TEST_F(TypeDeterminerTest, Expr_Call) { |
| 531 | ast::type::F32Type f32; |
| 532 | |
| 533 | ast::VariableList params; |
| 534 | auto func = |
| 535 | std::make_unique<ast::Function>("my_func", std::move(params), &f32); |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 536 | mod()->AddFunction(std::move(func)); |
dan sinclair | 3ca8746 | 2020-04-07 16:41:10 +0000 | [diff] [blame] | 537 | |
| 538 | // Register the function |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 539 | EXPECT_TRUE(td()->Determine()); |
dan sinclair | 3ca8746 | 2020-04-07 16:41:10 +0000 | [diff] [blame] | 540 | |
| 541 | ast::ExpressionList call_params; |
| 542 | ast::CallExpression call( |
| 543 | std::make_unique<ast::IdentifierExpression>("my_func"), |
| 544 | std::move(call_params)); |
| 545 | EXPECT_TRUE(td()->DetermineResultType(&call)); |
| 546 | ASSERT_NE(call.result_type(), nullptr); |
| 547 | EXPECT_TRUE(call.result_type()->IsF32()); |
| 548 | } |
| 549 | |
dan sinclair | ccb52dc | 2020-04-20 14:18:54 +0000 | [diff] [blame] | 550 | TEST_F(TypeDeterminerTest, Expr_Call_WithParams) { |
| 551 | ast::type::F32Type f32; |
| 552 | |
| 553 | ast::VariableList params; |
| 554 | auto func = |
| 555 | std::make_unique<ast::Function>("my_func", std::move(params), &f32); |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 556 | mod()->AddFunction(std::move(func)); |
dan sinclair | ccb52dc | 2020-04-20 14:18:54 +0000 | [diff] [blame] | 557 | |
| 558 | // Register the function |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 559 | EXPECT_TRUE(td()->Determine()); |
dan sinclair | ccb52dc | 2020-04-20 14:18:54 +0000 | [diff] [blame] | 560 | |
| 561 | ast::ExpressionList call_params; |
| 562 | call_params.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 563 | std::make_unique<ast::FloatLiteral>(&f32, 2.4))); |
| 564 | |
| 565 | auto* param_ptr = call_params.back().get(); |
| 566 | |
| 567 | ast::CallExpression call( |
| 568 | std::make_unique<ast::IdentifierExpression>("my_func"), |
| 569 | std::move(call_params)); |
| 570 | EXPECT_TRUE(td()->DetermineResultType(&call)); |
| 571 | ASSERT_NE(param_ptr->result_type(), nullptr); |
| 572 | EXPECT_TRUE(param_ptr->result_type()->IsF32()); |
| 573 | } |
| 574 | |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 575 | TEST_F(TypeDeterminerTest, Expr_Call_GLSLImport) { |
| 576 | ast::type::F32Type f32; |
| 577 | |
| 578 | mod()->AddImport(std::make_unique<ast::Import>("GLSL.std.450", "std")); |
| 579 | |
| 580 | // Register the function |
| 581 | EXPECT_TRUE(td()->Determine()); |
| 582 | |
| 583 | ast::ExpressionList call_params; |
| 584 | call_params.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 585 | std::make_unique<ast::FloatLiteral>(&f32, 2.4))); |
| 586 | |
| 587 | std::vector<std::string> name{"std", "round"}; |
| 588 | ast::CallExpression call(std::make_unique<ast::IdentifierExpression>(name), |
| 589 | std::move(call_params)); |
| 590 | |
| 591 | EXPECT_TRUE(td()->DetermineResultType(&call)); |
| 592 | ASSERT_NE(call.result_type(), nullptr); |
| 593 | EXPECT_TRUE(call.result_type()->IsF32()); |
| 594 | } |
| 595 | |
dan sinclair | 4e80795 | 2020-04-07 16:41:23 +0000 | [diff] [blame] | 596 | TEST_F(TypeDeterminerTest, Expr_Cast) { |
| 597 | ast::type::F32Type f32; |
| 598 | ast::CastExpression cast(&f32, |
| 599 | std::make_unique<ast::IdentifierExpression>("name")); |
| 600 | |
| 601 | EXPECT_TRUE(td()->DetermineResultType(&cast)); |
| 602 | ASSERT_NE(cast.result_type(), nullptr); |
| 603 | EXPECT_TRUE(cast.result_type()->IsF32()); |
| 604 | } |
| 605 | |
dan sinclair | b7edc4c | 2020-04-07 12:46:30 +0000 | [diff] [blame] | 606 | TEST_F(TypeDeterminerTest, Expr_Constructor_Scalar) { |
| 607 | ast::type::F32Type f32; |
| 608 | ast::ScalarConstructorExpression s( |
| 609 | std::make_unique<ast::FloatLiteral>(&f32, 1.0f)); |
| 610 | |
| 611 | EXPECT_TRUE(td()->DetermineResultType(&s)); |
| 612 | ASSERT_NE(s.result_type(), nullptr); |
| 613 | EXPECT_TRUE(s.result_type()->IsF32()); |
| 614 | } |
| 615 | |
| 616 | TEST_F(TypeDeterminerTest, Expr_Constructor_Type) { |
| 617 | ast::type::F32Type f32; |
| 618 | ast::type::VectorType vec(&f32, 3); |
| 619 | |
| 620 | ast::ExpressionList vals; |
| 621 | vals.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 622 | std::make_unique<ast::FloatLiteral>(&f32, 1.0f))); |
| 623 | vals.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 624 | std::make_unique<ast::FloatLiteral>(&f32, 1.0f))); |
| 625 | vals.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 626 | std::make_unique<ast::FloatLiteral>(&f32, 3.0f))); |
| 627 | |
| 628 | ast::TypeConstructorExpression tc(&vec, std::move(vals)); |
| 629 | |
| 630 | EXPECT_TRUE(td()->DetermineResultType(&tc)); |
| 631 | ASSERT_NE(tc.result_type(), nullptr); |
| 632 | ASSERT_TRUE(tc.result_type()->IsVector()); |
| 633 | EXPECT_TRUE(tc.result_type()->AsVector()->type()->IsF32()); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 634 | EXPECT_EQ(tc.result_type()->AsVector()->size(), 3u); |
dan sinclair | b7edc4c | 2020-04-07 12:46:30 +0000 | [diff] [blame] | 635 | } |
| 636 | |
dan sinclair | cab0e73 | 2020-04-07 12:57:27 +0000 | [diff] [blame] | 637 | TEST_F(TypeDeterminerTest, Expr_Identifier_GlobalVariable) { |
| 638 | ast::type::F32Type f32; |
dan sinclair | cab0e73 | 2020-04-07 12:57:27 +0000 | [diff] [blame] | 639 | auto var = |
| 640 | std::make_unique<ast::Variable>("my_var", ast::StorageClass::kNone, &f32); |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 641 | mod()->AddGlobalVariable(std::move(var)); |
dan sinclair | cab0e73 | 2020-04-07 12:57:27 +0000 | [diff] [blame] | 642 | |
| 643 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 644 | EXPECT_TRUE(td()->Determine()); |
dan sinclair | cab0e73 | 2020-04-07 12:57:27 +0000 | [diff] [blame] | 645 | |
| 646 | ast::IdentifierExpression ident("my_var"); |
| 647 | EXPECT_TRUE(td()->DetermineResultType(&ident)); |
| 648 | ASSERT_NE(ident.result_type(), nullptr); |
| 649 | EXPECT_TRUE(ident.result_type()->IsF32()); |
| 650 | } |
| 651 | |
| 652 | TEST_F(TypeDeterminerTest, Expr_Identifier_FunctionVariable) { |
| 653 | ast::type::F32Type f32; |
| 654 | |
| 655 | auto my_var = std::make_unique<ast::IdentifierExpression>("my_var"); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 656 | auto* my_var_ptr = my_var.get(); |
dan sinclair | cab0e73 | 2020-04-07 12:57:27 +0000 | [diff] [blame] | 657 | |
| 658 | ast::StatementList body; |
| 659 | body.push_back(std::make_unique<ast::VariableDeclStatement>( |
| 660 | std::make_unique<ast::Variable>("my_var", ast::StorageClass::kNone, |
| 661 | &f32))); |
| 662 | |
| 663 | body.push_back(std::make_unique<ast::AssignmentStatement>( |
| 664 | std::move(my_var), |
| 665 | std::make_unique<ast::IdentifierExpression>("my_var"))); |
| 666 | |
| 667 | ast::Function f("my_func", {}, &f32); |
| 668 | f.set_body(std::move(body)); |
| 669 | |
| 670 | EXPECT_TRUE(td()->DetermineFunction(&f)); |
| 671 | |
| 672 | ASSERT_NE(my_var_ptr->result_type(), nullptr); |
| 673 | EXPECT_TRUE(my_var_ptr->result_type()->IsF32()); |
| 674 | } |
| 675 | |
| 676 | TEST_F(TypeDeterminerTest, Expr_Identifier_Function) { |
| 677 | ast::type::F32Type f32; |
| 678 | |
| 679 | ast::VariableList params; |
| 680 | auto func = |
| 681 | std::make_unique<ast::Function>("my_func", std::move(params), &f32); |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 682 | mod()->AddFunction(std::move(func)); |
dan sinclair | cab0e73 | 2020-04-07 12:57:27 +0000 | [diff] [blame] | 683 | |
| 684 | // Register the function |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 685 | EXPECT_TRUE(td()->Determine()); |
dan sinclair | cab0e73 | 2020-04-07 12:57:27 +0000 | [diff] [blame] | 686 | |
| 687 | ast::IdentifierExpression ident("my_func"); |
| 688 | EXPECT_TRUE(td()->DetermineResultType(&ident)); |
| 689 | ASSERT_NE(ident.result_type(), nullptr); |
| 690 | EXPECT_TRUE(ident.result_type()->IsF32()); |
| 691 | } |
| 692 | |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 693 | TEST_F(TypeDeterminerTest, Expr_MemberAccessor_Struct) { |
| 694 | ast::type::I32Type i32; |
| 695 | ast::type::F32Type f32; |
| 696 | |
| 697 | ast::StructMemberDecorationList decos; |
| 698 | ast::StructMemberList members; |
| 699 | members.push_back(std::make_unique<ast::StructMember>("first_member", &i32, |
| 700 | std::move(decos))); |
| 701 | members.push_back(std::make_unique<ast::StructMember>("second_member", &f32, |
| 702 | std::move(decos))); |
| 703 | |
| 704 | auto strct = std::make_unique<ast::Struct>(ast::StructDecoration::kNone, |
| 705 | std::move(members)); |
| 706 | |
| 707 | ast::type::StructType st(std::move(strct)); |
| 708 | |
| 709 | auto var = std::make_unique<ast::Variable>("my_struct", |
| 710 | ast::StorageClass::kNone, &st); |
| 711 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 712 | mod()->AddGlobalVariable(std::move(var)); |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 713 | |
| 714 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 715 | EXPECT_TRUE(td()->Determine()); |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 716 | |
| 717 | auto ident = std::make_unique<ast::IdentifierExpression>("my_struct"); |
| 718 | auto mem_ident = std::make_unique<ast::IdentifierExpression>("second_member"); |
| 719 | |
| 720 | ast::MemberAccessorExpression mem(std::move(ident), std::move(mem_ident)); |
| 721 | EXPECT_TRUE(td()->DetermineResultType(&mem)); |
| 722 | ASSERT_NE(mem.result_type(), nullptr); |
| 723 | EXPECT_TRUE(mem.result_type()->IsF32()); |
| 724 | } |
| 725 | |
| 726 | TEST_F(TypeDeterminerTest, Expr_MemberAccessor_VectorSwizzle) { |
| 727 | ast::type::F32Type f32; |
| 728 | ast::type::VectorType vec3(&f32, 3); |
| 729 | |
| 730 | auto var = std::make_unique<ast::Variable>("my_vec", ast::StorageClass::kNone, |
| 731 | &vec3); |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 732 | mod()->AddGlobalVariable(std::move(var)); |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 733 | |
| 734 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 735 | EXPECT_TRUE(td()->Determine()); |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 736 | |
| 737 | auto ident = std::make_unique<ast::IdentifierExpression>("my_vec"); |
| 738 | auto swizzle = std::make_unique<ast::IdentifierExpression>("xy"); |
| 739 | |
| 740 | ast::MemberAccessorExpression mem(std::move(ident), std::move(swizzle)); |
| 741 | EXPECT_TRUE(td()->DetermineResultType(&mem)) << td()->error(); |
| 742 | ASSERT_NE(mem.result_type(), nullptr); |
| 743 | ASSERT_TRUE(mem.result_type()->IsVector()); |
| 744 | EXPECT_TRUE(mem.result_type()->AsVector()->type()->IsF32()); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 745 | EXPECT_EQ(mem.result_type()->AsVector()->size(), 2u); |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | TEST_F(TypeDeterminerTest, Expr_MultiLevel) { |
| 749 | // struct b { |
| 750 | // vec4<f32> foo |
| 751 | // } |
| 752 | // struct A { |
| 753 | // vec3<struct b> mem |
| 754 | // } |
| 755 | // var c : A |
| 756 | // c.mem[0].foo.yx |
| 757 | // -> vec2<f32> |
| 758 | // |
| 759 | // MemberAccessor{ |
| 760 | // MemberAccessor{ |
| 761 | // ArrayAccessor{ |
| 762 | // MemberAccessor{ |
| 763 | // Identifier{c} |
| 764 | // Identifier{mem} |
| 765 | // } |
| 766 | // ScalarConstructor{0} |
| 767 | // } |
| 768 | // Identifier{foo} |
| 769 | // } |
| 770 | // Identifier{yx} |
| 771 | // } |
| 772 | // |
| 773 | ast::type::I32Type i32; |
| 774 | ast::type::F32Type f32; |
| 775 | |
| 776 | ast::type::VectorType vec4(&f32, 4); |
| 777 | |
| 778 | ast::StructMemberDecorationList decos; |
| 779 | ast::StructMemberList b_members; |
| 780 | b_members.push_back( |
| 781 | std::make_unique<ast::StructMember>("foo", &vec4, std::move(decos))); |
| 782 | |
| 783 | auto strctB = std::make_unique<ast::Struct>(ast::StructDecoration::kNone, |
| 784 | std::move(b_members)); |
| 785 | ast::type::StructType stB(std::move(strctB)); |
| 786 | |
| 787 | ast::type::VectorType vecB(&stB, 3); |
| 788 | |
| 789 | ast::StructMemberList a_members; |
| 790 | a_members.push_back( |
| 791 | std::make_unique<ast::StructMember>("mem", &vecB, std::move(decos))); |
| 792 | |
| 793 | auto strctA = std::make_unique<ast::Struct>(ast::StructDecoration::kNone, |
| 794 | std::move(a_members)); |
| 795 | |
| 796 | ast::type::StructType stA(std::move(strctA)); |
| 797 | |
| 798 | auto var = |
| 799 | std::make_unique<ast::Variable>("c", ast::StorageClass::kNone, &stA); |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 800 | mod()->AddGlobalVariable(std::move(var)); |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 801 | |
| 802 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 803 | EXPECT_TRUE(td()->Determine()); |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 804 | |
| 805 | auto ident = std::make_unique<ast::IdentifierExpression>("c"); |
| 806 | auto mem_ident = std::make_unique<ast::IdentifierExpression>("mem"); |
| 807 | auto foo_ident = std::make_unique<ast::IdentifierExpression>("foo"); |
| 808 | auto idx = std::make_unique<ast::ScalarConstructorExpression>( |
| 809 | std::make_unique<ast::IntLiteral>(&i32, 0)); |
| 810 | auto swizzle = std::make_unique<ast::IdentifierExpression>("yx"); |
| 811 | |
| 812 | ast::MemberAccessorExpression mem( |
| 813 | std::make_unique<ast::MemberAccessorExpression>( |
| 814 | std::make_unique<ast::ArrayAccessorExpression>( |
| 815 | std::make_unique<ast::MemberAccessorExpression>( |
| 816 | std::move(ident), std::move(mem_ident)), |
| 817 | std::move(idx)), |
| 818 | std::move(foo_ident)), |
| 819 | std::move(swizzle)); |
| 820 | EXPECT_TRUE(td()->DetermineResultType(&mem)) << td()->error(); |
| 821 | ASSERT_NE(mem.result_type(), nullptr); |
| 822 | ASSERT_TRUE(mem.result_type()->IsVector()); |
| 823 | EXPECT_TRUE(mem.result_type()->AsVector()->type()->IsF32()); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 824 | EXPECT_EQ(mem.result_type()->AsVector()->size(), 2u); |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 825 | } |
| 826 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 827 | using Expr_Binary_BitwiseTest = TypeDeterminerTestWithParam<ast::BinaryOp>; |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 828 | TEST_P(Expr_Binary_BitwiseTest, Scalar) { |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 829 | auto op = GetParam(); |
| 830 | |
| 831 | ast::type::I32Type i32; |
| 832 | |
| 833 | auto var = |
| 834 | std::make_unique<ast::Variable>("val", ast::StorageClass::kNone, &i32); |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 835 | mod()->AddGlobalVariable(std::move(var)); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 836 | |
| 837 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 838 | ASSERT_TRUE(td()->Determine()) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 839 | |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 840 | ast::BinaryExpression expr( |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 841 | op, std::make_unique<ast::IdentifierExpression>("val"), |
| 842 | std::make_unique<ast::IdentifierExpression>("val")); |
| 843 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 844 | ASSERT_TRUE(td()->DetermineResultType(&expr)) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 845 | ASSERT_NE(expr.result_type(), nullptr); |
| 846 | EXPECT_TRUE(expr.result_type()->IsI32()); |
| 847 | } |
| 848 | |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 849 | TEST_P(Expr_Binary_BitwiseTest, Vector) { |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 850 | auto op = GetParam(); |
| 851 | |
| 852 | ast::type::I32Type i32; |
| 853 | ast::type::VectorType vec3(&i32, 3); |
| 854 | |
| 855 | auto var = |
| 856 | std::make_unique<ast::Variable>("val", ast::StorageClass::kNone, &vec3); |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 857 | mod()->AddGlobalVariable(std::move(var)); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 858 | |
| 859 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 860 | ASSERT_TRUE(td()->Determine()) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 861 | |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 862 | ast::BinaryExpression expr( |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 863 | op, std::make_unique<ast::IdentifierExpression>("val"), |
| 864 | std::make_unique<ast::IdentifierExpression>("val")); |
| 865 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 866 | ASSERT_TRUE(td()->DetermineResultType(&expr)) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 867 | ASSERT_NE(expr.result_type(), nullptr); |
| 868 | ASSERT_TRUE(expr.result_type()->IsVector()); |
| 869 | EXPECT_TRUE(expr.result_type()->AsVector()->type()->IsI32()); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 870 | EXPECT_EQ(expr.result_type()->AsVector()->size(), 3u); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 871 | } |
| 872 | INSTANTIATE_TEST_SUITE_P(TypeDeterminerTest, |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 873 | Expr_Binary_BitwiseTest, |
| 874 | testing::Values(ast::BinaryOp::kAnd, |
| 875 | ast::BinaryOp::kOr, |
| 876 | ast::BinaryOp::kXor, |
| 877 | ast::BinaryOp::kShiftLeft, |
| 878 | ast::BinaryOp::kShiftRight, |
| 879 | ast::BinaryOp::kShiftRightArith, |
| 880 | ast::BinaryOp::kAdd, |
| 881 | ast::BinaryOp::kSubtract, |
| 882 | ast::BinaryOp::kDivide, |
| 883 | ast::BinaryOp::kModulo)); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 884 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 885 | using Expr_Binary_LogicalTest = TypeDeterminerTestWithParam<ast::BinaryOp>; |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 886 | TEST_P(Expr_Binary_LogicalTest, Scalar) { |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 887 | auto op = GetParam(); |
| 888 | |
| 889 | ast::type::BoolType bool_type; |
| 890 | |
| 891 | auto var = std::make_unique<ast::Variable>("val", ast::StorageClass::kNone, |
| 892 | &bool_type); |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 893 | mod()->AddGlobalVariable(std::move(var)); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 894 | |
| 895 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 896 | ASSERT_TRUE(td()->Determine()) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 897 | |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 898 | ast::BinaryExpression expr( |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 899 | op, std::make_unique<ast::IdentifierExpression>("val"), |
| 900 | std::make_unique<ast::IdentifierExpression>("val")); |
| 901 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 902 | ASSERT_TRUE(td()->DetermineResultType(&expr)) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 903 | ASSERT_NE(expr.result_type(), nullptr); |
| 904 | EXPECT_TRUE(expr.result_type()->IsBool()); |
| 905 | } |
| 906 | |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 907 | TEST_P(Expr_Binary_LogicalTest, Vector) { |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 908 | auto op = GetParam(); |
| 909 | |
| 910 | ast::type::BoolType bool_type; |
| 911 | ast::type::VectorType vec3(&bool_type, 3); |
| 912 | |
| 913 | auto var = |
| 914 | std::make_unique<ast::Variable>("val", ast::StorageClass::kNone, &vec3); |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 915 | mod()->AddGlobalVariable(std::move(var)); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 916 | |
| 917 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 918 | ASSERT_TRUE(td()->Determine()) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 919 | |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 920 | ast::BinaryExpression expr( |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 921 | op, std::make_unique<ast::IdentifierExpression>("val"), |
| 922 | std::make_unique<ast::IdentifierExpression>("val")); |
| 923 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 924 | ASSERT_TRUE(td()->DetermineResultType(&expr)) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 925 | ASSERT_NE(expr.result_type(), nullptr); |
| 926 | ASSERT_TRUE(expr.result_type()->IsVector()); |
| 927 | EXPECT_TRUE(expr.result_type()->AsVector()->type()->IsBool()); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 928 | EXPECT_EQ(expr.result_type()->AsVector()->size(), 3u); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 929 | } |
| 930 | INSTANTIATE_TEST_SUITE_P(TypeDeterminerTest, |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 931 | Expr_Binary_LogicalTest, |
| 932 | testing::Values(ast::BinaryOp::kLogicalAnd, |
| 933 | ast::BinaryOp::kLogicalOr)); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 934 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 935 | using Expr_Binary_CompareTest = TypeDeterminerTestWithParam<ast::BinaryOp>; |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 936 | TEST_P(Expr_Binary_CompareTest, Scalar) { |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 937 | auto op = GetParam(); |
| 938 | |
| 939 | ast::type::I32Type i32; |
| 940 | |
| 941 | auto var = |
| 942 | std::make_unique<ast::Variable>("val", ast::StorageClass::kNone, &i32); |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 943 | mod()->AddGlobalVariable(std::move(var)); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 944 | |
| 945 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 946 | ASSERT_TRUE(td()->Determine()) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 947 | |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 948 | ast::BinaryExpression expr( |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 949 | op, std::make_unique<ast::IdentifierExpression>("val"), |
| 950 | std::make_unique<ast::IdentifierExpression>("val")); |
| 951 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 952 | ASSERT_TRUE(td()->DetermineResultType(&expr)) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 953 | ASSERT_NE(expr.result_type(), nullptr); |
| 954 | EXPECT_TRUE(expr.result_type()->IsBool()); |
| 955 | } |
| 956 | |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 957 | TEST_P(Expr_Binary_CompareTest, Vector) { |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 958 | auto op = GetParam(); |
| 959 | |
| 960 | ast::type::I32Type i32; |
| 961 | ast::type::VectorType vec3(&i32, 3); |
| 962 | |
| 963 | auto var = |
| 964 | std::make_unique<ast::Variable>("val", ast::StorageClass::kNone, &vec3); |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 965 | mod()->AddGlobalVariable(std::move(var)); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 966 | |
| 967 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 968 | ASSERT_TRUE(td()->Determine()) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 969 | |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 970 | ast::BinaryExpression expr( |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 971 | op, std::make_unique<ast::IdentifierExpression>("val"), |
| 972 | std::make_unique<ast::IdentifierExpression>("val")); |
| 973 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 974 | ASSERT_TRUE(td()->DetermineResultType(&expr)) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 975 | ASSERT_NE(expr.result_type(), nullptr); |
| 976 | ASSERT_TRUE(expr.result_type()->IsVector()); |
| 977 | EXPECT_TRUE(expr.result_type()->AsVector()->type()->IsBool()); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 978 | EXPECT_EQ(expr.result_type()->AsVector()->size(), 3u); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 979 | } |
| 980 | INSTANTIATE_TEST_SUITE_P(TypeDeterminerTest, |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 981 | Expr_Binary_CompareTest, |
| 982 | testing::Values(ast::BinaryOp::kEqual, |
| 983 | ast::BinaryOp::kNotEqual, |
| 984 | ast::BinaryOp::kLessThan, |
| 985 | ast::BinaryOp::kGreaterThan, |
| 986 | ast::BinaryOp::kLessThanEqual, |
| 987 | ast::BinaryOp::kGreaterThanEqual)); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 988 | |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 989 | TEST_F(TypeDeterminerTest, Expr_Binary_Multiply_Scalar_Scalar) { |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 990 | ast::type::I32Type i32; |
| 991 | |
| 992 | auto var = |
| 993 | std::make_unique<ast::Variable>("val", ast::StorageClass::kNone, &i32); |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 994 | mod()->AddGlobalVariable(std::move(var)); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 995 | |
| 996 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 997 | ASSERT_TRUE(td()->Determine()) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 998 | |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 999 | ast::BinaryExpression expr( |
| 1000 | ast::BinaryOp::kMultiply, |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1001 | std::make_unique<ast::IdentifierExpression>("val"), |
| 1002 | std::make_unique<ast::IdentifierExpression>("val")); |
| 1003 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1004 | ASSERT_TRUE(td()->DetermineResultType(&expr)) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1005 | ASSERT_NE(expr.result_type(), nullptr); |
| 1006 | EXPECT_TRUE(expr.result_type()->IsI32()); |
| 1007 | } |
| 1008 | |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 1009 | TEST_F(TypeDeterminerTest, Expr_Binary_Multiply_Vector_Scalar) { |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1010 | ast::type::F32Type f32; |
| 1011 | ast::type::VectorType vec3(&f32, 3); |
| 1012 | |
| 1013 | auto scalar = |
| 1014 | std::make_unique<ast::Variable>("scalar", ast::StorageClass::kNone, &f32); |
| 1015 | auto vector = std::make_unique<ast::Variable>( |
| 1016 | "vector", ast::StorageClass::kNone, &vec3); |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1017 | mod()->AddGlobalVariable(std::move(scalar)); |
| 1018 | mod()->AddGlobalVariable(std::move(vector)); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1019 | |
| 1020 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 1021 | ASSERT_TRUE(td()->Determine()) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1022 | |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 1023 | ast::BinaryExpression expr( |
| 1024 | ast::BinaryOp::kMultiply, |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1025 | std::make_unique<ast::IdentifierExpression>("vector"), |
| 1026 | std::make_unique<ast::IdentifierExpression>("scalar")); |
| 1027 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1028 | ASSERT_TRUE(td()->DetermineResultType(&expr)) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1029 | ASSERT_NE(expr.result_type(), nullptr); |
| 1030 | ASSERT_TRUE(expr.result_type()->IsVector()); |
| 1031 | EXPECT_TRUE(expr.result_type()->AsVector()->type()->IsF32()); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 1032 | EXPECT_EQ(expr.result_type()->AsVector()->size(), 3u); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 1035 | TEST_F(TypeDeterminerTest, Expr_Binary_Multiply_Scalar_Vector) { |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1036 | ast::type::F32Type f32; |
| 1037 | ast::type::VectorType vec3(&f32, 3); |
| 1038 | |
| 1039 | auto scalar = |
| 1040 | std::make_unique<ast::Variable>("scalar", ast::StorageClass::kNone, &f32); |
| 1041 | auto vector = std::make_unique<ast::Variable>( |
| 1042 | "vector", ast::StorageClass::kNone, &vec3); |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1043 | mod()->AddGlobalVariable(std::move(scalar)); |
| 1044 | mod()->AddGlobalVariable(std::move(vector)); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1045 | |
| 1046 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 1047 | ASSERT_TRUE(td()->Determine()) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1048 | |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 1049 | ast::BinaryExpression expr( |
| 1050 | ast::BinaryOp::kMultiply, |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1051 | std::make_unique<ast::IdentifierExpression>("scalar"), |
| 1052 | std::make_unique<ast::IdentifierExpression>("vector")); |
| 1053 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1054 | ASSERT_TRUE(td()->DetermineResultType(&expr)) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1055 | ASSERT_NE(expr.result_type(), nullptr); |
| 1056 | ASSERT_TRUE(expr.result_type()->IsVector()); |
| 1057 | EXPECT_TRUE(expr.result_type()->AsVector()->type()->IsF32()); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 1058 | EXPECT_EQ(expr.result_type()->AsVector()->size(), 3u); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 1061 | TEST_F(TypeDeterminerTest, Expr_Binary_Multiply_Vector_Vector) { |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1062 | ast::type::F32Type f32; |
| 1063 | ast::type::VectorType vec3(&f32, 3); |
| 1064 | |
| 1065 | auto vector = std::make_unique<ast::Variable>( |
| 1066 | "vector", ast::StorageClass::kNone, &vec3); |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1067 | mod()->AddGlobalVariable(std::move(vector)); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1068 | |
| 1069 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 1070 | ASSERT_TRUE(td()->Determine()) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1071 | |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 1072 | ast::BinaryExpression expr( |
| 1073 | ast::BinaryOp::kMultiply, |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1074 | std::make_unique<ast::IdentifierExpression>("vector"), |
| 1075 | std::make_unique<ast::IdentifierExpression>("vector")); |
| 1076 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1077 | ASSERT_TRUE(td()->DetermineResultType(&expr)) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1078 | ASSERT_NE(expr.result_type(), nullptr); |
| 1079 | ASSERT_TRUE(expr.result_type()->IsVector()); |
| 1080 | EXPECT_TRUE(expr.result_type()->AsVector()->type()->IsF32()); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 1081 | EXPECT_EQ(expr.result_type()->AsVector()->size(), 3u); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 1084 | TEST_F(TypeDeterminerTest, Expr_Binary_Multiply_Matrix_Scalar) { |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1085 | ast::type::F32Type f32; |
| 1086 | ast::type::MatrixType mat3x2(&f32, 3, 2); |
| 1087 | |
| 1088 | auto scalar = |
| 1089 | std::make_unique<ast::Variable>("scalar", ast::StorageClass::kNone, &f32); |
| 1090 | auto matrix = std::make_unique<ast::Variable>( |
| 1091 | "matrix", ast::StorageClass::kNone, &mat3x2); |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1092 | mod()->AddGlobalVariable(std::move(scalar)); |
| 1093 | mod()->AddGlobalVariable(std::move(matrix)); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1094 | |
| 1095 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 1096 | ASSERT_TRUE(td()->Determine()) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1097 | |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 1098 | ast::BinaryExpression expr( |
| 1099 | ast::BinaryOp::kMultiply, |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1100 | std::make_unique<ast::IdentifierExpression>("matrix"), |
| 1101 | std::make_unique<ast::IdentifierExpression>("scalar")); |
| 1102 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1103 | ASSERT_TRUE(td()->DetermineResultType(&expr)) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1104 | ASSERT_NE(expr.result_type(), nullptr); |
| 1105 | ASSERT_TRUE(expr.result_type()->IsMatrix()); |
| 1106 | |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 1107 | auto* mat = expr.result_type()->AsMatrix(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1108 | EXPECT_TRUE(mat->type()->IsF32()); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 1109 | EXPECT_EQ(mat->rows(), 3u); |
| 1110 | EXPECT_EQ(mat->columns(), 2u); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 1113 | TEST_F(TypeDeterminerTest, Expr_Binary_Multiply_Scalar_Matrix) { |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1114 | ast::type::F32Type f32; |
| 1115 | ast::type::MatrixType mat3x2(&f32, 3, 2); |
| 1116 | |
| 1117 | auto scalar = |
| 1118 | std::make_unique<ast::Variable>("scalar", ast::StorageClass::kNone, &f32); |
| 1119 | auto matrix = std::make_unique<ast::Variable>( |
| 1120 | "matrix", ast::StorageClass::kNone, &mat3x2); |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1121 | mod()->AddGlobalVariable(std::move(scalar)); |
| 1122 | mod()->AddGlobalVariable(std::move(matrix)); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1123 | |
| 1124 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 1125 | ASSERT_TRUE(td()->Determine()) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1126 | |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 1127 | ast::BinaryExpression expr( |
| 1128 | ast::BinaryOp::kMultiply, |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1129 | std::make_unique<ast::IdentifierExpression>("scalar"), |
| 1130 | std::make_unique<ast::IdentifierExpression>("matrix")); |
| 1131 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1132 | ASSERT_TRUE(td()->DetermineResultType(&expr)) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1133 | ASSERT_NE(expr.result_type(), nullptr); |
| 1134 | ASSERT_TRUE(expr.result_type()->IsMatrix()); |
| 1135 | |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 1136 | auto* mat = expr.result_type()->AsMatrix(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1137 | EXPECT_TRUE(mat->type()->IsF32()); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 1138 | EXPECT_EQ(mat->rows(), 3u); |
| 1139 | EXPECT_EQ(mat->columns(), 2u); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 1142 | TEST_F(TypeDeterminerTest, Expr_Binary_Multiply_Matrix_Vector) { |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1143 | ast::type::F32Type f32; |
| 1144 | ast::type::VectorType vec3(&f32, 2); |
| 1145 | ast::type::MatrixType mat3x2(&f32, 3, 2); |
| 1146 | |
| 1147 | auto vector = std::make_unique<ast::Variable>( |
| 1148 | "vector", ast::StorageClass::kNone, &vec3); |
| 1149 | auto matrix = std::make_unique<ast::Variable>( |
| 1150 | "matrix", ast::StorageClass::kNone, &mat3x2); |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1151 | mod()->AddGlobalVariable(std::move(vector)); |
| 1152 | mod()->AddGlobalVariable(std::move(matrix)); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1153 | |
| 1154 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 1155 | ASSERT_TRUE(td()->Determine()) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1156 | |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 1157 | ast::BinaryExpression expr( |
| 1158 | ast::BinaryOp::kMultiply, |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1159 | std::make_unique<ast::IdentifierExpression>("matrix"), |
| 1160 | std::make_unique<ast::IdentifierExpression>("vector")); |
| 1161 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1162 | ASSERT_TRUE(td()->DetermineResultType(&expr)) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1163 | ASSERT_NE(expr.result_type(), nullptr); |
| 1164 | ASSERT_TRUE(expr.result_type()->IsVector()); |
| 1165 | EXPECT_TRUE(expr.result_type()->AsVector()->type()->IsF32()); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 1166 | EXPECT_EQ(expr.result_type()->AsVector()->size(), 3u); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 1169 | TEST_F(TypeDeterminerTest, Expr_Binary_Multiply_Vector_Matrix) { |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1170 | ast::type::F32Type f32; |
| 1171 | ast::type::VectorType vec3(&f32, 3); |
| 1172 | ast::type::MatrixType mat3x2(&f32, 3, 2); |
| 1173 | |
| 1174 | auto vector = std::make_unique<ast::Variable>( |
| 1175 | "vector", ast::StorageClass::kNone, &vec3); |
| 1176 | auto matrix = std::make_unique<ast::Variable>( |
| 1177 | "matrix", ast::StorageClass::kNone, &mat3x2); |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1178 | mod()->AddGlobalVariable(std::move(vector)); |
| 1179 | mod()->AddGlobalVariable(std::move(matrix)); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1180 | |
| 1181 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 1182 | ASSERT_TRUE(td()->Determine()) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1183 | |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 1184 | ast::BinaryExpression expr( |
| 1185 | ast::BinaryOp::kMultiply, |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1186 | std::make_unique<ast::IdentifierExpression>("vector"), |
| 1187 | std::make_unique<ast::IdentifierExpression>("matrix")); |
| 1188 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1189 | ASSERT_TRUE(td()->DetermineResultType(&expr)) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1190 | ASSERT_NE(expr.result_type(), nullptr); |
| 1191 | ASSERT_TRUE(expr.result_type()->IsVector()); |
| 1192 | EXPECT_TRUE(expr.result_type()->AsVector()->type()->IsF32()); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 1193 | EXPECT_EQ(expr.result_type()->AsVector()->size(), 2u); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 1196 | TEST_F(TypeDeterminerTest, Expr_Binary_Multiply_Matrix_Matrix) { |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1197 | ast::type::F32Type f32; |
| 1198 | ast::type::MatrixType mat4x3(&f32, 4, 3); |
| 1199 | ast::type::MatrixType mat3x4(&f32, 3, 4); |
| 1200 | |
| 1201 | auto matrix1 = std::make_unique<ast::Variable>( |
| 1202 | "mat4x3", ast::StorageClass::kNone, &mat4x3); |
| 1203 | auto matrix2 = std::make_unique<ast::Variable>( |
| 1204 | "mat3x4", ast::StorageClass::kNone, &mat3x4); |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1205 | mod()->AddGlobalVariable(std::move(matrix1)); |
| 1206 | mod()->AddGlobalVariable(std::move(matrix2)); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1207 | |
| 1208 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 1209 | ASSERT_TRUE(td()->Determine()) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1210 | |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 1211 | ast::BinaryExpression expr( |
| 1212 | ast::BinaryOp::kMultiply, |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1213 | std::make_unique<ast::IdentifierExpression>("mat4x3"), |
| 1214 | std::make_unique<ast::IdentifierExpression>("mat3x4")); |
| 1215 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1216 | ASSERT_TRUE(td()->DetermineResultType(&expr)) << td()->error(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1217 | ASSERT_NE(expr.result_type(), nullptr); |
| 1218 | ASSERT_TRUE(expr.result_type()->IsMatrix()); |
| 1219 | |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 1220 | auto* mat = expr.result_type()->AsMatrix(); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1221 | EXPECT_TRUE(mat->type()->IsF32()); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 1222 | EXPECT_EQ(mat->rows(), 4u); |
| 1223 | EXPECT_EQ(mat->columns(), 4u); |
dan sinclair | 9b97802 | 2020-04-07 19:26:39 +0000 | [diff] [blame] | 1224 | } |
| 1225 | |
dan sinclair | b173056 | 2020-04-07 19:26:49 +0000 | [diff] [blame] | 1226 | using UnaryDerivativeExpressionTest = |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1227 | TypeDeterminerTestWithParam<ast::UnaryDerivative>; |
dan sinclair | b173056 | 2020-04-07 19:26:49 +0000 | [diff] [blame] | 1228 | TEST_P(UnaryDerivativeExpressionTest, Expr_UnaryDerivative) { |
| 1229 | auto derivative = GetParam(); |
| 1230 | |
| 1231 | ast::type::F32Type f32; |
| 1232 | |
| 1233 | ast::type::VectorType vec4(&f32, 4); |
| 1234 | |
| 1235 | auto var = |
| 1236 | std::make_unique<ast::Variable>("ident", ast::StorageClass::kNone, &vec4); |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1237 | mod()->AddGlobalVariable(std::move(var)); |
dan sinclair | b173056 | 2020-04-07 19:26:49 +0000 | [diff] [blame] | 1238 | |
| 1239 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 1240 | EXPECT_TRUE(td()->Determine()); |
dan sinclair | b173056 | 2020-04-07 19:26:49 +0000 | [diff] [blame] | 1241 | |
| 1242 | ast::UnaryDerivativeExpression der( |
| 1243 | derivative, ast::DerivativeModifier::kNone, |
| 1244 | std::make_unique<ast::IdentifierExpression>("ident")); |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1245 | EXPECT_TRUE(td()->DetermineResultType(&der)); |
dan sinclair | b173056 | 2020-04-07 19:26:49 +0000 | [diff] [blame] | 1246 | ASSERT_NE(der.result_type(), nullptr); |
| 1247 | ASSERT_TRUE(der.result_type()->IsVector()); |
| 1248 | EXPECT_TRUE(der.result_type()->AsVector()->type()->IsF32()); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 1249 | EXPECT_EQ(der.result_type()->AsVector()->size(), 4u); |
dan sinclair | b173056 | 2020-04-07 19:26:49 +0000 | [diff] [blame] | 1250 | } |
| 1251 | INSTANTIATE_TEST_SUITE_P(TypeDeterminerTest, |
| 1252 | UnaryDerivativeExpressionTest, |
| 1253 | testing::Values(ast::UnaryDerivative::kDpdx, |
| 1254 | ast::UnaryDerivative::kDpdy, |
| 1255 | ast::UnaryDerivative::kFwidth)); |
| 1256 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1257 | using UnaryMethodExpressionBoolTest = |
| 1258 | TypeDeterminerTestWithParam<ast::UnaryMethod>; |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 1259 | TEST_P(UnaryMethodExpressionBoolTest, Expr_UnaryMethod_Any) { |
| 1260 | auto op = GetParam(); |
| 1261 | |
| 1262 | ast::type::BoolType bool_type; |
| 1263 | ast::type::VectorType vec3(&bool_type, 3); |
| 1264 | |
| 1265 | auto var = std::make_unique<ast::Variable>("my_var", ast::StorageClass::kNone, |
| 1266 | &vec3); |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1267 | mod()->AddGlobalVariable(std::move(var)); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 1268 | |
| 1269 | ast::ExpressionList params; |
| 1270 | params.push_back(std::make_unique<ast::IdentifierExpression>("my_var")); |
| 1271 | |
| 1272 | ast::UnaryMethodExpression exp(op, std::move(params)); |
| 1273 | |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 1274 | // Register the variable |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 1275 | EXPECT_TRUE(td()->Determine()); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 1276 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1277 | EXPECT_TRUE(td()->DetermineResultType(&exp)); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 1278 | ASSERT_NE(exp.result_type(), nullptr); |
| 1279 | EXPECT_TRUE(exp.result_type()->IsBool()); |
| 1280 | } |
| 1281 | INSTANTIATE_TEST_SUITE_P(TypeDeterminerTest, |
| 1282 | UnaryMethodExpressionBoolTest, |
| 1283 | testing::Values(ast::UnaryMethod::kAny, |
| 1284 | ast::UnaryMethod::kAll)); |
| 1285 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1286 | using UnaryMethodExpressionVecTest = |
| 1287 | TypeDeterminerTestWithParam<ast::UnaryMethod>; |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 1288 | TEST_P(UnaryMethodExpressionVecTest, Expr_UnaryMethod_Bool) { |
| 1289 | auto op = GetParam(); |
| 1290 | |
| 1291 | ast::type::F32Type f32; |
| 1292 | ast::type::VectorType vec3(&f32, 3); |
| 1293 | |
| 1294 | auto var = std::make_unique<ast::Variable>("my_var", ast::StorageClass::kNone, |
| 1295 | &vec3); |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1296 | mod()->AddGlobalVariable(std::move(var)); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 1297 | |
| 1298 | ast::ExpressionList params; |
| 1299 | params.push_back(std::make_unique<ast::IdentifierExpression>("my_var")); |
| 1300 | |
| 1301 | ast::UnaryMethodExpression exp(op, std::move(params)); |
| 1302 | |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 1303 | // Register the variable |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 1304 | EXPECT_TRUE(td()->Determine()); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 1305 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1306 | EXPECT_TRUE(td()->DetermineResultType(&exp)); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 1307 | ASSERT_NE(exp.result_type(), nullptr); |
| 1308 | ASSERT_TRUE(exp.result_type()->IsVector()); |
| 1309 | EXPECT_TRUE(exp.result_type()->AsVector()->type()->IsBool()); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 1310 | EXPECT_EQ(exp.result_type()->AsVector()->size(), 3u); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 1311 | } |
| 1312 | TEST_P(UnaryMethodExpressionVecTest, Expr_UnaryMethod_Vec) { |
| 1313 | auto op = GetParam(); |
| 1314 | |
| 1315 | ast::type::F32Type f32; |
| 1316 | |
| 1317 | auto var = |
| 1318 | std::make_unique<ast::Variable>("my_var", ast::StorageClass::kNone, &f32); |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1319 | mod()->AddGlobalVariable(std::move(var)); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 1320 | |
| 1321 | ast::ExpressionList params; |
| 1322 | params.push_back(std::make_unique<ast::IdentifierExpression>("my_var")); |
| 1323 | |
| 1324 | ast::UnaryMethodExpression exp(op, std::move(params)); |
| 1325 | |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 1326 | // Register the variable |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 1327 | EXPECT_TRUE(td()->Determine()); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 1328 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1329 | EXPECT_TRUE(td()->DetermineResultType(&exp)); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 1330 | ASSERT_NE(exp.result_type(), nullptr); |
| 1331 | EXPECT_TRUE(exp.result_type()->IsBool()); |
| 1332 | } |
| 1333 | INSTANTIATE_TEST_SUITE_P(TypeDeterminerTest, |
| 1334 | UnaryMethodExpressionVecTest, |
| 1335 | testing::Values(ast::UnaryMethod::kIsInf, |
| 1336 | ast::UnaryMethod::kIsNan, |
| 1337 | ast::UnaryMethod::kIsFinite, |
| 1338 | ast::UnaryMethod::kIsNormal)); |
| 1339 | |
| 1340 | TEST_F(TypeDeterminerTest, Expr_UnaryMethod_Dot) { |
| 1341 | ast::type::F32Type f32; |
| 1342 | ast::type::VectorType vec3(&f32, 3); |
| 1343 | |
| 1344 | auto var = std::make_unique<ast::Variable>("my_var", ast::StorageClass::kNone, |
| 1345 | &vec3); |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1346 | mod()->AddGlobalVariable(std::move(var)); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 1347 | |
| 1348 | ast::ExpressionList params; |
| 1349 | params.push_back(std::make_unique<ast::IdentifierExpression>("my_var")); |
| 1350 | params.push_back(std::make_unique<ast::IdentifierExpression>("my_var")); |
| 1351 | |
| 1352 | ast::UnaryMethodExpression exp(ast::UnaryMethod::kDot, std::move(params)); |
| 1353 | |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 1354 | // Register the variable |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 1355 | EXPECT_TRUE(td()->Determine()); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 1356 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1357 | EXPECT_TRUE(td()->DetermineResultType(&exp)); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 1358 | ASSERT_NE(exp.result_type(), nullptr); |
| 1359 | EXPECT_TRUE(exp.result_type()->IsF32()); |
| 1360 | } |
| 1361 | |
| 1362 | TEST_F(TypeDeterminerTest, Expr_UnaryMethod_OuterProduct) { |
| 1363 | ast::type::F32Type f32; |
| 1364 | ast::type::VectorType vec3(&f32, 3); |
| 1365 | ast::type::VectorType vec2(&f32, 2); |
| 1366 | |
| 1367 | auto var1 = |
| 1368 | std::make_unique<ast::Variable>("v3", ast::StorageClass::kNone, &vec3); |
| 1369 | auto var2 = |
| 1370 | std::make_unique<ast::Variable>("v2", ast::StorageClass::kNone, &vec2); |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1371 | mod()->AddGlobalVariable(std::move(var1)); |
| 1372 | mod()->AddGlobalVariable(std::move(var2)); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 1373 | |
| 1374 | ast::ExpressionList params; |
| 1375 | params.push_back(std::make_unique<ast::IdentifierExpression>("v3")); |
| 1376 | params.push_back(std::make_unique<ast::IdentifierExpression>("v2")); |
| 1377 | |
| 1378 | ast::UnaryMethodExpression exp(ast::UnaryMethod::kOuterProduct, |
| 1379 | std::move(params)); |
| 1380 | |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 1381 | // Register the variable |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 1382 | EXPECT_TRUE(td()->Determine()); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 1383 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1384 | EXPECT_TRUE(td()->DetermineResultType(&exp)); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 1385 | ASSERT_NE(exp.result_type(), nullptr); |
| 1386 | ASSERT_TRUE(exp.result_type()->IsMatrix()); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 1387 | auto* mat = exp.result_type()->AsMatrix(); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 1388 | EXPECT_TRUE(mat->type()->IsF32()); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 1389 | EXPECT_EQ(mat->rows(), 3u); |
| 1390 | EXPECT_EQ(mat->columns(), 2u); |
dan sinclair | 8dcfd10 | 2020-04-07 19:27:00 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1393 | using UnaryOpExpressionTest = TypeDeterminerTestWithParam<ast::UnaryOp>; |
dan sinclair | 0e25762 | 2020-04-07 19:27:11 +0000 | [diff] [blame] | 1394 | TEST_P(UnaryOpExpressionTest, Expr_UnaryOp) { |
| 1395 | auto op = GetParam(); |
| 1396 | |
| 1397 | ast::type::F32Type f32; |
| 1398 | |
| 1399 | ast::type::VectorType vec4(&f32, 4); |
| 1400 | |
| 1401 | auto var = |
| 1402 | std::make_unique<ast::Variable>("ident", ast::StorageClass::kNone, &vec4); |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1403 | mod()->AddGlobalVariable(std::move(var)); |
dan sinclair | 0e25762 | 2020-04-07 19:27:11 +0000 | [diff] [blame] | 1404 | |
| 1405 | // Register the global |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 1406 | EXPECT_TRUE(td()->Determine()); |
dan sinclair | 0e25762 | 2020-04-07 19:27:11 +0000 | [diff] [blame] | 1407 | |
| 1408 | ast::UnaryOpExpression der( |
| 1409 | op, std::make_unique<ast::IdentifierExpression>("ident")); |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1410 | EXPECT_TRUE(td()->DetermineResultType(&der)); |
dan sinclair | 0e25762 | 2020-04-07 19:27:11 +0000 | [diff] [blame] | 1411 | ASSERT_NE(der.result_type(), nullptr); |
| 1412 | ASSERT_TRUE(der.result_type()->IsVector()); |
| 1413 | EXPECT_TRUE(der.result_type()->AsVector()->type()->IsF32()); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 1414 | EXPECT_EQ(der.result_type()->AsVector()->size(), 4u); |
dan sinclair | 0e25762 | 2020-04-07 19:27:11 +0000 | [diff] [blame] | 1415 | } |
| 1416 | INSTANTIATE_TEST_SUITE_P(TypeDeterminerTest, |
| 1417 | UnaryOpExpressionTest, |
| 1418 | testing::Values(ast::UnaryOp::kNegation, |
| 1419 | ast::UnaryOp::kNot)); |
| 1420 | |
dan sinclair | ee8ae04 | 2020-04-08 19:58:20 +0000 | [diff] [blame] | 1421 | TEST_F(TypeDeterminerTest, StorageClass_SetsIfMissing) { |
| 1422 | ast::type::I32Type i32; |
| 1423 | |
| 1424 | auto var = |
| 1425 | std::make_unique<ast::Variable>("var", ast::StorageClass::kNone, &i32); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 1426 | auto* var_ptr = var.get(); |
dan sinclair | ee8ae04 | 2020-04-08 19:58:20 +0000 | [diff] [blame] | 1427 | auto stmt = std::make_unique<ast::VariableDeclStatement>(std::move(var)); |
| 1428 | |
| 1429 | auto func = |
| 1430 | std::make_unique<ast::Function>("func", ast::VariableList{}, &i32); |
| 1431 | ast::StatementList stmts; |
| 1432 | stmts.push_back(std::move(stmt)); |
| 1433 | func->set_body(std::move(stmts)); |
| 1434 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1435 | mod()->AddFunction(std::move(func)); |
dan sinclair | ee8ae04 | 2020-04-08 19:58:20 +0000 | [diff] [blame] | 1436 | |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 1437 | EXPECT_TRUE(td()->Determine()) << td()->error(); |
dan sinclair | ee8ae04 | 2020-04-08 19:58:20 +0000 | [diff] [blame] | 1438 | EXPECT_EQ(var_ptr->storage_class(), ast::StorageClass::kFunction); |
| 1439 | } |
| 1440 | |
| 1441 | TEST_F(TypeDeterminerTest, StorageClass_DoesNotSetOnConst) { |
| 1442 | ast::type::I32Type i32; |
| 1443 | |
| 1444 | auto var = |
| 1445 | std::make_unique<ast::Variable>("var", ast::StorageClass::kNone, &i32); |
| 1446 | var->set_is_const(true); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 1447 | auto* var_ptr = var.get(); |
dan sinclair | ee8ae04 | 2020-04-08 19:58:20 +0000 | [diff] [blame] | 1448 | auto stmt = std::make_unique<ast::VariableDeclStatement>(std::move(var)); |
| 1449 | |
| 1450 | auto func = |
| 1451 | std::make_unique<ast::Function>("func", ast::VariableList{}, &i32); |
| 1452 | ast::StatementList stmts; |
| 1453 | stmts.push_back(std::move(stmt)); |
| 1454 | func->set_body(std::move(stmts)); |
| 1455 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1456 | mod()->AddFunction(std::move(func)); |
dan sinclair | ee8ae04 | 2020-04-08 19:58:20 +0000 | [diff] [blame] | 1457 | |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 1458 | EXPECT_TRUE(td()->Determine()) << td()->error(); |
dan sinclair | ee8ae04 | 2020-04-08 19:58:20 +0000 | [diff] [blame] | 1459 | EXPECT_EQ(var_ptr->storage_class(), ast::StorageClass::kNone); |
| 1460 | } |
| 1461 | |
| 1462 | TEST_F(TypeDeterminerTest, StorageClass_NonFunctionClassError) { |
| 1463 | ast::type::I32Type i32; |
| 1464 | |
| 1465 | auto var = std::make_unique<ast::Variable>( |
| 1466 | "var", ast::StorageClass::kWorkgroup, &i32); |
| 1467 | auto stmt = std::make_unique<ast::VariableDeclStatement>(std::move(var)); |
| 1468 | |
| 1469 | auto func = |
| 1470 | std::make_unique<ast::Function>("func", ast::VariableList{}, &i32); |
| 1471 | ast::StatementList stmts; |
| 1472 | stmts.push_back(std::move(stmt)); |
| 1473 | func->set_body(std::move(stmts)); |
| 1474 | |
dan sinclair | cd077b0 | 2020-04-20 14:19:04 +0000 | [diff] [blame] | 1475 | mod()->AddFunction(std::move(func)); |
dan sinclair | ee8ae04 | 2020-04-08 19:58:20 +0000 | [diff] [blame] | 1476 | |
dan sinclair | b950e80 | 2020-04-20 14:20:01 +0000 | [diff] [blame] | 1477 | EXPECT_FALSE(td()->Determine()); |
dan sinclair | ee8ae04 | 2020-04-08 19:58:20 +0000 | [diff] [blame] | 1478 | EXPECT_EQ(td()->error(), |
| 1479 | "function variable has a non-function storage class"); |
| 1480 | } |
| 1481 | |
dan sinclair | ca1723e | 2020-04-20 15:47:55 +0000 | [diff] [blame] | 1482 | struct GLSLData { |
| 1483 | const char* name; |
| 1484 | uint32_t value; |
| 1485 | }; |
| 1486 | inline std::ostream& operator<<(std::ostream& out, GLSLData data) { |
| 1487 | out << data.name; |
| 1488 | return out; |
| 1489 | } |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 1490 | using ImportData_SingleParamTest = TypeDeterminerTestWithParam<GLSLData>; |
dan sinclair | ca1723e | 2020-04-20 15:47:55 +0000 | [diff] [blame] | 1491 | |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 1492 | TEST_P(ImportData_SingleParamTest, Scalar) { |
dan sinclair | ca1723e | 2020-04-20 15:47:55 +0000 | [diff] [blame] | 1493 | auto param = GetParam(); |
| 1494 | |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 1495 | ast::type::F32Type f32; |
| 1496 | |
| 1497 | ast::ExpressionList params; |
| 1498 | params.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 1499 | std::make_unique<ast::FloatLiteral>(&f32, 1.f))); |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 1500 | |
| 1501 | ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error(); |
| 1502 | |
| 1503 | uint32_t id = 0; |
dan sinclair | ca1723e | 2020-04-20 15:47:55 +0000 | [diff] [blame] | 1504 | auto* type = td()->GetImportData("GLSL.std.450", param.name, params, &id); |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 1505 | ASSERT_NE(type, nullptr); |
| 1506 | EXPECT_TRUE(type->is_float_scalar()); |
dan sinclair | ca1723e | 2020-04-20 15:47:55 +0000 | [diff] [blame] | 1507 | EXPECT_EQ(id, param.value); |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 1508 | } |
| 1509 | |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 1510 | TEST_P(ImportData_SingleParamTest, Vector) { |
dan sinclair | ca1723e | 2020-04-20 15:47:55 +0000 | [diff] [blame] | 1511 | auto param = GetParam(); |
| 1512 | |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 1513 | ast::type::F32Type f32; |
| 1514 | ast::type::VectorType vec(&f32, 3); |
| 1515 | |
| 1516 | ast::ExpressionList vals; |
| 1517 | vals.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1518 | std::make_unique<ast::FloatLiteral>(&f32, 1.0f))); |
| 1519 | vals.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1520 | std::make_unique<ast::FloatLiteral>(&f32, 1.0f))); |
| 1521 | vals.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1522 | std::make_unique<ast::FloatLiteral>(&f32, 3.0f))); |
| 1523 | |
| 1524 | ast::ExpressionList params; |
| 1525 | params.push_back( |
| 1526 | std::make_unique<ast::TypeConstructorExpression>(&vec, std::move(vals))); |
| 1527 | |
| 1528 | ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error(); |
| 1529 | |
| 1530 | uint32_t id = 0; |
dan sinclair | ca1723e | 2020-04-20 15:47:55 +0000 | [diff] [blame] | 1531 | auto* type = td()->GetImportData("GLSL.std.450", param.name, params, &id); |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 1532 | ASSERT_NE(type, nullptr); |
| 1533 | EXPECT_TRUE(type->is_float_vector()); |
| 1534 | EXPECT_EQ(type->AsVector()->size(), 3); |
dan sinclair | ca1723e | 2020-04-20 15:47:55 +0000 | [diff] [blame] | 1535 | EXPECT_EQ(id, param.value); |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 1536 | } |
| 1537 | |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 1538 | TEST_P(ImportData_SingleParamTest, Error_Integer) { |
dan sinclair | ca1723e | 2020-04-20 15:47:55 +0000 | [diff] [blame] | 1539 | auto param = GetParam(); |
| 1540 | |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 1541 | ast::type::I32Type i32; |
| 1542 | |
| 1543 | ast::ExpressionList params; |
| 1544 | params.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1545 | std::make_unique<ast::IntLiteral>(&i32, 1))); |
| 1546 | |
| 1547 | ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error(); |
| 1548 | |
| 1549 | uint32_t id = 0; |
dan sinclair | ca1723e | 2020-04-20 15:47:55 +0000 | [diff] [blame] | 1550 | auto* type = td()->GetImportData("GLSL.std.450", param.name, params, &id); |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 1551 | ASSERT_EQ(type, nullptr); |
dan sinclair | ca1723e | 2020-04-20 15:47:55 +0000 | [diff] [blame] | 1552 | EXPECT_EQ(td()->error(), std::string("incorrect type for ") + param.name + |
| 1553 | ". Requires a float scalar or a float vector"); |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 1554 | } |
| 1555 | |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 1556 | TEST_P(ImportData_SingleParamTest, Error_NoParams) { |
dan sinclair | ca1723e | 2020-04-20 15:47:55 +0000 | [diff] [blame] | 1557 | auto param = GetParam(); |
| 1558 | |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 1559 | ast::ExpressionList params; |
| 1560 | uint32_t id = 0; |
dan sinclair | ca1723e | 2020-04-20 15:47:55 +0000 | [diff] [blame] | 1561 | auto* type = td()->GetImportData("GLSL.std.450", param.name, params, &id); |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 1562 | ASSERT_EQ(type, nullptr); |
dan sinclair | ca1723e | 2020-04-20 15:47:55 +0000 | [diff] [blame] | 1563 | EXPECT_EQ(td()->error(), std::string("incorrect number of parameters for ") + |
| 1564 | param.name + ". Expected 1 got 0"); |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 1565 | } |
| 1566 | |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 1567 | TEST_P(ImportData_SingleParamTest, Error_MultipleParams) { |
dan sinclair | ca1723e | 2020-04-20 15:47:55 +0000 | [diff] [blame] | 1568 | auto param = GetParam(); |
| 1569 | |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 1570 | ast::type::F32Type f32; |
| 1571 | ast::ExpressionList params; |
| 1572 | params.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1573 | std::make_unique<ast::FloatLiteral>(&f32, 1.f))); |
| 1574 | params.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1575 | std::make_unique<ast::FloatLiteral>(&f32, 1.f))); |
| 1576 | params.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1577 | std::make_unique<ast::FloatLiteral>(&f32, 1.f))); |
| 1578 | |
| 1579 | ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error(); |
| 1580 | |
| 1581 | uint32_t id = 0; |
dan sinclair | ca1723e | 2020-04-20 15:47:55 +0000 | [diff] [blame] | 1582 | auto* type = td()->GetImportData("GLSL.std.450", param.name, params, &id); |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 1583 | ASSERT_EQ(type, nullptr); |
dan sinclair | ca1723e | 2020-04-20 15:47:55 +0000 | [diff] [blame] | 1584 | EXPECT_EQ(td()->error(), std::string("incorrect number of parameters for ") + |
| 1585 | param.name + ". Expected 1 got 3"); |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
dan sinclair | a49328f | 2020-04-20 15:49:50 +0000 | [diff] [blame] | 1588 | INSTANTIATE_TEST_SUITE_P( |
| 1589 | TypeDeterminerTest, |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 1590 | ImportData_SingleParamTest, |
dan sinclair | a49328f | 2020-04-20 15:49:50 +0000 | [diff] [blame] | 1591 | testing::Values(GLSLData{"round", GLSLstd450Round}, |
| 1592 | GLSLData{"roundeven", GLSLstd450RoundEven}, |
| 1593 | GLSLData{"trunc", GLSLstd450Trunc}, |
| 1594 | GLSLData{"fabs", GLSLstd450FAbs}, |
| 1595 | GLSLData{"fsign", GLSLstd450FSign}, |
| 1596 | GLSLData{"floor", GLSLstd450Floor}, |
| 1597 | GLSLData{"ceil", GLSLstd450Ceil}, |
| 1598 | GLSLData{"fract", GLSLstd450Fract}, |
| 1599 | GLSLData{"radians", GLSLstd450Radians}, |
| 1600 | GLSLData{"degrees", GLSLstd450Degrees}, |
| 1601 | GLSLData{"sin", GLSLstd450Sin}, |
| 1602 | GLSLData{"cos", GLSLstd450Cos}, |
| 1603 | GLSLData{"tan", GLSLstd450Tan}, |
| 1604 | GLSLData{"asin", GLSLstd450Asin}, |
| 1605 | GLSLData{"acos", GLSLstd450Acos}, |
| 1606 | GLSLData{"atan", GLSLstd450Atan}, |
| 1607 | GLSLData{"sinh", GLSLstd450Sinh}, |
| 1608 | GLSLData{"cosh", GLSLstd450Cosh}, |
| 1609 | GLSLData{"tanh", GLSLstd450Tanh}, |
| 1610 | GLSLData{"asinh", GLSLstd450Asinh}, |
| 1611 | GLSLData{"acosh", GLSLstd450Acosh}, |
| 1612 | GLSLData{"atanh", GLSLstd450Atanh}, |
| 1613 | GLSLData{"exp", GLSLstd450Exp}, |
| 1614 | GLSLData{"log", GLSLstd450Log}, |
| 1615 | GLSLData{"exp2", GLSLstd450Exp2}, |
| 1616 | GLSLData{"log2", GLSLstd450Log2}, |
| 1617 | GLSLData{"sqrt", GLSLstd450Sqrt}, |
dan sinclair | 3df2044 | 2020-04-20 15:51:18 +0000 | [diff] [blame] | 1618 | GLSLData{"inversesqrt", GLSLstd450InverseSqrt}, |
| 1619 | GLSLData{"normalize", GLSLstd450Normalize})); |
dan sinclair | ca1723e | 2020-04-20 15:47:55 +0000 | [diff] [blame] | 1620 | |
dan sinclair | 652a4b9 | 2020-04-20 21:09:14 +0000 | [diff] [blame] | 1621 | TEST_F(TypeDeterminerTest, ImportData_Length_Scalar) { |
| 1622 | ast::type::F32Type f32; |
| 1623 | |
| 1624 | ast::ExpressionList params; |
| 1625 | params.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 1626 | std::make_unique<ast::FloatLiteral>(&f32, 1.f))); |
dan sinclair | 652a4b9 | 2020-04-20 21:09:14 +0000 | [diff] [blame] | 1627 | |
| 1628 | ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error(); |
| 1629 | |
| 1630 | uint32_t id = 0; |
| 1631 | auto* type = td()->GetImportData("GLSL.std.450", "length", params, &id); |
| 1632 | ASSERT_NE(type, nullptr); |
| 1633 | EXPECT_TRUE(type->is_float_scalar()); |
| 1634 | EXPECT_EQ(id, GLSLstd450Length); |
| 1635 | } |
| 1636 | |
| 1637 | TEST_F(TypeDeterminerTest, ImportData_Length_FloatVector) { |
| 1638 | ast::type::F32Type f32; |
| 1639 | ast::type::VectorType vec(&f32, 3); |
| 1640 | |
| 1641 | ast::ExpressionList vals; |
| 1642 | vals.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1643 | std::make_unique<ast::FloatLiteral>(&f32, 1.0f))); |
| 1644 | vals.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1645 | std::make_unique<ast::FloatLiteral>(&f32, 1.0f))); |
| 1646 | vals.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1647 | std::make_unique<ast::FloatLiteral>(&f32, 3.0f))); |
| 1648 | |
| 1649 | ast::ExpressionList params; |
| 1650 | params.push_back( |
| 1651 | std::make_unique<ast::TypeConstructorExpression>(&vec, std::move(vals))); |
| 1652 | |
| 1653 | ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error(); |
| 1654 | |
| 1655 | uint32_t id = 0; |
| 1656 | auto* type = td()->GetImportData("GLSL.std.450", "length", params, &id); |
| 1657 | ASSERT_NE(type, nullptr); |
| 1658 | EXPECT_TRUE(type->is_float_scalar()); |
| 1659 | EXPECT_EQ(id, GLSLstd450Length); |
| 1660 | } |
| 1661 | |
| 1662 | TEST_F(TypeDeterminerTest, ImportData_Length_Error_Integer) { |
| 1663 | ast::type::I32Type i32; |
| 1664 | |
| 1665 | ast::ExpressionList params; |
| 1666 | params.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1667 | std::make_unique<ast::IntLiteral>(&i32, 1))); |
| 1668 | |
| 1669 | ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error(); |
| 1670 | |
| 1671 | uint32_t id = 0; |
| 1672 | auto* type = td()->GetImportData("GLSL.std.450", "length", params, &id); |
| 1673 | ASSERT_EQ(type, nullptr); |
| 1674 | EXPECT_EQ( |
| 1675 | td()->error(), |
| 1676 | "incorrect type for length. Requires a float scalar or a float vector"); |
| 1677 | } |
| 1678 | |
| 1679 | TEST_F(TypeDeterminerTest, ImportData_Length_Error_NoParams) { |
| 1680 | ast::ExpressionList params; |
| 1681 | uint32_t id = 0; |
| 1682 | auto* type = td()->GetImportData("GLSL.std.450", "length", params, &id); |
| 1683 | ASSERT_EQ(type, nullptr); |
| 1684 | EXPECT_EQ(td()->error(), |
| 1685 | "incorrect number of parameters for length. Expected 1 got 0"); |
| 1686 | } |
| 1687 | |
| 1688 | TEST_F(TypeDeterminerTest, ImportData_Length_Error_MultipleParams) { |
| 1689 | ast::type::F32Type f32; |
| 1690 | ast::ExpressionList params; |
| 1691 | params.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1692 | std::make_unique<ast::FloatLiteral>(&f32, 1.f))); |
| 1693 | params.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1694 | std::make_unique<ast::FloatLiteral>(&f32, 1.f))); |
| 1695 | params.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1696 | std::make_unique<ast::FloatLiteral>(&f32, 1.f))); |
| 1697 | |
| 1698 | ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error(); |
| 1699 | |
| 1700 | uint32_t id = 0; |
| 1701 | auto* type = td()->GetImportData("GLSL.std.450", "length", params, &id); |
| 1702 | ASSERT_EQ(type, nullptr); |
| 1703 | EXPECT_EQ(td()->error(), |
| 1704 | "incorrect number of parameters for length. Expected 1 got 3"); |
| 1705 | } |
| 1706 | |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 1707 | using ImportData_TwoParamTest = TypeDeterminerTestWithParam<GLSLData>; |
| 1708 | |
| 1709 | TEST_P(ImportData_TwoParamTest, Scalar) { |
| 1710 | auto param = GetParam(); |
| 1711 | |
| 1712 | ast::type::F32Type f32; |
| 1713 | |
| 1714 | ast::ExpressionList params; |
| 1715 | params.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1716 | std::make_unique<ast::FloatLiteral>(&f32, 1.f))); |
| 1717 | params.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1718 | std::make_unique<ast::FloatLiteral>(&f32, 1.f))); |
| 1719 | |
| 1720 | ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error(); |
| 1721 | |
| 1722 | uint32_t id = 0; |
| 1723 | auto* type = td()->GetImportData("GLSL.std.450", param.name, params, &id); |
| 1724 | ASSERT_NE(type, nullptr); |
| 1725 | EXPECT_TRUE(type->is_float_scalar()); |
| 1726 | EXPECT_EQ(id, param.value); |
| 1727 | } |
| 1728 | |
| 1729 | TEST_P(ImportData_TwoParamTest, Vector) { |
| 1730 | auto param = GetParam(); |
| 1731 | |
| 1732 | ast::type::F32Type f32; |
| 1733 | ast::type::VectorType vec(&f32, 3); |
| 1734 | |
| 1735 | ast::ExpressionList vals_1; |
| 1736 | vals_1.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1737 | std::make_unique<ast::FloatLiteral>(&f32, 1.0f))); |
| 1738 | vals_1.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1739 | std::make_unique<ast::FloatLiteral>(&f32, 1.0f))); |
| 1740 | vals_1.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1741 | std::make_unique<ast::FloatLiteral>(&f32, 3.0f))); |
| 1742 | |
| 1743 | ast::ExpressionList vals_2; |
| 1744 | vals_2.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1745 | std::make_unique<ast::FloatLiteral>(&f32, 1.0f))); |
| 1746 | vals_2.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1747 | std::make_unique<ast::FloatLiteral>(&f32, 1.0f))); |
| 1748 | vals_2.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1749 | std::make_unique<ast::FloatLiteral>(&f32, 3.0f))); |
| 1750 | |
| 1751 | ast::ExpressionList params; |
| 1752 | params.push_back(std::make_unique<ast::TypeConstructorExpression>( |
| 1753 | &vec, std::move(vals_1))); |
| 1754 | params.push_back(std::make_unique<ast::TypeConstructorExpression>( |
| 1755 | &vec, std::move(vals_2))); |
| 1756 | |
| 1757 | ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error(); |
| 1758 | |
| 1759 | uint32_t id = 0; |
| 1760 | auto* type = td()->GetImportData("GLSL.std.450", param.name, params, &id); |
| 1761 | ASSERT_NE(type, nullptr); |
| 1762 | EXPECT_TRUE(type->is_float_vector()); |
| 1763 | EXPECT_EQ(type->AsVector()->size(), 3); |
| 1764 | EXPECT_EQ(id, param.value); |
| 1765 | } |
| 1766 | |
| 1767 | TEST_P(ImportData_TwoParamTest, Error_Integer) { |
| 1768 | auto param = GetParam(); |
| 1769 | |
| 1770 | ast::type::I32Type i32; |
| 1771 | |
| 1772 | ast::ExpressionList params; |
| 1773 | params.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1774 | std::make_unique<ast::IntLiteral>(&i32, 1))); |
| 1775 | params.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1776 | std::make_unique<ast::IntLiteral>(&i32, 2))); |
| 1777 | |
| 1778 | ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error(); |
| 1779 | |
| 1780 | uint32_t id = 0; |
| 1781 | auto* type = td()->GetImportData("GLSL.std.450", param.name, params, &id); |
| 1782 | ASSERT_EQ(type, nullptr); |
| 1783 | EXPECT_EQ(td()->error(), |
| 1784 | std::string("incorrect type for ") + param.name + |
| 1785 | ". Requires float scalar or a float vector values"); |
| 1786 | } |
| 1787 | |
| 1788 | TEST_P(ImportData_TwoParamTest, Error_NoParams) { |
| 1789 | auto param = GetParam(); |
| 1790 | |
| 1791 | ast::ExpressionList params; |
| 1792 | uint32_t id = 0; |
| 1793 | auto* type = td()->GetImportData("GLSL.std.450", param.name, params, &id); |
| 1794 | ASSERT_EQ(type, nullptr); |
| 1795 | EXPECT_EQ(td()->error(), std::string("incorrect number of parameters for ") + |
| 1796 | param.name + ". Expected 2 got 0"); |
| 1797 | } |
| 1798 | |
| 1799 | TEST_P(ImportData_TwoParamTest, Error_OneParam) { |
| 1800 | auto param = GetParam(); |
| 1801 | |
| 1802 | ast::type::F32Type f32; |
| 1803 | ast::ExpressionList params; |
| 1804 | params.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1805 | std::make_unique<ast::FloatLiteral>(&f32, 1.f))); |
| 1806 | |
| 1807 | ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error(); |
| 1808 | |
| 1809 | uint32_t id = 0; |
| 1810 | auto* type = td()->GetImportData("GLSL.std.450", param.name, params, &id); |
| 1811 | ASSERT_EQ(type, nullptr); |
| 1812 | EXPECT_EQ(td()->error(), std::string("incorrect number of parameters for ") + |
| 1813 | param.name + ". Expected 2 got 1"); |
| 1814 | } |
| 1815 | |
| 1816 | TEST_P(ImportData_TwoParamTest, Error_MismatchedParamCount) { |
| 1817 | auto param = GetParam(); |
| 1818 | |
| 1819 | ast::type::F32Type f32; |
| 1820 | ast::type::VectorType vec2(&f32, 2); |
| 1821 | ast::type::VectorType vec3(&f32, 3); |
| 1822 | |
| 1823 | ast::ExpressionList vals_1; |
| 1824 | vals_1.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1825 | std::make_unique<ast::FloatLiteral>(&f32, 1.0f))); |
| 1826 | vals_1.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1827 | std::make_unique<ast::FloatLiteral>(&f32, 1.0f))); |
| 1828 | |
| 1829 | ast::ExpressionList vals_2; |
| 1830 | vals_2.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1831 | std::make_unique<ast::FloatLiteral>(&f32, 1.0f))); |
| 1832 | vals_2.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1833 | std::make_unique<ast::FloatLiteral>(&f32, 1.0f))); |
| 1834 | vals_2.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1835 | std::make_unique<ast::FloatLiteral>(&f32, 3.0f))); |
| 1836 | |
| 1837 | ast::ExpressionList params; |
| 1838 | params.push_back(std::make_unique<ast::TypeConstructorExpression>( |
| 1839 | &vec2, std::move(vals_1))); |
| 1840 | params.push_back(std::make_unique<ast::TypeConstructorExpression>( |
| 1841 | &vec3, std::move(vals_2))); |
| 1842 | |
| 1843 | ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error(); |
| 1844 | |
| 1845 | uint32_t id = 0; |
| 1846 | auto* type = td()->GetImportData("GLSL.std.450", param.name, params, &id); |
| 1847 | ASSERT_EQ(type, nullptr); |
| 1848 | EXPECT_EQ(td()->error(), |
| 1849 | std::string("mismatched parameter types for ") + param.name); |
| 1850 | } |
| 1851 | |
| 1852 | TEST_P(ImportData_TwoParamTest, Error_MismatchedParamType) { |
| 1853 | auto param = GetParam(); |
| 1854 | |
| 1855 | ast::type::F32Type f32; |
| 1856 | ast::type::VectorType vec(&f32, 3); |
| 1857 | |
| 1858 | ast::ExpressionList vals; |
| 1859 | vals.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1860 | std::make_unique<ast::FloatLiteral>(&f32, 1.0f))); |
| 1861 | vals.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1862 | std::make_unique<ast::FloatLiteral>(&f32, 1.0f))); |
| 1863 | vals.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1864 | std::make_unique<ast::FloatLiteral>(&f32, 3.0f))); |
| 1865 | |
| 1866 | ast::ExpressionList params; |
| 1867 | params.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1868 | std::make_unique<ast::FloatLiteral>(&f32, 1.0f))); |
| 1869 | params.push_back( |
| 1870 | std::make_unique<ast::TypeConstructorExpression>(&vec, std::move(vals))); |
| 1871 | |
| 1872 | ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error(); |
| 1873 | |
| 1874 | uint32_t id = 0; |
| 1875 | auto* type = td()->GetImportData("GLSL.std.450", param.name, params, &id); |
| 1876 | ASSERT_EQ(type, nullptr); |
| 1877 | EXPECT_EQ(td()->error(), |
| 1878 | std::string("mismatched parameter types for ") + param.name); |
| 1879 | } |
| 1880 | |
| 1881 | TEST_P(ImportData_TwoParamTest, Error_TooManyParams) { |
| 1882 | auto param = GetParam(); |
| 1883 | |
| 1884 | ast::type::F32Type f32; |
| 1885 | ast::ExpressionList params; |
| 1886 | params.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1887 | std::make_unique<ast::FloatLiteral>(&f32, 1.f))); |
| 1888 | params.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1889 | std::make_unique<ast::FloatLiteral>(&f32, 1.f))); |
| 1890 | params.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1891 | std::make_unique<ast::FloatLiteral>(&f32, 1.f))); |
| 1892 | |
| 1893 | ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error(); |
| 1894 | |
| 1895 | uint32_t id = 0; |
| 1896 | auto* type = td()->GetImportData("GLSL.std.450", param.name, params, &id); |
| 1897 | ASSERT_EQ(type, nullptr); |
| 1898 | EXPECT_EQ(td()->error(), std::string("incorrect number of parameters for ") + |
| 1899 | param.name + ". Expected 2 got 3"); |
| 1900 | } |
| 1901 | |
| 1902 | INSTANTIATE_TEST_SUITE_P(TypeDeterminerTest, |
| 1903 | ImportData_TwoParamTest, |
dan sinclair | 2ee4a7e | 2020-04-21 12:58:35 +0000 | [diff] [blame] | 1904 | testing::Values(GLSLData{"atan2", GLSLstd450Atan2}, |
| 1905 | GLSLData{"pow", GLSLstd450Pow}, |
| 1906 | GLSLData{"fmin", GLSLstd450FMin}, |
| 1907 | GLSLData{"fmax", GLSLstd450FMax}, |
| 1908 | GLSLData{"step", GLSLstd450Step}, |
| 1909 | GLSLData{"reflect", GLSLstd450Reflect}, |
| 1910 | GLSLData{"nmin", GLSLstd450NMin}, |
| 1911 | GLSLData{"nmax", GLSLstd450NMax})); |
dan sinclair | 37d62c9 | 2020-04-21 12:55:06 +0000 | [diff] [blame] | 1912 | |
dan sinclair | 5444438 | 2020-04-21 13:04:15 +0000 | [diff] [blame^] | 1913 | TEST_F(TypeDeterminerTest, ImportData_Distance_Scalar) { |
| 1914 | ast::type::F32Type f32; |
| 1915 | |
| 1916 | ast::ExpressionList params; |
| 1917 | params.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1918 | std::make_unique<ast::FloatLiteral>(&f32, 1.f))); |
| 1919 | params.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1920 | std::make_unique<ast::FloatLiteral>(&f32, 1.f))); |
| 1921 | |
| 1922 | ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error(); |
| 1923 | |
| 1924 | uint32_t id = 0; |
| 1925 | auto* type = td()->GetImportData("GLSL.std.450", "distance", params, &id); |
| 1926 | ASSERT_NE(type, nullptr); |
| 1927 | EXPECT_TRUE(type->is_float_scalar()); |
| 1928 | EXPECT_EQ(id, GLSLstd450Distance); |
| 1929 | } |
| 1930 | |
| 1931 | TEST_F(TypeDeterminerTest, ImportData_Distance_Vector) { |
| 1932 | ast::type::F32Type f32; |
| 1933 | ast::type::VectorType vec(&f32, 3); |
| 1934 | |
| 1935 | ast::ExpressionList vals_1; |
| 1936 | vals_1.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1937 | std::make_unique<ast::FloatLiteral>(&f32, 1.0f))); |
| 1938 | vals_1.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1939 | std::make_unique<ast::FloatLiteral>(&f32, 1.0f))); |
| 1940 | vals_1.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1941 | std::make_unique<ast::FloatLiteral>(&f32, 3.0f))); |
| 1942 | |
| 1943 | ast::ExpressionList vals_2; |
| 1944 | vals_2.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1945 | std::make_unique<ast::FloatLiteral>(&f32, 1.0f))); |
| 1946 | vals_2.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1947 | std::make_unique<ast::FloatLiteral>(&f32, 1.0f))); |
| 1948 | vals_2.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1949 | std::make_unique<ast::FloatLiteral>(&f32, 3.0f))); |
| 1950 | |
| 1951 | ast::ExpressionList params; |
| 1952 | params.push_back(std::make_unique<ast::TypeConstructorExpression>( |
| 1953 | &vec, std::move(vals_1))); |
| 1954 | params.push_back(std::make_unique<ast::TypeConstructorExpression>( |
| 1955 | &vec, std::move(vals_2))); |
| 1956 | |
| 1957 | ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error(); |
| 1958 | |
| 1959 | uint32_t id = 0; |
| 1960 | auto* type = td()->GetImportData("GLSL.std.450", "distance", params, &id); |
| 1961 | ASSERT_NE(type, nullptr); |
| 1962 | EXPECT_TRUE(type->IsF32()); |
| 1963 | EXPECT_EQ(id, GLSLstd450Distance); |
| 1964 | } |
| 1965 | |
| 1966 | TEST_F(TypeDeterminerTest, ImportData_Distance_Error_Integer) { |
| 1967 | ast::type::I32Type i32; |
| 1968 | |
| 1969 | ast::ExpressionList params; |
| 1970 | params.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1971 | std::make_unique<ast::IntLiteral>(&i32, 1))); |
| 1972 | params.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1973 | std::make_unique<ast::IntLiteral>(&i32, 2))); |
| 1974 | |
| 1975 | ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error(); |
| 1976 | |
| 1977 | uint32_t id = 0; |
| 1978 | auto* type = td()->GetImportData("GLSL.std.450", "distance", params, &id); |
| 1979 | ASSERT_EQ(type, nullptr); |
| 1980 | EXPECT_EQ(td()->error(), |
| 1981 | "incorrect type for distance. Requires float scalar or a float " |
| 1982 | "vector values"); |
| 1983 | } |
| 1984 | |
| 1985 | TEST_F(TypeDeterminerTest, ImportData_Distance_Error_NoParams) { |
| 1986 | ast::ExpressionList params; |
| 1987 | uint32_t id = 0; |
| 1988 | auto* type = td()->GetImportData("GLSL.std.450", "distance", params, &id); |
| 1989 | ASSERT_EQ(type, nullptr); |
| 1990 | EXPECT_EQ(td()->error(), |
| 1991 | "incorrect number of parameters for distance. Expected 2 got 0"); |
| 1992 | } |
| 1993 | |
| 1994 | TEST_F(TypeDeterminerTest, ImportData_Distance_Error_OneParam) { |
| 1995 | ast::type::F32Type f32; |
| 1996 | ast::ExpressionList params; |
| 1997 | params.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 1998 | std::make_unique<ast::FloatLiteral>(&f32, 1.f))); |
| 1999 | |
| 2000 | ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error(); |
| 2001 | |
| 2002 | uint32_t id = 0; |
| 2003 | auto* type = td()->GetImportData("GLSL.std.450", "distance", params, &id); |
| 2004 | ASSERT_EQ(type, nullptr); |
| 2005 | EXPECT_EQ(td()->error(), |
| 2006 | "incorrect number of parameters for distance. Expected 2 got 1"); |
| 2007 | } |
| 2008 | |
| 2009 | TEST_F(TypeDeterminerTest, ImportData_Distance_Error_MismatchedParamCount) { |
| 2010 | ast::type::F32Type f32; |
| 2011 | ast::type::VectorType vec2(&f32, 2); |
| 2012 | ast::type::VectorType vec3(&f32, 3); |
| 2013 | |
| 2014 | ast::ExpressionList vals_1; |
| 2015 | vals_1.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 2016 | std::make_unique<ast::FloatLiteral>(&f32, 1.0f))); |
| 2017 | vals_1.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 2018 | std::make_unique<ast::FloatLiteral>(&f32, 1.0f))); |
| 2019 | |
| 2020 | ast::ExpressionList vals_2; |
| 2021 | vals_2.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 2022 | std::make_unique<ast::FloatLiteral>(&f32, 1.0f))); |
| 2023 | vals_2.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 2024 | std::make_unique<ast::FloatLiteral>(&f32, 1.0f))); |
| 2025 | vals_2.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 2026 | std::make_unique<ast::FloatLiteral>(&f32, 3.0f))); |
| 2027 | |
| 2028 | ast::ExpressionList params; |
| 2029 | params.push_back(std::make_unique<ast::TypeConstructorExpression>( |
| 2030 | &vec2, std::move(vals_1))); |
| 2031 | params.push_back(std::make_unique<ast::TypeConstructorExpression>( |
| 2032 | &vec3, std::move(vals_2))); |
| 2033 | |
| 2034 | ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error(); |
| 2035 | |
| 2036 | uint32_t id = 0; |
| 2037 | auto* type = td()->GetImportData("GLSL.std.450", "distance", params, &id); |
| 2038 | ASSERT_EQ(type, nullptr); |
| 2039 | EXPECT_EQ(td()->error(), "mismatched parameter types for distance"); |
| 2040 | } |
| 2041 | |
| 2042 | TEST_F(TypeDeterminerTest, ImportData_Distance_Error_MismatchedParamType) { |
| 2043 | ast::type::F32Type f32; |
| 2044 | ast::type::VectorType vec(&f32, 3); |
| 2045 | |
| 2046 | ast::ExpressionList vals; |
| 2047 | vals.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 2048 | std::make_unique<ast::FloatLiteral>(&f32, 1.0f))); |
| 2049 | vals.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 2050 | std::make_unique<ast::FloatLiteral>(&f32, 1.0f))); |
| 2051 | vals.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 2052 | std::make_unique<ast::FloatLiteral>(&f32, 3.0f))); |
| 2053 | |
| 2054 | ast::ExpressionList params; |
| 2055 | params.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 2056 | std::make_unique<ast::FloatLiteral>(&f32, 1.0f))); |
| 2057 | params.push_back( |
| 2058 | std::make_unique<ast::TypeConstructorExpression>(&vec, std::move(vals))); |
| 2059 | |
| 2060 | ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error(); |
| 2061 | |
| 2062 | uint32_t id = 0; |
| 2063 | auto* type = td()->GetImportData("GLSL.std.450", "distance", params, &id); |
| 2064 | ASSERT_EQ(type, nullptr); |
| 2065 | EXPECT_EQ(td()->error(), "mismatched parameter types for distance"); |
| 2066 | } |
| 2067 | |
| 2068 | TEST_F(TypeDeterminerTest, ImportData_Distance_Error_TooManyParams) { |
| 2069 | ast::type::F32Type f32; |
| 2070 | ast::ExpressionList params; |
| 2071 | params.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 2072 | std::make_unique<ast::FloatLiteral>(&f32, 1.f))); |
| 2073 | params.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 2074 | std::make_unique<ast::FloatLiteral>(&f32, 1.f))); |
| 2075 | params.push_back(std::make_unique<ast::ScalarConstructorExpression>( |
| 2076 | std::make_unique<ast::FloatLiteral>(&f32, 1.f))); |
| 2077 | |
| 2078 | ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error(); |
| 2079 | |
| 2080 | uint32_t id = 0; |
| 2081 | auto* type = td()->GetImportData("GLSL.std.450", "distance", params, &id); |
| 2082 | ASSERT_EQ(type, nullptr); |
| 2083 | EXPECT_EQ(td()->error(), |
| 2084 | "incorrect number of parameters for distance. Expected 2 got 3"); |
| 2085 | } |
| 2086 | |
dan sinclair | b7edc4c | 2020-04-07 12:46:30 +0000 | [diff] [blame] | 2087 | } // namespace |
| 2088 | } // namespace tint |