[validation] implement invalidate assignment to a different type.
Bug:6 Change-Id: I5bd98731a5827e5a6aa1761dd18e7e116dd68544 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/25524 Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
parent
453eb97a30
commit
591fe919c2
|
@ -18,7 +18,10 @@
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#include "src/ast/assignment_statement.h"
|
||||||
|
#include "src/ast/expression.h"
|
||||||
#include "src/ast/module.h"
|
#include "src/ast/module.h"
|
||||||
|
#include "src/ast/statement.h"
|
||||||
#include "src/validator_impl.h"
|
#include "src/validator_impl.h"
|
||||||
|
|
||||||
namespace tint {
|
namespace tint {
|
||||||
|
|
|
@ -21,13 +21,59 @@ ValidatorImpl::ValidatorImpl() = default;
|
||||||
ValidatorImpl::~ValidatorImpl() = default;
|
ValidatorImpl::~ValidatorImpl() = default;
|
||||||
|
|
||||||
void ValidatorImpl::set_error(const Source& src, const std::string& msg) {
|
void ValidatorImpl::set_error(const Source& src, const std::string& msg) {
|
||||||
error_ =
|
error_ +=
|
||||||
std::to_string(src.line) + ":" + std::to_string(src.column) + ": " + msg;
|
std::to_string(src.line) + ":" + std::to_string(src.column) + ": " + msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ValidatorImpl::Validate(const ast::Module& module) {
|
bool ValidatorImpl::Validate(const ast::Module& module) {
|
||||||
if (!CheckImports(module))
|
if (!CheckImports(module))
|
||||||
return false;
|
return false;
|
||||||
|
if (!ValidateFunctions(module.functions()))
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ValidatorImpl::ValidateFunctions(const ast::FunctionList& funcs) {
|
||||||
|
for (const auto& func : funcs) {
|
||||||
|
if (!ValidateFunction(*(func.get()))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ValidatorImpl::ValidateFunction(const ast::Function& func) {
|
||||||
|
if (!ValidateStatements(func.body()))
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ValidatorImpl::ValidateStatements(const ast::StatementList& stmts) {
|
||||||
|
for (const auto& stmt : stmts) {
|
||||||
|
if (!ValidateStatement(*(stmt.get()))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ValidatorImpl::ValidateStatement(const ast::Statement& stmt) {
|
||||||
|
if (stmt.IsAssign() && !ValidateAssign(*(stmt.AsAssign())))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ValidatorImpl::ValidateAssign(const ast::AssignmentStatement& a) {
|
||||||
|
auto lhs_result_type = a.lhs()->result_type()->UnwrapAliasPtrAlias();
|
||||||
|
auto rhs_result_type = a.rhs()->result_type()->UnwrapAliasPtrAlias();
|
||||||
|
if (lhs_result_type != rhs_result_type) {
|
||||||
|
// TODO(sarahM0): figur out what should be the error number.
|
||||||
|
set_error(a.source(), "v-000x: invalid assignment of '" +
|
||||||
|
lhs_result_type->type_name() + "' to '" +
|
||||||
|
rhs_result_type->type_name() + "'");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,10 @@
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#include "src/ast/assignment_statement.h"
|
||||||
|
#include "src/ast/expression.h"
|
||||||
#include "src/ast/module.h"
|
#include "src/ast/module.h"
|
||||||
|
#include "src/ast/statement.h"
|
||||||
|
|
||||||
namespace tint {
|
namespace tint {
|
||||||
|
|
||||||
|
@ -43,7 +46,26 @@ class ValidatorImpl {
|
||||||
/// @param src the source causing the error
|
/// @param src the source causing the error
|
||||||
/// @param msg the error message
|
/// @param msg the error message
|
||||||
void set_error(const Source& src, const std::string& msg);
|
void set_error(const Source& src, const std::string& msg);
|
||||||
|
/// Validates Functions
|
||||||
|
/// @param funcs the functions to check
|
||||||
|
/// @returns true if the validation was successful
|
||||||
|
bool ValidateFunctions(const ast::FunctionList& funcs);
|
||||||
|
/// Validates a function
|
||||||
|
/// @param func the function to check
|
||||||
|
/// @returns true if the validation was successful
|
||||||
|
bool ValidateFunction(const ast::Function& func);
|
||||||
|
/// Validates a set of statements
|
||||||
|
/// @param stmts the statements to check
|
||||||
|
/// @returns true if the validation was successful
|
||||||
|
bool ValidateStatements(const ast::StatementList& stmts);
|
||||||
|
/// Validates a statement
|
||||||
|
/// @param stmt the statement to check
|
||||||
|
/// @returns true if the validation was successful
|
||||||
|
bool ValidateStatement(const ast::Statement& stmt);
|
||||||
|
/// Validates an assignment
|
||||||
|
/// @param a the assignment to check
|
||||||
|
/// @returns true if the validation was successful
|
||||||
|
bool ValidateAssign(const ast::AssignmentStatement& a);
|
||||||
/// Validates v-0001: Only allowed import is "GLSL.std.450"
|
/// Validates v-0001: Only allowed import is "GLSL.std.450"
|
||||||
/// @param module the modele to check imports
|
/// @param module the modele to check imports
|
||||||
/// @returns ture if input complies with v-0001 rule
|
/// @returns ture if input complies with v-0001 rule
|
||||||
|
|
|
@ -48,11 +48,26 @@
|
||||||
#include "src/ast/type/struct_type.h"
|
#include "src/ast/type/struct_type.h"
|
||||||
#include "src/ast/type/vector_type.h"
|
#include "src/ast/type/vector_type.h"
|
||||||
#include "src/ast/type_constructor_expression.h"
|
#include "src/ast/type_constructor_expression.h"
|
||||||
|
#include "src/type_determiner.h"
|
||||||
|
|
||||||
namespace tint {
|
namespace tint {
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
using ValidatorTest = testing::Test;
|
class TypeDeterminerHelper {
|
||||||
|
public:
|
||||||
|
TypeDeterminerHelper()
|
||||||
|
: td_(std::make_unique<TypeDeterminer>(&ctx_, &mod_)) {}
|
||||||
|
|
||||||
|
TypeDeterminer* td() const { return td_.get(); }
|
||||||
|
ast::Module* mod() { return &mod_; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
Context ctx_;
|
||||||
|
ast::Module mod_;
|
||||||
|
std::unique_ptr<TypeDeterminer> td_;
|
||||||
|
};
|
||||||
|
|
||||||
|
class ValidatorTest : public TypeDeterminerHelper, public testing::Test {};
|
||||||
|
|
||||||
TEST_F(ValidatorTest, Import) {
|
TEST_F(ValidatorTest, Import) {
|
||||||
ast::Module m;
|
ast::Module m;
|
||||||
|
@ -64,58 +79,88 @@ TEST_F(ValidatorTest, Import) {
|
||||||
|
|
||||||
TEST_F(ValidatorTest, Import_Fail_NotGLSL) {
|
TEST_F(ValidatorTest, Import_Fail_NotGLSL) {
|
||||||
ast::Module m;
|
ast::Module m;
|
||||||
m.AddImport(std::make_unique<ast::Import>(Source{1, 1}, "not.GLSL", "glsl"));
|
m.AddImport(
|
||||||
|
std::make_unique<ast::Import>(Source{12, 34}, "not.GLSL", "glsl"));
|
||||||
|
|
||||||
tint::ValidatorImpl v;
|
tint::ValidatorImpl v;
|
||||||
EXPECT_FALSE(v.CheckImports(m));
|
EXPECT_FALSE(v.CheckImports(m));
|
||||||
ASSERT_TRUE(v.has_error());
|
ASSERT_TRUE(v.has_error());
|
||||||
EXPECT_EQ(v.error(), "1:1: v-0001: unknown import: not.GLSL");
|
EXPECT_EQ(v.error(), "12:34: v-0001: unknown import: not.GLSL");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(ValidatorTest, Import_Fail_Typo) {
|
TEST_F(ValidatorTest, Import_Fail_Typo) {
|
||||||
ast::Module m;
|
ast::Module m;
|
||||||
m.AddImport(
|
m.AddImport(
|
||||||
std::make_unique<ast::Import>(Source{1, 1}, "GLSL.std.4501", "glsl"));
|
std::make_unique<ast::Import>(Source{12, 34}, "GLSL.std.4501", "glsl"));
|
||||||
|
|
||||||
tint::ValidatorImpl v;
|
tint::ValidatorImpl v;
|
||||||
EXPECT_FALSE(v.CheckImports(m));
|
EXPECT_FALSE(v.CheckImports(m));
|
||||||
ASSERT_TRUE(v.has_error());
|
ASSERT_TRUE(v.has_error());
|
||||||
EXPECT_EQ(v.error(), "1:1: v-0001: unknown import: GLSL.std.4501");
|
EXPECT_EQ(v.error(), "12:34: v-0001: unknown import: GLSL.std.4501");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(ValidatorTest, DISABLED_AssignToScalar_Fail) {
|
TEST_F(ValidatorTest, DISABLED_AssignToScalar_Fail) {
|
||||||
// 1 = my_var
|
// 1 = my_var;
|
||||||
ast::type::I32Type i32;
|
ast::type::I32Type i32;
|
||||||
|
|
||||||
auto lhs = std::make_unique<ast::ScalarConstructorExpression>(
|
auto lhs = std::make_unique<ast::ScalarConstructorExpression>(
|
||||||
std::make_unique<ast::SintLiteral>(&i32, 1));
|
std::make_unique<ast::SintLiteral>(&i32, 1));
|
||||||
auto rhs = std::make_unique<ast::IdentifierExpression>("my_var");
|
auto rhs = std::make_unique<ast::IdentifierExpression>("my_var");
|
||||||
ast::AssignmentStatement assign(std::move(lhs), std::move(rhs));
|
ast::AssignmentStatement assign(Source{12, 32}, std::move(lhs),
|
||||||
|
std::move(rhs));
|
||||||
|
|
||||||
tint::ValidatorImpl v;
|
tint::ValidatorImpl v;
|
||||||
// TODO(sarahM0): Invalidate assignment to scalar.
|
// TODO(sarahM0): Invalidate assignment to scalar.
|
||||||
ASSERT_TRUE(v.has_error());
|
ASSERT_TRUE(v.has_error());
|
||||||
EXPECT_EQ(v.error(), "1:1: v-000x: invalid assignment");
|
// TODO(sarahM0): figure out what should be the error number.
|
||||||
|
EXPECT_EQ(v.error(), "12:34: v-000x: invalid assignment");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(ValidatorTest, DISABLED_AssignUncompatibleTypes_Fail) {
|
TEST_F(ValidatorTest, AssignIncompatibleTypes_Fail) {
|
||||||
// var a :i32;
|
// var a :i32;
|
||||||
// a = 2.3
|
// a = 2.3;
|
||||||
ast::type::F32Type f32;
|
ast::type::F32Type f32;
|
||||||
ast::type::I32Type i32;
|
ast::type::I32Type i32;
|
||||||
|
|
||||||
ast::Variable var("a", ast::StorageClass::kPrivate, &i32);
|
ast::Variable var("a", ast::StorageClass::kPrivate, &i32);
|
||||||
auto lhs = std::make_unique<ast::IdentifierExpression>("a");
|
auto lhs = std::make_unique<ast::IdentifierExpression>("a");
|
||||||
|
auto lhs_ptr = lhs.get();
|
||||||
auto rhs = std::make_unique<ast::ScalarConstructorExpression>(
|
auto rhs = std::make_unique<ast::ScalarConstructorExpression>(
|
||||||
std::make_unique<ast::FloatLiteral>(&f32, 2.3f));
|
std::make_unique<ast::FloatLiteral>(&f32, 2.3f));
|
||||||
|
auto rhs_ptr = rhs.get();
|
||||||
|
|
||||||
ast::AssignmentStatement assign(std::move(lhs), std::move(rhs));
|
ast::AssignmentStatement assign(Source{12, 34}, std::move(lhs),
|
||||||
|
std::move(rhs));
|
||||||
|
td()->RegisterVariableForTesting(&var);
|
||||||
|
EXPECT_TRUE(td()->DetermineResultType(&assign)) << td()->error();
|
||||||
|
ASSERT_NE(lhs_ptr->result_type(), nullptr);
|
||||||
|
ASSERT_NE(rhs_ptr->result_type(), nullptr);
|
||||||
|
|
||||||
tint::ValidatorImpl v;
|
tint::ValidatorImpl v;
|
||||||
// TODO(SarahM0): Invalidate assignments of different types.
|
EXPECT_FALSE(v.ValidateAssign(assign));
|
||||||
ASSERT_TRUE(v.has_error());
|
ASSERT_TRUE(v.has_error());
|
||||||
EXPECT_EQ(v.error(), "1:1: v-000x: invalid assignment");
|
// TODO(sarahM0): figure out what should be the error number.
|
||||||
|
EXPECT_EQ(v.error(),
|
||||||
|
"12:34: v-000x: invalid assignment of '__i32' to '__f32'");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(ValidatorTest, AssignCompatibleTypes_Pass) {
|
||||||
|
// var a :i32;
|
||||||
|
// a = 2;
|
||||||
|
ast::type::I32Type i32;
|
||||||
|
|
||||||
|
ast::Variable var("a", ast::StorageClass::kPrivate, &i32);
|
||||||
|
auto lhs = std::make_unique<ast::IdentifierExpression>("a");
|
||||||
|
auto rhs = std::make_unique<ast::ScalarConstructorExpression>(
|
||||||
|
std::make_unique<ast::SintLiteral>(&i32, 2));
|
||||||
|
|
||||||
|
ast::AssignmentStatement assign(Source{12, 34}, std::move(lhs),
|
||||||
|
std::move(rhs));
|
||||||
|
|
||||||
|
td()->RegisterVariableForTesting(&var);
|
||||||
|
EXPECT_TRUE(td()->DetermineResultType(&assign)) << td()->error();
|
||||||
|
tint::ValidatorImpl v;
|
||||||
|
EXPECT_TRUE(v.ValidateAssign(assign));
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
Loading…
Reference in New Issue