2020-07-22 12:59:19 +00:00
|
|
|
// Copyright 2020 The Tint Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include "spirv/unified1/GLSL.std.450.h"
|
|
|
|
#include "src/ast/array_accessor_expression.h"
|
|
|
|
#include "src/ast/assignment_statement.h"
|
|
|
|
#include "src/ast/binary_expression.h"
|
2020-08-06 21:24:14 +00:00
|
|
|
#include "src/ast/bool_literal.h"
|
2020-07-22 12:59:19 +00:00
|
|
|
#include "src/ast/break_statement.h"
|
|
|
|
#include "src/ast/call_expression.h"
|
2020-08-17 21:01:28 +00:00
|
|
|
#include "src/ast/call_statement.h"
|
2020-07-22 12:59:19 +00:00
|
|
|
#include "src/ast/case_statement.h"
|
|
|
|
#include "src/ast/continue_statement.h"
|
|
|
|
#include "src/ast/else_statement.h"
|
|
|
|
#include "src/ast/float_literal.h"
|
|
|
|
#include "src/ast/identifier_expression.h"
|
|
|
|
#include "src/ast/if_statement.h"
|
|
|
|
#include "src/ast/int_literal.h"
|
|
|
|
#include "src/ast/loop_statement.h"
|
|
|
|
#include "src/ast/member_accessor_expression.h"
|
2020-09-21 17:56:41 +00:00
|
|
|
#include "src/ast/pipeline_stage.h"
|
2020-07-22 12:59:19 +00:00
|
|
|
#include "src/ast/return_statement.h"
|
|
|
|
#include "src/ast/scalar_constructor_expression.h"
|
|
|
|
#include "src/ast/sint_literal.h"
|
2020-09-21 17:56:41 +00:00
|
|
|
#include "src/ast/stage_decoration.h"
|
2020-07-22 12:59:19 +00:00
|
|
|
#include "src/ast/struct.h"
|
|
|
|
#include "src/ast/struct_member.h"
|
|
|
|
#include "src/ast/switch_statement.h"
|
|
|
|
#include "src/ast/type/alias_type.h"
|
|
|
|
#include "src/ast/type/array_type.h"
|
|
|
|
#include "src/ast/type/bool_type.h"
|
|
|
|
#include "src/ast/type/f32_type.h"
|
|
|
|
#include "src/ast/type/i32_type.h"
|
|
|
|
#include "src/ast/type/matrix_type.h"
|
|
|
|
#include "src/ast/type/pointer_type.h"
|
|
|
|
#include "src/ast/type/struct_type.h"
|
|
|
|
#include "src/ast/type/vector_type.h"
|
2020-08-11 20:44:06 +00:00
|
|
|
#include "src/ast/type/void_type.h"
|
2020-07-22 12:59:19 +00:00
|
|
|
#include "src/ast/type_constructor_expression.h"
|
2020-08-06 21:24:14 +00:00
|
|
|
#include "src/ast/variable.h"
|
|
|
|
#include "src/ast/variable_decl_statement.h"
|
2020-07-23 23:49:52 +00:00
|
|
|
#include "src/type_determiner.h"
|
2020-11-06 17:31:15 +00:00
|
|
|
#include "src/validator/validator_impl.h"
|
|
|
|
#include "src/validator/validator_test_helper.h"
|
2020-07-22 12:59:19 +00:00
|
|
|
|
|
|
|
namespace tint {
|
|
|
|
namespace {
|
|
|
|
|
2020-08-25 14:06:05 +00:00
|
|
|
class ValidatorTest : public ValidatorTestHelper, public testing::Test {};
|
2020-07-22 12:59:19 +00:00
|
|
|
|
|
|
|
TEST_F(ValidatorTest, DISABLED_AssignToScalar_Fail) {
|
2020-07-23 23:49:52 +00:00
|
|
|
// 1 = my_var;
|
2020-11-30 23:30:58 +00:00
|
|
|
ast::type::I32 i32;
|
2020-07-22 12:59:19 +00:00
|
|
|
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* lhs = create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::SintLiteral>(Source{}, &i32, 1));
|
2020-12-11 19:16:13 +00:00
|
|
|
auto* rhs = create<ast::IdentifierExpression>(mod()->RegisterSymbol("my_var"),
|
|
|
|
"my_var");
|
2020-11-16 16:41:47 +00:00
|
|
|
ast::AssignmentStatement assign(Source{Source::Location{12, 34}}, lhs, rhs);
|
2020-07-22 12:59:19 +00:00
|
|
|
|
|
|
|
// TODO(sarahM0): Invalidate assignment to scalar.
|
2020-08-25 14:09:03 +00:00
|
|
|
ASSERT_TRUE(v()->has_error());
|
2020-07-23 23:49:52 +00:00
|
|
|
// TODO(sarahM0): figure out what should be the error number.
|
2020-11-26 17:49:22 +00:00
|
|
|
EXPECT_EQ(v()->error(), "12:34 v-000x: invalid assignment");
|
2020-07-22 12:59:19 +00:00
|
|
|
}
|
|
|
|
|
2020-08-06 21:24:14 +00:00
|
|
|
TEST_F(ValidatorTest, UsingUndefinedVariable_Fail) {
|
|
|
|
// b = 2;
|
2020-11-30 23:30:58 +00:00
|
|
|
ast::type::I32 i32;
|
2020-08-06 21:24:14 +00:00
|
|
|
|
2020-12-11 19:16:13 +00:00
|
|
|
auto* lhs = create<ast::IdentifierExpression>(
|
|
|
|
Source{Source::Location{12, 34}}, mod()->RegisterSymbol("b"), "b");
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* rhs = create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::SintLiteral>(Source{}, &i32, 2));
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* assign = create<ast::AssignmentStatement>(
|
2020-11-16 16:41:47 +00:00
|
|
|
Source{Source::Location{12, 34}}, lhs, rhs);
|
2020-08-06 21:24:14 +00:00
|
|
|
|
2020-11-16 16:31:07 +00:00
|
|
|
EXPECT_FALSE(td()->DetermineResultType(assign));
|
2020-10-14 18:26:31 +00:00
|
|
|
EXPECT_EQ(td()->error(),
|
|
|
|
"12:34: v-0006: identifier must be declared before use: b");
|
2020-08-06 21:24:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ValidatorTest, UsingUndefinedVariableInBlockStatement_Fail) {
|
|
|
|
// {
|
|
|
|
// b = 2;
|
|
|
|
// }
|
2020-11-30 23:30:58 +00:00
|
|
|
ast::type::I32 i32;
|
2020-08-06 21:24:14 +00:00
|
|
|
|
2020-12-11 19:16:13 +00:00
|
|
|
auto* lhs = create<ast::IdentifierExpression>(
|
|
|
|
Source{Source::Location{12, 34}}, mod()->RegisterSymbol("b"), "b");
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* rhs = create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::SintLiteral>(Source{}, &i32, 2));
|
2020-08-06 21:24:14 +00:00
|
|
|
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* body = create<ast::BlockStatement>();
|
2020-11-13 22:18:35 +00:00
|
|
|
body->append(create<ast::AssignmentStatement>(
|
2020-11-16 16:41:47 +00:00
|
|
|
Source{Source::Location{12, 34}}, lhs, rhs));
|
2020-08-06 21:24:14 +00:00
|
|
|
|
2020-11-16 16:31:07 +00:00
|
|
|
EXPECT_FALSE(td()->DetermineStatements(body));
|
2020-10-14 18:26:31 +00:00
|
|
|
EXPECT_EQ(td()->error(),
|
|
|
|
"12:34: v-0006: identifier must be declared before use: b");
|
2020-08-06 21:24:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ValidatorTest, AssignCompatibleTypes_Pass) {
|
|
|
|
// var a :i32 = 2;
|
|
|
|
// a = 2
|
2020-11-30 23:30:58 +00:00
|
|
|
ast::type::I32 i32;
|
2020-12-11 13:07:02 +00:00
|
|
|
auto* var = create<ast::Variable>(
|
|
|
|
Source{}, // source
|
|
|
|
"a", // name
|
|
|
|
ast::StorageClass::kNone, // storage_class
|
|
|
|
&i32, // type
|
|
|
|
false, // is_const
|
|
|
|
create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::SintLiteral>(Source{}, &i32, 2)), // constructor
|
|
|
|
ast::VariableDecorationList{}); // decorations
|
2020-08-06 21:24:14 +00:00
|
|
|
|
2020-12-11 19:16:13 +00:00
|
|
|
auto* lhs =
|
|
|
|
create<ast::IdentifierExpression>(mod()->RegisterSymbol("a"), "a");
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* rhs = create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::SintLiteral>(Source{}, &i32, 2));
|
2020-08-06 21:24:14 +00:00
|
|
|
|
2020-11-16 16:41:47 +00:00
|
|
|
ast::AssignmentStatement assign(Source{Source::Location{12, 34}}, lhs, rhs);
|
2020-11-16 16:31:07 +00:00
|
|
|
td()->RegisterVariableForTesting(var);
|
2020-08-06 21:24:14 +00:00
|
|
|
EXPECT_TRUE(td()->DetermineResultType(&assign)) << td()->error();
|
2020-11-16 16:41:47 +00:00
|
|
|
ASSERT_NE(lhs->result_type(), nullptr);
|
|
|
|
ASSERT_NE(rhs->result_type(), nullptr);
|
2020-08-25 14:09:03 +00:00
|
|
|
EXPECT_TRUE(v()->ValidateResultTypes(&assign));
|
2020-08-06 21:24:14 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 23:49:52 +00:00
|
|
|
TEST_F(ValidatorTest, AssignIncompatibleTypes_Fail) {
|
2020-08-06 21:24:14 +00:00
|
|
|
// {
|
|
|
|
// var a :i32 = 2;
|
|
|
|
// a = 2.3;
|
|
|
|
// }
|
2020-11-30 23:30:58 +00:00
|
|
|
ast::type::F32 f32;
|
|
|
|
ast::type::I32 i32;
|
2020-07-22 12:59:19 +00:00
|
|
|
|
2020-12-11 13:07:02 +00:00
|
|
|
auto* var = create<ast::Variable>(
|
|
|
|
Source{}, // source
|
|
|
|
"a", // name
|
|
|
|
ast::StorageClass::kNone, // storage_class
|
|
|
|
&i32, // type
|
|
|
|
false, // is_const
|
|
|
|
create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::SintLiteral>(Source{}, &i32, 2)), // constructor
|
|
|
|
ast::VariableDecorationList{}); // decorations
|
2020-12-11 13:07:02 +00:00
|
|
|
|
2020-12-11 19:16:13 +00:00
|
|
|
auto* lhs =
|
|
|
|
create<ast::IdentifierExpression>(mod()->RegisterSymbol("a"), "a");
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* rhs = create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::FloatLiteral>(Source{}, &f32, 2.3f));
|
2020-07-22 12:59:19 +00:00
|
|
|
|
2020-11-16 16:41:47 +00:00
|
|
|
ast::AssignmentStatement assign(Source{Source::Location{12, 34}}, lhs, rhs);
|
2020-11-16 16:31:07 +00:00
|
|
|
td()->RegisterVariableForTesting(var);
|
2020-07-23 23:49:52 +00:00
|
|
|
EXPECT_TRUE(td()->DetermineResultType(&assign)) << td()->error();
|
2020-11-16 16:41:47 +00:00
|
|
|
ASSERT_NE(lhs->result_type(), nullptr);
|
|
|
|
ASSERT_NE(rhs->result_type(), nullptr);
|
2020-07-22 12:59:19 +00:00
|
|
|
|
2020-08-25 14:09:03 +00:00
|
|
|
EXPECT_FALSE(v()->ValidateResultTypes(&assign));
|
|
|
|
ASSERT_TRUE(v()->has_error());
|
2020-07-23 23:49:52 +00:00
|
|
|
// TODO(sarahM0): figure out what should be the error number.
|
2020-08-25 14:09:03 +00:00
|
|
|
EXPECT_EQ(v()->error(),
|
2020-11-26 17:49:22 +00:00
|
|
|
"12:34 v-000x: invalid assignment of '__i32' to '__f32'");
|
2020-07-23 23:49:52 +00:00
|
|
|
}
|
|
|
|
|
2020-08-06 21:24:14 +00:00
|
|
|
TEST_F(ValidatorTest, AssignCompatibleTypesInBlockStatement_Pass) {
|
|
|
|
// {
|
|
|
|
// var a :i32 = 2;
|
|
|
|
// a = 2
|
|
|
|
// }
|
2020-11-30 23:30:58 +00:00
|
|
|
ast::type::I32 i32;
|
2020-12-11 13:07:02 +00:00
|
|
|
auto* var = create<ast::Variable>(
|
|
|
|
Source{}, // source
|
|
|
|
"a", // name
|
|
|
|
ast::StorageClass::kNone, // storage_class
|
|
|
|
&i32, // type
|
|
|
|
false, // is_const
|
|
|
|
create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::SintLiteral>(Source{}, &i32, 2)), // constructor
|
|
|
|
ast::VariableDecorationList{}); // decorations
|
2020-07-23 23:49:52 +00:00
|
|
|
|
2020-12-11 19:16:13 +00:00
|
|
|
auto* lhs =
|
|
|
|
create<ast::IdentifierExpression>(mod()->RegisterSymbol("a"), "a");
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* rhs = create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::SintLiteral>(Source{}, &i32, 2));
|
2020-07-23 23:49:52 +00:00
|
|
|
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* body = create<ast::BlockStatement>();
|
2020-11-16 16:41:47 +00:00
|
|
|
body->append(create<ast::VariableDeclStatement>(var));
|
2020-11-13 22:18:35 +00:00
|
|
|
body->append(create<ast::AssignmentStatement>(
|
2020-11-16 16:41:47 +00:00
|
|
|
Source{Source::Location{12, 34}}, lhs, rhs));
|
2020-08-06 21:24:14 +00:00
|
|
|
|
2020-11-16 16:31:07 +00:00
|
|
|
EXPECT_TRUE(td()->DetermineStatements(body)) << td()->error();
|
2020-11-16 16:41:47 +00:00
|
|
|
ASSERT_NE(lhs->result_type(), nullptr);
|
|
|
|
ASSERT_NE(rhs->result_type(), nullptr);
|
2020-07-23 23:49:52 +00:00
|
|
|
|
2020-11-16 16:31:07 +00:00
|
|
|
EXPECT_TRUE(v()->ValidateStatements(body)) << v()->error();
|
2020-08-06 21:24:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ValidatorTest, AssignIncompatibleTypesInBlockStatement_Fail) {
|
|
|
|
// {
|
|
|
|
// var a :i32 = 2;
|
|
|
|
// a = 2.3;
|
|
|
|
// }
|
2020-11-30 23:30:58 +00:00
|
|
|
ast::type::F32 f32;
|
|
|
|
ast::type::I32 i32;
|
2020-08-06 21:24:14 +00:00
|
|
|
|
2020-12-11 13:07:02 +00:00
|
|
|
auto* var = create<ast::Variable>(
|
|
|
|
Source{}, // source
|
|
|
|
"a", // name
|
|
|
|
ast::StorageClass::kNone, // storage_class
|
|
|
|
&i32, // type
|
|
|
|
false, // is_const
|
|
|
|
create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::SintLiteral>(Source{}, &i32, 2)), // constructor
|
|
|
|
ast::VariableDecorationList{}); // decorations
|
2020-12-11 19:16:13 +00:00
|
|
|
auto* lhs =
|
|
|
|
create<ast::IdentifierExpression>(mod()->RegisterSymbol("a"), "a");
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* rhs = create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::FloatLiteral>(Source{}, &f32, 2.3f));
|
2020-08-06 21:24:14 +00:00
|
|
|
|
|
|
|
ast::BlockStatement block;
|
2020-11-16 16:41:47 +00:00
|
|
|
block.append(create<ast::VariableDeclStatement>(var));
|
2020-11-13 22:18:35 +00:00
|
|
|
block.append(create<ast::AssignmentStatement>(
|
2020-11-16 16:41:47 +00:00
|
|
|
Source{Source::Location{12, 34}}, lhs, rhs));
|
2020-08-06 21:24:14 +00:00
|
|
|
|
|
|
|
EXPECT_TRUE(td()->DetermineStatements(&block)) << td()->error();
|
2020-11-16 16:41:47 +00:00
|
|
|
ASSERT_NE(lhs->result_type(), nullptr);
|
|
|
|
ASSERT_NE(rhs->result_type(), nullptr);
|
2020-08-06 21:24:14 +00:00
|
|
|
|
2020-08-25 14:09:03 +00:00
|
|
|
EXPECT_FALSE(v()->ValidateStatements(&block));
|
|
|
|
ASSERT_TRUE(v()->has_error());
|
2020-08-06 21:24:14 +00:00
|
|
|
// TODO(sarahM0): figure out what should be the error number.
|
2020-08-25 14:09:03 +00:00
|
|
|
EXPECT_EQ(v()->error(),
|
2020-11-26 17:49:22 +00:00
|
|
|
"12:34 v-000x: invalid assignment of '__i32' to '__f32'");
|
2020-08-06 21:24:14 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 15:04:53 +00:00
|
|
|
TEST_F(ValidatorTest, GlobalVariableWithStorageClass_Pass) {
|
|
|
|
// var<in> gloabl_var: f32;
|
2020-11-30 23:30:58 +00:00
|
|
|
ast::type::F32 f32;
|
2020-12-11 13:07:02 +00:00
|
|
|
mod()->AddGlobalVariable(
|
|
|
|
create<ast::Variable>(Source{Source::Location{12, 34}}, // source
|
|
|
|
"global_var", // name
|
|
|
|
ast::StorageClass::kInput, // storage_class
|
|
|
|
&f32, // type
|
|
|
|
false, // is_const
|
|
|
|
nullptr, // constructor
|
|
|
|
ast::VariableDecorationList{})); // decorations
|
2020-09-21 17:56:41 +00:00
|
|
|
EXPECT_TRUE(v()->ValidateGlobalVariables(mod()->global_variables()))
|
|
|
|
<< v()->error();
|
2020-08-25 15:04:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ValidatorTest, GlobalVariableNoStorageClass_Fail) {
|
|
|
|
// var gloabl_var: f32;
|
2020-11-30 23:30:58 +00:00
|
|
|
ast::type::F32 f32;
|
2020-12-11 13:07:02 +00:00
|
|
|
mod()->AddGlobalVariable(
|
|
|
|
create<ast::Variable>(Source{Source::Location{12, 34}}, // source
|
|
|
|
"global_var", // name
|
|
|
|
ast::StorageClass::kNone, // storage_class
|
|
|
|
&f32, // type
|
|
|
|
false, // is_const
|
|
|
|
nullptr, // constructor
|
|
|
|
ast::VariableDecorationList{})); // decorations
|
2020-08-25 15:04:53 +00:00
|
|
|
EXPECT_TRUE(td()->Determine()) << td()->error();
|
2020-08-27 17:59:20 +00:00
|
|
|
EXPECT_FALSE(v()->Validate(mod()));
|
|
|
|
EXPECT_EQ(v()->error(),
|
2020-11-26 17:49:22 +00:00
|
|
|
"12:34 v-0022: global variables must have a storage class");
|
2020-08-25 15:04:53 +00:00
|
|
|
}
|
2020-12-11 13:07:02 +00:00
|
|
|
|
2020-09-03 17:20:07 +00:00
|
|
|
TEST_F(ValidatorTest, GlobalConstantWithStorageClass_Fail) {
|
|
|
|
// const<in> gloabl_var: f32;
|
2020-11-30 23:30:58 +00:00
|
|
|
ast::type::F32 f32;
|
2020-12-11 13:07:02 +00:00
|
|
|
mod()->AddGlobalVariable(
|
|
|
|
create<ast::Variable>(Source{Source::Location{12, 34}}, // source
|
|
|
|
"global_var", // name
|
|
|
|
ast::StorageClass::kInput, // storage_class
|
|
|
|
&f32, // type
|
|
|
|
true, // is_const
|
|
|
|
nullptr, // constructor
|
|
|
|
ast::VariableDecorationList{})); // decorations
|
2020-09-03 17:20:07 +00:00
|
|
|
EXPECT_TRUE(td()->Determine()) << td()->error();
|
|
|
|
EXPECT_FALSE(v()->Validate(mod()));
|
|
|
|
EXPECT_EQ(
|
|
|
|
v()->error(),
|
2020-11-26 17:49:22 +00:00
|
|
|
"12:34 v-global01: global constants shouldn't have a storage class");
|
2020-09-03 17:20:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ValidatorTest, GlobalConstNoStorageClass_Pass) {
|
|
|
|
// const gloabl_var: f32;
|
2020-11-30 23:30:58 +00:00
|
|
|
ast::type::F32 f32;
|
2020-12-11 13:07:02 +00:00
|
|
|
mod()->AddGlobalVariable(
|
|
|
|
create<ast::Variable>(Source{Source::Location{12, 34}}, // source
|
|
|
|
"global_var", // name
|
|
|
|
ast::StorageClass::kNone, // storage_class
|
|
|
|
&f32, // type
|
|
|
|
true, // is_const
|
|
|
|
nullptr, // constructor
|
|
|
|
ast::VariableDecorationList{})); // decorations
|
2020-09-03 17:20:07 +00:00
|
|
|
EXPECT_TRUE(td()->Determine()) << td()->error();
|
|
|
|
EXPECT_FALSE(v()->Validate(mod())) << v()->error();
|
|
|
|
}
|
2020-08-25 15:04:53 +00:00
|
|
|
|
2020-08-11 20:44:06 +00:00
|
|
|
TEST_F(ValidatorTest, UsingUndefinedVariableGlobalVariable_Fail) {
|
|
|
|
// var global_var: f32 = 2.1;
|
|
|
|
// fn my_func() -> f32 {
|
|
|
|
// not_global_var = 3.14f;
|
|
|
|
// }
|
2020-11-30 23:30:58 +00:00
|
|
|
ast::type::F32 f32;
|
2020-12-11 13:07:02 +00:00
|
|
|
mod()->AddGlobalVariable(create<ast::Variable>(
|
|
|
|
Source{}, // source
|
|
|
|
"global_var", // name
|
|
|
|
ast::StorageClass::kPrivate, // storage_class
|
|
|
|
&f32, // type
|
|
|
|
false, // is_const
|
|
|
|
create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::FloatLiteral>(Source{}, &f32, 2.1)), // constructor
|
|
|
|
ast::VariableDecorationList{})); // decorations
|
2020-08-06 21:24:14 +00:00
|
|
|
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* lhs = create<ast::IdentifierExpression>(
|
2020-12-11 19:16:13 +00:00
|
|
|
Source{Source::Location{12, 34}}, mod()->RegisterSymbol("not_global_var"),
|
|
|
|
"not_global_var");
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* rhs = create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::FloatLiteral>(Source{}, &f32, 3.14f));
|
2020-08-06 21:24:14 +00:00
|
|
|
|
|
|
|
ast::VariableList params;
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* body = create<ast::BlockStatement>();
|
2020-11-13 22:18:35 +00:00
|
|
|
body->append(create<ast::AssignmentStatement>(
|
2020-11-16 16:41:47 +00:00
|
|
|
Source{Source::Location{12, 34}}, lhs, rhs));
|
2020-11-14 01:13:04 +00:00
|
|
|
|
2020-12-11 18:24:53 +00:00
|
|
|
auto* func = create<ast::Function>(Source{}, mod()->RegisterSymbol("my_func"),
|
|
|
|
"my_func", params, &f32, body,
|
2020-12-07 20:45:14 +00:00
|
|
|
ast::FunctionDecorationList{});
|
2020-11-16 16:41:47 +00:00
|
|
|
mod()->AddFunction(func);
|
2020-08-06 21:24:14 +00:00
|
|
|
|
2020-08-25 14:09:03 +00:00
|
|
|
EXPECT_FALSE(v()->Validate(mod()));
|
2020-11-26 17:49:22 +00:00
|
|
|
EXPECT_EQ(v()->error(), "12:34 v-0006: 'not_global_var' is not declared");
|
2020-08-06 21:24:14 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 20:44:06 +00:00
|
|
|
TEST_F(ValidatorTest, UsingUndefinedVariableGlobalVariable_Pass) {
|
|
|
|
// var global_var: f32 = 2.1;
|
2020-09-21 17:56:41 +00:00
|
|
|
// fn my_func() -> void {
|
2020-08-11 20:44:06 +00:00
|
|
|
// global_var = 3.14;
|
2020-09-21 17:56:41 +00:00
|
|
|
// return;
|
2020-08-11 20:44:06 +00:00
|
|
|
// }
|
2020-11-30 23:30:58 +00:00
|
|
|
ast::type::F32 f32;
|
|
|
|
ast::type::Void void_type;
|
2020-09-21 17:56:41 +00:00
|
|
|
|
2020-12-11 13:07:02 +00:00
|
|
|
mod()->AddGlobalVariable(create<ast::Variable>(
|
|
|
|
Source{}, // source
|
|
|
|
"global_var", // name
|
|
|
|
ast::StorageClass::kPrivate, // storage_class
|
|
|
|
&f32, // type
|
|
|
|
false, // is_const
|
|
|
|
create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::FloatLiteral>(Source{}, &f32, 2.1)), // constructor
|
|
|
|
ast::VariableDecorationList{})); // decorations
|
2020-08-06 21:24:14 +00:00
|
|
|
|
2020-12-11 19:16:13 +00:00
|
|
|
auto* lhs = create<ast::IdentifierExpression>(
|
|
|
|
mod()->RegisterSymbol("global_var"), "global_var");
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* rhs = create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::FloatLiteral>(Source{}, &f32, 3.14f));
|
2020-08-06 21:24:14 +00:00
|
|
|
|
|
|
|
ast::VariableList params;
|
|
|
|
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* body = create<ast::BlockStatement>();
|
2020-11-13 22:18:35 +00:00
|
|
|
body->append(create<ast::AssignmentStatement>(
|
2020-11-16 16:41:47 +00:00
|
|
|
Source{Source::Location{12, 34}}, lhs, rhs));
|
2020-12-07 21:09:57 +00:00
|
|
|
body->append(create<ast::ReturnStatement>(Source{}));
|
2020-12-07 20:45:14 +00:00
|
|
|
auto* func = create<ast::Function>(
|
2020-12-11 18:24:53 +00:00
|
|
|
Source{}, mod()->RegisterSymbol("my_func"), "my_func", params, &void_type,
|
|
|
|
body,
|
2020-12-07 20:45:14 +00:00
|
|
|
ast::FunctionDecorationList{
|
|
|
|
create<ast::StageDecoration>(ast::PipelineStage::kVertex, Source{}),
|
|
|
|
});
|
2020-11-16 16:41:47 +00:00
|
|
|
mod()->AddFunction(func);
|
2020-08-06 21:24:14 +00:00
|
|
|
|
|
|
|
EXPECT_TRUE(td()->Determine()) << td()->error();
|
2020-08-25 14:09:03 +00:00
|
|
|
EXPECT_TRUE(v()->Validate(mod())) << v()->error();
|
2020-08-06 21:24:14 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 20:44:06 +00:00
|
|
|
TEST_F(ValidatorTest, UsingUndefinedVariableInnerScope_Fail) {
|
2020-08-06 21:24:14 +00:00
|
|
|
// {
|
2020-08-11 20:44:06 +00:00
|
|
|
// if (true) { var a : f32 = 2.0; }
|
|
|
|
// a = 3.14;
|
2020-08-06 21:24:14 +00:00
|
|
|
// }
|
2020-11-30 23:30:58 +00:00
|
|
|
ast::type::F32 f32;
|
2020-12-11 13:07:02 +00:00
|
|
|
auto* var = create<ast::Variable>(
|
|
|
|
Source{}, // source
|
|
|
|
"a", // name
|
|
|
|
ast::StorageClass::kNone, // storage_class
|
|
|
|
&f32, // type
|
|
|
|
false, // is_const
|
|
|
|
create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::FloatLiteral>(Source{}, &f32, 2.0)), // constructor
|
|
|
|
ast::VariableDecorationList{}); // decorations
|
2020-08-06 21:24:14 +00:00
|
|
|
|
2020-11-30 23:30:58 +00:00
|
|
|
ast::type::Bool bool_type;
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* cond = create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::BoolLiteral>(Source{}, &bool_type, true));
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* body = create<ast::BlockStatement>();
|
2020-11-16 16:41:47 +00:00
|
|
|
body->append(create<ast::VariableDeclStatement>(var));
|
2020-08-06 21:24:14 +00:00
|
|
|
|
2020-12-11 19:16:13 +00:00
|
|
|
auto* lhs = create<ast::IdentifierExpression>(
|
|
|
|
Source{Source::Location{12, 34}}, mod()->RegisterSymbol("a"), "a");
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* rhs = create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::FloatLiteral>(Source{}, &f32, 3.14f));
|
2020-08-06 21:24:14 +00:00
|
|
|
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* outer_body = create<ast::BlockStatement>();
|
2020-12-07 20:11:24 +00:00
|
|
|
outer_body->append(
|
|
|
|
create<ast::IfStatement>(Source{}, cond, body, ast::ElseStatementList{}));
|
2020-11-13 22:18:35 +00:00
|
|
|
outer_body->append(create<ast::AssignmentStatement>(
|
2020-11-16 16:41:47 +00:00
|
|
|
Source{Source::Location{12, 34}}, lhs, rhs));
|
2020-08-06 21:24:14 +00:00
|
|
|
|
2020-11-16 16:31:07 +00:00
|
|
|
EXPECT_TRUE(td()->DetermineStatements(outer_body)) << td()->error();
|
2020-11-16 16:41:47 +00:00
|
|
|
ASSERT_NE(lhs->result_type(), nullptr);
|
|
|
|
ASSERT_NE(rhs->result_type(), nullptr);
|
2020-11-16 16:31:07 +00:00
|
|
|
EXPECT_FALSE(v()->ValidateStatements(outer_body));
|
2020-11-26 17:49:22 +00:00
|
|
|
EXPECT_EQ(v()->error(), "12:34 v-0006: 'a' is not declared");
|
2020-08-06 21:24:14 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 20:44:06 +00:00
|
|
|
TEST_F(ValidatorTest, UsingUndefinedVariableOuterScope_Pass) {
|
2020-08-06 21:24:14 +00:00
|
|
|
// {
|
2020-08-11 20:44:06 +00:00
|
|
|
// var a : f32 = 2.0;
|
|
|
|
// if (true) { a = 3.14; }
|
2020-08-06 21:24:14 +00:00
|
|
|
// }
|
2020-11-30 23:30:58 +00:00
|
|
|
ast::type::F32 f32;
|
2020-12-11 13:07:02 +00:00
|
|
|
auto* var = create<ast::Variable>(
|
|
|
|
Source{}, // source
|
|
|
|
"a", // name
|
|
|
|
ast::StorageClass::kNone, // storage_class
|
|
|
|
&f32, // type
|
|
|
|
false, // is_const
|
|
|
|
create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::FloatLiteral>(Source{}, &f32, 2.0)), // constructor
|
|
|
|
ast::VariableDecorationList{}); // decorations
|
2020-08-06 21:24:14 +00:00
|
|
|
|
2020-12-11 19:16:13 +00:00
|
|
|
auto* lhs = create<ast::IdentifierExpression>(
|
|
|
|
Source{Source::Location{12, 34}}, mod()->RegisterSymbol("a"), "a");
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* rhs = create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::FloatLiteral>(Source{}, &f32, 3.14f));
|
2020-11-16 16:41:47 +00:00
|
|
|
|
2020-11-30 23:30:58 +00:00
|
|
|
ast::type::Bool bool_type;
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* cond = create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::BoolLiteral>(Source{}, &bool_type, true));
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* body = create<ast::BlockStatement>();
|
2020-11-13 22:18:35 +00:00
|
|
|
body->append(create<ast::AssignmentStatement>(
|
2020-11-16 16:41:47 +00:00
|
|
|
Source{Source::Location{12, 34}}, lhs, rhs));
|
2020-08-06 21:24:14 +00:00
|
|
|
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* outer_body = create<ast::BlockStatement>();
|
2020-11-16 16:41:47 +00:00
|
|
|
outer_body->append(create<ast::VariableDeclStatement>(var));
|
2020-12-07 20:11:24 +00:00
|
|
|
outer_body->append(
|
|
|
|
create<ast::IfStatement>(Source{}, cond, body, ast::ElseStatementList{}));
|
2020-11-16 16:31:07 +00:00
|
|
|
EXPECT_TRUE(td()->DetermineStatements(outer_body)) << td()->error();
|
2020-11-16 16:41:47 +00:00
|
|
|
ASSERT_NE(lhs->result_type(), nullptr);
|
|
|
|
ASSERT_NE(rhs->result_type(), nullptr);
|
2020-11-16 16:31:07 +00:00
|
|
|
EXPECT_TRUE(v()->ValidateStatements(outer_body)) << v()->error();
|
2020-07-22 12:59:19 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 20:44:06 +00:00
|
|
|
TEST_F(ValidatorTest, GlobalVariableUnique_Pass) {
|
|
|
|
// var global_var0 : f32 = 0.1;
|
|
|
|
// var global_var1 : i32 = 0;
|
2020-11-30 23:30:58 +00:00
|
|
|
ast::type::F32 f32;
|
|
|
|
ast::type::I32 i32;
|
2020-12-11 13:07:02 +00:00
|
|
|
auto* var0 = create<ast::Variable>(
|
|
|
|
Source{}, // source
|
|
|
|
"global_var0", // name
|
|
|
|
ast::StorageClass::kPrivate, // storage_class
|
|
|
|
&f32, // type
|
|
|
|
false, // is_const
|
|
|
|
create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::FloatLiteral>(Source{}, &f32, 0.1)), // constructor
|
|
|
|
ast::VariableDecorationList{}); // decorations
|
2020-11-16 16:41:47 +00:00
|
|
|
mod()->AddGlobalVariable(var0);
|
2020-08-11 20:44:06 +00:00
|
|
|
|
2020-12-11 13:07:02 +00:00
|
|
|
auto* var1 = create<ast::Variable>(
|
|
|
|
Source{Source::Location{12, 34}}, // source
|
|
|
|
"global_var1", // name
|
|
|
|
ast::StorageClass::kPrivate, // storage_class
|
|
|
|
&f32, // type
|
|
|
|
false, // is_const
|
|
|
|
create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::SintLiteral>(Source{}, &i32, 0)), // constructor
|
|
|
|
ast::VariableDecorationList{}); // decorations
|
2020-11-16 16:41:47 +00:00
|
|
|
mod()->AddGlobalVariable(var1);
|
2020-08-11 20:44:06 +00:00
|
|
|
|
2020-09-21 17:56:41 +00:00
|
|
|
EXPECT_TRUE(v()->ValidateGlobalVariables(mod()->global_variables()))
|
|
|
|
<< v()->error();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ValidatorTest, GlobalVariableNotUnique_Fail) {
|
|
|
|
// var global_var : f32 = 0.1;
|
|
|
|
// var global_var : i32 = 0;
|
2020-11-30 23:30:58 +00:00
|
|
|
ast::type::F32 f32;
|
|
|
|
ast::type::I32 i32;
|
2020-12-11 13:07:02 +00:00
|
|
|
auto* var0 = create<ast::Variable>(
|
|
|
|
Source{}, // source
|
|
|
|
"global_var", // name
|
|
|
|
ast::StorageClass::kPrivate, // storage_class
|
|
|
|
&f32, // type
|
|
|
|
false, // is_const
|
|
|
|
create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::FloatLiteral>(Source{}, &f32, 0.1)), // constructor
|
|
|
|
ast::VariableDecorationList{}); // decorations
|
2020-11-16 16:41:47 +00:00
|
|
|
mod()->AddGlobalVariable(var0);
|
2020-09-21 17:56:41 +00:00
|
|
|
|
2020-12-11 13:07:02 +00:00
|
|
|
auto* var1 = create<ast::Variable>(
|
|
|
|
Source{Source::Location{12, 34}}, // source
|
|
|
|
"global_var", // name
|
|
|
|
ast::StorageClass::kPrivate, // storage_class
|
|
|
|
&f32, // type
|
|
|
|
false, // is_const
|
|
|
|
create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::SintLiteral>(Source{}, &i32, 0)), // constructor
|
|
|
|
ast::VariableDecorationList{}); // decorations
|
2020-11-16 16:41:47 +00:00
|
|
|
mod()->AddGlobalVariable(var1);
|
2020-09-21 17:56:41 +00:00
|
|
|
|
|
|
|
EXPECT_FALSE(v()->ValidateGlobalVariables(mod()->global_variables()));
|
|
|
|
EXPECT_EQ(v()->error(),
|
2020-11-26 17:49:22 +00:00
|
|
|
"12:34 v-0011: redeclared global identifier 'global_var'");
|
2020-08-11 20:44:06 +00:00
|
|
|
}
|
|
|
|
|
2020-08-07 14:34:34 +00:00
|
|
|
TEST_F(ValidatorTest, AssignToConstant_Fail) {
|
|
|
|
// {
|
|
|
|
// const a :i32 = 2;
|
|
|
|
// a = 2
|
|
|
|
// }
|
2020-11-30 23:30:58 +00:00
|
|
|
ast::type::I32 i32;
|
2020-12-11 13:07:02 +00:00
|
|
|
auto* var = create<ast::Variable>(
|
|
|
|
Source{}, // source
|
|
|
|
"a", // name
|
|
|
|
ast::StorageClass::kNone, // storage_class
|
|
|
|
&i32, // type
|
|
|
|
true, // is_const
|
|
|
|
create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::SintLiteral>(Source{}, &i32, 2)), // constructor
|
|
|
|
ast::VariableDecorationList{}); // decorations
|
2020-07-24 14:50:51 +00:00
|
|
|
|
2020-12-11 19:16:13 +00:00
|
|
|
auto* lhs =
|
|
|
|
create<ast::IdentifierExpression>(mod()->RegisterSymbol("a"), "a");
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* rhs = create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::SintLiteral>(Source{}, &i32, 2));
|
2020-07-24 14:50:51 +00:00
|
|
|
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* body = create<ast::BlockStatement>();
|
2020-11-16 16:41:47 +00:00
|
|
|
body->append(create<ast::VariableDeclStatement>(var));
|
2020-11-13 22:18:35 +00:00
|
|
|
body->append(create<ast::AssignmentStatement>(
|
2020-11-16 16:41:47 +00:00
|
|
|
Source{Source::Location{12, 34}}, lhs, rhs));
|
2020-08-07 14:34:34 +00:00
|
|
|
|
2020-11-16 16:31:07 +00:00
|
|
|
EXPECT_TRUE(td()->DetermineStatements(body)) << td()->error();
|
2020-11-16 16:41:47 +00:00
|
|
|
ASSERT_NE(lhs->result_type(), nullptr);
|
|
|
|
ASSERT_NE(rhs->result_type(), nullptr);
|
2020-07-24 14:50:51 +00:00
|
|
|
|
2020-11-16 16:31:07 +00:00
|
|
|
EXPECT_FALSE(v()->ValidateStatements(body));
|
2020-11-26 17:49:22 +00:00
|
|
|
EXPECT_EQ(v()->error(), "12:34 v-0021: cannot re-assign a constant: 'a'");
|
2020-07-24 14:50:51 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 20:44:06 +00:00
|
|
|
TEST_F(ValidatorTest, GlobalVariableFunctionVariableNotUnique_Fail) {
|
|
|
|
// var a: f32 = 2.1;
|
|
|
|
// fn my_func -> void {
|
|
|
|
// var a: f32 = 2.0;
|
|
|
|
// return 0;
|
|
|
|
// }
|
|
|
|
|
2020-11-30 23:30:58 +00:00
|
|
|
ast::type::Void void_type;
|
|
|
|
ast::type::F32 f32;
|
2020-12-11 13:07:02 +00:00
|
|
|
auto* global_var = create<ast::Variable>(
|
|
|
|
Source{}, // source
|
|
|
|
"a", // name
|
|
|
|
ast::StorageClass::kPrivate, // storage_class
|
|
|
|
&f32, // type
|
|
|
|
false, // is_const
|
|
|
|
create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::FloatLiteral>(Source{}, &f32, 2.1)), // constructor
|
|
|
|
ast::VariableDecorationList{}); // decorations
|
2020-11-16 16:41:47 +00:00
|
|
|
mod()->AddGlobalVariable(global_var);
|
2020-08-11 20:44:06 +00:00
|
|
|
|
2020-12-11 13:07:02 +00:00
|
|
|
auto* var = create<ast::Variable>(
|
|
|
|
Source{}, // source
|
|
|
|
"a", // name
|
|
|
|
ast::StorageClass::kNone, // storage_class
|
|
|
|
&f32, // type
|
|
|
|
false, // is_const
|
|
|
|
create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::FloatLiteral>(Source{}, &f32, 2.0)), // constructor
|
|
|
|
ast::VariableDecorationList{}); // decorations
|
2020-08-11 20:44:06 +00:00
|
|
|
ast::VariableList params;
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* body = create<ast::BlockStatement>();
|
2020-11-13 22:18:35 +00:00
|
|
|
body->append(create<ast::VariableDeclStatement>(
|
2020-11-16 16:41:47 +00:00
|
|
|
Source{Source::Location{12, 34}}, var));
|
2020-12-11 18:24:53 +00:00
|
|
|
auto* func = create<ast::Function>(Source{}, mod()->RegisterSymbol("my_func"),
|
|
|
|
"my_func", params, &void_type, body,
|
|
|
|
ast::FunctionDecorationList{});
|
2020-11-16 16:41:47 +00:00
|
|
|
|
|
|
|
mod()->AddFunction(func);
|
2020-08-11 20:44:06 +00:00
|
|
|
|
|
|
|
EXPECT_TRUE(td()->Determine()) << td()->error();
|
2020-11-16 16:41:47 +00:00
|
|
|
EXPECT_TRUE(td()->DetermineFunction(func)) << td()->error();
|
2020-08-25 14:09:03 +00:00
|
|
|
EXPECT_FALSE(v()->Validate(mod())) << v()->error();
|
2020-11-26 17:49:22 +00:00
|
|
|
EXPECT_EQ(v()->error(), "12:34 v-0013: redeclared identifier 'a'");
|
2020-08-11 20:44:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ValidatorTest, RedeclaredIndentifier_Fail) {
|
|
|
|
// fn my_func() -> void {
|
|
|
|
// var a :i32 = 2;
|
|
|
|
// var a :f21 = 2.0;
|
|
|
|
// }
|
2020-11-30 23:30:58 +00:00
|
|
|
ast::type::Void void_type;
|
|
|
|
ast::type::I32 i32;
|
|
|
|
ast::type::F32 f32;
|
2020-12-11 13:07:02 +00:00
|
|
|
auto* var = create<ast::Variable>(
|
|
|
|
Source{}, // source
|
|
|
|
"a", // name
|
|
|
|
ast::StorageClass::kNone, // storage_class
|
|
|
|
&i32, // type
|
|
|
|
false, // is_const
|
|
|
|
create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::SintLiteral>(Source{}, &i32, 2)), // constructor
|
|
|
|
ast::VariableDecorationList{}); // decorations
|
2020-12-11 13:07:02 +00:00
|
|
|
|
|
|
|
auto* var_a_float = create<ast::Variable>(
|
|
|
|
Source{}, // source
|
|
|
|
"a", // name
|
|
|
|
ast::StorageClass::kNone, // storage_class
|
|
|
|
&f32, // type
|
|
|
|
false, // is_const
|
|
|
|
create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::FloatLiteral>(Source{}, &f32, 0.1)), // constructor
|
|
|
|
ast::VariableDecorationList{}); // decorations
|
2020-08-11 20:44:06 +00:00
|
|
|
|
|
|
|
ast::VariableList params;
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* body = create<ast::BlockStatement>();
|
2020-11-16 16:41:47 +00:00
|
|
|
body->append(create<ast::VariableDeclStatement>(var));
|
2020-11-13 22:18:35 +00:00
|
|
|
body->append(create<ast::VariableDeclStatement>(
|
2020-11-16 16:41:47 +00:00
|
|
|
Source{Source::Location{12, 34}}, var_a_float));
|
2020-12-11 18:24:53 +00:00
|
|
|
auto* func = create<ast::Function>(Source{}, mod()->RegisterSymbol("my_func"),
|
|
|
|
"my_func", params, &void_type, body,
|
|
|
|
ast::FunctionDecorationList{});
|
2020-11-16 16:41:47 +00:00
|
|
|
|
|
|
|
mod()->AddFunction(func);
|
2020-08-11 20:44:06 +00:00
|
|
|
|
|
|
|
EXPECT_TRUE(td()->Determine()) << td()->error();
|
2020-11-16 16:41:47 +00:00
|
|
|
EXPECT_TRUE(td()->DetermineFunction(func)) << td()->error();
|
2020-08-25 14:09:03 +00:00
|
|
|
EXPECT_FALSE(v()->Validate(mod()));
|
2020-11-26 17:49:22 +00:00
|
|
|
EXPECT_EQ(v()->error(), "12:34 v-0014: redeclared identifier 'a'");
|
2020-08-11 20:44:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ValidatorTest, RedeclaredIdentifierInnerScope_Pass) {
|
|
|
|
// {
|
|
|
|
// if (true) { var a : f32 = 2.0; }
|
|
|
|
// var a : f32 = 3.14;
|
|
|
|
// }
|
2020-11-30 23:30:58 +00:00
|
|
|
ast::type::F32 f32;
|
2020-12-11 13:07:02 +00:00
|
|
|
auto* var = create<ast::Variable>(
|
|
|
|
Source{}, // source
|
|
|
|
"a", // name
|
|
|
|
ast::StorageClass::kNone, // storage_class
|
|
|
|
&f32, // type
|
|
|
|
false, // is_const
|
|
|
|
create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::FloatLiteral>(Source{}, &f32, 2.0)), // constructor
|
|
|
|
ast::VariableDecorationList{}); // decorations
|
2020-08-11 20:44:06 +00:00
|
|
|
|
2020-11-30 23:30:58 +00:00
|
|
|
ast::type::Bool bool_type;
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* cond = create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::BoolLiteral>(Source{}, &bool_type, true));
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* body = create<ast::BlockStatement>();
|
2020-11-16 16:41:47 +00:00
|
|
|
body->append(create<ast::VariableDeclStatement>(var));
|
2020-11-13 22:18:35 +00:00
|
|
|
|
2020-12-11 13:07:02 +00:00
|
|
|
auto* var_a_float = create<ast::Variable>(
|
|
|
|
Source{}, // source
|
|
|
|
"a", // name
|
|
|
|
ast::StorageClass::kNone, // storage_class
|
|
|
|
&f32, // type
|
|
|
|
false, // is_const
|
|
|
|
create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::FloatLiteral>(Source{}, &f32, 3.14)), // constructor
|
|
|
|
ast::VariableDecorationList{}); // decorations
|
2020-11-13 22:18:35 +00:00
|
|
|
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* outer_body = create<ast::BlockStatement>();
|
2020-12-07 20:11:24 +00:00
|
|
|
outer_body->append(
|
|
|
|
create<ast::IfStatement>(Source{}, cond, body, ast::ElseStatementList{}));
|
2020-11-13 22:18:35 +00:00
|
|
|
outer_body->append(create<ast::VariableDeclStatement>(
|
2020-11-16 16:41:47 +00:00
|
|
|
Source{Source::Location{12, 34}}, var_a_float));
|
2020-08-11 20:44:06 +00:00
|
|
|
|
2020-11-16 16:31:07 +00:00
|
|
|
EXPECT_TRUE(td()->DetermineStatements(outer_body)) << td()->error();
|
|
|
|
EXPECT_TRUE(v()->ValidateStatements(outer_body)) << v()->error();
|
2020-08-11 20:44:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ValidatorTest, DISABLED_RedeclaredIdentifierInnerScope_False) {
|
|
|
|
// TODO(sarahM0): remove DISABLED after implementing ValidateIfStatement
|
|
|
|
// and it should just work
|
|
|
|
// {
|
|
|
|
// var a : f32 = 3.14;
|
|
|
|
// if (true) { var a : f32 = 2.0; }
|
|
|
|
// }
|
2020-11-30 23:30:58 +00:00
|
|
|
ast::type::F32 f32;
|
2020-12-11 13:07:02 +00:00
|
|
|
auto* var_a_float = create<ast::Variable>(
|
|
|
|
Source{}, // source
|
|
|
|
"a", // name
|
|
|
|
ast::StorageClass::kNone, // storage_class
|
|
|
|
&f32, // type
|
|
|
|
false, // is_const
|
|
|
|
create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::FloatLiteral>(Source{}, &f32, 3.14)), // constructor
|
|
|
|
ast::VariableDecorationList{}); // decorations
|
2020-12-11 13:07:02 +00:00
|
|
|
|
|
|
|
auto* var = create<ast::Variable>(
|
|
|
|
Source{}, // source
|
|
|
|
"a", // name
|
|
|
|
ast::StorageClass::kNone, // storage_class
|
|
|
|
&f32, // type
|
|
|
|
false, // is_const
|
|
|
|
create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::FloatLiteral>(Source{}, &f32, 2.0)), // constructor
|
|
|
|
ast::VariableDecorationList{}); // decorations
|
2020-08-11 20:44:06 +00:00
|
|
|
|
2020-11-30 23:30:58 +00:00
|
|
|
ast::type::Bool bool_type;
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* cond = create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::BoolLiteral>(Source{}, &bool_type, true));
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* body = create<ast::BlockStatement>();
|
2020-11-13 22:18:35 +00:00
|
|
|
body->append(create<ast::VariableDeclStatement>(
|
2020-11-16 16:41:47 +00:00
|
|
|
Source{Source::Location{12, 34}}, var));
|
2020-08-11 20:44:06 +00:00
|
|
|
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* outer_body = create<ast::BlockStatement>();
|
2020-11-16 16:41:47 +00:00
|
|
|
outer_body->append(create<ast::VariableDeclStatement>(var_a_float));
|
2020-12-07 20:11:24 +00:00
|
|
|
outer_body->append(
|
|
|
|
create<ast::IfStatement>(Source{}, cond, body, ast::ElseStatementList{}));
|
2020-08-11 20:44:06 +00:00
|
|
|
|
2020-11-16 16:31:07 +00:00
|
|
|
EXPECT_TRUE(td()->DetermineStatements(outer_body)) << td()->error();
|
|
|
|
EXPECT_FALSE(v()->ValidateStatements(outer_body));
|
2020-11-26 17:49:22 +00:00
|
|
|
EXPECT_EQ(v()->error(), "12:34 v-0014: redeclared identifier 'a'");
|
2020-08-11 20:44:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ValidatorTest, RedeclaredIdentifierDifferentFunctions_Pass) {
|
2020-08-13 18:09:59 +00:00
|
|
|
// func0 { var a : f32 = 2.0; return; }
|
|
|
|
// func1 { var a : f32 = 3.0; return; }
|
2020-11-30 23:30:58 +00:00
|
|
|
ast::type::F32 f32;
|
|
|
|
ast::type::Void void_type;
|
2020-12-11 13:07:02 +00:00
|
|
|
auto* var0 = create<ast::Variable>(
|
|
|
|
Source{}, // source
|
|
|
|
"a", // name
|
|
|
|
ast::StorageClass::kNone, // storage_class
|
|
|
|
&f32, // type
|
|
|
|
false, // is_const
|
|
|
|
create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::FloatLiteral>(Source{}, &f32, 2.0)), // constructor
|
|
|
|
ast::VariableDecorationList{}); // decorations
|
2020-12-11 13:07:02 +00:00
|
|
|
|
|
|
|
auto* var1 = create<ast::Variable>(
|
|
|
|
Source{}, // source
|
|
|
|
"a", // name
|
|
|
|
ast::StorageClass::kNone, // storage_class
|
|
|
|
&void_type, // type
|
|
|
|
false, // is_const
|
|
|
|
create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::FloatLiteral>(Source{}, &f32, 1.0)), // constructor
|
|
|
|
ast::VariableDecorationList{}); // decorations
|
2020-08-11 20:44:06 +00:00
|
|
|
|
|
|
|
ast::VariableList params0;
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* body0 = create<ast::BlockStatement>();
|
2020-11-13 22:18:35 +00:00
|
|
|
body0->append(create<ast::VariableDeclStatement>(
|
2020-11-16 16:41:47 +00:00
|
|
|
Source{Source::Location{12, 34}}, var0));
|
2020-12-07 21:09:57 +00:00
|
|
|
body0->append(create<ast::ReturnStatement>(Source{}));
|
2020-12-11 18:24:53 +00:00
|
|
|
auto* func0 = create<ast::Function>(Source{}, mod()->RegisterSymbol("func0"),
|
|
|
|
"func0", params0, &void_type, body0,
|
|
|
|
ast::FunctionDecorationList{});
|
2020-08-11 20:44:06 +00:00
|
|
|
|
|
|
|
ast::VariableList params1;
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* body1 = create<ast::BlockStatement>();
|
2020-11-13 22:18:35 +00:00
|
|
|
body1->append(create<ast::VariableDeclStatement>(
|
2020-11-16 16:41:47 +00:00
|
|
|
Source{Source::Location{13, 34}}, var1));
|
2020-12-07 21:09:57 +00:00
|
|
|
body1->append(create<ast::ReturnStatement>(Source{}));
|
2020-12-07 20:45:14 +00:00
|
|
|
auto* func1 = create<ast::Function>(
|
2020-12-11 18:24:53 +00:00
|
|
|
Source{}, mod()->RegisterSymbol("func1"), "func1", params1, &void_type,
|
|
|
|
body1,
|
2020-12-07 20:45:14 +00:00
|
|
|
ast::FunctionDecorationList{
|
|
|
|
create<ast::StageDecoration>(ast::PipelineStage::kVertex, Source{}),
|
|
|
|
});
|
2020-08-11 20:44:06 +00:00
|
|
|
|
2020-11-16 16:41:47 +00:00
|
|
|
mod()->AddFunction(func0);
|
|
|
|
mod()->AddFunction(func1);
|
2020-08-11 20:44:06 +00:00
|
|
|
|
|
|
|
EXPECT_TRUE(td()->Determine()) << td()->error();
|
2020-08-25 14:09:03 +00:00
|
|
|
EXPECT_TRUE(v()->Validate(mod())) << v()->error();
|
2020-08-11 20:44:06 +00:00
|
|
|
}
|
|
|
|
|
2020-08-20 17:00:09 +00:00
|
|
|
TEST_F(ValidatorTest, VariableDeclNoConstructor_Pass) {
|
|
|
|
// {
|
|
|
|
// var a :i32;
|
|
|
|
// a = 2;
|
|
|
|
// }
|
2020-11-30 23:30:58 +00:00
|
|
|
ast::type::I32 i32;
|
2020-12-07 21:08:07 +00:00
|
|
|
auto* var =
|
2020-12-11 13:07:02 +00:00
|
|
|
create<ast::Variable>(Source{}, // source
|
|
|
|
"a", // name
|
|
|
|
ast::StorageClass::kNone, // storage_class
|
|
|
|
&i32, // type
|
|
|
|
false, // is_const
|
|
|
|
nullptr, // constructor
|
|
|
|
ast::VariableDecorationList{}); // decorations
|
2020-08-17 21:01:28 +00:00
|
|
|
|
2020-11-16 16:31:07 +00:00
|
|
|
td()->RegisterVariableForTesting(var);
|
2020-12-11 19:16:13 +00:00
|
|
|
auto* lhs =
|
|
|
|
create<ast::IdentifierExpression>(mod()->RegisterSymbol("a"), "a");
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* rhs = create<ast::ScalarConstructorExpression>(
|
2020-12-12 01:35:43 +00:00
|
|
|
create<ast::SintLiteral>(Source{}, &i32, 2));
|
2020-08-20 17:00:09 +00:00
|
|
|
|
2020-11-16 16:31:07 +00:00
|
|
|
auto* body = create<ast::BlockStatement>();
|
2020-11-16 16:41:47 +00:00
|
|
|
body->append(create<ast::VariableDeclStatement>(var));
|
2020-11-13 22:18:35 +00:00
|
|
|
body->append(create<ast::AssignmentStatement>(
|
2020-11-16 16:41:47 +00:00
|
|
|
Source{Source::Location{12, 34}}, lhs, rhs));
|
2020-08-20 17:00:09 +00:00
|
|
|
|
2020-11-16 16:31:07 +00:00
|
|
|
EXPECT_TRUE(td()->DetermineStatements(body)) << td()->error();
|
2020-11-16 16:41:47 +00:00
|
|
|
ASSERT_NE(lhs->result_type(), nullptr);
|
|
|
|
ASSERT_NE(rhs->result_type(), nullptr);
|
2020-11-16 16:31:07 +00:00
|
|
|
EXPECT_TRUE(v()->ValidateStatements(body)) << v()->error();
|
2020-08-17 21:01:28 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 12:59:19 +00:00
|
|
|
} // namespace
|
|
|
|
} // namespace tint
|