From 21244880f8a9bd793856decfbcf6c3071ac17c27 Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Wed, 22 Apr 2020 00:23:49 +0000 Subject: [PATCH] Expression results should not be aliases. This CL updates the expression class to set the alias value instead of an alias as the result type. Change-Id: If19ae394a09ba0dc76380514e53a488bbb5a7292 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/20140 Reviewed-by: David Neto --- BUILD.gn | 1 + src/CMakeLists.txt | 1 + src/ast/expression.cc | 9 ++++++ src/ast/expression.h | 2 +- src/ast/expression_test.cc | 57 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 src/ast/expression_test.cc diff --git a/BUILD.gn b/BUILD.gn index 299e340054..e3e4254687 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -451,6 +451,7 @@ source_set("tint_unittests_core_src") { "src/ast/decorated_variable_test.cc", "src/ast/else_statement_test.cc", "src/ast/entry_point_test.cc", + "src/ast/expression_test.cc", "src/ast/fallthrough_statement_test.cc", "src/ast/float_literal_test.cc", "src/ast/function_test.cc", diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7c0f91368a..12bbea807e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -267,6 +267,7 @@ set(TINT_TEST_SRCS ast/decorated_variable_test.cc ast/else_statement_test.cc ast/entry_point_test.cc + ast/expression_test.cc ast/fallthrough_statement_test.cc ast/float_literal_test.cc ast/function_test.cc diff --git a/src/ast/expression.cc b/src/ast/expression.cc index e12dc278e3..402b8ebac9 100644 --- a/src/ast/expression.cc +++ b/src/ast/expression.cc @@ -24,6 +24,7 @@ #include "src/ast/constructor_expression.h" #include "src/ast/identifier_expression.h" #include "src/ast/member_accessor_expression.h" +#include "src/ast/type/alias_type.h" #include "src/ast/unary_derivative_expression.h" #include "src/ast/unary_method_expression.h" #include "src/ast/unary_op_expression.h" @@ -37,6 +38,14 @@ Expression::Expression(const Source& source) : Node(source) {} Expression::~Expression() = default; +void Expression::set_result_type(type::Type* type) { + // The expression result should never be an alias type + while (type->IsAlias()) { + type = type->AsAlias()->type(); + } + result_type_ = type; +} + bool Expression::IsArrayAccessor() const { return false; } diff --git a/src/ast/expression.h b/src/ast/expression.h index da20aa8bb6..1ad81d0244 100644 --- a/src/ast/expression.h +++ b/src/ast/expression.h @@ -43,7 +43,7 @@ class Expression : public Node { /// Sets the resulting type of this expression /// @param type the result type to set - void set_result_type(type::Type* type) { result_type_ = type; } + void set_result_type(type::Type* type); /// @returns the resulting type from this expression type::Type* result_type() const { return result_type_; } diff --git a/src/ast/expression_test.cc b/src/ast/expression_test.cc new file mode 100644 index 0000000000..de72167d8c --- /dev/null +++ b/src/ast/expression_test.cc @@ -0,0 +1,57 @@ +// 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 "src/ast/expression.h" + +#include "gtest/gtest.h" +#include "src/ast/type/alias_type.h" +#include "src/ast/type/i32_type.h" + +namespace tint { +namespace ast { +namespace { + +class Expr : public Expression { + public: + Expr() : Expression() {} + + bool IsValid() const override { return true; } + void to_str(std::ostream&, size_t) const override {} +}; + +using ExpressionTest = testing::Test; + +TEST_F(ExpressionTest, set_result_type) { + type::I32Type i32; + + Expr e; + e.set_result_type(&i32); + ASSERT_NE(e.result_type(), nullptr); + EXPECT_TRUE(e.result_type()->IsI32()); +} + +TEST_F(ExpressionTest, set_result_type_alias) { + type::I32Type i32; + type::AliasType a("a", &i32); + type::AliasType b("b", &a); + + Expr e; + e.set_result_type(&b); + ASSERT_NE(e.result_type(), nullptr); + EXPECT_TRUE(e.result_type()->IsI32()); +} + +} // namespace +} // namespace ast +} // namespace tint \ No newline at end of file